Tasklet : One Shot ================== .. tab-set:: .. tab-item:: Tasklet : One Shot * In this program, you are going to learn .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to schedule the tasklet ? .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow * How to use below APIs ? * `DECLARE_TASKLET `_ * `tasklet_schedule `_ .. panels:: :container: container pb-4 :column: col-lg-12 p-2 :card: shadow Topics in this section, * :ref:`Key learnings in this program ` * :ref:`Explanation of program part by part ` * :ref:`List of headers ` * :ref:`Modules marco ` * :ref:`Initialize tasklet ` * :ref:`Module init function ` * :ref:`Module exit function ` * :ref:`Tasklet handler APIs ` * :ref:`Mention init and exit functions ` * :ref:`See the full program below ` * :ref:`Makefile ` * :ref:`Compile and Load ` * :ref:`Summary of Tasklet APIs ` * :ref:`Summary of miscellaneous APIs ` .. _p1_taskletOneShot_0: .. tab-set:: .. tab-item:: Key Learnings in this program * Tasklets are used to queue up work to be done at a later time. * ``DECLARE_TASKLET`` macro used to create the tasklet structure and assigns the parameters to that structure. For example, .. code-block:: c DECLARE_TASKLET(tasklet, (void * )my_tasklet_handler, 0); * ``tasklet_schedule`` Schedule a tasklet with a normal priority. If a tasklet has previously been scheduled (but not yet run), the new schedule will be silently discarded. For example, .. code-block:: c tasklet_schedule(&tasklet); .. _p1_taskletOneShot_4: .. tab-set:: .. tab-item:: Explanation of program part by part * Here is the explanation of the program part by part .. _p1_taskletOneShot_5: .. tab-set:: .. tab-item:: List of Headers * This contains the list of all .h files which needs to be included to refer to the APIs included in this program. .. code-block:: c #include #include #include #include #include .. _p1_taskletOneShot_6: .. tab-set:: .. tab-item:: Modules macro * Add the macro modules which lists out the information about the module such as license, author and description. .. code-block:: c MODULE_LICENSE("GPL"); MODULE_AUTHOR("Linux_usr"); MODULE_DESCRIPTION("Tasklet"); .. _p1_taskletOneShot_7: .. tab-set:: .. tab-item:: Initialize tasklet * Initialize tasklet and the tasklet handler APIs. .. code-block:: c void my_tasklet_handler(unsigned long data); DECLARE_TASKLET(tasklet, (void *)my_tasklet_handler, 0); .. _p1_taskletOneShot_8: .. tab-set:: .. tab-item:: Module init function * Add the definitions for the module init functions which contains the instructions what the module need to do once it is loaded. .. code-block:: c static int __init tasklet_init(void) { pr_info("Hello, world\n"); tasklet_schedule(&tasklet); return 0; } .. _p1_taskletOneShot_9: .. tab-set:: .. tab-item:: Module exit function * Add the definitions for the module exit function which contains the instructions which needs to executed once the module is unloaded from the kernel. .. code-block:: c static void __exit tasklet_exit(void) { pr_info("Goodbye, world\n"); } .. _p1_taskletOneShot_10: .. tab-set:: .. tab-item:: Tasklet handler APIs * Add the function definition of the tasklet handler API since this tasklet handler API is passed as an argument in ``DECLARE_TASKLET`` and will be executed once the ``tasklet_schedule`` is called. .. code-block:: c void my_tasklet_handler(unsigned long data) { pr_info("Tasklet function executed\n"); } .. _p1_taskletOneShot_11: .. tab-set:: .. tab-item:: Mention init and exit functions * Add the init and exit functions as a parameters to ``module_init`` and ``module_exit`` respectively. .. code-block:: c module_init(tasklet_init); module_exit(tasklet_exit); .. _p1_taskletOneShot_13: .. tab-set:: .. tab-item:: See the full program below .. tab-set:: .. tab-item:: tasklets.c .. literalinclude:: p1_taskletOneShot/tasklets.c :language: c :linenos: :emphasize-lines: 12, 22 .. _p1_taskletOneShot_1: .. tab-set:: .. tab-item:: Makefile .. literalinclude:: p1_taskletOneShot/Makefile :language: c :linenos: .. _p1_taskletOneShot_2: .. tab-set:: .. tab-item:: Compile and Load * Run make to compile the kernel module. .. code-block:: shell $make make -C /lib/modules/5.4.0-150-generic/build M=$HOME/kernel_tasklets modules make[1]: Entering directory '/usr/src/linux-headers-5.4.0-150-generic' CC [M] $HOME/kernel_tasklets/tasklets.o Building modules, stage 2. MODPOST 1 modules CC [M] $HOME/kernel_tasklets/tasklets.mod.o LD [M] $HOME/kernel_tasklets/tasklets.ko make[1]: Leaving directory '/usr/src/linux-headers-5.4.0-150-generic' * Check whether the tasklets.ko is generated or not using ls command. .. code-block:: shell $ ls -l total 36 -rw-rw-r-- 1 test test 154 Feb 26 13:18 Makefile -rw-rw-r-- 1 test test 47 Feb 26 13:18 modules.order -rw-rw-r-- 1 test test 0 Feb 26 13:18 Module.symvers -rw-rw-r-- 1 test test 820 Feb 26 13:18 tasklets.c -rw-rw-r-- 1 test test 5880 Feb 26 13:18 tasklets.ko -rw-rw-r-- 1 test test 47 Feb 26 13:18 tasklets.mod -rw-rw-r-- 1 test test 919 Feb 26 13:18 tasklets.mod.c -rw-rw-r-- 1 test test 3448 Feb 26 13:18 tasklets.mod.o -rw-rw-r-- 1 test test 3320 Feb 26 13:18 tasklets.o * Load the module using insmod command. .. code-block:: shell $ sudo insmod ./tasklets.ko * Check the kernel messages using dmesg to verify whether the module is loaded or not. .. code-block:: shell $ dmesg [ 982.336636] Hello, world [ 982.336644] Tasklet function executed * Unload the module from linux kernel using rmmod command. .. code-block:: shell $ sudo rmmod tasklets * Check the kernel messages to verify whether the module is unloaded or not. .. code-block:: shell $ dmesg [ 982.336636] Hello, world [ 982.336644] Tasklet function executed [ 989.128409] Goodbye, world .. _p1_taskletOneShot_3: .. tab-set:: .. tab-item:: Summary of Tasklet APIs =============================== ============================================ API Learning =============================== ============================================ DECLARE_TASKLET To create the tasklet structure tasklet_schedule To schedule a tasklet with a normal priority =============================== ============================================ .. _p1_taskletOneShot_12: .. tab-set:: .. tab-item:: Summary of miscellaneous APIs =============================== ============================================ API Learning =============================== ============================================ pr_info Prints an info-level message =============================== ============================================