[PATCH net] net: gianfar: fix DMA unmap of time stamped frames at teardown

From: Rosen Penev

Date: Fri Aug 28 2026 - 17:23:12 EST


free_skb_tx_queue() walks the TxBDs assuming each frame occupies
one descriptor plus one more per fragment. A frame sent with
hardware time stamping instead consumes one additional TxBD for
the time stamp buffer, which sits between the FCB and the frame
data and belongs to the head DMA mapping. The current walk then
lands on the wrong descriptors: it treats the time stamp BD as a
fragment (unmapping the still-outstanding time stamp buffer) while
the real fragment descriptors are skipped, so their DMA mappings
leak and remain attached to a skb that is about to be freed.

Fix the walk the same way gfar_clean_tx_ring() does on the
transmit path: identify time stamped frames, derive the head
buffer length from the time stamp BD length plus GMAC_FCB_LEN and
GMAC_TXPAL_LEN, skip the time stamp BD without unmapping it, and
start the fragment recycling on the correct descriptor.

Fixes: f0ee7acfcdd4 ("gianfar: Add hardware TX timestamping support")
Assisted-by: opencode:big-pickle
Signed-off-by: Rosen Penev <rosenp@xxxxxxxxx>
---
drivers/net/ethernet/freescale/gianfar.c | 30 ++++++++++++++++++++----
1 file changed, 25 insertions(+), 5 deletions(-)

diff --git a/drivers/net/ethernet/freescale/gianfar.c b/drivers/net/ethernet/freescale/gianfar.c
index cf636fc5aafa..c5a716d91fd1 100644
--- a/drivers/net/ethernet/freescale/gianfar.c
+++ b/drivers/net/ethernet/freescale/gianfar.c
@@ -1064,21 +1064,41 @@ static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
txbdp = tx_queue->tx_bd_base;

for (i = 0; i < tx_queue->tx_ring_size; i++) {
- if (!tx_queue->tx_skbuff[i])
+ struct sk_buff *skb = tx_queue->tx_skbuff[i];
+ bool do_tstamp;
+ int buflen;
+
+ if (!skb)
continue;

+ do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
+ priv->hwts_tx_en;
+
+ /* Sending a time stamped frame requires two additional
+ * buffers, the time stamp buffer itself being between the
+ * FCB and the actual frame data, all mapped together.
+ */
+ if (unlikely(do_tstamp))
+ buflen = be16_to_cpu(txbdp[1].length) +
+ GMAC_FCB_LEN + GMAC_TXPAL_LEN;
+ else
+ buflen = be16_to_cpu(txbdp->length);
+
dma_unmap_single(priv->dev, be32_to_cpu(txbdp->bufPtr),
- be16_to_cpu(txbdp->length), DMA_TO_DEVICE);
+ buflen, DMA_TO_DEVICE);
txbdp->lstatus = 0;
- for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
- j++) {
+
+ if (unlikely(do_tstamp))
+ txbdp++;
+
+ for (j = 0; j < skb_shinfo(skb)->nr_frags; j++) {
txbdp++;
dma_unmap_page(priv->dev, be32_to_cpu(txbdp->bufPtr),
be16_to_cpu(txbdp->length),
DMA_TO_DEVICE);
}
txbdp++;
- dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
+ dev_kfree_skb_any(skb);
tx_queue->tx_skbuff[i] = NULL;
}
kfree(tx_queue->tx_skbuff);
--
2.55.0