Skip to content

Commit 196c79f

Browse files
Song Shuaipalmer-dabbelt
authored andcommitted
riscv: ftrace: Add DYNAMIC_FTRACE_WITH_DIRECT_CALLS support
Select the DYNAMIC_FTRACE_WITH_DIRECT_CALLS to provide the register_ftrace_direct[_multi] interfaces allowing users to register the customed trampoline (direct_caller) as the mcount for one or more target functions. And modify_ftrace_direct[_multi] are also provided for modifying direct_caller. To make the direct_caller and the other ftrace hooks (e.g. function/fgraph tracer, k[ret]probes) co-exist, a temporary register is nominated to store the address of direct_caller in ftrace_regs_caller. After the setting of the address direct_caller by direct_ops->func and the RESTORE_REGS in ftrace_regs_caller, direct_caller will be jumped to by the `jr` inst. Add DYNAMIC_FTRACE_WITH_DIRECT_CALLS support for RISC-V. Signed-off-by: Song Shuai <suagrfillet@gmail.com> Tested-by: Guo Ren <guoren@kernel.org> Signed-off-by: Guo Ren <guoren@kernel.org> Acked-by: Björn Töpel <bjorn@rivosinc.com> Link: https://lore.kernel.org/r/20231130121531.1178502-4-bjorn@kernel.org Signed-off-by: Palmer Dabbelt <palmer@rivosinc.com>
1 parent 35e61e8 commit 196c79f

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

arch/riscv/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ config RISCV
114114
select HAVE_DEBUG_KMEMLEAK
115115
select HAVE_DMA_CONTIGUOUS if MMU
116116
select HAVE_DYNAMIC_FTRACE if !XIP_KERNEL && MMU && (CLANG_SUPPORTS_DYNAMIC_FTRACE || GCC_SUPPORTS_DYNAMIC_FTRACE)
117+
select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
117118
select HAVE_DYNAMIC_FTRACE_WITH_REGS if HAVE_DYNAMIC_FTRACE
118119
select HAVE_FTRACE_MCOUNT_RECORD if !XIP_KERNEL
119120
select HAVE_FUNCTION_GRAPH_TRACER

arch/riscv/include/asm/ftrace.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,13 @@ struct ftrace_regs;
135135
void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
136136
struct ftrace_ops *op, struct ftrace_regs *fregs);
137137
#define ftrace_graph_func ftrace_graph_func
138+
139+
static inline void __arch_ftrace_set_direct_caller(struct pt_regs *regs, unsigned long addr)
140+
{
141+
regs->t1 = addr;
142+
}
143+
#define arch_ftrace_set_direct_caller(fregs, addr) \
144+
__arch_ftrace_set_direct_caller(&(fregs)->regs, addr)
138145
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */
139146

140147
#endif /* __ASSEMBLY__ */

arch/riscv/kernel/mcount-dyn.S

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,14 +229,18 @@ SYM_FUNC_END(ftrace_caller)
229229

230230
#else /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */
231231
SYM_FUNC_START(ftrace_regs_caller)
232+
mv t1, zero
232233
SAVE_ABI_REGS 1
233234
PREPARE_ARGS
234235

235236
SYM_INNER_LABEL(ftrace_regs_call, SYM_L_GLOBAL)
236237
call ftrace_stub
237238

238239
RESTORE_ABI_REGS 1
240+
bnez t1, .Ldirect
239241
jr t0
242+
.Ldirect:
243+
jr t1
240244
SYM_FUNC_END(ftrace_regs_caller)
241245

242246
SYM_FUNC_START(ftrace_caller)
@@ -250,3 +254,9 @@ SYM_INNER_LABEL(ftrace_call, SYM_L_GLOBAL)
250254
jr t0
251255
SYM_FUNC_END(ftrace_caller)
252256
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_REGS */
257+
258+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
259+
SYM_CODE_START(ftrace_stub_direct_tramp)
260+
jr t0
261+
SYM_CODE_END(ftrace_stub_direct_tramp)
262+
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */

0 commit comments

Comments
 (0)