arm64: Add support for function error injection
authorLeo Yan <leo.yan@linaro.org>
Tue, 6 Aug 2019 10:00:14 +0000 (18:00 +0800)
committerWill Deacon <will@kernel.org>
Wed, 7 Aug 2019 12:53:09 +0000 (13:53 +0100)
Inspired by the commit 7cd01b08d35f ("powerpc: Add support for function
error injection"), this patch supports function error injection for
Arm64.

This patch mainly support two functions: one is regs_set_return_value()
which is used to overwrite the return value; the another function is
override_function_with_return() which is to override the probed
function returning and jump to its caller.

Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Leo Yan <leo.yan@linaro.org>
Signed-off-by: Will Deacon <will@kernel.org>
arch/arm64/Kconfig
arch/arm64/include/asm/ptrace.h
arch/arm64/lib/Makefile
arch/arm64/lib/error-inject.c [new file with mode: 0644]

index 3adcec0..b15803a 100644 (file)
@@ -148,6 +148,7 @@ config ARM64
        select HAVE_FAST_GUP
        select HAVE_FTRACE_MCOUNT_RECORD
        select HAVE_FUNCTION_TRACER
+       select HAVE_FUNCTION_ERROR_INJECTION
        select HAVE_FUNCTION_GRAPH_TRACER
        select HAVE_GCC_PLUGINS
        select HAVE_HW_BREAKPOINT if PERF_EVENTS
index 1dcf63a..fbebb41 100644 (file)
@@ -301,6 +301,11 @@ static inline unsigned long regs_return_value(struct pt_regs *regs)
        return regs->regs[0];
 }
 
+static inline void regs_set_return_value(struct pt_regs *regs, unsigned long rc)
+{
+       regs->regs[0] = rc;
+}
+
 /**
  * regs_get_kernel_argument() - get Nth function argument in kernel
  * @regs:      pt_regs of that context
index 33c2a4a..f182ccb 100644 (file)
@@ -33,3 +33,5 @@ UBSAN_SANITIZE_atomic_ll_sc.o := n
 lib-$(CONFIG_ARCH_HAS_UACCESS_FLUSHCACHE) += uaccess_flushcache.o
 
 obj-$(CONFIG_CRC32) += crc32.o
+
+obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
diff --git a/arch/arm64/lib/error-inject.c b/arch/arm64/lib/error-inject.c
new file mode 100644 (file)
index 0000000..ed15021
--- /dev/null
@@ -0,0 +1,18 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/error-injection.h>
+#include <linux/kprobes.h>
+
+void override_function_with_return(struct pt_regs *regs)
+{
+       /*
+        * 'regs' represents the state on entry of a predefined function in
+        * the kernel/module and which is captured on a kprobe.
+        *
+        * When kprobe returns back from exception it will override the end
+        * of probed function and directly return to the predefined
+        * function's caller.
+        */
+       instruction_pointer_set(regs, procedure_link_pointer(regs));
+}
+NOKPROBE_SYMBOL(override_function_with_return);