What are linux kenrel modules
welcome to this free linux kernel development course online! In this article we will discuss about what are linux kernel modules, whey we are need linux kernel moduels, how to build linux kernel modules, how to run linux kernel modules and all other relevant information.
Linux kernel is the core component of the linux operating systesm and it is responsible for managing hardware resources and providing services to user space applications. One of the most importat features of linux kernel is its support to lodable kernel modules [LKM]
Lodable kernel moudules will help the developmer to extend the fucntionality of the kernel with out need of recompiling or rebooting the entire system.
What is the linux kernel module:
In the early days of linux kernel development, when you want to add a new feature to kernel it involves two major steps:1. Modify the kernel source code 2. Recompileand reboot the system. This is time consuming and inefficient if we want to add 5 new features then 5 times we should recompile and reboot the kernel.
so modren linux systems came up with the concept called as kernel modules, this allow deelopers to dynamically insert or remove the modules as and when they are needed. This can be done during hte run time.
Linux kernel moduels are just a piece of code that can be loaded into the linux kernel when requried and unloaded when it is no longer needed. and this concept is called as LODABLE KERNEL MODULES. and it is denoted as .ko
Where are linux kernel modules stored?
Now let us understand where are linux kenrel moudles stored? so by default the linux kernel mdoules are stored in inte linux file system in the following directory:
/lib/modules/<kernel_version>/
The kernel modules are stored in this directory inside the Linux filesystem.
uname -r
To know your kernel version.
cd /lib/modules/$(uname -r)
To navigate to linux modules directory.
find . -name "*.ko"
To list all kernel modules.
find . -name "*.ko" | wc -l
To count number of modules .
what is difference between device driver and kenel module?
Many students think that both device drivers and kernel modules are same, but no they are not entirely same.
A kernel module is a piece of code that runs in kernel space, and a device driver is a piece of code or a special type of kernel module that is used to control the hardware devices.
TIP: every device driver is a kernel module but every kernel module is not a device driver.
Why are linux kernel modules used?
Kernel modules are used to implement various kernel functionalites which include- device drivers, file systems,network protocols, systems calls, TTY drivers etc.
Advantages of using linux kernel modules:
Memory efficiency: When ever you boot your system, only the base kernel image loads first, so we can load our kernel modules only when needed this will save memory, instead of loading every module during boot time.
No need to rebuild kernel: as kernel modules are loaded dynamically, we no need to rebuild the kernel and no need to reboot the systems, if the kernel modules are not there then when ever you modify the kernel code you should rebuild the kernel and then reboot the system, but with kernel modules funcionality you can simply load or unload the functionality when ever needed.
Stability: assume if our module contains some mallicious code which we call it as bug, then only that particular module will get affected, but the core kernel will be unaffected, so this will help to debug the issue easily. If the same code is included directly in the core kernel then entire kernel will crash affecting all other sub sytems of kernel.
Easy to maintain:
Load the module when needed test the functionality and remove the module when you dont need it.
Disadvantages of Kernel Modules:
even though kernel modules are extremely useful they have some disadvantages aswell.
- Memory Fragmentation: Kernel memory is divided into pages so when you insert multiple modules, they consume more pages, there will be scenarios where systems will be with limited RAM and they may experience higher memory usage compared to system where drivers are loaded directly into kernel image.
- Core kernel functions can nver be modules: always remember there are few components which are critical to kernel can not implemented as modules, for example 1.Memory management, process scheduling, interrupt handling. they all must be part of base kernel and not loadable modules.
- Security Risks: Since linux support the dynamic module loading, it is possible to load mallicious modules and it may harm the kernel. so alway the insertion of .ko modules should be restricted to root users.
kernel configuration for module support:
for a linux system to support loadable kernel modules feature, we should enable configuration flag CONFIG_MODULES=y in your kernel and compiled, to verify this you can check
cat /boot/config-$(uname -r) | grep CONFIG_MODULES
config enable path. if you see this option is already enabled then your system support the loadable kernel modules feature.
Types of Linux Kernel Modules:
Linux kernel modules are classified into two types
- In-tree kernel modules: in tree kenrel modules are the kernel modules that are inserted into the core kernel image, that means they are already part of the source tree for example: network drivers, filesystem drivers, hardware drivers.
- Out-of-tree modules: out of tree kernel modules are the modules that are not part of official linux kernel source code, they are loaded externally. Ex: third party drivers, proprietary hardware drivers, experimental modules which are under test.
Basic Linux kernel module commands:
lsmod Command
lsmod lists all currently loaded kernel modules.
lsmod
Count the number of loaded modules:
lsmod | wc -l
View modules page by page:
lsmod | less
Internally, this command reads information from:
/sys/module
modinfo Command
modinfo displays detailed information about a kernel module.
Example:
modinfo <module_name>
rmmod Command
rmmod is used to remove a loaded kernel module from the Linux kernel.
Example:
sudo rmmod <module_name>
these are basic commannds.
I hope you got basic understanding about linux kernel modules for next lecture we will start writing the hello world linux kernel module.
Free Learning to all !!
Embeddedpathashala.
