usb: gadget: u_serial: Fix race condition in TTY wakeup
authorKuen-Han Tsai <khtsai@google.com>
Tue, 17 Jun 2025 05:07:12 +0000 (13:07 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 19 Jun 2025 10:41:13 +0000 (12:41 +0200)
A race condition occurs when gs_start_io() calls either gs_start_rx() or
gs_start_tx(), as those functions briefly drop the port_lock for
usb_ep_queue(). This allows gs_close() and gserial_disconnect() to clear
port.tty and port_usb, respectively.

Use the null-safe TTY Port helper function to wake up TTY.

Example
  CPU1:       CPU2:
  gserial_connect() // lock
         gs_close() // await lock
  gs_start_rx()     // unlock
  usb_ep_queue()
         gs_close() // lock, reset port.tty and unlock
  gs_start_rx()     // lock
  tty_wakeup()      // NPE

Fixes: 35f95fd7f234 ("TTY: usb/u_serial, use tty from tty_port")
Cc: stable <stable@kernel.org>
Signed-off-by: Kuen-Han Tsai <khtsai@google.com>
Reviewed-by: Prashanth K <prashanth.k@oss.qualcomm.com>
Link: https://lore.kernel.org/linux-usb/20240116141801.396398-1-khtsai@google.com/
Link: https://lore.kernel.org/r/20250617050844.1848232-2-khtsai@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/usb/gadget/function/u_serial.c

index c043bdc30d8a5bf33055e9d2c950da8392b07c94..540dc5ab96fc86bff5af24b15a4933747f7f0fae 100644 (file)
@@ -295,8 +295,8 @@ __acquires(&port->port_lock)
                        break;
        }
 
-       if (do_tty_wake && port->port.tty)
-               tty_wakeup(port->port.tty);
+       if (do_tty_wake)
+               tty_port_tty_wakeup(&port->port);
        return status;
 }
 
@@ -574,7 +574,7 @@ static int gs_start_io(struct gs_port *port)
                gs_start_tx(port);
                /* Unblock any pending writes into our circular buffer, in case
                 * we didn't in gs_start_tx() */
-               tty_wakeup(port->port.tty);
+               tty_port_tty_wakeup(&port->port);
        } else {
                /* Free reqs only if we are still connected */
                if (port->port_usb) {