From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-5.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2E4E4C433B4 for ; Wed, 14 Apr 2021 12:57:54 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 101546117A for ; Wed, 14 Apr 2021 12:57:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1350776AbhDNM6O (ORCPT ); Wed, 14 Apr 2021 08:58:14 -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. From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B8A84C433B4 for ; Wed, 14 Apr 2021 12:56:41 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 3C8A3600CD for ; Wed, 14 Apr 2021 12:56:41 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3C8A3600CD Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding :Content-Type:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:Cc:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=3oP4OrNtFat9g4gG6iraX5ejs+cduzaG4RNRoZnl9qQ=; b=T18um49Q4oGbhRfRgmNpOfTN1 7WJO7ee/h9S/6jKNwaKjLSSs0O/uxYbI4Urr2o8ugNu2JSKucMvQUeqeBYYnqxqy6Q/QZQ74to6PG 8itpFKu4W/AYd4zToHqI8aCS7gNTAHXuE5CvMg3XSNPhHkTfwK5i3swZP+wfps7SdJDKj4XZSi3nz +dDCqWL+oaX/vXjx2A4JrRp5CuUAKQXC3XV5YL7seMm4q7JLYaXpqQzoChTAMhHrGYjziGPWGaH9j vh3h3PipEIYbtKO1NAyqftikxwtJww/Hk6WEh6h+TWAPhJaUTr1f/hZbTlFj7gzz1LQgIxZexiexA xYwnqa1yg==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lWf4E-00CeXe-3R; Wed, 14 Apr 2021 12:56:26 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lWf4C-00CeXQ-1r for linux-riscv@desiato.infradead.org; Wed, 14 Apr 2021 12:56:24 +0000 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-Disposition: inline In-Reply-To: X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.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. _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv