Received: by 2002:a05:6358:51dd:b0:131:369:b2a3 with SMTP id 29csp641420rwl; Wed, 9 Aug 2023 22:12:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF+ilje/6V8WCULPeLdNmJQpG8LLoKyfAS3ZR9ABurPbsvGoSYmQTDSSoHV41sWvf5aSnWU X-Received: by 2002:a17:906:1099:b0:993:d632:2c3 with SMTP id u25-20020a170906109900b00993d63202c3mr1314787eju.21.1691644325073; Wed, 09 Aug 2023 22:12:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691644325; cv=none; d=google.com; s=arc-20160816; b=LFCnDVZuWH0LB9M+qPIr/ivSKOveAzBUvd32qiboU9n57yYmmw9FoxEjfVf10vsw2X nT8mivYC05emvHyiab+l+mlDiOKSh3wbclbJtH35cmsVdDquFS6uigbuxWA/5rM7YSg+ Mq8E7P8S6m0CpPGRg3dTj3kGN22aFHLIm/TmMADDQQGnvLs/iVcILxq0AeoFbxqMShg2 zV+1afkEcEa6p7RY01/rYla+KPUB8Q+yUyi1GNOFr4pSwTsxPlE3h2GlOqGjLkcqaH9H kxzJCkTlPfbzSeEBUQx95gFGRcmJQWw32p5S628b97FBy2s5vPo+A0K23J/L53cSICuF p6uQ== 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=S9RDXJ5aWrysjU+rhwlqeMwMOsK56lpvgf+6LxPUCTg=; fh=/HAhUCSgceMdCiMxlR/A+InTwiEDI9nNuAOlAQnSzJk=; b=WKugZKU+fAwBUkkwh6gRqIKmSy9vWwybq11E6aw2QM3cI4XPv5sWeabWFOu8KL7p+H O8clatzOuRmKwJntPJX1HLC9TSShXkCxjTEml9vfNqk947UTk9isKY3RvViwGueO1SBd ru/4X5NKOuCkXjWuyBJpURgra4sVJu59KE06k3oecqKRHd76wbRs4FAc7nNWwjwTY5rz yizHFOI07PKJrtJiVmp3smalHVKKT4fYAmhdjEHXAMKwWcThLsXBsoKz1wjWAfVgCsV+ MxJ65J8adnQG3/+guWq1NKALoJMl+bY1IfwOsDwhcPbd4skgAHl08kySHiyQiK8fvSJ7 /I/A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=IRHbNtSc; 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 x15-20020a170906710f00b0099233867740si753727ejj.144.2023.08.09.22.11.39; Wed, 09 Aug 2023 22:12:05 -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=IRHbNtSc; 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 S232583AbjHJEF2 (ORCPT + 99 others); Thu, 10 Aug 2023 00:05:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46250 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232641AbjHJEFU (ORCPT ); Thu, 10 Aug 2023 00:05:20 -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 43FD0E69 for ; Wed, 9 Aug 2023 21:04:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691640276; 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=S9RDXJ5aWrysjU+rhwlqeMwMOsK56lpvgf+6LxPUCTg=; b=IRHbNtScy0/Nrm7nV9r/y/jDk0kJK6cOENHOsCDLgqyMBAi4IIpvX9ov2xnbrD/akhZIK8 mHqHZOooBlGmgdisaSzREFkzPQr1WrS1myiJ40xZSsvnlGdAVUJhvHP8/1wnd50b2ko17m BLREvk0Csn+s2rLNZXYk0NViE+DvLIQ= Received: from mail-oi1-f199.google.com (mail-oi1-f199.google.com [209.85.167.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-283-Pk3jcTnrM8y507xMyNPyBQ-1; Thu, 10 Aug 2023 00:04:34 -0400 X-MC-Unique: Pk3jcTnrM8y507xMyNPyBQ-1 Received: by mail-oi1-f199.google.com with SMTP id 5614622812f47-3a5ab57036fso584576b6e.3 for ; Wed, 09 Aug 2023 21:04:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691640273; x=1692245073; 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=S9RDXJ5aWrysjU+rhwlqeMwMOsK56lpvgf+6LxPUCTg=; b=AVHSK3m9lea5FMgXD2ccnwiFZZy4Z/kiHwvRVWDLZGzKwP8b/7bkEpCH/guNS+xikv Dh0C145D27s90BNR2srMaOU1d8i9b/r7O/FGmtQYn/FlI6T+jvgDHiNPNb6JB6pWdNxE DGIvmAFfDm383XezPuWfh2WIgTjFstkvY57FJG4Q0duahSAwh8kXzF67AFZ1mex+HAHV 1ltq0Esv5Qttq+CeBJvR42/EnJ5SLlfD1yproadMVzADKn9I3x3oRO0TCqMakfvchmhQ ++EiKymOSch2tVu6mZCzz69a5gds8AvtFB27s4AmZ4hLlkUqu292cGWbFppUR5YE2X/X phzQ== X-Gm-Message-State: AOJu0YyswhD9dX4SULSfce5BwzVDPYYbM1TQyMxxM+RAbD9txfu9A8zm dEMIYucBfRAFJrNZp5e56iao2sWlDi1cQyjEbytZY/O8A1VissYbI5CoEVz/Tn/8tGOl8ty4DW6 ZXyyAE6cFKOnZS+GPP+BxdHjz X-Received: by 2002:a05:6808:2124:b0:3a7:82e8:8fd1 with SMTP id r36-20020a056808212400b003a782e88fd1mr1804195oiw.20.1691640273706; Wed, 09 Aug 2023 21:04:33 -0700 (PDT) X-Received: by 2002:a05:6808:2124:b0:3a7:82e8:8fd1 with SMTP id r36-20020a056808212400b003a782e88fd1mr1804180oiw.20.1691640273540; Wed, 09 Aug 2023 21:04:33 -0700 (PDT) Received: from localhost.localdomain ([2804:431:c7ec:e667:6b7d:ed55:c363:a088]) by smtp.gmail.com with ESMTPSA id b12-20020aca674c000000b003a7b5193909sm310087oiy.19.2023.08.09.21.04.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 21:04:33 -0700 (PDT) From: Leonardo Bras To: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Andrea Parri , Andrzej Hajda , Arnd Bergmann , Palmer Dabbelt , Guo Ren Cc: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [RFC PATCH v5 5/5] riscv/cmpxchg: Implement xchg for variables of size 1 and 2 Date: Thu, 10 Aug 2023 01:03:47 -0300 Message-ID: <20230810040349.92279-7-leobras@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230810040349.92279-2-leobras@redhat.com> References: <20230810040349.92279-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_BLOCKED,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 xchg for variables of size 1-byte and 2-bytes is not yet available for riscv, even though its present in other architectures such as arm64 and x86. This could lead to not being able to implement some locking mechanisms or requiring some rework to make it work properly. Implement 1-byte and 2-bytes xchg in order to achieve parity with other architectures. Signed-off-by: Leonardo Bras --- arch/riscv/include/asm/cmpxchg.h | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/arch/riscv/include/asm/cmpxchg.h b/arch/riscv/include/asm/cmpxchg.h index ac9d0eeb74e6..26cea2395aae 100644 --- a/arch/riscv/include/asm/cmpxchg.h +++ b/arch/riscv/include/asm/cmpxchg.h @@ -11,6 +11,31 @@ #include #include +#define __arch_xchg_masked(prepend, append, r, p, n) \ +({ \ + u32 *__ptr32b = (u32 *)((ulong)(p) & ~0x3); \ + ulong __s = ((ulong)(p) & (0x4 - sizeof(*p))) * BITS_PER_BYTE; \ + ulong __mask = GENMASK(((sizeof(*p)) * BITS_PER_BYTE) - 1, 0) \ + << __s; \ + ulong __newx = (ulong)(n) << __s; \ + ulong __retx; \ + ulong __rc; \ + \ + __asm__ __volatile__ ( \ + prepend \ + "0: lr.w %0, %2\n" \ + " and %1, %0, %z4\n" \ + " or %1, %1, %z3\n" \ + " sc.w %1, %1, %2\n" \ + " bnez %1, 0b\n" \ + append \ + : "=&r" (__retx), "=&r" (__rc), "+A" (*(__ptr32b)) \ + : "rJ" (__newx), "rJ" (~__mask) \ + : "memory"); \ + \ + r = (__typeof__(*(p)))((__retx & __mask) >> __s); \ +}) + #define __arch_xchg(sfx, prepend, append, r, p, n) \ ({ \ __asm__ __volatile__ ( \ @@ -27,7 +52,13 @@ __typeof__(ptr) __ptr = (ptr); \ __typeof__(*(__ptr)) __new = (new); \ __typeof__(*(__ptr)) __ret; \ + \ switch (sizeof(*__ptr)) { \ + case 1: \ + case 2: \ + __arch_xchg_masked(prepend, append, \ + __ret, __ptr, __new); \ + break; \ case 4: \ __arch_xchg(".w" sfx, prepend, append, \ __ret, __ptr, __new); \ -- 2.41.0