From patchwork Wed Sep 21 12:51:24 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Zhongjin X-Patchwork-Id: 12983700 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id C83C6ECAAD8 for ; Wed, 21 Sep 2022 12:55:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=OOhpCW7NXHzVK1BqfIVhNh8oujIEKw7T4QW8OfYmpoY=; b=2+2kEaP2813Epp kZrDw+XVx6ywhFPX7nui4MdheZEPcvcwIIvGKeKEyhkYMeBXDZFAIKp10P1a1kLF0y+EY1Tw1y8AT uoIl/EgTdFin7PdyblT1pbY+4H3KbZIKbipEuvb0K923DF+ul4t/GqmXce78fI93oI8O578Iy5/SP MG+DNUlq4ei1+v1px2U5ABiNVLj66ADpeH2LbHqRYT9DFqjY3OVkiPD578s3ZretXRvzBsv3+oheq KjWX6FIzI08CjHGd6v3f3iKDM98Mr/KF8vNh/MdHLCEhyPMLkRjb7//RC3QuL2Vlybi1pZDaodgwW w3VS19sNXajObX+/4xJg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oazG7-00BMrp-JF; Wed, 21 Sep 2022 12:55:23 +0000 Received: from szxga01-in.huawei.com ([45.249.212.187]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oazFw-00BMl4-U0 for linux-riscv@lists.infradead.org; Wed, 21 Sep 2022 12:55:18 +0000 Received: from dggpemm500020.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MXdZG2Yl7zlW4Q; Wed, 21 Sep 2022 20:51:02 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500020.china.huawei.com (7.185.36.49) 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 1/4] riscv: stacktrace: Replace walk_stackframe with arch_stack_walk Date: Wed, 21 Sep 2022 20:51:24 +0800 Message-ID: <20220921125128.33913-2-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220921_055513_428986_84A69AD2 X-CRM114-Status: GOOD ( 13.01 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org walk_stackframe can be all replaced by arch_stack_walk. Since walk_stackframe is only called by arch_stack_walk and their only difference is the argument sequence. Signed-off-by: Chen Zhongjin --- arch/riscv/include/asm/stacktrace.h | 2 -- arch/riscv/kernel/perf_callchain.c | 2 +- arch/riscv/kernel/stacktrace.c | 29 +++++++++++++---------------- 3 files changed, 14 insertions(+), 19 deletions(-) diff --git a/arch/riscv/include/asm/stacktrace.h b/arch/riscv/include/asm/stacktrace.h index 3450c1912afd..b6cd3eddfd38 100644 --- a/arch/riscv/include/asm/stacktrace.h +++ b/arch/riscv/include/asm/stacktrace.h @@ -11,8 +11,6 @@ struct stackframe { unsigned long ra; }; -extern void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, - bool (*fn)(void *, unsigned long), void *arg); extern void dump_backtrace(struct pt_regs *regs, struct task_struct *task, const char *loglvl); diff --git a/arch/riscv/kernel/perf_callchain.c b/arch/riscv/kernel/perf_callchain.c index 3348a61de7d9..c023e0b1eb81 100644 --- a/arch/riscv/kernel/perf_callchain.c +++ b/arch/riscv/kernel/perf_callchain.c @@ -74,5 +74,5 @@ static bool fill_callchain(void *entry, unsigned long pc) void perf_callchain_kernel(struct perf_callchain_entry_ctx *entry, struct pt_regs *regs) { - walk_stackframe(NULL, regs, fill_callchain, entry); + arch_stack_walk(fill_callchain, entry, NULL, regs); } diff --git a/arch/riscv/kernel/stacktrace.c b/arch/riscv/kernel/stacktrace.c index 08d11a53f39e..b51e32d50a0e 100644 --- a/arch/riscv/kernel/stacktrace.c +++ b/arch/riscv/kernel/stacktrace.c @@ -16,8 +16,9 @@ #ifdef CONFIG_FRAME_POINTER -void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, - bool (*fn)(void *, unsigned long), void *arg) +noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, + void *cookie, struct task_struct *task, + struct pt_regs *regs) { unsigned long fp, sp, pc; int level = 0; @@ -29,7 +30,7 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, } else if (task == NULL || task == current) { fp = (unsigned long)__builtin_frame_address(0); sp = current_stack_pointer; - pc = (unsigned long)walk_stackframe; + pc = (unsigned long)arch_stack_walk; } else { /* task blocked in __switch_to */ fp = task->thread.s[0]; @@ -41,7 +42,8 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, unsigned long low, high; struct stackframe *frame; - if (unlikely(!__kernel_text_address(pc) || (level++ >= 1 && !fn(arg, pc)))) + if (unlikely(!__kernel_text_address(pc) || + (level++ >= 1 && !consume_entry(cookie, pc)))) break; /* Validate frame pointer */ @@ -66,8 +68,9 @@ void notrace walk_stackframe(struct task_struct *task, struct pt_regs *regs, #else /* !CONFIG_FRAME_POINTER */ -void notrace walk_stackframe(struct task_struct *task, - struct pt_regs *regs, bool (*fn)(void *, unsigned long), void *arg) +noinline notrace void arch_stack_walk(stack_trace_consume_fn consume_entry, + void *cookie, struct task_struct *task, + struct pt_regs *regs) { unsigned long sp, pc; unsigned long *ksp; @@ -77,7 +80,7 @@ void notrace walk_stackframe(struct task_struct *task, pc = instruction_pointer(regs); } else if (task == NULL || task == current) { sp = current_stack_pointer; - pc = (unsigned long)walk_stackframe; + pc = (unsigned long)arch_stack_walk; } else { /* task blocked in __switch_to */ sp = task->thread.sp; @@ -89,7 +92,7 @@ void notrace walk_stackframe(struct task_struct *task, ksp = (unsigned long *)sp; while (!kstack_end(ksp)) { - if (__kernel_text_address(pc) && unlikely(!fn(arg, pc))) + if (__kernel_text_address(pc) && unlikely(!consume_entry(cookie, pc))) break; pc = (*ksp++) - 0x4; } @@ -108,7 +111,7 @@ static bool print_trace_address(void *arg, unsigned long pc) noinline void dump_backtrace(struct pt_regs *regs, struct task_struct *task, const char *loglvl) { - walk_stackframe(task, regs, print_trace_address, (void *)loglvl); + arch_stack_walk(print_trace_address, (void *)loglvl, task, regs); } void show_stack(struct task_struct *task, unsigned long *sp, const char *loglvl) @@ -133,13 +136,7 @@ unsigned long __get_wchan(struct task_struct *task) if (!try_get_task_stack(task)) return 0; - walk_stackframe(task, NULL, save_wchan, &pc); + arch_stack_walk(save_wchan, &pc, task, NULL); put_task_stack(task); return pc; } - -noinline void arch_stack_walk(stack_trace_consume_fn consume_entry, void *cookie, - struct task_struct *task, struct pt_regs *regs) -{ - walk_stackframe(task, regs, consume_entry, cookie); -} From patchwork Wed Sep 21 12:51:25 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Zhongjin X-Patchwork-Id: 12983697 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id B0DFAC32771 for ; Wed, 21 Sep 2022 12:55:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=ESgw8/rJ86pArDGN/7IFoZIHTkrg/SY6CZKGdXhN1mM=; b=oHYbVQsEaWeDzA wQEUnxoPxTGcJWj2/I6egQ52szJqUWAGwphWWFMpV3qYVlfwsZTnPuvxeIMcqVL5vwjDTQuxVNKoz Ji1qsS734r//3yH1dPEQtGkpg2B867gQT/kbLgo9iq6Ri4p72x8N8nUbqz8ioHf5d9iOnRgPfzCg6 zP7kM+wN53sclV65wqKdk8LalkeYQ5DdjjaUpXd+YHaPYpB5V3+Zhz0f0nKj0fojnmmzllD6j2gjV sNSKXd4ebLYu7UH3riWmsmBbpGDzHLjUSVUEwH4hGeixKzP3Uj3/ws2MRBLhgDrTmV1RPkn7kQ7D5 b59tlsYBHyGoKXIUMlLg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oazG0-00BMo0-AQ; Wed, 21 Sep 2022 12:55:16 +0000 Received: from szxga08-in.huawei.com ([45.249.212.255]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oazFw-00BMl9-Pr for linux-riscv@lists.infradead.org; Wed, 21 Sep 2022 12:55:14 +0000 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220921_055513_260551_74FBC1F5 X-CRM114-Status: GOOD ( 14.69 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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/stacktrace.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; }; +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); 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 @@ #ifdef CONFIG_FRAME_POINTER -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 = 0; + unsigned long low, high, fp; + struct stackframe *frame; - if (regs) { - fp = frame_pointer(regs); - sp = user_stack_pointer(regs); - pc = instruction_pointer(regs); - } else if (task == NULL || task == current) { - fp = (unsigned long)__builtin_frame_address(0); - sp = current_stack_pointer; - pc = (unsigned long)arch_stack_walk; + fp = state->fp; + + /* Validate frame pointer */ + low = state->sp + sizeof(struct stackframe); + high = ALIGN(low, THREAD_SIZE); + + if (fp < low || fp > high || fp & 0x7) + return -EINVAL; + + /* Unwind stack frame */ + frame = (struct stackframe *)fp - 1; + state->sp = fp; + + if (state->regs && state->regs->epc == state->pc && + fp & 0x7) { + state->fp = frame->ra; + state->pc = state->regs->ra; } else { - /* task blocked in __switch_to */ - fp = task->thread.s[0]; - sp = task->thread.sp; - pc = task->thread.ra; + state->fp = frame->fp; + state->pc = ftrace_graph_ret_addr(current, NULL, frame->ra, + (unsigned long *)fp - 1); } - for (;;) { - unsigned long low, high; - struct stackframe *frame; + return 0; +} - if (unlikely(!__kernel_text_address(pc) || - (level++ >= 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; - /* Validate frame pointer */ - low = sp + sizeof(struct stackframe); - high = ALIGN(sp, THREAD_SIZE); - if (unlikely(fp < low || fp > high || fp & 0x7)) + ret = unwind_next(state); + if (ret < 0) break; - /* Unwind stack frame */ - frame = (struct stackframe *)fp - 1; - sp = fp; - if (regs && (regs->epc == pc) && (frame->fp & 0x7)) { - fp = frame->ra; - pc = regs->ra; - } else { - fp = frame->fp; - pc = 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 == NULL) + task = current; + if (regs) { + state.fp = frame_pointer(regs); + state.sp = user_stack_pointer(regs); + state.pc = instruction_pointer(regs); + state.regs = regs; + } else if (task == current) { + state.fp = (unsigned long)__builtin_frame_address(0); + state.sp = current_stack_pointer; + state.pc = (unsigned long)arch_stack_walk; + + /* skip frame of arch_stack_walk */ + unwind_next(&state); + } else { + /* task blocked in __switch_to */ + state.fp = task->thread.s[0]; + state.sp = task->thread.sp; + state.pc = task->thread.ra; } + + unwind(&state, consume_entry, cookie); } #else /* !CONFIG_FRAME_POINTER */ From patchwork Wed Sep 21 12:51:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Zhongjin X-Patchwork-Id: 12983696 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id CAB63ECAAD8 for ; Wed, 21 Sep 2022 12:55:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=Ixfjs3PIqShKlh0OPsOV+LI8V310+iXMdgtVbvSu+UQ=; b=lIQGCbsWo5O2W9 eTJnnW/JphtSLRiy0PuQrlxYFjbUWQyoGmmOM/tpgbW5l9hjbx40A42N3uxp06VTnux19Q4HEFcki hEoSHfGaOn+b45vwHao/0A8CN3/9jeluYGrb1v7qnAZDbeyPDAGSTO7bIXr3VZRIOOZZzTFAOkZ5H 4LUMyxqIEKQSeDuCmgioVges5C8NCxHo7HlgkWGLx8R51kWykXbOj/LHiNsIYkWRbxunTeS4M1qPl 9cJlsCZg3oEX7pEzikVIy7rm1ovDjSQTrjbl6lox8I8LGt5Rzb32OTVbEeGHU1bng/tNoJ60Q6AWR bsQYFGhSPyZoUB0CP8Ag==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oazG2-00BMoM-3V; Wed, 21 Sep 2022 12:55:18 +0000 Received: from szxga08-in.huawei.com ([45.249.212.255]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oazFw-00BMlB-Pq for linux-riscv@lists.infradead.org; Wed, 21 Sep 2022 12:55:16 +0000 Received: from dggpemm500023.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4MXdZH14Zxz14RgJ; Wed, 21 Sep 2022 20:51:03 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500023.china.huawei.com (7.185.36.83) 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 3/4] riscv: stacktrace: Save pt_regs in ENCODE_FRAME_POINTER Date: Wed, 21 Sep 2022 20:51:26 +0800 Message-ID: <20220921125128.33913-4-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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220921_055513_262197_55507A58 X-CRM114-Status: GOOD ( 18.00 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org To support stack unwinding when there is a pt_regs on stack, the position of pt_regs is nessesary. Because for some functions, compiler only push s0/fp on stack without ra. As the situation described in commit f766f77a74f5("riscv/stacktrace: Fix stack output without ra on the stack top") When irq happens there, the function frame looks like: prev function | ... | | | normal function +-----------------+ | ra to prev | | s0 of prev | | ... |<-+ leaf function +-----------------+ | | s0 of normal | | | empty slot | | irq pt_regs +-----------------+ | | epc (ra to leaf)| | | ra (ra to norm)| | | ... | | | s0 of leaf |--+ | ... | +-----------------+ When unwinding from unwinding from leaf to normal, beacause (ra to norm) is saved in pt_regs, but not stackframe of leaf, we have to get pt_regs for that. To get pt_regs position on stack, we can save the encoded *pt_regs in s0, as x86 architecture did. Then we can get s0, epc and ra easily. Add ENCODE_FRAME_POINTER in irq, ftrace and kretprobe trampoline entries. For ftrace and kretprobes we should also set pt_regs as kernel mode so that they are not mistaken as user_mode pt_regs. Signed-off-by: Chen Zhongjin Reviewed-by: Guo Ren --- arch/riscv/include/asm/frame.h | 45 +++++++++++++++++++ arch/riscv/kernel/entry.S | 3 ++ arch/riscv/kernel/mcount-dyn.S | 7 +++ arch/riscv/kernel/probes/kprobes_trampoline.S | 7 +++ 4 files changed, 62 insertions(+) create mode 100644 arch/riscv/include/asm/frame.h diff --git a/arch/riscv/include/asm/frame.h b/arch/riscv/include/asm/frame.h new file mode 100644 index 000000000000..2a1f45cf3a4e --- /dev/null +++ b/arch/riscv/include/asm/frame.h @@ -0,0 +1,45 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_RISCV_FRAME_H +#define _ASM_RISCV_FRAME_H + +#include + +#ifdef CONFIG_FRAME_POINTER + +#ifdef __ASSEMBLY__ + +/* + * This is a sneaky trick to help the unwinder find pt_regs on the stack. The + * frame pointer is replaced with an encoded pointer to pt_regs. The encoding + * is just setting the LSB, which makes it an invalid stack address and is also + * a signal to the unwinder that it's a pt_regs pointer in disguise. + * + * This macro must be used when sp point to pt_regs + */ +.macro ENCODE_FRAME_POINTER + add s0, sp, 0x1 +.endm + +#else /* !__ASSEMBLY__ */ + +#define ENCODE_FRAME_POINTER \ + "add s0, sp, 0x1\n\t" + +#endif /* __ASSEMBLY__ */ + +#else /* !CONFIG_FRAME_POINTER */ + +#ifdef __ASSEMBLY__ + +.macro ENCODE_FRAME_POINTER ptregs_offset=0 +.endm + +#else /* !__ASSEMBLY */ + +#define ENCODE_FRAME_POINTER + +#endif /* !__ASSEMBLY */ + +#endif /* CONFIG_FRAME_POINTER */ + +#endif /* _ASM_RISCV_FRAME_H */ diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index b9eda3fcbd6d..ecb15c7430b4 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -13,6 +13,7 @@ #include #include #include +#include #if !IS_ENABLED(CONFIG_PREEMPTION) .set resume_kernel, restore_all @@ -95,6 +96,8 @@ _save_context: REG_S s4, PT_CAUSE(sp) REG_S s5, PT_TP(sp) + ENCODE_FRAME_POINTER + /* * Set the scratch register to 0, so that if a recursive exception * occurs, the exception vector knows it came from the kernel diff --git a/arch/riscv/kernel/mcount-dyn.S b/arch/riscv/kernel/mcount-dyn.S index d171eca623b6..a362521e030e 100644 --- a/arch/riscv/kernel/mcount-dyn.S +++ b/arch/riscv/kernel/mcount-dyn.S @@ -10,6 +10,8 @@ #include #include #include +#include +#include .text @@ -172,6 +174,11 @@ ENDPROC(ftrace_caller) #ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS ENTRY(ftrace_regs_caller) SAVE_ALL +#ifdef CONFIG_FRAME_POINTER + li s0, SR_PP + REG_S s0, PT_STATUS(sp) + ENCODE_FRAME_POINTER +#endif addi a0, ra, -FENTRY_RA_OFFSET la a1, function_trace_op diff --git a/arch/riscv/kernel/probes/kprobes_trampoline.S b/arch/riscv/kernel/probes/kprobes_trampoline.S index 7bdb09ded39b..70760a16784f 100644 --- a/arch/riscv/kernel/probes/kprobes_trampoline.S +++ b/arch/riscv/kernel/probes/kprobes_trampoline.S @@ -6,6 +6,8 @@ #include #include +#include +#include .text .altmacro @@ -78,6 +80,11 @@ ENTRY(__kretprobe_trampoline) addi sp, sp, -(PT_SIZE_ON_STACK) save_all_base_regs +#ifdef CONFIG_FRAME_POINTER + li s0, SR_PP + REG_S s0, PT_STATUS(sp) + ENCODE_FRAME_POINTER +#endif move a0, sp /* pt_regs */ From patchwork Wed Sep 21 12:51:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Chen Zhongjin X-Patchwork-Id: 12983699 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 3B4C4C6FA93 for ; Wed, 21 Sep 2022 12:55:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=IDzrojgHf3mBacJwjHCgwbfbPIIHEBuEpYWCY++dmFA=; b=CxkshV9BNvGJO3 pU0xGeOlF8ZRFFaCAIDGMfRpyQe6xm4PcxYw00wy/7g99+ZJeC0rNT3HIhHChZ1Iy3a1LjbU3bPkC 1G3qXRASQhjvCtEUsV/IIUds1W+7/kslHzo9SwTp4kXJ+1gwLErC7Zz7xIMORCTytVMsC8P+nAz+Q BdvJeehj6jzB5Fup3O9KpWMkQlKcQz1I+b+cKL9iuQCd8JFLT/s63JefrH6fAzte1h8VozrELEkAH Nc8C69zV09rUTzWJOBNtNISrg5b1ggJOpiDJAosF925jnYDW43RgCR3tipIb8sPE+IOfv/+ZXLXsE VHBQWErVXAn70s/KvCDQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1oazG6-00BMql-75; Wed, 21 Sep 2022 12:55:22 +0000 Received: from szxga01-in.huawei.com ([45.249.212.187]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1oazFw-00BMlK-U2 for linux-riscv@lists.infradead.org; Wed, 21 Sep 2022 12:55:18 +0000 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 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20220921_055513_450041_0CAC17DF X-CRM114-Status: GOOD ( 14.56 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 @@ #ifdef CONFIG_FRAME_POINTER +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; - fp = state->fp; + regs = decode_frame_pointer(state->fp); /* Validate frame pointer */ - low = state->sp + sizeof(struct stackframe); + if (regs) { + if user_mode(regs) + return -1; + + fp = (unsigned long)regs; + low = state->sp; + } else { + fp = state->fp; + low = state->sp + sizeof(struct stackframe); + } high = ALIGN(low, THREAD_SIZE); if (fp < low || fp > high || fp & 0x7) return -EINVAL; - /* Unwind stack frame */ frame = (struct stackframe *)fp - 1; state->sp = fp; - if (state->regs && state->regs->epc == state->pc && - fp & 0x7) { - state->fp = frame->ra; - state->pc = state->regs->ra; + if (regs) { + /* Unwind from irq to interrupted function */ + state->fp = regs->s0; + state->pc = regs->epc; + state->regs = regs; + } else if (state->regs && state->regs->epc == state->pc) { + /* Unwind from interrupted function to caller*/ + if (frame->ra < low || frame->ra > high) { + /* normal function */ + state->fp = frame->fp; + state->pc = frame->ra; + } else { + /* leaf function */ + state->fp = frame->ra; + state->pc = state->regs->ra; + } + state->regs = NULL; } else { + /* Unwind from normal stack frame */ state->fp = frame->fp; state->pc = ftrace_graph_ret_addr(current, NULL, frame->ra, (unsigned long *)fp - 1);