Received: by 2002:a05:6359:6284:b0:131:369:b2a3 with SMTP id se4csp5061840rwb; Tue, 8 Aug 2023 20:06:25 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHN+QYwJeXupY7Mn6Ozge2CFGa6GHbLi5cc0fC4fm4AvqxohYA+lQwNEKrM974By2pjV49T X-Received: by 2002:a05:6a20:441f:b0:13f:b3b4:1986 with SMTP id ce31-20020a056a20441f00b0013fb3b41986mr2155423pzb.12.1691550384782; Tue, 08 Aug 2023 20:06:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691550384; cv=none; d=google.com; s=arc-20160816; b=Wi5Vk53YpsnTO+ZKADtl277DliCP+yBSpjFXXalSeFdSoCzVtqfr3D+Z8G47cSTwhi FBuhTURHVonN78gadBU0HaH01YLYomWfYBaPgC35m+73JQUI3lJIjdqhmYyPUIADeeYB ww+i1hP0Tm6sMBZLnDgi5HpGOq+eXs/8BNyLw9tKbVFAH+lCR2xOvjhNggCEuuRjS5WJ uiWjCnVYPr7aL4YOGmyAGiCQEukTpHdZi0RwQ+Sgc/PEcMo6MMSwPmK0wIdD0zv9mO8w j51XVvgSuzhA6KSd/nvIEBO+ohkLFo2Lyfq2oH0UUrq8vBfyVyOJgSXRLzY40tOgovLA 4Z8A== 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=QSwkRG8bcSleBdtSsjCnyBmp/8Y+sHuiXOKQQOlQtH4=; fh=RC6bsfN8bPPp/iDUCpnJMEtdiqa5XC4G6XtB4MmAVnA=; b=WNczu+wfxMowFPf51e7a9/mire85FE4c74ZGA4fKYxBEy4KhkW2vZPyPoFl0aPgBRt ojmt5sPXY6UykgqcNNxHMgbHhO6oJV08/ra1zjnLVhvRKIkDIS53u0PkoqHKf/fmdV85 JEpMw0CCNVlzrKfMNoUU4lYNISQRp3OVRzAAR9WP1OEUvXr30ksVTnTRAvm0e7vUXIN/ HdnD0W130vwdU+jbt/+OrMHtxmEsfm/LziuSKxx6igu+fv4hfY8svCLWNo8rAifKHxJ5 QX0AYqG1eLZjxhSQMV2FgZHhP2EyqgGwJc2c3a6F8FqOQx44SdTonGTGZ0xtPaGNxFqI yHKg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=WnzLQHsd; 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 bk13-20020a056a02028d00b0056532015a58si726249pgb.774.2023.08.08.20.06.13; Tue, 08 Aug 2023 20:06:24 -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=WnzLQHsd; 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 S230320AbjHICPM (ORCPT + 99 others); Tue, 8 Aug 2023 22:15:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39308 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229540AbjHICPI (ORCPT ); Tue, 8 Aug 2023 22:15:08 -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 CA68E1BDA for ; Tue, 8 Aug 2023 19:14:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1691547263; 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=QSwkRG8bcSleBdtSsjCnyBmp/8Y+sHuiXOKQQOlQtH4=; b=WnzLQHsd7uNV/mInjNmcgWwQEaaqMqkhL+Q107HLGkQxwVk+E14r9nXvBDSaoKZqojPxDe bBNsRJqoffpb6Jizzm1e8sJSbD5PzZGvcLIfGXyRBI/JhxsbdJP8Klkiw4P+vk0kjVQl0r +dkCqWxK7VGWw0LjZUJ6ab14NaHa8/k= Received: from mail-oa1-f69.google.com (mail-oa1-f69.google.com [209.85.160.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-371-5fwvUZTtMJeHDZxbD_W5lg-1; Tue, 08 Aug 2023 22:14:21 -0400 X-MC-Unique: 5fwvUZTtMJeHDZxbD_W5lg-1 Received: by mail-oa1-f69.google.com with SMTP id 586e51a60fabf-1b0812d43a0so597717fac.0 for ; Tue, 08 Aug 2023 19:14:21 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691547261; x=1692152061; 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=QSwkRG8bcSleBdtSsjCnyBmp/8Y+sHuiXOKQQOlQtH4=; b=dFc0LrQprMs9CqGnkc3Wbbm2DVSObRM1LEPTfAs7XL03ee32d3gBqsivnQdLqWe26R qdB2KWtFZMTOYsbcyLtH9Flpz88ELwpi63B95tr6E3n+YkAat9lLPWBw9AsQOB0Jm2Yc 9NgVctX+dNVJD3zMUjmG6LdQ0tP1ziWazSpt7bOoczSuCz93aPqV63H+xSW02zkFm4Mg t50d7I+nutIAp03kSVOlCZHGCNDLlXqH0o0miTVJn+Y19AuhFnFwtaY0+lNa4zhaf2Us tfeyCWF0/8GoL2VW9p5fHGrEggJg06X9L8LFW1kkCGOfwTujn8Y/SzJPhf0T3EI5pzkc NP2Q== X-Gm-Message-State: AOJu0YyEgv2SRvyg14A8BBynm4qyKFGSkdZ33GKCEC5njkl/7pumrmTY BzrZFMbFmo6ngyc6tf6fS8fs1W15HqPjRftyQ8c1TJINpGSdeo2+2meIRlGD7Tjjj1s/sY46D4y qN5H2/W8MjztJxxRl/bBY05R1 X-Received: by 2002:a05:6870:b6a9:b0:1bf:1c49:749c with SMTP id cy41-20020a056870b6a900b001bf1c49749cmr8730778oab.13.1691547260770; Tue, 08 Aug 2023 19:14:20 -0700 (PDT) X-Received: by 2002:a05:6870:b6a9:b0:1bf:1c49:749c with SMTP id cy41-20020a056870b6a900b001bf1c49749cmr8730768oab.13.1691547260529; Tue, 08 Aug 2023 19:14:20 -0700 (PDT) Received: from localhost.localdomain ([2804:431:c7ec:e667:6b7d:ed55:c363:a088]) by smtp.gmail.com with ESMTPSA id v10-20020a4a8c4a000000b0054fba751207sm6475518ooj.47.2023.08.08.19.14.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 08 Aug 2023 19:14:20 -0700 (PDT) From: Leonardo Bras To: Will Deacon , Peter Zijlstra , Boqun Feng , Mark Rutland , Paul Walmsley , Palmer Dabbelt , Albert Ou , Leonardo Bras , Andrea Parri , Ingo Molnar , Geert Uytterhoeven , Andrzej Hajda , Palmer Dabbelt , Guo Ren Cc: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org Subject: [RFC PATCH v4 5/5] riscv/cmpxchg: Implement xchg for variables of size 1 and 2 Date: Tue, 8 Aug 2023 23:13:10 -0300 Message-ID: <20230809021311.1390578-7-leobras@redhat.com> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230809021311.1390578-2-leobras@redhat.com> References: <20230809021311.1390578-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 cfada8a7cfd2..1640308f3c4d 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)) __new = (new); \ __typeof__(*(ptr)) __ret; \ __typeof__(ptr) __ptr = (ptr); \ + \ 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