Skip to content
systemcoding.dev
Kernels

Pass Parameters to a Kernel Module With module_param

Pass parameters to a Linux kernel module with module_param: types, permissions, MODULE_PARM_DESC, insmod and modprobe.d usage, and /sys/module values.

6 min read
Summarize in AI
Flat illustration of user space above the system call boundary, the kernel and the hardware, with the Linux, C and Rust logos.

Making a module configurable

A module often needs a number or a string that differs between machines: a buffer size, a debug level, the name of a device. Hard-coding it means rebuilding for every case. Linux lets you declare module parameters, variables that can be set when the module loads, from the command line, from a configuration file, from the kernel command line for built-in code and, for suitable ones, at run time through sysfs.

This post declares parameters with module_param, covers the types and the permission argument, shows each way to set a value and explains what is checked. It builds on the hello module and targets Linux 6.12 LTS. The definitive reference is include/linux/moduleparam.h and the kernel parameters documentation.

Declare parameters

params.cc
#include <linux/init.h>
#include <linux/module.h>
#include <linux/moduleparam.h>

static int count = 1;
module_param(count, int, 0444);
MODULE_PARM_DESC(count, "How many times to repeat the greeting");

static char *greeting = "hello";
module_param(greeting, charp, 0444);
MODULE_PARM_DESC(greeting, "Text to print at load time");

static bool shout;
module_param(shout, bool, 0644);
MODULE_PARM_DESC(shout, "Print in upper case");

static int __init params_init(void)
{
	int i;

	for (i = 0; i < count; i++)
		pr_info("params: %s (%d/%d)\n", greeting, i + 1, count);
	return 0;
}

static void __exit params_exit(void)
{
	pr_info("params: bye, shout=%d\n", shout);
}

module_init(params_init);
module_exit(params_exit);
MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("Module parameter demo");

module_param(name, type, perm) takes the variable name, which is also the parameter name, its type and a permission mask. The variable must be a static or global with an initial value, which becomes the default.

Types

TypeC variableNotes
int, uint, long, ulong, short, ushortmatching integerDecimal, 0x hex or leading-zero octal accepted
byteunsigned charA number, not a character
boolboolAccepts y, Y, 1 for true and n, N, 0 for false
invboolboolReads the value inverted
charpchar *The kernel allocates a copy of the string

Arrays use module_param_array(name, type, &num, perm), where num is a variable that receives how many values were supplied. module_param_named(external, variable, type, perm) exposes a variable under a different parameter name.

The permission argument

The third argument controls the file that appears under /sys/module/<module>/parameters/. It is an ordinary octal mode:

ValueMeaning
0No sysfs file; the parameter can be set only at load
0444Readable by everyone, not writable
0644Readable by everyone, writable by root

Write the mode in octal. Older code uses the symbolic S_IRUGO and S_IWUSR, and scripts/checkpatch.pl now asks for octal because the symbolic forms are easy to get wrong. Be careful with a writable parameter, since a value that changes while the module runs is a race unless the code reads it atomically or under a lock.

MODULE_PARM_DESC

MODULE_PARM_DESC(name, "text") stores a help string that modinfo prints. Always add one: it is the documentation users will see.

Set the values

With insmod or modprobe

Pass name=value pairs after the module:

Shell
make
sudo insmod ./params.ko count=2 greeting=hi
sudo dmesg | tail -n 2
sudo rmmod params
Text
[ 7312.100441] params: hi (1/2)
[ 7312.100460] params: hi (2/2)

The timestamps will differ. A string with spaces needs shell quoting that the kernel then parses again, so use greeting='"hello world"' with the inner double quotes. If a value is not valid for the type, the load fails:

Shell
sudo insmod ./params.ko count=abc
Text
insmod: ERROR: could not insert module ./params.ko: Invalid parameters
Text
params: `abc' invalid for parameter `count'

The first block is the insmod message and the second is the line the kernel logs. An unknown parameter name fails the same way, with Unknown parameter in the log.

From a configuration file

With modprobe, put options in /etc/modprobe.d/params.conf:

Text
options params count=3 greeting=hi

Every modprobe params, including automatic loads, then uses those values. Settings given on the command line take precedence over the file.

On the kernel command line

If the code is built into the kernel instead of being a module, the same parameters are set at boot with the module name as a prefix: params.count=3 params.greeting=hi. This is the reason the parameter name is stable and documented.

At run time through sysfs

Parameters with a permission that allows it can be read and changed in sysfs:

Shell
cat /sys/module/params/parameters/shout
echo 1 | sudo tee /sys/module/params/parameters/shout
cat /sys/module/params/parameters/shout
Text
N
1
Y

Boolean parameters read as Y or N. Writing the value changes the variable immediately, without notifying the module. If the module must react to a change, it needs a custom setter through module_param_cb, which takes a struct kernel_param_ops with set and get callbacks. Without it, the module must re-read the variable each time it uses it.

See the description

modinfo prints everything declared with MODULE_PARM_DESC:

Shell
modinfo -p ./params.ko
Text
count:How many times to repeat the greeting (int)
greeting:Text to print at load time (charp)
shout:Print in upper case (bool)

Each line shows the name, the description and the type.

Design guidance

Keep the number of parameters small. Each one is part of the module's interface and a promise to users, and a parameter that exists "just in case" becomes a compatibility burden. Prefer a runtime interface such as sysfs attributes, ioctl or a debugfs file for anything users will change while the module runs, because it can validate and apply a change. And do not parse strings in the kernel more than needed: charp gives you a plain C string and the module must validate its contents.

Validating values with a custom setter

A plain module_param stores whatever the user supplies, as long as it parses as the type. If a value must be in a range, the check has to be written. module_param_cb registers a struct kernel_param_ops with your own set function, which the kernel calls both at load time and when sysfs is written:

range.cc
static int level = 3;

static int level_set(const char *val, const struct kernel_param *kp)
{
	int n, ret;

	ret = kstrtoint(val, 0, &n);
	if (ret)
		return ret;
	if (n < 0 || n > 7)
		return -EINVAL;

	return param_set_int(val, kp);
}

static const struct kernel_param_ops level_ops = {
	.set = level_set,
	.get = param_get_int,
};

module_param_cb(level, &level_ops, &level, 0644);
MODULE_PARM_DESC(level, "Log level from 0 to 7");

With this version, insmod range.ko level=9 fails with an invalid-parameter error, and echo 9 > /sys/module/range/parameters/level returns an error to the shell instead of silently storing the value:

Text
tee: /sys/module/range/parameters/level: Invalid argument

kstrtoint parses a string safely and returns a negative errno on bad input, and param_set_int is the kernel's own setter that stores the value, so you only add the range check. This is the pattern used by many in-tree drivers, and it is the right answer whenever a parameter has constraints or the module must react to a change.

Security and stability notes

A parameter is an input to kernel code, so treat it as untrusted even though only root can set it. A buffer size taken from a parameter must be bounded before it is used to allocate memory or index an array, and a pointer or path taken from a string parameter must be validated like any user input. Also remember that a world-readable parameter file exposes its value to every user; do not put a secret or a key in a parameter, since /proc/cmdline and /sys/module make it visible. When the value has no reason to change after load, make it 0444 or 0, so a mistaken write cannot destabilize a running module.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.