linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: Ard Biesheuvel <ardb@kernel.org>
To: linux@armlinux.org.uk
Cc: linux-arm-kernel@lists.infradead.org,
	Ard Biesheuvel <ardb@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	Sudeep Holla <sudeep.holla@arm.com>,
	Cristian Marussi <cristian.marussi@arm.com>,
	Nathan Chancellor <nathan@kernel.org>,
	Nick Desaulniers <ndesaulniers@google.com>,
	Arnd Bergmann <arnd@arndb.de>,
	Linus Walleij <linus.walleij@linaro.org>,
	Masami Hiramatsu <mhiramat@kernel.org>
Subject: [PATCH v2 03/12] ARM: ftrace: use trampolines to keep .init.text in branching range
Date: Mon, 31 Jan 2022 18:03:38 +0100	[thread overview]
Message-ID: <20220131170347.381551-4-ardb@kernel.org> (raw)
In-Reply-To: <20220131170347.381551-1-ardb@kernel.org>

Kernel images that are large in comparison to the range of a direct
branch may fail to work as expected with ftrace, as patching a direct
branch to one of the core ftrace routines may not be possible from the
.init.text section, if it is emitted too far away from the normal .text
section.

This is more likely to affect Thumb2 builds, given that its range is
only -/+ 16 MiB (as opposed to ARM which has -/+ 32 MiB), but may occur
in either ISA.

To work around this, add a couple of trampolines to .init.text and
swap these in when the ftrace patching code is operating on callers in
.init.text.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
---
 arch/arm/kernel/entry-ftrace.S | 16 ++++++++++++++
 arch/arm/kernel/ftrace.c       | 23 +++++++++++++++++---
 2 files changed, 36 insertions(+), 3 deletions(-)

diff --git a/arch/arm/kernel/entry-ftrace.S b/arch/arm/kernel/entry-ftrace.S
index dca12a09322a..237d435e29aa 100644
--- a/arch/arm/kernel/entry-ftrace.S
+++ b/arch/arm/kernel/entry-ftrace.S
@@ -270,3 +270,19 @@ ENTRY(ftrace_stub)
 .Lftrace_stub:
 	ret	lr
 ENDPROC(ftrace_stub)
+
+#ifdef CONFIG_DYNAMIC_FTRACE
+
+	__INIT
+
+	.macro	init_tramp, dst:req
+ENTRY(\dst\()_from_init)
+	ldr	pc, =\dst
+ENDPROC(\dst\()_from_init)
+	.endm
+
+	init_tramp	ftrace_caller
+#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
+	init_tramp	ftrace_regs_caller
+#endif
+#endif
diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c
index db72d3a6522d..cb9eb8a463c5 100644
--- a/arch/arm/kernel/ftrace.c
+++ b/arch/arm/kernel/ftrace.c
@@ -51,9 +51,20 @@ static unsigned long ftrace_nop_replace(struct dyn_ftrace *rec)
 	return NOP;
 }
 
-static unsigned long adjust_address(struct dyn_ftrace *rec, unsigned long addr)
+void ftrace_caller_from_init(void);
+void ftrace_regs_caller_from_init(void);
+
+static unsigned long __ref adjust_address(struct dyn_ftrace *rec,
+					  unsigned long addr)
 {
-	return addr;
+	if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE) ||
+	    system_state >= SYSTEM_FREEING_INITMEM ||
+	    likely(!is_kernel_inittext(rec->ip)))
+		return addr;
+	if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) ||
+	    addr == (unsigned long)&ftrace_caller)
+		return (unsigned long)&ftrace_caller_from_init;
+	return (unsigned long)&ftrace_regs_caller_from_init;
 }
 
 int ftrace_arch_code_modify_prepare(void)
@@ -189,7 +200,13 @@ int ftrace_make_nop(struct module *mod,
 #endif
 
 	new = ftrace_nop_replace(rec);
-	ret = ftrace_modify_code(ip, old, new, true);
+	/*
+	 * Locations in .init.text may call __gnu_mcount_mc via a linker
+	 * emitted veneer if they are too far away from its implementation, and
+	 * so validation may fail spuriously in such cases. Let's work around
+	 * this by omitting those from validation.
+	 */
+	ret = ftrace_modify_code(ip, old, new, !is_kernel_inittext(ip));
 
 	return ret;
 }
-- 
2.30.2


_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

  parent reply	other threads:[~2022-01-31 17:18 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-31 17:03 [PATCH v2 00/12] ARM: ftrace fixes and cleanups Ard Biesheuvel
2022-01-31 17:03 ` [PATCH v2 01/12] ARM: ftrace: ensure that ADR take Thumb bit into account Ard Biesheuvel
2022-01-31 17:03 ` [PATCH v2 02/12] ARM: ftrace: use ADD not POP to counter PUSH at entry Ard Biesheuvel
2022-01-31 17:03 ` Ard Biesheuvel [this message]
2022-01-31 17:03 ` [PATCH v2 04/12] ARM: ftrace: avoid redundant loads or clobbering IP Ard Biesheuvel
2022-01-31 17:03 ` [PATCH v2 05/12] ARM: ftrace: avoid unnecessary literal loads Ard Biesheuvel
2022-01-31 17:03 ` [PATCH v2 06/12] ARM: ftrace: enable HAVE_FUNCTION_GRAPH_FP_TEST Ard Biesheuvel
2022-01-31 17:03 ` [PATCH v2 07/12] ARM: unwind: track location of LR value in stack frame Ard Biesheuvel
2022-01-31 17:03 ` [PATCH v2 08/12] ARM: ftrace: enable the graph tracer with the EABI unwinder Ard Biesheuvel
2022-01-31 17:03 ` [PATCH v2 09/12] ARM: kprobes: treat R7 as the frame pointer register in Thumb2 builds Ard Biesheuvel
2022-01-31 18:31   ` Nick Desaulniers
2022-02-01  7:42     ` Ard Biesheuvel
2022-02-01 13:18   ` Masami Hiramatsu
2022-02-01 14:05     ` Ard Biesheuvel
2022-02-02  6:10       ` Masami Hiramatsu
2022-02-02  8:00         ` Ard Biesheuvel
2022-01-31 17:03 ` [PATCH v2 10/12] drivers/firmware/scmi: disable ftrace for Clang " Ard Biesheuvel
2022-01-31 18:37   ` Nick Desaulniers
2022-02-01  8:12     ` Ard Biesheuvel
2022-01-31 22:04   ` Sudeep Holla
2022-01-31 17:03 ` [PATCH v2 11/12] ARM: cacheflush: avoid clobbering the frame pointer in Thumb2 mode Ard Biesheuvel
2022-01-31 18:40   ` Nick Desaulniers
2022-01-31 17:03 ` [PATCH v2 12/12] Revert "ARM: 9144/1: forbid ftrace with clang and thumb2_kernel" Ard Biesheuvel
2022-01-31 18:42   ` Nick Desaulniers
2022-01-31 17:21 ` [PATCH v2 00/12] ARM: ftrace fixes and cleanups Steven Rostedt

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220131170347.381551-4-ardb@kernel.org \
    --to=ardb@kernel.org \
    --cc=arnd@arndb.de \
    --cc=cristian.marussi@arm.com \
    --cc=linus.walleij@linaro.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux@armlinux.org.uk \
    --cc=mhiramat@kernel.org \
    --cc=nathan@kernel.org \
    --cc=ndesaulniers@google.com \
    --cc=rostedt@goodmis.org \
    --cc=sudeep.holla@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).