Received: by 2002:a05:6358:a55:b0:ec:fcf4:3ecf with SMTP id 21csp6512206rwb; Wed, 18 Jan 2023 06:11:33 -0800 (PST) X-Google-Smtp-Source: AMrXdXtU1uEUUhQNXrWhky/zH/AHW0zaCmE3Jk62ZyC6ANQTi8L05HgoJDpr8r/jRQWBC5FfmXR7 X-Received: by 2002:a17:902:ccce:b0:194:91eb:5b84 with SMTP id z14-20020a170902ccce00b0019491eb5b84mr8742692ple.22.1674051092786; Wed, 18 Jan 2023 06:11:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1674051092; cv=none; d=google.com; s=arc-20160816; b=JISJzF2TwJxT6vvdEb5uSlLJbYecqVAS/LDGowqeie3rBcYGbnMcwklWIA/wBIJzQL 6mDLSBFX0w5bSfa20qSFT9W+h39L/Lo72GqQ5HgTyiAc3Ugsc/YEI+RQtyx97Y11ZVXC McwE+YJVWW7kJDMCbt6Zlls+JuecnQ8qYJ92yyS6GGzDU4j8Oi/klwOBwyfAh1X405AJ CGUhvdIxvj5dlOI6LmZGvgNqPNwbn2PYAm0Hs+5MLo5t+0rTCs60VToBr6GlBTaAcQmm DwFiPaCQ84PNDvFBNaK0Q45+kqYZ5zBx9vdYpHiTQ43OK+5oEZSKAK6n/II7LdXrHOZt Qx4w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:from :references:cc:to:content-language:subject:user-agent:mime-version :date:message-id:dkim-signature:dkim-filter:secumail-id :dkim-signature; bh=58T3bVGsMNkDvPnQtkuPdqBFBPPv2eFSDsL5jLuYa4k=; b=eDYYbEdf5Bc+pQKvk+TmM7uywvNWf7XSa3NpzQicIX/notSXCSsDeHmd9673aVacXl GWbM6ufrNKh9r9WLez72LOGUxHKPclces9iiyXru/Q0RIYXc8kCMScFJiaHObLciHGMx rcXvgepjV32N0pBAmVRWnw2r63ozSXeQptJJxF7ggeRkE4ZHU2FRX3VMzQbxvzRhBOfo Ol8wKPq9GgMcYbn5XZZZfJA8g7tNyc1wL9xwds1LwBCia7MlOhY3oSLW5dA4CqBek3VR YKn+m/CfR4aiUvAwUcWWytZMv79DFp4c5Ic7mEczKSkmDtMrmHTDEZajCvf+nn+W6PKJ y/7g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kalray.eu header.s=sec-sig-email header.b=6NhlR0kH; dkim=pass header.i=@kalray.eu header.s=32AE1B44-9502-11E5-BA35-3734643DEF29 header.b=km45DKDO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=kalray.eu Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id h14-20020a170902f70e00b00192a2db815dsi36147374plo.375.2023.01.18.06.11.26; Wed, 18 Jan 2023 06:11:32 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kalray.eu header.s=sec-sig-email header.b=6NhlR0kH; dkim=pass header.i=@kalray.eu header.s=32AE1B44-9502-11E5-BA35-3734643DEF29 header.b=km45DKDO; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=NONE dis=NONE) header.from=kalray.eu Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231273AbjAROEh (ORCPT + 45 others); Wed, 18 Jan 2023 09:04:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60952 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231171AbjARODv (ORCPT ); Wed, 18 Jan 2023 09:03:51 -0500 Received: from fx302.security-mail.net (mxout.security-mail.net [85.31.212.42]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D566A49551 for ; Wed, 18 Jan 2023 05:40:29 -0800 (PST) Received: from localhost (localhost [127.0.0.1]) by fx302.security-mail.net (Postfix) with ESMTP id 47CE81C3E3B1 for ; Wed, 18 Jan 2023 14:40:28 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kalray.eu; s=sec-sig-email; t=1674049228; bh=wVUxhJVoV68waKeEkhzEL6KY6t3QAA6/TKJ0oALeiPc=; h=Date:Subject:To:Cc:References:From:In-Reply-To; b=6NhlR0kH2OAdEGOrXcTYdvnLQapEiue00FjasOzHooMkl5n9ZTPvk0Pexc2IW2wEf oLBwg9aVvchmgroYTkQXLRNiDAJXa48yb9rBdvgX2Gwd7ACCB2rIdRvj1QVwbZunmK bMXqpZmYb7umLio9NKemyXhmLOE9dxnuzMizJtGo= Received: from fx302 (localhost [127.0.0.1]) by fx302.security-mail.net (Postfix) with ESMTP id 1CDEF1C3E29E; Wed, 18 Jan 2023 14:40:28 +0100 (CET) Received: from zimbra2.kalray.eu (unknown [217.181.231.53]) by fx302.security-mail.net (Postfix) with ESMTPS id 4C76C1C3E272; Wed, 18 Jan 2023 14:40:27 +0100 (CET) Received: from zimbra2.kalray.eu (localhost [127.0.0.1]) by zimbra2.kalray.eu (Postfix) with ESMTPS id 23A0F27E0431; Wed, 18 Jan 2023 14:40:27 +0100 (CET) Received: from localhost (localhost [127.0.0.1]) by zimbra2.kalray.eu (Postfix) with ESMTP id 0842227E043E; Wed, 18 Jan 2023 14:40:27 +0100 (CET) Received: from zimbra2.kalray.eu ([127.0.0.1]) by localhost (zimbra2.kalray.eu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id MbMNEJPQrWTX; Wed, 18 Jan 2023 14:40:26 +0100 (CET) Received: from [127.0.0.1] (unknown [192.168.37.161]) by zimbra2.kalray.eu (Postfix) with ESMTPSA id 6DB2D27E0431; Wed, 18 Jan 2023 14:40:26 +0100 (CET) X-Virus-Scanned: E-securemail Secumail-id: <199b.63c7f6cb.4ac6c.0> DKIM-Filter: OpenDKIM Filter v2.10.3 zimbra2.kalray.eu 0842227E043E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kalray.eu; s=32AE1B44-9502-11E5-BA35-3734643DEF29; t=1674049227; bh=58T3bVGsMNkDvPnQtkuPdqBFBPPv2eFSDsL5jLuYa4k=; h=Message-ID:Date:MIME-Version:To:From; b=km45DKDOC2u0nr2RV2H37ZDEYjR9PXAOeVDCTdIps52vx7n+8C39kutjfP/WdX8Er hObRI28opJsJclYo12d04yTCEBqG9ZREZpO0JJekeqlGta8MitngskxbEDkj7u/Zgy TaxbvIS46LRbVQ520+OPBLoNudc1C852szRn+fX8= Message-ID: <752bdbe1-2ca5-2322-75fe-b4abd90e448c@kalray.eu> Date: Wed, 18 Jan 2023 14:40:26 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: [RFC PATCH 05/25] kvx: Add atomic/locking headers Content-Language: en-us To: Mark Rutland , Jules Maselbas Cc: Will Deacon , Peter Zijlstra , Boqun Feng , linux-kernel@vger.kernel.org, Clement Leger , Julian Vetter , Julien Villette References: <20230103164359.24347-1-ysionneau@kalray.eu> <20230103164359.24347-6-ysionneau@kalray.eu> <20230106141158.GC7446@tellis.lin.mbt.kalray.eu> From: Yann Sionneau In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-ALTERMIMEV2_out: done X-Spam-Status: No, score=-2.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,NICE_REPLY_A,RCVD_IN_DNSWL_LOW, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Mark, On 10/01/2023 14:24, Mark Rutland wrote: > On Fri, Jan 06, 2023 at 03:11:58PM +0100, Jules Maselbas wrote: >> Hi Mark, >> >> On Wed, Jan 04, 2023 at 09:53:24AM +0000, Mark Rutland wrote: >>> On Tue, Jan 03, 2023 at 05:43:39PM +0100, Yann Sionneau wrote: >>>> Add common headers (atomic, bitops, barrier and locking) for basic >>>> kvx support. >>>> >>>> CC: Will Deacon >>>> CC: Peter Zijlstra >>>> CC: Boqun Feng >>>> CC: Mark Rutland >>>> CC: linux-kernel@vger.kernel.org >>>> Co-developed-by: Clement Leger >>>> Signed-off-by: Clement Leger >>>> Co-developed-by: Jules Maselbas >>>> Signed-off-by: Jules Maselbas >>>> Co-developed-by: Julian Vetter >>>> Signed-off-by: Julian Vetter >>>> Co-developed-by: Julien Villette >>>> Signed-off-by: Julien Villette >>>> Co-developed-by: Yann Sionneau >>>> Signed-off-by: Yann Sionneau >>>> --- >>>> arch/kvx/include/asm/atomic.h | 104 +++++++++++++++++ >>>> arch/kvx/include/asm/barrier.h | 15 +++ >>>> arch/kvx/include/asm/bitops.h | 207 +++++++++++++++++++++++++++++++++ >>>> arch/kvx/include/asm/bitrev.h | 32 +++++ >>>> arch/kvx/include/asm/cmpxchg.h | 185 +++++++++++++++++++++++++++++ >>>> 5 files changed, 543 insertions(+) >>>> create mode 100644 arch/kvx/include/asm/atomic.h >>>> create mode 100644 arch/kvx/include/asm/barrier.h >>>> create mode 100644 arch/kvx/include/asm/bitops.h >>>> create mode 100644 arch/kvx/include/asm/bitrev.h >>>> create mode 100644 arch/kvx/include/asm/cmpxchg.h >>>> >>>> diff --git a/arch/kvx/include/asm/atomic.h b/arch/kvx/include/asm/atomic.h >>>> new file mode 100644 >>>> index 000000000000..eb8acbcbc70d >>>> --- /dev/null >>>> +++ b/arch/kvx/include/asm/atomic.h >>>> @@ -0,0 +1,104 @@ >>>> +/* SPDX-License-Identifier: GPL-2.0-only */ >>>> +/* >>>> + * Copyright (C) 2017-2023 Kalray Inc. >>>> + * Author(s): Clement Leger >>>> + */ >>>> + >>>> +#ifndef _ASM_KVX_ATOMIC_H >>>> +#define _ASM_KVX_ATOMIC_H >>>> + >>>> +#include >>>> + >>>> +#include >>>> + >>>> +#define ATOMIC64_INIT(i) { (i) } >>>> + >>>> +#define arch_atomic64_cmpxchg(v, old, new) (arch_cmpxchg(&((v)->counter), old, new)) >>>> +#define arch_atomic64_xchg(v, new) (arch_xchg(&((v)->counter), new)) >>>> + >>>> +static inline long arch_atomic64_read(const atomic64_t *v) >>>> +{ >>>> + return v->counter; >>>> +} >>> This is a plain read, and is *not* atomic. >>> >>> The compiler can replay a plain read an arbitrary number of times, and is >>> permitted to split it into smaller accesses. >>> >>> At minimum this needs to be >>> >>> READ_ONCE(v->counter) >>> >>> ... which will prevent replay. Whether or not that's actually atomic will >>> depend on the instructions the compiler generates, and how those instructions >>> are defines in your architecture. >> Good point, we are going to use {READ,WRITE}_ONCE macros Done for V2 (which we will send pretty soon). >> >>> Do you have a single instruction that can read a 64-bit memory location, and is >>> it guaranteed to result in a single access that cannot be split? >> We do have a single instruction that can read a 64-bit memory location >> (supported sizes are 8, 16, 32, 64, 128, 256). >> All accesses are guaranteed to not be split, unless they are misaligned. >> Furthermore, misaligned write accesses crossing a 32-byte boundary may >> complete in a non-atomic way. > Perfect, thanks for confirming! > > [...] > >>>> +static inline void arch_atomic64_set(atomic64_t *v, long i) >>>> +{ >>>> + v->counter = i; >>>> +} >>> Same comments as for arch_atomic64_read(); at minimum this needs to be: >>> >>> WRITE_ONCE(v->counter, i) >>> >>> ... but that may or may not actually be atomic on your architecture. >>> >>>> +#define ATOMIC64_RETURN_OP(op, c_op) \ >>>> +static inline long arch_atomic64_##op##_return(long i, atomic64_t *v) \ >>>> +{ \ >>>> + long new, old, ret; \ >>>> + \ >>>> + do { \ >>>> + old = v->counter; \ >>>> + new = old c_op i; \ >>>> + ret = arch_cmpxchg(&v->counter, old, new); \ >>>> + } while (ret != old); \ >>>> + \ >>>> + return new; \ >>>> +} >>>> + >>>> +#define ATOMIC64_OP(op, c_op) \ >>>> +static inline void arch_atomic64_##op(long i, atomic64_t *v) \ >>>> +{ \ >>>> + long new, old, ret; \ >>>> + \ >>>> + do { \ >>>> + old = v->counter; \ >>>> + new = old c_op i; \ >>>> + ret = arch_cmpxchg(&v->counter, old, new); \ >>>> + } while (ret != old); \ >>>> +} >>>> + >>>> +#define ATOMIC64_FETCH_OP(op, c_op) \ >>>> +static inline long arch_atomic64_fetch_##op(long i, atomic64_t *v) \ >>>> +{ \ >>>> + long new, old, ret; \ >>>> + \ >>>> + do { \ >>>> + old = v->counter; \ >>>> + new = old c_op i; \ >>>> + ret = arch_cmpxchg(&v->counter, old, new); \ >>>> + } while (ret != old); \ >>>> + \ >>>> + return old; \ >>>> +} >>> These look ok, but it'd be nicer if we could teach the generic atomic64 code to >>> do this, like the generic atomic code does. >>> >>> We could rename the existing asm-generic/atomic64 code to atomic64-spinlock, >>> and add a separate atomic64-cmpxchg (and likewise for the 32-bit code) to make >>> that clearer and consistent. >> I am not sure what this implies and how big this change might be, >> but I'll take a look at this. > Hmm... from a quick attempt just now it looks like that will be a bit more > churny than I thought. > > We can always factor this out later, so feel free to leave it as-is, thgouh if > we could make this genric (and have it look like asm-generic/atomic.h), it'd be > nice for consistency and maintenance. Ack for doing it later. > >>>> + >>>> +#define ATOMIC64_OPS(op, c_op) \ >>>> + ATOMIC64_OP(op, c_op) \ >>>> + ATOMIC64_RETURN_OP(op, c_op) \ >>>> + ATOMIC64_FETCH_OP(op, c_op) >>>> + >>>> +ATOMIC64_OPS(and, &) >>>> +ATOMIC64_OPS(or, |) >>>> +ATOMIC64_OPS(xor, ^) >>>> +ATOMIC64_OPS(add, +) >>>> +ATOMIC64_OPS(sub, -) >>>> + >>>> +#undef ATOMIC64_OPS >>>> +#undef ATOMIC64_FETCH_OP >>>> +#undef ATOMIC64_OP >>>> + >>>> +static inline int arch_atomic_add_return(int i, atomic_t *v) >>>> +{ >>>> + int new, old, ret; >>>> + >>>> + do { >>>> + old = v->counter; >>>> + new = old + i; >>>> + ret = arch_cmpxchg(&v->counter, old, new); >>>> + } while (ret != old); >>>> + >>>> + return new; >>>> +} >>>> + >>>> +static inline int arch_atomic_sub_return(int i, atomic_t *v) >>>> +{ >>>> + return arch_atomic_add_return(-i, v); >>>> +} >>> Likewise for these two. > The 32-bit atomics should come from asm-generic/atomic.h and not be necesary, > that has both arch_atomic_add_return() and arch_atomic_sub_return(). > >>>> diff --git a/arch/kvx/include/asm/cmpxchg.h b/arch/kvx/include/asm/cmpxchg.h >>>> new file mode 100644 >>>> index 000000000000..b1d128b060a2 >>>> --- /dev/null >>>> +++ b/arch/kvx/include/asm/cmpxchg.h >>>> @@ -0,0 +1,185 @@ >>>> +/* SPDX-License-Identifier: GPL-2.0-only */ >>>> +/* >>>> + * Copyright (C) 2017-2023 Kalray Inc. >>>> + * Author(s): Clement Leger >>>> + * Yann Sionneau >>>> + */ >>>> + >>>> +#ifndef _ASM_KVX_CMPXCHG_H >>>> +#define _ASM_KVX_CMPXCHG_H >>>> + >>>> +#include >>>> +#include >>>> +#include >>>> + >>>> +/* >>>> + * On kvx, we have a boolean compare and swap which means that the operation >>>> + * returns only the success of operation. >>>> + * If operation succeed, this is simple, we just need to return the provided >>>> + * old value. However, if it fails, we need to load the value to return it for >>>> + * the caller. If the loaded value is different from the "old" provided by the >>>> + * caller, we can return it since it will means it failed. >>>> + * However, if for some reason the value we read is equal to the old value >>>> + * provided by the caller, we can't simply return it or the caller will think it >>>> + * succeeded. So if the value we read is the same as the "old" provided by >>>> + * the caller, we try again until either we succeed or we fail with a different >>>> + * value than the provided one. >>>> + */ >>>> +#define __cmpxchg(ptr, old, new, op_suffix, load_suffix) \ >>>> +({ \ >>>> + register unsigned long __rn asm("r62"); \ >>>> + register unsigned long __ro asm("r63"); \ >>> Why do you need to specify the exact registers? >> r62 and r63 are hardcoded in the inline assembly, they are caller saved. >> I have a C implementation that uses builtins however this is not merged >> in our tree yet (but I want to). >> >>> e.g. does some instruction use these implicitly, or do you need two adjacent >>> register for encoding reasons? >> The atomic compare and swap (acswap) instruction needs a register "pair" >> which can only exists with "adjacent" registers: $r0r1, $r2r3 ect. > Ok; and you don't have a way to ask GCC for an arbitrary register pair, so you > chose r62+r63 as they can be clobbered? > > It might be worth looking into using an __int128_t to give you a register pair, > assuming your calling convention mandares adjacency of the two halves. That > could give you the pair while giving the compiler freedom to chose any suitable > pair (assuming you have a suitable operand modifier to extract the low/high > registers from the asm operand. In the V2 we replaced the assembly code with C code. acswap{d,w} instruction is generated by the mean of a compiler builtin. This way the code is much more readable and it allows to not hard code some register pair, letting the compiler allocate them. > >>>> + __asm__ __volatile__ ( \ >>>> + /* Fence to guarantee previous store to be committed */ \ >>>> + "fence\n" \ >>> This implies you can implement the relaxed form of cmpxchg(). >>> >>> What ordering do you get by default, and do you have any other barriers (e.g. >>> for acquire/release semantics), or just "fence" ? >> We have two barrier types: >> - fence: ensure that all uncached memory operations are committed to >> memory, typically used to ensure a write to memory is visible to >> other cores. >> - barrier: flush the core instruction pipeline and memory system > Ok. > > In the absence of barriers do you have any ordering guarantee? e.g. weak like > arm/power, TSO like x86, or something else entirely? Our memory ordering model is weak. ``` store @data_addr = new_value ;; store @data_is_valid_addr = 1 ;; ``` By running the previous pseudo-code, there is no guarantee that another CPU would see the *data_addr == new_value just because it sees *data_is_valid_addr == 1 The correct code would need an extra bundle containing a `fence` instruction in-between the 2 other bundles. > When you say "uncached memory operations", does that mean that you have cached > memory operatins which are not affected by the barriers, or is "uncached memory > operations" just meant to read as "oustanding/incomplete memory operations" ? The fence instruction will force all outstanding memory write operations to finish and reach their target. * For an uncached access it means that after the fence the DDR has been updated (or the target device register). * For a cached access it means the L2 cache has been updated, and therefore all the corresponding L1D lines invalidated. (L1I is untouched!) Bottom line is that after the fence, all other cores of the cluster running Linux will see the new value. Except of course if the write is through an uncached MMU mapping and the read is done through a cached MMU mapping, but this seems normal. Thanks for the review, it's much appreciated! -- Yann