Re: [PATCH v1] genirq/msi: Fix the mapping race of wire to MSI interrupts

From: Nickolai Zeldovich

Date: Mon Nov 16 2026 - 08:49:35 EST


On Mon, Nov 9, 2026 at 10:21 AM Thomas Gleixner <tglx@xxxxxxxxxx> wrote:
> Something like the uncompiled below is less intrusive and avoids the
> subtle breakage.

Thank you, your version looks better than what I was proposing. I'm
happy for you to take over this fix (your patch works and passes my
testing against v7.4-rc3), if that's easier for you.

Also, in the same function, there are two other potential races. Let
me know if these seem important enough to fix, and if it's useful for
me to send a patch for them or if you want to take them over as well.

(1) A second mapping of an existing interrupt checks its trigger type
without desc->lock, while __irq_set_trigger() changes the type under
desc->lock in two steps. With CPU0's specifier asking for the same
type that CPU1 sets:

CPU0: irq_create_fwspec_mapping() CPU1: __irq_set_trigger()
irqd_clear(..., IRQD_TRIGGER_MASK)
type == irq_get_trigger_type(virq)
false, reads NONE
irqd_set(..., flags)
irq_get_trigger_type(virq) == NONE
false, reads flags
"type mismatch", return 0

If CPU0 reads NONE in both checks, it calls irqd_set_trigger_type():
an unlocked read-modify-write of the state word that CPU1 writes under
desc->lock. This dates back to b62b2cf5759b and 1e2a7d78499e.

(2) A new wire to MSI interrupt can be requested while its creator
still writes its state without desc->lock:

CPU0: maps the wire first CPU1: second device, same wire
__irq_domain_alloc_irqs()
inserts virq, clears IRQ_NOREQUEST
irq_create_fwspec_mapping()
finds virq
msi_init_virq()
irqd_clr_can_reserve()
loads state
request_irq()
irq_startup(), under desc->lock:
clears DISABLED and MASKED,
sets STARTED
stores state

The interrupt ends up requested but disabled, so handle_fasteoi_irq()
never calls the handler. The irqd_set_trigger_type() at the end of
irq_create_fwspec_mapping() has the same problem.

One fix would be to do these checks and stores under desc->lock. I
reproduced (2) on QEMU riscv64 with two virtio-net-pci devices sharing
an INTx line and a test-only delay in CPU0's window. I reproduced the
"type mismatch" in (1) with a test module that calls
irq_modify_status() as CPU1, but I don't have a way to trigger it from
userspace.

Nickolai.

> ---
> diff --git a/kernel/irq/irqdomain.c b/kernel/irq/irqdomain.c
> index 4fdcb6df5306..10b0ac97b040 100644
> --- a/kernel/irq/irqdomain.c
> +++ b/kernel/irq/irqdomain.c
> @@ -938,6 +938,7 @@ unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
>
> mutex_lock(&domain->root->mutex);
>
> +retry:
> /*
> * If we've already configured this interrupt,
> * don't do it again, or hell will break loose.
> @@ -978,9 +979,17 @@ unsigned int irq_create_fwspec_mapping(struct irq_fwspec *fwspec)
> mutex_unlock(&domain->root->mutex);
> virq = msi_device_domain_alloc_wired(domain, hwirq, type);
> mutex_lock(&domain->root->mutex);
> - } else
> + /*
> + * Handle the case where a concurrent mapping raced and
> + * allocated the interrupt after the mutex_unlock()
> + * above.
> + */
> + if (virq == -EEXIST)
> + goto retry;
> + } else {
> virq = irq_domain_alloc_irqs_locked(domain, -1, 1, NUMA_NO_NODE,
> fwspec, false, NULL);
> + }
> if (virq <= 0) {
> virq = 0;
> goto out;
> diff --git a/kernel/irq/msi.c b/kernel/irq/msi.c
> index e1d85035dbc8..3fdad0063be6 100644
> --- a/kernel/irq/msi.c
> +++ b/kernel/irq/msi.c
> @@ -1572,6 +1572,12 @@ struct msi_map msi_domain_alloc_irq_at(struct device *dev, unsigned int domid, u
> * msi_desc::cookie so the underlying interrupt chip and domain code can
> * retrieve it.
> *
> + * The caller looks @hwirq up before invoking this function, but it cannot
> + * hold the domain lock across the call. Allocations are serialized by the
> + * MSI descriptor lock, so the lookup is repeated under it and a mapping
> + * which a concurrent caller created in the meantime is returned instead
> + * of a second one.
> + *
> * Return: The Linux interrupt number (> 0) or an error code
> */
> int msi_device_domain_alloc_wired(struct irq_domain *domain, unsigned int hwirq,
> @@ -1589,9 +1595,18 @@ int msi_device_domain_alloc_wired(struct irq_domain *domain, unsigned int hwirq,
>
> guard(msi_descs_lock)(dev);
> if (WARN_ON_ONCE(msi_get_device_domain(dev, domid) != domain))
> - map.index = -EINVAL;
> - else
> - map = __msi_domain_alloc_irq_at(dev, domid, MSI_ANY_INDEX, NULL, &icookie);
> + return -EINVAL;
> +
> + /*
> + * A concurrent mapping might have allocated @hwirq already after the
> + * caller dropped the domain mutex. Re-evaluate under the MSI descriptor
> + * lock, which is sufficient as it is the outermost lock for both
> + * alloc() and free().
> + */
> + if (irq_find_mapping(domain, hwirq) > 0)
> + return -EEXIST;
> +
> + map = __msi_domain_alloc_irq_at(dev, domid, MSI_ANY_INDEX, NULL, &icookie);
> return map.index >= 0 ? map.virq : map.index;
> }
>
>
>
>
>
>
>