Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp1818039rwl; Thu, 6 Apr 2023 01:25:28 -0700 (PDT) X-Google-Smtp-Source: AKy350YUYDY3m0gEWMyP7HBPesbLQlCEw/UF+35lTs/PAdvylHrEq4ZEJAQDDrR+XFKbEYiEEV2U X-Received: by 2002:a17:903:24e:b0:1a3:cd4c:8d08 with SMTP id j14-20020a170903024e00b001a3cd4c8d08mr10926195plh.38.1680769528478; Thu, 06 Apr 2023 01:25:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1680769528; cv=none; d=google.com; s=arc-20160816; b=fVVy3mOScELztxIbGKcXz5TW/a2Z1aLV/F6y4UohGA4cDJYiDd7+2cIkLhcK28VVJ0 a6dze6WWVOYkQOAKhCmr3woc/tKl+HgFEYpwHt3Qgi69tmm6pjgPy8J94v9Eg2zEXyS2 ELsLvnAdnECp9O0uJBdtW92g4RaMYZxWmGCwSqwFRyN4QLAlESljcBUzhcWevmOPtoxV nVL+LmPzffTfXD0shv2mbAlDrkL/eDVJYCqiAq/dZG2UYLLxFJvS3wZZHiQNQ1FZu0e8 MTvudCD/7SIHIuYR1ibA1h1InF/qbLsVZsjM3+Y1ugJVoZAgsoJpvqOIoplzEcLO+pWp mMNg== 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=CoyMTJO6DibrNwU14h2hkb03rb0JfdK/yYjXx3MC5eY=; b=QtO376LBy/8PccwuFh9a4D98HgabFNW079t66As+osyKoKTxTnlo17Ea8o8nAlsrQu 9SRadgvI1BaP5E7vKfhx2EC3i0U9rc7wK3fmyUQ349n7tFxoyrPXyhmApbWgVb//9crV vDaACzCQChyeHupO7S9A2Nn+NfKKuUBgUn98QgW7+wbvwJiYMaB8UrKQT6YWiETDp+zU 4QEhyYktcyxcaTB8xeuqmmlKAlvarjPK8gdhq+Y/HEQfwresFNQtmyECe0THg6b1wE5y jnKcH4jJMAIGLxNgRLit+rJQGWSQyNVVMav0jv5tkDeVI13Iw4ox+RaAK+jBbGb8SAsJ DsFA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=UbjlQT0Q; 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 y16-20020a170902b49000b001a0492c419csi1028911plr.523.2023.04.06.01.25.17; Thu, 06 Apr 2023 01:25:28 -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=UbjlQT0Q; 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 S235743AbjDFIV0 (ORCPT + 99 others); Thu, 6 Apr 2023 04:21:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234958AbjDFIVV (ORCPT ); Thu, 6 Apr 2023 04:21:21 -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 365D010A for ; Thu, 6 Apr 2023 01:20:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680769238; 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=CoyMTJO6DibrNwU14h2hkb03rb0JfdK/yYjXx3MC5eY=; b=UbjlQT0QJuUZA+00Q3dMxY58dV0kL3lz4kp2FHWLazmqdPi6tL8vw4DnbLn+jURd47BYG5 1fEr0s2biDoMij59bcAwJpzXHmB77xLdDlqHOFpI0xjUlh1kcxFlsQznNiLVKks6RweZwV 7JOG196iJjFor7w7TBK+LxYCeQO7xNA= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-278-IcbAaV8DNBS4XLG-27lBKA-1; Thu, 06 Apr 2023 04:20:35 -0400 X-MC-Unique: IcbAaV8DNBS4XLG-27lBKA-1 Received: by mail-qt1-f198.google.com with SMTP id f36-20020a05622a1a2400b003deb2fa544bso26378246qtb.0 for ; Thu, 06 Apr 2023 01:20:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680769234; 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=CoyMTJO6DibrNwU14h2hkb03rb0JfdK/yYjXx3MC5eY=; b=cEQkbajbKT/EHJtQ2DZ3+Yo6xErTJmXr4TTAoenfadQb90nsSLEqnH3WNn4wtma/AC 4fqswe7tWQrxMqvSaobukRgmltnXY5kzKTXvAe/OG/R7yGgz+mdtox2wve72RC7VgnFN IZTHQFBcHMV5ydGgmQKI/02LU1kB1QNKhGq+1d7SRDBxpoBn19eO9nrGsa22CVnLY2v6 mTyiMSGd7cKLARTRqVPcJb/qJBSJaU4q0mNWEmNU2WezGSaS9BSukXCQWtzYWdtoHDSg CDdf6zAp5WRB5BFyJfTHgx6G57uwY1+haqgJ/puaxHCSpvpTQ+kCaWOQj7vI2KnHnQ7g EY9g== X-Gm-Message-State: AAQBX9eUJpGaaDs238fdZ6Yf4tGqsLklcZ7YjvtQWoUhwRfll19Oa5kT fCEdjaXcemhOuACmdVa4vouLHHuLmZBSehVsa2R4ezZlDuUdYKmycLlyUU5lEXn77MIvjmyxI70 CdvLn+tjE1SLM5BWMd776IyJJ X-Received: by 2002:a05:6214:29e6:b0:5ae:371a:ab36 with SMTP id jv6-20020a05621429e600b005ae371aab36mr2372271qvb.50.1680769233814; Thu, 06 Apr 2023 01:20:33 -0700 (PDT) X-Received: by 2002:a05:6214:29e6:b0:5ae:371a:ab36 with SMTP id jv6-20020a05621429e600b005ae371aab36mr2372254qvb.50.1680769233458; Thu, 06 Apr 2023 01:20:33 -0700 (PDT) Received: from LeoBras.redhat.com ([2804:1b3:a802:2599:3b52:5a:179c:6ae7]) by smtp.gmail.com with ESMTPSA id 186-20020a3705c3000000b007456c75edbbsm292776qkf.129.2023.04.06.01.20.31 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 01:20:32 -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 v1 1/2] riscv/cmpxchg: Deduplicate cmpxchg() asm and macros Date: Thu, 6 Apr 2023 05:20:18 -0300 Message-Id: <20230406082018.70367-2-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230406082018.70367-1-leobras@redhat.com> References: <20230406082018.70367-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=unavailable 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 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. Then unify the result under a more general define, and simplify arch_cmpxchg* generation (This did not cause any change in generated asm) Signed-off-by: Leonardo Bras --- arch/riscv/include/asm/cmpxchg.h | 184 ++++++------------------------- 1 file changed, 36 insertions(+), 148 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h index 12debce235e52..f88fae357071c 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -163,51 +163,23 @@ * store NEW in MEM. Return the initial value in MEM. Success is * indicated by comparing RETURN with OLD. */ -#define __cmpxchg_relaxed(ptr, old, new, size) \ -({ \ - __typeof__(ptr) __ptr = (ptr); \ - __typeof__(*(ptr)) __old = (old); \ - __typeof__(*(ptr)) __new = (new); \ - __typeof__(*(ptr)) __ret; \ - 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" \ - : "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr) \ - : "rJ" ((long)__old), "rJ" (__new) \ - : "memory"); \ - 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" \ - : "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr) \ - : "rJ" (__old), "rJ" (__new) \ - : "memory"); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) -#define arch_cmpxchg_relaxed(ptr, o, n) \ +#define ____cmpxchg(lr_sfx, sc_sfx, prepend, append, r, rc, p, co, o, n)\ ({ \ - __typeof__(*(ptr)) _o_ = (o); \ - __typeof__(*(ptr)) _n_ = (n); \ - (__typeof__(*(ptr))) __cmpxchg_relaxed((ptr), \ - _o_, _n_, sizeof(*(ptr))); \ + __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" \ + : "=&r" (r), "=&r" (rc), "+A" (*(p)) \ + : "rJ" (co o), "rJ" (n) \ + : "memory"); \ }) -#define __cmpxchg_acquire(ptr, old, new, size) \ +#define ___cmpxchg(ptr, old, new, size, sc_sfx, prepend, append) \ ({ \ __typeof__(ptr) __ptr = (ptr); \ __typeof__(*(ptr)) __old = (old); \ @@ -216,28 +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" \ - RISCV_ACQUIRE_BARRIER \ - "1:\n" \ - : "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr) \ - : "rJ" ((long)__old), "rJ" (__new) \ - : "memory"); \ + ____cmpxchg(".w", ".w" sc_sfx, prepend, append, \ + __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" \ - : "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr) \ - : "rJ" (__old), "rJ" (__new) \ - : "memory"); \ + ____cmpxchg(".d", ".d" sc_sfx, prepend, append, \ + __ret, __rc, __ptr, /**/, __old, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -245,105 +201,37 @@ __ret; \ }) -#define arch_cmpxchg_acquire(ptr, o, n) \ +#define __cmpxchg_relaxed(ptr, old, new, size) \ + ___cmpxchg(ptr, old, new, size, "", "", "") + +#define _arch_cmpxchg(order, ptr, o, n) \ ({ \ __typeof__(*(ptr)) _o_ = (o); \ __typeof__(*(ptr)) _n_ = (n); \ - (__typeof__(*(ptr))) __cmpxchg_acquire((ptr), \ - _o_, _n_, sizeof(*(ptr))); \ + (__typeof__(*(ptr))) __cmpxchg ## order((ptr), _o_, _n_, \ + sizeof(*(ptr))); \ }) +#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) + +#define arch_cmpxchg_acquire(ptr, o, n) \ + _arch_cmpxchg(_acquire, ptr, o, n) + #define __cmpxchg_release(ptr, old, new, size) \ -({ \ - __typeof__(ptr) __ptr = (ptr); \ - __typeof__(*(ptr)) __old = (old); \ - __typeof__(*(ptr)) __new = (new); \ - __typeof__(*(ptr)) __ret; \ - 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" \ - : "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr) \ - : "rJ" ((long)__old), "rJ" (__new) \ - : "memory"); \ - 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" \ - : "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr) \ - : "rJ" (__old), "rJ" (__new) \ - : "memory"); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___cmpxchg(ptr, old, new, size, "", RISCV_RELEASE_BARRIER, "") #define arch_cmpxchg_release(ptr, o, n) \ -({ \ - __typeof__(*(ptr)) _o_ = (o); \ - __typeof__(*(ptr)) _n_ = (n); \ - (__typeof__(*(ptr))) __cmpxchg_release((ptr), \ - _o_, _n_, sizeof(*(ptr))); \ -}) + _arch_cmpxchg(_release, ptr, o, n) #define __cmpxchg(ptr, old, new, size) \ -({ \ - __typeof__(ptr) __ptr = (ptr); \ - __typeof__(*(ptr)) __old = (old); \ - __typeof__(*(ptr)) __new = (new); \ - __typeof__(*(ptr)) __ret; \ - 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" \ - : "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr) \ - : "rJ" ((long)__old), "rJ" (__new) \ - : "memory"); \ - 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" \ - : "=&r" (__ret), "=&r" (__rc), "+A" (*__ptr) \ - : "rJ" (__old), "rJ" (__new) \ - : "memory"); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___cmpxchg(ptr, old, new, size, ".rl", "", " fence rw, rw\n") #define arch_cmpxchg(ptr, o, n) \ -({ \ - __typeof__(*(ptr)) _o_ = (o); \ - __typeof__(*(ptr)) _n_ = (n); \ - (__typeof__(*(ptr))) __cmpxchg((ptr), \ - _o_, _n_, sizeof(*(ptr))); \ -}) + _arch_cmpxchg(, ptr, o, n) #define arch_cmpxchg_local(ptr, o, n) \ (__cmpxchg_relaxed((ptr), (o), (n), sizeof(*(ptr)))) -- 2.40.0