From nobody Thu Apr 2 23:01:51 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