Skip to contentSkip to conversation

dmaengine: edma: fix vchan cleanup and improvement

PATCH New Peng Fan (OSS) 2 participants 6 messages Nov 23

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

cover dmaengine: edma: fix vchan cleanup and improvement

To avoid delaying the fix because of the iommu discussion [1], I separate three patches that not relavant to iommu from patchset [2].

Patch 1 is a fix, patch 2,3 are for cleanup.

All three patches have been reviewed by Frank Li. There might be a sashiko warning about BIT_ULL in patch 2, that is false alarm, ignore it.

Detailed information could be found in each patch commit log.

[1] https://lore.kernel.org/all/[email protected]/
[2]https://lore.kernel.org/all/[email protected]/

---
Peng Fan (3):
dmaengine: fsl-edma: unregister channels before vchan cleanup
dmaengine: fsl-edma: use BIT_ULL for 64-bit channel mask
dmaengine: fsl-edma: use dmaengine_get_dma_device()

 drivers/dma/fsl-edma-common.c | 10 +++++-----
 drivers/dma/fsl-edma-main.c   | 26 ++++++++++++++++++--------
 2 files changed, 23 insertions(+), 13 deletions(-)

---
base-commit: 909ef7a9a30fa139e69e555e3606680f5cca7726
change-id: 20261123-edma-fixes-8493253613ca

Best regards,

Signature · 1 line
Peng Fan <[email protected]>
#1 lore raw

PATCH 1/3 dmaengine: fsl-edma: unregister channels before vchan cleanup

From: Peng Fan <[email protected]>

Since commit 4035726a6b72 ("dmaengine: fsl-edma: Use managed API dmaenginem_async_device_register()"), dma_async_device_unregister() runs as a devres action after fsl_edma_remove(). However, fsl_edma_remove() still calls fsl_edma_cleanup_vchan() first, which removes every channel from dma_dev.channels. When the devres action later runs, dma_async_device_unregister() sees an empty channel list and never unregisters the channel devices. They remain visible in /sys/class/dma.

Keep the vchan cleanup, but register it as a devres action before dmaenginem_async_device_register(). Devres actions run in reverse order, so the DMA channels are unregistered first and the vchan list is cleaned up afterwards. Remove the premature cleanup call from fsl_edma_remove().

Fixes 4035726a6b72 ("dmaengine: fsl-edma: Use managed API dmaenginem_async_device_register()")
---
 drivers/dma/fsl-edma-main.c | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
drivers/dma/fsl-edma-main.c +11 -1
index da5139911488..7f82dcb7831d 100644
@@ -31,6 +31,11 @@ static void fsl_edma_synchronize(struct dma_chan *chan)
 	vchan_synchronize(&fsl_chan->vchan);
 }
 
+static void fsl_edma_cleanup_vchan_action(void *data)
+{
+	fsl_edma_cleanup_vchan(data);
+}
+
 static irqreturn_t fsl_edma_tx_handler(int irq, void *dev_id)
 {
 	struct fsl_edma_engine *fsl_edma = dev_id;
@@ -881,6 +886,12 @@ static int fsl_edma_probe(struct platform_device *pdev)
 
 	platform_set_drvdata(pdev, fsl_edma);
 
+	ret = devm_add_action_or_reset(&pdev->dev,
+				       fsl_edma_cleanup_vchan_action,
+				       &fsl_edma->dma_dev);
+	if (ret)
+		return ret;
+
 	ret = dmaenginem_async_device_register(&fsl_edma->dma_dev);
 	if (ret)
 		return dev_err_probe(&pdev->dev, ret,
@@ -905,7 +916,6 @@ static void fsl_edma_remove(struct platform_device *pdev)
 	struct fsl_edma_engine *fsl_edma = platform_get_drvdata(pdev);
 
 	fsl_edma_irq_exit(pdev, fsl_edma);
-	fsl_edma_cleanup_vchan(&fsl_edma->dma_dev);
 }
 
 static int fsl_edma_suspend_late(struct device *dev)
Signature · 1 line
2.34.1
#2 lore raw

PATCH 2/3 dmaengine: fsl-edma: use BIT_ULL for 64-bit channel mask

In reply to Peng Fan (OSS)

From: Peng Fan <[email protected]>

chan_masked is a u64 bitmap, but several checks use BIT(), which uses unsigned long and overflows on 32-bit architectures for channels >= 32. Use BIT_ULL() so the upper channels are tested correctly.

---
 drivers/dma/fsl-edma-main.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/dma/fsl-edma-main.c +7 -7
index 7f82dcb7831d..818b9b986f13 100644
@@ -123,7 +123,7 @@ static irqreturn_t fsl_edma3_err_handler_shared(int irq, void *dev_id)
 	unsigned int ch;
 
 	for (ch = 0; ch < fsl_edma->n_chans; ch++) {
-		if (fsl_edma->chan_masked & BIT(ch))
+		if (fsl_edma->chan_masked & BIT_ULL(ch))
 			continue;
 
 		fsl_edma3_err_check(&fsl_edma->chans[ch]);
@@ -397,7 +397,7 @@ static int fsl_edma3_irq_init(struct platform_device *pdev, struct fsl_edma_engi
 
 		struct fsl_edma_chan *fsl_chan = &fsl_edma->chans[i];
 
-		if (fsl_edma->chan_masked & BIT(i))
+		if (fsl_edma->chan_masked & BIT_ULL(i))
 			continue;
 
 		/* request channel irq */
@@ -641,7 +641,7 @@ static void fsl_edma3_detach_pd(struct fsl_edma_engine *fsl_edma)
 	int i;
 
 	for (i = 0; i < fsl_edma->n_chans; i++) {
-		if (fsl_edma->chan_masked & BIT(i))
+		if (fsl_edma->chan_masked & BIT_ULL(i))
 			continue;
 		fsl_chan = &fsl_edma->chans[i];
 		if (fsl_chan->pd_dev_link)
@@ -669,7 +669,7 @@ static int fsl_edma3_attach_pd(struct platform_device *pdev, struct fsl_edma_eng
 	dev = &pdev->dev;
 
 	for (i = 0; i < fsl_edma->n_chans; i++) {
-		if (fsl_edma->chan_masked & BIT(i))
+		if (fsl_edma->chan_masked & BIT_ULL(i))
 			continue;
 
 		fsl_chan = &fsl_edma->chans[i];
@@ -797,7 +797,7 @@ static int fsl_edma_probe(struct platform_device *pdev)
 		struct fsl_edma_chan *fsl_chan = &fsl_edma->chans[i];
 		int len;
 
-		if (fsl_edma->chan_masked & BIT(i))
+		if (fsl_edma->chan_masked & BIT_ULL(i))
 			continue;
 
 		snprintf(fsl_chan->chan_name, sizeof(fsl_chan->chan_name), "%s-CH%02d",
@@ -927,7 +927,7 @@ static int fsl_edma_suspend_late(struct device *dev)
 
 	for (i = 0; i < fsl_edma->n_chans; i++) {
 		fsl_chan = &fsl_edma->chans[i];
-		if (fsl_edma->chan_masked & BIT(i))
+		if (fsl_edma->chan_masked & BIT_ULL(i))
 			continue;
 		spin_lock_irqsave(&fsl_chan->vchan.lock, flags);
 		/* Make sure chan is idle or will force disable. */
@@ -953,7 +953,7 @@ static int fsl_edma_resume_early(struct device *dev)
 
 	for (i = 0; i < fsl_edma->n_chans; i++) {
 		fsl_chan = &fsl_edma->chans[i];
-		if (fsl_edma->chan_masked & BIT(i))
+		if (fsl_edma->chan_masked & BIT_ULL(i))
 			continue;
 		fsl_chan->pm_state = RUNNING;
 		edma_write_tcdreg(fsl_chan, 0, csr);
Signature · 1 line
2.34.1
#3 lore raw

Re: [PATCH 2/3] dmaengine: fsl-edma: use BIT_ULL for 64-bit channel mask

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] dmaengine: fsl-edma: missing Fixes: trailer for chan_masked BIT_ULL() fix It also noticed 1 related…

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] dmaengine: fsl-edma: missing Fixes: trailer for chan_masked BIT_ULL() fix
It also noticed 1 related pre-existing issue this patch does not introduce:
- [Critical] dmaengine: fsl-edma: use-after-free in fsl_edma3_detach_pd() [1]
--

commit 61ad7f34b4fcfc1b5709f370095dce7e4b0ea1d5
Author: Peng Fan <[email protected]>

Quoted 8 lines

dmaengine: fsl-edma: use BIT_ULL for 64-bit channel mask

chan_masked is a u64 bitmap, but several checks use BIT(), which uses unsigned long and overflows on 32-bit architectures for channels >= 32. Use BIT_ULL() so the upper channels are tested correctly.

[Severity: Low]
This fixes the 32-bit BIT() overflow on the 64-bit chan_masked bitmap introduced in commit 72f5801a4e2b ("dmaengine: fsl-edma: integrate v3 support") and replicated in commits 2838a897654c ("dmaengine: fsl-edma: Do not suspend and resume the masked dma channel when the system is sleeping"), ccfa3131d4a0 ("dmaengine: fsl-edma: implement the cleanup path of fsl_edma3_attach_pd()"), and d175222f5e90 ("dmaegnine: fsl-edma: add edma error interrupt handler"). Should this include a Fixes: tag for 72f5801a4e2b ("dmaengine: fsl-edma: integrate v3 support") (and optionally for the follow-up commits)?

Quoted 4 lines
drivers/dma/fsl-edma-main.c +0 -0
index 7f82dcb7831d0..818b9b986f131 100644

[ ... ]

drivers/dma/fsl-edma-main.c static void fsl_edma3_detach_pd(struct fsl_edma_engine *fsl_edma) · line 647

Quoted 9 lines
drivers/dma/fsl-edma-main.c +1 -1
@@ -641,7 +641,7 @@ static void fsl_edma3_detach_pd(struct fsl_edma_engine *fsl_edma)
 	int i;
 
 	for (i = 0; i < fsl_edma->n_chans; i++) {
-		if (fsl_edma->chan_masked & BIT(i))
+		if (fsl_edma->chan_masked & BIT_ULL(i))
 			continue;
 		fsl_chan = &fsl_edma->chans[i];
 		if (fsl_chan->pd_dev_link)

[Severity: Critical]
This problem wasn't introduced by this patch, but does fsl_edma3_detach_pd() trigger a use-after-free on fsl_chan->pd_dev [1]?

drivers/dma/fsl-edma-main.c:fsl_edma3_detach_pd() {
    ...
		if (fsl_chan->pd_dev_link)
			device_link_del(fsl_chan->pd_dev_link);
		if (fsl_chan->pd_dev) {
			dev_pm_domain_detach(fsl_chan->pd_dev, false);
			pm_runtime_dont_use_autosuspend(fsl_chan->pd_dev);
			pm_runtime_set_suspended(fsl_chan->pd_dev);
		}
    ...
}

After device_link_del() removes the stateless device link, dev_pm_domain_detach() calls genpd_dev_pm_detach() -> device_unregister(), which drops the last reference to the virtual genpd device and frees fsl_chan->pd_dev. The subsequent pm_runtime_dont_use_autosuspend() and pm_runtime_set_suspended() calls then lock and modify the freed device during an attach error, probe failure, or driver unbind.

[1] https://sashiko.dev/#/bug/linux-274f4f33-0d23-4b6b-a957-25b9d8ad2f6a

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

PATCH 3/3 dmaengine: fsl-edma: use dmaengine_get_dma_device()

In reply to Peng Fan (OSS)

From: Peng Fan <[email protected]>

Use dmaengine_get_dma_device() instead of chan->device->dev when creating the TCD pool and when mapping the slave resource. This lets the driver use the per-channel device once per-channel IOMMU support enables it, without changing behaviour for existing users.

---
 drivers/dma/fsl-edma-common.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/dma/fsl-edma-common.c +5 -5
index 513b0f4e5be2..62cd60179bc6 100644
@@ -310,10 +310,10 @@ int fsl_edma_resume(struct dma_chan *chan)
 
 static void fsl_edma_unprep_slave_dma(struct fsl_edma_chan *fsl_chan)
 {
+	struct device *dev = dmaengine_get_dma_device(&fsl_chan->vchan.chan);
+
 	if (fsl_chan->dma_dir != DMA_NONE)
-		dma_unmap_resource(fsl_chan->vchan.chan.device->dev,
-				   fsl_chan->dma_dev_addr,
-				   fsl_chan->dma_dev_size,
+		dma_unmap_resource(dev, fsl_chan->dma_dev_addr, fsl_chan->dma_dev_size,
 				   fsl_chan->dma_dir, 0);
 	fsl_chan->dma_dir = DMA_NONE;
 }
@@ -321,7 +321,7 @@ static void fsl_edma_unprep_slave_dma(struct fsl_edma_chan *fsl_chan)
 static bool fsl_edma_prep_slave_dma(struct fsl_edma_chan *fsl_chan,
 				    enum dma_transfer_direction dir)
 {
-	struct device *dev = fsl_chan->vchan.chan.device->dev;
+	struct device *dev = dmaengine_get_dma_device(&fsl_chan->vchan.chan);
 	enum dma_data_direction dma_dir;
 	phys_addr_t addr = 0;
 	u32 size = 0;
@@ -1090,7 +1090,7 @@ int fsl_edma_alloc_chan_resources(struct dma_chan *chan)
 	if (fsl_edma_drvflags(fsl_chan) & FSL_EDMA_DRV_HAS_CHCLK)
 		clk_prepare_enable(fsl_chan->clk);
 
-	fsl_chan->tcd_pool = dma_pool_create("tcd_pool", chan->device->dev,
+	fsl_chan->tcd_pool = dma_pool_create("tcd_pool", dmaengine_get_dma_device(chan),
 				fsl_edma_drvflags(fsl_chan) & FSL_EDMA_DRV_TCD64 ?
 				sizeof(struct fsl_edma_hw_tcd64) : sizeof(struct fsl_edma_hw_tcd),
 				32, 0);
Signature · 1 line
2.34.1
#5 lore raw

Re: [PATCH 3/3] dmaengine: fsl-edma: use dmaengine_get_dma_device()

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce: - [High] dmaengine: fsl-edma: unchecked…

Thank you for your contribution! Sashiko AI review found no new issues in this patch, but noticed 1 related pre-existing issue it does not introduce:
- [High] dmaengine: fsl-edma: unchecked dma_pool_create() in fsl_edma_alloc_chan_resources() [1]
--

commit d7d84b7032569e7e063763bff448d75503da9441
Author: Peng Fan <[email protected]>

dmaengine: fsl-edma: use dmaengine_get_dma_device()

This commit uses dmaengine_get_dma_device() instead of chan->device->dev when creating the TCD pool and mapping slave DMA resources.

Quoted 4 lines
drivers/dma/fsl-edma-common.c +0 -0
index 513b0f4e5be21..62cd60179bc6f 100644

[ ... ]

drivers/dma/fsl-edma-common.c int fsl_edma_alloc_chan_resources(struct dma_chan *chan) · line 1096

Quoted 9 lines
drivers/dma/fsl-edma-common.c +1 -1
@@ -1090,7 +1090,7 @@ int fsl_edma_alloc_chan_resources(struct dma_chan *chan)
 	if (fsl_edma_drvflags(fsl_chan) & FSL_EDMA_DRV_HAS_CHCLK)
 		clk_prepare_enable(fsl_chan->clk);
 
-	fsl_chan->tcd_pool = dma_pool_create("tcd_pool", chan->device->dev,
+	fsl_chan->tcd_pool = dma_pool_create("tcd_pool", dmaengine_get_dma_device(chan),
 				fsl_edma_drvflags(fsl_chan) & FSL_EDMA_DRV_TCD64 ?
 				sizeof(struct fsl_edma_hw_tcd64) : sizeof(struct fsl_edma_hw_tcd),
 				32, 0);

[Severity: High]
This problem wasn't introduced by this patch, but can a failure in dma_pool_create() lead to a NULL pointer dereference [1]?

fsl_edma_alloc_chan_resources() does not check fsl_chan->tcd_pool for NULL and still returns 0. A subsequent transfer preparation then passes the NULL fsl_chan->tcd_pool to dma_pool_alloc() in fsl_edma_alloc_desc().

[1] https://sashiko.dev/#/bug/linux-39a3675f-2d52-4af1-8fbc-609bc1c22d2a

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