diff options
author | Jiri Slaby <jslaby@suse.cz> | 2010-10-31 18:17:51 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@suse.de> | 2010-11-09 18:04:21 -0500 |
commit | 100eeae2c5ce23b4db93ff320ee330ef1d740151 (patch) | |
tree | 02448f4c2cb5db6de79812f3b5a3dfebe7075afe /drivers/tty | |
parent | ebf7c06635fbcf21a59e60187e166c5c23c57b06 (diff) |
TTY: restore tty_ldisc_wait_idle
It was removed in 65b770468e98 (tty-ldisc: turn ldisc user count into
a proper refcount), but we need to wait for last user to quit the
ldisc before we close it in tty_set_ldisc.
Otherwise weird things start to happen. There might be processes
waiting in tty_read->n_tty_read on tty->read_wait for input to appear
and at that moment, a change of ldisc is fatal. n_tty_close is called,
it frees read_buf and the waiting process is still in the middle of
reading and goes nuts after it is woken.
Previously we prevented close to happen when others are in ldisc ops
by tty_ldisc_wait_idle in tty_set_ldisc. But the commit above removed
that. So revoke the change and test whether there is 1 user (=we), and
allow the close then.
We can do that without ldisc/tty locks, because nobody else can open
the device due to TTY_LDISC_CHANGING bit set, so we in fact wait for
everybody to leave.
I don't understand why tty_ldisc_lock would be needed either when the
counter is an atomic variable, so this is a lockless
tty_ldisc_wait_idle.
On the other hand, if we fail to wait (timeout or signal), we have to
reenable the halted ldiscs, so we take ldisc lock and reuse the setup
path at the end of tty_set_ldisc.
Signed-off-by: Jiri Slaby <jslaby@suse.cz>
Acked-by: Linus Torvalds <torvalds@linux-foundation.org>
Tested-by: Sebastian Andrzej Siewior <bigeasy@breakpoint.cc>
LKML-Reference: <20101031104136.GA511@Chamillionaire.breakpoint.cc>
LKML-Reference: <1287669539-22644-1-git-send-email-jslaby@suse.cz>
Cc: Alan Cox <alan@linux.intel.com>
Cc: stable@kernel.org [32, 33, 36]
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
Diffstat (limited to 'drivers/tty')
-rw-r--r-- | drivers/tty/tty_ldisc.c | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/drivers/tty/tty_ldisc.c b/drivers/tty/tty_ldisc.c index 412f9775d19c..5bbf33ad49f1 100644 --- a/drivers/tty/tty_ldisc.c +++ b/drivers/tty/tty_ldisc.c | |||
@@ -47,6 +47,7 @@ | |||
47 | 47 | ||
48 | static DEFINE_SPINLOCK(tty_ldisc_lock); | 48 | static DEFINE_SPINLOCK(tty_ldisc_lock); |
49 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); | 49 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_wait); |
50 | static DECLARE_WAIT_QUEUE_HEAD(tty_ldisc_idle); | ||
50 | /* Line disc dispatch table */ | 51 | /* Line disc dispatch table */ |
51 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; | 52 | static struct tty_ldisc_ops *tty_ldiscs[NR_LDISCS]; |
52 | 53 | ||
@@ -83,6 +84,7 @@ static void put_ldisc(struct tty_ldisc *ld) | |||
83 | return; | 84 | return; |
84 | } | 85 | } |
85 | local_irq_restore(flags); | 86 | local_irq_restore(flags); |
87 | wake_up(&tty_ldisc_idle); | ||
86 | } | 88 | } |
87 | 89 | ||
88 | /** | 90 | /** |
@@ -531,6 +533,23 @@ static int tty_ldisc_halt(struct tty_struct *tty) | |||
531 | } | 533 | } |
532 | 534 | ||
533 | /** | 535 | /** |
536 | * tty_ldisc_wait_idle - wait for the ldisc to become idle | ||
537 | * @tty: tty to wait for | ||
538 | * | ||
539 | * Wait for the line discipline to become idle. The discipline must | ||
540 | * have been halted for this to guarantee it remains idle. | ||
541 | */ | ||
542 | static int tty_ldisc_wait_idle(struct tty_struct *tty) | ||
543 | { | ||
544 | int ret; | ||
545 | ret = wait_event_interruptible_timeout(tty_ldisc_idle, | ||
546 | atomic_read(&tty->ldisc->users) == 1, 5 * HZ); | ||
547 | if (ret < 0) | ||
548 | return ret; | ||
549 | return ret > 0 ? 0 : -EBUSY; | ||
550 | } | ||
551 | |||
552 | /** | ||
534 | * tty_set_ldisc - set line discipline | 553 | * tty_set_ldisc - set line discipline |
535 | * @tty: the terminal to set | 554 | * @tty: the terminal to set |
536 | * @ldisc: the line discipline | 555 | * @ldisc: the line discipline |
@@ -634,8 +653,17 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc) | |||
634 | 653 | ||
635 | flush_scheduled_work(); | 654 | flush_scheduled_work(); |
636 | 655 | ||
656 | retval = tty_ldisc_wait_idle(tty); | ||
657 | |||
637 | tty_lock(); | 658 | tty_lock(); |
638 | mutex_lock(&tty->ldisc_mutex); | 659 | mutex_lock(&tty->ldisc_mutex); |
660 | |||
661 | /* handle wait idle failure locked */ | ||
662 | if (retval) { | ||
663 | tty_ldisc_put(new_ldisc); | ||
664 | goto enable; | ||
665 | } | ||
666 | |||
639 | if (test_bit(TTY_HUPPED, &tty->flags)) { | 667 | if (test_bit(TTY_HUPPED, &tty->flags)) { |
640 | /* We were raced by the hangup method. It will have stomped | 668 | /* We were raced by the hangup method. It will have stomped |
641 | the ldisc data and closed the ldisc down */ | 669 | the ldisc data and closed the ldisc down */ |
@@ -669,6 +697,7 @@ int tty_set_ldisc(struct tty_struct *tty, int ldisc) | |||
669 | 697 | ||
670 | tty_ldisc_put(o_ldisc); | 698 | tty_ldisc_put(o_ldisc); |
671 | 699 | ||
700 | enable: | ||
672 | /* | 701 | /* |
673 | * Allow ldisc referencing to occur again | 702 | * Allow ldisc referencing to occur again |
674 | */ | 703 | */ |