Received: by 10.223.185.116 with SMTP id b49csp7936466wrg; Thu, 1 Mar 2018 13:55:30 -0800 (PST) X-Google-Smtp-Source: AG47ELuAQpBNDANBG5PKf5fV0k9z6LAWLxSD+/s5uYiWEQ38+Ghg5jRq3etY7IKmU+PFnZuw+2/4 X-Received: by 10.99.126.22 with SMTP id z22mr1383657pgc.131.1519941330345; Thu, 01 Mar 2018 13:55:30 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519941330; cv=none; d=google.com; s=arc-20160816; b=GWi4FBJGgeOVoPsE0dR7bPyMeaWWh0+pecavmcj2vCf/ABwr5E2JvB1S8oF+UrM2jK RzKHr6MAEv+kkFn1/75HsN8fMWXEY6jsg1BlPWBeH17SefTnGjhwKZox65gwA3SKrXm3 k/0G/Q2Oi3F66r5/3rA3HZikIf0ypzi3zNzjSsCp3Y/7n8EJJZDQqBqaATetCPXkd+0B DQXSGP+w5vzcPQm1F6jtTRDWE8eFhtyZzeXmF8aHsw/AU5LsrdJhOfskeSgOrIugVtZd Uv0hHED0an0eCfjSqEypeUX97ucwveR/OppTp1WlGcsM4dpZhIJNdJso/vHfEigS+BvX YerQ== 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=saOrp0KHBQgyhfabDUnR91WcjMMOW9tro2FSaFawudw=; b=PDYA75t/Rb7w8WU5BbUqRfvHS5+Aawo8Ilqzlxn3c03VoPApgFqKnHns9lwRrTx8Wh SE02LzbDecvi23pYowIhIyOjDjhgZGPwbMy/N91KwiP74nWYAKzNqDvHXtyyCctUVLtd ASUFLwpUeCr4TcbRct9F3dAzDUfTe4FVbHidCrX6KuqNXR/jCNHGX2BMabfHAgZsFfJD 6epnjBAXmsL98HDuVzUAP4bAegYVL6zmbbwF10aLbJFJe2lgDMoD30/JmZztYvG/oLr2 rycJYUMt+m8+CAA+W0EeovdhUK5hputm4G6fm1DvR3uGslmuUKdTvwJKstRJDpFdup6/ TTyQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sifive.com header.s=google header.b=CVX4xCy6; 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 t21-v6si3768615plj.269.2018.03.01.13.55.15; Thu, 01 Mar 2018 13:55:30 -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=CVX4xCy6; 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 S1162399AbeCAVyf (ORCPT + 99 others); Thu, 1 Mar 2018 16:54:35 -0500 Received: from mail-pl0-f66.google.com ([209.85.160.66]:44184 "EHLO mail-pl0-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1162136AbeCAVya (ORCPT ); Thu, 1 Mar 2018 16:54:30 -0500 Received: by mail-pl0-f66.google.com with SMTP id w21-v6so4425442plp.11 for ; Thu, 01 Mar 2018 13:54:30 -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=saOrp0KHBQgyhfabDUnR91WcjMMOW9tro2FSaFawudw=; b=CVX4xCy6Z2VUumbN+XrnHfv5jf+GXUU+h9i13Rot/GEnl/GftmUJOLmYKVQDUbg8CB Eaz1Nn2YyuqVofDlWZ4ZZkNj9pTiLlWrNPC83Y39mIrNyQal1WyKRNOJBJCitQpV5VjS EraOT25rwE4t1R2kn1z0mjWnHfR3C/pY1yrNTcqUbgQCv+b497GPX5RbovKlirFxWzLX +So1s9cVZtBJl81qbQVQShabPrTup/bKzOGhIvBNZsTmNKgjPcv7TMHP+IfN9relP1t/ e9UUWHo2u9asUsXm1uOVrW8SrpYD66DjsmQnlc7jMNCcjzkbJ+X3L+2N25wiry4YWATb 1iNw== 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=saOrp0KHBQgyhfabDUnR91WcjMMOW9tro2FSaFawudw=; b=gZyC+RHznBKDvI51sHFMzSJ/tCXZ850Mo0hZE8Vj1NrdWLhUuZNSh4J6ie0a3sF/Yg OZVHWUA9BOexYSfEHaNLqWFd2TBuYvwpcLbZUs2Q+XOICIuCVaOKtLLrDkup1eSvJmrk BLI8wZ/wIsoug53NqQ+LZCLYMZb16fZu8PFzmtJhWXdnqcQDk2b8atC1lSMDJrhmezyH u9ZJfdQcuGN8mwaumSsN6wUBnI6xJ7BY8RY6huC2okkdZZ3JPtB8yij1648Tffj8wsKD ZU12wZGqA4/ES5dQJs/Y5KnJYvGW6NHCTOLXO6yDiJHCLRP6p5dijRTztD8Dm6H8thkg H3XA== X-Gm-Message-State: APf1xPBxlQ4/nv8iqQwrTqXNJllcX773/rG/oLW9i+Y67H5y41LA/2o7 uAPuDsSNk1c8xPlOyp5l9dHpbQ== X-Received: by 2002:a17:902:3124:: with SMTP id w33-v6mr3262825plb.119.1519941269661; Thu, 01 Mar 2018 13:54:29 -0800 (PST) Received: from localhost ([12.206.222.5]) by smtp.gmail.com with ESMTPSA id l6sm9764866pff.102.2018.03.01.13.54.28 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Mar 2018 13:54:29 -0800 (PST) Date: Thu, 01 Mar 2018 13:54:29 -0800 (PST) X-Google-Original-Date: Thu, 01 Mar 2018 13:43:26 PST (-0800) Subject: Re: [RFC PATCH] riscv/locking: Strengthen spin_lock() and spin_unlock() In-Reply-To: <20180301151141.GA6430@andrea> CC: Daniel Lustig , peterz@infradead.org, paulmck@linux.vnet.ibm.com, linux-kernel@vger.kernel.org, albert@sifive.com, stern@rowland.harvard.edu, Will Deacon , boqun.feng@gmail.com, npiggin@gmail.com, dhowells@redhat.com, j.alglave@ucl.ac.uk, luc.maranget@inria.fr, akiyks@gmail.com, mingo@kernel.org, Linus Torvalds , linux-riscv@lists.infradead.org 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 Thu, 01 Mar 2018 07:11:41 PST (-0800), parri.andrea@gmail.com wrote: > Hi Daniel, > > On Thu, Feb 22, 2018 at 11:47:57AM -0800, Daniel Lustig wrote: >> On 2/22/2018 10:27 AM, Peter Zijlstra wrote: >> > On Thu, Feb 22, 2018 at 10:13:17AM -0800, Paul E. McKenney wrote: >> >> So we have something that is not all that rare in the Linux kernel >> >> community, namely two conflicting more-or-less concurrent changes. >> >> This clearly needs to be resolved, either by us not strengthening the >> >> Linux-kernel memory model in the way we were planning to or by you >> >> strengthening RISC-V to be no weaker than PowerPC for these sorts of >> >> externally viewed release-acquire situations. >> >> >> >> Other thoughts? >> > >> > Like said in the other email, I would _much_ prefer to not go weaker >> > than PPC, I find that PPC is already painfully weak at times. >> >> Sure, and RISC-V could make this work too by using RCsc instructions >> and/or by using lightweight fences instead. It just wasn't clear at >> first whether smp_load_acquire() and smp_store_release() were RCpc, >> RCsc, or something else, and hence whether RISC-V would actually need >> to use something stronger than pure RCpc there. Likewise for >> spin_unlock()/spin_lock() and everywhere else this comes up. > > while digging into riscv's locks and atomics to fix the issues discussed > earlier in this thread, I became aware of another issue: > > Considering here the CMPXCHG primitives, for example, I noticed that the > implementation currently provides the four variants > > ATOMIC_OPS( , .aqrl) > ATOMIC_OPS(_acquire, .aq) > ATOMIC_OPS(_release, .rl) > ATOMIC_OPS(_relaxed, ) > > (corresponding, resp., to > > atomic_cmpxchg() > atomic_cmpxchg_acquire() > atomic_cmpxchg_release() > atomic_cmpxchg_relaxed() ) > > so that the first variant above ends up doing > > 0: lr.w.aqrl %0, %addr > bne %0, %old, 1f > sc.w.aqrl %1, %new, %addr > bnez %1, 0b > 1: > > From Sect. 2.3.5. ("Acquire/Release Ordering") of the Spec., > > "AMOs with both .aq and .rl set are fully-ordered operations. Treating > the load part and the store part as independent RCsc operations is not > in and of itself sufficient to enforce full fencing behavior, but this > subtle weak behavior is counterintuitive and not much of an advantage > architecturally, especially with lr and sc also available [...]." > > I understand that > > { x = y = u = v = 0 } > > P0() > > WRITE_ONCE(x, 1); ("relaxed" store, sw) > atomic_cmpxchg(&u, 0, 1); > r0 = READ_ONCE(y); ("relaxed" load, lw) > > P1() > > WRITE_ONCE(y, 1); > atomic_cmpxchg(&v, 0, 1); > r1 = READ_ONCE(x); > > could result in (u = v = 1 and r0 = r1 = 0) at the end; can you confirm? cmpxchg isn't an AMO, it's an LR SC sequence, so that blurb doesn't apply. I think "lr.w.aqrl" and "sc.w.aqrl" is not sufficient to perform a fully ordered operation (ie, it's an incorrect implementation of atomic_cmpxchg()), but I was hoping to get some time to actually internalize this part of the RISC-V memory model at some point to be sure.