mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Nicolai Buchwitz <nb@tipi-net.de>
To: "Théo Lebrun" <theo.lebrun@bootlin.com>
Cc: Conor Dooley <conor.dooley@microchip.com>,
	Andrew Lunn <andrew+netdev@lunn.ch>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>,
	Nicolas Ferre <nicolas.ferre@microchip.com>,
	Sean Anderson <sean.anderson@linux.dev>,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Vladimir Kondratiev <vladimir.kondratiev@mobileye.com>,
	Gregory CLEMENT <gregory.clement@bootlin.com>,
	Tawfik Bayouk <tawfik.bayouk@mobileye.com>,
	Thomas Petazzoni <thomas.petazzoni@bootlin.com>,
	stable@vger.kernel.org
Subject: Re: [PATCH net] net: macb: fix dma_alloc_coherent() leak on macb_alloc() error paths
Date: Fri, 18 Sep 2026 22:10:26 +0200	[thread overview]
Message-ID: <f66297b8318f8217778b786d3b02b2e5@tipi-net.de> (raw)
In-Reply-To: <20260918-macb-alloc-leak-v1-1-aba9a3d4f6e3@bootlin.com>

Hi Théo

On 18.9.2026 21:53, Théo Lebrun wrote:
> Fix 3 leaks in macb_alloc() error paths:
> - Tx buffer allocated but crossing a 4G boundary: Tx leaked.
> - Rx buffer allocation fails: Tx leaked.
> - Rx buffer allocated but crossing a 4G boundary: Tx & Rx leaked.
> 
> This is because our error handling calls macb_free(bp) which in turn
> frees the buffers stored in bp->queues[0], but nothing has been stored
> in there. Fix by storing allocated buffers into bp->queues[0] ASAP.
> 
> Fixes: 78d901897b3c ("net: macb: single dma_alloc_coherent() for DMA 
> descriptors")
> Cc: stable@vger.kernel.org
> Signed-off-by: Théo Lebrun <theo.lebrun@bootlin.com>
> ---
>  drivers/net/ethernet/cadence/macb_main.c | 14 ++++++++++++--
>  1 file changed, 12 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/net/ethernet/cadence/macb_main.c 
> b/drivers/net/ethernet/cadence/macb_main.c
> index b8234ac4b602..8e5c034dc3a4 100644
> --- a/drivers/net/ethernet/cadence/macb_main.c
> +++ b/drivers/net/ethernet/cadence/macb_main.c
> @@ -2749,14 +2749,24 @@ static int macb_alloc(struct macb *bp)
> 
>  	size = bp->num_queues * macb_tx_ring_size_per_queue(bp);
>  	tx = dma_alloc_coherent(dev, size, &tx_dma, GFP_KERNEL);
> -	if (!tx || upper_32_bits(tx_dma) != upper_32_bits(tx_dma + size - 1))
> +	if (!tx)
> +		goto out_err;
> +	/* Record the buffer so that the error path frees it. */
> +	bp->queues[0].tx_ring = tx;
> +	bp->queues[0].tx_ring_dma = tx_dma;
> +	if (upper_32_bits(tx_dma) != upper_32_bits(tx_dma + size - 1))
>  		goto out_err;
>  	netdev_dbg(bp->netdev, "Allocated %zu bytes for %u TX rings at %08lx 
> (mapped %p)\n",
>  		   size, bp->num_queues, (unsigned long)tx_dma, tx);
> 
>  	size = bp->num_queues * macb_rx_ring_size_per_queue(bp);
>  	rx = dma_alloc_coherent(dev, size, &rx_dma, GFP_KERNEL);
> -	if (!rx || upper_32_bits(rx_dma) != upper_32_bits(rx_dma + size - 1))
> +	if (!rx)
> +		goto out_err;
> +	/* Record the buffer so that the error path frees it. */
> +	bp->queues[0].rx_ring = rx;
> +	bp->queues[0].rx_ring_dma = rx_dma;
> +	if (upper_32_bits(rx_dma) != upper_32_bits(rx_dma + size - 1))
>  		goto out_err;
>  	netdev_dbg(bp->netdev, "Allocated %zu bytes for %u RX rings at %08lx 
> (mapped %p)\n",
>  		   size, bp->num_queues, (unsigned long)rx_dma, rx);
> 
> ---
> base-commit: 994db8ab9d90c64dd641b7ead6efe2eaea7a50dc
> change-id: 20260918-macb-alloc-leak-ef38989605c7
> 
> Best regards,
> --
> Théo Lebrun <theo.lebrun@bootlin.com>

Reviewed-by: Nicolai Buchwitz <nb@tipi-net.de>

Thanks,
Nicolai

      reply	other threads:[~2026-09-18 20:10 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-18 19:53 Théo Lebrun
2026-09-18 20:10 ` Nicolai Buchwitz [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=f66297b8318f8217778b786d3b02b2e5@tipi-net.de \
    --to=nb@tipi-net.de \
    --cc=andrew+netdev@lunn.ch \
    --cc=conor.dooley@microchip.com \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=gregory.clement@bootlin.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=nicolas.ferre@microchip.com \
    --cc=pabeni@redhat.com \
    --cc=sean.anderson@linux.dev \
    --cc=stable@vger.kernel.org \
    --cc=tawfik.bayouk@mobileye.com \
    --cc=theo.lebrun@bootlin.com \
    --cc=thomas.petazzoni@bootlin.com \
    --cc=vladimir.kondratiev@mobileye.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®