Device Drivers & Kernel Modules

module parameters

Sometimes a driver needs a setting decided not at compile time but at the moment it loads: which I/O port to use, how big a buffer to make, whether to enable a debug mode. Module parameters are named knobs you can hand to a module when you load it, like command-line flags for the module. You set them at the insmod or modprobe line, or even in a config file.

In the module you declare a parameter with the module_param(name, type, perm) macro: it ties a normal C variable to a name that the loader will fill in. For example module_param(buffer_size, int, 0644) creates a parameter buffer_size; if you load the module with insmod my.ko buffer_size=4096, the variable holds 4096 by the time init runs. The permission argument (0644 here) controls whether the parameter is also visible and writable under /sys/module/<name>/parameters/, so a privileged user can read it and sometimes change it on a live module. There is module_param_array() for lists of values too.

They matter because they make one binary configurable across machines without recompiling, which is essential for distributing drivers. Two honest caveats: a parameter you can write at runtime via sysfs can change under your code's feet, so a driver must not assume it is constant; and parameters are read once into the variable at load time unless you wired up sysfs write handling, so for a value you read at insmod and never touch again, changing the sysfs file later will not retroactively re-run your setup.

static int buffer_size = 1024; // default module_param(buffer_size, int, 0644); MODULE_PARM_DESC(buffer_size, "buffer size in bytes"); // $ sudo insmod my.ko buffer_size=4096 // then visible at /sys/module/my/parameters/buffer_size

A configurable buffer size, with a default, a load-time override, and a sysfs view. MODULE_PARM_DESC documents it.

A writable sysfs parameter can change at any time from another context; if your code caches or acts on it, you may need a lock and you must re-read it rather than assume the value from load time still holds.

Also called
module paramsmodule_param模組可調參數