Linux Device Drivers - Part 4 : Linux Kernel Moduels (LKM) and types of LKM's

In this sesssion, we will discuss about
  • Linux Kernel Modules
  • Base Kernel,
  • User Space and Kernel Space
  • Types of Linux Kernel Modules,
  • Which module type to choose while Kernel Programming ?
  • Location of Linux Kernel Modules in a linux PC.
mrtechpathi_tutorials_linux_device_drivers_4

What exactly is a module in Linux ?
Its a piece of code that can be added to the Base Kernel. We will be discussing about Base kernel in coming slides.

If you are planning to add some code to kernel, then it means you are adding a module to Linux Kernel.We can't just add code to Linux without any purpose.  We add code to make a device (like printer,bluetooth dongle etc) to work.We call this piece of code as device drivers in Windows.

Assuming many are familiar with Windows, we can draw one conclusion.As we have device drivers in Windows Operating system, we have modules in Linux Operating system.In short, Devices Drivers are called modules in Linux Operating system. Both terms can be used interchangeably.

Before knowing about Module types, we need to know about Base Kenel, User Space and Kernel Space.

What is a Base Kernel:

Kernel code which is compiled and loaded along with basic device drivers as a single entity is called Base Kernel.Note that all parts of base kernel will be loaded all the time.Once you have a working base kernel,it is good to leave it untouched as long as possible.

Next we will discuss about User space and Kernel space in Linux.

mrtechpathi_tutorials_linux_device_drivers_4
From this diagaram, Base Kernel along with Kernel modules (KM1 and KM10) fall under Kernel space. User applications fall under user space.

What are User Space and Kernel Space ?

Simplified answer is that the kernel runs in kernel space, and normal programs run in user space.

Normal programs in user space can't mess with memory (and other resources) owned by other programs or by the Linux kernel. This limits their ability to do corrupt things like crashing the machine.

The kernel is the core of the operating system. It normally has full access to all memory and machine hardware (and everything else on the machine. To keep the machine as stable as possible, you normally want only the most trusted, well-tested code to run in kernel mode/kernel space.

Modules in Linux are of two types.

1. Static Modules
2. Dynamic Modules

What is a static module ?

Static modules are those which are compiled as a part of the base kernel and is available at anytime.
mrtechpathi_tutorials_linux_device_drivers_4

From this diagram you can see the static modules are part of Kernel image. They are compiled and loaded as a part of base kernel image.

What is dynamic module ?

Dynamic modules are compiled as modules separately and loaded as and when required.

These are also called Loadable Kernel Modules. In short we call them as LKM from now on.

Every Kernel Programmer should clearly understand about dynamic modules and how they work.

From this diagram, we see the dynamic modules are compiled separtely. They are not part of basic kernel image.
mrtechpathi_tutorials_linux_device_drivers_4

To know how exactly dynamic module is loaded, we need to know 4 important steps.

Step 1: From this diagram, assume you have written a dynamic module xyz. You will also write an application which tries to access xyz functionalities.

Step 2: Once Linux is up on running on your PC or device, you will dynamically insmod your xyz module using "insmod" command to RAM. We will discuss about insmod in later sessions.
Step 3: You application in userspace xyz will try to access functionalities of your hardware through xyz module by calling Linux provided system calls. Normally they are called ioctl calls. We will discuss about ioctl calls later.

Step 4: Once the required hardware functionality is retrieved by the user application, when xyz module is no more needed, we can request kernel to remove xyz module using rmmod command.
mrtechpathi_tutorials_linux_device_drivers_4

These are 4 simple steps how a dynamic module is loaded, functionality of hardware is retrieved and dynamic module is removed.

Once after discussing about static and dynamic modules, we have a common question, which one to use ? which is the best ?

Yes ,always a device driver programmer has a choice between building his module as static module or dynamic module.

To decide on this, we need to first know few things.

  • Disadvantages of building your module as static.
  • Advantages of building your module as dynamic (as LKM).
Disadvantages of building your module as static:

Lets say you have written a static module "xyz" for a device driver.
When you compile this as static module along with kernel, you will be adding extra size to the kernel image permanently. This the first major disadvantage. As the kernel size grows, the time taken to load the kernel and size occupied by the kernel image in the memory.increases.

Whenever you modify your "xyz" device driver, you need to recompile your entire kernel to build it.
Also machine need to be rebooted for the changes to take effect.

One more important drawback is While you device driver is not stable, when you build it as static module, you will face issues which takes more time to debug.

Take scenario when there is a memory overflow bug in your "xyz" static module. Once it is statically compiled and the kernel is loaded, at some point of time, the kernel may crash.

In order to debug it people may take hours or days to fix it. Also your kernel which includes this "xyz" as part of it will be useless till you fix the crash issue.

This doesn't happen when you build you "xyz" as dynamic module. We will see how in a short while.

Advantages of building your module as dynamic (as LKM):

Lets say you built your xyz module as dynamic (LKM) now.

With this you don't have to rebuild your kernel as often. It can be compiled separately.

It saves you time and spares you the possiblity of introducing an error in rebuilding and reinstalling the base kernel.

It can be loaded onto kernel at run time without having the machine to reboot.

It saves you memory beacause you load them when you are actually using them.

It can be unloaded anytime and hence no permanent affect on kernel size.

Since these are compiled and built serpately from kernel, they are much faster to maintain and debug.

Each dynamic module is made up of object code that can be dynamically linked to the running kernel by the insmod program and can be unlinked by the rmmod program.

Many have a misconception that dynamci modules are bit slower than Static modules. I totally disagree with them because, loading either one is simply a brach to the memory location where it resides.

With information known till now, lets see the example which we discussed earlier about a overflow bug in your module. Assume this bug is present in your "xyz" module which is built as dyanmic module.

Once you boot-up your Linux on your Embedded device or PC, load your LKM's along with your “xyz” dynamic module, after a while when your system crashes because of overflow bug, to know if your "xyz" module is causing the issue, you can discard loading your "xyz" module after next boot up and just load the base kernel along with other dynamic modules and see if the system crash.

If it doesnt, then it clearly shows that your "xyz" module is the culprit and you can debug and fix the bug without any much headache.

Remember, you can even use your base kernel and other LKM's till you fix this overflow bug in your "xyz" dynamic module.

Hope everyone are clear with this explanation. If youare not clear, please drop a comment.

ok,Now if we repeate our earlier question as a kernel programmer, which one do you prefer static module or dynamic module ?

As a good kernel programmer, one should prefer building their module as dynamic module.

So when to use Static modules ?

There are times where one have to build something into the base kernel instead of making it a dynamic module.

Anything that is necessary to get the system up far enough to load dynamic modules must obviously be built into the base kernel.

For example, the driver for the disk drive that contains the root filesystem must be built into the base kernel

Final topic of this session,

Location of linux kernel modules in linux device or PC:

Usually, all Linux kernel modules (drivers) are stored in the module directory located that /lib/modules/$(uname -r) directory.

To see current modules, type:
$ ls /lib/modules/$(uname -r)

Use the following command to list all drivers for various devices:
$ ls /lib/modules/$(uname -r)/kernel/drivers/

Thats all for this session. If you have any doubts, dont hesitate to drop a comment or a mail.

For more explanation, please watch this video.

Comments

Popular posts from this blog

Linux Device Drivers Part - 5 : Building and Compiling Kernel Moduels

Linux Device Drivers - Part 13 : More on Device Numbers