Skip to contentSkip to conversation

iommu/amd: do not remove the shared IRQ domain on intcapxt failure

PATCH In review Chengyu Liu 2 participants 2 messages Nov 4 – Nov 21

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 iommu/amd: do not remove the shared IRQ domain on intcapxt failure

iommu_get_irqdomain() lazily creates a single irq domain and caches it in the global iommu_irqdomain, shared by all IOMMUs. Both failure branches of __iommu_setup_intcapxt() call irq_domain_remove() on it, which frees the domain, but the global pointer is never cleared and keeps handing out the freed domain.

The failure does not prevent reuse: register_syscore() runs in the IOMMU_ENABLED init state, one state before amd_iommu_enable_interrupts() runs in IOMMU_PCI_INIT, and the error paths never unregister it. On resume, amd_iommu_resume() re-runs amd_iommu_enable_interrupts(); for an IOMMU whose setup failed, int_enabled was never set, so __iommu_setup_intcapxt() runs again, gets the freed domain from iommu_get_irqdomain() and passes it to irq_domain_alloc_irqs().

The shared domain also makes a single failure affect the healthy
IOMMUs: if IOMMU#0 set up fine and IOMMU#1's setup fails, the remove frees a domain that still has IOMMU#0's irqs mapped, tripping the WARN_ON(!radix_tree_empty) in irq_domain_remove().

Keep the domain alive on failure; a later attempt (including the resume path) reuses the cached domain. The request_threaded_irq() failure branch still backs out the allocated irq via irq_domain_free_irqs().

Found by static analysis.

Fixes d1adcfbb520c ("iommu/amd: Fix IOMMU interrupt generation in X2APIC mode")
---
 drivers/iommu/amd/init.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/iommu/amd/init.c +8 -4
index 8a410d4aa3..78b11b04dd 100644
@@ -2522,6 +2522,13 @@ static int __iommu_setup_intcapxt(struct amd_iommu *iommu, const char *devname,
 	int irq, ret;
 	int node = dev_to_node(&iommu->dev->dev);
 
+	/*
+	 * The IRQ domain is a cached singleton shared by all IOMMUs; a
+	 * setup failure must not remove it, otherwise the global
+	 * iommu_irqdomain is left dangling (and a shared domain could be
+	 * freed while still mapped by another IOMMU).  Leave it in place
+	 * so a later retry (e.g. resume) can use it.
+	 */
 	domain = iommu_get_irqdomain();
 	if (!domain)
 		return -ENXIO;
@@ -2532,16 +2539,13 @@ static int __iommu_setup_intcapxt(struct amd_iommu *iommu, const char *devname,
 	info.hwirq = hwirq;
 
 	irq = irq_domain_alloc_irqs(domain, 1, node, &info);
-	if (irq < 0) {
-		irq_domain_remove(domain);
+	if (irq < 0)
 		return irq;
-	}
 
 	ret = request_threaded_irq(irq, NULL, thread_fn, IRQF_ONESHOT, devname,
 				   iommu);
 	if (ret) {
 		irq_domain_free_irqs(irq, 1);
-		irq_domain_remove(domain);
 		return ret;
 	}
 
Signature · 1 line
2.25.1
maintainer
#1 lore raw
Quoted 22 lines · Chengyu Liu

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

iommu_get_irqdomain() lazily creates a single irq domain and caches it in the global iommu_irqdomain, shared by all IOMMUs. Both failure branches of __iommu_setup_intcapxt() call irq_domain_remove() on it, which frees the domain, but the global pointer is never cleared and keeps handing out the freed domain.

The failure does not prevent reuse: register_syscore() runs in the IOMMU_ENABLED init state, one state before amd_iommu_enable_interrupts() runs in IOMMU_PCI_INIT, and the error paths never unregister it. On resume, amd_iommu_resume() re-runs amd_iommu_enable_interrupts(); for an IOMMU whose setup failed, int_enabled was never set, so __iommu_setup_intcapxt() runs again, gets the freed domain from iommu_get_irqdomain() and passes it to irq_domain_alloc_irqs().

The shared domain also makes a single failure affect the healthy
IOMMUs: if IOMMU#0 set up fine and IOMMU#1's setup fails, the remove frees a domain that still has IOMMU#0's irqs mapped, tripping the WARN_ON(!radix_tree_empty) in irq_domain_remove().

Quoted 6 lines

Keep the domain alive on failure; a later attempt (including the resume path) reuses the cached domain. The request_threaded_irq() failure branch still backs out the allocated irq via irq_domain_free_irqs().

Found by static analysis.

I have a slightly different patch for this issue which handles all scenarios.

https://lore.kernel.org/linux-iommu/[email protected]/

-Vasant