You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: lkmpg.tex
+8-5Lines changed: 8 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -1095,16 +1095,17 @@ \subsection{Unregistering A Device}
1095
1095
You can see what its value is by looking at the 3rd field with the command \sh|cat /proc/modules| or \sh|lsmod|.
1096
1096
If this number isn't zero, \sh|rmmod| will fail.
1097
1097
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}.
1098
-
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:
1098
+
You should not use this counter directly, but there are functions defined in \src{include/linux/module.h} which let you display this counter:
1099
1099
1100
1100
\begin{itemize}
1101
-
\item\cpp|try_module_get(THIS_MODULE)|: Increment the reference count of current module.
1102
-
\item\cpp|module_put(THIS_MODULE)|: Decrement the reference count of current module.
1103
1101
\item\cpp|module_refcount(THIS_MODULE)|: Return the value of reference count of current module.
1104
1102
\end{itemize}
1105
1103
1106
-
It is important to keep the counter accurate; if you ever lose track of the correct usage count, you will never be able to unload the module; it is now reboot time.
1107
-
This is bound to happen to you sooner or later during a module's development.
1104
+
Note: The use of \cpp|try_module_get(THIS_MODULE)| and \cpp|module_put(THIS_MODULE)| within a module's own code is considered unsafe and should be avoided.
1105
+
The kernel automatically manages the reference count when file operations are in progress,
1106
+
so manual reference counting is unnecessary and can lead to race conditions.
1107
+
For a deeper understanding of when and how to properly use module reference counting,
1108
+
see \url{https://stackoverflow.com/questions/1741415/linux-kernel-modules-when-to-use-try-module-get-module-put}.
The inode contains information about the file, for example the file's permissions, together with a pointer to the disk location or locations where the file's data can be found.
1153
1154
1154
1155
Because we do not get called when the file is opened or closed, there is nowhere for us to put \cpp|try_module_get| and \cpp|module_put| in this module, and if the file is opened and then the module is removed, there is no way to avoid the consequences.
1156
+
The kernel's automatic reference counting for file operations helps prevent module removal while files are in use,
1157
+
but \verb|/proc| files require careful handling due to their different lifecycle.
1155
1158
1156
1159
Here is a simple example showing how to use a \verb|/proc| file.
1157
1160
This is the HelloWorld for the \verb|/proc| filesystem.
0 commit comments