Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6640909rwb; Tue, 15 Nov 2022 01:18:18 -0800 (PST) X-Google-Smtp-Source: AA0mqf45Tymckhz4fQbQKfBbZl8PISQmkgYWjUOwedXgRGV4edGndNCwXJ521pIGnw0qfaj5dvXp X-Received: by 2002:a17:902:e154:b0:188:5688:828b with SMTP id d20-20020a170902e15400b001885688828bmr3213647pla.129.1668503898600; Tue, 15 Nov 2022 01:18:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668503898; cv=none; d=google.com; s=arc-20160816; b=dHYOAipDE9O6JfongqtgkbnsAL+hfj78bgike1Zk0KRToYHzWE6OicH3gun7Wv+Fbt gHaB0GGRdBhu4cH2hM7z81fpAQRgFptpXl7HL75slSCj7qF9iV3K1X55p8l+360n8Rt4 HMuXe2DH/McWUyVFlbk+p8UOA5PTblmoMzsMb7m+46gkgptaZh/+dHEDSnSU+RlmsQ5v 7Q/NciU6JSRh27wlhSibpJ1a4ag1IIeN5kJnO8Z0TBe1oeTp6lyd2iTOSjIlIK9aQA8w GSfNtrQyzS/Y1vl8yrFb1nud749ikxqMSzNb42ZMx9XmQYf48CWTkU/hvkRcLwF8Uo8N dhPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=3tY3UEiEAh/grWWo6NlAT5QnRxHAh2RVAp4tKlIZ39g=; b=QEg30RMFgencYjddqxGTIImK4FpGpc6shXjVsPtOWYaULlag1vCTDHUXnjFQtxzA1C GlZIx1+okbvtt0vmNMUBqLIL0E5FMd9E9mDIEgCEzD33H64jdqC2i6ZE9/a+PKVHXKdx Hn9p62x8QOuXLvOpZS8reTrxGQsTiRZVmQu0eX3qOr5L4/x9ce8OzhHGHV5s/aDlsdEI JjXqd0mrMneHSxRXcXZkGnL7zeGUN/yPZchwLrq1tl0oBWJgBpWpLNt+g1PNzeORsJRs wy9Dpr7J4rPbFx1aBWLJfE3bhxc/5Mn0Cmp18cxt6mex+37S0ENLkjs09ieD0Makq6iY A9bg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FOUHizE1; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k3-20020aa79d03000000b0056cac533dfdsi10462482pfp.261.2022.11.15.01.18.06; Tue, 15 Nov 2022 01:18:18 -0800 (PST) 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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=FOUHizE1; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232202AbiKOIZh (ORCPT + 88 others); Tue, 15 Nov 2022 03:25:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45378 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229585AbiKOIZe (ORCPT ); Tue, 15 Nov 2022 03:25:34 -0500 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3BA141E73D for ; Tue, 15 Nov 2022 00:25:33 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id B4426B81333 for ; Tue, 15 Nov 2022 08:25:31 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 58883C433D6 for ; Tue, 15 Nov 2022 08:25:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1668500730; bh=tJfEHk8rABrktQjIUxMkI2IQVEnnGdJGnzgh5VVTYJM=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=FOUHizE1y4kPnorXu7T5R2g75GG0lVo6NORs+4FakRjgsTNVUlg498/PYUO0IDT9K eToC+73QlEobxoadmdzKdZuCegtAvn+ghuIZs1pFtesd6WxjsNWxczaT2KKFicjSME LKXhBvm0hRJnh4ndTkcZKyJ3/9aQbqmY7nlolz5XzUnl2DC77cU9S74sot8B19DfvU CLgvLBrbFUFsvjd7sKZe4S1AUgSmiWgr6mapdbjMeFdAF9+QYoLmyEX4ZFWLbQJJc7 0sSzQakLanZyavQBQjbYpAvol8NaSGCqz1pdw6yVBhAw1i3RGsnAtc/Dk45cG7sufe Ocgx44vWwC4Pw== Received: by mail-ed1-f53.google.com with SMTP id v27so20833218eda.1 for ; Tue, 15 Nov 2022 00:25:30 -0800 (PST) X-Gm-Message-State: ANoB5pm7bZaPAd6vT+u4zelPw5bO+ww/8VTCV6o+xUsSAv7xYxjwvDu5 wqyHdUx4pgZLtzBht4mDoUHRMBjf0PpcJrs9PMw= X-Received: by 2002:aa7:c983:0:b0:461:c6e8:452e with SMTP id c3-20020aa7c983000000b00461c6e8452emr13794089edt.298.1668500728564; Tue, 15 Nov 2022 00:25:28 -0800 (PST) MIME-Version: 1.0 References: <20221115025527.13382-1-zhangqing@loongson.cn> <20221115025527.13382-2-zhangqing@loongson.cn> In-Reply-To: <20221115025527.13382-2-zhangqing@loongson.cn> From: Huacai Chen Date: Tue, 15 Nov 2022 16:25:15 +0800 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [PATCH v6 1/9] LoongArch/ftrace: Add basic support To: Qing Zhang Cc: Steven Rostedt , Ingo Molnar , loongarch@lists.linux.dev, linux-kernel@vger.kernel.org, Jinyang He Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 Hi, Qing, On Tue, Nov 15, 2022 at 10:55 AM Qing Zhang wrote: > > This patch contains basic ftrace support for LoongArch. > Specifically, function tracer (HAVE_FUNCTION_TRACER), function graph > tracer (HAVE_FUNCTION_GRAPH_TRACER) are implemented following the > instructions in Documentation/trace/ftrace-design.txt. > > Use `-pg` makes stub like a child function `void _mcount(void *ra)`. > Thus, it can be seen store RA and open stack before `call _mcount`. > Find `open stack` at first, and then find `store RA` > > Note that the functions in both inst.c and time.c should not be > hooked with the compiler's -pg option: to prevent infinite self- > referencing for the former, and to ignore early setup stuff for the > latter. > > Co-developed-by: Jinyang He > Signed-off-by: Jinyang He > Signed-off-by: Qing Zhang > --- > arch/loongarch/Kconfig | 2 + > arch/loongarch/Makefile | 5 ++ > arch/loongarch/include/asm/ftrace.h | 18 ++++++ > arch/loongarch/kernel/Makefile | 8 +++ > arch/loongarch/kernel/ftrace.c | 74 +++++++++++++++++++++++ > arch/loongarch/kernel/mcount.S | 94 +++++++++++++++++++++++++++++ > 6 files changed, 201 insertions(+) > create mode 100644 arch/loongarch/include/asm/ftrace.h > create mode 100644 arch/loongarch/kernel/ftrace.c > create mode 100644 arch/loongarch/kernel/mcount.S > > diff --git a/arch/loongarch/Kconfig b/arch/loongarch/Kconfig > index 1943f840e494..92c4ec8c8527 100644 > --- a/arch/loongarch/Kconfig > +++ b/arch/loongarch/Kconfig > @@ -91,6 +91,8 @@ config LOONGARCH > select HAVE_EBPF_JIT > select HAVE_EXIT_THREAD > select HAVE_FAST_GUP > + select HAVE_FUNCTION_GRAPH_TRACER This line should be moved to another patch. > + select HAVE_FUNCTION_TRACER > select HAVE_GENERIC_VDSO > select HAVE_IOREMAP_PROT > select HAVE_IRQ_EXIT_ON_IRQ_STACK > diff --git a/arch/loongarch/Makefile b/arch/loongarch/Makefile > index a0fc1f9980e3..6832a8f891fd 100644 > --- a/arch/loongarch/Makefile > +++ b/arch/loongarch/Makefile > @@ -36,6 +36,11 @@ ifneq ($(SUBARCH),$(ARCH)) > endif > endif > > +ifdef CONFIG_DYNAMIC_FTRACE > +KBUILD_CPPFLAGS += -DCC_USING_PATCHABLE_FUNCTION_ENTRY > +CC_FLAGS_FTRACE := -fpatchable-function-entry=2 > +endif > + > ifdef CONFIG_64BIT > ld-emul = $(64bit-emul) > cflags-y += -mabi=lp64s > diff --git a/arch/loongarch/include/asm/ftrace.h b/arch/loongarch/include/asm/ftrace.h > new file mode 100644 > index 000000000000..6a3e76234618 > --- /dev/null > +++ b/arch/loongarch/include/asm/ftrace.h > @@ -0,0 +1,18 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * Copyright (C) 2022 Loongson Technology Corporation Limited > + */ > + > +#ifndef _ASM_LOONGARCH_FTRACE_H > +#define _ASM_LOONGARCH_FTRACE_H > + > +#ifdef CONFIG_FUNCTION_TRACER > +#define MCOUNT_INSN_SIZE 4 /* sizeof mcount call */ > + > +#ifndef __ASSEMBLY__ > +extern void _mcount(void); > +#define mcount _mcount > + > +#endif /* __ASSEMBLY__ */ > +#endif /* CONFIG_FUNCTION_TRACER */ > +#endif /* _ASM_LOONGARCH_FTRACE_H */ > diff --git a/arch/loongarch/kernel/Makefile b/arch/loongarch/kernel/Makefile > index 86744531b100..3f71bce1c7ce 100644 > --- a/arch/loongarch/kernel/Makefile > +++ b/arch/loongarch/kernel/Makefile > @@ -15,6 +15,14 @@ obj-$(CONFIG_EFI) += efi.o > > obj-$(CONFIG_CPU_HAS_FPU) += fpu.o > > +ifdef CONFIG_FUNCTION_TRACER > +obj-y += mcount.o ftrace.o > +CFLAGS_REMOVE_ftrace.o = $(CC_FLAGS_FTRACE) > +CFLAGS_REMOVE_inst.o = $(CC_FLAGS_FTRACE) > +CFLAGS_REMOVE_time.o = $(CC_FLAGS_FTRACE) > +CFLAGS_REMOVE_perf_event.o = $(CC_FLAGS_FTRACE) > +endif > + > obj-$(CONFIG_MODULES) += module.o module-sections.o > obj-$(CONFIG_STACKTRACE) += stacktrace.o > > diff --git a/arch/loongarch/kernel/ftrace.c b/arch/loongarch/kernel/ftrace.c > new file mode 100644 > index 000000000000..c8ddc5f11f32 > --- /dev/null > +++ b/arch/loongarch/kernel/ftrace.c > @@ -0,0 +1,74 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Copyright (C) 2022 Loongson Technology Corporation Limited > + */ > + > +#include > +#include > +#include > +#include > + > +#include > +#include > +#include > +#include > +#include > +#include > +#include > + > +#include > + > +#ifdef CONFIG_FUNCTION_GRAPH_TRACER This should also be moved to another patch. > + > +/* > + * As `call _mcount` follows LoongArch psABI, ra-saved operation and > + * stack operation can be found before this insn. > + */ > + > +static int ftrace_get_parent_ra_addr(unsigned long insn_addr, int *ra_off) > +{ > + union loongarch_instruction *insn; > + int limit = 32; > + > + insn = (union loongarch_instruction *)insn_addr; > + > + do { > + insn--; > + limit--; > + > + if (is_ra_save_ins(insn)) > + *ra_off = -((1 << 12) - insn->reg2i12_format.immediate); > + > + } while (!is_stack_alloc_ins(insn) && limit); > + > + if (!limit) > + return -EINVAL; > + > + return 0; > +} > + > +void prepare_ftrace_return(unsigned long self_addr, > + unsigned long callsite_sp, unsigned long old) > +{ > + int ra_off; > + unsigned long return_hooker = (unsigned long)&return_to_handler; > + > + if (unlikely(ftrace_graph_is_dead())) > + return; > + > + if (unlikely(atomic_read(¤t->tracing_graph_pause))) > + return; > + > + if (ftrace_get_parent_ra_addr(self_addr, &ra_off)) > + goto out; > + > + if (!function_graph_enter(old, self_addr, 0, NULL)) > + *(unsigned long *)(callsite_sp + ra_off) = return_hooker; > + > + return; > + > +out: > + ftrace_graph_stop(); > + WARN_ON(1); > +} > +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ > diff --git a/arch/loongarch/kernel/mcount.S b/arch/loongarch/kernel/mcount.S > new file mode 100644 > index 000000000000..3de7c2d7fd12 > --- /dev/null > +++ b/arch/loongarch/kernel/mcount.S > @@ -0,0 +1,94 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > +/* > + * LoongArch specific _mcount support > + * > + * Copyright (C) 2022 Loongson Technology Corporation Limited > + */ > + > +#include > +#include > +#include > +#include > + > + .text > + > +#define MCOUNT_STACK_SIZE (2 * SZREG) > +#define MCOUNT_S0_OFFSET (0) > +#define MCOUNT_RA_OFFSET (SZREG) > + > + .macro MCOUNT_SAVE_REGS > + PTR_ADDI sp, sp, -MCOUNT_STACK_SIZE > + PTR_S s0, sp, MCOUNT_S0_OFFSET > + PTR_S ra, sp, MCOUNT_RA_OFFSET > + move s0, a0 > + .endm > + > + .macro MCOUNT_RESTORE_REGS > + move a0, s0 > + PTR_L ra, sp, MCOUNT_RA_OFFSET > + PTR_L s0, sp, MCOUNT_S0_OFFSET > + PTR_ADDI sp, sp, MCOUNT_STACK_SIZE > + .endm > + > + > +SYM_FUNC_START(_mcount) > + la.pcrel t1, ftrace_stub > + la.pcrel t2, ftrace_trace_function /* Prepare t2 for (1) */ > + PTR_L t2, t2, 0 > + beq t1, t2, fgraph_trace > + > + MCOUNT_SAVE_REGS > + > + move a0, ra /* arg0: self return address */ > + move a1, s0 /* arg1: parent's return address */ > + jirl ra, t2, 0 /* (1) call *ftrace_trace_function */ > + > + MCOUNT_RESTORE_REGS > + > +fgraph_trace: > +#ifdef CONFIG_FUNCTION_GRAPH_TRACER The same as above. Huacai > + la.pcrel t1, ftrace_stub > + la.pcrel t3, ftrace_graph_return > + PTR_L t3, t3, 0 > + bne t1, t3, ftrace_graph_caller > + la.pcrel t1, ftrace_graph_entry_stub > + la.pcrel t3, ftrace_graph_entry > + PTR_L t3, t3, 0 > + bne t1, t3, ftrace_graph_caller > +#endif > + > + .globl ftrace_stub > +ftrace_stub: > + jr ra > +SYM_FUNC_END(_mcount) > +EXPORT_SYMBOL(_mcount) > + > +#ifdef CONFIG_FUNCTION_GRAPH_TRACER > +SYM_FUNC_START(ftrace_graph_caller) > + MCOUNT_SAVE_REGS > + > + PTR_ADDI a0, ra, -4 /* arg0: Callsite self return addr */ > + PTR_ADDI a1, sp, MCOUNT_STACK_SIZE /* arg1: Callsite sp */ > + move a2, s0 /* arg2: Callsite parent ra */ > + bl prepare_ftrace_return > + > + MCOUNT_RESTORE_REGS > + jr ra > +SYM_FUNC_END(ftrace_graph_caller) > + > +SYM_FUNC_START(return_to_handler) > + PTR_ADDI sp, sp, -2 * SZREG > + PTR_S a0, sp, 0 > + PTR_S a1, sp, SZREG > + > + bl ftrace_return_to_handler > + > + /* restore the real parent address: a0 -> ra */ > + move ra, a0 > + > + PTR_L a0, sp, 0 > + PTR_L a1, sp, SZREG > + PTR_ADDI sp, sp, 2 * SZREG > + jr ra > +SYM_FUNC_END(return_to_handler) > +#endif /* CONFIG_FUNCTION_GRAPH_TRACER */ > -- > 2.36.0 > >