[PATCH v1 4/7] gpiolib: Call validate_desc() when VALIDATE_DESC() can't be used

Andy Shevchenko posted 7 patches 8 months, 1 week ago
There is a newer version of this series
[PATCH v1 4/7] gpiolib: Call validate_desc() when VALIDATE_DESC() can't be used
Posted by Andy Shevchenko 8 months, 1 week ago
Call validate_desc() directly when VALIDATE_DESC() can't be used.
It will print additional information useful for debugging.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/gpio/gpiolib.c | 25 +++++++++----------------
 1 file changed, 9 insertions(+), 16 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index 1fdf4d2ceb36..6b307144e41a 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -421,11 +421,8 @@ int gpiod_get_direction(struct gpio_desc *desc)
 	unsigned int offset;
 	int ret;
 
-	/*
-	 * We cannot use VALIDATE_DESC() as we must not return 0 for a NULL
-	 * descriptor like we usually do.
-	 */
-	if (IS_ERR_OR_NULL(desc))
+	ret = validate_desc(desc, __func__);
+	if (ret <= 0)
 		return -EINVAL;
 
 	CLASS(gpio_chip_guard, guard)(desc);
@@ -3992,13 +3989,10 @@ int gpiod_to_irq(const struct gpio_desc *desc)
 	struct gpio_device *gdev;
 	struct gpio_chip *gc;
 	int offset;
+	int ret;
 
-	/*
-	 * Cannot VALIDATE_DESC() here as gpiod_to_irq() consumer semantics
-	 * requires this function to not return zero on an invalid descriptor
-	 * but rather a negative error number.
-	 */
-	if (IS_ERR_OR_NULL(desc))
+	ret = validate_desc(desc, __func__);
+	if (ret <= 0)
 		return -EINVAL;
 
 	gdev = desc->gdev;
@@ -4010,13 +4004,12 @@ int gpiod_to_irq(const struct gpio_desc *desc)
 
 	offset = gpio_chip_hwgpio(desc);
 	if (gc->to_irq) {
-		int retirq = gc->to_irq(gc, offset);
+		ret = gc->to_irq(gc, offset);
+		if (ret)
+			return ret;
 
 		/* Zero means NO_IRQ */
-		if (!retirq)
-			return -ENXIO;
-
-		return retirq;
+		return -ENXIO;
 	}
 #ifdef CONFIG_GPIOLIB_IRQCHIP
 	if (gc->irq.chip) {
-- 
2.47.2
Re: [PATCH v1 4/7] gpiolib: Call validate_desc() when VALIDATE_DESC() can't be used
Posted by Linus Walleij 8 months, 1 week ago
On Tue, Apr 15, 2025 at 1:11 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:

> Call validate_desc() directly when VALIDATE_DESC() can't be used.
> It will print additional information useful for debugging.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

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

>         offset = gpio_chip_hwgpio(desc);
>         if (gc->to_irq) {
> -               int retirq = gc->to_irq(gc, offset);
> +               ret = gc->to_irq(gc, offset);
> +               if (ret)
> +                       return ret;
>
>                 /* Zero means NO_IRQ */
> -               if (!retirq)
> -                       return -ENXIO;
> -
> -               return retirq;
> +               return -ENXIO;

Totally unrelated change - but I'm fine with that :D

Yours,
Linus Walleij
Re: [PATCH v1 4/7] gpiolib: Call validate_desc() when VALIDATE_DESC() can't be used
Posted by Andy Shevchenko 8 months, 1 week ago
On Wed, Apr 16, 2025 at 10:44:18AM +0200, Linus Walleij wrote:
> On Tue, Apr 15, 2025 at 1:11 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> 
> > Call validate_desc() directly when VALIDATE_DESC() can't be used.
> > It will print additional information useful for debugging.
> >
> > Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> 
> Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Thank you!

...

> >         if (gc->to_irq) {
> > -               int retirq = gc->to_irq(gc, offset);
> > +               ret = gc->to_irq(gc, offset);
> > +               if (ret)
> > +                       return ret;
> >
> >                 /* Zero means NO_IRQ */
> > -               if (!retirq)
> > -                       return -ENXIO;
> > -
> > -               return retirq;
> > +               return -ENXIO;
> 
> Totally unrelated change - but I'm fine with that :D

It's not totally (it's a reuse of the same variable), but I can split it.
Bart, what do you prefer?

-- 
With Best Regards,
Andy Shevchenko


Re: [PATCH v1 4/7] gpiolib: Call validate_desc() when VALIDATE_DESC() can't be used
Posted by Andy Shevchenko 8 months, 1 week ago
On Wed, Apr 16, 2025 at 12:17:11PM +0300, Andy Shevchenko wrote:
> On Wed, Apr 16, 2025 at 10:44:18AM +0200, Linus Walleij wrote:
> > On Tue, Apr 15, 2025 at 1:11 PM Andy Shevchenko
> > <andriy.shevchenko@linux.intel.com> wrote:

...

> > >         if (gc->to_irq) {
> > > -               int retirq = gc->to_irq(gc, offset);
> > > +               ret = gc->to_irq(gc, offset);
> > > +               if (ret)
> > > +                       return ret;
> > >
> > >                 /* Zero means NO_IRQ */
> > > -               if (!retirq)
> > > -                       return -ENXIO;
> > > -
> > > -               return retirq;
> > > +               return -ENXIO;
> > 
> > Totally unrelated change - but I'm fine with that :D
> 
> It's not totally (it's a reuse of the same variable), but I can split it.
> Bart, what do you prefer?
Never mind, I will send a v2 soon.

Linus, I will leave your Rb tag in a split patch as it seems you've reviewed it
as a whole already.

-- 
With Best Regards,
Andy Shevchenko