Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp5066079rwb; Tue, 8 Aug 2023 20:12:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEkFF+ubwPq7XxfqpbE1bXJO/MAkQ0usapld4u5TEfwo773/tkl7MmbsJbDgTVchdk00FEw X-Received: by 2002:a17:907:aa6:b0:99b:6c47:1145 with SMTP id bz6-20020a1709070aa600b0099b6c471145mr959972ejc.32.1691550729132; Tue, 08 Aug 2023 20:12:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691550729; cv=none; d=google.com; s=arc-20160816; b=V1A4rulLRWrUIkWlYCoeZQAaOYYEtQNb9CbvSgwimmoP4jwXo2kDVUIpsXsBPR2xYT RmURKQB/4suNxrJ84mxYPoedIHJreZd6Q3dMKf7Oj4ISm0FbdhTnGrwBwkFZs5vDCmlX itjAW42PyFrrJST3Rsa5RnVGcOc5BG+YlN92gZ6JR+1RxQILEpjPyxARpj6EsbymFHIp 5HR5E8ozf2EUEezt4llPnhKRfG+9201PhV0u+LORLKItwIroJOVdBlw5p3+hD6roDSgb EFw190HXnMvpQ9wYuoizfDFc+KhiSSlqEe1LiLpSnUjs+9d3al95rz1OSlFHQkF3+oCt 8PEA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=cG8axZDwBuWJQ+k2FsV81xz+WKLWi2QNSuhJ6eUeaqw=; fh=RC6bsfN8bPPp/iDUCpnJMEtdiqa5XC4G6XtB4MmAVnA=; b=uSp6CUXlGhiBDjj9wJDFrkBcbtSFqC2tmjBCeOrZv2FUpPZXJkXaFGaWJBT3iUlunb okzDXuZEId9lV5/5g9XOP0WyiKZXWoJRwry7s6xabVd0njqrvxuZihZtJ5eDTggLkKCx rxDaDnlT0ypdkeajV/vGELBIxEtGd9GSmx3a17aDSIGSjvvNGrqT5kX5khhVO8pABx1G yyD1A13Ibf9WNYLDNWNYRI1wYAA4/cI/PfgEh4ahyeV5a9LC8stH3EGDqdCyeV2rPAaf 7Mxu0D7OKmyrPjJQkhdtTWEfonucDRT4pApBvdzN0bZdjjPiercLNTSaIGmuVCVd5pyr vtpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=XRqnmWPy; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id os13-20020a170906af6d00b00993150ed537si8084924ejb.189.2023.08.08.20.11.44; Tue, 08 Aug 2023 20:12:09 -0700 (PDT) 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=@redhat.com header.s=mimecast20190719 header.b=XRqnmWPy; 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=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230333AbjHICOo (ORCPT + 99 others); Tue, 8 Aug 2023 22:14:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229450AbjHICOl (ORCPT ); Tue, 8 Aug 2023 22:14:41 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 93954FE for ; Tue, 8 Aug 2023 19:14:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691547239; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cG8axZDwBuWJQ+k2FsV81xz+WKLWi2QNSuhJ6eUeaqw=; b=XRqnmWPyTZYG31ea3SCYHl3lm2SMlU82Zqp23COwH9n6Ot0i1EYPjLdvcZcbjSjjxcNFkU MWfQ25+nVqNa5hzksLlQjSEMPBkUk68AybqxFPQujSTSsOXsJxOPUvCh0IJIjaxuBM/VWF ndBbvaPhP294EiqjVupEGHNrvpZumyE= Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-278-WdnLxjy4MlijCfrkKkqyEg-1; Tue, 08 Aug 2023 22:13:58 -0400 X-MC-Unique: WdnLxjy4MlijCfrkKkqyEg-1 Received: by mail-ot1-f70.google.com with SMTP id 46e09a7af769-6b9cf208fb5so11856780a34.3 for ; Tue, 08 Aug 2023 19:13:58 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691547238; x=1692152038; 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=cG8axZDwBuWJQ+k2FsV81xz+WKLWi2QNSuhJ6eUeaqw=; b=hxbStUsIy3XFvyjQ23gqWiVmpxGsN7VuiCPA3O7voJmbbITR7gRTSt3TFkJh9PTjxZ R7oHdXQ5aklQFEdQshksQKMgUJhDp4HLTjtOutS7w0Ua1dAS9/06wOHL+C2YyJ2IE4g9 ZiHHsMvWWaxyUZ0XNH+2j8NB0C00LUBYMbHhKHlYKL9NTIawhNeay353JDktoISGjmO9 SKnEe6MgsJCYep9CzEH2oh5yFbY7ZVt3Se+NKepRyoD6Q09ZjOSdwD531gPeLb/nz+Hz aHuLZZwTt8T6atZXlw7lg3yOQIxa1faFUBnyn0VjcRuP+ued3QVItKd4TXJLx0dKjax5 pgRw== X-Gm-Message-State: AOJu0Yzd9Pw841GNPyovIeApTBNI67orY/FRClY+V5Q/lLEZSPUz2FRu zIH+kAKz4gEzEoFAd5xESAfcKYF+H5NRatTPI3GKFxAu2WKQwYb6PfbpeNCQCbMOd5HyFj8MjYh ANKvGw1MNUXoWUSLLgyKkOT6/ X-Received: by 2002:a05:6870:c084:b0:1bf:4f5e:55e2 with SMTP id c4-20020a056870c08400b001bf4f5e55e2mr1543844oad.53.1691547237749; Tue, 08 Aug 2023 19:13:57 -0700 (PDT) X-Received: by 2002:a05:6870:c084:b0:1bf:4f5e:55e2 with SMTP id c4-20020a056870c08400b001bf4f5e55e2mr1543823oad.53.1691547237498; Tue, 08 Aug 2023 19:13:57 -0700 (PDT) Received: from localhost.localdomain ([2804:431:c7ec:e667:6b7d:ed55:c363:a088]) by smtp.gmail.com with ESMTPSA id v10-20020a4a8c4a000000b0054fba751207sm6475518ooj.47.2023.08.08.19.13.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 19:13:57 -0700 (PDT) From: Leonardo Bras To: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Andrea Parri , Ingo Molnar , Geert Uytterhoeven , Andrzej Hajda , Palmer Dabbelt , Guo Ren Cc: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [RFC PATCH v4 1/5] riscv/cmpxchg: Deduplicate xchg() asm functions Date: Tue, 8 Aug 2023 23:13:06 -0300 Message-ID: <20230809021311.1390578-3-leobras@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230809021311.1390578-2-leobras@redhat.com> References: <20230809021311.1390578-2-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL, SPF_HELO_NONE,SPF_NONE 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 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: Andrea Parri --- arch/riscv/include/asm/cmpxchg.h | 136 +++++-------------------------- 1 file changed, 22 insertions(+), 114 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h index 2f4726d3cfcc..ec4ea4f3f908 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -11,60 +11,30 @@ #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"); \ - 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)) __new = (new); \ __typeof__(*(ptr)) __ret; \ - switch (size) { \ + __typeof__(ptr) __ptr = (ptr); \ + 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(); \ @@ -72,79 +42,17 @@ __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.41.0