All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: mm-commits@vger.kernel.org,tj@kernel.org,dennis@kernel.org,cl@gentwo.org,
	shalasheg@gmail.com,akpm@linux-foundation.org
Subject: + percpu_ref-replace-the-global-switch-lock-with-hashed-locks.patch added to mm-nonmm-unstable branch
Date: Sun, 29 Nov 2026 17:28:46 -0800	[thread overview]
Message-ID: <20261130012846.937B61F000C1@smtp.kernel.org> (raw)
In-Reply-To: <0100d9e7-140c-41f7-be26-f1148b101fe6@gmail.com>


The patch titled
     Subject: percpu_ref: replace the global switch lock with hashed locks
has been added to the -mm mm-nonmm-unstable branch.  Its filename is
     percpu_ref-replace-the-global-switch-lock-with-hashed-locks.patch

This patch will shortly appear at
     https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/percpu_ref-replace-the-global-switch-lock-with-hashed-locks.patch

This patch will later appear in the mm-nonmm-unstable branch at
    git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
   a) Consider who else should be cc'ed
   b) Prefer to cc a suitable mailing list as well
   c) Ideally: find the original patch on the mailing list and do a
      reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various
branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Egor Shalashnov <shalasheg@gmail.com>
Subject: percpu_ref: replace the global switch lock with hashed locks
Date: Mon, 30 Nov 2026 02:43:26 +0300

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

Link: https://patch.msgid.link/0100d9e7-140c-41f7-be26-f1148b101fe6@gmail.com
Signed-off-by: Egor Shalashnov <shalasheg@gmail.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Cc: Dennis Zhou <dennis@kernel.org>
Cc: Tejun Heo <tj@kernel.org>
Cc: Christoph Lameter <cl@gentwo.org>

 lib/percpu-refcount.c |   69 +++++++++++++++++++++++++++++++---------
 1 file changed, 54 insertions(+), 15 deletions(-)

--- a/lib/percpu-refcount.c~percpu_ref-replace-the-global-switch-lock-with-hashed-locks
+++ a/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 per
  */
 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 *
 	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_percp
 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(str
 	 * 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(str
 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_a
  */
 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_p
 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_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_co
  */
 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_re
 		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_
 	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);
_

Patches currently in -mm which might be from shalasheg@gmail.com are

percpu_ref-replace-the-global-switch-lock-with-hashed-locks.patch


      parent reply	other threads:[~2026-11-30  1:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-29 23:43 [PATCH v3] percpu_ref: replace the global switch lock with hashed locks Egor Shalashnov
2026-11-30  1:28 ` Andrew Morton
2026-11-30  1:28 ` Andrew Morton [this message]

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=20261130012846.937B61F000C1@smtp.kernel.org \
    --to=akpm@linux-foundation.org \
    --cc=cl@gentwo.org \
    --cc=dennis@kernel.org \
    --cc=mm-commits@vger.kernel.org \
    --cc=shalasheg@gmail.com \
    --cc=tj@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.