From: sashiko-bot@kernel.org
To: "Egor Shalashnov" <shalasheg@gmail.com>, linux-mm@kvack.org
Cc: akpm@linux-foundation.org, dennis@kernel.org, tj@kernel.org,
cl@gentwo.org
Subject: Re: [PATCH v2] percpu_ref: replace the global switch lock with hashed locks
Date: Sun, 29 Nov 2026 21:34:43 +0000 [thread overview]
Message-ID: <sashiko-outbox-240055@kernel.org> (raw)
In-Reply-To: <c54762a0-ad7a-47ca-b260-29f44c4e1a33@gmail.com>
Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] percpu_ref: uninitialized percpu_switch_lock spinlocks before
pure_initcall()
--
commit a266d7241fd78d3a31f1a9d5601843fafd5034eb
Author: Egor Shalashnov <shalasheg@gmail.com>
percpu_ref: replace the global switch lock with hashed locks
This commit replaces the global percpu_ref_switch_lock with a hashed array
of 64 cacheline-aligned spinlocks initialized from a pure_initcall.
> diff --git a/lib/percpu-refcount.c b/lib/percpu-refcount.c
> index f8d90689af9f5..2c89bcb56e885 100644
> --- 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
> +
> +/*
> + * 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;
[ ... ]
> @@ -474,6 +503,16 @@ void percpu_ref_resurrect(struct percpu_ref *ref)
> 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);
[Severity: Medium]
Can a lock in percpu_switch_lock[] be acquired before init_percpu_ref_lock()
runs?
pure_initcall() only runs during do_basic_setup() -> do_initcalls(), after
start_kernel() and do_pre_smp_initcalls(). However, start_kernel() already
uses percpu_ref in cgroup_init() (for example, cgroup_setup_root() and
mem_cgroup_css_online()), where an early failure unwind calls
percpu_ref_kill() or percpu_ref_exit() and takes lock_addr(ref) while the
spinlock_t is still zeroed, triggering a SPIN_BUG_ON() splat or hang under
CONFIG_DEBUG_SPINLOCK and operating on an uninitialized lockdep_map or
rt_mutex_base.wait_lock.
Initializing percpu_switch_lock[] statically at compile time with
__SPIN_LOCK_UNLOCKED() (similar to atomic64_lock[] in lib/atomic64.c) would
avoid the uninitialized window and make the initcall unnecessary.
--
Sashiko AI review · https://sashiko.dev/#/patchset/c54762a0-ad7a-47ca-b260-29f44c4e1a33@gmail.com?part=1
prev parent reply other threads:[~2026-11-29 21:34 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-29 14:10 [PATCH v2] percpu_ref: replace the global switch lock with hashed locks Egor Shalashnov
2026-11-29 20:04 ` Andrew Morton
2026-11-29 21:34 ` 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-240055@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 \
/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.