Skip to content

Commit 8b5ee2c

Browse files
seehearfeelchenhuacai
authored andcommitted
LoongArch: Add support for function error injection
Inspired by the commit 42d038c ("arm64: Add support for function error injection") and the commit ee55ff8 ("riscv: Add support for function error injection"), this patch supports function error injection for LoongArch. Mainly implement two functions: (1) regs_set_return_value() which is used to overwrite the return value, (2) override_function_with_return() which is used to override the probed function returning and jump to its caller. Here is a simple test under CONFIG_FUNCTION_ERROR_INJECTION and CONFIG_FAIL_FUNCTION: # echo sys_clone > /sys/kernel/debug/fail_function/inject # echo 100 > /sys/kernel/debug/fail_function/probability # dmesg bash: fork: Invalid argument # dmesg ... FAULT_INJECTION: forcing a failure. name fail_function, interval 1, probability 100, space 0, times 1 ... Call Trace: [<90000000002238f4>] show_stack+0x5c/0x180 [<90000000012e384c>] dump_stack_lvl+0x60/0x88 [<9000000000b1879c>] should_fail_ex+0x1b0/0x1f4 [<900000000032ead4>] fei_kprobe_handler+0x28/0x6c [<9000000000230970>] kprobe_breakpoint_handler+0xf0/0x118 [<90000000012e3e60>] do_bp+0x2c4/0x358 [<9000000002241924>] exception_handlers+0x1924/0x10000 [<900000000023b7d0>] sys_clone+0x0/0x4 [<90000000012e4744>] do_syscall+0x7c/0x94 [<9000000000221e44>] handle_syscall+0xc4/0x160 Tested-by: Hengqi Chen <hengqi.chen@gmail.com> Acked-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Signed-off-by: Tiezhu Yang <yangtiezhu@loongson.cn> Signed-off-by: Huacai Chen <chenhuacai@loongson.cn>
1 parent d4c937c commit 8b5ee2c

4 files changed

Lines changed: 18 additions & 0 deletions

File tree

arch/loongarch/Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ config LOONGARCH
100100
select HAVE_FAST_GUP
101101
select HAVE_FTRACE_MCOUNT_RECORD
102102
select HAVE_FUNCTION_ARG_ACCESS_API
103+
select HAVE_FUNCTION_ERROR_INJECTION
103104
select HAVE_FUNCTION_GRAPH_TRACER
104105
select HAVE_FUNCTION_TRACER
105106
select HAVE_GENERIC_VDSO

arch/loongarch/include/asm/ptrace.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,11 @@ static inline long regs_return_value(struct pt_regs *regs)
154154
return regs->regs[4];
155155
}
156156

157+
static inline void regs_set_return_value(struct pt_regs *regs, unsigned long val)
158+
{
159+
regs->regs[4] = val;
160+
}
161+
157162
#define instruction_pointer(regs) ((regs)->csr_era)
158163
#define profile_pc(regs) instruction_pointer(regs)
159164

arch/loongarch/lib/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@
55

66
lib-y += delay.o memset.o memcpy.o memmove.o \
77
clear_user.o copy_user.o csum.o dump_tlb.o unaligned.o
8+
9+
obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o

arch/loongarch/lib/error-inject.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
#include <linux/error-injection.h>
4+
#include <linux/kprobes.h>
5+
6+
void override_function_with_return(struct pt_regs *regs)
7+
{
8+
instruction_pointer_set(regs, regs->regs[1]);
9+
}
10+
NOKPROBE_SYMBOL(override_function_with_return);

0 commit comments

Comments
 (0)