Skip to contentSkip to conversation

dmaengine: at_hdmac: ioremap and get irq early

PATCH In review Rosen Penev 3 participants 4 messages Jul 23 – Aug 20

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

PATCH 1/1 dmaengine: at_hdmac: ioremap and get irq early

The functions can return -EPROBE_DEFER. Call them before anything else to avoid doing extra work.

---
 drivers/dma/at_hdmac.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/dma/at_hdmac.c +10 -7
index e5b30a57c477..2f6f5877fc3c 100644
@@ -1943,11 +1943,20 @@ static void at_dma_off(struct at_dma *atdma)
 static int __init at_dma_probe(struct platform_device *pdev)
 {
 	struct at_dma		*atdma;
+	void __iomem		*regs;
 	int			irq;
 	int			err;
 	int			i;
 	const struct at_dma_platform_data *plat_dat;
 
+	regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(regs))
+		return PTR_ERR(regs);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return irq;
+
 	/* setup platform data for each SoC */
 	dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
 	dma_cap_set(DMA_INTERLEAVE, at91sam9g45_config.cap_mask);
@@ -1968,13 +1977,7 @@ static int __init at_dma_probe(struct platform_device *pdev)
 	if (!atdma)
 		return -ENOMEM;
 
-	atdma->regs = devm_platform_ioremap_resource(pdev, 0);
-	if (IS_ERR(atdma->regs))
-		return PTR_ERR(atdma->regs);
-
-	irq = platform_get_irq(pdev, 0);
-	if (irq < 0)
-		return irq;
+	atdma->regs = regs;
 
 	/* discover transaction capabilities */
 	atdma->dma_device.cap_mask = plat_dat->cap_mask;
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Use-After-Free due to missing tasklet_kill() on driver removal --

--- Patch [1]: [PATCH] dmaengine: at_hdmac: ioremap and get irq early --- commit c590e127b59a7e207f5a617b28aad848c06bc09b
Author: Rosen Penev <[email protected]>

dmaengine: at_hdmac: ioremap and get irq early

This commit updates the driver to map I/O memory and retrieve the IRQ early during probe. By performing these operations before allocating memory and configuring the DMA controller, it avoids unnecessary work if they return -EPROBE_DEFER.

Quoted 4 lines
drivers/dma/at_hdmac.c +0 -0
index e5b30a57c477a..2f6f5877fc3cc 100644

[ ... ]

Quoted 5 lines
drivers/dma/at_hdmac.c +0 -1
@@ -1968,13 +1977,7 @@ static int __init at_dma_probe(struct platform_device *pdev)
 	if (!atdma)
 		return -ENOMEM;
 
-	atdma->regs = devm_platform_ioremap_resource(pdev, 0);

drivers/dma/at_hdmac.c static int __init at_dma_probe(struct platform_device *pdev) · line 1983

Quoted 10 lines
drivers/dma/at_hdmac.c +1 -6
-	if (IS_ERR(atdma->regs))
-		return PTR_ERR(atdma->regs);
-
-	irq = platform_get_irq(pdev, 0);
-	if (irq < 0)
-		return irq;
+	atdma->regs = regs;
 
 	/* discover transaction capabilities */
 	atdma->dma_device.cap_mask = plat_dat->cap_mask;

[Severity: High]
This is a pre-existing issue, but does at_dma_remove() need to call tasklet_kill() to prevent a use-after-free during driver removal?

If an interrupt fires just before driver removal or device unbind, it can schedule a channel tasklet via vchan_cookie_complete(), leaving it in the scheduler queue.

at_dma_remove() cleans up the channels but doesn't kill the tasklets:

drivers/dma/at_hdmac.c:at_dma_remove() {
...

	list_for_each_entry_safe(chan, _chan, &atdma->dma_device.channels,
			device_node) {
		/* Disable interrupts */
		atc_disable_chan_irq(atdma, chan->chan_id);
		list_del(&chan->device_node);
	}

...
}

When at_dma_remove() returns, the devres mechanism frees the at_dma structure containing the tasklet structs.

If a pending tasklet executes on the freed memory, can it cause a use-after-free and memory corruption?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#2 lore raw
In reply to Rosen Penev
Quoted 2 lines · Rosen Penev

The functions can return -EPROBE_DEFER. Call them before anything else to avoid doing extra work.

just move before one devm_kalloc(), I have not seen much beanfit for it.

Frank

Quoted 49 lines
---
 drivers/dma/at_hdmac.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/dma/at_hdmac.c +10 -8
index e5b30a57c477..2f6f5877fc3c 100644
@@ -1943,11 +1943,20 @@ static void at_dma_off(struct at_dma *atdma)
 static int __init at_dma_probe(struct platform_device *pdev)
 {
 	struct at_dma		*atdma;
+	void __iomem		*regs;
 	int			irq;
 	int			err;
 	int			i;
 	const struct at_dma_platform_data *plat_dat;
 
+	regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(regs))
+		return PTR_ERR(regs);
+
+	irq = platform_get_irq(pdev, 0);
+	if (irq < 0)
+		return irq;
+
 	/* setup platform data for each SoC */
 	dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
 	dma_cap_set(DMA_INTERLEAVE, at91sam9g45_config.cap_mask);
@@ -1968,13 +1977,7 @@ static int __init at_dma_probe(struct platform_device *pdev)
 	if (!atdma)
 		return -ENOMEM;
 
-	atdma->regs = devm_platform_ioremap_resource(pdev, 0);
-	if (IS_ERR(atdma->regs))
-		return PTR_ERR(atdma->regs);
-
-	irq = platform_get_irq(pdev, 0);
-	if (irq < 0)
-		return irq;
+	atdma->regs = regs;
 
 	/* discover transaction capabilities */
 	atdma->dma_device.cap_mask = plat_dat->cap_mask;
--

2.55.0

#3 lore raw
Quoted 6 lines · Frank Li
Quoted 2 lines · Rosen Penev

The functions can return -EPROBE_DEFER. Call them before anything else to avoid doing extra work.

just move before one devm_kalloc(), I have not seen much beanfit for it.

Not sure I follow. devm_platform_ioremap_resource() and platform_get_irq() just need a platform_device pointer.

Quoted 52 lines

Frank

Quoted 49 lines
---
 drivers/dma/at_hdmac.c | 17 ++++++++++-------
 1 file changed, 10 insertions(+), 7 deletions(-)
drivers/dma/at_hdmac.c +10 -8
index e5b30a57c477..2f6f5877fc3c 100644
@@ -1943,11 +1943,20 @@ static void at_dma_off(struct at_dma *atdma)
 static int __init at_dma_probe(struct platform_device *pdev)
 {
      struct at_dma           *atdma;
+     void __iomem            *regs;
      int                     irq;
      int                     err;
      int                     i;
      const struct at_dma_platform_data *plat_dat;
 
+     regs = devm_platform_ioremap_resource(pdev, 0);
+     if (IS_ERR(regs))
+             return PTR_ERR(regs);
+
+     irq = platform_get_irq(pdev, 0);
+     if (irq < 0)
+             return irq;
+
      /* setup platform data for each SoC */
      dma_cap_set(DMA_MEMCPY, at91sam9rl_config.cap_mask);
      dma_cap_set(DMA_INTERLEAVE, at91sam9g45_config.cap_mask);
@@ -1968,13 +1977,7 @@ static int __init at_dma_probe(struct platform_device *pdev)
      if (!atdma)
              return -ENOMEM;
 
-     atdma->regs = devm_platform_ioremap_resource(pdev, 0);
-     if (IS_ERR(atdma->regs))
-             return PTR_ERR(atdma->regs);
-
-     irq = platform_get_irq(pdev, 0);
-     if (irq < 0)
-             return irq;
+     atdma->regs = regs;
 
      /* discover transaction capabilities */
      atdma->dma_device.cap_mask = plat_dat->cap_mask;
--

2.55.0