Re: [PATCH v4 1/7] genirq: Synchronize in-flight handlers during NMI teardown

From: Mayank Rungta

Date: Tue Nov 17 2026 - 17:14:15 EST


Sashiko has found one potential issue:

> [Severity: Medium]
> Can __synchronize_nmi() return while handle_fasteoi_nmi() is still running
> on another CPU because it checks desc->nmi_active before querying
> IRQCHIP_STATE_ACTIVE?
>
> Unlike regular IRQ flow handlers, which acquire desc->lock before setting
> IRQD_IRQ_INPROGRESS and calling chip->irq_eoi(), handle_fasteoi_nmi() runs
> without desc->lock. On the NMI CPU, hardware acknowledge sets
> IRQCHIP_STATE_ACTIVE first, then handle_fasteoi_nmi() increments
> desc->nmi_active, then chip->irq_eoi() clears IRQCHIP_STATE_ACTIVE, and
> finally desc->nmi_active is decremented:
>
> CPU0 (NMI) CPU1
> <NMI acknowledged in hw>
> (IRQCHIP_STATE_ACTIVE = true)
> __synchronize_nmi()
> inprogress =
> atomic_read_acquire(&desc->nmi_active);
> -> 0
> handle_fasteoi_nmi()
> atomic_inc(&desc->nmi_active)
> action->handler(...)
> chip->irq_eoi()
> (IRQCHIP_STATE_ACTIVE = false)
> __irq_get_irqchip_state(...,
> IRQCHIP_STATE_ACTIVE, &inprogress);
> -> false
> /* __synchronize_nmi() returns */
> __cleanup_nmi()
> irq_nmi_teardown()
> atomic_dec(&desc->nmi_active) <-- NMI handler still in flight
>
> Querying IRQCHIP_STATE_ACTIVE before reading desc->nmi_active (or
> re-checking desc->nmi_active after observing IRQCHIP_STATE_ACTIVE is
> false) would prevent missing the handover between hardware-active and
> software-active states.
>
> --
> Sashiko AI review · https://sashiko.dev/#/patchset/20261112031629.396819-1-mrungta@xxxxxxxxxx?part=1

The observation is valid in theory, though practically benign for
disable_nmi() and free_nmi():

In that window, IRQD_IRQ_DISABLED is already set by __irq_disable() or
irq_shutdown() before __synchronize_nmi() is called. If
__synchronize_nmi() reads desc->nmi_active == 0 after
smp_mb__after_spinlock(), then the NMI CPU's
atomic_inc(&desc->nmi_active) occurred after IRQD_IRQ_DISABLED was
set, so handle_fasteoi_nmi() is guaranteed to observe
irqd_irq_disabled() == true and skip action->handler().

By the time __synchronize_nmi() reads IRQCHIP_STATE_ACTIVE as false,
chip->irq_eoi() has also completed, leaving only
atomic_dec(&desc->nmi_active) in handle_fasteoi_nmi(). The IRQ mapping
is not torn down in disable_nmi() or free_nmi(), and even if it were
torn down immediately after free_nmi(), free_desc() frees struct
irq_desc via call_rcu(&desc->rcu, delayed_free_desc), which cannot
complete until the NMI CPU exits NMI context (nmi_enter() /
ct_nmi_enter()), so no use-after-free can occur.

Human review feedback on v4 as-is would still be very much
appreciated. I plan to hold off on spinning a v5 just for this and
will swap the check order in __synchronize_nmi() alongside any
maintainer feedback.