Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp2466702pxb; Tue, 13 Apr 2021 02:35:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyJn41K27YOUPw69eHbAhlyeXi+X0IT98yGatzw83NocQ3l2/jqmVSdx/Ai4QimFyy41neW X-Received: by 2002:aa7:8d84:0:b029:1f8:3449:1bc6 with SMTP id i4-20020aa78d840000b02901f834491bc6mr27928955pfr.76.1618306539166; Tue, 13 Apr 2021 02:35:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618306539; cv=none; d=google.com; s=arc-20160816; b=t7gdEOKCXVy7AigS21ybi2bl0bqDBZwDSU6TSEa/XrP5RQGB+N50rSmwVSL2Aet01l xME2i78DNSq34c/M7K/Nw4r0M0kKj9y183TeVM7DpTAS3NGE078v28OeTUH0y1EwaD7R TUOQGZf/o3ygUoI9yuqlndsAR8f1xGneW74TkncgFhaKwwXvOXLLpsF16Y4efmxytBF7 M6p1vmAPEKOHxz/WrcQvkk+1KroPINowtTiL9sdc7rx9EYuxPDKKSqSrLMD+/b810xgS 4OIboVPk+XTDO9AWoDYiCq8gXURG2LQnZ9838S/ml6czgvud5VQtfOPM/N4XwnQ+Zpm4 zj2g== 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=uZZGljHI+pEQTN9kef9SQSB785V0yvmr/JmVQj1uQhM=; b=t7cC17Dybzg2nxt3lZ2y0JiDES2kDOglxyjJ+eiUVW/pf0IbfmuStdlilWHqAT7Vtn les4mvlQOyuiNtFZarqfjQI6Vs/osuTr0ix69iXeAT/iJyx7xzT1eC0fd5Pm/APEChSs Pk8//eeR9cCD57eKxki9LXiSbG+6IfHI2c74c3lQI7ryoCCFElK9c7xIyRV66trY+a+Q n/wdRYtw3rFvdf91czMyolLd4fRBvDqgmHse54eHBK+DaDVBwVZgQjgSQeupp4UStZPt u55HznI2UqwVo2gCxAj+r67mRl9B+dA0FWuzkoeWnwv47BD+EAFXtU4K8dO6Oh9CbnJU ckMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=desiato.20200630 header.b=UV6GxUjH; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 185si834367pfz.215.2021.04.13.02.35.27; Tue, 13 Apr 2021 02:35:39 -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=@infradead.org header.s=desiato.20200630 header.b=UV6GxUjH; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242580AbhDMIRi (ORCPT + 99 others); Tue, 13 Apr 2021 04:17:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33588 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241675AbhDMIRh (ORCPT ); Tue, 13 Apr 2021 04:17:37 -0400 Received: from desiato.infradead.org (desiato.infradead.org [IPv6:2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A0CCFC061574 for ; Tue, 13 Apr 2021 01:17:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=uZZGljHI+pEQTN9kef9SQSB785V0yvmr/JmVQj1uQhM=; b=UV6GxUjHogdqfKF2OVfvAuRjBC vDJ17oB2J+4vrYX3Xv/8QzCHnN0OKbKnX67T77g3aaFXA9JRMw3dy/aHppYzoiD+XvmFuFRZycoHF isR6cC+m3F94Vu16ZZ4nYcA0bqFqvzXTIkpkjBOixQn8H6zAMjJqT/0E5jzpjgLTwkrF9k5YPoqu6 lGCStF9w5A9T+hJWBBfzvDtXoQjLvkJMRrBotBqhGIojmG/87aNY8XrBTeWmNPtweuoECgT6EzCvE DvIdur/0oxBIUbMNQABWKZzfxE2uaIhWeXiJKLMz83tqW5Ydc2ylbRcRSFp2e+6FlJcewLUDwg1Jh IkT3nl1g==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by desiato.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lWEEM-008ZRN-DN; Tue, 13 Apr 2021 08:17:09 +0000 Received: from hirez.programming.kicks-ass.net (hirez.programming.kicks-ass.net [192.168.1.225]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (Client did not present a certificate) by noisy.programming.kicks-ass.net (Postfix) with ESMTPS id D990D3001F6; Tue, 13 Apr 2021 10:17:05 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id 6667E20224209; Tue, 13 Apr 2021 10:17:05 +0200 (CEST) Date: Tue, 13 Apr 2021 10:17:05 +0200 From: Peter Zijlstra To: Christoph =?iso-8859-1?Q?M=FCllner?= 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 Subject: Re: [PATCH] riscv: locks: introduce ticket-based spinlock implementation Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Apr 13, 2021 at 10:03:01AM +0200, Peter Zijlstra 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-word > store (through smp_store_release()) will fail the SC. > > Then you can do something like: > > void lock(atomic_t *lock) > { > u32 val = atomic_fetch_add(1<<16, lock); /* SC, gives us RCsc */ > u16 ticket = val >> 16; > > for (;;) { > if (ticket == (u16)val) > break; > cpu_relax(); > val = atomic_read_acquire(lock); > } A possibly better might be: if (ticket == (u16)val) return; atomic_cond_read_acquire(lock, ticket == (u16)VAL); Since that allows architectures to use WFE like constructs. > } > > void unlock(atomic_t *lock) > { > u16 *ptr = (u16 *)lock + (!!__BIG_ENDIAN__); > u32 val = 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.