tty: Don't block on IO when ldisc change is pending
authorDmitry Safonov <dima@arista.com>
Thu, 1 Nov 2018 00:24:48 +0000 (00:24 +0000)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 21 Dec 2019 09:41:25 +0000 (10:41 +0100)
[ Upstream commit c96cf923a98d1b094df9f0cf97a83e118817e31b ]

There might be situations where tty_ldisc_lock() has blocked, but there
is already IO on tty and it prevents line discipline changes.
It might theoretically turn into dead-lock.

Basically, provide more priority to pending tty_ldisc_lock() than to
servicing reads/writes over tty.

User-visible issue was reported by Mikulas where on pa-risc with
Debian 5 reboot took either 80 seconds, 3 minutes or 3:25 after proper
locking in tty_reopen().

Cc: Jiri Slaby <jslaby@suse.com>
Reported-by: Mikulas Patocka <mpatocka@redhat.com>
Signed-off-by: Dmitry Safonov <dima@arista.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
drivers/tty/n_hdlc.c
drivers/tty/n_r3964.c
drivers/tty/n_tty.c
drivers/tty/tty_ldisc.c
include/linux/tty.h

index 8d6253903f24f8fff57d797e23303e74a296325a..0c12dec110bc414f125a7dad747d796d4f3adedd 100644 (file)
@@ -614,7 +614,7 @@ static ssize_t n_hdlc_tty_read(struct tty_struct *tty, struct file *file,
                }
                        
                /* no data */
-               if (file->f_flags & O_NONBLOCK) {
+               if (tty_io_nonblock(tty, file)) {
                        ret = -EAGAIN;
                        break;
                }
@@ -681,7 +681,7 @@ static ssize_t n_hdlc_tty_write(struct tty_struct *tty, struct file *file,
                if (tbuf)
                        break;
 
-               if (file->f_flags & O_NONBLOCK) {
+               if (tty_io_nonblock(tty, file)) {
                        error = -EAGAIN;
                        break;
                }
index 345111467b85026ead297b67f27a70cbf67a867d..ee0e07b4a13d0c28e3be90ade952059ce4f9e572 100644 (file)
@@ -1080,7 +1080,7 @@ static ssize_t r3964_read(struct tty_struct *tty, struct file *file,
                pMsg = remove_msg(pInfo, pClient);
                if (pMsg == NULL) {
                        /* no messages available. */
-                       if (file->f_flags & O_NONBLOCK) {
+                       if (tty_io_nonblock(tty, file)) {
                                ret = -EAGAIN;
                                goto unlock;
                        }
index 904fc9c37fdea249cac61732a408450c0aa13252..8214b0326b3a12fb76138d32dec44b2c085a8575 100644 (file)
@@ -1704,7 +1704,7 @@ n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
 
        down_read(&tty->termios_rwsem);
 
-       while (1) {
+       do {
                /*
                 * When PARMRK is set, each input char may take up to 3 chars
                 * in the read buf; reduce the buffer space avail by 3x
@@ -1746,7 +1746,7 @@ n_tty_receive_buf_common(struct tty_struct *tty, const unsigned char *cp,
                        fp += n;
                count -= n;
                rcvd += n;
-       }
+       } while (!test_bit(TTY_LDISC_CHANGING, &tty->flags));
 
        tty->receive_room = room;
 
@@ -2213,7 +2213,7 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
                                        break;
                                if (!timeout)
                                        break;
-                               if (file->f_flags & O_NONBLOCK) {
+                               if (tty_io_nonblock(tty, file)) {
                                        retval = -EAGAIN;
                                        break;
                                }
@@ -2367,7 +2367,7 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
                }
                if (!nr)
                        break;
-               if (file->f_flags & O_NONBLOCK) {
+               if (tty_io_nonblock(tty, file)) {
                        retval = -EAGAIN;
                        break;
                }
index 3eb3f2a03bbb9e95cf68f6e75c473a5dd906a723..706faca834f2710996a79d74b7433e75a128f539 100644 (file)
@@ -348,6 +348,11 @@ int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
 {
        int ret;
 
+       /* Kindly asking blocked readers to release the read side */
+       set_bit(TTY_LDISC_CHANGING, &tty->flags);
+       wake_up_interruptible_all(&tty->read_wait);
+       wake_up_interruptible_all(&tty->write_wait);
+
        ret = __tty_ldisc_lock(tty, timeout);
        if (!ret)
                return -EBUSY;
@@ -358,6 +363,8 @@ int tty_ldisc_lock(struct tty_struct *tty, unsigned long timeout)
 void tty_ldisc_unlock(struct tty_struct *tty)
 {
        clear_bit(TTY_LDISC_HALTED, &tty->flags);
+       /* Can be cleared here - ldisc_unlock will wake up writers firstly */
+       clear_bit(TTY_LDISC_CHANGING, &tty->flags);
        __tty_ldisc_unlock(tty);
 }
 
index fe1b8623a3a1f1b17df64470d7dbc2c538f17e57..fe483976b119393e9f6e6b92d62050a3c00b2d39 100644 (file)
@@ -356,6 +356,7 @@ struct tty_file_private {
 #define TTY_NO_WRITE_SPLIT     17      /* Preserve write boundaries to driver */
 #define TTY_HUPPED             18      /* Post driver->hangup() */
 #define TTY_HUPPING            19      /* Hangup in progress */
+#define TTY_LDISC_CHANGING     20      /* Change pending - non-block IO */
 #define TTY_LDISC_HALTED       22      /* Line discipline is halted */
 
 /* Values for tty->flow_change */
@@ -373,6 +374,12 @@ static inline void tty_set_flow_change(struct tty_struct *tty, int val)
        smp_mb();
 }
 
+static inline bool tty_io_nonblock(struct tty_struct *tty, struct file *file)
+{
+       return file->f_flags & O_NONBLOCK ||
+               test_bit(TTY_LDISC_CHANGING, &tty->flags);
+}
+
 static inline bool tty_io_error(struct tty_struct *tty)
 {
        return test_bit(TTY_IO_ERROR, &tty->flags);