Skip to contentSkip to conversation

PCI: apple: use fwnode_irq_get() for port IRQ lookup

PATCH In review Rosen Penev 3 participants 3 messages Jul 22 – Jul 23

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

PATCH 1/1 PCI: apple: use fwnode_irq_get() for port IRQ lookup

Replace irq_of_parse_and_map() with fwnode_irq_get() on the PCIe controller device's fwnode, indexed by port->idx. This is equivalent for an OF-backed fwnode but uses the generic firmware-node API.

Unlike irq_of_parse_and_map(), fwnode_irq_get() returns a positive IRQ or a negative errno and never 0 (it rewrites 0 to -EINVAL). So store the result in an int and check for irq < 0, propagating the error (including -EPROBE_DEFER) instead of the previous unsigned int with an if (!irq) check that could never catch a negative failure.

Built for arm64 (defconfig + CONFIG_PCIE_APPLE) with LLVM=1; drivers/pci/controller/pcie-apple.o compiles cleanly.

Assisted-by opencode:hy3-free
---
 drivers/pci/controller/pcie-apple.c | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)
drivers/pci/controller/pcie-apple.c +4 -5
index c2cffc0659f4..535acf274b78 100644
@@ -400,14 +400,13 @@ static int apple_pcie_port_setup_irq(struct apple_pcie_port *port)
 {
 	struct fwnode_handle *fwnode = &port->np->fwnode;
 	struct apple_pcie *pcie = port->pcie;
-	unsigned int irq;
+	int irq;
 	u32 val = 0;
 
 	/* FIXME: consider moving each interrupt under each port */
-	irq = irq_of_parse_and_map(to_of_node(dev_fwnode(port->pcie->dev)),
-				   port->idx);
-	if (!irq)
-		return -ENXIO;
+	irq = fwnode_irq_get(dev_fwnode(pcie->dev), port->idx);
+	if (irq < 0)
+		return irq;
 
 	port->domain = irq_domain_create_linear(fwnode, 32,
 						&apple_port_irq_domain_ops,
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing teardown for partially initialized ports in apple_pcie_init() leaves dangling chained IRQ handlers and leaked IRQ domains, leading to a Use-After-Free on probe error.
- [High] MSI parent IRQ domain is leaked on probe failure, leading to Use-After-Free on re-probe.
--

--- Patch [1]: [PATCH] PCI: apple: use fwnode_irq_get() for port IRQ lookup --- commit eab8bdc8d6046daad9b1ca1595a1c3a4a1b10540
Author: Rosen Penev <[email protected]>

PCI: apple: use fwnode_irq_get() for port IRQ lookup

Replace irq_of_parse_and_map() with fwnode_irq_get() on the PCIe controller device's fwnode. Propagate the error instead of checking for zero, allowing negative errors like -EPROBE_DEFER to be handled.

Quoted 10 lines
drivers/pci/controller/pcie-apple.c +1 -1
index c2cffc0659f44..535acf274b786 100644
@@ -400,14 +400,13 @@ static int apple_pcie_port_setup_irq(struct apple_pcie_port *port)
 {
 	struct fwnode_handle *fwnode = &port->np->fwnode;
 	struct apple_pcie *pcie = port->pcie;
-	unsigned int irq;
+	int irq;

drivers/pci/controller/pcie-apple.c static int apple_pcie_port_setup_irq(struct apple_pcie_port *port) · line 409

Quoted 10 lines
drivers/pci/controller/pcie-apple.c +3 -4
 	u32 val = 0;
 
 	/* FIXME: consider moving each interrupt under each port */
-	irq = irq_of_parse_and_map(to_of_node(dev_fwnode(port->pcie->dev)),
-				   port->idx);
-	if (!irq)
-		return -ENXIO;
+	irq = fwnode_irq_get(dev_fwnode(pcie->dev), port->idx);
+	if (irq < 0)
+		return irq;

[Severity: High]
This isn't a bug introduced by this patch, but returning the error here (which can now be -EPROBE_DEFER) exposes a missing teardown path in apple_pcie_init().

When apple_pcie_port_setup_irq() returns an error, apple_pcie_setup_port() and then apple_pcie_init() abort without tearing down previously initialized ports:

drivers/pci/controller/pcie-apple.c:apple_pcie_init() {
    ...
    for_each_available_child_of_node_scoped(dev->of_node, of_port) {
        ret = apple_pcie_setup_port(pcie, of_port);
        if (ret) {
            dev_err(dev, "Port %pOF setup fail: %d\n", of_port, ret);
            return ret;
        }
    }
    ...
}

Because the chained handlers (irq_set_chained_handler_and_data) and IRQ domains (irq_domain_create_linear) set up for previous ports are not managed by devres, they remain active.

Devres will free the port structures, so if an interrupt fires or a re-probe occurs, won't apple_port_irq_handler() dereference a freed port pointer?

[Severity: High]
This is also a pre-existing issue, but propagating an error like -EPROBE_DEFER here makes a leak in apple_pcie_probe() much more reachable.

If this error propagates up and causes pci_host_common_init() to fail in apple_pcie_probe(), the MSI parent IRQ domain created earlier by apple_msi_init() is not removed:

drivers/pci/controller/pcie-apple.c:apple_pcie_probe() {
    ...
    ret = apple_msi_init(pcie);
    if (ret)
        return ret;
    ...
    return pci_host_common_init(pdev, bridge, &apple_pcie_cfg_ecam_ops);
}

The domain created via msi_create_parent_irq_domain() is not devres-managed. Devres will free the pcie structure on error, leaving the leaked domain's host_data pointing to freed memory.

On re-probe, IRQ lookups will match the leaked domain, and apple_msi_domain_alloc() will dereference the freed pcie pointer.

Should there be manual cleanup for the MSI domain when pci_host_common_init() fails?

Quoted 3 lines
drivers/pci/controller/pcie-apple.c +0 -0
 	port->domain = irq_domain_create_linear(fwnode, 32,
 						&apple_port_irq_domain_ops,

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer
#2 lore raw
In reply to Rosen Penev
Quoted 5 lines · Rosen Penev

Replace irq_of_parse_and_map() with fwnode_irq_get() on the PCIe controller device's fwnode, indexed by port->idx. This is equivalent for an OF-backed fwnode but uses the generic firmware-node API.

Quoted 8 lines

Unlike irq_of_parse_and_map(), fwnode_irq_get() returns a positive IRQ or a negative errno and never 0 (it rewrites 0 to -EINVAL). So store the result in an int and check for irq < 0, propagating the error (including -EPROBE_DEFER) instead of the previous unsigned int with an if (!irq) check that could never catch a negative failure.

Built for arm64 (defconfig + CONFIG_PCIE_APPLE) with LLVM=1; drivers/pci/controller/pcie-apple.o compiles cleanly.

But what is the point of this change? This driver is strongly OF-only, and there is no parallel universe where it would support ACPI.

But more importantly: -EPROBE_DEFER would make things worse, because there is nothing in the driver that actually deals with a state rollback on probe failure. Today, we rely on failing hard.

As it stands, I don't think this change is worse the hassle it has the potential to generate.

Thanks,

	M.
Signature · 1 line
Without deviation from the norm, progress is not possible.