[PATCH 2/2] KVM: x86: Use a switch statement in __feature_translate()

From: Jim Mattson
Date: Mon Oct 23 2023 - 20:16:55 EST


The compiler will probably do better than linear search.

No functional change intended.

Signed-off-by: Jim Mattson <jmattson@xxxxxxxxxx>
---
arch/x86/kvm/reverse_cpuid.h | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/arch/x86/kvm/reverse_cpuid.h b/arch/x86/kvm/reverse_cpuid.h
index 17007016d8b5..da52f5ea0351 100644
--- a/arch/x86/kvm/reverse_cpuid.h
+++ b/arch/x86/kvm/reverse_cpuid.h
@@ -116,20 +116,22 @@ static __always_inline void reverse_cpuid_check(unsigned int x86_leaf)
*/
static __always_inline u32 __feature_translate(int x86_feature)
{
- if (x86_feature == X86_FEATURE_SGX1)
+ switch (x86_feature) {
+ case X86_FEATURE_SGX1:
return KVM_X86_FEATURE_SGX1;
- else if (x86_feature == X86_FEATURE_SGX2)
+ case X86_FEATURE_SGX2:
return KVM_X86_FEATURE_SGX2;
- else if (x86_feature == X86_FEATURE_SGX_EDECCSSA)
+ case X86_FEATURE_SGX_EDECCSSA:
return KVM_X86_FEATURE_SGX_EDECCSSA;
- else if (x86_feature == X86_FEATURE_CONSTANT_TSC)
+ case X86_FEATURE_CONSTANT_TSC:
return KVM_X86_FEATURE_CONSTANT_TSC;
- else if (x86_feature == X86_FEATURE_PERFMON_V2)
+ case X86_FEATURE_PERFMON_V2:
return KVM_X86_FEATURE_PERFMON_V2;
- else if (x86_feature == X86_FEATURE_RRSBA_CTRL)
+ case X86_FEATURE_RRSBA_CTRL:
return KVM_X86_FEATURE_RRSBA_CTRL;
-
- return x86_feature;
+ default:
+ return x86_feature;
+ }
}

static __always_inline u32 __feature_leaf(int x86_feature)
--
2.42.0.758.gaed0368e0e-goog