[PATCH] proc: convert do_task_stat() to use lock_task_sighand()
Drop tasklist_lock. ->siglock protects almost all interesting data (including sub-threads traversal) except: ->signal->tty protected by tty_mutex ->real_parent the task can't be unhashed while we are holding ->siglock, so ->real_parent can change from under us but we can safely dereference it under rcu_read_lock() ->pgrp/->session we can get inconsistent numbers if the task does sys_setsid/daemonize at the same time. I hope this is acceptable. Signed-off-by: Oleg Nesterov <oleg@tv-sign.ru> Cc: "Eric W. Biederman" <ebiederm@xmission.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
This commit is contained in:
Родитель
5e6b3f42ed
Коммит
a593d6edeb
|
@ -321,7 +321,7 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
|
||||||
sigset_t sigign, sigcatch;
|
sigset_t sigign, sigcatch;
|
||||||
char state;
|
char state;
|
||||||
int res;
|
int res;
|
||||||
pid_t ppid, pgid = -1, sid = -1;
|
pid_t ppid = 0, pgid = -1, sid = -1;
|
||||||
int num_threads = 0;
|
int num_threads = 0;
|
||||||
struct mm_struct *mm;
|
struct mm_struct *mm;
|
||||||
unsigned long long start_time;
|
unsigned long long start_time;
|
||||||
|
@ -329,8 +329,8 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
|
||||||
unsigned long min_flt = 0, maj_flt = 0;
|
unsigned long min_flt = 0, maj_flt = 0;
|
||||||
cputime_t cutime, cstime, utime, stime;
|
cputime_t cutime, cstime, utime, stime;
|
||||||
unsigned long rsslim = 0;
|
unsigned long rsslim = 0;
|
||||||
struct task_struct *t;
|
|
||||||
char tcomm[sizeof(task->comm)];
|
char tcomm[sizeof(task->comm)];
|
||||||
|
unsigned long flags;
|
||||||
|
|
||||||
state = *get_task_state(task);
|
state = *get_task_state(task);
|
||||||
vsize = eip = esp = 0;
|
vsize = eip = esp = 0;
|
||||||
|
@ -348,15 +348,33 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
|
||||||
cutime = cstime = utime = stime = cputime_zero;
|
cutime = cstime = utime = stime = cputime_zero;
|
||||||
|
|
||||||
mutex_lock(&tty_mutex);
|
mutex_lock(&tty_mutex);
|
||||||
read_lock(&tasklist_lock);
|
rcu_read_lock();
|
||||||
if (task->sighand) {
|
if (lock_task_sighand(task, &flags)) {
|
||||||
spin_lock_irq(&task->sighand->siglock);
|
struct signal_struct *sig = task->signal;
|
||||||
num_threads = atomic_read(&task->signal->count);
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
|
num_threads = atomic_read(&sig->count);
|
||||||
collect_sigign_sigcatch(task, &sigign, &sigcatch);
|
collect_sigign_sigcatch(task, &sigign, &sigcatch);
|
||||||
|
|
||||||
|
cmin_flt = sig->cmin_flt;
|
||||||
|
cmaj_flt = sig->cmaj_flt;
|
||||||
|
cutime = sig->cutime;
|
||||||
|
cstime = sig->cstime;
|
||||||
|
rsslim = sig->rlim[RLIMIT_RSS].rlim_cur;
|
||||||
|
|
||||||
/* add up live thread stats at the group level */
|
/* add up live thread stats at the group level */
|
||||||
if (whole) {
|
if (whole) {
|
||||||
t = task;
|
struct task_struct *t = task;
|
||||||
do {
|
do {
|
||||||
min_flt += t->min_flt;
|
min_flt += t->min_flt;
|
||||||
maj_flt += t->maj_flt;
|
maj_flt += t->maj_flt;
|
||||||
|
@ -364,31 +382,20 @@ static int do_task_stat(struct task_struct *task, char * buffer, int whole)
|
||||||
stime = cputime_add(stime, t->stime);
|
stime = cputime_add(stime, t->stime);
|
||||||
t = next_thread(t);
|
t = next_thread(t);
|
||||||
} while (t != task);
|
} while (t != task);
|
||||||
|
|
||||||
|
min_flt += sig->min_flt;
|
||||||
|
maj_flt += sig->maj_flt;
|
||||||
|
utime = cputime_add(utime, sig->utime);
|
||||||
|
stime = cputime_add(stime, sig->stime);
|
||||||
}
|
}
|
||||||
|
|
||||||
spin_unlock_irq(&task->sighand->siglock);
|
sid = sig->session;
|
||||||
}
|
|
||||||
if (task->signal) {
|
|
||||||
if (task->signal->tty) {
|
|
||||||
tty_pgrp = task->signal->tty->pgrp;
|
|
||||||
tty_nr = new_encode_dev(tty_devnum(task->signal->tty));
|
|
||||||
}
|
|
||||||
pgid = process_group(task);
|
pgid = process_group(task);
|
||||||
sid = task->signal->session;
|
ppid = rcu_dereference(task->real_parent)->tgid;
|
||||||
cmin_flt = task->signal->cmin_flt;
|
|
||||||
cmaj_flt = task->signal->cmaj_flt;
|
unlock_task_sighand(task, &flags);
|
||||||
cutime = task->signal->cutime;
|
|
||||||
cstime = task->signal->cstime;
|
|
||||||
rsslim = task->signal->rlim[RLIMIT_RSS].rlim_cur;
|
|
||||||
if (whole) {
|
|
||||||
min_flt += task->signal->min_flt;
|
|
||||||
maj_flt += task->signal->maj_flt;
|
|
||||||
utime = cputime_add(utime, task->signal->utime);
|
|
||||||
stime = cputime_add(stime, task->signal->stime);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ppid = pid_alive(task) ? task->group_leader->real_parent->tgid : 0;
|
rcu_read_unlock();
|
||||||
read_unlock(&tasklist_lock);
|
|
||||||
mutex_unlock(&tty_mutex);
|
mutex_unlock(&tty_mutex);
|
||||||
|
|
||||||
if (!whole || num_threads<2)
|
if (!whole || num_threads<2)
|
||||||
|
|
Загрузка…
Ссылка в новой задаче