From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.208.170 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1795995813; b=2anJqUP5xS6VHlMXEvgzROvpRDSOx3+hsYB3hGenqvyjExB2k/oK3Sk3ZAohPjlsfAiF UV/ppwP46d8mN93iSb0dOUzRWE8cE5/EvU0c6WNgyGpTXyXYDJahVGjfvLFmdK2PcQpcA BR6gEgTEc7cYDLMHGBs550ob4m2gR+pYDgTn19FVK8Yx5Ek4AUNsLUc/IknXC4Yh5vxLY wX1d552RoOTGUefMk/rfBY1sHwDagZ6za8655iw0DoF6uOSJhQPKE+HXGCZT5Px4tYw41 Hc6bCLjUisQWA7sH8VlH/u6ZyYI527FqpNTh1epdGqaaVz0RIJTykL8Q+zOq72mofpg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795995813; 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:From:Subject:To:Cc: Content-Language:Content-Type:Content-Transfer-Encoding; bh=kugTaDyBaroI5W024pHsAWRj6usAp9T65njNFIN0FMo=; b=npjAv9Y+OkM0NzaHLMUbjDfAIeLtZykmxq5py5e1Nz3+uRHwznZfU3/LzzE0YJWMPhe1 OYOucHvEJGMio+NULTkg4rteq9omUvYoIPiPdIiBzsmY6q/19W6/2kLX9v7/Ub7Arjz5r 66NkXvWYh5Kd3R45zkDvrtoPqs00RT3Vtx/G6HQI8lnW9mcERIpx1sCjrKp9GBGj4U5Ad BuFEEJOr8faWpxCsbOm0SiRTnEjsu+2WWoV97GQyatw5/6vUp31mRwDIh/KuXFdhrCC0H DfCZOcKanFltvZcf2gLWTe+dACY81mxFuPFBmdA1r6Z3XrQcuTcm55auJfzuzfpDJzQ== 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=GHrVtSaJ; arc=none smtp.remote-ip=209.85.208.170 Received: from mail-lj1-f170.google.com (mail-lj1-f170.google.com [209.85.208.170]) (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 DFA62606A33B for ; Sun, 29 Nov 2026 23:43:32 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org DFA62606A33B 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=GHrVtSaJ Received: by mail-lj1-f170.google.com with SMTP id 38308e7fff4ca-3ae1fd4252fso96922961fa.0 for ; Sun, 29 Nov 2026 15:43:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20260707; t=1795995811; x=1796600611; darn=vger.kernel.org; h=content-transfer-encoding:content-type:content-language:cc:to :subject:from:user-agent:mime-version:date:message-id:from:to:cc :subject:date:message-id:reply-to:content-type; bh=kugTaDyBaroI5W024pHsAWRj6usAp9T65njNFIN0FMo=; b=GHrVtSaJV4fOfVUW7taLKHgj2SopVqOEMUxTuddjVQcaRg7VVU7lKbtS/75IjQQucY jIL4a2Lt+S/GB1ccRZ6eSOd1zvXFQmSlbJAZl1vjsylDoNvPQSuapx6WoPEfDqukw+6v 2BXB4CB/VepBTUjtjCdMjZsxO2peUXw9yf4IbNSnnvD+j87JEs0QRcD9VzXHZp4N1r3F HJUl08eVRt3dekNJ3GPKxNoig5H+ct25a2M9+YMGYVqLdtng5mQFQHJoAj8UNDRvsoNi JAE24c0d14a/x5YSy8ws1dNDudVU4aQP+B12eUhQ4DZPiU72fkl+oDOFhKmKzWxxJryj uONQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1795995811; x=1796600611; h=content-transfer-encoding:content-type:content-language:cc:to :subject:from: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=kugTaDyBaroI5W024pHsAWRj6usAp9T65njNFIN0FMo=; b=ONUzDUc+Imt42DzAAZoXxuOmTnMU8bUt7vcBCcQzsY25ua6yP+YOPQrK0D7uhxDTLS TbXkRo8ImuvfT9x0gM8W/+lIAkmbGjsqC1iZz1+oEwJ4AtiXRGYuY2JPt/b+E8lijjVU eT+n/GfQhtZC5ilfZP9056hXh68eGkmWWRHZg8XLSB9swMHolRoSO5GBEbj2bqJwZfG5 99bfpnOEublJVTe+hJjgNusHjx6eDdltjeCUaAaP5EoNm3EvQbSgrjGKjiLZ1mo3Mt06 1koaio5L3NooYb/5HPMdM9+RMIRtQFleO0cFlqJYomarwuLZgl9ktyGcsLKv7VUNlBfR NVdg== X-Forwarded-Encrypted: i=1; AL+hErGDkUHz70WNQsbDs/irqUvRVFP4/wRYPOzh0+oEoO/rrmWZifpJF49X8fLIT2B/eDzKKCSSUxySvB2qDsA=@vger.kernel.org X-Gm-Message-State: APuU4Z93mruJ8iissp3n5liD7XXhjV+57w+kKy3zUHSZ/L9Xk8viV2v9 FQbQzY6eAhh1+TCX0svrL6X14xTSkVh0VXcRzHqCB1dBptenKsRkGlog X-Gm-Gg: AZFdTZdvP2zunPqnjKW9Y29wbzQUFglrcbfOTDu2CTB80il7wqxK9+HO/g2+mRDGSa/ WJidHZytyaA/QKdRu1SHsYmkfW86lT9lc+bTSmc0pgoB14TXo0nweicGkvbbl+LpWwiktP88TPE iGuveFi9KRqnZa+D2XkZkMszvb1FX6Mq5mjr4aSix0RSdgwlE60yDW5phOf1IRdqxbEyhMh83h3 PFN092+FxyXuPLSs+w8lnFZUSO74Pc2IZ4WtpPSLm5+ZfqaqLmK6lNrISzgd2Nvfiz16N5W1wr8 b43D2RrE8VzMatCtcUxUP5ZLiBmpeqaUlzu6GQRYXx1lLLrF7uF40yfL+J569mONr1KbGNcJeq7 P1OmNP0zty6Z+GzH6aT/u0HaeB6PGSaXZDyfIBGpuEZsS3o/HOyWm5/5bmqXagV007mHpxxUDkv SbDAhEH4B2CD96fqLJZP/OGVoylw+X/9OfBnujQs75Bag3WqG/U3zTmRCFPwmya54tUf1sU/Nlm I+zwNLTeHPsKQStcZjfstQk7h9Kc7AJskyk0O9ftjgYJdnR9MjovcEjqQ8SkFW6gr+7NISIBXdu BNUZ5ITtZt+0GDL5IB2pLh2jawsIWFi/fkH3GRuPUvHwy2/bS8cXAyJmC/K+1aw= X-Received: by 2002:a2e:be87:0:b0:3ae:25c8:dca4 with SMTP id 38308e7fff4ca-3ae7fa429f1mr90996951fa.26.1795995810902; Sun, 29 Nov 2026 15:43:30 -0800 (PST) Received: from [172.20.10.6] ([31.173.87.203]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-3ae8050fdeasm84701541fa.15.2026.11.29.15.43.27 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 29 Nov 2026 15:43:29 -0800 (PST) Message-ID: <0100d9e7-140c-41f7-be26-f1148b101fe6@gmail.com> Date: Mon, 30 Nov 2026 02:43:26 +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 From: Egor Shalashnov Subject: [PATCH v3] percpu_ref: replace the global switch lock with hashed locks To: linux-mm@kvack.org Cc: akpm@linux-foundation.org, dennis@kernel.org, tj@kernel.org, cl@gentwo.org, linux-kernel@vger.kernel.org Content-Language: en-US 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. 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 Signed-off-by: Egor Shalashnov --- Changes since v2: - Use a single lock without hashing on !CONFIG_SMP. - Initialize the lock array statically and remove the initcall. - Align each array element to an SMP cacheline boundary properly. - Replace the custom hash with hash_ptr(). - Add the workload description and benchmark speedups to the changelog. v2: https://lore.kernel.org/linux-mm/c54762a0-ad7a-47ca-b260-29f44c4e1a33@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..58089913fda7 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,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 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 +170,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 +298,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 +309,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 +340,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 +387,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 +419,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 +432,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 +446,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 +454,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 +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_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); base-commit: 7cd8495c0f16275c4f9abe1cef09a7b5fcd6d046 -- 2.54.0