iommu/amd: do not remove the shared IRQ domain on intcapxt failure
Subsystems (3)
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.
--- drivers/iommu/amd/init.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
drivers/iommu/amd/init.c +8 -4
@@ -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
Vasant Hegde
maintainerQuoted 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 globaliommu_irqdomain, shared by all IOMMUs. Both failure branches of__iommu_setup_intcapxt()callirq_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 theIOMMU_ENABLEDinit state, one state beforeamd_iommu_enable_interrupts()runs inIOMMU_PCI_INIT, and the error paths never unregister it. On resume,amd_iommu_resume()re-runsamd_iommu_enable_interrupts(); for an IOMMU whose setup failed,int_enabledwas never set, so__iommu_setup_intcapxt()runs again, gets the freed domain fromiommu_get_irqdomain()and passes it toirq_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 theWARN_ON(!radix_tree_empty)inirq_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 viairq_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