From: Leonardo Bras <leobras@redhat.com>
To: Will Deacon <will@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Boqun Feng <boqun.feng@gmail.com>,
Mark Rutland <mark.rutland@arm.com>,
Paul Walmsley <paul.walmsley@sifive.com>,
Palmer Dabbelt <palmer@dabbelt.com>,
Albert Ou <aou@eecs.berkeley.edu>,
Leonardo Bras <leobras@redhat.com>,
Andrea Parri <parri.andrea@gmail.com>,
Geert Uytterhoeven <geert@linux-m68k.org>,
Andrzej Hajda <andrzej.hajda@intel.com>,
Palmer Dabbelt <palmer@rivosinc.com>, Guo Ren <guoren@kernel.org>
Cc: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org
Subject: [RFC PATCH v3 5/5] riscv/cmpxchg: Implement xchg for variables of size 1 and 2
Date: Fri, 4 Aug 2023 05:48:58 -0300 [thread overview]
Message-ID: <20230804084900.1135660-7-leobras@redhat.com> (raw)
In-Reply-To: <20230804084900.1135660-2-leobras@redhat.com>
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 <leobras@redhat.com>
---
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 dfb433ac544f..943f094375c7 100644
--- a/arch/riscv/include/asm/cmpxchg.h
+++ b/arch/riscv/include/asm/cmpxchg.h
@@ -11,6 +11,31 @@
#include <asm/barrier.h>
#include <asm/fence.h>
+#define __arch_xchg_mask(prepend, append, r, p, n) \
+({ \
+ /* Depends on 2-byte variables being 2-byte aligned */ \
+ ulong __s = ((ulong)(p) & 0x3) * BITS_PER_BYTE; \
+ ulong __mask = GENMASK(((sizeof(*p)) * BITS_PER_BYTE) - 1, 0) \
+ << __s; \
+ ulong __newx = (ulong)(n) << __s; \
+ ulong __retx; \
+ register unsigned int __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" (*(p)) \
+ : "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_mask(prepend, append, \
+ __ret, __ptr, __new); \
+ break; \
case 4: \
__arch_xchg(".w" sfx, prepend, append, \
__ret, __ptr, __new); \
--
2.41.0
prev parent reply other threads:[~2023-08-04 8:52 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-08-04 8:48 [RFC PATCH v3 0/5] Rework & improve riscv cmpxchg.h and atomic.h Leonardo Bras
2023-08-04 8:48 ` [RFC PATCH v3 1/5] riscv/cmpxchg: Deduplicate xchg() asm functions Leonardo Bras
2023-08-04 8:48 ` [RFC PATCH v3 2/5] riscv/cmpxchg: Deduplicate cmpxchg() asm and macros Leonardo Bras
2023-08-04 8:48 ` [RFC PATCH v3 3/5] riscv/atomic.h : Deduplicate arch_atomic.* Leonardo Bras
2023-08-04 8:48 ` [RFC PATCH v3 4/5] riscv/cmpxchg: Implement cmpxchg for variables of size 1 and 2 Leonardo Bras
2023-08-04 17:45 ` Guo Ren
2023-08-05 3:14 ` Leonardo Bras Soares Passos
2023-08-05 4:24 ` Guo Ren
2023-08-07 16:17 ` Leonardo Bras Soares Passos
2023-08-09 2:02 ` Leonardo Bras Soares Passos
2023-08-10 4:06 ` Leonardo Bras Soares Passos
2023-08-11 2:32 ` Guo Ren
2023-08-04 8:48 ` Leonardo Bras [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230804084900.1135660-7-leobras@redhat.com \
--to=leobras@redhat.com \
--cc=andrzej.hajda@intel.com \
--cc=aou@eecs.berkeley.edu \
--cc=boqun.feng@gmail.com \
--cc=geert@linux-m68k.org \
--cc=guoren@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-riscv@lists.infradead.org \
--cc=mark.rutland@arm.com \
--cc=palmer@dabbelt.com \
--cc=palmer@rivosinc.com \
--cc=parri.andrea@gmail.com \
--cc=paul.walmsley@sifive.com \
--cc=peterz@infradead.org \
--cc=will@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®