[tip: x86/cpu] tools/x86/kcpuid: Filter valid CPUID ranges

From: tip-bot2 for Ahmed S. Darwish
Date: Tue Mar 25 2025 - 05:06:53 EST


The following commit has been merged into the x86/cpu branch of tip:

Commit-ID: 72383c8274edf0a1736973462603dcfd0a088bb9
Gitweb: https://git.kernel.org/tip/72383c8274edf0a1736973462603dcfd0a088bb9
Author: Ahmed S. Darwish <darwi@xxxxxxxxxxxxx>
AuthorDate: Mon, 24 Mar 2025 15:20:35 +01:00
Committer: Ingo Molnar <mingo@xxxxxxxxxx>
CommitterDate: Tue, 25 Mar 2025 09:53:46 +01:00

tools/x86/kcpuid: Filter valid CPUID ranges

Next commits will introduce vendor-specific CPUID ranges like Transmeta's
0x8086000 range and Centaur's 0xc0000000.

Initially explicit vendor detection was implemented, but it turned out to
be not strictly necessary. As Dave Hansen noted, even established tools
like cpuid(1) just tries all ranges indices, and see if the CPU responds
back with something sensible.

Do something similar at setup_cpuid_range(). Query the range's index,
and check the maximum range function value returned. If it's within an
expected interval of [range_index, range_index + MAX_RANGE_INDEX_OFFSET],
accept the range as valid and further query its leaves.

Set MAX_RANGE_INDEX_OFFSET to a heuristic of 0xff. That should be
sensible enough since all the ranges covered by x86-cpuid-db XML database
are:

0x00000000 0x00000023
0x40000000 0x40000000
0x80000000 0x80000026
0x80860000 0x80860007
0xc0000000 0xc0000001

At setup_cpuid_range(), if the range's returned maximum function was not
sane, mark it as invalid by setting its number of leaves, range->nr, to
zero.

Introduce the for_each_valid_cpuid_range() iterator instead of sprinkling
"range->nr != 0" checks throughout the code.

Suggested-by: Dave Hansen <dave.hansen@xxxxxxxxx>
Signed-off-by: Ahmed S. Darwish <darwi@xxxxxxxxxxxxx>
Signed-off-by: Ingo Molnar <mingo@xxxxxxxxxx>
Cc: H. Peter Anvin <hpa@xxxxxxxxx>
Cc: Linus Torvalds <torvalds@xxxxxxxxxxxxxxxxxxxx>
Cc: Josh Poimboeuf <jpoimboe@xxxxxxxxxx>
Link: https://lore.kernel.org/r/20250324142042.29010-15-darwi@xxxxxxxxxxxxx
---
tools/arch/x86/kcpuid/kcpuid.c | 37 ++++++++++++++++++++++++---------
1 file changed, 27 insertions(+), 10 deletions(-)

diff --git a/tools/arch/x86/kcpuid/kcpuid.c b/tools/arch/x86/kcpuid/kcpuid.c
index ac3d36b..fe3d058 100644
--- a/tools/arch/x86/kcpuid/kcpuid.c
+++ b/tools/arch/x86/kcpuid/kcpuid.c
@@ -96,8 +96,13 @@ static char *range_to_str(struct cpuid_range *range)
}
}

-#define for_each_cpuid_range(range) \
- for (unsigned int i = 0; i < ARRAY_SIZE(ranges) && ((range) = &ranges[i]); i++)
+#define __for_each_cpuid_range(range, __condition) \
+ for (unsigned int i = 0; \
+ i < ARRAY_SIZE(ranges) && ((range) = &ranges[i]) && (__condition); \
+ i++)
+
+#define for_each_valid_cpuid_range(range) __for_each_cpuid_range(range, (range)->nr != 0)
+#define for_each_cpuid_range(range) __for_each_cpuid_range(range, true)

struct cpuid_range *index_to_cpuid_range(u32 index)
{
@@ -105,7 +110,7 @@ struct cpuid_range *index_to_cpuid_range(u32 index)
u32 range_idx = index & CPUID_INDEX_MASK;
struct cpuid_range *range;

- for_each_cpuid_range(range) {
+ for_each_valid_cpuid_range(range) {
if (range->index == range_idx && (u32)range->nr > func_idx)
return range;
}
@@ -223,20 +228,32 @@ static void raw_dump_range(struct cpuid_range *range)
}

#define MAX_SUBLEAF_NUM 64
+#define MAX_RANGE_INDEX_OFFSET 0xff
void setup_cpuid_range(struct cpuid_range *range)
{
- u32 max_func, idx_func;
+ u32 max_func, range_funcs_sz;
u32 eax, ebx, ecx, edx;

cpuid(range->index, max_func, ebx, ecx, edx);

- idx_func = (max_func & CPUID_FUNCTION_MASK) + 1;
- range->funcs = malloc(sizeof(struct cpuid_func) * idx_func);
+ /*
+ * If the CPUID range's maximum function value is garbage, then it
+ * is not recognized by this CPU. Set the range's number of valid
+ * leaves to zero so that for_each_valid_cpu_range() can ignore it.
+ */
+ if (max_func < range->index || max_func > (range->index + MAX_RANGE_INDEX_OFFSET)) {
+ range->nr = 0;
+ return;
+ }
+
+ range->nr = (max_func & CPUID_FUNCTION_MASK) + 1;
+ range_funcs_sz = range->nr * sizeof(struct cpuid_func);
+
+ range->funcs = malloc(range_funcs_sz);
if (!range->funcs)
err(EXIT_FAILURE, NULL);

- range->nr = idx_func;
- memset(range->funcs, 0, sizeof(struct cpuid_func) * idx_func);
+ memset(range->funcs, 0, range_funcs_sz);

for (u32 f = range->index; f <= max_func; f++) {
u32 max_subleaf = MAX_SUBLEAF_NUM;
@@ -523,7 +540,7 @@ static void show_info(void)

if (show_raw) {
/* Show all of the raw output of 'cpuid' instr */
- for_each_cpuid_range(range)
+ for_each_valid_cpuid_range(range)
raw_dump_range(range);
return;
}
@@ -552,7 +569,7 @@ static void show_info(void)
}

printf("CPU features:\n=============\n\n");
- for_each_cpuid_range(range)
+ for_each_valid_cpuid_range(range)
show_range(range);
}