what is lsmod, insmod,modinfo,rmmod,modprobe

what is lsmod, insmod,modinfo,rmmod,modprobe:

so in this lecture we will discuss about what is lsmod, insmod,modinfo,rmmod,modprobe: when we are working in linux kenrel development there are several commands that we use regularly like inserting module, removing module, module information and so on so to get this information we will use below commands

lsmod

insmod

modinfo

rmmod

modprobe

Understanding of this modules is verymuch important if you are working on linux kernel development, device driver development or in embedded linux system.

lsmod – list loaded kernel modules

lsmod command will show all the modules that are currently loaded into the kernel, you can give below command in linux shell

 

 lsmod 

example output

i2c_hid_acpi           12288  0
video                  77824  4 dell_wmi,dell_laptop,xe,i915
i2c_hid                40960  1 i2c_hid_acpi
hid                   180224  4 i2c_hid,usbhid,hid_multitouch,hid_generic
wmi                    28672  7 dell_wmi_sysman,video,dell_wmi,wmi_bmof,dell_smm_hwmon,dell_smbios,dell_wmi_descriptor

you can run same command in your terminal and see the output

1st column -> module name this will tell which module is loaded

2nd column -> memory used by module

3rd column -> number of moudles using it

internally lsmod will read the file

 

cat /proc/modules

this is the information that you will get from lsmod,

insmod – insert kernel module:

the insmod commad loads the module directly into the kernel

 sudo insmod hello.ko 

this will load the .ko into kernel

rmmod- Remove Kernel module

this removes the loaded kernel module from the kernel, to remove the kernel module you can give below command

 sudo rmmod hello 

important to note here, provide module name, if the module is bening used by other module – removal fails with an error

Module is in use

 

this is how rmmod works

modinfo – Display module information

modinfo shows the information that is embedded inside a module.

 

 modinfo hello.ko 

exampl output is

ilename: hello.ko license: GPL author: Raviteja description: Simple kernel module

this information comes from the macros in module like

MODULE_LICENSE
MODULE_AUTHOR
MODULE_DESCRIPTION

inside module source

modprobe – module loader

modprobe is a smart module loader, it can load the modules,remove modules, resolve dependencies automatically. for example

 sudo modprobe usb_storage 

to remove module

 sudo modprobe -r usb_storage 

now let us see the difference between modprobe and insmod

insmod vs modprobe

 

| Feature                | insmod    | modprobe         |
| ---------------------- | --------- | ---------------- |
| Dependency handling    | ❌ No      | ✅ Yes            |
| Path required          | Full path | Module name only |
| Uses modules directory | No        | Yes              |
| Intelligent loading    | No        | Yes              |

this is the basic difference between insmod and modprobe

how modprobe loads the modules

modprobe only loads the modules from

/lib/modules/$(uname -r)/

example

 

/lib/modules/6.8.0-90-generic/

if you try to give local path then you will not see any output and modprobe will not work, because modprobe only searches inside

/lib/modules//

so this is how modprobe will load the modules,.

How modprobe resolves dependency errors:

modprobe internally depends on depmod tool, depmod scans all kernel modules and calculates all the module dependencies, below is the command for depmod

 depmod 

it generates a file

 

/lib/modules/$(uname -r)/modules.dep

so depmod will help to resove any dependencies, in upcomming lectures i will teach you more about the depmod in detail. this is an introduction lecture so i hope you got the usage of depmod

mdoules.dep file

mdoules.dep file will store the module dependency information for example

/lib/modules/6.8.0-90-generic/modules.dep

example entry will look like this

kernel/drivers/net/wireless/xyz.ko: kernel/net/mac80211/mac80211.ko</pre

meaning

xyz.ko depends on mac80211.ko

so modprobe loads

mac80211.ko → xyz.ko

so this is how modprobe loads the dependency issues.

how to search modules:

if you want to search of any specific module in the kernel then you can give the below command.

 

 find /lib/modules/$(uname -r) -name "*80211*.ko" 

it will show any matching modules available

how to see modules.dep

 

 vi /lib/modules/$(uname -r)/modules.dep 

this is where modules .dp is present, you can open that file and see all the relevant information. when developing the kernel i will show you more about it for now just understand the meaning of modules.dep.

How modprobe inserts multiple modules:

modules are always inserted from right->left 

example

A depends on B depends on C

loading order is

C → B → A

so this will resolve dependencies, then what about removal order? we can not remove the modules randomly as they might be dependent on some other mdoules right? so removal order is in reverse that is from left to right

A → B → C

this is how module dependencies are handled

Reload Dependencies:

if new modules are loaded then run

 sudo depmod -a 

this regenerate the dependency files

What is an Alias in C

Now let us understand the symbol aliasing, and why we need symbol aliasing.

Alias means two different names refer to the same function or variable, this is implemented using GCC attribute:

 

__attribute__((alias("symbol_name")))

let us see an example for aliasing


static int alias_fun(int a, int b)
{
    printf("%s: sum of %d and %d = %d\n", __func__, a, b, a+b);
    return a+b;
}

static int def_fun(int a, int b) __attribute__((alias("alias_fun")));

below is explanation, here we define def_fun() then we create a alia_fun so both names refer to the same function in memory.

 

def_fun → alias_fun

now let us see some example for alias in kernel programming

 

#include

static int alias_fun(int a, int b)
{
	printf("%s:sum of %d and %d=%d\n",__func__,a,b,a+b);
	return a+b;
}
static int def_fun(int a, int b) __attribute__((alias("alias_fun")));




int main()
{
	def_fun(2,5);
	alias_fun(1,2);
	return 0;
}

let us see what we are doing here, main def_fun() is created and an alias fun() is created, so when def_fun or alias_fun is called it internally calls alias function and we will see same functionality.

 

#include
int old_var = 2;

extern int new_var __attribute__((alias("old_var")));
int main()
{
	printf("value of new var = %d\n", new_var);
	return 0;
}

this is one more example with alias variable, so here initially i created old_var with value 2 and new variable is alias of old variable so when i print new variable it will redirect to old variable and print the value.

with out module_init and module_exit functions:

 


#include<linux/kernel.h>
#include<linux/module.h>
MODULE_LICENSE("GPL");
int init_module(void)
{
	printk(KERN_INFO"%s: in init_module\n",__func__);
	return 0;
}
void cleanup_module(void)
{
	printk(KERN_INFO"%s: in cleanup_module \n",__func__);
}

let us analyse this example, so in the above example instead of module_init and module_exit I have used the default kernel functions which are init_module() and cleanup_module() -> what do you think will happen, the answer is module is successfully created and then loaded with out any issue, but we do not recommend this approach as they are default fucntions and it will cause issue in understanding which api is being called, this will cause impact on redability and maintainability in source code. so it is always recomened to use the cleaner programming design.

Conclusion

so that is all for this lecture, to conclude we discussed below topics:

Kernel modules can work without module_init() and module_exit() using default functions.

But modern Linux kernel modules should always use the macros.

__attribute__((alias())) allows two names to refer to the same symbol.

Alias can be applied to:

functions

variables

Kernel internally uses similar mechanisms to map module entry points.

Free learning to all !!

EmbeddedPathashala.

Leave a Reply

Your email address will not be published. Required fields are marked *