Freelance Projects

Upeksha Wisidagama

UW Loadable Kernel Module

Many device drivers and other kernel elements are compiled as modules. If a driver is compiled directly into the kernel, its code and static data occupy space even if they’re not used. But if the driver is compiled as a module, it requires memory only if memory is needed and subsequently loaded, into the kernel.

Interestingly, you won’t notice a performance hit for LKMs, so they’re a powerful means of creating a lean kernel that adapts to its environment based upon the available hardware and attached devices.

UW Linux Loadable Kernel Module

Create the Linux Loadable Kernel Module C file.

‘uw-lkm.c’
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <linux/module.h>

/**
 * Defines the license for this LKM
 */
MODULE_LICENSE("GPL");


/**
 * Init function called on module entry
 */
int uw_module_init( void )
{
    printk(KERN_INFO "uw_module_init called.  UW Module is now loaded.\n");
    return 0;
}

/**
 * Cleanup function called on module exit
 */
void uw_module_cleanup( void )
{
    printk(KERN_INFO "uw_module_cleanup called.  UW Module is now unloaded.\n");
    return;
}

/**
 * Declare entry and exit functions
 */
module_init( uw_module_init );
module_exit( uw_module_cleanup );

Next Create the Makefile.

‘Making of the UW Loadable Kernel Module’
1
obj-m += uw-lkm.o

Compile the Loadable Kernel Module using make -C /usr/src/linux-headers-`uname -r` SUBDIRS=$PWD modules.

Then insert the new module using sudo insmod uw-lkm.ko.

Finally, check the Log to confirm the new module is loaded using sudo dmesg | tail -1.

UW Loadable Kernel Module