Skip to contentSkip to conversation

percpu_ref: make percpu_ref_switch_lock per percpu_ref

PATCH In review Egor Shalashnov 2 participants 2 messages Nov 25 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 1/1 percpu_ref: make percpu_ref_switch_lock per percpu_ref

From: Qi Zheng <[email protected]>

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.

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/[email protected]/

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(-)
include/linux/percpu-refcount.h +3 -0
index 1e3212e..4582e79 100644
@@ -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
lib/percpu-refcount.c +15 -15
index f8d9068..5d46602 100644
@@ -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

maintainer
#2 lore raw
Quoted 7 lines · On Wed, 25 Nov 2026 18:55:55 +0300 Egor Shalashnov <[email protected]> wrote:

From: Qi Zheng <[email protected]>

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.

Quoted 7 lines

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.

Quoted 10 lines

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.

Quoted 4 lines

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?

Quoted 14 lines
include/linux/percpu-refcount.h +2 -0
@@ -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
lib/percpu-refcount.c +0 -0
index f8d9068..5d46602 100644

lib/percpu-refcount.c line 39

Quoted 4 lines
lib/percpu-refcount.c +0 -1
@@ -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.