Re: [PATCH RFC] time,signal: protect resource use statistics with seqlock

Oleg Nesterov
2014-08-15T16:39:22Z

On 08/15, Peter Zijlstra wrote:
>
> Also; why do we care about PROCESS_CPUTIME? People should really not use
> it. What are the 'valid' usecases you guys care about?

I do not really know. IIUC, the problematic usecase is sys_times().

I agree with Mike, "don't do this if you have a lot of threads". But
perhaps the kernel can help to applications which already abuse times().

However, if we only want to make sys_times() more scalable(), then
perhaps the "lockless" version of thread_group_cputime() makes more
sense. And given that do_sys_times() uses current we can simplify it;
is_dead is not possible and we do not need to take ->siglock twice:

void current_group_cputime(struct task_cputime *times)
{
struct task_struct *tsk = current, *t;
struct spinlock_t *siglock = &tsk->sighand->siglock;
struct signal_struct *sig = tsk->signal;
bool lockless = true;
u64 exec;

retry:
spin_lock_irq(siglock);
times->utime = sig->utime;
times->stime = sig->stime;
times->sum_exec_runtime = exec = sig->sum_sched_runtime;

if (lockless)
spin_unlock_irq(siglock);

rcu_read_lock();
for_each_thread(tsk, t) {
cputime_t utime, stime;
task_cputime(t, &utime, &stime);
times->utime += utime;
times->stime += stime;
times->sum_exec_runtime += task_sched_runtime(t);
}
rcu_read_unlock();

if (lockless) {
lockless = false;
spin_unlock_wait(siglock);
smp_rmb();
if (exec != sig->sum_sched_runtime)
goto retry;
} else {
spin_unlock_irq(siglock);
}
}

Oleg.