All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "YuanZhe Liu" <25031212351@stu.xidian.edu.cn>
Cc: linux-gpio@vger.kernel.org
Subject: Re: [PATCH] gpio: viperboard: implement .get_direction()
Date: Sat, 05 Dec 2026 17:03:49 +0000	[thread overview]
Message-ID: <sashiko-outbox-251752@kernel.org> (raw)
In-Reply-To: <20261205165253.4264-1-25031212351@stu.xidian.edu.cn>

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

      reply	other threads:[~2026-12-05 17:03 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05 16:52 [PATCH] gpio: viperboard: implement .get_direction() YuanZhe Liu
2026-12-05 17:03 ` sashiko-bot [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=sashiko-outbox-251752@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=25031212351@stu.xidian.edu.cn \
    --cc=linux-gpio@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.