From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755444AbcFQJ56 (ORCPT ); Fri, 17 Jun 2016 05:57:58 -0400 Received: from mga09.intel.com ([134.134.136.24]:63659 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753362AbcFQJ55 (ORCPT ); Fri, 17 Jun 2016 05:57:57 -0400 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.26,483,1459839600"; d="scan'208";a="829994597" Date: Fri, 17 Jun 2016 10:01:09 +0800 From: Yuyang Du To: Peter Zijlstra Cc: Chris Wilson , Andrey Ryabinin , Linus Torvalds , Mike Galbraith , Thomas Gleixner , bsegall@google.com, morten.rasmussen@arm.com, pjt@google.com, steve.muckle@linaro.org, linux-kernel@vger.kernel.org, kernel@kyup.com Subject: Re: [PATCH] sched/fair: Fix cfs_rq avg tracking underflow Message-ID: <20160617020109.GQ8105@intel.com> References: <20160609090142.GS32344@nuc-i3427.alporthouse.com> <20160609013324.GH8105@intel.com> <20160609130750.GQ30909@twins.programming.kicks-ass.net> <20160616085040.GF30927@twins.programming.kicks-ass.net> <20160616122504.GG30927@twins.programming.kicks-ass.net> <20160617091948.GJ30927@twins.programming.kicks-ass.net> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20160617091948.GJ30927@twins.programming.kicks-ass.net> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 17, 2016 at 11:19:48AM +0200, Peter Zijlstra wrote: > +/* > + * Unsigned subtract and clamp on underflow. > + * > + * Explicitly do a load-store to ensure the intermediate value never hits > + * memory. This allows lockless observations without ever seeing the negative > + * values. > + */ > +#define sub_positive(_ptr, _val) do { \ > + typeof(_ptr) ptr = (_ptr); \ > + typeof(*ptr) val = (_val); \ > + typeof(*ptr) res, var = READ_ONCE(*ptr); \ > + res = var - val; \ > + if (res > var) \ > + res = 0; \ > + WRITE_ONCE(*ptr, res); \ > +} while (0) > + maybe sub_nonnegative() or sub_til_zero() ... I wonder whether it is the if statement finally allows GCC to 'registerize' load_avg, and I almost tried it...