From: James Hilliard <james.hilliard1@gmail.com>
To: "Vinod Koul" <vkoul@kernel.org>, "Frank Li" <Frank.Li@kernel.org>,
"Chen-Yu Tsai" <wens@kernel.org>,
"Jernej Skrabec" <jernej.skrabec@gmail.com>,
"Samuel Holland" <samuel@sholland.org>,
"Hans de Goede" <hansg@kernel.org>,
"Emilio López" <emilio@elopez.com.ar>,
"Maxime Ripard" <mripard@kernel.org>
Cc: dmaengine@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
linux-sunxi@lists.linux.dev, linux-kernel@vger.kernel.org,
James Hilliard <james.hilliard1@gmail.com>
Subject: [PATCH v2 2/3] dmaengine: sun4i: retain cyclic descriptors until callbacks drain
Date: Sun, 15 Nov 2026 02:06:35 -0700 [thread overview]
Message-ID: <20261115-submit-sun4i-dma-callback-lifetime-v1-v2-2-fb7d258702f8@gmail.com> (raw)
In-Reply-To: <20261115-submit-sun4i-dma-callback-lifetime-v1-v2-0-fb7d258702f8@gmail.com>
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@gmail.com>
---
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
next prev parent reply other threads:[~2026-11-15 9:06 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-15 9:06 [PATCH v2 0/3] " James Hilliard
2026-11-15 9:06 ` [PATCH v2 1/3] dmaengine: virt-dma: share deferred descriptor retirement James Hilliard
2026-11-15 9:06 ` James Hilliard [this message]
2026-11-15 9:06 ` [PATCH v2 3/3] dmaengine: sun4i: retire interrupt state before channel reuse James Hilliard
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261115-submit-sun4i-dma-callback-lifetime-v1-v2-2-fb7d258702f8@gmail.com \
--to=james.hilliard1@gmail.com \
--cc=Frank.Li@kernel.org \
--cc=dmaengine@vger.kernel.org \
--cc=emilio@elopez.com.ar \
--cc=hansg@kernel.org \
--cc=jernej.skrabec@gmail.com \
--cc=linux-arm-kernel@lists.infradead.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-sunxi@lists.linux.dev \
--cc=mripard@kernel.org \
--cc=samuel@sholland.org \
--cc=vkoul@kernel.org \
--cc=wens@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®