net: spacemit: clear TX descriptor on fragment mapping failure
Subsystems (3)
PATCH 1/1 net: spacemit: clear TX descriptor on fragment mapping failure
emac_tx_mem_map() writes TX_DESC_0_OWN into the ring descriptor for every slot beyond old_head as soon as that slot's memset()'d local copy is committed with "*tx_desc_addr = tx_desc", i.e. before the buffers for that slot have necessarily all been mapped successfully. If emac_tx_map_frag() then fails on a later fragment, the err_free_skb path calls emac_free_tx_buf() to unmap and drop the skb, but leaves the already-written descriptor memory untouched, and tx_ring->head is never advanced past old_head (the "tx_ring->head = head" store is skipped by the goto).
So a slot between old_head and the rolled-back head can be left with TX_DESC_0_OWN set and buffer_addr_{1,2} pointing at DMA mappings that emac_free_tx_buf() just tore down, while software considers that slot free again. The next successful emac_tx_mem_map() call only rebuilds old_head itself; if the DMA engine auto-advances into the following descriptor once it finishes old_head's packet, it will fetch that stale, already-unmapped address.
emac_tx_clean_desc() already treats emac_free_tx_buf() and clearing the descriptor as a pair when reclaiming completed descriptors; do the same in the mapping failure path.
--- drivers/net/ethernet/spacemit/k1_emac.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/net/ethernet/spacemit/k1_emac.c +3 -0
@@ -803,6 +803,9 @@ static void emac_tx_mem_map(struct emac_priv *priv, struct sk_buff *skb)while (i != head) {emac_free_tx_buf (priv, i); + tx_desc_addr = &((struct emac_desc *)tx_ring->desc_addr)[i]; +memset (tx_desc_addr,0 ,sizeof (*tx_desc_addr)); +if (++i == tx_ring->total_cnt) i =0 ; }
Signature · 1 line
2.55.0
Quoted 22 lines · Muhammad Bilal
emac_tx_mem_map()writesTX_DESC_0_OWNinto the ring descriptor for every slot beyondold_headas soon as that slot's memset()'d local copy is committed with "*tx_desc_addr=tx_desc", i.e. before the buffers for that slot have necessarily all been mapped successfully. Ifemac_tx_map_frag()then fails on a later fragment, theerr_free_skbpath callsemac_free_tx_buf()to unmap and drop the skb, but leaves the already-written descriptor memory untouched, andtx_ring->headis never advanced pastold_head(the "tx_ring->head= head" store is skipped by the goto).So a slot between
old_headand the rolled-back head can be left withTX_DESC_0_OWNset and buffer_addr_{1,2} pointing at DMA mappings thatemac_free_tx_buf()just tore down, while software considers that slot free again. The next successfulemac_tx_mem_map()call only rebuildsold_headitself; if the DMA engine auto-advances into the following descriptor once it finishes old_head's packet, it will fetch that stale, already-unmapped address.
emac_tx_clean_desc()already treatsemac_free_tx_buf()and clearing the descriptor as a pair when reclaiming completed descriptors; do the same in the mapping failure path.
Quoted 5 lines
Fixes bfec6d7f2001 ("net: spacemit: Add K1 Ethernet MAC")Signed-off-by Muhammad Bilal <[email protected]>--- drivers/net/ethernet/spacemit/k1_emac.c | 3 +++ 1 file changed, 3 insertions(+)
Much appreciated. I indeed did not consider this.
Also, a minor nit, but it should probably be:
Thanks,
Vivian "dramforeve" Wang
Quoted 18 lines · Muhammad Bilal
emac_tx_mem_map()writesTX_DESC_0_OWNinto the ring descriptor for every slot beyondold_headas soon as that slot's memset()'d local copy is committed with "*tx_desc_addr=tx_desc", i.e. before the buffers for that slot have necessarily all been mapped successfully. Ifemac_tx_map_frag()then fails on a later fragment, theerr_free_skbpath callsemac_free_tx_buf()to unmap and drop the skb, but leaves the already-written descriptor memory untouched, andtx_ring->headis never advanced pastold_head(the "tx_ring->head= head" store is skipped by the goto).So a slot between
old_headand the rolled-back head can be left withTX_DESC_0_OWNset and buffer_addr_{1,2} pointing at DMA mappings thatemac_free_tx_buf()just tore down, while software considers that slot free again. The next successfulemac_tx_mem_map()call only rebuildsold_headitself; if the DMA engine auto-advances into the following descriptor once it finishes old_head's packet, it will fetch that stale, already-unmapped address.
Quoted 6 lines
emac_tx_clean_desc()already treatsemac_free_tx_buf()and clearing the descriptor as a pair when reclaiming completed descriptors; do the same in the mapping failure path.Fixes bfec6d7f2001 ("net: spacemit: Add K1 Ethernet MAC")Signed-off-by Muhammad Bilal <[email protected]>
Signature · 1 line
Troy Mitchell
This patch was applied to netdev/net.git (main) by Paolo Abeni <[email protected]>:
Hello:
This patch was applied to netdev/net.git (main)
by Paolo Abeni <[email protected]>:
Quoted 11 lines · On Sun, 20 Sep 2026 00:19:37 +0500 you wrote:
emac_tx_mem_map()writesTX_DESC_0_OWNinto the ring descriptor for every slot beyondold_headas soon as that slot's memset()'d local copy is committed with "*tx_desc_addr=tx_desc", i.e. before the buffers for that slot have necessarily all been mapped successfully. Ifemac_tx_map_frag()then fails on a later fragment, theerr_free_skbpath callsemac_free_tx_buf()to unmap and drop the skb, but leaves the already-written descriptor memory untouched, andtx_ring->headis never advanced pastold_head(the "tx_ring->head= head" store is skipped by the goto).[...]
Here is the summary with links:
- net: spacemit: clear TX descriptor on fragment mapping failure
https://git.kernel.org/netdev/net/c/2d14720beb58
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html