Passing Parameters to Linux Kernel Modules
Passing Parameters to Linux Kernel Modules, this is our next lecture in free linux kernel development course, so welcome back to embeddedpathashala, where we offer free embedded systesms courses online, please visit our webiste embeddedpathashala.com for more courses.
Let us start our new lecture Passing Parameters to Linux Kernel Modules, just like normal c program where we will pass the parameters using command line arguments for example
int main(int argc, char *argv[])
this is simple c syntax where we will pass the command line arguments, similarly in linux kernel programming also we have an option to pass the arguments which we call as parameters, so passing parameters in linux kernel modules is done when we are inserting the module itself.
This prameters or modules ill help driver or the module to behave differently based on the runtime cofigurations. For example: enable or disable the debugging logs, configure the driver mode, set buffer sizes, configure the device ids or enable optional features.
Instead of recompiling the module for every change we are passing parameters to linux kernel module during the load time.
How to pass parameters to kernel modules
module_param macro is used to define parameters in linux kernel, this macro is declared in
#include <linux/moduleparam.h>
syntax
module_param(name, type, perm);
let us see more about passing parameters in linux kenrel.
Arguments:
- Name: variable name is defined in the module for example
int loop_count;
next, we will see other parameter
2. type: this defines the datatype of the parameter, below are supported types for the linux kernel parameters.
| Type | Description |
| ------- | -------------------------- |
| charp | character pointer (string) |
| bool | boolean |
| invbool | inverted boolean |
| int | integer |
| long | long |
| short | short |
| uint | unsigned int |
| ulong | unsigned long |
| ushort | unsigned short |
so when you are passing parameters in linux kernel modules you should define a dataype.
3. permissions: permissions of the parameter entry are created in sysfs, below are few examples of permissions.
| Permission | Meaning |
| ---------- | ---------------------- |
| S_IRUGO | Readable by all users |
| S_IWUSR | Writable by owner |
| 0 | No sysfs entry created |
below is simple example
module_param(loop_count, int, S_IRUGO);
below is the complete source code for Passing Parameters to Linux Kernel Modules
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/moduleparam.h>
MODULE_LICENSE(“GPL”);
/* module parameters */
char *name = “embed”;
int loop_count = 1;
module_param(name, charp, S_IRUGO);
module_param(loop_count, int, S_IRUGO);
static int arg_test_init(void)
{
int i;
printk(KERN_INFO “%s: module init\n”, __func__);
printk(KERN_INFO “%s: loop_count=%d\n”, __func__, loop_count);
for(i = 0; i < loop_count; i++)
{
printk(KERN_INFO “%s: I am %s\n”, __func__, name);
}
return 0;
}
static void arg_test_exit(void)
{
printk(KERN_INFO “%s: module exit\n”, __func__);
}
module_init(arg_test_init);
module_exit(arg_test_exit);
let us analyse code,
module parameters:
char *name = "embed";
int loop_count = 1;
these are global variables that will receive the values from the module parameters, below are the default values
name = "embed"
loop_count = 1
if the user dont pass any parameters then this default parameters are considered.
module_param macro
module_param(name, charp, S_IRUGO);
this registerst the name as module parameters
| field | meaning |
| ------- | --------------- |
| name | variable name |
| charp | string type |
| S_IRUGO | read permission |
so above are the files that when passing parameters to linux kernel module
module_param(loop_count, int, S_IRUGO);
registerst the loop_count parameter.
init function
static int arg_test_init(void)
this is the function that gets executed when the kernel module is loaded, you can see below operations when a module is inserted.
Steps:
-
Prints initialization message
-
Prints parameter value
-
Loops
loop_counttimes -
Prints name
exit function
static void arg_test_exit(void)
this function is executed when a module is removed,
module_init and module_exit functions
module_init(arg_test_init);
module_exit(arg_test_exit);
below are steps to compile the module
Makefile to compile a linux kernel module:
obj-m += module_param.o
all:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
to compile linux kenrel module
make
output file generated
module_param.ko
so this is the file that we will load into linux kernel.
Loading the linux kernel modules with out parameters
Now let us see how we can load the linux kernel modules with out passing parameters
sudo insmod module_param.ko
to check output
dmesg
below is example output
arg_test_init: module init
arg_test_init: loop_count=1
arg_test_init: I am kernel
you can see this output in dmesg logs.
Now we will see how passing parameters to linux kernel module works
Passing Parameters to kernel module
sudo insmod module_param.ko loop_count=3
loop_count=3
I am kernel
I am kernel
I am kernel
example 2
sudo insmod module_param.ko loop_count=3 name="KERN_ARG"
I am KERN_ARG
I am KERN_ARG
I am KERN_ARG
so you can observe how output is being varied when we are passing parameters to kernel module, this passing parameters to kernel modules help us to acheive the required functionality based on scenario, we can modify the functionality based on the parameters we send.
What happens when a wrong value is passed as parameter to kernel module?
now let us study what will happen when we pass wrong parameter to the kenrel module.
sudo insmod module_param.ko loop_count=2.5
or
sudo insmod module_param.ko loop_count=text
result: module will not load, and below error appear in dmesg
insmod: ERROR: could not insert module param_array.ko: Invalid parameters
how to check module parameters?
to check the parameters for the modules already loaded you can give modinfo <module_name.ko> below is the sample output
raviteja@raviteja-Inspiron-15-3511:~/lsyspg/kernel_programming/Day6/param_array$ modinfo param_array.ko
filename: /home/raviteja/lsyspg/kernel_programming/Day6/param_array/param_array.ko
license: GPL
srcversion: 0E709F7C9003C83D8D1145E
depends:
retpoline: Y
name: param_array
vermagic: 6.8.0-101-generic SMP preempt mod_unload modversions
parm: param_array:array of int
now let us see how to check parameters of loaded module.
Checking Parameters of Loaded Module:
when a module is loaded it will create an entry in sysfs and below is the location
/sys/module//parameters/
for example
cd /sys/module/module_param/parameters
ls
example output,
loop_count
name
to read the parameter values
cat loop_count
cat name
so i hope you got understanding about passing parameters to linux kernel modules.
What will happen if permission is 0
so in the previous lecture when passing parameters to linux kenrel module we studied about parameter permission, now let us see what will happen when we set permission 0? example:
module_param(name, charp, 0);
then you can see the below output
/sys/module/module_param/parameters/
then you will not show this parameter, but modinfo still shows this parameter.
Passing an array to linux kernel modules:
In this lecture i will explain you about how to pass an array to linux kernel module
#include<linux/kernel.h>
#include<linux/module.h>
#include<linux/moduleparam.h>
MODULE_LICENSE("GPL");
int param_array[4];
static int argc_count = 0;
module_param_array(param_array, int, &argc_count, S_IWUSR | S_IRUSR);
static int test_arguments_init(void)
{
int i=0;
printk("%s\n",__func__);
printk("ARGC count:%d\n", argc_count);
printk("Array elements \n");
for(i=0;i<sizeof(param_array)/sizeof(param_array[i]);i++) {
printk("%d:\t%d\n",i,param_array[i]);
}
return 0;
}
static void test_arguments_exit(void)
{
printk("%s\n",__func__);
}
module_init(test_arguments_init);
module_exit(test_arguments_exit);
Makefile:
obj-m := param_array.o
all:
make -C /lib/modules/$(shell uname -r)/build M=${PWD} modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=${PWD} clean
How to pass an array to kernel module:
raviteja@raviteja-Inspiron-15-3511:~/lsyspg/kernel_programming/Day6/param_array$ sudo insmod param_array.ko param_array=1,2,3,4
output:
[ 6327.663758] test_arguments_init
[ 6327.663766] ARGC count:4
[ 6327.663768] Array elements
[ 6327.663769] 0: 1
[ 6327.663771] 1: 2
[ 6327.663772] 2: 3
[ 6327.663773] 3: 4
Passing parameters to built-in linux kernel modules:
Till now we studied how to pass the parameters to out of tree linux kenrel modules, now we will understand how to pass the parameters to built-in linux kenrel modules.
built-in modules are compiled inside linux kernel and we apss parameters during boot only. we will use below syntax
module_name.parameter=value
below is example
usbcore.autosuspend=-1
these are added in the below
GRUB_CMDLINE_LINUX
now let us see how we can pass parameters using modprobe
Inserting the module using modprobe:
modprobe is used to load a Linux kernel module along with its dependencies automatically.
Unlike insmod, you don’t need to specify the full path or worry about dependent modules, remember onething modprobe uses module dependency database which is generated by depmod which only scans from below path
/lib/modules/kernel-version/
but insmod simply loads the module directly from the given path, and there will be no dependency checking
example:
sudo modprobe mac80211
| Command | Use Case |
| ---------------- | ---------------------------------- |
| insmod module.ko | Load module from current directory |
| rmmod module | Remove module |
| modprobe module | Load module from `/lib/modules` |
| depmod | Update module dependency database |
this gives a summary about different commands we use
