diff mbox series

[09/10] gpiolib: use gpiochip_dup_line_label() in for_each helpers

Message ID 20231129142411.76863-10-brgl@bgdev.pl
State New
Headers show
Series gpio/pinctrl: replace gpiochip_is_requested() with a safer interface | expand

Commit Message

Bartosz Golaszewski Nov. 29, 2023, 2:24 p.m. UTC
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Rework for_each_requested_gpio_in_range() to use the new helper to
retrieve a dynamically allocated copy of the descriptor label and free
it at the end of each iteration.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 include/linux/gpio/driver.h | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

Comments

Bartosz Golaszewski Nov. 29, 2023, 2:43 p.m. UTC | #1
On Wed, Nov 29, 2023 at 3:24 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> Rework for_each_requested_gpio_in_range() to use the new helper to
> retrieve a dynamically allocated copy of the descriptor label and free
> it at the end of each iteration.
>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> ---
>  include/linux/gpio/driver.h | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
>
> diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
> index 9796a34e2fee..6405f6d454af 100644
> --- a/include/linux/gpio/driver.h
> +++ b/include/linux/gpio/driver.h
> @@ -543,8 +543,10 @@ char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset);
>   * @label:     label of current GPIO
>   */
>  #define for_each_requested_gpio_in_range(chip, i, base, size, label)                   \
> -       for (i = 0; i < size; i++)                                                      \
> -               if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else
> +       for (i = 0; i < size; i++, kfree(label))                                        \
> +               if ((label = gpiochip_dup_line_label(chip, base + i)) == NULL) {}       \
> +               else if (IS_ERR(label)) {}                                              \
> +               else
>

Ah, cr*p, it will leak if we break out of the loop, why did I think it
was correct?

Any ideas how to handle this one? I was thinking something like:

    for (i = 0, char *p __free(kfree) = label; i < size; i++)

would work but it doesn't.

Bart

>  /* Iterates over all requested GPIO of the given @chip */
>  #define for_each_requested_gpio(chip, i, label)                                                \
> --
> 2.40.1
>
Andy Shevchenko Nov. 29, 2023, 2:52 p.m. UTC | #2
On Wed, Nov 29, 2023 at 03:43:32PM +0100, Bartosz Golaszewski wrote:
> On Wed, Nov 29, 2023 at 3:24 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:

...

> Any ideas how to handle this one? I was thinking something like:
> 
>     for (i = 0, char *p __free(kfree) = label; i < size; i++)
> 
> would work but it doesn't.

Probably you want to ask Peter Z for this.
Bartosz Golaszewski Nov. 29, 2023, 8:55 p.m. UTC | #3
On Wed, Nov 29, 2023 at 3:52 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Wed, Nov 29, 2023 at 03:43:32PM +0100, Bartosz Golaszewski wrote:
> > On Wed, Nov 29, 2023 at 3:24 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> ...
>
> > Any ideas how to handle this one? I was thinking something like:
> >
> >     for (i = 0, char *p __free(kfree) = label; i < size; i++)
> >
> > would work but it doesn't.
>
> Probably you want to ask Peter Z for this.
>

Before I do, I'll give DEFINE_CLASS() a chance as it looks like it
could be the answer looking at how scoped_guard works.

Bart

> --
> With Best Regards,
> Andy Shevchenko
>
>
diff mbox series

Patch

diff --git a/include/linux/gpio/driver.h b/include/linux/gpio/driver.h
index 9796a34e2fee..6405f6d454af 100644
--- a/include/linux/gpio/driver.h
+++ b/include/linux/gpio/driver.h
@@ -543,8 +543,10 @@  char *gpiochip_dup_line_label(struct gpio_chip *gc, unsigned int offset);
  * @label:	label of current GPIO
  */
 #define for_each_requested_gpio_in_range(chip, i, base, size, label)			\
-	for (i = 0; i < size; i++)							\
-		if ((label = gpiochip_is_requested(chip, base + i)) == NULL) {} else
+	for (i = 0; i < size; i++, kfree(label))					\
+		if ((label = gpiochip_dup_line_label(chip, base + i)) == NULL) {}	\
+		else if (IS_ERR(label)) {}						\
+		else
 
 /* Iterates over all requested GPIO of the given @chip */
 #define for_each_requested_gpio(chip, i, label)						\