From: Greg Kroah-Hartman Date: Wed, 27 Mar 2024 10:49:03 +0000 (+0100) Subject: 6.6-stable patches X-Git-Tag: v6.7.12~228 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=6e9ada6aa1affd19eee20827caae6b467751eeba;p=thirdparty%2Fkernel%2Fstable-queue.git 6.6-stable patches added patches: kvm-x86-use-a-switch-statement-and-macros-in-__feature_translate.patch --- diff --git a/queue-6.6/kvm-x86-use-a-switch-statement-and-macros-in-__feature_translate.patch b/queue-6.6/kvm-x86-use-a-switch-statement-and-macros-in-__feature_translate.patch new file mode 100644 index 00000000000..2c1dce74438 --- /dev/null +++ b/queue-6.6/kvm-x86-use-a-switch-statement-and-macros-in-__feature_translate.patch @@ -0,0 +1,96 @@ +From 80c883db87d9ffe2d685e91ba07a087b1c246c78 Mon Sep 17 00:00:00 2001 +From: Jim Mattson +Date: Mon, 23 Oct 2023 17:16:36 -0700 +Subject: KVM: x86: Use a switch statement and macros in __feature_translate() + +From: Jim Mattson + +commit 80c883db87d9ffe2d685e91ba07a087b1c246c78 upstream. + +Use a switch statement with macro-generated case statements to handle +translating feature flags in order to reduce the probability of runtime +errors due to copy+paste goofs, to make compile-time errors easier to +debug, and to make the code more readable. + +E.g. the compiler won't directly generate an error for duplicate if +statements + + if (x86_feature == X86_FEATURE_SGX1) + return KVM_X86_FEATURE_SGX1; + else if (x86_feature == X86_FEATURE_SGX2) + return KVM_X86_FEATURE_SGX1; + +and so instead reverse_cpuid_check() will fail due to the untranslated +entry pointing at a Linux-defined leaf, which provides practically no +hint as to what is broken + + arch/x86/kvm/reverse_cpuid.h:108:2: error: call to __compiletime_assert_450 declared with 'error' attribute: + BUILD_BUG_ON failed: x86_leaf == CPUID_LNX_4 + BUILD_BUG_ON(x86_leaf == CPUID_LNX_4); + ^ +whereas duplicate case statements very explicitly point at the offending +code: + + arch/x86/kvm/reverse_cpuid.h:125:2: error: duplicate case value '361' + KVM_X86_TRANSLATE_FEATURE(SGX2); + ^ + arch/x86/kvm/reverse_cpuid.h:124:2: error: duplicate case value '360' + KVM_X86_TRANSLATE_FEATURE(SGX1); + ^ + +And without macros, the opposite type of copy+paste goof doesn't generate +any error at compile-time, e.g. this yields no complaints: + + case X86_FEATURE_SGX1: + return KVM_X86_FEATURE_SGX1; + case X86_FEATURE_SGX2: + return KVM_X86_FEATURE_SGX1; + +Note, __feature_translate() is forcibly inlined and the feature is known +at compile-time, so the code generation between an if-elif sequence and a +switch statement should be identical. + +Signed-off-by: Jim Mattson +Link: https://lore.kernel.org/r/20231024001636.890236-2-jmattson@google.com +[sean: use a macro, rewrite changelog] +Signed-off-by: Sean Christopherson +Signed-off-by: Greg Kroah-Hartman +--- + arch/x86/kvm/reverse_cpuid.h | 25 ++++++++++++------------- + 1 file changed, 12 insertions(+), 13 deletions(-) + +--- a/arch/x86/kvm/reverse_cpuid.h ++++ b/arch/x86/kvm/reverse_cpuid.h +@@ -116,20 +116,19 @@ static __always_inline void reverse_cpui + */ + static __always_inline u32 __feature_translate(int x86_feature) + { +- if (x86_feature == X86_FEATURE_SGX1) +- return KVM_X86_FEATURE_SGX1; +- else if (x86_feature == X86_FEATURE_SGX2) +- return KVM_X86_FEATURE_SGX2; +- else if (x86_feature == X86_FEATURE_SGX_EDECCSSA) +- return KVM_X86_FEATURE_SGX_EDECCSSA; +- else if (x86_feature == X86_FEATURE_CONSTANT_TSC) +- return KVM_X86_FEATURE_CONSTANT_TSC; +- else if (x86_feature == X86_FEATURE_PERFMON_V2) +- return KVM_X86_FEATURE_PERFMON_V2; +- else if (x86_feature == X86_FEATURE_RRSBA_CTRL) +- return KVM_X86_FEATURE_RRSBA_CTRL; ++#define KVM_X86_TRANSLATE_FEATURE(f) \ ++ case X86_FEATURE_##f: return KVM_X86_FEATURE_##f + +- return x86_feature; ++ switch (x86_feature) { ++ KVM_X86_TRANSLATE_FEATURE(SGX1); ++ KVM_X86_TRANSLATE_FEATURE(SGX2); ++ KVM_X86_TRANSLATE_FEATURE(SGX_EDECCSSA); ++ KVM_X86_TRANSLATE_FEATURE(CONSTANT_TSC); ++ KVM_X86_TRANSLATE_FEATURE(PERFMON_V2); ++ KVM_X86_TRANSLATE_FEATURE(RRSBA_CTRL); ++ default: ++ return x86_feature; ++ } + } + + static __always_inline u32 __feature_leaf(int x86_feature) diff --git a/queue-6.6/series b/queue-6.6/series index b2e4c15b04f..4c91e0eb1a9 100644 --- a/queue-6.6/series +++ b/queue-6.6/series @@ -1 +1,2 @@ kvm-x86-advertise-cpuid.-eax-7-ecx-2-edx-to-userspace.patch +kvm-x86-use-a-switch-statement-and-macros-in-__feature_translate.patch