linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] clk: imx: clk-imx8mn: fix memory leak in imx8mn_clocks_probe
@ 2023-04-07  1:44 Hao Luo
  2023-04-07  2:43 ` Peng Fan
  0 siblings, 1 reply; 4+ messages in thread
From: Hao Luo @ 2023-04-07  1:44 UTC (permalink / raw)
  To: Abel Vesa, Peng Fan, Michael Turquette, Stephen Boyd, Shawn Guo,
	Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam,
	NXP Linux Team
  Cc: hust-os-kernel-patches, Hao Luo, linux-clk, linux-arm-kernel,
	linux-kernel

Use devm_platform_ioremap_resource() 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().

Signed-off-by: Hao Luo <m202171776@hust.edu.cn>
---
 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..b054412f6373 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_platform_ioremap_resource(pdev, 0);
 	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


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* RE: [PATCH] clk: imx: clk-imx8mn: fix memory leak in imx8mn_clocks_probe
  2023-04-07  1:44 Hao Luo
@ 2023-04-07  2:43 ` Peng Fan
  0 siblings, 0 replies; 4+ messages in thread
From: Peng Fan @ 2023-04-07  2:43 UTC (permalink / raw)
  To: Hao Luo, Abel Vesa, Michael Turquette, Stephen Boyd, Shawn Guo,
	Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam,
	dl-linux-imx
  Cc: hust-os-kernel-patches@googlegroups.com,
	linux-clk@vger.kernel.org, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org

> Subject: [PATCH] clk: imx: clk-imx8mn: fix memory leak in
> imx8mn_clocks_probe
> 
> Use devm_platform_ioremap_resource() 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().
> 
> Signed-off-by: Hao Luo <m202171776@hust.edu.cn>
> ---
>  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..b054412f6373 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_platform_ioremap_resource(pdev, 0);

This is wrong. Here is to map anatop register space, not ccm register space.

Regards,
Peng.

>  	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


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH] clk: imx: clk-imx8mn: fix memory leak in imx8mn_clocks_probe
@ 2023-04-11  1:51 Hao Luo
  2023-04-12  9:07 ` Peng Fan
  0 siblings, 1 reply; 4+ messages in thread
From: Hao Luo @ 2023-04-11  1:51 UTC (permalink / raw)
  To: Abel Vesa, Peng Fan, Michael Turquette, Stephen Boyd, Shawn Guo,
	Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam,
	NXP Linux Team, Anson Huang
  Cc: hust-os-kernel-patches, Hao Luo, Dongliang Mu, linux-clk,
	linux-arm-kernel, linux-kernel

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 <m202171776@hust.edu.cn>
Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>
---
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


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] clk: imx: clk-imx8mn: fix memory leak in imx8mn_clocks_probe
  2023-04-11  1:51 [PATCH] clk: imx: clk-imx8mn: fix memory leak in imx8mn_clocks_probe Hao Luo
@ 2023-04-12  9:07 ` Peng Fan
  0 siblings, 0 replies; 4+ messages in thread
From: Peng Fan @ 2023-04-12  9:07 UTC (permalink / raw)
  To: Hao Luo, Abel Vesa, Peng Fan, Michael Turquette, Stephen Boyd,
	Shawn Guo, Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam,
	NXP Linux Team, Anson Huang
  Cc: hust-os-kernel-patches, Dongliang Mu, linux-clk, linux-arm-kernel,
	linux-kernel



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 <m202171776@hust.edu.cn>
> Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>

LGTM: Reviewed-by: Peng Fan <peng.fan@nxp.com>

> ---
> 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;
>   	}
>   

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-04-12  9:08 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-04-11  1:51 [PATCH] clk: imx: clk-imx8mn: fix memory leak in imx8mn_clocks_probe Hao Luo
2023-04-12  9:07 ` Peng Fan
  -- strict thread matches above, loose matches on Subject: below --
2023-04-07  1:44 Hao Luo
2023-04-07  2:43 ` Peng Fan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).