Skip to content

Commit 9cdc3b6

Browse files
Youling Tangchenhuacai
authored andcommitted
LoongArch: ftrace: Add direct call support
Select the HAVE_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. There are a few cases to distinguish: - If a direct call ops is the only one tracing a function AND the direct called trampoline is within the reach of a 'bl' instruction -> the ftrace patchsite jumps to the trampoline - Else -> the ftrace patchsite jumps to the ftrace_regs_caller trampoline points to ftrace_list_ops so it iterates over all registered ftrace ops, including the direct call ops and calls its call_direct_funcs handler which stores the direct called trampoline's address in the ftrace_regs and the ftrace_regs_caller trampoline will return to that address instead of returning to the traced function Signed-off-by: Qing Zhang <zhangqing@loongson.cn> Signed-off-by: Youling Tang <tangyouling@loongson.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent 24d4f52 commit 9cdc3b6

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

arch/loongarch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ config LOONGARCH
9393
select HAVE_DMA_CONTIGUOUS
9494
select HAVE_DYNAMIC_FTRACE
9595
select HAVE_DYNAMIC_FTRACE_WITH_ARGS
96+
select HAVE_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
9697
select HAVE_DYNAMIC_FTRACE_WITH_REGS
9798
select HAVE_EBPF_JIT
9899
select HAVE_EFFICIENT_UNALIGNED_ACCESS if !ARCH_STRICT_ALIGN

arch/loongarch/include/asm/ftrace.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ ftrace_regs_set_instruction_pointer(struct ftrace_regs *fregs, unsigned long ip)
8282
#define ftrace_graph_func ftrace_graph_func
8383
void ftrace_graph_func(unsigned long ip, unsigned long parent_ip,
8484
struct ftrace_ops *op, struct ftrace_regs *fregs);
85+
86+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
87+
static inline void
88+
__arch_ftrace_set_direct_caller(struct pt_regs *regs, unsigned long addr)
89+
{
90+
regs->regs[13] = addr; /* t1 */
91+
}
92+
93+
#define arch_ftrace_set_direct_caller(fregs, addr) \
94+
__arch_ftrace_set_direct_caller(&(fregs)->regs, addr)
95+
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */
96+
8597
#endif
8698

8799
#endif /* __ASSEMBLY__ */

arch/loongarch/kernel/ftrace_dyn.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ static bool ftrace_find_callable_addr(struct dyn_ftrace *rec, struct module *mod
6565
unsigned long pc = rec->ip + LOONGARCH_INSN_SIZE;
6666
struct plt_entry *plt;
6767

68+
/*
69+
* If a custom trampoline is unreachable, rely on the ftrace_regs_caller
70+
* trampoline which knows how to indirectly reach that trampoline through
71+
* ops->direct_call.
72+
*/
73+
if (*addr != FTRACE_ADDR && *addr != FTRACE_REGS_ADDR && !reachable_by_bl(*addr, pc))
74+
*addr = FTRACE_REGS_ADDR;
75+
6876
/*
6977
* When the target is within range of the 'bl' instruction, use 'addr'
7078
* as-is and branch to that directly.

arch/loongarch/kernel/mcount_dyn.S

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
.if \allregs
4343
PTR_S tp, sp, PT_R2
4444
PTR_S t0, sp, PT_R12
45-
PTR_S t1, sp, PT_R13
4645
PTR_S t2, sp, PT_R14
4746
PTR_S t3, sp, PT_R15
4847
PTR_S t4, sp, PT_R16
@@ -64,6 +63,8 @@
6463
PTR_S zero, sp, PT_R0
6564
.endif
6665
PTR_S ra, sp, PT_ERA /* Save trace function ra at PT_ERA */
66+
move t1, zero
67+
PTR_S t1, sp, PT_R13
6768
PTR_ADDI t8, sp, PT_SIZE
6869
PTR_S t8, sp, PT_R3
6970
.endm
@@ -104,8 +105,12 @@ ftrace_common_return:
104105
PTR_L a7, sp, PT_R11
105106
PTR_L fp, sp, PT_R22
106107
PTR_L t0, sp, PT_ERA
108+
PTR_L t1, sp, PT_R13
107109
PTR_ADDI sp, sp, PT_SIZE
110+
bnez t1, .Ldirect
108111
jr t0
112+
.Ldirect:
113+
jr t1
109114
SYM_CODE_END(ftrace_common)
110115

111116
SYM_CODE_START(ftrace_caller)
@@ -147,3 +152,9 @@ SYM_CODE_START(return_to_handler)
147152
jr ra
148153
SYM_CODE_END(return_to_handler)
149154
#endif
155+
156+
#ifdef CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS
157+
SYM_CODE_START(ftrace_stub_direct_tramp)
158+
jr t0
159+
SYM_CODE_END(ftrace_stub_direct_tramp)
160+
#endif /* CONFIG_DYNAMIC_FTRACE_WITH_DIRECT_CALLS */

0 commit comments

Comments
 (0)