@@ -585,6 +585,38 @@ \subsection{Passing Command Line Arguments to a Module}
585585
586586\samplec {examples/hello-5.c}
587587
588+ If you need to validate a parameter or react whenever it changes, use \cpp |module_param_cb()|.
589+ This variant takes four arguments: the parameter name, a pointer to a \cpp |struct kernel_param_ops|,
590+ an argument pointer (typically the address of the backing variable), and the sysfs permissions.
591+ The callbacks stored in \cpp |struct kernel_param_ops| let you override the \cpp |set| and \cpp |get|
592+ operations instead of relying on the default helpers such as \cpp |param_set_int()| and \cpp |param_get_int()|.
593+ With non-zero permissions, the parameter is also exposed through sysfs so the same callbacks can run after the module has already been loaded.
594+
595+ \begin {code }
596+ static int watched = 42;
597+
598+ static int watched_set(const char *val, const struct kernel_param *kp)
599+ {
600+ int ret = param_set_int(val, kp);
601+
602+ if (ret)
603+ return ret;
604+
605+ pr_info("watched updated to % d\n", watched);
606+ return 0;
607+ }
608+
609+ static const struct kernel_param_ops watched_ops = {
610+ .set = watched_set,
611+ .get = param_get_int,
612+ };
613+
614+ module_param_cb(watched, &watched_ops, &watched, 0644);
615+ \end {code }
616+
617+ The full example below also overrides \cpp |.get| so that every sysfs read is logged:
618+ \samplec {examples/hello-6.c}
619+
588620It is recommended to experiment with the following code:
589621\begin {verbatim }
590622$ sudo insmod hello-5.ko mystring="bebop" myintarray=-1
@@ -611,12 +643,30 @@ \subsection{Passing Command Line Arguments to a Module}
611643myintarray[1] = -1
612644got 2 arguments for myintarray.
613645
614- $ sudo rmmod hello_5
646+ $ sudo rmmod hello-5
615647$ sudo dmesg -t | tail -1
616648Goodbye, world 5
617649
618650$ sudo insmod hello-5.ko mylong=hello
619651insmod: ERROR: could not insert module hello-5.ko: Invalid parameters
652+
653+ $ sudo insmod hello-6.ko watched=7
654+ $ sudo dmesg -t | tail -3
655+ watched updated to 7
656+ Hello, world 6
657+ watched starts at 7
658+
659+ $ cat /sys/module/hello_6/parameters/watched
660+ 7
661+ $ sudo dmesg -t | tail -1
662+ watched was read
663+
664+ $ echo 21 | sudo tee /sys/module/hello_6/parameters/watched
665+ 21
666+ $ sudo dmesg -t | tail -1
667+ watched updated to 21
668+
669+ $ sudo rmmod hello-6
620670\end {verbatim }
621671
622672\subsection {Modules Spanning Multiple Files }
@@ -637,6 +687,7 @@ \subsection{Modules Spanning Multiple Files}
637687obj-m += hello-3.o
638688obj-m += hello-4.o
639689obj-m += hello-5.o
690+ obj-m += hello-6.o
640691obj-m += startstop.o
641692startstop-objs := start.o stop.o
642693
0 commit comments