Skip to contentSkip to conversation

net: moxa: dispose IRQ mappings on probe failure and device removal

PATCH Superseded Diego Fernando Mancera Gomez 2 participants 4 messages Aug 14

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

PATCH 1/1 net: moxa: dispose IRQ mappings on probe failure and device removal

moxart_mac_probe() maps the interrupt with irq_of_parse_and_map() but never disposes of the mapping on the error paths that follow a successful mapping, and neither does moxart_remove(). Every failed probe (for example EPROBE_DEFER from platform_get_ethdev_address(), DMA allocation failures or register_netdev() failure) and every device removal therefore leaks the interrupt mapping.

Register the disposal as a devres action with devm_add_action_or_reset() immediately after irq_of_parse_and_map() and before devm_request_irq(), so on unwind devres disposes of the mapping only after the handler has been released, and devm_add_action_or_reset() disposes of the mapping itself if the action cannot be registered. Since devres_release_all() runs only after the probe error path frees the netdev, release the handler with devm_free_irq() before free_netdev() on the error paths that follow a successful devm_request_irq(), and likewise in moxart_remove().

Fixes 6c821bd9edc9 ("net: Add MOXA ART SoCs ethernet driver")
---
 drivers/net/ethernet/moxa/moxart_ether.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
drivers/net/ethernet/moxa/moxart_ether.c +21 -0
index 8bd60168624a..7b5720f993ae 100644
@@ -451,6 +451,13 @@ static const struct net_device_ops moxart_netdev_ops = {
 	.ndo_validate_addr	= eth_validate_addr,
 };
 
+static void moxart_irq_dispose_mapping(void *data)
+{
+	unsigned int irq = (unsigned int)(uintptr_t)data;
+
+	irq_dispose_mapping(irq);
+}
+
 static int moxart_mac_probe(struct platform_device *pdev)
 {
 	struct device *p_dev = &pdev->dev;
@@ -459,6 +466,7 @@ static int moxart_mac_probe(struct platform_device *pdev)
 	struct moxart_mac_priv_t *priv;
 	struct resource *res;
 	unsigned int irq;
+	bool irq_requested = false;
 	int ret;
 
 	ndev = alloc_etherdev(sizeof(struct moxart_mac_priv_t));
@@ -472,6 +480,16 @@ static int moxart_mac_probe(struct platform_device *pdev)
 		goto irq_map_fail;
 	}
 
+	/* Dispose of the IRQ mapping after the devres-managed handler has
+	 * been released: devres runs actions in reverse registration order,
+	 * so registering this before devm_request_irq() guarantees the
+	 * correct order on probe unwind and device removal.
+	 */
+	ret = devm_add_action_or_reset(p_dev, moxart_irq_dispose_mapping,
+				       (void *)(uintptr_t)irq);
+	if (ret)
+		goto irq_map_fail;
+
 	priv = netdev_priv(ndev);
 	priv->ndev = ndev;
 	priv->pdev = pdev;
@@ -533,6 +551,7 @@ static int moxart_mac_probe(struct platform_device *pdev)
 		netdev_err(ndev, "devm_request_irq failed\n");
 		goto init_fail;
 	}
+	irq_requested = true;
 
 	ndev->netdev_ops = &moxart_netdev_ops;
 	netif_napi_add_weight(ndev, &priv->napi, moxart_rx_poll, RX_DESC_NUM);
@@ -552,6 +571,8 @@ static int moxart_mac_probe(struct platform_device *pdev)
 
 init_fail:
 	netdev_err(ndev, "init failed\n");
+	if (irq_requested)
+		devm_free_irq(p_dev, irq, ndev);
 	moxart_mac_free_memory(ndev);
 irq_map_fail:
 	free_netdev(ndev);
Signature · 1 line
2.54.0.windows.1
#1 lore raw
Quoted 1 line · Diego Fernando Mancera Gomez

moxart_mac_probe() maps the interrupt with irq_of_parse_and_map()

Since this is not a devm_ API call, please do the cleanup the old way.

Andrew

---
pw-bot: cr

#2 lore raw

PATCH v2 1/1 net: moxa: dispose IRQ mappings on probe failure and device removal

In reply to Diego Fernando Mancera Gomez

moxart_mac_probe() maps the interrupt with irq_of_parse_and_map() but never disposes of the mapping on the error paths that follow a successful mapping, and neither does moxart_remove(). Every failed probe (for example EPROBE_DEFER from platform_get_ethdev_address(), DMA allocation failures or register_netdev() failure) and every device removal therefore leaks the interrupt mapping.

Since irq_of_parse_and_map() is not a devm API, dispose of the mapping the traditional way: all error paths after a successful mapping fall through init_fail to a single irq_dispose_mapping(), and the register_netdev() failure path additionally releases the handler with devm_free_irq() first via a dedicated label, so the devres-managed IRQ is never released against a disposed mapping or a freed netdev. moxart_remove() releases the handler and disposes of the mapping before freeing the netdev.

Fixes 6c821bd9edc9 ("net: Add MOXA ART SoCs ethernet driver")

---
Changes in v2:
- Use the traditional IRQ mapping cleanup path as requested by Andrew Lunn.

 drivers/net/ethernet/moxa/moxart_ether.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
drivers/net/ethernet/moxa/moxart_ether.c +5 -1
index 8bd60168624a..8f79ff24aa05 100644
@@ -543,15 +543,18 @@ static int moxart_mac_probe(struct platform_device *pdev)
 
 	ret = register_netdev(ndev);
 	if (ret)
-		goto init_fail;
+		goto free_irq_dispose;
 
 	netdev_dbg(ndev, "%s: IRQ=%d address=%pM\n",
 		   __func__, ndev->irq, ndev->dev_addr);
 
 	return 0;
 
+free_irq_dispose:
+	devm_free_irq(p_dev, irq, ndev);
 init_fail:
 	netdev_err(ndev, "init failed\n");
+	irq_dispose_mapping(irq);
 	moxart_mac_free_memory(ndev);
 irq_map_fail:
 	free_netdev(ndev);
@@ -564,6 +567,7 @@ static void moxart_remove(struct platform_device *pdev)
 
 	unregister_netdev(ndev);
 	devm_free_irq(&pdev->dev, ndev->irq, ndev);
+	irq_dispose_mapping(ndev->irq);
 	moxart_mac_free_memory(ndev);
 	free_netdev(ndev);
 }
Signature · 1 line
2.54.0.windows.1
#3 lore raw
Quoted 9 lines
drivers/net/ethernet/moxa/moxart_ether.c +1 -1
@@ -543,15 +543,18 @@ static int moxart_mac_probe(struct platform_device *pdev)
 
 	ret = register_netdev(ndev);
 	if (ret)
-		goto init_fail;
+		goto free_irq_dispose;
 

drivers/net/ethernet/moxa/moxart_ether.c static int moxart_mac_probe(struct platform_device *pdev) · line 557

Quoted 10 lines
drivers/net/ethernet/moxa/moxart_ether.c +3 -0
 	netdev_dbg(ndev, "%s: IRQ=%d address=%pM\n",
 		   __func__, ndev->irq, ndev->dev_addr);
 
 	return 0;
 
+free_irq_dispose:
+	devm_free_irq(p_dev, irq, ndev);
 init_fail:
 	netdev_err(ndev, "init failed\n");
+	irq_dispose_mapping(irq);

Calling devm_free_FOO() suggests there are other problems, likely a bad conversion to devm_FOO().

Please take a look at the whole of probe() and fix all its problems.

Also, please read:

https://www.kernel.org/doc/html/latest/process/maintainer-netdev.html

Andrew

---
pw-bot: cr