Re: [PATCH 1/2] perf/x86: Prevent races between NMIs and switching LVTPC

From: Yosry Ahmed

Date: Tue Dec 01 2026 - 23:18:57 EST


On Tue, Dec 1, 2026 at 6:44 PM Mi, Dapeng <dapeng1.mi@xxxxxxxxxxxxxxx> wrote:
>
>
> On 12/2/2026 1:14 AM, Yosry Ahmed wrote:
> > perf_event_nmi_handler() checks guest_lvtpc_loaded to skip handling NMIs
> > and unmasking LVTPC on Intel (as it is automasked by hardware on PMI).
> > However, when loading the guest LVTPC, perf_load_guest_lvtpc() updates
> > APIC_LVTPC before setting guest_lvtpc_loaded. If an NMI arrives in
> > between, perf_event_nmi_handler() will observe !guest_lvtpc_loaded and
> > proceed.
> >
> > On Intel, the NMI handler will unconditionally write APIC_LVTPC to
> > unmask PMIs, overwriting it to APIC_DM_NMI and clobbering the guest PMI
> > vector. The first guest PMI is delivered as an unknown host NMI (which
> > results in a panic if unknown_nmi_panic/panic_on_unrecovered_nmi is
> > set). Subsequent guest PMIs will be completely lost because
> > perf_event_nmi_handler() will observe guest_lvtpc_loaded and bail
> > without unmasking LVTPC. Recovery only happens when
> > perf_put_guest_lvtpc() restores unmasked host NMIs in APIC_LVTPC.
> >
> > Set guest_lvtpc_loaded before writing APIC_LVTPC in
> > perf_load_guest_lvtpc() to prevent a racing NMI from clobbering LVTPC.
> > The current ordering of perf_put_guest_lvtpc() is correct,
> > guest_lvtpc_loaded should be cleared before writing APIC_LVTPC to ensure
> > that NMIs immediately start unmasking APIC_LVTPC as needed. Document it
> > as such.
> >
> > Fixes: 560ac136f25d ("perf/x86/core: Add APIs to switch to/from mediated PMI vector (for KVM)")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Signed-off-by: Yosry Ahmed <yosry@xxxxxxxxxx>
> > ---
> > arch/x86/events/core.c | 13 ++++++++++++-
> > 1 file changed, 12 insertions(+), 1 deletion(-)
> >
> > diff --git a/arch/x86/events/core.c b/arch/x86/events/core.c
> > index 4e344fb1ae3ed..c6608ada06ebd 100644
> > --- a/arch/x86/events/core.c
> > +++ b/arch/x86/events/core.c
> > @@ -2224,15 +2224,26 @@ void perf_load_guest_lvtpc(u32 guest_lvtpc)
> > {
> > u32 masked = guest_lvtpc & APIC_LVT_MASKED;
> >
> > + /*
> > + * Set guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
>
> I suppose "an NMI" here is a non-PMI NMI, right? If so, we'd better
> emphasize it in the comments.

If everything works correctly, yes, it should be a non-perf NMI. I can
emphasize that, maybe something like "otherwise a (non-PMI) NMI
arriving .."?

>
>
> > + * arriving in between will unconditionally write APIC_DM_NMI to
> > + * APIC_LVTPC on Intel, clobbering the guest's mediated PMI vector.
> > + */
> > + this_cpu_write(guest_lvtpc_loaded, true);
> > + barrier();
> > apic_write(APIC_LVTPC,
> > APIC_DM_FIXED | PERF_GUEST_MEDIATED_PMI_VECTOR | masked);
> > - this_cpu_write(guest_lvtpc_loaded, true);
> > }
> > EXPORT_SYMBOL_FOR_KVM(perf_load_guest_lvtpc);
> >
> > void perf_put_guest_lvtpc(void)
> > {
> > + /*
> > + * Clear guest_lvtpc_loaded before writing APIC_LVTPC, otherwise an NMI
> > + * arriving in between will skip unmasking APIC_LVTPC on Intel.
> > + */
> > this_cpu_write(guest_lvtpc_loaded, false);
> > + barrier();
> > apic_write(APIC_LVTPC, APIC_DM_NMI);
> > }
> > EXPORT_SYMBOL_FOR_KVM(perf_put_guest_lvtpc);