From: sashiko-bot@kernel.org
To: "Shan-Chun Hung" <shanchun1218@gmail.com>,
dmaengine@vger.kernel.org, devicetree@vger.kernel.org
Cc: robh@kernel.org, conor+dt@kernel.org, vkoul@kernel.org,
Frank.Li@kernel.org
Subject: Re: [PATCH v3 2/2] dmaengine: ma35d1-pdma: Add MA35D1 PDMA driver
Date: Thu, 19 Nov 2026 02:12:25 +0000 [thread overview]
Message-ID: <sashiko-outbox-222317@kernel.org> (raw)
In-Reply-To: <20261119015447.160470-3-shanchun1218@gmail.com>
Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] dmaengine: ma35d1-pdma: broken init and teardown order in
ma35d1_pdma_probe()
- [Medium] dmaengine: ma35d1-pdma: missing tasklet sync in
ma35d1_free_chan_resources()
- [Medium] dmaengine: ma35d1-pdma: status clear race in
ma35d1_pdma_interrupt()
--
commit fe61217d728ce53c5336f64af791df9cdaf7d2d2
Author: Shan-Chun Hung <shanchun1218@gmail.com>
dmaengine: ma35d1-pdma: Add MA35D1 PDMA driver
This commit adds a DMA engine driver for the Nuvoton MA35D1 PDMA
controller supporting up to 10 channels for memcpy, slave SG, and cyclic
transfers.
> diff --git a/drivers/dma/ma35d1-pdma.c b/drivers/dma/ma35d1-pdma.c
> new file mode 100644
> index 0000000000000..0d6a744043214
> --- /dev/null
> +++ b/drivers/dma/ma35d1-pdma.c
> @@ -0,0 +1,662 @@
[ ... ]
> +static void ma35d1_free_chan_resources(struct dma_chan *chan)
> +{
> + struct ma35d1_chan *ch = to_ma35d1_pdma_chan(chan);
> +
> + ma35d1_terminate_all(chan);
> + vchan_free_chan_resources(&ch->vc);
> + ch->request = 0;
> + memset(&ch->cfg, 0, sizeof(ch->cfg));
> +}
[Severity: Medium]
Can vchan_free_chan_resources() free an active descriptor while
ch->vc.task is still running?
If a client releases a channel without first calling
dmaengine_terminate_sync(), hardware DMA remains active until
ma35d1_terminate_all() runs here. An interrupt firing just before
ma35d1_terminate_all() disables the channel schedules ch->vc.task:
CPU0 CPU1
ma35d1_free_chan_resources()
<hardirq>
ma35d1_pdma_interrupt()
vchan_cyclic_callback(&ch->desc->vd)
tasklet_schedule(&vc->task)
<softirq>
vchan_complete()
spin_lock_irqsave(&vc->lock)
vd = vc->cyclic;
spin_unlock_irqrestore(&vc->lock)
ma35d1_terminate_all()
vchan_terminate_vdesc(&ch->desc->vd)
vchan_free_chan_resources(&ch->vc)
ma35d1_pdma_desc_free(vd)
kfree(d)
dmaengine_desc_get_callback_invoke(
&vd->tx, &vd->tx_result)
<-- use-after-free
Calling vchan_synchronize(&ch->vc) between ma35d1_terminate_all() and
vchan_free_chan_resources() waits for ch->vc.task to finish before freeing
the descriptors.
[ ... ]
> +static irqreturn_t ma35d1_pdma_interrupt(int irq, void *devid)
> +{
> + struct ma35d1_pdmadev *pdmadev = devid;
> + unsigned long pending, done, abort;
> + struct ma35d1_chan *ch;
> + unsigned long flags;
> + int i;
> +
> + done = readl(pdmadev->channels[0].base + PDMA_TDSTS);
> + abort = readl(pdmadev->channels[0].base + PDMA_ABTSTS);
> + pending = done | abort;
> + if (!pending)
> + return IRQ_NONE;
> +
> + writel(done, pdmadev->channels[0].base + PDMA_TDSTS);
> + writel(abort, pdmadev->channels[0].base + PDMA_ABTSTS);
> +
> + for_each_set_bit(i, &pending, PDMA_MAX_CHANS) {
> + ch = &pdmadev->channels[i];
> +
> + spin_lock_irqsave(&ch->vc.lock, flags);
> +
> + if (ch->desc) {
> + if (test_bit(i, &abort) && ch->desc->cyclic) {
> + vchan_terminate_vdesc(&ch->desc->vd);
> + ch->desc = NULL;
> + } else if (test_bit(i, &abort)) {
> + ch->desc->vd.tx_result.result = DMA_TRANS_ABORTED;
> + ma35d1_pdma_complete(ch);
> + } else if (ch->desc->cyclic) {
> + vchan_cyclic_callback(&ch->desc->vd);
> + } else {
> + ma35d1_pdma_complete(ch);
> + }
> + }
> +
> + spin_unlock_irqrestore(&ch->vc.lock, flags);
> + }
> +
> + return IRQ_HANDLED;
> +}
[Severity: Medium]
Can reading and clearing PDMA_TDSTS and PDMA_ABTSTS before acquiring
ch->vc.lock apply a stale completion or abort status to a newly started
transfer?
CPU0 CPU1
<hardirq>
ma35d1_pdma_interrupt()
done = readl(... + PDMA_TDSTS)
writel(done, ... + PDMA_TDSTS)
ma35d1_terminate_all()
spin_lock_irqsave(&ch->vc.lock)
writel(BIT(ch->id), ... + PDMA_TDSTS)
ch->desc = NULL;
spin_unlock_irqrestore(&ch->vc.lock)
ma35d1_issue_pending()
spin_lock_irqsave(&ch->vc.lock)
ma35d1_pdma_start_desc()
ch->desc = new_desc;
ma35d1_pdma_start(ch)
spin_unlock_irqrestore(&ch->vc.lock)
spin_lock_irqsave(&ch->vc.lock)
if (ch->desc)
ma35d1_pdma_complete(ch) <-- completes new_desc while DMA is active
spin_unlock_irqrestore(&ch->vc.lock)
Prematurely completing new_desc schedules its descriptor free callback and
starts the next descriptor on the channel while hardware DMA is still
using new_desc->sg. Checking and clearing channel i's bits in PDMA_TDSTS
and PDMA_ABTSTS under ch->vc.lock would prevent this race.
> +
> +static void ma35d1_pdma_teardown(void *data)
> +{
> + struct ma35d1_pdmadev *pdmadev = data;
> + void __iomem *base = pdmadev->channels[0].base;
> + int i;
> +
> + writel(0, base + PDMA_CHCTL);
> + writel(0, base + PDMA_INTEN);
> +
> + for (i = 0; i < PDMA_MAX_CHANS; i++)
> + tasklet_kill(&pdmadev->channels[i].vc.task);
> +}
[ ... ]
> + ret = devm_add_action_or_reset(dev, ma35d1_pdma_teardown, pdmadev);
> + if (ret)
> + return ret;
> +
> + ret = dmaenginem_async_device_register(&pdmadev->ddev);
> + if (ret)
> + return dev_err_probe(dev, ret, "Failed to register DMA device\n");
> +
> + ret = devm_request_irq(dev, pdmadev->irq, ma35d1_pdma_interrupt, 0, pdev->name, pdmadev);
> + if (ret)
> + return dev_err_probe(dev, ret, "Failed to request IRQ\n");
> +
> + ret = devm_of_dma_controller_register(dev, node, ma35d1_of_xlate, pdmadev);
> + if (ret)
> + return dev_err_probe(dev, ret, "of_dma_controller_register failed\n");
[Severity: Medium]
Does registering ma35d1_pdma_teardown() and pdmadev->ddev before
devm_request_irq() invert the probe and devres teardown order?
During probe, dmaenginem_async_device_register() exposes the DMA channels
before the IRQ handler is installed. Because devres runs cleanup actions
in reverse order, driver unbind or a failure in
devm_of_dma_controller_register() frees the IRQ and unregisters
pdmadev->ddev while hardware channels (PDMA_CHCTL) and interrupts
(PDMA_INTEN) are still enabled and before ma35d1_pdma_teardown() calls
tasklet_kill() on channels[i].vc.task.
Requesting the IRQ before registering ma35d1_pdma_teardown(), and
registering ma35d1_pdma_teardown() after
dmaenginem_async_device_register(), ensures hardware interrupts and
tasklets are stopped before the DMA device is unregistered and the IRQ is
freed.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261119015447.160470-1-shanchun1218@gmail.com?part=2
prev parent reply other threads:[~2026-11-19 2:12 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-19 1:54 [PATCH v3 0/2] dmaengine: ma35d1-pdma: Add MA35D1 PDMA driver Shan-Chun Hung
2026-11-19 1:54 ` [PATCH v3 1/2] dt-bindings: dma: nuvoton,ma35d1-pdma: Document MA35D1 PDMA controller Shan-Chun Hung
2026-11-19 1:54 ` [PATCH v3 2/2] dmaengine: ma35d1-pdma: Add MA35D1 PDMA driver Shan-Chun Hung
2026-11-19 2:12 ` sashiko-bot [this message]
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=sashiko-outbox-222317@kernel.org \
--to=sashiko-bot@kernel.org \
--cc=Frank.Li@kernel.org \
--cc=conor+dt@kernel.org \
--cc=devicetree@vger.kernel.org \
--cc=dmaengine@vger.kernel.org \
--cc=robh@kernel.org \
--cc=sashiko-reviews@lists.linux.dev \
--cc=shanchun1218@gmail.com \
--cc=vkoul@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