Re: [PATCH v3 6/6] dmaengine: plx_dma: don't invoke client callbacks with ring_lock held

From: Frank Li

Date: Tue Nov 17 2026 - 14:31:24 EST


On Tue, Nov 17, 2026 at 10:51:35AM -0600, Logan Gunthorpe wrote:
> plx_dma_process_desc() and plx_dma_abort_desc() invoke the client's
> completion callback with ring_lock held. A callback that submits a new
> transfer, which is common, deadlocks taking the same lock in
> plx_dma_prep_memcpy().
>
> Copy and clear the callback and advance tail under ring_lock, then drop
> it to invoke the callback. To keep callbacks in order and ensure
> plx_dma_free_chan_resources() can't return while one is still running
> from plx_dma_remove(), serialize completion with a new complete_lock
> held across the whole loop. plx_dma_tx_status() only trylocks it, so a
> callback that polls the channel won't deadlock.
>
> Fixes: 4d3df16840a2 ("dmaengine: plx-dma: Implement descriptor submission")
> Reported-by: Sashiko <sashiko-bot@xxxxxxxxxx>
> Link: https://lore.kernel.org/dmaengine/20260717223217.B6FBF1F000E9@xxxxxxxxxxxxxxx
> Signed-off-by: Logan Gunthorpe <logang@xxxxxxxxxxxx>
> ---
> drivers/dma/plx_dma.c | 45 ++++++++++++++++++++++++++++++++++++-------
> 1 file changed, 38 insertions(+), 7 deletions(-)
>
> diff --git a/drivers/dma/plx_dma.c b/drivers/dma/plx_dma.c
> index 725df770d5de..09dc44dc8f61 100644
> --- a/drivers/dma/plx_dma.c
> +++ b/drivers/dma/plx_dma.c
> @@ -108,6 +108,8 @@ struct plx_dma_dev {
> void __iomem *bar;
> struct tasklet_struct desc_task;
>
> + /* serializes descriptor completion and callback invocation */
> + spinlock_t complete_lock;
> spinlock_t ring_lock;
> bool ring_active;
> int head;
> @@ -132,12 +134,15 @@ static struct plx_dma_desc *plx_dma_get_desc(struct plx_dma_dev *plxdev, int i)
> return plxdev->desc_ring[i & (PLX_DMA_RING_COUNT - 1)];
> }
>
> -static void plx_dma_process_desc(struct plx_dma_dev *plxdev)
> +static void plx_dma_process_desc_unlocked(struct plx_dma_dev *plxdev)
> {
> + struct dmaengine_desc_callback cb;
> struct dmaengine_result res;
> struct plx_dma_desc *desc;
> u32 flags;
>
> + lockdep_assert_held(&plxdev->complete_lock);
> +
> spin_lock_bh(&plxdev->ring_lock);
>
> while (plxdev->tail != plxdev->head) {
> @@ -166,22 +171,35 @@ static void plx_dma_process_desc(struct plx_dma_dev *plxdev)
>
> dma_cookie_complete(&desc->txd);
> dma_descriptor_unmap(&desc->txd);
> - dmaengine_desc_get_callback_invoke(&desc->txd, &res);
> + dmaengine_desc_get_callback(&desc->txd, &cb);
> desc->txd.callback = NULL;
> desc->txd.callback_result = NULL;
> -
> plxdev->tail++;
> +
> + spin_unlock_bh(&plxdev->ring_lock);
> + dmaengine_desc_callback_invoke(&cb, &res);

Common method is slice to local complete queue in ring_lock. Then unlock
ring_lock after while loop. Then invoke cb in complete queue.

Frank

> + spin_lock_bh(&plxdev->ring_lock);
> }
>
> spin_unlock_bh(&plxdev->ring_lock);
> }
>
> +static void plx_dma_process_desc(struct plx_dma_dev *plxdev)
> +{
> + spin_lock_bh(&plxdev->complete_lock);
> + plx_dma_process_desc_unlocked(plxdev);
> + spin_unlock_bh(&plxdev->complete_lock);
> +}
> +
> static void plx_dma_abort_desc(struct plx_dma_dev *plxdev)
> {
> + struct dmaengine_desc_callback cb;
> struct dmaengine_result res;
> struct plx_dma_desc *desc;
>
> - plx_dma_process_desc(plxdev);
> + spin_lock_bh(&plxdev->complete_lock);
> +
> + plx_dma_process_desc_unlocked(plxdev);
>
> spin_lock_bh(&plxdev->ring_lock);
>
> @@ -193,14 +211,18 @@ static void plx_dma_abort_desc(struct plx_dma_dev *plxdev)
>
> dma_cookie_complete(&desc->txd);
> dma_descriptor_unmap(&desc->txd);
> - dmaengine_desc_get_callback_invoke(&desc->txd, &res);
> + dmaengine_desc_get_callback(&desc->txd, &cb);
> desc->txd.callback = NULL;
> desc->txd.callback_result = NULL;
> -
> plxdev->tail++;
> +
> + spin_unlock_bh(&plxdev->ring_lock);
> + dmaengine_desc_callback_invoke(&cb, &res);
> + spin_lock_bh(&plxdev->ring_lock);
> }
>
> spin_unlock_bh(&plxdev->ring_lock);
> + spin_unlock_bh(&plxdev->complete_lock);
> }
>
> static void __plx_dma_stop(struct plx_dma_dev *plxdev)
> @@ -337,7 +359,15 @@ static enum dma_status plx_dma_tx_status(struct dma_chan *chan,
> if (ret == DMA_COMPLETE)
> return ret;
>
> - plx_dma_process_desc(plxdev);
> + /*
> + * If another context is already completing descriptors (possibly
> + * a callback on this CPU that is polling the channel), let it
> + * finish rather than waiting on it.
> + */
> + if (spin_trylock_bh(&plxdev->complete_lock)) {
> + plx_dma_process_desc_unlocked(plxdev);
> + spin_unlock_bh(&plxdev->complete_lock);
> + }
>
> return dma_cookie_status(chan, cookie, txstate);
> }
> @@ -516,6 +546,7 @@ static int plx_dma_create(struct pci_dev *pdev)
> if (!plxdev)
> return -ENOMEM;
>
> + spin_lock_init(&plxdev->complete_lock);
> spin_lock_init(&plxdev->ring_lock);
> tasklet_setup(&plxdev->desc_task, plx_dma_desc_task);
>
> --
> 2.47.3
>