* [PATCH v2] percpu_ref: replace the global switch lock with hashed locks
@ 2026-11-29 14:10 Egor Shalashnov
2026-11-29 20:04 ` Andrew Morton
2026-11-29 21:34 ` sashiko-bot
0 siblings, 2 replies; 3+ messages in thread
From: Egor Shalashnov @ 2026-11-29 14:10 UTC (permalink / raw)
To: linux-mm; +Cc: akpm, dennis, tj, cl, linux-kernel
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 a hashed array of 64 cacheline-separated
spinlocks to fix this issue without introducing a separate
spinlock for each percpu_ref.
Signed-off-by: Egor Shalashnov <shalasheg@gmail.com>
---
Changes since v1:
- Replace the per-reference lock with a hashed array of 64
cacheline-separated spinlocks, following Andrew Morton's
suggestion.
- Initialize the lock array with spin_lock_init() from a pure
initcall.
v1:
https://lore.kernel.org/linux-mm/6c943754-05b5-4017-9145-0d145766909a@gmail.com/
lib/percpu-refcount.c | 69 +++++++++++++++++++++++++++++++++----------
1 file changed, 54 insertions(+), 15 deletions(-)
diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c
index f8d90689af9f..2c89bcb56e88 100644
--- a/lib/percpu-refcount.c
+++ b/lib/percpu-refcount.c
@@ -1,12 +1,15 @@
// SPDX-License-Identifier: GPL-2.0-only
#define pr_fmt(fmt) "%s: " fmt, __func__
+#include <linux/cache.h>
+#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/wait.h>
#include <linux/slab.h>
#include <linux/mm.h>
#include <linux/percpu-refcount.h>
+#include <linux/spinlock.h>
/*
* Initially, a percpu refcount is just a set of percpu counters. Initially, we
@@ -36,7 +39,26 @@
#define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1))
-static DEFINE_SPINLOCK(percpu_ref_switch_lock);
+#define NR_LOCKS 64
+
+/*
+ * Ensure each lock is in a separate cacheline.
+ */
+static union {
+ /* Serializes mode switching and teardown within this hash bucket. */
+ spinlock_t lock;
+ char pad[L1_CACHE_BYTES];
+} percpu_switch_lock[NR_LOCKS] __cacheline_aligned_in_smp;
+
+static inline spinlock_t *lock_addr(const struct percpu_ref *ref)
+{
+ unsigned long addr = (unsigned long)ref;
+
+ addr >>= L1_CACHE_SHIFT;
+ addr ^= (addr >> 8) ^ (addr >> 16);
+ return &percpu_switch_lock[addr & (NR_LOCKS - 1)].lock;
+}
+
static DECLARE_WAIT_QUEUE_HEAD(percpu_ref_switch_waitq);
static unsigned long __percpu *percpu_count_ptr(struct percpu_ref *ref)
@@ -129,6 +151,7 @@ static void __percpu_ref_exit(struct percpu_ref *ref)
*/
void percpu_ref_exit(struct percpu_ref *ref)
{
+ spinlock_t *lock = lock_addr(ref);
struct percpu_ref_data *data = ref->data;
unsigned long flags;
@@ -137,11 +160,11 @@ void percpu_ref_exit(struct percpu_ref *ref)
if (!data)
return;
- spin_lock_irqsave(&percpu_ref_switch_lock, flags);
+ spin_lock_irqsave(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(lock, flags);
kfree(data);
}
@@ -265,9 +288,10 @@ static void __percpu_ref_switch_to_percpu(struct percpu_ref *ref)
static void __percpu_ref_switch_mode(struct percpu_ref *ref,
percpu_ref_func_t *confirm_switch)
{
+ spinlock_t *lock = lock_addr(ref);
struct percpu_ref_data *data = ref->data;
- lockdep_assert_held(&percpu_ref_switch_lock);
+ lockdep_assert_held(lock);
/*
* If the previous ATOMIC switching hasn't finished yet, wait for
@@ -275,7 +299,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);
+ *lock);
if (data->force_atomic || percpu_ref_is_dying(ref))
__percpu_ref_switch_to_atomic(ref, confirm_switch);
@@ -306,14 +330,15 @@ static void __percpu_ref_switch_mode(struct percpu_ref *ref,
void percpu_ref_switch_to_atomic(struct percpu_ref *ref,
percpu_ref_func_t *confirm_switch)
{
+ spinlock_t *lock = lock_addr(ref);
unsigned long flags;
- spin_lock_irqsave(&percpu_ref_switch_lock, flags);
+ spin_lock_irqsave(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(lock, flags);
}
EXPORT_SYMBOL_GPL(percpu_ref_switch_to_atomic);
@@ -352,14 +377,15 @@ EXPORT_SYMBOL_GPL(percpu_ref_switch_to_atomic_sync);
*/
void percpu_ref_switch_to_percpu(struct percpu_ref *ref)
{
+ spinlock_t *lock = lock_addr(ref);
unsigned long flags;
- spin_lock_irqsave(&percpu_ref_switch_lock, flags);
+ spin_lock_irqsave(lock, flags);
ref->data->force_atomic = false;
__percpu_ref_switch_mode(ref, NULL);
- spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
+ spin_unlock_irqrestore(lock, flags);
}
EXPORT_SYMBOL_GPL(percpu_ref_switch_to_percpu);
@@ -383,9 +409,10 @@ EXPORT_SYMBOL_GPL(percpu_ref_switch_to_percpu);
void percpu_ref_kill_and_confirm(struct percpu_ref *ref,
percpu_ref_func_t *confirm_kill)
{
+ spinlock_t *lock = lock_addr(ref);
unsigned long flags;
- spin_lock_irqsave(&percpu_ref_switch_lock, flags);
+ spin_lock_irqsave(lock, flags);
WARN_ONCE(percpu_ref_is_dying(ref),
"%s called more than once on %ps!", __func__,
@@ -395,7 +422,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(lock, flags);
}
EXPORT_SYMBOL_GPL(percpu_ref_kill_and_confirm);
@@ -409,6 +436,7 @@ EXPORT_SYMBOL_GPL(percpu_ref_kill_and_confirm);
*/
bool percpu_ref_is_zero(struct percpu_ref *ref)
{
+ spinlock_t *lock = lock_addr(ref);
unsigned long __percpu *percpu_count;
unsigned long count, flags;
@@ -416,12 +444,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(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(lock, flags);
return count == 0;
}
@@ -462,10 +490,11 @@ EXPORT_SYMBOL_GPL(percpu_ref_reinit);
*/
void percpu_ref_resurrect(struct percpu_ref *ref)
{
+ spinlock_t *lock = lock_addr(ref);
unsigned long __percpu *percpu_count;
unsigned long flags;
- spin_lock_irqsave(&percpu_ref_switch_lock, flags);
+ spin_lock_irqsave(lock, flags);
WARN_ON_ONCE(!percpu_ref_is_dying(ref));
WARN_ON_ONCE(__ref_is_percpu(ref, &percpu_count));
@@ -474,6 +503,16 @@ 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(lock, flags);
}
EXPORT_SYMBOL_GPL(percpu_ref_resurrect);
+
+static int __init init_percpu_ref_lock(void)
+{
+ int i;
+
+ for (i = 0; i < NR_LOCKS; ++i)
+ spin_lock_init(&percpu_switch_lock[i].lock);
+ return 0;
+}
+pure_initcall(init_percpu_ref_lock);
base-commit: 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046
--
2.54.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* Re: [PATCH v2] percpu_ref: replace the global switch lock with hashed locks
2026-11-29 14:10 [PATCH v2] percpu_ref: replace the global switch lock with hashed locks Egor Shalashnov
@ 2026-11-29 20:04 ` Andrew Morton
2026-11-29 21:34 ` sashiko-bot
1 sibling, 0 replies; 3+ messages in thread
From: Andrew Morton @ 2026-11-29 20:04 UTC (permalink / raw)
To: Egor Shalashnov; +Cc: linux-mm, dennis, tj, cl, linux-kernel
On Sun, 29 Nov 2026 17:10:59 +0300 Egor Shalashnov <shalasheg@gmail.com> wrote:
> 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 a hashed array of 64 cacheline-separated
> spinlocks to fix this issue without introducing a separate
> spinlock for each percpu_ref.
Nice, thanks.
Please summarize the microbenchmark speedups in the changelog?
And briefly describe the real-world workload - tell readers "how come
Egor is the first person to report this".
It would be nice to summarize observed improvements in that workload,
but I wouldn't spend a lot of time over this - the patch will obviously
help a lot.
> --- a/lib/percpu-refcount.c
> +++ b/lib/percpu-refcount.c
> @@ -36,7 +39,26 @@
>
> #define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1))
>
> -static DEFINE_SPINLOCK(percpu_ref_switch_lock);
> +#define NR_LOCKS 64
Let's remember CONFIG_SMP=n. I guess making this "1" on !SMP will
sufficiently take care of this.
Also
#else
static inline spinlock_t *lock_addr(const struct percpu_ref *ref)
{
return &percpu_switch_lock[0].lock;
}
#endif /* CONFIG_SMP */
I suspect/hope gcc will do exactly this when NR_LOCKS=1, but not if we
switch lock_addr() to hash_ptr() (below).
> +
> +/*
> + * Ensure each lock is in a separate cacheline.
> + */
> +static union {
> + /* Serializes mode switching and teardown within this hash bucket. */
> + spinlock_t lock;
> + char pad[L1_CACHE_BYTES];
> +} percpu_switch_lock[NR_LOCKS] __cacheline_aligned_in_smp;
I agree with Sashiko ;) Can we statically initialize this at compile
time and remove init_percpu_ref_lock().
L1_CACHE_BYTES is too small on machines where SMP_CACHE_BYTES is larger
(ARC and sparc64) - using SMP_CACHE_BYTES here would address that.
I think this:
static struct {
spinlock_t lock;
} ____cacheline_aligned_in_smp percpu_switch_lock[NR_LOCKS] = {
[0 ... NR_LOCKS - 1] = {
.lock = __SPIN_LOCK_UNLOCKED(percpu_switch_lock.lock),
},
};
will address these things.
> +static inline spinlock_t *lock_addr(const struct percpu_ref *ref)
> +{
> + unsigned long addr = (unsigned long)ref;
> +
> + addr >>= L1_CACHE_SHIFT;
> + addr ^= (addr >> 8) ^ (addr >> 16);
> + return &percpu_switch_lock[addr & (NR_LOCKS - 1)].lock;
> +}
<ai>The hash is correct for serialization, but it omits address bits 12
and 13 with 64-byte L1 lines. Four refs at the same offset in
consecutive 4 KiB pages can therefore collide.</ai>
Using hash_ptr() here should address.
^ permalink raw reply [flat|nested] 3+ messages in thread* Re: [PATCH v2] percpu_ref: replace the global switch lock with hashed locks
2026-11-29 14:10 [PATCH v2] percpu_ref: replace the global switch lock with hashed locks Egor Shalashnov
2026-11-29 20:04 ` Andrew Morton
@ 2026-11-29 21:34 ` sashiko-bot
1 sibling, 0 replies; 3+ messages in thread
From: sashiko-bot @ 2026-11-29 21:34 UTC (permalink / raw)
To: Egor Shalashnov, linux-mm; +Cc: akpm, dennis, tj, cl
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] percpu_ref: uninitialized percpu_switch_lock spinlocks before
pure_initcall()
--
commit a266d7241fd78d3a31f1a9d5601843fafd5034eb
Author: Egor Shalashnov <shalasheg@gmail.com>
percpu_ref: replace the global switch lock with hashed locks
This commit replaces the global percpu_ref_switch_lock with a hashed array
of 64 cacheline-aligned spinlocks initialized from a pure_initcall.
> diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c
> index f8d90689af9f5..2c89bcb56e885 100644
> --- a/lib/percpu-refcount.c
> +++ b/lib/percpu-refcount.c
[ ... ]
> @@ -36,7 +39,26 @@
>
> #define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1))
>
> -static DEFINE_SPINLOCK(percpu_ref_switch_lock);
> +#define NR_LOCKS 64
> +
> +/*
> + * Ensure each lock is in a separate cacheline.
> + */
> +static union {
> + /* Serializes mode switching and teardown within this hash bucket. */
> + spinlock_t lock;
> + char pad[L1_CACHE_BYTES];
> +} percpu_switch_lock[NR_LOCKS] __cacheline_aligned_in_smp;
[ ... ]
> @@ -474,6 +503,16 @@ void percpu_ref_resurrect(struct percpu_ref *ref)
> spin_unlock_irqrestore(&percpu_ref_switch_lock, flags);
> + spin_unlock_irqrestore(lock, flags);
> }
> EXPORT_SYMBOL_GPL(percpu_ref_resurrect);
> +
> +static int __init init_percpu_ref_lock(void)
> +{
> + int i;
> +
> + for (i = 0; i < NR_LOCKS; ++i)
> + spin_lock_init(&percpu_switch_lock[i].lock);
> + return 0;
> +}
> +pure_initcall(init_percpu_ref_lock);
[Severity: Medium]
Can a lock in percpu_switch_lock[] be acquired before init_percpu_ref_lock()
runs?
pure_initcall() only runs during do_basic_setup() -> do_initcalls(), after
start_kernel() and do_pre_smp_initcalls(). However, start_kernel() already
uses percpu_ref in cgroup_init() (for example, cgroup_setup_root() and
mem_cgroup_css_online()), where an early failure unwind calls
percpu_ref_kill() or percpu_ref_exit() and takes lock_addr(ref) while the
spinlock_t is still zeroed, triggering a SPIN_BUG_ON() splat or hang under
CONFIG_DEBUG_SPINLOCK and operating on an uninitialized lockdep_map or
rt_mutex_base.wait_lock.
Initializing percpu_switch_lock[] statically at compile time with
__SPIN_LOCK_UNLOCKED() (similar to atomic64_lock[] in lib/atomic64.c) would
avoid the uninitialized window and make the initcall unnecessary.
--
Sashiko AI review · https://sashiko.dev/#/patchset/c54762a0-ad7a-47ca-b260-29f44c4e1a33@gmail.com?part=1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-29 21:34 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 14:10 [PATCH v2] percpu_ref: replace the global switch lock with hashed locks Egor Shalashnov
2026-11-29 20:04 ` Andrew Morton
2026-11-29 21:34 ` sashiko-bot
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox