[PATCH] x86/boot/tboot: Fix off-by-one AP WFS wait count and timeout check

From: Sun, Ning

Date: Fri Dec 04 2026 - 00:39:49 EST


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@xxxxxxxxx>
---
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