All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] gpiolib: devres: shrink devm_gpiochip_add_data_with_key()
@ 2020-11-10  9:39 Bartosz Golaszewski
  2020-11-11 14:56 ` Linus Walleij
  2020-11-11 15:02 ` Andy Shevchenko
  0 siblings, 2 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2020-11-10  9:39 UTC (permalink / raw)
  To: Linus Walleij; +Cc: linux-gpio, linux-kernel, Bartosz Golaszewski

From: Bartosz Golaszewski <bgolaszewski@baylibre.com>

If all we want to manage is a single pointer, there's no need to
manually allocate and add a new devres. We can simply use
devm_add_action_or_reset() and shrink the code by a good bit.

Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>
---
 drivers/gpio/gpiolib-devres.c | 19 ++++---------------
 1 file changed, 4 insertions(+), 15 deletions(-)

diff --git a/drivers/gpio/gpiolib-devres.c b/drivers/gpio/gpiolib-devres.c
index 7dbce4c4ebdf..0a292dd3e2a6 100644
--- a/drivers/gpio/gpiolib-devres.c
+++ b/drivers/gpio/gpiolib-devres.c
@@ -479,9 +479,9 @@ void devm_gpio_free(struct device *dev, unsigned int gpio)
 }
 EXPORT_SYMBOL_GPL(devm_gpio_free);
 
-static void devm_gpio_chip_release(struct device *dev, void *res)
+static void devm_gpio_chip_release(void *data)
 {
-	struct gpio_chip *gc = *(struct gpio_chip **)res;
+	struct gpio_chip *gc = data;
 
 	gpiochip_remove(gc);
 }
@@ -507,23 +507,12 @@ int devm_gpiochip_add_data_with_key(struct device *dev, struct gpio_chip *gc, vo
 				    struct lock_class_key *lock_key,
 				    struct lock_class_key *request_key)
 {
-	struct gpio_chip **ptr;
 	int ret;
 
-	ptr = devres_alloc(devm_gpio_chip_release, sizeof(*ptr),
-			     GFP_KERNEL);
-	if (!ptr)
-		return -ENOMEM;
-
 	ret = gpiochip_add_data_with_key(gc, data, lock_key, request_key);
-	if (ret < 0) {
-		devres_free(ptr);
+	if (ret < 0)
 		return ret;
-	}
 
-	*ptr = gc;
-	devres_add(dev, ptr);
-
-	return 0;
+	return devm_add_action_or_reset(dev, devm_gpio_chip_release, gc);
 }
 EXPORT_SYMBOL_GPL(devm_gpiochip_add_data_with_key);
-- 
2.29.1


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

* Re: [PATCH] gpiolib: devres: shrink devm_gpiochip_add_data_with_key()
  2020-11-10  9:39 [PATCH] gpiolib: devres: shrink devm_gpiochip_add_data_with_key() Bartosz Golaszewski
@ 2020-11-11 14:56 ` Linus Walleij
  2020-11-11 15:02 ` Andy Shevchenko
  1 sibling, 0 replies; 5+ messages in thread
From: Linus Walleij @ 2020-11-11 14:56 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: open list:GPIO SUBSYSTEM, linux-kernel, Bartosz Golaszewski

On Tue, Nov 10, 2020 at 10:39 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:

> From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
>
> If all we want to manage is a single pointer, there's no need to
> manually allocate and add a new devres. We can simply use
> devm_add_action_or_reset() and shrink the code by a good bit.
>
> Signed-off-by: Bartosz Golaszewski <bgolaszewski@baylibre.com>

Sweet!
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

I suppose I will get this with a pull request.

Yours,
Linus Walleij

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

* Re: [PATCH] gpiolib: devres: shrink devm_gpiochip_add_data_with_key()
  2020-11-10  9:39 [PATCH] gpiolib: devres: shrink devm_gpiochip_add_data_with_key() Bartosz Golaszewski
  2020-11-11 14:56 ` Linus Walleij
@ 2020-11-11 15:02 ` Andy Shevchenko
  2020-11-11 15:05   ` Andy Shevchenko
  1 sibling, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2020-11-11 15:02 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Linus Walleij, open list:GPIO SUBSYSTEM,
	Linux Kernel Mailing List, Bartosz Golaszewski

On Tue, Nov 10, 2020 at 11:42 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
>
> If all we want to manage is a single pointer, there's no need to
> manually allocate and add a new devres. We can simply use
> devm_add_action_or_reset() and shrink the code by a good bit.

Yes, it is possible to convert all one-function-based devm_*()
wrappers to use this approach.

The problem is, it will call the release() function on error which is
new (and probably undesired) behaviour.
I suppose you meant devm_add_action() here.


-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH] gpiolib: devres: shrink devm_gpiochip_add_data_with_key()
  2020-11-11 15:02 ` Andy Shevchenko
@ 2020-11-11 15:05   ` Andy Shevchenko
  2020-11-12  8:58     ` Bartosz Golaszewski
  0 siblings, 1 reply; 5+ messages in thread
From: Andy Shevchenko @ 2020-11-11 15:05 UTC (permalink / raw)
  To: Bartosz Golaszewski
  Cc: Linus Walleij, open list:GPIO SUBSYSTEM,
	Linux Kernel Mailing List, Bartosz Golaszewski

On Wed, Nov 11, 2020 at 5:02 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Tue, Nov 10, 2020 at 11:42 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> >
> > From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> >
> > If all we want to manage is a single pointer, there's no need to
> > manually allocate and add a new devres. We can simply use
> > devm_add_action_or_reset() and shrink the code by a good bit.
>
> Yes, it is possible to convert all one-function-based devm_*()
> wrappers to use this approach.
>
> The problem is, it will call the release() function on error which is
> new (and probably undesired) behaviour.
> I suppose you meant devm_add_action() here.

Ah, now it seems I got it. You need to release the chip in case if
devm_add_action() fail.
Dunno if devm_add_action() can somehow change the logic to be clearer here...

-- 
With Best Regards,
Andy Shevchenko

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

* Re: [PATCH] gpiolib: devres: shrink devm_gpiochip_add_data_with_key()
  2020-11-11 15:05   ` Andy Shevchenko
@ 2020-11-12  8:58     ` Bartosz Golaszewski
  0 siblings, 0 replies; 5+ messages in thread
From: Bartosz Golaszewski @ 2020-11-12  8:58 UTC (permalink / raw)
  To: Andy Shevchenko
  Cc: Linus Walleij, open list:GPIO SUBSYSTEM,
	Linux Kernel Mailing List, Bartosz Golaszewski

On Wed, Nov 11, 2020 at 4:04 PM Andy Shevchenko
<andy.shevchenko@gmail.com> wrote:
>
> On Wed, Nov 11, 2020 at 5:02 PM Andy Shevchenko
> <andy.shevchenko@gmail.com> wrote:
> >
> > On Tue, Nov 10, 2020 at 11:42 AM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
> > >
> > > From: Bartosz Golaszewski <bgolaszewski@baylibre.com>
> > >
> > > If all we want to manage is a single pointer, there's no need to
> > > manually allocate and add a new devres. We can simply use
> > > devm_add_action_or_reset() and shrink the code by a good bit.
> >
> > Yes, it is possible to convert all one-function-based devm_*()
> > wrappers to use this approach.
> >
> > The problem is, it will call the release() function on error which is
> > new (and probably undesired) behaviour.
> > I suppose you meant devm_add_action() here.
>
> Ah, now it seems I got it. You need to release the chip in case if
> devm_add_action() fail.
> Dunno if devm_add_action() can somehow change the logic to be clearer here...
>

devm_add_action_or_reset() is correct here - it undos the previous
chip registration on error.

Bartosz

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

end of thread, other threads:[~2020-11-12  8:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-10  9:39 [PATCH] gpiolib: devres: shrink devm_gpiochip_add_data_with_key() Bartosz Golaszewski
2020-11-11 14:56 ` Linus Walleij
2020-11-11 15:02 ` Andy Shevchenko
2020-11-11 15:05   ` Andy Shevchenko
2020-11-12  8:58     ` Bartosz Golaszewski

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.