Course contentsShow
← Embedded Systems & Computer Architecture
Lesson 840 of 93611. Introduction to Embedded LinuxFree lesson

Implementing Module Parameters for Runtime Configuration

Learners will define module parameters to allow users to pass configuration values into the kernel module during the loading process.

In this lesson, you will learn how to make your kernel modules flexible by defining parameters that users can modify dynamically when loading the module into the Linux kernel.

Why Use Module Parameters?

When developing kernel modules, you often need to configure behavior—such as setting a sampling frequency, enabling debug logging, or specifying a hardware address—without recompiling the entire module. Module parameters act as a bridge between the user-space insmod or modprobe commands and your kernel code. By defining these parameters, you allow the system integrator or user to inject specific values into your module at the exact moment it is initialized.

Defining Parameters

To expose a variable as a module parameter, we use the module_param() macro, which requires three arguments: the variable name, its data type, and the permission bits for the entry in /sys/module/<module_name>/parameters/.

Consider a simple driver where we want to define a buffer_size and a debug_enabled flag:

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

static int buffer_size = 1024;
static bool debug_enabled = false;

/* Define parameters: name, type, and sysfs permissions */
module_param(buffer_size, int, 0644);
module_param(debug_enabled, bool, 0644);

MODULE_PARM_DESC(buffer_size, "Size of the internal buffer in bytes");
MODULE_PARM_DESC(debug_enabled, "Enable verbose kernel logging");

Runtime Interaction

Once your module is compiled, you can pass values through the command line. If you load your module using insmod, you simply append the parameters to the command:

sudo insmod my_driver.ko buffer_size=2048 debug_enabled=1

The kernel automatically updates your variables with these values before the module's initialization function is called. Because we set the permissions to 0644, users can also view or modify these values at runtime by writing directly to the corresponding files in the /sys filesystem. This is particularly useful for debugging or tuning performance parameters without needing to unload and reload the driver, providing a seamless way to adjust hardware interactions on the fly.

Key Takeaway: Module parameters allow you to pass configuration data from user-space to your kernel module at load time, transforming static code into highly configurable hardware drivers.