Add PERF_CAP_RDPMC_METRICS_CLEAR (IA32_PERF_CAPABILITIES bit 19) support. When set, and IA32_FIXED_CTR_CTRL.METRICS_CLEAR_EN (bit 14) is also set, RDPMC of PERF_METRICS clears both PERF_METRICS and fixed counter 3 (SLOTS) after the read. Advertise the capability to guests only when the mediated vPMU is enabled and the host reports it, and allow the guest to set METRICS_CLEAR_EN for fixed counter 3 when PERF_METRICS, RDPMC_METRICS_CLEAR, and fixed counter 3 are all exposed to the guest. For the mediated vPMU, METRICS_CLEAR_EN takes effect natively via passthrough. However, RDPMC can still be intercepted for unrelated reasons, so also emulate the clear-on-read behavior for consistency. Signed-off-by: Zide Chen --- v10: - Gate METRICS_CLEAR_EN unmasking on both perf_metrics and FC3. - Check for valid fixed counter 3 PMC before clearing it in intel_emulate_rdpmc(). - Drop check in vmx_set_msr() requiring PERF_CAP_PERF_METRICS. (Jim) - Split event filter mask fix into a separate patch ("KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters"). v9: new patch. --- arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/vmx/pmu_intel.c | 26 ++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 3 ++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h index 8fad944197fb..5851edcdda05 100644 --- a/arch/x86/include/asm/msr-index.h +++ b/arch/x86/include/asm/msr-index.h @@ -333,6 +333,7 @@ #define PERF_CAP_PEBS_BASELINE BIT_ULL(14) #define PERF_CAP_PERF_METRICS BIT_ULL(15) #define PERF_CAP_PEBS_TIMING_INFO BIT_ULL(17) +#define PERF_CAP_RDPMC_METRICS_CLEAR BIT_ULL(19) #define PERF_CAP_PEBS_MASK (PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \ PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \ PERF_CAP_PEBS_TIMING_INFO) diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c index da6dae261514..0861c65bee95 100644 --- a/arch/x86/kvm/vmx/pmu_intel.c +++ b/arch/x86/kvm/vmx/pmu_intel.c @@ -86,6 +86,12 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) } } +static bool intel_pmu_metrics_clear_enabled(struct kvm_pmu *pmu) +{ + return fixed_ctrl_field(pmu->fixed_ctr_ctrl, KVM_FIXED_SLOTS_IDX) & + INTEL_FIXED_3_METRICS_CLEAR; +} + static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx, u64 *data) { @@ -140,6 +146,20 @@ static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx, return 1; *data = pmu->perf_metrics; + + /* + * Per the SDM, RDPMC of PERF_METRICS clears both PERF_METRICS + * and the SLOTS fixed counter when Metrics Clear Mode is + * enabled via INTEL_FIXED_3_METRICS_CLEAR in FIXED_CTR_CTRL. + */ + if (intel_pmu_metrics_clear_enabled(pmu)) { + pmc = kvm_pmc_idx_to_pmc(pmu, INTEL_PMC_IDX_FIXED_SLOTS); + + pmu->perf_metrics = 0; + if (pmc) + pmc_write_counter(pmc, 0); + } + return 0; default: return 1; @@ -552,6 +572,12 @@ static void intel_pmu_refresh_perf_metrics(struct kvm_vcpu *vcpu) ~GENMASK_ULL(kvm_pmu_cap.num_topdown_events * INTEL_TD_METRIC_FIELD_BITS - 1, 0); } + + if (kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_RDPMC_METRICS_CLEAR) { + pmu->fixed_ctr_ctrl_rsvd &= + ~intel_fixed_bits_by_idx(KVM_FIXED_SLOTS_IDX, + INTEL_FIXED_3_METRICS_CLEAR); + } } static void intel_pmu_refresh(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c index 7441808be049..d977fc3a31d8 100644 --- a/arch/x86/kvm/vmx/vmx.c +++ b/arch/x86/kvm/vmx/vmx.c @@ -7944,7 +7944,8 @@ static __init u64 vmx_get_perf_capabilities(void) */ if (enable_mediated_pmu && kvm_pmu_cap.num_topdown_events && kvm_pmu_cap.num_counters_fixed > KVM_FIXED_SLOTS_IDX) - perf_cap |= kvm_pmu_cap.perf_capabilities & PERF_CAP_PERF_METRICS; + perf_cap |= kvm_pmu_cap.perf_capabilities & + (PERF_CAP_PERF_METRICS | PERF_CAP_RDPMC_METRICS_CLEAR); return perf_cap; } -- 2.56.1