Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2824336pxb; Mon, 19 Apr 2021 15:12:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjNlC5eGj2eeXjTIBhesw/Unx2HZB8kLrv9WV9q2SvZdR2jjVUh5V5q2z+NCPQTe1upkKo X-Received: by 2002:a17:902:6b8c:b029:ea:f54f:c330 with SMTP id p12-20020a1709026b8cb02900eaf54fc330mr25628435plk.10.1618870338714; Mon, 19 Apr 2021 15:12:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618870338; cv=none; d=google.com; s=arc-20160816; b=bOribCzCGEuy/le1ESPafFfWU1YVkjKYJpLmp885iv32oepzSKKqNWCSfxeQ9u45bZ ssV6FTbGjD++m/ruDMvzERjaWux5mQesnmK7aZ4DLCzjY/csv3hqYbaWBSUkH+3GEwZV 46N/PJ21LfdyD9PeFwq7AYO7mJawtL/u9zbrjQURbCXznrSzt/Z2ZnbC6xrWtaOZc61N jexSbwy+dZSNCaH1BpUziSpx6iSGhyvLvkUg/r55cYvelpO79SUhIYdFxBXFBCtqMYiG uuVNyz/SUJWuZUGEG0tEJr93jCE/RiyeNmEIee7KZch/iUtJpCkN6/4NzXTigSctNSfw BB4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=th2BhSg9GCbzM4kXQk4N7aAXlwu2+hqUMpQjNKwFYR8=; b=H2+q1z2FZekHTP9uhfmH6LXzQ4Tz1KEmTeYdPNl8JRmMLS7r0jPBDs6FwZ9yX5cPgw +Z2n2GaZ31k+jfhDgD+lus8kaooZ+C2UjGO/bVQLS+rBUZKYjR8gpF2XoahvoIfVE/Ke i+QBUjyWqHTZO6cKA7Vh6eH0I/Gyby9QIFXw5EQk3o7FwKc/v+y4Tt2gK8/1MP2EFuk4 K1NyOw0wW3ZWEZRG4toLQ4gBqrBpI8FCqz7DZo7knDYcPrNm0/md8yvYb7futkmJ4p2M dpF9xLg8rhx6xkJBVx8x8JDfoBIZKNmfLnC2iikJT3bIoHSDf2O6ReRqWvZIBH+PM2Jq gtUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=eU5zqkKg; 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=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t8si18058582pgp.265.2021.04.19.15.12.06; Mon, 19 Apr 2021 15:12:18 -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=@kernel.org header.s=k20201202 header.b=eU5zqkKg; 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=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238914AbhDSRgT (ORCPT + 99 others); Mon, 19 Apr 2021 13:36:19 -0400 Received: from mail.kernel.org ([198.145.29.99]:36656 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233669AbhDSRgT (ORCPT ); Mon, 19 Apr 2021 13:36:19 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id E893961027; Mon, 19 Apr 2021 17:35:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1618853749; bh=/bJ3/kTj3ATokRkPczH/Qf4mgrd6aufqMsKhfPpuPMY=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=eU5zqkKgFrmCzGH75xqYw4jmDutyu7/V5foIO7bg2YNJOOXD2eg+8FP4sXADbEGBa viaC/oxWAYx1FycR5pf22+8dGuYksWjUHwZOy4/1E2Vm2PwQwYNDHzuhimw+khHs8b upti17P7LGL5ljJrBycNDGdOEI22wroBEgysRvPxARAzVcxcsfV5kC20JXzsG5UlaN PUGJTRNBgNSZre7VGL5FbqZ8sRKQiuMUhoGsoWOGCCScTauHFzHZOBqHwAQU7L5V1R 8tq/WRtJvAZtsm93z1bjl83e6+UrZv10qh2/qYvR40lmu3T/zs+iv1QwZWtURM5jsb MrEmPK7db7JuQ== Date: Mon, 19 Apr 2021 18:35:43 +0100 From: Will Deacon To: Peter Zijlstra Cc: Guo Ren , Christoph =?iso-8859-1?Q?M=FCllner?= , Palmer Dabbelt , Anup Patel , linux-riscv , Linux Kernel Mailing List , Guo Ren , Catalin Marinas , Will Deacon , Arnd Bergmann , jonas@southpole.se, stefan.kristiansson@saunalahti.fi, shorne@gmail.com Subject: Re: [RFC][PATCH] locking: Generic ticket-lock Message-ID: <20210419173543.GC31045@willie-the-truck> References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 14, 2021 at 12:16:38PM +0200, Peter Zijlstra wrote: > How's this then? Compile tested only on openrisc/simple_smp_defconfig. > > diff --git a/include/asm-generic/qspinlock.h b/include/asm-generic/qspinlock.h > index d74b13825501..a7a1296b0b4d 100644 > --- a/include/asm-generic/qspinlock.h > +++ b/include/asm-generic/qspinlock.h > @@ -2,6 +2,36 @@ > /* > * Queued spinlock > * > + * A 'generic' spinlock implementation that is based on MCS locks. An > + * architecture that's looking for a 'generic' spinlock, please first consider > + * ticket-lock.h and only come looking here when you've considered all the > + * constraints below and can show your hardware does actually perform better > + * with qspinlock. > + * > + * > + * It relies on atomic_*_release()/atomic_*_acquire() to be RCsc (or no weaker > + * than RCtso if you're power), where regular code only expects atomic_t to be > + * RCpc. Maybe capitalise "Power" to make it clear this about the architecture? > + * > + * It relies on a far greater (compared to ticket-lock.h) set of atomic > + * operations to behave well together, please audit them carefully to ensure > + * they all have forward progress. Many atomic operations may default to > + * cmpxchg() loops which will not have good forward progress properties on > + * LL/SC architectures. > + * > + * One notable example is atomic_fetch_or_acquire(), which x86 cannot (cheaply) > + * do. Carefully read the patches that introduced queued_fetch_set_pending_acquire(). > + * > + * It also heavily relies on mixed size atomic operations, in specific it > + * requires architectures to have xchg16; something which many LL/SC > + * architectures need to implement as a 32bit and+or in order to satisfy the > + * forward progress guarantees mentioned above. > + * > + * Further reading on mixed size atomics that might be relevant: > + * > + * http://www.cl.cam.ac.uk/~pes20/popl17/mixed-size.pdf > + * > + * > * (C) Copyright 2013-2015 Hewlett-Packard Development Company, L.P. > * (C) Copyright 2015 Hewlett-Packard Enterprise Development LP > * > diff --git a/include/asm-generic/ticket-lock-types.h b/include/asm-generic/ticket-lock-types.h > new file mode 100644 > index 000000000000..829759aedda8 > --- /dev/null > +++ b/include/asm-generic/ticket-lock-types.h > @@ -0,0 +1,11 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > + > +#ifndef __ASM_GENERIC_TICKET_LOCK_TYPES_H > +#define __ASM_GENERIC_TICKET_LOCK_TYPES_H > + > +#include > +typedef atomic_t arch_spinlock_t; > + > +#define __ARCH_SPIN_LOCK_UNLOCKED ATOMIC_INIT(0) > + > +#endif /* __ASM_GENERIC_TICKET_LOCK_TYPES_H */ > diff --git a/include/asm-generic/ticket-lock.h b/include/asm-generic/ticket-lock.h > new file mode 100644 > index 000000000000..3f0d53e21a37 > --- /dev/null > +++ b/include/asm-generic/ticket-lock.h > @@ -0,0 +1,86 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > + > +/* > + * '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 although > + * you'd be hard pressed to find anything useful in architecture specifications > + * about this. If your architecture cannot do this you might be better off with > + * a test-and-set. > + * > + * It further assumes atomic_*_release() + atomic_*_acquire() is RCpc and 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 word > + * modifications be sure to implement that (see ARM64 for example). > + * > + */ > + > +#ifndef __ASM_GENERIC_TICKET_LOCK_H > +#define __ASM_GENERIC_TICKET_LOCK_H > + > +#include > +#include > + > +static __always_inline void ticket_lock(arch_spinlock_t *lock) > +{ > + u32 val = atomic_fetch_add(1<<16, lock); /* SC, gives us RCsc */ I hate to say it, but smp_mb__after_unlock_lock() would make the intention a lot clearer here :( That is, the implementation as you have it gives stronger than RCsc semantics for all architectures. Alternatively, we could write the thing RCpc and throw an smp_mb() into the unlock path if CONFIG_ARCH_WEAK_RELEASE_ACQUIRE. > + u16 ticket = val >> 16; > + > + if (ticket == (u16)val) > + return; > + > + atomic_cond_read_acquire(lock, ticket == (u16)VAL); > +} > + > +static __always_inline bool ticket_trylock(arch_spinlock_t *lock) > +{ > + u32 old = atomic_read(lock); > + > + if ((old >> 16) != (old & 0xffff)) > + return false; > + > + return atomic_try_cmpxchg(lock, &old, old + (1<<16)); /* SC, for RCsc */ > +} > + > +static __always_inline void ticket_unlock(arch_spinlock_t *lock) > +{ > + u16 *ptr = (u16 *)lock + __is_defined(__BIG_ENDIAN); > + u32 val = atomic_read(lock); > + > + smp_store_release(ptr, (u16)val + 1); > +} > + > +static __always_inline int ticket_is_locked(arch_spinlock_t *lock) > +{ > + u32 val = atomic_read(lock); > + > + return ((val >> 16) != (val & 0xffff)); > +} > + > +static __always_inline int ticket_is_contended(arch_spinlock_t *lock) > +{ > + u32 val = atomic_read(lock); > + > + return (s16)((val >> 16) - (val & 0xffff)) > 1; Does this go wonky if the tickets are in the process of wrapping around? Will