Wait for completion
# |
Version |
---|---|
Ubuntu |
Ubuntu 22.10 |
Kernel |
6.7.9 |
In this program, you are going to learn
How to create completion variable ?
How to send completion signal for waiting thread?
How to signal once the task is completed?
How to use Kthread APIs ?
How to use Completion APIs ?
How to use below APIs ?
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_run
#include <linux/kthread.h>
#define kthread_run(threadfn, data, namefmt, ...)
where
kthread_run : is used to create and wake a kthread
return type: struct task_struct* (i.e) address of the kthread
threadfn: function to executed by kthread
data: data pointer for threadfn which can be used to send any possible arguments required for 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_run(mythread,NULL,"sample kthread");
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 the API: init_completion
#include <linux/completion.h>
static inline void init_completion(struct completion *x);
where
init_completion : initialize a dynamically allocated completion
x : pointer to completion structure that is to be initialized
Here is the example of how to use the API
init_completion(mycompletionvar);
Here is the prototype of the API: wait_for_completion
#include <linux/completion.h>
void wait_for_completion(struct completion *x);
where
wait_for_completion : waits for completion of a task
x : holds the state of this particular completion
Here is an example of how to use the API,
wait_for_completion(mycompletionvar);
Here is the prototype of the API: wait_for_completion_timeout
#include <linux/completion.h>
extern unsigned long wait_for_completion_timeout(struct completion *x, unsigned long timeout);
where
wait_for_completion_timeout : This waits for either a completion of a specific task to be signaled or for a specified timeout to expire. The timeout is in jiffies. It is not interruptible.
x : holds the state of this particular completion
timeout : timeout value in jiffies
Here is an example of how to use the API
wait_for_completion_timeout(mycompvar,msecs_to_jiffies(10));
Here is the prototype of the API: complete_all
#include <linux/completion.h>
void complete_all(struct completion *x);
where
complete_all : signals all threads waiting on this completion
x : holds the state of this particular completion
Here is an example of how to use the API
complete_all(mycompvar);
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 wait for completion signal using wait_for_completion and use with kthread.
Include the follow header files(.h) to refer the API being used for the execution.
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/jiffies.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 wait_for_completion");
Declare the thread and completion variables which we are going to create and use in this example
static struct task_struct *my_thread1;
static struct task_struct *my_thread2;
static struct task_struct *my_thread3;
struct completion thread_2_done;
struct completion thread_3_done;
Add the module init function which will be executed once we load the kernel module using insmod command.
static int __init wait_for_completion_init(void)
{
pr_info("Inside thread init function\n");
thread_start();
return 0;
}
Add the thread start function which is called from module init function, creates the thread and starts it’s execution.
void thread_start(void)
{
init_completion(&thread_2_done);
init_completion(&thread_3_done);
my_thread1 = kthread_run(my_thread_fn1,NULL,"wait_for_completion thread1 example");
if (IS_ERR(my_thread1))
pr_info("error creating in kthread\n");
else
pr_info("successfully created kthread\n");
my_thread2 = kthread_run(my_thread_fn2,NULL,"wait_for_completion thread2 example");
if (IS_ERR(my_thread2))
pr_info("error creating in kthread\n");
else
pr_info("successfully created kthread\n");
my_thread3 = kthread_run(my_thread_fn3,NULL,"wait_for_completion thread3 example");
if (IS_ERR(my_thread3))
pr_info("error creating in kthread\n");
else
pr_info("successfully create kthread\n");
}
Add the module exit function which will be executed once we unload the kernel module using rmmod command.
static void __exit wait_for_completion_exit(void)
{
pr_info("Inside kthread exit function\n");
thread_stop();
}
Add the thread stop function which is called from module exit function, destroys the thread created and stops it’s execution.
void thread_stop(void)
{
kthread_stop(my_thread3);
kthread_stop(my_thread2);
kthread_stop(my_thread1);
pr_info("destroyed thread and completed task\n");
}
Add the thread function API which will be called as soon as the kthread is created and is in running state.
static int my_thread_fn1(void *data)
{
while (!kthread_should_stop()) {
wait_for_completion(&thread_2_done);
pr_info("my_thread_fn1 execution\n");
msleep(1000);
}
return 0;
}
static int my_thread_fn2(void *data)
{
while(!kthread_should_stop()) {
pr_info("my_thread_fn2 execution\n");
wait_for_completion(&thread_3_done);
msleep(1000);
}
complete(&thread_2_done);
return 0;
}
static int my_thread_fn3(void *data)
{
while (!kthread_should_stop()) {
pr_info("my_thread_fn3 execution\n");
msleep(2000);
}
complete(&thread_3_done);
return 0;
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(wait_for_completion_init);
module_exit(wait_for_completion_exit);
1#include <linux/kernel.h>
2#include <linux/init.h>
3#include <linux/module.h>
4#include <linux/kthread.h>
5#include <linux/completion.h>
6#include <linux/delay.h>
7#include <linux/jiffies.h>
8
9MODULE_LICENSE("GPL");
10MODULE_DESCRIPTION("Example of wait_for_completion");
11MODULE_AUTHOR("linux usr");
12
13static struct task_struct *my_thread1;
14static struct task_struct *my_thread2;
15static struct task_struct *my_thread3;
16struct completion thread_2_done;
17struct completion thread_3_done;
18
19/* my_thread_fn1 - executes when thread1 is created,
20 * waits for completion signal from thread2,
21 * prints a message and sleeps for 1000ms,
22 * stops when kthread_stop is called */
23
24static int my_thread_fn1(void *data)
25{
26 while (!kthread_should_stop()) {
27 wait_for_completion(&thread_2_done);
28 pr_info("my_thread_fn1 execution\n");
29 msleep(1000);
30 }
31 return 0;
32}
33
34/* my_kthread_fn2 - executes when thread2 is created,
35 * prints a message and sleeps for 1000ms,
36 * waits for completion from thread 3,
37 * sends completion signal for thread 1,
38 * stops when kthread_stop is called */
39
40static int my_thread_fn2(void *data)
41{
42 while(!kthread_should_stop()) {
43 pr_info("my_thread_fn2 execution\n");
44 wait_for_completion(&thread_3_done);
45 msleep(1000);
46 }
47 complete(&thread_2_done);
48 return 0;
49}
50
51/* my_thread_fn3 - executes when thread1 is created,
52 * prints a message and sleeps for 2000ms,
53 * sends completion signal for thread2,
54 * stops when kthread_stop is called */
55
56static int my_thread_fn3(void *data)
57{
58 while (!kthread_should_stop()) {
59 pr_info("my_thread_fn3 execution\n");
60 msleep(2000);
61 }
62 complete(&thread_3_done);
63 return 0;
64}
65
66/* thread_start - create thread,completion var and starts execution */
67
68void thread_start(void)
69{
70 init_completion(&thread_2_done);
71 init_completion(&thread_3_done);
72 my_thread1 = kthread_run(my_thread_fn1,NULL,"wait_for_completion thread1 example");
73 if (IS_ERR(my_thread1))
74 pr_info("error creating in kthread\n");
75 else
76 pr_info("successfully created kthread\n");
77 my_thread2 = kthread_run(my_thread_fn2,NULL,"wait_for_completion thread2 example");
78 if (IS_ERR(my_thread2))
79 pr_info("error creating in kthread\n");
80 else
81 pr_info("successfully created kthread\n");
82 my_thread3 = kthread_run(my_thread_fn3,NULL,"wait_for_completion thread3 example");
83 if (IS_ERR(my_thread3))
84 pr_info("error creating in kthread\n");
85 else
86 pr_info("successfully create kthread\n");
87}
88
89/* wait_for_completion_init - calls thread_start,
90 * executes when module is loaded */
91
92static int __init wait_for_completion_init(void)
93{
94 pr_info("inside thread init function\n");
95 thread_start();
96 return 0;
97}
98
99/* thread_stop - destroys thread and stops execution */
100
101void thread_stop(void)
102{
103 kthread_stop(my_thread3);
104 kthread_stop(my_thread2);
105 kthread_stop(my_thread1);
106 pr_info("destroyed thread and completed task\n");
107}
108
109/* wait_for_completion_exit - calls thread_stop,
110 * executes when module is unloaded */
111
112static void __exit wait_for_completion_exit(void)
113{
114 pr_info("Inside kthread exit function\n");
115 thread_stop();
116}
117
118module_init(wait_for_completion_init);
119module_exit(wait_for_completion_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 wait_for_completion
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 wait_for_completion
8437 ? 00:00:00 wait_for_completion thread1 example
8438 ? 00:00:00 wait_for_completion thread2 example
8439 ? 00:00:00 wait_for_completion thread3 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
[ 4960.257737] inside thread init function
[ 4960.257819] successfully created kthread
[ 4960.257861] successfully created kthread
[ 4960.257866] my_thread_fn2 execution
[ 4960.257901] successfully create kthread
[ 4960.257904] my_thread_fn3 execution
[ 4962.286717] my_thread_fn3 execution
[ 4964.302783] my_thread_fn3 execution
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 wait_for_completion
test@test-V520-15IKL:~$
Check for kernel messages from exit function using dmesg command.
test@test-V520-15IKL:~$ sudo dmesg
[ 4969.495768] Inside kthread exit function
[ 4971.375104] my_thread_fn1 execution
[ 4972.399251] destroyed thread and completed task
In this example let’s see kthread waiting for completion using wait_for_completion_timeout.
Include the follow header files(.h) to refer the API being used for the execution.
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/jiffies.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 wait_for_completion_timeout");
Declare the thread variables which we are going to create and use in this example
static struct task_struct *my_thread1;
static struct task_struct *my_thread2;
struct completion thread_done;
Add the module init function which will be executed once we load the kernel module using insmod command.
static int __init wait_for_completion_timeout_init(void)
{
pr_info("Inside kthread init function\n");
thread_start();
return 0;
}
Add the thread start function called from the module init function which is used to create the thread and execute it.
void thread_start(void)
{
init_completion(&thread_done);
my_thread1 = kthread_run(my_thread_fn1,NULL,"wait_for_completion_timeout thread1 example");
if (IS_ERR(my_thread1))
pr_info("error creating in kthread\n");
else
pr_info("successfully created kthread\n");
my_thread2 = kthread_run(my_thread_fn2,NULL,"wait_for_completion_timeout thread2 example");
if (IS_ERR(my_thread2))
pr_info("error creating in kthread\n");
else
pr_info("successfully created kthread\n");
}
Add the module exit function which will be executed once we unload the kernel module using rmmod command.
static void __exit wait_for_completion_timeout_exit(void)
{
pr_info("Inside kthread exit function\n");
thread_stop();
}
Add the thread stop function called from the module exit function which is used to destroy the thread and stop its execution.
void thread_stop(void)
{
kthread_stop(my_thread2);
kthread_stop(my_thread1);
pr_info("destroyed thread and completed task\n");
}
Add the thread function API which will be called as soon as the kthread is created and is in running state.
static int my_thread_fn1(void *data)
{
while (!kthread_should_stop()) {
wait_for_completion_timeout(&thread_done,msecs_to_jiffies(100));
pr_info("my_thread_fn1 execution\n");
msleep(1000);
}
return 0;
}
static int my_thread_fn2(void *data)
{
while(!kthread_should_stop()) {
pr_info("my_thread_fn2 execution\n");
msleep(1000);
}
complete(&thread_done);
return 0;
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(wait_for_completion_timeout_init);
module_exit(wait_for_completion_timeout_exit);
1#include <linux/kernel.h>
2#include <linux/init.h>
3#include <linux/module.h>
4#include <linux/kthread.h>
5#include <linux/completion.h>
6#include <linux/delay.h>
7#include <linux/jiffies.h>
8
9MODULE_LICENSE("GPL");
10MODULE_DESCRIPTION("Example of wait_for_completion_timeout");
11MODULE_AUTHOR("linux usr");
12
13static struct task_struct *my_thread1;
14static struct task_struct *my_thread2;
15struct completion thread_done;
16
17/* my_thread_fn1 - executes when thread1 is created,
18 * waits for completion signal or until timer expires,
19 * prints a message and sleeps for 1000ms,
20 * stops when kthread_stop is called */
21
22static int my_thread_fn1(void *data)
23{
24 while (!kthread_should_stop()) {
25 wait_for_completion_timeout(&thread_done,msecs_to_jiffies(100));
26 pr_info("my_thread_fn1 execution\n");
27 msleep(1000);
28 }
29 return 0;
30}
31
32/* my_thread_fn2 - executes when thread2 is created,
33 * prints a message and sleeps for 1000ms,
34 * sends completion signal,
35 * stops when kthread_stop is called */
36
37static int my_thread_fn2(void *data)
38{
39 while(!kthread_should_stop()) {
40 pr_info("my_thread_fn2 execution\n");
41 msleep(1000);
42 }
43 complete(&thread_done);
44 return 0;
45}
46
47/* thread_start - creates thread and starts execution */
48
49void thread_start(void)
50{
51 init_completion(&thread_done);
52 my_thread1 = kthread_run(my_thread_fn1,NULL,"wait_for_completion_timeout thread1 example");
53 if (IS_ERR(my_thread1))
54 pr_info("error creating in kthread\n");
55 else
56 pr_info("successfully created kthread\n");
57 my_thread2 = kthread_run(my_thread_fn2,NULL,"wait_for_completion_timeout thread2 example");
58 if (IS_ERR(my_thread2))
59 pr_info("error creating in kthread\n");
60 else
61 pr_info("successfully created kthread\n");
62}
63
64/* wait_for_completion_timeout_init - calls thread_start,
65 * executes when module is loaded */
66
67static int __init wait_for_completion_timeout_init(void)
68{
69 pr_info("inside thread init function\n");
70 thread_start();
71 return 0;
72}
73
74/* thread_stop - destroys thread and stops execution */
75
76void thread_stop(void)
77{
78 kthread_stop(my_thread2);
79 kthread_stop(my_thread1);
80 pr_info("destroyed thread and completed task\n");
81}
82
83/* wait_for_completion_timeout_exit - calls thread_stop,
84 * executes when the module is unloaded */
85
86static void __exit wait_for_completion_timeout_exit(void)
87{
88 pr_info("inside kthread exit function\n");
89 thread_stop();
90}
91
92module_init(wait_for_completion_timeout_init);
93module_exit(wait_for_completion_timeout_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 wait_for_completion_timeout
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 wait_for_completion_timeout
4556 ? 00:00:00 wait_for_completion_timeout thread1 example
4557 ? 00:00:00 wait_for_completion_timeout thread2 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
[ 103.512409] inside thread init function
[ 103.512485] successfully created kthread
[ 103.512562] successfully created kthread
[ 103.512564] my_thread_fn2 execution
[ 103.615842] my_thread_fn1 execution
[ 104.536183] my_thread_fn2 execution
[ 104.736350] my_thread_fn1 execution
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 wait_for_completion_timeout
test@test-V520-15IKL:~$
Check for kernel messages from exit function using dmesg command.
test@test-V520-15IKL:~$ sudo dmesg
[ 113.015970] inside kthread exit function
[ 113.700021] my_thread_fn1 execution
[ 114.716557] destroyed thread and completed task
In this example let’s see how to send complete signal to all waiting threads.
Include the follow header files(.h) to refer the API being used for the execution.
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/completion.h>
#include <linux/delay.h>
#include <linux/jiffies.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 complete_all");
Declare the thread and completion variables which we are going to create and use in this example
static struct task_struct *my_thread1;
static struct task_struct *my_thread2;
static struct task_struct *my_thread3;
struct completion thread_done;
Add the module init function which will be executed once we load the kernel module using insmod command.
static int __init complete_all_init(void)
{
pr_info("Inside kthread init function\n");
thread_start();
return 0;
}
Add the thread start function called from module init function, which creates the thread, wakes it up and starts its execution.
void thread_start(void)
{
init_completion(&thread_done);
my_thread1 = kthread_run(my_thread_fn1,NULL,"complete_all thread1 example");
if (IS_ERR(my_thread1))
pr_info("error creating in kthread\n");
else
pr_info("successfully created kthread\n");
my_thread2 = kthread_run(my_thread_fn2,NULL,"complete_all thread2 example");
if (IS_ERR(my_thread2))
pr_info("error creating in kthread\n");
else
pr_info("successfully created kthread\n");
my_thread3 = kthread_run(my_thread_fn3,NULL,"complete_all thread3 example");
if (IS_ERR(my_thread3))
pr_info("error creating in kthread\n");
else
pr_info("successfully create kthread\n");
}
Add the module exit function which will be executed once we unload the kernel module using rmmod command.
static void __exit complete_all_exit(void)
{
pr_info("Inside kthread exit function\n");
thread_stop();
}
Add the thread stop function which is called from module exit function, which destroys the thread and stops the execution.
void thread_stop(void)
{
kthread_stop(my_thread2);
kthread_stop(my_thread1);
kthread_stop(my_thread3);
pr_info("destroyed thread and completed task\n");
}
Add the thread function API which will be called as soon as the kthread is created and is in running state.
static int my_thread_fn1(void *data)
{
while (!kthread_should_stop()) {
wait_for_completion(&thread_done);
pr_info("my_thread_fn1 execution\n");
msleep(1000);
}
return 0;
}
static int my_thread_fn2(void *data)
{
while(!kthread_should_stop()) {
pr_info("my_thread_fn2 execution\n");
msleep(1000);
}
complete_all(&thread_done);
return 0;
}
static int my_thread_fn3(void *data)
{
while (!kthread_should_stop()) {
wait_for_completion(&thread_done);
pr_info("my_thread_fn3 execution\n");
msleep(1000);
}
return 0;
}
Add the driver entry points which will be executed once the module is inserted or removed from the kernel.
module_init(complete_all_init);
module_exit(complete_all_exit);
1#include <linux/kernel.h>
2#include <linux/init.h>
3#include <linux/module.h>
4#include <linux/kthread.h>
5#include <linux/completion.h>
6#include <linux/delay.h>
7#include <linux/jiffies.h>
8
9MODULE_LICENSE("GPL");
10MODULE_DESCRIPTION("Example of complete_all");
11MODULE_AUTHOR("linux usr");
12
13static struct task_struct *my_thread1;
14static struct task_struct *my_thread2;
15static struct task_struct *my_thread3;
16struct completion thread_done;
17
18/* my_thread_fn1 - executes when thread1 is created,
19 * waits for completion signal from thread2,
20 * prints a message and sleeps for 1000ms,
21 * stops when thread_stop is called */
22
23static int my_thread_fn1(void *data)
24{
25 while (!kthread_should_stop()) {
26 wait_for_completion(&thread_done);
27 pr_info("my_thread_fn1 execution\n");
28 msleep(1000);
29 }
30 return 0;
31}
32
33/* my_thread_fn2 - executes when thread2 is created,
34 * prints a message and sleeps for 1000ms,
35 * sends completion signal for all waiting thread,
36 * stops when kthread_stop is called */
37
38static int my_thread_fn2(void *data)
39{
40 while(!kthread_should_stop()) {
41 pr_info("my_thread_fn2 execution\n");
42 msleep(1000);
43 }
44 complete_all(&thread_done);
45 return 0;
46}
47
48/* my_thread_fn3 - executes when thread3 is created,
49 * waits for completion signal from thread2,
50 * prints a message and sleep for 1000ms,
51 * exits when kthread_stop is called */
52
53static int my_thread_fn3(void *data)
54{
55 while (!kthread_should_stop()) {
56 wait_for_completion(&thread_done);
57 pr_info("my_thread_fn3 execution\n");
58 msleep(1000);
59 }
60 return 0;
61}
62
63/* thread_start - creates thread and starts execution */
64
65void thread_start(void)
66{
67 init_completion(&thread_done);
68 my_thread1 = kthread_run(my_thread_fn1,NULL,"complete_all thread1 example");
69 if (IS_ERR(my_thread1))
70 pr_info("error creating in kthread\n");
71 else
72 pr_info("successfully created kthread\n");
73 my_thread2 = kthread_run(my_thread_fn2,NULL,"complete_all thread2 example");
74 if (IS_ERR(my_thread2))
75 pr_info("error creating in kthread\n");
76 else
77 pr_info("successfully created kthread\n");
78 my_thread3 = kthread_run(my_thread_fn3,NULL,"complete_all thread3 example");
79 if (IS_ERR(my_thread3))
80 pr_info("error creating in kthread\n");
81 else
82 pr_info("successfully create kthread\n");
83}
84
85/* complete_all_init - calls thread_start,
86 * executes when module is loaded */
87
88static int __init complete_all_init(void)
89{
90 pr_info("inside thread init function\n");
91 thread_start();
92 return 0;
93}
94
95/* thread_stop - destroys thread and stops execution */
96
97void thread_stop(void)
98{
99 kthread_stop(my_thread2);
100 kthread_stop(my_thread1);
101 kthread_stop(my_thread3);
102 pr_info("destroyed thread and completed task\n");
103}
104
105/* complete_all_exit - calls thread_stop,
106 * executes when module is unloaded */
107
108static void __exit complete_all_exit(void)
109{
110 pr_info("inside kthread exit function\n");
111 thread_stop();
112}
113
114module_init(complete_all_init);
115module_exit(complete_all_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 complete_all
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 complete_all
7051 ? 00:00:00 complete_all thread1 example
7052 ? 00:00:00 complete_all thread2 example
7053 ? 00:00:00 complete_all thread3 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
[ 4066.974902] inside thread init function
[ 4066.974991] successfully created kthread
[ 4066.975081] successfully created kthread
[ 4066.975083] my_thread_fn2 execution
[ 4066.975167] successfully create kthread
[ 4067.994028] my_thread_fn2 execution
[ 4069.018196] my_thread_fn2 execution
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 complete_all
test@test-V520-15IKL:~$
Check for kernel messages from exit function using dmesg command.
test@test-V520-15IKL:~$ sudo dmesg
[ 4076.997671] inside kthread exit function
[ 4077.194549] my_thread_fn3 execution
[ 4077.194552] my_thread_fn1 execution
[ 4078.202468] my_thread_fn3 execution
[ 4079.226533] destroyed thread and completed task
kthread API |
Learning |
---|---|
kthread_run |
Create and wake a thread |
kthread_should_stop |
To determine when thread should exit |
kthread_stop |
Stop a thread created by kthread_create |
Completion API |
Learning |
---|---|
init_completion |
Initialize completion variable |
complete |
signals a signal thread waiting on this completion |
wait_for_completion |
waits for completion of task |
wait_for_completion_timeout |
waits for completion of task or waits until the timer expires |
complete_all |
signals all the thread waiting on this completion |
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 |