[BL training-materials-updates] labs/kernel-serial-output: fix module reference counting

Thomas Petazzoni thomas.petazzoni at bootlin.com
Thu Jul 19 14:09:01 CEST 2018


Repository : git://git.free-electrons.com/training-materials.git
On branch  : master
Link       : http://git.free-electrons.com/training-materials/commit/?id=094a6172a5765a7b35f50589b4eb4d803582530f

>---------------------------------------------------------------

commit 094a6172a5765a7b35f50589b4eb4d803582530f
Author: Thomas Petazzoni <thomas.petazzoni at bootlin.com>
Date:   Thu Jul 19 14:09:01 2018 +0200

    labs/kernel-serial-output: fix module reference counting
    
    Add a new step about .owner = THIS_MODULE, to get correct module
    reference counting.
    
    Signed-off-by: Thomas Petazzoni <thomas.petazzoni at bootlin.com>


>---------------------------------------------------------------

094a6172a5765a7b35f50589b4eb4d803582530f
 labs/kernel-serial-output/kernel-serial-output.tex | 24 ++++++++++++++++++++++
 1 file changed, 24 insertions(+)

diff --git a/labs/kernel-serial-output/kernel-serial-output.tex b/labs/kernel-serial-output/kernel-serial-output.tex
index fdd6b5e..6a65540 100644
--- a/labs/kernel-serial-output/kernel-serial-output.tex
+++ b/labs/kernel-serial-output/kernel-serial-output.tex
@@ -134,6 +134,30 @@ You'll quickly discover that newlines do not work properly. To fix
 this, when the user space application sends \verb+"\n"+, you must send
 \verb+"\n\r"+ to the serial port.
 
+\section{Module reference counting}
+
+Start an application in the background that writes nothing to the
+UART:
+
+\begin{verbatim}
+cat > /dev/serial-48024000 &
+\end{verbatim}
+
+Now, with \code{lsmod}, look at the reference count of your module: it
+is still 0, even though an application has your device opened. This
+means that you can \code{rmmod} your module even when an application
+is using it, which is not correct and can quickly cause a kernel
+crash.
+
+To fix this, we need to tell the kernel that our module is in charge
+of this character device. This is done by setting the \code{.owner}
+field of \code{struct file_operations} to the special value
+\code{THIS_MODULE}.
+
+After changing this, make sure the reference counter of your module,
+visible through \code{lsmod}, gets incremented when you run an
+application that uses the UART.
+
 \section{Going further: ioctl operation}
 
 Do it only if you finish ahead of the crowd!




More information about the training-materials-updates mailing list