From nobody Thu Apr 2 19:51:31 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 C2189C32771 for ; Wed, 21 Sep 2022 12:55:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229566AbiIUMzY (ORCPT ); Wed, 21 Sep 2022 08:55:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229873AbiIUMzN (ORCPT ); Wed, 21 Sep 2022 08:55:13 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 242E93137B; Wed, 21 Sep 2022 05:55:12 -0700 (PDT) Received: from dggpemm500024.china.huawei.com (unknown [172.30.72.56]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MXdZG6W7pzlVwv; Wed, 21 Sep 2022 20:51:02 +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; Wed, 21 Sep 2022 20:55:10 +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 4/4] riscv: stacktrace: Implement stacktrace for irq Date: Wed, 21 Sep 2022 20:51:27 +0800 Message-ID: <20220921125128.33913-5-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" 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