[PATCH] x86/tsc: Use topology_max_packages() to get package number

From: Feng Tang
Date: Fri Mar 15 2024 - 07:42:21 EST


Commit b50db7095fe0 ("x86/tsc: Disable clocksource watchdog for TSC
on qualified platorms") was introduced to solve problem that
sometimes TSC clocksource is wrongly judged as unstable by watchdog
like 'jiffies', HPET, etc.

In it, the hardware package number is a key factor for judging whether
to disable the watchdog for TSC, and 'nr_online_nodes' was chosen as
an estimation due to it is needed in early boot phase before
registering 'tsc-early' clocksource, where all none-boot CPUs are not
brought up yet.

Dave and Rui pointed out there are many cases in which 'nr_online_nodes'
is not accurate, like:

* numa emulation (numa=fake=8 etc.)
* numa=off
* platforms with CPU-less HBM nodes, CPU-less Optane memory nodes.
* SNC (sub-numa cluster) mode enabled
* 'nr_cpus=', 'possible_cpus=' 'maxcpus=' kernel cmdline parameter setup

Thomas' recent patchset of refactoring x86 topology code introduces
topology_max_package(), which works well in most of the above cases.
The only exceptions are 'nr_cpus=' and 'possible_cpus=' setup, which
sets up the 'nr_cpu_ids' and rejects the rest of the CPUs, and may
cause topology_max_package() less than the real package number, but
it's fine as it is rarely used debug option, and logical package
number really matters in this check. So use the more accurate
topology_max_package() to replace nr_online_nodes().

Reported-by: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx>
Closes: https://lore.kernel.org/lkml/a4860054-0f16-6513-f121-501048431086@xxxxxxxxx/
Signed-off-by: Feng Tang <feng.tang@xxxxxxxxx>
---
arch/x86/kernel/tsc.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/arch/x86/kernel/tsc.c b/arch/x86/kernel/tsc.c
index 5a69a49acc96..87e7c0e89db1 100644
--- a/arch/x86/kernel/tsc.c
+++ b/arch/x86/kernel/tsc.c
@@ -1252,15 +1252,12 @@ static void __init check_system_tsc_reliable(void)
* - TSC which does not stop in C-States
* - the TSC_ADJUST register which allows to detect even minimal
* modifications
- * - not more than two sockets. As the number of sockets cannot be
- * evaluated at the early boot stage where this has to be
- * invoked, check the number of online memory nodes as a
- * fallback solution which is an reasonable estimate.
+ * - not more than four sockets.
*/
if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC) &&
boot_cpu_has(X86_FEATURE_NONSTOP_TSC) &&
boot_cpu_has(X86_FEATURE_TSC_ADJUST) &&
- nr_online_nodes <= 4)
+ topology_max_packages() <= 4)
tsc_disable_clocksource_watchdog();
}

--
2.34.1