From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1756864AbcB0Wxy (ORCPT ); Sat, 27 Feb 2016 17:53:54 -0500 Received: from e18.ny.us.ibm.com ([129.33.205.208]:59790 "EHLO e18.ny.us.ibm.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1756728AbcB0Wxx (ORCPT ); Sat, 27 Feb 2016 17:53:53 -0500 X-IBM-Helo: d01dlp03.pok.ibm.com X-IBM-MailFrom: paulmck@linux.vnet.ibm.com X-IBM-RcptTo: linux-kernel@vger.kernel.org Date: Sat, 27 Feb 2016 14:53:47 -0800 From: "Paul E. McKenney" To: Sergey Fedorov Cc: linux-kernel@vger.kernel.org Subject: Re: Documentation/memory-barriers.txt: How can READ_ONCE() and WRITE_ONCE() provide cache coherence? Message-ID: <20160227225347.GV3522@linux.vnet.ibm.com> Reply-To: paulmck@linux.vnet.ibm.com References: <56D0C02D.6000905@gmail.com> <20160226213133.GI3522@linux.vnet.ibm.com> <56D2034C.4010803@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <56D2034C.4010803@gmail.com> User-Agent: Mutt/1.5.21 (2010-09-15) X-TM-AS-MML: disable X-Content-Scanned: Fidelis XPS MAILER x-cbid: 16022722-0045-0000-0000-000003729937 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Feb 27, 2016 at 11:13:00PM +0300, Sergey Fedorov wrote: > On 27.02.2016 00:31, Paul E. McKenney wrote: > >Without READ_ONCE(), common sub-expression elimination optimizations > >can cause later reads of a given variable to see older value than > >previous reads did. For a (silly) example: > > > > a = complicated_pure_function(x); > > b = x; > > c = complicated_pure_function(x); > > > >The compiler is within its rights to transform this into the following: > > > > a = complicated_pure_function(x); > > b = x; > > c = a(x); > > > >In this case, the assignment to b might see a newer value of x than did > >the later assignment to c. This violates cache coherence, which states > >that all reads from a given variable must agree on the order of values > >taken on by that variable. > > I see how READ_ONCE() and WRITE_ONCE() can prevent compiler from > speculating on variable values and optimizing memory accesses. But > concerning cache coherency itself, my understanding is that software > can really ensure hardware cache coherency by using one of the > following methods: > - by not using the caches > - by using some sort of cache maintenance instructions > - by using hardware cache coherency mechanisms (which is what > normally used) > > What kind of "cache coherency" do you mean? All current systems supporting Linux guarantee that volatile accesses to a given single variable will be seen in order, even when caches are active, and without using any cache-coherence instructions. Note "a given single variable". If there is more than one variable in play, explicit memory ordering is required. The "volatile" is also important, because the compiler (and in a few cases, the hardware) can reorder non-volatile accesses. Thanx, Paul