From nobody Tue Feb 10 20:06:06 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 DA1B9C61DA4 for ; Sat, 18 Mar 2023 08:02:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230020AbjCRIC2 (ORCPT ); Sat, 18 Mar 2023 04:02:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53188 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229488AbjCRICY (ORCPT ); Sat, 18 Mar 2023 04:02:24 -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 0BA3A20A07 for ; Sat, 18 Mar 2023 01:01:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679126497; 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=/yn+twTZLAkRdUYrcTaC/OxuB+ZzmlZ8IAyD5qH6ScI=; b=TiFdufxzQu4fp60xjHtwfqVMGmlqMmJed/dKupFvvBtfxNhcKjyla7E94Rw+4fcWyMWQhp fX3FUxYSu1FFOQJtwKVuUPFJMcY97LhYmFp56ZZ0dpg6vh22dkwH299OJzX40qj0Fyp7ut An6GMMhAR9+5R1y7iO6Nrh14rAZT09E= 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-608-bshf04h8PQOARmpipDrRTg-1; Sat, 18 Mar 2023 04:01:34 -0400 X-MC-Unique: bshf04h8PQOARmpipDrRTg-1 Received: by mail-oa1-f70.google.com with SMTP id 586e51a60fabf-177c2fb86b7so4223135fac.20 for ; Sat, 18 Mar 2023 01:01:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679126493; 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=/yn+twTZLAkRdUYrcTaC/OxuB+ZzmlZ8IAyD5qH6ScI=; b=iS/pqMNT58D5qBShD3xUBxzRSmHcEaemJf9WPUPq0Ncld2FXy9V1QMQIzh7KbIITKk LUiZfoWGJdYHUMYREK6vgPaRRtEbIVzSJX+NDzqULPNPQzR5BKylUkzGRNHxmjBEH96s 7wuHi62A5pAcWAky4lkuvMweyMHo5HANz/b6XSN5YkTsfBQAxYlxfjVGmH1K1EPxb7Q3 QAiW3LRjvMtesNRUlDqZtaUw+bsNs+3pkRAnO1xDShMuMGKJd0BguFQpotfEuvIwJwtP oNlEwdVkGtNOG1hsYlQ4aM9yWbZmUDati7oeRGNYpnGSY72gqkUOWDEhM0XzbzZ79EnB ZCTQ== X-Gm-Message-State: AO0yUKVwJNpacdwOFgP/0jt0ax2ZNYvrMRW4bndaY9Y+4fN4pI4LWNZx DkhE4fgsJUTS2PDTNYmLiluCBF2qQSQ3dDyoqPOnL3PRAEAUWGjzSNfOEU9AUh7ezIL2NusWoEb TNoFlXRgcup2jq7vfH/yGDngO X-Received: by 2002:a9d:6184:0:b0:68b:e0bc:c533 with SMTP id g4-20020a9d6184000000b0068be0bcc533mr548139otk.20.1679126493508; Sat, 18 Mar 2023 01:01:33 -0700 (PDT) X-Google-Smtp-Source: AK7set9NAItfduCFGzNneJt6a8h+WnJSJ4XrToipUUX1gzk0K9Pe+V0SU1X8xfnrS86JCcQd8uu/uA== X-Received: by 2002:a9d:6184:0:b0:68b:e0bc:c533 with SMTP id g4-20020a9d6184000000b0068be0bcc533mr548132otk.20.1679126493257; Sat, 18 Mar 2023 01:01:33 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a801:b074:274d:d04e:badc:c89f]) by smtp.gmail.com with ESMTPSA id c3-20020a9d4803000000b0069dc250cb24sm1824462otf.3.2023.03.18.01.01.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 01:01:32 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 1/6] riscv/cmpxchg: Deduplicate cmpxchg() asm functions Date: Sat, 18 Mar 2023 05:00:55 -0300 Message-Id: <20230318080059.1109286-2-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230318080059.1109286-1-leobras@redhat.com> References: <20230318080059.1109286-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 | 102 ++++++++----------------------- 1 file changed, 24 insertions(+), 78 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index 12debce235e52..21984d24cbfe7 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) \ +{ \ + __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" (__ret), "=3D&r" (__rc), "+A" (*__ptr) \ + : "rJ" ((long)__old), "rJ" (__new) \ + : "memory"); \ +} + #define __cmpxchg_relaxed(ptr, old, new, size) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ @@ -172,26 +188,10 @@ 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", "", ""); \ 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", "", ""); \ break; \ default: \ BUILD_BUG(); \ @@ -216,28 +216,10 @@ 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); \ 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); \ break; \ default: \ BUILD_BUG(); \ @@ -262,28 +244,10 @@ 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, ""); \ 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, ""); \ break; \ default: \ BUILD_BUG(); \ @@ -308,28 +272,10 @@ 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"); \ 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"); \ break; \ default: \ BUILD_BUG(); \ --=20 2.40.0 From nobody Tue Feb 10 20:06:06 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 82C55C74A5B for ; Sat, 18 Mar 2023 08:02:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229985AbjCRIC0 (ORCPT ); Sat, 18 Mar 2023 04:02:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53204 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229560AbjCRICY (ORCPT ); Sat, 18 Mar 2023 04:02:24 -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 2ACE720A06 for ; Sat, 18 Mar 2023 01:01:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679126499; 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=TPnhvOcPIzsxcdPaGdi9oJi4cZ44pFeBuguAKI/S1NE=; b=JPHMhhmqNsKx1R3ts69M2E0KV01KKCsmte7WSZrQxkBK92cau1785hEdopGO4SNGya2jVE qRjNrQwm7fYspIxRkzb6EVXxeWQBofPfhhQ0iGhH3e+St0kcaFDzcbDCThsJdGhl/ULBMd 2cscOxQkqC42BEl6GKNvs4aZqCAbnEY= Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-593-hsxZPx34O7eT0mNUyvN9eg-1; Sat, 18 Mar 2023 04:01:38 -0400 X-MC-Unique: hsxZPx34O7eT0mNUyvN9eg-1 Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-176347f3b28so4207177fac.23 for ; Sat, 18 Mar 2023 01:01:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679126497; 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=TPnhvOcPIzsxcdPaGdi9oJi4cZ44pFeBuguAKI/S1NE=; b=hh5G+5gVdLTDdHGEY/oe9wi8ejp6XjZujtbi570YKiaPY6gK0jWn+0iAOgqbzBbOuL DBGdTRVG9Q4QlDj/QteKnQ/W5BBGvecQYo/EOP+b91iL4OaHcEgV/jfoD+5egswWZdOt /RtTi15HWN+BlHiN7ogYtOhcRiqfEbNpxcPMMFcw8mjUvpXBNwQAG7GPwmsOFpxz1wLg r7mBo7PqwNladQh76Tm4AjCtzC0mmT0HaqgTC3p6K5XZbxxBvdH9YGsaFc7U3ujy/rhZ StyMCLBmR/9oR6Pa8LbaD6isaESiwnobo4wD5BIm92sahrxeJsnc69zSyCfsBNM3yFI9 fTOQ== X-Gm-Message-State: AO0yUKUND781bzn4cYVqRLI8ITTWvexMz1ARejCT8IBE2xKzh+zxkFcm hNMqO1vP8zn+dPcW1Ksfb9JXfKjfKEHqlR8mXz2WHzuJ/vvjSpfvIilL56k5ZMFICojWCRgqfLf EKsP8YSSiRg8ao+qJpezEOXhh5tl28Y7/P0g= X-Received: by 2002:a9d:6d07:0:b0:699:a4f5:c951 with SMTP id o7-20020a9d6d07000000b00699a4f5c951mr545060otp.2.1679126497073; Sat, 18 Mar 2023 01:01:37 -0700 (PDT) X-Google-Smtp-Source: AK7set+WcHJHToqcNvmcnD6lpwssEC4fCQRqdp8Ky5jVCA70hyM292yOTI3zEF7jhKXF67Z9dF5gdA== X-Received: by 2002:a9d:6d07:0:b0:699:a4f5:c951 with SMTP id o7-20020a9d6d07000000b00699a4f5c951mr545053otp.2.1679126496831; Sat, 18 Mar 2023 01:01:36 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a801:b074:274d:d04e:badc:c89f]) by smtp.gmail.com with ESMTPSA id c3-20020a9d4803000000b0069dc250cb24sm1824462otf.3.2023.03.18.01.01.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 01:01:36 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 2/6] riscv/cmpxchg: Deduplicate cmpxchg() macros Date: Sat, 18 Mar 2023 05:00:56 -0300 Message-Id: <20230318080059.1109286-3-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230318080059.1109286-1-leobras@redhat.com> References: <20230318080059.1109286-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 | 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 21984d24cbfe7..c7a13eec4dbcc 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) \ -{ \ +#define ____cmpxchg(lr_sfx, sc_sfx, prepend, append) \ +({ \ __asm__ __volatile__ ( \ prepend \ "0: lr" lr_sfx " %0, %2\n" \ @@ -177,9 +177,9 @@ : "=3D&r" (__ret), "=3D&r" (__rc), "+A" (*__ptr) \ : "rJ" ((long)__old), "rJ" (__new) \ : "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,10 +188,10 @@ register unsigned int __rc; \ switch (size) { \ case 4: \ - ___cmpxchg(".w", ".w", "", ""); \ + ____cmpxchg(".w", ".w" sc_sfx, prepend, append); \ break; \ case 8: \ - ___cmpxchg(".d", ".d", "", ""); \ + ____cmpxchg(".d", ".d" sc_sfx, prepend, append); \ break; \ default: \ BUILD_BUG(); \ @@ -199,6 +199,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); \ @@ -208,24 +211,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); \ - break; \ - case 8: \ - ___cmpxchg(".d", ".d", "", RISCV_ACQUIRE_BARRIER); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___cmpxchg(ptr, old, new, size, "", "", RISCV_ACQUIRE_BARRIER) =20 #define arch_cmpxchg_acquire(ptr, o, n) \ ({ \ @@ -236,24 +222,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, ""); \ - break; \ - case 8: \ - ___cmpxchg(".d", ".d", RISCV_RELEASE_BARRIER, ""); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___cmpxchg(ptr, old, new, size, "", RISCV_RELEASE_BARRIER, "") =20 #define arch_cmpxchg_release(ptr, o, n) \ ({ \ @@ -264,24 +233,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"); \ - break; \ - case 8: \ - ___cmpxchg(".d", ".d.rl", "", " fence rw, rw\n"); \ - 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 Tue Feb 10 20:06:06 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 27027C61DA4 for ; Sat, 18 Mar 2023 08:02:40 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230044AbjCRICi (ORCPT ); Sat, 18 Mar 2023 04:02:38 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53244 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230041AbjCRICb (ORCPT ); Sat, 18 Mar 2023 04:02:31 -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 1F3AC20A15 for ; Sat, 18 Mar 2023 01:01:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679126503; 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=XMfpmYwfPn+xgsAmrLdh6YxWI9aM4T+CUsR5ROJOFiM=; b=D9oMiD84CmIimDPPfvCQ/jQ83FmpLX8WkIDQ3Dl/C7qpOvI4v/wvFJCF35NzsHfglk/p4b 1JH1ZqDWVdvi+WnAEMoolFT0ojiT0lc69jmG4u2/YkGBEIK32xFQ5YY1la7dr2Uok3tFmB 6YEg5kQIwls/3teY1X2EaFrKh7UcSM4= 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-359-bmojtJIROiy64ShizXBI6Q-1; Sat, 18 Mar 2023 04:01:41 -0400 X-MC-Unique: bmojtJIROiy64ShizXBI6Q-1 Received: by mail-oa1-f72.google.com with SMTP id 586e51a60fabf-1778b36a88dso4234168fac.17 for ; Sat, 18 Mar 2023 01:01:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679126501; 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=XMfpmYwfPn+xgsAmrLdh6YxWI9aM4T+CUsR5ROJOFiM=; b=D/i0OzuXa/JqYIpsiMraBrVsXdvDdzp5x4uOr4LDefoqSsdOAbH/di9YTgwOV1hAEg StLQYFaZYQIboIaI3U2HC8WL68Bk9djTuFSCIP94oD/dC13ZHYxws4JxwomAtUZoR2/c JUO11/NItziWPh0pQJZLv688deUW04et+KRukVcT2e93LjP8ahFQ41tVDgIx63+D7bQm lgQQOm+V7N9dFO+bGx5KoPPVkR1HFYUdA5fDCVdQV7V9/evNfCAen54Xct9Rdh7o2lmh 856lDfxqsd3HhRB6+A4rj5j5tuEq32EeuJMQYtFiiK44rHFBnZEj48XNtqR3bECIcE7J Z1Uw== X-Gm-Message-State: AO0yUKUKXsf0QtX3p5jcit/l3yf899VKIGAa8ic09qthM4x0JLKiFGC0 3rHfO9XeoBXt+4Ax4WSrLaHVSdkDdDqDwKmpQoZNz16TSp72Wlkp6Ld2OabDVFtoOadibNfRGEp NT7YvpERzIjGF6BfBLNtm4Nrf X-Received: by 2002:a9d:4813:0:b0:68b:dfc9:c41c with SMTP id c19-20020a9d4813000000b0068bdfc9c41cmr696697otf.9.1679126500834; Sat, 18 Mar 2023 01:01:40 -0700 (PDT) X-Google-Smtp-Source: AK7set+qA+Wajn70KJF8VSYWlbJbIwFT+kahKHnCy/vx5pmbuTdfzLCDMZ/nlXQnhye3u/ZMg1TpNw== X-Received: by 2002:a9d:4813:0:b0:68b:dfc9:c41c with SMTP id c19-20020a9d4813000000b0068bdfc9c41cmr696690otf.9.1679126500582; Sat, 18 Mar 2023 01:01:40 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a801:b074:274d:d04e:badc:c89f]) by smtp.gmail.com with ESMTPSA id c3-20020a9d4803000000b0069dc250cb24sm1824462otf.3.2023.03.18.01.01.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 01:01:40 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 3/6] riscv/cmpxchg: Deduplicate arch_cmpxchg() macros Date: Sat, 18 Mar 2023 05:00:57 -0300 Message-Id: <20230318080059.1109286-4-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230318080059.1109286-1-leobras@redhat.com> References: <20230318080059.1109286-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 | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index c7a13eec4dbcc..e49a2edc6f36c 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -202,46 +202,35 @@ #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 Tue Feb 10 20:06:06 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 3E117C61DA4 for ; Sat, 18 Mar 2023 08:02:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230063AbjCRICw (ORCPT ); Sat, 18 Mar 2023 04:02:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230046AbjCRICj (ORCPT ); Sat, 18 Mar 2023 04:02:39 -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 BC14B21977 for ; Sat, 18 Mar 2023 01:01:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679126513; 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=xbEE9rLsMkeSG6CkDtERD6c7FbVv+HEbsIDG5ynJNIc=; b=IRgROxCrZ7t5sjyyCbgdF1MPhdBWKMOKi67sriAH2QwkFZIxYGy9lWc6ZEErGIZLSvhSyy 6S9iAj2sZqmm26CuboNg6iKSoBuuxVP2a3EMkLrvEN4ZHHBcnlgDox+aqIuRFyHL7S5lzi kk8mcngHjE8O9ZwC1lEQlT2SkIM1NwA= 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-567-8szKnRdvMyaPoKU3ZT1Sng-1; Sat, 18 Mar 2023 04:01:45 -0400 X-MC-Unique: 8szKnRdvMyaPoKU3ZT1Sng-1 Received: by mail-ot1-f70.google.com with SMTP id a15-20020a0568300b8f00b0069965814cf7so3332471otv.15 for ; Sat, 18 Mar 2023 01:01:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679126504; 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=xbEE9rLsMkeSG6CkDtERD6c7FbVv+HEbsIDG5ynJNIc=; b=QoBRoPlAr7c9RYNKv5xwgQ07zgGnez+DkWX2ts0+pXApV05/5+ezXYNLGPjL4K0g98 lcfrYxAiL1xF/MQD+4aQuK7PFwPSpyxdOaSAdCZsb4Kfv0LPYE2hpK8PVnH70xHtqYGK /5wyDWqY/TdaFDMkqVmIx3DZhNmt5zPjz2YqwS9sHJg1GA8a9syQAPMkdJKmxCNV1JEd yhPmt6+vdVscofEdUQFZSH3gF09Wr07QP2KzeL9ge4hgXChFbPUqp/R9BPAnt6f9PkP1 NviQgEQgUYMtG7xlQeQIE8cGTapdzy4u5XihjAua9Q5iHk/Dj3Soa9lVULTrx7woal9Z E7Ew== X-Gm-Message-State: AO0yUKWeJ9TIFOx7HLjOZyMxUZG8uws/lfndqD1+oxrIAFRgUYpR9/Yw QoGjPXKuRGWlTRV++4vtj5iK6mTPGxw8EDOoxCDV3LD8iXk6soWeZraQ5AdQic9OQYyU03bghlg iwozw0E8vucGjdYtqXmRmYyb4 X-Received: by 2002:a9d:6d07:0:b0:699:a4f5:c951 with SMTP id o7-20020a9d6d07000000b00699a4f5c951mr545249otp.2.1679126504310; Sat, 18 Mar 2023 01:01:44 -0700 (PDT) X-Google-Smtp-Source: AK7set98ZKY0u5f8b+haxIAAI83p8gfQCrvcJ86i5uq8nFKjoznRe70RyMpyZxxyaIYMjXHy5gPGaQ== X-Received: by 2002:a9d:6d07:0:b0:699:a4f5:c951 with SMTP id o7-20020a9d6d07000000b00699a4f5c951mr545241otp.2.1679126504089; Sat, 18 Mar 2023 01:01:44 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a801:b074:274d:d04e:badc:c89f]) by smtp.gmail.com with ESMTPSA id c3-20020a9d4803000000b0069dc250cb24sm1824462otf.3.2023.03.18.01.01.41 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 01:01:43 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 4/6] riscv/cmpxchg: Deduplicate xchg() asm functions Date: Sat, 18 Mar 2023 05:00:58 -0300 Message-Id: <20230318080059.1109286-5-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230318080059.1109286-1-leobras@redhat.com> References: <20230318080059.1109286-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 | 63 ++++++++++---------------------- 1 file changed, 19 insertions(+), 44 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index e49a2edc6f36c..13dc608229ef0 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) \ +({ \ + __asm__ __volatile__ ( \ + prepend \ + " amoswap" sfx " %0, %2, %1\n" \ + append \ + : "=3Dr" (__ret), "+A" (*__ptr) \ + : "r" (__new) \ + : "memory"); \ +}) + #define __xchg_relaxed(ptr, new, size) \ ({ \ __typeof__(ptr) __ptr =3D (ptr); \ @@ -18,18 +29,10 @@ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - " amoswap.w %0, %2, %1\n" \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".w", "", ""); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d %0, %2, %1\n" \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".d", "", ""); \ break; \ default: \ BUILD_BUG(); \ @@ -51,20 +54,10 @@ __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); \ 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); \ break; \ default: \ BUILD_BUG(); \ @@ -86,20 +79,10 @@ __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, ""); \ 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, ""); \ break; \ default: \ BUILD_BUG(); \ @@ -121,18 +104,10 @@ __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", "", ""); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d.aqrl %0, %2, %1\n" \ - : "=3Dr" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg("d.aqrl", "", ""); \ break; \ default: \ BUILD_BUG(); \ --=20 2.40.0 From nobody Tue Feb 10 20:06:06 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 4192CC74A5B for ; Sat, 18 Mar 2023 08:02:47 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230060AbjCRICp (ORCPT ); Sat, 18 Mar 2023 04:02:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230045AbjCRICj (ORCPT ); Sat, 18 Mar 2023 04:02:39 -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 A70C3211FA for ; Sat, 18 Mar 2023 01:01:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679126510; 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=7gfMcI5aMDF6IpHIz8HZtjeTtHnq7AJwdDOIUhbWBLk=; b=KH4rzIl1enHRsC6JE1NLaP5pl2g3qjMdq241lLu9VI94fJ0/3hyTJgZJV3v9V8YHSpCCVD H8a3BiRErzpf7hmQgl9MHtt2/ahO1rhxVvk7bC9bw1DWRZuSrgcjkRKichedTPTVWsnN4h IqIns2BEhVehNJrJjs6El6C9mUxW754= Received: from mail-ot1-f72.google.com (mail-ot1-f72.google.com [209.85.210.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-613-UGyMnnYDOIqCL7lnBcHH4Q-1; Sat, 18 Mar 2023 04:01:48 -0400 X-MC-Unique: UGyMnnYDOIqCL7lnBcHH4Q-1 Received: by mail-ot1-f72.google.com with SMTP id g19-20020a056830161300b0069d6fbb1a72so2990392otr.11 for ; Sat, 18 Mar 2023 01:01:48 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679126508; 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=7gfMcI5aMDF6IpHIz8HZtjeTtHnq7AJwdDOIUhbWBLk=; b=VyYHFQTVPwsvX1RBJcCCS5Ob34hSdl8ENXuesJL+2qyW4/Tw2bSaw3cozb6T6emgt4 sLE8fhI2vHhyWTgq9G6UGkJnJHsWHPYZU3YNhNmR9y9U5iQCQG8iXILF40AIaMWxYMXK vESOGQ9tdiBDiHxo0/Pvdsr2BHdyTEmll9ZTLoigEMyqHzAInaxnUOaK21mGIdrEcevr mmqvBDyRejTn3btjGbV/VT6HNSnQtiu7ZcNN/3JfKwWM7MiwMq234aafqOk2A+dPH6Q+ leqkSiWV9CmyNzfGdDVJFWtFXvJ640gMGAmJltD46QAo6GiaxeXtnMXhMbbxjoD+6Z9c MqMA== X-Gm-Message-State: AO0yUKWqriVokdox5jEH1jStdLdQnxV6YM1eihuStRlDZJuZQ954gzmF kXHSCgqXqdVRPz3/afsw9GgwWvWhTnVgQeKUtiMvZeMx0yPIrBcBzGFltbbSm0Ari/+9qiwRIlM FgeQPVBFVzjBA4s9xUMLnDRsh X-Received: by 2002:a4a:976c:0:b0:525:42c7:9a5f with SMTP id v41-20020a4a976c000000b0052542c79a5fmr622251ooi.6.1679126507897; Sat, 18 Mar 2023 01:01:47 -0700 (PDT) X-Google-Smtp-Source: AK7set/b+sFSDMT3cPiuGUXdE8iECw0N3iv23H541xyVl8EvWgowAjDCIuSTW7z8aNpA5JDXO8rfgw== X-Received: by 2002:a4a:976c:0:b0:525:42c7:9a5f with SMTP id v41-20020a4a976c000000b0052542c79a5fmr622245ooi.6.1679126507684; Sat, 18 Mar 2023 01:01:47 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a801:b074:274d:d04e:badc:c89f]) by smtp.gmail.com with ESMTPSA id c3-20020a9d4803000000b0069dc250cb24sm1824462otf.3.2023.03.18.01.01.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 01:01:47 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 5/6] riscv/cmpxchg: Deduplicate xchg() macros Date: Sat, 18 Mar 2023 05:00:59 -0300 Message-Id: <20230318080059.1109286-6-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230318080059.1109286-1-leobras@redhat.com> References: <20230318080059.1109286-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 | 62 ++++++-------------------------- 1 file changed, 10 insertions(+), 52 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpx= chg.h index 13dc608229ef0..23da4d8e6f0c8 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) \ +#define ____xchg(sfx, prepend, append) \ ({ \ __asm__ __volatile__ ( \ prepend \ @@ -22,17 +22,17 @@ : "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", "", ""); \ + ____xchg(".w" sfx, prepend, append); \ break; \ case 8: \ - ___xchg(".d", "", ""); \ + ____xchg(".d" sfx, prepend, append); \ break; \ default: \ BUILD_BUG(); \ @@ -40,6 +40,9 @@ __ret; \ }) =20 +#define __xchg_relaxed(ptr, new, size) \ + ___xchg(ptr, new, size, "", "", "") + #define arch_xchg_relaxed(ptr, x) \ ({ \ __typeof__(*(ptr)) _x_ =3D (x); \ @@ -48,22 +51,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); \ - break; \ - case 8: \ - ___xchg(".d", "", RISCV_ACQUIRE_BARRIER); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___xchg(ptr, new, size, "", "", RISCV_ACQUIRE_BARRIER) =20 #define arch_xchg_acquire(ptr, x) \ ({ \ @@ -73,22 +61,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, ""); \ - break; \ - case 8: \ - ___xchg(".d", RISCV_RELEASE_BARRIER, ""); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___xchg(ptr, new, size, "", RISCV_RELEASE_BARRIER, "") =20 #define arch_xchg_release(ptr, x) \ ({ \ @@ -98,22 +71,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", "", ""); \ - break; \ - case 8: \ - ___xchg("d.aqrl", "", ""); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___xchg(ptr, new, size, ".aqrl", "", "") =20 #define arch_xchg(ptr, x) \ ({ \ --=20 2.40.0 From nobody Tue Feb 10 20:06:06 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 076A9C61DA4 for ; Sat, 18 Mar 2023 08:02:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230087AbjCRICs (ORCPT ); Sat, 18 Mar 2023 04:02:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53326 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230041AbjCRICj (ORCPT ); Sat, 18 Mar 2023 04:02:39 -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 837E521955 for ; Sat, 18 Mar 2023 01:01:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679126513; 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=lpwvsmW/EIvjt3XpmgOB2tOjIn+1scWXRJYbQwzYCy4=; b=BPBjds4YinuZDo8EPPMSfJU1wZZDqbaNuxkPaE+U8vELYHOFCibdDiU4cb5EzNPnBker7e +rW3LgR8A7kSfu/8p/ZFrC/un98RLjDz2ffF2kM4zWoJapuwWoc35c0vr5T1bhi9bG9m8E qXmt8kWEue84aEhlVVS0tJu3ETjJT7w= Received: from mail-ot1-f69.google.com (mail-ot1-f69.google.com [209.85.210.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-635-G9jQ3qYsMrCKPnPQz9lzRQ-1; Sat, 18 Mar 2023 04:01:52 -0400 X-MC-Unique: G9jQ3qYsMrCKPnPQz9lzRQ-1 Received: by mail-ot1-f69.google.com with SMTP id e2-20020a9d5602000000b00694299f6ea9so3276239oti.19 for ; Sat, 18 Mar 2023 01:01:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679126511; 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=lpwvsmW/EIvjt3XpmgOB2tOjIn+1scWXRJYbQwzYCy4=; b=3RwAMVV9QoRo1XNjIYfiLQsdBTtu9sz3TU7AgqWinvalnJnQh7oqqY7MN5Zmr40Ak5 LXQ770dtt9BAQvzMIbISjbVNTCh0bC+97keFdRStLwWVwry1mGRUlnDczfBLzmkTVpzB QhWognS1IUYqF4Kx/6mWUzkTIndPh4S8bcJxH/bAxbvPHpSY44gZMsApF12W1u+pTe5q S7wq5cx6K1YbCjOax1qbeyt5JFr67yoVIk/kBjFx7XdmtOrl1MxdcA6aZV6cWgpLB0Cl 27sgwuXQcfcfy3930sqiMnn4QSFEIPt+JxpkQVthfW7erNFshBuc0EfVX+gotmHR38zC dmbA== X-Gm-Message-State: AO0yUKXko8SwG1FWhFT1S6sZSKzZzcJqsLM+ZqvaixGGCv2Rk8CQlPwd FlFeM0WdUak/kz2mzlcnnNT7vzsyLtvo8oBin2z6JISttbINadLsgsWHNiHAdqAHINex0vQOtXe TYenp7mpNrQN8MNxH1hFWoEjR X-Received: by 2002:a54:4105:0:b0:386:a6cb:82ed with SMTP id l5-20020a544105000000b00386a6cb82edmr5217854oic.33.1679126511679; Sat, 18 Mar 2023 01:01:51 -0700 (PDT) X-Google-Smtp-Source: AK7set/JkYinclH5dcLs6EHBhSKruKldE5cS9Bd8lNlJcC0BBckJockV5iTHiGLJSuRSCGUw//8tvQ== X-Received: by 2002:a54:4105:0:b0:386:a6cb:82ed with SMTP id l5-20020a544105000000b00386a6cb82edmr5217850oic.33.1679126511452; Sat, 18 Mar 2023 01:01:51 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a801:b074:274d:d04e:badc:c89f]) by smtp.gmail.com with ESMTPSA id c3-20020a9d4803000000b0069dc250cb24sm1824462otf.3.2023.03.18.01.01.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 18 Mar 2023 01:01:51 -0700 (PDT) From: Leonardo Bras To: Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Guo Ren Cc: linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org Subject: [RFC PATCH 6/6] riscv/cmpxchg: Deduplicate arch_xchg() macros Date: Sat, 18 Mar 2023 05:01:00 -0300 Message-Id: <20230318080059.1109286-7-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230318080059.1109286-1-leobras@redhat.com> References: <20230318080059.1109286-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 23da4d8e6f0c8..d13da2286c82a 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -43,41 +43,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