From: Yunhui Cui <cuiyunhui@bytedance.com>
To: pjw@kernel.org, palmer@dabbelt.com, aou@eecs.berkeley.edu,
alex@ghiti.fr, dennis@kernel.org, tj@kernel.org, cl@gentwo.org,
oleg@redhat.com, ast@kernel.org, daniel@iogearbox.net,
andrii@kernel.org, eddyz87@gmail.com, memxor@gmail.com,
martin.lau@linux.dev, song@kernel.org, yonghong.song@linux.dev,
jolsa@kernel.org, emil@etsalapatis.com, ihor.solodrai@linux.dev,
bjorn@kernel.org, pulehui@huawei.com, puranjay@kernel.org,
broonie@kernel.org, cuiyunhui@bytedance.com, dave@stgolabs.net,
david.laight.linux@gmail.com, jszhang@kernel.org,
tglx@kernel.org, conor.dooley@microchip.com, thuth@redhat.com,
ajones@ventanamicro.com, ben.dooks@codethink.co.uk,
rkrcmar@ventanamicro.com, samuel.holland@sifive.com,
zong.li@sifive.com, debug@rivosinc.com, seanwascoding@gmail.com,
andybnac@gmail.com, menglong8.dong@gmail.com,
cyrilbur@tenstorrent.com, wangruikang@iscas.ac.cn,
atishp@rivosinc.com, apatel@ventanamicro.com, arnd@arndb.de,
nathan@kernel.org, nick.desaulniers+lkml@gmail.com,
morbo@google.com, justinstitt@google.com,
qingfang.deng@siflower.com.cn, linux-riscv@lists.infradead.org,
linux-kernel@vger.kernel.org, linux-mm@kvack.org,
bpf@vger.kernel.org, linux-arch@vger.kernel.org,
llvm@lists.linux.dev
Subject: [PATCH v10 5/9] riscv: implement preemptible this_cpu add_return operations
Date: Thu, 12 Nov 2026 18:41:45 +0800 [thread overview]
Message-ID: <20261112104149.38589-6-cuiyunhui@bytedance.com> (raw)
In-Reply-To: <20261112104149.38589-1-cuiyunhui@bytedance.com>
Apply the recoverable per-CPU address handling to add-return operations.
Native-width AMOs return the old value, so derive the new value required
by the percpu API from the operand. Use Zabha for subword operations when
available, or a masked LR/SC sequence that returns the updated field.
Signed-off-by: Yunhui Cui <cuiyunhui@bytedance.com>
---
arch/riscv/include/asm/percpu.h | 134 ++++++++++++++++++++++++++++++++
1 file changed, 134 insertions(+)
diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
index 46eafa1ef02a1..9ff02f3811550 100644
--- a/arch/riscv/include/asm/percpu.h
+++ b/arch/riscv/include/asm/percpu.h
@@ -203,6 +203,129 @@ __PERCPU_8_16_OP_CASE(and, 16, .h, and, "and")
__PERCPU_8_16_OP_CASE(or, 8, .b, or, "or")
__PERCPU_8_16_OP_CASE(or, 16, .h, or, "or")
+static __always_inline u32
+__percpu_add_return_amo_case_32(void __percpu *pcp, unsigned long val)
+{
+ __PCPU_OP_CONTEXT();
+ u32 ret;
+
+ asm volatile (__PCPU_GPRS_BEGIN
+ "amoadd.w %[ret], %[val], 0(%[addr])\n"
+ __PCPU_GPRS_END
+ : [gprs] "=m" (__ti->pcpu_gprs),
+ [off] "=&r" (__off), [addr] "=&r" (__addr),
+ [ret] "=&r" (ret)
+ : [pcpu_offset] "m" (__ti->pcpu_offset),
+ [pcp] "r" (pcp), [val] "r" ((u32)val)
+ : "memory");
+
+ return ret + (u32)val;
+}
+
+#ifdef CONFIG_64BIT
+static __always_inline u64
+__percpu_add_return_amo_case_64(void __percpu *pcp, unsigned long val)
+{
+ __PCPU_OP_CONTEXT();
+ u64 ret;
+
+ asm volatile (__PCPU_GPRS_BEGIN
+ "amoadd.d %[ret], %[val], 0(%[addr])\n"
+ __PCPU_GPRS_END
+ : [gprs] "=m" (__ti->pcpu_gprs),
+ [off] "=&r" (__off), [addr] "=&r" (__addr),
+ [ret] "=&r" (ret)
+ : [pcpu_offset] "m" (__ti->pcpu_offset),
+ [pcp] "r" (pcp), [val] "r" ((u64)val)
+ : "memory");
+
+ return ret + (u64)val;
+}
+#endif
+
+static __always_inline unsigned long
+__percpu_add_return_8_16(void __percpu *pcp, unsigned long val,
+ unsigned int bits)
+{
+ __PCPU_OP_CONTEXT();
+ const u32 field_mask = GENMASK(bits - 1, 0);
+ const unsigned long shift = PERCPU_8_16_GET_SHIFT(pcp);
+ const u32 mask = field_mask << shift;
+ const u32 val_trunc = val & field_mask;
+ void __percpu *pcp32 = PERCPU_8_16_GET_PCP32(pcp);
+ u32 old, new, field;
+ unsigned long ret;
+
+ if (IS_ENABLED(CONFIG_RISCV_ISA_ZABHA) &&
+ riscv_has_extension_unlikely(RISCV_ISA_EXT_ZABHA)) {
+ if (bits == 8) {
+ asm volatile (__PCPU_GPRS_BEGIN
+ ".option push\n"
+ ".option arch, +zabha\n"
+ "amoadd.b %[ret], %[val], 0(%[addr])\n"
+ ".option pop\n"
+ __PCPU_GPRS_END
+ : [gprs] "=m" (__ti->pcpu_gprs),
+ [off] "=&r" (__off), [addr] "=&r" (__addr),
+ [ret] "=&r" (ret)
+ : [pcpu_offset] "m" (__ti->pcpu_offset),
+ [pcp] "r" (pcp), [val] "r" ((u8)val)
+ : "memory");
+ return (u8)(ret + val);
+ }
+
+ asm volatile (__PCPU_GPRS_BEGIN
+ ".option push\n"
+ ".option arch, +zabha\n"
+ "amoadd.h %[ret], %[val], 0(%[addr])\n"
+ ".option pop\n"
+ __PCPU_GPRS_END
+ : [gprs] "=m" (__ti->pcpu_gprs),
+ [off] "=&r" (__off), [addr] "=&r" (__addr),
+ [ret] "=&r" (ret)
+ : [pcpu_offset] "m" (__ti->pcpu_offset),
+ [pcp] "r" (pcp), [val] "r" ((u16)val)
+ : "memory");
+ return (u16)(ret + val);
+ }
+
+ asm volatile (__PCPU_GPRS_BEGIN
+ "0: lr.w %[old], 0(%[addr])\n"
+ "and %[field], %[old], %[mask]\n"
+ "srl %[field], %[field], %[shift]\n"
+ "add %[field], %[field], %[val]\n"
+ "and %[field], %[field], %[field_mask]\n"
+ "sll %[field], %[field], %[shift]\n"
+ "and %[new], %[old], %[inv_mask]\n"
+ "or %[new], %[new], %[field]\n"
+ "sc.w %[new], %[new], 0(%[addr])\n"
+ "bnez %[new], 0b\n"
+ __PCPU_GPRS_END
+ : [gprs] "=m" (__ti->pcpu_gprs),
+ [off] "=&r" (__off), [addr] "=&r" (__addr),
+ [old] "=&r" (old), [new] "=&r" (new),
+ [field] "=&r" (field)
+ : [pcpu_offset] "m" (__ti->pcpu_offset),
+ [pcp] "r" (pcp32), [mask] "r" (mask),
+ [shift] "r" (shift), [val] "r" (val_trunc),
+ [field_mask] "r" (field_mask), [inv_mask] "r" (~mask)
+ : "memory");
+
+ return field >> shift;
+}
+
+static __always_inline u8
+__percpu_add_return_amo_case_8(void __percpu *pcp, unsigned long val)
+{
+ return __percpu_add_return_8_16(pcp, val, 8);
+}
+
+static __always_inline u16
+__percpu_add_return_amo_case_16(void __percpu *pcp, unsigned long val)
+{
+ return __percpu_add_return_8_16(pcp, val, 16);
+}
+
#define _pcp_wrap(op, pcp, ...) \
({ \
op(&(pcp), __VA_ARGS__); \
@@ -256,6 +379,17 @@ __PERCPU_8_16_OP_CASE(or, 16, .h, or, "or")
#define this_cpu_or_8(pcp, val) _pcp_wrap(__percpu_or_amo_case_64, pcp, val)
#endif
+#define this_cpu_add_return_1(pcp, val) \
+ _pcp_wrap_return(__percpu_add_return_amo_case_8, pcp, val)
+#define this_cpu_add_return_2(pcp, val) \
+ _pcp_wrap_return(__percpu_add_return_amo_case_16, pcp, val)
+#define this_cpu_add_return_4(pcp, val) \
+ _pcp_wrap_return(__percpu_add_return_amo_case_32, pcp, val)
+#ifdef CONFIG_64BIT
+#define this_cpu_add_return_8(pcp, val) \
+ _pcp_wrap_return(__percpu_add_return_amo_case_64, pcp, val)
+#endif
+
#undef PERCPU_RW_OPS
#undef __PERCPU_AMO_OP_CASE
#undef __PERCPU_8_16_OP_CASE
--
2.39.5
next prev parent reply other threads:[~2026-11-12 10:44 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 10:41 [PATCH v10 0/9] riscv: optimize percpu access Yunhui Cui
2026-11-12 10:41 ` [PATCH v10 1/9] riscv: store percpu offset into thread_info Yunhui Cui
2026-11-12 10:41 ` [PATCH v10 2/9] riscv: add infrastructure for preemptible this_cpu operations Yunhui Cui
2026-11-12 12:02 ` David Laight
2026-11-12 10:41 ` [PATCH v10 3/9] riscv: implement preemptible this_cpu read and write operations Yunhui Cui
2026-11-12 10:41 ` [PATCH v10 4/9] riscv: implement preemptible this_cpu void RMW operations Yunhui Cui
2026-11-12 10:41 ` Yunhui Cui [this message]
2026-11-12 11:30 ` [PATCH v10 5/9] riscv: implement preemptible this_cpu add_return operations bot+bpf-ci
2026-11-12 12:09 ` David Laight
2026-11-12 10:41 ` [PATCH v10 6/9] riscv: implement preemptible this_cpu xchg operations Yunhui Cui
2026-11-12 10:41 ` [PATCH v10 7/9] riscv: implement preemptible this_cpu cmpxchg operations Yunhui Cui
2026-11-12 10:41 ` [PATCH v10 8/9] riscv: implement preemptible this_cpu cmpxchg128 Yunhui Cui
2026-11-12 10:41 ` [PATCH v10 9/9] riscv: select HAVE_CMPXCHG_LOCAL Yunhui Cui
2026-11-12 11:30 ` bot+bpf-ci
2026-11-12 12:10 ` [PATCH v10 0/9] riscv: optimize percpu access Jisheng Zhang
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=20261112104149.38589-6-cuiyunhui@bytedance.com \
--to=cuiyunhui@bytedance.com \
--cc=ajones@ventanamicro.com \
--cc=alex@ghiti.fr \
--cc=andrii@kernel.org \
--cc=andybnac@gmail.com \
--cc=aou@eecs.berkeley.edu \
--cc=apatel@ventanamicro.com \
--cc=arnd@arndb.de \
--cc=ast@kernel.org \
--cc=atishp@rivosinc.com \
--cc=ben.dooks@codethink.co.uk \
--cc=bjorn@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=broonie@kernel.org \
--cc=cl@gentwo.org \
--cc=conor.dooley@microchip.com \
--cc=cyrilbur@tenstorrent.com \
--cc=daniel@iogearbox.net \
--cc=dave@stgolabs.net \
--cc=david.laight.linux@gmail.com \
--cc=debug@rivosinc.com \
--cc=dennis@kernel.org \
--cc=eddyz87@gmail.com \
--cc=emil@etsalapatis.com \
--cc=ihor.solodrai@linux.dev \
--cc=jolsa@kernel.org \
--cc=jszhang@kernel.org \
--cc=justinstitt@google.com \
--cc=linux-arch@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-riscv@lists.infradead.org \
--cc=llvm@lists.linux.dev \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=menglong8.dong@gmail.com \
--cc=morbo@google.com \
--cc=nathan@kernel.org \
--cc=nick.desaulniers+lkml@gmail.com \
--cc=oleg@redhat.com \
--cc=palmer@dabbelt.com \
--cc=pjw@kernel.org \
--cc=pulehui@huawei.com \
--cc=puranjay@kernel.org \
--cc=qingfang.deng@siflower.com.cn \
--cc=rkrcmar@ventanamicro.com \
--cc=samuel.holland@sifive.com \
--cc=seanwascoding@gmail.com \
--cc=song@kernel.org \
--cc=tglx@kernel.org \
--cc=thuth@redhat.com \
--cc=tj@kernel.org \
--cc=wangruikang@iscas.ac.cn \
--cc=yonghong.song@linux.dev \
--cc=zong.li@sifive.com \
/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®