[PATCH v3] x86/tboot: Fix AP wait count and timeout reporting
From: Ning Sun
Date: Sun Dec 06 2026 - 01:59:07 EST
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@xxxxxxxxx>
---
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