[PATCH v1 10/13] tools/x86/kcpuid: Scan valid cpuid ranges only

From: Ahmed S. Darwish
Date: Thu Nov 28 2024 - 17:34:29 EST


kcpuid works in two runs: one run for invoking cpuid instructions and
saving all output in-memory, and another run for parsing that in-memory
output using the CSV bitfields specification.

CPU vendor detection was just added in previous commits. In both kcpuid
runs, it makes no sense to deal with cpuid ranges that are not valid for
the current CPU.

Avoid ugly "is range valid for CPU?" conditionals throughout the code by
modifying the for_each_cpuid_range() macro to iterate over cpuid ranges
that are only known to be valid for the current CPU.

Signed-off-by: Ahmed S. Darwish <darwi@xxxxxxxxxxxxx>
---
tools/arch/x86/kcpuid/kcpuid.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/tools/arch/x86/kcpuid/kcpuid.c b/tools/arch/x86/kcpuid/kcpuid.c
index 9769da3e4494..f798c909c7b2 100644
--- a/tools/arch/x86/kcpuid/kcpuid.c
+++ b/tools/arch/x86/kcpuid/kcpuid.c
@@ -119,7 +119,9 @@ 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 < ARRAY_SIZE(ranges) && \
+ ((range) = &ranges[i]) && \
+ (range->vendors & this_cpu_vendor); \
i++)

struct cpuid_range *index_to_cpuid_range(u32 index)
--
2.46.2