From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755354AbbCBTmx (ORCPT ); Mon, 2 Mar 2015 14:42:53 -0500 Received: from mx1.redhat.com ([209.132.183.28]:50412 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754303AbbCBTmv (ORCPT ); Mon, 2 Mar 2015 14:42:51 -0500 Date: Mon, 2 Mar 2015 20:40:33 +0100 From: Oleg Nesterov To: Jason Low Cc: Peter Zijlstra , Ingo Molnar , Linus Torvalds , "Paul E. McKenney" , Andrew Morton , Mike Galbraith , Frederic Weisbecker , Rik van Riel , Steven Rostedt , Scott Norton , Aswin Chandramouleeswaran , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] sched, timer: Use atomics for thread_group_cputimer to improve scalability Message-ID: <20150302194033.GA27914@redhat.com> References: <1425321731.5304.14.camel@j-VirtualBox> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1425321731.5304.14.camel@j-VirtualBox> User-Agent: Mutt/1.5.18 (2008-05-17) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > @@ -222,13 +239,10 @@ void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times) > * it. > */ > thread_group_cputime(tsk, &sum); > - raw_spin_lock_irqsave(&cputimer->lock, flags); > - cputimer->running = 1; > - update_gt_cputime(&cputimer->cputime, &sum); > - } else > - raw_spin_lock_irqsave(&cputimer->lock, flags); > - *times = cputimer->cputime; > - raw_spin_unlock_irqrestore(&cputimer->lock, flags); > + update_gt_cputime(cputimer, &sum); > + ACCESS_ONCE(cputimer->running) = 1; WRITE_ONCE() looks better... but it is not clear to me why do we need it at all. Oleg.