From nobody Thu Apr 2 21:32:08 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 510F7C6FA91 for ; Tue, 20 Sep 2022 15:16:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231314AbiITPQM (ORCPT ); Tue, 20 Sep 2022 11:16:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231346AbiITPPv (ORCPT ); Tue, 20 Sep 2022 11:15:51 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9872C5EDF0; Tue, 20 Sep 2022 08:15:48 -0700 (PDT) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MX4kJ6pmyzMnFp; Tue, 20 Sep 2022 23:11:04 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500020.china.huawei.com (7.185.36.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:45 +0800 Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:45 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , , , , , , , , , , , , Subject: [PATCH -next 1/7] riscv: stacktrace: Replace walk_stackframe with arch_stack_walk Date: Tue, 20 Sep 2022 23:11:56 +0800 Message-ID: <20220920151202.180057-2-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220920151202.180057-1-chenzhongjin@huawei.com> References: <20220920151202.180057-1-chenzhongjin@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" walk_stackframe can be all replaced by arch_stack_walk. Since walk_stackframe is only called by arch_stack_walk and their only difference is the argument sequence. Signed-off-by: Chen Zhongjin --- arch/riscv/include/asm/stacktrace.h | 2 -- arch/riscv/kernel/perf_callchain.c | 2 +- arch/riscv/kernel/stacktrace.c | 29 +++++++++++++---------------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/arch/riscv/include/asm/stacktrace.h b/arch/riscv/include/asm/s= tacktrace.h index 3450c1912afd..b6cd3eddfd38 100644 --- a/arch/riscv/include/asm/stacktrace.h +++ b/arch/riscv/include/asm/stacktrace.h @@ -11,8 +11,6 @@ struct stackframe { unsigned long ra; }; =20 -extern void notrace walk_stackframe(struct task_struct *task, struct pt_re= gs *regs, - bool (*fn)(void *, unsigned long), void *arg); extern void dump_backtrace(struct pt_regs *regs, struct task_struct *task, const char *loglvl); =20 diff --git a/arch/riscv/kernel/perf_callchain.c b/arch/riscv/kernel/perf_ca= llchain.c index 3348a61de7d9..c023e0b1eb81 100644 --- a/arch/riscv/kernel/perf_callchain.c +++ b/arch/riscv/kernel/perf_callchain.c @@ -74,5 +74,5 @@ static bool fill_callchain(void *entry, unsigned long pc) void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { - walk_stackframe(NULL, regs, fill_callchain, entry); + arch_stack_walk(fill_callchain, entry, NULL, regs); } diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c index 08d11a53f39e..b51e32d50a0e 100644 --- a/arch/riscv/kernel/stacktrace.c +++ b/arch/riscv/kernel/stacktrace.c @@ -16,8 +16,9 @@ =20 #ifdef CONFIG_FRAME_POINTER =20 -void notrace walk_stackframe(struct task_struct *task, struct pt_regs *reg= s, - bool (*fn)(void *, unsigned long), void *arg) +noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, + void *cookie, struct task_struct *task, + struct pt_regs *regs) { unsigned long fp, sp, pc; int level =3D 0; @@ -29,7 +30,7 @@ void notrace walk_stackframe(struct task_struct *task, st= ruct pt_regs *regs, } else if (task =3D=3D NULL || task =3D=3D current) { fp =3D (unsigned long)__builtin_frame_address(0); sp =3D current_stack_pointer; - pc =3D (unsigned long)walk_stackframe; + pc =3D (unsigned long)arch_stack_walk; } else { /* task blocked in __switch_to */ fp =3D task->thread.s[0]; @@ -41,7 +42,8 @@ void notrace walk_stackframe(struct task_struct *task, st= ruct pt_regs *regs, unsigned long low, high; struct stackframe *frame; =20 - if (unlikely(!__kernel_text_address(pc) || (level++ >=3D 1 && !fn(arg, p= c)))) + if (unlikely(!__kernel_text_address(pc) || + (level++ >=3D 1 && !consume_entry(cookie, pc)))) break; =20 /* Validate frame pointer */ @@ -66,8 +68,9 @@ void notrace walk_stackframe(struct task_struct *task, st= ruct pt_regs *regs, =20 #else /* !CONFIG_FRAME_POINTER */ =20 -void notrace walk_stackframe(struct task_struct *task, - struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg) +noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, + void *cookie, struct task_struct *task, + struct pt_regs *regs) { unsigned long sp, pc; unsigned long *ksp; @@ -77,7 +80,7 @@ void notrace walk_stackframe(struct task_struct *task, pc =3D instruction_pointer(regs); } else if (task =3D=3D NULL || task =3D=3D current) { sp =3D current_stack_pointer; - pc =3D (unsigned long)walk_stackframe; + pc =3D (unsigned long)arch_stack_walk; } else { /* task blocked in __switch_to */ sp =3D task->thread.sp; @@ -89,7 +92,7 @@ void notrace walk_stackframe(struct task_struct *task, =20 ksp =3D (unsigned long *)sp; while (!kstack_end(ksp)) { - if (__kernel_text_address(pc) && unlikely(!fn(arg, pc))) + if (__kernel_text_address(pc) && unlikely(!consume_entry(cookie, pc))) break; pc =3D (*ksp++) - 0x4; } @@ -108,7 +111,7 @@ static bool print_trace_address(void *arg, unsigned lon= g pc) noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *tas= k, const char *loglvl) { - walk_stackframe(task, regs, print_trace_address, (void *)loglvl); + arch_stack_walk(print_trace_address, (void *)loglvl, task, regs); } =20 void show_stack(struct task_struct *task, unsigned long *sp, const char *l= oglvl) @@ -133,13 +136,7 @@ unsigned long __get_wchan(struct task_struct *task) =20 if (!try_get_task_stack(task)) return 0; - walk_stackframe(task, NULL, save_wchan, &pc); + arch_stack_walk(save_wchan, &pc, task, NULL); put_task_stack(task); return pc; } - -noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, void *= cookie, - struct task_struct *task, struct pt_regs *regs) -{ - walk_stackframe(task, regs, consume_entry, cookie); -} --=20 2.17.1 From nobody Thu Apr 2 21:32:08 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7EB93ECAAD8 for ; Tue, 20 Sep 2022 15:16:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231637AbiITPQc (ORCPT ); Tue, 20 Sep 2022 11:16:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231375AbiITPPv (ORCPT ); Tue, 20 Sep 2022 11:15:51 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4784E61D4D; Tue, 20 Sep 2022 08:15:49 -0700 (PDT) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MX4mV1fgRzpTJ7; Tue, 20 Sep 2022 23:12:58 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500022.china.huawei.com (7.185.36.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:46 +0800 Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:45 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , , , , , , , , , , , , Subject: [PATCH -next 2/7] riscv: stacktrace: Introduce unwind functions Date: Tue, 20 Sep 2022 23:11:57 +0800 Message-ID: <20220920151202.180057-3-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220920151202.180057-1-chenzhongjin@huawei.com> References: <20220920151202.180057-1-chenzhongjin@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Now all riscv unwinding code is inside arch_stack_walk. It's not same as other architectures. Make some refactoring, to move unwinding code into unwind() and unwind_next() functions, which walks through all stack frames or single frame. This patch only moves code but doesn't make any logical change. Signed-off-by: Chen Zhongjin --- arch/riscv/include/asm/stacktrace.h | 7 ++ arch/riscv/kernel/stacktrace.c | 104 ++++++++++++++++++---------- 2 files changed, 74 insertions(+), 37 deletions(-) diff --git a/arch/riscv/include/asm/stacktrace.h b/arch/riscv/include/asm/s= tacktrace.h index b6cd3eddfd38..a39e4ef1dbd5 100644 --- a/arch/riscv/include/asm/stacktrace.h +++ b/arch/riscv/include/asm/stacktrace.h @@ -11,6 +11,13 @@ struct stackframe { unsigned long ra; }; =20 +struct unwind_state { + unsigned long fp; + unsigned long sp; + unsigned long pc; + struct pt_regs *regs; +}; + extern void dump_backtrace(struct pt_regs *regs, struct task_struct *task, const char *loglvl); =20 diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c index b51e32d50a0e..e84e21868a3e 100644 --- a/arch/riscv/kernel/stacktrace.c +++ b/arch/riscv/kernel/stacktrace.c @@ -16,54 +16,84 @@ =20 #ifdef CONFIG_FRAME_POINTER =20 -noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, - void *cookie, struct task_struct *task, - struct pt_regs *regs) +static int notrace unwind_next(struct unwind_state *state) { - unsigned long fp, sp, pc; - int level =3D 0; + unsigned long low, high, fp; + struct stackframe *frame; =20 - if (regs) { - fp =3D frame_pointer(regs); - sp =3D user_stack_pointer(regs); - pc =3D instruction_pointer(regs); - } else if (task =3D=3D NULL || task =3D=3D current) { - fp =3D (unsigned long)__builtin_frame_address(0); - sp =3D current_stack_pointer; - pc =3D (unsigned long)arch_stack_walk; + fp =3D state->fp; + + /* Validate frame pointer */ + low =3D state->sp + sizeof(struct stackframe); + high =3D ALIGN(low, THREAD_SIZE); + + if (fp < low || fp > high || fp & 0x7) + return -EINVAL; + + /* Unwind stack frame */ + frame =3D (struct stackframe *)fp - 1; + state->sp =3D fp; + + if (state->regs && state->regs->epc =3D=3D state->pc && + fp & 0x7) { + state->fp =3D frame->ra; + state->pc =3D state->regs->ra; } else { - /* task blocked in __switch_to */ - fp =3D task->thread.s[0]; - sp =3D task->thread.sp; - pc =3D task->thread.ra; + state->fp =3D frame->fp; + state->pc =3D ftrace_graph_ret_addr(current, NULL, frame->ra, + (unsigned long *)fp - 1); } =20 - for (;;) { - unsigned long low, high; - struct stackframe *frame; + return 0; +} =20 - if (unlikely(!__kernel_text_address(pc) || - (level++ >=3D 1 && !consume_entry(cookie, pc)))) +static void notrace unwind(struct unwind_state *state, + stack_trace_consume_fn consume_entry, void *cookie) +{ + while (1) { + int ret; + + if (!__kernel_text_address(state->pc)) + break; + + if (!consume_entry(cookie, state->pc)) break; =20 - /* Validate frame pointer */ - low =3D sp + sizeof(struct stackframe); - high =3D ALIGN(sp, THREAD_SIZE); - if (unlikely(fp < low || fp > high || fp & 0x7)) + ret =3D unwind_next(state); + if (ret < 0) break; - /* Unwind stack frame */ - frame =3D (struct stackframe *)fp - 1; - sp =3D fp; - if (regs && (regs->epc =3D=3D pc) && (frame->fp & 0x7)) { - fp =3D frame->ra; - pc =3D regs->ra; - } else { - fp =3D frame->fp; - pc =3D ftrace_graph_ret_addr(current, NULL, frame->ra, - (unsigned long *)(fp - 8)); - } + } +} + +noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, + void *cookie, struct task_struct *task, + struct pt_regs *regs) +{ + struct unwind_state state; + + if (task =3D=3D NULL) + task =3D current; =20 + if (regs) { + state.fp =3D frame_pointer(regs); + state.sp =3D user_stack_pointer(regs); + state.pc =3D instruction_pointer(regs); + state.regs =3D regs; + } else if (task =3D=3D current) { + state.fp =3D (unsigned long)__builtin_frame_address(0); + state.sp =3D current_stack_pointer; + state.pc =3D (unsigned long)arch_stack_walk; + + /* skip frame of arch_stack_walk */ + unwind_next(&state); + } else { + /* task blocked in __switch_to */ + state.fp =3D task->thread.s[0]; + state.sp =3D task->thread.sp; + state.pc =3D task->thread.ra; } + + unwind(&state, consume_entry, cookie); } =20 #else /* !CONFIG_FRAME_POINTER */ --=20 2.17.1 From nobody Thu Apr 2 21:32:08 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53BAFC6FA90 for ; Tue, 20 Sep 2022 15:16:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229984AbiITPQP (ORCPT ); Tue, 20 Sep 2022 11:16:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32884 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231363AbiITPPv (ORCPT ); Tue, 20 Sep 2022 11:15:51 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 31718606A7; Tue, 20 Sep 2022 08:15:49 -0700 (PDT) Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.57]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MX4mV3FPczpTJm; Tue, 20 Sep 2022 23:12:58 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500023.china.huawei.com (7.185.36.83) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:46 +0800 Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:46 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , , , , , , , , , , , , Subject: [PATCH -next 3/7] riscv: stacktrace: Save pt_regs in encoded fp on irq entry Date: Tue, 20 Sep 2022 23:11:58 +0800 Message-ID: <20220920151202.180057-4-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220920151202.180057-1-chenzhongjin@huawei.com> References: <20220920151202.180057-1-chenzhongjin@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" To support stack unwinding at irq entry, the position of pt_regs saved on stack is nessesary. Because for some functions, compiler only push s0/fp on stack without ra. As the situation described in commit f766f77a74f5("riscv/stacktrace: Fix stack output without ra on the s= tack top") When irq happens there, the the function frame looks like: prev function | ... | | | normal function +-----------------+ | ra to prev | | s0 of prev | | ... |<-+ leaf function +-----------------+ | | s0 of normal | | | empty slot | | irq pt_regs +-----------------+ | | epc (ra to leaf)| | | ra (ra to norm)| | | ... | | | s0 of leaf |--+ | ... | +-----------------+ If the position of register in pt_regs is {epc, s0}, we can easily unwind from irq frame to leaf function, as normal functions do. However when unwinding from unwinding from leaf to normal, beacause (ra to norm) is saved in pt_regs, but not stackframe of leaf, we have to get pt_regs for that. To get pt_regs position on stack, we can save the encoded *pt_regs in s0, as x86 architecture did. Then we can get s0, epc and ra easily. Signed-off-by: Chen Zhongjin Reviewed-by: Guo Ren --- arch/riscv/include/asm/frame.h | 45 ++++++++++++++++++++++++++++++++++ arch/riscv/kernel/entry.S | 3 +++ 2 files changed, 48 insertions(+) create mode 100644 arch/riscv/include/asm/frame.h diff --git a/arch/riscv/include/asm/frame.h b/arch/riscv/include/asm/frame.h new file mode 100644 index 000000000000..2a1f45cf3a4e --- /dev/null +++ b/arch/riscv/include/asm/frame.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_RISCV_FRAME_H +#define _ASM_RISCV_FRAME_H + +#include + +#ifdef CONFIG_FRAME_POINTER + +#ifdef __ASSEMBLY__ + +/* + * This is a sneaky trick to help the unwinder find pt_regs on the stack. = The + * frame pointer is replaced with an encoded pointer to pt_regs. The enco= ding + * is just setting the LSB, which makes it an invalid stack address and is= also + * a signal to the unwinder that it's a pt_regs pointer in disguise. + * + * This macro must be used when sp point to pt_regs + */ +.macro ENCODE_FRAME_POINTER + add s0, sp, 0x1 +.endm + +#else /* !__ASSEMBLY__ */ + +#define ENCODE_FRAME_POINTER \ + "add s0, sp, 0x1\n\t" + +#endif /* __ASSEMBLY__ */ + +#else /* !CONFIG_FRAME_POINTER */ + +#ifdef __ASSEMBLY__ + +.macro ENCODE_FRAME_POINTER ptregs_offset=3D0 +.endm + +#else /* !__ASSEMBLY */ + +#define ENCODE_FRAME_POINTER + +#endif /* !__ASSEMBLY */ + +#endif /* CONFIG_FRAME_POINTER */ + +#endif /* _ASM_RISCV_FRAME_H */ diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index b9eda3fcbd6d..ecb15c7430b4 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -13,6 +13,7 @@ #include #include #include +#include =20 #if !IS_ENABLED(CONFIG_PREEMPTION) .set resume_kernel, restore_all @@ -95,6 +96,8 @@ _save_context: REG_S s4, PT_CAUSE(sp) REG_S s5, PT_TP(sp) =20 + ENCODE_FRAME_POINTER + /* * Set the scratch register to 0, so that if a recursive exception * occurs, the exception vector knows it came from the kernel --=20 2.17.1 From nobody Thu Apr 2 21:32:08 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 760F6C6FA8E for ; Tue, 20 Sep 2022 15:16:08 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231321AbiITPQF (ORCPT ); Tue, 20 Sep 2022 11:16:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32844 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231340AbiITPPu (ORCPT ); Tue, 20 Sep 2022 11:15:50 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id EC28060535; Tue, 20 Sep 2022 08:15:48 -0700 (PDT) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.57]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4MX4lD0zS9zmVjB; Tue, 20 Sep 2022 23:11:52 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500024.china.huawei.com (7.185.36.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:46 +0800 Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:46 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , , , , , , , , , , , , Subject: [PATCH -next 4/7] riscv: syscall: Don't clobber s0 when syscall Date: Tue, 20 Sep 2022 23:11:59 +0800 Message-ID: <20220920151202.180057-5-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220920151202.180057-1-chenzhongjin@huawei.com> References: <20220920151202.180057-1-chenzhongjin@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" syscall uses s0 to load address of sys_call_table. Since now we uses s0 to save pt_regs for unwinding, clobber s0 can make unwinder treat s0 as pt_regs address. Use s1 for this job. Signed-off-by: Chen Zhongjin --- arch/riscv/kernel/entry.S | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index ecb15c7430b4..a3b14a649782 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -205,33 +205,33 @@ handle_syscall: check_syscall_nr: /* Check to make sure we don't jump to a bogus syscall number. */ li t0, __NR_syscalls - la s0, sys_ni_syscall + la s1, sys_ni_syscall /* * Syscall number held in a7. * If syscall number is above allowed value, redirect to ni_syscall. */ bgeu a7, t0, 3f #ifdef CONFIG_COMPAT - REG_L s0, PT_STATUS(sp) - srli s0, s0, SR_UXL_SHIFT - andi s0, s0, (SR_UXL >> SR_UXL_SHIFT) + REG_L s1, PT_STATUS(sp) + srli s1, s1, SR_UXL_SHIFT + andi s1, s1, (SR_UXL >> SR_UXL_SHIFT) li t0, (SR_UXL_32 >> SR_UXL_SHIFT) - sub t0, s0, t0 + sub t0, s1, t0 bnez t0, 1f =20 /* Call compat_syscall */ - la s0, compat_sys_call_table + la s1, compat_sys_call_table j 2f 1: #endif /* Call syscall */ - la s0, sys_call_table + la s1, sys_call_table 2: slli t0, a7, RISCV_LGPTR - add s0, s0, t0 - REG_L s0, 0(s0) + add s1, s1, t0 + REG_L s1, 0(s1) 3: - jalr s0 + jalr s1 =20 ret_from_syscall: /* Set user a0 to kernel a0 */ --=20 2.17.1 From nobody Thu Apr 2 21:32:08 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06E35ECAAD8 for ; Tue, 20 Sep 2022 15:16:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231450AbiITPQ3 (ORCPT ); Tue, 20 Sep 2022 11:16:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231372AbiITPPv (ORCPT ); Tue, 20 Sep 2022 11:15:51 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 474E161116; Tue, 20 Sep 2022 08:15:49 -0700 (PDT) Received: from dggpemm500021.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MX4mV62YhzpTJj; Tue, 20 Sep 2022 23:12:58 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500021.china.huawei.com (7.185.36.109) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:46 +0800 Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:46 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , , , , , , , , , , , , Subject: [PATCH -next 5/7] riscv: stacktrace: Implement stacktrace for irq Date: Tue, 20 Sep 2022 23:12:00 +0800 Message-ID: <20220920151202.180057-6-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220920151202.180057-1-chenzhongjin@huawei.com> References: <20220920151202.180057-1-chenzhongjin@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" After adding encoded fp onto stack to record pt_regs, now the unwinder have ability to unwind frame through irq. There is two steps to unwind irq frame and the interrupted frame: 1. When there is an encoded fp on stack, we can get the pt_regs and unwind frame by (regs->epc) and (regs->s0). 2. To unwind the interrupted frame, there is two possibilities, we can determine the situation by checking whether the value in frame->ra position is a fp value. If there is a fp in ra position: We are inside a leaf frame and there is only fp on ra position. Get fp from ra position and get next pc from pt_regs. Else: Just get fp and next pc from stack frame. Stacktrace before this patch: Call Trace: ... [] __flush_smp_call_function_queue+0xde/0x1fa [] generic_smp_call_function_single_interrupt+0x22/0x2a [] handle_IPI+0xaa/0x108 [] riscv_intc_irq+0x56/0x6e [] generic_handle_arch_irq+0x4c/0x76 [] ret_from_exception+0x0/0xc Stacktrace after this patch: Call Trace: ... [] __flush_smp_call_function_queue+0xde/0x1fa [] generic_smp_call_function_single_interrupt+0x22/0x2a [] handle_IPI+0xaa/0x108 [] riscv_intc_irq+0x56/0x6e [] generic_handle_arch_irq+0x4c/0x76 [] ret_from_exception+0x0/0xc + [] arch_cpu_idle+0x22/0x28 + [] default_idle_call+0x44/0xee + [] do_idle+0x116/0x126 + [] cpu_startup_entry+0x36/0x38 + [] kernel_init+0x0/0x15a + [] arch_post_acpi_subsys_init+0x0/0x38 + [] start_kernel+0x7c4/0x7f2 Signed-off-by: Chen Zhongjin --- arch/riscv/kernel/stacktrace.c | 45 ++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c index e84e21868a3e..976dc298ab3b 100644 --- a/arch/riscv/kernel/stacktrace.c +++ b/arch/riscv/kernel/stacktrace.c @@ -16,29 +16,60 @@ =20 #ifdef CONFIG_FRAME_POINTER =20 +static struct pt_regs *decode_frame_pointer(unsigned long fp) +{ + if (!(fp & 0x1)) + return NULL; + + return (struct pt_regs *)(fp & ~0x1); +} + static int notrace unwind_next(struct unwind_state *state) { unsigned long low, high, fp; struct stackframe *frame; + struct pt_regs *regs; =20 - fp =3D state->fp; + regs =3D decode_frame_pointer(state->fp); =20 /* Validate frame pointer */ - low =3D state->sp + sizeof(struct stackframe); + if (regs) { + if user_mode(regs) + return -1; + + fp =3D (unsigned long)regs; + low =3D state->sp; + } else { + fp =3D state->fp; + low =3D state->sp + sizeof(struct stackframe); + } high =3D ALIGN(low, THREAD_SIZE); =20 if (fp < low || fp > high || fp & 0x7) return -EINVAL; =20 - /* Unwind stack frame */ frame =3D (struct stackframe *)fp - 1; state->sp =3D fp; =20 - if (state->regs && state->regs->epc =3D=3D state->pc && - fp & 0x7) { - state->fp =3D frame->ra; - state->pc =3D state->regs->ra; + if (regs) { + /* Unwind from irq to interrupted function */ + state->fp =3D regs->s0; + state->pc =3D regs->epc; + state->regs =3D regs; + } else if (state->regs && state->regs->epc =3D=3D state->pc) { + /* Unwind from interrupted function to caller*/ + if (frame->ra < low || frame->ra > high) { + /* normal function */ + state->fp =3D frame->fp; + state->pc =3D frame->ra; + } else { + /* leaf function */ + state->fp =3D frame->ra; + state->pc =3D state->regs->ra; + } + state->regs =3D NULL; } else { + /* Unwind from normal stack frame */ state->fp =3D frame->fp; state->pc =3D ftrace_graph_ret_addr(current, NULL, frame->ra, (unsigned long *)fp - 1); --=20 2.17.1 From nobody Thu Apr 2 21:32:08 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 94C1CC6FA8E for ; Tue, 20 Sep 2022 15:16:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231564AbiITPQZ (ORCPT ); Tue, 20 Sep 2022 11:16:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32882 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231367AbiITPPv (ORCPT ); Tue, 20 Sep 2022 11:15:51 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4768F61129; Tue, 20 Sep 2022 08:15:49 -0700 (PDT) Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MX4l06HjSzlW5D; Tue, 20 Sep 2022 23:11:40 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500020.china.huawei.com (7.185.36.49) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:46 +0800 Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:46 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , , , , , , , , , , , , Subject: [PATCH -next 6/7] riscv: stacktrace: Fix unwinding on ftrace_regs_call Date: Tue, 20 Sep 2022 23:12:01 +0800 Message-ID: <20220920151202.180057-7-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220920151202.180057-1-chenzhongjin@huawei.com> References: <20220920151202.180057-1-chenzhongjin@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When unwinding on ftrace_regs_call, the traced function will be skipped because ftrace_regs_caller doesn't save the fp and ra. Save the encoded fp so that we can get the pt_regs so that we can unwind from ftrace_regs_call to the traced function. Also the pt_regs->status should be set as kernel mode. Stacktrace before this patch: Call Trace: ... [] handler_pre+0x30/0x4a [kprobe_unwind] [] aggr_pre_handler+0x60/0x94 [] kprobe_ftrace_handler+0x13e/0x188 [] ftrace_regs_call+0x8/0x10 [] do_one_initcall+0x4c/0x1f2 [] do_init_module+0x56/0x210 ... Stacktrace after this patch: Call Trace: ... [] handler_pre+0x30/0x4a [kprobe_unwind] [] aggr_pre_handler+0x60/0x94 [] kprobe_ftrace_handler+0x13e/0x188 [] ftrace_regs_call+0x8/0x10 + [] empty_call+0x0/0x1e [kprobe_unwind] [] do_one_initcall+0x4c/0x1f2 [] do_init_module+0x56/0x210 ... Signed-off-by: Chen Zhongjin --- arch/riscv/kernel/mcount-dyn.S | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/riscv/kernel/mcount-dyn.S b/arch/riscv/kernel/mcount-dyn.S index d171eca623b6..56a4014c392f 100644 --- a/arch/riscv/kernel/mcount-dyn.S +++ b/arch/riscv/kernel/mcount-dyn.S @@ -10,6 +10,8 @@ #include #include #include +#include +#include =20 .text =20 @@ -97,6 +99,11 @@ REG_S x29, PT_T4(sp) REG_S x30, PT_T5(sp) REG_S x31, PT_T6(sp) + +#ifdef CONFIG_FRAME_POINTER + li s0, SR_PP + REG_S s0, PT_STATUS(sp) +#endif .endm =20 .macro RESTORE_ALL @@ -172,6 +179,7 @@ ENDPROC(ftrace_caller) #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS ENTRY(ftrace_regs_caller) SAVE_ALL + ENCODE_FRAME_POINTER =20 addi a0, ra, -FENTRY_RA_OFFSET la a1, function_trace_op --=20 2.17.1 From nobody Thu Apr 2 21:32:08 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 463C4C54EE9 for ; Tue, 20 Sep 2022 15:16:37 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231549AbiITPQf (ORCPT ); Tue, 20 Sep 2022 11:16:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32904 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231379AbiITPPv (ORCPT ); Tue, 20 Sep 2022 11:15:51 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1DEA22B1A; Tue, 20 Sep 2022 08:15:49 -0700 (PDT) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.56]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4MX4nG3fd6zHpCw; Tue, 20 Sep 2022 23:13:38 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500022.china.huawei.com (7.185.36.162) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:47 +0800 Received: from ubuntu1804.huawei.com (10.67.175.36) by dggpemm500013.china.huawei.com (7.185.36.172) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 20 Sep 2022 23:15:46 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , , , , , , , , , , , , Subject: [PATCH -next 7/7] riscv: stacktrace: Fix unwinding on __kretporbe_trampoline Date: Tue, 20 Sep 2022 23:12:02 +0800 Message-ID: <20220920151202.180057-8-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220920151202.180057-1-chenzhongjin@huawei.com> References: <20220920151202.180057-1-chenzhongjin@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems703-chm.china.huawei.com (10.3.19.180) To dggpemm500013.china.huawei.com (7.185.36.172) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When unwinding on __kretprobe_trampoline, the caller of traced function will be skipped because unwinder doesn't read the saved pt_regs. Things going like this: caller's caller | ... |<---+ caller +---------------------------+ | | ra caller's caller | | | s0 of caller's caller | | | ... | | probed func returned +---------------------------+ | __kretprobe_trampoline | pt_regs: | | | epc caller | | | ra __kretprobe_trampoline| | | ... | | | s0 of caller | {ra, fp} | ... | Since from caller to __kretprobe_trampoline, the {ra, fp} are not changed, unwinder will go directly to caller's caller. Now we can have an ENCODED_FRAME_POINTER on stack and read the pt_regs, kretporbe will set the epc to correct_ret_addr so that we can unwind to the correct caller. Stacktrace before this patch: Call Trace: ... [] __kretprobe_trampoline_handler+0xc2/0x13e [] trampoline_probe_handler+0x30/0x46 [] __kretprobe_trampoline+0x52/0x92 [] kprobe_init+0x9c/0x1000 [kprobe_unwind] [] do_one_initcall+0x4c/0x1f2 ... Stacktrace after this patch: Call Trace: ... [] __kretprobe_trampoline_handler+0xc2/0x13e [] trampoline_probe_handler+0x30/0x46 [] __kretprobe_trampoline+0x52/0x92 + [] the_caller+0x2c/0x38 [kprobe_unwind] [] kprobe_init+0x9c/0x1000 [kprobe_unwind] [] do_one_initcall+0x4c/0x1f2 ... Signed-off-by: Chen Zhongjin --- arch/riscv/include/asm/stacktrace.h | 4 ++++ arch/riscv/kernel/probes/kprobes_trampoline.S | 8 ++++++++ arch/riscv/kernel/stacktrace.c | 5 +++++ 3 files changed, 17 insertions(+) diff --git a/arch/riscv/include/asm/stacktrace.h b/arch/riscv/include/asm/s= tacktrace.h index a39e4ef1dbd5..506c7c38b6cb 100644 --- a/arch/riscv/include/asm/stacktrace.h +++ b/arch/riscv/include/asm/stacktrace.h @@ -16,6 +16,10 @@ struct unwind_state { unsigned long sp; unsigned long pc; struct pt_regs *regs; +#ifdef CONFIG_KRETPROBES + struct llist_node *kr_cur; + struct task_struct *task; +#endif }; =20 extern void dump_backtrace(struct pt_regs *regs, struct task_struct *task, diff --git a/arch/riscv/kernel/probes/kprobes_trampoline.S b/arch/riscv/ker= nel/probes/kprobes_trampoline.S index 7bdb09ded39b..3c0677a714a6 100644 --- a/arch/riscv/kernel/probes/kprobes_trampoline.S +++ b/arch/riscv/kernel/probes/kprobes_trampoline.S @@ -6,6 +6,8 @@ =20 #include #include +#include +#include =20 .text .altmacro @@ -79,6 +81,12 @@ ENTRY(__kretprobe_trampoline) addi sp, sp, -(PT_SIZE_ON_STACK) save_all_base_regs =20 +#ifdef CONFIG_FRAME_POINTER + li s0, SR_PP + REG_S s0, PT_STATUS(sp) + ENCODE_FRAME_POINTER +#endif + move a0, sp /* pt_regs */ =20 call trampoline_probe_handler diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c index 976dc298ab3b..53edc685ca18 100644 --- a/arch/riscv/kernel/stacktrace.c +++ b/arch/riscv/kernel/stacktrace.c @@ -11,6 +11,7 @@ #include #include #include +#include =20 #include =20 @@ -123,6 +124,10 @@ noinline notrace void arch_stack_walk(stack_trace_cons= ume_fn consume_entry, state.sp =3D task->thread.sp; state.pc =3D task->thread.ra; } +#ifdef CONFIG_KRETPROBES + state.kr_cur =3D NULL; + state.task =3D task; +#endif =20 unwind(&state, consume_entry, cookie); } --=20 2.17.1