[PATCH v2 3/3] dmaengine: sun4i: retire interrupt state before channel reuse
From: James Hilliard
Date: Sun Nov 15 2026 - 04:07:08 EST
The IRQ handler samples pending status before looking up the physical
channel's virtual owner. Another CPU can terminate that owner and start
a transfer on a different virtual channel using the same physical channel.
The old pending bit then passes the new owner's association check and can
complete its transfer before the DMA has finished.
The handler also releases a completed transfer's physical channel before
masking and acknowledging its interrupts. A replacement transfer can start
in between, only to have its interrupts disabled or cleared by the old
handler's deferred cleanup.
Clear both pending interrupt sources when disabling a physical channel,
and read the status back before allowing the channel to be reused. Disable
the sources before releasing a completed transfer's channel. Re-read and
acknowledge each pending event under the owning virtual channel's lock,
before completing or rearming the transfer. This rejects stale status and
keeps the old handler from clearing a replacement transfer's events.
Fixes: b096c1377d1e ("dmaengine: sun4i: Add support for the DMA engine on sun[457]i SoCs")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/dma/sun4i-dma.c | 29 ++++++++++++++---------------
1 file changed, 14 insertions(+), 15 deletions(-)
diff --git a/drivers/dma/sun4i-dma.c b/drivers/dma/sun4i-dma.c
index b0c91dab62f0..6ecc45cadd91 100644
--- a/drivers/dma/sun4i-dma.c
+++ b/drivers/dma/sun4i-dma.c
@@ -400,6 +400,13 @@ static void set_pchan_interrupt(struct sun4i_dma_dev *priv,
writel_relaxed(reg, priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
+ /* Do not leave pending events for the channel's next owner. */
+ if (!half && !end) {
+ writel_relaxed(BIT(pchan_number * 2) | BIT(pchan_number * 2 + 1),
+ priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
+ readl(priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
+ }
+
spin_unlock_irqrestore(&priv->lock, flags);
}
@@ -1125,14 +1132,13 @@ static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
struct sun4i_dma_vchan *vchan;
struct sun4i_dma_contract *contract;
struct sun4i_dma_promise *promise;
- unsigned long pendirq, irqs, disableirqs;
+ unsigned long pendirq, irqs;
int bit, i, free_room, allow_mitigation = 1;
pendirq = readl_relaxed(priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
handle_pending:
- disableirqs = 0;
free_room = 0;
for_each_set_bit(bit, &pendirq, 32) {
@@ -1142,10 +1148,13 @@ static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
continue;
spin_lock(&vchan->vc.lock);
- if (vchan->pchan != pchan || !vchan->processing) {
+ /* The channel may have been reused since pendirq was sampled. */
+ irqs = readl_relaxed(priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
+ if (vchan->pchan != pchan || !vchan->processing || !(irqs & BIT(bit))) {
spin_unlock(&vchan->vc.lock);
continue;
}
+ writel_relaxed(BIT(bit), priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
contract = vchan->contract;
/*
@@ -1183,7 +1192,7 @@ static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
vchan->pchan = NULL;
free_room = 1;
- disableirqs |= BIT(bit);
+ set_pchan_interrupt(priv, pchan, 0, 0);
release_pchan(priv, pchan);
}
} else {
@@ -1191,21 +1200,11 @@ static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
if (contract->is_cyclic)
vchan_cyclic_callback(&contract->vd);
else
- disableirqs |= BIT(bit);
+ set_pchan_interrupt(priv, pchan, 0, 1);
}
spin_unlock(&vchan->vc.lock);
}
- /* Disable the IRQs for events we handled */
- spin_lock(&priv->lock);
- irqs = readl_relaxed(priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
- writel_relaxed(irqs & ~disableirqs,
- priv->base + SUN4I_DMA_IRQ_ENABLE_REG);
- spin_unlock(&priv->lock);
-
- /* Writing 1 to the pending field will clear the pending interrupt */
- writel_relaxed(pendirq, priv->base + SUN4I_DMA_IRQ_PENDING_STATUS_REG);
-
/*
* If a pchan was freed, we may be able to schedule something else,
* so have a look around
--
2.53.0