linux/drivers/net/ethernet/stmicro/stmmac
Furong Xu 66600fac7a net: stmmac: TSO: Fix unbalanced DMA map/unmap for non-paged SKB data
In case the non-paged data of a SKB carries protocol header and protocol
payload to be transmitted on a certain platform that the DMA AXI address
width is configured to 40-bit/48-bit, or the size of the non-paged data
is bigger than TSO_MAX_BUFF_SIZE on a certain platform that the DMA AXI
address width is configured to 32-bit, then this SKB requires at least
two DMA transmit descriptors to serve it.

For example, three descriptors are allocated to split one DMA buffer
mapped from one piece of non-paged data:
    dma_desc[N + 0],
    dma_desc[N + 1],
    dma_desc[N + 2].
Then three elements of tx_q->tx_skbuff_dma[] will be allocated to hold
extra information to be reused in stmmac_tx_clean():
    tx_q->tx_skbuff_dma[N + 0],
    tx_q->tx_skbuff_dma[N + 1],
    tx_q->tx_skbuff_dma[N + 2].
Now we focus on tx_q->tx_skbuff_dma[entry].buf, which is the DMA buffer
address returned by DMA mapping call. stmmac_tx_clean() will try to
unmap the DMA buffer _ONLY_IF_ tx_q->tx_skbuff_dma[entry].buf
is a valid buffer address.

The expected behavior that saves DMA buffer address of this non-paged
data to tx_q->tx_skbuff_dma[entry].buf is:
    tx_q->tx_skbuff_dma[N + 0].buf = NULL;
    tx_q->tx_skbuff_dma[N + 1].buf = NULL;
    tx_q->tx_skbuff_dma[N + 2].buf = dma_map_single();
Unfortunately, the current code misbehaves like this:
    tx_q->tx_skbuff_dma[N + 0].buf = dma_map_single();
    tx_q->tx_skbuff_dma[N + 1].buf = NULL;
    tx_q->tx_skbuff_dma[N + 2].buf = NULL;

On the stmmac_tx_clean() side, when dma_desc[N + 0] is closed by the
DMA engine, tx_q->tx_skbuff_dma[N + 0].buf is a valid buffer address
obviously, then the DMA buffer will be unmapped immediately.
There may be a rare case that the DMA engine does not finish the
pending dma_desc[N + 1], dma_desc[N + 2] yet. Now things will go
horribly wrong, DMA is going to access a unmapped/unreferenced memory
region, corrupted data will be transmited or iommu fault will be
triggered :(

In contrast, the for-loop that maps SKB fragments behaves perfectly
as expected, and that is how the driver should do for both non-paged
data and paged frags actually.

This patch corrects DMA map/unmap sequences by fixing the array index
for tx_q->tx_skbuff_dma[entry].buf when assigning DMA buffer address.

Tested and verified on DWXGMAC CORE 3.20a

Reported-by: Suraj Jaiswal <quic_jsuraj@quicinc.com>
Fixes: f748be531d ("stmmac: support new GMAC4")
Signed-off-by: Furong Xu <0x1207@gmail.com>
Reviewed-by: Hariprasad Kelam <hkelam@marvell.com>
Reviewed-by: Simon Horman <horms@kernel.org>
Link: https://patch.msgid.link/20241021061023.2162701-1-0x1207@gmail.com
Signed-off-by: Paolo Abeni <pabeni@redhat.com>
2024-10-29 11:37:10 +01:00
..
chain_mode.c
common.h
descs.h
descs_com.h
dwmac-anarion.c
dwmac-dwc-qos-eth.c
dwmac-generic.c
dwmac-imx.c
dwmac-ingenic.c
dwmac-intel-plat.c
dwmac-intel.c
dwmac-intel.h
dwmac-ipq806x.c
dwmac-loongson.c
dwmac-loongson1.c
dwmac-lpc18xx.c
dwmac-mediatek.c
dwmac-meson.c
dwmac-meson8b.c
dwmac-qcom-ethqos.c
dwmac-rk.c
dwmac-rzn1.c
dwmac-socfpga.c
dwmac-starfive.c
dwmac-sti.c
dwmac-stm32.c
dwmac-sun8i.c
dwmac-sunxi.c
dwmac-tegra.c net: stmmac: dwmac-tegra: Fix link bring-up sequence 2024-10-15 12:35:17 +02:00
dwmac-visconti.c
dwmac4.h
dwmac4_core.c
dwmac4_descs.c
dwmac4_descs.h
dwmac4_dma.c net: stmmac: dwmac4: Fix high address display by updating reg_space[] from register values 2024-10-29 11:29:40 +01:00
dwmac4_dma.h net: stmmac: dwmac4: Fix high address display by updating reg_space[] from register values 2024-10-29 11:29:40 +01:00
dwmac4_lib.c
dwmac5.c
dwmac5.h
dwmac100.h
dwmac100_core.c
dwmac100_dma.c
dwmac1000.h
dwmac1000_core.c
dwmac1000_dma.c
dwmac_dma.h
dwmac_lib.c
dwxgmac2.h
dwxgmac2_core.c
dwxgmac2_descs.c
dwxgmac2_dma.c
dwxlgmac2.h
enh_desc.c
hwif.c
hwif.h
Kconfig
Makefile
mmc.h
mmc_core.c
norm_desc.c
ring_mode.c
stmmac.h
stmmac_est.c
stmmac_est.h
stmmac_ethtool.c
stmmac_hwtstamp.c
stmmac_main.c net: stmmac: TSO: Fix unbalanced DMA map/unmap for non-paged SKB data 2024-10-29 11:37:10 +01:00
stmmac_mdio.c
stmmac_pci.c
stmmac_pcs.h
stmmac_platform.c
stmmac_platform.h
stmmac_ptp.c
stmmac_ptp.h
stmmac_selftests.c
stmmac_tc.c
stmmac_xdp.c
stmmac_xdp.h