Skip to content

Commit 23b44fc

Browse files
chleroympe
authored andcommitted
powerpc/ftrace: Make __ftrace_make_{nop/call}() common to PPC32 and PPC64
Since c93d4f6 ("powerpc/ftrace: Add module_trampoline_target() for PPC32"), __ftrace_make_nop() for PPC32 is very similar to the one for PPC64. Same for __ftrace_make_call(). Make them common. Signed-off-by: Christophe Leroy <[email protected]> Signed-off-by: Michael Ellerman <[email protected]> Link: https://lore.kernel.org/r/96f53c237316dab4b1b8c682685266faa92da816.1652074503.git.christophe.leroy@csgroup.eu
1 parent 5b89492 commit 23b44fc

File tree

1 file changed

+8
-100
lines changed

1 file changed

+8
-100
lines changed

arch/powerpc/kernel/trace/ftrace.c

Lines changed: 8 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ static unsigned long find_bl_target(unsigned long ip, ppc_inst_t op)
114114
}
115115

116116
#ifdef CONFIG_MODULES
117-
#ifdef CONFIG_PPC64
118117
static int
119118
__ftrace_make_nop(struct module *mod,
120119
struct dyn_ftrace *rec, unsigned long addr)
@@ -154,10 +153,11 @@ __ftrace_make_nop(struct module *mod,
154153
return -EINVAL;
155154
}
156155

157-
#ifdef CONFIG_MPROFILE_KERNEL
158-
/* When using -mkernel_profile there is no load to jump over */
156+
/* When using -mprofile-kernel or PPC32 there is no load to jump over */
159157
pop = ppc_inst(PPC_RAW_NOP());
160158

159+
#ifdef CONFIG_PPC64
160+
#ifdef CONFIG_MPROFILE_KERNEL
161161
if (copy_inst_from_kernel_nofault(&op, (void *)(ip - 4))) {
162162
pr_err("Fetching instruction at %lx failed.\n", ip - 4);
163163
return -EFAULT;
@@ -201,6 +201,7 @@ __ftrace_make_nop(struct module *mod,
201201
return -EINVAL;
202202
}
203203
#endif /* CONFIG_MPROFILE_KERNEL */
204+
#endif /* PPC64 */
204205

205206
if (patch_instruction((u32 *)ip, pop)) {
206207
pr_err("Patching NOP failed.\n");
@@ -209,48 +210,6 @@ __ftrace_make_nop(struct module *mod,
209210

210211
return 0;
211212
}
212-
213-
#else /* !PPC64 */
214-
static int
215-
__ftrace_make_nop(struct module *mod,
216-
struct dyn_ftrace *rec, unsigned long addr)
217-
{
218-
ppc_inst_t op;
219-
unsigned long ip = rec->ip;
220-
unsigned long tramp, ptr;
221-
222-
if (copy_from_kernel_nofault(&op, (void *)ip, MCOUNT_INSN_SIZE))
223-
return -EFAULT;
224-
225-
/* Make sure that that this is still a 24bit jump */
226-
if (!is_bl_op(op)) {
227-
pr_err("Not expected bl: opcode is %s\n", ppc_inst_as_str(op));
228-
return -EINVAL;
229-
}
230-
231-
/* lets find where the pointer goes */
232-
tramp = find_bl_target(ip, op);
233-
234-
/* Find where the trampoline jumps to */
235-
if (module_trampoline_target(mod, tramp, &ptr)) {
236-
pr_err("Failed to get trampoline target\n");
237-
return -EFAULT;
238-
}
239-
240-
if (ptr != addr) {
241-
pr_err("Trampoline location %08lx does not match addr\n",
242-
tramp);
243-
return -EINVAL;
244-
}
245-
246-
op = ppc_inst(PPC_RAW_NOP());
247-
248-
if (patch_instruction((u32 *)ip, op))
249-
return -EPERM;
250-
251-
return 0;
252-
}
253-
#endif /* PPC64 */
254213
#endif /* CONFIG_MODULES */
255214

256215
static unsigned long find_ftrace_tramp(unsigned long ip)
@@ -437,13 +396,12 @@ int ftrace_make_nop(struct module *mod,
437396
}
438397

439398
#ifdef CONFIG_MODULES
440-
#ifdef CONFIG_PPC64
441399
/*
442400
* Examine the existing instructions for __ftrace_make_call.
443401
* They should effectively be a NOP, and follow formal constraints,
444402
* depending on the ABI. Return false if they don't.
445403
*/
446-
#ifndef CONFIG_MPROFILE_KERNEL
404+
#ifdef CONFIG_PPC64_ELF_ABI_V1
447405
static int
448406
expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
449407
{
@@ -465,7 +423,7 @@ expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
465423
static int
466424
expected_nop_sequence(void *ip, ppc_inst_t op0, ppc_inst_t op1)
467425
{
468-
/* look for patched "NOP" on ppc64 with -mprofile-kernel */
426+
/* look for patched "NOP" on ppc64 with -mprofile-kernel or ppc32 */
469427
if (!ppc_inst_equal(op0, ppc_inst(PPC_RAW_NOP())))
470428
return 0;
471429
return 1;
@@ -484,8 +442,10 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
484442
if (copy_inst_from_kernel_nofault(op, ip))
485443
return -EFAULT;
486444

445+
#ifdef CONFIG_PPC64_ELF_ABI_V1
487446
if (copy_inst_from_kernel_nofault(op + 1, ip + 4))
488447
return -EFAULT;
448+
#endif
489449

490450
if (!expected_nop_sequence(ip, op[0], op[1])) {
491451
pr_err("Unexpected call sequence at %p: %s %s\n",
@@ -531,58 +491,6 @@ __ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
531491

532492
return 0;
533493
}
534-
535-
#else /* !CONFIG_PPC64: */
536-
static int
537-
__ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr)
538-
{
539-
int err;
540-
ppc_inst_t op;
541-
u32 *ip = (u32 *)rec->ip;
542-
struct module *mod = rec->arch.mod;
543-
unsigned long tramp;
544-
545-
/* read where this goes */
546-
if (copy_inst_from_kernel_nofault(&op, ip))
547-
return -EFAULT;
548-
549-
/* It should be pointing to a nop */
550-
if (!ppc_inst_equal(op, ppc_inst(PPC_RAW_NOP()))) {
551-
pr_err("Expected NOP but have %s\n", ppc_inst_as_str(op));
552-
return -EINVAL;
553-
}
554-
555-
/* If we never set up a trampoline to ftrace_caller, then bail */
556-
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
557-
if (!mod->arch.tramp || !mod->arch.tramp_regs) {
558-
#else
559-
if (!mod->arch.tramp) {
560-
#endif
561-
pr_err("No ftrace trampoline\n");
562-
return -EINVAL;
563-
}
564-
565-
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
566-
if (rec->flags & FTRACE_FL_REGS)
567-
tramp = mod->arch.tramp_regs;
568-
else
569-
#endif
570-
tramp = mod->arch.tramp;
571-
/* create the branch to the trampoline */
572-
err = create_branch(&op, ip, tramp, BRANCH_SET_LINK);
573-
if (err) {
574-
pr_err("REL24 out of range!\n");
575-
return -EINVAL;
576-
}
577-
578-
pr_devel("write to %lx\n", rec->ip);
579-
580-
if (patch_instruction(ip, op))
581-
return -EPERM;
582-
583-
return 0;
584-
}
585-
#endif /* CONFIG_PPC64 */
586494
#endif /* CONFIG_MODULES */
587495

588496
static int __ftrace_make_call_kernel(struct dyn_ftrace *rec, unsigned long addr)

0 commit comments

Comments
 (0)