in todays lecture we will discuss what is invbool in linux kernel module parameters, Hello students welcome to free embedded systems course online by embedded pathashala,
What is invbool in linux kernel module parameters
When working with linux kernel modules, parassing parameters at the load time is one of the most power full feature. Among the supported parameter types invbool is one of the less known but very usefultype. so in this will lecture i will explain you what is invbool in linux kernel module parameters.
what is invbool?
invbool is a special type of module parameter that is used in linux kernel modules, it has functionlaity like boolean(true/false), but catch is it inverts the value provided by the user.
so for example if you pass 1 (true) -> kernel interprets it as 0 (false)
if you pass 0 (false) -> kernel interprts it as 1 (true)
why we use invbool?
invbool is a type of parameter which is used when you want a parameter to enabled by default and you also want to allow users to disable it explicity, this will simplify the logic in some driver designs.
Example for invbool
#include<linux/init.h>
#include<linux/module.h>
#include<linux/moduleparam.h>
MODULE_LICENSE("GPL");
MODULE_AUTHOR("EMBEDDED PATHASHALA");
static bool inv_bool = true;
/* invbool parameter */
module_param(inv_bool, invbool, S_IRUGO);
static int init_fun(void)
{
pr_info("inv_bool test : %s\n", inv_bool ? "true" : "false");
return 0;
}
static void exit_fun(void)
{
return;
}
module_init(init_fun);
module_exit(exit_fun);
code explanation
Variable declaration:
static bool inv_bool = true;
module parameter definition
module_param(inv_bool, invbool, S_IRUGO);
Understanding module_param
-
inv_bool→ parameter name -
invbool→ type (inverted boolean) -
S_IRUGO→ read-only permission from sysfs
Kernel Log Output:
pr_info("inv_bool test : %s\n", inv_bool ? "true" : "false");
this will print the output on to the kernel
Makefile:
obj-m := invbool.o
all:
make -C /lib/modules/$(shell uname -r)/build M=${PWD} modules
clean:
make -C /lib/modules/$(shell uname -r)/build M=${PWD} clean
now let us test our module
Testing Module:
1. Load without parameter
raviteja@raviteja-Inspiron-15-3511:~/lsyspg/kernel_programming/Day7$ sudo insmod invbool.ko
now let us see the output
[ 5713.834895] invbool: loading out-of-tree module taints kernel.
[ 5713.834900] invbool: module verification failed: signature and/or required key missing - tainting kernel
[ 5713.835656] inv_bool test : true
when you dont pass any value we will see output as false
2. Load with parameter
sudo insmod invbool.ko inv_bool=1
dmesg -w
and below is the output
5918.095704] inv_bool test : false
now let us see another case
sudo insmod invbool.ko inv_bool=0
and below will be the output,
[ 6027.488582] inv_bool test : true
always do rmmod when you are trying to insert same module again
Truth Table for invbool
| Input Value | Normal bool | invbool Result |
|---|---|---|
| 0 | false | true |
| 1 | true | false |
