[PATCH v11 09/21] KVM: arm64: PMU: Recreate events after MDCR_EL2 changes
Akihiko Odaki
odaki at rsg.ci.i.u-tokyo.ac.jp
Tue Sep 22 01:51:03 PDT 2026
On 2026/09/22 15:54, Oliver Upton wrote:
> On Sun, Sep 20, 2026 at 08:15:50PM +0900, Akihiko Odaki wrote:
>> +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);
>> +}
>> +
>
> Urgh... I'm not a fan of this. Can you just modify kvm_vcpu_reload_pmu()
> to discard all backing perf events and recompute them only for enabled
> PMCs? We set KVM_REQ_RELOAD_PMU pretty rarely.
>
>> __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 75624725adf1..7dd2cafce247 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;
>> +}
>> +
>
> Just keep the old ^ new test inline for access_mdcr() and set the
> request unconditionally from set_mdcr(). In the usual case
> KVM_REQ_RELOAD_PMU is already set on a vCPU that userspace is restoring.
Hi Oliver,
Thanks for the feedback. I'll make both changes for v2.
I originally tried to preserve existing behavior and keep things
consistent, but I agree that simplicity is much better here given how
rarely KVM_REQ_RELOAD_PMU is set.
Regards,
Akihiko Odaki
More information about the linux-arm-kernel
mailing list