[v3,2/2] gpio: sim: initialize a managed pointer when declaring it

Message ID 20230917091225.6350-3-brgl@bgdev.pl
State New
Headers
Series gpio: sim: improve the usage of __free() |

Commit Message

Bartosz Golaszewski Sept. 17, 2023, 9:12 a.m. UTC
  From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Variables managed with __free() should typically be initialized where
they are declared so that the __free() callback is paired with its
counterpart resource allocator. Fix the second instance of using
__free() in gpio-sim to follow this pattern.

Fixes: 3faf89f27aab ("gpio: sim: simplify code with cleanup helpers")
Suggested-by: Linus Torvalds <torvalds@linux-foundation.org>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/gpio/gpio-sim.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
  

Comments

Andy Shevchenko Sept. 18, 2023, 7:06 a.m. UTC | #1
On Sun, Sep 17, 2023 at 11:12:25AM +0200, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> Variables managed with __free() should typically be initialized where
> they are declared so that the __free() callback is paired with its
> counterpart resource allocator. Fix the second instance of using
> __free() in gpio-sim to follow this pattern.

...

>  {
> -	struct gpio_sim_device *dev __free(kfree) = NULL;
>  	int id;
>  
> -	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
> +	struct gpio_sim_device *dev __free(kfree) = kzalloc(sizeof(*dev),
> +							    GFP_KERNEL);
>  	if (!dev)
>  		return ERR_PTR(-ENOMEM);

Aside: Oh, this might be a downside of the __free() sugar, as we can
theoretically end up with a code in the future like

	struct bar *foo;
	...
	struct baz *foo __free() = ...
	...

and I am not sure how it goes to work. Or relaxed variant with

	struct bar *foo;
	...
	{
		struct baz *foo __free() = ...
		...
	}

where we would have two variables with the same name, but different scope
(this, perhaps, would work, but I assume compiler should warn about shadowed
 name for the variable).

(Also what if in both cases bar == baz, i.e. same type?)
  

Patch

diff --git a/drivers/gpio/gpio-sim.c b/drivers/gpio/gpio-sim.c
index 7d52f7caa1c7..1a8a332a803b 100644
--- a/drivers/gpio/gpio-sim.c
+++ b/drivers/gpio/gpio-sim.c
@@ -1481,10 +1481,10 @@  static const struct config_item_type gpio_sim_device_config_group_type = {
 static struct config_group *
 gpio_sim_config_make_device_group(struct config_group *group, const char *name)
 {
-	struct gpio_sim_device *dev __free(kfree) = NULL;
 	int id;
 
-	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
+	struct gpio_sim_device *dev __free(kfree) = kzalloc(sizeof(*dev),
+							    GFP_KERNEL);
 	if (!dev)
 		return ERR_PTR(-ENOMEM);