Re: [PATCH v10 16/33] KVM: arm64: Set up MDCR_EL2 to handle a Partitioned PMU
From: Colton Lewis
Date: Thu Dec 03 2026 - 16:35:38 EST
Oliver Upton <oupton@xxxxxxxxxx> writes:
> On Thu, Nov 19, 2026 at 06:51:58PM +0000, Colton Lewis wrote:
>> + if (cpumask_test_cpu(smp_processor_id(),
>> + vcpu->kvm->arch.supported_cpus) &&
>> + nr_guest_cntr < nr_host_cntr &&
>> + (cpus_have_final_cap(ARM64_HAS_HPMN0) || nr_guest_cntr > 0)) {
>
> nr_pmu_counters <= *host_data_ptr(nr_event_counters) should be a
> precondition of running on a supported CPU... IOW, the number of guest
> counters must already be supported by the selected PMU implementation.
Ack, KVM_ARM_VCPU_PMU_V3_ENABLE_PARTITION and kvm_arm_pmu_v3_init()
already enforce that guest HPMN is strictly less than nr_event_counters
on supported_cpus, so I'll turn that into a KVM_BUG_ON() in v11 instead
of silently falling back.
>> + u64 hpmn = FIELD_GET(MDCR_EL2_HPMN, read_sysreg(mdcr_el2));
>> + bool hpme;
>> +
>> + if (hpmn < nr_host_cntr)
>> + hpme = !!(read_sysreg(mdcr_el2) & MDCR_EL2_HPME);
>> + else
>> + hpme = !!(read_sysreg(pmcr_el0) & ARMV8_PMU_PMCR_E);
>
> This bit is extremely confusing. MDCR_EL2.HPME needs to always follow
> the host enable/disable of the PMU. We already have the PMU driver
> publish information into KVM for percpu state, this might be another
> example of where that makes sense.
>
> It shouldn't ever be the case that on vcpu_load() the canonical bit for
> the host PMU is MDCR_EL2.HPME.
Agreed.
>> + mdcr |= (MDCR_EL2_HPMD | MDCR_EL2_HCCD);
>> + if (is_pmuv3p5(read_pmuver()))
>
> read_pmuver() reads the ID register for every invocation which will be a
> trap to the L0 hypervisor every time under nested. If you need this sort
> of percpu state, set it up in kvm_init_host_debug_data().
Good catch. kvm_init_host_debug_data() already extracts pmuver from
ID_AA64DFR0_EL1, so I'll record a host_data flag there.
>> + if (is_nested_ctxt(vcpu)) {
>> *mdcr |= (guest_mdcr & NV_MDCR_GUEST_INCLUDE);
>> + if (kvm_pmu_is_partitioned(vcpu->kvm) &&
>> + kvm_pmu_hyp_counter_mask(vcpu))
>> + *mdcr |= MDCR_EL2_TPM | MDCR_EL2_TPMCR;
>
> This could use a comment, because there's a rather load bearing detail
> here: you're emulating the effects of HPMN because the architecture
> doesn't nest.
Will add a comment in v11.
>> + if (kvm_pmu_is_partitioned(vcpu->kvm) &&
>> + (__vcpu_sys_reg(vcpu, PMUSERENR_EL0) &
>> + ARMV8_PMU_USERENR_MASK)) {
>> + *mdcr |= guest_mdcr & (MDCR_EL2_TPM | MDCR_EL2_TPMCR);
>> + if (kvm_pmu_hyp_counter_mask(vcpu))
>> + *mdcr |= MDCR_EL2_TPM | MDCR_EL2_TPMCR;
>
> Why do we need to do this in a hyp context?
is_hyp_ctxt() covers L1's userspace (EL0 with HCR_EL2.TGE=1) as well as
vEL2, and L1 can switch between them without re-running
kvm_arm_setup_mdcr_el2() (via kvm_hyp_handle_eret() or FEAT_NV3 on ERET,
and natively on SVC).
MDCR_EL2.{TPM,TPMCR,HPMN} still apply to L1's userspace
(BEHAVE_FORWARD_IN_HOST_EL0). Emulated PMU leaves physical TPM and TPMCR
set unconditionally, while Partitioned PMU clears them on FEAT_FGT. If
L1 leaves PMUSERENR_EL0 zero, hardware traps EL0 PMU accesses directly
to L1 vEL2 so physical TPM/TPMCR can stay clear. Once L1 enables EL0
access in PMUSERENR_EL0, L0 needs to re-enable physical TPM/TPMCR when
L1 sets virtual TPM/TPMCR or sub-partitions HPMN, otherwise L1's
userspace would bypass L1's traps and access counters >= L1's HPMN
directly.
I'll add a comment explaining this.