Linux Kernel Development – Kernel Module Parameters

In the previous post , I covered the basics of kernel development with a simple example of loadable kernel module that has only init and exit functions. In this post we will add a parameters to the module. Using the parameters, you can access the module global variables while loading the module and on runtime while the module is already loaded.

Loading a module with parameters

When you load a module using insmod command you can supply the parameters as key=value pairs for example:

The parameter can be a number, a string or an array (of numbers or strings)

To declare a simple parameter in a module, declare a global variable and use module_param macro for example:

Now you can compile and load the module with irq, debug and devname parameters:

The global variable will be initialized before the init function called use dmesg to see the output:

module_param macro

The macro prototype is

name – name of an already defined variable

type – parameter type(int,long,charp,…)

perm – permissions for /sys/module/<module_name>/parameters/<param> (or 0 for no such module parameter value file)

 

Reading and Changing the parameter value

The file in /sys/module/<module_name>/parameters is used to read/write the parameter value while the module is loaded. For example if we loaded a module with debug=0 and we want to turn on debugging messages without reloading the module we can use the file:

You can see the debug variable changed while you unload the module

Validating the parameter value

Using the file in /sys , if we have permission, we can set any value we want. If for example the irq can only be in the range 1 – 32 , there is no way to validate it because the user access the variable directly. To solve this problem we can implement an interface for setting an getting parameter values – kernel_param_ops

To implement the interface we need to define a variable , implement the set/get methods and register it using module_param_cb:

The actual parameter value is saved in num , the set function get the user input as a string and convert it to int , then we check if the value is correct (1 – 32) and set it using the param_set_int function provided by the kernel. To return the parameter value we use the provided param_get_int function

If we try to load the module with wrong value or if we try to set a wrong value using the /sys file beget an error:

We can also define a custom function for get but in this example its not necessary. It will be useful if you want the parameter value provided using a string but saved as a number

For example if we want a parameter for interrupt mode to be one from level, edge, polling but we want to save it as a number (1 – level, 2-edge, 3-polling) we will define the set function to convert from string to int and the get function to convert from int to string

For Example:

To load the kernel module or change the parameter value, use a string:

 

Declare a module parameter array

You can declare a parameter as array of numbers or string using module_param_array. For example:

While loading the module, use comma separated list to provide the values:

count is an output parameter and updated with the number of element that provided by the user (in this example 3)

Some notes about module parameters:

  • If you load a module using modprobe, you can set the parameters in /etc/modprobe.conf (or other file – depends on your distribution)
  • If you compile the module with the kernel (static), you can provide the parameters using the kernel command line on boot time
  • There are more functions – look at the source code (moduleparam.h)

You can find the full source code here

 

 

Tagged

Leave a Reply

Your email address will not be published. Required fields are marked *