From: Andrew Morton <akpm@linux-foundation.org>
To: Egor Shalashnov <shalasheg@gmail.com>
Cc: linux-mm@kvack.org, dennis@kernel.org, tj@kernel.org,
cl@gentwo.org, zhengqi.arch@bytedance.com,
linux-kernel@vger.kernel.org
Subject: Re: [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref
Date: Wed, 25 Nov 2026 11:35:37 -0800 [thread overview]
Message-ID: <20261125113537.bdb168bfe8962ef1aefc097b@linux-foundation.org> (raw)
In-Reply-To: <6c943754-05b5-4017-9145-0d145766909a@gmail.com>
On Wed, 25 Nov 2026 18:55:55 +0300 Egor Shalashnov <shalasheg@gmail.com> wrote:
> From: Qi Zheng <zhengqi.arch@bytedance.com>
>
> 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 per percpu_ref percpu_ref_switch_lock to
> fixes this situation.
>
> ...
>
> I would like to revive Qi Zheng's per-reference switch lock patch
> from 2022 as a standalone change.
This is a nice little change - it's a shame it was buried inside in a
larger patchset back in 2022.
> Our workload maintains many independent percpu_ref instances and
> repeatedly performs kill, waits for references to drain, and reinitializes
> them. Multiple CPUs execute these cycles concurrently on different
> instances.
>
> All these transitions compete for the same global switch lock.
> Reinitialization also clears the per-CPU counters while holding it.
> As the number of concurrent transitions grows, this shared lock
> becomes a significant bottleneck.
>
> With a module-local adaptation of Qi's per-reference locking change,
> we observed approximately 2.8x to 6.5x lower operation completion times
> in the measured workload. This motivates using a lock per instance
> so that independent transitions can proceed concurrently.
Well, an unpublished module isn't very useful for us.
And an intense microbenchmark isn't as interesting as a real-world
workload. So can you please provide more details about what "our
workload" is doing and some measurements on how much this patch
improves that workload?
> @@ -109,6 +110,8 @@ struct percpu_ref {
> */
> unsigned long percpu_count_ptr;
>
> + spinlock_t percpu_ref_switch_lock;
> +
> /*
> * 'percpu_ref' is often embedded into user structure, and only
> * 'percpu_count_ptr' is required in fast path, move other fields
> diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c
> index f8d9068..5d46602 100644
> --- a/lib/percpu-refcount.c
> +++ b/lib/percpu-refcount.c
> @@ -36,7 +36,6 @@
>
> #define PERCPU_COUNT_BIAS (1LU << (BITS_PER_LONG - 1))
>
> -static DEFINE_SPINLOCK(percpu_ref_switch_lock);
lol, yeah, that'll do it!
It does grow percpu_ref by 4 bytes, but LLM estimates that a busy
machine will have 10k-100k of these floating around so not to bad.
And I'm told that this will cause io_ring_ctx's hot fields to spill
into a second cacheline, but that can be fixed up by reordering
io_ring_ctx fields.
Alternatively, this looks like somewhere where a hashed spinlock will
work well. A static array of 32(?) cacheline_aligned spinlocks, hash
the percpu_ref* onto one of those. That'll get 90% of the benefit
without growing percpu_ref.
next prev parent reply other threads:[~2026-11-25 19:35 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <01612865-faa0-4fe5-8184-a177f104843f@gmail.com>
2026-11-25 15:55 ` [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref Egor Shalashnov
2026-11-25 19:35 ` Andrew Morton [this message]
2026-11-25 21:46 ` Egor Shalashnov
2026-11-29 14:07 ` Egor Shalashnov
2026-11-26 15:56 ` sashiko-bot
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=20261125113537.bdb168bfe8962ef1aefc097b@linux-foundation.org \
--to=akpm@linux-foundation.org \
--cc=cl@gentwo.org \
--cc=dennis@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=shalasheg@gmail.com \
--cc=tj@kernel.org \
--cc=zhengqi.arch@bytedance.com \
/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.