Linux GPIO subsystem development
 help / color / mirror / Atom feed
* [PATCH] gpio: viperboard: implement .get_direction()
@ 2026-12-05 16:52 YuanZhe Liu
  2026-12-05 17:03 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-12-05 16:52 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski
  Cc: linux-gpio, linux-kernel, stable, Bin Yu, Cheng Wen

The GPIO core warns in gpiochip_get_direction() when a user reads the
direction of an exported line and the chip does not provide the
.get_direction() callback:

  WARNING: CPU: 0 PID: 3996 at drivers/gpio/gpiolib.c:431
  gpiochip_get_direction
  gpiod_get_direction
  direction_show
  dev_attr_show
  sysfs_kf_seq_show

Reading the direction of an exported line is a normal operation, so it
must not emit a warning.

The viperboard driver already tracks the direction of every line in the
gpioa_out and gpiob_out bitmaps and its get() callbacks use them to tell
input from output lines. Report the tracked direction through
.get_direction() for both chips.

Fixes: 471e998c0e31 ("gpiolib: remove redundant callback check")
Cc: stable@vger.kernel.org
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
 drivers/gpio/gpio-viperboard.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)

diff --git a/drivers/gpio/gpio-viperboard.c b/drivers/gpio/gpio-viperboard.c
index b8d6dff5b663..432071a25383 100644
--- a/drivers/gpio/gpio-viperboard.c
+++ b/drivers/gpio/gpio-viperboard.c
@@ -252,6 +252,15 @@ static int vprbrd_gpioa_direction_output(struct gpio_chip *chip,
 	return 0;
 }
 
+static int vprbrd_gpioa_get_direction(struct gpio_chip *chip,
+				      unsigned int offset)
+{
+	struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
+
+	return (gpio->gpioa_out & (1 << offset)) ? GPIO_LINE_DIRECTION_OUT :
+						 GPIO_LINE_DIRECTION_IN;
+}
+
 /* ----- end of gpio a chip ---------------------------------------------- */
 
 /* ----- begin of gipo b chip -------------------------------------------- */
@@ -388,6 +397,15 @@ static int vprbrd_gpiob_direction_output(struct gpio_chip *chip,
 	return vprbrd_gpiob_set(chip, offset, value);
 }
 
+static int vprbrd_gpiob_get_direction(struct gpio_chip *chip,
+				      unsigned int offset)
+{
+	struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
+
+	return (gpio->gpiob_out & (1 << offset)) ? GPIO_LINE_DIRECTION_OUT :
+						 GPIO_LINE_DIRECTION_IN;
+}
+
 /* ----- end of gpio b chip ---------------------------------------------- */
 
 static int vprbrd_gpio_probe(struct platform_device *pdev)
@@ -412,6 +430,7 @@ static int vprbrd_gpio_probe(struct platform_device *pdev)
 	vb_gpio->gpioa.get = vprbrd_gpioa_get;
 	vb_gpio->gpioa.direction_input = vprbrd_gpioa_direction_input;
 	vb_gpio->gpioa.direction_output = vprbrd_gpioa_direction_output;
+	vb_gpio->gpioa.get_direction = vprbrd_gpioa_get_direction;
 
 	ret = devm_gpiochip_add_data(&pdev->dev, &vb_gpio->gpioa, vb_gpio);
 	if (ret < 0)
@@ -428,6 +447,7 @@ static int vprbrd_gpio_probe(struct platform_device *pdev)
 	vb_gpio->gpiob.get = vprbrd_gpiob_get;
 	vb_gpio->gpiob.direction_input = vprbrd_gpiob_direction_input;
 	vb_gpio->gpiob.direction_output = vprbrd_gpiob_direction_output;
+	vb_gpio->gpiob.get_direction = vprbrd_gpiob_get_direction;
 
 	return devm_gpiochip_add_data(&pdev->dev, &vb_gpio->gpiob, vb_gpio);
 }
-- 
2.34.1


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

* Re: [PATCH] gpio: viperboard: implement .get_direction()
  2026-12-05 16:52 [PATCH] gpio: viperboard: implement .get_direction() YuanZhe Liu
@ 2026-12-05 17:03 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 17:03 UTC (permalink / raw)
  To: YuanZhe Liu; +Cc: linux-gpio

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [Medium] gpio: viperboard: single-driver workaround leaves gpiolib
  WARN_ON unfixed
- [Medium] gpio: viperboard: stale direction after USB error corrupts
  GPIOD_FLAG_IS_OUT
- [Medium] gpio: viperboard: unlocked get_direction() reads race with
  direction updates
--

commit 35f11d2ebc7aa7c44e719194cb0daba3cd5528a2
Author: YuanZhe Liu <25031212351@stu.xidian.edu.cn>

gpio: viperboard: implement .get_direction()

This commit adds .get_direction() callbacks for both the gpioa and gpiob
chips in the viperboard GPIO driver using the cached gpioa_out and
gpiob_out bitmaps.

> diff --git a/drivers/gpio/gpio-viperboard.c b/drivers/gpio/gpio-viperboard.c
> index 89087fd48a819..1b83ab0d66af0 100644
> --- a/drivers/gpio/gpio-viperboard.c
> +++ b/drivers/gpio/gpio-viperboard.c
> @@ -252,6 +252,15 @@ static int vprbrd_gpioa_direction_output(struct gpio_chip *chip,
>  	return 0;
>  }
>  
> +static int vprbrd_gpioa_get_direction(struct gpio_chip *chip,
> +				      unsigned int offset)
> +{
> +	struct vprbrd_gpio *gpio = gpiochip_get_data(chip);
> +
> +	return (gpio->gpioa_out & (1 << offset)) ? GPIO_LINE_DIRECTION_OUT :
> +						 GPIO_LINE_DIRECTION_IN;
> +}
> +
>  /* ----- end of gpio a chip ---------------------------------------------- */

[Severity: Medium]
Can vprbrd_gpioa_get_direction() report a stale direction after a USB
transfer error and corrupt GPIOD_FLAG_IS_OUT in gpiolib?

vprbrd_gpioa_direction_input() and vprbrd_gpioa_direction_output() update
gpio->gpioa_out before usb_control_msg() and leave the bit changed if the
transfer fails with -EREMOTEIO.  The same applies to gpio->gpiob_out in
vprbrd_gpiob_direction_input() and vprbrd_gpiob_direction_output() before
vprbrd_gpiob_setdir().

With .get_direction() exposing these bitmaps, a later gpiod_get_direction()
call copies the failed direction into desc->flags, desynchronizing gpiolib
from the hardware.  Updating gpioa_out and gpiob_out only after the USB
direction command succeeds would avoid this.

[Severity: Medium]
Can vprbrd_gpioa_get_direction() race with concurrent direction updates on
gpio->gpioa_out?

gpiolib only holds a shared SRCU read lock across chip callbacks, while
vprbrd_gpioa_direction_input() and vprbrd_gpioa_direction_output() modify
gpio->gpioa_out outside vb->lock (and vprbrd_gpiob_get_direction() has the
same unlocked read on gpio->gpiob_out):

CPU0                                CPU1
vprbrd_gpioa_direction_output()
  gpio->gpioa_out |= (1 << off);
                                    vprbrd_gpioa_get_direction()
                                      gpio->gpioa_out & (1 << off)
                                        <-- data race / uncommitted state
  mutex_lock(&vb->lock)
  usb_control_msg(...)

This causes a data race and lets gpiod_get_direction() propagate an
uncommitted direction into desc->flags before the USB transfer completes.
Since both chips set can_sleep = true, serializing gpioa_out and gpiob_out
accesses under vb->lock alongside the USB transfers would prevent this.

[ ... ]

> @@ -412,6 +430,7 @@ static int vprbrd_gpio_probe(struct platform_device *pdev)
>  	vb_gpio->gpioa.get = vprbrd_gpioa_get;
>  	vb_gpio->gpioa.direction_input = vprbrd_gpioa_direction_input;
>  	vb_gpio->gpioa.direction_output = vprbrd_gpioa_direction_output;
> +	vb_gpio->gpioa.get_direction = vprbrd_gpioa_get_direction;
>  
>  	ret = devm_gpiochip_add_data(&pdev->dev, &vb_gpio->gpioa, vb_gpio);
>  	if (ret < 0)
> @@ -428,6 +447,7 @@ static int vprbrd_gpio_probe(struct platform_device *pdev)
>  	vb_gpio->gpiob.get = vprbrd_gpiob_get;
>  	vb_gpio->gpiob.direction_input = vprbrd_gpiob_direction_input;
>  	vb_gpio->gpiob.direction_output = vprbrd_gpiob_direction_output;
> +	vb_gpio->gpiob.get_direction = vprbrd_gpiob_get_direction;
>  
>  	return devm_gpiochip_add_data(&pdev->dev, &vb_gpio->gpiob, vb_gpio);
>  }

[Severity: Medium]
Should the WARN_ON(!gc->get_direction) regression be fixed in gpiolib
instead of working around it in gpio-viperboard alone?

Commit 471e998c0e31 ("gpiolib: remove redundant callback check") removed
the !guard.gc->get_direction check in gpiod_get_direction() before calling
gpiochip_get_direction(), exposing its WARN_ON(!gc->get_direction) to
normal callers such as sysfs direction_show().

Because get_direction is optional, every other GPIO driver without it (for
example gpio-da9052 and gpio-vx855) still hits the warning.  Returning
-EOPNOTSUPP without WARN_ON in gpiochip_get_direction() fixes the core bug,
leaving this patch standalone without the Fixes: or Cc: stable trailers.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205165253.4264-1-25031212351@stu.xidian.edu.cn?part=1

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

end of thread, other threads:[~2026-12-05 17:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-05 16:52 [PATCH] gpio: viperboard: implement .get_direction() YuanZhe Liu
2026-12-05 17:03 ` sashiko-bot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox