Received: by 2002:a05:6500:1b8f:b0:1fa:5c73:8e2d with SMTP id df15csp40379lqb; Tue, 28 May 2024 08:18:13 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXw6/w6XeCsg5uqVwVSXbLTo0d7oUPf8sJ0vFvLBbYK+ZOcsccxffxjIYxcymg3MfShjjnc5NTXuykgPXVXTwecBsxPgLXqLHjNznIR5g== X-Google-Smtp-Source: AGHT+IGcLoJnZp199+ygfSy0v+S4AQ5/sS0d+yJ+CJgjit/wSYi+5TZRDdvW6hHm4TdsGWQ/0ZW7 X-Received: by 2002:a05:6102:b08:b0:47e:f157:fe9b with SMTP id ada2fe7eead31-48a385b3b6emr13573861137.19.1716909493095; Tue, 28 May 2024 08:18:13 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716909493; cv=pass; d=google.com; s=arc-20160816; b=BfyePJ7qwKp55biACuWtMfYaOUqJ4SA2Wy8PvdV8SXC3CVVNySiWeNKcz/Xx8GDqUq q0DUgVEp+3xwAACd+DN43VSiwjpRuCiCjZDX/EurX2lZAcBKb3F5K1CCl27XLlWWpydM LIAiy1KOBG+vegaYKwCGYFqq+7E+cQplLqK9hZqO2ZqnsxXTVMHGGR/iypFNeUXLWyYS phFKPmIJn8dQvxb0iuMw+4CVyHFF1Ude08cH3soPqYndbGloBTmkBi7VUjuFQcoFONRT 58drWv/3IIlEErWfVMRXTXKjjT6gjJg9Za1N8fx8Sn9nvTP71TNIvE4z+S0wLcxZZ8NU IBhg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=JpMFH6VmSNUk91jIQ+0kjGx11FiPCz2ltKgHshZWUPg=; fh=brIWfOmgxCiYWy+Bqy0Chq1dIcAsDV68zE05skLKm0g=; b=Xolj3qR4D2P4BqqXa+V1ZpNMmhgYQEQCJQUG4DtmqzHje38GsOxzyF6zjt4t/JvW0E fGwB7t0O+PB8JGV1JKFNrX/OZUVHVFAufdOGZig5ti6GX/r5B+DwyQ9r9k7CpaW4tUC4 szImkHp86ckgS66e1/Qs+td8za7cML7+U57xRd8bOTk4lv6yeeRUDHgDf3c1qfjfjr3/ 6ylRTrbSIW//lYklRGECZnhLna/3H2SZFbpgnrtJjIloTTmP0UyNbtSb1eG0yiA60rO+ YF16EoYeWBvB4RE8k7xgZmg82gUFIBv+mZ15jLHszMp551eKED5Y7kqfIyERw4JZjH0V IrEg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=HlEkmPBY; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-192649-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192649-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id ada2fe7eead31-48a3a3551a4si2623991137.503.2024.05.28.08.18.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 08:18:13 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-192649-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=HlEkmPBY; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-192649-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-192649-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id A4BB51C231CF for ; Tue, 28 May 2024 15:18:08 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2DFB516D335; Tue, 28 May 2024 15:17:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="HlEkmPBY" Received: from mail-wr1-f46.google.com (mail-wr1-f46.google.com [209.85.221.46]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3261CF9D6 for ; Tue, 28 May 2024 15:17:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.46 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716909425; cv=none; b=OXVfiGeWOSe/fLhItZiF2d0IkqEHlWg8Kt2C9aYG8F2cBrCtsKd0lBM5iyNvLY8LhjDBOGkXvJlmk2l6RJALJJDbm9zScTud147uE+rP2zf6qngfNmOuoZgr5G6C4sOXCFCZK94+LiYYvCWICbCsuSy6f5GtOOU3p2KvulU7Uj8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716909425; c=relaxed/simple; bh=o4ZK/GG6lXAN1cfDXgIpVYZWTFwSck3jVd1HEtW4i7A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XbZexfgejFFiFfzlTYpC0Y7sTYrVGrqCiIFLwWOR4Qd+L2jCQ4376kgpjBfbASLtWZAUhjLyb2wjJipoHmR5ZPAZG1iFXK4v9K4Hks2G2DZ/2dlxRdRdRVgGiQ5oCSai9jnDXzIBK2gS9sNPaVLucgtaLQ/FAjjs6bqQy+FgzdY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=HlEkmPBY; arc=none smtp.client-ip=209.85.221.46 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-wr1-f46.google.com with SMTP id ffacd0b85a97d-354f14bd80cso599747f8f.1 for ; Tue, 28 May 2024 08:17:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716909422; x=1717514222; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=JpMFH6VmSNUk91jIQ+0kjGx11FiPCz2ltKgHshZWUPg=; b=HlEkmPBYlvrpedzK6tVnunSoPJGo6506m+heu9GCXVFbwWrxdV34Qi6K0FML+A2BF/ CsU9jlUCxMcChAzjUUBXlzmv2b0cwB5TA5xY7tBErLQOMOWYt759orDkcKp+EZeDjCep 8Zrk8foNA8h6YdHMuYqAv3B7niR2uSp5hkq0jHwsnwtl4C1HfoTyD0jtJFNpV3tHxoOl zjS8K+hEbwkiBxE1qv0Gfiv6hyp0YBq7OFE8MVOz1lVFt/rwrP96ScChw4x5uVACanfS +R1xjpryblG3TfpQhBdvekAfKEWj0C8heb2Vt9m0qW+VH1tv+qLekpZwvREfXY0kakS8 Nm+g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716909422; x=1717514222; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=JpMFH6VmSNUk91jIQ+0kjGx11FiPCz2ltKgHshZWUPg=; b=EfV5C7VT6VsgTP8muXDTcWtp9/U0qbpOBs279+SN21yzTHHstcm/pLtZw69a8MMGij 9p6WPMQoHYDtu045RB08DIXEz+lNWjvDzQ/qurXtYGndmwlrIU+zuSdBpyv0eINGkOY2 rtM5lrrmg1ZzDFb6J/xnKOgxvSBY6Dvzx8Ws/1PznHrZ0yEtLSauktBnwp1Hi4OVQCOR pacAwI2perYd5d50oXp1nOHAwvtYNW++PZU+mYhcmrDt0PJloWSs51XTa7NtROetzfYZ eldZE9isctO3iY0ZtAKhZ5n4QMoaytXm/Fpmznhe2GpQmDp9XyOG/fgB1nppVEB6jq8e TToQ== X-Forwarded-Encrypted: i=1; AJvYcCWcZME/lQFn0J1kwwaebsP61BwuFROnS+pF+MVn7yCLaejee/gJugCBmImAoyz+2prKsZK0jp14c2P9FGjr/Q5sQOnE3GGlrYOstdK/ X-Gm-Message-State: AOJu0Yw0rd5L8kDo/JGAguhkdDFSLW03eZvdSp7uoDUfpEQO7UsCMJpT Jr+F3zjBiiB90bSDQasVnzUXSjJRPWFSHt/s/Ea6IJ/r3/vY25c57mvjqYa5Zpo= X-Received: by 2002:a5d:4e43:0:b0:354:f9f0:a7ed with SMTP id ffacd0b85a97d-3552fdfa5d4mr7849836f8f.47.1716909421494; Tue, 28 May 2024 08:17:01 -0700 (PDT) Received: from alex-rivos.home (amontpellier-656-1-456-62.w92-145.abo.wanadoo.fr. [92.145.124.62]) by smtp.gmail.com with ESMTPSA id ffacd0b85a97d-3564afc3577sm11599649f8f.102.2024.05.28.08.17.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 28 May 2024 08:17:01 -0700 (PDT) From: Alexandre Ghiti To: Jonathan Corbet , Paul Walmsley , Palmer Dabbelt , Albert Ou , Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng , Arnd Bergmann , Leonardo Bras , Guo Ren , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-arch@vger.kernel.org Cc: Guo Ren Subject: [PATCH 6/7] asm-generic: ticket-lock: Add separate ticket-lock.h Date: Tue, 28 May 2024 17:10:51 +0200 Message-Id: <20240528151052.313031-7-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240528151052.313031-1-alexghiti@rivosinc.com> References: <20240528151052.313031-1-alexghiti@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Guo Ren Add a separate ticket-lock.h to include multiple spinlock versions and select one at compile time or runtime. Reviewed-by: Leonardo Bras Suggested-by: Arnd Bergmann Link: https://lore.kernel.org/linux-riscv/CAK8P3a2rnz9mQqhN6-e0CGUUv9rntRELFdxt_weiD7FxH7fkfQ@mail.gmail.com/ Signed-off-by: Guo Ren Signed-off-by: Guo Ren --- include/asm-generic/spinlock.h | 87 +--------------------- include/asm-generic/ticket_spinlock.h | 103 ++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 86 deletions(-) create mode 100644 include/asm-generic/ticket_spinlock.h diff --git a/include/asm-generic/spinlock.h b/include/asm-generic/spinlock.h index 4773334ee638..970590baf61b 100644 --- a/include/asm-generic/spinlock.h +++ b/include/asm-generic/spinlock.h @@ -1,94 +1,9 @@ /* 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 RCsc to create an RCsc hot path, along with - * a full fence after the spin to upgrade the otherwise-RCpc - * atomic_cond_read_acquire(). - * - * 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_SPINLOCK_H #define __ASM_GENERIC_SPINLOCK_H -#include -#include - -static __always_inline void arch_spin_lock(arch_spinlock_t *lock) -{ - u32 val = atomic_fetch_add(1<<16, &lock->val); - u16 ticket = val >> 16; - - if (ticket == (u16)val) - return; - - /* - * atomic_cond_read_acquire() is RCpc, but rather than defining a - * custom cond_read_rcsc() here we just emit a full fence. We only - * need the prior reads before subsequent writes ordering from - * smb_mb(), but as atomic_cond_read_acquire() just emits reads and we - * have no outstanding writes due to the atomic_fetch_add() the extra - * orderings are free. - */ - atomic_cond_read_acquire(&lock->val, ticket == (u16)VAL); - smp_mb(); -} - -static __always_inline bool arch_spin_trylock(arch_spinlock_t *lock) -{ - u32 old = atomic_read(&lock->val); - - if ((old >> 16) != (old & 0xffff)) - return false; - - return atomic_try_cmpxchg(&lock->val, &old, old + (1<<16)); /* SC, for RCsc */ -} - -static __always_inline void arch_spin_unlock(arch_spinlock_t *lock) -{ - u16 *ptr = (u16 *)lock + IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); - u32 val = atomic_read(&lock->val); - - smp_store_release(ptr, (u16)val + 1); -} - -static __always_inline int arch_spin_value_unlocked(arch_spinlock_t lock) -{ - u32 val = lock.val.counter; - - return ((val >> 16) == (val & 0xffff)); -} - -static __always_inline int arch_spin_is_locked(arch_spinlock_t *lock) -{ - arch_spinlock_t val = READ_ONCE(*lock); - - return !arch_spin_value_unlocked(val); -} - -static __always_inline int arch_spin_is_contended(arch_spinlock_t *lock) -{ - u32 val = atomic_read(&lock->val); - - return (s16)((val >> 16) - (val & 0xffff)) > 1; -} - +#include #include #endif /* __ASM_GENERIC_SPINLOCK_H */ diff --git a/include/asm-generic/ticket_spinlock.h b/include/asm-generic/ticket_spinlock.h new file mode 100644 index 000000000000..cfcff22b37b3 --- /dev/null +++ b/include/asm-generic/ticket_spinlock.h @@ -0,0 +1,103 @@ +/* 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 RCsc to create an RCsc hot path, along with + * a full fence after the spin to upgrade the otherwise-RCpc + * atomic_cond_read_acquire(). + * + * 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_SPINLOCK_H +#define __ASM_GENERIC_TICKET_SPINLOCK_H + +#include +#include + +static __always_inline void ticket_spin_lock(arch_spinlock_t *lock) +{ + u32 val = atomic_fetch_add(1<<16, &lock->val); + u16 ticket = val >> 16; + + if (ticket == (u16)val) + return; + + /* + * atomic_cond_read_acquire() is RCpc, but rather than defining a + * custom cond_read_rcsc() here we just emit a full fence. We only + * need the prior reads before subsequent writes ordering from + * smb_mb(), but as atomic_cond_read_acquire() just emits reads and we + * have no outstanding writes due to the atomic_fetch_add() the extra + * orderings are free. + */ + atomic_cond_read_acquire(&lock->val, ticket == (u16)VAL); + smp_mb(); +} + +static __always_inline bool ticket_spin_trylock(arch_spinlock_t *lock) +{ + u32 old = atomic_read(&lock->val); + + if ((old >> 16) != (old & 0xffff)) + return false; + + return atomic_try_cmpxchg(&lock->val, &old, old + (1<<16)); /* SC, for RCsc */ +} + +static __always_inline void ticket_spin_unlock(arch_spinlock_t *lock) +{ + u16 *ptr = (u16 *)lock + IS_ENABLED(CONFIG_CPU_BIG_ENDIAN); + u32 val = atomic_read(&lock->val); + + smp_store_release(ptr, (u16)val + 1); +} + +static __always_inline int ticket_spin_value_unlocked(arch_spinlock_t lock) +{ + u32 val = lock.val.counter; + + return ((val >> 16) == (val & 0xffff)); +} + +static __always_inline int ticket_spin_is_locked(arch_spinlock_t *lock) +{ + arch_spinlock_t val = READ_ONCE(*lock); + + return !ticket_spin_value_unlocked(val); +} + +static __always_inline int ticket_spin_is_contended(arch_spinlock_t *lock) +{ + u32 val = atomic_read(&lock->val); + + return (s16)((val >> 16) - (val & 0xffff)) > 1; +} + +/* + * Remapping spinlock architecture specific functions to the corresponding + * ticket spinlock functions. + */ +#define arch_spin_is_locked(l) ticket_spin_is_locked(l) +#define arch_spin_is_contended(l) ticket_spin_is_contended(l) +#define arch_spin_value_unlocked(l) ticket_spin_value_unlocked(l) +#define arch_spin_lock(l) ticket_spin_lock(l) +#define arch_spin_trylock(l) ticket_spin_trylock(l) +#define arch_spin_unlock(l) ticket_spin_unlock(l) + +#endif /* __ASM_GENERIC_TICKET_SPINLOCK_H */ -- 2.39.2