Skip to contentSkip to conversation

PCI: apple: Release port IRQs and domains on driver detach

PATCH New Guangshuo Li 2 participants 2 messages Nov 26

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

PATCH 1/1 PCI: apple: Release port IRQs and domains on driver detach

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")
---
 drivers/pci/controller/pcie-apple.c | 50 ++++++++++++++++++++++-------
 1 file changed, 38 insertions(+), 12 deletions(-)
drivers/pci/controller/pcie-apple.c +38 -12
index dfd0e345bc56..79dac3fb9a05 100644
@@ -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

Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] PCI: apple: missing IS_ERR() check for…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] PCI: apple: missing IS_ERR() check for port->phy in apple_pcie_setup_port() [1]
--

commit 102d3679b8d5966284cf3435e5c387deb0e77c02
Author: Guangshuo Li <[email protected]>

PCI: apple: Release port IRQs and domains on driver detach

This commit registers device-managed cleanup actions for the per-port interrupt domains, link IRQs, and the MSI parent domain, and wires up pci_host_common_remove() on driver detach.

Quoted 4 lines
drivers/pci/controller/pcie-apple.c +0 -0
index dfd0e345bc567..79dac3fb9a05c 100644

[ ... ]

drivers/pci/controller/pcie-apple.c static int apple_pcie_setup_port(struct apple_pcie *pcie, · line 607

Quoted 7 lines
drivers/pci/controller/pcie-apple.c +1 -0
@@ -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);

[Severity: High]
This problem wasn't introduced by this patch, but can apple_pcie_setup_port() [1] dereference an error pointer in port->phy when devm_ioremap_resource() fails?

Later in apple_pcie_setup_port(), port->phy is mapped without an IS_ERR() check and then passed to apple_pcie_setup_refclk() for MMIO access:

drivers/pci/controller/pcie-apple.c:apple_pcie_setup_port() {
	...
	snprintf(name, sizeof(name), "phy%d", port->idx);
	res = platform_get_resource_byname(platform, IORESOURCE_MEM, name);
	if (res)
		port->phy = devm_ioremap_resource(&platform->dev, res);
	else
		port->phy = pcie->base + CORE_PHY_DEFAULT_BASE(port->idx);

	rmw_set(PORT_APPCLK_EN, port->base + PORT_APPCLK);

	/* Assert PERST# before setting up the clock */
	gpiod_set_value_cansleep(reset, 1);

	ret = apple_pcie_setup_refclk(pcie, port);
	...
}

If mapping the named PHY resource fails, rmw_set() in apple_pcie_setup_refclk() will dereference the ERR_PTR() value in port->phy and crash during probe.

[1] https://sashiko.dev/#/bug/linux-7340dc46-b98f-4a1f-909a-9ae73ed0ddc9

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1