The Wayback Machine - https://web.archive.org/web/20230117040331/https://www.geeksforgeeks.org/linux-loadable-kernel-module/
Skip to content
Related Articles

Related Articles

Linux Loadable Kernel Module

Improve Article
Save Article
  • Last Updated : 21 Feb, 2022
Improve Article
Save Article

If you want to add code to a Linux kit, the basic way to do that is to add source files to the kernel source tree and assemble the kernel. In fact, the process of setting up the kernel consists mainly to select which files to upload to the kernel will be merged. But you can also add code to the Linux kit while it is running. A piece of code you add this way is called a loadable kernel module (LKM). These modules can do many things, but they are usually one of three items: 

  • device drivers
  • file system drivers
  • system calls. 

The kernel separates certain functions, including these, especially well so do not need to be encrypted across the kernel. Several parameters that a driver must know can change system to system. These can vary from the device number to use to numerous aspects of how the driver should operate.

Terminology

Downloadable kernel modules are often referred to as kernel modules or just modules, but they are misleading because there are so many types of modules in the world with different pieces built into the base kernel that can easily be called modules. We use the term kernel module or LKM for certain types of modules. Some people think of LKM as outside the kernel. They talk about LKMs that connect to the kernel. This is a mistake; LKM (if loaded) is a large part of the kernel. The correct term for part of The kernel tied to the image you are launching is “base kernel.” LKM connects to the base kernel. In some operating systems, equivalent to Linux LKM is called a “kernel extension.”

History of LKM

Loadable kernel modules were not in Linux at first. Anything we use for LKM today is built into the base kernel at the time of building the kernel instead. LKMs have existed at least since Linux 1.2 (1995). Device drivers and so on were always modular, however. When LKMs were established, a small amount of work on these modules was required to form LKMs. However, it had to be done individually, so it took time. Since about 2000, almost everything as sensible as LKM has at least had the option to be LKM.

The Case for LKM

You often have the choice between installing a module in a kernel by uploading it as LKM or committing to it. base kernel. LKM has many benefits in addition to binding to the basic kernel. Another advantage is that you do not need to rebuild your kernel often. This saves time and saves you .you may have made a mistake in restructuring and refining the base kernel. Once you have a job base kernel, it is best to leave it untouched for as long as possible. Another advantage is that LKM helps you diagnose system problems. Disruption to the device driver in the kernel can stop your system from running at all. And it can be very difficult to say which part of the foundation kernel created a problem. If the driver of the same LKM device, however, the base kernel is active again before the device driver is loaded. It is easy to track a problem when your system dies once you turn on/off the base kernel until the device driver causes the problem and we do not load the device drive until we fix the problem.

LKMs can save you memory because you should only download them if you really use them. All parts of the base kernel are loaded at all times – in real storage, not virtual storage. LKMs are very quick to maintain and repair. What may require a full restart to be done with the file system driver built into the kernel, you can do it with a few quick commands with LKM. You can try different parameters or change the code repeatedly in quick succession, without waiting for the boot. LKMs do less, by the way than base kernel modules. Calling anywhere is just a branch in the place of memory where it resides. Sometimes you have to build something in the base kernel instead of making it LKM. Anything that exists that is needed to move the system far enough to load LKMs should obviously be built into the base kernel. 

What LKMs are used for?

Let’s look at the major 6 LKMs used.

  • Device drivers: The device driver is designed for a specific piece of hardware. The kernel uses it to contact that piece of hardware without knowing any hardware details it works. For example, there is an ATA disk drive device driver. There is one for the NE2000 compatible Ethernet cards. In order to use any device, the kernel must contain a driver for its device.
  • Filesystem drivers:  The file system driver interprets the contents of the file system such as files and references and more. There are a lot of ways to save files and references. By all means, you need a file system driver. For example, there is an ext2 file type file system driver used almost all over the world on Linux disk drives. There is also one MS-DOS file system, as well as one NFS.
  • System calls: Userspace applications use system calls to access resources in the kernel. For example, there are system calls to read the file, create a new process, and close the program. Most System phones are part of the system and are state-of-the-art, so they are always built into the base kernel (no LKM option). But you can configure your system phone and install it as LKM. Or you can decide you don’t like the way Linux does something and issue an existing system call with Your LKM.
  • Network drivers: The network operator interprets the network protocol. It feeds and consumes data streams at various stages of kernel communication activity. For example, if you want an IPX link to your own network, you will use an IPX driver.
  • TTY line instructions: This is actually an upgrade of device drivers to storage devices.
  • Useful interpreters: Usable translator uploads and uses usable. Linux is designed for that to be able to use in a variety of formats, and each must have its own usable translator.

Compiling Kernel Modules

 The below example shows how our kernel module gets parameters from users at load time by insmod or modprobe command.

Example Code:

C++




#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
  
static char* whoisthis = "Mommy";
static int countpeople = 1;
  
module_param(countpeople, int, S_IRUGO);
module_param(whoisthis, charp, S_IRUGO);
  
static int __init m_init(void)
{
    pr_debug("parameters test module is loaded\n");
    for (int i = 0; i < countpeople; ++i) {
        pr_info("#%d Hello, %s\n", i, whoisthis);
    }
    return 0;
}
  
static void __exit m_exit(void)
{
    pr_debug("parameters test module is unloaded\n");
}
  
module_init(m_init);
module_exit(m_exit);
M ODULE_LICENSE("GPL");

Shell :

Build the module. To build this module, execute:

bash make KERNELDIR=/path/to/kernel/source/dir

If you have already set and exported the `KERNELDIR` environment variable, simply executing `make` is enough.

Usage :

Copy the  object file **hello_world.ko**  to the target machine, then run:

bash insmod module_parameters.ko

Running `dmesg | tail -10`, you can find something like this:

[ 1223.506799] parameters test module is loaded [ 1223.509953] #0 Hello, Mommy

It is right that by default, we set to print **hello, Mommy** only one time. Next, we unload this module and pass parameters to it when loading.

bash # Unloading using rmmod command rmmod module_parameters # Reloading the module and passing parameters to itre-load the module and passing parameters to it insmod module_parameters.ko whoisthis=dady countpeople=3    

This time, the message is changed:

[ 1322.364784] parameters test module is loaded
[ 1322.366768] #0 Hello, dady
[ 1322.367999] #1 Hello, dady
[ 1322.369154] #2 Hello, dady

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!