* [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref [not found] <01612865-faa0-4fe5-8184-a177f104843f@gmail.com> @ 2026-11-25 15:55 ` Egor Shalashnov 2026-11-25 19:35 ` Andrew Morton 2026-11-26 15:56 ` sashiko-bot 0 siblings, 2 replies; 4+ messages in thread From: Egor Shalashnov @ 2026-11-25 15:55 UTC (permalink / raw) To: linux-mm; +Cc: dennis, tj, cl, akpm, zhengqi.arch, linux-kernel From: Qi Zheng <zhengqi.arch@bytedance.com> Currently, percpu_ref uses the global percpu_ref_switch_lock to protect the mode switching operation. When multiple percpu_ref perform mode switching at the same time, the lock may become a performance bottleneck. This patch introduces per percpu_ref percpu_ref_switch_lock to fixes this situation. Signed-off-by: Qi Zheng <zhengqi.arch@bytedance.com> Signed-off-by: Egor Shalashnov <shalasheg@gmail.com> --- I would like to revive Qi Zheng's per-reference switch lock patch from 2022 as a standalone change. Our workload maintains many independent percpu_ref instances and repeatedly performs kill, waits for references to drain, and reinitializes them. Multiple CPUs execute these cycles concurrently on different instances. All these transitions compete for the same global switch lock. Reinitialization also clears the per-CPU counters while holding it. As the number of concurrent transitions grows, this shared lock becomes a significant bottleneck. With a module-local adaptation of Qi's per-reference locking change, we observed approximately 2.8x to 6.5x lower operation completion times in the measured workload. This motivates using a lock per instance so that independent transitions can proceed concurrently. Original posting: https://lore.kernel.org/linux-mm/20220429133552.33768-4-zhengqi.arch@bytedance.com/ Rebased onto v7.4-rc4 with an explicit spinlock_types.h include. include/linux/percpu-refcount.h | 3 +++ lib/percpu-refcount.c | 30 +++++++++++++++--------------- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h index 1e3212e..4582e79 100644 --- a/include/linux/percpu-refcount.h +++ b/include/linux/percpu-refcount.h @@ -53,6 +53,7 @@ #include <linux/atomic.h> #include <linux/percpu.h> #include <linux/rcupdate.h> +#include <linux/spinlock_types.h> #include <linux/types.h> #include <linux/gfp.h> @@ -109,6 +110,8 @@ struct percpu_ref { */ unsigned long percpu_count_ptr; + spinlock_t percpu_ref_switch_lock; + /* * 'percpu_ref' is often embedded into user structure, and only * 'percpu_count_ptr' is required in fast path, move other fields diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c index f8d9068..5d46602 100644 --- a/lib/percpu-refcount.c +++ b/lib/percpu-refcount.c @@ -36,7 +36,6 @@ #define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1)) -static DEFINE_SPINLOCK(percpu_ref_switch_lock); static DECLARE_WAIT_QUEUE_HEAD(percpu_ref_switch_waitq); static unsigned long __percpu *percpu_count_ptr(struct percpu_ref *ref) @@ -96,6 +95,7 @@ int percpu_ref_init(struct percpu_ref *ref, percpu_ref_func_t *release, start_count++; atomic_long_set(&data->count, start_count); + spin_lock_init(&ref->percpu_ref_switch_lock); data->release = release; data->confirm_switch = NULL; @@ -137,11 +137,11 @@ void percpu_ref_exit(struct percpu_ref *ref) if (!data) return; - spin_lock_irqsave(&percpu_ref_switch_lock, flags); + spin_lock_irqsave(&ref->percpu_ref_switch_lock, flags); ref->percpu_count_ptr |= atomic_long_read(&ref->data->count) << __PERCPU_REF_FLAG_BITS; ref->data = NULL; - spin_unlock_irqrestore(&percpu_ref_switch_lock, flags); + spin_unlock_irqrestore(&ref->percpu_ref_switch_lock, flags); kfree(data); } @@ -267,7 +267,7 @@ static void __percpu_ref_switch_mode(struct percpu_ref *ref, { struct percpu_ref_data *data = ref->data; - lockdep_assert_held(&percpu_ref_switch_lock); + lockdep_assert_held(&ref->percpu_ref_switch_lock); /* * If the previous ATOMIC switching hasn't finished yet, wait for @@ -275,7 +275,7 @@ static void __percpu_ref_switch_mode(struct percpu_ref *ref, * isn't in progress, this function can be called from any context. */ wait_event_lock_irq(percpu_ref_switch_waitq, !data->confirm_switch, - percpu_ref_switch_lock); + ref->percpu_ref_switch_lock); if (data->force_atomic || percpu_ref_is_dying(ref)) __percpu_ref_switch_to_atomic(ref, confirm_switch); @@ -308,12 +308,12 @@ void percpu_ref_switch_to_atomic(struct percpu_ref *ref, { unsigned long flags; - spin_lock_irqsave(&percpu_ref_switch_lock, flags); + spin_lock_irqsave(&ref->percpu_ref_switch_lock, flags); ref->data->force_atomic = true; __percpu_ref_switch_mode(ref, confirm_switch); - spin_unlock_irqrestore(&percpu_ref_switch_lock, flags); + spin_unlock_irqrestore(&ref->percpu_ref_switch_lock, flags); } EXPORT_SYMBOL_GPL(percpu_ref_switch_to_atomic); @@ -354,12 +354,12 @@ void percpu_ref_switch_to_percpu(struct percpu_ref *ref) { unsigned long flags; - spin_lock_irqsave(&percpu_ref_switch_lock, flags); + spin_lock_irqsave(&ref->percpu_ref_switch_lock, flags); ref->data->force_atomic = false; __percpu_ref_switch_mode(ref, NULL); - spin_unlock_irqrestore(&percpu_ref_switch_lock, flags); + spin_unlock_irqrestore(&ref->percpu_ref_switch_lock, flags); } EXPORT_SYMBOL_GPL(percpu_ref_switch_to_percpu); @@ -385,7 +385,7 @@ void percpu_ref_kill_and_confirm(struct percpu_ref *ref, { unsigned long flags; - spin_lock_irqsave(&percpu_ref_switch_lock, flags); + spin_lock_irqsave(&ref->percpu_ref_switch_lock, flags); WARN_ONCE(percpu_ref_is_dying(ref), "%s called more than once on %ps!", __func__, @@ -395,7 +395,7 @@ void percpu_ref_kill_and_confirm(struct percpu_ref *ref, __percpu_ref_switch_mode(ref, confirm_kill); percpu_ref_put(ref); - spin_unlock_irqrestore(&percpu_ref_switch_lock, flags); + spin_unlock_irqrestore(&ref->percpu_ref_switch_lock, flags); } EXPORT_SYMBOL_GPL(percpu_ref_kill_and_confirm); @@ -416,12 +416,12 @@ bool percpu_ref_is_zero(struct percpu_ref *ref) return false; /* protect us from being destroyed */ - spin_lock_irqsave(&percpu_ref_switch_lock, flags); + spin_lock_irqsave(&ref->percpu_ref_switch_lock, flags); if (ref->data) count = atomic_long_read(&ref->data->count); else count = ref->percpu_count_ptr >> __PERCPU_REF_FLAG_BITS; - spin_unlock_irqrestore(&percpu_ref_switch_lock, flags); + spin_unlock_irqrestore(&ref->percpu_ref_switch_lock, flags); return count == 0; } @@ -465,7 +465,7 @@ void percpu_ref_resurrect(struct percpu_ref *ref) unsigned long __percpu *percpu_count; unsigned long flags; - spin_lock_irqsave(&percpu_ref_switch_lock, flags); + spin_lock_irqsave(&ref->percpu_ref_switch_lock, flags); WARN_ON_ONCE(!percpu_ref_is_dying(ref)); WARN_ON_ONCE(__ref_is_percpu(ref, &percpu_count)); @@ -474,6 +474,6 @@ void percpu_ref_resurrect(struct percpu_ref *ref) percpu_ref_get(ref); __percpu_ref_switch_mode(ref, NULL); - spin_unlock_irqrestore(&percpu_ref_switch_lock, flags); + spin_unlock_irqrestore(&ref->percpu_ref_switch_lock, flags); } EXPORT_SYMBOL_GPL(percpu_ref_resurrect); base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref 2026-11-25 15:55 ` [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref Egor Shalashnov @ 2026-11-25 19:35 ` Andrew Morton 2026-11-25 21:46 ` Egor Shalashnov 2026-11-26 15:56 ` sashiko-bot 1 sibling, 1 reply; 4+ messages in thread From: Andrew Morton @ 2026-11-25 19:35 UTC (permalink / raw) To: Egor Shalashnov; +Cc: linux-mm, dennis, tj, cl, zhengqi.arch, linux-kernel On Wed, 25 Nov 2026 18:55:55 +0300 Egor Shalashnov <shalasheg@gmail.com> wrote: > From: Qi Zheng <zhengqi.arch@bytedance.com> > > Currently, percpu_ref uses the global percpu_ref_switch_lock to > protect the mode switching operation. When multiple percpu_ref > perform mode switching at the same time, the lock may become a > performance bottleneck. > > This patch introduces per percpu_ref percpu_ref_switch_lock to > fixes this situation. > > ... > > I would like to revive Qi Zheng's per-reference switch lock patch > from 2022 as a standalone change. This is a nice little change - it's a shame it was buried inside in a larger patchset back in 2022. > Our workload maintains many independent percpu_ref instances and > repeatedly performs kill, waits for references to drain, and reinitializes > them. Multiple CPUs execute these cycles concurrently on different > instances. > > All these transitions compete for the same global switch lock. > Reinitialization also clears the per-CPU counters while holding it. > As the number of concurrent transitions grows, this shared lock > becomes a significant bottleneck. > > With a module-local adaptation of Qi's per-reference locking change, > we observed approximately 2.8x to 6.5x lower operation completion times > in the measured workload. This motivates using a lock per instance > so that independent transitions can proceed concurrently. Well, an unpublished module isn't very useful for us. And an intense microbenchmark isn't as interesting as a real-world workload. So can you please provide more details about what "our workload" is doing and some measurements on how much this patch improves that workload? > @@ -109,6 +110,8 @@ struct percpu_ref { > */ > unsigned long percpu_count_ptr; > > + spinlock_t percpu_ref_switch_lock; > + > /* > * 'percpu_ref' is often embedded into user structure, and only > * 'percpu_count_ptr' is required in fast path, move other fields > diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c > index f8d9068..5d46602 100644 > --- a/lib/percpu-refcount.c > +++ b/lib/percpu-refcount.c > @@ -36,7 +36,6 @@ > > #define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1)) > > -static DEFINE_SPINLOCK(percpu_ref_switch_lock); lol, yeah, that'll do it! It does grow percpu_ref by 4 bytes, but LLM estimates that a busy machine will have 10k-100k of these floating around so not to bad. And I'm told that this will cause io_ring_ctx's hot fields to spill into a second cacheline, but that can be fixed up by reordering io_ring_ctx fields. Alternatively, this looks like somewhere where a hashed spinlock will work well. A static array of 32(?) cacheline_aligned spinlocks, hash the percpu_ref* onto one of those. That'll get 90% of the benefit without growing percpu_ref. ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref 2026-11-25 19:35 ` Andrew Morton @ 2026-11-25 21:46 ` Egor Shalashnov 0 siblings, 0 replies; 4+ messages in thread From: Egor Shalashnov @ 2026-11-25 21:46 UTC (permalink / raw) To: Andrew Morton; +Cc: linux-mm, dennis, tj, cl, zhengqi.arch, linux-kernel On 25/11/2026 22:35, Andrew Morton wrote: > Well, an unpublished module isn't very useful for us. > > And an intense microbenchmark isn't as interesting as a real-world > workload. So can you please provide more details about what "our > workload" is doing and some measurements on how much this patch > improves that workload? The workload is a full-volume discard on an out-of-tree storage block device backed by NVMe drives. The test machine has 128 logical CPUs. The device maintains a percpu_ref for each independently managed logical region of a thin volume. Normal I/O holds a reference while accessing that region. To process a discard, we kill the affected references and wait for outstanding I/O to drain before removing the mappings. After the metadata updates complete, we reinitialize the references so the regions can accept I/O again. A large discard repeats this lifecycle for many regions, with different regions processed concurrently on multiple CPUs. Although the regions are independent, their kill and reinit operations contend on the global percpu_ref_switch_lock. We measured the elapsed time of blkdiscard over the entire volume, refilling the volume before each run. Volume Global lock Per-reference lock Speedup 1 TB 3.424 s 1.218 s 2.81x 8 TB 27.943 s 6.133 s 4.56x 16 TB 56.021 s 8.591 s 6.52x > Alternatively, this looks like somewhere where a hashed spinlock will > work well. A static array of 32(?) cacheline_aligned spinlocks, hash > the percpu_ref* onto one of those. That'll get 90% of the benefit > without growing percpu_ref. This might be a better approach. I'll compare it with the global-lock and per-reference-lock variants and report the results. ^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref 2026-11-25 15:55 ` [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref Egor Shalashnov 2026-11-25 19:35 ` Andrew Morton @ 2026-11-26 15:56 ` sashiko-bot 1 sibling, 0 replies; 4+ messages in thread From: sashiko-bot @ 2026-11-26 15:56 UTC (permalink / raw) To: Egor Shalashnov, linux-mm; +Cc: dennis, tj, cl, akpm, zhengqi.arch Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] percpu_ref: global percpu_ref_switch_waitq still serializes mode switches - [Medium] percpu_ref: switch lock in struct percpu_ref bloats hot embedding cachelines -- commit d37012ad38aa40d26d6d73947c79cfb449eb07ba Author: Qi Zheng <qi.zheng@linux.dev> percpu_ref: make percpu_ref_switch_lock per percpu_ref This commit replaces the global percpu_ref_switch_lock with a per-instance spinlock inside struct percpu_ref to avoid lock contention across concurrent mode switches. > diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h > index 1e3212e2c8271..4582e798de8a6 100644 > --- a/include/linux/percpu-refcount.h > +++ b/include/linux/percpu-refcount.h [ ... ] > @@ -109,6 +110,8 @@ struct percpu_ref { > */ > unsigned long percpu_count_ptr; > > + spinlock_t percpu_ref_switch_lock; > + > /* > * 'percpu_ref' is often embedded into user structure, and only > * 'percpu_count_ptr' is required in fast path, move other fields [Severity: Medium] Should percpu_ref_switch_lock be placed in struct percpu_ref_data instead of struct percpu_ref? As noted in the comment right below, commit 2b0d3d3e4fcf ("percpu_ref: reduce memory footprint of percpu_ref in fast path") moved slow-path fields into struct percpu_ref_data to keep struct percpu_ref at 16 bytes for structures that embed it in hot cachelines. Adding percpu_ref_switch_lock here grows struct percpu_ref to 24 bytes on 64-bit SMP (and up to 88 bytes with lock debugging or PREEMPT_RT). In struct io_ring_ctx, for example, the first ____cacheline_aligned_in_smp read-mostly block is packed to 64 bytes, so expanding refs pushes notify_method and sq_thread_idle into a second cacheline. > diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c > index f8d90689af9f5..5d466026e5251 100644 > --- a/lib/percpu-refcount.c > +++ b/lib/percpu-refcount.c > @@ -36,7 +36,6 @@ > > #define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1)) > > -static DEFINE_SPINLOCK(percpu_ref_switch_lock); > static DECLARE_WAIT_QUEUE_HEAD(percpu_ref_switch_waitq); [ ... ] > @@ -267,7 +267,7 @@ static void __percpu_ref_switch_mode(struct percpu_ref *ref, > { > struct percpu_ref_data *data = ref->data; > > - lockdep_assert_held(&percpu_ref_switch_lock); > + lockdep_assert_held(&ref->percpu_ref_switch_lock); > > /* > * If the previous ATOMIC switching hasn't finished yet, wait for > @@ -275,7 +275,7 @@ static void __percpu_ref_switch_mode(struct percpu_ref *ref, > * isn't in progress, this function can be called from any context. > */ > wait_event_lock_irq(percpu_ref_switch_waitq, !data->confirm_switch, > - percpu_ref_switch_lock); > + ref->percpu_ref_switch_lock); [Severity: Medium] Does leaving percpu_ref_switch_waitq global keep the mode-switch path serialized on a shared spinlock? Every percpu-to-atomic transition completes in percpu_ref_call_confirm_rcu(), which unconditionally calls wake_up_all(&percpu_ref_switch_waitq). That acquires the global percpu_ref_switch_waitq.lock even when no task is waiting, and wakes waiters across all unrelated percpu_ref instances when tasks do wait. -- Sashiko AI review · https://sashiko.dev/#/patchset/6c943754-05b5-4017-9145-0d145766909a@gmail.com?part=1 ^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-26 15:56 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <01612865-faa0-4fe5-8184-a177f104843f@gmail.com>
2026-11-25 15:55 ` [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref Egor Shalashnov
2026-11-25 19:35 ` Andrew Morton
2026-11-25 21:46 ` Egor Shalashnov
2026-11-26 15:56 ` sashiko-bot
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox