[PATCH v10 18/33] KVM: arm64: Enforce PMU event filter at vcpu_load()
Oliver Upton
oupton at kernel.org
Tue Dec 1 20:11:11 PST 2026
On Thu, Nov 19, 2026 at 06:52:00PM +0000, Colton Lewis wrote:
> The KVM PMU event filter uAPI requires disallowed events not to count.
> Because userspace can install or update the event filter independently
> of guest register writes, re-apply the filter on every vCPU load as well
> as on trapped PMEVTYPER<n>_EL0 and PMCCFILTR_EL0 writes, excluding all
> exception levels in hardware whenever the selected event is blocked.
>
> When running in a nested hypervisor context (is_hyp_ctxt(vcpu)), L1
> vEL2 executes at physical NS-EL1: remap guest ARMV8_PMU_INCLUDE_EL2 onto
> physical EL1 (clearing ARMV8_PMU_EXCLUDE_NS_EL1 so P == NSK) and honor
> guest MDCR_EL2.HPMD for event counters 0..HPMN-1 and MDCR_EL2.HCCD for
> PMCCNTR_EL0.
>
> Signed-off-by: Colton Lewis <coltonlewis at google.com>
> ---
> arch/arm64/kvm/pmu-direct.c | 78 +++++++++++++++++++++++++++++++++++++
> 1 file changed, 78 insertions(+)
>
> diff --git a/arch/arm64/kvm/pmu-direct.c b/arch/arm64/kvm/pmu-direct.c
> index 5c8a8c42e353d..ace3e8e781538 100644
> --- a/arch/arm64/kvm/pmu-direct.c
> +++ b/arch/arm64/kvm/pmu-direct.c
> @@ -242,6 +242,67 @@ u64 kvm_pmu_direct_reg_read(struct kvm_vcpu *vcpu, unsigned int reg)
> }
> }
>
> +static void kvm_pmu_apply_single_event_filter(struct kvm_vcpu *vcpu, u8 idx)
> +{
> + u64 guest_counters;
> + u64 evtyper_set = ARMV8_PMU_EXCLUDE_EL0 |
> + ARMV8_PMU_EXCLUDE_EL1;
> + u64 evtyper_clr = ARMV8_PMU_INCLUDE_EL2;
> + bool guest_include_el2;
> + u64 val;
> + u64 evsel;
> +
> + guard(preempt)();
> +
> + if (!kvm_pmu_part_on_cpu(vcpu))
> + return;
> +
> + guest_counters = kvm_vcpu_pmu_guest_counter_mask(vcpu);
> + if (!test_bit(idx, (unsigned long *)&guest_counters))
> + return;
That's a KVM bug.
> + if (idx == ARMV8_PMU_CYCLE_IDX) {
> + val = __vcpu_sys_reg(vcpu, PMCCFILTR_EL0);
> + val &= kvm_pmu_evtyper_mask(vcpu->kvm) &
> + ~kvm_pmu_event_mask(vcpu->kvm);
> + evsel = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
> + } else {
> + val = __vcpu_sys_reg(vcpu, PMEVTYPER0_EL0 + idx);
> + val &= kvm_pmu_evtyper_mask(vcpu->kvm);
> + evsel = val & kvm_pmu_event_mask(vcpu->kvm);
> + }
> +
> + guest_include_el2 = (val & ARMV8_PMU_INCLUDE_EL2);
We have the "kvm_pmc_counts_at_elX()" helpers, those seem like they'd be
useful here.
> + val &= ~evtyper_clr;
> +
> + if (unlikely(is_hyp_ctxt(vcpu))) {
We can do without the unlikely() here.
Thanks,
Oliver
More information about the linux-arm-kernel
mailing list