Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp8042517rdb; Thu, 4 Jan 2024 17:13:44 -0800 (PST) X-Google-Smtp-Source: AGHT+IFCKgHrfoiNd5ehwC7Y7A3GHhX3LHwMp7ck+rdJ/DYg3OEofPmXVwjCPKbT38+iP99WKnts X-Received: by 2002:a17:906:25d4:b0:a27:9365:ef73 with SMTP id n20-20020a17090625d400b00a279365ef73mr699610ejb.38.1704417223913; Thu, 04 Jan 2024 17:13:43 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704417223; cv=none; d=google.com; s=arc-20160816; b=YVmtPQs//XSHciX4/CZG7V7kEUH7hK+JsDpY05p9hpf/RjjAIOPWGErJO8/Se80sWH XvdIGuGoQ7tKz/fACN3mWLHJ5V/9faYJAOpYvn2mem/70o3E9Tqqqy9mEYNxLaUgm3xM TW+KiV5C6TUA//wSZIsA7xNyfj4/nPIepBdY8TWaMuFjxsL+tJ8IRvK8bHgqtOnhMYda SQk1KgFiKAsw7ByYPQ+nl2KQ4VB95a+dAKtnKSxWY0f2J/CBs+XYjZI+BOhBQ7Y0ZWW/ qFdmm4ZOH1zUF9OEwYUDxvkZQ1hNawKsbDTIXbpdh5eZ/THqAJ7xTBOuYYEgCfZqq1pG 6O6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:dkim-signature; bh=C6AH0qOGa7u84h81QCJsBs7GCOhPyt51Bga5+IRGVvg=; fh=J5CYSLzAv55qI1WdAKTU31RF+nLU5kJwyDhOKXfokNs=; b=XMaEXBXBCuWJh0ABYVWoJOfj6y+uxi6ehouMjyUPWoaZwKjPGMKKB1ZWXThhaVXPrz 8YFLhCGRSHYfnvo2i32EVq5Yu3oTF6qxmbxiLQASsmvsm+yWRDzvN/FWLNalocRcxFG2 fdGrcNy+1T5C5ogiCyZkldYdo6txrVZGT+CHXer+fMQU9n5pNUOy96JFo63LbDFAvk3p AX0KhEK7HVSyJiu6sCtY8RbOPucq5C36e6PAcN1Cdup1GLkYsrzma0ipxHO4zcNLcsef 8dhdLnZKfxHeHBTasn1qGtgfkpYuhnb1Y6evQf1kY+Sdj0lMm3+XjK/FsqMx7I+e/nv7 whsw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iBF7yRuw; spf=pass (google.com: domain of linux-kernel+bounces-17374-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17374-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id fw13-20020a170907500d00b00a28b0625d12si175161ejc.402.2024.01.04.17.13.43 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 17:13:43 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17374-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=iBF7yRuw; spf=pass (google.com: domain of linux-kernel+bounces-17374-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17374-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=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 am.mirrors.kernel.org (Postfix) with ESMTPS id 784EB1F22DFE for ; Fri, 5 Jan 2024 01:13:43 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 2014D1FA3; Fri, 5 Jan 2024 01:13:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="iBF7yRuw" X-Original-To: linux-kernel@vger.kernel.org Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 557F41845 for ; Fri, 5 Jan 2024 01:13:34 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C99EDC43391 for ; Fri, 5 Jan 2024 01:13:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1704417213; bh=JNDfPZexh2mpstDALLBYw8BPHCxQYcpNV+7M+PjH5wA=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=iBF7yRuwNQlaWgY0RPrRcLoXwCYqLPNLRnaPLFjATzNyFdsRJuxL04JOKKIukaDa9 oXUtKnOe/tfocGxTkcdOalugdg0fsAW4ZfFSVDX3qBnfejnF2WYZmOiIAvNKVGZD0W tPrJW5hrxPWd3mYfJmjsRJuAl+DtwmzpyibPvrnHuIamcmsJfLXRCKb2n9WUAxPet0 x1yH6AuPK9hdABqasrNRYOnt5vRNQAvXv9uQJTA6DGjfV2/jw/zqSArv8nswbkgxna rMrKQgyfgcxdJOue/pIXQWqbYIoYAkMJa+T75dn9NQqmlb5xihUxeSNv+Hc0DGGNXX JqO2pVPjB0tpg== Received: by mail-lf1-f53.google.com with SMTP id 2adb3069b0e04-50ea9daac4cso1248837e87.3 for ; Thu, 04 Jan 2024 17:13:33 -0800 (PST) X-Gm-Message-State: AOJu0YzkqUG2DHiJuSPUb+U23+Iw8bO3o30lXChxe5NBcGqsmomZ/HJr oPPD6g6Mmj+OAWGs7pDRT25U9fohy8AB/JBkaF8= X-Received: by 2002:ac2:46f8:0:b0:50e:8e76:1323 with SMTP id q24-20020ac246f8000000b0050e8e761323mr615716lfo.121.1704417211777; Thu, 04 Jan 2024 17:13:31 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <20231231082955.16516-1-guoren@kernel.org> <20231231082955.16516-4-guoren@kernel.org> <20240102-81391283df04c430d76c0eb0@orel> In-Reply-To: From: Guo Ren Date: Fri, 5 Jan 2024 09:13:20 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH V2 3/3] riscv: xchg: Prefetch the destination word for sc.w To: Leonardo Bras Cc: Andrew Jones , paul.walmsley@sifive.com, palmer@dabbelt.com, panqinglin2020@iscas.ac.cn, bjorn@rivosinc.com, conor.dooley@microchip.com, peterz@infradead.org, keescook@chromium.org, wuwei2016@iscas.ac.cn, xiaoguang.xing@sophgo.com, chao.wei@sophgo.com, unicorn_wang@outlook.com, uwu@icenowy.me, jszhang@kernel.org, wefu@redhat.com, atishp@atishpatra.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, Guo Ren Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jan 4, 2024 at 10:17=E2=80=AFPM Leonardo Bras = wrote: > > On Thu, Jan 04, 2024 at 04:14:27PM +0800, Guo Ren wrote: > > On Thu, Jan 4, 2024 at 11:56=E2=80=AFAM Leonardo Bras wrote: > > > > > > On Thu, Jan 04, 2024 at 09:24:40AM +0800, Guo Ren wrote: > > > > On Thu, Jan 4, 2024 at 3:45=E2=80=AFAM Leonardo Bras wrote: > > > > > > > > > > On Wed, Jan 03, 2024 at 02:15:45PM +0800, Guo Ren wrote: > > > > > > On Tue, Jan 2, 2024 at 7:19=E2=80=AFPM Andrew Jones wrote: > > > > > > > > > > > > > > On Sun, Dec 31, 2023 at 03:29:53AM -0500, guoren@kernel.org w= rote: > > > > > > > > From: Guo Ren > > > > > > > > > > > > > > > > The cost of changing a cacheline from shared to exclusive s= tate can be > > > > > > > > significant, especially when this is triggered by an exclus= ive store, > > > > > > > > since it may result in having to retry the transaction. > > > > > > > > > > > > > > > > This patch makes use of prefetch.w to prefetch cachelines f= or write > > > > > > > > prior to lr/sc loops when using the xchg_small atomic routi= ne. > > > > > > > > > > > > > > > > This patch is inspired by commit: 0ea366f5e1b6 ("arm64: ato= mics: > > > > > > > > prefetch the destination word for write prior to stxr"). > > > > > > > > > > > > > > > > Signed-off-by: Guo Ren > > > > > > > > Signed-off-by: Guo Ren > > > > > > > > --- > > > > > > > > arch/riscv/include/asm/cmpxchg.h | 4 +++- > > > > > > > > 1 file changed, 3 insertions(+), 1 deletion(-) > > > > > > > > > > > > > > > > diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/= include/asm/cmpxchg.h > > > > > > > > index 26cea2395aae..d7b9d7951f08 100644 > > > > > > > > --- a/arch/riscv/include/asm/cmpxchg.h > > > > > > > > +++ b/arch/riscv/include/asm/cmpxchg.h > > > > > > > > @@ -10,6 +10,7 @@ > > > > > > > > > > > > > > > > #include > > > > > > > > #include > > > > > > > > +#include > > > > > > > > > > > > > > > > #define __arch_xchg_masked(prepend, append, r, p, n) = \ > > > > > > > > > > > > > > Are you sure this is based on v6.7-rc7? Because I don't see t= his macro. > > > > > > Oh, it is based on Leobras' patches. I would remove it in the n= ext of version. > > > > > > > > > > I would say this next :) > > > > Thx for the grammar correction. > > > > > > Oh, I was not intending to correct grammar. > > > I just meant the next thing I would mention is that it was based on t= op of > > > my patchset instead of v6.7-rc7: > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > ({ = \ > > > > > > > > @@ -23,6 +24,7 @@ > > > > > > > > = \ > > > > > > > > __asm__ __volatile__ ( = \ > > > > > > > > prepend = \ > > > > > > > > + PREFETCHW_ASM(%5) = \ > > > > > > > > "0: lr.w %0, %2\n" = \ > > > > > > > > " and %1, %0, %z4\n" = \ > > > > > > > > " or %1, %1, %z3\n" = \ > > > > > > > > @@ -30,7 +32,7 @@ > > > > > > > > " bnez %1, 0b\n" = \ > > > > > > > > append = \ > > > > > > > > : "=3D&r" (__retx), "=3D&r" (__rc), "+A" (*(__= ptr32b)) \ > > > > > > > > - : "rJ" (__newx), "rJ" (~__mask) = \ > > > > > > > > + : "rJ" (__newx), "rJ" (~__mask), "rJ" (__ptr32= b) \ > > > > > > > > > > > > > > I'm pretty sure we don't want to allow the J constraint for _= _ptr32b. > > > > > > > > > > > > > > > : "memory"); = \ > > > > > > > > = \ > > > > > > > > r =3D (__typeof__(*(p)))((__retx & __mask) >> __s); = \ > > > > > > > > -- > > > > > > > > 2.40.1 > > > > > > > > > > > > > > > > > > > > > > Thanks, > > > > > > > drew > > > > > > > > > > > > > > > > > > > > > > > > -- > > > > > > Best Regards > > > > > > Guo Ren > > > > > > > > > > > > > > > > Nice patch :) > > > > > Any reason it's not needed in __arch_cmpxchg_masked(), and __arch= _cmpxchg() ? > > > > CAS is a conditional AMO, unlike xchg (Stand AMO). Arm64 is wrong, = or > > > > they have a problem with the hardware. > > > > > > Sorry, I was unable to fully understand the reason here. > > > > > > You suggest that the PREFETCH.W was inserted on xchg_masked because i= t will > > > always switch the variable (no compare, blind CAS), but not on cmpxch= g. > > > > > > Is this because cmpxchg will depend on a compare, and thus it does no= t > > > garantee a write? so it would be unwise to always prefetch cacheline > > Yes, it has a comparison, so a store may not exist there. > > > > > exclusiveness for this cpu, where shared state would be enough. > > > Is that correct? > > Yes, exclusiveness would invalidate other harts' cache lines. > > I see. > > I recall a previous discussion on computer arch which stated that any LR > would require to get a cacheline in exclusive state for lr/sc to work, bu= t > I went through the RISC-V lr/sc documentation and could not find any info > about its cacheline behavior. No, lr couldn't get a cacheline in exclusive, that would break the ISA desi= gn. Think about "lr + wfe" pair. > > If this stands correct, the PREFETCH.W could be useful before every lr, > right? > (maybe that's the case for arm64 that you mentioned before) The arm64 "lr + sc" cmpxchg version is not good, don't follow that. They are moving to the LSE's cas instruction. > > Thanks! > Leo > > > > > > > > > Thanks! > > > Leo > > > > > > > > > > > > > > > > > > > > Thanks! > > > > > Leo > > > > > > > > > > > > > > > > > -- > > > > Best Regards > > > > Guo Ren > > > > > > > > > > > > > -- > > Best Regards > > Guo Ren > > > --=20 Best Regards Guo Ren