Linux-ARM-Kernel Archive on lore.kernel.org
 help / color / mirror / Atom feed
* Re: [PATCH 00/18] leds: switch to device_for_each_child_node_scoped()
       [not found] <20240927-leds_device_for_each_child_node_scoped-v1-0-95c0614b38c8@gmail.com>
@ 2024-11-23 19:29 ` Andy Shevchenko
       [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-5-95c0614b38c8@gmail.com>
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-11-23 19:29 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Pavel Machek, Lee Jones, Matthias Brugger,
	AngeloGioacchino Del Regno, Gene Chen, Jacek Anaszewski,
	Bartosz Golaszewski, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
	Jonathan Cameron, linux-leds, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-sunxi, stable

Fri, Sep 27, 2024 at 01:20:51AM +0200, Javier Carrasco kirjoitti:
> This series switches from the device_for_each_child_node() macro to its
> scoped variant, which in general makes the code more robust if new early
> exits are added to the loops, because there is no need for explicit
> calls to fwnode_handle_put(). Depending on the complexity of the loop
> and its error handling, the code gets simplified and it gets easier to
> follow.
> 
> The non-scoped variant of the macro is error-prone, and it has been the
> source of multiple bugs where the child node refcount was not
> decremented accordingly in error paths within the loops. The first patch
> of this series is a good example, which fixes that kind of bug that is
> regularly found in node iterators.
> 
> The uses of device_for_each_child_node() with no early exits have been
> left untouched because their simpilicty justifies the non-scoped
> variant.
> 
> Note that the child node is now declared in the macro, and therefore the
> explicit declaration is no longer required.
> 
> The general functionality should not be affected by this modification.
> If functional changes are found, please report them back as errors.


Thank you for this series. It may now benefit from the next steps:

1) converting to return dev_err_probe() or dev_warn_probe() directly since
   the goto had been replaced by direct return, hence saving even more LoCs;

2) dropping or refactoring the complex conditions due to your nice series
   being applied.

I'll comment individually on some to show what I meant by the above.

-- 
With Best Regards,
Andy Shevchenko




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

* Re: [PATCH 05/18] leds: cr0014114: switch to device_for_each_child_node_scoped()
       [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-5-95c0614b38c8@gmail.com>
@ 2024-11-23 19:31   ` Andy Shevchenko
  0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-11-23 19:31 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Pavel Machek, Lee Jones, Matthias Brugger,
	AngeloGioacchino Del Regno, Gene Chen, Jacek Anaszewski,
	Bartosz Golaszewski, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
	Jonathan Cameron, linux-leds, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-sunxi

Fri, Sep 27, 2024 at 01:20:56AM +0200, Javier Carrasco kirjoitti:
> Switch to device_for_each_child_node_scoped() to simplify the code by
> removing the need for calls to fwnode_handle_put() in the error paths.
> 
> This also prevents possible memory leaks if new error paths are added
> without the required call to fwnode_handle_put().

...

>  		if (ret) {
>  			dev_err(priv->dev,
>  				"failed to register LED device, err %d", ret);
> -			fwnode_handle_put(child);
>  			return ret;

Now it can be
			return dev_err_probe(..., ret, "failed to register LED device");

>  		}

-- 
With Best Regards,
Andy Shevchenko




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

* Re: [PATCH 06/18] leds: el15203000: switch to device_for_each_child_node_scoped()
       [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-6-95c0614b38c8@gmail.com>
@ 2024-11-23 19:34   ` Andy Shevchenko
  0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-11-23 19:34 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Pavel Machek, Lee Jones, Matthias Brugger,
	AngeloGioacchino Del Regno, Gene Chen, Jacek Anaszewski,
	Bartosz Golaszewski, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
	Jonathan Cameron, linux-leds, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-sunxi

Fri, Sep 27, 2024 at 01:20:57AM +0200, Javier Carrasco kirjoitti:
> Switch to device_for_each_child_node_scoped() to simplify the code by
> removing the need for calls to fwnode_handle_put() in the error paths.
> 
> This also prevents possible memory leaks if new error paths are added
> without the required call to fwnode_handle_put().
> 
> After switching to the scoped variant, there is no longer need for a
> jump to 'err_child_out', as an immediate return is possible.

...

>  		if (ret) {
>  			dev_err(priv->dev, "LED without ID number");
> -			goto err_child_out;
> +			return ret;

Now

			return dev_err_probe(...);

>  		}
>  
>  		if (led->reg > U8_MAX) {
>  			dev_err(priv->dev, "LED value %d is invalid", led->reg);
> -			ret = -EINVAL;
> -			goto err_child_out;
> +			return -EINVAL;

Ditto.

>  		}

>  			dev_err(priv->dev,
>  				"failed to register LED device %s, err %d",
>  				led->ldev.name, ret);
> -			goto err_child_out;
> +			return ret;

Ditto.

			return dev_err_probe(priv->dev, ret, "failed to register LED device %s\n",
				led->ldev.name);

...

Also notice missed '\n' at the end of the strings (and yes, I know
that it's not a problem for dev_*() macros, but still...).

-- 
With Best Regards,
Andy Shevchenko




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

* Re: [PATCH 07/18] leds: gpio: switch to device_for_each_child_node_scoped()
       [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-7-95c0614b38c8@gmail.com>
@ 2024-11-23 19:35   ` Andy Shevchenko
  0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-11-23 19:35 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Pavel Machek, Lee Jones, Matthias Brugger,
	AngeloGioacchino Del Regno, Gene Chen, Jacek Anaszewski,
	Bartosz Golaszewski, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
	Jonathan Cameron, linux-leds, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-sunxi

Fri, Sep 27, 2024 at 01:20:58AM +0200, Javier Carrasco kirjoitti:
> Switch to device_for_each_child_node_scoped() to simplify the code by
> removing the need for calls to fwnode_handle_put() in the error paths.
> 
> This also prevents possible memory leaks if new error paths are added
> without the required call to fwnode_handle_put().

...

>  		if (IS_ERR(led.gpiod)) {
>  			dev_err_probe(dev, PTR_ERR(led.gpiod), "Failed to get GPIO '%pfw'\n",
>  				      child);
> -			fwnode_handle_put(child);
>  			return ERR_CAST(led.gpiod);

Here you may use dev_err_ptr_probe()

>  		}

-- 
With Best Regards,
Andy Shevchenko




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

* Re: [PATCH 08/18] leds: lm3532: switch to device_for_each_child_node_scoped()
       [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-8-95c0614b38c8@gmail.com>
@ 2024-11-23 19:37   ` Andy Shevchenko
  0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-11-23 19:37 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Pavel Machek, Lee Jones, Matthias Brugger,
	AngeloGioacchino Del Regno, Gene Chen, Jacek Anaszewski,
	Bartosz Golaszewski, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
	Jonathan Cameron, linux-leds, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-sunxi

Fri, Sep 27, 2024 at 01:20:59AM +0200, Javier Carrasco kirjoitti:
> Switch to device_for_each_child_node_scoped() to simplify the code by
> removing the need for calls to fwnode_handle_put() in the error paths.
> 
> This also prevents possible memory leaks if new error paths are added
> without the required call to fwnode_handle_put().
> 
> After switching to the scoped variant, there is no longer need for a
> jump to 'child_out', as an immediate return is possible.

...

>  		if (ret) {
>  			dev_err(&priv->client->dev, "reg property missing\n");
> -			goto child_out;
> +			return ret;

return dev_err_probe(...);

>  		}

...

>  		if (ret) {
>  			dev_err(&priv->client->dev, "ti,led-mode property missing\n");
> -			goto child_out;
> +			return ret;
>  		}

...

>  		if (ret) {
>  			dev_err(&priv->client->dev, "led-sources property missing\n");
> -			goto child_out;
> +			return ret;
>  		}

...

>  		if (ret) {
>  			dev_err(&priv->client->dev, "led register err: %d\n",
>  				ret);
> -			goto child_out;
> +			return ret;
>  		}
>  
>  		ret = lm3532_init_registers(led);
>  		if (ret) {
>  			dev_err(&priv->client->dev, "register init err: %d\n",
>  				ret);
> -			goto child_out;
> +			return ret;
>  		}

Ditto for all above.

-- 
With Best Regards,
Andy Shevchenko




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

* Re: [PATCH 09/18] leds: lm3697: switch to device_for_each_child_node_scoped()
       [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-9-95c0614b38c8@gmail.com>
@ 2024-11-23 19:37   ` Andy Shevchenko
  0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-11-23 19:37 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Pavel Machek, Lee Jones, Matthias Brugger,
	AngeloGioacchino Del Regno, Gene Chen, Jacek Anaszewski,
	Bartosz Golaszewski, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
	Jonathan Cameron, linux-leds, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-sunxi

Fri, Sep 27, 2024 at 01:21:00AM +0200, Javier Carrasco kirjoitti:
> Switch to device_for_each_child_node_scoped() to simplify the code by
> removing the need for calls to fwnode_handle_put() in the error paths.
> 
> This also prevents possible memory leaks if new error paths are added
> without the required call to fwnode_handle_put().
> 
> After switching to the scoped variant, there is no longer need for a
> jump to 'child_out', as an immediate return is possible.

Same here and so on...
So, please revisit these drivers for the possible improvements.

-- 
With Best Regards,
Andy Shevchenko




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

* Re: [PATCH 16/18] leds: tca6507: switch to device_for_each_child_node_scoped()
       [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-16-95c0614b38c8@gmail.com>
@ 2024-11-23 19:40   ` Andy Shevchenko
  0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-11-23 19:40 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Pavel Machek, Lee Jones, Matthias Brugger,
	AngeloGioacchino Del Regno, Gene Chen, Jacek Anaszewski,
	Bartosz Golaszewski, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
	Jonathan Cameron, linux-leds, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-sunxi

Fri, Sep 27, 2024 at 01:21:07AM +0200, Javier Carrasco kirjoitti:
> Switch to device_for_each_child_node_scoped() to simplify the code by
> removing the need for calls to fwnode_handle_put() in the error path.
> 
> This also prevents possible memory leaks if new error paths are added
> without the required call to fwnode_handle_put().

...

>  		ret = fwnode_property_read_u32(child, "reg", &reg);
> -		if (ret || reg >= NUM_LEDS) {
> -			fwnode_handle_put(child);
> +		if (ret || reg >= NUM_LEDS)
>  			return ERR_PTR(ret ? : -EINVAL);
> -		}

It's now two nested conditionals instead of two independent ones:

		if (ret)
			return ERR_PTR(ret);
		if (reg >= NUM_LEDS)
			return ERR_PTR(-EINVAL);

I believe my wariant is much better to read, understand, and maintain.

-- 
With Best Regards,
Andy Shevchenko




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

* Re: [PATCH 15/18] leds: sun50i-a100: switch to device_for_each_child_node_scoped()
       [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-15-95c0614b38c8@gmail.com>
@ 2024-11-23 19:41   ` Andy Shevchenko
  0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-11-23 19:41 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Pavel Machek, Lee Jones, Matthias Brugger,
	AngeloGioacchino Del Regno, Gene Chen, Jacek Anaszewski,
	Bartosz Golaszewski, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
	Jonathan Cameron, linux-leds, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-sunxi

Fri, Sep 27, 2024 at 01:21:06AM +0200, Javier Carrasco kirjoitti:
> Switch to device_for_each_child_node_scoped() to simplify the code by
> removing the need for calls to fwnode_handle_put() in the error paths.
> 
> This also prevents possible memory leaks if new error paths are added
> without the required call to fwnode_handle_put().
> 
> The error handling after 'err_put_child' has been moved to the only goto
> that jumps to it (second device_for_each_child_node()), and the call to
> fwnode_handle_put() has been removed accordingly.

...

>  		ret = fwnode_property_read_u32(child, "reg", &addr);
> -		if (ret || addr >= LEDC_MAX_LEDS) {
> -			fwnode_handle_put(child);
> +		if (ret || addr >= LEDC_MAX_LEDS)
>  			return dev_err_probe(dev, -EINVAL, "'reg' must be between 0 and %d\n",
>  					     LEDC_MAX_LEDS - 1);
> -		}

This is a misleading message, what should be done is to split them:

		if (ret)
			reutrn ret;
		if (addr >= LEDC_MAX_LEDS)
			return dev_err_probe(dev, -EINVAL, "'reg' must be between 0 and %d\n",
					     LEDC_MAX_LEDS - 1);


>  		ret = fwnode_property_read_u32(child, "color", &color);
> -		if (ret || color != LED_COLOR_ID_RGB) {
> -			fwnode_handle_put(child);
> +		if (ret || color != LED_COLOR_ID_RGB)
>  			return dev_err_probe(dev, -EINVAL, "'color' must be LED_COLOR_ID_RGB\n");
> -		}

Ditto.

-- 
With Best Regards,
Andy Shevchenko




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

* Re: [PATCH 13/18] leds: pca963x: switch to device_for_each_child_node_scoped()
       [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-13-95c0614b38c8@gmail.com>
@ 2024-11-23 19:45   ` Andy Shevchenko
  0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-11-23 19:45 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Pavel Machek, Lee Jones, Matthias Brugger,
	AngeloGioacchino Del Regno, Gene Chen, Jacek Anaszewski,
	Bartosz Golaszewski, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
	Jonathan Cameron, linux-leds, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-sunxi

Fri, Sep 27, 2024 at 01:21:04AM +0200, Javier Carrasco kirjoitti:
> Switch to device_for_each_child_node_scoped() to simplify the code by
> removing the need for calls to fwnode_handle_put() in the error paths.
> 
> This also prevents possible memory leaks if new error paths are added
> without the required call to fwnode_handle_put().
> 
> After switching to the scoped variant, there is no longer need for a
> jump to 'err', as an immediate return is possible.

...

>  		if (ret || reg >= chipdef->n_leds) {
>  			dev_err(dev, "Invalid 'reg' property for node %pfw\n",
>  				child);
> -			ret = -EINVAL;
> -			goto err;
> +			return -EINVAL;
>  		}

I'm not sure how interpret this message, but the problem here is the shadowing
of the original error code. Hence I would split this:

		if (ret)
			return ret; // possibly return dev_err_probe() with another message
		if (reg >= chipdef->n_leds)
			return dev_err_probe(dev, -EINVAL, "Invalid 'reg' property for node %pfw\n",
					     child);

...

>  		if (ret) {
>  			dev_err(dev, "Failed to register LED for node %pfw\n",
>  				child);
> -			goto err;
> +			return ret;

			return dev_err_probe(...);

>  		}

-- 
With Best Regards,
Andy Shevchenko




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

* Re: [PATCH 11/18] leds: max77650: switch to device_for_each_child_node_scoped()
       [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-11-95c0614b38c8@gmail.com>
@ 2024-11-23 19:47   ` Andy Shevchenko
  0 siblings, 0 replies; 10+ messages in thread
From: Andy Shevchenko @ 2024-11-23 19:47 UTC (permalink / raw)
  To: Javier Carrasco
  Cc: Pavel Machek, Lee Jones, Matthias Brugger,
	AngeloGioacchino Del Regno, Gene Chen, Jacek Anaszewski,
	Bartosz Golaszewski, Chen-Yu Tsai, Jernej Skrabec, Samuel Holland,
	Jonathan Cameron, linux-leds, linux-kernel, linux-arm-kernel,
	linux-mediatek, linux-sunxi

Fri, Sep 27, 2024 at 01:21:02AM +0200, Javier Carrasco kirjoitti:
> Switch to device_for_each_child_node_scoped() to simplify the code by
> removing the need for calls to fwnode_handle_put() in the error paths.
> 
> This also prevents possible memory leaks if new error paths are added
> without the required call to fwnode_handle_put().
> 
> After switching to the scoped variant, there is no longer need for a
> jump to 'err_node_out', as an immediate return is possible.

...

>  		rv = fwnode_property_read_u32(child, "reg", &reg);
> -		if (rv || reg >= MAX77650_LED_NUM_LEDS) {
> -			rv = -EINVAL;
> -			goto err_node_put;
> -		}
> +		if (rv || reg >= MAX77650_LED_NUM_LEDS)
> +			return -EINVAL;

Again (yes, I know that is original issue, but with your series applied
it may be nicely resolved), shadowing error code. Should be

		if (rv)
			return rv;
		if (reg >= MAX77650_LED_NUM_LEDS)
			return -EINVAL;

-- 
With Best Regards,
Andy Shevchenko




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

end of thread, other threads:[~2024-11-23 19:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20240927-leds_device_for_each_child_node_scoped-v1-0-95c0614b38c8@gmail.com>
2024-11-23 19:29 ` [PATCH 00/18] leds: switch to device_for_each_child_node_scoped() Andy Shevchenko
     [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-5-95c0614b38c8@gmail.com>
2024-11-23 19:31   ` [PATCH 05/18] leds: cr0014114: " Andy Shevchenko
     [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-6-95c0614b38c8@gmail.com>
2024-11-23 19:34   ` [PATCH 06/18] leds: el15203000: " Andy Shevchenko
     [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-7-95c0614b38c8@gmail.com>
2024-11-23 19:35   ` [PATCH 07/18] leds: gpio: " Andy Shevchenko
     [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-8-95c0614b38c8@gmail.com>
2024-11-23 19:37   ` [PATCH 08/18] leds: lm3532: " Andy Shevchenko
     [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-9-95c0614b38c8@gmail.com>
2024-11-23 19:37   ` [PATCH 09/18] leds: lm3697: " Andy Shevchenko
     [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-16-95c0614b38c8@gmail.com>
2024-11-23 19:40   ` [PATCH 16/18] leds: tca6507: " Andy Shevchenko
     [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-15-95c0614b38c8@gmail.com>
2024-11-23 19:41   ` [PATCH 15/18] leds: sun50i-a100: " Andy Shevchenko
     [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-13-95c0614b38c8@gmail.com>
2024-11-23 19:45   ` [PATCH 13/18] leds: pca963x: " Andy Shevchenko
     [not found] ` <20240927-leds_device_for_each_child_node_scoped-v1-11-95c0614b38c8@gmail.com>
2024-11-23 19:47   ` [PATCH 11/18] leds: max77650: " Andy Shevchenko

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