Fundamentals 9 min read

Understanding Linux Kernel Modules: Loading, Unloading, and Best Practices

This article explains the Linux kernel module mechanism, covering core concepts such as dynamic loading, unloading, dependency management, and symbol export, and provides implementation details, practical usage scenarios, best‑practice guidelines, and complete code examples.

Golang Shines
Golang Shines
Golang Shines
Understanding Linux Kernel Modules: Loading, Unloading, and Best Practices

As an engineer focused on operating systems and embedded development, the author emphasizes the importance of modular design for scalability and maintainability, and introduces the Linux kernel module mechanism as the core way to extend kernel functionality dynamically at runtime.

Technical Principles

Core concepts of the module mechanism

Kernel Module: code that can be loaded and unloaded dynamically.

Module loading: places module code into kernel space and runs its initialization routine.

Module unloading: removes the module code from kernel space.

Module dependencies: management of relationships between modules.

Symbol export: functions and variables that a module makes available to other modules.

Implementation Details

// Module information macros
MODULE_AUTHOR("Author Name");
MODULE_DESCRIPTION("Module Description");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");

// Initialization and exit functions
static int __init my_module_init(void);
static void __exit my_module_exit(void);

module_init(my_module_init);
module_exit(my_module_exit);

// Symbol export
EXPORT_SYMBOL(my_function);
EXPORT_SYMBOL_GPL(my_function);
EXPORT_SYMBOL_NS(my_function, MY_NAMESPACE);

// Module parameters
static int my_param = 0;
module_param(my_param, int, 0644);
MODULE_PARM_DESC(my_param, "Description of my_param");

// Module dependencies
MODULE_DEPENDS("module1", "module2");
MODULE_SOFTDEP("pre: module1");
MODULE_SOFTDEP("post: module2");

Entrepreneurial Perspective

Flexible expansion: modules enable dynamic kernel feature growth, similar to agile business expansion.

Resource optimization: on‑demand loading saves system resources, akin to efficient asset allocation.

Rapid iteration: independent development and updates mirror fast product iteration.

Risk control: a faulty module does not compromise the core kernel, comparable to isolating business risks.

Practical Tips

Declare the correct license with MODULE_LICENSE.

Handle inter‑module dependencies properly.

Release all resources during module exit.

Implement robust error handling in the initialization routine.

Ensure compatibility with the target kernel version and use appropriate kernel APIs.

Code Examples

Basic Kernel Module

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>

// Module parameter
static int my_param = 10;
module_param(my_param, int, 0644);
MODULE_PARM_DESC(my_param, "An integer parameter");

// Initialization function
static int __init hello_init(void) {
    printk(KERN_INFO "Hello, Kernel Module!
");
    printk(KERN_INFO "Parameter value: %d
", my_param);
    return 0;
}

// Exit function
static void __exit hello_exit(void) {
    printk(KERN_INFO "Goodbye, Kernel Module!
");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("A simple Hello World kernel module");
MODULE_LICENSE("GPL");
MODULE_VERSION("1.0");

Module with Symbol Export

#include <linux/module.h>
#include <linux/kernel.h>

// Exported function
int my_add(int a, int b) {
    return a + b;
}
EXPORT_SYMBOL(my_add);

// Exported variable
int my_variable = 100;
EXPORT_SYMBOL(my_variable);

// Initialization
static int __init export_module_init(void) {
    printk(KERN_INFO "Export module initialized
");
    return 0;
}
module_init(export_module_init);

// Exit
static void __exit export_module_exit(void) {
    printk(KERN_INFO "Export module exited
");
}
module_exit(export_module_exit);

MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Module with exported symbols");
MODULE_LICENSE("GPL");

Using Exported Symbols

#include <linux/module.h>
#include <linux/kernel.h>

extern int my_add(int a, int b);
extern int my_variable;

static int __init import_module_init(void) {
    int result;
    printk(KERN_INFO "Import module initialized
");
    result = my_add(10, 20);
    printk(KERN_INFO "my_add(10, 20) = %d
", result);
    printk(KERN_INFO "my_variable = %d
", my_variable);
    return 0;
}
module_init(import_module_init);

static void __exit import_module_exit(void) {
    printk(KERN_INFO "Import module exited
");
}
module_exit(import_module_exit);

MODULE_AUTHOR("Your Name");
MODULE_DESCRIPTION("Module using exported symbols");
MODULE_LICENSE("GPL");
MODULE_DEPENDS("export_module");

Module Management Commands

# Load module
sudo insmod mymodule.ko
sudo insmod mymodule.ko my_param=20

# Unload module
sudo rmmod mymodule

# List loaded modules
lsmod

# Show module info
modinfo mymodule.ko

# Auto‑load module
sudo modprobe mymodule

# Show module dependencies
modprobe --show-depends mymodule

# View kernel log
dmesg | tail

# Find exported symbols
cat /proc/kallsyms | grep my_add

Conclusion

The Linux kernel module mechanism enables dynamic extension of kernel functionality through loading, unloading, dependency management, and symbol export. Proper licensing, dependency handling, resource management, error handling, and kernel‑version compatibility are essential to achieve optimal performance and reliability in real‑world deployments.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

LinuxDynamic LoadingKernel DevelopmentDevice DriversKernel ModulesSymbol Export
Golang Shines
Written by

Golang Shines

We share daily the latest Golang technical articles, practical resources, language news, tutorials, and real-world projects to help everyone learn and improve.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.