Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp4111290rwb; Tue, 20 Sep 2022 09:00:23 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7SX32OjXYrHxsA6+g9+T+QqSBGB7a7MHZwuJfibNhnvMgu65qRWeyNmIIZNbsMVNqa4uDr X-Received: by 2002:a17:90b:3e8d:b0:203:1b6d:2112 with SMTP id rj13-20020a17090b3e8d00b002031b6d2112mr4685600pjb.6.1663689622769; Tue, 20 Sep 2022 09:00:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663689622; cv=none; d=google.com; s=arc-20160816; b=XNKEOKzxWYEAycoib/tGJHeWwjBoplI0Mg9FFfrULLlmDCTwkzJordnWJPkqirE7c4 22jqinDo5MxRtB+B3lJ7gE7jv9LZaPD2ZZicr9TxX57ls7OdGb9otE0mzRXYN9N/FyH7 /U7kY2oGu1uk9Nu3zeuQCqgIlRAYXfY1vVrqbOa3k8/ZL6VSfhM/q62/lwk6B2xcpt+N hd1ie0jEaIV4u04WbCJyDcbpGztGMeqXlEPW9ZiuYRW5xBwDUEKsBUAirGKEcPwIx2w6 MPy802CH5H6WQCVmDbRE3E+DAMYQRBlZdLjhR6v6CRC5Qk33X5fM7+/6cGLlmN513PtM 1NYA== 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=IcG7qP/ErDVEQQyhm/+6RMPTd/LX8F8RpPVFRrUPL2A=; b=or28xEEY6sai2K5ewNmlzUIWC6n+H14S9aBnlkO9Y5RnQEHLDikNNAlcQlsi5qHqvS H1o1k8ZuwEA7mG7EWvAJ3xYICvUBqAfzs99Nyi9f6p6N1I7bEOs7J8i0r2Ug7UYjwWqL ThYzh9xetH5HcxeCa7J53ALOb7z+SG+FTFBOy/hvkTjk/n7qCioMXELH5U9cN5/Dy48c bPyGDpvqMr5qpg9hFTlGCC9woCd9DoARh1xuAup1qvwcucc3aFwMOS+Wzngl8txa2UZ5 FQq987fwQdKJPbTV9zrwNIo3RxcHcJ7GYfj6AkU3MgztkIamY35IW4aEF58sXpRau+fe dsBQ== 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 o21-20020a635a15000000b00429b4bd247asi104832pgb.397.2022.09.20.09.00.10; Tue, 20 Sep 2022 09:00:22 -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 S231339AbiITPQb (ORCPT + 99 others); Tue, 20 Sep 2022 11:16:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32900 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231375AbiITPPv (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 4784E61D4D; Tue, 20 Sep 2022 08:15:49 -0700 (PDT) Received: from dggpemm500022.china.huawei.com (unknown [172.30.72.55]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MX4mV1fgRzpTJ7; Tue, 20 Sep 2022 23:12:58 +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; 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:45 +0800 From: Chen Zhongjin To: , , CC: , , , , , , , , , , , , , , , , , , , , Subject: [PATCH -next 2/7] riscv: stacktrace: Introduce unwind functions Date: Tue, 20 Sep 2022 23:11:57 +0800 Message-ID: <20220920151202.180057-3-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 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 */ -- 2.17.1