lib/percpu_counter: allow concurrent sums with an rwlock
PATCH v2 1/1 lib/percpu_counter: allow concurrent sums with an rwlock
__percpu_counter_sum() holds a raw spinlock while reading the global count and per-CPU deltas, serializing concurrent sums of the same counter. Since a sum changes neither the global count nor the per-CPU deltas, use rwlock_t on !PREEMPT_RT to allow concurrent sums with a read lock. Global updates take the write lock. The per-CPU fast path remains lockless, so sums keep their existing snapshot semantics.
stress-ng dentry reaches this path through ext4_statfs():
statfs -> ext4_statfs -> __percpu_counter_sum
-> native_queued_spin_lock_slowpath
ext4_statfs() uses percpu_counter_sum_positive(), which calls __percpu_counter_sum(). At low writeback thresholds, dirty throttling also reaches percpu_counter_sum_positive() through wb_stat_sum(). Buffered writers sharing a writeback instance can sum the same page counters concurrently.
Keep the raw spinlock on PREEMPT_RT, where rwlock_t can sleep. Global counter updates can run in atomic context and must not sleep.
stress-ng dentry test average throughput change:
+------------------------+------------+--------------------+ | Case | Unit | Average change (%) | +------------------------+------------+--------------------+ | Dentry, 64 workers | bogo ops/s | +47.34 | +------------------------+------------+--------------------+
Dentry ran on ext4 with 64 workers pinned to physical CPUs:
stress-ng --dentry 64 --dentries 4096 --timeout 20s
fio results from the review thread (3 x 60 s per batch, vs. mean baseline):
+------+------------+-----------------+---------+ | Test | Throughput | CPU utilization | Cycles | +------+------------+-----------------+---------+ | fio | +6.91% | -40.41% | -40.80% | +------+------------+-----------------+---------+
fio ran on ext4 with 64 buffered jobs, 64 KiB sequential writes, a 32 GiB memory limit, and 64/32 MiB dirty/background limits.
---
Changes in v2:
- Replace the locking macros with static inline functions.
- Explain the RT lock choice in struct percpu_counter.
- Describe the percpu_counter_sum_positive() and dirty-throttling callers.
v1: https://lore.kernel.org/linux-mm/[email protected]/
include/linux/percpu_counter.h | 5 ++ lib/percpu_counter.c | 126 ++++++++++++++++++++++++++++----- 2 files changed, 115 insertions(+), 16 deletions(-)
include/linux/percpu_counter.h +5 -0
@@ -20,7 +20,12 @@ #ifdef CONFIG_SMP struct percpu_counter { +#ifdef CONFIG_PREEMPT_RT + /* Updates can run in atomic context, where an RT rwlock may sleep. */ raw_spinlock_t lock; +#else + rwlock_t lock; /* shared for sums, exclusive for updates */ +#endif s64 count; #ifdef CONFIG_HOTPLUG_CPU struct list_head list; /* All percpu_counters are on a list */
lib/percpu_counter.c +110 -16
210 lines not on this page · raw message
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.52.0