Skip to content

Commit 7b0a096

Browse files
Haoran Jiangchenhuacai
authored andcommitted
LoongArch: Replace kretprobe with rethook
This is an adaptation of commit f3a112c ("x86,rethook,kprobes: Replace kretprobe with rethook on x86") and commit b57c2f1 ("riscv: add riscv rethook implementation") to LoongArch. Mainly refer to commit b57c2f1 ("riscv: add riscv rethook implementation"). Replaces the kretprobe code with rethook on LoongArch. With this patch, kretprobe on LoongArch uses the rethook instead of kretprobe specific trampoline code. Signed-off-by: Haoran Jiang <[email protected]> Signed-off-by: Huacai Chen <[email protected]>
1 parent f02644e commit 7b0a096

File tree

7 files changed

+44
-28
lines changed

7 files changed

+44
-28
lines changed

arch/loongarch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ config LOONGARCH
127127
select HAVE_PERF_REGS
128128
select HAVE_PERF_USER_STACK_DUMP
129129
select HAVE_REGS_AND_STACK_ACCESS_API
130+
select HAVE_RETHOOK
130131
select HAVE_RSEQ
131132
select HAVE_SAMPLE_FTRACE_DIRECT
132133
select HAVE_SAMPLE_FTRACE_DIRECT_MULTI

arch/loongarch/include/asm/kprobes.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,6 @@ bool kprobe_fault_handler(struct pt_regs *regs, int trapnr);
4949
bool kprobe_breakpoint_handler(struct pt_regs *regs);
5050
bool kprobe_singlestep_handler(struct pt_regs *regs);
5151

52-
void __kretprobe_trampoline(void);
53-
void *trampoline_probe_handler(struct pt_regs *regs);
54-
5552
#else /* !CONFIG_KPROBES */
5653

5754
static inline bool kprobe_breakpoint_handler(struct pt_regs *regs) { return false; }

arch/loongarch/kernel/Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ ifdef CONFIG_FUNCTION_TRACER
2828
CFLAGS_REMOVE_inst.o = $(CC_FLAGS_FTRACE)
2929
CFLAGS_REMOVE_time.o = $(CC_FLAGS_FTRACE)
3030
CFLAGS_REMOVE_perf_event.o = $(CC_FLAGS_FTRACE)
31+
CFLAGS_REMOVE_rethook.o = $(CC_FLAGS_FTRACE)
32+
CFLAGS_REMOVE_rethook_trampoline.o = $(CC_FLAGS_FTRACE)
3133
endif
3234

3335
obj-$(CONFIG_MODULES) += module.o module-sections.o
@@ -52,7 +54,8 @@ obj-$(CONFIG_UNWINDER_PROLOGUE) += unwind_prologue.o
5254
obj-$(CONFIG_PERF_EVENTS) += perf_event.o perf_regs.o
5355
obj-$(CONFIG_HAVE_HW_BREAKPOINT) += hw_breakpoint.o
5456

55-
obj-$(CONFIG_KPROBES) += kprobes.o kprobes_trampoline.o
57+
obj-$(CONFIG_KPROBES) += kprobes.o
58+
obj-$(CONFIG_RETHOOK) += rethook.o rethook_trampoline.o
5659

5760
obj-$(CONFIG_JUMP_LABEL) += jump_label.o
5861

arch/loongarch/kernel/kprobes.c

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -378,27 +378,6 @@ int __init arch_init_kprobes(void)
378378
return 0;
379379
}
380380

381-
/* ASM function that handles the kretprobes must not be probed */
382-
NOKPROBE_SYMBOL(__kretprobe_trampoline);
383-
384-
/* Called from __kretprobe_trampoline */
385-
void __used *trampoline_probe_handler(struct pt_regs *regs)
386-
{
387-
return (void *)kretprobe_trampoline_handler(regs, NULL);
388-
}
389-
NOKPROBE_SYMBOL(trampoline_probe_handler);
390-
391-
void arch_prepare_kretprobe(struct kretprobe_instance *ri,
392-
struct pt_regs *regs)
393-
{
394-
ri->ret_addr = (kprobe_opcode_t *)regs->regs[1];
395-
ri->fp = NULL;
396-
397-
/* Replace the return addr with trampoline addr */
398-
regs->regs[1] = (unsigned long)&__kretprobe_trampoline;
399-
}
400-
NOKPROBE_SYMBOL(arch_prepare_kretprobe);
401-
402381
int arch_trampoline_kprobe(struct kprobe *p)
403382
{
404383
return 0;

arch/loongarch/kernel/rethook.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Generic return hook for LoongArch.
4+
*/
5+
6+
#include <linux/kprobes.h>
7+
#include <linux/rethook.h>
8+
#include "rethook.h"
9+
10+
/* This is called from arch_rethook_trampoline() */
11+
unsigned long __used arch_rethook_trampoline_callback(struct pt_regs *regs)
12+
{
13+
return rethook_trampoline_handler(regs, 0);
14+
}
15+
NOKPROBE_SYMBOL(arch_rethook_trampoline_callback);
16+
17+
void arch_rethook_prepare(struct rethook_node *rhn, struct pt_regs *regs, bool mcount)
18+
{
19+
rhn->frame = 0;
20+
rhn->ret_addr = regs->regs[1];
21+
22+
/* replace return addr with trampoline */
23+
regs->regs[1] = (unsigned long)arch_rethook_trampoline;
24+
}
25+
NOKPROBE_SYMBOL(arch_rethook_prepare);
26+
27+
/* ASM function that handles the rethook must not be probed itself */
28+
NOKPROBE_SYMBOL(arch_rethook_trampoline);

arch/loongarch/kernel/rethook.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __LOONGARCH_RETHOOK_H
3+
#define __LOONGARCH_RETHOOK_H
4+
5+
unsigned long arch_rethook_trampoline_callback(struct pt_regs *regs);
6+
void arch_rethook_prepare(struct rethook_node *rhn, struct pt_regs *regs, bool mcount);
7+
8+
#endif

arch/loongarch/kernel/kprobes_trampoline.S renamed to arch/loongarch/kernel/rethook_trampoline.S

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@
7575
csrxchg t0, t1, LOONGARCH_CSR_CRMD
7676
.endm
7777

78-
SYM_CODE_START(__kretprobe_trampoline)
78+
SYM_CODE_START(arch_rethook_trampoline)
7979
addi.d sp, sp, -PT_SIZE
8080
save_all_base_regs
8181

@@ -84,7 +84,7 @@ SYM_CODE_START(__kretprobe_trampoline)
8484

8585
move a0, sp /* pt_regs */
8686

87-
bl trampoline_probe_handler
87+
bl arch_rethook_trampoline_callback
8888

8989
/* use the result as the return-address */
9090
move ra, a0
@@ -93,4 +93,4 @@ SYM_CODE_START(__kretprobe_trampoline)
9393
addi.d sp, sp, PT_SIZE
9494

9595
jr ra
96-
SYM_CODE_END(__kretprobe_trampoline)
96+
SYM_CODE_END(arch_rethook_trampoline)

0 commit comments

Comments
 (0)