DMA Engine development
 help / color / mirror / Atom feed
* [PATCH] dmaengine: axi-dmac: change IRQ to threaded mode
@ 2026-11-17  6:01 Sergey Matsievskiy
  2026-11-17  6:45 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Sergey Matsievskiy @ 2026-11-17  6:01 UTC (permalink / raw)
  To: dmaengine; +Cc: lars, vkoul, Frank.Li, linux-kernel, Sergey Matsievskiy

Threaded IRQ handlers run in a less critical context compared to normal
IRQs, so they can perform more complex and time-consuming operations
without causing significant delays in other parts of the kernel.
Additionally, regmap-irqchip requires child devices to handle IRQs in
threaded mode.

In this patch change IRQ to threaded mode for compatibility with
regmap-irqchip.
Switch spin_lock() to spin_lock_irqsave() in IRQ handler as it may be
interrupted in threaded mode. spin_lock_irqsave() prevents recursive
spinlock deadlock.

Signed-off-by: Sergey Matsievskiy <matsievskiysv@gmail.com>
---
 drivers/dma/dma-axi-dmac.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/drivers/dma/dma-axi-dmac.c b/drivers/dma/dma-axi-dmac.c
index 98aaccd26a26..4a340e382e44 100644
--- a/drivers/dma/dma-axi-dmac.c
+++ b/drivers/dma/dma-axi-dmac.c
@@ -583,6 +583,7 @@ static irqreturn_t axi_dmac_interrupt_handler(int irq, void *devid)
 {
 	struct axi_dmac *dmac = devid;
 	unsigned int pending;
+	unsigned long flags;
 	bool start_next = false;
 
 	pending = axi_dmac_read(dmac, AXI_DMAC_REG_IRQ_PENDING);
@@ -591,7 +592,7 @@ static irqreturn_t axi_dmac_interrupt_handler(int irq, void *devid)
 
 	axi_dmac_write(dmac, AXI_DMAC_REG_IRQ_PENDING, pending);
 
-	spin_lock(&dmac->chan.vchan.lock);
+	spin_lock_irqsave(&dmac->chan.vchan.lock, flags);
 	/* One or more transfers have finished */
 	if (pending & AXI_DMAC_IRQ_EOT) {
 		unsigned int completed;
@@ -602,7 +603,7 @@ static irqreturn_t axi_dmac_interrupt_handler(int irq, void *devid)
 	/* Space has become available in the descriptor queue */
 	if ((pending & AXI_DMAC_IRQ_SOT) || start_next)
 		axi_dmac_start_transfer(&dmac->chan);
-	spin_unlock(&dmac->chan.vchan.lock);
+	spin_unlock_irqrestore(&dmac->chan.vchan.lock, flags);
 
 	return IRQ_HANDLED;
 }
@@ -1324,8 +1325,10 @@ static int axi_dmac_probe(struct platform_device *pdev)
 	if (ret)
 		return ret;
 
-	ret = devm_request_irq(&pdev->dev, dmac->irq, axi_dmac_interrupt_handler,
-			       IRQF_SHARED, dev_name(&pdev->dev), dmac);
+	ret = devm_request_threaded_irq(&pdev->dev, dmac->irq, NULL,
+					axi_dmac_interrupt_handler,
+					IRQF_SHARED | IRQF_ONESHOT,
+					dev_name(&pdev->dev), dmac);
 	if (ret)
 		return ret;
 
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] dmaengine: axi-dmac: change IRQ to threaded mode
  2026-11-17  6:01 [PATCH] dmaengine: axi-dmac: change IRQ to threaded mode Sergey Matsievskiy
@ 2026-11-17  6:45 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-17  6:45 UTC (permalink / raw)
  To: Sergey Matsievskiy, dmaengine; +Cc: vkoul, Frank.Li

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] dmaengine: axi-dmac: threaded IRQ race with
  axi_dmac_synchronize()
It also noticed 1 related pre-existing issue this patch does not
introduce:
- [High] dmaengine: dma-axi-dmac: cyclic desc use-after-free in
  axi_dmac_terminate_all() [1]
--

commit fcedf11ce19d4d148f53f6d8bd9c6a0e04ee8801
Author: Sergey Matsievskiy <matsievskiysv@gmail.com>

dmaengine: axi-dmac: change IRQ to threaded mode

This commit converts axi_dmac_interrupt_handler() to a threaded interrupt
handler using devm_request_threaded_irq() and switches vchan.lock acquisition
in the handler to spin_lock_irqsave().

> diff --git a/drivers/dma/dma-axi-dmac.c b/drivers/dma/dma-axi-dmac.c
> index dbc31b185c4d4..c4b6f6ae6be8c 100644
> --- a/drivers/dma/dma-axi-dmac.c
> +++ b/drivers/dma/dma-axi-dmac.c
> @@ -582,6 +582,7 @@ static irqreturn_t axi_dmac_interrupt_handler(int irq, void *devid)
>  {
>  	struct axi_dmac *dmac = devid;
>  	unsigned int pending;
> +	unsigned long flags;
>  	bool start_next = false;
>  
>  	pending = axi_dmac_read(dmac, AXI_DMAC_REG_IRQ_PENDING);
> @@ -590,7 +591,7 @@ static irqreturn_t axi_dmac_interrupt_handler(int irq, void *devid)
>  
>  	axi_dmac_write(dmac, AXI_DMAC_REG_IRQ_PENDING, pending);
>  
> -	spin_lock(&dmac->chan.vchan.lock);
> +	spin_lock_irqsave(&dmac->chan.vchan.lock, flags);
>  	/* One or more transfers have finished */
>  	if (pending & AXI_DMAC_IRQ_EOT) {
>  		unsigned int completed;

[Severity: High]
This problem wasn't introduced by this patch, but can axi_dmac_terminate_all()
cause a use-after-free on the active cyclic descriptor [1] in
vchan_complete()?

CPU0                              CPU1
axi_dmac_interrupt_handler()
  axi_dmac_transfer_done()
    vchan_cyclic_callback()
      vc->cyclic = vd;
      tasklet_schedule(&vc->task)
                                  axi_dmac_terminate_all()
                                    list_splice_tail_init(..., &head)
                                    vchan_dma_desc_free_list(..., &head)
                                      axi_dmac_desc_free()
vchan_complete()
  vd = vc->cyclic;
  dmaengine_desc_get_callback(&vd->tx, &cb)  <-- use-after-free

axi_dmac_terminate_all() splices chan->active_descs into its free list and
frees the descriptors without calling vchan_terminate_vdesc(), leaving
vc->cyclic pointing to the freed axi_dmac_desc when the scheduled tasklet
runs.

> @@ -601,7 +602,7 @@ static irqreturn_t axi_dmac_interrupt_handler(int irq, void *devid)
>  	/* Space has become available in the descriptor queue */
>  	if ((pending & AXI_DMAC_IRQ_SOT) || start_next)
>  		axi_dmac_start_transfer(&dmac->chan);
> -	spin_unlock(&dmac->chan.vchan.lock);
> +	spin_unlock_irqrestore(&dmac->chan.vchan.lock, flags);
>  
>  	return IRQ_HANDLED;
>  }
> @@ -1317,8 +1318,10 @@ static int axi_dmac_probe(struct platform_device *pdev)
>  	if (ret)
>  		return ret;
>  
> -	ret = devm_request_irq(&pdev->dev, dmac->irq, axi_dmac_interrupt_handler,
> -			       IRQF_SHARED, dev_name(&pdev->dev), dmac);
> +	ret = devm_request_threaded_irq(&pdev->dev, dmac->irq, NULL,
> +					axi_dmac_interrupt_handler,
> +					IRQF_SHARED | IRQF_ONESHOT,
> +					dev_name(&pdev->dev), dmac);

[Severity: High]
Does moving axi_dmac_interrupt_handler() to a thread without updating
axi_dmac_synchronize() and axi_dmac_terminate_all() introduce a race with
channel termination and teardown?

axi_dmac_synchronize() only calls vchan_synchronize() (which kills the vchan
tasklet) and does not call synchronize_irq(dmac->irq), while
axi_dmac_interrupt_handler() reads and clears AXI_DMAC_REG_IRQ_PENDING before
acquiring dmac->chan.vchan.lock:

CPU0 (IRQ thread)                 CPU1
axi_dmac_interrupt_handler()
  pending = axi_dmac_read(...)
  axi_dmac_write(..., pending)
                                  dmaengine_terminate_sync()
                                    axi_dmac_terminate_all()
                                    axi_dmac_synchronize()
                                      vchan_synchronize()
                                  axi_dmac_issue_pending()
                                    axi_dmac_start_transfer()
  spin_lock_irqsave(&vchan.lock)
  axi_dmac_transfer_done()
    list_del(&active->vdesc.node)
    vchan_cookie_complete()       <-- completes and frees new transfer

When an interrupt fires just before dmaengine_terminate_sync(), the woken IRQ
thread can run after a new transfer is issued and apply the stale
AXI_DMAC_IRQ_EOT to the new descriptor on chan->active_descs.  In chan->hw_sg
mode, axi_dmac_transfer_done() unconditionally pops and completes the head of
chan->active_descs, freeing the descriptor and its chan->pool SG entries via
axi_dmac_desc_free() while DMA hardware is still using them.

Similarly, during dma_release_channel(), an in-flight IRQ thread can run after
vchan_synchronize() and schedule the vchan tasklet while
axi_dmac_free_chan_resources() is destroying chan->pool.

[1] https://sashiko.dev/#/bug/linux-b5d0e7ef-3ad0-4c97-9c79-1694077caf95

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117060124.27914-1-matsievskiysv@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-17  6:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  6:01 [PATCH] dmaengine: axi-dmac: change IRQ to threaded mode Sergey Matsievskiy
2026-11-17  6:45 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox