[PATCH 1/3] gpiolib: don't use gpiochip_get_direction() when registering a chip

Bartosz Golaszewski posted 3 patches 11 months, 2 weeks ago
There is a newer version of this series
[PATCH 1/3] gpiolib: don't use gpiochip_get_direction() when registering a chip
Posted by Bartosz Golaszewski 11 months, 2 weeks ago
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

During chip registration we should neither check the return value of
gc->get_direction() nor hold the SRCU lock when calling it. The former
is because pin controllers may have pins set to alternate functions and
return errors from their get_direction() callbacks. That's alright - we
should default to the safe INPUT state and not bail-out. The latter is
not needed because we haven't registered the chip yet so there's nothing
to protect against dynamic removal. In fact: we currently hit a lockdep
splat. Revert to calling the gc->get_direction() callback directly not
not checking its value.

Fixes: 9d846b1aebbe ("gpiolib: check the return value of gpio_chip::get_direction()")
Fixes: e623c4303ed1 ("gpiolib: sanitize the return value of gpio_chip::get_direction()")
Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
Closes: https://lore.kernel.org/all/81f890fc-6688-42f0-9756-567efc8bb97a@samsung.com/
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 drivers/gpio/gpiolib.c | 20 ++++----------------
 1 file changed, 4 insertions(+), 16 deletions(-)

diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
index e8678a6c82ea..31d400b10167 100644
--- a/drivers/gpio/gpiolib.c
+++ b/drivers/gpio/gpiolib.c
@@ -1082,24 +1082,12 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
 
 		desc->gdev = gdev;
 
-		if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) {
-			ret = gpiochip_get_direction(gc, desc_index);
-			if (ret < 0)
-				/*
-				 * FIXME: Bail-out here once all GPIO drivers
-				 * are updated to not return errors in
-				 * situations that can be considered normal
-				 * operation.
-				 */
-				dev_warn(&gdev->dev,
-					 "%s: get_direction failed: %d\n",
-					 __func__, ret);
-
-			assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
-		} else {
+		if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index))
+			assign_bit(FLAG_IS_OUT, &desc->flags,
+				   !gc->get_direction(gc, desc_index));
+		else
 			assign_bit(FLAG_IS_OUT,
 				   &desc->flags, !gc->direction_input);
-		}
 	}
 
 	ret = of_gpiochip_add(gc);

-- 
2.45.2
Re: [PATCH 1/3] gpiolib: don't use gpiochip_get_direction() when registering a chip
Posted by Marek Szyprowski 11 months, 2 weeks ago
On 25.02.2025 12:56, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> During chip registration we should neither check the return value of
> gc->get_direction() nor hold the SRCU lock when calling it. The former
> is because pin controllers may have pins set to alternate functions and
> return errors from their get_direction() callbacks. That's alright - we
> should default to the safe INPUT state and not bail-out. The latter is
> not needed because we haven't registered the chip yet so there's nothing
> to protect against dynamic removal. In fact: we currently hit a lockdep
> splat. Revert to calling the gc->get_direction() callback directly not
> not checking its value.
>
> Fixes: 9d846b1aebbe ("gpiolib: check the return value of gpio_chip::get_direction()")
> Fixes: e623c4303ed1 ("gpiolib: sanitize the return value of gpio_chip::get_direction()")
> Reported-by: Marek Szyprowski <m.szyprowski@samsung.com>
> Closes: https://lore.kernel.org/all/81f890fc-6688-42f0-9756-567efc8bb97a@samsung.com/
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tested-by: Marek Szyprowski <m.szyprowski@samsung.com>
> ---
>   drivers/gpio/gpiolib.c | 20 ++++----------------
>   1 file changed, 4 insertions(+), 16 deletions(-)
>
> diff --git a/drivers/gpio/gpiolib.c b/drivers/gpio/gpiolib.c
> index e8678a6c82ea..31d400b10167 100644
> --- a/drivers/gpio/gpiolib.c
> +++ b/drivers/gpio/gpiolib.c
> @@ -1082,24 +1082,12 @@ int gpiochip_add_data_with_key(struct gpio_chip *gc, void *data,
>   
>   		desc->gdev = gdev;
>   
> -		if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index)) {
> -			ret = gpiochip_get_direction(gc, desc_index);
> -			if (ret < 0)
> -				/*
> -				 * FIXME: Bail-out here once all GPIO drivers
> -				 * are updated to not return errors in
> -				 * situations that can be considered normal
> -				 * operation.
> -				 */
> -				dev_warn(&gdev->dev,
> -					 "%s: get_direction failed: %d\n",
> -					 __func__, ret);
> -
> -			assign_bit(FLAG_IS_OUT, &desc->flags, !ret);
> -		} else {
> +		if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index))
> +			assign_bit(FLAG_IS_OUT, &desc->flags,
> +				   !gc->get_direction(gc, desc_index));
> +		else
>   			assign_bit(FLAG_IS_OUT,
>   				   &desc->flags, !gc->direction_input);
> -		}
>   	}
>   
>   	ret = of_gpiochip_add(gc);
>
Best regards
-- 
Marek Szyprowski, PhD
Samsung R&D Institute Poland
Re: [PATCH 1/3] gpiolib: don't use gpiochip_get_direction() when registering a chip
Posted by Andy Shevchenko 11 months, 2 weeks ago
On Tue, Feb 25, 2025 at 12:56:23PM +0100, Bartosz Golaszewski wrote:
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> 
> During chip registration we should neither check the return value of
> gc->get_direction() nor hold the SRCU lock when calling it. The former
> is because pin controllers may have pins set to alternate functions and
> return errors from their get_direction() callbacks. That's alright - we
> should default to the safe INPUT state and not bail-out. The latter is
> not needed because we haven't registered the chip yet so there's nothing
> to protect against dynamic removal. In fact: we currently hit a lockdep
> splat. Revert to calling the gc->get_direction() callback directly not
> not checking its value.

...

I think the below code deserves a commit (as a summary of the above commit
message).

> +		if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index))
> +			assign_bit(FLAG_IS_OUT, &desc->flags,
> +				   !gc->get_direction(gc, desc_index));
> +		else
>  			assign_bit(FLAG_IS_OUT,
>  				   &desc->flags, !gc->direction_input);

Otherwise LGTM,
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH 1/3] gpiolib: don't use gpiochip_get_direction() when registering a chip
Posted by Bartosz Golaszewski 11 months, 2 weeks ago
On Tue, Feb 25, 2025 at 2:22 PM Andy Shevchenko
<andriy.shevchenko@linux.intel.com> wrote:
>
> On Tue, Feb 25, 2025 at 12:56:23PM +0100, Bartosz Golaszewski wrote:
> > From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> >
> > During chip registration we should neither check the return value of
> > gc->get_direction() nor hold the SRCU lock when calling it. The former
> > is because pin controllers may have pins set to alternate functions and
> > return errors from their get_direction() callbacks. That's alright - we
> > should default to the safe INPUT state and not bail-out. The latter is
> > not needed because we haven't registered the chip yet so there's nothing
> > to protect against dynamic removal. In fact: we currently hit a lockdep
> > splat. Revert to calling the gc->get_direction() callback directly not
> > not checking its value.
>
> ...
>
> I think the below code deserves a commit (as a summary of the above commit
> message).
>

Can you rephrase? I'm not getting this one.

Bart

> > +             if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index))
> > +                     assign_bit(FLAG_IS_OUT, &desc->flags,
> > +                                !gc->get_direction(gc, desc_index));
> > +             else
> >                       assign_bit(FLAG_IS_OUT,
> >                                  &desc->flags, !gc->direction_input);
>
> Otherwise LGTM,
> Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
>
> --
> With Best Regards,
> Andy Shevchenko
>
>
Re: [PATCH 1/3] gpiolib: don't use gpiochip_get_direction() when registering a chip
Posted by Andy Shevchenko 11 months, 2 weeks ago
On Tue, Feb 25, 2025 at 03:43:29PM +0100, Bartosz Golaszewski wrote:
> On Tue, Feb 25, 2025 at 2:22 PM Andy Shevchenko
> <andriy.shevchenko@linux.intel.com> wrote:
> > On Tue, Feb 25, 2025 at 12:56:23PM +0100, Bartosz Golaszewski wrote:

> > > During chip registration we should neither check the return value of
> > > gc->get_direction() nor hold the SRCU lock when calling it. The former
> > > is because pin controllers may have pins set to alternate functions and
> > > return errors from their get_direction() callbacks. That's alright - we
> > > should default to the safe INPUT state and not bail-out. The latter is
> > > not needed because we haven't registered the chip yet so there's nothing
> > > to protect against dynamic removal. In fact: we currently hit a lockdep
> > > splat. Revert to calling the gc->get_direction() callback directly not
> > > not checking its value.

...

> > I think the below code deserves a commit (as a summary of the above commit
> > message).
> 
> Can you rephrase? I'm not getting this one.

Ah, s/commit/comment/

> > > +             if (gc->get_direction && gpiochip_line_is_valid(gc, desc_index))
> > > +                     assign_bit(FLAG_IS_OUT, &desc->flags,
> > > +                                !gc->get_direction(gc, desc_index));
> > > +             else
> > >                       assign_bit(FLAG_IS_OUT,
> > >                                  &desc->flags, !gc->direction_input);
> >
> > Otherwise LGTM,
> > Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

-- 
With Best Regards,
Andy Shevchenko