Linux Device Drivers Part - 6 : Loading Listing and Unloading Kernel Modules

In this part , we are going to discuss about

1. Loading Kernel modules
2. Listing  kernel modules
3. Unloading kernel modules

Before going ahead with these topics, I strongly recommend you to watch my earlier videos on Linux Device Driver Part 1 to 5.

Fine, lets start with our first topic

1. Loading Kernel modules 
In earlier session "Linux Device Drivers Part-5", we learnt how to build a sample helloworld Linux Kernel module.

In this session we will see how to load that sample module into the kernel.

We have two kernel utilities which does the trick of loading modules into the kernel.

We have two utilities to perform this task
1. insmod
2. modprobe

In following slides we will discuss about these utilities, their syntax and usage

So,What is insmod and what does it do ?

Insmod is a kernel utility that installs loadable Kernel modules into kernel.        

It actually loads the module code and data into the kernel land, it links any unresolved symbols in the module to the symbol table of the kernel.

Insmod accepts a number of command line options and it can assign a value to parameters in a module before linking it to the current kernel.

Note that if a module is correctly designed, it can be configured at load time by passing arguments to insmod.

Lets see syntax of insmod

Syntax:
insmod [filename]  [module-options...]

These module options are optional.

For example:
insmod my_module.ko          

This command inserts a module named my_module  into kernel with no module option
   
Take another example,
 insmod /path/to/my_module.ko param1=0x330  

This command inserts my_module into kernel, specifying the module options "param1=330" . Module-options are just like command line arguments to kernel object

We will discuss more about module options in upcoming sessions ===
One important point to note here is that insmod accepts only one module as input prameter

You can't load two loadable modules at a time using insmod utility.

Second utiltiy is modproble

It’s a Linux utility which offers more features than basic insmod utility.

When compared to insmod, modprobe has various advantages like
It has the ability to decide which modules to load. Modprobe checks a directory /lib/modules/$(uname -r) for modules. This way it knows what modules to load

Also, Its aware of module dependencies.To know details of module dependencies, Modprobe uses another configuration  file called modules.dep which is generated by depmod program

It supports resolution of recursive module dependencies when required.  When requested to load a particular module say its name x, if it has dependency on another module say y which is again dependency on z, modprobe tries to find these  modules in the current path or standard lib path and loads these modules to resolve all the dependecy symbols. 

These features are not available with basic insmod command

Coming to syntax of modprobe to install a module

Its Basic syntax is
      modprobe  [modulename...]

command followed by optional module name

For Example :
        $sudo modprobe

When modeprobe is soley used, it intelligently adds or removes a module from /lib/modules/'uname -r'

$sudo modprobe my_module.
  $sudo modprobe my_module.  

Note you should be a super user to execute modprobe.
Modprobe supports various options. Please refer modprobe man page for more details.

Lets learn the difference between insmod and modprobe

While loading a module, insmod fails with "Unresolved Symbol" error when an unresolved symbol is present in the loading module.

Where as modprobe looks at the module to be loaded to see  whether it references any symbols that are not currently defined in the kernel. If any such references are found, modprobe looks for other modules in the current module search path that define the relevant symbols.

When modprobe finds those modules which are needed by the module being loaded, it loads them into the kernel as well.

In short, while loading a module into kernel, if that module has any dependency on another module, modprobe find those modules under current module search path, it loads them into the kernel.

Note modeprobe is aware of default location of modules and knows how to figure out the dependencies and load the modules in the right order.

One more important point to note here is  we need to root permissions for executing both insmod and modporbe commands.

When a module is loaded, any symbol exported by the module becomes part of kernel symbol table.

So what exactly is kernel symbol table

Kernel Symbol Table:

In earlier slides we have discussed about insmod resolving undefined symbols by referring Kernel Symbol Table. The Kernel Symbol table contains the addresses of global kernel items, functions and variables that are needed to implement modularized drivers.

Now we know details of loading a kernel module using insmod and modprobe. Lets jump to our second topic,

2. Listing modules currently loaded in kernel

We use a Linux utility called “lsmod”

Lsmod command lists the modules currently loaded in the kenrel. Lsmod works by reading the /proc/modules virtual file.

Information on currently loaded modules can also be found in the sysfs virtual filesystem under /sys/modules.

Syntax:
$lsmod

3. Unloading kernel modules
Two Kernel Utilities to unload the modules are
$rmmod
$Modprobe

Lets learn about rmmod
rmmod is a Linux utility which unloads the loadable module.

Module should be already loaded to execute rmmod command.

Module fails if the kernel believes that the module is still in use or if the kernel has been configured to disallow module removal.

Syntax of rmmod

rmmod [module name]
Example: $rmmod my_module

Note name "my_module" doesnt have any extension like .ko here

Note :We need to be a super user for executing this command.

Modprobe syntax

Since we have already discussed about modprobe in earlier slides, lets learn the syntax for unloading a kernel module

Syntax is
$modprobe –r  [module-name]

For example:
  insmod my_module.ko    
$modprobe –r my_module

Note : There is no ko extension here and We need to be a super user for executing this command.

Lets wind up this session with an important question asked in forums.

Is it possible to forcibly remove the kernel modules when the are in use ?

Yes, its possible to configure the kernel to allow forced removal of modules, even when they appear to be busy.

Commands normally used are
rmmod -f yourmodule
modprobe -rf yourmodule

These commands only work when you enable the kernel configuration CONFIG_MODULE_FORCE_UNLOAD.

Hey don't forget to check out youtube version of this article here. 

If you like, please don't forget to give a thumbsup :) 

Comments

Popular posts from this blog

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

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

Linux Device Drivers - Part 13 : More on Device Numbers