Received: by 10.223.185.116 with SMTP id b49csp2940136wrg; Mon, 5 Mar 2018 11:07:19 -0800 (PST) X-Google-Smtp-Source: AG47ELsZjes5jvKZJKs4Uidz9rNbhH8u6p57NHyC6S0z9ic1BXyqzMm7rsxg0x8qTZybduHw0l9S X-Received: by 2002:a17:902:b28b:: with SMTP id u11-v6mr14007586plr.146.1520276839126; Mon, 05 Mar 2018 11:07:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1520276839; cv=none; d=google.com; s=arc-20160816; b=0TTbirfLbpubRkjKmMbLx2OmXZsj4XbGDuH6PyEeNLNCFF9SNf49bt+gaQQSZDISW9 FAmMi67f4sl7k9yIxXUwcpeE5wEymsRKxEXjD29uKphL4ynmLp8ZuPv9NwDmv+jhrXZ3 VtZzyfomCNd6bfv3VLrjZfZZoYTWWGydMZIEOr2jeaLik4MekWsoK5v091gRVkHT/bxV urX0ZqaXdi8K0UjzkIkinLTvOZO6ikkzX+5cHTUQWi2lrC13OfFUT/pLBxNobfZfBfV/ UWg8DGSxKNjGgDK6JWCbR43OnOA4ju6Tf8K6tV1V2y8jKHuDNlNli/XT5PSgKNOjIvdV 769Q== 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=RIb2jyugM9ZYsfVSxzeLlc0N/dyog7Auksv7dofZj1o=; b=Qv5PxRCuN7YMqzPyshFO1ggbv1aNXqB8PMCMFmwvzFayuK/zPC3slB6ROiMwwL7jtl u02soRlllPoChY2OfOGJ9nbrwB1+QQTelwvCmJSYsoP2uj4oAddO4ZPcBWBFCVdAxrZp H/iTDq2HxNo/vX973t4Jlqs6GCwJa7IIDO0X8YfznGsCWjndbxPdwaLiruad4bVANB6M q6h5CUIlxmasdxTRPAz/oRUg1lwcsozy1Gj6ggv/qm9cMrUtndmrfAQa600ll+2vm/W3 VhZDa+FAA+R41l65GrAN1/Rt1UqVWDtQ32RWZpmpD/NO4vS9CRfVFp2Winqx4lH1Bg/M sOeA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=oMHG+EIP; 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 l70si8643331pge.778.2018.03.05.11.07.04; Mon, 05 Mar 2018 11:07:19 -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=oMHG+EIP; 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 S932228AbeCETFv (ORCPT + 99 others); Mon, 5 Mar 2018 14:05:51 -0500 Received: from mail-wr0-f195.google.com ([209.85.128.195]:42119 "EHLO mail-wr0-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751462AbeCETFt (ORCPT ); Mon, 5 Mar 2018 14:05:49 -0500 Received: by mail-wr0-f195.google.com with SMTP id k9so18403281wre.9 for ; Mon, 05 Mar 2018 11:05:48 -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=RIb2jyugM9ZYsfVSxzeLlc0N/dyog7Auksv7dofZj1o=; b=oMHG+EIPmjKHqGcNN3gYQAaUAfscFHOkXSLh4yGZmZNhxv2aN/8N7A5V1HcabeVS4J 366ScKYuTg2CSGMp9w8UpOo44xXpeWMoWCTL+4UXcVgFqyy6tnhjGZutRTTe/sLYNCmw dLLKNKclPiZFJJr6FYfqvfKpsJlS6AZiHLsVlF0jSDUpSqlLMIW7ksyOn4gcGr+0vDjX 1zFYyzNSTs2nkXyCsc38Rh6HUDIwLHv8j85q4oAtQLbhft5ltrHNdSkWzmiSUvIFw63z uCgwSMy0MHOsP9hy498zJv9YMuL1tVs7Q3UZCqB9oCL+lh3p7isa3d9f1c9KqAkDJB5f NhXA== 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=RIb2jyugM9ZYsfVSxzeLlc0N/dyog7Auksv7dofZj1o=; b=WWvx+9HMjAR8UbDUF2PL3BQUIExuroN56jDKUUJoWGMCl310wRAuE6z/oUC0YYrSr7 OaH+G6yJhRQk4ZyKYp0YJfLjyS235LjY03ivSdTFLKOlDRkVWDTAfhXjrjQf8pmixzSr 3AgoHadJLbrXo7LUnjdYcWCS4FUOioPGIinSYmFnGHhDfAM9xDSbaI58DuEBJcHCvxOy MS7VqaBtLFaoiDVxd6uNRU9VBo6zlKrZu/XHMaNJB5OSlA6v4xUcfRJ1WTWIzWP7hsOb u5QURYuOywL90RmAjVg55CmjIE6xYQRByP2fvcxUiPg/ocEOYpEv0jSQ/rVemGqewYud Mg7A== X-Gm-Message-State: APf1xPB7DUX93wSTP+My11xu90dxERfgCCJAIj9hM1Lm7D/weAgHbJ9p jveKIv3K2/cTm9g8bQjai0s= X-Received: by 10.223.189.1 with SMTP id j1mr14482789wrh.174.1520276748020; Mon, 05 Mar 2018 11:05:48 -0800 (PST) Received: from andrea (94-74-211-130.client.rionet.cz. [94.74.211.130]) by smtp.gmail.com with ESMTPSA id 2sm11166050wmk.29.2018.03.05.11.05.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Mon, 05 Mar 2018 11:05:47 -0800 (PST) Date: Mon, 5 Mar 2018 20:05:40 +0100 From: Andrea Parri To: Palmer Dabbelt , Albert Ou Cc: Daniel Lustig , Alan Stern , 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: [RFC PATCH 2/2] riscv/atomic: Strengthen implementations with fences Message-ID: <20180305190540.GA4531@andrea> References: <1520274276-21871-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: <1520274276-21871-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 Mon, Mar 05, 2018 at 07:24:36PM +0100, 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) > > [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 > > Suggested-by: Daniel Lustig > 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 [...] > +static __always_inline \ > +c_t atomic##prefix##_xchg_release(atomic##prefix##_t *v, c_t n) \ > +{ \ > + return __xchg_acquire(&(v->counter), n, size); \ > +} \ [...] > +static __always_inline \ > +c_t atomic##prefix##_cmpxchg_release(atomic##prefix##_t *v, \ > + c_t o, c_t n) \ > +{ \ > + return __cmpxchg_acquire(&(v->counter), o, n, size); \ > +} \ These better be _release in v2 ... Andrea