Linux PCI subsystem development
 help / color / mirror / Atom feed
* [PATCH] PCI: apple: Release port IRQs and domains on driver detach
@ 2026-11-26  8:28 Guangshuo Li
  2026-11-26  8:42 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Guangshuo Li @ 2026-11-26  8:28 UTC (permalink / raw)
  To: maz, lpieralisi, kwilczynski, mani, bhelgaas; +Cc: robh, linux-pci

The port handlers and interrupt domains remain registered after their
controller storage is freed on probe failure or module unload. Make the
link IRQ requests and domain teardown device-managed, and stop the PCI
bus before releasing the ports and their shared MSI parent.

This issue was found by our static analysis tool under development.

Fixes: d8fcbe52d7d3 ("PCI: apple: Add INTx and per-port interrupt support")
Signed-off-by: Guangshuo Li <liguangshuo@mail.sdu.edu.cn>
---
 drivers/pci/controller/pcie-apple.c | 50 ++++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 12 deletions(-)

diff --git a/drivers/pci/controller/pcie-apple.c b/drivers/pci/controller/pcie-apple.c
index dfd0e345bc56..79dac3fb9a05 100644
--- a/drivers/pci/controller/pcie-apple.c
+++ b/drivers/pci/controller/pcie-apple.c
@@ -199,6 +199,7 @@ struct apple_pcie_port {
 	void __iomem		*base;
 	void __iomem		*phy;
 	struct irq_domain	*domain;
+	unsigned int		irq;
 	struct list_head	entry;
 	unsigned long		*sid_map;
 	int			sid_map_sz;
@@ -396,6 +397,22 @@ static void apple_port_irq_handler(struct irq_desc *desc)
 	chained_irq_exit(chip, desc);
 }
 
+static void apple_pcie_port_remove_irq(void *data)
+{
+	struct apple_pcie_port *port = data;
+	unsigned int i;
+
+	writel_relaxed(~0, port->base + PORT_INTMSK);
+	writel_relaxed(0, port->base + PORT_MSICFG);
+	irq_set_chained_handler_and_data(port->irq, NULL, NULL);
+	for (i = 0; i < 32; i++)
+		irq_dispose_mapping(irq_find_mapping(port->domain, i));
+	irq_domain_remove(port->domain);
+	irq_dispose_mapping(port->irq);
+	list_del(&port->entry);
+	of_node_put(port->np);
+}
+
 static int apple_pcie_port_setup_irq(struct apple_pcie_port *port)
 {
 	struct fwnode_handle *fwnode = &port->np->fwnode;
@@ -412,8 +429,11 @@ static int apple_pcie_port_setup_irq(struct apple_pcie_port *port)
 	port->domain = irq_domain_create_linear(fwnode, 32,
 						&apple_port_irq_domain_ops,
 						port);
-	if (!port->domain)
+	if (!port->domain) {
+		irq_dispose_mapping(irq);
 		return -ENOMEM;
+	}
+	port->irq = irq;
 
 	/* Disable all interrupts */
 	writel_relaxed(~0, port->base + PORT_INTMSK);
@@ -441,7 +461,8 @@ static int apple_pcie_port_setup_irq(struct apple_pcie_port *port)
 	}
 
 	writel_relaxed(val | PORT_MSICFG_EN, port->base + PORT_MSICFG);
-	return 0;
+	of_node_get(port->np);
+	return devm_add_action_or_reset(pcie->dev, apple_pcie_port_remove_irq, port);
 }
 
 static irqreturn_t apple_pcie_port_irq(int irq, void *data)
@@ -485,16 +506,15 @@ static int apple_pcie_port_register_irqs(struct apple_pcie_port *port)
 				[0]	= port_irqs[i].hwirq,
 			},
 		};
-		unsigned int irq;
-		int ret;
+		int irq, ret;
 
 		irq = irq_domain_alloc_irqs(port->domain, 1, NUMA_NO_NODE,
 					    &fwspec);
-		if (WARN_ON(!irq))
+		if (WARN_ON(irq <= 0))
 			continue;
 
-		ret = request_irq(irq, apple_pcie_port_irq, 0,
-				  port_irqs[i].name, port);
+		ret = devm_request_irq(port->pcie->dev, irq, apple_pcie_port_irq, 0,
+				       port_irqs[i].name, port);
 		WARN_ON(ret);
 	}
 
@@ -582,6 +602,7 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie,
 	port->idx = idx >> 11;
 	port->pcie = pcie;
 	port->np = np;
+	INIT_LIST_HEAD(&port->entry);
 
 	raw_spin_lock_init(&port->lock);
 
@@ -652,9 +673,6 @@ static int apple_pcie_setup_port(struct apple_pcie *pcie,
 	list_add_tail(&port->entry, &pcie->ports);
 	init_completion(&pcie->event);
 
-	/* In the success path, we keep a reference to np around */
-	of_node_get(np);
-
 	ret = apple_pcie_port_register_irqs(port);
 	WARN_ON(ret);
 
@@ -678,6 +696,11 @@ static const struct msi_parent_ops apple_msi_parent_ops = {
 	.init_dev_msi_info	= msi_lib_init_dev_msi_info,
 };
 
+static void apple_msi_remove(void *data)
+{
+	irq_domain_remove(data);
+}
+
 static int apple_msi_init(struct apple_pcie *pcie)
 {
 	struct fwnode_handle *fwnode = dev_fwnode(pcie->dev);
@@ -688,6 +711,7 @@ static int apple_msi_init(struct apple_pcie *pcie)
 		.host_data	= pcie,
 	};
 	struct of_phandle_args args = {};
+	struct irq_domain *domain;
 	int ret;
 
 	ret = of_parse_phandle_with_args(to_of_node(fwnode), "msi-ranges",
@@ -713,11 +737,12 @@ static int apple_msi_init(struct apple_pcie *pcie)
 		return -ENXIO;
 	}
 
-	if (!msi_create_parent_irq_domain(&info, &apple_msi_parent_ops)) {
+	domain = msi_create_parent_irq_domain(&info, &apple_msi_parent_ops);
+	if (!domain) {
 		dev_err(pcie->dev, "failed to create IRQ domain\n");
 		return -ENOMEM;
 	}
-	return 0;
+	return devm_add_action_or_reset(pcie->dev, apple_msi_remove, domain);
 }
 
 static struct apple_pcie *apple_pcie_lookup(struct device *dev)
@@ -886,6 +911,7 @@ MODULE_DEVICE_TABLE(of, apple_pcie_of_match);
 
 static struct platform_driver apple_pcie_driver = {
 	.probe	= apple_pcie_probe,
+	.remove	= pci_host_common_remove,
 	.driver	= {
 		.name			= "pcie-apple",
 		.of_match_table		= apple_pcie_of_match,

base-commit: 4dce97c6c265544342e716e62fd24bbb232590c5
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-26  8:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-26  8:28 [PATCH] PCI: apple: Release port IRQs and domains on driver detach Guangshuo Li
2026-11-26  8:42 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox