Part 6 of 8 · 2 min read
Modules and hybrids
Think of a power strip. The house wiring stays the same, and a new device is plugged in or removed without rewiring. A loadable module is plugged into the kernel in that way. The analogy stops in one place: a bad device trips a fuse, and a bad module runs inside the kernel and can crash it.
In plain words: Most real systems mix the ideas. The most common method today is the loadable kernel module (LKM). The kernel has a core of essential services, and more can be linked in as modules, either at boot or while the system runs. Linux, macOS, Solaris and Windows all do this.
The result resembles a layered system, since each part has a defined interface. It is more flexible, because any module may call any other. It resembles a microkernel, because the core is small and other services are added. And it is faster than a microkernel, because modules share the kernel's address space, so no message passing is needed.
The key lines.
Linux uses modules mainly for device drivers and file systems. Plug in a USB device and the kernel may load the matching driver at that moment. It can later remove it. This keeps the speed of a monolithic design while avoiding a rebuild of the kernel for each new device. On a Linux machine, the command lsmod lists the loaded modules. Output looks like this (made up to show the idea; real lists are longer):
Module Size Used by
usb_storage 81920 1
ext4 983040 2
snd_hda_intel 57344 3
The columns are the module name, its size in bytes, and how many other items use it.
Hybrid systems combine several designs.
- Windows is largely monolithic for speed, with a modular design and user-mode subsystems that emulate other environments.
- macOS and iOS use Darwin, which joins the Mach microkernel with the BSD Unix kernel in one address space. Message passing still exists within Mach, but no copying is needed, since the services share the same space.
- Android builds on a Linux kernel and adds its own libraries and runtime for apps.
A worked example.
A modules-versus-rebuild check (made up to show the idea). Rebuilding a large kernel to add a driver takes, say, 40 minutes and a reboot. Loading a module takes under a second and no reboot. For a laptop that meets 10 new devices a year, that saves about 400 minutes of rebuilds (10 × 40).
Loadable kernel module
A loadable kernel module is a unit of kernel code, kept in a separate file, that can be inserted into or removed from a running kernel on demand. Once loaded it runs in kernel mode as part of the kernel.
Example: a driver for a new USB device, loaded when the device is plugged in.
Check yourself:
1. How does a module differ from a microkernel server?
2. Which Linux parts are most often modules?
3. What does the "Used by" column of lsmod count?