Skip to contentSkip to conversation

do_task_stat: don't take tty_mutex

PATCH In review Oleg Nesterov 2 participants 3 messages Oct 22, 2006 – Oct 23, 2006 Proc filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 do_task_stat: don't take tty_mutex

Depends on
tty-signal-tty-locking.patch

->signal->tty is protected by ->siglock, no need to take the global tty_mutex.

fs/proc/array.c renamed from fs/proc/array.c~ +3 -11
@@ -346,20 +346,13 @@ static int do_task_stat(struct task_stru
 	sigemptyset(&sigcatch);
 	cutime = cstime = utime = stime = cputime_zero;
 
-	mutex_lock(&tty_mutex);
 	rcu_read_lock();
 	if (lock_task_sighand(task, &flags)) {
 		struct signal_struct *sig = task->signal;
-		struct tty_struct *tty = sig->tty;
 
-		if (tty) {
-			/*
-			 * sig->tty is not stable, but tty_mutex
-			 * protects us from release_dev(tty)
-			 */
-			barrier();
-			tty_pgrp = tty->pgrp;
-			tty_nr = new_encode_dev(tty_devnum(tty));
+		if (sig->tty) {
+			tty_pgrp = sig->tty->pgrp;
+			tty_nr = new_encode_dev(tty_devnum(sig->tty));
 		}
 
 		num_threads = atomic_read(&sig->count);
@@ -395,7 +388,6 @@ static int do_task_stat(struct task_stru
 		unlock_task_sighand(task, &flags);
 	}
 	rcu_read_unlock();
-	mutex_unlock(&tty_mutex);
 
 	if (!whole || num_threads<2)
 		wchan = get_wchan(task);
Acked-by on v1 1/1
#1 lore raw
Quoted 6 lines · Oleg Nesterov

Depends on
tty-signal-tty-locking.patch

->signal->tty is protected by ->siglock, no need to take the global tty_mutex.

->siglock protects the value of ->signal->tty, not memory it points to; however since destroying the tty also means clearing all ->signal->tty references, which means taking all ->siglocks, just holding the ->siglock around this piece of code looks sufficient indeed.

Quoted 35 lines
fs/proc/array.c renamed from fs/proc/array.c~ +3 -11
@@ -346,20 +346,13 @@ static int do_task_stat(struct task_stru
 	sigemptyset(&sigcatch);
 	cutime = cstime = utime = stime = cputime_zero;
 
-	mutex_lock(&tty_mutex);
 	rcu_read_lock();
 	if (lock_task_sighand(task, &flags)) {
 		struct signal_struct *sig = task->signal;
-		struct tty_struct *tty = sig->tty;
 
-		if (tty) {
-			/*
-			 * sig->tty is not stable, but tty_mutex
-			 * protects us from release_dev(tty)
-			 */
-			barrier();
-			tty_pgrp = tty->pgrp;
-			tty_nr = new_encode_dev(tty_devnum(tty));
+		if (sig->tty) {
+			tty_pgrp = sig->tty->pgrp;
+			tty_nr = new_encode_dev(tty_devnum(sig->tty));
 		}
 
 		num_threads = atomic_read(&sig->count);
@@ -395,7 +388,6 @@ static int do_task_stat(struct task_stru
 		unlock_task_sighand(task, &flags);
 	}
 	rcu_read_unlock();
-	mutex_unlock(&tty_mutex);
 
 	if (!whole || num_threads<2)
 		wchan = get_wchan(task);
#2 lore raw
Quoted 10 lines · Peter Zijlstra
Quoted 6 lines · Oleg Nesterov

Depends on
tty-signal-tty-locking.patch

->signal->tty is protected by ->siglock, no need to take the global tty_mutex.

->siglock protects the value of ->signal->tty, not memory it points to;

Yes. That is why we can't avoid tty_mutex in audit_log_exit() (unless we copy tty->name to the safe location). But is is bad we take tasklist_lock to access tsk->signal. This 'tsk' should be 'current', we can use get_current_tty(). Or we can use lock_task_sighand() to be safe.

What do you think?

Oleg.

Quoted 42 lines

however since destroying the tty also means clearing all ->signal->tty references, which means taking all ->siglocks, just holding the ->siglock around this piece of code looks sufficient indeed.

Quoted 35 lines
fs/proc/array.c renamed from fs/proc/array.c~ +3 -11
@@ -346,20 +346,13 @@ static int do_task_stat(struct task_stru
 	sigemptyset(&sigcatch);
 	cutime = cstime = utime = stime = cputime_zero;
 
-	mutex_lock(&tty_mutex);
 	rcu_read_lock();
 	if (lock_task_sighand(task, &flags)) {
 		struct signal_struct *sig = task->signal;
-		struct tty_struct *tty = sig->tty;
 
-		if (tty) {
-			/*
-			 * sig->tty is not stable, but tty_mutex
-			 * protects us from release_dev(tty)
-			 */
-			barrier();
-			tty_pgrp = tty->pgrp;
-			tty_nr = new_encode_dev(tty_devnum(tty));
+		if (sig->tty) {
+			tty_pgrp = sig->tty->pgrp;
+			tty_nr = new_encode_dev(tty_devnum(sig->tty));
 		}
 
 		num_threads = atomic_read(&sig->count);
@@ -395,7 +388,6 @@ static int do_task_stat(struct task_stru
 		unlock_task_sighand(task, &flags);
 	}
 	rcu_read_unlock();
-	mutex_unlock(&tty_mutex);
 
 	if (!whole || num_threads<2)
 		wchan = get_wchan(task);