Received: by 2002:a05:6358:700f:b0:131:369:b2a3 with SMTP id 15csp3334657rwo; Fri, 4 Aug 2023 03:20:14 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFj/8pooZhHvYaXUsU1+AgFOqv6iR5aVhOyHriZIAsws6jMwCjHgppvioue6O1mVCxoAHwa X-Received: by 2002:a17:902:dad2:b0:1b3:fafd:11c5 with SMTP id q18-20020a170902dad200b001b3fafd11c5mr1033417plx.44.1691144414163; Fri, 04 Aug 2023 03:20:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691144414; cv=none; d=google.com; s=arc-20160816; b=MIgq2oyUmSgrGH+noVZKWMlOrK3xJf6z6ydiwPjZNDPKaKF89V3zVsCzqHaZJGGdVw GZkFGl51W9tMv/6/Oz+EIrwGvWExgxP28wcnbrsshhNwrUFWK/MRz6CxU6v5UNO1eiE5 rhHpWNtdSUUuZyJgJEdh2jGKIgV0yr1KwT9f4bJlCTiQQaTaPvN3aMd6cO4sqUREXi4V 0ru043qN1847xS+0Jz7QneJu5aMzwdPq5DKbofKExtiHb9la18AutTLzuhVX+0EOAPGv +my36q3u/3PCVIl0uYStU/o4hPVxj36VJp0BifGqz2KZ13UZyZPPKsCs1VqpSmltn7+d 9KIw== 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=cG8axZDwBuWJQ+k2FsV81xz+WKLWi2QNSuhJ6eUeaqw=; fh=FxqTis2h219JnGsyNyJvfj1gvIHk0Hc+1mtVlaTtEr0=; b=wHICTzC5VRIAEuFxONgi/GMPXaouRu1mNMTOvEb5Zeoyfj3ykYjnsD6x7S4wxcXd50 RyAwTU+gp6wh3fvUqXs+lKWKwjQIGi+aPEV2iw4JgeT5ZRPt1WK49UB+mO5Jd2QKkJ+d G7hbdBO3oUfudXPJ+kfT7jTk7+OUjDvo35JMdBX0NmSAppTMO9jEIP7GBtYvqWftRMn7 2FynLxCn1pBZFCVnNHNQl13lo3ZEo0vYWQ5piYnso5fgZ6m1CM5gAGm41dW7YySBIR9I TxVnvAhZ9VbYUBibvTZhmw41URXVhlo8cuUtIn3Dbn1QYoBE0GS9WoxNCIgUEodGPaKe YEZA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=aTHJZVPD; 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 kr6-20020a170903080600b001bb974b26c4si1471393plb.238.2023.08.04.03.20.02; Fri, 04 Aug 2023 03:20:14 -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=aTHJZVPD; 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 S229965AbjHDIvp (ORCPT + 99 others); Fri, 4 Aug 2023 04:51:45 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44056 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229822AbjHDIuQ (ORCPT ); Fri, 4 Aug 2023 04:50:16 -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 75BD949C7 for ; Fri, 4 Aug 2023 01:49:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691138966; 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=cG8axZDwBuWJQ+k2FsV81xz+WKLWi2QNSuhJ6eUeaqw=; b=aTHJZVPDvXQU2ACiubqkDYqnlmbj5zYDAG/bmn+G8tS70DyVMre0LOF/ZDT3KUToLFJwBi 3WzGhEABpzH0AgttQNx9eAZ2rYuHjl4+JruBywDNzMKmmG7vZe8ystAwHVasUd7IeSQ6HD i6QhTenlWLyNFwXkSjRdJSIqmIwtk3E= 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-626-UQBDIiOyNkmOJjCclkQuLQ-1; Fri, 04 Aug 2023 04:49:25 -0400 X-MC-Unique: UQBDIiOyNkmOJjCclkQuLQ-1 Received: by mail-oa1-f72.google.com with SMTP id 586e51a60fabf-1b44332e279so2762838fac.0 for ; Fri, 04 Aug 2023 01:49:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691138965; x=1691743765; 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=cG8axZDwBuWJQ+k2FsV81xz+WKLWi2QNSuhJ6eUeaqw=; b=H7xeGF6X6WpNgVzDtInrI5wnb/sahGG9gos4qoXJTjLYCTsjWj8IgNFaeA4TVck8+i csvIJsMmCGb0+w4hkIkO7BQCzaoXLSltCPtTnrcrqTjpmceHYYDcsZT5FsFip+T1q7Xt rR5VGqcys8viFd/ie0Hkjnfbx4nxCJ1TeSh7w/85mVrXSZsCB+u/Z6TmxD8RcZ7aYs7a G2ECwBTVutwHjv4cokXaIvM7MIQgiyH9LW3npIuSQp0OUW84MZQZeiB+QKdbmVUlwS2K T0mE2PYZRyBfzI8UtwxxMgK0dHAVGgG2rC+xzyACu/XYqH6wCZ8FfFWtUXg+OLe6N+Ar TnKA== X-Gm-Message-State: AOJu0Yz7r6NktoZhPY4XB6TLZkLw7+4ss1sXDozs+/G9ntdnHLc5PJx9 wyuBQPFIzRDsqnjdf2TYIiwiQbD4AeLPhhf2rNl2wtZOCkbTbTK0oxP46ozT3P/7+qGoNcFZ7yh GNfXXsZEh8AZxrqUaY7fBfIin X-Received: by 2002:a05:6870:5b98:b0:1be:c8e2:3ec3 with SMTP id em24-20020a0568705b9800b001bec8e23ec3mr1046104oab.14.1691138964800; Fri, 04 Aug 2023 01:49:24 -0700 (PDT) X-Received: by 2002:a05:6870:5b98:b0:1be:c8e2:3ec3 with SMTP id em24-20020a0568705b9800b001bec8e23ec3mr1046089oab.14.1691138964579; Fri, 04 Aug 2023 01:49:24 -0700 (PDT) Received: from localhost.localdomain ([2804:1b3:a801:d380:694f:4f52:764c:4b7f]) by smtp.gmail.com with ESMTPSA id f8-20020a4ab008000000b0055516447257sm685679oon.29.2023.08.04.01.49.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Aug 2023 01:49:24 -0700 (PDT) From: Leonardo Bras To: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Andrea Parri , Geert Uytterhoeven , Andrzej Hajda , Palmer Dabbelt , Guo Ren Cc: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [RFC PATCH v3 1/5] riscv/cmpxchg: Deduplicate xchg() asm functions Date: Fri, 4 Aug 2023 05:48:54 -0300 Message-ID: <20230804084900.1135660-3-leobras@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230804084900.1135660-2-leobras@redhat.com> References: <20230804084900.1135660-2-leobras@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE autolearn=ham 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 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: Andrea Parri --- arch/riscv/include/asm/cmpxchg.h | 136 +++++-------------------------- 1 file changed, 22 insertions(+), 114 deletions(-) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h index 2f4726d3cfcc..ec4ea4f3f908 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -11,60 +11,30 @@ #include #include -#define __xchg_relaxed(ptr, new, size) \ +#define __arch_xchg(sfx, prepend, append, r, p, n) \ ({ \ - __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"); \ - break; \ - case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d %0, %2, %1\n" \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ - break; \ - default: \ - BUILD_BUG(); \ - } \ - __ret; \ -}) - -#define arch_xchg_relaxed(ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ = (x); \ - (__typeof__(*(ptr))) __xchg_relaxed((ptr), \ - _x_, sizeof(*(ptr))); \ + __asm__ __volatile__ ( \ + prepend \ + " amoswap" sfx " %0, %2, %1\n" \ + append \ + : "=r" (r), "+A" (*(p)) \ + : "r" (n) \ + : "memory"); \ }) -#define __xchg_acquire(ptr, new, size) \ +#define _arch_xchg(ptr, new, sfx, prepend, append) \ ({ \ - __typeof__(ptr) __ptr = (ptr); \ - __typeof__(new) __new = (new); \ + __typeof__(*(ptr)) __new = (new); \ __typeof__(*(ptr)) __ret; \ - switch (size) { \ + __typeof__(ptr) __ptr = (ptr); \ + switch (sizeof(*__ptr)) { \ case 4: \ - __asm__ __volatile__ ( \ - " amoswap.w %0, %2, %1\n" \ - RISCV_ACQUIRE_BARRIER \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + __arch_xchg(".w" sfx, prepend, append, \ + __ret, __ptr, __new); \ break; \ case 8: \ - __asm__ __volatile__ ( \ - " amoswap.d %0, %2, %1\n" \ - RISCV_ACQUIRE_BARRIER \ - : "=r" (__ret), "+A" (*__ptr) \ - : "r" (__new) \ - : "memory"); \ + __arch_xchg(".d" sfx, prepend, append, \ + __ret, __ptr, __new); \ break; \ default: \ BUILD_BUG(); \ @@ -72,79 +42,17 @@ __ret; \ }) -#define arch_xchg_acquire(ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ = (x); \ - (__typeof__(*(ptr))) __xchg_acquire((ptr), \ - _x_, sizeof(*(ptr))); \ -}) +#define arch_xchg_relaxed(ptr, x) \ + _arch_xchg(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; \ -}) +#define arch_xchg_acquire(ptr, x) \ + _arch_xchg(ptr, x, "", "", RISCV_ACQUIRE_BARRIER) #define arch_xchg_release(ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ = (x); \ - (__typeof__(*(ptr))) __xchg_release((ptr), \ - _x_, sizeof(*(ptr))); \ -}) - -#define __arch_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; \ -}) + _arch_xchg(ptr, x, "", RISCV_RELEASE_BARRIER, "") #define arch_xchg(ptr, x) \ -({ \ - __typeof__(*(ptr)) _x_ = (x); \ - (__typeof__(*(ptr))) __arch_xchg((ptr), _x_, sizeof(*(ptr))); \ -}) + _arch_xchg(ptr, x, ".aqrl", "", "") #define xchg32(ptr, x) \ ({ \ -- 2.41.0