Skip to contentSkip to conversation

net: cpsw: fix netdev cleanup on probe failure

PATCH Applied Karl Mehltretter 4 participants 6 messages Oct 19 – Oct 22

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

PATCH v2 1/1 net: cpsw: fix netdev cleanup on probe failure

The legacy CPSW driver registers its netdevs before requesting IRQs. If a later IRQ request fails in dual-EMAC mode, the probe error path unregisters only the primary netdev. Devres then calls free_netdev() on the still-registered secondary netdev, triggering the reg_state BUG_ON.

Once registered, either interface can queue rx_mode_work through dev_set_rx_mode(). The error path does not drain the work before devres releases the netdev and its private data, allowing the worker to access freed memory.

Use the same per-port cleanup for probe failure and remove. Unregister each netdev whose reg_state is NETREG_REGISTERED, then drain its rx_mode_work. The state check is needed because cpsw_probe_dual_emac() stores the secondary pointer before register_netdev() succeeds.

The workqueue lifetime issue was found during an LLM-assisted review of the 6.12.112-rc1 stable series. Inspecting the same path then exposed the dual-EMAC cleanup bug.

An instrumented KASAN ARM kernel and a QEMU CPSW probe stub reproduced both failures by forcing the TX IRQ request to return -EBUSY. Not tested on real hardware.

Fixes 070f9c658a59 ("net: ethernet: ti: cpsw: Push the request_irq function to the end of probe")
Fixes 0b8c878d1173 ("net: cpsw: Execute ndo_set_rx_mode callback in a work queue")
Assisted-by LLM

---
v2:
- fold the two fixes into one patch (Jakub)
- share registered-port cleanup between probe unwind and remove (Jakub)
- check NETREG_REGISTERED instead of adding a local flag (Jakub)
- add the requested discovery context (netdev bot)
- rebase on net

v1: https://lore.kernel.org/netdev/[email protected]/

Tested:
- ARM W=1 cpsw.o builds with TI_CPSW=y and TI_CPSW=m
- KASAN ARM runs with a QEMU CPSW probe stub. A test hook held rx_mode_work pending while the TX IRQ request failed with -EBUSY. Both failures reproduced without the combined fix; v2 completed single- and dual-EMAC cleanup without KASAN or BUG reports in two runs per mode.

 drivers/net/ethernet/ti/cpsw.c | 33 ++++++++++++++++++++-------------
 1 file changed, 20 insertions(+), 13 deletions(-)
drivers/net/ethernet/ti/cpsw.c +20 -13
index aa3531e844e87..01296c0582bf0 100644
@@ -1524,6 +1524,23 @@ static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
 	return ret;
 }
 
+static void cpsw_unregister_ports(struct cpsw_common *cpsw)
+{
+	struct net_device *ndev;
+	struct cpsw_priv *priv;
+	int i;
+
+	for (i = 0; i < cpsw->data.slaves; i++) {
+		ndev = cpsw->slaves[i].ndev;
+		if (!ndev || ndev->reg_state != NETREG_REGISTERED)
+			continue;
+
+		priv = netdev_priv(ndev);
+		unregister_netdev(ndev);
+		disable_work_sync(&priv->rx_mode_work);
+	}
+}
+
 static const struct of_device_id cpsw_of_mtable[] = {
 	{ .compatible = "ti,cpsw"},
 	{ .compatible = "ti,am335x-cpsw"},
@@ -1764,7 +1781,7 @@ static int cpsw_probe(struct platform_device *pdev)
 	return 0;
 
 clean_unregister_netdev_ret:
-	unregister_netdev(ndev);
+	cpsw_unregister_ports(cpsw);
 clean_cpts:
 	cpts_release(cpsw->cpts);
 	cpdma_ctlr_destroy(cpsw->dma);
@@ -1779,9 +1796,7 @@ static int cpsw_probe(struct platform_device *pdev)
 static void cpsw_remove(struct platform_device *pdev)
 {
 	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
-	struct net_device *ndev;
-	struct cpsw_priv *priv;
-	int i, ret;
+	int ret;
 
 	ret = pm_runtime_resume_and_get(&pdev->dev);
 	if (ret < 0) {
@@ -1793,15 +1808,7 @@ static void cpsw_remove(struct platform_device *pdev)
 		return;
 	}
 
-	for (i = 0; i < cpsw->data.slaves; i++) {
-		ndev = cpsw->slaves[i].ndev;
-		if (!ndev)
-			continue;
-
-		priv = netdev_priv(ndev);
-		unregister_netdev(ndev);
-		disable_work_sync(&priv->rx_mode_work);
-	}
+	cpsw_unregister_ports(cpsw);
 
 	cpts_release(cpsw->cpts);
 	cpdma_ctlr_destroy(cpsw->dma);

base-commit: 7ec48f396a1d89532c0537bc38b408b8c31ee37f

Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw
Quoted 61 lines · Karl Mehltretter

The legacy CPSW driver registers its netdevs before requesting IRQs. If a later IRQ request fails in dual-EMAC mode, the probe error path unregisters only the primary netdev. Devres then calls free_netdev() on the still-registered secondary netdev, triggering the reg_state BUG_ON.

Once registered, either interface can queue rx_mode_work through dev_set_rx_mode(). The error path does not drain the work before devres releases the netdev and its private data, allowing the worker to access freed memory.

Use the same per-port cleanup for probe failure and remove. Unregister each netdev whose reg_state is NETREG_REGISTERED, then drain its rx_mode_work. The state check is needed because cpsw_probe_dual_emac() stores the secondary pointer before register_netdev() succeeds.

The workqueue lifetime issue was found during an LLM-assisted review of the 6.12.112-rc1 stable series. Inspecting the same path then exposed the dual-EMAC cleanup bug.

An instrumented KASAN ARM kernel and a QEMU CPSW probe stub reproduced both failures by forcing the TX IRQ request to return -EBUSY. Not tested on real hardware.

Fixes 070f9c658a59 ("net: ethernet: ti: cpsw: Push the request_irq function to the end of probe")
Fixes 0b8c878d1173 ("net: cpsw: Execute ndo_set_rx_mode callback in a work queue")
Assisted-by LLM

---
v2:
- fold the two fixes into one patch (Jakub)
- share registered-port cleanup between probe unwind and remove (Jakub)
- check NETREG_REGISTERED instead of adding a local flag (Jakub)
- add the requested discovery context (netdev bot)
- rebase on net

v1: https://lore.kernel.org/netdev/[email protected]/

Tested:
- ARM W=1 cpsw.o builds with TI_CPSW=y and TI_CPSW=m
- KASAN ARM runs with a QEMU CPSW probe stub. A test hook held rx_mode_work pending while the TX IRQ request failed with -EBUSY. Both failures reproduced without the combined fix; v2 completed single- and dual-EMAC cleanup without KASAN or BUG reports in two runs per mode.

  drivers/net/ethernet/ti/cpsw.c | 33 ++++++++++++++++++++-------------
  1 file changed, 20 insertions(+), 13 deletions(-)
drivers/net/ethernet/ti/cpsw.c +4 -0
index aa3531e844e87..01296c0582bf0 100644
@@ -1524,6 +1524,23 @@ static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
  	return ret;
  }
 
+static void cpsw_unregister_ports(struct cpsw_common *cpsw)
+{
+	struct net_device *ndev;
+	struct cpsw_priv *priv;

drivers/net/ethernet/ti/cpsw.c static int cpsw_probe_dual_emac(struct cpsw_priv *priv) · line 1540

Quoted 10 lines
drivers/net/ethernet/ti/cpsw.c +10 -0
+	int i;
+
+	for (i = 0; i < cpsw->data.slaves; i++) {
+		ndev = cpsw->slaves[i].ndev;
+		if (!ndev || ndev->reg_state != NETREG_REGISTERED)
+			continue;
+
+		priv = netdev_priv(ndev);
+		unregister_netdev(ndev);
+		disable_work_sync(&priv->rx_mode_work);

Although the above code is a refactor, it appears to me that the correct sequence should be:

	disable_work_sync(&priv->rx_mode_work);
	unregister_netdev(ndev);

RX Mode work (cpsw_ndo_set_rx_mode_work) does have a 'netif_running' check so it might still be fine if the sequence isn't followed.

Please correct me if I am mistaken regarding the above. Rest of the patch looks good to me.

Regards,
Siddharth.

Quoted 45 lines
drivers/net/ethernet/ti/cpsw.c +6 -13
+	}
+}
+
  static const struct of_device_id cpsw_of_mtable[] = {
  	{ .compatible = "ti,cpsw"},
  	{ .compatible = "ti,am335x-cpsw"},
@@ -1764,7 +1781,7 @@ static int cpsw_probe(struct platform_device *pdev)
  	return 0;
 
  clean_unregister_netdev_ret:
-	unregister_netdev(ndev);
+	cpsw_unregister_ports(cpsw);
  clean_cpts:
  	cpts_release(cpsw->cpts);
  	cpdma_ctlr_destroy(cpsw->dma);
@@ -1779,9 +1796,7 @@ static int cpsw_probe(struct platform_device *pdev)
  static void cpsw_remove(struct platform_device *pdev)
  {
  	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
-	struct net_device *ndev;
-	struct cpsw_priv *priv;
-	int i, ret;
+	int ret;
 
  	ret = pm_runtime_resume_and_get(&pdev->dev);
  	if (ret < 0) {
@@ -1793,15 +1808,7 @@ static void cpsw_remove(struct platform_device *pdev)
  		return;
  	}
 
-	for (i = 0; i < cpsw->data.slaves; i++) {
-		ndev = cpsw->slaves[i].ndev;
-		if (!ndev)
-			continue;
-
-		priv = netdev_priv(ndev);
-		unregister_netdev(ndev);
-		disable_work_sync(&priv->rx_mode_work);
-	}
+	cpsw_unregister_ports(cpsw);
 
  	cpts_release(cpsw->cpts);
  	cpdma_ctlr_destroy(cpsw->dma);

base-commit: 7ec48f396a1d89532c0537bc38b408b8c31ee37f

#2 lore raw
Quoted 5 lines · Siddharth Vadapalli

drivers/net/ethernet/ti/cpsw.c static int cpsw_probe_dual_emac(struct cpsw_priv *priv) · line 1540

Quoted 2 lines
drivers/net/ethernet/ti/cpsw.c +2 -0
+		unregister_netdev(ndev);
+		disable_work_sync(&priv->rx_mode_work);

Although the above code is a refactor, it appears to me that the correct sequence should be:

Quoted 8 lines
	disable_work_sync(&priv->rx_mode_work);
	unregister_netdev(ndev);

RX Mode work (cpsw_ndo_set_rx_mode_work) does have a 'netif_running' check so it might still be fine if the sequence isn't followed.

Please correct me if I am mistaken regarding the above. Rest of the patch looks good to me.

Both orders are safe here. The worker and unregister_netdev() serialize on RTNL, so a worker running after unregister exits at !netif_running().

This matches cpsw_new, so I'd keep the current order.

Thanks,
Karl

reviewer Reviewed-by
#3 lore raw
Quoted 11 lines · Karl Mehltretter
Quoted 10 lines · Siddharth Vadapalli

drivers/net/ethernet/ti/cpsw.c static int cpsw_probe_dual_emac(struct cpsw_priv *priv) · line 1540

Quoted 2 lines
drivers/net/ethernet/ti/cpsw.c +2 -0
+		unregister_netdev(ndev);
+		disable_work_sync(&priv->rx_mode_work);

Although the above code is a refactor, it appears to me that the correct sequence should be:

	disable_work_sync(&priv->rx_mode_work);
	unregister_netdev(ndev);

RX Mode work (cpsw_ndo_set_rx_mode_work) does have a 'netif_running' check so it might still be fine if the sequence isn't followed.

Quoted 8 lines
Quoted 3 lines

Please correct me if I am mistaken regarding the above. Rest of the patch looks good to me.

Both orders are safe here. The worker and unregister_netdev() serialize on RTNL, so a worker running after unregister exits at !netif_running().

This matches cpsw_new, so I'd keep the current order.

Thank you for clarifying.

Regards,
Siddharth.

Reviewed-by on v2 1/1
#4 lore raw
In reply to Karl Mehltretter

Hi!

Quoted 23 lines · Karl Mehltretter

The legacy CPSW driver registers its netdevs before requesting IRQs. If a later IRQ request fails in dual-EMAC mode, the probe error path unregisters only the primary netdev. Devres then calls free_netdev() on the still-registered secondary netdev, triggering the reg_state BUG_ON.

Once registered, either interface can queue rx_mode_work through dev_set_rx_mode(). The error path does not drain the work before devres releases the netdev and its private data, allowing the worker to access freed memory.

Use the same per-port cleanup for probe failure and remove. Unregister each netdev whose reg_state is NETREG_REGISTERED, then drain its rx_mode_work. The state check is needed because cpsw_probe_dual_emac() stores the secondary pointer before register_netdev() succeeds.

The workqueue lifetime issue was found during an LLM-assisted review of the 6.12.112-rc1 stable series. Inspecting the same path then exposed the dual-EMAC cleanup bug.

An instrumented KASAN ARM kernel and a QEMU CPSW probe stub reproduced both failures by forcing the TX IRQ request to return -EBUSY. Not tested on real hardware.

Quoted 5 lines
Fixes 070f9c658a59 ("net: ethernet: ti: cpsw: Push the request_irq function to the end of probe")
Fixes 0b8c878d1173 ("net: cpsw: Execute ndo_set_rx_mode callback in a work queue")
Assisted-by LLM
Quoted 88 lines

---
v2:
  - fold the two fixes into one patch (Jakub)
  - share registered-port cleanup between probe unwind and remove (Jakub)
  - check NETREG_REGISTERED instead of adding a local flag (Jakub)
  - add the requested discovery context (netdev bot)
  - rebase on net

v1: https://lore.kernel.org/netdev/[email protected]/

Tested:
  - ARM W=1 cpsw.o builds with TI_CPSW=y and TI_CPSW=m
  - KASAN ARM runs with a QEMU CPSW probe stub. A test hook held rx_mode_work pending while the TX IRQ request failed with -EBUSY. Both failures reproduced without the combined fix; v2 completed single- and dual-EMAC cleanup without KASAN or BUG reports in two runs per mode.

 drivers/net/ethernet/ti/cpsw.c | 33 ++++++++++++++++++++-------------

 1 file changed, 20 insertions(+), 13 deletions(-)
drivers/net/ethernet/ti/cpsw.c +0 -0
index aa3531e844e87..01296c0582bf0 100644
@@ -1524,6 +1524,23 @@ static int cpsw_probe_dual_emac(struct cpsw_priv *priv)
  	return ret;
  }

drivers/net/ethernet/ti/cpsw.c +17 -0
+static void cpsw_unregister_ports(struct cpsw_common *cpsw)
+{
+	struct net_device *ndev;
+	struct cpsw_priv *priv;
+	int i;
+
+	for (i = 0; i < cpsw->data.slaves; i++) {
+		ndev = cpsw->slaves[i].ndev;
+		if (!ndev || ndev->reg_state != NETREG_REGISTERED)
+			continue;
+
+		priv = netdev_priv(ndev);
+		unregister_netdev(ndev);
+		disable_work_sync(&priv->rx_mode_work);
+	}
+}
+

 static const struct of_device_id cpsw_of_mtable[] = { { .compatible = "ti,cpsw"},
  { .compatible = "ti,am335x-cpsw"},

drivers/net/ethernet/ti/cpsw.c +0 -0
@@ -1764,7 +1781,7 @@ static int cpsw_probe(struct platform_device *pdev)
  	return 0;


 clean_unregister_netdev_ret:

drivers/net/ethernet/ti/cpsw.c +1 -1
-	unregister_netdev(ndev);
+	cpsw_unregister_ports(cpsw);

 clean_cpts:
  cpts_release(cpsw->cpts);
  cpdma_ctlr_destroy(cpsw->dma);

drivers/net/ethernet/ti/cpsw.c +1 -3
@@ -1779,9 +1796,7 @@ static int cpsw_probe(struct platform_device *pdev)
  static void cpsw_remove(struct platform_device *pdev)
  {
  	struct cpsw_common *cpsw = platform_get_drvdata(pdev);
-	struct net_device *ndev;
-	struct cpsw_priv *priv;
-	int i, ret;
+	int ret;


  ret = pm_runtime_resume_and_get(&pdev->dev); if (ret < 0) {

drivers/net/ethernet/ti/cpsw.c +0 -0
@@ -1793,15 +1808,7 @@ static void cpsw_remove(struct platform_device *pdev)
  		return;
  	}

drivers/net/ethernet/ti/cpsw.c +1 -9
-	for (i = 0; i < cpsw->data.slaves; i++) {
-		ndev = cpsw->slaves[i].ndev;
-		if (!ndev)
-			continue;
-
-		priv = netdev_priv(ndev);
-		unregister_netdev(ndev);
-		disable_work_sync(&priv->rx_mode_work);
-	}
+	cpsw_unregister_ports(cpsw);


  cpts_release(cpsw->cpts);
  cpdma_ctlr_destroy(cpsw->dma);

base-commit: 7ec48f396a1d89532c0537bc38b408b8c31ee37f

Signature · 3 lines
Alexander Sverdlin
Siemens AG
www.siemens.com
#5 lore raw
In reply to Karl Mehltretter
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 11 lines · On Mon, 19 Oct 2026 08:04:30 +0200 you wrote:

The legacy CPSW driver registers its netdevs before requesting IRQs. If a later IRQ request fails in dual-EMAC mode, the probe error path unregisters only the primary netdev. Devres then calls free_netdev() on the still-registered secondary netdev, triggering the reg_state BUG_ON.

Once registered, either interface can queue rx_mode_work through dev_set_rx_mode(). The error path does not drain the work before devres releases the netdev and its private data, allowing the worker to access freed memory.

[...]

Here is the summary with links:
- [net,v2] net: cpsw: fix netdev cleanup on probe failure

    https://git.kernel.org/netdev/net-next/c/6acb220312d5

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html