[PATCH v2] percpu_ref: replace the global switch lock with hashed locks

From: Egor Shalashnov

Date: Sun Nov 29 2026 - 09:11:07 EST


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@xxxxxxxxx>
---
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@xxxxxxxxx/

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