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

Jim Mattson
2023-10-24T00:16:52Z

The compiler will probably do better than linear search.

No functional change intended.

Signed-off-by: Jim Mattson <jmattson@google.com>
---
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