From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4716F606A322 for ; Thu, 3 Dec 2026 14:33:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=cXPxCFml Received: by kanga.kvack.org (Postfix) id 532896B05B3; Thu, 3 Dec 2026 09:33:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E3986B05B9; Thu, 3 Dec 2026 09:33:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3F8C06B05D8; Thu, 3 Dec 2026 09:33:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1A0EA6B05B3 for ; Thu, 3 Dec 2026 09:33:51 -0500 (EST) Received: from smtpin26.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay03.hostedemail.com (Postfix) with ESMTP id B3377A0736 for ; Thu, 3 Dec 2026 14:33:50 +0000 (UTC) X-FDA: 85502915820.26.B26608E Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by imf23.hostedemail.com (Postfix) with ESMTP id 1284F140014 for ; Thu, 3 Dec 2026 14:33:48 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=cXPxCFml; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf23.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796308429; b=OxW3EBsNilbK02V1yp9VIqt+14ewBbXLLjrvUNeCVC3WcMhhj5QDNekida6d5Rqt1kPQan 9nOc8rm03b+tWGqdkseggbj1JCUvnnJxpAvFeHTwJsAd21hsUD9TYmRum2orfgxIzXsyvR Sf40Omm7Mt3jaWIHZAtlPwEasG7KfEw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796308429; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=y85dfMMGgjfH8LA91KrLsbkbkOiNS66urmsMRj02x+U=; b=wXH4+eeyECipl5wHOAXcEOUvokNCfnAI9j+tGprPE1b0oRxSpwfeEN8MeyXYKIswwZ7/DL uN81ERtvdWz3nKWq59b2HbkwT77NHHRSx8Ujo4xujGgTQuOsUUNGn9qQVeRiaaTRXzCHM8 gnqE9uktOTS4AfAfle/T6qN/2/1Ipxk= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=cXPxCFml; dmarc=pass (policy=quarantine) header.from=kernel.org; spf=pass (imf23.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.234.252.31 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 340F240830; Thu, 3 Dec 2026 14:33:48 +0000 (UTC) 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@gma il.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-Rspam-User: X-Stat-Signature: qz65g74h3rheiu3bqbc1ey88nykuxswy X-Rspamd-Server: rspam11 X-Rspamd-Queue-Id: 1284F140014 X-Spam-Status: No, score=-8.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-HE-Tag: 1796308428-373193 X-HE-Meta: U2FsdGVkX1/gnLlkVBGJJGuArNNpdewQE2GOZ0TgsGpD1u6fMpGtByx49vQoJMAq7Ecze8C08vUlrXtmExKzjO3ELYEjSkFLp7m1jgqo4GGjgnFEU3DKdU+oTY8OmPKSUpekNzbayJnA62BMGfQb0XuKiz5KB3w+JMD9mxaNgF1JQIKAT4kP/ODmP12jJf0lMLLdVV6IZAcKJxbJYgK/gy9hFTVzatwxMJQKRuZn9NtwfSH5X1lpd0RcSIJmrcFof+YvZ26RvgjV5xpo3TIbBWRJ8sv59y3pS9Qh4vaqc09IUBmHOuw1RUQ4LpmDRad2xZYxRkfVImGZKeq7tHmfnk6msDnjaVpbAoV/zFz/U/Sv91EwytbC4hpn/4LaepVrxxzJTSpubO+96H+7TQ6qI45wyoqEJB531R/iMcrD84Yu+VIAGAaLbwibuwqwGpQ4xfLyeG1ClTNkGXJlZABgcJOkvqj6dF9OkafH8FP3k+tZ/zf1dCPbgyB/JN/PiNTIYkX1z18XU/PAbCdozlTnPHpWZXO4xcdLS3LICdV3ZHBGrcmkPbN3KB0AEYpY1Gwwb1CMG5H0Uu/iRsjOMH21oCeRqVvts9kO31RpvukQsJPBlWzLVZxMlLMW3U23/cdkyIc+rH40LpAnHlPprMj1RDA2Ohz0BNeSEUcrHa+ziqkORfDy++YMbLkm3RWv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ejrkRR3gL5Qw5dZYTbUGGZ2TC4xmlFy5mmi3ArMYv/5OgYWbnQoIW743o23WuaA+YV//bsDdsKaTZa+1HzwWOCb0UwsSpj1fj6fvbq7IJOr0P72G3wkfFqb+cbpW5gS6vou2uyaBZzcF2x4TFHNQVcMPc8y9BTZyoTFrA33Le69yqo1IK2chy5f68Pui FORbozb7 vRn5qY0niYuCKsJFNptG+g== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: 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