* [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support
@ 2026-11-24 21:38 Zide Chen
2026-11-24 21:38 ` [PATCH v10 01/18] perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants Zide Chen
` (17 more replies)
0 siblings, 18 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
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/20260918193937.569414-1-zide.chen@intel.com/
v8: https://lore.kernel.org/kvm/20260817183143.226156-1-zide.chen@intel.com/
QEMU: https://lore.kernel.org/qemu-devel/20260604025546.19378-7-zide.chen@intel.com/
[1] https://lore.kernel.org/kvm/20261112203227.289379-1-zide.chen@intel.com/
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
^ permalink raw reply [flat|nested] 19+ messages in thread
* [PATCH v10 01/18] perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 02/18] perf/x86/intel: Do not inflate capabilities for guests Zide Chen
` (16 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
---
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(-)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index 1ac2ca35db53..c76d9aa8e5eb 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -3180,15 +3180,16 @@ DEFINE_STATIC_CALL(intel_pmu_set_topdown_event_period, x86_perf_event_set_period
static 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,
diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
index 1eb13673e889..ca630bc282fb 100644
--- a/arch/x86/include/asm/perf_event.h
+++ b/arch/x86/include/asm/perf_event.h
@@ -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)
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 02/18] perf/x86/intel: Do not inflate capabilities for guests
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
2026-11-24 21:38 ` [PATCH v10 01/18] perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 03/18] perf/x86/intel: Track common hybrid capabilities Zide Chen
` (15 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
intel_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.
Fixes: 97588df87b56 ("perf/x86/intel: Add common intel_pmu_init_hybrid()")
Fixes: dd384d661f06 ("perf/x86/intel: Fix intel_cap handling on hybrid PMUs")
Signed-off-by: Zide Chen <zide.chen@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
---
v10: new patch.
---
arch/x86/events/intel/core.c | 4 ++++
1 file changed, 4 insertions(+)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index c76d9aa8e5eb..dbd84d82f36d 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -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.
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 03/18] perf/x86/intel: Track common hybrid capabilities
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
2026-11-24 21:38 ` [PATCH v10 01/18] perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants Zide Chen
2026-11-24 21:38 ` [PATCH v10 02/18] perf/x86/intel: Do not inflate capabilities for guests Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 04/18] perf/x86: Expose perf_capabilities to KVM Zide Chen
` (14 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
Now 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.
Fixes: f83d2f91d259 ("perf/x86/intel: Add Alder Lake Hybrid support")
Signed-off-by: Zide Chen <zide.chen@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
---
v10: new patch.
---
arch/x86/events/intel/core.c | 24 ++++++++++++++++--------
1 file changed, 16 insertions(+), 8 deletions(-)
diff --git a/arch/x86/events/intel/core.c b/arch/x86/events/intel/core.c
index dbd84d82f36d..44bede63f0c6 100644
--- a/arch/x86/events/intel/core.c
+++ b/arch/x86/events/intel/core.c
@@ -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;
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 04/18] perf/x86: Expose perf_capabilities to KVM
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (2 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 03/18] perf/x86/intel: Track common hybrid capabilities Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 05/18] perf/x86: Expose number of Topdown metric events " Zide Chen
` (13 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
perf 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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
---
v10: New patch.
---
arch/x86/events/core.c | 1 +
arch/x86/include/asm/perf_event.h | 1 +
2 files changed, 2 insertions(+)
diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 8b3ea0adb965..770ca960fa6e 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -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);
diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
index ca630bc282fb..a68cb7c79d8b 100644
--- a/arch/x86/include/asm/perf_event.h
+++ b/arch/x86/include/asm/perf_event.h
@@ -308,6 +308,7 @@ struct x86_pmu_capability {
int events_mask_len;
unsigned int pebs_ept :1;
unsigned int mediated :1;
+ u64 perf_capabilities;
};
/*
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 05/18] perf/x86: Expose number of Topdown metric events to KVM
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (3 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 04/18] perf/x86: Expose perf_capabilities to KVM Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 06/18] KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events Zide Chen
` (12 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
The 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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Cc: Peter Zijlstra <peterz@infradead.org>
---
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(+)
diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
index 770ca960fa6e..ea3d7e437aa9 100644
--- a/arch/x86/events/core.c
+++ b/arch/x86/events/core.c
@@ -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);
diff --git a/arch/x86/include/asm/perf_event.h b/arch/x86/include/asm/perf_event.h
index a68cb7c79d8b..19e35a68f32b 100644
--- a/arch/x86/include/asm/perf_event.h
+++ b/arch/x86/include/asm/perf_event.h
@@ -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;
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 06/18] KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (4 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 05/18] perf/x86: Expose number of Topdown metric events " Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 07/18] KVM: x86/pmu: Query perf-reported capabilities instead of RDMSR Zide Chen
` (11 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
Only 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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
---
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(-)
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index 70a8c4816135..8f72a405e737 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -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
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 07/18] KVM: x86/pmu: Query perf-reported capabilities instead of RDMSR
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (5 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 06/18] KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 08/18] KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h Zide Chen
` (10 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
KVM 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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
---
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(-)
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index 8f72a405e737..f916048b9c54 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -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;
/*
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index b341af4c6057..f04f2d3884d9 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -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
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 08/18] KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (6 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 07/18] KVM: x86/pmu: Query perf-reported capabilities instead of RDMSR Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 09/18] KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters Zide Chen
` (9 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
Move 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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
---
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(-)
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 090c9bbb74f4..2a9c1bbe5c40 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -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);
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index f916048b9c54..36a2269bbd96 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -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));
diff --git a/arch/x86/kvm/vmx/pmu_intel.h b/arch/x86/kvm/vmx/pmu_intel.h
index 5d9357640aa1..9f4452619da5 100644
--- a/arch/x86/kvm/vmx/pmu_intel.h
+++ b/arch/x86/kvm/vmx/pmu_intel.h
@@ -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);
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 09/18] KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (7 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 08/18] KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 10/18] KVM: x86/pmu: Block PMU passthrough if guest lacks PERF_METRICS Zide Chen
` (8 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
In 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.
Suggested-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Signed-off-by: Zide Chen <zide.chen@intel.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
---
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(-)
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index eef2f792dc9e..dba651d4e449 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -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)
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 10/18] KVM: x86/pmu: Block PMU passthrough if guest lacks PERF_METRICS
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (8 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 09/18] KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 11/18] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU Zide Chen
` (7 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
On 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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
---
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(-)
diff --git a/arch/x86/include/asm/msr-index.h b/arch/x86/include/asm/msr-index.h
index 3a8e51a0c9e8..8fad944197fb 100644
--- a/arch/x86/include/asm/msr-index.h
+++ b/arch/x86/include/asm/msr-index.h
@@ -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 | \
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index dba651d4e449..4574cac5f148 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -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);
}
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 2a9c1bbe5c40..ae1e6545df40 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -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);
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 11/18] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (9 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 10/18] KVM: x86/pmu: Block PMU passthrough if guest lacks PERF_METRICS Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 12/18] KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks Zide Chen
` (6 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
From: Dapeng Mi <dapeng1.mi@linux.intel.com>
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.
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Co-developed-by: Zide Chen <zide.chen@intel.com>
Signed-off-by: Zide Chen <zide.chen@intel.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
---
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(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index e2eef7057bd3..aad835338759 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -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)
diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c
index e91650af44c2..1ccea561f2e0 100644
--- a/arch/x86/kvm/msrs.c
+++ b/arch/x86/kvm/msrs.c
@@ -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;
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index 4574cac5f148..45721c6fa92b 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -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 =
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index ae1e6545df40..1d7cbe89fa95 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -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,
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 12/18] KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (10 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 11/18] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 13/18] KVM: x86/pmu: Emulate RDPMC on performance metrics Zide Chen
` (5 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
The 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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
Reviewed-by: Sandipan Das <sandipan.das@amd.com>
---
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(-)
diff --git a/arch/x86/include/asm/kvm-x86-pmu-ops.h b/arch/x86/include/asm/kvm-x86-pmu-ops.h
index 4a223c2793e3..4b50ed058aed 100644
--- a/arch/x86/include/asm/kvm-x86-pmu-ops.h
+++ b/arch/x86/include/asm/kvm-x86-pmu-ops.h
@@ -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)
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index 45721c6fa92b..d4fd1d9d5d93 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -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)
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 1d7cbe89fa95..40395d271392 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -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);
diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index c18286545a7a..6f9a190a6905 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -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,
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index 36a2269bbd96..858737504ec1 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -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,
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 13/18] KVM: x86/pmu: Emulate RDPMC on performance metrics
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (11 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 12/18] KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 14/18] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU Zide Chen
` (4 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
If 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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
Reviewed-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Reviewed-by: Jim Mattson <jmattson@google.com>
---
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(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index aad835338759..1261f8302dc8 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -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];
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index 858737504ec1..cb7db4784545 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -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;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 14/18] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (12 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 13/18] KVM: x86/pmu: Emulate RDPMC on performance metrics Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 15/18] KVM: x86/pmu: Support RDPMC Metrics Clear Mode Zide Chen
` (3 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
From: Dapeng Mi <dapeng1.mi@linux.intel.com>
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.
Signed-off-by: Dapeng Mi <dapeng1.mi@linux.intel.com>
Co-developed-by: Zide Chen <zide.chen@intel.com>
Signed-off-by: Zide Chen <zide.chen@intel.com>
---
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(-)
diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 1261f8302dc8..9d9b7aa38043 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -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];
diff --git a/arch/x86/kvm/msrs.c b/arch/x86/kvm/msrs.c
index 1ccea561f2e0..26eb853baab5 100644
--- a/arch/x86/kvm/msrs.c
+++ b/arch/x86/kvm/msrs.c
@@ -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 >=
diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
index d4fd1d9d5d93..93be6ac96a1c 100644
--- a/arch/x86/kvm/pmu.c
+++ b/arch/x86/kvm/pmu.c
@@ -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)
diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
index 40395d271392..a82f6a9b4cc9 100644
--- a/arch/x86/kvm/pmu.h
+++ b/arch/x86/kvm/pmu.h
@@ -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);
diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 9b0bfa2f854c..2b36088ee056 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -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);
}
/*
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index cb7db4784545..da6dae261514 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -207,6 +207,8 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
case MSR_CORE_PERF_FIXED_CTR_CTRL:
return kvm_pmu_has_perf_global_ctrl(pmu);
+ case MSR_PERF_METRICS:
+ return kvm_vcpu_has_perf_metrics(vcpu);
case MSR_IA32_PEBS_ENABLE:
ret = kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_PEBS_FORMAT;
break;
@@ -364,6 +366,9 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
case MSR_CORE_PERF_FIXED_CTR_CTRL:
msr_info->data = pmu->fixed_ctr_ctrl;
break;
+ case MSR_PERF_METRICS:
+ msr_info->data = pmu->perf_metrics;
+ break;
case MSR_IA32_PEBS_ENABLE:
msr_info->data = pmu->pebs_enable;
break;
@@ -413,6 +418,12 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
if (pmu->fixed_ctr_ctrl != data)
reprogram_fixed_counters(pmu, data);
break;
+ case MSR_PERF_METRICS:
+ if (data & pmu->perf_metrics_rsvd)
+ return 1;
+
+ pmu->perf_metrics = data;
+ break;
case MSR_IA32_PEBS_ENABLE:
if (data & pmu->pebs_enable_rsvd)
return 1;
@@ -519,6 +530,30 @@ static void intel_pmu_enable_fixed_counter_bits(struct kvm_pmu *pmu, u64 bits)
pmu->fixed_ctr_ctrl_rsvd &= ~intel_fixed_bits_by_idx(i, bits);
}
+static void intel_pmu_refresh_perf_metrics(struct kvm_vcpu *vcpu)
+{
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+
+ if (!kvm_vcpu_has_perf_metrics(vcpu))
+ return;
+
+ /*
+ * Per the SDM, setting GLOBAL_CTRL[48] is allowed as long as
+ * PERF_CAP_PERF_METRICS is present. However, hardware metrics remain
+ * inactive without enabling fixed counter 3.
+ */
+ pmu->global_ctrl_rsvd &= ~GLOBAL_CTRL_EN_PERF_METRICS;
+
+ if (!kvm_pmu_has_fixed_counter(pmu, KVM_FIXED_SLOTS_IDX))
+ return;
+
+ if (kvm_pmu_cap.num_topdown_events) {
+ pmu->perf_metrics_rsvd =
+ ~GENMASK_ULL(kvm_pmu_cap.num_topdown_events *
+ INTEL_TD_METRIC_FIELD_BITS - 1, 0);
+ }
+}
+
static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -597,6 +632,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
pmu->global_ctrl_rsvd = counter_rsvd;
+ intel_pmu_refresh_perf_metrics(vcpu);
+
/*
* GLOBAL_STATUS and GLOBAL_OVF_CONTROL (a.k.a. GLOBAL_STATUS_RESET)
* share reserved bit definitions. The kernel just happens to use
@@ -652,6 +689,9 @@ static void intel_pmu_init(struct kvm_vcpu *vcpu)
static void intel_pmu_reset(struct kvm_vcpu *vcpu)
{
+ struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+
+ pmu->perf_metrics = 0;
intel_pmu_release_guest_lbr_event(vcpu);
}
@@ -817,6 +857,15 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
u64 global_status, toggle;
+ /*
+ * Without fixed counter 3, Topdown metrics cannot count in hardware.
+ * Thus, the guest metric state is always zero and no context switch
+ * is needed.
+ */
+ if (kvm_vcpu_can_enable_perf_metrics(vcpu) &&
+ pmu->perf_metrics != rdpmc(INTEL_PMC_FIXED_RDPMC_METRICS))
+ wrmsrq(MSR_PERF_METRICS, pmu->perf_metrics);
+
rdmsrq(MSR_CORE_PERF_GLOBAL_STATUS, global_status);
toggle = pmu->global_status ^ global_status;
if (global_status & toggle)
@@ -845,6 +894,17 @@ static void intel_mediated_pmu_put(struct kvm_vcpu *vcpu)
*/
if (pmu->fixed_ctr_ctrl_hw)
wrmsrq(MSR_CORE_PERF_FIXED_CTR_CTRL, 0);
+
+ if (kvm_vcpu_can_enable_perf_metrics(vcpu)) {
+ pmu->perf_metrics = rdpmc(INTEL_PMC_FIXED_RDPMC_METRICS);
+ /*
+ * The SDM requires restoring fixed counter 3 before
+ * PERF_METRICS, but order doesn't matter when both are being
+ * zeroed.
+ */
+ if (pmu->perf_metrics)
+ wrmsrq(MSR_PERF_METRICS, 0);
+ }
}
struct kvm_pmu_ops intel_pmu_ops __initdata = {
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index f04f2d3884d9..7441808be049 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -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;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 15/18] KVM: x86/pmu: Support RDPMC Metrics Clear Mode
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (13 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 14/18] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:38 ` [PATCH v10 16/18] KVM: selftests: Rework PMU guest measurement asm operands Zide Chen
` (2 subsequent siblings)
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
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 <zide.chen@intel.com>
---
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
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 16/18] KVM: selftests: Rework PMU guest measurement asm operands
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (14 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 15/18] KVM: x86/pmu: Support RDPMC Metrics Clear Mode Zide Chen
@ 2026-11-24 21:38 ` Zide Chen
2026-11-24 21:39 ` [PATCH v10 17/18] KVM: selftests: Move and generalize PMU guest measurement helpers Zide Chen
2026-11-24 21:39 ` [PATCH v10 18/18] KVM: selftests: Add Topdown PERF_METRICS test Zide Chen
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:38 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
GUEST_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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
---
v10: new patch.
---
.../selftests/kvm/x86/pmu_counters_test.c | 81 ++++++++++---------
1 file changed, 45 insertions(+), 36 deletions(-)
diff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
index c3e784e16348..5ae7f2d15d04 100644
--- a/tools/testing/selftests/kvm/x86/pmu_counters_test.c
+++ b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
@@ -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,
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 17/18] KVM: selftests: Move and generalize PMU guest measurement helpers
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (15 preceding siblings ...)
2026-11-24 21:38 ` [PATCH v10 16/18] KVM: selftests: Rework PMU guest measurement asm operands Zide Chen
@ 2026-11-24 21:39 ` Zide Chen
2026-11-24 21:39 ` [PATCH v10 18/18] KVM: selftests: Add Topdown PERF_METRICS test Zide Chen
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:39 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
Move 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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
---
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(-)
diff --git a/tools/testing/selftests/kvm/include/x86/pmu.h b/tools/testing/selftests/kvm/include/x86/pmu.h
index 608ed83d7c6a..c06ac78db01f 100644
--- a/tools/testing/selftests/kvm/include/x86/pmu.h
+++ b/tools/testing/selftests/kvm/include/x86/pmu.h
@@ -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 */
diff --git a/tools/testing/selftests/kvm/x86/pmu_counters_test.c b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
index 5ae7f2d15d04..4836745bef7f 100644
--- a/tools/testing/selftests/kvm/x86/pmu_counters_test.c
+++ b/tools/testing/selftests/kvm/x86/pmu_counters_test.c
@@ -219,61 +219,10 @@ static void guest_assert_event_count(u8 idx, u32 pmc, u32 pmc_msr)
GUEST_ASSERT_EQ(_rdpmc(pmc), 0xdead);
}
-/*
- * 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_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_RUN_PAYLOAD(_ctrl_msr, _value, FEP, NUM_LOOPS); \
guest_assert_event_count(_idx, _pmc, _pmc_msr); \
} while (0)
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
* [PATCH v10 18/18] KVM: selftests: Add Topdown PERF_METRICS test
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
` (16 preceding siblings ...)
2026-11-24 21:39 ` [PATCH v10 17/18] KVM: selftests: Move and generalize PMU guest measurement helpers Zide Chen
@ 2026-11-24 21:39 ` Zide Chen
17 siblings, 0 replies; 19+ messages in thread
From: Zide Chen @ 2026-11-24 21:39 UTC (permalink / raw)
To: Sean Christopherson, Paolo Bonzini, Peter Zijlstra
Cc: kvm, Jim Mattson, Yosry Ahmed, Andi Kleen, Stephane Eranian,
linux-kernel, Mingwei Zhang, Zide Chen, Das Sandipan,
Shukla Manali, Dapeng Mi, Xudong Hao
Add 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.
Signed-off-by: Zide Chen <zide.chen@intel.com>
---
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
diff --git a/tools/arch/x86/include/asm/msr-index.h b/tools/arch/x86/include/asm/msr-index.h
index 18c4be75e927..851f5c6b9ade 100644
--- a/tools/arch/x86/include/asm/msr-index.h
+++ b/tools/arch/x86/include/asm/msr-index.h
@@ -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)
diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm
index be5eb2dd85aa..71e0b36089e2 100644
--- a/tools/testing/selftests/kvm/Makefile.kvm
+++ b/tools/testing/selftests/kvm/Makefile.kvm
@@ -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
diff --git a/tools/testing/selftests/kvm/include/x86/pmu.h b/tools/testing/selftests/kvm/include/x86/pmu.h
index c06ac78db01f..8ffb26a1d824 100644
--- a/tools/testing/selftests/kvm/include/x86/pmu.h
+++ b/tools/testing/selftests/kvm/include/x86/pmu.h
@@ -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))
diff --git a/tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c b/tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c
new file mode 100644
index 000000000000..07a6c948e4cd
--- /dev/null
+++ b/tools/testing/selftests/kvm/x86/pmu_perf_metrics_test.c
@@ -0,0 +1,391 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Test for KVM Topdown Microarchitecture Analysis (TMA) and PERF_METRICS.
+ *
+ * Copyright (C) 2026, Intel Corporation
+ */
+
+#include <linux/bitfield.h>
+
+#include "processor.h"
+#include "pmu.h"
+
+/* Number of iterations of the loop for the guest measurement payload. */
+#define NUM_LOOPS 10
+#define PERF_METRICS_TEST_VAL 0x12345678ULL
+#define SLOTS_TEST_VAL 0x12345ULL
+
+static void guest_test_rdpmc(u32 rdpmc_idx, bool expect_success, u64 expected_val)
+{
+ u8 vector;
+ u64 val;
+
+ vector = rdpmc_safe(rdpmc_idx, &val);
+ __GUEST_ASSERT(expect_success ? !vector : vector == GP_VECTOR,
+ "Expected %s on RDPMC(0x%x), got %s",
+ expect_success ? "no fault" : "#GP", rdpmc_idx, ex_str(vector));
+ if (expect_success)
+ GUEST_ASSERT_EQ(val, expected_val);
+
+ if (!is_forced_emulation_enabled)
+ return;
+
+ vector = rdpmc_safe_fep(rdpmc_idx, &val);
+ __GUEST_ASSERT(expect_success ? !vector : vector == GP_VECTOR,
+ "Expected %s on FEP RDPMC(0x%x), got %s",
+ expect_success ? "no fault" : "#GP", rdpmc_idx, ex_str(vector));
+ if (expect_success)
+ GUEST_ASSERT_EQ(val, expected_val);
+}
+
+static void assert_rdpmc_metrics_cleared(u8 vector, u64 val)
+{
+ __GUEST_ASSERT(!vector, "Expected no fault on RDPMC(0x%llx), got %s",
+ INTEL_RDPMC_METRICS, ex_str(vector));
+ GUEST_ASSERT_EQ(val, PERF_METRICS_TEST_VAL);
+ GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), 0);
+ GUEST_ASSERT_EQ(rdmsr(MSR_CORE_PERF_FIXED_CTR3), 0);
+}
+
+static void guest_test_no_perf_metrics(void)
+{
+ u64 metrics;
+ u8 vector;
+
+ /*
+ * When PERF_METRICS is not exposed to the guest, all accesses
+ * must fault (#GP): MSR_PERF_METRICS, GLOBAL_CTRL bit 48,
+ * and RDPMC of metrics type.
+ */
+ vector = rdmsr_safe(MSR_PERF_METRICS, &metrics);
+ GUEST_ASSERT_EQ(vector, GP_VECTOR);
+
+ vector = wrmsr_safe(MSR_PERF_METRICS, 0);
+ GUEST_ASSERT_EQ(vector, GP_VECTOR);
+
+ vector = wrmsr_safe(MSR_CORE_PERF_GLOBAL_CTRL,
+ GLOBAL_CTRL_EN_PERF_METRICS);
+ GUEST_ASSERT_EQ(vector, GP_VECTOR);
+
+ guest_test_rdpmc(INTEL_RDPMC_METRICS, false, 0);
+
+ /*
+ * If RDPMC Metrics Clear capability is set without PERF_METRICS,
+ * setting METRICS_CLEAR_EN in FIXED_CTR_CTRL must still #GP.
+ */
+ if (this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS) > FIXED_PMC_SLOTS_IDX) {
+ vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR_CTRL,
+ FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX,
+ FIXED_PMC_KERNEL | FIXED_PMC_METRICS_CLEAR));
+ GUEST_ASSERT_EQ(vector, GP_VECTOR);
+ }
+}
+
+static void guest_test_no_slots_counter(void)
+{
+ u8 vector;
+
+ /*
+ * Without fixed counter 3, all metric fields are reserved: reads
+ * return 0, writing 0 succeeds, and non-zero writes #GP.
+ */
+ GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), 0);
+
+ vector = wrmsr_safe(MSR_PERF_METRICS, 0);
+ GUEST_ASSERT_EQ(vector, 0);
+
+ vector = wrmsr_safe(MSR_PERF_METRICS, PERF_METRICS_TEST_VAL);
+ GUEST_ASSERT_EQ(vector, GP_VECTOR);
+
+ vector = wrmsr_safe(MSR_CORE_PERF_GLOBAL_CTRL,
+ GLOBAL_CTRL_EN_PERF_METRICS);
+ GUEST_ASSERT_EQ(vector, 0);
+
+ vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR3, 0);
+ GUEST_ASSERT_EQ(vector, GP_VECTOR);
+
+ vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR_CTRL,
+ FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX, FIXED_PMC_METRICS_CLEAR));
+ GUEST_ASSERT_EQ(vector, GP_VECTOR);
+
+ guest_test_rdpmc(INTEL_RDPMC_METRICS, true, 0);
+}
+
+static void guest_test_full_perf_metrics(void)
+{
+ int retiring, bad_spec, fe_bound, be_bound, sum, loops;
+ u64 global_ctrl, metrics, val;
+ u8 vector;
+
+ wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+ wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0);
+ wrmsr(MSR_PERF_METRICS, 0);
+
+ /* Enable fixed counter 3 (TOPDOWN.SLOTS) and PERF_METRICS. */
+ wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL,
+ FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX, FIXED_PMC_KERNEL));
+ global_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(FIXED_PMC_SLOTS_IDX) |
+ GLOBAL_CTRL_EN_PERF_METRICS;
+
+ GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, "", NUM_LOOPS);
+
+ /* Check test results. */
+ metrics = rdmsr(MSR_PERF_METRICS);
+ vector = rdpmc_safe(INTEL_RDPMC_METRICS, &val);
+ GUEST_ASSERT_EQ(vector, 0);
+ GUEST_ASSERT_EQ(val, metrics);
+
+ retiring = FIELD_GET(GENMASK_ULL(7, 0), metrics);
+ bad_spec = FIELD_GET(GENMASK_ULL(15, 8), metrics);
+ fe_bound = FIELD_GET(GENMASK_ULL(23, 16), metrics);
+ be_bound = FIELD_GET(GENMASK_ULL(31, 24), metrics);
+
+ /*
+ * Be conservative: the measured payload definitely retires work, so
+ * Retiring should be non-zero.
+ */
+ GUEST_ASSERT_NE(retiring, 0);
+
+ /*
+ * Each level-1 Topdown metric is an integer fraction of 0xff.
+ * A +/-3 error margin is chosen for a loose sanity check.
+ */
+ sum = retiring + bad_spec + fe_bound + be_bound;
+ GUEST_ASSERT(sum >= 0xfc && sum <= 0x102);
+
+ /*
+ * Burn some cycles; MSR_CORE_PERF_GLOBAL_CTRL is left zeroed after
+ * GUEST_RUN_PAYLOAD() returns, so PERF_METRICS is not counting.
+ */
+ loops = NUM_LOOPS;
+
+ __asm__ __volatile__("loop ." : "+c"(loops));
+
+ /* Confirm PERF_METRICS did not advance while counting was disabled. */
+ GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), metrics);
+
+ /*
+ * Only use the 4 level-1 metric fields (bits [31:0]), which are present
+ * whether the CPU supports 4 or 8 Topdown events.
+ */
+ wrmsr(MSR_PERF_METRICS, PERF_METRICS_TEST_VAL);
+
+ /* Force a VM-exit to verify KVM restores MSR_PERF_METRICS on VM-entry. */
+ GUEST_SYNC(0);
+ GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), PERF_METRICS_TEST_VAL);
+
+ guest_test_rdpmc(INTEL_RDPMC_METRICS, true, PERF_METRICS_TEST_VAL);
+
+ /*
+ * Test RDPMC Metrics Clear Mode if supported, or verify that setting
+ * METRICS_CLEAR_EN injects #GP if the capability is absent.
+ */
+ if (rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_RDPMC_METRICS_CLEAR) {
+ wrmsr(MSR_CORE_PERF_FIXED_CTR3, SLOTS_TEST_VAL);
+ wrmsr(MSR_PERF_METRICS, PERF_METRICS_TEST_VAL);
+
+ /* Enable Metrics Clear Mode for fixed counter 3. */
+ wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL,
+ FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX,
+ FIXED_PMC_KERNEL | FIXED_PMC_METRICS_CLEAR));
+
+ /* Confirm RDMSR does not clear either register. */
+ GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), PERF_METRICS_TEST_VAL);
+ GUEST_ASSERT_EQ(rdmsr(MSR_CORE_PERF_FIXED_CTR3), SLOTS_TEST_VAL);
+
+ /* RDPMC of PERF_METRICS must return the value and clear both. */
+ vector = rdpmc_safe(INTEL_RDPMC_METRICS, &val);
+ assert_rdpmc_metrics_cleared(vector, val);
+
+ if (is_forced_emulation_enabled) {
+ wrmsr(MSR_CORE_PERF_FIXED_CTR3, SLOTS_TEST_VAL);
+ wrmsr(MSR_PERF_METRICS, PERF_METRICS_TEST_VAL);
+ vector = rdpmc_safe_fep(INTEL_RDPMC_METRICS, &val);
+ assert_rdpmc_metrics_cleared(vector, val);
+ }
+
+ /* Restore FIXED_CTR_CTRL to normal counting. */
+ wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL,
+ FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX, FIXED_PMC_KERNEL));
+ } else {
+ vector = wrmsr_safe(MSR_CORE_PERF_FIXED_CTR_CTRL,
+ FIXED_PMC_CTRL(FIXED_PMC_SLOTS_IDX,
+ FIXED_PMC_KERNEL | FIXED_PMC_METRICS_CLEAR));
+ GUEST_ASSERT_EQ(vector, GP_VECTOR);
+ }
+}
+
+static void guest_test_perf_metrics(void)
+{
+ if (!(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS))
+ guest_test_no_perf_metrics();
+ else if (this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS) <= FIXED_PMC_SLOTS_IDX)
+ guest_test_no_slots_counter();
+ else
+ guest_test_full_perf_metrics();
+
+ GUEST_DONE();
+}
+
+static void run_vcpu(struct kvm_vcpu *vcpu)
+{
+ struct ucall uc;
+
+ do {
+ vcpu_run(vcpu);
+ switch (get_ucall(vcpu, &uc)) {
+ case UCALL_SYNC:
+ break;
+ case UCALL_ABORT:
+ REPORT_GUEST_ASSERT(uc);
+ break;
+ case UCALL_PRINTF:
+ pr_info("%s", uc.buffer);
+ break;
+ case UCALL_DONE:
+ break;
+ default:
+ TEST_FAIL("Unexpected ucall: %lu", uc.cmd);
+ }
+ } while (uc.cmd != UCALL_DONE);
+}
+
+static void test_one_perf_metrics(u8 pmu_version, u64 perf_capabilities,
+ u8 nr_fixed_counters)
+{
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+
+ vm = vm_create_with_one_vcpu(&vcpu, guest_test_perf_metrics);
+
+ vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, perf_capabilities);
+
+ vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_VERSION, pmu_version);
+ vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS, nr_fixed_counters);
+
+ run_vcpu(vcpu);
+
+ kvm_vm_free(vm);
+}
+
+static void test_perf_metrics(u8 pmu_version, u64 perf_capabilities)
+{
+ u8 nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
+
+ pr_info("Testing Perf Metrics, PMU version %u, perf_caps = 0x%lx\n",
+ pmu_version, perf_capabilities);
+
+ /* Test with full/supported fixed counters (fixed counter 3 present if supported). */
+ test_one_perf_metrics(pmu_version, perf_capabilities, nr_fixed_counters);
+
+ /* Test with fixed counter 3 absent. */
+ if (nr_fixed_counters > FIXED_PMC_SLOTS_IDX)
+ test_one_perf_metrics(pmu_version, perf_capabilities, FIXED_PMC_SLOTS_IDX);
+}
+
+static void test_guest_perf_metrics(void)
+{
+ u8 pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION);
+ const u64 perf_caps[] = {
+ 0,
+ PERF_CAP_PERF_METRICS,
+ PERF_CAP_PERF_METRICS | PERF_CAP_RDPMC_METRICS_CLEAR,
+ PERF_CAP_RDPMC_METRICS_CLEAR,
+ };
+ u64 advertised_perf_caps;
+ u8 max_pmu_version;
+ unsigned int i;
+ u8 v;
+
+ max_pmu_version = max_t(typeof(pmu_version), pmu_version, 5);
+ advertised_perf_caps = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
+
+ for (v = 2; v <= max_pmu_version; v++) {
+ for (i = 0; i < ARRAY_SIZE(perf_caps); i++) {
+ /* Ignore unsupported features. */
+ if (perf_caps[i] & ~advertised_perf_caps)
+ continue;
+
+ test_perf_metrics(v, perf_caps[i]);
+ }
+ }
+}
+
+static void test_host_perf_metrics(void)
+{
+ u64 advertised_perf_caps;
+ struct kvm_vcpu *vcpu;
+ struct kvm_vm *vm;
+ u64 val;
+ int ret;
+
+ advertised_perf_caps = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
+ if (!(advertised_perf_caps & PERF_CAP_PERF_METRICS))
+ return;
+
+ vm = vm_create_with_one_vcpu(&vcpu, NULL);
+ vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, advertised_perf_caps);
+ vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_VERSION, 2);
+
+ /*
+ * Case 1: Fixed counter 3 present and PERF_METRICS exposed.
+ * Host can write 0 and valid non-zero values to MSR_PERF_METRICS.
+ */
+ vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS,
+ FIXED_PMC_SLOTS_IDX + 1);
+
+ ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, PERF_METRICS_TEST_VAL);
+ TEST_ASSERT_EQ(ret, 1);
+ val = vcpu_get_msr(vcpu, MSR_PERF_METRICS);
+ TEST_ASSERT_EQ(val, PERF_METRICS_TEST_VAL);
+
+ ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, 0);
+ TEST_ASSERT_EQ(ret, 1);
+ val = vcpu_get_msr(vcpu, MSR_PERF_METRICS);
+ TEST_ASSERT_EQ(val, 0);
+
+ /*
+ * Case 2: Fixed counter 3 absent.
+ * Host writing 0 succeeds (for migration/probe), non-zero fails.
+ */
+ vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS,
+ FIXED_PMC_SLOTS_IDX);
+
+ ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, PERF_METRICS_TEST_VAL);
+ TEST_ASSERT_EQ(ret, 0);
+
+ ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, 0);
+ TEST_ASSERT_EQ(ret, 1);
+
+ /*
+ * Case 3: PERF_CAP_PERF_METRICS not exposed (even with fixed counter 3 present).
+ * Non-zero writes must fail. Writing 0 succeeds as KVM allows
+ * writing 0 to advertised feature MSRs for compatibility.
+ */
+ vcpu_set_cpuid_property(vcpu, X86_PROPERTY_PMU_NR_FIXED_COUNTERS,
+ FIXED_PMC_SLOTS_IDX + 1);
+ vcpu_set_msr(vcpu, MSR_IA32_PERF_CAPABILITIES, 0);
+
+ ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, PERF_METRICS_TEST_VAL);
+ TEST_ASSERT_EQ(ret, 0);
+
+ ret = _vcpu_set_msr(vcpu, MSR_PERF_METRICS, 0);
+ TEST_ASSERT_EQ(ret, 1);
+
+ kvm_vm_free(vm);
+}
+
+int main(int argc, char *argv[])
+{
+ TEST_REQUIRE(kvm_is_pmu_enabled());
+
+ TEST_REQUIRE(host_cpu_is_intel);
+ TEST_REQUIRE(kvm_cpu_has_p(X86_PROPERTY_PMU_VERSION));
+ TEST_REQUIRE(kvm_cpu_property(X86_PROPERTY_PMU_VERSION) >= 2);
+ TEST_REQUIRE(kvm_cpu_has(X86_FEATURE_PDCM));
+
+ test_host_perf_metrics();
+ test_guest_perf_metrics();
+
+ return 0;
+}
--
2.56.1
^ permalink raw reply related [flat|nested] 19+ messages in thread
end of thread, other threads:[~2026-11-24 21:53 UTC | newest]
Thread overview: 19+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 21:38 [PATCH v10 00/18] KVM: x86/pmu: Add hardware Topdown metrics support Zide Chen
2026-11-24 21:38 ` [PATCH v10 01/18] perf/x86: Add INTEL_TD_METRIC_FIELD_{BITS,MASK} constants Zide Chen
2026-11-24 21:38 ` [PATCH v10 02/18] perf/x86/intel: Do not inflate capabilities for guests Zide Chen
2026-11-24 21:38 ` [PATCH v10 03/18] perf/x86/intel: Track common hybrid capabilities Zide Chen
2026-11-24 21:38 ` [PATCH v10 04/18] perf/x86: Expose perf_capabilities to KVM Zide Chen
2026-11-24 21:38 ` [PATCH v10 05/18] perf/x86: Expose number of Topdown metric events " Zide Chen
2026-11-24 21:38 ` [PATCH v10 06/18] KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events Zide Chen
2026-11-24 21:38 ` [PATCH v10 07/18] KVM: x86/pmu: Query perf-reported capabilities instead of RDMSR Zide Chen
2026-11-24 21:38 ` [PATCH v10 08/18] KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h Zide Chen
2026-11-24 21:38 ` [PATCH v10 09/18] KVM: x86/pmu: Mask only OS/USR bits when filtering fixed counters Zide Chen
2026-11-24 21:38 ` [PATCH v10 10/18] KVM: x86/pmu: Block PMU passthrough if guest lacks PERF_METRICS Zide Chen
2026-11-24 21:38 ` [PATCH v10 11/18] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU Zide Chen
2026-11-24 21:38 ` [PATCH v10 12/18] KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks Zide Chen
2026-11-24 21:38 ` [PATCH v10 13/18] KVM: x86/pmu: Emulate RDPMC on performance metrics Zide Chen
2026-11-24 21:38 ` [PATCH v10 14/18] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU Zide Chen
2026-11-24 21:38 ` [PATCH v10 15/18] KVM: x86/pmu: Support RDPMC Metrics Clear Mode Zide Chen
2026-11-24 21:38 ` [PATCH v10 16/18] KVM: selftests: Rework PMU guest measurement asm operands Zide Chen
2026-11-24 21:39 ` [PATCH v10 17/18] KVM: selftests: Move and generalize PMU guest measurement helpers Zide Chen
2026-11-24 21:39 ` [PATCH v10 18/18] KVM: selftests: Add Topdown PERF_METRICS test Zide Chen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox