Creating Tasklet : Dynamic Method
Topics in this section,
# |
Version |
---|---|
Ubuntu |
Ubuntu 22.10 |
Kernel |
6.8.0 |
In this program, you are going to learn
How to create tasklet in dynamic method ?
What are the different ways to create tasklet ?
How to kill the tasklet ?
How to use tasklet APIs ?
How to use below APIs ?
Here is the function prototype of the API: tasklet_init
#include <linux/interrupt.h>
extern void tasklet_init(struct tasklet_struct *t, void (*func)(unsigned long), unsigned long data);
Here is an example of how to use the API,
tasklet_init(my_tasklet, tasklet_handler, 10);
Here is the function prototype of the API: tasklet_schedule
#include <linux/interrupt.h>
static inline void tasklet_schedule(struct tasklet_struct *t);
Here is an example of how to use the API,
tasklet_schedule(my_tasklet);
Here is the function prototype of the API: tasklet_kill
#include <linux/interrupt.h>
extern void tasklet_kill(struct tasklet_struct *t);
Here is an example of how to use the API,
tasklet_kill(my_tasklet);
Here is the function prototype of the API: tasklet_setup
#include <linux/interrupt.h>
extern void tasklet_setup(struct tasklet_struct *t, void (*callback)(struct tasklet_struct *));
Here is an example of how to use the API,
tasklet_setup(&task_t->tasklet, tasklet_handler);
Here is the prototype of module paramter APIs
#include <linux/module.h>
#define MODULE_LICENSE(_license) MODULE_FILE MODULE_INFO(license, _license)
#define MODULE_AUTHOR(_author) MODULE_INFO(author, _author)
#define MODULE_DESCRIPTION(_description) MODULE_INFO(description, _description)
where
MODULE_LICENSE: tells the kernel what license is used by our module.
MODULE_AUTHOR: denotes the author of this kernel module.
MODULE_DESCRIPTION: gives a basic idea about what the kernel module does.
These information can be found when modinfo command is used which lists out all these above mentioned information.
Here is the example of how to use the Module parameter APIs,
MODULE_LICENSE("GPL");
MODULE_AUTHOR("usr");
MODULE_DESCRIPTION("Tasklet");
Here is the prototype of the Driver entry point API’s
#include <linux/module.h>
#define module_init(x) __initcall(x);
#define module_exit(x) __exitcall(x);
where
module_init: driver initialization entry point which will be called at module insertion time.
module_exit: driver exit entry point which will be called during the removal of module.
- x:
function to be run at module insertion for module_init function.
function to be run when driver is removed for module_exit function.
Here is an example of how to use the driver entry point API’s
module_init(task_init);
module_exit(task_exit);
In this example let’s see how to create tasklet dynamically using tasklet_init and execute it.
Include the follow header files(.h) to refer the API being used for the execution.
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
Add the following module macros to display information about the license, author and description about the module.
MODULE_LICENSE("GPL");
MODULE_AUTHOR("usr");
MODULE_DESCRIPTION("Tasklet");
Initialize the tasklet which we are going to create and use in this example
tasklet_init(my_tasklet, tasklet_handler, 10);
This function executes once the tasklet is scheduled using tasklet_schedule.
void tasklet_handler(unsigned long data)
{
pr_info("Executing tasklet handler function\n");
pr_info("data : %ld\n", data);
}
This function schedules a tasklet.
void tasklet_start(void)
{
my_tasklet = kmalloc(sizeof(struct tasklet_struct), GFP_KERNEL);
if (my_tasklet == NULL)
pr_info("Cannot allocate memory\n");
tasklet_init(my_tasklet, tasklet_handler, 10);
pr_info("Scheduling the tasklet\n");
tasklet_schedule(my_tasklet);
}
This function calls tasklet_start function, will be executed once the module is loaded into the linux kernel.
static int __init task_init(void)
{
pr_info("driver loaded\n");
tasklet_start();
return 0;
}
This function kills tasklet and stops it’s execution.
void tasklet_stop(void)
{
tasklet_kill(my_tasklet);
if (my_tasklet != NULL)
kfree(my_tasklet);
}
This function calls tasklet_stop function, will be executed once the module is removed from the linux kernel
static void __exit task_exit(void)
{
pr_info("driver unloaded!\n");
tasklet_stop();
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(task_init);
module_exit(task_exit);
1#include <linux/init.h>
2#include <linux/kernel.h>
3#include <linux/module.h>
4#include <linux/fs.h>
5#include <linux/interrupt.h>
6
7MODULE_LICENSE("GPL");
8MODULE_AUTHOR("usr");
9MODULE_DESCRIPTION("Tasklet");
10
11struct tasklet_struct *my_tasklet;
12void tasklet_handler(unsigned long data);
13void tasklet_start(void);
14void tasklet_stop(void);
15
16/* tasklet_handler - it executes once the tasklet is scheduled using tasklet_schedule */
17void tasklet_handler(unsigned long data)
18{
19 pr_info("Executing tasklet handler function\n");
20 pr_info("data : %ld\n", data);
21}
22
23/* tasklet_start - initializes the tasklet dynamically using tasklet_init
24 * schedules a tasklet using tasklet_schedule
25 */
26void tasklet_start(void)
27{
28 my_tasklet = kmalloc(sizeof(struct tasklet_struct), GFP_KERNEL);
29 if (my_tasklet == NULL)
30 pr_info("Cannot allocate memory\n");
31 tasklet_init(my_tasklet, tasklet_handler, 10);
32
33 pr_info("Scheduling the tasklet\n");
34 tasklet_schedule(my_tasklet);
35}
36
37/* task_init - calls tasklet_start function,
38 * will be executed once the module is loaded into the linux kernel
39 */
40
41static int __init task_init(void)
42{
43 pr_info("driver loaded\n");
44 tasklet_start();
45 return 0;
46}
47
48/* tasklet_stop - kills tasklet and stops it's execution */
49
50void tasklet_stop(void)
51{
52 tasklet_kill(my_tasklet);
53 if (my_tasklet != NULL)
54 kfree(my_tasklet);
55}
56
57/* task_exit - calls tasklet_stop function,
58 * will be executed once the module is removed from the linux kernel
59 */
60
61static void __exit task_exit(void)
62{
63 pr_info("driver unloaded!\n");
64 tasklet_stop();
65}
66
67module_init(task_init);
68module_exit(task_exit);
1obj-m += tasklet.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5 sudo ../../checkpatch.pl --no-tree -f tasklet.c
6
7clean:
8 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run make to compile the kernel source and generate the .ko image.
$ make
make -C /lib/modules/6.8.0/build M=/home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc1 modules
make[1]: Entering directory '/home/arun/Desktop/kernel_api/linux-6.8'
CC [M] /home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc1/tasklet.o
MODPOST /home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc1/Module.symvers
CC [M] /home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc1/tasklet.mod.o
LD [M] /home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc1/tasklet.ko
make[1]: Leaving directory '/home/arun/Desktop/kernel_api/linux-6.8'
Check if the .ko is generated or not using ls command.
$ ls -l
total 380
-rw-rw-r-- 1 arun arun 206 Jun 21 14:46 Makefile
-rw-rw-r-- 1 arun arun 67 Jul 11 16:37 modules.order
-rw-rw-r-- 1 arun arun 0 Jul 11 16:37 Module.symvers
-rw-rw-r-- 1 arun arun 1586 Jun 24 18:37 tasklet.c
-rw-rw-r-- 1 arun arun 180456 Jul 11 16:37 tasklet.ko
-rw-rw-r-- 1 arun arun 67 Jul 11 16:37 tasklet.mod
-rw-rw-r-- 1 arun arun 1166 Jul 11 16:37 tasklet.mod.c
-rw-rw-r-- 1 arun arun 149416 Jul 11 16:37 tasklet.mod.o
-rw-rw-r-- 1 arun arun 32432 Jul 11 16:37 tasklet.o
Run modinfo command to get the information about the kernel module.
$ modinfo tasklet.ko
filename: /home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc1/tasklet.ko
description: Tasklet
author: usr
license: GPL
srcversion: 6FA47F552DD69ABBBA7E0D3
depends:
retpoline: Y
name: tasklet
vermagic: 6.8.0 SMP preempt mod_unload modversions
insert the module using insmod command.
$ sudo insmod ./tasklet.ko
check if the module is loaded or not using lsmod command.
$ lsmod | grep tasklet
tasklet 12288 0
check for the kernel messages from init function and thread function once the module is loaded and thread is created.
$ sudo dmesg
[534946.455200] driver unloaded
[536173.924724] driver loaded
[536173.924728] Scheduling the tasklet
[536173.924888] Executing tasklet handler function
[536173.924891] data : 10
remove the module from kernel using rmmod command.
$ sudo rmmod tasklet
check if the module is still loaded after removing the kernel module using lsmod if it is not displayed in lsmod output it is verified that the module is removed successfully.
$ lsmod | grep tasklet
Check for kernel messages from exit function using dmesg command.
$ sudo dmesg
[534946.455200] driver unloaded
In this example let’s see how to create tasklet dynamically using tasklet_setup and execute it.
Include the follow header files(.h) to refer the API being used for the execution.
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
Add the following module macros to display information about the license, author and description about the module.
MODULE_LICENSE("GPL");
MODULE_AUTHOR("usr");
MODULE_DESCRIPTION("Tasklet");
Initialize the tasklet which we are going to create and use in this example
tasklet_setup(&task_t->tasklet, tasklet_handler);
This function executes once the tasklet is scheduled using tasklet_schedule.
void tasklet_handler(struct tasklet_struct *t)
{
struct task *task_1 = from_tasklet(task_1, t, tasklet);
pr_info("Inside tasklet handler function\n");
pr_info("task_a : %d\n", task_1->task_a);
pr_info("task_b : %d\n", task_1->task_b);
pr_info("task_c : %c\n", task_1->task_c);
pr_info("task_d : %s\n", task_1->task_d);
}
This function schedules a tasklet.
void tasklet_start(void)
{
task_t = kmalloc(sizeof(struct tasklet_struct *), GFP_KERNEL);
if (task_t == NULL)
pr_info("Cannot allocate memory\n");
task_t->task_a = 10;
task_t->task_b = 20;
task_t->task_c = 'a';
strncpy(task_t->task_d, "hello", sizeof(task_t->task_d));
tasklet_setup(&task_t->tasklet, tasklet_handler);
pr_info("Scheduling the tasklet\n");
tasklet_schedule(&task_t->tasklet);
}
This function calls tasklet_start function, will be executed once the module is loaded into the linux kernel.
static int __init task_init(void)
{
pr_info("driver loaded\n");
tasklet_start();
return 0;
}
This function kills tasklet and stops it’s execution.
void tasklet_stop(void)
{
tasklet_kill(&task_t->tasklet);
if (task_t != NULL)
kfree(task_t);
}
This function calls tasklet_stop function, will be executed once the module is removed from the linux kernel
static void __exit task_exit(void)
{
pr_info("driver unloaded!\n");
tasklet_stop();
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(task_init);
module_exit(task_exit);
1/* Creating a tasklet : Dynamic Method : tasklet_setup */
2
3#include <linux/init.h>
4#include <linux/kernel.h>
5#include <linux/module.h>
6#include <linux/fs.h>
7#include <linux/interrupt.h>
8
9MODULE_LICENSE("GPL");
10MODULE_AUTHOR("usr");
11MODULE_DESCRIPTION("Tasklet");
12
13void tasklet_handler(struct tasklet_struct *t);
14void tasklet_start(void);
15void tasklet_stop(void);
16
17struct task {
18 struct tasklet_struct tasklet;
19 int task_a;
20 int task_b;
21 char task_c;
22 char task_d[10];
23};
24
25struct task *task_t;
26
27/* tasklet_handler - it executes once the tasklet is scheduled using tasklet_schedule */
28void tasklet_handler(struct tasklet_struct *t)
29{
30 struct task *task_1 = from_tasklet(task_1, t, tasklet);
31
32 pr_info("Inside tasklet handler function\n");
33 pr_info("task_a : %d\n", task_1->task_a);
34 pr_info("task_b : %d\n", task_1->task_b);
35 pr_info("task_c : %c\n", task_1->task_c);
36 pr_info("task_d : %s\n", task_1->task_d);
37}
38
39/* tasklet_start - initializes tasklet dynamically
40 * schedules a tasklet
41 */
42void tasklet_start(void)
43{
44 task_t = kmalloc(sizeof(struct tasklet_struct *), GFP_KERNEL);
45 if (task_t == NULL)
46 pr_info("Cannot allocate memory\n");
47
48 task_t->task_a = 10;
49 task_t->task_b = 20;
50 task_t->task_c = 'a';
51 strncpy(task_t->task_d, "hello", sizeof(task_t->task_d));
52 tasklet_setup(&task_t->tasklet, tasklet_handler);
53
54 pr_info("Scheduling the tasklet\n");
55 tasklet_schedule(&task_t->tasklet);
56}
57
58/* task_init - calls tasklet_start function,
59 * will be executed once the module is loaded into the linux kernel
60 */
61static int __init task_init(void)
62{
63 pr_info("driver loaded\n");
64 tasklet_start();
65 return 0;
66}
67
68/* tasklet_stop - kills tasklet and stops it's execution */
69void tasklet_stop(void)
70{
71 tasklet_kill(&task_t->tasklet);
72 if (task_t != NULL)
73 kfree(task_t);
74}
75
76/* task_exit - calls tasklet_stop function,
77 * will be executed once the module is removed from the linux kernel
78 */
79static void __exit task_exit(void)
80{
81 pr_info("driver unloaded!\n");
82 tasklet_stop();
83}
84
85module_init(task_init);
86module_exit(task_exit);
1obj-m += tasklet.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5 sudo ../../checkpatch.pl --no-tree -f tasklet.c
6
7clean:
8 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run make to compile the kernel source and generate the .ko image.
$ make
make -C /lib/modules/6.8.0/build M=/home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc2 modules
make[1]: Entering directory '/home/arun/Desktop/kernel_api/linux-6.8'
CC [M] /home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc2/tasklet.o
MODPOST /home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc2/Module.symvers
CC [M] /home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc2/tasklet.mod.o
LD [M] /home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc2/tasklet.ko
make[1]: Leaving directory '/home/arun/Desktop/kernel_api/linux-6.8'
Check if the .ko is generated or not using ls command.
$ ls -l
total 384
-rw-rw-r-- 1 arun arun 206 Jun 21 15:08 Makefile
-rw-rw-r-- 1 arun arun 67 Jul 11 16:47 modules.order
-rw-rw-r-- 1 arun arun 0 Jul 11 16:47 Module.symvers
-rw-rw-r-- 1 arun arun 2026 Jun 24 15:24 tasklet.c
-rw-rw-r-- 1 arun arun 182688 Jul 11 16:47 tasklet.ko
-rw-rw-r-- 1 arun arun 67 Jul 11 16:47 tasklet.mod
-rw-rw-r-- 1 arun arun 1167 Jul 11 16:47 tasklet.mod.c
-rw-rw-r-- 1 arun arun 149416 Jul 11 16:47 tasklet.mod.o
-rw-rw-r-- 1 arun arun 34664 Jul 11 16:47 tasklet.o
Run modinfo command to get the information about the kernel module.
$ modinfo tasklet.ko
filename: /home/arun/Desktop/kernel_api/tasklet/create_dynamic/tc2/tasklet.ko
description: Tasklet
author: usr
license: GPL
srcversion: 3D90BF7ADED729B5A2D4BF7
depends:
retpoline: Y
name: tasklet
vermagic: 6.8.0 SMP preempt mod_unload modversions
insert the module using insmod command.
$ sudo insmod ./tasklet.ko
check if the module is loaded or not using lsmod command.
$ lsmod | grep tasklet
tasklet 12288 0
check for the kernel messages from init function and thread function once the module is loaded and thread is created.
$ sudo dmesg
[536418.420974] driver loaded
[536418.420979] Scheduling the tasklet
[536418.421469] Inside tasklet handler function
[536418.421471] task_a : 10
[536418.421474] task_b : 20
[536418.421476] task_c : a
[536418.421477] task_d : hello
remove the module from kernel using rmmod command.
$ sudo rmmod tasklet
check if the module is still loaded after removing the kernel module using lsmod if it is not displayed in lsmod output it is verified that the module is removed successfully.
$ lsmod | grep tasklet
Check for kernel messages from exit function using dmesg command.
$ sudo dmesg
[534946.455200] driver unloaded