>From fd2d041407ce8c3c8c643d9d64c63a8a05ba85af Mon Sep 17 00:00:00 2001 From: Sean Christopherson Date: Tue, 26 Jul 2022 09:37:49 -0700 Subject: [PATCH 2/2] KVM: x86: Cache guest CPUID's PDMC for fast lookup Add a cache entry for X86_FEATURE_PDCM to allow for expedited lookups. For all intents and purposes, VMX queries X86_FEATURE_PDCM by default on every VM-Enter. Signed-off-by: Sean Christopherson --- arch/x86/kvm/cpuid.c | 1 + arch/x86/kvm/cpuid.h | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c index 27b25fdb4335..fd32fddd7bc1 100644 --- a/arch/x86/kvm/cpuid.c +++ b/arch/x86/kvm/cpuid.c @@ -420,6 +420,7 @@ static int kvm_set_cpuid(struct kvm_vcpu *vcpu, struct kvm_cpuid_entry2 *e2, /* Update the cache before doing anything else. */ vcpu->arch.kvm_cpuid_x86_feature_cache = 0; + kvm_cpuid_cache_update(vcpu, X86_FEATURE_PDCM); kvm_update_kvm_cpuid_base(vcpu); kvm_vcpu_after_set_cpuid(vcpu); diff --git a/arch/x86/kvm/cpuid.h b/arch/x86/kvm/cpuid.h index 49009d16022a..65114cf7742e 100644 --- a/arch/x86/kvm/cpuid.h +++ b/arch/x86/kvm/cpuid.h @@ -86,6 +86,7 @@ static __always_inline u32 *guest_cpuid_get_register(struct kvm_vcpu *vcpu, } enum kvm_cpuid_cached_feature { + KVM_CACHED_X86_FEATURE_PDCM, NR_KVM_CACHED_X86_FEATURES, }; @@ -95,6 +96,10 @@ static __always_inline int guest_cpuid_get_cache_bit(unsigned int x86_feature) BUILD_BUG_ON(NR_KVM_CACHED_X86_FEATURES > cache_bytes * BITS_PER_BYTE); + /* Use a "dumb" if statement, this is all resolved at compile time. */ + if (x86_feature == X86_FEATURE_PDCM) + return KVM_CACHED_X86_FEATURE_PDCM; + return NR_KVM_CACHED_X86_FEATURES; } -- 2.37.1.455.g008518b4e5-goog