Creating completion variable =============================== .. 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:`DECLARE_COMPLETION ` * :ref:`init_completion ` * :ref:`COMPLETION_INITIALIZER ` * :ref:`COMPLETION_INITIALIZER_ONSTACK ` * :ref:`DECLARE_COMPLETION_ONSTACK ` * :ref:`complete ` * :ref:`wait_for_completion ` * :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: Create completion variable with DECLARE_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: Create completion variable using init_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 3: Create completion variable using COMPLETION_INITIALIZER ` * :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 4: Create completion variable using COMPLETION_INITIALIZER_ONSTACK ` * :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 5: Create completion variable using DECLARE_COMPLETION_ONSTACK ` * :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 ` .. _p1_createcompvar_0: .. tab-set:: .. tab-item:: Version Info =============================== ======================================= # Version =============================== ======================================= Ubuntu Ubuntu 22.10 Kernel 6.7.9 =============================== ======================================= .. _p1_createcompvar_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 * What are the different ways to create completion variable? .. 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 ? * `DECLARE_COMPLETION `_ * `init_completion `_ * `COMPLETION_INITIALIZER `_ * `COMPLETION_INITIALIZER_ONSTACK `_ * `DECLARE_COMPLETION_ONSTACK `_ * `complete `_ * `wait_for_completion `_ .. 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 `_ .. _p1_createcompvar_2: .. tab-set:: .. tab-item:: Explanation of Kthread APIs .. _p1_createcompvar_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. } .. _p1_createcompvar_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"); .. _p1_createcompvar_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); .. _p1_createcompvar_82: .. tab-set:: .. tab-item:: Explanation of Completion variable APIs .. _p1_createcompvar_83: .. tab-set:: .. tab-item:: DECLARE_COMPLETION * Here is the function prototype of the API: `DECLARE_COMPLETION `_ .. code-block:: c #include #define DECLARE_COMPLETION(work) \ struct completion work = COMPLETION_INITIALIZER(work) * where * `DECLARE_COMPLETION `_ : declare and initialize a completion structure * work: identifier for completion structure * Here is the example of how to use the API .. code-block:: c static DECLARE_COMPLETION(mycompletionvar); .. _p1_createcompvar_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); .. _p1_createcompvar_85: .. tab-set:: .. tab-item:: COMPLETION_INITIALIZER * Here is the prototype of the API: `COMPLETION_INITIALIZER `_ .. code-block:: c #include #define COMPLETION_INITIALIZER(work) \ { 0, __SWAIT_QUEUE_HEAD_INITIALIZER((work).wait) } * where * `COMPLETION_INITIALIZER `_ : initializes a completion structure * work : identifier for completion structure * Here is the example of how to use the API .. code-block:: c COMPLETION_INITIALIZER(mycompletionvar); .. _p1_createcompvar_86: .. tab-set:: .. tab-item:: COMPLETION_INITIALIZER_ONSTACK * Here is the prototype of the API: `COMPLETION_INITIALIZER_ONSTACK `_ .. code-block:: c #include #define COMPLETION_INITIALIZER_ONSTACK(work) \ (*({ init_completion(&work); &work; })) * where * `COMPLETION_INITIALIZER_ONSTACK `_ : nitializes a completion structure on the kernel stack. * work: identifier for the completion structure * Here is the example of how to use the API .. code-block:: c COMPLETION_INITIALIZER_ONSTACK(mycompletionvar); .. _p1_createcompvar_87: .. tab-set:: .. tab-item:: DECLARE_COMPLETION_ONSTACK * Here is the prototype of the API: `DECLARE_COMPLETION_ONSTACK `_ .. code-block:: c #include # define DECLARE_COMPLETION_ONSTACK(work) \ struct completion work = COMPLETION_INITIALIZER_ONSTACK(work) * where * `DECLARE_COMPLETION_ONSTACK `_ : This macro declares and initializes a completion structure on the kernel stack. * work: identifier for the completion structure * Here is the example of how to use the API .. code-block:: c DECLARE_COMPLETION_ONSTACK(mycompletionvar); .. _p1_createcompvar_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); .. _p1_createcompvar_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); .. _p1_createcompvar_8: .. tab-set:: .. tab-item:: Explanation of miscellaneous APIs .. _p1_createcompvar_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"); .. _p1_createcompvar_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 } .. _p1_createcompvar_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"); .. _p1_createcompvar_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); .. _p1_createcompvar_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); .. _p1_createcompvar_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); .. _p1_createcompvar_15: .. tab-set:: .. tab-item:: Example 1: Create completion variable with DECLARE_COMPLETION * In this example let's see how to create completion variable and use with kthread. .. _p1_createcompvar_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 .. _p1_createcompvar_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 DECLARE_COMPLETION"); .. _p1_createcompvar_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 DECLARE_COMPLETION(thread_done); .. _p1_createcompvar_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 declare_completion_init(void) { pr_info("Inside thread init function\n"); thread_start(); return 0; } .. _p1_createcompvar_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) { my_thread1 = kthread_run(my_thread_fn1,NULL,"DECLARE_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,"DECLARE_COMPLETION thread2 example"); if (IS_ERR(my_thread2)) pr_info("error creating in kthread\n"); else pr_info("successfully created kthread\n"); } .. _p1_createcompvar_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 declare_completion_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p1_createcompvar_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_thread2); kthread_stop(my_thread1); pr_info("destroyed thread and completed task\n"); } .. _p1_createcompvar_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()) { pr_info("my_thread_fn1 execution\n"); wait_for_completion(&thread_done); 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; } .. _p1_createcompvar_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(declare_completion_init); module_exit(declare_completion_exit); .. _p1_createcompvar_25: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p1_createcompvar/declare_completion/kthread.c :language: c :linenos: .. _p1_createcompvar_26: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p1_createcompvar/declare_completion/Makefile :language: c :linenos: .. _p1_createcompvar_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 DECLARE_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 DECLARE_COMPLETION 11293 ? 00:00:00 DECLARE_COMPLETION thread1 example 11294 ? 00:00:00 DECLARE_COMPLETION 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 [ 6013.312821] inside thread init function [ 6013.312847] successfully created kthread [ 6013.312849] my_thread_fn1 execution [ 6013.312875] successfully created kthread [ 6013.312876] my_thread_fn2 execution [ 6014.337095] my_thread_fn2 execution [ 6015.361109] 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 DECLARE_COMPLETION test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 6022.497201] destroyed thread and completed task .. _p1_createcompvar_28: .. tab-set:: .. tab-item:: Example 2: Create completion variable using init_completion * In this example let's see how to create completion variable with init_completion and use kthread with it. .. _p1_createcompvar_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 .. _p1_createcompvar_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 init_completion"); .. _p1_createcompvar_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; static struct completion thread_done; .. _p1_createcompvar_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 init_completion_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p1_createcompvar_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); pr_info("completion variable initialized\n"); my_thread1 = kthread_run(my_thread_fn1,NULL,"init_completion thread 1 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,"init_completion thread2 example"); if (IS_ERR(my_thread2)) pr_info("error creating in kthread\n"); else pr_info("successfully created kthread\n"); } .. _p1_createcompvar_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 init_completion_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p1_createcompvar_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_thread1); kthread_stop(my_thread2); pr_info("destroyed thread and completed task\n"); } .. _p1_createcompvar_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()) { pr_info("my_thread_fn1 execution\n"); msleep(1000); } complete(&thread_done); return 0; } static int my_thread_fn2(void *data) { while(!kthread_should_stop()) { pr_info("my_thread_fn2 execution\n"); wait_for_completion(&thread_done); msleep(1000); } return 0; } .. _p1_createcompvar_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(init_completion_init); module_exit(init_completion_exit); .. _p1_createcompvar_38: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p1_createcompvar/init_completion/kthread.c :language: c :linenos: .. _p1_createcompvar_39: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p1_createcompvar/init_completion/Makefile :language: c :linenos: .. _p1_createcompvar_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 init_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 init_completion 19813 ? 00:00:00 init_completion thread 1 example 19814 ? 00:00:00 init_completion 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 [15868.565622] inside thread init function [15868.565627] completion variable initialized [15868.565754] successfully created kthread [15868.565760] my_thread_fn1 execution [15868.565868] successfully created kthread [15868.565878] my_thread_fn2 execution [15869.588653] my_thread_fn1 execution [15870.612639] 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 init_completion test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [15873.684426] destroyed thread and completed task .. _p1_createcompvar_41: .. tab-set:: .. tab-item:: Example 3: Create completion variable using COMPLETION_INITIALIZER * In this example let's see how to create completion variable using completion_initializer and use kthread with it. .. _p1_createcompvar_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 .. _p1_createcompvar_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 completion initializer"); .. _p1_createcompvar_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 completion thread_done = COMPLETION_INITIALIZER(thread_done); .. _p1_createcompvar_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 completion_initializer_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p1_createcompvar_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:: void thread_start(void) { my_thread1 = kthread_run(my_thread_fn1,NULL,"completion_initializer 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,"completion_initializer thread2 example"); if (IS_ERR(my_thread2)) pr_info("error creating in kthread\n"); else pr_info("successfully created kthread\n"); } .. _p1_createcompvar_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 completion_initializer_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p1_createcompvar_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_thread1); kthread_stop(my_thread2); pr_info("destroyed thread and completed task\n"); } .. _p1_createcompvar_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()) { pr_info("my_thread_fn1 execution\n"); msleep(1000); } complete(&thread_done); return 0; } static int my_thread_fn2(void *data) { while(!kthread_should_stop()) { wait_for_completion(&thread_done); pr_info("my_thread_fn2 execution\n"); msleep(1000); } return 0; } .. _p1_createcompvar_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(kthread_completion_initializer_init); module_exit(kthread_completion_initializer_exit); .. _p1_createcompvar_51: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p1_createcompvar/completion_initializer/kthread.c :language: c :linenos: .. _p1_createcompvar_52: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p1_createcompvar/completion_initializer/Makefile :language: c :linenos: .. _p1_createcompvar_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 completion_initializer 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 completion_initializer 22765 ? 00:00:00 completion_initializer thread1 example 22766 ? 00:00:00 completion_initializer 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 [23035.070258] inside thread init function [23035.070335] successfully created kthread [23035.070341] my_thread_fn1 execution [23035.070382] successfully created kthread [23036.084091] my_thread_fn1 execution [23037.108194] my_thread_fn1 execution [23038.132183] my_thread_fn1 execution [23039.156253] 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 completion_initializer test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [23047.946466] inside kthread exit function [23048.372744] my_thread_fn2 execution [23049.396770] destroyed thread and completed task .. _p1_createcompvar_54: .. tab-set:: .. tab-item:: Example 4: Create completion variable using COMPLETION_INITIALIZER_ONSTACK * In this example let's see how to create completion variable using COMPLETION_INITIALIZER_ONSTACK and use kthreads with it. .. _p1_createcompvar_55: .. 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 .. _p1_createcompvar_56: .. 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 completion_initializer_onstack"); .. _p1_createcompvar_57: .. 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 struct my_completion { struct completion thread_done; }; static struct task_struct *my_thread1; static struct task_struct *my_thread2; struct my_completion completion_var; .. _p1_createcompvar_58: .. 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 completion_initializer_onstack_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p1_createcompvar_59: .. 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:: void thread_start(void) { completion_var.thread_done = COMPLETION_INITIALIZER_ONSTACK(completion_var.thread_done); my_thread1 = kthread_run(my_thread_fn1,NULL,"completion_initializer_onstack 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,"completion_initializer_onstack thread2 example"); if (IS_ERR(my_thread2)) pr_info("error creating in kthread\n"); else pr_info("successfully created kthread\n"); } .. _p1_createcompvar_60: .. 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 completion_initializer_onstack_exit(void) { pr_info("inside kthread exit function\n"); thread_stop(); } .. _p1_createcompvar_61: .. 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_thread1); kthread_stop(my_thread2); pr_info("destroyed thread and completed task\n"); } .. _p1_createcompvar_62: .. 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()) { pr_info("my_thread_fn1 execution\n"); msleep(1000); } complete(&completion_var.thread_done); return 0; } static int my_thread_fn2(void *data) { while(!kthread_should_stop()) { wait_for_completion(&completion_var.thread_done); pr_info("my_thread_fn2 execution\n"); msleep(1000); } return 0; } .. _p1_createcompvar_63: .. 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(completion_initializer_onstack_init); module_exit(completion_initializer_onstack_exit); .. _p1_createcompvar_64: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p1_createcompvar/completion_initializer_onstack/kthread.c :language: c :linenos: .. _p1_createcompvar_65: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p1_createcompvar/completion_initializer_onstack/Makefile :language: c :linenos: .. _p1_createcompvar_66: .. 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 completion_initializer_onstack 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 completion_initializer_onstack 4967 ? 00:00:00 completion_initializer_onstack thread1 example 4968 ? 00:00:00 completion_initializer_onstack 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 [ 2083.334632] inside thread init function [ 2083.334752] successfully created kthread [ 2083.334756] my_thread_fn1 execution [ 2083.334916] successfully created kthread [ 2084.345928] my_thread_fn1 execution [ 2085.369973] my_thread_fn1 execution [ 2086.398003] 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 completion_initializer_onstack test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 2094.780840] inside kthread exit function [ 2095.610379] my_thread_fn2 execution [ 2096.634403] destroyed thread and completed task .. _p1_createcompvar_67: .. tab-set:: .. tab-item:: Example 5: Create completion variable using DECLARE_COMPLETION_ONSTACK * In this example let's see how to create completion variable using DECLARE_COMPLETION_ONSTACK and use it with kthread. .. _p1_createcompvar_68: .. 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 .. _p1_createcompvar_69: .. 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 declare_completion_onstack"); .. _p1_createcompvar_70: .. 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; DECLARE_COMPLETION_ONSTACK(thread_done); .. _p1_createcompvar_71: .. 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 declare_completion_onstack_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p1_createcompvar_72: .. 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:: void thread_start(void) { my_thread1 = kthread_run(my_thread_fn1,NULL,"declare_completion_onstack 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,"declare_completion_onstack thread2 example"); if (IS_ERR(my_thread2)) pr_info("error creating in kthread\n"); else pr_info("successfully created kthread\n"); } .. _p1_createcompvar_73: .. 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 declare_completion_onstack_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p1_createcompvar_74: .. 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); pr_info("destroyed thread and completed task\n"); } .. _p1_createcompvar_75: .. 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(&thread_done); return 0; } .. _p1_createcompvar_76: .. 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(declare_completion_onstack_init); module_exit(declare_completion_onstack_exit); .. _p1_createcompvar_77: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p1_createcompvar/declare_completion_onstack/kthread.c :language: c :linenos: .. _p1_createcompvar_78: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p1_createcompvar/declare_completion_onstack/Makefile :language: c :linenos: .. _p1_createcompvar_79: .. 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 declare_completion_onstack 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 declare_completion_onstack 5863 ? 00:00:00 declare_completion_onstack thread1 example 5864 ? 00:00:00 declare_completion_onstack 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 [ 4329.862271] inside thread init function [ 4329.862331] successfully created kthread [ 4329.862344] successfully created kthread [ 4329.862345] my_thread_fn2 execution [ 4330.886693] my_thread_fn2 execution [ 4331.910730] 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 declare_completion_onstack test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 4340.770038] inside kthread exit function [ 4341.127057] my_thread_fn1 execution [ 4342.151095] destroyed thread and completed task .. _p1_createcompvar_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 =============================== ======================================= .. _p1_createcompvar_90: .. tab-set:: .. tab-item:: Summary of Completion APIs =============================== ================================================================================== Completion API Learning =============================== ================================================================================== DECLARE_COMPLETION Declare completion variable init_completion Initialize completion variable COMPLETION_INITIALIZER Initializes a completion structure COMPLETION_INITIALIZER_ONSTACK Initializes a completion structure on the kernel stack. DECLARE_COMPLETION_ONSTACK This macro declares and initializes a completion structure on the kernel stack. complete signals a signal thread waiting on this completion wait_for_completion waits for completion of task =============================== ================================================================================== .. _p1_createcompvar_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 =============================== ===========================================================================================