Kernel KVM virtualization development
 help / color / mirror / Atom feed
From: Zide Chen <zide.chen@intel.com>
To: Sean Christopherson <seanjc@google.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Peter Zijlstra <peterz@infradead.org>
Cc: kvm@vger.kernel.org, Jim Mattson <jmattson@google.com>,
	Yosry Ahmed <yosry@kernel.org>, Andi Kleen <ak@linux.intel.com>,
	Stephane Eranian <eranian@google.com>,
	linux-kernel@vger.kernel.org, Mingwei Zhang <mizhang@google.com>,
	Zide Chen <zide.chen@intel.com>,
	Das Sandipan <Sandipan.Das@amd.com>,
	Shukla Manali <Manali.Shukla@amd.com>,
	Dapeng Mi <dapeng1.mi@linux.intel.com>,
	Xudong Hao <xudong.hao@intel.com>
Subject: [PATCH v10 14/18] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU
Date: Tue, 24 Nov 2026 14:38:57 -0700	[thread overview]
Message-ID: <20261124213901.120572-15-zide.chen@intel.com> (raw)
In-Reply-To: <20261124213901.120572-1-zide.chen@intel.com>

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


  parent reply	other threads:[~2026-11-24 21:53 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 ` Zide Chen [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261124213901.120572-15-zide.chen@intel.com \
    --to=zide.chen@intel.com \
    --cc=Manali.Shukla@amd.com \
    --cc=Sandipan.Das@amd.com \
    --cc=ak@linux.intel.com \
    --cc=dapeng1.mi@linux.intel.com \
    --cc=eranian@google.com \
    --cc=jmattson@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mizhang@google.com \
    --cc=pbonzini@redhat.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.com \
    --cc=xudong.hao@intel.com \
    --cc=yosry@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox