[PATCH 43/76] tty: Redo current tty locking

Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
From: Alan Cox
Date: Sunday, October 5, 2008 - 9:15 am

Currently it is sometimes locked by the tty mutex and sometimes by the
sighand lock. The latter is in fact correct and now we can hand back referenced
objects we can fix this up without problems around sleeping functions.

Signed-off-by: Alan Cox <alan@redhat.com>
---

 drivers/char/tty_io.c    |   18 ++++--------------
 fs/dquot.c               |    2 --
 include/linux/tty.h      |    1 +
 security/selinux/hooks.c |    2 --
 4 files changed, 5 insertions(+), 18 deletions(-)

diff --git a/drivers/char/tty_io.c b/drivers/char/tty_io.c
index b5f57d0..f40298e 100644
--- a/drivers/char/tty_io.c
+++ b/drivers/char/tty_io.c
@@ -739,13 +739,11 @@ void tty_vhangup_self(void)
 {
 	struct tty_struct *tty;
 
-	mutex_lock(&tty_mutex);
 	tty = get_current_tty();
 	if (tty) {
 		tty_vhangup(tty);
 		tty_kref_put(tty);
 	}
-	mutex_unlock(&tty_mutex);
 }
 
 /**
@@ -801,11 +799,9 @@ void disassociate_ctty(int on_exit)
 	struct pid *tty_pgrp = NULL;
 
 
-	mutex_lock(&tty_mutex);
 	tty = get_current_tty();
 	if (tty) {
 		tty_pgrp = get_pid(tty->pgrp);
-		mutex_unlock(&tty_mutex);
 		lock_kernel();
 		if (on_exit && tty->driver->type != TTY_DRIVER_TYPE_PTY)
 			tty_vhangup(tty);
@@ -822,7 +818,6 @@ void disassociate_ctty(int on_exit)
 			kill_pgrp(old_pgrp, SIGCONT, on_exit);
 			put_pid(old_pgrp);
 		}
-		mutex_unlock(&tty_mutex);
 		return;
 	}
 	if (tty_pgrp) {
@@ -837,7 +832,6 @@ void disassociate_ctty(int on_exit)
 	current->signal->tty_old_pgrp = NULL;
 	spin_unlock_irq(&current->sighand->siglock);
 
-	mutex_lock(&tty_mutex);
 	tty = get_current_tty();
 	if (tty) {
 		unsigned long flags;
@@ -854,7 +848,6 @@ void disassociate_ctty(int on_exit)
 		       " = NULL", tty);
 #endif
 	}
-	mutex_unlock(&tty_mutex);
 
 	/* Now clear signal->tty under the lock */
 	read_lock(&tasklist_lock);
@@ -3180,14 +3173,11 @@ static void proc_set_tty(struct task_struct *tsk, struct tty_struct *tty)
 struct tty_struct *get_current_tty(void)
 {
 	struct tty_struct *tty;
-	WARN_ON_ONCE(!mutex_is_locked(&tty_mutex));
+	unsigned long flags;
+
+	spin_lock_irqsave(&current->sighand->siglock, flags);
 	tty = tty_kref_get(current->signal->tty);
-	/*
-	 * session->tty can be changed/cleared from under us, make sure we
-	 * issue the load. The obtained pointer, when not NULL, is valid as
-	 * long as we hold tty_mutex.
-	 */
-	barrier();
+	spin_unlock_irqrestore(&current->sighand->siglock, flags);
 	return tty;
 }
 EXPORT_SYMBOL_GPL(get_current_tty);
diff --git a/fs/dquot.c b/fs/dquot.c
index 7417a6c..ad7e590 100644
--- a/fs/dquot.c
+++ b/fs/dquot.c
@@ -895,9 +895,7 @@ static void print_warning(struct dquot *dquot, const int warntype)
 	    warntype == QUOTA_NL_BSOFTBELOW || !need_print_warning(dquot))
 		return;
 
-	mutex_lock(&tty_mutex);
 	tty = get_current_tty();
-	mutex_unlock(&tty_mutex);
 	if (!tty)
 		return;
 	tty_write_message(tty, dquot->dq_sb->s_id);
diff --git a/include/linux/tty.h b/include/linux/tty.h
index c30ed8d..e00393a 100644
--- a/include/linux/tty.h
+++ b/include/linux/tty.h
@@ -361,6 +361,7 @@ extern int is_ignored(int sig);
 extern int tty_signal(int sig, struct tty_struct *tty);
 extern void tty_hangup(struct tty_struct *tty);
 extern void tty_vhangup(struct tty_struct *tty);
+extern void tty_vhangup_self(void);
 extern void tty_unhangup(struct file *filp);
 extern int tty_hung_up_p(struct file *filp);
 extern void do_SAK(struct tty_struct *tty);
diff --git a/security/selinux/hooks.c b/security/selinux/hooks.c
index c856db8..215cddd 100644
--- a/security/selinux/hooks.c
+++ b/security/selinux/hooks.c
@@ -2120,9 +2120,7 @@ static inline void flush_unauthorized_files(struct files_struct *files)
 	long j = -1;
 	int drop_tty = 0;
 
-	mutex_lock(&tty_mutex);
 	tty = get_current_tty();
-	mutex_unlock(&tty_mutex);
 	if (tty) {
 		file_list_lock();
 		file = list_entry(tty->tty_files.next, typeof(*file), f_u.fu_list);

--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
[PATCH 00/76] Queued TTY Patches, Alan Cox, (Sun Oct 5, 9:04 am)
[PATCH 04/76] epca: call tty_port_init, Alan Cox, (Sun Oct 5, 9:05 am)
[PATCH 05/76] 8250: Remove NR_IRQ usage, Alan Cox, (Sun Oct 5, 9:05 am)
[PATCH 16/76] Char: merge ip2main and ip2base, Alan Cox, (Sun Oct 5, 9:06 am)
[PATCH 17/76] ip2, cleanup globals, Alan Cox, (Sun Oct 5, 9:06 am)
[PATCH 18/76] ip2, fix sparse warnings, Alan Cox, (Sun Oct 5, 9:07 am)
[PATCH 19/76] ip2, init/deinit cleanup, Alan Cox, (Sun Oct 5, 9:07 am)
[PATCH 26/76] tty: Add a kref count, Alan Cox, (Sun Oct 5, 9:08 am)
[PATCH 28/76] Char: cyclades. remove bogus iomap, Alan Cox, (Sun Oct 5, 9:08 am)
[PATCH 29/76] Char: sx, fix io unmapping, Alan Cox, (Sun Oct 5, 9:08 am)
[PATCH 32/76] tty: ipw need reworking, Alan Cox, (Sun Oct 5, 9:09 am)
[PATCH 33/76] tty: Add termiox, Alan Cox, (Sun Oct 5, 9:09 am)
[PATCH 35/76] tty: compare the tty winsize, Alan Cox, (Sun Oct 5, 9:10 am)
[PATCH 38/76] tty: usb-serial krefs, Alan Cox, (Sun Oct 5, 9:11 am)
[PATCH 40/76] stallion: Use krefs, Alan Cox, (Sun Oct 5, 9:13 am)
[PATCH 41/76] mxser: Switch to kref tty, Alan Cox, (Sun Oct 5, 9:14 am)
[PATCH 42/76] tty: the vhangup syscall is racy, Alan Cox, (Sun Oct 5, 9:15 am)
[PATCH 43/76] tty: Redo current tty locking, Alan Cox, (Sun Oct 5, 9:15 am)
[PATCH 46/76] vt: remove bogus lock dropping, Alan Cox, (Sun Oct 5, 9:18 am)
[PATCH 47/76] tty: shutdown method, Alan Cox, (Sun Oct 5, 9:18 am)
[PATCH 51/76] tty: kref the tty driver object, Alan Cox, (Sun Oct 5, 9:20 am)
[PATCH 52/76] tty: More driver operations, Alan Cox, (Sun Oct 5, 9:20 am)
[PATCH 55/76] Move tty lookup/reopen to caller, Alan Cox, (Sun Oct 5, 9:21 am)
[PATCH 57/76] Simplify devpts_get_tty(), Alan Cox, (Sun Oct 5, 9:21 am)
[PATCH 58/76] Simplify devpts_pty_new(), Alan Cox, (Sun Oct 5, 9:21 am)
[PATCH 59/76] Simplify devpts_pty_kill, Alan Cox, (Sun Oct 5, 9:21 am)
[PATCH 60/76] pty: Coding style and polish, Alan Cox, (Sun Oct 5, 9:22 am)
[PATCH 62/76] pty: simplify unix98 allocation, Alan Cox, (Sun Oct 5, 9:22 am)
[PATCH 63/76] tty: simplify ktermios allocation, Alan Cox, (Sun Oct 5, 9:22 am)
[PATCH 66/76] tty: fix up gigaset a bit, Alan Cox, (Sun Oct 5, 9:23 am)
[PATCH 67/76] tty: Remove lots of NULL checks, Alan Cox, (Sun Oct 5, 9:23 am)
[PATCH 74/76] tty: tty_io.c shadows sparse fix, Alan Cox, (Sun Oct 5, 9:25 am)
[PATCH 75/76] usb: fix pl2303 initialization, Alan Cox, (Sun Oct 5, 9:25 am)
Re: [PATCH 38/76] tty: usb-serial krefs, Greg KH, (Sun Oct 5, 10:36 pm)
Re: [PATCH 67/76] tty: Remove lots of NULL checks, Geert Uytterhoeven, (Mon Oct 6, 1:58 am)
Re: [PATCH 38/76] tty: usb-serial krefs, Alan Cox, (Mon Oct 6, 2:02 am)
Re: [PATCH 26/76] tty: Add a kref count, Louis Rilling, (Mon Oct 6, 3:20 am)
Re: [PATCH 36/76] tty: Make get_current_tty use a kref, Louis Rilling, (Mon Oct 6, 3:35 am)
Re: [PATCH 26/76] tty: Add a kref count, Alan Cox, (Mon Oct 6, 3:52 am)
Re: [PATCH 38/76] tty: usb-serial krefs, Greg KH, (Mon Oct 6, 9:13 pm)