Creating Swait ================= .. 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 Swait APIs ` * :ref:`DECLARE_SWAIT_QUEUE_HEAD ` * :ref:`init_swait_queue_head ` * :ref:`DECLARE_SWAIT_QUEUE_HEAD_ONSTACK ` * :ref:`swait_event_exclusive ` * :ref:`swake_up_one ` * :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 swaitqueue head using DECLARE_SWAIT_QUEUE_HEAD ` * :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 swaitqueue head using init_swait_queue_head ` * :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 swaitqueue head using DECLARE_SWAIT_QUEUE_HEAD_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 Workqueue APIs ` * :ref:`Summary of Miscellaneous APIs ` .. _p1_createswait_0: .. tab-set:: .. tab-item:: Version Info =============================== ======================================= # Version =============================== ======================================= Ubuntu Ubuntu 22.10 Kernel 6.7.9 =============================== ======================================= .. _p1_createswait_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 swaitqueue? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * What are the different ways to create swaitqueue? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to wakeup the waiting threads 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 Swaitqueue APIs ? * `DECLARE_SWAIT_QUEUE_HEAD `_ * `init_swait_queue_head `_ * `DECLARE_SWAIT_QUEUE_HEAD_ONSTACK `_ * `swait_event_exclusive `_ * `swake_up_one `_ .. 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_createswait_2: .. tab-set:: .. tab-item:: Explanation of Kthread APIs .. _p1_createswait_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_createswait_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_createswait_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_createswait_82: .. tab-set:: .. tab-item:: Explanation of Swait APIs .. _p1_createswait_83: .. tab-set:: .. tab-item:: DECLARE_SWAIT_QUEUE_HEAD * Here is the prototype of the API: `DECLARE_SWAIT_QUEUE_HEAD `_ .. code-block:: c # include #define DECLARE_SWAIT_QUEUE_HEAD(name) \ struct swait_queue_head name = __SWAIT_QUEUE_HEAD_INITIALIZER(name) * where * `DECLARE_SWAIT_QUEUE_HEAD `_ : To declare and initialize swaitqueue head. * name : name which is given to the wait queue head on declaration. * Here is the example of the API, .. code-block:: c DECLARE_SWAIT_QUEUE_HEAD(myswaitqueue); .. _p1_createswait_84: .. tab-set:: .. tab-item:: init_swait_queue_head * Here is the prototype of the API: `init_swait_queue_head `_ .. code-block:: c # include #define init_swait_queue_head(q) * where * `init_swait_queue_head `_ : initialize swaitqueue head. * q : name of the swaitqueue head which is initialized * Here is the example of how to use the API, .. code-block:: c init_swait_queue_head(myswaitqueue); .. _p1_createswait_85: .. tab-set:: .. tab-item:: DECLARE_SWAIT_QUEUE_HEAD_ONSTACK * Here is the prototype of the API: `DECLARE_SWAIT_QUEUE_HEAD_ONSTACK `_ .. code-block:: c # include # define DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(name) * where * `DECLARE_SWAIT_QUEUE_HEAD_ONSTACK `_ : Declare wait queue head on kernel stack. * name : wait queue head name which needs to be declared. * Here is the example of how to use the API .. code-block:: c DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(myswaitqueue); .. _p1_createswait_86: .. tab-set:: .. tab-item:: swait_event_exclusive * Here is the prototype of the API : `swait_event_exclusive `_ .. code-block:: c #include #define swait_event_exclusive(wq_head, condition) * where * `swait_event_exclusive `_ : sleeps until the condition is true * wq_head : the waitqueue to wait on * condition : a C expression for the event to wait for * Here is an example of how to use the API, .. code-block :: c swait_event_exclusive(mywaitqueue,flag != 0); .. _p1_createswait_87: .. tab-set:: .. tab-item:: swake_up_one * Here is the prototype of the API: `swake_up_one `_ .. code-block:: c #include void swake_up_one(struct swait_queue_head *q); * where * `swake_up_one `_ : wakes up the waitqueue head which is in waiting state. * q : waitqueue which is in waiting state. * Here is the example of how to use the API .. code-block:: c swake_up_one(myswaitqueue); .. _p1_createswait_8: .. tab-set:: .. tab-item:: Explanation of miscellaneous APIs .. _p1_createswait_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_createswait_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_createswait_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_createswait_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_createswait_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_createswait_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_createswait_15: .. tab-set:: .. tab-item:: Example 1: Create swaitqueue using DECLARE_SWAIT_QUEUE_HEAD * In this example let's see how to create swaitqueue and use with kthread. .. _p1_createswait_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_createswait_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_SWAIT_QUEUE_HEAD"); .. _p1_createswait_18: .. tab-set:: .. tab-item:: Initialize thread variables * Declare the thread and waitqueue variables which we are going to create and use in this example .. code-block:: c static struct task_struct *wait_thread; static struct task_struct *wake_thread; DECLARE_SWAIT_QUEUE_HEAD(wqueue); int i = 0; .. _p1_createswait_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 kthread_swait_init(void) { pr_info("Inside thread init function\n"); thread_start(); return 0; } .. _p1_createswait_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) { wait_thread = kthread_run(wait_thread_fn,NULL,"kthread_swait wait_thread example"); if (IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); wake_up_thread = kthread_run(wake_up_thread_fn,NULL,"kthread_swait wake_thread example"); if(IS_ERR(wake_up_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p1_createswait_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 kthread_swait_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p1_createswait_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(wake_thread); kthread_stop(wait_thread); pr_info("destroyed threads\n"); } .. _p1_createswait_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 wait_thread_fn(void *data) { while (!kthread_should_stop()) { swait_event_exclusive(swait_queue,i > 3); pr_info("wait thread execution i = %d\n",i); msleep(1000); } return 0; } static int wake_thread_fn(void *data) { while(!kthread_should_stop()) { pr_info("wake thread execution i = %d\n",i); i++; swake_up_one(&swait_queue); msleep(1000); } return 0; } .. _p1_createswait_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(kthread_swait_init); module_exit(kthread_swait_exit); .. _p1_createswait_25: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p1_createswait/declare_swait_queue_head/kthread.c :language: c :linenos: .. _p1_createswait_26: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p1_createswait/declare_swait_queue_head/Makefile :language: c :linenos: .. _p1_createswait_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_swait_queue_head 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 kthread_swait 6373 ? 00:00:00 kthread_swait wait_thread example 6374 ? 00:00:00 kthread_swait wake_thread 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 [ 770.083248] inside kthread init function [ 770.083320] successfully created wait thread [ 770.083371] successfully created wake thread [ 770.083377] wake thread execution i = 0 [ 771.090390] wake thread execution i = 1 [ 772.115023] wake thread execution i = 2 [ 773.139696] wake thread execution i = 3 [ 773.139723] wait thread execution i = 4 [ 774.164227] wait thread execution i = 4 [ 774.164244] wake thread execution i = 4 [ 775.188857] wait thread execution i = 5 * 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 kthread_swait test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 781.097648] inside kthread exit function [ 781.315947] destroyed threads .. _p1_createswait_28: .. tab-set:: .. tab-item:: Example 2: Create swaitqueue head using init_swait_queue_head * In this example let's see how to create swaitqueue head with init_swait_queue_head and use kthread with it. .. _p1_createswait_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_createswait_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_swait_queue_head"); .. _p1_createswait_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 *wait_thread; static struct task_struct *wake_up_thread; struct swait_queue_head swait_queue; int wait_flag = 0; .. _p1_createswait_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 kthread_swait_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p1_createswait_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_swait_queue_head(&swait_queue); pr_info("initialized workqueue head\n"); wait_thread = kthread_run(wait_thread_fn,NULL,"kthread_swait wait_thread example"); if (IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); wake_thread = kthread_run(wake_up_thread_fn,NULL,"kthread_swait wake_thread example"); if(IS_ERR(wake_up_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p1_createswait_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 kthread_swait_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p1_createswait_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(wake_thread); kthread_stop(wait_thread); pr_info("destroyed threads\n"); } .. _p1_createswait_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 wait_thread_fn(void *data) { while (!kthread_should_stop()) { swait_event_exclusive(swait_queue,i > 3); pr_info("wait thread execution i = %d\n",i); msleep(1000); } return 0; } static int wake_thread_fn(void *data) { while(!kthread_should_stop()) { pr_info("wake thread execution i = %d\n",i); i++; swake_up_one(&swait_queue); msleep(1000); } return 1; } .. _p1_createswait_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(kthread_swait_init); module_exit(kthread_swait_exit); .. _p1_createswait_38: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p1_createswait/init_swait_queue_head/kthread.c :language: c :linenos: .. _p1_createswait_39: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p1_createswait/init_swait_queue_head/Makefile :language: c :linenos: .. _p1_createswait_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_swait_queue_head 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 kthread_swait 7978 ? 00:00:00 kthread_swait wait_thread example 7979 ? 00:00:00 kthread_swait wake_thread 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 [ 2142.952854] inside kthread init function [ 2142.952990] successfully created wait thread [ 2142.953054] successfully created wake thread [ 2142.953060] wake thread execution i = 0 [ 2143.979965] wake thread execution i = 1 [ 2145.003735] wake thread execution i = 2 [ 2146.027725] wake thread execution i = 3 [ 2146.027844] wait thread execution i = 4 [ 2147.051539] wake thread execution i = 4 [ 2147.051553] wait thread execution i = 5 * 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 kthread_swait test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 2156.374391] inside kthread exit function [ 2158.314468] destroyed threads .. _p1_createswait_41: .. tab-set:: .. tab-item:: Example 3: Create waitqueue using DECLARE_SWAIT_QUEUE_HEAD_ONSTACK * In this example let's see how to create swaitqueue using DECLARE_SWAIT_QUEUE_HEAD_ONSTACK and use kthread with it. .. _p1_createswait_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_createswait_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 declare_swait_queue_head_onstack"); .. _p1_createswait_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 *wait_thread; static struct task_struct *wake_thread; DECLARE_SWAIT_QUEUE_HEAD_ONSTACK(swait_queue); int i = 0; .. _p1_createswait_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 kthread_swait_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p1_createswait_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) { wait_thread = kthread_run(wait_thread_fn,NULL,"kthread_swait wait_thread example"); if (IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); wake_up_thread = kthread_run(wake_up_thread_fn,NULL,"kthread_swait wake_thread example"); if(IS_ERR(wake_up_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p1_createswait_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 kthread_swait_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p1_createswait_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(wake_thread); kthread_stop(wait_thread); pr_info("destroyed threads\n"); } .. _p1_createswait_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 wait_thread_fn(void *data) { while (!kthread_should_stop()) { swait_event_exclusive(swait_queue,i > 3); pr_info("wait thread execution i = %d\n",i); msleep(1000); } return 0; } static int wake_thread_fn(void *data) { while(!kthread_should_stop()) { pr_info("wake thread execution i = %d\n",i); i++; swake_up_one(&swait_queue); msleep(1000); } return 1; } .. _p1_createswait_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_swait_init); module_exit(kthread_swait_exit); .. _p1_createswait_51: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p1_createswait/declare_swait_queue_head_onstack/kthread.c :language: c :linenos: .. _p1_createswait_52: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p1_createswait/declare_swait_queue_head_onstack/Makefile :language: c :linenos: .. _p1_createswait_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 declare_swait_queue_head_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 kthread_swait 8518 ? 00:00:00 kthread_swait wait_thread example 8519 ? 00:00:00 kthread_swait wake_thread 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 [ 2969.952183] inside kthread init function [ 2969.952345] successfully created wait thread [ 2969.952482] successfully created wake thread [ 2969.952540] wake thread execution i = 0 [ 2970.986614] wake thread execution i = 1 [ 2972.010535] wake thread execution i = 2 [ 2973.034516] wake thread execution i = 3 [ 2973.034627] wait thread execution i = 4 [ 2974.058501] wake thread execution i = 4 [ 2974.058504] wait thread execution i = 4 [ 2975.082471] wake thread execution i = 5 [ 2975.082471] wait thread execution i = 5 * 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 kthread_swait test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 2978.622086] inside kthread exit function [ 2980.202260] destroyed threads .. _p1_createswait_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_createswait_90: .. tab-set:: .. tab-item:: Summary of swaitqueue APIs ================================= ================================================================================== Workqueue API Learning ================================= ================================================================================== DECLARE_SWAIT_QUEUE_HEAD Declare swaitqueue head init_swait_queue_head Initialize swaitqueue head DECLARE_SWAIT_QUEUEU_HEAD_ONSTACK Declare swaitqueue head on current stack swait_event_exclusive sleep until a condition gets true swake_up_one wakes up the waiting queue ================================= ================================================================================== .. _p1_createswait_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 =============================== ===========================================================================================