KVM: x86: Intel LBR related perf cleanups
Subsystems (4)
Sean Christopherson
maintainerv5 cover KVM: x86: Intel LBR related perf cleanups
PeterZ, I dropped your ACK from v4 because the perf patches were completely broken.
Fix a bug where KVM incorrectly advertises PMU_CAP_LBR_FMT to userspace if perf has disabled LBRs, e.g. because probing one or more LBR MSRs during setup hit a #GP.
The non-KVM patch cleans up a KVM-specific perf API to fix a benign bug where KVM ignores the error return from the API.
The remaining patches clean up KVM's PERF_CAPABILITIES mess, which makes everything far more complex than it needs to be by
v5:
- Drop perf patches that removed stubs. The stubs are sadly necessary when CPU_SUP_INTEL=n && KVM_INTEL={m,y}, which is possible due to KVM_INTEL effectively depending on INTEL || CENTAUR || ZHAOXIN. [hint provided by kernel test robot].
- Add a patch to ignore guest CPUID on host userspace MSR writes.
- Add supported PERF_CAPABILITIES to kvm_caps to simplify code for all parties.
v4
- https://lore.kernel.org/all/[email protected]:
- Make vmx_get_perf_capabilities() non-inline to avoid references to x86_perf_get_lbr() when CPU_SUP_INTEL=n. [kernel test robot]
v3:
- https://lore.kernel.org/all/[email protected]
- Drop patches for bug #1 (already merged).
- Drop misguided "clean up the capability check" patch. [Like]
v2:
- https://lore.kernel.org/all/[email protected]
- Add patches to fix bug #2. [Like]
- Add a patch to clean up the capability check.
- Tweak the changelog for the PMU refresh bug fix to call out that KVM should disallow changing feature MSRs after KVM_RUN. [Like]
v1: https://lore.kernel.org/all/[email protected]
Sean Christopherson (8):
perf/x86/core: Zero @lbr instead of returning -1 in x86_perf_get_lbr()
stub
KVM: VMX: Advertise PMU LBRs if and only if perf supports LBRs
KVM: VMX: Fold vmx_supported_debugctl() into vcpu_supported_debugctl()
KVM: VMX: Ignore guest CPUID for host userspace writes to DEBUGCTL
KVM: x86: Track supported PERF_CAPABILITIES in kvm_caps
KVM: x86: Init vcpu->arch.perf_capabilities in common x86 code
KVM: x86: Handle PERF_CAPABILITIES in common x86's
kvm_get_msr_feature()
KVM: x86: Directly query supported PERF_CAPABILITIES for WRMSR checks
arch/x86/events/intel/lbr.c | 6 +--- arch/x86/include/asm/perf_event.h | 6 ++-- arch/x86/kvm/svm/svm.c | 3 +- arch/x86/kvm/vmx/capabilities.h | 37 ---------------------- arch/x86/kvm/vmx/pmu_intel.c | 1 - arch/x86/kvm/vmx/vmx.c | 51 +++++++++++++++++++++++-------- arch/x86/kvm/x86.c | 14 ++++----- arch/x86/kvm/x86.h | 1 + 8 files changed, 52 insertions(+), 67 deletions(-)
base-commit: e18d6152ff0f41b7f01f9817372022df04e0d354
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Sean Christopherson
maintainerPATCH v5 1/8 perf/x86/core: Zero @lbr instead of returning -1 in x86_perf_get_lbr() stub
Drop the return value from x86_perf_get_lbr() and have the stub zero out the @lbr structure instead of returning -1 to indicate "no LBR support". KVM doesn't actually check the return value, and instead subtly relies on zeroing the number of LBRs in intel_pmu_init().
Formalize "nr=0 means unsupported" so that KVM doesn't need to add a pointless check on the return value to fix KVM's benign bug.
Note, the stub is necessary even though KVM x86 selects PERF_EVENTS and the caller exists only when CONFIG_KVM_INTEL=y. Despite the name, KVM_INTEL doesn't strictly require CPU_SUP_INTEL, it can be built with any of INTEL || CENTAUR || ZHAOXIN CPUs.
--- arch/x86/events/intel/lbr.c | 6 +----- arch/x86/include/asm/perf_event.h | 6 +++--- 2 files changed, 4 insertions(+), 8 deletions(-)
arch/x86/events/intel/lbr.c +1 -5
@@ -1876,10 +1876,8 @@ void __init intel_pmu_arch_lbr_init(void)* x86_perf_get_lbr - get the LBR records information * * @lbr: the caller's memory to store the LBR records information -* -* Returns: 0 indicates the LBR info has been successfully obtained */ -int x86_perf_get_lbr (struct x86_pmu_lbr *lbr) +void x86_perf_get_lbr (struct x86_pmu_lbr *lbr) {int lbr_fmt = x86_pmu.intel_cap.lbr_format;
@@ -1887,8 +1885,6 @@ int x86_perf_get_lbr(struct x86_pmu_lbr *lbr) lbr->from = x86_pmu.lbr_from; lbr->to = x86_pmu.lbr_to; lbr->info = (lbr_fmt ==LBR_FORMAT_INFO ) ? x86_pmu.lbr_info :0 ; - -return 0 ; }EXPORT_SYMBOL_GPL (x86_perf_get_lbr);
arch/x86/include/asm/perf_event.h +3 -3
@@ -542,12 +542,12 @@ static inline void perf_check_microcode(void) { }#if defined(CONFIG_PERF_EVENTS ) && defined(CONFIG_CPU_SUP_INTEL )extern struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr,void *data); -extern int x86_perf_get_lbr (struct x86_pmu_lbr *lbr); +extern void x86_perf_get_lbr (struct x86_pmu_lbr *lbr);#else struct perf_guest_switch_msr *perf_guest_get_msrs (int *nr,void *data); -static inline int x86_perf_get_lbr (struct x86_pmu_lbr *lbr) +static inline void x86_perf_get_lbr (struct x86_pmu_lbr *lbr) { -return -1 ; +memset (lbr,0 ,sizeof (*lbr)); }#endif
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Sean Christopherson
maintainerPATCH v5 2/8 KVM: VMX: Advertise PMU LBRs if and only if perf supports LBRs
In reply to Sean ChristophersonAdvertise LBR support to userspace via MSR_IA32_PERF_CAPABILITIES if and only if perf fully supports LBRs. Perf may disable LBRs (by zeroing the number of LBRs) even on platforms the allegedly support LBRs, e.g. if probing any LBR MSRs during setup fails.
--- arch/x86/kvm/vmx/capabilities.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/capabilities.h +4 -1
@@ -400,6 +400,7 @@ static inline bool vmx_pebs_supported(void)static inline 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)
@@ -408,7 +409,9 @@ static inline u64 vmx_get_perf_capabilities(void)if (boot_cpu_has (X86_FEATURE_PDCM ))rdmsrl (MSR_IA32_PERF_CAPABILITIES , host_perf_cap); - perf_cap |= host_perf_cap &PMU_CAP_LBR_FMT ; +x86_perf_get_lbr (&lbr); +if (lbr.nr) + perf_cap |= host_perf_cap &PMU_CAP_LBR_FMT ;if (vmx_pebs_supported ()) { perf_cap |= host_perf_cap &PERF_CAP_PEBS_MASK ;
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Sean Christopherson
maintainerPATCH v5 3/8 KVM: VMX: Fold vmx_supported_debugctl() into vcpu_supported_debugctl()
In reply to Sean ChristophersonFold vmx_supported_debugctl() into vcpu_supported_debugctl(), its only caller. Setting bits only to clear them a few instructions later is rather silly, and splitting the logic makes things seem more complicated than they actually are.
Opportunistically drop DEBUGCTLMSR_LBR_MASK now that there's a single reference to the pair of bits. The extra layer of indirection provides no meaningful value and makes it unnecessarily tedious to understand what KVM is doing.
No functional change.
--- arch/x86/kvm/vmx/capabilities.h | 15 --------------- arch/x86/kvm/vmx/vmx.c | 12 +++++++----- 2 files changed, 7 insertions(+), 20 deletions(-)
arch/x86/kvm/vmx/capabilities.h +0 -15
@@ -24,8 +24,6 @@ extern int __read_mostly pt_mode;#define PMU_CAP_FW_WRITES (1ULL << 13)#define PMU_CAP_LBR_FMT 0x3f -#define DEBUGCTLMSR_LBR_MASK (DEBUGCTLMSR_LBR | DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) -struct nested_vmx_msrs {/* * We only store the "true" versions of the VMX capability MSRs. We
@@ -422,19 +420,6 @@ static inline u64 vmx_get_perf_capabilities(void)return perf_cap; } -static inline u64 vmx_supported_debugctl (void ) -{ -u64 debugctl =0 ; - -if (boot_cpu_has (X86_FEATURE_BUS_LOCK_DETECT )) - debugctl |=DEBUGCTLMSR_BUS_LOCK_DETECT ; - -if (vmx_get_perf_capabilities () &PMU_CAP_LBR_FMT ) - debugctl |=DEBUGCTLMSR_LBR_MASK ; - -return debugctl; -} -static inline bool cpu_has_notify_vmexit (void ) { return vmcs_config.cpu_based_2nd_exec_ctrl &
arch/x86/kvm/vmx/vmx.c +7 -5
@@ -2023,13 +2023,15 @@ static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu,static u64 vcpu_supported_debugctl (struct kvm_vcpu *vcpu) { -u64 debugctl =vmx_supported_debugctl (); +u64 debugctl =0 ; -if (!intel_pmu_lbr_is_enabled (vcpu)) - debugctl &= ~DEBUGCTLMSR_LBR_MASK ; +if (boot_cpu_has (X86_FEATURE_BUS_LOCK_DETECT ) && +guest_cpuid_has (vcpu,X86_FEATURE_BUS_LOCK_DETECT )) + debugctl |=DEBUGCTLMSR_BUS_LOCK_DETECT ; -if (!guest_cpuid_has (vcpu,X86_FEATURE_BUS_LOCK_DETECT )) - debugctl &= ~DEBUGCTLMSR_BUS_LOCK_DETECT ; +if ((vmx_get_perf_capabilities () &PMU_CAP_LBR_FMT ) && +intel_pmu_lbr_is_enabled (vcpu)) + debugctl |=DEBUGCTLMSR_LBR |DEBUGCTLMSR_FREEZE_LBRS_ON_PMI ;return debugctl; }
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Sean Christopherson
maintainerPATCH v5 4/8 KVM: VMX: Ignore guest CPUID for host userspace writes to DEBUGCTL
In reply to Sean ChristophersonIgnore guest CPUID for host userspace writes to the DEBUGCTL MSR, KVM's ABI is that setting CPUID vs. state can be done in any order, i.e. KVM allows userspace to stuff MSRs prior to setting the guest's CPUID that makes the new MSR "legal".
Keep the vmx_get_perf_capabilities() check for guest writes, even though it's technically unnecessary since the vCPU's PERF_CAPABILITIES is consulted when refreshing LBR support. A future patch will clean up vmx_get_perf_capabilities() to avoid the RDMSR on every call, at which point the paranoia will incur no meaningful overhead.
Note, prior to vmx_get_perf_capabilities() checking that the host fully supports LBRs via x86_perf_get_lbr(), KVM effectively relied on intel_pmu_lbr_is_enabled() to guard against host userspace enabling LBRs on platforms without full support.
--- arch/x86/kvm/vmx/vmx.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/vmx.c +6 -4
@@ -2021,16 +2021,16 @@ static u64 nested_vmx_truncate_sysenter_addr(struct kvm_vcpu *vcpu,return (unsigned long )data; } -static u64 vcpu_supported_debugctl (struct kvm_vcpu *vcpu) +static u64 vmx_get_supported_debugctl (struct kvm_vcpu *vcpu,bool host_initiated) {u64 debugctl =0 ;if (boot_cpu_has (X86_FEATURE_BUS_LOCK_DETECT ) && -guest_cpuid_has (vcpu,X86_FEATURE_BUS_LOCK_DETECT )) + (host_initiated ||guest_cpuid_has (vcpu,X86_FEATURE_BUS_LOCK_DETECT ))) debugctl |=DEBUGCTLMSR_BUS_LOCK_DETECT ;if ((vmx_get_perf_capabilities () &PMU_CAP_LBR_FMT ) && -intel_pmu_lbr_is_enabled (vcpu)) + (host_initiated ||intel_pmu_lbr_is_enabled (vcpu))) debugctl |=DEBUGCTLMSR_LBR |DEBUGCTLMSR_FREEZE_LBRS_ON_PMI ;return debugctl;
@@ -2105,7 +2105,9 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)vmcs_writel (GUEST_SYSENTER_ESP , data);break ;case MSR_IA32_DEBUGCTLMSR : { -u64 invalid = data & ~vcpu_supported_debugctl (vcpu); +u64 invalid; + + invalid = data & ~vmx_get_supported_debugctl (vcpu, msr_info->host_initiated);if (invalid & (DEBUGCTLMSR_BTF |DEBUGCTLMSR_LBR )) {if (report_ignored_msrs) vcpu_unimpl(vcpu, "%s: BTF|LBR in IA32_DEBUGCTLMSR 0x%llx, nop\n",
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Sean Christopherson
maintainerPATCH v5 5/8 KVM: x86: Track supported PERF_CAPABILITIES in kvm_caps
In reply to Sean ChristophersonTrack KVM's supported PERF_CAPABILITIES in kvm_caps instead of computing the supported capabilities on the fly every time. Using kvm_caps will also allow for future cleanups as the kvm_caps values can be used directly in common x86 code.
--- arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/vmx/capabilities.h | 25 ------------------------ arch/x86/kvm/vmx/pmu_intel.c | 2 +- arch/x86/kvm/vmx/vmx.c | 34 +++++++++++++++++++++++++++++---- arch/x86/kvm/x86.h | 1 + 5 files changed, 34 insertions(+), 30 deletions(-)
arch/x86/kvm/svm/svm.c +2 -0
@@ -2715,6 +2715,7 @@ static int svm_get_msr_feature(struct kvm_msr_entry *msr) msr->data |=MSR_F10H_DECFG_LFENCE_SERIALIZE ;break ;case MSR_IA32_PERF_CAPABILITIES : + msr->data = kvm_caps.supported_perf_cap;return 0 ;default :return KVM_MSR_RET_INVALID ;
@@ -4898,6 +4899,7 @@ static __init void svm_set_cpu_caps(void) {kvm_set_cpu_caps (); + kvm_caps.supported_perf_cap =0 ; kvm_caps.supported_xss =0 ;/* CPUID 0x80000001 and 0x8000000A (SVM features) */
arch/x86/kvm/vmx/capabilities.h +0 -25
@@ -395,31 +395,6 @@ static inline bool vmx_pebs_supported(void)return boot_cpu_has (X86_FEATURE_PEBS ) && kvm_pmu_cap.pebs_ept; } -static inline 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) -return 0 ; - -if (boot_cpu_has (X86_FEATURE_PDCM )) -rdmsrl (MSR_IA32_PERF_CAPABILITIES , host_perf_cap); - -x86_perf_get_lbr (&lbr); -if (lbr.nr) - perf_cap |= host_perf_cap &PMU_CAP_LBR_FMT ; - -if (vmx_pebs_supported ()) { - perf_cap |= host_perf_cap &PERF_CAP_PEBS_MASK ; -if ((perf_cap &PERF_CAP_PEBS_FORMAT ) <4 ) - perf_cap &= ~PERF_CAP_PEBS_BASELINE ; - } - -return perf_cap; -} -static inline bool cpu_has_notify_vmexit (void ) { return vmcs_config.cpu_based_2nd_exec_ctrl &
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -631,7 +631,7 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu) pmu->fixed_counters[i].current_config =0 ; } - vcpu->arch.perf_capabilities =vmx_get_perf_capabilities (); + vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; lbr_desc->records.nr =0 ; lbr_desc->event =NULL ; lbr_desc->msr_passthrough = false;
arch/x86/kvm/vmx/vmx.c +30 -4
@@ -1850,7 +1850,7 @@ static int vmx_get_msr_feature(struct kvm_msr_entry *msr)return 1 ;return vmx_get_vmx_msr (&vmcs_config.nested, msr->index, &msr->data);case MSR_IA32_PERF_CAPABILITIES : - msr->data =vmx_get_perf_capabilities (); + msr->data = kvm_caps.supported_perf_cap;return 0 ;default :return KVM_MSR_RET_INVALID ;
@@ -2029,7 +2029,7 @@ static u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated (host_initiated || guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT))) debugctl |=DEBUGCTLMSR_BUS_LOCK_DETECT ; -if ((vmx_get_perf_capabilities () &PMU_CAP_LBR_FMT ) && +if ((kvm_caps.supported_perf_cap &PMU_CAP_LBR_FMT ) && (host_initiated ||intel_pmu_lbr_is_enabled (vcpu))) debugctl |=DEBUGCTLMSR_LBR |DEBUGCTLMSR_FREEZE_LBRS_ON_PMI ;
@@ -2342,14 +2342,14 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 1 ;if (data &PMU_CAP_LBR_FMT ) {if ((data &PMU_CAP_LBR_FMT ) != - (vmx_get_perf_capabilities () &PMU_CAP_LBR_FMT )) + (kvm_caps.supported_perf_cap &PMU_CAP_LBR_FMT ))return 1 ;if (!cpuid_model_is_consistent (vcpu))return 1 ; }if (data &PERF_CAP_PEBS_FORMAT ) {if ((data &PERF_CAP_PEBS_MASK ) != - (vmx_get_perf_capabilities () &PERF_CAP_PEBS_MASK )) + (kvm_caps.supported_perf_cap &PERF_CAP_PEBS_MASK ))return 1 ;if (!guest_cpuid_has (vcpu,X86_FEATURE_DS ))return 1 ;
@@ -7669,6 +7669,31 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)vmx_update_exception_bitmap (vcpu); } +static 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) +return 0 ; + +if (boot_cpu_has (X86_FEATURE_PDCM )) +rdmsrl (MSR_IA32_PERF_CAPABILITIES , host_perf_cap); + +x86_perf_get_lbr (&lbr); +if (lbr.nr) + perf_cap |= host_perf_cap &PMU_CAP_LBR_FMT ; + +if (vmx_pebs_supported ()) { + perf_cap |= host_perf_cap &PERF_CAP_PEBS_MASK ; +if ((perf_cap &PERF_CAP_PEBS_FORMAT ) <4 ) + perf_cap &= ~PERF_CAP_PEBS_BASELINE ; + } + +return perf_cap; +} +static __init void vmx_set_cpu_caps (void ) {kvm_set_cpu_caps ();
@@ -7691,6 +7716,7 @@ static __init void vmx_set_cpu_caps(void)if (!enable_pmu)kvm_cpu_cap_clear (X86_FEATURE_PDCM ); + kvm_caps.supported_perf_cap =vmx_get_perf_capabilities ();if (!enable_sgx) {kvm_cpu_cap_clear (X86_FEATURE_SGX );
arch/x86/kvm/x86.h +1 -0
@@ -27,6 +27,7 @@ struct kvm_caps {u64 supported_mce_cap;u64 supported_xcr0;u64 supported_xss; +u64 supported_perf_cap; };void kvm_spurious_fault (void );
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Re: [PATCH v5 5/8] KVM: x86: Track supported PERF_CAPABILITIES in kvm_caps
Quoted 6 lines · Sean Christopherson
Track KVM's supported
PERF_CAPABILITIESinkvm_capsinstead of computing the supported capabilities on the fly every time. Usingkvm_capswill also allow for future cleanups as thekvm_capsvalues can be used directly in common x86 code.Signed-off-by Sean Christopherson <[email protected]>
A similar change was made to one of my git branches. Please move forward.
Quoted 168 lines
--- arch/x86/kvm/svm/svm.c | 2 ++ arch/x86/kvm/vmx/capabilities.h | 25 ------------------------ arch/x86/kvm/vmx/pmu_intel.c | 2 +- arch/x86/kvm/vmx/vmx.c | 34 +++++++++++++++++++++++++++++---- arch/x86/kvm/x86.h | 1 + 5 files changed, 34 insertions(+), 30 deletions(-)arch/x86/kvm/svm/svm.c +2
-0@@ -2715,6 +2715,7 @@ static int svm_get_msr_feature(struct kvm_msr_entry *msr) msr->data |=MSR_F10H_DECFG_LFENCE_SERIALIZE ;break ;case MSR_IA32_PERF_CAPABILITIES : + msr->data = kvm_caps.supported_perf_cap;return 0 ;default :return KVM_MSR_RET_INVALID ;@@ -4898,6 +4899,7 @@ static __init void svm_set_cpu_caps(void) {kvm_set_cpu_caps (); + kvm_caps.supported_perf_cap =0 ; kvm_caps.supported_xss =0 ;/* CPUID 0x80000001 and 0x8000000A (SVM features) */ arch/x86/kvm/vmx/capabilities.h +0
-25@@ -395,31 +395,6 @@ static inline bool vmx_pebs_supported(void)return boot_cpu_has (X86_FEATURE_PEBS ) && kvm_pmu_cap.pebs_ept; } -static inline 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) -return 0 ; - -if (boot_cpu_has (X86_FEATURE_PDCM )) -rdmsrl (MSR_IA32_PERF_CAPABILITIES , host_perf_cap); - -x86_perf_get_lbr (&lbr); -if (lbr.nr) - perf_cap |= host_perf_cap &PMU_CAP_LBR_FMT ; - -if (vmx_pebs_supported ()) { - perf_cap |= host_perf_cap &PERF_CAP_PEBS_MASK ; -if ((perf_cap &PERF_CAP_PEBS_FORMAT ) <4 ) - perf_cap &= ~PERF_CAP_PEBS_BASELINE ; - } - -return perf_cap; -} -static inline bool cpu_has_notify_vmexit (void ) { return vmcs_config.cpu_based_2nd_exec_ctrl &arch/x86/kvm/vmx/pmu_intel.c +1
-1@@ -631,7 +631,7 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu) pmu->fixed_counters[i].current_config =0 ; } - vcpu->arch.perf_capabilities =vmx_get_perf_capabilities (); + vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; lbr_desc->records.nr =0 ; lbr_desc->event =NULL ; lbr_desc->msr_passthrough = false;arch/x86/kvm/vmx/vmx.c +30
-4@@ -1850,7 +1850,7 @@ static int vmx_get_msr_feature(struct kvm_msr_entry *msr)return 1 ;return vmx_get_vmx_msr (&vmcs_config.nested, msr->index, &msr->data);case MSR_IA32_PERF_CAPABILITIES : - msr->data =vmx_get_perf_capabilities (); + msr->data = kvm_caps.supported_perf_cap;return 0 ;default :return KVM_MSR_RET_INVALID ;@@ -2029,7 +2029,7 @@ static u64 vmx_get_supported_debugctl(struct kvm_vcpu *vcpu, bool host_initiated (host_initiated || guest_cpuid_has(vcpu, X86_FEATURE_BUS_LOCK_DETECT))) debugctl |=DEBUGCTLMSR_BUS_LOCK_DETECT ; -if ((vmx_get_perf_capabilities () &PMU_CAP_LBR_FMT ) && +if ((kvm_caps.supported_perf_cap &PMU_CAP_LBR_FMT ) && (host_initiated ||intel_pmu_lbr_is_enabled (vcpu))) debugctl |=DEBUGCTLMSR_LBR |DEBUGCTLMSR_FREEZE_LBRS_ON_PMI ;@@ -2342,14 +2342,14 @@ static int vmx_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 1 ;if (data &PMU_CAP_LBR_FMT ) {if ((data &PMU_CAP_LBR_FMT ) != - (vmx_get_perf_capabilities () &PMU_CAP_LBR_FMT )) + (kvm_caps.supported_perf_cap &PMU_CAP_LBR_FMT ))return 1 ;if (!cpuid_model_is_consistent (vcpu))return 1 ; }if (data &PERF_CAP_PEBS_FORMAT ) {if ((data &PERF_CAP_PEBS_MASK ) != - (vmx_get_perf_capabilities () &PERF_CAP_PEBS_MASK )) + (kvm_caps.supported_perf_cap &PERF_CAP_PEBS_MASK ))return 1 ;if (!guest_cpuid_has (vcpu,X86_FEATURE_DS ))return 1 ;@@ -7669,6 +7669,31 @@ static void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)vmx_update_exception_bitmap (vcpu); } +static 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) +return 0 ; + +if (boot_cpu_has (X86_FEATURE_PDCM )) +rdmsrl (MSR_IA32_PERF_CAPABILITIES , host_perf_cap); + +x86_perf_get_lbr (&lbr); +if (lbr.nr) + perf_cap |= host_perf_cap &PMU_CAP_LBR_FMT ; + +if (vmx_pebs_supported ()) { + perf_cap |= host_perf_cap &PERF_CAP_PEBS_MASK ; +if ((perf_cap &PERF_CAP_PEBS_FORMAT ) <4 ) + perf_cap &= ~PERF_CAP_PEBS_BASELINE ; + } + +return perf_cap; +} +static __init void vmx_set_cpu_caps (void ) {kvm_set_cpu_caps ();@@ -7691,6 +7716,7 @@ static __init void vmx_set_cpu_caps(void)if (!enable_pmu)kvm_cpu_cap_clear (X86_FEATURE_PDCM ); + kvm_caps.supported_perf_cap =vmx_get_perf_capabilities ();if (!enable_sgx) {kvm_cpu_cap_clear (X86_FEATURE_SGX );arch/x86/kvm/x86.h +1
-0@@ -27,6 +27,7 @@ struct kvm_caps {u64 supported_mce_cap;u64 supported_xcr0;u64 supported_xss; +u64 supported_perf_cap; };void kvm_spurious_fault (void );
Sean Christopherson
maintainerPATCH v5 6/8 KVM: x86: Init vcpu->arch.perf_capabilities in common x86 code
In reply to Sean ChristophersonInitialize vcpu->arch.perf_capabilities in x86's kvm_arch_vcpu_create() instead of deferring initialization to vendor code. For better or worse, common x86 handles reads and writes to the MSR, and so common x86 should also handle initializing the MSR.
No functional change intended.
--- arch/x86/kvm/vmx/pmu_intel.c | 1 - arch/x86/kvm/x86.c | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/pmu_intel.c +0 -1
@@ -631,7 +631,6 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu) pmu->fixed_counters[i].current_config =0 ; } - vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap; lbr_desc->records.nr =0 ; lbr_desc->event =NULL ; lbr_desc->msr_passthrough = false;
arch/x86/kvm/x86.c +2 -0
@@ -11821,6 +11821,8 @@ int kvm_arch_vcpu_create(struct kvm_vcpu *vcpu) vcpu->arch.pat =MSR_IA32_CR_PAT_DEFAULT ;kvm_async_pf_hash_reset (vcpu); + + vcpu->arch.perf_capabilities = kvm_caps.supported_perf_cap;kvm_pmu_init (vcpu); vcpu->arch.pending_external_vector =-1 ;
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Sean Christopherson
maintainerPATCH v5 7/8 KVM: x86: Handle PERF_CAPABILITIES in common x86's kvm_get_msr_feature()
In reply to Sean ChristophersonHandle PERF_CAPABILITIES directly in kvm_get_msr_feature() now that the supported value is available in kvm_caps.
No functional change intended.
--- arch/x86/kvm/svm/svm.c | 3 --- arch/x86/kvm/vmx/vmx.c | 3 --- arch/x86/kvm/x86.c | 3 +++ 3 files changed, 3 insertions(+), 6 deletions(-)
arch/x86/kvm/svm/svm.c +0 -3
@@ -2714,9 +2714,6 @@ static int svm_get_msr_feature(struct kvm_msr_entry *msr)if (boot_cpu_has (X86_FEATURE_LFENCE_RDTSC )) msr->data |=MSR_F10H_DECFG_LFENCE_SERIALIZE ;break ; -case MSR_IA32_PERF_CAPABILITIES : - msr->data = kvm_caps.supported_perf_cap; -return 0 ;default :return KVM_MSR_RET_INVALID ; }
arch/x86/kvm/vmx/vmx.c +0 -3
@@ -1849,9 +1849,6 @@ static int vmx_get_msr_feature(struct kvm_msr_entry *msr)if (!nested)return 1 ;return vmx_get_vmx_msr (&vmcs_config.nested, msr->index, &msr->data); -case MSR_IA32_PERF_CAPABILITIES : - msr->data = kvm_caps.supported_perf_cap; -return 0 ;default :return KVM_MSR_RET_INVALID ; }
arch/x86/kvm/x86.c +3 -0
@@ -1653,6 +1653,9 @@ static int kvm_get_msr_feature(struct kvm_msr_entry *msr)case MSR_IA32_ARCH_CAPABILITIES : msr->data =kvm_get_arch_capabilities ();break ; +case MSR_IA32_PERF_CAPABILITIES : + msr->data = kvm_caps.supported_perf_cap; +break ;case MSR_IA32_UCODE_REV :rdmsrl_safe (msr->index, &msr->data);break ;
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Sean Christopherson
maintainerPATCH v5 8/8 KVM: x86: Directly query supported PERF_CAPABILITIES for WRMSR checks
In reply to Sean ChristophersonUse kvm_caps.supported_perf_cap directly instead of bouncing through kvm_get_msr_feature() when checking the incoming value for writes to PERF_CAPABILITIES.
Note, kvm_get_msr_feature() is guaranteed to succeed when getting PERF_CAPABILITIES, i.e. dropping that check is a nop.
No functional change intended.
--- arch/x86/kvm/x86.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-)
arch/x86/kvm/x86.c +2 -7
@@ -3568,20 +3568,15 @@ int kvm_set_msr_common(struct kvm_vcpu *vcpu, struct msr_data *msr_info)return 1 ; vcpu->arch.arch_capabilities = data;break ; -case MSR_IA32_PERF_CAPABILITIES : { -struct kvm_msr_entry msr_ent = {.index = msr, .data =0 }; - +case MSR_IA32_PERF_CAPABILITIES :if (!msr_info->host_initiated)return 1 ; -if (kvm_get_msr_feature (&msr_ent)) -return 1 ; -if (data & ~msr_ent.data) +if (data & ~kvm_caps.supported_perf_cap)return 1 ; vcpu->arch.perf_capabilities = data;kvm_pmu_refresh (vcpu);return 0 ; - }case MSR_EFER :return set_efer (vcpu, msr_info);case MSR_K7_HWCR :
Signature · 1 line
2.38.0.rc1.362.ged0d419d3c-goog
Quoted 64 lines · Sean Christopherson
PeterZ, I dropped your ACK from v4 because the perf patches were completely broken.
Fix a bug where KVM incorrectly advertises
PMU_CAP_LBR_FMTto userspace if perf has disabled LBRs, e.g. because probing one or more LBR MSRs during setup hit a #GP.The non-KVM patch cleans up a KVM-specific perf API to fix a benign bug where KVM ignores the error return from the API.
The remaining patches clean up KVM's
PERF_CAPABILITIESmess, which makes everything far more complex than it needs to be byv5:
- Drop perf patches that removed stubs. The stubs are sadly necessarywhen CPU_SUP_INTEL=n && KVM_INTEL={m,y}, which is possible due to KVM_INTEL effectively depending on INTEL || CENTAUR || ZHAOXIN. [hint provided by kernel test robot].- Add a patch to ignore guest CPUID on host userspace MSR writes.
- Add supportedPERF_CAPABILITIEStokvm_capsto simplify code for all parties.v4
- https://lore.kernel.org/all/[email protected]:
- Makevmx_get_perf_capabilities()non-inline to avoid references tox86_perf_get_lbr()when CPU_SUP_INTEL=n. [kernel test robot]v3:
- https://lore.kernel.org/all/[email protected]
- Drop patches for bug #1 (already merged).
- Drop misguided "clean up the capability check" patch. [Like]v2:
- https://lore.kernel.org/all/[email protected]
- Add patches to fix bug #2. [Like]
- Add a patch to clean up the capability check.
- Tweak the changelog for the PMU refresh bug fix to call out that KVM should disallow changing feature MSRs afterKVM_RUN. [Like]v1: https://lore.kernel.org/all/[email protected]
Sean Christopherson (8):
perf/x86/core: Zero @lbr instead of returning -1 inx86_perf_get_lbr()
stub
KVM: VMX: Advertise PMU LBRs if and only if perf supports LBRs
KVM: VMX: Foldvmx_supported_debugctl()intovcpu_supported_debugctl()
KVM: VMX: Ignore guest CPUID for host userspace writes to DEBUGCTL
KVM: x86: Track supportedPERF_CAPABILITIESinkvm_caps
KVM: x86: Initvcpu->arch.perf_capabilitiesin common x86 code
KVM: x86: HandlePERF_CAPABILITIESin common x86's
kvm_get_msr_feature()
KVM: x86: Directly query supportedPERF_CAPABILITIESfor WRMSR checksarch/x86/events/intel/lbr.c | 6 +--- arch/x86/include/asm/perf_event.h | 6 ++-- arch/x86/kvm/svm/svm.c | 3 +- arch/x86/kvm/vmx/capabilities.h | 37 ---------------------- arch/x86/kvm/vmx/pmu_intel.c | 1 - arch/x86/kvm/vmx/vmx.c | 51 +++++++++++++++++++++++-------- arch/x86/kvm/x86.c | 14 ++++----- arch/x86/kvm/x86.h | 1 + 8 files changed, 52 insertions(+), 67 deletions(-)
Quoted 1 line
base-commit:
e18d6152ff0f41b7f01f9817372022df04e0d354
Queued, with patches 2-4 destined to kvm/master.
Paolo