The initial KVM_ARM_VCPU_INIT path holds config_lock while resetting the vCPU, but repeated initialization of an existing vCPU does not. As a result, reset_mdcr() can race with VM-wide PMU configuration and overwrite MDCR_EL2 with a stale counter count. Take config_lock around the repeated-init checks and reset. The vCPU ioctl already holds vcpu->mutex, matching the established lock ordering. Fixes: c8823e51b534 ("KVM: arm64: Fix MDCR_EL2.HPMN reset value") Closes: https://sashiko.dev/#/patchset/20260710-hybrid-v8-0-621409f3a592@rsg.ci.i.u-tokyo.ac.jp?part=1 Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- arch/arm64/kvm/arm.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index ccae82c1242b..8eff03c32418 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -1670,29 +1670,26 @@ static int __kvm_vcpu_set_target(struct kvm_vcpu *vcpu, { unsigned long features = init->features[0]; struct kvm *kvm = vcpu->kvm; - int ret = -EINVAL; + int ret; - mutex_lock(&kvm->arch.config_lock); + lockdep_assert_held(&kvm->arch.config_lock); if (test_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags) && kvm_vcpu_init_changed(vcpu, init)) - goto out_unlock; + return -EINVAL; bitmap_copy(kvm->arch.vcpu_features, &features, KVM_VCPU_MAX_FEATURES); ret = kvm_setup_vcpu(vcpu); if (ret) - goto out_unlock; + return ret; /* Now we know what it is, we can reset it. */ kvm_reset_vcpu(vcpu); set_bit(KVM_ARCH_FLAG_VCPU_FEATURES_CONFIGURED, &kvm->arch.flags); vcpu_set_flag(vcpu, VCPU_INITIALIZED); - ret = 0; -out_unlock: - mutex_unlock(&kvm->arch.config_lock); - return ret; + return 0; } static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, @@ -1708,6 +1705,8 @@ static int kvm_vcpu_set_target(struct kvm_vcpu *vcpu, if (ret) return ret; + guard(mutex)(&vcpu->kvm->arch.config_lock); + if (!kvm_vcpu_initialized(vcpu)) return __kvm_vcpu_set_target(vcpu, init); -- 2.55.0 KVM allows userspace to write any value to MDCR_EL2.HPMN. However, kvm_arm_set_nr_counters() rewrites HPMN for every vCPU whenever userspace changes the PMU or its counter count. This can discard a value previously restored with KVM_SET_ONE_REG. The architecture only defines HPMN's value on warm reset. Stop rewriting it after vCPU initialization and update nr_pmu_counters directly instead. reset_mdcr() continues to initialize HPMN from the counter count current at KVM_ARM_VCPU_INIT. Fixes: c8823e51b534 ("KVM: arm64: Fix MDCR_EL2.HPMN reset value") Closes: https://sashiko.dev/#/patchset/20260706-hybrid-v8-0-de459617b59d%40rsg.ci.i.u-tokyo.ac.jp?part=6 Assisted-by: Codex:gpt-5.5 Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- arch/arm64/kvm/pmu-emul.c | 22 ++-------------------- 1 file changed, 2 insertions(+), 20 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 5b1af7e2176f..832dd145af2a 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -1035,30 +1035,12 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm) 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->arch.nr_pmu_counters = kvm_arm_pmu_get_max_counters(kvm); } /** @@ -1136,7 +1118,7 @@ static int kvm_arm_pmu_v3_set_nr_counters(struct kvm_vcpu *vcpu, unsigned int n) if (n > kvm_arm_pmu_get_max_counters(kvm)) return -EINVAL; - kvm_arm_set_nr_counters(kvm, n); + kvm->arch.nr_pmu_counters = n; return 0; } -- 2.55.0 PMU configuration is VM-scoped, but SET_NR_COUNTERS can be issued through any vCPU. Checking only whether that vCPU's PMU has been initialized allows userspace to change the counter count through an idle sibling after another vCPU has run. Reject the attribute once any vCPU has run. This keeps the VM-wide implemented counter mask and the PMCR_EL0.N value stable after guest execution begins. Fixes: b7628c797376 ("KVM: arm64: Allow userspace to limit the number of PMU counters for EL2 VMs") Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- arch/arm64/kvm/pmu-emul.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 832dd145af2a..950b7a349659 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -1112,6 +1112,9 @@ static int kvm_arm_pmu_v3_set_nr_counters(struct kvm_vcpu *vcpu, unsigned int n) { struct kvm *kvm = vcpu->kvm; + if (kvm_vm_has_ran_once(kvm)) + return -EBUSY; + if (!kvm->arch.arm_pmu) return -EINVAL; -- 2.55.0 Add a regression test that runs one vCPU and then tries to change the VM-scoped PMU counter count through an idle sibling. Verify that SET_NR_COUNTERS fails with EBUSY once any vCPU has run. Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Akihiko Odaki --- .../selftests/kvm/arm64/vpmu_counter_access.c | 47 ++++++++++++++++++++-- 1 file changed, 43 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c b/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c index 22223395969e..05135bcdb462 100644 --- a/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +++ b/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c @@ -443,11 +443,10 @@ static void destroy_vpmu_vm(void) kvm_vm_free(vpmu_vm.vm); } -static void run_vcpu(struct kvm_vcpu *vcpu, u64 pmcr_n) +static void run_vcpu(struct kvm_vcpu *vcpu) { struct ucall uc; - vcpu_args_set(vcpu, 1, pmcr_n); vcpu_run(vcpu); switch (get_ucall(vcpu, &uc)) { case UCALL_ABORT: @@ -461,6 +460,11 @@ static void run_vcpu(struct kvm_vcpu *vcpu, u64 pmcr_n) } } +static void guest_code_done(void) +{ + GUEST_DONE(); +} + static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, bool expect_fail) { struct kvm_vcpu *vcpu; @@ -503,7 +507,8 @@ static void run_access_test(u64 pmcr_n) /* Save the initial sp to restore them later to run the guest again */ sp = vcpu_get_reg(vcpu, ctxt_reg_alias(vcpu, SYS_SP_EL1)); - run_vcpu(vcpu, pmcr_n); + vcpu_args_set(vcpu, 1, pmcr_n); + run_vcpu(vcpu); /* * Reset and re-initialize the vCPU, and run the guest code again to @@ -516,7 +521,8 @@ static void run_access_test(u64 pmcr_n) vcpu_set_reg(vcpu, ctxt_reg_alias(vcpu, SYS_SP_EL1), sp); vcpu_set_reg(vcpu, ARM64_CORE_REG(regs.pc), (u64)guest_code); - run_vcpu(vcpu, pmcr_n); + vcpu_args_set(vcpu, 1, pmcr_n); + run_vcpu(vcpu); destroy_vpmu_vm(); } @@ -622,6 +628,37 @@ static bool kvm_supports_nr_counters_attr(void) return supported; } +static void test_set_nr_counters_after_vcpu_run(void) +{ + struct kvm_vcpu *running_vcpu, *stopped_vcpu; + unsigned int nr_counters = 0; + struct kvm_vcpu_init init; + struct kvm_vm *vm; + int ret; + u64 irq = 23; + + vm = vm_create(2); + vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init); + init.features[0] |= BIT(KVM_ARM_VCPU_PMU_V3); + running_vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code_done); + stopped_vcpu = aarch64_vcpu_add(vm, 1, &init, guest_code_done); + kvm_arch_vm_finalize_vcpus(vm); + + vcpu_device_attr_set(running_vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_IRQ, &irq); + vcpu_device_attr_set(running_vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_INIT, NULL); + run_vcpu(running_vcpu); + + ret = __vcpu_device_attr_set(stopped_vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_SET_NR_COUNTERS, + &nr_counters); + TEST_ASSERT(ret == -1 && errno == EBUSY, + KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret)); + + kvm_vm_free(vm); +} + int main(void) { u64 i, pmcr_n; @@ -630,6 +667,8 @@ int main(void) TEST_REQUIRE(kvm_supports_vgic_v3()); TEST_REQUIRE(kvm_supports_nr_counters_attr()); + test_set_nr_counters_after_vcpu_run(); + pmcr_n = get_pmcr_n_limit(); for (i = 0; i <= pmcr_n; i++) { run_access_test(i); -- 2.55.0 kvm_pmu_implemented_counter_mask() derives the counter count from kvm_vcpu_read_pmcr(). For a nested vCPU outside EL2, PMCR_EL0.N reflects MDCR_EL2.HPMN instead of the VM-wide counter count. Consequently, processing a PMU reload while the saved context is L2 can clear and fail to recreate state for counters reserved for EL2. Build the implemented counter mask from nr_pmu_counters instead. Keep the EL-dependent HPMN restriction in kvm_pmu_accessible_counter_mask(), where it controls guest access. Fixes: 600f6fa5c90c ("KVM: arm64: Let kvm_vcpu_read_pmcr() return an EL-dependent value for PMCR_EL0.N") Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- arch/arm64/kvm/pmu-emul.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 950b7a349659..a5ccb6ae44d5 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -317,12 +317,12 @@ u64 kvm_pmu_accessible_counter_mask(struct kvm_vcpu *vcpu) u64 kvm_pmu_implemented_counter_mask(struct kvm_vcpu *vcpu) { - u64 val = FIELD_GET(ARMV8_PMU_PMCR_N, kvm_vcpu_read_pmcr(vcpu)); + u64 n = vcpu->kvm->arch.nr_pmu_counters; - if (val == 0) + if (n == 0) return BIT(ARMV8_PMU_CYCLE_IDX); else - return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX); + return GENMASK(n - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX); } static void kvm_pmc_enable_perf_event(struct kvm_pmc *pmc) -- 2.55.0 KVM_GET_ONE_REG and KVM_SET_ONE_REG transfer raw vCPU state for migration. Masking PMU bitmap registers with counters accessible at the current EL drops EL2-reserved bits when the vCPU is stopped in EL1 or EL0. Use the implemented counter mask for these userspace accesses so the complete bitmap state survives migration. Guest register accesses continue to use the accessible counter mask. Assisted-by: Codex:gpt-5.6-sol Fixes: 9a1c58cfefb0 ("KVM: arm64: nv: Adjust range of accessible PMCs according to HPMN") Closes: https://sashiko.dev/#/patchset/20260720-hybrid-v9-0-2b713ca1b5dc@rsg.ci.i.u-tokyo.ac.jp?part=5 Signed-off-by: Akihiko Odaki --- arch/arm64/kvm/sys_regs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 44aae52c473d..42b6705bc21d 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1326,7 +1326,7 @@ static bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p, static int set_pmreg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, u64 val) { - u64 mask = kvm_pmu_accessible_counter_mask(vcpu); + u64 mask = kvm_pmu_implemented_counter_mask(vcpu); __vcpu_assign_sys_reg(vcpu, r->reg, val & mask); kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); @@ -1336,7 +1336,7 @@ static int set_pmreg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, u64 va static int get_pmreg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, u64 *val) { - u64 mask = kvm_pmu_accessible_counter_mask(vcpu); + u64 mask = kvm_pmu_implemented_counter_mask(vcpu); *val = __vcpu_sys_reg(vcpu, r->reg) & mask; return 0; -- 2.55.0 kvm_reset_sys_regs() resets PMCR_EL0, PMEVTYPERn_EL0, PMCCFILTR_EL0, and MDCR_EL2 before requesting a PMU reload. A reload reuses existing perf events even when their sample periods, event selections, or filters no longer match the reset system registers. Add a deferred event-recreation request. It stops existing events on the vCPU thread before reprogramming counters. Use it during system-register reset so the next vCPU run rebuilds all backing events from reset state. Fixes: fe53538069bb ("KVM: arm64: PMU: Reload when resetting") Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Akihiko Odaki --- arch/arm64/kvm/pmu-emul.c | 15 +++++++++++++++ arch/arm64/kvm/sys_regs.c | 2 +- include/kvm/arm_pmu.h | 3 +++ 3 files changed, 19 insertions(+), 1 deletion(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index a5ccb6ae44d5..8530cc907b56 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -571,6 +571,12 @@ void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val) kvm_pmu_counter_increment(vcpu, val, ARMV8_PMUV3_PERFCTR_SW_INCR); } +void kvm_pmu_request_recreate(struct kvm_vcpu *vcpu) +{ + vcpu->arch.pmu.events_need_recreate = true; + kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); +} + /** * kvm_pmu_handle_pmcr - handle PMCR register * @vcpu: The vcpu pointer @@ -900,7 +906,16 @@ u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1) void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu) { + struct kvm_pmu *pmu = &vcpu->arch.pmu; u64 mask = kvm_pmu_implemented_counter_mask(vcpu); + int i; + + if (pmu->events_need_recreate) { + for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++) + kvm_pmu_stop_counter(kvm_vcpu_idx_to_pmc(vcpu, i)); + + pmu->events_need_recreate = false; + } __vcpu_rmw_sys_reg(vcpu, PMOVSSET_EL0, &=, mask); __vcpu_rmw_sys_reg(vcpu, PMINTENSET_EL1, &=, mask); diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 42b6705bc21d..1a57e07cec9a 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -5390,7 +5390,7 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu) set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags); if (kvm_vcpu_has_pmu(vcpu)) - kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); + kvm_pmu_request_recreate(vcpu); } /** diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index 6b4a118d17ca..65cf9b49a0c0 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -32,6 +32,7 @@ struct kvm_pmu { struct kvm_pmc pmc[KVM_ARMV8_PMU_MAX_COUNTERS]; int irq_num; bool created; + bool events_need_recreate; }; struct arm_pmu_entry { @@ -55,6 +56,7 @@ void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu); bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu); bool kvm_pmu_update_run(struct kvm_vcpu *vcpu); void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val); +void kvm_pmu_request_recreate(struct kvm_vcpu *vcpu); void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val); void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data, u64 select_idx); @@ -135,6 +137,7 @@ static inline bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu) } static inline bool kvm_pmu_update_run(struct kvm_vcpu *vcpu) { return false; } static inline void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val) {} +static inline void kvm_pmu_request_recreate(struct kvm_vcpu *vcpu) {} static inline void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) {} static inline void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data, u64 select_idx) {} -- 2.55.0 MDCR_EL2.HPMN changes which counters are reserved for EL2 and thus which enable control, event filter, and overflow width apply. HPMD changes EL2 filtering, while HLP changes the overflow width and sample period. The existing guest MDCR_EL2 handling only requests a PMU reload for HPME. Reloading enables or disables existing perf events, but does not rebuild events whose attributes have become stale. Generic userspace writes through KVM_SET_ONE_REG do not request a reload at all. Use a common helper for guest and userspace writes. Recreate events after HPMN, HPMD, or HLP changes. This preserves counter values. Defer event creation to the vCPU thread instead of an arbitrary ioctl thread. Use a setter-only accessor so register restore gains these side effects without changing generic reads or rejecting register values. Fixes: fe827f916662 ("KVM: arm64: nv: Honor MDCR_EL2.HPME") Fixes: 8a34979030f6 ("KVM: arm64: nv: Apply EL2 event filtering when in hyp context") Fixes: 16535d55e91f ("KVM: arm64: nv: Honor MDCR_EL2.HLP") Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- arch/arm64/kvm/pmu-emul.c | 14 ++++++++++++++ arch/arm64/kvm/sys_regs.c | 22 ++++++++++++++-------- include/kvm/arm_pmu.h | 2 ++ 3 files changed, 30 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 8530cc907b56..953255111779 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -612,6 +612,20 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) } } +void kvm_pmu_apply_mdcr(struct kvm_vcpu *vcpu, u64 old, u64 val) +{ + u64 changed = old ^ val; + + /* + * HPMN determines which counters HPMD and HLP apply to. Changes to + * these fields require new perf event filters and sample periods. + */ + if (changed & (MDCR_EL2_HPMN | MDCR_EL2_HPMD | MDCR_EL2_HLP)) + kvm_pmu_request_recreate(vcpu); + else if (changed & MDCR_EL2_HPME) + kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); +} + static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc) { struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc); diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 1a57e07cec9a..bae1b69a5ab9 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -3114,17 +3114,22 @@ static bool access_mdcr(struct kvm_vcpu *vcpu, } __vcpu_assign_sys_reg(vcpu, MDCR_EL2, val); - - /* - * Request a reload of the PMU to enable/disable the counters - * affected by HPME. - */ - if ((old ^ val) & MDCR_EL2_HPME) - kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); + kvm_pmu_apply_mdcr(vcpu, old, val); return true; } +static int set_mdcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, + u64 val) +{ + u64 old = __vcpu_sys_reg(vcpu, MDCR_EL2); + + __vcpu_assign_sys_reg(vcpu, MDCR_EL2, val); + kvm_pmu_apply_mdcr(vcpu, old, val); + + return 0; +} + static bool access_ras(struct kvm_vcpu *vcpu, struct sys_reg_params *p, const struct sys_reg_desc *r) @@ -3836,7 +3841,8 @@ static const struct sys_reg_desc sys_reg_descs[] = { EL2_REG_FILTERED(SCTLR2_EL2, access_vm_reg, reset_val, 0, sctlr2_el2_visibility), EL2_REG_VNCR(HCR_EL2, reset_hcr, 0), - EL2_REG(MDCR_EL2, access_mdcr, reset_mdcr, 0), + SYS_REG_USER_FILTER(MDCR_EL2, access_mdcr, reset_mdcr, 0, + NULL, set_mdcr, el2_visibility), EL2_REG(CPTR_EL2, access_rw, reset_val, CPTR_NVHE_EL2_RES1), EL2_REG_VNCR(HSTR_EL2, reset_val, 0), EL2_REG_VNCR_FILT(HFGRTR_EL2, fgt_visibility), diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index 65cf9b49a0c0..f12c916c04d5 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -58,6 +58,7 @@ bool kvm_pmu_update_run(struct kvm_vcpu *vcpu); void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val); void kvm_pmu_request_recreate(struct kvm_vcpu *vcpu); void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val); +void kvm_pmu_apply_mdcr(struct kvm_vcpu *vcpu, u64 old, u64 val); void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data, u64 select_idx); void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu); @@ -139,6 +140,7 @@ static inline bool kvm_pmu_update_run(struct kvm_vcpu *vcpu) { return false; } static inline void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val) {} static inline void kvm_pmu_request_recreate(struct kvm_vcpu *vcpu) {} static inline void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) {} +static inline void kvm_pmu_apply_mdcr(struct kvm_vcpu *vcpu, u64 old, u64 val) {} static inline void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data, u64 select_idx) {} static inline int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, -- 2.55.0 PMCR_EL0.LC and PMCR_EL0.LP control whether cycle and event counters overflow at 32 or 64 bits. They therefore determine the sample period of each backing perf event. Guest and userspace writes do not recreate existing events. Changing either bit after an event has been created leaves its old sample period in place, resulting in an incorrect overflow point. Request event recreation when either bit changes. Preserve the existing reload behavior for PMCR_EL0.E and other userspace writes. Fixes: c82d28cbf1d4 ("KVM: arm64: PMU: Distinguish between 64bit counter and 64bit overflow") Fixes: 11af4c37165e ("KVM: arm64: PMU: Implement PMUv3p5 long counter support") Closes: https://sashiko.dev/#/patchset/20260720-hybrid-v9-0-2b713ca1b5dc@rsg.ci.i.u-tokyo.ac.jp?part=6 Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Akihiko Odaki --- arch/arm64/kvm/pmu-emul.c | 18 ++++++++++++++---- arch/arm64/kvm/sys_regs.c | 3 ++- include/kvm/arm_pmu.h | 2 ++ 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 953255111779..e24ad6c72937 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -577,6 +577,17 @@ void kvm_pmu_request_recreate(struct kvm_vcpu *vcpu) kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); } +void kvm_pmu_apply_pmcr(struct kvm_vcpu *vcpu, u64 old, u64 val, bool force_reload) +{ + u64 changed = old ^ val; + + /* Reload the PMU if the write affects the backing perf events. */ + if (changed & (ARMV8_PMU_PMCR_LC | ARMV8_PMU_PMCR_LP)) + kvm_pmu_request_recreate(vcpu); + else if (force_reload || (changed & ARMV8_PMU_PMCR_E)) + kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); +} + /** * kvm_pmu_handle_pmcr - handle PMCR register * @vcpu: The vcpu pointer @@ -584,19 +595,18 @@ void kvm_pmu_request_recreate(struct kvm_vcpu *vcpu) */ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) { + u64 old = __vcpu_sys_reg(vcpu, PMCR_EL0); int i; /* Fixup PMCR_EL0 to reconcile the PMU version and the LP bit */ if (!kvm_has_feat(vcpu->kvm, ID_AA64DFR0_EL1, PMUVer, V3P5)) val &= ~ARMV8_PMU_PMCR_LP; - /* Request a reload of the PMU to enable/disable affected counters */ - if ((__vcpu_sys_reg(vcpu, PMCR_EL0) ^ val) & ARMV8_PMU_PMCR_E) - kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); - /* The reset bits don't indicate any state, and shouldn't be saved. */ __vcpu_assign_sys_reg(vcpu, PMCR_EL0, (val & ~(ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_P))); + kvm_pmu_apply_pmcr(vcpu, old, __vcpu_sys_reg(vcpu, PMCR_EL0), false); + if (val & ARMV8_PMU_PMCR_C) kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0); diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index bae1b69a5ab9..2754b743da01 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1517,6 +1517,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, { u8 new_n = FIELD_GET(ARMV8_PMU_PMCR_N, val); struct kvm *kvm = vcpu->kvm; + u64 old = __vcpu_sys_reg(vcpu, r->reg); mutex_lock(&kvm->arch.config_lock); @@ -1549,7 +1550,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, val |= ARMV8_PMU_PMCR_LC; __vcpu_assign_sys_reg(vcpu, r->reg, val); - kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); + kvm_pmu_apply_pmcr(vcpu, old, val, true); return 0; } diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index f12c916c04d5..29bcc09da93e 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -57,6 +57,7 @@ bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu); bool kvm_pmu_update_run(struct kvm_vcpu *vcpu); void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val); void kvm_pmu_request_recreate(struct kvm_vcpu *vcpu); +void kvm_pmu_apply_pmcr(struct kvm_vcpu *vcpu, u64 old, u64 val, bool force_reload); void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val); void kvm_pmu_apply_mdcr(struct kvm_vcpu *vcpu, u64 old, u64 val); void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data, @@ -139,6 +140,7 @@ static inline bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu) static inline bool kvm_pmu_update_run(struct kvm_vcpu *vcpu) { return false; } static inline void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val) {} static inline void kvm_pmu_request_recreate(struct kvm_vcpu *vcpu) {} +static inline void kvm_pmu_apply_pmcr(struct kvm_vcpu *vcpu, u64 old, u64 val, bool force_reload) {} static inline void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val) {} static inline void kvm_pmu_apply_mdcr(struct kvm_vcpu *vcpu, u64 old, u64 val) {} static inline void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, -- 2.55.0 KVM_SET_ONE_REG writes to PMEVTYPERn_EL0 and PMCCFILTR_EL0 use the generic raw system register assignment. If a backing perf event already exists, it keeps its old event selection and privilege filters. Add a setter that preserves the raw userspace register value and requests deferred event recreation. This rebuilds the event on the vCPU thread without changing values returned by KVM_GET_ONE_REG or applying guest-write masking. Fixes: 9feb21ac57d5 ("arm64: KVM: Add access handler for event type register") Closes: https://sashiko.dev/#/patchset/20260720-hybrid-v9-0-2b713ca1b5dc@rsg.ci.i.u-tokyo.ac.jp?part=6 Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Akihiko Odaki --- arch/arm64/kvm/sys_regs.c | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 2754b743da01..a5c121482c71 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1334,6 +1334,15 @@ static int set_pmreg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, u64 va return 0; } +static int set_pmu_evtyper(struct kvm_vcpu *vcpu, + const struct sys_reg_desc *r, u64 val) +{ + __vcpu_assign_sys_reg(vcpu, r->reg, val); + kvm_pmu_request_recreate(vcpu); + + return 0; +} + static int get_pmreg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, u64 *val) { u64 mask = kvm_pmu_implemented_counter_mask(vcpu); @@ -1584,7 +1593,7 @@ static int set_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, /* Macro to expand the PMEVTYPERn_EL0 register */ #define PMU_PMEVTYPER_EL0(n) \ { PMU_SYS_REG(PMEVTYPERn_EL0(n)), \ - .reset = reset_pmevtyper, \ + .reset = reset_pmevtyper, .set_user = set_pmu_evtyper, \ .access = access_pmu_evtyper, .reg = (PMEVTYPER0_EL0 + n), } /* Macro to expand the AMU counter and type registers*/ @@ -3833,7 +3842,8 @@ static const struct sys_reg_desc sys_reg_descs[] = { * in 32bit mode. Here we choose to reset it as zero for consistency. */ { PMU_SYS_REG(PMCCFILTR_EL0), .access = access_pmu_evtyper, - .reset = reset_val, .reg = PMCCFILTR_EL0, .val = 0 }, + .reset = reset_val, .reg = PMCCFILTR_EL0, .val = 0, + .set_user = set_pmu_evtyper }, EL2_REG_VNCR(VPIDR_EL2, reset_unknown, 0), EL2_REG_VNCR(VMPIDR_EL2, reset_unknown, 0), -- 2.55.0 Commit 9486aa387716 ("perf tools: Fix 64 bit integer format strings") defined the tools-local u64 as uint64_t so that PRI*64 format macros match its underlying C type. On architectures where uint64_t and __u64 are distinct types, however, ____MAKE_OP() still gives the u64_* helpers an __u64 interface. Teach ____MAKE_OP() to accept the helper name and storage type separately. Keep endian helpers on __le* and __be*, and generate the native helpers using the tools-local u* types. Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- tools/include/linux/bitfield.h | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/tools/include/linux/bitfield.h b/tools/include/linux/bitfield.h index ddf81f24956b..1c633343d576 100644 --- a/tools/include/linux/bitfield.h +++ b/tools/include/linux/bitfield.h @@ -142,32 +142,32 @@ static __always_inline u64 field_mask(u64 field) return field / field_multiplier(field); } #define field_max(field) ((typeof(field))field_mask(field)) -#define ____MAKE_OP(type,base,to,from) \ -static __always_inline __##type type##_encode_bits(base v, base field) \ +#define ____MAKE_OP(name,type,base,to,from) \ +static __always_inline type name##_encode_bits(base v, base field) \ { \ if (__builtin_constant_p(v) && (v & ~field_mask(field))) \ __field_overflow(); \ return to((v & field_mask(field)) * field_multiplier(field)); \ } \ -static __always_inline __##type type##_replace_bits(__##type old, \ - base val, base field) \ +static __always_inline type name##_replace_bits(type old, \ + base val, base field) \ { \ - return (old & ~to(field)) | type##_encode_bits(val, field); \ + return (old & ~to(field)) | name##_encode_bits(val, field); \ } \ -static __always_inline void type##p_replace_bits(__##type *p, \ +static __always_inline void name##p_replace_bits(type *p, \ base val, base field) \ { \ - *p = (*p & ~to(field)) | type##_encode_bits(val, field); \ + *p = (*p & ~to(field)) | name##_encode_bits(val, field); \ } \ -static __always_inline base type##_get_bits(__##type v, base field) \ +static __always_inline base name##_get_bits(type v, base field) \ { \ return (from(v) & field)/field_multiplier(field); \ } -#define __MAKE_OP(size) \ - ____MAKE_OP(le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \ - ____MAKE_OP(be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \ - ____MAKE_OP(u##size,u##size,,) -____MAKE_OP(u8,u8,,) +#define __MAKE_OP(size) \ + ____MAKE_OP(le##size,__le##size,u##size,cpu_to_le##size,le##size##_to_cpu) \ + ____MAKE_OP(be##size,__be##size,u##size,cpu_to_be##size,be##size##_to_cpu) \ + ____MAKE_OP(u##size,u##size,u##size,,) +____MAKE_OP(u8,u8,u8,,) __MAKE_OP(16) __MAKE_OP(32) __MAKE_OP(64) -- 2.55.0 Add MDCR_EL2 coverage to vpmu_counter_access when EL2 is available. For each configured PMCR_EL0.N value, verify that an arbitrary HPMN value, including one above the configured counter count, round-trips through KVM_SET_ONE_REG and survives SET_NR_COUNTERS. Also verify that HPME can be toggled without disturbing HPMN and that KVM_ARM_VCPU_INIT resets HPMN from the configured count. Assisted-by: Codex:gpt-5.5 Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- .../selftests/kvm/arm64/vpmu_counter_access.c | 90 ++++++++++++++++++++-- 1 file changed, 85 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c b/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c index 05135bcdb462..73b4f1870d3d 100644 --- a/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +++ b/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c @@ -25,6 +25,14 @@ /* The cycle counter bit position that's common among the PMU registers */ #define ARMV8_PMU_CYCLE_IDX 31 +#ifndef MDCR_EL2_HPMN +#define MDCR_EL2_HPMN GENMASK_ULL(4, 0) +#endif + +#ifndef MDCR_EL2_HPME +#define MDCR_EL2_HPME BIT_ULL(7) +#endif + struct vpmu_vm { struct kvm_vm *vm; struct kvm_vcpu *vcpu; @@ -465,15 +473,12 @@ static void guest_code_done(void) GUEST_DONE(); } -static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, bool expect_fail) +static void set_nr_counters(struct kvm_vcpu *vcpu, + unsigned int nr_counters, bool expect_fail) { - struct kvm_vcpu *vcpu; unsigned int prev; int ret; - create_vpmu_vm(guest_code); - vcpu = vpmu_vm.vcpu; - prev = get_pmcr_n(vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_PMCR_EL0))); ret = __vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, @@ -485,6 +490,17 @@ static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, bool nr_counters, prev); else TEST_ASSERT(!ret, KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret)); +} + +static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, + bool expect_fail) +{ + struct kvm_vcpu *vcpu; + + create_vpmu_vm(guest_code); + vcpu = vpmu_vm.vcpu; + + set_nr_counters(vcpu, nr_counters, expect_fail); vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT, NULL); } @@ -589,6 +605,69 @@ static void run_pmregs_validity_test(u64 pmcr_n) destroy_vpmu_vm(); } +static void run_mdcr_el2_validity_test(u64 pmcr_n) +{ + struct kvm_vcpu_init init; + struct kvm_vcpu *vcpu; + u64 expected_mdcr, mdcr; + + pr_debug("MDCR_EL2 test with pmcr_n %lu\n", pmcr_n); + + create_vpmu_vm(guest_code); + if (!vm_supports_el2(vpmu_vm.vm)) { + destroy_vpmu_vm(); + return; + } + + vcpu = vpmu_vm.vcpu; + + mdcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MDCR_EL2)); + expected_mdcr = u64_replace_bits(mdcr, FIELD_MAX(MDCR_EL2_HPMN), + MDCR_EL2_HPMN); + vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MDCR_EL2), + expected_mdcr); + + mdcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MDCR_EL2)); + TEST_ASSERT(mdcr == expected_mdcr, + "MDCR_EL2 was not properly updated after HPMN write (expected 0x%lx, got 0x%lx)", + expected_mdcr, mdcr); + + set_nr_counters(vcpu, pmcr_n, false); + + mdcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MDCR_EL2)); + TEST_ASSERT(mdcr == expected_mdcr, + "MDCR_EL2 changed after PMU_V3_SET_NR_COUNTERS (expected 0x%lx, got 0x%lx)", + expected_mdcr, mdcr); + + vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_INIT, NULL); + + mdcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MDCR_EL2)); + TEST_ASSERT(mdcr == expected_mdcr, + "MDCR_EL2 changed after PMU_V3_INIT (expected 0x%lx, got 0x%lx)", + expected_mdcr, mdcr); + + expected_mdcr = mdcr ^ MDCR_EL2_HPME; + vcpu_set_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MDCR_EL2), + expected_mdcr); + + mdcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MDCR_EL2)); + TEST_ASSERT(mdcr == expected_mdcr, + "MDCR_EL2 was not properly updated after HPME write (expected 0x%lx, got 0x%lx)", + expected_mdcr, mdcr); + + kvm_get_default_vcpu_target(vpmu_vm.vm, &init); + init.features[0] |= (1 << KVM_ARM_VCPU_PMU_V3); + aarch64_vcpu_setup(vcpu, &init); + + mdcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MDCR_EL2)); + TEST_ASSERT(FIELD_GET(MDCR_EL2_HPMN, mdcr) == pmcr_n, + "MDCR_EL2.HPMN is not reset after INIT (expected %lu, got %lu)", + pmcr_n, FIELD_GET(MDCR_EL2_HPMN, mdcr)); + + destroy_vpmu_vm(); +} + /* * Create a guest with one vCPU, and attempt to set the PMCR_EL0.N for * the vCPU to @pmcr_n, which is larger than the host value. @@ -673,6 +752,7 @@ int main(void) for (i = 0; i <= pmcr_n; i++) { run_access_test(i); run_pmregs_validity_test(i); + run_mdcr_el2_validity_test(i); } for (i = pmcr_n + 1; i < ARMV8_PMU_MAX_COUNTERS; i++) -- 2.55.0 KVM relies on ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS as a system-wide precondition for PMUv3 emulation on affected Apple CPUs. A local CPU erratum is too weak for that: once the workaround is enabled, a late CPU that misses it may still be onlined. Make the workaround an all-early-CPU capability instead. The matcher is still local because it depends on MIDR_EL1, but the capability is only advertised if all early CPUs match. A late CPU that misses an advertised capability is therefore rejected. Although kept in the errata capability table, this capability describes optional trap support, not a workaround required for safe CPU operation. Therefore, a late CPU can safely match the capability even when the system does not advertise it. On these CPUs, ID_AA64DFR0_EL1.PMUVer == 0xf means the Arm-architected PMUv3 is not implemented. Without the system capability, KVM does not advertise PMUv3, and direct accesses to architected PMUv3 registers remain UNDEFINED. HACR_EL2[56] only makes those accesses trap to EL2 so KVM can emulate PMUv3; leaving it clear does not make them access Apple's native PMU. The native Apple PMU uses separate implementation-defined system register encodings, which KVM independently traps with HCR_EL2.TIDCP. Allowing a late CPU with extra HACR_EL2 trap support therefore does not expose host PMU state to a guest. Fixes: bed9b8ec8c71 ("KVM: arm64: Advertise PMUv3 if IMPDEF traps are present") Assisted-by: Codex:gpt-5.5 Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- arch/arm64/kernel/cpu_errata.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/arch/arm64/kernel/cpu_errata.c b/arch/arm64/kernel/cpu_errata.c index 78e4e105f6ab..9b2d9d0f1738 100644 --- a/arch/arm64/kernel/cpu_errata.c +++ b/arch/arm64/kernel/cpu_errata.c @@ -1012,7 +1012,7 @@ const struct arm64_cpu_capabilities arm64_errata[] = { { .desc = "Apple IMPDEF PMUv3 Traps", .capability = ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS, - .type = ARM64_CPUCAP_LOCAL_CPU_ERRATUM, + .type = ARM64_CPUCAP_EARLY_LOCAL_CPU_FEATURE, .matches = has_impdef_pmuv3, .cpu_enable = cpu_enable_impdef_pmuv3_traps, }, -- 2.55.0 From: Oliver Upton commit e9b152cb957c ("arm/arm64: kvm: Set vcpu->cpu to -1 on vcpu_put") reset vcpu->cpu in order for the VGIC to determine if there was any vCPU running at the time of access. The VGIC has gone through an entire rewrite since then, and with commit 7d450e282171 ("KVM: arm/arm64: vgic-new: Add userland access to VGIC dist registers") the user accessors just grab all vCPU mutexes instead. Drop this remaining vestige such that kvm_arch_vcpu_load() can properly detect a CPU migration. While at it, rework kvm_reset_vcpu() to do a much more pedantic check that the provided vCPU is actually what's running on the present CPU. Signed-off-by: Oliver Upton Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- arch/arm64/kvm/arm.c | 1 - arch/arm64/kvm/reset.c | 16 +++++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index 8eff03c32418..dce6ba0f51e6 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -754,7 +754,6 @@ void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu) kvm_arm_vmid_clear_active(); vcpu_clear_on_unsupported_cpu(vcpu); - vcpu->cpu = -1; } static void __kvm_arm_vcpu_power_off(struct kvm_vcpu *vcpu) diff --git a/arch/arm64/kvm/reset.c b/arch/arm64/kvm/reset.c index 10eb7249aa9e..cdcdea850909 100644 --- a/arch/arm64/kvm/reset.c +++ b/arch/arm64/kvm/reset.c @@ -178,17 +178,24 @@ static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu) void kvm_reset_vcpu(struct kvm_vcpu *vcpu) { struct vcpu_reset_state reset_state; - bool loaded; + struct kvm_vcpu *running; + bool loaded = false; spin_lock(&vcpu->arch.mp_state_lock); reset_state = vcpu->arch.reset_state; vcpu->arch.reset_state.reset = false; spin_unlock(&vcpu->arch.mp_state_lock); - preempt_disable(); - loaded = (vcpu->cpu != -1); - if (loaded) + guard(preempt)(); + + running = kvm_get_running_vcpu(); + if (running) { + if (KVM_BUG_ON(running != vcpu, vcpu->kvm)) + return; + + loaded = true; kvm_arch_vcpu_put(vcpu); + } if (!kvm_arm_vcpu_sve_finalized(vcpu)) { if (vcpu_has_feature(vcpu, KVM_ARM_VCPU_SVE)) @@ -215,7 +222,6 @@ void kvm_reset_vcpu(struct kvm_vcpu *vcpu) if (loaded) kvm_arch_vcpu_load(vcpu, smp_processor_id()); - preempt_enable(); } u32 kvm_get_pa_bits(struct kvm *kvm) -- 2.55.0 Convert the list of PMUs to a RCU-protected list that has primitives to avoid read-side contention. Assisted-by: Codex:gpt-5.5 Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- arch/arm64/kvm/pmu-emul.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index e24ad6c72937..94bc69727c9f 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -7,9 +7,9 @@ #include #include #include -#include #include #include +#include #include #include #include @@ -17,6 +17,10 @@ #define PERF_ATTR_CFG1_COUNTER_64BIT BIT(0) +/* + * arm_pmus is append-only. kvm_supports_guest_pmuv3() feeds persistent + * VM state, so a true result must remain valid after the check. + */ static LIST_HEAD(arm_pmus); static DEFINE_MUTEX(arm_pmus_lock); @@ -26,7 +30,6 @@ 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); } @@ -819,7 +822,7 @@ void kvm_host_pmu_init(struct arm_pmu *pmu) return; entry->arm_pmu = pmu; - list_add_tail(&entry->entry, &arm_pmus); + list_add_tail_rcu(&entry->entry, &arm_pmus); } static struct arm_pmu *kvm_pmu_probe_armpmu(void) @@ -828,7 +831,7 @@ static struct arm_pmu *kvm_pmu_probe_armpmu(void) struct arm_pmu *pmu; int cpu; - guard(mutex)(&arm_pmus_lock); + guard(rcu)(); /* * It is safe to use a stale cpu to iterate the list of PMUs so long as @@ -848,7 +851,7 @@ static struct arm_pmu *kvm_pmu_probe_armpmu(void) * carried here. */ cpu = raw_smp_processor_id(); - list_for_each_entry(entry, &arm_pmus, entry) { + list_for_each_entry_rcu(entry, &arm_pmus, entry) { pmu = entry->arm_pmu; if (cpumask_test_cpu(cpu, &pmu->supported_cpus)) @@ -1114,9 +1117,9 @@ static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id) int ret = -ENXIO; lockdep_assert_held(&kvm->arch.config_lock); - mutex_lock(&arm_pmus_lock); + guard(rcu)(); - list_for_each_entry(entry, &arm_pmus, entry) { + list_for_each_entry_rcu(entry, &arm_pmus, entry) { arm_pmu = entry->arm_pmu; if (arm_pmu->pmu.type == pmu_id) { if (kvm_vm_has_ran_once(kvm) || @@ -1143,7 +1146,6 @@ static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id) } } - mutex_unlock(&arm_pmus_lock); return ret; } -- 2.55.0 Replace the VM argument with the pPMU used for event creation. The current caller still passes the VM's default pPMU, but this removes the implicit lookup from kvm_map_pmu_event() so later changes can map events against the pPMU selected for an individual vCPU. Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- arch/arm64/kvm/pmu-emul.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 94bc69727c9f..05b48be05abd 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -682,10 +682,8 @@ static bool kvm_pmc_counts_at_el2(struct kvm_pmc *pmc) return kvm_pmc_read_evtreg(pmc) & ARMV8_PMU_INCLUDE_EL2; } -static int kvm_map_pmu_event(struct kvm *kvm, unsigned int eventsel) +static int kvm_map_pmu_event(struct arm_pmu *pmu, unsigned int eventsel) { - struct arm_pmu *pmu = kvm->arch.arm_pmu; - /* * The CPU PMU likely isn't PMUv3; let the driver provide a mapping * for the guest's PMUv3 event ID. @@ -737,7 +735,7 @@ static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc) * Don't create an event if we're running on hardware that requires * PMUv3 event translation and we couldn't find a valid mapping. */ - eventsel = kvm_map_pmu_event(vcpu->kvm, eventsel); + eventsel = kvm_map_pmu_event(vcpu->kvm->arch.arm_pmu, eventsel); if (eventsel < 0) return; -- 2.55.0 kvm_pmu_probe_armpmu() currently samples the current CPU internally, which ties the helper to default PMU selection. Move that policy to kvm_arm_set_default_pmu() by passing raw_smp_processor_id() from the caller, and make the helper search for the pPMU covering an explicit CPU. Move the helper above kvm_pmu_create_perf_event() so later code can reuse it when creating PMU events for a VCPU's current pCPU. This preserves the existing default PMU selection behavior while preparing fixed-counters-only mode to select a pPMU at runtime. Signed-off-by: Akihiko Odaki Reviewed-by: Fuad Tabba --- arch/arm64/kvm/pmu-emul.c | 72 +++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 37 deletions(-) diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index 05b48be05abd..ccd268d76235 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -694,6 +694,23 @@ static int kvm_map_pmu_event(struct arm_pmu *pmu, unsigned int eventsel) return eventsel; } +static struct arm_pmu *kvm_pmu_probe_armpmu(int cpu) +{ + struct arm_pmu_entry *entry; + struct arm_pmu *pmu; + + guard(rcu)(); + + list_for_each_entry_rcu(entry, &arm_pmus, entry) { + pmu = entry->arm_pmu; + + if (cpumask_test_cpu(cpu, &pmu->supported_cpus)) + return pmu; + } + + return NULL; +} + /** * kvm_pmu_create_perf_event - create a perf event for a counter * @pmc: Counter context @@ -823,42 +840,6 @@ void kvm_host_pmu_init(struct arm_pmu *pmu) list_add_tail_rcu(&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(rcu)(); - - /* - * 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_rcu(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]; @@ -1098,7 +1079,24 @@ static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu) */ int kvm_arm_set_default_pmu(struct kvm *kvm) { - struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu(); + /* + * 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. + */ + struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu(raw_smp_processor_id()); if (!arm_pmu) return -ENODEV; -- 2.55.0 Add internal state for PMUv3 emulation without programmable event counters. When fixed-counters-only mode is active, KVM reports no programmable counters and hides PMCEID, avoiding event-counter state whose behavior can depend on the selected hardware PMU. The cycle counter still uses a host perf event. Unlike the normal PMU path, fixed-counters-only mode may create that event from the hardware PMU attached to the VCPU's current pCPU. If the VCPU later loads on a pCPU that is not covered by the existing event's PMU, request a PMU reload so the cycle counter can be recreated against the new pCPU's PMU. Keep this affinity check limited to fixed-counters-only VMs; the normal programmable-counter mode continues to use the VM-wide PMU and does not need per-load reload decisions. Registered pPMUs must cover every CPU on which fixed-counters-only emulation runs. On ACPI systems, a CPU brought online after PMU probing may have an unregistered PMU implementation. Full coverage is also not guaranteed when ARM64_WORKAROUND_PMUV3_IMPDEF_TRAPS is present. Fixed-counters-only emulation is not supported once a vCPU runs on an uncovered CPU. If event creation detects such a CPU, warn and add TAINT_CPU_OUT_OF_SPEC. Add a separate internal flag for explicit userspace PMU selection. The UAPI wiring added later will use it to keep explicit PMU selection and fixed-counters-only mode mutually exclusive while still allowing fixed-counters-only mode to replace the default PMU selected during KVM_ARM_VCPU_INIT. The UAPI wiring that sets the fixed-counters-only flag and records explicit PMU selection is added later in the series. Assisted-by: Codex:gpt-5.5 Signed-off-by: Akihiko Odaki --- arch/arm64/include/asm/kvm_host.h | 4 +++ arch/arm64/kvm/arm.c | 2 ++ arch/arm64/kvm/pmu-emul.c | 60 ++++++++++++++++++++++++++++++++++----- include/kvm/arm_pmu.h | 2 ++ 4 files changed, 61 insertions(+), 7 deletions(-) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index 27fe0cd5b2d7..10e3ff41079e 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -367,6 +367,10 @@ 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 + /* PMUv3 is emulated with an explicitly specified hardware PMU */ +#define KVM_ARCH_FLAG_PMU_V3_EXPLICIT 12 + /* PMUv3 is emulated without programmable event counters */ +#define KVM_ARCH_FLAG_PMU_V3_FIXED_COUNTERS_ONLY 13 unsigned long flags; /* VM-wide vCPU feature set */ diff --git a/arch/arm64/kvm/arm.c b/arch/arm64/kvm/arm.c index dce6ba0f51e6..dfebfeb95619 100644 --- a/arch/arm64/kvm/arm.c +++ b/arch/arm64/kvm/arm.c @@ -652,6 +652,7 @@ static bool kvm_vcpu_should_clear_twe(struct kvm_vcpu *vcpu) void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) { struct kvm_s2_mmu *mmu; + int last_cpu = vcpu->cpu; int *last_ran; if (is_protected_kvm_enabled()) @@ -701,6 +702,7 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu) if (has_vhe()) kvm_vcpu_load_vhe(vcpu); kvm_arch_vcpu_load_fp(vcpu); + kvm_vcpu_load_pmu(vcpu, last_cpu); kvm_vcpu_pmu_restore_guest(vcpu); if (kvm_arm_is_pvtime_enabled(&vcpu->arch)) kvm_make_request(KVM_REQ_RECORD_STEAL, vcpu); diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index ccd268d76235..a19a913a8925 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -83,6 +83,11 @@ u64 kvm_pmu_evtyper_mask(struct kvm *kvm) return mask; } +static bool kvm_pmu_fixed_counters_only(struct kvm *kvm) +{ + return test_bit(KVM_ARCH_FLAG_PMU_V3_FIXED_COUNTERS_ONLY, &kvm->arch.flags); +} + /** * kvm_pmc_is_64bit - determine if counter is 64bit * @pmc: counter context @@ -711,14 +716,10 @@ static struct arm_pmu *kvm_pmu_probe_armpmu(int cpu) return NULL; } -/** - * kvm_pmu_create_perf_event - create a perf event for a counter - * @pmc: Counter context - */ -static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc) +static void kvm_pmu_create_perf_event_with_pmu(struct kvm_pmc *pmc, + struct arm_pmu *arm_pmu) { struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc); - struct arm_pmu *arm_pmu = vcpu->kvm->arch.arm_pmu; struct perf_event *event; struct perf_event_attr attr; int eventsel; @@ -752,7 +753,7 @@ static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc) * Don't create an event if we're running on hardware that requires * PMUv3 event translation and we couldn't find a valid mapping. */ - eventsel = kvm_map_pmu_event(vcpu->kvm->arch.arm_pmu, eventsel); + eventsel = kvm_map_pmu_event(arm_pmu, eventsel); if (eventsel < 0) return; @@ -797,6 +798,32 @@ static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc) pmc->perf_event = event; } +/** + * kvm_pmu_create_perf_event - create a perf event for a counter + * @pmc: Counter context + */ +static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc) +{ + struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc); + struct arm_pmu *arm_pmu = vcpu->kvm->arch.arm_pmu; + + if (kvm_pmu_fixed_counters_only(vcpu->kvm)) { + do { + arm_pmu = kvm_pmu_probe_armpmu(READ_ONCE(vcpu->cpu)); + + if (!arm_pmu) { + pr_warn_once("kvm: Unsupported PMU variation detected.\n"); + add_taint(TAINT_CPU_OUT_OF_SPEC, LOCKDEP_STILL_OK); + return; + } + + kvm_pmu_create_perf_event_with_pmu(pmc, arm_pmu); + } while (!cpumask_test_cpu(READ_ONCE(vcpu->cpu), &arm_pmu->supported_cpus)); + } else { + kvm_pmu_create_perf_event_with_pmu(pmc, arm_pmu); + } +} + /** * kvm_pmu_set_counter_event_type - set selected counter to monitor some event * @vcpu: The vcpu pointer @@ -883,6 +910,13 @@ u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1) u64 val, mask = 0; int base, i, nr_events; + /* + * Hide the hardware PMU's event set to keep PMCEID stable across + * physical CPU migration. + */ + if (kvm_pmu_fixed_counters_only(vcpu->kvm)) + return 0; + if (!pmceid1) { val = compute_pmceid0(vcpu); base = 0; @@ -910,6 +944,15 @@ u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1) return val & mask; } +void kvm_vcpu_load_pmu(struct kvm_vcpu *vcpu, int last_cpu) +{ + if (!kvm_pmu_fixed_counters_only(vcpu->kvm) || vcpu->cpu == last_cpu || last_cpu == -1) + return; + + if (kvm_pmu_probe_armpmu(vcpu->cpu) != kvm_pmu_probe_armpmu(last_cpu)) + kvm_pmu_request_recreate(vcpu); +} + void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu) { struct kvm_pmu *pmu = &vcpu->arch.pmu; @@ -1034,6 +1077,9 @@ u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm) { struct arm_pmu *arm_pmu = kvm->arch.arm_pmu; + if (kvm_pmu_fixed_counters_only(kvm)) + return 0; + /* * 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 diff --git a/include/kvm/arm_pmu.h b/include/kvm/arm_pmu.h index 29bcc09da93e..17b861ed171d 100644 --- a/include/kvm/arm_pmu.h +++ b/include/kvm/arm_pmu.h @@ -62,6 +62,7 @@ void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val); void kvm_pmu_apply_mdcr(struct kvm_vcpu *vcpu, u64 old, u64 val); void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data, u64 select_idx); +void kvm_vcpu_load_pmu(struct kvm_vcpu *vcpu, int last_cpu); void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu); int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr); @@ -174,6 +175,7 @@ static inline u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1) 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) {} +static inline void kvm_vcpu_load_pmu(struct kvm_vcpu *vcpu, int last_cpu) {} static inline void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu) {} static inline u8 kvm_arm_pmu_get_pmuver_limit(void) { -- 2.55.0 Introduce the KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY attribute to create a "fixed-counters-only" PMU. Much like KVM_ARM_VCPU_PMU_V3_IRQ and other read-write attributes, this attribute provides a getter that facilitates kernel and userspace debugging/testing. Allow strict PMUv3 vCPUs to initialize the PMU after selecting fixed-counters-only mode, without selecting a VM-wide hardware PMU. Assisted-by: Codex:gpt-5.6-sol Signed-off-by: Akihiko Odaki --- Documentation/virt/kvm/api.rst | 8 +++++-- Documentation/virt/kvm/devices/vcpu.rst | 42 ++++++++++++++++++++++++++------- arch/arm64/include/uapi/asm/kvm.h | 1 + arch/arm64/kvm/pmu-emul.c | 34 ++++++++++++++++++++++---- tools/arch/arm64/include/uapi/asm/kvm.h | 1 + 5 files changed, 71 insertions(+), 15 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 4988c32df4bf..42065138603b 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -3521,9 +3521,13 @@ Possible features: * Userspace must explicitly select a PMU implementation before initializing the PMU or configuring a PMU event filter + (KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY also satisfies the + initialization requirement; event filtering remains unavailable + in that mode) - * If the PMU implements FEAT_PMUv3p4, PMMIR_EL1.SLOTS provides the - hardware value of the underlying implementation + * If a hardware PMU is selected and implements FEAT_PMUv3p4, + PMMIR_EL1.SLOTS provides its hardware value. In fixed-counters-only + mode, PMMIR_EL1 reads as zero * Writes to PMCR_EL0.N via KVM_SET_ONE_REG are ignored diff --git a/Documentation/virt/kvm/devices/vcpu.rst b/Documentation/virt/kvm/devices/vcpu.rst index deb5c51bc00c..4ec3291f0ba7 100644 --- a/Documentation/virt/kvm/devices/vcpu.rst +++ b/Documentation/virt/kvm/devices/vcpu.rst @@ -53,9 +53,9 @@ Returns: ======= ====================================================== -EEXIST Interrupt number already used -ENODEV PMUv3 not supported or GIC not initialized - -ENXIO PMUv3 not supported, missing VCPU feature, missing - hardware PMU, or interrupt number not set (non-GICv5 - guests, only) + -ENXIO PMUv3 not supported, missing VCPU feature, + neither hardware PMU nor FIXED_COUNTERS_ONLY selected, + or interrupt number not set (non-GICv5 guests only) -EBUSY PMUv3 already initialized ======= ====================================================== @@ -64,7 +64,8 @@ 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. +after selecting a hardware PMU or enabling +KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY. 1.3 ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_FILTER ----------------------------------------- @@ -78,7 +79,8 @@ after selecting a hardware PMU. -ENODEV PMUv3 not supported or GIC not initialized -ENXIO PMUv3 not properly configured or in-kernel irqchip not configured as required prior to calling this attribute - -EBUSY PMUv3 already initialized or a VCPU has already run + -EBUSY PMUv3 already initialized, a VCPU has already run or + FIXED_COUNTERS_ONLY has already been set -EINVAL Invalid filter range ======= ====================================================== @@ -123,14 +125,14 @@ after selecting a hardware PMU. :Returns: - ======= ==================================================== + ======= =========================================================== -EBUSY PMUv3 already initialized, a VCPU has already run or - an event filter has already been set + an event filter or FIXED_COUNTERS_ONLY has already been set -EFAULT Error accessing the PMU identifier -ENXIO PMU not found -ENODEV PMUv3 not supported or GIC not initialized -ENOMEM Could not allocate memory - ======= ==================================================== + ======= =========================================================== Request that the VCPU uses the specified hardware PMU when creating guest events for the purpose of PMU emulation. The PMU identifier can be read from the "type" @@ -172,6 +174,30 @@ explicitly selected, or the number of counters is out of range for the selected PMU. Selecting a new PMU cancels the effect of setting this attribute. +1.6 ATTRIBUTE: KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY +------------------------------------------------------ + +:Parameters: no additional parameter in kvm_device_attr.addr + +:Returns: + + ======= ================================================== + -EBUSY PMUv3 already initialized, a VCPU has already run, + an event filter has already been set or + a hardware PMU has already been specified + -ENXIO Attempted to get before setting + -ENODEV Attempted to set while PMUv3 not supported + ======= ================================================== + +If set, KVM emulates PMUv3 without programmable event counters. + +With KVM_ARM_VCPU_PMU_V3_STRICT, enabling this attribute satisfies the +PMU selection requirement for KVM_ARM_VCPU_PMU_V3_INIT. + +When this attribute is enabled, the vCPU can run on any physical CPU +that has a PMU, regardless of the underlying implementation. This +attribute is VM-scoped. + 2. GROUP: KVM_ARM_VCPU_TIMER_CTRL ================================= diff --git a/arch/arm64/include/uapi/asm/kvm.h b/arch/arm64/include/uapi/asm/kvm.h index 019e5e3d892e..3bd7f201252a 100644 --- a/arch/arm64/include/uapi/asm/kvm.h +++ b/arch/arm64/include/uapi/asm/kvm.h @@ -438,6 +438,7 @@ 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_FIXED_COUNTERS_ONLY 5 #define KVM_ARM_VCPU_TIMER_CTRL 1 #define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0 #define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1 diff --git a/arch/arm64/kvm/pmu-emul.c b/arch/arm64/kvm/pmu-emul.c index a19a913a8925..c68331cdfaf9 100644 --- a/arch/arm64/kvm/pmu-emul.c +++ b/arch/arm64/kvm/pmu-emul.c @@ -1003,8 +1003,8 @@ 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) + /* Strict PMUv3 requires a hardware PMU or fixed-counters-only mode. */ + if (!vcpu->kvm->arch.arm_pmu && !kvm_pmu_fixed_counters_only(vcpu->kvm)) return -ENXIO; if (irqchip_in_kernel(vcpu->kvm)) { @@ -1139,8 +1139,8 @@ int kvm_arm_set_default_pmu(struct kvm *kvm) * 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. + * to select a PMU type directly, or request fixed-counters-only + * emulation. But, be wary of the baggage being carried here. */ struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu(raw_smp_processor_id()); @@ -1165,11 +1165,13 @@ static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id) arm_pmu = entry->arm_pmu; if (arm_pmu->pmu.type == pmu_id) { if (kvm_vm_has_ran_once(kvm) || + kvm_pmu_fixed_counters_only(kvm) || (kvm->arch.pmu_filter && kvm->arch.arm_pmu != arm_pmu)) { ret = -EBUSY; break; } + set_bit(KVM_ARCH_FLAG_PMU_V3_EXPLICIT, &kvm->arch.flags); kvm_arm_set_pmu(kvm, arm_pmu); cpumask_copy(kvm->arch.supported_cpus, &arm_pmu->supported_cpus); @@ -1191,6 +1193,22 @@ static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id) return ret; } +static int kvm_arm_pmu_v3_set_pmu_fixed_counters_only(struct kvm_vcpu *vcpu) +{ + struct kvm *kvm = vcpu->kvm; + + lockdep_assert_held(&kvm->arch.config_lock); + + if (kvm_vm_has_ran_once(kvm) || kvm->arch.pmu_filter || + test_bit(KVM_ARCH_FLAG_PMU_V3_EXPLICIT, &kvm->arch.flags)) + return -EBUSY; + + set_bit(KVM_ARCH_FLAG_PMU_V3_FIXED_COUNTERS_ONLY, &kvm->arch.flags); + kvm->arch.nr_pmu_counters = 0; + + return 0; +} + static int kvm_arm_pmu_v3_set_nr_counters(struct kvm_vcpu *vcpu, unsigned int n) { struct kvm *kvm = vcpu->kvm; @@ -1268,7 +1286,7 @@ int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) filter.action != KVM_PMU_EVENT_DENY)) return -EINVAL; - if (kvm_vm_has_ran_once(kvm)) + if (kvm_vm_has_ran_once(kvm) || kvm_pmu_fixed_counters_only(kvm)) return -EBUSY; if (!kvm->arch.arm_pmu) @@ -1316,6 +1334,8 @@ 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_FIXED_COUNTERS_ONLY: + return kvm_arm_pmu_v3_set_pmu_fixed_counters_only(vcpu); case KVM_ARM_VCPU_PMU_V3_INIT: return kvm_arm_pmu_v3_init(vcpu); } @@ -1342,6 +1362,9 @@ int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr) irq = vcpu->arch.pmu.irq_num; return put_user(irq, uaddr); } + case KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY: + if (kvm_pmu_fixed_counters_only(vcpu->kvm)) + return 0; } return -ENXIO; @@ -1355,6 +1378,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_FIXED_COUNTERS_ONLY: if (kvm_vcpu_has_pmu(vcpu)) return 0; } diff --git a/tools/arch/arm64/include/uapi/asm/kvm.h b/tools/arch/arm64/include/uapi/asm/kvm.h index 1c13bfa2d38a..39a1a1e412e6 100644 --- a/tools/arch/arm64/include/uapi/asm/kvm.h +++ b/tools/arch/arm64/include/uapi/asm/kvm.h @@ -437,6 +437,7 @@ 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_FIXED_COUNTERS_ONLY 5 #define KVM_ARM_VCPU_TIMER_CTRL 1 #define KVM_ARM_VCPU_TIMER_IRQ_VTIMER 0 #define KVM_ARM_VCPU_TIMER_IRQ_PTIMER 1 -- 2.55.0 Assert the following: - FIXED_COUNTERS_ONLY is unset at initialization. - FIXED_COUNTERS_ONLY can be set. - Setting an event filter when FIXED_COUNTERS_ONLY has already been set results in EBUSY. - Setting FIXED_COUNTERS_ONLY when an event filter has already been set results in EBUSY. - Setting FIXED_COUNTERS_ONLY when a VCPU has already run results in EBUSY. Also verify that strict PMUv3 initialization requires a PMU selection and succeeds after selecting fixed-counters-only mode. Run the existing PMU and MDCR_EL2 tests in fixed-only mode. Verify that enabling the mode preserves the userspace MDCR_EL2 value until a subsequent KVM_ARM_VCPU_INIT resets HPMN to zero. Assisted-by: Codex:gpt-5.5 Signed-off-by: Akihiko Odaki --- .../selftests/kvm/arm64/vpmu_counter_access.c | 202 ++++++++++++++++++--- 1 file changed, 173 insertions(+), 29 deletions(-) diff --git a/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c b/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c index 73b4f1870d3d..637653f7a25b 100644 --- a/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c +++ b/tools/testing/selftests/kvm/arm64/vpmu_counter_access.c @@ -411,12 +411,7 @@ static void create_vpmu_vm(void *guest_code) { struct kvm_vcpu_init init; u8 pmuver, ec; - u64 dfr0, irq = 23; - struct kvm_device_attr irq_attr = { - .group = KVM_ARM_VCPU_PMU_V3_CTRL, - .attr = KVM_ARM_VCPU_PMU_V3_IRQ, - .addr = (u64)&irq, - }; + u64 dfr0; /* The test creates the vpmu_vm multiple times. Ensure a clean state */ memset(&vpmu_vm, 0, sizeof(vpmu_vm)); @@ -442,8 +437,6 @@ static void create_vpmu_vm(void *guest_code) TEST_ASSERT(pmuver != ID_AA64DFR0_EL1_PMUVer_IMP_DEF && pmuver >= ID_AA64DFR0_EL1_PMUVer_IMP, "Unexpected PMUVER (0x%x) on the vCPU with PMUv3", pmuver); - - vcpu_ioctl(vpmu_vm.vcpu, KVM_SET_DEVICE_ATTR, &irq_attr); } static void destroy_vpmu_vm(void) @@ -493,13 +486,22 @@ static void set_nr_counters(struct kvm_vcpu *vcpu, } static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, + bool fixed_counters_only, bool expect_fail) { struct kvm_vcpu *vcpu; + u64 irq = 23; create_vpmu_vm(guest_code); vcpu = vpmu_vm.vcpu; + if (fixed_counters_only) + vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY, NULL); + + vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_IRQ, &irq); + set_nr_counters(vcpu, nr_counters, expect_fail); vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT, NULL); @@ -509,15 +511,15 @@ static void test_create_vpmu_vm_with_nr_counters(unsigned int nr_counters, * Create a guest with one vCPU, set the PMCR_EL0.N for the vCPU to @pmcr_n, * and run the test. */ -static void run_access_test(u64 pmcr_n) +static void run_access_test(u64 pmcr_n, bool fixed_counters_only) { u64 sp; struct kvm_vcpu *vcpu; struct kvm_vcpu_init init; - pr_debug("Test with pmcr_n %lu\n", pmcr_n); + pr_debug("Test with pmcr_n %lu, fixed_counters_only %d\n", pmcr_n, fixed_counters_only); - test_create_vpmu_vm_with_nr_counters(pmcr_n, false); + test_create_vpmu_vm_with_nr_counters(pmcr_n, fixed_counters_only, false); vcpu = vpmu_vm.vcpu; /* Save the initial sp to restore them later to run the guest again */ @@ -553,14 +555,14 @@ static struct pmreg_sets validity_check_reg_sets[] = { * Create a VM, and check if KVM handles the userspace accesses of * the PMU register sets in @validity_check_reg_sets[] correctly. */ -static void run_pmregs_validity_test(u64 pmcr_n) +static void run_pmregs_validity_test(u64 pmcr_n, bool fixed_counters_only) { int i; struct kvm_vcpu *vcpu; u64 set_reg_id, clr_reg_id, reg_val; u64 valid_counters_mask, max_counters_mask; - test_create_vpmu_vm_with_nr_counters(pmcr_n, false); + test_create_vpmu_vm_with_nr_counters(pmcr_n, fixed_counters_only, false); vcpu = vpmu_vm.vcpu; valid_counters_mask = get_counters_mask(pmcr_n); @@ -605,13 +607,15 @@ static void run_pmregs_validity_test(u64 pmcr_n) destroy_vpmu_vm(); } -static void run_mdcr_el2_validity_test(u64 pmcr_n) +static void run_mdcr_el2_validity_test(u64 pmcr_n, bool fixed_counters_only) { struct kvm_vcpu_init init; struct kvm_vcpu *vcpu; u64 expected_mdcr, mdcr; + u64 irq = 23; - pr_debug("MDCR_EL2 test with pmcr_n %lu\n", pmcr_n); + pr_debug("MDCR_EL2 test with pmcr_n %lu, fixed_counters_only %d\n", + pmcr_n, fixed_counters_only); create_vpmu_vm(guest_code); if (!vm_supports_el2(vpmu_vm.vm)) { @@ -632,6 +636,16 @@ static void run_mdcr_el2_validity_test(u64 pmcr_n) "MDCR_EL2 was not properly updated after HPMN write (expected 0x%lx, got 0x%lx)", expected_mdcr, mdcr); + if (fixed_counters_only) { + vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY, NULL); + + mdcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MDCR_EL2)); + TEST_ASSERT(mdcr == expected_mdcr, + "MDCR_EL2 changed after PMU_V3_FIXED_COUNTERS_ONLY (expected 0x%lx, got 0x%lx)", + expected_mdcr, mdcr); + } + set_nr_counters(vcpu, pmcr_n, false); mdcr = vcpu_get_reg(vcpu, KVM_ARM64_SYS_REG(SYS_MDCR_EL2)); @@ -639,6 +653,9 @@ static void run_mdcr_el2_validity_test(u64 pmcr_n) "MDCR_EL2 changed after PMU_V3_SET_NR_COUNTERS (expected 0x%lx, got 0x%lx)", expected_mdcr, mdcr); + vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_IRQ, &irq); + vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, KVM_ARM_VCPU_PMU_V3_INIT, NULL); @@ -673,11 +690,11 @@ static void run_mdcr_el2_validity_test(u64 pmcr_n) * the vCPU to @pmcr_n, which is larger than the host value. * The attempt should fail as @pmcr_n is too big to set for the vCPU. */ -static void run_error_test(u64 pmcr_n) +static void run_error_test(u64 pmcr_n, bool fixed_counters_only) { pr_debug("Error test with pmcr_n %lu (larger than the host)\n", pmcr_n); - test_create_vpmu_vm_with_nr_counters(pmcr_n, true); + test_create_vpmu_vm_with_nr_counters(pmcr_n, fixed_counters_only, true); destroy_vpmu_vm(); } @@ -738,25 +755,152 @@ static void test_set_nr_counters_after_vcpu_run(void) kvm_vm_free(vm); } -int main(void) +static void test_config(u64 pmcr_n, bool fixed_counters_only) +{ + u64 i; + + for (i = 0; i <= pmcr_n; i++) { + run_access_test(i, fixed_counters_only); + run_pmregs_validity_test(i, fixed_counters_only); + run_mdcr_el2_validity_test(i, fixed_counters_only); + } + + for (i = pmcr_n + 1; i < ARMV8_PMU_MAX_COUNTERS; i++) + run_error_test(i, fixed_counters_only); +} + +static void test_fixed_counters_only_strict(void) +{ + struct kvm_vcpu_init init; + struct kvm_vcpu *vcpu; + struct kvm_vm *vm; + u64 irq = 23; + int ret; + + if (!kvm_has_cap(KVM_CAP_ARM_PMU_V3_STRICT)) + return; + + vm = vm_create(1); + kvm_get_default_vcpu_target(vm, &init); + init.features[0] |= BIT(KVM_ARM_VCPU_PMU_V3) | + BIT(KVM_ARM_VCPU_PMU_V3_STRICT); + vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code_done); + kvm_arch_vm_finalize_vcpus(vm); + vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_IRQ, &irq); + + /* STRICT still requires a PMU selection before initialization. */ + ret = __vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_INIT, NULL); + TEST_ASSERT(ret == -1 && errno == ENXIO, + KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret)); + + vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY, NULL); + vcpu_device_attr_set(vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_INIT, NULL); + + kvm_vm_free(vm); +} + +static void test_fixed_counters_only(void) { - u64 i, pmcr_n; + struct kvm_pmu_event_filter filter = { .nevents = 0 }; + struct kvm_vm *vm; + struct kvm_vcpu *running_vcpu; + struct kvm_vcpu *stopped_vcpu; + struct kvm_vcpu_init init; + int ret; + u64 irq = 23; + + create_vpmu_vm(guest_code); + ret = __vcpu_has_device_attr(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY); + if (ret) { + TEST_ASSERT(ret == -1 && errno == ENXIO, + KVM_IOCTL_ERROR(KVM_HAS_DEVICE_ATTR, ret)); + destroy_vpmu_vm(); + return; + } + + /* Assert that FIXED_COUNTERS_ONLY is unset at initialization. */ + ret = __vcpu_device_attr_get(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY, NULL); + TEST_ASSERT(ret == -1 && errno == ENXIO, + KVM_IOCTL_ERROR(KVM_GET_DEVICE_ATTR, ret)); + + /* Assert that setting FIXED_COUNTERS_ONLY succeeds. */ + vcpu_device_attr_set(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY, NULL); + + /* Assert that FIXED_COUNTERS_ONLY is set. */ + vcpu_device_attr_get(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY, NULL); + + /* + * Setting an event filter when FIXED_COUNTERS_ONLY has already been set + * results in EBUSY. + */ + ret = __vcpu_device_attr_set(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FILTER, &filter); + TEST_ASSERT(ret == -1 && errno == EBUSY, + KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret)); + + destroy_vpmu_vm(); + + create_vpmu_vm(guest_code); + + /* + * Assert that setting FIXED_COUNTERS_ONLY when an event filter has + * already been set results in EBUSY. + */ + vcpu_device_attr_set(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FILTER, &filter); + + ret = __vcpu_device_attr_set(vpmu_vm.vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY, NULL); + TEST_ASSERT(ret == -1 && errno == EBUSY, + KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret)); + + destroy_vpmu_vm(); + + /* + * Assert that setting FIXED_COUNTERS_ONLY when a VCPU has already run + * results in EBUSY. + */ + vm = vm_create(2); + vm_ioctl(vm, KVM_ARM_PREFERRED_TARGET, &init); + init.features[0] |= (1 << KVM_ARM_VCPU_PMU_V3); + running_vcpu = aarch64_vcpu_add(vm, 0, &init, guest_code_done); + stopped_vcpu = aarch64_vcpu_add(vm, 1, &init, guest_code_done); + kvm_arch_vm_finalize_vcpus(vm); + vcpu_device_attr_set(running_vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_IRQ, &irq); + vcpu_device_attr_set(running_vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_INIT, NULL); + run_vcpu(running_vcpu); + + ret = __vcpu_device_attr_set(stopped_vcpu, KVM_ARM_VCPU_PMU_V3_CTRL, + KVM_ARM_VCPU_PMU_V3_FIXED_COUNTERS_ONLY, NULL); + TEST_ASSERT(ret == -1 && errno == EBUSY, + KVM_IOCTL_ERROR(KVM_SET_DEVICE_ATTR, ret)); + + kvm_vm_free(vm); + + test_fixed_counters_only_strict(); + test_config(0, true); +} + +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_set_nr_counters_after_vcpu_run(); - - pmcr_n = get_pmcr_n_limit(); - for (i = 0; i <= pmcr_n; i++) { - run_access_test(i); - run_pmregs_validity_test(i); - run_mdcr_el2_validity_test(i); - } - - for (i = pmcr_n + 1; i < ARMV8_PMU_MAX_COUNTERS; i++) - run_error_test(i); + test_config(get_pmcr_n_limit(), false); + test_fixed_counters_only(); return 0; } -- 2.55.0