Linux Device Driver Programming 15- Linux kernel module Introduction

  • Post author:
  • Post category:Blog

 

Linux kernel module Introduction

 

 

From this article onwards, let’s explore about the Linux kernel modules and let’s write a simple hello world kernel module.

First, let’s learn how to write a simple hello world kernel module. We will learn how to compile the kernel module using the kernel build mechanism and transfer the kernel module to the Beagle bone black hardware, loading it and unloading it.

 

What are Linux kernel module? 

Linux supports dynamic insertion and removal of code from the kernel while the system is up and running. The code what we add and remove at run time is called a kernel module. 

Once the Linux kernel module is loaded into the Linux kernel, you can start using new features and functionalities exposed by the kernel module without even restarting the device.

Linux kernel module dynamically extends the functionality of the kernel by introducing new features to the kernel such as security, device drivers, the file system drivers, system calls, and various other things. Basically, it’s a kind of modular approach.

You have a base kernel, and if you want to add new features, then you can add it as a module to the running kernel. Support for Linux kernel module allows your embedded Linux systems to have only minimal base kernel image, and optional device drivers and other features are supplied on demand via module insertion.

For example: when a hot-pluggable new device such as USB is inserted, the device driver, which is Linux kernel module, gets loaded automatically to the kernel.

That’s why the code what we add and remove at runtime is called as a kernel module. A device driver is an example of a Linux kernel module. Basically, a Linux kernel module is like a plugin to the running Linux kernel.

 

Types of Linux Kernel Modules

There are two main types of LKMs: 

  1. Static  Linux kernel Module
  2. Dynamic Linux Kernel Module

 

Static Linux Kernel Module:

When you build a Linux kernel, you can make your module statically linked to the kernel image. That means the module becomes part of the final Linux kernel image. This method increases the size of the final Linux kernel image.

Since the module is ‘built-in’ into the Linux kernel image, you can’t unload the module. It occupies the memory permanently during the runtime.

The advantage is that the module becomes permanently resident in memory during runtime.

 

Dynamic Linux Kernel Module:

In contrast, dynamic kernel modules are not embedded in the final kernel image during compilation. Instead, they are compiled and linked separately, resulting in .ko files.

These dynamic modules can be loaded and unloaded from the kernel using user-space programs like insmod, modprobe, or rmmod. This approach allows for more flexibility in managing kernel modules.

In a previous article, we demonstrated the creation of dynamic kernel modules and the commands (insmod, modprobe, and rmmod) used to interact with them.

 

Summary

When you are building the kernel, you can either link modules directly into the kernel or build them as separate modules that can be loaded into the kernel at some other time.

 

Get the Full Course on Linux Device Driver Here.

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.