ARM64 PMU Partitioning
Subsystems (7)
v6 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.
IMPORTANT: This iteration does not yet implement the dynamic counter reservation approach suggested by Will Deacon in January [3]. I am working on it, but wanted to send this version first to keep momentum going and ensure I've addressed all issues besides that.
v6:
* Rebase onto v6.19-rc7
* Drop the reorganization patches I had previously included from Sean and Anish and rework without them.
* Inline FGT programming for easier readability
* Change register access path to drop simultaneous writing of the virtual and physical registers and write only where the canonical state should reside. The PMU register fast path behaves like a simple accessor now, relying on generic helpers when needed.
* Related to the previous, drop several patches modifying sys_regs.c and incorporate PMOVS and PMEVTYPER into the fast path instead.
* Move the register fast path call to kvm_hyp_handle_sysreg_vhe since this feature depends on VHE mode
* Remove the heavyweight access checks from the fast path that had the potential to inject an undefined exception. For what checks are necessary, just return false and let the normal path handle injecting exceptions
* Remove the legacy support for writeable PMCR.N. VMMs must use the vCPU attribute to change the number of counters.
* Simplify kvm_pmu_hpmn by relying on kvm_vcpu_on_unsupported_cpu and moving HPMN validation of nr_pmu_counters to the ioctl boundary when it is set.
* Disable preemption during context swap
* Simplify iteration of counters to context swap by iterating a bitmask
* Clear PMOVS flags during load to avoid the possibility of generating a spurious interrupt when writing PMINTEN or PMCNTEN
* Make kvm_pmu_apply_event_filter() hyp safe
* Cleanly separate interrupt handling so the host driver clears the overflow flags for the host counters only and KVM handles clearing the guest counter flags.
* Ensure the guest PMU state is on hardware before checking hardware for the purposes of determining if an overflow should be injected into the guest.
* Naming and commit message improvements
* Change uAPI to vCPU device attribute selected when other PMU attributes are selected.
* Remove some checks for exceptions when accessing invalid counter indices with the Partitioned PMU. Hardware does not guarantee them so the Partitioned PMU can't either.
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
[3] https://lore.kernel.org/kvmarm/aWjlfl85vSd6sMwT@willie-the-truck/
Colton Lewis (18):
arm64: cpufeature: Add cpucap for HPMN0
KVM: arm64: Reorganize PMU functions
perf: arm_pmuv3: Introduce method to partition the PMU
perf: arm_pmuv3: Generalize counter bitmasks
perf: arm_pmuv3: Keep out of guest counter partition
KVM: arm64: Set up FGT for Partitioned PMU
KVM: arm64: Define access helpers for PMUSERENR and PMSELR
KVM: arm64: Write fast path PMU register handlers
KVM: arm64: Setup MDCR_EL2 to handle a partitioned PMU
KVM: arm64: Context swap Partitioned PMU guest registers
KVM: arm64: Enforce PMU event filter at vcpu_load()
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 | 28 + 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 | 8 + arch/arm64/kvm/Makefile | 2 +- arch/arm64/kvm/arm.c | 2 + arch/arm64/kvm/config.c | 41 +- arch/arm64/kvm/debug.c | 31 +- arch/arm64/kvm/hyp/vhe/switch.c | 240 ++++++ arch/arm64/kvm/pmu-direct.c | 439 +++++++++++ arch/arm64/kvm/pmu-emul.c | 674 +--------------- arch/arm64/kvm/pmu.c | 717 ++++++++++++++++++ arch/arm64/kvm/sys_regs.c | 9 +- arch/arm64/tools/cpucaps | 1 + arch/arm64/tools/sysreg | 6 +- drivers/perf/arm_pmuv3.c | 149 +++- include/kvm/arm_pmu.h | 126 +++ include/linux/perf/arm_pmu.h | 1 + include/linux/perf/arm_pmuv3.h | 14 +- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../selftests/kvm/arm64/vpmu_counter_access.c | 112 ++- tools/testing/selftests/kvm/lib/find_bit.c | 1 + 24 files changed, 1889 insertions(+), 750 deletions(-) create mode 100644 arch/arm64/kvm/pmu-direct.c create mode 100644 tools/testing/selftests/kvm/lib/find_bit.c
base-commit: 63804fed149a6750ffd28610c5c1c98cce6bd377 --
2.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 1/19 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 | 8 ++++++++ arch/arm64/kvm/sys_regs.c | 3 ++- arch/arm64/tools/cpucaps | 1 + arch/arm64/tools/sysreg | 6 +++--- 4 files changed, 14 insertions(+), 4 deletions(-)
arch/arm64/kernel/cpufeature.c +8 -0
@@ -555,6 +555,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 ),
@@ -2950,6 +2951,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
@@ -3229,7 +3229,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
@@ -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
@@ -1666,9 +1666,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.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 2/19 KVM: arm64: Reorganize PMU includes
In reply to Colton LewisFrom: 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
@@ -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
@@ -1414,25 +1414,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
@@ -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
@@ -9,9 +9,19 @@#include <linux/perf_event.h> #include <linux/perf/arm_pmuv3.h> +#include <linux/perf/arm_pmu.h> #define KVM_ARMV8_PMU_MAX_COUNTERS 32 +#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 */
@@ -66,6 +76,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 );
@@ -159,6 +172,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.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 3/19 KVM: arm64: Reorganize PMU functions
In reply to Colton LewisA 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 | 672 +------------------------------------ arch/arm64/kvm/pmu.c | 676 ++++++++++++++++++++++++++++++++++++++ include/kvm/arm_pmu.h | 7 + 3 files changed, 684 insertions(+), 671 deletions(-)
arch/arm64/kvm/pmu-emul.c +1 -671
@@ -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 (sizeof (*entry),GFP_KERNEL ); -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,393 +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 (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)) -return -ENXIO ; - - 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; - -kvm_for_each_vcpu (i, vcpu, kvm) { -if (!kvm_arm_pmu_irq_initialized (vcpu)) -continue ; - -if (irq_is_ppi (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 (irq) ||irq_is_spi (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 +676 -0
687 lines not on this page · raw message
include/kvm/arm_pmu.h +7 -0
@@ -53,13 +53,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);
@@ -132,6 +135,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.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 4/19 perf: arm_pmuv3: Introduce method to partition the PMU
In reply to Colton LewisFor PMUv3, the register field MDCR_EL2.HPMN partitiones 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 module parameter reserved_host_counters to reserve a number of counters for the host. This number is set at boot because the perf subsystem assumes the number of counters will not change after the PMU is probed.
Introduce the function armv8pmu_partition() to modify the PMU driver's cntr_mask of available counters to exclude the counters being reserved for the guest and record reserved_guest_counters as 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 | 78 +++++++++++++++++++++++++++++- include/kvm/arm_pmu.h | 8 +++ include/linux/perf/arm_pmu.h | 1 + 7 files changed, 117 insertions(+), 3 deletions(-) create mode 100644 arch/arm64/kvm/pmu-direct.c
arch/arm/include/asm/arm_pmuv3.h +4 -0
@@ -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
@@ -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
@@ -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
@@ -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 +76 -2
110 lines not on this page · raw message
include/kvm/arm_pmu.h +8 -0
@@ -47,7 +47,10 @@ struct arm_pmu_entry {struct arm_pmu *arm_pmu; }; +extern int armv8pmu_max_guest_counters; +bool kvm_supports_guest_pmuv3 (void ); +bool has_host_pmu_partition_support (void );#define kvm_arm_pmu_irq_initialized (v) ((v)->arch.pmu.irq_num >= VGIC_NR_SGIS)u64 kvm_pmu_get_counter_value (struct kvm_vcpu *vcpu,u64 select_idx);void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val);
@@ -117,6 +120,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 +1 -0
@@ -129,6 +129,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.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH v6 04/19] perf: arm_pmuv3: Introduce method to partition the PMU
Quoted 155 lines · Colton Lewis
For PMUv3, the register field MDCR_EL2.HPMN partitiones 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 module parameter
reserved_host_countersto reserve a number of counters for the host. This number is set at boot because the perf subsystem assumes the number of counters will not change after the PMU is probed.Introduce the function
armv8pmu_partition()to modify the PMU driver'scntr_maskof available counters to exclude the counters being reserved for the guest and recordreserved_guest_countersas 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.
Signed-off-by Colton Lewis <[email protected]>--- 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 | 78 +++++++++++++++++++++++++++++- include/kvm/arm_pmu.h | 8 +++ include/linux/perf/arm_pmu.h | 1 + 7 files changed, 117 insertions(+), 3 deletions(-)create mode 100644
arch/arm64/kvm/pmu-direct.carch/arm/include/asm/arm_pmuv3.h +4
-0@@ -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@@ -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@@ -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.oarch/arm64/kvm/pmu-direct.c new file +22
-0@@ -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 +28
-0@@ -42,6 +42,13 @@#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED +static int reserved_host_counters__read_mostly =-1 ; +int armv8pmu_max_guest_counters =-1 ; + +module_param (reserved_host_counters, int,0 ); +MODULE_PARM_DESC (reserved_host_counters, +"PMU Partition: -1 = No partition; +N = Reserve N counters for the host" ); +/* * ARMv8 Architectural defined events, not all of these may * be supported on any given implementation. Unsupported events will @@ -532,6 +539,11 @@ static void armv8pmu_pmcr_write(u64 val)write_pmcr (val); } +static u64 armv8pmu_pmcr_n_read (void ) +{ +return FIELD_GET (ARMV8_PMU_PMCR_N ,armv8pmu_pmcr_read ()); +} +static int armv8pmu_has_overflowed (u64 pmovsr) {return !!(pmovsr &ARMV8_PMU_OVERFLOWED_MASK );@@ -1309,6 +1321,61 @@ struct armv8pmu_probe_info {bool present; }; +/** +* armv8pmu_reservation_is_valid() - Determine if reservation is allowed +* @host_counters: Number of host counters to reserve +* +* Determine if the number of host counters in the argument is an +* allowed reservation, 0 to NR_COUNTERS inclusive. +* +* Return: True if reservation allowed, false otherwise +*/ +static bool armv8pmu_reservation_is_valid (int host_counters) +{ +return host_counters >=0 && + host_counters <=armv8pmu_pmcr_n_read (); +} + +/**
drivers/perf/arm_pmuv3.c struct armv8pmu_probe_info { · line 1349
Quoted 10 lines
drivers/perf/arm_pmuv3.c +10
-0+* armv8pmu_partition() - Partition the PMU +* @pmu: Pointer to pmu being partitioned +* @host_counters: Number of host counters to reserve +* +* Partition the given PMU by taking a number of host counters to +* reserve and, if it is a valid reservation, recording the +* corresponding HPMN value in the max_guest_counters field of the PMU and +* clearing the guest-reserved counters from the counter mask. +* +* Return: 0 on success, -ERROR otherwise
Hi Colton,
Couple of minor nits. But this error return value isn't used by the caller.
Quoted 6 lines
drivers/perf/arm_pmuv3.c +6
-0+*/ +static int armv8pmu_partition (struct arm_pmu *pmu,int host_counters) +{ +u8 nr_counters; +u8 hpmn; +
drivers/perf/arm_pmuv3.c struct armv8pmu_probe_info { · line 1365
Quoted 10 lines
drivers/perf/arm_pmuv3.c +10
-0+if (!armv8pmu_reservation_is_valid (host_counters)) { +pr_err ("PMU partition reservation of %d host counters is not valid" , host_counters); +return -EINVAL ; + } + + nr_counters =armv8pmu_pmcr_n_read (); + hpmn = nr_counters - host_counters; + + pmu->max_guest_counters = hpmn; + armv8pmu_max_guest_counters = hpmn;
And this could be more like 'bool armv8pmu_partitioned'. PMUs will have different numbers of counters so it's a bit misleading to have one global, and the actual value isn't used either.
Quoted 82 lines
drivers/perf/arm_pmuv3.c +22
-2+ +bitmap_clear (pmu->cntr_mask,0 , hpmn); +bitmap_set (pmu->cntr_mask, hpmn, host_counters); +clear_bit (ARMV8_PMU_CYCLE_IDX , pmu->cntr_mask); + +if (pmuv3_has_icntr ()) +clear_bit (ARMV8_PMU_INSTR_IDX , pmu->cntr_mask); + +pr_info ("Partitioned PMU with %d host counters -> %u guest counters" , host_counters, hpmn); + +return 0 ; +} +static void __armv8pmu_probe_pmu (void *info) {struct armv8pmu_probe_info *probe = info;@@ -1323,10 +1390,10 @@ static void __armv8pmu_probe_pmu(void *info) cpu_pmu->pmuver = pmuver; probe->present = true; + cpu_pmu->max_guest_counters =-1 ;/* Read the nb of CNTx counters supported from PMNC */ -bitmap_set (cpu_pmu->cntr_mask, -0 ,FIELD_GET (ARMV8_PMU_PMCR_N ,armv8pmu_pmcr_read ())); +bitmap_set (cpu_pmu->cntr_mask,0 ,armv8pmu_pmcr_n_read ());/* Add the CPU cycles counter */ set_bit (ARMV8_PMU_CYCLE_IDX , cpu_pmu->cntr_mask);@@ -1335,6 +1402,13 @@ static void __armv8pmu_probe_pmu(void *info)if (pmuv3_has_icntr ())set_bit (ARMV8_PMU_INSTR_IDX , cpu_pmu->cntr_mask); +if (reserved_host_counters >=0 ) { +if (has_host_pmu_partition_support ()) +armv8pmu_partition (cpu_pmu, reserved_host_counters); +else +pr_err ("PMU partition is not supported" ); + } + pmceid[0 ] = pmceid_raw[0 ] =read_pmceid0 (); pmceid[1 ] = pmceid_raw[1 ] =read_pmceid1 ();include/kvm/arm_pmu.h +8
-0@@ -47,7 +47,10 @@ struct arm_pmu_entry {struct arm_pmu *arm_pmu; }; +extern int armv8pmu_max_guest_counters; +bool kvm_supports_guest_pmuv3 (void ); +bool has_host_pmu_partition_support (void );#define kvm_arm_pmu_irq_initialized (v) ((v)->arch.pmu.irq_num >= VGIC_NR_SGIS)u64 kvm_pmu_get_counter_value (struct kvm_vcpu *vcpu,u64 select_idx);void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val);@@ -117,6 +120,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 +1
-0@@ -129,6 +129,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))
Re: [PATCH v6 04/19] perf: arm_pmuv3: Introduce method to partition the PMU
Quoted 174 lines · James Clark
Quoted 166 lines · Colton Lewis
For PMUv3, the register field MDCR_EL2.HPMN partitiones 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 module parameter
reserved_host_countersto reserve a number of counters for the host. This number is set at boot because the perf subsystem assumes the number of counters will not change after the PMU is probed.Introduce the function
armv8pmu_partition()to modify the PMU driver'scntr_maskof available counters to exclude the counters being reserved for the guest and recordreserved_guest_countersas 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.
Signed-off-by Colton Lewis <[email protected]>--- 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 | 78 +++++++++++++++++++++++++++++- include/kvm/arm_pmu.h | 8 +++ include/linux/perf/arm_pmu.h | 1 + 7 files changed, 117 insertions(+), 3 deletions(-)create mode 100644
arch/arm64/kvm/pmu-direct.carch/arm/include/asm/arm_pmuv3.h +4
-0@@ -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@@ -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@@ -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.oarch/arm64/kvm/pmu-direct.c new file +12
-0@@ -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
+ */arch/arm64/kvm/pmu-direct.c new file +5
-0+bool has_host_pmu_partition_support (void ) +{ +return has_vhe () && +system_supports_pmuv3 (); +}drivers/perf/arm_pmuv3.c +7
-0@@ -42,6 +42,13 @@#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED +static int reserved_host_counters__read_mostly =-1 ; +int armv8pmu_max_guest_counters =-1 ; + +module_param (reserved_host_counters, int,0 ); +MODULE_PARM_DESC (reserved_host_counters, +"PMU Partition: -1 = No partition; +N = Reserve N counters for the host"); +/* * ARMv8 Architectural defined events, not all of these may * be supported on any given implementation. Unsupported events will
drivers/perf/arm_pmuv3.c +28
-0@@ -532,6 +539,11 @@ static void armv8pmu_pmcr_write(u64 val)write_pmcr (val); } +static u64 armv8pmu_pmcr_n_read (void ) +{ +return FIELD_GET (ARMV8_PMU_PMCR_N ,armv8pmu_pmcr_read ()); +} +static int armv8pmu_has_overflowed (u64 pmovsr) {return !!(pmovsr &ARMV8_PMU_OVERFLOWED_MASK );@@ -1309,6 +1321,61 @@ struct armv8pmu_probe_info {bool present; }; +/** +* armv8pmu_reservation_is_valid() - Determine if reservation is allowed +* @host_counters: Number of host counters to reserve +* +* Determine if the number of host counters in the argument is an +* allowed reservation, 0 to NR_COUNTERS inclusive. +* +* Return: True if reservation allowed, false otherwise +*/ +static bool armv8pmu_reservation_is_valid (int host_counters) +{ +return host_counters >=0 && + host_counters <=armv8pmu_pmcr_n_read (); +} + +/** +* armv8pmu_partition() - Partition the PMU +* @pmu: Pointer to pmu being partitioned +* @host_counters: Number of host counters to reserve +* +* Partition the given PMU by taking a number of host counters to +* reserve and, if it is a valid reservation, recording the +* corresponding HPMN value in the max_guest_counters field of the PMU Quoted 4 lines
and
+ * clearing the guest-reserved counters from the counter mask.
+ *
+ * Return: 0 on success,-ERRORotherwiseHi Colton,
Quoted 1 line
Couple of minor nits. But this error return value isn't used by the caller.
Fair point. I don't think the caller can do anything with it, so I'll make the function void (if it still exists in the same form with the dynamic reservation approach)
Quoted 13 lines
Quoted 13 lines
+ */
drivers/perf/arm_pmuv3.c +7
-0+static int armv8pmu_partition (struct arm_pmu *pmu,int host_counters) +{ +u8 nr_counters; +u8 hpmn; + +if (!armv8pmu_reservation_is_valid (host_counters)) { + pr_err("PMU partition reservation of %d host counters is not valid",
host_counters);drivers/perf/arm_pmuv3.c +4
-0+return -EINVAL ; + } + + nr_counters =armv8pmu_pmcr_n_read ();
Quoted 8 lines
drivers/perf/arm_pmuv3.c struct armv8pmu_probe_info { · line 1365
Quoted 4 lines
drivers/perf/arm_pmuv3.c +4
-0+ hpmn = nr_counters - host_counters; + + pmu->max_guest_counters = hpmn; + armv8pmu_max_guest_counters = hpmn;And this could be more like 'bool
armv8pmu_partitioned'. PMUs will have different numbers of counters so it's a bit misleading to have one global, and the actual value isn't used either.
I can do that.
Quoted 85 lines
Quoted 85 lines
drivers/perf/arm_pmuv3.c +9
-0+ +bitmap_clear (pmu->cntr_mask,0 , hpmn); +bitmap_set (pmu->cntr_mask, hpmn, host_counters); +clear_bit (ARMV8_PMU_CYCLE_IDX , pmu->cntr_mask); + +if (pmuv3_has_icntr ()) +clear_bit (ARMV8_PMU_INSTR_IDX , pmu->cntr_mask); + + pr_info("Partitioned PMU with %d host counters -> %u guest counters",
host_counters, hpmn);drivers/perf/arm_pmuv3.c +13
-2+ +return 0 ; +} +static void __armv8pmu_probe_pmu (void *info) {struct armv8pmu_probe_info *probe = info;@@ -1323,10 +1390,10 @@ static void __armv8pmu_probe_pmu(void *info) cpu_pmu->pmuver = pmuver; probe->present = true; + cpu_pmu->max_guest_counters =-1 ;/* Read the nb of CNTx counters supported from PMNC */ -bitmap_set (cpu_pmu->cntr_mask, -0 ,FIELD_GET (ARMV8_PMU_PMCR_N ,armv8pmu_pmcr_read ())); +bitmap_set (cpu_pmu->cntr_mask,0 ,armv8pmu_pmcr_n_read ());/* Add the CPU cycles counter */ set_bit (ARMV8_PMU_CYCLE_IDX , cpu_pmu->cntr_mask);@@ -1335,6 +1402,13 @@ static void __armv8pmu_probe_pmu(void *info)if (pmuv3_has_icntr ())set_bit (ARMV8_PMU_INSTR_IDX , cpu_pmu->cntr_mask); +if (reserved_host_counters >=0 ) { +if (has_host_pmu_partition_support ()) +armv8pmu_partition (cpu_pmu, reserved_host_counters); +else +pr_err ("PMU partition is not supported" ); + } + pmceid[0 ] = pmceid_raw[0 ] =read_pmceid0 (); pmceid[1 ] = pmceid_raw[1 ] =read_pmceid1 ();include/kvm/arm_pmu.h +3
-0@@ -47,7 +47,10 @@ struct arm_pmu_entry {struct arm_pmu *arm_pmu; }; +extern int armv8pmu_max_guest_counters; +bool kvm_supports_guest_pmuv3 (void ); +bool has_host_pmu_partition_support (void );#define kvm_arm_pmu_irq_initialized (v) ((v)->arch.pmu.irq_num >= VGIC_NR_SGIS) u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx);void kvm_pmu_set_counter_value(struct
kvm_vcpu*vcpu, u64select_idx,
u64 val);include/kvm/arm_pmu.h +5
-0@@ -117,6 +120,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 +1
-0@@ -129,6 +129,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))
Re: [PATCH v6 04/19] perf: arm_pmuv3: Introduce method to partition the PMU
In reply to Colton LewisQuoted 178 lines · Colton Lewis
For PMUv3, the register field MDCR_EL2.HPMN partitiones 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 module parameter
reserved_host_countersto reserve a number of counters for the host. This number is set at boot because the perf subsystem assumes the number of counters will not change after the PMU is probed.Introduce the function
armv8pmu_partition()to modify the PMU driver'scntr_maskof available counters to exclude the counters being reserved for the guest and recordreserved_guest_countersas 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.
Signed-off-by Colton Lewis <[email protected]>--- 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 | 78 +++++++++++++++++++++++++++++- include/kvm/arm_pmu.h | 8 +++ include/linux/perf/arm_pmu.h | 1 + 7 files changed, 117 insertions(+), 3 deletions(-)create mode 100644
arch/arm64/kvm/pmu-direct.carch/arm/include/asm/arm_pmuv3.h +4
-0@@ -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@@ -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@@ -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.oarch/arm64/kvm/pmu-direct.c new file +22
-0@@ -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 +51
-0@@ -42,6 +42,13 @@#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED +static int reserved_host_counters__read_mostly =-1 ; +int armv8pmu_max_guest_counters =-1 ; + +module_param (reserved_host_counters, int,0 ); +MODULE_PARM_DESC (reserved_host_counters, +"PMU Partition: -1 = No partition; +N = Reserve N counters for the host" ); +/* * ARMv8 Architectural defined events, not all of these may * be supported on any given implementation. Unsupported events will @@ -532,6 +539,11 @@ static void armv8pmu_pmcr_write(u64 val)write_pmcr (val); } +static u64 armv8pmu_pmcr_n_read (void ) +{ +return FIELD_GET (ARMV8_PMU_PMCR_N ,armv8pmu_pmcr_read ()); +} +static int armv8pmu_has_overflowed (u64 pmovsr) {return !!(pmovsr &ARMV8_PMU_OVERFLOWED_MASK );@@ -1309,6 +1321,61 @@ struct armv8pmu_probe_info {bool present; }; +/** +* armv8pmu_reservation_is_valid() - Determine if reservation is allowed +* @host_counters: Number of host counters to reserve +* +* Determine if the number of host counters in the argument is an +* allowed reservation, 0 to NR_COUNTERS inclusive. +* +* Return: True if reservation allowed, false otherwise +*/ +static bool armv8pmu_reservation_is_valid (int host_counters) +{ +return host_counters >=0 && + host_counters <=armv8pmu_pmcr_n_read (); +} + +/** +* armv8pmu_partition() - Partition the PMU +* @pmu: Pointer to pmu being partitioned +* @host_counters: Number of host counters to reserve +* +* Partition the given PMU by taking a number of host counters to +* reserve and, if it is a valid reservation, recording the +* corresponding HPMN value in the max_guest_counters field of the PMU and +* clearing the guest-reserved counters from the counter mask. +* +* Return: 0 on success, -ERROR otherwise +*/ +static int armv8pmu_partition (struct arm_pmu *pmu,int host_counters) +{ +u8 nr_counters; +u8 hpmn; + +if (!armv8pmu_reservation_is_valid (host_counters)) { +pr_err ("PMU partition reservation of %d host counters is not valid" , host_counters); +return -EINVAL ; + } + + nr_counters =armv8pmu_pmcr_n_read (); + hpmn = nr_counters - host_counters;
drivers/perf/arm_pmuv3.c struct armv8pmu_probe_info { · line 1372
Quoted 10 lines
drivers/perf/arm_pmuv3.c +10
-0+ + pmu->max_guest_counters = hpmn; + armv8pmu_max_guest_counters = hpmn; + +bitmap_clear (pmu->cntr_mask,0 , hpmn); +bitmap_set (pmu->cntr_mask, hpmn, host_counters); +clear_bit (ARMV8_PMU_CYCLE_IDX , pmu->cntr_mask); + +if (pmuv3_has_icntr ()) +clear_bit (ARMV8_PMU_INSTR_IDX , pmu->cntr_mask);
We take the fixed instruction counter away from the host here but then guest never gets it because AA64DFR1 is RAZ. Probably doesn't need to be a blocker to expose the instruction counter, but worth noting that using this feature results in losing a counter completely.
There's a comment above kvm_pmu_guest_counter_mask() that suggests the instruction counter is available for guests, which is why I was looking here. I think "Compute the bitmask that selects the guest-reserved counters ... These are the counters in 0..HPMN and the cycle and instruction counters." shouldn't include "instruction counters".
Re: [PATCH v6 04/19] perf: arm_pmuv3: Introduce method to partition the PMU
Quoted 201 lines · James Clark
Quoted 184 lines · Colton Lewis
For PMUv3, the register field MDCR_EL2.HPMN partitiones 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 module parameter
reserved_host_countersto reserve a number of counters for the host. This number is set at boot because the perf subsystem assumes the number of counters will not change after the PMU is probed.Introduce the function
armv8pmu_partition()to modify the PMU driver'scntr_maskof available counters to exclude the counters being reserved for the guest and recordreserved_guest_countersas 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.
Signed-off-by Colton Lewis <[email protected]>--- 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 | 78 +++++++++++++++++++++++++++++- include/kvm/arm_pmu.h | 8 +++ include/linux/perf/arm_pmu.h | 1 + 7 files changed, 117 insertions(+), 3 deletions(-)create mode 100644
arch/arm64/kvm/pmu-direct.carch/arm/include/asm/arm_pmuv3.h +4
-0@@ -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@@ -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@@ -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.oarch/arm64/kvm/pmu-direct.c new file +12
-0@@ -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
+ */arch/arm64/kvm/pmu-direct.c new file +5
-0+bool has_host_pmu_partition_support (void ) +{ +return has_vhe () && +system_supports_pmuv3 (); +}drivers/perf/arm_pmuv3.c +7
-0@@ -42,6 +42,13 @@#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_ACCESS 0xEC#define ARMV8_THUNDER_PERFCTR_L1I_CACHE_PREF_MISS 0xED +static int reserved_host_counters__read_mostly =-1 ; +int armv8pmu_max_guest_counters =-1 ; + +module_param (reserved_host_counters, int,0 ); +MODULE_PARM_DESC (reserved_host_counters, +"PMU Partition: -1 = No partition; +N = Reserve N counters for the host"); +/* * ARMv8 Architectural defined events, not all of these may * be supported on any given implementation. Unsupported events will
drivers/perf/arm_pmuv3.c +28
-0@@ -532,6 +539,11 @@ static void armv8pmu_pmcr_write(u64 val)write_pmcr (val); } +static u64 armv8pmu_pmcr_n_read (void ) +{ +return FIELD_GET (ARMV8_PMU_PMCR_N ,armv8pmu_pmcr_read ()); +} +static int armv8pmu_has_overflowed (u64 pmovsr) {return !!(pmovsr &ARMV8_PMU_OVERFLOWED_MASK );@@ -1309,6 +1321,61 @@ struct armv8pmu_probe_info {bool present; }; +/** +* armv8pmu_reservation_is_valid() - Determine if reservation is allowed +* @host_counters: Number of host counters to reserve +* +* Determine if the number of host counters in the argument is an +* allowed reservation, 0 to NR_COUNTERS inclusive. +* +* Return: True if reservation allowed, false otherwise +*/ +static bool armv8pmu_reservation_is_valid (int host_counters) +{ +return host_counters >=0 && + host_counters <=armv8pmu_pmcr_n_read (); +} + +/** +* armv8pmu_partition() - Partition the PMU +* @pmu: Pointer to pmu being partitioned +* @host_counters: Number of host counters to reserve +* +* Partition the given PMU by taking a number of host counters to +* reserve and, if it is a valid reservation, recording the +* corresponding HPMN value in the max_guest_counters field of the PMU and
+ * clearing the guest-reserved counters from the counter mask.
+ *
+ * Return: 0 on success,-ERRORotherwise
+ */drivers/perf/arm_pmuv3.c +7
-0+static int armv8pmu_partition (struct arm_pmu *pmu,int host_counters) +{ +u8 nr_counters; +u8 hpmn; + +if (!armv8pmu_reservation_is_valid (host_counters)) { + pr_err("PMU partition reservation of %d host counters is not valid",
host_counters);drivers/perf/arm_pmuv3.c +5
-0+return -EINVAL ; + } + + nr_counters =armv8pmu_pmcr_n_read (); + hpmn = nr_counters - host_counters;drivers/perf/arm_pmuv3.c struct armv8pmu_probe_info { · line 1372
Quoted 10 lines
drivers/perf/arm_pmuv3.c +10
-0+ + pmu->max_guest_counters = hpmn; + armv8pmu_max_guest_counters = hpmn; + +bitmap_clear (pmu->cntr_mask,0 , hpmn); +bitmap_set (pmu->cntr_mask, hpmn, host_counters); +clear_bit (ARMV8_PMU_CYCLE_IDX , pmu->cntr_mask); + +if (pmuv3_has_icntr ()) +clear_bit (ARMV8_PMU_INSTR_IDX , pmu->cntr_mask);We take the fixed instruction counter away from the host here but then guest never gets it because AA64DFR1 is RAZ. Probably doesn't need to be a blocker to expose the instruction counter, but worth noting that using this feature results in losing a counter completely.
Quoted 5 lines
There's a comment above
kvm_pmu_guest_counter_mask()that suggests the instruction counter is available for guests, which is why I was looking here. I think "Compute the bitmask that selects the guest-reserved counters ... These are the counters in 0..HPMN and the cycle and instruction counters." shouldn't include "instruction counters".
Good point. Early iterations intended to expose the instruction counter to guests but that was dropped. I will drop it here too.
PATCH v6 5/19 perf: arm_pmuv3: Generalize counter bitmasks
In reply to Colton LewisThe 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 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-)
drivers/perf/arm_pmuv3.c +2 -2
@@ -546,7 +546,7 @@ static u64 armv8pmu_pmcr_n_read(void)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)
@@ -782,7 +782,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
@@ -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
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 6/19 perf: arm_pmuv3: Keep out of guest counter partition
In reply to Colton LewisIf the PMU is partitioned, keep the driver out of the guest counter partition and only use the host counter partition.
Define some functions that determine whether the PMU is partitioned and construct mutually exclusive bitmaps for testing which partition a particular counter is in. Note that despite their separate position in the bitmap, the cycle and instruction counters are always in the guest partition.
--- arch/arm/include/asm/arm_pmuv3.h | 18 +++++++ arch/arm64/kvm/pmu-direct.c | 86 ++++++++++++++++++++++++++++++++ drivers/perf/arm_pmuv3.c | 40 +++++++++++++-- include/kvm/arm_pmu.h | 24 +++++++++ 4 files changed, 164 insertions(+), 4 deletions(-)
arch/arm/include/asm/arm_pmuv3.h +18 -0
@@ -231,6 +231,24 @@ static inline bool kvm_set_pmuserenr(u64 val) }static inline void kvm_vcpu_pmu_resync_el0 (void ) {} +static inline void kvm_pmu_host_counters_enable (void ) {} +static inline void kvm_pmu_host_counters_disable (void ) {} + +static inline bool kvm_pmu_is_partitioned (struct arm_pmu *pmu) +{ +return false; +} + +static inline u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu) +{ +return ~0 ; +} + +static inline u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu) +{ +return ~0 ; +} +/* PMU Version in DFR Register */ #define ARMV8_PMU_DFR_VER_NI 0
arch/arm64/kvm/pmu-direct.c +86 -0
95 lines not on this page · raw message
drivers/perf/arm_pmuv3.c +36 -4
@@ -871,6 +871,9 @@ static void armv8pmu_start(struct arm_pmu *cpu_pmu)brbe_enable (cpu_pmu);/* Enable all counters */ +if (kvm_pmu_is_partitioned (cpu_pmu)) +kvm_pmu_host_counters_enable (); +armv8pmu_pmcr_write (armv8pmu_pmcr_read () |ARMV8_PMU_PMCR_E ); }
@@ -882,6 +885,9 @@ static void armv8pmu_stop(struct arm_pmu *cpu_pmu)brbe_disable ();/* Disable all counters */ +if (kvm_pmu_is_partitioned (cpu_pmu)) +kvm_pmu_host_counters_disable (); +armv8pmu_pmcr_write (armv8pmu_pmcr_read () & ~ARMV8_PMU_PMCR_E ); }
@@ -1028,6 +1034,12 @@ static bool armv8pmu_can_use_pmccntr(struct pmu_hw_events *cpuc,if (cpu_pmu->has_smt)return false; +/* +* If partitioned at all, pmccntr belongs to the guest. +*/ +if (kvm_pmu_is_partitioned (cpu_pmu)) +return false; +return true; }
@@ -1054,6 +1066,7 @@ static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,* may not know how to handle it. */ if ((evtype ==ARMV8_PMUV3_PERFCTR_INST_RETIRED ) && + !kvm_pmu_is_partitioned (cpu_pmu) && !armv8pmu_event_get_threshold (&event->attr) &&test_bit (ARMV8_PMU_INSTR_IDX , cpu_pmu->cntr_mask) && !armv8pmu_event_want_user_access (event)) {
@@ -1065,7 +1078,7 @@ static int armv8pmu_get_event_idx(struct pmu_hw_events *cpuc,* Otherwise use events counters */ if (armv8pmu_event_is_chained (event)) -return armv8pmu_get_chain_idx (cpuc, cpu_pmu); +return armv8pmu_get_chain_idx (cpuc, cpu_pmu);else return armv8pmu_get_single_idx (cpuc, cpu_pmu); }
@@ -1177,6 +1190,14 @@ static int armv8pmu_set_event_filter(struct hw_perf_event *event,return 0 ; } +static void armv8pmu_reset_host_counters (struct arm_pmu *cpu_pmu) +{ +int idx; + +for_each_set_bit (idx, cpu_pmu->cntr_mask,ARMV8_PMU_MAX_GENERAL_COUNTERS ) +armv8pmu_write_evcntr (idx,0 ); +} +static void armv8pmu_reset (void *info) {struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;
@@ -1184,6 +1205,9 @@ static void armv8pmu_reset(void *info)bitmap_to_arr64 (&mask, cpu_pmu->cntr_mask,ARMPMU_MAX_HWEVENTS ); +if (kvm_pmu_is_partitioned (cpu_pmu)) + mask &=kvm_pmu_host_counter_mask (cpu_pmu); +/* The counter and interrupt enable registers are unknown at reset. */ armv8pmu_disable_counter (mask);armv8pmu_disable_intens (mask);
@@ -1196,11 +1220,19 @@ static void armv8pmu_reset(void *info)brbe_invalidate (); } + pmcr =ARMV8_PMU_PMCR_LC ; +/* -* Initialize & Reset PMNC. Request overflow interrupt for -* 64 bit cycle counter but cheat in armv8pmu_write_counter(). +* Initialize & Reset PMNC. Request overflow interrupt for 64 +* bit cycle counter but cheat in armv8pmu_write_counter(). +* +* When partitioned, there is no single bit to reset only the +* host counters. so reset them individually. */ - pmcr =ARMV8_PMU_PMCR_P |ARMV8_PMU_PMCR_C |ARMV8_PMU_PMCR_LC ; +if (kvm_pmu_is_partitioned (cpu_pmu)) +armv8pmu_reset_host_counters (cpu_pmu); +else + pmcr =ARMV8_PMU_PMCR_P |ARMV8_PMU_PMCR_C ;/* Enable long event counter support where available */ if (armv8pmu_has_long_event (cpu_pmu))
include/kvm/arm_pmu.h +24 -0
@@ -92,6 +92,12 @@ void kvm_vcpu_pmu_resync_el0(void);#define kvm_vcpu_has_pmu (vcpu) \ (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3)) +bool kvm_pmu_is_partitioned (struct arm_pmu *pmu); +u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu); +u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu); +void kvm_pmu_host_counters_enable (void ); +void kvm_pmu_host_counters_disable (void ); +/* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure
@@ -228,6 +234,24 @@ static inline bool kvm_pmu_counter_is_hyp(struct kvm_vcpu *vcpu, unsigned int idstatic inline void kvm_pmu_nested_transition (struct kvm_vcpu *vcpu) {} +static inline bool kvm_pmu_is_partitioned (void *pmu) +{ +return false; +} + +static inline u64 kvm_pmu_host_counter_mask (void *pmu) +{ +return ~0 ; +} + +static inline u64 kvm_pmu_guest_counter_mask (void *pmu) +{ +return ~0 ; +} + +static inline void kvm_pmu_host_counters_enable (void ) {} +static inline void kvm_pmu_host_counters_disable (void ) {} +#endif #endif
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH v6 06/19] perf: arm_pmuv3: Keep out of guest counter partition
Quoted 115 lines · Colton Lewis
If the PMU is partitioned, keep the driver out of the guest counter partition and only use the host counter partition.
Define some functions that determine whether the PMU is partitioned and construct mutually exclusive bitmaps for testing which partition a particular counter is in. Note that despite their separate position in the bitmap, the cycle and instruction counters are always in the guest partition.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm/include/asm/arm_pmuv3.h | 18 +++++++ arch/arm64/kvm/pmu-direct.c | 86 ++++++++++++++++++++++++++++++++ drivers/perf/arm_pmuv3.c | 40 +++++++++++++-- include/kvm/arm_pmu.h | 24 +++++++++ 4 files changed, 164 insertions(+), 4 deletions(-)arch/arm/include/asm/arm_pmuv3.h +18
-0@@ -231,6 +231,24 @@ static inline bool kvm_set_pmuserenr(u64 val) }static inline void kvm_vcpu_pmu_resync_el0 (void ) {} +static inline void kvm_pmu_host_counters_enable (void ) {} +static inline void kvm_pmu_host_counters_disable (void ) {} + +static inline bool kvm_pmu_is_partitioned (struct arm_pmu *pmu) +{ +return false; +} + +static inline u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu) +{ +return ~0 ; +} + +static inline u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu) +{ +return ~0 ; +} +/* PMU Version in DFR Register */ #define ARMV8_PMU_DFR_VER_NI 0arch/arm64/kvm/pmu-direct.c +24
-0@@ -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,87 @@ bool has_host_pmu_partition_support(void)return has_vhe () &&system_supports_pmuv3 (); } + +/** +* kvm_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 kvm_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_host_counter_mask() - Compute bitmask of host-reserved counters
+ * @pmu: Pointer toarm_pmustruct
+ *
+ * Compute the bitmask that selects the host-reserved counters in the
+ * {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers. These are the counters
+ * in HPMN..N
+ *
+ * Return: Bitmask
+ */arch/arm64/kvm/pmu-direct.c +12
-0+u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu) +{ +u8 nr_counters = *host_data_ptr (nr_event_counters); + +if (!kvm_pmu_is_partitioned (pmu)) +return ARMV8_PMU_CNT_MASK_ALL ; + +return GENMASK (nr_counters -1 , pmu->max_guest_counters); +} + +/** +* kvm_pmu_guest_counter_mask() - Compute bitmask of guest-reserved counters
+ * @pmu: Pointer toarm_pmustruct
+ *
+ * Compute the bitmask that selects the guest-reserved counters in the
+ * {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers. These are the counters
+ * in 0..HPMN and the cycle and instruction counters.
+ *
+ * Return: Bitmask
+ */
arch/arm64/kvm/pmu-direct.c bool has_host_pmu_partition_support(void) · line 79
Quoted 5 lines
arch/arm64/kvm/pmu-direct.c +5
-0+u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu) +{ +return ARMV8_PMU_CNT_MASK_C &GENMASK (pmu->max_guest_counters -1 ,0 ); +} +
I introduced a mistake here before sending. & should be | at least. Letting the list know in case someone wants to try running my series.
u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu)
{
if (kvm_pmu_is_partitioned(pmu))
return ARMV8_PMU_CNT_MASK_C | GENMASK(pmu->max_guest_counters - 1, 0);
return 0;
}Quoted 182 lines
arch/arm64/kvm/pmu-direct.c +29
-0+/** +* kvm_pmu_host_counters_enable() - Enable host-reserved counters +* +* When partitioned the enable bit for host-reserved counters is +* MDCR_EL2.HPME instead of the typical PMCR_EL0.E, which now +* exclusively controls the guest-reserved counters. Enable that bit. +*/ +void kvm_pmu_host_counters_enable (void ) +{ +u64 mdcr =read_sysreg (mdcr_el2); + + mdcr |=MDCR_EL2_HPME ; +write_sysreg (mdcr, mdcr_el2); +} + +/** +* kvm_pmu_host_counters_disable() - Disable host-reserved counters +* +* When partitioned the disable bit for host-reserved counters is +* MDCR_EL2.HPME instead of the typical PMCR_EL0.E, which now +* exclusively controls the guest-reserved counters. Disable that bit. +*/ +void kvm_pmu_host_counters_disable (void ) +{ +u64 mdcr =read_sysreg (mdcr_el2); + + mdcr &= ~MDCR_EL2_HPME ; +write_sysreg (mdcr, mdcr_el2); +}drivers/perf/arm_pmuv3.c +13
-0@@ -871,6 +871,9 @@ static void armv8pmu_start(struct arm_pmu *cpu_pmu)brbe_enable (cpu_pmu);/* Enable all counters */ +if (kvm_pmu_is_partitioned (cpu_pmu)) +kvm_pmu_host_counters_enable (); +armv8pmu_pmcr_write (armv8pmu_pmcr_read () |ARMV8_PMU_PMCR_E ); }@@ -882,6 +885,9 @@ static void armv8pmu_stop(struct arm_pmu *cpu_pmu)brbe_disable ();/* Disable all counters */ +if (kvm_pmu_is_partitioned (cpu_pmu)) +kvm_pmu_host_counters_disable (); +armv8pmu_pmcr_write (armv8pmu_pmcr_read () & ~ARMV8_PMU_PMCR_E ); }@@ -1028,6 +1034,12 @@ static bool armv8pmu_can_use_pmccntr(struct pmu_hw_events *cpuc, if (cpu_pmu->has_smt)return false; +/* +* If partitioned at all, pmccntr belongs to the guest. +*/ +if (kvm_pmu_is_partitioned (cpu_pmu)) +return false; +return true; }@@ -1054,6 +1066,7 @@ static int armv8pmu_get_event_idx(structpmu_hw_events *cpuc, * may not know how to handle it. */ if ((evtype == ARMV8_PMUV3_PERFCTR_INST_RETIRED) && + !kvm_pmu_is_partitioned(cpu_pmu) && !armv8pmu_event_get_threshold(&event->attr) && test_bit(ARMV8_PMU_INSTR_IDX, cpu_pmu->cntr_mask) &&!armv8pmu_event_want_user_access(event)) {drivers/perf/arm_pmuv3.c +1
-1@@ -1065,7 +1078,7 @@ static int armv8pmu_get_event_idx(structpmu_hw_events *cpuc, * Otherwise use events counters */ if (armv8pmu_event_is_chained (event)) -return armv8pmu_get_chain_idx (cpuc, cpu_pmu); +return armv8pmu_get_chain_idx (cpuc, cpu_pmu);else return armv8pmu_get_single_idx (cpuc, cpu_pmu);}drivers/perf/arm_pmuv3.c +8
-0@@ -1177,6 +1190,14 @@ static int armv8pmu_set_event_filter(structhw_perf_event *event, return 0; } +static void armv8pmu_reset_host_counters (struct arm_pmu *cpu_pmu) +{ +int idx; + +for_each_set_bit (idx, cpu_pmu->cntr_mask,ARMV8_PMU_MAX_GENERAL_COUNTERS ) +armv8pmu_write_evcntr (idx,0 ); +} +static void armv8pmu_reset (void *info){ struct arm_pmu *cpu_pmu = (struct arm_pmu *)info;drivers/perf/arm_pmuv3.c +14
-3@@ -1184,6 +1205,9 @@ static void armv8pmu_reset(void *info)bitmap_to_arr64 (&mask, cpu_pmu->cntr_mask,ARMPMU_MAX_HWEVENTS ); +if (kvm_pmu_is_partitioned (cpu_pmu)) + mask &=kvm_pmu_host_counter_mask (cpu_pmu); +/* The counter and interrupt enable registers are unknown at reset. */ armv8pmu_disable_counter (mask);armv8pmu_disable_intens (mask);@@ -1196,11 +1220,19 @@ static void armv8pmu_reset(void *info)brbe_invalidate (); } + pmcr =ARMV8_PMU_PMCR_LC ; +/* -* Initialize & Reset PMNC. Request overflow interrupt for -* 64 bit cycle counter but cheat in armv8pmu_write_counter(). +* Initialize & Reset PMNC. Request overflow interrupt for 64 +* bit cycle counter but cheat in armv8pmu_write_counter(). +* +* When partitioned, there is no single bit to reset only the +* host counters. so reset them individually. */ - pmcr =ARMV8_PMU_PMCR_P |ARMV8_PMU_PMCR_C |ARMV8_PMU_PMCR_LC ; +if (kvm_pmu_is_partitioned (cpu_pmu)) +armv8pmu_reset_host_counters (cpu_pmu); +else + pmcr =ARMV8_PMU_PMCR_P |ARMV8_PMU_PMCR_C ;/* Enable long event counter support where available */ if (armv8pmu_has_long_event (cpu_pmu))include/kvm/arm_pmu.h +6
-0@@ -92,6 +92,12 @@ void kvm_vcpu_pmu_resync_el0(void);#define kvm_vcpu_has_pmu (vcpu) \ (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3)) +bool kvm_pmu_is_partitioned (struct arm_pmu *pmu); +u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu); +u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu); +void kvm_pmu_host_counters_enable (void ); +void kvm_pmu_host_counters_disable (void ); +/* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure
include/kvm/arm_pmu.h +0
-0@@ -228,6 +234,24 @@ static inline bool kvm_pmu_counter_is_hyp(struct
kvm_vcpu*vcpu, unsigned int idstatic inline void
kvm_pmu_nested_transition(struct kvm_vcpu *vcpu){}include/kvm/arm_pmu.h +18
-1+static inline bool kvm_pmu_is_partitioned (void *pmu) +{ +return false; +} + +static inline u64 kvm_pmu_host_counter_mask (void *pmu) +{ +return ~0 ; +} + +static inline u64 kvm_pmu_guest_counter_mask (void *pmu) +{ +return ~0 ; +} + +static inline void kvm_pmu_host_counters_enable (void ) {} +static inline void kvm_pmu_host_counters_disable (void ) {} +#endif #endif --2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH v6 06/19] perf: arm_pmuv3: Keep out of guest counter partition
In reply to Colton LewisQuoted 106 lines · Colton Lewis
If the PMU is partitioned, keep the driver out of the guest counter partition and only use the host counter partition.
Define some functions that determine whether the PMU is partitioned and construct mutually exclusive bitmaps for testing which partition a particular counter is in. Note that despite their separate position in the bitmap, the cycle and instruction counters are always in the guest partition.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm/include/asm/arm_pmuv3.h | 18 +++++++ arch/arm64/kvm/pmu-direct.c | 86 ++++++++++++++++++++++++++++++++ drivers/perf/arm_pmuv3.c | 40 +++++++++++++-- include/kvm/arm_pmu.h | 24 +++++++++ 4 files changed, 164 insertions(+), 4 deletions(-)arch/arm/include/asm/arm_pmuv3.h +18
-0@@ -231,6 +231,24 @@ static inline bool kvm_set_pmuserenr(u64 val) }static inline void kvm_vcpu_pmu_resync_el0 (void ) {} +static inline void kvm_pmu_host_counters_enable (void ) {} +static inline void kvm_pmu_host_counters_disable (void ) {} + +static inline bool kvm_pmu_is_partitioned (struct arm_pmu *pmu) +{ +return false; +} + +static inline u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu) +{ +return ~0 ; +} + +static inline u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu) +{ +return ~0 ; +} +/* PMU Version in DFR Register */ #define ARMV8_PMU_DFR_VER_NI 0arch/arm64/kvm/pmu-direct.c +45
-0@@ -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,87 @@ bool has_host_pmu_partition_support(void)return has_vhe () &&system_supports_pmuv3 (); } + +/** +* kvm_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 kvm_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_host_counter_mask() - Compute bitmask of host-reserved counters +* @pmu: Pointer to arm_pmu struct +* +* Compute the bitmask that selects the host-reserved counters in the +* {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers. These are the counters +* in HPMN..N +* +* Return: Bitmask +*/ +u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu) +{ +u8 nr_counters = *host_data_ptr (nr_event_counters); + +if (!kvm_pmu_is_partitioned (pmu)) +return ARMV8_PMU_CNT_MASK_ALL ; + +return GENMASK (nr_counters -1 , pmu->max_guest_counters); +} + +/** +* kvm_pmu_guest_counter_mask() - Compute bitmask of guest-reserved counters +* @pmu: Pointer to arm_pmu struct
arch/arm64/kvm/pmu-direct.c bool has_host_pmu_partition_support(void) · line 77
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+* +* Compute the bitmask that selects the guest-reserved counters in the +* {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers. These are the counters +* in 0..HPMN and the cycle and instruction counters. +* +* Return: Bitmask +*/ +u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu) +{ +return ARMV8_PMU_CNT_MASK_C &GENMASK (pmu->max_guest_counters -1 ,0 );
This should be an | instead of an & otherwise it's always zero. None of the passed through counters count anything with it like this, although the cycle counter always worked even with this issue.
I'm not sure if the selftests that you added catch this? I didn't try running them but seems like checking for non zero counter values is a very easy thing to test.
Re: [PATCH v6 06/19] perf: arm_pmuv3: Keep out of guest counter partition
Quoted 124 lines · James Clark
Quoted 115 lines · Colton Lewis
If the PMU is partitioned, keep the driver out of the guest counter partition and only use the host counter partition.
Define some functions that determine whether the PMU is partitioned and construct mutually exclusive bitmaps for testing which partition a particular counter is in. Note that despite their separate position in the bitmap, the cycle and instruction counters are always in the guest partition.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm/include/asm/arm_pmuv3.h | 18 +++++++ arch/arm64/kvm/pmu-direct.c | 86 ++++++++++++++++++++++++++++++++ drivers/perf/arm_pmuv3.c | 40 +++++++++++++-- include/kvm/arm_pmu.h | 24 +++++++++ 4 files changed, 164 insertions(+), 4 deletions(-)arch/arm/include/asm/arm_pmuv3.h +18
-0@@ -231,6 +231,24 @@ static inline bool kvm_set_pmuserenr(u64 val) }static inline void kvm_vcpu_pmu_resync_el0 (void ) {} +static inline void kvm_pmu_host_counters_enable (void ) {} +static inline void kvm_pmu_host_counters_disable (void ) {} + +static inline bool kvm_pmu_is_partitioned (struct arm_pmu *pmu) +{ +return false; +} + +static inline u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu) +{ +return ~0 ; +} + +static inline u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu) +{ +return ~0 ; +} +/* PMU Version in DFR Register */ #define ARMV8_PMU_DFR_VER_NI 0arch/arm64/kvm/pmu-direct.c +24
-0@@ -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,87 @@ bool has_host_pmu_partition_support(void)return has_vhe () &&system_supports_pmuv3 (); } + +/** +* kvm_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 kvm_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_host_counter_mask() - Compute bitmask of host-reserved counters
+ * @pmu: Pointer toarm_pmustruct
+ *
+ * Compute the bitmask that selects the host-reserved counters in the
+ * {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers. These are the counters
+ * in HPMN..N
+ *
+ * Return: Bitmask
+ */arch/arm64/kvm/pmu-direct.c +12
-0+u64 kvm_pmu_host_counter_mask (struct arm_pmu *pmu) +{ +u8 nr_counters = *host_data_ptr (nr_event_counters); + +if (!kvm_pmu_is_partitioned (pmu)) +return ARMV8_PMU_CNT_MASK_ALL ; + +return GENMASK (nr_counters -1 , pmu->max_guest_counters); +} + +/** +* kvm_pmu_guest_counter_mask() - Compute bitmask of guest-reserved counters
+ * @pmu: Pointer toarm_pmustruct
+ *
+ * Compute the bitmask that selects the guest-reserved counters in the
+ * {PMCNTEN,PMINTEN,PMOVS}{SET,CLR} registers. These are the counters
+ * in 0..HPMN and the cycle and instruction counters.
+ *
+ * Return: Bitmask
+ */arch/arm64/kvm/pmu-direct.c bool has_host_pmu_partition_support(void) · line 77
Quoted 3 lines
arch/arm64/kvm/pmu-direct.c +3
-0+u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu) +{ +return ARMV8_PMU_CNT_MASK_C &GENMASK (pmu->max_guest_counters -1 ,0 );This should be an | instead of an & otherwise it's always zero. None of the passed through counters count anything with it like this, although the cycle counter always worked even with this issue.
Quoted 3 lines
I'm not sure if the selftests that you added catch this? I didn't try running them but seems like checking for non zero counter values is a very easy thing to test.
I caught this myself and called it out here:
https://lore.kernel.org/kvmarm/[email protected]/
The selftest didn't catch this, so you're right it's a good idea to check.
PATCH v6 7/19 KVM: arm64: Set up FGT for Partitioned PMU
In reply to Colton LewisIn 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.
NOTE: This patch temporarily forces kvm_vcpu_pmu_is_partitioned() to be false to prevent partial feature activation for easier debugging.
--- arch/arm64/kvm/config.c | 41 ++++++++++++++++++++++++++++++++++--- arch/arm64/kvm/pmu-direct.c | 33 +++++++++++++++++++++++++++++ include/kvm/arm_pmu.h | 23 +++++++++++++++++++++ 3 files changed, 94 insertions(+), 3 deletions(-)
arch/arm64/kvm/config.c +38 -3
@@ -1489,12 +1489,47 @@ static void __compute_hfgwtr(struct kvm_vcpu *vcpu) *vcpu_fgt (vcpu,HFGWTR_EL2 ) |=HFGWTR_EL2_TCR_EL1 ; } +static void __compute_hdfgrtr (struct kvm_vcpu *vcpu) +{ +__compute_fgt (vcpu,HDFGRTR_EL2 ); + + *vcpu_fgt (vcpu,HDFGRTR_EL2 ) |= +HDFGRTR_EL2_PMOVS + |HDFGRTR_EL2_PMCCFILTR_EL0 + | HDFGRTR_EL2_PMEVTYPERn_EL0 + | HDFGRTR_EL2_PMCEIDn_EL0 + |HDFGRTR_EL2_PMMIR_EL1 ; +} +static void __compute_hdfgwtr (struct kvm_vcpu *vcpu) {__compute_fgt (vcpu,HDFGWTR_EL2 );if (is_hyp_ctxt (vcpu)) *vcpu_fgt (vcpu,HDFGWTR_EL2 ) |=HDFGWTR_EL2_MDSCR_EL1 ; + + *vcpu_fgt (vcpu,HDFGWTR_EL2 ) |= +HDFGWTR_EL2_PMOVS + |HDFGWTR_EL2_PMCCFILTR_EL0 + | HDFGWTR_EL2_PMEVTYPERn_EL0; +} + +static void __compute_hdfgrtr2 (struct kvm_vcpu *vcpu) +{ +__compute_fgt (vcpu,HDFGRTR2_EL2 ); + + *vcpu_fgt (vcpu,HDFGRTR2_EL2 ) &= + ~(HDFGRTR2_EL2_nPMICFILTR_EL0 + | HDFGRTR2_EL2_nPMICNTR_EL0); +} + +static void __compute_hdfgwtr2 (struct kvm_vcpu *vcpu) +{ +__compute_fgt (vcpu,HDFGWTR2_EL2 ); + + *vcpu_fgt (vcpu,HDFGWTR2_EL2 ) &= + ~(HDFGWTR2_EL2_nPMICFILTR_EL0 + | HDFGWTR2_EL2_nPMICNTR_EL0); }void kvm_vcpu_load_fgt (struct kvm_vcpu *vcpu)
@@ -1505,7 +1540,7 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)__compute_fgt (vcpu,HFGRTR_EL2 );__compute_hfgwtr (vcpu);__compute_fgt (vcpu,HFGITR_EL2 ); -__compute_fgt (vcpu,HDFGRTR_EL2 ); +__compute_hdfgrtr (vcpu);__compute_hdfgwtr (vcpu);__compute_fgt (vcpu,HAFGRTR_EL2 );
@@ -1515,6 +1550,6 @@ void kvm_vcpu_load_fgt(struct kvm_vcpu *vcpu)__compute_fgt (vcpu,HFGRTR2_EL2 );__compute_fgt (vcpu,HFGWTR2_EL2 );__compute_fgt (vcpu,HFGITR2_EL2 ); -__compute_fgt (vcpu,HDFGRTR2_EL2 ); -__compute_fgt (vcpu,HDFGWTR2_EL2 ); +__compute_hdfgrtr2 (vcpu); +__compute_hdfgwtr2 (vcpu); }
arch/arm64/kvm/pmu-direct.c +33 -0
@@ -42,6 +42,39 @@ bool kvm_pmu_is_partitioned(struct arm_pmu *pmu) pmu->max_guest_counters <= *host_data_ptr (nr_event_counters); } +/** +* kvm_vcpu_pmu_is_partitioned() - Determine if given VCPU has a partitioned PMU +* @vcpu: Pointer to kvm_vcpu struct +* +* Determine if given VCPU has a partitioned PMU by extracting that +* field and passing it to :c:func:`kvm_pmu_is_partitioned` +* +* Return: True if the VCPU PMU is partitioned, false otherwise +*/ +bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) +{ +return kvm_pmu_is_partitioned (vcpu->kvm->arch.arm_pmu) && + false; +} + +/** +* kvm_vcpu_pmu_use_fgt() - Determine if we can use FGT +* @vcpu: Pointer to struct kvm_vcpu +* +* Determine if we can use FGT for direct access to registers. We can +* if capabilities permit the number of guest counters requested. +* +* Return: True if we can use FGT, false otherwise +*/ +bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu) +{ +u8 hpmn = vcpu->kvm->arch.nr_pmu_counters; + +return kvm_vcpu_pmu_is_partitioned (vcpu) && +cpus_have_final_cap (ARM64_HAS_FGT ) && + (hpmn !=0 ||cpus_have_final_cap (ARM64_HAS_HPMN0 )); +} +/** * kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved counters * @pmu: Pointer to arm_pmu struct
include/kvm/arm_pmu.h +23 -0
@@ -98,6 +98,21 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);void kvm_pmu_host_counters_enable (void );void kvm_pmu_host_counters_disable (void ); +#if !defined(__KVM_NVHE_HYPERVISOR__) +bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu); +bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu); +#else +static inline bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) +{ +return false; +} + +static inline bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu) +{ +return false; +} +#endif +/* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure
@@ -137,6 +152,14 @@ static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, {return 0 ; } +static inline bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) +{ +return false; +} +static inline bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu) +{ +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,
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 8/19 KVM: arm64: Define access helpers for PMUSERENR and PMSELR
In reply to Colton LewisIn order to ensure register permission checks will have consistent results whether or not the PMU is partitioned, define some access helpers for PMUSERENR and PMSELR that always return the canonical value for those registers, whether it lives in a physical or virtual register.
--- arch/arm64/kvm/pmu.c | 16 ++++++++++++++++ arch/arm64/kvm/sys_regs.c | 6 +++--- include/kvm/arm_pmu.h | 12 ++++++++++++ 3 files changed, 31 insertions(+), 3 deletions(-)
arch/arm64/kvm/pmu.c +16 -0
@@ -885,3 +885,19 @@ u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)return u64_replace_bits (pmcr, n,ARMV8_PMU_PMCR_N ); } + +u64 kvm_vcpu_read_pmselr (struct kvm_vcpu *vcpu) +{ +if (kvm_vcpu_pmu_is_partitioned (vcpu)) +return read_sysreg (pmselr_el0); +else +return __vcpu_sys_reg (vcpu,PMSELR_EL0 ); +} + +u64 kvm_vcpu_read_pmuserenr (struct kvm_vcpu *vcpu) +{ +if (kvm_vcpu_pmu_is_partitioned (vcpu)) +return read_sysreg (pmuserenr_el0); +else +return __vcpu_sys_reg (vcpu,PMUSERENR_EL0 ); +}
arch/arm64/kvm/sys_regs.c +3 -3
@@ -987,7 +987,7 @@ static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)static bool check_pmu_access_disabled (struct kvm_vcpu *vcpu,u64 flags) { -u64 reg =__vcpu_sys_reg (vcpu,PMUSERENR_EL0 ); +u64 reg =kvm_vcpu_read_pmuserenr (vcpu);bool enabled = (reg & flags) ||vcpu_mode_priv (vcpu);if (!enabled)
@@ -1141,7 +1141,7 @@ static bool access_pmu_evcntr(struct kvm_vcpu *vcpu,return false; idx =SYS_FIELD_GET (PMSELR_EL0 ,SEL , -__vcpu_sys_reg (vcpu,PMSELR_EL0 )); +kvm_vcpu_read_pmselr (vcpu)); }else if (r->Op2 ==0 ) {/* PMCCNTR_EL0 */ if (pmu_access_cycle_counter_el0_disabled (vcpu))
@@ -1191,7 +1191,7 @@ static bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p,if (r->CRn ==9 && r->CRm ==13 && r->Op2 ==1 ) {/* PMXEVTYPER_EL0 */ - idx =SYS_FIELD_GET (PMSELR_EL0 ,SEL ,__vcpu_sys_reg (vcpu,PMSELR_EL0 )); + idx =SYS_FIELD_GET (PMSELR_EL0 ,SEL ,kvm_vcpu_read_pmselr (vcpu)); reg =PMEVTYPER0_EL0 + idx; }else if (r->CRn ==14 && (r->CRm &12 ) ==12 ) { idx = ((r->CRm &3 ) <<3 ) | (r->Op2 &7 );
include/kvm/arm_pmu.h +12 -0
@@ -130,6 +130,8 @@ int kvm_arm_set_default_pmu(struct kvm *kvm);u8 kvm_arm_pmu_get_max_counters (struct kvm *kvm);u64 kvm_vcpu_read_pmcr (struct kvm_vcpu *vcpu); +u64 kvm_vcpu_read_pmselr (struct kvm_vcpu *vcpu); +u64 kvm_vcpu_read_pmuserenr (struct kvm_vcpu *vcpu);bool kvm_pmu_counter_is_hyp (struct kvm_vcpu *vcpu,unsigned int idx);void kvm_pmu_nested_transition (struct kvm_vcpu *vcpu);#else
@@ -250,6 +252,16 @@ static inline u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)return 0 ; } +static inline u64 kvm_vcpu_read_pmselr (struct kvm_vcpu *vcpu) +{ +return 0 ; +} + +static u64 kvm_vcpu_read_pmuserenr (struct kvm_vcpu *vcpu) +{ +return 0 ; +} +static inline bool kvm_pmu_counter_is_hyp (struct kvm_vcpu *vcpu,unsigned int idx) {return false;
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
kernel test robot
build botRe: [PATCH v6 08/19] KVM: arm64: Define access helpers for PMUSERENR and PMSELR
kernel test robot noticed the following build warnings:
Hi Colton,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 63804fed149a6750ffd28610c5c1c98cce6bd377]
url: https://github.com/intel-lab-lkp/linux/commits/Colton-Lewis/arm64-cpufeature-Add-cpucap-for-HPMN0/20260210-064939 base: 63804fed149a6750ffd28610c5c1c98cce6bd377 patch link: https://lore.kernel.org/r/20260209221414.2169465-9-coltonlewis%40google.com patch subject: [PATCH v6 08/19] KVM: arm64: Define access helpers for PMUSERENR and PMSELR config: arm64-allnoconfig (https://download.01.org/0day-ci/archive/20260210/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 15.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260210/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from arch/arm64/include/asm/kvm_host.h:38,
from include/linux/kvm_host.h:45,
from arch/arm64/kernel/asm-offsets.c:16:
>> include/kvm/arm_pmu.h:260:12: warning: 'kvm_vcpu_read_pmuserenr' defined but not used [-Wunused-function] 260 | static u64 kvm_vcpu_read_pmuserenr(struct kvm_vcpu *vcpu)
| ^~~~~~~~~~~~~~~~~~~~~~~
--
In file included from arch/arm64/include/asm/kvm_host.h:38,
from include/linux/kvm_host.h:45,
from arch/arm64/kernel/asm-offsets.c:16:
>> include/kvm/arm_pmu.h:260:12: warning: 'kvm_vcpu_read_pmuserenr' defined but not used [-Wunused-function] 260 | static u64 kvm_vcpu_read_pmuserenr(struct kvm_vcpu *vcpu)
| ^~~~~~~~~~~~~~~~~~~~~~~
vim +/kvm_vcpu_read_pmuserenr +260 include/kvm/arm_pmu.h
259
> 260 static u64 kvm_vcpu_read_pmuserenr(struct kvm_vcpu *vcpu)
261 {
262 return 0;
263 }
264
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v6 08/19] KVM: arm64: Define access helpers for PMUSERENR and PMSELR
In reply to Colton Lewiskernel test robot noticed the following build warnings:
Hi Colton,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 63804fed149a6750ffd28610c5c1c98cce6bd377]
url: https://github.com/intel-lab-lkp/linux/commits/Colton-Lewis/arm64-cpufeature-Add-cpucap-for-HPMN0/20260210-064939 base: 63804fed149a6750ffd28610c5c1c98cce6bd377 patch link: https://lore.kernel.org/r/20260209221414.2169465-9-coltonlewis%40google.com patch subject: [PATCH v6 08/19] KVM: arm64: Define access helpers for PMUSERENR and PMSELR config: arm64-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260210/[email protected]/config) compiler: aarch64-linux-gnu-gcc (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260210/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/
All warnings (new ones prefixed by >>):
In file included from ./arch/arm64/include/asm/kvm_host.h:38,
from ./include/linux/kvm_host.h:45,
from arch/arm64/kernel/asm-offsets.c:16:
>> ./include/kvm/arm_pmu.h:260:12: warning: 'kvm_vcpu_read_pmuserenr' defined but not used [-Wunused-function] 260 | static u64 kvm_vcpu_read_pmuserenr(struct kvm_vcpu *vcpu)
| ^~~~~~~~~~~~~~~~~~~~~~~
vim +/kvm_vcpu_read_pmuserenr +260 ./include/kvm/arm_pmu.h
2d3f843993bdd5 Colton Lewis 2026-02-09 259
2d3f843993bdd5 Colton Lewis 2026-02-09 @260 static u64 kvm_vcpu_read_pmuserenr(struct kvm_vcpu *vcpu)
2d3f843993bdd5 Colton Lewis 2026-02-09 261 {
2d3f843993bdd5 Colton Lewis 2026-02-09 262 return 0;
2d3f843993bdd5 Colton Lewis 2026-02-09 263 }2d3f843993bdd5 Colton Lewis 2026-02-09 264
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v6 9/19 KVM: arm64: Write fast path PMU register handlers
In reply to Colton LewisWe may want a partitioned PMU but not have FEAT_FGT to untrap the specific registers that would normally be untrapped. Add a handler for those registers in the fast path so we can still get a performance boost from partitioning.
The idea is to handle traps for all the PMU registers quickly by writing directly to the hardware when possible instead of hooking into the emulated vPMU as the standard handlers in sys_regs.c do.
For registers that can'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/hyp/vhe/switch.c | 238 ++++++++++++++++++++++++++++++++ 1 file changed, 238 insertions(+)
arch/arm64/kvm/hyp/vhe/switch.c +238 -0
@@ -28,6 +28,8 @@#include <asm/thread_info.h> #include <asm/vectors.h> +#include <../../sys_regs.h> +/* VHE specific context */ DEFINE_PER_CPU(struct kvm_host_data, kvm_host_data); DEFINE_PER_CPU(struct kvm_cpu_context, kvm_hyp_ctxt);
@@ -482,6 +484,239 @@ static bool kvm_hyp_handle_zcr_el2(struct kvm_vcpu *vcpu, u64 *exit_code)return false; } +/** +* kvm_hyp_handle_pmu_regs() - Fast handler for PMU registers +* @vcpu: Pointer to vcpu struct +* +* This handler immediately writes through certain PMU registers when +* we have a partitioned PMU (that is, MDCR_EL2.HPMN is set to reserve +* a range of counters for the guest) but the machine does not have +* FEAT_FGT to selectively untrap the registers we want. +* +* Return: True if the exception was successfully handled, false otherwise +*/ +static bool kvm_hyp_handle_pmu_regs (struct kvm_vcpu *vcpu) +{ +struct sys_reg_params p; +u64 pmuser; +u64 pmselr; +u64 esr; +u64 val; +u64 mask; +u32 sysreg; +u8 nr_cnt; +u8 rt; +u8 idx; +bool ret; + +if (!kvm_vcpu_pmu_is_partitioned (vcpu)) +return false; + + pmuser =kvm_vcpu_read_pmuserenr (vcpu); + +if (!(pmuser &ARMV8_PMU_USERENR_EN )) +return false; + + esr =kvm_vcpu_get_esr (vcpu); + p =esr_sys64_to_params (esr); + sysreg =esr_sys64_to_sysreg (esr); + rt =kvm_vcpu_sys_get_rt (vcpu); + val =vcpu_get_reg (vcpu, rt); + nr_cnt = vcpu->kvm->arch.nr_pmu_counters; + +switch (sysreg) { +case SYS_PMCR_EL0 : + mask =ARMV8_PMU_PMCR_MASK ; + +if (p.is_write) { +write_sysreg (val & mask, pmcr_el0); + }else { + mask |=ARMV8_PMU_PMCR_N ; + val =u64_replace_bits ( +read_sysreg (pmcr_el0), + nr_cnt, +ARMV8_PMU_PMCR_N ); +vcpu_set_reg (vcpu, rt, val & mask); + } + + ret = true; +break ; +case SYS_PMUSERENR_EL0 : + mask =ARMV8_PMU_USERENR_MASK ; + +if (p.is_write) { +write_sysreg (val & mask, pmuserenr_el0); + }else { + val =read_sysreg (pmuserenr_el0); +vcpu_set_reg (vcpu, rt, val & mask); + } + + ret = true; +break ; +case SYS_PMSELR_EL0 : + mask =PMSELR_EL0_SEL_MASK ; + val &= mask; + +if (p.is_write) { +write_sysreg (val & mask, pmselr_el0); + }else { + val =read_sysreg (pmselr_el0); +vcpu_set_reg (vcpu, rt, val & mask); + } + ret = true; +break ; +case SYS_PMINTENCLR_EL1 : + mask =kvm_pmu_accessible_counter_mask (vcpu); + +if (p.is_write) { +write_sysreg (val & mask, pmintenclr_el1); + }else { + val =read_sysreg (pmintenclr_el1); +vcpu_set_reg (vcpu, rt, val & mask); + } + ret = true; + +break ; +case SYS_PMINTENSET_EL1 : + mask =kvm_pmu_accessible_counter_mask (vcpu); + +if (p.is_write) { +write_sysreg (val & mask, pmintenset_el1); + }else { + val =read_sysreg (pmintenset_el1); +vcpu_set_reg (vcpu, rt, val & mask); + } + + ret = true; +break ; +case SYS_PMCNTENCLR_EL0 : + mask =kvm_pmu_accessible_counter_mask (vcpu); + +if (p.is_write) { +write_sysreg (val & mask, pmcntenclr_el0); + }else { + val =read_sysreg (pmcntenclr_el0); +vcpu_set_reg (vcpu, rt, val & mask); + } + + ret = true; +break ; +case SYS_PMCNTENSET_EL0 : + mask =kvm_pmu_accessible_counter_mask (vcpu); + +if (p.is_write) { +write_sysreg (val & mask, pmcntenset_el0); + }else { + val =read_sysreg (pmcntenset_el0); +vcpu_set_reg (vcpu, rt, val & mask); + } + + ret = true; +break ; +case SYS_PMOVSCLR_EL0 : + mask =kvm_pmu_accessible_counter_mask (vcpu); + +if (p.is_write) { + __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, &=, ~(val & mask)); + }else { + val =__vcpu_sys_reg (vcpu,PMOVSSET_EL0 ); +vcpu_set_reg (vcpu, rt, val & mask); + } + + ret = true; +break ; +case SYS_PMOVSSET_EL0 : + mask =kvm_pmu_accessible_counter_mask (vcpu); + +if (p.is_write) { + __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, val & mask); + }else { + val =__vcpu_sys_reg (vcpu,PMOVSSET_EL0 ); +vcpu_set_reg (vcpu, rt, val & mask); + } + + ret = true; +break ; +case SYS_PMCCNTR_EL0 : +case SYS_PMXEVCNTR_EL0 : + case SYS_PMEVCNTRn_EL0(0) ... SYS_PMEVCNTRn_EL0(30): +if (sysreg ==SYS_PMCCNTR_EL0 ) + idx =ARMV8_PMU_CYCLE_IDX ; +else if (sysreg ==SYS_PMXEVCNTR_EL0 ) + idx =FIELD_GET (PMSELR_EL0_SEL ,kvm_vcpu_read_pmselr (vcpu)); +else + idx = ((p.CRm &3 ) <<3 ) | (p.Op2 &7 ); + +if (idx ==ARMV8_PMU_CYCLE_IDX && + !(pmuser &ARMV8_PMU_USERENR_CR )) { + ret = false; +break ; + }else if (!(pmuser &ARMV8_PMU_USERENR_ER )) { + ret = false; +break ; + } + +if (idx >= nr_cnt && idx <ARMV8_PMU_CYCLE_IDX ) { + ret = false; +break ; + } + + pmselr =read_sysreg (pmselr_el0); +write_sysreg (idx, pmselr_el0); + +if (p.is_write) { +write_sysreg (val, pmxevcntr_el0); + }else { + val =read_sysreg (pmxevcntr_el0); +vcpu_set_reg (vcpu, rt, val); + } + +write_sysreg (pmselr, pmselr_el0); + ret = true; +break ; +case SYS_PMCCFILTR_EL0 : +case SYS_PMXEVTYPER_EL0 : + case SYS_PMEVTYPERn_EL0(0) ... SYS_PMEVTYPERn_EL0(30): +if (sysreg ==SYS_PMCCFILTR_EL0 ) + idx =ARMV8_PMU_CYCLE_IDX ; +else if (sysreg ==SYS_PMXEVTYPER_EL0 ) + idx =FIELD_GET (PMSELR_EL0_SEL ,kvm_vcpu_read_pmselr (vcpu)); +else + idx = ((p.CRm &3 ) <<3 ) | (p.Op2 &7 ); + +if (idx ==ARMV8_PMU_CYCLE_IDX && + !(pmuser &ARMV8_PMU_USERENR_CR )) { + ret = false; +break ; + }else if (!(pmuser &ARMV8_PMU_USERENR_ER )) { + ret = false; +break ; + } + +if (idx >= nr_cnt && idx <ARMV8_PMU_CYCLE_IDX ) { + ret = false; +break ; + } + +if (p.is_write) { +__vcpu_assign_sys_reg (vcpu,PMEVTYPER0_EL0 + idx, val); + }else { + val =__vcpu_sys_reg (vcpu,PMEVTYPER0_EL0 + idx); +vcpu_set_reg (vcpu, rt, val); + } + + ret = true; +break ; +default : + ret = false; + } + +if (ret) +__kvm_skip_instr (vcpu); + +return ret; +} +static bool kvm_hyp_handle_sysreg_vhe (struct kvm_vcpu *vcpu,u64 *exit_code) {if (kvm_hyp_handle_tlbi_el2 (vcpu, exit_code))
@@ -496,6 +731,9 @@ static bool kvm_hyp_handle_sysreg_vhe(struct kvm_vcpu *vcpu, u64 *exit_code)if (kvm_hyp_handle_zcr_el2 (vcpu, exit_code))return true; +if (kvm_hyp_handle_pmu_regs (vcpu)) +return true; +return kvm_hyp_handle_sysreg (vcpu, exit_code); }
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Marc Zyngier
maintainerRe: [PATCH v6 09/19] KVM: arm64: Write fast path PMU register handlers
Quoted 9 lines · Colton Lewis
We may want a partitioned PMU but not have
FEAT_FGTto untrap the specific registers that would normally be untrapped. Add a handler for those registers in the fast path so we can still get a performance boost from partitioning.The idea is to handle traps for all the PMU registers quickly by writing directly to the hardware when possible instead of hooking into the emulated vPMU as the standard handlers in
sys_regs.cdo.
This seems extremely premature. My assumption is that PMU traps are rare, and that doing a full exit should be acceptable. Until you demonstrate the contrary, I don't want this sort of massive bloat in the most performance-critical path.
"Start walking before you try to run".
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v6 09/19] KVM: arm64: Write fast path PMU register handlers
Quoted 8 lines · Marc Zyngier
Quoted 4 lines · Colton Lewis
We may want a partitioned PMU but not have
FEAT_FGTto untrap the specific registers that would normally be untrapped. Add a handler for those registers in the fast path so we can still get a performance boost from partitioning.
Quoted 8 lines
Quoted 3 lines
The idea is to handle traps for all the PMU registers quickly by writing directly to the hardware when possible instead of hooking into the emulated vPMU as the standard handlers in
sys_regs.cdo.This seems extremely premature. My assumption is that PMU traps are rare, and that doing a full exit should be acceptable. Until you demonstrate the contrary, I don't want this sort of massive bloat in the most performance-critical path.
After some consideration I agree. I will try a full exit and see if that is sufficient.
PATCH v6 10/19 KVM: arm64: Setup MDCR_EL2 to handle a partitioned PMU
In reply to Colton LewisSetup MDCR_EL2 to handle a partitioned PMU. That means calculate an appropriate value for HPMN instead of the default maximum setting the host allows (which implies no partition) so hardware enforces that a guest will only see the counters in the guest partition.
Setting HPMN to a non default value means the global enable bit for the host counters is now MDCR_EL2.HPME instead of the usual PMCR_EL0.E. Enable the HPME bit to allow the host to count guest events. Since HPME only has an effect when HPMN is set which we only do for the guest, it is correct to enable it unconditionally here.
Unset the TPM and TPMCR bits, which trap all PMU accesses, if FGT (fine grain trapping) is being used.
If available, set the filtering bits HPMD and HCCD to be extra sure nothing in the guest counts at EL2.
--- arch/arm64/kvm/debug.c | 29 ++++++++++++++++++++++++++--- arch/arm64/kvm/pmu-direct.c | 24 ++++++++++++++++++++++++ arch/arm64/kvm/pmu.c | 7 +++++++ include/kvm/arm_pmu.h | 11 +++++++++++ 4 files changed, 68 insertions(+), 3 deletions(-)
arch/arm64/kvm/debug.c +26 -3
@@ -36,20 +36,43 @@ static int cpu_has_spe(u64 dfr0)*/ static void kvm_arm_setup_mdcr_el2 (struct kvm_vcpu *vcpu) { +int hpmn =kvm_pmu_hpmn (vcpu); +preempt_disable ();/* * 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 , hpmn); 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_vcpu_pmu_is_partitioned (vcpu)) { +/* +* Filtering these should be redundant because we trap +* all the TYPER and FILTR registers anyway and ensure +* they filter EL2, but set the bits if they are here. +*/ +if (is_pmuv3p1 (read_pmuver ())) + vcpu->arch.mdcr_el2 |=MDCR_EL2_HPMD ; +if (is_pmuv3p5 (read_pmuver ())) + vcpu->arch.mdcr_el2 |=MDCR_EL2_HCCD ; + +/* +* Take out the coarse grain traps if we are using +* fine grain traps. +*/ +if (kvm_vcpu_pmu_use_fgt (vcpu)) + vcpu->arch.mdcr_el2 &= ~(MDCR_EL2_TPM |MDCR_EL2_TPMCR ); + + }/* Is the VM being debugged by userspace? */ if (vcpu->guest_debug)
arch/arm64/kvm/pmu-direct.c +24 -0
@@ -139,3 +139,27 @@ void kvm_pmu_host_counters_disable(void) mdcr &= ~MDCR_EL2_HPME ;write_sysreg (mdcr, mdcr_el2); } + +/** +* kvm_pmu_hpmn() - Calculate HPMN field value +* @vcpu: Pointer to struct kvm_vcpu +* +* Calculate the appropriate value to set for MDCR_EL2.HPMN. If +* partitioned, this is the number of counters set for the guest if +* supported, falling back to max_guest_counters if needed. If we are not +* partitioned or can't set the implied HPMN value, fall back to the +* host value. +* +* Return: A valid HPMN value +*/ +u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu) +{ +u8 nr_guest_cntr = vcpu->kvm->arch.nr_pmu_counters; + +if (kvm_vcpu_pmu_is_partitioned (vcpu) + && !vcpu_on_unsupported_cpu (vcpu) + && (cpus_have_final_cap (ARM64_HAS_HPMN0 ) || nr_guest_cntr >0 )) +return nr_guest_cntr; + +return *host_data_ptr (nr_event_counters); +}
arch/arm64/kvm/pmu.c +7 -0
@@ -542,6 +542,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 (kvm_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.
include/kvm/arm_pmu.h +11 -0
@@ -98,6 +98,9 @@ u64 kvm_pmu_guest_counter_mask(struct arm_pmu *pmu);void kvm_pmu_host_counters_enable (void );void kvm_pmu_host_counters_disable (void ); +u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu); +u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu); +#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu);
@@ -162,6 +165,14 @@ static inline bool kvm_vcpu_pmu_use_fgt(struct kvm_vcpu *vcpu) {return false; } +static inline u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu) +{ +return 0 ; +} +static inline u8 kvm_pmu_hpmn (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.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 11/19 KVM: arm64: Context swap Partitioned PMU guest registers
In reply to Colton LewisSave and restore newly untrapped registers that can be directly accessed by the guest when the PMU is partitioned.
* PMEVCNTRn_EL0
* PMCCNTR_EL0
* PMSELR_EL0
* PMCR_EL0
* PMCNTEN_EL0
* PMINTEN_EL1
If we know we are not partitioned (that is, using the emulated vPMU), then return immediately. A later patch will make this lazy so the context swaps don't happen unless the guest has accessed the PMU.
PMEVTYPER is handled in a following patch since we must apply the KVM event filter before writing values to hardware.
PMOVS guest counters are cleared to avoid the possibility of generating spurious interrupts when PMINTEN is written. This is fine because the virtual register for PMOVS is always the canonical value.
--- arch/arm64/kvm/arm.c | 2 + arch/arm64/kvm/pmu-direct.c | 123 ++++++++++++++++++++++++++++++++++++ include/kvm/arm_pmu.h | 4 ++ 3 files changed, 129 insertions(+)
arch/arm64/kvm/arm.c +2 -0
@@ -635,6 +635,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);
@@ -676,6 +677,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 +123 -0
132 lines not on this page · raw message
include/kvm/arm_pmu.h +4 -0
@@ -100,6 +100,8 @@ void kvm_pmu_host_counters_disable(void);u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu);u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu); +void kvm_pmu_load (struct kvm_vcpu *vcpu); +void kvm_pmu_put (struct kvm_vcpu *vcpu);#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);
@@ -173,6 +175,8 @@ static inline u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) {return 0 ; } +static inline void kvm_pmu_load (struct kvm_vcpu *vcpu) {} +static inline void kvm_pmu_put (struct kvm_vcpu *vcpu) {}static inline void kvm_pmu_set_counter_value (struct kvm_vcpu *vcpu,u64 select_idx,u64 val) {} static inline void kvm_pmu_set_counter_value_user(struct kvm_vcpu *vcpu,
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
Re: [PATCH v6 11/19] KVM: arm64: Context swap Partitioned PMU guest registers
Quoted 88 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_EL1If 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.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/arm.c | 2 + arch/arm64/kvm/pmu-direct.c | 123 ++++++++++++++++++++++++++++++++++++ include/kvm/arm_pmu.h | 4 ++ 3 files changed, 129 insertions(+)arch/arm64/kvm/arm.c +2
-0@@ -635,6 +635,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);@@ -676,6 +677,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 +25
-0@@ -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 @@ -163,3 +164,125 @@ u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu)return *host_data_ptr (nr_event_counters); } + +/** +* kvm_pmu_load() - Load untrapped PMU registers +* @vcpu: Pointer to struct kvm_vcpu +* +* Load 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_load (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu; +unsigned long guest_counters; +u64 mask; +u8 i; +u64 val; + +/* +* 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_vcpu_pmu_is_partitioned (vcpu)) +return ; +
arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 200
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+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 ) { + val =__vcpu_sys_reg (vcpu,PMEVCNTR0_EL0 + i); + +write_sysreg (i, pmselr_el0); +write_sysreg (val, pmxevcntr_el0);
This needs to have a special case for ARMV8_PMU_CYCLE_IDX because you can't use pmxevcntr_el0 to read or write PMCCNTR_EL0:
D24.5.22:
SEL 0b11111 Select the cycle counter, PMCCNTR_EL0:
MRS and MSR of PMXEVCNTR_EL0 are CONSTRAINED UNPREDICTABLE.
There are 3 separate instances of the same thing in the patches. I was getting undefined instruction errors on my Radxa O6 board until they were all fixed.
Re: [PATCH v6 11/19] KVM: arm64: Context swap Partitioned PMU guest registers
Quoted 107 lines · James Clark
Quoted 90 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_EL1If 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.
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/kvm/arm.c | 2 + arch/arm64/kvm/pmu-direct.c | 123 ++++++++++++++++++++++++++++++++++++ include/kvm/arm_pmu.h | 4 ++ 3 files changed, 129 insertions(+)arch/arm64/kvm/arm.c +2
-0@@ -635,6 +635,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);@@ -676,6 +677,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 +1
-0@@ -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
arch/arm64/kvm/pmu-direct.c +24
-0@@ -163,3 +164,125 @@ u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu)return *host_data_ptr (nr_event_counters); } + +/** +* kvm_pmu_load() - Load untrapped PMU registers +* @vcpu: Pointer to struct kvm_vcpu +* +* Load 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_load (struct kvm_vcpu *vcpu) +{ +struct arm_pmu *pmu; +unsigned long guest_counters; +u64 mask; +u8 i; +u64 val; + +/* +* 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_vcpu_pmu_is_partitioned (vcpu)) +return ; +arch/arm64/kvm/pmu-direct.c u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu) · line 200
Quoted 10 lines
arch/arm64/kvm/pmu-direct.c +10
-0+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 ) { + val =__vcpu_sys_reg (vcpu,PMEVCNTR0_EL0 + i); + +write_sysreg (i, pmselr_el0); +write_sysreg (val, pmxevcntr_el0);This needs to have a special case for
ARMV8_PMU_CYCLE_IDXbecause you can't usepmxevcntr_el0to read or writePMCCNTR_EL0:D24.5.22:
Quoted 8 lines
SEL 0b11111 Select the cycle counter,
PMCCNTR_EL0:MRS and MSR of
PMXEVCNTR_EL0are CONSTRAINED UNPREDICTABLE.There are 3 separate instances of the same thing in the patches. I was getting undefined instruction errors on my Radxa O6 board until they were all fixed.
Looks like it. I had a special case on a previous iteration but someone suggested I could get rid of it by iterating the mask.
I missed that the cycle counter was CONSTRAINED UNPREDICTABLE.
PATCH v6 12/19 KVM: arm64: Enforce PMU event filter at vcpu_load()
In reply to Colton LewisThe 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 | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+)
arch/arm64/kvm/pmu-direct.c +48 -0
@@ -165,6 +165,53 @@ u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu)return *host_data_ptr (nr_event_counters); } +/** +* 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 =kvm_pmu_guest_counter_mask (pmu); +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 ; + +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; + +write_sysreg (i, pmselr_el0); +write_sysreg (val, pmxevtyper_el0); + } +} +/** * kvm_pmu_load() - Load untrapped PMU registers * @vcpu: Pointer to struct kvm_vcpu
@@ -192,6 +239,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.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 13/19 KVM: arm64: Implement lazy PMU context swaps
In reply to Colton LewisSince 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 | 2 +- arch/arm64/kvm/hyp/vhe/switch.c | 2 ++ arch/arm64/kvm/pmu-direct.c | 26 ++++++++++++++++++++++++-- include/kvm/arm_pmu.h | 5 +++++ 6 files changed, 38 insertions(+), 4 deletions(-)
arch/arm64/include/asm/kvm_host.h +1 -0
@@ -1377,6 +1377,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
@@ -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 +1 -1
@@ -34,7 +34,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) {int hpmn =kvm_pmu_hpmn (vcpu);
arch/arm64/kvm/hyp/vhe/switch.c +2 -0
@@ -524,6 +524,8 @@ static bool kvm_hyp_handle_pmu_regs(struct kvm_vcpu *vcpu) val =vcpu_get_reg (vcpu, rt); nr_cnt = vcpu->kvm->arch.nr_pmu_counters; +kvm_pmu_set_physical_access (vcpu); +switch (sysreg) {case SYS_PMCR_EL0 : mask =ARMV8_PMU_PMCR_MASK ;
arch/arm64/kvm/pmu-direct.c +24 -2
@@ -72,10 +72,30 @@ bool kvm_vcpu_pmu_use_fgt(struct kvm_vcpu *vcpu)u8 hpmn = vcpu->kvm->arch.nr_pmu_counters;return kvm_vcpu_pmu_is_partitioned (vcpu) && + vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_GUEST_OWNED &&cpus_have_final_cap (ARM64_HAS_FGT ) && (hpmn !=0 ||cpus_have_final_cap (ARM64_HAS_HPMN0 )); } +/** +* kvm_pmu_set_physical_access() +* @vcpu: Pointer to vcpu struct +* +* Reconfigure the guest for physical access of PMU hardware if +* allowed. This means reconfiguring mdcr_el2 and loading the vCPU +* state onto hardware. +* +*/ + +void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu) +{ +if (kvm_vcpu_pmu_is_partitioned (vcpu) + && vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_FREE ) { + vcpu->arch.pmu.access =VCPU_PMU_ACCESS_GUEST_OWNED ; +kvm_arm_setup_mdcr_el2 (vcpu); + } +} +/** * kvm_pmu_host_counter_mask() - Compute bitmask of host-reserved counters * @pmu: Pointer to arm_pmu struct
@@ -232,7 +252,8 @@ 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_vcpu_pmu_is_partitioned (vcpu)) +if (!kvm_vcpu_pmu_is_partitioned (vcpu) || + vcpu->arch.pmu.access !=VCPU_PMU_ACCESS_GUEST_OWNED )return ;preempt_disable ();
@@ -302,7 +323,8 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)* accessing the PMU anyway, so no need to bother with the * swap. */ -if (!kvm_vcpu_pmu_is_partitioned (vcpu)) +if (!kvm_vcpu_pmu_is_partitioned (vcpu) || + vcpu->arch.pmu.access !=VCPU_PMU_ACCESS_GUEST_OWNED )return ;preempt_disable ();
include/kvm/arm_pmu.h +5 -0
@@ -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>
@@ -40,6 +41,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,8 @@ u8 kvm_pmu_hpmn(struct kvm_vcpu *vcpu);void kvm_pmu_load (struct kvm_vcpu *vcpu);void kvm_pmu_put (struct kvm_vcpu *vcpu); +void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu); +#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);bool kvm_vcpu_pmu_use_fgt (struct kvm_vcpu *vcpu);
@@ -177,6 +181,7 @@ static inline u8 kvm_pmu_hpmn(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_physical_access (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.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 14/19 perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
In reply to Colton LewisBecause 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 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 | 2 ++ 5 files changed, 52 insertions(+), 7 deletions(-)
arch/arm/include/asm/arm_pmuv3.h +6 -0
@@ -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 );
@@ -249,6 +254,7 @@ static inline u64 kvm_pmu_guest_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
@@ -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
@@ -356,3 +356,25 @@ void kvm_pmu_put(struct kvm_vcpu *vcpu)preempt_enable (); } + +/** +* 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
@@ -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; }
@@ -903,16 +902,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?
@@ -920,6 +920,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) */
@@ -961,6 +967,10 @@ static irqreturn_t armv8pmu_handle_irq(struct arm_pmu *cpu_pmu)*/ perf_event_overflow (event, &data, regs); } + +if (kvm_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 +2 -0
@@ -99,6 +99,7 @@ u64 kvm_pmu_host_counter_mask(struct arm_pmu *pmu);u64 kvm_pmu_guest_counter_mask (struct arm_pmu *pmu);void kvm_pmu_host_counters_enable (void );void kvm_pmu_host_counters_disable (void ); +void kvm_pmu_handle_guest_irq (struct arm_pmu *pmu,u64 pmovsr);u8 kvm_pmu_guest_num_counters (struct kvm_vcpu *vcpu);u8 kvm_pmu_hpmn (struct kvm_vcpu *vcpu);
@@ -306,6 +307,7 @@ static inline u64 kvm_pmu_guest_counter_mask(void *pmu)static inline void kvm_pmu_host_counters_enable (void ) {}static inline void kvm_pmu_host_counters_disable (void ) {} +static inline void kvm_pmu_handle_guest_irq (struct arm_pmu *pmu,u64 pmovsr) {}#endif
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
kernel test robot
build botRe: [PATCH v6 14/19] perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
kernel test robot noticed the following build warnings:
Hi Colton,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 63804fed149a6750ffd28610c5c1c98cce6bd377]
url: https://github.com/intel-lab-lkp/linux/commits/Colton-Lewis/arm64-cpufeature-Add-cpucap-for-HPMN0/20260210-064939 base: 63804fed149a6750ffd28610c5c1c98cce6bd377 patch link: https://lore.kernel.org/r/20260209221414.2169465-15-coltonlewis%40google.com patch subject: [PATCH v6 14/19] perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters config: arm64-randconfig-001-20260210 (https://download.01.org/0day-ci/archive/20260210/[email protected]/config) compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 9b8addffa70cee5b2acc5454712d9cf78ce45710) rustc: rustc 1.88.0 (6b00bc388 2025-06-23) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260210/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from arch/arm64/kernel/asm-offsets.c:16:
In file included from include/linux/kvm_host.h:45:
In file included from arch/arm64/include/asm/kvm_host.h:38:
>> include/kvm/arm_pmu.h:310:52: warning: declaration of 'struct arm_pmu' will not be visible outside of this function [-Wvisibility]
310 | static inline void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) {}
| ^ include/kvm/arm_pmu.h:281:12: warning: unused function 'kvm_vcpu_read_pmuserenr' [-Wunused-function]
281 | static u64 kvm_vcpu_read_pmuserenr(struct kvm_vcpu *vcpu)
| ^~~~~~~~~~~~~~~~~~~~~~~
2 warnings generated.
--
In file included from arch/arm64/kernel/asm-offsets.c:16:
In file included from include/linux/kvm_host.h:45:
In file included from arch/arm64/include/asm/kvm_host.h:38:
>> include/kvm/arm_pmu.h:310:52: warning: declaration of 'struct arm_pmu' will not be visible outside of this function [-Wvisibility]
310 | static inline void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) {}
| ^
include/kvm/arm_pmu.h:281:12: warning: unused function 'kvm_vcpu_read_pmuserenr' [-Wunused-function] 281 | static u64 kvm_vcpu_read_pmuserenr(struct kvm_vcpu *vcpu)
| ^~~~~~~~~~~~~~~~~~~~~~~
2 warnings generated.
warning: unused variable: args
--> rust/kernel/kunit.rs:19:12
|
19 | pub fn err(args: fmt::Arguments<'_>) {
| ^^^^ help: if this is intentional, prefix it with an underscore: _args
|
= note: #[warn(unused_variables)] on by default
vim +310 include/kvm/arm_pmu.h
307
308 static inline void kvm_pmu_host_counters_enable(void) {}
309 static inline void kvm_pmu_host_counters_disable(void) {}
> 310 static inline void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) {}311
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botRe: [PATCH v6 14/19] perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters
In reply to Colton Lewiskernel test robot noticed the following build warnings:
Hi Colton,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 63804fed149a6750ffd28610c5c1c98cce6bd377]
url: https://github.com/intel-lab-lkp/linux/commits/Colton-Lewis/arm64-cpufeature-Add-cpucap-for-HPMN0/20260210-064939 base: 63804fed149a6750ffd28610c5c1c98cce6bd377 patch link: https://lore.kernel.org/r/20260209221414.2169465-15-coltonlewis%40google.com patch subject: [PATCH v6 14/19] perf: arm_pmuv3: Handle IRQs for Partitioned PMU guest counters config: arm64-allnoconfig-bpf (https://download.01.org/0day-ci/archive/20260210/[email protected]/config) compiler: aarch64-linux-gnu-gcc (Debian 14.2.0-19) 14.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260210/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/r/[email protected]/
All warnings (new ones prefixed by >>):
In file included from ./arch/arm64/include/asm/kvm_host.h:38,
from ./include/linux/kvm_host.h:45,
from arch/arm64/kernel/asm-offsets.c:16:
>> ./include/kvm/arm_pmu.h:310:52: warning: 'struct arm_pmu' declared inside parameter list will not be visible outside of this definition or declaration
310 | static inline void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) {}
| ^~~~~~~ ./include/kvm/arm_pmu.h:281:12: warning: 'kvm_vcpu_read_pmuserenr' defined but not used [-Wunused-function]
281 | static u64 kvm_vcpu_read_pmuserenr(struct kvm_vcpu *vcpu)
| ^~~~~~~~~~~~~~~~~~~~~~~
vim +310 ./include/kvm/arm_pmu.h
baec257585c39b Colton Lewis 2026-02-09 307
baec257585c39b Colton Lewis 2026-02-09 308 static inline void kvm_pmu_host_counters_enable(void) {}
baec257585c39b Colton Lewis 2026-02-09 309 static inline void kvm_pmu_host_counters_disable(void) {}ad5f1148c818bf Colton Lewis 2026-02-09 @310 static inline void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) {} baec257585c39b Colton Lewis 2026-02-09 311
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v6 15/19 KVM: arm64: Detect overflows for the Partitioned PMU
In reply to Colton LewisWhen 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 | 30 ++++++++++++++++++++++++++++++ arch/arm64/kvm/pmu-emul.c | 4 ++-- arch/arm64/kvm/pmu.c | 6 +++++- include/kvm/arm_pmu.h | 2 ++ 4 files changed, 39 insertions(+), 3 deletions(-)
arch/arm64/kvm/pmu-direct.c +30 -0
@@ -378,3 +378,33 @@ void kvm_pmu_handle_guest_irq(struct arm_pmu *pmu, u64 pmovsr) __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, |=, govf); } + +/** +* 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; + +if (vcpu->arch.pmu.access ==VCPU_PMU_ACCESS_FREE ) +return false; + + pmu = vcpu->kvm->arch.arm_pmu; + mask =kvm_pmu_guest_counter_mask (pmu); + 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
@@ -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
@@ -408,7 +408,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_vcpu_pmu_is_partitioned (vcpu)) + 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
@@ -90,6 +90,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.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 16/19 KVM: arm64: Add vCPU device attr to partition the PMU
In reply to Colton LewisAdd 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 flagKVM_ARCH_FLAG_PARTITIONED_PMU_ENABLED.
--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 2 ++ arch/arm64/kvm/pmu-direct.c | 35 ++++++++++++++++++++++++++++--- arch/arm64/kvm/pmu.c | 14 +++++++++++++ include/kvm/arm_pmu.h | 9 ++++++++ 5 files changed, 59 insertions(+), 3 deletions(-)
arch/arm64/include/asm/kvm_host.h +2 -0
@@ -353,6 +353,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 12unsigned long flags;/* VM-wide vCPU feature set */
arch/arm64/include/uapi/asm/kvm.h +2 -0
@@ -436,6 +436,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 +32 -3
@@ -44,8 +44,8 @@ bool kvm_pmu_is_partitioned(struct arm_pmu *pmu) }/** -* kvm_vcpu_pmu_is_partitioned() - Determine if given VCPU has a partitioned PMU -* @vcpu: Pointer to kvm_vcpu struct +* kvm_pmu_is_partitioned() - Determine if given VCPU has a partitioned PMU +* @kvm: Pointer to kvm_vcpu struct * * Determine if given VCPU has a partitioned PMU by extracting that * field and passing it to :c:func:`kvm_pmu_is_partitioned`
@@ -55,7 +55,36 @@ bool kvm_pmu_is_partitioned(struct arm_pmu *pmu)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) {return kvm_pmu_is_partitioned (vcpu->kvm->arch.arm_pmu) && - false; +test_bit (KVM_ARCH_FLAG_PARTITION_PMU_ENABLED , &vcpu->kvm->arch.flags); +} + +/** +* 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_max_guest_counters >-1 ; +} + +/** +* 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); }/**
arch/arm64/kvm/pmu.c +14 -0
@@ -759,6 +759,19 @@ 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; +bool enable; + +if (get_user (enable, uaddr)) +return -EFAULT ; + +if (!has_kvm_pmu_partition_support ()) +return -EPERM ; + +kvm_pmu_partition_enable (kvm, enable); +return 0 ; + }case KVM_ARM_VCPU_PMU_V3_INIT :return kvm_arm_pmu_v3_init (vcpu); }
@@ -798,6 +811,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
@@ -109,6 +109,8 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu);void kvm_pmu_put (struct kvm_vcpu *vcpu);void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu); +bool has_kvm_pmu_partition_support (void ); +void kvm_pmu_partition_enable (struct kvm *kvm,bool enable);#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);
@@ -311,6 +313,13 @@ static inline void kvm_pmu_host_counters_enable(void) {}static inline void kvm_pmu_host_counters_disable (void ) {}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.53.0.rc2.204.g2597b5adb4-goog
kernel test robot
build botRe: [PATCH v6 16/19] KVM: arm64: Add vCPU device attr to partition the PMU
kernel test robot noticed the following build warnings:
Hi Colton,
kernel test robot noticed the following build warnings:
[auto build test WARNING on 63804fed149a6750ffd28610c5c1c98cce6bd377]
url: https://github.com/intel-lab-lkp/linux/commits/Colton-Lewis/arm64-cpufeature-Add-cpucap-for-HPMN0/20260210-064939 base: 63804fed149a6750ffd28610c5c1c98cce6bd377 patch link: https://lore.kernel.org/r/20260209221414.2169465-17-coltonlewis%40google.com patch subject: [PATCH v6 16/19] KVM: arm64: Add vCPU device attr to partition the PMU config: arm64-defconfig (https://download.01.org/0day-ci/archive/20260210/[email protected]/config) compiler: aarch64-linux-gcc (GCC) 15.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260210/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: arch/arm64/kvm/pmu-direct.c:55 function parameter 'vcpu' not described in 'kvm_vcpu_pmu_is_partitioned' >> Warning: arch/arm64/kvm/pmu-direct.c:55 expecting prototype for kvm_pmu_is_partitioned(). Prototype was for kvm_vcpu_pmu_is_partitioned() instead
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Re: [PATCH v6 16/19] KVM: arm64: Add vCPU device attr to partition the PMU
In reply to Colton LewisQuoted 8 lines · 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_PARTITIONED_PMU_ENABLED.
Typo, should be: KVM_ARCH_FLAG_PARTITION_PMU_ENABLED. Or maybe the #define should be fixed.
I couldn't see if this was discussed before, but what's the reason to not use the guest partition by default and make this flag control reverting back to use the non passed through PMU?
Seems like if you already have to enable it by creating a partition on the host, then you more than likely want your guests to use it. And it's lower overhead so it's "better". Right now it's two things that you have to set at the same time to do one thing.
Or does having to set it on the host go away with the dynamic approach here [1]?
[1]: https://lore.kernel.org/kvmarm/aWjlfl85vSd6sMwT@willie-the-truck/
Quoted 148 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 2 ++ arch/arm64/kvm/pmu-direct.c | 35 ++++++++++++++++++++++++++++--- arch/arm64/kvm/pmu.c | 14 +++++++++++++ include/kvm/arm_pmu.h | 9 ++++++++ 5 files changed, 59 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_host.h +2
-0@@ -353,6 +353,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 12unsigned long flags;/* VM-wide vCPU feature set */ arch/arm64/include/uapi/asm/kvm.h +2
-0@@ -436,6 +436,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 1arch/arm64/kvm/pmu-direct.c +32
-3@@ -44,8 +44,8 @@ bool kvm_pmu_is_partitioned(struct arm_pmu *pmu) }/** -* kvm_vcpu_pmu_is_partitioned() - Determine if given VCPU has a partitioned PMU -* @vcpu: Pointer to kvm_vcpu struct +* kvm_pmu_is_partitioned() - Determine if given VCPU has a partitioned PMU +* @kvm: Pointer to kvm_vcpu struct * * Determine if given VCPU has a partitioned PMU by extracting that * field and passing it to :c:func:`kvm_pmu_is_partitioned` @@ -55,7 +55,36 @@ bool kvm_pmu_is_partitioned(struct arm_pmu *pmu)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) {return kvm_pmu_is_partitioned (vcpu->kvm->arch.arm_pmu) && - false; +test_bit (KVM_ARCH_FLAG_PARTITION_PMU_ENABLED , &vcpu->kvm->arch.flags); +} + +/** +* 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_max_guest_counters >-1 ; +} + +/** +* 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); }/** arch/arm64/kvm/pmu.c +14
-0@@ -759,6 +759,19 @@ 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; +bool enable; + +if (get_user (enable, uaddr)) +return -EFAULT ; + +if (!has_kvm_pmu_partition_support ()) +return -EPERM ; + +kvm_pmu_partition_enable (kvm, enable); +return 0 ; + }case KVM_ARM_VCPU_PMU_V3_INIT :return kvm_arm_pmu_v3_init (vcpu); }@@ -798,6 +811,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@@ -109,6 +109,8 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu);void kvm_pmu_put (struct kvm_vcpu *vcpu);void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu); +bool has_kvm_pmu_partition_support (void ); +void kvm_pmu_partition_enable (struct kvm *kvm,bool enable);#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);@@ -311,6 +313,13 @@ static inline void kvm_pmu_host_counters_enable(void) {}static inline void kvm_pmu_host_counters_disable (void ) {}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
Re: [PATCH v6 16/19] KVM: arm64: Add vCPU device attr to partition the PMU
Thanks James for the review.
Quoted 7 lines · James Clark
Quoted 5 lines · 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.
Quoted 5 lines
Quoted 2 lines
The enabled capability is tracked by the new flag
KVM_ARCH_FLAG_PARTITIONED_PMU_ENABLED.Typo, should be:
KVM_ARCH_FLAG_PARTITION_PMU_ENABLED. Or maybe the #define should be fixed.
Stale commit message. I will fix.
Quoted 11 lines
I couldn't see if this was discussed before, but what's the reason to not use the guest partition by default and make this flag control reverting back to use the non passed through PMU?
Seems like if you already have to enable it by creating a partition on the host, then you more than likely want your guests to use it. And it's lower overhead so it's "better". Right now it's two things that you have to set at the same time to do one thing.
Or does having to set it on the host go away with the dynamic approach here [1]?
Yes, the plan is to have it go away on the host with a dynamic approach.
Quoted 157 lines
[1]: https://lore.kernel.org/kvmarm/aWjlfl85vSd6sMwT@willie-the-truck/
Quoted 154 lines
Signed-off-by Colton Lewis <[email protected]>--- arch/arm64/include/asm/kvm_host.h | 2 ++ arch/arm64/include/uapi/asm/kvm.h | 2 ++ arch/arm64/kvm/pmu-direct.c | 35 ++++++++++++++++++++++++++++--- arch/arm64/kvm/pmu.c | 14 +++++++++++++ include/kvm/arm_pmu.h | 9 ++++++++ 5 files changed, 59 insertions(+), 3 deletions(-)arch/arm64/include/asm/kvm_host.h +2
-0@@ -353,6 +353,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 12unsigned long flags;/* VM-wide vCPU feature set */ arch/arm64/include/uapi/asm/kvm.h +2
-0@@ -436,6 +436,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 1arch/arm64/kvm/pmu-direct.c +0
-1@@ -44,8 +44,8 @@ bool kvm_pmu_is_partitioned(struct arm_pmu *pmu) }/** -* kvm_vcpu_pmu_is_partitioned() - Determine if given VCPU has a partitioned PMU
- * @vcpu: Pointer tokvm_vcpustruct
+ *kvm_pmu_is_partitioned()- Determine if given VCPU has a partitioned PMU
+ * @kvm: Pointer tokvm_vcpustruct
*
* Determine if given VCPU has a partitioned PMU by extracting that
* field and passing it to :c:func:kvm_pmu_is_partitionedarch/arm64/kvm/pmu-direct.c +30
-1@@ -55,7 +55,36 @@ bool kvm_pmu_is_partitioned(struct arm_pmu *pmu)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu) {return kvm_pmu_is_partitioned (vcpu->kvm->arch.arm_pmu) && - false; +test_bit (KVM_ARCH_FLAG_PARTITION_PMU_ENABLED , &vcpu->kvm->arch.flags); +} + +/** +* 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_max_guest_counters >-1 ; +} + +/** +* 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); }/** arch/arm64/kvm/pmu.c +13
-0@@ -759,6 +759,19 @@ 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; +bool enable; + +if (get_user (enable, uaddr)) +return -EFAULT ; + +if (!has_kvm_pmu_partition_support ()) +return -EPERM ; + +kvm_pmu_partition_enable (kvm, enable); +return 0 ; + }case KVM_ARM_VCPU_PMU_V3_INIT :return kvm_arm_pmu_v3_init (vcpu);}arch/arm64/kvm/pmu.c +1
-0@@ -798,6 +811,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@@ -109,6 +109,8 @@ void kvm_pmu_load(struct kvm_vcpu *vcpu);void kvm_pmu_put (struct kvm_vcpu *vcpu);void kvm_pmu_set_physical_access (struct kvm_vcpu *vcpu); +bool has_kvm_pmu_partition_support (void ); +void kvm_pmu_partition_enable (struct kvm *kvm,bool enable);#if !defined(__KVM_NVHE_HYPERVISOR__)bool kvm_vcpu_pmu_is_partitioned (struct kvm_vcpu *vcpu);@@ -311,6 +313,13 @@ static inline voidkvm_pmu_host_counters_enable (void) {}static inline void kvm_pmu_host_counters_disable (void ) {}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
PATCH v6 17/19 KVM: selftests: Add find_bit to KVM library
In reply to Colton LewisSome 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 | 1 + 2 files changed, 2 insertions(+) create mode 100644 tools/testing/selftests/kvm/lib/find_bit.c
tools/testing/selftests/kvm/Makefile.kvm +1 -0
@@ -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 +1 -0
@@ -0,0 +1 @@ +#include "../../../../lib/find_bit.c"
Signature · 1 line
2.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 18/19 KVM: arm64: selftests: Add test case for partitioned PMU
In reply to Colton LewisRerun 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
@@ -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(uint64_t 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;uint8_t pmuver, ec;
@@ -409,6 +420,13 @@ static void create_vpmu_vm(void *guest_code) .attr = KVM_ARM_VCPU_PMU_V3_IRQ, .addr = (uint64_t)&irq, }; +bool 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, uint64_t 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 (uint64_t pmcr_n) +static void run_access_test (uint64_t pmcr_n,enum pmu_impl impl) {uint64_t sp;struct kvm_vcpu *vcpu;
@@ -497,7 +525,7 @@ static void run_access_test(uint64_t 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 (uint64_t pmcr_n) +static void run_pmregs_validity_test (uint64_t pmcr_n,enum pmu_impl impl) {int i;struct kvm_vcpu *vcpu;uint64_t set_reg_id, clr_reg_id, reg_val;uint64_t 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(uint64_t 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 (uint64_t pmcr_n) +static void run_error_test (uint64_t 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(uint64_t 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 uint64_t get_pmcr_n_limit (void ) +static uint64_t get_pmcr_n_limit (enum pmu_impl impl) {uint64_t 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) {uint64_t 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.53.0.rc2.204.g2597b5adb4-goog
PATCH v6 19/19 KVM: arm64: selftests: Relax testing for exceptions when partitioned
In reply to Colton LewisBecause 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
@@ -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 {uint64_t 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.53.0.rc2.204.g2597b5adb4-goog
Marc Zyngier
maintainerQuoted 6 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.
Quoted 7 lines
An overview of what this series accomplishes was presented at KVM Forum 2025. Slides [1] and video [2] are linked below.
IMPORTANT: This iteration does not yet implement the dynamic counter reservation approach suggested by Will Deacon in January [3]. I am working on it, but wanted to send this version first to keep momentum going and ensure I've addressed all issues besides that.
[...]
As I have asked before, this is missing an example of how userspace is going to use this. Without it, it is impossible to correctly review this series.
Please consider this as a blocker.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Hey Marc, thanks for the review.
Quoted 16 lines · Marc Zyngier
Quoted 5 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.
Quoted 7 lines
An overview of what this series accomplishes was presented at KVM Forum 2025. Slides [1] and video [2] are linked below.
IMPORTANT: This iteration does not yet implement the dynamic counter reservation approach suggested by Will Deacon in January [3]. I am working on it, but wanted to send this version first to keep momentum going and ensure I've addressed all issues besides that.
Quoted 7 lines
[...]
As I have asked before, this is missing an example of how userspace is going to use this. Without it, it is impossible to correctly review this series.
Please consider this as a blocker.
Understood. I remember you asking for a QEMU patch specifically.
I had hoped that the use in the selftest was sufficient to show how to use the uAPI. If not, I can send out an example QEMU patch to the QEMU ARM mailing list.
Quoted 6 lines
Thanks,
M.--
Without deviation from the norm, progress is not possible.
Marc Zyngier
maintainerQuoted 23 lines · Colton Lewis
Hey Marc, thanks for the review.
Quoted 17 lines · Marc Zyngier
Quoted 12 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.
IMPORTANT: This iteration does not yet implement the dynamic counter reservation approach suggested by Will Deacon in January [3]. I am working on it, but wanted to send this version first to keep momentum going and ensure I've addressed all issues besides that.
[...]
Quoted 7 lines
Quoted 5 lines
As I have asked before, this is missing an example of how userspace is going to use this. Without it, it is impossible to correctly review this series.
Please consider this as a blocker.
Understood. I remember you asking for a QEMU patch specifically.
No. *any* VMM. QEMU, kvmtool, crosvm, firecrackpoter, whichever you want.
Quoted 2 lines
I had hoped that the use in the selftest was sufficient to show how to use the uAPI.
The selftests are absolutely pointless, like 99% of all selftests. They don't demonstrate how the userspace API works, now how configuring the PMU is ordered with the rest of the save/restore flow.
Quoted 2 lines
If not, I can send out an example QEMU patch to the QEMU ARM mailing list.
Please do.
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Quoted 33 lines · Marc Zyngier
Quoted 22 lines · Colton Lewis
Hey Marc, thanks for the review.
Quoted 17 lines · Marc Zyngier
Quoted 12 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.
IMPORTANT: This iteration does not yet implement the dynamic counter reservation approach suggested by Will Deacon in January [3]. I am working on it, but wanted to send this version first to keep momentum going and ensure I've addressed all issues besides that.
[...]
Quoted 7 lines
Quoted 5 lines
As I have asked before, this is missing an example of how userspace is going to use this. Without it, it is impossible to correctly review this series.
Please consider this as a blocker.
Understood. I remember you asking for a QEMU patch specifically.
Quoted 11 lines
No. *any* VMM. QEMU, kvmtool, crosvm, firecrackpoter, whichever you want.
Quoted 2 lines
I had hoped that the use in the selftest was sufficient to show how to use the uAPI.
The selftests are absolutely pointless, like 99% of all selftests. They don't demonstrate how the userspace API works, now how configuring the PMU is ordered with the rest of the save/restore flow.
Quoted 2 lines
If not, I can send out an example QEMU patch to the QEMU ARM mailing list.
Okay I sent one to you, qemu-arm, and everyone else I asked to review this series.