Skip to content

Commit 2b81996

Browse files
Xu KuohaiKernel Patches Daemon
authored andcommitted
ftrace: Fix deadloop caused by direct call in ftrace selftest
After direct call is enabled for arm64, ftrace selftest enters a dead loop: <trace_selftest_dynamic_test_func>: 00 bti c 01 mov x9, x30 <trace_direct_tramp>: 02 bl <trace_direct_tramp> ----------> ret | lr/x30 is 03, return to 03 | 03 mov w0, #0x0 <-----------------------------| | | | dead loop! | | | 04 ret ---- lr/x30 is still 03, go back to 03 ----| The reason is that when the direct caller trace_direct_tramp() returns to the patched function trace_selftest_dynamic_test_func(), lr is still the address after the instrumented instruction in the patched function, so when the patched function exits, it returns to itself! To fix this issue, we need to restore lr before trace_direct_tramp() exits, so rewrite a dedicated trace_direct_tramp() for arm64. Reported-by: Li Huafei <[email protected]> Signed-off-by: Xu Kuohai <[email protected]>
1 parent 951c8ff commit 2b81996

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

arch/arm64/include/asm/ftrace.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,16 @@ static inline bool arch_syscall_match_sym_name(const char *sym,
126126
*/
127127
return !strcmp(sym + 8, name);
128128
}
129+
130+
#ifdef CONFIG_FTRACE_SELFTEST
131+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
132+
133+
#define trace_direct_tramp trace_direct_tramp
134+
extern void trace_direct_tramp(void);
135+
136+
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
137+
#endif /* CONFIG_FTRACE_SELFTEST */
138+
129139
#endif /* ifndef __ASSEMBLY__ */
130140

131141
#endif /* __ASM_FTRACE_H */

arch/arm64/kernel/entry-ftrace.S

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,13 @@ SYM_CODE_START(return_to_handler)
357357
ret
358358
SYM_CODE_END(return_to_handler)
359359
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
360+
361+
#ifdef CONFIG_FTRACE_SELFTEST
362+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
363+
SYM_FUNC_START(trace_direct_tramp)
364+
mov x10, x30
365+
mov x30, x9
366+
ret x10
367+
SYM_FUNC_END(trace_direct_tramp)
368+
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
369+
#endif /* CONFIG_FTRACE_SELFTEST */

kernel/trace/trace_selftest.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,8 +785,10 @@ static struct fgraph_ops fgraph_ops __initdata = {
785785
};
786786

787787
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
788+
#ifndef trace_direct_tramp
788789
noinline __noclone static void trace_direct_tramp(void) { }
789790
#endif
791+
#endif
790792

791793
/*
792794
* Pretty much the same than for the function tracer from which the selftest

0 commit comments

Comments
 (0)