KVM: VMX: Snapshot LBR capabilities during module initialization
authorSean Christopherson <seanjc@google.com>
Thu, 7 Mar 2024 01:13:42 +0000 (17:13 -0800)
committerSean Christopherson <seanjc@google.com>
Thu, 11 Apr 2024 19:58:46 +0000 (12:58 -0700)
Snapshot VMX's LBR capabilities once during module initialization instead
of calling into perf every time a vCPU reconfigures its vPMU.  This will
allow massaging the LBR capabilities, e.g. if the CPU doesn't support
callstacks, without having to remember to update multiple locations.

Opportunistically tag vmx_get_perf_capabilities() with __init, as it's
only called from vmx_set_cpu_caps().

Reviewed-by: Mingwei Zhang <mizhang@google.com>
Link: https://lore.kernel.org/r/20240307011344.835640-2-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/vmx/pmu_intel.c
arch/x86/kvm/vmx/vmx.c
arch/x86/kvm/vmx/vmx.h

index 12ade343a17ed5c7aaa2efc1ebf3b3b40046d907..be40474de6e4dbd28379780c075385a79ed776d2 100644 (file)
@@ -535,7 +535,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
        perf_capabilities = vcpu_get_perf_capabilities(vcpu);
        if (cpuid_model_is_consistent(vcpu) &&
            (perf_capabilities & PMU_CAP_LBR_FMT))
-               x86_perf_get_lbr(&lbr_desc->records);
+               memcpy(&lbr_desc->records, &vmx_lbr_caps, sizeof(vmx_lbr_caps));
        else
                lbr_desc->records.nr = 0;
 
index e3315bda62372afb8e620383b552dc31237d3778..aca41d3419f9083a8cb2327092ce4fa8d76b9af5 100644 (file)
@@ -218,6 +218,8 @@ module_param(ple_window_max, uint, 0444);
 int __read_mostly pt_mode = PT_MODE_SYSTEM;
 module_param(pt_mode, int, S_IRUGO);
 
+struct x86_pmu_lbr __ro_after_init vmx_lbr_caps;
+
 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_should_flush);
 static DEFINE_STATIC_KEY_FALSE(vmx_l1d_flush_cond);
 static DEFINE_MUTEX(vmx_l1d_flush_mutex);
@@ -7862,10 +7864,9 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
        vmx_update_exception_bitmap(vcpu);
 }
 
-static u64 vmx_get_perf_capabilities(void)
+static __init u64 vmx_get_perf_capabilities(void)
 {
        u64 perf_cap = PMU_CAP_FW_WRITES;
-       struct x86_pmu_lbr lbr;
        u64 host_perf_cap = 0;
 
        if (!enable_pmu)
@@ -7875,8 +7876,8 @@ static u64 vmx_get_perf_capabilities(void)
                rdmsrl(MSR_IA32_PERF_CAPABILITIES, host_perf_cap);
 
        if (!cpu_feature_enabled(X86_FEATURE_ARCH_LBR)) {
-               x86_perf_get_lbr(&lbr);
-               if (lbr.nr)
+               x86_perf_get_lbr(&vmx_lbr_caps);
+               if (vmx_lbr_caps.nr)
                        perf_cap |= host_perf_cap & PMU_CAP_LBR_FMT;
        }
 
index 1742c88ba9c8066a7329355b830fabd5ca0a1b51..90f9e443464645507ba767d246290e13079fa397 100644 (file)
@@ -110,6 +110,8 @@ struct lbr_desc {
        bool msr_passthrough;
 };
 
+extern struct x86_pmu_lbr vmx_lbr_caps;
+
 /*
  * The nested_vmx structure is part of vcpu_vmx, and holds information we need
  * for correct emulation of VMX (i.e., nested VMX) on this vcpu.