Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp5352590rwb; Wed, 21 Sep 2022 06:45:43 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5HCPGX+3TosYUclBi38qUb6OouRWNUEE3s/l2m/EV0ye3oQG9ojo8QiSCdYNqEIIxELe7k X-Received: by 2002:a17:907:75f4:b0:77c:1b9b:96b3 with SMTP id jz20-20020a17090775f400b0077c1b9b96b3mr21133531ejc.459.1663767943492; Wed, 21 Sep 2022 06:45:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663767943; cv=none; d=google.com; s=arc-20160816; b=Nsi7oLuxNHhAxzqKb2sXOOfe1q6feJv9DhMxkyQQonbBorc3nSVcGJx+0bUoBT8XaX hyRRqMMoyxyMlX8TyEy7UutL/bCx2rvcLfqKbg2L5i3mG38qb2E8t+v4O3Re2pN4893Z RCiLC4EIMRt8fpyLBTFKsXbXqC0WSbZSiJh2Enz0l8Wi7SRleTKkvuxbklfjag4FwWRC fE6M4MSlMcMx9e5+5IkIQLvD2qhsOp7CaaypNnDdAbauL7ClTM3Wdw3ViW0Jftwt9Cvj r5tU6RDiTMM9erwldny4JcECjPa2hUUUWlgQUp7KWwJ6pQ5bfqidaHXBle5xfLtVylbv 8kcg== 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=hH++oOUlI8koRRKzWnpPFM7aRBUq9Vm7gxYo6EECNfB7578IWjrxzrjev0LZUAkEs6 MHkYDdo9F6mgmhQZLsUI3U75MbvJuzcsl7wgijv6ZHIt1TLoumm6QDSil+W8pl2ebAGx lzH01n33xBVNPFpYJ0ETdtqQjXZ61aBW7XdxRIUDNjPwrbruorss4yQq8pO9pcPMW9tm r7YQG7RpAlN4q1nM3RT/EzwT7B7wSNMTF3AJlOu/Vf4T5QLFoGOklYcQJ86JOW0ypYa6 jtdetDPV4Zy41SjQN9UxqM2z2SC0jKWPdhZoM1qJpthr3iuNGjVLmkdyd3B9RRsGAqYc BDjg== 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 n13-20020aa7db4d000000b0044ecac7a710si2186519edt.65.2022.09.21.06.45.15; Wed, 21 Sep 2022 06:45:43 -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 S229900AbiIUMzV (ORCPT + 99 others); Wed, 21 Sep 2022 08:55:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38192 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229873AbiIUMzN (ORCPT ); Wed, 21 Sep 2022 08:55:13 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 242E93137B; Wed, 21 Sep 2022 05:55:12 -0700 (PDT) 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 Content-Type: text/plain 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-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