Day 82 - kthread Internals¶
Date¶
2026-06-22
Objective¶
Learn how Linux kernel threads are created, started, stopped, and managed.
Understand the relationship between:
kthread_create()wake_up_process()kthread_run()kthread_stop()kthread_should_stop()
Build a userspace simulation of Linux kthread infrastructure and extend it with a simplified kthread_worker framework.
Topics Covered¶
Kernel Thread Fundamentals¶
Studied the purpose of kernel threads and how they differ from workqueues.
Key concepts:
- Dedicated execution context
- Long-lived background task
- Cooperative termination
- Driver-owned thread lifecycle
Thread Lifecycle¶
Studied the typical lifecycle of a Linux kernel thread.
kthread_create()
↓
CREATED
↓
wake_up_process()
↓
RUNNING
↓
kthread_stop()
↓
STOP_REQUESTED
↓
thread exits
↓
EXITED
A kernel thread is not forcefully terminated.
The thread must periodically check:
and exit cooperatively.
kthread APIs¶
Studied:
kthread_create()wake_up_process()kthread_run()kthread_should_stop()kthread_stop()
Relationship:
TLS-Based Current Task Simulation¶
Implemented a userspace simulation of Linux current.
Each thread owns an independent TLS copy.
Verified that multiple threads maintain independent task contexts without interfering with each other.
kthread_worker¶
Studied the architecture of kthread_worker and kthread_work.
Architecture:
Unlike workqueues, all queued work items execute on the same worker thread.
Typical use cases:
- State machines
- Ordered command processing
- Firmware update workflows
- Background driver services
Labs Completed¶
Lab1 - kthread_create() and wake_up_process()¶
Implemented:
kthread_create()wake_up_process()
Verified:
- Thread creation does not start execution
- Thread begins execution only after
wake_up_process()
Lab2 - kthread_run()¶
Implemented:
kthread_run()
Verified:
Lab3 - Cooperative Thread Stop¶
Implemented:
kthread_should_stop()kthread_stop()
Verified:
- Stop request propagation
- Cooperative thread termination
- Thread lifecycle handling
Lab4 - TLS Current Task Simulation¶
Implemented:
Verified:
- Independent task context per thread
- Multi-thread isolation
- Linux
currentconcept simulation
Lab5 - kthread_worker Fundamentals¶
Implemented:
kthread_workerkthread_workqueue_kthread_work()
Verified:
- Dedicated worker thread execution
- Ordered work processing
- Single-thread execution model
Example:
All work items executed on the same worker thread.
Lab6 - Worker Synchronization¶
Implemented:
- Pending state tracking
- Running state tracking
- Duplicate queue prevention
- Flush synchronization
- Worker stop handling
Verified:
- Duplicate pending work rejection
- Duplicate running work rejection
flush_kthread_worker()waits for active work completion- Worker stop drains pending work before exit
Framework Implemented¶
utils/kthread¶
Implemented:
kthread_create()wake_up_process()kthread_run()kthread_stop()kthread_should_stop()kthread_destroy()
Task structure:
struct task_struct
{
pthread_t thread;
bool started;
bool should_stop;
int (*threadfn)(void *data);
void *data;
char name[32];
};
utils/kthread_worker¶
Implemented:
init_kthread_work()kthread_worker_init()queue_kthread_work()flush_kthread_worker()kthread_worker_stop()
Worker structure:
kthread_worker
├── task
├── head
├── tail
├── active_works
├── running
├── lock
├── not_empty
└── work_done
Key Takeaways¶
- Kernel threads provide dedicated execution contexts.
kthread_run()is a convenience wrapper aroundkthread_create()andwake_up_process().- Thread termination is cooperative through
kthread_should_stop(). - TLS can be used to simulate Linux
current. kthread_workerexecutes queued work on a dedicated thread.- Workqueues and kthread workers solve similar problems but provide different execution guarantees.
Next Step¶
Day 83 - Deferred Work Architecture and Selection
Topics:
- Deferred execution models
- Threaded IRQ
- Workqueue
- kthread_worker
- Dedicated kthread
- Driver execution context selection
- Deferred work design patterns
Goal:
Understand how Linux drivers choose between threaded IRQs, workqueues, kthread workers, and dedicated kernel threads for deferred processing.