Skip to content

Commit 2bc56fd

Browse files
mhiramatrostedt
authored andcommitted
ftrace: Add ftrace_get_symaddr to convert fentry_ip to symaddr
This introduces ftrace_get_symaddr() which tries to convert fentry_ip passed by ftrace or fgraph callback to symaddr without calling kallsyms API. It returns the symbol address or 0 if it fails to convert it. Cc: Alexei Starovoitov <alexei.starovoitov@gmail.com> Cc: Florent Revest <revest@chromium.org> Cc: Martin KaFai Lau <martin.lau@linux.dev> Cc: bpf <bpf@vger.kernel.org> Cc: Alexei Starovoitov <ast@kernel.org> Cc: Jiri Olsa <jolsa@kernel.org> Cc: Alan Maguire <alan.maguire@oracle.com> Cc: Mark Rutland <mark.rutland@arm.com> Link: https://lore.kernel.org/173519011487.391279.5450806886342723151.stgit@devnote2 Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Reported-by: kernel test robot <lkp@intel.com> Closes: https://lore.kernel.org/oe-kbuild-all/202412061423.K79V55Hd-lkp@intel.com/ Closes: https://lore.kernel.org/oe-kbuild-all/202412061804.5VRzF14E-lkp@intel.com/ Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 54b6b4a commit 2bc56fd

File tree

4 files changed

+99
-0
lines changed

4 files changed

+99
-0
lines changed

arch/arm64/include/asm/ftrace.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ extern unsigned long ftrace_graph_call;
5252
extern void return_to_handler(void);
5353

5454
unsigned long ftrace_call_adjust(unsigned long addr);
55+
unsigned long arch_ftrace_get_symaddr(unsigned long fentry_ip);
56+
#define ftrace_get_symaddr(fentry_ip) arch_ftrace_get_symaddr(fentry_ip)
5557

5658
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_ARGS
5759
#define HAVE_ARCH_FTRACE_REGS

arch/arm64/kernel/ftrace.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,69 @@ unsigned long ftrace_call_adjust(unsigned long addr)
143143
return addr;
144144
}
145145

146+
/* Convert fentry_ip to the symbol address without kallsyms */
147+
unsigned long arch_ftrace_get_symaddr(unsigned long fentry_ip)
148+
{
149+
u32 insn;
150+
151+
/*
152+
* When using patchable-function-entry without pre-function NOPS, ftrace
153+
* entry is the address of the first NOP after the function entry point.
154+
*
155+
* The compiler has either generated:
156+
*
157+
* func+00: func: NOP // To be patched to MOV X9, LR
158+
* func+04: NOP // To be patched to BL <caller>
159+
*
160+
* Or:
161+
*
162+
* func-04: BTI C
163+
* func+00: func: NOP // To be patched to MOV X9, LR
164+
* func+04: NOP // To be patched to BL <caller>
165+
*
166+
* The fentry_ip is the address of `BL <caller>` which is at `func + 4`
167+
* bytes in either case.
168+
*/
169+
if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_CALL_OPS))
170+
return fentry_ip - AARCH64_INSN_SIZE;
171+
172+
/*
173+
* When using patchable-function-entry with pre-function NOPs, BTI is
174+
* a bit different.
175+
*
176+
* func+00: func: NOP // To be patched to MOV X9, LR
177+
* func+04: NOP // To be patched to BL <caller>
178+
*
179+
* Or:
180+
*
181+
* func+00: func: BTI C
182+
* func+04: NOP // To be patched to MOV X9, LR
183+
* func+08: NOP // To be patched to BL <caller>
184+
*
185+
* The fentry_ip is the address of `BL <caller>` which is at either
186+
* `func + 4` or `func + 8` depends on whether there is a BTI.
187+
*/
188+
189+
/* If there is no BTI, the func address should be one instruction before. */
190+
if (!IS_ENABLED(CONFIG_ARM64_BTI_KERNEL))
191+
return fentry_ip - AARCH64_INSN_SIZE;
192+
193+
/* We want to be extra safe in case entry ip is on the page edge,
194+
* but otherwise we need to avoid get_kernel_nofault()'s overhead.
195+
*/
196+
if ((fentry_ip & ~PAGE_MASK) < AARCH64_INSN_SIZE * 2) {
197+
if (get_kernel_nofault(insn, (u32 *)(fentry_ip - AARCH64_INSN_SIZE * 2)))
198+
return 0;
199+
} else {
200+
insn = *(u32 *)(fentry_ip - AARCH64_INSN_SIZE * 2);
201+
}
202+
203+
if (aarch64_insn_is_bti(le32_to_cpu((__le32)insn)))
204+
return fentry_ip - AARCH64_INSN_SIZE * 2;
205+
206+
return fentry_ip - AARCH64_INSN_SIZE;
207+
}
208+
146209
/*
147210
* Replace a single instruction, which may be a branch or NOP.
148211
* If @validate == true, a replaced instruction is checked against 'old'.

arch/x86/include/asm/ftrace.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ static inline unsigned long ftrace_call_adjust(unsigned long addr)
3434
return addr;
3535
}
3636

37+
static inline unsigned long arch_ftrace_get_symaddr(unsigned long fentry_ip)
38+
{
39+
#ifdef CONFIG_X86_KERNEL_IBT
40+
u32 instr;
41+
42+
/* We want to be extra safe in case entry ip is on the page edge,
43+
* but otherwise we need to avoid get_kernel_nofault()'s overhead.
44+
*/
45+
if ((fentry_ip & ~PAGE_MASK) < ENDBR_INSN_SIZE) {
46+
if (get_kernel_nofault(instr, (u32 *)(fentry_ip - ENDBR_INSN_SIZE)))
47+
return fentry_ip;
48+
} else {
49+
instr = *(u32 *)(fentry_ip - ENDBR_INSN_SIZE);
50+
}
51+
if (is_endbr(instr))
52+
fentry_ip -= ENDBR_INSN_SIZE;
53+
#endif
54+
return fentry_ip;
55+
}
56+
#define ftrace_get_symaddr(fentry_ip) arch_ftrace_get_symaddr(fentry_ip)
57+
3758
#ifdef CONFIG_HAVE_DYNAMIC_FTRACE_WITH_ARGS
3859

3960
#include <linux/ftrace_regs.h>

include/linux/ftrace.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -622,6 +622,19 @@ enum {
622622
FTRACE_MAY_SLEEP = (1 << 5),
623623
};
624624

625+
/* Arches can override ftrace_get_symaddr() to convert fentry_ip to symaddr. */
626+
#ifndef ftrace_get_symaddr
627+
/**
628+
* ftrace_get_symaddr - return the symbol address from fentry_ip
629+
* @fentry_ip: the address of ftrace location
630+
*
631+
* Get the symbol address from @fentry_ip (fast path). If there is no fast
632+
* search path, this returns 0.
633+
* User may need to use kallsyms API to find the symbol address.
634+
*/
635+
#define ftrace_get_symaddr(fentry_ip) (0)
636+
#endif
637+
625638
#ifdef CONFIG_DYNAMIC_FTRACE
626639

627640
void ftrace_arch_code_modify_prepare(void);

0 commit comments

Comments
 (0)