[PATCH net] net: ethernet: mtk_eth_soc: fix potential memory leak in mtk_rx_alloc()

Lorenzo Bianconi lorenzo.bianconi at redhat.com
Sun Nov 20 03:49:01 PST 2022


> When fail to dma_map_single() in mtk_rx_alloc(), it returns directly.
> But the memory allocated for local variable data is not freed, and
> local variabel data has not been attached to ring->data[i] yet, so the
> memory allocated for local variable data will not be freed outside
> mtk_rx_alloc() too. Thus memory leak would occur in this scenario.
> 
> Add skb_free_frag(data) when dma_map_single() failed.
> 
> Fixes: 23233e577ef9 ("net: ethernet: mtk_eth_soc: rely on page_pool for single page buffers")
> Signed-off-by: Ziyang Xuan <william.xuanziyang at huawei.com>
> ---
>  drivers/net/ethernet/mediatek/mtk_eth_soc.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)

Acked-by: Lorenzo Bianconi <lorenzo.bianconi at redhat.com>

> 
> diff --git a/drivers/net/ethernet/mediatek/mtk_eth_soc.c b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
> index 7cd381530aa4..bc47ef1e4dd5 100644
> --- a/drivers/net/ethernet/mediatek/mtk_eth_soc.c
> +++ b/drivers/net/ethernet/mediatek/mtk_eth_soc.c
> @@ -2378,8 +2378,10 @@ static int mtk_rx_alloc(struct mtk_eth *eth, int ring_no, int rx_flag)
>  				data + NET_SKB_PAD + eth->ip_align,
>  				ring->buf_size, DMA_FROM_DEVICE);
>  			if (unlikely(dma_mapping_error(eth->dma_dev,
> -						       dma_addr)))
> +						       dma_addr))) {
> +				skb_free_frag(data);
>  				return -ENOMEM;
> +			}
>  		}
>  		rxd->rxd1 = (unsigned int)dma_addr;
>  		ring->data[i] = data;
> -- 
> 2.25.1
> 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 228 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20221120/fc2be1dd/attachment.sig>


More information about the linux-arm-kernel mailing list