Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6822241rwb; Mon, 5 Dec 2022 19:03:17 -0800 (PST) X-Google-Smtp-Source: AA0mqf6xUrUcCkgrbtV/GEgjlQzXq7DVamsSl19MtEaTTrVijHk37kpMN2q8xyDP4EoUZx+1XEX0 X-Received: by 2002:a17:903:32ce:b0:189:a0eb:4a26 with SMTP id i14-20020a17090332ce00b00189a0eb4a26mr32071347plr.162.1670295797084; Mon, 05 Dec 2022 19:03:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670295797; cv=none; d=google.com; s=arc-20160816; b=fP+hEj1Qzlu0+FlCw9kZ8JreMfUvxWuqSXXKTTFYQ+ZBuU8fDCYe9l9bEAq2Y5OxsQ teci2reNfpWWZ4kf27A3PU18+5eqhutgPRGydGXhAAZ5GJ5z8XEi2WrzWP2jr0NR3bNv 0mQ80EPnDvStztbzlCTUL2hjZmZgqK1Mj92i+vlJFGsSCzf/H6FoIpjzHVOlwv3XymXX SweR/L1e9jTlg71NxSivlerev9WrryF3rPkCTdgn9FVQO0Z769HbmwcqWRlzPYuwMLM5 OpfUghu/cjnlxo/msmVg9q8hKbT8FV8C5Sw5/SZecdZ85xQSuJasWy63DXfJNScMzkY1 nAqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:to:from:cc:in-reply-to:subject:date:dkim-signature; bh=Ab8kuhVwWTBvpOQiwmZyZf7s/mXfT6Eb6o4hLBwr+lA=; b=llq1TFbChutPtu6o7RfcXKHcT/M6PdIzI0OinC5mANInsEJL4CkZZixriHGoJ1ddp4 TaSSqe0Y9uhkB9FIleEcgCvGOLa9zNjaR6P8AG/gs098Uw9+/AnllYTUMiAI0vAu2gBE 1mBn2LtFO7MTXsv95AzFU5uIu93aJgA6tJyzDXulQ4d2ppDg95flRsCq6KPifOBWxv1S 1JXKC8ltT8zZemeJZC0d01+HQ7NSZMtKn4hYFx9FhwJTR0kKU96YMU+EXjolyWtyGs0j JUOCIfz1+Ggx7/QsABOFntQe0MSSueHWzMpuEMqzdYbU5loFAMJlgotc7vV3JQLqJ+Lg p0Qg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@dabbelt-com.20210112.gappssmtp.com header.s=20210112 header.b=j3OwCgaU; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id s3-20020a632143000000b00476c718a6b4si16398697pgm.453.2022.12.05.19.03.06; Mon, 05 Dec 2022 19:03:17 -0800 (PST) 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; dkim=pass header.i=@dabbelt-com.20210112.gappssmtp.com header.s=20210112 header.b=j3OwCgaU; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233587AbiLFDAD (ORCPT + 80 others); Mon, 5 Dec 2022 22:00:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233119AbiLFC75 (ORCPT ); Mon, 5 Dec 2022 21:59:57 -0500 Received: from mail-pg1-x52f.google.com (mail-pg1-x52f.google.com [IPv6:2607:f8b0:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B302622521 for ; Mon, 5 Dec 2022 18:59:55 -0800 (PST) Received: by mail-pg1-x52f.google.com with SMTP id v3so12204547pgh.4 for ; Mon, 05 Dec 2022 18:59:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dabbelt-com.20210112.gappssmtp.com; s=20210112; h=content-transfer-encoding:mime-version:message-id:to:from:cc :in-reply-to:subject:date:from:to:cc:subject:date:message-id :reply-to; bh=Ab8kuhVwWTBvpOQiwmZyZf7s/mXfT6Eb6o4hLBwr+lA=; b=j3OwCgaUTPUnSnJm1D1l4qBt9ksLQcUh9+GXI2ojHU4oLznI58uDGTbMbaqHLYeKOB b39jczj9xPwTDKzd+FzbnRqZZc6Kt1iW2gWP7K40KQoWfcqMTaV7WGxfhvkM5lUfJuoM Wx8Qvb6APgb019OoOY55DzpFX4qKfpnZg2yIAZzNMvWv1AxYudjso7bC8StfUF0nMX2X xlycpOvHqF9xd90EJ1CFj4cchlK+kBn7WbTmnOD1LKS73DiJ4CTOXRcujO3Ym64nNShF j1KIvpCqjs6m8HyUyLFD9CpJ8PPdd1/frfxMRClmYoCFRAbVBjsd1/V93YQRR8Ri7A2E I7HQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:to:from:cc :in-reply-to:subject:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=Ab8kuhVwWTBvpOQiwmZyZf7s/mXfT6Eb6o4hLBwr+lA=; b=dyUEkZN7oU0ZAEXYxiJHybmy7RKzq2nzaSL4HKm8w6ql16GeWdS4Ewv09fuKRvwRn1 OV9FooSOd/vvW4IGdLPtf3a6qBVKdGk3bXvjTNAoa2hLs5frIKUTvHaEDF3m4feM3bpy cULIq0n4UyDXAFrIrSRVd5gRqJ7nvkLTLzeC2WEBR81d4x2wRE8+fG5Gv/igaB2gI7zH o1bgk/NSywIczYibkkoMExtt4mw9a105z6vaHimcR/JGU1gzVfu4RwxBWqOs7W5+zQUb 29sjpjXA4SXhj7P0I3eRc+VZ2DcGLb1aDJOsUnRVSNHOEawVMeZLsxY4YuS/I8eTtnft UTEw== X-Gm-Message-State: ANoB5pkz4ijLujbBks9zNQIf7qo8dEosoAWcm1d/W5wuPJYufZ/0oyVa NX6netGikhC/rFzxXduFqijDTA== X-Received: by 2002:a63:f962:0:b0:477:1bb8:bbf4 with SMTP id q34-20020a63f962000000b004771bb8bbf4mr59094188pgk.19.1670295594913; Mon, 05 Dec 2022 18:59:54 -0800 (PST) Received: from localhost ([50.221.140.188]) by smtp.gmail.com with ESMTPSA id b11-20020a1709027e0b00b0018957322953sm11250715plm.45.2022.12.05.18.59.53 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Dec 2022 18:59:54 -0800 (PST) Date: Mon, 05 Dec 2022 18:59:54 -0800 (PST) X-Google-Original-Date: Mon, 05 Dec 2022 18:59:29 PST (-0800) Subject: Re: [PATCH 2/2] riscv: stacktrace: Make walk_stackframe cross pt_regs frame In-Reply-To: <20221109064937.3643993-3-guoren@kernel.org> CC: anup@brainfault.org, Paul Walmsley , Conor Dooley , heiko@sntech.de, peterz@infradead.org, Arnd Bergmann , linux-arch@vger.kernel.org, keescook@chromium.org, paulmck@kernel.org, frederic@kernel.org, nsaenzju@redhat.com, changbin.du@intel.com, vincent.chen@sifive.com, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, guoren@linux.alibaba.com, guoren@kernel.org From: Palmer Dabbelt To: guoren@kernel.org Message-ID: Mime-Version: 1.0 (MHng) Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS 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 Tue, 08 Nov 2022 22:49:37 PST (-0800), guoren@kernel.org wrote: > From: Guo Ren > > The current walk_stackframe with FRAME_POINTER would stop unwinding at > ret_from_exception: > BUG: sleeping function called from invalid context at kernel/locking/rwsem.c:1518 > in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 1, name: init > CPU: 0 PID: 1 Comm: init Not tainted 5.10.113-00021-g15c15974895c-dirty #192 > Call Trace: > [] walk_stackframe+0x0/0xee > [] show_stack+0x32/0x4a > [] dump_stack_lvl+0x72/0x8e > [] dump_stack+0x14/0x1c > [] ___might_sleep+0x12e/0x138 > [] __might_sleep+0x10/0x18 > [] down_read+0x22/0xa4 > [] do_page_fault+0xb0/0x2fe > [] ret_from_exception+0x0/0xc > > The optimization would help walk_stackframe cross the pt_regs frame and > get more backtrace of debug info: > BUG: sleeping function called from invalid context at kernel/locking/rwsem.c:1518 > in_atomic(): 0, irqs_disabled(): 1, non_block: 0, pid: 1, name: init > CPU: 0 PID: 1 Comm: init Not tainted 5.10.113-00021-g15c15974895c-dirty #192 > Call Trace: > [] walk_stackframe+0x0/0xee > [] show_stack+0x32/0x4a > [] dump_stack_lvl+0x72/0x8e > [] dump_stack+0x14/0x1c > [] ___might_sleep+0x12e/0x138 > [] __might_sleep+0x10/0x18 > [] down_read+0x22/0xa4 > [] do_page_fault+0xb0/0x2fe > [] ret_from_exception+0x0/0xc > [] riscv_intc_irq+0x1a/0x72 > [] ret_from_exception+0x0/0xc > [] vma_link+0x54/0x160 > [] mmap_region+0x2cc/0x4d0 > [] do_mmap+0x2d8/0x3ac > [] vm_mmap_pgoff+0x70/0xb8 > [] vm_mmap+0x2a/0x36 > [] elf_map+0x72/0x84 > [] load_elf_binary+0x69a/0xec8 > [] bprm_execve+0x246/0x53a > [] kernel_execve+0xe8/0x124 > [] run_init_process+0xfa/0x10c > [] try_to_run_init_process+0x12/0x3c > [] kernel_init+0xb4/0xf8 > [] ret_from_exception+0x0/0xc > > Here is the error injection test code for the above output: > drivers/irqchip/irq-riscv-intc.c: > static asmlinkage void riscv_intc_irq(struct pt_regs *regs) > { > unsigned long cause = regs->cause & ~CAUSE_IRQ_FLAG; > + u32 tmp; __get_user(tmp, (u32 *)0); > > Signed-off-by: Guo Ren > Signed-off-by: Guo Ren > Cc: Palmer Dabbelt > Cc: Changbin Du > --- > arch/riscv/kernel/entry.S | 2 +- > arch/riscv/kernel/stacktrace.c | 9 +++++++++ > 2 files changed, 10 insertions(+), 1 deletion(-) > > diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S > index b9eda3fcbd6d..329cf51fcd4d 100644 > --- a/arch/riscv/kernel/entry.S > +++ b/arch/riscv/kernel/entry.S > @@ -248,7 +248,7 @@ ret_from_syscall_rejected: > andi t0, t0, _TIF_SYSCALL_WORK > bnez t0, handle_syscall_trace_exit > > -ret_from_exception: > +ENTRY(ret_from_exception) This at least needs an END(), but it should also be converted over to some non-function entry flavor. I converted it over to SYM_CODE_START_NOALIGN(), with the cooresponding SYM_CODE_END(), and put it on for-next. > REG_L s0, PT_STATUS(sp) > csrc CSR_STATUS, SR_IE > #ifdef CONFIG_TRACE_IRQFLAGS > diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c > index bcfe9eb55f80..75c8dd64fc48 100644 > --- a/arch/riscv/kernel/stacktrace.c > +++ b/arch/riscv/kernel/stacktrace.c > @@ -16,6 +16,8 @@ > > #ifdef CONFIG_FRAME_POINTER > > +extern asmlinkage void ret_from_exception(void); > + > void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, > bool (*fn)(void *, unsigned long), void *arg) > { > @@ -59,6 +61,13 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, > fp = frame->fp; > pc = ftrace_graph_ret_addr(current, NULL, frame->ra, > &frame->ra); > + if (pc == (unsigned long)ret_from_exception) { > + if (unlikely(!__kernel_text_address(pc) || !fn(arg, pc))) > + break; > + > + pc = ((struct pt_regs *)sp)->epc; > + fp = ((struct pt_regs *)sp)->s0; > + } > } > > }