Create Kthread Worker ======================= .. 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_create_worker ` * :ref:`kthread_init_worker ` * :ref:`kthread_should_stop ` * :ref:`kthread_run ` * :ref:`kthread_flush_worker ` * :ref:`kthread_destroy_worker ` * :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: Creating kthread worker using kthread_create_worker ` * :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: Creating thread worker using kthread_init_worker ` * :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: Flush created kthread worker using kthread_flush_worker ` * :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 Miscellaneous APIs ` .. _p3_CreateKthreadworker_0: .. tab-set:: .. tab-item:: Version Info =============================== ======================================= # Version =============================== ======================================= Ubuntu Ubuntu 22.10 Kernel 6.7.9 =============================== ======================================= .. _p3_CreateKthreadworker_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 the kthread worker ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * What are the different ways to create kthread worker? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to destroy the kthread worker ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use Kthread APIs ? * `kthread_create_worker `_ * `kthread_init_worker `_ * `kthread_run `_ * `kthread_flush_worker `_ * `kthread_destroy_worker `_ .. 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_CreateKthreadworker_2: .. tab-set:: .. tab-item:: Explanation of Kthread APIs .. _p3_CreateKthreadworker_3: .. tab-set:: .. tab-item:: kthread_create_worker * Here is the function prototype of the API: `kthread_create_worker `_ .. code-block:: c #include struct kthread_worker *kthread_create_worker(unsigned int flags, const char namefmt[], ...); * where * `kthread_create_worker `_: creates a kthread worker. * return type: * returns a pointer (i.e struct kthread_worker*) to the allocated worker on success * ERR_PTR(-ENOMEM) when the needed structures could not get allocated * ERR_PTR(-EINTR) when the caller was killed by a fatal signal. * flags: flags modifying the default behaviour of the kthread worker. * namefmt: printf-style name for the kthread worker. * Here is an example of how to use the API, .. code-block:: c kthread_create_worker(0,"sample kthread worker"); .. _p3_CreateKthreadworker_4: .. tab-set:: .. tab-item:: kthread_init_worker * Here is the function prototype of the API: `kthread_init_worker `_ .. code-block:: c #include #define kthread_init_worker(worker) \ do { \ static struct lock_class_key __key; \ __kthread_init_worker((worker), "("#worker")->lock", &__key); \ } while (0) * where * `kthread_init_worker `_: initializes a kthread worker. * worker: kthread worker which needs to be initialized * Here is an example of how to use the API, .. code-block:: c kthread_init_worker(myworker); .. _p3_CreateKthreadworker_50: .. 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_CreateKthreadworker_5: .. 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_CreateKthreadworker_6: .. tab-set:: .. tab-item:: kthread_flush_worker * Here is the function prototype of the API: `kthread_flush_worker `_ .. code-block:: c #include void kthread_flush_worker(struct kthread_worker *worker); * where * `kthread_flush_worker `_: flush all current works on kthread worker, waits until all currently executing or pending works are completed. * worker: kthread worker which needs to be flushed. * Here is the example of how to use the API, .. code-block:: c kthread_flush_worker(myworker); .. _p3_CreateKthreadworker_7: .. tab-set:: .. tab-item:: kthread_destroy_worker * Here is the prototye of the API: `kthread_destroy_worker `_ .. code-block:: c #include void kthread_destroy_worker(struct kthread_worker *worker); * where * `kthread_destroy_worker `_ : flush and destroy the kthread worker. * worker: worker which needs to be destroyed. * Here is an example of how to use the API, .. code-block:: c kthread_destroy_worker(myworker); .. _p3_CreateKthreadworker_8: .. tab-set:: .. tab-item:: Explanation of miscellaneous APIs .. _p3_CreateKthreadworker_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_CreateKthreadworker_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_CreateKthreadworker_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_CreateKthreadworker_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_CreateKthreadworker_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_CreateKthreadworker_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_CreateKthreadworker_15: .. tab-set:: .. tab-item:: Example 1: Creating Kthread worker using kthread_create_worker * In this example let's see how to create kthread worker using kthread_create_worker and execute it. .. _p3_CreateKthreadworker_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 .. _p3_CreateKthreadworker_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 kthread_create_worker"); .. _p3_CreateKthreadworker_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 kthread_worker *def_worker; static struct task_struct *def_thread; .. _p3_CreateKthreadworker_19: .. tab-set:: .. tab-item:: Module init function * Add the module init function which will be executed once we load the kernel module using insmod command. .. code-block:: c static int __init kthread_create_worker_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p3_CreateKthreadworker_51: .. tab-set:: .. tab-item:: thread start function * Add the thread start function which creates kthread worker and kthread, on success creation it starts executing the task. .. code-block:: c void thread_start(void) { def_worker = kthread_create_worker(0,"kthread_create_worker worker example"); if (IS_ERR(def_thread)) pr_info("Error creating in kthread worker\n"); else pr_info("Created kthread_worker\n"); def_thread = kthread_run(def_thread_task,NULL,"kthread_create_worker thread example"); if (IS_ERR(def_thread)) pr_info("Error creating thread using kthread_run\n"); else pr_info("Created kthread task\n"); } .. _p3_CreateKthreadworker_20: .. tab-set:: .. tab-item:: Module exit function * Add the module exit function which will be executed once we unload the kernel module using rmmod command. .. code-block:: c static void __exit kthread_create_worker_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p3_CreateKthreadworker_52: .. tab-set:: .. tab-item:: Thread stop function * Add the thread stop function which destroys the thread and stops the execution. .. code-block:: c void thread_stop(void) { kthread_destroy_worker(def_worker); pr_info("Destroyed kthread worker and thread\n"); } .. _p3_CreateKthreadworker_21: .. tab-set:: .. tab-item:: Thread function API * Add the thread function API which will be called as soon as the kthread is created and is in running state. .. code-block:: c static int def_thread_task(void *data) { while(!kthread_should_stop()) { pr_info("Inside def_thread_task\n"); msleep(1000); } return 0; } .. _p3_CreateKthreadworker_22: .. tab-set:: .. tab-item:: Driver entry points * Add the driver entry points which will be executed once the module is inserted or removed from the kernel. .. code-block:: c module_init(kthread_create_worker_init); module_exit(kthread_create_worker_exit); .. _p3_CreateKthreadworker_23: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p3_CreateKthreadworker/kthread_create_worker/kthread.c :language: c :linenos: .. _p3_CreateKthreadworker_24: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p3_CreateKthreadworker/kthread_create_worker/Makefile :language: c :linenos: .. _p3_CreateKthreadworker_25: .. 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 kthread_create_worker author: Linux Usr license: GPL srcversion: 8D2147F67AB01CF0E482DAC depends: retpoline: Y name: kthread vermagic: 6.7.9 SMP preempt mod_unload modversions * insert the module using insmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo insmod ./kthread.ko * check if the module is loaded or not using lsmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo lsmod | grep kthread kthread 16384 0 * check if the thread is created or not using ps command. .. code-block:: shell test@test-V520-15IKL:~$ ps -N | grep kthread_create_worker 6390 ? 00:00:00 kthread_create_worker worker example 6391 ? 00:00:00 kthread_create_worker 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 [20742.666348] Inside kthread init function [20742.666423] Created kthread_worker [20742.666461] Created kthread task [20742.666481] Inside def_thread_task [20743.688961] Inside def_thread_task [20744.712987] Inside def_thread_task * remove the module from kernel using rmmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo rmmod kthread * check if the module is still loaded after removing the kernel module using lsmod if it is not displayed in lsmod output it is verified that the module is removed successfully. .. code-block:: shell test@test-V520-15IKL:~$ sudo lsmod | grep kthread test@test-V520-15IKL:~$ * check if the thread is destroyed using ps command if it is not displayed in ps output we can confirm that the thread is destroyed successfully. .. code-block:: shell test@test-V520-15IKL:~$ ps -N | grep kthread_create_worker test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [20753.695905] Inside kthread exit function [20753.696107] Destroyed kthread worker and thread .. _p3_CreateKthreadworker_26: .. tab-set:: .. tab-item:: Example 2: Creating Kthread worker using kthread_init_worker * In this example let's see how to create kthread worker using kthread_init_worker and execute it. .. _p3_CreateKthreadworker_27: .. 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_CreateKthreadworker_28: .. 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 kthread_init_worker"); .. _p3_CreateKthreadworker_29: .. tab-set:: .. tab-item:: Initialize thread variables * Declare the thread variables which we are going to create and use in this example .. code-block:: c struct kthread_worker my_worker; struct task_struct *my_thread; .. _p3_CreateKthreadworker_30: .. tab-set:: .. tab-item:: Module init function * Add the module init function which will be executed once we load the kernel module using insmod command. .. code-block:: c static int __init kthread_init_worker_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p3_CreateKthreadworker_53: .. tab-set:: .. tab-item:: Thread start function * Add the thread start function in which the worker and thread is created and starts its execution. .. code-block:: c void thread_start(void) { kthread_init_worker(&my_worker); pr_info("kthread worker is initialized\n"); my_thread = kthread_run(my_work_fn,NULL,"kthread_init_worker thread example"); if(IS_ERR(my_thread)) pr_info("Error creating kthread\n"); else pr_info("Thread created successfully\n"); } .. _p3_CreateKthreadworker_31: .. tab-set:: .. tab-item:: Module exit function * Add the module exit function which will be executed once we unload the kernel module using rmmod command. .. code-block:: c static void __exit kthread_init_worker_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p3_CreateKthreadworker_54: .. tab-set:: .. tab-item:: Thread stop function * Add the thread stop function in which worker is destroyed and the thread stops its execution. .. code-block:: c void thread_stop(void) { kthread_destroy_worker(&my_worker); pr_info("Destroyed kthread and worker\n"); } .. _p3_CreateKthreadworker_32: .. 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 my_work_fn(void *data) { while (!kthread_should_stop()) { pr_info("Inside kthread work function\n"); msleep(100); } return 0; } .. _p3_CreateKthreadworker_33: .. tab-set:: .. tab-item:: Driver entry points * Add the driver entry points which will be executed once the module is inserted or removed from the kernel. .. code-block:: c module_init(kthread_init_worker_init); module_exit(kthread_init_worker_exit); .. _p3_CreateKthreadworker_34: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p3_CreateKthreadworker/kthread_init_worker/kthread.c :language: c :linenos: .. _p3_CreateKthreadworker_35: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p3_CreateKthreadworker/kthread_init_worker/Makefile :language: c :linenos: .. _p3_CreateKthreadworker_36: .. 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 kthread_init_worker author: Linux Usr license: GPL srcversion: 8D2147F67AB01CF0E482DAC depends: retpoline: Y name: kthread vermagic: 6.7.9 SMP preempt mod_unload modversions * insert the module using insmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo insmod ./kthread.ko * check if the module is loaded or not using lsmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo lsmod | grep kthread kthread 16384 0 * check if the thread is created or not using ps command. .. code-block:: shell test@test-V520-15IKL:~$ ps -N | grep kthread_init_worker 5241 ? 00:00:00 kthread_init_worker 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 [ 162.737148] Inside kthread init function [ 162.737152] kthread worker is initialized [ 162.737179] Thread created successfully [ 162.737181] Inside kthread work function [ 162.844228] Inside kthread work function [ 162.952298] Inside kthread work function * remove the module from kernel using rmmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo rmmod kthread * check if the module is still loaded after removing the kernel module using lsmod if it is not displayed in lsmod output it is verified that the module is removed successfully. .. code-block:: shell test@test-V520-15IKL:~$ sudo lsmod | grep kthread test@test-V520-15IKL:~$ * check if the thread is destroyed using ps command if it is not displayed in ps output we can confirm that the thread is destroyed successfully. .. code-block:: shell test@test-V520-15IKL:~$ ps -N | grep kthread_init_worker test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 185.693286] Inside kthread exit function [ 185.693433] Destroyed kthread and worker .. _p3_CreateKthreadworker_37: .. tab-set:: .. tab-item:: Example 3: Flush created kthread worker using kthread_flush_worker * In this example let's see how to flush created worker using kthread_flush_worker. .. _p3_CreateKthreadworker_38: .. 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 .. _p3_CreateKthreadworker_39: .. 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 kthread_flush_worker"); .. _p3_CreateKthreadworker_40: .. 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 kthread_worker *def_worker; static struct task_struct *def_thread; .. _p3_CreateKthreadworker_41: .. tab-set:: .. tab-item:: Module init function * Add the module init function which will be executed once we load the kernel module using insmod command. .. code-block:: c static int __init kthread_flush_worker_init(void) { pr_info("Inside kthread init function\n"); thread_start(); return 0; } .. _p3_CreateKthreadworker_55: .. tab-set:: .. tab-item:: thread start function * Add the thread start function in which the thread and worker is created and starts its execution. .. code-block:: c void thread_start (void) { def_worker = kthread_create_worker(0,"kthread_flush_worker worker example"); if (IS_ERR(def_worker)) pr_info("Error creating in kthread worker\n"); else pr_info("Created kthread_worker\n"); def_thread = kthread_run(def_thread_task,NULL,"kthread_flush_worker thread example"); if (IS_ERR(def_thread)) pr_info("Error creating thread using kthread_run\n"); else pr_info("Created kthread task\n"); } .. _p3_CreateKthreadworker_42: .. tab-set:: .. tab-item:: Module exit function * Add the module exit function which will be executed once we unload the kernel module using rmmod command. .. code-block:: c static void __exit kthread_flush_worker_exit(void) { pr_info("Inside kthread exit function\n"); thread_stop(); } .. _p3_CreateKthreadworker_56: .. tab-set:: .. tab-item:: thread stop function * Add the thread stop function which flushes the worker and stops the execution. .. code-block:: c void thread_stop(void) { kthread_flush_worker(def_worker); pr_info("Destroyed kthread worker and thread\n"); } .. _p3_CreateKthreadworker_43: .. tab-set:: .. tab-item:: Thread function API * Add the thread function API which will be called as soon as the kthread is created and is in running state. .. code-block:: c static int def_thread_task(void *data) { while (!kthread_should_stop()) { pr_info("Inside def_thread_task\n"); msleep(100); } return 0; } .. _p3_CreateKthreadworker_44: .. tab-set:: .. tab-item:: Driver entry points * Add the driver entry points which will be executed once the module is inserted or removed from the kernel. .. code-block:: c module_init(kthread_flush_worker_init); module_exit(kthread_flush_worker_exit); .. _p3_CreateKthreadworker_45: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: kthread.c .. literalinclude:: p3_CreateKthreadworker/kthread_flush_worker/kthread.c :language: c :linenos: .. _p3_CreateKthreadworker_46: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p3_CreateKthreadworker/kthread_flush_worker/Makefile :language: c :linenos: .. _p3_CreateKthreadworker_47: .. 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 kthread_flush_worker author: Linux Usr license: GPL srcversion: 8D2147F67AB01CF0E482DAC depends: retpoline: Y name: kthread vermagic: 6.7.9 SMP preempt mod_unload modversions * insert the module using insmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo insmod ./kthread.ko * check if the module is loaded or not using lsmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo lsmod | grep kthread kthread 16384 0 * check if the thread is created or not using ps command. .. code-block:: shell test@test-V520-15IKL:~$ ps -N | grep kthread_flush_worker 6568 ? 00:00:00 kthread_flush_worker worker example 6569 ? 00:00:00 kthread_flush_worker 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 [ 2395.398511] Inside kthread init function [ 2395.398540] Created kthread_worker [ 2395.398556] Created kthread task [ 2395.398558] Inside def_thread_task [ 2395.505983] Inside def_thread_task [ 2395.614115] Inside def_thread_task * remove the module from kernel using rmmod command. .. code-block:: shell test@test-V520-15IKL:~$ sudo rmmod kthread * check if the module is still loaded after removing the kernel module using lsmod if it is not displayed in lsmod output it is verified that the module is removed successfully. .. code-block:: shell test@test-V520-15IKL:~$ sudo lsmod | grep kthread test@test-V520-15IKL:~$ * check if the thread is destroyed using ps command if it is not displayed in ps output we can confirm that the thread is destroyed successfully. .. code-block:: shell test@test-V520-15IKL:~$ ps -N | grep kthread_flush_worker test@test-V520-15IKL:~$ * Check for kernel messages from exit function using dmesg command. .. code-block:: shell test@test-V520-15IKL:~$ sudo dmesg [ 2405.616976] Inside kthread exit function [ 2405.617018] Destroyed kthread worker and thread .. _p3_CreateKthreadworker_48: .. tab-set:: .. tab-item:: Summary of Kthread APIs =============================== ======================================= kthread API Learning =============================== ======================================= kthread_create_worker Create kthread worker kthread_init_worker Initialize kthread worker kthread_run Create and wake a thread kthread_should_stop To determine when thread should exit kthread_flush_worker Complete and flush kthread worker kthread_destroy_worker Destroy kthread worker =============================== ======================================= .. _p3_CreateKthreadworker_49: .. 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 =============================== ===========================================================================================