[PATCH v11 09/21] KVM: arm64: PMU: Recreate events after MDCR_EL2 changes

Oliver Upton oupton at kernel.org
Mon Sep 21 23:54:59 PDT 2026


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.

Thanks,
Oliver



More information about the linux-arm-kernel mailing list