Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit be0f272b authored by Ard Biesheuvel's avatar Ard Biesheuvel Committed by Will Deacon
Browse files

arm64: ftrace: emit ftrace-mod.o contents through code



When building the arm64 kernel with both CONFIG_ARM64_MODULE_PLTS and
CONFIG_DYNAMIC_FTRACE enabled, the ftrace-mod.o object file is built
with the kernel and contains a trampoline that is linked into each
module, so that modules can be loaded far away from the kernel and
still reach the ftrace entry point in the core kernel with an ordinary
relative branch, as is emitted by the compiler instrumentation code
dynamic ftrace relies on.

In order to be able to build out of tree modules, this object file
needs to be included into the linux-headers or linux-devel packages,
which is undesirable, as it makes arm64 a special case (although a
precedent does exist for 32-bit PPC).

Given that the trampoline essentially consists of a PLT entry, let's
not bother with a source or object file for it, and simply patch it
in whenever the trampoline is being populated, using the existing
PLT support routines.

Cc: <stable@vger.kernel.org>
Signed-off-by: default avatarArd Biesheuvel <ard.biesheuvel@linaro.org>
Signed-off-by: default avatarWill Deacon <will.deacon@arm.com>
parent 7e8b9c1d
Loading
Loading
Loading
Loading
+0 −3
Original line number Diff line number Diff line
@@ -83,9 +83,6 @@ endif

ifeq ($(CONFIG_ARM64_MODULE_PLTS),y)
KBUILD_LDFLAGS_MODULE	+= -T $(srctree)/arch/arm64/kernel/module.lds
ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
KBUILD_LDFLAGS_MODULE	+= $(objtree)/arch/arm64/kernel/ftrace-mod.o
endif
endif

# Default value
+1 −1
Original line number Diff line number Diff line
@@ -32,7 +32,7 @@ struct mod_arch_specific {
	struct mod_plt_sec	init;

	/* for CONFIG_DYNAMIC_FTRACE */
	void			*ftrace_trampoline;
	struct plt_entry 	*ftrace_trampoline;
};
#endif

+0 −3
Original line number Diff line number Diff line
@@ -61,6 +61,3 @@ extra-y += $(head-y) vmlinux.lds
ifeq ($(CONFIG_DEBUG_EFI),y)
AFLAGS_head.o += -DVMLINUX_PATH="\"$(realpath $(objtree)/vmlinux)\""
endif

# will be included by each individual module but not by the core kernel itself
extra-$(CONFIG_DYNAMIC_FTRACE) += ftrace-mod.o

arch/arm64/kernel/ftrace-mod.S

deleted100644 → 0
+0 −18
Original line number Diff line number Diff line
/*
 * Copyright (C) 2017 Linaro Ltd <ard.biesheuvel@linaro.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#include <linux/linkage.h>
#include <asm/assembler.h>

	.section	".text.ftrace_trampoline", "ax"
	.align		3
0:	.quad		0
__ftrace_trampoline:
	ldr		x16, 0b
	br		x16
ENDPROC(__ftrace_trampoline)
+8 −6
Original line number Diff line number Diff line
@@ -76,7 +76,7 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)

	if (offset < -SZ_128M || offset >= SZ_128M) {
#ifdef CONFIG_ARM64_MODULE_PLTS
		unsigned long *trampoline;
		struct plt_entry trampoline;
		struct module *mod;

		/*
@@ -104,22 +104,24 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
		 * is added in the future, but for now, the pr_err() below
		 * deals with a theoretical issue only.
		 */
		trampoline = (unsigned long *)mod->arch.ftrace_trampoline;
		if (trampoline[0] != addr) {
			if (trampoline[0] != 0) {
		trampoline = get_plt_entry(addr);
		if (!plt_entries_equal(mod->arch.ftrace_trampoline,
				       &trampoline)) {
			if (!plt_entries_equal(mod->arch.ftrace_trampoline,
					       &(struct plt_entry){})) {
				pr_err("ftrace: far branches to multiple entry points unsupported inside a single module\n");
				return -EINVAL;
			}

			/* point the trampoline to our ftrace entry point */
			module_disable_ro(mod);
			trampoline[0] = addr;
			*mod->arch.ftrace_trampoline = trampoline;
			module_enable_ro(mod, true);

			/* update trampoline before patching in the branch */
			smp_wmb();
		}
		addr = (unsigned long)&trampoline[1];
		addr = (unsigned long)(void *)mod->arch.ftrace_trampoline;
#else /* CONFIG_ARM64_MODULE_PLTS */
		return -EINVAL;
#endif /* CONFIG_ARM64_MODULE_PLTS */
Loading