From owner-freebsd-hackers@FreeBSD.ORG Sat Jan 4 23:29:17 2014 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id 4D22AF33; Sat, 4 Jan 2014 23:29:17 +0000 (UTC) Received: from lath.rinet.ru (lath.rinet.ru [195.54.192.90]) by mx1.freebsd.org (Postfix) with ESMTP id 09FE01A61; Sat, 4 Jan 2014 23:29:16 +0000 (UTC) Received: by lath.rinet.ru (Postfix, from userid 222) id 604588210; Sun, 5 Jan 2014 03:29:10 +0400 (MSK) Date: Sun, 5 Jan 2014 03:29:10 +0400 From: Oleg Bulyzhin To: Konstantin Belousov Subject: Re: atomic_load_acq @ i386/amd64 Message-ID: <20140104232910.GA12331@lath.rinet.ru> References: <20140103205159.GA99722@lath.rinet.ru> <20140104172923.GY59496@kib.kiev.ua> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20140104172923.GY59496@kib.kiev.ua> User-Agent: Mutt/1.5.21 (2010-09-15) Cc: freebsd-hackers@freebsd.org, Oleg Bulyzhin X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 04 Jan 2014 23:29:17 -0000 On Sat, Jan 04, 2014 at 07:29:23PM +0200, Konstantin Belousov wrote: > On Sat, Jan 04, 2014 at 12:51:59AM +0400, Oleg Bulyzhin wrote: > > > > Hello. > > > > I've got a question: why atomic_load_acq_* implemented on i386/amd64 archs > > with locked cmpxchg instruction? Comment about this > > (in /sys/(amd64|i386)/include/atomic.h) looks wrong for me. I believe > > acquire/release semantics does not require StoreLoad barrier so simple aligned > > load should be enough. (because acquire/release semantics does not guarantee > > sequential consistency). > > You did not explicitely wrote which statement in the comment is false, in > your opinion. > > FreeBSD assumes a property of _acq/_rel stuff which is sometimes called > 'total lock ordering'. It is indeed sort of sequential consistency, but > only for atomic+membar ops. Would atomic_load_acq() implemented as plain > load, it can pass stores, in particular stores from the _rel op, which > breaks the guarantee. > > For x86, there are indeed two possible schemes for implementing critical > section, one is lock cmpxchg for get(), and plain store for release(), > which is what we use. Another is plain load for get(), and xchg for > release(). Then, the load_acq() must be adopted to not break the acq/rel > consistency, and since we use plain store for release(), load_acq must > use serialing instruction. Perhaps i was not clear enough, i'm talking about this one: "However, loads may pass stores, so for atomic_load_acq we have to ensure a Store/Load barrier to do the load in SMP kernels." As far as i know acquire/release semantics guarantees following: if we have this code _acq _rel following statements are true: 1) cannot leave (due to reordering) acq/rel block 2) may leak past _acq 3) may leak before _rel So neither _acq nor _rel requires full membar. I.e. op_acq is: up reordering is prohibited> op_rel is: down reordering is prohibited> Intel documentation says about only thing (for simple load/stores) can be reordered: "Reads may be reordered with older writes to different locations but not with older writes to the same location." So, if older store can pass our load_acq() it would not break requirements. And i do not understand how load op from load_acq() can pass store op from store_rel(), intel doc says: "Writes are not reordered with older reads". Well, while writing this email i realized what is disturbing me: it's atomic(9) "Multiple Processors" section. It claims atomics are not atomic in common MP case and says atomics are atomic @i386. It looks strange for me: 1) i guess it's not "atomic" even for i386/MP without proper membar pairing. 2) if we have acq/rel modifiers for atomics why we cannot guarantee "atomicity" for any MP arch? P.S. please correct me if i'm wrong in my statements, i'm spending my new year holidays for ignorance elimination. ;) -- Oleg. ================================================================ === Oleg Bulyzhin -- OBUL-RIPN -- OBUL-RIPE -- oleg@rinet.ru === ================================================================