Skip to content

Commit cc93b92

Browse files
rnavmpe
authored andcommitted
powerpc/ftrace: Add separate ftrace_init_nop() with additional validation
Currently, we validate instructions around the ftrace location every time we have to enable/disable ftrace. Introduce ftrace_init_nop() to instead perform all the validation during ftrace initialization. This allows us to simply patch the necessary instructions during enabling/disabling ftrace. Signed-off-by: Naveen N Rao <naveen@kernel.org> Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu> Signed-off-by: Michael Ellerman <mpe@ellerman.id.au> Link: https://msgid.link/f373684081e8e98be09b7f44d2d93069768324dc.1687166935.git.naveen@kernel.org
1 parent 33bb8a0 commit cc93b92

2 files changed

Lines changed: 77 additions & 0 deletions

File tree

arch/powerpc/include/asm/ftrace.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,17 @@ static inline unsigned long ftrace_call_adjust(unsigned long addr)
2929
unsigned long prepare_ftrace_return(unsigned long parent, unsigned long ip,
3030
unsigned long sp);
3131

32+
struct module;
33+
struct dyn_ftrace;
3234
struct dyn_arch_ftrace {
3335
struct module *mod;
3436
};
3537

3638
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
39+
#define ftrace_need_init_nop() (true)
40+
int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec);
41+
#define ftrace_init_nop ftrace_init_nop
42+
3743
struct ftrace_regs {
3844
struct pt_regs regs;
3945
};

arch/powerpc/kernel/trace/ftrace.c

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,16 @@
3131
#define NUM_FTRACE_TRAMPS 2
3232
static unsigned long ftrace_tramps[NUM_FTRACE_TRAMPS];
3333

34+
static ppc_inst_t ftrace_create_branch_inst(unsigned long ip, unsigned long addr, int link)
35+
{
36+
ppc_inst_t op;
37+
38+
WARN_ON(!is_offset_in_branch_range(addr - ip));
39+
create_branch(&op, (u32 *)ip, addr, link ? BRANCH_SET_LINK : 0);
40+
41+
return op;
42+
}
43+
3444
static ppc_inst_t
3545
ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
3646
{
@@ -597,6 +607,67 @@ int ftrace_modify_call(struct dyn_ftrace *rec, unsigned long old_addr,
597607
}
598608
#endif
599609

610+
int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec)
611+
{
612+
unsigned long addr, ip = rec->ip;
613+
ppc_inst_t old, new;
614+
int ret = 0;
615+
616+
/* Verify instructions surrounding the ftrace location */
617+
if (IS_ENABLED(CONFIG_PPC32)) {
618+
/* Expected sequence: 'mflr r0', 'stw r0,4(r1)', 'bl _mcount' */
619+
ret = ftrace_validate_inst(ip - 8, ppc_inst(PPC_RAW_MFLR(_R0)));
620+
if (!ret)
621+
ret = ftrace_validate_inst(ip - 4, ppc_inst(PPC_RAW_STW(_R0, _R1, 4)));
622+
} else if (IS_ENABLED(CONFIG_MPROFILE_KERNEL)) {
623+
/* Expected sequence: 'mflr r0', ['std r0,16(r1)'], 'bl _mcount' */
624+
ret = ftrace_read_inst(ip - 4, &old);
625+
if (!ret && !ppc_inst_equal(old, ppc_inst(PPC_RAW_MFLR(_R0)))) {
626+
ret = ftrace_validate_inst(ip - 8, ppc_inst(PPC_RAW_MFLR(_R0)));
627+
ret |= ftrace_validate_inst(ip - 4, ppc_inst(PPC_RAW_STD(_R0, _R1, 16)));
628+
}
629+
} else {
630+
return -EINVAL;
631+
}
632+
633+
if (ret)
634+
return ret;
635+
636+
if (!core_kernel_text(ip)) {
637+
if (!mod) {
638+
pr_err("0x%lx: No module provided for non-kernel address\n", ip);
639+
return -EFAULT;
640+
}
641+
rec->arch.mod = mod;
642+
}
643+
644+
/* Nop-out the ftrace location */
645+
new = ppc_inst(PPC_RAW_NOP());
646+
addr = MCOUNT_ADDR;
647+
if (is_offset_in_branch_range(addr - ip)) {
648+
/* Within range */
649+
old = ftrace_create_branch_inst(ip, addr, 1);
650+
ret = ftrace_modify_code(ip, old, new);
651+
} else if (core_kernel_text(ip) || (IS_ENABLED(CONFIG_MODULES) && mod)) {
652+
/*
653+
* We would be branching to a linker-generated stub, or to the module _mcount
654+
* stub. Let's just confirm we have a 'bl' here.
655+
*/
656+
ret = ftrace_read_inst(ip, &old);
657+
if (ret)
658+
return ret;
659+
if (!is_bl_op(old)) {
660+
pr_err("0x%lx: expected (bl) != found (%08lx)\n", ip, ppc_inst_as_ulong(old));
661+
return -EINVAL;
662+
}
663+
ret = patch_instruction((u32 *)ip, new);
664+
} else {
665+
return -EINVAL;
666+
}
667+
668+
return ret;
669+
}
670+
600671
int ftrace_update_ftrace_func(ftrace_func_t func)
601672
{
602673
unsigned long ip = (unsigned long)(&ftrace_call);

0 commit comments

Comments
 (0)