Commit bfac7009 authored by Oleg Nesterov's avatar Oleg Nesterov Committed by Ingo Molnar

sched: thread_group_cputime: Simplify, document the "alive" check

thread_group_cputime() looks as if it is rcu-safe, but in fact this
was wrong until ea6d290c which pins task->signal to task_struct.
It checks ->sighand != NULL under rcu, but this can't help if ->signal
can go away. Fortunately the caller either holds ->siglock, or it is
fastpath_timer_check() which uses current and checks exit_state == 0.

- Since ea6d290c commit tsk->signal is stable, we can read it first
  and avoid the initialization from INIT_CPUTIME.

- Even if tsk->signal is always valid, we still have to check it
  is safe to use next_thread() under rcu_read_lock(). Currently
  the code checks ->sighand != NULL, change it to use pid_alive()
  which is commonly used to ensure the task wasn't unhashed before
  we take rcu_read_lock().

  Add the comment to explain this check.

- Change the main loop to use the while_each_thread() helper.
Signed-off-by: default avatarOleg Nesterov <oleg@redhat.com>
Signed-off-by: default avatarPeter Zijlstra <a.p.zijlstra@chello.nl>
LKML-Reference: <20100610230956.GA25921@redhat.com>
Signed-off-by: default avatarIngo Molnar <mingo@elte.hu>
parent 48286d50
...@@ -232,31 +232,24 @@ static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p, ...@@ -232,31 +232,24 @@ static int cpu_clock_sample(const clockid_t which_clock, struct task_struct *p,
void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times) void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
{ {
struct sighand_struct *sighand; struct signal_struct *sig = tsk->signal;
struct signal_struct *sig;
struct task_struct *t; struct task_struct *t;
*times = INIT_CPUTIME; times->utime = sig->utime;
times->stime = sig->stime;
times->sum_exec_runtime = sig->sum_sched_runtime;
rcu_read_lock(); rcu_read_lock();
sighand = rcu_dereference(tsk->sighand); /* make sure we can trust tsk->thread_group list */
if (!sighand) if (!likely(pid_alive(tsk)))
goto out; goto out;
sig = tsk->signal;
t = tsk; t = tsk;
do { do {
times->utime = cputime_add(times->utime, t->utime); times->utime = cputime_add(times->utime, t->utime);
times->stime = cputime_add(times->stime, t->stime); times->stime = cputime_add(times->stime, t->stime);
times->sum_exec_runtime += t->se.sum_exec_runtime; times->sum_exec_runtime += t->se.sum_exec_runtime;
} while_each_thread(tsk, t);
t = next_thread(t);
} while (t != tsk);
times->utime = cputime_add(times->utime, sig->utime);
times->stime = cputime_add(times->stime, sig->stime);
times->sum_exec_runtime += sig->sum_sched_runtime;
out: out:
rcu_read_unlock(); rcu_read_unlock();
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment