* [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
0 siblings, 1 reply; 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 [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
0 siblings, 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
2026-11-29 14:07 ` Egor Shalashnov
0 siblings, 1 reply; 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 21:46 ` Egor Shalashnov
@ 2026-11-29 14:07 ` Egor Shalashnov
0 siblings, 0 replies; 4+ messages in thread
From: Egor Shalashnov @ 2026-11-29 14:07 UTC (permalink / raw)
To: Andrew Morton; +Cc: linux-mm, dennis, tj, cl, zhengqi.arch, linux-kernel
On 26/11/2026 00:46, Egor Shalashnov wrote:
> This might be a better approach. I'll compare it with the global-lock
> and per-reference-lock variants and report the results.
I compared the global switch lock, per-reference locks, and hashed
lock arrays with 16, 32, 64 and 128 cacheline-separated locks.
The machine has 128 logical CPUs and runs Fedora kernel
7.3.3-200.fc44.x86_64. Volumes were prefilled before each run.
To increase lock contention, fio 3.40 used io_uring with 128 jobs,
one pinned to each logical CPU. Each job discarded a separate,
non-overlapping range; together they covered the entire volume.
The maximum discard request size was 4095 MiB.
Full-volume fio discard completion times, in seconds:
Volume Global Per-ref Hash16 Hash32 Hash64 Hash128
1 TB 3.393 0.675 0.838 0.759 0.722 0.719
2 TB 5.039 1.315 1.470 1.423 1.399 1.399
4 TB 8.705 1.406 1.555 1.471 1.471 1.485
8 TB 16.955 2.667 2.991 2.835 2.834 2.832
16 TB 34.056 5.263 6.122 5.015 4.997 5.172
32 TB 67.177 9.755 12.680 10.145 9.467 9.646
64 TB 134.081 21.421 24.058 19.539 20.311 19.009
128 TB 263.881 40.170 48.588 38.663 40.135 39.236
256 TB 543.297 80.053 91.324 78.679 83.590 77.425
I also tested full-volume discard using a single blkdiscard process.
Volume Global Per-ref Hash16 Hash32 Hash64 Hash128
1 TB 2.722 1.018 1.268 1.167 1.117 1.017
2 TB 7.085 1.871 2.573 2.373 2.121 1.821
4 TB 14.354 3.025 4.932 4.179 3.878 3.427
8 TB 28.391 6.186 9.544 8.342 7.441 6.687
16 TB 56.520 11.655 19.124 16.265 14.561 13.311
32 TB 92.209 20.629 27.544 22.931 21.123 26.303
64 TB 162.383 39.483 54.974 48.209 43.342 40.180
128 TB 321.230 90.283 115.726 102.877 87.113 94.157
256 TB 556.408 181.729 231.681 210.062 177.198 165.985
The 64-lock implementation gives results close to the per-reference
implementation. I have prepared that version for the next revision.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-29 14:07 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-29 14:07 ` Egor Shalashnov
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®