tty: introduce and use tty_port_tty_vhangup() helper
authorJiri Slaby (SUSE) <jirislaby@kernel.org>
Wed, 11 Jun 2025 10:02:47 +0000 (12:02 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 17 Jun 2025 11:42:33 +0000 (13:42 +0200)
This code (tty_get -> vhangup -> tty_put) is repeated on few places.
Introduce a helper similar to tty_port_tty_hangup() (asynchronous) to
handle even vhangup (synchronous).

And use it on those places.

In fact, reuse the tty_port_tty_hangup()'s code and call tty_vhangup()
depending on a new bool parameter.

Signed-off-by: "Jiri Slaby (SUSE)" <jirislaby@kernel.org>
Cc: Karsten Keil <isdn@linux-pingi.de>
Cc: David Lin <dtwlin@gmail.com>
Cc: Johan Hovold <johan@kernel.org>
Cc: Alex Elder <elder@kernel.org>
Cc: Oliver Neukum <oneukum@suse.com>
Cc: Marcel Holtmann <marcel@holtmann.org>
Cc: Johan Hedberg <johan.hedberg@gmail.com>
Cc: Luiz Augusto von Dentz <luiz.dentz@gmail.com>
Reviewed-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com>
Link: https://lore.kernel.org/r/20250611100319.186924-2-jirislaby@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/isdn/capi/capi.c
drivers/staging/greybus/uart.c
drivers/tty/serial/serial_core.c
drivers/tty/tty_port.c
drivers/usb/class/cdc-acm.c
drivers/usb/serial/usb-serial.c
include/linux/tty_port.h
net/bluetooth/rfcomm/tty.c

index 70dee9ad4bae273c71eb16a8cc2b082b22e20300..78e6e7748fb937163642087bd1798e36781deeca 100644 (file)
@@ -306,15 +306,9 @@ static void capincci_alloc_minor(struct capidev *cdev, struct capincci *np)
 static void capincci_free_minor(struct capincci *np)
 {
        struct capiminor *mp = np->minorp;
-       struct tty_struct *tty;
 
        if (mp) {
-               tty = tty_port_tty_get(&mp->port);
-               if (tty) {
-                       tty_vhangup(tty);
-                       tty_kref_put(tty);
-               }
-
+               tty_port_tty_vhangup(&mp->port);
                capiminor_free(mp);
        }
 }
index 308ed1ca99472bb607343c75456585a00b17b633..10df5c37c83ede2d8641439e1923ebdc1f48c20d 100644 (file)
@@ -916,7 +916,6 @@ static void gb_uart_remove(struct gbphy_device *gbphy_dev)
 {
        struct gb_tty *gb_tty = gb_gbphy_get_data(gbphy_dev);
        struct gb_connection *connection = gb_tty->connection;
-       struct tty_struct *tty;
        int ret;
 
        ret = gbphy_runtime_get_sync(gbphy_dev);
@@ -929,11 +928,7 @@ static void gb_uart_remove(struct gbphy_device *gbphy_dev)
        wake_up_all(&gb_tty->wioctl);
        mutex_unlock(&gb_tty->mutex);
 
-       tty = tty_port_tty_get(&gb_tty->port);
-       if (tty) {
-               tty_vhangup(tty);
-               tty_kref_put(tty);
-       }
+       tty_port_tty_vhangup(&gb_tty->port);
 
        gb_connection_disable_rx(connection);
        tty_unregister_device(gb_tty_driver, gb_tty->minor);
index 1f7708a91fc6d7238d0edbebd316811aba59263d..d6485714eb0faae34728c67c1421d927494ec032 100644 (file)
@@ -3209,7 +3209,6 @@ static void serial_core_remove_one_port(struct uart_driver *drv,
        struct uart_state *state = drv->state + uport->line;
        struct tty_port *port = &state->port;
        struct uart_port *uart_port;
-       struct tty_struct *tty;
 
        mutex_lock(&port->mutex);
        uart_port = uart_port_check(state);
@@ -3228,11 +3227,7 @@ static void serial_core_remove_one_port(struct uart_driver *drv,
         */
        tty_port_unregister_device(port, drv->tty_driver, uport->line);
 
-       tty = tty_port_tty_get(port);
-       if (tty) {
-               tty_vhangup(port->tty);
-               tty_kref_put(tty);
-       }
+       tty_port_tty_vhangup(port);
 
        /*
         * If the port is used as a console, unregister it
index 4af1fbf73f51ca6eb321e7bf507a1cde74442b89..903eebdbe12d45f39b345697d6bec879ba5dc6df 100644 (file)
@@ -396,15 +396,19 @@ EXPORT_SYMBOL(tty_port_hangup);
  * @port: tty port
  * @check_clocal: hang only ttys with %CLOCAL unset?
  */
-void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
+void __tty_port_tty_hangup(struct tty_port *port, bool check_clocal, bool async)
 {
        struct tty_struct *tty = tty_port_tty_get(port);
 
-       if (tty && (!check_clocal || !C_CLOCAL(tty)))
-               tty_hangup(tty);
+       if (tty && (!check_clocal || !C_CLOCAL(tty))) {
+               if (async)
+                       tty_hangup(tty);
+               else
+                       tty_vhangup(tty);
+       }
        tty_kref_put(tty);
 }
-EXPORT_SYMBOL_GPL(tty_port_tty_hangup);
+EXPORT_SYMBOL_GPL(__tty_port_tty_hangup);
 
 /**
  * tty_port_tty_wakeup - helper to wake up a tty
index c2ecfa3c83496fcbd97af815c500f2d4044a36ca..f9171fbedf5ca84d0aca8265e8d0ac4a0aadb523 100644 (file)
@@ -1571,7 +1571,6 @@ err_put_port:
 static void acm_disconnect(struct usb_interface *intf)
 {
        struct acm *acm = usb_get_intfdata(intf);
-       struct tty_struct *tty;
        int i;
 
        /* sibling interface is already cleaning up */
@@ -1598,11 +1597,7 @@ static void acm_disconnect(struct usb_interface *intf)
        usb_set_intfdata(acm->data, NULL);
        mutex_unlock(&acm->mutex);
 
-       tty = tty_port_tty_get(&acm->port);
-       if (tty) {
-               tty_vhangup(tty);
-               tty_kref_put(tty);
-       }
+       tty_port_tty_vhangup(&acm->port);
 
        cancel_delayed_work_sync(&acm->dwork);
 
index 7266558d823ac2c5e6c00417aef6ab90bd2cb4dd..c78ff40b1e5f8a61abfeb6fb4603ca621465bbe7 100644 (file)
@@ -1176,7 +1176,6 @@ static void usb_serial_disconnect(struct usb_interface *interface)
        struct usb_serial *serial = usb_get_intfdata(interface);
        struct device *dev = &interface->dev;
        struct usb_serial_port *port;
-       struct tty_struct *tty;
 
        /* sibling interface is cleaning up */
        if (!serial)
@@ -1191,11 +1190,7 @@ static void usb_serial_disconnect(struct usb_interface *interface)
 
        for (i = 0; i < serial->num_ports; ++i) {
                port = serial->port[i];
-               tty = tty_port_tty_get(&port->port);
-               if (tty) {
-                       tty_vhangup(tty);
-                       tty_kref_put(tty);
-               }
+               tty_port_tty_vhangup(&port->port);
                usb_serial_port_poison_urbs(port);
                wake_up_interruptible(&port->port.delta_msr_wait);
                cancel_work_sync(&port->work);
index 08f89a59836621b2c8864c46c4ed8883b3f9045b..021f9a8415c05817304445b5eef4d70f52ffe2ab 100644 (file)
@@ -232,7 +232,7 @@ bool tty_port_carrier_raised(struct tty_port *port);
 void tty_port_raise_dtr_rts(struct tty_port *port);
 void tty_port_lower_dtr_rts(struct tty_port *port);
 void tty_port_hangup(struct tty_port *port);
-void tty_port_tty_hangup(struct tty_port *port, bool check_clocal);
+void __tty_port_tty_hangup(struct tty_port *port, bool check_clocal, bool async);
 void tty_port_tty_wakeup(struct tty_port *port);
 int tty_port_block_til_ready(struct tty_port *port, struct tty_struct *tty,
                struct file *filp);
@@ -251,4 +251,14 @@ static inline int tty_port_users(struct tty_port *port)
        return port->count + port->blocked_open;
 }
 
+static inline void tty_port_tty_hangup(struct tty_port *port, bool check_clocal)
+{
+       __tty_port_tty_hangup(port, check_clocal, true);
+}
+
+static inline void tty_port_tty_vhangup(struct tty_port *port)
+{
+       __tty_port_tty_hangup(port, false, false);
+}
+
 #endif
index 21a5b5535ebceb3f7117f53258676939e13e921b..827dfbe6608569c986c63046a43ec991633bf5f3 100644 (file)
@@ -438,7 +438,6 @@ static int __rfcomm_release_dev(void __user *arg)
 {
        struct rfcomm_dev_req req;
        struct rfcomm_dev *dev;
-       struct tty_struct *tty;
 
        if (copy_from_user(&req, arg, sizeof(req)))
                return -EFAULT;
@@ -464,11 +463,7 @@ static int __rfcomm_release_dev(void __user *arg)
                rfcomm_dlc_close(dev->dlc, 0);
 
        /* Shut down TTY synchronously before freeing rfcomm_dev */
-       tty = tty_port_tty_get(&dev->port);
-       if (tty) {
-               tty_vhangup(tty);
-               tty_kref_put(tty);
-       }
+       tty_port_tty_vhangup(&dev->port);
 
        if (!test_bit(RFCOMM_TTY_OWNED, &dev->status))
                tty_port_put(&dev->port);