[PATCH] pmdomain: imx: Fix reference count leak in imx_gpc_probe()

Wentao Liang posted 1 patch 1 week, 4 days ago
There is a newer version of this series
drivers/pmdomain/imx/gpc.c | 31 +++++++++++++++++++++----------
1 file changed, 21 insertions(+), 10 deletions(-)
[PATCH] pmdomain: imx: Fix reference count leak in imx_gpc_probe()
Posted by Wentao Liang 1 week, 4 days ago
of_get_child_by_name() returns a node pointer with refcount incremented,
we should use of_node_put() on it when not needed anymore. Add missing
of_node_put() calls in imx_gpc_probe() to avoid reference count leak.

This commit fixes the following issues:
1. Add of_node_put(pgc_node) before returning from imx_gpc_probe()
2. Use goto pattern to consolidate error handling

Fixes: 721cabf6c660 ("soc: imx: move PGC handling to a new GPC driver")
Cc: stable@vger.kernel.org
Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
---
 drivers/pmdomain/imx/gpc.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/drivers/pmdomain/imx/gpc.c b/drivers/pmdomain/imx/gpc.c
index f18c7e6e75dd..754c60effc8a 100644
--- a/drivers/pmdomain/imx/gpc.c
+++ b/drivers/pmdomain/imx/gpc.c
@@ -416,8 +416,10 @@ static int imx_gpc_probe(struct platform_device *pdev)
 		return 0;
 
 	base = devm_platform_ioremap_resource(pdev, 0);
-	if (IS_ERR(base))
-		return PTR_ERR(base);
+	if (IS_ERR(base)) {
+		ret = PTR_ERR(base);
+		goto err_free;
+	}
 
 	regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
 					   &imx_gpc_regmap_config);
@@ -425,7 +427,7 @@ static int imx_gpc_probe(struct platform_device *pdev)
 		ret = PTR_ERR(regmap);
 		dev_err(&pdev->dev, "failed to init regmap: %d\n",
 			ret);
-		return ret;
+		goto err_free;
 	}
 
 	/*
@@ -460,29 +462,33 @@ static int imx_gpc_probe(struct platform_device *pdev)
 		int domain_index;
 
 		ipg_clk = devm_clk_get(&pdev->dev, "ipg");
-		if (IS_ERR(ipg_clk))
-			return PTR_ERR(ipg_clk);
+		if (IS_ERR(ipg_clk)) {
+			ret = PTR_ERR(ipg_clk);
+			goto err_free;
+		}
 		ipg_rate_mhz = clk_get_rate(ipg_clk) / 1000000;
 
 		for_each_child_of_node_scoped(pgc_node, np) {
 			ret = of_property_read_u32(np, "reg", &domain_index);
 			if (ret)
-				return ret;
+				goto err_free;
 
 			if (domain_index >= of_id_data->num_domains)
 				continue;
 
 			pd_pdev = platform_device_alloc("imx-pgc-power-domain",
 							domain_index);
-			if (!pd_pdev)
-				return -ENOMEM;
+			if (!pd_pdev) {
+				ret = -ENOMEM;
+				goto err_free;
+			}
 
 			ret = platform_device_add_data(pd_pdev,
 						       &imx_gpc_domains[domain_index],
 						       sizeof(imx_gpc_domains[domain_index]));
 			if (ret) {
 				platform_device_put(pd_pdev);
-				return ret;
+				goto err_free;
 			}
 			domain = pd_pdev->dev.platform_data;
 			domain->regmap = regmap;
@@ -495,12 +501,17 @@ static int imx_gpc_probe(struct platform_device *pdev)
 			ret = platform_device_add(pd_pdev);
 			if (ret) {
 				platform_device_put(pd_pdev);
-				return ret;
+				goto err_free;
 			}
 		}
 	}
 
+	of_node_put(pgc_node);
 	return 0;
+
+err_free:
+	of_node_put(pgc_node);
+	return ret;
 }
 
 static void imx_gpc_remove(struct platform_device *pdev)
-- 
2.34.1
Re: [PATCH] pmdomain: imx: Fix reference count leak in imx_gpc_probe()
Posted by Markus Elfring 1 week, 3 days ago
…
> @@ -495,12 +501,17 @@ static int imx_gpc_probe(struct platform_device *pdev)
…
>  	}
>  
> +	of_node_put(pgc_node);
>  	return 0;
> +

A duplicate statement can be avoided here.

	ret = 0;


> +err_free:
> +	of_node_put(pgc_node);
> +	return ret;
>  }


Regards,
Markus
Re: [PATCH] pmdomain: imx: Fix reference count leak in imx_gpc_probe()
Posted by Frank Li 1 week, 3 days ago
On Mon, Dec 08, 2025 at 09:42:42AM +0000, Wentao Liang wrote:
> of_get_child_by_name() returns a node pointer with refcount incremented,
> we should use of_node_put() on it when not needed anymore. Add missing
> of_node_put() calls in imx_gpc_probe() to avoid reference count leak.
>
> This commit fixes the following issues:
> 1. Add of_node_put(pgc_node) before returning from imx_gpc_probe()
> 2. Use goto pattern to consolidate error handling
>
> Fixes: 721cabf6c660 ("soc: imx: move PGC handling to a new GPC driver")
> Cc: stable@vger.kernel.org
> Signed-off-by: Wentao Liang <vulab@iscas.ac.cn>
> ---
>  drivers/pmdomain/imx/gpc.c | 31 +++++++++++++++++++++----------
>  1 file changed, 21 insertions(+), 10 deletions(-)


Just need one line change

struct device_node *pgc_node __free(pgc_node) pgc_node =
	of_get_child_by_name(pdev->dev.of_node, "pgc");


Frank

>
> diff --git a/drivers/pmdomain/imx/gpc.c b/drivers/pmdomain/imx/gpc.c
> index f18c7e6e75dd..754c60effc8a 100644
> --- a/drivers/pmdomain/imx/gpc.c
> +++ b/drivers/pmdomain/imx/gpc.c
> @@ -416,8 +416,10 @@ static int imx_gpc_probe(struct platform_device *pdev)
>  		return 0;
>
>  	base = devm_platform_ioremap_resource(pdev, 0);
> -	if (IS_ERR(base))
> -		return PTR_ERR(base);
> +	if (IS_ERR(base)) {
> +		ret = PTR_ERR(base);
> +		goto err_free;
> +	}
>
>  	regmap = devm_regmap_init_mmio_clk(&pdev->dev, NULL, base,
>  					   &imx_gpc_regmap_config);
> @@ -425,7 +427,7 @@ static int imx_gpc_probe(struct platform_device *pdev)
>  		ret = PTR_ERR(regmap);
>  		dev_err(&pdev->dev, "failed to init regmap: %d\n",
>  			ret);
> -		return ret;
> +		goto err_free;
>  	}
>
>  	/*
> @@ -460,29 +462,33 @@ static int imx_gpc_probe(struct platform_device *pdev)
>  		int domain_index;
>
>  		ipg_clk = devm_clk_get(&pdev->dev, "ipg");
> -		if (IS_ERR(ipg_clk))
> -			return PTR_ERR(ipg_clk);
> +		if (IS_ERR(ipg_clk)) {
> +			ret = PTR_ERR(ipg_clk);
> +			goto err_free;
> +		}
>  		ipg_rate_mhz = clk_get_rate(ipg_clk) / 1000000;
>
>  		for_each_child_of_node_scoped(pgc_node, np) {
>  			ret = of_property_read_u32(np, "reg", &domain_index);
>  			if (ret)
> -				return ret;
> +				goto err_free;
>
>  			if (domain_index >= of_id_data->num_domains)
>  				continue;
>
>  			pd_pdev = platform_device_alloc("imx-pgc-power-domain",
>  							domain_index);
> -			if (!pd_pdev)
> -				return -ENOMEM;
> +			if (!pd_pdev) {
> +				ret = -ENOMEM;
> +				goto err_free;
> +			}
>
>  			ret = platform_device_add_data(pd_pdev,
>  						       &imx_gpc_domains[domain_index],
>  						       sizeof(imx_gpc_domains[domain_index]));
>  			if (ret) {
>  				platform_device_put(pd_pdev);
> -				return ret;
> +				goto err_free;
>  			}
>  			domain = pd_pdev->dev.platform_data;
>  			domain->regmap = regmap;
> @@ -495,12 +501,17 @@ static int imx_gpc_probe(struct platform_device *pdev)
>  			ret = platform_device_add(pd_pdev);
>  			if (ret) {
>  				platform_device_put(pd_pdev);
> -				return ret;
> +				goto err_free;
>  			}
>  		}
>  	}
>
> +	of_node_put(pgc_node);
>  	return 0;
> +
> +err_free:
> +	of_node_put(pgc_node);
> +	return ret;
>  }
>
>  static void imx_gpc_remove(struct platform_device *pdev)
> --
> 2.34.1
>