workqueue: Shrink the lock time
v4 cover workqueue: Shrink the lock time
The goal of this patchset is to decrease the time spent under the workqueue pool->lock.
Currently the worker process is woken up inside pool->lock. The wakeup ends in wake_up_process(), which takes the target task's rq->lock, so rq->lock nests under pool->lock on the two hottest paths of a contended unbound workqueue (__queue_work() enqueue and process_one_work() chain kick). On some architectures the wakeup is even more expensive: on arm64 waking a CPU that is idle (in wfi) issues an IPI.
Doing all of that while holding pool->lock lengthens the locked region and hurts throughput on contended unbound pools.
This series shortens the locked region by selecting and claiming the worker to wake under pool->lock, but issuing the actual wakeup after the lock is dropped, using the wake_q machinery (wake_q_add() under the lock, wake_up_q() after).
Because the win is a shorter pool->lock hold time, it shows up most clearly as lower enqueue latency under contention.
Performance numbers (based on in-kernel workqueue microbenchmark)
VMs and arm64 (Grace) is where this series is meant to pay off -- waking an idle CPU sitting in wfi costs an IPI (on arm; similar type of operation on VMs), so doing it under pool->lock lengthens the critical section.
The arm64 bare-metal numbers match what the x86-or-arm64 VM showed:
affinity_scope baseline patched tput p95
(items/s) (items/s) gain drop
-------------- --------- --------- ------ ------
cpu 2,569,880 3,029,740 +17.9% -13.6%
smt 2,586,485 3,044,788 +17.7% -14.0%
cache_shard 572,055 797,621 +39.4% -37.1%
cache 538,132 724,997 +34.7% -30.1%
numa 528,673 658,215 +24.5% -20.5%
system 524,287 614,486 +17.2% -21.1%(p95 drop = change in p95 enqueue latency; negative is better.) (tput gain = number of requests enqueued per sec; bigger is better.)
Patch 1 is a pure refactor introducing kick_pool_pick(). Patch 2 defers the wakeup on the enqueue path (__queue_work()). Patch 3 defers the wakeup on the per-work chain-kick path (process_one_work()).
Changes in v4:
- replace raw_spin_unlock_wake() with a standard
raw_spin_unlock() + wake_up_q() (Sebastian Andrzej Siewior)
- Link to v3: https://lore.kernel.org/r/[email protected]
Changes in v3:
- Drop the "park kicked worker on pool->kicked_list" patch (v2 1/4).
* That is a fix that is independent of this patch, in case we want to revamp it, it can be sent separately.
- Link to v2: https://lore.kernel.org/r/[email protected]
Changes in v2:
- Close the idle_cull_fn() vs kicked-worker race by parking the kicked worker on a new pool->kicked_list under pool->lock (new patch 1). Reported by Hillf Danton.
- Use the wake_q machinery (wake_q_add() / wake_up_q() via raw_spin_unlock_wake()) instead of plumbing a task_struct out of the helper by hand. Suggested by Sebastian Andrzej Siewior.
- Link to v1: https://lore.kernel.org/r/[email protected]
---
Breno Leitao (3):
workqueue: split kick_pool() into kick_pool_pick() + wake_up_q()
workqueue: defer the worker wakeup outside pool->lock in __queue_work()
workqueue: defer the worker wakeup outside pool->lock in process_one_work()kernel/workqueue.c | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-)
---
base-commit: 8d6dbbbe3ba62de0a63e962ee004afb848c8e3ac
change-id: 20260526-fastwake-02982fd66312
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
PATCH v4 1/3 workqueue: split kick_pool() into kick_pool_pick() + wake_up_q()
Factor the worker selection out of kick_pool() into kick_pool_pick(), which picks and claims the worker under pool->lock but, instead of waking it, queues it on a caller-provided wake_q via wake_q_add(). The caller issues the wakeup later with wake_up_q(). wake_q_add() is safe under the lock (cmpxchg + get_task_struct()); only wake_up_q() takes rq->lock.
kick_pool() becomes a thin wrapper using a local wake_q, so all existing callers keep waking under pool->lock.
Pure refactor, no functional change.
--- kernel/workqueue.c | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-)
kernel/workqueue.c +27 -5
@@ -52,6 +52,7 @@#include <linux/uaccess.h> #include <linux/sched/isolation.h> #include <linux/sched/debug.h> +#include <linux/sched/wake_q.h> #include <linux/nmi.h> #include <linux/kvm_para.h> #include <linux/delay.h>
@@ -1258,13 +1259,17 @@ static void kick_bh_pool(struct worker_pool *pool) }/** -* kick_pool - wake up an idle worker if necessary +* kick_pool_pick - select and claim an idle worker, deferring the wakeup * @pool: pool to kick +* @wakeq: wake_q to queue the selected worker on * -* @pool may have pending work items. Wake up worker if necessary. Returns -* whether a worker was woken up. +* Like kick_pool() but queues the picked worker on @wakeq (wake_q_add()) +* instead of waking it, so the caller can wake_up_q(@wakeq) after dropping +* pool->lock. Returns whether a worker was selected. +* +* Must be called with @pool->lock held. */ -static bool kick_pool (struct worker_pool *pool) +static bool kick_pool_pick (struct worker_pool *pool,struct wake_q_head *wakeq) {struct worker *worker =first_idle_worker (pool);struct task_struct *p;
@@ -1310,10 +1315,27 @@ static bool kick_pool(struct worker_pool *pool) } }#endif -wake_up_process (p); +wake_q_add (wakeq, p);return true; } +/** +* kick_pool - wake up an idle worker if necessary +* @pool: pool to kick +* +* @pool may have pending work items. Wake up worker if necessary. Returns +* whether a worker was woken up. +*/ +static bool kick_pool (struct worker_pool *pool) +{ +DEFINE_WAKE_Q (wakeq); +bool kicked; + + kicked =kick_pool_pick (pool, &wakeq); +wake_up_q (&wakeq); +return kicked; +} +#ifdef CONFIG_WQ_CPU_INTENSIVE_REPORT /*
Signature · 1 line
2.53.0-Meta
Tejun Heo
maintainerRe: [PATCH v4 1/3] workqueue: split kick_pool() into kick_pool_pick() + wake_up_q()
kernel/workqueue.c static bool kick_pool(struct worker_pool *pool) · line 1318
Quoted 1 line · Breno Leitao
kernel/workqueue.c +1
-0+wake_q_add (wakeq, p);
This is two extra atomic ops for every work item scheduling. This isn't necessarily a deal braker but is this the only way to do this? Can't you just stash the task pointer, extend irq disabled region and wake under rcu protection?
Thanks.
Signature · 1 line
tejun
Re: [PATCH v4 1/3] workqueue: split kick_pool() into kick_pool_pick() + wake_up_q()
Quoted 8 lines · Tejun Heo
kernel/workqueue.c static bool kick_pool(struct worker_pool *pool) · line 1318
Quoted 1 line · Breno Leitao
kernel/workqueue.c +1
-0+wake_q_add (wakeq, p);This is two extra atomic ops for every work item scheduling. This isn't necessarily a deal braker but is this the only way to do this?
Can't you just stash the task pointer, extend irq disabled region and wake under rcu protection?
Agreed, that's a better approach. Thanks for the suggestion.
I have a working PoC implementing this. I'll clean it up and post an updated series.
Thanks,
--breno
PATCH v4 2/3 workqueue: defer the worker wakeup outside pool->lock in __queue_work()
In reply to Breno Leitao__queue_work() is the enqueue hot path: it inserts the work item and calls kick_pool() while holding pool->lock. kick_pool() ends in a wakeup, which takes the target task's rq->lock, so rq->lock nests under pool->lock on every enqueue that wakes a worker on a contended unbound pool.
Use kick_pool_pick() to select and claim the worker under pool->lock, queue it on an on-stack wake_q, and issue the wakeup with wake_up_q() right after dropping the lock. Worker selection, wake_cpu setup and claiming the worker off pool->idle_list still happen under the lock; only the rq->lock acquisition moves out.
--- kernel/workqueue.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/workqueue.c +3 -1
@@ -2299,6 +2299,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq, {struct pool_workqueue *pwq;struct worker_pool *last_pool, *pool; +DEFINE_WAKE_Q (wakeq);unsigned int work_flags;unsigned int req_cpu = cpu;
@@ -2421,7 +2422,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq,trace_workqueue_activate_work (work);insert_work (pwq, work, &pool->worklist, work_flags); -kick_pool (pool); +kick_pool_pick (pool, &wakeq); }else { work_flags |=WORK_STRUCT_INACTIVE ;insert_work (pwq, work, &pwq->inactive_works, work_flags);
@@ -2429,6 +2430,7 @@ static void __queue_work(int cpu, struct workqueue_struct *wq, out:raw_spin_unlock (&pool->lock); +wake_up_q (&wakeq);rcu_read_unlock (); }
Signature · 1 line
2.53.0-Meta
PATCH v4 3/3 workqueue: defer the worker wakeup outside pool->lock in process_one_work()
In reply to Breno Leitaoprocess_one_work() kicks the pool to chain execution of the remaining work items on WORKER_NOT_RUNNING pools (the UNBOUND and CPU_INTENSIVE ones), calling kick_pool() while holding pool->lock. As in the enqueue path, the wakeup pulls the target rq->lock in under pool->lock.
Use kick_pool_pick() to select and claim the worker under pool->lock and issue the wakeup with wake_up_q() after the lock is dropped.
--- kernel/workqueue.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
kernel/workqueue.c +3 -1
@@ -3251,6 +3251,7 @@ __acquires(&pool->lock) {struct pool_workqueue *pwq =get_work_pwq (work);struct worker_pool *pool = worker->pool; +DEFINE_WAKE_Q (wakeq);unsigned long work_data;int lockdep_start_depth, rcu_start_depth;bool bh_draining = pool->flags &POOL_BH_DRAINING ;
@@ -3305,7 +3306,7 @@ __acquires(&pool->lock)* chain execution of the pending work items for WORKER_NOT_RUNNING * workers such as the UNBOUND and CPU_INTENSIVE ones. */ -kick_pool (pool); +kick_pool_pick (pool, &wakeq);/* * Record the last pool and clear PENDING which should be the last
@@ -3317,6 +3318,7 @@ __acquires(&pool->lock) pwq->stats[PWQ_STAT_STARTED ]++;raw_spin_unlock_irq (&pool->lock); +wake_up_q (&wakeq); rcu_start_depth =rcu_preempt_depth (); lockdep_start_depth =lockdep_depth (current);
Signature · 1 line
2.53.0-Meta
Quoted 2 lines · Breno Leitao
The goal of this patchset is to decrease the time spent under the workqueue
pool->lock.
…
Quoted 1 line
Signed-off-by Breno Leitao <[email protected]>
Sebastian