Day82 - kthread Internals¶
Objective¶
Learn how Linux kernel threads are created, started, stopped, and managed internally.
Implement a userspace simulation of:
kthread_create()wake_up_process()kthread_run()kthread_should_stop()kthread_stop()
Extend the framework with:
kthread_workerkthread_work
and understand how Linux builds higher-level deferred execution frameworks on top of kernel threads.
Lab Architecture¶
kthread Framework¶
Execution flow:
TLS Current Task Simulation¶
Implemented a userspace simulation of Linux current.
Helper APIs:
This allows:
to operate without requiring a task pointer.
kthread_worker Framework¶
kthread_work
├── func
├── next
├── pending
└── running
↓
kthread_worker
├── task
├── head
├── tail
├── active_works
├── running
├── lock
├── not_empty
└── work_done
Execution flow:
Lab1 - kthread_create() and wake_up_process()¶
Goal¶
Understand the separation between:
- Thread creation
- Thread execution
Implemented APIs¶
Test Flow¶
Expected Output¶
[TEST] task created but not running yet
[TEST] wake_up_process ret=0
[THREAD] run: hello from kthread
[TEST] kthread_stop ret=0
Key Learning¶
kthread_create() allocates and initializes a thread but does not start execution.
wake_up_process() starts the thread.
Lab2 - kthread_run()¶
Goal¶
Implement a convenience wrapper.
Implemented API¶
Implementation concept:
Test Flow¶
Expected Output¶
Key Learning¶
Most Linux drivers use:
instead of manually calling:
Lab3 - Cooperative Thread Stop¶
Goal¶
Implement cooperative thread termination.
Implemented APIs¶
Thread Loop¶
Test Flow¶
Expected Output¶
Key Learning¶
Linux does not forcibly terminate a thread.
Threads must periodically check:
and exit voluntarily.
Lab4 - TLS Current Task Simulation¶
Goal¶
Simulate Linux current.
Implementation¶
Thread entry:
Updated API:
Test Flow¶
Create two threads:
Print TLS task pointer.
Expected Output¶
[THREAD-1] task=0x11111111
[THREAD-2] task=0x22222222
[THREAD-1] task=0x11111111
[THREAD-2] task=0x22222222
Key Learning¶
TLS provides independent storage for each thread.
This closely resembles Linux's current task concept.
Lab5 - kthread_worker Fundamentals¶
Goal¶
Implement a dedicated worker thread.
Implemented APIs¶
Test Flow¶
Queue multiple work items:
Expected Output¶
[WORK-1] execute tid=135119002138304
[WORK-1] done
[WORK-2] execute tid=135119002138304
[WORK-2] done
[WORK-3] execute tid=135119002138304
[WORK-3] done
Key Learning¶
All work items execute on the same worker thread.
Unlike workqueues:
Lab6 - Worker Synchronization¶
Goal¶
Implement synchronization and lifecycle handling.
Implemented APIs¶
Case1 - Duplicate Pending Work¶
Test¶
Expected¶
Case2 - Duplicate Running Work¶
Test¶
Queue a work item and requeue it while running.
Expected¶
Case3 - flush_kthread_worker()¶
Test¶
Expected¶
Key Learning¶
Flush waits for active work completion.
Case4 - Stop Drains Pending Works¶
Test¶
Queue:
Immediately stop worker.
Expected¶
Key Learning¶
Worker stop drains queued work before thread termination.
Summary¶
kthread APIs¶
Implemented:
kthread_create()
wake_up_process()
kthread_run()
kthread_should_stop()
kthread_stop()
kthread_destroy()
kthread_worker APIs¶
Implemented:
init_kthread_work()
kthread_worker_init()
queue_kthread_work()
flush_kthread_worker()
kthread_worker_stop()
Key Concepts¶
- Dedicated kernel thread lifecycle
- Cooperative thread termination
- TLS-based current task simulation
- Dedicated worker thread architecture
- Ordered work execution
- Flush synchronization
- Worker lifecycle management
Comparison¶
| Item | Workqueue | kthread_worker |
|---|---|---|
| Worker Threads | Worker Pool | Dedicated Thread |
| Execution Order | Not Guaranteed | Guaranteed |
| Thread Ownership | Kernel Managed | Driver Managed |
| State Machine Usage | Limited | Common |
| Long-lived Services | Uncommon | Common |
Next Step¶
Day83 - Deferred Work Architecture and Selection