Received: by 10.223.164.202 with SMTP id h10csp119719wrb; Thu, 30 Nov 2017 07:48:29 -0800 (PST) X-Google-Smtp-Source: AGs4zMaDtWbNl+oothgGbNiPyAo5QTgZe+USBcJgpbNrlGCfch8kZnQFCw0xa/Ea0wvTvyLimUCL X-Received: by 10.99.95.13 with SMTP id t13mr2737829pgb.448.1512056909133; Thu, 30 Nov 2017 07:48:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1512056909; cv=none; d=google.com; s=arc-20160816; b=QZ1tZqgJLxWmONyimvctcfOcH8HNPl1uif3f/gNPVv4lCCb9FZ4Z1hNonaox6RjPgw kT4yv7oOiBYbCslAXP62vnCut0OwIgtcup0D9YUpQa9n8HWQ/lDKF9ZuyXfH+l67F55f iA9X8mScZHY9QuV1tg4+Xis4TvyHp5CsC1kJ9BuTaGSINFOnSVvPj2pafgk+1cDnqcdK 9Jt6PQa2W2gYFDf43Pe6QwN2FCaV6QMT+CT/I4qD0qKrzVVYy/FmiFbinGbn6tCybt5p GZR2f+nnKY5JQl4ubfettL/j7LW2oz1OGsYfb79+5nJN7DJf/dO2KNNPB9cBxVJR+4Qh InNA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:in-reply-to :subject:cc:to:from:date:arc-authentication-results; bh=896nGTIqDyXApLeWn0P95YPQ/tmyENbfRm6DXz/J5bI=; b=LMwEesarhtopCksromEn1JJgfhLPvNxu1in7RW1g3S3bIrrOoBLelvYX+1hjgg45RW bnWUA44KmNZFEkxX7/FxgCGiWuEywIaLSLE1AIIMrJMbQ9eA62gC7mC2zFEyZx590awE kb7sCQPQ26GFLALKIP2iyzoGigjitY5OXi5qSJr0V0kVTve2sz9sXXNjicV2b3S2/nDx vgIft675wU5xeXYVgR9VBz1eS+nymfY0jd21K1sC+kc6WBstfjo1XCU0/gfXVKONPS1F tuqxXFYOImgprbxO6a/w5F20L6BcVvxtvTkLtbpbX62GeLQlVPlHLpgunEWhw3YPFxyz vCqQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id y3si3158471pgp.554.2017.11.30.07.48.15; Thu, 30 Nov 2017 07:48:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=harvard.edu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752369AbdK3PqY (ORCPT + 99 others); Thu, 30 Nov 2017 10:46:24 -0500 Received: from iolanthe.rowland.org ([192.131.102.54]:34690 "HELO iolanthe.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S1751267AbdK3PqX (ORCPT ); Thu, 30 Nov 2017 10:46:23 -0500 Received: (qmail 2382 invoked by uid 2102); 30 Nov 2017 10:46:22 -0500 Received: from localhost (sendmail-bs@127.0.0.1) by localhost with SMTP; 30 Nov 2017 10:46:22 -0500 Date: Thu, 30 Nov 2017 10:46:22 -0500 (EST) From: Alan Stern X-X-Sender: stern@iolanthe.rowland.org To: Boqun Feng 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 In-Reply-To: <20171130085509.GA9516@tardis> Message-ID: MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 30 Nov 2017, Boqun Feng wrote: > On Wed, Nov 29, 2017 at 02:44:37PM -0500, Alan Stern wrote: > > On Wed, 29 Nov 2017, Daniel Lustig wrote: > > > > > 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: > > > > > > "MP+wmb+xchg-acq" (or some such) > > > > > > {} > > > > > > P0(int *x, int *y) > > > { > > > WRITE_ONCE(*x, 1); > > > smp_wmb(); > > > WRITE_ONCE(*y, 1); > > > } > > > > > > P1(int *x, int *y) > > > { > > > r1 = atomic_xchg_relaxed(y, 2); > > > r2 = smp_load_acquire(y); > > > r3 = READ_ONCE(*x); > > > } > > > > > > exists (1:r1=1 /\ 1:r2=2 /\ 1:r3=0) > > > > > > C/C++ would call the atomic_xchg_relaxed part of a release sequence > > > and hence would forbid this outcome. > > > > > > x86 and Power would forbid this. ARM forbids this via a special-case > > > rule in the memory model, ordering atomics with later load-acquires. > > > > > > 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. > > > > > > 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. > > > > > > Have you all thought about this test before? Any idea which way you > > > are leaning regarding the outcome above? > > > > 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. > > > > On the other hand, herd isn't careful enough in the way it implements > > internal dependencies for RMW operations. If we change > > atomic_xchg_relaxed(y, 2) to atomic_inc(y) and remove r1 from the test: > > > > C MP+wmb+inc-acq > > > > {} > > > > P0(int *x, int *y) > > { > > WRITE_ONCE(*x, 1); > > smp_wmb(); > > WRITE_ONCE(*y, 1); > > } > > > > P1(int *x, int *y) > > { > > atomic_inc(y); > > r2 = smp_load_acquire(y); > > r3 = READ_ONCE(*x); > > } > > > > exists (1:r2=2 /\ 1:r3=0) > > > > 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. > > > > (Although the smp_load_acquire is allowed to execute before the write > > part of the atomic_inc, it cannot execute before the read part. I > > think a similar argument applies even on ARM.) > > > > 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? Firstly, sending the addition request to the memory controller _is_ a write operation. Secondly, even though the CPU hardware might not execute a read operation during an AMO, the LKMM and herd nevertheless represent the atomic update as a specially-annotated read event followed by a write event. In an other-multicopy-atomic system, P0's write to y must become visible to P1 before P1 executes the smp_load_acquire, because the write was visible to the memory controller when the controller carried out the AMO, and the write becomes visible to the memory controller and to P1 at the same time (by other-multicopy-atomicity). That's why I said the test would be forbidden on ARM. But even on a non-other-multicopy-atomic system, there has to be some synchronization between the memory controller and P1's CPU. Otherwise, how could the system guarantee that P1's smp_load_acquire would see the post-increment value of y? It seems reasonable to assume that this synchronization would also cause P1 to see x=1. Alan Stern From 1585504876242195086@xxx Thu Nov 30 15:21:19 +0000 2017 X-GM-THRID: 1585255508732072548 X-Gmail-Labels: Inbox,Category Forums,HistoricalUnread