Skip to contentSkip to conversation

net: stmmac: fix DMA allocation and IRQ failure cleanup

PATCH In review Karl Mehltretter 4 participants 8 messages Oct 14 – Oct 15

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

cover net: stmmac: fix DMA allocation and IRQ failure cleanup

A DMA error-path review for the BeagleV Ahead found three bugs in the common STMMAC code. Partial allocation can call buffer cleanup without the metadata it indexes. A later TX allocation failure leaks the RX resources allocated first. IRQ registration failure can release descriptors and packet buffers while DMA remains active.

Patches 1 and 2 fix allocation cleanup. Patch 3 fixes the IRQ failure unwind independently.

Testing:

- BeagleV Ahead, TH1520 DWMAC 3.70a: guarded baseline/fixed boots for RX and TX allocation failures and normal-open IRQ failure. Baseline runs reached the guarded unsafe boundaries. Fixed runs cleared the tracked resources and stopped DMA before cleanup.
- A matched follow-up applied the same RTL8211F-VD resume repair to all baseline and fixed kernels. At both 100 and 1000 Mbit/s, each variant passed eight close/open cycles (32 total), delay-register checks, pings and bidirectional 1 MiB SHA-256 transfers with no RX CRC or DMA-API errors.
- Native-XDP IRQ failure was exercised on the same board. The baseline reached IRQ unwind with DMA active and required a test-only rescue; the fixed kernel stopped DMA before unwind. Both reached descriptor cleanup with DMA inactive and no DMA-API errors.
- The physical native-XDP pair used CONFIG_BPF_JIT=n in both variants to bypass an independent RISC-V execmem Oops after the STMMAC unwind. The default configuration's XDP-open path remains covered by the QEMU fault-injection matrix.
- The exact combined code passed 24 of 24 checked 64 MiB transfers on fresh BeagleV Ahead boots with no DMA-API errors.
- QEMU orangepi-pc/dwmac-sun8i: 36 of 36 cold boots passed across the allocation and IRQ matrices, including XDP-open IRQ failure.
- The exact combined code cross-built the STMMAC objects and BeagleV Ahead DTB with a RISC-V defconfig and DWMAC_THEAD enabled.

The guards and fault-injection hooks are test-only and are not in this series. The RTL8211F-VD repair is being submitted separately.

Based on 24a19b953e2a (Linux 7.3-rc7).

Karl Mehltretter (3):
net: stmmac: handle partially allocated DMA queues
net: stmmac: free RX resources when TX allocation fails
net: stmmac: stop DMA when IRQ registration fails

 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 26 +++++++++++++++----
 1 file changed, 21 insertions(+), 5 deletions(-)

base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907

Signature · 1 line
2.39.5 (Apple Git-154)
#1 lore raw

PATCH 1/3 net: stmmac: handle partially allocated DMA queues

DMA allocation failure unwinds every configured queue, including the failed queue and queues not yet allocated. The cleanup helpers then dereference buffer arrays that may not exist.

Only traverse allocated buffer arrays. Clear freed pointers because the queue structures persist and XDP reconfiguration can reuse them.

Found by inspection. On a BeagleV Ahead (TH1520), fault injection at the RX buffer metadata and TX DMA metadata allocations reached these invalid cleanup paths before a test guard stopped them. With this change, cleanup completed without the guard.

Fixes 54139cf3bb33 ("net: stmmac: adding multiple buffers for rx")
Fixes ce736788e8a9 ("net: stmmac: adding multiple buffers for TX")
Assisted-by LLM
---
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 22 ++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +17 -5
index b837e8e27..495ff7aef 100644
@@ -2144,10 +2144,12 @@ static void __free_dma_rx_desc_resources(struct stmmac_priv *priv,
 	void *addr;
 
 	/* Release the DMA RX socket buffers */
-	if (rx_q->xsk_pool)
-		dma_free_rx_xskbufs(priv, dma_conf, queue);
-	else
-		dma_free_rx_skbufs(priv, dma_conf, queue);
+	if (rx_q->buf_pool) {
+		if (rx_q->xsk_pool)
+			dma_free_rx_xskbufs(priv, dma_conf, queue);
+		else
+			dma_free_rx_skbufs(priv, dma_conf, queue);
+	}
 
 	rx_q->buf_alloc_num = 0;
 	rx_q->xsk_pool = NULL;
@@ -2161,13 +2163,17 @@ static void __free_dma_rx_desc_resources(struct stmmac_priv *priv,
 	size = stmmac_get_rx_desc_size(priv) * dma_conf->dma_rx_size;
 
 	dma_free_coherent(priv->device, size, addr, rx_q->dma_rx_phy);
+	rx_q->dma_erx = NULL;
+	rx_q->dma_rx = NULL;
 
 	if (xdp_rxq_info_is_reg(&rx_q->xdp_rxq))
 		xdp_rxq_info_unreg(&rx_q->xdp_rxq);
 
 	kfree(rx_q->buf_pool);
+	rx_q->buf_pool = NULL;
 	if (rx_q->page_pool)
 		page_pool_destroy(rx_q->page_pool);
+	rx_q->page_pool = NULL;
 }
 
 static void free_dma_rx_desc_resources(struct stmmac_priv *priv,
@@ -2196,7 +2202,8 @@ static void __free_dma_tx_desc_resources(struct stmmac_priv *priv,
 	void *addr;
 
 	/* Release the DMA TX socket buffers */
-	dma_free_tx_skbufs(priv, dma_conf, queue);
+	if (tx_q->tx_skbuff_dma && tx_q->tx_skbuff)
+		dma_free_tx_skbufs(priv, dma_conf, queue);
 
 	if (priv->extend_desc) {
 		addr = tx_q->dma_etx;
@@ -2209,9 +2216,14 @@ static void __free_dma_tx_desc_resources(struct stmmac_priv *priv,
 	size = stmmac_get_tx_desc_size(priv, tx_q) * dma_conf->dma_tx_size;
 
 	dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
+	tx_q->dma_etx = NULL;
+	tx_q->dma_entx = NULL;
+	tx_q->dma_tx = NULL;
 
 	kfree(tx_q->tx_skbuff_dma);
+	tx_q->tx_skbuff_dma = NULL;
 	kfree(tx_q->tx_skbuff);
+	tx_q->tx_skbuff = NULL;
 }
 
 static void free_dma_tx_desc_resources(struct stmmac_priv *priv,
Signature · 1 line
2.39.5 (Apple Git-154)
#3 lore raw

PATCH 2/3 net: stmmac: free RX resources when TX allocation fails

In reply to Karl Mehltretter

alloc_dma_desc_resources() allocates every RX queue before allocating TX queues. If TX allocation fails, its error path only frees TX resources and leaks all RX resources.

Free the RX resources after the TX allocator unwinds its queues.

Found by inspection. On a BeagleV Ahead (TH1520), forcing TX DMA metadata allocation to fail left the RX cleanup event absent. With this change, both TX and RX resources were released.

Fixes 71fedb0198cb ("net: stmmac: break some functions into RX and TX scopes")
Assisted-by LLM
---
 drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2 -0
index 495ff7aef..8fd7d2173 100644
@@ -2425,6 +2425,8 @@ static int alloc_dma_desc_resources(struct stmmac_priv *priv,
 		return ret;
 
 	ret = alloc_dma_tx_desc_resources(priv, dma_conf);
+	if (ret)
+		free_dma_rx_desc_resources(priv, dma_conf);
 
 	return ret;
 }
Signature · 1 line
2.39.5 (Apple Git-154)
#5 lore raw

PATCH 3/3 net: stmmac: stop DMA when IRQ registration fails

In reply to Karl Mehltretter

Both __stmmac_open() and stmmac_xdp_open() start DMA before requesting IRQs. If IRQ registration fails, their error paths release descriptors and packet buffers while the DMA channels remain active.

Stop all DMA channels before releasing those resources, matching the normal close paths.

Found by inspection. On a BeagleV Ahead (TH1520), fault injection in both normal-open and native-XDP-open paths reached IRQ unwind with DMA active on the baseline kernel. With this change, the DMA process states were idle before unwind and resource cleanup in both paths.

Fixes 523f11b5d4fd ("net: stmmac: move hardware setup for stmmac_open to new function")
Fixes ac746c8520d9 ("net: stmmac: enhance XDP ZC driver level switching performance")
Assisted-by LLM
---
 drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2 -0
index 8fd7d2173..5c4df26a6 100644
@@ -4206,6 +4206,7 @@ irq_error:
 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
 		hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
 
+	stmmac_stop_all_dma(priv);
 	stmmac_release_ptp(priv);
 init_error:
 	return ret;
@@ -7259,6 +7260,7 @@ int stmmac_xdp_open(struct net_device *dev)
 irq_error:
 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
 		hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
+	stmmac_stop_all_dma(priv);
 
 init_error:
 	free_dma_desc_resources(priv, &priv->dma_conf);
Signature · 1 line
2.39.5 (Apple Git-154)
#6 lore raw

Re: [PATCH net 3/3] net: stmmac: stop DMA when IRQ registration fails

Hi,

Quoted 11 lines

Both __stmmac_open() and stmmac_xdp_open() start DMA before requesting IRQs. If IRQ registration fails, their error paths release descriptors and packet buffers while the DMA channels remain active.

Stop all DMA channels before releasing those resources, matching the normal close paths.

Found by inspection. On a BeagleV Ahead (TH1520), fault injection in both normal-open and native-XDP-open paths reached IRQ unwind with DMA active on the baseline kernel. With this change, the DMA process states were idle before unwind and resource cleanup in both paths.

This fix looks suspiciously similar to a fix already accepted into net-next [1].

Quoted 32 lines
Fixes 523f11b5d4fd ("net: stmmac: move hardware setup for stmmac_open to new function")
Fixes ac746c8520d9 ("net: stmmac: enhance XDP ZC driver level switching performance")
Assisted-by LLM
---
 drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2 -0
index 8fd7d2173..5c4df26a6 100644
@@ -4206,6 +4206,7 @@ irq_error:
 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
 		hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
 
+	stmmac_stop_all_dma(priv);
 	stmmac_release_ptp(priv);
 init_error:
 	return ret;
@@ -7259,6 +7260,7 @@ int stmmac_xdp_open(struct net_device *dev)
 irq_error:
 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
 		hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
+	stmmac_stop_all_dma(priv);
 
 init_error:
 	free_dma_desc_resources(priv, &priv->dma_conf);
Signature · 1 line
2.39.5 (Apple Git-154)

[1] https://lore.kernel.org/netdev/32EB352EB66D74E7+20260827025415.2684733-1-zhaojinming@uniontech.com/

#2 lore raw

Re: [PATCH net 1/3] net: stmmac: handle partially allocated DMA queues

In reply to Karl Mehltretter

Hi,

Quoted 12 lines · Karl Mehltretter

DMA allocation failure unwinds every configured queue, including the failed queue and queues not yet allocated. The cleanup helpers then dereference buffer arrays that may not exist.

Only traverse allocated buffer arrays. Clear freed pointers because the queue structures persist and XDP reconfiguration can reuse them.

Found by inspection. On a BeagleV Ahead (TH1520), fault injection at the RX buffer metadata and TX DMA metadata allocations reached these invalid cleanup paths before a test guard stopped them. With this change, cleanup completed without the guard.

Quoted 4 lines
Fixes 54139cf3bb33 ("net: stmmac: adding multiple buffers for rx")
Fixes ce736788e8a9 ("net: stmmac: adding multiple buffers for TX")
Assisted-by LLM

Please look at https://lore.kernel.org/netdev/[email protected]/ .

LLMs keep finding the same bugs again and again, make sure you ask it if there isn't already an in-flight patch for it before sending it :)

Maxime

Quoted 68 lines
---
 .../net/ethernet/stmicro/stmmac/stmmac_main.c | 22 ++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +17 -5
index b837e8e27..495ff7aef 100644
@@ -2144,10 +2144,12 @@ static void __free_dma_rx_desc_resources(struct stmmac_priv *priv,
 	void *addr;
 
 	/* Release the DMA RX socket buffers */
-	if (rx_q->xsk_pool)
-		dma_free_rx_xskbufs(priv, dma_conf, queue);
-	else
-		dma_free_rx_skbufs(priv, dma_conf, queue);
+	if (rx_q->buf_pool) {
+		if (rx_q->xsk_pool)
+			dma_free_rx_xskbufs(priv, dma_conf, queue);
+		else
+			dma_free_rx_skbufs(priv, dma_conf, queue);
+	}
 
 	rx_q->buf_alloc_num = 0;
 	rx_q->xsk_pool = NULL;
@@ -2161,13 +2163,17 @@ static void __free_dma_rx_desc_resources(struct stmmac_priv *priv,
 	size = stmmac_get_rx_desc_size(priv) * dma_conf->dma_rx_size;
 
 	dma_free_coherent(priv->device, size, addr, rx_q->dma_rx_phy);
+	rx_q->dma_erx = NULL;
+	rx_q->dma_rx = NULL;
 
 	if (xdp_rxq_info_is_reg(&rx_q->xdp_rxq))
 		xdp_rxq_info_unreg(&rx_q->xdp_rxq);
 
 	kfree(rx_q->buf_pool);
+	rx_q->buf_pool = NULL;
 	if (rx_q->page_pool)
 		page_pool_destroy(rx_q->page_pool);
+	rx_q->page_pool = NULL;
 }
 
 static void free_dma_rx_desc_resources(struct stmmac_priv *priv,
@@ -2196,7 +2202,8 @@ static void __free_dma_tx_desc_resources(struct stmmac_priv *priv,
 	void *addr;
 
 	/* Release the DMA TX socket buffers */
-	dma_free_tx_skbufs(priv, dma_conf, queue);
+	if (tx_q->tx_skbuff_dma && tx_q->tx_skbuff)
+		dma_free_tx_skbufs(priv, dma_conf, queue);
 
 	if (priv->extend_desc) {
 		addr = tx_q->dma_etx;
@@ -2209,9 +2216,14 @@ static void __free_dma_tx_desc_resources(struct stmmac_priv *priv,
 	size = stmmac_get_tx_desc_size(priv, tx_q) * dma_conf->dma_tx_size;
 
 	dma_free_coherent(priv->device, size, addr, tx_q->dma_tx_phy);
+	tx_q->dma_etx = NULL;
+	tx_q->dma_entx = NULL;
+	tx_q->dma_tx = NULL;
 
 	kfree(tx_q->tx_skbuff_dma);
+	tx_q->tx_skbuff_dma = NULL;
 	kfree(tx_q->tx_skbuff);
+	tx_q->tx_skbuff = NULL;
 }
 
 static void free_dma_tx_desc_resources(struct stmmac_priv *priv,
#4 lore raw

Re: [PATCH net 2/3] net: stmmac: free RX resources when TX allocation fails

In reply to Karl Mehltretter

Hi,

Quoted 6 lines · Karl Mehltretter

alloc_dma_desc_resources() allocates every RX queue before allocating TX queues. If TX allocation fails, its error path only frees TX resources and leaks all RX resources.

Free the RX resources after the TX allocator unwinds its queues.

Quoted 7 lines

Found by inspection. On a BeagleV Ahead (TH1520), forcing TX DMA metadata allocation to fail left the RX cleanup event absent. With this change, both TX and RX resources were released.

Fixes 71fedb0198cb ("net: stmmac: break some functions into RX and TX scopes")
Assisted-by LLM

Also covered by https://lore.kernel.org/netdev/[email protected]/.

Thanks,

Maxime

Quoted 17 lines
---
 drivers/net/ethernet/stmicro/stmmac/stmmac_main.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +2 -0
index 495ff7aef..8fd7d2173 100644
@@ -2425,6 +2425,8 @@ static int alloc_dma_desc_resources(struct stmmac_priv *priv,
 		return ret;
 
 	ret = alloc_dma_tx_desc_resources(priv, dma_conf);
+	if (ret)
+		free_dma_rx_desc_resources(priv, dma_conf);
 
 	return ret;
 }
#7 lore raw

Re: [PATCH net 3/3] net: stmmac: stop DMA when IRQ registration fails

In reply to Karl Mehltretter
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] net/stmmac: stmmac_xdp_set_prog() ignores stmmac_xdp_open() failure - [Medium] net: stmmac: MAC…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] net/stmmac: stmmac_xdp_set_prog() ignores stmmac_xdp_open() failure
- [Medium] net: stmmac: MAC Rx/Tx left enabled on open error paths
--

commit 7ffd7d4ee658f5d19fab03ed54e7532669eeea56
Author: Karl Mehltretter <[email protected]>

net: stmmac: stop DMA when IRQ registration fails

Both __stmmac_open() and stmmac_xdp_open() start DMA before requesting IRQs. If IRQ registration fails, their error paths release descriptors and packet buffers while the DMA channels remain active.

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c static int __stmmac_open(struct net_device *dev, · line 4209

Quoted 9 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1 -0
index 8fd7d21730911..5c4df26a64bd6 100644
@@ -4206,6 +4206,7 @@ static int __stmmac_open(struct net_device *dev,
 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
 		hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
 
+	stmmac_stop_all_dma(priv);

[Severity: Medium]
In this error path, phylink_stop() is called just above this snippet, but it only invokes the driver's mac_link_down callback if the link is considered logically UP.

If the link was never UP, the MAC hardware is left active since it was enabled earlier during stmmac_hw_setup().

Should the MAC be explicitly disabled here so it doesn't remain permanently active?

drivers/net/ethernet/stmicro/stmmac/stmmac_main.c int stmmac_xdp_open(struct net_device *dev) · line 7263

Quoted 8 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +1 -0
 	stmmac_release_ptp(priv);
 init_error:
 	return ret;
@@ -7259,6 +7260,7 @@ int stmmac_xdp_open(struct net_device *dev)
 irq_error:
 	for (chan = 0; chan < priv->plat->tx_queues_to_use; chan++)
 		hrtimer_cancel(&priv->dma_conf.tx_queue[chan].txtimer);
+	stmmac_stop_all_dma(priv);

[Severity: Medium]
Earlier in stmmac_xdp_open(), the MAC is explicitly enabled with stmmac_mac_set(..., true).

However, this error cleanup path lacks the corresponding stmmac_mac_set(..., false).

Will the MAC continue to process incoming packets and potentially overflow the internal MTL FIFOs while the DMA and IRQs are stopped?

[Severity: High]
If we hit this error path, stmmac_xdp_open() cleans up its descriptors and returns an error.

However, its caller stmmac_xdp_set_prog() ignores this return value:

drivers/net/ethernet/stmicro/stmmac/stmmac_xdp.c:stmmac_xdp_set_prog() {
    ...
	if (if_running && need_update)
		stmmac_xdp_open(dev);

	if (prog)
		xdp_features_set_redirect_target(dev, false);

	return 0;
}

If an error occurs here, does this leave the interface functionally dead while still reporting success to userspace?

Quoted 3 lines
drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +0 -0
 init_error:
 	free_dma_desc_resources(priv, &priv->dma_conf);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3