workqueue: Shrink the lock time
v3 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 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]
---
To: Tejun Heo <[email protected]>
To: Lai Jiangshan <[email protected]>
Cc: [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 | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-)
---
base-commit: 8d6dbbbe3ba62de0a63e962ee004afb848c8e3ac
change-id: 20260526-fastwake-02982fd66312
Best regards,
Signature · 1 line
Breno Leitao <[email protected]>
PATCH v3 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
PATCH v3 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 via raw_spin_unlock_wake(). 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 | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/workqueue.c +3 -2
@@ -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,14 +2422,14 @@ 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); } out: -raw_spin_unlock (&pool->lock); +raw_spin_unlock_wake (&pool->lock, &wakeq);rcu_read_unlock (); }
Signature · 1 line
2.53.0-Meta
Re: [PATCH v3 2/3] workqueue: defer the worker wakeup outside pool->lock in __queue_work()
Quoted 13 lines · Breno Leitao
__queue_work()is the enqueue hot path: it inserts the work item and callskick_pool()while holdingpool->lock.kick_pool()ends in a wakeup, which takes the target task'srq->lock, sorq->locknests underpool->lockon every enqueue that wakes a worker on a contended unbound pool.Use
kick_pool_pick()to select and claim the worker underpool->lock, queue it on an on-stackwake_q, and issue the wakeup withwake_up_q()right after dropping the lock viaraw_spin_unlock_wake(). Worker selection,wake_cpusetup and claiming the worker offpool->idle_liststill happen under the lock; only therq->lockacquisition moves out.Signed-off-by Breno Leitao <[email protected]>
…
Quoted 2 lines
kernel/workqueue.c +0
-0
…
kernel/workqueue.c static void __queue_work(int cpu, struct workqueue_struct *wq, · line 2434
Quoted 5 lines
out:
kernel/workqueue.c +1
-1-raw_spin_unlock (&pool->lock); +raw_spin_unlock_wake (&pool->lock, &wakeq);rcu_read_unlock (); }
It is not wrong but I am not sure if this is really needed here. The pattern
preempt_disable(); raw_spin_unlock(); wake_up_q() preempt_enable();
is used to prevent task preemption after the unlock operation. The futex/ locking code needs to wake a task but before the unlock operation the task priority might have been lowered as result of dropping the lock. This means it might not be the task with the highest priority in the system and the task with highest is not yet active and we schedule a task in middle in the instead.
To form this easier: say we have Task A prio=1, B prio=2 and C prio=3 with higher number higher priority.
A owns a lock and is preempted by B. C gets on the CPU preempts B. C wants A's lock so it passes its priority to A (PI-boost) and goes idle. A gets on the CPU and unlocks.
Now: As part of the unlock operation (before the raw_spin_unlock()) A goes back to its initial priority and C is not yet woken up meaning B is the task with highest priority and the held raw_spinlock_t is the only thing preventing scheduling. So we disable preemption, unlock and then perform the wake-up so C becomes the next candidate (as it should be) to occupy the CPU.
Otherwise it would be B which means per definition a task with lower priority runs before a task with higher priority.
I don't think workqueue has this requirements here. Worst case something else gets on the CPU and worker wakeup is delayed until the task is scheduled again. It could be fine since the preemption could happen before queue-work().
It probably does not lead to a huge performance
regression but who knows.
Your goal was to lower the contention on the pool lock during the wake up which you achieved. The __queue_work() remains still not preemptible until after the wake up which might be fine.
Sebastian
Re: [PATCH v3 2/3] workqueue: defer the worker wakeup outside pool->lock in __queue_work()
Hello Sebastian,
Quoted 58 lines · Sebastian Andrzej Siewior
Quoted 13 lines · Breno Leitao
__queue_work()is the enqueue hot path: it inserts the work item and callskick_pool()while holdingpool->lock.kick_pool()ends in a wakeup, which takes the target task'srq->lock, sorq->locknests underpool->lockon every enqueue that wakes a worker on a contended unbound pool.Use
kick_pool_pick()to select and claim the worker underpool->lock, queue it on an on-stackwake_q, and issue the wakeup withwake_up_q()right after dropping the lock viaraw_spin_unlock_wake(). Worker selection,wake_cpusetup and claiming the worker offpool->idle_liststill happen under the lock; only therq->lockacquisition moves out.Signed-off-by Breno Leitao <[email protected]>…
Quoted 2 lines
kernel/workqueue.c +0
-0…
kernel/workqueue.c static void __queue_work(int cpu, struct workqueue_struct *wq, · line 2434
Quoted 5 lines
out:
kernel/workqueue.c +1
-1-raw_spin_unlock (&pool->lock); +raw_spin_unlock_wake (&pool->lock, &wakeq);rcu_read_unlock (); }It is not wrong but I am not sure if this is really needed here. The pattern
preempt_disable(); raw_spin_unlock(); wake_up_q() preempt_enable();is used to prevent task preemption after the unlock operation. The futex/ locking code needs to wake a task but before the unlock operation the task priority might have been lowered as result of dropping the lock. This means it might not be the task with the highest priority in the system and the task with highest is not yet active and we schedule a task in middle in the instead.
To form this easier: say we have Task A prio=1, B prio=2 and C prio=3 with higher number higher priority.
A owns a lock and is preempted by B. C gets on the CPU preempts B. C wants A's lock so it passes its priority to A (PI-boost) and goes idle. A gets on the CPU and unlocks.
Now: As part of the unlock operation (before theraw_spin_unlock()) A goes back to its initial priority and C is not yet woken up meaning B is the task with highest priority and the heldraw_spinlock_tis the only thing preventing scheduling. So we disable preemption, unlock and then perform the wake-up so C becomes the next candidate (as it should be) to occupy the CPU.
Otherwise it would be B which means per definition a task with lower priority runs before a task with higher priority.I don't think workqueue has this requirements here. Worst case something else gets on the CPU and worker wakeup is delayed until the task is scheduled again. It could be fine since the preemption could happen before queue-work().
It probably does not lead to a huge performance
regression but who knows.
Quoted 3 lines
Your goal was to lower the contention on the pool lock during the wake up which you achieved. The
__queue_work()remains still not preemptible until after the wake up which might be fine.
You're absolutely right — the preemption dance isn't needed here.
I used raw_spin_unlock_wake() because the helper seemed to match what I was doing, but this preempt_disable/enable pattern (while cheap) is unnecessary for this use case.
I'll update the series to use the standard approach:
raw_spin_unlock_irq(&pool->lock); wake_up_q(&wakeq);
Thanks,
--breno
PATCH v3 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 via raw_spin_unlock_irq_wake().
--- kernel/workqueue.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
kernel/workqueue.c +3 -2
@@ -3250,6 +3250,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 ;
@@ -3304,7 +3305,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
@@ -3315,7 +3316,7 @@ __acquires(&pool->lock)set_work_pool_and_clear_pending (work, pool->id,pool_offq_flags (pool)); pwq->stats[PWQ_STAT_STARTED ]++; -raw_spin_unlock_irq (&pool->lock); +raw_spin_unlock_irq_wake (&pool->lock, &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.
…
Sebastian
Hello,
I tested v3 of this patch series on a 2-socket Intel Xeon Gold 6330 system (56 physical cores, 112 threads) using a buffered random write fio workload on a 4-NVMe RAID0 configuration.
Each socket contains a 42 MB LLC shared by 28 physical cores, and the tests were run with cache_shard_size=28 to align shards with the LLC topology.
fio configuration:
ioengine=libaio
rw=randwrite
bs=4k
iodepth=256
numjobs=224
runtime=60s
time_based=1
direct=0 (buffered I/O)
I compared:
- Default cache affinity
- Unpatched cache_shard (size=28)
- Patched cache_shard (size=28)
Lock contention and throughput metrics were collected using perf lock record across four independent runs of the workload, and the reported values are averages of those runs.
Average results:
Configuration Avg IOPS (k) Avg Context
Switches Avg Lock Wait Time
--------------------------------------------------------------------------------------------------------------------
Cache 191.5 1.54M
1.30 h
Unpatched cache_shard 189.8 1.60M
1.34 h
Patched cache_shard 191.8 1.51M
1.29 hThroughput remained very similar across all configurations, while the patched configuration showed slightly lower lock wait times and context-switch counts than the unpatched cache_shard configuration.
For this workload, I did not observe a measurable throughput improvement, but the patch appears to reduce some scheduler and locking overhead.
Thanks,
Krishna
Quoted 86 lines · Breno Leitao
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 inwake_up_process(), which takes the target task'srq->lock, sorq->locknests underpool->lockon the two hottest paths of a contended unbound workqueue (__queue_work()enqueue andprocess_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->locklengthens 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 thewake_qmachinery (wake_q_add()under the lock,wake_up_q()after).Because the win is a shorter
pool->lockhold 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->locklengthens 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 v3:
- Drop the "park kicked worker onpool->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 theidle_cull_fn()vs kicked-worker race by parking the kicked worker on a newpool->kicked_listunderpool->lock(new patch 1). Reported by Hillf Danton.
- Use thewake_qmachinery (wake_q_add()/wake_up_q()viaraw_spin_unlock_wake()) instead of plumbing atask_structout of the helper by hand. Suggested by Sebastian Andrzej Siewior.
- Link to v1: https://lore.kernel.org/r/[email protected]Signed-off-by Breno Leitao <[email protected]>---
To: Tejun Heo <[email protected]>
To: Lai Jiangshan <[email protected]>
Cc: [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 | 42 +++++++++++++++++++++++++++++++++--------- 1 file changed, 33 insertions(+), 9 deletions(-)---
base-commit:8d6dbbbe3ba62de0a63e962ee004afb848c8e3ac
change-id: 20260526-fastwake-02982fd66312Best regards,
--
Breno Leitao <[email protected]>