Tasklet : One Shot

  • In this program, you are going to learn

  • How to schedule the tasklet ?

  • Tasklets are used to queue up work to be done at a later time.

  • DECLARE_TASKLET macro used to create the tasklet structure and assigns the parameters to that structure. For example,

DECLARE_TASKLET(tasklet, (void * )my_tasklet_handler, 0);
  • tasklet_schedule Schedule a tasklet with a normal priority. If a tasklet has previously been scheduled (but not yet run), the new schedule will be silently discarded. For example,

tasklet_schedule(&tasklet);
  • Here is the explanation of the program part by part

  • This contains the list of all .h files which needs to be included to refer to the APIs included in this program.

#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/interrupt.h>
  • Add the macro modules which lists out the information about the module such as license, author and description.

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux_usr");
MODULE_DESCRIPTION("Tasklet");
  • Initialize tasklet and the tasklet handler APIs.

void my_tasklet_handler(unsigned long data);
DECLARE_TASKLET(tasklet, (void *)my_tasklet_handler, 0);
  • Add the definitions for the module init functions which contains the instructions what the module need to do once it is loaded.

static int __init tasklet_init(void)
{
    pr_info("Hello, world\n");
    tasklet_schedule(&tasklet);
    return 0;
}
  • Add the definitions for the module exit function which contains the instructions which needs to executed once the module is unloaded from the kernel.

static void __exit tasklet_exit(void)
{
    pr_info("Goodbye, world\n");
}
  • Add the function definition of the tasklet handler API since this tasklet handler API is passed as an argument in DECLARE_TASKLET and will be executed once the tasklet_schedule is called.

void my_tasklet_handler(unsigned long data)
{
    pr_info("Tasklet function executed\n");
}
  • Add the init and exit functions as a parameters to module_init and module_exit respectively.

module_init(tasklet_init);
module_exit(tasklet_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("Linux_usr");
 9MODULE_DESCRIPTION("Tasklet");
10
11void my_tasklet_handler(unsigned long data);
12DECLARE_TASKLET(tasklet, (void *)my_tasklet_handler, 0);
13
14void my_tasklet_handler(unsigned long data)
15{
16	pr_info("Tasklet function executed\n");
17}
18
19static int __init tasklet_init(void)
20{
21	pr_info("Hello, world\n");
22	tasklet_schedule(&tasklet);
23	return 0;
24}
25
26static void __exit tasklet_exit(void)
27{
28	pr_info("Goodbye, world\n");
29}
30
31module_init(tasklet_init);
32module_exit(tasklet_exit);
1obj-m += tasklets.o
2all:
3	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
4clean:
5	make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
  • Run make to compile the kernel module.

$make
make -C /lib/modules/5.4.0-150-generic/build M=$HOME/kernel_tasklets modules
make[1]: Entering directory '/usr/src/linux-headers-5.4.0-150-generic'
  CC [M]  $HOME/kernel_tasklets/tasklets.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC [M]  $HOME/kernel_tasklets/tasklets.mod.o
  LD [M]  $HOME/kernel_tasklets/tasklets.ko
make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-150-generic'
  • Check whether the tasklets.ko is generated or not using ls command.

$ ls -l
total 36
-rw-rw-r-- 1 test test  154 Feb 26 13:18 Makefile
-rw-rw-r-- 1 test test   47 Feb 26 13:18 modules.order
-rw-rw-r-- 1 test test    0 Feb 26 13:18 Module.symvers
-rw-rw-r-- 1 test test  820 Feb 26 13:18 tasklets.c
-rw-rw-r-- 1 test test 5880 Feb 26 13:18 tasklets.ko
-rw-rw-r-- 1 test test   47 Feb 26 13:18 tasklets.mod
-rw-rw-r-- 1 test test  919 Feb 26 13:18 tasklets.mod.c
-rw-rw-r-- 1 test test 3448 Feb 26 13:18 tasklets.mod.o
-rw-rw-r-- 1 test test 3320 Feb 26 13:18 tasklets.o
  • Load the module using insmod command.

$ sudo insmod ./tasklets.ko
  • Check the kernel messages using dmesg to verify whether the module is loaded or not.

$ dmesg
[  982.336636] Hello, world
[  982.336644] Tasklet function executed
  • Unload the module from linux kernel using rmmod command.

$ sudo rmmod tasklets
  • Check the kernel messages to verify whether the module is unloaded or not.

$ dmesg
[  982.336636] Hello, world
[  982.336644] Tasklet function executed
[  989.128409] Goodbye, world

API

Learning

DECLARE_TASKLET

To create the tasklet structure

tasklet_schedule

To schedule a tasklet with a normal priority

API

Learning

pr_info

Prints an info-level message