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

From: Egor Shalashnov

Date: Sun Nov 29 2026 - 18:43:37 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.

The contention was observed during full-volume discard in a
thin-provisioning implementation in an out-of-tree module.
Logical regions are processed concurrently, each requiring its
reference to be killed, drained, and reinitialized after processing
discard. Reinitialization also clears the per-CPU counters while
holding the switch lock.

In module-local fio discard benchmarks with 128 jobs, one pinned
to each logical CPU, the 64-lock implementation gave the following
completion times in seconds:

Volume (TB) Global Hash64 Speedup
1 3.393 0.722 4.70x
2 5.039 1.399 3.60x
4 8.705 1.471 5.92x
8 16.955 2.834 5.98x
16 34.056 4.997 6.82x
32 67.177 9.467 7.10x
64 134.081 20.311 6.60x
128 263.881 40.135 6.57x
256 543.297 83.590 6.50x

Signed-off-by: Egor Shalashnov <shalasheg@xxxxxxxxx>
---
Changes since v2:
- Use a single lock without hashing on !CONFIG_SMP.
- Initialize the lock array statically and remove the initcall.
- Align each array element to an SMP cacheline boundary properly.
- Replace the custom hash with hash_ptr().
- Add the workload description and benchmark speedups to the changelog.

v2:
https://lore.kernel.org/linux-mm/c54762a0-ad7a-47ca-b260-29f44c4e1a33@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..58089913fda7 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/hash.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,36 @@

#define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1))

-static DEFINE_SPINLOCK(percpu_ref_switch_lock);
+#ifdef CONFIG_SMP
+#define NR_LOCKS 64
+#else
+#define NR_LOCKS 1
+#endif
+
+/*
+ * Ensure each lock is in a separate cacheline on SMP.
+ */
+static struct {
+ /* Serializes mode switching and teardown within this hash bucket. */
+ spinlock_t lock;
+} ____cacheline_aligned_in_smp percpu_switch_lock[NR_LOCKS] = {
+ [0 ... NR_LOCKS - 1] = {
+ .lock = __SPIN_LOCK_UNLOCKED(percpu_switch_lock.lock),
+ },
+};
+
+#ifdef CONFIG_SMP
+static inline spinlock_t *lock_addr(const struct percpu_ref *ref)
+{
+ return &percpu_switch_lock[hash_ptr(ref, ilog2(NR_LOCKS))].lock;
+}
+#else
+static inline spinlock_t *lock_addr(const struct percpu_ref *ref)
+{
+ return &percpu_switch_lock[0].lock;
+}
+#endif /* CONFIG_SMP */
+
static DECLARE_WAIT_QUEUE_HEAD(percpu_ref_switch_waitq);

static unsigned long __percpu *percpu_count_ptr(struct percpu_ref *ref)
@@ -129,6 +161,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 +170,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 +298,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 +309,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 +340,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 +387,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 +419,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 +432,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 +446,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 +454,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 +500,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 +513,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(lock, flags);
}
EXPORT_SYMBOL_GPL(percpu_ref_resurrect);

base-commit: 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046
--
2.54.0