From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.210.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796374802; b=W58kp8DtvZrUwhGXydnU8FfdgGF5MlbCQ2E5MW65ncrEKgc8/vGZaJyakl38PaBLkr/K umVDnKW4WH2LuhA53WinLnY6IndVDnpZB8QUsYkGeA52qP8fau0rNb/+fKCbdPgDJlheH /IGR0zwS9Hfhh6eugr4ngjr8UuE4/2rui7Su1dWkhpAIzT8nQosjC20UnmZevEmPWIKbt oqnYZMGAnsfDmMwbc4j8o79gH7AXX+zw8FMKKcTMNhsWlDpyWtxS04G3gBea40ng49DkO kpiR8AHCrg3/nvp0NB10XhBIcHf+q308yyzxIvuwmq4T0BvNXQ09hoi/U1x2JQWHrZQ== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796374802; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: From:To:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding; bh=f9ipNOFCeCY8lpJ9taZk7NVbHKTAw6+TdJMPxeadMKE=; b=G1yVnsXuIeOBH1HMTKnRj7w+oAxUoBMLuHuuVZrE0t0b9mA0/VdxKJOysZPxzdEun561 GJ/qmET2rSslXhdPqsq7UsKWZKRFwqQQfxPdOjH7Cg5N9jnQvbh87IknTDp4lcRWm/dak midOZvMPW5/ACTwRmZap9eOQLF43AF7LvU/Bp3Yo8Eg+kUjRX4n9ZR+C/PbceUNhWe+V5 1x3BH3FQLI5XFvgCt+5Cu15gBOlrowDJwup+I401Fa6hYn8SlbXPLpm5JL8uGfcjs3ilw aEhwSrqQsQ+D0J8tbJCTXTGAuQ13+vCcuEkN69pMcNNNLbXTK3E/NmN1e/WA2KHlyDQ== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=bytedance.com; spf=pass smtp.mailfrom=bytedance.com; dkim=pass header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=QYI14B6z; arc=none smtp.remote-ip=209.85.210.179 Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9737F606A32F for ; Fri, 4 Dec 2026 09:00:01 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 9737F606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=bytedance.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=bytedance.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=bytedance.com header.i=@bytedance.com header.a=rsa-sha256 header.s=google header.b=QYI14B6z Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-8b3577deeb0so360856b3a.3 for ; Fri, 04 Dec 2026 01:00:01 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=bytedance.com; s=google; t=1796374801; x=1796979601; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:from:to:cc:subject:date:message-id :reply-to:content-type; bh=f9ipNOFCeCY8lpJ9taZk7NVbHKTAw6+TdJMPxeadMKE=; b=QYI14B6zYylh55aTgvG48+U9cAQJmN6T6aqHrNFr6PN0yWx4GqnFm476OIYU2GO6JW Gp3kd4PU4jZL4g49PdgWNs/z6Rj6SyJ40Ko7m6nCqFI8tKjFyDMy+Zkth0XWPCMNCRZN g9G2Q1K7O3TlB3Qa1hOP1vZtLLBPnRS009NfxrTg41b6gqJIqC7N/PfRIKfOo5snKEuD LrXYWd1uBJ2ZGKSDhwa6/n75ADz6EhpsPsCofZryrEwHP/ZsNHNbMS8l97WTcX7KRtZJ gAxyD7kr4ohEWRjvS672gm1qEMG59Gf0k7526P+8QbMhRy1qB89fg95sB7tV7PIaiN/e tNYA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796374801; x=1796979601; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:to:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to:content-type; bh=f9ipNOFCeCY8lpJ9taZk7NVbHKTAw6+TdJMPxeadMKE=; b=dEnVgdcEYkn/v8Eo5la89D6+BvY2j/1dmAaIYOOu5q0+VGQh42FpV6ght0+cvEF6if jynTeMGRpOpDqcLaVIF/x5Elz6n8LCWcZ86+cpqedEu5y5YmYLYdCZrfpZpgk9yJx5ke 7XUoho9/FYzB66DZ0Y8N4VrPNaUkOio2uJNPR+0B7qYwJeyZmj55eRm6Mg1OuupnWfPD nYjFo4NHbJR73SLtvzp9/ycjjvxLgX1yw+pzMh8rJMNb85nyNX3f9Ja4/fr7zvthf4GZ bhbl6Dt3j/yx4Mg9NvNdeShKvMA0xF/jNR07H0Iwjl4InZA/ZqHT2l6DbbL8o7m72L21 M8AA== X-Forwarded-Encrypted: i=1; AL+hErFmePc4JIEwPYkN4Nu8wIOlurguOpgxWhP9+uXA7I7/FkShfxy5RUoRnkiTAM/WYeuG+nTScKstKKFIUD8=@vger.kernel.org X-Gm-Message-State: APuU4Z/FywHg6BNZwlWDJmwxBc007pP0Lq8Ew8QuJuLExtwDSw3B07Z5 38y1SqBkrGb0Ph3LjGCp9/vyCWeae9YYHJupq8hdqlhK5icHsrotjaAESpLrs/kQqV0= X-Gm-Gg: AZFdTZcXRV2eV/HQjC8CndvxR1P+YAgxnbd0h7s2OVdE+J1p+aHOlYg9K+pBeQdQHDr vSF3IVqRLIe41G2Nt1xr0B77U96uOl7yo58r00JbKae/OKVEaNmGOEzED54gO9yy3A/4lvUKiC0 Q2S8clj2ViqRKXhjcpXLExOesqIOjc3m388TPVv4Nv5omiWe+L0q+RLvrymkKG/3F3zbb+o3s8A 5hDZZ6Pu7i7qrPREiiTpUBgHlMcAR00hnZKszZzZqEYQtvOa6So+WpuYhiXHzTDbqThHfKmTe9s lDcjsAZyXvr/V3JdQ9+4NbPzZPLbEoSm/fvuhTsB9jdIsKzgNz/4exwq0hswdnYKpMjYeZLcAV2 bsxMK73LfDrOzTY8+NFRngfZVuVdGeLQSShaVqffmv8V6hAPy3ybn9qlJrclbmx3saQ4SDYtiOR ucOXBff3SzJts5sVXzRTB2Twm6/mfoQ6G127j5Tqn1ljLXpez+YKvVjQoZ0CBD9GJhCy61ELMB6 0xiBgpKf+9UNMlzbX59gnN+9MErQjkwOJvXd8m8GT/XkrhCo/ctS1AKJVd54SHp4izsQFsRzbxy ctsU038ZlAHneMZPY7mU1nNxEp8sKW/dTgcK7kfrNTqXXul1xOQS47H8KVKgKH18OuNMGWv1j2r m2ugpxIfb6QrL X-Received: by 2002:a17:90b:1648:b0:3b3:b0f4:1bf5 with SMTP id 98e67ed59e1d1-3b5e062264cmr1086497a91.25.1796374800673; Fri, 04 Dec 2026 01:00:00 -0800 (PST) Received: from FR9WL0RHKY.bytedance.net ([139.177.225.244]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-3b5c7843862sm8255173a91.2.2026.12.04.00.59.44 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 04 Dec 2026 01:00:00 -0800 (PST) From: Yunhui Cui 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 v12 01/10] riscv: store percpu offset into thread_info Date: Fri, 4 Dec 2026 16:58:49 +0800 Message-Id: <20261204085859.42277-2-cuiyunhui@bytedance.com> X-Mailer: git-send-email 2.39.2 (Apple Git-143) In-Reply-To: <20261204085859.42277-1-cuiyunhui@bytedance.com> References: <20261204085859.42277-1-cuiyunhui@bytedance.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit RISC-V currently derives the current per-CPU offset from the CPU number and __per_cpu_offset[]. Cache it in thread_info so per-CPU accesses can load the offset directly. Initialize the cached offset for the boot and secondary CPUs, and propagate it across context switches. Set it before each secondary CPU startup, including parallel bringup and CPU hotplug. Link: https://lists.riscv.org/g/tech-privileged/topic/risc_v_tech_arch_review/113437553?page=2 Link: https://lore.kernel.org/r/20260826012457.ig3rfmji7vywf6kc@offworld Signed-off-by: Yunhui Cui --- arch/riscv/include/asm/asm.h | 6 +----- arch/riscv/include/asm/percpu.h | 16 ++++++++++++++++ arch/riscv/include/asm/switch_to.h | 9 +++++++++ arch/riscv/include/asm/thread_info.h | 5 ++++- arch/riscv/kernel/asm-offsets.c | 4 +++- arch/riscv/kernel/smpboot.c | 10 +++++++++- arch/riscv/net/bpf_jit_comp64.c | 15 ++------------- 7 files changed, 44 insertions(+), 21 deletions(-) create mode 100644 arch/riscv/include/asm/percpu.h diff --git a/arch/riscv/include/asm/asm.h b/arch/riscv/include/asm/asm.h index b8bf842d4c136..06ac38f733b0a 100644 --- a/arch/riscv/include/asm/asm.h +++ b/arch/riscv/include/asm/asm.h @@ -92,11 +92,7 @@ #ifdef CONFIG_SMP .macro asm_per_cpu dst sym tmp - lw \tmp, TASK_TI_CPU_NUM(tp) - slli \tmp, \tmp, RISCV_LGPTR - la \dst, __per_cpu_offset - add \dst, \dst, \tmp - REG_L \tmp, 0(\dst) + REG_L \tmp, TASK_TI_PCPU_OFFSET(tp) la \dst, \sym add \dst, \dst, \tmp .endm diff --git a/arch/riscv/include/asm/percpu.h b/arch/riscv/include/asm/percpu.h new file mode 100644 index 0000000000000..bbb88a42b48f8 --- /dev/null +++ b/arch/riscv/include/asm/percpu.h @@ -0,0 +1,16 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ + +#ifndef __ASM_PERCPU_H +#define __ASM_PERCPU_H + +#include + +#include + +#ifdef CONFIG_SMP +#define __my_cpu_offset (((struct thread_info *)current)->pcpu_offset) +#endif + +#include + +#endif /* __ASM_PERCPU_H */ diff --git a/arch/riscv/include/asm/switch_to.h b/arch/riscv/include/asm/switch_to.h index 970f89935f0ee..1b11c79847610 100644 --- a/arch/riscv/include/asm/switch_to.h +++ b/arch/riscv/include/asm/switch_to.h @@ -107,6 +107,14 @@ static inline void __switch_to_envcfg(struct task_struct *next) :: "r" (next->thread.envcfg) : "memory"); } +static inline void __switch_to_pcpu_offset(struct task_struct *prev, + struct task_struct *next) +{ +#ifdef CONFIG_SMP + next->thread_info.pcpu_offset = prev->thread_info.pcpu_offset; +#endif +} + extern struct task_struct *__switch_to(struct task_struct *, struct task_struct *); @@ -143,6 +151,7 @@ do { \ if (switch_to_should_flush_icache(__next)) \ local_flush_icache_all(); \ __switch_to_envcfg(__next); \ + __switch_to_pcpu_offset(__prev, __next); \ ((last) = __switch_to(__prev, __next)); \ } while (0) diff --git a/arch/riscv/include/asm/thread_info.h b/arch/riscv/include/asm/thread_info.h index 55019fdfa9eca..523e95c1eedb8 100644 --- a/arch/riscv/include/asm/thread_info.h +++ b/arch/riscv/include/asm/thread_info.h @@ -53,6 +53,7 @@ struct thread_info { unsigned long flags; /* low level flags */ int preempt_count; /* 0=>preemptible, <0=>BUG */ + int cpu; /* * These stack pointers are overwritten on every system call or * exception. SP is also saved to the stack it can be recovered when @@ -60,8 +61,10 @@ struct thread_info { */ long kernel_sp; /* Kernel stack pointer */ long user_sp; /* User stack pointer */ - int cpu; unsigned long syscall_work; /* SYSCALL_WORK_ flags */ +#ifdef CONFIG_SMP + unsigned long pcpu_offset; +#endif #ifdef CONFIG_SHADOW_CALL_STACK void *scs_base; void *scs_sp; diff --git a/arch/riscv/kernel/asm-offsets.c b/arch/riscv/kernel/asm-offsets.c index a75f0cfea1e9f..50fdfe75eaf78 100644 --- a/arch/riscv/kernel/asm-offsets.c +++ b/arch/riscv/kernel/asm-offsets.c @@ -38,6 +38,9 @@ void asm_offsets(void) OFFSET(TASK_THREAD_SUM, task_struct, thread.sum); OFFSET(TASK_TI_CPU, task_struct, thread_info.cpu); +#ifdef CONFIG_SMP + OFFSET(TASK_TI_PCPU_OFFSET, task_struct, thread_info.pcpu_offset); +#endif OFFSET(TASK_TI_PREEMPT_COUNT, task_struct, thread_info.preempt_count); OFFSET(TASK_TI_KERNEL_SP, task_struct, thread_info.kernel_sp); OFFSET(TASK_TI_USER_SP, task_struct, thread_info.user_sp); @@ -50,7 +53,6 @@ void asm_offsets(void) OFFSET(TASK_TI_A2, task_struct, thread_info.a2); #endif - OFFSET(TASK_TI_CPU_NUM, task_struct, thread_info.cpu); #ifdef CONFIG_RISCV_USER_CFI OFFSET(TASK_TI_CFI_STATE, task_struct, thread_info.user_cfi_state); OFFSET(TASK_TI_USER_SSP, task_struct, thread_info.user_cfi_state.user_shdw_stk); diff --git a/arch/riscv/kernel/smpboot.c b/arch/riscv/kernel/smpboot.c index 6436cf1fac067..a40f0c47a238d 100644 --- a/arch/riscv/kernel/smpboot.c +++ b/arch/riscv/kernel/smpboot.c @@ -177,6 +177,10 @@ void __init setup_smp(void) static int start_secondary_cpu(int cpu, struct task_struct *tidle) { + /* The idle task is reused across CPU hotplug. */ + tidle->thread_info.cpu = cpu; + tidle->thread_info.pcpu_offset = per_cpu_offset(cpu); + if (cpu_ops->cpu_start) return cpu_ops->cpu_start(cpu, tidle); @@ -192,7 +196,6 @@ int arch_cpuhp_kick_ap_alive(unsigned int cpu, struct task_struct *tidle) int __cpu_up(unsigned int cpu, struct task_struct *tidle) { int ret; - tidle->thread_info.cpu = cpu; ret = start_secondary_cpu(cpu, tidle); if (!ret) { @@ -210,6 +213,11 @@ int __cpu_up(unsigned int cpu, struct task_struct *tidle) } #endif +void __init smp_prepare_boot_cpu(void) +{ + __my_cpu_offset = per_cpu_offset(smp_processor_id()); +} + void __init smp_cpus_done(unsigned int max_cpus) { } diff --git a/arch/riscv/net/bpf_jit_comp64.c b/arch/riscv/net/bpf_jit_comp64.c index 7d15617aee1ba..b7b2429f31be4 100644 --- a/arch/riscv/net/bpf_jit_comp64.c +++ b/arch/riscv/net/bpf_jit_comp64.c @@ -12,7 +12,6 @@ #include #include #include -#include #include "bpf_jit.h" /* @@ -1553,15 +1552,8 @@ int bpf_jit_emit_insn(const struct bpf_insn *insn, struct rv_jit_context *ctx, if (rd != rs) emit_mv(rd, rs, ctx); #ifdef CONFIG_SMP - /* Load current CPU number in T1 */ - emit_lw(RV_REG_T1, offsetof(struct thread_info, cpu), + emit_ld(RV_REG_T1, offsetof(struct thread_info, pcpu_offset), RV_REG_TP, ctx); - /* Load address of __per_cpu_offset array in T2 */ - emit_addr(RV_REG_T2, (u64)&__per_cpu_offset, extra_pass, ctx); - /* Get address of __per_cpu_offset[cpu] in T1 */ - emit_sh3add(RV_REG_T1, RV_REG_T1, RV_REG_T2, ctx); - /* Load __per_cpu_offset[cpu] in T1 */ - emit_ld(RV_REG_T1, 0, RV_REG_T1, ctx); /* Add the offset to Rd */ emit_add(rd, rd, RV_REG_T1, ctx); #endif @@ -2228,10 +2220,7 @@ static void emit_priv_stack_frame(struct rv_jit_context *ctx) fp += round_up(ctx->prog->aux->stack_depth, STACK_ALIGN); emit_imm(RV_REG_S5, fp, ctx); #ifdef CONFIG_SMP - emit_lw(RV_REG_T1, offsetof(struct thread_info, cpu), RV_REG_TP, ctx); - emit_imm(RV_REG_T2, (u64)__per_cpu_offset, ctx); - emit_sh3add(RV_REG_T1, RV_REG_T1, RV_REG_T2, ctx); - emit_ld(RV_REG_T1, 0, RV_REG_T1, ctx); + emit_ld(RV_REG_T1, offsetof(struct thread_info, pcpu_offset), RV_REG_TP, ctx); emit_add(RV_REG_S5, RV_REG_S5, RV_REG_T1, ctx); #endif } -- 2.39.5