Re: [PATCH v4 01/12] dmaengine: switchtec-dma: fix double-free in switchtec_dma_free_desc()

[email protected] Tue, 28 Jul 2026 17:34:45 +0000
Newsgroups org.kernel.vger.dmaengine,org.kernel.vger.linux-pci
Message-ID <[email protected]>
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Missing resource cleanup on error paths in switchtec_dma_alloc_chan_resources() causes memory leak of DMA descriptors and rings.
--

--- Patch [1]: [PATCH v4 01/12] dmaengine: switchtec-dma: fix double-free in switchtec_dma_free_desc() ---
commit 5f9c3413d8c3e6b409c7cf9c3bd9dfaa071f19f6
Author: Logan Gunthorpe <[email protected]>

dmaengine: switchtec-dma: fix double-free in switchtec_dma_free_desc()

This commit fixes a double-free in switchtec_dma_free_desc() by setting the
pointers to NULL after freeing them. This ensures that any subsequent calls
to the function are a no-op for resources that have already been released.

> If switchtec_dma_alloc_chan_resources() fails partway through and calls
> it during unwind, then a later retry of alloc_chan_resources() fails
> in switchtec_dma_alloc_desc() before reallocating one of those

[Severity: High]
This is a pre-existing issue, but does switchtec_dma_alloc_chan_resources()
actually call switchtec_dma_free_desc() during unwind?

If switchtec_dma_alloc_desc() succeeds, but a subsequent step fails:

drivers/dma/switchtec_dma.c:switchtec_dma_alloc_chan_resources() {
    ...
	rc = switchtec_dma_alloc_desc(swdma_chan);
	if (rc)
		return rc;

	rc = enable_channel(swdma_chan);
	if (rc)
		return rc;

	rc = reset_channel(swdma_chan);
	if (rc)
		return rc;

	rc = unhalt_channel(swdma_chan);
	if (rc)
		return rc;
    ...
}

It appears we return early without unwinding the allocations made by
switchtec_dma_alloc_desc(). Since the DMA engine core does not automatically
invoke device_free_chan_resources() when device_alloc_chan_resources()
fails, does this result in a permanent leak of the DMA descriptors and
rings?

> diff --git a/drivers/dma/switchtec_dma.c b/drivers/dma/switchtec_dma.c
> index 3ef9286406159..a4a7d66d042de 100644
> --- a/drivers/dma/switchtec_dma.c
> +++ b/drivers/dma/switchtec_dma.c
[ ... ]
> @@ -886,14 +886,18 @@ static void switchtec_dma_free_desc(struct switchtec_dma_chan *swdma_chan)
>  	if (swdma_chan->hw_sq)
>  		dma_free_coherent(swdma_dev->dma_dev.dev, size,
>  				  swdma_chan->hw_sq, swdma_chan->dma_addr_sq);
> +	swdma_chan->hw_sq = NULL;
>  
>  	size = SWITCHTEC_DMA_CQ_SIZE * sizeof(*swdma_chan->hw_cq);
>  	if (swdma_chan->hw_cq)
>  		dma_free_coherent(swdma_dev->dma_dev.dev, size,
>  				  swdma_chan->hw_cq, swdma_chan->dma_addr_cq);
> +	swdma_chan->hw_cq = NULL;
>  
> -	for (i = 0; i < SWITCHTEC_DMA_RING_SIZE; i++)
> +	for (i = 0; i < SWITCHTEC_DMA_RING_SIZE; i++) {
>  		kfree(swdma_chan->desc_ring[i]);
> +		swdma_chan->desc_ring[i] = NULL;
> +	}
>  }

-- 
Sashiko AI review ยท https://sashiko.dev/#/patchset/[email protected]?part=1