KVM: arm64: Expose PMMIR_EL1.SLOTS to guests
Subsystems (5)
v3 cover KVM: arm64: Expose PMMIR_EL1.SLOTS to guests
Today when the perf tool runs in a guest on cores with PMUv3p4, it fails to parse the default metrics with "Failure to read '#slots'", since perf can only read 0 from sysfs caps/slots, which is backed by PMMIR_EL1.SLOTS that KVM traps as RAZ/WI.
Taking into account backward compatibility and heterogeneous systems, the exposure of PMMIR_EL1.SLOTS is gated behind a new vCPU feature flag:
- Patch 1 exposes PMMIR_EL1.SLOTS of the selected PMU under the (as yet unsettable) KVM_ARM_VCPU_PMU_V3_STRICT flag, and adds userspace get/set for PMMIR_EL1 so that SLOTS can be reset to 0 for backward compatibility.
- Patch 2 stops masking STALL_SLOT* in PMCEID1 under the flag.
- Patch 3 makes strict-mode userspace configure the number of event counters via the KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS attribute rather than PMCR_EL0.N, whose meaning is context-dependent under NV.
- Patch 4 adds the KVM_ARM_VCPU_PMU_V3_STRICT flag itself: when set, KVM does not create a default PMU during vCPU init, and the VMM must select one explicitly via KVM_ARM_VCPU_PMU_V3_SET_PMU before the first KVM_RUN. This flips the gate on and can serve as an umbrella flag for future PMUv3 UAPI changes.
When the flag is not set, behaviors are unchanged.
v2: https://lore.kernel.org/r/[email protected]
v1: https://lore.kernel.org/r/[email protected]
v2 -> v3 changes:
- Reordered so the KVM_ARM_VCPU_PMU_V3_STRICT flag / KVM_CAP exposure comes last (now patch 4), after the flag-guarded behavior patches.
- Reworked on top of Oliver Upton's refinements (thanks!):
* Move the NULL-arm_pmu check into kvm_arm_pmu_v3_init() (returns -ENXIO) instead of failing at the first KVM_RUN, and report zero counters from kvm_arm_pmu_get_max_counters() before a PMU is set.
* Documentation and -ENXIO ordering notes for the PMU device attributes (SET_PMU must precede INIT/FILTER).
- New patch (Oliver): ignore userspace writes to PMCR_EL0.N under the strict flag and require the KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS attribute, since PMCR_EL0.N is context-dependent under NV.
- Advertise the KVM_CAP_ARM_PMU_V3_STRICT capability.
- Documentation fixes in api.rst (the flag Requires KVM_ARM_VCPU_PMU_V3).
Congkai Tan (3):
KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI
KVM: arm64: Advertise STALL_SLOT* in PMCEID1 under strict PMUv3 UAPI
KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature
Oliver Upton (1):
KVM: arm64: Ignore writes to PMCR_EL0.N when using strict UAPI
Documentation/virt/kvm/api.rst | 11 ++++ Documentation/virt/kvm/devices/vcpu.rst | 11 +++- arch/arm64/include/asm/kvm_host.h | 5 +- arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/arm.c | 19 ++++-- arch/arm64/kvm/pmu-emul.c | 54 ++++++++++++---- arch/arm64/kvm/sys_regs.c | 64 ++++++++++++++++++- include/kvm/arm_pmu.h | 4 ++ include/uapi/linux/kvm.h | 1 + .../selftests/kvm/arm64/get-reg-list.c | 1 + 10 files changed, 149 insertions(+), 22 deletions(-)
base-commit: 8cdeaa50eae8dad34885515f62559ee83e7e8dda
Signature · 1 line
2.50.1
PATCH v3 1/4 KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI
Introduce a new field pmmir_slots in struct kvm_arch to store PMMIR_EL1.SLOTS. It only saves the actual hardware PMU value when the VMM explicitly selects a PMU under KVM_ARM_VCPU_PMU_V3_STRICT. Otherwise, it stays 0 after allocation.
Use this field to implement guest access, userspace get, and userspace set for PMMIR_EL1:
- access_pmmir(): uses the value in kvm->arch.pmmir_slots directly. If the VMM selected a PMU and KVM_ARM_VCPU_PMU_V3_STRICT is set, the guest can correctly read the underlying core's SLOTS. Otherwise, it continues to read 0 since the true SLOTS value can be nondeterministic.
- get_pmmir(): same as access_pmmir().
- set_pmmir(): only the SLOTS field is writable; a value setting any other bit is rejected with -EINVAL, since get_pmmir() returns SLOTS zero-extended. A value of 0 resets kvm->arch.pmmir_slots to 0 for backward compatibility, as the register is RAZ in older KVM, a value matching the current SLOTS is accepted as a no-op, and anything else is rejected with -EINVAL. Once the VM has run PMMIR_EL1 is immutable, so a mismatching write then returns -EBUSY.
The register is now exposed via KVM_GET_REG_LIST for PMUv3 vCPUs, so add it to the get-reg-list selftest's PMU register list.
--- arch/arm64/include/asm/kvm_host.h | 3 + arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/pmu-emul.c | 11 ++++ arch/arm64/kvm/sys_regs.c | 63 ++++++++++++++++++- include/kvm/arm_pmu.h | 4 ++ .../selftests/kvm/arm64/get-reg-list.c | 1 + 6 files changed, 81 insertions(+), 2 deletions(-)
arch/arm64/include/asm/kvm_host.h +3 -0
@@ -387,6 +387,9 @@ struct kvm_arch {/* Maximum number of counters for the guest */ u8 nr_pmu_counters; +/* PMMIR_EL1.SLOTS value exposed to the guest. */ +u8 pmmir_slots; +/* Hypercall features firmware registers' descriptor */ struct kvm_smccc_features smccc_feat;struct maple_tree smccc_filter;
arch/arm64/include/uapi/asm/kvm.h +1 -0
@@ -106,6 +106,7 @@ struct kvm_regs {#define KVM_ARM_VCPU_PTRAUTH_GENERIC 6/* VCPU uses generic authentication */ #define KVM_ARM_VCPU_HAS_EL2 7/* Support nested virtualization */ #define KVM_ARM_VCPU_HAS_EL2_E2H0 8/* Limit NV support to E2H RES0 */ +#define KVM_ARM_VCPU_PMU_V3_STRICT 9/* No default PMU creation */ struct kvm_vcpu_init {__u32 target;
arch/arm64/kvm/pmu-emul.c +11 -0
@@ -1092,6 +1092,17 @@ static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id)kvm_arm_set_pmu (kvm, arm_pmu);cpumask_copy (kvm->arch.supported_cpus, &arm_pmu->supported_cpus); + +/* +* Since a specific PMU is explicitly selected, +* PMMIR_EL1.SLOTS is deterministic to the guest. +* If KVM_ARM_VCPU_PMU_V3_STRICT is set, snapshot +* the value to allow the guest to read it. +*/ +if (kvm_vcpu_has_pmuv3_strict (vcpu)) + kvm->arch.pmmir_slots = +FIELD_GET (ARMV8_PMU_SLOTS , + arm_pmu->reg_pmmir); ret =0 ;break ; }
arch/arm64/kvm/sys_regs.c +61 -2
81 lines not on this page · raw message
include/kvm/arm_pmu.h +4 -0
@@ -75,6 +75,9 @@ void kvm_vcpu_pmu_resync_el0(void);#define kvm_vcpu_has_pmu (vcpu) \ (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3)) +#define kvm_vcpu_has_pmuv3_strict (vcpu) \ + (vcpu_has_feature(vcpu, KVM_ARM_VCPU_PMU_V3_STRICT)) +/* * Updates the vcpu's view of the pmu events for this cpu. * Must be called before every vcpu run after disabling interrupts, to ensure
@@ -160,6 +163,7 @@ static inline u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1) }#define kvm_vcpu_has_pmu (vcpu) ({ false; }) +#define kvm_vcpu_has_pmuv3_strict (vcpu) ({ false; })static inline void kvm_pmu_update_vcpu_events (struct kvm_vcpu *vcpu) {}static inline void kvm_vcpu_pmu_restore_guest (struct kvm_vcpu *vcpu) {}static inline void kvm_vcpu_pmu_restore_host (struct kvm_vcpu *vcpu) {}
tools/testing/selftests/kvm/arm64/get-reg-list.c +1 -0
@@ -532,6 +532,7 @@ static __u64 base_regs[] = {static __u64 pmu_regs[] = {ARM64_SYS_REG (3 ,0 ,9 ,14 ,1 ),/* PMINTENSET_EL1 */ ARM64_SYS_REG (3 ,0 ,9 ,14 ,2 ),/* PMINTENCLR_EL1 */ +ARM64_SYS_REG (3 ,0 ,9 ,14 ,6 ),/* PMMIR_EL1 */ ARM64_SYS_REG (3 ,3 ,9 ,12 ,0 ),/* PMCR_EL0 */ ARM64_SYS_REG (3 ,3 ,9 ,12 ,1 ),/* PMCNTENSET_EL0 */ ARM64_SYS_REG (3 ,3 ,9 ,12 ,2 ),/* PMCNTENCLR_EL0 */
Signature · 1 line
2.50.1
[email protected]
AI reviewRe: [PATCH v3 1/4] KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] The implementation of `KVM_ARM_VCPU_PMU_V3_STRICT` introduces two logic errors that completely…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The implementation of KVM_ARM_VCPU_PMU_V3_STRICT introduces two logic errors that completely break the new feature and its API contract:
1. KVM_VCPU_MAX_FEATURES was not increased from 9 to 10 to accommodate the new flag (which is defined as 9). As a result, KVM_VCPU_VALID_FEATURES excludes bit 9, causing kvm_vcpu_init_check_features() to unconditionally reject KVM_ARM_VCPU_INIT ioctls with -ENOENT when this feature is requested.
2. Even if the feature validation were fixed, the code fails to enforce the documented 'No default PMU creation' behavior. kvm_setup_vcpu() unconditionally calls kvm_arm_set_default_pmu() if kvm_vcpu_has_pmu() is true, without checking if the strict flag is set. Thus, KVM will always assign a default PMU on initialization, defeating the stated purpose of the flag. --
--- Patch [1]: [PATCH v3 1/4] KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI --- commit 5883b44965f93aed147ca4ed5fb390ae481daa37
Author: Congkai Tan <[email protected]>
KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI
This commit introduces a new VCPU feature flag KVM_ARM_VCPU_PMU_V3_STRICT to prevent KVM from automatically creating a default PMU, and exposes the PMMIR_EL1.SLOTS value to the guest when a PMU is explicitly selected.
arch/arm64/include/uapi/asm/kvm.h struct kvm_regs { · line 109
Quoted 9 lines
arch/arm64/include/uapi/asm/kvm.h +1
-0@@ -106,6 +106,7 @@ struct kvm_regs {#define KVM_ARM_VCPU_PTRAUTH_GENERIC 6/* VCPU uses generic authentication */ #define KVM_ARM_VCPU_HAS_EL2 7/* Support nested virtualization */ #define KVM_ARM_VCPU_HAS_EL2_E2H0 8/* Limit NV support to E2H RES0 */ +#define KVM_ARM_VCPU_PMU_V3_STRICT 9/* No default PMU creation */
[Severity: High]
Does KVM_VCPU_MAX_FEATURES need to be updated to 10 to support this new flag?
Currently, KVM_VCPU_MAX_FEATURES in arch/arm64/include/asm/kvm_host.h is 9, so KVM_VCPU_VALID_FEATURES (BIT(KVM_VCPU_MAX_FEATURES) - 1) only covers bits 0 through 8. When userspace attempts to use this new flag (bit 9), kvm_vcpu_init_check_features() will reject it with -ENOENT.
Additionally, does this patch actually prevent default PMU creation?
Looking at kvm_setup_vcpu() in arch/arm64/kvm/arm.c:
if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu) ret = kvm_arm_set_default_pmu(kvm);
It appears that kvm_arm_set_default_pmu() will still be called on initialization even if the strict flag is set, since there is no check for kvm_vcpu_has_pmuv3_strict(vcpu) here.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Fuad Tabba
reviewerRe: [PATCH v3 1/4] KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI
Quoted 5 lines · On Wed, 22 Jul 2026 at 21:50, <[email protected]> wrote:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] The implementation ofKVM_ARM_VCPU_PMU_V3_STRICTintroduces two logic errors that completely break the new feature and its API contract:
1.KVM_VCPU_MAX_FEATURESwas not increased from 9 to 10 to accommodate the new flag (which is defined as 9). As a result,KVM_VCPU_VALID_FEATURESexcludes bit 9, causingkvm_vcpu_init_check_features()to unconditionally rejectKVM_ARM_VCPU_INITioctls with-ENOENTwhen this feature is requested.
2. Even if the feature validation were fixed, the code fails to enforce the documented 'No default PMU creation' behavior.kvm_setup_vcpu()unconditionally callskvm_arm_set_default_pmu()ifkvm_vcpu_has_pmu()is true, without checking if the strict flag is set. Thus, KVM will always assign a default PMU on initialization, defeating the stated purpose of the flag.
Both are done in patch 4: it bumps KVM_VCPU_MAX_FEATURES to 10 and adds the !kvm_vcpu_has_pmuv3_strict() check in kvm_setup_vcpu(). Until then the flag stays unsettable by design (INIT rejects bit 9 with -ENOENT), so patches 1-3 are inert and there is no half-enabled state.
/fuad
Quoted 44 lines
--
--- Patch [1]: [PATCH v3 1/4] KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI --- commit
5883b44965f93aed147ca4ed5fb390ae481daa37
Author: Congkai Tan <[email protected]>KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI
This commit introduces a new VCPU feature flag
KVM_ARM_VCPU_PMU_V3_STRICTto prevent KVM from automatically creating a default PMU, and exposes the PMMIR_EL1.SLOTS value to the guest when a PMU is explicitly selected.arch/arm64/include/uapi/asm/kvm.h struct kvm_regs { · line 109
Quoted 9 lines
arch/arm64/include/uapi/asm/kvm.h +1
-0@@ -106,6 +106,7 @@ struct kvm_regs {#define KVM_ARM_VCPU_PTRAUTH_GENERIC 6/* VCPU uses generic authentication */ #define KVM_ARM_VCPU_HAS_EL2 7/* Support nested virtualization */ #define KVM_ARM_VCPU_HAS_EL2_E2H0 8/* Limit NV support to E2H RES0 */ +#define KVM_ARM_VCPU_PMU_V3_STRICT 9/* No default PMU creation */ [Severity: High]
DoesKVM_VCPU_MAX_FEATURESneed to be updated to 10 to support this new flag?Currently,
KVM_VCPU_MAX_FEATURESinarch/arm64/include/asm/kvm_host.his 9, soKVM_VCPU_VALID_FEATURES(BIT(KVM_VCPU_MAX_FEATURES)- 1) only covers bits 0 through 8. When userspace attempts to use this new flag (bit 9),kvm_vcpu_init_check_features()will reject it with-ENOENT.Additionally, does this patch actually prevent default PMU creation?
Looking at
kvm_setup_vcpu()inarch/arm64/kvm/arm.c:if (kvm_vcpu_has_pmu(vcpu) && !kvm->arch.arm_pmu) ret = kvm_arm_set_default_pmu(kvm);It appears that
kvm_arm_set_default_pmu()will still be called on initialization even if the strict flag is set, since there is no check forkvm_vcpu_has_pmuv3_strict(vcpu)here.--
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v3 2/4 KVM: arm64: Advertise STALL_SLOT* in PMCEID1 under strict PMUv3 UAPI
In reply to Congkai TanSkip masking STALL_SLOT, STALL_SLOT_FRONTEND and STALL_SLOT_BACKEND out of PMCEID1 when KVM_ARM_VCPU_PMU_V3_STRICT is set, because this is when PMMIR_EL1.SLOTS is exposed to guests, making these events meaningful for collection.
Change the parameter of compute_pmceid1() from arm_pmu to kvm_vcpu, to check if KVM_ARM_VCPU_PMU_V3_STRICT is set. Also updated the signature of compute_pmceid0() for consistency.
--- arch/arm64/kvm/pmu-emul.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-)
arch/arm64/kvm/pmu-emul.c +13 -12
@@ -838,9 +838,9 @@ static u64 __compute_pmceid(struct arm_pmu *pmu, bool pmceid1)return ((u64 )hi[pmceid1] <<32 ) | lo[pmceid1]; } -static u64 compute_pmceid0 (struct arm_pmu *pmu) +static u64 compute_pmceid0 (struct kvm_vcpu *vcpu) { -u64 val =__compute_pmceid (pmu,0 ); +u64 val =__compute_pmceid (vcpu->kvm->arch.arm_pmu,0 );/* always support SW_INCR */ val |=BIT (ARMV8_PMUV3_PERFCTR_SW_INCR );
@@ -849,32 +849,33 @@ static u64 compute_pmceid0(struct arm_pmu *pmu)return val; } -static u64 compute_pmceid1 (struct arm_pmu *pmu) +static u64 compute_pmceid1 (struct kvm_vcpu *vcpu) { -u64 val =__compute_pmceid (pmu,1 ); +u64 val =__compute_pmceid (vcpu->kvm->arch.arm_pmu,1 );/* -* Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled -* as RAZ +* If KVM_ARM_VCPU_PMU_V3_STRICT is not set, PMMIR_EL1 is +* unconditionally RAZ, so don't advertise STALL_SLOT* events. */ - 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 )); +if (!kvm_vcpu_has_pmuv3_strict (vcpu)) + val &= ~(BIT_ULL (ARMV8_PMUV3_PERFCTR_STALL_SLOT -32 ) | +BIT_ULL (ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND -32 ) | +BIT_ULL (ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND -32 )); +return val; }u64 kvm_pmu_get_pmceid (struct kvm_vcpu *vcpu,bool pmceid1) { -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); + val =compute_pmceid0 (vcpu); base =0 ; }else { - val =compute_pmceid1 (cpu_pmu); + val =compute_pmceid1 (vcpu); base =32 ; }
Signature · 1 line
2.50.1
PATCH v3 3/4 KVM: arm64: Ignore writes to PMCR_EL0.N when using strict UAPI
In reply to Congkai TanFrom: Oliver Upton <[email protected]>
KVM allows userspace to configure the number of event counters by writing to PMCR_EL0.N. While this makes a bit of sense prior to NV, the value of the field is context-dependent whether or not the vCPU is at EL2 or EL1. As such, the vCPU attribute was added afterwards and made mandatory for NV.
As we're in the process of papering over old mistakes, force userspace to use the vCPU attribute if KVM_ARM_VCPU_PMU_V3_STRICT is set.
--- arch/arm64/kvm/sys_regs.c | 1 + 1 file changed, 1 insertion(+)
arch/arm64/kvm/sys_regs.c +1 -0
@@ -1502,6 +1502,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r,*/ if (!kvm_vm_has_ran_once (kvm) && !vcpu_has_nv (vcpu) && + !kvm_vcpu_has_pmuv3_strict (vcpu) && new_n <=kvm_arm_pmu_get_max_counters (kvm)) kvm->arch.nr_pmu_counters = new_n;
Signature · 1 line
2.50.1
PATCH v3 4/4 KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature
In reply to Congkai TanIntroduce a new vCPU feature KVM_ARM_VCPU_PMU_V3_STRICT. When set, KVM does not create a default PMU when initializing the vCPU, and userspace must select one explicitly via KVM_ARM_VCPU_PMU_V3_SET_PMU before the first KVM_RUN.
The flag forces the VMM to be aware of the PMU implementation of the guest to be created, so that certain information about the PMU becomes deterministic (if on a heterogeneous system) and becomes safe to be exposed to the guest. It can be used as an umbrella flag to gate future PMUv3 UAPI changes.
When no default PMU is created, kvm->arch.arm_pmu stays NULL until SET_PMU runs, so kvm_arm_pmu_v3_init() now refuses to run if kvm->arch.arm_pmu is NULL.
--- Documentation/virt/kvm/api.rst | 11 +++++++++++ Documentation/virt/kvm/devices/vcpu.rst | 11 +++++++++-- arch/arm64/include/asm/kvm_host.h | 2 +- arch/arm64/kvm/arm.c | 19 +++++++++++++++---- arch/arm64/kvm/pmu-emul.c | 18 +++++++++++++++++- include/uapi/linux/kvm.h | 1 + 6 files changed, 54 insertions(+), 8 deletions(-)
Documentation/virt/kvm/api.rst +11 -0
@@ -3515,6 +3515,17 @@ Possible features: Depends on KVM_CAP_ARM_PSCI_0_2. - KVM_ARM_VCPU_PMU_V3: Emulate PMUv3 for the CPU. Depends on KVM_CAP_ARM_PMU_V3. + - KVM_ARM_VCPU_PMU_V3_STRICT: Enable strict PMUv3 UAPI. + Requires KVM_ARM_VCPU_PMU_V3. Depends on KVM_CAP_ARM_PMU_V3_STRICT. + When enabled: + + * Userspace must explicitly select a PMU implementation before + initializing the PMU or configuring a PMU event filter + + * If the PMU implements FEAT_PMUv3p4, PMMIR_EL1.SLOTS provides the + hardware value of the underlying implementation + + * Writes to PMCR_EL0.N via KVM_SET_ONE_REG are ignored - KVM_ARM_VCPU_PTRAUTH_ADDRESS: Enables Address Pointer authentication for arm64 only.
Documentation/virt/kvm/devices/vcpu.rst +9 -2
@@ -53,8 +53,9 @@ Returns: ======= ====================================================== -EEXIST Interrupt number already used -ENODEV PMUv3 not supported or GIC not initialized - -ENXIO PMUv3 not supported, missing VCPU feature or interrupt - number not set (non-GICv5 guests, only) + -ENXIO PMUv3 not supported, missing VCPU feature, missing + hardware PMU, or interrupt number not set (non-GICv5 + guests, only) -EBUSY PMUv3 already initialized ======= ======================================================
@@ -62,6 +63,9 @@ Request the initialization of the PMUv3. If using the PMUv3 with an in-kernel virtual GIC implementation, this must be done after initializing the in-kernel irqchip. +When the KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature is enabled this must be done +after selecting a hardware PMU. + 1.3 ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_FILTER -----------------------------------------
@@ -108,6 +112,9 @@ hardware event. Filtering event 0x1E (CHAIN) has no effect either, as it isn't strictly speaking an event. Filtering the cycle counter is possible using event 0x11 (CPU_CYCLES). +When the KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature is enabled this must be done +after selecting a hardware PMU. + 1.4 ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_SET_PMU ------------------------------------------
arch/arm64/include/asm/kvm_host.h +1 -1
@@ -39,7 +39,7 @@#define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS -#define KVM_VCPU_MAX_FEATURES 9 +#define KVM_VCPU_MAX_FEATURES 10#define KVM_VCPU_VALID_FEATURES (BIT(KVM_VCPU_MAX_FEATURES) - 1)#define KVM_REQ_SLEEP \
arch/arm64/kvm/arm.c +15 -4
@@ -452,6 +452,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =get_num_wrps ();break ;case KVM_CAP_ARM_PMU_V3 : +case KVM_CAP_ARM_PMU_V3_STRICT : r =kvm_supports_guest_pmuv3 ();break ;case KVM_CAP_ARM_INJECT_SERROR_ESR :
@@ -1563,8 +1564,10 @@ static unsigned long system_supported_vcpu_features(void)if (!cpus_have_final_cap (ARM64_HAS_32BIT_EL1 ))clear_bit (KVM_ARM_VCPU_EL1_32BIT , &features); -if (!kvm_supports_guest_pmuv3 ()) +if (!kvm_supports_guest_pmuv3 ()) {clear_bit (KVM_ARM_VCPU_PMU_V3 , &features); +clear_bit (KVM_ARM_VCPU_PMU_V3_STRICT , &features); + }if (!system_supports_sve ())clear_bit (KVM_ARM_VCPU_SVE , &features);
@@ -1605,6 +1608,11 @@ static int kvm_vcpu_init_check_features(struct kvm_vcpu *vcpu, test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, &features))return -EINVAL ; +/* Strict PMUv3 UAPI requires PMUv3. */ +if (test_bit (KVM_ARM_VCPU_PMU_V3_STRICT , &features) && + !test_bit (KVM_ARM_VCPU_PMU_V3 , &features)) +return -EINVAL ; +if (!test_bit (KVM_ARM_VCPU_EL1_32BIT , &features))return 0 ;
@@ -1634,10 +1642,13 @@ static int kvm_setup_vcpu(struct kvm_vcpu *vcpu)int ret =0 ;/* -* When the vCPU has a PMU, but no PMU is set for the guest -* yet, set the default one. +* When the vCPU has a PMU, but no PMU is set for the guest yet, set +* the default one. If KVM_ARM_VCPU_PMU_V3_STRICT is set, no default +* PMU is created, and userspace must select a PMU via +* KVM_ARM_VCPU_PMU_V3_SET_PMU. */ -if (kvm_vcpu_has_pmu (vcpu) && !kvm->arch.arm_pmu) +if (kvm_vcpu_has_pmu (vcpu) && !kvm->arch.arm_pmu && + !kvm_vcpu_has_pmuv3_strict (vcpu)) ret =kvm_arm_set_default_pmu (kvm);/* Prepare for nested if required */
arch/arm64/kvm/pmu-emul.c +17 -1
@@ -939,6 +939,10 @@ int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)static int kvm_arm_pmu_v3_init (struct kvm_vcpu *vcpu) { +/* Only possible when using KVM_ARM_VCPU_PMU_V3_STRICT */ +if (!vcpu->kvm->arch.arm_pmu) +return -ENXIO ; +if (irqchip_in_kernel (vcpu->kvm)) {int ret;
@@ -1009,6 +1013,14 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm) {struct arm_pmu *arm_pmu = kvm->arch.arm_pmu; +/* +* Under KVM_ARM_VCPU_PMU_V3_STRICT no PMU exists until userspace sets +* one, so this can be reached before arm_pmu is set. Report no +* counters in that case. +*/ +if (!arm_pmu) +return 0 ; +/* * PMUv3 requires that all event counters are capable of counting any * event, though the same may not be true of non-PMUv3 hardware.
@@ -1050,7 +1062,8 @@ static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) }/** -* kvm_arm_set_default_pmu - No PMU set, get the default one. +* kvm_arm_set_default_pmu - No PMU set and KVM_ARM_VCPU_PMU_V3_STRICT not +* set, get the default one. * @kvm: The kvm pointer * * The observant among you will notice that the supported_cpus
@@ -1190,6 +1203,9 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)if (kvm_vm_has_ran_once (kvm))return -EBUSY ; +if (!kvm->arch.arm_pmu) +return -ENXIO ; +if (!kvm->arch.pmu_filter) { kvm->arch.pmu_filter =bitmap_alloc (nr_events,GFP_KERNEL_ACCOUNT );if (!kvm->arch.pmu_filter)
include/uapi/linux/kvm.h +1 -0
@@ -997,6 +997,7 @@ struct kvm_enable_cap {#define KVM_CAP_S390_KEYOP 247#define KVM_CAP_S390_VSIE_ESAMODE 248#define KVM_CAP_S390_HPAGE_2G 249 +#define KVM_CAP_ARM_PMU_V3_STRICT 250struct kvm_irq_routing_irqchip {__u32 irqchip;
Signature · 1 line
2.50.1
On Wed, 22 Jul 2026 at 21:27, Congkai Tan <[email protected]> wrote:
...
Quoted 8 lines
Congkai Tan (3):
KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI
KVM: arm64: Advertise STALL_SLOT* in PMCEID1 under strict PMUv3 UAPI
KVM: arm64: AddKVM_ARM_VCPU_PMU_V3_STRICTvCPU featureOliver Upton (1):
KVM: arm64: Ignore writes to PMCR_EL0.N when using strict UAPI
Tested on QEMU: boot, get-reg-list, and vpmu_counter_access pass. This doesn't exercise the KVM_ARM_VCPU_PMU_V3_STRICT runtime path, and the series adds no functional selftest for the flag.
For the series:
Cheers,
/fuad
Quoted 9 lines · Congkai Tan
Today when the perf tool runs in a guest on cores with PMUv3p4, it fails to parse the default metrics with "Failure to read '#slots'", since perf can only read 0 from sysfs caps/slots, which is backed by PMMIR_EL1.SLOTS that KVM traps as RAZ/WI.
Taking into account backward compatibility and heterogeneous systems, the exposure of PMMIR_EL1.SLOTS is gated behind a new vCPU feature flag:
[...]
Applied to next, thanks!
[1/4] KVM: arm64: Expose PMMIR_EL1.SLOTS under strict PMUv3 UAPI
https://git.kernel.org/kvmarm/kvmarm/c/453f6ff04b8d [2/4] KVM: arm64: Advertise STALL_SLOT* in PMCEID1 under strict PMUv3 UAPI https://git.kernel.org/kvmarm/kvmarm/c/203b22f76dc3 [3/4] KVM: arm64: Ignore writes to PMCR_EL0.N when using strict UAPI https://git.kernel.org/kvmarm/kvmarm/c/bbef6317f97b [4/4] KVM: arm64: Add KVM_ARM_VCPU_PMU_V3_STRICT vCPU feature https://git.kernel.org/kvmarm/kvmarm/c/ad220e275c39
--
Best,
Oliver