Linux GPIO subsystem development
 help / color / mirror / Atom feed
* [PATCH] gpio: mpsse: return -ENODEV when the USB interface is gone
@ 2026-12-05 16:46 YuanZhe Liu
  2026-12-05 16:58 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: YuanZhe Liu @ 2026-12-05 16:46 UTC (permalink / raw)
  To: Linus Walleij, Bartosz Golaszewski
  Cc: Mary Strodl, linux-gpio, linux-kernel, stable, Bin Yu, Cheng Wen

gpio_mpsse_disconnect() clears priv->intf and the interface drvdata,
but the GPIO callbacks keep dereferencing priv->intf.  A line request
issued while the device is being unplugged reaches
gpio_mpsse_set_bank() after priv->intf has been cleared, and
mpsse_bulk_xfer() calls usb_get_intfdata(NULL), which faults on
&intf->dev.driver_data.

Return -ENODEV when the interface or its drvdata is gone, and check
the same before mpsse_read() dereferences the private data.  This
covers both the direction input and direction output paths.

Fixes: c46a74ff05c0 ("gpio: add support for FTDI's MPSSE as GPIO")
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-mpsse.c | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/drivers/gpio/gpio-mpsse.c b/drivers/gpio/gpio-mpsse.c
index 61c5ca96314b..ae1a97645214 100644
--- a/drivers/gpio/gpio-mpsse.c
+++ b/drivers/gpio/gpio-mpsse.c
@@ -105,11 +105,20 @@ static DEFINE_IDA(gpio_mpsse_ida);
 
 static int mpsse_bulk_xfer(struct usb_interface *intf, struct bulk_desc *desc)
 {
-	struct mpsse_priv *priv = usb_get_intfdata(intf);
-	struct usb_device *udev = priv->udev;
+	struct mpsse_priv *priv;
+	struct usb_device *udev;
 	unsigned int pipe;
 	int ret;
 
+	if (!intf)
+		return -ENODEV;
+
+	priv = usb_get_intfdata(intf);
+	if (!priv)
+		return -ENODEV;
+
+	udev = priv->udev;
+
 	if (desc->tx)
 		pipe = usb_sndbulkpipe(udev, priv->bulk_out->bEndpointAddress);
 	else
@@ -144,7 +153,14 @@ static int mpsse_read(struct usb_interface *intf, u8 *buf, size_t len)
 {
 	int ret;
 	struct bulk_desc desc;
-	struct mpsse_priv *priv = usb_get_intfdata(intf);
+	struct mpsse_priv *priv;
+
+	if (!intf)
+		return -ENODEV;
+
+	priv = usb_get_intfdata(intf);
+	if (!priv)
+		return -ENODEV;
 
 	desc.len_actual = 0;
 	desc.tx = false;
-- 
2.34.1


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

* Re: [PATCH] gpio: mpsse: return -ENODEV when the USB interface is gone
  2026-12-05 16:46 [PATCH] gpio: mpsse: return -ENODEV when the USB interface is gone YuanZhe Liu
@ 2026-12-05 16:58 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-12-05 16:58 UTC (permalink / raw)
  To: YuanZhe Liu; +Cc: linux-gpio

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [High] gpio: mpsse: unsynchronized disconnect checks leave TOCTOU and
  post-disconnect I/O
--

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

gpio: mpsse: return -ENODEV when the USB interface is gone

This commit adds NULL checks for the USB interface and its driver data in
mpsse_bulk_xfer() and mpsse_read() so they return -ENODEV instead of
dereferencing a cleared interface pointer after disconnect.

> diff --git a/drivers/gpio/gpio-mpsse.c b/drivers/gpio/gpio-mpsse.c
> index 61c5ca96314b7..ae1a976452143 100644
> --- a/drivers/gpio/gpio-mpsse.c
> +++ b/drivers/gpio/gpio-mpsse.c
> @@ -105,11 +105,20 @@ static DEFINE_IDA(gpio_mpsse_ida);
>  
>  static int mpsse_bulk_xfer(struct usb_interface *intf, struct bulk_desc *desc)
>  {
> -	struct mpsse_priv *priv = usb_get_intfdata(intf);
> -	struct usb_device *udev = priv->udev;
> +	struct mpsse_priv *priv;
> +	struct usb_device *udev;
>  	unsigned int pipe;
>  	int ret;
>  
> +	if (!intf)
> +		return -ENODEV;
> +
> +	priv = usb_get_intfdata(intf);
> +	if (!priv)
> +		return -ENODEV;
> +
> +	udev = priv->udev;
> +
>  	if (desc->tx)
>  		pipe = usb_sndbulkpipe(udev, priv->bulk_out->bEndpointAddress);
>  	else
> @@ -144,7 +153,14 @@ static int mpsse_read(struct usb_interface *intf, u8 *buf, size_t len)
>  {
>  	int ret;
>  	struct bulk_desc desc;
> -	struct mpsse_priv *priv = usb_get_intfdata(intf);
> +	struct mpsse_priv *priv;
> +
> +	if (!intf)
> +		return -ENODEV;
> +
> +	priv = usb_get_intfdata(intf);
> +	if (!priv)
> +		return -ENODEV;
>  
>  	desc.len_actual = 0;
>  	desc.tx = false;

[Severity: High]
Can these checks in mpsse_bulk_xfer() and mpsse_read() still race with
gpio_mpsse_disconnect() and allow usb_bulk_msg() to run after disconnect
returns?

CPU0                                CPU1
gpio_mpsse_set_bank()
  ...
  mpsse_bulk_xfer(priv->intf, ...)
    if (!intf)  /* non-NULL */
    priv = usb_get_intfdata(intf)
    if (!priv)  /* non-NULL */
                                    usb_unbind_interface()
                                      gpio_mpsse_disconnect()
                                        priv->intf = NULL;
                                        usb_set_intfdata(intf, NULL);
                                      usb_enable_interface(..., false)
    usb_bulk_msg(udev, ...)  <-- I/O on unbound interface

gpio_mpsse_disconnect() clears priv->intf and calls
usb_set_intfdata(intf, NULL) without holding priv->io_mutex, even though
the GPIO callbacks hold priv->io_mutex while reading priv->intf.

In addition, gpio_mpsse_probe() uses devm_gpiochip_add_data(), so
gpiochip_remove() and its synchronize_srcu() barrier only run later in
device_unbind_cleanup() -> devres_release_all(), after
usb_unbind_interface() has already returned.

This violates the usb_bulk_msg() contract that disconnect() wait for
in-flight transfers to complete.  On driver unbind or module unload while
the device stays attached, a racing thread can submit a bulk transfer to
an unbound interface while stalling gpiochip_remove() under device_lock.

Because priv->intf is accessed without locks or READ_ONCE()/WRITE_ONCE(),
inlining these static helpers can also reload priv->intf between the NULL
check and usb_get_intfdata(intf), leaving a NULL pointer dereference.

Callbacks that skip USB I/O, such as gpio_mpsse_get_direction(), also
continue to succeed after disconnect instead of returning -ENODEV.

Registering the chip with gpiochip_add_data() (with priv->gpio.parent set
to dev) and calling gpiochip_remove(&priv->gpio) synchronously in
gpio_mpsse_disconnect() before clearing priv->intf and intfdata would
drain in-flight callbacks and transfers before disconnect returns.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205164613.3860-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 16:58 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:46 [PATCH] gpio: mpsse: return -ENODEV when the USB interface is gone YuanZhe Liu
2026-12-05 16:58 ` sashiko-bot

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