From nobody Wed Sep 17 23:51:34 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B6E7C4332F for ; Thu, 15 Dec 2022 04:03:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229749AbiLOEDf (ORCPT ); Wed, 14 Dec 2022 23:03:35 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229683AbiLOEC0 (ORCPT ); Wed, 14 Dec 2022 23:02:26 -0500 Received: from loongson.cn (mail.loongson.cn [114.242.206.163]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1CB1FF0C for ; Wed, 14 Dec 2022 20:02:24 -0800 (PST) Received: from loongson.cn (unknown [111.9.175.10]) by gateway (Coremail) with SMTP id _____8BxnutPnJpjDLoFAA--.13437S3; Thu, 15 Dec 2022 12:02:23 +0800 (CST) Received: from localhost.localdomain (unknown [111.9.175.10]) by localhost.localdomain (Coremail) with SMTP id AQAAf8BxtOVDnJpjVBkAAA--.887S8; Thu, 15 Dec 2022 12:02:23 +0800 (CST) From: Jinyang He To: Huacai Chen , WANG Xuerui , Qing Zhang Cc: loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, Steven Rostedt , Masami Hiramatsu , Mark Rutland Subject: [PATCH 6/6] LoongArch: Add generic ex-handler unwind in prologue unwinder Date: Thu, 15 Dec 2022 12:01:41 +0800 Message-Id: <20221215040141.18610-7-hejinyang@loongson.cn> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20221215040141.18610-1-hejinyang@loongson.cn> References: <20221215040141.18610-1-hejinyang@loongson.cn> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-CM-TRANSID: AQAAf8BxtOVDnJpjVBk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CI42IY6xAIw20EY4v20xvaj4 0_Jr0_JF4lIxAIcVC2z280aVAFwI0_Gr0_Cr1lIxAIcVC2z280aVCY1x0267AKxVW8JVW8 JrUvcSsGvfC2KfnxnUUI43ZEXa7IUn0385UUUUU== Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" When exception is triggered, code flow go handle_\exception in some cases. One of stackframe in this case as follows, high -> +-------+ | REGS | <- a pt_regs | | | | <- ex trigger | REGS | <- ex pt_regs <-+ | | | | | | low -> +-------+ ->unwind-+ When unwinder unwind to handler_\exception it cannot go on prologue analysis. It is asynchronous code flow, we should get the next frame PC from regs->csr_era but not from regs->regs[1]. And we copy the handler codes to eentry in the early time and copy the handler codes to NUMA-relative memory named pcpu_handlers if NUMA is enabled. Thus, unwinder cannot unwind normally. Therefore, try to give some hint in handler_\exception and fixup it in unwind_next_frame. Reported-by: Qing Zhang Signed-off-by: Jinyang He --- arch/loongarch/include/asm/unwind.h | 2 +- arch/loongarch/kernel/genex.S | 3 + arch/loongarch/kernel/unwind_prologue.c | 100 +++++++++++++++++++++--- arch/loongarch/mm/tlb.c | 2 +- 4 files changed, 92 insertions(+), 15 deletions(-) diff --git a/arch/loongarch/include/asm/unwind.h b/arch/loongarch/include/a= sm/unwind.h index a16aff1d086a..c02cb3b39fe2 100644 --- a/arch/loongarch/include/asm/unwind.h +++ b/arch/loongarch/include/asm/unwind.h @@ -24,7 +24,7 @@ struct unwind_state { char type; /* UNWINDER_XXX */ struct stack_info stack_info; struct task_struct *task; - bool first, error, is_ftrace; + bool first, error, reset; int graph_idx; unsigned long sp, pc, ra; const struct unwinder_ops *ops; diff --git a/arch/loongarch/kernel/genex.S b/arch/loongarch/kernel/genex.S index 75e5be807a0d..7e5c293ed89f 100644 --- a/arch/loongarch/kernel/genex.S +++ b/arch/loongarch/kernel/genex.S @@ -67,14 +67,17 @@ SYM_FUNC_END(except_vec_cex) .macro BUILD_HANDLER exception handler prep .align 5 SYM_FUNC_START(handle_\exception) + 666: BACKUP_T0T1 SAVE_ALL build_prep_\prep move a0, sp la.abs t0, do_\handler jirl ra, t0, 0 + 668: RESTORE_ALL_AND_RET SYM_FUNC_END(handle_\exception) + SYM_DATA(unwind_hint_\exception, .word 668b - 666b) .endm =20 BUILD_HANDLER ade ade badv diff --git a/arch/loongarch/kernel/unwind_prologue.c b/arch/loongarch/kerne= l/unwind_prologue.c index 441641227c10..c34bb035ac56 100644 --- a/arch/loongarch/kernel/unwind_prologue.c +++ b/arch/loongarch/kernel/unwind_prologue.c @@ -2,23 +2,102 @@ /* * Copyright (C) 2022 Loongson Technology Corporation Limited */ +#include #include #include =20 #include +#include #include +#include #include =20 static const struct unwinder_ops *guard_unwinder =3D &unwinder_guess; =20 -static inline void unwind_state_fixup(struct unwind_state *state) +extern const int unwind_hint_ade; +extern const int unwind_hint_ale; +extern const int unwind_hint_bp; +extern const int unwind_hint_fpe; +extern const int unwind_hint_fpu; +extern const int unwind_hint_lsx; +extern const int unwind_hint_lasx; +extern const int unwind_hint_lbt; +extern const int unwind_hint_ri; +extern const int unwind_hint_watch; +extern unsigned long eentry; +#ifdef CONFIG_NUMA +extern unsigned long pcpu_handlers[NR_CPUS]; +#endif + +static inline bool scan_handler(unsigned long entry_offset) { -#ifdef CONFIG_DYNAMIC_FTRACE - static unsigned long ftrace =3D (unsigned long)ftrace_call + 4; + int idx, offset; =20 - if (state->pc =3D=3D ftrace) - state->is_ftrace =3D true; + if (entry_offset >=3D EXCCODE_INT_START * VECSIZE) + return false; + + idx =3D entry_offset / VECSIZE; + offset =3D entry_offset % VECSIZE; + switch (idx) { + case EXCCODE_ADE: + return offset =3D=3D unwind_hint_ade; + case EXCCODE_ALE: + return offset =3D=3D unwind_hint_ale; + case EXCCODE_BP: + return offset =3D=3D unwind_hint_bp; + case EXCCODE_FPE: + return offset =3D=3D unwind_hint_fpe; + case EXCCODE_FPDIS: + return offset =3D=3D unwind_hint_fpu; + case EXCCODE_LSXDIS: + return offset =3D=3D unwind_hint_lsx; + case EXCCODE_LASXDIS: + return offset =3D=3D unwind_hint_lasx; + case EXCCODE_BTDIS: + return offset =3D=3D unwind_hint_lbt; + case EXCCODE_INE: + return offset =3D=3D unwind_hint_ri; + case EXCCODE_WATCH: + return offset =3D=3D unwind_hint_watch; + default: + return false; + } +} + +static inline bool fix_exceptions(unsigned long pc) +{ +#ifdef CONFIG_NUMA + int cpu; + + for_each_possible_cpu(cpu) { + if (!pcpu_handlers[cpu]) + continue; + if (scan_handler(pc - pcpu_handlers[cpu])) + return true; + } #endif + return scan_handler(pc - eentry); +} + +/* + * As we meet ftrace_regs_entry, reset first flag like first doing + * tracing. Prologue analysis will stop soon because PC is at entry. + */ +static inline bool fix_ftrace(unsigned long pc) +{ +#ifdef CONFIG_DYNAMIC_FTRACE + return pc =3D=3D (unsigned long)ftrace_call + LOONGARCH_INSN_SIZE; +#else + return false; +#endif +} + +static inline bool unwind_state_fixup(struct unwind_state *state) +{ + if (!fix_exceptions(state->pc) && !fix_ftrace(state->pc)) + return false; + state->reset =3D true; + return true; } =20 static unsigned long get_return_address(struct unwind_state *state) @@ -46,14 +125,10 @@ static bool unwind_by_prologue(struct unwind_state *st= ate) if (state->sp >=3D info->end || state->sp < info->begin) return false; =20 - if (state->is_ftrace) { - /* - * As we meet ftrace_regs_entry, reset first flag like first doing - * tracing. Prologue analysis will stop soon because PC is at entry. - */ + if (state->reset) { regs =3D (struct pt_regs *)state->sp; state->first =3D true; - state->is_ftrace =3D false; + state->reset =3D false; state->pc =3D regs->csr_era; state->ra =3D regs->regs[1]; state->sp =3D regs->regs[3]; @@ -118,8 +193,7 @@ static bool unwind_by_prologue(struct unwind_state *sta= te) =20 out: state->first =3D false; - unwind_state_fixup(state); - return !!__kernel_text_address(state->pc); + return unwind_state_fixup(state) || __kernel_text_address(state->pc); } =20 static int raw_show_trace; diff --git a/arch/loongarch/mm/tlb.c b/arch/loongarch/mm/tlb.c index da3681f131c8..8bad6b0cff59 100644 --- a/arch/loongarch/mm/tlb.c +++ b/arch/loongarch/mm/tlb.c @@ -251,7 +251,7 @@ static void output_pgtable_bits_defines(void) } =20 #ifdef CONFIG_NUMA -static unsigned long pcpu_handlers[NR_CPUS]; +unsigned long pcpu_handlers[NR_CPUS]; #endif extern long exception_handlers[VECSIZE * 128 / sizeof(long)]; =20 --=20 2.34.3