From nobody Thu Apr 2 19:51:32 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 E9194C6FA91 for ; Wed, 21 Sep 2022 12:55:30 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230129AbiIUMz2 (ORCPT ); Wed, 21 Sep 2022 08:55:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38168 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229877AbiIUMzN (ORCPT ); Wed, 21 Sep 2022 08:55:13 -0400 Received: from szxga08-in.huawei.com (szxga08-in.huawei.com [45.249.212.255]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BB75843617; Wed, 21 Sep 2022 05:55:11 -0700 (PDT) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.55]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4MXdZG6qlmz14Rfn; Wed, 21 Sep 2022 20:51:02 +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; Wed, 21 Sep 2022 20:55:09 +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; Wed, 21 Sep 2022 20:55:09 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , , , , , , , , , , Subject: [PATCH for-next v2 2/4] riscv: stacktrace: Introduce unwind functions Date: Wed, 21 Sep 2022 20:51:25 +0800 Message-ID: <20220921125128.33913-3-chenzhongjin@huawei.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220921125128.33913-1-chenzhongjin@huawei.com> References: <20220921125128.33913-1-chenzhongjin@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.67.175.36] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) 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