From mboxrd@z Thu Jan 1 00:00:00 1970 From: Peter Zijlstra Subject: Re: [PATCH V5] powercap/drivers/idle_injection: Add an idle injection framework Date: Thu, 7 Jun 2018 11:32:01 +0200 Message-ID: <20180607093201.GL12198@hirez.programming.kicks-ass.net> References: <1528190208-22915-1-git-send-email-daniel.lezcano@linaro.org> <20180606122357.GN12258@hirez.programming.kicks-ass.net> <22f5cf0b-049e-7938-55f6-4b4b154f8389@linaro.org> <20180606150203.GE12180@hirez.programming.kicks-ass.net> <20180607083229.GJ12198@hirez.programming.kicks-ass.net> <20180607084251.rv2tg3kgz4aohlpd@vireshk-i7> <9996fb40-c7aa-db61-5445-52c146f44d85@linaro.org> <20180607084921.toctrooftl6y7kkx@vireshk-i7> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Return-path: Content-Disposition: inline In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org To: Daniel Lezcano Cc: Viresh Kumar , rjw@rjwysocki.net, linux-kernel@vger.kernel.org, Eduardo Valentin , Javi Merino , Leo Yan , Kevin Wangtao , Vincent Guittot , Rui Zhang , Daniel Thompson , "open list:POWER MANAGEMENT CORE" List-Id: linux-pm@vger.kernel.org On Thu, Jun 07, 2018 at 11:09:13AM +0200, Daniel Lezcano wrote: > On 07/06/2018 10:49, Viresh Kumar wrote: > > On 07-06-18, 10:46, Daniel Lezcano wrote: > >> Yes, correct. > >> > >> But if we don't care about who wins to store to value, is there a risk > >> of scramble variable if we just assign a value ? > > > > Normally no, as the compiler wouldn't screw it up badly. But there is no rule > > which stops the compiler from doing this: > > > > idle_duration_ms = 5; > > idle_duration_ms = -5; > > idle_duration_ms = 0; > > idle_duration_ms = ; > > > > So we *must* use READ/WRITE_ONCE() to make sure garbage values aren't seen by > > readers. > > Ok understood. Why would a compiler do this kind of things ? I think the above can happen when the compiler uses the variable as a scratch pad -- very rare I would say. In general a compiler needs to proof that doing this makes no observable difference ("as-if" rule). And since it is a regular variable it can assume data-race-free and do the above (or something like that). Because if there is a data-race it is UB and it can still do whatever it pleases. And here I think the point is that regular variables are considered only in the context of a single linear execution context. Locks are assumed to bound observability. And here the "volatile" and "_atomic" type specifiers again tell the compiler something 'special' is going on and you should not muck with things.