From: sashiko-bot@kernel.org
To: "Yunhui Cui" <cuiyunhui@bytedance.com>,
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, 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-mm@kvack.org,
bpf@vger.kernel.org
Subject: Re: [PATCH v11 02/10] riscv: add infrastructure for preemptible this_cpu operations
Date: Thu, 03 Dec 2026 14:33:45 +0000 [thread overview]
Message-ID: <sashiko-outbox-247978@kernel.org> (raw)
In-Reply-To: <20261203135328.32990-3-cuiyunhui@bytedance.com>
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [Medium] riscv: noinstr violation in riscv_pcpu_regs_fixup() and
entry-common.o
- [Medium] riscv: thread_info.pcpu_gprs padding pushes scs_sp across
cacheline
- [Medium] riscv: unconditional pcpu_gprs migration fixup overhead on
CONFIG_SMP=n
- [Low] riscv: asymmetric gprs operand syntax in __PCPU_GPRS_BEGIN/END
macros
- [Low] riscv: redundant pcpu_gprs_exit store in ret_from_exception when
zero
- [Low] riscv: handle_kernel_stack_overflow() leaves stale
thread_info.pcpu_gprs
--
commit 072f5f618ab3d6a5823cb33debcecd13e832daaa
Author: Yunhui Cui <cuiyunhui@bytedance.com>
riscv: add infrastructure for preemptible this_cpu operations
This commit adds per-CPU register tracking in thread_info and pt_regs,
inline assembly helpers to delimit preemptible per-CPU regions, and an
exception-return fixup routine to recompute per-CPU offset and address
registers after CPU migration.
Link: https://lore.kernel.org/r/20261108213254.67805ef0@pumpkin
Link: https://lore.kernel.org/r/avG07pmpcLGkq5i-@xhacker
[ ... ]
> diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h
> index faf9a75ec66d7..3cd60ef18815e 100644
> --- a/arch/riscv/include/asm/percpu.h
> +++ b/arch/riscv/include/asm/percpu.h
> @@ -3,12 +3,58 @@
[ ... ]
> +/*
> + * Mark a region where the per-CPU offset and address registers may need to
> + * be rewritten by exception return after migration. The three registers
> + * must remain distinct for the whole region.
> + */
> +#define __PCPU_GPRS_BEGIN(gprs, pcp, off, addr, pcpu_offset) \
> + __DEFINE_ASM_GPR_NUMS \
> + __PCPU_GPRS_ASSERT_DISTINCT(pcp, off, addr) \
> + "li %[" #off "], " __PCPU_GPRS_VALUE "\n"\
> + "sh %[" #off "], %[" #gprs "]\n" \
> + REG_L " %[" #off "], %[" #pcpu_offset "]\n"\
> + "add %[" #addr "], %[" #pcp "], %[" #off "]\n"
> +
> +#define __PCPU_GPRS_END(gprs) \
> + "sh zero, " gprs "\n"
[Severity: Low]
Should __PCPU_GPRS_END(gprs) format its operand the same way as
__PCPU_GPRS_BEGIN()?
__PCPU_GPRS_BEGIN() takes an unquoted operand name and wraps it in %[#gprs],
while __PCPU_GPRS_END() concatenates gprs as a string literal, forcing
callers to pass gprs to one and "%[gprs]" to the other. __PCPU_GPR_NUM()
also uses __stringify() while the asm lines use raw #.
Using "%[" __stringify(gprs) "]" in __PCPU_GPRS_END() and __stringify()
throughout __PCPU_GPRS_BEGIN() keeps both macros symmetric. It would also
help to note in the comment that pcp must stay unmodified and off/addr
cannot hold live outputs before __PCPU_GPRS_END().
> diff --git a/arch/riscv/include/asm/ptrace.h b/arch/riscv/include/asm/ptrace.h
> index addc8188152f7..355818c709b36 100644
> --- a/arch/riscv/include/asm/ptrace.h
> +++ b/arch/riscv/include/asm/ptrace.h
> @@ -53,6 +53,8 @@ struct pt_regs {
> unsigned long cause;
> /* a0 value before the syscall */
> unsigned long orig_a0;
> + /* Active preemptible per-CPU operation, if any. */
> + unsigned long pcpu_gprs;
> };
[Severity: Medium]
Should this migration-fixup infrastructure be gated on CONFIG_SMP?
On CONFIG_SMP=n kernels tasks cannot migrate and per-CPU offsets are
always zero, yet pcpu_gprs is unconditionally added to struct pt_regs and
struct thread_info, entry-common.o is built unconditionally, and
handle_exception()/ret_from_exception() save and restore it on every trap.
Adding pcpu_gprs pushes struct pt_regs past a 16-byte boundary, growing
PT_SIZE_ON_STACK by 16 bytes on every trap frame (to 304 bytes on RV64 and
160 on RV32). __PCPU_GPRS_BEGIN()/__PCPU_GPRS_END() also add extra loads,
stores, and fixup calls on every this_cpu operation to recompute pcp + 0.
[ ... ]
> diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h
> index f10ba62b61016..51c4076763c1b 100644
> --- a/arch/riscv/include/asm/thread_info.h
> +++ b/arch/riscv/include/asm/thread_info.h
> @@ -63,6 +63,7 @@ struct thread_info {
> long user_sp; /* User stack pointer */
> unsigned long syscall_work; /* SYSCALL_WORK_ flags */
> unsigned long pcpu_offset;
> + unsigned short pcpu_gprs;
> #ifdef CONFIG_SHADOW_CALL_STACK
> void *scs_base;
> void *scs_sp;
[Severity: Medium]
Does placing 2-byte pcpu_gprs before scs_base push scs_sp into a second
cache line on RV64?
struct task_struct (with thread_info at offset 0) is aligned to
L1_CACHE_BYTES (64 bytes). Before this patch, flags through scs_sp fit
within the first 64 bytes; inserting unsigned short pcpu_gprs at offset 48
adds 6 bytes of alignment padding and shifts hot scs_sp to offset 64.
Because handle_exception(), ret_from_exception(), and __switch_to() access
TASK_TI_SCS_SP(tp), every syscall, IRQ, and context switch under
CONFIG_SHADOW_CALL_STACK will touch two cache lines instead of one.
Putting scs_sp before cold scs_base and placing pcpu_gprs after scs_sp
keeps all hot entry fields in the first cache line.
[ ... ]
> diff --git a/arch/riscv/kernel/entry-common.c b/arch/riscv/kernel/entry-common.c
> new file mode 100644
> index 0000000000000..164505962d131
> --- /dev/null
> +++ b/arch/riscv/kernel/entry-common.c
> @@ -0,0 +1,30 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +
> +#include <linux/bitfield.h>
> +#include <linux/compiler.h>
> +#include <linux/percpu.h>
> +
> +#include <asm/current.h>
> +#include <asm/ptrace.h>
> +
> +/*
> + * If the interrupted context had an active per-CPU operation, rewrite its
> + * saved offset and address registers for the CPU on which it will resume.
> + */
> +void noinstr riscv_pcpu_regs_fixup(struct pt_regs *regs)
> +{
> + unsigned long pcpu_offset = __my_cpu_offset;
> + unsigned long gprs = regs->pcpu_gprs;
> + unsigned long pcp;
> + unsigned int reg_pcp;
> + unsigned int reg_off;
> + unsigned int reg_addr;
> +
> + reg_pcp = FIELD_GET(PCPU_GPR_PCP, gprs);
> + reg_off = FIELD_GET(PCPU_GPR_OFF, gprs);
> + reg_addr = FIELD_GET(PCPU_GPR_ADDR, gprs);
> +
> + pcp = regs_get_gpr(regs, reg_pcp * sizeof(unsigned long));
> + regs_set_gpr(regs, reg_off * sizeof(unsigned long), pcpu_offset);
> + regs_set_gpr(regs, reg_addr * sizeof(unsigned long), pcp + pcpu_offset);
> +}
[Severity: Medium]
Can riscv_pcpu_regs_fixup() call non-noinstr instrumentation from
ret_from_exception?
ret_from_exception invokes this helper after irqentry_exit(), outside RCU
watching. regs_get_gpr() and regs_set_gpr() use unlikely() instead of
unlikely_notrace(), so CONFIG_TRACE_BRANCH_PROFILING emits calls to
ftrace_likely_update(), which can tamper with SR_SUM or enter the tracer.
arch/riscv/kernel/Makefile also builds entry-common.o without
-DDISABLE_BRANCH_PROFILING or KCOV, UBSAN, and KASAN opt-outs, leaving
riscv_pcpu_regs_fixup() open to UBSAN and KCOV (on GCC < 12) calls.
Using unlikely_notrace() in regs_get_gpr()/regs_set_gpr() and disabling
branch profiling, KCOV, UBSAN, and KASAN for entry-common.o avoids this.
> diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S
> index 628b85e3915a7..65298ac828959 100644
> --- a/arch/riscv/kernel/entry.S
> +++ b/arch/riscv/kernel/entry.S
> @@ -198,6 +198,8 @@ SYM_CODE_START(handle_exception)
> REG_S s4, PT_CAUSE(sp)
> REG_S s5, PT_TP(sp)
>
> + pcpu_gprs_entry tp sp s0
> +
> /*
> * Set the scratch register to 0, so that if a recursive exception
> * occurs, the exception vector knows it came from the kernel
[Severity: Low]
Does handle_kernel_stack_overflow() leave a stale thread_info.pcpu_gprs
value active?
Under CONFIG_VMAP_STACK, handle_exception branches to
handle_kernel_stack_overflow before reaching pcpu_gprs_entry, and
handle_kernel_stack_overflow tail-calls handle_bad_stack without calling
pcpu_gprs_entry or clearing TASK_TI_PCPU_GPRS(tp).
If a stack overflow hits inside a __PCPU_GPRS_BEGIN/__PCPU_GPRS_END region
and a nested trap occurs during handle_bad_stack() or panic(),
ret_from_exception will run riscv_pcpu_regs_fixup() on the stale descriptor
and clobber two unrelated GPRs in the panic path.
> @@ -281,6 +283,14 @@ SYM_CODE_START_NOALIGN(ret_from_exception)
> move a0, sp
> call riscv_v_context_nesting_end
> #endif
> + REG_L t0, PT_PCPU_GPRS(sp)
> + beqz t0, .Lpcpu_regs_restored
> + move a0, sp
> + call riscv_pcpu_regs_fixup
> + REG_L t0, PT_PCPU_GPRS(sp)
> +.Lpcpu_regs_restored:
> + pcpu_gprs_exit tp t0
> +
> REG_L a0, PT_STATUS(sp)
> restore_userssp s3, a0
> /*
[Severity: Low]
Is the pcpu_gprs_exit store redundant when PT_PCPU_GPRS(sp) is zero?
Every path into ret_from_exception already has TASK_TI_PCPU_GPRS(tp)
cleared, so jumping above pcpu_gprs_exit executes an unnecessary sh zero
store on every syscall and exception return. Moving pcpu_gprs_exit before
.Lpcpu_regs_restored avoids that store on the fast path.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261203135328.32990-1-cuiyunhui@bytedance.com?part=2
next prev parent reply other threads:[~2026-12-03 14:33 UTC|newest]
Thread overview: 16+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-12-03 13:53 [PATCH v11 00/10] riscv: optimize percpu access Yunhui Cui
2026-12-03 13:53 ` [PATCH v11 01/10] riscv: store percpu offset into thread_info Yunhui Cui
2026-12-03 14:20 ` sashiko-bot
2026-12-03 13:53 ` [PATCH v11 02/10] riscv: add infrastructure for preemptible this_cpu operations Yunhui Cui
2026-12-03 14:33 ` sashiko-bot [this message]
2026-12-03 13:53 ` [PATCH v11 03/10] riscv: add common primitives " Yunhui Cui
2026-12-03 14:19 ` sashiko-bot
2026-12-03 13:53 ` [PATCH v11 04/10] riscv: implement preemptible this_cpu read and write operations Yunhui Cui
2026-12-03 14:23 ` sashiko-bot
2026-12-03 13:53 ` [PATCH v11 05/10] riscv: implement preemptible this_cpu void RMW operations Yunhui Cui
2026-12-03 14:18 ` sashiko-bot
2026-12-03 13:53 ` [PATCH v11 06/10] riscv: implement preemptible this_cpu add_return operations Yunhui Cui
2026-12-03 13:53 ` [PATCH v11 07/10] riscv: implement preemptible this_cpu xchg operations Yunhui Cui
2026-12-03 13:53 ` [PATCH v11 08/10] riscv: implement preemptible this_cpu cmpxchg operations Yunhui Cui
2026-12-03 13:53 ` [PATCH v11 09/10] riscv: implement preemptible this_cpu cmpxchg128 Yunhui Cui
2026-12-03 13:53 ` [PATCH v11 10/10] riscv: select HAVE_CMPXCHG_LOCAL Yunhui Cui
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=sashiko-outbox-247978@kernel.org \
--to=sashiko-bot@kernel.org \
--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=cuiyunhui@bytedance.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-mm@kvack.org \
--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=sashiko-reviews@lists.linux.dev \
--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