All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jonathan Cameron <Jonathan.Cameron@Huawei.com>
To: Kousik Sanagavarapu <five231003@gmail.com>
Cc: Nishanth Menon <nm@ti.com>,
	Santosh Shilimkar <ssantosh@kernel.org>,
	Julia Lawall <julia.lawall@inria.fr>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Javier Carrasco <javier.carrasco.cruz@gmail.com>,
	<linux-kernel@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>
Subject: Re: [PATCH 1/3] soc: ti: pruss: do device_node auto cleanup
Date: Thu, 16 May 2024 15:41:31 +0100	[thread overview]
Message-ID: <20240516154131.00007a8f@Huawei.com> (raw)
In-Reply-To: <20240510071432.62913-2-five231003@gmail.com>

On Fri, 10 May 2024 12:43:22 +0530
Kousik Sanagavarapu <five231003@gmail.com> wrote:

> Use scope based cleanup instead of manual of_node_put() calls, hence
> simplifying the handling of error paths at various places.
> 
> Suggested-by: Julia Lawall <julia.lawall@inria.fr>
> Signed-off-by: Kousik Sanagavarapu <five231003@gmail.com>
> ---
>  drivers/soc/ti/pruss.c | 48 ++++++++++++++----------------------------
>  1 file changed, 16 insertions(+), 32 deletions(-)
> 
> diff --git a/drivers/soc/ti/pruss.c b/drivers/soc/ti/pruss.c
> index 24a42e0b645c..8f238d59eca9 100644
> --- a/drivers/soc/ti/pruss.c
> +++ b/drivers/soc/ti/pruss.c
> @@ -381,13 +381,13 @@ static int pruss_clk_mux_setup(struct pruss *pruss, struct clk *clk_mux,
>  static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
>  {
>  	const struct pruss_private_data *data;
> -	struct device_node *clks_np;
>  	struct device *dev = pruss->dev;
>  	int ret = 0;
>  
>  	data = of_device_get_match_data(dev);
>  
> -	clks_np = of_get_child_by_name(cfg_node, "clocks");
> +	struct device_node *clks_np __free(device_node) =
> +			of_get_child_by_name(cfg_node, "clocks");
>  	if (!clks_np) {
>  		dev_err(dev, "%pOF is missing its 'clocks' node\n", cfg_node);
>  		return -ENODEV;
> @@ -398,7 +398,7 @@ static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
>  					  "coreclk-mux", clks_np);
>  		if (ret) {
>  			dev_err(dev, "failed to setup coreclk-mux\n");
> -			goto put_clks_node;
> +			return ret;

This driver would also benefit from use of dev_err_probe() for all cases
like this that are called only from the probe() callback.
			return dev_err_probe(dev, ret, "failed to setup coreclk-mux\n2);

Handles any deferred cases nicely, but in general simplifies code by
making these printer error message and return cases a single line.


>  		}
>  	}
>  
> @@ -406,13 +406,10 @@ static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
>  				  clks_np);
>  	if (ret) {
>  		dev_err(dev, "failed to setup iepclk-mux\n");
> -		goto put_clks_node;
> +		return ret;
>  	}
>  
> -put_clks_node:
> -	of_node_put(clks_np);
> -
> -	return ret;
> +	return 0;
>  }
>  
>  static struct regmap_config regmap_conf = {
> @@ -424,26 +421,22 @@ static struct regmap_config regmap_conf = {
>  static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss)
>  {
>  	struct device_node *np = dev_of_node(dev);
> -	struct device_node *child;
> +	struct device_node *child __free(device_node) =
> +			of_get_child_by_name(np, "cfg");
>  	struct resource res;
>  	int ret;
>  
> -	child = of_get_child_by_name(np, "cfg");
>  	if (!child) {
>  		dev_err(dev, "%pOF is missing its 'cfg' node\n", child);
>  		return -ENODEV;
>  	}
>  
> -	if (of_address_to_resource(child, 0, &res)) {
> -		ret = -ENOMEM;
> -		goto node_put;
> -	}
> +	if (of_address_to_resource(child, 0, &res))
> +		return -ENOMEM;
>  
>  	pruss->cfg_base = devm_ioremap(dev, res.start, resource_size(&res));
> -	if (!pruss->cfg_base) {
> -		ret = -ENOMEM;
> -		goto node_put;
> -	}
> +	if (!pruss->cfg_base)
> +		return -ENOMEM;
>  
>  	regmap_conf.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", child,
>  				     (u64)res.start);
> @@ -455,16 +448,13 @@ static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss)
>  	if (IS_ERR(pruss->cfg_regmap)) {
>  		dev_err(dev, "regmap_init_mmio failed for cfg, ret = %ld\n",
>  			PTR_ERR(pruss->cfg_regmap));
> -		ret = PTR_ERR(pruss->cfg_regmap);
> -		goto node_put;
> +		return PTR_ERR(pruss->cfg_regmap);
>  	}
>  
>  	ret = pruss_clk_init(pruss, child);
>  	if (ret)
>  		dev_err(dev, "pruss_clk_init failed, ret = %d\n", ret);
>  
> -node_put:
> -	of_node_put(child);
>  	return ret;
>  }
>  
> @@ -472,7 +462,6 @@ static int pruss_probe(struct platform_device *pdev)
>  {
>  	struct device *dev = &pdev->dev;
>  	struct device_node *np = dev_of_node(dev);
> -	struct device_node *child;
>  	struct pruss *pruss;
>  	struct resource res;
>  	int ret, i, index;
> @@ -494,7 +483,8 @@ static int pruss_probe(struct platform_device *pdev)
>  	pruss->dev = dev;
>  	mutex_init(&pruss->lock);
>  
> -	child = of_get_child_by_name(np, "memories");
> +	struct device_node *child __free(device_node) =
> +			of_get_child_by_name(np, "memories");

In this case, the scope of the device_node being held is increased quite
a bit.  Whilst that shouldn't be a problem, it is a bit ugly.

Perhaps factor this code out into another function similar to the ones above?

Jonathan


>  	if (!child) {
>  		dev_err(dev, "%pOF is missing its 'memories' node\n", child);
>  		return -ENODEV;
> @@ -510,22 +500,17 @@ static int pruss_probe(struct platform_device *pdev)
>  
>  		index = of_property_match_string(child, "reg-names",
>  						 mem_names[i]);
> -		if (index < 0) {
> -			of_node_put(child);
> +		if (index < 0)
>  			return index;
> -		}
>  
> -		if (of_address_to_resource(child, index, &res)) {
> -			of_node_put(child);
> +		if (of_address_to_resource(child, index, &res))
>  			return -EINVAL;
> -		}
>  
>  		pruss->mem_regions[i].va = devm_ioremap(dev, res.start,
>  							resource_size(&res));
>  		if (!pruss->mem_regions[i].va) {
>  			dev_err(dev, "failed to parse and map memory resource %d %s\n",
>  				i, mem_names[i]);
> -			of_node_put(child);
>  			return -ENOMEM;
>  		}
>  		pruss->mem_regions[i].pa = res.start;
> @@ -535,7 +520,6 @@ static int pruss_probe(struct platform_device *pdev)
>  			mem_names[i], &pruss->mem_regions[i].pa,
>  			pruss->mem_regions[i].size, pruss->mem_regions[i].va);
>  	}
> -	of_node_put(child);
>  
>  	platform_set_drvdata(pdev, pruss);
>  


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

WARNING: multiple messages have this Message-ID (diff)
From: Jonathan Cameron <Jonathan.Cameron@Huawei.com>
To: Kousik Sanagavarapu <five231003@gmail.com>
Cc: Nishanth Menon <nm@ti.com>,
	Santosh Shilimkar <ssantosh@kernel.org>,
	Julia Lawall <julia.lawall@inria.fr>,
	Shuah Khan <skhan@linuxfoundation.org>,
	Javier Carrasco <javier.carrasco.cruz@gmail.com>,
	<linux-kernel@vger.kernel.org>,
	<linux-arm-kernel@lists.infradead.org>
Subject: Re: [PATCH 1/3] soc: ti: pruss: do device_node auto cleanup
Date: Thu, 16 May 2024 15:41:31 +0100	[thread overview]
Message-ID: <20240516154131.00007a8f@Huawei.com> (raw)
In-Reply-To: <20240510071432.62913-2-five231003@gmail.com>

On Fri, 10 May 2024 12:43:22 +0530
Kousik Sanagavarapu <five231003@gmail.com> wrote:

> Use scope based cleanup instead of manual of_node_put() calls, hence
> simplifying the handling of error paths at various places.
> 
> Suggested-by: Julia Lawall <julia.lawall@inria.fr>
> Signed-off-by: Kousik Sanagavarapu <five231003@gmail.com>
> ---
>  drivers/soc/ti/pruss.c | 48 ++++++++++++++----------------------------
>  1 file changed, 16 insertions(+), 32 deletions(-)
> 
> diff --git a/drivers/soc/ti/pruss.c b/drivers/soc/ti/pruss.c
> index 24a42e0b645c..8f238d59eca9 100644
> --- a/drivers/soc/ti/pruss.c
> +++ b/drivers/soc/ti/pruss.c
> @@ -381,13 +381,13 @@ static int pruss_clk_mux_setup(struct pruss *pruss, struct clk *clk_mux,
>  static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
>  {
>  	const struct pruss_private_data *data;
> -	struct device_node *clks_np;
>  	struct device *dev = pruss->dev;
>  	int ret = 0;
>  
>  	data = of_device_get_match_data(dev);
>  
> -	clks_np = of_get_child_by_name(cfg_node, "clocks");
> +	struct device_node *clks_np __free(device_node) =
> +			of_get_child_by_name(cfg_node, "clocks");
>  	if (!clks_np) {
>  		dev_err(dev, "%pOF is missing its 'clocks' node\n", cfg_node);
>  		return -ENODEV;
> @@ -398,7 +398,7 @@ static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
>  					  "coreclk-mux", clks_np);
>  		if (ret) {
>  			dev_err(dev, "failed to setup coreclk-mux\n");
> -			goto put_clks_node;
> +			return ret;

This driver would also benefit from use of dev_err_probe() for all cases
like this that are called only from the probe() callback.
			return dev_err_probe(dev, ret, "failed to setup coreclk-mux\n2);

Handles any deferred cases nicely, but in general simplifies code by
making these printer error message and return cases a single line.


>  		}
>  	}
>  
> @@ -406,13 +406,10 @@ static int pruss_clk_init(struct pruss *pruss, struct device_node *cfg_node)
>  				  clks_np);
>  	if (ret) {
>  		dev_err(dev, "failed to setup iepclk-mux\n");
> -		goto put_clks_node;
> +		return ret;
>  	}
>  
> -put_clks_node:
> -	of_node_put(clks_np);
> -
> -	return ret;
> +	return 0;
>  }
>  
>  static struct regmap_config regmap_conf = {
> @@ -424,26 +421,22 @@ static struct regmap_config regmap_conf = {
>  static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss)
>  {
>  	struct device_node *np = dev_of_node(dev);
> -	struct device_node *child;
> +	struct device_node *child __free(device_node) =
> +			of_get_child_by_name(np, "cfg");
>  	struct resource res;
>  	int ret;
>  
> -	child = of_get_child_by_name(np, "cfg");
>  	if (!child) {
>  		dev_err(dev, "%pOF is missing its 'cfg' node\n", child);
>  		return -ENODEV;
>  	}
>  
> -	if (of_address_to_resource(child, 0, &res)) {
> -		ret = -ENOMEM;
> -		goto node_put;
> -	}
> +	if (of_address_to_resource(child, 0, &res))
> +		return -ENOMEM;
>  
>  	pruss->cfg_base = devm_ioremap(dev, res.start, resource_size(&res));
> -	if (!pruss->cfg_base) {
> -		ret = -ENOMEM;
> -		goto node_put;
> -	}
> +	if (!pruss->cfg_base)
> +		return -ENOMEM;
>  
>  	regmap_conf.name = kasprintf(GFP_KERNEL, "%pOFn@%llx", child,
>  				     (u64)res.start);
> @@ -455,16 +448,13 @@ static int pruss_cfg_of_init(struct device *dev, struct pruss *pruss)
>  	if (IS_ERR(pruss->cfg_regmap)) {
>  		dev_err(dev, "regmap_init_mmio failed for cfg, ret = %ld\n",
>  			PTR_ERR(pruss->cfg_regmap));
> -		ret = PTR_ERR(pruss->cfg_regmap);
> -		goto node_put;
> +		return PTR_ERR(pruss->cfg_regmap);
>  	}
>  
>  	ret = pruss_clk_init(pruss, child);
>  	if (ret)
>  		dev_err(dev, "pruss_clk_init failed, ret = %d\n", ret);
>  
> -node_put:
> -	of_node_put(child);
>  	return ret;
>  }
>  
> @@ -472,7 +462,6 @@ static int pruss_probe(struct platform_device *pdev)
>  {
>  	struct device *dev = &pdev->dev;
>  	struct device_node *np = dev_of_node(dev);
> -	struct device_node *child;
>  	struct pruss *pruss;
>  	struct resource res;
>  	int ret, i, index;
> @@ -494,7 +483,8 @@ static int pruss_probe(struct platform_device *pdev)
>  	pruss->dev = dev;
>  	mutex_init(&pruss->lock);
>  
> -	child = of_get_child_by_name(np, "memories");
> +	struct device_node *child __free(device_node) =
> +			of_get_child_by_name(np, "memories");

In this case, the scope of the device_node being held is increased quite
a bit.  Whilst that shouldn't be a problem, it is a bit ugly.

Perhaps factor this code out into another function similar to the ones above?

Jonathan


>  	if (!child) {
>  		dev_err(dev, "%pOF is missing its 'memories' node\n", child);
>  		return -ENODEV;
> @@ -510,22 +500,17 @@ static int pruss_probe(struct platform_device *pdev)
>  
>  		index = of_property_match_string(child, "reg-names",
>  						 mem_names[i]);
> -		if (index < 0) {
> -			of_node_put(child);
> +		if (index < 0)
>  			return index;
> -		}
>  
> -		if (of_address_to_resource(child, index, &res)) {
> -			of_node_put(child);
> +		if (of_address_to_resource(child, index, &res))
>  			return -EINVAL;
> -		}
>  
>  		pruss->mem_regions[i].va = devm_ioremap(dev, res.start,
>  							resource_size(&res));
>  		if (!pruss->mem_regions[i].va) {
>  			dev_err(dev, "failed to parse and map memory resource %d %s\n",
>  				i, mem_names[i]);
> -			of_node_put(child);
>  			return -ENOMEM;
>  		}
>  		pruss->mem_regions[i].pa = res.start;
> @@ -535,7 +520,6 @@ static int pruss_probe(struct platform_device *pdev)
>  			mem_names[i], &pruss->mem_regions[i].pa,
>  			pruss->mem_regions[i].size, pruss->mem_regions[i].va);
>  	}
> -	of_node_put(child);
>  
>  	platform_set_drvdata(pdev, pruss);
>  


  reply	other threads:[~2024-05-16 14:42 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-10  7:13 [PATCH 0/3] Use scope based cleanup in drivers/soc/ti/ Kousik Sanagavarapu
2024-05-10  7:13 ` Kousik Sanagavarapu
2024-05-10  7:13 ` [PATCH 1/3] soc: ti: pruss: do device_node auto cleanup Kousik Sanagavarapu
2024-05-10  7:13   ` Kousik Sanagavarapu
2024-05-16 14:41   ` Jonathan Cameron [this message]
2024-05-16 14:41     ` Jonathan Cameron
2024-05-10  7:13 ` [PATCH 2/3] soc: ti: knav_qmss_queue: " Kousik Sanagavarapu
2024-05-10  7:13   ` Kousik Sanagavarapu
2024-05-11 10:12   ` kernel test robot
2024-05-11 10:12     ` kernel test robot
2024-05-12 10:26     ` Kousik Sanagavarapu
2024-05-12 10:26       ` Kousik Sanagavarapu
2024-05-12 10:36       ` Julia Lawall
2024-05-12 10:36         ` Julia Lawall
2024-05-13  6:44       ` Nathan Chancellor
2024-05-13  6:44         ` Nathan Chancellor
2024-05-13  7:23         ` Kousik Sanagavarapu
2024-05-13  7:23           ` Kousik Sanagavarapu
2024-05-16 14:48           ` Jonathan Cameron
2024-05-16 14:48             ` Jonathan Cameron
2024-05-10  7:13 ` [PATCH 3/3] soc: ti: pm33xx: " Kousik Sanagavarapu
2024-05-10  7:13   ` Kousik Sanagavarapu
2024-05-11  9:06 ` [PATCH 0/3] Use scope based cleanup in drivers/soc/ti/ Kousik Sanagavarapu
2024-05-11  9:06   ` Kousik Sanagavarapu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240516154131.00007a8f@Huawei.com \
    --to=jonathan.cameron@huawei.com \
    --cc=five231003@gmail.com \
    --cc=javier.carrasco.cruz@gmail.com \
    --cc=julia.lawall@inria.fr \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nm@ti.com \
    --cc=skhan@linuxfoundation.org \
    --cc=ssantosh@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.