From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.221.54 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795961464; b=qcsSWJatMkAUYye0l50FmGd54IwKYRIDGMm2e8pVr9ntUsQ4k6LIBm6L0EMbvaLTR0Jb ycO0w5SlvAjyT1O4/lQ05UdLHEE12zyqzFlYHz+vT0mqO5egn3uxb+i5I2+l3yqDVNFIM wLowipMV7nUUY7fqMCHwFEWyNlZ/UM6oCJZdnQwHn5+NjGw3i3JBd5zIgXqrDRbjuXVHZ zuIRHT8sktHjWrOR9ntBVZ4FgUQRsTvVmVqD26T3wx9A6Bjw75hvvTCeQ0Jm1BwOm1OPX hxbVRc6UteR2agW7OLXSVFTKy9gl36wdzn0L4DuXDch4xipcMy7xIfn6cl7jtVD2FUA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795961464; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Message-ID:Date:MIME-Version:User-Agent:Content-Language:To:Cc:From: Subject:Content-Type:Content-Transfer-Encoding; bh=cNS9UWC/UcgpgHN7NJvqaXmMZfy/I7jXaN2Qkz9midk=; b=Y+qSW2uMGbNmdP0bYlZAcyzpDzKmv/c5soeLczG11hHWpE8n00fxbnngFtvkCSKvFVyU 1foprK/qshEHBV+ybwpswfM6HxfomESedwD7CLVm2dxTLqvQbBhvclW3QkEdeGm2Ag4LF QoVe0gONerJDzCcd3g3IXOZDwt+3rPUUrnKNSycAGSXR7V9Wve3eyCPulbWuvNIaZrmmu zi8ZC3RSfX5YWmu1lwS29/BYJpQ1mkGEmFIVF+Py/sphI1ITliLJVDbBwhYCEm5HDwzjh OGOJngsnqUQd6x/yBLzG26NSdb1a+nijsKy8jIOlYyYQfLk0b8IMDKWJ2v+XziBdEsA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=R35sd8bx; arc=none smtp.remote-ip=209.85.221.54 Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C357B606A31C for ; Sun, 29 Nov 2026 14:11:03 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C357B606A31C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.a=rsa-sha256 header.s=20260707 header.b=R35sd8bx Received: by mail-wr1-f54.google.com with SMTP id ffacd0b85a97d-4a3e706b5dfso6003135f8f.2 for ; Sun, 29 Nov 2026 06:11:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795961462; x=1796566262; darn=vger.kernel.org; h=content-transfer-encoding:content-type:subject:from:cc:to :content-language:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=cNS9UWC/UcgpgHN7NJvqaXmMZfy/I7jXaN2Qkz9midk=; b=R35sd8bxsi9Vhh0PWoIlzDZLuiuxswluGYMAzENpnUFK+EvXqzn25s1uJOQ8ezeLs8 N0yDvEidwNq1hdomiW6WXlpWTLF+nCVCDIWHGpBoeoJg1iACUS/Mvgj8ol9yn3C2Pc1+ +hxiczlNEs/DH47Q18sjfsd42aX3XFsBH0Ph6ihMuZm2PZXEOlmgkirIPLDTbvZr5VKv gDbfpd8iaHuJWnQVEh6jlB4NJImTnZ9TWl6q5cJ1RZdc7AVwjfEe1ZBJGBZJnLIpIHq9 W/aCfPLpRoJjO8vpcck546nFt0gUvFKgThSm9aUxfW2eN5DTuRTGb3R4OMXRzSbjz50p kW1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795961462; x=1796566262; h=content-transfer-encoding:content-type:subject:from:cc:to :content-language:user-agent:mime-version:date:message-id:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=cNS9UWC/UcgpgHN7NJvqaXmMZfy/I7jXaN2Qkz9midk=; b=sYVIBehCNmO/tAmWoGyxXrMcLq2YlL96Im7DtVzep1lNmOg8adUCYkXy5HKACNmxBC rR22ExvwsdVq8U3HOGXeqvZ6+vF1S9BZFwQBA/aTbP2tHhmoq3Yd2uRf0l0B/kEumwb5 Gx9mPJgdHetPkVGyIAWQMqtpo83EiCI6v6P0aejX/pDpoIgrRWfWCtZdDLuB6rcagbRa eaOMzx6PEh92RRDc3SRLYqMifr9fG35glgwQYgzXI+wGQd0mVrlVQ3xrNjiM8ZtZiSQ5 fDwLsqNGDWO103gnFdE4B8jgUtdqkSaPOv0EAS3fiq4wGeKl94PShpNMZBrsqwLBlvvj 7Cbg== X-Forwarded-Encrypted: i=1; AL+hErHzpRtxDahvdl97jUYysV7HujCkgHpn04ZHiCd6fYrvIrMr6JL779UGB+O7Au2drjG4eNMbLh2yzAdVa5E=@vger.kernel.org X-Gm-Message-State: APuU4Z+eaYGRjMGWMzeaLyIN18bjqvJM3xKvvP32mIlOoRs0DMdJLOA5 8PGWhc4uWiPWHdEWK6Hon0EVdzB6pC+CdsqmnldAXrKyyHlV6yyh71PG X-Gm-Gg: AZFdTZcqM4YixKN/GZxAwiNoX/Jru9pPjJ0uVyw0HL9kgSw5/zzoKV72F5NFsrSFuRN DQ8XkEIiN/S0ZGWT9nTa7EINbNLr5oLfB61Lwb3kT5tOv93Qfc7hv/+QVkskRUzUclU7D5nthFs yQUtQzxpMGu6rIv9Vxra7bXiqpY3xVk3ncTxo4Mrm5D/Z1/Lvz8qIcUmo402csHj81LcloGNs1o NOI5akXGIkpnGPS0xR+JMin3dPMzyNQIU5DwDUyscS7jIpzOnHoxnEy/cbdnZOI8YiJEA36tOwr 84p6wAKoZ2CR3l1K/pMF52AKiDhtGySUxS0q2IjDtCnT6Lfa3KyECMTNsopHmSbNdmXNXCRkm4L GRU3vvEhcy1cRbQ+iC3Yx2iDTd2y7hglA57nd04DmoPh7fCbfjCThxQy9rRvAbOIdYTW3w4DG9T G1CahZE0+7UWzHw2S1fL27dNDb/qGv4aJ1IKB5szAXULpd8x8dZsFomt5p+FDgQuGhPb4352c3i dqpzL2Xi9EHNDOLVleoddLzb447PqA/RuSuDfhibp+kWwb+67PtSDPl4gEFiVDSHSG5HHdSG3zA GW+9lOXK95w2yBorGro09j/qdLLyeOBuFgr31tdSNKBI7tDm03E49XvV/oJRpUM+ X-Received: by 2002:a05:6000:250a:b0:49e:1601:2fa1 with SMTP id ffacd0b85a97d-4abb514618bmr70181128f8f.33.1795961461884; Sun, 29 Nov 2026 06:11:01 -0800 (PST) Received: from [192.168.68.162] ([87.237.44.52]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-4ab6b110c3csm56334815f8f.36.2026.11.29.06.11.00 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 29 Nov 2026 06:11:01 -0800 (PST) Message-ID: Date: Sun, 29 Nov 2026 17:10:59 +0300 X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, dennis@kernel.org, tj@kernel.org, cl@gentwo.org, linux-kernel@vger.kernel.org From: Egor Shalashnov Subject: [PATCH v2] percpu_ref: replace the global switch lock with hashed locks Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit 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. Signed-off-by: Egor Shalashnov --- Changes since v1: - Replace the per-reference lock with a hashed array of 64 cacheline-separated spinlocks, following Andrew Morton's suggestion. - Initialize the lock array with spin_lock_init() from a pure initcall. v1: https://lore.kernel.org/linux-mm/6c943754-05b5-4017-9145-0d145766909a@gmail.com/ 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..2c89bcb56e88 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 +#include #include #include #include #include #include #include +#include /* * Initially, a percpu refcount is just a set of percpu counters. Initially, we @@ -36,7 +39,26 @@ #define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1)) -static DEFINE_SPINLOCK(percpu_ref_switch_lock); +#define NR_LOCKS 64 + +/* + * Ensure each lock is in a separate cacheline. + */ +static union { + /* Serializes mode switching and teardown within this hash bucket. */ + spinlock_t lock; + char pad[L1_CACHE_BYTES]; +} percpu_switch_lock[NR_LOCKS] __cacheline_aligned_in_smp; + +static inline spinlock_t *lock_addr(const struct percpu_ref *ref) +{ + unsigned long addr = (unsigned long)ref; + + addr >>= L1_CACHE_SHIFT; + addr ^= (addr >> 8) ^ (addr >> 16); + return &percpu_switch_lock[addr & (NR_LOCKS - 1)].lock; +} + static DECLARE_WAIT_QUEUE_HEAD(percpu_ref_switch_waitq); static unsigned long __percpu *percpu_count_ptr(struct percpu_ref *ref) @@ -129,6 +151,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 +160,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 +288,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 +299,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 +330,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 +377,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 +409,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 +422,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 +436,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 +444,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 +490,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 +503,16 @@ 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); + +static int __init init_percpu_ref_lock(void) +{ + int i; + + for (i = 0; i < NR_LOCKS; ++i) + spin_lock_init(&percpu_switch_lock[i].lock); + return 0; +} +pure_initcall(init_percpu_ref_lock); base-commit: 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046 -- 2.54.0