Tasklet Schedule
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 ?
What are the different ways to create tasklet ?
How to schedule the tasklet ?
How to kill the tasklet ?
How to use tasklet APIs ?
How to use below APIs ?
Here is the function prototype of the API: DECLARE_TASKLET
#include <linux/interrupt.h>
#define DECLARE_TASKLET(name, _callback)
Here is an example of how to use the API,
DECLARE_TASKLET(my_tasklet, (void *)tasklet_handler);
Here is the function prototype of the API: tasklet_hi_schedule
#include <linux/interrupt.h>
static inline void tasklet_hi_schedule(struct tasklet_struct *t);
Here is an example of how to use the API,
tasklet_hi_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 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 schedule the tasklet 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");
Declare and initialize the tasklet which we are going to create and use in this example
DECLARE_TASKLET(my_tasklet, (void *)tasklet_handler);
This function executes once the tasklet is scheduled using tasklet_schedule.
void my_tasklet_handler(struct tasklet_struct *t)
{
int *ptr;
ptr = (int *) t->data;
pr_info("Executing asklet handler function\n");
pr_info("ptr value : %ld\n", (unsigned long)*ptr);
}
This function schedules a tasklet.
void tasklet_start(void)
{
my_tasklet.data = (unsigned long)&a;
pr_info("Scheduling the tasklet\n");
tasklet_hi_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);
}
This function calls tasklet_stop function, will be executed once the module is removed from the linux kernel
static void __exit task_exit(void)
{
tasklet_stop();
pr_info("driver unloaded!\n");
}
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/* Scheduling high priority tasklet : tasklet_hi_schedule */
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("Linux_usr");
11MODULE_DESCRIPTION("Tasklet");
12
13void my_tasklet_handler(struct tasklet_struct *t);
14void tasklet_start(void);
15void tasklet_stop(void);
16
17DECLARE_TASKLET(my_tasklet, (void *)my_tasklet_handler);
18
19int a = 10;
20
21/* tasklet_handler - it executes once the tasklet is scheduled using tasklet_schedule */
22void my_tasklet_handler(struct tasklet_struct *t)
23{
24 int *ptr;
25 ptr = (int *) t->data;
26
27 pr_info("Executing asklet handler function\n");
28 pr_info("ptr value : %ld\n", (unsigned long)*ptr);
29}
30
31/* tasklet_start - schedules a high priority tasklet using tasklet_hi_schedule */
32void tasklet_start(void)
33{
34 my_tasklet.data = (unsigned long)&a;
35 pr_info("Scheduling the tasklet\n");
36 tasklet_hi_schedule(&my_tasklet);
37}
38
39/* task_init - calls tasklet_start function,
40 * will be executed once the module is loaded into the linux kernel
41 */
42static int __init task_init(void)
43{
44 pr_info("driver loaded\n");
45 tasklet_start();
46 return 0;
47}
48
49/* tasklet_stop - kills tasklet and stops it's execution */
50void tasklet_stop(void)
51{
52 tasklet_kill(&my_tasklet);
53}
54
55/* task_exit - calls tasklet_stop function,
56 * will be executed once the module is removed from the linux kernel
57 */
58static void __exit task_exit(void)
59{
60 tasklet_stop();
61 pr_info("driver unloaded\n");
62}
63
64module_init(task_init);
65module_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/schedule/tc1 modules
make[1]: Entering directory '/home/arun/Desktop/kernel_api/linux-6.8'
CC [M] /home/arun/Desktop/kernel_api/tasklet/schedule/tc1/tasklet.o
MODPOST /home/arun/Desktop/kernel_api/tasklet/schedule/tc1/Module.symvers
CC [M] /home/arun/Desktop/kernel_api/tasklet/schedule/tc1/tasklet.mod.o
LD [M] /home/arun/Desktop/kernel_api/tasklet/schedule/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 376
-rw-rw-r-- 1 arun arun 207 Jul 15 16:25 Makefile
-rw-rw-r-- 1 arun arun 61 Jul 15 16:25 modules.order
-rw-rw-r-- 1 arun arun 0 Jul 15 16:25 Module.symvers
-rw-rw-r-- 1 arun arun 1535 Jun 25 11:55 tasklet.c
-rw-rw-r-- 1 arun arun 177872 Jul 15 16:25 tasklet.ko
-rw-rw-r-- 1 arun arun 61 Jul 15 16:25 tasklet.mod
-rw-rw-r-- 1 arun arun 1042 Jul 15 16:25 tasklet.mod.c
-rw-rw-r-- 1 arun arun 149144 Jul 15 16:25 tasklet.mod.o
-rw-rw-r-- 1 arun arun 30104 Jul 15 16:25 tasklet.o
Run modinfo command to get the information about the kernel module.
$ modinfo tasklet.ko
filename: /home/arun/Desktop/kernel_api/tasklet/schedule/tc1/tasklet.ko
description: Tasklet
author: Linux_usr
license: GPL
srcversion: 7C8393D7358C934E5135044
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
[ 486.310964] driver loaded
[ 486.310966] Scheduling the tasklet
[ 486.310972] Executing asklet handler function
[ 486.310973] ptr value : 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