From: Andrea Parri <parri.andrea@gmail.com>
To: Thomas Gleixner <tglx@kernel.org>, Peter Zijlstra <peterz@infradead.org>
Cc: Zhu Jun <zhujun2@cmss.chinamobile.com>, linux-kernel@vger.kernel.org
Subject: [PATCH] cpu/hotplug: Read st->bringup after the barrier in cpuhp_thread_fun()
Date: Mon, 30 Nov 2026 10:46:07 +0100 [thread overview]
Message-ID: <20261130094608.7691-1-parri.andrea@gmail.com> (raw)
When an AP hotplug callback fails, cpuhp_kick_ap() inverts st->bringup,
kicks the AP thread again to roll back and waits on the completion for
the new direction. cpuhp_thread_fun() takes the direction from
st->bringup, but loads it before the smp_mb() that pairs with the one in
__cpuhp_kick_ap().
Only a control dependency in smpboot_thread_fn() orders that load after
the st->should_run load in cpuhp_should_run(), and that does not order
loads. If the AP thread has not gone to sleep since completing the
failed step, it can observe the new st->should_run with the old
st->bringup:
BP AP
__cpuhp_kick_ap() cpuhp_thread_fun()
wait_for_ap_thread(st, true) callback fails
complete_ap_thread(st, true)
cpuhp_reset_state() smpboot_thread_fn()
r1 = st->bringup // true, early
st->bringup = false
__cpuhp_kick_ap()
smp_mb()
st->should_run = true
r0 = st->should_run // true
cpuhp_thread_fun(), bringup = r1
complete_ap_thread(st, true)
wait_for_ap_thread(st, false)
With bringup still true, cpuhp_next_state() finds st->state above
st->target and returns false, so the AP completes st->done_up and the
BP waits on st->done_down forever with the hotplug lock held. A failed
teardown, rolled back by the same cpuhp_kick_ap(), hangs the same way in
the other direction.
The window is small. The AP thread must still be on its way from
complete_ap_thread() to the st->should_run check when the BP, woken by
that completion, kicks it again; if it went to sleep instead,
wake_up_process() orders the loads. x86 does not reorder loads and is
not affected.
Load st->bringup after the smp_mb(), with the rest of the state the
barrier is documented to cover. smp_load_acquire() in
cpuhp_should_run() would also work, but the barrier and its pairing
comment are already in place.
With the st->should_run accesses marked with READ_ONCE() and
WRITE_ONCE(), the Linux kernel memory model allows the outcome above and
forbids it with this change. Left plain, those accesses are a data race
of their own. The hang has not been reproduced on hardware.
The bug was found with the help of an LLM, which was also used to write
the fix, the changelog and the litmus test.
Fixes: 4dddfb5faa61 ("smp/hotplug: Rewrite AP state machine core")
Signed-off-by: Andrea Parri <parri.andrea@gmail.com>
---
Zhu Jun's pending patch [1] marks the st->should_run accesses with
READ_ONCE() and WRITE_ONCE() but leaves the st->bringup load before the
barrier. The two changes are independent. They touch adjacent lines,
so whichever is applied second needs a trivial rebase.
LKMM litmus test for the interleaving in the changelog, with plain
accesses to st->bringup as in the code. The test marks the
st->should_run accesses, as [1] does; with plain accesses herd7 also
flags the race on st->should_run, which [1] addresses. P0 is the BP
side, P1 the AP thread (see the comment in the test).
herd7 finds the stale value and flags a data race on st->bringup.
Moving the r1 load below the second smp_mb() in P1, as this patch does,
removes both, so st->bringup needs no READ_ONCE()/WRITE_ONCE() once it
is loaded after the barrier:
C cpuhp-bringup-buggy
(*
* Result: Sometimes DATARACE
*
* P0 is the BP in cpuhp_reset_state() and __cpuhp_kick_ap() during a
* rollback. P1 is the AP thread finding ->should_run set without
* sleeping: the first smp_mb() stands for set_current_state() in
* smpboot_thread_fn(), the second is the one in cpuhp_thread_fun(),
* and r2 is the WARN_ON_ONCE() reload of ->should_run.
*)
{
bringup=1;
}
P0(int *bringup, int *should_run)
{
*bringup = 0;
smp_mb();
WRITE_ONCE(*should_run, 1);
}
P1(int *bringup, int *should_run)
{
int r0;
int r1;
int r2;
smp_mb();
r0 = READ_ONCE(*should_run);
if (r0) {
r1 = *bringup;
r2 = READ_ONCE(*should_run);
smp_mb();
}
}
exists (1:r0=1 /\ 1:r1=1)
$ herd7 -conf linux-kernel.cfg cpuhp-bringup-buggy.litmus
Flag data-race
Observation cpuhp-bringup-buggy Sometimes 1 2
$ herd7 -conf linux-kernel.cfg cpuhp-bringup-fixed.litmus
Observation cpuhp-bringup-fixed Never 0 2
Build-tested only: kernel/cpu.o with W=1 for x86_64 and arm64 defconfig.
[1] https://lore.kernel.org/r/20261111062853.16835-1-zhujun2@cmss.chinamobile.com
kernel/cpu.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/kernel/cpu.c b/kernel/cpu.c
index 376d297a6292b..6e51b0045cae2 100644
--- a/kernel/cpu.c
+++ b/kernel/cpu.c
@@ -1065,8 +1065,8 @@ static int cpuhp_should_run(unsigned int cpu)
static void cpuhp_thread_fun(unsigned int cpu)
{
struct cpuhp_cpu_state *st = this_cpu_ptr(&cpuhp_state);
- bool bringup = st->bringup;
enum cpuhp_state state;
+ bool bringup;
if (WARN_ON_ONCE(!st->should_run))
return;
@@ -1076,6 +1076,7 @@ static void cpuhp_thread_fun(unsigned int cpu)
* that if we see ->should_run we also see the rest of the state.
*/
smp_mb();
+ bringup = st->bringup;
/*
* The BP holds the hotplug lock, but we're now running on the AP,
base-commit: fe6ff8d5d675c01793b9f051d16055dd4aa75244
--
2.53.0
reply other threads:[~2026-11-30 9:46 UTC|newest]
Thread overview: [no followups] expand[flat|nested] mbox.gz Atom feed
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=20261130094608.7691-1-parri.andrea@gmail.com \
--to=parri.andrea@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@kernel.org \
--cc=zhujun2@cmss.chinamobile.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
all inboxes | Powered by JetHome®