Destroy Work ============ .. 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:`Example 1 : Destroying 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 : Destroying 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 3 : Destroying 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 : Destroying 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 ` .. _destroy_work_0: .. tab-set:: .. tab-item:: Version Info =============================== ======================================= # Version =============================== ======================================= Ubuntu Ubuntu 22.10 Kernel 6.8.0 =============================== ======================================= .. _destroy_work_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 `_ .. _destroy_work_2: .. tab-set:: .. tab-item:: Explanation of workqueue APIs .. _destroy_work_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); .. _destroy_work_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); .. _destroy_work_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); .. _destroy_work_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); .. _destroy_work_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); .. _destroy_work_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); .. _destroy_work_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); .. _destroy_work_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); .. _destroy_work_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); .. _destroy_work_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); .. _destroy_work_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)); .. _destroy_work_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); .. _destroy_work_18: .. tab-set:: .. tab-item:: Example 1 : Destroying the workqueue * In this example let's see how to destroy the workqueue and execute it. .. _destroy_work_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 .. _destroy_work_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 cancel_workqueue"); .. _destroy_work_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); .. _destroy_work_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); } } .. _destroy_work_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)) { queue_work(my_workqueue,&my_work_1); } if (!work_pending(&my_work_2)) { queue_work(my_workqueue,&my_work_2); } } .. _destroy_work_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; } .. _destroy_work_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); destroy_workqueue(my_workqueue); } .. _destroy_work_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; } .. _destroy_work_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); .. _destroy_work_28: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: tc1.c .. literalinclude:: destroy_work/cancel_work/tc1.c :language: c :linenos: .. _destroy_work_29: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: destroy_work/cancel_work/Makefile :language: c :linenos: .. _destroy_work_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 .. _destroy_work_31: .. tab-set:: .. tab-item:: Example 2 : Destroying the workqueue * In this example let's see how to destroy the workqueue and execute it. .. _destroy_work_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 .. _destroy_work_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 flush_work"); .. _destroy_work_34: .. 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); .. _destroy_work_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_1\n"); i++; msleep(1000); } } .. _destroy_work_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) { 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)) { queue_work(my_workqueue,&my_work_1); } if (!work_pending(&my_work_2)) { queue_work(my_workqueue,&my_work_2); } } .. _destroy_work_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; } .. _destroy_work_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"); flush_work(&my_work_1); flush_work(&my_work_2); destroy_workqueue(my_workqueue); } .. _destroy_work_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; } .. _destroy_work_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); .. _destroy_work_41: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: tc2.c .. literalinclude:: destroy_work/flush_work/tc2.c :language: c :linenos: .. _destroy_work_42: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: destroy_work/flush_work/Makefile :language: c :linenos: .. _destroy_work_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 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 tc2.c -rw-rw-r-- 1 test test 180944 Aug 2 12:48 tc2.ko -rw-rw-r-- 1 test test 76 Aug 2 12:48 tc2.mod -rw-rw-r-- 1 test test 1103 Aug 2 12:48 tc2.mod.c -rw-rw-r-- 1 test test 149280 Aug 2 12:48 tc2.mod.o -rw-rw-r-- 1 test test 33112 Aug 2 12:48 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 [ 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 tc2 * Check kernel messages to see if the module is unloaded or not. .. code-block:: shell $ dmesg [ 1835.094347] Inside queue_exit function .. _destroy_work_44: .. tab-set:: .. tab-item:: Example 3 : Destroying the workqueue * In this example let's see how to destroy workqueue and execute it. .. _destroy_work_45: .. 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 .. _destroy_work_46: .. 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 flush_delayed_work"); .. _destroy_work_47: .. tab-set:: .. tab-item:: Initializing work * Initialize the work which we are going to use in this example .. code-block:: c INIT_DELAYED_WORK(&my_work_1,workqueue_func_1); INIT_DELAYED_WORK(&my_work_2,workqueue_func_2); .. _destroy_work_48: .. 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); } } .. _destroy_work_49: .. 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_DELAYED_WORK(&my_work_1,workqueue_func_1); INIT_DELAYED_WORK(&my_work_2,workqueue_func_2); queue_delayed_work(my_workqueue,&my_work_1,msecs_to_jiffies(100)); queue_delayed_work(my_workqueue,&my_work_2,msecs_to_jiffies(300)); } .. _destroy_work_50: .. 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; } .. _destroy_work_51: .. 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) { flush_delayed_work_sync(&my_work_1); flush_delayed_work_sync(&my_work_2); pr_info("Destroying workqueue\n"); destroy_workqueue(my_workqueue); } .. _destroy_work_52: .. 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; } .. _destroy_work_53: .. 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); .. _destroy_work_54: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: tc3.c .. literalinclude:: destroy_work/flush_delayed_work/tc3.c :language: c :linenos: .. _destroy_work_55: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: destroy_work/flush_delayed_work/Makefile :language: c :linenos: .. _destroy_work_56: .. 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 388 -rw-rw-r-- 1 test test 151 Aug 2 11:47 Makefile -rw-rw-r-- 1 test test 84 Aug 2 12:42 modules.order -rw-rw-r-- 1 test test 0 Aug 2 12:42 Module.symvers -rw-rw-r-- 1 test test 2390 Aug 2 11:47 tc3.c -rw-rw-r-- 1 test test 184616 Aug 2 12:42 tc3.ko -rw-rw-r-- 1 test test 84 Aug 2 12:42 tc3.mod -rw-rw-r-- 1 test test 1233 Aug 2 12:42 tc3.mod.c -rw-rw-r-- 1 test test 149488 Aug 2 12:42 tc3.mod.o -rw-rw-r-- 1 test test 36616 Aug 2 12:42 tc3.o * Load the module to kernel using insmod command. .. code-block:: shell $ sudo insmod ./tc3.ko * Check kernel messages to verify if the module is loaded or not. .. code-block:: shell $ dmesg [282169.096177] Inside queue_init function [282169.198261] Inside workqueue_func_1 [282170.218283] Inside workqueue_func_1 [282171.242140] Inside workqueue_func_1 [282172.266278] Inside workqueue_func_1 [282173.290254] Inside workqueue_func_1 [282174.314224] Inside workqueue_func_1 [282175.338284] Inside workqueue_func_1 [282176.362292] Inside workqueue_func_1 [282177.386297] Inside workqueue_func_1 [282178.410281] Inside workqueue_func_1 [282179.434217] Inside workqueue_func_2 [282180.458275] Inside workqueue_func_2 [282181.482260] Inside workqueue_func_2 [282182.506300] Inside workqueue_func_2 [282183.530275] Inside workqueue_func_2 [282184.554217] Inside workqueue_func_2 [282185.578258] Inside workqueue_func_2 [282186.602256] Inside workqueue_func_2 [282187.626266] Inside workqueue_func_2 [282188.650265] Inside workqueue_func_2 * Unload the module using rmmod command. .. code-block:: shell $ sudo rmmod tc3 * Check kernel messages to see if the module is unloaded or not. .. code-block:: shell $ dmesg [282192.532346] Inside queue_exit function [282192.532350] Destroying workqueue .. _destroy_work_57: .. tab-set:: .. tab-item:: Example 4 : Destroying the workqueue * In this example let's see how to create workqueue and execute it. .. _destroy_work_58: .. 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 .. _destroy_work_59: .. 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 flush_rcu_work"); .. _destroy_work_60: .. tab-set:: .. tab-item:: Initializing work * Initialize the work which we are going to use in this example .. code-block:: c INIT_RCU_WORK(&my_work,workqueue_func); .. _destroy_work_61: .. tab-set:: .. tab-item:: Workqueue Handler Function * This 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); } } .. _destroy_work_62: .. 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_RCU_WORK(&my_work,workqueue_func); queue_rcu_work(my_workqueue,&my_work); } .. _destroy_work_63: .. 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; } .. _destroy_work_64: .. 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"); flush_rcu_work(&my_work); flush_workqueue(my_workqueue); } .. _destroy_work_65: .. 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; } .. _destroy_work_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(queue_init); module_exit(queue_exit); .. _destroy_work_67: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: tc4.c .. literalinclude:: destroy_work/flush_rcu_work/tc4.c :language: c :linenos: .. _destroy_work_68: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: destroy_work/flush_rcu_work/Makefile :language: c :linenos: .. _destroy_work_69: .. 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 380 -rw-rw-r-- 1 test test 151 Aug 2 11:47 Makefile -rw-rw-r-- 1 test test 80 Aug 2 12:46 modules.order -rw-rw-r-- 1 test test 0 Aug 2 12:46 Module.symvers -rw-rw-r-- 1 test test 1715 Aug 2 11:47 tc4.c -rw-rw-r-- 1 test test 180448 Aug 2 12:46 tc4.ko -rw-rw-r-- 1 test test 80 Aug 2 12:46 tc4.mod -rw-rw-r-- 1 test test 1104 Aug 2 12:46 tc4.mod.c -rw-rw-r-- 1 test test 149288 Aug 2 12:46 tc4.mod.o -rw-rw-r-- 1 test test 32624 Aug 2 12:46 tc4.o * Load the module to kernel using insmod command. .. code-block:: shell $ sudo insmod ./tc4.ko * Check kernel messages to verify if the module is loaded or not. .. code-block:: shell $ dmesg [282424.157821] Inside queue_init function [282424.202296] Inside workqueue_func [282425.226337] Inside workqueue_func [282426.250143] Inside workqueue_func [282427.274333] Inside workqueue_func [282428.298179] Inside workqueue_func [282429.322313] Inside workqueue_func [282430.346329] Inside workqueue_func [282431.370333] Inside workqueue_func [282432.394338] Inside workqueue_func [282433.418338] Inside workqueue_func * Unload the module using rmmod command. .. code-block:: shell $ sudo rmmod tc4 * Check kernel messages to see if the module is unloaded or not. .. code-block:: shell $ dmesg [282439.203573] Inside queue_exit function [282439.203578] Destroying workqueue