Skip to contentSkip to conversation

dmaengine: milbeaut-hdmac: Fix deadlock with the channel IRQ handler

PATCH In review Ginger Li 3 participants 3 messages Sep 23 – Nov 20

Subsystems (3)
lore
Share & tools
lore

Ginger Li

author
#0 lore raw

PATCH 1/1 dmaengine: milbeaut-hdmac: Fix deadlock with the channel IRQ handler

milbeaut_hdmac_interrupt() is a hardirq handler and takes mc->vc.lock with a plain spin_lock() for the whole time it runs. interrupts are disabled locally in that context, so the handler itself is fine.

milbeaut_hdmac_chan_config(), milbeaut_hdmac_chan_pause() and milbeaut_hdmac_chan_resume() are called from process context and take the same lock with a plain spin_lock() as well, so interrupts stay enabled while the lock is held.

If a channel interrupt is delivered on the CPU that is inside one of those critical sections, milbeaut_hdmac_interrupt() spins on a lock that the interrupted code is holding and can never release, and the CPU hangs.

Take mc->vc.lock with spin_lock_irqsave() in those three functions, as milbeaut_hdmac_issue_pending() and the other process context users of this lock already do.

Fixes 6c3214e698e4 ("dmaengine: milbeaut-hdmac: Add HDMAC driver for Milbeaut platforms")
---
 drivers/dma/milbeaut-hdmac.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/dma/milbeaut-hdmac.c +9 -6
@@ -214,10 +214,11 @@ milbeaut_hdmac_chan_config(struct dma_chan *chan, stru
 {
 	struct virt_dma_chan *vc = to_virt_chan(chan);
 	struct milbeaut_hdmac_chan *mc = to_milbeaut_hdmac_chan(vc);
+	unsigned long flags;
 
-	spin_lock(&mc->vc.lock);
+	spin_lock_irqsave(&mc->vc.lock, flags);
 	mc->cfg = *cfg;
-	spin_unlock(&mc->vc.lock);
+	spin_unlock_irqrestore(&mc->vc.lock, flags);
 
 	return 0;
 }
@@ -226,13 +227,14 @@ static int milbeaut_hdmac_chan_pause(struct dma_chan *
 {
 	struct virt_dma_chan *vc = to_virt_chan(chan);
 	struct milbeaut_hdmac_chan *mc = to_milbeaut_hdmac_chan(vc);
+	unsigned long flags;
 	u32 val;
 
-	spin_lock(&mc->vc.lock);
+	spin_lock_irqsave(&mc->vc.lock, flags);
 	val = readl_relaxed(mc->reg_ch_base + MLB_HDMAC_DMACA);
 	val |= MLB_HDMAC_PB;
 	writel_relaxed(val, mc->reg_ch_base + MLB_HDMAC_DMACA);
-	spin_unlock(&mc->vc.lock);
+	spin_unlock_irqrestore(&mc->vc.lock, flags);
 
 	return 0;
 }
@@ -241,13 +243,14 @@ static int milbeaut_hdmac_chan_resume(struct dma_chan 
 {
 	struct virt_dma_chan *vc = to_virt_chan(chan);
 	struct milbeaut_hdmac_chan *mc = to_milbeaut_hdmac_chan(vc);
+	unsigned long flags;
 	u32 val;
 
-	spin_lock(&mc->vc.lock);
+	spin_lock_irqsave(&mc->vc.lock, flags);
 	val = readl_relaxed(mc->reg_ch_base + MLB_HDMAC_DMACA);
 	val &= ~MLB_HDMAC_PB;
 	writel_relaxed(val, mc->reg_ch_base + MLB_HDMAC_DMACA);
-	spin_unlock(&mc->vc.lock);
+	spin_unlock_irqrestore(&mc->vc.lock, flags);
 
 	return 0;
 }
Signature · 1 line
2.43.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 15 lines · Ginger Li

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

milbeaut_hdmac_interrupt() is a hardirq handler and takes mc->vc.lock with a plain spin_lock() for the whole time it runs. interrupts are disabled locally in that context, so the handler itself is fine.

milbeaut_hdmac_chan_config(), milbeaut_hdmac_chan_pause() and milbeaut_hdmac_chan_resume() are called from process context and take the same lock with a plain spin_lock() as well, so interrupts stay enabled while the lock is held.

If a channel interrupt is delivered on the CPU that is inside one of those critical sections, milbeaut_hdmac_interrupt() spins on a lock that the interrupted code is holding and can never release, and the CPU hangs.

Quoted 7 lines

Take mc->vc.lock with spin_lock_irqsave() in those three functions, as milbeaut_hdmac_issue_pending() and the other process context users of this lock already do.

Fixes 6c3214e698e4 ("dmaengine: milbeaut-hdmac: Add HDMAC driver for Milbeaut platforms")

---

Quoted 57 lines
 drivers/dma/milbeaut-hdmac.c | 15 +++++++++------
 1 file changed, 9 insertions(+), 6 deletions(-)
drivers/dma/milbeaut-hdmac.c +9 -7
@@ -214,10 +214,11 @@ milbeaut_hdmac_chan_config(struct dma_chan *chan, stru
 {
        struct virt_dma_chan *vc = to_virt_chan(chan);
        struct milbeaut_hdmac_chan *mc = to_milbeaut_hdmac_chan(vc);
+       unsigned long flags;
 
-       spin_lock(&mc->vc.lock);
+       spin_lock_irqsave(&mc->vc.lock, flags);
        mc->cfg = *cfg;
-       spin_unlock(&mc->vc.lock);
+       spin_unlock_irqrestore(&mc->vc.lock, flags);
 
        return 0;
 }
@@ -226,13 +227,14 @@ static int milbeaut_hdmac_chan_pause(struct dma_chan *
 {
        struct virt_dma_chan *vc = to_virt_chan(chan);
        struct milbeaut_hdmac_chan *mc = to_milbeaut_hdmac_chan(vc);
+       unsigned long flags;
        u32 val;
 
-       spin_lock(&mc->vc.lock);
+       spin_lock_irqsave(&mc->vc.lock, flags);
        val = readl_relaxed(mc->reg_ch_base + MLB_HDMAC_DMACA);
        val |= MLB_HDMAC_PB;
        writel_relaxed(val, mc->reg_ch_base + MLB_HDMAC_DMACA);
-       spin_unlock(&mc->vc.lock);
+       spin_unlock_irqrestore(&mc->vc.lock, flags);
 
        return 0;
 }
@@ -241,13 +243,14 @@ static int milbeaut_hdmac_chan_resume(struct dma_chan
 {
        struct virt_dma_chan *vc = to_virt_chan(chan);
        struct milbeaut_hdmac_chan *mc = to_milbeaut_hdmac_chan(vc);
+       unsigned long flags;
        u32 val;
 
-       spin_lock(&mc->vc.lock);
+       spin_lock_irqsave(&mc->vc.lock, flags);
        val = readl_relaxed(mc->reg_ch_base + MLB_HDMAC_DMACA);
        val &= ~MLB_HDMAC_PB;
        writel_relaxed(val, mc->reg_ch_base + MLB_HDMAC_DMACA);
-       spin_unlock(&mc->vc.lock);
+       spin_unlock_irqrestore(&mc->vc.lock, flags);
 
        return 0;
 }
--

2.43.0

maintainer
#2 lore raw
In reply to Ginger Li
Quoted 9 lines · Ginger Li

milbeaut_hdmac_interrupt() is a hardirq handler and takes mc->vc.lock with a plain spin_lock() for the whole time it runs. interrupts are disabled locally in that context, so the handler itself is fine.

milbeaut_hdmac_chan_config(), milbeaut_hdmac_chan_pause() and milbeaut_hdmac_chan_resume() are called from process context and take the same lock with a plain spin_lock() as well, so interrupts stay enabled while the lock is held.

Quoted 7 lines

If a channel interrupt is delivered on the CPU that is inside one of those critical sections, milbeaut_hdmac_interrupt() spins on a lock that the interrupted code is holding and can never release, and the CPU hangs.

Take mc->vc.lock with spin_lock_irqsave() in those three functions, as milbeaut_hdmac_issue_pending() and the other process context users of this lock already do.

Have you seen this deadlock in reallife, any logs?

Signature · 1 line
~Vinod