Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Claudiu Beznea <claudiu.beznea@tuxon.dev>
To: Felix Gu <ustc.gu@gmail.com>,
	Ryan Wanner <ryan.wanner@microchip.com>,
	Mark Brown <broonie@kernel.org>,
	Nicolas Ferre <nicolas.ferre@microchip.com>,
	Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Radu Pirea <radu.pirea@microchip.com>,
	Richard Genoud <richard.genoud@bootlin.com>,
	Wenyou Yang <wenyou.yang@atmel.com>
Cc: linux-spi@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	Mark Brown <broonie@opensource.wolfsonmicro.com>
Subject: Re: [PATCH 1/2] spi: atmel: fix resource leak on DMA buffer allocation failure
Date: Sat, 16 May 2026 19:05:25 +0300	[thread overview]
Message-ID: <b272b786-75d5-42f2-9622-ae4659722c44@tuxon.dev> (raw)
In-Reply-To: <20260516-atmel-v1-1-674fb4707af6@gmail.com>

Hi, Felix,

On 5/15/26 20:20, Felix Gu wrote:
> The original code set use_dma to false when dma_alloc_coherent() fails,
> so DMA channels allocated earlier were never freed, causing a resource
> leak.
> 
> Fix by moving the bounce buffer allocation into
> atmel_spi_configure_dma() and extending atmel_spi_release_dma() to
> also free the bounce buffers. Any allocation failure in the DMA
> configuration path now rolls back both channels and buffers through
> the same release function.
> 
> Fixes: a9889ed62d06 ("spi: atmel: Implements transfers with bounce buffer")
> Signed-off-by: Felix Gu<ustc.gu@gmail.com>
> ---
>   drivers/spi/spi-atmel.c | 113 ++++++++++++++++++++++++------------------------
>   1 file changed, 57 insertions(+), 56 deletions(-)
> 
> diff --git a/drivers/spi/spi-atmel.c b/drivers/spi/spi-atmel.c
> index 25aa294631c8..e519a86a2b45 100644
> --- a/drivers/spi/spi-atmel.c
> +++ b/drivers/spi/spi-atmel.c
> @@ -559,6 +559,34 @@ static int atmel_spi_dma_slave_config(struct atmel_spi *as, u8 bits_per_word)
>   	return err;
>   }
>   
> +static void atmel_spi_release_dma(struct spi_controller *host,
> +				  struct atmel_spi *as)
> +{
> +	if (host->dma_rx) {
> +		dma_release_channel(host->dma_rx);
> +		host->dma_rx = NULL;
> +	}
> +	if (host->dma_tx) {
> +		dma_release_channel(host->dma_tx);
> +		host->dma_tx = NULL;
> +	}
> +
> +	if (IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) {
> +		if (as->addr_tx_bbuf) {
> +			dma_free_coherent(&as->pdev->dev, SPI_MAX_DMA_XFER,
> +					  as->addr_tx_bbuf,
> +					  as->dma_addr_tx_bbuf);
> +			as->addr_tx_bbuf = NULL;
> +		}
> +		if (as->addr_rx_bbuf) {
> +			dma_free_coherent(&as->pdev->dev, SPI_MAX_DMA_XFER,
> +					  as->addr_rx_bbuf,
> +					  as->dma_addr_rx_bbuf);
> +			as->addr_rx_bbuf = NULL;
> +		}
> +	}
> +}
> +
>   static int atmel_spi_configure_dma(struct spi_controller *host,
>   				   struct atmel_spi *as)
>   {
> @@ -569,7 +597,8 @@ static int atmel_spi_configure_dma(struct spi_controller *host,
>   	if (IS_ERR(host->dma_tx)) {
>   		err = PTR_ERR(host->dma_tx);
>   		dev_dbg(dev, "No TX DMA channel, DMA is disabled\n");
> -		goto error_clear;
> +		host->dma_tx = NULL;
> +		return err;
>   	}
>   
>   	host->dma_rx = dma_request_chan(dev, "rx");
> @@ -580,12 +609,31 @@ static int atmel_spi_configure_dma(struct spi_controller *host,
>   		 * requested tx channel.
>   		 */
>   		dev_dbg(dev, "No RX DMA channel, DMA is disabled\n");
> -		goto error;
> +		host->dma_rx = NULL;
> +		goto err_release_dma;
>   	}
>   
>   	err = atmel_spi_dma_slave_config(as, 8);
>   	if (err)
> -		goto error;
> +		goto err_release_dma;
> +
> +	if (IS_ENABLED(CONFIG_SOC_SAM_V4_V5)) {
> +		as->addr_tx_bbuf = dma_alloc_coherent(dev, SPI_MAX_DMA_XFER,
> +						      &as->dma_addr_tx_bbuf,
> +						      GFP_KERNEL | GFP_DMA);
You could use dmam_alloc_coherent() and avoid bulking the failure path.

Thank you,
Claudiu


  reply	other threads:[~2026-05-16 16:05 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-05-15 17:20 [PATCH 0/2] spi: atmel: two fixes Felix Gu
2026-05-15 17:20 ` [PATCH 1/2] spi: atmel: fix resource leak on DMA buffer allocation failure Felix Gu
2026-05-16 16:05   ` Claudiu Beznea [this message]
2026-05-15 17:20 ` [PATCH 2/2] spi: atmel: fix DMA resource leak on probe error paths Felix Gu
2026-05-16  2:33 ` [PATCH 0/2] spi: atmel: two fixes Mark Brown
2026-05-16  4:41   ` Felix Gu
2026-05-16  6:36     ` Mark Brown

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=b272b786-75d5-42f2-9622-ae4659722c44@tuxon.dev \
    --to=claudiu.beznea@tuxon.dev \
    --cc=alexandre.belloni@bootlin.com \
    --cc=broonie@kernel.org \
    --cc=broonie@opensource.wolfsonmicro.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=nicolas.ferre@microchip.com \
    --cc=radu.pirea@microchip.com \
    --cc=richard.genoud@bootlin.com \
    --cc=ryan.wanner@microchip.com \
    --cc=ustc.gu@gmail.com \
    --cc=wenyou.yang@atmel.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