KVM: arm64: Fixes for guest-only PMU event overflows on Apple Silicon
Subsystems (6)
Oliver Upton
maintainercover KVM: arm64: Fixes for guest-only PMU event overflows on Apple Silicon
A rather annoying detail of the Apple CPU PMU is that the FIQ appears to depend on the event filter configuration / value of HCR_EL2.TGE. Events that filter out the host ELs do not appear to drive the FIQ line when HCR_EL2.TGE=1.
This manifests when using the vPMU for sampling inside of a KVM guest, as the overflowed PMU generates a storm of FIQs that are no longer pending by the time interrupts are unmasked.
This series performs a rather disgusting hack to work around the issue, forcing a pending FIQ by performing a self-IPI when exiting due to a FIQ and the PMU is in an interrupt condition. The mitigation works on all implementations that support PMUv3 traps as the 'fast' IPI feature (IPIs taken as a FIQ) long predates vPMU support.
Applies to 7.4-rc3, tested with various perf record invocations in an L1 KVM guest.
Oliver Upton (4):
KVM: arm64: Track IRQ and FIQ exceptions separately
perf kvm stat: Add new 'ARM_EXCEPTION_FIQ' exception type
KVM: arm64: Let the irqchip provide a fixup for FIQs taken to EL2 irqchip/apple-aic: Work around FIQ storms upon vPMU counter overflow
arch/arm64/include/asm/kvm_asm.h | 12 ++++--- arch/arm64/kvm/arm.c | 5 ++- arch/arm64/kvm/handle_exit.c | 1 + arch/arm64/kvm/hyp/entry.S | 6 ++++ arch/arm64/kvm/hyp/hyp-entry.S | 6 +++- arch/arm64/kvm/hyp/include/hyp/switch.h | 4 +-- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 2 ++ arch/arm64/kvm/vgic/vgic-init.c | 6 ++++ arch/arm64/kvm/vgic/vgic.h | 9 +++++ drivers/irqchip/irq-apple-aic.c | 33 +++++++++++++++++-- include/kvm/arm_vgic.h | 4 +++ include/linux/irqchip/arm-vgic-info.h | 3 ++ .../kvm-stat-arch/arm64_exception_types.h | 2 ++ 13 files changed, 83 insertions(+), 10 deletions(-)
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.47.3
Oliver Upton
maintainerPATCH 1/4 KVM: arm64: Track IRQ and FIQ exceptions separately
Use separate exception codes for IRQs and FIQs so KVM can apply an FIQ-specific workaround on broken hardware.
--- arch/arm64/include/asm/kvm_asm.h | 12 ++++++++---- arch/arm64/kvm/arm.c | 2 +- arch/arm64/kvm/handle_exit.c | 1 + arch/arm64/kvm/hyp/entry.S | 6 ++++++ arch/arm64/kvm/hyp/hyp-entry.S | 6 +++++- arch/arm64/kvm/hyp/include/hyp/switch.h | 4 ++-- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 2 ++ 7 files changed, 25 insertions(+), 8 deletions(-)
arch/arm64/include/asm/kvm_asm.h +8 -4
@@ -12,15 +12,18 @@#include <asm/virt.h> #include <asm/sysreg.h> -#define ARM_EXIT_WITH_SERROR_BIT 31 -#define ARM_EXCEPTION_CODE (x) ((x) & ~(1U << ARM_EXIT_WITH_SERROR_BIT)) -#define ARM_EXCEPTION_IS_TRAP (x) (ARM_EXCEPTION_CODE((x)) == ARM_EXCEPTION_TRAP) -#define ARM_SERROR_PENDING (x) !!((x) & (1U << ARM_EXIT_WITH_SERROR_BIT)) +#define ARM_EXIT_WITH_SERROR_BIT 31 +#define ARM_EXCEPTION_CODE (x) ((x) & ~(1U << ARM_EXIT_WITH_SERROR_BIT)) +#define ARM_EXCEPTION_IS_TRAP (x) (ARM_EXCEPTION_CODE((x)) == ARM_EXCEPTION_TRAP) +#define ARM_EXCEPTION_IS_INTERRUPT (x) ((ARM_EXCEPTION_CODE((x)) == ARM_EXCEPTION_IRQ) || \ + (ARM_EXCEPTION_CODE((x)) == ARM_EXCEPTION_FIQ)) +#define ARM_SERROR_PENDING (x) !!((x) & (1U << ARM_EXIT_WITH_SERROR_BIT))#define ARM_EXCEPTION_IRQ 0#define ARM_EXCEPTION_EL1_SERROR 1#define ARM_EXCEPTION_TRAP 2#define ARM_EXCEPTION_IL 3 +#define ARM_EXCEPTION_FIQ 4/* The hyp-stub will return this for any kvm_call_hyp() call */ #define ARM_EXCEPTION_HYP_GONE HVC_STUB_ERR
@@ -28,6 +31,7 @@ {ARM_EXCEPTION_IRQ, "IRQ" }, \ {ARM_EXCEPTION_EL1_SERROR, "SERROR" }, \ {ARM_EXCEPTION_TRAP, "TRAP" }, \ + {ARM_EXCEPTION_FIQ, "FIQ" }, \ {ARM_EXCEPTION_HYP_GONE, "HYP_GONE" }/*
arch/arm64/kvm/arm.c +1 -1
@@ -1547,7 +1547,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)* context synchronization event) is necessary to ensure that * pending interrupts are taken. */ -if (ARM_EXCEPTION_CODE (ret) ==ARM_EXCEPTION_IRQ ) { +if (ARM_EXCEPTION_IS_INTERRUPT (ret)) {local_irq_enable ();isb ();local_irq_disable ();
arch/arm64/kvm/handle_exit.c +1 -0
@@ -459,6 +459,7 @@ int handle_exit(struct kvm_vcpu *vcpu, int exception_index)switch (exception_index) {case ARM_EXCEPTION_IRQ : +case ARM_EXCEPTION_FIQ :return 1 ;case ARM_EXCEPTION_EL1_SERROR :return 1 ;
arch/arm64/kvm/hyp/entry.S +6 -0
@@ -48,7 +48,13 @@ alternative_else_nop_endif // synchronization event so that callers don't need ISBs for anything // that would usually be synchronized by the ERET. isb + + // Both a FIQ and IRQ could be pending. Arbitrarily give precedence + // to reporting a pending FIQ. mov x0, #ARM_EXCEPTION_IRQ + mov x2, #ARM_EXCEPTION_FIQ + tst x1, #ISR_EL1_F + csel x0, x2, x0, ne ret 1:
arch/arm64/kvm/hyp/hyp-entry.S +5 -1
@@ -80,11 +80,15 @@ el1_trap: b __guest_exit el1_irq: -el1_fiq: get_vcpu_ptr x1, x0 mov x0, #ARM_EXCEPTION_IRQ b __guest_exit +el1_fiq: + get_vcpu_ptr x1, x0 + mov x0, #ARM_EXCEPTION_FIQ + b __guest_exit + el1_error: get_vcpu_ptr x1, x0 mov x0, #ARM_EXCEPTION_EL1_SERROR
arch/arm64/kvm/hyp/include/hyp/switch.h +2 -2
@@ -923,11 +923,11 @@ static inline void synchronize_vcpu_pstate(struct kvm_vcpu *vcpu)static inline bool __fixup_guest_exit (struct kvm_vcpu *vcpu,u64 *exit_code,const exit_handler_fn *handlers) { -if (ARM_EXCEPTION_CODE (*exit_code) !=ARM_EXCEPTION_IRQ ) +if (!ARM_EXCEPTION_IS_INTERRUPT (*exit_code)) vcpu->arch.fault.esr_el2 =read_sysreg_el2 (SYS_ESR );if (ARM_SERROR_PENDING (*exit_code) && -ARM_EXCEPTION_CODE (*exit_code) !=ARM_EXCEPTION_IRQ ) { + !ARM_EXCEPTION_IS_INTERRUPT (*exit_code)) {u8 esr_ec =kvm_vcpu_trap_get_class (vcpu);/*
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2 -0
@@ -732,6 +732,7 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)switch (ARM_EXCEPTION_CODE (hyp_vcpu->exit_code)) {case ARM_EXCEPTION_IRQ : +case ARM_EXCEPTION_FIQ :case ARM_EXCEPTION_EL1_SERROR :case ARM_EXCEPTION_IL :break ;
@@ -787,6 +788,7 @@ static void sync_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu, u32 exit_reason)switch (ARM_EXCEPTION_CODE (exit_reason)) {case ARM_EXCEPTION_IRQ : +case ARM_EXCEPTION_FIQ :break ;case ARM_EXCEPTION_TRAP :/* SError pending: not handled at EL2, the guest replays it. */
Signature · 1 line
2.47.3
Oliver Upton
maintainerPATCH 2/4 perf kvm stat: Add new 'ARM_EXCEPTION_FIQ' exception type
In reply to Oliver UptonAlign the tools definition with the corresponding kernel definition.
--- tools/perf/util/kvm-stat-arch/arm64_exception_types.h | 2 ++ 1 file changed, 2 insertions(+)
tools/perf/util/kvm-stat-arch/arm64_exception_types.h +2 -0
@@ -10,6 +10,7 @@#define ARM_EXCEPTION_EL1_SERROR 1#define ARM_EXCEPTION_TRAP 2#define ARM_EXCEPTION_IL 3 +#define ARM_EXCEPTION_FIQ 4/* The hyp-stub will return this for any kvm_call_hyp() call */ #define ARM_EXCEPTION_HYP_GONE HVC_STUB_ERR
@@ -18,6 +19,7 @@ {ARM_EXCEPTION_EL1_SERROR, "SERROR" }, \ {ARM_EXCEPTION_TRAP, "TRAP" }, \ {ARM_EXCEPTION_IL, "ILLEGAL" }, \ + {ARM_EXCEPTION_FIQ, "FIQ" }, \ {ARM_EXCEPTION_HYP_GONE, "HYP_GONE" }/* Per asm/esr.h */
Signature · 1 line
2.47.3
Oliver Upton
maintainerPATCH 3/4 KVM: arm64: Let the irqchip provide a fixup for FIQs taken to EL2
In reply to Oliver UptonCertain implementations suffer from disappearing FIQs which are no longer pending by the time KVM unmasks interrupts. That's no good; give the irqchip a hook to intervene on FIQ exceptions taken from the guest.
--- arch/arm64/kvm/arm.c | 3 +++ arch/arm64/kvm/vgic/vgic-init.c | 6 ++++++ arch/arm64/kvm/vgic/vgic.h | 9 +++++++++ include/kvm/arm_vgic.h | 4 ++++ include/linux/irqchip/arm-vgic-info.h | 3 +++ 5 files changed, 25 insertions(+)
arch/arm64/kvm/arm.c +3 -0
@@ -1537,6 +1537,9 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)*/ smp_store_release (&vcpu->mode,OUTSIDE_GUEST_MODE ); +/* Fixup a borked asynchronous exception before unmasking IRQs */ +vgic_fixup_async_exception (ret); +/* * We must ensure that any pending interrupts are taken before * we exit guest timing so that timer ticks are accounted as
arch/arm64/kvm/vgic/vgic-init.c +6 -0
@@ -912,6 +912,12 @@ int kvm_vgic_hyp_init(void) ret = -ENODEV ; } +if (gic_kvm_info->fixup_fiq_exception) { + kvm_vgic_global_state.fixup_fiq_exception = gic_kvm_info->fixup_fiq_exception; +static_branch_enable (&kvm_vgic_global_state.needs_fiq_exception_fixup); +kvm_info ("irqchip requires FIQ exception fixup\n" ); + } + kvm_vgic_global_state.maint_irq = gic_kvm_info->maint_irq; out_free:
arch/arm64/kvm/vgic/vgic.h +9 -0
@@ -536,4 +536,13 @@ static inline bool vgic_supports_direct_irqs(struct kvm *kvm)int vgic_its_debug_init (struct kvm_device *dev);void vgic_its_debug_destroy (struct kvm_device *dev); +static inline void vgic_fixup_async_exception (int ret) +{ +if (!static_branch_unlikely (&kvm_vgic_global_state.needs_fiq_exception_fixup)) +return ; + +if (ARM_EXCEPTION_CODE (ret) ==ARM_EXCEPTION_FIQ ) + kvm_vgic_global_state.fixup_fiq_exception (); +} +#endif
include/kvm/arm_vgic.h +4 -0
@@ -202,6 +202,10 @@ struct vgic_global {/* GICv3 system register CPU interface */ struct static_key_false gicv3_cpuif; +/* Hot garbage irqchip that loses FIQs */ +struct static_key_false needs_fiq_exception_fixup; +void (*fixup_fiq_exception)(void ); +/* GICv3 compat mode on a GICv5 host */ bool has_gcie_v3_compat;
include/linux/irqchip/arm-vgic-info.h +3 -0
@@ -43,6 +43,9 @@ struct gic_kvm_info {void __iomem *base;bool non_coherent; } gicv5_irs; + +/* Disappearing FIQs, need to rematerialize */ +void (*fixup_fiq_exception)(void ); };#ifdef CONFIG_KVM
Signature · 1 line
2.47.3
Oliver Upton
maintainerPATCH 4/4 irqchip/apple-aic: Work around FIQ storms upon vPMU counter overflow
In reply to Oliver UptonApple's PMU appears to behave strangely in that the FIQ line level is dependent on the event filter bits as well as the value of HCR_EL2.TGE. This manifests as an FIQ storm when using the vPMU in sampling mode inside of a KVM guest. Those FIQs are no longer pending by the time KVM unmasks interrupts after changing TGE as the host event filters are cleared.
Work around the issue by leaning into the crappiness of the underlying
hardware: pend an FIQ that will actually be taken by queueing a self-IPI for the current CPU.
--- drivers/irqchip/irq-apple-aic.c | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-)
drivers/irqchip/irq-apple-aic.c +31 -2
@@ -554,6 +554,14 @@ static void aic_fiq_eoi(struct irq_data *d) ARCH_TIMER_CTRL_IT_STAT)) == \ (ARCH_TIMER_CTRL_ENABLE | ARCH_TIMER_CTRL_IT_STAT)) +static bool aic_cpu_pmu_fiq_firing (void ) +{ +u64 pmcr0 =read_sysreg_s (SYS_IMP_APL_PMCR0_EL1 ); + +return FIELD_GET (PMCR0_IMODE , pmcr0) ==PMCR0_IMODE_FIQ && + pmcr0 &PMCR0_IACT ; +} + static void __exception_irq_entry aic_handle_fiq(struct pt_regs *regs) {/*
@@ -597,8 +605,7 @@ static void __exception_irq_entry aic_handle_fiq(struct pt_regs *regs) AIC_FIQ_HWIRQ(AIC_TMR_EL02_VIRT)); } -if ((read_sysreg_s (SYS_IMP_APL_PMCR0_EL1 ) & (PMCR0_IMODE |PMCR0_IACT )) == - (FIELD_PREP (PMCR0_IMODE ,PMCR0_IMODE_FIQ ) |PMCR0_IACT )) +if (aic_cpu_pmu_fiq_firing ())generic_handle_domain_irq (aic_irqc->hw_domain,AIC_FIQ_HWIRQ (AIC_CPU_PMU_P ));
@@ -925,6 +932,25 @@ static struct gic_kvm_info vgic_info __initdata = { .no_hw_deactivation = true, }; +static void aic_fixup_guest_fiq_exception (void ) +{ +lockdep_assert_irqs_disabled (); + +/* +* The line level of the PMU appears to evaluate the event filter bits +* (dependent on HCR_EL2.TGE) in addition to the counter overflow state. +* So for PMU events that exclude the host, such as those used for a vPMU, +* the CPU takes an FIQ exception to EL2 which is no longer pending by the +* time KVM unmasks interrupts. +* +* Abuse the absence of a proper irqchip behind the FIQ line and queue a +* self-IPI. aic_handle_fiq() will walk all FIQ sources and detect the +* overflowed PMU. +*/ +if (aic_cpu_pmu_fiq_firing ()) +aic_ipi_send_fast (smp_processor_id ()); +} +static void build_fiq_affinity (struct aic_irq_chip *ic,struct device_node *aff) {int i, n;
@@ -1112,6 +1138,9 @@ static int __init aic_of_ic_init(struct device_node *node, struct device_node *p vgic_info.maint_irq =irq_create_fwspec_mapping (&mi);WARN_ON (!vgic_info.maint_irq); + +if (static_branch_likely (&use_fast_ipi)) + vgic_info.fixup_fiq_exception = aic_fixup_guest_fiq_exception; }vgic_set_kvm_info (&vgic_info);
Signature · 1 line
2.47.3
[email protected]
AI reviewSashiko has reviewed this 4-patch series and found no issues.
Sashiko has reviewed this 4-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Marc Zyngier
maintainerRe: [PATCH 1/4] KVM: arm64: Track IRQ and FIQ exceptions separately
In reply to Oliver UptonOn Tue, 01 Dec 2026 20:44:59 +0000,
Oliver Upton <[email protected]> wrote:
[...]
Quoted 5 lines
arch/arm64/kvm/hyp/entry.S +0
-0
arch/arm64/kvm/hyp/entry.S alternative_else_nop_endif · line 57
Quoted 10 lines
arch/arm64/kvm/hyp/entry.S +6
-0@@ -48,7 +48,13 @@ alternative_else_nop_endif // synchronization event so that callers don't need ISBs for anything // that would usually be synchronized by the ERET. isb + + // Both a FIQ and IRQ could be pending. Arbitrarily give precedence + // to reporting a pending FIQ. mov x0, #ARM_EXCEPTION_IRQ + mov x2, #ARM_EXCEPTION_FIQ + tst x1, #ISR_EL1_F + csel x0, x2, x0, ne
I think this has the potential of going wrong on an NMI capable platform. Not that we support that yet, but it is around the corner. I also wonder if we should simply re-encode the exception types to better align with ISR_EL1. In any case, something like:
orr x1, x1, x1, lsr #(ISR_EL1_FS_SHIFT - ISR_EL1_F_SHIFT) tst x1, #ISR_EL1_F csel x0, x2, x0, ne
should do the trick.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Oliver Upton
maintainerRe: [PATCH 1/4] KVM: arm64: Track IRQ and FIQ exceptions separately
Quoted 26 lines · Marc Zyngier
On Tue, 01 Dec 2026 20:44:59 +0000,
Oliver Upton <[email protected]> wrote:[...]
Quoted 5 lines
arch/arm64/kvm/hyp/entry.S +0
-0arch/arm64/kvm/hyp/entry.S alternative_else_nop_endif · line 57
Quoted 10 lines
arch/arm64/kvm/hyp/entry.S +6
-0@@ -48,7 +48,13 @@ alternative_else_nop_endif // synchronization event so that callers don't need ISBs for anything // that would usually be synchronized by the ERET. isb + + // Both a FIQ and IRQ could be pending. Arbitrarily give precedence + // to reporting a pending FIQ. mov x0, #ARM_EXCEPTION_IRQ + mov x2, #ARM_EXCEPTION_FIQ + tst x1, #ISR_EL1_F + csel x0, x2, x0, neI think this has the potential of going wrong on an NMI capable platform. Not that we support that yet, but it is around the corner. I also wonder if we should simply re-encode the exception types to better align with
ISR_EL1. In any case, something like:
Quoted 5 lines
orr x1, x1, x1, lsr #(ISR_EL1_FS_SHIFT - ISR_EL1_F_SHIFT) tst x1, #ISR_EL1_F csel x0, x2, x0, neshould do the trick.
Yep, works for me. I've got this applied locally, will include it in a respin if that proves necessary.
Thanks,
Oliver