[FE training-materials-updates] serial/output lab: insist on the reasons why the container_of trick works

Michael Opdenacker michael.opdenacker at free-electrons.com
Fri Nov 18 08:59:34 CET 2016

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


commit ba1bec3fe9abe522f70c2ab1381c33c3a0e83037
Author: Michael Opdenacker <michael.opdenacker at free-electrons.com>
Date:   Fri Nov 18 08:59:34 2016 +0100

    serial/output lab: insist on the reasons why the container_of trick works
    Signed-off-by: Michael Opdenacker <michael.opdenacker at free-electrons.com>


 labs/kernel-serial-output/kernel-serial-output.tex | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/labs/kernel-serial-output/kernel-serial-output.tex b/labs/kernel-serial-output/kernel-serial-output.tex
index ab28791..25ceaa0 100644
--- a/labs/kernel-serial-output/kernel-serial-output.tex
+++ b/labs/kernel-serial-output/kernel-serial-output.tex
@@ -94,8 +94,9 @@ from the \code{miscdevice} structure itself, accessible through the
 \code{private_data} field of the open file structure (\code{file}).
 At the time we registered our {\em misc} device, we didn't keep any
-pointer to the \code{feserial_dev} structure. However, the
-\code{miscdevice} structure is accessible, and being a member of the
+pointer to the \code{feserial_dev} structure. However, as the
+\code{miscdevice} structure is accessible through
+\code{file->private_data}, and is a member of the
 \code{feserial_dev} structure, we can use a magic macro to compute
 the address of the parent structure:
@@ -107,6 +108,10 @@ struct feserial_dev *dev =
 See \url{http://linuxwell.com/2012/11/10/magical-container_of-macro/}
 for interesting implementation details about this macro.
+This wouldn't have been possible if the \code{miscdevice} structure
+was allocated separately and was just referred to by a pointer in
+\code{feserial_dev}, instead of being a member of it.
 Now, add code that copies (in a secure way) each character from the
 user space buffer to the UART device.

More information about the training-materials-updates mailing list