From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751810AbdK3IxU (ORCPT ); Thu, 30 Nov 2017 03:53:20 -0500 Received: from mail-wm0-f47.google.com ([74.125.82.47]:36186 "EHLO mail-wm0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751178AbdK3IxS (ORCPT ); Thu, 30 Nov 2017 03:53:18 -0500 X-Google-Smtp-Source: AGs4zMak0TcKzE7fXxhaX565bBzM3T3buT9gwGXUrhK3KZ0F57LS5zcRJaHXH3sU8TY+qqAQ9rtnYQ== X-ME-Sender: Date: Thu, 30 Nov 2017 16:55:09 +0800 From: Boqun Feng To: Alan Stern Cc: Daniel Lustig , "Paul E. McKenney" , Andrea Parri , Luc Maranget , Jade Alglave , Nicholas Piggin , Peter Zijlstra , Will Deacon , David Howells , Palmer Dabbelt , Kernel development list Subject: Re: Unlock-lock questions and the Linux Kernel Memory Model Message-ID: <20171130085509.GA9516@tardis> References: <17506ed0-1ce8-791d-7cf1-c40426015a99@nvidia.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="qDbXVdCdHGoSgWSk" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.9.1 (2017-09-22) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --qDbXVdCdHGoSgWSk Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Nov 29, 2017 at 02:44:37PM -0500, Alan Stern wrote: > On Wed, 29 Nov 2017, Daniel Lustig wrote: >=20 > > While we're here, let me ask about another test which isn't directly > > about unlock/lock but which is still somewhat related to this > > discussion: > >=20 > > "MP+wmb+xchg-acq" (or some such) > >=20 > > {} > >=20 > > P0(int *x, int *y) > > { > > WRITE_ONCE(*x, 1); > > smp_wmb(); > > WRITE_ONCE(*y, 1); > > } > >=20 > > P1(int *x, int *y) > > { > > r1 =3D atomic_xchg_relaxed(y, 2); > > r2 =3D smp_load_acquire(y); > > r3 =3D READ_ONCE(*x); > > } > >=20 > > exists (1:r1=3D1 /\ 1:r2=3D2 /\ 1:r3=3D0) > >=20 > > C/C++ would call the atomic_xchg_relaxed part of a release sequence > > and hence would forbid this outcome. > >=20 > > x86 and Power would forbid this. ARM forbids this via a special-case > > rule in the memory model, ordering atomics with later load-acquires. > >=20 > > RISC-V, however, wouldn't forbid this by default using RCpc or RCsc > > atomics for smp_load_acquire(). It's an "fri; rfi" type of pattern, > > because xchg doesn't have an inherent internal data dependency. > >=20 > > If the Linux memory model is going to forbid this outcome, then > > RISC-V would either need to use fences instead, or maybe we'd need to > > add a special rule to our memory model similarly. This is one detail > > where RISC-V is still actively deciding what to do. > >=20 > > Have you all thought about this test before? Any idea which way you > > are leaning regarding the outcome above? >=20 > Good questions. Currently the LKMM allows this, and I think it should > because xchg doesn't have a dependency from its read to its write. >=20 > On the other hand, herd isn't careful enough in the way it implements=20 > internal dependencies for RMW operations. If we change=20 > atomic_xchg_relaxed(y, 2) to atomic_inc(y) and remove r1 from the test: >=20 > C MP+wmb+inc-acq >=20 > {} >=20 > P0(int *x, int *y) > { > WRITE_ONCE(*x, 1); > smp_wmb(); > WRITE_ONCE(*y, 1); > } >=20 > P1(int *x, int *y) > { > atomic_inc(y); > r2 =3D smp_load_acquire(y); > r3 =3D READ_ONCE(*x); > } >=20 > exists (1:r2=3D2 /\ 1:r3=3D0) >=20 > then the test _should_ be forbidden, but it isn't -- herd doesn't > realize that all atomic RMW operations other than xchg must have a > dependency (either data or control) between their internal read and > write. >=20 > (Although the smp_load_acquire is allowed to execute before the write=20 > part of the atomic_inc, it cannot execute before the read part. I=20 > think a similar argument applies even on ARM.) >=20 But in case of AMOs, which directly send the addition request to memory controller, so there wouldn't be any read part or even write part of the atomic_inc() executed by CPU. Would this be allowed then? Regards, Boqun > Luc, consider this a bug report. :-) >=20 > Alan >=20 --qDbXVdCdHGoSgWSk Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCAAdFiEEj5IosQTPz8XU1wRHSXnow7UH+rgFAlofx2oACgkQSXnow7UH +rjpeAf6As2wArXaG/77yDz2hOPT/5E9alsU3cmHDDB+dLdR6tOcOXcs+W0bsvVS EoABsDpl+nZ2W7q1bHNURCW0gp2q5uYigA/6F/ZVz+A53K/HluSkNSq8hSoh+KNI rnbh26V7EmG+TYoNG/h+YEcFqpWbfjD4+KeZn95pjxfenRaik9SaoPUwYf8VJZKj jxw/muk36lf483WJZR2LysbgNmJfC+Beqiur2cJNmAFlkwLgk0U/kv6m9DCm9FVP A7nqcHuC/c1oXNNTWRES5iC6VbCLogrRSI7s/lbrmHHto8pkaRCOfHf3sP+mOi8i +qwk63du7bmsJT7BIsShCzd4T7ZkYg== =Nu3u -----END PGP SIGNATURE----- --qDbXVdCdHGoSgWSk--