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:`DEFINE_WAIT ` * :ref:`wake_up ` * :ref:`wait_event ` * :ref:`wait_event_timeout ` * :ref:`prepare_to_wait ` * :ref:`prepare_to_wait_exclusive ` * :ref:`prepare_to_wait_event ` * :ref:`finish_wait ` * :ref:`wait_woken ` * :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 ` * :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_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 using prepare_to_wait ` * :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 using prepare_to_wait_exclusive ` * :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 using prepare_to_wait_event ` * :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:`Example 6: Waiting and waking up using wait_woken ` * :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 Waitqueue APIs ` * :ref:`Summary of Miscellaneous APIs ` .. _p3_waitevent_0: .. tab-set:: .. tab-item:: Version Info =============================== ======================================= # Version =============================== ======================================= Ubuntu Ubuntu 22.10 Kernel 6.7.9 =============================== ======================================= .. _p3_waitevent_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 * What are the different ways to add waitqueue entries to waitqueue head? .. 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 `_ * `DEFINE_WAIT `_ * `wake_up `_ * `wait_event `_ * `wait_event_timeout `_ * `prepare_to_wait `_ * `prepare_to_wait_exclusive `_ * `prepare_to_wait_event `_ * `finish_wait `_ * `wait_woken `_ .. 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 `_ .. _p3_waitevent_2: .. tab-set:: .. tab-item:: Explanation of Kthread APIs .. _p3_waitevent_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. } .. _p3_waitevent_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"); .. _p3_waitevent_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); .. _p3_waitevent_82: .. tab-set:: .. tab-item:: Explanation of Waitqueue APIs .. _p3_waitevent_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); .. _p3_waitevent_121: .. tab-set:: .. tab-item:: DEFINE_WAIT * Here is the prototype of the API: `DEFINE_WAIT `_ .. code-block:: c # include #define DEFINE_WAIT(name) DEFINE_WAIT_FUNC(name, autoremove_wake_function) * where * `DEFINE_WAIT `_ : Used to define wait queue entry * name : wait queue entry name which needs to be initialized * Here is the example of the API .. code-block:: c DEFINE_WAIT(mywait); .. _p3_waitevent_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. .. _p3_waitevent_118: .. tab-set:: .. tab-item:: wait_event * Here is the prototype of the API: `wait_event `_ .. code-block:: c # include #define wait_event(wq_head, condition) * where * `wait_event `_ : The process is put to sleep (TASK_UNINTERRUPTIBLE) until the condition evaluates to true. The @condition is checked each time the waitqueue wq_head is woken up. * wq_head: the waitqueue to wait on * condition: a C expression for the event to wait for * Here is an example of the API .. code-block:: c wait_event(mywq_head,i < 10); .. _p3_waitevent_119: .. tab-set:: .. tab-item:: wait_event_timeout * Here is the prototype of the API: `wait_event_timeout `_ .. code-block:: c # include #define wait_event_timeout(wq_head, condition, timeout) * where * `wait_event_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 * return type: 0 if the condition evaluated to false after the timeout elapsed, 1 if the condition evaluated to true after the @timeout elapsed, or the remaining jiffies (at least 1) if the condition evaluated to true before the timeout elapsed. .. _p3_waitevent_120: .. tab-set:: .. tab-item:: prepare_to_wait * Here is the prototype of the API: `prepare_to_wait `_ .. code-block:: c # include void prepare_to_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state); * where * `prepare_to_wait `_ : used to put waitqueue head in waiting state and will execute when wake_up is called. * wq_head : waitqueue head to be waited * wq_entry: entry of the waitqueue head * state: state at which it should be set while waiting * Here is an example of the API .. code-block:: c prepare_to_wait(mywq_head,mywq_entry,TASK_UNINTERRUPTIBLE); .. _p3_waitevent_123: .. tab-set:: .. tab-item:: prepare_to_wait_exclusive * Here is the prototype of the API: `prepare_to_wait_exclusive `_ .. code-block:: c # include bool prepare_to_wait_exclusive(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state); * where * `prepare_to_wait_exclusive `_ : puts waitqueue head in waiting state till wake_up is called. * wq_head: waitqueue head to be waited * wq_entry: waitqueue descriptor * state: state at which it should be set while waiting * Here is an example of how to use the API .. code-block:: c prepare_to_wait_exclusive(mywq_head,mywq_entry,TASK_UNINTERRUPTIBLE); .. _p3_waitevent_124: .. tab-set:: .. tab-item:: prepare_to_wait_event * Here is the prototype of the API: `prepare_to_wait_event `_ .. code-block:: c # include long prepare_to_wait_event(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry, int state); * where * `prepare_to_wait_event `_ : puts waitqueue in waiting state till wake_up is called. * wq_head: waitqueue head to be waited * wq_entry: waitqueue descriptor * state: state at which it should be set while waiting * Here is an example of how to use the API .. code-block:: c prepare_to_wait_event(mywq_head,mywq_entry,TASK_UNINTERRUPTIBLE) .. _p3_waitevent_122: .. tab-set:: .. tab-item:: finish_wait * Here is the prototype of the API: `finish_wait `_ .. code-block:: c # include void finish_wait(struct wait_queue_head *wq_head, struct wait_queue_entry *wq_entry); * where * `finish_wait `_ : clean up after waiting in a queue * wq_head: waitqueue waited on * wq_entry: wait descriptor * Here is an example of how to use the API .. code-block:: c finish_wait(mywq_head,mywq_entry); .. _p3_waitevent_125: .. tab-set:: .. tab-item:: wait_woken * Here is the prototype of the API: `wait_woken `_ .. code-block:: c # include long wait_woken(struct wait_queue_entry *wq_entry, unsigned mode, long timeout); * where * `wait_woken `_ : puts in waiting state and wakes up when the timer gets expire. * wq_entry: waitqueue description * mode: state at which it is set during waiting state * timeout: timer until which it stays in waiting state * Here is an example of how to use the API .. code-block:: c wait_woken(mywait,TASK_UNINTERRUPTIBLE,msecs_to_jiffies(100)); .. _p3_waitevent_8: .. tab-set:: .. tab-item:: Explanation of miscellaneous APIs .. _p3_waitevent_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"); .. _p3_waitevent_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 } .. _p3_waitevent_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"); .. _p3_waitevent_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); .. _p3_waitevent_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); .. _p3_waitevent_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); .. _p3_waitevent_15: .. tab-set:: .. tab-item:: Example 1: Waiting for an event using wait_event * In this example let's see how to wait for an event using wait_event and use with kthread. .. _p3_waitevent_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 .. _p3_waitevent_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"); .. _p3_waitevent_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; static DECLARE_WAIT_QUEUE_HEAD(wqueue); int i = 0; .. _p3_waitevent_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_init(void) { pr_info("Inside thread init function\n"); thread_start(); return 0; } .. _p3_waitevent_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) { wake_thread = kthread_run(wake_thread_fn,NULL,"wait_event 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 wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p3_waitevent_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_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p3_waitevent_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"); } .. _p3_waitevent_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(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; } .. _p3_waitevent_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_init); module_exit(wait_event_exit); .. _p3_waitevent_25: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p3_waitevent/wait_event/kthread.c :language: c :linenos: .. _p3_waitevent_26: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p3_waitevent/wait_event/Makefile :language: c :linenos: .. _p3_waitevent_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 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 10918 ? 00:00:00 wait_event wake_thread example 10919 ? 00:00:00 wait_event 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 [ 2888.995943] inside kthread init function [ 2888.996075] successfully created wake thread [ 2888.996081] wake thread execution i value = 0 [ 2888.996117] successfully created wait thread [ 2890.027417] wake thread execution i value = 1 [ 2891.051318] wake thread execution i value = 2 [ 2892.075206] wake thread execution i value = 3 [ 2892.075312] wait thread execution i value = 4 [ 2893.099073] wake thread execution i value = 4 [ 2893.099156] 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 test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 2894.667898] inside thread exit function [ 2896.170898] destroyed threads and completed tasks .. _p3_waitevent_28: .. tab-set:: .. tab-item:: Example 2: Waiting for an event using wait_event_timeout * In this example let's see how to wait for an event using wait_event_timeout. .. _p3_waitevent_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 .. _p3_waitevent_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_timeout"); .. _p3_waitevent_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); wait_queue_entry_t wait; int wait_flag = 0; .. _p3_waitevent_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_timeout_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p3_waitevent_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_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_timeout wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p3_waitevent_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_timeout_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p3_waitevent_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"); } .. _p3_waitevent_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_timeout(wqueue,i > 100,msecs_to_jiffies(1000)); 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; } .. _p3_waitevent_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_timeout_init); module_exit(wait_event_timeout_exit); .. _p3_waitevent_38: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p3_waitevent/wait_event_timeout/kthread.c :language: c :linenos: .. _p3_waitevent_39: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p3_waitevent/wait_event_timeout/Makefile :language: c :linenos: .. _p3_waitevent_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_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_timeout 15015 ? 00:00:00 wait_event_timeout wake_thread example 15016 ? 00:00:00 wait_event_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 [ 5993.812634] inside kthread init function [ 5993.812659] successfully created wake thread [ 5993.812660] wake thread execution i value = 0 [ 5993.812676] successfully created wait thread [ 5994.838087] wake thread execution i value = 1 [ 5994.838086] wait thread execution i value = 1 [ 5995.862214] wake thread execution i value = 2 [ 5996.886288] wake thread execution i value = 3 [ 5996.886297] wait thread execution i value = 4 * 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_timeout test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 6003.196103] inside kthread exit function [ 6006.103486] destroyed threads and completed tasks .. _p3_waitevent_41: .. tab-set:: .. tab-item:: Example 3: Waiting using prepare_to_wait * In this example let's see how to wait using prepare_to_wait .. _p3_waitevent_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 .. _p3_waitevent_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 prepare_to_wait"); .. _p3_waitevent_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; static DECLARE_WAIT_QUEUE_HEAD_ONSTACK(wqueue); wait_queue_entry_t wait; int wait_flag = 0; .. _p3_waitevent_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 prepare_to_wait_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p3_waitevent_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) { wake_thread = kthread_run(wake_thread_fn,NULL,"prepare_to_wait 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,"prepare_to_wait wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p3_waitevent_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 prepare_to_wait_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p3_waitevent_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"); } .. _p3_waitevent_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) { DEFINE_WAIT(wait); while(!kthread_should_stop()) { prepare_to_wait(&wqueue,&wait,TASK_INTERRUPTIBLE); schedule(); pr_info("wait thread execution i value = %d\n",i); msleep(1000); } finish_wait(&wqueue,&wait); 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; } .. _p3_waitevent_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(prepare_to_wait_init); module_exit(prepare_to_wait_exit); .. _p3_waitevent_51: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p3_waitevent/prepare_to_wait/kthread.c :language: c :linenos: .. _p3_waitevent_52: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p3_waitevent/prepare_to_wait/Makefile :language: c :linenos: .. _p3_waitevent_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 prepare_to_wait 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 prepare_to_wait 7662 ? 00:00:00 prepare_to_wait wake_thread example 7663 ? 00:00:00 prepare_to_wait 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 [ 1319.765501] inside kthread init function [ 1319.765565] successfully created wake thread [ 1319.765572] wake thread execution i value = 0 [ 1319.765613] successfully created wait thread [ 1320.797801] wake thread execution i value = 1 [ 1320.797872] wait thread execution i value = 2 [ 1321.825765] wake thread execution i value = 2 [ 1321.825871] wait thread execution i value = 3 * 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 prepare_to_wait test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 1330.817383] inside kthread exit function [ 1331.021672] destroyed threads and completed tasks .. _p3_waitevent_56: .. tab-set:: .. tab-item:: Example 4: Waiting using prepare_to_wait_exclusive * In this example let's see how to wait using prepare_to_wait_exclusive. .. _p3_waitevent_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 .. _p3_waitevent_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 prepare_to_wait_exclusive"); .. _p3_waitevent_59: .. 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_WAIT_QUEUE_HEAD(wqueue); struct wait_queue_entry wait; int wait_flag = 0; .. _p3_waitevent_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 prepare_to_wait_exclusive_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p3_waitevent_61: .. 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) { wake_thread = kthread_run(wake_thread_fn,NULL,"prepare_to_wait_exclusive 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,"prepare_to_wait_exclusive wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p3_waitevent_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 prepare_to_wait_exclusive_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p3_waitevent_63: .. 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"); } .. _p3_waitevent_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) { DEFINE_WAIT(wait); while(!kthread_should_stop()) { prepare_to_wait_exclusive(&wqueue,&wait,TASK_INTERRUPTIBLE); schedule(); pr_info("wait thread execution i value = %d\n",i); msleep(1000); } finish_wait(&wqueue,&wait); 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; } .. _p3_waitevent_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(prepare_to_wait_exclusive_init); module_exit(prepare_to_wait_exclusive_exit); .. _p3_waitevent_67: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p3_waitevent/prepare_to_wait_exclusive/kthread.c :language: c :linenos: .. _p3_waitevent_68: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p3_waitevent/prepare_to_wait_exclusive/Makefile :language: c :linenos: .. _p3_waitevent_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 prepare_to_wait_exclusive 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 prepare_to_wait_exclusive 16463 ? 00:00:00 prepare_to_wait_exclusive wake_thread example 16464 ? 00:00:00 prepare_to_wait_exclusive 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 [ 7174.172701] inside kthread init function [ 7174.172792] successfully created wake thread [ 7174.172793] wake thread execution i value = 0 [ 7174.172859] successfully created wait thread [ 7175.184158] wake thread execution i value = 1 [ 7175.184252] wait thread execution i value = 2 [ 7176.208146] wake thread execution i value = 2 [ 7176.208166] wait thread execution i value = 3 [ 7177.232125] wake thread execution i value = 3 * 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 prepare_to_wait_exclusive test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 7184.655846] inside kthread exit function [ 7186.447855] destroyed threads and completed tasks .. _p3_waitevent_70: .. tab-set:: .. tab-item:: Example 5: Waiting for prepare_to_wait_event * In this example let's see how to wait using prepare_to_wait_event. .. _p3_waitevent_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 .. _p3_waitevent_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 prepare_to_wait_event"); .. _p3_waitevent_73: .. 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_WAIT_QUEUE_HEAD(wqueue); struct wait_queue_entry wait; int wait_flag = 0; .. _p3_waitevent_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 prepare_to_wait_event_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p3_waitevent_75: .. 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) { wake_thread = kthread_run(wake_thread_fn,NULL,"prepare_to_wait_event 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,"prepare_to_wait_event wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p3_waitevent_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 prepare_to_wait_event_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p3_waitevent_77: .. 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"); } .. _p3_waitevent_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) { DEFINE_WAIT(wait); while(!kthread_should_stop()) { prepare_to_wait_event(&wqueue,&wait,TASK_INTERRUPTIBLE); schedule(); pr_info("wait thread execution i value = %d\n",i); msleep(1000); } finish_wait(&wqueue,&wait); return 0; } int wake_thread_fn(void *data) { while(!kthread_should_stop()) { pr_info("wake thread execution i value = %d\n",i); i++; msleep(1000); } return 0; } .. _p3_waitevent_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(prepare_to_wait_event_init); module_exit(prepare_to_wait_event_exit); .. _p3_waitevent_101: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p3_waitevent/prepare_to_wait_event/kthread.c :language: c :linenos: .. _p3_waitevent_102: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p3_waitevent/prepare_to_wait_event/Makefile :language: c :linenos: .. _p3_waitevent_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 prepare_to_wait_event 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 prepare_to_wait_event 19802 ? 00:00:00 prepare_to_wait_event wake_thread example 19803 ? 00:00:00 prepare_to_wait_event 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 [ 9587.691360] inside kthread init function [ 9587.691507] successfully created wake thread [ 9587.691513] wake thread execution i value = 0 [ 9587.691677] successfully created wait thread [ 9588.718682] wake thread execution i value = 1 [ 9589.742720] wake thread execution i value = 2 [ 9590.766771] wake thread execution i value = 3 * 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 prepare_to_wait_event test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 9601.069175] inside kthread exit function [ 9602.031027] wait thread execution i value = 14 [ 9603.055049] destroyed threads and completed tasks .. _p3_waitevent_104: .. tab-set:: .. tab-item:: Example 6: Waiting and waking using wait_woken * In this example let's see how to wait and wake up wait descriptor using wait_woken. .. _p3_waitevent_105: .. 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 .. _p3_waitevent_106: .. 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_woken"); .. _p3_waitevent_107: .. 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_up_thread; DECLARE_WAIT_QUEUE_HEAD(wqueue); struct wait_queue_entry wait; int i = 0; .. _p3_waitevent_108: .. 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_woken_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p3_waitevent_109: .. 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) { wake_thread = kthread_run(wake_thread_fn,NULL,"wait_woken 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_woken wait_thread example"); if(IS_ERR(wait_thread)) pr_info("error creating thread\n"); else pr_info("kthread created successfully\n"); } .. _p3_waitevent_110: .. 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_woken_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p3_waitevent_111: .. 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 and completed tasks\n"); } .. _p3_waitevent_112: .. 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) { DEFINE_WAIT(wait); while(!kthread_should_stop()) { wait_woken(&wait,TASK_UNINTERRUPTIBLE,msecs_to_jiffies(2000)); 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++; msleep(1000); } return 0; } .. _p3_waitevent_114: .. 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_woken_init); module_exit(wait_woken_exit); .. _p3_waitevent_115: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p3_waitevent/wait_woken/kthread.c :language: c :linenos: .. _p3_waitevent_116: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p3_waitevent/wait_woken/Makefile :language: c :linenos: .. _p3_waitevent_117: .. 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_woken 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_woken 24197 ? 00:00:00 wait_woken wake_thread example 24198 ? 00:00:00 wait_woken 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 [12873.287079] inside kthread init function [12873.287202] successfully created wake thread [12873.287222] successfully created wait thread [12873.287233] wake thread execution i value = 0 [12874.320890] wake thread execution i value = 1 [12875.312942] wait thread execution i value = 2 [12875.344928] wake thread execution i value = 2 [12876.368973] wake thread execution i value = 3 [12877.393097] wake thread execution i value = 4 [12878.353048] 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_woken test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [12882.952350] inside kthread exit function [12884.561252] destroyed threads and completed tasks .. _p3_waitevent_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 =============================== ======================================= .. _p3_waitevent_90: .. tab-set:: .. tab-item:: Summary of Waitqueue APIs ================================= ================================================================================== Waitqueue API Learning ================================= ================================================================================== DECLARE_WAIT_QUEUE_HEAD Declare waitqueue head DEFINE_WAIT Declare waitqueue descriptor wait_event sleeps until the condition is true wait_event_timeout sleeps until the condition is true or timer gets expired prepare_to_wait sleeps until a wake_up signal is sent finish_wait clean up after waiting in a queue wake_up wake up the waiting waitqueues under wait_event APIs ================================= ================================================================================== .. _p3_waitevent_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 =============================== ===========================================================================================