A kernel module is loaded into kernel space. In case any specific value needs to be sent to the Kernel module during initialization or dynamically at run-time, a method needs to be provided which will achieve the same.
One such method to achieve the above requirement is the “module_param” macro. The “module_param” macro takes in three parameters. They are
- Name of the parameter
- type of the parameter
- The access rights provided to the parameter (This is because a file with the same name as the parameter is created in sysfs file system)
The Sample program to send values to the kernel is shown below
When we compile the above code as a kernel module without passing any arguments to the module and invoke “modinfo” command on the kernel module which is compiled- we get the below output
It can be seen that the Integer parameter and the char string variable are defined for the module. On kernel module load – we get the below output
As can be seen above the values of the integer variable and the string are as defined in the program. We shall run the “rmmod command” to remove the module and the output is shown below. The string parameter and the Integer parameter are the same values as printed out during the kernel module initialization.
Passing Values during Module Load
Now, if we pass load time arguments to the kernel module (as is done for argv for a Main function of a process) – the executed code and output is shown .
run the command for our code example – sudo insmod module_param_code.ko Intparameter=20 charparameter='”Hollow Hollow world”‘
As can be seen from the dmesg output – the integer value and the character string is now changed to the values passed to the module during module load.
NOTE: When a string variable is passed to the kernel module – the string should be enclosed with double quotes and externally with single quotes. The single quotes are used by the shell in which the insmod command is invoked and the double quotes string literal is passed on to the kernel module
Changing the values of the Parameters at run-time
The Intparameter and the charparameter are stored as files in /sys/module/<module_name>/parameters.
In the case of our above example – it would be /sys/module/module_param_code/parameters.
If the value of the file is modified, the parameter value used by the module is also modified. This allows a user to modify the values passed to the module at run-time. since, the access permissions we have provided to the file is 0644 – only root user can modify the file.
Invoking the “rmmod” command prints out the modified value of the integer parameter and the character parameter. The output is shown below
In the next article, we will explore how the procfs and sysfs filesystems work.