From: Egor Shalashnov <shalasheg@gmail.com>
To: linux-mm@kvack.org
Cc: dennis@kernel.org, tj@kernel.org, cl@gentwo.org,
akpm@linux-foundation.org, zhengqi.arch@bytedance.com,
linux-kernel@vger.kernel.org
Subject: [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref
Date: Wed, 25 Nov 2026 18:55:55 +0300 [thread overview]
Message-ID: <6c943754-05b5-4017-9145-0d145766909a@gmail.com> (raw)
In-Reply-To: <01612865-faa0-4fe5-8184-a177f104843f@gmail.com>
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
parent reply other threads:[~2026-11-25 15:55 UTC|newest]
Thread overview: expand[flat|nested] mbox.gz Atom feed
[parent not found: <01612865-faa0-4fe5-8184-a177f104843f@gmail.com>]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=6c943754-05b5-4017-9145-0d145766909a@gmail.com \
--to=shalasheg@gmail.com \
--cc=akpm@linux-foundation.org \
--cc=cl@gentwo.org \
--cc=dennis@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=tj@kernel.org \
--cc=zhengqi.arch@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
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®