Skip to content

Commit 5f1d429

Browse files
ambarusvinodkoul
authored andcommitted
dmaengine: at_hdmac: Use devm_kzalloc() and struct_size()
Use the resource-managed kzalloc to simplify error logic. Memory allocated with this function is automatically freed on driver detach. Use struct_size() helper to calculate the size of the atdma structure with its trailing flexible array. While here, move the mem allocation higher in the probe method, as failing to allocate memory indicates a serious system issue, and everything else does not matter anyway. All these help the code look a bit cleaner. Signed-off-by: Tudor Ambarus <[email protected]> Acked-by: Nicolas Ferre <[email protected]> Link: https://lore.kernel.org/r/[email protected] Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Vinod Koul <[email protected]>
1 parent b50cf4b commit 5f1d429

File tree

1 file changed

+9
-14
lines changed

1 file changed

+9
-14
lines changed

drivers/dma/at_hdmac.c

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <linux/platform_device.h>
2020
#include <linux/slab.h>
2121
#include <linux/of.h>
22+
#include <linux/overflow.h>
2223
#include <linux/of_device.h>
2324
#include <linux/of_dma.h>
2425

@@ -1786,6 +1787,12 @@ static int __init at_dma_probe(struct platform_device *pdev)
17861787
if (!plat_dat)
17871788
return -ENODEV;
17881789

1790+
atdma = devm_kzalloc(&pdev->dev,
1791+
struct_size(atdma, chan, plat_dat->nr_channels),
1792+
GFP_KERNEL);
1793+
if (!atdma)
1794+
return -ENOMEM;
1795+
17891796
io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
17901797
if (!io)
17911798
return -EINVAL;
@@ -1794,21 +1801,13 @@ static int __init at_dma_probe(struct platform_device *pdev)
17941801
if (irq < 0)
17951802
return irq;
17961803

1797-
size = sizeof(struct at_dma);
1798-
size += plat_dat->nr_channels * sizeof(struct at_dma_chan);
1799-
atdma = kzalloc(size, GFP_KERNEL);
1800-
if (!atdma)
1801-
return -ENOMEM;
1802-
18031804
/* discover transaction capabilities */
18041805
atdma->dma_common.cap_mask = plat_dat->cap_mask;
18051806
atdma->all_chan_mask = (1 << plat_dat->nr_channels) - 1;
18061807

18071808
size = resource_size(io);
1808-
if (!request_mem_region(io->start, size, pdev->dev.driver->name)) {
1809-
err = -EBUSY;
1810-
goto err_kfree;
1811-
}
1809+
if (!request_mem_region(io->start, size, pdev->dev.driver->name))
1810+
return -EBUSY;
18121811

18131812
atdma->regs = ioremap(io->start, size);
18141813
if (!atdma->regs) {
@@ -1963,8 +1962,6 @@ static int __init at_dma_probe(struct platform_device *pdev)
19631962
atdma->regs = NULL;
19641963
err_release_r:
19651964
release_mem_region(io->start, size);
1966-
err_kfree:
1967-
kfree(atdma);
19681965
return err;
19691966
}
19701967

@@ -2003,8 +2000,6 @@ static int at_dma_remove(struct platform_device *pdev)
20032000
io = platform_get_resource(pdev, IORESOURCE_MEM, 0);
20042001
release_mem_region(io->start, resource_size(io));
20052002

2006-
kfree(atdma);
2007-
20082003
return 0;
20092004
}
20102005

0 commit comments

Comments
 (0)