Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp197219pxb; Wed, 14 Apr 2021 12:55:36 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwoDwAE1+6hAuXNuoBr8Wr1aAPNju927A3tk/xl4rwoAI2YAoORWxatw1hTKfjWcaKAfS0y X-Received: by 2002:a17:90a:550f:: with SMTP id b15mr5305909pji.102.1618430135989; Wed, 14 Apr 2021 12:55:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618430135; cv=none; d=google.com; s=arc-20160816; b=ub1C4IqzyRpFnV4vnMli4H06b2Z2zCqb+vqcga42h59JcQJfI3w7YiSsiVgXN1ryuz l/e1i83Ky7rLAUNsFR/qxWIMr3ICJvF+jVsb3ciJzykf0dE2rmTKqB2uwpDzY2R+8VpQ ATqfya8CyGw/VvkKGHTCrlKslOmEUi8H3DiPkVFvTvBmbHXuKjM6TGbRXBJb7LRQPo5U VUsgL3CNX35Hfe682ORIs5oMDsls3W3lo07PPNt07czcKfkFj4RS0S465GbJmduUz2VR LBJb+CTY91UeevV+mCuKLrIL77xlAnqkcWgXet7Tkar81QZNlMyAygCn7/8YFCSEu1kr Pubg== 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=cPfVvcbK8T4D/6el7vVPft6WcH2dgQZt6307pfljaIs=; b=yjaoJvRxfflt/qPJKx1ihv05/H3NtILWCodHNzcoGkoc18pcMbAJe4tTeQJeE89AP5 6VsUaFR8b7OZchCHlxYqlNIgEdXaQnrsano61Y/On4kt6z+pQXvvs2eKyEZ7ByY0d1YE Bdy8Hl6c89qkpvKPw6SPedEufxrpvcHmwgW2O4trX1IgMg7V5DGYXalh2tSvOoSpY2a2 YgFDJxyuUvNqkgusPeKla0xPXqsDrqrSsz9FQTlU4m26IjvIawYrg/ZbEqnlYJNyOdz1 IVfjOvSbsDiO3lqU+ysat1e/ectnmYM2fh3KzHzjrH4QXxTjl2tbR0rBXF7mP+CKs30A oPXw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=QoWGo05r; 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 a13si597101pjo.34.2021.04.14.12.55.24; Wed, 14 Apr 2021 12:55:35 -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=casper.20170209 header.b=QoWGo05r; 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 S1349604AbhDNHJa (ORCPT + 99 others); Wed, 14 Apr 2021 03:09:30 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1348568AbhDNHJL (ORCPT ); Wed, 14 Apr 2021 03:09:11 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D2893C061574 for ; Wed, 14 Apr 2021 00:08:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; 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=cPfVvcbK8T4D/6el7vVPft6WcH2dgQZt6307pfljaIs=; b=QoWGo05rX8BTJjkF5ptA2dFFAt fULA6iI1uxTwTXJF/lE3ZsWeMkLeKB1QnIs7RdgHjQaoiRpQ2+cGn+N5u/oDRXFuuI/1HKTqearur 75+TDpMCNoM+vuMitFtxt4Hboytxc/SLq0NBKh1H4FVPRLFlsdCoscYcHZVBCXvZmS54Frsu8wCZj XnA0fPVx5HV3cD9/N1B6dymb7EahE6hQSKY0kdR0qZiLMdI/B3Ss+O2l7jxf9oGC5AXmAMtj2NOvJ 2M7/a9OvGIpCK7mQYaBZe8yojj0cnlknsfb+kET4/81eiFYcHoaOgVDBL9Dr2/gjYoPfjEM3uq1QN 5vgvzQzg==; Received: from j217100.upc-j.chello.nl ([24.132.217.100] helo=noisy.programming.kicks-ass.net) by casper.infradead.org with esmtpsa (Exim 4.94 #2 (Red Hat Linux)) id 1lWZdL-006mde-Sp; Wed, 14 Apr 2021 07:08:20 +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 35C86300033; Wed, 14 Apr 2021 09:08:19 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id F1B4F203D395C; Wed, 14 Apr 2021 09:08:18 +0200 (CEST) Date: Wed, 14 Apr 2021 09:08:18 +0200 From: Peter Zijlstra To: Guo Ren Cc: 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 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 Wed, Apr 14, 2021 at 10:26:57AM +0800, Guo Ren wrote: > Thx Peter, > > On Tue, Apr 13, 2021 at 4:17 PM Peter Zijlstra wrote: > > > > 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); > > > } > Should it be? > for (;;) { > if (ticket == (u16)val) { > __atomic_acquire_fence(); > break; > } No, atomic_fetch_add() is full smp_mb(), it even has a comment on that says so. Also, __atomic_acquire_fence() is an implementation detail of atomic, and architectures need not provide it. On top of that, IIRC the atomic _acquire/_release have RCpc ordering, where we want our locks to have RCsc ordering (and very much not weaker than RCtso). Even more so, adding barriers to atomics should really not be conditional.