Re: [PATCH v3 2/2] ARM: ftrace: Add MODULE_PLTS support
On 9 March 2018 at 08:54, Alexander Sverdlinwrote: > Hello Ard! > > On 09/03/18 09:50, Ard Biesheuvel wrote: >>> +static u32 fixed_plts[] = { >>> + FTRACE_ADDR, >>> + MCOUNT_ADDR, >>> +#ifdef CONFIG_OLD_MCOUNT >>> + (unsigned long)ftrace_caller_old, >>> + (unsigned long)mcount, >>> +#endif >>> +}; >>> + >> Do we need this stuff in the PLT loader if ftrace is disabled? > > You are right, it probably would not even build w/o FTRACE. > I'll fix it, thanks for looking into this! > Also, perhaps you can make the array const (after you put #ifdef's around all entries) >>> static bool in_init(const struct module *mod, unsigned long loc) >>> { >>> return loc - (u32)mod->init_layout.base < mod->init_layout.size; >>> @@ -31,26 +41,43 @@ u32 get_module_plt(struct module *mod, unsigned long >>> loc, Elf32_Addr val) >>> { >>> struct mod_plt_sec *pltsec = !in_init(mod, loc) ? >arch.core : >>> >arch.init; >>> + int idx; >>> + struct plt_entries *plt; >>> + >>> + /* Pre-allocate entries in the first plt */ >>> + if (!pltsec->plt_count) { >>> + plt = (struct plt_entries *)pltsec->plt->sh_addr; >>> + for (idx = 0; idx < ARRAY_SIZE(plt->ldr); ++idx) >>> + plt->ldr[idx] = PLT_ENT_LDR; >>> + memcpy(plt->lit, fixed_plts, sizeof(fixed_plts)); >>> + pltsec->plt_count = ARRAY_SIZE(fixed_plts); >>> + /* >>> +* cache the address, >>> +* ELF header is available only during module load >>> +*/ >>> + pltsec->plt_ent = plt; >>> + } >>> + plt = pltsec->plt_ent; >>> >>> - struct plt_entries *plt = (struct plt_entries >>> *)pltsec->plt->sh_addr; >>> - int idx = 0; >>> + idx = ARRAY_SIZE(fixed_plts); >>> + while (idx) >>> + if (plt->lit[--idx] == val) >>> + return (u32)>ldr[idx]; >>> ... and put all this handling in a helper function that only gets called if ARRAY_SIZE() > 0 AFAICT, that should allow you to leave the rest unchanged.
Re: [PATCH v3 2/2] ARM: ftrace: Add MODULE_PLTS support
On 9 March 2018 at 08:54, Alexander Sverdlin wrote: > Hello Ard! > > On 09/03/18 09:50, Ard Biesheuvel wrote: >>> +static u32 fixed_plts[] = { >>> + FTRACE_ADDR, >>> + MCOUNT_ADDR, >>> +#ifdef CONFIG_OLD_MCOUNT >>> + (unsigned long)ftrace_caller_old, >>> + (unsigned long)mcount, >>> +#endif >>> +}; >>> + >> Do we need this stuff in the PLT loader if ftrace is disabled? > > You are right, it probably would not even build w/o FTRACE. > I'll fix it, thanks for looking into this! > Also, perhaps you can make the array const (after you put #ifdef's around all entries) >>> static bool in_init(const struct module *mod, unsigned long loc) >>> { >>> return loc - (u32)mod->init_layout.base < mod->init_layout.size; >>> @@ -31,26 +41,43 @@ u32 get_module_plt(struct module *mod, unsigned long >>> loc, Elf32_Addr val) >>> { >>> struct mod_plt_sec *pltsec = !in_init(mod, loc) ? >arch.core : >>> >arch.init; >>> + int idx; >>> + struct plt_entries *plt; >>> + >>> + /* Pre-allocate entries in the first plt */ >>> + if (!pltsec->plt_count) { >>> + plt = (struct plt_entries *)pltsec->plt->sh_addr; >>> + for (idx = 0; idx < ARRAY_SIZE(plt->ldr); ++idx) >>> + plt->ldr[idx] = PLT_ENT_LDR; >>> + memcpy(plt->lit, fixed_plts, sizeof(fixed_plts)); >>> + pltsec->plt_count = ARRAY_SIZE(fixed_plts); >>> + /* >>> +* cache the address, >>> +* ELF header is available only during module load >>> +*/ >>> + pltsec->plt_ent = plt; >>> + } >>> + plt = pltsec->plt_ent; >>> >>> - struct plt_entries *plt = (struct plt_entries >>> *)pltsec->plt->sh_addr; >>> - int idx = 0; >>> + idx = ARRAY_SIZE(fixed_plts); >>> + while (idx) >>> + if (plt->lit[--idx] == val) >>> + return (u32)>ldr[idx]; >>> ... and put all this handling in a helper function that only gets called if ARRAY_SIZE() > 0 AFAICT, that should allow you to leave the rest unchanged.
Re: [PATCH v3 2/2] ARM: ftrace: Add MODULE_PLTS support
Hello Ard! On 09/03/18 09:50, Ard Biesheuvel wrote: >> +static u32 fixed_plts[] = { >> + FTRACE_ADDR, >> + MCOUNT_ADDR, >> +#ifdef CONFIG_OLD_MCOUNT >> + (unsigned long)ftrace_caller_old, >> + (unsigned long)mcount, >> +#endif >> +}; >> + > Do we need this stuff in the PLT loader if ftrace is disabled? You are right, it probably would not even build w/o FTRACE. I'll fix it, thanks for looking into this! >> static bool in_init(const struct module *mod, unsigned long loc) >> { >> return loc - (u32)mod->init_layout.base < mod->init_layout.size; >> @@ -31,26 +41,43 @@ u32 get_module_plt(struct module *mod, unsigned long >> loc, Elf32_Addr val) >> { >> struct mod_plt_sec *pltsec = !in_init(mod, loc) ? >arch.core : >> >arch.init; >> + int idx; >> + struct plt_entries *plt; >> + >> + /* Pre-allocate entries in the first plt */ >> + if (!pltsec->plt_count) { >> + plt = (struct plt_entries *)pltsec->plt->sh_addr; >> + for (idx = 0; idx < ARRAY_SIZE(plt->ldr); ++idx) >> + plt->ldr[idx] = PLT_ENT_LDR; >> + memcpy(plt->lit, fixed_plts, sizeof(fixed_plts)); >> + pltsec->plt_count = ARRAY_SIZE(fixed_plts); >> + /* >> +* cache the address, >> +* ELF header is available only during module load >> +*/ >> + pltsec->plt_ent = plt; >> + } >> + plt = pltsec->plt_ent; >> >> - struct plt_entries *plt = (struct plt_entries *)pltsec->plt->sh_addr; >> - int idx = 0; >> + idx = ARRAY_SIZE(fixed_plts); >> + while (idx) >> + if (plt->lit[--idx] == val) >> + return (u32)>ldr[idx]; >> > Same here. -- Best regards, Alexander Sverdlin.
Re: [PATCH v3 2/2] ARM: ftrace: Add MODULE_PLTS support
Hello Ard! On 09/03/18 09:50, Ard Biesheuvel wrote: >> +static u32 fixed_plts[] = { >> + FTRACE_ADDR, >> + MCOUNT_ADDR, >> +#ifdef CONFIG_OLD_MCOUNT >> + (unsigned long)ftrace_caller_old, >> + (unsigned long)mcount, >> +#endif >> +}; >> + > Do we need this stuff in the PLT loader if ftrace is disabled? You are right, it probably would not even build w/o FTRACE. I'll fix it, thanks for looking into this! >> static bool in_init(const struct module *mod, unsigned long loc) >> { >> return loc - (u32)mod->init_layout.base < mod->init_layout.size; >> @@ -31,26 +41,43 @@ u32 get_module_plt(struct module *mod, unsigned long >> loc, Elf32_Addr val) >> { >> struct mod_plt_sec *pltsec = !in_init(mod, loc) ? >arch.core : >> >arch.init; >> + int idx; >> + struct plt_entries *plt; >> + >> + /* Pre-allocate entries in the first plt */ >> + if (!pltsec->plt_count) { >> + plt = (struct plt_entries *)pltsec->plt->sh_addr; >> + for (idx = 0; idx < ARRAY_SIZE(plt->ldr); ++idx) >> + plt->ldr[idx] = PLT_ENT_LDR; >> + memcpy(plt->lit, fixed_plts, sizeof(fixed_plts)); >> + pltsec->plt_count = ARRAY_SIZE(fixed_plts); >> + /* >> +* cache the address, >> +* ELF header is available only during module load >> +*/ >> + pltsec->plt_ent = plt; >> + } >> + plt = pltsec->plt_ent; >> >> - struct plt_entries *plt = (struct plt_entries *)pltsec->plt->sh_addr; >> - int idx = 0; >> + idx = ARRAY_SIZE(fixed_plts); >> + while (idx) >> + if (plt->lit[--idx] == val) >> + return (u32)>ldr[idx]; >> > Same here. -- Best regards, Alexander Sverdlin.
Re: [PATCH v3 2/2] ARM: ftrace: Add MODULE_PLTS support
Hi Alexander, On 9 March 2018 at 08:41, Alexander Sverdlinwrote: > 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 > [] (unwind_backtrace) from [] (show_stack+0x11/0x14) > [] (show_stack) from [] (dump_stack+0x81/0xa8) > [] (dump_stack) from [] (warn_slowpath_common+0x69/0x90) > [] (warn_slowpath_common) from [] > (warn_slowpath_null+0x17/0x1c) > [] (warn_slowpath_null) from [] > (__arm_gen_branch+0x83/0x8c) > [] (__arm_gen_branch) from [] (ftrace_make_nop+0xf/0x24) > [] (ftrace_make_nop) from [] > (ftrace_process_locs+0x27b/0x3e8) > [] (ftrace_process_locs) from [] > (load_module+0x11e9/0x1a44) > [] (load_module) from [] (SyS_finit_module+0x59/0x84) > [] (SyS_finit_module) from [] (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 > [] (unwind_backtrace) from [] (show_stack+0x11/0x14) > [] (show_stack) from [] (dump_stack+0x81/0xa8) > [] (dump_stack) from [] (warn_slowpath_common+0x69/0x90) > [] (warn_slowpath_common) from [] > (warn_slowpath_null+0x17/0x1c) > [] (warn_slowpath_null) from [] (ftrace_bug+0x1b1/0x234) > [] (ftrace_bug) from [] (ftrace_process_locs+0x285/0x3e8) > [] (ftrace_process_locs) from [] > (load_module+0x11e9/0x1a44) > [] (load_module) from [] (SyS_finit_module+0x59/0x84) > [] (SyS_finit_module) from [] (ret_fast_syscall+0x1/0x18) > ---[ end trace e1b64ced7a89adcd ]--- > ftrace failed to modify [] 0xe9ef7006 > actual: 02:f0:3b:fa > ftrace record flags: 0 > (0) expected tramp: c0314265 > > Signed-off-by: Alexander Sverdlin > --- > arch/arm/include/asm/ftrace.h | 3 +++ > arch/arm/include/asm/module.h | 1 + > arch/arm/kernel/ftrace.c | 62 > --- > arch/arm/kernel/module-plts.c | 53 +++- > 4 files changed, 97 insertions(+), 22 deletions(-) > > diff --git a/arch/arm/include/asm/ftrace.h b/arch/arm/include/asm/ftrace.h > index 9e842ff..faeb6b1 100644 > --- a/arch/arm/include/asm/ftrace.h > +++ b/arch/arm/include/asm/ftrace.h > @@ -19,6 +19,9 @@ struct dyn_arch_ftrace { > #ifdef CONFIG_OLD_MCOUNT > boolold_mcount; > #endif > +#ifdef CONFIG_ARM_MODULE_PLTS > + struct module *mod; > +#endif > }; > > static inline unsigned long ftrace_call_adjust(unsigned long addr) > diff --git a/arch/arm/include/asm/module.h b/arch/arm/include/asm/module.h > index 6996405..e3d7a51 100644 > --- a/arch/arm/include/asm/module.h > +++ b/arch/arm/include/asm/module.h > @@ -30,6 +30,7 @@ struct plt_entries { > > struct mod_plt_sec { > struct elf32_shdr *plt; > + struct plt_entries *plt_ent; > int plt_count; > }; > > diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c > index 5617932..b55355f 100644 > --- a/arch/arm/kernel/ftrace.c > +++ b/arch/arm/kernel/ftrace.c > @@ -98,6 +98,19 @@ int ftrace_arch_code_modify_post_process(void) > > static unsigned long ftrace_call_replace(unsigned long pc, unsigned long > addr) > { > + s32 offset = addr - pc; > + s32 blim = 0xfe08; > + s32 flim = 0x0204; > + > + if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) { > + blim = 0xff04; > + flim = 0x0102; > + } > + > + if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) && > + (offset < blim || offset > flim)) > + return 0; > + > return arm_gen_branch_link(pc, addr); > } > > @@ -166,10 +179,22 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned > long addr) > { > unsigned long new, old; > unsigned long ip = rec->ip; > + unsigned long aaddr = adjust_address(rec, addr); > > old = ftrace_nop_replace(rec); > > - new = ftrace_call_replace(ip, adjust_address(rec, addr)); > + new = ftrace_call_replace(ip, aaddr); > + > +#ifdef CONFIG_ARM_MODULE_PLTS > + if (!new) { > + struct module *mod = rec->arch.mod; > + > + if (mod) { > + aaddr = get_module_plt(mod, ip, aaddr); > + new = ftrace_call_replace(ip, aaddr); > + } > + } > +#endif > > return ftrace_modify_code(rec->ip, old, new, true); > } > @@ -199,20 +224,39 @@ int ftrace_make_nop(struct module *mod, > unsigned long new; > int ret; > > - old = ftrace_call_replace(ip, adjust_address(rec, addr)); > - new = ftrace_nop_replace(rec); > - ret = ftrace_modify_code(ip, old, new, true); > +#ifdef CONFIG_ARM_MODULE_PLTS > +
Re: [PATCH v3 2/2] ARM: ftrace: Add MODULE_PLTS support
Hi Alexander, On 9 March 2018 at 08:41, Alexander Sverdlin wrote: > 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 > [] (unwind_backtrace) from [] (show_stack+0x11/0x14) > [] (show_stack) from [] (dump_stack+0x81/0xa8) > [] (dump_stack) from [] (warn_slowpath_common+0x69/0x90) > [] (warn_slowpath_common) from [] > (warn_slowpath_null+0x17/0x1c) > [] (warn_slowpath_null) from [] > (__arm_gen_branch+0x83/0x8c) > [] (__arm_gen_branch) from [] (ftrace_make_nop+0xf/0x24) > [] (ftrace_make_nop) from [] > (ftrace_process_locs+0x27b/0x3e8) > [] (ftrace_process_locs) from [] > (load_module+0x11e9/0x1a44) > [] (load_module) from [] (SyS_finit_module+0x59/0x84) > [] (SyS_finit_module) from [] (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 > [] (unwind_backtrace) from [] (show_stack+0x11/0x14) > [] (show_stack) from [] (dump_stack+0x81/0xa8) > [] (dump_stack) from [] (warn_slowpath_common+0x69/0x90) > [] (warn_slowpath_common) from [] > (warn_slowpath_null+0x17/0x1c) > [] (warn_slowpath_null) from [] (ftrace_bug+0x1b1/0x234) > [] (ftrace_bug) from [] (ftrace_process_locs+0x285/0x3e8) > [] (ftrace_process_locs) from [] > (load_module+0x11e9/0x1a44) > [] (load_module) from [] (SyS_finit_module+0x59/0x84) > [] (SyS_finit_module) from [] (ret_fast_syscall+0x1/0x18) > ---[ end trace e1b64ced7a89adcd ]--- > ftrace failed to modify [] 0xe9ef7006 > actual: 02:f0:3b:fa > ftrace record flags: 0 > (0) expected tramp: c0314265 > > Signed-off-by: Alexander Sverdlin > --- > arch/arm/include/asm/ftrace.h | 3 +++ > arch/arm/include/asm/module.h | 1 + > arch/arm/kernel/ftrace.c | 62 > --- > arch/arm/kernel/module-plts.c | 53 +++- > 4 files changed, 97 insertions(+), 22 deletions(-) > > diff --git a/arch/arm/include/asm/ftrace.h b/arch/arm/include/asm/ftrace.h > index 9e842ff..faeb6b1 100644 > --- a/arch/arm/include/asm/ftrace.h > +++ b/arch/arm/include/asm/ftrace.h > @@ -19,6 +19,9 @@ struct dyn_arch_ftrace { > #ifdef CONFIG_OLD_MCOUNT > boolold_mcount; > #endif > +#ifdef CONFIG_ARM_MODULE_PLTS > + struct module *mod; > +#endif > }; > > static inline unsigned long ftrace_call_adjust(unsigned long addr) > diff --git a/arch/arm/include/asm/module.h b/arch/arm/include/asm/module.h > index 6996405..e3d7a51 100644 > --- a/arch/arm/include/asm/module.h > +++ b/arch/arm/include/asm/module.h > @@ -30,6 +30,7 @@ struct plt_entries { > > struct mod_plt_sec { > struct elf32_shdr *plt; > + struct plt_entries *plt_ent; > int plt_count; > }; > > diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c > index 5617932..b55355f 100644 > --- a/arch/arm/kernel/ftrace.c > +++ b/arch/arm/kernel/ftrace.c > @@ -98,6 +98,19 @@ int ftrace_arch_code_modify_post_process(void) > > static unsigned long ftrace_call_replace(unsigned long pc, unsigned long > addr) > { > + s32 offset = addr - pc; > + s32 blim = 0xfe08; > + s32 flim = 0x0204; > + > + if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) { > + blim = 0xff04; > + flim = 0x0102; > + } > + > + if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) && > + (offset < blim || offset > flim)) > + return 0; > + > return arm_gen_branch_link(pc, addr); > } > > @@ -166,10 +179,22 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned > long addr) > { > unsigned long new, old; > unsigned long ip = rec->ip; > + unsigned long aaddr = adjust_address(rec, addr); > > old = ftrace_nop_replace(rec); > > - new = ftrace_call_replace(ip, adjust_address(rec, addr)); > + new = ftrace_call_replace(ip, aaddr); > + > +#ifdef CONFIG_ARM_MODULE_PLTS > + if (!new) { > + struct module *mod = rec->arch.mod; > + > + if (mod) { > + aaddr = get_module_plt(mod, ip, aaddr); > + new = ftrace_call_replace(ip, aaddr); > + } > + } > +#endif > > return ftrace_modify_code(rec->ip, old, new, true); > } > @@ -199,20 +224,39 @@ int ftrace_make_nop(struct module *mod, > unsigned long new; > int ret; > > - old = ftrace_call_replace(ip, adjust_address(rec, addr)); > - new = ftrace_nop_replace(rec); > - ret = ftrace_modify_code(ip, old, new, true); > +#ifdef CONFIG_ARM_MODULE_PLTS > + /* mod is only supplied during module loading */ > +
[PATCH v3 2/2] ARM: 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 [] (unwind_backtrace) from [] (show_stack+0x11/0x14) [] (show_stack) from [] (dump_stack+0x81/0xa8) [] (dump_stack) from [] (warn_slowpath_common+0x69/0x90) [] (warn_slowpath_common) from [] (warn_slowpath_null+0x17/0x1c) [] (warn_slowpath_null) from [] (__arm_gen_branch+0x83/0x8c) [] (__arm_gen_branch) from [] (ftrace_make_nop+0xf/0x24) [] (ftrace_make_nop) from [] (ftrace_process_locs+0x27b/0x3e8) [] (ftrace_process_locs) from [] (load_module+0x11e9/0x1a44) [] (load_module) from [] (SyS_finit_module+0x59/0x84) [] (SyS_finit_module) from [] (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 [] (unwind_backtrace) from [] (show_stack+0x11/0x14) [] (show_stack) from [] (dump_stack+0x81/0xa8) [] (dump_stack) from [] (warn_slowpath_common+0x69/0x90) [] (warn_slowpath_common) from [] (warn_slowpath_null+0x17/0x1c) [] (warn_slowpath_null) from [] (ftrace_bug+0x1b1/0x234) [] (ftrace_bug) from [] (ftrace_process_locs+0x285/0x3e8) [] (ftrace_process_locs) from [] (load_module+0x11e9/0x1a44) [] (load_module) from [] (SyS_finit_module+0x59/0x84) [] (SyS_finit_module) from [] (ret_fast_syscall+0x1/0x18) ---[ end trace e1b64ced7a89adcd ]--- ftrace failed to modify [] 0xe9ef7006 actual: 02:f0:3b:fa ftrace record flags: 0 (0) expected tramp: c0314265 Signed-off-by: Alexander Sverdlin--- arch/arm/include/asm/ftrace.h | 3 +++ arch/arm/include/asm/module.h | 1 + arch/arm/kernel/ftrace.c | 62 --- arch/arm/kernel/module-plts.c | 53 +++- 4 files changed, 97 insertions(+), 22 deletions(-) diff --git a/arch/arm/include/asm/ftrace.h b/arch/arm/include/asm/ftrace.h index 9e842ff..faeb6b1 100644 --- a/arch/arm/include/asm/ftrace.h +++ b/arch/arm/include/asm/ftrace.h @@ -19,6 +19,9 @@ struct dyn_arch_ftrace { #ifdef CONFIG_OLD_MCOUNT boolold_mcount; #endif +#ifdef CONFIG_ARM_MODULE_PLTS + struct module *mod; +#endif }; static inline unsigned long ftrace_call_adjust(unsigned long addr) diff --git a/arch/arm/include/asm/module.h b/arch/arm/include/asm/module.h index 6996405..e3d7a51 100644 --- a/arch/arm/include/asm/module.h +++ b/arch/arm/include/asm/module.h @@ -30,6 +30,7 @@ struct plt_entries { struct mod_plt_sec { struct elf32_shdr *plt; + struct plt_entries *plt_ent; int plt_count; }; diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c index 5617932..b55355f 100644 --- a/arch/arm/kernel/ftrace.c +++ b/arch/arm/kernel/ftrace.c @@ -98,6 +98,19 @@ int ftrace_arch_code_modify_post_process(void) static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr) { + s32 offset = addr - pc; + s32 blim = 0xfe08; + s32 flim = 0x0204; + + if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) { + blim = 0xff04; + flim = 0x0102; + } + + if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) && + (offset < blim || offset > flim)) + return 0; + return arm_gen_branch_link(pc, addr); } @@ -166,10 +179,22 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) { unsigned long new, old; unsigned long ip = rec->ip; + unsigned long aaddr = adjust_address(rec, addr); old = ftrace_nop_replace(rec); - new = ftrace_call_replace(ip, adjust_address(rec, addr)); + new = ftrace_call_replace(ip, aaddr); + +#ifdef CONFIG_ARM_MODULE_PLTS + if (!new) { + struct module *mod = rec->arch.mod; + + if (mod) { + aaddr = get_module_plt(mod, ip, aaddr); + new = ftrace_call_replace(ip, aaddr); + } + } +#endif return ftrace_modify_code(rec->ip, old, new, true); } @@ -199,20 +224,39 @@ int ftrace_make_nop(struct module *mod, unsigned long new; int ret; - old = ftrace_call_replace(ip, adjust_address(rec, addr)); - new = ftrace_nop_replace(rec); - ret = ftrace_modify_code(ip, old, new, true); +#ifdef CONFIG_ARM_MODULE_PLTS + /* mod is only supplied during module loading */ + if (!mod) + mod = rec->arch.mod; + else + rec->arch.mod = mod; +#endif -#ifdef CONFIG_OLD_MCOUNT - if (ret == -EINVAL && addr == MCOUNT_ADDR) { - rec->arch.old_mcount = true; + for (;;) { + unsigned long aaddr =
[PATCH v3 2/2] ARM: 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 [] (unwind_backtrace) from [] (show_stack+0x11/0x14) [] (show_stack) from [] (dump_stack+0x81/0xa8) [] (dump_stack) from [] (warn_slowpath_common+0x69/0x90) [] (warn_slowpath_common) from [] (warn_slowpath_null+0x17/0x1c) [] (warn_slowpath_null) from [] (__arm_gen_branch+0x83/0x8c) [] (__arm_gen_branch) from [] (ftrace_make_nop+0xf/0x24) [] (ftrace_make_nop) from [] (ftrace_process_locs+0x27b/0x3e8) [] (ftrace_process_locs) from [] (load_module+0x11e9/0x1a44) [] (load_module) from [] (SyS_finit_module+0x59/0x84) [] (SyS_finit_module) from [] (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 [] (unwind_backtrace) from [] (show_stack+0x11/0x14) [] (show_stack) from [] (dump_stack+0x81/0xa8) [] (dump_stack) from [] (warn_slowpath_common+0x69/0x90) [] (warn_slowpath_common) from [] (warn_slowpath_null+0x17/0x1c) [] (warn_slowpath_null) from [] (ftrace_bug+0x1b1/0x234) [] (ftrace_bug) from [] (ftrace_process_locs+0x285/0x3e8) [] (ftrace_process_locs) from [] (load_module+0x11e9/0x1a44) [] (load_module) from [] (SyS_finit_module+0x59/0x84) [] (SyS_finit_module) from [] (ret_fast_syscall+0x1/0x18) ---[ end trace e1b64ced7a89adcd ]--- ftrace failed to modify [] 0xe9ef7006 actual: 02:f0:3b:fa ftrace record flags: 0 (0) expected tramp: c0314265 Signed-off-by: Alexander Sverdlin --- arch/arm/include/asm/ftrace.h | 3 +++ arch/arm/include/asm/module.h | 1 + arch/arm/kernel/ftrace.c | 62 --- arch/arm/kernel/module-plts.c | 53 +++- 4 files changed, 97 insertions(+), 22 deletions(-) diff --git a/arch/arm/include/asm/ftrace.h b/arch/arm/include/asm/ftrace.h index 9e842ff..faeb6b1 100644 --- a/arch/arm/include/asm/ftrace.h +++ b/arch/arm/include/asm/ftrace.h @@ -19,6 +19,9 @@ struct dyn_arch_ftrace { #ifdef CONFIG_OLD_MCOUNT boolold_mcount; #endif +#ifdef CONFIG_ARM_MODULE_PLTS + struct module *mod; +#endif }; static inline unsigned long ftrace_call_adjust(unsigned long addr) diff --git a/arch/arm/include/asm/module.h b/arch/arm/include/asm/module.h index 6996405..e3d7a51 100644 --- a/arch/arm/include/asm/module.h +++ b/arch/arm/include/asm/module.h @@ -30,6 +30,7 @@ struct plt_entries { struct mod_plt_sec { struct elf32_shdr *plt; + struct plt_entries *plt_ent; int plt_count; }; diff --git a/arch/arm/kernel/ftrace.c b/arch/arm/kernel/ftrace.c index 5617932..b55355f 100644 --- a/arch/arm/kernel/ftrace.c +++ b/arch/arm/kernel/ftrace.c @@ -98,6 +98,19 @@ int ftrace_arch_code_modify_post_process(void) static unsigned long ftrace_call_replace(unsigned long pc, unsigned long addr) { + s32 offset = addr - pc; + s32 blim = 0xfe08; + s32 flim = 0x0204; + + if (IS_ENABLED(CONFIG_THUMB2_KERNEL)) { + blim = 0xff04; + flim = 0x0102; + } + + if (IS_ENABLED(CONFIG_ARM_MODULE_PLTS) && + (offset < blim || offset > flim)) + return 0; + return arm_gen_branch_link(pc, addr); } @@ -166,10 +179,22 @@ int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr) { unsigned long new, old; unsigned long ip = rec->ip; + unsigned long aaddr = adjust_address(rec, addr); old = ftrace_nop_replace(rec); - new = ftrace_call_replace(ip, adjust_address(rec, addr)); + new = ftrace_call_replace(ip, aaddr); + +#ifdef CONFIG_ARM_MODULE_PLTS + if (!new) { + struct module *mod = rec->arch.mod; + + if (mod) { + aaddr = get_module_plt(mod, ip, aaddr); + new = ftrace_call_replace(ip, aaddr); + } + } +#endif return ftrace_modify_code(rec->ip, old, new, true); } @@ -199,20 +224,39 @@ int ftrace_make_nop(struct module *mod, unsigned long new; int ret; - old = ftrace_call_replace(ip, adjust_address(rec, addr)); - new = ftrace_nop_replace(rec); - ret = ftrace_modify_code(ip, old, new, true); +#ifdef CONFIG_ARM_MODULE_PLTS + /* mod is only supplied during module loading */ + if (!mod) + mod = rec->arch.mod; + else + rec->arch.mod = mod; +#endif -#ifdef CONFIG_OLD_MCOUNT - if (ret == -EINVAL && addr == MCOUNT_ADDR) { - rec->arch.old_mcount = true; + for (;;) { + unsigned long aaddr = adjust_address(rec, addr); + +