Check Workqueue =============== .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Version Info ` * :ref:`Learnings in this program ` * :ref:`Explanation of Workqueue APIs ` * :ref:`create_workqueue ` * :ref:`INIT_WORK ` * :ref:`queue_work ` * :ref:`destroy_workqueue ` * :ref:`INIT_DELAYED_WORK ` * :ref:`queue_delayed_work ` * :ref:`cancel_delayed_work_sync ` * :ref:`schedule_delayed_work ` * :ref:`cancel_delayed_work ` * :ref:`work_pending ` * :ref:`schedule_work ` * :ref:`cancel_workqueue ` * :ref:`Example 1 : Checking the workqueue ` * :ref:`List of headers ` * :ref:`Module macros ` * :ref:`Initializing work ` * :ref:`Workqueue Handler Function ` * :ref:`Queue Start Function ` * :ref:`Queue Init Function ` * :ref:`Queue Stop Function ` * :ref:`Queue Exit Function ` * :ref:`Driver entry points ` * :ref:`See the full program below ` * :ref:`Makefile ` * :ref:`Compile and Load ` * :ref:`Example 2 : Checking the workqueue ` * :ref:`List of headers ` * :ref:`Module macros ` * :ref:`Initializing work ` * :ref:`Workqueue Handler Function ` * :ref:`Queue Start Function ` * :ref:`Queue Init Function ` * :ref:`Queue Stop Function ` * :ref:`Queue Exit Function ` * :ref:`Driver entry points ` * :ref:`See the full program below ` * :ref:`Makefile ` * :ref:`Compile and Load ` .. _check_workqueue_0: .. tab-set:: .. tab-item:: Version Info =============================== ======================================= # Version =============================== ======================================= Ubuntu Ubuntu 22.10 Kernel 6.8.0 =============================== ======================================= .. _check_workqueue_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 workqueue ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to allocate the work to the queue ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use Workqueue APIs ? * `create_workqueue `_ * `INIT_WORK `_ * `queue_work `_ * `destroy_workqueue `_ * `INIT_DELAYED_WORK `_ * `delayed_work_pending `_ * `schedule_delayed_work `_ * `cancel_delayed_work_sync `_ * `cancel_delayed_work `_ * `work_pending `_ * `schedule_work `_ * `cancel_work `_ .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use below APIs ? * `MODULE_LICENSE `_ * `MODULE_DESCRIPTION `_ * `MODULE_AUTHOR `_ * `module_init `_ * `module_exit `_ .. _check_workqueue_2: .. tab-set:: .. tab-item:: Explanation of workqueue APIs .. _check_workqueue_3: .. tab-set:: .. tab-item:: create_workqueue * Here is the function prototype of the API: `create_workqueue `_ .. code-block:: c #include #define create_workqueue(name) * Here is an example of how to use the API, .. code-block:: c my_workqueue = create_workqueue(WQ_NAME); .. _check_workqueue_4: .. tab-set:: .. tab-item:: INIT_WORK * Here is the function prototype of the API: `INIT_WORK `_ .. code-block:: c #include #define INIT_WORK(_work, _func) * Here is an example of how to use the API, .. code-block:: c INIT_WORK(&my_work_1,workqueue_func_1); .. _check_workqueue_5: .. tab-set:: .. tab-item:: queue_work * Here is the function prototype of the API: `queue_work `_ .. code-block:: c #include static inline bool queue_work(struct workqueue_struct *wq, struct work_struct *work); * Here is an example of how to use the API, .. code-block:: c queue_work(my_workqueue,&my_work_1); .. _check_workqueue_6: .. tab-set:: .. tab-item:: destroy_workqueue * Here is the function prototype of the API: `queue_work `_ .. code-block:: c #include extern void destroy_workqueue(struct workqueue_struct *wq); * Here is an example of how to use the API, .. code-block:: c destroy_workqueue(my_workqueue); .. _check_workqueue_7: .. tab-set:: .. tab-item:: work_pending * Here is the function prototype of the API: `work_pending `_ .. code-block:: c #include #define work_pending(work) * Here is an example of how to use the API, .. code-block:: c work_pending(&my_work_1); .. _check_workqueue_8: .. tab-set:: .. tab-item:: INIT_DELAYED_WORK * Here is the function prototype of the API: `INIT_DELAYED_WORK `_ .. code-block:: c #include #define INIT_DELAYED_WORK(_work, _func) * Here is an example of how to use the API, .. code-block:: c INIT_DELAYED_WORK(&my_work_1,workqueue_func_1); INIT_DELAYED_WORK(&my_work_2,workqueue_func_2); .. _check_workqueue_9: .. tab-set:: .. tab-item:: delayed_work_pending * Here is the function prototype of the API: `delayed_work_pending `_ .. code-block:: c #include #define delayed_work_pending(w) * Here is an example of how to use the API, .. code-block:: c delayed_work_pending(&my_work_1); .. _check_workqueue_10: .. tab-set:: .. tab-item:: cancel_delayed_work_sync * Here is the function prototype of the API: `cancel_delayed_work_sync `_ .. code-block:: c #include extern bool cancel_delayed_work_sync(struct delayed_work *dwork); * Here is an example of how to use the API, .. code-block:: c cancel_delayed_work_sync(&my_work_1); cancel_delayed_work_sync(&my_work_2); .. _check_workqueue_11: .. tab-set:: .. tab-item:: schedule_work * Here is the function prototype of the API: `schedule_work `_ .. code-block:: c #include static inline bool schedule_work(struct work_struct *work); * Here is an example of how to use the API, .. code-block:: c schedule_work(&my_work_1); schedule_work(&my_work_2); .. _check_workqueue_12: .. tab-set:: .. tab-item:: cancel_work * Here is the function prototype of the API: `cancel_work `_ .. code-block:: c #include extern bool cancel_work(struct work_struct *work); * Here is an example of how to use the API, .. code-block:: c cancel_work(&my_work_1); cancel_work(&my_work_2); .. _check_workqueue_13: .. tab-set:: .. tab-item:: schedule_delayed_work * Here is the function prototype of the API: `schedule_delayed_work `_ .. code-block:: c #include static inline bool schedule_delayed_work(struct delayed_work *dwork, unsigned long delay); * Here is an example of how to use the API, .. code-block:: c schedule_delayed_work(&my_work,msecs_to_jiffies(100)); .. _check_workqueue_14: .. tab-set:: .. tab-item:: cancel_delayed_work * Here is the function prototype of the API: `cancel_delayed_work `_ .. code-block:: c #include extern bool cancel_delayed_work(struct delayed_work *dwork); * Here is an example of how to use the API, .. code-block:: c cancel_delayed_work(&my_work); .. _check_workqueue_18: .. tab-set:: .. tab-item:: Example 1 : Checking the workqueue * In this example let's see how to check the workqueue and execute it. .. _check_workqueue_19: .. 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 .. _check_workqueue_20: .. 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 work_pending"); .. _check_workqueue_21: .. tab-set:: .. tab-item:: Initializing work * Initialize the work which we are going to use in this example .. code-block:: c INIT_WORK(&my_work_1,workqueue_func_1); INIT_WORK(&my_work_2,workqueue_func_2); .. _check_workqueue_22: .. tab-set:: .. tab-item:: Workqueue Handler Function * This workqueue_func_1 function executes when work is queued to workqueue. .. code-block:: c void workqueue_func_1(struct work_struct *work) { int i = 0; while (i < 10) { pr_info("Inside workqueue_func_1\n"); i++; msleep(1000); } } * This workqueue_func_2 function executes when work is queued to workqueue. .. code-block:: c void workqueue_func_2(struct work_struct *work) { int i = 0; while (i < 10) { pr_info("Inside workqueue_func_1\n"); i++; msleep(1000); } } .. _check_workqueue_23: .. tab-set:: .. tab-item:: Queue Start Function * This function creates workqueue, intiializes work and queues to the workqueue. .. code-block:: c void queue_start(void) { my_workqueue = create_workqueue(WQ_NAME); INIT_WORK(&my_work_1,workqueue_func_1); INIT_WORK(&my_work_2,workqueue_func_2); if (!work_pending(&my_work_1)) { schedule_work(&my_work_1); queue_work(my_workqueue,&my_work_1); } if (!work_pending(&my_work_2)) { schedule_work(&my_work_2); queue_work(my_workqueue,&my_work_2); } } .. _check_workqueue_24: .. tab-set:: .. tab-item:: Queue Init Function * This function executes when module is loaded. .. code-block:: c static int __init queue_init(void) { pr_info("Inside queue_init function\n"); queue_start(); return 0; } .. _check_workqueue_25: .. tab-set:: .. tab-item:: Queue Stop Function * This function executes during execution of queue_exit, waits until the completion and destroys workqueue. .. code-block:: c void queue_stop(void) { pr_info("Destroying workqueue\n"); cancel_work(&my_work_1); cancel_work(&my_work_2); } .. _check_workqueue_26: .. tab-set:: .. tab-item:: Queue Exit Function * This function executes when module is unloaded. .. code-block:: c static void __exit queue_exit(void) { pr_info("Inside queue_exit function\n"); queue_stop(); return; } .. _check_workqueue_27: .. 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(queue_init); module_exit(queue_exit); .. _check_workqueue_28: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: tc1.c .. literalinclude:: check_workqueue/work_pending/tc1.c :language: c :linenos: .. _check_workqueue_29: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: check_workqueue/work_pending/Makefile :language: c :linenos: .. _check_workqueue_30: .. tab-set:: .. tab-item:: Compile and Load * Run make to load the kernel module. .. code-block:: shell $ make * Check if work_queue.ko is generated or not using ls command. .. code-block:: shell $ ls -l total 384 -rw-rw-r-- 1 test test 151 Aug 2 11:47 Makefile -rw-rw-r-- 1 test test 76 Aug 2 12:48 modules.order -rw-rw-r-- 1 test test 0 Aug 2 12:48 Module.symvers -rw-rw-r-- 1 test test 2040 Aug 2 11:47 tc1.c -rw-rw-r-- 1 test test 180944 Aug 2 12:48 tc1.ko -rw-rw-r-- 1 test test 76 Aug 2 12:48 tc1.mod -rw-rw-r-- 1 test test 1103 Aug 2 12:48 tc1.mod.c -rw-rw-r-- 1 test test 149280 Aug 2 12:48 tc1.mod.o -rw-rw-r-- 1 test test 33112 Aug 2 12:48 tc1.o * Load the module to kernel using insmod command. .. code-block:: shell $ sudo insmod ./tc1.ko * Check kernel messages to verify if the module is loaded or not. .. code-block:: shell $ dmesg [ 1812.726131] Inside queue_init function [ 1812.726612] Inside workqueue_func_2 [ 1812.726979] Inside workqueue_func_1 [ 1813.754717] Inside workqueue_func_1 [ 1814.778711] Inside workqueue_func_1 [ 1815.802723] Inside workqueue_func_1 [ 1816.826704] Inside workqueue_func_1 [ 1817.850705] Inside workqueue_func_1 [ 1818.874707] Inside workqueue_func_1 [ 1819.898722] Inside workqueue_func_1 [ 1820.922729] Inside workqueue_func_1 [ 1821.946714] Inside workqueue_func_1 * Unload the module using rmmod command. .. code-block:: shell $ sudo rmmod tc1 * Check kernel messages to see if the module is unloaded or not. .. code-block:: shell $ dmesg [ 1835.094347] Inside queue_exit function .. _check_workqueue_31: .. tab-set:: .. tab-item:: Example 2 : Checking the workqueue * In this example let's see how to check the workqueue and execute it. .. _check_workqueue_32: .. 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 .. _check_workqueue_33: .. 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 delayed_work_pending"); .. _check_workqueue_34: .. tab-set:: .. tab-item:: Initializing work * Initialize the work which we are going to use in this example .. code-block:: c INIT_WORK_ONSTACK(&my_work_1,workqueue_func_1); INIT_WORK_ONSTACK(&my_work_2,workqueue_func_2); .. _check_workqueue_35: .. tab-set:: .. tab-item:: Workqueue Handler Function * This workqueue_func_1 function executes when work is queued to workqueue. .. code-block:: c void workqueue_func_1(struct work_struct *work) { int i = 0; while (i < 10) { pr_info("Inside workqueue_func_1\n"); i++; msleep(1000); } } * This workqueue_func_2 function executes when work is queued to workqueue. .. code-block:: c void workqueue_func_2(struct work_struct *work) { int i = 0; while (i < 10) { pr_info("Inside workqueue_func_2\n"); i++; msleep(1000); } } .. _check_workqueue_36: .. tab-set:: .. tab-item:: Queue Start Function * This function creates workqueue, intiializes work and queues to the workqueue. .. code-block:: c void queue_start(void) { INIT_DELAYED_WORK(&my_work_1,workqueue_func_1); INIT_DELAYED_WORK(&my_work_2,workqueue_func_2); if (!delayed_work_pending(&my_work_1)) { pr_info("Delayed work is not scheduled yet\n"); schedule_delayed_work(&my_work_1,msecs_to_jiffies(500)); } schedule_delayed_work(&my_work_2,msecs_to_jiffies(500)); } .. _check_workqueue_37: .. tab-set:: .. tab-item:: Queue Init Function * This function executes when module is loaded. .. code-block:: c static int __init queue_init(void) { pr_info("Inside queue_init function\n"); queue_start(); return 0; } .. _check_workqueue_38: .. tab-set:: .. tab-item:: Queue Stop Function * This function executes during execution of queue_exit, waits until the completion and destroys workqueue. .. code-block:: c void queue_stop(void) { pr_info("Destroying workqueue\n"); cancel_delayed_work_sync(&my_work_1); cancel_delayed_work(&my_work_2); } .. _check_workqueue_39: .. tab-set:: .. tab-item:: Queue Exit Function * This function executes when module is unloaded. .. code-block:: c static void __exit queue_exit(void) { pr_info("Inside queue_exit function\n"); queue_stop(); return; } .. _check_workqueue_40: .. 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(queue_init); module_exit(queue_exit); .. _check_workqueue_41: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: tc2.c .. literalinclude:: check_workqueue/delayed_work_pending/tc2.c :language: c :linenos: .. _check_workqueue_42: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: check_workqueue/delayed_work_pending/Makefile :language: c :linenos: .. _check_workqueue_43: .. tab-set:: .. tab-item:: Compile and Load * Run make to load the kernel module. .. code-block:: shell $ make * Check if work_queue.ko is generated or not using ls command. .. code-block:: shell $ ls -l total 384 -rw-rw-r-- 1 test test 151 Aug 2 11:47 Makefile -rw-rw-r-- 1 test test 84 Aug 2 12:51 modules.order -rw-rw-r-- 1 test test 0 Aug 2 12:51 Module.symvers -rw-rw-r-- 1 test test 2356 Aug 2 11:47 tc2.c -rw-rw-r-- 1 test test 181992 Aug 2 12:51 tc2.ko -rw-rw-r-- 1 test test 84 Aug 2 12:51 tc2.mod -rw-rw-r-- 1 test test 1103 Aug 2 12:51 tc2.mod.c -rw-rw-r-- 1 test test 149296 Aug 2 12:51 tc2.mod.o -rw-rw-r-- 1 test test 34160 Aug 2 12:51 tc2.o * Load the module to kernel using insmod command. .. code-block:: shell $ sudo insmod ./tc2.ko * Check kernel messages to verify if the module is loaded or not. .. code-block:: shell $ dmesg [ 1927.976881] Inside queue_init function [ 1927.977802] Inside workqueue_func_1 [ 1927.977886] Inside workqueue_func_2 [ 1928.986617] Inside workqueue_func_2 [ 1928.986634] Inside workqueue_func_1 [ 1930.010693] Inside workqueue_func_2 [ 1930.010731] Inside workqueue_func_1 [ 1931.034722] Inside workqueue_func_2 [ 1931.034739] Inside workqueue_func_1 [ 1932.058735] Inside workqueue_func_2 [ 1932.058751] Inside workqueue_func_1 [ 1933.082732] Inside workqueue_func_2 [ 1933.082760] Inside workqueue_func_1 [ 1934.106692] Inside workqueue_func_1 [ 1934.106709] Inside workqueue_func_2 [ 1935.130737] Inside workqueue_func_2 [ 1935.130753] Inside workqueue_func_1 [ 1936.154741] Inside workqueue_func_1 [ 1936.154758] Inside workqueue_func_2 [ 1937.178738] Inside workqueue_func_2 [ 1937.178755] Inside workqueue_func_1 * Unload the module using rmmod command. .. code-block:: shell $ sudo rmmod tc2 * Check kernel messages to see if the module is unloaded or not. .. code-block:: shell $ dmesg [ 1955.215382] Inside queue_exit function