Linux Kernel Module

In this chapter we will discuss about Loadable kernel modules. We will understand what are Loadable kernel modules, why Loadable kernel modules were introduced, what are the benefits of kernel modules, what are the usage of Loadable kernel modules and how Loadable kernel modules are different with user space modules.

When you write some piece of code and want to add that code in kernel , one way of doing this is to place your code in one of kernel source directory and recompile the kernel code . So your code will also compile with whole kernel code. Your code will be part of kernel booting image.

static_module

But suppose if you want to do little change in your code then again you need to rebuild your kernel to have your code compiled. So don’t you think it’s a tedious task of recompiling kernel again and again even for just a silly change in your code. To avoid this limitation with many other limitations Linux introduced the concept of Loadable kernel modules.

Kernel module is the piece of code which can be loaded and unloaded into kernel upon demand. it means you can load your code or module into kernel whenever it required and you can unload your code and module from kernel whenever you don’s required.

Kernel modules are compiled separately and whenever there is requirement for that module , you can insert it into kernel by using of some commands. When required work is done by module and no longer required you can easily unload your module by running some commands.

kernel_module

Benefits of Kernel module

  1. You no need to build whole kernel again just to have a small change in your code. you can change your code , compile it separately and then load into kernel.
  2. You can easily debug any issue by using modules. suppose you compile code with as part of kernel and after building your kernel is not booting then it’s very hard to find that issue is caused by your code or some other part of kernel. But if you use concept of kernel module then you can first boot your base kernel without your module and see if its booting fine. In case if its booting fine then you can add your module and if after inserting your module kernel got crashed then it means your module is the culprit. You can separately debug the issue in your module.
  3. When you compile your code as part of kernel , your code will be part of your kernel ‘s compiled image then it will be loaded all the time in memory even though you don’t required it. But kernel module will be loaded only when it will be required and can be unloaded when not required in future.
  4. Their performance is same as other code which is part of kernel itself.
  5. You can frequently change your code easily without rebooting kernel.

Usage of Linux kernel modules

Because of so many features provided by kernel modules , are used for various things

  • Device drivers:- Device Driver is an interface between User application and Hardware. User application request to Device driver for any interaction to hardware. Device driver check if the application has the permission to access the hardware or not. And depending upon that Device driver of Requested hardware respond.
  • file system drivers:- Our system always connected to multiple storage devices. Each device provides different way of accessing data stored in device. These different way of accessing the devices are provided by file system driver.
  • system calls:- It provides an interface to the applications running in user space for getting the controlled access to hardware. This interface provides a mechanism by which applications can request operating system resources. If you are implementing your own syscall you can implement it as a module and load in kernel whenever its required.

 

Kernel Module vs User module

From functionality of loading and unloading of kernel module some time people thinks its same as user modules. But there are lot of difference in both.

kernel-vs-user-module

adana elektrikci

', 'auto'); ga('send', 'pageview');