From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796308428; b=TL6yMC2Fpn5l8Mt4aT/i9hD4rSWYJ3aaAkIW4G7eXLUeZngpOBDLcC60jaK9+VWV/i2d CivMAYptE0mjp8g6k6X2T4jzERQxjmT1Lhtb5ji69+dKAXTz86r94mtjxGd1xpu6wf1ET 7lE98PfGDIh+cPj1BZveNgohXpUOd5WYxmgFlR5OfUkoFRl8rh9G5uObNbYtPNXC4hxCr aR93srItrtmfF9z4kkkgj7WUzTgBq2FBWgYUL0LNWVvVLNCrn6YjVN15ffVGypjeQdiiU tpNLt6WMp8gVtgjY3CTumT6mXUImCFQbCMB6gug3EO6bGgx9Yb7IPxMhExfaqxSKdFQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796308428; h=Received:DKIM-Signature:Message-ID:From:Subject:Reply-To:To: In-Reply-To:References:Content-Type:Content-Transfer-Encoding:Date; bh=y85dfMMGgjfH8LA91KrLsbkbkOiNS66urmsMRj02x+U=; b=dp1wf/ePbejv7raaRuy8RyyncNV/NzulDO3D2ulaARU1yyU9V1B8X8DG5OGwl6x1GOpF u42oYX5D6/foooKFUnWjd4n8t1+ED0VkM4LrQsRhVSnOagHB9/jLB8vBOEHNPwnyn/u7f NAmHLiPo4Af74Ro7Zm6gd5h4rdLK4evHlXubn9GuW2oxD/9BH8JoR0j0pt6qaPvm6o1D4 xqnQOjkxHNkbcqJ6r/PgWiKANTNnKmRvFNP39GFM65yuj3AACNNKPcI+mx3SHJDtjg2lB rKigJUplChnOftdSrD3JXIkGt9bBs6O8HOqxjJAW0DNq5jxqhqVR2D0oYdbRd1b5cDQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=cXPxCFml; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7D62F606A322 for ; Thu, 3 Dec 2026 14:33:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=cXPxCFml Received: by smtp.kernel.org (Postfix) with ESMTPSA id D4B2E1F000C1; Thu, 3 Dec 2026 14:33:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796308428; bh=y85dfMMGgjfH8LA91KrLsbkbkOiNS66urmsMRj02x+U=; h=From:Subject:Reply-To:To:In-Reply-To:References:Date; b=cXPxCFmlsdvtVHf1KRrJt+Dt/Spv1LaD9O1IkGIL6cliE6ASNJinnZhmF91u4phO8 xJLXC1/y5RrcKYYzh0n40RW1YJSrfCoEcvWfpgIUCnSIFx3z/xoi4WxZ9D8rTKsCG8 VAP8SmoRI++svko61Z5Qh9uxr1oWCLVtoo27oWFjw+qPvFWZJQb5yaJYmTUZ3I6AED ZcGOzROaXqY5mcraYr7gH9mdegkj88qD89erkKVdjHYrBmP0ivDc1oDOjQ+0+a5CBa 7iZntWuGFahOvHtoAEMWen9lpY59z+o0YWmXwI8wWhUDsJMY5O+MQLpmaG/TWsEyA8 Z5UYmRn7aHBcg== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [PATCH v11 02/10] riscv: add infrastructure for preemptible this_cpu operations Reply-To: sashiko-reviews@lists.linux.dev To: "Yunhui Cui" , 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 In-Reply-To: <20261203135328.32990-3-cuiyunhui@bytedance.com> References: <20261203135328.32990-1-cuiyunhui@bytedance.com> <20261203135328.32990-3-cuiyunhui@bytedance.com> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 14:33:45 +0000 X-Mailing-List: bpf@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list 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=3Dn - [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 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/per= cpu.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/ptr= ace.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=3Dn 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/as= m/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-c= ommon.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 > +#include > +#include > + > +#include > +#include > + > +/* > + * If the interrupted context had an active per-CPU operation, rewrite i= ts > + * saved offset and address registers for the CPU on which it will resum= e. > + */ > +void noinstr riscv_pcpu_regs_fixup(struct pt_regs *regs) > +{ > + unsigned long pcpu_offset =3D __my_cpu_offset; > + unsigned long gprs =3D regs->pcpu_gprs; > + unsigned long pcp; > + unsigned int reg_pcp; > + unsigned int reg_off; > + unsigned int reg_addr; > + > + reg_pcp =3D FIELD_GET(PCPU_GPR_PCP, gprs); > + reg_off =3D FIELD_GET(PCPU_GPR_OFF, gprs); > + reg_addr =3D FIELD_GET(PCPU_GPR_ADDR, gprs); > + > + pcp =3D 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) > =20 > + 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. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261203135328.3299= 0-1-cuiyunhui@bytedance.com?part=3D2