Skip to contentSkip to conversation

gpiolib: don't check the retval of get_direction() when registering a chip

PATCH Applied Bartosz Golaszewski 2 participants 3 messages Mar 11, 2025 – Mar 13, 2025 GPIO

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 gpiolib: don't check the retval of get_direction() when registering a chip

From: Bartosz Golaszewski <[email protected]>

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 and *not* checking its value.

---
This commit is in linux-next as 9becde08f1bc ("gpiolib: don't use gpiochip_get_direction() when registering a chip") and was applied as a fix to commits e623c4303ed1 ("gpiolib: sanitize the return value of gpio_chip::get_direction()") and 9d846b1aebbe ("gpiolib: check the return value of gpio_chip::get_direction()"). Becuase the former is queued for v6.15-rc1, this fix was never applied to v6.14 and sent upstream.

However, the warning it addresses is now queued for v6.14. I've rebased this commit on top of v6.14-rc6 and would like to send it upstream. Once merged, I'll pull v6.14-rc7 back into my for-next branch and fix the conflicts.

 drivers/gpio/gpiolib.c | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)
drivers/gpio/gpiolib.c +11 -16
index 8741600af7ef..de708d081858 100644
@@ -1056,24 +1056,19 @@ 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 = gc->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 {
+		/*
+		 * We would typically want to check the return value of
+		 * get_direction() here but we must not check the return value
+		 * and bail-out as pin controllers can have pins configured to
+		 * alternate functions and return -EINVAL. Also: there's no
+		 * need to take the SRCU lock here.
+		 */
+		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);
Signature · 1 line
2.45.2
Tested-by Gene C on v1 1/1
#1 lore raw
Quoted 18 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

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 and
*not* checking its value.

Quoted 83 lines
Fixes 9d846b1aebbe ("gpiolib: check the return value of

gpio_chip::get_direction()")

Closes:
https://lore.kernel.org/all/[email protected]/

Link:
https://lore.kernel.org/r/[email protected]

---
This commit is in linux-next as 9becde08f1bc ("gpiolib: don't use gpiochip_get_direction() when registering a chip") and was applied as a fix to commits e623c4303ed1 ("gpiolib: sanitize the return value of gpio_chip::get_direction()") and 9d846b1aebbe ("gpiolib: check the return
value of gpio_chip::get_direction()"). Becuase the former is queued for
v6.15-rc1, this fix was never applied to v6.14 and sent upstream.

However, the warning it addresses is now queued for v6.14. I've rebased
this commit on top of v6.14-rc6 and would like to send it upstream. Once
merged, I'll pull v6.14-rc7 back into my for-next branch and fix the conflicts.

 drivers/gpio/gpiolib.c | 27 +++++++++++----------------

 1 file changed, 11 insertions(+), 16 deletions(-)
drivers/gpio/gpiolib.c +0 -0
index 8741600af7ef..de708d081858 100644
@@ -1056,24 +1056,19 @@ 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 = gc->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 {
+ /*
+ * We would typically want to check the return value of
+ * get_direction() here but we must not check the return value
+ * and bail-out as pin controllers can have pins configured to
+ * alternate functions and return -EINVAL. Also:
there's no
+ * need to take the SRCU lock here.
+ */
+ 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-

Quoted 1 line

direction_input);

- }
  }

  ret = of_gpiochip_add(gc);

Signature · 1 line
Gene
author Applied
#2 lore raw
In reply to Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

Quoted 11 lines · Bartosz Golaszewski

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 and *not* checking its value.

[...]

Applied, thanks!

[1/1] gpiolib: don't check the retval of get_direction() when registering a chip

      commit: 0102fbf52b93e609fec0dab53b1fb4fe69113f5e

Best regards,

Signature · 1 line
Bartosz Golaszewski <[email protected]>