Binding Kthread
Topics in this section,
# |
Version |
---|---|
Ubuntu |
Ubuntu 22.10 |
Kernel |
6.7.9 |
In this program, you are going to learn
How to create the kernel thread ?
How to bind the created thread to cpu?
How to stop the kernel thread ?
How to use Kthread APIs ?
How to use below APIs ?
Here is the function prototype of the API: kthread_create
#include <linux/kthread.h>
#define kthread_create(threadfn, data, namefmt, arg...) \
kthread_create_on_node(threadfn, data, NUMA_NO_NODE, namefmt, ##arg)
where
kthread_create: creates a kthread and can be waked up to start its execution using wake_up_process
return type: struct task_struct* (i.e) returns the address of the kthread
threadfn: the function to execute on kthread
data: data pointer for the threadfn which can be used to pass any possible arguments required for the threadfn.
namefmt: printf-style format for the thread name which will be displayed on ps output when the thread is in execution.
Here is an example of how to use the API,
kthread_create(mythread,NULL,"sample kthread");
Here is the function prototype of the API: kthread_should_stop
#include <linux/kthread.h>
bool kthread_should_stop(void);
where
kthread_should_stop: this is used to determine whether the thread should return now. Whenever the kthread_stop() is called it will be woken and returns true.
return type: returns true when the thread is stopped, false when the thread is still in execution
Here is an example of how to use the API,
while (!kthread_should_stop())
{
//add the instructions to be performed during thread execution.
}
Here is the function prototype of the API: kthread_bind
#include <linux/kthread.h>
void kthread_bind(struct task_struct *k, unsigned int cpu);
where
kthread_bind : bind a just-created thread to cpu
k: thread created using any of the thread creation APIs
cpu: cpu for the thread to run on
Here is an example of how to use the API,
kthread_bind(mythread,cpu);
Here is the function prototype of the API: kthread_stop
#include <linux/kthread.h>
int kthread_stop(struct task_struct *k);
where
kthread_stop: stops a kthread.
return type: returns the return value of threadfn() which is passed as an argument during kthread creation.
k: kthread created by one of the API used to created kthread.
Here is the example of how to use the API,
kthread_stop(mythread);
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("Linux Usr");
MODULE_DESCRIPTION("Sample kernel module");
Here is the prototype of the API: IS_ERR
#include <linux/err.h>
static inline bool __must_check IS_ERR(__force const void *ptr);
where
IS_ERR: Detects an error pointer
return type: return true if it’s an error pointer else return false.
ptr: pointer which needs to detected.
Here is an example of how to use the API,
if(IS_ERR(sample_ptr)) {
//instructions to be executed when error ptr is detected
} else {
//instructions to be executed when error ptr is not detected
}
Here is the example of the API: wake_up_process
#include <linux/sched.h>
extern int wake_up_process(struct task_struct *tsk);
where
wake_up_process: wake up a specific process
return type: returns 1 if the process is woken up, 0 if the process is in running state.
tsk: process to be woken up.
Here is the example of how to use the API,
wake_up_process(mythread);
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(myinitmodule);
module_exit(myexitmodule);
In this example let’s see how to bind a thread created using any of the thread creation APIs to CPU using kthread_bind 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/kthread.h>
#include <linux/delay.h>
#include <linux/slab.h>
#include <linux/threads.h>
Add the following module macros to display information about the license, author and description about the module.
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Linux Usr");
MODULE_DESCRIPTION("Example of kthread_bind");
Declare the thread variables which we are going to create and use in this example
static struct task_struct *def_thread;
unsigned int cpu;
Add the module init function which will be executed once we load the kernel module using insmod command.
static int __init kthread_bind_init(void)
{
pr_info("Inside kthread init function\n");
thread_start();
return 0;
}
Add the thread start function which creates the thread, binds it to the current node and starts the execution.
void thread_start(void)
{
def_thread = kthread_create(my_thread_fn,NULL,"kthread_bind example");
if(IS_ERR(def_thread))
pr_info("Error in creating thread\n");
else {
pr_info("Thread created successfully\n");
cpu = 0;
kthread_bind(def_thread,cpu);
pr_info("Thread binded successfully\n");
wake_up_process(def_thread);
pr_info("wake up process done successfully\n");
}
}
Add the module exit function which will be executed once we unload the kernel module using rmmod command.
static void __exit kthread_bind_exit(void)
{
kthread_stop(def_thread);
pr_info("Destroy thread\n");
}
Add the thread stop function which destroys the thread and stops execution.
void thread_stop(void)
{
kthread_stop(def_thread);
pr_info("Destroyed thread\n");
}
Add the thread function API which will be called as soon as the kthread is created and is in running state.
int my_thread_fn(void *data)
{
while(!kthread_should_stop()) {
pr_info("inside my_thread_fn\n");
msleep(100);
}
return 0;
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(kthread_bind_init);
module_exit(kthread_bind_exit);
1#include <linux/init.h>
2#include <linux/kernel.h>
3#include <linux/module.h>
4#include <linux/kthread.h>
5#include <linux/delay.h>
6#include <linux/slab.h>
7#include <linux/threads.h>
8
9MODULE_LICENSE("GPL");
10MODULE_AUTHOR("Linux Usr");
11MODULE_DESCRIPTION("Example of kthread_bind");
12
13static struct task_struct *def_thread;
14unsigned int cpu;
15
16/* my_thread_fn - this functions executes once the thread is created,
17 * continues it execution until kthread_stop() is being called,
18 * prints a messages and goes to sleep for 100ms. */
19
20int my_thread_fn(void *data)
21{
22 while(!kthread_should_stop()) {
23 pr_info("inside my_thread_fn\n");
24 msleep(100);
25 }
26 return 0;
27}
28
29/* thread_start - create a kthread,
30 * if success bind the thread to current node,
31 * wake up the thread and start execution. */
32
33void thread_start(void)
34{
35 def_thread = kthread_create(my_thread_fn,NULL,"kthread_bind example");
36 if(IS_ERR(def_thread))
37 pr_info("Error in creating thread\n");
38 else {
39 pr_info("Thread created successfully\n");
40 cpu = 0;
41 kthread_bind(def_thread,cpu);
42 pr_info("Thread binded successfully\n");
43 wake_up_process(def_thread);
44 pr_info("wake up process done successfully\n");
45 }
46}
47
48/* kthread_bind_init - calls thread_start function,
49 * will execute when the module is loaded. */
50
51static int __init kthread_bind_init(void)
52{
53 pr_info("Inside kthread init function\n");
54 thread_start();
55 return 0;
56}
57
58/* thread_stop - destroy the thread created and stop execution */
59
60void thread_stop(void)
61{
62 kthread_stop(def_thread);
63 pr_info("Destroyed thread\n");
64}
65
66/* kthread_bind_exit - calls thread_stop function,
67 * executes once the module is unloaded.*/
68
69static void __exit kthread_bind_exit(void)
70{
71 pr_info("Inside kthread exit function\n");
72 thread_stop();
73}
74
75module_init(kthread_bind_init);
76module_exit(kthread_bind_exit);
1obj-m += kthread.o
2
3all:
4 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules
5
6clean:
7 make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean
Run make to compile the kernel source and generate the .ko image.
make -C /lib/modules/6.7.9/build M=$HOME/kthread_examples/
make[1]: Entering directory '/usr/src/linux-headers-6.7.9'
warning: the compiler differs from the one used to build the kernel
The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
You are using: gcc (Ubuntu 12.2.0-3ubuntu1) 12.2.0
CC [M] $HOME/kthread_examples/kthread.o
MODPOST $HOME/kthread_examples/Module.symvers
CC [M] $HOME/kthread_examples/kthread.mod.o
LD [M] $HOME/kthread_examples/kthread.ko
BTF [M] $HOME/kthread_examples/kthread.ko
Skipping BTF generation for $HOME/kthread_examples/kthread.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-6.7.9'
Check if the .ko is generated or not using ls command.
test@test-V520-15IKL:~$ ls -l
total 360
-rw-rw-r-- 1 test test 713 Mar 18 16:06 kthread.c
-rw-rw-r-- 1 test test 169784 Mar 18 16:08 kthread.ko
-rw-rw-r-- 1 test test 58 Mar 18 16:08 kthread.mod
-rw-rw-r-- 1 test test 1047 Mar 18 16:08 kthread.mod.c
-rw-rw-r-- 1 test test 96512 Mar 18 16:08 kthread.mod.o
-rw-rw-r-- 1 test test 74696 Mar 18 16:08 kthread.o
-rw-rw-r-- 1 test test 161 Mar 18 16:00 Makefile
-rw-rw-r-- 1 test test 58 Mar 18 16:08 modules.order
-rw-rw-r-- 1 test test 0 Mar 18 16:08 Module.symvers
Run modinfo command to get the information about the kernel module.
test@test-V520-15IKL:~/.../tc_1$ modinfo kthread.ko
filename: $HOME/kthread_examples/kthread.ko
description: Example of kthread_bind
author: Linux Usr
license: GPL
srcversion: 8D2147F67AB01CF0E482DAC
depends:
retpoline: Y
name: kthread
vermagic: 6.7.9 SMP preempt mod_unload modversions
insert the module using insmod command.
test@test-V520-15IKL:~$ sudo insmod ./kthread.ko
check if the module is loaded or not using lsmod command.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
kthread 16384 0
check if the thread is created or not using ps command.
test@test-V520-15IKL:~$ ps -N | grep kthread_bind
11077 ? 00:00:00 kthread_bind example
check for the kernel messages from init function and thread function once the module is loaded and thread is created.
test@test-V520-15IKL:~$ sudo dmesg
[17651.010835] Inside kthread init function
[17651.010899] Thread created successfully
[17651.010902] Thread binded successfully
[17651.010905] wake up process done successfully
[17651.010912] inside my_thread_fn
[17651.116561] inside my_thread_fn
[17651.224588] inside my_thread_fn
remove the module from kernel using rmmod command.
test@test-V520-15IKL:~$ sudo rmmod kthread
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.
test@test-V520-15IKL:~$ sudo lsmod | grep kthread
test@test-V520-15IKL:~$
check if the thread is destroyed using ps command if it is not displayed in ps output we can confirm that the thread is destroyed successfully.
test@test-V520-15IKL:~$ ps -N | grep kthread_bind
test@test-V520-15IKL:~$
Check for kernel messages from exit function using dmesg command.
test@test-V520-15IKL:~$ sudo dmesg
[17655.687989] Inside kthread exit function
[17655.776798] Destroyed thread
kthread API |
Learning |
---|---|
kthread_create |
Create a thread |
kthread_bind |
Bind a just created thread to cpu |
kthread_should_stop |
To determine when thread should exit |
kthread_stop |
Stop a thread created by kthread_create |
API |
Learning |
---|---|
MODULE_LICENSE |
Used to denote the license used in the kernel module |
MODULE_AUTHOR |
Used to mention the author of the kernel module |
MODULE_DESCRIPTION |
Used to describe what the module does |
IS_ERR |
Detects an error pointer |
wake_up_process |
wake up a specific process |
msleep |
will put in sleep for a certain amount of msecs time. |
module_init |
Driver initialization entry point |
module_exit |
Driver exit entry point |
pr_info |
Print an info-level message |