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. The checks on their own are not enough: a callback that has just read a non-NULL priv->intf can still submit a transfer after disconnect() returns. Take io_mutex, which the callbacks already hold around all bulk I/O, while clearing priv->intf and the interface drvdata, so an in-flight transfer completes first and later callbacks observe the cleared state. Fixes: c46a74ff05c0 ("gpio: add support for FTDI's MPSSE as GPIO") Cc: stable@vger.kernel.org Co-developed-by: Bin Yu Signed-off-by: Bin Yu Co-developed-by: Cheng Wen Signed-off-by: Cheng Wen Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn> --- drivers/gpio/gpio-mpsse.c | 33 ++++++++++++++++++++++++++++----- 1 file changed, 28 insertions(+), 5 deletions(-) v2: - Take io_mutex around clearing priv->intf and the interface drvdata, so a transfer that already passed the NULL check cannot outlive disconnect(). diff --git a/drivers/gpio/gpio-mpsse.c b/drivers/gpio/gpio-mpsse.c index 61c5ca96314b..8a74dc5de7eb 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; @@ -731,8 +747,15 @@ static void gpio_mpsse_disconnect(struct usb_interface *intf) scoped_guard(mutex, &priv->irq_race) gpio_mpsse_stop_all_except(priv, NULL); - priv->intf = NULL; - usb_set_intfdata(intf, NULL); + /* + * The GPIO callbacks hold io_mutex around all bulk I/O, so taking + * it here waits for a transfer that already passed the NULL check + * and keeps new ones from submitting once the device is going away. + */ + scoped_guard(mutex, &priv->io_mutex) { + priv->intf = NULL; + usb_set_intfdata(intf, NULL); + } } static struct usb_driver gpio_mpsse_driver = { -- 2.34.1