Skip to contentSkip to conversation

dmaengine: axi-dmac: change IRQ to threaded mode

PATCH New Sergey Matsievskiy 2 participants 2 messages Nov 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dmaengine: axi-dmac: change IRQ to threaded mode

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.

---
 drivers/dma/dma-axi-dmac.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
drivers/dma/dma-axi-dmac.c +7 -4
index 98aaccd26a26..4a340e382e44 100644
@@ -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;
 
Signature · 1 line
2.47.3
#1 lore raw
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…

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 <[email protected]>

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().

Quoted 13 lines
drivers/dma/dma-axi-dmac.c +1 -0
index dbc31b185c4d4..c4b6f6ae6be8c 100644
@@ -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);

drivers/dma/dma-axi-dmac.c static irqreturn_t axi_dmac_interrupt_handler(int irq, void *devid) · line 597

Quoted 8 lines
drivers/dma/dma-axi-dmac.c +1 -1
@@ -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.

Quoted 10 lines
drivers/dma/dma-axi-dmac.c +1 -1
@@ -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;
 }

drivers/dma/dma-axi-dmac.c static int axi_dmac_probe(struct platform_device *pdev) · line 1324

Quoted 9 lines
drivers/dma/dma-axi-dmac.c +4 -2
@@ -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

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1