Wait for completion ==================== .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Version Info ` * :ref:`Learnings in this section ` * :ref:`Explanation of kthread APIs ` * :ref:`kthread_should_stop ` * :ref:`kthread_run ` * :ref:`kthread_stop ` * :ref:`Explanation of Completion variable APIs ` * :ref:`init_completion ` * :ref:`wait_for_completion ` * :ref:`wait_for_completion_timeout ` * :ref:`complete_all ` * :ref:`complete ` * :ref:`Explanation of miscellaneous APIs ` * :ref:`Module parameter APIs ` * :ref:`IS_ERR ` * :ref:`pr_info ` * :ref:`wake_up_process ` * :ref:`msleep ` * :ref:`Driver entry point APIs ` * :ref:`Example 1: Wait for completion signal using wait_for_completion ` * :ref:`List of headers ` * :ref:`Module Macros ` * :ref:`Initialize thread variables ` * :ref:`Module init function ` * :ref:`Thread start function ` * :ref:`Module exit function ` * :ref:`Thread stop function ` * :ref:`Thread function API ` * :ref:`Driver entry points ` * :ref:`See the full program below ` * :ref:`Makefile ` * :ref:`Compile and Load the module ` * :ref:`Example 2: Wait for completion signal using wait_for_completion_timeout ` * :ref:`List of headers ` * :ref:`Module Macros ` * :ref:`Initialize thread variables ` * :ref:`Module init function ` * :ref:`Thread start function ` * :ref:`Module exit function ` * :ref:`Thread stop function ` * :ref:`Thread function API ` * :ref:`Driver entry points ` * :ref:`See the full program below ` * :ref:`Makefile ` * :ref:`Compile and Load the module ` * :ref:`Example 3: Send completion signals to all waiting thread ` * :ref:`List of headers ` * :ref:`Module Macros ` * :ref:`Initialize thread variables ` * :ref:`Module init function ` * :ref:`Thread start function ` * :ref:`Module exit function ` * :ref:`Thread stop function ` * :ref:`Thread function API ` * :ref:`Driver entry points ` * :ref:`See the full program below ` * :ref:`Makefile ` * :ref:`Compile and Load the module ` * :ref:`Summary of Kthread APIs ` * :ref:`Summary of Completion APIs ` * :ref:`Summary of Miscellaneous APIs ` .. _p2_waitforcompletion_0: .. tab-set:: .. tab-item:: Version Info =============================== ======================================= # Version =============================== ======================================= Ubuntu Ubuntu 22.10 Kernel 6.7.9 =============================== ======================================= .. _p2_waitforcompletion_1: .. tab-set:: .. tab-item:: Learnings in this section * In this program, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to create completion variable ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to send completion signal for waiting thread? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to signal once the task is completed? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use Kthread APIs ? * `kthread_should_stop `_ * `kthread_run `_ * `kthread_stop `_ .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use Completion APIs ? * `init_completion `_ * `wait_for_completion `_ * `wait_for_completion_timeout `_ * `complete_all `_ * `complete `_ .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use below APIs ? * `MODULE_LICENSE `_ * `MODULE_DESCRIPTION `_ * `MODULE_AUTHOR `_ * `IS_ERR `_ * `pr_info `_ * `wake_up_process `_ * `msleep `_ * `module_init `_ * `module_exit `_ .. _p2_waitforcompletion_2: .. tab-set:: .. tab-item:: Explanation of Kthread APIs .. _p2_waitforcompletion_5: .. tab-set:: .. tab-item:: kthread_should_stop * Here is the function prototype of the API: `kthread_should_stop `_ .. code-block:: c #include 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, .. code-block:: c while (!kthread_should_stop()) { //add the instructions to be performed during thread execution. } .. _p2_waitforcompletion_6: .. tab-set:: .. tab-item:: kthread_run * Here is the function prototype of the API: `kthread_run `_ .. code-block:: c #include #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, .. code-block:: c kthread_run(mythread,NULL,"sample kthread"); .. _p2_waitforcompletion_7: .. tab-set:: .. tab-item:: kthread_stop * Here is the function prototype of the API: `kthread_stop `_ .. code-block:: c #include 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, .. code-block:: c kthread_stop(mythread); .. _p2_waitforcompletion_82: .. tab-set:: .. tab-item:: Explanation of Completion variable APIs .. _p2_waitforcompletion_84: .. tab-set:: .. tab-item:: init_completion * Here is the prototype of the API: `init_completion `_ .. code-block:: c #include 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 .. code-block:: c init_completion(mycompletionvar); .. _p2_waitforcompletion_89: .. tab-set:: .. tab-item:: wait_for_completion * Here is the prototype of the API: `wait_for_completion `_ .. code-block:: c #include 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, .. code-block:: c wait_for_completion(mycompletionvar); .. _p2_waitforcompletion_85: .. tab-set:: .. tab-item:: wait_for_completion_timeout * Here is the prototype of the API: `wait_for_completion_timeout `_ .. code-block:: c #include 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 .. code-block:: c wait_for_completion_timeout(mycompvar,msecs_to_jiffies(10)); .. _p2_waitforcompletion_86: .. tab-set:: .. tab-item:: complete_all * Here is the prototype of the API: `complete_all `_ .. code-block:: c #include 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 .. code-block:: c complete_all(mycompvar); .. _p2_waitforcompletion_88: .. tab-set:: .. tab-item:: complete * Here is the prototype of the API: `complete `_ .. code-block:: c #include void complete(struct completion *x); * where * `complete `_ : signals a single thread waiting on this completion * x: holds the state of this particular completion * Here is an example of how to use the API .. code-block:: c complete(mycompletevar); .. _p2_waitforcompletion_8: .. tab-set:: .. tab-item:: Explanation of miscellaneous APIs .. _p2_waitforcompletion_9: .. tab-set:: .. tab-item:: Module parameter APIs * Here is the prototype of module paramter APIs .. code-block:: c #include #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, .. code-block:: c MODULE_LICENSE("GPL"); MODULE_AUTHOR("Linux Usr"); MODULE_DESCRIPTION("Sample kernel module"); .. _p2_waitforcompletion_10: .. tab-set:: .. tab-item:: IS_ERR * Here is the prototype of the API: `IS_ERR `_ .. code-block:: c #include 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, .. code-block:: c 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 } .. _p2_waitforcompletion_11: .. tab-set:: .. tab-item:: pr_info * Here is the prototype of the API: `pr_info `_ .. code-block:: c #include #define pr_info(fmt, ...) \ printk(KERN_INFO pr_fmt(fmt), ##__VA_ARGS__) * where * `pr_info `_: Prints an info-level messages * fmt: format string * Here is an example of how to use the API, .. code-block:: c pr_info("//sample print statement"); .. _p2_waitforcompletion_12: .. tab-set:: .. tab-item:: wake_up_process * Here is the example of the API: `wake_up_process `_ .. code-block:: c #include 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, .. code-block:: c wake_up_process(mythread); .. _p2_waitforcompletion_13: .. tab-set:: .. tab-item:: msleep * Here is the example of the API: `msleep `_ .. code-block:: c #include void msleep(unsigned int msecs); * where * `msleep `_: will put it in sleep for a certain amount of msecs time. * msecs: time in milliseconds to sleep for * Here is the example of how to use the API, .. code-block:: c msleep(1000); .. _p2_waitforcompletion_14: .. tab-set:: .. tab-item:: Driver entry point API's * Here is the prototype of the Driver entry point API's .. code-block:: c #include #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 .. code-block:: c module_init(myinitmodule); module_exit(myexitmodule); .. _p2_waitforcompletion_15: .. tab-set:: .. tab-item:: Example 1: Wait for completion signal using wait_for_completion * In this example let's see how to wait for completion signal using wait_for_completion and use with kthread. .. _p2_waitforcompletion_16: .. tab-set:: .. tab-item:: List of headers * Include the follow header files(.h) to refer the API being used for the execution. .. code-block:: c #include #include #include #include #include #include #include .. _p2_waitforcompletion_17: .. tab-set:: .. tab-item:: Module macros * Add the following module macros to display information about the license, author and description about the module. .. code-block:: c MODULE_LICENSE("GPL"); MODULE_AUTHOR("Linux Usr"); MODULE_DESCRIPTION("Example of wait_for_completion"); .. _p2_waitforcompletion_18: .. tab-set:: .. tab-item:: Initialize thread variables * Declare the thread and completion variables which we are going to create and use in this example .. code-block:: c 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; .. _p2_waitforcompletion_19: .. tab-set:: .. tab-item:: Module init function * Add the module init function which will be executed once we load the kernel module using insmod command. .. code-block:: c static int __init wait_for_completion_init(void) { pr_info("Inside thread init function\n"); thread_start(); return 0; } .. _p2_waitforcompletion_20: .. tab-set:: .. tab-item:: Thread start function * Add the thread start function which is called from module init function, creates the thread and starts it's execution. .. code-block:: c 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"); } .. _p2_waitforcompletion_21: .. tab-set:: .. tab-item:: Module exit function * Add the module exit function which will be executed once we unload the kernel module using rmmod command. .. code-block:: c static void __exit wait_for_completion_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p2_waitforcompletion_22: .. tab-set:: .. tab-item:: Thread stop function * Add the thread stop function which is called from module exit function, destroys the thread created and stops it's execution. .. code-block:: c void thread_stop(void) { kthread_stop(my_thread3); kthread_stop(my_thread2); kthread_stop(my_thread1); pr_info("destroyed thread and completed task\n"); } .. _p2_waitforcompletion_23: .. tab-set:: .. tab-item:: Thread function API * Add the thread function API which will be called as soon as the kthread is created and is in running state. .. code-block:: c 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; } .. _p2_waitforcompletion_24: .. tab-set:: .. tab-item:: Driver entry points * Add the driver entry points which will be executed once the module is inserted or removed from the kernel. .. code-block:: c module_init(wait_for_completion_init); module_exit(wait_for_completion_exit); .. _p2_waitforcompletion_25: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p2_waitforcompletion/wait_for_completion/kthread.c :language: c :linenos: .. _p2_waitforcompletion_26: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p2_waitforcompletion/wait_for_completion/Makefile :language: c :linenos: .. _p2_waitforcompletion_27: .. tab-set:: .. tab-item:: Compile and load the module * Run make to compile the kernel source and generate the .ko image. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell test@test-V520-15IKL:~$ sudo insmod ./kthread.ko * check if the module is loaded or not using lsmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo lsmod | grep kthread kthread 16384 0 * check if the thread is created or not using ps command. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell test@test-V520-15IKL:~$ ps -N | grep wait_for_completion test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell 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 .. _p2_waitforcompletion_28: .. tab-set:: .. tab-item:: Example 2: Waiting for completion using wait_for_completion_timeout * In this example let's see kthread waiting for completion using wait_for_completion_timeout. .. _p2_waitforcompletion_29: .. tab-set:: .. tab-item:: List of headers * Include the follow header files(.h) to refer the API being used for the execution. .. code-block:: c #include #include #include #include #include #include #include .. _p2_waitforcompletion_30: .. tab-set:: .. tab-item:: Module macros * Add the following module macros to display information about the license, author and description about the module. .. code-block:: c MODULE_LICENSE("GPL"); MODULE_AUTHOR("Linux Usr"); MODULE_DESCRIPTION("example of wait_for_completion_timeout"); .. _p2_waitforcompletion_31: .. tab-set:: .. tab-item:: Initialize thread variables * Declare the thread variables which we are going to create and use in this example .. code-block:: c static struct task_struct *my_thread1; static struct task_struct *my_thread2; struct completion thread_done; .. _p2_waitforcompletion_32: .. tab-set:: .. tab-item:: Module init function * Add the module init function which will be executed once we load the kernel module using insmod command. .. code-block:: c static int __init wait_for_completion_timeout_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p2_waitforcompletion_33: .. tab-set:: .. tab-item:: Thread start function * Add the thread start function called from the module init function which is used to create the thread and execute it. .. code-block:: c 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"); } .. _p2_waitforcompletion_34: .. tab-set:: .. tab-item:: Module exit function * Add the module exit function which will be executed once we unload the kernel module using rmmod command. .. code-block:: c static void __exit wait_for_completion_timeout_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p2_waitforcompletion_35: .. tab-set:: .. tab-item:: Thread stop function * Add the thread stop function called from the module exit function which is used to destroy the thread and stop its execution. .. code-block:: c void thread_stop(void) { kthread_stop(my_thread2); kthread_stop(my_thread1); pr_info("destroyed thread and completed task\n"); } .. _p2_waitforcompletion_36: .. tab-set:: .. tab-item:: Thread function API * Add the thread function API which will be called as soon as the kthread is created and is in running state. .. code-block:: c 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; } .. _p2_waitforcompletion_37: .. tab-set:: .. tab-item:: Driver entry points * Add the driver entry points which will be executed once the module is inserted or removed from the kernel. .. code-block:: c module_init(wait_for_completion_timeout_init); module_exit(wait_for_completion_timeout_exit); .. _p2_waitforcompletion_38: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p2_waitforcompletion/wait_for_completion_timeout/kthread.c :language: c :linenos: .. _p2_waitforcompletion_39: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p2_waitforcompletion/wait_for_completion_timeout/Makefile :language: c :linenos: .. _p2_waitforcompletion_40: .. tab-set:: .. tab-item:: Compile and load the module * Run make to compile the kernel source and generate the .ko image. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell test@test-V520-15IKL:~$ sudo insmod ./kthread.ko * check if the module is loaded or not using lsmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo lsmod | grep kthread kthread 16384 0 * check if the thread is created or not using ps command. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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 .. _p2_waitforcompletion_41: .. tab-set:: .. tab-item:: Example 3: Send complete signal to all waiting threads * In this example let's see how to send complete signal to all waiting threads. .. _p2_waitforcompletion_42: .. tab-set:: .. tab-item:: List of headers * Include the follow header files(.h) to refer the API being used for the execution. .. code-block:: c #include #include #include #include #include #include #include .. _p2_waitforcompletion_43: .. tab-set:: .. tab-item:: Module macros * Add the following module macros to display information about the license, author and description about the module. .. code-block:: c MODULE_LICENSE("GPL"); MODULE_AUTHOR("Linux Usr"); MODULE_DESCRIPTION("example of complete_all"); .. _p2_waitforcompletion_44: .. tab-set:: .. tab-item:: Initialize thread variables * Declare the thread and completion variables which we are going to create and use in this example .. code-block:: c static struct task_struct *my_thread1; static struct task_struct *my_thread2; static struct task_struct *my_thread3; struct completion thread_done; .. _p2_waitforcompletion_45: .. tab-set:: .. tab-item:: Module init function * Add the module init function which will be executed once we load the kernel module using insmod command. .. code-block:: c static int __init complete_all_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p2_waitforcompletion_46: .. tab-set:: .. tab-item:: thread start function * Add the thread start function called from module init function, which creates the thread, wakes it up and starts its execution. .. code-block:: c 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"); } .. _p2_waitforcompletion_47: .. tab-set:: .. tab-item:: Module exit function * Add the module exit function which will be executed once we unload the kernel module using rmmod command. .. code-block:: c static void __exit complete_all_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p2_waitforcompletion_48: .. tab-set:: .. tab-item:: thread stop function * Add the thread stop function which is called from module exit function, which destroys the thread and stops the execution. .. code-block:: c void thread_stop(void) { kthread_stop(my_thread2); kthread_stop(my_thread1); kthread_stop(my_thread3); pr_info("destroyed thread and completed task\n"); } .. _p2_waitforcompletion_49: .. tab-set:: .. tab-item:: Thread function API * Add the thread function API which will be called as soon as the kthread is created and is in running state. .. code-block:: c 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; } .. _p2_waitforcompletion_50: .. tab-set:: .. tab-item:: Driver entry points * Add the driver entry points which will be executed once the module is inserted or removed from the kernel. .. code-block:: c module_init(complete_all_init); module_exit(complete_all_exit); .. _p2_waitforcompletion_51: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p2_waitforcompletion/complete_all/kthread.c :language: c :linenos: .. _p2_waitforcompletion_52: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p2_waitforcompletion/complete_all/Makefile :language: c :linenos: .. _p2_waitforcompletion_53: .. tab-set:: .. tab-item:: Compile and load the module * Run make to compile the kernel source and generate the .ko image. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell test@test-V520-15IKL:~$ sudo insmod ./kthread.ko * check if the module is loaded or not using lsmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo lsmod | grep kthread kthread 16384 0 * check if the thread is created or not using ps command. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell 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. .. code-block:: shell test@test-V520-15IKL:~$ ps -N | grep complete_all test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell 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 .. _p2_waitforcompletion_80: .. tab-set:: .. tab-item:: Summary of Kthread APIs =============================== ======================================= 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 =============================== ======================================= .. _p2_waitforcompletion_90: .. tab-set:: .. tab-item:: Summary of Completion APIs =============================== ================================================================================== 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 =============================== ================================================================================== .. _p2_waitforcompletion_81: .. tab-set:: .. tab-item:: Summary of miscellaneous APIs =============================== =========================================================================================== 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 =============================== ===========================================================================================