Received: by 10.223.185.116 with SMTP id b49csp1877807wrg; Thu, 22 Feb 2018 04:45:34 -0800 (PST) X-Google-Smtp-Source: AH8x226VZtwgzgVjZUcpiOUp90+kdIxI9LPtpmPnphbR2zLWk4483tlOTm1wdh94Mn2FtVEbdlNb X-Received: by 2002:a17:902:c1:: with SMTP id a59-v6mr6348260pla.284.1519303534845; Thu, 22 Feb 2018 04:45:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519303534; cv=none; d=google.com; s=arc-20160816; b=pHcwsBzyERid0x6bZoNP9fUqgZsUde6mlQfl+vIj8NJZWYQeABZsrhCWU8Ng9GsSe8 0YXyIyxTHfxnfup3QQcrKQisd+pamk3gpBFPh4OyiylhHn7c8U79PnXuc70dxuFJNGqD FHSWOMmuUdfgR0qCUGkZax3gVZ/lBVKQ6BIA7f/dYtK6wCB1leKX8loDG96xTRGseGY0 fyWyMNdH3tobxcRfUDhZAXfgoSl0YUo1HJVyex+Nqyq909634SSRvfdtUMMm6qMhIeAD wy8SJKaPlx6+2MnwdvuLQ6H+r2GHqnmgXDOe1a/RO48d6JSaWhwSffwnzY30ACRl6QGu Al0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature:arc-authentication-results; bh=KpMeQUw1UHqH20+ndd8Io4MXy3FVwzq5rIqrJ8UeEqk=; b=SZ9Gzj3mZTLd1DxT4GjBn8cPqTlCeVHFX/wt6y1BypWQpUKWcaxjl8K/kglzgsUZ0U VEZsdEG1pLsBLOyNC1xysZOA7yw90tjafixjycGb4rKCdy4Oft2iDxFH/IS3lLAZViWb c+OjulHPf2YPjxpKQuJNtiTnZ+zLP35Z2UzESToeZSFEfsNPis5aa/7N6JIl4LL9EgIO KetHkvpdukJ1+DGx7MUBjClmt7DTAWxBRDElB9Np9VX1C85XnEc/6tvwfw6721Z/rbdE XVn1+KF5ZkYN9LCOgSduiDNdy5QcyEbKuayzQHoqp1RcwYQI1V9M0vWZ0jfcOY6Qcq1G HFEg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=l/7ApKKN; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m14si2598pff.156.2018.02.22.04.45.19; Thu, 22 Feb 2018 04:45:34 -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; dkim=pass header.i=@gmail.com header.s=20161025 header.b=l/7ApKKN; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S932355AbeBVMoN (ORCPT + 99 others); Thu, 22 Feb 2018 07:44:13 -0500 Received: from mail-wm0-f65.google.com ([74.125.82.65]:54390 "EHLO mail-wm0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932167AbeBVMoL (ORCPT ); Thu, 22 Feb 2018 07:44:11 -0500 Received: by mail-wm0-f65.google.com with SMTP id z81so3653732wmb.4 for ; Thu, 22 Feb 2018 04:44:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=KpMeQUw1UHqH20+ndd8Io4MXy3FVwzq5rIqrJ8UeEqk=; b=l/7ApKKNECBFF1yjvMsil4oRIOscLmOEOX8GiB/YgQT72eVw4WLHXMXIHcqfjT/q1B UleIHZJyaLj6H0Xd7Tl3k/o7c472TzaA6Jdmvv/EXGmY0ZAbsM+dnYirriRC9BtW3aoz IrsnPtWK2KUnCJFvB4NNCTiDurQF9EJQKpbR8Uarg3KmULQAUmF2JA6tIDi2GBDhdMi8 JAcSXRATLj0hVfBprvUn+td+ahzAnHnkewI7aYF+2UR7FITWf73amcWgpyoLDyhwbsD7 N55MFcilGmcW41eYCfO1CeClr+7/bWL1TCAIBl08nh8bvDedbs1jpjCnlDHHblhyXYFJ fOhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=KpMeQUw1UHqH20+ndd8Io4MXy3FVwzq5rIqrJ8UeEqk=; b=t9f/KoVW7viGLybx41y3suSAi7c6K2yKg1m01lr/6cQn44O+Fw5XG7ebn3pILwndl7 /ClaC6oZLjFPFxTD5D+DBtzvhdElKhutwZ/1k3Rlb1plINpCigk0Xjs06HCxkAkJcO0L neRCK+g8SzSDBUpMdibAX72q0aknacd54eEy6/LA3050NwgGCJEaohZmouXAtZkXEXSf DzwkvwQlY/z3rkE75vJBWTgaCKrRCPZ/XpWNEFf7nSnqFqheDHaBlADqy2zZTthkE0i6 9wif+6EZddqrt70x1pYZzZFoc63b3n88MhmotzTKqRYQwQAbL4mYMiqSXGRPslGFuEU+ T0kg== X-Gm-Message-State: APf1xPD+PaT+uaYHpgy3GlJ0p45LBx2hwOy9K+Kx02nMqvcN42p31aNs dFZJmfO9+SZIWfbChJMcf/eKMhYt X-Received: by 10.80.159.1 with SMTP id b1mr9162162edf.132.1519303450129; Thu, 22 Feb 2018 04:44:10 -0800 (PST) Received: from andrea (85.100.broadband17.iol.cz. [109.80.100.85]) by smtp.gmail.com with ESMTPSA id v15sm249048edl.12.2018.02.22.04.44.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 22 Feb 2018 04:44:09 -0800 (PST) Date: Thu, 22 Feb 2018 13:44:03 +0100 From: Andrea Parri To: linux-kernel@vger.kernel.org Cc: Palmer Dabbelt , Albert Ou , Daniel Lustig , Alan Stern , Will Deacon , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , "Paul E. McKenney" , Akira Yokosawa , Ingo Molnar , Linus Torvalds , linux-riscv@lists.infradead.org Subject: Re: [RFC PATCH] riscv/locking: Strengthen spin_lock() and spin_unlock() Message-ID: <20180222124258.GA12446@andrea> References: <1519301990-11766-1-git-send-email-parri.andrea@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1519301990-11766-1-git-send-email-parri.andrea@gmail.com> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Feb 22, 2018 at 01:19:50PM +0100, Andrea Parri wrote: > The LKMM defines certain memory ordering constraints for spin_lock(), > spin_unlock() and other primitives that the kernel developer can rely > on; unfortunately, some of these constraints are not currently met by > the RISC-V implementation of spin_lock(), spin_unlock(). > > The following MP-like program exemplifies the issue: according to our > LKMM, program "unlock-lock-read-ordering" below can never reach state > (1:r0=1 /\ 1:r1=0). However, when we map this C program to the RISCV > program "RISCV-unlock-lock-read-ordering" below following the current > implementation, the corresponding state is reachable according to the > RISCV specification and its formalizations [2]. > > C unlock-lock-read-ordering > > {} > /* s initially owned by P1 */ > > P0(int *x, int *y) > { > WRITE_ONCE(*x, 1); > smp_wmb(); > WRITE_ONCE(*y, 1); > } > > P1(int *x, int *y, spinlock_t *s) > { > int r0; > int r1; > > r0 = READ_ONCE(*y); > spin_unlock(s); > spin_lock(s); > r1 = READ_ONCE(*y); This last read should have been from 'x' of course (and similarly in unlock-lock-write-ordering). Sorry, Andrea > } > > exists (1:r0=1 /\ 1:r1=0) > > RISCV RISCV-unlock-lock-read-ordering > { > 0:x2=x; 0:x4=y; > 1:x2=y; 1:x4=x; 1:x6=s; > s=1; > } > P0 | P1 ; > ori x1,x0,1 | lw x1,0(x2) ; > sw x1,0(x2) | amoswap.w.rl x0,x0,(x6) ; > fence w,w | ori x5,x0,1 ; > ori x3,x0,1 | amoswap.w.aq x0,x5,(x6) ; > sw x3,0(x4) | lw x3,0(x4) ; > exists > (1:x1=1 /\ 1:x3=0) > > The issue can in fact be exarcebated if, as envisaged/discussed in [3], > the LKMM will be modified to become even more "demanding" on the order- > ing constraints associated to the locking primitives. For example the > state (1:r0=1 /\ 1:r1=0) in program "unlock-lock-write-ordering" below > is currently allowed by LKMM (as is the corresponding state in "RISCV- > unlock-lock-write-ordering" below). However, proposals modifying LKMM > to _forbid_ that state have already appeared on LKML [4]. > > C unlock-lock-write-ordering > > {} > /* s initially owned by P0 */ > > P0(int *x, int *y, spinlock_t *s) > { > WRITE_ONCE(*x, 1); > spin_unlock(s); > spin_lock(s); > WRITE_ONCE(*y, 1); > } > > P1(int *x, int *y) > { > int r0; > int r1; > > r0 = READ_ONCE(*y); > smp_rmb(); > r1 = READ_ONCE(*y); > } > > exists (1:r0=1 /\ 1:r1=0) > > RISCV RISCV-unlock-lock-write-ordering > { > 0:x2=x; 0:x4=y; 0:x6=s; > 1:x2=y; 1:x4=x; > s=1; > } > P0 | P1 ; > ori x1,x0,1 | lw x1,0(x2) ; > sw x1,0(x2) | fence r,r ; > amoswap.w.rl x0,x0,(x6) | lw x3,0(x4) ; > ori x5,x0,1 | ; > amoswap.w.aq x0,x5,(x6) | ; > ori x3,x0,1 | ; > sw x3,0(x4) | ; > exists > (1:x1=1 /\ 1:x3=0) > > [Curiously, RISC-V's current implementations of smp_load_acquire() and > smp_store_release() provide way stronger ordering than what currently > required by LKMM since those're relying on the generic implementation > (c.f, also, [5]). ] > > This RFC fixes the issue by strengthening RISC-V's implementations of > spin_lock() and spin_unlock(), based on "A spinlock with fences" from > Section 2.3.5 ("Acquire/Release Ordering") of the RISC-V draft spec. > It does _not_ attempt to fix read-lock and atomics (for which, AFAICT, > similar considerations would hold). > > IMPORTANT. This patch is _NOT_ intended to be applied as is. Rather, > this is intended to test the waters, implicit questions being "Should > we take this direction?" "Are changes to LKMM needed?" (and develop a > technical discussion on the above issues.) > > [1] https://marc.info/?l=linux-kernel&m=151633436614259&w=2 > [2] https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/hKywNHBkAXM > [3] https://marc.info/?l=linux-kernel&m=151181741400461&w=2 > [4] https://marc.info/?l=linux-kernel&m=151871035014425&w=2 > [5] https://marc.info/?l=linux-kernel&m=151912186913692&w=2 > > Signed-off-by: Andrea Parri > Cc: Palmer Dabbelt > Cc: Albert Ou > Cc: Daniel Lustig > Cc: Alan Stern > Cc: Will Deacon > Cc: Peter Zijlstra > Cc: Boqun Feng > Cc: Nicholas Piggin > Cc: David Howells > Cc: Jade Alglave > Cc: Luc Maranget > Cc: "Paul E. McKenney" > Cc: Akira Yokosawa > Cc: Ingo Molnar > Cc: Linus Torvalds > Cc: linux-riscv@lists.infradead.org > Cc: linux-kernel@vger.kernel.org > --- > arch/riscv/include/asm/spinlock.h | 6 ++++-- > 1 file changed, 4 insertions(+), 2 deletions(-) > > diff --git a/arch/riscv/include/asm/spinlock.h b/arch/riscv/include/asm/spinlock.h > index 2fd27e8ef1fd6..2f89fc62c9196 100644 > --- a/arch/riscv/include/asm/spinlock.h > +++ b/arch/riscv/include/asm/spinlock.h > @@ -28,8 +28,9 @@ > > static inline void arch_spin_unlock(arch_spinlock_t *lock) > { > + RISCV_FENCE(rw,w); > __asm__ __volatile__ ( > - "amoswap.w.rl x0, x0, %0" > + "amoswap.w x0, x0, %0" > : "=A" (lock->lock) > :: "memory"); > } > @@ -39,10 +40,11 @@ static inline int arch_spin_trylock(arch_spinlock_t *lock) > int tmp = 1, busy; > > __asm__ __volatile__ ( > - "amoswap.w.aq %0, %2, %1" > + "amoswap.w %0, %2, %1" > : "=r" (busy), "+A" (lock->lock) > : "r" (tmp) > : "memory"); > + RISCV_FENCE(r,rw); > > return !busy; > } > -- > 2.7.4 >