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>
Wed, 5 Dec 2018 11:16:33 +0000 (12:16 +0100)
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>
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 dabb391909aad63d9152e2cdd5dab35f73d1fe7e..99460af61b7712a6339b04bb4fd3d7561c9cc9e6 100644 (file)
@@ -612,7 +612,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;
                }
@@ -679,7 +679,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 749a608c40b0279d64991e937161f32ac7443158..f75696f0ee2d03c83de6ee7af0ae93bd93440c4a 100644 (file)
@@ -1085,7 +1085,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 3ad460219fd62ea580bbdc7a9102ff22de881d14..5dc9686697cfa4c80e5c6e3995f7ef74ad1f583d 100644 (file)
@@ -1702,7 +1702,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
@@ -1744,7 +1744,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;
 
@@ -2211,7 +2211,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;
                                }
@@ -2365,7 +2365,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 fc4c97cae01ef4812da9b1bf61d6efe5d9cf6784..9434d20cf3cad852bd236795c01f41a86fe0ea88 100644 (file)
@@ -327,6 +327,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;
@@ -337,6 +342,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 414db2bce7150cc94c8b24cc4106161511e4ead1..80ae5528ef8eedcd17692278d40bd7e83203fab2 100644 (file)
@@ -366,6 +366,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 */
@@ -383,6 +384,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);