Skip to contentSkip to conversation

KVM: x86/pmu: Add mediated vPMU PerfMon v5 support

PATCH In review Zide Chen 7 participants 82 messages Aug 27 – Nov 4

Subsystems (5)
x86 KVM Kselftest perf events +1 more subsystems
lore
Share & tools
lore

Zide Chen

author
#0 lore raw

PATCH 1/1 KVM: x86/pmu: Add mediated vPMU PerfMon v5 support

KVM currently caps the guest Intel architectural PMU version at 2. This series bumps it to PerfMon v5 and adds support for some features that are introduced in PerfMon v3-v5.

- CPUID.0AH:ECX fixed-counter bitmap (v5): supersedes EDX[4:0] and lets KVM/guests represent non-contiguous fixed counters (e.g. some E-core server parts skip fixed counter 3).
- Add support for IA32_PERF_GLOBAL_STATUS_SET (v4): lets software set individual bits in the global status MSR.
- Add support for IA32_PERF_GLOBAL_INUSE (v4): reports which counters and PMI are currently claimed.
- Implement ANYTHREAD_DEPRECATION capability.

Intel PerfMon v5 doesn't support non-contiguous GP counters; however, this series lays the groundwork for GP counter bitmap support anyway, for consistency with the host perf API interface, and prepares for Perfmon Masking support.

Freeze_PerfMon_On_PMI virtualization is not implemented because, as commit 3daa96d67274 ("perf/intel: Remove Perfmon-v4 counter_freezing support") states, the Freeze-on-PMI mechanism violates the perf counter independence.

Streamlined Freeze_LBRs_On_PMI is not implemented because LBR is not yet supported on mediated vPMU.

Tested on SPR and CWF:

- Ran "perf test" on both L0 and L1; no issues attributable to this series have been observed so far.

- Ran KVM selftests on both L0 and L1.

- Passed AnyThread tests on both fixed and gp counters:

  $ perf stat -e r20003c,r2000c4,r2000c0 -- sleep 1

- Tested various guest fixed-counter configurations:

      CPUID.0AH:ECX = 0x7   CPUID.0AH:EDX[4:0]=3
      CPUID.0AH:ECX = 0x2   CPUID.0AH:EDX[4:0]=0
      CPUID.0AH:ECX = 0x6   CPUID.0AH:EDX[4:0]=0
      CPUID.0AH:ECX = 0x5   CPUID.0AH:EDX[4:0]=1
      CPUID.0AH:ECX = 0x0   CPUID.0AH:EDX[4:0]=0

Verified that the guest PMU reflected the configured fixed-counter and that the following command used fixed or general-purpose counters as expected:

  $ perf stat -e instructions,cpu-cycles,ref-cycles -- sleep 1

- Manually tested IA32_PERF_GLOBAL_INUSE.

- Ran some additional perf tests on both L0 and L1.

- Sanity testing on nested VMs.

Patch series summary:
patch 1-4: Code cleanup and a fix.
patch 5-10: Implement PMC bitmap support.
patch 11-14: Implement PerfMon v3-v5 new features. patch 15: Advertise PerfMon version up to v5.
patch 16: Fix pmu_counters_test selftests fixed counter test case.

Rebased to kvm-x86/next: 76671054f9a1

v2 changes:
- Dropped v1 11/15: KVM: x86/pmu: Emulate streamlined Freeze-LBR-on-PMI
- New patch 4/15 to gate BUFFER_OVF reserved bit on guest DS feature.
- Some renames:
- all_valid_pmc_mask to pmc_exists;
- add _is to kvm_{gp,fixed}_pmc_supported();
- split kvm_for_each_set_pmc_idx() to kvm_for_each_{gp,fixed}_counter().
- Add missing AnyThr handling in IA32_FIXED_CTR_CTRL and advertise anythread_deprecated only if kvm_pmu_cap.version >= 5.
- Cap vPMU v3/v4 to v2, as AnyThread cannot be safely virtualized.
- Miscellaneous updates to changelogs and some minor fixes.

Dapeng Mi (6):
KVM: x86/pmu: Drop nr_arch_{gp,fixed}_counters from kvm_pmu perf/x86: Plumb counter bitmap from x86_pmu to x86_pmu_cap
KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities perf/x86: Remove num_counters_{gp,fixed} from x86_pmu_capability
KVM: x86/pmu: Advertise PerfMon version 5 on Intel hosts
KVM: selftests: Support fixed counters bitmap in pmu_counters_test

Zide Chen (10):
KVM: x86/pmu: Remove redundant Perf Global Status MSR bit definitions
KVM: x86/pmu: Rename all_valid_pmc_idx to pmc_exists
KVM: x86/pmu: Rename reserved_bits to eventsel_rsvd in kvm_pmu
KVM: x86/pmu: Gate BUFFER_OVF reserved bit on guest DS
KVM: x86/pmu: Add PMC bitmap accessor helpers
KVM: x86/pmu: Expose kvm_host_pmu to vendor modules
KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs
KVM: x86/pmu: Populate CPUID.0AH:ECX fixed-counter bitmap
KVM: x86/pmu: Factor out fixed counter control bit calculation
KVM: x86/pmu: Ignore AnyThread bit if CPUID.0AH:EDX[15] is set

 arch/x86/events/core.c                        |   8 +-
 arch/x86/include/asm/kvm_host.h               |   9 +-
 arch/x86/include/asm/msr-index.h              |  12 +-
 arch/x86/include/asm/perf_event.h             |  10 +-
 arch/x86/kvm/cpuid.c                          |  21 ++-
 arch/x86/kvm/msrs.c                           |  12 +-
 arch/x86/kvm/pmu.c                            |  85 ++++++---
 arch/x86/kvm/pmu.h                            |  54 +++++-
 arch/x86/kvm/svm/pmu.c                        |  37 ++--
 arch/x86/kvm/svm/svm.c                        |  17 +-
 arch/x86/kvm/vmx/nested.c                     |   8 +-
 arch/x86/kvm/vmx/pmu_intel.c                  | 167 +++++++++++++-----
 arch/x86/kvm/vmx/vmx.c                        |  19 +-
 .../selftests/kvm/include/x86/processor.h     |   6 +-
 .../selftests/kvm/x86/pmu_counters_test.c     |  26 ++-
 15 files changed, 349 insertions(+), 142 deletions(-)
Signature · 1 line
2.55.0
author
#1 lore raw

PATCH v2 1/16 KVM: x86/pmu: Remove redundant Perf Global Status MSR bit definitions

IA32_PERF_GLOBAL_STATUS and its SET/RESET counterparts share bit definitions with perf_event.h, which is the canonical home for Intel PMU bit definitions (e.g., PERFEVTSEL, FIXED_CTR_CTRL, GLOBAL_STATUS). Drop the duplicate definitions from msr-index.h and update KVM code to use the macros from perf_event.h.

Drop the comment as well, since the macros now match the MSR naming.

No functional change intended.

---
 arch/x86/include/asm/msr-index.h |  8 --------
 arch/x86/kvm/vmx/pmu_intel.c     | 11 ++---------
 arch/x86/kvm/vmx/vmx.c           |  2 +-
 3 files changed, 3 insertions(+), 18 deletions(-)
arch/x86/include/asm/msr-index.h +0 -8
index 18c4be75e927..11b99d237e05 100644
@@ -1243,14 +1243,6 @@
 
 #define MSR_PERF_METRICS		0x00000329
 
-/* PERF_GLOBAL_OVF_CTL bits */
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT	55
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI		(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT		62
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF			(1ULL <<  MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT		63
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD			(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT)
-
 /* Geode defined MSRs */
 #define MSR_GEODE_BUSCONT_CONF0		0x00001900
 
arch/x86/kvm/vmx/pmu_intel.c +2 -9
index bfa8612fb450..e71ddd51ae56 100644
@@ -576,17 +576,10 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 			 ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
 	pmu->global_ctrl_rsvd = counter_rsvd;
 
-	/*
-	 * 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);
+			& ~(GLOBAL_STATUS_BUFFER_OVF | GLOBAL_STATUS_COND_CHG);
 	if (vmx_pt_mode_is_host_guest())
-		pmu->global_status_rsvd &=
-				~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
+		pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;
 
 	if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
 		if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
arch/x86/kvm/vmx/vmx.c +1 -1
index e3bfe6aca1a0..d3789b2ee18e 100644
@@ -8612,7 +8612,7 @@ static unsigned int vmx_handle_intel_pt_intr(void)
 		return 0;
 
 	kvm_make_request(KVM_REQ_PMI, vcpu);
-	__set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
+	__set_bit(GLOBAL_STATUS_TRACE_TOPAPMI_BIT,
 		  (unsigned long *)&vcpu->arch.pmu.global_status);
 	return 1;
 }
Signature · 1 line
2.55.0
#2 lore raw

Re: [PATCH v2 01/16] KVM: x86/pmu: Remove redundant Perf Global Status MSR bit definitions

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] Missing tools header synchronization for msr-index.h --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing tools header synchronization for msr-index.h
--

commit e6cc32e583cfbb8bac5003f58672184385cc895a
Author: Zide Chen <[email protected]>

KVM: x86/pmu: Remove redundant Perf Global Status MSR bit definitions

This commit removes duplicate Perf Global Status MSR bit definitions from arch/x86/include/asm/msr-index.h, utilizing the canonical macros from perf_event.h instead. It also updates the KVM PMU code to match.

arch/x86/include/asm/msr-index.h line 1247

Quoted 10 lines
arch/x86/include/asm/msr-index.h +0 -2
index 18c4be75e9271..11b99d237e05b 100644
@@ -1243,14 +1243,6 @@
 
 #define MSR_PERF_METRICS		0x00000329
 
-/* PERF_GLOBAL_OVF_CTL bits */
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT	55

[Severity: Low]
Does this removal also need to be synchronized with the tools copy of this header?

Since the definitions are being removed from the kernel's arch/x86/include/asm/msr-index.h, leaving tools/arch/x86/include/asm/msr-index.h out of sync might cause the build system's check-headers.sh script to emit a warning when building tools like perf.

Quoted 7 lines
arch/x86/include/asm/msr-index.h +0 -6
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI		(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT		62
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF			(1ULL <<  MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT		63
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD			(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT)
-
 /* Geode defined MSRs */
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

Re: [PATCH v2 01/16] KVM: x86/pmu: Remove redundant Perf Global Status MSR bit definitions

Quoted 28 lines · On 8/27/2026 5:57 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] Missing tools header synchronization for msr-index.h
--

commit e6cc32e583cfbb8bac5003f58672184385cc895a
Author: Zide Chen <[email protected]>

KVM: x86/pmu: Remove redundant Perf Global Status MSR bit definitions

This commit removes duplicate Perf Global Status MSR bit definitions from arch/x86/include/asm/msr-index.h, utilizing the canonical macros from perf_event.h instead. It also updates the KVM PMU code to match.

arch/x86/include/asm/msr-index.h line 1247

Quoted 10 lines
arch/x86/include/asm/msr-index.h +0 -2
index 18c4be75e9271..11b99d237e05b 100644
@@ -1243,14 +1243,6 @@
 
 #define MSR_PERF_METRICS		0x00000329
 
-/* PERF_GLOBAL_OVF_CTL bits */
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT	55

[Severity: Low]
Does this removal also need to be synchronized with the tools copy of this header?

Quoted 5 lines

Since the definitions are being removed from the kernel's arch/x86/include/asm/msr-index.h, leaving tools/arch/x86/include/asm/msr-index.h out of sync might cause the build system's check-headers.sh script to emit a warning when building tools like perf.

The removed definitions are not used by tools/perf.

More generally, the usual practice is to update the kernel header only and let the tools-side copies be synchronized separately by the perf maintainers when needed. The check-headers.sh warning is typically addressed later by a dedicated header synchronization patch.

Quoted 9 lines
Quoted 8 lines
arch/x86/include/asm/msr-index.h +0 -6
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI		(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT		62
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF			(1ULL <<  MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT		63
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD			(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT)
-
 /* Geode defined MSRs */
Reviewed-by on v2 1/16
#4 lore raw

Re: [PATCH v2 01/16] KVM: x86/pmu: Remove redundant Perf Global Status MSR bit definitions

In reply to Zide Chen

Thanks for this clean-up. This was what I intended to do for long time. :)

Quoted 74 lines · Zide Chen

IA32_PERF_GLOBAL_STATUS and its SET/RESET counterparts share bit definitions with perf_event.h, which is the canonical home for Intel PMU bit definitions (e.g., PERFEVTSEL, FIXED_CTR_CTRL, GLOBAL_STATUS). Drop the duplicate definitions from msr-index.h and update KVM code to use the macros from perf_event.h.

Drop the comment as well, since the macros now match the MSR naming.

No functional change intended.

---
 arch/x86/include/asm/msr-index.h |  8 --------
 arch/x86/kvm/vmx/pmu_intel.c     | 11 ++---------
 arch/x86/kvm/vmx/vmx.c           |  2 +-
 3 files changed, 3 insertions(+), 18 deletions(-)
arch/x86/include/asm/msr-index.h +0 -8
index 18c4be75e927..11b99d237e05 100644
@@ -1243,14 +1243,6 @@
 
 #define MSR_PERF_METRICS		0x00000329
 
-/* PERF_GLOBAL_OVF_CTL bits */
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT	55
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI		(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT		62
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF			(1ULL <<  MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF_BIT)
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT		63
-#define MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD			(1ULL << MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD_BIT)
-
 /* Geode defined MSRs */
 #define MSR_GEODE_BUSCONT_CONF0		0x00001900
 
arch/x86/kvm/vmx/pmu_intel.c +2 -9
index bfa8612fb450..e71ddd51ae56 100644
@@ -576,17 +576,10 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 			 ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
 	pmu->global_ctrl_rsvd = counter_rsvd;
 
-	/*
-	 * 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);
+			& ~(GLOBAL_STATUS_BUFFER_OVF | GLOBAL_STATUS_COND_CHG);
 	if (vmx_pt_mode_is_host_guest())
-		pmu->global_status_rsvd &=
-				~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
+		pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;
 
 	if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
 		if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
arch/x86/kvm/vmx/vmx.c +1 -1
index e3bfe6aca1a0..d3789b2ee18e 100644
@@ -8612,7 +8612,7 @@ static unsigned int vmx_handle_intel_pt_intr(void)
 		return 0;
 
 	kvm_make_request(KVM_REQ_PMI, vcpu);
-	__set_bit(MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI_BIT,
+	__set_bit(GLOBAL_STATUS_TRACE_TOPAPMI_BIT,
 		  (unsigned long *)&vcpu->arch.pmu.global_status);
 	return 1;
 }
Reviewed-by on v2 1/16
#5 lore raw

Re: [PATCH v2 01/16] KVM: x86/pmu: Remove redundant Perf Global Status MSR bit definitions

In reply to Zide Chen
Quoted 7 lines · Zide Chen

IA32_PERF_GLOBAL_STATUS and its SET/RESET counterparts share bit definitions with perf_event.h, which is the canonical home for Intel PMU bit definitions (e.g., PERFEVTSEL, FIXED_CTR_CTRL, GLOBAL_STATUS). Drop the duplicate definitions from msr-index.h and update KVM code to use the macros from perf_event.h.

Quoted 6 lines

Drop the comment as well, since the macros now match the MSR naming.

No functional change intended.

author
#6 lore raw

PATCH v2 2/16 KVM: x86/pmu: Rename all_valid_pmc_idx to pmc_exists

In reply to Zide Chen

all_valid_pmc_idx is a bitmap of valid PMC indices, not an index itself; rename it to pmc_exists to better reflect what it holds and keep the name consistent with other fields in the same struct.

No functional change intended.

---
v2:
- Pick the name pmc_exists other than all_valid_pmc_mask. (Sean)

---
 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/pmu.c              | 10 +++++-----
 arch/x86/kvm/vmx/pmu_intel.c    |  2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index 283847619ff8..e473796da7d9 100644
@@ -594,7 +594,7 @@ struct kvm_pmu {
 		DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
 		atomic64_t __reprogram_pmi;
 	};
-	DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);
+	DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
 	DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);
 
 	DECLARE_BITMAP(pmc_counting_instructions, X86_PMC_IDX_MAX);
arch/x86/kvm/pmu.c +5 -5
index a7d60c8785cd..8f2effee3b7b 100644
@@ -950,7 +950,7 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu)
 	bitmap_zero(pmu->reprogram_pmi, X86_PMC_IDX_MAX);
 	bitmap_zero(pmu->pmc_has_mode_specific_enables, X86_PMC_IDX_MAX);
 
-	kvm_for_each_pmc(pmu, pmc, i, pmu->all_valid_pmc_idx) {
+	kvm_for_each_pmc(pmu, pmc, i, pmu->pmc_exists) {
 		pmc_stop_counter(pmc);
 		pmc->counter = 0;
 		pmc->emulated_counter = 0;
@@ -996,7 +996,7 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->fixed_ctr_ctrl_rsvd = ~0ull;
 	pmu->pebs_enable_rsvd = ~0ull;
 	pmu->pebs_data_cfg_rsvd = ~0ull;
-	bitmap_zero(pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX);
+	bitmap_zero(pmu->pmc_exists, X86_PMC_IDX_MAX);
 
 	if (!vcpu->kvm->arch.enable_pmu)
 		return;
@@ -1017,8 +1017,8 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (kvm_vcpu_has_mediated_pmu(vcpu))
 		kvm_pmu_call(write_global_ctrl)(pmu->global_ctrl);
 
-	bitmap_set(pmu->all_valid_pmc_idx, 0, pmu->nr_arch_gp_counters);
-	bitmap_set(pmu->all_valid_pmc_idx, KVM_FIXED_PMC_BASE_IDX,
+	bitmap_set(pmu->pmc_exists, 0, pmu->nr_arch_gp_counters);
+	bitmap_set(pmu->pmc_exists, KVM_FIXED_PMC_BASE_IDX,
 		   pmu->nr_arch_fixed_counters);
 }
 
@@ -1040,7 +1040,7 @@ void kvm_pmu_cleanup(struct kvm_vcpu *vcpu)
 
 	pmu->need_cleanup = false;
 
-	bitmap_andnot(bitmask, pmu->all_valid_pmc_idx,
+	bitmap_andnot(bitmask, pmu->pmc_exists,
 		      pmu->pmc_in_use, X86_PMC_IDX_MAX);
 
 	kvm_for_each_pmc(pmu, pmc, i, bitmask) {
arch/x86/kvm/vmx/pmu_intel.c +1 -1
index e71ddd51ae56..322a3ad580dd 100644
@@ -557,7 +557,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 		lbr_desc->records.nr = 0;
 
 	if (lbr_desc->records.nr)
-		bitmap_set(pmu->all_valid_pmc_idx, INTEL_PMC_IDX_FIXED_VLBR, 1);
+		bitmap_set(pmu->pmc_exists, INTEL_PMC_IDX_FIXED_VLBR, 1);
 
 	if (pmu->version == 1)
 		return;
Signature · 1 line
2.55.0
Reviewed-by on v2 2/16
#7 lore raw

Re: [PATCH v2 02/16] KVM: x86/pmu: Rename all_valid_pmc_idx to pmc_exists

Quoted 84 lines · Zide Chen

all_valid_pmc_idx is a bitmap of valid PMC indices, not an index itself; rename it to pmc_exists to better reflect what it holds and keep the name consistent with other fields in the same struct.

No functional change intended.

---
v2:
- Pick the name pmc_exists other than all_valid_pmc_mask. (Sean)

---
 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/pmu.c              | 10 +++++-----
 arch/x86/kvm/vmx/pmu_intel.c    |  2 +-
 3 files changed, 7 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index 283847619ff8..e473796da7d9 100644
@@ -594,7 +594,7 @@ struct kvm_pmu {
 		DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
 		atomic64_t __reprogram_pmi;
 	};
-	DECLARE_BITMAP(all_valid_pmc_idx, X86_PMC_IDX_MAX);
+	DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
 	DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);
 
 	DECLARE_BITMAP(pmc_counting_instructions, X86_PMC_IDX_MAX);
arch/x86/kvm/pmu.c +5 -5
index a7d60c8785cd..8f2effee3b7b 100644
@@ -950,7 +950,7 @@ static void kvm_pmu_reset(struct kvm_vcpu *vcpu)
 	bitmap_zero(pmu->reprogram_pmi, X86_PMC_IDX_MAX);
 	bitmap_zero(pmu->pmc_has_mode_specific_enables, X86_PMC_IDX_MAX);
 
-	kvm_for_each_pmc(pmu, pmc, i, pmu->all_valid_pmc_idx) {
+	kvm_for_each_pmc(pmu, pmc, i, pmu->pmc_exists) {
 		pmc_stop_counter(pmc);
 		pmc->counter = 0;
 		pmc->emulated_counter = 0;
@@ -996,7 +996,7 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->fixed_ctr_ctrl_rsvd = ~0ull;
 	pmu->pebs_enable_rsvd = ~0ull;
 	pmu->pebs_data_cfg_rsvd = ~0ull;
-	bitmap_zero(pmu->all_valid_pmc_idx, X86_PMC_IDX_MAX);
+	bitmap_zero(pmu->pmc_exists, X86_PMC_IDX_MAX);
 
 	if (!vcpu->kvm->arch.enable_pmu)
 		return;
@@ -1017,8 +1017,8 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (kvm_vcpu_has_mediated_pmu(vcpu))
 		kvm_pmu_call(write_global_ctrl)(pmu->global_ctrl);
 
-	bitmap_set(pmu->all_valid_pmc_idx, 0, pmu->nr_arch_gp_counters);
-	bitmap_set(pmu->all_valid_pmc_idx, KVM_FIXED_PMC_BASE_IDX,
+	bitmap_set(pmu->pmc_exists, 0, pmu->nr_arch_gp_counters);
+	bitmap_set(pmu->pmc_exists, KVM_FIXED_PMC_BASE_IDX,
 		   pmu->nr_arch_fixed_counters);
 }
 
@@ -1040,7 +1040,7 @@ void kvm_pmu_cleanup(struct kvm_vcpu *vcpu)
 
 	pmu->need_cleanup = false;
 
-	bitmap_andnot(bitmask, pmu->all_valid_pmc_idx,
+	bitmap_andnot(bitmask, pmu->pmc_exists,
 		      pmu->pmc_in_use, X86_PMC_IDX_MAX);
 
 	kvm_for_each_pmc(pmu, pmc, i, bitmask) {
arch/x86/kvm/vmx/pmu_intel.c +1 -1
index e71ddd51ae56..322a3ad580dd 100644
@@ -557,7 +557,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 		lbr_desc->records.nr = 0;
 
 	if (lbr_desc->records.nr)
-		bitmap_set(pmu->all_valid_pmc_idx, INTEL_PMC_IDX_FIXED_VLBR, 1);
+		bitmap_set(pmu->pmc_exists, INTEL_PMC_IDX_FIXED_VLBR, 1);
 
 	if (pmu->version == 1)
 		return;
Reviewed-by on v2 2/16
#8 lore raw

Re: [PATCH v2 02/16] KVM: x86/pmu: Rename all_valid_pmc_idx to pmc_exists

In reply to Zide Chen
Quoted 8 lines · Zide Chen

all_valid_pmc_idx is a bitmap of valid PMC indices, not an index itself; rename it to pmc_exists to better reflect what it holds and keep the name consistent with other fields in the same struct.

No functional change intended.

author
#9 lore raw

PATCH v2 3/16 KVM: x86/pmu: Rename reserved_bits to eventsel_rsvd in kvm_pmu

In reply to Zide Chen

This field stores the bits that are reserved in guest IA32_PERFEVTSELx MSRs. Rename it to eventsel_rsvd to better reflect its purpose and to align with other field names in struct kvm_pmu.

Opportunistically, replace the magic number 0xffffffff00200000ull with macros.

No functional change intended.

---
v2:
- Replace magic numbers in AMD pmu->eventsel_rsvd.

---
 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/pmu.c              |  6 +++++-
 arch/x86/kvm/svm/pmu.c          | 12 +++++++++---
 arch/x86/kvm/vmx/pmu_intel.c    | 12 ++++++------
 4 files changed, 21 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index e473796da7d9..90d80483df7f 100644
@@ -580,7 +580,7 @@ struct kvm_pmu {
 	u64 counter_bitmask[2];
 	u64 global_ctrl_rsvd;
 	u64 global_status_rsvd;
-	u64 reserved_bits;
+	u64 eventsel_rsvd;
 	u64 raw_event_mask;
 	struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
 	struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
arch/x86/kvm/pmu.c +5 -1
index 8f2effee3b7b..964e74bdaaa7 100644
@@ -989,7 +989,11 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->nr_arch_fixed_counters = 0;
 	pmu->counter_bitmask[KVM_PMC_GP] = 0;
 	pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
-	pmu->reserved_bits = 0xffffffff00200000ull;
+	/*
+	 * KVM is not able to emulate the AnyThread bit due to cross-VM
+	 * information leakage on SMT cores.
+	 */
+	pmu->eventsel_rsvd = GENMASK_ULL(63, 32) | ARCH_PERFMON_EVENTSEL_ANY;
 	pmu->raw_event_mask = X86_RAW_EVENT_MASK;
 	pmu->global_ctrl_rsvd = ~0ull;
 	pmu->global_status_rsvd = ~0ull;
arch/x86/kvm/svm/pmu.c +9 -3
index c18286545a7a..52224872e0d4 100644
@@ -163,7 +163,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	/* MSR_EVNTSELn */
 	pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);
 	if (pmc) {
-		data &= ~pmu->reserved_bits;
+		data &= ~pmu->eventsel_rsvd;
 		if (data != pmc->eventsel) {
 			pmc->eventsel = data;
 			pmc->eventsel_hw = (data & ~AMD64_EVENTSEL_HOSTONLY) |
@@ -214,9 +214,15 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 
 	pmu->counter_bitmask[KVM_PMC_GP] = BIT_ULL(48) - 1;
 
-	pmu->reserved_bits = 0xfffffff000280000ull;
+	/*
+	 * AMD PerfEvtSeln registers do not support PinControl, and KVM does
+	 * not emulate AnyThread due to cross-VM information leakage on SMT
+	 * cores (as on the Intel side).
+	 */
+	pmu->eventsel_rsvd = GENMASK_ULL(63, 36) | ARCH_PERFMON_EVENTSEL_PIN_CONTROL |
+			     ARCH_PERFMON_EVENTSEL_ANY;
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_SVM) && kvm_vcpu_has_mediated_pmu(vcpu))
-		pmu->reserved_bits &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
+		pmu->eventsel_rsvd &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
 
 	pmu->raw_event_mask = AMD64_RAW_EVENT_MASK;
 	/* not applicable to AMD; but clean them to prevent any fall out */
arch/x86/kvm/vmx/pmu_intel.c +6 -6
index 322a3ad580dd..11c9161dc853 100644
@@ -385,7 +385,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	struct kvm_pmc *pmc;
 	u32 msr = msr_info->index;
 	u64 data = msr_info->data;
-	u64 reserved_bits, diff;
+	u64 eventsel_rsvd, diff;
 
 	switch (msr) {
 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
@@ -433,11 +433,11 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			pmc_write_counter(pmc, data);
 			break;
 		} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
-			reserved_bits = pmu->reserved_bits;
+			eventsel_rsvd = pmu->eventsel_rsvd;
 			if ((pmc->idx == 2) &&
 			    (pmu->raw_event_mask & HSW_IN_TX_CHECKPOINTED))
-				reserved_bits ^= HSW_IN_TX_CHECKPOINTED;
-			if (data & reserved_bits)
+				eventsel_rsvd ^= HSW_IN_TX_CHECKPOINTED;
+			if (data & eventsel_rsvd)
 				return 1;
 
 			if (data != pmc->eventsel) {
@@ -545,7 +545,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (entry &&
 	    (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
 	    (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) {
-		pmu->reserved_bits ^= HSW_IN_TX;
+		pmu->eventsel_rsvd ^= HSW_IN_TX;
 		pmu->raw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
 	}
 
@@ -584,7 +584,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
 		if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
 			pmu->pebs_enable_rsvd = counter_rsvd;
-			pmu->reserved_bits &= ~ICL_EVENTSEL_ADAPTIVE;
+			pmu->eventsel_rsvd &= ~ICL_EVENTSEL_ADAPTIVE;
 			pmu->pebs_data_cfg_rsvd = ~0xff00000full;
 			intel_pmu_enable_fixed_counter_bits(pmu, ICL_FIXED_0_ADAPTIVE);
 		} else {
Signature · 1 line
2.55.0
Reviewed-by on v2 3/16
#10 lore raw

Re: [PATCH v2 03/16] KVM: x86/pmu: Rename reserved_bits to eventsel_rsvd in kvm_pmu

Quoted 128 lines · Zide Chen

This field stores the bits that are reserved in guest IA32_PERFEVTSELx MSRs. Rename it to eventsel_rsvd to better reflect its purpose and to align with other field names in struct kvm_pmu.

Opportunistically, replace the magic number 0xffffffff00200000ull with macros.

No functional change intended.

---
v2:
- Replace magic numbers in AMD pmu->eventsel_rsvd.

---
 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/pmu.c              |  6 +++++-
 arch/x86/kvm/svm/pmu.c          | 12 +++++++++---
 arch/x86/kvm/vmx/pmu_intel.c    | 12 ++++++------
 4 files changed, 21 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index e473796da7d9..90d80483df7f 100644
@@ -580,7 +580,7 @@ struct kvm_pmu {
 	u64 counter_bitmask[2];
 	u64 global_ctrl_rsvd;
 	u64 global_status_rsvd;
-	u64 reserved_bits;
+	u64 eventsel_rsvd;
 	u64 raw_event_mask;
 	struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
 	struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
arch/x86/kvm/pmu.c +5 -1
index 8f2effee3b7b..964e74bdaaa7 100644
@@ -989,7 +989,11 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->nr_arch_fixed_counters = 0;
 	pmu->counter_bitmask[KVM_PMC_GP] = 0;
 	pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
-	pmu->reserved_bits = 0xffffffff00200000ull;
+	/*
+	 * KVM is not able to emulate the AnyThread bit due to cross-VM
+	 * information leakage on SMT cores.
+	 */
+	pmu->eventsel_rsvd = GENMASK_ULL(63, 32) | ARCH_PERFMON_EVENTSEL_ANY;
 	pmu->raw_event_mask = X86_RAW_EVENT_MASK;
 	pmu->global_ctrl_rsvd = ~0ull;
 	pmu->global_status_rsvd = ~0ull;
arch/x86/kvm/svm/pmu.c +9 -3
index c18286545a7a..52224872e0d4 100644
@@ -163,7 +163,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	/* MSR_EVNTSELn */
 	pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);
 	if (pmc) {
-		data &= ~pmu->reserved_bits;
+		data &= ~pmu->eventsel_rsvd;
 		if (data != pmc->eventsel) {
 			pmc->eventsel = data;
 			pmc->eventsel_hw = (data & ~AMD64_EVENTSEL_HOSTONLY) |
@@ -214,9 +214,15 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 
 	pmu->counter_bitmask[KVM_PMC_GP] = BIT_ULL(48) - 1;
 
-	pmu->reserved_bits = 0xfffffff000280000ull;
+	/*
+	 * AMD PerfEvtSeln registers do not support PinControl, and KVM does
+	 * not emulate AnyThread due to cross-VM information leakage on SMT
+	 * cores (as on the Intel side).
+	 */
+	pmu->eventsel_rsvd = GENMASK_ULL(63, 36) | ARCH_PERFMON_EVENTSEL_PIN_CONTROL |
+			     ARCH_PERFMON_EVENTSEL_ANY;
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_SVM) && kvm_vcpu_has_mediated_pmu(vcpu))
-		pmu->reserved_bits &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
+		pmu->eventsel_rsvd &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
 
 	pmu->raw_event_mask = AMD64_RAW_EVENT_MASK;
 	/* not applicable to AMD; but clean them to prevent any fall out */
arch/x86/kvm/vmx/pmu_intel.c +6 -6
index 322a3ad580dd..11c9161dc853 100644
@@ -385,7 +385,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	struct kvm_pmc *pmc;
 	u32 msr = msr_info->index;
 	u64 data = msr_info->data;
-	u64 reserved_bits, diff;
+	u64 eventsel_rsvd, diff;
 
 	switch (msr) {
 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
@@ -433,11 +433,11 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			pmc_write_counter(pmc, data);
 			break;
 		} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
-			reserved_bits = pmu->reserved_bits;
+			eventsel_rsvd = pmu->eventsel_rsvd;
 			if ((pmc->idx == 2) &&
 			    (pmu->raw_event_mask & HSW_IN_TX_CHECKPOINTED))
-				reserved_bits ^= HSW_IN_TX_CHECKPOINTED;
-			if (data & reserved_bits)
+				eventsel_rsvd ^= HSW_IN_TX_CHECKPOINTED;
+			if (data & eventsel_rsvd)
 				return 1;
 
 			if (data != pmc->eventsel) {
@@ -545,7 +545,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (entry &&
 	    (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
 	    (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) {
-		pmu->reserved_bits ^= HSW_IN_TX;
+		pmu->eventsel_rsvd ^= HSW_IN_TX;
 		pmu->raw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
 	}
 
@@ -584,7 +584,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
 		if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
 			pmu->pebs_enable_rsvd = counter_rsvd;
-			pmu->reserved_bits &= ~ICL_EVENTSEL_ADAPTIVE;
+			pmu->eventsel_rsvd &= ~ICL_EVENTSEL_ADAPTIVE;
 			pmu->pebs_data_cfg_rsvd = ~0xff00000full;
 			intel_pmu_enable_fixed_counter_bits(pmu, ICL_FIXED_0_ADAPTIVE);
 		} else {
Reviewed-by on v2 3/16
#11 lore raw

Re: [PATCH v2 03/16] KVM: x86/pmu: Rename reserved_bits to eventsel_rsvd in kvm_pmu

In reply to Zide Chen
Quoted 5 lines · Zide Chen

This field stores the bits that are reserved in guest IA32_PERFEVTSELx MSRs. Rename it to eventsel_rsvd to better reflect its purpose and to align with other field names in struct kvm_pmu.

Quoted 7 lines

Opportunistically, replace the magic number 0xffffffff00200000ull with macros.

No functional change intended.

Reviewed-by on v2 3/16
#12 lore raw

Re: [PATCH v2 03/16] KVM: x86/pmu: Rename reserved_bits to eventsel_rsvd in kvm_pmu

In reply to Zide Chen
Quoted 120 lines · Zide Chen

This field stores the bits that are reserved in guest IA32_PERFEVTSELx MSRs. Rename it to eventsel_rsvd to better reflect its purpose and to align with other field names in struct kvm_pmu.

Opportunistically, replace the magic number 0xffffffff00200000ull with macros.

No functional change intended.

---
v2:
- Replace magic numbers in AMD pmu->eventsel_rsvd.

---
 arch/x86/include/asm/kvm_host.h |  2 +-
 arch/x86/kvm/pmu.c              |  6 +++++-
 arch/x86/kvm/svm/pmu.c          | 12 +++++++++---
 arch/x86/kvm/vmx/pmu_intel.c    | 12 ++++++------
 4 files changed, 21 insertions(+), 11 deletions(-)
arch/x86/include/asm/kvm_host.h +1 -1
index e473796da7d9..90d80483df7f 100644
@@ -580,7 +580,7 @@ struct kvm_pmu {
 	u64 counter_bitmask[2];
 	u64 global_ctrl_rsvd;
 	u64 global_status_rsvd;
-	u64 reserved_bits;
+	u64 eventsel_rsvd;
 	u64 raw_event_mask;
 	struct kvm_pmc gp_counters[KVM_MAX_NR_GP_COUNTERS];
 	struct kvm_pmc fixed_counters[KVM_MAX_NR_FIXED_COUNTERS];
arch/x86/kvm/pmu.c +5 -1
index 8f2effee3b7b..964e74bdaaa7 100644
@@ -989,7 +989,11 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->nr_arch_fixed_counters = 0;
 	pmu->counter_bitmask[KVM_PMC_GP] = 0;
 	pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
-	pmu->reserved_bits = 0xffffffff00200000ull;
+	/*
+	 * KVM is not able to emulate the AnyThread bit due to cross-VM
+	 * information leakage on SMT cores.
+	 */
+	pmu->eventsel_rsvd = GENMASK_ULL(63, 32) | ARCH_PERFMON_EVENTSEL_ANY;
 	pmu->raw_event_mask = X86_RAW_EVENT_MASK;
 	pmu->global_ctrl_rsvd = ~0ull;
 	pmu->global_status_rsvd = ~0ull;
arch/x86/kvm/svm/pmu.c +9 -3
index c18286545a7a..52224872e0d4 100644
@@ -163,7 +163,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	/* MSR_EVNTSELn */
 	pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_EVNTSEL);
 	if (pmc) {
-		data &= ~pmu->reserved_bits;
+		data &= ~pmu->eventsel_rsvd;
 		if (data != pmc->eventsel) {
 			pmc->eventsel = data;
 			pmc->eventsel_hw = (data & ~AMD64_EVENTSEL_HOSTONLY) |
@@ -214,9 +214,15 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 
 	pmu->counter_bitmask[KVM_PMC_GP] = BIT_ULL(48) - 1;
 
-	pmu->reserved_bits = 0xfffffff000280000ull;
+	/*
+	 * AMD PerfEvtSeln registers do not support PinControl, and KVM does
+	 * not emulate AnyThread due to cross-VM information leakage on SMT
+	 * cores (as on the Intel side).
+	 */
+	pmu->eventsel_rsvd = GENMASK_ULL(63, 36) | ARCH_PERFMON_EVENTSEL_PIN_CONTROL |
+			     ARCH_PERFMON_EVENTSEL_ANY;
 	if (guest_cpu_cap_has(vcpu, X86_FEATURE_SVM) && kvm_vcpu_has_mediated_pmu(vcpu))
-		pmu->reserved_bits &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
+		pmu->eventsel_rsvd &= ~AMD64_EVENTSEL_HOST_GUEST_MASK;
 
 	pmu->raw_event_mask = AMD64_RAW_EVENT_MASK;
 	/* not applicable to AMD; but clean them to prevent any fall out */
arch/x86/kvm/vmx/pmu_intel.c +5 -5
index 322a3ad580dd..11c9161dc853 100644
@@ -385,7 +385,7 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	struct kvm_pmc *pmc;
 	u32 msr = msr_info->index;
 	u64 data = msr_info->data;
-	u64 reserved_bits, diff;
+	u64 eventsel_rsvd, diff;
 
 	switch (msr) {
 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
@@ -433,11 +433,11 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			pmc_write_counter(pmc, data);
 			break;
 		} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
-			reserved_bits = pmu->reserved_bits;
+			eventsel_rsvd = pmu->eventsel_rsvd;
 			if ((pmc->idx == 2) &&
 			    (pmu->raw_event_mask & HSW_IN_TX_CHECKPOINTED))
-				reserved_bits ^= HSW_IN_TX_CHECKPOINTED;
-			if (data & reserved_bits)
+				eventsel_rsvd ^= HSW_IN_TX_CHECKPOINTED;
+			if (data & eventsel_rsvd)
 				return 1;
 
 			if (data != pmc->eventsel) {
@@ -545,7 +545,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (entry &&
 	    (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
 	    (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) {
-		pmu->reserved_bits ^= HSW_IN_TX;
+		pmu->eventsel_rsvd ^= HSW_IN_TX;
 		pmu->raw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
 	}
 

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

Quoted 8 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -584,7 +584,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
 		if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
 			pmu->pebs_enable_rsvd = counter_rsvd;
-			pmu->reserved_bits &= ~ICL_EVENTSEL_ADAPTIVE;
+			pmu->eventsel_rsvd &= ~ICL_EVENTSEL_ADAPTIVE;
 			pmu->pebs_data_cfg_rsvd = ~0xff00000full;
 			intel_pmu_enable_fixed_counter_bits(pmu, ICL_FIXED_0_ADAPTIVE);
 		} else {
author
#13 lore raw

PATCH v2 4/16 KVM: x86/pmu: Gate BUFFER_OVF reserved bit on guest DS

In reply to Zide Chen

global_status_rsvd unconditionally clears GLOBAL_STATUS_BUFFER_OVF (bit 62), even when the guest does not have X86_FEATURE_DS feature. Clear it only when guest_cpu_cap_has(vcpu, X86_FEATURE_DS), matching hardware behavior.

Fixes f5132b01386b ("KVM: Expose a version 2 architectural PMU to a guests")

---
v2: new patch.

---
 arch/x86/kvm/vmx/pmu_intel.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +10 -2
index 11c9161dc853..9fdae437e569 100644
@@ -576,8 +576,16 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 			 ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
 	pmu->global_ctrl_rsvd = counter_rsvd;
 
-	pmu->global_status_rsvd = pmu->global_ctrl_rsvd
-			& ~(GLOBAL_STATUS_BUFFER_OVF | GLOBAL_STATUS_COND_CHG);
+	/*
+	 * Layout of bits 48:0 in IA32_PERF_GLOBAL_CTRL is identical to
+	 * IA32_PERF_GLOBAL_STATUS, but IA32_PERF_GLOBAL_STATUS has additional
+	 * bits.
+	 */
+	pmu->global_status_rsvd = pmu->global_ctrl_rsvd;
+	if (guest_cpu_cap_has(vcpu, X86_FEATURE_DS))
+		pmu->global_status_rsvd &= ~GLOBAL_STATUS_BUFFER_OVF;
+	pmu->global_status_rsvd &= ~GLOBAL_STATUS_COND_CHG;
+
 	if (vmx_pt_mode_is_host_guest())
 		pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;
 
Signature · 1 line
2.55.0
Reviewed-by on v2 4/16
#14 lore raw

Re: [PATCH v2 04/16] KVM: x86/pmu: Gate BUFFER_OVF reserved bit on guest DS

Quoted 36 lines · Zide Chen

global_status_rsvd unconditionally clears GLOBAL_STATUS_BUFFER_OVF (bit 62), even when the guest does not have X86_FEATURE_DS feature. Clear it only when guest_cpu_cap_has(vcpu, X86_FEATURE_DS), matching hardware behavior.

Fixes f5132b01386b ("KVM: Expose a version 2 architectural PMU to a guests")

---
v2: new patch.

---
 arch/x86/kvm/vmx/pmu_intel.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +10 -2
index 11c9161dc853..9fdae437e569 100644
@@ -576,8 +576,16 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 			 ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
 	pmu->global_ctrl_rsvd = counter_rsvd;
 
-	pmu->global_status_rsvd = pmu->global_ctrl_rsvd
-			& ~(GLOBAL_STATUS_BUFFER_OVF | GLOBAL_STATUS_COND_CHG);
+	/*
+	 * Layout of bits 48:0 in IA32_PERF_GLOBAL_CTRL is identical to
+	 * IA32_PERF_GLOBAL_STATUS, but IA32_PERF_GLOBAL_STATUS has additional
+	 * bits.
+	 */
+	pmu->global_status_rsvd = pmu->global_ctrl_rsvd;
+	if (guest_cpu_cap_has(vcpu, X86_FEATURE_DS))
+		pmu->global_status_rsvd &= ~GLOBAL_STATUS_BUFFER_OVF;
+	pmu->global_status_rsvd &= ~GLOBAL_STATUS_COND_CHG;
+
 	if (vmx_pt_mode_is_host_guest())
 		pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;
 
#15 lore raw

Re: [PATCH v2 04/16] KVM: x86/pmu: Gate BUFFER_OVF reserved bit on guest DS

In reply to Zide Chen
Quoted 22 lines · Zide Chen

global_status_rsvd unconditionally clears GLOBAL_STATUS_BUFFER_OVF (bit 62), even when the guest does not have X86_FEATURE_DS feature. Clear it only when guest_cpu_cap_has(vcpu, X86_FEATURE_DS), matching hardware behavior.

Fixes f5132b01386b ("KVM: Expose a version 2 architectural PMU to a guests")

---
v2: new patch.

---
 arch/x86/kvm/vmx/pmu_intel.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index 11c9161dc853..9fdae437e569 100644
@@ -576,8 +576,16 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
                         ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
        pmu->global_ctrl_rsvd = counter_rsvd;
 

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

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +8 -2
-       pmu->global_status_rsvd = pmu->global_ctrl_rsvd
-                       & ~(GLOBAL_STATUS_BUFFER_OVF | GLOBAL_STATUS_COND_CHG);
+       /*
+        * Layout of bits 48:0 in IA32_PERF_GLOBAL_CTRL is identical to
+        * IA32_PERF_GLOBAL_STATUS, but IA32_PERF_GLOBAL_STATUS has additional
+        * bits.
+        */
+       pmu->global_status_rsvd = pmu->global_ctrl_rsvd;
+       if (guest_cpu_cap_has(vcpu, X86_FEATURE_DS))
+               pmu->global_status_rsvd &= ~GLOBAL_STATUS_BUFFER_OVF;

According to Intel SDM Vol. 4, Table 2-2 ("IA-32 Architectural MSRs"), bit 62 (OvfBuf / ClrOvfBuf) in IA32_PERF_GLOBAL_STATUS (0x38e) and IA32_PERF_GLOBAL_STATUS_RESET (0x390) is supported "If CPUID.0AH:EAX[7:0] > 0", and bit 62 (Set OvfBuf) in IA32_PERF_GLOBAL_STATUS_SET (0x391) is supported "If CPUID.0AH:EAX[7:0] > 3". Though it makes some sense, a dependency on CPUID.01H:EDX.DS[bit 21] is not documented in the SDM.

In mediated PMU mode, vmx_pebs_supported() is false, so KVM clears X86_FEATURE_DS for mediated PMU guests. However, when PMU global controls don't have to be intercepted, MSR_CORE_PERF_GLOBAL_STATUS, MSR_CORE_PERF_GLOBAL_OVF_CTRL, and MSR_CORE_PERF_GLOBAL_STATUS_SET are passed through directly to the guest. Hardware certainly allows bit 62 to be written to IA32_PERF_GLOBAL_OVF_CTRL or IA32_PERF_GLOBAL_STATUS_SET.

If a guest sets bit 62 in hardware via IA32_PERF_GLOBAL_STATUS_SET, intel_mediated_pmu_put() will save bit 62 into pmu->global_status, and subsequent KVM_SET_MSRS(MSR_CORE_PERF_GLOBAL_STATUS) or intercepted writes to MSR_CORE_PERF_GLOBAL_OVF_CTRL will fail the pmu->global_status_rsvd check.

Essentially, this means that we can't pass through the PMU global controls unless their reserved bits match hardware.

author
#16 lore raw

Re: [PATCH v2 04/16] KVM: x86/pmu: Gate BUFFER_OVF reserved bit on guest DS

Quoted 48 lines · Jim Mattson
Quoted 22 lines · Zide Chen

global_status_rsvd unconditionally clears GLOBAL_STATUS_BUFFER_OVF (bit 62), even when the guest does not have X86_FEATURE_DS feature. Clear it only when guest_cpu_cap_has(vcpu, X86_FEATURE_DS), matching hardware behavior.

Fixes f5132b01386b ("KVM: Expose a version 2 architectural PMU to a guests")

---
v2: new patch.

---
 arch/x86/kvm/vmx/pmu_intel.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index 11c9161dc853..9fdae437e569 100644
@@ -576,8 +576,16 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
                         ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
        pmu->global_ctrl_rsvd = counter_rsvd;
 

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

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +8 -2
-       pmu->global_status_rsvd = pmu->global_ctrl_rsvd
-                       & ~(GLOBAL_STATUS_BUFFER_OVF | GLOBAL_STATUS_COND_CHG);
+       /*
+        * Layout of bits 48:0 in IA32_PERF_GLOBAL_CTRL is identical to
+        * IA32_PERF_GLOBAL_STATUS, but IA32_PERF_GLOBAL_STATUS has additional
+        * bits.
+        */
+       pmu->global_status_rsvd = pmu->global_ctrl_rsvd;
+       if (guest_cpu_cap_has(vcpu, X86_FEATURE_DS))
+               pmu->global_status_rsvd &= ~GLOBAL_STATUS_BUFFER_OVF;

According to Intel SDM Vol. 4, Table 2-2 ("IA-32 Architectural MSRs"), bit 62 (OvfBuf / ClrOvfBuf) in IA32_PERF_GLOBAL_STATUS (0x38e) and IA32_PERF_GLOBAL_STATUS_RESET (0x390) is supported "If CPUID.0AH:EAX[7:0] > 0", and bit 62 (Set OvfBuf) in IA32_PERF_GLOBAL_STATUS_SET (0x391) is supported "If CPUID.0AH:EAX[7:0] > 3". Though it makes some sense, a dependency on CPUID.01H:EDX.DS[bit 21] is not documented in the SDM.

In mediated PMU mode, vmx_pebs_supported() is false, so KVM clears X86_FEATURE_DS for mediated PMU guests. However, when PMU global controls don't have to be intercepted, MSR_CORE_PERF_GLOBAL_STATUS, MSR_CORE_PERF_GLOBAL_OVF_CTRL, and MSR_CORE_PERF_GLOBAL_STATUS_SET are passed through directly to the guest. Hardware certainly allows bit 62 to be written to IA32_PERF_GLOBAL_OVF_CTRL or IA32_PERF_GLOBAL_STATUS_SET.

Quoted 8 lines

If a guest sets bit 62 in hardware via IA32_PERF_GLOBAL_STATUS_SET, intel_mediated_pmu_put() will save bit 62 into pmu->global_status, and subsequent KVM_SET_MSRS(MSR_CORE_PERF_GLOBAL_STATUS) or intercepted writes to MSR_CORE_PERF_GLOBAL_OVF_CTRL will fail the pmu->global_status_rsvd check.

Essentially, this means that we can't pass through the PMU global controls unless their reserved bits match hardware.

Thanks for bringing this up. This patch should be dropped.

author
#17 lore raw

PATCH v2 5/16 KVM: x86/pmu: Add PMC bitmap accessor helpers

In reply to Zide Chen

pmu->nr_arch_{gp,fixed}_counters is not able to represent that a PMU may include non-contiguous GP or fixed counters.

pmu->pmc_exists already holds a bitmap indicating both fixed and general-purpose counters, and loops over valid counters can be done via pmu->pmc_exists alone. Extend it to a union so that the u64 alias is available for convenient mask arithmetic operations.

Add the necessary helpers to prepare for bitmap-based PMC counter implementation.

No functional change intended.

---
v2:
- Rename kvm_{gp,fixed}_pmc_supported() to kvm_is_{gp,fixed}_pmc_supported() and simplify them by removing the temporary bitmap. (Sean)
- Split kvm_for_each_set_pmc_idx() to kvm_for_each_gp_counter() and kvm_for_each_fixed_counter(). (Sean)

---
 arch/x86/include/asm/kvm_host.h |  5 +++-
 arch/x86/kvm/pmu.h              | 53 +++++++++++++++++++++++++++++----
 2 files changed, 51 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -1
index 90d80483df7f..49a8a2e9cee0 100644
@@ -594,7 +594,10 @@ struct kvm_pmu {
 		DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
 		atomic64_t __reprogram_pmi;
 	};
-	DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
+	union {
+		DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
+		u64 pmc_exists64;
+	};
 	DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);
 
 	DECLARE_BITMAP(pmc_counting_instructions, X86_PMC_IDX_MAX);
arch/x86/kvm/pmu.h +47 -6

88 lines not on this page · raw message

Signature · 1 line
2.55.0
Reviewed-by on v2 5/16
#18 lore raw

Re: [PATCH v2 05/16] KVM: x86/pmu: Add PMC bitmap accessor helpers

Quoted 140 lines · Zide Chen

pmu->nr_arch_{gp,fixed}_counters is not able to represent that a PMU may include non-contiguous GP or fixed counters.

pmu->pmc_exists already holds a bitmap indicating both fixed and general-purpose counters, and loops over valid counters can be done via pmu->pmc_exists alone. Extend it to a union so that the u64 alias is available for convenient mask arithmetic operations.

Add the necessary helpers to prepare for bitmap-based PMC counter implementation.

No functional change intended.

---
v2:
- Rename kvm_{gp,fixed}_pmc_supported() to kvm_is_{gp,fixed}_pmc_supported() and simplify them by removing the temporary bitmap. (Sean)
- Split kvm_for_each_set_pmc_idx() to kvm_for_each_gp_counter() and kvm_for_each_fixed_counter(). (Sean)

---
 arch/x86/include/asm/kvm_host.h |  5 +++-
 arch/x86/kvm/pmu.h              | 53 +++++++++++++++++++++++++++++----
 2 files changed, 51 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +4 -1
index 90d80483df7f..49a8a2e9cee0 100644
@@ -594,7 +594,10 @@ struct kvm_pmu {
 		DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
 		atomic64_t __reprogram_pmi;
 	};
-	DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
+	union {
+		DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
+		u64 pmc_exists64;
+	};
 	DECLARE_BITMAP(pmc_in_use, X86_PMC_IDX_MAX);
 
 	DECLARE_BITMAP(pmc_counting_instructions, X86_PMC_IDX_MAX);
arch/x86/kvm/pmu.h +47 -6
index 090c9bbb74f4..b7a319b1eccc 100644
@@ -88,6 +88,30 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)
 	return enable_mediated_pmu && vcpu_to_pmu(vcpu)->version;
 }
 
+static inline unsigned long kvm_gp_pmc_mask(struct kvm_pmu *pmu)
+{
+	return pmu->pmc_exists64 &
+	       GENMASK_ULL(KVM_MAX_NR_GP_COUNTERS - 1, 0);
+}
+
+static inline unsigned long kvm_fixed_pmc_mask(struct kvm_pmu *pmu)
+{
+	return (pmu->pmc_exists64 >> KVM_FIXED_PMC_BASE_IDX) &
+		GENMASK_ULL(KVM_MAX_NR_FIXED_COUNTERS - 1, 0);
+}
+
+static inline bool kvm_is_gp_pmc_supported(struct kvm_pmu *pmu, unsigned int idx)
+{
+	return idx < KVM_MAX_NR_GP_COUNTERS &&
+	       test_bit(idx, pmu->pmc_exists);
+}
+
+static inline bool kvm_is_fixed_pmc_supported(struct kvm_pmu *pmu, unsigned int idx)
+{
+	return idx < KVM_MAX_NR_FIXED_COUNTERS &&
+	       test_bit(KVM_FIXED_PMC_BASE_IDX + idx, pmu->pmc_exists);
+}
+
 /*
  * KVM tracks all counters in 64-bit bitmaps, with general purpose counters
  * mapped to bits 31:0 and fixed counters mapped to 63:32, e.g. fixed counter 0
@@ -104,11 +128,11 @@ static inline bool kvm_vcpu_has_mediated_pmu(struct kvm_vcpu *vcpu)
  */
 static inline struct kvm_pmc *kvm_pmc_idx_to_pmc(struct kvm_pmu *pmu, int idx)
 {
-	if (idx < pmu->nr_arch_gp_counters)
+	if (kvm_is_gp_pmc_supported(pmu, idx))
 		return &pmu->gp_counters[idx];
 
 	idx -= KVM_FIXED_PMC_BASE_IDX;
-	if (idx >= 0 && idx < pmu->nr_arch_fixed_counters)
+	if (kvm_is_fixed_pmc_supported(pmu, idx))
 		return &pmu->fixed_counters[idx];
 
 	return NULL;
@@ -120,6 +144,17 @@ static inline struct kvm_pmc *kvm_pmc_idx_to_pmc(struct kvm_pmu *pmu, int idx)
 			continue;				\
 		else						\
 
+/*
+ * @mask is expected to be a scalar unsigned long derived from pmu->pmc_exists,
+ * which is already constrained by KVM_MAX_NR_{AMD,INTEL}_{GP,FIXED}_COUNTERS,
+ * so iteration up to KVM's maximum counter count is safe.
+ */
+#define kvm_for_each_gp_counter(i, mask)	\
+	for_each_set_bit((i), &(mask), KVM_MAX_NR_GP_COUNTERS)
+
+#define kvm_for_each_fixed_counter(i, mask)	\
+	for_each_set_bit((i), &(mask), KVM_MAX_NR_FIXED_COUNTERS)
+
 static inline u64 pmc_bitmask(struct kvm_pmc *pmc)
 {
 	struct kvm_pmu *pmu = pmc_to_pmu(pmc);
@@ -168,9 +203,12 @@ static inline bool kvm_valid_perf_global_ctrl(struct kvm_pmu *pmu,
 static inline struct kvm_pmc *get_gp_pmc(struct kvm_pmu *pmu, u32 msr,
 					 u32 base)
 {
-	if (msr >= base && msr < base + pmu->nr_arch_gp_counters) {
+	if (msr >= base && msr < base + KVM_MAX_NR_GP_COUNTERS) {
 		u32 index = array_index_nospec(msr - base,
-					       pmu->nr_arch_gp_counters);
+					       KVM_MAX_NR_GP_COUNTERS);
+
+		if (!kvm_is_gp_pmc_supported(pmu, index))
+			return NULL;
 
 		return &pmu->gp_counters[index];
 	}
@@ -183,9 +221,12 @@ static inline struct kvm_pmc *get_fixed_pmc(struct kvm_pmu *pmu, u32 msr)
 {
 	int base = MSR_CORE_PERF_FIXED_CTR0;
 
-	if (msr >= base && msr < base + pmu->nr_arch_fixed_counters) {
+	if (msr >= base && msr < base + KVM_MAX_NR_FIXED_COUNTERS) {
 		u32 index = array_index_nospec(msr - base,
-					       pmu->nr_arch_fixed_counters);
+					       KVM_MAX_NR_FIXED_COUNTERS);
+
+		if (!kvm_is_fixed_pmc_supported(pmu, index))
+			return NULL;
 
 		return &pmu->fixed_counters[index];
 	}
Reviewed-by on v2 5/16
#19 lore raw

Re: [PATCH v2 05/16] KVM: x86/pmu: Add PMC bitmap accessor helpers

In reply to Zide Chen
Quoted 33 lines · Zide Chen

pmu->nr_arch_{gp,fixed}_counters is not able to represent that a PMU may include non-contiguous GP or fixed counters.

pmu->pmc_exists already holds a bitmap indicating both fixed and general-purpose counters, and loops over valid counters can be done via pmu->pmc_exists alone. Extend it to a union so that the u64 alias is available for convenient mask arithmetic operations.

Add the necessary helpers to prepare for bitmap-based PMC counter implementation.

No functional change intended.

---
v2:
- Rename kvm_{gp,fixed}_pmc_supported() to kvm_is_{gp,fixed}_pmc_supported() and simplify them by removing the temporary bitmap. (Sean)
- Split kvm_for_each_set_pmc_idx() to kvm_for_each_gp_counter() and kvm_for_each_fixed_counter(). (Sean)

---
 arch/x86/include/asm/kvm_host.h |  5 +++-
 arch/x86/kvm/pmu.h              | 53 +++++++++++++++++++++++++++++----
 2 files changed, 51 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -0
index 90d80483df7f..49a8a2e9cee0 100644

arch/x86/include/asm/kvm_host.h struct kvm_pmu { · line 600

Quoted 8 lines
arch/x86/include/asm/kvm_host.h +4 -1
@@ -594,7 +594,10 @@ struct kvm_pmu {
                DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
                atomic64_t __reprogram_pmi;
        };
-       DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
+       union {
+               DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
+               u64 pmc_exists64;
+       };

Is it worth adding:

BUILD_BUG_ON(sizeof(pmu->pmc_exists) != sizeof(pmu->pmc_exists64));

Either way,

author
#20 lore raw

Re: [PATCH v2 05/16] KVM: x86/pmu: Add PMC bitmap accessor helpers

Quoted 45 lines · Jim Mattson
Quoted 33 lines · Zide Chen

pmu->nr_arch_{gp,fixed}_counters is not able to represent that a PMU may include non-contiguous GP or fixed counters.

pmu->pmc_exists already holds a bitmap indicating both fixed and general-purpose counters, and loops over valid counters can be done via pmu->pmc_exists alone. Extend it to a union so that the u64 alias is available for convenient mask arithmetic operations.

Add the necessary helpers to prepare for bitmap-based PMC counter implementation.

No functional change intended.

---
v2:
- Rename kvm_{gp,fixed}_pmc_supported() to kvm_is_{gp,fixed}_pmc_supported() and simplify them by removing the temporary bitmap. (Sean)
- Split kvm_for_each_set_pmc_idx() to kvm_for_each_gp_counter() and kvm_for_each_fixed_counter(). (Sean)

---
 arch/x86/include/asm/kvm_host.h |  5 +++-
 arch/x86/kvm/pmu.h              | 53 +++++++++++++++++++++++++++++----
 2 files changed, 51 insertions(+), 7 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -0
index 90d80483df7f..49a8a2e9cee0 100644

arch/x86/include/asm/kvm_host.h struct kvm_pmu { · line 600

Quoted 8 lines
arch/x86/include/asm/kvm_host.h +4 -1
@@ -594,7 +594,10 @@ struct kvm_pmu {
                DECLARE_BITMAP(reprogram_pmi, X86_PMC_IDX_MAX);
                atomic64_t __reprogram_pmi;
        };
-       DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
+       union {
+               DECLARE_BITMAP(pmc_exists, X86_PMC_IDX_MAX);
+               u64 pmc_exists64;
+       };

Is it worth adding:

Quoted 1 line

BUILD_BUG_ON(sizeof(pmu->pmc_exists) != sizeof(pmu->pmc_exists64));

Good idea! I can add this:

arch/x86/kvm/pmu.c +2 -0
index 1dac6ae2cbc2..41eaefeea429 100644
@@ -1051,6 +1051,8 @@ void kvm_pmu_init(struct kvm_vcpu *vcpu)
 {
        struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 
+       BUILD_BUG_ON(sizeof(pmu->pmc_exists) != sizeof(pmu->pmc_exists64));
+
        memset(pmu, 0, sizeof(*pmu));
        kvm_pmu_call(init)(vcpu);
 }
Quoted 4 lines

Either way,

author
#21 lore raw

PATCH v2 6/16 KVM: x86/pmu: Drop nr_arch_{gp,fixed}_counters from kvm_pmu

In reply to Zide Chen

From: Dapeng Mi <[email protected]>

This is a preparatory step toward fully bitmap-based PMU capability handling. Later patches switch KVM's view of host PMU capabilities from counter counts to counter bitmaps.

Populate and use pmc_exists directly when walking guest-visible PMCs, instead of relying on pmu->nr_arch_{gp,fixed}_counters as intermediate state.

Iterate counters via the newly added pmc_exists based helpers, and remove the now-redundant nr_arch_{gp,fixed}_counters fields from struct kvm_pmu.

Note: {vmx,svm}_recalc_pmu_msr_intercepts() may leave stale intercepts on GP MSRs after CPUID narrowing. This corner case is resolved by the following patch: "KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities".

---
v2:
- Fix AMD legacy PMU MSR intercept handling for guest PMU narrowing reconfiguration by adding for_each_clear_bit_from().
- Re-word the stale interception changelog part.

---
 arch/x86/include/asm/kvm_host.h |  2 --
 arch/x86/kvm/pmu.c              | 26 +++++++++----------
 arch/x86/kvm/svm/pmu.c          | 25 ++++++++----------
 arch/x86/kvm/svm/svm.c          |  8 ++++--
 arch/x86/kvm/vmx/nested.c       |  6 +++--
 arch/x86/kvm/vmx/pmu_intel.c    | 46 ++++++++++++++++++++++-----------
 arch/x86/kvm/vmx/vmx.c          |  6 +++--
 7 files changed, 68 insertions(+), 51 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -2
index 49a8a2e9cee0..717ba98c08c9 100644
@@ -569,8 +569,6 @@ struct kvm_pmc {
 
 struct kvm_pmu {
 	u8 version;
-	unsigned nr_arch_gp_counters;
-	unsigned nr_arch_fixed_counters;
 	unsigned available_event_types;
 	u64 fixed_ctr_ctrl;
 	u64 fixed_ctr_ctrl_hw;
arch/x86/kvm/pmu.c +12 -14
index 964e74bdaaa7..b0fcd0a538d2 100644
@@ -785,8 +785,8 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu)
 	 * KVM's capabilities are constrained based on KVM support, i.e. KVM's
 	 * capabilities themselves may be a subset of hardware capabilities.
 	 */
-	return pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp ||
-	       pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed;
+	return kvm_gp_pmc_mask(pmu) != BIT_ULL(kvm_host_pmu.num_counters_gp) - 1 ||
+	       kvm_fixed_pmc_mask(pmu) != BIT_ULL(kvm_host_pmu.num_counters_fixed) - 1;
 }
 
 bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
@@ -985,8 +985,6 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	kvm_pmu_reset(vcpu);
 
 	pmu->version = 0;
-	pmu->nr_arch_gp_counters = 0;
-	pmu->nr_arch_fixed_counters = 0;
 	pmu->counter_bitmask[KVM_PMC_GP] = 0;
 	pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
 	/*
@@ -1014,16 +1012,12 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	 * in the global controls).  Emulate that behavior when refreshing the
 	 * PMU so that userspace doesn't need to manually set PERF_GLOBAL_CTRL.
 	 */
-	if (pmu->nr_arch_gp_counters &&
+	if (kvm_gp_pmc_mask(pmu) &&
 	    (kvm_pmu_has_perf_global_ctrl(pmu) || kvm_vcpu_has_mediated_pmu(vcpu)))
-		pmu->global_ctrl = GENMASK_ULL(pmu->nr_arch_gp_counters - 1, 0);
+		pmu->global_ctrl = kvm_gp_pmc_mask(pmu);
 
 	if (kvm_vcpu_has_mediated_pmu(vcpu))
 		kvm_pmu_call(write_global_ctrl)(pmu->global_ctrl);
-
-	bitmap_set(pmu->pmc_exists, 0, pmu->nr_arch_gp_counters);
-	bitmap_set(pmu->pmc_exists, KVM_FIXED_PMC_BASE_IDX,
-		   pmu->nr_arch_fixed_counters);
 }
 
 void kvm_pmu_init(struct kvm_vcpu *vcpu)
@@ -1332,6 +1326,8 @@ static __always_inline u32 gp_eventsel_msr(u32 idx)
 static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
 	struct kvm_pmc *pmc;
 	u32 i;
 
@@ -1340,14 +1336,14 @@ static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
 	 * is intercepted if hardware has counters that aren't visible to the
 	 * guest (KVM will inject #GP as appropriate).
 	 */
-	for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+	kvm_for_each_gp_counter(i, gp_mask) {
 		pmc = &pmu->gp_counters[i];
 
 		if (pmc->counter != rdpmc(i))
 			wrmsrq(gp_counter_msr(i), pmc->counter);
 		wrmsrq(gp_eventsel_msr(i), pmc->eventsel_hw);
 	}
-	for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
+	kvm_for_each_fixed_counter(i, fixed_mask) {
 		pmc = &pmu->fixed_counters[i];
 
 		if (pmc->counter != rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i))
@@ -1390,6 +1386,8 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)
 static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
 	struct kvm_pmc *pmc;
 	u32 i;
 
@@ -1397,7 +1395,7 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
 	 * Clear selectors and counters to ensure hardware doesn't count using
 	 * guest controls when the host (perf) restores its state.
 	 */
-	for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+	kvm_for_each_gp_counter(i, gp_mask) {
 		pmc = &pmu->gp_counters[i];
 
 		pmc->counter = rdpmc(i);
@@ -1407,7 +1405,7 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
 			wrmsrq(gp_eventsel_msr(i), 0);
 	}
 
-	for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
+	kvm_for_each_fixed_counter(i, fixed_mask) {
 		pmc = &pmu->fixed_counters[i];
 
 		pmc->counter = rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i);
arch/x86/kvm/svm/pmu.c +11 -14
index 52224872e0d4..0943ccc1d6b8 100644
@@ -27,12 +27,11 @@ enum pmu_type {
 
 static struct kvm_pmc *amd_pmu_get_pmc(struct kvm_pmu *pmu, int pmc_idx)
 {
-	unsigned int num_counters = pmu->nr_arch_gp_counters;
-
-	if (pmc_idx >= num_counters)
+	if (!kvm_is_gp_pmc_supported(pmu, pmc_idx))
 		return NULL;
 
-	return &pmu->gp_counters[array_index_nospec(pmc_idx, num_counters)];
+	pmc_idx = array_index_nospec(pmc_idx, KVM_MAX_NR_AMD_GP_COUNTERS);
+	return &pmu->gp_counters[pmc_idx];
 }
 
 static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr,
@@ -77,7 +76,7 @@ static int amd_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 
-	if (idx >= pmu->nr_arch_gp_counters)
+	if (!kvm_is_gp_pmc_supported(pmu, idx))
 		return -EINVAL;
 
 	return 0;
@@ -117,7 +116,7 @@ static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 		return pmu->version > 1;
 	default:
 		if (msr > MSR_F15H_PERF_CTR5 &&
-		    msr < MSR_F15H_PERF_CTL0 + 2 * pmu->nr_arch_gp_counters)
+		    msr < MSR_F15H_PERF_CTL0 + 2 * hweight_long(kvm_gp_pmc_mask(pmu)))
 			return pmu->version > 1;
 		break;
 	}
@@ -184,6 +183,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 {
+	unsigned int nr_gp_counters = AMD64_NUM_COUNTERS;
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	union cpuid_0x80000022_ebx ebx;
 
@@ -197,18 +197,16 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 		BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
 			     x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);
 		ebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)->ebx;
-		pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
+		nr_gp_counters = ebx.split.num_core_pmc;
 	} else if (guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
-		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;
-	} else {
-		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
+		nr_gp_counters = AMD64_NUM_COUNTERS_CORE;
 	}
 
-	pmu->nr_arch_gp_counters = min_t(unsigned int, pmu->nr_arch_gp_counters,
-					 kvm_pmu_cap.num_counters_gp);
+	pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
+			    (BIT_ULL(kvm_pmu_cap.num_counters_gp) - 1);
 
 	if (pmu->version > 1) {
-		pmu->global_ctrl_rsvd = ~(BIT_ULL(pmu->nr_arch_gp_counters) - 1);
+		pmu->global_ctrl_rsvd = ~pmu->pmc_exists64;
 		pmu->global_status_rsvd = pmu->global_ctrl_rsvd;
 	}
 
@@ -227,7 +225,6 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->raw_event_mask = AMD64_RAW_EVENT_MASK;
 	/* not applicable to AMD; but clean them to prevent any fall out */
 	pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
-	pmu->nr_arch_fixed_counters = 0;
 }
 
 static void amd_pmu_init(struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.c +6 -2
index 9d607b98bd06..fb8442a08b63 100644
@@ -753,18 +753,22 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 {
 	bool intercept = !kvm_vcpu_has_mediated_pmu(vcpu);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
 	int i;
 
 	if (!enable_mediated_pmu)
 		return;
 
 	/* Legacy counters are always available for AMD CPUs with a PMU. */
-	for (i = 0; i < min(pmu->nr_arch_gp_counters, AMD64_NUM_COUNTERS); i++)
+	for_each_set_bit(i, &gp_mask, AMD64_NUM_COUNTERS)
 		svm_set_intercept_for_msr(vcpu, MSR_K7_PERFCTR0 + i,
 					  MSR_TYPE_RW, intercept);
+	for_each_clear_bit(i, &gp_mask, AMD64_NUM_COUNTERS)
+		svm_enable_intercept_for_msr(vcpu, MSR_K7_PERFCTR0 + i,
+					     MSR_TYPE_RW);
 
 	intercept |= !guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE);
-	for (i = 0; i < pmu->nr_arch_gp_counters; i++)
+	kvm_for_each_gp_counter(i, gp_mask)
 		svm_set_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
 					  MSR_TYPE_RW, intercept);
 
arch/x86/kvm/vmx/nested.c +4 -2
index 7ed79894d11d..0cff369982ae 100644
@@ -697,6 +697,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
 	int i;
 
 	/*
@@ -706,12 +708,12 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
 	if (!kvm_vcpu_has_mediated_pmu(vcpu))
 		return;
 
-	for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+	kvm_for_each_gp_counter(i, gp_mask) {
 		nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PERFCTR0 + i);
 		nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PMC0 + i);
 	}
 
-	for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
+	kvm_for_each_fixed_counter(i, fixed_mask)
 		nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_FIXED_CTR0 + i);
 
 	nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);
arch/x86/kvm/vmx/pmu_intel.c +31 -15

124 lines not on this page · raw message

arch/x86/kvm/vmx/vmx.c +4 -2
index d3789b2ee18e..be994adbd954 100644
@@ -4230,6 +4230,8 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 	bool has_mediated_pmu = kvm_vcpu_has_mediated_pmu(vcpu);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
 	bool intercept = !has_mediated_pmu;
 	int i;
 
@@ -4250,7 +4252,7 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 
 	vm_exit_controls_changebit(vmx, vm_exit_controls_bits, has_mediated_pmu);
 
-	for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+	kvm_for_each_gp_counter(i, gp_mask) {
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
 					  MSR_TYPE_RW, intercept);
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i, MSR_TYPE_RW,
@@ -4263,7 +4265,7 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 					  MSR_TYPE_RW, true);
 	}
 
-	for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
+	kvm_for_each_fixed_counter(i, fixed_mask)
 		vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
 					  MSR_TYPE_RW, intercept);
 	for ( ; i < kvm_pmu_cap.num_counters_fixed; i++)
Signature · 1 line
2.55.0
Reviewed-by on v2 6/16
#22 lore raw

Re: [PATCH v2 06/16] KVM: x86/pmu: Drop nr_arch_{gp,fixed}_counters from kvm_pmu

Quoted 450 lines · Zide Chen

From: Dapeng Mi <[email protected]>

This is a preparatory step toward fully bitmap-based PMU capability handling. Later patches switch KVM's view of host PMU capabilities from counter counts to counter bitmaps.

Populate and use pmc_exists directly when walking guest-visible PMCs, instead of relying on pmu->nr_arch_{gp,fixed}_counters as intermediate state.

Iterate counters via the newly added pmc_exists based helpers, and remove the now-redundant nr_arch_{gp,fixed}_counters fields from struct kvm_pmu.

Note: {vmx,svm}_recalc_pmu_msr_intercepts() may leave stale intercepts on GP MSRs after CPUID narrowing. This corner case is resolved by the following patch: "KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities".

---
v2:
- Fix AMD legacy PMU MSR intercept handling for guest PMU narrowing reconfiguration by adding for_each_clear_bit_from().
- Re-word the stale interception changelog part.

---
 arch/x86/include/asm/kvm_host.h |  2 --
 arch/x86/kvm/pmu.c              | 26 +++++++++----------
 arch/x86/kvm/svm/pmu.c          | 25 ++++++++----------
 arch/x86/kvm/svm/svm.c          |  8 ++++--
 arch/x86/kvm/vmx/nested.c       |  6 +++--
 arch/x86/kvm/vmx/pmu_intel.c    | 46 ++++++++++++++++++++++-----------
 arch/x86/kvm/vmx/vmx.c          |  6 +++--
 7 files changed, 68 insertions(+), 51 deletions(-)
arch/x86/include/asm/kvm_host.h +0 -2
index 49a8a2e9cee0..717ba98c08c9 100644
@@ -569,8 +569,6 @@ struct kvm_pmc {
 
 struct kvm_pmu {
 	u8 version;
-	unsigned nr_arch_gp_counters;
-	unsigned nr_arch_fixed_counters;
 	unsigned available_event_types;
 	u64 fixed_ctr_ctrl;
 	u64 fixed_ctr_ctrl_hw;
arch/x86/kvm/pmu.c +12 -14
index 964e74bdaaa7..b0fcd0a538d2 100644
@@ -785,8 +785,8 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu)
 	 * KVM's capabilities are constrained based on KVM support, i.e. KVM's
 	 * capabilities themselves may be a subset of hardware capabilities.
 	 */
-	return pmu->nr_arch_gp_counters != kvm_host_pmu.num_counters_gp ||
-	       pmu->nr_arch_fixed_counters != kvm_host_pmu.num_counters_fixed;
+	return kvm_gp_pmc_mask(pmu) != BIT_ULL(kvm_host_pmu.num_counters_gp) - 1 ||
+	       kvm_fixed_pmc_mask(pmu) != BIT_ULL(kvm_host_pmu.num_counters_fixed) - 1;
 }
 
 bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
@@ -985,8 +985,6 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	kvm_pmu_reset(vcpu);
 
 	pmu->version = 0;
-	pmu->nr_arch_gp_counters = 0;
-	pmu->nr_arch_fixed_counters = 0;
 	pmu->counter_bitmask[KVM_PMC_GP] = 0;
 	pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
 	/*
@@ -1014,16 +1012,12 @@ void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
 	 * in the global controls).  Emulate that behavior when refreshing the
 	 * PMU so that userspace doesn't need to manually set PERF_GLOBAL_CTRL.
 	 */
-	if (pmu->nr_arch_gp_counters &&
+	if (kvm_gp_pmc_mask(pmu) &&
 	    (kvm_pmu_has_perf_global_ctrl(pmu) || kvm_vcpu_has_mediated_pmu(vcpu)))
-		pmu->global_ctrl = GENMASK_ULL(pmu->nr_arch_gp_counters - 1, 0);
+		pmu->global_ctrl = kvm_gp_pmc_mask(pmu);
 
 	if (kvm_vcpu_has_mediated_pmu(vcpu))
 		kvm_pmu_call(write_global_ctrl)(pmu->global_ctrl);
-
-	bitmap_set(pmu->pmc_exists, 0, pmu->nr_arch_gp_counters);
-	bitmap_set(pmu->pmc_exists, KVM_FIXED_PMC_BASE_IDX,
-		   pmu->nr_arch_fixed_counters);
 }
 
 void kvm_pmu_init(struct kvm_vcpu *vcpu)
@@ -1332,6 +1326,8 @@ static __always_inline u32 gp_eventsel_msr(u32 idx)
 static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
 	struct kvm_pmc *pmc;
 	u32 i;
 
@@ -1340,14 +1336,14 @@ static void kvm_pmu_load_guest_pmcs(struct kvm_vcpu *vcpu)
 	 * is intercepted if hardware has counters that aren't visible to the
 	 * guest (KVM will inject #GP as appropriate).
 	 */
-	for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+	kvm_for_each_gp_counter(i, gp_mask) {
 		pmc = &pmu->gp_counters[i];
 
 		if (pmc->counter != rdpmc(i))
 			wrmsrq(gp_counter_msr(i), pmc->counter);
 		wrmsrq(gp_eventsel_msr(i), pmc->eventsel_hw);
 	}
-	for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
+	kvm_for_each_fixed_counter(i, fixed_mask) {
 		pmc = &pmu->fixed_counters[i];
 
 		if (pmc->counter != rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i))
@@ -1390,6 +1386,8 @@ void kvm_mediated_pmu_load(struct kvm_vcpu *vcpu)
 static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
 	struct kvm_pmc *pmc;
 	u32 i;
 
@@ -1397,7 +1395,7 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
 	 * Clear selectors and counters to ensure hardware doesn't count using
 	 * guest controls when the host (perf) restores its state.
 	 */
-	for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+	kvm_for_each_gp_counter(i, gp_mask) {
 		pmc = &pmu->gp_counters[i];
 
 		pmc->counter = rdpmc(i);
@@ -1407,7 +1405,7 @@ static void kvm_pmu_put_guest_pmcs(struct kvm_vcpu *vcpu)
 			wrmsrq(gp_eventsel_msr(i), 0);
 	}
 
-	for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
+	kvm_for_each_fixed_counter(i, fixed_mask) {
 		pmc = &pmu->fixed_counters[i];
 
 		pmc->counter = rdpmc(INTEL_PMC_FIXED_RDPMC_BASE | i);
arch/x86/kvm/svm/pmu.c +11 -14
index 52224872e0d4..0943ccc1d6b8 100644
@@ -27,12 +27,11 @@ enum pmu_type {
 
 static struct kvm_pmc *amd_pmu_get_pmc(struct kvm_pmu *pmu, int pmc_idx)
 {
-	unsigned int num_counters = pmu->nr_arch_gp_counters;
-
-	if (pmc_idx >= num_counters)
+	if (!kvm_is_gp_pmc_supported(pmu, pmc_idx))
 		return NULL;
 
-	return &pmu->gp_counters[array_index_nospec(pmc_idx, num_counters)];
+	pmc_idx = array_index_nospec(pmc_idx, KVM_MAX_NR_AMD_GP_COUNTERS);
+	return &pmu->gp_counters[pmc_idx];
 }
 
 static inline struct kvm_pmc *get_gp_pmc_amd(struct kvm_pmu *pmu, u32 msr,
@@ -77,7 +76,7 @@ static int amd_check_rdpmc_early(struct kvm_vcpu *vcpu, unsigned int idx)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 
-	if (idx >= pmu->nr_arch_gp_counters)
+	if (!kvm_is_gp_pmc_supported(pmu, idx))
 		return -EINVAL;
 
 	return 0;
@@ -117,7 +116,7 @@ static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 		return pmu->version > 1;
 	default:
 		if (msr > MSR_F15H_PERF_CTR5 &&
-		    msr < MSR_F15H_PERF_CTL0 + 2 * pmu->nr_arch_gp_counters)
+		    msr < MSR_F15H_PERF_CTL0 + 2 * hweight_long(kvm_gp_pmc_mask(pmu)))
 			return pmu->version > 1;
 		break;
 	}
@@ -184,6 +183,7 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 
 static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 {
+	unsigned int nr_gp_counters = AMD64_NUM_COUNTERS;
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	union cpuid_0x80000022_ebx ebx;
 
@@ -197,18 +197,16 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 		BUILD_BUG_ON(x86_feature_cpuid(X86_FEATURE_PERFMON_V2).function != 0x80000022 ||
 			     x86_feature_cpuid(X86_FEATURE_PERFMON_V2).index);
 		ebx.full = kvm_find_cpuid_entry_index(vcpu, 0x80000022, 0)->ebx;
-		pmu->nr_arch_gp_counters = ebx.split.num_core_pmc;
+		nr_gp_counters = ebx.split.num_core_pmc;
 	} else if (guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
-		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS_CORE;
-	} else {
-		pmu->nr_arch_gp_counters = AMD64_NUM_COUNTERS;
+		nr_gp_counters = AMD64_NUM_COUNTERS_CORE;
 	}
 
-	pmu->nr_arch_gp_counters = min_t(unsigned int, pmu->nr_arch_gp_counters,
-					 kvm_pmu_cap.num_counters_gp);
+	pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
+			    (BIT_ULL(kvm_pmu_cap.num_counters_gp) - 1);
 
 	if (pmu->version > 1) {
-		pmu->global_ctrl_rsvd = ~(BIT_ULL(pmu->nr_arch_gp_counters) - 1);
+		pmu->global_ctrl_rsvd = ~pmu->pmc_exists64;
 		pmu->global_status_rsvd = pmu->global_ctrl_rsvd;
 	}
 
@@ -227,7 +225,6 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->raw_event_mask = AMD64_RAW_EVENT_MASK;
 	/* not applicable to AMD; but clean them to prevent any fall out */
 	pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
-	pmu->nr_arch_fixed_counters = 0;
 }
 
 static void amd_pmu_init(struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/svm.c +6 -2
index 9d607b98bd06..fb8442a08b63 100644
@@ -753,18 +753,22 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 {
 	bool intercept = !kvm_vcpu_has_mediated_pmu(vcpu);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
 	int i;
 
 	if (!enable_mediated_pmu)
 		return;
 
 	/* Legacy counters are always available for AMD CPUs with a PMU. */
-	for (i = 0; i < min(pmu->nr_arch_gp_counters, AMD64_NUM_COUNTERS); i++)
+	for_each_set_bit(i, &gp_mask, AMD64_NUM_COUNTERS)
 		svm_set_intercept_for_msr(vcpu, MSR_K7_PERFCTR0 + i,
 					  MSR_TYPE_RW, intercept);
+	for_each_clear_bit(i, &gp_mask, AMD64_NUM_COUNTERS)
+		svm_enable_intercept_for_msr(vcpu, MSR_K7_PERFCTR0 + i,
+					     MSR_TYPE_RW);
 
 	intercept |= !guest_cpu_cap_has(vcpu, X86_FEATURE_PERFCTR_CORE);
-	for (i = 0; i < pmu->nr_arch_gp_counters; i++)
+	kvm_for_each_gp_counter(i, gp_mask)
 		svm_set_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
 					  MSR_TYPE_RW, intercept);
 
arch/x86/kvm/vmx/nested.c +4 -2
index 7ed79894d11d..0cff369982ae 100644
@@ -697,6 +697,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
 	int i;
 
 	/*
@@ -706,12 +708,12 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
 	if (!kvm_vcpu_has_mediated_pmu(vcpu))
 		return;
 
-	for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+	kvm_for_each_gp_counter(i, gp_mask) {
 		nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PERFCTR0 + i);
 		nested_vmx_merge_msr_bitmaps_rw(MSR_IA32_PMC0 + i);
 	}
 
-	for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
+	kvm_for_each_fixed_counter(i, fixed_mask)
 		nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_FIXED_CTR0 + i);
 
 	nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);
arch/x86/kvm/vmx/pmu_intel.c +31 -15
index 9fdae437e569..926d1c57f8bf 100644
@@ -66,12 +66,13 @@ static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
 	 * hardware, e.g. to ensure the event filter is enforced.
 	 */
 	u64 old_fixed_ctr_ctrl = pmu->fixed_ctr_ctrl_hw;
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
 	struct kvm_pmc *pmc;
 	int i;
 
 	pmu->fixed_ctr_ctrl = data;
 	pmu->fixed_ctr_ctrl_hw = data;
-	for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
+	kvm_for_each_fixed_counter(i, fixed_mask) {
 		u8 new_ctrl = fixed_ctrl_field(data, i);
 		u8 old_ctrl = fixed_ctrl_field(old_fixed_ctr_ctrl, i);
 
@@ -107,6 +108,8 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
 	if (WARN_ON_ONCE(!pmu->version))
 		return NULL;
 
+	idx &= INTEL_RDPMC_INDEX_MASK;
+
 	/*
 	 * General Purpose (GP) PMCs are supported on all PMUs, and fixed PMCs
 	 * are supported on all architectural PMUs, i.e. on all virtual PMUs
@@ -117,23 +120,25 @@ static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
 	 */
 	switch (type) {
 	case INTEL_RDPMC_FIXED:
+		if (!kvm_is_fixed_pmc_supported(pmu, idx))
+			return NULL;
+
 		counters = pmu->fixed_counters;
-		num_counters = pmu->nr_arch_fixed_counters;
+		num_counters = KVM_MAX_NR_INTEL_FIXED_COUNTERS;
 		bitmask = pmu->counter_bitmask[KVM_PMC_FIXED];
 		break;
 	case INTEL_RDPMC_GP:
+		if (!kvm_is_gp_pmc_supported(pmu, idx))
+			return NULL;
+
 		counters = pmu->gp_counters;
-		num_counters = pmu->nr_arch_gp_counters;
+		num_counters = KVM_MAX_NR_INTEL_GP_COUNTERS;
 		bitmask = pmu->counter_bitmask[KVM_PMC_GP];
 		break;
 	default:
 		return NULL;
 	}
 
-	idx &= INTEL_RDPMC_INDEX_MASK;
-	if (idx >= num_counters)
-		return NULL;
-
 	*mask &= bitmask;
 	return &counters[array_index_nospec(idx, num_counters)];
 }
@@ -492,9 +497,10 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)
 
 static void intel_pmu_enable_fixed_counter_bits(struct kvm_pmu *pmu, u64 bits)
 {
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
 	int i;
 
-	for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
+	kvm_for_each_fixed_counter(i, fixed_mask)
 		pmu->fixed_ctr_ctrl_rsvd &= ~intel_fixed_bits_by_idx(i, bits);
 }
 
@@ -506,6 +512,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	union cpuid10_eax eax;
 	union cpuid10_edx edx;
 	u64 perf_capabilities;
+	u64 fixed_cntr_mask;
+	int nr_gp_counters;
 	u64 counter_rsvd;
 
 	if (!lbr_desc)
@@ -532,8 +540,6 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (!pmu->version)
 		return;
 
-	pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters,
-					 kvm_pmu_cap.num_counters_gp);
 	eax.split.bit_width = min_t(int, eax.split.bit_width,
 				    kvm_pmu_cap.bit_width_gp);
 	pmu->counter_bitmask[KVM_PMC_GP] = BIT_ULL(eax.split.bit_width) - 1;
@@ -541,6 +547,17 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 				      kvm_pmu_cap.events_mask_len);
 	pmu->available_event_types = ~entry->ebx & (BIT_ULL(eax.split.mask_length) - 1);
 
+	fixed_cntr_mask = BIT_ULL(edx.split.num_counters_fixed) - 1;
+	fixed_cntr_mask &= BIT_ULL(kvm_pmu_cap.num_counters_fixed) - 1;
+
+	/*
+	 * The number of counters comes from guest CPUID data. Clamp the value
+	 * to avoid a shift-by-64 in BIT_ULL().
+	 */
+	nr_gp_counters = min_t(int, eax.split.num_counters, X86_PMC_IDX_MAX - 1);
+	pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
+			    (BIT_ULL(kvm_pmu_cap.num_counters_gp) - 1);
+
 	entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
 	if (entry &&
 	    (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
@@ -562,8 +579,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	if (pmu->version == 1)
 		return;
 
-	pmu->nr_arch_fixed_counters = min_t(int, edx.split.num_counters_fixed,
-					    kvm_pmu_cap.num_counters_fixed);
+	pmu->pmc_exists64 |= fixed_cntr_mask << INTEL_PMC_IDX_FIXED;
 	edx.split.bit_width_fixed = min_t(int, edx.split.bit_width_fixed,
 					  kvm_pmu_cap.bit_width_fixed);
 	pmu->counter_bitmask[KVM_PMC_FIXED] = BIT_ULL(edx.split.bit_width_fixed) - 1;
@@ -572,8 +588,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 						 INTEL_FIXED_0_USER |
 						 INTEL_FIXED_0_ENABLE_PMI);
 
-	counter_rsvd = ~((BIT_ULL(pmu->nr_arch_gp_counters) - 1) |
-			 ((BIT_ULL(pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
+	counter_rsvd = ~(kvm_gp_pmc_mask(pmu) |
+			((u64)kvm_fixed_pmc_mask(pmu) << KVM_FIXED_PMC_BASE_IDX));
 	pmu->global_ctrl_rsvd = counter_rsvd;
 
 	/*
@@ -596,7 +612,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 			pmu->pebs_data_cfg_rsvd = ~0xff00000full;
 			intel_pmu_enable_fixed_counter_bits(pmu, ICL_FIXED_0_ADAPTIVE);
 		} else {
-			pmu->pebs_enable_rsvd = ~(BIT_ULL(pmu->nr_arch_gp_counters) - 1);
+			pmu->pebs_enable_rsvd = ~kvm_gp_pmc_mask(pmu);
 		}
 	}
 }
arch/x86/kvm/vmx/vmx.c +4 -2
index d3789b2ee18e..be994adbd954 100644
@@ -4230,6 +4230,8 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 	bool has_mediated_pmu = kvm_vcpu_has_mediated_pmu(vcpu);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
 	bool intercept = !has_mediated_pmu;
 	int i;
 
@@ -4250,7 +4252,7 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 
 	vm_exit_controls_changebit(vmx, vm_exit_controls_bits, has_mediated_pmu);
 
-	for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+	kvm_for_each_gp_counter(i, gp_mask) {
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
 					  MSR_TYPE_RW, intercept);
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i, MSR_TYPE_RW,
@@ -4263,7 +4265,7 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 					  MSR_TYPE_RW, true);
 	}
 
-	for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
+	kvm_for_each_fixed_counter(i, fixed_mask)
 		vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
 					  MSR_TYPE_RW, intercept);
 	for ( ; i < kvm_pmu_cap.num_counters_fixed; i++)
#23 lore raw

Re: [PATCH v2 06/16] KVM: x86/pmu: Drop nr_arch_{gp,fixed}_counters from kvm_pmu

In reply to Zide Chen
Quoted 11 lines · Zide Chen

From: Dapeng Mi <[email protected]>

This is a preparatory step toward fully bitmap-based PMU capability handling. Later patches switch KVM's view of host PMU capabilities from counter counts to counter bitmaps.

Populate and use pmc_exists directly when walking guest-visible PMCs, instead of relying on pmu->nr_arch_{gp,fixed}_counters as intermediate state.

Quoted 8 lines

Iterate counters via the newly added pmc_exists based helpers, and remove the now-redundant nr_arch_{gp,fixed}_counters fields from struct kvm_pmu.

Note: {vmx,svm}_recalc_pmu_msr_intercepts() may leave stale intercepts on GP MSRs after CPUID narrowing. This corner case is resolved by the following patch: "KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities".

Are you saying that you are deliberately breaking bisection?!? Shame on you!

Quoted 9 lines

---
v2:
- Fix AMD legacy PMU MSR intercept handling for guest PMU narrowing reconfiguration by adding for_each_clear_bit_from().
- Re-word the stale interception changelog part.
---

[ ... ]

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

Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +1 -1
-       for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
+       kvm_for_each_fixed_counter(i, fixed_mask)
                vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
                                          MSR_TYPE_RW, intercept);
        for ( ; i < kvm_pmu_cap.num_counters_fixed; i++)

Doesn't this potentially leave stale intercepts on FC MSRs after CPUID narrowing as well?

author
#24 lore raw

Re: [PATCH v2 06/16] KVM: x86/pmu: Drop nr_arch_{gp,fixed}_counters from kvm_pmu

Quoted 16 lines · Jim Mattson
Quoted 15 lines · Zide Chen

From: Dapeng Mi <[email protected]>

This is a preparatory step toward fully bitmap-based PMU capability handling. Later patches switch KVM's view of host PMU capabilities from counter counts to counter bitmaps.

Populate and use pmc_exists directly when walking guest-visible PMCs, instead of relying on pmu->nr_arch_{gp,fixed}_counters as intermediate state.

Iterate counters via the newly added pmc_exists based helpers, and remove the now-redundant nr_arch_{gp,fixed}_counters fields from struct kvm_pmu.

Quoted 6 lines
Quoted 4 lines

Note: {vmx,svm}_recalc_pmu_msr_intercepts() may leave stale intercepts on GP MSRs after CPUID narrowing. This corner case is resolved by the following patch: "KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities".

Are you saying that you are deliberately breaking bisection?!? Shame on you!

Yeah, I feel bad about this... the other solution could be to temporarily use for_each_clear_bit().

- for ( ; i < kvm_pmu_cap.num_counters_gp; i++) {
+ for_each_clear_bit(i, &gp_mask, kvm_pmu_cap.num_counters_gp) {
vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
MSR_TYPE_RW, true);

And then remove for_each_clear_bit() in the coming patch when kvm_pmu_cap.num_counters_gp is dropped:

- for_each_clear_bit(i, &gp_mask, kvm_pmu_cap.num_counters_gp) {

+ host_only_gp_mask = kvm_host_pmu.cntr_mask64 & ~gp_mask;
+ for_each_set_bit(i, &host_only_gp_mask, INTEL_PMC_MAX_GENERIC) {

Quoted 10 lines
Quoted 9 lines

---
v2:
- Fix AMD legacy PMU MSR intercept handling for guest PMU narrowing reconfiguration by adding for_each_clear_bit_from().
- Re-word the stale interception changelog part.
---

[ ... ]

Quoted 8 lines

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

Quoted 5 lines
arch/x86/kvm/vmx/vmx.c +1 -1
-       for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
+       kvm_for_each_fixed_counter(i, fixed_mask)
                vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
                                          MSR_TYPE_RW, intercept);
        for ( ; i < kvm_pmu_cap.num_counters_fixed; i++)

Doesn't this potentially leave stale intercepts on FC MSRs after CPUID narrowing as well?

Yes.

author
#25 lore raw

PATCH v2 7/16 KVM: x86/pmu: Expose kvm_host_pmu to vendor modules

In reply to Zide Chen

kvm_host_pmu holds the unadulterated host PMU capabilities, which can be used to compare against guest capabilities to determine whether certain MSRs should be intercepted by KVM.

Exposing it directly avoids the need to introduce per-field accessors, which would add boilerplate without hiding any implementation detail worth encapsulating.

Opportunistically, fix typo "Unadultered" in the comment.

---
 arch/x86/kvm/pmu.c | 5 +++--
 arch/x86/kvm/pmu.h | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)
arch/x86/kvm/pmu.c +3 -2
index b0fcd0a538d2..7837e2e1af98 100644
@@ -27,8 +27,9 @@
 /* This is enough to filter the vast majority of currently defined events. */
 #define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300
 
-/* Unadultered PMU capabilities of the host, i.e. of hardware. */
-static struct x86_pmu_capability __read_mostly kvm_host_pmu;
+/* Unadulterated PMU capabilities of the host, i.e. of hardware. */
+struct x86_pmu_capability __read_mostly kvm_host_pmu;
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_host_pmu);
 
 /* KVM's PMU capabilities, i.e. the intersection of KVM and hardware support. */
 struct x86_pmu_capability __read_mostly kvm_pmu_cap;
arch/x86/kvm/pmu.h +1 -0
index b7a319b1eccc..9a658ae5b268 100644
@@ -252,6 +252,7 @@ static inline bool pmc_is_locally_enabled(struct kvm_pmc *pmc)
 	return !kvm_pmu_call(pmc_is_disabled_in_current_mode)(pmc);
 }
 
+extern struct x86_pmu_capability kvm_host_pmu;
 extern struct x86_pmu_capability kvm_pmu_cap;
 
 void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops);
Signature · 1 line
2.55.0
Reviewed-by on v2 7/16
#26 lore raw

Re: [PATCH v2 07/16] KVM: x86/pmu: Expose kvm_host_pmu to vendor modules

Quoted 44 lines · Zide Chen

kvm_host_pmu holds the unadulterated host PMU capabilities, which can be used to compare against guest capabilities to determine whether certain MSRs should be intercepted by KVM.

Exposing it directly avoids the need to introduce per-field accessors, which would add boilerplate without hiding any implementation detail worth encapsulating.

Opportunistically, fix typo "Unadultered" in the comment.

---
 arch/x86/kvm/pmu.c | 5 +++--
 arch/x86/kvm/pmu.h | 1 +
 2 files changed, 4 insertions(+), 2 deletions(-)
arch/x86/kvm/pmu.c +3 -2
index b0fcd0a538d2..7837e2e1af98 100644
@@ -27,8 +27,9 @@
 /* This is enough to filter the vast majority of currently defined events. */
 #define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300
 
-/* Unadultered PMU capabilities of the host, i.e. of hardware. */
-static struct x86_pmu_capability __read_mostly kvm_host_pmu;
+/* Unadulterated PMU capabilities of the host, i.e. of hardware. */
+struct x86_pmu_capability __read_mostly kvm_host_pmu;
+EXPORT_SYMBOL_FOR_KVM_INTERNAL(kvm_host_pmu);
 
 /* KVM's PMU capabilities, i.e. the intersection of KVM and hardware support. */
 struct x86_pmu_capability __read_mostly kvm_pmu_cap;
arch/x86/kvm/pmu.h +1 -0
index b7a319b1eccc..9a658ae5b268 100644
@@ -252,6 +252,7 @@ static inline bool pmc_is_locally_enabled(struct kvm_pmc *pmc)
 	return !kvm_pmu_call(pmc_is_disabled_in_current_mode)(pmc);
 }
 
+extern struct x86_pmu_capability kvm_host_pmu;
 extern struct x86_pmu_capability kvm_pmu_cap;
 
 void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops);
Reviewed-by on v2 7/16
#27 lore raw

Re: [PATCH v2 07/16] KVM: x86/pmu: Expose kvm_host_pmu to vendor modules

In reply to Zide Chen
Quoted 5 lines · Zide Chen

kvm_host_pmu holds the unadulterated host PMU capabilities, which can be used to compare against guest capabilities to determine whether certain MSRs should be intercepted by KVM.

Quoted 7 lines

Exposing it directly avoids the need to introduce per-field accessors, which would add boilerplate without hiding any implementation detail worth encapsulating.

Opportunistically, fix typo "Unadultered" in the comment.

author
#28 lore raw

PATCH v2 8/16 perf/x86: Plumb counter bitmap from x86_pmu to x86_pmu_cap

In reply to Zide Chen

From: Dapeng Mi <[email protected]>

Intel PerfMon v5 introduced CPUID.0AH:ECX to support non-contiguous fixed counters and Architectural PerfMon Extension leaf (0x23) further supports non-contiguous general-purpose counters.

num_counters_{gp,fixed} indicates the total number of GP or fixed counters, but cannot represent non-contiguous counters.

Add cntr_mask and fixed_cntr_mask unions so that KVM can get the accurate counter availability directly from x86_pmu_cap. The u64 alias is convenient for mask arithmetic, while the bitmap form works with for_each_set_bit() and friends.

num_counters_{gp,fixed} in x86_pmu_capability will be removed once callers have been converted to the use of {,fixed_}cntr_mask.

---
 arch/x86/events/core.c            | 6 ++++--
 arch/x86/include/asm/perf_event.h | 8 ++++++++
 2 files changed, 12 insertions(+), 2 deletions(-)
arch/x86/events/core.c +4 -2
index d1af33d96d0a..3fb95090cd8f 100644
@@ -3134,8 +3134,10 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
 	cap->version		= x86_pmu.version;
 	cap->num_counters_gp	= x86_pmu_num_counters(NULL);
 	cap->num_counters_fixed	= x86_pmu_num_counters_fixed(NULL);
-	cap->bit_width_gp	= cap->num_counters_gp ? x86_pmu.cntval_bits : 0;
-	cap->bit_width_fixed	= cap->num_counters_fixed ? x86_pmu.cntval_bits : 0;
+	cap->cntr_mask64	= x86_pmu.cntr_mask64;
+	cap->fixed_cntr_mask64	= x86_pmu.fixed_cntr_mask64;
+	cap->bit_width_gp	= cap->cntr_mask64 ? x86_pmu.cntval_bits : 0;
+	cap->bit_width_fixed	= cap->fixed_cntr_mask64 ? x86_pmu.cntval_bits : 0;
 	cap->events_mask	= (unsigned int)x86_pmu.events_maskl;
 	cap->events_mask_len	= x86_pmu.events_mask_len;
 	cap->pebs_ept		= x86_pmu.pebs_ept;
arch/x86/include/asm/perf_event.h +8 -0
index 1eb13673e889..190fda412a61 100644
@@ -302,6 +302,14 @@ struct x86_pmu_capability {
 	int		version;
 	int		num_counters_gp;
 	int		num_counters_fixed;
+	union {
+		u64	cntr_mask64;
+		DECLARE_BITMAP(cntr_mask, X86_PMC_IDX_MAX);
+	};
+	union {
+		u64	fixed_cntr_mask64;
+		DECLARE_BITMAP(fixed_cntr_mask, X86_PMC_IDX_MAX);
+	};
 	int		bit_width_gp;
 	int		bit_width_fixed;
 	unsigned int	events_mask;
Signature · 1 line
2.55.0
Reviewed-by on v2 8/16
#29 lore raw

Re: [PATCH v2 08/16] perf/x86: Plumb counter bitmap from x86_pmu to x86_pmu_cap

Quoted 15 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Intel PerfMon v5 introduced CPUID.0AH:ECX to support non-contiguous fixed counters and Architectural PerfMon Extension leaf (0x23) further supports non-contiguous general-purpose counters.

num_counters_{gp,fixed} indicates the total number of GP or fixed counters, but cannot represent non-contiguous counters.

Add cntr_mask and fixed_cntr_mask unions so that KVM can get the accurate counter availability directly from x86_pmu_cap. The u64 alias is convenient for mask arithmetic, while the bitmap form works with for_each_set_bit() and friends.

Quoted 5 lines

num_counters_{gp,fixed} in x86_pmu_capability will be removed once callers have been converted to the use of {,fixed_}cntr_mask.

author
#30 lore raw

PATCH v2 9/16 KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities

In reply to Zide Chen

From: Dapeng Mi <[email protected]>

Intel platforms support non-contiguous fixed counters via CPUID.0AH:ECX starting with PerfMon v5, and support non-contiguous GP counters through the Architectural PerfMon Extension (CPUID leaf 23H).

struct x86_pmu_capability now exposes {,fixed_}cntr_mask64 bitmaps, which may contain sparse bits representing non-contiguous counters. Switch KVM's kvm_host_pmu and kvm_pmu_cap consumers over to the new bitmask fields.

CPUID.0AH:EAX[15:8] and CPUID.0AH:EDX[4:0] enumerate only contiguous counters. Derive these values from kvm_pmu_cap.{,fixed_}cntr_mask64 as the number of consecutive counters starting at index 0.

---
 arch/x86/kvm/cpuid.c         | 14 +++++++++++---
 arch/x86/kvm/msrs.c          | 12 ++++++------
 arch/x86/kvm/pmu.c           | 21 ++++++++++++---------
 arch/x86/kvm/svm/pmu.c       |  2 +-
 arch/x86/kvm/svm/svm.c       |  9 +++++----
 arch/x86/kvm/vmx/pmu_intel.c |  7 ++++---
 arch/x86/kvm/vmx/vmx.c       |  7 +++++--
 7 files changed, 44 insertions(+), 28 deletions(-)
arch/x86/kvm/cpuid.c +11 -3
index ddb022cb203a..106e719e7aee 100644
@@ -1528,10 +1528,18 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 		}
 
 		eax.split.version_id = kvm_pmu_cap.version;
-		eax.split.num_counters = kvm_pmu_cap.num_counters_gp;
+
+		/* Contiguous GP counters only. */
+		eax.split.num_counters =
+			find_first_zero_bit(kvm_pmu_cap.cntr_mask,
+					    KVM_MAX_NR_GP_COUNTERS);
 		eax.split.bit_width = kvm_pmu_cap.bit_width_gp;
 		eax.split.mask_length = kvm_pmu_cap.events_mask_len;
-		edx.split.num_counters_fixed = kvm_pmu_cap.num_counters_fixed;
+
+		/* Contiguous fixed counters only. */
+		edx.split.num_counters_fixed =
+			find_first_zero_bit(kvm_pmu_cap.fixed_cntr_mask,
+					    KVM_MAX_NR_FIXED_COUNTERS);
 		edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;
 
 		if (kvm_pmu_cap.version)
@@ -1896,7 +1904,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 
 		cpuid_entry_override(entry, CPUID_8000_0022_EAX);
 
-		ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
+		ebx.split.num_core_pmc = hweight64(kvm_pmu_cap.cntr_mask64);
 		entry->ebx = ebx.full;
 		break;
 	}
arch/x86/kvm/msrs.c +6 -6
index 66fa7140d65d..49ab49f7c96e 100644
@@ -2610,20 +2610,20 @@ static void kvm_probe_msr_to_save(u32 msr_index)
 		break;
 	case MSR_ARCH_PERFMON_PERFCTR0 ...
 	     MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
-		if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
-		    kvm_pmu_cap.num_counters_gp)
+		if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_PERFCTR0) &
+		      kvm_pmu_cap.cntr_mask64))
 			return;
 		break;
 	case MSR_ARCH_PERFMON_EVENTSEL0 ...
 	     MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1:
-		if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >=
-		    kvm_pmu_cap.num_counters_gp)
+		if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_EVENTSEL0) &
+		      kvm_pmu_cap.cntr_mask64))
 			return;
 		break;
 	case MSR_ARCH_PERFMON_FIXED_CTR0 ...
 	     MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1:
-		if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >=
-		    kvm_pmu_cap.num_counters_fixed)
+		if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_FIXED_CTR0) &
+		      kvm_pmu_cap.fixed_cntr_mask64))
 			return;
 		break;
 	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
arch/x86/kvm/pmu.c +12 -9
index 7837e2e1af98..437a7bc49bf8 100644
@@ -20,7 +20,6 @@
 #include <asm/perf_event.h>
 #include <asm/cpu_device_id.h>
 #include "x86.h"
-#include "cpuid.h"
 #include "lapic.h"
 #include "pmu.h"
 
@@ -156,8 +155,8 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
 		 * there are a non-zero number of counters, but fewer than what
 		 * is architecturally required.
 		 */
-		if (!kvm_host_pmu.num_counters_gp ||
-		    WARN_ON_ONCE(kvm_host_pmu.num_counters_gp < min_nr_gp_ctrs))
+		if (!kvm_host_pmu.cntr_mask64 ||
+		    WARN_ON_ONCE(hweight64(kvm_host_pmu.cntr_mask64) < min_nr_gp_ctrs))
 			enable_pmu = false;
 		else if (is_intel && !kvm_host_pmu.version)
 			enable_pmu = false;
@@ -177,10 +176,14 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
 
 	memcpy(&kvm_pmu_cap, &kvm_host_pmu, sizeof(kvm_host_pmu));
 	kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
-	kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,
-					  pmu_ops->MAX_NR_GP_COUNTERS);
-	kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,
-					     KVM_MAX_NR_FIXED_COUNTERS);
+	kvm_pmu_cap.cntr_mask64 &=
+		GENMASK_ULL(pmu_ops->MAX_NR_GP_COUNTERS - 1, 0);
+	kvm_pmu_cap.fixed_cntr_mask64 &=
+		GENMASK_ULL(KVM_MAX_NR_FIXED_COUNTERS - 1, 0);
+
+	/* Legacy vPMU exposes at most 3 fixed counters. */
+	if (!enable_mediated_pmu)
+		kvm_pmu_cap.fixed_cntr_mask64 &= GENMASK_ULL(2, 0);
 
 	kvm_pmu_eventsel.INSTRUCTIONS_RETIRED =
 		perf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS);
@@ -786,8 +789,8 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu)
 	 * KVM's capabilities are constrained based on KVM support, i.e. KVM's
 	 * capabilities themselves may be a subset of hardware capabilities.
 	 */
-	return kvm_gp_pmc_mask(pmu) != BIT_ULL(kvm_host_pmu.num_counters_gp) - 1 ||
-	       kvm_fixed_pmc_mask(pmu) != BIT_ULL(kvm_host_pmu.num_counters_fixed) - 1;
+	return kvm_gp_pmc_mask(pmu) != kvm_host_pmu.cntr_mask64 ||
+	       kvm_fixed_pmc_mask(pmu) != kvm_host_pmu.fixed_cntr_mask64;
 }
 
 bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/pmu.c +1 -1
index 0943ccc1d6b8..fb5a298244ec 100644
@@ -203,7 +203,7 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 	}
 
 	pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
-			    (BIT_ULL(kvm_pmu_cap.num_counters_gp) - 1);
+			    kvm_pmu_cap.cntr_mask64;
 
 	if (pmu->version > 1) {
 		pmu->global_ctrl_rsvd = ~pmu->pmc_exists64;
arch/x86/kvm/svm/svm.c +5 -4
index fb8442a08b63..6f3427027e99 100644
@@ -754,6 +754,7 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 	bool intercept = !kvm_vcpu_has_mediated_pmu(vcpu);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+	unsigned long host_only_gp_mask;
 	int i;
 
 	if (!enable_mediated_pmu)
@@ -772,7 +773,8 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 		svm_set_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
 					  MSR_TYPE_RW, intercept);
 
-	for ( ; i < kvm_pmu_cap.num_counters_gp; i++)
+	host_only_gp_mask = kvm_pmu_cap.cntr_mask64 & ~gp_mask;
+	kvm_for_each_gp_counter(i, host_only_gp_mask)
 		svm_enable_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
 					     MSR_TYPE_RW);
 
@@ -5563,9 +5565,8 @@ static __init void svm_set_cpu_caps(void)
 		 * access to enough counters to virtualize "core" support,
 		 * otherwise limit vPMU support to the legacy number of counters.
 		 */
-		if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)
-			kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
-							  kvm_pmu_cap.num_counters_gp);
+		if (hweight64(kvm_pmu_cap.cntr_mask64) < AMD64_NUM_COUNTERS_CORE)
+			kvm_pmu_cap.cntr_mask64 &= GENMASK_ULL(AMD64_NUM_COUNTERS - 1, 0);
 		else
 			kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
 
arch/x86/kvm/vmx/pmu_intel.c +4 -3
index 926d1c57f8bf..4df55a3e21da 100644
@@ -491,7 +491,8 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)
 	 * have a known encoding for the associated general purpose event.
 	 */
 	eventsel = perf_get_hw_event_config(fixed_pmc_perf_ids[index]);
-	WARN_ON_ONCE(!eventsel && index < kvm_pmu_cap.num_counters_fixed);
+	WARN_ON_ONCE(!eventsel &&
+		     (kvm_pmu_cap.fixed_cntr_mask64 & BIT_ULL(index)));
 	return eventsel;
 }
 
@@ -548,7 +549,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->available_event_types = ~entry->ebx & (BIT_ULL(eax.split.mask_length) - 1);
 
 	fixed_cntr_mask = BIT_ULL(edx.split.num_counters_fixed) - 1;
-	fixed_cntr_mask &= BIT_ULL(kvm_pmu_cap.num_counters_fixed) - 1;
+	fixed_cntr_mask &= kvm_pmu_cap.fixed_cntr_mask64;
 
 	/*
 	 * The number of counters comes from guest CPUID data. Clamp the value
@@ -556,7 +557,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	 */
 	nr_gp_counters = min_t(int, eax.split.num_counters, X86_PMC_IDX_MAX - 1);
 	pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
-			    (BIT_ULL(kvm_pmu_cap.num_counters_gp) - 1);
+			    kvm_pmu_cap.cntr_mask64;
 
 	entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
 	if (entry &&
arch/x86/kvm/vmx/vmx.c +5 -2
index be994adbd954..56daf5c61082 100644
@@ -4230,6 +4230,7 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 	bool has_mediated_pmu = kvm_vcpu_has_mediated_pmu(vcpu);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long host_only_gp_mask, host_only_fixed_mask;
 	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
 	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
 	bool intercept = !has_mediated_pmu;
@@ -4252,23 +4253,25 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 
 	vm_exit_controls_changebit(vmx, vm_exit_controls_bits, has_mediated_pmu);
 
+	host_only_gp_mask = kvm_host_pmu.cntr_mask64 & ~gp_mask;
 	kvm_for_each_gp_counter(i, gp_mask) {
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
 					  MSR_TYPE_RW, intercept);
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i, MSR_TYPE_RW,
 					  intercept || !fw_writes_is_enabled(vcpu));
 	}
-	for ( ; i < kvm_pmu_cap.num_counters_gp; i++) {
+	for_each_set_bit(i, &host_only_gp_mask, INTEL_PMC_MAX_GENERIC) {
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
 					  MSR_TYPE_RW, true);
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i,
 					  MSR_TYPE_RW, true);
 	}
 
+	host_only_fixed_mask = kvm_host_pmu.fixed_cntr_mask64 & ~fixed_mask;
 	kvm_for_each_fixed_counter(i, fixed_mask)
 		vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
 					  MSR_TYPE_RW, intercept);
-	for ( ; i < kvm_pmu_cap.num_counters_fixed; i++)
+	for_each_set_bit(i, &host_only_fixed_mask, INTEL_PMC_MAX_FIXED)
 		vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
 					  MSR_TYPE_RW, true);
 
Signature · 1 line
2.55.0
Reviewed-by on v2 9/16
#31 lore raw

Re: [PATCH v2 09/16] KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities

Quoted 265 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Intel platforms support non-contiguous fixed counters via CPUID.0AH:ECX starting with PerfMon v5, and support non-contiguous GP counters through the Architectural PerfMon Extension (CPUID leaf 23H).

struct x86_pmu_capability now exposes {,fixed_}cntr_mask64 bitmaps, which may contain sparse bits representing non-contiguous counters. Switch KVM's kvm_host_pmu and kvm_pmu_cap consumers over to the new bitmask fields.

CPUID.0AH:EAX[15:8] and CPUID.0AH:EDX[4:0] enumerate only contiguous counters. Derive these values from kvm_pmu_cap.{,fixed_}cntr_mask64 as the number of consecutive counters starting at index 0.

---
 arch/x86/kvm/cpuid.c         | 14 +++++++++++---
 arch/x86/kvm/msrs.c          | 12 ++++++------
 arch/x86/kvm/pmu.c           | 21 ++++++++++++---------
 arch/x86/kvm/svm/pmu.c       |  2 +-
 arch/x86/kvm/svm/svm.c       |  9 +++++----
 arch/x86/kvm/vmx/pmu_intel.c |  7 ++++---
 arch/x86/kvm/vmx/vmx.c       |  7 +++++--
 7 files changed, 44 insertions(+), 28 deletions(-)
arch/x86/kvm/cpuid.c +11 -3
index ddb022cb203a..106e719e7aee 100644
@@ -1528,10 +1528,18 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 		}
 
 		eax.split.version_id = kvm_pmu_cap.version;
-		eax.split.num_counters = kvm_pmu_cap.num_counters_gp;
+
+		/* Contiguous GP counters only. */
+		eax.split.num_counters =
+			find_first_zero_bit(kvm_pmu_cap.cntr_mask,
+					    KVM_MAX_NR_GP_COUNTERS);
 		eax.split.bit_width = kvm_pmu_cap.bit_width_gp;
 		eax.split.mask_length = kvm_pmu_cap.events_mask_len;
-		edx.split.num_counters_fixed = kvm_pmu_cap.num_counters_fixed;
+
+		/* Contiguous fixed counters only. */
+		edx.split.num_counters_fixed =
+			find_first_zero_bit(kvm_pmu_cap.fixed_cntr_mask,
+					    KVM_MAX_NR_FIXED_COUNTERS);
 		edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;
 
 		if (kvm_pmu_cap.version)
@@ -1896,7 +1904,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 
 		cpuid_entry_override(entry, CPUID_8000_0022_EAX);
 
-		ebx.split.num_core_pmc = kvm_pmu_cap.num_counters_gp;
+		ebx.split.num_core_pmc = hweight64(kvm_pmu_cap.cntr_mask64);
 		entry->ebx = ebx.full;
 		break;
 	}
arch/x86/kvm/msrs.c +6 -6
index 66fa7140d65d..49ab49f7c96e 100644
@@ -2610,20 +2610,20 @@ static void kvm_probe_msr_to_save(u32 msr_index)
 		break;
 	case MSR_ARCH_PERFMON_PERFCTR0 ...
 	     MSR_ARCH_PERFMON_PERFCTR0 + KVM_MAX_NR_GP_COUNTERS - 1:
-		if (msr_index - MSR_ARCH_PERFMON_PERFCTR0 >=
-		    kvm_pmu_cap.num_counters_gp)
+		if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_PERFCTR0) &
+		      kvm_pmu_cap.cntr_mask64))
 			return;
 		break;
 	case MSR_ARCH_PERFMON_EVENTSEL0 ...
 	     MSR_ARCH_PERFMON_EVENTSEL0 + KVM_MAX_NR_GP_COUNTERS - 1:
-		if (msr_index - MSR_ARCH_PERFMON_EVENTSEL0 >=
-		    kvm_pmu_cap.num_counters_gp)
+		if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_EVENTSEL0) &
+		      kvm_pmu_cap.cntr_mask64))
 			return;
 		break;
 	case MSR_ARCH_PERFMON_FIXED_CTR0 ...
 	     MSR_ARCH_PERFMON_FIXED_CTR0 + KVM_MAX_NR_FIXED_COUNTERS - 1:
-		if (msr_index - MSR_ARCH_PERFMON_FIXED_CTR0 >=
-		    kvm_pmu_cap.num_counters_fixed)
+		if (!(BIT_ULL(msr_index - MSR_ARCH_PERFMON_FIXED_CTR0) &
+		      kvm_pmu_cap.fixed_cntr_mask64))
 			return;
 		break;
 	case MSR_AMD64_PERF_CNTR_GLOBAL_CTL:
arch/x86/kvm/pmu.c +12 -9
index 7837e2e1af98..437a7bc49bf8 100644
@@ -20,7 +20,6 @@
 #include <asm/perf_event.h>
 #include <asm/cpu_device_id.h>
 #include "x86.h"
-#include "cpuid.h"
 #include "lapic.h"
 #include "pmu.h"
 
@@ -156,8 +155,8 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
 		 * there are a non-zero number of counters, but fewer than what
 		 * is architecturally required.
 		 */
-		if (!kvm_host_pmu.num_counters_gp ||
-		    WARN_ON_ONCE(kvm_host_pmu.num_counters_gp < min_nr_gp_ctrs))
+		if (!kvm_host_pmu.cntr_mask64 ||
+		    WARN_ON_ONCE(hweight64(kvm_host_pmu.cntr_mask64) < min_nr_gp_ctrs))
 			enable_pmu = false;
 		else if (is_intel && !kvm_host_pmu.version)
 			enable_pmu = false;
@@ -177,10 +176,14 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
 
 	memcpy(&kvm_pmu_cap, &kvm_host_pmu, sizeof(kvm_host_pmu));
 	kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
-	kvm_pmu_cap.num_counters_gp = min(kvm_pmu_cap.num_counters_gp,
-					  pmu_ops->MAX_NR_GP_COUNTERS);
-	kvm_pmu_cap.num_counters_fixed = min(kvm_pmu_cap.num_counters_fixed,
-					     KVM_MAX_NR_FIXED_COUNTERS);
+	kvm_pmu_cap.cntr_mask64 &=
+		GENMASK_ULL(pmu_ops->MAX_NR_GP_COUNTERS - 1, 0);
+	kvm_pmu_cap.fixed_cntr_mask64 &=
+		GENMASK_ULL(KVM_MAX_NR_FIXED_COUNTERS - 1, 0);
+
+	/* Legacy vPMU exposes at most 3 fixed counters. */
+	if (!enable_mediated_pmu)
+		kvm_pmu_cap.fixed_cntr_mask64 &= GENMASK_ULL(2, 0);
 
 	kvm_pmu_eventsel.INSTRUCTIONS_RETIRED =
 		perf_get_hw_event_config(PERF_COUNT_HW_INSTRUCTIONS);
@@ -786,8 +789,8 @@ static bool kvm_need_any_pmc_intercept(struct kvm_vcpu *vcpu)
 	 * KVM's capabilities are constrained based on KVM support, i.e. KVM's
 	 * capabilities themselves may be a subset of hardware capabilities.
 	 */
-	return kvm_gp_pmc_mask(pmu) != BIT_ULL(kvm_host_pmu.num_counters_gp) - 1 ||
-	       kvm_fixed_pmc_mask(pmu) != BIT_ULL(kvm_host_pmu.num_counters_fixed) - 1;
+	return kvm_gp_pmc_mask(pmu) != kvm_host_pmu.cntr_mask64 ||
+	       kvm_fixed_pmc_mask(pmu) != kvm_host_pmu.fixed_cntr_mask64;
 }
 
 bool kvm_need_perf_global_ctrl_intercept(struct kvm_vcpu *vcpu)
arch/x86/kvm/svm/pmu.c +1 -1
index 0943ccc1d6b8..fb5a298244ec 100644
@@ -203,7 +203,7 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
 	}
 
 	pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
-			    (BIT_ULL(kvm_pmu_cap.num_counters_gp) - 1);
+			    kvm_pmu_cap.cntr_mask64;
 
 	if (pmu->version > 1) {
 		pmu->global_ctrl_rsvd = ~pmu->pmc_exists64;
arch/x86/kvm/svm/svm.c +5 -4
index fb8442a08b63..6f3427027e99 100644
@@ -754,6 +754,7 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 	bool intercept = !kvm_vcpu_has_mediated_pmu(vcpu);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+	unsigned long host_only_gp_mask;
 	int i;
 
 	if (!enable_mediated_pmu)
@@ -772,7 +773,8 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 		svm_set_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
 					  MSR_TYPE_RW, intercept);
 
-	for ( ; i < kvm_pmu_cap.num_counters_gp; i++)
+	host_only_gp_mask = kvm_pmu_cap.cntr_mask64 & ~gp_mask;
+	kvm_for_each_gp_counter(i, host_only_gp_mask)
 		svm_enable_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
 					     MSR_TYPE_RW);
 
@@ -5563,9 +5565,8 @@ static __init void svm_set_cpu_caps(void)
 		 * access to enough counters to virtualize "core" support,
 		 * otherwise limit vPMU support to the legacy number of counters.
 		 */
-		if (kvm_pmu_cap.num_counters_gp < AMD64_NUM_COUNTERS_CORE)
-			kvm_pmu_cap.num_counters_gp = min(AMD64_NUM_COUNTERS,
-							  kvm_pmu_cap.num_counters_gp);
+		if (hweight64(kvm_pmu_cap.cntr_mask64) < AMD64_NUM_COUNTERS_CORE)
+			kvm_pmu_cap.cntr_mask64 &= GENMASK_ULL(AMD64_NUM_COUNTERS - 1, 0);
 		else
 			kvm_cpu_cap_check_and_set(X86_FEATURE_PERFCTR_CORE);
 
arch/x86/kvm/vmx/pmu_intel.c +4 -3
index 926d1c57f8bf..4df55a3e21da 100644
@@ -491,7 +491,8 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)
 	 * have a known encoding for the associated general purpose event.
 	 */
 	eventsel = perf_get_hw_event_config(fixed_pmc_perf_ids[index]);
-	WARN_ON_ONCE(!eventsel && index < kvm_pmu_cap.num_counters_fixed);
+	WARN_ON_ONCE(!eventsel &&
+		     (kvm_pmu_cap.fixed_cntr_mask64 & BIT_ULL(index)));
 	return eventsel;
 }
 
@@ -548,7 +549,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->available_event_types = ~entry->ebx & (BIT_ULL(eax.split.mask_length) - 1);
 
 	fixed_cntr_mask = BIT_ULL(edx.split.num_counters_fixed) - 1;
-	fixed_cntr_mask &= BIT_ULL(kvm_pmu_cap.num_counters_fixed) - 1;
+	fixed_cntr_mask &= kvm_pmu_cap.fixed_cntr_mask64;
 
 	/*
 	 * The number of counters comes from guest CPUID data. Clamp the value
@@ -556,7 +557,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	 */
 	nr_gp_counters = min_t(int, eax.split.num_counters, X86_PMC_IDX_MAX - 1);
 	pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
-			    (BIT_ULL(kvm_pmu_cap.num_counters_gp) - 1);
+			    kvm_pmu_cap.cntr_mask64;
 
 	entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
 	if (entry &&
arch/x86/kvm/vmx/vmx.c +5 -2
index be994adbd954..56daf5c61082 100644
@@ -4230,6 +4230,7 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 	bool has_mediated_pmu = kvm_vcpu_has_mediated_pmu(vcpu);
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct vcpu_vmx *vmx = to_vmx(vcpu);
+	unsigned long host_only_gp_mask, host_only_fixed_mask;
 	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
 	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
 	bool intercept = !has_mediated_pmu;
@@ -4252,23 +4253,25 @@ static void vmx_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
 
 	vm_exit_controls_changebit(vmx, vm_exit_controls_bits, has_mediated_pmu);
 
+	host_only_gp_mask = kvm_host_pmu.cntr_mask64 & ~gp_mask;
 	kvm_for_each_gp_counter(i, gp_mask) {
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
 					  MSR_TYPE_RW, intercept);
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i, MSR_TYPE_RW,
 					  intercept || !fw_writes_is_enabled(vcpu));
 	}
-	for ( ; i < kvm_pmu_cap.num_counters_gp; i++) {
+	for_each_set_bit(i, &host_only_gp_mask, INTEL_PMC_MAX_GENERIC) {
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PERFCTR0 + i,
 					  MSR_TYPE_RW, true);
 		vmx_set_intercept_for_msr(vcpu, MSR_IA32_PMC0 + i,
 					  MSR_TYPE_RW, true);
 	}
 
+	host_only_fixed_mask = kvm_host_pmu.fixed_cntr_mask64 & ~fixed_mask;
 	kvm_for_each_fixed_counter(i, fixed_mask)
 		vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
 					  MSR_TYPE_RW, intercept);
-	for ( ; i < kvm_pmu_cap.num_counters_fixed; i++)
+	for_each_set_bit(i, &host_only_fixed_mask, INTEL_PMC_MAX_FIXED)
 		vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_FIXED_CTR0 + i,
 					  MSR_TYPE_RW, true);
 
#32 lore raw

Re: [PATCH v2 09/16] KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities

In reply to Zide Chen
Quoted 20 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Intel platforms support non-contiguous fixed counters via CPUID.0AH:ECX starting with PerfMon v5, and support non-contiguous GP counters through the Architectural PerfMon Extension (CPUID leaf 23H).

struct x86_pmu_capability now exposes {,fixed_}cntr_mask64 bitmaps, which may contain sparse bits representing non-contiguous counters. Switch KVM's kvm_host_pmu and kvm_pmu_cap consumers over to the new bitmask fields.

CPUID.0AH:EAX[15:8] and CPUID.0AH:EDX[4:0] enumerate only contiguous counters. Derive these values from kvm_pmu_cap.{,fixed_}cntr_mask64 as the number of consecutive counters starting at index 0.

---

[ ... ]

arch/x86/kvm/svm/svm.c static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu) · line 776

Quoted 6 lines
arch/x86/kvm/svm/svm.c +1 -1
@@ -772,7 +773,8 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
                svm_set_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
                                          MSR_TYPE_RW, intercept);
 
-       for ( ; i < kvm_pmu_cap.num_counters_gp; i++)
+       host_only_gp_mask = kvm_pmu_cap.cntr_mask64 & ~gp_mask;

Should kvm_pmu_cap.cntr_mask64 be kvm_host_pmu.cntr_mask64, as it is on the VMX side?

[ ... ]

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

Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -556,7 +557,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
         */
        nr_gp_counters = min_t(int, eax.split.num_counters, X86_PMC_IDX_MAX - 1);
        pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
-                           (BIT_ULL(kvm_pmu_cap.num_counters_gp) - 1);
+                           kvm_pmu_cap.cntr_mask64;

CPUID.0AH:EAX[15:8] (eax.split.num_counters) can only enumerate contiguous GP counters 0 .. nr_gp_counters - 1. If kvm_pmu_cap.cntr_mask64 ever had a hole below nr_gp_counters (when leaf 0x23 isn't exposed to the guest to advertise a sparse mask), masking (BIT_ULL(nr_gp_counters) - 1) with kvm_pmu_cap.cntr_mask64 would silently punch a hole in the guest's GP counters even though guest CPUID.0AH:EAX[15:8] told the guest that all counters 0 .. nr_gp_counters - 1 exist. Should nr_gp_counters be clamped to find_first_zero_bit(kvm_pmu_cap.cntr_mask, KVM_MAX_NR_GP_COUNTERS) until leaf 0x23 is supported?

author
#33 lore raw

Re: [PATCH v2 09/16] KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities

Quoted 26 lines · Jim Mattson
Quoted 20 lines · Zide Chen

From: Dapeng Mi <[email protected]>

Intel platforms support non-contiguous fixed counters via CPUID.0AH:ECX starting with PerfMon v5, and support non-contiguous GP counters through the Architectural PerfMon Extension (CPUID leaf 23H).

struct x86_pmu_capability now exposes {,fixed_}cntr_mask64 bitmaps, which may contain sparse bits representing non-contiguous counters. Switch KVM's kvm_host_pmu and kvm_pmu_cap consumers over to the new bitmask fields.

CPUID.0AH:EAX[15:8] and CPUID.0AH:EDX[4:0] enumerate only contiguous counters. Derive these values from kvm_pmu_cap.{,fixed_}cntr_mask64 as the number of consecutive counters starting at index 0.

---

[ ... ]

Quoted 4 lines
arch/x86/kvm/svm/svm.c +0 -0
@@ -772,7 +773,8 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu)
                svm_set_intercept_for_msr(vcpu, MSR_F15H_PERF_CTR + 2 * i,
                                          MSR_TYPE_RW, intercept);
 
Quoted 5 lines

arch/x86/kvm/svm/svm.c static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu) · line 776

Quoted 2 lines
arch/x86/kvm/svm/svm.c +1 -1
-       for ( ; i < kvm_pmu_cap.num_counters_gp; i++)
+       host_only_gp_mask = kvm_pmu_cap.cntr_mask64 & ~gp_mask;

Should kvm_pmu_cap.cntr_mask64 be kvm_host_pmu.cntr_mask64, as it is on the VMX side?

Yes, thank you for pointing it out.

Quoted 18 lines

[ ... ]

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

Quoted 6 lines
arch/x86/kvm/vmx/pmu_intel.c +1 -1
@@ -556,7 +557,7 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
         */
        nr_gp_counters = min_t(int, eax.split.num_counters, X86_PMC_IDX_MAX - 1);
        pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
-                           (BIT_ULL(kvm_pmu_cap.num_counters_gp) - 1);
+                           kvm_pmu_cap.cntr_mask64;

CPUID.0AH:EAX[15:8] (eax.split.num_counters) can only enumerate contiguous GP counters 0 .. nr_gp_counters - 1. If kvm_pmu_cap.cntr_mask64 ever had a hole below nr_gp_counters (when leaf 0x23 isn't exposed to the guest to advertise a sparse mask), masking (BIT_ULL(nr_gp_counters) - 1) with kvm_pmu_cap.cntr_mask64 would silently punch a hole in the guest's GP counters even though guest CPUID.0AH:EAX[15:8] told the guest that all counters 0 .. nr_gp_counters - 1 exist. Should nr_gp_counters be clamped to find_first_zero_bit(kvm_pmu_cap.cntr_mask, KVM_MAX_NR_GP_COUNTERS) until leaf 0x23 is supported?

Regardless of whether or not CPUID 0x23 is supported in KVM, I think "if kvm_pmu_cap.cntr_mask64 ever had a hole below nr_gp_counters" would only happen in the case of an invalid guest configuration.

Anyway, it is good to clamp nr_gp_counters to remove any theoretical hole, keeping it semantically correct. For example:

/*
* CPUID.0AH:EAX[15:8] can only enumerate contiguous counters. Clamp the
* value to the number of contiguous GP counters supported by KVM. */
nr_gp_counters = min_t(int, eax.split.num_counters,

                       find_first_zero_bit(kvm_pmu_cap.cntr_mask,
                                           KVM_MAX_NR_GP_COUNTERS));

pmu->pmc_exists64 = BIT_ULL(nr_gp_counters) - 1;

Thanks for the review!

author
#34 lore raw

PATCH v2 10/16 perf/x86: Remove num_counters_{gp,fixed} from x86_pmu_capability

In reply to Zide Chen

From: Dapeng Mi <[email protected]>

Now that KVM has switched to bitmap-based PMU capabilities, num_counters_{gp,fixed} can be removed from x86_pmu_capability.

---
 arch/x86/events/core.c            | 2 --
 arch/x86/include/asm/perf_event.h | 2 --
 2 files changed, 4 deletions(-)
arch/x86/events/core.c +0 -2
index 3fb95090cd8f..9b6df8bc9059 100644
@@ -3132,8 +3132,6 @@ void perf_get_x86_pmu_capability(struct x86_pmu_capability *cap)
 	 * base PMU holds the correct number of counters for P-cores.
 	 */
 	cap->version		= x86_pmu.version;
-	cap->num_counters_gp	= x86_pmu_num_counters(NULL);
-	cap->num_counters_fixed	= x86_pmu_num_counters_fixed(NULL);
 	cap->cntr_mask64	= x86_pmu.cntr_mask64;
 	cap->fixed_cntr_mask64	= x86_pmu.fixed_cntr_mask64;
 	cap->bit_width_gp	= cap->cntr_mask64 ? x86_pmu.cntval_bits : 0;
arch/x86/include/asm/perf_event.h +0 -2
index 190fda412a61..17b0bc7dfce7 100644
@@ -300,8 +300,6 @@ union cpuid_0x80000022_ebx {
 
 struct x86_pmu_capability {
 	int		version;
-	int		num_counters_gp;
-	int		num_counters_fixed;
 	union {
 		u64	cntr_mask64;
 		DECLARE_BITMAP(cntr_mask, X86_PMC_IDX_MAX);
Signature · 1 line
2.55.0
author
#36 lore raw

PATCH v2 11/16 KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

In reply to Zide Chen

Intel PerfMon v4 introduces IA32_PERF_GLOBAL_STATUS_SET (0x391) to allow software to set individual bits in the global status MSR. Reads of IA32_PERF_GLOBAL_STATUS_SET always return zero.

IA32_PERF_GLOBAL_INUSE (0x392) is also introduced in v4, to track which counters and the PMI are currently claimed by other agents, allowing independent software agents to check counter availability without a shared scheduler arbitrating between them.

IA32_PERF_GLOBAL_INUSE is an read-only MSR, and any write attempt results in a #GP.

Neither MSR is part of the VM state, so they don't need to be advertised to userspace, nor saved and restored during live migration.

---
v2:
- Change intel_pmu_get_global_inuse() to return u64, to match the surrounding code style.
- Add the missing vmcs02 updates for these two MSRs.
- Change "> 3" to ">= 4" to make the "v4-gated" more obvious and match the existing code style.

---
 arch/x86/include/asm/msr-index.h |  4 ++++
 arch/x86/kvm/pmu.c               |  9 ++++++++
 arch/x86/kvm/vmx/nested.c        |  2 ++
 arch/x86/kvm/vmx/pmu_intel.c     | 37 ++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c           |  4 ++++
 5 files changed, 56 insertions(+)
arch/x86/include/asm/msr-index.h +4 -0
index 11b99d237e05..0b093cf41edf 100644
@@ -1240,6 +1240,10 @@
 #define MSR_CORE_PERF_GLOBAL_CTRL	0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL	0x00000390
 #define MSR_CORE_PERF_GLOBAL_STATUS_SET	0x00000391
+#define MSR_CORE_PERF_GLOBAL_INUSE	0x00000392
+
+/* Intel IA32_PERF_GLOBAL_INUSE MSR */
+#define PERF_GLOBAL_INUSE_PMI_INUSE	BIT_ULL(63)
 
 #define MSR_PERF_METRICS		0x00000329
 
arch/x86/kvm/pmu.c +9 -0
index 437a7bc49bf8..7c05cf5157bf 100644
@@ -832,6 +832,8 @@ bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 	case MSR_CORE_PERF_GLOBAL_CTRL:
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
 		return kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu));
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		return vcpu_to_pmu(vcpu)->version >= 4;
 	default:
 		break;
 	}
@@ -865,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
 		msr_info->data = 0;
 		break;
 	default:
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (!msr_info->host_initiated)
 			pmu->global_status &= ~data;
 		break;
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		if (data & pmu->global_status_rsvd)
+			return 1;
+		if (!msr_info->host_initiated)
+			pmu->global_status |= data;
+		break;
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
 		if (!msr_info->host_initiated)
 			pmu->global_status |= data & ~pmu->global_status_rsvd;
arch/x86/kvm/vmx/nested.c +2 -0
index 0cff369982ae..ae7dd3636e70 100644
@@ -719,6 +719,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
 	nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);
 	nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_STATUS);
 	nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_OVF_CTRL);
+	nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_STATUS_SET);
+	nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_INUSE);
 }
 
 /*
arch/x86/kvm/vmx/pmu_intel.c +37 -0
index 4df55a3e21da..3070fba2687f 100644
@@ -194,6 +194,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_CORE_PERF_GLOBAL_INUSE:
+		return pmu->version >= 4;
 	case MSR_IA32_PEBS_ENABLE:
 		ret = vcpu_get_perf_capabilities(vcpu) & PERF_CAP_PEBS_FORMAT;
 		break;
@@ -341,6 +343,38 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)
+{
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+	bool pmi_inuse = false;
+	u64 eventsel, data = 0;
+	u32 fixed_ctrl;
+	int i;
+
+	kvm_for_each_gp_counter(i, gp_mask) {
+		eventsel = pmu->gp_counters[i].eventsel;
+
+		if (eventsel & ARCH_PERFMON_EVENTSEL_EVENT)
+			data |= BIT_ULL(i);
+		pmi_inuse |= eventsel & ARCH_PERFMON_EVENTSEL_INT;
+	}
+	kvm_for_each_fixed_counter(i, fixed_mask) {
+		fixed_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i);
+
+		if (fixed_ctrl & (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER))
+			data |= BIT_ULL(KVM_FIXED_PMC_BASE_IDX + i);
+		pmi_inuse |= fixed_ctrl & INTEL_FIXED_0_ENABLE_PMI;
+	}
+	pmi_inuse |= pmu->pebs_enable;
+
+	if (pmi_inuse)
+		data |= PERF_GLOBAL_INUSE_PMI_INUSE;
+
+	return data;
+}
+
 static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -351,6 +385,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_CORE_PERF_GLOBAL_INUSE:
+		msr_info->data = intel_pmu_get_global_inuse(vcpu);
+		break;
 	case MSR_IA32_PEBS_ENABLE:
 		msr_info->data = pmu->pebs_enable;
 		break;
arch/x86/kvm/vmx/vmx.c +4 -0
index 56daf5c61082..2dabb7e64956 100644
@@ -4282,6 +4282,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);
+	vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_STATUS_SET,
+				  MSR_TYPE_RW, intercept || pmu->version < 4);
+	vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_INUSE,
+				  MSR_TYPE_RW, intercept || pmu->version < 4);
 }
 
 static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
Signature · 1 line
2.55.0
#37 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Medium] Incorrect IN_USE bit calculation for fixed counters in IA32_PERF_GLOBAL_INUSE omits the…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Incorrect IN_USE bit calculation for fixed counters in IA32_PERF_GLOBAL_INUSE omits the INTEL_FIXED_0_ENABLE_PMI condition.
- [Medium] Emulated writes to MSR_CORE_PERF_GLOBAL_STATUS_SET fail to trigger a PMI when the corresponding global_ctrl bit is set. --

commit a29f967a55c3b6e45cb682091b4b9a33c0b976dc
Author: Zide Chen <[email protected]>

KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Intel PerfMon v4 introduces IA32_PERF_GLOBAL_STATUS_SET (0x391) and IA32_PERF_GLOBAL_INUSE (0x392). This patch adds emulation for both MSRs in KVM, but there appear to be a few issues with the implementation.

Quoted 4 lines
arch/x86/kvm/pmu.c +0 -0
index 437a7bc49bf84..7c05cf5157bfc 100644

[ ... ]

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 942

Quoted 10 lines
arch/x86/kvm/pmu.c +6 -0
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (!msr_info->host_initiated)
 			pmu->global_status &= ~data;
 		break;
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		if (data & pmu->global_status_rsvd)
+			return 1;
+		if (!msr_info->host_initiated)
+			pmu->global_status |= data;
+		break;

[Severity: Medium]
Will emulated guest writes to MSR_CORE_PERF_GLOBAL_STATUS_SET fail to trigger a PMI when the corresponding global_ctrl bit is set?

This handler correctly updates the global status, but it appears to omit the required secondary side-effect of generating a performance monitoring interrupt.

If a guest writes a 1 to a bit in MSR_CORE_PERF_GLOBAL_STATUS_SET while the corresponding bit in IA32_PERF_GLOBAL_CTRL is enabled, it seems the interrupt will silently fail to generate because the code doesn't check global_ctrl or set KVM_REQ_PMI.

Quoted 3 lines
arch/x86/kvm/pmu.c +0 -0
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
 		if (!msr_info->host_initiated)
 			pmu->global_status |= data & ~pmu->global_status_rsvd;

[ ... ]

Quoted 4 lines
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index 4df55a3e21da9..3070fba2687f4 100644

[ ... ]

Quoted 16 lines
arch/x86/kvm/vmx/pmu_intel.c +12 -0
@@ -341,6 +343,38 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)
+{
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+	bool pmi_inuse = false;
+	u64 eventsel, data = 0;
+	u32 fixed_ctrl;
+	int i;
+
+	kvm_for_each_gp_counter(i, gp_mask) {
+		eventsel = pmu->gp_counters[i].eventsel;

arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, · line 367

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +10 -0
+
+		if (eventsel & ARCH_PERFMON_EVENTSEL_EVENT)
+			data |= BIT_ULL(i);
+		pmi_inuse |= eventsel & ARCH_PERFMON_EVENTSEL_INT;
+	}
+	kvm_for_each_fixed_counter(i, fixed_mask) {
+		fixed_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i);
+
+		if (fixed_ctrl & (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER))
+			data |= BIT_ULL(KVM_FIXED_PMC_BASE_IDX + i);

[Severity: Medium]
Does this code improperly omit INTEL_FIXED_0_ENABLE_PMI when calculating the IN_USE bit for fixed counters?

If a fixed counter is configured only for PMI (without OS or User bits), this code seems to falsely report it as unused, which violates the Intel SDM specification for FCn_InUse.

Should INTEL_FIXED_0_ENABLE_PMI be included in this condition to ensure the availability check remains accurate?

Quoted 9 lines
arch/x86/kvm/vmx/pmu_intel.c +9 -0
+		pmi_inuse |= fixed_ctrl & INTEL_FIXED_0_ENABLE_PMI;
+	}
+	pmi_inuse |= pmu->pebs_enable;
+
+	if (pmi_inuse)
+		data |= PERF_GLOBAL_INUSE_PMI_INUSE;
+
+	return data;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
author
#38 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 34 lines · On 8/27/2026 6:05 PM, [email protected] wrote:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Medium] Incorrect IN_USE bit calculation for fixed counters in IA32_PERF_GLOBAL_INUSE omits the INTEL_FIXED_0_ENABLE_PMI condition.
- [Medium] Emulated writes to MSR_CORE_PERF_GLOBAL_STATUS_SET fail to trigger a PMI when the corresponding global_ctrl bit is set. --

commit a29f967a55c3b6e45cb682091b4b9a33c0b976dc
Author: Zide Chen <[email protected]>

KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Intel PerfMon v4 introduces IA32_PERF_GLOBAL_STATUS_SET (0x391) and IA32_PERF_GLOBAL_INUSE (0x392). This patch adds emulation for both MSRs in KVM, but there appear to be a few issues with the implementation.

Quoted 4 lines
arch/x86/kvm/pmu.c +0 -0
index 437a7bc49bf84..7c05cf5157bfc 100644

[ ... ]

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 942

Quoted 10 lines
arch/x86/kvm/pmu.c +6 -0
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (!msr_info->host_initiated)
 			pmu->global_status &= ~data;
 		break;
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		if (data & pmu->global_status_rsvd)
+			return 1;
+		if (!msr_info->host_initiated)
+			pmu->global_status |= data;
+		break;

[Severity: Medium]
Will emulated guest writes to MSR_CORE_PERF_GLOBAL_STATUS_SET fail to trigger a PMI when the corresponding global_ctrl bit is set?

Quoted 7 lines

This handler correctly updates the global status, but it appears to omit the required secondary side-effect of generating a performance monitoring interrupt.

If a guest writes a 1 to a bit in MSR_CORE_PERF_GLOBAL_STATUS_SET while the corresponding bit in IA32_PERF_GLOBAL_CTRL is enabled, it seems the interrupt will silently fail to generate because the code doesn't check global_ctrl or set KVM_REQ_PMI.

According to the Intel SDM, IA32_PERF_GLOBAL_STATUS_SET only sets the corresponding bits in IA32_PERF_GLOBAL_STATUS, but not generate a PMI.

Generating a PMI / KVM_REQ_PMI on writes to this MSR would deviate from hardware behavior and cause spurious PMIs in the guest, e.g., during live migration state restoration, or during KVM pmu context switch.

Quoted 41 lines
Quoted 4 lines
arch/x86/kvm/pmu.c +0 -0
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
 		if (!msr_info->host_initiated)
 			pmu->global_status |= data & ~pmu->global_status_rsvd;

[ ... ]

Quoted 4 lines
arch/x86/kvm/vmx/pmu_intel.c +0 -0
index 4df55a3e21da9..3070fba2687f4 100644

[ ... ]

Quoted 16 lines
arch/x86/kvm/vmx/pmu_intel.c +12 -0
@@ -341,6 +343,38 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)
+{
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+	bool pmi_inuse = false;
+	u64 eventsel, data = 0;
+	u32 fixed_ctrl;
+	int i;
+
+	kvm_for_each_gp_counter(i, gp_mask) {
+		eventsel = pmu->gp_counters[i].eventsel;

arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, · line 367

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +10 -0
+
+		if (eventsel & ARCH_PERFMON_EVENTSEL_EVENT)
+			data |= BIT_ULL(i);
+		pmi_inuse |= eventsel & ARCH_PERFMON_EVENTSEL_INT;
+	}
+	kvm_for_each_fixed_counter(i, fixed_mask) {
+		fixed_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i);
+
+		if (fixed_ctrl & (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER))
+			data |= BIT_ULL(KVM_FIXED_PMC_BASE_IDX + i);

[Severity: Medium]
Does this code improperly omit INTEL_FIXED_0_ENABLE_PMI when calculating the IN_USE bit for fixed counters?

Quoted 6 lines

If a fixed counter is configured only for PMI (without OS or User bits), this code seems to falsely report it as unused, which violates the Intel SDM specification for FCn_InUse.

Should INTEL_FIXED_0_ENABLE_PMI be included in this condition to ensure the availability check remains accurate?

False positive. The implementation strictly follows the SDM:

IA32_PERF_GLOBAL_INUSE.FC0_InUse[bit 32]: This bit reflects the logical state of (IA32_FIXED_CTR_CTRL[1:0] != 0).

Also I verified that the behavior is same between the host and the guest in this regard.

Note that in practice, reading this MSR on the host may be inconsistent. The Linux perf driver clear the EN bit but not the whole EvtSel MSRs after a perf event finishes, so the corresponding bits in INUSE may remain set. In contrast, kvm_pmu_put_guest_pmcs() explicitly clears these MSRs, which means the in-use bits will be cleared on physical CPUs running a vCPU.

Quoted 11 lines
Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +9 -0
+		pmi_inuse |= fixed_ctrl & INTEL_FIXED_0_ENABLE_PMI;
+	}
+	pmi_inuse |= pmu->pebs_enable;
+
+	if (pmi_inuse)
+		data |= PERF_GLOBAL_INUSE_PMI_INUSE;
+
+	return data;
+}
#39 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

In reply to Zide Chen
Quoted 116 lines · Zide Chen

Intel PerfMon v4 introduces IA32_PERF_GLOBAL_STATUS_SET (0x391) to allow software to set individual bits in the global status MSR. Reads of IA32_PERF_GLOBAL_STATUS_SET always return zero.

IA32_PERF_GLOBAL_INUSE (0x392) is also introduced in v4, to track which counters and the PMI are currently claimed by other agents, allowing independent software agents to check counter availability without a shared scheduler arbitrating between them.

IA32_PERF_GLOBAL_INUSE is an read-only MSR, and any write attempt results in a #GP.

Neither MSR is part of the VM state, so they don't need to be advertised to userspace, nor saved and restored during live migration.

---
v2:
- Change intel_pmu_get_global_inuse() to return u64, to match the surrounding code style.
- Add the missing vmcs02 updates for these two MSRs.
- Change "> 3" to ">= 4" to make the "v4-gated" more obvious and match the existing code style.

---
 arch/x86/include/asm/msr-index.h |  4 ++++
 arch/x86/kvm/pmu.c               |  9 ++++++++
 arch/x86/kvm/vmx/nested.c        |  2 ++
 arch/x86/kvm/vmx/pmu_intel.c     | 37 ++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c           |  4 ++++
 5 files changed, 56 insertions(+)
arch/x86/include/asm/msr-index.h +4 -0
index 11b99d237e05..0b093cf41edf 100644
@@ -1240,6 +1240,10 @@
 #define MSR_CORE_PERF_GLOBAL_CTRL	0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL	0x00000390
 #define MSR_CORE_PERF_GLOBAL_STATUS_SET	0x00000391
+#define MSR_CORE_PERF_GLOBAL_INUSE	0x00000392
+
+/* Intel IA32_PERF_GLOBAL_INUSE MSR */
+#define PERF_GLOBAL_INUSE_PMI_INUSE	BIT_ULL(63)
 
 #define MSR_PERF_METRICS		0x00000329
 
arch/x86/kvm/pmu.c +9 -0
index 437a7bc49bf8..7c05cf5157bf 100644
@@ -832,6 +832,8 @@ bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 	case MSR_CORE_PERF_GLOBAL_CTRL:
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
 		return kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu));
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		return vcpu_to_pmu(vcpu)->version >= 4;
 	default:
 		break;
 	}
@@ -865,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
 		msr_info->data = 0;
 		break;
 	default:
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (!msr_info->host_initiated)
 			pmu->global_status &= ~data;
 		break;
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		if (data & pmu->global_status_rsvd)
+			return 1;
+		if (!msr_info->host_initiated)
+			pmu->global_status |= data;
+		break;
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
 		if (!msr_info->host_initiated)
 			pmu->global_status |= data & ~pmu->global_status_rsvd;
arch/x86/kvm/vmx/nested.c +2 -0
index 0cff369982ae..ae7dd3636e70 100644
@@ -719,6 +719,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
 	nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);
 	nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_STATUS);
 	nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_OVF_CTRL);
+	nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_STATUS_SET);
+	nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_INUSE);
 }
 
 /*
arch/x86/kvm/vmx/pmu_intel.c +6 -0
index 4df55a3e21da..3070fba2687f 100644
@@ -194,6 +194,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_CORE_PERF_GLOBAL_INUSE:
+		return pmu->version >= 4;
 	case MSR_IA32_PEBS_ENABLE:
 		ret = vcpu_get_perf_capabilities(vcpu) & PERF_CAP_PEBS_FORMAT;
 		break;
@@ -341,6 +343,38 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)
+{
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);

arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, · line 359

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +10 -0
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+	bool pmi_inuse = false;
+	u64 eventsel, data = 0;
+	u32 fixed_ctrl;
+	int i;
+
+	kvm_for_each_gp_counter(i, gp_mask) {
+		eventsel = pmu->gp_counters[i].eventsel;
+
+		if (eventsel & ARCH_PERFMON_EVENTSEL_EVENT)

Why to check ARCH_PERFMON_EVENTSEL_EVENT instead of ARCH_PERFMON_EVENTSEL_ENABLE here? Suppose only ARCH_PERFMON_EVENTSEL_ENABLE is set, then the counter is in use.

Thanks.

Quoted 46 lines
arch/x86/kvm/vmx/pmu_intel.c +21 -0
+			data |= BIT_ULL(i);
+		pmi_inuse |= eventsel & ARCH_PERFMON_EVENTSEL_INT;
+	}
+	kvm_for_each_fixed_counter(i, fixed_mask) {
+		fixed_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i);
+
+		if (fixed_ctrl & (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER))
+			data |= BIT_ULL(KVM_FIXED_PMC_BASE_IDX + i);
+		pmi_inuse |= fixed_ctrl & INTEL_FIXED_0_ENABLE_PMI;
+	}
+	pmi_inuse |= pmu->pebs_enable;
+
+	if (pmi_inuse)
+		data |= PERF_GLOBAL_INUSE_PMI_INUSE;
+
+	return data;
+}
+
 static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -351,6 +385,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_CORE_PERF_GLOBAL_INUSE:
+		msr_info->data = intel_pmu_get_global_inuse(vcpu);
+		break;
 	case MSR_IA32_PEBS_ENABLE:
 		msr_info->data = pmu->pebs_enable;
 		break;
arch/x86/kvm/vmx/vmx.c +4 -0
index 56daf5c61082..2dabb7e64956 100644
@@ -4282,6 +4282,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);
+	vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_STATUS_SET,
+				  MSR_TYPE_RW, intercept || pmu->version < 4);
+	vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_INUSE,
+				  MSR_TYPE_RW, intercept || pmu->version < 4);
 }
 
 static void vmx_recalc_msr_intercepts(struct kvm_vcpu *vcpu)
author
#40 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 124 lines · Dapeng
Quoted 122 lines · Zide Chen

Intel PerfMon v4 introduces IA32_PERF_GLOBAL_STATUS_SET (0x391) to allow software to set individual bits in the global status MSR. Reads of IA32_PERF_GLOBAL_STATUS_SET always return zero.

IA32_PERF_GLOBAL_INUSE (0x392) is also introduced in v4, to track which counters and the PMI are currently claimed by other agents, allowing independent software agents to check counter availability without a shared scheduler arbitrating between them.

IA32_PERF_GLOBAL_INUSE is an read-only MSR, and any write attempt results in a #GP.

Neither MSR is part of the VM state, so they don't need to be advertised to userspace, nor saved and restored during live migration.

---
v2:
- Change intel_pmu_get_global_inuse() to return u64, to match the surrounding code style.
- Add the missing vmcs02 updates for these two MSRs.
- Change "> 3" to ">= 4" to make the "v4-gated" more obvious and match the existing code style.

---
 arch/x86/include/asm/msr-index.h |  4 ++++
 arch/x86/kvm/pmu.c               |  9 ++++++++
 arch/x86/kvm/vmx/nested.c        |  2 ++
 arch/x86/kvm/vmx/pmu_intel.c     | 37 ++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c           |  4 ++++
 5 files changed, 56 insertions(+)
arch/x86/include/asm/msr-index.h +4 -0
index 11b99d237e05..0b093cf41edf 100644
@@ -1240,6 +1240,10 @@
 #define MSR_CORE_PERF_GLOBAL_CTRL	0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL	0x00000390
 #define MSR_CORE_PERF_GLOBAL_STATUS_SET	0x00000391
+#define MSR_CORE_PERF_GLOBAL_INUSE	0x00000392
+
+/* Intel IA32_PERF_GLOBAL_INUSE MSR */
+#define PERF_GLOBAL_INUSE_PMI_INUSE	BIT_ULL(63)
 
 #define MSR_PERF_METRICS		0x00000329
 
arch/x86/kvm/pmu.c +9 -0
index 437a7bc49bf8..7c05cf5157bf 100644
@@ -832,6 +832,8 @@ bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 	case MSR_CORE_PERF_GLOBAL_CTRL:
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
 		return kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu));
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		return vcpu_to_pmu(vcpu)->version >= 4;
 	default:
 		break;
 	}
@@ -865,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
 		msr_info->data = 0;
 		break;
 	default:
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (!msr_info->host_initiated)
 			pmu->global_status &= ~data;
 		break;
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		if (data & pmu->global_status_rsvd)
+			return 1;
+		if (!msr_info->host_initiated)
+			pmu->global_status |= data;
+		break;
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
 		if (!msr_info->host_initiated)
 			pmu->global_status |= data & ~pmu->global_status_rsvd;
arch/x86/kvm/vmx/nested.c +2 -0
index 0cff369982ae..ae7dd3636e70 100644
@@ -719,6 +719,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
 	nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);
 	nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_STATUS);
 	nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_OVF_CTRL);
+	nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_STATUS_SET);
+	nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_INUSE);
 }
 
 /*
arch/x86/kvm/vmx/pmu_intel.c +12 -0
index 4df55a3e21da..3070fba2687f 100644
@@ -194,6 +194,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_CORE_PERF_GLOBAL_INUSE:
+		return pmu->version >= 4;
 	case MSR_IA32_PEBS_ENABLE:
 		ret = vcpu_get_perf_capabilities(vcpu) & PERF_CAP_PEBS_FORMAT;
 		break;
@@ -341,6 +343,38 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)
+{
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+	bool pmi_inuse = false;
+	u64 eventsel, data = 0;
+	u32 fixed_ctrl;
+	int i;
+
Quoted 8 lines

arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, · line 359

Quoted 4 lines
arch/x86/kvm/vmx/pmu_intel.c +4 -0
+	kvm_for_each_gp_counter(i, gp_mask) {
+		eventsel = pmu->gp_counters[i].eventsel;
+
+		if (eventsel & ARCH_PERFMON_EVENTSEL_EVENT)

Why to check ARCH_PERFMON_EVENTSEL_EVENT instead of ARCH_PERFMON_EVENTSEL_ENABLE here? Suppose only ARCH_PERFMON_EVENTSEL_ENABLE is set, then the counter is in use.

This implementation strictly follows the SDM, which checks the event bits, not the enable bit. This is also verified on bare metal.

"IA32_PERF_GLOBAL_INUSE.PERFEVTSEL0_InUse[bit 0]: This bit reflects the logical state of (IA32_PERFEVTSEL0[7:0] != 0)."

That's why, in the reply above to Sashiko, I noted that on bare metal without a vCPU scheduled in, after a perf event is closed, the counter could still show as in use. perf/x86 clears the enable bit while keeps the event bits set.

#41 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 132 lines · Zide
Quoted 122 lines · Dapeng
Quoted 121 lines · Zide Chen

Intel PerfMon v4 introduces IA32_PERF_GLOBAL_STATUS_SET (0x391) to allow software to set individual bits in the global status MSR. Reads of IA32_PERF_GLOBAL_STATUS_SET always return zero.

IA32_PERF_GLOBAL_INUSE (0x392) is also introduced in v4, to track which counters and the PMI are currently claimed by other agents, allowing independent software agents to check counter availability without a shared scheduler arbitrating between them.

IA32_PERF_GLOBAL_INUSE is an read-only MSR, and any write attempt results in a #GP.

Neither MSR is part of the VM state, so they don't need to be advertised to userspace, nor saved and restored during live migration.

---
v2:
- Change intel_pmu_get_global_inuse() to return u64, to match the surrounding code style.
- Add the missing vmcs02 updates for these two MSRs.
- Change "> 3" to ">= 4" to make the "v4-gated" more obvious and match the existing code style.

---
 arch/x86/include/asm/msr-index.h |  4 ++++
 arch/x86/kvm/pmu.c               |  9 ++++++++
 arch/x86/kvm/vmx/nested.c        |  2 ++
 arch/x86/kvm/vmx/pmu_intel.c     | 37 ++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c           |  4 ++++
 5 files changed, 56 insertions(+)
arch/x86/include/asm/msr-index.h +4 -0
index 11b99d237e05..0b093cf41edf 100644
@@ -1240,6 +1240,10 @@
 #define MSR_CORE_PERF_GLOBAL_CTRL	0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL	0x00000390
 #define MSR_CORE_PERF_GLOBAL_STATUS_SET	0x00000391
+#define MSR_CORE_PERF_GLOBAL_INUSE	0x00000392
+
+/* Intel IA32_PERF_GLOBAL_INUSE MSR */
+#define PERF_GLOBAL_INUSE_PMI_INUSE	BIT_ULL(63)
 
 #define MSR_PERF_METRICS		0x00000329
 
arch/x86/kvm/pmu.c +9 -0
index 437a7bc49bf8..7c05cf5157bf 100644
@@ -832,6 +832,8 @@ bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
 	case MSR_CORE_PERF_GLOBAL_CTRL:
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
 		return kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu));
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		return vcpu_to_pmu(vcpu)->version >= 4;
 	default:
 		break;
 	}
@@ -865,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
 		msr_info->data = 0;
 		break;
 	default:
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (!msr_info->host_initiated)
 			pmu->global_status &= ~data;
 		break;
+	case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+		if (data & pmu->global_status_rsvd)
+			return 1;
+		if (!msr_info->host_initiated)
+			pmu->global_status |= data;
+		break;
 	case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
 		if (!msr_info->host_initiated)
 			pmu->global_status |= data & ~pmu->global_status_rsvd;
arch/x86/kvm/vmx/nested.c +2 -0
index 0cff369982ae..ae7dd3636e70 100644
@@ -719,6 +719,8 @@ static void nested_vmx_merge_pmu_msr_bitmaps(struct kvm_vcpu *vcpu,
 	nested_vmx_merge_msr_bitmaps_rw(MSR_CORE_PERF_GLOBAL_CTRL);
 	nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_STATUS);
 	nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_OVF_CTRL);
+	nested_vmx_merge_msr_bitmaps_write(MSR_CORE_PERF_GLOBAL_STATUS_SET);
+	nested_vmx_merge_msr_bitmaps_read(MSR_CORE_PERF_GLOBAL_INUSE);
 }
 
 /*
arch/x86/kvm/vmx/pmu_intel.c +11 -0
index 4df55a3e21da..3070fba2687f 100644
@@ -194,6 +194,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_CORE_PERF_GLOBAL_INUSE:
+		return pmu->version >= 4;
 	case MSR_IA32_PEBS_ENABLE:
 		ret = vcpu_get_perf_capabilities(vcpu) & PERF_CAP_PEBS_FORMAT;
 		break;
@@ -341,6 +343,38 @@ static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
 	return true;
 }
 
+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)
+{
+	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+	bool pmi_inuse = false;
+	u64 eventsel, data = 0;
+	u32 fixed_ctrl;
+	int i;
Quoted 8 lines

arch/x86/kvm/vmx/pmu_intel.c static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, · line 359

Quoted 5 lines
arch/x86/kvm/vmx/pmu_intel.c +5 -0
+
+	kvm_for_each_gp_counter(i, gp_mask) {
+		eventsel = pmu->gp_counters[i].eventsel;
+
+		if (eventsel & ARCH_PERFMON_EVENTSEL_EVENT)

Why to check ARCH_PERFMON_EVENTSEL_EVENT instead of ARCH_PERFMON_EVENTSEL_ENABLE here? Suppose only ARCH_PERFMON_EVENTSEL_ENABLE is set, then the counter is in use.

Quoted 5 lines

This implementation strictly follows the SDM, which checks the event bits, not the enable bit. This is also verified on bare metal.

"IA32_PERF_GLOBAL_INUSE.PERFEVTSEL0_InUse[bit 0]: This bit reflects the logical state of (IA32_PERFEVTSEL0[7:0] != 0)."

Ok, thanks for double checking.

Quoted 5 lines

That's why, in the reply above to Sashiko, I noted that on bare metal without a vCPU scheduled in, after a perf event is closed, the counter could still show as in use. perf/x86 clears the enable bit while keeps the event bits set.

That's why I feel confusing. Currently perf/x86 doesn't clear the Evensel field and only clears the enable bit, so IN_USE bit would be set forever once an event ever used the counter even the GP counter is not enabled right now.

It's deserved to add a comment here, I believe some persons have same question with me.

Anyway, let's keep consistent with SDM here. Thanks.

#42 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

In reply to Zide Chen
Quoted 54 lines · Zide Chen

Intel PerfMon v4 introduces IA32_PERF_GLOBAL_STATUS_SET (0x391) to allow software to set individual bits in the global status MSR. Reads of IA32_PERF_GLOBAL_STATUS_SET always return zero.

IA32_PERF_GLOBAL_INUSE (0x392) is also introduced in v4, to track which counters and the PMI are currently claimed by other agents, allowing independent software agents to check counter availability without a shared scheduler arbitrating between them.

IA32_PERF_GLOBAL_INUSE is an read-only MSR, and any write attempt results in a #GP.

Neither MSR is part of the VM state, so they don't need to be advertised to userspace, nor saved and restored during live migration.

---
v2:
- Change intel_pmu_get_global_inuse() to return u64, to match the surrounding code style.
- Add the missing vmcs02 updates for these two MSRs.
- Change "> 3" to ">= 4" to make the "v4-gated" more obvious and match the existing code style.

---
 arch/x86/include/asm/msr-index.h |  4 ++++
 arch/x86/kvm/pmu.c               |  9 ++++++++
 arch/x86/kvm/vmx/nested.c        |  2 ++
 arch/x86/kvm/vmx/pmu_intel.c     | 37 ++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c           |  4 ++++
 5 files changed, 56 insertions(+)
arch/x86/include/asm/msr-index.h +4 -0
index 11b99d237e05..0b093cf41edf 100644
@@ -1240,6 +1240,10 @@
 #define MSR_CORE_PERF_GLOBAL_CTRL      0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL  0x00000390
 #define MSR_CORE_PERF_GLOBAL_STATUS_SET        0x00000391
+#define MSR_CORE_PERF_GLOBAL_INUSE     0x00000392
+
+/* Intel IA32_PERF_GLOBAL_INUSE MSR */
+#define PERF_GLOBAL_INUSE_PMI_INUSE    BIT_ULL(63)
 
 #define MSR_PERF_METRICS               0x00000329
 
arch/x86/kvm/pmu.c +0 -0
index 437a7bc49bf8..7c05cf5157bf 100644

arch/x86/kvm/pmu.c bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) · line 836

Quoted 5 lines
arch/x86/kvm/pmu.c +2 -0
@@ -832,6 +832,8 @@ bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
        case MSR_CORE_PERF_GLOBAL_CTRL:
        case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
                return kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu));
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+               return vcpu_to_pmu(vcpu)->version >= 4;

MSR_CORE_PERF_GLOBAL_STATUS_SET should be handled in pmu_intel.c? It is an Intel-specific MSR (note that commit 79a71cc2568f ("KVM: x86/pmu: Move Intel specific MSRs to pmu_intel.c") moved all of MSR_CORE_PERF_GLOBAL_* into pmu_intel.c).

Moreover, checking "vcpu_to_pmu(vcpu)->version >= 4" here without checking the vendor will expose MSR_CORE_PERF_GLOBAL_STATUS_SET on AMD when AMD's pmu->version reaches 4.

Quoted 9 lines
arch/x86/kvm/pmu.c +1 -0
@@ -865,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
        case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
                msr_info->data = 0;
                break;
        default:

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 942

Quoted 9 lines
arch/x86/kvm/pmu.c +6 -0
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                if (!msr_info->host_initiated)
                        pmu->global_status &= ~data;
                break;
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+               if (data & pmu->global_status_rsvd)
+                       return 1;
+               if (!msr_info->host_initiated)
+                       pmu->global_status |= data;
+               break;

According to Intel SDM Vol. 4, Table 2-2 ("IA-32 Architectural MSRs"), IA32_PERF_GLOBAL_STATUS.CondChgd[bit 63] cannot be set by writing to IA32_PERF_GLOBAL_STATUS_SET, because IA32_PERF_GLOBAL_STATUS_SET[bit 63] is reserved. In other words, it is not possible to save/restore IA32_PERF_GLOBAL_STATUS when bit 63 is set.

Because bit 63 is clear in pmu->global_status_rsvd:

1. An intercepted guest WRMSR(MSR_CORE_PERF_GLOBAL_STATUS_SET, BIT_ULL(63)) succeeds instead of injecting #GP(0) and sets bit 63 in pmu->global_status.
2. When bit 63 is set in pmu->global_status (whether it was set by kvm_pmu_set_msr() or by intel_mediated_pmu_put()), the next intel_mediated_pmu_load() will try to write an illegal value to MSR_CORE_PERF_GLOBAL_STATUS_SET. I don't think we get a #GP, because every wrmsr is "safe" these days, but the write will be ignored.

OTOH, what about PerfMon v4 bits 58 (GLOBAL_STATUS_LBRS_FROZEN), 59 (GLOBAL_STATUS_COUNTERS_FROZEN), and 61 (GLOBAL_STATUS_OVF_UNCORE)? Per Intel SDM Vol. 4, Table 2-2, those bits are valid in IA32_PERF_GLOBAL_STATUS, IA32_PERF_GLOBAL_STATUS_RESET, and IA32_PERF_GLOBAL_STATUS_SET when CPUID.0AH:EAX[7:0] > 3.

When those MSRs are passed through in mediated mode, the guest can set bits 58, 59, or 61 in hardware via IA32_PERF_GLOBAL_STATUS_SET. Leaving those bits set in pmu->global_status_rsvd for pmu->version >= 4 will break KVM_SET_MSRS(MSR_CORE_PERF_GLOBAL_STATUS) as well as any intercepted guest writes to GLOBAL_OVF_CTRL / GLOBAL_STATUS_SET.

As I pointed out in patch 04/16, we can't pass through the PMU global controls unless their reserved bits match hardware.

[ ... ]

Quoted 15 lines
arch/x86/kvm/pmu.c +15 -0
+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)
+{
+       struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+       unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+       unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+       bool pmi_inuse = false;
+       u64 eventsel, data = 0;
+       u32 fixed_ctrl;
+       int i;
+
+       kvm_for_each_gp_counter(i, gp_mask) {
+               eventsel = pmu->gp_counters[i].eventsel;
+
+               if (eventsel & ARCH_PERFMON_EVENTSEL_EVENT)
+                       data |= BIT_ULL(i);

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)

Quoted 10 lines
arch/x86/kvm/pmu.c +10 -0
+               pmi_inuse |= eventsel & ARCH_PERFMON_EVENTSEL_INT;
+       }
+       kvm_for_each_fixed_counter(i, fixed_mask) {
+               fixed_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i);
+
+               if (fixed_ctrl & (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER))
+                       data |= BIT_ULL(KVM_FIXED_PMC_BASE_IDX + i);
+               pmi_inuse |= fixed_ctrl & INTEL_FIXED_0_ENABLE_PMI;
+       }
+       pmi_inuse |= pmu->pebs_enable;

Nit: I am always complaining about unnecessary use of '!!', but I think it is warranted in the two assignments to pmi_inuse above.

[ ... ]

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

Quoted 12 lines
arch/x86/kvm/vmx/vmx.c +4 -0
index 56daf5c61082..2dabb7e64956 100644
@@ -4282,6 +4282,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);
+       vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_STATUS_SET,
+                                 MSR_TYPE_RW, intercept || pmu->version < 4);
+       vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_INUSE,
+                                 MSR_TYPE_RW, intercept || pmu->version < 4);

When a PMU event filter denies a fixed counter in mediated mode, kvm_mediated_pmu_refresh_event_filter() clears the entire 4-bit control nybble in pmu->fixed_ctr_ctrl_hw (written to physical IA32_FIXED_CTR_CTRL), while pmu->fixed_ctr_ctrl contains the guest-written value.

If MSR_CORE_PERF_GLOBAL_INUSE is passed through, a guest RDMSR of IA32_PERF_GLOBAL_INUSE will see FCx_InUse == 0 (and potentially PMI_InUse == 0) from hardware even though RDMSR(IA32_FIXED_CTR_CTRL) reports the fixed counter as enabled (and intel_pmu_get_global_inuse() reports it as in-use when MSR_CORE_PERF_GLOBAL_INUSE is intercepted).

author
#43 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 50 lines · Jim Mattson
Quoted 49 lines · Zide Chen

Intel PerfMon v4 introduces IA32_PERF_GLOBAL_STATUS_SET (0x391) to allow software to set individual bits in the global status MSR. Reads of IA32_PERF_GLOBAL_STATUS_SET always return zero.

IA32_PERF_GLOBAL_INUSE (0x392) is also introduced in v4, to track which counters and the PMI are currently claimed by other agents, allowing independent software agents to check counter availability without a shared scheduler arbitrating between them.

IA32_PERF_GLOBAL_INUSE is an read-only MSR, and any write attempt results in a #GP.

Neither MSR is part of the VM state, so they don't need to be advertised to userspace, nor saved and restored during live migration.

---
v2:
- Change intel_pmu_get_global_inuse() to return u64, to match the surrounding code style.
- Add the missing vmcs02 updates for these two MSRs.
- Change "> 3" to ">= 4" to make the "v4-gated" more obvious and match the existing code style.

---
 arch/x86/include/asm/msr-index.h |  4 ++++
 arch/x86/kvm/pmu.c               |  9 ++++++++
 arch/x86/kvm/vmx/nested.c        |  2 ++
 arch/x86/kvm/vmx/pmu_intel.c     | 37 ++++++++++++++++++++++++++++++++
 arch/x86/kvm/vmx/vmx.c           |  4 ++++
 5 files changed, 56 insertions(+)
arch/x86/include/asm/msr-index.h +4 -0
index 11b99d237e05..0b093cf41edf 100644
@@ -1240,6 +1240,10 @@
 #define MSR_CORE_PERF_GLOBAL_CTRL      0x0000038f
 #define MSR_CORE_PERF_GLOBAL_OVF_CTRL  0x00000390
 #define MSR_CORE_PERF_GLOBAL_STATUS_SET        0x00000391
+#define MSR_CORE_PERF_GLOBAL_INUSE     0x00000392
+
+/* Intel IA32_PERF_GLOBAL_INUSE MSR */
+#define PERF_GLOBAL_INUSE_PMI_INUSE    BIT_ULL(63)
 
 #define MSR_PERF_METRICS               0x00000329
 
Quoted 15 lines

arch/x86/kvm/pmu.c bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) · line 836

Quoted 10 lines
arch/x86/kvm/pmu.c +2 -0
index 437a7bc49bf8..7c05cf5157bf 100644
@@ -832,6 +832,8 @@ bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
        case MSR_CORE_PERF_GLOBAL_CTRL:
        case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
                return kvm_pmu_has_perf_global_ctrl(vcpu_to_pmu(vcpu));
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+               return vcpu_to_pmu(vcpu)->version >= 4;

MSR_CORE_PERF_GLOBAL_STATUS_SET should be handled in pmu_intel.c? It is an Intel-specific MSR (note that commit 79a71cc2568f ("KVM: x86/pmu: Move Intel specific MSRs to pmu_intel.c") moved all of MSR_CORE_PERF_GLOBAL_* into pmu_intel.c).

Right. In v3 (rebased on top of 79a71cc2568f), both MSRs are handled in pmu_intel.c, so it won't be exposed on AMD.

Quoted 31 lines

Moreover, checking "vcpu_to_pmu(vcpu)->version >= 4" here without checking the vendor will expose MSR_CORE_PERF_GLOBAL_STATUS_SET on AMD when AMD's pmu->version reaches 4.

Quoted 9 lines
arch/x86/kvm/pmu.c +1 -0
@@ -865,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
        case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
                msr_info->data = 0;
                break;
        default:

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 942

Quoted 9 lines
arch/x86/kvm/pmu.c +6 -0
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                if (!msr_info->host_initiated)
                        pmu->global_status &= ~data;
                break;
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+               if (data & pmu->global_status_rsvd)
+                       return 1;
+               if (!msr_info->host_initiated)
+                       pmu->global_status |= data;
+               break;

According to Intel SDM Vol. 4, Table 2-2 ("IA-32 Architectural MSRs"), IA32_PERF_GLOBAL_STATUS.CondChgd[bit 63] cannot be set by writing to IA32_PERF_GLOBAL_STATUS_SET, because IA32_PERF_GLOBAL_STATUS_SET[bit 63] is reserved. In other words, it is not possible to save/restore IA32_PERF_GLOBAL_STATUS when bit 63 is set.

Because bit 63 is clear in pmu->global_status_rsvd:

Quoted 8 lines

1. An intercepted guest WRMSR(MSR_CORE_PERF_GLOBAL_STATUS_SET, BIT_ULL(63)) succeeds instead of injecting #GP(0) and sets bit 63 in pmu->global_status.
2. When bit 63 is set in pmu->global_status (whether it was set by kvm_pmu_set_msr() or by intel_mediated_pmu_put()), the next intel_mediated_pmu_load() will try to write an illegal value to MSR_CORE_PERF_GLOBAL_STATUS_SET. I don't think we get a #GP, because every wrmsr is "safe" these days, but the write will be ignored.

Thank you for the detailed analysis. Seems the following change is needed if the below proposal is not adopted:

@@ -939,7 +939,7 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)

                        pmu->global_status &= ~data;
                break;
        case MSR_CORE_PERF_GLOBAL_STATUS_SET:

- if (data & pmu->global_status_rsvd)
+ if (data & (pmu->global_status_rsvd |
GLOBAL_STATUS_COND_CHG)

Quoted 6 lines

OTOH, what about PerfMon v4 bits 58 (GLOBAL_STATUS_LBRS_FROZEN), 59 (GLOBAL_STATUS_COUNTERS_FROZEN), and 61 (GLOBAL_STATUS_OVF_UNCORE)? Per Intel SDM Vol. 4, Table 2-2, those bits are valid in IA32_PERF_GLOBAL_STATUS, IA32_PERF_GLOBAL_STATUS_RESET, and IA32_PERF_GLOBAL_STATUS_SET when CPUID.0AH:EAX[7:0] > 3.

Quoted 8 lines

When those MSRs are passed through in mediated mode, the guest can set bits 58, 59, or 61 in hardware via IA32_PERF_GLOBAL_STATUS_SET. Leaving those bits set in pmu->global_status_rsvd for pmu->version >= 4 will break KVM_SET_MSRS(MSR_CORE_PERF_GLOBAL_STATUS) as well as any intercepted guest writes to GLOBAL_OVF_CTRL / GLOBAL_STATUS_SET.

As I pointed out in patch 04/16, we can't pass through the PMU global controls unless their reserved bits match hardware.

Totally agree. Then the handling of bit 55 breaks this rule as well.

if (vmx_pt_mode_is_host_guest())

        pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;

Always intercepting GLOBAL_OVF_CTRL and GLOBAL_STATUS_SET can solve this problem, but it may impair guest performance, especially for nested.

Instead, I'd propal to have separate reserved masks:
- pmu->global_status_rsvd: unchanged, strictly tracking virtualized guest state and preserving live migration validation.
- pmu->global_status_{clr,set}_rsvd: unmasking all architecturally valid hardware bits for the corresponding MSRs on v4+.

Any bits allowed by hardware but unvirtualized by KVM are scrubbed/filtered in intel_mediated_pmu_{put,load}().

Quoted 22 lines

[ ... ]

Quoted 20 lines
arch/x86/kvm/pmu.c +20 -0
+static u64 intel_pmu_get_global_inuse(struct kvm_vcpu *vcpu)
+{
+       struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
+       unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+       unsigned long gp_mask = kvm_gp_pmc_mask(pmu);
+       bool pmi_inuse = false;
+       u64 eventsel, data = 0;
+       u32 fixed_ctrl;
+       int i;
+
+       kvm_for_each_gp_counter(i, gp_mask) {
+               eventsel = pmu->gp_counters[i].eventsel;
+
+               if (eventsel & ARCH_PERFMON_EVENTSEL_EVENT)
+                       data |= BIT_ULL(i);
+               pmi_inuse |= eventsel & ARCH_PERFMON_EVENTSEL_INT;
+       }
+       kvm_for_each_fixed_counter(i, fixed_mask) {
+               fixed_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i);
+
Quoted 8 lines

arch/x86/kvm/pmu.c

Quoted 5 lines
arch/x86/kvm/pmu.c +5 -0
+               if (fixed_ctrl & (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER))
+                       data |= BIT_ULL(KVM_FIXED_PMC_BASE_IDX + i);
+               pmi_inuse |= fixed_ctrl & INTEL_FIXED_0_ENABLE_PMI;
+       }
+       pmi_inuse |= pmu->pebs_enable;

Nit: I am always complaining about unnecessary use of '!!', but I think it is warranted in the two assignments to pmi_inuse above.

True, !! isn't required since pmi_inuse is a bool, but adding it makes the intent clearer. I'll add it.

Quoted 11 lines

[ ... ]

Quoted 9 lines
arch/x86/kvm/vmx/vmx.c +1 -0
index 56daf5c61082..2dabb7e64956 100644
@@ -4282,6 +4282,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);
+       vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_STATUS_SET,
Quoted 8 lines

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

Quoted 3 lines
arch/x86/kvm/vmx/vmx.c +3 -0
+                                 MSR_TYPE_RW, intercept || pmu->version < 4);
+       vmx_set_intercept_for_msr(vcpu, MSR_CORE_PERF_GLOBAL_INUSE,
+                                 MSR_TYPE_RW, intercept || pmu->version < 4);

When a PMU event filter denies a fixed counter in mediated mode, kvm_mediated_pmu_refresh_event_filter() clears the entire 4-bit control nybble in pmu->fixed_ctr_ctrl_hw (written to physical IA32_FIXED_CTR_CTRL), while pmu->fixed_ctr_ctrl contains the guest-written value.

Good point. Clearing the entire 0xf nibble was overly aggressive and clobbered non-enable bits (ENABLE_PMI, AnyThread, and METRICS_CLEAR_EN).

But as long as we always intercep the INUSE MSR, then this is irrelevant here. I can refined kvm_mediated_pmu_refresh_event_filter() to only mask out bits [1:0] (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER), in the Topdown patch set.

Quoted 5 lines

If MSR_CORE_PERF_GLOBAL_INUSE is passed through, a guest RDMSR of IA32_PERF_GLOBAL_INUSE will see FCx_InUse == 0 (and potentially PMI_InUse == 0) from hardware even though RDMSR(IA32_FIXED_CTR_CTRL) reports the fixed counter as enabled (and intel_pmu_get_global_inuse() reports it as in-use when MSR_CORE_PERF_GLOBAL_INUSE is intercepted).

Agreed.

The architectural purpose of the INUSE MSR is software resource allocation within the OS, not a physical hardware sensor.

The purpose of KVM_SET_PMU_EVENT_FILTER is to stop hardware counting while letting the guest believe the event was successfully configured.

Thus, if an event is filtered out, the INUSE MSR should still consider the counter to be in use. Since kvm_mediated_pmu_refresh_event_filter() needs to mask out the (INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER) bits anyway, passing through the INUSE MSR fails to reflect this.

So it seems better to always intercept the INUSE MSR.

#44 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 1 line · Zide

On 10/28/2026 4:24 PM, Jim Mattson wrote:

[ ... ]

Quoted 11 lines
Quoted 2 lines

As I pointed out in patch 04/16, we can't pass through the PMU global controls unless their reserved bits match hardware.

Totally agree. Then the handling of bit 55 breaks this rule as well.

if (vmx_pt_mode_is_host_guest())

        pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;

Always intercepting GLOBAL_OVF_CTRL and GLOBAL_STATUS_SET can solve this problem, but it may impair guest performance, especially for nested.

Quoted 8 lines

Instead, I'd propal to have separate reserved masks:
- pmu->global_status_rsvd: unchanged, strictly tracking virtualized guest state and preserving live migration validation.
- pmu->global_status_{clr,set}_rsvd: unmasking all architecturally valid hardware bits for the corresponding MSRs on v4+.

Any bits allowed by hardware but unvirtualized by KVM are scrubbed/filtered in intel_mediated_pmu_{put,load}().

That is a terrible proposal. MSR bits that vanish at unpredictable times?!? No. If an MSR contains bits that aren't virtualized, that MSR can't be passed through. Full stop.

author
#45 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 20 lines · Jim Mattson
Quoted 1 line · Zide

On 10/28/2026 4:24 PM, Jim Mattson wrote:

[ ... ]

Quoted 17 lines
Quoted 2 lines

As I pointed out in patch 04/16, we can't pass through the PMU global controls unless their reserved bits match hardware.

Totally agree. Then the handling of bit 55 breaks this rule as well.

if (vmx_pt_mode_is_host_guest())

        pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;

Always intercepting GLOBAL_OVF_CTRL and GLOBAL_STATUS_SET can solve this problem, but it may impair guest performance, especially for nested.

Instead, I'd propal to have separate reserved masks:
- pmu->global_status_rsvd: unchanged, strictly tracking virtualized guest state and preserving live migration validation.
- pmu->global_status_{clr,set}_rsvd: unmasking all architecturally valid hardware bits for the corresponding MSRs on v4+.

Quoted 6 lines
Quoted 2 lines

Any bits allowed by hardware but unvirtualized by KVM are scrubbed/filtered in intel_mediated_pmu_{put,load}().

That is a terrible proposal. MSR bits that vanish at unpredictable times?!? No. If an MSR contains bits that aren't virtualized, that MSR can't be passed through. Full stop.

OK, I agree. I will intercept GLOBAL_STATUS_SET (0x391) and remove the nested_vmx_merge_msr_bitmaps() for it.

However, I propose we keep GLOBAL_OVF_CTRL (0x390) passed through, even though its reserved bits don't match hardware. Since it is write-1-to-clear, it cannot manufacture unvirtualized state or leak host data.

The only divergence is the lack of a #GP if the guest writes an unvirtualized but hardware-valid bit. If we want to eliminate this behavior by intercepting it, it will force an additional VM-exit on every single guest PMI.

What do you think?

#46 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 39 lines · Zide
Quoted 20 lines · Jim Mattson
Quoted 1 line · Zide

On 10/28/2026 4:24 PM, Jim Mattson wrote:

[ ... ]

Quoted 17 lines
Quoted 2 lines

As I pointed out in patch 04/16, we can't pass through the PMU global controls unless their reserved bits match hardware.

Totally agree. Then the handling of bit 55 breaks this rule as well.

if (vmx_pt_mode_is_host_guest())

        pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;

Always intercepting GLOBAL_OVF_CTRL and GLOBAL_STATUS_SET can solve this problem, but it may impair guest performance, especially for nested.

Instead, I'd propal to have separate reserved masks:
- pmu->global_status_rsvd: unchanged, strictly tracking virtualized guest state and preserving live migration validation.
- pmu->global_status_{clr,set}_rsvd: unmasking all architecturally valid hardware bits for the corresponding MSRs on v4+.

Quoted 6 lines
Quoted 2 lines

Any bits allowed by hardware but unvirtualized by KVM are scrubbed/filtered in intel_mediated_pmu_{put,load}().

That is a terrible proposal. MSR bits that vanish at unpredictable times?!? No. If an MSR contains bits that aren't virtualized, that MSR can't be passed through. Full stop.

OK, I agree. I will intercept GLOBAL_STATUS_SET (0x391) and remove the nested_vmx_merge_msr_bitmaps() for it.

However, I propose we keep GLOBAL_OVF_CTRL (0x390) passed through, even though its reserved bits don't match hardware. Since it is write-1-to-clear, it cannot manufacture unvirtualized state or leak host data.

Quoted 6 lines

The only divergence is the lack of a #GP if the guest writes an unvirtualized but hardware-valid bit. If we want to eliminate this behavior by intercepting it, it will force an additional VM-exit on every single guest PMI.

What do you think?

That seems reasonable to me, as long as the architectural misbehavior is documented in Documentation/virt/kvm/x86/errata.rst.

author
#47 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 45 lines · Jim Mattson
Quoted 44 lines · Zide
Quoted 20 lines · Jim Mattson
Quoted 1 line · Zide

On 10/28/2026 4:24 PM, Jim Mattson wrote:

[ ... ]

Quoted 17 lines
Quoted 2 lines

As I pointed out in patch 04/16, we can't pass through the PMU global controls unless their reserved bits match hardware.

Totally agree. Then the handling of bit 55 breaks this rule as well.

if (vmx_pt_mode_is_host_guest())

        pmu->global_status_rsvd &= ~GLOBAL_STATUS_TRACE_TOPAPMI;

Always intercepting GLOBAL_OVF_CTRL and GLOBAL_STATUS_SET can solve this problem, but it may impair guest performance, especially for nested.

Instead, I'd propal to have separate reserved masks:
- pmu->global_status_rsvd: unchanged, strictly tracking virtualized guest state and preserving live migration validation.
- pmu->global_status_{clr,set}_rsvd: unmasking all architecturally valid hardware bits for the corresponding MSRs on v4+.

Quoted 6 lines
Quoted 2 lines

Any bits allowed by hardware but unvirtualized by KVM are scrubbed/filtered in intel_mediated_pmu_{put,load}().

That is a terrible proposal. MSR bits that vanish at unpredictable times?!? No. If an MSR contains bits that aren't virtualized, that MSR can't be passed through. Full stop.

OK, I agree. I will intercept GLOBAL_STATUS_SET (0x391) and remove the nested_vmx_merge_msr_bitmaps() for it.

However, I propose we keep GLOBAL_OVF_CTRL (0x390) passed through, even though its reserved bits don't match hardware. Since it is write-1-to-clear, it cannot manufacture unvirtualized state or leak host data.

The only divergence is the lack of a #GP if the guest writes an unvirtualized but hardware-valid bit. If we want to eliminate this behavior by intercepting it, it will force an additional VM-exit on every single guest PMI.

Quoted 4 lines
Quoted 1 line

What do you think?

That seems reasonable to me, as long as the architectural misbehavior is documented in Documentation/virt/kvm/x86/errata.rst.

How about add something like this to errata.rst:

IA32_PERF_GLOBAL_STATUS_RESET
-----------------------------
To reduce VM-exits to performance-critical paths, KVM passes through writes to IA32_PERF_GLOBAL_STATUS_RESET (0x390) in mediated PMU mode, even if the MSR has bits that are not supported by KVM. Consequently, KVM does not inject #GP if the guest writes bits that are valid in hardware but unvirtualized by KVM.

#48 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

In reply to Jim Mattson
Quoted 27 lines
Quoted 9 lines
arch/x86/kvm/pmu.c +1 -0
@@ -865,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
        case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
                msr_info->data = 0;
                break;
        default:

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 942

Quoted 9 lines
arch/x86/kvm/pmu.c +6 -0
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                if (!msr_info->host_initiated)
                        pmu->global_status &= ~data;
                break;
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+               if (data & pmu->global_status_rsvd)
+                       return 1;
+               if (!msr_info->host_initiated)
+                       pmu->global_status |= data;
+               break;

According to Intel SDM Vol. 4, Table 2-2 ("IA-32 Architectural MSRs"), IA32_PERF_GLOBAL_STATUS.CondChgd[bit 63] cannot be set by writing to IA32_PERF_GLOBAL_STATUS_SET, because IA32_PERF_GLOBAL_STATUS_SET[bit 63] is reserved. In other words, it is not possible to save/restore IA32_PERF_GLOBAL_STATUS when bit 63 is set.

Because bit 63 is clear in pmu->global_status_rsvd:

Quoted 8 lines

1. An intercepted guest WRMSR(MSR_CORE_PERF_GLOBAL_STATUS_SET, BIT_ULL(63)) succeeds instead of injecting #GP(0) and sets bit 63 in pmu->global_status.
2. When bit 63 is set in pmu->global_status (whether it was set by kvm_pmu_set_msr() or by intel_mediated_pmu_put()), the next intel_mediated_pmu_load() will try to write an illegal value to MSR_CORE_PERF_GLOBAL_STATUS_SET. I don't think we get a #GP, because every wrmsr is "safe" these days, but the write will be ignored.

On SPR, it does produce a #GP that gets ignored (and logged) and the bit is not set. Interestingly, on CLX, the write goes through and the bit is set. Didn't check other CPUs.

Quoted 6 lines

OTOH, what about PerfMon v4 bits 58 (GLOBAL_STATUS_LBRS_FROZEN), 59 (GLOBAL_STATUS_COUNTERS_FROZEN), and 61 (GLOBAL_STATUS_OVF_UNCORE)? Per Intel SDM Vol. 4, Table 2-2, those bits are valid in IA32_PERF_GLOBAL_STATUS, IA32_PERF_GLOBAL_STATUS_RESET, and IA32_PERF_GLOBAL_STATUS_SET when CPUID.0AH:EAX[7:0] > 3.

Quoted 8 lines

When those MSRs are passed through in mediated mode, the guest can set bits 58, 59, or 61 in hardware via IA32_PERF_GLOBAL_STATUS_SET. Leaving those bits set in pmu->global_status_rsvd for pmu->version >= 4 will break KVM_SET_MSRS(MSR_CORE_PERF_GLOBAL_STATUS) as well as any intercepted guest writes to GLOBAL_OVF_CTRL / GLOBAL_STATUS_SET.

As I pointed out in patch 04/16, we can't pass through the PMU global controls unless their reserved bits match hardware.

I think there are two other relevant (but pre-existing) problems here:

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

(I think the PMI may also be lost as LVTPC points at the mediated vPMU vector)

2. On AMD, bit 58 and 59 have a similar problem. HW accepts those bits but they are reserved for the guest, so the guest can set them if GLOBAL_STATUS_SET is passed through, but userspace cannot restore them.

author
#49 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 36 lines · Yosry Ahmed
Quoted 27 lines
Quoted 9 lines
arch/x86/kvm/pmu.c +1 -0
@@ -865,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
        case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
                msr_info->data = 0;
                break;
        default:

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 942

Quoted 9 lines
arch/x86/kvm/pmu.c +6 -0
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                if (!msr_info->host_initiated)
                        pmu->global_status &= ~data;
                break;
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+               if (data & pmu->global_status_rsvd)
+                       return 1;
+               if (!msr_info->host_initiated)
+                       pmu->global_status |= data;
+               break;

According to Intel SDM Vol. 4, Table 2-2 ("IA-32 Architectural MSRs"), IA32_PERF_GLOBAL_STATUS.CondChgd[bit 63] cannot be set by writing to IA32_PERF_GLOBAL_STATUS_SET, because IA32_PERF_GLOBAL_STATUS_SET[bit 63] is reserved. In other words, it is not possible to save/restore IA32_PERF_GLOBAL_STATUS when bit 63 is set.

Because bit 63 is clear in pmu->global_status_rsvd:

Quoted 8 lines

1. An intercepted guest WRMSR(MSR_CORE_PERF_GLOBAL_STATUS_SET, BIT_ULL(63)) succeeds instead of injecting #GP(0) and sets bit 63 in pmu->global_status.
2. When bit 63 is set in pmu->global_status (whether it was set by kvm_pmu_set_msr() or by intel_mediated_pmu_put()), the next intel_mediated_pmu_load() will try to write an illegal value to MSR_CORE_PERF_GLOBAL_STATUS_SET. I don't think we get a #GP, because every wrmsr is "safe" these days, but the write will be ignored.

Quoted 3 lines

On SPR, it does produce a #GP that gets ignored (and logged) and the bit is not set. Interestingly, on CLX, the write goes through and the bit is set. Didn't check other CPUs.

Good catch! I tested a few more CPUs (DMR/CWF/SRF/GNR/NVL/LNL), and all of them #GP on writes that set bit 63.

However, SKX, CLX, and ICX accept writes with bit 63 set and do not generate #GP. On those systems, bit 63 also becomes visible when reading back GLOBAL_STATUS. This behavior is documented in their errata:

SKX95/CLX89/ICX166. Bit 63 of IA32_PERF_GLOBAL_STATUS_SET MSR (391H) is reserved. Due to this erratum, setting the bit will not result in #GP.

SKX:
https://cdrdv2-public.intel.com/336065/336065_Intel%20Xeon%20Processor%20Scalable%20Family%20Public%20Specification%20Update_rev17.pdf

CLX:
https://cdrdv2-public.intel.com/338848/338848_2nd%20Gen%20Intel%C2%AE%20Xeon%C2%AE%20Scalable%20Processors%20Specification%20Update_Rev028US.pdf

ICX:
https://cdrdv2-public.intel.com/637780/637780_3rd_Gen_Xeon_Scalable_Spec_Update_026US.pdf

Given that SKX95/CLX89/ICX166 document this behavior as an erratum, while SPR and newer CPUs appear to follow the SDM and #GP, should KVM continue to inject #GP for writes that set bit 63?

Quoted 15 lines
Quoted 6 lines

OTOH, what about PerfMon v4 bits 58 (GLOBAL_STATUS_LBRS_FROZEN), 59 (GLOBAL_STATUS_COUNTERS_FROZEN), and 61 (GLOBAL_STATUS_OVF_UNCORE)? Per Intel SDM Vol. 4, Table 2-2, those bits are valid in IA32_PERF_GLOBAL_STATUS, IA32_PERF_GLOBAL_STATUS_RESET, and IA32_PERF_GLOBAL_STATUS_SET when CPUID.0AH:EAX[7:0] > 3.

Quoted 8 lines

When those MSRs are passed through in mediated mode, the guest can set bits 58, 59, or 61 in hardware via IA32_PERF_GLOBAL_STATUS_SET. Leaving those bits set in pmu->global_status_rsvd for pmu->version >= 4 will break KVM_SET_MSRS(MSR_CORE_PERF_GLOBAL_STATUS) as well as any intercepted guest writes to GLOBAL_OVF_CTRL / GLOBAL_STATUS_SET.

As I pointed out in patch 04/16, we can't pass through the PMU global controls unless their reserved bits match hardware.

Quoted 7 lines

I think there are two other relevant (but pre-existing) problems here:

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

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

Quoted 6 lines

(I think the PMI may also be lost as LVTPC points at the mediated vPMU vector)

2. On AMD, bit 58 and 59 have a similar problem. HW accepts those bits but they are reserved for the guest, so the guest can set them if GLOBAL_STATUS_SET is passed through, but userspace cannot restore them.

Right. That seems orthogonal to this series and could be addressed with follow-up fixes.

#50 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 60 lines · Zide
Quoted 36 lines · Yosry Ahmed
Quoted 27 lines
Quoted 9 lines
arch/x86/kvm/pmu.c +1 -0
@@ -865,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
        case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
                msr_info->data = 0;
                break;
        default:

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 942

Quoted 9 lines
arch/x86/kvm/pmu.c +6 -0
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                if (!msr_info->host_initiated)
                        pmu->global_status &= ~data;
                break;
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+               if (data & pmu->global_status_rsvd)
+                       return 1;
+               if (!msr_info->host_initiated)
+                       pmu->global_status |= data;
+               break;

According to Intel SDM Vol. 4, Table 2-2 ("IA-32 Architectural MSRs"), IA32_PERF_GLOBAL_STATUS.CondChgd[bit 63] cannot be set by writing to IA32_PERF_GLOBAL_STATUS_SET, because IA32_PERF_GLOBAL_STATUS_SET[bit 63] is reserved. In other words, it is not possible to save/restore IA32_PERF_GLOBAL_STATUS when bit 63 is set.

Because bit 63 is clear in pmu->global_status_rsvd:

Quoted 8 lines

1. An intercepted guest WRMSR(MSR_CORE_PERF_GLOBAL_STATUS_SET, BIT_ULL(63)) succeeds instead of injecting #GP(0) and sets bit 63 in pmu->global_status.
2. When bit 63 is set in pmu->global_status (whether it was set by kvm_pmu_set_msr() or by intel_mediated_pmu_put()), the next intel_mediated_pmu_load() will try to write an illegal value to MSR_CORE_PERF_GLOBAL_STATUS_SET. I don't think we get a #GP, because every wrmsr is "safe" these days, but the write will be ignored.

Quoted 3 lines

On SPR, it does produce a #GP that gets ignored (and logged) and the bit is not set. Interestingly, on CLX, the write goes through and the bit is set. Didn't check other CPUs.

Good catch! I tested a few more CPUs (DMR/CWF/SRF/GNR/NVL/LNL), and all of them #GP on writes that set bit 63.

However, SKX, CLX, and ICX accept writes with bit 63 set and do not generate #GP. On those systems, bit 63 also becomes visible when reading back GLOBAL_STATUS. This behavior is documented in their errata:

SKX95/CLX89/ICX166. Bit 63 of IA32_PERF_GLOBAL_STATUS_SET MSR (391H) is reserved. Due to this erratum, setting the bit will not result in #GP.

SKX:
https://cdrdv2-public.intel.com/336065/336065_Intel%20Xeon%20Processor%20Scalable%20Family%20Public%20Specification%20Update_rev17.pdf

CLX:
https://cdrdv2-public.intel.com/338848/338848_2nd%20Gen%20Intel%C2%AE%20Xeon%C2%AE%20Scalable%20Processors%20Specification%20Update_Rev028US.pdf

Quoted 6 lines

ICX:
https://cdrdv2-public.intel.com/637780/637780_3rd_Gen_Xeon_Scalable_Spec_Update_026US.pdf

Given that SKX95/CLX89/ICX166 document this behavior as an erratum, while SPR and newer CPUs appear to follow the SDM and #GP, should KVM continue to inject #GP for writes that set bit 63?

Ugh. It seems like we have two options, neither of them all that appealing.

1. KVM fixes the errata and raises #GP. To ensure consistency, KVM always intercepts the MSR on affected CPUs.
2. KVM mimics the errata and allows writes to bit 63 when virtualizing affected CPUs. This allows the MSR to be passed through, unless an unaffected CPU masquerades as an affected one.

Sean? WDYT?

author
#51 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 70 lines · Jim Mattson
Quoted 60 lines · Zide
Quoted 36 lines · Yosry Ahmed
Quoted 27 lines
Quoted 9 lines
arch/x86/kvm/pmu.c +1 -0
@@ -865,6 +867,7 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_CLR:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS_SET:
        case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
                msr_info->data = 0;
                break;
        default:

arch/x86/kvm/pmu.c int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) · line 942

Quoted 9 lines
arch/x86/kvm/pmu.c +6 -0
@@ -931,6 +934,12 @@ int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                if (!msr_info->host_initiated)
                        pmu->global_status &= ~data;
                break;
+       case MSR_CORE_PERF_GLOBAL_STATUS_SET:
+               if (data & pmu->global_status_rsvd)
+                       return 1;
+               if (!msr_info->host_initiated)
+                       pmu->global_status |= data;
+               break;

According to Intel SDM Vol. 4, Table 2-2 ("IA-32 Architectural MSRs"), IA32_PERF_GLOBAL_STATUS.CondChgd[bit 63] cannot be set by writing to IA32_PERF_GLOBAL_STATUS_SET, because IA32_PERF_GLOBAL_STATUS_SET[bit 63] is reserved. In other words, it is not possible to save/restore IA32_PERF_GLOBAL_STATUS when bit 63 is set.

Because bit 63 is clear in pmu->global_status_rsvd:

Quoted 8 lines

1. An intercepted guest WRMSR(MSR_CORE_PERF_GLOBAL_STATUS_SET, BIT_ULL(63)) succeeds instead of injecting #GP(0) and sets bit 63 in pmu->global_status.
2. When bit 63 is set in pmu->global_status (whether it was set by kvm_pmu_set_msr() or by intel_mediated_pmu_put()), the next intel_mediated_pmu_load() will try to write an illegal value to MSR_CORE_PERF_GLOBAL_STATUS_SET. I don't think we get a #GP, because every wrmsr is "safe" these days, but the write will be ignored.

Quoted 3 lines

On SPR, it does produce a #GP that gets ignored (and logged) and the bit is not set. Interestingly, on CLX, the write goes through and the bit is set. Didn't check other CPUs.

Good catch! I tested a few more CPUs (DMR/CWF/SRF/GNR/NVL/LNL), and all of them #GP on writes that set bit 63.

However, SKX, CLX, and ICX accept writes with bit 63 set and do not generate #GP. On those systems, bit 63 also becomes visible when reading back GLOBAL_STATUS. This behavior is documented in their errata:

SKX95/CLX89/ICX166. Bit 63 of IA32_PERF_GLOBAL_STATUS_SET MSR (391H) is reserved. Due to this erratum, setting the bit will not result in #GP.

SKX:
https://cdrdv2-public.intel.com/336065/336065_Intel%20Xeon%20Processor%20Scalable%20Family%20Public%20Specification%20Update_rev17.pdf

CLX:
https://cdrdv2-public.intel.com/338848/338848_2nd%20Gen%20Intel%C2%AE%20Xeon%C2%AE%20Scalable%20Processors%20Specification%20Update_Rev028US.pdf

Quoted 6 lines

ICX:
https://cdrdv2-public.intel.com/637780/637780_3rd_Gen_Xeon_Scalable_Spec_Update_026US.pdf

Given that SKX95/CLX89/ICX166 document this behavior as an erratum, while SPR and newer CPUs appear to follow the SDM and #GP, should KVM continue to inject #GP for writes that set bit 63?

Ugh. It seems like we have two options, neither of them all that appealing.

Quoted 7 lines

1. KVM fixes the errata and raises #GP. To ensure consistency, KVM always intercepts the MSR on affected CPUs.
2. KVM mimics the errata and allows writes to bit 63 when virtualizing affected CPUs. This allows the MSR to be passed through, unless an unaffected CPU masquerades as an affected one.

Sean? WDYT?

After looking into this more closely, I think KVM should simply reject guest writes to bit 63 (CondChgd).

In platform-specific documentation (EDS for SPR, ICX, DMR, PTL, etc.), this bit is explicitly described as: "Set in case of a change to IA32_MISC_ENABLES.PERFMON_AVAILABLE (bit 7)", clarifying the SDM wording "changes to the state of performance monitoring hardware", or "CondChgd: Status bits of this register have changed.".

This is also consistent with my testing, where I was unable to trigger CondChgd on the host while exercising other PMU functionality that sets GLOBAL_STATUS bits.

Since IA32_MISC_ENABLE is intercepted by KVM and is read-only for the guest, CondChgd should never be set on GLOBAL_STATUS in the guest.

KVM cannot preserve this bit on GLOBAL_STATUS across vPMU context switches, since it cannot be restored through GLOBAL_STATUS_SET. That is fine, however, since CondChgd is not set on the guest.

BTW, the history on early CPUs is somewhat messy. As mentioned earlier, some CPUs have errata that accept writing to bit 63 without #GP (SKX95, CLX89, CPX89, ICX166, ICL019, etc.). Additionally, the reset value of this bit is not consistent on some early CPUs, as noted in commit b292d7a10487 ("perf/x86/intel: ignore CondChgd bit to avoid false NMI handling").

I'd suggest splitting this into two separate patches: one patch to emulate each MSR, for clarity.

#52 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

In reply to Chen, Zide
Quoted 26 lines
Quoted 15 lines
Quoted 6 lines

OTOH, what about PerfMon v4 bits 58 (GLOBAL_STATUS_LBRS_FROZEN), 59 (GLOBAL_STATUS_COUNTERS_FROZEN), and 61 (GLOBAL_STATUS_OVF_UNCORE)? Per Intel SDM Vol. 4, Table 2-2, those bits are valid in IA32_PERF_GLOBAL_STATUS, IA32_PERF_GLOBAL_STATUS_RESET, and IA32_PERF_GLOBAL_STATUS_SET when CPUID.0AH:EAX[7:0] > 3.

Quoted 8 lines

When those MSRs are passed through in mediated mode, the guest can set bits 58, 59, or 61 in hardware via IA32_PERF_GLOBAL_STATUS_SET. Leaving those bits set in pmu->global_status_rsvd for pmu->version >= 4 will break KVM_SET_MSRS(MSR_CORE_PERF_GLOBAL_STATUS) as well as any intercepted guest writes to GLOBAL_OVF_CTRL / GLOBAL_STATUS_SET.

As I pointed out in patch 04/16, we can't pass through the PMU global controls unless their reserved bits match hardware.

Quoted 7 lines

I think there are two other relevant (but pre-existing) problems here:

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

Quoted 7 lines

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

Looking at the code:

kvm_mediated_pmu_load() -> perf_load_guest_context() -> perf_ctx_disable() will iterate PMUs with for_each_epc() and disable them, which skips PMUs without PERF_PMU_CAP_MEDIATED_VPMU.

Same for kvm_mediated_pmu_load() -> perf_load_guest_context() ->ctx_sched_out(), it also uses for_each_epc() to iterate PMUs and schedule them out.

pt_init() does not set PERF_PMU_CAP_MEDIATED_VPMU on pt_pmu.pmu.capabilities

Please correct me if I am wrong.

Quoted 6 lines

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

Quoted 3 lines

(I think the PMI may also be lost as LVTPC points at the mediated vPMU vector)

Quoted 6 lines
Quoted 3 lines

2. On AMD, bit 58 and 59 have a similar problem. HW accepts those bits but they are reserved for the guest, so the guest can set them if GLOBAL_STATUS_SET is passed through, but userspace cannot restore them.

Right. That seems orthogonal to this series and could be addressed with follow-up fixes.

Maybe AMD folks on the thread will take a look :)

author
#53 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 40 lines · Yosry Ahmed
Quoted 26 lines
Quoted 15 lines
Quoted 6 lines

OTOH, what about PerfMon v4 bits 58 (GLOBAL_STATUS_LBRS_FROZEN), 59 (GLOBAL_STATUS_COUNTERS_FROZEN), and 61 (GLOBAL_STATUS_OVF_UNCORE)? Per Intel SDM Vol. 4, Table 2-2, those bits are valid in IA32_PERF_GLOBAL_STATUS, IA32_PERF_GLOBAL_STATUS_RESET, and IA32_PERF_GLOBAL_STATUS_SET when CPUID.0AH:EAX[7:0] > 3.

Quoted 8 lines

When those MSRs are passed through in mediated mode, the guest can set bits 58, 59, or 61 in hardware via IA32_PERF_GLOBAL_STATUS_SET. Leaving those bits set in pmu->global_status_rsvd for pmu->version >= 4 will break KVM_SET_MSRS(MSR_CORE_PERF_GLOBAL_STATUS) as well as any intercepted guest writes to GLOBAL_OVF_CTRL / GLOBAL_STATUS_SET.

As I pointed out in patch 04/16, we can't pass through the PMU global controls unless their reserved bits match hardware.

Quoted 7 lines

I think there are two other relevant (but pre-existing) problems here:

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

Quoted 7 lines

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

Looking at the code:

kvm_mediated_pmu_load() -> perf_load_guest_context() -> perf_ctx_disable() will iterate PMUs with for_each_epc() and disable them, which skips PMUs without PERF_PMU_CAP_MEDIATED_VPMU.

Quoted 7 lines

Same for kvm_mediated_pmu_load() -> perf_load_guest_context() ->ctx_sched_out(), it also uses for_each_epc() to iterate PMUs and schedule them out.

pt_init() does not set PERF_PMU_CAP_MEDIATED_VPMU on pt_pmu.pmu.capabilities

Please correct me if I am wrong.

My apologies, this bug is indeed real because pt_pmu lacks the PERF_PMU_CAP_MEDIATED_VPMU capability.

Adding this capability to pt_pmu seems intuitive, but unlike

the core PMU (which merely toggles enable bits),
Intel PT is an AUX-based PMU. Stopping and restarting it on every VM transition is heavier, and I'm not sure whether it could introduce a noticeable performance penalty.

However, whether the solution is adding PERF_PMU_CAP_MEDIATED_VPMU or some other mechanism to enforce mutual exclusion between pt_pmu and mediated vPMU, we can address that in a separate patch.

To immediately protect guest state and live migration in this series, I'm thinking of adding the following defensive filter in intel_mediated_pmu_put():

+ /*

+  * Scrub bits that are not virtualized for the guest to prevent
+  * possible host state from leaking into guest state and breaking
+  * live migration.
+  */
+  pmu->global_status &= ~pmu->global_status_rsvd;
Quoted 15 lines
Quoted 7 lines

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

Quoted 3 lines

(I think the PMI may also be lost as LVTPC points at the mediated vPMU vector)

Quoted 6 lines
Quoted 3 lines

2. On AMD, bit 58 and 59 have a similar problem. HW accepts those bits but they are reserved for the guest, so the guest can set them if GLOBAL_STATUS_SET is passed through, but userspace cannot restore them.

Right. That seems orthogonal to this series and could be addressed with follow-up fixes.

Maybe AMD folks on the thread will take a look :)

#54 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 50 lines
Quoted 23 lines
Quoted 9 lines
Quoted 5 lines

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

Quoted 7 lines

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

Looking at the code:

kvm_mediated_pmu_load() -> perf_load_guest_context() -> perf_ctx_disable() will iterate PMUs with for_each_epc() and disable them, which skips PMUs without PERF_PMU_CAP_MEDIATED_VPMU.

Quoted 7 lines

Same for kvm_mediated_pmu_load() -> perf_load_guest_context() ->ctx_sched_out(), it also uses for_each_epc() to iterate PMUs and schedule them out.

pt_init() does not set PERF_PMU_CAP_MEDIATED_VPMU on pt_pmu.pmu.capabilities

Please correct me if I am wrong.

My apologies, this bug is indeed real because pt_pmu lacks the PERF_PMU_CAP_MEDIATED_VPMU capability.

Adding this capability to pt_pmu seems intuitive, but unlike

the core PMU (which merely toggles enable bits),
Intel PT is an AUX-based PMU. Stopping and restarting it on every VM transition is heavier, and I'm not sure whether it could introduce a noticeable performance penalty.

However, whether the solution is adding PERF_PMU_CAP_MEDIATED_VPMU or some other mechanism to enforce mutual exclusion between pt_pmu and mediated vPMU, we can address that in a separate patch.

To immediately protect guest state and live migration in this series, I'm thinking of adding the following defensive filter in intel_mediated_pmu_put():

+ /*

Review

Quoted 5 lines
(quoted diff) +5 -0
+  * Scrub bits that are not virtualized for the guest to prevent
+  * possible host state from leaking into guest state and breaking
+  * live migration.
+  */
+  pmu->global_status &= ~pmu->global_status_rsvd;

This doesn't prevent the leakage, it just clears it on the guest->host switch. So it helps live migration but I am not sure if it's better or worse if the guest sees the bit unexpectedly appear AND then unexpectedly disappear.

We can clear it when the MSR is read by userspace, which should fix live migration, but the guest will still see the bit, and it will still disappear unexpectedly but only during live migration.

The guest still won't be able to clear it when OVF_CTRL is intercepted.

author
#55 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 56 lines · Yosry Ahmed
Quoted 50 lines
Quoted 23 lines
Quoted 9 lines
Quoted 5 lines

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

Quoted 7 lines

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

Looking at the code:

kvm_mediated_pmu_load() -> perf_load_guest_context() -> perf_ctx_disable() will iterate PMUs with for_each_epc() and disable them, which skips PMUs without PERF_PMU_CAP_MEDIATED_VPMU.

Quoted 7 lines

Same for kvm_mediated_pmu_load() -> perf_load_guest_context() ->ctx_sched_out(), it also uses for_each_epc() to iterate PMUs and schedule them out.

pt_init() does not set PERF_PMU_CAP_MEDIATED_VPMU on pt_pmu.pmu.capabilities

Please correct me if I am wrong.

My apologies, this bug is indeed real because pt_pmu lacks the PERF_PMU_CAP_MEDIATED_VPMU capability.

Adding this capability to pt_pmu seems intuitive, but unlike

the core PMU (which merely toggles enable bits),
Intel PT is an AUX-based PMU. Stopping and restarting it on every VM transition is heavier, and I'm not sure whether it could introduce a noticeable performance penalty.

However, whether the solution is adding PERF_PMU_CAP_MEDIATED_VPMU or some other mechanism to enforce mutual exclusion between pt_pmu and mediated vPMU, we can address that in a separate patch.

To immediately protect guest state and live migration in this series, I'm thinking of adding the following defensive filter in intel_mediated_pmu_put():

+ /*

Review

Quoted 5 lines
(quoted diff) +5 -0
+  * Scrub bits that are not virtualized for the guest to prevent
+  * possible host state from leaking into guest state and breaking
+  * live migration.
+  */
+  pmu->global_status &= ~pmu->global_status_rsvd;
Quoted 4 lines

This doesn't prevent the leakage, it just clears it on the guest->host switch. So it helps live migration but I am not sure if it's better or worse if the guest sees the bit unexpectedly appear AND then unexpectedly disappear.

Right, as long as the GLOBAL_STATUS is passthru, the leak can't be prevented.

Quoted 5 lines

We can clear it when the MSR is read by userspace, which should fix live migration, but the guest will still see the bit, and it will still disappear unexpectedly but only during live migration.

The guest still won't be able to clear it when OVF_CTRL is intercepted.

GLOBAL_STATUS and OVF_CTRL share the same interception policy. If they are passed through, the guest can see the bit and clear it; if both are intercepted, then bit 55 can't leak to the guest.

It's not perfect, but at least the guest won't observe bit 55 and then get an unexpected #GP when attempting to clear it.

#56 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 71 lines · Zide
Quoted 56 lines · Yosry Ahmed
Quoted 50 lines
Quoted 23 lines
Quoted 9 lines
Quoted 5 lines

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

Quoted 7 lines

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

Looking at the code:

kvm_mediated_pmu_load() -> perf_load_guest_context() -> perf_ctx_disable() will iterate PMUs with for_each_epc() and disable them, which skips PMUs without PERF_PMU_CAP_MEDIATED_VPMU.

Quoted 7 lines

Same for kvm_mediated_pmu_load() -> perf_load_guest_context() ->ctx_sched_out(), it also uses for_each_epc() to iterate PMUs and schedule them out.

pt_init() does not set PERF_PMU_CAP_MEDIATED_VPMU on pt_pmu.pmu.capabilities

Please correct me if I am wrong.

My apologies, this bug is indeed real because pt_pmu lacks the PERF_PMU_CAP_MEDIATED_VPMU capability.

Adding this capability to pt_pmu seems intuitive, but unlike

the core PMU (which merely toggles enable bits),
Intel PT is an AUX-based PMU. Stopping and restarting it on every VM transition is heavier, and I'm not sure whether it could introduce a noticeable performance penalty.

However, whether the solution is adding PERF_PMU_CAP_MEDIATED_VPMU or some other mechanism to enforce mutual exclusion between pt_pmu and mediated vPMU, we can address that in a separate patch.

To immediately protect guest state and live migration in this series, I'm thinking of adding the following defensive filter in intel_mediated_pmu_put():

+ /*

Review

Quoted 5 lines
(quoted diff) +5 -0
+  * Scrub bits that are not virtualized for the guest to prevent
+  * possible host state from leaking into guest state and breaking
+  * live migration.
+  */
+  pmu->global_status &= ~pmu->global_status_rsvd;
Quoted 4 lines

This doesn't prevent the leakage, it just clears it on the guest->host switch. So it helps live migration but I am not sure if it's better or worse if the guest sees the bit unexpectedly appear AND then unexpectedly disappear.

Right, as long as the GLOBAL_STATUS is passthru, the leak can't be prevented.

Quoted 4 lines

We can clear it when the MSR is read by userspace, which should fix live migration, but the guest will still see the bit, and it will still disappear unexpectedly but only during live migration.

Quoted 8 lines
Quoted 1 line

The guest still won't be able to clear it when OVF_CTRL is intercepted.

GLOBAL_STATUS and OVF_CTRL share the same interception policy. If they are passed through, the guest can see the bit and clear it; if both are intercepted, then bit 55 can't leak to the guest.

It's not perfect, but at least the guest won't observe bit 55 and then get an unexpected #GP when attempting to clear it.

I'm not sure what imperfect solution you are referring to, but Yosry's proposal that the bit gets cleared on a guest->host switch is a non-starter. MSR bits must not vanish unpredictably.

author
#57 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 71 lines · Jim Mattson
Quoted 70 lines · Zide
Quoted 52 lines · Yosry Ahmed
Quoted 47 lines
Quoted 21 lines
Quoted 8 lines
Quoted 5 lines

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

Quoted 7 lines

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

Looking at the code:

kvm_mediated_pmu_load() -> perf_load_guest_context() -> perf_ctx_disable() will iterate PMUs with for_each_epc() and disable them, which skips PMUs without PERF_PMU_CAP_MEDIATED_VPMU.

Quoted 7 lines

Same for kvm_mediated_pmu_load() -> perf_load_guest_context() ->ctx_sched_out(), it also uses for_each_epc() to iterate PMUs and schedule them out.

pt_init() does not set PERF_PMU_CAP_MEDIATED_VPMU on pt_pmu.pmu.capabilities

Please correct me if I am wrong.

My apologies, this bug is indeed real because pt_pmu lacks the PERF_PMU_CAP_MEDIATED_VPMU capability.

Adding this capability to pt_pmu seems intuitive, but unlike

the core PMU (which merely toggles enable bits),
Intel PT is an AUX-based PMU. Stopping and restarting it on every VM transition is heavier, and I'm not sure whether it could introduce a noticeable performance penalty.

However, whether the solution is adding PERF_PMU_CAP_MEDIATED_VPMU or some other mechanism to enforce mutual exclusion between pt_pmu and mediated vPMU, we can address that in a separate patch.

To immediately protect guest state and live migration in this series, I'm thinking of adding the following defensive filter in intel_mediated_pmu_put():

+ /*

Review

Quoted 5 lines
(quoted diff) +5 -0
+  * Scrub bits that are not virtualized for the guest to prevent
+  * possible host state from leaking into guest state and breaking
+  * live migration.
+  */
+  pmu->global_status &= ~pmu->global_status_rsvd;
Quoted 4 lines

This doesn't prevent the leakage, it just clears it on the guest->host switch. So it helps live migration but I am not sure if it's better or worse if the guest sees the bit unexpectedly appear AND then unexpectedly disappear.

Right, as long as the GLOBAL_STATUS is passthru, the leak can't be prevented.

Quoted 5 lines

We can clear it when the MSR is read by userspace, which should fix live migration, but the guest will still see the bit, and it will still disappear unexpectedly but only during live migration.

The guest still won't be able to clear it when OVF_CTRL is intercepted.

GLOBAL_STATUS and OVF_CTRL share the same interception policy. If they are passed through, the guest can see the bit and clear it; if both are intercepted, then bit 55 can't leak to the guest.

Quoted 5 lines
Quoted 2 lines

It's not perfect, but at least the guest won't observe bit 55 and then get an unexpected #GP when attempting to clear it.

I'm not sure what imperfect solution you are referring to, but Yosry's proposal that the bit gets cleared on a guest->host switch is a non-starter. MSR bits must not vanish unpredictably.

Agree, then maybe Yosry's suggestion is what we can do for now: filter out unsupported bits when the MSR is read by userspace.

arch/x86/kvm/pmu.c +0 -0
index 38c7a4fa1007..3849b3f5234a 100644
@@ -862,6 +862,8 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct

msr_data *msr_info)

        switch (msr) {
        case MSR_CORE_PERF_GLOBAL_STATUS:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:

+ if (msr_info->host_initiated)
+ pmu->global_status &= ~pmu->global_status_rsvd;

The underlying issue still requires proper mutual exclusion between Intel PT and mediated vPMU, which I think can be addressed separately.

#58 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 75 lines · Zide
Quoted 71 lines · Jim Mattson
Quoted 70 lines · Zide
Quoted 52 lines · Yosry Ahmed
Quoted 47 lines
Quoted 21 lines
Quoted 8 lines
Quoted 5 lines

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

Quoted 7 lines

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

Looking at the code:

kvm_mediated_pmu_load() -> perf_load_guest_context() -> perf_ctx_disable() will iterate PMUs with for_each_epc() and disable them, which skips PMUs without PERF_PMU_CAP_MEDIATED_VPMU.

Quoted 7 lines

Same for kvm_mediated_pmu_load() -> perf_load_guest_context() ->ctx_sched_out(), it also uses for_each_epc() to iterate PMUs and schedule them out.

pt_init() does not set PERF_PMU_CAP_MEDIATED_VPMU on pt_pmu.pmu.capabilities

Please correct me if I am wrong.

My apologies, this bug is indeed real because pt_pmu lacks the PERF_PMU_CAP_MEDIATED_VPMU capability.

Adding this capability to pt_pmu seems intuitive, but unlike

the core PMU (which merely toggles enable bits),
Intel PT is an AUX-based PMU. Stopping and restarting it on every VM transition is heavier, and I'm not sure whether it could introduce a noticeable performance penalty.

However, whether the solution is adding PERF_PMU_CAP_MEDIATED_VPMU or some other mechanism to enforce mutual exclusion between pt_pmu and mediated vPMU, we can address that in a separate patch.

To immediately protect guest state and live migration in this series, I'm thinking of adding the following defensive filter in intel_mediated_pmu_put():

+ /*

Review

Quoted 5 lines
(quoted diff) +5 -0
+  * Scrub bits that are not virtualized for the guest to prevent
+  * possible host state from leaking into guest state and breaking
+  * live migration.
+  */
+  pmu->global_status &= ~pmu->global_status_rsvd;
Quoted 4 lines

This doesn't prevent the leakage, it just clears it on the guest->host switch. So it helps live migration but I am not sure if it's better or worse if the guest sees the bit unexpectedly appear AND then unexpectedly disappear.

Right, as long as the GLOBAL_STATUS is passthru, the leak can't be prevented.

Quoted 5 lines

We can clear it when the MSR is read by userspace, which should fix live migration, but the guest will still see the bit, and it will still disappear unexpectedly but only during live migration.

The guest still won't be able to clear it when OVF_CTRL is intercepted.

GLOBAL_STATUS and OVF_CTRL share the same interception policy. If they are passed through, the guest can see the bit and clear it; if both are intercepted, then bit 55 can't leak to the guest.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

It's not perfect, but at least the guest won't observe bit 55 and then get an unexpected #GP when attempting to clear it.

I'm not sure what imperfect solution you are referring to, but Yosry's proposal that the bit gets cleared on a guest->host switch is a non-starter. MSR bits must not vanish unpredictably.

I don't think I proposed this? :)

Quoted 3 lines

Agree, then maybe Yosry's suggestion is what we can do for now: filter out unsupported bits when the MSR is read by userspace.

This I did propose, but I also noted that the MSR bit will still vanish unpredictable, just on save/restore instead of guest->host switch, which should be better.

Quoted 13 lines
arch/x86/kvm/pmu.c +2 -0
index 38c7a4fa1007..3849b3f5234a 100644
@@ -862,6 +862,8 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct
 msr_data *msr_info)
        switch (msr) {
        case MSR_CORE_PERF_GLOBAL_STATUS:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+               if (msr_info->host_initiated)
+                       pmu->global_status &= ~pmu->global_status_rsvd;
 
Quoted 2 lines

The underlying issue still requires proper mutual exclusion between Intel PT and mediated vPMU, which I think can be addressed separately.

Or maybe handle the host PT while the guest is running? Not sure if that would be correct.

author
#59 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 85 lines · Yosry Ahmed
Quoted 75 lines · Zide
Quoted 71 lines · Jim Mattson
Quoted 70 lines · Zide
Quoted 52 lines · Yosry Ahmed
Quoted 47 lines
Quoted 21 lines
Quoted 8 lines
Quoted 5 lines

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

Quoted 7 lines

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

Looking at the code:

kvm_mediated_pmu_load() -> perf_load_guest_context() -> perf_ctx_disable() will iterate PMUs with for_each_epc() and disable them, which skips PMUs without PERF_PMU_CAP_MEDIATED_VPMU.

Quoted 7 lines

Same for kvm_mediated_pmu_load() -> perf_load_guest_context() ->ctx_sched_out(), it also uses for_each_epc() to iterate PMUs and schedule them out.

pt_init() does not set PERF_PMU_CAP_MEDIATED_VPMU on pt_pmu.pmu.capabilities

Please correct me if I am wrong.

My apologies, this bug is indeed real because pt_pmu lacks the PERF_PMU_CAP_MEDIATED_VPMU capability.

Adding this capability to pt_pmu seems intuitive, but unlike

the core PMU (which merely toggles enable bits),
Intel PT is an AUX-based PMU. Stopping and restarting it on every VM transition is heavier, and I'm not sure whether it could introduce a noticeable performance penalty.

However, whether the solution is adding PERF_PMU_CAP_MEDIATED_VPMU or some other mechanism to enforce mutual exclusion between pt_pmu and mediated vPMU, we can address that in a separate patch.

To immediately protect guest state and live migration in this series, I'm thinking of adding the following defensive filter in intel_mediated_pmu_put():

+ /*

Review

Quoted 5 lines
(quoted diff) +5 -0
+  * Scrub bits that are not virtualized for the guest to prevent
+  * possible host state from leaking into guest state and breaking
+  * live migration.
+  */
+  pmu->global_status &= ~pmu->global_status_rsvd;
Quoted 4 lines

This doesn't prevent the leakage, it just clears it on the guest->host switch. So it helps live migration but I am not sure if it's better or worse if the guest sees the bit unexpectedly appear AND then unexpectedly disappear.

Right, as long as the GLOBAL_STATUS is passthru, the leak can't be prevented.

Quoted 5 lines

We can clear it when the MSR is read by userspace, which should fix live migration, but the guest will still see the bit, and it will still disappear unexpectedly but only during live migration.

The guest still won't be able to clear it when OVF_CTRL is intercepted.

GLOBAL_STATUS and OVF_CTRL share the same interception policy. If they are passed through, the guest can see the bit and clear it; if both are intercepted, then bit 55 can't leak to the guest.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

It's not perfect, but at least the guest won't observe bit 55 and then get an unexpected #GP when attempting to clear it.

I'm not sure what imperfect solution you are referring to, but Yosry's proposal that the bit gets cleared on a guest->host switch is a non-starter. MSR bits must not vanish unpredictably.

I don't think I proposed this? :)

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Agree, then maybe Yosry's suggestion is what we can do for now: filter out unsupported bits when the MSR is read by userspace.

This I did propose, but I also noted that the MSR bit will still vanish unpredictable, just on save/restore instead of guest->host switch, which should be better.

Yes.

Quoted 13 lines
Quoted 13 lines
arch/x86/kvm/pmu.c +2 -0
index 38c7a4fa1007..3849b3f5234a 100644
@@ -862,6 +862,8 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct
 msr_data *msr_info)
        switch (msr) {
        case MSR_CORE_PERF_GLOBAL_STATUS:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+               if (msr_info->host_initiated)
+                       pmu->global_status &= ~pmu->global_status_rsvd;
 
Quoted 5 lines
Quoted 2 lines

The underlying issue still requires proper mutual exclusion between Intel PT and mediated vPMU, which I think can be addressed separately.

Or maybe handle the host PT while the guest is running? Not sure if that would be correct.

Since KVM switches APIC LVTPC in guest context, it seems difficult to correctly handle host PT PMIs while the guest is running.

I guess Intel PT virtualization could be re-enabled sooner or later, and addressing the exclusion issue would be a must at that point anyway.

#60 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
arch/x86/kvm/pmu.c +2 -0
index 38c7a4fa1007..3849b3f5234a 100644
@@ -862,6 +862,8 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct
 msr_data *msr_info)
        switch (msr) {
        case MSR_CORE_PERF_GLOBAL_STATUS:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+               if (msr_info->host_initiated)
+                       pmu->global_status &= ~pmu->global_status_rsvd;
 
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

The underlying issue still requires proper mutual exclusion between Intel PT and mediated vPMU, which I think can be addressed separately.

Or maybe handle the host PT while the guest is running? Not sure if that would be correct.

Since KVM switches APIC LVTPC in guest context, it seems difficult to correctly handle host PT PMIs while the guest is running.

One hacky way to do it is to make perf_guest_handle_mediated_pmi() handle host PT before calling into KVM. This doesn't completely stop the guest from seeing the bit tho. Also, I think we may have no way of finding out if we still need to inject a PMI into the guest for a different reason or not :/

Quoted 3 lines

I guess Intel PT virtualization could be re-enabled sooner or later, and addressing the exclusion issue would be a must at that point anyway.

author
#61 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 18 lines · Yosry Ahmed
Quoted 18 lines
Quoted 17 lines
Quoted 12 lines
arch/x86/kvm/pmu.c +2 -0
index 38c7a4fa1007..3849b3f5234a 100644
@@ -862,6 +862,8 @@ int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct
 msr_data *msr_info)
        switch (msr) {
        case MSR_CORE_PERF_GLOBAL_STATUS:
        case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS:
+               if (msr_info->host_initiated)
+                       pmu->global_status &= ~pmu->global_status_rsvd;
 
Quoted 2 lines

The underlying issue still requires proper mutual exclusion between Intel PT and mediated vPMU, which I think can be addressed separately.

Or maybe handle the host PT while the guest is running? Not sure if that would be correct.

Quoted 8 lines
Quoted 2 lines

Since KVM switches APIC LVTPC in guest context, it seems difficult to correctly handle host PT PMIs while the guest is running.

One hacky way to do it is to make perf_guest_handle_mediated_pmi() handle host PT before calling into KVM. This doesn't completely stop the guest from seeing the bit tho. Also, I think we may have no way of finding out if we still need to inject a PMI into the guest for a different reason or not :/

Right, and the host perf subsystem doesn't know which GLOBAL_STATUS bits it is supposed to handle.

The hardware-assisted perfmon_mask solution seems like a clean way to avoid this problem. :)

And I think if Intel PT virtualization is re-enabled at some point, addressing the mutual exclusion issue would probably be necessary regardless.

Quoted 3 lines
Quoted 3 lines

I guess Intel PT virtualization could be re-enabled sooner or later, and addressing the exclusion issue would be a must at that point anyway.

#62 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

In reply to Yosry Ahmed
Quoted 86 lines · Yosry Ahmed
Quoted 75 lines · Zide
Quoted 71 lines · Jim Mattson
Quoted 70 lines · Zide
Quoted 52 lines · Yosry Ahmed
Quoted 47 lines
Quoted 21 lines
Quoted 8 lines
Quoted 5 lines

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

Quoted 7 lines

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

Looking at the code:

kvm_mediated_pmu_load() -> perf_load_guest_context() -> perf_ctx_disable() will iterate PMUs with for_each_epc() and disable them, which skips PMUs without PERF_PMU_CAP_MEDIATED_VPMU.

Quoted 7 lines

Same for kvm_mediated_pmu_load() -> perf_load_guest_context() ->ctx_sched_out(), it also uses for_each_epc() to iterate PMUs and schedule them out.

pt_init() does not set PERF_PMU_CAP_MEDIATED_VPMU on pt_pmu.pmu.capabilities

Please correct me if I am wrong.

My apologies, this bug is indeed real because pt_pmu lacks the PERF_PMU_CAP_MEDIATED_VPMU capability.

Adding this capability to pt_pmu seems intuitive, but unlike

the core PMU (which merely toggles enable bits),
Intel PT is an AUX-based PMU. Stopping and restarting it on every VM transition is heavier, and I'm not sure whether it could introduce a noticeable performance penalty.

However, whether the solution is adding PERF_PMU_CAP_MEDIATED_VPMU or some other mechanism to enforce mutual exclusion between pt_pmu and mediated vPMU, we can address that in a separate patch.

To immediately protect guest state and live migration in this series, I'm thinking of adding the following defensive filter in intel_mediated_pmu_put():

+ /*

Review

Quoted 5 lines
(quoted diff) +5 -0
+  * Scrub bits that are not virtualized for the guest to prevent
+  * possible host state from leaking into guest state and breaking
+  * live migration.
+  */
+  pmu->global_status &= ~pmu->global_status_rsvd;
Quoted 4 lines

This doesn't prevent the leakage, it just clears it on the guest->host switch. So it helps live migration but I am not sure if it's better or worse if the guest sees the bit unexpectedly appear AND then unexpectedly disappear.

Right, as long as the GLOBAL_STATUS is passthru, the leak can't be prevented.

Quoted 5 lines

We can clear it when the MSR is read by userspace, which should fix live migration, but the guest will still see the bit, and it will still disappear unexpectedly but only during live migration.

The guest still won't be able to clear it when OVF_CTRL is intercepted.

GLOBAL_STATUS and OVF_CTRL share the same interception policy. If they are passed through, the guest can see the bit and clear it; if both are intercepted, then bit 55 can't leak to the guest.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

It's not perfect, but at least the guest won't observe bit 55 and then get an unexpected #GP when attempting to clear it.

I'm not sure what imperfect solution you are referring to, but Yosry's proposal that the bit gets cleared on a guest->host switch is a non-starter. MSR bits must not vanish unpredictably.

I don't think I proposed this? :)

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Agree, then maybe Yosry's suggestion is what we can do for now: filter out unsupported bits when the MSR is read by userspace.

This I did propose, but I also noted that the MSR bit will still vanish unpredictable, just on save/restore instead of guest->host switch, which should be better.

I will reject any proposal that involves MSR bits vanishing unpredictably.

author
#63 lore raw

Re: [PATCH v2 11/16] KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs

Quoted 87 lines · Jim Mattson
Quoted 86 lines · Yosry Ahmed
Quoted 75 lines · Zide
Quoted 71 lines · Jim Mattson
Quoted 70 lines · Zide
Quoted 52 lines · Yosry Ahmed
Quoted 47 lines
Quoted 21 lines
Quoted 8 lines
Quoted 5 lines

1. GLOBAL_STATUS_TRACE_TOPAPMI_BIT can leak from the host into the guest. If a host PT PMI arrives while the guest context is loaded, bit 55 will be set for the guest, and the guest won't be able to clear it if OVF_CTRL is intercepted (it will #GP as it is reserved for the guest). Similarly, the value can't be restored through KVM_SET_MSRS.

I think mediated vPMU assumes that no host PMIs can be generated while guest PMU state is loaded.

Quoted 7 lines

If PT state can leak into the guest via bit 55, similar leakage can also occur for other status bits, e.g. when the guest exposes fewer general-purpose or fixed counters than the host.

The scenario where both host and guest can be active simultaneously is handled by the perfmon_mask case, where intel_mediated_pmu_{load,put}() filter guest-owned status bits.

Looking at the code:

kvm_mediated_pmu_load() -> perf_load_guest_context() -> perf_ctx_disable() will iterate PMUs with for_each_epc() and disable them, which skips PMUs without PERF_PMU_CAP_MEDIATED_VPMU.

Quoted 7 lines

Same for kvm_mediated_pmu_load() -> perf_load_guest_context() ->ctx_sched_out(), it also uses for_each_epc() to iterate PMUs and schedule them out.

pt_init() does not set PERF_PMU_CAP_MEDIATED_VPMU on pt_pmu.pmu.capabilities

Please correct me if I am wrong.

My apologies, this bug is indeed real because pt_pmu lacks the PERF_PMU_CAP_MEDIATED_VPMU capability.

Adding this capability to pt_pmu seems intuitive, but unlike

the core PMU (which merely toggles enable bits),
Intel PT is an AUX-based PMU. Stopping and restarting it on every VM transition is heavier, and I'm not sure whether it could introduce a noticeable performance penalty.

However, whether the solution is adding PERF_PMU_CAP_MEDIATED_VPMU or some other mechanism to enforce mutual exclusion between pt_pmu and mediated vPMU, we can address that in a separate patch.

To immediately protect guest state and live migration in this series, I'm thinking of adding the following defensive filter in intel_mediated_pmu_put():

+ /*

Review

Quoted 5 lines
(quoted diff) +5 -0
+  * Scrub bits that are not virtualized for the guest to prevent
+  * possible host state from leaking into guest state and breaking
+  * live migration.
+  */
+  pmu->global_status &= ~pmu->global_status_rsvd;
Quoted 4 lines

This doesn't prevent the leakage, it just clears it on the guest->host switch. So it helps live migration but I am not sure if it's better or worse if the guest sees the bit unexpectedly appear AND then unexpectedly disappear.

Right, as long as the GLOBAL_STATUS is passthru, the leak can't be prevented.

Quoted 5 lines

We can clear it when the MSR is read by userspace, which should fix live migration, but the guest will still see the bit, and it will still disappear unexpectedly but only during live migration.

The guest still won't be able to clear it when OVF_CTRL is intercepted.

GLOBAL_STATUS and OVF_CTRL share the same interception policy. If they are passed through, the guest can see the bit and clear it; if both are intercepted, then bit 55 can't leak to the guest.

Quoted 5 lines
Quoted 5 lines
Quoted 2 lines

It's not perfect, but at least the guest won't observe bit 55 and then get an unexpected #GP when attempting to clear it.

I'm not sure what imperfect solution you are referring to, but Yosry's proposal that the bit gets cleared on a guest->host switch is a non-starter. MSR bits must not vanish unpredictably.

I don't think I proposed this? :)

Quoted 1 line
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

Agree, then maybe Yosry's suggestion is what we can do for now: filter out unsupported bits when the MSR is read by userspace.

This I did propose, but I also noted that the MSR bit will still vanish unpredictable, just on save/restore instead of guest->host switch, which should be better.

I will reject any proposal that involves MSR bits vanishing unpredictably.

In that case, perhaps the simplest approach is to leave this issue out of this series and fix the Intel PT mutual exclusion issue separately?

Also, once that issue is fixed, filtering out pmu->global_status_rsvd bits feels mostly defensive anyway, and I'm not sure it solves any additional problem by itself.

author
#64 lore raw

PATCH v2 12/16 KVM: x86/pmu: Populate CPUID.0AH:ECX fixed-counter bitmap

In reply to Zide Chen

PerfMon v5 adds a fixed-counter support bitmap in CPUID.0AH:ECX. This is the superset of EDX[4:0] which indicates the contiguous counters starting from 0. For backward compatibility, it's recommended to use the following to determine if a fixed counter is supported:

FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i);

---
 arch/x86/kvm/cpuid.c         | 5 ++++-
 arch/x86/kvm/vmx/pmu_intel.c | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +4 -1
index 106e719e7aee..63201d4ea4a1 100644
@@ -1547,7 +1547,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 
 		entry->eax = eax.full;
 		entry->ebx = kvm_pmu_cap.events_mask;
-		entry->ecx = 0;
+		if (kvm_pmu_cap.version >= 5)
+			entry->ecx &= (u32)kvm_pmu_cap.fixed_cntr_mask64;
+		else
+			entry->ecx = 0;
 		entry->edx = edx.full;
 		break;
 	}
arch/x86/kvm/vmx/pmu_intel.c +3 -0
index a6d2ebff7cc9..1ce69e764ae1 100644
@@ -585,7 +585,10 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 				      kvm_pmu_cap.events_mask_len);
 	pmu->available_event_types = ~entry->ebx & (BIT_ULL(eax.split.mask_length) - 1);
 
+	/* FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i) */
 	fixed_cntr_mask = BIT_ULL(edx.split.num_counters_fixed) - 1;
+	if (pmu->version >= 5)
+		fixed_cntr_mask |= entry->ecx;
 	fixed_cntr_mask &= kvm_pmu_cap.fixed_cntr_mask64;
 
 	/*
Signature · 1 line
2.55.0
Reviewed-by on v2 12/16
#65 lore raw

Re: [PATCH v2 12/16] KVM: x86/pmu: Populate CPUID.0AH:ECX fixed-counter bitmap

Quoted 45 lines · Zide Chen

PerfMon v5 adds a fixed-counter support bitmap in CPUID.0AH:ECX. This is the superset of EDX[4:0] which indicates the contiguous counters starting from 0. For backward compatibility, it's recommended to use the following to determine if a fixed counter is supported:

FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i);

---
 arch/x86/kvm/cpuid.c         | 5 ++++-
 arch/x86/kvm/vmx/pmu_intel.c | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +4 -1
index 106e719e7aee..63201d4ea4a1 100644
@@ -1547,7 +1547,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 
 		entry->eax = eax.full;
 		entry->ebx = kvm_pmu_cap.events_mask;
-		entry->ecx = 0;
+		if (kvm_pmu_cap.version >= 5)
+			entry->ecx &= (u32)kvm_pmu_cap.fixed_cntr_mask64;
+		else
+			entry->ecx = 0;
 		entry->edx = edx.full;
 		break;
 	}
arch/x86/kvm/vmx/pmu_intel.c +3 -0
index a6d2ebff7cc9..1ce69e764ae1 100644
@@ -585,7 +585,10 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 				      kvm_pmu_cap.events_mask_len);
 	pmu->available_event_types = ~entry->ebx & (BIT_ULL(eax.split.mask_length) - 1);
 
+	/* FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i) */
 	fixed_cntr_mask = BIT_ULL(edx.split.num_counters_fixed) - 1;
+	if (pmu->version >= 5)
+		fixed_cntr_mask |= entry->ecx;
 	fixed_cntr_mask &= kvm_pmu_cap.fixed_cntr_mask64;
 
 	/*
#66 lore raw

Re: [PATCH v2 12/16] KVM: x86/pmu: Populate CPUID.0AH:ECX fixed-counter bitmap

In reply to Zide Chen
Quoted 20 lines · Zide Chen

PerfMon v5 adds a fixed-counter support bitmap in CPUID.0AH:ECX. This is the superset of EDX[4:0] which indicates the contiguous counters starting from 0. For backward compatibility, it's recommended to use the following to determine if a fixed counter is supported:

FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i);

---
 arch/x86/kvm/cpuid.c         | 5 ++++-
 arch/x86/kvm/vmx/pmu_intel.c | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +0 -0
index 106e719e7aee..63201d4ea4a1 100644

arch/x86/kvm/cpuid.c static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) · line 1551

Quoted 6 lines
arch/x86/kvm/cpuid.c +2 -1
@@ -1547,7 +1547,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 
                entry->eax = eax.full;
                entry->ebx = kvm_pmu_cap.events_mask;
-               entry->ecx = 0;
+               if (kvm_pmu_cap.version >= 5)
+                       entry->ecx &= (u32)kvm_pmu_cap.fixed_cntr_mask64;

Is the '&' necessary?

author
#67 lore raw

Re: [PATCH v2 12/16] KVM: x86/pmu: Populate CPUID.0AH:ECX fixed-counter bitmap

Quoted 22 lines · Jim Mattson
Quoted 21 lines · Zide Chen

PerfMon v5 adds a fixed-counter support bitmap in CPUID.0AH:ECX. This is the superset of EDX[4:0] which indicates the contiguous counters starting from 0. For backward compatibility, it's recommended to use the following to determine if a fixed counter is supported:

FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i);

---
 arch/x86/kvm/cpuid.c         | 5 ++++-
 arch/x86/kvm/vmx/pmu_intel.c | 3 +++
 2 files changed, 7 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +0 -0
index 106e719e7aee..63201d4ea4a1 100644
@@ -1547,7 +1547,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 
Quoted 7 lines

arch/x86/kvm/cpuid.c static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function) · line 1551

Quoted 5 lines
arch/x86/kvm/cpuid.c +2 -1
                entry->eax = eax.full;
                entry->ebx = kvm_pmu_cap.events_mask;
-               entry->ecx = 0;
+               if (kvm_pmu_cap.version >= 5)
+                       entry->ecx &= (u32)kvm_pmu_cap.fixed_cntr_mask64;

Is the '&' necessary?

You are right. kvm_pmu_cap.fixed_cntr_mask64 already reflects KVM's supported fixed counters, so we can directly assign entry->ecx without the & (matching how eax, ebx, and edx are populated). I will drop it in v3.

author
#68 lore raw

PATCH v2 13/16 KVM: x86/pmu: Factor out fixed counter control bit calculation

In reply to Zide Chen

Rename intel_pmu_enable_fixed_counter_bits() to intel_pmu_fixed_ctrl_bits() and have it return the computed fixed counter control mask instead of updating fixed_ctr_ctrl_rsvd directly.

Accumulate the desired fixed counter control bits during PMU setup and apply them once in intel_pmu_refresh().

Move the helper next to intel_pmu_set_msr() so it can also be reused there, which will be needed for upcoming ANYTHREAD_DEPRECATION emulation.

No functional change intended.

---
v2: New patch

---
 arch/x86/kvm/vmx/pmu_intel.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +19 -13
index 1ce69e764ae1..c7472778137a 100644
@@ -421,6 +421,18 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	return 0;
 }
 
+static u64 intel_pmu_fixed_ctrl_bits(struct kvm_pmu *pmu, u64 bits)
+{
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	u64 fixed_ctrl_bits = 0;
+	int i;
+
+	kvm_for_each_fixed_counter(i, fixed_mask)
+		fixed_ctrl_bits |= intel_fixed_bits_by_idx(i, bits);
+
+	return fixed_ctrl_bits;
+}
+
 static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -533,20 +545,12 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)
 	return eventsel;
 }
 
-static void intel_pmu_enable_fixed_counter_bits(struct kvm_pmu *pmu, u64 bits)
-{
-	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
-	int i;
-
-	kvm_for_each_fixed_counter(i, fixed_mask)
-		pmu->fixed_ctr_ctrl_rsvd &= ~intel_fixed_bits_by_idx(i, bits);
-}
-
 static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
 	struct kvm_cpuid_entry2 *entry;
+	u64 fixed_enable_bits = 0;
 	union cpuid10_eax eax;
 	union cpuid10_edx edx;
 	u64 perf_capabilities;
@@ -625,9 +629,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 					  kvm_pmu_cap.bit_width_fixed);
 	pmu->counter_bitmask[KVM_PMC_FIXED] = BIT_ULL(edx.split.bit_width_fixed) - 1;
 
-	intel_pmu_enable_fixed_counter_bits(pmu, INTEL_FIXED_0_KERNEL |
-						 INTEL_FIXED_0_USER |
-						 INTEL_FIXED_0_ENABLE_PMI);
+	fixed_enable_bits |= INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER |
+			     INTEL_FIXED_0_ENABLE_PMI;
 
 	counter_rsvd = ~(kvm_gp_pmc_mask(pmu) |
 			((u64)kvm_fixed_pmc_mask(pmu) << KVM_FIXED_PMC_BASE_IDX));
@@ -651,11 +654,14 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 			pmu->pebs_enable_rsvd = counter_rsvd;
 			pmu->eventsel_rsvd &= ~ICL_EVENTSEL_ADAPTIVE;
 			pmu->pebs_data_cfg_rsvd = ~0xff00000full;
-			intel_pmu_enable_fixed_counter_bits(pmu, ICL_FIXED_0_ADAPTIVE);
+			fixed_enable_bits |= ICL_FIXED_0_ADAPTIVE;
 		} else {
 			pmu->pebs_enable_rsvd = ~kvm_gp_pmc_mask(pmu);
 		}
 	}
+
+	pmu->fixed_ctr_ctrl_rsvd &=
+		~intel_pmu_fixed_ctrl_bits(pmu, fixed_enable_bits);
 }
 
 static void intel_pmu_init(struct kvm_vcpu *vcpu)
Signature · 1 line
2.55.0
Reviewed-by on v2 13/16
#69 lore raw

Re: [PATCH v2 13/16] KVM: x86/pmu: Factor out fixed counter control bit calculation

Quoted 93 lines · Zide Chen

Rename intel_pmu_enable_fixed_counter_bits() to intel_pmu_fixed_ctrl_bits() and have it return the computed fixed counter control mask instead of updating fixed_ctr_ctrl_rsvd directly.

Accumulate the desired fixed counter control bits during PMU setup and apply them once in intel_pmu_refresh().

Move the helper next to intel_pmu_set_msr() so it can also be reused there, which will be needed for upcoming ANYTHREAD_DEPRECATION emulation.

No functional change intended.

---
v2: New patch

---
 arch/x86/kvm/vmx/pmu_intel.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)
arch/x86/kvm/vmx/pmu_intel.c +19 -13
index 1ce69e764ae1..c7472778137a 100644
@@ -421,6 +421,18 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 	return 0;
 }
 
+static u64 intel_pmu_fixed_ctrl_bits(struct kvm_pmu *pmu, u64 bits)
+{
+	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
+	u64 fixed_ctrl_bits = 0;
+	int i;
+
+	kvm_for_each_fixed_counter(i, fixed_mask)
+		fixed_ctrl_bits |= intel_fixed_bits_by_idx(i, bits);
+
+	return fixed_ctrl_bits;
+}
+
 static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
@@ -533,20 +545,12 @@ static __always_inline u64 intel_get_fixed_pmc_eventsel(unsigned int index)
 	return eventsel;
 }
 
-static void intel_pmu_enable_fixed_counter_bits(struct kvm_pmu *pmu, u64 bits)
-{
-	unsigned long fixed_mask = kvm_fixed_pmc_mask(pmu);
-	int i;
-
-	kvm_for_each_fixed_counter(i, fixed_mask)
-		pmu->fixed_ctr_ctrl_rsvd &= ~intel_fixed_bits_by_idx(i, bits);
-}
-
 static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
 	struct kvm_cpuid_entry2 *entry;
+	u64 fixed_enable_bits = 0;
 	union cpuid10_eax eax;
 	union cpuid10_edx edx;
 	u64 perf_capabilities;
@@ -625,9 +629,8 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 					  kvm_pmu_cap.bit_width_fixed);
 	pmu->counter_bitmask[KVM_PMC_FIXED] = BIT_ULL(edx.split.bit_width_fixed) - 1;
 
-	intel_pmu_enable_fixed_counter_bits(pmu, INTEL_FIXED_0_KERNEL |
-						 INTEL_FIXED_0_USER |
-						 INTEL_FIXED_0_ENABLE_PMI);
+	fixed_enable_bits |= INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER |
+			     INTEL_FIXED_0_ENABLE_PMI;
 
 	counter_rsvd = ~(kvm_gp_pmc_mask(pmu) |
 			((u64)kvm_fixed_pmc_mask(pmu) << KVM_FIXED_PMC_BASE_IDX));
@@ -651,11 +654,14 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 			pmu->pebs_enable_rsvd = counter_rsvd;
 			pmu->eventsel_rsvd &= ~ICL_EVENTSEL_ADAPTIVE;
 			pmu->pebs_data_cfg_rsvd = ~0xff00000full;
-			intel_pmu_enable_fixed_counter_bits(pmu, ICL_FIXED_0_ADAPTIVE);
+			fixed_enable_bits |= ICL_FIXED_0_ADAPTIVE;
 		} else {
 			pmu->pebs_enable_rsvd = ~kvm_gp_pmc_mask(pmu);
 		}
 	}
+
+	pmu->fixed_ctr_ctrl_rsvd &=
+		~intel_pmu_fixed_ctrl_bits(pmu, fixed_enable_bits);
 }
 
 static void intel_pmu_init(struct kvm_vcpu *vcpu)
Reviewed-by on v2 13/16
#70 lore raw

Re: [PATCH v2 13/16] KVM: x86/pmu: Factor out fixed counter control bit calculation

In reply to Zide Chen
Quoted 8 lines · Zide Chen

Rename intel_pmu_enable_fixed_counter_bits() to intel_pmu_fixed_ctrl_bits() and have it return the computed fixed counter control mask instead of updating fixed_ctr_ctrl_rsvd directly.

Accumulate the desired fixed counter control bits during PMU setup and apply them once in intel_pmu_refresh().

Quoted 7 lines

Move the helper next to intel_pmu_set_msr() so it can also be reused there, which will be needed for upcoming ANYTHREAD_DEPRECATION emulation.

No functional change intended.

author
#71 lore raw

PATCH v2 14/16 KVM: x86/pmu: Ignore AnyThread bit if CPUID.0AH:EDX[15] is set

In reply to Zide Chen

Intel PerfMon v5 introduces the ANYTHREAD_DEPRECATION capability (CPUID.0AH:EDX[15]) to indicate that AnyThread counting is deprecated and that writes to the AnyThread bit in IA32_PERFEVTSELx are ignored.

When ANYTHREAD_DEPRECATION is present in the guest CPUID, emulate the architectural behavior and silently ignore writes to the AnyThread bit instead of injecting #GP.

Continue to inject #GP when ANYTHREAD_DEPRECATION is not present in the guest CPUID, e.g. for PerfMon v3/v4 guests or when the capability is not configured for a PerfMon v5+ guest.

Advertise CPUID.0AH:EDX[15] only for PMU version 5+ guests, since the bit doesn't exist below version 5.

---
v2:
- Advertise anythread_deprecated only if kvm_pmu_cap.version >= 5.
- Add the missing AnyThr bit handling for IA32_FIXED_CTR_CTRL.
- Update the comments to be clearer.

---
 arch/x86/kvm/cpuid.c         |  2 +-
 arch/x86/kvm/vmx/pmu_intel.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
index 63201d4ea4a1..ef208906b3ee 100644
@@ -1542,7 +1542,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 					    KVM_MAX_NR_FIXED_COUNTERS);
 		edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;
 
-		if (kvm_pmu_cap.version)
+		if (kvm_pmu_cap.version >= 5)
 			edx.split.anythread_deprecated = 1;
 
 		entry->eax = eax.full;
arch/x86/kvm/vmx/pmu_intel.c +19 -0
index c7472778137a..599c69dfa6e3 100644
@@ -446,6 +446,12 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (data & pmu->fixed_ctr_ctrl_rsvd)
 			return 1;
 
+		/*
+		 * This is to emulate ANYTHREAD_DEPRECATION. Guests without
+		 * support for it would have had the write rejected by
+		 * pmu->fixed_ctr_ctrl_rsvd above.
+		 */
+		data &= ~intel_pmu_fixed_ctrl_bits(pmu, INTEL_FIXED_0_ANYTHREAD);
 		if (pmu->fixed_ctr_ctrl != data)
 			reprogram_fixed_counters(pmu, data);
 		break;
@@ -494,6 +500,8 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			if (data & eventsel_rsvd)
 				return 1;
 
+			/* See above comments for MSR_CORE_PERF_FIXED_CTR_CTRL. */
+			data &= ~ARCH_PERFMON_EVENTSEL_ANY;
 			if (data != pmc->eventsel) {
 				pmc->eventsel = data;
 				pmc->eventsel_hw = data;
@@ -603,6 +611,17 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
 			    kvm_pmu_cap.cntr_mask64;
 
+	/*
+	 * AnyThread counting is not supported by KVM due to cross-VM
+	 * information leakage concerns on SMT cores. Therefore, AnyThread
+	 * remains unavailable for PerfMon v3/v4 guests, where AnyThread
+	 * deprecation is not enumerated.
+	 */
+	if (pmu->version >= 5 && edx.split.anythread_deprecated) {
+		pmu->eventsel_rsvd &= ~ARCH_PERFMON_EVENTSEL_ANY;
+		fixed_enable_bits |= INTEL_FIXED_0_ANYTHREAD;
+	}
+
 	entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
 	if (entry &&
 	    (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
Signature · 1 line
2.55.0
Reviewed-by on v2 14/16
#72 lore raw

Re: [PATCH v2 14/16] KVM: x86/pmu: Ignore AnyThread bit if CPUID.0AH:EDX[15] is set

Quoted 83 lines · Zide Chen

Intel PerfMon v5 introduces the ANYTHREAD_DEPRECATION capability (CPUID.0AH:EDX[15]) to indicate that AnyThread counting is deprecated and that writes to the AnyThread bit in IA32_PERFEVTSELx are ignored.

When ANYTHREAD_DEPRECATION is present in the guest CPUID, emulate the architectural behavior and silently ignore writes to the AnyThread bit instead of injecting #GP.

Continue to inject #GP when ANYTHREAD_DEPRECATION is not present in the guest CPUID, e.g. for PerfMon v3/v4 guests or when the capability is not configured for a PerfMon v5+ guest.

Advertise CPUID.0AH:EDX[15] only for PMU version 5+ guests, since the bit doesn't exist below version 5.

---
v2:
- Advertise anythread_deprecated only if kvm_pmu_cap.version >= 5.
- Add the missing AnyThr bit handling for IA32_FIXED_CTR_CTRL.
- Update the comments to be clearer.

---
 arch/x86/kvm/cpuid.c         |  2 +-
 arch/x86/kvm/vmx/pmu_intel.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
index 63201d4ea4a1..ef208906b3ee 100644
@@ -1542,7 +1542,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
 					    KVM_MAX_NR_FIXED_COUNTERS);
 		edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;
 
-		if (kvm_pmu_cap.version)
+		if (kvm_pmu_cap.version >= 5)
 			edx.split.anythread_deprecated = 1;
 
 		entry->eax = eax.full;
arch/x86/kvm/vmx/pmu_intel.c +19 -0
index c7472778137a..599c69dfa6e3 100644
@@ -446,6 +446,12 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 		if (data & pmu->fixed_ctr_ctrl_rsvd)
 			return 1;
 
+		/*
+		 * This is to emulate ANYTHREAD_DEPRECATION. Guests without
+		 * support for it would have had the write rejected by
+		 * pmu->fixed_ctr_ctrl_rsvd above.
+		 */
+		data &= ~intel_pmu_fixed_ctrl_bits(pmu, INTEL_FIXED_0_ANYTHREAD);
 		if (pmu->fixed_ctr_ctrl != data)
 			reprogram_fixed_counters(pmu, data);
 		break;
@@ -494,6 +500,8 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
 			if (data & eventsel_rsvd)
 				return 1;
 
+			/* See above comments for MSR_CORE_PERF_FIXED_CTR_CTRL. */
+			data &= ~ARCH_PERFMON_EVENTSEL_ANY;
 			if (data != pmc->eventsel) {
 				pmc->eventsel = data;
 				pmc->eventsel_hw = data;
@@ -603,6 +611,17 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
 	pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
 			    kvm_pmu_cap.cntr_mask64;
 
+	/*
+	 * AnyThread counting is not supported by KVM due to cross-VM
+	 * information leakage concerns on SMT cores. Therefore, AnyThread
+	 * remains unavailable for PerfMon v3/v4 guests, where AnyThread
+	 * deprecation is not enumerated.
+	 */
+	if (pmu->version >= 5 && edx.split.anythread_deprecated) {
+		pmu->eventsel_rsvd &= ~ARCH_PERFMON_EVENTSEL_ANY;
+		fixed_enable_bits |= INTEL_FIXED_0_ANYTHREAD;
+	}
+
 	entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
 	if (entry &&
 	    (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
#73 lore raw

Re: [PATCH v2 14/16] KVM: x86/pmu: Ignore AnyThread bit if CPUID.0AH:EDX[15] is set

In reply to Zide Chen
Quoted 71 lines · Zide Chen

Intel PerfMon v5 introduces the ANYTHREAD_DEPRECATION capability (CPUID.0AH:EDX[15]) to indicate that AnyThread counting is deprecated and that writes to the AnyThread bit in IA32_PERFEVTSELx are ignored.

When ANYTHREAD_DEPRECATION is present in the guest CPUID, emulate the architectural behavior and silently ignore writes to the AnyThread bit instead of injecting #GP.

Continue to inject #GP when ANYTHREAD_DEPRECATION is not present in the guest CPUID, e.g. for PerfMon v3/v4 guests or when the capability is not configured for a PerfMon v5+ guest.

Advertise CPUID.0AH:EDX[15] only for PMU version 5+ guests, since the bit doesn't exist below version 5.

---
v2:
- Advertise anythread_deprecated only if kvm_pmu_cap.version >= 5.
- Add the missing AnyThr bit handling for IA32_FIXED_CTR_CTRL.
- Update the comments to be clearer.

---
 arch/x86/kvm/cpuid.c         |  2 +-
 arch/x86/kvm/vmx/pmu_intel.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
index 63201d4ea4a1..ef208906b3ee 100644
@@ -1542,7 +1542,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
                                            KVM_MAX_NR_FIXED_COUNTERS);
                edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;
 
-               if (kvm_pmu_cap.version)
+               if (kvm_pmu_cap.version >= 5)
                        edx.split.anythread_deprecated = 1;
 
                entry->eax = eax.full;
arch/x86/kvm/vmx/pmu_intel.c +9 -0
index c7472778137a..599c69dfa6e3 100644
@@ -446,6 +446,12 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                if (data & pmu->fixed_ctr_ctrl_rsvd)
                        return 1;
 
+               /*
+                * This is to emulate ANYTHREAD_DEPRECATION. Guests without
+                * support for it would have had the write rejected by
+                * pmu->fixed_ctr_ctrl_rsvd above.
+                */
+               data &= ~intel_pmu_fixed_ctrl_bits(pmu, INTEL_FIXED_0_ANYTHREAD);
                if (pmu->fixed_ctr_ctrl != data)
                        reprogram_fixed_counters(pmu, data);
                break;
@@ -494,6 +500,8 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                        if (data & eventsel_rsvd)
                                return 1;
 
+                       /* See above comments for MSR_CORE_PERF_FIXED_CTR_CTRL. */
+                       data &= ~ARCH_PERFMON_EVENTSEL_ANY;
                        if (data != pmc->eventsel) {
                                pmc->eventsel = data;
                                pmc->eventsel_hw = data;
@@ -603,6 +611,17 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
        pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
                            kvm_pmu_cap.cntr_mask64;
 
+       /*

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

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +10 -0
+        * AnyThread counting is not supported by KVM due to cross-VM
+        * information leakage concerns on SMT cores. Therefore, AnyThread
+        * remains unavailable for PerfMon v3/v4 guests, where AnyThread
+        * deprecation is not enumerated.
+        */
+       if (pmu->version >= 5 && edx.split.anythread_deprecated) {
+               pmu->eventsel_rsvd &= ~ARCH_PERFMON_EVENTSEL_ANY;
+               fixed_enable_bits |= INTEL_FIXED_0_ANYTHREAD;
+       }
+

According to Intel SDM Vol. 4, Table 2-2 ("IA-32 Architectural MSRs"), only Fixed Counters 0, 1, and 2 have an AnyThread bit (bits 2, 6, and 10 of IA32_FIXED_CTR_CTRL). FC3 has never had an AnyThread bit; IA32_FIXED_CTR_CTR[bit 14] is reserved in PerfMon v5 and becomes METRICS_CLEAR_EN in PerfMon v6. Passing INTEL_FIXED_0_ANYTHREAD to intel_pmu_fixed_ctrl_bits() will wrongly clear bit 14 in pmu->fixed_ctr_ctrl_rsvd once Fixed Counter 3 is enabled.

On hardware with CPUID.0AH:EDX.ANYTHREAD_DEPRECATION[15] = 1, if software sets the AnyThread bit in IA32_PERFEVTSELx or IA32_FIXED_CTR_CTRL and then reads the MSR back with RDMSR, does hardware return 0 for the AnyThread bit, or does it return 1 but otherwise ignore the bit?

author
#74 lore raw

Re: [PATCH v2 14/16] KVM: x86/pmu: Ignore AnyThread bit if CPUID.0AH:EDX[15] is set

Quoted 83 lines · Jim Mattson
Quoted 71 lines · Zide Chen

Intel PerfMon v5 introduces the ANYTHREAD_DEPRECATION capability (CPUID.0AH:EDX[15]) to indicate that AnyThread counting is deprecated and that writes to the AnyThread bit in IA32_PERFEVTSELx are ignored.

When ANYTHREAD_DEPRECATION is present in the guest CPUID, emulate the architectural behavior and silently ignore writes to the AnyThread bit instead of injecting #GP.

Continue to inject #GP when ANYTHREAD_DEPRECATION is not present in the guest CPUID, e.g. for PerfMon v3/v4 guests or when the capability is not configured for a PerfMon v5+ guest.

Advertise CPUID.0AH:EDX[15] only for PMU version 5+ guests, since the bit doesn't exist below version 5.

---
v2:
- Advertise anythread_deprecated only if kvm_pmu_cap.version >= 5.
- Add the missing AnyThr bit handling for IA32_FIXED_CTR_CTRL.
- Update the comments to be clearer.

---
 arch/x86/kvm/cpuid.c         |  2 +-
 arch/x86/kvm/vmx/pmu_intel.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+), 1 deletion(-)
arch/x86/kvm/cpuid.c +1 -1
index 63201d4ea4a1..ef208906b3ee 100644
@@ -1542,7 +1542,7 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
                                            KVM_MAX_NR_FIXED_COUNTERS);
                edx.split.bit_width_fixed = kvm_pmu_cap.bit_width_fixed;
 
-               if (kvm_pmu_cap.version)
+               if (kvm_pmu_cap.version >= 5)
                        edx.split.anythread_deprecated = 1;
 
                entry->eax = eax.full;
arch/x86/kvm/vmx/pmu_intel.c +9 -0
index c7472778137a..599c69dfa6e3 100644
@@ -446,6 +446,12 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                if (data & pmu->fixed_ctr_ctrl_rsvd)
                        return 1;
 
+               /*
+                * This is to emulate ANYTHREAD_DEPRECATION. Guests without
+                * support for it would have had the write rejected by
+                * pmu->fixed_ctr_ctrl_rsvd above.
+                */
+               data &= ~intel_pmu_fixed_ctrl_bits(pmu, INTEL_FIXED_0_ANYTHREAD);
                if (pmu->fixed_ctr_ctrl != data)
                        reprogram_fixed_counters(pmu, data);
                break;
@@ -494,6 +500,8 @@ static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
                        if (data & eventsel_rsvd)
                                return 1;
 
+                       /* See above comments for MSR_CORE_PERF_FIXED_CTR_CTRL. */
+                       data &= ~ARCH_PERFMON_EVENTSEL_ANY;
                        if (data != pmc->eventsel) {
                                pmc->eventsel = data;
                                pmc->eventsel_hw = data;
@@ -603,6 +611,17 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
        pmu->pmc_exists64 = (BIT_ULL(nr_gp_counters) - 1) &
                            kvm_pmu_cap.cntr_mask64;
 
+       /*

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

Quoted 10 lines
arch/x86/kvm/vmx/pmu_intel.c +10 -0
+        * AnyThread counting is not supported by KVM due to cross-VM
+        * information leakage concerns on SMT cores. Therefore, AnyThread
+        * remains unavailable for PerfMon v3/v4 guests, where AnyThread
+        * deprecation is not enumerated.
+        */
+       if (pmu->version >= 5 && edx.split.anythread_deprecated) {
+               pmu->eventsel_rsvd &= ~ARCH_PERFMON_EVENTSEL_ANY;
+               fixed_enable_bits |= INTEL_FIXED_0_ANYTHREAD;
+       }
+
Quoted 7 lines

According to Intel SDM Vol. 4, Table 2-2 ("IA-32 Architectural MSRs"), only Fixed Counters 0, 1, and 2 have an AnyThread bit (bits 2, 6, and 10 of IA32_FIXED_CTR_CTRL). FC3 has never had an AnyThread bit; IA32_FIXED_CTR_CTR[bit 14] is reserved in PerfMon v5 and becomes METRICS_CLEAR_EN in PerfMon v6. Passing INTEL_FIXED_0_ANYTHREAD to intel_pmu_fixed_ctrl_bits() will wrongly clear bit 14 in pmu->fixed_ctr_ctrl_rsvd once Fixed Counter 3 is enabled.

Right, the IA32_FIXED_CTR_CTR[bit 14] should be handled differently. Thanks!

Quoted 4 lines

On hardware with CPUID.0AH:EDX.ANYTHREAD_DEPRECATION[15] = 1, if software sets the AnyThread bit in IA32_PERFEVTSELx or IA32_FIXED_CTR_CTRL and then reads the MSR back with RDMSR, does hardware return 0 for the AnyThread bit, or does it return 1 but otherwise ignore the bit?

It returns 1. That said, the current implementation, which clears the AnyThread bit in intel_pmu_set_msr(), breaks this behavior.

To make matters more subtle, in the case of an invalid configuration where ANYTHREAD_DEPRECATION is set in the guest CPUID while deprecation is not supported by the host (Ice Lake, for instance), it is better for KVM not to pass the AnyThread bits to the host hardware and expect them to be ignored by the host.

Is the complete solution to keep the AnyThread bits set in the guest-visible states (pmc->eventsel and pmu->fixed_ctr_ctrl) for RDMSR readback, but drop them from the hardware-visible states (their _hw counterparts)?

#75 lore raw

Re: [PATCH v2 14/16] KVM: x86/pmu: Ignore AnyThread bit if CPUID.0AH:EDX[15] is set

Quoted 16 lines · Zide
Quoted 4 lines · Jim Mattson

On hardware with CPUID.0AH:EDX.ANYTHREAD_DEPRECATION[15] = 1, if software sets the AnyThread bit in IA32_PERFEVTSELx or IA32_FIXED_CTR_CTRL and then reads the MSR back with RDMSR, does hardware return 0 for the AnyThread bit, or does it return 1 but otherwise ignore the bit?

It returns 1. That said, the current implementation, which clears the AnyThread bit in intel_pmu_set_msr(), breaks this behavior.

To make matters more subtle, in the case of an invalid configuration where ANYTHREAD_DEPRECATION is set in the guest CPUID while deprecation is not supported by the host (Ice Lake, for instance), it is better for KVM not to pass the AnyThread bits to the host hardware and expect them to be ignored by the host.

Quoted 4 lines

Is the complete solution to keep the AnyThread bits set in the guest-visible states (pmc->eventsel and pmu->fixed_ctr_ctrl) for RDMSR readback, but drop them from the hardware-visible states (their _hw counterparts)?

That makes sense to me.

author
#76 lore raw

PATCH v2 15/16 KVM: x86/pmu: Advertise PerfMon version 5 on Intel hosts

In reply to Zide Chen

From: Dapeng Mi <[email protected]>

KVM currently caps the guest PerfMon version at 2 for all Intel platforms. Now that KVM emulates the basic architectural PMU features introduced in PerfMon versions 3 through 5, raise the guest PerfMon version to 5. Features that require additional emulation support, e.g. architectural LBR will be enabled separately.

PerfMon versions 3 and 4 continue to be exposed as version 2 because KVM cannot safely virtualize AnyThread due to cross-VM information leakage. PerfMon v5 is supported because it provides the AnyThread-deprecated capability, allowing KVM to indicate that AnyThread is deprecated.

When enable_mediated_pmu is disabled or in the non-Intel paths, KVM retains the existing cap of version 2.

---
v2:
- Cap vPMU v3/v4 to v2, as AnyThread cannot be safely virtualized. (Sashiko)

---
 arch/x86/kvm/pmu.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
arch/x86/kvm/pmu.c +15 -1
index 4cb7c796daac..1a7be7d73b62 100644
@@ -175,7 +175,21 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
 	}
 
 	memcpy(&kvm_pmu_cap, &kvm_host_pmu, sizeof(kvm_host_pmu));
-	kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
+
+	if (is_intel && enable_mediated_pmu) {
+		kvm_pmu_cap.version = min(kvm_pmu_cap.version, 5);
+
+		/*
+		 * PMU v3/v4 has AnyThread support, which KVM cannot safely
+		 * virtualize. Expose PMU v2 instead of advertising a partially
+		 * functional PMU v3/v4 to guests.
+		 */
+		if (kvm_pmu_cap.version == 3 || kvm_pmu_cap.version == 4)
+			kvm_pmu_cap.version = 2;
+	} else {
+		kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
+	}
+
 	kvm_pmu_cap.cntr_mask64 &=
 		GENMASK_ULL(pmu_ops->MAX_NR_GP_COUNTERS - 1, 0);
 	kvm_pmu_cap.fixed_cntr_mask64 &=
Signature · 1 line
2.55.0
#77 lore raw

Re: [PATCH v2 15/16] KVM: x86/pmu: Advertise PerfMon version 5 on Intel hosts

Quoted 38 lines · Zide Chen

From: Dapeng Mi <[email protected]>

KVM currently caps the guest PerfMon version at 2 for all Intel platforms. Now that KVM emulates the basic architectural PMU features introduced in PerfMon versions 3 through 5, raise the guest PerfMon version to 5. Features that require additional emulation support, e.g. architectural LBR will be enabled separately.

PerfMon versions 3 and 4 continue to be exposed as version 2 because KVM cannot safely virtualize AnyThread due to cross-VM information leakage. PerfMon v5 is supported because it provides the AnyThread-deprecated capability, allowing KVM to indicate that AnyThread is deprecated.

When enable_mediated_pmu is disabled or in the non-Intel paths, KVM retains the existing cap of version 2.

---
v2:
- Cap vPMU v3/v4 to v2, as AnyThread cannot be safely virtualized. (Sashiko)

---
 arch/x86/kvm/pmu.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
arch/x86/kvm/pmu.c +1 -1
index 4cb7c796daac..1a7be7d73b62 100644
@@ -175,7 +175,21 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
        }
 
        memcpy(&kvm_pmu_cap, &kvm_host_pmu, sizeof(kvm_host_pmu));
-       kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
+

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

Quoted 10 lines
arch/x86/kvm/pmu.c +10 -0
+       if (is_intel && enable_mediated_pmu) {
+               kvm_pmu_cap.version = min(kvm_pmu_cap.version, 5);
+
+               /*
+                * PMU v3/v4 has AnyThread support, which KVM cannot safely
+                * virtualize. Expose PMU v2 instead of advertising a partially
+                * functional PMU v3/v4 to guests.
+                */
+               if (kvm_pmu_cap.version == 3 || kvm_pmu_cap.version == 4)
+                       kvm_pmu_cap.version = 2;

Nit: on Intel, enable_mediated_pmu already requires host_pmu->version >= 4 (see intel_is_mediated_pmu_supported()), so kvm_pmu_cap.version == 3 is impossible when (is_intel && enable_mediated_pmu) is true. Hence, I think this can be simplified to:

        if (is_intel && enable_mediated_pmu && kvm_pmu_cap.version >= 5)
                kvm_pmu_cap.version = 5;
        else
                kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
author
#78 lore raw

Re: [PATCH v2 15/16] KVM: x86/pmu: Advertise PerfMon version 5 on Intel hosts

Quoted 55 lines · Jim Mattson
Quoted 38 lines · Zide Chen

From: Dapeng Mi <[email protected]>

KVM currently caps the guest PerfMon version at 2 for all Intel platforms. Now that KVM emulates the basic architectural PMU features introduced in PerfMon versions 3 through 5, raise the guest PerfMon version to 5. Features that require additional emulation support, e.g. architectural LBR will be enabled separately.

PerfMon versions 3 and 4 continue to be exposed as version 2 because KVM cannot safely virtualize AnyThread due to cross-VM information leakage. PerfMon v5 is supported because it provides the AnyThread-deprecated capability, allowing KVM to indicate that AnyThread is deprecated.

When enable_mediated_pmu is disabled or in the non-Intel paths, KVM retains the existing cap of version 2.

---
v2:
- Cap vPMU v3/v4 to v2, as AnyThread cannot be safely virtualized. (Sashiko)

---
 arch/x86/kvm/pmu.c | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)
arch/x86/kvm/pmu.c +1 -1
index 4cb7c796daac..1a7be7d73b62 100644
@@ -175,7 +175,21 @@ void kvm_init_pmu_capability(struct kvm_pmu_ops *pmu_ops)
        }
 
        memcpy(&kvm_pmu_cap, &kvm_host_pmu, sizeof(kvm_host_pmu));
-       kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);
+

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

Quoted 10 lines
arch/x86/kvm/pmu.c +10 -0
+       if (is_intel && enable_mediated_pmu) {
+               kvm_pmu_cap.version = min(kvm_pmu_cap.version, 5);
+
+               /*
+                * PMU v3/v4 has AnyThread support, which KVM cannot safely
+                * virtualize. Expose PMU v2 instead of advertising a partially
+                * functional PMU v3/v4 to guests.
+                */
+               if (kvm_pmu_cap.version == 3 || kvm_pmu_cap.version == 4)
+                       kvm_pmu_cap.version = 2;

Nit: on Intel, enable_mediated_pmu already requires host_pmu->version >= 4 (see intel_is_mediated_pmu_supported()), so kvm_pmu_cap.version == 3 is impossible when (is_intel && enable_mediated_pmu) is true. Hence, I think this can be simplified to:

Quoted 4 lines
        if (is_intel && enable_mediated_pmu && kvm_pmu_cap.version >= 5)
                kvm_pmu_cap.version = 5;
        else
                kvm_pmu_cap.version = min(kvm_pmu_cap.version, 2);

Nice simplification. You are right that version == 3 is dead code. I will adopt this simplified code in v3.

author
#79 lore raw

PATCH v2 16/16 KVM: selftests: Support fixed counters bitmap in pmu_counters_test

In reply to Zide Chen

From: Dapeng Mi <[email protected]>

On PerfMon v5, CPUID.0AH:ECX represents the fixed counter bitmask, while EDX[4:0] indicates the number of contiguous fixed counters starting from 0:

FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i).

Fix test_fixed_counters() to derive supported fixed counters using the above formula instead of relying solely on EDX[4:0]. This allows the test to cover non-contiguous fixed counter configurations.

Update this_pmu_has() to take the PMU version into consideration. Without this fix, it could return incorrect results in test cases such as test_arch_events(), which configure a PMU version different from the one originally advertised by KVM without explicitly updating CPUID.0AH.{ECX,EDX} to match the new PMU version.

For example, after downgrading a guest from PMU v5 to PMU v2, this_pmu_has() would still consult CPUID.0AH.ECX while KVM ignores it, resulting in a mismatch in the reported fixed counter capabilities.

---
v2:
- Fix this_pmu_has() to honor the guest's configured PMU version.
- Update the pmu_vm_create_with_vcpus() calls after rebasing.

---
 .../selftests/kvm/include/x86/processor.h     |  6 ++++-
 .../selftests/kvm/x86/pmu_counters_test.c     | 26 ++++++++++++++-----
 2 files changed, 25 insertions(+), 7 deletions(-)
tools/testing/selftests/kvm/include/x86/processor.h +5 -1
index 6e6f70035508..c983dbfd9d70 100644
@@ -827,6 +827,7 @@ static __always_inline bool this_cpu_has_p(struct kvm_x86_cpu_property property)
 
 static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature)
 {
+	u8 pmu_version;
 	u32 nr_bits;
 
 	if (feature.f.reg == KVM_CPUID_EBX) {
@@ -836,7 +837,10 @@ static inline bool this_pmu_has(struct kvm_x86_pmu_feature feature)
 
 	GUEST_ASSERT(feature.f.reg == KVM_CPUID_ECX);
 	nr_bits = this_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
-	return nr_bits > feature.f.bit || this_cpu_has(feature.f);
+	pmu_version = this_cpu_property(X86_PROPERTY_PMU_VERSION);
+
+	return (pmu_version < 5) ? nr_bits > feature.f.bit :
+	       nr_bits > feature.f.bit || this_cpu_has(feature.f);
 }
 
 static __always_inline u64 this_cpu_supported_xcr0(void)
tools/testing/selftests/kvm/x86/pmu_counters_test.c +20 -6
index c3e784e16348..21bd8ec90c98 100644
@@ -3,6 +3,7 @@
  * Copyright (C) 2023, Tencent, Inc.
  */
 #include <x86intrin.h>
+#include <linux/bitmap.h>
 
 #include "pmu.h"
 #include "processor.h"
@@ -631,23 +632,36 @@ static void __test_fixed_counters(struct kvm_vcpu *vcpu, u8 nr_fixed_counters,
 static void test_fixed_counters(u8 pmu_version, u64 perf_capabilities)
 {
 	u8 nr_fixed_counters = kvm_cpu_property(X86_PROPERTY_PMU_NR_FIXED_COUNTERS);
+	unsigned long fixed_subset;
 	struct kvm_vcpu **vcpus;
 	struct kvm_vm *vm;
+	u32 fixed_bitmap;
 	int i = 0;
-	u32 k;
-	u8 j;
 
 	pr_info("Testing %u fixed counters, PMU version %u, perf_caps = %lx\n",
 		nr_fixed_counters, pmu_version, perf_capabilities);
 
+	fixed_bitmap = BIT_ULL(nr_fixed_counters) - 1;
+	if (pmu_version >= 5)
+		fixed_bitmap |= kvm_cpu_property(X86_PROPERTY_PMU_FIXED_COUNTERS_BITMASK);
 
-	vm = pmu_vm_create_with_vcpus((nr_fixed_counters + 1) * BIT(nr_fixed_counters),
+	vm = pmu_vm_create_with_vcpus((1 << __builtin_popcount(fixed_bitmap)),
 				      guest_test_fixed_counters,
 				      pmu_version, perf_capabilities, &vcpus);
 
-	for (j = 0; j <= nr_fixed_counters; j++) {
-		for (k = 0; k <= (BIT(nr_fixed_counters) - 1); k++)
-			__test_fixed_counters(vcpus[i++], j, k);
+	for (fixed_subset = 0; fixed_subset <= fixed_bitmap; fixed_subset++) {
+		u32 nr_contiguous;
+
+		/*
+		 * The loop walks all values from 0 to fixed_bitmap, so skip any
+		 * value that is not a subset of fixed_bitmap.
+		 */
+		if (fixed_subset & ~fixed_bitmap)
+			continue;
+
+		nr_contiguous = find_first_zero_bit(&fixed_subset,
+						    MAX_NR_FIXED_COUNTERS);
+		__test_fixed_counters(vcpus[i++], nr_contiguous, fixed_subset);
 	}
 
 	pmu_vm_free(vm, vcpus);
Signature · 1 line
2.55.0
author
#81 lore raw
In reply to Zide Chen

Hi Sean,

Friendly ping on this series. The v2 addresses all of your feedback from the v1 review. I'd appreciate your thoughts whenever you have some cycles.

For context, it would be ideal to land this vPMU v5 series before my other series to keep the stack clean:

1. PMU Partitioning:
https://lore.kernel.org/kvm/[email protected]/T/#t
2. TMA:
https://lore.kernel.org/kvm/[email protected]/

Thanks,
Zide

Quoted 113 lines · Zide Chen

KVM currently caps the guest Intel architectural PMU version at 2. This series bumps it to PerfMon v5 and adds support for some features that are introduced in PerfMon v3-v5.

- CPUID.0AH:ECX fixed-counter bitmap (v5): supersedes EDX[4:0] and lets KVM/guests represent non-contiguous fixed counters (e.g. some E-core server parts skip fixed counter 3).
- Add support for IA32_PERF_GLOBAL_STATUS_SET (v4): lets software set individual bits in the global status MSR.
- Add support for IA32_PERF_GLOBAL_INUSE (v4): reports which counters and PMI are currently claimed.
- Implement ANYTHREAD_DEPRECATION capability.

Intel PerfMon v5 doesn't support non-contiguous GP counters; however, this series lays the groundwork for GP counter bitmap support anyway, for consistency with the host perf API interface, and prepares for Perfmon Masking support.

Freeze_PerfMon_On_PMI virtualization is not implemented because, as commit 3daa96d67274 ("perf/intel: Remove Perfmon-v4 counter_freezing support") states, the Freeze-on-PMI mechanism violates the perf counter independence.

Streamlined Freeze_LBRs_On_PMI is not implemented because LBR is not yet supported on mediated vPMU.

Tested on SPR and CWF:

- Ran "perf test" on both L0 and L1; no issues attributable to this series have been observed so far.

- Ran KVM selftests on both L0 and L1.

- Passed AnyThread tests on both fixed and gp counters:

  $ perf stat -e r20003c,r2000c4,r2000c0 -- sleep 1

- Tested various guest fixed-counter configurations:

      CPUID.0AH:ECX = 0x7   CPUID.0AH:EDX[4:0]=3
      CPUID.0AH:ECX = 0x2   CPUID.0AH:EDX[4:0]=0
      CPUID.0AH:ECX = 0x6   CPUID.0AH:EDX[4:0]=0
      CPUID.0AH:ECX = 0x5   CPUID.0AH:EDX[4:0]=1
      CPUID.0AH:ECX = 0x0   CPUID.0AH:EDX[4:0]=0

Verified that the guest PMU reflected the configured fixed-counter and that the following command used fixed or general-purpose counters as expected:

  $ perf stat -e instructions,cpu-cycles,ref-cycles -- sleep 1

- Manually tested IA32_PERF_GLOBAL_INUSE.

- Ran some additional perf tests on both L0 and L1.

- Sanity testing on nested VMs.

Patch series summary:
patch 1-4: Code cleanup and a fix.
patch 5-10: Implement PMC bitmap support.
patch 11-14: Implement PerfMon v3-v5 new features. patch 15: Advertise PerfMon version up to v5.
patch 16: Fix pmu_counters_test selftests fixed counter test case.

Rebased to kvm-x86/next: 76671054f9a1

v2 changes:
- Dropped v1 11/15: KVM: x86/pmu: Emulate streamlined Freeze-LBR-on-PMI
- New patch 4/15 to gate BUFFER_OVF reserved bit on guest DS feature.
- Some renames:
- all_valid_pmc_mask to pmc_exists;
- add _is to kvm_{gp,fixed}_pmc_supported();
- split kvm_for_each_set_pmc_idx() to kvm_for_each_{gp,fixed}_counter().
- Add missing AnyThr handling in IA32_FIXED_CTR_CTRL and advertise anythread_deprecated only if kvm_pmu_cap.version >= 5.
- Cap vPMU v3/v4 to v2, as AnyThread cannot be safely virtualized.
- Miscellaneous updates to changelogs and some minor fixes.

Dapeng Mi (6):
KVM: x86/pmu: Drop nr_arch_{gp,fixed}_counters from kvm_pmu perf/x86: Plumb counter bitmap from x86_pmu to x86_pmu_cap
KVM: x86/pmu: Switch to bitmask-based KVM PMU capabilities perf/x86: Remove num_counters_{gp,fixed} from x86_pmu_capability
KVM: x86/pmu: Advertise PerfMon version 5 on Intel hosts
KVM: selftests: Support fixed counters bitmap in pmu_counters_test

Zide Chen (10):
KVM: x86/pmu: Remove redundant Perf Global Status MSR bit definitions
KVM: x86/pmu: Rename all_valid_pmc_idx to pmc_exists
KVM: x86/pmu: Rename reserved_bits to eventsel_rsvd in kvm_pmu
KVM: x86/pmu: Gate BUFFER_OVF reserved bit on guest DS
KVM: x86/pmu: Add PMC bitmap accessor helpers
KVM: x86/pmu: Expose kvm_host_pmu to vendor modules
KVM: x86/pmu: Emulate the GLOBAL_STATUS_SET and GLOBAL_INUSE MSRs
KVM: x86/pmu: Populate CPUID.0AH:ECX fixed-counter bitmap
KVM: x86/pmu: Factor out fixed counter control bit calculation
KVM: x86/pmu: Ignore AnyThread bit if CPUID.0AH:EDX[15] is set

 arch/x86/events/core.c                        |   8 +-
 arch/x86/include/asm/kvm_host.h               |   9 +-
 arch/x86/include/asm/msr-index.h              |  12 +-
 arch/x86/include/asm/perf_event.h             |  10 +-
 arch/x86/kvm/cpuid.c                          |  21 ++-
 arch/x86/kvm/msrs.c                           |  12 +-
 arch/x86/kvm/pmu.c                            |  85 ++++++---
 arch/x86/kvm/pmu.h                            |  54 +++++-
 arch/x86/kvm/svm/pmu.c                        |  37 ++--
 arch/x86/kvm/svm/svm.c                        |  17 +-
 arch/x86/kvm/vmx/nested.c                     |   8 +-
 arch/x86/kvm/vmx/pmu_intel.c                  | 167 +++++++++++++-----
 arch/x86/kvm/vmx/vmx.c                        |  19 +-
 .../selftests/kvm/include/x86/processor.h     |   6 +-
 .../selftests/kvm/x86/pmu_counters_test.c     |  26 ++-
 15 files changed, 349 insertions(+), 142 deletions(-)