Skip to content

Commit 1f12fb2

Browse files
asverdlinRussell King (Oracle)
authored andcommitted
ARM: 9079/1: ftrace: Add MODULE_PLTS support
Teach ftrace_make_call() and ftrace_make_nop() about PLTs. Teach PLT code about FTRACE and all its callbacks. Otherwise the following might happen: ------------[ cut here ]------------ WARNING: CPU: 14 PID: 2265 at .../arch/arm/kernel/insn.c:14 __arm_gen_branch+0x83/0x8c() ... Hardware name: LSI Axxia AXM55XX [<c0314a49>] (unwind_backtrace) from [<c03115e9>] (show_stack+0x11/0x14) [<c03115e9>] (show_stack) from [<c0519f51>] (dump_stack+0x81/0xa8) [<c0519f51>] (dump_stack) from [<c032185d>] (warn_slowpath_common+0x69/0x90) [<c032185d>] (warn_slowpath_common) from [<c03218f3>] (warn_slowpath_null+0x17/0x1c) [<c03218f3>] (warn_slowpath_null) from [<c03143cf>] (__arm_gen_branch+0x83/0x8c) [<c03143cf>] (__arm_gen_branch) from [<c0314337>] (ftrace_make_nop+0xf/0x24) [<c0314337>] (ftrace_make_nop) from [<c038ebcb>] (ftrace_process_locs+0x27b/0x3e8) [<c038ebcb>] (ftrace_process_locs) from [<c0378d79>] (load_module+0x11e9/0x1a44) [<c0378d79>] (load_module) from [<c037974d>] (SyS_finit_module+0x59/0x84) [<c037974d>] (SyS_finit_module) from [<c030e981>] (ret_fast_syscall+0x1/0x18) ---[ end trace e1b64ced7a89adcc ]--- ------------[ cut here ]------------ WARNING: CPU: 14 PID: 2265 at .../kernel/trace/ftrace.c:1979 ftrace_bug+0x1b1/0x234() ... Hardware name: LSI Axxia AXM55XX [<c0314a49>] (unwind_backtrace) from [<c03115e9>] (show_stack+0x11/0x14) [<c03115e9>] (show_stack) from [<c0519f51>] (dump_stack+0x81/0xa8) [<c0519f51>] (dump_stack) from [<c032185d>] (warn_slowpath_common+0x69/0x90) [<c032185d>] (warn_slowpath_common) from [<c03218f3>] (warn_slowpath_null+0x17/0x1c) [<c03218f3>] (warn_slowpath_null) from [<c038e87d>] (ftrace_bug+0x1b1/0x234) [<c038e87d>] (ftrace_bug) from [<c038ebd5>] (ftrace_process_locs+0x285/0x3e8) [<c038ebd5>] (ftrace_process_locs) from [<c0378d79>] (load_module+0x11e9/0x1a44) [<c0378d79>] (load_module) from [<c037974d>] (SyS_finit_module+0x59/0x84) [<c037974d>] (SyS_finit_module) from [<c030e981>] (ret_fast_syscall+0x1/0x18) ---[ end trace e1b64ced7a89adcd ]--- ftrace failed to modify [<e9ef7006>] 0xe9ef7006 actual: 02:f0:3b:fa ftrace record flags: 0 (0) expected tramp: c0314265 Signed-off-by: Alexander Sverdlin <alexander.sverdlin@nokia.com> Signed-off-by: Russell King <rmk+kernel@armlinux.org.uk>
1 parent 27e331a commit 1f12fb2

File tree

4 files changed

+82
-12
lines changed

4 files changed

+82
-12
lines changed

arch/arm/include/asm/ftrace.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ extern void __gnu_mcount_nc(void);
1515

1616
#ifdef CONFIG_DYNAMIC_FTRACE
1717
struct dyn_arch_ftrace {
18+
#ifdef CONFIG_ARM_MODULE_PLTS
19+
struct module *mod;
20+
#endif
1821
};
1922

2023
static inline unsigned long ftrace_call_adjust(unsigned long addr)

arch/arm/include/asm/module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ struct plt_entries {
3030

3131
struct mod_plt_sec {
3232
struct elf32_shdr *plt;
33+
struct plt_entries *plt_ent;
3334
int plt_count;
3435
};
3536

arch/arm/kernel/ftrace.c

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,9 +68,10 @@ int ftrace_arch_code_modify_post_process(void)
6868
return 0;
6969
}
7070

71-
static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr)
71+
static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr,
72+
bool warn)
7273
{
73-
return arm_gen_branch_link(pc, addr, true);
74+
return arm_gen_branch_link(pc, addr, warn);
7475
}
7576

7677
static int ftrace_modify_code(unsigned long pc, unsigned long old,
@@ -104,14 +105,14 @@ int ftrace_update_ftrace_func(ftrace_func_t func)
104105
int ret;
105106

106107
pc = (unsigned long)&ftrace_call;
107-
new = ftrace_call_replace(pc, (unsigned long)func);
108+
new = ftrace_call_replace(pc, (unsigned long)func, true);
108109

109110
ret = ftrace_modify_code(pc, 0, new, false);
110111

111112
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
112113
if (!ret) {
113114
pc = (unsigned long)&ftrace_regs_call;
114-
new = ftrace_call_replace(pc, (unsigned long)func);
115+
new = ftrace_call_replace(pc, (unsigned long)func, true);
115116

116117
ret = ftrace_modify_code(pc, 0, new, false);
117118
}
@@ -124,10 +125,22 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
124125
{
125126
unsigned long new, old;
126127
unsigned long ip = rec->ip;
128+
unsigned long aaddr = adjust_address(rec, addr);
129+
struct module *mod = NULL;
130+
131+
#ifdef CONFIG_ARM_MODULE_PLTS
132+
mod = rec->arch.mod;
133+
#endif
127134

128135
old = ftrace_nop_replace(rec);
129136

130-
new = ftrace_call_replace(ip, adjust_address(rec, addr));
137+
new = ftrace_call_replace(ip, aaddr, !mod);
138+
#ifdef CONFIG_ARM_MODULE_PLTS
139+
if (!new && mod) {
140+
aaddr = get_module_plt(mod, ip, aaddr);
141+
new = ftrace_call_replace(ip, aaddr, true);
142+
}
143+
#endif
131144

132145
return ftrace_modify_code(rec->ip, old, new, true);
133146
}
@@ -140,9 +153,9 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
140153
unsigned long new, old;
141154
unsigned long ip = rec->ip;
142155

143-
old = ftrace_call_replace(ip, adjust_address(rec, old_addr));
156+
old = ftrace_call_replace(ip, adjust_address(rec, old_addr), true);
144157

145-
new = ftrace_call_replace(ip, adjust_address(rec, addr));
158+
new = ftrace_call_replace(ip, adjust_address(rec, addr), true);
146159

147160
return ftrace_modify_code(rec->ip, old, new, true);
148161
}
@@ -152,12 +165,29 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
152165
int ftrace_make_nop(struct module *mod,
153166
struct dyn_ftrace *rec, unsigned long addr)
154167
{
168+
unsigned long aaddr = adjust_address(rec, addr);
155169
unsigned long ip = rec->ip;
156170
unsigned long old;
157171
unsigned long new;
158172
int ret;
159173

160-
old = ftrace_call_replace(ip, adjust_address(rec, addr));
174+
#ifdef CONFIG_ARM_MODULE_PLTS
175+
/* mod is only supplied during module loading */
176+
if (!mod)
177+
mod = rec->arch.mod;
178+
else
179+
rec->arch.mod = mod;
180+
#endif
181+
182+
old = ftrace_call_replace(ip, aaddr,
183+
!IS_ENABLED(CONFIG_ARM_MODULE_PLTS) || !mod);
184+
#ifdef CONFIG_ARM_MODULE_PLTS
185+
if (!old && mod) {
186+
aaddr = get_module_plt(mod, ip, aaddr);
187+
old = ftrace_call_replace(ip, aaddr, true);
188+
}
189+
#endif
190+
161191
new = ftrace_nop_replace(rec);
162192
ret = ftrace_modify_code(ip, old, new, true);
163193

arch/arm/kernel/module-plts.c

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
*/
55

66
#include <linux/elf.h>
7+
#include <linux/ftrace.h>
78
#include <linux/kernel.h>
89
#include <linux/module.h>
910
#include <linux/sort.h>
@@ -20,19 +21,52 @@
2021
(PLT_ENT_STRIDE - 8))
2122
#endif
2223

24+
static const u32 fixed_plts[] = {
25+
#ifdef CONFIG_FUNCTION_TRACER
26+
FTRACE_ADDR,
27+
MCOUNT_ADDR,
28+
#endif
29+
};
30+
2331
static bool in_init(const struct module *mod, unsigned long loc)
2432
{
2533
return loc - (u32)mod->init_layout.base < mod->init_layout.size;
2634
}
2735

36+
static void prealloc_fixed(struct mod_plt_sec *pltsec, struct plt_entries *plt)
37+
{
38+
int i;
39+
40+
if (!ARRAY_SIZE(fixed_plts) || pltsec->plt_count)
41+
return;
42+
pltsec->plt_count = ARRAY_SIZE(fixed_plts);
43+
44+
for (i = 0; i < ARRAY_SIZE(plt->ldr); ++i)
45+
plt->ldr[i] = PLT_ENT_LDR;
46+
47+
BUILD_BUG_ON(sizeof(fixed_plts) > sizeof(plt->lit));
48+
memcpy(plt->lit, fixed_plts, sizeof(fixed_plts));
49+
}
50+
2851
u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val)
2952
{
3053
struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
3154
&mod->arch.init;
55+
struct plt_entries *plt;
56+
int idx;
57+
58+
/* cache the address, ELF header is available only during module load */
59+
if (!pltsec->plt_ent)
60+
pltsec->plt_ent = (struct plt_entries *)pltsec->plt->sh_addr;
61+
plt = pltsec->plt_ent;
3262

33-
struct plt_entries *plt = (struct plt_entries *)pltsec->plt->sh_addr;
34-
int idx = 0;
63+
prealloc_fixed(pltsec, plt);
64+
65+
for (idx = 0; idx < ARRAY_SIZE(fixed_plts); ++idx)
66+
if (plt->lit[idx] == val)
67+
return (u32)&plt->ldr[idx];
3568

69+
idx = 0;
3670
/*
3771
* Look for an existing entry pointing to 'val'. Given that the
3872
* relocations are sorted, this will be the last entry we allocated.
@@ -180,8 +214,8 @@ static unsigned int count_plts(const Elf32_Sym *syms, Elf32_Addr base,
180214
int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
181215
char *secstrings, struct module *mod)
182216
{
183-
unsigned long core_plts = 0;
184-
unsigned long init_plts = 0;
217+
unsigned long core_plts = ARRAY_SIZE(fixed_plts);
218+
unsigned long init_plts = ARRAY_SIZE(fixed_plts);
185219
Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
186220
Elf32_Sym *syms = NULL;
187221

@@ -236,13 +270,15 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
236270
mod->arch.core.plt->sh_size = round_up(core_plts * PLT_ENT_SIZE,
237271
sizeof(struct plt_entries));
238272
mod->arch.core.plt_count = 0;
273+
mod->arch.core.plt_ent = NULL;
239274

240275
mod->arch.init.plt->sh_type = SHT_NOBITS;
241276
mod->arch.init.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
242277
mod->arch.init.plt->sh_addralign = L1_CACHE_BYTES;
243278
mod->arch.init.plt->sh_size = round_up(init_plts * PLT_ENT_SIZE,
244279
sizeof(struct plt_entries));
245280
mod->arch.init.plt_count = 0;
281+
mod->arch.init.plt_ent = NULL;
246282

247283
pr_debug("%s: plt=%x, init.plt=%x\n", __func__,
248284
mod->arch.core.plt->sh_size, mod->arch.init.plt->sh_size);

0 commit comments

Comments
 (0)