Interruptible Wait Event APIs ============================== .. 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 Waitqueue APIs ` * :ref:`DECLARE_WAIT_QUEUE_HEAD ` * :ref:`wait_event_interruptible ` * :ref:`wait_event_interruptible_timeout ` * :ref:`wait_event_interruptible_locked ` * :ref:`wait_event_interruptible_locked_irq ` * :ref:`wait_event_interruptible_exclusive_locked_irq ` * :ref:`wake_up ` * :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: Waiting for an event using wait_event_interruptible ` * :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: Waiting for an event using wait_event_interruptible_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: Waiting for an event using wait_event_interruptible_locked ` * :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: Waiting for an event wait_event_interruptible_locked_irq ` * :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: Waiting for an event using wait_event_interruptible_exclusive_locked_irq ` * :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 ` * :ref:`Summary of Kthread APIs ` * :ref:`Summary of Waitqueue APIs ` * :ref:`Summary of Miscellaneous APIs ` .. _p6_interruptwait_0: .. tab-set:: .. tab-item:: Version Info =============================== ======================================= # Version =============================== ======================================= Ubuntu Ubuntu 22.10 Kernel 6.7.9 =============================== ======================================= .. _p6_interruptwait_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 waitqueue? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use interruptible wait event APIs? .. 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 Waitqueue APIs ? * `DECLARE_WAIT_QUEUE_HEAD `_ * `wait_event_interruptible `_ * `wait_event_interruptible_timeout `_ * `wait_event_interruptible_locked `_ * `wait_event_interruptible_locked_irq `_ * `wait_event_interruptible_exclusive_locked_irq `_ * `wake_up `_ .. 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 `_ .. _p6_interruptwait_2: .. tab-set:: .. tab-item:: Explanation of Kthread APIs .. _p6_interruptwait_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. } .. _p6_interruptwait_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"); .. _p6_interruptwait_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); .. _p6_interruptwait_82: .. tab-set:: .. tab-item:: Explanation of Waitqueue APIs .. _p6_interruptwait_83: .. tab-set:: .. tab-item:: DECLARE_WAIT_QUEUE_HEAD * Here is the prototype of the API: `DECLARE_WAIT_QUEUE_HEAD `_ .. code-block:: c # include #define DECLARE_WAIT_QUEUE_HEAD(name) \ struct wait_queue_head name = __WAIT_QUEUE_HEAD_INITIALIZER(name) * where * `DECLARE_WAIT_QUEUE_HEAD `_ : To declare and initialize wait queue head. * name : name which is given to the wait queue head on declaration. * Here is the example of the API, .. code-block:: c DECLARE_WAIT_QUEUE_HEAD(mywaitqueue); .. _p6_interruptwait_84: .. tab-set:: .. tab-item:: wait_event_interruptible * Here is the prototype of the API: `wait_event_interruptible `_ .. code-block:: c # include #define wait_event_interruptible(wq_head, condition) * where * `wait_event_interruptible `_ : sleep until a condition gets 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 wait_event_interruptible(mywq_head, i<10); .. _p6_interruptwait_85: .. tab-set:: .. tab-item:: wake_event_interruptible_timeout * Here is the prototype of the API: `wake_event_interruptible_timeout `_ .. code-block:: c #include #define wait_event_interruptible_timeout(wq_head,condition,timeout) * where * `wait_event_interruptible_timeout `_ : sleep until a condition gets true or a timeout elapses * wq_head: the waitqueue to wait on * condition: a C expression for the event to wait for * timeout: timeout, in jiffies * Here is an example of the API .. code-block:: c wait_event_interruptible_timeout(mywq_head,i<10,msecs_to_jiffies(100)); .. _p6_interruptwait_86: .. tab-set:: .. tab-item:: wait_event_interruptible_locked * Here is the prototype of the API: `wait_event_interruptible_locked `_ .. code-block:: c #include #define wait_event_interruptible_locked(wq, condition) * where * `wait_event_interruptible_locked `_: sleep until a condition gets 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 wait_event_interruptible_lock(mywq_head,i<10); .. _p6_interruptwait_87: .. tab-set:: .. tab-item:: wait_event_interruptible_locked_irq * Here is the prototype of the API: `wait_event_interruptible_locked_irq `_ .. code-block:: c #include #define wait_event_interruptible_locked_irq(wq, condition) * where * `wait_event_interruptible_locked_irq `_ : sleep until a condition gets true * wq: 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 wait_event_interruptible_locked_irq(mywq,i<3); .. _p6_interruptwait_88: .. tab-set:: .. tab-item:: wait_event_interruptible_exclusive_locked_irq * Here is the prototype of the API: `wait_event_interruptible_exclusive_locked_irq `_ .. code-block:: c #include #define wait_event_interruptible_exclusive_locked_irq(wq, condition) * where * `wait_event_interruptible_exclusive_locked_irq `_ : sleep until a condition gets true * wq: the waitqueue to wait on * condition: a C expression for the event to wait for .. _p6_interruptwait_99: .. tab-set:: .. tab-item:: wake_up * Here is the prototype of the API: `wake_up `_ .. code-block:: c # include #define wake_up(x) __wake_up(x, TASK_NORMAL, 1, NULL) * where * `wake_up `_ : wake up the waiting waitqueues under wait_event * x: waitqueue which needs to be woken up. * Here is an example of how to use the API, .. code-block:: c wake_up(mywq_head); .. _p6_interruptwait_8: .. tab-set:: .. tab-item:: Explanation of miscellaneous APIs .. _p6_interruptwait_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"); .. _p6_interruptwait_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 } .. _p6_interruptwait_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"); .. _p6_interruptwait_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); .. _p6_interruptwait_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); .. _p6_interruptwait_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); .. _p6_interruptwait_15: .. tab-set:: .. tab-item:: Example 1: Waiting for an event using wait_event_interruptible * In this example let's see how to wait for an event using wait_event_interruptible. .. _p6_interruptwait_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 .. _p6_interruptwait_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_event_interruptible"); .. _p6_interruptwait_18: .. 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_thread; static DECLARE_WAIT_QUEUE_HEAD(wqueue); wait_queue_entry_t wait; int wait_flag = 0; .. _p6_interruptwait_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_event_interruptible_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p6_interruptwait_20: .. 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) { pr_info("initialized waitqueue head\n"); wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible wake_thread example"); if (IS_ERR(wake_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p6_interruptwait_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_event_interruptible_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p6_interruptwait_22: .. 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 and completed tasks\n"); } .. _p6_interruptwait_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 int wait_thread_fn(void *data) { while(!kthread_should_stop()) { wait_event_interruptible(wqueue,i > 3); pr_info("wait thread execution i value = %d\n",i); msleep(1000); } return 0; } int wake_thread_fn(void *data) { while(!kthread_should_stop()) { pr_info("wake thread execution i value = %d\n",i); i++; wake_up(&wqueue); msleep(1000); } return 0; } .. _p6_interruptwait_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_event_interruptible_init); module_exit(wait_event_interruptible_exit); .. _p6_interruptwait_25: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p6_interruptwait/wait_event_interruptible/kthread.c :language: c :linenos: .. _p6_interruptwait_26: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p6_interruptwait/wait_event_interruptible/Makefile :language: c :linenos: .. _p6_interruptwait_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_event_interruptible 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_event_interruptible 10002 ? 00:00:00 wait_event_interruptible wake_thread example 10003 ? 00:00:00 wait_event_interruptible wait_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 [14725.272797] inside kthread init function [14725.272885] successfully created wake thread [14725.272888] wake thread execution i value = 0 [14725.272905] successfully created wait thread [14726.280279] wake thread execution i value = 1 [14727.304261] wake thread execution i value = 2 [14728.328289] wake thread execution i value = 3 [14728.328403] wait thread execution i value = 4 [14729.352301] wake thread execution i value = 4 [14729.352303] wait thread execution i value = 4 [14730.376299] wake thread execution i value = 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 wait_event_interruptible test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [14734.186330] inside kthread exit function [14735.496434] destroyed threads and completed tasks .. _p6_interruptwait_28: .. tab-set:: .. tab-item:: Example 2: Waiting for an event using wait_event_interruptible_timeout * In this example let's see how to wait for an event using wait_event_interruptible_timeout. .. _p6_interruptwait_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 .. _p6_interruptwait_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_event_interruptible_timeout"); .. _p6_interruptwait_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_thread; static DECLARE_WAIT_QUEUE_HEAD(wqueue); int i = 0; .. _p6_interruptwait_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_event_interruptible_timeout_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p6_interruptwait_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) { pr_info("initialized waitqueue head\n"); wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_timeout wake_thread example"); if (IS_ERR(wake_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_timeout wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p6_interruptwait_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_event_interruptible_timeout_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p6_interruptwait_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 and completed tasks\n"); } .. _p6_interruptwait_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 int wait_thread_fn(void *data) { while(!kthread_should_stop()) { wait_event_interruptible_timeout(wqueue,i > 3,msecs_to_jiffies(100)); pr_info("wait thread execution i value = %d\n",i); msleep(1000); } return 0; } int wake_thread_fn(void *data) { while(!kthread_should_stop()) { pr_info("wake thread execution i value = %d\n",i); i++; wake_up(&wqueue); msleep(1000); } return 0; } .. _p6_interruptwait_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_event_interruptible_timeout_init); module_exit(wait_event_interruptible_timeout_exit); .. _p6_interruptwait_38: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p6_interruptwait/wait_event_interruptible_timeout/kthread.c :language: c :linenos: .. _p6_interruptwait_39: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p6_interruptwait/wait_event_interruptible_timeout/Makefile :language: c :linenos: .. _p6_interruptwait_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_event_interruptible_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_event_interruptible_timeout 11202 ? 00:00:00 wait_event_interruptible_timeout wake_thread example 11203 ? 00:00:00 wait_event_interruptible_timeout wait_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 [15872.375515] inside kthread init function [15872.375597] successfully created wake thread [15872.375599] wake thread execution i value = 0 [15872.375664] successfully created wait thread [15873.381504] wait thread execution i value = 1 [15873.381503] wake thread execution i value = 1 [15874.405544] wake thread execution i value = 2 [15875.429546] wait thread execution i value = 3 [15875.433549] wake thread execution i value = 3 [15876.453574] wake thread execution i value = 4 [15876.453576] wait thread execution i value = 4 [15877.477618] wake thread execution i value = 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 wait_event_interruptible_timeout test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [15885.710747] inside kthread exit function [15887.718020] destroyed threads and completed tasks .. _p6_interruptwait_41: .. tab-set:: .. tab-item:: Example 3: Waiting for an event using wait_event_interruptible_locked * In this example let's see how to wait for an event using wait_event_interruptible_locked .. _p6_interruptwait_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 .. _p6_interruptwait_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 wait_event_interruptible_locked"); .. _p6_interruptwait_44: .. 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_thread; static DECLARE_WAIT_QUEUE_HEAD(wqueue); int i = 0; .. _p6_interruptwait_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 wait_event_interruptible_locked_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p6_interruptwait_46: .. 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) { pr_info("initialized waitqueue head\n"); wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_locked wake_thread example"); if (IS_ERR(wake_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_locked wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p6_interruptwait_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 wait_event_interruptible_locked_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p6_interruptwait_48: .. 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 and completed tasks\n"); } .. _p6_interruptwait_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 int wait_thread_fn(void *data) { while(!kthread_should_stop()) { spin_lock(&wqueue.lock); wait_event_interruptible_locked(wqueue,i > 3); pr_info("wait thread execution i value = %d\n",i); spin_unlock(&wqueue.lock); msleep(1000); } return 0; } int wake_thread_fn(void *data) { while(!kthread_should_stop()) { pr_info("wake thread execution i value = %d\n",i); i++; wake_up(&wqueue); msleep(1000); } return 0; } .. _p6_interruptwait_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(wait_event_interruptible_locked_init); module_exit(wait_event_interruptible_locked_exit); .. _p6_interruptwait_51: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p6_interruptwait/wait_event_interruptible_locked/kthread.c :language: c :linenos: .. _p6_interruptwait_52: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p6_interruptwait/wait_event_interruptible_locked/Makefile :language: c :linenos: .. _p6_interruptwait_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 wait_event_interruptible_locked 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_event_interruptible_locked 15393 ? 00:00:00 wait_event_interruptible_locked wake_thread example 15394 ? 00:00:00 wait_event_interruptible_locked wait_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 [27764.569719] inside kthread init function [27764.569747] successfully created wake thread [27764.569749] wake thread execution i value = 0 [27764.569765] successfully created wait thread [27765.579192] wake thread execution i value = 1 [27766.599227] wake thread execution i value = 2 [27767.623052] wake thread execution i value = 3 [27767.623194] wait thread execution i value = 4 [27768.647208] wake thread execution i value = 4 [27768.647208] wait thread execution i value = 4 [27769.671197] wake thread execution i value = 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 wait_event_interruptible_locked test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [27774.940643] inside kthread exit function [27776.839328] destroyed threads and completed tasks .. _p6_interruptwait_56: .. tab-set:: .. tab-item:: Example 4: Waiting for an event using wait_event_interruptible_locked_irq * In this example let's see how to wait for an event using wait_event_interruptible_locked_irq. .. _p6_interruptwait_57: .. 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 .. _p6_interruptwait_58: .. 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_event_interruptible_locked_irq"); .. _p6_interruptwait_59: .. 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_thread; static DECLARE_WAIT_QUEUE_HEAD(wqueue); wait_queue_entry_t wait; int wait_flag = 0; .. _p6_interruptwait_60: .. 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_event_interruptible_locked_irq_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p6_interruptwait_61: .. 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) { pr_info("initialized waitqueue head\n"); wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_locked_irq wake_thread example"); if (IS_ERR(wake_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_locked_irq wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p6_interruptwait_62: .. 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_event_interruptible_locked_irq_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p6_interruptwait_63: .. 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 and completed tasks\n"); } .. _p6_interruptwait_64: .. 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 int wait_thread_fn(void *data) { unsigned long flags; while(!kthread_should_stop()) { spin_lock_irqsave(&wqueue.lock,flags); wait_event_interruptible_locked_irq(wqueue,i > 3); pr_info("wait thread execution i value = %d\n",i); spin_unlock_irqrestore(&wqueue.lock,flags); msleep(1000); } return 0; } int wake_thread_fn(void *data) { while(!kthread_should_stop()) { pr_info("wake thread execution i value = %d\n",i); i++; wake_up(&wqueue); msleep(1000); } return 0; } .. _p6_interruptwait_66: .. 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_event_interruptible_locked_irq_init); module_exit(wait_event_interruptible_locked_irq_exit); .. _p6_interruptwait_67: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p6_interruptwait/wait_event_interruptible_locked_irq/kthread.c :language: c :linenos: .. _p6_interruptwait_68: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p6_interruptwait/wait_event_interruptible_locked_irq/Makefile :language: c :linenos: .. _p6_interruptwait_69: .. 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_event_interruptible_locked_irq 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_event_interruptible_locked_irq 23209 ? 00:00:00 wait_event_interruptible_locked_irq wake_thread example 23210 ? 00:00:00 wait_event_interruptible_locked_irq wait_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 [40011.772449] inside kthread init function [40011.772477] successfully created wake thread [40011.772480] wake thread execution i value = 0 [40011.772492] successfully created wait thread [40012.808287] wake thread execution i value = 1 [40013.832275] wake thread execution i value = 2 [40014.856419] wake thread execution i value = 3 [40014.856531] wait thread execution i value = 4 [40015.880471] wait thread execution i value = 4 [40015.880474] wake thread execution i value = 4 [40016.904524] wake thread execution i value = 5 [40016.904524] wait thread execution i value = 5 [40017.928553] wait thread execution i value = 6 * 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_event_interruptible_locked_irq test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [40027.601355] inside kthread exit function [40029.193068] destroyed threads and completed tasks .. _p6_interruptwait_70: .. tab-set:: .. tab-item:: Example 5: Waiting for an event using wait_event_interruptible_exclusive_locked_irq * In this example let's see how to wait for an event using wait_event_interruptible_exclusive_locked_irq .. _p6_interruptwait_71: .. 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 .. _p6_interruptwait_72: .. 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_event_interruptible_exclusive_locked_irq"); .. _p6_interruptwait_73: .. 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_thread; static DECLARE_WAIT_QUEUE_HEAD(wqueue); int i = 0; .. _p6_interruptwait_74: .. 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_event_interruptible_exclusive_locked_irq_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p6_interruptwait_75: .. 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) { pr_info("initialized waitqueue head\n"); wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event_interruptible_exclusive_locked_irq wake_thread example"); if (IS_ERR(wake_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); wait_thread = kthread_run(wait_thread_fn,NULL,"wait_event_interruptible_exclusive_locked_irq wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p6_interruptwait_76: .. 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_event_interruptible_exclusive_locked_irq_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p6_interruptwait_77: .. 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 and completed tasks\n"); } .. _p6_interruptwait_78: .. 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 int wait_thread_fn(void *data) { unsigned long flags; while(!kthread_should_stop()) { spin_lock_irqsave(&wqueue.lock,flags); wait_event_interruptible(wqueue,i > 3); pr_info("wait thread execution i value = %d\n",i); spin_lock_irqrestore(&wqueue.lock,flags); msleep(1000); } return 0; } int wake_thread_fn(void *data) { while(!kthread_should_stop()) { pr_info("wake thread execution i value = %d\n",i); i++; wake_up(&wqueue); msleep(1000); } return 0; } .. _p6_interruptwait_100: .. 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_event_interruptible_exclusive_locked_irq_init); module_exit(wait_event_interruptible_exclusive_locked_irq_exit); .. _p6_interruptwait_101: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p6_interruptwait/wait_event_interruptible_exclusive_locked_irq/kthread.c :language: c :linenos: .. _p6_interruptwait_102: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p6_interruptwait/wait_event_interruptible_exclusive_locked_irq/Makefile :language: c :linenos: .. _p6_interruptwait_103: .. 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_event_interruptible_exclusive_locked_irq 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_event_interruptible_exclusive_locked_irq 24178 ? 00:00:00 wait_event_interruptible_exclusive_locked_irq wake_thread example 24179 ? 00:00:00 wait_event_interruptible_exclusive_locked_irq wait_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 [42047.260115] inside kthread init function [42047.260201] successfully created wake thread [42047.260207] wake thread execution i value = 0 [42047.260259] successfully created wait thread [42048.282972] wake thread execution i value = 1 [42049.306983] wake thread execution i value = 2 [42050.331051] wake thread execution i value = 3 [42050.331074] wait thread execution i value = 4 [42051.355088] wait thread execution i value = 4 [42051.355088] wake thread execution i value = 4 [42052.379120] wait thread execution i value = 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 wait_event_interruptible_exclusive_locked_irq test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [42058.828661] inside kthread exit function [42060.571530] destroyed threads and completed tasks .. _p6_interruptwait_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 =============================== ======================================= .. _p6_interruptwait_90: .. tab-set:: .. tab-item:: Summary of Waitqueue APIs ================================= ================================================================================== Waitqueue API Learning ================================= ================================================================================== DECLARE_WAIT_QUEUE_HEAD Declare waitqueue head wait_event_interruptible sleeps until the condition is true wait_event_interruptible_timeout sleeps until the condition is true or timer gets expired wake_up wake up the waiting waitqueues under wait_event APIs ================================= ================================================================================== .. _p6_interruptwait_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 =============================== ===========================================================================================