[PATCH v10 13/18] KVM: x86/pmu: Emulate RDPMC on performance metrics
From: Zide Chen
Date: Tue Nov 24 2026 - 16:54:26 EST
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@xxxxxxxxx>
Reviewed-by: Dapeng Mi <dapeng1.mi@xxxxxxxxxxxxxxx>
Reviewed-by: Jim Mattson <jmattson@xxxxxxxxxx>
---
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