Skip to content

Commit f4fcbf2

Browse files
rnavmpe
authored andcommitted
powerpc/ftrace: Refactor ftrace_modify_code()
Split up ftrace_modify_code() into a few helpers for future use. Also update error messages accordingly. 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/a8daa49712b44ff539e6c22a2ea649a540386798.1687166935.git.naveen@kernel.org
1 parent bad90aa commit f4fcbf2

1 file changed

Lines changed: 27 additions & 20 deletions

File tree

arch/powerpc/kernel/trace/ftrace.c

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -50,32 +50,39 @@ ftrace_call_replace(unsigned long ip, unsigned long addr, int link)
5050
return op;
5151
}
5252

53-
static inline int
54-
ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
53+
static inline int ftrace_read_inst(unsigned long ip, ppc_inst_t *op)
5554
{
56-
ppc_inst_t replaced;
55+
if (copy_inst_from_kernel_nofault(op, (void *)ip)) {
56+
pr_err("0x%lx: fetching instruction failed\n", ip);
57+
return -EFAULT;
58+
}
5759

58-
/*
59-
* Note:
60-
* We are paranoid about modifying text, as if a bug was to happen, it
61-
* could cause us to read or write to someplace that could cause harm.
62-
* Carefully read and modify the code with probe_kernel_*(), and make
63-
* sure what we read is what we expected it to be before modifying it.
64-
*/
60+
return 0;
61+
}
6562

66-
/* read the text we want to modify */
67-
if (copy_inst_from_kernel_nofault(&replaced, (void *)ip))
68-
return -EFAULT;
63+
static inline int ftrace_validate_inst(unsigned long ip, ppc_inst_t inst)
64+
{
65+
ppc_inst_t op;
66+
int ret;
6967

70-
/* Make sure it is what we expect it to be */
71-
if (!ppc_inst_equal(replaced, old)) {
72-
pr_err("%p: replaced (%08lx) != old (%08lx)", (void *)ip,
73-
ppc_inst_as_ulong(replaced), ppc_inst_as_ulong(old));
74-
return -EINVAL;
68+
ret = ftrace_read_inst(ip, &op);
69+
if (!ret && !ppc_inst_equal(op, inst)) {
70+
pr_err("0x%lx: expected (%08lx) != found (%08lx)\n",
71+
ip, ppc_inst_as_ulong(inst), ppc_inst_as_ulong(op));
72+
ret = -EINVAL;
7573
}
7674

77-
/* replace the text with the new text */
78-
return patch_instruction((u32 *)ip, new);
75+
return ret;
76+
}
77+
78+
static inline int ftrace_modify_code(unsigned long ip, ppc_inst_t old, ppc_inst_t new)
79+
{
80+
int ret = ftrace_validate_inst(ip, old);
81+
82+
if (!ret)
83+
ret = patch_instruction((u32 *)ip, new);
84+
85+
return ret;
7986
}
8087

8188
/*

0 commit comments

Comments
 (0)