2023-04-11 02:21:28

by Hao Luo

[permalink] [raw]
Subject: [PATCH] clk: imx: clk-imx8mn: fix memory leak in imx8mn_clocks_probe

Use devm_of_iomap() instead of of_iomap() to automatically handle
the unused ioremap region.

If any error occurs, regions allocated by kzalloc() will leak,
but using devm_kzalloc() instead will automatically free the memory
using devm_kfree().

Fixes: daeb14545514 ("clk: imx: imx8mn: Switch to clk_hw based API")
Fixes: 96d6392b54db ("clk: imx: Add support for i.MX8MN clock driver")
Signed-off-by: Hao Luo <[email protected]>
Reviewed-by: Dongliang Mu <[email protected]>
---
The issue is discovered by static analysis, and the patch is not tested yet.
---
drivers/clk/imx/clk-imx8mn.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/clk/imx/clk-imx8mn.c b/drivers/clk/imx/clk-imx8mn.c
index a042ed3a9d6c..569b2abf4052 100644
--- a/drivers/clk/imx/clk-imx8mn.c
+++ b/drivers/clk/imx/clk-imx8mn.c
@@ -323,7 +323,7 @@ static int imx8mn_clocks_probe(struct platform_device *pdev)
void __iomem *base;
int ret;

- clk_hw_data = kzalloc(struct_size(clk_hw_data, hws,
+ clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws,
IMX8MN_CLK_END), GFP_KERNEL);
if (WARN_ON(!clk_hw_data))
return -ENOMEM;
@@ -340,10 +340,10 @@ static int imx8mn_clocks_probe(struct platform_device *pdev)
hws[IMX8MN_CLK_EXT4] = imx_get_clk_hw_by_name(np, "clk_ext4");

np = of_find_compatible_node(NULL, NULL, "fsl,imx8mn-anatop");
- base = of_iomap(np, 0);
+ base = devm_of_iomap(dev, np, 0, NULL);
of_node_put(np);
- if (WARN_ON(!base)) {
- ret = -ENOMEM;
+ if (WARN_ON(IS_ERR(base))) {
+ ret = PTR_ERR(base);
goto unregister_hws;
}

--
2.34.1


2023-04-12 09:25:20

by Peng Fan (OSS)

[permalink] [raw]
Subject: Re: [PATCH] clk: imx: clk-imx8mn: fix memory leak in imx8mn_clocks_probe



On 4/11/2023 9:51 AM, Hao Luo wrote:
> Use devm_of_iomap() instead of of_iomap() to automatically handle
> the unused ioremap region.
>
> If any error occurs, regions allocated by kzalloc() will leak,
> but using devm_kzalloc() instead will automatically free the memory
> using devm_kfree().
>
> Fixes: daeb14545514 ("clk: imx: imx8mn: Switch to clk_hw based API")
> Fixes: 96d6392b54db ("clk: imx: Add support for i.MX8MN clock driver")
> Signed-off-by: Hao Luo <[email protected]>
> Reviewed-by: Dongliang Mu <[email protected]>

LGTM: Reviewed-by: Peng Fan <[email protected]>

> ---
> The issue is discovered by static analysis, and the patch is not tested yet.

Please add Version change log in future patches.

Regards,
Peng.

> ---
> drivers/clk/imx/clk-imx8mn.c | 8 ++++----
> 1 file changed, 4 insertions(+), 4 deletions(-)
>
> diff --git a/drivers/clk/imx/clk-imx8mn.c b/drivers/clk/imx/clk-imx8mn.c
> index a042ed3a9d6c..569b2abf4052 100644
> --- a/drivers/clk/imx/clk-imx8mn.c
> +++ b/drivers/clk/imx/clk-imx8mn.c
> @@ -323,7 +323,7 @@ static int imx8mn_clocks_probe(struct platform_device *pdev)
> void __iomem *base;
> int ret;
>
> - clk_hw_data = kzalloc(struct_size(clk_hw_data, hws,
> + clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws,
> IMX8MN_CLK_END), GFP_KERNEL);
> if (WARN_ON(!clk_hw_data))
> return -ENOMEM;
> @@ -340,10 +340,10 @@ static int imx8mn_clocks_probe(struct platform_device *pdev)
> hws[IMX8MN_CLK_EXT4] = imx_get_clk_hw_by_name(np, "clk_ext4");
>
> np = of_find_compatible_node(NULL, NULL, "fsl,imx8mn-anatop");
> - base = of_iomap(np, 0);
> + base = devm_of_iomap(dev, np, 0, NULL);
> of_node_put(np);
> - if (WARN_ON(!base)) {
> - ret = -ENOMEM;
> + if (WARN_ON(IS_ERR(base))) {
> + ret = PTR_ERR(base);
> goto unregister_hws;
> }
>

2023-06-12 09:56:45

by Abel Vesa

[permalink] [raw]
Subject: Re: [PATCH] clk: imx: clk-imx8mn: fix memory leak in imx8mn_clocks_probe


On Tue, 11 Apr 2023 09:51:07 +0800, Hao Luo wrote:
> Use devm_of_iomap() instead of of_iomap() to automatically handle
> the unused ioremap region.
>
> If any error occurs, regions allocated by kzalloc() will leak,
> but using devm_kzalloc() instead will automatically free the memory
> using devm_kfree().
>
> [...]

Applied, thanks!

[1/1] clk: imx: clk-imx8mn: fix memory leak in imx8mn_clocks_probe
commit: 188d070de9132667956f5aadd98d2bd87d3eac89

Best regards,
--
Abel Vesa <[email protected]>