Linux kernel modules are pieces of code that can be loaded and unloaded into the Linux kernel dynamically. They extend the functionality of the kernel without the need to reboot the system. Kernel modules can add device drivers, file system support, networking protocols, and more to the kernel at runtime.
Dynamic Loading: Modules can be loaded and unloaded from the kernel dynamically, allowing for flexibility and resource efficiency.Efficient Memory Usage: Modules can be loaded only when needed, conserving memory resources.Easy Maintenance: Modules can be updated or removed without rebooting the system, simplifying maintenance tasks.Modular Design: The modular design of the Linux kernel allows for easier development and testing of new features.
Writing kernel modules requires understanding of the Linux kernel's internals, including data structures, APIs, and kernel space programming techniques. Kernel modules are typically written in C, although other languages like Rust are also being explored.
Kernel modules are compiled separately from the kernel itself. Developers need the appropriate kernel headers and build tools installed on their system to compile modules. The Makefile is commonly used to automate the compilation process.Modules are built using kbuild which is the build system used by the Linux kernel, Doesn't matter whether it is a static module or dynamic module.
Modules can be loaded into the kernel using the insmod command or by placing them in the appropriate directory (/lib/modules) and using the modprobe command. They can be unloaded using the rmmod command.
-
Initialization: Modules initialize themselves when they are loaded into the kernel. This involves allocating resources, registering device drivers, and setting up any necessary data structures. -
Functionality: Once initialized, modules provide their intended functionality to the kernel. For example, a device driver module may handle communication with a hardware device. -
Cleanup: When a module is unloaded from the kernel, it must clean up any resources it allocated during initialization and unregister any device drivers it registered.
Kernel modules can accept parameters that modify their behavior. These parameters can be specified when loading the module using the insmod or modprobe commands. Module parameters are accessed within the module code and can be used to configure module behavior.
- Static vs Dynamic Modules
- Static Modules
- Integrated directly into the kernel image during the kernel build process.
- Part of the kernel image and always present, consuming memory even if not actively used ( Cannot be loaded or unloaded dynamically).
- Suitable for essential functionalities that need to be available at all times.
- Dynamic Modules
- Compiled separately from the kernel.
- Loaded into the kernel at runtime using utilities like
insmodormodprobeand can be unloaded usingrmmod.. - Provide flexibility by allowing kernel extensions to be loaded and unloaded as needed, conserving memory when not in use.
- Static Modules
- In-tree vs Out-of-tree Modules
- In-tree Modules
- Kernel modules that are part of the official Linux kernel source tree.
- Maintained and distributed along with the kernel source code.
- Subject to the same development and release cycle as the kernel.
- Out-of-tree Modules
- Kernel modules developed separately from the main Linux kernel source tree.
- Not part of the official kernel source code.
- Typically developed by third parties or as independent projects.
- In-tree Modules
Note:
-
Loading Out of tree module
taints the kernel.The kernel issues a warning saying Out of tree module has been loaded,This is just awarningfrom the kernel and we can safely ignore this warning -
Out of tree modules must use
kbuildto stay compatible with changes in the build infrastructure and to pick up right flags to GCC.You must have a prebuilt kernel source available that contains the configuration and header files used in the build The reason is, modules are linked against object files found in the kernel source tree.This ensures that as the developer changes the kernel configuration, his custom driver is automatically rebuilt with the correct kernel configuration.
There are two ways to obtain a prebuilt kernel version:
- Download kernel from your distributor and build it by yourself
- install the Linux-headers of the target Linux kernel