public inbox for linux-clk@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] clk: imx: clk-imx8mp: improve error handling in imx8mp_clocks_probe()
@ 2023-05-03  7:06 Yuxing Liu
  2023-05-24  7:53 ` Yuxing Liu
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Yuxing Liu @ 2023-05-03  7:06 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, Yuxing Liu, Dongliang Mu, linux-clk,
	linux-arm-kernel, linux-kernel

Replace of_iomap() and kzalloc() with devm_of_iomap() and devm_kzalloc()
which can automatically release the related memory when the device
or driver is removed or unloaded to avoid potential memory leak.

In this case, iounmap(anatop_base) in line 427,433 are removed
as manual release is not required.

Besides, referring to clk-imx8mq.c, check the return code of
of_clk_add_hw_provider, if it returns negtive, print error info
and unregister hws, which makes the program more robust.

Fixes: 9c140d992676 ("clk: imx: Add support for i.MX8MP clock driver")
Signed-off-by: Yuxing Liu <lyx2022@hust.edu.cn>
Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>
---
This issue is found by static analysis and remains untested.
---
 drivers/clk/imx/clk-imx8mp.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

diff --git a/drivers/clk/imx/clk-imx8mp.c b/drivers/clk/imx/clk-imx8mp.c
index 3253589851ff..de7d2d2176be 100644
--- a/drivers/clk/imx/clk-imx8mp.c
+++ b/drivers/clk/imx/clk-imx8mp.c
@@ -414,25 +414,22 @@ static int imx8mp_clocks_probe(struct platform_device *pdev)
 	struct device *dev = &pdev->dev;
 	struct device_node *np;
 	void __iomem *anatop_base, *ccm_base;
+	int err;
 
 	np = of_find_compatible_node(NULL, NULL, "fsl,imx8mp-anatop");
-	anatop_base = of_iomap(np, 0);
+	anatop_base = devm_of_iomap(dev, np, 0, NULL);
 	of_node_put(np);
-	if (WARN_ON(!anatop_base))
-		return -ENOMEM;
+	if (WARN_ON(IS_ERR(anatop_base)))
+		return PTR_ERR(anatop_base);
 
 	np = dev->of_node;
 	ccm_base = devm_platform_ioremap_resource(pdev, 0);
-	if (WARN_ON(IS_ERR(ccm_base))) {
-		iounmap(anatop_base);
+	if (WARN_ON(IS_ERR(ccm_base)))
 		return PTR_ERR(ccm_base);
-	}
 
-	clk_hw_data = kzalloc(struct_size(clk_hw_data, hws, IMX8MP_CLK_END), GFP_KERNEL);
-	if (WARN_ON(!clk_hw_data)) {
-		iounmap(anatop_base);
+	clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws, IMX8MP_CLK_END), GFP_KERNEL);
+	if (WARN_ON(!clk_hw_data))
 		return -ENOMEM;
-	}
 
 	clk_hw_data->num = IMX8MP_CLK_END;
 	hws = clk_hw_data->hws;
@@ -721,7 +718,12 @@ static int imx8mp_clocks_probe(struct platform_device *pdev)
 
 	imx_check_clk_hws(hws, IMX8MP_CLK_END);
 
-	of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_hw_data);
+	err = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_hw_data);
+	if (err < 0) {
+		dev_err(dev, "failed to register hws for i.MX8MP\n");
+		imx_unregister_hw_clocks(hws, IMX8MP_CLK_END);
+		return err;
+	}
 
 	imx_register_uart_clocks();
 
-- 
2.34.1


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

* Re: [PATCH] clk: imx: clk-imx8mp: improve error handling in imx8mp_clocks_probe()
  2023-05-03  7:06 [PATCH] clk: imx: clk-imx8mp: improve error handling in imx8mp_clocks_probe() Yuxing Liu
@ 2023-05-24  7:53 ` Yuxing Liu
  2023-06-12  8:27 ` Abel Vesa
  2023-06-12  9:32 ` Abel Vesa
  2 siblings, 0 replies; 4+ messages in thread
From: Yuxing Liu @ 2023-05-24  7:53 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, Dongliang Mu, linux-clk, linux-arm-kernel,
	linux-kernel

ping?

在 2023/5/3 15:06, Yuxing Liu 写道:
> Replace of_iomap() and kzalloc() with devm_of_iomap() and devm_kzalloc()
> which can automatically release the related memory when the device
> or driver is removed or unloaded to avoid potential memory leak.
>
> In this case, iounmap(anatop_base) in line 427,433 are removed
> as manual release is not required.
>
> Besides, referring to clk-imx8mq.c, check the return code of
> of_clk_add_hw_provider, if it returns negtive, print error info
> and unregister hws, which makes the program more robust.
>
> Fixes: 9c140d992676 ("clk: imx: Add support for i.MX8MP clock driver")
> Signed-off-by: Yuxing Liu <lyx2022@hust.edu.cn>
> Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>
> ---
> This issue is found by static analysis and remains untested.
> ---
>   drivers/clk/imx/clk-imx8mp.c | 24 +++++++++++++-----------
>   1 file changed, 13 insertions(+), 11 deletions(-)
>
> diff --git a/drivers/clk/imx/clk-imx8mp.c b/drivers/clk/imx/clk-imx8mp.c
> index 3253589851ff..de7d2d2176be 100644
> --- a/drivers/clk/imx/clk-imx8mp.c
> +++ b/drivers/clk/imx/clk-imx8mp.c
> @@ -414,25 +414,22 @@ static int imx8mp_clocks_probe(struct platform_device *pdev)
>   	struct device *dev = &pdev->dev;
>   	struct device_node *np;
>   	void __iomem *anatop_base, *ccm_base;
> +	int err;
>   
>   	np = of_find_compatible_node(NULL, NULL, "fsl,imx8mp-anatop");
> -	anatop_base = of_iomap(np, 0);
> +	anatop_base = devm_of_iomap(dev, np, 0, NULL);
>   	of_node_put(np);
> -	if (WARN_ON(!anatop_base))
> -		return -ENOMEM;
> +	if (WARN_ON(IS_ERR(anatop_base)))
> +		return PTR_ERR(anatop_base);
>   
>   	np = dev->of_node;
>   	ccm_base = devm_platform_ioremap_resource(pdev, 0);
> -	if (WARN_ON(IS_ERR(ccm_base))) {
> -		iounmap(anatop_base);
> +	if (WARN_ON(IS_ERR(ccm_base)))
>   		return PTR_ERR(ccm_base);
> -	}
>   
> -	clk_hw_data = kzalloc(struct_size(clk_hw_data, hws, IMX8MP_CLK_END), GFP_KERNEL);
> -	if (WARN_ON(!clk_hw_data)) {
> -		iounmap(anatop_base);
> +	clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws, IMX8MP_CLK_END), GFP_KERNEL);
> +	if (WARN_ON(!clk_hw_data))
>   		return -ENOMEM;
> -	}
>   
>   	clk_hw_data->num = IMX8MP_CLK_END;
>   	hws = clk_hw_data->hws;
> @@ -721,7 +718,12 @@ static int imx8mp_clocks_probe(struct platform_device *pdev)
>   
>   	imx_check_clk_hws(hws, IMX8MP_CLK_END);
>   
> -	of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_hw_data);
> +	err = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_hw_data);
> +	if (err < 0) {
> +		dev_err(dev, "failed to register hws for i.MX8MP\n");
> +		imx_unregister_hw_clocks(hws, IMX8MP_CLK_END);
> +		return err;
> +	}
>   
>   	imx_register_uart_clocks();
>   

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

* Re: [PATCH] clk: imx: clk-imx8mp: improve error handling in imx8mp_clocks_probe()
  2023-05-03  7:06 [PATCH] clk: imx: clk-imx8mp: improve error handling in imx8mp_clocks_probe() Yuxing Liu
  2023-05-24  7:53 ` Yuxing Liu
@ 2023-06-12  8:27 ` Abel Vesa
  2023-06-12  9:32 ` Abel Vesa
  2 siblings, 0 replies; 4+ messages in thread
From: Abel Vesa @ 2023-06-12  8:27 UTC (permalink / raw)
  To: Yuxing Liu
  Cc: Abel Vesa, Peng Fan, Michael Turquette, Stephen Boyd, Shawn Guo,
	Sascha Hauer, Pengutronix Kernel Team, Fabio Estevam,
	NXP Linux Team, Anson Huang, hust-os-kernel-patches, Dongliang Mu,
	linux-clk, linux-arm-kernel, linux-kernel

On 23-05-03 07:06:07, Yuxing Liu wrote:
> Replace of_iomap() and kzalloc() with devm_of_iomap() and devm_kzalloc()
> which can automatically release the related memory when the device
> or driver is removed or unloaded to avoid potential memory leak.
> 
> In this case, iounmap(anatop_base) in line 427,433 are removed
> as manual release is not required.
> 
> Besides, referring to clk-imx8mq.c, check the return code of
> of_clk_add_hw_provider, if it returns negtive, print error info
> and unregister hws, which makes the program more robust.
> 
> Fixes: 9c140d992676 ("clk: imx: Add support for i.MX8MP clock driver")
> Signed-off-by: Yuxing Liu <lyx2022@hust.edu.cn>
> Reviewed-by: Dongliang Mu <dzm91@hust.edu.cn>

Reviewed-by: Abel Vesa <abel.vesa@linaro.org>

> ---
> This issue is found by static analysis and remains untested.
> ---
>  drivers/clk/imx/clk-imx8mp.c | 24 +++++++++++++-----------
>  1 file changed, 13 insertions(+), 11 deletions(-)
> 
> diff --git a/drivers/clk/imx/clk-imx8mp.c b/drivers/clk/imx/clk-imx8mp.c
> index 3253589851ff..de7d2d2176be 100644
> --- a/drivers/clk/imx/clk-imx8mp.c
> +++ b/drivers/clk/imx/clk-imx8mp.c
> @@ -414,25 +414,22 @@ static int imx8mp_clocks_probe(struct platform_device *pdev)
>  	struct device *dev = &pdev->dev;
>  	struct device_node *np;
>  	void __iomem *anatop_base, *ccm_base;
> +	int err;
>  
>  	np = of_find_compatible_node(NULL, NULL, "fsl,imx8mp-anatop");
> -	anatop_base = of_iomap(np, 0);
> +	anatop_base = devm_of_iomap(dev, np, 0, NULL);
>  	of_node_put(np);
> -	if (WARN_ON(!anatop_base))
> -		return -ENOMEM;
> +	if (WARN_ON(IS_ERR(anatop_base)))
> +		return PTR_ERR(anatop_base);
>  
>  	np = dev->of_node;
>  	ccm_base = devm_platform_ioremap_resource(pdev, 0);
> -	if (WARN_ON(IS_ERR(ccm_base))) {
> -		iounmap(anatop_base);
> +	if (WARN_ON(IS_ERR(ccm_base)))
>  		return PTR_ERR(ccm_base);
> -	}
>  
> -	clk_hw_data = kzalloc(struct_size(clk_hw_data, hws, IMX8MP_CLK_END), GFP_KERNEL);
> -	if (WARN_ON(!clk_hw_data)) {
> -		iounmap(anatop_base);
> +	clk_hw_data = devm_kzalloc(dev, struct_size(clk_hw_data, hws, IMX8MP_CLK_END), GFP_KERNEL);
> +	if (WARN_ON(!clk_hw_data))
>  		return -ENOMEM;
> -	}
>  
>  	clk_hw_data->num = IMX8MP_CLK_END;
>  	hws = clk_hw_data->hws;
> @@ -721,7 +718,12 @@ static int imx8mp_clocks_probe(struct platform_device *pdev)
>  
>  	imx_check_clk_hws(hws, IMX8MP_CLK_END);
>  
> -	of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_hw_data);
> +	err = of_clk_add_hw_provider(np, of_clk_hw_onecell_get, clk_hw_data);
> +	if (err < 0) {
> +		dev_err(dev, "failed to register hws for i.MX8MP\n");
> +		imx_unregister_hw_clocks(hws, IMX8MP_CLK_END);
> +		return err;
> +	}
>  
>  	imx_register_uart_clocks();
>  
> -- 
> 2.34.1
> 

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

* Re: [PATCH] clk: imx: clk-imx8mp: improve error handling in imx8mp_clocks_probe()
  2023-05-03  7:06 [PATCH] clk: imx: clk-imx8mp: improve error handling in imx8mp_clocks_probe() Yuxing Liu
  2023-05-24  7:53 ` Yuxing Liu
  2023-06-12  8:27 ` Abel Vesa
@ 2023-06-12  9:32 ` Abel Vesa
  2 siblings, 0 replies; 4+ messages in thread
From: Abel Vesa @ 2023-06-12  9:32 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, Yuxing Liu
  Cc: hust-os-kernel-patches, Dongliang Mu, linux-clk, linux-arm-kernel,
	linux-kernel


On Wed, 03 May 2023 07:06:07 +0000, Yuxing Liu wrote:
> Replace of_iomap() and kzalloc() with devm_of_iomap() and devm_kzalloc()
> which can automatically release the related memory when the device
> or driver is removed or unloaded to avoid potential memory leak.
> 
> In this case, iounmap(anatop_base) in line 427,433 are removed
> as manual release is not required.
> 
> [...]

Applied, thanks!

[1/1] clk: imx: clk-imx8mp: improve error handling in imx8mp_clocks_probe()
      commit: 878b02d5f3b56cb090dbe2c70c89273be144087f

Best regards,
-- 
Abel Vesa <abel.vesa@linaro.org>

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

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

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-05-03  7:06 [PATCH] clk: imx: clk-imx8mp: improve error handling in imx8mp_clocks_probe() Yuxing Liu
2023-05-24  7:53 ` Yuxing Liu
2023-06-12  8:27 ` Abel Vesa
2023-06-12  9:32 ` Abel Vesa

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox