From patchwork Thu Apr 6 08:20:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Leonardo Bras X-Patchwork-Id: 13202993 X-Patchwork-Delegate: palmer@dabbelt.com 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 51DAAC77B6C for ; Thu, 6 Apr 2023 08:20:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=6oYAIGpk+O3i8eeaXUQy6NI7/o1LGjRbJKNxMMDTHH8=; b=aEYRQRMaPczRpz YhLyUC+76ZE/I11y3d++sUJi4/Cnn/Xiyk/GmW0e1Vg3mhR1014PYgN9u0V+/edFHEEExfJ7YGkFs WSEwkE5BwFB54ALK/y0h7CyzrkYryJ+SISXauYxAyrix7qxW2C41uUmw2ZQLHAI8InDgm/9sS5N6R FatBWrQGQnzkpw6Qrwyw+V8MA7VWpgj/XG82fTTl+GvVXR/lhK8EnOHzMuMiEeeuefKMiWD64v3pw MJC3/wes1Z4Qk4jPmUsH4jT19GtXveaAmvSB2ygQTU3dugdzBKSB7okSD0VTSZdt9jLnZkk2bp4w4 RvQyanVpCYw8MeHcmoTQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.96 #2 (Red Hat Linux)) id 1pkKrL-006jTx-0p; Thu, 06 Apr 2023 08:20:43 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.96 #2 (Red Hat Linux)) id 1pkKrI-006jSc-1x for linux-riscv@lists.infradead.org; Thu, 06 Apr 2023 08:20:42 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1680769237; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wSaMfai72G9Tt4xz9KKUysgG6AJqyHxWXMI7tIwY4iE=; b=ixljf8DIklOUAZioAtRnVsdSpjq/+V6swtPV8zBbtCjCmsuPF26fFwgOHTXJTj0+Ks2uNd s6bZrszyw5peFEUoqYK2rq1UZi1eJebfT6zzh6xh6Yi/WOivIHvMwg78lr32voiJdFTYTQ TwCZH9fISzCN4lrqB8GDs25+jGQ6nws= Received: from mail-qt1-f197.google.com (mail-qt1-f197.google.com [209.85.160.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-364-U6QtiBMCPYGcFGESXgczVg-1; Thu, 06 Apr 2023 04:20:36 -0400 X-MC-Unique: U6QtiBMCPYGcFGESXgczVg-1 Received: by mail-qt1-f197.google.com with SMTP id h6-20020ac85846000000b003e3c23d562aso26261840qth.1 for ; Thu, 06 Apr 2023 01:20:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1680769236; 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=wSaMfai72G9Tt4xz9KKUysgG6AJqyHxWXMI7tIwY4iE=; b=LTUkdoXa//Sv1PJvm9g1qv5Yu4j7HpeAD1TJMdgqic3MIfBjQ0A2871xl/O7bb1xV0 DlpZ8Dwf07WjFnrAIH3okk/2lMbW/1AnkoGuyHQmpxOVUi/RtI1luQF87RHMrmork7Pv 3qAXRcifKXU9ai2DQExMFKvjo+TGM25wbRjtoBphixdGX5W0bctioanxFJ++ayiXjKgi tJ5HAvni0R9T8nnXYvTLqAyzOLnvqPddngx6L68WIxz16TkXQnVJPSkGGZzb6RnGaRqX HmMsAMjPxRjHxewNme8NHvWC7Q4qFH9EhjBzfoQb/6g/y5iOToyIcCAihBzaRLiDqqeK 61pw== X-Gm-Message-State: AAQBX9fVI+mcyh8v2FPzBPm5UUpQPdg5453kJJifFQXeW0zV2ahvuGP/ YyvUwnEQ4pyY5OcpNXAc6j//3FRPjptA46/zMlCgq0ZHvW0Zax/yv/IlXDFoOtq6lI8dFI9eavD IW/anGRH/lhRUkKFpuu7XXGchuT0z X-Received: by 2002:ad4:5dcc:0:b0:5cb:6880:1924 with SMTP id m12-20020ad45dcc000000b005cb68801924mr2965864qvh.13.1680769235796; Thu, 06 Apr 2023 01:20:35 -0700 (PDT) X-Google-Smtp-Source: AKy350b3FwOIlgZSFBk3ro3ZOBAKXBnZZPxON2GwjbIBNkLe/RaTkjn0brP0Q17gIbHvf8If/JBg2w== X-Received: by 2002:ad4:5dcc:0:b0:5cb:6880:1924 with SMTP id m12-20020ad45dcc000000b005cb68801924mr2965850qvh.13.1680769235574; Thu, 06 Apr 2023 01:20:35 -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.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Apr 2023 01:20:35 -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 2/2] riscv/cmpxchg: Deduplicate xchg() asm functions Date: Thu, 6 Apr 2023 05:20:19 -0300 Message-Id: <20230406082018.70367-3-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 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230406_012040_739785_7BC8F743 X-CRM114-Status: GOOD ( 10.44 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org In this header every xchg define (_relaxed, _acquire, _release, vanilla) contain it's own asm file, both for 4-byte variables an 8-byte variables, on a total of 8 versions of mostly the same asm. This is usually bad, as it means any change may be done in up to 8 different places. Unify those versions by creating a new define with enough parameters to generate any version of the previous 8. Then unify the result under a more general define, and simplify arch_xchg* generation. (This did not cause any change in generated asm) Signed-off-by: Leonardo Bras Reviewed-by: Guo Ren --- arch/riscv/include/asm/cmpxchg.h | 135 +++++++------------------------ 1 file changed, 31 insertions(+), 104 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h index f88fae357071c..905a888d8b04d 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -11,25 +11,30 @@ #include #include -#define __xchg_relaxed(ptr, new, size) \ +#define ____xchg(sfx, prepend, append, r, p, n) \ +({ \ + __asm__ __volatile__ ( \ + prepend \ + " amoswap" sfx " %0, %2, %1\n" \ + append \ + : "=r" (r), "+A" (*(p)) \ + : "r" (n) \ + : "memory"); \ +}) + +#define ___xchg(ptr, new, size, sfx, prepend, append) \ ({ \ __typeof__(ptr) __ptr = (ptr); \ __typeof__(new) __new = (new); \ __typeof__(*(ptr)) __ret; \ switch (size) { \ case 4: \ - __asm__ __volatile__ ( \ - " amoswap.w %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ____xchg(".w" sfx, prepend, append, \ + __ret, __ptr, __new); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + ____xchg(".d" sfx, prepend, append, \ + __ret, __ptr, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -37,114 +42,36 @@ __ret; \ }) -#define arch_xchg_relaxed(ptr, x) \ +#define __xchg_relaxed(ptr, new, size) \ + ___xchg(ptr, new, size, "", "", "") + +#define _arch_xchg(order, ptr, x) \ ({ \ __typeof__(*(ptr)) _x_ = (x); \ - (__typeof__(*(ptr))) __xchg_relaxed((ptr), \ - _x_, sizeof(*(ptr))); \ + (__typeof__(*(ptr))) __xchg ## order((ptr), \ + _x_, sizeof(*(ptr))); \ }) +#define arch_xchg_relaxed(ptr, x) \ + _arch_xchg(_relaxed, ptr, x) + #define __xchg_acquire(ptr, new, size) \ -({ \ - __typeof__(ptr) __ptr = (ptr); \ - __typeof__(new) __new = (new); \ - __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"); \ - break; \ - case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d %0, %2, %1\n" \ - RISCV_ACQUIRE_BARRIER \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___xchg(ptr, new, size, "", "", RISCV_ACQUIRE_BARRIER) #define arch_xchg_acquire(ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ = (x); \ - (__typeof__(*(ptr))) __xchg_acquire((ptr), \ - _x_, sizeof(*(ptr))); \ -}) + _arch_xchg(_acquire, ptr, x) #define __xchg_release(ptr, new, size) \ -({ \ - __typeof__(ptr) __ptr = (ptr); \ - __typeof__(new) __new = (new); \ - __typeof__(*(ptr)) __ret; \ - switch (size) { \ - case 4: \ - __asm__ __volatile__ ( \ - RISCV_RELEASE_BARRIER \ - " amoswap.w %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ - break; \ - case 8: \ - __asm__ __volatile__ ( \ - RISCV_RELEASE_BARRIER \ - " amoswap.d %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___xchg(ptr, new, size, "", RISCV_RELEASE_BARRIER, "") #define arch_xchg_release(ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ = (x); \ - (__typeof__(*(ptr))) __xchg_release((ptr), \ - _x_, sizeof(*(ptr))); \ -}) + _arch_xchg(_release, ptr, x) #define __xchg(ptr, new, size) \ -({ \ - __typeof__(ptr) __ptr = (ptr); \ - __typeof__(new) __new = (new); \ - __typeof__(*(ptr)) __ret; \ - switch (size) { \ - case 4: \ - __asm__ __volatile__ ( \ - " amoswap.w.aqrl %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ - break; \ - case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d.aqrl %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) + ___xchg(ptr, new, size, ".aqrl", "", "") #define arch_xchg(ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ = (x); \ - (__typeof__(*(ptr))) __xchg((ptr), _x_, sizeof(*(ptr))); \ -}) + _arch_xchg(, ptr, x) #define xchg32(ptr, x) \ ({ \