Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2659763pxb; Tue, 13 Apr 2021 07:15:26 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzK5I3rSW9MR9nIUetl7DiOb1/L1DOZuOnz2ArqFiRVnwPBGmQnumkUfBhJLGy/MFWZFIam X-Received: by 2002:a17:907:70d3:: with SMTP id yk19mr2679548ejb.108.1618323326395; Tue, 13 Apr 2021 07:15:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618323326; cv=none; d=google.com; s=arc-20160816; b=bhA5vF2epd0lSfbAnbwVVDpAvKlDTJtTLit0dcSwODIYxdIXDlxkCIMZeowbVMuxjp MQE8SBpApyUE4lLjeWLosqKYt17jSCRfIAKcK9g7Qt2+HvtX6ntSIpCsSclBw5ANRvRL cd5UIq+86fupNh7AxQTQbCmawYXh0Co72kjZ+VJVVS+QWjdZGdHS3PLAaPUU9mD1OGcD CxIiWxftdAszS7AHjzpTSST+2MZ/HmuYJQELtZgvNDYHOvF32vBlLGTLFGWFu+YId14s yLLWTT9Qf4QRweBq2aqjOtRDHNiRQbwhfxONvIdh6vO20RzycKnM8uU63rdkFdPWeez9 8uBg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=3+fqnoLwxyQPHQHnoLLAVQBCiytVesKrPDDSyoe7Zqg=; b=aNQseOpEvB0oVYu8Tu7ThVEd6h4IjYnZczDLFhZdbxFA+xqTJseSaoUWgnHCErDPQP EcR+RyXqVW+7JsFybLmEG63JbaG4CFaYTda+9RTWKXl/lbPuiXW7BBYxKzSCF2HGylfj u52nM86B0St3uqUWvS8YUHDU8CXIWKYRabjifxz11xHcK0B9c6nT3fwj2pNR4dLhMDJV XfLSE1WWoV5+y0xHYbPLrOZ1XMSi1VaSp3xKdzUBL7rDf5kk4nSaA7uhDFfbXjh8AowE xzm8mv3ToffmeXjKzLe9yEjWQRIm+el363Jq1vpb3Huj/LDL1/UjqtdxMj8LtNQ2KKHq amXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=QvOv1wOu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id g3si9877981edu.276.2021.04.13.07.15.02; Tue, 13 Apr 2021 07:15:26 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=QvOv1wOu; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S239141AbhDMJXN (ORCPT + 99 others); Tue, 13 Apr 2021 05:23:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238315AbhDMJXL (ORCPT ); Tue, 13 Apr 2021 05:23:11 -0400 Received: from mail-ua1-x932.google.com (mail-ua1-x932.google.com [IPv6:2607:f8b0:4864:20::932]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0A28EC061574 for ; Tue, 13 Apr 2021 02:22:52 -0700 (PDT) Received: by mail-ua1-x932.google.com with SMTP id s2so5102169uap.1 for ; Tue, 13 Apr 2021 02:22:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=3+fqnoLwxyQPHQHnoLLAVQBCiytVesKrPDDSyoe7Zqg=; b=QvOv1wOuaUSs/Qm4R+41uWhRkCEcpDeQWbvMM4smQD31kuqRumI1NyigCeageOAdaE I4fMOa0DXTfi3G0aTmh+6redYSzmYSvn8w1B5m4CkzgrE1W2t4OgOv4eyq2cYPzWfwTz ziCG1/UxTtcjZk1PiQGSv+YSBhCa60yNjLGDBnBzHiz7iq4+huRbuMkc9o7OuiZlcs+e C29ixSkYJdx6sW5Sc3JLDZHPth1M6RhsT9dxzGuZPWDvD5pbavGh/7DMz7bTR1hVrQAD iF2bMOhKSFDXoePtFFDYzCla4ZcIR0HKbs4gY10KtaZ2rEmnHxnp4NvnoOWcyEsqVz4X qD8Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=3+fqnoLwxyQPHQHnoLLAVQBCiytVesKrPDDSyoe7Zqg=; b=BATPbLSf0Kq6qtEZpRjaW4OJbenMt0ckSPguNjzSTb0OoIIxc2Yb3PcJj5TRCgWFoa 9+WKIjGxTZn2qf7GrO+SmeUGOJO9rb7bKY5jU/v9XezYFPGUIY/yeW6ubThY/2FWssfZ v0n7CnNjewMWXaUnDqnxbkWC5lB4CRvvLwcoeR7DgtEW5ghEh1VQ+DYQYkblvwqytkiO cbPrHH/B552oMo6r72Ivp8HWJLSSUzN1E8JvIVPbNlMdhkhXQ2UnWh4r43veyThlgxmV xYapZUY4prgr7NZhr2hOK4ClPnOtuqbeJYAEnP+jOT5SUQG23DdQFCkYPTyAeRdCwfvV aReg== X-Gm-Message-State: AOAM533VHCIXzCEisCFpin5zEhaxOTIsgIsffy4V3LkNVCUYJoIOn/bw 7FmLPCqi5jNyQa6+YNsDXsaWPbP7Ips3jQONfao= X-Received: by 2002:a9f:2c90:: with SMTP id w16mr22430324uaj.88.1618305771239; Tue, 13 Apr 2021 02:22:51 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: =?UTF-8?Q?Christoph_M=C3=BCllner?= Date: Tue, 13 Apr 2021 11:22:40 +0200 Message-ID: Subject: Re: [PATCH] riscv: locks: introduce ticket-based spinlock implementation To: Peter Zijlstra Cc: Palmer Dabbelt , Anup Patel , Guo Ren , linux-riscv , Linux Kernel Mailing List , Guo Ren , catalin.marinas@arm.com, will.deacon@arm.com, Arnd Bergmann Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 13, 2021 at 10:03 AM Peter Zijlstra wrot= e: > > On Mon, Apr 12, 2021 at 11:54:55PM +0200, Christoph M=C3=BCllner wrote: > > On Mon, Apr 12, 2021 at 7:33 PM Palmer Dabbelt wro= te: > > > > My plan is to add a generic ticket-based lock, which can be selected = at > > > compile time. It'll have no architecture dependencies (though it'll > > > likely have some hooks for architectures that can make this go faster= ). > > > Users can then just pick which spinlock flavor they want, with the id= ea > > > being that smaller systems will perform better with ticket locks and > > > larger systems will perform better with queued locks. The main goal > > > here is to give the less widely used architectures an easy way to hav= e > > > fair locks, as right now we've got a lot of code duplication because = any > > > architecture that wants ticket locks has to do it themselves. > > > > In the case of LL/SC sequences, we have a maximum of 16 instructions > > on RISC-V. My concern with a pure-C implementation would be that > > we cannot guarantee this (e.g. somebody wants to compile with -O0) > > and I don't know of a way to abort the build in case this limit exceeds= . > > Therefore I have preferred inline assembly for OpenSBI (my initial idea > > was to use closure-like LL/SC macros, where you can write the loop > > in form of C code). > > For ticket locks you really only needs atomic_fetch_add() and > smp_store_release() and an architectural guarantees that the > atomic_fetch_add() has fwd progress under contention and that a sub-word > store (through smp_store_release()) will fail the SC. > > Then you can do something like: > > void lock(atomic_t *lock) > { > u32 val =3D atomic_fetch_add(1<<16, lock); /* SC, gives us RCsc *= / > u16 ticket =3D val >> 16; > > for (;;) { > if (ticket =3D=3D (u16)val) > break; > cpu_relax(); > val =3D atomic_read_acquire(lock); > } > } > > void unlock(atomic_t *lock) > { > u16 *ptr =3D (u16 *)lock + (!!__BIG_ENDIAN__); > u32 val =3D atomic_read(lock); > > smp_store_release(ptr, (u16)val + 1); > } > > That's _almost_ as simple as a test-and-set :-) It isn't quite optimal > on x86 for not being allowed to use a memop on unlock, since its being > forced into a load-store because of all the volatile, but whatever. What about trylock()? I.e. one could implement trylock() without a loop, by letting trylock() fail if the SC fails. That looks safe on first view, but nobody does this right now.