From 467cc83cc77f258b0ec9218c5f8601e3626ad8a5 Mon Sep 17 00:00:00 2001 From: fennecJ <58484289+fennecJ@users.noreply.github.com> Date: Tue, 17 Aug 2021 00:23:22 +0800 Subject: [PATCH] Improve the descriptions about reference count (#62) The term "reference count" would be better than "use count." In addition, the usage of module_refcount(THIS_MODULE) was appended for the reference counter query. --- lkmpg.tex | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lkmpg.tex b/lkmpg.tex index c3935f4..7d1ece2 100644 --- a/lkmpg.tex +++ b/lkmpg.tex @@ -894,14 +894,15 @@ The results of this would be impossible to predict, but they can not be very pos Normally, when you do not want to allow something, you return an error code (a negative number) from the function which is supposed to do it. With \cpp|cleanup_module| that's impossible because it is a void function. However, there is a counter which keeps track of how many processes are using your module. -You can see what its value is by looking at the 3rd field of \verb|/proc/modules|. +You can see what its value is by looking at the 3rd field with the command \sh|cat /proc/modules| or \sh|sudo lsmod|. If this number isn't zero, \sh|rmmod| will fail. Note that you do not have to check the counter within \cpp|cleanup_module| because the check will be performed for you by the system call \cpp|sys_delete_module|, defined in \src{include/linux/syscalls.h}. You should not use this counter directly, but there are functions defined in \src{include/linux/module.h} which let you increase, decrease and display this counter: \begin{itemize} - \item \cpp|try_module_get(THIS_MODULE)|: Increment the use count. - \item \cpp|module_put(THIS_MODULE)|: Decrement the use count. + \item \cpp|try_module_get(THIS_MODULE)|: Increment the reference count of current module. + \item \cpp|module_put(THIS_MODULE)|: Decrement the reference count of current module. + \item \cpp|module_refcount(THIS_MODULE)|: Return the value of reference count of current module. \end{itemize} It is important to keep the counter accurate; if you ever do lose track of the correct usage count, you will never be able to unload the module; it's now reboot time, boys and girls. -- 2.39.5