Skip to content

Commit be0f272

Browse files
Ard Biesheuvelwildea01
authored andcommitted
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: Ard Biesheuvel <ard.biesheuvel@linaro.org> Signed-off-by: Will Deacon <will.deacon@arm.com>
1 parent 7e8b9c1 commit be0f272

7 files changed

Lines changed: 22 additions & 31 deletions

File tree

‎arch/arm64/Makefile‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ endif
8383

8484
ifeq ($(CONFIG_ARM64_MODULE_PLTS),y)
8585
KBUILD_LDFLAGS_MODULE += -T $(srctree)/arch/arm64/kernel/module.lds
86-
ifeq ($(CONFIG_DYNAMIC_FTRACE),y)
87-
KBUILD_LDFLAGS_MODULE += $(objtree)/arch/arm64/kernel/ftrace-mod.o
88-
endif
8986
endif
9087

9188
# Default value

‎arch/arm64/include/asm/module.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ struct mod_arch_specific {
3232
struct mod_plt_sec init;
3333

3434
/* for CONFIG_DYNAMIC_FTRACE */
35-
void *ftrace_trampoline;
35+
struct plt_entry *ftrace_trampoline;
3636
};
3737
#endif
3838

‎arch/arm64/kernel/Makefile‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,3 @@ extra-y += $(head-y) vmlinux.lds
6161
ifeq ($(CONFIG_DEBUG_EFI),y)
6262
AFLAGS_head.o += -DVMLINUX_PATH="\"$(realpath $(objtree)/vmlinux)\""
6363
endif
64-
65-
# will be included by each individual module but not by the core kernel itself
66-
extra-$(CONFIG_DYNAMIC_FTRACE) += ftrace-mod.o

‎arch/arm64/kernel/ftrace-mod.S‎

Lines changed: 0 additions & 18 deletions
This file was deleted.

‎arch/arm64/kernel/ftrace.c‎

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
7676

7777
if (offset < -SZ_128M || offset >= SZ_128M) {
7878
#ifdef CONFIG_ARM64_MODULE_PLTS
79-
unsigned long *trampoline;
79+
struct plt_entry trampoline;
8080
struct module *mod;
8181

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

114116
/* point the trampoline to our ftrace entry point */
115117
module_disable_ro(mod);
116-
trampoline[0] = addr;
118+
*mod->arch.ftrace_trampoline = trampoline;
117119
module_enable_ro(mod, true);
118120

119121
/* update trampoline before patching in the branch */
120122
smp_wmb();
121123
}
122-
addr = (unsigned long)&trampoline[1];
124+
addr = (unsigned long)(void *)mod->arch.ftrace_trampoline;
123125
#else /* CONFIG_ARM64_MODULE_PLTS */
124126
return -EINVAL;
125127
#endif /* CONFIG_ARM64_MODULE_PLTS */

‎arch/arm64/kernel/module-plts.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
120120
unsigned long core_plts = 0;
121121
unsigned long init_plts = 0;
122122
Elf64_Sym *syms = NULL;
123+
Elf_Shdr *tramp = NULL;
123124
int i;
124125

125126
/*
@@ -131,6 +132,10 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
131132
mod->arch.core.plt = sechdrs + i;
132133
else if (!strcmp(secstrings + sechdrs[i].sh_name, ".init.plt"))
133134
mod->arch.init.plt = sechdrs + i;
135+
else if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE) &&
136+
!strcmp(secstrings + sechdrs[i].sh_name,
137+
".text.ftrace_trampoline"))
138+
tramp = sechdrs + i;
134139
else if (sechdrs[i].sh_type == SHT_SYMTAB)
135140
syms = (Elf64_Sym *)sechdrs[i].sh_addr;
136141
}
@@ -181,5 +186,12 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
181186
mod->arch.init.plt_num_entries = 0;
182187
mod->arch.init.plt_max_entries = init_plts;
183188

189+
if (tramp) {
190+
tramp->sh_type = SHT_NOBITS;
191+
tramp->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
192+
tramp->sh_addralign = __alignof__(struct plt_entry);
193+
tramp->sh_size = sizeof(struct plt_entry);
194+
}
195+
184196
return 0;
185197
}

‎arch/arm64/kernel/module.lds‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
SECTIONS {
22
.plt (NOLOAD) : { BYTE(0) }
33
.init.plt (NOLOAD) : { BYTE(0) }
4+
.text.ftrace_trampoline (NOLOAD) : { BYTE(0) }
45
}

0 commit comments

Comments
 (0)