[PATCH v3 2/6] dmaengine: plx_dma: fix descriptor ring leak in alloc_chan_resources()

From: Logan Gunthorpe

Date: Tue Nov 17 2026 - 11:52:54 EST


If the device is removed between plx_dma_alloc_desc() and the pdev
check, the error path frees hw_ring but leaks desc_ring and every
descriptor in it.

Add plx_dma_free_desc() and use it here, in plx_dma_free_chan_resources()
and in the plx_dma_alloc_desc() error path.

Fixes: c2dbcaa8c672 ("dmaengine: plx-dma: Implement hardware initialization and cleanup")
Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
Link: https://lore.kernel.org/dmaengine/20260717223217.B6FBF1F000E9@xxxxxxxxxxxxxxx
Reviewed-by: Frank Li <Frank.Li@xxxxxxx>
Signed-off-by: Logan Gunthorpe <logang@xxxxxxxxxxxx>
---
drivers/dma/plx_dma.c | 24 +++++++++++++++---------
1 file changed, 15 insertions(+), 9 deletions(-)

diff --git a/drivers/dma/plx_dma.c b/drivers/dma/plx_dma.c
index 0a7ad2473cb1..5b0e32fec79b 100644
--- a/drivers/dma/plx_dma.c
+++ b/drivers/dma/plx_dma.c
@@ -374,6 +374,16 @@ static irqreturn_t plx_dma_isr(int irq, void *devid)
return IRQ_HANDLED;
}

+static void plx_dma_free_desc(struct plx_dma_dev *plxdev)
+{
+ int i;
+
+ for (i = 0; i < PLX_DMA_RING_COUNT; i++)
+ kfree(plxdev->desc_ring[i]);
+
+ kfree(plxdev->desc_ring);
+}
+
static int plx_dma_alloc_desc(struct plx_dma_dev *plxdev)
{
struct plx_dma_desc *desc;
@@ -398,9 +408,7 @@ static int plx_dma_alloc_desc(struct plx_dma_dev *plxdev)
return 0;

free_and_exit:
- for (i = 0; i < PLX_DMA_RING_COUNT; i++)
- kfree(plxdev->desc_ring[i]);
- kfree(plxdev->desc_ring);
+ plx_dma_free_desc(plxdev);
return -ENOMEM;
}

@@ -424,7 +432,7 @@ static int plx_dma_alloc_chan_resources(struct dma_chan *chan)
if (!rcu_dereference(plxdev->pdev)) {
rcu_read_unlock();
rc = -ENODEV;
- goto out_free_hw_ring;
+ goto out_free_desc;
}

writel(PLX_REG_CTRL_RESET_VAL, plxdev->bar + PLX_REG_CTRL);
@@ -443,6 +451,8 @@ static int plx_dma_alloc_chan_resources(struct dma_chan *chan)

return PLX_DMA_RING_COUNT;

+out_free_desc:
+ plx_dma_free_desc(plxdev);
out_free_hw_ring:
dma_free_coherent(plxdev->dma_dev.dev, ring_sz, plxdev->hw_ring,
plxdev->hw_ring_dma);
@@ -454,7 +464,6 @@ static void plx_dma_free_chan_resources(struct dma_chan *chan)
struct plx_dma_dev *plxdev = chan_to_plx_dma_dev(chan);
size_t ring_sz = PLX_DMA_RING_COUNT * sizeof(*plxdev->hw_ring);
int irq = -1;
- int i;

spin_lock_bh(&plxdev->ring_lock);
plxdev->ring_active = false;
@@ -474,10 +483,7 @@ static void plx_dma_free_chan_resources(struct dma_chan *chan)

plx_dma_abort_desc(plxdev);

- for (i = 0; i < PLX_DMA_RING_COUNT; i++)
- kfree(plxdev->desc_ring[i]);
-
- kfree(plxdev->desc_ring);
+ plx_dma_free_desc(plxdev);
dma_free_coherent(plxdev->dma_dev.dev, ring_sz, plxdev->hw_ring,
plxdev->hw_ring_dma);

--
2.47.3