[PATCH v2 2/3] dmaengine: sun4i: retain cyclic descriptors until callbacks drain

From: James Hilliard

Date: Sun Nov 15 2026 - 04:07:18 EST


A cyclic IRQ queues a virt-dma tasklet with a pointer to the active
descriptor. sun4i_dma_terminate_all() frees it without clearing the queued
cyclic callback or waiting for one which has already started. The tasklet
can subsequently access the freed descriptor. There is no
device_synchronize callback to let clients drain these callbacks before
releasing their own resources either.

Serialize both half-transfer and end-transfer IRQ handling with termination
under the virtual-channel lock. Recheck the physical-channel association
after acquiring that lock so an IRQ which looked up the channel before
termination cannot use its detached transfer state.

Use vchan_terminate_all() to retire descriptors until synchronization. This
clears any pending cyclic callback. Retain them until a new synchronize
callback has waited for the IRQ handler and drained the tasklet. Drain
callbacks before freeing channel resources as well. Termination itself
remains usable from atomic context and from a completion callback.

The shared helper retains all collected descriptors. An in-flight
non-cyclic callback still owns its descriptor on the tasklet's private
list and retires it when the callback returns.

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 | 41 +++++++++++++++++++++++++++--------------
1 file changed, 27 insertions(+), 14 deletions(-)

diff --git a/drivers/dma/sun4i-dma.c b/drivers/dma/sun4i-dma.c
index 225e4a53502b..b0c91dab62f0 100644
--- a/drivers/dma/sun4i-dma.c
+++ b/drivers/dma/sun4i-dma.c
@@ -293,10 +293,20 @@ static int convert_buswidth(enum dma_slave_buswidth addr_width)
return (addr_width >> 1);
}

+static void sun4i_dma_synchronize(struct dma_chan *chan)
+{
+ struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
+ struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
+
+ synchronize_irq(priv->irq);
+ vchan_synchronize(&vchan->vc);
+}
+
static void sun4i_dma_free_chan_resources(struct dma_chan *chan)
{
struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);

+ sun4i_dma_synchronize(chan);
vchan_free_chan_resources(&vchan->vc);
}

@@ -322,7 +332,7 @@ static struct sun4i_dma_pchan *find_and_use_pchan(struct sun4i_dma_dev *priv,
spin_lock_irqsave(&priv->lock, flags);
for_each_clear_bit_from(i, priv->pchans_used, max) {
pchan = &pchans[i];
- pchan->vchan = vchan;
+ WRITE_ONCE(pchan->vchan, vchan);
set_bit(i, priv->pchans_used);
break;
}
@@ -339,7 +349,7 @@ static void release_pchan(struct sun4i_dma_dev *priv,

spin_lock_irqsave(&priv->lock, flags);

- pchan->vchan = NULL;
+ WRITE_ONCE(pchan->vchan, NULL);
clear_bit(nr, priv->pchans_used);

spin_unlock_irqrestore(&priv->lock, flags);
@@ -970,13 +980,11 @@ static int sun4i_dma_terminate_all(struct dma_chan *chan)
{
struct sun4i_dma_dev *priv = to_sun4i_dma_dev(chan->device);
struct sun4i_dma_vchan *vchan = to_sun4i_dma_vchan(chan);
- struct sun4i_dma_pchan *pchan = vchan->pchan;
- LIST_HEAD(head);
+ struct sun4i_dma_pchan *pchan;
unsigned long flags;

spin_lock_irqsave(&vchan->vc.lock, flags);
- vchan_get_all_descriptors(&vchan->vc, &head);
- spin_unlock_irqrestore(&vchan->vc.lock, flags);
+ pchan = vchan->pchan;

/*
* Clearing the configuration register will halt the pchan. Interrupts
@@ -991,13 +999,14 @@ static int sun4i_dma_terminate_all(struct dma_chan *chan)
release_pchan(priv, pchan);
}

- spin_lock_irqsave(&vchan->vc.lock, flags);
/* Clear these so the vchan is usable again */
vchan->processing = NULL;
vchan->pchan = NULL;
- spin_unlock_irqrestore(&vchan->vc.lock, flags);
+ vchan->contract = NULL;

- vchan_dma_desc_free_list(&vchan->vc, &head);
+ /* A tasklet may already be using the active cyclic descriptor. */
+ vchan_terminate_all(&vchan->vc);
+ spin_unlock_irqrestore(&vchan->vc.lock, flags);

return 0;
}
@@ -1128,9 +1137,15 @@ static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)

for_each_set_bit(bit, &pendirq, 32) {
pchan = &pchans[bit >> 1];
- vchan = pchan->vchan;
+ vchan = READ_ONCE(pchan->vchan);
if (!vchan) /* a terminated channel may still interrupt */
continue;
+
+ spin_lock(&vchan->vc.lock);
+ if (vchan->pchan != pchan || !vchan->processing) {
+ spin_unlock(&vchan->vc.lock);
+ continue;
+ }
contract = vchan->contract;

/*
@@ -1138,8 +1153,6 @@ static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
* interrupt (odd bit)
*/
if (bit & 1) {
- spin_lock(&vchan->vc.lock);
-
/*
* Move the promise into the completed list now that
* we're done with it
@@ -1173,8 +1186,6 @@ static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
disableirqs |= BIT(bit);
release_pchan(priv, pchan);
}
-
- spin_unlock(&vchan->vc.lock);
} else {
/* Half done interrupt */
if (contract->is_cyclic)
@@ -1182,6 +1193,7 @@ static irqreturn_t sun4i_dma_interrupt(int irq, void *dev_id)
else
disableirqs |= BIT(bit);
}
+ spin_unlock(&vchan->vc.lock);
}

/* Disable the IRQs for events we handled */
@@ -1276,6 +1288,7 @@ static int sun4i_dma_probe(struct platform_device *pdev)
priv->slave.device_prep_dma_cyclic = sun4i_dma_prep_dma_cyclic;
priv->slave.device_config = sun4i_dma_config;
priv->slave.device_terminate_all = sun4i_dma_terminate_all;
+ priv->slave.device_synchronize = sun4i_dma_synchronize;
priv->slave.copy_align = 2;
priv->slave.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |

--
2.53.0