Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp1032728pxb; Fri, 15 Apr 2022 19:02:04 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzxD0jyVT4lNwP7BuyzJTbgPSsMDuyBZdMQjuTHZjAWd6fzNxjbDFStVP91z/LABMFFCApB X-Received: by 2002:a65:60d3:0:b0:39c:f431:5859 with SMTP id r19-20020a6560d3000000b0039cf4315859mr1346438pgv.442.1650074523451; Fri, 15 Apr 2022 19:02:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650074523; cv=none; d=google.com; s=arc-20160816; b=nkEAGhPnHkTrFx+/WdaKtBcCAWWN4KTPam+cvMV0yU8YvhOm59PMiOir+nSrf48FKK 1c2uoeWLdohI8HLF7bWUlBcMqWYx14zhGROnuV+e7BDwhe7WmkKsxEASXeBjMa2o2OM7 jYv4fGeDgYt6aCGDrK8K8+rAFeehCTIk+V/fkdpW7VCYMHHbKmv7DEo5gKdKZkriq2EH lLmonsxTHbIa4Z3DLWEW1ugMF9+P6pePwwvIsKrSD2/X+IBx5iUxyMDaAWmfru4Gvq1v r2Mdc8FFzENcRLRye/yRx3nORH2/9Aew4lyK/uYOIx25PHGtXgTOob2LXrgiNsWRk0a9 TVAA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=f7S9Nf0iOB82baXteCdpXugH6eTdDEypH4RQLkPdzMA=; b=sZdlZ2Iknul/ywukLlSNI5hw6nzmZtvwrTyJ4LFUjArlEh7MWWFYk4S+6NZ84rca/T tnjxWky/xcU5UatZUjvaqiAiKwQjMaV1b6OHP/cg+BK1P9JRX/m/+87uwndt+EHyHNS2 0db5h5TDAdYwrOcOR7ckgTZjDml8CO45nRNLyBT/f4xN6+QeduebGrrLfRBXFdB0aMdL WXYZ1S7cBHDnWwA0ojR6jnPpJhDmNdiUaPwcMDYXwW4e2UL6XRmwO2lRxLgPKBLFuQGi pWdVJ61PiWmhZKGHXUEqNTkhhrSAeYUY6jtO1vZKgi3y3Hfp5Wc0E3SEY0bZmitdg6Lk hkwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="m4BT/K/0"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id i70-20020a628749000000b00505911788d7si2863251pfe.326.2022.04.15.19.02.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 15 Apr 2022 19:02:03 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20210112 header.b="m4BT/K/0"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id A92C51C7BA8; Fri, 15 Apr 2022 18:21:50 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1348350AbiDOBML (ORCPT + 99 others); Thu, 14 Apr 2022 21:12:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55608 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348342AbiDOBMJ (ORCPT ); Thu, 14 Apr 2022 21:12:09 -0400 Received: from mail-qv1-xf2c.google.com (mail-qv1-xf2c.google.com [IPv6:2607:f8b0:4864:20::f2c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D4F22BB08; Thu, 14 Apr 2022 18:09:42 -0700 (PDT) Received: by mail-qv1-xf2c.google.com with SMTP id c1so5653042qvl.3; Thu, 14 Apr 2022 18:09:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=f7S9Nf0iOB82baXteCdpXugH6eTdDEypH4RQLkPdzMA=; b=m4BT/K/0yJDzWkiBmM5y4I2etTUOAhnNFnHc1jDMHcc/IJ8ioruOoc/NbNgbkzhA5v XuduPHH2yTBaxzrLm++6WtL0LxrwFnnM3GaJGS64OgFN1FWUH3oTCEylxd2oEsiLLM3Q 2qhHdt6QOrVuFjj/JjwMktFia5+Iv66P5yM8mJSXMuGEKHpJZ5+WfEuOLPRSPFH6tpch SoCnbuA0qIXQjhex2DdHaPZFKwDUpS1PURP9nwdydJRNNm94sMLuuKFlT0fLxdP3DjSY jkSTmCtZHY0ZMJH63+cVzrEWanJgpr2cHep+AAWv+GLw0r5fXALJfJxJAeLx4LkjbO6W tVfw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=f7S9Nf0iOB82baXteCdpXugH6eTdDEypH4RQLkPdzMA=; b=PKn5ERYY0a3+OnvfN7O2pPE0LIHKJR/DJHQU5HqyPmU+gaSOeafuRkS9RqsCfP0prY 5nDbAIijbOw/P+VFHcIgN5ehCDJAXs2bS+wl1GdsaZMfYuAhlVuJPllbrRXO7SPmrDuM 5GNj9sL/FIa44VrZngMJKIESC7qq2Y2EqtbhwCOsiaEjMv5g68+VxcWU1/295CovDFeq VqvioNvIF6vxYs9Dnuf/WqTEIZiuddETrlYvPCJaacNlgcwBLKYPZyR8EhMFMcFKsLy6 pq74ShpNDZvXf9aPNHAkfnoRfze+jCJRxrnksKleN0kD5kw/G+QfyBjs71+SNlKqoVHo eBTw== X-Gm-Message-State: AOAM533mPeh5Xzyv07hk/ITxyro/tC3oCEE0ScvmWLNO6DBjj+aCAb5s FTubExDdeRrbP2QpPtTyulk= X-Received: by 2002:a05:6214:5188:b0:446:23f5:1483 with SMTP id kl8-20020a056214518800b0044623f51483mr4871615qvb.19.1649984981121; Thu, 14 Apr 2022 18:09:41 -0700 (PDT) Received: from auth1-smtp.messagingengine.com (auth1-smtp.messagingengine.com. [66.111.4.227]) by smtp.gmail.com with ESMTPSA id b126-20020a37b284000000b0069a11927e57sm1683932qkf.101.2022.04.14.18.09.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 14 Apr 2022 18:09:40 -0700 (PDT) Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailauth.nyi.internal (Postfix) with ESMTP id 56D2A27C0054; Thu, 14 Apr 2022 21:09:39 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute2.internal (MEProxy); Thu, 14 Apr 2022 21:09:39 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrudelgedggeefucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepfffhvffukfhfgggtuggjsehgtderredttddvnecuhfhrohhmpeeuohhquhhn ucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrfgrth htvghrnhepueehjeejieevueeuteeileeuvddvvedvieeltddtudekgeegueelvddtkeet tdevnecuffhomhgrihhnpehkvghrnhgvlhdrohhrghenucevlhhushhtvghrufhiiigvpe dtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegsohhquhhnodhmvghsmhhtphgruhhthhhp vghrshhonhgrlhhithihqdeiledvgeehtdeigedqudejjeekheehhedvqdgsohhquhhnrd hfvghngheppehgmhgrihhlrdgtohhmsehfihigmhgvrdhnrghmvg X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 14 Apr 2022 21:09:35 -0400 (EDT) Date: Fri, 15 Apr 2022 09:09:29 +0800 From: Boqun Feng To: Palmer Dabbelt Cc: Arnd Bergmann , heiko@sntech.de, guoren@kernel.org, shorne@gmail.com, peterz@infradead.org, mingo@redhat.com, Will Deacon , longman@redhat.com, jonas@southpole.se, stefan.kristiansson@saunalahti.fi, Paul Walmsley , Palmer Dabbelt , aou@eecs.berkeley.edu, macro@orcam.me.uk, Greg KH , sudipm.mukherjee@gmail.com, wangkefeng.wang@huawei.com, jszhang@kernel.org, linux-csky@vger.kernel.org, linux-kernel@vger.kernel.org, openrisc@lists.librecores.org, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org Subject: Re: [PATCH v3 1/7] asm-generic: ticket-lock: New generic ticket-based spinlock Message-ID: References: <20220414220214.24556-1-palmer@rivosinc.com> <20220414220214.24556-2-palmer@rivosinc.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="vliStJTlDzun4Idq" Content-Disposition: inline In-Reply-To: <20220414220214.24556-2-palmer@rivosinc.com> X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org --vliStJTlDzun4Idq Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Hi, On Thu, Apr 14, 2022 at 03:02:08PM -0700, Palmer Dabbelt wrote: > From: Peter Zijlstra >=20 > This is a simple, fair spinlock. Specifically it doesn't have all the > subtle memory model dependencies that qspinlock has, which makes it more > suitable for simple systems as it is more likely to be correct. It is > implemented entirely in terms of standard atomics and thus works fine > without any arch-specific code. >=20 > This replaces the existing asm-generic/spinlock.h, which just errored > out on SMP systems. >=20 > Signed-off-by: Peter Zijlstra (Intel) > Signed-off-by: Palmer Dabbelt > --- > include/asm-generic/spinlock.h | 85 +++++++++++++++++++++++++--- > include/asm-generic/spinlock_types.h | 17 ++++++ > 2 files changed, 94 insertions(+), 8 deletions(-) > create mode 100644 include/asm-generic/spinlock_types.h >=20 > diff --git a/include/asm-generic/spinlock.h b/include/asm-generic/spinloc= k.h > index adaf6acab172..ca829fcb9672 100644 > --- a/include/asm-generic/spinlock.h > +++ b/include/asm-generic/spinlock.h > @@ -1,12 +1,81 @@ > /* SPDX-License-Identifier: GPL-2.0 */ > -#ifndef __ASM_GENERIC_SPINLOCK_H > -#define __ASM_GENERIC_SPINLOCK_H > + > /* > - * You need to implement asm/spinlock.h for SMP support. The generic > - * version does not handle SMP. > + * 'Generic' ticket-lock implementation. > + * > + * It relies on atomic_fetch_add() having well defined forward progress > + * guarantees under contention. If your architecture cannot provide this= , stick > + * to a test-and-set lock. > + * > + * It also relies on atomic_fetch_add() being safe vs smp_store_release(= ) on a > + * sub-word of the value. This is generally true for anything LL/SC alth= ough > + * you'd be hard pressed to find anything useful in architecture specifi= cations > + * about this. If your architecture cannot do this you might be better o= ff with > + * a test-and-set. > + * > + * It further assumes atomic_*_release() + atomic_*_acquire() is RCpc an= d hence > + * uses atomic_fetch_add() which is SC to create an RCsc lock. > + * > + * The implementation uses smp_cond_load_acquire() to spin, so if the > + * architecture has WFE like instructions to sleep instead of poll for w= ord > + * modifications be sure to implement that (see ARM64 for example). > + * > */ > -#ifdef CONFIG_SMP > -#error need an architecture specific asm/spinlock.h > -#endif > =20 > -#endif /* __ASM_GENERIC_SPINLOCK_H */ > +#ifndef __ASM_GENERIC_TICKET_LOCK_H > +#define __ASM_GENERIC_TICKET_LOCK_H > + > +#include > +#include > + > +static __always_inline void arch_spin_lock(arch_spinlock_t *lock) > +{ > + u32 val =3D atomic_fetch_add(1<<16, lock); /* SC, gives us RCsc */ > + u16 ticket =3D val >> 16; > + > + if (ticket =3D=3D (u16)val) > + return; > + > + atomic_cond_read_acquire(lock, ticket =3D=3D (u16)VAL); Looks like my follow comment is missing: https://lore.kernel.org/lkml/YjM+P32I4fENIqGV@boqun-archlinux/ Basically, I suggested that 1) instead of "SC", use "fully-ordered" as that's a complete definition in our atomic API ("RCsc" is fine), 2) introduce a RCsc atomic_cond_read_acquire() or add a full barrier here to make arch_spin_lock() RCsc otherwise arch_spin_lock() is RCsc on fastpath but RCpc on slowpath. Regards, Boqun > +} > + > +static __always_inline bool arch_spin_trylock(arch_spinlock_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 */ > +} > + [...] --vliStJTlDzun4Idq Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCAAdFiEEj5IosQTPz8XU1wRHSXnow7UH+rgFAmJYxcUACgkQSXnow7UH +rihtgf/QlwBFoAeUTlH4MhKKy3Fr6WrwXq/WIgA71uyveBP8XeJF92iatARzevG wKLiIQzO4VzrpLJ8Ydy+o4ia1xb+nCqwVE0MD2oqPjxwvvyi/7HxZFBI4iq3PZHm 3FRwaGPnNrQGfFmyjM/byXc6uQ/NlPY65rZJcR5wS5j14F5NXAUjOBXrWVQT8nBU livaSZhbWwyn5oEnzsy/G/oAeD16LIJbjF2pQyX3FPtd1sIlIdOCjWN3TSIM2e2+ enFK5X7w489U+pKfVIOygTWHe95dd9JNsEsSyDmXkTJQnn/cCbYNfI+BR/H2Hc9G HyLCBV1KDHETDNQ25uo2WoTdfceyNQ== =T3Uh -----END PGP SIGNATURE----- --vliStJTlDzun4Idq--