Skip to content

Commit cf9a578

Browse files
Xu KuohaiKernel Patches Daemon
authored andcommitted
arm64: ftrace: Add ftrace direct call support
Add ftrace direct support for arm64. 1. When there is custom trampoline only, replace the fentry nop to a jump instruction that jumps directly to the custom trampoline. 2. When ftrace trampoline and custom trampoline coexist, jump from fentry to ftrace trampoline first, then jump to custom trampoline when ftrace trampoline exits. The current unused register pt_regs->orig_x0 is used as an intermediary for jumping from ftrace trampoline to custom trampoline. Signed-off-by: Xu Kuohai <xukuohai@huawei.com> Acked-by: Song Liu <songliubraving@fb.com>
1 parent 9278c1a commit cf9a578

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

arch/arm64/Kconfig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,8 @@ config ARM64
177177
select HAVE_DYNAMIC_FTRACE
178178
select HAVE_DYNAMIC_FTRACE_WITH_REGS \
179179
if $(cc-option,-fpatchable-function-entry=2)
180+
select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS \
181+
if DYNAMIC_FTRACE_WITH_REGS
180182
select FTRACE_MCOUNT_USE_PATCHABLE_FUNCTION_ENTRY \
181183
if DYNAMIC_FTRACE_WITH_REGS
182184
select HAVE_EFFICIENT_UNALIGNED_ACCESS

arch/arm64/include/asm/ftrace.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,16 @@ static inline unsigned long ftrace_call_adjust(unsigned long addr)
7878
return addr;
7979
}
8080

81+
static inline void arch_ftrace_set_direct_caller(struct pt_regs *regs,
82+
unsigned long addr)
83+
{
84+
/*
85+
* Place custom trampoline address in regs->orig_x0 to let ftrace
86+
* trampoline jump to it.
87+
*/
88+
regs->orig_x0 = addr;
89+
}
90+
8191
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_REGS
8292
struct dyn_ftrace;
8393
int ftrace_init_nop(struct module *mod, struct dyn_ftrace *rec);

arch/arm64/kernel/asm-offsets.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ int main(void)
8080
DEFINE(S_SDEI_TTBR1, offsetof(struct pt_regs, sdei_ttbr1));
8181
DEFINE(S_PMR_SAVE, offsetof(struct pt_regs, pmr_save));
8282
DEFINE(S_STACKFRAME, offsetof(struct pt_regs, stackframe));
83+
DEFINE(S_ORIG_X0, offsetof(struct pt_regs, orig_x0));
8384
DEFINE(PT_REGS_SIZE, sizeof(struct pt_regs));
8485
BLANK();
8586
#ifdef CONFIG_COMPAT

arch/arm64/kernel/entry-ftrace.S

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
str x29, [sp, #S_FP]
6161
.endif
6262

63+
/* Set orig_x0 to zero */
64+
str xzr, [sp, #S_ORIG_X0]
65+
6366
/* Save the callsite's SP and LR */
6467
add x10, sp, #(PT_REGS_SIZE + 16)
6568
stp x9, x10, [sp, #S_LR]
@@ -119,12 +122,21 @@ ftrace_common_return:
119122
/* Restore the callsite's FP, LR, PC */
120123
ldr x29, [sp, #S_FP]
121124
ldr x30, [sp, #S_LR]
122-
ldr x9, [sp, #S_PC]
123-
125+
ldr x10, [sp, #S_PC]
126+
127+
ldr x11, [sp, #S_ORIG_X0]
128+
cbz x11, 1f
129+
/* Set x9 to parent ip before jump to custom trampoline */
130+
mov x9, x30
131+
/* Set lr to self ip */
132+
ldr x30, [sp, #S_PC]
133+
/* Set x10 (used for return address) to custom trampoline */
134+
mov x10, x11
135+
1:
124136
/* Restore the callsite's SP */
125137
add sp, sp, #PT_REGS_SIZE + 16
126138

127-
ret x9
139+
ret x10
128140
SYM_CODE_END(ftrace_common)
129141

130142
#ifdef CONFIG_FUNCTION_GRAPH_TRACER

0 commit comments

Comments
 (0)