Skip to content

Commit d3736d8

Browse files
0x7f454c46gregkh
authored andcommitted
tty: Don't hold ldisc lock in tty_reopen() if ldisc present
Try to get reference for ldisc during tty_reopen(). If ldisc present, we don't need to do tty_ldisc_reinit() and lock the write side for line discipline semaphore. Effectively, it optimizes fast-path for tty_reopen(), but more importantly it won't interrupt ongoing IO on the tty as no ldisc change is needed. Fixes user-visible issue when tty_reopen() interrupted login process for user with a long password, observed and reported by Lukas. Fixes: c96cf92 ("tty: Don't block on IO when ldisc change is pending") Fixes: 83d817f ("tty: Hold tty_ldisc_lock() during tty_reopen()") Cc: Jiri Slaby <jslaby@suse.com> Reported-by: Lukas F. Hartmann <lukas@mntmn.com> Tested-by: Lukas F. Hartmann <lukas@mntmn.com> Cc: stable <stable@vger.kernel.org> Signed-off-by: Dmitry Safonov <dima@arista.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d3a28a5 commit d3736d8

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

drivers/tty/tty_io.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,7 +1256,8 @@ static void tty_driver_remove_tty(struct tty_driver *driver, struct tty_struct *
12561256
static int tty_reopen(struct tty_struct *tty)
12571257
{
12581258
struct tty_driver *driver = tty->driver;
1259-
int retval;
1259+
struct tty_ldisc *ld;
1260+
int retval = 0;
12601261

12611262
if (driver->type == TTY_DRIVER_TYPE_PTY &&
12621263
driver->subtype == PTY_TYPE_MASTER)
@@ -1268,13 +1269,18 @@ static int tty_reopen(struct tty_struct *tty)
12681269
if (test_bit(TTY_EXCLUSIVE, &tty->flags) && !capable(CAP_SYS_ADMIN))
12691270
return -EBUSY;
12701271

1271-
retval = tty_ldisc_lock(tty, 5 * HZ);
1272-
if (retval)
1273-
return retval;
1272+
ld = tty_ldisc_ref_wait(tty);
1273+
if (ld) {
1274+
tty_ldisc_deref(ld);
1275+
} else {
1276+
retval = tty_ldisc_lock(tty, 5 * HZ);
1277+
if (retval)
1278+
return retval;
12741279

1275-
if (!tty->ldisc)
1276-
retval = tty_ldisc_reinit(tty, tty->termios.c_line);
1277-
tty_ldisc_unlock(tty);
1280+
if (!tty->ldisc)
1281+
retval = tty_ldisc_reinit(tty, tty->termios.c_line);
1282+
tty_ldisc_unlock(tty);
1283+
}
12781284

12791285
if (retval == 0)
12801286
tty->count++;

0 commit comments

Comments
 (0)