Day81 - Workqueue Internals¶
Goal¶
Understand how Linux Workqueue manages worker threads, work lifecycle, synchronization, and concurrency internally.
Lab1 - System Workqueue¶
Objective¶
Implement a global system workqueue and support:
workqueue_system_init()workqueue_system_exit()schedule_work()
Implementation¶
Implemented a global workqueue instance:
Added the following APIs:
Implemented the relationship:
Test Result¶
[TEST] schedule_work work1 ret=0
[TEST] schedule_work work2 ret=0
[WORK] run on system_wq
[WORK] run on system_wq
Key Observation¶
Linux drivers commonly use:
instead of creating dedicated workqueues.
The global system workqueue is shared among many drivers.
Lab2 - Worker Pool¶
Objective¶
Convert the workqueue from a single-worker model to a worker-pool model.
Implementation¶
Added worker pool support:
Added worker object:
Updated workqueue:
Each worker waits on the same queue and dequeues work items concurrently.
Test Result¶
[INFO] [WORKER-1] start
[INFO] [WORKER-2] start
[INFO] [WORKER-3] start
[INFO] [WORKER-4] start
[WORK-1] start
[WORK-2] start
[WORK-3] start
[WORK-4] start
[WORK-1] done
[WORK-2] done
[WORK-3] done
[WORK-4] done
Key Observation¶
The framework evolved from:
to:
Multiple work items can execute concurrently.
Lab3 - Work Lifecycle¶
Objective¶
Track work states and prevent duplicate scheduling.
Implementation¶
Added state tracking:
Updated scheduling logic:
State Machine¶
Test Result¶
[TEST] #1 schedule_work, ret=0
[TEST] #2 schedule_work, ret=-17
[WORK] done
[TEST] #3 schedule_work, ret=0
Key Observation¶
The framework no longer checks whether a work item exists in the queue.
Instead, scheduling decisions are based on the lifecycle state of the work item itself.
Lab4 - Work Synchronization APIs¶
Objective¶
Implement synchronization APIs for work completion and cancellation.
Implementation¶
Added work completion synchronization:
Added active work accounting:
Implemented:
flush_work()¶
Wait until a specific work item becomes idle.
flush_workqueue()¶
Wait until all pending and running work items complete.
Implemented using:
cancel_work_sync()¶
Behavior:
| Work State | Result |
|---|---|
| PENDING | Remove from queue |
| RUNNING | Wait for completion |
| IDLE | Return immediately |
Test Result¶
Running work:
Pending work:
schedule work1
schedule work2
schedule work3
schedule work4
schedule work5
cancel_work_sync(work5)
cancelled=1
Verified:
never appeared.
Key Observation¶
flush_work() waits for a specific work item.
flush_workqueue() waits for the entire workqueue to become idle.
cancel_work_sync() combines cancellation and synchronization semantics.
Summary¶
Implemented:
- System Workqueue
- Worker Pool
- Work Lifecycle Management
flush_work()flush_workqueue()cancel_work_sync()
Added:
pendingrunningactive_workswork_done
Verified:
- Shared system workqueue
- Concurrent worker execution
- Work lifecycle tracking
- Duplicate scheduling prevention
- Work completion synchronization
- Pending work cancellation
Next Step¶
Day82 - 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