Re: [PATCH v2] dmaengine: mmp_pdma: Check virtual channel before scheduling tasklet
From: Frank Li
Date: Tue Dec 01 2026 - 17:13:42 EST
On Sun, Nov 29, 2026 at 11:28:17PM +0800, Ruoyu Wang wrote:
> mmp_pdma_chan_handler() clears a physical-channel interrupt and then
> unconditionally schedules phy->vchan->tasklet. The physical channel can
> be detached from its virtual channel when the channel is terminated or
> when no pending work remains, so a late or shared interrupt can reach the
> handler with phy->vchan already NULL. If the physical channel has been
> reassigned in the meantime, the interrupt can also schedule the tasklet
> of the new virtual channel.
>
> Handle the interrupt and read phy->vchan under phy_lock, which protects
> the attach/detach stores, and skip tasklet scheduling when there is no
> virtual channel. Call clear_chan_irq() in mmp_pdma_free_phy() before
> detaching, so that a pending interrupt is not handled for the next
> virtual channel.
>
> This issue was found by a static analysis checker and confirmed by
> manual source review.
>
> Fixes: c8acd6aa6bed ("dmaengine: mmp-pdma support")
> Signed-off-by: Ruoyu Wang <ruoyuw560@xxxxxxxxx>
> ---
Reviewed-by: Frank Li <Frank.Li@xxxxxxx>
> Changes in v2:
> - Take phy_lock in the interrupt handler and call clear_chan_irq() before
> detaching the physical channel, to address the phy->vchan race pointed
> out by Frank Li.
>
> v1: https://lore.kernel.org/r/20260707150356.2257833-1-ruoyuw560@xxxxxxxxx/
>
> drivers/dma/mmp_pdma.c | 16 ++++++++++++----
> 1 file changed, 12 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/dma/mmp_pdma.c b/drivers/dma/mmp_pdma.c
> index 6045d6d1c..38c187299 100644
> --- a/drivers/dma/mmp_pdma.c
> +++ b/drivers/dma/mmp_pdma.c
> @@ -134,6 +134,7 @@ struct mmp_pdma_phy {
> int idx;
> void __iomem *base;
> struct mmp_pdma_chan *vchan;
> + struct mmp_pdma_device *pdev;
> };
>
> /**
> @@ -369,12 +370,17 @@ static int clear_chan_irq(struct mmp_pdma_phy *phy)
> static irqreturn_t mmp_pdma_chan_handler(int irq, void *dev_id)
> {
> struct mmp_pdma_phy *phy = dev_id;
> + irqreturn_t ret = IRQ_NONE;
>
> - if (clear_chan_irq(phy) != 0)
> - return IRQ_NONE;
> + spin_lock(&phy->pdev->phy_lock);
> + if (clear_chan_irq(phy) == 0) {
> + if (phy->vchan)
> + tasklet_schedule(&phy->vchan->tasklet);
> + ret = IRQ_HANDLED;
> + }
> + spin_unlock(&phy->pdev->phy_lock);
>
> - tasklet_schedule(&phy->vchan->tasklet);
> - return IRQ_HANDLED;
> + return ret;
> }
>
> static irqreturn_t mmp_pdma_int_handler(int irq, void *dev_id)
> @@ -452,6 +458,7 @@ static void mmp_pdma_free_phy(struct mmp_pdma_chan *pchan)
> writel(0, pchan->phy->base + reg);
>
> spin_lock_irqsave(&pdev->phy_lock, flags);
> + clear_chan_irq(pchan->phy);
> pchan->phy->vchan = NULL;
> pchan->phy = NULL;
> spin_unlock_irqrestore(&pdev->phy_lock, flags);
> @@ -1155,6 +1162,7 @@ static int mmp_pdma_chan_init(struct mmp_pdma_device *pdev, int idx, int irq)
>
> phy->idx = idx;
> phy->base = pdev->base;
> + phy->pdev = pdev;
>
> if (irq) {
> ret = devm_request_irq(pdev->dev, irq, mmp_pdma_chan_handler,
>
> base-commit: 4f069ab1ec243e6b60dc438772905855c60774e8
> --
> 2.43.0
>