Skip to contentSkip to conversation

ARM64 PMU Partitioning

PATCH Superseded Colton Lewis 5 participants 48 messages Jun 12 – Aug 17

Subsystems (8)

v8 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

v8 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.

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

The kernel command line parameter for the driver still exists, but now only defines an upper limit of counters the guest might use rather than taking those counters from the host permanently.

I would appreciate any discussion on whether that parameter should still exist as it's an inconvenient enabling gate on the feature that is no longer required. The question comes down to what, if any, guards we want against a guest monopolizing all counters on a system.

v8:

* Rebase on top of v7.1-rc7.

* Implement Oliver Upton's accessor proposal to centralize PMU register access and simplify trap handlers. Instead of one singular accessor, implement as two because the read and write paths are always different anyway.

* Introduce the partitioning flag along with the
kvm_pmu_is_partitioned predicate

* Don't use ifdef for partitioning predicates as that can be handled by has_vhe

* Clean up MDCR_EL2 handling by open-coding use_fgt and hpmn and unconditionally setting RES0 bits.

* Use {read,write}_pmcrcntrn in context swaps

* Put operators on preceeding lines

* Rename hw_cntr_mask to hw_cntr_impl to clarify it tracks the number of counters implemented by hardware

* Use GENMASK_ULL in mask functions returning u64

* warn_once when host events are squeezed out by guest counter allocations.

* Address Sashiko AI Review findings:

- Critical fixes for lazy PMU context swaps (ensuring guest state is loaded on transition to GUEST_OWNED), PMSELR_EL0 trapping to prevent stale selector index, and masking guest PMCR_EL0 writes to prevent host reset.

- High priority fixes for lock safety (disabling IRQs when acquiring perf context lock), disabling guest counters on vCPU put, preserving VHE host profiling in MDCR_EL2, waking halted vCPUs on guest PMU interrupts, masking host configuration leaks, preemption safety in per-CPU accesses, emulating PMCR.N reads, and preventing data races in PMOVSSET_EL0 accesses.

- Medium/Low fixes for user-access fallback safety, VM-wide state modification restrictions, selftests type safety, and cleanup of unused fields and typos.

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 (20):
arm64: cpufeature: Add cpucap for HPMN0
KVM: arm64: Reorganize PMU functions
perf: arm_pmuv3: Generalize counter bitmasks
perf: arm_pmuv3: Check cntr_mask before using pmccntr
perf: arm_pmuv3: Allocate counter indices from high to low
perf: arm_pmuv3: Add method to partition the 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: 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
KVM: arm64: selftests: Relax testing for exceptions when partitioned

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

 arch/arm/include/asm/arm_pmuv3.h              |  18 +
 arch/arm64/include/asm/arm_pmuv3.h            |  12 +-
 arch/arm64/include/asm/kvm_host.h             |  17 +-
 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                          |   2 +
 arch/arm64/kvm/config.c                       |  41 +-
 arch/arm64/kvm/debug.c                        |  30 +-
 arch/arm64/kvm/pmu-direct.c                   | 507 ++++++++++++
 arch/arm64/kvm/pmu-emul.c                     | 684 +----------------
 arch/arm64/kvm/pmu.c                          | 720 ++++++++++++++++++
 arch/arm64/kvm/sys_regs.c                     | 271 +++++--
 arch/arm64/tools/cpucaps                      |   1 +
 arch/arm64/tools/sysreg                       |   6 +-
 drivers/perf/arm_pmuv3.c                      | 136 +++-
 include/kvm/arm_pmu.h                         |  93 ++-
 include/linux/perf/arm_pmu.h                  |   8 +
 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 | 112 ++-
 tools/testing/selftests/kvm/lib/find_bit.c    |   2 +
 26 files changed, 1918 insertions(+), 823 deletions(-)
 create mode 100644 arch/arm64/kvm/pmu-direct.c
 create mode 100644 tools/testing/selftests/kvm/lib/find_bit.c

base-commit: 4549871118cf616eecdd2d939f78e3b9e1dddc48

Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#1 lore raw

PATCH v8 1/21 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      |  3 ++-
 arch/arm64/tools/cpucaps       |  1 +
 arch/arm64/tools/sysreg        |  6 +++---
 4 files changed, 15 insertions(+), 5 deletions(-)
arch/arm64/kernel/cpufeature.c +9 -1
index 6d53bb15cf7bb..096545a6e4043 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>
@@ -560,6 +560,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),
@@ -2965,6 +2966,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 +2 -1
index fa5c93c7a1352..c52873a6f91ed 100644
@@ -3326,7 +3326,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 811c2479e82d6..f8fb4a6395428 100644
@@ -42,6 +42,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 6c3ff14e561e6..2d5cbc8ced114 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.54.0.1136.gdb2ca164c4-goog
#2 lore raw

PATCH v8 2/21 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 a49042bfa801f..0d7a620c69ee2 100644
@@ -1480,25 +1480,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 8014ff766cff5..8d3b832cd633a 100644
@@ -9,6 +9,11 @@
  */
 
 #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 0a36a3d5c8944..ec74a58905712 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 */
@@ -69,6 +79,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);
@@ -162,6 +175,12 @@ static inline u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
 
 #define kvm_vcpu_has_pmu(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.54.0.1136.gdb2ca164c4-goog
#3 lore raw

PATCH v8 3/21 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 | 682 +------------------------------------
 arch/arm64/kvm/pmu.c      | 686 ++++++++++++++++++++++++++++++++++++++
 include/kvm/arm_pmu.h     |   7 +
 3 files changed, 694 insertions(+), 681 deletions(-)
arch/arm64/kvm/pmu-emul.c +1 -681
index c816db5d67611..d1110febe7436 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,24 +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;
-	bool overflow;
-
-	overflow = kvm_pmu_overflow_status(vcpu);
-	if (pmu->irq_level == overflow)
-		return;
-
-	pmu->irq_level = overflow;
-
-	if (likely(irqchip_in_kernel(vcpu->kvm))) {
-		int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu,
-					      pmu->irq_num, overflow, pmu);
-		WARN_ON(ret);
-	}
-}
-
 bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
 {
 	struct kvm_pmu *pmu = &vcpu->arch.pmu;
@@ -436,43 +316,6 @@ void kvm_pmu_update_run(struct kvm_vcpu *vcpu)
 		regs->device_irq_level |= KVM_ARM_DEV_PMU;
 }
 
-/**
- * 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
@@ -784,132 +627,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 arm_pmu *pmu)
-{
-	u64 val = __compute_pmceid(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 arm_pmu *pmu)
-{
-	u64 val = __compute_pmceid(pmu, 1);
-
-	/*
-	 * Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled
-	 * as RAZ
-	 */
-	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)
-{
-	struct arm_pmu *cpu_pmu = vcpu->kvm->arch.arm_pmu;
-	unsigned long *bmap = vcpu->kvm->arch.pmu_filter;
-	u64 val, mask = 0;
-	int base, i, nr_events;
-
-	if (!pmceid1) {
-		val = compute_pmceid0(cpu_pmu);
-		base = 0;
-	} else {
-		val = compute_pmceid1(cpu_pmu);
-		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);
@@ -921,403 +638,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)
-{
-	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;
-
-	/*
-	 * 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, 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);
-			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.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 +686 -0

697 lines not on this page · raw message

include/kvm/arm_pmu.h +7 -0
index ec74a58905712..520f6d926ac8c 100644
@@ -56,13 +56,16 @@ 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);
 void kvm_pmu_update_run(struct kvm_vcpu *vcpu);
 void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val);
@@ -135,6 +138,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.54.0.1136.gdb2ca164c4-goog
#4 lore raw

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

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] The KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS ioctl modifies VM-wide state and VCPU system registers without checking if the VM has already run, allowing a data race with active VCPUs.
- [High] Host-userspace-reachable WARN_ONCE in __kvm_pmu_event_mask allows unprivileged userspace to trigger a Denial of Service.
--

commit 5436d8ff9f4b80e9bf5e0df08454fa9ddf4d9e29
Author: Colton Lewis <[email protected]>

KVM: arm64: Reorganize PMU functions

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.

Quoted 4 lines
arch/arm64/kvm/pmu.c +0 -0
index 6b48a3d16d0d5..9ad3520417413 100644

[ ... ]

Quoted 5 lines
arch/arm64/kvm/pmu.c +5 -0
+static u32 __kvm_pmu_event_mask(unsigned int pmuver)
+{
+	switch (pmuver) {
+	case ID_AA64DFR0_EL1_PMUVer_IMP:
+		return GENMASK(9, 0);

arch/arm64/kvm/pmu.c void kvm_vcpu_pmu_resync_el0(void) · line 381

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

[Severity: High]
This is a pre-existing issue, but can this warning be triggered by unprivileged host userspace?

The pmuver here is extracted from the guest's SYS_ID_AA64DFR0_EL1 register. KVM allows userspace to downgrade ID registers using safe semantics, meaning userspace might purposefully set an unhandled PMUVer (such as 2 or 3) via the KVM_SET_ONE_REG ioctl.

If this happens, wouldn't it hit this default case and trigger the warning, potentially causing a host denial of service if panic_on_warn is enabled?

[ ... ]

Quoted 18 lines
arch/arm64/kvm/pmu.c +18 -0
+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);
+		}
+	}
+}

[ ... ]

Quoted 2 lines
arch/arm64/kvm/pmu.c +2 -0
+int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
+{

[ ... ]

arch/arm64/kvm/pmu.c void kvm_vcpu_pmu_resync_el0(void) · line 760

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

[Severity: High]
This isn't a bug introduced by this patch, but does this ioctl bypass the kvm_vm_has_ran_once() check applied to other VM-wide attributes?

If userspace initializes VCPU 0, starts it via KVM_RUN, and then invokes this ioctl on an uninitialized VCPU 1, the check for vcpu->arch.pmu.created at the start of kvm_arm_pmu_v3_set_attr() would pass.

Since kvm_arm_set_nr_counters() loops over all VCPUs and performs a read-modify-write on MDCR_EL2 without taking vcpu->mutex or pausing VCPUs:

kvm_arm_set_nr_counters() {
    ...
    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);
    }
}

Could this cause a data race on hypervisor control registers if VCPU 0 is actively running, potentially allowing unprivileged userspace to corrupt execution state?

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

PATCH v8 4/21 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 8d3b832cd633a..1cceb1f614515 100644
@@ -534,7 +534,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)
@@ -770,7 +770,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.54.0.1136.gdb2ca164c4-goog
#6 lore raw

PATCH v8 5/21 perf: arm_pmuv3: Check cntr_mask before using pmccntr

In reply to Colton Lewis

Check cntr_mask before using pmccntr to ensure it's available. With a partitioned PMU, there may be instances where pmccntr is being used by the guest and will be absent from cntr_mask.

---
 drivers/perf/arm_pmuv3.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/perf/arm_pmuv3.c +2 -1
index 1cceb1f614515..17bb1cfdc271c 100644
@@ -1028,7 +1028,8 @@ 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))
+		if (test_bit(ARMV8_PMU_CYCLE_IDX, cpu_pmu->cntr_mask) &&
+		    !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) &&
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#7 lore raw

Re: [PATCH 05/21] perf: arm_pmuv3: Check cntr_mask before using pmccntr

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] A 64-bit user-access CPU_CYCLES event can bypass hardware width validation and be incorrectly mapped to a 32-bit general-purpose counter if armv8pmu_can_use_pmccntr() returns false. --

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

perf: arm_pmuv3: Check cntr_mask before using pmccntr

Check cntr_mask before using pmccntr to ensure it's available. With a partitioned PMU, there may be instances where pmccntr is being used by the guest and will be absent from cntr_mask.

Quoted 5 lines
drivers/perf/arm_pmuv3.c +0 -0
index 1cceb1f614515..17bb1cfdc271c 100644

drivers/perf/arm_pmuv3.c static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc, · line 1035

Quoted 9 lines
drivers/perf/arm_pmuv3.c +2 -1
@@ -1028,7 +1028,8 @@ 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))
+		if (test_bit(ARMV8_PMU_CYCLE_IDX, cpu_pmu->cntr_mask) &&
+		    !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) &&

[Severity: High]
This isn't a bug introduced by this patch, but is it possible for a 64-bit user-access CPU_CYCLES event to bypass hardware width validation here?

Because the -EAGAIN rejection logic is nested inside the armv8pmu_can_use_pmccntr() check, if that function returns false (for example, if threshold counting is enabled or the system has SMT), this validation is entirely skipped.

It looks like the event would then fall through to armv8pmu_get_single_idx() and potentially be allocated a 32-bit general-purpose counter. Userspace perf tools reading the userpage pmc_width as 64 would then treat a 32-bit counter as 64-bit, leading to unhandled wraparounds.

Looking at __armv8_pmuv3_map_event(), CPU_CYCLES events are explicitly exempted from the 64-bit unsupported check:

__armv8_pmuv3_map_event()

        if (armv8pmu_event_is_64bit(event) &&
            (hw_event_id != ARMV8_PMUV3_PERFCTR_CPU_CYCLES) &&
            !armv8pmu_has_long_event(armpmu))
                return -EOPNOTSUPP;

Does this code incorrectly map 64-bit CPU_CYCLES events to 32-bit counters when the dedicated 64-bit PMCCNTR is unavailable?

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

Re: [PATCH 05/21] perf: arm_pmuv3: Check cntr_mask before using pmccntr

In reply to Colton Lewis
Quoted 14 lines · Colton Lewis

Check cntr_mask before using pmccntr to ensure it's available. With a partitioned PMU, there may be instances where pmccntr is being used by the guest and will be absent from cntr_mask.

---
  drivers/perf/arm_pmuv3.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
drivers/perf/arm_pmuv3.c +0 -0
index 1cceb1f614515..17bb1cfdc271c 100644

drivers/perf/arm_pmuv3.c static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc, · line 1030

Quoted 3 lines
drivers/perf/arm_pmuv3.c +0 -0
@@ -1028,7 +1028,8 @@ 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)) {

Something smells wrong here - we can use PMCCNTR but we can't?

drivers/perf/arm_pmuv3.c static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc, · line 1031

Quoted 2 lines
drivers/perf/arm_pmuv3.c +1 -1
-		if (!test_and_set_bit(ARMV8_PMU_CYCLE_IDX, cpuc->used_mask))
+		if (test_bit(ARMV8_PMU_CYCLE_IDX, cpu_pmu->cntr_mask) &&

...i.e. this test would seem to belong in the helper function.

drivers/perf/arm_pmuv3.c static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc, · line 1035

Quoted 4 lines
drivers/perf/arm_pmuv3.c +1 -0
+		    !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) &&

Plus it also doesn't make much sense to fall through to the "we _could_ accommodate this event if only PMCCNTR was free" case here if we know PMCCNTR can never be free because it's not even available.

Thanks,
Robin.

#9 lore raw

Re: [PATCH 05/21] perf: arm_pmuv3: Check cntr_mask before using pmccntr

Hi Robin, thanks for the review

Quoted 26 lines · Robin Murphy
Quoted 14 lines · Colton Lewis

Check cntr_mask before using pmccntr to ensure it's available. With a partitioned PMU, there may be instances where pmccntr is being used by the guest and will be absent from cntr_mask.

---
   drivers/perf/arm_pmuv3.c | 3 ++-
   1 file changed, 2 insertions(+), 1 deletion(-)
drivers/perf/arm_pmuv3.c +0 -0
index 1cceb1f614515..17bb1cfdc271c 100644

drivers/perf/arm_pmuv3.c static int armv8pmu_get_event_idx(struct · line 1031

Quoted 4 lines
drivers/perf/arm_pmuv3.c +0 -0
@@ -1028,7 +1028,8 @@ 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)) {

Something smells wrong here - we can use PMCCNTR but we can't?

drivers/perf/arm_pmuv3.c static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc, · line 1031

Quoted 2 lines
drivers/perf/arm_pmuv3.c +1 -1
-		if (!test_and_set_bit(ARMV8_PMU_CYCLE_IDX, cpuc->used_mask))
+		if (test_bit(ARMV8_PMU_CYCLE_IDX, cpu_pmu->cntr_mask) &&

...i.e. this test would seem to belong in the helper function.

Quoted 8 lines
Quoted 4 lines
drivers/perf/arm_pmuv3.c +1 -0
+		    !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) &&

Plus it also doesn't make much sense to fall through to the "we _could_ accommodate this event if only PMCCNTR was free" case here if we know PMCCNTR can never be free because it's not even available.

I agree it would be a good idea to refactor these if statements.

Probably something like:

/* tests moved to armv8pmu_can_use_pmccntr */

if (armv8pmu_can_use_pmccntr()) {
	set_bit(ARMV8_PMU_CYCLE_IDX, cpu_pmu->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;
#10 lore raw

PATCH v8 6/21 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.

Doing this with index math instead of defining a for_each_set_bit_reverse macro is safe because cntr_mask is always a dense range while the host is running.

---
 drivers/perf/arm_pmuv3.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
drivers/perf/arm_pmuv3.c +16 -9
index 17bb1cfdc271c..d7a49dc0b0be6 100644
@@ -953,10 +953,12 @@ 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) {
-		if (!test_and_set_bit(idx, cpuc->used_mask))
+	for (idx = ARMV8_PMU_MAX_GENERAL_COUNTERS - 1; idx >= 0; idx--) {
+		if (test_bit(idx, cpu_pmu->cntr_mask) &&
+		    !test_and_set_bit(idx, cpuc->used_mask))
 			return idx;
 	}
+
 	return -EAGAIN;
 }
 
@@ -969,17 +971,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, cpu_pmu->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, cpu_pmu->cntr_mask) &&
+		    test_bit(idx - 1, cpu_pmu->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.54.0.1136.gdb2ca164c4-goog
#11 lore raw

PATCH v8 7/21 perf: arm_pmuv3: Add method to partition the PMU

In reply to Colton Lewis

For PMUv3, the register field MDCR_EL2.HPMN partitions the PMU counters into two ranges where counters 0..HPMN-1 are accessible by EL1 and, if allowed, EL0 while counters HPMN..N are only accessible by EL2.

Create a module parameter reserved_host_counters to reserve a number of counters for the host. Counters not reserved for the host may be used by a guest VM when the PMU is partitioned.

Add the function armv8pmu_partition() to check the validity of the reservation and record a partition has happened and the maximum allowable value for HPMN.

Due to the difficulty this feature would create for the driver running in nVHE mode, partitioning is only allowed in VHE mode. In order to support a partitioning on nVHE we'd need to explicitly disable guest counters on every exit and reset HPMN to place all counters in the first range.

---
 arch/arm/include/asm/arm_pmuv3.h   |  4 ++
 arch/arm64/include/asm/arm_pmuv3.h |  5 ++
 arch/arm64/kvm/Makefile            |  2 +-
 arch/arm64/kvm/pmu-direct.c        | 22 +++++++++
 drivers/perf/arm_pmuv3.c           | 77 ++++++++++++++++++++++++++++--
 include/kvm/arm_pmu.h              | 10 +++-
 include/linux/perf/arm_pmu.h       |  7 +++
 7 files changed, 121 insertions(+), 6 deletions(-)
 create mode 100644 arch/arm64/kvm/pmu-direct.c
arch/arm/include/asm/arm_pmuv3.h +4 -0
index ecfede0c03486..fddcd6e6f76b2 100644
@@ -221,6 +221,10 @@ 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;
arch/arm64/include/asm/arm_pmuv3.h +5 -0
index cf2b2212e00a2..27c4d6d47da31 100644
@@ -171,6 +171,11 @@ static inline bool pmuv3_implemented(int pmuver)
 		 pmuver == ID_AA64DFR0_EL1_PMUVer_NI);
 }
 
+static inline bool is_pmuv3p1(int pmuver)
+{
+	return pmuver >= ID_AA64DFR0_EL1_PMUVer_V3P1;
+}
+
 static inline bool is_pmuv3p4(int pmuver)
 {
 	return pmuver >= ID_AA64DFR0_EL1_PMUVer_V3P4;
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 +22 -0
new file mode 100644
index 0000000000000..74e40e4915416
@@ -0,0 +1,22 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2025 Google LLC
+ * Author: Colton Lewis <[email protected]>
+ */
+
+#include <linux/kvm_host.h>
+
+#include <asm/arm_pmuv3.h>
+
+/**
+ * has_host_pmu_partition_support() - Determine if partitioning is possible
+ *
+ * Partitioning is only supported in VHE mode with PMUv3
+ *
+ * Return: True if partitioning is possible, false otherwise
+ */
+bool has_host_pmu_partition_support(void)
+{
+	return has_vhe() &&
+		system_supports_pmuv3();
+}
drivers/perf/arm_pmuv3.c +73 -4

108 lines not on this page · raw message

include/kvm/arm_pmu.h +9 -1
index 520f6d926ac8c..f9a0823666949 100644
@@ -50,8 +50,11 @@ struct arm_pmu_entry {
 	struct arm_pmu *arm_pmu;
 };
 
+extern bool armv8pmu_is_partitioned;
+
 bool kvm_supports_guest_pmuv3(void);
-#define kvm_arm_pmu_irq_initialized(v)	((v)->arch.pmu.irq_num != 0)
+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);
 void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu, u64 select_idx, u64 val);
@@ -120,6 +123,11 @@ 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)
include/linux/perf/arm_pmu.h +7 -0
index 52b37f7bdbf9e..2e1e7a48e05ff 100644
@@ -109,6 +109,12 @@ struct arm_pmu {
 	 */
 	int		(*map_pmuv3_event)(unsigned int eventsel);
 	DECLARE_BITMAP(cntr_mask, ARMPMU_MAX_HWEVENTS);
+	/*
+	 * Keep a copy at initialization of which counters the
+	 * hardware implements to restore cntr_mask after
+	 * modifications.
+	 */
+	DECLARE_BITMAP(hw_cntr_impl, ARMPMU_MAX_HWEVENTS);
 	bool		secure_access; /* 32-bit ARM only */
 	struct platform_device	*plat_device;
 	struct pmu_hw_events	__percpu *hw_events;
@@ -129,6 +135,7 @@ struct arm_pmu {
 
 	/* Only to be used by ACPI probing code */
 	unsigned long acpi_cpuid;
+	int		max_guest_counters;
 };
 
 #define to_arm_pmu(p) (container_of(p, struct arm_pmu, pmu))
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#12 lore raw

PATCH v8 8/21 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 PMIFILTR 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 PMCIFILTR are special cases of the same.

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

---
 arch/arm/include/asm/arm_pmuv3.h  |  4 +++
 arch/arm64/include/asm/kvm_host.h |  2 ++
 arch/arm64/kvm/config.c           | 41 ++++++++++++++++++++++++++++---
 arch/arm64/kvm/pmu-direct.c       | 36 +++++++++++++++++++++++++++
 include/kvm/arm_pmu.h             | 12 +++++++++
 5 files changed, 92 insertions(+), 3 deletions(-)
arch/arm/include/asm/arm_pmuv3.h +4 -0
index fddcd6e6f76b2..eedf58ea01b10 100644
@@ -231,6 +231,10 @@ static inline bool kvm_set_pmuserenr(u64 val)
 }
 
 static inline void kvm_vcpu_pmu_resync_el0(void) {}
+static inline bool pmu_is_partitioned(struct arm_pmu *pmu)
+{
+	return false;
+}
 
 /* PMU Version in DFR Register */
 #define ARMV8_PMU_DFR_VER_NI        0
arch/arm64/include/asm/kvm_host.h +2 -0
index 0d7a620c69ee2..9c7e9b92dfbd3 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/config.c +38 -3

65 lines not on this page · raw message

arch/arm64/kvm/pmu-direct.c +36 -0
index 74e40e4915416..0135989667564 100644
@@ -5,6 +5,8 @@
  */
 
 #include <linux/kvm_host.h>
+#include <linux/perf/arm_pmu.h>
+#include <linux/perf/arm_pmuv3.h>
 
 #include <asm/arm_pmuv3.h>
 
@@ -20,3 +22,37 @@ bool has_host_pmu_partition_support(void)
 	return has_vhe() &&
 		system_supports_pmuv3();
 }
+
+/**
+ * pmu_is_partitioned() - Determine if given PMU is partitioned
+ * @pmu: Pointer to arm_pmu struct
+ *
+ * Determine if given PMU is partitioned by looking at hpmn field. The
+ * PMU is partitioned if this field is less than the number of
+ * counters in the system.
+ *
+ * Return: True if the PMU is partitioned, false otherwise
+ */
+bool pmu_is_partitioned(struct arm_pmu *pmu)
+{
+	if (!pmu)
+		return false;
+
+	return pmu->max_guest_counters >= 0 &&
+		pmu->max_guest_counters <= *host_data_ptr(nr_event_counters);
+}
+
+/**
+ * kvm_pmu_is_partitioned() - Determine if KVM has a partitioned PMU
+ * @kvm: Pointer to kvm struct
+ *
+ * Determine if KVM has a partitioned PMU by extracting that field and
+ * passing it to :c:func:`pmu_is_partitioned`
+ *
+ * Return: True if the KVM PMU is partitioned, false otherwise
+ */
+bool kvm_pmu_is_partitioned(struct kvm *kvm)
+{
+	return pmu_is_partitioned(kvm->arch.arm_pmu) &&
+		test_bit(KVM_ARCH_FLAG_PARTITION_PMU_ENABLED, &kvm->arch.flags);
+}
include/kvm/arm_pmu.h +12 -0
index f9a0823666949..36960b9e52da2 100644
@@ -95,6 +95,9 @@ void kvm_vcpu_pmu_resync_el0(void);
 #define kvm_vcpu_has_pmu(vcpu)					\
 	(vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3))
 
+bool pmu_is_partitioned(struct arm_pmu *pmu);
+bool kvm_pmu_is_partitioned(struct kvm *kvm);
+
 /*
  * Updates the vcpu's view of the pmu events for this cpu.
  * Must be called before every vcpu run after disabling interrupts, to ensure
@@ -134,6 +137,10 @@ static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu,
 {
 	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,
@@ -231,6 +238,11 @@ 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 bool pmu_is_partitioned(void *pmu)
+{
+	return false;
+}
+
 #endif
 
 #endif
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#13 lore raw

Re: [PATCH 08/21] KVM: arm64: Set up FGT for Partitioned PMU

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] `pmu_is_partitioned()` unsafely accesses a per-CPU variable via `host_data_ptr()` without…

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] pmu_is_partitioned() unsafely accesses a per-CPU variable via host_data_ptr() without preemption disabled, and evaluates incorrectly on heterogeneous systems.
- [High] has_host_pmu_partition_support() enables PMU partitioning without checking for FEAT_FGT, breaking vPMU interrupts on systems without it.
- [Low] Dummy inline implementation of pmu_is_partitioned() unnecessarily drops type safety by using void *.
--

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

KVM: arm64: Set up FGT for Partitioned PMU

This commit utilizes fine-grained traps (FEAT_FGT) to avoid trapping common PMU register accesses by the guest, which improves performance for partitioned PMUs. It selectively traps specific registers like PMOVS_EL0 to track overflow IRQs while leaving common counters untrapped.

arch/arm64/kvm/pmu-direct.c bool has_host_pmu_partition_support(void) · line 24

Quoted 8 lines
arch/arm64/kvm/pmu-direct.c +0 -0
index 74e40e4915416..0135989667564 100644
@@ -20,3 +22,37 @@ bool has_host_pmu_partition_support(void)
 	return has_vhe() &&
 		system_supports_pmuv3();
 }

[Severity: High]
Should this also check for cpus_have_final_cap(ARM64_HAS_FGT)?

Without FEAT_FGT, PMOVS_EL0 accesses won't be trapped. Since the host PMU IRQ handler clears the hardware overflow bit and records it in a software virtual overflow state, an untrapped guest read will incorrectly see 0, and guest writes will bypass KVM completely.

Could this lead to KVM's virtual overflow state never being cleared, resulting in a persistent vIRQ and a guest interrupt storm on pre-ARMv8.6 systems?

Quoted 9 lines
arch/arm64/kvm/pmu-direct.c +9 -0
+
+/**
+ * pmu_is_partitioned() - Determine if given PMU is partitioned
+ * @pmu: Pointer to arm_pmu struct
+ *
+ * Determine if given PMU is partitioned by looking at hpmn field. The
+ * PMU is partitioned if this field is less than the number of
+ * counters in the system.
+ *

arch/arm64/kvm/pmu-direct.c bool has_host_pmu_partition_support(void) · line 43

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+ * Return: True if the PMU is partitioned, false otherwise
+ */
+bool pmu_is_partitioned(struct arm_pmu *pmu)
+{
+	if (!pmu)
+		return false;
+
+	return pmu->max_guest_counters >= 0 &&
+		pmu->max_guest_counters <= *host_data_ptr(nr_event_counters);
+}

[Severity: High]
Does evaluating host_data_ptr() here require preemption to be disabled?

host_data_ptr() expands to this_cpu_ptr(&kvm_host_data), and this function is heavily used by KVM's PMU sysreg accessors which are reachable from guest exit handlers with preemption enabled. Accessing this_cpu_ptr() here can trigger a "BUG: using smp_processor_id() in preemptible" warning.

Additionally, on heterogeneous platforms (big.LITTLE) where different CPUs might have a different number of event counters, could the PMU partition status sporadically flip between true and false depending on which CPU the thread is scheduled on, silently corrupting the vPMU state?

[ ... ]

Quoted 4 lines
include/kvm/arm_pmu.h +0 -0
index f9a0823666949..36960b9e52da2 100644

[ ... ]

include/kvm/arm_pmu.h static inline bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int id · line 245

Quoted 9 lines
include/kvm/arm_pmu.h +5 -0
@@ -231,6 +238,11 @@ 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 bool pmu_is_partitioned(void *pmu)
+{
+	return false;
+}
+

[Severity: Low]
Is there a specific reason to use void *pmu for this fallback stub?

Since struct arm_pmu is forward declared and widely used in this header, using struct arm_pmu *pmu instead would preserve static type checking when the feature is disabled.

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

PATCH v8 9/21 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 registers that shouldn't be written to hardware because they require special handling (PMEVTYPER and PMOVS), write to the virtual register. A later patch will ensure these are handled correctly at vcpu_load time.

---
 arch/arm64/kvm/pmu-direct.c |  30 ++++
 arch/arm64/kvm/sys_regs.c   | 265 +++++++++++++++++++++++++++++-------
 include/kvm/arm_pmu.h       |   7 +
 3 files changed, 254 insertions(+), 48 deletions(-)
arch/arm64/kvm/pmu-direct.c +30 -0
index 0135989667564..43f04c2f33d44 100644
@@ -9,6 +9,7 @@
 #include <linux/perf/arm_pmuv3.h>
 
 #include <asm/arm_pmuv3.h>
+#include <asm/kvm_emulate.h>
 
 /**
  * has_host_pmu_partition_support() - Determine if partitioning is possible
@@ -56,3 +57,32 @@ bool kvm_pmu_is_partitioned(struct kvm *kvm)
 	return pmu_is_partitioned(kvm->arch.arm_pmu) &&
 		test_bit(KVM_ARCH_FLAG_PARTITION_PMU_ENABLED, &kvm->arch.flags);
 }
+
+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);
+	}
+}
+
+u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu)
+{
+	return u64_replace_bits(
+		read_sysreg(pmcr_el0),
+		vcpu->kvm->arch.nr_pmu_counters,
+		ARMV8_PMU_PMCR_N);
+}
arch/arm64/kvm/sys_regs.c +217 -48

372 lines not on this page · raw message

include/kvm/arm_pmu.h +7 -0
index 36960b9e52da2..700d5f275b557 100644
@@ -97,6 +97,8 @@ void kvm_vcpu_pmu_resync_el0(void);
 
 bool pmu_is_partitioned(struct arm_pmu *pmu);
 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);
 
 /*
  * Updates the vcpu's view of the pmu events for this cpu.
@@ -141,6 +143,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.54.0.1136.gdb2ca164c4-goog
#15 lore raw

Re: [PATCH 09/21] KVM: arm64: Add Partitioned PMU register trap handlers

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider: - [High] Writes to PMU control-plane system registers lack the architecturally mandated immediate `isb()`…

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] Writes to PMU control-plane system registers lack the architecturally mandated immediate isb() synchronization barrier.
- [High] Trapped accesses to PMEVTYPER, PMCCFILTR_EL0, and PMOVSSET_EL0 fail to synchronize with the active hardware PMU state while the vCPU is running.
- [High] kvm_pmu_direct_pmcr_read bypasses Nested Virtualization (NV) counter limits, exposing incorrect PMCR_EL0.N to L2 guests.
- [High] Guest PMSELR_EL0 and PMUSERENR_EL0 state is irrevocably lost across vCPU context switches in partitioned PMU mode.
--

commit 62a57b5fbe8c480374618085132cc6e2e40657b1
Author: Colton Lewis <[email protected]>

KVM: arm64: Add Partitioned PMU register trap handlers

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.

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +2 -0
index 0135989667564..43f04c2f33d44 100644
@@ -56,3 +57,32 @@ bool kvm_pmu_is_partitioned(struct kvm *kvm)
 	return pmu_is_partitioned(kvm->arch.arm_pmu) &&
 		test_bit(KVM_ARCH_FLAG_PARTITION_PMU_ENABLED, &kvm->arch.flags);
 }
+
+void kvm_pmu_direct_pmcr_write(struct kvm_vcpu *vcpu, u64 val)

arch/arm64/kvm/pmu-direct.c bool kvm_pmu_is_partitioned(struct kvm *kvm) · line 71

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+{
+	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) {

[Severity: High]
Does this write to pmcr_el0 need an isb() barrier before we evaluate reset_p?

The ARM64 architecture requires updates to control-plane system registers like pmcr_el0 to be followed immediately by an isb() instruction. Without it, the pipeline might execute the subsequent instructions under an undefined state.

Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6 -0
+		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)

arch/arm64/kvm/pmu-direct.c bool kvm_pmu_is_partitioned(struct kvm *kvm) · line 87

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+			write_pmevcntrn(i, 0);
+	}
+}
+
+u64 kvm_pmu_direct_pmcr_read(struct kvm_vcpu *vcpu)
+{
+	return u64_replace_bits(
+		read_sysreg(pmcr_el0),
+		vcpu->kvm->arch.nr_pmu_counters,
+		ARMV8_PMU_PMCR_N);

[Severity: High]
Can this bypass Nested Virtualization counter limits and expose an incorrect PMCR_EL0.N value to L2 guests?

The existing kvm_vcpu_read_pmcr() function properly accounts for nested virtualization by checking for nested guests and restricting PMCR_EL0.N based on the L1 hypervisor's MDCR_EL2.HPMN limit. This direct read inserts the full vcpu->kvm->arch.nr_pmu_counters, which means L2 guests under a partitioned PMU might escape the L1 limit and observe the full count provisioned to L1.

Quoted 5 lines
arch/arm64/kvm/pmu-direct.c +1 -0
+}
arch/arm64/kvm/sys_regs.c +0 -0
index c52873a6f91ed..94572bc52c32a 100644

[ ... ]

Quoted 7 lines
arch/arm64/kvm/sys_regs.c +7 -0
+static void pmu_reg_write(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg, u64 val, bool set)
+{
+	unsigned long flags;
+	u64 mask;
+	int idx;
+
+	switch (reg) {

[ ... ]

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

Quoted 3 lines
arch/arm64/kvm/sys_regs.c +3 -0
+	case PMSELR_EL0:
+		if (kvm_pmu_is_partitioned(vcpu->kvm))
+			write_sysreg(val, pmselr_el0);

[Severity: High]
Does this need an isb() after writing pmselr_el0? Like pmcr_el0, writes to control-plane registers require an immediate synchronization barrier before returning or executing further instructions.

Also, if we are untrapping or writing directly to the physical pmselr_el0 and pmuserenr_el0 registers, won't this state be irrevocably lost across vCPU context switches?

When entering a guest, __activate_traps_common() in arch/arm64/kvm/hyp/include/hyp/switch.h unconditionally clobbers the physical pmselr_el0 to 0 and pmuserenr_el0 to ARMV8_PMU_USERENR_MASK. Any state restored by kvm_pmu_load() or set here would be destroyed before the guest actually executes.

[ ... ]

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

Quoted 6 lines
arch/arm64/kvm/sys_regs.c +6 -0
+	case PMEVTYPER0_EL0 ... PMCCFILTR_EL0:
+		idx = reg - PMEVTYPER0_EL0;
+
+		if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+			mask = kvm_pmu_evtyper_mask(vcpu->kvm);
+			__vcpu_assign_sys_reg(vcpu, reg, val & mask);

[Severity: High]
Since traps are processed synchronously while the vCPU is loaded, doesn't updating only the virtual sysreg array leave the active hardware with stale configurations?

Writing to the virtual array here without requesting a PMU reload (KVM_REQ_RELOAD_PMU) or updating the hardware means that upon ERET back to the guest, the physical PMU will continue executing with the old state.

[ ... ]

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

Quoted 6 lines
arch/arm64/kvm/sys_regs.c +6 -0
+	case PMCNTENSET_EL0:
+		if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+			if (set)
+				write_sysreg(val, pmcntenset_el0);
+			else
+				write_sysreg(val, pmcntenclr_el0);

[Severity: High]
Does returning after this write without an isb() violate the architectural requirements?

Writing to pmcntenset_el0 or pmcntenclr_el0 needs an immediate isb() to ensure the state change is resolved before returning to the caller or executing subsequent pipeline instructions.

[ ... ]

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

Quoted 3 lines
arch/arm64/kvm/sys_regs.c +3 -0
+	case PMUSERENR_EL0:
+		if (kvm_pmu_is_partitioned(vcpu->kvm))
+			write_sysreg(val, pmuserenr_el0);

[Severity: High]
Is an isb() barrier required here after writing pmuserenr_el0, similar to the other control-plane registers?

[ ... ]

Quoted 6 lines
arch/arm64/kvm/sys_regs.c +6 -0
+static u64 pmu_reg_read(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg)
+{
+	u64 val = 0;
+	int idx;
+
+	switch (reg) {

[ ... ]

arch/arm64/kvm/sys_regs.c

Quoted 2 lines
arch/arm64/kvm/sys_regs.c +2 -0
+	case PMOVSSET_EL0:
+		val = __vcpu_sys_reg(vcpu, reg);

[Severity: High]
Will this return an accurate value if there are unhandled physical hardware overflows?

Because we only read the virtual array here, we could mask unhandled physical hardware overflows from polling guests if the host hasn't yet processed the IRQ and updated the virtual state.

Quoted 1 line
arch/arm64/kvm/sys_regs.c +1 -0
+		break;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
#16 lore raw

PATCH v8 10/21 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.

---
 arch/arm64/kvm/debug.c | 27 ++++++++++++++++++++++++---
 arch/arm64/kvm/pmu.c   |  7 +++++++
 2 files changed, 31 insertions(+), 3 deletions(-)
arch/arm64/kvm/debug.c +24 -3
index f4d7b12045e8f..c84321277d893 100644
@@ -43,14 +43,35 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
 	 * 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 |
 				MDCR_EL2_TPMCR |
 				MDCR_EL2_TDRA |
-				MDCR_EL2_TDOSA);
+				MDCR_EL2_TDOSA |
+				MDCR_EL2_HPME);
+
+	if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		u8 nr_guest_cntr = vcpu->kvm->arch.nr_pmu_counters;
+
+		vcpu->arch.mdcr_el2 |= (MDCR_EL2_HPMD | MDCR_EL2_HCCD);
+
+		/*
+		 * 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_FGT) &&
+		    (cpus_have_final_cap(ARM64_HAS_HPMN0) || nr_guest_cntr > 0)) {
+			vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_TPM | MDCR_EL2_TPMCR | MDCR_EL2_HPMN);
+			vcpu->arch.mdcr_el2 |= FIELD_PREP(MDCR_EL2_HPMN, nr_guest_cntr);
+		}
+
+
+	}
 
 	/* Is the VM being debugged by userspace? */
 	if (vcpu->guest_debug)
arch/arm64/kvm/pmu.c +7 -0
index 9ad3520417413..55cda8021400a 100644
@@ -552,6 +552,13 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)
 	if (cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS))
 		return 1;
 
+	/*
+	 * If partitioned then we are limited by the max counters in
+	 * the guest partition.
+	 */
+	if (pmu_is_partitioned(arm_pmu))
+		return arm_pmu->max_guest_counters;
+
 	/*
 	 * The arm_pmu->cntr_mask considers the fixed counter(s) as well.
 	 * Ignore those and return only the general-purpose counters.
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#17 lore raw

Re: [PATCH 10/21] KVM: arm64: Set up MDCR_EL2 to handle a Partitioned PMU

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [Critical] MDCR_EL2_HPMN update is incorrectly gated by FEAT_FGT, breaking hardware PMU isolation and allowing guest escapes on non-FGT CPUs.
- [High] Unconditional restriction of PMU counters for non-partitioned VMs based on host capabilities.
- [High] Evaluating a per-CPU variable via this_cpu_ptr() from a preemptible context in kvm_arm_pmu_get_max_counters().
- [Medium] MDCR_EL2_HPME is set unconditionally for all VMs.

Pre-existing issues:
- [High] Missing Context Synchronization Event (isb) after writing to the MDCR_EL2 control-plane system register. --

commit 414972a4abdb129c1ad96b121fefd7a789901b90
Author: Colton Lewis <[email protected]>

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

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.

Quoted 4 lines
arch/arm64/kvm/debug.c +0 -0
index f4d7b12045e8f..c84321277d893 100644

[ ... ]

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

Quoted 10 lines
arch/arm64/kvm/debug.c +2 -1
@@ -43,14 +43,35 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
 	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 |
 				MDCR_EL2_TPMCR |
 				MDCR_EL2_TDRA |
-				MDCR_EL2_TDOSA);
+				MDCR_EL2_TDOSA |
+				MDCR_EL2_HPME);

[Severity: Medium]
The commit message states "If partitioned, set the HPME, HPMD, and HCCD bits", but MDCR_EL2_HPME is added to the unconditional bitmask here. Does this leave an inactive state bit that could cause unpredictable interactions in nested virtualization environments for non-partitioned VMs?

Quoted 7 lines
arch/arm64/kvm/debug.c +7 -0
+
+	if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		u8 nr_guest_cntr = vcpu->kvm->arch.nr_pmu_counters;
+
+		vcpu->arch.mdcr_el2 |= (MDCR_EL2_HPMD | MDCR_EL2_HCCD);
+
+		/*

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

Quoted 10 lines
arch/arm64/kvm/debug.c +10 -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_FGT) &&
+		    (cpus_have_final_cap(ARM64_HAS_HPMN0) || nr_guest_cntr > 0)) {
+			vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_TPM | MDCR_EL2_TPMCR | MDCR_EL2_HPMN);
+			vcpu->arch.mdcr_el2 |= FIELD_PREP(MDCR_EL2_HPMN, nr_guest_cntr);
+		}

[Severity: Critical]
Is it safe to gate the MDCR_EL2_HPMN update behind the ARM64_HAS_FGT capability check?

If FEAT_FGT is absent, HPMN remains equal to the total number of event counters. While KVM falls back to trapping PMU accesses via TPM=1 on non-FGT CPUs, when the PMU is guest-owned, the trap handlers can write unmasked guest values directly to the hardware register:

arch/arm64/kvm/sys_regs.c:pmu_reg_write() {
    if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED) {
        if (set)
            write_sysreg(val, pmcntenset_el0);
    ...
}

Since KVM executes at EL2 and HPMN equals the total number of event counters, could this bypass hardware protections and allow a guest to overwrite host performance counters on non-FGT CPUs?

[Severity: High]
This is a pre-existing issue, but I noticed that after MDCR_EL2 is updated directly when has_vhe() is true in kvm_arm_setup_mdcr_el2():

arch/arm64/kvm/debug.c:kvm_arm_setup_mdcr_el2() {
    ...
    if (has_vhe())
        write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);

    preempt_enable();
}

There is no isb() immediately following the write to this control-plane register. Because KVM immediately accesses PMU registers that depend on MDCR_EL2 state (like writing to PMCNTENSET_EL0 after returning to pmu_reg_write), could it observe architecturally undefined pipeline state?

Quoted 6 lines
arch/arm64/kvm/debug.c +3 -0
+
+
+	}
 
 	/* Is the VM being debugged by userspace? */
 	if (vcpu->guest_debug)

[ ... ]

Quoted 4 lines
arch/arm64/kvm/pmu.c +0 -0
index 9ad3520417413..55cda8021400a 100644

[ ... ]

arch/arm64/kvm/pmu.c u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm) · line 560

Quoted 10 lines
arch/arm64/kvm/pmu.c +6 -0
@@ -552,6 +552,13 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)
 	if (cpus_have_final_cap(ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS))
 		return 1;
 
+	/*
+	 * If partitioned then we are limited by the max counters in
+	 * the guest partition.
+	 */
+	if (pmu_is_partitioned(arm_pmu))
+		return arm_pmu->max_guest_counters;

[Severity: High]
Does this incorrectly restrict the returned counters for all VMs on a partitioned host?

By using pmu_is_partitioned(arm_pmu) which checks the host capability, rather than kvm_pmu_is_partitioned(kvm) which checks if the specific VM has opted into the feature, this limits the counters to arm_pmu->max_guest_counters even for non-partitioned VMs that safely use perf_event multiplexing.

Could this degrade PMU capabilities for non-partitioned VMs and break live migration?

[Severity: High]
Can evaluating pmu_is_partitioned() here trigger a BUG splat?

kvm_arm_pmu_get_max_counters() is called from preemptible contexts during VM configuration, and pmu_is_partitioned() evaluates a per-CPU variable:

arch/arm64/kvm/pmu-direct.c:pmu_is_partitioned() {
    ...
    return pmu->max_guest_counters >= 0 &&
        pmu->max_guest_counters <= *host_data_ptr(nr_event_counters);
}

Since host_data_ptr() uses this_cpu_ptr(), could accessing this CPU-local variable with preemption enabled result in a BUG splat for using smp_processor_id() in preemptible contexts?

Quoted 4 lines
arch/arm64/kvm/pmu.c +1 -0
+
 	/*
 	 * The arm_pmu->cntr_mask considers the fixed counter(s) as well.
 	 * Ignore those and return only the general-purpose counters.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
#18 lore raw

PATCH v8 11/21 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/arm/include/asm/arm_pmuv3.h |   4 +
 arch/arm64/kvm/arm.c             |   2 +
 arch/arm64/kvm/pmu-direct.c      | 183 +++++++++++++++++++++++++++++++
 include/kvm/arm_pmu.h            |  16 +++
 4 files changed, 205 insertions(+)
arch/arm/include/asm/arm_pmuv3.h +4 -0
index eedf58ea01b10..f6031bd522718 100644
@@ -235,6 +235,10 @@ static inline bool pmu_is_partitioned(struct arm_pmu *pmu)
 {
 	return false;
 }
+static inline u64 kvm_pmu_host_counter_mask(struct arm_pmu *pmu)
+{
+	return ~0;
+}
 
 /* PMU Version in DFR Register */
 #define ARMV8_PMU_DFR_VER_NI        0
arch/arm64/kvm/arm.c +2 -0
index 9453321ef8c67..24f63edc8b384 100644
@@ -700,6 +700,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 		kvm_vcpu_load_vhe(vcpu);
 	kvm_arch_vcpu_load_fp(vcpu);
 	kvm_vcpu_pmu_restore_guest(vcpu);
+	kvm_pmu_load(vcpu);
 	if (kvm_arm_is_pvtime_enabled(&vcpu->arch))
 		kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu);
 
@@ -743,6 +744,7 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)
 	kvm_timer_vcpu_put(vcpu);
 	kvm_vgic_put(vcpu);
 	kvm_vcpu_pmu_restore_host(vcpu);
+	kvm_pmu_put(vcpu);
 	if (vcpu_has_nv(vcpu))
 		kvm_vcpu_put_hw_mmu(vcpu);
 	kvm_arm_vmid_clear_active();
arch/arm64/kvm/pmu-direct.c +183 -0

186 lines not on this page · raw message

include/kvm/arm_pmu.h +16 -0
index 700d5f275b557..61f8d4ed35e10 100644
@@ -99,6 +99,10 @@ bool pmu_is_partitioned(struct arm_pmu *pmu);
 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(struct arm_pmu *pmu);
+u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);
+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.
@@ -148,6 +152,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,
@@ -250,6 +256,16 @@ static inline bool pmu_is_partitioned(void *pmu)
 	return false;
 }
 
+static inline u64 kvm_pmu_host_counter_mask(void *kvm)
+{
+	return ~0;
+}
+
+static inline u64 kvm_pmu_guest_counter_mask(void *kvm)
+{
+	return 0;
+}
+
 #endif
 
 #endif
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#19 lore raw

Re: [PATCH 11/21] KVM: arm64: Context swap Partitioned PMU guest registers

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider: - [High] TOCTOU race in `kvm_pmu_put()` leads to lost hardware overflows and inconsistent counter values.…

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:
- [High] TOCTOU race in kvm_pmu_put() leads to lost hardware overflows and inconsistent counter values.
- [High] kvm_pmu_put() clobbers the guest's virtual PMCR_EL0 with host physical read-only fields.
- [High] Out-of-bounds bit shift and undefined behavior in GENMASK_ULL for kvm_pmu_host_counter_mask().
- [High] Missing mandatory isb() after writing control-plane PMU registers in kvm_pmu_put().
- [High] Guest accesses to the trapped PMOVSSET_EL0 register return stale data, causing the guest to permanently miss PMU overflows. --

commit 1e067bf0381df5bfe1db83a20f08aa650fd65999
Author: Colton Lewis <[email protected]>

KVM: arm64: Context swap Partitioned PMU guest registers

This commit saves and restores untrapped PMU registers when the PMU is partitioned, bypassing the context swap for emulated vPMUs. It also clears PMOVS guest hardware counters to avoid generating spurious interrupts.

Quoted 4 lines
arch/arm64/kvm/pmu-direct.c +0 -0
index 43f04c2f33d44..79022447cfb9a 100644

[ ... ]

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

Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6 -0
+u64 kvm_pmu_host_counter_mask(struct arm_pmu *pmu)
+{
+	u8 nr_counters = *host_data_ptr(nr_event_counters);
+
+	if (pmu_is_partitioned(pmu))
+		return GENMASK_ULL(nr_counters - 1, pmu->max_guest_counters);

[Severity: High]
Does this code trigger undefined behavior if the PMU implements zero event counters or if the guest is configured to use all available counters?

If nr_counters == 0, nr_counters - 1 evaluates to -1, resulting in GENMASK_ULL(-1, 0), which expands to a right shift by 64.

If max_guest_counters == nr_counters, the high bound is strictly less than the low bound, violating the macro's constraints. Both cases require explicit bounds checking.

Quoted 3 lines
arch/arm64/kvm/pmu-direct.c +3 -0
+
+	return ARMV8_PMU_CNT_MASK_ALL;
+}

[ ... ]

Quoted 2 lines
arch/arm64/kvm/pmu-direct.c +2 -0
+void kvm_pmu_put(struct kvm_vcpu *vcpu)
+{

[ ... ]

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

Quoted 5 lines
arch/arm64/kvm/pmu-direct.c +5 -0
+	val = read_sysreg(pmselr_el0);
+	__vcpu_assign_sys_reg(vcpu, PMSELR_EL0, val);
+
+	val = read_sysreg(pmcr_el0);
+	__vcpu_assign_sys_reg(vcpu, PMCR_EL0, val);

[Severity: High]
Can this clobber the guest's virtual PMCR_EL0 with host-specific read-only fields?

By reading the raw hardware pmcr_el0 and directly assigning it to the vCPU state, KVM overwrites the virtualized read-only fields (such as IMP, IDCODE, and N) with the host's physical values.

This might serialize the leaked host topology when userspace reads the register, causing migration issues if the VM is moved to a host with different PMU capabilities.

Quoted 3 lines
arch/arm64/kvm/pmu-direct.c +3 -0
+
+	/* Mask these to only save the guest relevant bits. */
+	mask = kvm_pmu_guest_counter_mask(pmu);

[ ... ]

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

Quoted 9 lines
arch/arm64/kvm/pmu-direct.c +9 -0
+	/* Save pending guest hardware overflows. */
+	local_irq_save(flags);
+	val = read_sysreg(pmovsset_el0);
+	__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, val & mask);
+	write_sysreg(val & mask, pmovsclr_el0);
+	local_irq_restore(flags);
+
+	/* Stop guest counters and disable interrupts in hardware. */
+	write_sysreg(mask, pmcntenclr_el0);

[Severity: High]
Does this create a race condition where hardware overflows can be permanently lost?

The hardware counters are still running while kvm_pmu_put() reads their state and the overflow register. A counter might overflow in the window between reading pmovsset_el0 and stopping the counters with pmcntenclr_el0.

If the guest had disabled physical PMU interrupts for that counter, no IRQ fires to rescue the overflow state. The overflow bit remains set in hardware but is missed by KVM, and when the vCPU is scheduled back in, kvm_pmu_load() will unconditionally clear pmovsclr_el0, destroying the unrecorded overflow.

Also, does this design lead to stale reads when the guest polls for overflows?

Since the counters run directly in hardware without syncing back to memory until vcpu_put, if a guest traps to pmu_reg_read() in arch/arm64/kvm/sys_regs.c to read PMOVSSET_EL0:

pmu_reg_read()

    case PMOVSSET_EL0:
        val = __vcpu_sys_reg(vcpu, reg);
        break;

It will receive the stale in-memory value instead of seeing the active hardware overflow accumulating in the physical register.

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

Quoted 5 lines
arch/arm64/kvm/pmu-direct.c +5 -0
+	write_sysreg(mask, pmintenclr_el1);
+
+	kvm_pmu_set_guest_counters(pmu, 0);
+	preempt_enable();
+}

[Severity: High]
Are we missing a mandatory Context Synchronization Event here?

After writing to control-plane registers like pmcntenclr_el0 and pmintenclr_el1, an isb() must follow to ensure the CPU pipeline resolves the new configuration.

Without the barrier, the CPU could continue executing the host kernel using a stale configuration, potentially allowing hardware counters to run and deliver spurious physical PMU interrupts during host execution.

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

Re: [PATCH 11/21] KVM: arm64: Context swap Partitioned PMU guest registers

In reply to Colton Lewis
Quoted 29 lines · 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/arm/include/asm/arm_pmuv3.h |   4 +
  arch/arm64/kvm/arm.c             |   2 +
  arch/arm64/kvm/pmu-direct.c      | 183 +++++++++++++++++++++++++++++++
  include/kvm/arm_pmu.h            |  16 +++
  4 files changed, 205 insertions(+)

[...]

Quoted 57 lines
arch/arm64/kvm/pmu-direct.c +57 -0
+
+/**
+ * kvm_pmu_put() - Put untrapped PMU registers
+ * @vcpu: Pointer to struct kvm_vcpu
+ *
+ * Put all untrapped PMU registers from the VCPU into the PCPU. Mask
+ * to only bits belonging to guest-reserved counters and leave
+ * host-reserved counters alone in bitmask registers.
+ */
+void kvm_pmu_put(struct kvm_vcpu *vcpu)
+{
+	struct arm_pmu *pmu;
+	unsigned long guest_counters;
+	unsigned long flags;
+	u64 mask;
+	u8 i;
+	u64 val;
+
+	/*
+	 * If we aren't guest-owned then we know the guest is not
+	 * accessing the PMU anyway, so no need to bother with the
+	 * swap.
+	 */
+	if (!kvm_pmu_is_partitioned(vcpu->kvm))
+		return;
+
+	preempt_disable();
+
+	pmu = vcpu->kvm->arch.arm_pmu;
+	guest_counters = kvm_pmu_guest_counter_mask(pmu);
+
+	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {
+		if (i == ARMV8_PMU_CYCLE_IDX)
+			val = read_pmccntr();
+		else
+			val = read_pmevcntrn(i);
+
+		__vcpu_assign_sys_reg(vcpu, PMEVCNTR0_EL0 + i, val);
+	}
+
+	val = read_sysreg(pmselr_el0);
+	__vcpu_assign_sys_reg(vcpu, PMSELR_EL0, val);
+
+	val = read_sysreg(pmcr_el0);
+	__vcpu_assign_sys_reg(vcpu, PMCR_EL0, val);
+
+	/* Mask these to only save the guest relevant bits. */
+	mask = kvm_pmu_guest_counter_mask(pmu);
+
+	val = read_sysreg(pmcntenset_el0);
+	__vcpu_assign_sys_reg(vcpu, PMCNTENSET_EL0, val & mask);
+
+	val = read_sysreg(pmintenset_el1);
+	__vcpu_assign_sys_reg(vcpu, PMINTENSET_EL1, val & mask);
+
+	/* Save pending guest hardware overflows. */
+	local_irq_save(flags);

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

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+	val = read_sysreg(pmovsset_el0);
+	__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, val & mask);
+	write_sysreg(val & mask, pmovsclr_el0);
+	local_irq_restore(flags);
+
+	/* Stop guest counters and disable interrupts in hardware. */
+	write_sysreg(mask, pmcntenclr_el0);
+	write_sysreg(mask, pmintenclr_el1);
+
+	kvm_pmu_set_guest_counters(pmu, 0);

Hi Colton,

This function doesn't get added until "KVM: arm64: Apply dynamic guest counter reservations" a few commits later.

kvm_pmu_guest_counter_mask() is also used in a commit before it's added.

#21 lore raw

Re: [PATCH 11/21] KVM: arm64: Context swap Partitioned PMU guest registers

Quoted 103 lines · James Clark
Quoted 28 lines · 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/arm/include/asm/arm_pmuv3.h |   4 +
   arch/arm64/kvm/arm.c             |   2 +
   arch/arm64/kvm/pmu-direct.c      | 183 +++++++++++++++++++++++++++++++
   include/kvm/arm_pmu.h            |  16 +++
   4 files changed, 205 insertions(+)

[...]

Quoted 57 lines
arch/arm64/kvm/pmu-direct.c +57 -0
+
+/**
+ * kvm_pmu_put() - Put untrapped PMU registers
+ * @vcpu: Pointer to struct kvm_vcpu
+ *
+ * Put all untrapped PMU registers from the VCPU into the PCPU. Mask
+ * to only bits belonging to guest-reserved counters and leave
+ * host-reserved counters alone in bitmask registers.
+ */
+void kvm_pmu_put(struct kvm_vcpu *vcpu)
+{
+	struct arm_pmu *pmu;
+	unsigned long guest_counters;
+	unsigned long flags;
+	u64 mask;
+	u8 i;
+	u64 val;
+
+	/*
+	 * If we aren't guest-owned then we know the guest is not
+	 * accessing the PMU anyway, so no need to bother with the
+	 * swap.
+	 */
+	if (!kvm_pmu_is_partitioned(vcpu->kvm))
+		return;
+
+	preempt_disable();
+
+	pmu = vcpu->kvm->arch.arm_pmu;
+	guest_counters = kvm_pmu_guest_counter_mask(pmu);
+
+	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {
+		if (i == ARMV8_PMU_CYCLE_IDX)
+			val = read_pmccntr();
+		else
+			val = read_pmevcntrn(i);
+
+		__vcpu_assign_sys_reg(vcpu, PMEVCNTR0_EL0 + i, val);
+	}
+
+	val = read_sysreg(pmselr_el0);
+	__vcpu_assign_sys_reg(vcpu, PMSELR_EL0, val);
+
+	val = read_sysreg(pmcr_el0);
+	__vcpu_assign_sys_reg(vcpu, PMCR_EL0, val);
+
+	/* Mask these to only save the guest relevant bits. */
+	mask = kvm_pmu_guest_counter_mask(pmu);
+
+	val = read_sysreg(pmcntenset_el0);
+	__vcpu_assign_sys_reg(vcpu, PMCNTENSET_EL0, val & mask);
+
+	val = read_sysreg(pmintenset_el1);
+	__vcpu_assign_sys_reg(vcpu, PMINTENSET_EL1, val & mask);
+
+	/* Save pending guest hardware overflows. */
+	local_irq_save(flags);

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

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+	val = read_sysreg(pmovsset_el0);
+	__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, val & mask);
+	write_sysreg(val & mask, pmovsclr_el0);
+	local_irq_restore(flags);
+
+	/* Stop guest counters and disable interrupts in hardware. */
+	write_sysreg(mask, pmcntenclr_el0);
+	write_sysreg(mask, pmintenclr_el1);
+
+	kvm_pmu_set_guest_counters(pmu, 0);

Hi Colton,

Quoted 4 lines

This function doesn't get added until "KVM: arm64: Apply dynamic guest counter reservations" a few commits later.

kvm_pmu_guest_counter_mask() is also used in a commit before it's added.

My bad. I'll correct that.

#22 lore raw

PATCH v8 12/21 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 | 52 +++++++++++++++++++++++++++++++++++++
 1 file changed, 52 insertions(+)
arch/arm64/kvm/pmu-direct.c +52 -0
index 79022447cfb9a..49f1feb5d280c 100644
@@ -131,6 +131,57 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu)
 	return 0;
 }
 
+/**
+ * kvm_pmu_apply_event_filter()
+ * @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;
+	u64 evtyper_set = ARMV8_PMU_EXCLUDE_EL0 |
+		ARMV8_PMU_EXCLUDE_EL1;
+	u64 evtyper_clr = ARMV8_PMU_INCLUDE_EL2;
+	bool guest_include_el2;
+	u8 i;
+	u64 val;
+	u64 evsel;
+
+	if (!pmu)
+		return;
+
+	guest_counters = kvm_pmu_guest_counter_mask(pmu);
+
+	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {
+		if (i == 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 + i);
+			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)) && guest_include_el2)
+			val &= ~ARMV8_PMU_EXCLUDE_EL1;
+
+		if (vcpu->kvm->arch.pmu_filter &&
+		    !test_bit(evsel, vcpu->kvm->arch.pmu_filter))
+			val |= evtyper_set;
+
+		if (i == ARMV8_PMU_CYCLE_IDX)
+			write_pmccfiltr(val);
+		else
+			write_pmevtypern(i, val);
+	}
+}
+
 /**
  * kvm_pmu_load() - Load untrapped PMU registers
  * @vcpu: Pointer to struct kvm_vcpu
@@ -158,6 +209,7 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
 
 	pmu = vcpu->kvm->arch.arm_pmu;
 	guest_counters = kvm_pmu_guest_counter_mask(pmu);
+	kvm_pmu_apply_event_filter(vcpu);
 
 	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {
 		val = __vcpu_sys_reg(vcpu, PMEVCNTR0_EL0 + i);
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#23 lore raw

Re: [PATCH 12/21] KVM: arm64: Enforce PMU event filter at vcpu_load()

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Missing Context Synchronization Event (isb) after writing PMU event configuration registers before enabling counters.
- [High] Guest PMU counters erroneously count guest EL2 execution when the guest hypervisor requested NOT to count at EL2.

Pre-existing issues:
- [High] Guest writes to PMEVTYPERn_EL0 and PMCCFILTR_EL0 under a partitioned PMU are trapped but not synchronously propagated to the hardware PMU, breaking dynamic PMU configuration. --

commit 0b5fddb99fdf908d09da2215064503d365b4feac
Author: Colton Lewis <[email protected]>

KVM: arm64: Enforce PMU event filter at vcpu_load()

This commit enforces the KVM PMU event filter by rechecking and applying the filter on every vcpu load. It excludes counting at all exception levels if the event is filtered before writing to the hardware.

arch/arm64/kvm/pmu-direct.c +0 -0
@@ -131,6 +131,57 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu)
 

[ ... ]

Quoted 4 lines
(quoted diff) +4 -0
+	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {
+		if (i == ARMV8_PMU_CYCLE_IDX) {
+			val = __vcpu_sys_reg(vcpu, PMCCFILTR_EL0);
+			evsel = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;

Review

Quoted 10 lines
(quoted diff) +10 -0
+		} else {
+			val = __vcpu_sys_reg(vcpu, PMEVTYPER0_EL0 + i);
+			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)) && guest_include_el2)
+			val &= ~ARMV8_PMU_EXCLUDE_EL1;

[Severity: High]
When the guest is executing at guest EL2 (which runs at host EL1 under nested virtualization), does this correctly handle the case where the guest requested not to count at guest EL2?

If guest_include_el2 is false, but the guest's configuration originally had ARMV8_PMU_EXCLUDE_EL1 cleared (for example, to profile guest EL1), this code leaves it cleared. Will this cause the hardware PMU to erroneously count guest EL2 execution at host EL1?

Review

Quoted 11 lines
(quoted diff) +11 -0
+
+		if (vcpu->kvm->arch.pmu_filter &&
+		    !test_bit(evsel, vcpu->kvm->arch.pmu_filter))
+			val |= evtyper_set;
+
+		if (i == ARMV8_PMU_CYCLE_IDX)
+			write_pmccfiltr(val);
+		else
+			write_pmevtypern(i, val);
+	}
+}

[Severity: High]
Does this sequence require a context synchronization event?

According to the ARM64 architecture rules, writes to control-plane system registers must be followed by an isb() as the very next instruction. This code loops over the counters and writes the registers without issuing an isb().

Later, kvm_pmu_load() writes to PMCNTENSET_EL0 to enable the counters:

kvm_pmu_load() {
        ...
        kvm_pmu_apply_event_filter(vcpu);
        ...
        val = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
        write_sysreg(val & mask, pmcntenset_el0);
}

Without an intervening isb(), could the PMU hardware start counting before the pipeline resolves the new event configurations, leading to spurious event counts or PMU filter bypass?

[Severity: High]
This is a pre-existing issue, but does applying the event filter only during vcpu_load() break dynamic PMU configuration when the PMU is partitioned?

When the partitioned PMU is enabled, KVM traps guest writes to PMEVTYPERn_EL0 in pmu_reg_write() in arch/arm64/kvm/sys_regs.c:

pmu_reg_write() {
        ...
        if (kvm_pmu_is_partitioned(vcpu->kvm)) {
                mask = kvm_pmu_evtyper_mask(vcpu->kvm);
                __vcpu_assign_sys_reg(vcpu, reg, val & mask);
        }
        ...
}

This updates the in-memory sys_regs array but fails to push the new value to the physical hardware registers or request a KVM_REQ_RELOAD_PMU.

If the guest subsequently enables the counter (which writes directly to the physical PMCNTENSET_EL0 without a trap delay), the hardware PMU will use the stale event type. The new configuration and the event filter applied here in kvm_pmu_apply_event_filter() are delayed until the next VCPU reschedule.

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

PATCH v8 13/21 perf: Add perf_pmu_resched_update()

In reply to Colton Lewis

To modify PMU guest counter reservations dynamically, we need to update the available counters safely.

Introduce perf_pmu_resched_update() to allow updating the PMU struct in between scheduling perf events out and scheduling them back in again. It takes a callback operation to call in between schedule out and schedule in. This accomplishes the goal with minimal perf API expansion.

Refactor ctx_resched call the callback in the right place.

---
 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 48d851fbd8ea5..a08db3ee38b10 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 7935d5663944e..ad2fc080bacac 100644
@@ -2983,9 +2983,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;
@@ -3021,6 +3022,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)
@@ -3032,6 +3036,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.54.0.1136.gdb2ca164c4-goog
#25 lore raw

PATCH v8 14/21 KVM: arm64: Apply dynamic guest counter reservations

In reply to Colton Lewis

Apply dynamic guest counter reservations by checking if the requested guest mask collides with any events the host has scheduled and calling pmu_perf_resched_update() with a hook that updates the mask of available counters in between schedule out and schedule in.

---
 arch/arm64/kvm/pmu-direct.c  | 69 +++++++++++++++++++++++++++++++++++-
 include/linux/perf/arm_pmu.h |  1 +
 2 files changed, 69 insertions(+), 1 deletion(-)
arch/arm64/kvm/pmu-direct.c +68 -1
index 49f1feb5d280c..044f011c9c84b 100644
@@ -87,6 +87,73 @@ 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);
+	unsigned long *new_mask = data;
+
+	bitmap_copy(arm_pmu->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);
+	bool need_resched = false;
+
+	bitmap_from_arr64(guest_bitmap, &guest_mask, ARMPMU_MAX_HWEVENTS);
+	bitmap_copy(new_mask, cpu_pmu->hw_cntr_impl, ARMPMU_MAX_HWEVENTS);
+
+	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 hw_cntr_impl.
+		 * Only resched if we previously squeezed an event.
+		 */
+		if (cpuc->host_squeezed) {
+			need_resched = true;
+			cpuc->host_squeezed = false;
+		}
+	}
+
+	if (need_resched) {
+		/* Collision: run full perf reschedule */
+		perf_pmu_resched_update(&cpu_pmu->pmu, kvm_pmu_update_mask, new_mask);
+	} else {
+		/* Host was never using guest counters anyway */
+		bitmap_copy(cpu_pmu->cntr_mask, new_mask, ARMPMU_MAX_HWEVENTS);
+	}
+}
+
 /**
  * kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved counters
  * @pmu: Pointer to arm_pmu struct
@@ -209,6 +276,7 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
 
 	pmu = vcpu->kvm->arch.arm_pmu;
 	guest_counters = kvm_pmu_guest_counter_mask(pmu);
+	kvm_pmu_set_guest_counters(pmu, guest_counters);
 	kvm_pmu_apply_event_filter(vcpu);
 
 	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {
@@ -317,7 +385,6 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)
 	/* Stop guest counters and disable interrupts in hardware. */
 	write_sysreg(mask, pmcntenclr_el0);
 	write_sysreg(mask, pmintenclr_el1);
-
 	kvm_pmu_set_guest_counters(pmu, 0);
 	preempt_enable();
 }
include/linux/perf/arm_pmu.h +1 -0
index 2e1e7a48e05ff..3139f80e877f7 100644
@@ -75,6 +75,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.54.0.1136.gdb2ca164c4-goog
#26 lore raw

Re: [PATCH 14/21] KVM: arm64: Apply dynamic guest counter reservations

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Data race and state corruption of the global `arm_pmu->cntr_mask` when dynamically reserving…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Data race and state corruption of the global arm_pmu->cntr_mask when dynamically reserving guest PMU counters.
- [High] Lockless access to cpuc->events[idx] in kvm_pmu_set_guest_counters() can cause a NULL pointer dereference (kernel oops) if interrupted by a concurrent perf IPI. --

commit 4f21c28ed6e1adfadba5b0251c4089bb44f7b7b3
Author: Colton Lewis <[email protected]>

KVM: arm64: Apply dynamic guest counter reservations

Apply dynamic guest counter reservations by checking if the requested guest mask collides with any events the host has scheduled and calling pmu_perf_resched_update() with a hook that updates the mask of available counters in between schedule out and schedule in.

Quoted 5 lines
arch/arm64/kvm/pmu-direct.c +0 -0
index 49f1feb5d280c..044f011c9c84b 100644

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

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +7 -0
@@ -87,6 +87,73 @@ 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);
+	unsigned long *new_mask = data;
+
+	bitmap_copy(arm_pmu->cntr_mask, new_mask, ARMPMU_MAX_HWEVENTS);

[Severity: Critical]
Does this locklessly overwrite the shared arm_pmu->cntr_mask?

Since struct arm_pmu and its cntr_mask are shared across all CPUs in a PMU cluster, altering it dynamically from a per-CPU context without cross-CPU locking can cause torn writes and clobber reservations for other CPUs sharing the PMU.

Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6 -0
+}
+
+/**
+ * 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

[ ... ]

Quoted 5 lines
arch/arm64/kvm/pmu-direct.c +5 -0
+	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? */

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

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+		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) {

[Severity: High]
Can this result in a NULL pointer dereference?

The function iterates over cpuc->events[idx] with preemption disabled but local IRQs enabled. If a concurrent perf IPI interrupts KVM between the two compiler-generated loads of cpuc->events[idx] and frees the event slot (setting it to NULL), the subsequent dereference of ->attr.pinned will cause a fatal kernel oops.

Using READ_ONCE() for the load would ensure the compiler reads it only once.

Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6 -0
+					pr_warn_once("perf: Pinned host event squeezed out by KVM guest PMU partition\n");
+					break;
+				}
+			}
+		}
+	} else {

[ ... ]

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

Quoted 6 lines
arch/arm64/kvm/pmu-direct.c +6 -0
+	if (need_resched) {
+		/* Collision: run full perf reschedule */
+		perf_pmu_resched_update(&cpu_pmu->pmu, kvm_pmu_update_mask, new_mask);
+	} else {
+		/* Host was never using guest counters anyway */
+		bitmap_copy(cpu_pmu->cntr_mask, new_mask, ARMPMU_MAX_HWEVENTS);

[Severity: Critical]
Does this restoration also silently destroy active reservations for other CPUs in the cluster?

When one CPU exits a guest (kvm_pmu_put()), this unconditionally restores cntr_mask to hw_cntr_impl, which would remove any active PMU counter reservations for guests concurrently running on other CPUs. This might allow the host perf subsystem to allocate those guest-reserved counters, leading to PMU register corruption and interrupt storms.

Quoted 2 lines
arch/arm64/kvm/pmu-direct.c +2 -0
+	}
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
#27 lore raw

Re: [PATCH 14/21] KVM: arm64: Apply dynamic guest counter reservations

In reply to Colton Lewis
Quoted 98 lines · Colton Lewis

Apply dynamic guest counter reservations by checking if the requested guest mask collides with any events the host has scheduled and calling pmu_perf_resched_update() with a hook that updates the mask of available counters in between schedule out and schedule in.

---
 arch/arm64/kvm/pmu-direct.c  | 69 +++++++++++++++++++++++++++++++++++-
 include/linux/perf/arm_pmu.h |  1 +
 2 files changed, 69 insertions(+), 1 deletion(-)
arch/arm64/kvm/pmu-direct.c +68 -0
index 49f1feb5d280c..044f011c9c84b 100644
@@ -87,6 +87,73 @@ 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);
+	unsigned long *new_mask = data;
+
+	bitmap_copy(arm_pmu->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);
+	bool need_resched = false;
+
+	bitmap_from_arr64(guest_bitmap, &guest_mask, ARMPMU_MAX_HWEVENTS);
+	bitmap_copy(new_mask, cpu_pmu->hw_cntr_impl, ARMPMU_MAX_HWEVENTS);
+
+	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 hw_cntr_impl.
+		 * Only resched if we previously squeezed an event.
+		 */
+		if (cpuc->host_squeezed) {
+			need_resched = true;
+			cpuc->host_squeezed = false;
+		}
+	}
+
+	if (need_resched) {
+		/* Collision: run full perf reschedule */
+		perf_pmu_resched_update(&cpu_pmu->pmu, kvm_pmu_update_mask, new_mask);
+	} else {
+		/* Host was never using guest counters anyway */
+		bitmap_copy(cpu_pmu->cntr_mask, new_mask, ARMPMU_MAX_HWEVENTS);
+	}
+}
+
 /**
  * kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved counters
  * @pmu: Pointer to arm_pmu struct
@@ -209,6 +276,7 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
 
 	pmu = vcpu->kvm->arch.arm_pmu;
 	guest_counters = kvm_pmu_guest_counter_mask(pmu);
+	kvm_pmu_set_guest_counters(pmu, guest_counters);
 	kvm_pmu_apply_event_filter(vcpu);
 
 	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {

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

Quoted 5 lines
arch/arm64/kvm/pmu-direct.c +0 -1
@@ -317,7 +385,6 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)
 	/* Stop guest counters and disable interrupts in hardware. */
 	write_sysreg(mask, pmcntenclr_el0);
 	write_sysreg(mask, pmintenclr_el1);
-
 	kvm_pmu_set_guest_counters(pmu, 0);

I must have missed something.

Here, vcpu->kvm->arch.arm_pmu->cntr_mask is restored to cntr_mask_impl. Is this same struct shared with the perf driver (to_arm_pmu(event->pmu)), and shared across other vCPUs as well?

If so, if another pCPU still has a PMU-partition-enabled vcpu loaded with guest-owned counters (MDCR_EL2.HPMN is set), could a host event be scheduled onto one of those guest-owned counters, conflicting with the guest?

Quoted 2 lines
arch/arm64/kvm/pmu-direct.c +0 -0
 	preempt_enable();
 }
#28 lore raw

Re: [PATCH 14/21] KVM: arm64: Apply dynamic guest counter reservations

Hi Zide, thanks for the review.

Quoted 111 lines · Zide
Quoted 102 lines · Colton Lewis

Apply dynamic guest counter reservations by checking if the requested guest mask collides with any events the host has scheduled and calling pmu_perf_resched_update() with a hook that updates the mask of available counters in between schedule out and schedule in.

---
  arch/arm64/kvm/pmu-direct.c  | 69 +++++++++++++++++++++++++++++++++++-
  include/linux/perf/arm_pmu.h |  1 +
  2 files changed, 69 insertions(+), 1 deletion(-)
arch/arm64/kvm/pmu-direct.c +67 -0
index 49f1feb5d280c..044f011c9c84b 100644
@@ -87,6 +87,73 @@ 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);
+	unsigned long *new_mask = data;
+
+	bitmap_copy(arm_pmu->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);
+	bool need_resched = false;
+
+	bitmap_from_arr64(guest_bitmap, &guest_mask, ARMPMU_MAX_HWEVENTS);
+	bitmap_copy(new_mask, cpu_pmu->hw_cntr_impl, ARMPMU_MAX_HWEVENTS);
+
+	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 hw_cntr_impl.
+		 * Only resched if we previously squeezed an event.
+		 */
+		if (cpuc->host_squeezed) {
+			need_resched = true;
+			cpuc->host_squeezed = false;
+		}
+	}
+
+	if (need_resched) {
+		/* Collision: run full perf reschedule */
+		perf_pmu_resched_update(&cpu_pmu->pmu, kvm_pmu_update_mask, new_mask);
+	} else {
+		/* Host was never using guest counters anyway */
+		bitmap_copy(cpu_pmu->cntr_mask, new_mask, ARMPMU_MAX_HWEVENTS);
+	}
+}
+

/**
* kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved
counters
* @pmu: Pointer to arm_pmu struct

arch/arm64/kvm/pmu-direct.c +1 -0
@@ -209,6 +276,7 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu)
 
  	pmu = vcpu->kvm->arch.arm_pmu;
  	guest_counters = kvm_pmu_guest_counter_mask(pmu);
+	kvm_pmu_set_guest_counters(pmu, guest_counters);
  	kvm_pmu_apply_event_filter(vcpu);
 
  	for_each_set_bit(i, &guest_counters, ARMPMU_MAX_HWEVENTS) {

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

Quoted 5 lines
arch/arm64/kvm/pmu-direct.c +0 -1
@@ -317,7 +385,6 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)
  	/* Stop guest counters and disable interrupts in hardware. */
  	write_sysreg(mask, pmcntenclr_el0);
  	write_sysreg(mask, pmintenclr_el1);
-
  	kvm_pmu_set_guest_counters(pmu, 0);

I must have missed something.

Quoted 8 lines

Here, vcpu->kvm->arch.arm_pmu->cntr_mask is restored to cntr_mask_impl. Is this same struct shared with the perf driver (to_arm_pmu(event->pmu)), and shared across other vCPUs as well?

If so, if another pCPU still has a PMU-partition-enabled vcpu loaded with guest-owned counters (MDCR_EL2.HPMN is set), could a host event be scheduled onto one of those guest-owned counters, conflicting with the guest?

You're correct that is shared more widely than appropriate and could cause such a conflict.

cntr_mask should be moved to the per-CPU struct pmu_hw_events inside struct arm_pmu.

Quoted 2 lines
Quoted 2 lines
arch/arm64/kvm/pmu-direct.c +0 -0
  	preempt_enable();
  }
#29 lore raw

PATCH v8 15/21 KVM: arm64: Implement lazy PMU context swaps

In reply to Colton Lewis

Since many guests will never touch the PMU, they need not pay the cost of context swapping those registers.

Use an enum to implement a simple state machine for PMU register access. The PMU is either free or guest owned. We only need to context swap if the PMU registers are guest owned. The PMU initially starts as free and only transitions to guest owned if a guest has touched the PMU registers.

---
 arch/arm64/include/asm/kvm_host.h  |  1 +
 arch/arm64/include/asm/kvm_types.h |  6 +++++-
 arch/arm64/kvm/debug.c             |  5 +++--
 arch/arm64/kvm/pmu-direct.c        | 21 +++++++++++++++++++--
 arch/arm64/kvm/sys_regs.c          | 29 ++++++++++++++++-------------
 include/kvm/arm_pmu.h              |  8 ++++++++
 6 files changed, 52 insertions(+), 18 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
index 9c7e9b92dfbd3..32573b10d9c5b 100644
@@ -1445,6 +1445,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 +3 -2
index c84321277d893..ab80325e67c5c 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)
 {
 	preempt_disable();
 
@@ -63,7 +63,8 @@ static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
 		 * fine grain traps and enforce counter access with
 		 * HPMN.
 		 */
-		if (!vcpu_on_unsupported_cpu(vcpu) &&
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED &&
+		    !vcpu_on_unsupported_cpu(vcpu) &&
 		    cpus_have_final_cap(ARM64_HAS_FGT) &&
 		    (cpus_have_final_cap(ARM64_HAS_HPMN0) || nr_guest_cntr > 0)) {
 			vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_TPM | MDCR_EL2_TPMCR | MDCR_EL2_HPMN);
arch/arm64/kvm/pmu-direct.c +19 -2
index 044f011c9c84b..bb1f3dca03869 100644
@@ -269,7 +269,7 @@ 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 (vcpu->arch.pmu.access != VCPU_PMU_ACCESS_GUEST_OWNED)
 		return;
 
 	preempt_disable();
@@ -343,7 +343,7 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)
 	 * accessing the PMU anyway, so no need to bother with the
 	 * swap.
 	 */
-	if (!kvm_pmu_is_partitioned(vcpu->kvm))
+	if (vcpu->arch.pmu.access != VCPU_PMU_ACCESS_GUEST_OWNED)
 		return;
 
 	preempt_disable();
@@ -388,3 +388,20 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)
 	kvm_pmu_set_guest_counters(pmu, 0);
 	preempt_enable();
 }
+
+/**
+ * kvm_pmu_set_guest_owned() - Give PMU ownership to guest
+ * @vcpu: Pointer to vcpu struct
+ *
+ * Reconfigure the guest for physical access of PMU hardware if
+ * allowed. This means reconfiguring mdcr_el2.
+ *
+ */
+void kvm_pmu_set_guest_owned(struct kvm_vcpu *vcpu)
+{
+	if (kvm_pmu_is_partitioned(vcpu->kvm) &&
+	    vcpu->arch.pmu.access == VCPU_PMU_ACCESS_FREE) {
+		vcpu->arch.pmu.access = VCPU_PMU_ACCESS_GUEST_OWNED;
+		kvm_arm_setup_mdcr_el2(vcpu);
+	}
+}
arch/arm64/kvm/sys_regs.c +16 -13
index 94572bc52c32a..f0eebeeb5ed96 100644
@@ -1085,15 +1085,17 @@ static void pmu_reg_write(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg, u64 val,
 	u64 mask;
 	int idx;
 
+	kvm_pmu_set_guest_owned(vcpu);
+
 	switch (reg) {
 	case PMCR_EL0:
-		if (kvm_pmu_is_partitioned(vcpu->kvm))
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED)
 			kvm_pmu_direct_pmcr_write(vcpu, val);
 		else
 			kvm_pmu_handle_pmcr(vcpu, val);
 		break;
 	case PMSELR_EL0:
-		if (kvm_pmu_is_partitioned(vcpu->kvm))
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED)
 			write_sysreg(val, pmselr_el0);
 		else
 			__vcpu_assign_sys_reg(vcpu, reg, val);
@@ -1101,7 +1103,7 @@ static void pmu_reg_write(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg, u64 val,
 	case PMEVCNTR0_EL0 ... PMCCNTR_EL0:
 		idx = reg - PMEVCNTR0_EL0;
 
-		if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED) {
 			if (idx == ARMV8_PMU_CYCLE_IDX)
 				write_sysreg(val, pmccntr_el0);
 			else
@@ -1122,7 +1124,7 @@ static void pmu_reg_write(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg, u64 val,
 		}
 		break;
 	case PMCNTENSET_EL0:
-		if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED) {
 			if (set)
 				write_sysreg(val, pmcntenset_el0);
 			else
@@ -1139,7 +1141,7 @@ static void pmu_reg_write(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg, u64 val,
 		}
 		break;
 	case PMINTENSET_EL1:
-		if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED) {
 			if (set)
 				write_sysreg(val, pmintenset_el1);
 			else
@@ -1166,7 +1168,7 @@ 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_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED)
 			write_sysreg(val, pmuserenr_el0);
 		else
 			__vcpu_assign_sys_reg(vcpu, reg, val);
@@ -1175,7 +1177,6 @@ static void pmu_reg_write(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg, u64 val,
 		WARN_ON(1);
 		break;
 	}
-
 }
 
 /**
@@ -1192,15 +1193,17 @@ static u64 pmu_reg_read(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg)
 	u64 val = 0;
 	int idx;
 
+	kvm_pmu_set_guest_owned(vcpu);
+
 	switch (reg) {
 	case PMCR_EL0:
-		if (kvm_pmu_is_partitioned(vcpu->kvm))
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED)
 			val = kvm_pmu_direct_pmcr_read(vcpu);
 		else
 			val = kvm_vcpu_read_pmcr(vcpu);
 		break;
 	case PMSELR_EL0:
-		if (kvm_pmu_is_partitioned(vcpu->kvm))
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED)
 			val = read_sysreg(pmselr_el0);
 		else
 			val = __vcpu_sys_reg(vcpu, reg);
@@ -1208,7 +1211,7 @@ static u64 pmu_reg_read(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg)
 	case PMEVCNTR0_EL0 ... PMCCNTR_EL0:
 		idx = reg - PMEVCNTR0_EL0;
 
-		if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED) {
 			if (idx == ARMV8_PMU_CYCLE_IDX)
 				val = read_sysreg(pmccntr_el0);
 			else
@@ -1221,7 +1224,7 @@ static u64 pmu_reg_read(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg)
 		val = __vcpu_sys_reg(vcpu, reg);
 		break;
 	case PMCNTENSET_EL0:
-		if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED) {
 			val = read_sysreg(pmcntenset_el0);
 			val &= kvm_pmu_guest_counter_mask(vcpu->kvm->arch.arm_pmu);
 		} else {
@@ -1229,7 +1232,7 @@ static u64 pmu_reg_read(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg)
 		}
 		break;
 	case PMINTENSET_EL1:
-		if (kvm_pmu_is_partitioned(vcpu->kvm)) {
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED) {
 			val = read_sysreg(pmintenset_el1);
 			val &= kvm_pmu_guest_counter_mask(vcpu->kvm->arch.arm_pmu);
 		} else {
@@ -1240,7 +1243,7 @@ static u64 pmu_reg_read(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg)
 		val = __vcpu_sys_reg(vcpu, reg);
 		break;
 	case PMUSERENR_EL0:
-		if (kvm_pmu_is_partitioned(vcpu->kvm))
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED)
 			val = read_sysreg(pmuserenr_el0);
 		else
 			val = __vcpu_sys_reg(vcpu, reg);
include/kvm/arm_pmu.h +8 -0
index 61f8d4ed35e10..b77ddb94dc99b 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>
@@ -43,6 +44,7 @@ struct kvm_pmu {
 	int irq_num;
 	bool created;
 	bool irq_level;
+	enum vcpu_pmu_register_access access;
 };
 
 struct arm_pmu_entry {
@@ -103,6 +105,9 @@ u64 kvm_pmu_host_counter_mask(struct arm_pmu *pmu);
 u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);
 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);
+
+#define kvm_pmu_get_access(vcpu)	((vcpu)->arch.pmu.access)
 
 /*
  * Updates the vcpu's view of the pmu events for this cpu.
@@ -147,6 +152,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)
 {
@@ -154,6 +161,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.54.0.1136.gdb2ca164c4-goog
#30 lore raw

Re: [PATCH 15/21] KVM: arm64: Implement lazy PMU context swaps

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

New issues:
- [High] Lazy PMU context swap fails to load the guest's PMU context into hardware when transitioning to GUEST_OWNED.

Pre-existing issues:
- [High] Missing Context Synchronization Event (isb) after writing to the control-plane register mdcr_el2. --

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

KVM: arm64: Implement lazy PMU context swaps

Since many guests will never touch the PMU, they need not pay the cost of context swapping those registers.

Use an enum to implement a simple state machine for PMU register access. The PMU is either free or guest owned. We only need to context swap if the PMU registers are guest owned. The PMU initially starts as free and only transitions to guest owned if a guest has touched the PMU registers.

Quoted 4 lines
arch/arm64/kvm/pmu-direct.c +0 -0
index 044f011c9c84b..bb1f3dca03869 100644

[ ... ]

Quoted 9 lines
arch/arm64/kvm/pmu-direct.c +5 -0
@@ -388,3 +388,20 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)
 	kvm_pmu_set_guest_counters(pmu, 0);
 	preempt_enable();
 }
+
+/**
+ * kvm_pmu_set_guest_owned() - Give PMU ownership to guest
+ * @vcpu: Pointer to vcpu struct
+ *

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

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+ * Reconfigure the guest for physical access of PMU hardware if
+ * allowed. This means reconfiguring mdcr_el2.
+ *
+ */
+void kvm_pmu_set_guest_owned(struct kvm_vcpu *vcpu)
+{
+	if (kvm_pmu_is_partitioned(vcpu->kvm) &&
+	    vcpu->arch.pmu.access == VCPU_PMU_ACCESS_FREE) {
+		vcpu->arch.pmu.access = VCPU_PMU_ACCESS_GUEST_OWNED;
+		kvm_arm_setup_mdcr_el2(vcpu);

[Severity: High]
This is a pre-existing issue, but does kvm_arm_setup_mdcr_el2() need an isb() after writing to mdcr_el2?

When has_vhe() is true, it writes directly to mdcr_el2 but does not issue an isb() before returning. Now that kvm_pmu_set_guest_owned() is called from the active trap handling path, won't the immediate subsequent PMU hardware accesses run against an architecturally undefined pipeline state?

[Severity: High]
Does this transition the PMU ownership without loading the guest's PMU state?

If we don't call kvm_pmu_load(vcpu) here to synchronize the in-memory guest PMU state to the hardware PMU registers, won't the trap handler immediately access uninitialized physical hardware registers containing the host's state?

Quoted 7 lines
arch/arm64/kvm/pmu-direct.c +2 -0
+	}
+}
arch/arm64/kvm/sys_regs.c +0 -0
index 94572bc52c32a..f0eebeeb5ed96 100644

[ ... ]

arch/arm64/kvm/sys_regs.c static u64 pmu_reg_read(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg) · line 1201

Quoted 11 lines
arch/arm64/kvm/sys_regs.c +3 -1
@@ -1192,15 +1193,17 @@ static u64 pmu_reg_read(struct kvm_vcpu *vcpu, enum vcpu_sysreg reg)
 	u64 val = 0;
 	int idx;
 
+	kvm_pmu_set_guest_owned(vcpu);
+
 	switch (reg) {
 	case PMCR_EL0:
-		if (kvm_pmu_is_partitioned(vcpu->kvm))
+		if (kvm_pmu_get_access(vcpu) == VCPU_PMU_ACCESS_GUEST_OWNED)
 			val = kvm_pmu_direct_pmcr_read(vcpu);

[Severity: High]
Could this leak host state to the guest and later corrupt the guest's state?

Because kvm_pmu_set_guest_owned() didn't load the guest's PMU state, this read (and subsequent ones in this handler) will access the physical PMU registers that still hold the host's state.

Later, when the guest exits, kvm_pmu_put() will read these physical hardware registers and save them back into vcpu->arch, permanently corrupting the guest's saved PMU context.

Doesn't this also silently break live migration if restored PMU state is wiped out upon the guest's first trapped access?

Quoted 3 lines
arch/arm64/kvm/sys_regs.c +0 -0
 		else
 			val = kvm_vcpu_read_pmcr(vcpu);
 		break;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
#31 lore raw

PATCH v8 16/21 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 will still trigger interrupts that need to be
handled by the host PMU interrupt handler. Clear the overflow flags in
hardware to handle the interrupt as normal, but update the virtual overflow register for later injecting the interrupt into the guest.

---
 arch/arm/include/asm/arm_pmuv3.h   |  6 ++++++
 arch/arm64/include/asm/arm_pmuv3.h |  5 +++++
 arch/arm64/kvm/pmu-direct.c        | 22 ++++++++++++++++++++++
 drivers/perf/arm_pmuv3.c           | 24 +++++++++++++++++-------
 include/kvm/arm_pmu.h              |  3 +++
 5 files changed, 53 insertions(+), 7 deletions(-)
arch/arm/include/asm/arm_pmuv3.h +6 -0
index f6031bd522718..896fc5d6add0c 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);
@@ -239,6 +244,7 @@ static inline u64 kvm_pmu_host_counter_mask(struct arm_pmu *pmu)
 {
 	return ~0;
 }
+static inline void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) {}
 
 /* PMU Version in DFR Register */
 #define ARMV8_PMU_DFR_VER_NI        0
arch/arm64/include/asm/arm_pmuv3.h +5 -0
index 27c4d6d47da31..69ff4d014bf39 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 +22 -0
index bb1f3dca03869..64f40cfb31012 100644
@@ -405,3 +405,25 @@ void kvm_pmu_set_guest_owned(struct kvm_vcpu *vcpu)
 		kvm_arm_setup_mdcr_el2(vcpu);
 	}
 }
+
+/**
+ * kvm_pmu_handle_guest_irq() - Record IRQs in guest counters
+ * @pmu: PMU to check for overflows
+ * @pmovsr: Overflow flags reported by driver
+ *
+ * Set overflow flags in guest-reserved counters in the VCPU register
+ * for the guest to clear later.
+ */
+void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr)
+{
+	struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
+	u64 mask = kvm_pmu_guest_counter_mask(pmu);
+	u64 govf = pmovsr & mask;
+
+	write_pmovsclr(govf);
+
+	if (!vcpu)
+		return;
+
+	__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, govf);
+}
drivers/perf/arm_pmuv3.c +17 -7
index c187397134990..6ab15a5209608 100644
@@ -774,16 +774,15 @@ 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);
+	/* Only report interrupt enabled counters. */
+	value &= read_pmintenset();
 
 	return value;
 }
@@ -897,16 +896,17 @@ 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 host_set = kvm_pmu_host_counter_mask(cpu_pmu);
+	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?
@@ -914,6 +914,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 the kvm hook at the bottom of
+	 * this function.
+	 */
+	write_pmovsclr(pmovsr & host_set);
+
 	/*
 	 * Handle the counter(s) overflow(s)
 	 */
@@ -955,6 +961,10 @@ static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)
 		 */
 		perf_event_overflow(event, &data, regs);
 	}
+
+	if (pmu_is_partitioned(cpu_pmu))
+		kvm_pmu_handle_guest_irq(cpu_pmu, pmovsr);
+
 	armv8pmu_start(cpu_pmu);
 
 	return IRQ_HANDLED;
include/kvm/arm_pmu.h +3 -0
index b77ddb94dc99b..25163a689ae80 100644
@@ -106,6 +106,7 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);
 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(struct arm_pmu *pmu, u64 pmovsr);
 
 #define kvm_pmu_get_access(vcpu)	((vcpu)->arch.pmu.access)
 
@@ -274,6 +275,8 @@ static inline u64 kvm_pmu_guest_counter_mask(void *kvm)
 	return 0;
 }
 
+static inline void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) {}
+
 #endif
 
 #endif
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#32 lore raw

Re: [PATCH 16/21] perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] Host perf event overflows corrupt the virtual PMU state of running vCPUs. - [High] KVM traps…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] Host perf event overflows corrupt the virtual PMU state of running vCPUs.
- [High] KVM traps PMOVSCLR_EL0 but fails to read live hardware state, breaking guest PMU polling. --

commit 18a5ac7857cf364463b827796f49cf7c00c6ef08
Author: Colton Lewis <[email protected]>

perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters

Because ARM hardware is not yet capable of direct PPI injection into guests, guest counters will still trigger interrupts that need to be handled by the host PMU interrupt handler. Clear the overflow flags in hardware to handle the interrupt as normal, but update the virtual overflow register for later injecting the interrupt into the guest.

Quoted 5 lines
arch/arm64/kvm/pmu-direct.c +0 -0
index bb1f3dca03869..64f40cfb31012 100644
@@ -405,3 +405,25 @@ void kvm_pmu_set_guest_owned(struct kvm_vcpu *vcpu)

[ ... ]

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+/**
+ * kvm_pmu_handle_guest_irq() - Record IRQs in guest counters
+ * @pmu: PMU to check for overflows
+ * @pmovsr: Overflow flags reported by driver
+ *
+ * Set overflow flags in guest-reserved counters in the VCPU register
+ * for the guest to clear later.
+ */
+void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr)
+{

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

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+	struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
+	u64 mask = kvm_pmu_guest_counter_mask(pmu);
+	u64 govf = pmovsr & mask;
+
+	write_pmovsclr(govf);
+
+	if (!vcpu)
+		return;
+
+	__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, govf);

[Severity: Critical]
Can host perf event overflows corrupt the virtual pmu state of running vCPUs?

When the host pmu is partitioned, the host perf subsystem is allowed to use the guest partition counters (0..hpmn) whenever the currently running vCPU does not own the pmu (e.g., vCPUs using the emulated pmu).

However, kvm_pmu_host_counter_mask() unconditionally excludes these counters from the host's clear mask. When a host event on these counters overflows, the host irq handler leaves the hardware flag set and passes the overflow to kvm_pmu_handle_guest_irq().

This code blindly applies the host overflow to the currently running vCPU's PMOVSSET_EL0 software state without verifying if the vCPU actually owns the pmu partition:

    vcpu->arch.pmu.access == VCPU_PMU_ACCESS_GUEST_OWNED

Could this corrupt the virtual pmu state and inject spurious overflows into innocent VMs?

Quoted 12 lines
arch/arm64/kvm/pmu-direct.c +1 -0
+}
drivers/perf/arm_pmuv3.c +1 -1
index c187397134990..6ab15a5209608 100644
@@ -774,16 +774,15 @@ 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)
 {

drivers/perf/arm_pmuv3.c static void armv8pmu_disable_event_irq(struct perf_event *event) · line 785

Quoted 10 lines
drivers/perf/arm_pmuv3.c +2 -3
 	u64 value;
 
 	/* Read */
 	value = read_pmovsclr();
 
-	/* Write to clear flags */
-	value &= ARMV8_PMU_CNT_MASK_ALL;
-	write_pmovsclr(value);
+	/* Only report interrupt enabled counters. */
+	value &= read_pmintenset();

[Severity: High]
Does masking out overflow flags for counters without interrupts enabled break guest pmu polling?

KVM unconditionally traps guest accesses to PMOVSCLR_EL0 and PMOVSSET_EL0 (via FGT HDFGRTR_EL2_PMOVS) to prevent the guest from seeing host counter overflows. However, the trap handlers (pmu_reg_read and pmu_reg_write in arch/arm64/kvm/sys_regs.c) only interact with KVM's software shadow state and ignore the live hardware pmovsclr_el0 register for guest-owned counters.

Since this masks hardware overflows with read_pmintenset(), counters with interrupts disabled will never trigger the host irq handler to sync the shadow state.

Consequently, will a guest polling for overflows with pmu interrupts disabled loop infinitely reading stale zeros and miss the overflow events entirely?

Quoted 3 lines
drivers/perf/arm_pmuv3.c +0 -0
 	return value;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
#33 lore raw

PATCH v8 17/21 KVM: arm64: Detect overflows for the Partitioned PMU

In reply to Colton Lewis

When we re-enter the VM after handling a PMU interrupt, calculate whether it was any of the guest counters that overflowed and inject an interrupt into the guest if so.

---
 arch/arm64/kvm/pmu-direct.c | 48 +++++++++++++++++++++++++++++++++++++
 arch/arm64/kvm/pmu-emul.c   |  4 ++--
 arch/arm64/kvm/pmu.c        |  6 ++++-
 include/kvm/arm_pmu.h       |  2 ++
 4 files changed, 57 insertions(+), 3 deletions(-)
arch/arm64/kvm/pmu-direct.c +48 -0
index 64f40cfb31012..0062d1d8e1999 100644
@@ -426,4 +426,52 @@ void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr)
 		return;
 
 	__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, govf);
+
+	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 access is still free,
+ * then the guest hasn't accessed the PMU yet so we know the guest
+ * context is not loaded onto the pCPU and an overflow is impossible.
+ *
+ * Return: True if there was an overflow, false otherwise
+ */
+bool kvm_pmu_part_overflow_status(struct kvm_vcpu *vcpu)
+{
+	struct arm_pmu *pmu;
+	u64 mask, pmovs, pmint, pmcr;
+	bool overflow;
+
+	pmu = vcpu->kvm->arch.arm_pmu;
+	mask = kvm_pmu_guest_counter_mask(pmu);
+
+	if (vcpu->arch.pmu.access == VCPU_PMU_ACCESS_FREE) {
+		pmovs = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
+		pmint = __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
+		pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
+
+		if ((pmcr & ARMV8_PMU_PMCR_E) && (mask & pmovs & pmint))
+			kvm_pmu_set_guest_owned(vcpu);
+		else
+			return false;
+	}
+
+	pmovs = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
+	pmint = read_pmintenset();
+	pmcr = read_pmcr();
+	overflow = (pmcr & ARMV8_PMU_PMCR_E) && (mask & pmovs & pmint);
+
+	return overflow;
 }
arch/arm64/kvm/pmu-emul.c +2 -2
index d1110febe7436..ebc68090bdb26 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);
 
@@ -405,7 +405,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 +5 -1
index 55cda8021400a..f5ee18b4dfae7 100644
@@ -409,7 +409,11 @@ static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
 	struct kvm_pmu *pmu = &vcpu->arch.pmu;
 	bool overflow;
 
-	overflow = kvm_pmu_overflow_status(vcpu);
+	if (kvm_pmu_is_partitioned(vcpu->kvm))
+		overflow = kvm_pmu_part_overflow_status(vcpu);
+	else
+		overflow = kvm_pmu_emul_overflow_status(vcpu);
+
 	if (pmu->irq_level == overflow)
 		return;
 
include/kvm/arm_pmu.h +2 -0
index 25163a689ae80..f72d080ee7ba2 100644
@@ -93,6 +93,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.54.0.1136.gdb2ca164c4-goog
#34 lore raw

Re: [PATCH 17/21] KVM: arm64: Detect overflows for the Partitioned PMU

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [High] Missing return and failure to synchronize hardware registers when transitioning to VCPU_PMU_ACCESS_GUEST_OWNED drops interrupts and corrupts guest PMU state.
- [High] kvm_pmu_part_overflow_status() evaluates overflow using stale software PMOVSSET_EL0 instead of the physical register when the PMU is guest-owned, missing hardware overflows.
- [Low] Unused prototype for kvm_pmu_overflow_status left in header.

Pre-existing issues:
- [High] Non-atomic read-modify-write of PMOVSSET_EL0 in hardirq context corrupts concurrent process-context writes.
- [High] Missing required isb() synchronization immediately following a write to the pmcr_el0 control-plane system register. --

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

KVM: arm64: Detect overflows for the Partitioned PMU

When we re-enter the VM after handling a PMU interrupt, calculate whether it was any of the guest counters that overflowed and inject an interrupt into the guest if so.

arch/arm64/kvm/pmu-direct.c

Quoted 4 lines
arch/arm64/kvm/pmu-direct.c +0 -0
index 64f40cfb31012..0062d1d8e1999 100644

[Severity: High]
This isn't a bug introduced by this patch, but looking at kvm_pmu_direct_pmcr_write() in this file, is there a missing isb() after writing the control register?

arch/arm64/kvm/pmu-direct.c:kvm_pmu_direct_pmcr_write() {
    write_sysreg(val, pmcr_el0);
    if (reset_p) {

...
}

Architectural rules require every write to a control-plane system register to be followed by an isb() as the very next instruction. Since it is followed immediately by a conditional branch, can the pipeline observe undefined state?

arch/arm64/kvm/pmu-direct.c void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) · line 428

Quoted 4 lines
arch/arm64/kvm/pmu-direct.c +0 -0
@@ -426,4 +426,52 @@ void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr)
 		return;
 
 	__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, govf);

[Severity: High]
This is a pre-existing issue, but does this non-atomic read-modify-write in hardirq context corrupt concurrent process-context writes?

When the physical PMU interrupt handler calls kvm_pmu_handle_guest_irq(), it executes this macro in hardirq context. Concurrently, the VM-exit path (access_pmovs()) can execute the same macro in process context with interrupts enabled.

If the hardware interrupt preempts the process context between the read and write phases, could the process context overwrite the sysreg with its stale read and silently drop the overflow bits?

Quoted 13 lines
arch/arm64/kvm/pmu-direct.c +13 -0
+
+	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 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+bool kvm_pmu_part_overflow_status(struct kvm_vcpu *vcpu)
+{
+	struct arm_pmu *pmu;
+	u64 mask, pmovs, pmint, pmcr;
+	bool overflow;
+
+	pmu = vcpu->kvm->arch.arm_pmu;
+	mask = kvm_pmu_guest_counter_mask(pmu);
+
+	if (vcpu->arch.pmu.access == VCPU_PMU_ACCESS_FREE) {

arch/arm64/kvm/pmu-direct.c void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) · line 470

Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10 -0
+		pmovs = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
+		pmint = __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
+		pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
+
+		if ((pmcr & ARMV8_PMU_PMCR_E) && (mask & pmovs & pmint))
+			kvm_pmu_set_guest_owned(vcpu);
+		else
+			return false;
+	}
+

[Severity: High]
Does this fall through to read uninitialized hardware state?

When a virtual overflow is detected and kvm_pmu_set_guest_owned() is called, it transitions the state but there is no return true; here. Execution falls through to unconditionally read the physical hardware registers below.

Additionally, looking at kvm_pmu_set_guest_owned(), it grants direct hardware access via MDCR_EL2 but fails to invoke kvm_pmu_load() to populate the hardware PMU with the guest's context.

Since the hardware wasn't loaded, could these reads return host state, causing the calculation to evaluate to false and drop the pending interrupt?

arch/arm64/kvm/pmu-direct.c void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) · line 473

Quoted 3 lines
arch/arm64/kvm/pmu-direct.c +3 -0
+	pmovs = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
+	pmint = read_pmintenset();
+	pmcr = read_pmcr();

[Severity: High]
Does this evaluate overflow using stale software state instead of the physical register when the PMU is guest-owned?

When the PMU is guest-owned, physical hardware overflows update the physical register. The software context is only synchronized during kvm_pmu_put().

Since this is called during guest exit via kvm_pmu_sync_hwstate() before kvm_pmu_put() runs, could unconditionally reading the software context via __vcpu_sys_reg() evaluate against stale state and miss physical hardware overflows?

Quoted 4 lines
arch/arm64/kvm/pmu-direct.c +3 -0
+	overflow = (pmcr & ARMV8_PMU_PMCR_E) && (mask & pmovs & pmint);
+
+	return overflow;
 }

[ ... ]

include/kvm/arm_pmu.h bool kvm_set_pmuserenr(u64 val); · line 97

Quoted 10 lines
include/kvm/arm_pmu.h +2 -0
index 25163a689ae80..f72d080ee7ba2 100644
@@ -93,6 +93,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);

[Severity: Low]
Since kvm_pmu_overflow_status() was renamed to kvm_pmu_emul_overflow_status() in this patch, should the old prototype be removed from this header file?

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

PATCH v8 18/21 KVM: arm64: Add vCPU device attr to partition the PMU

In reply to Colton Lewis

Add a new PMU device attr to enable the partitioned PMU for a given VM. This capability can be set when the PMU is initially configured before the vCPU starts running and is allowed where PMUv3 and VHE are supported and the host driver was configured with arm_pmuv3.reserved_host_counters.

The enabled capability is tracked by the new flag
KVM_ARCH_FLAG_PARTITION_PMU_ENABLED.

---
 arch/arm64/include/uapi/asm/kvm.h |  2 ++
 arch/arm64/kvm/pmu-direct.c       | 30 ++++++++++++++++++++++++++++++
 arch/arm64/kvm/pmu.c              | 23 +++++++++++++++++++++++
 include/kvm/arm_pmu.h             |  9 +++++++++
 4 files changed, 64 insertions(+)
arch/arm64/include/uapi/asm/kvm.h +2 -0
index 1c13bfa2d38aa..7f57b8c132925 100644
@@ -437,6 +437,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-direct.c +30 -0
index 0062d1d8e1999..2d2294b78ebe0 100644
@@ -24,6 +24,36 @@ bool has_host_pmu_partition_support(void)
 		system_supports_pmuv3();
 }
 
+
+/**
+ * has_kvm_pmu_partition_support() - If we can enable/disable partition
+ *
+ * Return: true if allowed, false otherwise.
+ */
+bool has_kvm_pmu_partition_support(void)
+{
+	return has_host_pmu_partition_support() &&
+		kvm_supports_guest_pmuv3() &&
+		armv8pmu_is_partitioned;
+}
+
+/**
+ * kvm_pmu_partition_enable() - Enable/disable partition flag
+ * @kvm: Pointer to vcpu
+ * @enable: Whether to enable or disable
+ *
+ * If we want to enable the partition, the guest is free to grab
+ * hardware by accessing PMU registers. Otherwise, the host maintains
+ * control.
+ */
+void kvm_pmu_partition_enable(struct kvm *kvm, bool enable)
+{
+	if (enable)
+		set_bit(KVM_ARCH_FLAG_PARTITION_PMU_ENABLED, &kvm->arch.flags);
+	else
+		clear_bit(KVM_ARCH_FLAG_PARTITION_PMU_ENABLED, &kvm->arch.flags);
+}
+
 /**
  * pmu_is_partitioned() - Determine if given PMU is partitioned
  * @pmu: Pointer to arm_pmu struct
arch/arm64/kvm/pmu.c +23 -0
index f5ee18b4dfae7..4e15948ac2565 100644
@@ -769,6 +769,28 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
 
 		return kvm_arm_pmu_v3_set_nr_counters(vcpu, n);
 	}
+	case KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION: {
+		unsigned int __user *uaddr = (unsigned int __user *)(long)attr->addr;
+		u32 val;
+
+		if (get_user(val, uaddr))
+			return -EFAULT;
+
+		if (!has_kvm_pmu_partition_support())
+			return -EPERM;
+
+		if (kvm_vm_has_ran_once(kvm))
+			return -EBUSY;
+
+		kvm_pmu_partition_enable(kvm, val);
+		if (val) {
+			unsigned int max_counters = kvm_arm_pmu_get_max_counters(kvm);
+
+			if (kvm->arch.nr_pmu_counters > max_counters)
+				kvm_arm_set_nr_counters(kvm, max_counters);
+		}
+		return 0;
+	}
 	case KVM_ARM_VCPU_PMU_V3_INIT:
 		return kvm_arm_pmu_v3_init(vcpu);
 	}
@@ -808,6 +830,7 @@ int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
 	case KVM_ARM_VCPU_PMU_V3_FILTER:
 	case KVM_ARM_VCPU_PMU_V3_SET_PMU:
 	case KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS:
+	case KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION:
 		if (kvm_vcpu_has_pmu(vcpu))
 			return 0;
 	}
include/kvm/arm_pmu.h +9 -0
index f72d080ee7ba2..6a5572994b7fa 100644
@@ -99,6 +99,8 @@ 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))
 
+bool has_kvm_pmu_partition_support(void);
+void kvm_pmu_partition_enable(struct kvm *kvm, bool enable);
 bool pmu_is_partitioned(struct arm_pmu *pmu);
 bool kvm_pmu_is_partitioned(struct kvm *kvm);
 void kvm_pmu_direct_pmcr_write(struct kvm_vcpu *vcpu, u64 val);
@@ -279,6 +281,13 @@ static inline u64 kvm_pmu_guest_counter_mask(void *kvm)
 
 static inline void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) {}
 
+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.54.0.1136.gdb2ca164c4-goog
#36 lore raw

Re: [PATCH 18/21] KVM: arm64: Add vCPU device attr to partition the PMU

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Unprivileged host userspace can trigger a host kernel panic (NULL pointer dereference) by calling…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Unprivileged host userspace can trigger a host kernel panic (NULL pointer dereference) by calling KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION before the VM's PMU is initialized.
- [Medium] kvm_arm_pmu_v3_has_attr() incorrectly advertises support for the KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION attribute on systems that do not actually support it, breaking live migration. --

commit 69324cfdaa77bd05919e5ffe00139f0a4c7a3e0c
Author: Colton Lewis <[email protected]>

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

Add a new PMU device attr to enable the partitioned PMU for a given VM. This capability can be set when the PMU is initially configured before the vCPU starts running.

Quoted 14 lines
arch/arm64/kvm/pmu.c +6 -0
index f5ee18b4dfae7..4e15948ac2565 100644
@@ -769,6 +769,28 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
 
 		return kvm_arm_pmu_v3_set_nr_counters(vcpu, n);
 	}
+	case KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION: {
+		unsigned int __user *uaddr = (unsigned int __user *)(long)attr->addr;
+		u32 val;
+
+		if (get_user(val, uaddr))
+			return -EFAULT;

arch/arm64/kvm/pmu.c int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) · line 787

Quoted 10 lines
arch/arm64/kvm/pmu.c +10 -0
+
+		if (!has_kvm_pmu_partition_support())
+			return -EPERM;
+
+		if (kvm_vm_has_ran_once(kvm))
+			return -EBUSY;
+
+		kvm_pmu_partition_enable(kvm, val);
+		if (val) {
+			unsigned int max_counters = kvm_arm_pmu_get_max_counters(kvm);

[Severity: High]
Can an unprivileged host userspace process trigger a NULL pointer dereference here if the PMU is not yet initialized?

If KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION is called before the PMU is initialized, kvm->arch.arm_pmu will be NULL.

When this code calls kvm_arm_pmu_get_max_counters(), it dereferences the NULL pointer inside bitmap_weight():

arch/arm64/kvm/pmu.c:kvm_arm_pmu_get_max_counters() {
    struct arm_pmu *arm_pmu = kvm->arch.arm_pmu;
    ...
    return bitmap_weight(arm_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS);
}
Quoted 6 lines
arch/arm64/kvm/pmu.c +6 -0
+
+			if (kvm->arch.nr_pmu_counters > max_counters)
+				kvm_arm_set_nr_counters(kvm, max_counters);
+		}
+		return 0;
+	}

[ ... ]

arch/arm64/kvm/pmu.c int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) · line 836

Quoted 8 lines
arch/arm64/kvm/pmu.c +1 -0
@@ -808,6 +830,7 @@ int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
 	case KVM_ARM_VCPU_PMU_V3_FILTER:
 	case KVM_ARM_VCPU_PMU_V3_SET_PMU:
 	case KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS:
+	case KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION:
 		if (kvm_vcpu_has_pmu(vcpu))
 			return 0;
 	}

[Severity: Medium]
Does this incorrectly advertise support for KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION on systems that don't support it?

If kvm_arm_pmu_v3_has_attr() returns 0 as long as the VCPU has a PMU, VMMs might assume the partition capability is available.

However, setting this attribute in kvm_arm_pmu_v3_set_attr() explicitly rejects the configuration if has_kvm_pmu_partition_support() is false:

arch/arm64/kvm/pmu.c:kvm_arm_pmu_v3_set_attr() {
    ...
    case KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION: {
        ...
        if (!has_kvm_pmu_partition_support())
            return -EPERM;
    }
}

Could this inconsistency cause live migrations to fail when the VMM attempts to restore this attribute on a destination that lacks partition support?

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

PATCH v8 19/21 KVM: selftests: Add find_bit to KVM library

In reply to Colton Lewis

Some selftests have a dependency on find_bit and weren't compiling separately without it, so I've added it to the KVM library here using the same method as files like rbtree.c.

---
 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 9118a5a51b89f..fa7a2746b1c13 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.54.0.1136.gdb2ca164c4-goog
#38 lore raw

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

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The non-atomic `__vcpu_rmw_sys_reg` macro is used to update `PMOVSSET_EL0` from both the VCPU…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The non-atomic __vcpu_rmw_sys_reg macro is used to update PMOVSSET_EL0 from both the VCPU thread and the PMU NMI handler, leading to lost guest overflow events. --

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

KVM: selftests: Add find_bit to KVM library

This commit adds the find_bit functionality to the KVM selftests library. It resolves separate compilation issues for selftests that have a dependency on find_bit by including it similarly to rbtree.c.

Quoted 5 lines
arch/arm64/kvm/pmu-direct.c new file +0 -0
new file mode 100644
index 0000000000000..2d2294b78ebe0

[ ... ]

Quoted 2 lines
arch/arm64/kvm/pmu-direct.c new file +2 -0
+void kvm_pmu_put(struct kvm_vcpu *vcpu)
+{

[ ... ]

arch/arm64/kvm/pmu-direct.c

Quoted 13 lines
arch/arm64/kvm/pmu-direct.c new file +13 -0
+	/* Save pending guest hardware overflows. */
+	local_irq_save(flags);
+	val = read_sysreg(pmovsset_el0);
+	__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, val & mask);
+	write_sysreg(val & mask, pmovsclr_el0);
+	local_irq_restore(flags);
+
+	/* Stop guest counters and disable interrupts in hardware. */
+	write_sysreg(mask, pmcntenclr_el0);
+	write_sysreg(mask, pmintenclr_el1);
+	kvm_pmu_set_guest_counters(pmu, 0);
+	preempt_enable();
+}

[Severity: High]
Does this non-atomic register update risk losing guest overflow events?

Since __vcpu_rmw_sys_reg() is non-atomic, and local_irq_save() does not mask NMIs on ARM64 when pseudo-NMIs are enabled, could an NMI interrupt the VCPU thread exactly between the load and store phases here?

If the PMU NMI handler injects a guest overflow flag during this window, wouldn't it be overwritten and permanently lost by the stale writeback?

[ ... ]

arch/arm64/kvm/pmu-direct.c

Quoted 12 lines
arch/arm64/kvm/pmu-direct.c new file +12 -0
+void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr)
+{
+	struct kvm_vcpu *vcpu = kvm_get_running_vcpu();
+	u64 mask = kvm_pmu_guest_counter_mask(pmu);
+	u64 govf = pmovsr & mask;
+
+	write_pmovsclr(govf);
+
+	if (!vcpu)
+		return;
+
+	__vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, govf);

[Severity: High]
Is this concurrent modification safe?

Since this NMI handler asynchronously modifies PMOVSSET_EL0 using the same non-atomic __vcpu_rmw_sys_reg() macro used by the VCPU thread in kvm_pmu_put(), could they race?

If this handler runs while the VCPU thread is performing its own read-modify-write on PMOVSSET_EL0, wouldn't the guest overflow flag be overwritten and lost, leading to incorrect counter states in the guest?

Quoted 3 lines
arch/arm64/kvm/pmu-direct.c new file +3 -0
+
+	if (kvm_pmu_part_overflow_status(vcpu)) {
+		kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
#39 lore raw

PATCH v8 20/21 KVM: arm64: selftests: Add test case for Partitioned PMU

In reply to Colton Lewis

Rerun all tests for a Partitioned PMU in vpmu_counter_access.

Create an enum specifying whether we are testing the emulated or Partitioned PMU and all the test functions are modified to take the implementation as an argument and make the difference in setup appropriately.

---
 .../selftests/kvm/arm64/vpmu_counter_access.c | 94 ++++++++++++++-----
 1 file changed, 73 insertions(+), 21 deletions(-)
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +73 -21
index 22223395969e0..9be6034335283 100644
@@ -25,9 +25,20 @@
 /* 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;
+	bool pmu_partitioned;
 };
 
 static struct vpmu_vm vpmu_vm;
@@ -399,7 +410,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 +420,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 +454,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);
+		vpmu_vm.pmu_partitioned = partition;
+		pr_debug("Set PMU partitioning: %d\n", partition);
+	}
+
 }
 
 static void destroy_vpmu_vm(void)
@@ -461,13 +488,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)));
@@ -489,7 +517,7 @@ static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, bool
  * 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 +525,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 +559,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 +616,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,11 +628,11 @@ 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;
 
-	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);
@@ -614,7 +642,7 @@ 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 +650,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())
+		test_pmu(PARTITIONED);
 
 	return 0;
 }
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
#40 lore raw

PATCH v8 21/21 KVM: arm64: selftests: Relax testing for exceptions when partitioned

In reply to Colton Lewis

Because the Partitioned PMU must lean heavily on underlying hardware support, it can't guarantee an exception occurs when accessing an invalid pmc index.

The ARM manual specifies that accessing PMEVCNTR<n>_EL0 where n is greater than the number of counters on the system is constrained unpredictable when FEAT_FGT is not implemented, and it is desired the Partitioned PMU still work without FEAT_FGT.

Though KVM could enforce exceptions here since all PMU accesses without FEAT_FGT are trapped, that creates further difficulties. For one example, the manual also says that after writing a value to PMSELR_EL0 greater than the number of counters on a system, direct reads will return an unknown value, meaning KVM could not rely on the hardware register to hold the correct value.

---
 .../selftests/kvm/arm64/vpmu_counter_access.c | 20 ++++++++++++++-----
 1 file changed, 15 insertions(+), 5 deletions(-)
tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +15 -5
index 9be6034335283..e8c3856df77b7 100644
@@ -38,10 +38,14 @@ const char *pmu_impl_str[] = {
 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;
@@ -342,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.
@@ -459,7 +468,7 @@ static void create_vpmu_vm(void *guest_code, enum pmu_impl impl)
 		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);
-		vpmu_vm.pmu_partitioned = partition;
+		guest_context.pmu_partitioned = partition;
 		pr_debug("Set PMU partitioning: %d\n", partition);
 	}
 
@@ -511,6 +520,7 @@ static void test_create_vpmu_vm_with_nr_counters(
 		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);
 }
 
 /*
Signature · 1 line
2.54.0.1136.gdb2ca164c4-goog
reviewer
#41 lore raw
In reply to Colton Lewis
Quoted 8 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.

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

Quoted 9 lines

The kernel command line parameter for the driver still exists, but now only defines an upper limit of counters the guest might use rather than taking those counters from the host permanently.

I would appreciate any discussion on whether that parameter should still exist as it's an inconvenient enabling gate on the feature that is no longer required. The question comes down to what, if any, guards we want against a guest monopolizing all counters on a system.

Hi Colton,

The existence of the parameter makes sense, but can't the default be arm_pmuv3.reserved_host_counters=0 instead of -1 (partition disabled)? It's still a bit fiddly having to do two things to make it work, and there's no documentation about what the defaults or other prerequisites are. IMO it's not even trivial to work out that you need to prefix it with "arm_pmuv3.", which documentation would improve.

Testing the whole set I ran into a few issues:

This warn is hit when there is some kind of interaction with sleeping. I tried to bisect it but it only appears on the last commit when the option to enable partitioning is added.

	/*
	 * ARM pmu always has to reprogram the period, so ignore
	 * PERF_EF_RELOAD, see the comment below.
	 */
	if (flags & PERF_EF_RELOAD)
		WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));

Steps to reproduce:

Host (arm_pmuv3.reserved_host_counters=0):

 $ sudo perf stat -C 2 -e \
   'branches,branches,branches,branches,branches,branches'
 $ sudo taskset --cpu-list 2 ./lkvm run --kernel \
   /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

 $ sleep 1
  WARNING: drivers/perf/arm_pmu.c:302 at cpu_pm_pmu_notify+0x278/0x2c0, 
CPU#2: swapper/2/0
   Call trace:
  cpu_pm_pmu_notify+0x278/0x2c0 (P)
  notifier_call_chain+0x84/0x1d0
  raw_notifier_call_chain+0x24/0x38
  cpu_pm_exit+0x34/0x68
  acpi_processor_ffh_lpi_enter+0x40/0x78
  acpi_idle_lpi_enter+0x54/0x78
  cpuidle_enter_state+0xb4/0x248
  cpuidle_enter+0x44/0x68
  do_idle+0x21c/0x300
  cpu_startup_entry+0x40/0x50
  secondary_start_kernel+0x120/0x150
  __secondary_switched+0xc0/0xc8

When running the guest on a single CPU I get different counts for the same event for a single process, although this never happens on a host. I think there might even be some Perf tests which expect them to be the same, and this doesn't depend on whether any events are running on the host or not. Not sure if you ran all the Perf selftests in a guest or not?

I'm not sure if the exception level filtering isn't working or there is something wrong with freezing. Doesn't the host PMU driver need to freeze with HPME? I see it's still doing it with PMCR_EL0.E which freezes the guest's counters now doesn't it?

Host (arm_pmuv3.reserved_host_counters=0):

 $ sudo taskset --cpu-list 2 ./lkvm run --kernel \
   /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

 $ perf stat -e branches,branches true

Performance counter stats for 'true':

            167963      branches 

            160925      branches
 $ perf stat -e branches,branches,branches,branches,branches true

Performance counter stats for 'true':

            164425      branches 

            164425      branches 

            164425      branches 

            157743      branches 

            157743      branches

When running the guest on two CPUs I just get zeros. Do the kvm selftests not catch this? Or is it something to do with my setup:

Host (arm_pmuv3.reserved_host_counters=0):

 $ sudo taskset --cpu-list 2-3 ./lkvm run --kernel \
   /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

 $ perf stat -e branches,branches true

Performance counter stats for 'true':

0 branches

0 branches

I also noticed I get the "squeezed" warning printed after launching the guest but not using Perf. This comment implies that not using counters makes it a nop:

/*

	 * 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 (vcpu->arch.pmu.access != VCPU_PMU_ACCESS_GUEST_OWNED)
		return;

But maybe linux is touching them in a way that makes them guest owned, even on probe? Or is the guest/host ownership tracking not working, I didn't look too hard.

Finally I think there are 3 critical Sashiko comments on this version. If they're false positives, then maybe some comments in the code or commit messages could reassure it.

Thanks
James

Quoted 129 lines

v8:

* Rebase on top of v7.1-rc7.

* Implement Oliver Upton's accessor proposal to centralize PMU register access and simplify trap handlers. Instead of one singular accessor, implement as two because the read and write paths are always different anyway.

* Introduce the partitioning flag along with the
kvm_pmu_is_partitioned predicate

* Don't use ifdef for partitioning predicates as that can be handled by has_vhe

* Clean up MDCR_EL2 handling by open-coding use_fgt and hpmn and unconditionally setting RES0 bits.

* Use {read,write}_pmcrcntrn in context swaps

* Put operators on preceeding lines

* Rename hw_cntr_mask to hw_cntr_impl to clarify it tracks the number of counters implemented by hardware

* Use GENMASK_ULL in mask functions returning u64

* warn_once when host events are squeezed out by guest counter allocations.

* Address Sashiko AI Review findings:

- Critical fixes for lazy PMU context swaps (ensuring guest state is loaded on transition to GUEST_OWNED), PMSELR_EL0 trapping to prevent stale selector index, and masking guest PMCR_EL0 writes to prevent host reset.

- High priority fixes for lock safety (disabling IRQs when acquiring perf context lock), disabling guest counters on vCPU put, preserving VHE host profiling in MDCR_EL2, waking halted vCPUs on guest PMU interrupts, masking host configuration leaks, preemption safety in per-CPU accesses, emulating PMCR.N reads, and preventing data races in PMOVSSET_EL0 accesses.

- Medium/Low fixes for user-access fallback safety, VM-wide state modification restrictions, selftests type safety, and cleanup of unused fields and typos.

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 (20):
arm64: cpufeature: Add cpucap for HPMN0
KVM: arm64: Reorganize PMU functions
perf: arm_pmuv3: Generalize counter bitmasks
perf: arm_pmuv3: Check cntr_mask before using pmccntr
perf: arm_pmuv3: Allocate counter indices from high to low
perf: arm_pmuv3: Add method to partition the 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: 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
KVM: arm64: selftests: Relax testing for exceptions when partitioned

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

  arch/arm/include/asm/arm_pmuv3.h              |  18 +
  arch/arm64/include/asm/arm_pmuv3.h            |  12 +-
  arch/arm64/include/asm/kvm_host.h             |  17 +-
  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                          |   2 +
  arch/arm64/kvm/config.c                       |  41 +-
  arch/arm64/kvm/debug.c                        |  30 +-
  arch/arm64/kvm/pmu-direct.c                   | 507 ++++++++++++
  arch/arm64/kvm/pmu-emul.c                     | 684 +----------------
  arch/arm64/kvm/pmu.c                          | 720 ++++++++++++++++++
  arch/arm64/kvm/sys_regs.c                     | 271 +++++--
  arch/arm64/tools/cpucaps                      |   1 +
  arch/arm64/tools/sysreg                       |   6 +-
  drivers/perf/arm_pmuv3.c                      | 136 +++-
  include/kvm/arm_pmu.h                         |  93 ++-
  include/linux/perf/arm_pmu.h                  |   8 +
  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 | 112 ++-
  tools/testing/selftests/kvm/lib/find_bit.c    |   2 +
  26 files changed, 1918 insertions(+), 823 deletions(-)

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

base-commit: 4549871118cf616eecdd2d939f78e3b9e1dddc48

#42 lore raw

Hi James, thanks for the review.

Quoted 19 lines · James Clark
Quoted 8 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.

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

Quoted 8 lines

The kernel command line parameter for the driver still exists, but now only defines an upper limit of counters the guest might use rather than taking those counters from the host permanently.

I would appreciate any discussion on whether that parameter should still exist as it's an inconvenient enabling gate on the feature that is no longer required. The question comes down to what, if any, guards we want against a guest monopolizing all counters on a system.

Quoted 4 lines

Hi Colton,

The existence of the parameter makes sense, but can't the default be arm_pmuv3.reserved_host_counters=0 instead of -1 (partition disabled)?

How would you interpret reserved_host_counters=0? Because that would express that the host should have zero counters and the guest gets them all. I remember in early versions of this series last year I did use 0 as the default but reviewers asked for a way to express the scenario of of zero counters for the host.

Quoted 4 lines

It's still a bit fiddly having to do two things to make it work, and there's no documentation about what the defaults or other prerequisites are. IMO it's not even trivial to work out that you need to prefix it with "arm_pmuv3.", which documentation would improve.

It is fiddly, which is why the more I think about it the more I'd rather ditch the kernel cmdline entirely and leave it up to KVM. That's the most flexible approach. There isn't a good way to change the cmdline after boot. Trying to make it writeable via sysfs runs into the problem of when is it safe to apply the change.

I thought it was commonly understood module parameters need to be prefixed by the module name, but I can add better documentation to that effect.

Quoted 24 lines

Testing the whole set I ran into a few issues:

This warn is hit when there is some kind of interaction with sleeping. I tried to bisect it but it only appears on the last commit when the option to enable partitioning is added.

	/*
	 * ARM pmu always has to reprogram the period, so ignore
	 * PERF_EF_RELOAD, see the comment below.
	 */
	if (flags & PERF_EF_RELOAD)
		WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));

Steps to reproduce:

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo perf stat -C 2 -e \
     'branches,branches,branches,branches,branches,branches'
   $ sudo taskset --cpu-list 2 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ sleep 1
Quoted 15 lines
    WARNING: drivers/perf/arm_pmu.c:302 at cpu_pm_pmu_notify+0x278/0x2c0,
CPU#2: swapper/2/0
     Call trace:
    cpu_pm_pmu_notify+0x278/0x2c0 (P)
    notifier_call_chain+0x84/0x1d0
    raw_notifier_call_chain+0x24/0x38
    cpu_pm_exit+0x34/0x68
    acpi_processor_ffh_lpi_enter+0x40/0x78
    acpi_idle_lpi_enter+0x54/0x78
    cpuidle_enter_state+0xb4/0x248
    cpuidle_enter+0x44/0x68
    do_idle+0x21c/0x300
    cpu_startup_entry+0x40/0x50
    secondary_start_kernel+0x120/0x150
    __secondary_switched+0xc0/0xc8

I'll attempt to reproduce that. I've never seen it myself but I've mainly been using QEMU for my testing.

Quoted 5 lines

When running the guest on a single CPU I get different counts for the same event for a single process, although this never happens on a host. I think there might even be some Perf tests which expect them to be the same, and this doesn't depend on whether any events are running on the host or not. Not sure if you ran all the Perf selftests in a guest or not?

I'll investigate but I'm not sure perf is intended to guarantee that. perf stat just runs the event counters but may not write or read them at exactly the same time.

Quoted 4 lines

I'm not sure if the exception level filtering isn't working or there is something wrong with freezing. Doesn't the host PMU driver need to freeze with HPME? I see it's still doing it with PMCR_EL0.E which freezes the guest's counters now doesn't it?

HPME only has any effect while HPMN is programmed to a value different than the number of counters on the system. That is only true in a guest context. As I understand it, that means HPME has no effect anywhere the host driver needs to freeze counters since that happens in host context.

Maybe HPMN stays programmed during interrupt handling though. I'll confirm.

Quoted 29 lines

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo taskset --cpu-list 2 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ perf stat -e branches,branches true
    Performance counter stats for 'true':

              167963      branches

              160925      branches
   $ perf stat -e branches,branches,branches,branches,branches true
    Performance counter stats for 'true':

              164425      branches

              164425      branches

              164425      branches

              157743      branches

              157743      branches
Quoted 2 lines

When running the guest on two CPUs I just get zeros. Do the kvm selftests not catch this? Or is it something to do with my setup:

KVM selftests definitely don't catch it. I'll see if I can reproduce.

Since the problem happens with 2 CPUs I wonder if something is triggering the lazy context swap behavior so the counter values aren't being moved to the 2nd CPU correctly.

Quoted 24 lines

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo taskset --cpu-list 2-3 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ perf stat -e branches,branches true

Performance counter stats for 'true':

0 branches

0 branches

I also noticed I get the "squeezed" warning printed after launching the guest but not using Perf. This comment implies that not using counters makes it a nop:

   	/*
	 * 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 (vcpu->arch.pmu.access != VCPU_PMU_ACCESS_GUEST_OWNED)
		return;
Quoted 3 lines

But maybe linux is touching them in a way that makes them guest owned, even on probe? Or is the guest/host ownership tracking not working, I didn't look too hard.

Maybe. I'll check. The guest is also running its own PMU driver that might trigger guest ownership on initialization since ownership is determined by which registers the guest touches.

Quoted 3 lines

Finally I think there are 3 critical Sashiko comments on this version. If they're false positives, then maybe some comments in the code or commit messages could reassure it.

I've looked at these and concluded they are worth fixing.

Quoted 132 lines

Thanks
James

Quoted 129 lines

v8:

* Rebase on top of v7.1-rc7.

* Implement Oliver Upton's accessor proposal to centralize PMU register access and simplify trap handlers. Instead of one singular accessor, implement as two because the read and write paths are always different anyway.

* Introduce the partitioning flag along with the
kvm_pmu_is_partitioned predicate

* Don't use ifdef for partitioning predicates as that can be handled by has_vhe

* Clean up MDCR_EL2 handling by open-coding use_fgt and hpmn and unconditionally setting RES0 bits.

* Use {read,write}_pmcrcntrn in context swaps

* Put operators on preceeding lines

* Rename hw_cntr_mask to hw_cntr_impl to clarify it tracks the number of counters implemented by hardware

* Use GENMASK_ULL in mask functions returning u64

* warn_once when host events are squeezed out by guest counter allocations.

* Address Sashiko AI Review findings:

- Critical fixes for lazy PMU context swaps (ensuring guest state is loaded on transition to GUEST_OWNED), PMSELR_EL0 trapping to prevent stale selector index, and masking guest PMCR_EL0 writes to prevent host reset.

- High priority fixes for lock safety (disabling IRQs when acquiring perf context lock), disabling guest counters on vCPU put, preserving VHE host profiling in MDCR_EL2, waking halted vCPUs on guest PMU interrupts, masking host configuration leaks, preemption safety in per-CPU accesses, emulating PMCR.N reads, and preventing data races in PMOVSSET_EL0 accesses.

- Medium/Low fixes for user-access fallback safety, VM-wide state modification restrictions, selftests type safety, and cleanup of unused fields and typos.

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 (20):
arm64: cpufeature: Add cpucap for HPMN0
KVM: arm64: Reorganize PMU functions
perf: arm_pmuv3: Generalize counter bitmasks
perf: arm_pmuv3: Check cntr_mask before using pmccntr
perf: arm_pmuv3: Allocate counter indices from high to low
perf: arm_pmuv3: Add method to partition the 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: 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
KVM: arm64: selftests: Relax testing for exceptions when partitioned

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

   arch/arm/include/asm/arm_pmuv3.h              |  18 +
   arch/arm64/include/asm/arm_pmuv3.h            |  12 +-
   arch/arm64/include/asm/kvm_host.h             |  17 +-
   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                          |   2 +
   arch/arm64/kvm/config.c                       |  41 +-
   arch/arm64/kvm/debug.c                        |  30 +-
   arch/arm64/kvm/pmu-direct.c                   | 507 ++++++++++++
   arch/arm64/kvm/pmu-emul.c                     | 684 +----------------
   arch/arm64/kvm/pmu.c                          | 720 ++++++++++++++++++
   arch/arm64/kvm/sys_regs.c                     | 271 +++++--
   arch/arm64/tools/cpucaps                      |   1 +
   arch/arm64/tools/sysreg                       |   6 +-
   drivers/perf/arm_pmuv3.c                      | 136 +++-
   include/kvm/arm_pmu.h                         |  93 ++-
   include/linux/perf/arm_pmu.h                  |   8 +
   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 | 112 ++-
   tools/testing/selftests/kvm/lib/find_bit.c    |   2 +
   26 files changed, 1918 insertions(+), 823 deletions(-)

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

base-commit: 4549871118cf616eecdd2d939f78e3b9e1dddc48

reviewer
#43 lore raw
Quoted 25 lines · Colton Lewis

Hi James, thanks for the review.

Quoted 20 lines · James Clark
Quoted 8 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.

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

Quoted 8 lines

The kernel command line parameter for the driver still exists, but now only defines an upper limit of counters the guest might use rather than taking those counters from the host permanently.

I would appreciate any discussion on whether that parameter should still exist as it's an inconvenient enabling gate on the feature that is no longer required. The question comes down to what, if any, guards we want against a guest monopolizing all counters on a system.

Hi Colton,

Quoted 9 lines
Quoted 2 lines

The existence of the parameter makes sense, but can't the default be arm_pmuv3.reserved_host_counters=0 instead of -1 (partition disabled)?

How would you interpret reserved_host_counters=0? Because that would express that the host should have zero counters and the guest gets them all. I remember in early versions of this series last year I did use 0 as the default but reviewers asked for a way to express the scenario of of zero counters for the host.

I think I would interpret it as it is now. Can't the host still use them until the guest does and then they only get squeezed if there is a collision? That doesn't sound too bad to me. And if a host wants reserved counters they can set a positive number.

Is reserved_host_counters==max_counters equivalent to -1? And there's no overview in the cover letter or documentation about how it interacts with the dynamic guest counter reservations. A bit more detail or a few paragraphs in one place would help users, maybe with examples so they don't have to piece together all the commit messages to work it out.

Quoted 11 lines
Quoted 4 lines

It's still a bit fiddly having to do two things to make it work, and there's no documentation about what the defaults or other prerequisites are. IMO it's not even trivial to work out that you need to prefix it with "arm_pmuv3.", which documentation would improve.

It is fiddly, which is why the more I think about it the more I'd rather ditch the kernel cmdline entirely and leave it up to KVM. That's the most flexible approach. There isn't a good way to change the cmdline after boot. Trying to make it writeable via sysfs runs into the problem of when is it safe to apply the change.

Quoted 5 lines

I thought it was commonly understood module parameters need to be prefixed by the module name, but I can add better documentation to that effect.

People just want it written out fully somewhere so they can copy paste it. Kernel developers might be able to piece it together, but not everyone will.

Quoted 41 lines
Quoted 24 lines

Testing the whole set I ran into a few issues:

This warn is hit when there is some kind of interaction with sleeping. I tried to bisect it but it only appears on the last commit when the option to enable partitioning is added.

    /*
     * ARM pmu always has to reprogram the period, so ignore
     * PERF_EF_RELOAD, see the comment below.
     */
    if (flags & PERF_EF_RELOAD) WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));

Steps to reproduce:

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo perf stat -C 2 -e \
     'branches,branches,branches,branches,branches,branches'
   $ sudo taskset --cpu-list 2 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ sleep 1
Quoted 15 lines
    WARNING: drivers/perf/arm_pmu.c:302 at cpu_pm_pmu_notify+0x278/0x2c0,
CPU#2: swapper/2/0
     Call trace:
    cpu_pm_pmu_notify+0x278/0x2c0 (P)
    notifier_call_chain+0x84/0x1d0
    raw_notifier_call_chain+0x24/0x38
    cpu_pm_exit+0x34/0x68
    acpi_processor_ffh_lpi_enter+0x40/0x78
    acpi_idle_lpi_enter+0x54/0x78
    cpuidle_enter_state+0xb4/0x248
    cpuidle_enter+0x44/0x68
    do_idle+0x21c/0x300
    cpu_startup_entry+0x40/0x50
    secondary_start_kernel+0x120/0x150
    __secondary_switched+0xc0/0xc8
Quoted 3 lines

I'll attempt to reproduce that. I've never seen it myself but I've mainly been using QEMU for my testing.

The newest Arm FVP should have cpuidle enabled if you boot it with ACPI, but I hit this on a Radxa Orion O6 board.

Quoted 11 lines
Quoted 6 lines

When running the guest on a single CPU I get different counts for the same event for a single process, although this never happens on a host. I think there might even be some Perf tests which expect them to be the same, and this doesn't depend on whether any events are running on the host or not. Not sure if you ran all the Perf selftests in a guest or not?

I'll investigate but I'm not sure perf is intended to guarantee that. perf stat just runs the event counters but may not write or read them at exactly the same time.

Is that true? The perf core calls perf_pmu_disable() when a process is scheduled out before reading the count of each event of that process in an inner loop. The perf_pmu_disable() clears PMCR_EL0.E which freezes all of the counters so they can be read out in a consistent state.

It's important that they're all stopped at the same time because counters might be used in metrics as ratios of each other. So I think it's deliberately designed that way and appears to not be working in a guest now.

Quoted 10 lines
Quoted 4 lines

I'm not sure if the exception level filtering isn't working or there is something wrong with freezing. Doesn't the host PMU driver need to freeze with HPME? I see it's still doing it with PMCR_EL0.E which freezes the guest's counters now doesn't it?

HPME only has any effect while HPMN is programmed to a value different than the number of counters on the system. That is only true in a guest context. As I understand it, that means HPME has no effect anywhere the host driver needs to freeze counters since that happens in host context.

Oh yeah that makes sense, I didn't think about that. I'm not sure if my other idea about exception level filtering being responsible makes sense either.

Quoted 33 lines

Maybe HPMN stays programmed during interrupt handling though. I'll confirm.

Quoted 27 lines

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo taskset --cpu-list 2 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ perf stat -e branches,branches true

    Performance counter stats for 'true':

              167963      branches

              160925      branches

   $ perf stat -e branches,branches,branches,branches,branches true

    Performance counter stats for 'true':

              164425      branches

              164425      branches

              164425      branches

              157743      branches

              157743      branches

Quoted 9 lines
Quoted 2 lines

When running the guest on two CPUs I just get zeros. Do the kvm selftests not catch this? Or is it something to do with my setup:

KVM selftests definitely don't catch it. I'll see if I can reproduce.

Since the problem happens with 2 CPUs I wonder if something is triggering the lazy context swap behavior so the counter values aren't being moved to the 2nd CPU correctly.

If that was the case I would have expected it to be intermittent, or you would get a partial count if the second CPU started from 0, but not zeros every where every time. Let me know if I can run any more tests.

Quoted 24 lines
Quoted 23 lines

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo taskset --cpu-list 2-3 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ perf stat -e branches,branches true

   Performance counter stats for 'true':

                   0      branches

                   0      branches

I also noticed I get the "squeezed" warning printed after launching the guest but not using Perf. This comment implies that not using counters makes it a nop:

       /*
     * 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 (vcpu->arch.pmu.access != VCPU_PMU_ACCESS_GUEST_OWNED) return;

Quoted 8 lines
Quoted 3 lines

But maybe linux is touching them in a way that makes them guest owned, even on probe? Or is the guest/host ownership tracking not working, I didn't look too hard.

Maybe. I'll check. The guest is also running its own PMU driver that might trigger guest ownership on initialization since ownership is determined by which registers the guest touches.

It doesn't enable any counters though does it, even if it touches some of the registers to probe? Should that result in squeezing out host counters? Maybe it's more complicated to implement, but right now it seems like there is some complexity without much benefit as 90% of hosts are going to probe the counters just to see what's there but not use them.

Quoted 139 lines
Quoted 3 lines

Finally I think there are 3 critical Sashiko comments on this version. If they're false positives, then maybe some comments in the code or commit messages could reassure it.

I've looked at these and concluded they are worth fixing.

Quoted 133 lines

Thanks
James

Quoted 130 lines

v8:

* Rebase on top of v7.1-rc7.

* Implement Oliver Upton's accessor proposal to centralize PMU register access and simplify trap handlers. Instead of one singular accessor, implement as two because the read and write paths are always different anyway.

* Introduce the partitioning flag along with the
    kvm_pmu_is_partitioned predicate

* Don't use ifdef for partitioning predicates as that can be handled by has_vhe

* Clean up MDCR_EL2 handling by open-coding use_fgt and hpmn and unconditionally setting RES0 bits.

* Use {read,write}_pmcrcntrn in context swaps

* Put operators on preceeding lines

* Rename hw_cntr_mask to hw_cntr_impl to clarify it tracks the number of counters implemented by hardware

* Use GENMASK_ULL in mask functions returning u64

* warn_once when host events are squeezed out by guest counter allocations.

* Address Sashiko AI Review findings:

    - Critical fixes for lazy PMU context swaps (ensuring guest state is loaded on transition to GUEST_OWNED), PMSELR_EL0 trapping to prevent stale selector index, and masking guest PMCR_EL0 writes to prevent host reset.

    - High priority fixes for lock safety (disabling IRQs when acquiring perf context lock), disabling guest counters on vCPU put, preserving VHE host profiling in MDCR_EL2, waking halted vCPUs on guest PMU interrupts, masking host configuration leaks, preemption safety in per-CPU accesses, emulating PMCR.N reads, and preventing data races in PMOVSSET_EL0 accesses.

    - Medium/Low fixes for user-access fallback safety, VM-wide state modification restrictions, selftests type safety, and cleanup of unused fields and typos.

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

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

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

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

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

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

v1:
https://lore.kernel.org/kvm/20250602192702.2125115-1- [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 (20):
    arm64: cpufeature: Add cpucap for HPMN0
    KVM: arm64: Reorganize PMU functions
    perf: arm_pmuv3: Generalize counter bitmasks
    perf: arm_pmuv3: Check cntr_mask before using pmccntr
    perf: arm_pmuv3: Allocate counter indices from high to low
    perf: arm_pmuv3: Add method to partition the 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: 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
    KVM: arm64: selftests: Relax testing for exceptions when partitioned

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

   arch/arm/include/asm/arm_pmuv3.h              |  18 + arch/arm64/include/asm/arm_pmuv3.h            |  12 +- arch/arm64/include/asm/kvm_host.h             |  17 +- 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                          |   2 + arch/arm64/kvm/config.c                       |  41 +- arch/arm64/kvm/debug.c                        |  30 +- arch/arm64/kvm/pmu-direct.c                   | 507 ++++++++++++ arch/arm64/kvm/pmu-emul.c                     | 684 +---------------- arch/arm64/kvm/pmu.c                          | 720 ++++++++++++++
++++
   arch/arm64/kvm/sys_regs.c                     | 271 +++++-- arch/arm64/tools/cpucaps                      |   1 + arch/arm64/tools/sysreg                       |   6 +- drivers/perf/arm_pmuv3.c                      | 136 +++- include/kvm/arm_pmu.h                         |  93 ++- include/linux/perf/arm_pmu.h                  |   8 + 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 | 112 ++- tools/testing/selftests/kvm/lib/find_bit.c    |   2 +

   26 files changed, 1918 insertions(+), 823 deletions(-)

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

base-commit: 4549871118cf616eecdd2d939f78e3b9e1dddc48

#44 lore raw
Quoted 16 lines · James Clark
Quoted 5 lines · Colton Lewis

Hi James, thanks for the review.

Quoted 1 line · James Clark

Hi Colton,

Quoted 8 lines
Quoted 2 lines

The existence of the parameter makes sense, but can't the default be arm_pmuv3.reserved_host_counters=0 instead of -1 (partition disabled)?

How would you interpret reserved_host_counters=0? Because that would express that the host should have zero counters and the guest gets them all. I remember in early versions of this series last year I did use 0 as the default but reviewers asked for a way to express the scenario of of zero counters for the host.

Quoted 4 lines

I think I would interpret it as it is now. Can't the host still use them until the guest does and then they only get squeezed if there is a collision? That doesn't sound too bad to me. And if a host wants reserved counters they can set a positive number.

Yeah that makes sense. I was thinking in an old headspace when it was just a static reservation instead of changing when a guest is loaded.

Quoted 5 lines

Is reserved_host_counters==max_counters equivalent to -1? And there's no overview in the cover letter or documentation about how it interacts with the dynamic guest counter reservations. A bit more detail or a few paragraphs in one place would help users, maybe with examples so they don't have to piece together all the commit messages to work it out.

It's not equivalent since we need a case where the guest only has access to the fixed-purpose counters like the dedicated cycle counter. reserved_host_counters specifies the number of general purpose counters.

I'll document the operation more clearly. In the future please feel free to email me to ask if you are confused about something.

Quoted 17 lines
Quoted 11 lines
Quoted 4 lines

It's still a bit fiddly having to do two things to make it work, and there's no documentation about what the defaults or other prerequisites are. IMO it's not even trivial to work out that you need to prefix it with "arm_pmuv3.", which documentation would improve.

It is fiddly, which is why the more I think about it the more I'd rather ditch the kernel cmdline entirely and leave it up to KVM. That's the most flexible approach. There isn't a good way to change the cmdline after boot. Trying to make it writeable via sysfs runs into the problem of when is it safe to apply the change.

Quoted 3 lines

I thought it was commonly understood module parameters need to be prefixed by the module name, but I can add better documentation to that effect.

Quoted 3 lines

People just want it written out fully somewhere so they can copy paste it. Kernel developers might be able to piece it together, but not everyone will.

This is the KVM mailing list. Kernel developers are the primary audience, but I will make reasonable efforts to include others in the future.

Quoted 41 lines
Quoted 39 lines
Quoted 39 lines

Testing the whole set I ran into a few issues:

This warn is hit when there is some kind of interaction with sleeping. I tried to bisect it but it only appears on the last commit when the option to enable partitioning is added.

    /*
     * ARM pmu always has to reprogram the period, so ignore
     * PERF_EF_RELOAD, see the comment below.
     */
    if (flags & PERF_EF_RELOAD) WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));

Steps to reproduce:

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo perf stat -C 2 -e \
     'branches,branches,branches,branches,branches,branches'
   $ sudo taskset --cpu-list 2 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ sleep 1
    WARNING: drivers/perf/arm_pmu.c:302 at cpu_pm_pmu_notify+0x278/0x2c0,
CPU#2: swapper/2/0
     Call trace:
    cpu_pm_pmu_notify+0x278/0x2c0 (P)
    notifier_call_chain+0x84/0x1d0
    raw_notifier_call_chain+0x24/0x38
    cpu_pm_exit+0x34/0x68
    acpi_processor_ffh_lpi_enter+0x40/0x78
    acpi_idle_lpi_enter+0x54/0x78
    cpuidle_enter_state+0xb4/0x248
    cpuidle_enter+0x44/0x68
    do_idle+0x21c/0x300
    cpu_startup_entry+0x40/0x50
    secondary_start_kernel+0x120/0x150
    __secondary_switched+0xc0/0xc8
Quoted 6 lines
Quoted 2 lines

I'll attempt to reproduce that. I've never seen it myself but I've mainly been using QEMU for my testing.

The newest Arm FVP should have cpuidle enabled if you boot it with ACPI, but I hit this on a Radxa Orion O6 board.

Noted. Thanks.

Quoted 17 lines
Quoted 10 lines
Quoted 6 lines

When running the guest on a single CPU I get different counts for the same event for a single process, although this never happens on a host. I think there might even be some Perf tests which expect them to be the same, and this doesn't depend on whether any events are running on the host or not. Not sure if you ran all the Perf selftests in a guest or not?

I'll investigate but I'm not sure perf is intended to guarantee that. perf stat just runs the event counters but may not write or read them at exactly the same time.

Is that true? The perf core calls perf_pmu_disable() when a process is scheduled out before reading the count of each event of that process in an inner loop. The perf_pmu_disable() clears PMCR_EL0.E which freezes all of the counters so they can be read out in a consistent state.

Quoted 4 lines

It's important that they're all stopped at the same time because counters might be used in metrics as ratios of each other. So I think it's deliberately designed that way and appears to not be working in a guest now.

By default I think perf assumes events can be measured independently, If you want to guarantee events are scheduled together to avoid measurement skew you need to make sure the events are grouped.

The common way to do that is with {} around the event list:

perf stat -e {branches,branches}

Please see if that resolves the issue.

Quoted 11 lines
Quoted 9 lines
Quoted 4 lines

I'm not sure if the exception level filtering isn't working or there is something wrong with freezing. Doesn't the host PMU driver need to freeze with HPME? I see it's still doing it with PMCR_EL0.E which freezes the guest's counters now doesn't it?

HPME only has any effect while HPMN is programmed to a value different than the number of counters on the system. That is only true in a guest context. As I understand it, that means HPME has no effect anywhere the host driver needs to freeze counters since that happens in host context.

Quoted 3 lines

Oh yeah that makes sense, I didn't think about that. I'm not sure if my other idea about exception level filtering being responsible makes sense either.

I'll check that too.

Quoted 38 lines
Quoted 37 lines

Maybe HPMN stays programmed during interrupt handling though. I'll confirm.

Quoted 24 lines

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo taskset --cpu-list 2 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ perf stat -e branches,branches true

    Performance counter stats for 'true':

              167963      branches

              160925      branches

   $ perf stat -e branches,branches,branches,branches,branches true

    Performance counter stats for 'true':

              164425      branches

              164425      branches

              164425      branches

Quoted 7 lines

              157743      branches

              157743      branches

When running the guest on two CPUs I just get zeros. Do the kvm selftests not catch this? Or is it something to do with my setup:

KVM selftests definitely don't catch it. I'll see if I can reproduce.

Quoted 8 lines
Quoted 3 lines

Since the problem happens with 2 CPUs I wonder if something is triggering the lazy context swap behavior so the counter values aren't being moved to the 2nd CPU correctly.

If that was the case I would have expected it to be intermittent, or you would get a partial count if the second CPU started from 0, but not zeros every where every time. Let me know if I can run any more tests.

I'll let you know if I think of something.

Quoted 33 lines
Quoted 24 lines
Quoted 23 lines

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo taskset --cpu-list 2-3 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ perf stat -e branches,branches true

   Performance counter stats for 'true':

                   0      branches

                   0      branches

I also noticed I get the "squeezed" warning printed after launching the guest but not using Perf. This comment implies that not using counters makes it a nop:

       /*
     * 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 (vcpu->arch.pmu.access != VCPU_PMU_ACCESS_GUEST_OWNED) return;

Quoted 7 lines
Quoted 3 lines

But maybe linux is touching them in a way that makes them guest owned, even on probe? Or is the guest/host ownership tracking not working, I didn't look too hard.

Maybe. I'll check. The guest is also running its own PMU driver that might trigger guest ownership on initialization since ownership is determined by which registers the guest touches.

Quoted 5 lines

It doesn't enable any counters though does it, even if it touches some of the registers to probe? Should that result in squeezing out host counters? Maybe it's more complicated to implement, but right now it seems like there is some complexity without much benefit as 90% of hosts are going to probe the counters just to see what's there but not use them.

As far as I'm aware the driver probe shouldn't shouldn't enable any counters or trigger squeezing. I'll narrow the conditions that trigger guest ownership.

My first suspect is the guest driver probe is triggering guest ownership by reading PMCR_EL0, which I agree should be changed to avoid that.

reviewer
#45 lore raw
Quoted 57 lines · Colton Lewis
Quoted 16 lines · James Clark
Quoted 5 lines · Colton Lewis

Hi James, thanks for the review.

Quoted 1 line · James Clark

Hi Colton,

Quoted 8 lines
Quoted 2 lines

The existence of the parameter makes sense, but can't the default be arm_pmuv3.reserved_host_counters=0 instead of -1 (partition disabled)?

How would you interpret reserved_host_counters=0? Because that would express that the host should have zero counters and the guest gets them all. I remember in early versions of this series last year I did use 0 as the default but reviewers asked for a way to express the scenario of of zero counters for the host.

Quoted 4 lines

I think I would interpret it as it is now. Can't the host still use them until the guest does and then they only get squeezed if there is a collision? That doesn't sound too bad to me. And if a host wants reserved counters they can set a positive number.

Yeah that makes sense. I was thinking in an old headspace when it was just a static reservation instead of changing when a guest is loaded.

Quoted 5 lines

Is reserved_host_counters==max_counters equivalent to -1? And there's no overview in the cover letter or documentation about how it interacts with the dynamic guest counter reservations. A bit more detail or a few paragraphs in one place would help users, maybe with examples so they don't have to piece together all the commit messages to work it out.

It's not equivalent since we need a case where the guest only has access to the fixed-purpose counters like the dedicated cycle counter. reserved_host_counters specifies the number of general purpose counters.

I'll document the operation more clearly. In the future please feel free to email me to ask if you are confused about something.

Quoted 14 lines
Quoted 14 lines
Quoted 4 lines

It's still a bit fiddly having to do two things to make it work, and there's no documentation about what the defaults or other prerequisites are. IMO it's not even trivial to work out that you need to prefix it with "arm_pmuv3.", which documentation would improve.

It is fiddly, which is why the more I think about it the more I'd rather ditch the kernel cmdline entirely and leave it up to KVM. That's the most flexible approach. There isn't a good way to change the cmdline after boot. Trying to make it writeable via sysfs runs into the problem of when is it safe to apply the change.

I thought it was commonly understood module parameters need to be prefixed by the module name, but I can add better documentation to that effect.

Quoted 8 lines
Quoted 3 lines

People just want it written out fully somewhere so they can copy paste it. Kernel developers might be able to piece it together, but not everyone will.

This is the KVM mailing list. Kernel developers are the primary audience, but I will make reasonable efforts to include others in the future.

I'm more thinking of the users of this after it gets merged, rather than just for the review.

Quoted 77 lines
Quoted 41 lines
Quoted 39 lines
Quoted 39 lines

Testing the whole set I ran into a few issues:

This warn is hit when there is some kind of interaction with sleeping. I tried to bisect it but it only appears on the last commit when the option to enable partitioning is added.

    /*
     * ARM pmu always has to reprogram the period, so ignore
     * PERF_EF_RELOAD, see the comment below.
     */
    if (flags & PERF_EF_RELOAD) WARN_ON_ONCE(!(hwc->state & PERF_HES_UPTODATE));

Steps to reproduce:

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo perf stat -C 2 -e \
     'branches,branches,branches,branches,branches,branches'
   $ sudo taskset --cpu-list 2 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ sleep 1
    WARNING: drivers/perf/arm_pmu.c:302 at cpu_pm_pmu_notify+0x278/0x2c0,
CPU#2: swapper/2/0
     Call trace:
    cpu_pm_pmu_notify+0x278/0x2c0 (P)
    notifier_call_chain+0x84/0x1d0
    raw_notifier_call_chain+0x24/0x38
    cpu_pm_exit+0x34/0x68
    acpi_processor_ffh_lpi_enter+0x40/0x78
    acpi_idle_lpi_enter+0x54/0x78
    cpuidle_enter_state+0xb4/0x248
    cpuidle_enter+0x44/0x68
    do_idle+0x21c/0x300
    cpu_startup_entry+0x40/0x50
    secondary_start_kernel+0x120/0x150
    __secondary_switched+0xc0/0xc8
Quoted 6 lines
Quoted 2 lines

I'll attempt to reproduce that. I've never seen it myself but I've mainly been using QEMU for my testing.

The newest Arm FVP should have cpuidle enabled if you boot it with ACPI, but I hit this on a Radxa Orion O6 board.

Noted. Thanks.

Quoted 17 lines
Quoted 10 lines
Quoted 6 lines

When running the guest on a single CPU I get different counts for the same event for a single process, although this never happens on a host. I think there might even be some Perf tests which expect them to be the same, and this doesn't depend on whether any events are running on the host or not. Not sure if you ran all the Perf selftests in a guest or not?

I'll investigate but I'm not sure perf is intended to guarantee that. perf stat just runs the event counters but may not write or read them at exactly the same time.

Is that true? The perf core calls perf_pmu_disable() when a process is scheduled out before reading the count of each event of that process in an inner loop. The perf_pmu_disable() clears PMCR_EL0.E which freezes all of the counters so they can be read out in a consistent state.

Quoted 4 lines

It's important that they're all stopped at the same time because counters might be used in metrics as ratios of each other. So I think it's deliberately designed that way and appears to not be working in a guest now.

By default I think perf assumes events can be measured independently, If you want to guarantee events are scheduled together to avoid measurement skew you need to make sure the events are grouped.

Quoted 6 lines

The common way to do that is with {} around the event list:

perf stat -e {branches,branches}

Please see if that resolves the issue.

Groups only change how the events are scheduled, not how the driver starts or stops multiple events running on the same PMU (grouped or ungrouped). In my repro I had less events than counters in HW, so they will always be scheduled at the same time regardless of grouping.

I did notice something extra though, you have to first open some amount of counters, and then open more than that. Then the second time the ones with different counts will be however many were opened first time, as if some state has stuck.

For example if I open two counters then 6, the first two always have different counts the second time:

  $ perf stat -e branches,branches true

Performance counter stats for 'true':

            106129      branches 

            106129      branches 
  $ perf stat -e 
'{branches,branches,branches,branches,branches,branches}' true

Performance counter stats for 'true':

            117013      branches 

            117013      branches 

            110364      branches 

            110364      branches 

            110364      branches 

            110364      branches

After opening 6 again a third time they'll all have the same counts.

Quoted 111 lines
Quoted 11 lines
Quoted 9 lines
Quoted 4 lines

I'm not sure if the exception level filtering isn't working or there is something wrong with freezing. Doesn't the host PMU driver need to freeze with HPME? I see it's still doing it with PMCR_EL0.E which freezes the guest's counters now doesn't it?

HPME only has any effect while HPMN is programmed to a value different than the number of counters on the system. That is only true in a guest context. As I understand it, that means HPME has no effect anywhere the host driver needs to freeze counters since that happens in host context.

Quoted 3 lines

Oh yeah that makes sense, I didn't think about that. I'm not sure if my other idea about exception level filtering being responsible makes sense either.

I'll check that too.

Quoted 38 lines
Quoted 37 lines

Maybe HPMN stays programmed during interrupt handling though. I'll confirm.

Quoted 24 lines

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo taskset --cpu-list 2 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ perf stat -e branches,branches true

    Performance counter stats for 'true':

              167963      branches

              160925      branches

   $ perf stat -e branches,branches,branches,branches,branches true

    Performance counter stats for 'true':

              164425      branches

              164425      branches

              164425      branches

Quoted 7 lines

              157743      branches

              157743      branches

When running the guest on two CPUs I just get zeros. Do the kvm selftests not catch this? Or is it something to do with my setup:

KVM selftests definitely don't catch it. I'll see if I can reproduce.

Quoted 8 lines
Quoted 3 lines

Since the problem happens with 2 CPUs I wonder if something is triggering the lazy context swap behavior so the counter values aren't being moved to the 2nd CPU correctly.

If that was the case I would have expected it to be intermittent, or you would get a partial count if the second CPU started from 0, but not zeros every where every time. Let me know if I can run any more tests.

I'll let you know if I think of something.

Quoted 33 lines
Quoted 24 lines
Quoted 23 lines

Host (arm_pmuv3.reserved_host_counters=0):

   $ sudo taskset --cpu-list 2-3 ./lkvm run --kernel \
     /boot/vmlinux-7.1.0-rc7+ -m 1024 -c 1 --pmu

Guest:

   $ perf stat -e branches,branches true

   Performance counter stats for 'true':

                   0      branches

                   0      branches

I also noticed I get the "squeezed" warning printed after launching the guest but not using Perf. This comment implies that not using counters makes it a nop:

       /*
     * 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 (vcpu->arch.pmu.access != VCPU_PMU_ACCESS_GUEST_OWNED) return;

Quoted 7 lines
Quoted 3 lines

But maybe linux is touching them in a way that makes them guest owned, even on probe? Or is the guest/host ownership tracking not working, I didn't look too hard.

Maybe. I'll check. The guest is also running its own PMU driver that might trigger guest ownership on initialization since ownership is determined by which registers the guest touches.

Quoted 5 lines

It doesn't enable any counters though does it, even if it touches some of the registers to probe? Should that result in squeezing out host counters? Maybe it's more complicated to implement, but right now it seems like there is some complexity without much benefit as 90% of hosts are going to probe the counters just to see what's there but not use them.

As far as I'm aware the driver probe shouldn't shouldn't enable any counters or trigger squeezing. I'll narrow the conditions that trigger guest ownership.

My first suspect is the guest driver probe is triggering guest ownership by reading PMCR_EL0, which I agree should be changed to avoid that.

#46 lore raw
Quoted 76 lines · James Clark
Quoted 27 lines · Colton Lewis
Quoted 17 lines · James Clark
Quoted 10 lines
Quoted 6 lines

When running the guest on a single CPU I get different counts for the same event for a single process, although this never happens on a host. I think there might even be some Perf tests which expect them to be the same, and this doesn't depend on whether any events are running on the host or not. Not sure if you ran all the Perf selftests in a guest or not?

I'll investigate but I'm not sure perf is intended to guarantee that. perf stat just runs the event counters but may not write or read them at exactly the same time.

Is that true? The perf core calls perf_pmu_disable() when a process is scheduled out before reading the count of each event of that process in an inner loop. The perf_pmu_disable() clears PMCR_EL0.E which freezes all of the counters so they can be read out in a consistent state.

Quoted 4 lines

It's important that they're all stopped at the same time because counters might be used in metrics as ratios of each other. So I think it's deliberately designed that way and appears to not be working in a guest now.

By default I think perf assumes events can be measured independently, If you want to guarantee events are scheduled together to avoid measurement skew you need to make sure the events are grouped.

Quoted 5 lines

The common way to do that is with {} around the event list:

perf stat -e {branches,branches}

Please see if that resolves the issue.

Groups only change how the events are scheduled, not how the driver starts or stops multiple events running on the same PMU (grouped or ungrouped). In my repro I had less events than counters in HW, so they will always be scheduled at the same time regardless of grouping.

I did notice something extra though, you have to first open some amount of counters, and then open more than that. Then the second time the ones with different counts will be however many were opened first time, as if some state has stuck.

For example if I open two counters then 6, the first two always have different counts the second time:

    $ perf stat -e branches,branches true

Performance counter stats for 'true':

106129 branches

106129 branches

$ perf stat -e
'{branches,branches,branches,branches,branches,branches}' true

    Performance counter stats for 'true':

              117013      branches

              117013      branches

              110364      branches

              110364      branches

              110364      branches

              110364      branches
Quoted 1 line

After opening 6 again a third time they'll all have the same counts.

Interesting. Thanks. To clarify, is this happening in the host or VM?

reviewer
#47 lore raw
Quoted 73 lines · Colton Lewis
Quoted 70 lines · James Clark
Quoted 27 lines · Colton Lewis
Quoted 17 lines · James Clark
Quoted 10 lines
Quoted 6 lines

When running the guest on a single CPU I get different counts for the same event for a single process, although this never happens on a host. I think there might even be some Perf tests which expect them to be the same, and this doesn't depend on whether any events are running on the host or not. Not sure if you ran all the Perf selftests in a guest or not?

I'll investigate but I'm not sure perf is intended to guarantee that. perf stat just runs the event counters but may not write or read them at exactly the same time.

Is that true? The perf core calls perf_pmu_disable() when a process is scheduled out before reading the count of each event of that process in an inner loop. The perf_pmu_disable() clears PMCR_EL0.E which freezes all of the counters so they can be read out in a consistent state.

Quoted 4 lines

It's important that they're all stopped at the same time because counters might be used in metrics as ratios of each other. So I think it's deliberately designed that way and appears to not be working in a guest now.

By default I think perf assumes events can be measured independently, If you want to guarantee events are scheduled together to avoid measurement skew you need to make sure the events are grouped.

Quoted 5 lines

The common way to do that is with {} around the event list:

perf stat -e {branches,branches}

Please see if that resolves the issue.

Groups only change how the events are scheduled, not how the driver starts or stops multiple events running on the same PMU (grouped or ungrouped). In my repro I had less events than counters in HW, so they will always be scheduled at the same time regardless of grouping.

I did notice something extra though, you have to first open some amount of counters, and then open more than that. Then the second time the ones with different counts will be however many were opened first time, as if some state has stuck.

For example if I open two counters then 6, the first two always have different counts the second time:

    $ perf stat -e branches,branches true

    Performance counter stats for 'true':

              106129      branches

              106129      branches

    $ perf stat -e
'{branches,branches,branches,branches,branches,branches}' true

    Performance counter stats for 'true':

              117013      branches

              117013      branches

              110364      branches

              110364      branches

Quoted 8 lines
Quoted 6 lines

              110364      branches

              110364      branches

After opening 6 again a third time they'll all have the same counts.

Interesting. Thanks. To clarify, is this happening in the host or VM?

This is in the VM