Received: by 2002:a05:6a10:17d3:0:0:0:0 with SMTP id hz19csp347189pxb; Wed, 14 Apr 2021 17:25:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1J6ogyaVS5TUDqGV1FeLfrRY0TLzS8niQ2SYiX5y0wTc8nB9bR91jFYAhO0TSO1ZFfkDZ X-Received: by 2002:aa7:8097:0:b029:229:83ec:cac0 with SMTP id v23-20020aa780970000b029022983eccac0mr797937pff.67.1618446312935; Wed, 14 Apr 2021 17:25:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1618446312; cv=none; d=google.com; s=arc-20160816; b=wf0zPcrIv3djzOpz1ELZBmpsmJzBNPpLE8PqifB9Nop9fo+9tBdbeftDVwxKQFJTiX hfsnCbosL1HvuqX8s1p9/Iy12bB6zWa2tQEioGM7ApYs2GNvSYKlTDX4aDfz1dGmmvtt 9esl2LgJZJB2mvOmEnY3VMgZP2Wav027zh2rgehVAiWqzazHZDTbr/KUfriecynieA/E o3uP+Qofpf1j8w5bD/SHg8f3cIOzgdx9qaC3ZydBGJe12OrmqNc5UhqwNTzpTdbJJerC 9YERtfoyd51DBmBHLVBoS9+5y31ob98XwsZxobCun9wQe0/mz7cFqxTkJ6CE7JyCzX5C yXDA== 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=Xvv1QEzJPf3S0gZF7Gup5VVBeUBTNACCxNNuZgdPkBs=; b=QycxtY8AGPyrnChtcJWpDsrgOBXwgdKXuvZh8aSlgExdv4BI20k3xGJU0GTO2RHNlK 6i6TcFaHiZf/AQfB/wkQsV6hjhouSRtGAswmp9IgHo06YkvpBy25/+PCzKGvptWEXfSP Hi7CcTsjA1sp1WBDWYDVp8dd+T1+GUlm9Nz3eboNYyIBL2J1uGQdVoex3ZMNXwZAcWo0 r0JQUf5IhzeGHvvxrWe4Ny9nkMgXDj9pH4iZXpsFSXEVIxcKu67iMXIWUGtVgnIcP5l/ Qa9anplPXe1NzbwrDiEfsZetwBlTrwVIukJIE3A87xNt91/DZFTIGrARnOaxJmMd3unA fK9A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=CO3wZOW6; 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 j12si1228655pfi.167.2021.04.14.17.25.00; Wed, 14 Apr 2021 17:25:12 -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=CO3wZOW6; 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 S1347675AbhDNM6L (ORCPT + 99 others); Wed, 14 Apr 2021 08:58:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240417AbhDNM5t (ORCPT ); Wed, 14 Apr 2021 08:57:49 -0400 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id F0F4CC061574 for ; Wed, 14 Apr 2021 05:57:21 -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=Xvv1QEzJPf3S0gZF7Gup5VVBeUBTNACCxNNuZgdPkBs=; b=CO3wZOW6Mfb25oRUOR2ACzdI6x ILopvgZEOWCldFmiqIMGpnOEcMfpqhcSGI6VM16mkpHiqlYlibnvLdtR2xSwnSFm+tpoSoNM5wCI+ noKQBvj8QIaOygEsA1/oQNlm0SJ4egamJCByKHuIKCmlGEUlbWUrYYhnQDNC9gpN3n88faM9GeWlo ds49BK+Ic3U52aJY98ydMPQodHo+tTUBP4nZJMQTp3AgPoJZsCVrNMSeWlYMZiImZ0K4ygFeVMNb9 IlsQsqDhLngnjh7QbSIL7DQ1hC8LIzUREfjJx073+i1A3xhhT6axXPUJCHsQHAhWmhSoEelCZv5Px zJROLVMg==; 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 1lWf3n-00782E-9P; Wed, 14 Apr 2021 12:56:12 +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 D7F9C300222; Wed, 14 Apr 2021 14:55:57 +0200 (CEST) Received: by hirez.programming.kicks-ass.net (Postfix, from userid 1000) id BA4532065A47C; Wed, 14 Apr 2021 14:55:57 +0200 (CEST) Date: Wed, 14 Apr 2021 14:55:57 +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 , Jonas Bonn , Stefan Kristiansson , Stafford Horne Subject: Re: [RFC][PATCH] locking: Generic ticket-lock 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 08:39:33PM +0800, Guo Ren wrote: > I've tested it on csky SMP*4 hw (860) & riscv SMP*4 hw (c910) and it's okay. W00t :-) > Hope you can keep > typedef struct { > union { > atomic_t lock; > struct __raw_tickets { > #ifdef __BIG_ENDIAN > u16 next; > u16 owner; > #else > u16 owner; > u16 next; > #endif > } tickets; > }; > } arch_spinlock_t; > > Using owner & next is much more readable. That almost doubles the line-count of the thing ;-) > > + * It further assumes atomic_*_release() + atomic_*_acquire() is RCpc and hence > > + * uses atomic_fetch_add() which is SC to create an RCsc lock. This ^^^ then vvv > > +static __always_inline void ticket_lock(arch_spinlock_t *lock) > > +{ > > + u32 val = atomic_fetch_add(1<<16, lock); /* SC, gives us RCsc */ > atomic_fetch_add_acquire ? Then we must rely on the arch to implement RCsc atomics. And I for one can never tell wth Risc-V actually does. > > +static __always_inline int ticket_is_locked(arch_spinlock_t *lock) > > +{ > > + u32 val = atomic_read(lock); > > + > > + return ((val >> 16) != (val & 0xffff)); > I perfer: > return !arch_spin_value_unlocked(READ_ONCE(*lock)); > > +} > > +} > > + > > +static __always_inline int ticket_value_unlocked(arch_spinlock_t lock) > > +{ > > + return !ticket_is_locked(&lock); > Are you sure to let ticket_is_locked->atomic_read(lock) again, the > lock has contained all information? > > return lock.tickets.owner == lock.tickets.next; Yeah, I wrote then the wrong way around. Couldn't be bothered to go back when I figured it out. > > + > > +static __always_inline int ticket_is_contended(arch_spinlock_t *lock) > > +{ > > + u32 val = atomic_read(lock); > > + > > + return (s16)((val >> 16) - (val & 0xffff)) > 1; > How big-endian ? How not? Endian-ness only matters when you go poke at sub-words, which the above does not. Only ticket_unlock() does and cares about that.