From: sashiko-bot@kernel.org
To: "Egor Shalashnov" <shalasheg@gmail.com>, linux-mm@kvack.org
Cc: dennis@kernel.org, tj@kernel.org, cl@gentwo.org,
akpm@linux-foundation.org, zhengqi.arch@bytedance.com
Subject: Re: [PATCH] percpu_ref: make percpu_ref_switch_lock per percpu_ref
Date: Thu, 26 Nov 2026 15:56:12 +0000 [thread overview]
Message-ID: <sashiko-outbox-235862@kernel.org> (raw)
In-Reply-To: <6c943754-05b5-4017-9145-0d145766909a@gmail.com>
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Medium] percpu_ref: global percpu_ref_switch_waitq still serializes
mode switches
- [Medium] percpu_ref: switch lock in struct percpu_ref bloats hot
embedding cachelines
--
commit d37012ad38aa40d26d6d73947c79cfb449eb07ba
Author: Qi Zheng <qi.zheng@linux.dev>
percpu_ref: make percpu_ref_switch_lock per percpu_ref
This commit replaces the global percpu_ref_switch_lock with a per-instance
spinlock inside struct percpu_ref to avoid lock contention across
concurrent mode switches.
> diff --git a/include/linux/percpu-refcount.h b/include/linux/percpu-refcount.h
> index 1e3212e2c8271..4582e798de8a6 100644
> --- a/include/linux/percpu-refcount.h
> +++ b/include/linux/percpu-refcount.h
[ ... ]
> @@ -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
[Severity: Medium]
Should percpu_ref_switch_lock be placed in struct percpu_ref_data instead
of struct percpu_ref?
As noted in the comment right below, commit 2b0d3d3e4fcf ("percpu_ref:
reduce memory footprint of percpu_ref in fast path") moved slow-path fields
into struct percpu_ref_data to keep struct percpu_ref at 16 bytes for
structures that embed it in hot cachelines.
Adding percpu_ref_switch_lock here grows struct percpu_ref to 24 bytes on
64-bit SMP (and up to 88 bytes with lock debugging or PREEMPT_RT). In
struct io_ring_ctx, for example, the first ____cacheline_aligned_in_smp
read-mostly block is packed to 64 bytes, so expanding refs pushes
notify_method and sq_thread_idle into a second cacheline.
> diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c
> index f8d90689af9f5..5d466026e5251 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);
> static DECLARE_WAIT_QUEUE_HEAD(percpu_ref_switch_waitq);
[ ... ]
> @@ -267,7 +267,7 @@ static void __percpu_ref_switch_mode(struct percpu_ref *ref,
> {
> struct percpu_ref_data *data = ref->data;
>
> - lockdep_assert_held(&percpu_ref_switch_lock);
> + lockdep_assert_held(&ref->percpu_ref_switch_lock);
>
> /*
> * If the previous ATOMIC switching hasn't finished yet, wait for
> @@ -275,7 +275,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);
> + ref->percpu_ref_switch_lock);
[Severity: Medium]
Does leaving percpu_ref_switch_waitq global keep the mode-switch path
serialized on a shared spinlock?
Every percpu-to-atomic transition completes in
percpu_ref_call_confirm_rcu(), which unconditionally calls
wake_up_all(&percpu_ref_switch_waitq). That acquires the global
percpu_ref_switch_waitq.lock even when no task is waiting, and wakes
waiters across all unrelated percpu_ref instances when tasks do wait.
--
Sashiko AI review · https://sashiko.dev/#/patchset/6c943754-05b5-4017-9145-0d145766909a@gmail.com?part=1
prev parent reply other threads:[~2026-11-26 15:56 UTC|newest]
Thread overview: 4+ 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
2026-11-25 21:46 ` Egor Shalashnov
2026-11-26 15:56 ` sashiko-bot [this message]
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=sashiko-outbox-235862@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=cl@gentwo.org \
--cc=dennis@kernel.org \
--cc=linux-mm@kvack.org \
--cc=sashiko-reviews@lists.linux.dev \
--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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox