From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796002127; b=zgyyYlTQyNbPrZ3sj6ePO06z6RMv9OYHTOsbSUpvuXIMx0oCVDdPIjgppWKVSDPS02bk MrNgTRKUTIeaGw+8muKNh9hYG60J+Et5hzRn019dtZw9eNEykdf4sai5cEPk3StzjYO4P k+re29Z7MEsZGqukJmZM0JFtjIHFPpHfliYc+Hqi8tARtXgezMtJvnI171AA554u30Kf6 pAMIpxHdIk7ldDkI1U0gUlt+N8OeLf0uUdITO/jovIKJssSJ1ZGgE4d0eLYwKvk+lQ1+s qulT46IgERYk52mHxJnDD0a8ghM9cnn9M+eFFY61VbtxncGO872Omi6d6dE9EjI5QOw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796002127; h=Received:DKIM-Signature:Date:To:From:In-Reply-To:References:Subject: Message-Id; bh=H3CKkuiF7PWotDaxQ3Yc+dvNqgUKAjuoQeWhrETN5tA=; b=FXCJtfwpsUcRErPjQ9GCIMb9k8A86aB7EofHK2cG31rFtX+/g8GAdZ5T7yAM853K+DdB xBWsA3D6LE1KUHHiJ4gi4eLf68BCyr6MMIROQHgjApMtPHJRQwEl+QMOKSmAf+4cMYrds ac5HqMXLPo8xvHkQzY4Y8V+tIz7DcBpLP+nJeJfBPXR3HnRr/zPAXppWvSjCg+M6tnMxk u2JrNRhDu6kXKEDzXq6jsV/PbjSMWmf7JVkUOeakI+chMgI/STcr9yXxuw6HO4qYdXttD lm+A+nKqoAIo/ntiFUVxHDtdosIUgD9sNaKIbT0Qn2b86LDq6bm6EV4ievZ6QTxNmKA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=OMQvvlAe; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 19451606A31C for ; Mon, 30 Nov 2026 01:28:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.a=rsa-sha256 header.s=korg header.b=OMQvvlAe Received: by smtp.kernel.org (Postfix) with ESMTPSA id 937B61F000C1; Mon, 30 Nov 2026 01:28:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1796002126; bh=H3CKkuiF7PWotDaxQ3Yc+dvNqgUKAjuoQeWhrETN5tA=; h=Date:To:From:In-Reply-To:References:Subject; b=OMQvvlAeR3ZmNexnKXLYEvyl2xkjnDBIXGdAOE3+kKVKLLmU2Qjm8EX9vrA4qRcCH sacCqoprSnasLJ2eoRoF7BElsaGAJlRS2GyZ8KC5CMeHyXmVzY+1sXbVt450OkWOUf tPTvh63r1nZvu86ds86aUomaPsheSglw1ScQJhh0= Date: Sun, 29 Nov 2026 17:28:46 -0800 To: mm-commits@vger.kernel.org,tj@kernel.org,dennis@kernel.org,cl@gentwo.org, shalasheg@gmail.com,akpm@linux-foundation.org From: Andrew Morton In-Reply-To: <0100d9e7-140c-41f7-be26-f1148b101fe6@gmail.com> References: <0100d9e7-140c-41f7-be26-f1148b101fe6@gmail.com> Subject: + percpu_ref-replace-the-global-switch-lock-with-hashed-locks.patch added to mm-nonmm-unstable branch Message-Id: <20261130012846.937B61F000C1@smtp.kernel.org> X-Mailing-List: mm-commits@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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 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 Signed-off-by: Andrew Morton Cc: Dennis Zhou Cc: Tejun Heo Cc: Christoph Lameter 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 +#include #include #include #include #include #include #include +#include /* * 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