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=1795982698; b=1UkMt+/CFI2I28UMr3+61j78f9Ovijw/8jW0helSbQNFfni8qgoNuYLXk5fSFyUtDthP G4HL2ckWwAvoQ/6s3CToGuHNdY6g6tYYkfFMI3p+ai25szHmlY0VZ7FK8XK5IwxAjWOwQ FSqV5lK1Z0oLH+FxxWnpuSEaQVaMH9RYvYq0UXhqCMsM5TJRObuJMZW3piIPfngjQ+/5T kOeMbaMqF4cfn0oumuw6mkxGTgvowZgozqZccJmpARsc9SxZSw6eHtUhxlMaVuMEAWlvV Gtvr4orFnoWS5Csfol81K5Vxb0LDCEZQ5iLB/+l4c8TK4qdSww2Tp1kAUEYaRtgLgDA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795982698; h=Received:DKIM-Signature:Date:From:To:Cc:Subject:Message-Id: In-Reply-To:References:X-Mailer:Mime-Version:Content-Type: Content-Transfer-Encoding; bh=MTrKmNfeyecw8NkSs5nJHxixrLE3I96V/Q0jGXyrUwg=; b=G+sw96vrxrLXVpic/f0XJD/Jw8/HQqTmnmFDw2BzXw3Zc1vXIsXnIVO4MyOAoGxPHzyp K7nfvHQsp5duwyz8zOEg1JbuwGWGGD8fzVBwDosU9Lr8HhhNlznCKYqyPtBfPLifQv00V Zi434DWfRjBDHqQiXOvtI1Hqm4cTUv9xyePs0CdA5cJsoylfYr94gRRxJ45MTYuwUY31Q mMZhRL+iODjznd8UNSpYuYBYF171Go+MiC3IlUoeRABK95QB0Q4XogBfrx3WdfC/pihJ3 FQg+rDJIUTnymmngR0QGWcbAervxrAcLNAXxz17+oNPa6la1rb+Pq9Gutn5d/Ef20xg== 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="OTr/lBRI"; 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 A1859606A323 for ; Sun, 29 Nov 2026 20:04:58 +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=OTr/lBRI Received: by smtp.kernel.org (Postfix) with ESMTPSA id 823841F000C1; Sun, 29 Nov 2026 20:04:57 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux-foundation.org; s=korg; t=1795982697; bh=MTrKmNfeyecw8NkSs5nJHxixrLE3I96V/Q0jGXyrUwg=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=OTr/lBRIrTxgUUUBEX6lYZjQmC+QzJJHuuHeHaA5KeAhiKjtNOSEza/Dq+VCY9eGQ OqyR2KLEFVXnb8Dhd/bd3qe3DcY9Shig0mtHwyGjmDdknrvEJzF9fkrgkaM+83B5lp 4LBltyy5RR7ZA1EjWwv+sgTlPq6MKWQ2PcawM0Hw= Date: Sun, 29 Nov 2026 12:04:57 -0800 From: Andrew Morton To: Egor Shalashnov Cc: linux-mm@kvack.org, dennis@kernel.org, tj@kernel.org, cl@gentwo.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] percpu_ref: replace the global switch lock with hashed locks Message-Id: <20261129120457.eb0a31459b5f61f2c20d14a7@linux-foundation.org> In-Reply-To: References: X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) 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 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 29 Nov 2026 17:10:59 +0300 Egor Shalashnov wrote: > 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. Nice, thanks. Please summarize the microbenchmark speedups in the changelog? And briefly describe the real-world workload - tell readers "how come Egor is the first person to report this". It would be nice to summarize observed improvements in that workload, but I wouldn't spend a lot of time over this - the patch will obviously help a lot. > --- a/lib/percpu-refcount.c > +++ b/lib/percpu-refcount.c > @@ -36,7 +39,26 @@ > > #define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1)) > > -static DEFINE_SPINLOCK(percpu_ref_switch_lock); > +#define NR_LOCKS 64 Let's remember CONFIG_SMP=n. I guess making this "1" on !SMP will sufficiently take care of this. Also #else static inline spinlock_t *lock_addr(const struct percpu_ref *ref) { return &percpu_switch_lock[0].lock; } #endif /* CONFIG_SMP */ I suspect/hope gcc will do exactly this when NR_LOCKS=1, but not if we switch lock_addr() to hash_ptr() (below). > + > +/* > + * 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; I agree with Sashiko ;) Can we statically initialize this at compile time and remove init_percpu_ref_lock(). L1_CACHE_BYTES is too small on machines where SMP_CACHE_BYTES is larger (ARC and sparc64) - using SMP_CACHE_BYTES here would address that. I think this: static struct { spinlock_t lock; } ____cacheline_aligned_in_smp percpu_switch_lock[NR_LOCKS] = { [0 ... NR_LOCKS - 1] = { .lock = __SPIN_LOCK_UNLOCKED(percpu_switch_lock.lock), }, }; will address these things. > +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; > +} The hash is correct for serialization, but it omits address bits 12 and 13 with 64-byte L1 lines. Four refs at the same offset in consecutive 4 KiB pages can therefore collide. Using hash_ptr() here should address.