Skip to contentSkip to conversation

dmaengine: stm32-dmamux: don't resume runtime PM under a spinlock

PATCH Superseded Hyunjung Ko 3 participants 5 messages Nov 18

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

PATCH 1/1 dmaengine: stm32-dmamux: don't resume runtime PM under a spinlock

stm32_dmamux_route_allocate() calls pm_runtime_resume_and_get() while holding dmamux->lock with interrupts disabled. The runtime-resume callback stm32_dmamux_runtime_resume() calls clk_prepare_enable(), which can sleep, and the device is not marked pm_runtime_irq_safe(), so this can sleep in atomic context.

The lock does not protect anything here: the dma_inuse bitmap is already updated under the lock earlier in the function, and this second critical section only wraps the pm_runtime call. Drop the lock around it.

Fixes 4f3ceca254e0 ("dmaengine: stm32-dmamux: Add PM Runtime support")
---
 drivers/dma/stm32/stm32-dmamux.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
drivers/dma/stm32/stm32-dmamux.c +1 -5
index 57318715d9f3..890328b3d302 100644
@@ -139,13 +139,9 @@ static void *stm32_dmamux_route_allocate(struct of_phandle_args *dma_spec,
 	}
 
 	/* Set dma request */
-	spin_lock_irqsave(&dmamux->lock, flags);
 	ret = pm_runtime_resume_and_get(&pdev->dev);
-	if (ret < 0) {
-		spin_unlock_irqrestore(&dmamux->lock, flags);
+	if (ret < 0)
 		goto err_put_dma_spec_np;
-	}
-	spin_unlock_irqrestore(&dmamux->lock, flags);
 
 	mux->request = dma_spec->args[0];
 
Signature · 1 line
2.43.0
maintainer
#1 lore raw

Hi,

Quoted 5 lines · Hyunjung Ko

stm32_dmamux_route_allocate() calls pm_runtime_resume_and_get() while holding dmamux->lock with interrupts disabled. The runtime-resume callback stm32_dmamux_runtime_resume() calls clk_prepare_enable(), which can sleep, and the device is not marked pm_runtime_irq_safe(), so this can sleep in atomic context.

Indeed.

Quoted 4 lines

The lock does not protect anything here: the dma_inuse bitmap is already updated under the lock earlier in the function, and this second critical section only wraps the pm_runtime call. Drop the lock around it.

The lock is misplaced and completely useless here. Your patch is correct but incomplete.
The lock should instead protect the CCR register write, as in stm32_dmamux_free(), which also needs to be fixed.

Quoted 17 lines
Fixes 4f3ceca254e0 ("dmaengine: stm32-dmamux: Add PM Runtime support")
---
  drivers/dma/stm32/stm32-dmamux.c | 6 +-----
  1 file changed, 1 insertion(+), 5 deletions(-)
drivers/dma/stm32/stm32-dmamux.c +0 -1
index 57318715d9f3..890328b3d302 100644
@@ -139,13 +139,9 @@ static void *stm32_dmamux_route_allocate(struct of_phandle_args *dma_spec,
  	}
 
  	/* Set dma request */
-	spin_lock_irqsave(&dmamux->lock, flags);

drivers/dma/stm32/stm32-dmamux.c static void *stm32_dmamux_route_allocate(struct of_phandle_args *dma_spec, · line 147

Quoted 10 lines
drivers/dma/stm32/stm32-dmamux.c +1 -4
  	ret = pm_runtime_resume_and_get(&pdev->dev);
-	if (ret < 0) {
-		spin_unlock_irqrestore(&dmamux->lock, flags);
+	if (ret < 0)
  		goto err_put_dma_spec_np;
-	}
-	spin_unlock_irqrestore(&dmamux->lock, flags);
 
  	mux->request = dma_spec->args[0];
 

In addition, I suggest:
@@
- stm32_dmamux_write(dmamux->iomem, STM32_DMAMUX_CCR(mux->chan_id),
- mux->request);
+ spin_lock_irqsave(&dmamux->lock, flags);
+ stm32_dmamux_write(dmamux->iomem, STM32_DMAMUX_CCR(mux->chan_id),
+ mux->request);
+ spin_unlock_irqrestore(&dmamux->lock, flags);
+

And moving pm_runtime_put_sync() in stm32_dmamux_free():
@@ static void stm32_dmamux_free(struct device *dev, void *route_data)

     /* Clear dma request */
     spin_lock_irqsave(&dmamux->lock, flags);

     stm32_dmamux_write(dmamux->iomem, STM32_DMAMUX_CCR(mux->chan_id), 0);
     clear_bit(mux->chan_id, dmamux->dma_inuse);

- pm_runtime_put_sync(dev);
-

     spin_unlock_irqrestore(&dmamux->lock, flags);

+ pm_runtime_put_sync(dev);
+

     dev_dbg(dev, "Unmapping DMAMUX(%u) to DMA%u(%u)\n",
         mux->request, mux->master, mux->chan_id);

Runtime PM calls are made outside the lock, which is held only for the CCR write and the dma_inuse bitmap.

Regards,
Amelie

#2 lore raw

PATCH v2 1/1 dmaengine: stm32-dmamux: don't call runtime PM under a spinlock

stm32_dmamux_route_allocate() calls pm_runtime_resume_and_get() while holding dmamux->lock with interrupts disabled, and stm32_dmamux_free() calls pm_runtime_put_sync() in the same way. The runtime callbacks enable and disable the clock with clk_prepare_enable() / clk_disable_unprepare(), which can sleep, and the device is not marked pm_runtime_irq_safe(), so both sleep in atomic context.

The lock was in the wrong place: it did not protect the runtime PM call, while the CCR write in stm32_dmamux_route_allocate() was left unlocked even though stm32_dmamux_free() writes the same register under the lock. Move the runtime PM calls outside the lock in both functions and take the lock for the CCR write instead, matching stm32_dmamux_free().

Fixes 4f3ceca254e0 ("dmaengine: stm32-dmamux: Add PM Runtime support")

---
v2: Amelie Delaunay pointed out that v1 was correct but incomplete:
- also take dmamux->lock for the CCR write in stm32_dmamux_route_allocate(), which stm32_dmamux_free() already does for the same register;
- also move pm_runtime_put_sync() out of the lock in stm32_dmamux_free(), which has the same sleep-in-atomic problem.
Link to v1: https://lore.kernel.org/all/[email protected]/

 drivers/dma/stm32/stm32-dmamux.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/dma/stm32/stm32-dmamux.c +6 -7
index 5731871..8290122 100644
@@ -73,10 +73,10 @@ static void stm32_dmamux_free(struct device *dev, void *route_data)
 	stm32_dmamux_write(dmamux->iomem, STM32_DMAMUX_CCR(mux->chan_id), 0);
 	clear_bit(mux->chan_id, dmamux->dma_inuse);
 
-	pm_runtime_put_sync(dev);
-
 	spin_unlock_irqrestore(&dmamux->lock, flags);
 
+	pm_runtime_put_sync(dev);
+
 	dev_dbg(dev, "Unmapping DMAMUX(%u) to DMA%u(%u)\n",
 		mux->request, mux->master, mux->chan_id);
 
@@ -139,13 +139,9 @@ static void *stm32_dmamux_route_allocate(struct of_phandle_args *dma_spec,
 	}
 
 	/* Set dma request */
-	spin_lock_irqsave(&dmamux->lock, flags);
 	ret = pm_runtime_resume_and_get(&pdev->dev);
-	if (ret < 0) {
-		spin_unlock_irqrestore(&dmamux->lock, flags);
+	if (ret < 0)
 		goto err_put_dma_spec_np;
-	}
-	spin_unlock_irqrestore(&dmamux->lock, flags);
 
 	mux->request = dma_spec->args[0];
 
@@ -156,8 +152,11 @@ static void *stm32_dmamux_route_allocate(struct of_phandle_args *dma_spec,
 	dma_spec->args[0] = mux->chan_id - min;
 	dma_spec->args_count = 4;
 
+	spin_lock_irqsave(&dmamux->lock, flags);
 	stm32_dmamux_write(dmamux->iomem, STM32_DMAMUX_CCR(mux->chan_id),
 			   mux->request);
+	spin_unlock_irqrestore(&dmamux->lock, flags);
+
 	dev_dbg(&pdev->dev, "Mapping DMAMUX(%u) to DMA%u(%u)\n",
 		mux->request, mux->master, mux->chan_id);
 
Signature · 1 line
2.43.0
Reviewed-by on v2 1/1
#3 lore raw

Re: [PATCH v2] dmaengine: stm32-dmamux: don't call runtime PM under a spinlock

On Thu, Nov 19, 2026 at 12:28:43AM +0900, Hyunjung Ko wrote:

Avoid post new version patch to old email thread, which cause other tools.

Quoted 13 lines

stm32_dmamux_route_allocate() calls pm_runtime_resume_and_get() while holding dmamux->lock with interrupts disabled, and stm32_dmamux_free() calls pm_runtime_put_sync() in the same way. The runtime callbacks enable and disable the clock with clk_prepare_enable() / clk_disable_unprepare(), which can sleep, and the device is not marked pm_runtime_irq_safe(), so both sleep in atomic context.

The lock was in the wrong place: it did not protect the runtime PM call, while the CCR write in stm32_dmamux_route_allocate() was left unlocked even though stm32_dmamux_free() writes the same register under the lock. Move the runtime PM calls outside the lock in both functions and take the lock for the CCR write instead, matching stm32_dmamux_free().

Quoted 5 lines
Fixes 4f3ceca254e0 ("dmaengine: stm32-dmamux: Add PM Runtime support")

---

Quoted 58 lines

v2: Amelie Delaunay pointed out that v1 was correct but incomplete:
- also take dmamux->lock for the CCR write in stm32_dmamux_route_allocate(), which stm32_dmamux_free() already does for the same register;
- also move pm_runtime_put_sync() out of the lock in stm32_dmamux_free(), which has the same sleep-in-atomic problem.
Link to v1: https://lore.kernel.org/all/[email protected]/

 drivers/dma/stm32/stm32-dmamux.c | 13 ++++++-------
 1 file changed, 6 insertions(+), 7 deletions(-)
drivers/dma/stm32/stm32-dmamux.c +6 -8
index 5731871..8290122 100644
@@ -73,10 +73,10 @@ static void stm32_dmamux_free(struct device *dev, void *route_data)
 	stm32_dmamux_write(dmamux->iomem, STM32_DMAMUX_CCR(mux->chan_id), 0);
 	clear_bit(mux->chan_id, dmamux->dma_inuse);
 
-	pm_runtime_put_sync(dev);
-
 	spin_unlock_irqrestore(&dmamux->lock, flags);
 
+	pm_runtime_put_sync(dev);
+
 	dev_dbg(dev, "Unmapping DMAMUX(%u) to DMA%u(%u)\n",
 		mux->request, mux->master, mux->chan_id);
 
@@ -139,13 +139,9 @@ static void *stm32_dmamux_route_allocate(struct of_phandle_args *dma_spec,
 	}
 
 	/* Set dma request */
-	spin_lock_irqsave(&dmamux->lock, flags);
 	ret = pm_runtime_resume_and_get(&pdev->dev);
-	if (ret < 0) {
-		spin_unlock_irqrestore(&dmamux->lock, flags);
+	if (ret < 0)
 		goto err_put_dma_spec_np;
-	}
-	spin_unlock_irqrestore(&dmamux->lock, flags);
 
 	mux->request = dma_spec->args[0];
 
@@ -156,8 +152,11 @@ static void *stm32_dmamux_route_allocate(struct of_phandle_args *dma_spec,
 	dma_spec->args[0] = mux->chan_id - min;
 	dma_spec->args_count = 4;
 
+	spin_lock_irqsave(&dmamux->lock, flags);
 	stm32_dmamux_write(dmamux->iomem, STM32_DMAMUX_CCR(mux->chan_id),
 			   mux->request);
+	spin_unlock_irqrestore(&dmamux->lock, flags);
+
 	dev_dbg(&pdev->dev, "Mapping DMAMUX(%u) to DMA%u(%u)\n",
 		mux->request, mux->master, mux->chan_id);
 
--

2.43.0

maintainer Reviewed-by Tested-by on v2 1/1
#4 lore raw

Re: [PATCH v2] dmaengine: stm32-dmamux: don't call runtime PM under a spinlock

In reply to Hyunjung Ko
Quoted 13 lines · Hyunjung Ko

stm32_dmamux_route_allocate() calls pm_runtime_resume_and_get() while holding dmamux->lock with interrupts disabled, and stm32_dmamux_free() calls pm_runtime_put_sync() in the same way. The runtime callbacks enable and disable the clock with clk_prepare_enable() / clk_disable_unprepare(), which can sleep, and the device is not marked pm_runtime_irq_safe(), so both sleep in atomic context.

The lock was in the wrong place: it did not protect the runtime PM call, while the CCR write in stm32_dmamux_route_allocate() was left unlocked even though stm32_dmamux_free() writes the same register under the lock. Move the runtime PM calls outside the lock in both functions and take the lock for the CCR write instead, matching stm32_dmamux_free().

Quoted 4 lines
Fixes 4f3ceca254e0 ("dmaengine: stm32-dmamux: Add PM Runtime support")

I’ve tested your patch on the stm32mp157f-dk2. Thanks!

Quoted 56 lines

---
v2: Amelie Delaunay pointed out that v1 was correct but incomplete:
- also take dmamux->lock for the CCR write in stm32_dmamux_route_allocate(), which stm32_dmamux_free() already does for the same register;
- also move pm_runtime_put_sync() out of the lock in stm32_dmamux_free(), which has the same sleep-in-atomic problem.
Link to v1: https://lore.kernel.org/all/[email protected]/

  drivers/dma/stm32/stm32-dmamux.c | 13 ++++++-------
  1 file changed, 6 insertions(+), 7 deletions(-)
drivers/dma/stm32/stm32-dmamux.c +6 -7
index 5731871..8290122 100644
@@ -73,10 +73,10 @@ static void stm32_dmamux_free(struct device *dev, void *route_data)
  	stm32_dmamux_write(dmamux->iomem, STM32_DMAMUX_CCR(mux->chan_id), 0);
  	clear_bit(mux->chan_id, dmamux->dma_inuse);
 
-	pm_runtime_put_sync(dev);
-
  	spin_unlock_irqrestore(&dmamux->lock, flags);
 
+	pm_runtime_put_sync(dev);
+
  	dev_dbg(dev, "Unmapping DMAMUX(%u) to DMA%u(%u)\n",
  		mux->request, mux->master, mux->chan_id);
 
@@ -139,13 +139,9 @@ static void *stm32_dmamux_route_allocate(struct of_phandle_args *dma_spec,
  	}
 
  	/* Set dma request */
-	spin_lock_irqsave(&dmamux->lock, flags);
  	ret = pm_runtime_resume_and_get(&pdev->dev);
-	if (ret < 0) {
-		spin_unlock_irqrestore(&dmamux->lock, flags);
+	if (ret < 0)
  		goto err_put_dma_spec_np;
-	}
-	spin_unlock_irqrestore(&dmamux->lock, flags);
 
  	mux->request = dma_spec->args[0];
 
@@ -156,8 +152,11 @@ static void *stm32_dmamux_route_allocate(struct of_phandle_args *dma_spec,
  	dma_spec->args[0] = mux->chan_id - min;
  	dma_spec->args_count = 4;
 
+	spin_lock_irqsave(&dmamux->lock, flags);
  	stm32_dmamux_write(dmamux->iomem, STM32_DMAMUX_CCR(mux->chan_id),
  			   mux->request);
+	spin_unlock_irqrestore(&dmamux->lock, flags);
+
  	dev_dbg(&pdev->dev, "Mapping DMAMUX(%u) to DMA%u(%u)\n",
  		mux->request, mux->master, mux->chan_id);