Skip to contentSkip to conversation

ARM64 PMU Partitioning

PATCH Superseded Colton Lewis 4 participants 58 messages Sep 24 – Oct 1

Subsystems (8)

v9 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v9 cover ARM64 PMU Partitioning

This series creates a new PMU scheme on ARM, a partitioned PMU that allows reserving a subset of counters for more direct guest access, significantly reducing overhead. More details, including performance benchmarks, can be read in the v1 cover letter linked below.

There is no longer a kernel command line parameter (arm_pmuv3.reserved_host_counters); PMU partitioning is now completely controlled via the KVM API using KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION and KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS vCPU device attributes. When partitioning is enabled for a VM, userspace must explicitly configure a guest event counter count strictly less than the maximum general-purpose counters implemented by the PMU (leaving at least one general-purpose counter for the host) prior to calling KVM_ARM_VCPU_PMU_V3_INIT. A QEMU patch demonstrating how to use the uAPI is sent separately.

An overview of what this series accomplishes was presented at KVM Forum 2025. Slides [1] and video [2] are linked below.

v9:

* Rebase on top of v7.3-rc4.

* Drop the arm_pmuv3.reserved_host_counters module parameter so partitioning is completely controlled via the KVM vCPU device attribute uAPI (KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION and KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS), and document the new attribute and counter allocation rules in Documentation/virt/kvm/devices/vcpu.rst (James Clark).

* Move the dynamic counter allocation mask (cntr_mask) from global struct arm_pmu to per-CPU struct pmu_hw_events (cpuc->cntr_mask) in a dedicated drivers/perf patch, fixing multi-pCPU counter reservation clobbering on vCPU migration and eliminating the cpu_pm_pmu_setup() cpuidle WARN_ON_ONCE (Zide Chen, James Clark).

* Synchronously propagate trapped guest writes to PMEVTYPER<n>_EL0 and PMCCFILTR_EL0 to hardware via kvm_pmu_apply_single_event_filter() when guest-owned, fixing in-guest perf stat event count skew across runs (James Clark, Sashiko AI Review).

* Refactor armv8pmu_can_use_pmccntr() and armv8pmu_get_event_idx() to check cntr_mask inside armv8pmu_can_use_pmccntr() and un-nest the 64-bit user-access check so cycle events fall back cleanly to general-purpose counters when PMCCNTR_EL0 is reserved by a guest (Robin Murphy).

* Restrict the lazy transition to VCPU_PMU_ACCESS_GUEST_OWNED to when the guest actively enables counting (PMCR_EL0.E = 1 or setting guest counter bits in PMCNTENSET_EL0 / PMINTENSET_EL1), preventing guest boot-time PMU probing from prematurely claiming hardware counters and triggering spurious host counter preemption warnings (James Clark).

* Fix patch dependency ordering and series bisectability across all commits, removing intermediate max_guest_counters / hw_cntr_impl churn and squashing the selftest exception relaxation into the Partitioned PMU selftest patch (James Clark).

* Fix compiler warning for struct arm_pmu declaration in include/kvm/arm_pmu.h (kernel test robot) and guard kvm_pmu_host_counter_mask() when KVM is compiled in but not active (wuyifan).

* Track physical CPU PMU residency in vcpu->arch.pmu.loaded_on_cpu separately from VCPU_PMU_ACCESS_GUEST_OWNED, and toggle MDCR_EL2.HPME via kvm_pmu_host_start() / kvm_pmu_host_stop() instead of PMCR_EL0.E when starting/stopping host perf events while a partitioned guest is loaded.

* Allow kvm_vcpu_pmu_resync_el0() to resynchronize VHE EL0 event filters (PMEVTYPER<n>_EL0.U) in process context and order kvm_pmu_put() before kvm_vcpu_pmu_restore_host() in kvm_arch_vcpu_put().

* Address additional Sashiko AI Review findings:
- Check idx - 1 against cpuc->cntr_mask in armv8pmu_get_chain_idx()

    to prevent 64-bit chained host events from crossing an odd `HPMN`
    partition boundary, and use `cpuc->cntr_mask` in
    `armv8pmu_enable_user_access()`.
  - Latch live hardware `PMOVSSET_EL0` overflow bits for guest counters
    with IRQs disabled (`local_irq_save()`) in `kvm_pmu_part_overflow_status()`
    and during trapped guest accesses to `PMOVS{SET,CLR}_EL0`.
  - Add mandatory `isb()` barriers after control-plane system register
    writes (`mdcr_el2`, `pmcntenclr_el0`, `pmintenclr_el1`), preserve
    guest `PMSELR_EL0` / `PMUSERENR_EL0` when `MDCR_EL2.TPM == 0`, and
    restore host `PMCR_EL0` control flags on `kvm_pmu_put()`.

v8:
https://lore.kernel.org/kvmarm/[email protected]/

v7:
https://lore.kernel.org/kvmarm/[email protected]/

v6:
https://lore.kernel.org/kvmarm/[email protected]/

v5:
https://lore.kernel.org/kvmarm/[email protected]/

v4:
https://lore.kernel.org/kvmarm/[email protected]/

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

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

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

[1] https://gitlab.com/qemu-project/kvm-forum/-/raw/main/_attachments/2025/Optimizing__itvHkhc.pdf
[2] https://www.youtube.com/watch?v=YRzZ8jMIA6M&list=PLW3ep1uCIRfxwmllXTOA2txfDWN6vUOHp&index=9

Colton Lewis (21):
arm64: cpufeature: Add cpucap for HPMN0
KVM: arm64: Reorganize PMU functions
perf: arm_pmuv3: Generalize counter bitmasks
perf: arm_pmuv3: Move counter allocation mask to per-CPU struct
pmu_hw_events
perf: arm_pmuv3: Check cntr_mask before using pmccntr
perf: arm_pmuv3: Allocate counter indices from high to low
KVM: arm64: Add initial scaffolding for Partitioned PMU
KVM: arm64: Set up FGT for Partitioned PMU
KVM: arm64: Add Partitioned PMU register trap handlers
KVM: arm64: Set up MDCR_EL2 to handle a Partitioned PMU
KVM: arm64: Context swap Partitioned PMU guest registers
KVM: arm64: Enforce PMU event filter at vcpu_load()
perf: Add perf_pmu_resched_update()
KVM: arm64: Allow kvm_vcpu_pmu_resync_el0() to resync filters in
process context
KVM: arm64: Apply dynamic guest counter reservations
KVM: arm64: Implement lazy PMU context swaps
perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
KVM: arm64: Detect overflows for the Partitioned PMU
KVM: arm64: Add vCPU device attr to partition the PMU
KVM: selftests: Add find_bit to KVM library
KVM: arm64: selftests: Add test case for Partitioned PMU

Marc Zyngier (1):
KVM: arm64: Reorganize PMU includes

 Documentation/virt/kvm/devices/vcpu.rst       |  42 +-
 arch/arm/include/asm/arm_pmuv3.h              |  16 +
 arch/arm64/include/asm/arm_pmuv3.h            |   7 +-
 arch/arm64/include/asm/kvm_host.h             |  18 +-
 arch/arm64/include/asm/kvm_types.h            |   6 +-
 arch/arm64/include/uapi/asm/kvm.h             |   2 +
 arch/arm64/kernel/cpufeature.c                |  10 +-
 arch/arm64/kvm/Makefile                       |   2 +-
 arch/arm64/kvm/arm.c                          |   4 +-
 arch/arm64/kvm/config.c                       |  49 +-
 arch/arm64/kvm/debug.c                        |  41 +-
 arch/arm64/kvm/pmu-direct.c                   | 636 ++++++++++++++
 arch/arm64/kvm/pmu-emul.c                     | 718 +---------------
 arch/arm64/kvm/pmu.c                          | 787 +++++++++++++++++-
 arch/arm64/kvm/sys_regs.c                     | 334 ++++++--
 arch/arm64/tools/cpucaps                      |   1 +
 arch/arm64/tools/sysreg                       |   6 +-
 drivers/perf/arm_pmu.c                        |   7 +-
 drivers/perf/arm_pmuv3.c                      |  97 ++-
 include/kvm/arm_pmu.h                         |  93 ++-
 include/linux/perf/arm_pmu.h                  |   2 +
 include/linux/perf/arm_pmuv3.h                |  14 +-
 include/linux/perf_event.h                    |   3 +
 kernel/events/core.c                          |  31 +-
 tools/include/perf/arm_pmuv3.h                |  12 +-
 tools/testing/selftests/kvm/Makefile.kvm      |   1 +
 .../selftests/kvm/arm64/vpmu_counter_access.c | 129 ++-
 tools/testing/selftests/kvm/lib/find_bit.c    |   2 +
 28 files changed, 2200 insertions(+), 870 deletions(-)
 create mode 100644 arch/arm64/kvm/pmu-direct.c
 create mode 100644 tools/testing/selftests/kvm/lib/find_bit.c

base-commit: 93f51579e7df248780214094418f205253383cc5

Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#1 lore raw

PATCH v9 1/22 arm64: cpufeature: Add cpucap for HPMN0

Add a capability for FEAT_HPMN0, whether MDCR_EL2.HPMN can specify 0 counters reserved for the guest.

This required changing HPMN0 to an UnsignedEnum in tools/sysreg because otherwise not all the appropriate macros are generated to add it to arm64_cpu_capabilities_arm64_features.

---
 arch/arm64/kernel/cpufeature.c | 10 +++++++++-
 arch/arm64/kvm/sys_regs.c      |  5 ++++-
 arch/arm64/tools/cpucaps       |  1 +
 arch/arm64/tools/sysreg        |  6 +++---
 4 files changed, 17 insertions(+), 5 deletions(-)
arch/arm64/kernel/cpufeature.c +9 -1
index 32102c3912fa7..f8431cf858619 100644
@@ -77,7 +77,7 @@
 #include <linux/percpu.h>
 #include <linux/sched/isolation.h>
 
-#include <asm/arm_pmuv3.h>
+#include <linux/perf/arm_pmuv3.h>
 #include <asm/cpu.h>
 #include <asm/cpufeature.h>
 #include <asm/cpu_ops.h>
@@ -564,6 +564,7 @@ static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
 };
 
 static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_HPMN0_SHIFT, 4, 0),
 	S_ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_DoubleLock_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_PMSVer_SHIFT, 4, 0),
 	ARM64_FTR_BITS(FTR_HIDDEN, FTR_STRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_CTX_CMPs_SHIFT, 4, 0),
@@ -3012,6 +3013,13 @@ static const struct arm64_cpu_capabilities arm64_features[] = {
 		.matches = has_cpuid_feature,
 		ARM64_CPUID_FIELDS(ID_AA64MMFR0_EL1, FGT, FGT2)
 	},
+	{
+		.desc = "HPMN0",
+		.type = ARM64_CPUCAP_SYSTEM_FEATURE,
+		.capability = ARM64_HAS_HPMN0,
+		.matches = has_cpuid_feature,
+		ARM64_CPUID_FIELDS(ID_AA64DFR0_EL1, HPMN0, IMP)
+	},
 #ifdef CONFIG_ARM64_SME
 	{
 		.desc = "Scalable Matrix Extension",
arch/arm64/kvm/sys_regs.c +4 -1
index 44aae52c473d7..f7f750b7a91c4 100644
@@ -2219,6 +2219,8 @@ static u64 sanitise_id_aa64dfr0_el1(const struct kvm_vcpu *vcpu, u64 val)
 	if (kvm_vcpu_has_pmu(vcpu))
 		val |= SYS_FIELD_PREP(ID_AA64DFR0_EL1, PMUVer,
 				      kvm_arm_pmu_get_pmuver_limit());
+	else
+		val &= ~ID_AA64DFR0_EL1_HPMN0_MASK;
 
 	/* Hide SPE from guests */
 	val &= ~ID_AA64DFR0_EL1_PMSVer_MASK;
@@ -3417,7 +3419,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
 		    ID_AA64DFR0_EL1_DoubleLock_MASK |
 		    ID_AA64DFR0_EL1_WRPs_MASK |
 		    ID_AA64DFR0_EL1_PMUVer_MASK |
-		    ID_AA64DFR0_EL1_DebugVer_MASK),
+		    ID_AA64DFR0_EL1_DebugVer_MASK |
+		    ID_AA64DFR0_EL1_HPMN0_MASK),
 	ID_SANITISED(ID_AA64DFR1_EL1),
 	ID_UNALLOCATED(5,2),
 	ID_UNALLOCATED(5,3),
arch/arm64/tools/cpucaps +1 -0
index 2775ba3359cfe..a309c18235e71 100644
@@ -43,6 +43,7 @@ HAS_GIC_PRIO_MASKING
 HAS_GIC_PRIO_RELAXED_SYNC
 HAS_ICH_HCR_EL2_TDIR
 HAS_HCR_NV1
+HAS_HPMN0
 HAS_HCX
 HAS_LDAPR
 HAS_LPA2
arch/arm64/tools/sysreg +3 -3
index e2d37ee221b84..1035b406ef183 100644
@@ -1679,9 +1679,9 @@ EndEnum
 EndSysreg
 
 Sysreg	ID_AA64DFR0_EL1	3	0	0	5	0
-Enum	63:60	HPMN0
-	0b0000	UNPREDICTABLE
-	0b0001	DEF
+UnsignedEnum	63:60	HPMN0
+	0b0000	NI
+	0b0001	IMP
 EndEnum
 UnsignedEnum	59:56	ExtTrcBuff
 	0b0000	NI
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#2 lore raw

Re: [PATCH v9 01/22] arm64: cpufeature: Add cpucap for HPMN0

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] arm64: cpufeature: Missing AArch32 ID_DFR1_EL1_HPMN0 in ftr_id_dfr1 array --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] arm64: cpufeature: Missing AArch32 ID_DFR1_EL1_HPMN0 in ftr_id_dfr1 array --

commit 5ba8cce217bacafa8dc1ca20dcd2a95acd9bf835
Author: Colton Lewis <[email protected]>

arm64: cpufeature: Add cpucap for HPMN0

This commit adds a CPU capability for FEAT_HPMN0, allowing MDCR_EL2.HPMN to specify 0 counters reserved for the guest. It updates the tools/sysreg definition to UnsignedEnum and adds the corresponding feature tracking.

Quoted 4 lines
arch/arm64/kernel/cpufeature.c +0 -0
index 32102c3912fa7..f8431cf858619 100644

[ ... ]

arch/arm64/kernel/cpufeature.c static const struct arm64_ftr_bits ftr_id_mmfr0[] = { · line 567

Quoted 5 lines
arch/arm64/kernel/cpufeature.c +1 -0
@@ -564,6 +564,7 @@ static const struct arm64_ftr_bits ftr_id_mmfr0[] = {
 };
 
 static const struct arm64_ftr_bits ftr_id_aa64dfr0[] = {
+	ARM64_FTR_BITS(FTR_HIDDEN, FTR_NONSTRICT, FTR_LOWER_SAFE, ID_AA64DFR0_EL1_HPMN0_SHIFT, 4, 0),

[Severity: Medium]
Does this also require updating the AArch32 counterpart register ID_DFR1_EL1 in the ftr_id_dfr1 array?

By adding ID_AA64DFR0_EL1_HPMN0 as FTR_NONSTRICT here but omitting the field mapping from ftr_id_dfr1, the AArch32 view remains implicitly STRICT. On a system with asymmetric HPMN0 support across CPUs, when a secondary CPU boots and executes update_32bit_cpu_features(), it triggers check_update_ftr_reg(SYS_ID_DFR1_EL1).

This could cause the boot sanity checks to unexpectedly fail and taint the kernel due to the STRICT default conflicting with this NONSTRICT definition.

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

PATCH v9 2/22 KVM: arm64: Reorganize PMU includes

In reply to Colton Lewis

From: Marc Zyngier <[email protected]>

Including *all* of asm/kvm_host.h in asm/arm_pmuv3.h is a bad idea because that is much more than arm_pmuv3.h logically needs and creates a circular dependency that makes it easy to introduce compiler errors when editing this code.

asm/kvm_host.h includes kvm/arm_pmu.h includes perf/arm_pmuv3.h includes asm/arm_pmuv3.h includes asm/kvm_host.h

Reorganize the PMU includes to be more sane. In particular:

- Remove the circular dependency by removing the kvm_host.h include from asm/arm_pmuv3.h since 99% of it isn't needed.

- Move the remaining tiny bit of KVM/PMU interface from kvm_host.h into arm_pmu.h

- Conditionally on ARM64, include the more targeted arm_pmu.h directly in the arm_pmuv3.c driver.

---
 arch/arm64/include/asm/arm_pmuv3.h |  2 --
 arch/arm64/include/asm/kvm_host.h  | 14 --------------
 drivers/perf/arm_pmuv3.c           |  5 +++++
 include/kvm/arm_pmu.h              | 19 +++++++++++++++++++
 4 files changed, 24 insertions(+), 16 deletions(-)
arch/arm64/include/asm/arm_pmuv3.h +0 -2
index 8a777dec8d88a..cf2b2212e00a2 100644
@@ -6,8 +6,6 @@
 #ifndef __ASM_PMUV3_H
 #define __ASM_PMUV3_H
 
-#include <asm/kvm_host.h>
-
 #include <asm/cpufeature.h>
 #include <asm/sysreg.h>
 
arch/arm64/include/asm/kvm_host.h +0 -14
index 27fe0cd5b2d7a..217c36af1ca51 100644
@@ -1467,25 +1467,11 @@ void kvm_arch_vcpu_ctxflush_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu);
 void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu);
 
-static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
-{
-	return (!has_vhe() && attr->exclude_host);
-}
-
 #ifdef CONFIG_KVM
-void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr);
-void kvm_clr_pmu_events(u64 clr);
-bool kvm_set_pmuserenr(u64 val);
 void kvm_enable_trbe(void);
 void kvm_disable_trbe(void);
 void kvm_tracing_set_el1_configuration(u64 trfcr_while_in_guest);
 #else
-static inline void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr) {}
-static inline void kvm_clr_pmu_events(u64 clr) {}
-static inline bool kvm_set_pmuserenr(u64 val)
-{
-	return false;
-}
 static inline void kvm_enable_trbe(void) {}
 static inline void kvm_disable_trbe(void) {}
 static inline void kvm_tracing_set_el1_configuration(u64 trfcr_while_in_guest) {}
drivers/perf/arm_pmuv3.c +5 -0
index 03359e078301f..fbd631ec71a65 100644
@@ -10,6 +10,11 @@
 
 #include <asm/cputype.h>
 #include <asm/irq_regs.h>
+
+#if defined(CONFIG_ARM64)
+#include <kvm/arm_pmu.h>
+#endif
+
 #include <asm/perf_event.h>
 #include <asm/virt.h>
 
include/kvm/arm_pmu.h +19 -0
index 6b4a118d17ca9..93f41ac77c25f 100644
@@ -9,12 +9,22 @@
 
 #include <linux/perf_event.h>
 #include <linux/perf/arm_pmuv3.h>
+#include <linux/perf/arm_pmu.h>
 
 #define KVM_ARMV8_PMU_MAX_COUNTERS	32
 
 /* PPI #23 - architecturally specified for GICv5 */
 #define KVM_ARMV8_PMU_GICV5_IRQ		0x20000017
 
+#define kvm_pmu_counter_deferred(attr)			\
+	({						\
+		!has_vhe() && (attr)->exclude_host;	\
+	})
+
+struct kvm;
+struct kvm_device_attr;
+struct kvm_vcpu;
+
 #if IS_ENABLED(CONFIG_HW_PERF_EVENTS) && IS_ENABLED(CONFIG_KVM)
 struct kvm_pmc {
 	u8 idx;	/* index into the pmu->pmc array */
@@ -68,6 +78,9 @@ int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu,
 int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu);
 
 struct kvm_pmu_events *kvm_get_pmu_events(void);
+void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr);
+void kvm_clr_pmu_events(u64 clr);
+bool kvm_set_pmuserenr(u64 val);
 void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu);
 void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 void kvm_vcpu_pmu_resync_el0(void);
@@ -165,6 +178,12 @@ static inline u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
 #define kvm_vcpu_has_pmu(vcpu)		({ false; })
 #define kvm_vcpu_has_pmuv3_strict(vcpu)	({ false; })
 static inline void kvm_pmu_update_vcpu_events(struct kvm_vcpu *vcpu) {}
+static inline void kvm_set_pmu_events(u64 set, struct perf_event_attr *attr) {}
+static inline void kvm_clr_pmu_events(u64 clr) {}
+static inline bool kvm_set_pmuserenr(u64 val)
+{
+	return false;
+}
 static inline void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu) {}
 static inline void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu) {}
 static inline void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu) {}
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#4 lore raw

Re: [PATCH v9 02/22] KVM: arm64: Reorganize PMU includes

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 15 lines

From: Marc Zyngier <[email protected]>

Including *all* of asm/kvm_host.h in asm/arm_pmuv3.h is a bad idea because that is much more than arm_pmuv3.h logically needs and creates a circular dependency that makes it easy to introduce compiler errors when editing this code.

asm/kvm_host.h includes kvm/arm_pmu.h includes perf/arm_pmuv3.h includes asm/arm_pmuv3.h includes asm/kvm_host.h

Reorganize the PMU includes to be more sane. In particular:

- Remove the circular dependency by removing the kvm_host.h include from asm/arm_pmuv3.h since 99% of it isn't needed.

Quoted 8 lines

- Move the remaining tiny bit of KVM/PMU interface from kvm_host.h into arm_pmu.h

- Conditionally on ARM64, include the more targeted arm_pmu.h directly in the arm_pmuv3.c driver.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 3/22 KVM: arm64: Reorganize PMU functions

In reply to Colton Lewis

A lot of functions in pmu-emul.c aren't specific to the emulated PMU implementation. Move them to the more appropriate pmu.c file where shared PMU functions should live.

---
 arch/arm64/kvm/pmu-emul.c | 702 +------------------------------------
 arch/arm64/kvm/pmu.c      | 705 ++++++++++++++++++++++++++++++++++++++
 include/kvm/arm_pmu.h     |   9 +-
 3 files changed, 714 insertions(+), 702 deletions(-)
arch/arm64/kvm/pmu-emul.c +1 -701
index 5b1af7e2176fa..2a2c924fc6b8f 100644
@@ -17,19 +17,10 @@
 
 #define PERF_ATTR_CFG1_COUNTER_64BIT	BIT(0)
 
-static LIST_HEAD(arm_pmus);
-static DEFINE_MUTEX(arm_pmus_lock);
-
 static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc);
 static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc);
 static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc);
 
-bool kvm_supports_guest_pmuv3(void)
-{
-	guard(mutex)(&arm_pmus_lock);
-	return !list_empty(&arm_pmus);
-}
-
 static struct kvm_vcpu *kvm_pmc_to_vcpu(const struct kvm_pmc *pmc)
 {
 	return container_of(pmc, struct kvm_vcpu, arch.pmu.pmc[pmc->idx]);
@@ -40,46 +31,6 @@ static struct kvm_pmc *kvm_vcpu_idx_to_pmc(struct kvm_vcpu *vcpu, int cnt_idx)
 	return &vcpu->arch.pmu.pmc[cnt_idx];
 }
 
-static u32 __kvm_pmu_event_mask(unsigned int pmuver)
-{
-	switch (pmuver) {
-	case ID_AA64DFR0_EL1_PMUVer_IMP:
-		return GENMASK(9, 0);
-	case ID_AA64DFR0_EL1_PMUVer_V3P1:
-	case ID_AA64DFR0_EL1_PMUVer_V3P4:
-	case ID_AA64DFR0_EL1_PMUVer_V3P5:
-	case ID_AA64DFR0_EL1_PMUVer_V3P7:
-		return GENMASK(15, 0);
-	default:		/* Shouldn't be here, just for sanity */
-		WARN_ONCE(1, "Unknown PMU version %d\n", pmuver);
-		return 0;
-	}
-}
-
-static u32 kvm_pmu_event_mask(struct kvm *kvm)
-{
-	u64 dfr0 = kvm_read_vm_id_reg(kvm, SYS_ID_AA64DFR0_EL1);
-	u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, dfr0);
-
-	return __kvm_pmu_event_mask(pmuver);
-}
-
-u64 kvm_pmu_evtyper_mask(struct kvm *kvm)
-{
-	u64 mask = ARMV8_PMU_EXCLUDE_EL1 | ARMV8_PMU_EXCLUDE_EL0 |
-		   kvm_pmu_event_mask(kvm);
-
-	if (kvm_has_feat(kvm, ID_AA64PFR0_EL1, EL2, IMP))
-		mask |= ARMV8_PMU_INCLUDE_EL2;
-
-	if (kvm_has_feat(kvm, ID_AA64PFR0_EL1, EL3, IMP))
-		mask |= ARMV8_PMU_EXCLUDE_NS_EL0 |
-			ARMV8_PMU_EXCLUDE_NS_EL1 |
-			ARMV8_PMU_EXCLUDE_EL3;
-
-	return mask;
-}
-
 /**
  * kvm_pmc_is_64bit - determine if counter is 64bit
  * @pmc: counter context
@@ -272,59 +223,6 @@ void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
 	irq_work_sync(&vcpu->arch.pmu.overflow_work);
 }
 
-static u64 kvm_pmu_hyp_counter_mask(struct kvm_vcpu *vcpu)
-{
-	unsigned int hpmn, n;
-
-	if (!vcpu_has_nv(vcpu))
-		return 0;
-
-	hpmn = SYS_FIELD_GET(MDCR_EL2, HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
-	n = vcpu->kvm->arch.nr_pmu_counters;
-
-	/*
-	 * Programming HPMN to a value greater than PMCR_EL0.N is
-	 * CONSTRAINED UNPREDICTABLE. Make the implementation choice that an
-	 * UNKNOWN number of counters (in our case, zero) are reserved for EL2.
-	 */
-	if (hpmn >= n)
-		return 0;
-
-	/*
-	 * Programming HPMN=0 is CONSTRAINED UNPREDICTABLE if FEAT_HPMN0 isn't
-	 * implemented. Since KVM's ability to emulate HPMN=0 does not directly
-	 * depend on hardware (all PMU registers are trapped), make the
-	 * implementation choice that all counters are included in the second
-	 * range reserved for EL2/EL3.
-	 */
-	return GENMASK(n - 1, hpmn);
-}
-
-bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int idx)
-{
-	return kvm_pmu_hyp_counter_mask(vcpu) & BIT(idx);
-}
-
-u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu)
-{
-	u64 mask = kvm_pmu_implemented_counter_mask(vcpu);
-
-	if (!vcpu_has_nv(vcpu) || vcpu_is_el2(vcpu))
-		return mask;
-
-	return mask & ~kvm_pmu_hyp_counter_mask(vcpu);
-}
-
-u64 kvm_pmu_implemented_counter_mask(struct kvm_vcpu *vcpu)
-{
-	u64 val = FIELD_GET(ARMV8_PMU_PMCR_N, kvm_vcpu_read_pmcr(vcpu));
-
-	if (val == 0)
-		return BIT(ARMV8_PMU_CYCLE_IDX);
-	else
-		return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX);
-}
-
 static void kvm_pmc_enable_perf_event(struct kvm_pmc *pmc)
 {
 	if (!pmc->perf_event) {
@@ -370,7 +268,7 @@ void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u64 val)
  * counter where the values of the global enable control, PMOVSSET_EL0[n], and
  * PMINTENSET_EL1[n] are all 1.
  */
-static bool kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
+bool kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
 {
 	u64 reg = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
 
@@ -393,16 +291,6 @@ static bool kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
 	return reg;
 }
 
-static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
-{
-	struct kvm_pmu *pmu = &vcpu->arch.pmu;
-
-	if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
-		return;
-
-	WARN_ON(kvm_vgic_inject_irq(vcpu->kvm, vcpu, pmu->irq_num,
-				    kvm_pmu_overflow_status(vcpu), pmu));
-}
 
 bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
 {
@@ -423,43 +311,6 @@ bool kvm_pmu_update_run(struct kvm_vcpu *vcpu)
 	return update;
 }
 
-/**
- * kvm_pmu_flush_hwstate - flush pmu state to cpu
- * @vcpu: The vcpu pointer
- *
- * Check if the PMU has overflowed while we were running in the host, and inject
- * an interrupt if that was the case.
- */
-void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu)
-{
-	kvm_pmu_update_state(vcpu);
-}
-
-/**
- * kvm_pmu_sync_hwstate - sync pmu state from cpu
- * @vcpu: The vcpu pointer
- *
- * Check if the PMU has overflowed while we were running in the guest, and
- * inject an interrupt if that was the case.
- */
-void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
-{
-	kvm_pmu_update_state(vcpu);
-}
-
-/*
- * When perf interrupt is an NMI, we cannot safely notify the vcpu corresponding
- * to the event.
- * This is why we need a callback to do it once outside of the NMI context.
- */
-static void kvm_pmu_perf_overflow_notify_vcpu(struct irq_work *work)
-{
-	struct kvm_vcpu *vcpu;
-
-	vcpu = container_of(work, struct kvm_vcpu, arch.pmu.overflow_work);
-	kvm_vcpu_kick(vcpu);
-}
-
 /*
  * Perform an increment on any of the counters described in @mask,
  * generating the overflow if required, and propagate it as a chained
@@ -771,133 +622,6 @@ void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
 	kvm_pmu_create_perf_event(pmc);
 }
 
-void kvm_host_pmu_init(struct arm_pmu *pmu)
-{
-	struct arm_pmu_entry *entry;
-
-	/*
-	 * Check the sanitised PMU version for the system, as KVM does not
-	 * support implementations where PMUv3 exists on a subset of CPUs.
-	 */
-	if (!pmuv3_implemented(kvm_arm_pmu_get_pmuver_limit()))
-		return;
-
-	guard(mutex)(&arm_pmus_lock);
-
-	entry = kmalloc_obj(*entry);
-	if (!entry)
-		return;
-
-	entry->arm_pmu = pmu;
-	list_add_tail(&entry->entry, &arm_pmus);
-}
-
-static struct arm_pmu *kvm_pmu_probe_armpmu(void)
-{
-	struct arm_pmu_entry *entry;
-	struct arm_pmu *pmu;
-	int cpu;
-
-	guard(mutex)(&arm_pmus_lock);
-
-	/*
-	 * It is safe to use a stale cpu to iterate the list of PMUs so long as
-	 * the same value is used for the entirety of the loop. Given this, and
-	 * the fact that no percpu data is used for the lookup there is no need
-	 * to disable preemption.
-	 *
-	 * It is still necessary to get a valid cpu, though, to probe for the
-	 * default PMU instance as userspace is not required to specify a PMU
-	 * type. In order to uphold the preexisting behavior KVM selects the
-	 * PMU instance for the core during vcpu init. A dependent use
-	 * case would be a user with disdain of all things big.LITTLE that
-	 * affines the VMM to a particular cluster of cores.
-	 *
-	 * In any case, userspace should just do the sane thing and use the UAPI
-	 * to select a PMU type directly. But, be wary of the baggage being
-	 * carried here.
-	 */
-	cpu = raw_smp_processor_id();
-	list_for_each_entry(entry, &arm_pmus, entry) {
-		pmu = entry->arm_pmu;
-
-		if (cpumask_test_cpu(cpu, &pmu->supported_cpus))
-			return pmu;
-	}
-
-	return NULL;
-}
-
-static u64 __compute_pmceid(struct arm_pmu *pmu, bool pmceid1)
-{
-	u32 hi[2], lo[2];
-
-	bitmap_to_arr32(lo, pmu->pmceid_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS);
-	bitmap_to_arr32(hi, pmu->pmceid_ext_bitmap, ARMV8_PMUV3_MAX_COMMON_EVENTS);
-
-	return ((u64)hi[pmceid1] << 32) | lo[pmceid1];
-}
-
-static u64 compute_pmceid0(struct kvm_vcpu *vcpu)
-{
-	u64 val = __compute_pmceid(vcpu->kvm->arch.arm_pmu, 0);
-
-	/* always support SW_INCR */
-	val |= BIT(ARMV8_PMUV3_PERFCTR_SW_INCR);
-	/* always support CHAIN */
-	val |= BIT(ARMV8_PMUV3_PERFCTR_CHAIN);
-	return val;
-}
-
-static u64 compute_pmceid1(struct kvm_vcpu *vcpu)
-{
-	u64 val = __compute_pmceid(vcpu->kvm->arch.arm_pmu, 1);
-
-	/*
-	 * If KVM_ARM_VCPU_PMU_V3_STRICT is not set, PMMIR_EL1 is
-	 * unconditionally RAZ, so don't advertise STALL_SLOT* events.
-	 */
-	if (!kvm_vcpu_has_pmuv3_strict(vcpu))
-		val &= ~(BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32) |
-			 BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND - 32) |
-			 BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND - 32));
-
-	return val;
-}
-
-u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
-{
-	unsigned long *bmap = vcpu->kvm->arch.pmu_filter;
-	u64 val, mask = 0;
-	int base, i, nr_events;
-
-	if (!pmceid1) {
-		val = compute_pmceid0(vcpu);
-		base = 0;
-	} else {
-		val = compute_pmceid1(vcpu);
-		base = 32;
-	}
-
-	if (!bmap)
-		return val;
-
-	nr_events = kvm_pmu_event_mask(vcpu->kvm) + 1;
-
-	for (i = 0; i < 32; i += 8) {
-		u64 byte;
-
-		byte = bitmap_get_value8(bmap, base + i);
-		mask |= byte << i;
-		if (nr_events >= (0x4000 + base + 32)) {
-			byte = bitmap_get_value8(bmap, 0x4000 + base + i);
-			mask |= byte << (32 + i);
-		}
-	}
-
-	return val & mask;
-}
-
 void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu)
 {
 	u64 mask = kvm_pmu_implemented_counter_mask(vcpu);
@@ -909,430 +633,6 @@ void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu)
 	kvm_pmu_reprogram_counter_mask(vcpu, mask);
 }
 
-int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)
-{
-	if (!vcpu->arch.pmu.created)
-		return -EINVAL;
-
-	/*
-	 * A valid interrupt configuration for the PMU is either to have a
-	 * properly configured interrupt number and using an in-kernel
-	 * irqchip, or to not have an in-kernel GIC and not set an IRQ.
-	 */
-	if (irqchip_in_kernel(vcpu->kvm)) {
-		int irq = vcpu->arch.pmu.irq_num;
-		/*
-		 * If we are using an in-kernel vgic, at this point we know
-		 * the vgic will be initialized, so we can check the PMU irq
-		 * number against the dimensions of the vgic and make sure
-		 * it's valid.
-		 */
-		if (!irq_is_ppi(vcpu->kvm, irq) &&
-		    !vgic_valid_spi(vcpu->kvm, irq))
-			return -EINVAL;
-	} else if (kvm_arm_pmu_irq_initialized(vcpu)) {
-		   return -EINVAL;
-	}
-
-	return 0;
-}
-
-static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)
-{
-	/* Only possible when using KVM_ARM_VCPU_PMU_V3_STRICT */
-	if (!vcpu->kvm->arch.arm_pmu)
-		return -ENXIO;
-
-	if (irqchip_in_kernel(vcpu->kvm)) {
-		int ret;
-
-		/*
-		 * If using the PMU with an in-kernel virtual GIC
-		 * implementation, we require the GIC to be already
-		 * initialized when initializing the PMU.
-		 */
-		if (!vgic_initialized(vcpu->kvm))
-			return -ENODEV;
-
-		if (!kvm_arm_pmu_irq_initialized(vcpu)) {
-			if (!vgic_is_v5(vcpu->kvm))
-				return -ENXIO;
-
-			/* Use the architected irq number for GICv5. */
-			vcpu->arch.pmu.irq_num = KVM_ARMV8_PMU_GICV5_IRQ;
-		}
-
-		ret = kvm_vgic_set_owner(vcpu, vcpu->arch.pmu.irq_num,
-					 &vcpu->arch.pmu);
-		if (ret)
-			return ret;
-	}
-
-	init_irq_work(&vcpu->arch.pmu.overflow_work,
-		      kvm_pmu_perf_overflow_notify_vcpu);
-
-	vcpu->arch.pmu.created = true;
-	return 0;
-}
-
-/*
- * For one VM the interrupt type must be same for each vcpu.
- * As a PPI, the interrupt number is the same for all vcpus,
- * while as an SPI it must be a separate number per vcpu.
- */
-static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
-{
-	unsigned long i;
-	struct kvm_vcpu *vcpu;
-
-	/* On GICv5, the PMUIRQ is architecturally mandated to be PPI 23 */
-	if (vgic_is_v5(kvm) && irq != KVM_ARMV8_PMU_GICV5_IRQ)
-		return false;
-
-	kvm_for_each_vcpu(i, vcpu, kvm) {
-		if (!kvm_arm_pmu_irq_initialized(vcpu))
-			continue;
-
-		if (irq_is_ppi(vcpu->kvm, irq)) {
-			if (vcpu->arch.pmu.irq_num != irq)
-				return false;
-		} else {
-			if (vcpu->arch.pmu.irq_num == irq)
-				return false;
-		}
-	}
-
-	return true;
-}
-
-/**
- * kvm_arm_pmu_get_max_counters - Return the max number of PMU counters.
- * @kvm: The kvm pointer
- */
-u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)
-{
-	struct arm_pmu *arm_pmu = kvm->arch.arm_pmu;
-
-	/*
-	 * Under KVM_ARM_VCPU_PMU_V3_STRICT no PMU exists until userspace sets
-	 * one, so this can be reached before arm_pmu is set. Report no
-	 * counters in that case.
-	 */
-	if (!arm_pmu)
-		return 0;
-
-	/*
-	 * PMUv3 requires that all event counters are capable of counting any
-	 * event, though the same may not be true of non-PMUv3 hardware.
-	 */
-	if (cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS))
-		return 1;
-
-	/*
-	 * The arm_pmu->cntr_mask considers the fixed counter(s) as well.
-	 * Ignore those and return only the general-purpose counters.
-	 */
-	return bitmap_weight(arm_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS);
-}
-
-static void kvm_arm_set_nr_counters(struct kvm *kvm, unsigned int nr)
-{
-	kvm->arch.nr_pmu_counters = nr;
-
-	/* Reset MDCR_EL2.HPMN behind the vcpus' back... */
-	if (test_bit(KVM_ARM_VCPU_HAS_EL2, kvm->arch.vcpu_features)) {
-		struct kvm_vcpu *vcpu;
-		unsigned long i;
-
-		kvm_for_each_vcpu(i, vcpu, kvm) {
-			u64 val = __vcpu_sys_reg(vcpu, MDCR_EL2);
-			val &= ~MDCR_EL2_HPMN;
-			val |= FIELD_PREP(MDCR_EL2_HPMN, kvm->arch.nr_pmu_counters);
-			__vcpu_assign_sys_reg(vcpu, MDCR_EL2, val);
-		}
-	}
-}
-
-static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
-{
-	lockdep_assert_held(&kvm->arch.config_lock);
-
-	kvm->arch.arm_pmu = arm_pmu;
-	kvm_arm_set_nr_counters(kvm, kvm_arm_pmu_get_max_counters(kvm));
-}
-
-/**
- * kvm_arm_set_default_pmu - No PMU set and KVM_ARM_VCPU_PMU_V3_STRICT not
- * set, get the default one.
- * @kvm: The kvm pointer
- *
- * The observant among you will notice that the supported_cpus
- * mask does not get updated for the default PMU even though it
- * is quite possible the selected instance supports only a
- * subset of cores in the system. This is intentional, and
- * upholds the preexisting behavior on heterogeneous systems
- * where vCPUs can be scheduled on any core but the guest
- * counters could stop working.
- */
-int kvm_arm_set_default_pmu(struct kvm *kvm)
-{
-	struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu();
-
-	if (!arm_pmu)
-		return -ENODEV;
-
-	kvm_arm_set_pmu(kvm, arm_pmu);
-	return 0;
-}
-
-static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id)
-{
-	struct kvm *kvm = vcpu->kvm;
-	struct arm_pmu_entry *entry;
-	struct arm_pmu *arm_pmu;
-	int ret = -ENXIO;
-
-	lockdep_assert_held(&kvm->arch.config_lock);
-	mutex_lock(&arm_pmus_lock);
-
-	list_for_each_entry(entry, &arm_pmus, entry) {
-		arm_pmu = entry->arm_pmu;
-		if (arm_pmu->pmu.type == pmu_id) {
-			if (kvm_vm_has_ran_once(kvm) ||
-			    (kvm->arch.pmu_filter && kvm->arch.arm_pmu != arm_pmu)) {
-				ret = -EBUSY;
-				break;
-			}
-
-			kvm_arm_set_pmu(kvm, arm_pmu);
-			cpumask_copy(kvm->arch.supported_cpus, &arm_pmu->supported_cpus);
-
-			/*
-			 * Since a specific PMU is explicitly selected,
-			 * PMMIR_EL1.SLOTS is deterministic to the guest.
-			 * If KVM_ARM_VCPU_PMU_V3_STRICT is set, snapshot
-			 * the value to allow the guest to read it.
-			 */
-			if (kvm_vcpu_has_pmuv3_strict(vcpu))
-				kvm->arch.pmmir_slots =
-					FIELD_GET(ARMV8_PMU_SLOTS,
-						  arm_pmu->reg_pmmir);
-			ret = 0;
-			break;
-		}
-	}
-
-	mutex_unlock(&arm_pmus_lock);
-	return ret;
-}
-
-static int kvm_arm_pmu_v3_set_nr_counters(struct kvm_vcpu *vcpu, unsigned int n)
-{
-	struct kvm *kvm = vcpu->kvm;
-
-	if (!kvm->arch.arm_pmu)
-		return -EINVAL;
-
-	if (n > kvm_arm_pmu_get_max_counters(kvm))
-		return -EINVAL;
-
-	kvm_arm_set_nr_counters(kvm, n);
-	return 0;
-}
-
-int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
-{
-	struct kvm *kvm = vcpu->kvm;
-
-	lockdep_assert_held(&kvm->arch.config_lock);
-
-	if (!kvm_vcpu_has_pmu(vcpu))
-		return -ENODEV;
-
-	if (vcpu->arch.pmu.created)
-		return -EBUSY;
-
-	switch (attr->attr) {
-	case KVM_ARM_VCPU_PMU_V3_IRQ: {
-		int __user *uaddr = (int __user *)(long)attr->addr;
-		int irq;
-
-		if (!irqchip_in_kernel(kvm))
-			return -EINVAL;
-
-		if (get_user(irq, uaddr))
-			return -EFAULT;
-
-		/* The PMU overflow interrupt can be a PPI or a valid SPI. */
-		if (!(irq_is_ppi(vcpu->kvm, irq) || irq_is_spi(vcpu->kvm, irq)))
-			return -EINVAL;
-
-		if (!pmu_irq_is_valid(kvm, irq))
-			return -EINVAL;
-
-		if (kvm_arm_pmu_irq_initialized(vcpu))
-			return -EBUSY;
-
-		kvm_debug("Set kvm ARM PMU irq: %d\n", irq);
-		vcpu->arch.pmu.irq_num = irq;
-		return 0;
-	}
-	case KVM_ARM_VCPU_PMU_V3_FILTER: {
-		u8 pmuver = kvm_arm_pmu_get_pmuver_limit();
-		struct kvm_pmu_event_filter __user *uaddr;
-		struct kvm_pmu_event_filter filter;
-		int nr_events;
-
-		/*
-		 * Allow userspace to specify an event filter for the entire
-		 * event range supported by PMUVer of the hardware, rather
-		 * than the guest's PMUVer for KVM backward compatibility.
-		 */
-		nr_events = __kvm_pmu_event_mask(pmuver) + 1;
-
-		uaddr = (struct kvm_pmu_event_filter __user *)(long)attr->addr;
-
-		if (copy_from_user(&filter, uaddr, sizeof(filter)))
-			return -EFAULT;
-
-		if (((u32)filter.base_event + filter.nevents) > nr_events ||
-		    (filter.action != KVM_PMU_EVENT_ALLOW &&
-		     filter.action != KVM_PMU_EVENT_DENY))
-			return -EINVAL;
-
-		if (kvm_vm_has_ran_once(kvm))
-			return -EBUSY;
-
-		if (!kvm->arch.arm_pmu)
-			return -ENXIO;
-
-		if (!kvm->arch.pmu_filter) {
-			kvm->arch.pmu_filter = bitmap_alloc(nr_events, GFP_KERNEL_ACCOUNT);
-			if (!kvm->arch.pmu_filter)
-				return -ENOMEM;
-
-			/*
-			 * The default depends on the first applied filter.
-			 * If it allows events, the default is to deny.
-			 * Conversely, if the first filter denies a set of
-			 * events, the default is to allow.
-			 */
-			if (filter.action == KVM_PMU_EVENT_ALLOW)
-				bitmap_zero(kvm->arch.pmu_filter, nr_events);
-			else
-				bitmap_fill(kvm->arch.pmu_filter, nr_events);
-		}
-
-		if (filter.action == KVM_PMU_EVENT_ALLOW)
-			bitmap_set(kvm->arch.pmu_filter, filter.base_event, filter.nevents);
-		else
-			bitmap_clear(kvm->arch.pmu_filter, filter.base_event, filter.nevents);
-
-		return 0;
-	}
-	case KVM_ARM_VCPU_PMU_V3_SET_PMU: {
-		int __user *uaddr = (int __user *)(long)attr->addr;
-		int pmu_id;
-
-		if (get_user(pmu_id, uaddr))
-			return -EFAULT;
-
-		return kvm_arm_pmu_v3_set_pmu(vcpu, pmu_id);
-	}
-	case KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS: {
-		unsigned int __user *uaddr = (unsigned int __user *)(long)attr->addr;
-		unsigned int n;
-
-		if (get_user(n, uaddr))
-			return -EFAULT;
-
-		return kvm_arm_pmu_v3_set_nr_counters(vcpu, n);
-	}
-	case KVM_ARM_VCPU_PMU_V3_INIT:
-		return kvm_arm_pmu_v3_init(vcpu);
-	}
-
-	return -ENXIO;
-}
-
-int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
-{
-	switch (attr->attr) {
-	case KVM_ARM_VCPU_PMU_V3_IRQ: {
-		int __user *uaddr = (int __user *)(long)attr->addr;
-		int irq;
-
-		if (!irqchip_in_kernel(vcpu->kvm))
-			return -EINVAL;
-
-		if (!kvm_vcpu_has_pmu(vcpu))
-			return -ENODEV;
-
-		if (!kvm_arm_pmu_irq_initialized(vcpu))
-			return -ENXIO;
-
-		irq = vcpu->arch.pmu.irq_num;
-		return put_user(irq, uaddr);
-	}
-	}
-
-	return -ENXIO;
-}
-
-int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
-{
-	switch (attr->attr) {
-	case KVM_ARM_VCPU_PMU_V3_IRQ:
-	case KVM_ARM_VCPU_PMU_V3_INIT:
-	case KVM_ARM_VCPU_PMU_V3_FILTER:
-	case KVM_ARM_VCPU_PMU_V3_SET_PMU:
-	case KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS:
-		if (kvm_vcpu_has_pmu(vcpu))
-			return 0;
-	}
-
-	return -ENXIO;
-}
-
-u8 kvm_arm_pmu_get_pmuver_limit(void)
-{
-	unsigned int pmuver;
-
-	pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer,
-			       read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1));
-
-	/*
-	 * Spoof a barebones PMUv3 implementation if the system supports IMPDEF
-	 * traps of the PMUv3 sysregs
-	 */
-	if (cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS))
-		return ID_AA64DFR0_EL1_PMUVer_IMP;
-
-	/*
-	 * Otherwise, treat IMPLEMENTATION DEFINED functionality as
-	 * unimplemented
-	 */
-	if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF)
-		return 0;
-
-	return min(pmuver, ID_AA64DFR0_EL1_PMUVer_V3P5);
-}
-
-/**
- * kvm_vcpu_read_pmcr - Read PMCR_EL0 register for the vCPU
- * @vcpu: The vcpu pointer
- */
-u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
-{
-	u64 pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
-	u64 n = vcpu->kvm->arch.nr_pmu_counters;
-
-	if (vcpu_has_nv(vcpu) && !vcpu_is_el2(vcpu))
-		n = FIELD_GET(MDCR_EL2_HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
-
-	return u64_replace_bits(pmcr, n, ARMV8_PMU_PMCR_N);
-}
-
 void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu)
 {
 	bool reprogrammed = false;
arch/arm64/kvm/pmu.c +705 -0

716 lines not on this page · raw message

include/kvm/arm_pmu.h +8 -1
index 93f41ac77c25f..e1bf29de0b6dd 100644
@@ -50,18 +50,21 @@ struct arm_pmu_entry {
 };
 
 bool kvm_supports_guest_pmuv3(void);
-#define kvm_arm_pmu_irq_initialized(v)	((v)->arch.pmu.irq_num != 0)
+#define kvm_arm_pmu_irq_initialized(v)	((v)->arch.pmu.irq_num >= VGIC_NR_SGIS)
 u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx);
 void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val);
 void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu, u64 select_idx, u64 val);
 u64 kvm_pmu_implemented_counter_mask(struct kvm_vcpu *vcpu);
+u64 kvm_pmu_hyp_counter_mask(struct kvm_vcpu *vcpu);
 u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu);
+u32 kvm_pmu_event_mask(struct kvm *kvm);
 u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1);
 void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu);
 void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu);
 void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u64 val);
 void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu);
 void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu);
+bool kvm_pmu_overflow_status(struct kvm_vcpu *vcpu);
 bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu);
 bool kvm_pmu_update_run(struct kvm_vcpu *vcpu);
 void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val);
@@ -137,6 +140,10 @@ static inline u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu)
 {
 	return 0;
 }
+static inline u32 kvm_pmu_event_mask(struct kvm *kvm)
+{
+	return 0;
+}
 static inline void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu) {}
 static inline void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu) {}
 static inline void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u64 val) {}
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#6 lore raw

Re: [PATCH v9 03/22] KVM: arm64: Reorganize PMU functions

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] kvm: arm_pmu.h relies on VGIC_NR_SGIS without including arm_vgic.h --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] kvm: arm_pmu.h relies on VGIC_NR_SGIS without including arm_vgic.h --

commit 11313a8fb957f8fbf17d341d98c082ca4fdb6a91
Author: Colton Lewis <[email protected]>
KVM: arm64: Reorganize PMU functions

This commit moves several functions that aren't specific to the emulated PMU implementation out of pmu-emul.c and relocates them to the more appropriate pmu.c file where shared PMU functions live.

[ ... ]

include/kvm/arm_pmu.h struct arm_pmu_entry { · line 53

Quoted 10 lines
include/kvm/arm_pmu.h +1 -1
index 93f41ac77c25f..e1bf29de0b6dd 100644
@@ -50,18 +50,21 @@ struct arm_pmu_entry {
 };
 
 bool kvm_supports_guest_pmuv3(void);
-#define kvm_arm_pmu_irq_initialized(v)	((v)->arch.pmu.irq_num != 0)
+#define kvm_arm_pmu_irq_initialized(v)	((v)->arch.pmu.irq_num >= VGIC_NR_SGIS)

[Severity: Low]
Does this make the include/kvm/arm_pmu.h header no longer self-contained?

The updated kvm_arm_pmu_irq_initialized() macro relies on VGIC_NR_SGIS, but the header doesn't appear to include <kvm/arm_vgic.h>. Any future C file that includes this header and expands this macro without also including the VGIC header will likely fail to compile.

Should we add the missing include to ensure the header remains self-contained?

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

PATCH v9 4/22 perf: arm_pmuv3: Generalize counter bitmasks

In reply to Colton Lewis

The OVSR bitmasks are valid for enable and interrupt registers as well as overflow registers. Generalize the names.

---
 drivers/perf/arm_pmuv3.c       |  4 ++--
 include/linux/perf/arm_pmuv3.h | 14 +++++++-------
 tools/include/perf/arm_pmuv3.h | 12 +++++++-----
 3 files changed, 16 insertions(+), 14 deletions(-)
drivers/perf/arm_pmuv3.c +2 -2
index fbd631ec71a65..4a6c1f3bcea1f 100644
@@ -535,7 +535,7 @@ static void armv8pmu_pmcr_write(u64 val)
 
 static int armv8pmu_has_overflowed(u64 pmovsr)
 {
-	return !!(pmovsr & ARMV8_PMU_OVERFLOWED_MASK);
+	return !!(pmovsr & ARMV8_PMU_CNT_MASK_ALL);
 }
 
 static int armv8pmu_counter_has_overflowed(u64 pmnc, int idx)
@@ -771,7 +771,7 @@ static u64 armv8pmu_getreset_flags(void)
 	value = read_pmovsclr();
 
 	/* Write to clear flags */
-	value &= ARMV8_PMU_OVERFLOWED_MASK;
+	value &= ARMV8_PMU_CNT_MASK_ALL;
 	write_pmovsclr(value);
 
 	return value;
include/linux/perf/arm_pmuv3.h +7 -7
index d698efba28a27..fd2a34b4a64d1 100644
@@ -224,14 +224,14 @@
 				 ARMV8_PMU_PMCR_LC | ARMV8_PMU_PMCR_LP)
 
 /*
- * PMOVSR: counters overflow flag status reg
+ * Counter bitmask layouts for overflow, enable, and interrupts
  */
-#define ARMV8_PMU_OVSR_P		GENMASK(30, 0)
-#define ARMV8_PMU_OVSR_C		BIT(31)
-#define ARMV8_PMU_OVSR_F		BIT_ULL(32) /* arm64 only */
-/* Mask for writable bits is both P and C fields */
-#define ARMV8_PMU_OVERFLOWED_MASK	(ARMV8_PMU_OVSR_P | ARMV8_PMU_OVSR_C | \
-					ARMV8_PMU_OVSR_F)
+#define ARMV8_PMU_CNT_MASK_P		GENMASK(30, 0)
+#define ARMV8_PMU_CNT_MASK_C		BIT(31)
+#define ARMV8_PMU_CNT_MASK_F		BIT_ULL(32) /* arm64 only */
+#define ARMV8_PMU_CNT_MASK_ALL		(ARMV8_PMU_CNT_MASK_P | \
+					 ARMV8_PMU_CNT_MASK_C | \
+					 ARMV8_PMU_CNT_MASK_F)
 
 /*
  * PMXEVTYPER: Event selection reg
tools/include/perf/arm_pmuv3.h +7 -5
index 1e397d55384ed..d045b0f3b93fe 100644
@@ -226,12 +226,14 @@
 				 ARMV8_PMU_PMCR_LC | ARMV8_PMU_PMCR_LP)
 
 /*
- * PMOVSR: counters overflow flag status reg
+ * Counter bitmask layouts for overflow, enable, and interrupts
  */
-#define ARMV8_PMU_OVSR_P		GENMASK(30, 0)
-#define ARMV8_PMU_OVSR_C		BIT(31)
-/* Mask for writable bits is both P and C fields */
-#define ARMV8_PMU_OVERFLOWED_MASK	(ARMV8_PMU_OVSR_P | ARMV8_PMU_OVSR_C)
+#define ARMV8_PMU_CNT_MASK_P		GENMASK(30, 0)
+#define ARMV8_PMU_CNT_MASK_C		BIT(31)
+#define ARMV8_PMU_CNT_MASK_F		BIT_ULL(32) /* arm64 only */
+#define ARMV8_PMU_CNT_MASK_ALL		(ARMV8_PMU_CNT_MASK_P | \
+					 ARMV8_PMU_CNT_MASK_C | \
+					 ARMV8_PMU_CNT_MASK_F)
 
 /*
  * PMXEVTYPER: Event selection reg
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#8 lore raw

Re: [PATCH v9 04/22] perf: arm_pmuv3: Generalize counter bitmasks

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

The OVSR bitmasks are valid for enable and interrupt registers as well as overflow registers. Generalize the names.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 5/22 perf: arm_pmuv3: Move counter allocation mask to per-CPU struct pmu_hw_events

In reply to Colton Lewis

In preparation for dynamic per-CPU PMU counter reservations when running KVM guests with a partitioned PMU, move the counter allocation mask from the global struct arm_pmu to per-CPU struct pmu_hw_events.

Initialize cpuc->cntr_mask from the static cpu_pmu->cntr_mask during PMU probe and CPU hotplug startup, and update event counter allocation helpers (armv8pmu_get_single_idx(), armv8pmu_get_chain_idx(), and armv8pmu_get_event_idx()) to query the per-CPU mask.

---
 drivers/perf/arm_pmu.c       |  7 ++++++-
 drivers/perf/arm_pmuv3.c     | 18 +++++++++++++-----
 include/linux/perf/arm_pmu.h |  1 +
 3 files changed, 20 insertions(+), 6 deletions(-)
drivers/perf/arm_pmu.c +6 -1
index 1150695653892..344adcd3521d0 100644
@@ -408,9 +408,11 @@ validate_group(struct perf_event *event)
 
 	/*
 	 * Initialise the fake PMU. We only need to populate the
-	 * used_mask for the purposes of validation.
+	 * used_mask and cntr_mask for the purposes of validation.
 	 */
 	memset(&fake_pmu.used_mask, 0, sizeof(fake_pmu.used_mask));
+	bitmap_copy(fake_pmu.cntr_mask, to_arm_pmu(event->pmu)->cntr_mask,
+		    ARMPMU_MAX_HWEVENTS);
 
 	if (!validate_event(event->pmu, &fake_pmu, leader))
 		return -EINVAL;
@@ -717,6 +719,7 @@ bool arm_pmu_irq_is_nmi(void)
 static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
 {
 	struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
+	struct pmu_hw_events *cpuc = per_cpu_ptr(pmu->hw_events, cpu);
 	int irq;
 
 	if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))
@@ -724,6 +727,8 @@ static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
 	if (pmu->reset)
 		pmu->reset(pmu);
 
+	bitmap_copy(cpuc->cntr_mask, pmu->cntr_mask, ARMPMU_MAX_HWEVENTS);
+
 	irq = armpmu_get_cpu_irq(pmu, cpu);
 	if (irq)
 		per_cpu(cpu_irq_ops, cpu)->enable_pmuirq(irq);
drivers/perf/arm_pmuv3.c +13 -5
index 4a6c1f3bcea1f..49289e5993dd7 100644
@@ -813,7 +813,7 @@ static void armv8pmu_enable_user_access(struct arm_pmu *cpu_pmu)
 		write_pmuacr(mask);
 	} else {
 		/* Clear any unused counters to avoid leaking their contents */
-		for_each_andnot_bit(i, cpu_pmu->cntr_mask, cpuc->used_mask,
+		for_each_andnot_bit(i, cpuc->cntr_mask, cpuc->used_mask,
 				    ARMPMU_MAX_HWEVENTS) {
 			if (i == ARMV8_PMU_CYCLE_IDX)
 				write_pmccntr(0);
@@ -917,7 +917,7 @@ static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
 	 * to prevent skews in group events.
 	 */
 	armv8pmu_stop(cpu_pmu);
-	for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS) {
+	for_each_set_bit(idx, cpuc->cntr_mask, ARMPMU_MAX_HWEVENTS) {
 		struct perf_event *event = cpuc->events[idx];
 		struct hw_perf_event *hwc;
 
@@ -958,7 +958,7 @@ static int armv8pmu_get_single_idx(struct pmu_hw_events *cpuc,
 {
 	int idx;
 
-	for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) {
+	for_each_set_bit(idx, cpuc->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) {
 		if (!test_and_set_bit(idx, cpuc->used_mask))
 			return idx;
 	}
@@ -974,7 +974,7 @@ static int armv8pmu_get_chain_idx(struct pmu_hw_events *cpuc,
 	 * Chaining requires two consecutive event counters, where
 	 * the lower idx must be even.
 	 */
-	for_each_set_bit(idx, cpu_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) {
+	for_each_set_bit(idx, cpuc->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) {
 		if (!(idx & 0x1))
 			continue;
 		if (!test_and_set_bit(idx, cpuc->used_mask)) {
@@ -1042,7 +1042,7 @@ static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
 	 */
 	if ((evtype == ARMV8_PMUV3_PERFCTR_INST_RETIRED) &&
 	    !armv8pmu_event_get_threshold(&event->attr) &&
-	    test_bit(ARMV8_PMU_INSTR_IDX, cpu_pmu->cntr_mask) &&
+	    test_bit(ARMV8_PMU_INSTR_IDX, cpuc->cntr_mask) &&
 	    !armv8pmu_event_want_user_access(event)) {
 		if (!test_and_set_bit(ARMV8_PMU_INSTR_IDX, cpuc->used_mask))
 			return ARMV8_PMU_INSTR_IDX;
@@ -1426,6 +1426,7 @@ static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
 		.present = false,
 	};
 	int ret;
+	int cpu;
 
 	ret = smp_call_function_any(&cpu_pmu->supported_cpus,
 				    __armv8pmu_probe_pmu,
@@ -1441,6 +1442,13 @@ static int armv8pmu_probe_pmu(struct arm_pmu *cpu_pmu)
 		if (ret)
 			return ret;
 	}
+
+	for_each_possible_cpu(cpu) {
+		struct pmu_hw_events *cpuc = per_cpu_ptr(cpu_pmu->hw_events, cpu);
+
+		bitmap_copy(cpuc->cntr_mask, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS);
+	}
+
 	return 0;
 }
 
include/linux/perf/arm_pmu.h +1 -0
index 02d2c7f45b527..be1e345e99a77 100644
@@ -62,6 +62,7 @@ struct pmu_hw_events {
 	 * an event. A 0 means that the counter can be used.
 	 */
 	DECLARE_BITMAP(used_mask, ARMPMU_MAX_HWEVENTS);
+	DECLARE_BITMAP(cntr_mask, ARMPMU_MAX_HWEVENTS);
 
 	/*
 	 * When using percpu IRQs, we need a percpu dev_id. Place it here as we
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#10 lore raw

Re: [PATCH v9 05/22] perf: arm_pmuv3: Move counter allocation mask to per-CPU struct pmu_hw_events

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] perf: arm_pmuv3: info leak of KVM guest counters to host userspace - [Low] perf: arm_pmu…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] perf: arm_pmuv3: info leak of KVM guest counters to host userspace
- [Low] perf: arm_pmu: incomplete migration to per-CPU cntr_mask in cpu_pm_pmu_setup() --

commit 44148c2c0e2e2ea859f2a156493d6c82c8cca8a9
Author: Colton Lewis <[email protected]>

perf: arm_pmuv3: Move counter allocation mask to per-CPU struct pmu_hw_events

This commit introduces a per-CPU counter allocation mask (cntr_mask) to prepare for dynamic PMU partitioning with KVM. It initializes the per-CPU mask from the global mask and updates the allocation functions to use it.

Quoted 4 lines
drivers/perf/arm_pmu.c +0 -0
index 1150695653892..344adcd3521d0 100644

[ ... ]

Quoted 9 lines
drivers/perf/arm_pmu.c +1 -0
@@ -717,6 +719,7 @@ bool arm_pmu_irq_is_nmi(void)
 static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
 {
 	struct arm_pmu *pmu = hlist_entry_safe(node, struct arm_pmu, node);
+	struct pmu_hw_events *cpuc = per_cpu_ptr(pmu->hw_events, cpu);
 	int irq;
 
 	if (!cpumask_test_cpu(cpu, &pmu->supported_cpus))

drivers/perf/arm_pmu.c static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node) · line 734

Quoted 8 lines
drivers/perf/arm_pmu.c +2 -0
@@ -724,6 +727,8 @@ static int arm_perf_starting_cpu(unsigned int cpu, struct hlist_node *node)
 	if (pmu->reset)
 		pmu->reset(pmu);
 
+	bitmap_copy(cpuc->cntr_mask, pmu->cntr_mask, ARMPMU_MAX_HWEVENTS);
+
 	irq = armpmu_get_cpu_irq(pmu, cpu);
 	if (irq)
 		per_cpu(cpu_irq_ops, cpu)->enable_pmuirq(irq);

[Severity: Low]
Is there a missing update to cpu_pm_pmu_setup() in this file?

Looking at cpu_pm_pmu_setup(), it continues to use the global mask rather than the newly introduced per-CPU mask:

drivers/perf/arm_pmu.c:cpu_pm_pmu_setup() {
    ...
	for_each_set_bit(idx, armpmu->cntr_mask, ARMPMU_MAX_HWEVENTS) {
		event = hw_events->events[idx];
		if (!event)
			continue;
    ...
}

While hw_events->events[idx] safely evaluates to NULL for counters partitioned to KVM, should this loop be updated to iterate over hw_events->cntr_mask for consistency with the new per-CPU abstractions introduced here?

Quoted 4 lines
drivers/perf/arm_pmuv3.c +0 -0
index 4a6c1f3bcea1f..49289e5993dd7 100644

[ ... ]

drivers/perf/arm_pmuv3.c static void armv8pmu_enable_user_access(struct arm_pmu *cpu_pmu) · line 819

Quoted 9 lines
drivers/perf/arm_pmuv3.c +1 -1
@@ -813,7 +813,7 @@ static void armv8pmu_enable_user_access(struct arm_pmu *cpu_pmu)
 		write_pmuacr(mask);
 	} else {
 		/* Clear any unused counters to avoid leaking their contents */
-		for_each_andnot_bit(i, cpu_pmu->cntr_mask, cpuc->used_mask,
+		for_each_andnot_bit(i, cpuc->cntr_mask, cpuc->used_mask,
 				    ARMPMU_MAX_HWEVENTS) {
 			if (i == ARMV8_PMU_CYCLE_IDX)
 				write_pmccntr(0);

[Severity: High]
Does this change create an information leak across VM boundaries?

On pre-v3.9 hardware, armv8pmu_enable_user_access() enables EL0 counter access globally. It clears unused counters to prevent leaking kernel state. However, by changing the loop to iterate over the per-CPU cpuc->cntr_mask instead of the global cpu_pmu->cntr_mask, counters reserved for KVM (which are dynamically removed from cpuc->cntr_mask) will be skipped.

Since KVM does not zero hardware counters on vcpu_put, won't this allow unprivileged host userspace to read the leftover guest counter values?

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

PATCH v9 6/22 perf: arm_pmuv3: Check cntr_mask before using pmccntr

In reply to Colton Lewis

When the PMU is partitioned, PMCCNTR_EL0 is reserved for the guest and removed from cpuc->cntr_mask. Move the PMCCNTR_EL0 availability checks (cpuc->cntr_mask and cpuc->used_mask) into armv8pmu_can_use_pmccntr() and un-nest the 64-bit user-access fallback check in armv8pmu_get_event_idx() so host CPU_CYCLES events fall back to general-purpose event counters when PMCCNTR_EL0 is unavailable.

---
 drivers/perf/arm_pmuv3.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/perf/arm_pmuv3.c +11 -6
index 49289e5993dd7..c15a34684137a 100644
@@ -1015,6 +1015,10 @@ static bool armv8pmu_can_use_pmccntr(struct pmu_hw_events *cpuc,
 	if (cpu_pmu->avoid_pmccntr)
 		return false;
 
+	if (!test_bit(ARMV8_PMU_CYCLE_IDX, cpuc->cntr_mask) ||
+	    test_bit(ARMV8_PMU_CYCLE_IDX, cpuc->used_mask))
+		return false;
+
 	return true;
 }
 
@@ -1027,14 +1031,15 @@ static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,
 
 	/* Always prefer to place a cycle counter into the cycle counter. */
 	if (armv8pmu_can_use_pmccntr(cpuc, event)) {
-		if (!test_and_set_bit(ARMV8_PMU_CYCLE_IDX, cpuc->used_mask))
-			return ARMV8_PMU_CYCLE_IDX;
-		else if (armv8pmu_event_is_64bit(event) &&
-			   armv8pmu_event_want_user_access(event) &&
-			   !armv8pmu_has_long_event(cpu_pmu))
-				return -EAGAIN;
+		set_bit(ARMV8_PMU_CYCLE_IDX, cpuc->used_mask);
+		return ARMV8_PMU_CYCLE_IDX;
 	}
 
+	if (armv8pmu_event_is_64bit(event) &&
+	    armv8pmu_event_want_user_access(event) &&
+	    !armv8pmu_has_long_event(cpu_pmu))
+		return -EAGAIN;
+
 	/*
 	 * Always prefer to place a instruction counter into the instruction counter,
 	 * but don't expose the instruction counter to userspace access as userspace
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#12 lore raw

Re: [PATCH v9 06/22] perf: arm_pmuv3: Check cntr_mask before using pmccntr

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

When the PMU is partitioned, PMCCNTR_EL0 is reserved for the guest and removed from cpuc->cntr_mask. Move the PMCCNTR_EL0 availability checks (cpuc->cntr_mask and cpuc->used_mask) into armv8pmu_can_use_pmccntr() and un-nest the 64-bit user-access fallback check in armv8pmu_get_event_idx() so host CPU_CYCLES events fall back to general-purpose event counters when PMCCNTR_EL0 is unavailable.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 7/22 perf: arm_pmuv3: Allocate counter indices from high to low

In reply to Colton Lewis

To minimize collisions between host and guest counters, allocate host counters from high to low. How the pivot HPMN is defined to partition the counters gives the guest the low index counters.

Verify both the upper odd counter and lower even counter are present in cpuc->cntr_mask when allocating 64-bit chained events so that an odd HPMN boundary does not leak a guest counter into a host chained pair.

---
 drivers/perf/arm_pmuv3.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/perf/arm_pmuv3.c +16 -9
index c15a34684137a..4fcdae8021a56 100644
@@ -958,10 +958,12 @@ static int armv8pmu_get_single_idx(struct pmu_hw_events *cpuc,
 {
 	int idx;
 
-	for_each_set_bit(idx, cpuc->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) {
-		if (!test_and_set_bit(idx, cpuc->used_mask))
+	for (idx = ARMV8_PMU_MAX_GENERAL_COUNTERS - 1; idx >= 0; idx--) {
+		if (test_bit(idx, cpuc->cntr_mask) &&
+		    !test_and_set_bit(idx, cpuc->used_mask))
 			return idx;
 	}
+
 	return -EAGAIN;
 }
 
@@ -974,17 +976,22 @@ static int armv8pmu_get_chain_idx(struct pmu_hw_events *cpuc,
 	 * Chaining requires two consecutive event counters, where
 	 * the lower idx must be even.
 	 */
-	for_each_set_bit(idx, cpuc->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS) {
+	for (idx = ARMV8_PMU_MAX_GENERAL_COUNTERS - 1; idx >= 0; idx--) {
 		if (!(idx & 0x1))
 			continue;
-		if (!test_and_set_bit(idx, cpuc->used_mask)) {
-			/* Check if the preceding even counter is available */
-			if (!test_and_set_bit(idx - 1, cpuc->used_mask))
-				return idx;
-			/* Release the Odd counter */
-			clear_bit(idx, cpuc->used_mask);
+
+		if (test_bit(idx, cpuc->cntr_mask) &&
+		    test_bit(idx - 1, cpuc->cntr_mask)) {
+			if (!test_and_set_bit(idx, cpuc->used_mask)) {
+				/* Check if the preceding even counter is available */
+				if (!test_and_set_bit(idx - 1, cpuc->used_mask))
+					return idx;
+				/* Release the Odd counter */
+				clear_bit(idx, cpuc->used_mask);
+			}
 		}
 	}
+
 	return -EAGAIN;
 }
 
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#14 lore raw

Re: [PATCH v9 07/22] perf: arm_pmuv3: Allocate counter indices from high to low

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 9 lines

To minimize collisions between host and guest counters, allocate host counters from high to low. How the pivot HPMN is defined to partition the counters gives the guest the low index counters.

Verify both the upper odd counter and lower even counter are present in cpuc->cntr_mask when allocating 64-bit chained events so that an odd HPMN boundary does not leak a guest counter into a host chained pair.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 8/22 KVM: arm64: Add initial scaffolding for Partitioned PMU

In reply to Colton Lewis

Add the foundational helpers for Partitioned PMU support in arch/arm64/kvm/pmu-direct.c:
- has_host_pmu_partition_support() and has_kvm_pmu_partition_support() to check VHE and PMUv3 availability.
- kvm_pmu_partition_enable() and kvm_pmu_is_partitioned() to track per-VM partitioning state via KVM_ARCH_FLAG_PARTITION_PMU_ENABLED.
- kvm_pmu_host_counter_mask() and kvm_pmu_guest_counter_mask() to compute the bitmasks of host-reserved (HPMN..N-1) and guest-reserved (0..HPMN-1 and PMCCNTR_EL0) hardware counters.

---
 arch/arm/include/asm/arm_pmuv3.h  |   8 ++
 arch/arm64/include/asm/kvm_host.h |   2 +
 arch/arm64/kvm/Makefile           |   2 +-
 arch/arm64/kvm/pmu-direct.c       | 120 ++++++++++++++++++++++++++++++
 include/kvm/arm_pmu.h             |  32 ++++++++
 5 files changed, 163 insertions(+), 1 deletion(-)
 create mode 100644 arch/arm64/kvm/pmu-direct.c
arch/arm/include/asm/arm_pmuv3.h +8 -0
index ecfede0c03486..cec26c12bc009 100644
@@ -221,12 +221,20 @@ static inline bool kvm_pmu_counter_deferred(struct perf_event_attr *attr)
 	return false;
 }
 
+static inline bool has_host_pmu_partition_support(void)
+{
+	return false;
+}
 static inline bool kvm_set_pmuserenr(u64 val)
 {
 	return false;
 }
 
 static inline void kvm_vcpu_pmu_resync_el0(void) {}
+static inline u64 kvm_pmu_host_counter_mask(void)
+{
+	return ~0;
+}
 
 /* PMU Version in DFR Register */
 #define ARMV8_PMU_DFR_VER_NI        0
arch/arm64/include/asm/kvm_host.h +2 -0
index 217c36af1ca51..98143daab6a55 100644
@@ -367,6 +367,8 @@ struct kvm_arch {
 #define KVM_ARCH_FLAG_WRITABLE_IMP_ID_REGS		10
 	/* Unhandled SEAs are taken to userspace */
 #define KVM_ARCH_FLAG_EXIT_SEA				11
+	/* Partitioned PMU Enabled */
+#define KVM_ARCH_FLAG_PARTITION_PMU_ENABLED		12
 	unsigned long flags;
 
 	/* VM-wide vCPU feature set */
arch/arm64/kvm/Makefile +1 -1
index 59612d2f277c1..0e7b8e65c4c93 100644
@@ -26,7 +26,7 @@ kvm-y += arm.o mmu.o mmio.o psci.o hypercalls.o pvtime.o \
 	 vgic/vgic-its.o vgic/vgic-debug.o vgic/vgic-v3-nested.o \
 	 vgic/vgic-v5.o
 
-kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu.o
+kvm-$(CONFIG_HW_PERF_EVENTS)  += pmu-emul.o pmu-direct.o pmu.o
 kvm-$(CONFIG_ARM64_PTR_AUTH)  += pauth.o
 kvm-$(CONFIG_PTDUMP_STAGE2_DEBUGFS) += ptdump.o
 
arch/arm64/kvm/pmu-direct.c new file +120 -0

120 lines not on this page · raw message

include/kvm/arm_pmu.h +32 -0
index e1bf29de0b6dd..45ed338ee7504 100644
@@ -50,6 +50,7 @@ struct arm_pmu_entry {
 };
 
 bool kvm_supports_guest_pmuv3(void);
+bool has_host_pmu_partition_support(void);
 #define kvm_arm_pmu_irq_initialized(v)	((v)->arch.pmu.irq_num >= VGIC_NR_SGIS)
 u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx);
 void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val);
@@ -94,6 +95,11 @@ void kvm_vcpu_pmu_resync_el0(void);
 #define kvm_vcpu_has_pmuv3_strict(vcpu)				\
 	(vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3_STRICT))
 
+bool has_kvm_pmu_partition_support(void);
+void kvm_pmu_partition_enable(struct kvm *kvm, bool enable);
+bool kvm_pmu_is_partitioned(struct kvm *kvm);
+u64 kvm_pmu_host_counter_mask(void);
+u64 kvm_pmu_guest_counter_mask(void);
 /*
  * Updates the vcpu's view of the pmu events for this cpu.
  * Must be called before every vcpu run after disabling interrupts, to ensure
@@ -122,12 +128,21 @@ static inline bool kvm_supports_guest_pmuv3(void)
 	return false;
 }
 
+static inline bool has_host_pmu_partition_support(void)
+{
+	return false;
+}
+
 #define kvm_arm_pmu_irq_initialized(v)	(false)
 static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu,
 					    u64 select_idx)
 {
 	return 0;
 }
+static inline bool kvm_pmu_is_partitioned(struct kvm *kvm)
+{
+	return false;
+}
 static inline void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu,
 					     u64 select_idx, u64 val) {}
 static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,
@@ -226,6 +241,23 @@ static inline bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int id
 
 static inline void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu) {}
 
+static inline u64 kvm_pmu_host_counter_mask(void)
+{
+	return ~0;
+}
+
+static inline u64 kvm_pmu_guest_counter_mask(void)
+{
+	return 0;
+}
+
+static inline bool has_kvm_pmu_partition_support(void)
+{
+	return false;
+}
+
+static inline void kvm_pmu_partition_enable(struct kvm *kvm, bool enable) {}
+
 #endif
 
 #endif
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#16 lore raw

Re: [PATCH v9 08/22] KVM: arm64: Add initial scaffolding for Partitioned PMU

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Add the foundational helpers for Partitioned PMU support in arch/arm64/kvm/pmu-direct.c:
- has_host_pmu_partition_support() and has_kvm_pmu_partition_support() to check VHE and PMUv3 availability.
- kvm_pmu_partition_enable() and kvm_pmu_is_partitioned() to track per-VM partitioning state via KVM_ARCH_FLAG_PARTITION_PMU_ENABLED.
- kvm_pmu_host_counter_mask() and kvm_pmu_guest_counter_mask() to compute the bitmasks of host-reserved (HPMN..N-1) and guest-reserved (0..HPMN-1 and PMCCNTR_EL0) hardware counters.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 9/22 KVM: arm64: Set up FGT for Partitioned PMU

In reply to Colton Lewis

In order to gain the best performance benefit from partitioning the PMU, utilize fine grain traps (FEAT_FGT and FEAT_FGT2) to avoid trapping common PMU register accesses by the guest to remove that overhead.

Untrapped:
- PMCR_EL0
- PMUSERENR_EL0
- PMSELR_EL0
- PMCCNTR_EL0
- PMCNTEN_EL0
- PMINTEN_EL1
- PMEVCNTRn_EL0

These are safe to untrap because writing MDCR_EL2.HPMN as this series will do limits the effect of writes to any of these registers to the partition of counters 0..HPMN-1. Reads from these registers will not leak information from between guests as all these registers are context swapped by a later patch in this series. Reads from these registers also do not leak any information about the host's hardware beyond what is promised by PMUv3.

Trapped:
- PMOVS_EL0
- PMEVTYPERn_EL0
- PMCCFILTR_EL0
- PMICNTR_EL0
- PMICFILTR_EL0
- PMCEIDn_EL0
- PMMIR_EL1

PMOVS remains trapped so KVM can track overflow IRQs that will need to be injected into the guest.

PMICNTR and PMICFILTR remain trapped because KVM is not handling them yet.

PMEVTYPERn remains trapped so KVM can limit which events guests can count, such as disallowing counting at EL2. PMCCFILTR and PMICFILTR are special cases of the same.

PMCEIDn and PMMIR remain trapped because they can leak information specific to the host hardware implementation.

---
 arch/arm64/kvm/config.c | 49 ++++++++++++++++++++++++++++++++++++++---
 1 file changed, 46 insertions(+), 3 deletions(-)
arch/arm64/kvm/config.c +46 -3
index 1053676551aff..f00ef9d598a95 100644
@@ -1708,12 +1708,55 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu)
 		*vcpu_fgt(vcpu, HFGWTR_EL2) |= HFGWTR_EL2_TCR_EL1;
 }
 
+static void __compute_hdfgrtr(struct kvm_vcpu *vcpu)
+{
+	__compute_fgt(vcpu, HDFGRTR_EL2);
+
+	if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		*vcpu_fgt(vcpu, HDFGRTR_EL2) |=
+			(HDFGRTR_EL2_PMOVS |
+			 HDFGRTR_EL2_PMCCFILTR_EL0 |
+			 HDFGRTR_EL2_PMEVTYPERn_EL0 |
+			 HDFGRTR_EL2_PMCEIDn_EL0 |
+			 HDFGRTR_EL2_PMMIR_EL1) & ~hdfgrtr_masks.res0;
+	}
+}
+
 static void __compute_hdfgwtr(struct kvm_vcpu *vcpu)
 {
 	__compute_fgt(vcpu, HDFGWTR_EL2);
 
 	if (is_hyp_ctxt(vcpu))
 		*vcpu_fgt(vcpu, HDFGWTR_EL2) |= HDFGWTR_EL2_MDSCR_EL1;
+
+	if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		*vcpu_fgt(vcpu, HDFGWTR_EL2) |=
+			(HDFGWTR_EL2_PMOVS |
+			 HDFGWTR_EL2_PMCCFILTR_EL0 |
+			 HDFGWTR_EL2_PMEVTYPERn_EL0) & ~hdfgwtr_masks.res0;
+	}
+}
+
+static void __compute_hdfgrtr2(struct kvm_vcpu *vcpu)
+{
+	__compute_fgt(vcpu, HDFGRTR2_EL2);
+
+	if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		*vcpu_fgt(vcpu, HDFGRTR2_EL2) &=
+			~(HDFGRTR2_EL2_nPMICFILTR_EL0 |
+			  HDFGRTR2_EL2_nPMICNTR_EL0) | hdfgrtr2_masks.res1;
+	}
+}
+
+static void __compute_hdfgwtr2(struct kvm_vcpu *vcpu)
+{
+	__compute_fgt(vcpu, HDFGWTR2_EL2);
+
+	if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		*vcpu_fgt(vcpu, HDFGWTR2_EL2) &=
+			~(HDFGWTR2_EL2_nPMICFILTR_EL0 |
+			  HDFGWTR2_EL2_nPMICNTR_EL0) | hdfgwtr2_masks.res1;
+	}
 }
 
 static void __compute_ich_hfgrtr(struct kvm_vcpu *vcpu)
@@ -1750,7 +1793,7 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)
 	__compute_fgt(vcpu, HFGRTR_EL2);
 	__compute_hfgwtr(vcpu);
 	__compute_fgt(vcpu, HFGITR_EL2);
-	__compute_fgt(vcpu, HDFGRTR_EL2);
+	__compute_hdfgrtr(vcpu);
 	__compute_hdfgwtr(vcpu);
 	__compute_fgt(vcpu, HAFGRTR_EL2);
 
@@ -1758,8 +1801,8 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)
 		__compute_fgt(vcpu, HFGRTR2_EL2);
 		__compute_fgt(vcpu, HFGWTR2_EL2);
 		__compute_fgt(vcpu, HFGITR2_EL2);
-		__compute_fgt(vcpu, HDFGRTR2_EL2);
-		__compute_fgt(vcpu, HDFGWTR2_EL2);
+		__compute_hdfgrtr2(vcpu);
+		__compute_hdfgwtr2(vcpu);
 	}
 
 	if (cpus_have_final_cap(ARM64_HAS_GICV5_CPUIF)) {
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#18 lore raw

Re: [PATCH v9 09/22] KVM: arm64: Set up FGT for Partitioned PMU

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines

In order to gain the best performance benefit from partitioning the PMU, utilize fine grain traps (FEAT_FGT and FEAT_FGT2) to avoid trapping common PMU register accesses by the guest to remove that overhead.

Untrapped:
- PMCR_EL0
- PMUSERENR_EL0
- PMSELR_EL0
- PMCCNTR_EL0
- PMCNTEN_EL0
- PMINTEN_EL1
- PMEVCNTRn_EL0

Quoted 7 lines

These are safe to untrap because writing MDCR_EL2.HPMN as this series [ ... ]

PMCEIDn and PMMIR remain trapped because they can leak information specific to the host hardware implementation.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 10/22 KVM: arm64: Add Partitioned PMU register trap handlers

In reply to Colton Lewis

We may want a partitioned PMU but not have FEAT_FGT to untrap the specific registers that would normally be untrapped. Add handling for those trapped register accesses that does the right thing if the PMU is partitioned.

For PMOVSCLR_EL0, clear guest overflow bits in both virtual and physical registers. For PMEVTYPER and PMCCFILTR, write to the virtual register; subsequent patches enforce the event filter at vcpu_load() and on trapped register writes.

---
 arch/arm64/kvm/pmu-direct.c |  51 +++++++
 arch/arm64/kvm/sys_regs.c   | 277 +++++++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h       |   7 +
 3 files changed, 287 insertions(+), 48 deletions(-)
arch/arm64/kvm/pmu-direct.c +51 -0
index f224b97823ce5..5045051e91cd3 100644
@@ -64,6 +64,57 @@ bool kvm_pmu_is_partitioned(struct kvm *kvm)
 	return kvm && test_bit(KVM_ARCH_FLAG_PARTITION_PMU_ENABLED, &kvm->arch.flags);
 }
 
+/**
+ * kvm_pmu_direct_pmcr_write() - Handle guest writes to PMCR_EL0
+ * @vcpu: Pointer to vcpu struct
+ * @val: Value written to PMCR_EL0
+ *
+ * Write control bits to physical pmcr_el0 and reset guest-owned general
+ * event counters when PMCR_EL0.P is set.
+ */
+void kvm_pmu_direct_pmcr_write(struct kvm_vcpu *vcpu, u64 val)
+{
+	bool reset_p = val & ARMV8_PMU_PMCR_P;
+	unsigned long mask;
+	int i;
+
+	val &= ~ARMV8_PMU_PMCR_P;
+
+	write_sysreg(val, pmcr_el0);
+
+	if (reset_p) {
+		mask = kvm_pmu_implemented_counter_mask(vcpu) & ~BIT(ARMV8_PMU_CYCLE_IDX);
+
+		if (!vcpu_is_el2(vcpu))
+			mask &= ~kvm_pmu_hyp_counter_mask(vcpu);
+
+		for_each_set_bit(i, &mask, ARMV8_PMU_MAX_GENERAL_COUNTERS)
+			write_pmevcntrn(i, 0);
+	}
+}
+
+/**
+ * kvm_pmu_direct_pmcr_read() - Handle guest reads from PMCR_EL0
+ * @vcpu: Pointer to vcpu struct
+ *
+ * Read physical pmcr_el0 and replace PMCR_EL0.N with the number of
+ * event counters partitioned to the guest.
+ *
+ * Return: Filtered PMCR_EL0 value
+ */
+u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu)
+{
+	u64 n = vcpu->kvm->arch.nr_pmu_counters;
+
+	if (vcpu_has_nv(vcpu) && !vcpu_is_el2(vcpu))
+		n = FIELD_GET(MDCR_EL2_HPMN, __vcpu_sys_reg(vcpu, MDCR_EL2));
+
+	return u64_replace_bits(
+		read_sysreg(pmcr_el0),
+		n,
+		ARMV8_PMU_PMCR_N);
+}
+
 /**
  * kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved counters
  *
arch/arm64/kvm/sys_regs.c +229 -48

384 lines not on this page · raw message

include/kvm/arm_pmu.h +7 -0
index 45ed338ee7504..a24788243ac99 100644
@@ -98,6 +98,8 @@ void kvm_vcpu_pmu_resync_el0(void);
 bool has_kvm_pmu_partition_support(void);
 void kvm_pmu_partition_enable(struct kvm *kvm, bool enable);
 bool kvm_pmu_is_partitioned(struct kvm *kvm);
+void kvm_pmu_direct_pmcr_write(struct kvm_vcpu *vcpu, u64 val);
+u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu);
 u64 kvm_pmu_host_counter_mask(void);
 u64 kvm_pmu_guest_counter_mask(void);
 /*
@@ -143,6 +145,11 @@ static inline bool kvm_pmu_is_partitioned(struct kvm *kvm)
 {
 	return false;
 }
+static inline void kvm_pmu_direct_pmcr_write(struct kvm_vcpu *vcpu, u64 val) {}
+static inline u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu)
+{
+	return 0;
+}
 static inline void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu,
 					     u64 select_idx, u64 val) {}
 static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#20 lore raw

Re: [PATCH v9 10/22] KVM: arm64: Add Partitioned PMU register trap handlers

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] arm64: range-check over enum vcpu_sysreg --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] arm64: range-check over enum vcpu_sysreg
--

commit 35ffc169e71bb4303b4d3e6f955bbc06a22fd20d
Author: Colton Lewis <[email protected]>

KVM: arm64: Add Partitioned PMU register trap handlers

This commit adds handling for partitioned PMU register accesses that are trapped when FEAT_FGT is unavailable. It passes through or filters accesses to registers like PMCR_EL0, PMEVCNTR, and PMCCFILTR based on the partitioning configuration.

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +0 -0
index f7f750b7a91c4..c8210a8b10389 100644

[ ... ]

Quoted 6 lines
arch/arm64/kvm/sys_regs.c +2 -0
@@ -1092,9 +1092,204 @@ static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
 	return __vcpu_sys_reg(vcpu, r->reg);
 }
 
+/**
+ * pmu_reg_write() - Register writes for Partitioned PMU

[ ... ]

Quoted 2 lines
arch/arm64/kvm/sys_regs.c +2 -0
+static void pmu_reg_write(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg, u64 val, bool set)
+{

[ ... ]

Quoted 1 line
arch/arm64/kvm/sys_regs.c +1 -0
+	switch (reg) {

[ ... ]

arch/arm64/kvm/sys_regs.c

Quoted 2 lines
arch/arm64/kvm/sys_regs.c +2 -0
+	case PMEVCNTR0_EL0 ... PMCCNTR_EL0:
+		idx = reg - PMEVCNTR0_EL0;

[Severity: Low]
Is it safe to use a numeric range and arithmetic over enum vcpu_sysreg values here in pmu_reg_write()? Since the enum indexes sparse VNCR-mapped registers by byte offset rather than declaration order, range checks and arithmetic can cover unintended registers. Should this use an explicit per-register allowlist instead?

Quoted 2 lines
arch/arm64/kvm/sys_regs.c +2 -0
+
+		if (kvm_pmu_is_partitioned(vcpu->kvm)) {

[ ... ]

arch/arm64/kvm/sys_regs.c static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) · line 1135

Quoted 2 lines
arch/arm64/kvm/sys_regs.c +2 -0
+	case PMEVTYPER0_EL0 ... PMCCFILTR_EL0:
+		idx = reg - PMEVTYPER0_EL0;

[Severity: Low]
Does this range check and subtraction also rely on contiguous enum values? Like the previous case, should this be converted to an explicit per-register match to avoid issues with the sparse enum numbering?

[ ... ]

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +0 -0
@@ -1276,18 +1471,21 @@ static bool access_pmu_evcntr(struct kvm_vcpu *vcpu,
 			      struct sys_reg_params *p,
 			      const struct sys_reg_desc *r)
 {

[ ... ]

arch/arm64/kvm/sys_regs.c static bool access_pmu_evcntr(struct kvm_vcpu *vcpu, · line 1475

Quoted 5 lines
arch/arm64/kvm/sys_regs.c +2 -0
 	if (!pmu_counter_idx_valid(vcpu, idx))
 		return false;
 
+	reg = PMEVCNTR0_EL0 + idx;
+

[Severity: Low]
Does this arithmetic over the enum value in access_pmu_evcntr() assume a contiguous layout? Using addition on enum vcpu_sysreg values might point to an unintended register due to the sparse byte-offset mapping. Should this be explicitly mapped instead?

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

PATCH v9 11/22 KVM: arm64: Set up MDCR_EL2 to handle a Partitioned PMU

In reply to Colton Lewis

Set up MDCR_EL2 to handle a Partitioned PMU. If partitioned, set the HPME, HPMD, and HCCD bits. If we have the ability to use Fine Grain Traps (FEAT_FGT) also, unset the TPM and TPMCR bits that trap all PMU accesses and set HPMN to the correct number of guest counters so hardware enforces the right values.

Protect vcpu->arch.mdcr_el2 updates with local_irq_save() to prevent races against PMU interrupt handlers modifying MDCR_EL2.HPME.

---
 arch/arm64/kvm/debug.c | 39 +++++++++++++++++++++++++++++++++++----
 1 file changed, 35 insertions(+), 4 deletions(-)
arch/arm64/kvm/debug.c +35 -4
index f4d7b12045e8f..a722fd4594e09 100644
@@ -37,14 +37,16 @@ static int cpu_has_spe(u64 dfr0)
  */
 static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
 {
-	preempt_disable();
+	unsigned long flags;
+
+	local_irq_save(flags);
 
 	/*
 	 * This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK
 	 * to disable guest access to the profiling and trace buffers
 	 */
-	vcpu->arch.mdcr_el2 = FIELD_PREP(MDCR_EL2_HPMN,
-					 *host_data_ptr(nr_event_counters));
+
+	vcpu->arch.mdcr_el2 = FIELD_PREP(MDCR_EL2_HPMN, *host_data_ptr(nr_event_counters));
 	vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
 				MDCR_EL2_TPMS |
 				MDCR_EL2_TTRF |
@@ -52,6 +54,35 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
 				MDCR_EL2_TDRA |
 				MDCR_EL2_TDOSA);
 
+	if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		u8 nr_guest_cntr = vcpu->kvm->arch.nr_pmu_counters;
+		u64 hpmn = FIELD_GET(MDCR_EL2_HPMN, read_sysreg(mdcr_el2));
+		bool hpme;
+
+		if (hpmn < *host_data_ptr(nr_event_counters))
+			hpme = !!(read_sysreg(mdcr_el2) & MDCR_EL2_HPME);
+		else
+			hpme = !!(read_sysreg(pmcr_el0) & ARMV8_PMU_PMCR_E);
+
+		vcpu->arch.mdcr_el2 |= (MDCR_EL2_HPMD | MDCR_EL2_HCCD);
+		if (hpme)
+			vcpu->arch.mdcr_el2 |= MDCR_EL2_HPME;
+
+		/*
+		 * Take out the coarse grain traps if we are using
+		 * fine grain traps and enforce counter access with
+		 * HPMN.
+		 */
+		if (!vcpu_on_unsupported_cpu(vcpu) &&
+		    (cpus_have_final_cap(ARM64_HAS_HPMN0) || nr_guest_cntr > 0)) {
+			vcpu->arch.mdcr_el2 &= ~MDCR_EL2_HPMN;
+			vcpu->arch.mdcr_el2 |= FIELD_PREP(MDCR_EL2_HPMN, nr_guest_cntr);
+
+			if (cpus_have_final_cap(ARM64_HAS_FGT))
+				vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_TPM | MDCR_EL2_TPMCR);
+		}
+	}
+
 	/* Is the VM being debugged by userspace? */
 	if (vcpu->guest_debug)
 		/* Route all software debug exceptions to EL2 */
@@ -70,7 +101,7 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
 	if (has_vhe())
 		write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
 
-	preempt_enable();
+	local_irq_restore(flags);
 }
 
 void kvm_init_host_debug_data(void)
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#22 lore raw

Re: [PATCH v9 11/22] KVM: arm64: Set up MDCR_EL2 to handle a Partitioned PMU

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] KVM: arm64: Missing validation allows 0 PMU counters on CPUs lacking FEAT_HPMN0, breaking host PMU…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] KVM: arm64: Missing validation allows 0 PMU counters on CPUs lacking FEAT_HPMN0, breaking host PMU profiling
- [High] KVM: arm64: local_irq_save() fails to mask pseudo-NMIs, causing TOCTOU race on mdcr_el2 --

commit c7c5ea4b657ecda0bf87202cf7ee0d846216d5cf
Author: Colton Lewis <[email protected]>

KVM: arm64: Set up MDCR_EL2 to handle a Partitioned PMU

This commit sets up MDCR_EL2 to handle partitioned PMUs, configuring the HPME, HPMD, and HCCD bits when a partitioned PMU is used. It also unsets coarse grain PMU traps if fine grain traps are available and protects mdcr_el2 updates using local_irq_save() to prevent races with the PMU NMI handler.

arch/arm64/kvm/debug.c static int cpu_has_spe(u64 dfr0) · line 42

Quoted 12 lines
arch/arm64/kvm/debug.c +3 -1
index f4d7b12045e8f..a722fd4594e09 100644
@@ -37,14 +37,16 @@ static int cpu_has_spe(u64 dfr0)
  */
 static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
 {
-	preempt_disable();
+	unsigned long flags;
+
+	local_irq_save(flags);
        ^^^^^^^^^^^^^^^^^^^^^^

[Severity: High]
Does this lock adequately protect against PMU interrupts?

On ARM64 systems with GICv3, the perf subsystem can use pseudo-NMIs for PMU interrupts, and local_irq_save() does not mask NMIs. This leaves a structurally open race window where a PMU NMI could fire during the read-modify-write phase of vcpu->arch.mdcr_el2 inside kvm_arm_setup_mdcr_el2().

[ ... ]

arch/arm64/kvm/debug.c static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu) · line 77

Quoted 7 lines
arch/arm64/kvm/debug.c +7 -0
+		/*
+		 * Take out the coarse grain traps if we are using
+		 * fine grain traps and enforce counter access with
+		 * HPMN.
+		 */
+		if (!vcpu_on_unsupported_cpu(vcpu) &&
+		    (cpus_have_final_cap(ARM64_HAS_HPMN0) || nr_guest_cntr > 0)) {
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[Severity: High]
Can host userspace break host PMU profiling by configuring 0 guest counters?

If host userspace configures a partitioned PMU with 0 guest counters via KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS on a CPU where FEAT_HPMN0 is unsupported, this condition evaluates to false and skips configuring HPMN.

Because KVM skips HPMN configuration when nr_guest_cntr == 0 on these older CPUs, HPMN defaults to N. This transfers all host counters to PMCR_EL0.E control and renders MDCR_EL2.HPME entirely ineffective for the host, which loses the ability to stop host counters. This continuous hardware counting could lead to NMI storms.

Should KVM reject such invalid configurations on CPUs lacking FEAT_HPMN0 instead?

Quoted 8 lines
arch/arm64/kvm/debug.c +8 -0
+			vcpu->arch.mdcr_el2 &= ~MDCR_EL2_HPMN;
+			vcpu->arch.mdcr_el2 |= FIELD_PREP(MDCR_EL2_HPMN, nr_guest_cntr);
+
+			if (cpus_have_final_cap(ARM64_HAS_FGT))
+				vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_TPM | MDCR_EL2_TPMCR);
+		}
+	}
+

[ ... ]

arch/arm64/kvm/debug.c static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu) · line 102

Quoted 3 lines
arch/arm64/kvm/debug.c +0 -0
 	/* Write MDCR_EL2 directly if we're already at EL2 */
 	if (has_vhe())
 		write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

[Severity: High]
If a PMU pseudo-NMI fires during the critical section and calls kvm_pmu_host_stop() to clear HPME, will this write clobber that update?

Because local_irq_save() fails to mask NMIs, this line writes the potentially stale, cached vcpu->arch.mdcr_el2 state back to hardware, discarding any intermediate HPME updates from the NMI handler and leaving PMU counters in the wrong hardware state.

Quoted 4 lines
arch/arm64/kvm/debug.c +1 -1
-	preempt_enable();
+	local_irq_restore(flags);
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#23 lore raw

PATCH v9 12/22 KVM: arm64: Context swap Partitioned PMU guest registers

In reply to Colton Lewis

Save and restore newly untrapped registers that can be directly accessed by the guest when the PMU is partitioned.

- PMEVCNTRn_EL0
- PMCCNTR_EL0
- PMSELR_EL0
- PMCR_EL0
- PMCNTEN_EL0
- PMINTEN_EL1

If we know we are not partitioned (that is, using the emulated vPMU), then return immediately. A later patch will make this lazy so the context swaps don't happen unless the guest has accessed the PMU.

PMEVTYPER is handled in a following patch since we must apply the KVM event filter before writing values to hardware.

PMOVS guest counters are cleared to avoid the possibility of generating spurious interrupts when PMINTEN is written. This is fine because the virtual register for PMOVS is always the canonical value.

---
 arch/arm64/kvm/arm.c        |   4 +-
 arch/arm64/kvm/pmu-direct.c | 153 +++++++++++++++++++++++++++++++++++-
 arch/arm64/kvm/sys_regs.c   |   6 +-
 include/kvm/arm_pmu.h       |   5 ++
 4 files changed, 164 insertions(+), 4 deletions(-)
arch/arm64/kvm/arm.c +3 -1
index 8b080804bc90b..75e0f746623ec 100644
@@ -714,6 +714,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 	if (has_vhe())
 		kvm_vcpu_load_vhe(vcpu);
 	kvm_arch_vcpu_load_fp(vcpu);
+	kvm_pmu_load(vcpu);
 	kvm_vcpu_pmu_restore_guest(vcpu);
 	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
 		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
@@ -755,13 +756,14 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 			vcpu_set_flag(vcpu, PKVM_HOST_STATE_DIRTY);
 	}
 
+	kvm_pmu_put(vcpu);
+	kvm_vcpu_pmu_restore_host(vcpu);
 	kvm_vcpu_put_debug(vcpu);
 	kvm_arch_vcpu_put_fp(vcpu);
 	if (has_vhe())
 		kvm_vcpu_put_vhe(vcpu);
 	kvm_timer_vcpu_put(vcpu);
 	kvm_vgic_put(vcpu);
-	kvm_vcpu_pmu_restore_host(vcpu);
 	if (vcpu_has_nv(vcpu))
 		kvm_vcpu_put_hw_mmu(vcpu);
 	kvm_arm_vmid_clear_active();
arch/arm64/kvm/pmu-direct.c +152 -1

162 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +4 -2
index c8210a8b10389..ebcf52261df65 100644
@@ -1189,7 +1189,8 @@ static void pmu_reg_write(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg, u64 val,
 		local_irq_restore(flags);
 		break;
 	case PMUSERENR_EL0:
-		if (kvm_pmu_is_partitioned(vcpu->kvm))
+		if (kvm_pmu_is_partitioned(vcpu->kvm) &&
+		    !(vcpu->arch.mdcr_el2 & MDCR_EL2_TPM))
 			write_sysreg(val, pmuserenr_el0);
 		__vcpu_assign_sys_reg(vcpu, reg, val);
 		break;
@@ -1274,7 +1275,8 @@ static u64 pmu_reg_read(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg)
 		local_irq_restore(flags);
 		break;
 	case PMUSERENR_EL0:
-		if (kvm_pmu_is_partitioned(vcpu->kvm))
+		if (kvm_pmu_is_partitioned(vcpu->kvm) &&
+		    !(vcpu->arch.mdcr_el2 & MDCR_EL2_TPM))
 			val = read_sysreg(pmuserenr_el0);
 		else
 			val = __vcpu_sys_reg(vcpu, reg);
include/kvm/arm_pmu.h +5 -0
index a24788243ac99..2604a6a46d5f3 100644
@@ -102,6 +102,9 @@ void kvm_pmu_direct_pmcr_write(struct kvm_vcpu *vcpu, u64 val);
 u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu);
 u64 kvm_pmu_host_counter_mask(void);
 u64 kvm_pmu_guest_counter_mask(void);
+void kvm_pmu_load(struct kvm_vcpu *vcpu);
+void kvm_pmu_put(struct kvm_vcpu *vcpu);
+
 /*
  * Updates the vcpu's view of the pmu events for this cpu.
  * Must be called before every vcpu run after disabling interrupts, to ensure
@@ -150,6 +153,8 @@ static inline u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu)
 {
 	return 0;
 }
+static inline void kvm_pmu_load(struct kvm_vcpu *vcpu) {}
+static inline void kvm_pmu_put(struct kvm_vcpu *vcpu) {}
 static inline void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu,
 					     u64 select_idx, u64 val) {}
 static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#24 lore raw

Re: [PATCH v9 12/22] KVM: arm64: Context swap Partitioned PMU guest registers

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 14 lines

Save and restore newly untrapped registers that can be directly accessed by the guest when the PMU is partitioned.

- PMEVCNTRn_EL0
- PMCCNTR_EL0
- PMSELR_EL0
- PMCR_EL0
- PMCNTEN_EL0
- PMINTEN_EL1

If we know we are not partitioned (that is, using the emulated vPMU), then return immediately. A later patch will make this lazy so the context swaps don't happen unless the guest has accessed the PMU.

Quoted 8 lines

PMEVTYPER is handled in a following patch since we must apply the KVM event filter before writing values to hardware.

PMOVS guest counters are cleared to avoid the possibility of generating spurious interrupts when PMINTEN is written. This is fine because the virtual register for PMOVS is always the canonical value.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 13/22 KVM: arm64: Enforce PMU event filter at vcpu_load()

In reply to Colton Lewis

The KVM API for event filtering says that counters do not count when blocked by the event filter. To enforce that, the event filter must be rechecked on every load since it might have changed since the last time the guest wrote a value. If the event is filtered, exclude counting at all exception levels before writing the hardware.

---
 arch/arm64/kvm/pmu-direct.c | 77 +++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/sys_regs.c   |  1 +
 include/kvm/arm_pmu.h       |  3 ++
 3 files changed, 81 insertions(+)
arch/arm64/kvm/pmu-direct.c +77 -0
index 92eec0fa33867..a22c9258c2452 100644
@@ -169,6 +169,82 @@ u64 kvm_pmu_guest_counter_mask(void)
 	return kvm_vcpu_pmu_guest_counter_mask(kvm_get_running_vcpu());
 }
 
+/**
+ * kvm_pmu_apply_single_event_filter() - Apply event filter to a single counter
+ * @vcpu: Pointer to vcpu struct
+ * @idx: Counter index
+ *
+ * Compute the filtered event value and write it directly to the hardware register.
+ */
+void kvm_pmu_apply_single_event_filter(struct kvm_vcpu *vcpu, u8 idx)
+{
+	struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu;
+	u64 guest_counters;
+	u64 evtyper_set = ARMV8_PMU_EXCLUDE_EL0 |
+		ARMV8_PMU_EXCLUDE_EL1;
+	u64 evtyper_clr = ARMV8_PMU_INCLUDE_EL2;
+	bool guest_include_el2;
+	u64 val;
+	u64 evsel;
+
+	if (!pmu || vcpu != kvm_get_running_vcpu())
+		return;
+
+	guest_counters = kvm_vcpu_pmu_guest_counter_mask(vcpu);
+	if (!test_bit(idx, (unsigned long *)&guest_counters))
+		return;
+
+	if (idx == ARMV8_PMU_CYCLE_IDX) {
+		val = __vcpu_sys_reg(vcpu, PMCCFILTR_EL0);
+		evsel = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
+	} else {
+		val = __vcpu_sys_reg(vcpu, PMEVTYPER0_EL0 + idx);
+		evsel = val & kvm_pmu_event_mask(vcpu->kvm);
+	}
+
+	guest_include_el2 = (val & ARMV8_PMU_INCLUDE_EL2);
+	val &= ~evtyper_clr;
+
+	if (unlikely(is_hyp_ctxt(vcpu))) {
+		if (guest_include_el2)
+			val &= ~ARMV8_PMU_EXCLUDE_EL1;
+		else
+			val |= ARMV8_PMU_EXCLUDE_EL1;
+	}
+
+	if (vcpu->kvm->arch.pmu_filter &&
+	    !test_bit(evsel, vcpu->kvm->arch.pmu_filter))
+		val |= evtyper_set;
+
+	if (idx == ARMV8_PMU_CYCLE_IDX)
+		write_pmccfiltr(val);
+	else
+		write_pmevtypern(idx, val);
+}
+
+/**
+ * kvm_pmu_apply_event_filter() - Apply event filter to all guest counters
+ * @vcpu: Pointer to vcpu struct
+ *
+ * To uphold the guarantee of the KVM PMU event filter, we must ensure
+ * no counter counts if the event is filtered. Accomplish this by
+ * filtering all exception levels if the event is filtered.
+ */
+static void kvm_pmu_apply_event_filter(struct kvm_vcpu *vcpu)
+{
+	struct arm_pmu *pmu = vcpu->kvm->arch.arm_pmu;
+	unsigned long guest_counters;
+	u8 i;
+
+	if (!pmu)
+		return;
+
+	guest_counters = kvm_vcpu_pmu_guest_counter_mask(vcpu);
+
+	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS)
+		kvm_pmu_apply_single_event_filter(vcpu, i);
+}
+
 /**
  * kvm_pmu_load() - Load untrapped PMU registers
  * @vcpu: Pointer to struct kvm_vcpu
@@ -194,6 +270,7 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
 	preempt_disable();
 
 	guest_counters = kvm_vcpu_pmu_guest_counter_mask(vcpu);
+	kvm_pmu_apply_event_filter(vcpu);
 
 	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {
 		val = __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i);
arch/arm64/kvm/sys_regs.c +1 -0
index ebcf52261df65..c4aa6a448b6ca 100644
@@ -1137,6 +1137,7 @@ static void pmu_reg_write(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg, u64 val,
 		if (kvm_pmu_is_partitioned(vcpu->kvm)) {
 			mask = kvm_pmu_evtyper_mask(vcpu->kvm);
 			__vcpu_assign_sys_reg(vcpu, reg, val & mask);
+			kvm_pmu_apply_single_event_filter(vcpu, idx);
 		} else {
 			kvm_pmu_set_counter_event_type(vcpu, val, idx);
 			kvm_vcpu_pmu_restore_guest(vcpu);
include/kvm/arm_pmu.h +3 -0
index 2604a6a46d5f3..ddbbbb050b9de 100644
@@ -104,6 +104,7 @@ u64 kvm_pmu_host_counter_mask(void);
 u64 kvm_pmu_guest_counter_mask(void);
 void kvm_pmu_load(struct kvm_vcpu *vcpu);
 void kvm_pmu_put(struct kvm_vcpu *vcpu);
+void kvm_pmu_apply_single_event_filter(struct kvm_vcpu *vcpu, u8 idx);
 
 /*
  * Updates the vcpu's view of the pmu events for this cpu.
@@ -263,6 +264,8 @@ static inline u64 kvm_pmu_guest_counter_mask(void)
 	return 0;
 }
 
+static inline void kvm_pmu_apply_single_event_filter(struct kvm_vcpu *vcpu, u8 idx) {}
+
 static inline bool has_kvm_pmu_partition_support(void)
 {
 	return false;
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#26 lore raw

Re: [PATCH v9 13/22] KVM: arm64: Enforce PMU event filter at vcpu_load()

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 7 lines

The KVM API for event filtering says that counters do not count when blocked by the event filter. To enforce that, the event filter must be rechecked on every load since it might have changed since the last time the guest wrote a value. If the event is filtered, exclude counting at all exception levels before writing the hardware.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 14/22 perf: Add perf_pmu_resched_update()

In reply to Colton Lewis

To modify PMU guest counter reservations dynamically, the per-CPU available counter mask must be updated while host perf events are temporarily scheduled out.

Introduce perf_pmu_resched_update() to update PMU state while holding cpuctx->ctx.lock in between scheduling perf events out and scheduling them back in. It accepts a callback invoked between ctx_sched_out() and perf_event_sched_in(), accomplishing atomic counter mask updates with minimal perf API expansion.

Refactor ctx_resched() into __ctx_resched() to invoke the callback at the appropriate point.

---
 include/linux/perf_event.h |  3 +++
 kernel/events/core.c       | 31 ++++++++++++++++++++++++++++---
 2 files changed, 31 insertions(+), 3 deletions(-)
include/linux/perf_event.h +3 -0
index 5842552294c19..32e3c0d365963 100644
@@ -1242,6 +1242,9 @@ extern int perf_event_task_disable(void);
 extern int perf_event_task_enable(void);
 
 extern void perf_pmu_resched(struct pmu *pmu);
+extern void perf_pmu_resched_update(struct pmu *pmu,
+				    void (*update)(struct pmu *, void *),
+				    void *data);
 
 extern int perf_event_refresh(struct perf_event *event, int refresh);
 extern void perf_event_update_userpage(struct perf_event *event);
kernel/events/core.c +28 -3
index db7b76d6b68aa..06d1128d194fa 100644
@@ -2991,9 +2991,10 @@ static void perf_event_sched_in(struct perf_cpu_context *cpuctx,
  * event_type is a bit mask of the types of events involved. For CPU events,
  * event_type is only either EVENT_PINNED or EVENT_FLEXIBLE.
  */
-static void ctx_resched(struct perf_cpu_context *cpuctx,
-			struct perf_event_context *task_ctx,
-			struct pmu *pmu, enum event_type_t event_type)
+static void __ctx_resched(struct perf_cpu_context *cpuctx,
+			  struct perf_event_context *task_ctx,
+			  struct pmu *pmu, enum event_type_t event_type,
+			  void (*update)(struct pmu *, void *), void *data)
 {
 	bool cpu_event = !!(event_type & EVENT_CPU);
 	struct perf_event_pmu_context *epc;
@@ -3029,6 +3030,9 @@ static void ctx_resched(struct perf_cpu_context *cpuctx,
 	else if (event_type & EVENT_PINNED)
 		ctx_sched_out(&cpuctx->ctx, pmu, EVENT_FLEXIBLE);
 
+	if (update)
+		update(pmu, data);
+
 	perf_event_sched_in(cpuctx, task_ctx, pmu, 0);
 
 	for_each_epc(epc, &cpuctx->ctx, pmu, 0)
@@ -3040,6 +3044,27 @@ static void ctx_resched(struct perf_cpu_context *cpuctx,
 	}
 }
 
+static void ctx_resched(struct perf_cpu_context *cpuctx,
+			struct perf_event_context *task_ctx,
+			struct pmu *pmu, enum event_type_t event_type)
+{
+	__ctx_resched(cpuctx, task_ctx, pmu, event_type, NULL, NULL);
+}
+
+void perf_pmu_resched_update(struct pmu *pmu, void (*update)(struct pmu *, void *), void *data)
+{
+	struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context);
+	struct perf_event_context *task_ctx = cpuctx->task_ctx;
+	unsigned long flags;
+
+	local_irq_save(flags);
+	perf_ctx_lock(cpuctx, task_ctx);
+	__ctx_resched(cpuctx, task_ctx, pmu, EVENT_ALL|EVENT_CPU, update, data);
+	perf_ctx_unlock(cpuctx, task_ctx);
+	local_irq_restore(flags);
+}
+EXPORT_SYMBOL_GPL(perf_pmu_resched_update);
+
 void perf_pmu_resched(struct pmu *pmu)
 {
 	struct perf_cpu_context *cpuctx = this_cpu_ptr(&perf_cpu_context);
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#28 lore raw

Re: [PATCH v9 14/22] perf: Add perf_pmu_resched_update()

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

To modify PMU guest counter reservations dynamically, the per-CPU available counter mask must be updated while host perf events are temporarily scheduled out.

Introduce perf_pmu_resched_update() to update PMU state while holding cpuctx->ctx.lock in between scheduling perf events out and scheduling them back in. It accepts a callback invoked between ctx_sched_out() and perf_event_sched_in(), accomplishing atomic counter mask updates with minimal perf API expansion.

Quoted 4 lines

Refactor ctx_resched() into __ctx_resched() to invoke the callback at the appropriate point.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 15/22 KVM: arm64: Allow kvm_vcpu_pmu_resync_el0() to resync filters in process context

In reply to Colton Lewis

Previously, kvm_vcpu_pmu_resync_el0() only scheduled KVM_REQ_RESYNC_PMU_EL0 when called from interrupt context (such as an IPI reprogramming host perf events). When perf_pmu_resched_update() is invoked synchronously from process context during vCPU load/put to reschedule host events around guest-reserved counters, armv8pmu_enable_event() calls kvm_set_pmu_events() and kvm_vcpu_pmu_resync_el0() in process context.

Update kvm_vcpu_pmu_resync_el0() to call kvm_vcpu_pmu_restore_guest() directly when not in interrupt context so that newly rescheduled host perf events have their EL0 exclusion filters applied immediately.

---
 arch/arm64/kvm/pmu.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/arm64/kvm/pmu.c +5 -2
index 6f60f86fad387..a8214ab52fcb5 100644
@@ -213,14 +213,17 @@ void kvm_vcpu_pmu_resync_el0(void)
 {
 	struct kvm_vcpu *vcpu;
 
-	if (!has_vhe() || !in_interrupt())
+	if (!has_vhe())
 		return;
 
 	vcpu = kvm_get_running_vcpu();
 	if (!vcpu)
 		return;
 
-	kvm_make_request(KVM_REQ_RESYNC_PMU_EL0, vcpu);
+	if (in_interrupt())
+		kvm_make_request(KVM_REQ_RESYNC_PMU_EL0, vcpu);
+	else
+		kvm_vcpu_pmu_restore_guest(vcpu);
 }
 
 void kvm_host_pmu_init(struct arm_pmu *pmu)
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#30 lore raw

Re: [PATCH v9 15/22] KVM: arm64: Allow kvm_vcpu_pmu_resync_el0() to resync filters in process context

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

Previously, kvm_vcpu_pmu_resync_el0() only scheduled KVM_REQ_RESYNC_PMU_EL0 when called from interrupt context (such as an IPI reprogramming host perf events). When perf_pmu_resched_update() is invoked synchronously from process context during vCPU load/put to reschedule host events around guest-reserved counters, armv8pmu_enable_event() calls kvm_set_pmu_events() and kvm_vcpu_pmu_resync_el0() in process context.

Quoted 5 lines

Update kvm_vcpu_pmu_resync_el0() to call kvm_vcpu_pmu_restore_guest() directly when not in interrupt context so that newly rescheduled host perf events have their EL0 exclusion filters applied immediately.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 16/22 KVM: arm64: Apply dynamic guest counter reservations

In reply to Colton Lewis

Reserve and release guest PMU counters dynamically during vCPU load and put rather than statically at VM creation.

Add kvm_pmu_set_guest_counters() in arch/arm64/kvm/pmu-direct.c, called from kvm_pmu_load() and kvm_pmu_put(). When the requested guest counter mask collides with active host events in cpuc->used_mask (or when releasing counters after squeezing a host event), invoke perf_pmu_resched_update() with kvm_pmu_update_mask() to update the per-CPU cpuc->cntr_mask between scheduling host events out and back in; otherwise update cpuc->cntr_mask directly with interrupts disabled.

---
 arch/arm64/kvm/pmu-direct.c  | 77 ++++++++++++++++++++++++++++++++++++
 include/linux/perf/arm_pmu.h |  1 +
 2 files changed, 78 insertions(+)
arch/arm64/kvm/pmu-direct.c +77 -0
index a22c9258c2452..31d5afc44f36e 100644
@@ -115,6 +115,77 @@ u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu)
 		ARMV8_PMU_PMCR_N);
 }
 
+/* Callback to update counter mask between perf scheduling */
+static void kvm_pmu_update_mask(struct pmu *pmu, void *data)
+{
+	struct arm_pmu *arm_pmu = to_arm_pmu(pmu);
+	struct pmu_hw_events *cpuc = this_cpu_ptr(arm_pmu->hw_events);
+	unsigned long *new_mask = data;
+
+	bitmap_copy(cpuc->cntr_mask, new_mask, ARMPMU_MAX_HWEVENTS);
+}
+
+/**
+ * kvm_pmu_set_guest_counters() - Handle dynamic counter reservations
+ * @cpu_pmu: struct arm_pmu to potentially modify
+ * @guest_mask: new guest mask for the pmu
+ *
+ * Check if guest counters will interfere with current host events and
+ * call into perf_pmu_resched_update if a reschedule is required.
+ */
+static void kvm_pmu_set_guest_counters(struct arm_pmu *cpu_pmu, u64 guest_mask)
+{
+	struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
+	DECLARE_BITMAP(guest_bitmap, ARMPMU_MAX_HWEVENTS);
+	DECLARE_BITMAP(new_mask, ARMPMU_MAX_HWEVENTS);
+	unsigned long flags;
+	bool need_resched = false;
+
+	bitmap_from_arr64(guest_bitmap, &guest_mask, ARMPMU_MAX_HWEVENTS);
+	bitmap_copy(new_mask, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS);
+
+	local_irq_save(flags);
+	if (guest_mask) {
+		/* Subtract guest counters from available host mask */
+		bitmap_andnot(new_mask, new_mask, guest_bitmap, ARMPMU_MAX_HWEVENTS);
+
+		/* Did we collide with an active host event? */
+		if (bitmap_intersects(cpuc->used_mask, guest_bitmap, ARMPMU_MAX_HWEVENTS)) {
+			int idx;
+
+			need_resched = true;
+			cpuc->host_squeezed = true;
+
+			/* Look for pinned events that are about to be preempted */
+			for_each_set_bit(idx, guest_bitmap, ARMPMU_MAX_HWEVENTS) {
+				if (test_bit(idx, cpuc->used_mask) && cpuc->events[idx] &&
+				    cpuc->events[idx]->attr.pinned) {
+					pr_warn_once("perf: Pinned host event squeezed out by KVM guest PMU partition\n");
+					break;
+				}
+			}
+		}
+	} else {
+		/*
+		 * Restoring to full mask.
+		 * Only resched if we previously squeezed an event.
+		 */
+		if (cpuc->host_squeezed) {
+			need_resched = true;
+			cpuc->host_squeezed = false;
+		}
+	}
+	if (!need_resched)
+		/* Host was never using guest counters anyway */
+		bitmap_copy(cpuc->cntr_mask, new_mask, ARMPMU_MAX_HWEVENTS);
+	local_irq_restore(flags);
+
+	if (need_resched) {
+		/* Collision: run full perf reschedule */
+		perf_pmu_resched_update(&cpu_pmu->pmu, kvm_pmu_update_mask, new_mask);
+	}
+}
+
 /**
  * kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved counters
  *
@@ -255,6 +326,7 @@ static void kvm_pmu_apply_event_filter(struct kvm_vcpu *vcpu)
  */
 void kvm_pmu_load(struct kvm_vcpu *vcpu)
 {
+	struct arm_pmu *pmu;
 	unsigned long guest_counters;
 	u64 mask;
 	u8 i;
@@ -269,7 +341,9 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
 
 	preempt_disable();
 
+	pmu = vcpu->kvm->arch.arm_pmu;
 	guest_counters = kvm_vcpu_pmu_guest_counter_mask(vcpu);
+	kvm_pmu_set_guest_counters(pmu, guest_counters);
 	kvm_pmu_apply_event_filter(vcpu);
 
 	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {
@@ -329,6 +403,7 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
  */
 void kvm_pmu_put(struct kvm_vcpu *vcpu)
 {
+	struct arm_pmu *pmu;
 	unsigned long guest_counters;
 	unsigned long flags;
 	u64 mask;
@@ -345,6 +420,7 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)
 
 	preempt_disable();
 
+	pmu = vcpu->kvm->arch.arm_pmu;
 	guest_counters = kvm_vcpu_pmu_guest_counter_mask(vcpu);
 	mask = guest_counters;
 
@@ -395,5 +471,6 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)
 	write_sysreg(val & mask, pmovsclr_el0);
 	local_irq_restore(flags);
 
+	kvm_pmu_set_guest_counters(pmu, 0);
 	preempt_enable();
 }
include/linux/perf/arm_pmu.h +1 -0
index be1e345e99a77..45658273ffa86 100644
@@ -76,6 +76,7 @@ struct pmu_hw_events {
 
 	/* Active events requesting branch records */
 	unsigned int		branch_users;
+	bool host_squeezed;
 };
 
 enum armpmu_attr_groups {
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#32 lore raw

Re: [PATCH v9 16/22] KVM: arm64: Apply dynamic guest counter reservations

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kvm: arm64: guest PMU overflow state lost on partition without FEAT_FGT --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: arm64: guest PMU overflow state lost on partition without FEAT_FGT --

commit 59a912c6296a832c23a72990ddffa47591b8277e
Author: Colton Lewis <[email protected]>

KVM: arm64: Apply dynamic guest counter reservations

Reserve and release guest PMU counters dynamically during vCPU load and put rather than statically at VM creation.

Quoted 4 lines
arch/arm64/kvm/pmu-direct.c +0 -0
index a22c9258c2452..31d5afc44f36e 100644

[ ... ]

arch/arm64/kvm/pmu-direct.c void kvm_pmu_load(struct kvm_vcpu *vcpu) · line 349

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +2 -0
@@ -269,7 +341,9 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
 
 	preempt_disable();
 
+	pmu = vcpu->kvm->arch.arm_pmu;
 	guest_counters = kvm_vcpu_pmu_guest_counter_mask(vcpu);
+	kvm_pmu_set_guest_counters(pmu, guest_counters);
 	kvm_pmu_apply_event_filter(vcpu);
 
 	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {

[Severity: High]
Can this result in silently dropped guest overflow interrupts when partitioning is enabled on hardware without fine-grained traps?

Further down in kvm_pmu_load(), the physical hardware overflow register is cleared unconditionally:

	/* Clear the hardware overflow flags so there is no chance of
	 * creating spurious interrupts. The hardware here is never
	 * the canonical version anyway.
	 */
	write_sysreg(mask, pmovsclr_el0);

	val = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);

However, there is no corresponding write to pmovsset_el0 to restore the guest's accumulated overflow state from vcpu->arch.ctxt.sys_regs[PMOVSSET_EL0] before entering the guest.

When trap bits are cleared to allow untrapped guest register accesses, and fine-grained traps are not available to intercept pmovsset_el0, the guest's interrupt handler will read this cleared hardware register directly. It will observe 0 instead of the actual overflow state, causing the interrupt to be dropped without processing.

Should the guest's saved overflow bits be restored here so the untrapped reads observe the correct state?

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

Re: [PATCH v9 16/22] KVM: arm64: Apply dynamic guest counter reservations

In reply to Colton Lewis
Quoted 61 lines · Colton Lewis

Reserve and release guest PMU counters dynamically during vCPU load and put rather than statically at VM creation.

Add kvm_pmu_set_guest_counters() in arch/arm64/kvm/pmu-direct.c, called from kvm_pmu_load() and kvm_pmu_put(). When the requested guest counter mask collides with active host events in cpuc->used_mask (or when releasing counters after squeezing a host event), invoke perf_pmu_resched_update() with kvm_pmu_update_mask() to update the per-CPU cpuc->cntr_mask between scheduling host events out and back in; otherwise update cpuc->cntr_mask directly with interrupts disabled.

---
  arch/arm64/kvm/pmu-direct.c  | 77 ++++++++++++++++++++++++++++++++++++
  include/linux/perf/arm_pmu.h |  1 +
  2 files changed, 78 insertions(+)
arch/arm64/kvm/pmu-direct.c +36 -0
index a22c9258c2452..31d5afc44f36e 100644
@@ -115,6 +115,77 @@ u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu)
  		ARMV8_PMU_PMCR_N);
  }
 
+/* Callback to update counter mask between perf scheduling */
+static void kvm_pmu_update_mask(struct pmu *pmu, void *data)
+{
+	struct arm_pmu *arm_pmu = to_arm_pmu(pmu);
+	struct pmu_hw_events *cpuc = this_cpu_ptr(arm_pmu->hw_events);
+	unsigned long *new_mask = data;
+
+	bitmap_copy(cpuc->cntr_mask, new_mask, ARMPMU_MAX_HWEVENTS);
+}
+
+/**
+ * kvm_pmu_set_guest_counters() - Handle dynamic counter reservations
+ * @cpu_pmu: struct arm_pmu to potentially modify
+ * @guest_mask: new guest mask for the pmu
+ *
+ * Check if guest counters will interfere with current host events and
+ * call into perf_pmu_resched_update if a reschedule is required.
+ */
+static void kvm_pmu_set_guest_counters(struct arm_pmu *cpu_pmu, u64 guest_mask)
+{
+	struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
+	DECLARE_BITMAP(guest_bitmap, ARMPMU_MAX_HWEVENTS);
+	DECLARE_BITMAP(new_mask, ARMPMU_MAX_HWEVENTS);
+	unsigned long flags;
+	bool need_resched = false;
+
+	bitmap_from_arr64(guest_bitmap, &guest_mask, ARMPMU_MAX_HWEVENTS);
+	bitmap_copy(new_mask, cpu_pmu->cntr_mask, ARMPMU_MAX_HWEVENTS);
+
+	local_irq_save(flags);
+	if (guest_mask) {
+		/* Subtract guest counters from available host mask */
+		bitmap_andnot(new_mask, new_mask, guest_bitmap, ARMPMU_MAX_HWEVENTS);
+
+		/* Did we collide with an active host event? */
+		if (bitmap_intersects(cpuc->used_mask, guest_bitmap, ARMPMU_MAX_HWEVENTS)) {

arch/arm64/kvm/pmu-direct.c u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu) · line 163

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+			int idx;
+
+			need_resched = true;
+			cpuc->host_squeezed = true;
+
+			/* Look for pinned events that are about to be preempted */
+			for_each_set_bit(idx, guest_bitmap, ARMPMU_MAX_HWEVENTS) {
+				if (test_bit(idx, cpuc->used_mask) && cpuc->events[idx] &&
+				    cpuc->events[idx]->attr.pinned) {
+					pr_warn_once("perf: Pinned host event squeezed out by KVM guest PMU partition\n");

If you enable pseudo-NMIs, watchdog_hardlockup_enable() installs the watchdog using a pinned PMU event. If host userspace also has a pinned event on the mandatory 1 PMU counter assigned to the host, then a guest could potentially squeeze out the watchdog.

I'm wondering if we need to prioritise kernel owned events? Or we just treat them the same as any other event, and with PMU partitioning assume they can't be guaranteed to be running? I feel like you would expect a watchdog to be a bit more than best effort though, especially if there was always a guaranteed counter available to put it on.

I didn't follow it through completely, but it also looks like if the event gets squeezed it would enter an error state and then never be re-enabled, even after the guest stops running.

Note, that I think the current ordering means that the watchdog won't actually get squeezed out because it's created first. But I don't think we can rely on the ordering as a strong guarantee, and it might get broken by refactoring in the future.

Quoted 81 lines
arch/arm64/kvm/pmu-direct.c +31 -0
+					break;
+				}
+			}
+		}
+	} else {
+		/*
+		 * Restoring to full mask.
+		 * Only resched if we previously squeezed an event.
+		 */
+		if (cpuc->host_squeezed) {
+			need_resched = true;
+			cpuc->host_squeezed = false;
+		}
+	}
+	if (!need_resched)
+		/* Host was never using guest counters anyway */
+		bitmap_copy(cpuc->cntr_mask, new_mask, ARMPMU_MAX_HWEVENTS);
+	local_irq_restore(flags);
+
+	if (need_resched) {
+		/* Collision: run full perf reschedule */
+		perf_pmu_resched_update(&cpu_pmu->pmu, kvm_pmu_update_mask, new_mask);
+	}
+}
+
  /**
   * kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved counters
   *
@@ -255,6 +326,7 @@ static void kvm_pmu_apply_event_filter(struct kvm_vcpu *vcpu)
   */
  void kvm_pmu_load(struct kvm_vcpu *vcpu)
  {
+	struct arm_pmu *pmu;
  	unsigned long guest_counters;
  	u64 mask;
  	u8 i;
@@ -269,7 +341,9 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
 
  	preempt_disable();
 
+	pmu = vcpu->kvm->arch.arm_pmu;
  	guest_counters = kvm_vcpu_pmu_guest_counter_mask(vcpu);
+	kvm_pmu_set_guest_counters(pmu, guest_counters);
  	kvm_pmu_apply_event_filter(vcpu);
 
  	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {
@@ -329,6 +403,7 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
   */
  void kvm_pmu_put(struct kvm_vcpu *vcpu)
  {
+	struct arm_pmu *pmu;
  	unsigned long guest_counters;
  	unsigned long flags;
  	u64 mask;
@@ -345,6 +420,7 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)
 
  	preempt_disable();
 
+	pmu = vcpu->kvm->arch.arm_pmu;
  	guest_counters = kvm_vcpu_pmu_guest_counter_mask(vcpu);
  	mask = guest_counters;
 
@@ -395,5 +471,6 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)
  	write_sysreg(val & mask, pmovsclr_el0);
  	local_irq_restore(flags);
 
+	kvm_pmu_set_guest_counters(pmu, 0);
  	preempt_enable();
  }
include/linux/perf/arm_pmu.h +1 -0
index be1e345e99a77..45658273ffa86 100644
@@ -76,6 +76,7 @@ struct pmu_hw_events {
 
  	/* Active events requesting branch records */
  	unsigned int		branch_users;
+	bool host_squeezed;
  };
 
  enum armpmu_attr_groups {
#34 lore raw

Re: [PATCH v9 16/22] KVM: arm64: Apply dynamic guest counter reservations

Hi James,

Quoted 11 lines · James Clark

If you enable pseudo-NMIs, watchdog_hardlockup_enable() installs the watchdog using a pinned PMU event. If host userspace also has a pinned event on the mandatory 1 PMU counter assigned to the host, then a guest could potentially squeeze out the watchdog.

I'm wondering if we need to prioritise kernel owned events? Or we just treat them the same as any other event, and with PMU partitioning assume they can't be guaranteed to be running? I feel like you would expect a watchdog to be a bit more than best effort though, especially if there was always a guaranteed counter available to put it on.

Quoted 8 lines

I didn't follow it through completely, but it also looks like if the event gets squeezed it would enter an error state and then never be re-enabled, even after the guest stops running.

Note, that I think the current ordering means that the watchdog won't actually get squeezed out because it's created first. But I don't think we can rely on the ordering as a strong guarantee, and it might get broken by refactoring in the future.

Good catch. Even without future refactoring, creation order can be defeated today if /proc/sys/kernel/nmi_watchdog is toggled off and back on at runtime while a userspace per-CPU pinned event is already open, giving the re-created watchdog a higher group_index and causing merge_sched_in() to put it into PERF_EVENT_STATE_ERROR when a partitioned guest takes PMCCNTR_EL0.

Prioritizing kernel-owned events (!event->owner) ahead of userspace events in perf_event_groups_cmp() and perf_less_group_idx() guarantees that a kernel-pinned watchdog always wins the mandatory host counter over userspace pinned events regardless of creation order. I will look into adding that for v10.

Thanks,
Colton

#35 lore raw

PATCH v9 17/22 KVM: arm64: Implement lazy PMU context swaps

In reply to Colton Lewis

Since many guests never touch the PMU, avoid paying the cost of reserving hardware counters and context swapping PMU registers on every vCPU load and put until the guest actively enables PMU counters or interrupts.

Track per-vCPU PMU ownership state via enum vcpu_pmu_access (VCPU_PMU_ACCESS_FREE vs. VCPU_PMU_ACCESS_GUEST_OWNED):
- While FREE, trap all PMU register accesses via MDCR_EL2 (HPMN set to the full host counter count, TPM/TPMCR set, and FGT traps left at default) so host perf retains all counters including PMCCNTR_EL0. Reads and non-enabling writes (such as guest kernel PMU probe resets) operate on virtual register state without claiming hardware counters.
- When the guest enables counting or interrupts (writing PMCR_EL0.E = 1 or setting guest counter bits in PMCNTENSET_EL0 or PMINTENSET_EL1), transition to GUEST_OWNED via kvm_pmu_set_guest_owned(), reserve the guest's partition of counters, load guest PMU state into hardware via kvm_pmu_load(), and untrap guest partition accesses via FGT.

---
 arch/arm64/include/asm/kvm_host.h  |   1 +
 arch/arm64/include/asm/kvm_types.h |   6 +-
 arch/arm64/kvm/debug.c             |   4 +-
 arch/arm64/kvm/pmu-direct.c        |  50 +++++++++++--
 arch/arm64/kvm/pmu-emul.c          |   6 +-
 arch/arm64/kvm/sys_regs.c          | 114 ++++++++++++++++++++---------
 include/kvm/arm_pmu.h              |  10 +++
 7 files changed, 146 insertions(+), 45 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 98143daab6a55..8dff576667d10 100644
@@ -1432,6 +1432,7 @@ static inline bool kvm_system_needs_idmapped_vectors(void)
 	return cpus_have_final_cap(ARM64_SPECTRE_V3A);
 }
 
+void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu);
 void kvm_init_host_debug_data(void);
 void kvm_debug_init_vhe(void);
 void kvm_vcpu_load_debug(struct kvm_vcpu *vcpu);
arch/arm64/include/asm/kvm_types.h +5 -1
index 9a126b9e2d7c9..4e39cbc80aa0b 100644
@@ -4,5 +4,9 @@
 
 #define KVM_ARCH_NR_OBJS_PER_MEMORY_CACHE 40
 
-#endif /* _ASM_ARM64_KVM_TYPES_H */
+enum vcpu_pmu_register_access {
+	VCPU_PMU_ACCESS_FREE,
+	VCPU_PMU_ACCESS_GUEST_OWNED,
+};
 
+#endif /* _ASM_ARM64_KVM_TYPES_H */
arch/arm64/kvm/debug.c +2 -2
index a722fd4594e09..d74354ec8351f 100644
@@ -35,7 +35,7 @@ static int cpu_has_spe(u64 dfr0)
  *  - Self-hosted Trace Filter controls (MDCR_EL2_TTRF)
  *  - Self-hosted Trace (MDCR_EL2_TTRF/MDCR_EL2_E2TB)
  */
-static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
+void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
 {
 	unsigned long flags;
 
@@ -54,7 +54,7 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
 				MDCR_EL2_TDRA |
 				MDCR_EL2_TDOSA);
 
-	if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+	if (kvm_pmu_is_partitioned(vcpu->kvm) && vcpu->arch.pmu.loaded_on_cpu) {
 		u8 nr_guest_cntr = vcpu->kvm->arch.nr_pmu_counters;
 		u64 hpmn = FIELD_GET(MDCR_EL2_HPMN, read_sysreg(mdcr_el2));
 		bool hpme;
arch/arm64/kvm/pmu-direct.c +44 -6

101 lines not on this page · raw message

arch/arm64/kvm/pmu-emul.c +5 -1
index 2a2c924fc6b8f..e8195984b0373 100644
@@ -630,7 +630,8 @@ void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu)
 	__vcpu_rmw_sys_reg(vcpu, PMINTENSET_EL1, &=, mask);
 	__vcpu_rmw_sys_reg(vcpu, PMCNTENSET_EL0, &=, mask);
 
-	kvm_pmu_reprogram_counter_mask(vcpu, mask);
+	if (!kvm_pmu_is_partitioned(vcpu->kvm))
+		kvm_pmu_reprogram_counter_mask(vcpu, mask);
 }
 
 void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu)
@@ -639,6 +640,9 @@ void kvm_pmu_nested_transition(struct kvm_vcpu *vcpu)
 	unsigned long mask;
 	int i;
 
+	if (kvm_pmu_is_partitioned(vcpu->kvm))
+		return;
+
 	mask = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
 	for_each_set_bit(i, &mask, 32) {
 		struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
arch/arm64/kvm/sys_regs.c +79 -35

212 lines not on this page · raw message

include/kvm/arm_pmu.h +10 -0
index ddbbbb050b9de..0f2e39367d637 100644
@@ -7,6 +7,7 @@
 #ifndef __ASM_ARM_KVM_PMU_H
 #define __ASM_ARM_KVM_PMU_H
 
+#include <linux/kvm_types.h>
 #include <linux/perf_event.h>
 #include <linux/perf/arm_pmuv3.h>
 #include <linux/perf/arm_pmu.h>
@@ -42,6 +43,8 @@ struct kvm_pmu {
 	struct kvm_pmc pmc[KVM_ARMV8_PMU_MAX_COUNTERS];
 	int irq_num;
 	bool created;
+	bool loaded_on_cpu;
+	enum vcpu_pmu_register_access access;
 };
 
 struct arm_pmu_entry {
@@ -100,12 +103,16 @@ void kvm_pmu_partition_enable(struct kvm *kvm, bool enable);
 bool kvm_pmu_is_partitioned(struct kvm *kvm);
 void kvm_pmu_direct_pmcr_write(struct kvm_vcpu *vcpu, u64 val);
 u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu);
+u64 kvm_vcpu_pmu_guest_counter_mask(struct kvm_vcpu *vcpu);
 u64 kvm_pmu_host_counter_mask(void);
 u64 kvm_pmu_guest_counter_mask(void);
 void kvm_pmu_load(struct kvm_vcpu *vcpu);
 void kvm_pmu_put(struct kvm_vcpu *vcpu);
+void kvm_pmu_set_guest_owned(struct kvm_vcpu *vcpu);
 void kvm_pmu_apply_single_event_filter(struct kvm_vcpu *vcpu, u8 idx);
 
+#define kvm_pmu_get_access(vcpu)	((vcpu)->arch.pmu.access)
+
 /*
  * Updates the vcpu's view of the pmu events for this cpu.
  * Must be called before every vcpu run after disabling interrupts, to ensure
@@ -149,6 +156,8 @@ static inline bool kvm_pmu_is_partitioned(struct kvm *kvm)
 {
 	return false;
 }
+
+#define kvm_pmu_get_access(vcpu)	(VCPU_PMU_ACCESS_FREE)
 static inline void kvm_pmu_direct_pmcr_write(struct kvm_vcpu *vcpu, u64 val) {}
 static inline u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu)
 {
@@ -156,6 +165,7 @@ static inline u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu)
 }
 static inline void kvm_pmu_load(struct kvm_vcpu *vcpu) {}
 static inline void kvm_pmu_put(struct kvm_vcpu *vcpu) {}
+static inline void kvm_pmu_set_guest_owned(struct kvm_vcpu *vcpu) {}
 static inline void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu,
 					     u64 select_idx, u64 val) {}
 static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#36 lore raw

Re: [PATCH v9 17/22] KVM: arm64: Implement lazy PMU context swaps

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] kvm: arm64: PMU hardware permanently frozen after live migration --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] kvm: arm64: PMU hardware permanently frozen after live migration --

commit 458afe99983a6c37492623080fcc934e4e368118
Author: Colton Lewis <[email protected]>

KVM: arm64: Implement lazy PMU context swaps

This patch implements lazy PMU context swapping to avoid the overhead of reserving hardware counters on every vCPU load and put. It introduces per-vCPU PMU ownership tracking, transitioning to GUEST_OWNED only when the guest actively enables PMU counters or interrupts.

arch/arm64/kvm/pmu-direct.c void kvm_pmu_load(struct kvm_vcpu *vcpu) · line 347

Quoted 11 lines
arch/arm64/kvm/pmu-direct.c +3 -1
@@ -336,7 +342,9 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
 	 * If we aren't guest-owned then we know the guest isn't using
 	 * the PMU anyway, so no need to bother with the swap.
 	 */
-	if (!kvm_pmu_is_partitioned(vcpu->kvm))
+	if (!kvm_pmu_is_partitioned(vcpu->kvm) ||
+	    kvm_pmu_get_access(vcpu) != VCPU_PMU_ACCESS_GUEST_OWNED ||
+	    vcpu->arch.pmu.loaded_on_cpu)

[Severity: High]
Does this logic cause the PMU hardware to remain permanently frozen after a live migration?

When userspace restores the PMU state via KVM_SET_ONE_REG (which routes through set_pmcr() and set_pmreg() in arch/arm64/kvm/sys_regs.c), it bypasses pmu_reg_write() and assigns the system registers directly. Because the state is never transitioned to VCPU_PMU_ACCESS_GUEST_OWNED on this restore path, it remains VCPU_PMU_ACCESS_FREE.

Later, when kvm_pmu_load() runs, it aborts early due to the access state being FREE, meaning the physical counters are never configured or started. Although kvm_pmu_part_overflow_status() acts as a fallback to catch state restoration, it explicitly requires an overflow to be pending:

arch/arm64/kvm/pmu-direct.c:kvm_pmu_part_overflow_status() {
    ...
    if (overflow && kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_FREE)
        kvm_pmu_set_guest_owned(vcpu);
    ...
}

If the guest is migrated while actively counting but no overflow is pending, the counters will never tick, will never overflow, and this fallback will never trigger. Could the restore paths be updated to ensure the ownership transitions correctly?

Quoted 3 lines
arch/arm64/kvm/pmu-direct.c +0 -0
 		return;
 
 	preempt_disable();
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
#37 lore raw

PATCH v9 18/22 perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters

In reply to Colton Lewis

Because ARM hardware is not yet capable of direct PPI injection into guests, guest counters trigger physical PMU interrupts that must be handled by the host PMU interrupt handler.

In armv8pmu_handle_irq(), clear the overflow flags in hardware for host counters, restrict host perf event handling to bits in cpuc->cntr_mask, and pass the overflow flags to kvm_pmu_handle_guest_irq(). The KVM hook clears the guest counter overflow flags in hardware and records them in the running vCPU's virtual PMOVSSET_EL0 register for subsequent guest interrupt injection.

Additionally, provide kvm_pmu_host_start() and kvm_pmu_host_stop() so that armv8pmu_start() and armv8pmu_stop() toggle MDCR_EL2.HPME rather than PMCR_EL0.E when a partitioned guest's PMU state is loaded on the CPU.

---
 arch/arm/include/asm/arm_pmuv3.h   |  8 ++++
 arch/arm64/include/asm/arm_pmuv3.h |  5 +++
 arch/arm64/kvm/pmu-direct.c        | 67 ++++++++++++++++++++++++++++++
 drivers/perf/arm_pmuv3.c           | 34 ++++++++-------
 include/kvm/arm_pmu.h              |  6 +++
 5 files changed, 104 insertions(+), 16 deletions(-)
arch/arm/include/asm/arm_pmuv3.h +8 -0
index cec26c12bc009..9b8d6bbbe70cf 100644
@@ -180,6 +180,11 @@ static inline void write_pmintenset(u32 val)
 	write_sysreg(val, PMINTENSET);
 }
 
+static inline u32 read_pmintenset(void)
+{
+	return read_sysreg(PMINTENSET);
+}
+
 static inline void write_pmintenclr(u32 val)
 {
 	write_sysreg(val, PMINTENCLR);
@@ -235,6 +240,9 @@ static inline u64 kvm_pmu_host_counter_mask(void)
 {
 	return ~0;
 }
+static inline void kvm_pmu_handle_guest_irq(u64 pmovsr) {}
+static inline bool kvm_pmu_host_start(void) { return false; }
+static inline bool kvm_pmu_host_stop(void) { return false; }
 
 /* PMU Version in DFR Register */
 #define ARMV8_PMU_DFR_VER_NI        0
arch/arm64/include/asm/arm_pmuv3.h +5 -0
index cf2b2212e00a2..9037a28365951 100644
@@ -110,6 +110,11 @@ static inline void write_pmintenset(u64 val)
 	write_sysreg(val, pmintenset_el1);
 }
 
+static inline u64 read_pmintenset(void)
+{
+	return read_sysreg(pmintenset_el1);
+}
+
 static inline void write_pmintenclr(u64 val)
 {
 	write_sysreg(val, pmintenclr_el1);
arch/arm64/kvm/pmu-direct.c +67 -0

70 lines not on this page · raw message

drivers/perf/arm_pmuv3.c +18 -16
index 4fcdae8021a56..3e5f8207a0fee 100644
@@ -763,18 +763,9 @@ static void armv8pmu_disable_event_irq(struct perf_event *event)
 	armv8pmu_disable_intens(BIT(event->hw.idx));
 }
 
-static u64 armv8pmu_getreset_flags(void)
+static u64 armv8pmu_getovf_flags(void)
 {
-	u64 value;
-
-	/* Read */
-	value = read_pmovsclr();
-
-	/* Write to clear flags */
-	value &= ARMV8_PMU_CNT_MASK_ALL;
-	write_pmovsclr(value);
-
-	return value;
+	return read_pmovsclr() & ARMV8_PMU_CNT_MASK_ALL;
 }
 
 static void update_pmuserenr(u64 val)
@@ -864,7 +855,8 @@ static void armv8pmu_start(struct arm_pmu *cpu_pmu)
 		brbe_enable(cpu_pmu);
 
 	/* Enable all counters */
-	armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
+	if (!kvm_pmu_host_start())
+		armv8pmu_pmcr_write(armv8pmu_pmcr_read() | ARMV8_PMU_PMCR_E);
 }
 
 static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
@@ -875,7 +867,8 @@ static void armv8pmu_stop(struct arm_pmu *cpu_pmu)
 		brbe_disable();
 
 	/* Disable all counters */
-	armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
+	if (!kvm_pmu_host_stop())
+		armv8pmu_pmcr_write(armv8pmu_pmcr_read() & ~ARMV8_PMU_PMCR_E);
 }
 
 static void read_branch_records(struct pmu_hw_events *cpuc,
@@ -890,16 +883,16 @@ static void read_branch_records(struct pmu_hw_events *cpuc,
 
 static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
 {
-	u64 pmovsr;
 	struct perf_sample_data data;
 	struct pmu_hw_events *cpuc = this_cpu_ptr(cpu_pmu->hw_events);
 	struct pt_regs *regs;
+	u64 pmovsr;
 	int idx;
 
 	/*
-	 * Get and reset the IRQ flags
+	 * Get the IRQ flags
 	 */
-	pmovsr = armv8pmu_getreset_flags();
+	pmovsr = armv8pmu_getovf_flags();
 
 	/*
 	 * Did an overflow occur?
@@ -907,6 +900,12 @@ static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
 	if (!armv8pmu_has_overflowed(pmovsr))
 		return IRQ_NONE;
 
+	/*
+	 * Guest flag reset is handled by the kvm hook at the bottom of
+	 * this function.
+	 */
+	write_pmovsclr(pmovsr & ~kvm_pmu_guest_counter_mask());
+
 	/*
 	 * Handle the counter(s) overflow(s)
 	 */
@@ -948,6 +947,9 @@ static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
 		 */
 		perf_event_overflow(event, &data, regs);
 	}
+
+	kvm_pmu_handle_guest_irq(pmovsr);
+
 	armv8pmu_start(cpu_pmu);
 
 	return IRQ_HANDLED;
include/kvm/arm_pmu.h +6 -0
index 0f2e39367d637..955f142a41b94 100644
@@ -109,6 +109,9 @@ u64 kvm_pmu_guest_counter_mask(void);
 void kvm_pmu_load(struct kvm_vcpu *vcpu);
 void kvm_pmu_put(struct kvm_vcpu *vcpu);
 void kvm_pmu_set_guest_owned(struct kvm_vcpu *vcpu);
+void kvm_pmu_handle_guest_irq(u64 pmovsr);
+bool kvm_pmu_host_start(void);
+bool kvm_pmu_host_stop(void);
 void kvm_pmu_apply_single_event_filter(struct kvm_vcpu *vcpu, u8 idx);
 
 #define kvm_pmu_get_access(vcpu)	((vcpu)->arch.pmu.access)
@@ -274,6 +277,9 @@ static inline u64 kvm_pmu_guest_counter_mask(void)
 	return 0;
 }
 
+static inline void kvm_pmu_handle_guest_irq(u64 pmovsr) {}
+static inline bool kvm_pmu_host_start(void) { return false; }
+static inline bool kvm_pmu_host_stop(void) { return false; }
 static inline void kvm_pmu_apply_single_event_filter(struct kvm_vcpu *vcpu, u8 idx) {}
 
 static inline bool has_kvm_pmu_partition_support(void)
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#38 lore raw

Re: [PATCH v9 18/22] perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Because ARM hardware is not yet capable of direct PPI injection into guests, guest counters trigger physical PMU interrupts that must be handled by the host PMU interrupt handler.

In armv8pmu_handle_irq(), clear the overflow flags in hardware for host counters, restrict host perf event handling to bits in cpuc->cntr_mask, and pass the overflow flags to kvm_pmu_handle_guest_irq(). The KVM hook clears the guest counter overflow flags in hardware and records them in the running vCPU's virtual PMOVSSET_EL0 register for subsequent guest interrupt injection.

Quoted 6 lines

Additionally, provide kvm_pmu_host_start() and kvm_pmu_host_stop() so that armv8pmu_start() and armv8pmu_stop() toggle MDCR_EL2.HPME rather than PMCR_EL0.E when a partitioned guest's PMU state is loaded on the CPU.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 19/22 KVM: arm64: Detect overflows for the Partitioned PMU

In reply to Colton Lewis

When re-entering the VM or synchronizing PMU state after a guest counter overflow interrupt, evaluate whether any enabled and unmasked guest counter has overflowed ((PMOVSSET_EL0 & PMINTENSET_EL1 & PMCNTENSET_EL0) with PMCR_EL0.E set) and update the virtual PMU interrupt level in the vGIC accordingly.

---
 arch/arm64/kvm/pmu-direct.c | 55 +++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/pmu-emul.c   | 12 ++++++--
 arch/arm64/kvm/pmu.c        |  8 +++++-
 include/kvm/arm_pmu.h       |  2 ++
 4 files changed, 73 insertions(+), 4 deletions(-)
arch/arm64/kvm/pmu-direct.c +55 -0
index 95b5a64052762..ec821aa54688f 100644
@@ -534,6 +534,61 @@ void kvm_pmu_handle_guest_irq(u64 pmovsr)
 
 	for_each_set_bit(i, (unsigned long *)&govf, 64)
 		set_bit(i, (unsigned long *)__ctxt_sys_reg(&vcpu->arch.ctxt, PMOVSSET_EL0));
+
+	if (kvm_pmu_part_overflow_status(vcpu)) {
+		kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+
+		if (!in_nmi())
+			kvm_vcpu_kick(vcpu);
+		else
+			irq_work_queue(&vcpu->arch.pmu.overflow_work);
+	}
+}
+
+/**
+ * kvm_pmu_part_overflow_status() - Determine if any guest counters have overflowed
+ * @vcpu: Pointer to struct kvm_vcpu
+ *
+ * Determine if any guest counters have overflowed and therefore an
+ * IRQ needs to be injected into the guest. If an overflow is detected
+ * while access is VCPU_PMU_ACCESS_FREE (for example, after state
+ * restoration via KVM_SET_ONE_REG), transition the vCPU to
+ * VCPU_PMU_ACCESS_GUEST_OWNED.
+ *
+ * Return: True if there was an overflow, false otherwise
+ */
+bool kvm_pmu_part_overflow_status(struct kvm_vcpu *vcpu)
+{
+	u64 mask = kvm_vcpu_pmu_guest_counter_mask(vcpu);
+	u64 pmovs, pmint, pmcr;
+	unsigned long flags;
+	bool overflow;
+
+	local_irq_save(flags);
+	if (vcpu->arch.pmu.loaded_on_cpu &&
+	    vcpu == kvm_get_running_vcpu()) {
+		u64 hw_ovf = read_sysreg(pmovsset_el0) & mask;
+
+		if (hw_ovf) {
+			__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, hw_ovf);
+			write_sysreg(hw_ovf, pmovsclr_el0);
+		}
+		pmint = read_pmintenset();
+		pmcr = read_pmcr();
+	} else {
+		pmint = __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
+		pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
+	}
+
+	pmovs = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
+	local_irq_restore(flags);
+
+	overflow = (pmcr & ARMV8_PMU_PMCR_E) && (mask & pmovs & pmint);
+
+	if (overflow && kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_FREE)
+		kvm_pmu_set_guest_owned(vcpu);
+
+	return overflow;
 }
 
 /**
arch/arm64/kvm/pmu-emul.c +9 -3
index e8195984b0373..21e2c96abfdb1 100644
@@ -268,7 +268,7 @@ void kvm_pmu_reprogram_counter_mask(struct kvm_vcpu *vcpu, u64 val)
  * counter where the values of the global enable control, PMOVSSET_EL0[n], and
  * PMINTENSET_EL1[n] are all 1.
  */
-bool kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
+bool kvm_pmu_emul_overflow_status(struct kvm_vcpu *vcpu)
 {
 	u64 reg = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
 
@@ -296,8 +296,14 @@ bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
 {
 	struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
 	bool run_level = sregs->device_irq_level & KVM_ARM_DEV_PMU;
+	bool overflow;
 
-	return kvm_pmu_overflow_status(vcpu) != run_level;
+	if (kvm_pmu_is_partitioned(vcpu->kvm))
+		overflow = kvm_pmu_part_overflow_status(vcpu);
+	else
+		overflow = kvm_pmu_emul_overflow_status(vcpu);
+
+	return overflow != run_level;
 }
 
 /*
@@ -400,7 +406,7 @@ static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
 		kvm_pmu_counter_increment(vcpu, BIT(idx + 1),
 					  ARMV8_PMUV3_PERFCTR_CHAIN);
 
-	if (kvm_pmu_overflow_status(vcpu)) {
+	if (kvm_pmu_emul_overflow_status(vcpu)) {
 		kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
 
 		if (!in_nmi())
arch/arm64/kvm/pmu.c +7 -1
index a8214ab52fcb5..4a3c6600b2678 100644
@@ -409,12 +409,18 @@ u64 kvm_pmu_evtyper_mask(struct kvm *kvm)
 static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = &vcpu->arch.pmu;
+	bool overflow;
 
 	if (unlikely(!irqchip_in_kernel(vcpu->kvm)))
 		return;
 
+	if (kvm_pmu_is_partitioned(vcpu->kvm))
+		overflow = kvm_pmu_part_overflow_status(vcpu);
+	else
+		overflow = kvm_pmu_emul_overflow_status(vcpu);
+
 	WARN_ON(kvm_vgic_inject_irq(vcpu->kvm, vcpu, pmu->irq_num,
-				    kvm_pmu_overflow_status(vcpu), pmu));
+				    overflow, pmu));
 }
 
 /**
include/kvm/arm_pmu.h +2 -0
index 955f142a41b94..428a609eaf1a7 100644
@@ -91,6 +91,8 @@ bool kvm_set_pmuserenr(u64 val);
 void kvm_vcpu_pmu_restore_guest(struct kvm_vcpu *vcpu);
 void kvm_vcpu_pmu_restore_host(struct kvm_vcpu *vcpu);
 void kvm_vcpu_pmu_resync_el0(void);
+bool kvm_pmu_emul_overflow_status(struct kvm_vcpu *vcpu);
+bool kvm_pmu_part_overflow_status(struct kvm_vcpu *vcpu);
 
 #define kvm_vcpu_has_pmu(vcpu)					\
 	(vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3))
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#40 lore raw

Re: [PATCH v9 19/22] KVM: arm64: Detect overflows for the Partitioned PMU

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [Critical] kvm: arm64: PMU partition clears hardware PMOVSSET_EL0 without restore - [High] kvm: arm64…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [Critical] kvm: arm64: PMU partition clears hardware PMOVSSET_EL0 without restore
- [High] kvm: arm64: Data race in PMOVSSET_EL0 update against NMI handler
- [Medium] kvm: arm64: kvm_pmu_should_notify_user() indirectly alters hardware PMU state --

commit ff8f6a7646a20c591702abbe44679f3e9bd4462d
Author: Colton Lewis <[email protected]>

KVM: arm64: Detect overflows for the Partitioned PMU

When re-entering the VM or synchronizing PMU state after a guest counter overflow interrupt, evaluate whether any enabled and unmasked guest counter has overflowed and update the virtual PMU interrupt level in the vGIC accordingly.

Quoted 3 lines
arch/arm64/kvm/pmu-direct.c +0 -0

[ ... ]

Quoted 17 lines
arch/arm64/kvm/pmu-direct.c +13 -0
@@ -534,6 +534,61 @@ void kvm_pmu_handle_guest_irq(u64 pmovsr)
 
 	for_each_set_bit(i, (unsigned long *)&govf, 64)
 		set_bit(i, (unsigned long *)__ctxt_sys_reg(&vcpu->arch.ctxt, PMOVSSET_EL0));
+
+	if (kvm_pmu_part_overflow_status(vcpu)) {
+		kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
+
+		if (!in_nmi())
+			kvm_vcpu_kick(vcpu);
+		else
+			irq_work_queue(&vcpu->arch.pmu.overflow_work);
+	}
+}
+
+/**
+ * kvm_pmu_part_overflow_status() - Determine if any guest counters have overflowed

[ ... ]

Quoted 4 lines
arch/arm64/kvm/pmu-direct.c +4 -0
+bool kvm_pmu_part_overflow_status(struct kvm_vcpu *vcpu)
+{
+	u64 mask = kvm_vcpu_pmu_guest_counter_mask(vcpu);
+	u64 pmovs, pmint, pmcr;

arch/arm64/kvm/pmu-direct.c void kvm_pmu_handle_guest_irq(u64 pmovsr) · line 573

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+	unsigned long flags;
+	bool overflow;
+
+	local_irq_save(flags);
+	if (vcpu->arch.pmu.loaded_on_cpu &&
+	    vcpu == kvm_get_running_vcpu()) {
+		u64 hw_ovf = read_sysreg(pmovsset_el0) & mask;
+
+		if (hw_ovf) {
+			__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, hw_ovf);

[Severity: High]
Is this non-atomic update vulnerable to a race with pseudo-NMIs?

Since local_irq_save() does not mask pseudo-NMIs on ARM64, the PMU interrupt can fire as an NMI and invoke kvm_pmu_handle_guest_irq(), which updates this exact software state using an atomic set_bit().

When the NMI returns, this interrupted non-atomic read-modify-write operation will complete and could silently overwrite the NMI handler's updates, resulting in lost interrupts and corrupted hardware state.

arch/arm64/kvm/pmu-direct.c void kvm_pmu_handle_guest_irq(u64 pmovsr) · line 574

Quoted 1 line
arch/arm64/kvm/pmu-direct.c +1 -0
+			write_sysreg(hw_ovf, pmovsclr_el0);

[Severity: Critical]
Does this break the guest's ability to read the overflow bit?

The hardware overflow register is cleared here and the state is saved to the software context, but the canonical software state is never written back to the hardware before the guest accesses it directly.

Because the partitioned PMU grants the guest direct, untrapped EL0/EL1 access to PMOVSSET_EL0 for its assigned counters, failing to restore this will cause the guest to read a cleared hardware register (a 0 value) upon resuming execution, permanently breaking its ability to identify which counter overflowed.

Quoted 22 lines
arch/arm64/kvm/pmu-direct.c +17 -0
+		}
+		pmint = read_pmintenset();
+		pmcr = read_pmcr();
+	} else {
+		pmint = __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
+		pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
+	}
+
+	pmovs = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
+	local_irq_restore(flags);
+
+	overflow = (pmcr & ARMV8_PMU_PMCR_E) && (mask & pmovs & pmint);
+
+	if (overflow && kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_FREE)
+		kvm_pmu_set_guest_owned(vcpu);
+
+	return overflow;
 }
arch/arm64/kvm/pmu-emul.c +0 -0

[ ... ]

arch/arm64/kvm/pmu-emul.c bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu) · line 302

Quoted 9 lines
arch/arm64/kvm/pmu-emul.c +3 -1
@@ -296,8 +296,14 @@ bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
 {
 	struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
 	bool run_level = sregs->device_irq_level & KVM_ARM_DEV_PMU;
+	bool overflow;
 
-	return kvm_pmu_overflow_status(vcpu) != run_level;
+	if (kvm_pmu_is_partitioned(vcpu->kvm))
+		overflow = kvm_pmu_part_overflow_status(vcpu);

[Severity: Medium]
Does this call introduce hardware-altering side effects into a runnable status predicate?

kvm_arch_vcpu_runnable() evaluates kvm_pmu_should_notify_user() in sleep polling loops (like kvm_vcpu_block). However, kvm_pmu_part_overflow_status() conditionally transitions PMU access to VCPU_PMU_ACCESS_GUEST_OWNED and invokes kvm_pmu_load(), forcing hardware register writes from a pure getter.

Triggering hardware reprogramming inside waitqueue checks violates architectural layering and could introduce latency or synchronization issues.

Quoted 5 lines
arch/arm64/kvm/pmu-emul.c +4 -0
+	else
+		overflow = kvm_pmu_emul_overflow_status(vcpu);
+
+	return overflow != run_level;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
#41 lore raw

PATCH v9 20/22 KVM: arm64: Add vCPU device attr to partition the PMU

In reply to Colton Lewis

Add the KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION vCPU device attribute to enable a Partitioned PMU for a VM where PMUv3 and VHE are supported.

When partitioning is enabled (tracked via KVM_ARCH_FLAG_PARTITION_PMU_ENABLED), userspace must explicitly configure the number of guest event counters via KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS with a value strictly less than the maximum number of general-purpose counters implemented by the PMU (or 0 only when FEAT_HPMN0 is supported), leaving at least one general-purpose counter reserved for host profiling prior to calling KVM_ARM_VCPU_PMU_V3_INIT.

---
 Documentation/virt/kvm/devices/vcpu.rst | 42 ++++++++++++++-
 arch/arm64/include/asm/kvm_host.h       |  1 +
 arch/arm64/include/uapi/asm/kvm.h       |  2 +
 arch/arm64/kvm/pmu.c                    | 71 ++++++++++++++++++++++++-
 arch/arm64/kvm/sys_regs.c               |  5 +-
 5 files changed, 118 insertions(+), 3 deletions(-)
Documentation/virt/kvm/devices/vcpu.rst +41 -1
index deb5c51bc00c8..fb5921ed9dea2 100644
@@ -57,6 +57,9 @@ Returns:
                   hardware PMU, or interrupt number not set (non-GICv5
                   guests, only)
 	 -EBUSY   PMUv3 already initialized
+	 -EINVAL  Partitioning enabled without explicitly configuring
+		  fewer than max_counters via
+		  KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS
 	 =======  ======================================================
 
 Request the initialization of the PMUv3.  If using the PMUv3 with an in-kernel
@@ -162,7 +165,8 @@ the cpu field to the processor id.
 	 -EFAULT  Error accessing the value pointed to by addr
 	 -ENODEV  PMUv3 not supported or GIC not initialized
 	 -EINVAL  No PMUv3 explicitly selected, or value of N out of
-	 	  range
+	 	  range (or N >= max_counters when partitioning is
+		  enabled)
 	 =======  ====================================================
 
 Set the number of implemented event counters in the virtual PMU. This
@@ -172,6 +176,42 @@ explicitly selected, or the number of counters is out of range for the
 selected PMU. Selecting a new PMU cancels the effect of setting this
 attribute.
 
+1.6 ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION
+---------------------------------------------------
+
+:Parameters: in kvm_device_attr.addr the address to an unsigned int (u32)
+	     boolean value (non-zero to enable PMU partitioning, 0 to disable)
+
+:Returns:
+
+	 =======  ========================================================
+	 -EBUSY   PMUv3 already initialized or a VCPU has already run
+	 -EFAULT  Error accessing the value pointed to by addr
+	 -ENODEV  KVM_ARM_VCPU_PMU_V3 feature missing from VCPU
+	 -EPERM   Host hardware or kernel configuration does not support
+		  PMU partitioning (requires ARM64 VHE mode and PMUv3)
+	 -EINVAL  No PMUv3 associated with the VM, or
+		  KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS was already set to
+		  >= max_counters
+	 -ENXIO   Returned by KVM_HAS_DEVICE_ATTR when PMU partitioning is
+		  unsupported on host hardware
+	 =======  ========================================================
+
+Enable or disable hardware PMU partitioning for the VM. When enabled, physical
+PMUv3 hardware counters are partitioned between the guest and host using
+MDCR_EL2.HPMN (and FEAT_FGT fine-grained traps when supported by hardware).
+This grants the guest direct, untrapped EL0/EL1 hardware access to event
+counters 0..HPMN-1 and the cycle counter (PMCCNTR_EL0).
+
+When PMU partitioning is enabled, userspace must explicitly configure the
+number of guest event counters via KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS with a
+value strictly less than the maximum number of general-purpose counters
+implemented by the PMU (leaving at least one general-purpose counter reserved
+for host profiling) prior to calling KVM_ARM_VCPU_PMU_V3_INIT. Note that
+KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS configures general-purpose event counters
+(PMCR_EL0.N / MDCR_EL2.HPMN); the dedicated cycle counter (PMCCNTR_EL0) is
+unconditionally assigned to the guest partition when partitioning is enabled.
+
 2. GROUP: KVM_ARM_VCPU_TIMER_CTRL
 =================================
 
arch/arm64/include/asm/kvm_host.h +1 -0
index 8dff576667d10..6180b977d8965 100644
@@ -388,6 +388,7 @@ struct kvm_arch {
 
 	/* Maximum number of counters for the guest */
 	u8 nr_pmu_counters;
+	bool pmu_nr_counters_specified;
 
 	/* PMMIR_EL1.SLOTS value exposed to the guest. */
 	u8 pmmir_slots;
arch/arm64/include/uapi/asm/kvm.h +2 -0
index 019e5e3d892e6..9d38090eb5e71 100644
@@ -438,6 +438,8 @@ enum {
 #define   KVM_ARM_VCPU_PMU_V3_FILTER		2
 #define   KVM_ARM_VCPU_PMU_V3_SET_PMU		3
 #define   KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS	4
+#define   KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION	5
+
 #define KVM_ARM_VCPU_TIMER_CTRL		1
 #define   KVM_ARM_VCPU_TIMER_IRQ_VTIMER		0
 #define   KVM_ARM_VCPU_TIMER_IRQ_PTIMER		1
arch/arm64/kvm/pmu.c +70 -1

119 lines not on this page · raw message

arch/arm64/kvm/sys_regs.c +4 -1
index 20c46ef705d30..a4779593c4f8d 100644
@@ -1756,7 +1756,10 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
 	if (!kvm_vm_has_ran_once(kvm) &&
 	    !vcpu_has_nv(vcpu)	      &&
 	    !kvm_vcpu_has_pmuv3_strict(vcpu) &&
-	    new_n <= kvm_arm_pmu_get_max_counters(kvm))
+	    !kvm->arch.pmu_nr_counters_specified &&
+	    new_n <= kvm_arm_pmu_get_max_counters(kvm) &&
+	    (!kvm_pmu_is_partitioned(kvm) ||
+	     new_n < kvm_arm_pmu_get_max_counters(kvm)))
 		kvm->arch.nr_pmu_counters = new_n;
 
 	mutex_unlock(&kvm->arch.config_lock);
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#42 lore raw

Re: [PATCH v9 20/22] KVM: arm64: Add vCPU device attr to partition the PMU

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] kvm: Missing FEAT_HPMN0 validation allows PMU partition mismatch breaking host profiling --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] kvm: Missing FEAT_HPMN0 validation allows PMU partition mismatch breaking host profiling --

commit 35f982d966d35bba8af3b23d939070b539de71cf
Author: Colton Lewis <[email protected]>

KVM: arm64: Add vCPU device attr to partition the PMU

This commit introduces the KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION attribute to enable partitioned PMU support for a VM. When partitioning is enabled, userspace must configure the number of guest event counters strictly less than the maximum implemented by the PMU, reserving at least one for host profiling.

Quoted 4 lines
arch/arm64/kvm/pmu.c +0 -0
index 4a3c6600b2678..31e46a5e937c7 100644

[ ... ]

Quoted 13 lines
arch/arm64/kvm/pmu.c +7 -0
@@ -667,14 +694,26 @@ static int kvm_arm_pmu_v3_set_nr_counters(struct kvm_vcpu *vcpu, unsigned int n)
 {
 	struct kvm *kvm = vcpu->kvm;
+	unsigned int max_counters;
+
+	if (kvm_vm_has_ran_once(kvm) ||
+	    (kvm_arm_pmu_any_vcpu_created(kvm) &&
+	     (!kvm->arch.pmu_nr_counters_specified ||
+	      kvm->arch.nr_pmu_counters != n)))
+		return -EBUSY;
 
 	if (!kvm->arch.arm_pmu)
 		return -EINVAL;

arch/arm64/kvm/pmu.c static int kvm_arm_pmu_v3_set_nr_counters(struct kvm_vcpu *vcpu, unsigned int n) · line 714

Quoted 10 lines
arch/arm64/kvm/pmu.c +5 -1
 
-	if (n > kvm_arm_pmu_get_max_counters(kvm))
+	max_counters = kvm_arm_pmu_get_max_counters(kvm);
+	if (n > max_counters)
+		return -EINVAL;
+
+	if (kvm_pmu_is_partitioned(kvm) && n >= max_counters)
 		return -EINVAL;
 
 	kvm_arm_set_nr_counters(kvm, n);

[Severity: Medium]
The commit message states that the number of guest event counters must be configured "with a value strictly less than the maximum... or 0 only when FEAT_HPMN0 is supported".

Is the FEAT_HPMN0 check missing from kvm_arm_pmu_v3_set_nr_counters() here?

If n is set to 0 on hardware without ARM64_HAS_HPMN0, the hardware fallback in debug.c will prevent a panic by leaving HPMN set to max_counters. However, kvm_pmu_host_counter_mask() will still assume all counters belong to the host.

Because the hardware assigns all counters to the guest partition in this state, does this mismatch break host-side performance profiling?

[ ... ]

Quoted 4 lines
arch/arm64/kvm/sys_regs.c +0 -0
index 20c46ef705d30..a4779593c4f8d 100644

[ ... ]

arch/arm64/kvm/sys_regs.c static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, · line 1763

Quoted 10 lines
arch/arm64/kvm/sys_regs.c +4 -1
@@ -1756,7 +1756,10 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,
 	if (!kvm_vm_has_ran_once(kvm) &&
 	    !vcpu_has_nv(vcpu)	      &&
 	    !kvm_vcpu_has_pmuv3_strict(vcpu) &&
-	    new_n <= kvm_arm_pmu_get_max_counters(kvm))
+	    !kvm->arch.pmu_nr_counters_specified &&
+	    new_n <= kvm_arm_pmu_get_max_counters(kvm) &&
+	    (!kvm_pmu_is_partitioned(kvm) ||
+	     new_n < kvm_arm_pmu_get_max_counters(kvm)))
 		kvm->arch.nr_pmu_counters = new_n;

[Severity: Medium]
Can userspace also bypass the FEAT_HPMN0 validation by writing a 0 to PMCR_EL0.N via SET_ONE_REG?

Without a check for FEAT_HPMN0 in set_pmcr(), it appears nr_pmu_counters could be set to 0, which would lead to the same host profiling breakage described above.

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

Re: [PATCH v9 20/22] KVM: arm64: Add vCPU device attr to partition the PMU

In reply to Colton Lewis
Quoted 36 lines · Colton Lewis

Add the KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION vCPU device attribute to enable a Partitioned PMU for a VM where PMUv3 and VHE are supported.

When partitioning is enabled (tracked via KVM_ARCH_FLAG_PARTITION_PMU_ENABLED), userspace must explicitly configure the number of guest event counters via KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS with a value strictly less than the maximum number of general-purpose counters implemented by the PMU (or 0 only when FEAT_HPMN0 is supported), leaving at least one general-purpose counter reserved for host profiling prior to calling KVM_ARM_VCPU_PMU_V3_INIT.

---
  Documentation/virt/kvm/devices/vcpu.rst | 42 ++++++++++++++-
  arch/arm64/include/asm/kvm_host.h       |  1 +
  arch/arm64/include/uapi/asm/kvm.h       |  2 +
  arch/arm64/kvm/pmu.c                    | 71 ++++++++++++++++++++++++-
  arch/arm64/kvm/sys_regs.c               |  5 +-
  5 files changed, 118 insertions(+), 3 deletions(-)
Documentation/virt/kvm/devices/vcpu.rst +3 -0
index deb5c51bc00c8..fb5921ed9dea2 100644
@@ -57,6 +57,9 @@ Returns:
                    hardware PMU, or interrupt number not set (non-GICv5
                    guests, only)
  	 -EBUSY   PMUv3 already initialized
+	 -EINVAL  Partitioning enabled without explicitly configuring
+		  fewer than max_counters via
+		  KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS
  	 =======  ======================================================
 
  Request the initialization of the PMUv3.  If using the PMUv3 with an in-kernel

Documentation/virt/kvm/devices/vcpu.rst the cpu field to the processor id. · line 169

Quoted 6 lines
Documentation/virt/kvm/devices/vcpu.rst +2 -1
@@ -162,7 +165,8 @@ the cpu field to the processor id.
  	 -EFAULT  Error accessing the value pointed to by addr
  	 -ENODEV  PMUv3 not supported or GIC not initialized
  	 -EINVAL  No PMUv3 explicitly selected, or value of N out of
-	 	  range
+	 	  range (or N >= max_counters when partitioning is
+		  enabled)

"No PMUv3 explicitly selected, or value of N > max_counters, or N > max_counters - 1 when partitioning is enabled".

That might be clearer, otherwise it seems like max_counters being relevant to "out of range" only applies to when partitioning is enabled, not that it's max_counters all the time but one is -1.

#44 lore raw

Re: [PATCH v9 20/22] KVM: arm64: Add vCPU device attr to partition the PMU

Hi James,

Quoted 12 lines · James Clark

Review Parameters: in:imaan address of a u32 value (N) · line 85

Quoted 8 lines
(quoted diff) +3 -1
@@ -77,7 +80,9 @@ Parameters: in:imaan address of a u32 value (N)
  Returns:
  	 =======  ====================================================
  	 -EBUSY   PMUv3 already initialized, or a vCPU has already run
-	 -EINVAL  No PMUv3 explicitly selected, or value of N out of range
+	 -EINVAL  No PMUv3 explicitly selected, or value of N out of
+		  range (>= max_counters when partitioning is
+		  enabled)

"No PMUv3 explicitly selected, or value of N > max_counters, or N > max_counters - 1 when partitioning is enabled".

Quoted 3 lines

That might be clearer, otherwise it seems like max_counters being relevant to "out of range" only applies to when partitioning is enabled, not that it's max_counters all the time but one is -1.

Agreed, that reads much better. I will update it to your phrasing in v10.

Thanks,
Colton

#45 lore raw

PATCH v9 21/22 KVM: selftests: Add find_bit to KVM library

In reply to Colton Lewis

Add lib/find_bit.c to the KVM selftests build using the same wrapper pattern as lib/rbtree.c so selftests that depend on find_bit helpers link cleanly when built standalone.

---
 tools/testing/selftests/kvm/Makefile.kvm   | 1 +
 tools/testing/selftests/kvm/lib/find_bit.c | 2 ++
 2 files changed, 3 insertions(+)
 create mode 100644 tools/testing/selftests/kvm/lib/find_bit.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
index 96bab7002d39e..cfd1a4fe09ec4 100644
@@ -5,6 +5,7 @@ all:
 
 LIBKVM += lib/assert.c
 LIBKVM += lib/elf.c
+LIBKVM += lib/find_bit.c
 LIBKVM += lib/guest_modes.c
 LIBKVM += lib/io.c
 LIBKVM += lib/kvm_util.c
tools/testing/selftests/kvm/lib/find_bit.c new file +2 -0
new file mode 100644
index 0000000000000..5534248c663f7
@@ -0,0 +1,2 @@
+// SPDX-License-Identifier: GPL-2.0
+#include "../../../../lib/find_bit.c"
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#46 lore raw

Re: [PATCH v9 21/22] KVM: selftests: Add find_bit to KVM library

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 5 lines

Add lib/find_bit.c to the KVM selftests build using the same wrapper pattern as lib/rbtree.c so selftests that depend on find_bit helpers link cleanly when built standalone.

Sashiko has reviewed this patch and found no issues. It looks great!

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

PATCH v9 22/22 KVM: arm64: selftests: Add test case for Partitioned PMU

In reply to Colton Lewis

Extend arm64/vpmu_counter_access to run all PMU counter access, PMCR.N validation, and overflow interrupt tests against both the emulated vPMU and the Partitioned PMU.

Introduce enum vpmu_impl (EMULATED_VPMU vs. PARTITIONED_VPMU) and thread it through the test setup helpers to configure KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION and KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS when testing the Partitioned PMU.

Skip out-of-bounds counter access exception tests when the PMU is partitioned, as hardware does not guarantee an UNDEFINED exception on accesses to unimplemented counters.

---
 .../selftests/kvm/arm64/vpmu_counter_access.c | 129 ++++++++++++++----
 1 file changed, 99 insertions(+), 30 deletions(-)
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +99 -30
index 22223395969e0..811f8156cea3c 100644
@@ -25,12 +25,27 @@
 /* The cycle counter bit position that's common among the PMU registers */
 #define ARMV8_PMU_CYCLE_IDX		31
 
+enum pmu_impl {
+	EMULATED,
+	PARTITIONED
+};
+
+const char *pmu_impl_str[] = {
+	"Emulated",
+	"Partitioned"
+};
+
 struct vpmu_vm {
 	struct kvm_vm *vm;
 	struct kvm_vcpu *vcpu;
 };
 
+struct guest_context {
+	bool pmu_partitioned;
+};
+
 static struct vpmu_vm vpmu_vm;
+static struct guest_context guest_context;
 
 struct pmreg_sets {
 	u64 set_reg_id;
@@ -331,11 +346,16 @@ static void test_access_invalid_pmc_regs(struct pmc_accessor *acc, int pmc_idx)
 	/*
 	 * Reading/writing the event count/type registers should cause
 	 * an UNDEFINED exception.
+	 *
+	 * If the pmu is partitioned, we can't guarantee it because
+	 * hardware doesn't.
 	 */
-	TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->read_cntr(pmc_idx));
-	TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->write_cntr(pmc_idx, 0));
-	TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->read_typer(pmc_idx));
-	TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->write_typer(pmc_idx, 0));
+	if (!guest_context.pmu_partitioned) {
+		TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->read_cntr(pmc_idx));
+		TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->write_cntr(pmc_idx, 0));
+		TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->read_typer(pmc_idx));
+		TEST_EXCEPTION(ESR_ELx_EC_UNKNOWN, acc->write_typer(pmc_idx, 0));
+	}
 	/*
 	 * The bit corresponding to the (unimplemented) counter in
 	 * {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers should be RAZ.
@@ -399,7 +419,7 @@ static void guest_code(u64 expected_pmcr_n)
 }
 
 /* Create a VM that has one vCPU with PMUv3 configured. */
-static void create_vpmu_vm(void *guest_code)
+static void create_vpmu_vm(void *guest_code, enum pmu_impl impl)
 {
 	struct kvm_vcpu_init init;
 	u8 pmuver, ec;
@@ -409,6 +429,13 @@ static void create_vpmu_vm(void *guest_code)
 		.attr = KVM_ARM_VCPU_PMU_V3_IRQ,
 		.addr = (u64)&irq,
 	};
+	u32 partition = (impl == PARTITIONED);
+	struct kvm_device_attr part_attr = {
+		.group = KVM_ARM_VCPU_PMU_V3_CTRL,
+		.attr = KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION,
+		.addr = (uint64_t)&partition
+	};
+	int ret;
 
 	/* The test creates the vpmu_vm multiple times. Ensure a clean state */
 	memset(&vpmu_vm, 0, sizeof(vpmu_vm));
@@ -436,6 +463,15 @@ static void create_vpmu_vm(void *guest_code)
 		    "Unexpected PMUVER (0x%x) on the vCPU with PMUv3", pmuver);
 
 	vcpu_ioctl(vpmu_vm.vcpu, KVM_SET_DEVICE_ATTR, &irq_attr);
+
+	ret = __vcpu_has_device_attr(
+		vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION);
+	if (!ret) {
+		vcpu_ioctl(vpmu_vm.vcpu, KVM_SET_DEVICE_ATTR, &part_attr);
+		guest_context.pmu_partitioned = partition;
+		pr_debug("Set PMU partitioning: %d\n", partition);
+	}
+
 }
 
 static void destroy_vpmu_vm(void)
@@ -461,13 +497,14 @@ static void run_vcpu(struct kvm_vcpu *vcpu, u64 pmcr_n)
 	}
 }
 
-static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, bool expect_fail)
+static void test_create_vpmu_vm_with_nr_counters(
+	unsigned int nr_counters, enum pmu_impl impl, bool expect_fail)
 {
 	struct kvm_vcpu *vcpu;
 	unsigned int prev;
 	int ret;
 
-	create_vpmu_vm(guest_code);
+	create_vpmu_vm(guest_code, impl);
 	vcpu = vpmu_vm.vcpu;
 
 	prev = get_pmcr_n(vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0)));
@@ -475,21 +512,24 @@ static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, bool
 	ret = __vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL,
 				     KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS, &nr_counters);
 
-	if (expect_fail)
+	if (expect_fail) {
 		TEST_ASSERT(ret && errno == EINVAL,
 			    "Setting more PMU counters (%u) than available (%u) unexpectedly succeeded",
 			    nr_counters, prev);
-	else
-		TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret));
+		return;
+	}
+
+	TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret));
 
 	vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT, NULL);
+	sync_global_to_guest(vpmu_vm.vm, guest_context);
 }
 
 /*
  * Create a guest with one vCPU, set the PMCR_EL0.N for the vCPU to @pmcr_n,
  * and run the test.
  */
-static void run_access_test(u64 pmcr_n)
+static void run_access_test(u64 pmcr_n, enum pmu_impl impl)
 {
 	u64 sp;
 	struct kvm_vcpu *vcpu;
@@ -497,7 +537,7 @@ static void run_access_test(u64 pmcr_n)
 
 	pr_debug("Test with pmcr_n %lu\n", pmcr_n);
 
-	test_create_vpmu_vm_with_nr_counters(pmcr_n, false);
+	test_create_vpmu_vm_with_nr_counters(pmcr_n, impl, false);
 	vcpu = vpmu_vm.vcpu;
 
 	/* Save the initial sp to restore them later to run the guest again */
@@ -531,14 +571,14 @@ static struct pmreg_sets validity_check_reg_sets[] = {
  * Create a VM, and check if KVM handles the userspace accesses of
  * the PMU register sets in @validity_check_reg_sets[] correctly.
  */
-static void run_pmregs_validity_test(u64 pmcr_n)
+static void run_pmregs_validity_test(u64 pmcr_n, enum pmu_impl impl)
 {
 	int i;
 	struct kvm_vcpu *vcpu;
 	u64 set_reg_id, clr_reg_id, reg_val;
 	u64 valid_counters_mask, max_counters_mask;
 
-	test_create_vpmu_vm_with_nr_counters(pmcr_n, false);
+	test_create_vpmu_vm_with_nr_counters(pmcr_n, impl, false);
 	vcpu = vpmu_vm.vcpu;
 
 	valid_counters_mask = get_counters_mask(pmcr_n);
@@ -588,11 +628,11 @@ static void run_pmregs_validity_test(u64 pmcr_n)
  * the vCPU to @pmcr_n, which is larger than the host value.
  * The attempt should fail as @pmcr_n is too big to set for the vCPU.
  */
-static void run_error_test(u64 pmcr_n)
+static void run_error_test(u64 pmcr_n, enum pmu_impl impl)
 {
-	pr_debug("Error test with pmcr_n %lu (larger than the host)\n", pmcr_n);
+	pr_debug("Error test with pmcr_n %lu (larger than the host allows)\n", pmcr_n);
 
-	test_create_vpmu_vm_with_nr_counters(pmcr_n, true);
+	test_create_vpmu_vm_with_nr_counters(pmcr_n, impl, true);
 	destroy_vpmu_vm();
 }
 
@@ -600,21 +640,26 @@ static void run_error_test(u64 pmcr_n)
  * Return the default number of implemented PMU event counters excluding
  * the cycle counter (i.e. PMCR_EL0.N value) for the guest.
  */
-static u64 get_pmcr_n_limit(void)
+static u64 get_pmcr_n_limit(enum pmu_impl impl)
 {
-	u64 pmcr;
+	u64 pmcr, n;
 
-	create_vpmu_vm(guest_code);
+	create_vpmu_vm(guest_code, impl);
 	pmcr = vcpu_get_reg(vpmu_vm.vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0));
 	destroy_vpmu_vm();
-	return get_pmcr_n(pmcr);
+
+	n = get_pmcr_n(pmcr);
+	if (impl == PARTITIONED && n > 0)
+		return n - 1;
+
+	return n;
 }
 
 static bool kvm_supports_nr_counters_attr(void)
 {
 	bool supported;
 
-	create_vpmu_vm(NULL);
+	create_vpmu_vm(NULL, EMULATED);
 	supported = !__vcpu_has_device_attr(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL,
 					    KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS);
 	destroy_vpmu_vm();
@@ -622,22 +667,46 @@ static bool kvm_supports_nr_counters_attr(void)
 	return supported;
 }
 
-int main(void)
+static bool kvm_supports_partition_attr(void)
+{
+	bool supported;
+
+	create_vpmu_vm(NULL, EMULATED);
+	supported = !__vcpu_has_device_attr(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL,
+					    KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION);
+	destroy_vpmu_vm();
+
+	return supported;
+}
+
+void test_pmu(enum pmu_impl impl)
 {
 	u64 i, pmcr_n;
 
-	TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_PMU_V3));
-	TEST_REQUIRE(kvm_supports_vgic_v3());
-	TEST_REQUIRE(kvm_supports_nr_counters_attr());
+	pr_info("Testing PMU: Implementation = %s\n", pmu_impl_str[impl]);
+
+	pmcr_n = get_pmcr_n_limit(impl);
+	pr_debug("PMCR_EL0.N: Limit = %lu\n", pmcr_n);
 
-	pmcr_n = get_pmcr_n_limit();
 	for (i = 0; i <= pmcr_n; i++) {
-		run_access_test(i);
-		run_pmregs_validity_test(i);
+		run_access_test(i, impl);
+		run_pmregs_validity_test(i, impl);
 	}
 
 	for (i = pmcr_n + 1; i < ARMV8_PMU_MAX_COUNTERS; i++)
-		run_error_test(i);
+		run_error_test(i, impl);
+}
+
+int main(void)
+{
+	TEST_REQUIRE(kvm_has_cap(KVM_CAP_ARM_PMU_V3));
+	TEST_REQUIRE(kvm_supports_vgic_v3());
+	TEST_REQUIRE(kvm_supports_nr_counters_attr());
+
+	test_pmu(EMULATED);
+
+	if (kvm_supports_partition_attr() && get_pmcr_n_limit(EMULATED) > 0)
+		test_pmu(PARTITIONED);
 
 	return 0;
 }
Signature · 1 line
2.56.0.rc1.310.g51773c2048-goog
#48 lore raw

Re: [PATCH v9 22/22] KVM: arm64: selftests: Add test case for Partitioned PMU

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] selftests: code and commit message mismatch for enum pmu_impl --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] selftests: code and commit message mismatch for enum pmu_impl
--

commit 46faadbd8c25e77d7c2d05f65dc67de455edf047
Author: Colton Lewis <[email protected]>

KVM: arm64: selftests: Add test case for Partitioned PMU

This commit extends the vpmu_counter_access test to run against both the emulated vPMU and Partitioned PMU. It introduces a new enum to represent the implementation type and skips out-of-bounds counter access exception tests when partitioned.

Quoted 2 lines

Introduce enum vpmu_impl (EMULATED_VPMU vs. PARTITIONED_VPMU) and thread it through the test setup helpers to configure

[Severity: Low]
This isn't a bug in the code, but there is a mismatch between the commit message and the actual enum definition. The commit message states it introduces enum vpmu_impl with values EMULATED_VPMU and PARTITIONED_VPMU.

tools/testing/selftests/kvm/arm64/vpmu_counter_access.c line 31

Quoted 12 lines
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +4 -0
index 22223395969e0..811f8156cea3c 100644
@@ -25,12 +25,27 @@
 /* The cycle counter bit position that's common among the PMU registers */
 #define ARMV8_PMU_CYCLE_IDX		31
 
+enum pmu_impl {
+	EMULATED,
+	PARTITIONED
+};

[Severity: Low]
Could the commit message be updated to match the actual enum pmu_impl and its values used here in vpmu_counter_access.c?

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

PATCH 1/1 target/arm: Enable KVM PMU partitioning and counter limit

In reply to Colton Lewis

Add CPU properties to configure KVM ARM64 PMU counter partitioning:
- 'pmu-partition' (bool): enables KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION
- 'pmu-num-counters' (uint32): sets KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS

When 'pmu-partition=on' and 'pmu-num-counters=N' are passed via -cpu, QEMU enables hardware PMU partitioning and reserves N general-purpose event counters for the guest prior to calling KVM_ARM_VCPU_PMU_V3_INIT.

Note that this patch is an example to demonstrate the KVM ARM64 PMU Partitioning uAPI [1]. That is why it aborts immediately if setting the device attributes does not succeed.

[1] https://lore.kernel.org/kvmarm/[email protected]/

---
 target/arm/cpu.c | 20 ++++++++++++++++++++
 target/arm/cpu.h |  4 ++++
 target/arm/kvm.c | 32 ++++++++++++++++++++++++++++++++
 3 files changed, 56 insertions(+)
target/arm/cpu.c +20 -0
index f58a1db843..7538c6a24c 100644
@@ -1359,6 +1359,20 @@ static bool arm_get_pmu(Object *obj, Error **errp)
     return cpu->has_pmu;
 }
 
+static bool arm_get_pmu_partition(Object *obj, Error **errp)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    return cpu->kvm_pmu_partition;
+}
+
+static void arm_set_pmu_partition(Object *obj, bool value, Error **errp)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    cpu->kvm_pmu_partition = value;
+}
+
 static void arm_set_pmu(Object *obj, bool value, Error **errp)
 {
     ARMCPU *cpu = ARM_CPU(obj);
@@ -1604,6 +1618,12 @@ static void arm_cpu_post_init(Object *obj)
     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
         cpu->has_pmu = true;
         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
+        object_property_add_bool(obj, "pmu-partition",
+                                 arm_get_pmu_partition,
+                                 arm_set_pmu_partition);
+        object_property_add_uint32_ptr(obj, "pmu-num-counters",
+                                       &cpu->kvm_pmu_nr_counters,
+                                       OBJ_PROP_FLAG_READWRITE);
     }
 
     /*
target/arm/cpu.h +4 -0
index e3f931dba2..1320e76fd4 100644
@@ -1004,6 +1004,10 @@ struct ArchCPU {
     bool has_el3;
     /* CPU has PMU (Performance Monitor Unit) */
     bool has_pmu;
+    /* CPU has PMU Partitioning enabled in KVM mode */
+    bool kvm_pmu_partition;
+    /* Number of PMU counters allocated to guest in KVM mode */
+    uint32_t kvm_pmu_nr_counters;
     /* CPU has VFP */
     bool has_vfp;
     /* CPU has 32 VFP registers */
target/arm/kvm.c +32 -0
index ed99be7fd8..3aa8243278 100644
@@ -1846,6 +1846,10 @@ static bool kvm_arm_set_device_attr(ARMCPU *cpu, struct kvm_device_attr *attr,
     return true;
 }
 
+#ifndef KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION
+#define KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION 5
+#endif
+
 void kvm_arm_pmu_init(ARMCPU *cpu)
 {
     struct kvm_device_attr attr = {
@@ -1856,6 +1860,34 @@ void kvm_arm_pmu_init(ARMCPU *cpu)
     if (!cpu->has_pmu) {
         return;
     }
+
+    if (cpu->kvm_pmu_partition) {
+        uint32_t partition = 1;
+        struct kvm_device_attr part_attr = {
+            .group = KVM_ARM_VCPU_PMU_V3_CTRL,
+            .attr = KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION,
+            .addr = (intptr_t)&partition,
+        };
+
+        if (!kvm_arm_set_device_attr(cpu, &part_attr, "PMU partition")) {
+            error_report("failed to enable KVM PMU partition");
+            abort();
+        }
+    }
+
+    if (cpu->kvm_pmu_nr_counters > 0) {
+        struct kvm_device_attr cnt_attr = {
+            .group = KVM_ARM_VCPU_PMU_V3_CTRL,
+            .attr = KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS,
+            .addr = (intptr_t)&cpu->kvm_pmu_nr_counters,
+        };
+
+        if (!kvm_arm_set_device_attr(cpu, &cnt_attr, "PMU nr_counters")) {
+            error_report("failed to set KVM PMU nr_counters");
+            abort();
+        }
+    }
+
     if (!kvm_arm_set_device_attr(cpu, &attr, "PMU")) {
         error_report("failed to init PMU");
         abort();

base-commit: 014b1f730c0faa08067f8e9b5829e62ea3d4b177

Signature · 1 line
2.56.0.rc1.315.gc6ed9934b7-goog
#50 lore raw

Re: [PATCH] target/arm: Enable KVM PMU partitioning and counter limit

Quoted 9 lines · Colton Lewis

Add CPU properties to configure KVM ARM64 PMU counter partitioning:
- 'pmu-partition' (bool): enables KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION
- 'pmu-num-counters' (uint32): sets KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS

When 'pmu-partition=on' and 'pmu-num-counters=N' are passed via -cpu, QEMU enables hardware PMU partitioning and reserves N general-purpose event counters for the guest prior to calling KVM_ARM_VCPU_PMU_V3_INIT.

Quoted 7 lines

Note that this patch is an example to demonstrate the KVM ARM64 PMU Partitioning uAPI [1]. That is why it aborts immediately if setting the device attributes does not succeed.

[1] https://lore.kernel.org/kvmarm/[email protected]/

FYI for next time around, QEMU patches that depend on not-yet-accepted kernel ABI changes should be tagged RFC, please.

Quoted 4 lines
---
 target/arm/cpu.c | 20 ++++++++++++++++++++
 target/arm/cpu.h |  4 ++++
 target/arm/kvm.c | 32 ++++++++++++++++++++++++++++++++

New CPU properties should be documented in docs/system/arm/cpu-features.rst .

Quoted 13 lines
 3 files changed, 56 insertions(+)
target/arm/cpu.c +3 -0
index f58a1db843..7538c6a24c 100644
@@ -1359,6 +1359,20 @@ static bool arm_get_pmu(Object *obj, Error **errp)
     return cpu->has_pmu;
 }
 
+static bool arm_get_pmu_partition(Object *obj, Error **errp)
+{
+    ARMCPU *cpu = ARM_CPU(obj);

target/arm/cpu.c static bool arm_get_pmu(Object *obj, Error **errp) · line 1374

Quoted 10 lines
target/arm/cpu.c +10 -0
+
+    return cpu->kvm_pmu_partition;
+}
+
+static void arm_set_pmu_partition(Object *obj, bool value, Error **errp)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    cpu->kvm_pmu_partition = value;
+}

If your getter/setter are only updating the struct field, you can use a DEFINE_PROP_BOOL to avoid having to have them (compare arm_cpu_has_el2_property etc.)

Quoted 5 lines
target/arm/cpu.c +1 -0
+
 static void arm_set_pmu(Object *obj, bool value, Error **errp)
 {
     ARMCPU *cpu = ARM_CPU(obj);

target/arm/cpu.c static void arm_cpu_post_init(Object *obj) · line 1626

Quoted 9 lines
target/arm/cpu.c +6 -0
@@ -1604,6 +1618,12 @@ static void arm_cpu_post_init(Object *obj)
     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
         cpu->has_pmu = true;
         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
+        object_property_add_bool(obj, "pmu-partition",
+                                 arm_get_pmu_partition,
+                                 arm_set_pmu_partition);
+        object_property_add_uint32_ptr(obj, "pmu-num-counters",
+                                       &cpu->kvm_pmu_nr_counters,
+                                       OBJ_PROP_FLAG_READWRITE);

What should we do with these properties if we're not using KVM (i.e. for TCG, hvf, etc) ?

thanks
-- PMM

#51 lore raw

Re: [PATCH] target/arm: Enable KVM PMU partitioning and counter limit

Hi Peter,

Thanks for reviewing!

Quoted 2 lines · Peter Maydell

FYI for next time around, QEMU patches that depend on not-yet-accepted kernel ABI changes should be tagged RFC, please.

Ack, sorry about that. I will tag future versions with RFC until the kernel uAPI is accepted.

Quoted 6 lines
Quoted 4 lines
---
 target/arm/cpu.c | 20 ++++++++++++++++++++
 target/arm/cpu.h |  4 ++++
 target/arm/kvm.c | 32 ++++++++++++++++++++++++++++++++

New CPU properties should be documented in docs/system/arm/cpu-features.rst .

Will document them there in the next revision.

Quoted 9 lines
Quoted 9 lines
target/arm/cpu.c +9 -0
+static bool arm_get_pmu_partition(Object *obj, Error **errp)
+{
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    return cpu->kvm_pmu_partition;
+}
+
+static void arm_set_pmu_partition(Object *obj, bool value, Error **errp)
+{
Quoted 8 lines

target/arm/cpu.c static bool arm_get_pmu(Object *obj, Error **errp) · line 1374

Quoted 4 lines
target/arm/cpu.c +4 -0
+    ARMCPU *cpu = ARM_CPU(obj);
+
+    cpu->kvm_pmu_partition = value;
+}

If your getter/setter are only updating the struct field, you can use a DEFINE_PROP_BOOL to avoid having to have them (compare arm_cpu_has_el2_property etc.)

Thanks, I will switch to DEFINE_PROP_BOOL.

Quoted 10 lines

target/arm/cpu.c static void arm_cpu_post_init(Object *obj) · line 1626

Quoted 9 lines
     if (arm_feature(&cpu->env, ARM_FEATURE_PMU)) {
         cpu->has_pmu = true;
         object_property_add_bool(obj, "pmu", arm_get_pmu, arm_set_pmu);
target/arm/cpu.c +6 -0
+        object_property_add_bool(obj, "pmu-partition",
+                                 arm_get_pmu_partition,
+                                 arm_set_pmu_partition);
+        object_property_add_uint32_ptr(obj, "pmu-num-counters",
+                                       &cpu->kvm_pmu_nr_counters,
+                                       OBJ_PROP_FLAG_READWRITE);
Quoted 2 lines

What should we do with these properties if we're not using KVM (i.e. for TCG, hvf, etc) ?

PMU partitioning is specific to KVM, so moving it into kvm_arm_add_vcpu_properties() (which is only called when kvm_enabled()) and naming it "kvm-pmu-partition" per the KVM VCPU Features convention in docs/system/arm/cpu-features.rst seems to make the most sense.

For the counter limit property, would you prefer that it also live in kvm_arm_add_vcpu_properties() as "kvm-pmu-num-counters" (KVM-only), or keep "pmu-num-counters" general so TCG can also use it to override PMCR_EL0.N in reset_pmcr_el0?

Thanks,
Colton

#52 lore raw

Re: [PATCH] target/arm: Enable KVM PMU partitioning and counter limit

Quoted 14 lines · Colton Lewis

Hi Peter,

Thanks for reviewing!

Quoted 2 lines · Peter Maydell

What should we do with these properties if we're not using KVM (i.e. for TCG, hvf, etc) ?

PMU partitioning is specific to KVM, so moving it into kvm_arm_add_vcpu_properties() (which is only called when kvm_enabled()) and naming it "kvm-pmu-partition" per the KVM VCPU Features convention in docs/system/arm/cpu-features.rst seems to make the most sense.

Quoted 4 lines

For the counter limit property, would you prefer that it also live in kvm_arm_add_vcpu_properties() as "kvm-pmu-num-counters" (KVM-only), or keep "pmu-num-counters" general so TCG can also use it to override PMCR_EL0.N in reset_pmcr_el0?

If it can reasonably easily be implemented (or potentially implemented) for TCG, then I guess the general name makes most sense.

-- PMM

#53 lore raw

Re: [PATCH] target/arm: Enable KVM PMU partitioning and counter limit

Hi Peter,

Quoted 8 lines · Peter Maydell
Quoted 4 lines · Colton Lewis

For the counter limit property, would you prefer that it also live in kvm_arm_add_vcpu_properties() as "kvm-pmu-num-counters" (KVM-only), or keep "pmu-num-counters" general so TCG can also use it to override PMCR_EL0.N in reset_pmcr_el0?

If it can reasonably easily be implemented (or potentially implemented) for TCG, then I guess the general name makes most sense.

Sounds good. I will keep "pmu-num-counters" general (and wire it up to override PMCR_EL0.N for TCG as well) and move the partitioning toggle into kvm_arm_add_vcpu_properties() as "kvm-pmu-partition" in RFC v2.

Thanks,
Colton

reviewer Tested-by on the v9 series
#54 lore raw
In reply to Colton Lewis
Quoted 170 lines · Colton Lewis

This series creates a new PMU scheme on ARM, a partitioned PMU that allows reserving a subset of counters for more direct guest access, significantly reducing overhead. More details, including performance benchmarks, can be read in the v1 cover letter linked below.

There is no longer a kernel command line parameter (arm_pmuv3.reserved_host_counters); PMU partitioning is now completely controlled via the KVM API using KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION and KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS vCPU device attributes. When partitioning is enabled for a VM, userspace must explicitly configure a guest event counter count strictly less than the maximum general-purpose counters implemented by the PMU (leaving at least one general-purpose counter for the host) prior to calling KVM_ARM_VCPU_PMU_V3_INIT. A QEMU patch demonstrating how to use the uAPI is sent separately.

An overview of what this series accomplishes was presented at KVM Forum 2025. Slides [1] and video [2] are linked below.

v9:

* Rebase on top of v7.3-rc4.

* Drop the arm_pmuv3.reserved_host_counters module parameter so partitioning is completely controlled via the KVM vCPU device attribute uAPI (KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION and KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS), and document the new attribute and counter allocation rules in Documentation/virt/kvm/devices/vcpu.rst (James Clark).

* Move the dynamic counter allocation mask (cntr_mask) from global struct arm_pmu to per-CPU struct pmu_hw_events (cpuc->cntr_mask) in a dedicated drivers/perf patch, fixing multi-pCPU counter reservation clobbering on vCPU migration and eliminating the cpu_pm_pmu_setup() cpuidle WARN_ON_ONCE (Zide Chen, James Clark).

* Synchronously propagate trapped guest writes to PMEVTYPER<n>_EL0 and PMCCFILTR_EL0 to hardware via kvm_pmu_apply_single_event_filter() when guest-owned, fixing in-guest perf stat event count skew across runs (James Clark, Sashiko AI Review).

* Refactor armv8pmu_can_use_pmccntr() and armv8pmu_get_event_idx() to check cntr_mask inside armv8pmu_can_use_pmccntr() and un-nest the 64-bit user-access check so cycle events fall back cleanly to general-purpose counters when PMCCNTR_EL0 is reserved by a guest (Robin Murphy).

* Restrict the lazy transition to VCPU_PMU_ACCESS_GUEST_OWNED to when the guest actively enables counting (PMCR_EL0.E = 1 or setting guest counter bits in PMCNTENSET_EL0 / PMINTENSET_EL1), preventing guest boot-time PMU probing from prematurely claiming hardware counters and triggering spurious host counter preemption warnings (James Clark).

* Fix patch dependency ordering and series bisectability across all commits, removing intermediate max_guest_counters / hw_cntr_impl churn and squashing the selftest exception relaxation into the Partitioned PMU selftest patch (James Clark).

* Fix compiler warning for struct arm_pmu declaration in include/kvm/arm_pmu.h (kernel test robot) and guard kvm_pmu_host_counter_mask() when KVM is compiled in but not active (wuyifan).

* Track physical CPU PMU residency in vcpu->arch.pmu.loaded_on_cpu separately from VCPU_PMU_ACCESS_GUEST_OWNED, and toggle MDCR_EL2.HPME via kvm_pmu_host_start() / kvm_pmu_host_stop() instead of PMCR_EL0.E when starting/stopping host perf events while a partitioned guest is loaded.

* Allow kvm_vcpu_pmu_resync_el0() to resynchronize VHE EL0 event filters (PMEVTYPER<n>_EL0.U) in process context and order kvm_pmu_put() before kvm_vcpu_pmu_restore_host() in kvm_arch_vcpu_put().

* Address additional Sashiko AI Review findings:
- Check idx - 1 against cpuc->cntr_mask in armv8pmu_get_chain_idx()

     to prevent 64-bit chained host events from crossing an odd `HPMN`
     partition boundary, and use `cpuc->cntr_mask` in
     `armv8pmu_enable_user_access()`.
   - Latch live hardware `PMOVSSET_EL0` overflow bits for guest counters
     with IRQs disabled (`local_irq_save()`) in `kvm_pmu_part_overflow_status()`
     and during trapped guest accesses to `PMOVS{SET,CLR}_EL0`.
   - Add mandatory `isb()` barriers after control-plane system register
     writes (`mdcr_el2`, `pmcntenclr_el0`, `pmintenclr_el1`), preserve
     guest `PMSELR_EL0` / `PMUSERENR_EL0` when `MDCR_EL2.TPM == 0`, and
     restore host `PMCR_EL0` control flags on `kvm_pmu_put()`.

v8:
https://lore.kernel.org/kvmarm/[email protected]/

v7:
https://lore.kernel.org/kvmarm/[email protected]/

v6:
https://lore.kernel.org/kvmarm/[email protected]/

v5:
https://lore.kernel.org/kvmarm/[email protected]/

v4:
https://lore.kernel.org/kvmarm/[email protected]/

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

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

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

[1] https://gitlab.com/qemu-project/kvm-forum/-/raw/main/_attachments/2025/Optimizing__itvHkhc.pdf
[2] https://www.youtube.com/watch?v=YRzZ8jMIA6M&list=PLW3ep1uCIRfxwmllXTOA2txfDWN6vUOHp&index=9

Colton Lewis (21):
arm64: cpufeature: Add cpucap for HPMN0
KVM: arm64: Reorganize PMU functions
perf: arm_pmuv3: Generalize counter bitmasks
perf: arm_pmuv3: Move counter allocation mask to per-CPU struct
pmu_hw_events
perf: arm_pmuv3: Check cntr_mask before using pmccntr
perf: arm_pmuv3: Allocate counter indices from high to low
KVM: arm64: Add initial scaffolding for Partitioned PMU
KVM: arm64: Set up FGT for Partitioned PMU
KVM: arm64: Add Partitioned PMU register trap handlers
KVM: arm64: Set up MDCR_EL2 to handle a Partitioned PMU
KVM: arm64: Context swap Partitioned PMU guest registers
KVM: arm64: Enforce PMU event filter at vcpu_load()
perf: Add perf_pmu_resched_update()
KVM: arm64: Allow kvm_vcpu_pmu_resync_el0() to resync filters in
process context
KVM: arm64: Apply dynamic guest counter reservations
KVM: arm64: Implement lazy PMU context swaps
perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
KVM: arm64: Detect overflows for the Partitioned PMU
KVM: arm64: Add vCPU device attr to partition the PMU
KVM: selftests: Add find_bit to KVM library
KVM: arm64: selftests: Add test case for Partitioned PMU

Marc Zyngier (1):
KVM: arm64: Reorganize PMU includes

  Documentation/virt/kvm/devices/vcpu.rst       |  42 +-
  arch/arm/include/asm/arm_pmuv3.h              |  16 +
  arch/arm64/include/asm/arm_pmuv3.h            |   7 +-
  arch/arm64/include/asm/kvm_host.h             |  18 +-
  arch/arm64/include/asm/kvm_types.h            |   6 +-
  arch/arm64/include/uapi/asm/kvm.h             |   2 +
  arch/arm64/kernel/cpufeature.c                |  10 +-
  arch/arm64/kvm/Makefile                       |   2 +-
  arch/arm64/kvm/arm.c                          |   4 +-
  arch/arm64/kvm/config.c                       |  49 +-
  arch/arm64/kvm/debug.c                        |  41 +-
  arch/arm64/kvm/pmu-direct.c                   | 636 ++++++++++++++
  arch/arm64/kvm/pmu-emul.c                     | 718 +---------------
  arch/arm64/kvm/pmu.c                          | 787 +++++++++++++++++-
  arch/arm64/kvm/sys_regs.c                     | 334 ++++++--
  arch/arm64/tools/cpucaps                      |   1 +
  arch/arm64/tools/sysreg                       |   6 +-
  drivers/perf/arm_pmu.c                        |   7 +-
  drivers/perf/arm_pmuv3.c                      |  97 ++-
  include/kvm/arm_pmu.h                         |  93 ++-
  include/linux/perf/arm_pmu.h                  |   2 +
  include/linux/perf/arm_pmuv3.h                |  14 +-
  include/linux/perf_event.h                    |   3 +
  kernel/events/core.c                          |  31 +-
  tools/include/perf/arm_pmuv3.h                |  12 +-
  tools/testing/selftests/kvm/Makefile.kvm      |   1 +
  .../selftests/kvm/arm64/vpmu_counter_access.c | 129 ++-
  tools/testing/selftests/kvm/lib/find_bit.c    |   2 +
  28 files changed, 2200 insertions(+), 870 deletions(-)
Quoted 5 lines

create mode 100644 arch/arm64/kvm/pmu-direct.c create mode 100644 tools/testing/selftests/kvm/lib/find_bit.c

base-commit: 93f51579e7df248780214094418f205253383cc5

Hi Colton,

Looks good to me, everything seems to be working now:

There are still a few Sashiko comments though, and one critical one about racing with pseudo-NMI PMU interrupts that looked reasonable. I tried to test it and reproduce an actual issue but couldn't, so maybe it's bogus.

#55 lore raw

Hi James,

Quoted 3 lines · James Clark

Looks good to me, everything seems to be working now:

Thank you for testing and for all the reviews!

Quoted 3 lines

There are still a few Sashiko comments though, and one critical one about racing with pseudo-NMI PMU interrupts that looked reasonable. I tried to test it and reproduce an actual issue but couldn't, so maybe it's bogus.

The "Critical" Sashiko comment on Patch 19/22 (claiming that clearing hardware PMOVSSET_EL0 loses overflow bits because the guest has direct untrapped access to PMOVSSET_EL0) is a false positive: Patch 09/22 sets HDFGRTR_EL2_PMOVS and HDFGWTR_EL2_PMOVS (and without FGT, MDCR_EL2.TPM is set), so guest accesses to PMOVSSET_EL0 and PMOVSCLR_EL0 always trap to pmu_reg_read() / pmu_reg_write().

The pseudo-NMI race comments on Patch 11/22 and Patch 19/22 are theoretically possible in a narrow 2-instruction RMW window because local_irq_save() does not mask GICv3 pseudo-NMIs, which explains why it wasn't reproducible in practice. In v10 I will make the PMOVSSET_EL0 shadow updates and vcpu->arch.mdcr_el2 assignment atomic, and address the other valid Sashiko findings.

Thanks,
Colton

reviewer
#56 lore raw
In reply to Colton Lewis
Quoted 9 lines · Colton Lewis

This series creates a new PMU scheme on ARM, a partitioned PMU that allows reserving a subset of counters for more direct guest access, significantly reducing overhead. More details, including performance benchmarks, can be read in the v1 cover letter linked below.

There is no longer a kernel command line parameter (arm_pmuv3.reserved_host_counters); PMU partitioning is now completely controlled via the KVM API using KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION and KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS vCPU device attributes. When

As far as I know there isn't a straightforward way to get the number of hw counters from userspace. You have to either parse dmesg or attempt to open incrementally more counters in a group until it fails.

This makes doing things like "use all counters for guest" or "reserve two for the host" difficult. Maybe it's time to add a file to the PMU that userspace can use query this.

#57 lore raw

Hi James,

Quoted 11 lines · James Clark
Quoted 9 lines · Colton Lewis

This series creates a new PMU scheme on ARM, a partitioned PMU that allows reserving a subset of counters for more direct guest access, significantly reducing overhead. More details, including performance benchmarks, can be read in the v1 cover letter linked below.

There is no longer a kernel command line parameter (arm_pmuv3.reserved_host_counters); PMU partitioning is now completely controlled via the KVM API using KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION and KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS vCPU device attributes. When

Quoted 7 lines

As far as I know there isn't a straightforward way to get the number of hw counters from userspace. You have to either parse dmesg or attempt to open incrementally more counters in a group until it fails.

This makes doing things like "use all counters for guest" or "reserve two for the host" difficult. Maybe it's time to add a file to the PMU that userspace can use query this.

Inside a VMM, userspace can query the default PMCR_EL0.N via KVM_GET_ONE_REG(SYS_PMCR_EL0) after KVM_ARM_VCPU_INIT (or KVM_ARM_VCPU_PMU_V3_SET_PMU) and before calling KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS, since KVM initializes nr_pmu_counters to the PMU's max_counters (which is how vpmu_counter_access discovers the limit).

For users and management tools outside the VMM (e.g. choosing N to pass on the QEMU command line), I agree it's awkward that /sys/bus/event_source/devices/<pmu>/caps/ exposes slots and bus_width but not the number of counters. I will add a patch to expose a caps attribute for the number of counters in drivers/perf/arm_pmuv3.c.

Thanks,
Colton