Skip to contentSkip to conversation

x86: cpuid: improve support for broken CPUID configurations

PATCH Superseded Maxim Levitsky 3 participants 15 messages Jun 22, 2022

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86: cpuid: improve support for broken CPUID configurations

This patch series aims to harden the cpuid code against the case when the hypervisor exposes a broken CPUID configuration to the guest, in the form of having a feature disabled but not features that depend on it.

This is the more generic way to fix kernel panic in aes-ni kernel driver, which was triggered by CPUID configuration in which AVX is disabled but not AVX2.

https://lore.kernel.org/all/[email protected]/T/

This was tested by booting a guest with AVX disabled and not AVX2, and observing that both a warning is now printed in dmesg, and that avx2 is gone from /proc/cpuinfo.

Best regards,
Maxim Levitsky

Maxim Levitsky (4):
perf/x86/intel/lbr: use setup_clear_cpu_cap instead of clear_cpu_cap x86/cpuid: refactor setup_clear_cpu_cap/clear_feature x86/cpuid: move filter_cpuid_features to cpuid-deps.c x86/cpuid: check for dependencies violations in CPUID and attempt to
fix them

 arch/x86/events/intel/lbr.c       |  2 +-
 arch/x86/include/asm/cpufeature.h |  1 +
 arch/x86/kernel/cpu/common.c      | 50 +-----------------
 arch/x86/kernel/cpu/cpuid-deps.c  | 84 +++++++++++++++++++++++++++----
 4 files changed, 78 insertions(+), 59 deletions(-)
Signature · 1 line
2.26.3
#1 lore raw

PATCH 1/4 perf/x86/intel/lbr: use setup_clear_cpu_cap instead of clear_cpu_cap

clear_cpu_cap(&boot_cpu_data) is very similar to setup_clear_cpu_cap except that the latter also sets a bit in 'cpu_caps_cleared' which later clears the same cap in secondary cpus, which is likely what is meant here.

---
 arch/x86/events/intel/lbr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/lbr.c +1 -1
index 13179f31fe10fa..b08715172309a7 100644
@@ -1860,7 +1860,7 @@ void __init intel_pmu_arch_lbr_init(void)
 	return;
 
 clear_arch_lbr:
-	clear_cpu_cap(&boot_cpu_data, X86_FEATURE_ARCH_LBR);
+	setup_clear_cpu_cap(X86_FEATURE_ARCH_LBR);
 }
 
 /**
Signature · 1 line
2.26.3
#2 lore raw

Re: [PATCH 1/4] perf/x86/intel/lbr: use setup_clear_cpu_cap instead of clear_cpu_cap

Quoted 6 lines · Maxim Levitsky

clear_cpu_cap(&boot_cpu_data) is very similar to setup_clear_cpu_cap except that the latter also sets a bit in 'cpu_caps_cleared' which later clears the same cap in secondary cpus, which is likely what is meant here.

Seems like a:

Fixes 47125db27e47 ("perf/x86/intel/lbr: Support Architectural LBR")

would be in order.

Kan, does this change look right to you?

Quoted 16 lines
 arch/x86/events/intel/lbr.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/lbr.c +1 -1
index 13179f31fe10fa..b08715172309a7 100644
@@ -1860,7 +1860,7 @@ void __init intel_pmu_arch_lbr_init(void)
 	return;
 
 clear_arch_lbr:
-	clear_cpu_cap(&boot_cpu_data, X86_FEATURE_ARCH_LBR);
+	setup_clear_cpu_cap(X86_FEATURE_ARCH_LBR);
 }
 
 /**
#3 lore raw

Re: [PATCH 1/4] perf/x86/intel/lbr: use setup_clear_cpu_cap instead of clear_cpu_cap

Quoted 8 lines · Dave Hansen
Quoted 6 lines · Maxim Levitsky

clear_cpu_cap(&boot_cpu_data) is very similar to setup_clear_cpu_cap except that the latter also sets a bit in 'cpu_caps_cleared' which later clears the same cap in secondary cpus, which is likely what is meant here.

Quoted 7 lines

Seems like a:

Fixes 47125db27e47 ("perf/x86/intel/lbr: Support Architectural LBR")

would be in order.

Kan, does this change look right to you?

For the current implementation, the Arch LBR feature should be either supported by all the CPUs or disabled on all the CPUs. It cannot be only enabled for partial CPUs, even in a hybrid platform. So the current code only check the boot CPU via static_cpu_has().

Ideally, Yes, I think it may be better to clear the bit for all CPUs, which makes the capability consistent among CPUs.

Thanks,
Kan

Quoted 18 lines
Quoted 17 lines
  arch/x86/events/intel/lbr.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/lbr.c +1 -1
index 13179f31fe10fa..b08715172309a7 100644
@@ -1860,7 +1860,7 @@ void __init intel_pmu_arch_lbr_init(void)
  	return;
 
  clear_arch_lbr:
-	clear_cpu_cap(&boot_cpu_data, X86_FEATURE_ARCH_LBR);
+	setup_clear_cpu_cap(X86_FEATURE_ARCH_LBR);
  }
 
  /**
Reviewed-by
#4 lore raw

Re: [PATCH 1/4] perf/x86/intel/lbr: use setup_clear_cpu_cap instead of clear_cpu_cap

Quoted 15 lines · Kan
Quoted 12 lines · Dave Hansen
Quoted 6 lines · Maxim Levitsky

clear_cpu_cap(&boot_cpu_data) is very similar to setup_clear_cpu_cap except that the latter also sets a bit in 'cpu_caps_cleared' which later clears the same cap in secondary cpus, which is likely what is meant here.

Seems like a:

Fixes 47125db27e47 ("perf/x86/intel/lbr: Support Architectural LBR")
Quoted 9 lines
Quoted 3 lines

would be in order.

Kan, does this change look right to you?

For the current implementation, the Arch LBR feature should be either supported by all the CPUs or disabled on all the CPUs. It cannot be only enabled for partial CPUs, even in a hybrid platform. So the current code only check the boot CPU via static_cpu_has().

Ideally, Yes, I think it may be better to clear the bit for all CPUs, which makes the capability consistent among CPUs.

Thanks,
Kan

Quoted 18 lines

Thanks,
Kan

Quoted 16 lines
Quoted 15 lines

  arch/x86/events/intel/lbr.c | 2 +-

  1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/events/intel/lbr.c +0 -0
index 13179f31fe10fa..b08715172309a7 100644
@@ -1860,7 +1860,7 @@ void __init intel_pmu_arch_lbr_init(void)
       return;

  clear_arch_lbr:
-    clear_cpu_cap(&boot_cpu_data, X86_FEATURE_ARCH_LBR); +    setup_clear_cpu_cap(X86_FEATURE_ARCH_LBR); }
  /**

#5 lore raw

PATCH 2/4 x86/cpuid: refactor setup_clear_cpu_cap/clear_feature

In reply to Maxim Levitsky

Simplify the code a bit by always passing &boot_cpu_data in case the setup_clear_cpu_cap was called.

Also unify clear_cpu_cap and do_clear_cpu_cap.

---
 arch/x86/kernel/cpu/cpuid-deps.c | 17 +++++------------
 1 file changed, 5 insertions(+), 12 deletions(-)
arch/x86/kernel/cpu/cpuid-deps.c +5 -12
index c881bcafba7d70..d6777d07ba3302 100644
@@ -88,18 +88,16 @@ static inline void clear_feature(struct cpuinfo_x86 *c, unsigned int feature)
 	 * rest of the cpufeature code uses atomics as well, so keep it for
 	 * consistency. Cleanup all of it separately.
 	 */
-	if (!c) {
-		clear_cpu_cap(&boot_cpu_data, feature);
+	clear_bit(feature, (unsigned long *)c->x86_capability);
+
+	if (c == &boot_cpu_data)
 		set_bit(feature, (unsigned long *)cpu_caps_cleared);
-	} else {
-		clear_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)
 
-static void do_clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature)
+void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature)
 {
 	DECLARE_BITMAP(disable, MAX_FEATURE_BITS);
 	const struct cpuid_dep *d;
@@ -129,12 +127,7 @@ static void do_clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature)
 	} while (changed);
 }
 
-void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature)
-{
-	do_clear_cpu_cap(c, feature);
-}
-
 void setup_clear_cpu_cap(unsigned int feature)
 {
-	do_clear_cpu_cap(NULL, feature);
+	clear_cpu_cap(&boot_cpu_data, feature);
 }
Signature · 1 line
2.26.3
#6 lore raw

Re: [PATCH 2/4] x86/cpuid: refactor setup_clear_cpu_cap/clear_feature

Quoted 4 lines · Maxim Levitsky

Simplify the code a bit by always passing &boot_cpu_data in case the setup_clear_cpu_cap was called.

Also unify clear_cpu_cap and do_clear_cpu_cap.

Please always add a "()" suffix to functions. "foo" is a variable, but "foo()" is a function.

I also really like when a changelog has a clear problem statement. I _think_ the problem here is something along the lines of the 'c' argument to clear_feature() having different behavior when it is NULL versus '&boot_cpu_data'.

Basically, there's no reason to support clearing a bit in '&boot_cpu_data' without also setting that bit in 'cpu_caps_cleared'.

Quoted 4 lines

{

arch/x86/kernel/cpu/cpuid-deps.c +1 -1
-	do_clear_cpu_cap(NULL, feature);
+	clear_cpu_cap(&boot_cpu_data, feature);
 }
#7 lore raw

Re: [PATCH 2/4] x86/cpuid: refactor setup_clear_cpu_cap/clear_feature

Quoted 8 lines · Dave Hansen
Quoted 4 lines · Maxim Levitsky

Simplify the code a bit by always passing &boot_cpu_data in case the setup_clear_cpu_cap was called.

Also unify clear_cpu_cap and do_clear_cpu_cap.

Please always add a "()" suffix to functions. "foo" is a variable, but "foo()" is a function.

Will do next time!

Quoted 5 lines

I also really like when a changelog has a clear problem statement. I _think_ the problem here is something along the lines of the 'c' argument to clear_feature() having different behavior when it is NULL versus '&boot_cpu_data'.

To be honest I didn't try to fix any problem here, I just wanted to simplify clear_feature() a bit by avoiding a recursive call to clear_cpu_cap.

Quoted 8 lines

Basically, there's no reason to support clearing a bit in '&boot_cpu_data' without also setting that bit in 'cpu_caps_cleared'.

arch/x86/kernel/cpu/cpuid-deps.c static void do_clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature) · line 133

Quoted 4 lines

{

arch/x86/kernel/cpu/cpuid-deps.c +1 -1
-	do_clear_cpu_cap(NULL, feature);
+	clear_cpu_cap(&boot_cpu_data, feature);
 }

I'll try to think about a better changelog message for this.

Thank you!
Best regards,
Maxim Levitsky

#8 lore raw

PATCH 3/4 x86/cpuid: move filter_cpuid_features to cpuid-deps.c

In reply to Maxim Levitsky

No functional change intended.

---
 arch/x86/include/asm/cpufeature.h |  1 +
 arch/x86/kernel/cpu/common.c      | 46 -----------------------------
 arch/x86/kernel/cpu/cpuid-deps.c  | 48 +++++++++++++++++++++++++++++++
 3 files changed, 49 insertions(+), 46 deletions(-)
arch/x86/include/asm/cpufeature.h +1 -0
index ea34cc31b0474f..3eb5fe0d654e63 100644
@@ -147,6 +147,7 @@ extern const char * const x86_bug_flags[NBUGINTS*32];
 
 extern void setup_clear_cpu_cap(unsigned int bit);
 extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
+extern void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn);
 
 #define setup_force_cpu_cap(bit) do { \
 	set_cpu_cap(&boot_cpu_data, bit);	\
arch/x86/kernel/cpu/common.c +0 -46

52 lines not on this page · raw message

arch/x86/kernel/cpu/cpuid-deps.c +48 -0

51 lines not on this page · raw message

Signature · 1 line
2.26.3
#9 lore raw

Re: [PATCH 3/4] x86/cpuid: move filter_cpuid_features to cpuid-deps.c

Quoted 1 line · Maxim Levitsky

No functional change intended.

It would be really nice to at least write a "why" sentence. You wrote the "what" (move code), but I have no idea why you are moving it.

Quoted 2 lines
 arch/x86/kernel/cpu/common.c      | 46 -----------------------------
 arch/x86/kernel/cpu/cpuid-deps.c  | 48 +++++++++++++++++++++++++++++++

That looks a wee bit odd for a code move. Where did the 2 lines go?

Quoted 21 lines
arch/x86/include/asm/cpufeature.h +1 -0
index ea34cc31b0474f..3eb5fe0d654e63 100644
@@ -147,6 +147,7 @@ extern const char * const x86_bug_flags[NBUGINTS*32];
 
 extern void setup_clear_cpu_cap(unsigned int bit);
 extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
+extern void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn);
 
 #define setup_force_cpu_cap(bit) do { \
 	set_cpu_cap(&boot_cpu_data, bit);	\
arch/x86/kernel/cpu/common.c +0 -1
index 4730b0a58f24a5..4cc79971d2d847 100644
@@ -620,52 +620,6 @@ __noendbr void cet_disable(void)
 		wrmsrl(MSR_IA32_S_CET, 0);
 }
 
-/*

...

Quoted 4 lines

-}

/*
* Naming convention should be: <Name> [(<Codename>)]

One, by leaving extra whitespace.

arch/x86/kernel/cpu/cpuid-deps.c void setup_clear_cpu_cap(unsigned int feature) · line 136

Quoted 11 lines
arch/x86/kernel/cpu/cpuid-deps.c +3 -0
index d6777d07ba3302..bcb091d02a754b 100644
@@ -131,3 +131,51 @@ void setup_clear_cpu_cap(unsigned int feature)
 {
 	clear_cpu_cap(&boot_cpu_data, feature);
 }
+
+
+/*

Two, by adding extra whitespace.

#10 lore raw

Re: [PATCH 3/4] x86/cpuid: move filter_cpuid_features to cpuid-deps.c

Quoted 5 lines · Dave Hansen
Quoted 1 line · Maxim Levitsky

No functional change intended.

It would be really nice to at least write a "why" sentence. You wrote the "what" (move code), but I have no idea why you are moving it.

This is a good point, I will add a justification in V2.

Quoted 35 lines
Quoted 3 lines
 arch/x86/kernel/cpu/common.c      | 46 -----------------------------
 arch/x86/kernel/cpu/cpuid-deps.c  | 48 +++++++++++++++++++++++++++++++

That looks a wee bit odd for a code move. Where did the 2 lines go?

Quoted 21 lines
arch/x86/include/asm/cpufeature.h +1 -0
index ea34cc31b0474f..3eb5fe0d654e63 100644
@@ -147,6 +147,7 @@ extern const char * const x86_bug_flags[NBUGINTS*32];
 
 extern void setup_clear_cpu_cap(unsigned int bit);
 extern void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int bit);
+extern void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn);
 
 #define setup_force_cpu_cap(bit) do { \
 	set_cpu_cap(&boot_cpu_data, bit);	\
arch/x86/kernel/cpu/common.c +0 -1
index 4730b0a58f24a5..4cc79971d2d847 100644
@@ -620,52 +620,6 @@ __noendbr void cet_disable(void)
 		wrmsrl(MSR_IA32_S_CET, 0);
 }
 
-/*

...

Quoted 4 lines

-}

/*
* Naming convention should be: <Name> [(<Codename>)]

One, by leaving extra whitespace.

Quoted 14 lines

arch/x86/kernel/cpu/cpuid-deps.c void setup_clear_cpu_cap(unsigned int feature) · line 136

Quoted 11 lines
arch/x86/kernel/cpu/cpuid-deps.c +3 -0
index d6777d07ba3302..bcb091d02a754b 100644
@@ -131,3 +131,51 @@ void setup_clear_cpu_cap(unsigned int feature)
 {
 	clear_cpu_cap(&boot_cpu_data, feature);
 }
+
+
+/*

Two, by adding extra whitespace.

I will fix this, I didn't notice that I added new lines. Next time I move code around, I'll check that the number count matches.

Thanks!
Best regards,
Maxim Levitsky

#11 lore raw

PATCH 4/4 x86/cpuid: check for dependencies violations in CPUID and attempt to fix them

In reply to Maxim Levitsky

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

While the above is not supported, the kernel should try to not crash, and clearing the dependent cpu caps is the best way to do it.

---
 arch/x86/kernel/cpu/common.c     |  4 ++--
 arch/x86/kernel/cpu/cpuid-deps.c | 27 +++++++++++++++++++++++++--
 2 files changed, 27 insertions(+), 4 deletions(-)
arch/x86/kernel/cpu/common.c +2 -2
index 4cc79971d2d847..c83a8f447d6aed 100644
@@ -1469,7 +1469,7 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)
 			this_cpu->c_early_init(c);
 
 		c->cpu_index = 0;
-		filter_cpuid_features(c, false);
+		filter_cpuid_features(c, true);
 
 		if (this_cpu->c_bsp_init)
 			this_cpu->c_bsp_init(c);
@@ -1757,7 +1757,7 @@ static void identify_cpu(struct cpuinfo_x86 *c)
 	 */
 
 	/* Filter out anything that depends on CPUID levels we don't have */
-	filter_cpuid_features(c, true);
+	filter_cpuid_features(c, false);
 
 	/* If the model name is still unset, do table lookup. */
 	if (!c->x86_model_id[0]) {
arch/x86/kernel/cpu/cpuid-deps.c +25 -2
index bcb091d02a754b..6d9c0e39851805 100644
@@ -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)
 
@@ -127,6 +132,7 @@ void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature)
 	} while (changed);
 }
 
+
 void setup_clear_cpu_cap(unsigned int feature)
 {
 	clear_cpu_cap(&boot_cpu_data, feature);
@@ -137,6 +143,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;
@@ -151,9 +161,10 @@ cpuid_dependent_features[] = {
 	{ 0, 0 }
 };
 
-void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
+void filter_cpuid_features(struct cpuinfo_x86 *c, bool early)
 {
 	const struct cpuid_dependent_feature *df;
+	const struct cpuid_dep *d;
 
 	for (df = cpuid_dependent_features; df->feature; df++) {
 
@@ -172,10 +183,22 @@ void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
 			continue;
 
 		clear_cpu_cap(c, df->feature);
-		if (!warn)
+		if (early)
 			continue;
 
 		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_feature(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));
+	}
 }
Signature · 1 line
2.26.3
#12 lore raw

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

Quoted 5 lines · Maxim Levitsky

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

While the above is not supported, the kernel should try to not crash, and clearing the dependent cpu caps is the best way to do it.

That's a rather paltry changelog.

If I remember correctly, there's a crystal clear problem:

If a CPU enumerates support for AVX2 but AVX via CPUID, the kernel crashes.

There's also a follow-on problem. The kernel has all the data it needs to fix this, but just doesn't consult it:

To make matters worse, the kernel _knows_ that this is an ill- advised situation: The kernel prevents itself from clearing the software representation of the AVX CPUID bit without also clearing AVX2.

But, the kernel only consults this knowledge when it is clearing cpu_cap bits. It does not consult this information when it is populating those cpu_cap bits.

Quoted 14 lines
arch/x86/kernel/cpu/common.c +1 -1
index 4cc79971d2d847..c83a8f447d6aed 100644
@@ -1469,7 +1469,7 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)
 			this_cpu->c_early_init(c);
 
 		c->cpu_index = 0;
-		filter_cpuid_features(c, false);
+		filter_cpuid_features(c, true);
 
 		if (this_cpu->c_bsp_init)
 			this_cpu->c_bsp_init(c);

arch/x86/kernel/cpu/common.c static void identify_cpu(struct cpuinfo_x86 *c) · line 1763

Quoted 8 lines
arch/x86/kernel/cpu/common.c +1 -1
@@ -1757,7 +1757,7 @@ static void identify_cpu(struct cpuinfo_x86 *c)
 	 */
 
 	/* Filter out anything that depends on CPUID levels we don't have */
-	filter_cpuid_features(c, true);
+	filter_cpuid_features(c, false);
 
 	/* If the model name is still unset, do table lookup. */
 	if (!c->x86_model_id[0]) {

While we're at it, could we please rid ourselves of this unreadable mystery true/false gunk?

Quoted 17 lines
arch/x86/kernel/cpu/cpuid-deps.c +5 -0
index bcb091d02a754b..6d9c0e39851805 100644
@@ -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)
 

arch/x86/kernel/cpu/cpuid-deps.c void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature) · line 136

Quoted 5 lines
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -127,6 +132,7 @@ void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature)
 	} while (changed);
 }
 
+
 void setup_clear_cpu_cap(unsigned int feature)

More superfluous whitespace.

Quoted 14 lines
arch/x86/kernel/cpu/cpuid-deps.c +4 -0
 {
 	clear_cpu_cap(&boot_cpu_data, feature);
@@ -137,6 +143,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;

arch/x86/kernel/cpu/cpuid-deps.c cpuid_dependent_features[] = { · line 165

Quoted 6 lines
arch/x86/kernel/cpu/cpuid-deps.c +1 -1
@@ -151,9 +161,10 @@ cpuid_dependent_features[] = {
 	{ 0, 0 }
 };
 
-void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
+void filter_cpuid_features(struct cpuinfo_x86 *c, bool early)
 {

I have at least an inkling what 'warn' could mean. But, 'early'? One man's 'early' is another one's 'late'.

arch/x86/kernel/cpu/cpuid-deps.c void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) · line 187

Quoted 12 lines
arch/x86/kernel/cpu/cpuid-deps.c +2 -1
 	const struct cpuid_dependent_feature *df;
+	const struct cpuid_dep *d;
 
 	for (df = cpuid_dependent_features; df->feature; df++) {
 
@@ -172,10 +183,22 @@ void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
 			continue;
 
 		clear_cpu_cap(c, df->feature);
-		if (!warn)
+		if (early)
 			continue;

Why is it that 'early' calls don't want warnings?

Quoted 6 lines
arch/x86/kernel/cpu/cpuid-deps.c +3 -0
 		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++) {
+

arch/x86/kernel/cpu/cpuid-deps.c void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) · line 204

Quoted 10 lines
arch/x86/kernel/cpu/cpuid-deps.c +9 -0
+		if (!test_feature(c, d->feature) || test_feature(c, d->depends))
+			continue;
+
+		clear_feature(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));
+	}
 }

The do_clear_cpu_cap() does this with a loop, presumably because a later (higher index in the array) feature in cpuid_deps[] could theoretically clear an earlier (lower index) feature.

Also, is that message strictly correct? There might have been a clearcpuid= argument or even another dependency that ended up clearing a bit. It might have nothing to do with CPUID itself.

#13 lore raw

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

Quoted 17 lines · Dave Hansen
Quoted 5 lines · Maxim Levitsky

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

While the above is not supported, the kernel should try to not crash, and clearing the dependent cpu caps is the best way to do it.

That's a rather paltry changelog.

If I remember correctly, there's a crystal clear problem:

If a CPU enumerates support for AVX2 but AVX via CPUID, the kernel crashes.

There's also a follow-on problem. The kernel has all the data it needs to fix this, but just doesn't consult it:

Quoted 8 lines

To make matters worse, the kernel _knows_ that this is an ill- advised situation: The kernel prevents itself from clearing the software representation of the AVX CPUID bit without also clearing AVX2.

But, the kernel only consults this knowledge when it is clearing cpu_cap bits. It does not consult this information when it is populating those cpu_cap bits.

Yes, I agree. I'll update the changelog with something more in depth.

Quoted 21 lines
Quoted 21 lines
arch/x86/kernel/cpu/common.c +2 -2
index 4cc79971d2d847..c83a8f447d6aed 100644
@@ -1469,7 +1469,7 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)
 			this_cpu->c_early_init(c);
 
 		c->cpu_index = 0;
-		filter_cpuid_features(c, false);
+		filter_cpuid_features(c, true);
 
 		if (this_cpu->c_bsp_init)
 			this_cpu->c_bsp_init(c);
@@ -1757,7 +1757,7 @@ static void identify_cpu(struct cpuinfo_x86 *c)
 	 */
 
 	/* Filter out anything that depends on CPUID levels we don't have */
-	filter_cpuid_features(c, true);
+	filter_cpuid_features(c, false);
 
Quoted 5 lines

arch/x86/kernel/cpu/common.c static void identify_cpu(struct cpuinfo_x86 *c) · line 1763

Quoted 2 lines
arch/x86/kernel/cpu/common.c +0 -0
 	/* If the model name is still unset, do table lookup. */
 	if (!c->x86_model_id[0]) {

While we're at it, could we please rid ourselves of this unreadable mystery true/false gunk?

It is present if I understand the code correctly to avoid printing a warning twice. It used to be 'warn' parameter, and I changed it to 'early' parameter, inverting its boolean value, because I have seen that warning is not printed at all, and I assumed that it is because the first early call already clears the cpuid cap and the second call doesn't get the warning.

Now however, looking at that I think that the same will
happen with the cpuid level fitering as well, and thus we can just remove that 'warn' parameter.

Quoted 17 lines
Quoted 17 lines
arch/x86/kernel/cpu/cpuid-deps.c +5 -0
index bcb091d02a754b..6d9c0e39851805 100644
@@ -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)
 
Quoted 8 lines

arch/x86/kernel/cpu/cpuid-deps.c void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature) · line 136

Quoted 6 lines
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
@@ -127,6 +132,7 @@ void clear_cpu_cap(struct cpuinfo_x86 *c, unsigned int feature)
 	} while (changed);
 }
 
+
 void setup_clear_cpu_cap(unsigned int feature)

More superfluous whitespace.

Sorry about that, will check better next time.

Quoted 18 lines
Quoted 18 lines
arch/x86/kernel/cpu/cpuid-deps.c +4 -0
 {
 	clear_cpu_cap(&boot_cpu_data, feature);
@@ -137,6 +143,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;
@@ -151,9 +161,10 @@ cpuid_dependent_features[] = {
 	{ 0, 0 }
 };
 
Quoted 6 lines

arch/x86/kernel/cpu/cpuid-deps.c cpuid_dependent_features[] = { · line 165

Quoted 3 lines
arch/x86/kernel/cpu/cpuid-deps.c +1 -1
-void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
+void filter_cpuid_features(struct cpuinfo_x86 *c, bool early)
 {

I have at least an inkling what 'warn' could mean. But, 'early'? One man's 'early' is another one's 'late'.

I understand what you mean, as I said above, I will try to reproduce the original issue of cpuid level mismatch and see if I can remove the warn parameter at all.

Quoted 9 lines
Quoted 9 lines
arch/x86/kernel/cpu/cpuid-deps.c +1 -0
 	const struct cpuid_dependent_feature *df;
+	const struct cpuid_dep *d;
 
 	for (df = cpuid_dependent_features; df->feature; df++) {
 
@@ -172,10 +183,22 @@ void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn)
 			continue;
 
Quoted 6 lines

arch/x86/kernel/cpu/cpuid-deps.c void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) · line 187

Quoted 4 lines
arch/x86/kernel/cpu/cpuid-deps.c +1 -1
 		clear_cpu_cap(c, df->feature);
-		if (!warn)
+		if (early)
 			continue;

Why is it that 'early' calls don't want warnings?

I don't know to be honest, except that I assumed that this allows to not print the warning twice, but as I said above, I might be able to just remove that code.

Quoted 13 lines
Quoted 13 lines
arch/x86/kernel/cpu/cpuid-deps.c +9 -0
 		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_feature(c, d->feature);
+
+		pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, because it depends on "
Quoted 8 lines

arch/x86/kernel/cpu/cpuid-deps.c void filter_cpuid_features(struct cpuinfo_x86 *c, bool warn) · line 204

Quoted 4 lines
arch/x86/kernel/cpu/cpuid-deps.c +3 -0
+			X86_CAP_FMT " which is not supported in CPUID\n",
+			x86_cap_flag(d->feature), x86_cap_flag(d->depends));
+	}
 }

The do_clear_cpu_cap() does this with a loop, presumably because a later (higher index in the array) feature in cpuid_deps[] could theoretically clear an earlier (lower index) feature.

Sorry this is my silly mistake. I intended to call clear_cpu_cap here, which will if needed disable all the depedencies, so a loop doesn't seem to be needed here.

It's not very efficient but this is only done once per vCPU so shouldn't matter.

Quoted 4 lines

Also, is that message strictly correct? There might have been a clearcpuid= argument or even another dependency that ended up clearing a bit. It might have nothing to do with CPUID itself.

I think it should work, because clearcpuid= will end up calling clear_cpu_cap which will disable both the requested feature and everything that depends on it, thus filter_cpuid_features should not notice any inconsistencies.

Other way around, if the clear_cpu_cap is called before filter_cpuid_features, it might 'fix' the inconsistency, and silence the warning but that isn't an issue IMHO.

Thanks a lot for the review,
Best regards,
Maxim Levitsky

Quoted 1 line
#14 lore raw

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

Quoted 56 lines · Maxim Levitsky
Quoted 17 lines · Dave Hansen
Quoted 5 lines · Maxim Levitsky

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

While the above is not supported, the kernel should try to not crash, and clearing the dependent cpu caps is the best way to do it.

That's a rather paltry changelog.

If I remember correctly, there's a crystal clear problem:

If a CPU enumerates support for AVX2 but AVX via CPUID, the kernel crashes.

There's also a follow-on problem. The kernel has all the data it needs to fix this, but just doesn't consult it:

Quoted 8 lines

To make matters worse, the kernel _knows_ that this is an ill- advised situation: The kernel prevents itself from clearing the software representation of the AVX CPUID bit without also clearing AVX2.

But, the kernel only consults this knowledge when it is clearing cpu_cap bits. It does not consult this information when it is populating those cpu_cap bits.

Yes, I agree. I'll update the changelog with something more in depth.

Quoted 21 lines
Quoted 21 lines
arch/x86/kernel/cpu/common.c +2 -2
index 4cc79971d2d847..c83a8f447d6aed 100644
@@ -1469,7 +1469,7 @@ static void __init early_identify_cpu(struct cpuinfo_x86 *c)
 			this_cpu->c_early_init(c);
 
 		c->cpu_index = 0;
-		filter_cpuid_features(c, false);
+		filter_cpuid_features(c, true);
 
 		if (this_cpu->c_bsp_init)
 			this_cpu->c_bsp_init(c);
@@ -1757,7 +1757,7 @@ static void identify_cpu(struct cpuinfo_x86 *c)
 	 */
 
 	/* Filter out anything that depends on CPUID levels we don't have */
-	filter_cpuid_features(c, true);
+	filter_cpuid_features(c, false);
 
Quoted 5 lines

arch/x86/kernel/cpu/common.c static void identify_cpu(struct cpuinfo_x86 *c) · line 1763

Quoted 2 lines
arch/x86/kernel/cpu/common.c +0 -0
 	/* If the model name is still unset, do table lookup. */
 	if (!c->x86_model_id[0]) {

While we're at it, could we please rid ourselves of this unreadable mystery true/false gunk?

Quoted 5 lines

It is present if I understand the code correctly to avoid printing a warning twice. It used to be 'warn' parameter, and I changed it to 'early' parameter, inverting its boolean value, because I have seen that warning is not printed at all, and I assumed that it is because the first early call already clears the cpuid cap and the second call doesn't get the warning.

If the goal is truly to suppress the warning once per dependency, it would be trivial to do:

	struct cpuid_dep {
	        unsigned int    feature;
        	unsigned int    depends;
+		bool warned;
	};

Then:

	...
	clear_feature(c, d->feature);

+ if (!d->warned)
+ pr_warn(...);
+ d->warned = 1;

You could even have two bits if you feel that we need separate warnings for the hardware (true CPUID) and software (setup_clear_cpu_cap()) checks.

Quoted 21 lines
Quoted 12 lines
Quoted 12 lines
arch/x86/kernel/cpu/common.c +9 -0
 		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_feature(c, d->feature);
+
+		pr_warn("CPU: CPU feature " X86_CAP_FMT " disabled, because it depends on "
Quoted 8 lines

arch/x86/kernel/cpu/common.c

Quoted 4 lines
arch/x86/kernel/cpu/common.c +3 -0
+			X86_CAP_FMT " which is not supported in CPUID\n",
+			x86_cap_flag(d->feature), x86_cap_flag(d->depends));
+	}
 }

The do_clear_cpu_cap() does this with a loop, presumably because a later (higher index in the array) feature in cpuid_deps[] could theoretically clear an earlier (lower index) feature.

Quoted 5 lines

Sorry this is my silly mistake. I intended to call clear_cpu_cap here, which will if needed disable all the depedencies, so a loop doesn't seem to be needed here.

It's not very efficient but this is only done once per vCPU so shouldn't matter.

Right, and it only loops if it is actually clearing features, which is arguably the result of a broken CPU or hypervisor, which is rare too. It's not inefficient in any case that matters.