Re: [PATCH v2] sched, timer: Use atomics for thread_group_cputimer to improve scalability

Oleg Nesterov
2015-03-02T19:46:09Z

On 03/02, Oleg Nesterov wrote:
>
> Well, I forgot everything about this code, but let me ask anyway ;)
>
> On 03/02, Jason Low wrote:
> >
> > -static void update_gt_cputime(struct task_cputime *a, struct task_cputime *b)
> > +static inline void __update_gt_cputime(atomic64_t *cputime, u64 sum_cputime)
> > {
> > - if (b->utime > a->utime)
> > - a->utime = b->utime;
> > -
> > - if (b->stime > a->stime)
> > - a->stime = b->stime;
> > + u64 curr_cputime;
> > + /*
> > + * Set cputime to sum_cputime if sum_cputime > cputime. Use cmpxchg
> > + * to avoid race conditions with concurrent updates to cputime.
> > + */
> > +retry:
> > + curr_cputime = atomic64_read(cputime);
> > + if (sum_cputime > curr_cputime) {
> > + if (atomic64_cmpxchg(cputime, curr_cputime, sum_cputime) != curr_cputime)
> > + goto retry;
> > + }
> > +}
> >
> > - if (b->sum_exec_runtime > a->sum_exec_runtime)
> > - a->sum_exec_runtime = b->sum_exec_runtime;
> > +static void update_gt_cputime(struct thread_group_cputimer *cputimer, struct task_cputime *sum)
> > +{
> > + __update_gt_cputime(&cputimer->utime, sum->utime);
> > + __update_gt_cputime(&cputimer->stime, sum->stime);
> > + __update_gt_cputime(&cputimer->sum_exec_runtime, sum->sum_exec_runtime);
> > }
>
> And this is called if !cputimer_running().
>
> So who else can update these atomic64_t's ? The caller is called under ->siglock.
> IOW, do we really need to cmpxchg/retry ?
>
> Just curious, I am sure I missed something.

Ah, sorry, I seem to understand.

We still can race with account_group_*time() even if ->running == 0. Because
(say) account_group_exec_runtime() can race with 1 -> 0 -> 1 transition.

Or is there another reason?

Oleg.