Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2755694pxb; Tue, 13 Apr 2021 09:24:14 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxDCXJeLA6VPj7OpVL4rbAHnalJjVMEGNoKurzdQj8UWzExnOTLM6g00DoAkz3S4zFFcIyt X-Received: by 2002:a17:906:46d6:: with SMTP id k22mr11493291ejs.243.1618331054012; Tue, 13 Apr 2021 09:24:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618331054; cv=none; d=google.com; s=arc-20160816; b=K1nV6/l25lKgM7sOpFha9wn7m937sT0kdzXF3RotXOXKfmgLJVso/z+6AjIh6sjoSA kZg55iB7q4+oycerguk81gub9Dd/Kn6j3JUHmpq2U8kULW8L8GK5kbA54TNVgCy9GrLV tUpm6AYrAUPhYdKYk0UZLDqydPvK1bra6V6IQV+XjX58nzN8shOkGkGUt2EsVMGMni1y 1JetPXXDxZl1rrGAn/dcgeYrN+oPxYpIrU5lii2oYQWpvBSJvdmY5cE8gyAInkuJPLy+ lpkWVeMAGhXOX1L62Isfi6twjkqwy+LTzTpJ6ZoJ6cF2d5BRoYuPAQ82w9j1rKT7qWd9 grpQ== 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=0lVf0nl5+qZIZ/jwfGQvy92YMy+Bwdb1xbccBw3TFn4=; b=lx5BZGMnQljMgqbRN+2ynBy4YZ+YyVDYJ8amPUSbuNxVGtENCzena1m0mQ61QlDiPX L1cpOUg276ax9vkT8425B7B9evbLZHEyFG2XqUjibCvLqknh3H+L4xvTUYXPwqtqN920 2yZgQ2KTKMsTrigAlj1CVsVX1Be6MKSh5OnWd0BxOCCEvVeiX0q36VD/OOpTGB8g2QP9 C/GVF7eDTlGQZSNUf5aU2BXjUZ+h4zj8hhebzAOOsJeSkPoowMrLrZX2Tma6J4lMkf1x XpkE/GtphTwIA5nHNavHC8Mj++mbKmo1AyjD+u7krHC2HVfosW0SmFB62xibigDlkM+7 VECA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=OQucTICy; 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 dr7si12544517ejc.126.2021.04.13.09.23.50; Tue, 13 Apr 2021 09:24:14 -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=OQucTICy; 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 S243650AbhDMKZd (ORCPT + 99 others); Tue, 13 Apr 2021 06:25:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33446 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243397AbhDMKZc (ORCPT ); Tue, 13 Apr 2021 06:25:32 -0400 Received: from mail-vs1-xe2e.google.com (mail-vs1-xe2e.google.com [IPv6:2607:f8b0:4864:20::e2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C4077C061574 for ; Tue, 13 Apr 2021 03:25:12 -0700 (PDT) Received: by mail-vs1-xe2e.google.com with SMTP id 2so8254332vsh.4 for ; Tue, 13 Apr 2021 03:25:12 -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=0lVf0nl5+qZIZ/jwfGQvy92YMy+Bwdb1xbccBw3TFn4=; b=OQucTICyR/tvEMXOZVjDg+CuM65o1aglrPM1pxZwsPXp1hq20Ab1ovqvtWIGG24X+0 fa8WvN4zl0fWpYqVcXRaQSaRvwZP28mJyVG6tjv6u/oqP5CDONyT4OoUbgqf7iVrzPss ZXxDcWSEUkdupAb3uez+3j+IAeZXETzsvlf+0nUM4oQk0RG+7GpkEYm2AiTNMWqQHwe7 oV079OC27O+PVqHKUUuni8LlJiYch4KoCaksdtWX8vWfpHUeoHDdCWyJp3223DAbe1Y+ dhuO3xPCWjkggaeGDmyiXboig1YavxvI5GAsv5rrBt5qJR/R6TvavU6R5RFR7hDYmqD+ NAGw== 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=0lVf0nl5+qZIZ/jwfGQvy92YMy+Bwdb1xbccBw3TFn4=; b=cvmc4fHwBxKySZP263pThNIu8yxUuKVHW6sO1kof6oR+gCbj2Opqb3SVphFTY6jbmE E+PEBy52P+Cqk5JzDTPR6RVlkFoFeGEoDKa0Pwz317KabejpxTYWHPRuUqLdQK7eKxrn b/Sjwisle547VaEfBgJ+49v2xfHo+hLW+ODWSZbqc5ojUnH88Ba3LCN4cndjeoOXh0nR 5i8NQanU4FvbHDLIRcpPNT170l9a9abgGXP06rIL9qRde3U20QJu5uHfJB+yyHV+v4Ba S7Dj1myibhS9wou2Hm1y9YrfCf3JZN15iQG/H43C+AEemwwmz6pddrZe1oqL8KVZu2Kb ECZw== X-Gm-Message-State: AOAM530CjHEl2vq/3YkytO5I5b/JhIZVt5Qv4lC7mcoy5grNxW2K/TGS uEphGwPMH7BipzjINCUAm+iwfFOqttEeWd9Ha78= X-Received: by 2002:a67:e40b:: with SMTP id d11mr6143063vsf.23.1618309511657; Tue, 13 Apr 2021 03:25:11 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: =?UTF-8?Q?Christoph_M=C3=BCllner?= Date: Tue, 13 Apr 2021 12:25:00 +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 11:37 AM Peter Zijlstra wrot= e: > > On Tue, Apr 13, 2021 at 11:22:40AM +0200, Christoph M=C3=BCllner wrote: > > > > 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-w= ord > > > 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 RC= sc */ > > > 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 optima= l > > > on x86 for not being allowed to use a memop on unlock, since its bein= g > > > 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. > > Generic code has to use cmpxchg(), and then you get something like this: > > bool trylock(atomic_t *lock) > { > u32 old =3D atomic_read(lock); > > if ((old >> 16) !=3D (old & 0xffff)) > return false; > > return atomic_try_cmpxchg(lock, &old, old + (1<<16)); /* SC, for = RCsc */ > } This approach requires two loads (atomic_read() and cmpxchg()), which is not required. Detecting this pattern and optimizing it in a compiler is quite unlikely. A bit less generic solution would be to wrap the LL/SC (would be mandatory in this case) instructions and do something like this: uint32_t __smp_load_acquire_reserved(void*); int __smp_store_release_conditional(void*, uint32_t); typedef union { uint32_t v32; struct { uint16_t owner; uint16_t next; }; } arch_spinlock_t; int trylock(arch_spinlock_t *lock) { arch_spinlock_t l; int success; do { l.v32 =3D __smp_load_acquire_reserved(lock); if (l.owner !=3D l.next) return 0; l.next++; success =3D __smp_store_release_conditional(lock, l.v32); } while (!success); return success; } But here we can't tell the compiler to optimize the code between LL and SC.= .. > > That will try and do the full LL/SC loop, because it wants to complete > the cmpxchg, but in generic code we have no other option. > > (Is this what C11's weak cmpxchg is for?)