[PATCH v11 3/4] x86/cpu: Do a sanity check on required feature bits
From: Maciej Wieczor-Retman
Date: Fri Mar 20 2026 - 08:52:45 EST
From: Maciej Wieczor-Retman <maciej.wieczor-retman@xxxxxxxxx>
After CPU identification concludes, do a sanity check by comparing the
final x86_capability bitmask with the pre-defined required feature bits.
Because for_each_set_bit() expects 64-bit values and feature bitmasks
are 32-bit use DECLARE_BITMAP() to avoid unaligned memory accesses if
NCAPINTS is ever an odd number.
Signed-off-by: Maciej Wieczor-Retman <maciej.wieczor-retman@xxxxxxxxx>
---
Changelog v11:
- Use DECLARE_BITMAP() on missing[] and recast to a u32 pointer for
iterating. Do it to avoid unaligned memory accesses when later using
for_each_set_bit() if NCAPINTS is going to ever become an odd number.
missing[] was previously an u32 array and for_each_set_bit() works on
unsigned long chunks of memory.
- Add a paragraph about the above to the patch message.
- Remove Peter's acked-by due to more changes.
Changelog v10:
- Shorten the comment before the sanity check.
- cpu -> CPU in the warning.
- NCAPINTS << 5 -> NCAPINTS * 32
Changelog v9:
- REQUIRED_MASK_INITIALIZER -> REQUIRED_MASK_INIT
- Redo the comments.
- Fix reverse xmas order.
- Inside for_each_set_bit: (void *) -> (unsigned long *).
- 16 -> X86_CAP_BUF_SIZE.
Changelog v6:
- Add Peter's acked-by tag.
- Rename patch subject to imperative form.
- Add a char buffer to the x86_cap_name() call.
arch/x86/kernel/cpu/common.c | 34 ++++++++++++++++++++++++++++++++++
1 file changed, 34 insertions(+)
diff --git a/arch/x86/kernel/cpu/common.c b/arch/x86/kernel/cpu/common.c
index 0e318f3d56cb..92159a0963c8 100644
--- a/arch/x86/kernel/cpu/common.c
+++ b/arch/x86/kernel/cpu/common.c
@@ -2005,6 +2005,38 @@ const char *x86_cap_name(unsigned int bit, char *buf)
return buf;
}
+/*
+ * As a sanity check compare the final x86_capability bitmask with the initial
+ * predefined required feature bits.
+ */
+static void verify_required_features(const struct cpuinfo_x86 *c)
+{
+ u32 required_features[NCAPINTS] = REQUIRED_MASK_INIT;
+ DECLARE_BITMAP(missing, NCAPINTS * 32);
+ char cap_buf[X86_CAP_BUF_SIZE];
+ u32 *missing_u32;
+ unsigned int i;
+ u32 error = 0;
+
+ memset(missing, 0, sizeof(missing));
+ missing_u32 = (u32 *)missing;
+
+ for (i = 0; i < NCAPINTS; i++) {
+ missing_u32[i] = ~c->x86_capability[i] & required_features[i];
+ error |= missing_u32[i];
+ }
+
+ if (!error)
+ return;
+
+ /* At least one required feature is missing */
+ pr_warn("CPU %d: missing required feature(s):", c->cpu_index);
+ for_each_set_bit(i, missing, NCAPINTS * 32)
+ pr_cont(" %s", x86_cap_name(i, cap_buf));
+ pr_cont("\n");
+ add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK);
+}
+
/*
* This does the hard work of actually picking apart the CPU stuff...
*/
@@ -2134,6 +2166,8 @@ static void identify_cpu(struct cpuinfo_x86 *c)
mcheck_cpu_init(c);
numa_add_cpu(smp_processor_id());
+
+ verify_required_features(c);
}
/*
--
2.53.0