[tip: x86/cpu] tools/x86/kcpuid: Consolidate index validity checks
From: tip-bot2 for Ahmed S. Darwish
Date: Tue Mar 25 2025 - 05:06:35 EST
The following commit has been merged into the x86/cpu branch of tip:
Commit-ID: 74d29127f83042500c20c903dd67151dbdd86ec8
Gitweb: https://git.kernel.org/tip/74d29127f83042500c20c903dd67151dbdd86ec8
Author: Ahmed S. Darwish <darwi@xxxxxxxxxxxxx>
AuthorDate: Mon, 24 Mar 2025 15:20:34 +01:00
Committer: Ingo Molnar <mingo@xxxxxxxxxx>
CommitterDate: Tue, 25 Mar 2025 09:53:46 +01:00
tools/x86/kcpuid: Consolidate index validity checks
Let index_to_cpuid_range() return a CPUID range only if the passed index
is within a CPUID range's maximum supported function on the CPU.
Returning a CPUID range that is invalid on the CPU for the passed index
does not make sense.
This also avoids repeating the "function index is within CPUID range"
checks, both at setup_cpuid_range() and index_to_func().
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-14-darwi@xxxxxxxxxxxxx
---
tools/arch/x86/kcpuid/kcpuid.c | 21 +++++++++------------
1 file changed, 9 insertions(+), 12 deletions(-)
diff --git a/tools/arch/x86/kcpuid/kcpuid.c b/tools/arch/x86/kcpuid/kcpuid.c
index 1f48de3..ac3d36b 100644
--- a/tools/arch/x86/kcpuid/kcpuid.c
+++ b/tools/arch/x86/kcpuid/kcpuid.c
@@ -101,10 +101,12 @@ static char *range_to_str(struct cpuid_range *range)
struct cpuid_range *index_to_cpuid_range(u32 index)
{
+ u32 func_idx = index & CPUID_FUNCTION_MASK;
+ u32 range_idx = index & CPUID_INDEX_MASK;
struct cpuid_range *range;
for_each_cpuid_range(range) {
- if (range->index == (index & CPUID_INDEX_MASK))
+ if (range->index == range_idx && (u32)range->nr > func_idx)
return range;
}
@@ -331,17 +333,16 @@ static void parse_line(char *line)
/* index/main-leaf */
index = strtoull(tokens[0], NULL, 0);
- /* Skip line parsing if it's not covered by known ranges */
+ /*
+ * Skip line parsing if the index is not covered by known-valid
+ * CPUID ranges on this CPU.
+ */
range = index_to_cpuid_range(index);
if (!range)
return;
- /* Skip line parsing for non-existing indexes */
- index &= CPUID_FUNCTION_MASK;
- if ((int)index >= range->nr)
- return;
-
/* Skip line parsing if the index CPUID output is all zero */
+ index &= CPUID_FUNCTION_MASK;
func = &range->funcs[index];
if (!func->nr)
return;
@@ -505,17 +506,13 @@ static void show_range(struct cpuid_range *range)
static inline struct cpuid_func *index_to_func(u32 index)
{
+ u32 func_idx = index & CPUID_FUNCTION_MASK;
struct cpuid_range *range;
- u32 func_idx;
range = index_to_cpuid_range(index);
if (!range)
return NULL;
- func_idx = index & CPUID_FUNCTION_MASK;
- if ((func_idx + 1) > (u32)range->nr)
- return NULL;
-
return &range->funcs[func_idx];
}