Day 81 - Workqueue Internals¶
Today's Goal¶
Understand how Linux implements Workqueue internally, including worker pools, concurrency management, work lifecycle control, and the relationship between Workqueue, kthread, and Threaded IRQ.
Topics Covered¶
Why Linux Uses Workqueue¶
Workqueue allows deferred work to execute in process context instead of interrupt context.
Benefits:
- Sleepable context
- Can use mutexes
- Can perform blocking operations
- Simplifies deferred processing
Typical flow:
System Workqueue¶
Linux provides a global workqueue that drivers can share.
Common APIs:
Instead of creating a dedicated workqueue:
The simulation framework was extended with:
Worker Pool¶
A workqueue is not limited to a single worker thread.
Modern Linux workqueues use worker pools:
Multiple workers can execute independent work items concurrently.
Work Lifecycle¶
A work item transitions through several states:
State tracking was implemented using:
This prevents duplicate scheduling of the same work item.
flush_work()¶
Wait until a specific work item completes.
Typical usage:
Useful during driver removal to ensure worker threads are no longer accessing resources.
flush_workqueue()¶
Wait until all pending and running work items in a workqueue complete.
The simulation framework tracks active work items using:
cancel_work_sync()¶
Cancel a pending work item or wait for a running work item to finish.
Behavior:
| State | Result |
|---|---|
| PENDING | Removed from queue |
| RUNNING | Wait for completion |
| IDLE | Return immediately |
Workqueue vs kthread¶
| Item | Workqueue | kthread |
|---|---|---|
| Thread creation | Kernel managed | Driver managed |
| Sleepable | Yes | Yes |
| Best for | Deferred work | Long-running tasks |
| Resource management | Simple | More flexible |
| Typical usage | Very common | Less common |
Labs Completed¶
Lab1 - System Workqueue¶
Implemented:
system_wqschedule_work()workqueue_system_init()workqueue_system_exit()
Verified:
Lab2 - Worker Pool¶
Converted the framework from:
to:
Implemented:
Each worker waits on the same workqueue and executes work items concurrently.
Verified that multiple work items execute in parallel.
Lab3 - Work Lifecycle¶
Added work state tracking:
Updated scheduling logic:
Verified:
- IDLE → queue succeeds
- PENDING → queue rejected
- RUNNING → queue rejected
- IDLE after completion → queue succeeds
Lab4 - Work Synchronization APIs¶
Implemented:
Added:
Verified:
- flush individual work item
- flush entire workqueue
- cancel pending work item
- wait for running work item
Key Takeaways¶
- Workqueue executes deferred work in process context.
- Linux commonly uses a shared system workqueue.
- Worker pools provide concurrency and scalability.
- Work lifecycle management is essential for correctness.
flush_work()andcancel_work_sync()are frequently used during driver shutdown.- Workqueue internally resembles a kernel-managed worker-thread framework.
Next Step¶
Day 82 - kthread Internals
Topics:
- Kernel Thread Architecture
- kthread_create()
- kthread_run()
- kthread_stop()
- kthread_should_stop()
- kthread Worker Pattern
- kthread_work
- Workqueue vs kthread Internals
- Driver Usage Patterns