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 6DB69C6FD1D for ; Tue, 21 Mar 2023 06:36:41 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230348AbjCUGgk (ORCPT ); Tue, 21 Mar 2023 02:36:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230342AbjCUGgd (ORCPT ); Tue, 21 Mar 2023 02:36:33 -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 6E98F3B858 for ; Mon, 20 Mar 2023 23:35:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1679380526; 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=uYmUB/WTTMvmBA0Z285enRAA2vTEDkWBaww5wTiZcXo=; b=LD6PZ/PWifhc4ZUQeAiJkN886Z5rS5B4p7Nb0y97j08Rb+4rUlSiiJBzfoc/MP0Up+R3n3 7v6Fvr+JXDfNRLgYlEhED6uSBkiDTOPVxAGbVNBU72EESWw1TkEpow0RikGBwCCgHwlzfl K8s52/99Yk4Xp6Od1XnsFA4i2ki9AE4= Received: from mail-oi1-f200.google.com (mail-oi1-f200.google.com [209.85.167.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-655-t0dZcG4BOJSUcJZQt2Mbew-1; Tue, 21 Mar 2023 02:35:22 -0400 X-MC-Unique: t0dZcG4BOJSUcJZQt2Mbew-1 Received: by mail-oi1-f200.google.com with SMTP id bh14-20020a056808180e00b00364c7610c6aso6081381oib.6 for ; Mon, 20 Mar 2023 23:35:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679380522; 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=uYmUB/WTTMvmBA0Z285enRAA2vTEDkWBaww5wTiZcXo=; b=R5ZH57zs+wsLD6I/VstVVNfAY5RLtw6yrwBcGSTA26d/LsUlL1NBAe9oij5TgpsGH9 nt8SBIrx3Xg9m8QhP5fKRFVGTZE2kKfgTGt/TDh08Znwfz8upxWHWuJR7IPF16qY19bB s2ZxblZwL6uLMRye9AD2InVTyunTILdg/cOAXXZ7B2IC1tfMxhXyvrM4p8h/nTHz3u2y gGoEJwNyB079+fLvSxpznlxGWYj+KfGZEXrYcRzs1lBcZKWddRdKeGr8JrNtiq4NODoA ARCev3MgciWNzicduMJtD/JRLV+arinByQYybObfEPvT1ngsPleZg+AP9XssTRSuWPe/ u/Ew== X-Gm-Message-State: AO0yUKUX/wTcvMVYcaxCEyCRKrbpqPm3RGlvefrZriGc2ypj+tvxP0qX st0vlItTB9yKI1QIqcgqWrjFb0MtRlFPbWuC4FcLu1i+XXcGRmukXP95zYoL8Gv2zg7t3nZsLX9 7VgO6hTGS4/CE4az8YTeJbgFBad7XoAyUBFo= X-Received: by 2002:a05:6830:138c:b0:69e:1412:75b8 with SMTP id d12-20020a056830138c00b0069e141275b8mr781583otq.15.1679380521774; Mon, 20 Mar 2023 23:35:21 -0700 (PDT) X-Google-Smtp-Source: AK7set/O7aq75O5qq7VbvS/dReIriYuF/Eh9gUgT7dPlFJIkJw5WajWjKU4lERNuA7fyWJf4dIfjFw== X-Received: by 2002:a05:6830:138c:b0:69e:1412:75b8 with SMTP id d12-20020a056830138c00b0069e141275b8mr781575otq.15.1679380521553; Mon, 20 Mar 2023 23:35:21 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a801:b074:274d:d04e:badc:c89f]) by smtp.gmail.com with ESMTPSA id y75-20020a4a454e000000b005293e9a12f5sm4545147ooa.45.2023.03.20.23.35.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Mar 2023 23:35:21 -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 v2 4/6] riscv/cmpxchg: Deduplicate xchg() asm functions Date: Tue, 21 Mar 2023 03:34:30 -0300 Message-Id: <20230321063430.2218795-5-leobras@redhat.com> X-Mailer: git-send-email 2.40.0 In-Reply-To: <20230321063430.2218795-1-leobras@redhat.com> References: <20230321063430.2218795-1-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In this header every xchg define (_relaxed, _acquire, _release, vanilla) contain it's own asm file, both for 4-byte variables an 8-byte variables, on a total of 8 versions of mostly the same asm. This is usually bad, as it means any change may be done in up to 8 different places. Unify those versions by creating a new define with enough parameters to generate any version of the previous 8. (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/cmpxchg.h index e49a2edc6f36c..715bf61e72c82 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -11,6 +11,17 @@ #include #include +#define ___xchg(sfx, prepend, append) \ +({ \ + __asm__ __volatile__ ( \ + prepend \ + " amoswap" sfx " %0, %2, %1\n" \ + append \ + : "=r" (__ret), "+A" (*__ptr) \ + : "r" (__new) \ + : "memory"); \ +}) + #define __xchg_relaxed(ptr, new, size) \ ({ \ __typeof__(ptr) __ptr = (ptr); \ @@ -18,18 +29,10 @@ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - " amoswap.w %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".w", "", ""); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d %0, %2, %1\n" \ - : "=r" (__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 \ - : "=r" (__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 \ - : "=r" (__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" \ - : "=r" (__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" \ - : "=r" (__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" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".w.aqrl", "", ""); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d.aqrl %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ___xchg(".d.aqrl", "", ""); \ break; \ default: \ BUILD_BUG(); \ -- 2.40.0