Skip to content

Commit f1a54ae

Browse files
author
Mark Rutland
committed
arm64: module/ftrace: intialize PLT at load time
Currently we lazily-initialize a module's ftrace PLT at runtime when we install the first ftrace call. To do so we have to apply a number of sanity checks, transiently mark the module text as RW, and perform an IPI as part of handling Neoverse-N1 erratum #1542419. We only expect the ftrace trampoline to point at ftrace_caller() (AKA FTRACE_ADDR), so let's simplify all of this by intializing the PLT at module load time, before the module loader marks the module RO and performs the intial I-cache maintenance for the module. Thus we can rely on the module having been correctly intialized, and can simplify the runtime work necessary to install an ftrace call in a module. This will also allow for the removal of module_disable_ro(). Tested by forcing ftrace_make_call() to use the module PLT, and then loading up a module after setting up ftrace with: | echo ":mod:<module-name>" > set_ftrace_filter; | echo function > current_tracer; | modprobe <module-name> Since FTRACE_ADDR is only defined when CONFIG_DYNAMIC_FTRACE is selected, we wrap its use along with most of module_init_ftrace_plt() with ifdeffery rather than using IS_ENABLED(). Signed-off-by: Mark Rutland <mark.rutland@arm.com> Reviewed-by: Amit Daniel Kachhap <amit.kachhap@arm.com> Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Reviewed-by: Torsten Duwe <duwe@suse.de> Tested-by: Amit Daniel Kachhap <amit.kachhap@arm.com> Tested-by: Torsten Duwe <duwe@suse.de> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: James Morse <james.morse@arm.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Will Deacon <will@kernel.org>
1 parent bd8b21d commit f1a54ae

File tree

2 files changed

+35
-52
lines changed

2 files changed

+35
-52
lines changed

arch/arm64/kernel/ftrace.c

+14-41
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,21 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
7373

7474
if (offset < -SZ_128M || offset >= SZ_128M) {
7575
#ifdef CONFIG_ARM64_MODULE_PLTS
76-
struct plt_entry trampoline, *dst;
7776
struct module *mod;
7877

78+
/*
79+
* There is only one ftrace trampoline per module. For now,
80+
* this is not a problem since on arm64, all dynamic ftrace
81+
* invocations are routed via ftrace_caller(). This will need
82+
* to be revisited if support for multiple ftrace entry points
83+
* is added in the future, but for now, the pr_err() below
84+
* deals with a theoretical issue only.
85+
*/
86+
if (addr != FTRACE_ADDR) {
87+
pr_err("ftrace: far branches to multiple entry points unsupported inside a single module\n");
88+
return -EINVAL;
89+
}
90+
7991
/*
8092
* On kernels that support module PLTs, the offset between the
8193
* branch instruction and its target may legally exceed the
@@ -93,46 +105,7 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
93105
if (WARN_ON(!mod))
94106
return -EINVAL;
95107

96-
/*
97-
* There is only one ftrace trampoline per module. For now,
98-
* this is not a problem since on arm64, all dynamic ftrace
99-
* invocations are routed via ftrace_caller(). This will need
100-
* to be revisited if support for multiple ftrace entry points
101-
* is added in the future, but for now, the pr_err() below
102-
* deals with a theoretical issue only.
103-
*
104-
* Note that PLTs are place relative, and plt_entries_equal()
105-
* checks whether they point to the same target. Here, we need
106-
* to check if the actual opcodes are in fact identical,
107-
* regardless of the offset in memory so use memcmp() instead.
108-
*/
109-
dst = mod->arch.ftrace_trampoline;
110-
trampoline = get_plt_entry(addr, dst);
111-
if (memcmp(dst, &trampoline, sizeof(trampoline))) {
112-
if (plt_entry_is_initialized(dst)) {
113-
pr_err("ftrace: far branches to multiple entry points unsupported inside a single module\n");
114-
return -EINVAL;
115-
}
116-
117-
/* point the trampoline to our ftrace entry point */
118-
module_disable_ro(mod);
119-
*dst = trampoline;
120-
module_enable_ro(mod, true);
121-
122-
/*
123-
* Ensure updated trampoline is visible to instruction
124-
* fetch before we patch in the branch. Although the
125-
* architecture doesn't require an IPI in this case,
126-
* Neoverse-N1 erratum #1542419 does require one
127-
* if the TLB maintenance in module_enable_ro() is
128-
* skipped due to rodata_enabled. It doesn't seem worth
129-
* it to make it conditional given that this is
130-
* certainly not a fast-path.
131-
*/
132-
flush_icache_range((unsigned long)&dst[0],
133-
(unsigned long)&dst[1]);
134-
}
135-
addr = (unsigned long)dst;
108+
addr = (unsigned long)mod->arch.ftrace_trampoline;
136109
#else /* CONFIG_ARM64_MODULE_PLTS */
137110
return -EINVAL;
138111
#endif /* CONFIG_ARM64_MODULE_PLTS */

arch/arm64/kernel/module.c

+21-11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <linux/bitops.h>
1111
#include <linux/elf.h>
12+
#include <linux/ftrace.h>
1213
#include <linux/gfp.h>
1314
#include <linux/kasan.h>
1415
#include <linux/kernel.h>
@@ -485,24 +486,33 @@ static const Elf_Shdr *find_section(const Elf_Ehdr *hdr,
485486
return NULL;
486487
}
487488

489+
static int module_init_ftrace_plt(const Elf_Ehdr *hdr,
490+
const Elf_Shdr *sechdrs,
491+
struct module *mod)
492+
{
493+
#if defined(CONFIG_ARM64_MODULE_PLTS) && defined(CONFIG_DYNAMIC_FTRACE)
494+
const Elf_Shdr *s;
495+
struct plt_entry *plt;
496+
497+
s = find_section(hdr, sechdrs, ".text.ftrace_trampoline");
498+
if (!s)
499+
return -ENOEXEC;
500+
501+
plt = (void *)s->sh_addr;
502+
*plt = get_plt_entry(FTRACE_ADDR, plt);
503+
mod->arch.ftrace_trampoline = plt;
504+
#endif
505+
return 0;
506+
}
507+
488508
int module_finalize(const Elf_Ehdr *hdr,
489509
const Elf_Shdr *sechdrs,
490510
struct module *me)
491511
{
492512
const Elf_Shdr *s;
493-
494513
s = find_section(hdr, sechdrs, ".altinstructions");
495514
if (s)
496515
apply_alternatives_module((void *)s->sh_addr, s->sh_size);
497516

498-
#ifdef CONFIG_ARM64_MODULE_PLTS
499-
if (IS_ENABLED(CONFIG_DYNAMIC_FTRACE)) {
500-
s = find_section(hdr, sechdrs, ".text.ftrace_trampoline");
501-
if (!s)
502-
return -ENOEXEC;
503-
me->arch.ftrace_trampoline = (void *)s->sh_addr;
504-
}
505-
#endif
506-
507-
return 0;
517+
return module_init_ftrace_plt(hdr, sechdrs, me);
508518
}

0 commit comments

Comments
 (0)