Skip to content

Commit e78c349

Browse files
Rik van Rielingomolnar
authored andcommitted
time, signal: Protect resource use statistics with seqlock
Both times() and clock_gettime(CLOCK_PROCESS_CPUTIME_ID) have scalability issues on large systems, due to both functions being serialized with a lock. The lock protects against reporting a wrong value, due to a thread in the task group exiting, its statistics reporting up to the signal struct, and that exited task's statistics being counted twice (or not at all). Protecting that with a lock results in times() and clock_gettime() being completely serialized on large systems. This can be fixed by using a seqlock around the events that gather and propagate statistics. As an additional benefit, the protection code can be moved into thread_group_cputime(), slightly simplifying the calling functions. In the case of posix_cpu_clock_get_task() things can be simplified a lot, because the calling function already ensures that the task sticks around, and the rest is now taken care of in thread_group_cputime(). This way the statistics reporting code can run lockless. Signed-off-by: Rik van Riel <riel@redhat.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Alex Thorlton <athorlton@sgi.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Daeseok Youn <daeseok.youn@gmail.com> Cc: David Rientjes <rientjes@google.com> Cc: Dongsheng Yang <yangds.fnst@cn.fujitsu.com> Cc: Geert Uytterhoeven <geert@linux-m68k.org> Cc: Guillaume Morin <guillaume@morinfr.org> Cc: Ionut Alexa <ionut.m.alexa@gmail.com> Cc: Kees Cook <keescook@chromium.org> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Li Zefan <lizefan@huawei.com> Cc: Michal Hocko <mhocko@suse.cz> Cc: Michal Schmidt <mschmidt@redhat.com> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Vladimir Davydov <vdavydov@parallels.com> Cc: umgwanakikbuti@gmail.com Cc: fweisbec@gmail.com Cc: srao@redhat.com Cc: lwoodman@redhat.com Cc: atheurer@redhat.com Link: http://lkml.kernel.org/r/20140816134010.26a9b572@annuminas.surriel.com Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 90ed9cb commit e78c349

6 files changed

Lines changed: 26 additions & 29 deletions

File tree

‎include/linux/sched.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ struct signal_struct {
645645
* Live threads maintain their own counters and add to these
646646
* in __exit_signal, except for the group leader.
647647
*/
648+
seqlock_t stats_lock;
648649
cputime_t utime, stime, cutime, cstime;
649650
cputime_t gtime;
650651
cputime_t cgtime;

‎kernel/exit.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ static void __exit_signal(struct task_struct *tsk)
127127
* the signal_struct.
128128
*/
129129
task_cputime(tsk, &utime, &stime);
130+
write_seqlock(&sig->stats_lock);
130131
sig->utime += utime;
131132
sig->stime += stime;
132133
sig->gtime += task_gtime(tsk);
@@ -140,6 +141,7 @@ static void __exit_signal(struct task_struct *tsk)
140141
sig->sum_sched_runtime += tsk->se.sum_exec_runtime;
141142
sig->nr_threads--;
142143
__unhash_process(tsk, group_dead);
144+
write_sequnlock(&sig->stats_lock);
143145

144146
/*
145147
* Do this under ->siglock, we can race with another thread
@@ -1042,6 +1044,7 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
10421044
spin_lock_irq(&p->real_parent->sighand->siglock);
10431045
psig = p->real_parent->signal;
10441046
sig = p->signal;
1047+
write_seqlock(&psig->stats_lock);
10451048
psig->cutime += tgutime + sig->cutime;
10461049
psig->cstime += tgstime + sig->cstime;
10471050
psig->cgtime += task_gtime(p) + sig->gtime + sig->cgtime;
@@ -1064,6 +1067,7 @@ static int wait_task_zombie(struct wait_opts *wo, struct task_struct *p)
10641067
psig->cmaxrss = maxrss;
10651068
task_io_accounting_add(&psig->ioac, &p->ioac);
10661069
task_io_accounting_add(&psig->ioac, &sig->ioac);
1070+
write_sequnlock(&psig->stats_lock);
10671071
spin_unlock_irq(&p->real_parent->sighand->siglock);
10681072
}
10691073

‎kernel/fork.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1068,6 +1068,7 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
10681068
sig->curr_target = tsk;
10691069
init_sigpending(&sig->shared_pending);
10701070
INIT_LIST_HEAD(&sig->posix_timers);
1071+
seqlock_init(&sig->stats_lock);
10711072

10721073
hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
10731074
sig->real_timer.function = it_real_fn;

‎kernel/sched/cputime.c‎

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -288,18 +288,28 @@ void thread_group_cputime(struct task_struct *tsk, struct task_cputime *times)
288288
struct signal_struct *sig = tsk->signal;
289289
cputime_t utime, stime;
290290
struct task_struct *t;
291-
292-
times->utime = sig->utime;
293-
times->stime = sig->stime;
294-
times->sum_exec_runtime = sig->sum_sched_runtime;
291+
unsigned int seq, nextseq;
295292

296293
rcu_read_lock();
297-
for_each_thread(tsk, t) {
298-
task_cputime(t, &utime, &stime);
299-
times->utime += utime;
300-
times->stime += stime;
301-
times->sum_exec_runtime += task_sched_runtime(t);
302-
}
294+
/* Attempt a lockless read on the first round. */
295+
nextseq = 0;
296+
do {
297+
seq = nextseq;
298+
read_seqbegin_or_lock(&sig->stats_lock, &seq);
299+
times->utime = sig->utime;
300+
times->stime = sig->stime;
301+
times->sum_exec_runtime = sig->sum_sched_runtime;
302+
303+
for_each_thread(tsk, t) {
304+
task_cputime(t, &utime, &stime);
305+
times->utime += utime;
306+
times->stime += stime;
307+
times->sum_exec_runtime += task_sched_runtime(t);
308+
}
309+
/* If lockless access failed, take the lock. */
310+
nextseq = 1;
311+
} while (need_seqretry(&sig->stats_lock, seq));
312+
done_seqretry(&sig->stats_lock, seq);
303313
rcu_read_unlock();
304314
}
305315

@@ -611,9 +621,6 @@ void task_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
611621
cputime_adjust(&cputime, &p->prev_cputime, ut, st);
612622
}
613623

614-
/*
615-
* Must be called with siglock held.
616-
*/
617624
void thread_group_cputime_adjusted(struct task_struct *p, cputime_t *ut, cputime_t *st)
618625
{
619626
struct task_cputime cputime;

‎kernel/sys.c‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -862,11 +862,9 @@ void do_sys_times(struct tms *tms)
862862
{
863863
cputime_t tgutime, tgstime, cutime, cstime;
864864

865-
spin_lock_irq(&current->sighand->siglock);
866865
thread_group_cputime_adjusted(current, &tgutime, &tgstime);
867866
cutime = current->signal->cutime;
868867
cstime = current->signal->cstime;
869-
spin_unlock_irq(&current->sighand->siglock);
870868
tms->tms_utime = cputime_to_clock_t(tgutime);
871869
tms->tms_stime = cputime_to_clock_t(tgstime);
872870
tms->tms_cutime = cputime_to_clock_t(cutime);

‎kernel/time/posix-cpu-timers.c‎

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -272,22 +272,8 @@ static int posix_cpu_clock_get_task(struct task_struct *tsk,
272272
if (same_thread_group(tsk, current))
273273
err = cpu_clock_sample(which_clock, tsk, &rtn);
274274
} else {
275-
unsigned long flags;
276-
struct sighand_struct *sighand;
277-
278-
/*
279-
* while_each_thread() is not yet entirely RCU safe,
280-
* keep locking the group while sampling process
281-
* clock for now.
282-
*/
283-
sighand = lock_task_sighand(tsk, &flags);
284-
if (!sighand)
285-
return err;
286-
287275
if (tsk == current || thread_group_leader(tsk))
288276
err = cpu_clock_sample_group(which_clock, tsk, &rtn);
289-
290-
unlock_task_sighand(tsk, &flags);
291277
}
292278

293279
if (!err)

0 commit comments

Comments
 (0)