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

From: Doug Anderson

Date: Mon Nov 23 2026 - 18:28:40 EST


Hi,

On Wed, Nov 11, 2026 at 7:16 PM Mayank Rungta <mrungta@xxxxxxxxxx> wrote:
>
> When freeing or disabling an interrupt requested via request_nmi(),
> __cleanup_nmi() and __synchronize_hardirq() currently do not wait for
> in-flight instances of the NMI handler on other CPUs to complete, which
> can lead to use-after-free conditions on module unload or driver unbind.
>
> Regular IRQs and NMIs require different synchronization mechanisms:
> - Regular IRQ flow handlers already acquire desc->lock, so tracking
> in-flight execution via IRQD_IRQ_INPROGRESS under desc->lock avoids
> extra atomic operations in the interrupt fast path and extra memory
> barriers during synchronization.
> - NMI handlers cannot acquire desc->lock, and querying hardware
> IRQCHIP_STATE_ACTIVE alone does not cover handler execution after EOI.
> Track in-flight NMI handlers locklessly via an atomic counter
> (desc->nmi_active) in handle_fasteoi_nmi(), paired with a dedicated
> __synchronize_nmi() helper that waits for both nmi_active and
> (when supported) IRQCHIP_STATE_ACTIVE to drain.
>
> Because handle_fasteoi_nmi() runs locklessly and cannot mask lazily
> disabled interrupts in hardware, force unlazy disable
> (_IRQ_DISABLE_UNLAZY) in request_nmi() and mask the interrupt in
> hardware before setting IRQD_IRQ_DISABLED in __irq_disable() (matching
> irq_shutdown()). This prevents a level-triggered NMI from observing
> IRQD_IRQ_DISABLED while still unmasked in hardware and livelocking on
> EOI, and is safe for regular IRQs because __irq_disable() runs under
> desc->lock and no chip disable/mask callbacks inspect
> irqd_irq_disabled().
>
> Update __cleanup_nmi() to serialize teardown, shut down and synchronize
> the NMI before clearing desc->action and NMI state, and warn if
> free_nmi() is called from interrupt context.
>
> Signed-off-by: Mayank Rungta <mrungta@xxxxxxxxxx>
> ---
> include/linux/irqdesc.h | 4 ++
> kernel/irq/chip.c | 35 +++++++++----
> kernel/irq/manage.c | 111 ++++++++++++++++++++++++++++++++++------
> kernel/irq/settings.h | 5 ++
> 4 files changed, 130 insertions(+), 25 deletions(-)

This is a relatively big change, but IMO it's a problem worth solving.
I'd already reviewed this ahead of time, though of course that doesn't
mean I didn't miss anything, the Sashiko feedback being one example.
I'd be happy to add a Reviewed-by tag on v5 with the proposed
resolution to the Sashiko feedback.

-Doug