Operating Systems and Embedded Software · Kernel
Kernel Module
Also known as loadable kernel module, LKM, .ko file, out-of-tree module
A unit of kernel code, typically a device driver or file system, compiled separately as a .ko file and loaded into or removed from a running Linux kernel without rebooting.
Explanation
Modules let a single kernel image support many hardware variants, because drivers are loaded only when the matching hardware or feature is present. They are built with kbuild, the kernel's build system, either in-tree as part of the kernel source or out-of-tree as external modules. External modules must be compiled against the headers and configuration of the kernel they will run on.
Tools such as modprobe load a module and its dependencies from /lib/modules/<kernel version>, applying options from configuration files or the kernel command line. A module runs with full kernel privileges, so a bug can crash the whole system. The kernel's module signing facility can refuse unsigned modules, or modules signed with an invalid key, which is an important control in a secure boot chain.
Out-of-tree modules carry a maintenance cost. The kernel documentation explains that internal kernel interfaces are deliberately not kept stable, so the most reliable way to keep a driver working across upgrades is to have it in the main kernel tree. Licensing obligations also apply.
How it works
A module defines init and exit functions and declares its licence and metadata. kbuild compiles it to a .ko object and, when CONFIG_MODVERSIONS is enabled, records symbol versions. At load time (insmod or modprobe) the kernel checks the signature if enforcement is enabled, resolves symbols exported by the kernel or other modules, and runs the init function, which typically registers a driver. The exit function runs when the module is removed.
Automotive applications
- Loading CAN, Ethernet PHY or camera drivers per hardware variant
- Shipping vendor drivers for SoC accelerators
- Debug and tracing helpers during development
- Optional features enabled only in some product variants
Example
A head-unit image ships one kernel for two hardware revisions. At boot, modprobe loads whichever audio-codec module matches the fitted codec. Both modules are signed with the OEM key, and the kernel is configured to reject unsigned modules.
An illustrative example: any figures in it are worked numbers, not measurements.
References
- Building External Modules (opens in a new tab)
- Kernel module signing facility (opens in a new tab)
“The kernel module signing facility cryptographically signs modules during installation and then checks the signature upon loading the module.”
- The Linux Kernel Driver Interface (stable API nonsense) (opens in a new tab)
Definitions are original summaries. Standards text is not reproduced; follow the links for the official publications.