Skip to contentSkip to conversation

dmaengine: txx9dmac: use devm_platform_ioremap_resource()

PATCH Applied Rosen Penev 4 participants 4 messages Jul 14 – Sep 3 dmaengine

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 dmaengine: txx9dmac: use devm_platform_ioremap_resource()

Replace the open-coded platform_get_resource() plus devm_request_mem_region() and devm_ioremap() sequence with a single devm_platform_ioremap_resource() call, which folds the resource lookup, region reservation and mapping into one step and returns an ERR_PTR on failure, checked with IS_ERR() and propagated via PTR_ERR().

This is behaviorally equivalent: the driver already reserved the region with devm_request_mem_region(), so the non-overlapping reg requirement of devm_platform_ioremap_resource() was already satisfied. The txx9dmac platform device (arch/mips/txx9/generic/setup.c) provides a single IORESOURCE_MEM window per DMAC instance, and the child txx9dmac-chan devices carry only IRQ resources, so no region conflict is introduced.

Assisted-by opencode:hy3-free
---
 drivers/dma/txx9dmac.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
drivers/dma/txx9dmac.c +5 -11
index 05622b68a936..6595a54a4b97 100644
@@ -1167,26 +1167,20 @@ static void txx9dmac_chan_remove(struct platform_device *pdev)
 static int __init txx9dmac_probe(struct platform_device *pdev)
 {
 	struct txx9dmac_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	struct resource *io;
 	struct txx9dmac_dev *ddev;
+	void __iomem *regs;
 	u32 mcr;
 	int err;
 
-	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-	if (!io)
-		return -EINVAL;
+	regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(regs))
+		return PTR_ERR(regs);
 
 	ddev = devm_kzalloc(&pdev->dev, sizeof(*ddev), GFP_KERNEL);
 	if (!ddev)
 		return -ENOMEM;
 
-	if (!devm_request_mem_region(&pdev->dev, io->start, resource_size(io),
-				     dev_name(&pdev->dev)))
-		return -EBUSY;
-
-	ddev->regs = devm_ioremap(&pdev->dev, io->start, resource_size(io));
-	if (!ddev->regs)
-		return -ENOMEM;
+	ddev->regs = regs;
 	ddev->have_64bit_regs = pdata->have_64bit_regs;
 	if (__is_dmac64(ddev))
 		ddev->descsize = sizeof(struct txx9dmac_hwdesc);
Signature · 1 line
2.55.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

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

Pre-existing issues:
- [High] The error path in txx9dmac_chan_probe fails to kill dc->tasklet and leaves a dangling pointer in dc->ddev->chan, leading to Use-After-Free.
- [High] Missing dma_mapping_error() check after mapping memory with dma_map_single() in txx9dmac_desc_alloc.
--

--- Patch [1]: [PATCH] dmaengine: txx9dmac: use devm_platform_ioremap_resource() --- commit bc23f2a8099e4ec156f46c83d8a2cae426f12327
Author: Rosen Penev <[email protected]>

dmaengine: txx9dmac: use devm_platform_ioremap_resource()

Replace the open-coded platform_get_resource() plus devm_request_mem_region() and devm_ioremap() sequence with a single devm_platform_ioremap_resource() call. This folds the resource lookup, region reservation and mapping into one step and returns an ERR_PTR on failure. This is behaviorally equivalent as the driver already reserved the region.

Quoted 26 lines
drivers/dma/txx9dmac.c +4 -5
index 05622b68a9363..6595a54a4b97f 100644
@@ -1167,26 +1167,20 @@ static void txx9dmac_chan_remove(struct platform_device *pdev)
 static int __init txx9dmac_probe(struct platform_device *pdev)
 {
 	struct txx9dmac_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	struct resource *io;
 	struct txx9dmac_dev *ddev;
+	void __iomem *regs;
 	u32 mcr;
 	int err;
 
-	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-	if (!io)
-		return -EINVAL;
+	regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(regs))
+		return PTR_ERR(regs);
 
 	ddev = devm_kzalloc(&pdev->dev, sizeof(*ddev), GFP_KERNEL);
 	if (!ddev)
 		return -ENOMEM;
 
-	if (!devm_request_mem_region(&pdev->dev, io->start, resource_size(io),

drivers/dma/txx9dmac.c static void txx9dmac_chan_remove(struct platform_device *pdev) · line 1186

Quoted 10 lines
drivers/dma/txx9dmac.c +1 -6
-				     dev_name(&pdev->dev)))
-		return -EBUSY;
-
-	ddev->regs = devm_ioremap(&pdev->dev, io->start, resource_size(io));
-	if (!ddev->regs)
-		return -ENOMEM;
+	ddev->regs = regs;
 	ddev->have_64bit_regs = pdata->have_64bit_regs;
 	if (__is_dmac64(ddev))
 		ddev->descsize = sizeof(struct txx9dmac_hwdesc);

While reviewing this file, I noticed a couple of pre-existing issues.

drivers/dma/txx9dmac.c:txx9dmac_chan_probe() {
    ...
	dc->ddev->chan[ch] = dc;
	dc->chan.device = &dc->dma;
	list_add_tail(&dc->chan.device_node, &dc->chan.device->channels);
	dma_cookie_init(&dc->chan);
    ...
	err = dma_async_device_register(&dc->dma);
	if (err)
		return err;
    ...
}

[Severity: High]
This is a pre-existing issue, but does this error path lead to a Use-After-Free?

If dma_async_device_register() fails, the function returns without performing cleanup. It leaves a dangling pointer in dc->ddev->chan[ch] and fails to call tasklet_kill(&dc->tasklet), unlike the cleanup in txx9dmac_chan_remove().

Because the devres framework will free the dc structure, a previously scheduled tasklet or an interrupt handler referencing ddev->chan[0] could dereference the freed dc structure.

drivers/dma/txx9dmac.c:txx9dmac_desc_alloc() {
    ...
	desc->txd.phys = dma_map_single(chan2parent(&dc->chan), &desc->hwdesc,
					ddev->descsize, DMA_TO_DEVICE);
	return desc;
}

[Severity: High]
This is a pre-existing issue, but does this code need to check for DMA mapping errors?

The physical address returned by dma_map_single() is assigned and returned without being validated with dma_mapping_error().

If the mapping fails, it returns a bad DMA address, and the hardware will subsequently attempt to access this invalid address during descriptor fetching.

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

Replace the open-coded platform_get_resource() plus devm_request_mem_region() and devm_ioremap() sequence with a single devm_platform_ioremap_resource() call, which folds the resource lookup, region reservation and mapping into one step and returns an ERR_PTR on failure, checked with IS_ERR() and propagated via PTR_ERR().

This is behaviorally equivalent: the driver already reserved the region with devm_request_mem_region(), so the non-overlapping reg requirement of devm_platform_ioremap_resource() was already satisfied. The txx9dmac platform device (arch/mips/txx9/generic/setup.c) provides a single IORESOURCE_MEM window per DMAC instance, and the child txx9dmac-chan devices carry only IRQ resources, so no region conflict is introduced.

Quoted 3 lines
Assisted-by opencode:hy3-free

---

Quoted 42 lines
 drivers/dma/txx9dmac.c | 16 +++++-----------
 1 file changed, 5 insertions(+), 11 deletions(-)
drivers/dma/txx9dmac.c +5 -12
index 05622b68a936..6595a54a4b97 100644
@@ -1167,26 +1167,20 @@ static void txx9dmac_chan_remove(struct platform_device *pdev)
 static int __init txx9dmac_probe(struct platform_device *pdev)
 {
 	struct txx9dmac_platform_data *pdata = dev_get_platdata(&pdev->dev);
-	struct resource *io;
 	struct txx9dmac_dev *ddev;
+	void __iomem *regs;
 	u32 mcr;
 	int err;
 
-	io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
-	if (!io)
-		return -EINVAL;
+	regs = devm_platform_ioremap_resource(pdev, 0);
+	if (IS_ERR(regs))
+		return PTR_ERR(regs);
 
 	ddev = devm_kzalloc(&pdev->dev, sizeof(*ddev), GFP_KERNEL);
 	if (!ddev)
 		return -ENOMEM;
 
-	if (!devm_request_mem_region(&pdev->dev, io->start, resource_size(io),
-				     dev_name(&pdev->dev)))
-		return -EBUSY;
-
-	ddev->regs = devm_ioremap(&pdev->dev, io->start, resource_size(io));
-	if (!ddev->regs)
-		return -ENOMEM;
+	ddev->regs = regs;
 	ddev->have_64bit_regs = pdata->have_64bit_regs;
 	if (__is_dmac64(ddev))
 		ddev->descsize = sizeof(struct txx9dmac_hwdesc);
--

2.55.0

maintainer Applied
#3 lore raw
In reply to Rosen Penev
Quoted 6 lines · Rosen Penev

Replace the open-coded platform_get_resource() plus devm_request_mem_region() and devm_ioremap() sequence with a single devm_platform_ioremap_resource() call, which folds the resource lookup, region reservation and mapping into one step and returns an ERR_PTR on failure, checked with IS_ERR() and propagated via PTR_ERR().

Quoted 8 lines

This is behaviorally equivalent: the driver already reserved the region with devm_request_mem_region(), so the non-overlapping reg requirement of devm_platform_ioremap_resource() was already satisfied. The txx9dmac platform device (arch/mips/txx9/generic/setup.c) provides a single IORESOURCE_MEM window per DMAC instance, and the child txx9dmac-chan devices carry only IRQ resources, so no region conflict is introduced.

[...]

Applied, thanks!

[1/1] dmaengine: txx9dmac: use devm_platform_ioremap_resource()

      commit: e99f446c47318f4489eb3a675d53d4047951a6d0

Best regards,

Signature · 1 line
~Vinod