Skip to contentSkip to conversation

dmaengine: mmp_pdma: Check virtual channel before scheduling tasklet

PATCH In review Ruoyu Wang 2 participants 2 messages Nov 29 – Dec 1 dmaengine

lore
Share & tools
lore

Ruoyu Wang

author
#0 lore raw

PATCH v2 1/1 dmaengine: mmp_pdma: Check virtual channel before scheduling tasklet

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")

---
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/[email protected]/

 drivers/dma/mmp_pdma.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/dma/mmp_pdma.c +12 -4
index 6045d6d1c..38c187299 100644
@@ -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

Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#1 lore raw
Quoted 14 lines · Ruoyu Wang

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.

Quoted 6 lines

This issue was found by a static analysis checker and confirmed by manual source review.

Fixes c8acd6aa6bed ("dmaengine: mmp-pdma support")

---

Quoted 65 lines

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/[email protected]/

 drivers/dma/mmp_pdma.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
drivers/dma/mmp_pdma.c +12 -4
index 6045d6d1c..38c187299 100644
@@ -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