* [PATCH] x86/boot/tboot: Fix off-by-one AP WFS wait count and timeout check
@ 2026-12-04 5:39 Sun, Ning
2026-12-04 15:30 ` Dave Hansen
0 siblings, 1 reply; 4+ messages in thread
From: Sun, Ning @ 2026-12-04 5:39 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Hansen, Dave,
H. Peter Anvin
Cc: Frederic Weisbecker, x86@kernel.org,
tboot-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org
In tboot_dying_cpu(), ap_wfs_count is incremented before checking
whether this is the last secondary CPU going offline and calling
tboot_wait_for_aps(). However, the currently dying CPU is still
executing the hotplug teardown path and has not yet halted into
Wait-For-SIPI (WFS) mode.
Consequently, tboot->num_in_wfs can only equal (ap_wfs_count - 1)
at this point, causing tboot_wait_for_aps() to unconditionally busy-wait
for the full AP_WAIT_TIMEOUT duration (1 second) and fail when
offlining the final secondary CPU.
Furthermore, tboot_wait_for_aps() checks `if (timeout)` instead of
`if (!timeout)` when logging a timeout warning-an inverted condition
that was previously masked because the off-by-one bug always forced
`timeout` down to zero.
Pass `atomic_read(&ap_wfs_count) - 1` to tboot_wait_for_aps() in
tboot_dying_cpu() (while keeping ap_wfs_count incremented for the
subsequent WFS transition), and fix the inverted timeout warning
condition in tboot_wait_for_aps().
Tested on an Intel TXT platform with tboot 1.12.2 by offlining all
secondary CPUs and verifying that no timeout warning occurs.
Fixes: 5527c230f3d7 ("intel_txt: Intel(R) TXT and tboot kernel support")
Signed-off-by: Ning Sun <ning.sun@intel.com>
---
arch/x86/kernel/tboot.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/arch/x86/kernel/tboot.c b/arch/x86/kernel/tboot.c
index f8a556a921b1..d8e3f4a19c02 100644
--- a/arch/x86/kernel/tboot.c
+++ b/arch/x86/kernel/tboot.c
@@ -430,14 +430,14 @@ static int tboot_wait_for_aps(int num_aps)
mdelay(1);
timeout--;
}
- if (timeout)
+ if (!timeout)
pr_warn("tboot wait for APs timeout\n");
return !(atomic_read((atomic_t *)&tboot->num_in_wfs) == num_aps);
}
static int tboot_dying_cpu(unsigned int cpu)
{
atomic_inc(&ap_wfs_count);
if (num_online_cpus() == 1) {
- if (tboot_wait_for_aps(atomic_read(&ap_wfs_count)))
+ if (tboot_wait_for_aps(atomic_read(&ap_wfs_count) - 1))
return -EBUSY;
}
return 0;
--
2.43.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] x86/boot/tboot: Fix off-by-one AP WFS wait count and timeout check
2026-12-04 5:39 [PATCH] x86/boot/tboot: Fix off-by-one AP WFS wait count and timeout check Sun, Ning
@ 2026-12-04 15:30 ` Dave Hansen
2026-12-04 17:58 ` Sun, Ning
2026-12-06 6:58 ` [PATCH v3] x86/tboot: Fix AP wait count and timeout reporting Ning Sun
0 siblings, 2 replies; 4+ messages in thread
From: Dave Hansen @ 2026-12-04 15:30 UTC (permalink / raw)
To: Sun, Ning, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
H. Peter Anvin
Cc: Frederic Weisbecker, x86@kernel.org,
tboot-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org
On 12/3/26 21:39, Sun, Ning wrote:
> In tboot_dying_cpu(), ap_wfs_count is incremented before checking
> whether this is the last secondary CPU going offline and calling
> tboot_wait_for_aps(). However, the currently dying CPU is still
> executing the hotplug teardown path and has not yet halted into
> Wait-For-SIPI (WFS) mode.
>
> Consequently, tboot->num_in_wfs can only equal (ap_wfs_count - 1)
> at this point, causing tboot_wait_for_aps() to unconditionally busy-wait
> for the full AP_WAIT_TIMEOUT duration (1 second) and fail when
> offlining the final secondary CPU.
>
> Furthermore, tboot_wait_for_aps() checks `if (timeout)` instead of
> `if (!timeout)` when logging a timeout warning-an inverted condition
> that was previously masked because the off-by-one bug always forced
> `timeout` down to zero.
>
> Pass `atomic_read(&ap_wfs_count) - 1` to tboot_wait_for_aps() in
> tboot_dying_cpu() (while keeping ap_wfs_count incremented for the
> subsequent WFS transition), and fix the inverted timeout warning
> condition in tboot_wait_for_aps().
>
> Tested on an Intel TXT platform with tboot 1.12.2 by offlining all
> secondary CPUs and verifying that no timeout warning occurs.
The changelog appears to have become rather contaminated with AI
detritus. Could you clean it up a bit please?
What is the end user visible effect of this bug and of the fix?
What's up with this fixes tag?
How long has this bug been there, how did you find it, and why do you
think it persisted so long?
Fixes: 5527c230f3d7 ("intel_txt: Intel(R) TXT and tboot kernel support")
linux.git$ git show 5527c230f3d7
fatal: ambiguous argument '5527c230f3d7': unknown revision or path ...
My tree has:
commit 3162534069597e34dd0ac9eb711be8dc23835ae7
Author: Joseph Cihula <joseph.cihula@intel.com>
Date: Tue Jun 30 19:30:59 2009 -0700
x86, intel_txt: Intel TXT boot support
but that doesn't match the SHA1 *or* the commit summary exactly.
What's up?
^ permalink raw reply [flat|nested] 4+ messages in thread
* RE: [PATCH] x86/boot/tboot: Fix off-by-one AP WFS wait count and timeout check
2026-12-04 15:30 ` Dave Hansen
@ 2026-12-04 17:58 ` Sun, Ning
2026-12-06 6:58 ` [PATCH v3] x86/tboot: Fix AP wait count and timeout reporting Ning Sun
1 sibling, 0 replies; 4+ messages in thread
From: Sun, Ning @ 2026-12-04 17:58 UTC (permalink / raw)
To: Hansen, Dave, Thomas Gleixner, Ingo Molnar, Borislav Petkov,
H. Peter Anvin
Cc: Frederic Weisbecker, x86@kernel.org,
tboot-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org
> -----Original Message-----
> From: Hansen, Dave <dave.hansen@intel.com>
> Sent: Friday, December 4, 2026 7:30 AM
> To: Sun, Ning <ning.sun@intel.com>; Thomas Gleixner <tglx@linutronix.de>; Ingo Molnar <mingo@redhat.com>; Borislav Petkov
> <bp@alien8.de>; H. Peter Anvin <hpa@zytor.com>
> Cc: Frederic Weisbecker <frederic@kernel.org>; x86@kernel.org; tboot-devel@lists.sourceforge.net; linux-kernel@vger.kernel.org
> Subject: Re: [PATCH] x86/boot/tboot: Fix off-by-one AP WFS wait count and timeout check
>
> On 12/3/26 21:39, Sun, Ning wrote:
> > In tboot_dying_cpu(), ap_wfs_count is incremented before checking
> > whether this is the last secondary CPU going offline and calling
> > tboot_wait_for_aps(). However, the currently dying CPU is still
> > executing the hotplug teardown path and has not yet halted into
> > Wait-For-SIPI (WFS) mode.
> >
> > Consequently, tboot->num_in_wfs can only equal (ap_wfs_count - 1)
> > at this point, causing tboot_wait_for_aps() to unconditionally busy-wait
> > for the full AP_WAIT_TIMEOUT duration (1 second) and fail when
> > offlining the final secondary CPU.
> >
> > Furthermore, tboot_wait_for_aps() checks `if (timeout)` instead of
> > `if (!timeout)` when logging a timeout warning-an inverted condition
> > that was previously masked because the off-by-one bug always forced
> > `timeout` down to zero.
> >
> > Pass `atomic_read(&ap_wfs_count) - 1` to tboot_wait_for_aps() in
> > tboot_dying_cpu() (while keeping ap_wfs_count incremented for the
> > subsequent WFS transition), and fix the inverted timeout warning
> > condition in tboot_wait_for_aps().
> >
> > Tested on an Intel TXT platform with tboot 1.12.2 by offlining all
> > secondary CPUs and verifying that no timeout warning occurs.
>
> The changelog appears to have become rather contaminated with AI
> detritus. Could you clean it up a bit please?
Hello Dave, apologies for the noise; I will clean up and update the changelog in v3.
>
> What is the end user visible effect of this bug and of the fix?
The bug causes a 1-second delay and `-EBUSY` in `dmesg` when offlining the last secondary CPU on Intel TXT/tboot systems, alongside inverted timeout warning logic. The fix resolves both issues.
>
> What's up with this fixes tag?
>
> How long has this bug been there, how did you find it, and why do you
> think it persisted so long?
>
> Fixes: 5527c230f3d7 ("intel_txt: Intel(R) TXT and tboot kernel support")
>
> linux.git$ git show 5527c230f3d7
> fatal: ambiguous argument '5527c230f3d7': unknown revision or path ...
>
> My tree has:
>
> commit 3162534069597e34dd0ac9eb711be8dc23835ae7
> Author: Joseph Cihula <joseph.cihula@intel.com>
> Date: Tue Jun 30 19:30:59 2009 -0700
>
> x86, intel_txt: Intel TXT boot support
>
> but that doesn't match the SHA1 *or* the commit summary exactly.
>
> What's up?
That `Fixes` tag was incorrect; the code dates back to 2009 (commit 316253406959). I will update it and send a v3 shortly.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v3] x86/tboot: Fix AP wait count and timeout reporting
2026-12-04 15:30 ` Dave Hansen
2026-12-04 17:58 ` Sun, Ning
@ 2026-12-06 6:58 ` Ning Sun
1 sibling, 0 replies; 4+ messages in thread
From: Ning Sun @ 2026-12-06 6:58 UTC (permalink / raw)
To: Thomas Gleixner, Ingo Molnar, Borislav Petkov, Dave Hansen,
H . Peter Anvin
Cc: Frederic Weisbecker, x86, tboot-devel, linux-kernel, Ning Sun
On Intel TXT systems using tboot, offlining the last secondary CPU
can busy-wait until the AP wait timeout expires. The hotplug core then
reports that the x86/tboot:dying callback failed with -EBUSY, although
the CPU still goes offline.
This was found while reviewing Frederic Weisbecker's patch,
"[PATCH 46/47] x86/boot/tboot: Move CPUHP_AP_X86_TBOOT_DYING post
stop_machine()".
The wait target includes the outgoing CPU, which has not yet entered
tboot's wait-for-SIPI (WFS) state. The target also increases on every
offline operation but is never reduced when a CPU comes back online.
Replace the counter with a cpumask. Clear a CPU's bit in the starting
callback and use the mask weight before setting the outgoing CPU's bit
as the wait target.
Also fix the inverted timeout warning condition and use a single final
WFS count check for both the warning and the return value.
The exact count comparison and timeout budget are unchanged. APs
parked by tboot but never brought online by Linux, and failures before
the starting callback, remain accounting limitations.
Tested on an Intel TXT system with tboot 1.12.2 running Linux 6.18.64
by performing three complete secondary CPU offline/online cycles.
The stall and -EBUSY warning no longer occur. S3 was not tested because
the platform supports only s2idle.
Signed-off-by: Ning Sun <ning.sun@intel.com>
---
Changes in v3:
- Replace ap_wfs_count with a cpumask cleared from a new starting
callback so the wait target tracks offline/online cycles.
- Exclude the outgoing CPU by reading cpumask_weight() before
setting its bit.
- Base both the timeout warning and -EBUSY return on a single final
WFS count check.
- Shorten the commit message, describe the user-visible failure, and
omit the unverified Fixes tag.
arch/x86/kernel/tboot.c | 39 ++++++++++++++++++++++++++++-----------
1 file changed, 28 insertions(+), 11 deletions(-)
diff --git a/arch/x86/kernel/tboot.c b/arch/x86/kernel/tboot.c
index 46b8f1f16676..cc5265f3055c 100644
--- a/arch/x86/kernel/tboot.c
+++ b/arch/x86/kernel/tboot.c
@@ -314,7 +314,8 @@ static int tboot_extended_sleep(u8 sleep_state, u32 val_a, u32 val_b)
return -ENODEV;
}
-static atomic_t ap_wfs_count;
+/* CPUs expected in tboot WFS after offline, cleared on CPU startup. */
+static struct cpumask tboot_wfs_cpus;
static int tboot_wait_for_aps(int num_aps)
{
@@ -327,19 +328,35 @@ static int tboot_wait_for_aps(int num_aps)
timeout--;
}
- if (timeout)
+ if (atomic_read((atomic_t *)&tboot->num_in_wfs) != num_aps) {
pr_warn("tboot wait for APs timeout\n");
+ return -EBUSY;
+ }
+
+ return 0;
+}
- return !(atomic_read((atomic_t *)&tboot->num_in_wfs) == num_aps);
+static int tboot_starting_cpu(unsigned int cpu)
+{
+ /* tboot drops this CPU from num_in_wfs when it is woken by SIPI. */
+ cpumask_clear_cpu(cpu, &tboot_wfs_cpus);
+ return 0;
}
static int tboot_dying_cpu(unsigned int cpu)
{
- atomic_inc(&ap_wfs_count);
- if (num_online_cpus() == 1) {
- if (tboot_wait_for_aps(atomic_read(&ap_wfs_count)))
- return -EBUSY;
- }
+ /*
+ * This callback runs on the outgoing CPU, which enters tboot's
+ * wait-for-SIPI only later from play_dead(). Exclude it from the
+ * wait target.
+ */
+ int num_aps = cpumask_weight(&tboot_wfs_cpus);
+
+ cpumask_set_cpu(cpu, &tboot_wfs_cpus);
+
+ if (num_online_cpus() == 1)
+ return tboot_wait_for_aps(num_aps);
+
return 0;
}
@@ -417,9 +434,9 @@ static __init int tboot_late_init(void)
tboot_create_trampoline();
- atomic_set(&ap_wfs_count, 0);
- cpuhp_setup_state(CPUHP_AP_X86_TBOOT_DYING, "x86/tboot:dying", NULL,
- tboot_dying_cpu);
+ cpumask_clear(&tboot_wfs_cpus);
+ cpuhp_setup_state_nocalls(CPUHP_AP_X86_TBOOT_DYING, "x86/tboot:dying",
+ tboot_starting_cpu, tboot_dying_cpu);
#ifdef CONFIG_DEBUG_FS
debugfs_create_file("tboot_log", S_IRUSR,
arch_debugfs_dir, NULL, &tboot_log_fops);
base-commit: 28a23309c8e40d651faf4cc3cc9b71458a11b987
--
2.53.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-12-06 6:59 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 5:39 [PATCH] x86/boot/tboot: Fix off-by-one AP WFS wait count and timeout check Sun, Ning
2026-12-04 15:30 ` Dave Hansen
2026-12-04 17:58 ` Sun, Ning
2026-12-06 6:58 ` [PATCH v3] x86/tboot: Fix AP wait count and timeout reporting Ning Sun
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.