Received: by 10.223.185.111 with SMTP id b44csp531710wrg; Fri, 9 Mar 2018 08:59:26 -0800 (PST) X-Google-Smtp-Source: AG47ELtYUHHlj0b85PM6+iGC6L1fQnoWCYSmjpuMSb5Tdn9giKwnNS/nVYLNLgT1VQt1cqRKwoZK X-Received: by 2002:a17:902:8a89:: with SMTP id p9-v6mr28278415plo.379.1520614766292; Fri, 09 Mar 2018 08:59:26 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520614766; cv=none; d=google.com; s=arc-20160816; b=mQYH6JT/sFa5z08mFVpSgZ5r+x2B2p1sEkv3GXoYz9KiT5o+sBpAWZxkwRKRrjuAX8 DKajItfLe31kQ2lISpPd/jbFbmCTdfBgZxhRWmSwLXpzNev/82OJpWcHdwai6QS0cYXo 4j/+hqSdL74qVY6lRvfeY5TEMX5MNUyfognpSFfzOGBFb5uQ16Cuf0c9NTMgzxqplA2n MIXhTONnaazr2iG6NjNbNA0tXO2h7Qn+ZGRSmamUiNPWB/R9HwNnxXJY7/+pCCORAV/l fLNBUDCaX8ZPjbkCBpX2DbL1jx7qlgkLex2SD+WTdrSh07vwNGxzzSi+f9F7tytteWH1 2CLA== 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=ilPdKEfy6Eis/2xSIg0X3Zw2ssNyQ4QZ1PuZGuckggQ=; b=x4+R0fP/ON+pzJq3/fhGnPp+ufWdiNPVwsshZYohtAbf5ytgSWz/xxz07Qx23clGKp UOsZ7Hx8o8S2NVSnKvkNUv9OAVj4Xd6gM9BrgVPWHK5APWckIgAl9pcivCfq6vHKnRqf v6g4EG4mpnPJi0qQCkGJeUW/hkbH8YUTj0IziGeZbk92WE1R28cgnr8Sf20utsF7qAWB VHCOL3c/a5QFVRTBnIFGQ2rFYrXn08pe6N1LT3UQhLr9Faekplk8RHOWOIGN2+AL789X eyoitHJyMZQQ6ZEoInrKV4ZZXSpPlknuqLUluZw6dTIKNZJOE+sqMfMgBt8UjWBi3Lwu fefg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Fg54lP38; 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 z22si1125293pfa.4.2018.03.09.08.59.11; Fri, 09 Mar 2018 08:59:26 -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=Fg54lP38; 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 S932087AbeCIQ6I (ORCPT + 99 others); Fri, 9 Mar 2018 11:58:08 -0500 Received: from mail-wr0-f194.google.com ([209.85.128.194]:38162 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751211AbeCIQ6G (ORCPT ); Fri, 9 Mar 2018 11:58:06 -0500 Received: by mail-wr0-f194.google.com with SMTP id n7so9657763wrn.5 for ; Fri, 09 Mar 2018 08:58:05 -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=ilPdKEfy6Eis/2xSIg0X3Zw2ssNyQ4QZ1PuZGuckggQ=; b=Fg54lP38X2IR4k/gH3PIdkTwOFfeW/kSEuUhNg0DwhswZTMVQqFn0C3RVC4TyBgJ+t GE33EHE7IDZCnoZiQEklwSFwdgDKuQIYOIjYG/lLTsHA/2LiMF6MKiyLe75zBPfna2e0 qeaUYIlaO4/UUI8bDvOQzrD5Oz6wcr+HU88tZWzjJcNQyli/SsUTeDDrfB08irVN+Ny3 +zOAyFc+6tY5w4HcINsnorizuBr0glj7XSP6NduCIi9ir0vMgGC/ig2jvZsl3S7HlT8O WHHCjt570kpCMkKm/H4UKiHI4PSng6HTntUgpdw4YfLY8UqXFA/Lc8rhXgDfcHCEZ1zY 7oTw== 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=ilPdKEfy6Eis/2xSIg0X3Zw2ssNyQ4QZ1PuZGuckggQ=; b=glrlbOj5uWKWSXjlRvN2/V7+Xc/kGKN+aBbZdrGM2HpxtfzQOzSjReFd9xQQ8TMMUa nNscv8Cr01IaxpBV/ah6AmzCLmtiF7BAhX3tiV+pb1f99yYj+MpmSJ2U9N23HXdzRKq8 /wqQT1XFjNMgLH7e2FDnWIzAwlz+bpCVC/9qrezjcGw7Wh91+8RMcFChmbFAICzU6Zi1 N6gddHvJcFBlVfcAm59+GVY8U7DZWz1HbPhTfjye1HWINm+YTCcxcpSpbNhXmRTi3DH9 tqTrwPeOxpJ9spYKbZZiRYthB0VMikOjB4Oh+tc0+FPxyemEQOccTNMaiJswYpgVFT9W 1pxA== X-Gm-Message-State: APf1xPCZV24EfXEivsXnV68CtJONNJprmzoflapqJRoaZ/tQoAnHjMVK IogdGbRWpxcRdss67vAj/lw= X-Received: by 10.223.141.200 with SMTP id o66mr25656527wrb.39.1520614684975; Fri, 09 Mar 2018 08:58:04 -0800 (PST) Received: from andrea (85.100.broadband17.iol.cz. [109.80.100.85]) by smtp.gmail.com with ESMTPSA id g96sm1535263wrd.73.2018.03.09.08.58.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 09 Mar 2018 08:58:04 -0800 (PST) Date: Fri, 9 Mar 2018 17:57:58 +0100 From: Andrea Parri To: Alan Stern Cc: Palmer Dabbelt , Albert Ou , Daniel Lustig , Will Deacon , Peter Zijlstra , Boqun Feng , Nicholas Piggin , David Howells , Jade Alglave , Luc Maranget , Paul McKenney , Akira Yokosawa , Ingo Molnar , Linus Torvalds , linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 2/2] riscv/atomic: Strengthen implementations with fences Message-ID: <20180309165758.GA24626@andrea> References: <1520597620-16650-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: 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 Fri, Mar 09, 2018 at 11:39:11AM -0500, Alan Stern wrote: > On Fri, 9 Mar 2018, Andrea Parri wrote: > > > Atomics present the same issue with locking: release and acquire > > variants need to be strengthened to meet the constraints defined > > by the Linux-kernel memory consistency model [1]. > > > > Atomics present a further issue: implementations of atomics such > > as atomic_cmpxchg() and atomic_add_unless() rely on LR/SC pairs, > > which do not give full-ordering with .aqrl; for example, current > > implementations allow the "lr-sc-aqrl-pair-vs-full-barrier" test > > below to end up with the state indicated in the "exists" clause. > > > > In order to "synchronize" LKMM and RISC-V's implementation, this > > commit strengthens the implementations of the atomics operations > > by replacing .rl and .aq with the use of ("lightweigth") fences, > > and by replacing .aqrl LR/SC pairs in sequences such as: > > > > 0: lr.w.aqrl %0, %addr > > bne %0, %old, 1f > > ... > > sc.w.aqrl %1, %new, %addr > > bnez %1, 0b > > 1: > > > > with sequences of the form: > > > > 0: lr.w %0, %addr > > bne %0, %old, 1f > > ... > > sc.w.rl %1, %new, %addr /* SC-release */ > > bnez %1, 0b > > fence rw, rw /* "full" fence */ > > 1: > > > > following Daniel's suggestion. > > > > These modifications were validated with simulation of the RISC-V > > memory consistency model. > > > > C lr-sc-aqrl-pair-vs-full-barrier > > > > {} > > > > P0(int *x, int *y, atomic_t *u) > > { > > int r0; > > int r1; > > > > WRITE_ONCE(*x, 1); > > r0 = atomic_cmpxchg(u, 0, 1); > > r1 = READ_ONCE(*y); > > } > > > > P1(int *x, int *y, atomic_t *v) > > { > > int r0; > > int r1; > > > > WRITE_ONCE(*y, 1); > > r0 = atomic_cmpxchg(v, 0, 1); > > r1 = READ_ONCE(*x); > > } > > > > exists (u=1 /\ v=1 /\ 0:r1=0 /\ 1:r1=0) > > There's another aspect to this imposed by the LKMM, and I'm not sure > whether your patch addresses it. You add a fence after the cmpxchg > operation but nothing before it. So what would happen with the > following litmus test (which the LKMM forbids)? Available RISC-V memory model formalizations forbid it; an intuitive explanation could probably be derived by paralleling the argument for arm64, as pointed out by Daniel at: https://marc.info/?l=linux-kernel&m=151994289015267&w=2 Andrea > > C SB-atomic_cmpxchg-mb > > {} > > P0(int *x, int *y) > { > int r0; > > WRITE_ONCE(*x, 1); > r0 = atomic_cmpxchg(y, 0, 0); > } > > P1(int *x, int *y) > { > int r1; > > WRITE_ONCE(*y, 1); > smp_mb(); > r1 = READ_ONCE(*x); > } > > exists (0:r0=0 /\ 1:r1=0) > > This is yet another illustration showing that full fences are stronger > than cominations of release + acquire. > > Alan Stern >