Work item types
work_func_t has the signature:
work_struct pointer using container_of:
Declaring and initializing work items
Static declaration
Dynamic initialization
Pointer to the work item to initialize.
The function to execute. Signature:
void fn(struct work_struct *work).Scheduling work
schedule_work
work on the global system_wq workqueue for execution on the current CPU. Returns true if the work was successfully queued, false if it was already pending.
schedule_delayed_work
dwork after delay jiffies. If delay is 0, the work executes immediately. Returns true if queued, false if already pending.
Delay in jiffies before the work is executed. Use
msecs_to_jiffies(ms) or usecs_to_jiffies(us) for time-based delays.queue_work
work on a specific workqueue wq rather than the global system_wq. Returns true if queued, false if already pending.
Target workqueue. Use a system workqueue or one created with
alloc_workqueue.queue_delayed_work
Flushing and canceling work
flush_work
work has finished executing. If work is currently pending (not yet started), it is allowed to start and then waited upon. Returns true if the work was pending, false if it had already completed.
cancel_work_sync
work and wait for any currently executing instance to finish. After this returns, work is guaranteed not to be running and will not run unless re-queued. Returns true if the work was pending.
cancel_delayed_work_sync
flush_workqueue
wq have finished. Work items queued after this call begins are not waited on.
Creating custom workqueues
alloc_workqueue
printf-style name for the workqueue, also used for the rescuer thread.Workqueue behavior flags (see below).
Maximum number of concurrently executing work items per CPU. Specify
0 to use the default (1024). Use alloc_ordered_workqueue when strict serial execution is required.Workqueue flags
WQ_UNBOUND
WQ_UNBOUND
Work items are not bound to any specific CPU. Workers are managed by special unbound worker pools. Useful when work is long-running or CPU-intensive and should be scheduled freely by the system scheduler.
WQ_HIGHPRI
WQ_HIGHPRI
Work items are queued to the high-priority worker pool. Workers run with elevated scheduling priority (lower nice value). Use for latency-sensitive work.
WQ_FREEZABLE
WQ_FREEZABLE
The workqueue participates in system suspend freeze operations. Work items are drained and no new items execute until the system thaws. Use for work associated with user-facing activity that should not run during suspend.
WQ_MEM_RECLAIM
WQ_MEM_RECLAIM
Required for any workqueue that may be used in memory reclaim paths. Guarantees at least one execution context is available regardless of memory pressure, via a reserved rescue worker.
WQ_CPU_INTENSIVE
WQ_CPU_INTENSIVE
Work items do not contribute to the concurrency level tracked by the worker pool. CPU-intensive work items do not prevent other items in the same pool from starting. Use for work that is expected to consume many CPU cycles.
WQ_BH
WQ_BH
BH workqueues execute in softirq context on the queueing CPU. All BH work items execute in queueing order. BH work items cannot sleep. Must use
max_active = 0. Only WQ_HIGHPRI is permitted as an additional flag.destroy_workqueue
wq and free all associated resources. Implicitly calls flush_workqueue before freeing. Call this from module exit or device remove.
System workqueues
For work items that do not require special isolation, use one of the pre-allocated system workqueues. There is no performance difference between a dedicated workqueue and a system workqueue.| Workqueue | Description |
|---|---|
system_wq | Bound, normal priority. General-purpose. Equivalent to schedule_work(). |
system_highpri_wq | Bound, high priority. For latency-sensitive work. |
system_long_wq | Bound, normal priority. For work that may run for a long time. |
system_unbound_wq | Unbound. For work items that benefit from scheduler freedom. |
system_freezable_wq | Bound, freezable. Participates in system suspend. |
system_power_efficient_wq | Bound or unbound depending on workqueue.power_efficient. |
Worker thread lifetime
The cmwq manages worker pools automatically:Work queued
A work item is appended to the shared worklist of the target worker pool (determined by CPU affinity and priority).
Worker awakened
If no worker is currently running on the CPU, a sleeping worker is woken. If all workers are busy and a new context is needed, a new worker thread is created.
Work executes
The worker calls the work function. While executing, the worker is considered active and counts toward the pool’s concurrency level.
ps output as [kworker/CPU:ID] for bound workers or [kworker/uN:ID] for unbound workers.
Non-reentrance guarantee
A work item is guaranteed not to be re-entrant (executing on multiple CPUs simultaneously) provided:- The work function pointer has not been changed.
- The work item has not been queued to a different workqueue.
- The work item has not been re-initialized with
INIT_WORK.
