From nobody Sun Feb 8 14:11:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 35BE7C761A6 for ; Tue, 4 Apr 2023 16:38:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234897AbjDDQip (ORCPT ); Tue, 4 Apr 2023 12:38:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43672 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233366AbjDDQin (ORCPT ); Tue, 4 Apr 2023 12:38:43 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 67B0B3C2F for ; Tue, 4 Apr 2023 09:37:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680626274; 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=gkLzLdGanqQWkIW56OlfNGGVARUbVzCrAbRW+lCFaME=; b=a+KpInYWHrmA9vpEDiZkcIcckXyzGrRceHym6Ilsg+5WhyViZM/fj9SDaTd1nZaJUtcPmw phnWzV66qgGR4OjvEzwZgcZil5bxprVwW2GG/U7M9OCHFU8e5idsG9ZOZkdoQ4oXS/3rRG 9Z7U+vvWM8PxLEKHkVe58WgIedgIvo4= Received: from mail-oa1-f70.google.com (mail-oa1-f70.google.com [209.85.160.70]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-413-Il7ey56ZMd6v5tvtA_KOHg-1; Tue, 04 Apr 2023 12:37:53 -0400 X-MC-Unique: Il7ey56ZMd6v5tvtA_KOHg-1 Received: by mail-oa1-f70.google.com with SMTP id 586e51a60fabf-17afa2c993cso17471580fac.2 for ; Tue, 04 Apr 2023 09:37:53 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680626273; 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=gkLzLdGanqQWkIW56OlfNGGVARUbVzCrAbRW+lCFaME=; b=wFU50QA2Dk/FCRSFmPnqwSYckHH5o9ocoksVnIb3sQTSTqOgmKXd6a+jBgJKq0SQyW oabmF8TR3ZT3MuNj0ibu/EaIRzC/3Dz5pjsIqUI72a0LAUMd9rI3JdVs69d2FTdJeynq DV9gB99Ei4AszFCm2ITtwJC0v2m5mweowvyKjxy6rZm5oxwZ6otFEOwfFHCriej8z8Oc XfU690kEWM7r6jOnTBdf9Gdv1adVxkCvmEQabmiM2lWRBfLfYD8Q5wWws45Kdbww7I5g eBydPgi54QQ7QL4P18Tkz3w1Vh/y5NpZrrPSyn4X+xXkvmLcXzTJkb+ZK7XStLqRFLIG /Dqg== X-Gm-Message-State: AAQBX9dHE2aBTYTehGJ74XhaKEHQTVbqSS3Jb6GXwW72IAuIL8R87bX7 HL7aM0KpzRoBDAIhnlJxKHpFJYrU0fz8v1OLgyyidCHoQw5ozKQ3ZBhU/b6eGb3j5N93ehFu0OB Tu3oeYh5nkhk4wxq/qpGgOKMe X-Received: by 2002:aca:a90a:0:b0:367:6c7:ba98 with SMTP id s10-20020acaa90a000000b0036706c7ba98mr1652991oie.23.1680626272166; Tue, 04 Apr 2023 09:37:52 -0700 (PDT) X-Google-Smtp-Source: AKy350ZmlgAkxSWHcDx8gnC09wC/9Kl6psMH2hmRnFwk+D+PeGCc7kRnI/0vT1pWa4hNNU8zKSAvjg== X-Received: by 2002:aca:a90a:0:b0:367:6c7:ba98 with SMTP id s10-20020acaa90a000000b0036706c7ba98mr1652948oie.23.1680626270456; Tue, 04 Apr 2023 09:37:50 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a802:2599:c6a8:e6db:a08:61b6]) by smtp.gmail.com with ESMTPSA id bd1-20020a056808220100b0038755008179sm5264623oib.26.2023.04.04.09.37.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 09:37:50 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren , Andrea Parri , Conor Dooley Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v3 1/6] riscv/cmpxchg: Deduplicate cmpxchg() asm functions Date: Tue, 4 Apr 2023 13:37:36 -0300 Message-Id: <20230404163741.2762165-2-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230404163741.2762165-1-leobras@redhat.com> References: <20230404163741.2762165-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" In this header every cmpxchg 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. (This did not cause any change in generated asm) Signed-off-by: Leonardo Bras --- arch/riscv/include/asm/cmpxchg.h | 110 +++++++++---------------------- 1 file changed, 32 insertions(+), 78 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index 12debce235e52..22f2ad13cad71 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -163,6 +163,22 @@ * store NEW in MEM. Return the initial value in MEM. Success is * indicated by comparing RETURN with OLD. */ + +#define ___cmpxchg(lr_sfx, sc_sfx, prepend, append, r, rc, p, co, o, n) \ +{ \ + __asm__ __volatile__ ( \ + prepend \ + "0: lr" lr_sfx " %0, %2\n" \ + " bne %0, %z3, 1f\n" \ + " sc" sc_sfx " %1, %z4, %2\n" \ + " bnez %1, 0b\n" \ + append \ + "1:\n" \ + : "=3D&r" (r), "=3D&r" (rc), "+A" (*(p)) \ + : "rJ" (co o), "rJ" (n) \ + : "memory"); \ +} + #define __cmpxchg_relaxed(ptr, old, new, size) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ @@ -172,26 +188,12 @@ register unsigned int __rc; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - "0: lr.w %0, %2\n" \ - " bne %0, %z3, 1f\n" \ - " sc.w %1, %z4, %2\n" \ - " bnez %1, 0b\n" \ - "1:\n" \ - : "=3D&r" (__ret), "=3D&r" (__rc), "+A" (*__ptr) \ - : "rJ" ((long)__old), "rJ" (__new) \ - : "memory"); \ + ___cmpxchg(".w", ".w", "", "", \ + __ret, __rc, __ptr, (long), __old, __new); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - "0: lr.d %0, %2\n" \ - " bne %0, %z3, 1f\n" \ - " sc.d %1, %z4, %2\n" \ - " bnez %1, 0b\n" \ - "1:\n" \ - : "=3D&r" (__ret), "=3D&r" (__rc), "+A" (*__ptr) \ - : "rJ" (__old), "rJ" (__new) \ - : "memory"); \ + ___cmpxchg(".d", ".d", "", "", \ + __ret, __rc, __ptr, /**/, __old, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -216,28 +218,12 @@ register unsigned int __rc; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - "0: lr.w %0, %2\n" \ - " bne %0, %z3, 1f\n" \ - " sc.w %1, %z4, %2\n" \ - " bnez %1, 0b\n" \ - RISCV_ACQUIRE_BARRIER \ - "1:\n" \ - : "=3D&r" (__ret), "=3D&r" (__rc), "+A" (*__ptr) \ - : "rJ" ((long)__old), "rJ" (__new) \ - : "memory"); \ + ___cmpxchg(".w", ".w", "", RISCV_ACQUIRE_BARRIER, \ + __ret, __rc, __ptr, (long), __old, __new); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - "0: lr.d %0, %2\n" \ - " bne %0, %z3, 1f\n" \ - " sc.d %1, %z4, %2\n" \ - " bnez %1, 0b\n" \ - RISCV_ACQUIRE_BARRIER \ - "1:\n" \ - : "=3D&r" (__ret), "=3D&r" (__rc), "+A" (*__ptr) \ - : "rJ" (__old), "rJ" (__new) \ - : "memory"); \ + ___cmpxchg(".d", ".d", "", RISCV_ACQUIRE_BARRIER, \ + __ret, __rc, __ptr, /**/, __old, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -262,28 +248,12 @@ register unsigned int __rc; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - RISCV_RELEASE_BARRIER \ - "0: lr.w %0, %2\n" \ - " bne %0, %z3, 1f\n" \ - " sc.w %1, %z4, %2\n" \ - " bnez %1, 0b\n" \ - "1:\n" \ - : "=3D&r" (__ret), "=3D&r" (__rc), "+A" (*__ptr) \ - : "rJ" ((long)__old), "rJ" (__new) \ - : "memory"); \ + ___cmpxchg(".w", ".w", RISCV_RELEASE_BARRIER, "", \ + __ret, __rc, __ptr, (long), __old, __new); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - RISCV_RELEASE_BARRIER \ - "0: lr.d %0, %2\n" \ - " bne %0, %z3, 1f\n" \ - " sc.d %1, %z4, %2\n" \ - " bnez %1, 0b\n" \ - "1:\n" \ - : "=3D&r" (__ret), "=3D&r" (__rc), "+A" (*__ptr) \ - : "rJ" (__old), "rJ" (__new) \ - : "memory"); \ + ___cmpxchg(".d", ".d", RISCV_RELEASE_BARRIER, "", \ + __ret, __rc, __ptr, /**/, __old, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -308,28 +278,12 @@ register unsigned int __rc; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - "0: lr.w %0, %2\n" \ - " bne %0, %z3, 1f\n" \ - " sc.w.rl %1, %z4, %2\n" \ - " bnez %1, 0b\n" \ - " fence rw, rw\n" \ - "1:\n" \ - : "=3D&r" (__ret), "=3D&r" (__rc), "+A" (*__ptr) \ - : "rJ" ((long)__old), "rJ" (__new) \ - : "memory"); \ + ___cmpxchg(".w", ".w.rl", "", " fence rw, rw\n", \ + __ret, __rc, __ptr, (long), __old, __new); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - "0: lr.d %0, %2\n" \ - " bne %0, %z3, 1f\n" \ - " sc.d.rl %1, %z4, %2\n" \ - " bnez %1, 0b\n" \ - " fence rw, rw\n" \ - "1:\n" \ - : "=3D&r" (__ret), "=3D&r" (__rc), "+A" (*__ptr) \ - : "rJ" (__old), "rJ" (__new) \ - : "memory"); \ + ___cmpxchg(".d", ".d.rl", "", " fence rw, rw\n", \ + __ret, __rc, __ptr, /**/, __old, __new); \ break; \ default: \ BUILD_BUG(); \ --=20 2.40.0 From nobody Sun Feb 8 14:11:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9E404C6FD1D for ; Tue, 4 Apr 2023 16:38:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235203AbjDDQir (ORCPT ); Tue, 4 Apr 2023 12:38:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234948AbjDDQip (ORCPT ); Tue, 4 Apr 2023 12:38:45 -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 BF7C2197 for ; Tue, 4 Apr 2023 09:37:57 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680626276; 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=5L3HWpAkpN3snqo22epQ9Rf+TQRWSZtMYOZoBBQpjwA=; b=dSdtXsCN4pvOsj+0Bn71IvGLHVQ6onI5nDOxqHZTDOEne6dDrzmsGJU+2WS8JWOmcqjbCX 4ro+VZf5VBfpyJBLC4CB2nezLpr4HbaP67XKBWYgP3ah522SFqkz3jeLknYVdKmL1XeI+F 93NP3K4GzmFOSN8Vgpn/Kuq5EJ3FuWU= Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-208-k9PNDsoHOj2IO8LzPjca0Q-1; Tue, 04 Apr 2023 12:37:55 -0400 X-MC-Unique: k9PNDsoHOj2IO8LzPjca0Q-1 Received: by mail-oi1-f198.google.com with SMTP id f18-20020a05680814d200b003877ce3bfb4so8298909oiw.3 for ; Tue, 04 Apr 2023 09:37:55 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680626275; 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=5L3HWpAkpN3snqo22epQ9Rf+TQRWSZtMYOZoBBQpjwA=; b=mjTTq9WD/k0qtm9jNGMr1uM9bH+e75Ezthk987fHhTkJIuOCGrhoawrxrHD1f8oflm wsRMD/OSM6XVrxG4NA4j2KiFqHGv1p6/yB9jHDNuwdgcy1aKxEDMd+yzB+iNW7oGU32d wfwU9jtmLtFDmcVkoRJgKcO/0tIdaWMtTGUAjkgLgn6/M9wiNFHo6VWXI9u5c+9TCVsy G3X3Vpgcr478iYhBj71cuxAsK/QLYBbRXCvEpVBRh40AlpWrTfrview9vSZJV3W34O7v YbacC7CRnxqwi7da1q0ADo06OPeVjpmeeKycYBPkV5azTr4rslcAl1cUITa5iG7+EneV XXwg== X-Gm-Message-State: AAQBX9cV9UsYoNMKZvCnbcncAdmpM4Zv7tTjTUNZ+KE/mJVUNAoKle2D FSaBlcopP0/UMzG02R91XUXtv2Rd+pBUXkJf3z4dkdrRqVW6gZmMvE/wvFvCcjD0HrxAx6H5JLt ZIAYNqtd87z9VRa1hyxIe+VAF X-Received: by 2002:a05:6808:2a8d:b0:38b:5e4:9d3b with SMTP id fc13-20020a0568082a8d00b0038b05e49d3bmr1486713oib.4.1680626275007; Tue, 04 Apr 2023 09:37:55 -0700 (PDT) X-Google-Smtp-Source: AKy350aJ2KGdlh0EzLpckgpOVoJxkOrXA0su/AVKejuIMw/SU54LRZ1/NiSU07C24R5ULdhhMlwE/g== X-Received: by 2002:a05:6808:2a8d:b0:38b:5e4:9d3b with SMTP id fc13-20020a0568082a8d00b0038b05e49d3bmr1486669oib.4.1680626273296; Tue, 04 Apr 2023 09:37:53 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a802:2599:c6a8:e6db:a08:61b6]) by smtp.gmail.com with ESMTPSA id bd1-20020a056808220100b0038755008179sm5264623oib.26.2023.04.04.09.37.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 09:37:52 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren , Andrea Parri , Conor Dooley Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v3 2/6] riscv/cmpxchg: Deduplicate cmpxchg() macros Date: Tue, 4 Apr 2023 13:37:37 -0300 Message-Id: <20230404163741.2762165-3-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230404163741.2762165-1-leobras@redhat.com> References: <20230404163741.2762165-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Every cmpxchg define (_relaxed, _acquire, _release, vanilla) contain it's own define for creating tmp variables and selecting the correct asm code for give variable size. All those defines are mostly the same code (other than specific barriers), so there is no need to keep the 4 copies. Unify those under a more general define, that can reproduce the previous 4 versions. (This did not cause any change in generated asm) Signed-off-by: Leonardo Bras --- arch/riscv/include/asm/cmpxchg.h | 82 ++++++-------------------------- 1 file changed, 14 insertions(+), 68 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index 22f2ad13cad71..f76a82274a4fd 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -164,8 +164,8 @@ * indicated by comparing RETURN with OLD. */ =20 -#define ___cmpxchg(lr_sfx, sc_sfx, prepend, append, r, rc, p, co, o, n) \ -{ \ +#define ____cmpxchg(lr_sfx, sc_sfx, prepend, append, r, rc, p, co, o, n)\ +({ \ __asm__ __volatile__ ( \ prepend \ "0: lr" lr_sfx " %0, %2\n" \ @@ -177,9 +177,9 @@ : "=3D&r" (r), "=3D&r" (rc), "+A" (*(p)) \ : "rJ" (co o), "rJ" (n) \ : "memory"); \ -} +}) =20 -#define __cmpxchg_relaxed(ptr, old, new, size) \ +#define ___cmpxchg(ptr, old, new, size, sc_sfx, prepend, append) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ __typeof__(*(ptr)) __old =3D (old); \ @@ -188,12 +188,12 @@ register unsigned int __rc; \ switch (size) { \ case 4: \ - ___cmpxchg(".w", ".w", "", "", \ - __ret, __rc, __ptr, (long), __old, __new); \ + ____cmpxchg(".w", ".w" sc_sfx, prepend, append, \ + __ret, __rc, __ptr, (long), __old, __new); \ break; \ case 8: \ - ___cmpxchg(".d", ".d", "", "", \ - __ret, __rc, __ptr, /**/, __old, __new); \ + ____cmpxchg(".d", ".d" sc_sfx, prepend, append, \ + __ret, __rc, __ptr, /**/, __old, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -201,6 +201,9 @@ __ret; \ }) =20 +#define __cmpxchg_relaxed(ptr, old, new, size) \ + ___cmpxchg(ptr, old, new, size, "", "", "") + #define arch_cmpxchg_relaxed(ptr, o, n) \ ({ \ __typeof__(*(ptr)) _o_ =3D (o); \ @@ -210,26 +213,7 @@ }) =20 #define __cmpxchg_acquire(ptr, old, new, size) \ -({ \ - __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(*(ptr)) __old =3D (old); \ - __typeof__(*(ptr)) __new =3D (new); \ - __typeof__(*(ptr)) __ret; \ - register unsigned int __rc; \ - switch (size) { \ - case 4: \ - ___cmpxchg(".w", ".w", "", RISCV_ACQUIRE_BARRIER, \ - __ret, __rc, __ptr, (long), __old, __new); \ - break; \ - case 8: \ - ___cmpxchg(".d", ".d", "", RISCV_ACQUIRE_BARRIER, \ - __ret, __rc, __ptr, /**/, __old, __new); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___cmpxchg(ptr, old, new, size, "", "", RISCV_ACQUIRE_BARRIER) =20 #define arch_cmpxchg_acquire(ptr, o, n) \ ({ \ @@ -240,26 +224,7 @@ }) =20 #define __cmpxchg_release(ptr, old, new, size) \ -({ \ - __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(*(ptr)) __old =3D (old); \ - __typeof__(*(ptr)) __new =3D (new); \ - __typeof__(*(ptr)) __ret; \ - register unsigned int __rc; \ - switch (size) { \ - case 4: \ - ___cmpxchg(".w", ".w", RISCV_RELEASE_BARRIER, "", \ - __ret, __rc, __ptr, (long), __old, __new); \ - break; \ - case 8: \ - ___cmpxchg(".d", ".d", RISCV_RELEASE_BARRIER, "", \ - __ret, __rc, __ptr, /**/, __old, __new); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___cmpxchg(ptr, old, new, size, "", RISCV_RELEASE_BARRIER, "") =20 #define arch_cmpxchg_release(ptr, o, n) \ ({ \ @@ -270,26 +235,7 @@ }) =20 #define __cmpxchg(ptr, old, new, size) \ -({ \ - __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(*(ptr)) __old =3D (old); \ - __typeof__(*(ptr)) __new =3D (new); \ - __typeof__(*(ptr)) __ret; \ - register unsigned int __rc; \ - switch (size) { \ - case 4: \ - ___cmpxchg(".w", ".w.rl", "", " fence rw, rw\n", \ - __ret, __rc, __ptr, (long), __old, __new); \ - break; \ - case 8: \ - ___cmpxchg(".d", ".d.rl", "", " fence rw, rw\n", \ - __ret, __rc, __ptr, /**/, __old, __new); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___cmpxchg(ptr, old, new, size, ".rl", "", " fence rw, rw\n") =20 #define arch_cmpxchg(ptr, o, n) \ ({ \ --=20 2.40.0 From nobody Sun Feb 8 14:11:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 84CC9C761A6 for ; Tue, 4 Apr 2023 16:38:57 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235370AbjDDQiz (ORCPT ); Tue, 4 Apr 2023 12:38:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43778 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235166AbjDDQir (ORCPT ); Tue, 4 Apr 2023 12:38:47 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5DE0013D for ; Tue, 4 Apr 2023 09:38:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680626279; 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=x67eJiKWfkOOGp7f4Z5Mhye30XEwf/bDKb9kNuQLIhw=; b=ie9rx0bEy22TtSRPwkJgtSNRWc5tl7tWVpKgWrG+DNqJ0dvZX+ESkVwyb5tnS7Sf2fnLfC HaX5YeNBA/J+RNDWsJkS3wKjtsqojabOo/CxFsXsud3TDUlnlmK41QC1jlHxLKS0eknWnV XqB6j2wVzCTKOoEgk2wopMIFnBSER+M= Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-299-p-nem63GMNaSgoB1MmMQGA-1; Tue, 04 Apr 2023 12:37:57 -0400 X-MC-Unique: p-nem63GMNaSgoB1MmMQGA-1 Received: by mail-oi1-f198.google.com with SMTP id j6-20020aca6546000000b003890ca8e7faso7569892oiw.11 for ; Tue, 04 Apr 2023 09:37:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680626276; 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=x67eJiKWfkOOGp7f4Z5Mhye30XEwf/bDKb9kNuQLIhw=; b=e0gMnZaQebwnUPyao0QhPhSomnJ/UGU9GT9N1bAtRncCOB2J3A4kznV0wvfwWFb4Us f+QTYol+6AoyFNWjuyQiXb4wfjaURtP31yzT1oXYJzYBBefS4OJFTEqSu4B78mCv1AfW JoUcaTv6ZnFq87WjdU7PT5RK2zCgppZ4RqRrPiuy6FGMbVPhFxrWIeokHCcY4fMWXoTo Io7NwaQ+rDYIFSqKaSmUQi8MQjpcgPdXGOF23gEW1KF6zrXn6nNaTJOWsGnMfGbvcvLL VaLHX4MGz6WDtc5JJ9fnUlsccoKCsnG8Hfn3E6k+vDRdm+VAmPTCcWSt+MYNxyN0AT9l cyYw== X-Gm-Message-State: AAQBX9cyQTl0o+MRLrBwcIY8ZsyTS2mHkbQb3965XlN7pLKM9J4yvlHm G3X+2xLw1H74COP7WfdaJWCwG93uDhB9XN8z44xmOsJm6aPMrVslOyzQaFGxz9o9WOr4PCs8KiV mIapM4NaNKL4MOxzYgyemYLYw X-Received: by 2002:a54:471a:0:b0:389:72d5:f16f with SMTP id k26-20020a54471a000000b0038972d5f16fmr1575913oik.28.1680626276555; Tue, 04 Apr 2023 09:37:56 -0700 (PDT) X-Google-Smtp-Source: AKy350ZFxm9VKkuRCMgs6kh4K+aV5vzqKnbbzO6Oq2hq27GK/DiYuPVZe/Rh2OKQA1BsOa41q5DNAw== X-Received: by 2002:a54:471a:0:b0:389:72d5:f16f with SMTP id k26-20020a54471a000000b0038972d5f16fmr1575905oik.28.1680626276359; Tue, 04 Apr 2023 09:37:56 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a802:2599:c6a8:e6db:a08:61b6]) by smtp.gmail.com with ESMTPSA id bd1-20020a056808220100b0038755008179sm5264623oib.26.2023.04.04.09.37.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 09:37:55 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren , Andrea Parri , Conor Dooley Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v3 3/6] riscv/cmpxchg: Deduplicate arch_cmpxchg() macros Date: Tue, 4 Apr 2023 13:37:38 -0300 Message-Id: <20230404163741.2762165-4-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230404163741.2762165-1-leobras@redhat.com> References: <20230404163741.2762165-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Every arch_cmpxchg define (_relaxed, _acquire, _release, vanilla) contain it's own define for creating tmp variables and calling the correct internal macro for the desired version. Those defines are mostly the same code, so there is no need to keep the 4 copies. Create a helper define to avoid code duplication. (This did not cause any change in generated asm) Signed-off-by: Leonardo Bras --- arch/riscv/include/asm/cmpxchg.h | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index f76a82274a4fd..f88fae357071c 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -204,46 +204,34 @@ #define __cmpxchg_relaxed(ptr, old, new, size) \ ___cmpxchg(ptr, old, new, size, "", "", "") =20 -#define arch_cmpxchg_relaxed(ptr, o, n) \ +#define _arch_cmpxchg(order, ptr, o, n) \ ({ \ __typeof__(*(ptr)) _o_ =3D (o); \ __typeof__(*(ptr)) _n_ =3D (n); \ - (__typeof__(*(ptr))) __cmpxchg_relaxed((ptr), \ - _o_, _n_, sizeof(*(ptr))); \ + (__typeof__(*(ptr))) __cmpxchg ## order((ptr), _o_, _n_, \ + sizeof(*(ptr))); \ }) =20 +#define arch_cmpxchg_relaxed(ptr, o, n) \ + _arch_cmpxchg(_relaxed, ptr, o, n) + #define __cmpxchg_acquire(ptr, old, new, size) \ ___cmpxchg(ptr, old, new, size, "", "", RISCV_ACQUIRE_BARRIER) =20 #define arch_cmpxchg_acquire(ptr, o, n) \ -({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ - __typeof__(*(ptr)) _n_ =3D (n); \ - (__typeof__(*(ptr))) __cmpxchg_acquire((ptr), \ - _o_, _n_, sizeof(*(ptr))); \ -}) + _arch_cmpxchg(_acquire, ptr, o, n) =20 #define __cmpxchg_release(ptr, old, new, size) \ ___cmpxchg(ptr, old, new, size, "", RISCV_RELEASE_BARRIER, "") =20 #define arch_cmpxchg_release(ptr, o, n) \ -({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ - __typeof__(*(ptr)) _n_ =3D (n); \ - (__typeof__(*(ptr))) __cmpxchg_release((ptr), \ - _o_, _n_, sizeof(*(ptr))); \ -}) + _arch_cmpxchg(_release, ptr, o, n) =20 #define __cmpxchg(ptr, old, new, size) \ ___cmpxchg(ptr, old, new, size, ".rl", "", " fence rw, rw\n") =20 #define arch_cmpxchg(ptr, o, n) \ -({ \ - __typeof__(*(ptr)) _o_ =3D (o); \ - __typeof__(*(ptr)) _n_ =3D (n); \ - (__typeof__(*(ptr))) __cmpxchg((ptr), \ - _o_, _n_, sizeof(*(ptr))); \ -}) + _arch_cmpxchg(, ptr, o, n) =20 #define arch_cmpxchg_local(ptr, o, n) \ (__cmpxchg_relaxed((ptr), (o), (n), sizeof(*(ptr)))) --=20 2.40.0 From nobody Sun Feb 8 14:11:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2B767C6FD1D for ; Tue, 4 Apr 2023 16:39:00 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235311AbjDDQi6 (ORCPT ); Tue, 4 Apr 2023 12:38:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235176AbjDDQir (ORCPT ); Tue, 4 Apr 2023 12:38:47 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 53436113 for ; Tue, 4 Apr 2023 09:38:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680626282; 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=Po1dD0V9oZXC1u6q0PdogM7Oei1BCdcz7qvnbPIGYkM=; b=OdFwLT28DbbewUbIvux2oG/pwoplp++WMiKUws6hcnGjzGkLdYy2F7iG9FuegnTxGQk71U fX04jPeEjNdttaxix/khwNuA6ZtQ8Pn66dY/a3ROR9Z1CeI/HuPHIxqveuf2jaKBz7ZLic 9JufmMIyssyWqz+EwqpPG5gnsjVlHOE= Received: from mail-oa1-f71.google.com (mail-oa1-f71.google.com [209.85.160.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-47-R7FTlUbkPyq8V8_wkWfKcw-1; Tue, 04 Apr 2023 12:38:00 -0400 X-MC-Unique: R7FTlUbkPyq8V8_wkWfKcw-1 Received: by mail-oa1-f71.google.com with SMTP id 586e51a60fabf-17a678c2de9so17440054fac.14 for ; Tue, 04 Apr 2023 09:38:00 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680626279; 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=Po1dD0V9oZXC1u6q0PdogM7Oei1BCdcz7qvnbPIGYkM=; b=sLD4Ai4wxvAV/SpCBjdOZzJGkwZckWEwfptFZoPm6xaeSo8XJBMTkijgow5G6bng4M hFZURIofEK7C5ij7gzxZZjW9a8ohQYT29hWKAkBT8Oe1ebfbrYIvCoN5+KoyUE8wJgXt hQd9y/sTrLBGhJNQyGk7whZZbz+EaxE2K1yoMkUYluorNCATtOWhs6yXzqCypOLGILW9 yoRr/vANru2QwPFty0BIGhc6v7YysQMSA9pkDNzRlnDeJFAcUpdpIGQFunRQzLrRjx+3 EJ7L+DS1DwmuVhs+YAflQbm68z8LQvWfIaE5ivI6EPwRU/uYFbm8alh7fvUYc4O4MND3 RmOw== X-Gm-Message-State: AAQBX9dnYGL2kgXwzjE/US9fzO9NqryTM6tEaOkvoai64UAP1n3+0Q2P vmf8wwvxxqbZAe+6PyRlJINA2Xvjgx2fqp+c3yXo4wDoUVn2EgURx6mucxQC/bk6IS6gFsu9tGI kLQ+m186C+LOzg6zHKwG4bTPK X-Received: by 2002:a05:6808:4b:b0:384:23f0:21b7 with SMTP id v11-20020a056808004b00b0038423f021b7mr1347053oic.14.1680626279663; Tue, 04 Apr 2023 09:37:59 -0700 (PDT) X-Google-Smtp-Source: AKy350aOrxgdZwug+N0+LNMDxyZG6RB11xLyFdHeoJrlEj4dqfaDrYNSHKssIXTp1eibxVXFi6Velw== X-Received: by 2002:a05:6808:4b:b0:384:23f0:21b7 with SMTP id v11-20020a056808004b00b0038423f021b7mr1347044oic.14.1680626279345; Tue, 04 Apr 2023 09:37:59 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a802:2599:c6a8:e6db:a08:61b6]) by smtp.gmail.com with ESMTPSA id bd1-20020a056808220100b0038755008179sm5264623oib.26.2023.04.04.09.37.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 09:37:59 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren , Andrea Parri , Conor Dooley Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v3 4/6] riscv/cmpxchg: Deduplicate xchg() asm functions Date: Tue, 4 Apr 2023 13:37:39 -0300 Message-Id: <20230404163741.2762165-5-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230404163741.2762165-1-leobras@redhat.com> References: <20230404163741.2762165-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" 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. (This did not cause any change in generated asm) Signed-off-by: Leonardo Bras --- arch/riscv/include/asm/cmpxchg.h | 71 ++++++++++++-------------------- 1 file changed, 27 insertions(+), 44 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index f88fae357071c..a524484946815 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -11,6 +11,17 @@ #include #include =20 +#define ___xchg(sfx, prepend, append, r, p, n) \ +({ \ + __asm__ __volatile__ ( \ + prepend \ + " amoswap" sfx " %0, %2, %1\n" \ + append \ + : "=3Dr" (r), "+A" (*(p)) \ + : "r" (n) \ + : "memory"); \ +}) + #define __xchg_relaxed(ptr, new, size) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ @@ -18,18 +29,12 @@ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - " amoswap.w %0, %2, %1\n" \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".w", "", "", \ + __ret, __ptr, __new); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d %0, %2, %1\n" \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".d", "", "", \ + __ret, __ptr, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -51,20 +56,12 @@ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - " amoswap.w %0, %2, %1\n" \ - RISCV_ACQUIRE_BARRIER \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".w", "", RISCV_ACQUIRE_BARRIER, \ + __ret, __ptr, __new); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d %0, %2, %1\n" \ - RISCV_ACQUIRE_BARRIER \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".d", "", RISCV_ACQUIRE_BARRIER, \ + __ret, __ptr, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -86,20 +83,12 @@ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - RISCV_RELEASE_BARRIER \ - " amoswap.w %0, %2, %1\n" \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".w", RISCV_RELEASE_BARRIER, "", \ + __ret, __ptr, __new); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - RISCV_RELEASE_BARRIER \ - " amoswap.d %0, %2, %1\n" \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".d", RISCV_RELEASE_BARRIER, "", \ + __ret, __ptr, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -121,18 +110,12 @@ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - " amoswap.w.aqrl %0, %2, %1\n" \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".w.aqrl", "", "", \ + __ret, __ptr, __new); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d.aqrl %0, %2, %1\n" \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".d.aqrl", "", "", \ + __ret, __ptr, __new); \ break; \ default: \ BUILD_BUG(); \ --=20 2.40.0 From nobody Sun Feb 8 14:11:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id D8077C761A6 for ; Tue, 4 Apr 2023 16:39:13 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235398AbjDDQjM (ORCPT ); Tue, 4 Apr 2023 12:39:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43968 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235160AbjDDQiz (ORCPT ); Tue, 4 Apr 2023 12:38:55 -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 73F303ABC for ; Tue, 4 Apr 2023 09:38:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680626286; 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=b0rQxrNAcokC6W+51FfMD9h8pzjOdzJF6kUxCcV/Qp0=; b=L02M0KUyJ0bdg33efyOiSdmOueLDtuKyPxYR6gmsw9ny3KOahi+FzoWWFpvWu7gLooILKM 8rV5v8EFfwbcviJlHXT8q5HSdixXwd84KvvlbkS80Q6fh2DFoRtwNVGcunxl4U92lWePkF hHrgXwPmtl/MmQ/4W9GDI2eA7I7iw70= Received: from mail-oa1-f72.google.com (mail-oa1-f72.google.com [209.85.160.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-362-Ij9M2tDiMBaVg6HuZLTQpw-1; Tue, 04 Apr 2023 12:38:05 -0400 X-MC-Unique: Ij9M2tDiMBaVg6HuZLTQpw-1 Received: by mail-oa1-f72.google.com with SMTP id 586e51a60fabf-17fd0d597dcso10069077fac.6 for ; Tue, 04 Apr 2023 09:38:04 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680626284; 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=b0rQxrNAcokC6W+51FfMD9h8pzjOdzJF6kUxCcV/Qp0=; b=qbmvx16XMTgyFODpnKiIIKwygfaOgZuv1RtJRU/PhXXAwj0cNW26egQwnmz8CINAmJ idk+cZjZl/3F9e1EDy9xs9MtgiEz/+clx6icntlMcG9ZQ41iPrdzxtGE8OYBN/yvD0It H3bhodsb8dzqSFtCcVGXaXfsGnOjeXjXIGbI+KszKlzymObpzc7csUhKeumfiXusBpxz GkU9697EFgqnwwT4fAO6kG4OuqSPKAurRTACgouBGMFdHEvLqryziuP9S+WFU+l7RNs7 DJ0DJAvdO38WxMXzZm4vCSv6tIOyDKaLke3BB0LNlA54PWIiJv4nDAE0SB5YNuFjqJvD 4JnQ== X-Gm-Message-State: AAQBX9feidreTTGWpTsfryrCPg1BIxfpeezfW7oXnxHlJaAduFNu8e7n kRwnP5kIOXSgQwi5DSCQ/jbnyRdCWap3fiRWLlNAM3I0TdKeJ+F0FYankdgfAVgJbIKpo5NM1ds m7XCA7FxDLZF/wVMaQlQTeUoqlySqWM2qH70= X-Received: by 2002:a05:6808:6141:b0:389:4a00:51b5 with SMTP id dl1-20020a056808614100b003894a0051b5mr1470946oib.8.1680626282540; Tue, 04 Apr 2023 09:38:02 -0700 (PDT) X-Google-Smtp-Source: AKy350YnaLdfH8tuMD+Yi3A2v4l8UvYgA1HiIUzt7doj1XBln6gnqsmFL5ffbVmpHlQtBFCWz3mhXA== X-Received: by 2002:a05:6808:6141:b0:389:4a00:51b5 with SMTP id dl1-20020a056808614100b003894a0051b5mr1470938oib.8.1680626282256; Tue, 04 Apr 2023 09:38:02 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a802:2599:c6a8:e6db:a08:61b6]) by smtp.gmail.com with ESMTPSA id bd1-20020a056808220100b0038755008179sm5264623oib.26.2023.04.04.09.37.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 09:38:01 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren , Andrea Parri , Conor Dooley Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v3 5/6] riscv/cmpxchg: Deduplicate xchg() macros Date: Tue, 4 Apr 2023 13:37:40 -0300 Message-Id: <20230404163741.2762165-6-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230404163741.2762165-1-leobras@redhat.com> References: <20230404163741.2762165-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Every xchg define (_relaxed, _acquire, _release, vanilla) contain it's own define for creating tmp variables and selecting the correct asm code for given variable size. All those defines are mostly the same code (other than specific barriers), so there is no need to keep the 4 copies. Unify those under a more general define, that can reproduce the previous 4 versions. (This did not cause any change in generated asm) Signed-off-by: Leonardo Bras --- arch/riscv/include/asm/cmpxchg.h | 72 ++++++-------------------------- 1 file changed, 12 insertions(+), 60 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index a524484946815..480234719b78c 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -11,7 +11,7 @@ #include #include =20 -#define ___xchg(sfx, prepend, append, r, p, n) \ +#define ____xchg(sfx, prepend, append, r, p, n) \ ({ \ __asm__ __volatile__ ( \ prepend \ @@ -22,19 +22,19 @@ : "memory"); \ }) =20 -#define __xchg_relaxed(ptr, new, size) \ +#define ___xchg(ptr, new, size, sfx, prepend, append) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ __typeof__(new) __new =3D (new); \ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 4: \ - ___xchg(".w", "", "", \ - __ret, __ptr, __new); \ + ____xchg(".w" sfx, prepend, append, \ + __ret, __ptr, __new); \ break; \ case 8: \ - ___xchg(".d", "", "", \ - __ret, __ptr, __new); \ + ____xchg(".d" sfx, prepend, append, \ + __ret, __ptr, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -42,6 +42,9 @@ __ret; \ }) =20 +#define __xchg_relaxed(ptr, new, size) \ + ___xchg(ptr, new, size, "", "", "") + #define arch_xchg_relaxed(ptr, x) \ ({ \ __typeof__(*(ptr)) _x_ =3D (x); \ @@ -50,24 +53,7 @@ }) =20 #define __xchg_acquire(ptr, new, size) \ -({ \ - __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(new) __new =3D (new); \ - __typeof__(*(ptr)) __ret; \ - switch (size) { \ - case 4: \ - ___xchg(".w", "", RISCV_ACQUIRE_BARRIER, \ - __ret, __ptr, __new); \ - break; \ - case 8: \ - ___xchg(".d", "", RISCV_ACQUIRE_BARRIER, \ - __ret, __ptr, __new); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___xchg(ptr, new, size, "", "", RISCV_ACQUIRE_BARRIER) =20 #define arch_xchg_acquire(ptr, x) \ ({ \ @@ -77,24 +63,7 @@ }) =20 #define __xchg_release(ptr, new, size) \ -({ \ - __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(new) __new =3D (new); \ - __typeof__(*(ptr)) __ret; \ - switch (size) { \ - case 4: \ - ___xchg(".w", RISCV_RELEASE_BARRIER, "", \ - __ret, __ptr, __new); \ - break; \ - case 8: \ - ___xchg(".d", RISCV_RELEASE_BARRIER, "", \ - __ret, __ptr, __new); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___xchg(ptr, new, size, "", RISCV_RELEASE_BARRIER, "") =20 #define arch_xchg_release(ptr, x) \ ({ \ @@ -104,24 +73,7 @@ }) =20 #define __xchg(ptr, new, size) \ -({ \ - __typeof__(ptr) __ptr =3D (ptr); \ - __typeof__(new) __new =3D (new); \ - __typeof__(*(ptr)) __ret; \ - switch (size) { \ - case 4: \ - ___xchg(".w.aqrl", "", "", \ - __ret, __ptr, __new); \ - break; \ - case 8: \ - ___xchg(".d.aqrl", "", "", \ - __ret, __ptr, __new); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___xchg(ptr, new, size, ".aqrl", "", "") =20 #define arch_xchg(ptr, x) \ ({ \ --=20 2.40.0 From nobody Sun Feb 8 14:11:27 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 8D00FC761A6 for ; Tue, 4 Apr 2023 16:39:03 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235436AbjDDQjB (ORCPT ); Tue, 4 Apr 2023 12:39:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43974 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235253AbjDDQiv (ORCPT ); Tue, 4 Apr 2023 12:38:51 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4B00F3C14 for ; Tue, 4 Apr 2023 09:38:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680626287; 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=/Eu0Zi/ZTTdFnwlWVeYiPrkCPaBaCVeU5jDe5lhDGhs=; b=QB+UfjMvuegx8n5K1epr7BOdTbYMqLiyMnrdbfTEy7ixmfF2/lHabi9rpH1Ad0T1TcQZqn UtCt5ndiArw5PXrB4vM0Gx8PWUycrITpNlmoDTC+Y1LEcl3iw54A9KGMYvfrIlW7ryh9ph tNfB4iXkWB8Jj4uexHvhEyMxO7Zgn5A= Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-163-pwkNvc4bMtm6ffO0UYQuwg-1; Tue, 04 Apr 2023 12:38:06 -0400 X-MC-Unique: pwkNvc4bMtm6ffO0UYQuwg-1 Received: by mail-oi1-f199.google.com with SMTP id i128-20020acaea86000000b003893eb09687so7075368oih.20 for ; Tue, 04 Apr 2023 09:38:05 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680626285; 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=/Eu0Zi/ZTTdFnwlWVeYiPrkCPaBaCVeU5jDe5lhDGhs=; b=FL03yYC1qWsheJXUGNtbt6ZdChtl8JflgWZRzicyBnV63eOgrYGR1EI8xH5wqqjWfg 1egKzBWJV1h5aMF77SlMj6gADuCV295somY1SdBEcprwUDldBD40NhEzWoY0HsiLMYZ6 toyYYq9zbEYwYw5qTPm3vG2kF8aXJztavuaWaDt3BtCG4p53GcZlnBnGZeSORlFgtt4X c18+2DyyeQF6pSt8zP+bJWaAcn70BHiZS1Zf42LP2f6p/MbN7ueKXYNZOsSzwXZOgwuJ nxZNtg8FDItZTsOwharN4JNyI0DtbepnteW52oLwOrMT8cq9TIqXwVgzMYFYkGexLsw4 MVVA== X-Gm-Message-State: AAQBX9cSoYK5B7chakNSAAFsFFDjleKFd6kXoYT7vmgBv3PufiykZydf Y5Lc6aLiWiitoMFebW66GJ6O7Suo9UJDZxvxIf5iE53khXW31MG+x0IghdxQD2UeD49DU/Yx5r3 chsMas87mXtN17s2sj357blYF X-Received: by 2002:aca:d944:0:b0:384:3cc0:9ffd with SMTP id q65-20020acad944000000b003843cc09ffdmr1605626oig.9.1680626285347; Tue, 04 Apr 2023 09:38:05 -0700 (PDT) X-Google-Smtp-Source: AKy350ZH+tLo6+HoUfrUJlea3gq5Hmp/51sq6E4j8u/P3iEiEiy01tvmDOosUyXWMyR/S17heMTNgg== X-Received: by 2002:aca:d944:0:b0:384:3cc0:9ffd with SMTP id q65-20020acad944000000b003843cc09ffdmr1605617oig.9.1680626285148; Tue, 04 Apr 2023 09:38:05 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a802:2599:c6a8:e6db:a08:61b6]) by smtp.gmail.com with ESMTPSA id bd1-20020a056808220100b0038755008179sm5264623oib.26.2023.04.04.09.38.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Apr 2023 09:38:04 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren , Andrea Parri , Conor Dooley Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH v3 6/6] riscv/cmpxchg: Deduplicate arch_xchg() macros Date: Tue, 4 Apr 2023 13:37:41 -0300 Message-Id: <20230404163741.2762165-7-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230404163741.2762165-1-leobras@redhat.com> References: <20230404163741.2762165-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Every arch_xchg define (_relaxed, _acquire, _release, vanilla) contain it's own define for creating tmp variables and calling the correct internal macro for the desired version. Those defines are mostly the same code, so there is no need to keep the 4 copies. Create a helper define to avoid code duplication. (This did not cause any change in generated asm) Signed-off-by: Leonardo Bras --- arch/riscv/include/asm/cmpxchg.h | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index 480234719b78c..905a888d8b04d 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -45,41 +45,33 @@ #define __xchg_relaxed(ptr, new, size) \ ___xchg(ptr, new, size, "", "", "") =20 -#define arch_xchg_relaxed(ptr, x) \ +#define _arch_xchg(order, ptr, x) \ ({ \ __typeof__(*(ptr)) _x_ =3D (x); \ - (__typeof__(*(ptr))) __xchg_relaxed((ptr), \ - _x_, sizeof(*(ptr))); \ + (__typeof__(*(ptr))) __xchg ## order((ptr), \ + _x_, sizeof(*(ptr))); \ }) =20 +#define arch_xchg_relaxed(ptr, x) \ + _arch_xchg(_relaxed, ptr, x) + #define __xchg_acquire(ptr, new, size) \ ___xchg(ptr, new, size, "", "", RISCV_ACQUIRE_BARRIER) =20 #define arch_xchg_acquire(ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ =3D (x); \ - (__typeof__(*(ptr))) __xchg_acquire((ptr), \ - _x_, sizeof(*(ptr))); \ -}) + _arch_xchg(_acquire, ptr, x) =20 #define __xchg_release(ptr, new, size) \ ___xchg(ptr, new, size, "", RISCV_RELEASE_BARRIER, "") =20 #define arch_xchg_release(ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ =3D (x); \ - (__typeof__(*(ptr))) __xchg_release((ptr), \ - _x_, sizeof(*(ptr))); \ -}) + _arch_xchg(_release, ptr, x) =20 #define __xchg(ptr, new, size) \ ___xchg(ptr, new, size, ".aqrl", "", "") =20 #define arch_xchg(ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ =3D (x); \ - (__typeof__(*(ptr))) __xchg((ptr), _x_, sizeof(*(ptr))); \ -}) + _arch_xchg(, ptr, x) =20 #define xchg32(ptr, x) \ ({ \ --=20 2.40.0