Skip to contentSkip to conversation

KVM: x86/pmu: Add hardware Topdown metrics support

PATCH Superseded Zide Chen 4 participants 54 messages Jun 29 – Jul 27

Subsystems (5)
x86 KVM Kselftest perf events +1 more subsystems

v6 of 9 Superseded Latest is v9 →

lore
Share & tools
lore

Zide Chen

author
#0 lore raw

v6 cover KVM: x86/pmu: Add hardware Topdown metrics support

The Top-Down Microarchitecture Analysis (TMA) method is a structured approach for identifying performance bottlenecks in out-of-order processors.

Currently, guests support the TMA method by collecting Topdown events using GP counters, which may trigger multiplexing. To free up scarce GP counters, eliminate multiplexing-induced skew, and obtain coherent Topdown metric ratios, it is desirable to expose fixed counter 3 and the IA32_PERF_METRICS MSR to guests.

Several attempts have been made to virtualize this under the legacy vPMU model [1][2][3], but they were unsuccessful. With the new mediated vPMU, enabling TMA support in guests becomes much simpler. It avoids invasive changes to the perf core, eliminates CPU pinning and fixed-counter affinity issues, and reduces the large overhead of trapping and emulating MSR accesses.

[1] https://lore.kernel.org/kvm/[email protected]/
[2] https://lore.kernel.org/all/[email protected]/T/
[3] https://lwn.net/ml/linux-kernel/[email protected]/

Tested on an Sapphire Rapids. Without this series, only raw topdown.*_slots events work in the guest, and metric events (e.g. cpu/topdown-bad-spec/) are not available.

With this series, metric events are visible in the guest. Run this command on both host and guest:

$ perf stat --topdown --no-metric-only -- taskset -c 2 perf bench sched messaging

Host results:

# Running 'sched/messaging' benchmark:
# 20 sender and receiver processes per group
# 10 groups == 400 processes run

     Total time: 1.500 [sec]

Performance counter stats for 'taskset -c 2 perf bench sched messaging':

     4,266,060,558      TOPDOWN.SLOTS:u              #     32.0 %  tma_frontend_bound
                                                     #      5.2 %  tma_bad_speculation
       588,397,905      topdown-retiring:u           #     13.8 %  tma_retiring
                                                     #     49.0 %  tma_backend_bound
     1,376,283,990      topdown-fe-bound:u
     2,096,827,304      topdown-be-bound:u
       217,425,841      topdown-bad-spec:u
         5,050,520      INT_MISC.UOP_DROPPING:u

Rebased to kvm-x86/next: 50406d35f563

v6 changes:
- patch 6/8: New patch to refactor rdpmc emulation code.
- patch 7/8: More strict handling of RDPMC ECX argument.
- patch 8/8: Move perf metrics out of test_arch_events().
- patch 2/8: Minor fix of comments.
v5 changes:
- patch 3,5,6/7: new patches to handle RDPMC on metrics.
- patch 6/7: remove host_initiated check.
v4 changes:
- patch 3/4: Remove WARN_ON_ONCE() and simply reject the guest accesses by checking host_initiated. (Sashiko)
- patch 3/4: Passthru MSR_PERF_METRICS only if has_mediated_pmu is true. (Sashiko)
v3 changes:
- patch 2/4: Move the non-contiguous counter filter code to pmu.c (Dapeng)
- patch 3/4: Replace WARN_ON() with WARN_ON_ONCE(). (Dapeng)
- patch 4/4: Change abs() with explicit bounds (sum >= 0xfd && sum <= 0x102).
- Minor comment cleanups.

v2 changes:
- As suggested by Dapeng, implement a new selftest patch.
- Don't advertise fixed counter 3 if the host doesn't support it.
- Minor change in patch 1 to remove a magic number.

v5:
https://lore.kernel.org/kvm/[email protected]/
v4:
https://lore.kernel.org/kvm/[email protected]/
QEMU:
https://lore.kernel.org/qemu-devel/[email protected]/

Dapeng Mi (2):
KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU
KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

Mingwei Zhang (1):
KVM: x86/pmu: Snapshot host IA32_PERF_CAPABILITIES in kvm_host

Zide Chen (5):
KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events
KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h
KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks
KVM: x86/pmu: Emulate RDPMC on performance metrics
KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

 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              |  1 +
 arch/x86/include/asm/perf_event.h             |  1 +
 arch/x86/kvm/msrs.c                           | 10 +-
 arch/x86/kvm/pmu.c                            | 37 +++++--
 arch/x86/kvm/pmu.h                            | 16 ++-
 arch/x86/kvm/svm/pmu.c                        | 13 ++-
 arch/x86/kvm/vmx/pmu_intel.c                  | 99 +++++++++++++------
 arch/x86/kvm/vmx/pmu_intel.h                  | 10 +-
 arch/x86/kvm/vmx/vmx.c                        | 15 +--
 arch/x86/kvm/x86.c                            |  4 +
 tools/arch/x86/include/asm/msr-index.h        |  1 +
 tools/testing/selftests/kvm/include/x86/pmu.h |  3 +
 .../selftests/kvm/x86/pmu_counters_test.c     | 72 +++++++++++++-
 15 files changed, 220 insertions(+), 68 deletions(-)
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH v6 1/8 KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events

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.

---
v6:
- Re-arrange the code for early return. Clearer.
v2:
- Replace 3 in "if (index < 3)" with ARRAY_SIZE(fixed_pmc_perf_ids).

---
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +8 -6
index a73a9515d96c..f15af497d27f 100644
@@ -464,11 +464,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,
@@ -477,8 +474,13 @@ 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.
+	 */
+	if (index >= ARRAY_SIZE(fixed_pmc_perf_ids))
+		return 0;
 
 	/*
 	 * Yell if perf reports support for a fixed counter but perf doesn't
Signature · 1 line
2.54.0
Reviewed-by on v6 1/8
#2 lore raw

Re: [PATCH v6 1/8] KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events

LGTM. Thanks.

Quoted 56 lines · Zide Chen

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.

---
v6:
- Re-arrange the code for early return. Clearer.
v2:
- Replace 3 in "if (index < 3)" with ARRAY_SIZE(fixed_pmc_perf_ids).

---
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +8 -6
index a73a9515d96c..f15af497d27f 100644
@@ -464,11 +464,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,
@@ -477,8 +474,13 @@ 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.
+	 */
+	if (index >= ARRAY_SIZE(fixed_pmc_perf_ids))
+		return 0;
 
 	/*
 	 * Yell if perf reports support for a fixed counter but perf doesn't
Reviewed-by on v6 1/8
#3 lore raw

Re: [PATCH v6 1/8] KVM: x86/pmu: Do not map fixed counters >= 3 to generic perf events

In reply to Zide Chen
Quoted 11 lines · Zide Chen

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.

Quoted 4 lines

The two BUILD_BUG_ON() checks are no longer needed, so drop them along with __always_inline.

author
#4 lore raw

PATCH v6 2/8 KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU

In reply to Zide Chen

From: Dapeng Mi <[email protected]>

Starting with Ice Lake, Intel introduced fixed counter 3, which counts TOPDOWN.SLOTS - the number of available slots for an unhalted logical processor. It serves as the denominator for top-level metrics in the Top-down 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.

---
v6:
- Update comments to replace 2 with KVM_MAX_NR_INTEL_FIXED_COUNTERS - 1. v3:
- Move the non-contiguous counter filter code to pmu.c
v2:
- Don't advertise fixed counter 3 to userspace if the host doesn't support it.

---
 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/msrs.c             |  4 ++--
 arch/x86/kvm/pmu.c              | 18 +++++++++++++++++-
 3 files changed, 20 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index d8700eb848b4..dc9e4e8bfc07 100644
@@ -609,7 +609,7 @@ struct kvm_pmc {
 #define KVM_MAX_NR_GP_COUNTERS		KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \
 						KVM_MAX_NR_AMD_GP_COUNTERS)
 
-#define KVM_MAX_NR_INTEL_FIXED_COUNTERS	3
+#define KVM_MAX_NR_INTEL_FIXED_COUNTERS	4
 #define KVM_MAX_NR_AMD_FIXED_COUNTERS	0
 #define KVM_MAX_NR_FIXED_COUNTERS	KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUNTERS, \
 						KVM_MAX_NR_AMD_FIXED_COUNTERS)
arch/x86/kvm/msrs.c +2 -2
index c230b18d87e3..3bf42d90ad14 100644
@@ -228,7 +228,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,
@@ -2688,7 +2688,7 @@ void kvm_init_msr_lists(void)
 {
 	unsigned i;
 
-	BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 3,
+	BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 4,
 			 "Please update the fixed PMCs in msrs_to_save_pmu[]");
 
 	num_msrs_to_save = 0;
arch/x86/kvm/pmu.c +17 -1
index 62d0ed99ebe9..f82ba63767d0 100644
@@ -99,7 +99,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 +135,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 +184,19 @@ 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;
+	}
+
+	if (!enable_mediated_pmu && kvm_pmu_cap.num_counters_fixed > 3)
+		kvm_pmu_cap.num_counters_fixed = 3;
+
 	kvm_pmu_eventsel.INSTRUCTIONS_RETIRED =
 		perf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS);
 	kvm_pmu_eventsel.BRANCH_INSTRUCTIONS_RETIRED =
Signature · 1 line
2.54.0
Reviewed-by on v6 2/8
#5 lore raw

Re: [PATCH v6 2/8] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU

Quoted 112 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Starting with Ice Lake, Intel introduced fixed counter 3, which counts TOPDOWN.SLOTS - the number of available slots for an unhalted logical processor. It serves as the denominator for top-level metrics in the Top-down 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.

---
v6:
- Update comments to replace 2 with KVM_MAX_NR_INTEL_FIXED_COUNTERS - 1. v3:
- Move the non-contiguous counter filter code to pmu.c
v2:
- Don't advertise fixed counter 3 to userspace if the host doesn't support it.

---
 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/msrs.c             |  4 ++--
 arch/x86/kvm/pmu.c              | 18 +++++++++++++++++-
 3 files changed, 20 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index d8700eb848b4..dc9e4e8bfc07 100644
@@ -609,7 +609,7 @@ struct kvm_pmc {
 #define KVM_MAX_NR_GP_COUNTERS		KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \
 						KVM_MAX_NR_AMD_GP_COUNTERS)
 
-#define KVM_MAX_NR_INTEL_FIXED_COUNTERS	3
+#define KVM_MAX_NR_INTEL_FIXED_COUNTERS	4
 #define KVM_MAX_NR_AMD_FIXED_COUNTERS	0
 #define KVM_MAX_NR_FIXED_COUNTERS	KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUNTERS, \
 						KVM_MAX_NR_AMD_FIXED_COUNTERS)
arch/x86/kvm/msrs.c +2 -2
index c230b18d87e3..3bf42d90ad14 100644
@@ -228,7 +228,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,
@@ -2688,7 +2688,7 @@ void kvm_init_msr_lists(void)
 {
 	unsigned i;
 
-	BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 3,
+	BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 4,
 			 "Please update the fixed PMCs in msrs_to_save_pmu[]");
 
 	num_msrs_to_save = 0;
arch/x86/kvm/pmu.c +17 -1
index 62d0ed99ebe9..f82ba63767d0 100644
@@ -99,7 +99,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 +135,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 +184,19 @@ 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;
+	}
+
+	if (!enable_mediated_pmu && kvm_pmu_cap.num_counters_fixed > 3)
+		kvm_pmu_cap.num_counters_fixed = 3;
+
 	kvm_pmu_eventsel.INSTRUCTIONS_RETIRED =
 		perf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS);
 	kvm_pmu_eventsel.BRANCH_INSTRUCTIONS_RETIRED =
Reviewed-by on v6 2/8
#6 lore raw

Re: [PATCH v6 2/8] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU

In reply to Zide Chen
Quoted 96 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Starting with Ice Lake, Intel introduced fixed counter 3, which counts TOPDOWN.SLOTS - the number of available slots for an unhalted logical processor. It serves as the denominator for top-level metrics in the Top-down 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.

---
v6:
- Update comments to replace 2 with KVM_MAX_NR_INTEL_FIXED_COUNTERS - 1. v3:
- Move the non-contiguous counter filter code to pmu.c
v2:
- Don't advertise fixed counter 3 to userspace if the host doesn't support it.

---
 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/msrs.c             |  4 ++--
 arch/x86/kvm/pmu.c              | 18 +++++++++++++++++-
 3 files changed, 20 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index d8700eb848b4..dc9e4e8bfc07 100644
@@ -609,7 +609,7 @@ struct kvm_pmc {
 #define KVM_MAX_NR_GP_COUNTERS         KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \
                                                KVM_MAX_NR_AMD_GP_COUNTERS)
 
-#define KVM_MAX_NR_INTEL_FIXED_COUNTERS        3
+#define KVM_MAX_NR_INTEL_FIXED_COUNTERS        4
 #define KVM_MAX_NR_AMD_FIXED_COUNTERS  0
 #define KVM_MAX_NR_FIXED_COUNTERS      KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUNTERS, \
                                                KVM_MAX_NR_AMD_FIXED_COUNTERS)
arch/x86/kvm/msrs.c +2 -2
index c230b18d87e3..3bf42d90ad14 100644
@@ -228,7 +228,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,
@@ -2688,7 +2688,7 @@ void kvm_init_msr_lists(void)
 {
        unsigned i;
 
-       BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 3,
+       BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 4,
                         "Please update the fixed PMCs in msrs_to_save_pmu[]");
 
        num_msrs_to_save = 0;
arch/x86/kvm/pmu.c +4 -1
index 62d0ed99ebe9..f82ba63767d0 100644
@@ -99,7 +99,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 +135,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 +184,19 @@ 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);

arch/x86/kvm/pmu.c void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops) · line 195

Quoted 10 lines
arch/x86/kvm/pmu.c +9 -0
 
+       /*
+        * 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;
+       }

Nit: Should this CPUID.0AH decoding be left to perf? Perhaps struct x86_pmu_capability should include an additional field:
num_contiguous_counters_fixed?

author
#7 lore raw

Re: [PATCH v6 2/8] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU

Quoted 98 lines · Jim Mattson
Quoted 97 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Starting with Ice Lake, Intel introduced fixed counter 3, which counts TOPDOWN.SLOTS - the number of available slots for an unhalted logical processor. It serves as the denominator for top-level metrics in the Top-down 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.

---
v6:
- Update comments to replace 2 with KVM_MAX_NR_INTEL_FIXED_COUNTERS - 1. v3:
- Move the non-contiguous counter filter code to pmu.c
v2:
- Don't advertise fixed counter 3 to userspace if the host doesn't support it.

---
 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/msrs.c             |  4 ++--
 arch/x86/kvm/pmu.c              | 18 +++++++++++++++++-
 3 files changed, 20 insertions(+), 4 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index d8700eb848b4..dc9e4e8bfc07 100644
@@ -609,7 +609,7 @@ struct kvm_pmc {
 #define KVM_MAX_NR_GP_COUNTERS         KVM_MAX(KVM_MAX_NR_INTEL_GP_COUNTERS, \
                                                KVM_MAX_NR_AMD_GP_COUNTERS)
 
-#define KVM_MAX_NR_INTEL_FIXED_COUNTERS        3
+#define KVM_MAX_NR_INTEL_FIXED_COUNTERS        4
 #define KVM_MAX_NR_AMD_FIXED_COUNTERS  0
 #define KVM_MAX_NR_FIXED_COUNTERS      KVM_MAX(KVM_MAX_NR_INTEL_FIXED_COUNTERS, \
                                                KVM_MAX_NR_AMD_FIXED_COUNTERS)
arch/x86/kvm/msrs.c +2 -2
index c230b18d87e3..3bf42d90ad14 100644
@@ -228,7 +228,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,
@@ -2688,7 +2688,7 @@ void kvm_init_msr_lists(void)
 {
        unsigned i;
 
-       BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 3,
+       BUILD_BUG_ON_MSG(KVM_MAX_NR_FIXED_COUNTERS != 4,
                         "Please update the fixed PMCs in msrs_to_save_pmu[]");
 
        num_msrs_to_save = 0;
arch/x86/kvm/pmu.c +4 -1
index 62d0ed99ebe9..f82ba63767d0 100644
@@ -99,7 +99,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 +135,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 +184,19 @@ 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);
 
Quoted 13 lines

arch/x86/kvm/pmu.c void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops) · line 195

Quoted 9 lines
arch/x86/kvm/pmu.c +9 -0
+       /*
+        * 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;
+       }

Nit: Should this CPUID.0AH decoding be left to perf? Perhaps struct x86_pmu_capability should include an additional field:
num_contiguous_counters_fixed?

This is a temporary workaround that will be removed once non-contiguous fixed counter support is added in the vPMU v5 series:

https://lore.kernel.org/kvm/[email protected]/

BTW, the vPMU v5 series is based on top of this Topdown series, so it cannot be merged to the Sashiko branch for review. I will post a kvm-x86/next based v2 shortly with a new cleanup patch and minor changelog/comment fixes.

Quoted 1 line
#8 lore raw

Re: [PATCH v6 2/8] KVM: x86/pmu: Support Intel fixed counter 3 on mediated vPMU

Quoted 11 lines · Zide
Quoted 3 lines · Jim Mattson

Nit: Should this CPUID.0AH decoding be left to perf? Perhaps struct x86_pmu_capability should include an additional field:
num_contiguous_counters_fixed?

This is a temporary workaround that will be removed once non-contiguous fixed counter support is added in the vPMU v5 series:

Quoted 6 lines

https://lore.kernel.org/kvm/[email protected]/

BTW, the vPMU v5 series is based on top of this Topdown series, so it cannot be merged to the Sashiko branch for review. I will post a kvm-x86/next based v2 shortly with a new cleanup patch and minor changelog/comment fixes.

I am running our internal Sashiko on your vPMU v5 series now. I'll let you know if it finds any issues.

author
#9 lore raw

PATCH v6 3/8 KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h

In reply to Zide Chen

This is in preparation for it to be called from common x86 code, for example kvm_need_rdpmc_intercept(), to check the guest's PERF_METRICS capability.

Rename it to kvm_vcpu_get_perf_caps() to indicate that it's part of the common API, and shorten _capabilities to _caps.

No functional change intended.

---
v5: new patch.

---
 arch/x86/kvm/pmu.h           |  8 ++++++++
 arch/x86/kvm/vmx/pmu_intel.c |  6 +++---
 arch/x86/kvm/vmx/pmu_intel.h | 10 +---------
 3 files changed, 12 insertions(+), 12 deletions(-)
arch/x86/kvm/pmu.h +8 -0
index a5821d7c87f9..1b2f66a2e915 100644
@@ -271,6 +271,14 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu)
 				  X86_PMC_IDX_MAX);
 }
 
+static inline u64 kvm_vcpu_get_perf_caps(struct kvm_vcpu *vcpu)
+{
+	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_PDCM))
+		return 0;
+
+	return vcpu->arch.perf_capabilities;
+}
+
 void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);
 int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
 int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx);
arch/x86/kvm/vmx/pmu_intel.c +3 -3
index f15af497d27f..e426ddc8add4 100644
@@ -189,13 +189,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;
@@ -550,7 +550,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 		pmu->raw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
 	}
 
-	perf_capabilities = vcpu_get_perf_capabilities(vcpu);
+	perf_capabilities = kvm_vcpu_get_perf_caps(vcpu);
 	if (intel_pmu_lbr_is_compatible(vcpu) &&
 	    (perf_capabilities & PERF_CAP_LBR_FMT))
 		memcpy(&lbr_desc->records, &vmx_lbr_caps, sizeof(vmx_lbr_caps));
arch/x86/kvm/vmx/pmu_intel.h +1 -9
index 5d9357640aa1..afdbbc9991d6 100644
@@ -6,17 +6,9 @@
 
 #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;
-}
-
 static inline bool fw_writes_is_enabled(struct kvm_vcpu *vcpu)
 {
-	return (vcpu_get_perf_capabilities(vcpu) & PERF_CAP_FW_WRITES) != 0;
+	return (kvm_vcpu_get_perf_caps(vcpu) & PERF_CAP_FW_WRITES) != 0;
 }
 
 bool intel_pmu_lbr_is_enabled(struct kvm_vcpu *vcpu);
Signature · 1 line
2.54.0
Reviewed-by on v6 3/8
#10 lore raw

Re: [PATCH v6 3/8] KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h

Quoted 89 lines · Zide Chen

This is in preparation for it to be called from common x86 code, for example kvm_need_rdpmc_intercept(), to check the guest's PERF_METRICS capability.

Rename it to kvm_vcpu_get_perf_caps() to indicate that it's part of the common API, and shorten _capabilities to _caps.

No functional change intended.

---
v5: new patch.

---
 arch/x86/kvm/pmu.h           |  8 ++++++++
 arch/x86/kvm/vmx/pmu_intel.c |  6 +++---
 arch/x86/kvm/vmx/pmu_intel.h | 10 +---------
 3 files changed, 12 insertions(+), 12 deletions(-)
arch/x86/kvm/pmu.h +8 -0
index a5821d7c87f9..1b2f66a2e915 100644
@@ -271,6 +271,14 @@ static inline bool kvm_pmu_is_fastpath_emulation_allowed(struct kvm_vcpu *vcpu)
 				  X86_PMC_IDX_MAX);
 }
 
+static inline u64 kvm_vcpu_get_perf_caps(struct kvm_vcpu *vcpu)
+{
+	if (!guest_cpu_cap_has(vcpu, X86_FEATURE_PDCM))
+		return 0;
+
+	return vcpu->arch.perf_capabilities;
+}
+
 void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu);
 int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned pmc, u64 *data);
 int kvm_pmu_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx);
arch/x86/kvm/vmx/pmu_intel.c +3 -3
index f15af497d27f..e426ddc8add4 100644
@@ -189,13 +189,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;
@@ -550,7 +550,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 		pmu->raw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
 	}
 
-	perf_capabilities = vcpu_get_perf_capabilities(vcpu);
+	perf_capabilities = kvm_vcpu_get_perf_caps(vcpu);
 	if (intel_pmu_lbr_is_compatible(vcpu) &&
 	    (perf_capabilities & PERF_CAP_LBR_FMT))
 		memcpy(&lbr_desc->records, &vmx_lbr_caps, sizeof(vmx_lbr_caps));
arch/x86/kvm/vmx/pmu_intel.h +1 -9
index 5d9357640aa1..afdbbc9991d6 100644
@@ -6,17 +6,9 @@
 
 #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;
-}
-
 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);
Reviewed-by on v6 3/8
#11 lore raw

Re: [PATCH v6 3/8] KVM: x86/pmu: Rename and move vcpu_get_perf_capabilities() to pmu.h

In reply to Zide Chen
Quoted 11 lines · Zide Chen

This is in preparation for it to be called from common x86 code, for example kvm_need_rdpmc_intercept(), to check the guest's PERF_METRICS capability.

Rename it to kvm_vcpu_get_perf_caps() to indicate that it's part of the common API, and shorten _capabilities to _caps.

No functional change intended.

author
#12 lore raw

PATCH v6 4/8 KVM: x86/pmu: Snapshot host IA32_PERF_CAPABILITIES in kvm_host

In reply to Zide Chen

From: Mingwei Zhang <[email protected]>

Cache the unadulterated snapshot of perf_capabilities so that KVM can compare guest vPMU capabilities against raw hardware capabilities.

For example, if the host supports PERF_METRICS but it is not configured for the guest, KVM can use it to determine that RDPMC accesses must be intercepted.

---
v5: new patch.

---
 arch/x86/include/asm/kvm_host.h | 1 +
 arch/x86/kvm/vmx/vmx.c          | 8 ++------
 arch/x86/kvm/x86.c              | 4 ++++
 3 files changed, 7 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
index dc9e4e8bfc07..80f638588bf7 100644
@@ -347,6 +347,7 @@ struct kvm_host_values {
 	u64 xss;
 	u64 s_cet;
 	u64 arch_capabilities;
+	u64 perf_capabilities;
 };
 extern struct kvm_host_values kvm_host;
 
arch/x86/kvm/vmx/vmx.c +2 -6
index aded7039bd3e..b736b9ff965b 100644
@@ -8050,14 +8050,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);
@@ -8070,11 +8066,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_host.perf_capabilities & PERF_CAP_LBR_FMT;
 	}
 
 	if (vmx_pebs_supported()) {
-		perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK;
+		perf_cap |= kvm_host.perf_capabilities & PERF_CAP_PEBS_MASK;
 
 		/*
 		 * Disallow adaptive PEBS as it is functionally broken, can be
arch/x86/kvm/x86.c +4 -0
index 8dbc0fa302a8..8e775855f9be 100644
@@ -7032,6 +7032,10 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
 	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
 		rdmsrq(MSR_IA32_ARCH_CAPABILITIES, kvm_host.arch_capabilities);
 
+	if (boot_cpu_has(X86_FEATURE_PDCM))
+		rdmsrq_safe(MSR_IA32_PERF_CAPABILITIES,
+			    &kvm_host.perf_capabilities);
+
 	WARN_ON_ONCE(kvm_nr_uret_msrs);
 
 	r = ops->hardware_setup();
Signature · 1 line
2.54.0
Reviewed-by on v6 4/8
#13 lore raw

Re: [PATCH v6 4/8] KVM: x86/pmu: Snapshot host IA32_PERF_CAPABILITIES in kvm_host

Quoted 79 lines · Zide Chen

From: Mingwei Zhang <[email protected]>

Cache the unadulterated snapshot of perf_capabilities so that KVM can compare guest vPMU capabilities against raw hardware capabilities.

For example, if the host supports PERF_METRICS but it is not configured for the guest, KVM can use it to determine that RDPMC accesses must be intercepted.

---
v5: new patch.

---
 arch/x86/include/asm/kvm_host.h | 1 +
 arch/x86/kvm/vmx/vmx.c          | 8 ++------
 arch/x86/kvm/x86.c              | 4 ++++
 3 files changed, 7 insertions(+), 6 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -0
index dc9e4e8bfc07..80f638588bf7 100644
@@ -347,6 +347,7 @@ struct kvm_host_values {
 	u64 xss;
 	u64 s_cet;
 	u64 arch_capabilities;
+	u64 perf_capabilities;
 };
 extern struct kvm_host_values kvm_host;
 
arch/x86/kvm/vmx/vmx.c +2 -6
index aded7039bd3e..b736b9ff965b 100644
@@ -8050,14 +8050,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);
@@ -8070,11 +8066,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_host.perf_capabilities & PERF_CAP_LBR_FMT;
 	}
 
 	if (vmx_pebs_supported()) {
-		perf_cap |= host_perf_cap & PERF_CAP_PEBS_MASK;
+		perf_cap |= kvm_host.perf_capabilities & PERF_CAP_PEBS_MASK;
 
 		/*
 		 * Disallow adaptive PEBS as it is functionally broken, can be
arch/x86/kvm/x86.c +4 -0
index 8dbc0fa302a8..8e775855f9be 100644
@@ -7032,6 +7032,10 @@ int kvm_x86_vendor_init(struct kvm_x86_init_ops *ops)
 	if (boot_cpu_has(X86_FEATURE_ARCH_CAPABILITIES))
 		rdmsrq(MSR_IA32_ARCH_CAPABILITIES, kvm_host.arch_capabilities);
 
+	if (boot_cpu_has(X86_FEATURE_PDCM))
+		rdmsrq_safe(MSR_IA32_PERF_CAPABILITIES,
+			    &kvm_host.perf_capabilities);
+
 	WARN_ON_ONCE(kvm_nr_uret_msrs);
 
 	r = ops->hardware_setup();
Reviewed-by on v6 4/8
#14 lore raw

Re: [PATCH v6 4/8] KVM: x86/pmu: Snapshot host IA32_PERF_CAPABILITIES in kvm_host

In reply to Zide Chen
Quoted 6 lines · Zide Chen

From: Mingwei Zhang <[email protected]>

Cache the unadulterated snapshot of perf_capabilities so that KVM can compare guest vPMU capabilities against raw hardware capabilities.

Quoted 6 lines

For example, if the host supports PERF_METRICS but it is not configured for the guest, KVM can use it to determine that RDPMC accesses must be intercepted.

author
#15 lore raw

PATCH v6 5/8 KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

In reply to Zide Chen

From: Dapeng Mi <[email protected]>

Bit 15 in IA32_PERF_CAPABILITIES indicates that the CPU provides built-in support for Topdown Microarchitecture Analysis (TMA) L1 metrics via the IA32_PERF_METRICS MSR.

Expose this capability only when mediated vPMU is enabled, as emulating IA32_PERF_METRICS in the legacy vPMU model is impractical.

Pass IA32_PERF_METRICS through to the guest only when mediated vPMU is enabled and bit 15 is set in guest IA32_PERF_CAPABILITIES. Allow kvm_pmu_{get,set}_msr() to handle this MSR for host accesses.

Save and restore this MSR on host/guest PMU context switches so that host PMU activity does not clobber the guest value, and guest state is not leaked into the host.

---
v5:
- Remove host_initiated check in set/get MSR handlers.
v4:
- Remove WARN_ON_ONCE() and simply reject the guest accesses by checking host_initiated. (Sashiko)
- Passthru MSR_PERF_METRICS only if has_mediated_pmu is true. (Sashiko)
- Remove the redundant !! in vcpu_has_perf_metrics().
v3:
- Replace WARN_ON() with WARN_ON_ONCE(). (Dapeng)
- Add comments to explain why we don't validate writes on PERF_METRICS.

---
 arch/x86/include/asm/kvm_host.h   |  1 +
 arch/x86/include/asm/msr-index.h  |  1 +
 arch/x86/include/asm/perf_event.h |  1 +
 arch/x86/kvm/msrs.c               |  6 +++++-
 arch/x86/kvm/pmu.h                |  5 +++++
 arch/x86/kvm/vmx/pmu_intel.c      | 31 +++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c            |  7 +++++++
 7 files changed, 51 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
index 80f638588bf7..96376d8a5199 100644
@@ -630,6 +630,7 @@ struct kvm_pmu {
 	u64 global_status_rsvd;
 	u64 reserved_bits;
 	u64 raw_event_mask;
+	u64 perf_metrics;
 	struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
 	struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
 
arch/x86/include/asm/msr-index.h +1 -0
index 18c4be75e927..fdcaeb6c8352 100644
@@ -331,6 +331,7 @@
 #define PERF_CAP_PEBS_FORMAT		0xf00
 #define PERF_CAP_FW_WRITES		BIT_ULL(13)
 #define PERF_CAP_PEBS_BASELINE		BIT_ULL(14)
+#define PERF_CAP_PERF_METRICS		BIT_ULL(15)
 #define PERF_CAP_PEBS_TIMING_INFO	BIT_ULL(17)
 #define PERF_CAP_PEBS_MASK		(PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \
 					 PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \
arch/x86/include/asm/perf_event.h +1 -0
index 1eb13673e889..bc2e1cbcd9b9 100644
@@ -447,6 +447,7 @@ static inline bool is_topdown_idx(int idx)
 #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT	54
 #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD	BIT_ULL(GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT)
 #define GLOBAL_STATUS_PERF_METRICS_OVF_BIT	48
+#define GLOBAL_STATUS_PERF_METRICS_OVF		BIT_ULL(GLOBAL_STATUS_PERF_METRICS_OVF_BIT)
 
 #define GLOBAL_CTRL_EN_PERF_METRICS		BIT_ULL(48)
 /*
arch/x86/kvm/msrs.c +5 -1
index 3bf42d90ad14..c751a8dbd45d 100644
@@ -230,7 +230,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. */
@@ -2625,6 +2625,10 @@ static void kvm_probe_msr_to_save(u32 msr_index)
 		     intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2))
 			return;
 		break;
+	case MSR_PERF_METRICS:
+		if (!(kvm_caps.supported_perf_cap & PERF_CAP_PERF_METRICS))
+			return;
+		break;
 	case MSR_ARCH_PERFMON_PERFCTR0 ...
 	     MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
 		if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
arch/x86/kvm/pmu.h +5 -0
index 1b2f66a2e915..3066cade5790 100644
@@ -279,6 +279,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);
arch/x86/kvm/vmx/pmu_intel.c +31 -0
index e426ddc8add4..225afd3937c3 100644
@@ -188,6 +188,8 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 	switch (msr) {
 	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;
@@ -345,6 +347,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;
@@ -394,6 +399,15 @@ 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:
+		/*
+		 * On platforms that support only hardware level-1, bits [63:32]
+		 * are reserved and ignored by hardware. If hardware level-2 is also
+		 * supported, they may contain valid metric data.
+		 * Either way, guest writes are passed through verbatim.
+		 */
+		pmu->perf_metrics = data;
+		break;
 	case MSR_IA32_PEBS_ENABLE:
 		if (data & pmu->pebs_enable_rsvd)
 			return 1;
@@ -589,6 +603,11 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 		pmu->global_status_rsvd &=
 				~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
 
+	if (perf_capabilities & PERF_CAP_PERF_METRICS) {
+		pmu->global_ctrl_rsvd &= ~GLOBAL_CTRL_EN_PERF_METRICS;
+		pmu->global_status_rsvd &= ~GLOBAL_STATUS_PERF_METRICS_OVF;
+	}
+
 	if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
 		if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
 			pmu->pebs_enable_rsvd = counter_rsvd;
@@ -632,6 +651,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);
 }
 
@@ -803,6 +825,9 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	u64 global_status, toggle;
 
+	if (kvm_vcpu_has_perf_metrics(vcpu))
+		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)
@@ -831,6 +856,12 @@ 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_has_perf_metrics(vcpu)) {
+		pmu->perf_metrics = rdpmc(INTEL_PMC_FIXED_RDPMC_METRICS);
+		if (pmu->perf_metrics)
+			wrmsrq(MSR_PERF_METRICS, 0);
+	}
 }
 
 struct kvm_pmu_ops intel_pmu_ops __initdata = {
arch/x86/kvm/vmx/vmx.c +7 -0
index b736b9ff965b..21eb4b339fa6 100644
@@ -4273,6 +4273,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_has_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)
@@ -8095,6 +8099,9 @@ static __init u64 vmx_get_perf_capabilities(void)
 		perf_cap &= ~PERF_CAP_PEBS_BASELINE;
 	}
 
+	if (enable_mediated_pmu)
+		perf_cap |= kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS;
+
 	return perf_cap;
 }
 
Signature · 1 line
2.54.0
Reviewed-by on v6 5/8
#16 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

Quoted 225 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Bit 15 in IA32_PERF_CAPABILITIES indicates that the CPU provides built-in support for Topdown Microarchitecture Analysis (TMA) L1 metrics via the IA32_PERF_METRICS MSR.

Expose this capability only when mediated vPMU is enabled, as emulating IA32_PERF_METRICS in the legacy vPMU model is impractical.

Pass IA32_PERF_METRICS through to the guest only when mediated vPMU is enabled and bit 15 is set in guest IA32_PERF_CAPABILITIES. Allow kvm_pmu_{get,set}_msr() to handle this MSR for host accesses.

Save and restore this MSR on host/guest PMU context switches so that host PMU activity does not clobber the guest value, and guest state is not leaked into the host.

---
v5:
- Remove host_initiated check in set/get MSR handlers.
v4:
- Remove WARN_ON_ONCE() and simply reject the guest accesses by checking host_initiated. (Sashiko)
- Passthru MSR_PERF_METRICS only if has_mediated_pmu is true. (Sashiko)
- Remove the redundant !! in vcpu_has_perf_metrics().
v3:
- Replace WARN_ON() with WARN_ON_ONCE(). (Dapeng)
- Add comments to explain why we don't validate writes on PERF_METRICS.

---
 arch/x86/include/asm/kvm_host.h   |  1 +
 arch/x86/include/asm/msr-index.h  |  1 +
 arch/x86/include/asm/perf_event.h |  1 +
 arch/x86/kvm/msrs.c               |  6 +++++-
 arch/x86/kvm/pmu.h                |  5 +++++
 arch/x86/kvm/vmx/pmu_intel.c      | 31 +++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c            |  7 +++++++
 7 files changed, 51 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
index 80f638588bf7..96376d8a5199 100644
@@ -630,6 +630,7 @@ struct kvm_pmu {
 	u64 global_status_rsvd;
 	u64 reserved_bits;
 	u64 raw_event_mask;
+	u64 perf_metrics;
 	struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
 	struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
 
arch/x86/include/asm/msr-index.h +1 -0
index 18c4be75e927..fdcaeb6c8352 100644
@@ -331,6 +331,7 @@
 #define PERF_CAP_PEBS_FORMAT		0xf00
 #define PERF_CAP_FW_WRITES		BIT_ULL(13)
 #define PERF_CAP_PEBS_BASELINE		BIT_ULL(14)
+#define PERF_CAP_PERF_METRICS		BIT_ULL(15)
 #define PERF_CAP_PEBS_TIMING_INFO	BIT_ULL(17)
 #define PERF_CAP_PEBS_MASK		(PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \
 					 PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \
arch/x86/include/asm/perf_event.h +1 -0
index 1eb13673e889..bc2e1cbcd9b9 100644
@@ -447,6 +447,7 @@ static inline bool is_topdown_idx(int idx)
 #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT	54
 #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD	BIT_ULL(GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT)
 #define GLOBAL_STATUS_PERF_METRICS_OVF_BIT	48
+#define GLOBAL_STATUS_PERF_METRICS_OVF		BIT_ULL(GLOBAL_STATUS_PERF_METRICS_OVF_BIT)
 
 #define GLOBAL_CTRL_EN_PERF_METRICS		BIT_ULL(48)
 /*
arch/x86/kvm/msrs.c +5 -1
index 3bf42d90ad14..c751a8dbd45d 100644
@@ -230,7 +230,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. */
@@ -2625,6 +2625,10 @@ static void kvm_probe_msr_to_save(u32 msr_index)
 		     intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2))
 			return;
 		break;
+	case MSR_PERF_METRICS:
+		if (!(kvm_caps.supported_perf_cap & PERF_CAP_PERF_METRICS))
+			return;
+		break;
 	case MSR_ARCH_PERFMON_PERFCTR0 ...
 	     MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
 		if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
arch/x86/kvm/pmu.h +5 -0
index 1b2f66a2e915..3066cade5790 100644
@@ -279,6 +279,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);
arch/x86/kvm/vmx/pmu_intel.c +31 -0
index e426ddc8add4..225afd3937c3 100644
@@ -188,6 +188,8 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 	switch (msr) {
 	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;
@@ -345,6 +347,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;
@@ -394,6 +399,15 @@ 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:
+		/*
+		 * On platforms that support only hardware level-1, bits [63:32]
+		 * are reserved and ignored by hardware. If hardware level-2 is also
+		 * supported, they may contain valid metric data.
+		 * Either way, guest writes are passed through verbatim.
+		 */
+		pmu->perf_metrics = data;
+		break;
 	case MSR_IA32_PEBS_ENABLE:
 		if (data & pmu->pebs_enable_rsvd)
 			return 1;
@@ -589,6 +603,11 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 		pmu->global_status_rsvd &=
 				~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
 
+	if (perf_capabilities & PERF_CAP_PERF_METRICS) {
+		pmu->global_ctrl_rsvd &= ~GLOBAL_CTRL_EN_PERF_METRICS;
+		pmu->global_status_rsvd &= ~GLOBAL_STATUS_PERF_METRICS_OVF;
+	}
+
 	if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
 		if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
 			pmu->pebs_enable_rsvd = counter_rsvd;
@@ -632,6 +651,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);
 }
 
@@ -803,6 +825,9 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	u64 global_status, toggle;
 
+	if (kvm_vcpu_has_perf_metrics(vcpu))
+		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)
@@ -831,6 +856,12 @@ 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_has_perf_metrics(vcpu)) {
+		pmu->perf_metrics = rdpmc(INTEL_PMC_FIXED_RDPMC_METRICS);
+		if (pmu->perf_metrics)
+			wrmsrq(MSR_PERF_METRICS, 0);
+	}
 }
 
 struct kvm_pmu_ops intel_pmu_ops __initdata = {
arch/x86/kvm/vmx/vmx.c +7 -0
index b736b9ff965b..21eb4b339fa6 100644
@@ -4273,6 +4273,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_has_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)
@@ -8095,6 +8099,9 @@ static __init u64 vmx_get_perf_capabilities(void)
 		perf_cap &= ~PERF_CAP_PEBS_BASELINE;
 	}
 
+	if (enable_mediated_pmu)
+		perf_cap |= kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS;
+
 	return perf_cap;
 }
 
Reviewed-by on v6 5/8
#17 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

In reply to Zide Chen
Quoted 14 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Bit 15 in IA32_PERF_CAPABILITIES indicates that the CPU provides built-in support for Topdown Microarchitecture Analysis (TMA) L1 metrics via the IA32_PERF_METRICS MSR.

Expose this capability only when mediated vPMU is enabled, as emulating IA32_PERF_METRICS in the legacy vPMU model is impractical.

Pass IA32_PERF_METRICS through to the guest only when mediated vPMU is enabled and bit 15 is set in guest IA32_PERF_CAPABILITIES. Allow kvm_pmu_{get,set}_msr() to handle this MSR for host accesses.

Quoted 6 lines

Save and restore this MSR on host/guest PMU context switches so that host PMU activity does not clobber the guest value, and guest state is not leaked into the host.

author
#18 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

In reply to Zide Chen
Quoted 156 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Bit 15 in IA32_PERF_CAPABILITIES indicates that the CPU provides built-in support for Topdown Microarchitecture Analysis (TMA) L1 metrics via the IA32_PERF_METRICS MSR.

Expose this capability only when mediated vPMU is enabled, as emulating IA32_PERF_METRICS in the legacy vPMU model is impractical.

Pass IA32_PERF_METRICS through to the guest only when mediated vPMU is enabled and bit 15 is set in guest IA32_PERF_CAPABILITIES. Allow kvm_pmu_{get,set}_msr() to handle this MSR for host accesses.

Save and restore this MSR on host/guest PMU context switches so that host PMU activity does not clobber the guest value, and guest state is not leaked into the host.

---
v5:
- Remove host_initiated check in set/get MSR handlers.
v4:
- Remove WARN_ON_ONCE() and simply reject the guest accesses by checking host_initiated. (Sashiko)
- Passthru MSR_PERF_METRICS only if has_mediated_pmu is true. (Sashiko)
- Remove the redundant !! in vcpu_has_perf_metrics().
v3:
- Replace WARN_ON() with WARN_ON_ONCE(). (Dapeng)
- Add comments to explain why we don't validate writes on PERF_METRICS.

---
 arch/x86/include/asm/kvm_host.h   |  1 +
 arch/x86/include/asm/msr-index.h  |  1 +
 arch/x86/include/asm/perf_event.h |  1 +
 arch/x86/kvm/msrs.c               |  6 +++++-
 arch/x86/kvm/pmu.h                |  5 +++++
 arch/x86/kvm/vmx/pmu_intel.c      | 31 +++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c            |  7 +++++++
 7 files changed, 51 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
index 80f638588bf7..96376d8a5199 100644
@@ -630,6 +630,7 @@ struct kvm_pmu {
 	u64 global_status_rsvd;
 	u64 reserved_bits;
 	u64 raw_event_mask;
+	u64 perf_metrics;
 	struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
 	struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
 
arch/x86/include/asm/msr-index.h +1 -0
index 18c4be75e927..fdcaeb6c8352 100644
@@ -331,6 +331,7 @@
 #define PERF_CAP_PEBS_FORMAT		0xf00
 #define PERF_CAP_FW_WRITES		BIT_ULL(13)
 #define PERF_CAP_PEBS_BASELINE		BIT_ULL(14)
+#define PERF_CAP_PERF_METRICS		BIT_ULL(15)
 #define PERF_CAP_PEBS_TIMING_INFO	BIT_ULL(17)
 #define PERF_CAP_PEBS_MASK		(PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \
 					 PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \
arch/x86/include/asm/perf_event.h +1 -0
index 1eb13673e889..bc2e1cbcd9b9 100644
@@ -447,6 +447,7 @@ static inline bool is_topdown_idx(int idx)
 #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT	54
 #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD	BIT_ULL(GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT)
 #define GLOBAL_STATUS_PERF_METRICS_OVF_BIT	48
+#define GLOBAL_STATUS_PERF_METRICS_OVF		BIT_ULL(GLOBAL_STATUS_PERF_METRICS_OVF_BIT)
 
 #define GLOBAL_CTRL_EN_PERF_METRICS		BIT_ULL(48)
 /*
arch/x86/kvm/msrs.c +5 -1
index 3bf42d90ad14..c751a8dbd45d 100644
@@ -230,7 +230,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. */
@@ -2625,6 +2625,10 @@ static void kvm_probe_msr_to_save(u32 msr_index)
 		     intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2))
 			return;
 		break;
+	case MSR_PERF_METRICS:
+		if (!(kvm_caps.supported_perf_cap & PERF_CAP_PERF_METRICS))
+			return;
+		break;
 	case MSR_ARCH_PERFMON_PERFCTR0 ...
 	     MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
 		if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
arch/x86/kvm/pmu.h +5 -0
index 1b2f66a2e915..3066cade5790 100644
@@ -279,6 +279,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);
arch/x86/kvm/vmx/pmu_intel.c +14 -0
index e426ddc8add4..225afd3937c3 100644
@@ -188,6 +188,8 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 	switch (msr) {
 	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;
@@ -345,6 +347,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;
@@ -394,6 +399,15 @@ 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:
+		/*
+		 * On platforms that support only hardware level-1, bits [63:32]
+		 * are reserved and ignored by hardware. If hardware level-2 is also
+		 * supported, they may contain valid metric data.
+		 * Either way, guest writes are passed through verbatim.
+		 */
+		pmu->perf_metrics = data;
+		break;
 	case MSR_IA32_PEBS_ENABLE:
 		if (data & pmu->pebs_enable_rsvd)
 			return 1;

arch/x86/kvm/vmx/pmu_intel.c static void intel_pmu_refresh(struct kvm_vcpu *vcpu) · line 610

Quoted 8 lines
arch/x86/kvm/vmx/pmu_intel.c +5 -0
@@ -589,6 +603,11 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 		pmu->global_status_rsvd &=
 				~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
 
+	if (perf_capabilities & PERF_CAP_PERF_METRICS) {
+		pmu->global_ctrl_rsvd &= ~GLOBAL_CTRL_EN_PERF_METRICS;
+		pmu->global_status_rsvd &= ~GLOBAL_STATUS_PERF_METRICS_OVF;
+	}
+

Nit: the following code looks cleaner. Since bits [48:0] are identical between the GLOBAL_CTRL and GLOBAL_STATUS MSRs, follow the existing pattern of deriving global_status_rsvd from global_ctrl_rsvd, then handle bits [63:49] separately.

@@ -577,6 +590,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)

        counter_rsvd = ~((BIT_ULL(pmu->nr_arch_gp_counters) - 1) |
                         ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) <<
KVM_FIXED_PMC_BASE_IDX));
        pmu->global_ctrl_rsvd = counter_rsvd;

+ if (perf_capabilities & PERF_CAP_PERF_METRICS)
+ pmu->global_ctrl_rsvd &= ~GLOBAL_CTRL_EN_PERF_METRICS;

        /*
         * GLOBAL_STATUS and GLOBAL_OVF_CONTROL (a.k.a. GLOBAL_STATUS_RESET)
         * share reserved bit definitions.  The kernel just happens to use
         * OVF_CTRL for the names.
         */
        pmu->global_status_rsvd = pmu->global_ctrl_rsvd
                        & ~(MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF |
                            MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD);
#19 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

In reply to Zide Chen
Quoted 179 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Bit 15 in IA32_PERF_CAPABILITIES indicates that the CPU provides built-in support for Topdown Microarchitecture Analysis (TMA) L1 metrics via the IA32_PERF_METRICS MSR.

Expose this capability only when mediated vPMU is enabled, as emulating IA32_PERF_METRICS in the legacy vPMU model is impractical.

Pass IA32_PERF_METRICS through to the guest only when mediated vPMU is enabled and bit 15 is set in guest IA32_PERF_CAPABILITIES. Allow kvm_pmu_{get,set}_msr() to handle this MSR for host accesses.

Save and restore this MSR on host/guest PMU context switches so that host PMU activity does not clobber the guest value, and guest state is not leaked into the host.

---
v5:
- Remove host_initiated check in set/get MSR handlers.
v4:
- Remove WARN_ON_ONCE() and simply reject the guest accesses by checking host_initiated. (Sashiko)
- Passthru MSR_PERF_METRICS only if has_mediated_pmu is true. (Sashiko)
- Remove the redundant !! in vcpu_has_perf_metrics().
v3:
- Replace WARN_ON() with WARN_ON_ONCE(). (Dapeng)
- Add comments to explain why we don't validate writes on PERF_METRICS.

---
 arch/x86/include/asm/kvm_host.h   |  1 +
 arch/x86/include/asm/msr-index.h  |  1 +
 arch/x86/include/asm/perf_event.h |  1 +
 arch/x86/kvm/msrs.c               |  6 +++++-
 arch/x86/kvm/pmu.h                |  5 +++++
 arch/x86/kvm/vmx/pmu_intel.c      | 31 +++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c            |  7 +++++++
 7 files changed, 51 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
index 80f638588bf7..96376d8a5199 100644
@@ -630,6 +630,7 @@ struct kvm_pmu {
        u64 global_status_rsvd;
        u64 reserved_bits;
        u64 raw_event_mask;
+       u64 perf_metrics;
        struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
        struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
 
arch/x86/include/asm/msr-index.h +1 -0
index 18c4be75e927..fdcaeb6c8352 100644
@@ -331,6 +331,7 @@
 #define PERF_CAP_PEBS_FORMAT           0xf00
 #define PERF_CAP_FW_WRITES             BIT_ULL(13)
 #define PERF_CAP_PEBS_BASELINE         BIT_ULL(14)
+#define PERF_CAP_PERF_METRICS          BIT_ULL(15)
 #define PERF_CAP_PEBS_TIMING_INFO      BIT_ULL(17)
 #define PERF_CAP_PEBS_MASK             (PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \
                                         PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \
arch/x86/include/asm/perf_event.h +1 -0
index 1eb13673e889..bc2e1cbcd9b9 100644
@@ -447,6 +447,7 @@ static inline bool is_topdown_idx(int idx)
 #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT  54
 #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD      BIT_ULL(GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT)
 #define GLOBAL_STATUS_PERF_METRICS_OVF_BIT     48
+#define GLOBAL_STATUS_PERF_METRICS_OVF         BIT_ULL(GLOBAL_STATUS_PERF_METRICS_OVF_BIT)
 
 #define GLOBAL_CTRL_EN_PERF_METRICS            BIT_ULL(48)
 /*
arch/x86/kvm/msrs.c +5 -1
index 3bf42d90ad14..c751a8dbd45d 100644
@@ -230,7 +230,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. */
@@ -2625,6 +2625,10 @@ static void kvm_probe_msr_to_save(u32 msr_index)
                     intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2))
                        return;
                break;
+       case MSR_PERF_METRICS:
+               if (!(kvm_caps.supported_perf_cap & PERF_CAP_PERF_METRICS))
+                       return;
+               break;
        case MSR_ARCH_PERFMON_PERFCTR0 ...
             MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
                if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
arch/x86/kvm/pmu.h +5 -0
index 1b2f66a2e915..3066cade5790 100644
@@ -279,6 +279,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);
arch/x86/kvm/vmx/pmu_intel.c +22 -0
index e426ddc8add4..225afd3937c3 100644
@@ -188,6 +188,8 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
        switch (msr) {
        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;
@@ -345,6 +347,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;
@@ -394,6 +399,15 @@ 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:
+               /*
+                * On platforms that support only hardware level-1, bits [63:32]
+                * are reserved and ignored by hardware. If hardware level-2 is also
+                * supported, they may contain valid metric data.
+                * Either way, guest writes are passed through verbatim.
+                */
+               pmu->perf_metrics = data;
+               break;
        case MSR_IA32_PEBS_ENABLE:
                if (data & pmu->pebs_enable_rsvd)
                        return 1;
@@ -589,6 +603,11 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
                pmu->global_status_rsvd &=
                                ~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
 
+       if (perf_capabilities & PERF_CAP_PERF_METRICS) {
+               pmu->global_ctrl_rsvd &= ~GLOBAL_CTRL_EN_PERF_METRICS;
+               pmu->global_status_rsvd &= ~GLOBAL_STATUS_PERF_METRICS_OVF;
+       }
+
        if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
                if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
                        pmu->pebs_enable_rsvd = counter_rsvd;
@@ -632,6 +651,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);
 }
 

arch/x86/kvm/vmx/pmu_intel.c static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu) · line 830

Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +3 -0
@@ -803,6 +825,9 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
        u64 global_status, toggle;
 
+       if (kvm_vcpu_has_perf_metrics(vcpu))
+               wrmsrq(MSR_PERF_METRICS, pmu->perf_metrics);
+

Our internal Sashiko asks:

Does this write to MSR_PERF_METRICS violate the architectural requirement to write IA32_FIXED_CTR3 first?
According to the Intel SDM, software must write to IA32_FIXED_CTR3 before writing to IA32_PERF_METRICS to properly clear the TMA internal state machine. During VM-entry, kvm_pmu_load_guest_pmcs() is called before intel_mediated_pmu_load() to load the fixed counters. However, it conditionally skips the write if the guest's counter value matches the host hardware's current value:

kvm_pmu_load_guest_pmcs() {
    ...
    if (pmc->counter != rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i))
        wrmsrq(fixed_counter_msr(i), pmc->counter);
    ...
}

If skipped, KVM will unconditionally write to MSR_PERF_METRICS here in intel_mediated_pmu_load() without a preceding write to IA32_FIXED_CTR3. Could this omission fail to reset the TMA state machine and potentially break TMA metrics?

arch/x86/kvm/vmx/pmu_intel.c static void intel_mediated_pmu_put(struct kvm_vcpu *vcpu) · line 863

Quoted 12 lines
arch/x86/kvm/vmx/pmu_intel.c +5 -0
        rdmsrq(MSR_CORE_PERF_GLOBAL_STATUS, global_status);
        toggle = pmu->global_status ^ global_status;
        if (global_status & toggle)
@@ -831,6 +856,12 @@ 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_has_perf_metrics(vcpu)) {
+               pmu->perf_metrics = rdpmc(INTEL_PMC_FIXED_RDPMC_METRICS);
+               if (pmu->perf_metrics)
+                       wrmsrq(MSR_PERF_METRICS, 0);

And here:

Will clearing MSR_PERF_METRICS here occur before clearing MSR_CORE_PERF_FIXED_CTR3, reversing the architecturally mandated write order? In KVM's mediated PMU context switch out (VM-exit), kvm_mediated_pmu_put() calls the mediated_put callback before clearing the guest PMCs:

kvm_mediated_pmu_put() {
    ...
    kvm_pmu_call(mediated_put)(vcpu);
    kvm_pmu_put_guest_pmcs(vcpu);
    ...
}

This means intel_mediated_pmu_put() writes 0 to MSR_PERF_METRICS here, and then kvm_pmu_put_guest_pmcs() conditionally writes 0 to MSR_CORE_PERF_FIXED_CTR3 later. Additionally, if the fixed counter is already 0, the write to FIXED_CTR3 is skipped entirely.
Could this write to MSR_PERF_METRICS without a preceding initialization of FIXED_CTR3 corrupt the hardware state machine or cause undefined behavior?

Quoted 32 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -0
+       }
 }
 
 struct kvm_pmu_ops intel_pmu_ops __initdata = {
arch/x86/kvm/vmx/vmx.c +7 -1
index b736b9ff965b..21eb4b339fa6 100644
@@ -4273,6 +4273,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_has_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)
@@ -8095,6 +8099,9 @@ static __init u64 vmx_get_perf_capabilities(void)
                perf_cap &= ~PERF_CAP_PEBS_BASELINE;
        }
 
+       if (enable_mediated_pmu)
+               perf_cap |= kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS;
+
        return perf_cap;
 }
 
--

2.54.0

author
#20 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

Quoted 203 lines · Jim Mattson
Quoted 179 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Bit 15 in IA32_PERF_CAPABILITIES indicates that the CPU provides built-in support for Topdown Microarchitecture Analysis (TMA) L1 metrics via the IA32_PERF_METRICS MSR.

Expose this capability only when mediated vPMU is enabled, as emulating IA32_PERF_METRICS in the legacy vPMU model is impractical.

Pass IA32_PERF_METRICS through to the guest only when mediated vPMU is enabled and bit 15 is set in guest IA32_PERF_CAPABILITIES. Allow kvm_pmu_{get,set}_msr() to handle this MSR for host accesses.

Save and restore this MSR on host/guest PMU context switches so that host PMU activity does not clobber the guest value, and guest state is not leaked into the host.

---
v5:
- Remove host_initiated check in set/get MSR handlers.
v4:
- Remove WARN_ON_ONCE() and simply reject the guest accesses by checking host_initiated. (Sashiko)
- Passthru MSR_PERF_METRICS only if has_mediated_pmu is true. (Sashiko)
- Remove the redundant !! in vcpu_has_perf_metrics().
v3:
- Replace WARN_ON() with WARN_ON_ONCE(). (Dapeng)
- Add comments to explain why we don't validate writes on PERF_METRICS.

---
 arch/x86/include/asm/kvm_host.h   |  1 +
 arch/x86/include/asm/msr-index.h  |  1 +
 arch/x86/include/asm/perf_event.h |  1 +
 arch/x86/kvm/msrs.c               |  6 +++++-
 arch/x86/kvm/pmu.h                |  5 +++++
 arch/x86/kvm/vmx/pmu_intel.c      | 31 +++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c            |  7 +++++++
 7 files changed, 51 insertions(+), 1 deletion(-)
arch/x86/include/asm/kvm_host.h +1 -0
index 80f638588bf7..96376d8a5199 100644
@@ -630,6 +630,7 @@ struct kvm_pmu {
        u64 global_status_rsvd;
        u64 reserved_bits;
        u64 raw_event_mask;
+       u64 perf_metrics;
        struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
        struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
 
arch/x86/include/asm/msr-index.h +1 -0
index 18c4be75e927..fdcaeb6c8352 100644
@@ -331,6 +331,7 @@
 #define PERF_CAP_PEBS_FORMAT           0xf00
 #define PERF_CAP_FW_WRITES             BIT_ULL(13)
 #define PERF_CAP_PEBS_BASELINE         BIT_ULL(14)
+#define PERF_CAP_PERF_METRICS          BIT_ULL(15)
 #define PERF_CAP_PEBS_TIMING_INFO      BIT_ULL(17)
 #define PERF_CAP_PEBS_MASK             (PERF_CAP_PEBS_TRAP | PERF_CAP_ARCH_REG | \
                                         PERF_CAP_PEBS_FORMAT | PERF_CAP_PEBS_BASELINE | \
arch/x86/include/asm/perf_event.h +1 -0
index 1eb13673e889..bc2e1cbcd9b9 100644
@@ -447,6 +447,7 @@ static inline bool is_topdown_idx(int idx)
 #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT  54
 #define GLOBAL_STATUS_ARCH_PEBS_THRESHOLD      BIT_ULL(GLOBAL_STATUS_ARCH_PEBS_THRESHOLD_BIT)
 #define GLOBAL_STATUS_PERF_METRICS_OVF_BIT     48
+#define GLOBAL_STATUS_PERF_METRICS_OVF         BIT_ULL(GLOBAL_STATUS_PERF_METRICS_OVF_BIT)
 
 #define GLOBAL_CTRL_EN_PERF_METRICS            BIT_ULL(48)
 /*
arch/x86/kvm/msrs.c +5 -1
index 3bf42d90ad14..c751a8dbd45d 100644
@@ -230,7 +230,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. */
@@ -2625,6 +2625,10 @@ static void kvm_probe_msr_to_save(u32 msr_index)
                     intel_pt_validate_hw_cap(PT_CAP_num_address_ranges) * 2))
                        return;
                break;
+       case MSR_PERF_METRICS:
+               if (!(kvm_caps.supported_perf_cap & PERF_CAP_PERF_METRICS))
+                       return;
+               break;
        case MSR_ARCH_PERFMON_PERFCTR0 ...
             MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
                if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
arch/x86/kvm/pmu.h +5 -0
index 1b2f66a2e915..3066cade5790 100644
@@ -279,6 +279,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);
arch/x86/kvm/vmx/pmu_intel.c +22 -0
index e426ddc8add4..225afd3937c3 100644
@@ -188,6 +188,8 @@ static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
        switch (msr) {
        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;
@@ -345,6 +347,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;
@@ -394,6 +399,15 @@ 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:
+               /*
+                * On platforms that support only hardware level-1, bits [63:32]
+                * are reserved and ignored by hardware. If hardware level-2 is also
+                * supported, they may contain valid metric data.
+                * Either way, guest writes are passed through verbatim.
+                */
+               pmu->perf_metrics = data;
+               break;
        case MSR_IA32_PEBS_ENABLE:
                if (data & pmu->pebs_enable_rsvd)
                        return 1;
@@ -589,6 +603,11 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
                pmu->global_status_rsvd &=
                                ~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
 
+       if (perf_capabilities & PERF_CAP_PERF_METRICS) {
+               pmu->global_ctrl_rsvd &= ~GLOBAL_CTRL_EN_PERF_METRICS;
+               pmu->global_status_rsvd &= ~GLOBAL_STATUS_PERF_METRICS_OVF;
+       }
+
        if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
                if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
                        pmu->pebs_enable_rsvd = counter_rsvd;
@@ -632,6 +651,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);
 }
 

arch/x86/kvm/vmx/pmu_intel.c static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu) · line 830

Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +3 -0
@@ -803,6 +825,9 @@ static void intel_mediated_pmu_load(struct kvm_vcpu *vcpu)
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
        u64 global_status, toggle;
 
+       if (kvm_vcpu_has_perf_metrics(vcpu))
+               wrmsrq(MSR_PERF_METRICS, pmu->perf_metrics);
+

Our internal Sashiko asks:

Does this write to MSR_PERF_METRICS violate the architectural requirement to write IA32_FIXED_CTR3 first?
According to the Intel SDM, software must write to IA32_FIXED_CTR3 before writing to IA32_PERF_METRICS to properly clear the TMA internal state machine. During VM-entry, kvm_pmu_load_guest_pmcs() is called before intel_mediated_pmu_load() to load the fixed counters. However, it conditionally skips the write if the guest's counter value matches the host hardware's current value:

kvm_pmu_load_guest_pmcs() {
    ...
    if (pmc->counter != rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i))
        wrmsrq(fixed_counter_msr(i), pmc->counter);
    ...
}
Quoted 4 lines

If skipped, KVM will unconditionally write to MSR_PERF_METRICS here in intel_mediated_pmu_load() without a preceding write to IA32_FIXED_CTR3. Could this omission fail to reset the TMA state machine and potentially break TMA metrics?

IA32_PERF_GLOBAL_CTRL is already cleared before either fixed counter 3 or MSR_PERF_METRICS is restored.

The SDM does not appear to require an explicit WRMSR to fixed counter 3. If the write is skipped, the hardware already contains the guest's fixed counter 3 value.

Quoted 25 lines

arch/x86/kvm/vmx/pmu_intel.c static void intel_mediated_pmu_put(struct kvm_vcpu *vcpu) · line 863

Quoted 12 lines
arch/x86/kvm/vmx/pmu_intel.c +5 -0
        rdmsrq(MSR_CORE_PERF_GLOBAL_STATUS, global_status);
        toggle = pmu->global_status ^ global_status;
        if (global_status & toggle)
@@ -831,6 +856,12 @@ 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_has_perf_metrics(vcpu)) {
+               pmu->perf_metrics = rdpmc(INTEL_PMC_FIXED_RDPMC_METRICS);
+               if (pmu->perf_metrics)
+                       wrmsrq(MSR_PERF_METRICS, 0);

And here:

Will clearing MSR_PERF_METRICS here occur before clearing MSR_CORE_PERF_FIXED_CTR3, reversing the architecturally mandated write order? In KVM's mediated PMU context switch out (VM-exit), kvm_mediated_pmu_put() calls the mediated_put callback before clearing the guest PMCs:

kvm_mediated_pmu_put() {
    ...
    kvm_pmu_call(mediated_put)(vcpu);
    kvm_pmu_put_guest_pmcs(vcpu);
    ...
}
Quoted 6 lines

This means intel_mediated_pmu_put() writes 0 to MSR_PERF_METRICS here, and then kvm_pmu_put_guest_pmcs() conditionally writes 0 to MSR_CORE_PERF_FIXED_CTR3 later. Additionally, if the fixed counter is already 0, the write to FIXED_CTR3 is skipped entirely.
Could this write to MSR_PERF_METRICS without a preceding initialization of FIXED_CTR3 corrupt the hardware state machine or cause undefined behavior?

Similarly, IA32_PERF_GLOBAL_CTRL is already cleared at this point, so both EN_FIXED_CTR3 and EN_PERF_METRICS have already been disabled.

The SDM guidance that "fixed counter 3 must be restored before PERF_METRICS" appears to apply only to a running PMU, which is not the case here.

#21 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

Quoted 3 lines · Zide

The SDM guidance that "fixed counter 3 must be restored before PERF_METRICS" appears to apply only to a running PMU, which is not the case here.

That's not my reading of the SDM. There are three bullet points about save/restore at the end of volume 3, section 22.3.9.3:

* PERF_METRICS and fixed-function performance-monitoring counter 3 should be saved and restored together.
* To ensure that PERF_METRICS and fixed-function performance-monitoring counter 3 remain synchronized,
both should be disabled during both save and restore. Software should enable/disable them atomically, with a
single write to IA32_PERF_GLOBAL_CTRL to set/clear both EN_PERF_METRICS[bit 48] and
EN_FIXED_CTR3[bit 35].
* On state restore, fixed-function performance-monitoring counter 3 must be restored before PERF_METRICS,
otherwise undefined results may be observed.

The second bullet contradicts your claim above.

author
#22 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

Quoted 21 lines · Jim Mattson
Quoted 3 lines · Zide

The SDM guidance that "fixed counter 3 must be restored before PERF_METRICS" appears to apply only to a running PMU, which is not the case here.

That's not my reading of the SDM. There are three bullet points about save/restore at the end of volume 3, section 22.3.9.3:

* PERF_METRICS and fixed-function performance-monitoring counter 3 should be saved and restored together.
* To ensure that PERF_METRICS and fixed-function performance-monitoring counter 3 remain synchronized,
both should be disabled during both save and restore. Software should enable/disable them atomically, with a
single write to IA32_PERF_GLOBAL_CTRL to set/clear both EN_PERF_METRICS[bit 48] and
EN_FIXED_CTR3[bit 35].
* On state restore, fixed-function performance-monitoring counter 3 must be restored before PERF_METRICS,
otherwise undefined results may be observed.

Quoted 1 line

The second bullet contradicts your claim above.

Thanks for pointing this out. I followed up with an internal ucode expert, and my earlier explanation was incorrect.

The ordering requirement is not dependent on whether the PMU is enabled. The relevant recalculation happens on the WRMSR itself, so the SDM guidance to restore fixed counter 3 before PERF_METRICS still applies even when counting is disabled.

That said, for the code path being discussed, both PERF_METRICS and fixed counter 3 are written with 0. I was advised that this specific all-zero case still converges to the intended hardware state regardless of write order, so the write ordering here does not affect the final result.

Sorry for the confusion, and thanks for the correction.

#23 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

Quoted 35 lines · Zide
Quoted 21 lines · Jim Mattson
Quoted 3 lines · Zide

The SDM guidance that "fixed counter 3 must be restored before PERF_METRICS" appears to apply only to a running PMU, which is not the case here.

That's not my reading of the SDM. There are three bullet points about save/restore at the end of volume 3, section 22.3.9.3:

* PERF_METRICS and fixed-function performance-monitoring counter 3 should be saved and restored together.
* To ensure that PERF_METRICS and fixed-function performance-monitoring counter 3 remain synchronized,
both should be disabled during both save and restore. Software should enable/disable them atomically, with a
single write to IA32_PERF_GLOBAL_CTRL to set/clear both EN_PERF_METRICS[bit 48] and
EN_FIXED_CTR3[bit 35].
* On state restore, fixed-function performance-monitoring counter 3 must be restored before PERF_METRICS,
otherwise undefined results may be observed.

Quoted 1 line

The second bullet contradicts your claim above.

Thanks for pointing this out. I followed up with an internal ucode expert, and my earlier explanation was incorrect.

The ordering requirement is not dependent on whether the PMU is enabled. The relevant recalculation happens on the WRMSR itself, so the SDM guidance to restore fixed counter 3 before PERF_METRICS still applies even when counting is disabled.

Quoted 6 lines

That said, for the code path being discussed, both PERF_METRICS and fixed counter 3 are written with 0. I was advised that this specific all-zero case still converges to the intended hardware state regardless of write order, so the write ordering here does not affect the final result.

Sorry for the confusion, and thanks for the correction.

At the very least, the reliance on an undocumented exception to the requirement stated in the SDM warrants an explicit comment.

author
#24 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

Quoted 41 lines · Jim Mattson
Quoted 40 lines · Zide
Quoted 21 lines · Jim Mattson
Quoted 3 lines · Zide

The SDM guidance that "fixed counter 3 must be restored before PERF_METRICS" appears to apply only to a running PMU, which is not the case here.

That's not my reading of the SDM. There are three bullet points about save/restore at the end of volume 3, section 22.3.9.3:

* PERF_METRICS and fixed-function performance-monitoring counter 3 should be saved and restored together.
* To ensure that PERF_METRICS and fixed-function performance-monitoring counter 3 remain synchronized,
both should be disabled during both save and restore. Software should enable/disable them atomically, with a
single write to IA32_PERF_GLOBAL_CTRL to set/clear both EN_PERF_METRICS[bit 48] and
EN_FIXED_CTR3[bit 35].
* On state restore, fixed-function performance-monitoring counter 3 must be restored before PERF_METRICS,
otherwise undefined results may be observed.

Quoted 1 line

The second bullet contradicts your claim above.

Thanks for pointing this out. I followed up with an internal ucode expert, and my earlier explanation was incorrect.

The ordering requirement is not dependent on whether the PMU is enabled. The relevant recalculation happens on the WRMSR itself, so the SDM guidance to restore fixed counter 3 before PERF_METRICS still applies even when counting is disabled.

That said, for the code path being discussed, both PERF_METRICS and fixed counter 3 are written with 0. I was advised that this specific all-zero case still converges to the intended hardware state regardless of write order, so the write ordering here does not affect the final result.

Quoted 4 lines
Quoted 1 line

Sorry for the confusion, and thanks for the correction.

At the very least, the reliance on an undocumented exception to the requirement stated in the SDM warrants an explicit comment.

Yes, will do.

#25 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

In reply to Zide Chen
Quoted 47 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Bit 15 in IA32_PERF_CAPABILITIES indicates that the CPU provides built-in support for Topdown Microarchitecture Analysis (TMA) L1 metrics via the IA32_PERF_METRICS MSR.

Expose this capability only when mediated vPMU is enabled, as emulating IA32_PERF_METRICS in the legacy vPMU model is impractical.

Pass IA32_PERF_METRICS through to the guest only when mediated vPMU is enabled and bit 15 is set in guest IA32_PERF_CAPABILITIES. Allow kvm_pmu_{get,set}_msr() to handle this MSR for host accesses.

Save and restore this MSR on host/guest PMU context switches so that host PMU activity does not clobber the guest value, and guest state is not leaked into the host.

---
v5:
- Remove host_initiated check in set/get MSR handlers.
v4:
- Remove WARN_ON_ONCE() and simply reject the guest accesses by checking host_initiated. (Sashiko)
- Passthru MSR_PERF_METRICS only if has_mediated_pmu is true. (Sashiko)
- Remove the redundant !! in vcpu_has_perf_metrics().
v3:
- Replace WARN_ON() with WARN_ON_ONCE(). (Dapeng)
- Add comments to explain why we don't validate writes on PERF_METRICS.

---
 arch/x86/include/asm/kvm_host.h   |  1 +
 arch/x86/include/asm/msr-index.h  |  1 +
 arch/x86/include/asm/perf_event.h |  1 +
 arch/x86/kvm/msrs.c               |  6 +++++-
 arch/x86/kvm/pmu.h                |  5 +++++
 arch/x86/kvm/vmx/pmu_intel.c      | 31 +++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c            |  7 +++++++
 7 files changed, 51 insertions(+), 1 deletion(-)

...

arch/x86/kvm/vmx/vmx.c +0 -0
index b736b9ff965b..21eb4b339fa6 100644

arch/x86/kvm/vmx/vmx.c static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu) · line 4280

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +4 -0
@@ -4273,6 +4273,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_has_perf_metrics(vcpu);
+       vmx_set_intercept_for_msr(vcpu, MSR_PERF_METRICS,
+                                 MSR_TYPE_RW, intercept);
 }

Should we also update nested_vmx_merge_pmu_msr_bitmaps()?

author
#26 lore raw

Re: [PATCH v6 5/8] KVM: x86/pmu: Support PERF_METRICS MSR in mediated vPMU

Quoted 42 lines · Jim Mattson
Quoted 41 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Bit 15 in IA32_PERF_CAPABILITIES indicates that the CPU provides built-in support for Topdown Microarchitecture Analysis (TMA) L1 metrics via the IA32_PERF_METRICS MSR.

Expose this capability only when mediated vPMU is enabled, as emulating IA32_PERF_METRICS in the legacy vPMU model is impractical.

Pass IA32_PERF_METRICS through to the guest only when mediated vPMU is enabled and bit 15 is set in guest IA32_PERF_CAPABILITIES. Allow kvm_pmu_{get,set}_msr() to handle this MSR for host accesses.

Save and restore this MSR on host/guest PMU context switches so that host PMU activity does not clobber the guest value, and guest state is not leaked into the host.

---
v5:
- Remove host_initiated check in set/get MSR handlers.
v4:
- Remove WARN_ON_ONCE() and simply reject the guest accesses by checking host_initiated. (Sashiko)
- Passthru MSR_PERF_METRICS only if has_mediated_pmu is true. (Sashiko)
- Remove the redundant !! in vcpu_has_perf_metrics().
v3:
- Replace WARN_ON() with WARN_ON_ONCE(). (Dapeng)
- Add comments to explain why we don't validate writes on PERF_METRICS.

---
 arch/x86/include/asm/kvm_host.h   |  1 +
 arch/x86/include/asm/msr-index.h  |  1 +
 arch/x86/include/asm/perf_event.h |  1 +
 arch/x86/kvm/msrs.c               |  6 +++++-
 arch/x86/kvm/pmu.h                |  5 +++++
 arch/x86/kvm/vmx/pmu_intel.c      | 31 +++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c            |  7 +++++++
 7 files changed, 51 insertions(+), 1 deletion(-)
Quoted 16 lines
Quoted 6 lines

...

arch/x86/kvm/vmx/vmx.c +0 -0
index b736b9ff965b..21eb4b339fa6 100644

arch/x86/kvm/vmx/vmx.c static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu) · line 4280

Quoted 8 lines
arch/x86/kvm/vmx/vmx.c +4 -0
@@ -4273,6 +4273,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_has_perf_metrics(vcpu);
+       vmx_set_intercept_for_msr(vcpu, MSR_PERF_METRICS,
+                                 MSR_TYPE_RW, intercept);
 }

Should we also update nested_vmx_merge_pmu_msr_bitmaps()?

Yes, I can include nested_vmx_merge_pmu_msr_bitmaps() in v7 and post it today.

I just verified that Topdown works correctly, including MSR passthrough and RDPMC passthrough in a nested Linux setup on top of the vPMU v5 series (to enable medisted vPMU on nested).

Initially, I was planning to revisit this as part of a broader effort on nested PMU virtualization, including features such as Arch LBR. However, I now agree that including it in this series makes sense.

author
#27 lore raw

PATCH v6 6/8 KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks

In reply to Zide Chen

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 don't 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.

Also drop the redundant bitmask in intel_emulate_rdpmc() since pmc_read_counter() already applies the counter's bit-width mask.

No functional change intended.

---
v6: new patch.

---
 arch/x86/include/asm/kvm-x86-pmu-ops.h |  2 +-
 arch/x86/kvm/pmu.c                     |  9 +--------
 arch/x86/kvm/pmu.h                     |  4 ++--
 arch/x86/kvm/svm/pmu.c                 | 13 +++++++++----
 arch/x86/kvm/vmx/pmu_intel.c           | 25 ++++++++++++-------------
 5 files changed, 25 insertions(+), 28 deletions(-)
arch/x86/include/asm/kvm-x86-pmu-ops.h +1 -1
index 4a223c2793e3..4b50ed058aed 100644
@@ -13,7 +13,7 @@
  * KVM_X86_PMU_OP_OPTIONAL() can be used for those functions that can have
  * a NULL definition.
  */
-KVM_X86_PMU_OP(rdpmc_ecx_to_pmc)
+KVM_X86_PMU_OP(emulate_rdpmc)
 KVM_X86_PMU_OP(msr_idx_to_pmc)
 KVM_X86_PMU_OP_OPTIONAL(check_rdpmc_early)
 KVM_X86_PMU_OP(is_valid_msr)
arch/x86/kvm/pmu.c +1 -8
index f82ba63767d0..8ef2d4761790 100644
@@ -768,8 +768,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;
@@ -777,17 +775,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_need_any_pmc_intercept(struct kvm_vcpu *vcpu)
arch/x86/kvm/pmu.h +2 -2
index 3066cade5790..cdbefda844b9 100644
@@ -24,8 +24,8 @@
 #define KVM_FIXED_PMC_BASE_IDX INTEL_PMC_IDX_FIXED
 
 struct kvm_pmu_ops {
-	struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu,
-		unsigned int idx, u64 *mask);
+	int (*emulate_rdpmc)(struct kvm_vcpu *vcpu, unsigned int idx,
+			     u64 *data);
 	struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, u32 msr);
 	int (*check_rdpmc_early)(struct kvm_vcpu *vcpu, unsigned int idx);
 	bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr);
arch/x86/kvm/svm/pmu.c +9 -4
index c18286545a7a..0517fd4bbcd7 100644
@@ -84,10 +84,15 @@ 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 +307,7 @@ static bool amd_pmc_is_disabled_in_current_mode(struct kvm_pmc *pmc)
 }
 
 struct kvm_pmu_ops amd_pmu_ops __initdata = {
-	.rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc,
+	.emulate_rdpmc = amd_emulate_rdpmc,
 	.msr_idx_to_pmc = amd_msr_idx_to_pmc,
 	.check_rdpmc_early = amd_check_rdpmc_early,
 	.is_valid_msr = amd_is_valid_msr,
arch/x86/kvm/vmx/pmu_intel.c +12 -13
index 225afd3937c3..080677372c9b 100644
@@ -84,14 +84,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
@@ -104,7 +103,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
@@ -118,23 +119,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)
@@ -865,7 +864,7 @@ static void intel_mediated_pmu_put(struct kvm_vcpu *vcpu)
 }
 
 struct kvm_pmu_ops intel_pmu_ops __initdata = {
-	.rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc,
+	.emulate_rdpmc = intel_emulate_rdpmc,
 	.msr_idx_to_pmc = intel_msr_idx_to_pmc,
 	.is_valid_msr = intel_is_valid_msr,
 	.get_msr = intel_pmu_get_msr,
Signature · 1 line
2.54.0
Reviewed-by on v6 6/8
#28 lore raw

Re: [PATCH v6 6/8] KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks

Quoted 4 lines · Zide Chen

The current RDPMC emulation splits responsibility: rdpmc_ecx_to_pmc() in each vendor returns a kvm_pmc, then common code calls pmc_read_counter().

Quoted 8 lines

This design cannot support RDPMC reads that don't 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.

Also drop the redundant bitmask in intel_emulate_rdpmc() since pmc_read_counter() already applies the counter's bit-width mask.

Nice cleanup.

Quoted 177 lines

No functional change intended.

---
v6: new patch.

---
 arch/x86/include/asm/kvm-x86-pmu-ops.h |  2 +-
 arch/x86/kvm/pmu.c                     |  9 +--------
 arch/x86/kvm/pmu.h                     |  4 ++--
 arch/x86/kvm/svm/pmu.c                 | 13 +++++++++----
 arch/x86/kvm/vmx/pmu_intel.c           | 25 ++++++++++++-------------
 5 files changed, 25 insertions(+), 28 deletions(-)
arch/x86/include/asm/kvm-x86-pmu-ops.h +1 -1
index 4a223c2793e3..4b50ed058aed 100644
@@ -13,7 +13,7 @@
  * KVM_X86_PMU_OP_OPTIONAL() can be used for those functions that can have
  * a NULL definition.
  */
-KVM_X86_PMU_OP(rdpmc_ecx_to_pmc)
+KVM_X86_PMU_OP(emulate_rdpmc)
 KVM_X86_PMU_OP(msr_idx_to_pmc)
 KVM_X86_PMU_OP_OPTIONAL(check_rdpmc_early)
 KVM_X86_PMU_OP(is_valid_msr)
arch/x86/kvm/pmu.c +1 -8
index f82ba63767d0..8ef2d4761790 100644
@@ -768,8 +768,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;
@@ -777,17 +775,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_need_any_pmc_intercept(struct kvm_vcpu *vcpu)
arch/x86/kvm/pmu.h +2 -2
index 3066cade5790..cdbefda844b9 100644
@@ -24,8 +24,8 @@
 #define KVM_FIXED_PMC_BASE_IDX INTEL_PMC_IDX_FIXED
 
 struct kvm_pmu_ops {
-	struct kvm_pmc *(*rdpmc_ecx_to_pmc)(struct kvm_vcpu *vcpu,
-		unsigned int idx, u64 *mask);
+	int (*emulate_rdpmc)(struct kvm_vcpu *vcpu, unsigned int idx,
+			     u64 *data);
 	struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, u32 msr);
 	int (*check_rdpmc_early)(struct kvm_vcpu *vcpu, unsigned int idx);
 	bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr);
arch/x86/kvm/svm/pmu.c +9 -4
index c18286545a7a..0517fd4bbcd7 100644
@@ -84,10 +84,15 @@ 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 +307,7 @@ static bool amd_pmc_is_disabled_in_current_mode(struct kvm_pmc *pmc)
 }
 
 struct kvm_pmu_ops amd_pmu_ops __initdata = {
-	.rdpmc_ecx_to_pmc = amd_rdpmc_ecx_to_pmc,
+	.emulate_rdpmc = amd_emulate_rdpmc,
 	.msr_idx_to_pmc = amd_msr_idx_to_pmc,
 	.check_rdpmc_early = amd_check_rdpmc_early,
 	.is_valid_msr = amd_is_valid_msr,
arch/x86/kvm/vmx/pmu_intel.c +12 -13
index 225afd3937c3..080677372c9b 100644
@@ -84,14 +84,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
@@ -104,7 +103,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
@@ -118,23 +119,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)
@@ -865,7 +864,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,
Reviewed-by on v6 6/8
#29 lore raw

Re: [PATCH v6 6/8] KVM: x86/pmu: Move RDPMC emulation into per-vendor callbacks

In reply to Zide Chen
Quoted 11 lines · Zide Chen

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 don't 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.

Quoted 6 lines

Also drop the redundant bitmask in intel_emulate_rdpmc() since pmc_read_counter() already applies the counter's bit-width mask.

No functional change intended.

author
#30 lore raw

PATCH v6 7/8 KVM: x86/pmu: Emulate RDPMC on performance metrics

In reply to Zide Chen

If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #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 2000H.

For simplicity, Metrics Clear Mode is not supported.

---
v6:
- Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc().
- Reject non-zero index.
v5:
- new patch.

---
 arch/x86/kvm/pmu.c           |  7 +++++++
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/kvm/pmu.c +7 -0
index 8ef2d4761790..04b9c840f218 100644
@@ -806,6 +806,12 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+	return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+		!kvm_vcpu_has_perf_metrics(vcpu);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -818,6 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 		return true;
 
 	return kvm_need_any_pmc_intercept(vcpu) ||
+	       kvm_need_perf_metrics_intercept(vcpu) ||
 	       pmu->counter_bitmask[KVM_PMC_GP] != (BIT_ULL(kvm_host_pmu.bit_width_gp) - 1) ||
 	       pmu->counter_bitmask[KVM_PMC_FIXED] != (BIT_ULL(kvm_host_pmu.bit_width_fixed) - 1);
 }
arch/x86/kvm/vmx/pmu_intel.c +14 -0
index 080677372c9b..93b5a8360377 100644
@@ -30,6 +30,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)
@@ -124,6 +125,19 @@ static int intel_emulate_rdpmc(struct kvm_vcpu *vcpu, unsigned int idx,
 		counters = pmu->gp_counters;
 		num_counters = pmu->nr_arch_gp_counters;
 		break;
+	case INTEL_RDPMC_METRICS:
+		if (!kvm_vcpu_has_perf_metrics(vcpu))
+			return 1;
+
+		/*
+		 * The index in ECX[15:0] is implementation specific, but no
+		 * platform currently supports a non-zero index.
+		 */
+		if (idx)
+			return 1;
+
+		*data = pmu->perf_metrics;
+		return 0;
 	default:
 		return 1;
 	}
Signature · 1 line
2.54.0
Reviewed-by on v6 7/8
#31 lore raw

Re: [PATCH v6 7/8] KVM: x86/pmu: Emulate RDPMC on performance metrics

Quoted 78 lines · Zide Chen

If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #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 2000H.

For simplicity, Metrics Clear Mode is not supported.

---
v6:
- Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc().
- Reject non-zero index.
v5:
- new patch.

---
 arch/x86/kvm/pmu.c           |  7 +++++++
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/kvm/pmu.c +7 -0
index 8ef2d4761790..04b9c840f218 100644
@@ -806,6 +806,12 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+	return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+		!kvm_vcpu_has_perf_metrics(vcpu);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -818,6 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 		return true;
 
 	return kvm_need_any_pmc_intercept(vcpu) ||
+	       kvm_need_perf_metrics_intercept(vcpu) ||
 	       pmu->counter_bitmask[KVM_PMC_GP] != (BIT_ULL(kvm_host_pmu.bit_width_gp) - 1) ||
 	       pmu->counter_bitmask[KVM_PMC_FIXED] != (BIT_ULL(kvm_host_pmu.bit_width_fixed) - 1);
 }
arch/x86/kvm/vmx/pmu_intel.c +14 -0
index 080677372c9b..93b5a8360377 100644
@@ -30,6 +30,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)
@@ -124,6 +125,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;
 	}
#32 lore raw

Re: [PATCH v6 7/8] KVM: x86/pmu: Emulate RDPMC on performance metrics

In reply to Zide Chen
Quoted 40 lines · Zide Chen

If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #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 2000H.

For simplicity, Metrics Clear Mode is not supported.

---
v6:
- Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc().
- Reject non-zero index.
v5:
- new patch.

---
 arch/x86/kvm/pmu.c           |  7 +++++++
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/kvm/pmu.c +6 -0
index 8ef2d4761790..04b9c840f218 100644
@@ -806,6 +806,12 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+       return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+               !kvm_vcpu_has_perf_metrics(vcpu);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);

arch/x86/kvm/pmu.c bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) · line 824

Quoted 1 line
arch/x86/kvm/pmu.c +0 -0
@@ -818,6 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
                return true;

I know we have a strong disagreement here, but I really feel that this must be secure and future-proof out of the box.
What if we added a module parameter, enable_rdpmc_passthrough, which defaults to false?

Then:

if (!enable_rdpmc_passthrough)

        return true;

Is that a reasonable compromise?

Quoted 5 lines
arch/x86/kvm/pmu.c +1 -0
        return kvm_need_any_pmc_intercept(vcpu) ||
+              kvm_need_perf_metrics_intercept(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);
 }
#33 lore raw

Re: [PATCH v6 7/8] KVM: x86/pmu: Emulate RDPMC on performance metrics

Quoted 46 lines · Jim Mattson
Quoted 39 lines · Zide Chen

If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #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 2000H.

For simplicity, Metrics Clear Mode is not supported.

---
v6:
- Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc().
- Reject non-zero index.
v5:
- new patch.

---
 arch/x86/kvm/pmu.c           |  7 +++++++
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/kvm/pmu.c +6 -0
index 8ef2d4761790..04b9c840f218 100644
@@ -806,6 +806,12 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+       return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+               !kvm_vcpu_has_perf_metrics(vcpu);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);

arch/x86/kvm/pmu.c bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) · line 824

Quoted 1 line
arch/x86/kvm/pmu.c +0 -0
@@ -818,6 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
                return true;

I know we have a strong disagreement here, but I really feel that this must be secure and future-proof out of the box.
What if we added a module parameter, enable_rdpmc_passthrough, which defaults to false?

Quoted 6 lines

Then:

if (!enable_rdpmc_passthrough)

        return true;

Is that a reasonable compromise?

Yes, this is an option. Another option is to add a helper to explicitly tell which platforms are secure and can pass-through rdpmc. Maybe like this,

static bool intel_pmu_can_passthrough_rdpmc()
{
    switch (boot_cpu_data.x86_vfm) {
    case INTEL_ICELAKE_X:
    case INTEL_SAPPHIRERAPIDS_X:
    case INTEL_EMERALDRAPIDS_X:
    case INTEL_GRANITERAPIDS_X:
    ... ...
        return true;
    default:
        return false;
    }

    return false;
}

Then rdpmc can be passed through by default on these known secure platforms. For any new platforms, the rdpmc would be intercepted out of box until we confirm it's secured and explicitly support them.

How about this? At least for me, it looks like an overkill to disable rdpmc passthrough unconditionally on these known secure platforms.

Quoted 6 lines
Quoted 6 lines
arch/x86/kvm/pmu.c +1 -0
        return kvm_need_any_pmc_intercept(vcpu) ||
+              kvm_need_perf_metrics_intercept(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);
 }
#34 lore raw

Re: [PATCH v6 7/8] KVM: x86/pmu: Emulate RDPMC on performance metrics

Quoted 75 lines · Dapeng
Quoted 46 lines · Jim Mattson
Quoted 39 lines · Zide Chen

If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #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 2000H.

For simplicity, Metrics Clear Mode is not supported.

---
v6:
- Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc().
- Reject non-zero index.
v5:
- new patch.

---
 arch/x86/kvm/pmu.c           |  7 +++++++
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/kvm/pmu.c +6 -0
index 8ef2d4761790..04b9c840f218 100644
@@ -806,6 +806,12 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+       return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+               !kvm_vcpu_has_perf_metrics(vcpu);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);

arch/x86/kvm/pmu.c bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) · line 824

Quoted 1 line
arch/x86/kvm/pmu.c +0 -0
@@ -818,6 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
                return true;

I know we have a strong disagreement here, but I really feel that this must be secure and future-proof out of the box.
What if we added a module parameter, enable_rdpmc_passthrough, which defaults to false?

Quoted 6 lines

Then:

if (!enable_rdpmc_passthrough)

        return true;

Is that a reasonable compromise?

Yes, this is an option. Another option is to add a helper to explicitly tell which platforms are secure and can pass-through rdpmc. Maybe like this,

static bool intel_pmu_can_passthrough_rdpmc()

{
    switch (boot_cpu_data.x86_vfm) {
    case INTEL_ICELAKE_X:
    case INTEL_SAPPHIRERAPIDS_X:
    case INTEL_EMERALDRAPIDS_X:
    case INTEL_GRANITERAPIDS_X:
    ... ...
        return true;
    default:
        return false;
    }

    return false;
}
Quoted 6 lines

Then rdpmc can be passed through by default on these known secure platforms. For any new platforms, the rdpmc would be intercepted out of box until we confirm it's secured and explicitly support them.

How about this? At least for me, it looks like an overkill to disable rdpmc passthrough unconditionally on these known secure platforms.

The F/M match would only apply on bare metal, since you can't trust F/M in a VM.

What if we combine this with the module parameter, but now the module parameter defaults to true? Somewhere in module load, we have:

if (boot_cpu_has(X86_FEATURE_HYPERVISOR) || !intel_pmu_can_passthrough_rdpmc())

        enable_rdpmc_passthrough = false;
#35 lore raw

Re: [PATCH v6 7/8] KVM: x86/pmu: Emulate RDPMC on performance metrics

Quoted 80 lines · Jim Mattson
Quoted 73 lines · Dapeng
Quoted 46 lines · Jim Mattson
Quoted 39 lines · Zide Chen

If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #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 2000H.

For simplicity, Metrics Clear Mode is not supported.

---
v6:
- Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc().
- Reject non-zero index.
v5:
- new patch.

---
 arch/x86/kvm/pmu.c           |  7 +++++++
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/kvm/pmu.c +6 -0
index 8ef2d4761790..04b9c840f218 100644
@@ -806,6 +806,12 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+       return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+               !kvm_vcpu_has_perf_metrics(vcpu);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);

arch/x86/kvm/pmu.c bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) · line 824

Quoted 1 line
arch/x86/kvm/pmu.c +0 -0
@@ -818,6 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
                return true;

I know we have a strong disagreement here, but I really feel that this must be secure and future-proof out of the box.
What if we added a module parameter, enable_rdpmc_passthrough, which defaults to false?

Quoted 6 lines

Then:

if (!enable_rdpmc_passthrough)

        return true;

Is that a reasonable compromise?

Yes, this is an option. Another option is to add a helper to explicitly tell which platforms are secure and can pass-through rdpmc. Maybe like this,

static bool intel_pmu_can_passthrough_rdpmc()

{
    switch (boot_cpu_data.x86_vfm) {
    case INTEL_ICELAKE_X:
    case INTEL_SAPPHIRERAPIDS_X:
    case INTEL_EMERALDRAPIDS_X:
    case INTEL_GRANITERAPIDS_X:
    ... ...
        return true;
    default:
        return false;
    }

    return false;
}
Quoted 6 lines

Then rdpmc can be passed through by default on these known secure platforms. For any new platforms, the rdpmc would be intercepted out of box until we confirm it's secured and explicitly support them.

How about this? At least for me, it looks like an overkill to disable rdpmc passthrough unconditionally on these known secure platforms.

Quoted 7 lines

The F/M match would only apply on bare metal, since you can't trust F/M in a VM.

What if we combine this with the module parameter, but now the module parameter defaults to true? Somewhere in module load, we have:

if (boot_cpu_has(X86_FEATURE_HYPERVISOR) || !intel_pmu_can_passthrough_rdpmc())

        enable_rdpmc_passthrough = false;

It looks good to me. :)

#36 lore raw

Re: [PATCH v6 7/8] KVM: x86/pmu: Emulate RDPMC on performance metrics

Quoted 85 lines · Dapeng
Quoted 82 lines · Jim Mattson
Quoted 73 lines · Dapeng
Quoted 46 lines · Jim Mattson
Quoted 39 lines · Zide Chen

If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #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 2000H.

For simplicity, Metrics Clear Mode is not supported.

---
v6:
- Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc().
- Reject non-zero index.
v5:
- new patch.

---
 arch/x86/kvm/pmu.c           |  7 +++++++
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/kvm/pmu.c +6 -0
index 8ef2d4761790..04b9c840f218 100644
@@ -806,6 +806,12 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+       return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+               !kvm_vcpu_has_perf_metrics(vcpu);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);

arch/x86/kvm/pmu.c bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) · line 824

Quoted 1 line
arch/x86/kvm/pmu.c +0 -0
@@ -818,6 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
                return true;

I know we have a strong disagreement here, but I really feel that this must be secure and future-proof out of the box.
What if we added a module parameter, enable_rdpmc_passthrough, which defaults to false?

Quoted 6 lines

Then:

if (!enable_rdpmc_passthrough)

        return true;

Is that a reasonable compromise?

Yes, this is an option. Another option is to add a helper to explicitly tell which platforms are secure and can pass-through rdpmc. Maybe like this,

static bool intel_pmu_can_passthrough_rdpmc()

{
    switch (boot_cpu_data.x86_vfm) {
    case INTEL_ICELAKE_X:
    case INTEL_SAPPHIRERAPIDS_X:
    case INTEL_EMERALDRAPIDS_X:
    case INTEL_GRANITERAPIDS_X:
    ... ...
        return true;
    default:
        return false;
    }

    return false;
}
Quoted 6 lines

Then rdpmc can be passed through by default on these known secure platforms. For any new platforms, the rdpmc would be intercepted out of box until we confirm it's secured and explicitly support them.

How about this? At least for me, it looks like an overkill to disable rdpmc passthrough unconditionally on these known secure platforms.

The F/M match would only apply on bare metal, since you can't trust F/M in a VM.

Quoted 7 lines
Quoted 5 lines

What if we combine this with the module parameter, but now the module parameter defaults to true? Somewhere in module load, we have:

if (boot_cpu_has(X86_FEATURE_HYPERVISOR) || !intel_pmu_can_passthrough_rdpmc())

        enable_rdpmc_passthrough = false;

It looks good to me. :)

On second thought, it doesn't really matter if L0 has lied to L1 about F/M. If the hardware supports more RDPMC types than the L1 virtual CPU does, L0 must intercept and emulate RDPMC for vmcs01. Even if L1 decides to pass through RDPMC to L2, L0 will still intercept and emulate RDPMC for vmcs02.

So, we can drop the module parameter and just use
intel_pmu_can_passthrough_rdpmc().

To keep the list short, I assume that any CPU with only three fixed counters will return true.

#37 lore raw

Re: [PATCH v6 7/8] KVM: x86/pmu: Emulate RDPMC on performance metrics

Quoted 96 lines · Jim Mattson
Quoted 81 lines · Dapeng
Quoted 79 lines · Jim Mattson
Quoted 78 lines · Dapeng
Quoted 46 lines · Jim Mattson
Quoted 39 lines · Zide Chen

If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #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 2000H.

For simplicity, Metrics Clear Mode is not supported.

---
v6:
- Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc().
- Reject non-zero index.
v5:
- new patch.

---
 arch/x86/kvm/pmu.c           |  7 +++++++
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/kvm/pmu.c +6 -0
index 8ef2d4761790..04b9c840f218 100644
@@ -806,6 +806,12 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+       return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+               !kvm_vcpu_has_perf_metrics(vcpu);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);

arch/x86/kvm/pmu.c bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) · line 824

Quoted 1 line
arch/x86/kvm/pmu.c +0 -0
@@ -818,6 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
                return true;

I know we have a strong disagreement here, but I really feel that this must be secure and future-proof out of the box.
What if we added a module parameter, enable_rdpmc_passthrough, which defaults to false?

Quoted 6 lines

Then:

if (!enable_rdpmc_passthrough)

        return true;

Is that a reasonable compromise?

Yes, this is an option. Another option is to add a helper to explicitly tell which platforms are secure and can pass-through rdpmc. Maybe like this,

static bool intel_pmu_can_passthrough_rdpmc()

{
    switch (boot_cpu_data.x86_vfm) {
    case INTEL_ICELAKE_X:
    case INTEL_SAPPHIRERAPIDS_X:
    case INTEL_EMERALDRAPIDS_X:
    case INTEL_GRANITERAPIDS_X:
    ... ...
        return true;
    default:
        return false;
    }

    return false;
}

Then rdpmc can be passed through by default on these known secure platforms. For any new platforms, the rdpmc would be intercepted out of box until we confirm it's secured and explicitly support them.

How about this? At least for me, it looks like an overkill to disable rdpmc passthrough unconditionally on these known secure platforms.

Quoted 8 lines
Quoted 7 lines

The F/M match would only apply on bare metal, since you can't trust F/M in a VM.

What if we combine this with the module parameter, but now the module parameter defaults to true? Somewhere in module load, we have:

if (boot_cpu_has(X86_FEATURE_HYPERVISOR) || !intel_pmu_can_passthrough_rdpmc())

        enable_rdpmc_passthrough = false;

It looks good to me. :)

On second thought, it doesn't really matter if L0 has lied to L1 about F/M. If the hardware supports more RDPMC types than the L1 virtual CPU does, L0 must intercept and emulate RDPMC for vmcs01. Even if L1 decides to pass through RDPMC to L2, L0 will still intercept and emulate RDPMC for vmcs02.

Quoted 5 lines

So, we can drop the module parameter and just use
intel_pmu_can_passthrough_rdpmc().

To keep the list short, I assume that any CPU with only three fixed counters will return true.

Sounds good.

Quoted 1 line
author
#38 lore raw

Re: [PATCH v6 7/8] KVM: x86/pmu: Emulate RDPMC on performance metrics

In reply to Jim Mattson
Quoted 94 lines · Jim Mattson
Quoted 85 lines · Dapeng
Quoted 82 lines · Jim Mattson
Quoted 73 lines · Dapeng
Quoted 46 lines · Jim Mattson
Quoted 39 lines · Zide Chen

If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #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 2000H.

For simplicity, Metrics Clear Mode is not supported.

---
v6:
- Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc().
- Reject non-zero index.
v5:
- new patch.

---
 arch/x86/kvm/pmu.c           |  7 +++++++
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/kvm/pmu.c +6 -0
index 8ef2d4761790..04b9c840f218 100644
@@ -806,6 +806,12 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+       return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+               !kvm_vcpu_has_perf_metrics(vcpu);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);

arch/x86/kvm/pmu.c bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) · line 824

Quoted 1 line
arch/x86/kvm/pmu.c +0 -0
@@ -818,6 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
                return true;

I know we have a strong disagreement here, but I really feel that this must be secure and future-proof out of the box.
What if we added a module parameter, enable_rdpmc_passthrough, which defaults to false?

Quoted 6 lines

Then:

if (!enable_rdpmc_passthrough)

        return true;

Is that a reasonable compromise?

Yes, this is an option. Another option is to add a helper to explicitly tell which platforms are secure and can pass-through rdpmc. Maybe like this,

static bool intel_pmu_can_passthrough_rdpmc()

{
    switch (boot_cpu_data.x86_vfm) {
    case INTEL_ICELAKE_X:
    case INTEL_SAPPHIRERAPIDS_X:
    case INTEL_EMERALDRAPIDS_X:
    case INTEL_GRANITERAPIDS_X:
    ... ...
        return true;
    default:
        return false;
    }

    return false;
}
Quoted 6 lines

Then rdpmc can be passed through by default on these known secure platforms. For any new platforms, the rdpmc would be intercepted out of box until we confirm it's secured and explicitly support them.

How about this? At least for me, it looks like an overkill to disable rdpmc passthrough unconditionally on these known secure platforms.

The F/M match would only apply on bare metal, since you can't trust F/M in a VM.

Quoted 7 lines
Quoted 5 lines

What if we combine this with the module parameter, but now the module parameter defaults to true? Somewhere in module load, we have:

if (boot_cpu_has(X86_FEATURE_HYPERVISOR) || !intel_pmu_can_passthrough_rdpmc())

        enable_rdpmc_passthrough = false;

It looks good to me. :)

Quoted 8 lines

On second thought, it doesn't really matter if L0 has lied to L1 about F/M. If the hardware supports more RDPMC types than the L1 virtual CPU does, L0 must intercept and emulate RDPMC for vmcs01. Even if L1 decides to pass through RDPMC to L2, L0 will still intercept and emulate RDPMC for vmcs02.

So, we can drop the module parameter and just use
intel_pmu_can_passthrough_rdpmc().

Thanks, will post a new version based on this.

Quoted 2 lines

To keep the list short, I assume that any CPU with only three fixed counters will return true.

Will do some study on the available CPUs.

author
#39 lore raw

Re: [PATCH v6 7/8] KVM: x86/pmu: Emulate RDPMC on performance metrics

In reply to Jim Mattson
Quoted 100 lines · Jim Mattson
Quoted 85 lines · Dapeng
Quoted 82 lines · Jim Mattson
Quoted 73 lines · Dapeng
Quoted 46 lines · Jim Mattson
Quoted 39 lines · Zide Chen

If the host has the PERF_METRICS capability but it's not present on the guest, RDPMC interception must be enabled and KVM should inject an #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 2000H.

For simplicity, Metrics Clear Mode is not supported.

---
v6:
- Merge kvm_pmu_rdpmc_metrics() into intel_emulate_rdpmc().
- Reject non-zero index.
v5:
- new patch.

---
 arch/x86/kvm/pmu.c           |  7 +++++++
 arch/x86/kvm/vmx/pmu_intel.c | 14 ++++++++++++++
 2 files changed, 21 insertions(+)
arch/x86/kvm/pmu.c +6 -0
index 8ef2d4761790..04b9c840f218 100644
@@ -806,6 +806,12 @@ bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
 }
 EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_need_perf_global_ctrl_intercept);
 
+static bool kvm_need_perf_metrics_intercept(struct kvm_vcpu *vcpu)
+{
+       return (kvm_host.perf_capabilities & PERF_CAP_PERF_METRICS) &&
+               !kvm_vcpu_has_perf_metrics(vcpu);
+}
+
 bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
 {
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);

arch/x86/kvm/pmu.c bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu) · line 824

Quoted 1 line
arch/x86/kvm/pmu.c +0 -0
@@ -818,6 +824,7 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
                return true;

I know we have a strong disagreement here, but I really feel that this must be secure and future-proof out of the box.
What if we added a module parameter, enable_rdpmc_passthrough, which defaults to false?

Quoted 6 lines

Then:

if (!enable_rdpmc_passthrough)

        return true;

Is that a reasonable compromise?

Yes, this is an option. Another option is to add a helper to explicitly tell which platforms are secure and can pass-through rdpmc. Maybe like this,

static bool intel_pmu_can_passthrough_rdpmc()

{
    switch (boot_cpu_data.x86_vfm) {
    case INTEL_ICELAKE_X:
    case INTEL_SAPPHIRERAPIDS_X:
    case INTEL_EMERALDRAPIDS_X:
    case INTEL_GRANITERAPIDS_X:
    ... ...
        return true;
    default:
        return false;
    }

    return false;
}
Quoted 6 lines

Then rdpmc can be passed through by default on these known secure platforms. For any new platforms, the rdpmc would be intercepted out of box until we confirm it's secured and explicitly support them.

How about this? At least for me, it looks like an overkill to disable rdpmc passthrough unconditionally on these known secure platforms.

The F/M match would only apply on bare metal, since you can't trust F/M in a VM.

Quoted 7 lines
Quoted 5 lines

What if we combine this with the module parameter, but now the module parameter defaults to true? Somewhere in module load, we have:

if (boot_cpu_has(X86_FEATURE_HYPERVISOR) || !intel_pmu_can_passthrough_rdpmc())

        enable_rdpmc_passthrough = false;

It looks good to me. :)

On second thought, it doesn't really matter if L0 has lied to L1 about F/M. If the hardware supports more RDPMC types than the L1 virtual CPU does, L0 must intercept and emulate RDPMC for vmcs01. Even if L1 decides to pass through RDPMC to L2, L0 will still intercept and emulate RDPMC for vmcs02.

Quoted 5 lines

So, we can drop the module parameter and just use
intel_pmu_can_passthrough_rdpmc().

To keep the list short, I assume that any CPU with only three fixed counters will return true.

How about this to keep the list short? To be conservative, don't assume future PMUv6 CPUs won't introduce new RDPMC types.

arch/x86/kvm/pmu.c +0 -0
index 9dc453598d06..b2351b764788 100644
@@ -817,6 +817,31 @@ static bool kvm_need_perf_metrics_intercept(struct

kvm_vcpu *vcpu)

                !kvm_vcpu_has_perf_metrics(vcpu);

}

+static bool kvm_rdpmc_encoding_supported(void)
+{
+ /* KVM understands all RDPMC encodings prior to PMUv6. */
+ if (kvm_host_pmu.version < 6)
+ return true;
+
+ /*
+ * Future PMUv6 implementations and future PMU versions require
RDPMC
+ * interception until their RDPMC encodings are audited and
supported
+ * by KVM.
+ */

+       switch (boot_cpu_data.x86_vfm) {
+       case INTEL_ATOM_DARKMONT_X:
+       case INTEL_NOVALAKE:
+       case INTEL_NOVALAKE_L:
+       case INTEL_PANTHERLAKE_L:
+       case INTEL_PANTHERLAKE_R:
+       case INTEL_WILDCATLAKE_L:
+       case INTEL_LUNARLAKE_M:

+ return true;
+ default:
+ return false;
+ }
+}
+
bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)

 {
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -828,6 +853,9 @@ bool kvm_need_rdpmc_intercept(struct kvm_vcpu *vcpu)
        if (enable_vmware_backdoor)
                return true;

+ if (!kvm_rdpmc_encoding_supported())
+ return true;
+

        return kvm_need_any_pmc_intercept(vcpu) ||
               kvm_need_perf_metrics_intercept(vcpu) ||
               pmu->counter_bitmask[KVM_PMC_GP] !=

(BIT_ULL(kvm_host_pmu.bit_width_gp) - 1) ||

author
#40 lore raw

PATCH v6 8/8 KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

In reply to Zide Chen

Add a test case to exercise IA32_PERF_METRICS, i.e. architectural support for Topdown (TMA) Level 1 metrics, enumerated by IA32_PERF_CAPABILITIES[15].

Only check for non-zero metrics, as they are derived and depend on the workload, CPU model, and host scheduling, making precise expectations fragile.

Extend the PMU selftest to cover Intel fixed counter 3 by bumping MAX_NR_FIXED_COUNTERS to 4 and validating basic functionality.

---
v6:
- Move perf metrics test out of test_arch_events(); it doesn't belong there, and this also avoids redundant runs of perf metrics test.
- Correct the +/-3 error margin.
v3:
- Slightly reword comment to explain the sum of topdown metrics is close to 100%.
- Change abs() with explicit bounds (sum >= 0xfd && sum <= 0x102) for better readability.
v2:
- New patch.

---
 tools/arch/x86/include/asm/msr-index.h        |  1 +
 tools/testing/selftests/kvm/include/x86/pmu.h |  3 +
 .../selftests/kvm/x86/pmu_counters_test.c     | 94 ++++++++++++++++++-
 3 files changed, 93 insertions(+), 5 deletions(-)
tools/arch/x86/include/asm/msr-index.h +1 -0
index eff29645719b..e7745e2cd543 100644
@@ -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 | \
tools/testing/selftests/kvm/include/x86/pmu.h +3 -0
index 608ed83d7c6a..6c19503e0bb7 100644
@@ -52,6 +52,9 @@
 /* Fixed PMC controls, Intel only. */
 #define FIXED_PMC_GLOBAL_CTRL_ENABLE(_idx)	BIT_ULL((32 + (_idx)))
 
+/* PERF_METRICS enable, Intel only. */
+#define PERF_METRICS_GLOBAL_CTRL_ENABLE		BIT_ULL(48)
+
 #define FIXED_PMC_KERNEL			BIT_ULL(0)
 #define FIXED_PMC_USER				BIT_ULL(1)
 #define FIXED_PMC_ANYTHREAD			BIT_ULL(2)
tools/testing/selftests/kvm/x86/pmu_counters_test.c +89 -5

149 lines not on this page · raw message

Signature · 1 line
2.54.0
#41 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The newly added test for PERF_METRICS writes a non-zero value (0xdeaddead) to MSR_PERF_METRICS…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The newly added test for PERF_METRICS writes a non-zero value (0xdeaddead) to MSR_PERF_METRICS and expects to read it back verbatim, which violates Intel's architectural rules and will cause a #GP exception on hardware. --

commit 35ae6d43235ee910691fc703d1aee89a9dbe653a
Author: Zide Chen <[email protected]>

KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

This patch adds a test case to exercise IA32_PERF_METRICS, verifying architectural support for Topdown Level 1 metrics. It also extends the PMU selftest to cover Intel fixed counter 3 by bumping the max fixed counters and validating basic functionality.

Quoted 3 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +0 -0

[ ... ]

Quoted 6 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +2 -0
@@ -318,6 +322,75 @@ static void guest_test_arch_event(u8 idx)
 				FIXED_PMC_GLOBAL_CTRL_ENABLE(i));
 }
 
+static void __guest_test_perf_metrics(void)
+{

[ ... ]

tools/testing/selftests/kvm/x86/pmu_counters_test.c static void guest_test_arch_event(u8 idx) · line 373

Quoted 7 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +7 -0
+	/* Sanity check after PERF_METRICS disabled. */
+	__asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
+	GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), metrics);
+	wrmsr(MSR_PERF_METRICS, 0xdeaddead);
+
+	GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), 0xdeaddead);
+}

[Severity: Medium]
Will this write to MSR_PERF_METRICS cause a #GP exception on hardware?

According to the Intel SDM, software should only write 0 to clear the metrics, and writing a non-zero value like 0xdeaddead may trigger a general protection fault.

If this occurs on a host where MSR_PERF_METRICS writes are passed through to the physical CPU, could it cause the guest to crash with an unhandled #GP(0) exception and fail the selftest?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
#42 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

In reply to Zide Chen
Quoted 110 lines · Zide Chen

Add a test case to exercise IA32_PERF_METRICS, i.e. architectural support for Topdown (TMA) Level 1 metrics, enumerated by IA32_PERF_CAPABILITIES[15].

Only check for non-zero metrics, as they are derived and depend on the workload, CPU model, and host scheduling, making precise expectations fragile.

Extend the PMU selftest to cover Intel fixed counter 3 by bumping MAX_NR_FIXED_COUNTERS to 4 and validating basic functionality.

---
v6:
- Move perf metrics test out of test_arch_events(); it doesn't belong there, and this also avoids redundant runs of perf metrics test.
- Correct the +/-3 error margin.
v3:
- Slightly reword comment to explain the sum of topdown metrics is close to 100%.
- Change abs() with explicit bounds (sum >= 0xfd && sum <= 0x102) for better readability.
v2:
- New patch.

---
 tools/arch/x86/include/asm/msr-index.h        |  1 +
 tools/testing/selftests/kvm/include/x86/pmu.h |  3 +
 .../selftests/kvm/x86/pmu_counters_test.c     | 94 ++++++++++++++++++-
 3 files changed, 93 insertions(+), 5 deletions(-)
tools/arch/x86/include/asm/msr-index.h +1 -0
index eff29645719b..e7745e2cd543 100644
@@ -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 | \
tools/testing/selftests/kvm/include/x86/pmu.h +3 -0
index 608ed83d7c6a..6c19503e0bb7 100644
@@ -52,6 +52,9 @@
 /* Fixed PMC controls, Intel only. */
 #define FIXED_PMC_GLOBAL_CTRL_ENABLE(_idx)	BIT_ULL((32 + (_idx)))
 
+/* PERF_METRICS enable, Intel only. */
+#define PERF_METRICS_GLOBAL_CTRL_ENABLE		BIT_ULL(48)
+
 #define FIXED_PMC_KERNEL			BIT_ULL(0)
 #define FIXED_PMC_USER				BIT_ULL(1)
 #define FIXED_PMC_ANYTHREAD			BIT_ULL(2)
tools/testing/selftests/kvm/x86/pmu_counters_test.c +21 -4
index dc6afac3aa91..38057754e024 100644
@@ -6,6 +6,7 @@
 
 #include "pmu.h"
 #include "processor.h"
+#include <linux/bitfield.h>
 
 /* Number of iterations of the loop for the guest measurement payload. */
 #define NUM_LOOPS			10
@@ -241,17 +242,20 @@ do {										\
 	);									\
 } while (0)
 
-#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP)		\
+#define GUEST_RUN_PAYLOAD(_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);		\
-										\
+} while (0)
+
+#define GUEST_TEST_EVENT(_idx, _pmc, _pmc_msr, _ctrl_msr, _value, FEP)		\
+do {										\
+	wrmsr(_pmc_msr, 0);							\
+	GUEST_RUN_PAYLOAD(_ctrl_msr, _value, FEP);				\
 	guest_assert_event_count(_idx, _pmc, _pmc_msr);				\
 } while (0)
 
@@ -318,6 +322,75 @@ static void guest_test_arch_event(u8 idx)
 				FIXED_PMC_GLOBAL_CTRL_ENABLE(i));
 }
 
+static void __guest_test_perf_metrics(void)
+{
+	int retiring, bad_spec, fe_bound, be_bound, sum;
+	u64 global_ctrl, metrics;
+
+	if ((guest_get_pmu_version() < 2) ||	/* Does guest have GLOBAL_CTRL? */
+	    !this_cpu_has(X86_FEATURE_PDCM) ||
+	    !(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS))
+		return;
+
+	wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+	wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0);
+	wrmsr(MSR_PERF_METRICS, 0);

tools/testing/selftests/kvm/x86/pmu_counters_test.c static void guest_test_arch_event(u8 idx) · line 347

Quoted 10 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +10 -0
+
+	/* Enable fixed ctr3 (TOPDOWN.SLOTS) and PERF_METRICS. */
+	wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(3, FIXED_PMC_KERNEL));
+	global_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(3) |
+		      PERF_METRICS_GLOBAL_CTRL_ENABLE;
+
+	GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, "");
+
+	/* Check test results. */
+	metrics = rdmsr(MSR_PERF_METRICS);

Could we use rdpmc instead of rdmsr here? rdpmc is a preferred way to read counter value.

Quoted 12 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +12 -0
+	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(metrics, 0ULL);
+	GUEST_ASSERT_NE(retiring, 0ULL);
+

tools/testing/selftests/kvm/x86/pmu_counters_test.c static void guest_test_arch_event(u8 idx) · line 369

Quoted 10 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +10 -0
+	/*
+	 * Each level-1 topdown metrics is an integer fraction of 255.
+	 * An +/-3 error margin is chosen for a loose sanity check.
+	 */
+	sum = retiring + bad_spec + fe_bound + be_bound;
+	GUEST_ASSERT(sum >= 0xfc && sum <= 0x102);
+
+	/* Sanity check after PERF_METRICS disabled. */
+	__asm__ __volatile__("loop ." : "+c"((int){NUM_LOOPS}));
+	GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), metrics);

Better rdpmc as well here.

tools/testing/selftests/kvm/x86/pmu_counters_test.c static void guest_test_arch_event(u8 idx) · line 372

Quoted 3 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +3 -0
+	wrmsr(MSR_PERF_METRICS, 0xdeaddead);
+
+	GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), 0xdeaddead);

We can still use rdmsr here, then rdpmc and wrmsr/rdmsr for PERF_METRICS are all validated.

Thanks.

Quoted 72 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +33 -1
+}
+
+static void guest_test_perf_metrics(void)
+{
+	__guest_test_perf_metrics();
+	GUEST_DONE();
+}
+
+static void test_perf_metrics(u8 pmu_version, u64 perf_capabilities)
+{
+	struct kvm_vcpu *vcpu;
+	struct kvm_vm *vm;
+
+	vm = pmu_vm_create_with_one_vcpu(&vcpu, guest_test_perf_metrics,
+					 pmu_version, perf_capabilities);
+
+	run_vcpu(vcpu);
+
+	kvm_vm_free(vm);
+}
+
 static void guest_test_arch_events(void)
 {
 	u8 i;
@@ -361,7 +434,7 @@ static void test_arch_events(u8 pmu_version, u64 perf_capabilities,
  * other than PMCs in the future.
  */
 #define MAX_NR_GP_COUNTERS	8
-#define MAX_NR_FIXED_COUNTERS	3
+#define MAX_NR_FIXED_COUNTERS	4
 
 #define GUEST_ASSERT_PMC_MSR_ACCESS(insn, msr, expect_gp, vector)		\
 __GUEST_ASSERT(expect_gp ? vector == GP_VECTOR : !vector,			\
@@ -585,6 +658,7 @@ static void test_intel_counters(void)
 	u8 nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
 	u8 nr_gp_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_GP_COUNTERS);
 	u8 pmu_version = kvm_cpu_property(X86_PROPERTY_PMU_VERSION);
+	u64 advertised_perf_caps = kvm_get_feature_msr(MSR_IA32_PERF_CAPABILITIES);
 	unsigned int i;
 	u8 v, j;
 	u32 k;
@@ -592,6 +666,7 @@ static void test_intel_counters(void)
 	const u64 perf_caps[] = {
 		0,
 		PMU_CAP_FW_WRITES,
+		PERF_CAP_PERF_METRICS,
 	};
 
 	/*
@@ -649,6 +724,10 @@ static void test_intel_counters(void)
 			if (!kvm_has_perf_caps && perf_caps[i])
 				continue;
 
+			/* Ignore unsupported features. */
+			if (perf_caps[i] & ~advertised_perf_caps)
+				continue;
+
 			pr_info("Testing arch events, PMU version %u, perf_caps = %lx\n",
 				v, perf_caps[i]);
 
@@ -675,6 +754,11 @@ static void test_intel_counters(void)
 				for (k = 0; k <= (BIT(nr_fixed_counters) - 1); k++)
 					test_fixed_counters(v, perf_caps[i], j, k);
 			}
+
+			pr_info("Testing Perf Metrics, PMU version %u, perf_caps = %lx\n",
+				v, perf_caps[i]);
+
+			test_perf_metrics(v, perf_caps[i]);
 		}
 	}
 }
#43 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Quoted 35 lines · Dapeng
Quoted 32 lines · Zide Chen

Add a test case to exercise IA32_PERF_METRICS, i.e. architectural support for Topdown (TMA) Level 1 metrics, enumerated by IA32_PERF_CAPABILITIES[15].

Only check for non-zero metrics, as they are derived and depend on the workload, CPU model, and host scheduling, making precise expectations fragile.

Extend the PMU selftest to cover Intel fixed counter 3 by bumping MAX_NR_FIXED_COUNTERS to 4 and validating basic functionality.

---
...

tools/testing/selftests/kvm/x86/pmu_counters_test.c +18 -0
+static void __guest_test_perf_metrics(void)
+{
+     int retiring, bad_spec, fe_bound, be_bound, sum;
+     u64 global_ctrl, metrics;
+
+     if ((guest_get_pmu_version() < 2) ||    /* Does guest have GLOBAL_CTRL? */
+         !this_cpu_has(X86_FEATURE_PDCM) ||
+         !(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS))
+             return;
+
+     wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+     wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0);
+     wrmsr(MSR_PERF_METRICS, 0);
+
+     /* Enable fixed ctr3 (TOPDOWN.SLOTS) and PERF_METRICS. */
+     wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(3, FIXED_PMC_KERNEL));
+     global_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(3) |
+                   PERF_METRICS_GLOBAL_CTRL_ENABLE;
Quoted 8 lines

tools/testing/selftests/kvm/x86/pmu_counters_test.c static void guest_test_arch_event(u8 idx) · line 347

Quoted 5 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +5 -0
+
+     GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, "");
+
+     /* Check test results. */
+     metrics = rdmsr(MSR_PERF_METRICS);

Could we use rdpmc instead of rdmsr here? rdpmc is a preferred way to read counter value.

This is in-guest code, so the unintercepted RDMSR will be much faster than the emulated RDPMC.

Should we rethink that preference, or add hardware support for selective RDPMC intercepts?

#44 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Quoted 42 lines · Jim Mattson
Quoted 33 lines · Dapeng
Quoted 31 lines · Zide Chen

Add a test case to exercise IA32_PERF_METRICS, i.e. architectural support for Topdown (TMA) Level 1 metrics, enumerated by IA32_PERF_CAPABILITIES[15].

Only check for non-zero metrics, as they are derived and depend on the workload, CPU model, and host scheduling, making precise expectations fragile.

Extend the PMU selftest to cover Intel fixed counter 3 by bumping MAX_NR_FIXED_COUNTERS to 4 and validating basic functionality.

---
...

tools/testing/selftests/kvm/x86/pmu_counters_test.c +17 -0
+static void __guest_test_perf_metrics(void)
+{
+     int retiring, bad_spec, fe_bound, be_bound, sum;
+     u64 global_ctrl, metrics;
+
+     if ((guest_get_pmu_version() < 2) ||    /* Does guest have GLOBAL_CTRL? */
+         !this_cpu_has(X86_FEATURE_PDCM) ||
+         !(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS))
+             return;
+
+     wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+     wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0);
+     wrmsr(MSR_PERF_METRICS, 0);
+
+     /* Enable fixed ctr3 (TOPDOWN.SLOTS) and PERF_METRICS. */
+     wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(3, FIXED_PMC_KERNEL));
+     global_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(3) |
Quoted 8 lines

tools/testing/selftests/kvm/x86/pmu_counters_test.c static void guest_test_arch_event(u8 idx) · line 347

Quoted 6 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +6 -0
+                   PERF_METRICS_GLOBAL_CTRL_ENABLE;
+
+     GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, "");
+
+     /* Check test results. */
+     metrics = rdmsr(MSR_PERF_METRICS);

Could we use rdpmc instead of rdmsr here? rdpmc is a preferred way to read counter value.

Quoted 5 lines

This is in-guest code, so the unintercepted RDMSR will be much faster than the emulated RDPMC.

Should we rethink that preference, or add hardware support for selective RDPMC intercepts?

Hmm, in current most cases, rdpmc and rdmsr should share consistent interception configuration for PERF_METRICS except host and guest have different counters bitmap.

Considering this is a test case, the test coverage should be more important than the performance, there should be at least a place to call rdpmc against the PERF_METRICS, otherwise, that path won't be validated.

author
#45 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Quoted 50 lines · Dapeng
Quoted 42 lines · Jim Mattson
Quoted 33 lines · Dapeng
Quoted 31 lines · Zide Chen

Add a test case to exercise IA32_PERF_METRICS, i.e. architectural support for Topdown (TMA) Level 1 metrics, enumerated by IA32_PERF_CAPABILITIES[15].

Only check for non-zero metrics, as they are derived and depend on the workload, CPU model, and host scheduling, making precise expectations fragile.

Extend the PMU selftest to cover Intel fixed counter 3 by bumping MAX_NR_FIXED_COUNTERS to 4 and validating basic functionality.

---
...

tools/testing/selftests/kvm/x86/pmu_counters_test.c +17 -0
+static void __guest_test_perf_metrics(void)
+{
+     int retiring, bad_spec, fe_bound, be_bound, sum;
+     u64 global_ctrl, metrics;
+
+     if ((guest_get_pmu_version() < 2) ||    /* Does guest have GLOBAL_CTRL? */
+         !this_cpu_has(X86_FEATURE_PDCM) ||
+         !(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS))
+             return;
+
+     wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+     wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0);
+     wrmsr(MSR_PERF_METRICS, 0);
+
+     /* Enable fixed ctr3 (TOPDOWN.SLOTS) and PERF_METRICS. */
+     wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(3, FIXED_PMC_KERNEL));
+     global_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(3) |
Quoted 8 lines

tools/testing/selftests/kvm/x86/pmu_counters_test.c static void guest_test_arch_event(u8 idx) · line 347

Quoted 6 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +6 -0
+                   PERF_METRICS_GLOBAL_CTRL_ENABLE;
+
+     GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, "");
+
+     /* Check test results. */
+     metrics = rdmsr(MSR_PERF_METRICS);

Could we use rdpmc instead of rdmsr here? rdpmc is a preferred way to read counter value.

Quoted 5 lines

This is in-guest code, so the unintercepted RDMSR will be much faster than the emulated RDPMC.

Should we rethink that preference, or add hardware support for selective RDPMC intercepts?

Quoted 7 lines

Hmm, in current most cases, rdpmc and rdmsr should share consistent interception configuration for PERF_METRICS except host and guest have different counters bitmap.

Considering this is a test case, the test coverage should be more important than the performance, there should be at least a place to call rdpmc against the PERF_METRICS, otherwise, that path won't be validated.

How about keeping rdmsr() in __guest_test_perf_metrics(), and using RDPMC in the sanity test?

@@ -369,7 +371,7 @@ static void __guest_test_perf_metrics(void)

        GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), metrics);
        wrmsr(MSR_PERF_METRICS, 0xdeaddead);

- GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), 0xdeaddead);
+ guest_test_rdpmc(INTEL_RDPMC_METRICS, true, 0xdeaddead);
}

Quoted 1 line
#46 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Quoted 60 lines · Zide
Quoted 47 lines · Dapeng
Quoted 41 lines · Jim Mattson
Quoted 32 lines · Dapeng
Quoted 31 lines · Zide Chen

Add a test case to exercise IA32_PERF_METRICS, i.e. architectural support for Topdown (TMA) Level 1 metrics, enumerated by IA32_PERF_CAPABILITIES[15].

Only check for non-zero metrics, as they are derived and depend on the workload, CPU model, and host scheduling, making precise expectations fragile.

Extend the PMU selftest to cover Intel fixed counter 3 by bumping MAX_NR_FIXED_COUNTERS to 4 and validating basic functionality.

---
...

tools/testing/selftests/kvm/x86/pmu_counters_test.c +17 -0
+static void __guest_test_perf_metrics(void)
+{
+     int retiring, bad_spec, fe_bound, be_bound, sum;
+     u64 global_ctrl, metrics;
+
+     if ((guest_get_pmu_version() < 2) ||    /* Does guest have GLOBAL_CTRL? */
+         !this_cpu_has(X86_FEATURE_PDCM) ||
+         !(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS))
+             return;
+
+     wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+     wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0);
+     wrmsr(MSR_PERF_METRICS, 0);
+
+     /* Enable fixed ctr3 (TOPDOWN.SLOTS) and PERF_METRICS. */
+     wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(3, FIXED_PMC_KERNEL));
+     global_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(3) |
Quoted 8 lines

tools/testing/selftests/kvm/x86/pmu_counters_test.c static void guest_test_arch_event(u8 idx) · line 347

Quoted 6 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +6 -0
+                   PERF_METRICS_GLOBAL_CTRL_ENABLE;
+
+     GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, "");
+
+     /* Check test results. */
+     metrics = rdmsr(MSR_PERF_METRICS);

Could we use rdpmc instead of rdmsr here? rdpmc is a preferred way to read counter value.

Quoted 5 lines

This is in-guest code, so the unintercepted RDMSR will be much faster than the emulated RDPMC.

Should we rethink that preference, or add hardware support for selective RDPMC intercepts?

Quoted 7 lines

Hmm, in current most cases, rdpmc and rdmsr should share consistent interception configuration for PERF_METRICS except host and guest have different counters bitmap.

Considering this is a test case, the test coverage should be more important than the performance, there should be at least a place to call rdpmc against the PERF_METRICS, otherwise, that path won't be validated.

How about keeping rdmsr() in __guest_test_perf_metrics(), and using RDPMC in the sanity test?

Review static void __guest_test_perf_metrics(void) · line 375

Quoted 6 lines
(quoted diff) +1 -1
@@ -369,7 +371,7 @@ static void __guest_test_perf_metrics(void)
        GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), metrics);
        wrmsr(MSR_PERF_METRICS, 0xdeaddead);
 
-       GUEST_ASSERT_EQ(rdmsr(MSR_PERF_METRICS), 0xdeaddead);
+       guest_test_rdpmc(INTEL_RDPMC_METRICS, true, 0xdeaddead);
 }

Yes, it looks good for me. guest_test_rdpmc() can cover both the interception and pass-through cases for rdpmc.

Quoted 1 line
#47 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

In reply to Mi, Dapeng
Quoted 48 lines · Dapeng
Quoted 45 lines · Jim Mattson
Quoted 33 lines · Dapeng
Quoted 31 lines · Zide Chen

Add a test case to exercise IA32_PERF_METRICS, i.e. architectural support for Topdown (TMA) Level 1 metrics, enumerated by IA32_PERF_CAPABILITIES[15].

Only check for non-zero metrics, as they are derived and depend on the workload, CPU model, and host scheduling, making precise expectations fragile.

Extend the PMU selftest to cover Intel fixed counter 3 by bumping MAX_NR_FIXED_COUNTERS to 4 and validating basic functionality.

---
...

tools/testing/selftests/kvm/x86/pmu_counters_test.c +17 -0
+static void __guest_test_perf_metrics(void)
+{
+     int retiring, bad_spec, fe_bound, be_bound, sum;
+     u64 global_ctrl, metrics;
+
+     if ((guest_get_pmu_version() < 2) ||    /* Does guest have GLOBAL_CTRL? */
+         !this_cpu_has(X86_FEATURE_PDCM) ||
+         !(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS))
+             return;
+
+     wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+     wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0);
+     wrmsr(MSR_PERF_METRICS, 0);
+
+     /* Enable fixed ctr3 (TOPDOWN.SLOTS) and PERF_METRICS. */
+     wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(3, FIXED_PMC_KERNEL));
+     global_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(3) |
Quoted 8 lines

tools/testing/selftests/kvm/x86/pmu_counters_test.c static void guest_test_arch_event(u8 idx) · line 347

Quoted 6 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +6 -0
+                   PERF_METRICS_GLOBAL_CTRL_ENABLE;
+
+     GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, "");
+
+     /* Check test results. */
+     metrics = rdmsr(MSR_PERF_METRICS);

Could we use rdpmc instead of rdmsr here? rdpmc is a preferred way to read counter value.

This is in-guest code, so the unintercepted RDMSR will be much faster than the emulated RDPMC.

Quoted 6 lines
Quoted 2 lines

Should we rethink that preference, or add hardware support for selective RDPMC intercepts?

Hmm, in current most cases, rdpmc and rdmsr should share consistent interception configuration for PERF_METRICS except host and guest have different counters bitmap.

I'm assuming that Intel wants to reserve the right to cover other resources with RDPMC in the future. Unless we have some way of determining, through a hardware enumeration, that all readable resources contain guest state, then we will akways have to intercept RDPMC.

If I'm wrong, and Intel is willing to guarantee that no new resources will ever be added, then we can let the guest execute RDPMC natively, when the guest owns all of the counters and PERF_METRICS.

#48 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Quoted 49 lines · Jim Mattson
Quoted 48 lines · Dapeng
Quoted 46 lines · Jim Mattson
Quoted 32 lines · Dapeng
Quoted 31 lines · Zide Chen

Add a test case to exercise IA32_PERF_METRICS, i.e. architectural support for Topdown (TMA) Level 1 metrics, enumerated by IA32_PERF_CAPABILITIES[15].

Only check for non-zero metrics, as they are derived and depend on the workload, CPU model, and host scheduling, making precise expectations fragile.

Extend the PMU selftest to cover Intel fixed counter 3 by bumping MAX_NR_FIXED_COUNTERS to 4 and validating basic functionality.

---
...

tools/testing/selftests/kvm/x86/pmu_counters_test.c +17 -0
+static void __guest_test_perf_metrics(void)
+{
+     int retiring, bad_spec, fe_bound, be_bound, sum;
+     u64 global_ctrl, metrics;
+
+     if ((guest_get_pmu_version() < 2) ||    /* Does guest have GLOBAL_CTRL? */
+         !this_cpu_has(X86_FEATURE_PDCM) ||
+         !(rdmsr(MSR_IA32_PERF_CAPABILITIES) & PERF_CAP_PERF_METRICS))
+             return;
+
+     wrmsr(MSR_CORE_PERF_GLOBAL_CTRL, 0);
+     wrmsr(MSR_CORE_PERF_FIXED_CTR3, 0);
+     wrmsr(MSR_PERF_METRICS, 0);
+
+     /* Enable fixed ctr3 (TOPDOWN.SLOTS) and PERF_METRICS. */
+     wrmsr(MSR_CORE_PERF_FIXED_CTR_CTRL, FIXED_PMC_CTRL(3, FIXED_PMC_KERNEL));
+     global_ctrl = FIXED_PMC_GLOBAL_CTRL_ENABLE(3) |
Quoted 8 lines

tools/testing/selftests/kvm/x86/pmu_counters_test.c static void guest_test_arch_event(u8 idx) · line 347

Quoted 6 lines
tools/testing/selftests/kvm/x86/pmu_counters_test.c +6 -0
+                   PERF_METRICS_GLOBAL_CTRL_ENABLE;
+
+     GUEST_RUN_PAYLOAD(MSR_CORE_PERF_GLOBAL_CTRL, global_ctrl, "");
+
+     /* Check test results. */
+     metrics = rdmsr(MSR_PERF_METRICS);

Could we use rdpmc instead of rdmsr here? rdpmc is a preferred way to read counter value.

This is in-guest code, so the unintercepted RDMSR will be much faster than the emulated RDPMC.

Should we rethink that preference, or add hardware support for selective RDPMC intercepts?

Quoted 8 lines
Quoted 3 lines

Hmm, in current most cases, rdpmc and rdmsr should share consistent interception configuration for PERF_METRICS except host and guest have different counters bitmap.

I'm assuming that Intel wants to reserve the right to cover other resources with RDPMC in the future. Unless we have some way of determining, through a hardware enumeration, that all readable resources contain guest state, then we will akways have to intercept RDPMC.

rdpmc is the preferable way to read PMU counters since the better performance than rdmsr, and most of current code regardless of kernel or user-space already use rdpmc to read PMC counters. If we set the rdpmc to interception by default, then it may cause extra performance hit.

Currently I didn't hear there is any plan to extend the rdpmc to support more types for Intel platforms ...

Quoted 5 lines

If I'm wrong, and Intel is willing to guarantee that no new resources will ever be added, then we can let the guest execute RDPMC natively, when the guest owns all of the counters and PERF_METRICS.

#49 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Quoted 7 lines · Dapeng

rdpmc is the preferable way to read PMU counters since the better performance than rdmsr, and most of current code regardless of kernel or user-space already use rdpmc to read PMC counters. If we set the rdpmc to interception by default, then it may cause extra performance hit.

Currently I didn't hear there is any plan to extend the rdpmc to support more types for Intel platforms ...

Is Intel willing to commit to that?

#50 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Quoted 10 lines · Jim Mattson
Quoted 7 lines · Dapeng

rdpmc is the preferable way to read PMU counters since the better performance than rdmsr, and most of current code regardless of kernel or user-space already use rdpmc to read PMC counters. If we set the rdpmc to interception by default, then it may cause extra performance hit.

Currently I didn't hear there is any plan to extend the rdpmc to support more types for Intel platforms ...

Is Intel willing to commit to that?

I'm afraid not. :(

rdpmc is still possible to be extended to support more types in the future, but recently we don't see such requirements.

#51 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Quoted 10 lines · Dapeng
Quoted 7 lines · Jim Mattson
Quoted 5 lines · Dapeng

rdpmc is the preferable way to read PMU counters since the better performance than rdmsr, and most of current code regardless of kernel or user-space already use rdpmc to read PMC counters. If we set the rdpmc to interception by default, then it may cause extra performance hit.

Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Currently I didn't hear there is any plan to extend the rdpmc to support more types for Intel platforms ...

Is Intel willing to commit to that?

I'm afraid not. :(

Then, KVM must always intercept RDPMC.

Quoted 2 lines

rdpmc is still possible to be extended to support more types in the future, but recently we don't see such requirements.

That's no comfort.

An alternative would be for Intel to provide a way for KVM to query which types of PMC are supported in ECX[31:16], before or concurrently with any microarchitecture that supports more types than CPUs support today.

author
#52 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Quoted 22 lines · Jim Mattson
Quoted 10 lines · Dapeng
Quoted 7 lines · Jim Mattson
Quoted 5 lines · Dapeng

rdpmc is the preferable way to read PMU counters since the better performance than rdmsr, and most of current code regardless of kernel or user-space already use rdpmc to read PMC counters. If we set the rdpmc to interception by default, then it may cause extra performance hit.

Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Currently I didn't hear there is any plan to extend the rdpmc to support more types for Intel platforms ...

Is Intel willing to commit to that?

I'm afraid not. :(

Then, KVM must always intercept RDPMC.

Quoted 2 lines

rdpmc is still possible to be extended to support more types in the future, but recently we don't see such requirements.

Quoted 6 lines

That's no comfort.

An alternative would be for Intel to provide a way for KVM to query which types of PMC are supported in ECX[31:16], before or concurrently with any microarchitecture that supports more types than CPUs support today.

While I can't speak for future Intel architectures, we're not aware of any plans to extend RDPMC with additional resource types in the foreseeable future.

Regarding the proposed enumeration mechanism, I'm not sure it changes the fundamental situation. Even if a future architecture provides an enumeration mechanism for new RDPMC resource types, RDPMC passthrough on an older KVM could still exist until KVM gains support for the new resource types.

Given that, I'm not sure the possibility of a future RDPMC extension by itself is sufficient justification for intercepting RDPMC unconditionally today. RDPMC is the standard low-overhead interface for reading PMU data, and forcing every RDPMC through a VM-Exit would impose a significant performance penalty.

#53 lore raw

Re: [PATCH v6 8/8] KVM: selftests: Add PERF_METRICS and fixed counter 3 tests

Quoted 43 lines · Zide
Quoted 22 lines · Jim Mattson
Quoted 10 lines · Dapeng
Quoted 7 lines · Jim Mattson
Quoted 5 lines · Dapeng

rdpmc is the preferable way to read PMU counters since the better performance than rdmsr, and most of current code regardless of kernel or user-space already use rdpmc to read PMC counters. If we set the rdpmc to interception by default, then it may cause extra performance hit.

Quoted 5 lines
Quoted 3 lines
Quoted 2 lines

Currently I didn't hear there is any plan to extend the rdpmc to support more types for Intel platforms ...

Is Intel willing to commit to that?

I'm afraid not. :(

Then, KVM must always intercept RDPMC.

Quoted 2 lines

rdpmc is still possible to be extended to support more types in the future, but recently we don't see such requirements.

Quoted 6 lines

That's no comfort.

An alternative would be for Intel to provide a way for KVM to query which types of PMC are supported in ECX[31:16], before or concurrently with any microarchitecture that supports more types than CPUs support today.

While I can't speak for future Intel architectures, we're not aware of any plans to extend RDPMC with additional resource types in the foreseeable future.

Regarding the proposed enumeration mechanism, I'm not sure it changes the fundamental situation. Even if a future architecture provides an enumeration mechanism for new RDPMC resource types, RDPMC passthrough on an older KVM could still exist until KVM gains support for the new resource types.

Quoted 5 lines

Given that, I'm not sure the possibility of a future RDPMC extension by itself is sufficient justification for intercepting RDPMC unconditionally today. RDPMC is the standard low-overhead interface for reading PMU data, and forcing every RDPMC through a VM-Exit would impose a significant performance penalty.

I *am* sure. If passing through RDPMC could leak host-level information on future hardware, then KVM must not pass through RDPMC. Full stop.

Note that KVM cannot pass through RDPMC on AMD hardware today, because AMD's RDPMC already leaks host resources (e.g. data fabric PMCs).