Skip to contentSkip to conversation

workqueue: Introduce a sharded cache affinity scope

PATCH Superseded Breno Leitao 3 participants 16 messages Mar 20 – Mar 27

Subsystems (5)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover workqueue: Introduce a sharded cache affinity scope

TL;DR: Some modern processors have many CPUs per LLC (L3 cache), and unbound workqueues using the default affinity (WQ_AFFN_CACHE) collapse to a single worker pool, causing heavy spinlock (pool->lock) contention. Create a new affinity (WQ_AFFN_CACHE_SHARD) that caps each pool at wq_cache_shard_size CPUs (default 8).

Changes from RFC:

* wq_cache_shard_size is in terms of cores (not vCPU). So, wq_cache_shard_size=8 means the pool will have 8 cores and their siblings, like 16 threads/CPUs if SMT=1

* Got more data:

- AMD EPYC: All means are within ~1 stdev of zero. The deltas are indistinguishable from noise. Shard scoping has no measurable effect regardless of shard size. This is justified due to AMD EPYC having 11 L3 domains and lock contention not being a problem:

- ARM: A strong, consistent signal. At shard sizes 8 and 16 the mean improvement is ~7% with relatively tight stdev (~1–2%) at write, meaning the gain is real and reproducible across all IO engines. Even shard size 4 shows a solid +3.5% with the tightest stdev (0.97%).

Reads: Small shard sizes (2, 4) show a slight regression of ~1.3–1.7% (low stdev, so consistent). Larger shard sizes (8, 16) flip to a modest +1.4% gain, though shard_size=8 reads have high variance (stdev 2.79%) driven by a single outliers (which seems noise)

- Sweet spot: Shard size 8 to 16 offers the best overall profile — highest write gain (6.95%) with the lowest write stdev (1.18%), plus a consistent read gain (1.42%, stdev 0.70%), no impact on Intel.

* ARM (NVIDIA Grace - Neoverse V2 - single L3 domain: CPUs 0-71)

  ┌────────────┬────────────┬─────────────┬───────────┬────────────┐
  │ Shard Size │ Write Mean │ Write StDev │ Read Mean │ Read StDev │
  ├────────────┼────────────┼─────────────┼───────────┼────────────┤
  │ 2          │ +0.75%     │ 1.32%       │ -1.28%    │ 0.45%      │
  ├────────────┼────────────┼─────────────┼───────────┼────────────┤
  │ 4          │ +3.45%     │ 0.97%       │ -1.73%    │ 0.52%      │
  ├────────────┼────────────┼─────────────┼───────────┼────────────┤
  │ 8          │ +6.72%     │ 1.97%       │ +1.38%    │ 2.79%      │
  ├────────────┼────────────┼─────────────┼───────────┼────────────┤
  │ 16         │ +6.95%     │ 1.18%       │ +1.42%    │ 0.70%      │
  └────────────┴────────────┴─────────────┴───────────┴────────────┘

* Intel (AMD EPYC 9D64 88-Core Processor - 11 L3 domains, 8 Cores / 16 vCPUs each)

  ┌────────────┬────────────┬─────────────┬───────────┬────────────┐
  │ Shard Size │ Write Mean │ Write StDev │ Read Mean │ Read StDev │
  ├────────────┼────────────┼─────────────┼───────────┼────────────┤
  │ 2          │ +3.22%     │ 1.90%       │ -0.08%    │ 0.72%      │
  ├────────────┼────────────┼─────────────┼───────────┼────────────┤
  │ 4          │ +0.92%     │ 1.59%       │ +0.67%    │ 2.33%      │
  ├────────────┼────────────┼─────────────┼───────────┼────────────┤
  │ 8          │ +1.75%     │ 1.47%       │ -0.42%    │ 0.72%      │
  ├────────────┼────────────┼─────────────┼───────────┼────────────┤
  │ 16         │ +1.22%     │ 1.72%       │ +0.43%    │ 1.32%      │
  └────────────┴────────────┴─────────────┴───────────┴────────────┘

---
Changes in v2:
- wq_cache_shard_size is in terms of cores (not vCPU)
- Link to v1: https://patch.msgid.link/[email protected]

---
Breno Leitao (5):
workqueue: fix typo in WQ_AFFN_SMT comment
workqueue: add WQ_AFFN_CACHE_SHARD affinity scope
workqueue: set WQ_AFFN_CACHE_SHARD as the default affinity scope tools/workqueue: add CACHE_SHARD support to wq_dump.py
workqueue: add test_workqueue benchmark module

 include/linux/workqueue.h  |   3 +-
 kernel/workqueue.c         | 110 +++++++++++++++++-
 lib/Kconfig.debug          |  10 ++
 lib/Makefile               |   1 +
 lib/test_workqueue.c       | 277 +++++++++++++++++++++++++++++++++++++++++++++
 tools/workqueue/wq_dump.py |   3 +-
 6 files changed, 401 insertions(+), 3 deletions(-)

---
base-commit: 1adb306427e971ccac25b19410c9f068b92bd583
change-id: 20260309-workqueue_sharded-2327956e889b

Best regards,

Signature · 1 line
Breno Leitao <[email protected]>
#1 lore raw

PATCH v2 1/5 workqueue: fix typo in WQ_AFFN_SMT comment

Fix "poer" -> "per" in the WQ_AFFN_SMT enum comment.

---
 include/linux/workqueue.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/workqueue.h +1 -1
index a4749f56398fd..17543aec2a6e1 100644
@@ -131,7 +131,7 @@ struct rcu_work {
 enum wq_affn_scope {
 	WQ_AFFN_DFL,			/* use system default */
 	WQ_AFFN_CPU,			/* one pod per CPU */
-	WQ_AFFN_SMT,			/* one pod poer SMT */
+	WQ_AFFN_SMT,			/* one pod per SMT */
 	WQ_AFFN_CACHE,			/* one pod per LLC */
 	WQ_AFFN_NUMA,			/* one pod per NUMA node */
 	WQ_AFFN_SYSTEM,			/* one pod across the whole system */
Signature · 1 line
2.52.0
#2 lore raw

PATCH v2 2/5 workqueue: add WQ_AFFN_CACHE_SHARD affinity scope

In reply to Breno Leitao

On systems where many CPUs share one LLC, unbound workqueues using WQ_AFFN_CACHE collapse to a single worker pool, causing heavy spinlock contention on pool->lock. For example, Chuck Lever measured 39% of cycles lost to native_queued_spin_lock_slowpath on a 12-core shared-L3 NFS-over-RDMA system.

The existing affinity hierarchy (cpu, smt, cache, numa, system) offers no intermediate option between per-LLC and per-SMT-core granularity.

Add WQ_AFFN_CACHE_SHARD, which subdivides each LLC into groups of at most wq_cache_shard_size cores (default 8, tunable via boot parameter). Shards are always split on core (SMT group) boundaries so that Hyper-Threading siblings are never placed in different pods. Cores are distributed across shards as evenly as possible -- for example, 36 cores in a single LLC with max shard size 8 produces 5 shards of 8+7+7+7+7 cores.

The implementation follows the same comparator pattern as other affinity
scopes: cpu_cache_shard_id() computes a per-CPU shard index on the fly from the already-initialized WQ_AFFN_CACHE and WQ_AFFN_SMT topology, and cpus_share_cache_shard() is passed to init_pod_type().

Benchmark on NVIDIA Grace (72 CPUs, single LLC, 50k items/thread), show cache_shard delivers ~5x the throughput and ~6.5x lower p50 latency compared to cache scope on this 72-core single-LLC system.

---
 include/linux/workqueue.h |   1 +
 kernel/workqueue.c        | 108 ++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+)
include/linux/workqueue.h +1 -0
index 17543aec2a6e1..50bdb7e30d35f 100644
@@ -133,6 +133,7 @@ enum wq_affn_scope {
 	WQ_AFFN_CPU,			/* one pod per CPU */
 	WQ_AFFN_SMT,			/* one pod per SMT */
 	WQ_AFFN_CACHE,			/* one pod per LLC */
+	WQ_AFFN_CACHE_SHARD,		/* synthetic sub-LLC shards */
 	WQ_AFFN_NUMA,			/* one pod per NUMA node */
 	WQ_AFFN_SYSTEM,			/* one pod across the whole system */
 
kernel/workqueue.c +108 -0

135 lines not on this page · raw message

Signature · 1 line
2.52.0
#7 lore raw

PATCH v2 3/5 workqueue: set WQ_AFFN_CACHE_SHARD as the default affinity scope

In reply to Breno Leitao

Set WQ_AFFN_CACHE_SHARD as the default affinity scope for unbound workqueues. On systems where many CPUs share one LLC, the previous default (WQ_AFFN_CACHE) collapses all CPUs to a single worker pool, causing heavy spinlock contention on pool->lock.

WQ_AFFN_CACHE_SHARD subdivides each LLC into smaller groups, providing a better balance between locality and contention. Users can revert to the previous behavior with workqueue.default_affinity_scope=cache.

On systems with 8 or fewer cores per LLC, CACHE_SHARD produces a single shard covering the entire LLC, making it functionally identical to the previous CACHE default. The sharding only activates when an LLC has more than 8 cores.

---
 kernel/workqueue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/workqueue.c +1 -1
index ebbc7971b4fa6..6bc2e69dd5cc2 100644
@@ -441,7 +441,7 @@ static bool wq_topo_initialized __read_mostly = false;
 static struct kmem_cache *pwq_cache;
 
 static struct wq_pod_type wq_pod_types[WQ_AFFN_NR_TYPES];
-static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE;
+static enum wq_affn_scope wq_affn_dfl = WQ_AFFN_CACHE_SHARD;
 
 /* buf for wq_update_unbound_pod_attrs(), protected by CPU hotplug exclusion */
 static struct workqueue_attrs *unbound_wq_update_pwq_attrs_buf;
Signature · 1 line
2.52.0
#8 lore raw

PATCH v2 4/5 tools/workqueue: add CACHE_SHARD support to wq_dump.py

In reply to Breno Leitao

The WQ_AFFN_CACHE_SHARD affinity scope was added to the kernel but wq_dump.py was not updated to enumerate it. Add the missing constant lookup and include it in the affinity scopes iteration so that drgn output shows the CACHE_SHARD pod topology alongside the other scopes.

---
 tools/workqueue/wq_dump.py | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
tools/workqueue/wq_dump.py +2 -1
index d29b918306b48..06948ffcfc4b6 100644
@@ -107,6 +107,7 @@ WQ_MEM_RECLAIM          = prog['WQ_MEM_RECLAIM']
 WQ_AFFN_CPU             = prog['WQ_AFFN_CPU']
 WQ_AFFN_SMT             = prog['WQ_AFFN_SMT']
 WQ_AFFN_CACHE           = prog['WQ_AFFN_CACHE']
+WQ_AFFN_CACHE_SHARD     = prog['WQ_AFFN_CACHE_SHARD']
 WQ_AFFN_NUMA            = prog['WQ_AFFN_NUMA']
 WQ_AFFN_SYSTEM          = prog['WQ_AFFN_SYSTEM']
 
@@ -138,7 +139,7 @@ def print_pod_type(pt):
         print(f' [{cpu}]={pt.cpu_pod[cpu].value_()}', end='')
     print('')
 
-for affn in [WQ_AFFN_CPU, WQ_AFFN_SMT, WQ_AFFN_CACHE, WQ_AFFN_NUMA, WQ_AFFN_SYSTEM]:
+for affn in [WQ_AFFN_CPU, WQ_AFFN_SMT, WQ_AFFN_CACHE, WQ_AFFN_CACHE_SHARD, WQ_AFFN_NUMA, WQ_AFFN_SYSTEM]:
     print('')
     print(f'{wq_affn_names[affn].string_().decode().upper()}{" (default)" if affn == wq_affn_dfl else ""}')
     print_pod_type(wq_pod_types[affn])
Signature · 1 line
2.52.0
#9 lore raw

PATCH v2 5/5 workqueue: add test_workqueue benchmark module

In reply to Breno Leitao

Add a kernel module that benchmarks queue_work() throughput on an unbound workqueue to measure pool->lock contention under different affinity scope configurations (cache vs cache_shard).

The module spawns N kthreads (default: num_online_cpus()), each bound to a different CPU. All threads start simultaneously and queue work items, measuring the latency of each queue_work() call. Results are reported as p50/p90/p95 latencies for each affinity scope.

The affinity scope is switched between runs via the workqueue's sysfs affinity_scope attribute (WQ_SYSFS), avoiding the need for any new exported symbols.

The module runs as __init-only, returning -EAGAIN to auto-unload, and can be re-run via insmod.

Example of the output:

running 50 threads, 50000 items/thread

   cpu              6806017 items/sec p50=2574    p90=5068    p95=5818 ns
   smt              6821040 items/sec p50=2624    p90=5168    p95=5949 ns
   cache_shard      1633653 items/sec p50=5337    p90=9694    p95=11207 ns
   cache            286069 items/sec p50=72509    p90=82304   p95=85009 ns
   numa             319403 items/sec p50=63745    p90=73480   p95=76505 ns
   system           308461 items/sec p50=66561    p90=75714   p95=78048 ns
---
 lib/Kconfig.debug    |  10 ++
 lib/Makefile         |   1 +
 lib/test_workqueue.c | 277 +++++++++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 288 insertions(+)
lib/Kconfig.debug +10 -0
index 93f356d2b3d95..38bee649697f3 100644
@@ -2628,6 +2628,16 @@ config TEST_VMALLOC
 
 	  If unsure, say N.
 
+config TEST_WORKQUEUE
+	tristate "Test module for stress/performance analysis of workqueue"
+	default n
+	help
+	  This builds the "test_workqueue" module for benchmarking
+	  workqueue throughput under contention. Useful for evaluating
+	  affinity scope changes (e.g., cache_shard vs cache).
+
+	  If unsure, say N.
+
 config TEST_BPF
 	tristate "Test BPF filter functionality"
 	depends on m && NET
lib/Makefile +1 -0
index 1b9ee167517f3..ea660cca04f40 100644
@@ -79,6 +79,7 @@ UBSAN_SANITIZE_test_ubsan.o := y
 obj-$(CONFIG_TEST_KSTRTOX) += test-kstrtox.o
 obj-$(CONFIG_TEST_LKM) += test_module.o
 obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o
+obj-$(CONFIG_TEST_WORKQUEUE) += test_workqueue.o
 obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_keys.o
 obj-$(CONFIG_TEST_STATIC_KEYS) += test_static_key_base.o
lib/test_workqueue.c new file +277 -0

277 lines not on this page · raw message

Signature · 1 line
2.52.0
#10 lore raw
In reply to Breno Leitao
Quoted 11 lines · Breno Leitao

TL;DR: Some modern processors have many CPUs per LLC (L3 cache), and unbound workqueues using the default affinity (WQ_AFFN_CACHE) collapse to a single worker pool, causing heavy spinlock (pool->lock) contention. Create a new affinity (WQ_AFFN_CACHE_SHARD) that caps each pool at wq_cache_shard_size CPUs (default 8).

Changes from RFC:

* wq_cache_shard_size is in terms of cores (not vCPU). So, wq_cache_shard_size=8 means the pool will have 8 cores and their siblings, like 16 threads/CPUs if SMT=1

My concern about the "cores per shard" approach is that it improves the default situation for moderately-sized machines little or not at all.

A machine with one L3 and 10 cores will go from 1 UNBOUND pool to only 2. For virtual machines commonly deployed as cloud instances, which are 2, 4, or 8 core systems (up to 16 threads) there will still be significant contention for UNBOUND workers.

IOW, if you want good scaling, human intervention (via a boot command-line option) is still needed.

Signature · 1 line
Chuck Lever
#11 lore raw

Hello Chuck,

Quoted 17 lines · Chuck Lever
Quoted 11 lines · Breno Leitao

TL;DR: Some modern processors have many CPUs per LLC (L3 cache), and unbound workqueues using the default affinity (WQ_AFFN_CACHE) collapse to a single worker pool, causing heavy spinlock (pool->lock) contention. Create a new affinity (WQ_AFFN_CACHE_SHARD) that caps each pool at wq_cache_shard_size CPUs (default 8).

Changes from RFC:

* wq_cache_shard_size is in terms of cores (not vCPU). So, wq_cache_shard_size=8 means the pool will have 8 cores and their siblings, like 16 threads/CPUs if SMT=1

My concern about the "cores per shard" approach is that it improves the default situation for moderately-sized machines little or not at all.

Quoted 5 lines

A machine with one L3 and 10 cores will go from 1 UNBOUND pool to only 2. For virtual machines commonly deployed as cloud instances, which are 2, 4, or 8 core systems (up to 16 threads) there will still be significant contention for UNBOUND workers.

Could you clarify your concern? Are you suggesting the default value of wq_cache_shard_size=8 is too high, or that the cores-per-shard approach fundamentally doesn't scale well for moderately-sized systems?

Any approach—whether sharding by cores or by LLC—ultimately relies on heuristics that may need tuning for specific workloads. The key difference is where we draw the line. The current default of 8 cores prevents the worst-case scenario: severe lock contention on large systems with 16+ CPUs all hammering a single unbound workqueue.

For smaller systems (2-4 CPUs), contention is usually negligible regardless of the approach. My perf lock contention measurements consistently show minimal contention in that range.

Quoted 2 lines

IOW, if you want good scaling, human intervention (via a boot command-line option) is still needed.

I am not convinced. The wq_cache_shard_size approach creates multiple pools on large systems while leaving small systems (<8 cores) unchanged. This eliminates the pathological lock contention we're observing on high-core-count machines without impacting smaller deployments.

In contrast, splitting pools per LLC would force fragmentation even on systems that aren't experiencing contention, increasing the need for manual tuning across a wider range of configurations.

Thanks for the review,
--breno

#12 lore raw
Quoted 30 lines · Breno Leitao

Hello Chuck,

Quoted 17 lines · Chuck Lever
Quoted 11 lines · Breno Leitao

TL;DR: Some modern processors have many CPUs per LLC (L3 cache), and unbound workqueues using the default affinity (WQ_AFFN_CACHE) collapse to a single worker pool, causing heavy spinlock (pool->lock) contention. Create a new affinity (WQ_AFFN_CACHE_SHARD) that caps each pool at wq_cache_shard_size CPUs (default 8).

Changes from RFC:

* wq_cache_shard_size is in terms of cores (not vCPU). So, wq_cache_shard_size=8 means the pool will have 8 cores and their siblings, like 16 threads/CPUs if SMT=1

My concern about the "cores per shard" approach is that it improves the default situation for moderately-sized machines little or not at all.

Quoted 5 lines

A machine with one L3 and 10 cores will go from 1 UNBOUND pool to only 2. For virtual machines commonly deployed as cloud instances, which are 2, 4, or 8 core systems (up to 16 threads) there will still be significant contention for UNBOUND workers.

Could you clarify your concern? Are you suggesting the default value of wq_cache_shard_size=8 is too high, or that the cores-per-shard approach fundamentally doesn't scale well for moderately-sized systems?

Quoted 5 lines

Any approach—whether sharding by cores or by LLC—ultimately relies on heuristics that may need tuning for specific workloads. The key difference is where we draw the line. The current default of 8 cores prevents the worst-case scenario: severe lock contention on large systems with 16+ CPUs all hammering a single unbound workqueue.

An 8-core machine with 16 threads can handle quite a bit of I/O, but with the proposed scheme it will still have a single UNBOUND pool. For NFS workloads I commonly benchmark, splitting the UNBOUND pool on such systems is a very clear win.

Quoted 9 lines

For smaller systems (2-4 CPUs), contention is usually negligible regardless of the approach. My perf lock contention measurements consistently show minimal contention in that range.

Quoted 2 lines

IOW, if you want good scaling, human intervention (via a boot command-line option) is still needed.

I am not convinced. The wq_cache_shard_size approach creates multiple pools on large systems while leaving small systems (<8 cores) unchanged.

This is exactly my concern. Smaller systems /do/ experience measurable contention in this area. I don't object to your series at all, it's clean and well-motivated; but the cores-per-shard approach doesn't scale down to very commonly deployed machine sizes.

We might also argue that the NFS client and other subsystems that make significant use of UNBOUND workqueues in their I/O paths might be well advised to modify their approach. (net/sunrpc/sched.c, hint hint)

Quoted 6 lines

This eliminates the pathological lock contention we're observing on high-core-count machines without impacting smaller deployments.

In contrast, splitting pools per LLC would force fragmentation even on systems that aren't experiencing contention, increasing the need for manual tuning across a wider range of configurations.

I claim that smaller deployments also need help. Further, I don't see how UNBOUND pool fragmentation is a problem on such systems that needs to be addressed (IMHO).

Signature · 1 line
Chuck Lever
#13 lore raw

Hello Chuck,

Quoted 9 lines · Chuck Lever
Quoted 3 lines · Breno Leitao

I am not convinced. The wq_cache_shard_size approach creates multiple pools on large systems while leaving small systems (<8 cores) unchanged.

This is exactly my concern. Smaller systems /do/ experience measurable contention in this area. I don't object to your series at all, it's clean and well-motivated; but the cores-per-shard approach doesn't scale down to very commonly deployed machine sizes.

I don't see why the cores-per-shard approach wouldn't scale down effectively.

The sharding mechanism itself is independent of whether we use cores-per-shard or shards-per-LLC as the allocation strategy, correct?

Regardless of the approach, we retain full control over the granularity of the shards.

Quoted 8 lines

We might also argue that the NFS client and other subsystems that make significant use of UNBOUND workqueues in their I/O paths might be well advised to modify their approach. (net/sunrpc/sched.c, hint hint)

Quoted 2 lines

This eliminates the pathological lock contention we're observing on high-core-count machines without impacting smaller deployments.

Quoted 7 lines
Quoted 3 lines

In contrast, splitting pools per LLC would force fragmentation even on systems that aren't experiencing contention, increasing the need for manual tuning across a wider range of configurations.

I claim that smaller deployments also need help. Further, I don't see how UNBOUND pool fragmentation is a problem on such systems that needs to be addressed (IMHO).

Are you suggesting we should reduce the default value to something like wq_cache_shard_size=2 instead of wq_cache_shard_size=8?

Thanks for the feedback,
--breno

#14 lore raw
Quoted 8 lines · Breno Leitao

Hello Chuck,

Quoted 5 lines · Chuck Lever
Quoted 3 lines · Breno Leitao

I am not convinced. The wq_cache_shard_size approach creates multiple pools on large systems while leaving small systems (<8 cores) unchanged.

Quoted 7 lines
Quoted 4 lines

This is exactly my concern. Smaller systems /do/ experience measurable contention in this area. I don't object to your series at all, it's clean and well-motivated; but the cores-per-shard approach doesn't scale down to very commonly deployed machine sizes.

I don't see why the cores-per-shard approach wouldn't scale down effectively.

Sharding the UNBOUND pool is fine. But with a fixed cores-per-shard ratio of 8, it doesn't scale down to smaller systems.

Quoted 18 lines

The sharding mechanism itself is independent of whether we use cores-per-shard or shards-per-LLC as the allocation strategy, correct?

Regardless of the approach, we retain full control over the granularity of the shards.

Quoted 12 lines

We might also argue that the NFS client and other subsystems that make significant use of UNBOUND workqueues in their I/O paths might be well advised to modify their approach. (net/sunrpc/sched.c, hint hint)

Quoted 6 lines

This eliminates the pathological lock contention we're observing on high-core-count machines without impacting smaller deployments.

In contrast, splitting pools per LLC would force fragmentation even on systems that aren't experiencing contention, increasing the need for manual tuning across a wider range of configurations.

Quoted 6 lines
Quoted 3 lines

I claim that smaller deployments also need help. Further, I don't see how UNBOUND pool fragmentation is a problem on such systems that needs to be addressed (IMHO).

Are you suggesting we should reduce the default value to something like wq_cache_shard_size=2 instead of wq_cache_shard_size=8?

A shard size of 2 clearly won't scale properly to hundreds of cores. A varying default cores-per-shard ratio would help scaling in both directions, without having to manually tune.

Signature · 1 line
Chuck Lever
maintainer
#15 lore raw

Hello,

Quoted 5 lines · Chuck Lever
Quoted 2 lines

I don't see why the cores-per-shard approach wouldn't scale down effectively.

Sharding the UNBOUND pool is fine. But with a fixed cores-per-shard ratio of 8, it doesn't scale down to smaller systems.

You aren't making a lot of sense. Contention is primarily the function of the number of CPUs competing, not inverse of how many cores are in the LLC.

Quoted 3 lines

A shard size of 2 clearly won't scale properly to hundreds of cores. A varying default cores-per-shard ratio would help scaling in both directions, without having to manually tune.

If your workload is bottlenecked on pool lock on small machines, the right course of action is either making the offending workqueue per-cpu or configure the unbound workqueue for that specific use case. That's why it's progrmatically configurable in the first place.

Thanks.

Signature · 1 line
tejun
maintainer
#3 lore raw

Re: [PATCH v2 2/5] workqueue: add WQ_AFFN_CACHE_SHARD affinity scope

In reply to Breno Leitao

Hello,

Quoted 38 lines · Breno Leitao
kernel/workqueue.c +38 -0
+/**
+ * llc_count_cores - count distinct cores (SMT groups) within a cpumask
+ * @pod_cpus: the cpumask to scan (typically an LLC pod)
+ * @smt_pt:   the SMT pod type, used to identify sibling groups
+ *
+ * A core is represented by the lowest-numbered CPU in its SMT group. Returns
+ * the number of distinct cores found in @pod_cpus.
+ */
+static int __init llc_count_cores(const struct cpumask *pod_cpus,
+				  struct wq_pod_type *smt_pt)
+{
+	const struct cpumask *smt_cpus;
+	int nr_cores = 0, c;
+
+	for_each_cpu(c, pod_cpus) {
+		smt_cpus = smt_pt->pod_cpus[smt_pt->cpu_pod[c]];
+		if (cpumask_first(smt_cpus) == c)
+			nr_cores++;
+	}
+
+	return nr_cores;
+}
+
+/**
+ * llc_cpu_core_pos - find a CPU's core position within a cpumask
+ * @cpu:      the CPU to locate
+ * @pod_cpus: the cpumask to scan (typically an LLC pod)
+ * @smt_pt:   the SMT pod type, used to identify sibling groups
+ *
+ * Returns the zero-based index of @cpu's core among the distinct cores in
+ * @pod_cpus, ordered by lowest CPU number in each SMT group.
+ */
+static int __init llc_cpu_core_pos(int cpu, const struct cpumask *pod_cpus,
+				   struct wq_pod_type *smt_pt)
+{
+	const struct cpumask *smt_cpus;
+	int core_pos = 0, c;
+

kernel/workqueue.c static bool __init cpus_share_numa(int cpu0, int cpu1) · line 8161

Quoted 10 lines
kernel/workqueue.c +10 -0
+	for_each_cpu(c, pod_cpus) {
+		smt_cpus = smt_pt->pod_cpus[smt_pt->cpu_pod[c]];
+		if (cpumask_test_cpu(cpu, smt_cpus))
+			break;
+		if (cpumask_first(smt_cpus) == c)
+			core_pos++;
+	}
+
+	return core_pos;
+}

Can you do the above two in a separate pass and record the results and then use that to implement cpu_cache_shard_id()? Doing all of it on the fly makes it unnecessarily difficult to follow and init_pod_type() is already O(N^2) and the above makes it O(N^4). Make the machine large enough and this may become noticeable.

Quoted 4 lines

+/**
+ * cpu_cache_shard_id - compute the shard index for a CPU within its LLC pod
+ * @cpu: the CPU to look up
+ *

Quoted 8 lines

+ * Returns a shard index that is unique within the CPU's LLC pod. The LLC is
+ * divided into shards of at most wq_cache_shard_size cores, always split on
+ * core (SMT group) boundaries so that SMT siblings are never placed in
+ * different shards. Cores are distributed across shards as evenly as possible.
+ *
+ * Example: 36 cores with wq_cache_shard_size=8 gives 5 shards of
+ * 8+7+7+7+7 cores.
+ */

I always feel a bit uneasy about using max number as split point in cases like this because the reason why you picked 8 as the default was that testing showed shard sizes close to 8 seems to behave the best (or at least acceptably in most cases). However, setting max number to 8 doesn't necessarily keep you close to that. e.g. If there are 9 cores, you end up with 5 and 4 even though 9 is a lot closer to the 8 that we picked as the default. Can the sharding logic updated so that "whatever sharding that gets the system closest to the config target?".

Thanks.

Signature · 1 line
tejun
#4 lore raw

Re: [PATCH v2 2/5] workqueue: add WQ_AFFN_CACHE_SHARD affinity scope

Hello Tejun,

Quoted 15 lines · Tejun Heo
Quoted 14 lines · Breno Leitao
(quoted diff) +14 -0
+static int __init llc_cpu_core_pos(int cpu, const struct cpumask *pod_cpus,
+				   struct wq_pod_type *smt_pt)
+{
+	const struct cpumask *smt_cpus;
+	int core_pos = 0, c;
+
+	for_each_cpu(c, pod_cpus) {
+		smt_cpus = smt_pt->pod_cpus[smt_pt->cpu_pod[c]];
+		if (cpumask_test_cpu(cpu, smt_cpus))
+			break;
+		if (cpumask_first(smt_cpus) == c)
+			core_pos++;
+	}
+
Quoted 8 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+	return core_pos;
+}

Can you do the above two in a separate pass and record the results and then use that to implement cpu_cache_shard_id()? Doing all of it on the fly makes it unnecessarily difficult to follow and init_pod_type() is already O(N^2) and the above makes it O(N^4). Make the machine large enough and this may become noticeable.

Ack. I am planning to create a __initdata per-CPU array to host the shard per CPU, and query it instad.

	/* Per-CPU shard index within its LLC pod; populated by precompute_cache_shard_ids() */
	static int __initdata cpu_shard_id[NR_CPUS];
Quoted 12 lines
Quoted 11 lines

+ * cpu_cache_shard_id - compute the shard index for a CPU within its LLC pod
+ * @cpu: the CPU to look up
+ *
+ * Returns a shard index that is unique within the CPU's LLC pod. The LLC is
+ * divided into shards of at most wq_cache_shard_size cores, always split on
+ * core (SMT group) boundaries so that SMT siblings are never placed in
+ * different shards. Cores are distributed across shards as evenly as possible.
+ *
+ * Example: 36 cores with wq_cache_shard_size=8 gives 5 shards of
+ * 8+7+7+7+7 cores.
+ */

Quoted 8 lines

I always feel a bit uneasy about using max number as split point in cases like this because the reason why you picked 8 as the default was that testing showed shard sizes close to 8 seems to behave the best (or at least acceptably in most cases). However, setting max number to 8 doesn't necessarily keep you close to that. e.g. If there are 9 cores, you end up with 5 and 4 even though 9 is a lot closer to the 8 that we picked as the default. Can the sharding logic updated so that "whatever sharding that gets the system closest to the config target?".

I think DIV_ROUND_CLOSEST will do what we want, something as:

nr_shards = max(1, DIV_ROUND_CLOSEST(nr_cores,
wq_cache_shard_size))
cores_per_shard = nr_cores / nr_shards
remainder = nr_cores % nr_shards

The first remainder shards get cores_per_shard+1 cores (large shards), the rest get cores_per_shard.

Assuming wq_cache_shard_size = 8;, we would have the following number of pool per number of CPU (not vCPU):

- 1–11 CPUs → DIV_ROUND_CLOSEST(n, 8) ≤ 1 → 1 pool containing all CPUs.
- 12 CPUs → DIV_ROUND_CLOSEST(12, 8) = 2 → 2 pools of 6 cores each. This is the first split.
- 12–19 → 2 pools
- 20–27 → 3 pools
- 28–35 → 4 pools
- 36–43 → 5 pools
- 44–51 → 6 pools
- 52–59 → 7 pools
- 60–67 → 8 pools
- 68–75 → 9 pools (e.g. 72-CPU NVIDIA Grace → 9×8)
- 76–83 → 10 pools
- 84–91 → 11 pools
- 92–99 → 12 pools
- 100 → 13 pools (9×8 + 4×7)

Is this what you meant?

This is the current code I have been testing with the changes above:

commit ff6c6272e5925d3099109107789e685f58bd4c1e

Author: Breno Leitao <[email protected]>
Date:   Mon Mar 9 08:39:52 2026 -0700

workqueue: add WQ_AFFN_CACHE_SHARD affinity scope

On systems where many CPUs share one LLC, unbound workqueues using WQ_AFFN_CACHE collapse to a single worker pool, causing heavy spinlock contention on pool->lock. For example, Chuck Lever measured 39% of cycles lost to native_queued_spin_lock_slowpath on a 12-core shared-L3 NFS-over-RDMA system.

The existing affinity hierarchy (cpu, smt, cache, numa, system) offers no intermediate option between per-LLC and per-SMT-core granularity.

Add WQ_AFFN_CACHE_SHARD, which subdivides each LLC into groups of at most wq_cache_shard_size cores (default 8, tunable via boot parameter). Shards are always split on core (SMT group) boundaries so that Hyper-Threading siblings are never placed in different pods. Cores are distributed across shards as evenly as possible -- for example, 36 cores in a single LLC with max shard size 8 produces 5 shards of 8+7+7+7+7 cores.

The implementation follows the same comparator pattern as other affinity
scopes: cpu_cache_shard_id() computes a per-CPU shard index on the fly from the already-initialized WQ_AFFN_CACHE and WQ_AFFN_SMT topology, and cpus_share_cache_shard() is passed to init_pod_type().

Benchmark on NVIDIA Grace (72 CPUs, single LLC, 50k items/thread), show cache_shard delivers ~5x the throughput and ~6.5x lower p50 latency compared to cache scope on this 72-core single-LLC system.

include/linux/workqueue.h +1 -0
index 17543aec2a6e1..50bdb7e30d35f 100644
@@ -133,6 +133,7 @@ enum wq_affn_scope {
 	WQ_AFFN_CPU,			/* one pod per CPU */
 	WQ_AFFN_SMT,			/* one pod per SMT */
 	WQ_AFFN_CACHE,			/* one pod per LLC */
+	WQ_AFFN_CACHE_SHARD,		/* synthetic sub-LLC shards */
 	WQ_AFFN_NUMA,			/* one pod per NUMA node */
 	WQ_AFFN_SYSTEM,			/* one pod across the whole system */
 
kernel/workqueue.c +141 -0

168 lines not on this page · raw message

maintainer
#5 lore raw

Re: [PATCH v2 2/5] workqueue: add WQ_AFFN_CACHE_SHARD affinity scope

Quoted 18 lines · Breno Leitao

Assuming wq_cache_shard_size = 8;, we would have the following number of pool per number of CPU (not vCPU):

- 1–11 CPUs → DIV_ROUND_CLOSEST(n, 8) ≤ 1 → 1 pool containing all CPUs.
- 12 CPUs → DIV_ROUND_CLOSEST(12, 8) = 2 → 2 pools of 6 cores each. This is the first split.
- 12–19 → 2 pools
- 20–27 → 3 pools
- 28–35 → 4 pools
- 36–43 → 5 pools
- 44–51 → 6 pools
- 52–59 → 7 pools
- 60–67 → 8 pools
- 68–75 → 9 pools (e.g. 72-CPU NVIDIA Grace → 9×8)
- 76–83 → 10 pools
- 84–91 → 11 pools
- 92–99 → 12 pools
- 100 → 13 pools (9×8 + 4×7)

Quoted 1 line

Is this what you meant?

Yes.

Quoted 4 lines
kernel/workqueue.c +4 -0
+static int __init llc_core_to_shard(int core_pos, int cores_per_shard,
+				    int remainder)
+{
+	int ret;

kernel/workqueue.c static bool __init cpus_share_numa(int cpu0, int cpu1) · line 8168

Quoted 10 lines
kernel/workqueue.c +10 -0
+
+	/*
+	 * These cores falls within the large shards.
+	 * Each large shard has (cores_per_shard + 1) cores
+	 */
+	if (core_pos < remainder * (cores_per_shard + 1))
+		return core_pos / (cores_per_shard + 1);
+
+	/* These are standard shards */
+	ret = (core_pos - remainder * (cores_per_shard + 1)) / cores_per_shard;

This is too smart. Any chance you can dumb it down? If you have to go through intermediate data structures, that's fine too.

Thanks.

Signature · 1 line
tejun
#6 lore raw

Re: [PATCH v2 2/5] workqueue: add WQ_AFFN_CACHE_SHARD affinity scope

Quoted 10 lines · Tejun Heo
Quoted 9 lines · Breno Leitao
(quoted diff) +9 -0
+static int __init llc_core_to_shard(int core_pos, int cores_per_shard,
+				    int remainder)
+{
+	int ret;
+
+	/*
+	 * These cores falls within the large shards.
+	 * Each large shard has (cores_per_shard + 1) cores
+	 */
Quoted 8 lines

Review

Quoted 5 lines
(quoted diff) +5 -0
+	if (core_pos < remainder * (cores_per_shard + 1))
+		return core_pos / (cores_per_shard + 1);
+
+	/* These are standard shards */
+	ret = (core_pos - remainder * (cores_per_shard + 1)) / cores_per_shard;

This is too smart. Any chance you can dumb it down? If you have to go through intermediate data structures, that's fine too.

Thanks, Let me create a layout to represent the shard, and give some names to shard, then it will make the code easier to digest.

Tl;DR: We have "large shard" and "regular shards". Where large shards is regular shards + 1 (if the division is not exact).

	/* Layout of shards within one LLC pod */
	struct llc_shard_layout {
		int nr_large_shards;	/* number of large shards (cores_per_shard + 1) */
		int cores_per_shard;	/* base number of cores per default shard */
		int nr_shards;		/* total number of shards */
		/* nr_default shards = (nr_shards - nr_large_shards) */
	};

Then, when populating the it using:

	static struct llc_shard_layout __init llc_calc_shard_layout(int nr_cores)
	{
		struct llc_shard_layout layout;

		layout.nr_shards = max(1, DIV_ROUND_CLOSEST(nr_cores, q_cache_shard_size));
		layout.cores_per_shard = nr_cores / layout.nr_shards;
		layout.nr_large_shards = nr_cores % layout.nr_shards; (whit was the remainder in the last patch)

		return layout;
	}

This is the full patch I am working on:

commit ea801773c3b80f50d81c52f4e174276013f1e562

Author: Breno Leitao <[email protected]>
Date:   Mon Mar 9 08:39:52 2026 -0700

workqueue: add WQ_AFFN_CACHE_SHARD affinity scope

On systems where many CPUs share one LLC, unbound workqueues using WQ_AFFN_CACHE collapse to a single worker pool, causing heavy spinlock contention on pool->lock. For example, Chuck Lever measured 39% of cycles lost to native_queued_spin_lock_slowpath on a 12-core shared-L3 NFS-over-RDMA system.

The existing affinity hierarchy (cpu, smt, cache, numa, system) offers no intermediate option between per-LLC and per-SMT-core granularity.

Add WQ_AFFN_CACHE_SHARD, which subdivides each LLC into groups of at most wq_cache_shard_size cores (default 8, tunable via boot parameter). Shards are always split on core (SMT group) boundaries so that Hyper-Threading siblings are never placed in different pods. Cores are distributed across shards as evenly as possible -- for example, 36 cores in a single LLC with max shard size 8 produces 5 shards of 8+7+7+7+7 cores.

The implementation follows the same comparator pattern as other affinity
scopes: cpu_cache_shard_id() computes a per-CPU shard index on the fly from the already-initialized WQ_AFFN_CACHE and WQ_AFFN_SMT topology, and cpus_share_cache_shard() is passed to init_pod_type().

Benchmark on NVIDIA Grace (72 CPUs, single LLC, 50k items/thread), show cache_shard delivers ~5x the throughput and ~6.5x lower p50 latency compared to cache scope on this 72-core single-LLC system.

include/linux/workqueue.h +1 -0
index 17543aec2a6e1..50bdb7e30d35f 100644
@@ -133,6 +133,7 @@ enum wq_affn_scope {
 	WQ_AFFN_CPU,			/* one pod per CPU */
 	WQ_AFFN_SMT,			/* one pod per SMT */
 	WQ_AFFN_CACHE,			/* one pod per LLC */
+	WQ_AFFN_CACHE_SHARD,		/* synthetic sub-LLC shards */
 	WQ_AFFN_NUMA,			/* one pod per NUMA node */
 	WQ_AFFN_SYSTEM,			/* one pod across the whole system */
 
kernel/workqueue.c +168 -0

201 lines not on this page · raw message