Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5408207rwb; Wed, 21 Sep 2022 07:27:11 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7QACibSlNUaEo2taMVXgAL+wIUVcOWdCHhxVKhcf42JElsuZ98v2dWbuFGUO+z9XMV3ayG X-Received: by 2002:a17:90a:bc83:b0:203:9dd:15ed with SMTP id x3-20020a17090abc8300b0020309dd15edmr9692545pjr.171.1663770431249; Wed, 21 Sep 2022 07:27:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663770431; cv=none; d=google.com; s=arc-20160816; b=cQIupO0hbOQyfcG77D7ISf9h0mGqn1XKbBRekmyXWlr3PCuRl7xZtemo2nBVXwKSUY URkBM6ABFDw37PfIzqFYydGRXrMdY9Lq9MHOUyVxF22sDVD4wPPlwZPRLSXnE6QRGlJW 7eOhMAO0LZabZ9YsU99F9x7nmh70TsuTHqjYHz/fzkh8Mka9DWZ996O7BFdhRaYqUG2Y WvZ4giGYFKbWKfM9YsPNLw8YkgjgwvFyp5i30K4XtE5ZnCqz3zlqKYaz8FeTqwCCEKO+ FEWdKFUj+X+32K9SCIp+6zAuWhCjK1xcM1kTLtXTBPTVIKrkaVdQEYblxCunPWU1DIoS hoZA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=Y//n1JT1A6s2gVcIHHPRWNjQGQQBfJvybRQnUPR580o=; b=NsbfeZIlaGrpV87LfCb4MVOhrEZfC6GoFrHy2dqbAUMqLM5Gh0a/Qcp3v/HI6VmQ66 D49llgcagvzMIgm2w1j0KayBWEnbkNKW96syKkrRXjqkjIpbMwkKj2Pcbb5CSd1SpRGN 1ZaYwa2xfch2vllCLdhsSVeK4OBMT7rn/3wctUw8O0J38f9lErKYmxGMuDVTJHSvhW3S 4rPOESrI7M9VWtAMhuL38Ey8N4h1DY0lGbSN2lDZ2ZUqZ92MdLA45wziHmrlfq81TadA 58FzMX7o5CwcDcxTu7GVnO6B2nVroj+9a8QCoK9o5MQq0/J2hLYo6/enmu1s2/B5Gemu a/9w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e31-20020a630f1f000000b00438f77814b2si3022806pgl.124.2022.09.21.07.26.59; Wed, 21 Sep 2022 07:27:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229525AbiIUOC6 (ORCPT + 99 others); Wed, 21 Sep 2022 10:02:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33694 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229571AbiIUOCz (ORCPT ); Wed, 21 Sep 2022 10:02:55 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B44328E4F0; Wed, 21 Sep 2022 07:02:54 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id B6F88139F; Wed, 21 Sep 2022 07:03:00 -0700 (PDT) Received: from FVFF77S0Q05N (unknown [172.31.20.19]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id DAADC3F73B; Wed, 21 Sep 2022 07:02:50 -0700 (PDT) Date: Wed, 21 Sep 2022 15:02:39 +0100 From: Mark Rutland To: Chen Zhongjin Cc: linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, linux-perf-users@vger.kernel.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, peterz@infradead.org, mingo@redhat.com, acme@kernel.org, alexander.shishkin@linux.intel.com, namhyung@kernel.org, jolsa@kernel.org, guoren@kernel.org, frederic@kernel.org, vincent.chen@sifive.com, ardb@kernel.org, mhiramat@kernel.org, rostedt@goodmis.org, keescook@chromium.org, catalin.marinas@arm.com Subject: Re: [PATCH for-next v2 0/4] riscv: Improvments for stacktrace Message-ID: References: <20220921125128.33913-1-chenzhongjin@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220921125128.33913-1-chenzhongjin@huawei.com> X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_NONE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Sep 21, 2022 at 08:51:23PM +0800, Chen Zhongjin wrote: > Currently, the stacktrace with FRAME_POINTER on riscv has some problem: > > 1. stacktrace will stop at irq so it can't get the stack frames before > irq entry. > 2. stacktrace can't unwind all the real stack frames when there is > k{ret}probes or ftrace. > > These are mainly becase when there is a pt_regs on stack, we can't unwind > the stack frame as normal function. > > Some architectures (e.g. arm64) create a extra stackframe inside pt_regs. > However this doesn't work for riscv because the ra is not ensured to be > pushed to stack. As explained in: > commit f766f77a74f5("riscv/stacktrace: Fix stack output without ra on the stack top") FWIW, this is also a latent problem on arm64, since we don't know whether the LR is live at an exception boundary (and we currently always ignore it). My plan to fix that on arm64 is to push an empty frame record to the stack upon an exception, and use that to find the pt_regs, from which we can get the PC and LR (and then we can report the later as unreliable). That should be roughly equivalent to what you do in this series (where use use the LSB to identify that the pointer is actually a pt_regs). One important thing to note is that when crossing an exception boundary you won't know whether RA is live, and so you might try to consume an address twice (if it has also been pushed to the stack). That could be problematic for unwinding ftrace or kretprobes. On arm64 we had to implement HAVE_FUNCTION_GRAPH_RET_ADDR_PTR so that we could reliably unwind ftrace. See commit: c6d3cd32fd0064af ("arm64: ftrace: use HAVE_FUNCTION_GRAPH_RET_ADDR_PTR") ... and we haven't yet come up with something similar for kretprobes (though I suspect we'll need to). Thanks, Mark. > So, I choosed the method of x86 that, if there is a pt_regs on stack, > we encoded the frame pointer and save it. When unwinding stack frame, > we can get pt_regs and registers required for unwinding stacks. > > In addition, the patch set contains some refactoring of stacktrace.c to > keep the stacktrace code on riscv consistent with other architectures. > > Stacktrace before for kretprobes: > > 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: > > 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 > ... > > Stacktrace before for ftrace: > > Call Trace: > ... > [] kprobe_ftrace_handler+0x13e/0x188 > [] ftrace_regs_call+0x8/0x10 > [] do_one_initcall+0x4c/0x1f2 > [] do_init_module+0x56/0x210 > ... > > Stacktrace after: > > Call Trace: > ... > [] handler_pre+0x30/0x4a [kprobe_unwind] > [] aggr_pre_handler+0x60/0x94 > [] kprobe_ftrace_handler+0x13e/0x188 > [] ftrace_regs_call+0x8/0x10 > + [] traced_func+0x0/0x1e [kprobe_unwind] > [] do_one_initcall+0x4c/0x1f2 > [] do_init_module+0x56/0x210 > ... > > Noticed that the caller of ftrace and probed func of kretprobe > cannot be unwind because they are inside function pro/epilogue. > > --- > v1 -> v2: > - Merge three patches which add ENCODE_FRAME_POINTER together > - Update commit message > - Delete the KRETPORBES stuff added in unwind_state, we don't need them > to recover the kretporbes ret_addr because we can get it in pt_regs > --- > Chen Zhongjin (4): > riscv: stacktrace: Replace walk_stackframe with arch_stack_walk > riscv: stacktrace: Introduce unwind functions > riscv: stacktrace: Save pt_regs in ENCODE_FRAME_POINTER > riscv: stacktrace: Implement stacktrace for irq > > arch/riscv/include/asm/frame.h | 45 ++++++ > arch/riscv/include/asm/stacktrace.h | 9 +- > arch/riscv/kernel/entry.S | 3 + > arch/riscv/kernel/mcount-dyn.S | 7 + > arch/riscv/kernel/perf_callchain.c | 2 +- > arch/riscv/kernel/probes/kprobes_trampoline.S | 7 + > arch/riscv/kernel/stacktrace.c | 150 ++++++++++++------ > 7 files changed, 174 insertions(+), 49 deletions(-) > create mode 100644 arch/riscv/include/asm/frame.h > > -- > 2.17.1 >