[PATCH v2 5/5] x86/cpuid: check for dependencies violations in CPUID and attempt to fix them

Maxim Levitsky
2022-07-18T14:12:17Z

Due to configuration bugs, sometimes a CPU feature is disabled in CPUID,
but not features that depend on it.

For example, when one attempts to disable AVX2 but not AVX in the
guest's CPUID, the guest kernel crashes in aes-ni driver, when it
is used.

While the aes-ni driver can also be fixed to be more eager to detect this kind
of situation, it is simpler to fix this in a generic way since the kernel
has all the required info in the form of a dependency table.

Signed-off-by: Maxim Levitsky <mlevitsk@redhat.com>
---
arch/x86/kernel/cpu/cpuid-deps.c | 22 ++++++++++++++++++++++
1 file changed, 22 insertions(+)

diff --git a/arch/x86/kernel/cpu/cpuid-deps.c b/arch/x86/kernel/cpu/cpuid-deps.c
index e1b5f5c02c0106..376296c1f55ab2 100644
--- a/arch/x86/kernel/cpu/cpuid-deps.c
+++ b/arch/x86/kernel/cpu/cpuid-deps.c
@@ -94,6 +94,11 @@ static inline void clear_feature(struct cpuinfo_x86 *c, unsigned int feature)
set_bit(feature, (unsigned long *)cpu_caps_cleared);
}

+static inline bool test_feature(struct cpuinfo_x86 *c, unsigned int feature)
+{
+ return test_bit(feature, (unsigned long *)c->x86_capability);
+}
+
/* Take the capabilities and the BUG bits into account */
#define MAX_FEATURE_BITS ((NCAPINTS + NBUGINTS) * sizeof(u32) * 8)

@@ -136,6 +141,10 @@ void setup_clear_cpu_cap(unsigned int feature)
* Some CPU features depend on higher CPUID levels, which may not always
* be available due to CPUID level capping or broken virtualization
* software. Add those features to this table to auto-disable them.
+ *
+ * Also due to configuration bugs, some CPUID features might be present
+ * while CPUID features that they depend on are not present,
+ * e.g a AVX2 present but AVX is not present.
*/
struct cpuid_dependent_feature {
u32 feature;
@@ -153,6 +162,7 @@ cpuid_dependent_features[] = {
void filter_cpuid_features(struct cpuinfo_x86 *c)
{
const struct cpuid_dependent_feature *df;
+ const struct cpuid_dep *d;

for (df = cpuid_dependent_features; df->feature; df++) {

@@ -175,4 +185,16 @@ void filter_cpuid_features(struct cpuinfo_x86 *c)
pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, no CPUID level 0x%x\n",
x86_cap_flag(df->feature), df->level);
}
+
+ for (d = cpuid_deps; d->feature; d++) {
+
+ if (!test_feature(c, d->feature) || test_feature(c, d->depends))
+ continue;
+
+ clear_cpu_cap(c, d->feature);
+
+ pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, because it depends on "
+ X86_CAP_FMT " which is not supported in CPUID\n",
+ x86_cap_flag(d->feature), x86_cap_flag(d->depends));
+ }
}
--
2.34.3