KVM: x86/pmu: Add hardware Topdown metrics support
Subsystems (5)
v10 cover KVM: x86/pmu: Add hardware Topdown metrics support
Activation of the PERF_METRICS feature in hardware requires both PERF_METRICS_AVAILABLE and the existence of fixed counter 3. However, the presence of the IA32_PERF_METRICS MSR is architecturally decoupled from it.
This version enforces the following dependencies between PERF_METRICS and fixed counter 3 to comply with the SDM:
- Advertise PERF_METRICS and RDPMC_METRICS_CLEAR only when mediated vPMU is enabled and host perf reports num_topdown_events != 0.
Gate on host Topdown events in addition to host fixed counter 3 so that this feature is advertised on known CPU models only.
- Gate IA32_PERF_METRICS and RDPMC existence solely on guest PERF_CAP_PERF_METRICS (per SDM Vol 4 Table 2-2). Without guest fixed counter 3, neither RDMSR nor RDPMC faults (#GP): all metric fields are reserved (~0ull), reads (RDMSR and RDPMC) succeed and return 0, writing 0 succeeds, and only non-zero writes #GP.
With fixed counter 3, valid metric fields are unmasked based on host-provided num_topdown_events.
- Gate IA32_PERF_METRICS, GLOBAL_CTRL, and RDPMC passthrough (and hardware context switching) on having both guest PERF_METRICS and fixed counter 3. If either is missing, metrics cannot count in hardware; accesses remain intercepted and handled cleanly in KVM software (e.g., emulated RDPMC succeeds and returns 0).
- Allow setting GLOBAL_CTRL[48] whenever PERF_METRICS is present on the guest, because the SDM defines bit 48 as an architectural control bit (not reserved) when PERF_METRICS is supported, even if fixed counter 3 is absent.
- Require guest fixed counter 3 to unmask FIXED_CTR_CTRL[14] (METRICS_CLEAR_EN), as bit 14 resides in fixed counter 3's control nibble. Without fixed counter 3, bit 14 remains reserved; when enabled with fixed counter 3, RDPMC of metrics clears both PERF_METRICS and fixed counter 3 in hardware and emulation.
Notes:
A. If the mediated vPMU PMU v5 series [1] lands first:
- Patch 9 ("Mask only OS/USR bits when filtering fixed counters") is already included in the PMU v5 series and should be dropped from here.
- In patch 11, the contiguous fixed counter clamp in kvm_init_pmu_capability() can be removed, as PMU v5 supports bitmasks.
- The fixed counter 3 checks across the series will be updated to use the new PMU v5 helper:
kvm_is_fixed_pmc_supported(pmu, KVM_FIXED_SLOTS_IDX)
B. Patch 3 only fixes perf_capabilities in the base PMU. Broader base PMU setup on hybrid hardware running with a single core type (e.g., P/E-core only SKUs or firmware configs) is orthogonal and left for separate patches.
Patch series summary:
patch 1-5: Prerequisites in host perf
patch 6-9: KVM preparations
patch 10-15: Intel Topdown feature virtualization
patch 16-18: Topdown PERF_METRICS selftests.
Tests: Tested Topdown metrics on ICX (4 metrics), and SPR (8 metrics). Tested RDPMC Metrics Clear on DMR. Tested nested.
Rebased on kvm-x86/next: commit 6bd2905303c5 ("Merge branch 'vmx'")
v10:
- Implement the above dependency policies to comply with the SDM.
- Re-order, consolidation, and new patches:
* patch 2: do not inflate capabilities under hypervisors.
* patch 3: refine broken perf capabilities handling in host perf.
* patch 4: export perf_capabilities from host perf to KVM.
* patch 7: use host-reported perf_capabilities in VMX instead of rdmsrq().
* patch 9: move fixed counter filter mask fix into preparation group. (Dapeng)
* patch 10: intercept unexposed PERF_METRICS before fixed counter 3 (patch 11) to avoid premature hardware passthrough.
* Re-order RDPMC emulation (patches 12-13) before PERF_METRICS MSR support (patch 14) to eliminate a bisection gap before advertising the capability.
* Fold v9 patch 11 (reserved bit validation) into patch 14. (Jim)
- Selftests:
* Moved Topdown metrics tests to a separate test file to avoid bloating pmu_counters_test.c.
* Added coverage for RDPMC Metrics Clear Mode, host MSR access, and capability permutations.
v9:
- Reject writes to reserved MSR_PERF_METRICS bits (bits [63:32]).
- Support RDPMC Metrics Clear Mode.
- perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants.
- perf/x86: Expose number of Topdown metric events to KVM.
- Remove the incorrect comment about writing to PERF_METRICS.
- Remove references to Metrics Clear from the changelog of patch 7.
v9: https://lore.kernel.org/kvm/[email protected]/
v8: https://lore.kernel.org/kvm/[email protected]/
QEMU: https://lore.kernel.org/qemu-devel/[email protected]/
[1] https://lore.kernel.org/kvm/[email protected]/
Dapeng Mi (2):
KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU
KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU
Zide Chen (16):
perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants perf/x86/intel: Do not inflate capabilities for guests perf/x86/intel: Track common hybrid capabilities perf/x86: Expose perf_capabilities to KVM
perf/x86: Expose number of Topdown metric events to KVM
KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events
KVM: x86/pmu: Query perf-reported capabilities instead of RDMSR
KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h
KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters
KVM: x86/pmu: Block PMU passthrough if guest lacks PERF_METRICS
KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks
KVM: x86/pmu: Emulate RDPMC on performance metrics
KVM: x86/pmu: Support RDPMC Metrics Clear Mode
KVM: selftests: Rework PMU guest measurement asm operands
KVM: selftests: Move and generalize PMU guest measurement helpers
KVM: selftests: Add Topdown PERF_METRICS test
arch/x86/events/core.c | 3 + arch/x86/events/intel/core.c | 39 +- arch/x86/include/asm/kvm-x86-pmu-ops.h | 2 +- arch/x86/include/asm/kvm_host.h | 4 +- arch/x86/include/asm/msr-index.h | 2 + arch/x86/include/asm/perf_event.h | 6 + arch/x86/kvm/msrs.c | 10 +- arch/x86/kvm/pmu.c | 60 ++- arch/x86/kvm/pmu.h | 33 +- arch/x86/kvm/svm/pmu.c | 14 +- arch/x86/kvm/vmx/nested.c | 2 + arch/x86/kvm/vmx/pmu_intel.c | 159 +++++-- arch/x86/kvm/vmx/pmu_intel.h | 12 +- arch/x86/kvm/vmx/vmx.c | 21 +- tools/arch/x86/include/asm/msr-index.h | 2 + tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/include/x86/pmu.h | 60 +++ .../selftests/kvm/x86/pmu_counters_test.c | 52 +-- .../selftests/kvm/x86/pmu_perf_metrics_test.c | 391 ++++++++++++++++++ 19 files changed, 741 insertions(+), 132 deletions(-) create mode 100644 tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c
--
2.56.1
PATCH v10 1/18 perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants
Replace the hardcoded field width (8) and the equivalent 0xff mask used for PERF_METRICS MSR field access with named constants INTEL_TD_METRIC_FIELD_{BITS,MASK}.
KVM will use INTEL_TD_METRIC_FIELD_BITS to derive the reserved bits of the PERF_METRICS MSR.
No functional change intended.
---
v10:
- Keep literal 8 for umask shift in INTEL_TD_METRIC_AVAILABLE_MAX. (Sashiko)
- Sync icl_get_metrics_event_value() comment with INTEL_TD_METRIC_FIELD_MASK.
v9: new patch.
--- arch/x86/events/intel/core.c | 11 ++++++----- arch/x86/include/asm/perf_event.h | 4 ++++ 2 files changed, 10 insertions(+), 5 deletions(-)
arch/x86/events/intel/core.c +6 -5
@@ -3180,15 +3180,16 @@ DEFINE_STATIC_CALL(intel_pmu_set_topdown_event_period, x86_perf_event_set_periodstatic inline u64 icl_get_metrics_event_value (u64 metric,u64 slots,int idx) { +int shift = (idx -INTEL_PMC_IDX_METRIC_BASE ) *INTEL_TD_METRIC_FIELD_BITS ;u32 val;/* -* The metric is reported as an 8bit integer fraction -* summing up to 0xff. -* slots-in-metric = (Metric / 0xff) * slots +* The metric is reported as an integer fraction +* summing up to INTEL_TD_METRIC_FIELD_MASK. +* slots-in-metric = (Metric / INTEL_TD_METRIC_FIELD_MASK) * slots */ - val = (metric >> ((idx -INTEL_PMC_IDX_METRIC_BASE ) *8 )) &0xff ; -return mul_u64_u32_div (slots, val,0xff ); + val = (metric >> shift) &INTEL_TD_METRIC_FIELD_MASK ; +return mul_u64_u32_div (slots, val,INTEL_TD_METRIC_FIELD_MASK ); } static u64 icl_get_topdown_value(struct perf_event *event,
arch/x86/include/asm/perf_event.h +4 -0
@@ -418,6 +418,10 @@ static inline bool use_fixed_pseudo_encoding(u64 code)#define INTEL_TD_METRIC_MAX INTEL_TD_METRIC_MEM_BOUND#define INTEL_TD_METRIC_NUM 8 +/* Width and mask of each metric field in the PERF_METRICS MSR. */ +#define INTEL_TD_METRIC_FIELD_BITS 8 +#define INTEL_TD_METRIC_FIELD_MASK GENMASK_ULL(INTEL_TD_METRIC_FIELD_BITS - 1, 0) +#define INTEL_TD_CFG_METRIC_CLEAR_BIT 0#define INTEL_TD_CFG_METRIC_CLEAR BIT_ULL(INTEL_TD_CFG_METRIC_CLEAR_BIT)
Signature · 1 line
2.56.1
PATCH v10 2/18 perf/x86/intel: Do not inflate capabilities for guests
In reply to Zide Chenintel_pmu_broken_perf_cap() works around erratum RPL018 by forcing perf_metrics on for RPL and MTL P-cores. When running as a guest, this may erroneously advertise a feature the vCPU does not possess.
Skip the workaround when running under a hypervisor.
---
v10: new patch.
--- arch/x86/events/intel/core.c | 4 ++++ 1 file changed, 4 insertions(+)
arch/x86/events/intel/core.c +4 -0
@@ -6163,6 +6163,10 @@ static void intel_pmu_check_extra_regs(struct extra_reg *extra_regs);static inline bool intel_pmu_broken_perf_cap (void ) { +/* Don't inflate capabilities when running as a guest. */ +if (boot_cpu_has (X86_FEATURE_HYPERVISOR )) +return false; +/* * The Perf Metric (Bit 15) is always cleared on P-core of * RPL and MTL. Details can be found in RPL018 erratum.
Signature · 1 line
2.56.1
PATCH v10 3/18 perf/x86/intel: Track common hybrid capabilities
In reply to Zide ChenNow that intel_update_pmu_caps() updates intel_cap on hybrid_pmu[], the assignment in intel_pmu_init_hybrid() is obsolete and can be dropped. Additionally, ADL P-cores report perf_metrics correctly, so hardcoding perf_metrics for P-cores is not required.
Intersect capabilities across all online PMU types. This clears all potentially inflated asymmetric features (such as rdpmc_metrics_clear) from x86_pmu.intel_cap when the BSP is a P-core.
For erratum RPL018, set perf_metrics in the base x86_pmu.intel_cap on all-P-core systems to keep x86_pmu consistent and allow consumers like KVM to detect Topdown support.
Opportunistically fix a typo in the comment.
---
v10: new patch.
--- arch/x86/events/intel/core.c | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-)
arch/x86/events/intel/core.c +16 -8
@@ -6280,11 +6280,23 @@ static void intel_update_pmu_caps(struct pmu *pmu)/* * Restore perf_metrics on platforms with broken -* perf_capablities. +* perf_capabilities. */ if (intel_pmu_broken_perf_cap () && -hybrid_pmu (pmu)->pmu_type == hybrid_big) +hybrid_pmu (pmu)->pmu_type == hybrid_big) {hybrid (pmu, intel_cap).perf_metrics =1 ; +if (!cpu_feature_enabled (X86_FEATURE_HYBRID_CPU )) + x86_pmu.intel_cap.perf_metrics =1 ; + } + +/* +* For hybrid, x86_pmu.intel_cap tracks common capabilities +* across all online PMU types. A bitwise AND is safe because +* multi-bit PEBS/LBR format fields are identical across core +* types when supported, or zero if unsupported on a core. +*/ + x86_pmu.intel_cap.capabilities &= +hybrid (pmu, intel_cap).capabilities; } }
@@ -7746,14 +7758,10 @@ static __always_inline int intel_pmu_init_hybrid(enum hybrid_pmu_type pmus)__EVENT_CONSTRAINT (0 , pmu->cntr_mask64,0 ,x86_pmu_num_counters (&pmu->pmu),0 ,0 ); - pmu->intel_cap.capabilities = x86_pmu.intel_cap.capabilities; -if (pmu->pmu_type & hybrid_small_tiny) { - pmu->intel_cap.perf_metrics =0 ; +if (pmu->pmu_type & hybrid_small_tiny) pmu->mid_ack = true; - }else if (pmu->pmu_type & hybrid_big) { - pmu->intel_cap.perf_metrics =1 ; +else if (pmu->pmu_type & hybrid_big) pmu->late_ack = true; - } }return 0 ;
Signature · 1 line
2.56.1
PATCH v10 4/18 perf/x86: Expose perf_capabilities to KVM
In reply to Zide Chenperf already applies model-specific errata (e.g. RPL018 on RPL/MTL) and sanitizes contradictory values (e.g. clearing pebs_baseline when the PEBS format is < 4). Reading the MSR directly in KVM bypasses all of that.
Add perf_capabilities to struct x86_pmu_capability, next to the counter counts, bit widths and event masks it already exports, so that KVM can use the validated value.
---
v10: New patch.
--- arch/x86/events/core.c | 1 + arch/x86/include/asm/perf_event.h | 1 + 2 files changed, 2 insertions(+)
arch/x86/events/core.c +1 -0
@@ -3164,6 +3164,7 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap) cap->events_mask_len = x86_pmu.events_mask_len; cap->pebs_ept = x86_pmu.pebs_ept; cap->mediated = !!(pmu.capabilities &PERF_PMU_CAP_MEDIATED_VPMU ); + cap->perf_capabilities = x86_pmu.intel_cap.capabilities; }EXPORT_SYMBOL_FOR_KVM (perf_get_x86_pmu_capability);
arch/x86/include/asm/perf_event.h +1 -0
@@ -308,6 +308,7 @@ struct x86_pmu_capability {int events_mask_len; unsigned int pebs_ept :1; unsigned int mediated :1; +u64 perf_capabilities; };/*
Signature · 1 line
2.56.1
PATCH v10 5/18 perf/x86: Expose number of Topdown metric events to KVM
In reply to Zide ChenThe number of Topdown metric events varies by CPU: ICL/TGL/RKL support 4, while SPR/GNR/DMR and some hybrid CPUs support 8.
Add num_topdown_events to struct x86_pmu_capability and populate it in perf_get_x86_pmu_capability() so that KVM can determine how many PERF_METRICS fields are valid on the host CPU and correctly identify the reserved bits in IA32_PERF_METRICS.
---
v10:
- Add Reviewed-by tag from Dapeng Mi.
- Export num_topdown_events as 0 if PERF_METRICS is disabled or absent.
--- arch/x86/events/core.c | 2 ++ arch/x86/include/asm/perf_event.h | 1 + 2 files changed, 3 insertions(+)
arch/x86/events/core.c +2 -0
@@ -3165,6 +3165,8 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap) cap->pebs_ept = x86_pmu.pebs_ept; cap->mediated = !!(pmu.capabilities &PERF_PMU_CAP_MEDIATED_VPMU ); cap->perf_capabilities = x86_pmu.intel_cap.capabilities; + cap->num_topdown_events = x86_pmu.intel_cap.perf_metrics ? + x86_pmu.num_topdown_events :0 ; }EXPORT_SYMBOL_FOR_KVM (perf_get_x86_pmu_capability);
arch/x86/include/asm/perf_event.h +1 -0
@@ -306,6 +306,7 @@ struct x86_pmu_capability {int bit_width_fixed;unsigned int events_mask;int events_mask_len; +int num_topdown_events; unsigned int pebs_ept :1; unsigned int mediated :1;u64 perf_capabilities;
Signature · 1 line
2.56.1
PATCH v10 6/18 KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events
In reply to Zide ChenOnly fixed counters 0..2 have matching generic cross-platform hardware perf events (INSTRUCTIONS, CPU_CYCLES, REF_CPU_CYCLES). Therefore, perf_get_hw_event_config() is only applicable to these counters.
KVM does not intend to emulate fixed counters >= 3 on legacy (non-mediated) vPMU, while for mediated vPMU, KVM does not care what the fixed counter event mappings are. Therefore, return 0 for their eventsel.
The two BUILD_BUG_ON() checks are no longer needed, so drop them along with __always_inline.
---
v10:
- Add comment to mention event mapping is not used for mediated PMU. v6:
- Re-arrange the code for early return. Clearer.
--- arch/x86/kvm/vmx/pmu_intel.c | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +9 -6
@@ -468,11 +468,8 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)* different perf_event is already utilizing the requested counter, but the end * result is the same (ignoring the fact that using a general purpose counter * will likely exacerbate counter contention). -* -* Forcibly inlined to allow asserting on @index at build time, and there should -* never be more than one user. */ -static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index) +static u64 intel_get_fixed_pmc_eventsel (unsigned int index) {const enum perf_hw_id fixed_pmc_perf_ids[] = { [0 ] =PERF_COUNT_HW_INSTRUCTIONS ,
@@ -481,8 +478,14 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index) };u64 eventsel; -BUILD_BUG_ON (ARRAY_SIZE (fixed_pmc_perf_ids) !=KVM_MAX_NR_INTEL_FIXED_COUNTERS ); -BUILD_BUG_ON (index >=KVM_MAX_NR_INTEL_FIXED_COUNTERS ); +/* +* Fixed counters 3 and above don't have a corresponding generic +* hardware perf event, and KVM does not intend to emulate them on +* non-mediated vPMU. For mediated vPMU, event mappings are unused +* since the hardware counters are passed through directly. +*/ +if (index >=ARRAY_SIZE (fixed_pmc_perf_ids)) +return 0 ;/* * Yell if perf reports support for a fixed counter but perf doesn't
Signature · 1 line
2.56.1
PATCH v10 7/18 KVM: x86/pmu: Query perf-reported capabilities instead of RDMSR
In reply to Zide ChenKVM reads MSR_IA32_PERF_CAPABILITIES directly, which bypasses the errata workarounds (e.g. RPL018) and sanitizing that perf applies to the value.
Use the capabilities reported by perf instead. This also avoids reading the MSR twice during VMX PMU initialization.
---
v10:
- Re-purposed the patch "Snapshot host IA32_PERF_CAPABILITIES" to simple references update.
--- arch/x86/kvm/vmx/pmu_intel.c | 7 +------ arch/x86/kvm/vmx/vmx.c | 8 ++------ 2 files changed, 3 insertions(+), 12 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +1 -6
@@ -775,16 +775,11 @@ void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu)static bool intel_pmu_is_mediated_pmu_supported (struct x86_pmu_capability *host_pmu) { -u64 host_perf_cap =0 ; - -if (boot_cpu_has (X86_FEATURE_PDCM )) -rdmsrq (MSR_IA32_PERF_CAPABILITIES , host_perf_cap); -/* * Require v4+ for MSR_CORE_PERF_GLOBAL_STATUS_SET, and full-width * writes so that KVM can precisely load guest counter values. */ -if (host_pmu->version <4 || !(host_perf_cap &PERF_CAP_FW_WRITES )) +if (host_pmu->version <4 || !(host_pmu->perf_capabilities &PERF_CAP_FW_WRITES ))return false;/*
arch/x86/kvm/vmx/vmx.c +2 -6
@@ -7889,14 +7889,10 @@ void vmx_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)static __init u64 vmx_get_perf_capabilities (void ) {u64 perf_cap =PERF_CAP_FW_WRITES ; -u64 host_perf_cap =0 ;if (!enable_pmu)return 0 ; -if (boot_cpu_has (X86_FEATURE_PDCM )) -rdmsrq (MSR_IA32_PERF_CAPABILITIES , host_perf_cap); -if (!cpu_feature_enabled (X86_FEATURE_ARCH_LBR ) && !enable_mediated_pmu) {x86_perf_get_lbr (&vmx_lbr_caps);
@@ -7909,11 +7905,11 @@ static __init u64 vmx_get_perf_capabilities(void)if (!vmx_lbr_caps.has_callstack)memset (&vmx_lbr_caps,0 ,sizeof (vmx_lbr_caps));else if (vmx_lbr_caps.nr) - perf_cap |= host_perf_cap &PERF_CAP_LBR_FMT ; + perf_cap |= kvm_pmu_cap.perf_capabilities &PERF_CAP_LBR_FMT ; }if (vmx_pebs_supported ()) { - perf_cap |= host_perf_cap &PERF_CAP_PEBS_MASK ; + perf_cap |= kvm_pmu_cap.perf_capabilities &PERF_CAP_PEBS_MASK ;/* * Disallow adaptive PEBS as it is functionally broken, can be
Signature · 1 line
2.56.1
PATCH v10 8/18 KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h
In reply to Zide ChenMove vcpu_get_perf_capabilities() from vmx/pmu_intel.h to pmu.h so that common x86 code can check whether the guest has PERF_METRICS.
Rename the helper to kvm_vcpu_get_perf_caps() to indicate that it is part of the common API, and shorten _capabilities to _caps.
No functional change intended.
--- arch/x86/kvm/pmu.h | 10 ++++++++++ arch/x86/kvm/vmx/pmu_intel.c | 6 +++--- arch/x86/kvm/vmx/pmu_intel.h | 12 ++---------- 3 files changed, 15 insertions(+), 13 deletions(-)
arch/x86/kvm/pmu.h +10 -0
@@ -6,6 +6,8 @@#include <asm/kvm_host.h> +#include "cpuid.h" +#define vcpu_to_pmu (vcpu) (&(vcpu)->arch.pmu)#define pmu_to_vcpu (pmu) (container_of((pmu), struct kvm_vcpu, arch.pmu))#define pmc_to_pmu (pmc) (&(pmc)->vcpu->arch.pmu)
@@ -271,6 +273,14 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu) X86_PMC_IDX_MAX); } +static inline u64 kvm_vcpu_get_perf_caps (struct kvm_vcpu *vcpu) +{ +if (!guest_cpu_cap_has (vcpu,X86_FEATURE_PDCM )) +return 0 ; + +return vcpu->arch.perf_capabilities; +} +void kvm_pmu_deliver_pmi (struct kvm_vcpu *vcpu);int kvm_pmu_rdpmc (struct kvm_vcpu *vcpu,unsigned pmc,u64 *data);int kvm_pmu_check_rdpmc_early (struct kvm_vcpu *vcpu,unsigned int idx);
arch/x86/kvm/vmx/pmu_intel.c +3 -3
@@ -193,13 +193,13 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)case MSR_CORE_PERF_FIXED_CTR_CTRL :return kvm_pmu_has_perf_global_ctrl (pmu);case MSR_IA32_PEBS_ENABLE : - ret =vcpu_get_perf_capabilities (vcpu) &PERF_CAP_PEBS_FORMAT ; + ret =kvm_vcpu_get_perf_caps (vcpu) &PERF_CAP_PEBS_FORMAT ;break ;case MSR_IA32_DS_AREA : ret =guest_cpu_cap_has (vcpu,X86_FEATURE_DS );break ;case MSR_PEBS_DATA_CFG : - perf_capabilities =vcpu_get_perf_capabilities (vcpu); + perf_capabilities =kvm_vcpu_get_perf_caps (vcpu); ret = (perf_capabilities &PERF_CAP_PEBS_BASELINE ) && ((perf_capabilities &PERF_CAP_PEBS_FORMAT ) >3 );break ;
@@ -555,7 +555,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu) pmu->raw_event_mask |= (HSW_IN_TX |HSW_IN_TX_CHECKPOINTED ); } - perf_capabilities =vcpu_get_perf_capabilities (vcpu); + perf_capabilities =kvm_vcpu_get_perf_caps (vcpu);if (intel_pmu_lbr_is_compatible (vcpu) && (perf_capabilities &PERF_CAP_LBR_FMT ))memcpy (&lbr_desc->records, &vmx_lbr_caps,sizeof (vmx_lbr_caps));
arch/x86/kvm/vmx/pmu_intel.h +2 -10
@@ -4,19 +4,11 @@#include <linux/kvm_host.h> -#include "cpuid.h" - -static inline u64 vcpu_get_perf_capabilities (struct kvm_vcpu *vcpu) -{ -if (!guest_cpu_cap_has (vcpu,X86_FEATURE_PDCM )) -return 0 ; - -return vcpu->arch.perf_capabilities; -} +#include "pmu.h" static inline bool fw_writes_is_enabled (struct kvm_vcpu *vcpu) { -return (vcpu_get_perf_capabilities (vcpu) &PERF_CAP_FW_WRITES ) !=0 ; +return (kvm_vcpu_get_perf_caps (vcpu) &PERF_CAP_FW_WRITES ) !=0 ; }bool intel_pmu_lbr_is_enabled (struct kvm_vcpu *vcpu);
Signature · 1 line
2.56.1
PATCH v10 9/18 KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters
In reply to Zide ChenIn preparation for supporting AnyThread deprecation and RDPMC Metrics Clear Mode, mask only INTEL_FIXED_0_KERNEL and INTEL_FIXED_0_USER when updating fixed_ctr_ctrl_hw in kvm_mediated_pmu_refresh_event_filter().
Masking the full 0xf nibble would cause two issues:
1. AnyThread for fixed counters 0-2: When a counter is allowed, copying the full nibble from guest-visible pmu->fixed_ctr_ctrl back into hardware-visible pmu->fixed_ctr_ctrl_hw would resurrect AnyThread in hardware, defeating SMT cross-VM isolation.
2. Metrics Clear for fixed counter 3: If an event filter denies fixed counter 3, clearing 0xf inadvertently strips METRICS_CLEAR_EN from hardware. If the guest sets METRICS_CLEAR_EN and executes native RDPMC, metrics will remain uncleared, returning stale data.
Masking only the OS/USR bits gates hardware counting without clobbering other control bits, matching general-purpose counter filtering which only masks the event ENABLE bit.
---
v10:
- New patch split from "KVM: x86/pmu: Support RDPMC Metrics Clear Mode".
--- arch/x86/kvm/pmu.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
arch/x86/kvm/pmu.c +4 -1
@@ -557,7 +557,10 @@ static void kvm_mediated_pmu_refresh_event_filter(struct kvm_pmc *pmc) pmc->eventsel_hw |= pmc->eventsel &ARCH_PERFMON_EVENTSEL_ENABLE ; }else { -u64 mask =intel_fixed_bits_by_idx (pmc->idx -KVM_FIXED_PMC_BASE_IDX ,0xf ); +u64 mask; + + mask =intel_fixed_bits_by_idx (pmc->idx -KVM_FIXED_PMC_BASE_IDX , +INTEL_FIXED_0_KERNEL |INTEL_FIXED_0_USER ); pmu->fixed_ctr_ctrl_hw &= ~mask;if (allowed)
Signature · 1 line
2.56.1
PATCH v10 10/18 KVM: x86/pmu: Block PMU passthrough if guest lacks PERF_METRICS
In reply to Zide ChenOn hosts supporting PERF_METRICS, if either the PERF_METRICS MSR or fixed counter 3 is absent from the guest, keep GLOBAL_CTRL and RDPMC intercepted so the guest cannot set GLOBAL_CTRL[48] or read metrics via RDPMC without fixed counter 3.
KVM does not expose PERF_METRICS or fixed counter 3 to guests yet, so this keeps GLOBAL_CTRL and RDPMC intercepted on hosts that support PERF_METRICS until both are virtualized.
Opportunistically, rename kvm_need_any_pmc_intercept() to kvm_pmu_incomplete_passthrough(). The function does not decide whether a particular PMC MSR needs interception. It reports whether the guest PMU differs from the host PMU, and the PERF_METRICS check widens that further.
---
v10: new patch.
--- arch/x86/include/asm/msr-index.h | 1 + arch/x86/kvm/pmu.c | 22 +++++++++++++++++----- arch/x86/kvm/pmu.h | 5 +++++ 3 files changed, 23 insertions(+), 5 deletions(-)
arch/x86/include/asm/msr-index.h +1 -0
@@ -331,6 +331,7 @@#define PERF_CAP_PEBS_FORMAT 0xf00#define PERF_CAP_FW_WRITES BIT_ULL(13)#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_PEBS_MASK (PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \ PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \
arch/x86/kvm/pmu.c +17 -5
@@ -778,7 +778,7 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)return 0 ; } -static bool kvm_need_any_pmc_intercept (struct kvm_vcpu *vcpu) +static bool kvm_pmu_incomplete_passthrough (struct kvm_vcpu *vcpu) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu);
@@ -790,13 +790,25 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu)* KVM's capabilities are constrained based on KVM support, i.e. KVM's * capabilities themselves may be a subset of hardware capabilities. */ -return pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp || - pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed; +if (pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp || + pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed) +return true; + +/* +* RDPMC passthrough must be blocked if fixed counter 3 is missing +* to avoid leaking host metrics, as IA32_PERF_METRICS is not +* context-switched without it. +* +* That condition is already caught by the counter count check above, +* so only check for PERF_METRICS here. +*/ +return (kvm_host_pmu.perf_capabilities &PERF_CAP_PERF_METRICS ) && + !kvm_vcpu_has_perf_metrics (vcpu); }bool kvm_need_perf_global_ctrl_intercept (struct kvm_vcpu *vcpu) { -return kvm_need_any_pmc_intercept (vcpu) || +return kvm_pmu_incomplete_passthrough (vcpu) || !kvm_pmu_has_perf_global_ctrl (vcpu_to_pmu (vcpu)); }EXPORT_SYMBOL_FOR_KVM_INTERNAL (kvm_need_perf_global_ctrl_intercept);
@@ -812,7 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)if (enable_vmware_backdoor)return true; -return kvm_need_any_pmc_intercept (vcpu) || +return kvm_pmu_incomplete_passthrough (vcpu) || pmu->counter_bitmask[KVM_PMC_GP ] != (BIT_ULL (kvm_host_pmu.bit_width_gp) -1 ) || pmu->counter_bitmask[KVM_PMC_FIXED ] != (BIT_ULL (kvm_host_pmu.bit_width_fixed) -1 ); }
arch/x86/kvm/pmu.h +5 -0
@@ -281,6 +281,11 @@ static inline u64 kvm_vcpu_get_perf_caps(struct kvm_vcpu *vcpu)return vcpu->arch.perf_capabilities; } +static inline bool kvm_vcpu_has_perf_metrics (struct kvm_vcpu *vcpu) +{ +return kvm_vcpu_get_perf_caps (vcpu) &PERF_CAP_PERF_METRICS ; +} +void kvm_pmu_deliver_pmi (struct kvm_vcpu *vcpu);int kvm_pmu_rdpmc (struct kvm_vcpu *vcpu,unsigned pmc,u64 *data);int kvm_pmu_check_rdpmc_early (struct kvm_vcpu *vcpu,unsigned int idx);
Signature · 1 line
2.56.1
PATCH v10 11/18 KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU
In reply to Zide ChenFrom: Dapeng Mi <[email protected]>
Starting with Ice Lake, Intel introduced fixed counter 3, which counts TOPDOWN.SLOTS - the number of available slots for an unhalted logical processor. It serves as the denominator for top-level metrics in the Topdown Microarchitecture Analysis method.
Emulating this counter on legacy vPMU would require introducing a new generic perf encoding for the Intel-specific TOPDOWN.SLOTS event in order to call perf_get_hw_event_config(). This is undesirable as it would pollute the generic perf event encoding.
Moreover, KVM does not intend to emulate IA32_PERF_METRICS in the legacy vPMU model, and without IA32_PERF_METRICS, emulating this counter has little practical value. Therefore, expose fixed counter 3 to guests only when mediated vPMU is enabled.
On PMU v5+, clamp num_counters_fixed to the number of contiguous fixed counters enumerated in CPUID.0xA:EDX[4:0] to prevent exposing non- contiguous counter holes that KVM cannot emulate.
---
v10:
- Rename INTEL_FIXED_SLOTS_IDX to KVM_FIXED_SLOTS_IDX to match naming convention in pmu.h.
- Add explanatory comment for non-mediated PMU fixed counter capping.
--- arch/x86/include/asm/kvm_host.h | 2 +- arch/x86/kvm/msrs.c | 4 ++-- arch/x86/kvm/pmu.c | 23 ++++++++++++++++++++++- arch/x86/kvm/pmu.h | 1 + 4 files changed, 26 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
@@ -563,7 +563,7 @@ struct kvm_pmc {#define KVM_MAX_NR_GP_COUNTERS KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \ KVM_MAX_NR_AMD_GP_COUNTERS) -#define KVM_MAX_NR_INTEL_FIXED_COUNTERS 3 +#define KVM_MAX_NR_INTEL_FIXED_COUNTERS 4#define KVM_MAX_NR_AMD_FIXED_COUNTERS 0#define KVM_MAX_NR_FIXED_COUNTERS KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUNTERS, \ KVM_MAX_NR_AMD_FIXED_COUNTERS)
arch/x86/kvm/msrs.c +2 -2
@@ -217,7 +217,7 @@ static const u32 msrs_to_save_base[] = {static const u32 msrs_to_save_pmu[] = {MSR_ARCH_PERFMON_FIXED_CTR0 ,MSR_ARCH_PERFMON_FIXED_CTR1 , -MSR_ARCH_PERFMON_FIXED_CTR0 +2 , +MSR_ARCH_PERFMON_FIXED_CTR2 ,MSR_ARCH_PERFMON_FIXED_CTR3 ,MSR_CORE_PERF_FIXED_CTR_CTRL ,MSR_CORE_PERF_GLOBAL_STATUS ,MSR_CORE_PERF_GLOBAL_CTRL ,MSR_IA32_PEBS_ENABLE ,MSR_IA32_DS_AREA ,MSR_PEBS_DATA_CFG ,
@@ -2682,7 +2682,7 @@ void kvm_init_msr_lists(void) {unsigned i; -BUILD_BUG_ON_MSG (KVM_MAX_NR_FIXED_COUNTERS !=3 , +BUILD_BUG_ON_MSG (KVM_MAX_NR_FIXED_COUNTERS !=4 ,"Please update the fixed PMCs in msrs_to_save_pmu[]" ); num_msrs_to_save =0 ;
arch/x86/kvm/pmu.c +22 -1
@@ -19,6 +19,7 @@#include <linux/moduleparam.h> #include <asm/perf_event.h> #include <asm/cpu_device_id.h> +#include <asm/cpuid/api.h> #include "x86.h" #include "cpuid.h" #include "lapic.h"
@@ -99,7 +100,8 @@ static const struct x86_cpu_id vmx_pebs_pdist_cpu[] = {* all perf counters (both gp and fixed). The mapping relationship * between pmc and perf counters is as the following: * * Intel: [0 .. KVM_MAX_NR_INTEL_GP_COUNTERS-1] <=> gp counters -* [KVM_FIXED_PMC_BASE_IDX .. KVM_FIXED_PMC_BASE_IDX + 2] <=> fixed +* [KVM_FIXED_PMC_BASE_IDX .. KVM_FIXED_PMC_BASE_IDX + +* KVM_MAX_NR_INTEL_FIXED_COUNTERS - 1] <=> fixed * * AMD: [0 .. AMD64_NUM_COUNTERS-1] and, for families 15H * and later, [0 .. AMD64_NUM_COUNTERS_CORE-1] <=> gp counters */
@@ -134,6 +136,8 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops) {bool is_intel = boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL ;int min_nr_gp_ctrs = pmu_ops->MIN_NR_GP_COUNTERS; +union cpuid10_edx edx; +u32 eax, ebx, ecx;/* * Hybrid PMUs don't play nice with virtualization without careful
@@ -181,6 +185,23 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops) kvm_pmu_cap.num_counters_fixed =min (kvm_pmu_cap.num_counters_fixed,KVM_MAX_NR_FIXED_COUNTERS ); +/* +* Currently, KVM doesn't support non-contiguous fixed counters; make +* sure only contiguous ones are retained in kvm_pmu_cap. +*/ +if (kvm_host_pmu.version >=5 ) { +cpuid (0xa , &eax, &ebx, &ecx, &edx.full); +if (kvm_pmu_cap.num_counters_fixed > edx.split.num_counters_fixed) + kvm_pmu_cap.num_counters_fixed = edx.split.num_counters_fixed; + } + +/* +* Fixed counters >= KVM_FIXED_SLOTS_IDX (counter 3) do not map to +* generic perf events and cannot be emulated without mediated PMU. +*/ +if (!enable_mediated_pmu && kvm_pmu_cap.num_counters_fixed >KVM_FIXED_SLOTS_IDX ) + kvm_pmu_cap.num_counters_fixed =KVM_FIXED_SLOTS_IDX ; + kvm_pmu_eventsel.INSTRUCTIONS_RETIRED =perf_get_hw_event_config (PERF_COUNT_HW_INSTRUCTIONS ); kvm_pmu_eventsel.BRANCH_INSTRUCTIONS_RETIRED =
arch/x86/kvm/pmu.h +1 -0
@@ -24,6 +24,7 @@#define VMWARE_BACKDOOR_PMC_APPARENT_TIME 0x10002#define KVM_FIXED_PMC_BASE_IDX INTEL_PMC_IDX_FIXED +#define KVM_FIXED_SLOTS_IDX (INTEL_PMC_IDX_FIXED_SLOTS - INTEL_PMC_IDX_FIXED)struct kvm_pmu_ops { struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu,
Signature · 1 line
2.56.1
PATCH v10 12/18 KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks
In reply to Zide ChenThe current RDPMC emulation splits responsibility: rdpmc_ecx_to_pmc() in each vendor returns a kvm_pmc, then common code calls pmc_read_counter().
This design cannot support RDPMC reads that do not map to a counter, such as PERF_METRICS on Intel platforms.
Replace rdpmc_ecx_to_pmc() with emulate_rdpmc(), which takes full ownership of the emulation and writes the result directly into @data.
Opportunistically drop the redundant bitmask in intel_emulate_rdpmc() since pmc_read_counter() already applies the counter's bit-width mask.
No functional change intended.
---
v6: new patch.
--- arch/x86/include/asm/kvm-x86-pmu-ops.h | 2 +- arch/x86/kvm/pmu.c | 9 +-------- arch/x86/kvm/pmu.h | 4 ++-- arch/x86/kvm/svm/pmu.c | 14 ++++++++++---- arch/x86/kvm/vmx/pmu_intel.c | 25 ++++++++++++------------- 5 files changed, 26 insertions(+), 28 deletions(-)
arch/x86/include/asm/kvm-x86-pmu-ops.h +1 -1
@@ -13,7 +13,7 @@* KVM_X86_PMU_OP_OPTIONAL() can be used for those functions that can have * a NULL definition. */ -KVM_X86_PMU_OP(rdpmc_ecx_to_pmc) +KVM_X86_PMU_OP(emulate_rdpmc) KVM_X86_PMU_OP(msr_idx_to_pmc) KVM_X86_PMU_OP_OPTIONAL(check_rdpmc_early) KVM_X86_PMU_OP(is_valid_msr)
arch/x86/kvm/pmu.c +1 -8
@@ -777,8 +777,6 @@ static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)int kvm_pmu_rdpmc (struct kvm_vcpu *vcpu,unsigned idx,u64 *data) {struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); -struct kvm_pmc *pmc; -u64 mask = ~0ull ;if (!pmu->version)return 1 ;
@@ -786,17 +784,12 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)if (is_vmware_backdoor_pmc (idx))return kvm_pmu_rdpmc_vmware (vcpu, idx, data); - pmc =kvm_pmu_call (rdpmc_ecx_to_pmc)(vcpu, idx, &mask); -if (!pmc) -return 1 ; -if (!kvm_is_cr4_bit_set (vcpu,X86_CR4_PCE ) && (kvm_x86_call (get_cpl)(vcpu) !=0 ) &&kvm_is_cr0_bit_set (vcpu,X86_CR0_PE ))return 1 ; - *data =pmc_read_counter (pmc) & mask; -return 0 ; +return kvm_pmu_call (emulate_rdpmc)(vcpu, idx, data); }static bool kvm_pmu_incomplete_passthrough (struct kvm_vcpu *vcpu)
arch/x86/kvm/pmu.h +2 -2
@@ -27,8 +27,8 @@#define KVM_FIXED_SLOTS_IDX (INTEL_PMC_IDX_FIXED_SLOTS - INTEL_PMC_IDX_FIXED)struct kvm_pmu_ops { -struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu, -unsigned int idx,u64 *mask); +int (*emulate_rdpmc)(struct kvm_vcpu *vcpu,unsigned int idx, +u64 *data);struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu,u32 msr);int (*check_rdpmc_early)(struct kvm_vcpu *vcpu,unsigned int idx);bool (*is_valid_msr)(struct kvm_vcpu *vcpu,u32 msr);
arch/x86/kvm/svm/pmu.c +10 -4
@@ -84,10 +84,16 @@ static int amd_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx) }/* idx is the ECX register of RDPMC instruction */ -static struct kvm_pmc *amd_rdpmc_ecx_to_pmc (struct kvm_vcpu *vcpu, -unsigned int idx,u64 *mask) +static int amd_emulate_rdpmc (struct kvm_vcpu *vcpu,unsigned int idx, +u64 *data) { -return amd_pmu_get_pmc (vcpu_to_pmu (vcpu), idx); +struct kvm_pmc *pmc =amd_pmu_get_pmc (vcpu_to_pmu (vcpu), idx); + +if (!pmc) +return 1 ; + + *data =pmc_read_counter (pmc); +return 0 ; }static struct kvm_pmc *amd_msr_idx_to_pmc (struct kvm_vcpu *vcpu,u32 msr)
@@ -302,7 +308,7 @@ static bool amd_pmc_is_disabled_in_current_mode(struct kvm_pmc *pmc) }struct kvm_pmu_ops amd_pmu_ops__initdata = { - .rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc, + .emulate_rdpmc = amd_emulate_rdpmc, .msr_idx_to_pmc = amd_msr_idx_to_pmc, .check_rdpmc_early = amd_check_rdpmc_early, .is_valid_msr = amd_is_valid_msr,
arch/x86/kvm/vmx/pmu_intel.c +12 -13
@@ -85,14 +85,13 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) } } -static struct kvm_pmc *intel_rdpmc_ecx_to_pmc (struct kvm_vcpu *vcpu, -unsigned int idx,u64 *mask) +static int intel_emulate_rdpmc (struct kvm_vcpu *vcpu,unsigned int idx, +u64 *data) {unsigned int type = idx &INTEL_RDPMC_TYPE_MASK ;struct kvm_pmu *pmu =vcpu_to_pmu (vcpu); -struct kvm_pmc *counters; +struct kvm_pmc *counters, *pmc;unsigned int num_counters; -u64 bitmask;/* * The encoding of ECX for RDPMC is different for architectural versus
@@ -105,7 +104,9 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,* as KVM doesn't support such PMUs. */ if (WARN_ON_ONCE (!pmu->version)) -return NULL ; +return 1 ; + + idx &=INTEL_RDPMC_INDEX_MASK ;/* * General Purpose (GP) PMCs are supported on all PMUs, and fixed PMCs
@@ -119,23 +120,21 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,case INTEL_RDPMC_FIXED : counters = pmu->fixed_counters; num_counters = pmu->nr_arch_fixed_counters; - bitmask = pmu->counter_bitmask[KVM_PMC_FIXED ];break ;case INTEL_RDPMC_GP : counters = pmu->gp_counters; num_counters = pmu->nr_arch_gp_counters; - bitmask = pmu->counter_bitmask[KVM_PMC_GP ];break ;default : -return NULL ; +return 1 ; } - idx &=INTEL_RDPMC_INDEX_MASK ;if (idx >= num_counters) -return NULL ; +return 1 ; - *mask &= bitmask; -return &counters[array_index_nospec (idx, num_counters)]; + pmc = &counters[array_index_nospec (idx, num_counters)]; + *data =pmc_read_counter (pmc); +return 0 ; }static inline struct kvm_pmc *get_fw_gp_pmc (struct kvm_pmu *pmu,u32 msr)
@@ -833,7 +832,7 @@ static void intel_mediated_pmu_put(struct kvm_vcpu *vcpu) }struct kvm_pmu_ops intel_pmu_ops__initdata = { - .rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc, + .emulate_rdpmc = intel_emulate_rdpmc, .msr_idx_to_pmc = intel_msr_idx_to_pmc, .is_valid_msr = intel_is_valid_msr, .get_msr = intel_pmu_get_msr,
Signature · 1 line
2.56.1
PATCH v10 13/18 KVM: x86/pmu: Emulate RDPMC on performance metrics
In reply to Zide ChenIf the host has the PERF_METRICS capability but it is not present on the guest, RDPMC interception must be enabled and KVM should inject a #GP when the guest attempts a PERF_METRICS RDPMC.
If the guest has PERF_METRICS but RDPMC interception is enabled for other reasons, KVM needs to emulate RDPMC with type INTEL_RDPMC_METRICS.
Add perf_metrics to struct kvm_pmu to store the guest's metrics value. RDPMC reads will return 0 until MSR_PERF_METRICS is virtualized and context switched.
---
v10:
- Drop kvm_need_rdpmc_intercept() hunk as it's included in previous patch already after patch re-order.
- Updated comments to mention Topdown metric type is not rejected. v9:
- Remove references to Metrics Clear from the changelog.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/vmx/pmu_intel.c | 20 ++++++++++++++++++-- 2 files changed, 19 insertions(+), 2 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -583,6 +583,7 @@ struct kvm_pmu {u64 global_status_rsvd;u64 reserved_bits;u64 raw_event_mask; +u64 perf_metrics;struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS ];struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS ];
arch/x86/kvm/vmx/pmu_intel.c +18 -2
@@ -31,6 +31,7 @@*/ #define INTEL_RDPMC_GP 0#define INTEL_RDPMC_FIXED INTEL_PMC_FIXED_RDPMC_BASE +#define INTEL_RDPMC_METRICS INTEL_PMC_FIXED_RDPMC_METRICS#define INTEL_RDPMC_TYPE_MASK GENMASK(31, 16)#define INTEL_RDPMC_INDEX_MASK GENMASK(15, 0)
@@ -113,8 +114,10 @@ static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx,* are supported on all architectural PMUs, i.e. on all virtual PMUs * supported by KVM. Note, KVM only emulates fixed PMCs for PMU v2+, * but the type itself is still valid, i.e. let RDPMC fail due to -* accessing a non-existent counter. Reject attempts to read all other -* types, which are unknown/unsupported. +* accessing a non-existent counter. +* +* Topdown metrics are supported only when the guest has PERF_METRICS. +* Reject attempts to read all other types, which are unknown/unsupported. */ switch (type) {case INTEL_RDPMC_FIXED :
@@ -125,6 +128,19 @@ static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx, counters = pmu->gp_counters; num_counters = pmu->nr_arch_gp_counters;break ; +case INTEL_RDPMC_METRICS : +if (!kvm_vcpu_has_perf_metrics (vcpu)) +return 1 ; + +/* +* The index in ECX[15:0] is implementation specific, but no +* platform currently supports a non-zero index. +*/ +if (idx) +return 1 ; + + *data = pmu->perf_metrics; +return 0 ;default :return 1 ; }
Signature · 1 line
2.56.1
PATCH v10 14/18 KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU
In reply to Zide ChenFrom: Dapeng Mi <[email protected]>
Expose IA32_PERF_CAPABILITIES.PERF_METRICS_AVAILABLE[bit 15] only when mediated vPMU is enabled, as emulating IA32_PERF_METRICS in the legacy vPMU model is impractical.
Enabling Topdown metrics in hardware requires both IA32_PERF_METRICS and fixed counter 3 (SLOTS). Therefore, gate MSR passthrough and hardware context switching on fixed counter 3 in addition to guest IA32_PERF_CAPABILITIES.PERF_METRICS_AVAILABLE[bit 15].
Architecturally, IA32_PERF_METRICS can be partly decoupled from fixed counter 3. In that case, keep the MSR intercepted and treat all metric fields as reserved (~0ull): reads return 0, writes of 0 succeed, and non-zero writes are rejected. IA32_PERF_GLOBAL_CTRL[48] can likewise be set without the counter.
Save and restore IA32_PERF_METRICS on vPMU context switches. Also add IA32_PERF_METRICS to msrs_to_save_pmu[] to support guest migration.
---
v10:
- Fold v9 patch 11 ("KVM: x86/pmu: Reject writes to reserved MSR_PERF_METRICS bits") into this patch. (Jim)
- Gate advertising PERF_CAP_PERF_METRICS on num_topdown_events != 0. (Jim)
- Gate IA32_PERF_METRICS passthrough, perf_metrics_rsvd sizing, and context switch on fixed counter 3. (Jim, Yosry)
v9:
- Remove the incorrect comment about writing to MSR_PERF_METRICS.
--- arch/x86/include/asm/kvm_host.h | 1 + arch/x86/kvm/msrs.c | 6 +++- arch/x86/kvm/pmu.c | 1 + arch/x86/kvm/pmu.h | 13 +++++++ arch/x86/kvm/vmx/nested.c | 2 ++ arch/x86/kvm/vmx/pmu_intel.c | 60 +++++++++++++++++++++++++++++++++ arch/x86/kvm/vmx/vmx.c | 12 +++++++ 7 files changed, 94 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
@@ -584,6 +584,7 @@ struct kvm_pmu {u64 reserved_bits;u64 raw_event_mask;u64 perf_metrics; +u64 perf_metrics_rsvd;struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS ];struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS ];
arch/x86/kvm/msrs.c +5 -1
@@ -219,7 +219,7 @@ static const u32 msrs_to_save_pmu[] = {MSR_ARCH_PERFMON_FIXED_CTR0 ,MSR_ARCH_PERFMON_FIXED_CTR1 ,MSR_ARCH_PERFMON_FIXED_CTR2 ,MSR_ARCH_PERFMON_FIXED_CTR3 ,MSR_CORE_PERF_FIXED_CTR_CTRL ,MSR_CORE_PERF_GLOBAL_STATUS , -MSR_CORE_PERF_GLOBAL_CTRL , +MSR_CORE_PERF_GLOBAL_CTRL ,MSR_PERF_METRICS ,MSR_IA32_PEBS_ENABLE ,MSR_IA32_DS_AREA ,MSR_PEBS_DATA_CFG , /* This part of MSRs should match KVM_MAX_NR_INTEL_GP_COUNTERS. */
@@ -2619,6 +2619,10 @@ static void kvm_probe_msr_to_save(u32 msr_index) intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2))return ;break ; +case MSR_PERF_METRICS : +if (!(kvm_caps.supported_perf_cap &PERF_CAP_PERF_METRICS )) +return ; +break ; case MSR_ARCH_PERFMON_PERFCTR0 ...MSR_ARCH_PERFMON_PERFCTR0 +KVM_MAX_NR_GP_COUNTERS -1 : if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
arch/x86/kvm/pmu.c +1 -0
@@ -1019,6 +1019,7 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu) pmu->fixed_ctr_ctrl_rsvd = ~0ull ; pmu->pebs_enable_rsvd = ~0ull ; pmu->pebs_data_cfg_rsvd = ~0ull ; + pmu->perf_metrics_rsvd = ~0ull ;bitmap_zero (pmu->all_valid_pmc_idx,X86_PMC_IDX_MAX );if (!vcpu->kvm->arch.enable_pmu)
arch/x86/kvm/pmu.h +13 -0
@@ -287,6 +287,19 @@ static inline bool kvm_vcpu_has_perf_metrics(struct kvm_vcpu *vcpu)return kvm_vcpu_get_perf_caps (vcpu) &PERF_CAP_PERF_METRICS ; } +static inline bool kvm_pmu_has_fixed_counter (struct kvm_pmu *pmu, +unsigned int idx) +{ +return idx < pmu->nr_arch_fixed_counters; +} + +/* Functional Topdown metrics in hardware also require fixed counter 3 (SLOTS). */ +static inline bool kvm_vcpu_can_enable_perf_metrics (struct kvm_vcpu *vcpu) +{ +return kvm_vcpu_has_perf_metrics (vcpu) && +kvm_pmu_has_fixed_counter (vcpu_to_pmu (vcpu),KVM_FIXED_SLOTS_IDX ); +} +void kvm_pmu_deliver_pmi (struct kvm_vcpu *vcpu);int kvm_pmu_rdpmc (struct kvm_vcpu *vcpu,unsigned pmc,u64 *data);int kvm_pmu_check_rdpmc_early (struct kvm_vcpu *vcpu,unsigned int idx);
arch/x86/kvm/vmx/nested.c +2 -0
@@ -724,6 +724,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,nested_vmx_merge_msr_bitmaps_rw (MSR_CORE_PERF_GLOBAL_CTRL );nested_vmx_merge_msr_bitmaps_read (MSR_CORE_PERF_GLOBAL_STATUS );nested_vmx_merge_msr_bitmaps_write (MSR_CORE_PERF_GLOBAL_OVF_CTRL ); + +nested_vmx_merge_msr_bitmaps_rw (MSR_PERF_METRICS ); }/*
arch/x86/kvm/vmx/pmu_intel.c +60 -0
108 lines not on this page · raw message
arch/x86/kvm/vmx/vmx.c +12 -0
@@ -4289,6 +4289,10 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu) MSR_TYPE_RW, intercept);vmx_set_intercept_for_msr (vcpu,MSR_CORE_PERF_GLOBAL_OVF_CTRL ,MSR_TYPE_RW , intercept); + + intercept = !has_mediated_pmu || !kvm_vcpu_can_enable_perf_metrics (vcpu); +vmx_set_intercept_for_msr (vcpu,MSR_PERF_METRICS , +MSR_TYPE_RW , intercept); }static void vmx_recalc_msr_intercepts (struct kvm_vcpu *vcpu)
@@ -7934,6 +7938,14 @@ static __init u64 vmx_get_perf_capabilities(void) perf_cap &= ~PERF_CAP_PEBS_BASELINE ; } +/* +* In addition to fixed counter 3, gate on num_topdown_events to +* avoid advertising on generic arch setups (e.g. virtualized hosts). +*/ +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 ; +return perf_cap; }
Signature · 1 line
2.56.1
PATCH v10 15/18 KVM: x86/pmu: Support RDPMC Metrics Clear Mode
In reply to Zide ChenAdd 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.
---
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(-)
arch/x86/include/asm/msr-index.h +1 -0
@@ -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)
arch/x86/kvm/vmx/pmu_intel.c +26 -0
@@ -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)
arch/x86/kvm/vmx/vmx.c +2 -1
@@ -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; }
Signature · 1 line
2.56.1
PATCH v10 16/18 KVM: selftests: Rework PMU guest measurement asm operands
In reply to Zide ChenGUEST_MEASURE_EVENT() hardcodes symbols specific to pmu_counters_test, preventing it from being safely shared with other PMU tests.
Flush and reload a local dummy stack variable instead of kvm_pmu_version. Because the dummy variable resides on the stack, pass its address in %rsi ("S") to remain immune to %rsp and %rbp changes caused by "enter $0, $0".
Also mark %rax/%rdx/%rcx as read-write outputs (+a/+d/+c) and add "cc" to clobbers since the asm modifies them, pass loop count via an immediate operand ("i"), and fix the "CLFUSH" typo in the comment.
No change in what the test measures.
---
v10: new patch.
--- .../selftests/kvm/x86/pmu_counters_test.c | 81 ++++++++++--------- 1 file changed, 45 insertions(+), 36 deletions(-)
tools/testing/selftests/kvm/x86/pmu_counters_test.c +45 -36
@@ -221,51 +221,60 @@ static void guest_assert_event_count(u8 idx, u32 pmc, u32 pmc_msr)/* * Enable and disable the PMC in a monolithic asm blob to ensure that the -* compiler can't insert _any_ code into the measured sequence. Note, ECX -* doesn't need to be clobbered as the input value, @pmc_msr, is restored -* before the end of the sequence. +* compiler can't insert _any_ code into the measured sequence. * -* If CLFUSH{,OPT} is supported, flush the cacheline containing (at least) the -* CLFUSH{,OPT} instruction on each loop iteration to force LLC references and -* misses, i.e. to allow testing that those events actually count. +* If CLFLUSH{,OPT} is supported, flush and reload a dummy cacheline on each +* loop iteration to force LLC references and misses, i.e. to allow testing +* that those events actually count. * * If forced emulation is enabled (and specified), force emulation on a subset * of the measured code to verify that KVM correctly emulates instructions and * branches retired events in conjunction with hardware also counting said * events. */ -#define GUEST_MEASURE_EVENT (_msr, _value, clflush,FEP ) \ -do { \ - __asm__ __volatile__("wrmsr\n\t" \ - " mov $" __stringify(NUM_LOOPS) ", %%ecx\n\t" \ - "1:\n\t" \ - FEP "enter $0, $0\n\t" \ - clflush "\n\t" \ - "mfence\n\t" \ - "mov %[m], %%eax\n\t" \ - FEP "leave\n\t" \ - FEP "loop 1b\n\t" \ - FEP "mov %%edi, %%ecx\n\t" \ - FEP "xor %%eax, %%eax\n\t" \ - FEP "xor %%edx, %%edx\n\t" \ - "wrmsr\n\t" \ - :: "a"((u32)_value), "d"(_value >> 32), \ - "c"(_msr), "D"(_msr), [m]"m"(kvm_pmu_version) \ - ); \ +#define GUEST_MEASURE_EVENT (_msr, _value, clflush,FEP , _loops) \ +do { \ + u32 _a = (u32)(_value), _d = (u32)((_value) >> 32); \ + u32 _c = (_msr); \ + u32 _dummy = 0; \ + \ + __asm__ __volatile__("wrmsr\n\t" \ + "mov %[loops], %%ecx\n\t" \ + "1:\n\t" \ + FEP "enter $0, $0\n\t" \ + clflush "\n\t" \ + "mfence\n\t" \ + "mov (%[ptr]), %%eax\n\t" \ + FEP "leave\n\t" \ + FEP "loop 1b\n\t" \ + FEP "mov %%edi, %%ecx\n\t" \ + FEP "xor %%eax, %%eax\n\t" \ + FEP "xor %%edx, %%edx\n\t" \ + "wrmsr\n\t" \ + : "+a"(_a), "+d"(_d), "+c"(_c) \ + : "D"(_msr), [ptr]"S"(&_dummy), \ + [loops]"i"(_loops) \ + : "memory", "cc" \ + ); \ } while (0) -#define GUEST_TEST_EVENT (_idx, _pmc, _pmc_msr, _ctrl_msr, _value,FEP ) \ -do { \ - wrmsr(_pmc_msr, 0); \ - \ - if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflushopt %[m]", FEP); \ - else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "clflush %[m]", FEP); \ - else \ - GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", FEP); \ - \ - guest_assert_event_count(_idx, _pmc, _pmc_msr); \ +#define GUEST_TEST_EVENT (_idx, _pmc, _pmc_msr, _ctrl_msr, _value,FEP ) \ +do { \ + wrmsr(_pmc_msr, 0); \ + \ + if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, \ + "clflushopt (%[ptr])", \ + FEP, NUM_LOOPS); \ + else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, \ + "clflush (%[ptr])", \ + FEP, NUM_LOOPS); \ + else \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", \ + FEP, NUM_LOOPS); \ + \ + guest_assert_event_count(_idx, _pmc, _pmc_msr); \ } while (0) static void __guest_test_arch_event(u8 idx, u32 pmc, u32 pmc_msr,
Signature · 1 line
2.56.1
PATCH v10 17/18 KVM: selftests: Move and generalize PMU guest measurement helpers
In reply to Zide ChenMove GUEST_MEASURE_EVENT() from pmu_counters_test.c into include/x86/pmu.h so the measurement assembly sequence can be shared across PMU selftests (e.g. Topdown PERF_METRICS).
Introduce GUEST_RUN_PAYLOAD() to execute the loop while selecting the supported cache-flush instruction (CLFLUSHOPT, CLFLUSH, or NOP).
No functional change intended.
---
v10: new patch.
--- tools/testing/selftests/kvm/include/x86/pmu.h | 54 +++++++++++++++++++ .../selftests/kvm/x86/pmu_counters_test.c | 53 +----------------- 2 files changed, 55 insertions(+), 52 deletions(-)
tools/testing/selftests/kvm/include/x86/pmu.h +54 -0
@@ -127,4 +127,58 @@ static inline bool this_pmu_has_errata(enum pmu_errata errata) return pmu_errata_mask & BIT_ULL(errata); } +/* +* Enable and disable the PMC in a monolithic asm blob to ensure that the +* compiler can't insert _any_ code into the measured sequence. +* +* If CLFLUSH{,OPT} is supported, flush and reload a dummy cacheline on each +* loop iteration to force LLC references and misses, i.e. to allow testing +* that those events actually count. +* +* If forced emulation is enabled (and specified), force emulation on a subset +* of the measured code to verify that KVM correctly emulates instructions and +* branches retired events in conjunction with hardware also counting said +* events. +*/ +#define GUEST_MEASURE_EVENT (_msr, _value, clflush,FEP , _loops) \ +do { \ + u32 _a = (u32)(_value), _d = (u32)((_value) >> 32); \ + u32 _c = (_msr); \ + u32 _dummy = 0; \ + \ + __asm__ __volatile__("wrmsr\n\t" \ + "mov %[loops], %%ecx\n\t" \ + "1:\n\t" \ + FEP "enter $0, $0\n\t" \ + clflush "\n\t" \ + "mfence\n\t" \ + "mov (%[ptr]), %%eax\n\t" \ + FEP "leave\n\t" \ + FEP "loop 1b\n\t" \ + FEP "mov %%edi, %%ecx\n\t" \ + FEP "xor %%eax, %%eax\n\t" \ + FEP "xor %%edx, %%edx\n\t" \ + "wrmsr\n\t" \ + : "+a"(_a), "+d"(_d), "+c"(_c) \ + : "D"(_msr), [ptr]"S"(&_dummy), \ + [loops]"i"(_loops) \ + : "memory", "cc" \ + ); \ +} while (0) + +#define GUEST_RUN_PAYLOAD (_ctrl_msr, _value,FEP , _loops) \ +do { \ + if (this_cpu_has(X86_FEATURE_CLFLUSHOPT)) \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, \ + "clflushopt (%[ptr])", \ + FEP, _loops); \ + else if (this_cpu_has(X86_FEATURE_CLFLUSH)) \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, \ + "clflush (%[ptr])", \ + FEP, _loops); \ + else \ + GUEST_MEASURE_EVENT(_ctrl_msr, _value, "nop", \ + FEP, _loops); \ +} while (0) +#endif /* SELFTEST_KVM_PMU_H */
tools/testing/selftests/kvm/x86/pmu_counters_test.c +1 -52
62 lines not on this page · raw message
Signature · 1 line
2.56.1
PATCH v10 18/18 KVM: selftests: Add Topdown PERF_METRICS test
In reply to Zide ChenAdd a dedicated selftest to verify Intel Topdown (PERF_METRICS) support under the mediated vPMU.
Verify across PMU versions and capability combinations:
- Positive Topdown counting.
- RDPMC Metrics Clear Mode.
- RDPMC (native and FEP), RDMSR, and WRMSR access to PERF_METRICS.
- Behavior when fixed counter 3 is absent.
- Behavior when PERF_CAP_PERF_METRICS is unadvertised.
- Host userspace save/restore via KVM_SET_MSRS and KVM_GET_MSRS.
---
v10:
- Move Topdown metrics tests to a dedicated file to avoid bloating pmu_counters_test.c.
- Add coverage for RDPMC Metrics Clear Mode, host MSR access, and capability permutations.
--- tools/arch/x86/include/asm/msr-index.h | 2 + tools/testing/selftests/kvm/Makefile.kvm | 1 + tools/testing/selftests/kvm/include/x86/pmu.h | 6 + .../selftests/kvm/x86/pmu_perf_metrics_test.c | 391 ++++++++++++++++++ 4 files changed, 400 insertions(+) create mode 100644 tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c
tools/arch/x86/include/asm/msr-index.h +2 -0
@@ -331,7 +331,9 @@#define PERF_CAP_PEBS_FORMAT 0xf00#define PERF_CAP_FW_WRITES BIT_ULL(13)#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)
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -106,6 +106,7 @@ TEST_GEN_PROGS_x86 += x86/nested_x2apic_test TEST_GEN_PROGS_x86 += x86/platform_info_test TEST_GEN_PROGS_x86 += x86/pmu_counters_test TEST_GEN_PROGS_x86 += x86/pmu_event_filter_test +TEST_GEN_PROGS_x86 += x86/pmu_perf_metrics_test TEST_GEN_PROGS_x86 += x86/private_mem_conversions_test TEST_GEN_PROGS_x86 += x86/private_mem_kvm_exits_test TEST_GEN_PROGS_x86 += x86/save_restore_pf_stress_test
tools/testing/selftests/kvm/include/x86/pmu.h +6 -0
@@ -50,11 +50,17 @@#define INTEL_RDPMC_FAST BIT_ULL(31)/* Fixed PMC controls, Intel only. */ +#define FIXED_PMC_SLOTS_IDX 3#define FIXED_PMC_GLOBAL_CTRL_ENABLE (_idx) BIT_ULL((32 + (_idx))) +/* PERF_METRICS enable, Intel only. */ +#define GLOBAL_CTRL_EN_PERF_METRICS BIT_ULL(48) +#define FIXED_PMC_KERNEL BIT_ULL(0)#define FIXED_PMC_USER BIT_ULL(1)#define FIXED_PMC_ANYTHREAD BIT_ULL(2) +/* Fixed counter 3 reuses bit 2 for METRICS_CLEAR_EN (Intel only). */ +#define FIXED_PMC_METRICS_CLEAR BIT_ULL(2)#define FIXED_PMC_ENABLE_PMI BIT_ULL(3)#define FIXED_PMC_NR_BITS 4#define FIXED_PMC_CTRL (_idx, _val) ((_val) << ((_idx) * FIXED_PMC_NR_BITS))
tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c new file +391 -0
391 lines not on this page · raw message
Signature · 1 line
2.56.1