Linux GPIO subsystem development
 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: mpsse: return -ENODEV when the USB interface is gone
Date: Sat, 05 Dec 2026 16:58:39 +0000	[thread overview]
Message-ID: <sashiko-outbox-251750@kernel.org> (raw)
In-Reply-To: <20261205164613.3860-1-25031212351@stu.xidian.edu.cn>

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

      reply	other threads:[~2026-12-05 16:58 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 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-251750@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox