Day 10: Using Workqueues & Tasklets – Deferred Work Handling in Kernel Space ⚡🛠️


1️⃣ Why Use Deferred Work in Kernel Space?

🔹 ISRs must execute quickly and return control to the CPU.

🔹 Some operations cannot be performed inside an ISR (e.g., memory allocation, I/O).

🔹 Solution: Use Tasklets or Workqueues to process data outside the ISR.


Article content

2️⃣ Using Tasklets for Quick Deferred Execution

🔹 Tasklets are lightweight functions that execute in SoftIRQ context (not preemptible).

🔹 They run after the ISR completes, but before returning to user space.

🔹 Tasklets must not sleep, allocate memory, or perform blocking I/O.

✅ Implementing a Tasklet in a Kernel Module

📌 Step 1: Create a file tasklet_driver.c

#include <linux/module.h> 
#include <linux/interrupt.h> 

static struct tasklet_struct my_tasklet; 

// Tasklet Function 
void my_tasklet_func(unsigned long data) { 
       printk(KERN_INFO "Tasklet executed with data: %lu\n", data); 
} 

// Module Init: Initialize Tasklet 
static int __init tasklet_init(void) { 
      tasklet_init(&my_tasklet, my_tasklet_func, 1234);
      tasklet_schedule(&my_tasklet); 
      printk(KERN_INFO "Tasklet scheduled\n"); 
      return 0; 
} 

// Module Exit: Cleanup Tasklet 
static void __exit tasklet_exit(void) { 
      tasklet_kill(&my_tasklet); 
      printk(KERN_INFO "Tasklet removed\n"); 
} 

module_init(tasklet_init); 
module_exit(tasklet_exit); 

MODULE_LICENSE("GPL"); 
MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Tasklet Example");        

📌 Step 2: Create a Makefile

obj-m += tasklet_driver.o 

KDIR := /lib/modules/$(shell uname -r)/build 
PWD := $(shell pwd) 

all: 
     make -C $(KDIR) M=$(PWD) modules 

clean: 
     make -C $(KDIR) M=$(PWD) clean        

📌 Step 3: Compile & Load the Module

make 
sudo insmod tasklet_driver.ko        

📌 Step 4: Check Kernel Logs

dmesg | tail -10        

📌 Step 5: Remove the Module

sudo rmmod tasklet_driver        

Key Takeaways:

✔ Tasklets are used for quick deferred execution after an interrupt.

✔ Tasklets do not run in process context (cannot sleep/block).


3️⃣ Using Workqueues for Heavy Deferred Execution

🔹 Workqueues execute tasks in a kernel thread, allowing them to:

Sleep or block (unlike Tasklets)

Perform I/O operations (e.g., file reads/writes)

Process data in the background


✅ Implementing a Workqueue in a Kernel Module

📌 Step 1: Create a file workqueue_driver.c

#include <linux/module.h> 
#include <linux/workqueue.h> 
#include <linux/slab.h> 

static struct workqueue_struct *my_workqueue; 
static struct work_struct my_work; 

// Workqueue Function 
void my_work_func(struct work_struct *work) { 
       printk(KERN_INFO "Workqueue executed in process context\n"); 
} 

// Module Init: Initialize Workqueue 
static int __init workqueue_init(void) { 
      my_workqueue = create_singlethread_workqueue("my_wq"); 
      if (!my_workqueue) return -ENOMEM; 

      INIT_WORK(&my_work, my_work_func);
      queue_work(my_workqueue, &my_work); 
      printk(KERN_INFO "Workqueue scheduled\n"); 
      return 0; 
} 

// Module Exit: Cleanup Workqueue 
static void __exit workqueue_exit(void) {
      flush_workqueue(my_workqueue);
      destroy_workqueue(my_workqueue); 
      printk(KERN_INFO "Workqueue removed\n"); 
} 

module_init(workqueue_init); 
module_exit(workqueue_exit); 

MODULE_LICENSE("GPL"); 
MODULE_AUTHOR("Your Name"); MODULE_DESCRIPTION("Workqueue Example");        

📌 Step 2: Compile & Load the Module

make 
sudo insmod workqueue_driver.ko        

📌 Step 3: Check Kernel Logs

dmesg | tail -10        

📌 Step 4: Remove the Module

sudo rmmod workqueue_driver        

Key Takeaways:

✔ Workqueues run in process context, so they can sleep or perform I/O operations.

✔ Use workqueues for long-running tasks that shouldn't block interrupts.


4️⃣ When to Use Tasklets vs. Workqueues?


Article content

Use Tasklets for quick hardware processing tasks.

Use Workqueues for complex tasks requiring I/O or sleeping.


5️⃣ Debugging & Monitoring Deferred Work

📌 Check if a tasklet or workqueue is running:

cat /proc/interrupts | grep -i softirq        

📌 View kernel workqueues in real-time:

cat /sys/kernel/debug/workqueue        

📌 Manually schedule a workqueue for debugging:

echo 1 > /proc/sys/kernel/printk        

🔹 Summary of Today’s Lesson

Tasklets and Workqueues allow deferred execution in Linux kernel drivers

Tasklets run in SoftIRQ context and must not sleep/block

Workqueues run in process context and can sleep or perform I/O

Use Tasklets for quick hardware event processing, Workqueues for long tasks


📢 Stay tuned for Day 11: Spinlocks vs. Mutexes – Synchronization in Device Drivers! 🔄


🔗 Click the links below to begin learning more about Linux and device driver: ✇️


🎯 Develop your abilities now at https://lnkd.in/gJvJmVck

🎯 Learn about the industry at https://lnkd.in/gJvJmVck

🎯 Improve your technical discussion with experts from around the world at https://lnkd.in/gEtJmKnf

To view or add a comment, sign in

More articles by R.K. Williams

Others also viewed

Explore content categories