Received: by 10.223.185.116 with SMTP id b49csp4517839wrg; Tue, 6 Mar 2018 18:04:06 -0800 (PST) X-Google-Smtp-Source: AG47ELtokAyPMcLwBRwr0nBfR+iZSDynaeWMGzXrPAves5g1Oh9qrqhVovAbjIh2Ht/16zlyyNbQ X-Received: by 2002:a17:902:5401:: with SMTP id d1-v6mr18902767pli.176.1520388246555; Tue, 06 Mar 2018 18:04:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520388246; cv=none; d=google.com; s=arc-20160816; b=rt3zMH/YK5kSGkmDBKnYxL8+TVotEJXcVuNHfvSeEVA4PvuqVj6wh14xrhOB/ahWEs zy1EzYNBFbOQj4tI4O0R+cVl3SJ7RmKbTTKFsyj//sD1J8ud7r7g7mWogAQJiz02iSJz TI8pcChMmbfIxIE8O7EYfj6WorSDmq8Ny/zV8nZiMqIYbT4CNCDgTgs++GpZGpwT6eoL aORSqTI80IRZp1/kMDK00fJdazpk4F/DLo83LmS1FoUsQSPsL7Cb4VuViIfnNLCA38qC l6FDu0+rtN+mbC3kEToWB7h0xSIJmDokdqGVYy0hLRQTmlK0S01jcfanalk2/F46W/6d WV1Q== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:to:from:cc:in-reply-to:subject:date:dkim-signature :arc-authentication-results; bh=GmHeXyEa1233HdDce/1lB1JysFW2IEV/u8TXiUiC40g=; b=O7JGAGVh/HLEU8EYbOjaJxaYbrJ6A2pU2WGv1LMvfnJquE6PbWcZZfqb00pjfROalk YQn+eaFMu3xBmgy9qlS9h3QMCrIRSFkG4dqKEWStFMAai3jezoT6nDVBUseCAlXOA+hB //sQ6W1rfVe2vI59OHR4myhT/EiRXriaa4CweWBfK66Yich24oD/HejQf+iu7VSfyqmD kDiDtnyU5nCE1UcmNN+7ix4zWR9apQA60KOhn9uiyvl3B+uyMzk4VbhZZCUqGz4UnXyD B+P9LocDorCvDfXEJjn3YYZMbiTb0dLMd7iIFYE0B62E6WVQTEaDIl7LNCwlG4S0g4AB sOCg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=DFtqUmjd; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b68si12893423pfg.249.2018.03.06.18.03.52; Tue, 06 Mar 2018 18:04:06 -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=@sifive.com header.s=google header.b=DFtqUmjd; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933415AbeCGCCq (ORCPT + 99 others); Tue, 6 Mar 2018 21:02:46 -0500 Received: from mail-pl0-f65.google.com ([209.85.160.65]:39513 "EHLO mail-pl0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933190AbeCGCCa (ORCPT ); Tue, 6 Mar 2018 21:02:30 -0500 Received: by mail-pl0-f65.google.com with SMTP id s13-v6so449690plq.6 for ; Tue, 06 Mar 2018 18:02:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sifive.com; s=google; h=date:subject:in-reply-to:cc:from:to:message-id:mime-version :content-transfer-encoding; bh=GmHeXyEa1233HdDce/1lB1JysFW2IEV/u8TXiUiC40g=; b=DFtqUmjdjQ0mx3kEGAIk1bM++6+EOAOR1S7riSP+CID2VidUVlZNfbZzfvdkhTDWp8 SF0m0uhtkhF6Hw8Ky+3mI1+6g3OLQrfPxX0xKeR7RzXtAILKlzJkRYmsJU6AnuGbr9Td OKiiQAaoAw0YlUb8Lr7yGdIvd0nJF9lIfrq6vvbcp44w9j6VA+NFHBvUxjmguHyUB54u imp3VAnu3ekq6H80aPl1LpPMZwMguaa6QiOuhwiA4FGLRGVfLuByDO0outDMlwB2j8mm bGRXILlWCGB4AR1sS2ZuSiGZUDhLJnv210ynbCh5DTudx9lDt1qKJpqItXpuR5EIz5rg oowA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:subject:in-reply-to:cc:from:to:message-id :mime-version:content-transfer-encoding; bh=GmHeXyEa1233HdDce/1lB1JysFW2IEV/u8TXiUiC40g=; b=NzqiUM6W0CIAEiABAL/TgMBnsV/6QoBXPd25cLcSIl86PT/WF5bQG6Z7ZcPRPD0ad5 D7ygYDCs/qNbsmuk9o3N4TO+oWd4Bmqk+AZCiBpuH7HvYIVQbHuStCQjSj6mQY1prA/B P6Som1w9o5gwN38bjDA+g72AShU5C/2PKDcAzC4FtBFpVCein132VoiS/mG0mwqWoTc1 9ok0iUdzdPWdufrGJ0vfMM+v3Ga6pNHcngN92PCPI9c67+HwOaKu3uo2LN4sDdGuqwh8 7F9IwvMR1EnVTCbGlWsyTBH386cjnEs0J46mubp/Ue5HpKHScb7zLXb8Ur1ifF+76Kis bPxQ== X-Gm-Message-State: APf1xPDgoNkXidxU3AgShNeqsRF5L+n+R3GiHMglKRXuPnWpJ6EcdTZH 4s6BdJ7bjEewnliczF+oPva3ag== X-Received: by 2002:a17:902:66e6:: with SMTP id e93-v6mr18707871plk.312.1520388149133; Tue, 06 Mar 2018 18:02:29 -0800 (PST) Received: from localhost ([12.206.222.5]) by smtp.gmail.com with ESMTPSA id x123sm27715654pgb.5.2018.03.06.18.02.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 06 Mar 2018 18:02:28 -0800 (PST) Date: Tue, 06 Mar 2018 18:02:28 -0800 (PST) X-Google-Original-Date: Tue, 06 Mar 2018 17:58:10 PST (-0800) Subject: Re: [RFC PATCH 1/2] riscv/spinlock: Strengthen implementations with fences In-Reply-To: <1520274249-21815-1-git-send-email-parri.andrea@gmail.com> CC: albert@sifive.com, Daniel Lustig , stern@rowland.harvard.edu, Will Deacon , peterz@infradead.org, boqun.feng@gmail.com, npiggin@gmail.com, dhowells@redhat.com, j.alglave@ucl.ac.uk, luc.maranget@inria.fr, paulmck@linux.vnet.ibm.com, akiyks@gmail.com, mingo@kernel.org, Linus Torvalds , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, parri.andrea@gmail.com From: Palmer Dabbelt To: parri.andrea@gmail.com Message-ID: Mime-Version: 1.0 (MHng) Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 05 Mar 2018 10:24:09 PST (-0800), parri.andrea@gmail.com wrote: > Current implementations map locking operations using .rl and .aq > annotations. However, this mapping is unsound w.r.t. the kernel > memory consistency model (LKMM) [1]: > > Referring to the "unlock-lock-read-ordering" test reported below, > Daniel wrote: > > "I think an RCpc interpretation of .aq and .rl would in fact > allow the two normal loads in P1 to be reordered [...] > > The intuition would be that the amoswap.w.aq can forward from > the amoswap.w.rl while that's still in the store buffer, and > then the lw x3,0(x4) can also perform while the amoswap.w.rl > is still in the store buffer, all before the l1 x1,0(x2) > executes. That's not forbidden unless the amoswaps are RCsc, > unless I'm missing something. > > Likewise even if the unlock()/lock() is between two stores. > A control dependency might originate from the load part of > the amoswap.w.aq, but there still would have to be something > to ensure that this load part in fact performs after the store > part of the amoswap.w.rl performs globally, and that's not > automatic under RCpc." > > Simulation of the RISC-V memory consistency model confirmed this > expectation. > > In order to "synchronize" LKMM and RISC-V's implementation, this > commit strengthens the implementations of the locking operations > by replacing .rl and .aq with the use of ("lightweigth") fences, > resp., "fence rw, w" and "fence r , rw". > > 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(*x); > } > > exists (1:r0=1 /\ 1:r1=0) > > [1] https://marc.info/?l=linux-kernel&m=151930201102853&w=2 > https://groups.google.com/a/groups.riscv.org/forum/#!topic/isa-dev/hKywNHBkAXM > https://marc.info/?l=linux-kernel&m=151633436614259&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/fence.h | 12 ++++++++++++ > arch/riscv/include/asm/spinlock.h | 29 +++++++++++++++-------------- > 2 files changed, 27 insertions(+), 14 deletions(-) > create mode 100644 arch/riscv/include/asm/fence.h Oh, sorry about this -- I thought I'd deleted all this code, but I guess I just wrote a patch and then forgot about it. Here's my original patch, which I have marked as a WIP: commit 39908f1f8b75ae88ce44dc77b8219a94078ad298 Author: Palmer Dabbelt Date: Tue Dec 5 16:26:50 2017 -0800 RISC-V: Use generic spin and rw locks This might not be exactly the right thing to do: we could use LR/SC to produce slightly better locks by rolling the tests into the LR/SC. I'm going to defer that until I get a better handle on the new memory model and just be safe here: after some discussion I'm pretty sure the AMOs are good, and cmpxchg is safe (by being way too string). Since we'd want to rewrite the spinlocks anyway so they queue, I don't see any reason to keep the old implementations around. Signed-off-by: Palmer Dabbelt diff --git a/arch/riscv/include/asm/spinlock.h b/arch/riscv/include/asm/spinlock.h index 2fd27e8ef1fd..9b166ea81fe5 100644 --- a/arch/riscv/include/asm/spinlock.h +++ b/arch/riscv/include/asm/spinlock.h @@ -15,128 +15,7 @@ #ifndef _ASM_RISCV_SPINLOCK_H #define _ASM_RISCV_SPINLOCK_H -#include -#include - -/* - * Simple spin lock operations. These provide no fairness guarantees. - */ - -/* FIXME: Replace this with a ticket lock, like MIPS. */ - -#define arch_spin_is_locked(x) (READ_ONCE((x)->lock) != 0) - -static inline void arch_spin_unlock(arch_spinlock_t *lock) -{ - __asm__ __volatile__ ( - "amoswap.w.rl x0, x0, %0" - : "=A" (lock->lock) - :: "memory"); -} - -static inline int arch_spin_trylock(arch_spinlock_t *lock) -{ - int tmp = 1, busy; - - __asm__ __volatile__ ( - "amoswap.w.aq %0, %2, %1" - : "=r" (busy), "+A" (lock->lock) - : "r" (tmp) - : "memory"); - - return !busy; -} - -static inline void arch_spin_lock(arch_spinlock_t *lock) -{ - while (1) { - if (arch_spin_is_locked(lock)) - continue; - - if (arch_spin_trylock(lock)) - break; - } -} - -/***********************************************************/ - -static inline void arch_read_lock(arch_rwlock_t *lock) -{ - int tmp; - - __asm__ __volatile__( - "1: lr.w %1, %0\n" - " bltz %1, 1b\n" - " addi %1, %1, 1\n" - " sc.w.aq %1, %1, %0\n" - " bnez %1, 1b\n" - : "+A" (lock->lock), "=&r" (tmp) - :: "memory"); -} - -static inline void arch_write_lock(arch_rwlock_t *lock) -{ - int tmp; - - __asm__ __volatile__( - "1: lr.w %1, %0\n" - " bnez %1, 1b\n" - " li %1, -1\n" - " sc.w.aq %1, %1, %0\n" - " bnez %1, 1b\n" - : "+A" (lock->lock), "=&r" (tmp) - :: "memory"); -} - -static inline int arch_read_trylock(arch_rwlock_t *lock) -{ - int busy; - - __asm__ __volatile__( - "1: lr.w %1, %0\n" - " bltz %1, 1f\n" - " addi %1, %1, 1\n" - " sc.w.aq %1, %1, %0\n" - " bnez %1, 1b\n" - "1:\n" - : "+A" (lock->lock), "=&r" (busy) - :: "memory"); - - return !busy; -} - -static inline int arch_write_trylock(arch_rwlock_t *lock) -{ - int busy; - - __asm__ __volatile__( - "1: lr.w %1, %0\n" - " bnez %1, 1f\n" - " li %1, -1\n" - " sc.w.aq %1, %1, %0\n" - " bnez %1, 1b\n" - "1:\n" - : "+A" (lock->lock), "=&r" (busy) - :: "memory"); - - return !busy; -} - -static inline void arch_read_unlock(arch_rwlock_t *lock) -{ - __asm__ __volatile__( - "amoadd.w.rl x0, %1, %0" - : "+A" (lock->lock) - : "r" (-1) - : "memory"); -} - -static inline void arch_write_unlock(arch_rwlock_t *lock) -{ - __asm__ __volatile__ ( - "amoswap.w.rl x0, x0, %0" - : "=A" (lock->lock) - :: "memory"); -} +#include +#include #endif /* _ASM_RISCV_SPINLOCK_H */