Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4123053rwb; Tue, 20 Sep 2022 09:07:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5WgALj6mwBm6N6nDByFPKMJt8LZ+vA11ZtqSNw7bqrwImylLgMW1x4lPdKdUNj0rY1UHaR X-Received: by 2002:a17:90a:243:b0:200:80ea:a6f3 with SMTP id t3-20020a17090a024300b0020080eaa6f3mr4677671pje.9.1663690032387; Tue, 20 Sep 2022 09:07:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663690032; cv=none; d=google.com; s=arc-20160816; b=suWSArQuJ47k9C65f4JkfzA+/h3Li/rt4kLJeiBKx4v96/TQCCu9GdJKKL/GIxAvNZ ivqq/PmCCTL4G7SegWcrV9cVx6ApMVfeeJ0jYWTMwDp6g+PQSmBSiu3bF3UyedlGx031 tVV19zkFEJt/qTYvv+g7EQiJK/RRK4PgqVmpMrg5SVJIeDttte1FAfbFrz9KBjj0eL/w te34au4Mn3+SYV0pwPMbUy2kdulAh3ibHx2ZUdUQvvJTT1hY3O3Z7a681oA4ddBlxCwm kjYiq5kjyj7O/XH3Y5pfYRHHqE1vjG36AvBqn3FMiS4bYXJhP7S4eJ+ZjHag0QN5Ok5p sV5g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:references:in-reply-to:message-id :date:subject:cc:to:from; bh=mKN5cunEvjQC2pNjJm3gFNmT920BvrG4PAHdp+S8nZU=; b=v8aUZoHqTfekZzA2pY/H9YXgDRUxUyiV4hbLmfvaAgdp71xhWt0fc5KrYbe0l5KJxt B73r29uhmRimYiK3cqo33yzIVX0a9eiK7Dt3AAfGNnO930fsURij3dCoJCJ5V51LfYIh IRONycEu+PoljJKavc89mr0zvKzvnS4fZn0WIyPUIvDAU15VImedCXLafuvRR+yLg8DG PJnH9gIcuEArFVryHYOUQmOribuT680LZ4PRdIQfv7CoscUKgqEGQ4dw1sBSHnZvdOQD FtBoTj+KrhvunwTEvUIBH3bdxcCSZHTrHbnEmSlZtNg3+rihnBFMDU3/A7vsee520/og Ye1w== 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id r20-20020a170902e3d400b00174de06d91fsi80545ple.326.2022.09.20.09.07.00; Tue, 20 Sep 2022 09:07:12 -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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231442AbiITPQ2 (ORCPT + 99 others); Tue, 20 Sep 2022 11:16:28 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32894 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231372AbiITPPv (ORCPT ); Tue, 20 Sep 2022 11:15:51 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 474E161116; Tue, 20 Sep 2022 08:15:49 -0700 (PDT) Received: from dggpemm500021.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MX4mV62YhzpTJj; Tue, 20 Sep 2022 23:12:58 +0800 (CST) Received: from dggpemm500013.china.huawei.com (7.185.36.172) by dggpemm500021.china.huawei.com (7.185.36.109) 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 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 5/7] riscv: stacktrace: Implement stacktrace for irq Date: Tue, 20 Sep 2022 23:12:00 +0800 Message-ID: <20220920151202.180057-6-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 Content-Type: text/plain 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 X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, 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 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); -- 2.17.1