Commit f8747d4a authored by Peter Hurley's avatar Peter Hurley Committed by Greg Kroah-Hartman

tty: Fix pty master read() after slave closes

Commit f95499c3,
  n_tty: Don't wait for buffer work in read() loop
creates a race window which can cause a pty master read()
to miss the last pty slave write(s) and return -EIO instead,
thus signalling the pty slave is closed. This can happen when
the pty slave is written and immediately closed but before the
tty buffer i/o loop receives the new input; the pty master
read() is scheduled, sees its read buffer is empty and the
pty slave has been closed, and exits.

Because tty_flush_to_ldisc() has significant performance impact
for parallel i/o, rather than revert the commit, special case this
condition (ie., when the read buffer is empty and the 'other' pty
has been closed) and, only then, wait for buffer work to complete
before re-testing if the read buffer is still empty.

As before, subsequent pty master reads return any available data
until no more data is available, and then returns -EIO to
indicate the pty slave has closed.
Reported-by: default avatarMikael Pettersson <mikpelinux@gmail.com>
Signed-off-by: default avatarPeter Hurley <peter@hurleysoftware.com>
Tested-by: default avatarMikael Pettersson <mikpelinux@gmail.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 15c03dd4
...@@ -2183,28 +2183,34 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, ...@@ -2183,28 +2183,34 @@ static ssize_t n_tty_read(struct tty_struct *tty, struct file *file,
if (!input_available_p(tty, 0)) { if (!input_available_p(tty, 0)) {
if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) { if (test_bit(TTY_OTHER_CLOSED, &tty->flags)) {
retval = -EIO; up_read(&tty->termios_rwsem);
break; tty_flush_to_ldisc(tty);
} down_read(&tty->termios_rwsem);
if (tty_hung_up_p(file)) if (!input_available_p(tty, 0)) {
break; retval = -EIO;
if (!timeout) break;
break; }
if (file->f_flags & O_NONBLOCK) { } else {
retval = -EAGAIN; if (tty_hung_up_p(file))
break; break;
} if (!timeout)
if (signal_pending(current)) { break;
retval = -ERESTARTSYS; if (file->f_flags & O_NONBLOCK) {
break; retval = -EAGAIN;
} break;
n_tty_set_room(tty); }
up_read(&tty->termios_rwsem); if (signal_pending(current)) {
retval = -ERESTARTSYS;
break;
}
n_tty_set_room(tty);
up_read(&tty->termios_rwsem);
timeout = schedule_timeout(timeout); timeout = schedule_timeout(timeout);
down_read(&tty->termios_rwsem); down_read(&tty->termios_rwsem);
continue; continue;
}
} }
__set_current_state(TASK_RUNNING); __set_current_state(TASK_RUNNING);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment