Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp7921054rdb; Thu, 4 Jan 2024 11:55:06 -0800 (PST) X-Google-Smtp-Source: AGHT+IHXYpL/RUN7w5kqDEq1jVTX4AASJCk5NKkupiha5VcFf3uJk7ZS5tkyBrJOd7JOOBwS9Ivf X-Received: by 2002:a05:6a20:87aa:b0:194:ae7b:3847 with SMTP id g42-20020a056a2087aa00b00194ae7b3847mr733269pzf.34.1704398106333; Thu, 04 Jan 2024 11:55:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1704398106; cv=none; d=google.com; s=arc-20160816; b=UQtOB5Y/xUJh50y6/OTYRzrcsSd88JkyKN0Vdhl809rIjPkAT+ce+pnsi7fZaTzNaF SnICGeIs8cA+Uizv3ABS36bfd1FFSmkkvaq4CetVTabn3JmKT7gZjrP8oezHzi7hvRoJ Jg1EFZLTdBJ8QQ5FUAQ2kSQ5HG/CEFv6d6SLzNAHLgIKV+SwzzUm/9NfB7176sr5UgYn sM7HjKn/NZ5XWbz1JvYKaLGaUlzAz5HL2HWl1oRyQFs9TkZ20ioUY088ISoU37M2vmJc rCq/Bh3PvbqzsO++g9MtnFFrNTBGY2rdKNK5e94D+7o3brZwgnUMQxicZ0SwH4IaQbOZ BKTA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-disposition:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:message-id:subject:cc :to:from:date:feedback-id:dkim-signature; bh=4dn8vRr4qHHYrzVumxZ4mBTr1b1gKVQEBFS3/J0er3M=; fh=u4L/YdCdy4HkT00Q6KGGhzIo0+ErPhEqvfGjmNlPCM8=; b=MmmlASQ3vfEm87d8nmDpxyOuFB4TPPdnzremNyDI7sj+mt/tL/zkX7n21cVZ5jNram 9durj0sDIxID5pK86AhvbpwvdN8GNoo8J1wxeKhNRl92awDJ0wWkf8ePZhbzorG3sk0W MwwGHYWVFJi0fxCrk0FG+Bbt5MlnhJtme4shycOFBBMMXaGIpNGaZ4S47Y722Hha3VGi BvMiPP6BVYMeqN0rmS3wgQDYq6BZys6eIqUBpkvf3k3odYRqf0WnNOXS3ke7bamodcB8 hwzxep3B1gyNF2rRvF/RH/4J1Ti+KGSU+3hzs5goOhgFmsgkZXg4MJ7Ok9Qz5uCCDvqj BvFw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=cgxrzYFh; spf=pass (google.com: domain of linux-kernel+bounces-17185-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17185-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id r20-20020a638f54000000b005ce2be01947si77120pgn.446.2024.01.04.11.55.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 11:55:06 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-17185-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=cgxrzYFh; spf=pass (google.com: domain of linux-kernel+bounces-17185-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-17185-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com 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 sv.mirrors.kernel.org (Postfix) with ESMTPS id D7613285377 for ; Thu, 4 Jan 2024 19:55:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 85F302C694; Thu, 4 Jan 2024 19:54:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="cgxrzYFh" X-Original-To: linux-kernel@vger.kernel.org Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) (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 434D52C686 for ; Thu, 4 Jan 2024 19:54:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-qt1-f170.google.com with SMTP id d75a77b69052e-42836658198so4098231cf.0 for ; Thu, 04 Jan 2024 11:54:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1704398095; x=1705002895; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:from:to:cc:subject:date :message-id:reply-to; bh=4dn8vRr4qHHYrzVumxZ4mBTr1b1gKVQEBFS3/J0er3M=; b=cgxrzYFh8nf+SZC6N3AaQa7aCExplSg3Puguu8OYB+qEe/w/+SGUBVChBcG1Y+4Zmy OrpNtKBDViFMfRZkaXPDWKq8VTan9mXXypKKp+ofdDDc6/rEQYQ+NKEqlFxal9aRtet+ 1LH7zNLBuwmHIrTfl//YgjkLgD2yvp29nnEU59q+YfJbAznSfocg5vpFA5Zygfd93Cqf MznkkxsIE7zuBlCiBODgBTpHjlpmZ4g4v1uvMpTte1dMyjkmjgg3Wv92sY07PwZEX4UR 6IoInc45mITynJvnk8yrrodKcyDl/k2fGkPVc9CnPwlRmXhfQTNDqIULhG2xLDYru4t1 FebQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1704398095; x=1705002895; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:feedback-id:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=4dn8vRr4qHHYrzVumxZ4mBTr1b1gKVQEBFS3/J0er3M=; b=Kgc/xJogHQRii1C1gR+zizg5VXyjc9/h5ith7D/CfyXd0ntgkJ6IbM7gfg4eMsIK0A OAWC+hW2Dt5HyWZCyG4VF3WsUfPgM+0rCSAAt+Q82T/UnSzLPY1htnnQ/dJb5sZa/srd 3OTah4LEiBHbNJj4NmppLd4ePIuzzTQxEL3aPTFFA8UKydLpVQFOIUnmFWC3nCjOc1eY JW0BDCYgL6MgNZuk4eSqiDiGZhLDBlp6/K5/9xvLfdzQmApOgkJZ4RDLUqW5eUPDrcuW 6yWUjrsnruwl6LkPZ/ZvFvrp3YqxojsS2GGZdETUpTSfCgPjhv+xzKweEJisYO4zyNAn +RPw== X-Gm-Message-State: AOJu0YwxOhP4HnjlJFz4eJM+YvMcXHIV0+VFHi+VaSEQjJDvplv+HlKt UEgGp/ynTvi3Z3gcEN/MgCc= X-Received: by 2002:ac8:7c41:0:b0:427:921b:af98 with SMTP id o1-20020ac87c41000000b00427921baf98mr1028926qtv.87.1704398095086; Thu, 04 Jan 2024 11:54:55 -0800 (PST) Received: from auth2-smtp.messagingengine.com (auth2-smtp.messagingengine.com. [66.111.4.228]) by smtp.gmail.com with ESMTPSA id ch11-20020a05622a40cb00b00427f42aadedsm83600qtb.9.2024.01.04.11.54.54 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 04 Jan 2024 11:54:54 -0800 (PST) Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailauth.nyi.internal (Postfix) with ESMTP id 3963627C0061; Thu, 4 Jan 2024 14:54:54 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Thu, 04 Jan 2024 14:54:54 -0500 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrvdegjedgudefudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpeffhffvvefukfhfgggtuggjsehttdortddttddvnecuhfhrohhmpeeuohhq uhhnucfhvghnghcuoegsohhquhhnrdhfvghnghesghhmrghilhdrtghomheqnecuggftrf grthhtvghrnhepiedtfeevhfetkeelgfethfegleekfeffledvvefhheeukedtvefhtedt vdetvedvnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomh epsghoqhhunhdomhgvshhmthhprghuthhhphgvrhhsohhnrghlihhthidqieelvdeghedt ieegqddujeejkeehheehvddqsghoqhhunhdrfhgvnhhgpeepghhmrghilhdrtghomhesfh higihmvgdrnhgrmhgv X-ME-Proxy: Feedback-ID: iad51458e:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 4 Jan 2024 14:54:52 -0500 (EST) Date: Thu, 4 Jan 2024 11:53:45 -0800 From: Boqun Feng To: Leonardo Bras Cc: Will Deacon , Peter Zijlstra , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Guo Ren , Andrea Parri , Geert Uytterhoeven , Ingo Molnar , Andrzej Hajda , linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: Re: [PATCH v1 1/5] riscv/cmpxchg: Deduplicate xchg() asm functions Message-ID: References: <20240103163203.72768-2-leobras@redhat.com> <20240103163203.72768-3-leobras@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20240103163203.72768-3-leobras@redhat.com> On Wed, Jan 03, 2024 at 01:31:59PM -0300, Leonardo Bras wrote: > In this header every xchg define (_relaxed, _acquire, _release, vanilla) > contain it's own asm file, both for 4-byte variables an 8-byte variables, > on a total of 8 versions of mostly the same asm. > > This is usually bad, as it means any change may be done in up to 8 > different places. > > Unify those versions by creating a new define with enough parameters to > generate any version of the previous 8. > > Then unify the result under a more general define, and simplify > arch_xchg* generation. > > (This did not cause any change in generated asm) > > Signed-off-by: Leonardo Bras > Reviewed-by: Guo Ren > Reviewed-by: Andrea Parri > Tested-by: Guo Ren > --- > arch/riscv/include/asm/cmpxchg.h | 138 ++++++------------------------- > 1 file changed, 23 insertions(+), 115 deletions(-) > > diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h > index 2f4726d3cfcc2..48478a8eecee7 100644 > --- a/arch/riscv/include/asm/cmpxchg.h > +++ b/arch/riscv/include/asm/cmpxchg.h > @@ -11,140 +11,48 @@ > #include > #include > > -#define __xchg_relaxed(ptr, new, size) \ > +#define __arch_xchg(sfx, prepend, append, r, p, n) \ > ({ \ > - __typeof__(ptr) __ptr = (ptr); \ > - __typeof__(new) __new = (new); \ > - __typeof__(*(ptr)) __ret; \ > - switch (size) { \ > - case 4: \ > - __asm__ __volatile__ ( \ > - " amoswap.w %0, %2, %1\n" \ > - : "=r" (__ret), "+A" (*__ptr) \ > - : "r" (__new) \ > - : "memory"); \ Hmm... actually xchg_relaxed() doesn't need to be a barrier(), so the "memory" clobber here is not needed here. Of course, it's out of the scope of this series, but I'm curious to see what would happen if we remove the "memory" clobber _relaxed() ;-) Regards, Boqun > - break; \ > - case 8: \ > - __asm__ __volatile__ ( \ > - " amoswap.d %0, %2, %1\n" \ > - : "=r" (__ret), "+A" (*__ptr) \ > - : "r" (__new) \ > - : "memory"); \ > - break; \ > - default: \ > - BUILD_BUG(); \ > - } \ > - __ret; \ > -}) > - > -#define arch_xchg_relaxed(ptr, x) \ > -({ \ > - __typeof__(*(ptr)) _x_ = (x); \ > - (__typeof__(*(ptr))) __xchg_relaxed((ptr), \ > - _x_, sizeof(*(ptr))); \ > + __asm__ __volatile__ ( \ > + prepend \ > + " amoswap" sfx " %0, %2, %1\n" \ > + append \ > + : "=r" (r), "+A" (*(p)) \ > + : "r" (n) \ > + : "memory"); \ > }) > > -#define __xchg_acquire(ptr, new, size) \ > +#define _arch_xchg(ptr, new, sfx, prepend, append) \ > ({ \ > __typeof__(ptr) __ptr = (ptr); \ > - __typeof__(new) __new = (new); \ > - __typeof__(*(ptr)) __ret; \ > - switch (size) { \ > + __typeof__(*(__ptr)) __new = (new); \ > + __typeof__(*(__ptr)) __ret; \ > + switch (sizeof(*__ptr)) { \ > case 4: \ > - __asm__ __volatile__ ( \ > - " amoswap.w %0, %2, %1\n" \ > - RISCV_ACQUIRE_BARRIER \ > - : "=r" (__ret), "+A" (*__ptr) \ > - : "r" (__new) \ > - : "memory"); \ > + __arch_xchg(".w" sfx, prepend, append, \ > + __ret, __ptr, __new); \ > break; \ > case 8: \ > - __asm__ __volatile__ ( \ > - " amoswap.d %0, %2, %1\n" \ > - RISCV_ACQUIRE_BARRIER \ > - : "=r" (__ret), "+A" (*__ptr) \ > - : "r" (__new) \ > - : "memory"); \ > + __arch_xchg(".d" sfx, prepend, append, \ > + __ret, __ptr, __new); \ > break; \ > default: \ > BUILD_BUG(); \ > } \ > - __ret; \ > + (__typeof__(*(__ptr)))__ret; \ > }) > > -#define arch_xchg_acquire(ptr, x) \ > -({ \ > - __typeof__(*(ptr)) _x_ = (x); \ > - (__typeof__(*(ptr))) __xchg_acquire((ptr), \ > - _x_, sizeof(*(ptr))); \ > -}) > +#define arch_xchg_relaxed(ptr, x) \ > + _arch_xchg(ptr, x, "", "", "") > > -#define __xchg_release(ptr, new, size) \ > -({ \ > - __typeof__(ptr) __ptr = (ptr); \ > - __typeof__(new) __new = (new); \ > - __typeof__(*(ptr)) __ret; \ > - switch (size) { \ > - case 4: \ > - __asm__ __volatile__ ( \ > - RISCV_RELEASE_BARRIER \ > - " amoswap.w %0, %2, %1\n" \ > - : "=r" (__ret), "+A" (*__ptr) \ > - : "r" (__new) \ > - : "memory"); \ > - break; \ > - case 8: \ > - __asm__ __volatile__ ( \ > - RISCV_RELEASE_BARRIER \ > - " amoswap.d %0, %2, %1\n" \ > - : "=r" (__ret), "+A" (*__ptr) \ > - : "r" (__new) \ > - : "memory"); \ > - break; \ > - default: \ > - BUILD_BUG(); \ > - } \ > - __ret; \ > -}) > +#define arch_xchg_acquire(ptr, x) \ > + _arch_xchg(ptr, x, "", "", RISCV_ACQUIRE_BARRIER) > > #define arch_xchg_release(ptr, x) \ > -({ \ > - __typeof__(*(ptr)) _x_ = (x); \ > - (__typeof__(*(ptr))) __xchg_release((ptr), \ > - _x_, sizeof(*(ptr))); \ > -}) > - > -#define __arch_xchg(ptr, new, size) \ > -({ \ > - __typeof__(ptr) __ptr = (ptr); \ > - __typeof__(new) __new = (new); \ > - __typeof__(*(ptr)) __ret; \ > - switch (size) { \ > - case 4: \ > - __asm__ __volatile__ ( \ > - " amoswap.w.aqrl %0, %2, %1\n" \ > - : "=r" (__ret), "+A" (*__ptr) \ > - : "r" (__new) \ > - : "memory"); \ > - break; \ > - case 8: \ > - __asm__ __volatile__ ( \ > - " amoswap.d.aqrl %0, %2, %1\n" \ > - : "=r" (__ret), "+A" (*__ptr) \ > - : "r" (__new) \ > - : "memory"); \ > - break; \ > - default: \ > - BUILD_BUG(); \ > - } \ > - __ret; \ > -}) > + _arch_xchg(ptr, x, "", RISCV_RELEASE_BARRIER, "") > > #define arch_xchg(ptr, x) \ > -({ \ > - __typeof__(*(ptr)) _x_ = (x); \ > - (__typeof__(*(ptr))) __arch_xchg((ptr), _x_, sizeof(*(ptr))); \ > -}) > + _arch_xchg(ptr, x, ".aqrl", "", "") > > #define xchg32(ptr, x) \ > ({ \ > -- > 2.43.0 >