Linux Device Drivers
Loadable kernel modules and statically linked modules
Comparison between statically linked modules and loadable kernel modules
Statically linked modules
- They are linked to the kernel images
- They are loaded as part of the kernel during boot time, so there is no need to access them via some root file system
- For implementing any changes to statically linked modules, one needs to edit, recompile the kernel and afterwards reboot to load the new Linux Kernel
Loadable Kernel Modules (LKM) = dynamic modules
- They utilize by convention the suffix .ko which stands for “kernel object”
- The Loadable Kernel Modules consist of files in the file system with the extension .ko and we can load them and manage them without the need for rebooting
- Loadable Kernel Modules are to be loaded only after the Linux Kernel was loaded
- If one needs to modify a dynamic kernel module (Loadable Kernel module), then one needs to edit it, recompile it, remove the existing module and load the newly compiled module. All these steps do not involve the need for booting.
Note: Not all Linux Kernel modules can be made as dynamic modules (Loadable Kernel Modules). If the kernel module is required for booting properly, then this kernel module has to be statically linked to the kernel, and not dynamically loaded later.
Advantages of the loadable kernel modules:
- they avoid rebooting
- they allow faster development cycles.
The insmod command can be used for loading a dynamic kernel module (loadable kernel module) without loading the other modules that this module is depending upon. This has to be run under root privileges. The Linux Kernel is already running and we are adding code it (could be device drivers or other things).
Using insmod requires that all its dependencies are already loaded. Insmode adds modules to already loaded modules. This means that with insmod the modules have to be loaded in a particular order.
The rmmod command is used for removing dynamically loaded kernel modules (Loadable Kernel Modules). When one appeals to the rmmod command to remove a module, the kernel checks whether the module is part of a running process, and if that module is being used, then the rmmod command will return an error and it will not remove the module.
Although, one can force the execution of the rmmod command using the flag -f rmmod -f …, and it will remove even modules being used, but this is not advisable.
lsmod command lists all the modules that have been dynamically loaded. It gets information from
/sys/modules/*. The order of the dynamically loaded kernel modules shown with lsmod is the chronological one, the newest loaded modules can be found at the top of the list.
The modules that are depending on other modules are called stacked modules.
The depmod command creates the module dependency information. One can use the depmod command to see all modules that a given loadable kernel modules is depending upon before inserting this module with insmod.
modinfo command prints information about the module. It extracts the information from the module file. modinfo allows plenty of flags.
The modprobe command allows loading a loadable kernel module that accounts and loads also all its dependencies. With modprobe the kernel loads all the necessary Linux Kernel Modules which had to be downloaded before. In the case of loading the same Linux Kernel Modules with insmod, it would return a dependency error.
modprobe goes to the dependency file and based on the listing in the dependency file for that particular module, it loads those modules before loading the actual module.
- insmod to load a loadable module that is in the current directory,
- modprobe to load a module from
/lib/modules/*, cause modprobe always look for the module with that name residing under
/lib/modulesand not the current directory.
Configuration and blacklist files, define and use parameters with the Loadable Kernel Modules (LKM)
When you load Loadable Kernel modules, you can provide some initial values by means of variables that are configured as parameters.
If you want to consider configuration for the modules, you need to look into
/etc/modules.d. For parameters of the modules, one can use modinfo -p to list the parameters that a module has.
The directory /etc/modules.d
- the directory /etc/modules.d contains a number of configuration files affecting modprobe.
- The configuration files automatically set parameters when using modprobe
Blacklist mark the modules to never load. They are used for some reason (e.g. you intend to use other drivers than the ones in the blacklist file).
This command displays all the parameter types and names of a loadable kernel module.
How to define the parameters of a loadable kernel module
There exist special types of macros to define and document the parameters of a Loadable Kernel Module.
module_param(variable, int, S_IRWXU) : to define the loadable kernel module
MODULE_PARM_DESC(variable,"description of the parameter here"); : to document the loadable kernel module
How to pass the parameters to a loadable kernel module
The directory /sys/module
- each loadable kernel module has a subdirectory inside /sys/module
- the subdirectory is called “parameters” if that module has parameters
- if the parameter files are writable: then, you can change parameters via command line to be considered when the module loads next time