How the Linux Workqueue Subsystem Works: Worker Pools, max_active, and Why Deferred Work Runs in Process Context

A driver that finishes an interrupt handler cannot allocate memory, take a mutex, or call into the block layer. The interrupt context is atomic. The work that must still happen—updating a device state machine, issuing a follow-up I/O, or notifying userspace—is placed on a workqueue and runs later inside a kernel thread. That thread is a kworker.

Related reading: How Linux Interrupts Work, How the Linux Context Switch Works, and How Linux CFS Works.

The contract

A work item is a struct work_struct that holds a function pointer and is linked into a queue. When the function must run in process context (it may sleep, take locks, or allocate with GFP_KERNEL), the caller queues it. The kernel guarantees that the function will eventually execute on a kworker thread, that the same work item is not executed concurrently with itself, and that flush_work() or flush_workqueue() waits until previously queued items have finished.

The modern implementation is Concurrency Managed Workqueue (cmwq). User-facing workqueues no longer own their own threads. They are domains that impose attributes (priority, locality, max concurrency, reclaim guarantees) on work items. Shared per-CPU worker pools execute the items.

Worker pools and kworkers

Each possible CPU has two worker pools: one normal and one high-priority. Unbound workqueues are served by a dynamic set of additional pools whose workers are not pinned to a particular CPU. Workers appear in the process list as [kworker/0:1-events], [kworker/u8:0-writeback], and similar. Idle workers are retained for a while; new ones are created on demand when pending work would otherwise stall.

Concurrency management on bound pools hooks into the scheduler. The pool tracks how many of its workers are runnable. While at least one worker is runnable, new work is not started. When the last running worker sleeps, the pool immediately schedules another so the CPU does not sit idle with pending items. The result is enough concurrency to keep the pool progressing without a permanent thread per workqueue.

max_active and attributes

alloc_workqueue(name, flags, max_active) creates a domain. max_active (default 1024 when passed as 0) limits how many work items of that workqueue may be executing simultaneously per CPU. This is a throttle, not a thread count. Specifying 0 is the normal choice unless the caller deliberately wants to serialize or limit parallelism.

Important flags:

  • WQ_UNBOUND — workers not bound to the queueing CPU; useful for CPU-intensive or highly variable concurrency.
  • WQ_MEM_RECLAIM — reserves a rescuer thread so the workqueue can still make progress under memory pressure. Required for any work that may be queued from reclaim paths.
  • WQ_HIGHPRI — uses the high-priority pool (elevated nice).
  • WQ_CPU_INTENSIVE — the work does not count toward the pool’s concurrency limit, so long-running items do not starve other work on the same pool.
  • WQ_BH — executes in softirq context instead of a thread (cannot sleep).

Ordered (single-threaded) behavior is obtained with alloc_ordered_workqueue(), not by setting max_active = 1 on an unbound queue.

Queuing, flushing, and cancellation

queue_work() or schedule_work() links the item into the appropriate pool’s worklist and wakes a worker if needed. The same work item cannot be queued twice; a second queue_work() while it is already pending or running returns false.

flush_work() waits for one specific item to finish. flush_workqueue() waits for every item that was already queued at the moment of the call (new items queued after the flush starts do not delay it). cancel_work_sync() removes a pending item and waits if it is already running. These are the tools used in device shutdown and module exit paths.

Why process context matters

Softirqs and tasklets (covered in the interrupts article) run in atomic context. They cannot sleep. Workqueues exist so that the deferred portion of interrupt handling, writeback, or asynchronous device operations can use the full process-context API: mutexes, GFP_KERNEL allocations, blocking I/O, and the scheduler. The price is an extra scheduling hop and the memory occupied by kworker stacks. For short, non-sleeping work the softirq path remains cheaper; for anything that may block, a workqueue is required.

Misconceptions

“A workqueue is a thread pool I own.” After cmwq the workqueue is only a domain. Threads belong to shared pools. Creating many workqueues does not create many threads.

“max_active = 1 makes the queue single-threaded.” On modern kernels use alloc_ordered_workqueue() for strict ordering.

“I can queue reclaim-path work on any workqueue.” Without WQ_MEM_RECLAIM the pool may be unable to create a worker under memory pressure and the system deadlocks.

Takeaways

Workqueues move work from atomic context into process context by handing a function pointer to a shared pool of kworker threads. Concurrency is regulated by the pool (for bound queues) and by the per-workqueue max_active limit. Forward progress under memory pressure requires the reclaim flag and its rescuer. Flush and cancel operations exist so that shutdown paths can wait for or discard previously queued work. The mechanism is deliberately minimal: enough threads to keep pending work moving, no more.

See also the kernel documentation at Documentation/core-api/workqueue.rst.

Previous Post