1. Overview

There are two ways to add new code files to our kernel. One way is through the kernel config file, where we can choose files we want to add to our kernel tree. After kernel configuration, we can compile our custom kernel. However, there is another way to extend kernel capabilities dynamically at run time. This can be done using a loadable kernel module.

In this tutorial, we will see what these loadable kernel modules are, how can we list them, and how we can add and remove them from the kernel tree.

2. What Are Loadable Kernel Modules?

A loadable kernel module is an object file that can be dynamically loaded to and removed from the running kernel. A few of the popular uses of loadable kernel modules are to add device drivers for hardware, system calls, or filesystem drivers. These modules may also take parameters to customize their behavior.

The addition of loadable modules to the kernel does not require rebuilding the whole kernel. They even extend the functionality of the kernel without the need of rebooting the system. As all parts of the kernel stay loaded in memory, a loadable kernel module can save us memory as we can unload it when we don’t require it. Moreover, modules are easy to load, unload, maintain, and debug.

3. How to List All Loadable Kernel Modules

Loadable kernel modules are object files with a .ko (kernel object) extension that we can find under the /lib/modules/ directory. If more than one Linux kernel is available under this directory, we can use the uname -r command-line utility to know the active kernel for which we want to list the kernel modules:

$ ls /lib/modules/`uname -r`
kernel              modules.builtin.bin   modules.order         vdso
modules.alias       modules.dep           modules.softdep
modules.alias.bin   modules.dep.bin       modules.symbols
modules.builtin     modules.devname       modules.symbols.bin

Here, most of the modules are organized within their subdirectories under the /kernel directory:

$ ls /lib/modules/`uname -r`/kernel/
arch    Documentation  fs      lib  net       sound
crypto  drivers        kernel  mm   security

In Linux, the kmod package provides tools to manage tasks related to kernel modules, such as listing, inserting, removing, and resolving dependencies and aliases. From one of those tools, we can know the number of all the available loadable kernel modules using modprobe command as below:

$ modprobe -c | wc -l
25738

We can also list the modules that are currently loaded using lsmod command. This command formats a readable output from /proc/modules for the currently loaded modules:

$ lsmod
Module             Size    Used by
binfmt_misc        17468   1
fuse               100461  3
bnep               19624   2
bluetooth          483238  5 bnep
6lowpan_iphc       18702   1 bluetooth
rfkill             26772   2 bluetooth
xt_pkttype         12504   3
xt_LOG             17718   10
xt_limit           12711   10
iscsi_ibft         12862   0
iscsi_boot_sysfs   16000   1 iscsi_ibft
...

In the above output, the first column specifies the name of kernel modules loaded in memory, the second column specifies the amount of memory they use, and the last column indicates the number of processes that are using that respective module and other modules which depend on it.

We can also use modprobe and lsmod in combination with the grep command to search for particular modules:

$ lsmod | grep MODULE_NAME

To see additional information about a module, we can use the modinfo command with root privileges, which extracts information for the given module name to this command:

# modinfo bluetooth
filename: /lib/modules/3.16.6-2-desktop/kernel/net/bluetooth/bluetooth.ko
alias: net-pf-31
license: GPL
version: 2.19
description: Bluetooth Core ver 2.19
author: Marcel Holtmann <[email protected]>
srcversion: 7639F7FE0FBAF24B6557FF4
depends: 6lowpan_iphc,rfkill
intree: Y
vermagic: 3.16.6-2-desktop SMP preempt mod_unload modversions 
parm: disable_esco:Disable eSCO connection creation (bool)
parm: disable_ertm:Disable enhanced retransmission mode (bool) 

4. How to Load and Unload Kernel Module

The modprobe command from the kmod package is very useful for manually adding and removing a module from a Linux kernel. We require root privileges to load and unload a module from a kernel.

To load a kernel module we simply need to pass the module name without the extension:

# modprobe MODULE_NAME

The modprobe command reads the module dependencies from the /lib/modules/`uname -r`/modules.dep.bin file and loads dependencies from the same file.

Alternatively, for a module that does not reside under /lib/modules/`uname -r`/ subdirectories, for instance, a module created by us in some other directory, we can load it using the insmod command by providing a file name and arguments:

# insmod filename [args]

To unload a module, the same modprobe command is used with the -r option:

# modprobe -r MODULE_NAME

Alternatively, we can also use rmmod:

# rmmod MODULE_NAME

5. Conclusion

In this article, we discussed loadable kernel modules and a few advantages of them. We also explored how we can list currently loaded and available modules. Lastly, we saw how to load and unload kernel modules with the command-line tools.