mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net] net: fec: fix TSO DMA mapping failure handling
@ 2026-11-27  3:39 Keisuke Tsukuda
  2026-11-27  6:29 ` Wei Fang
  0 siblings, 1 reply; 2+ messages in thread
From: Keisuke Tsukuda @ 2026-11-27  3:39 UTC (permalink / raw)
  To: netdev, wei.fang
  Cc: frank.li, shenwei.wang, andrew+netdev, davem, edumazet, kuba,
	pabeni, imx, linux-kernel, Keisuke Tsukuda

From: Keisuke Tsukuda <tkdkei@outlook.jp>

The TSO descriptor helpers free the skb and return NETDEV_TX_OK when
dma_map_single() fails. As this is zero, fec_enet_txq_submit_tso()
continues after the failure. A failed data mapping then reaches
tso_build_data() with the freed skb, causing a use-after-free.

Return -ENOMEM from the helpers without freeing the skb. The caller
can unmap staged descriptors, free the skb once, and return
NETDEV_TX_OK after consuming the failed packet.

That cleanup must retain BD_ENET_TX_WRAP at the end of the ring.
It must also avoid touching descriptors already owned by the MAC:
the helpers currently set READY as each descriptor is built, so a
prior transmission can advance into a partly mapped TSO packet before
a later mapping fails. Leave the first descriptor unready until all
mappings succeed, then publish it after dma_wmb().

The error cleanup in fec_enet_txq_submit_tso() came from commit
61dc1fd9205b ("net: fec: implement TSO descriptor cleanup").
Backports to older trees need that cleanup or an equivalent.

The mismatch was found by inspecting the TSO error path. On an i.MX6Q
board, test-only 7.4.0-rc2 RAM kernels injected a failure before
the first data DMA mapping using an unregistered netdev and fake TX
ring. The unfixed full caller produced a KASAN use-after-free in
tso_build_data(). With this change, a ring-end test kept the first
descriptor unready, retained WRAP, and freed the skb once without a
KASAN report. The probes performed no real DMA map or transmission.
The hardware race and header mapping failure were not reproduced.
Network selftests were not run.
The updated FEC object built under ARM allyesconfig and allmodconfig
with W=1 using GCC 13.3.

Fixes: 06a4df5863f7 ("net: fec: fix improper use of NETDEV_TX_BUSY")
Cc: stable@vger.kernel.org
Signed-off-by: Keisuke Tsukuda <tkdkei@outlook.jp>
---
 drivers/net/ethernet/freescale/fec_main.c | 21 +++++++++++++--------
 1 file changed, 13 insertions(+), 8 deletions(-)

diff --git a/drivers/net/ethernet/freescale/fec_main.c b/drivers/net/ethernet/freescale/fec_main.c
index f7636f23d..a2c760218 100644
--- a/drivers/net/ethernet/freescale/fec_main.c
+++ b/drivers/net/ethernet/freescale/fec_main.c
@@ -746,10 +746,9 @@ fec_enet_txq_put_data_tso(struct fec_enet_priv_tx_q *txq, struct sk_buff *skb,
 
 	addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
 	if (dma_mapping_error(&fep->pdev->dev, addr)) {
-		dev_kfree_skb_any(skb);
 		if (net_ratelimit())
 			netdev_err(ndev, "Tx DMA memory map failed\n");
-		return NETDEV_TX_OK;
+		return -ENOMEM;
 	}
 
 	bdp->cbd_datlen = cpu_to_fec16(size);
@@ -792,8 +791,11 @@ fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
 	unsigned int estatus = 0;
 
 	status = fec16_to_cpu(bdp->cbd_sc);
-	status &= ~BD_ENET_TX_STATS;
-	status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
+	status &= ~(BD_ENET_TX_STATS | BD_ENET_TX_READY);
+	status |= BD_ENET_TX_TC;
+	/* Keep the first descriptor unavailable until all mappings succeed. */
+	if (bdp != txq->bd.cur)
+		status |= BD_ENET_TX_READY;
 
 	bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
 	dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
@@ -808,10 +810,9 @@ fec_enet_txq_put_hdr_tso(struct fec_enet_priv_tx_q *txq,
 		dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
 					hdr_len, DMA_TO_DEVICE);
 		if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
-			dev_kfree_skb_any(skb);
 			if (net_ratelimit())
 				netdev_err(ndev, "Tx DMA memory map failed\n");
-			return NETDEV_TX_OK;
+			return -ENOMEM;
 		}
 	}
 
@@ -901,6 +902,10 @@ static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
 	txq->tx_buf[index].buf_p = skb;
 
 	skb_tx_timestamp(skb);
+	dma_wmb();
+	txq->bd.cur->cbd_sc |= cpu_to_fec16(BD_ENET_TX_READY);
+	/* Make the READY update visible before advancing the ring cursor. */
+	wmb();
 	txq->bd.cur = bdp;
 
 	/* Trigger transmission start */
@@ -922,7 +927,7 @@ static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
 					 DMA_TO_DEVICE);
 
 		/* Clear standard buffer descriptor fields */
-		tmp_bdp->cbd_sc = 0;
+		tmp_bdp->cbd_sc &= cpu_to_fec16(BD_ENET_TX_WRAP);
 		tmp_bdp->cbd_datlen = 0;
 		tmp_bdp->cbd_bufaddr = 0;
 
@@ -937,7 +942,7 @@ static int fec_enet_txq_submit_tso(struct fec_enet_priv_tx_q *txq,
 
 	dev_kfree_skb_any(skb);
 
-	return ret;
+	return NETDEV_TX_OK;
 }
 
 static netdev_tx_t

^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: [PATCH net] net: fec: fix TSO DMA mapping failure handling
  2026-11-27  3:39 [PATCH net] net: fec: fix TSO DMA mapping failure handling Keisuke Tsukuda
@ 2026-11-27  6:29 ` Wei Fang
  0 siblings, 0 replies; 2+ messages in thread
From: Wei Fang @ 2026-11-27  6:29 UTC (permalink / raw)
  To: Keisuke Tsukuda
  Cc: Frank Li, Shenwei Wang, andrew+netdev, davem, edumazet, kuba,
	pabeni, imx, linux-kernel, netdev

> --- a/drivers/net/ethernet/freescale/fec_main.c
> +++ b/drivers/net/ethernet/freescale/fec_main.c
> @@ -746,10 +746,9 @@ fec_enet_txq_put_data_tso(struct
> fec_enet_priv_tx_q *txq, struct sk_buff *skb,
> 
>         addr = dma_map_single(&fep->pdev->dev, data, size, DMA_TO_DEVICE);
>         if (dma_mapping_error(&fep->pdev->dev, addr)) {
> -               dev_kfree_skb_any(skb);
>                 if (net_ratelimit())
>                         netdev_err(ndev, "Tx DMA memory map failed\n");
> -               return NETDEV_TX_OK;
> +               return -ENOMEM;
>         }
> 
>         bdp->cbd_datlen = cpu_to_fec16(size);
> @@ -792,8 +791,11 @@ fec_enet_txq_put_hdr_tso(struct
> fec_enet_priv_tx_q *txq,
>         unsigned int estatus = 0;
> 
>         status = fec16_to_cpu(bdp->cbd_sc);
> -       status &= ~BD_ENET_TX_STATS;
> -       status |= (BD_ENET_TX_TC | BD_ENET_TX_READY);
> +       status &= ~(BD_ENET_TX_STATS | BD_ENET_TX_READY);
> +       status |= BD_ENET_TX_TC;
> +       /* Keep the first descriptor unavailable until all mappings succeed. */
> +       if (bdp != txq->bd.cur)
> +               status |= BD_ENET_TX_READY;
> 
>         bufaddr = txq->tso_hdrs + index * TSO_HEADER_SIZE;
>         dmabuf = txq->tso_hdrs_dma + index * TSO_HEADER_SIZE;
> @@ -808,10 +810,9 @@ fec_enet_txq_put_hdr_tso(struct
> fec_enet_priv_tx_q *txq,
>                 dmabuf = dma_map_single(&fep->pdev->dev, bufaddr,
>                                         hdr_len, DMA_TO_DEVICE);
>                 if (dma_mapping_error(&fep->pdev->dev, dmabuf)) {
> -                       dev_kfree_skb_any(skb);
>                         if (net_ratelimit())
>                                 netdev_err(ndev, "Tx DMA memory map failed\n");
> -                       return NETDEV_TX_OK;
> +                       return -ENOMEM;
>                 }
>         }
> 
> @@ -901,6 +902,10 @@ static int fec_enet_txq_submit_tso(struct
> fec_enet_priv_tx_q *txq,
>         txq->tx_buf[index].buf_p = skb;
> 
>         skb_tx_timestamp(skb);
> +       dma_wmb();
> +       txq->bd.cur->cbd_sc |= cpu_to_fec16(BD_ENET_TX_READY);
> +       /* Make the READY update visible before advancing the ring cursor. */
> +       wmb();

I think using dma_wmb() is sufficient here.


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-27  6:29 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-27  3:39 [PATCH net] net: fec: fix TSO DMA mapping failure handling Keisuke Tsukuda
2026-11-27  6:29 ` Wei Fang

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®