Skip to contentSkip to conversation

Separate error injection table from kprobes

RFC PATCH Superseded Masami Hiramatsu 5 participants 30 messages Dec 26, 2017 – Jan 9, 2018

Subsystems (16)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Masami Hiramatsu

author maintainer
#0 lore raw

v2 cover Separate error injection table from kprobes

Hi Josef and Alexei,

Here are the 2nd version of patches to moving error injection table from kprobes. In this series I did a small fixes and add function-based fault injection.

Here is the previous version:

https://lkml.org/lkml/2017/12/22/554

There are 2 main reasons why I separate it from kprobes.

- kprobes users can modify execution path not only at error-injection whitelist functions but also other functions. I don't like to suggest user that such limitation is from kprobes itself.

- This error injection information is also useful for ftrace (function-hook) and livepatch. It should not be limited by CONFIG_KPROBES.

So I introduced CONFIG_FUNCTION_ERROR_INJECTION for this feature. Also CONFIG_FAIL_FUNCTION is added, which provides function-based error injection interface via debugfs following fault-injection framework. See [4/4].

Any thoughts?

BTW, I think we should add an error-range description in ALLOW_ERROR_INJECTION() macro. If user sets a success return value and override it by mistake, caller must break data or cause kernel panic.

Thank you,

---

Masami Hiramatsu (4):
tracing/kprobe: bpf: Check error injectable event is on function entry tracing/kprobe: bpf: Compare instruction pointer with original one
error-injection: Separate error-injection from kprobe
error-injection: Support fault injection framework

 Documentation/fault-injection/fault-injection.txt |    5 +
 arch/Kconfig                                      |    2 
 arch/x86/Kconfig                                  |    2 
 arch/x86/include/asm/error-injection.h            |   12 +
 arch/x86/kernel/kprobes/ftrace.c                  |   14 -
 arch/x86/lib/Makefile                             |    2 
 arch/x86/lib/error-inject.c                       |   19 ++
 fs/btrfs/disk-io.c                                |    2 
 fs/btrfs/free-space-cache.c                       |    2 
 include/asm-generic/error-injection.h             |   20 ++
 include/asm-generic/vmlinux.lds.h                 |   14 +
 include/linux/bpf.h                               |   12 -
 include/linux/error-injection.h                   |   21 ++
 include/linux/kprobes.h                           |    1 
 include/linux/module.h                            |    6 -
 kernel/Makefile                                   |    1 
 kernel/fail_function.c                            |  169 ++++++++++++++++++
 kernel/kprobes.c                                  |  163 -----------------
 kernel/module.c                                   |    8 -
 kernel/trace/Kconfig                              |    4 
 kernel/trace/bpf_trace.c                          |    9 -
 kernel/trace/trace_kprobe.c                       |   32 +--
 kernel/trace/trace_probe.h                        |   12 +
 lib/Kconfig.debug                                 |   14 +
 lib/Makefile                                      |    1 
 lib/error-inject.c                                |  198 +++++++++++++++++++++
 26 files changed, 506 insertions(+), 239 deletions(-)
 create mode 100644 arch/x86/include/asm/error-injection.h
 create mode 100644 arch/x86/lib/error-inject.c
 create mode 100644 include/asm-generic/error-injection.h
 create mode 100644 include/linux/error-injection.h
 create mode 100644 kernel/fail_function.c
 create mode 100644 lib/error-inject.c

--
Masami Hiramatsu (Linaro)

author maintainer
#1 lore raw

RFC PATCH v2 1/4 tracing/kprobe: bpf: Check error injectable event is on function entry

Check whether error injectable event is on function entry or not. Currently it checks the event is ftrace-based kprobes or not, but that is wrong. It should check if the event is on the entry of target function. Since error injection will override a function to just return with modified return value, that operation must be done before the target function starts making stackframe.

As a side effect, bpf error injection is no need to depend on function-tracer. It can work with sw-breakpoint based kprobe events too.

---
 kernel/trace/Kconfig        |    2 --
 kernel/trace/bpf_trace.c    |    6 +++---
 kernel/trace/trace_kprobe.c |    8 +++++---
 kernel/trace/trace_probe.h  |   12 ++++++------
 4 files changed, 14 insertions(+), 14 deletions(-)
kernel/trace/Kconfig +0 -2
index ae3a2d519e50..6400e1bf97c5 100644
@@ -533,9 +533,7 @@ config FUNCTION_PROFILER
 config BPF_KPROBE_OVERRIDE
 	bool "Enable BPF programs to override a kprobed function"
 	depends on BPF_EVENTS
-	depends on KPROBES_ON_FTRACE
 	depends on HAVE_KPROBE_OVERRIDE
-	depends on DYNAMIC_FTRACE_WITH_REGS
 	default n
 	help
 	 Allows BPF to override the execution of a probed function and
kernel/trace/bpf_trace.c +3 -3
index f6d2327ecb59..d663660f8392 100644
@@ -800,11 +800,11 @@ int perf_event_attach_bpf_prog(struct perf_event *event,
 	int ret = -EEXIST;
 
 	/*
-	 * Kprobe override only works for ftrace based kprobes, and only if they
-	 * are on the opt-in list.
+	 * Kprobe override only works if they are on the function entry,
+	 * and only if they are on the opt-in list.
 	 */
 	if (prog->kprobe_override &&
-	    (!trace_kprobe_ftrace(event->tp_event) ||
+	    (!trace_kprobe_on_func_entry(event->tp_event) ||
 	     !trace_kprobe_error_injectable(event->tp_event)))
 		return -EINVAL;
 
kernel/trace/trace_kprobe.c +5 -3
index 91f4b57dab82..265e3e27e8dc 100644
@@ -88,13 +88,15 @@ static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
 	return nhit;
 }
 
-int trace_kprobe_ftrace(struct trace_event_call *call)
+bool trace_kprobe_on_func_entry(struct trace_event_call *call)
 {
 	struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
-	return kprobe_ftrace(&tk->rp.kp);
+
+	return kprobe_on_func_entry(tk->rp.kp.addr, tk->rp.kp.symbol_name,
+				    tk->rp.kp.offset);
 }
 
-int trace_kprobe_error_injectable(struct trace_event_call *call)
+bool trace_kprobe_error_injectable(struct trace_event_call *call)
 {
 	struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
 	unsigned long addr;
kernel/trace/trace_probe.h +6 -6
index 5e54d748c84c..e101c5bb9eda 100644
@@ -252,8 +252,8 @@ struct symbol_cache;
 unsigned long update_symbol_cache(struct symbol_cache *sc);
 void free_symbol_cache(struct symbol_cache *sc);
 struct symbol_cache *alloc_symbol_cache(const char *sym, long offset);
-int trace_kprobe_ftrace(struct trace_event_call *call);
-int trace_kprobe_error_injectable(struct trace_event_call *call);
+bool trace_kprobe_on_func_entry(struct trace_event_call *call);
+bool trace_kprobe_error_injectable(struct trace_event_call *call);
 #else
 /* uprobes do not support symbol fetch methods */
 #define fetch_symbol_u8			NULL
@@ -280,14 +280,14 @@ alloc_symbol_cache(const char *sym, long offset)
 	return NULL;
 }
 
-static inline int trace_kprobe_ftrace(struct trace_event_call *call)
+static inline bool trace_kprobe_on_func_entry(struct trace_event_call *call)
 {
-	return 0;
+	return false;
 }
 
-static inline int trace_kprobe_error_injectable(struct trace_event_call *call)
+static inline bool trace_kprobe_error_injectable(struct trace_event_call *call)
 {
-	return 0;
+	return false;
 }
 #endif /* CONFIG_KPROBE_EVENTS */
 
author maintainer
#15 lore raw

RFC PATCH v2 2/4 tracing/kprobe: bpf: Compare instruction pointer with original one

In reply to Masami Hiramatsu

Compare instruction pointer with original one on the stack instead using per-cpu bpf_kprobe_override flag.

This patch also consolidates reset_current_kprobe() and preempt_enable_no_resched() blocks. Those can be done in one place.

---
 kernel/trace/bpf_trace.c    |    1 -
 kernel/trace/trace_kprobe.c |   21 +++++++--------------
 2 files changed, 7 insertions(+), 15 deletions(-)
kernel/trace/bpf_trace.c +0 -1
index d663660f8392..cefa9b0e396c 100644
@@ -83,7 +83,6 @@ EXPORT_SYMBOL_GPL(trace_call_bpf);
 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
 {
-	__this_cpu_write(bpf_kprobe_override, 1);
 	regs_set_return_value(regs, rc);
 	arch_ftrace_kprobe_override_function(regs);
 	return 0;
kernel/trace/trace_kprobe.c +7 -14
index 265e3e27e8dc..a7c7035963f2 100644
@@ -42,8 +42,6 @@ struct trace_kprobe {
 	(offsetof(struct trace_kprobe, tp.args) +	\
 	(sizeof(struct probe_arg) * (n)))
 
-DEFINE_PER_CPU(int, bpf_kprobe_override);
-
 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
 {
 	return tk->rp.handler != NULL;
@@ -1204,6 +1202,7 @@ kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
 	int rctx;
 
 	if (bpf_prog_array_valid(call)) {
+		unsigned long orig_ip = instruction_pointer(regs);
 		int ret;
 
 		ret = trace_call_bpf(call, regs);
@@ -1211,12 +1210,13 @@ kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
 		/*
 		 * We need to check and see if we modified the pc of the
 		 * pt_regs, and if so clear the kprobe and return 1 so that we
-		 * don't do the instruction skipping.  Also reset our state so
-		 * we are clean the next pass through.
+		 * don't do the single stepping.
+		 * The ftrace kprobe handler leaves it up to us to re-enable
+		 * preemption here before returning if we've modified the ip.
 		 */
-		if (__this_cpu_read(bpf_kprobe_override)) {
-			__this_cpu_write(bpf_kprobe_override, 0);
+		if (orig_ip != instruction_pointer(regs)) {
 			reset_current_kprobe();
+			preempt_enable_no_resched();
 			return 1;
 		}
 		if (!ret)
@@ -1324,15 +1324,8 @@ static int kprobe_dispatcher(struct kprobe *kp, struct pt_regs *regs)
 	if (tk->tp.flags & TP_FLAG_TRACE)
 		kprobe_trace_func(tk, regs);
 #ifdef CONFIG_PERF_EVENTS
-	if (tk->tp.flags & TP_FLAG_PROFILE) {
+	if (tk->tp.flags & TP_FLAG_PROFILE)
 		ret = kprobe_perf_func(tk, regs);
-		/*
-		 * The ftrace kprobe handler leaves it up to us to re-enable
-		 * preemption here before returning if we've modified the ip.
-		 */
-		if (ret)
-			preempt_enable_no_resched();
-	}
 #endif
 	return ret;
 }
author maintainer
#17 lore raw

RFC PATCH v2 3/4 error-injection: Separate error-injection from kprobe

In reply to Masami Hiramatsu

Since error-injection framework is not limited to be used by kprobes, nor bpf. Other kernel subsystems can use it freely for checking safeness of error-injection, e.g. livepatch, ftrace etc.
So this separate error-injection framework from kprobes.

Some differences has been made:

- "kprobe" word is removed from any APIs/structures.
- BPF_ALLOW_ERROR_INJECTION() is renamed to ALLOW_ERROR_INJECTION() since it is not limited for BPF too.
- CONFIG_FUNCTION_ERROR_INJECTION is the config item of this feature. It is automatically enabled if the arch supports error injection feature for kprobe or ftrace etc.

---
Changes in v2:
- Fix the override function name to override_function_with_return()
- Show only function name in the list, user don't have to care about it's size, since function override only happens at the entry.

---
 arch/Kconfig                           |    2 
 arch/x86/Kconfig                       |    2 
 arch/x86/include/asm/error-injection.h |   12 ++
 arch/x86/kernel/kprobes/ftrace.c       |   14 --
 arch/x86/lib/Makefile                  |    2 
 arch/x86/lib/error-inject.c            |   19 +++
 fs/btrfs/disk-io.c                     |    2 
 fs/btrfs/free-space-cache.c            |    2 
 include/asm-generic/error-injection.h  |   20 +++
 include/asm-generic/vmlinux.lds.h      |   14 +-
 include/linux/bpf.h                    |   12 --
 include/linux/error-injection.h        |   21 +++
 include/linux/kprobes.h                |    1 
 include/linux/module.h                 |    6 -
 kernel/kprobes.c                       |  163 --------------------------
 kernel/module.c                        |    8 +
 kernel/trace/Kconfig                   |    2 
 kernel/trace/bpf_trace.c               |    2 
 kernel/trace/trace_kprobe.c            |    3 
 lib/Kconfig.debug                      |    4 +
 lib/Makefile                           |    1 
 lib/error-inject.c                     |  198 ++++++++++++++++++++++++++++++++
 22 files changed, 300 insertions(+), 210 deletions(-)
 create mode 100644 arch/x86/include/asm/error-injection.h
 create mode 100644 arch/x86/lib/error-inject.c
 create mode 100644 include/asm-generic/error-injection.h
 create mode 100644 include/linux/error-injection.h
 create mode 100644 lib/error-inject.c
arch/Kconfig +1 -1
index d3f4aaf9cb7a..97376accfb14 100644
@@ -196,7 +196,7 @@ config HAVE_OPTPROBES
 config HAVE_KPROBES_ON_FTRACE
 	bool
 
-config HAVE_KPROBE_OVERRIDE
+config HAVE_FUNCTION_ERROR_INJECTION
 	bool
 
 config HAVE_NMI
arch/x86/Kconfig +1 -1
index 04d66e6fa447..fc519e3ae754 100644
@@ -154,7 +154,7 @@ config X86
 	select HAVE_KERNEL_XZ
 	select HAVE_KPROBES
 	select HAVE_KPROBES_ON_FTRACE
-	select HAVE_KPROBE_OVERRIDE
+	select HAVE_FUNCTION_ERROR_INJECTION
 	select HAVE_KRETPROBES
 	select HAVE_KVM
 	select HAVE_LIVEPATCH			if X86_64
arch/x86/include/asm/error-injection.h new file +12 -0
new file mode 100644
index 000000000000..6c2a133622f4
@@ -0,0 +1,12 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_ERROR_INJECTION_H
+#define _ASM_ERROR_INJECTION_H
+
+#include <asm/linkage.h>
+#include <asm/ptrace.h>
+#include <asm-generic/error-injection.h>
+
+asmlinkage void just_return_func(void);
+void override_function_with_return(struct pt_regs *regs);
+
+#endif /* _ASM_ERROR_INJECTION_H */
arch/x86/kernel/kprobes/ftrace.c +0 -14
index 1ea748d682fd..8dc0161cec8f 100644
@@ -97,17 +97,3 @@ int arch_prepare_kprobe_ftrace(struct kprobe *p)
 	p->ainsn.boostable = false;
 	return 0;
 }
-
-asmlinkage void override_func(void);
-asm(
-	".type override_func, @function\n"
-	"override_func:\n"
-	"	ret\n"
-	".size override_func, .-override_func\n"
-);
-
-void arch_ftrace_kprobe_override_function(struct pt_regs *regs)
-{
-	regs->ip = (unsigned long)&override_func;
-}
-NOKPROBE_SYMBOL(arch_ftrace_kprobe_override_function);
arch/x86/lib/Makefile +2 -0
index 7b181b61170e..081f09435d28 100644
@@ -26,6 +26,8 @@ lib-y += memcpy_$(BITS).o
 lib-$(CONFIG_RWSEM_XCHGADD_ALGORITHM) += rwsem.o
 lib-$(CONFIG_INSTRUCTION_DECODER) += insn.o inat.o insn-eval.o
 lib-$(CONFIG_RANDOMIZE_BASE) += kaslr.o
+lib-$(CONFIG_FUNCTION_ERROR_INJECTION)	+= error-inject.o
+
 
 obj-y += msr.o msr-reg.o msr-reg-export.o hweight.o
 
arch/x86/lib/error-inject.c new file +19 -0
new file mode 100644
index 000000000000..7b881d03d0dd
@@ -0,0 +1,19 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <linux/error-injection.h>
+#include <linux/kprobes.h>
+
+asmlinkage void just_return_func(void);
+
+asm(
+	".type just_return_func, @function\n"
+	"just_return_func:\n"
+	"	ret\n"
+	".size just_return_func, .-just_return_func\n"
+);
+
+void override_function_with_return(struct pt_regs *regs)
+{
+	regs->ip = (unsigned long)&just_return_func;
+}
+NOKPROBE_SYMBOL(override_function_with_return);
fs/btrfs/disk-io.c +1 -1
index 5da18ebc9222..5c540129ad81 100644
@@ -3124,7 +3124,7 @@ int open_ctree(struct super_block *sb,
 		goto fail_block_groups;
 	goto retry_root_backup;
 }
-BPF_ALLOW_ERROR_INJECTION(open_ctree);
+ALLOW_ERROR_INJECTION(open_ctree);
 
 static void btrfs_end_buffer_write_sync(struct buffer_head *bh, int uptodate)
 {
fs/btrfs/free-space-cache.c +1 -1
index fb1382893bfc..2a75e088b215 100644
@@ -333,7 +333,7 @@ static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,
 
 	return 0;
 }
-BPF_ALLOW_ERROR_INJECTION(io_ctl_init);
+ALLOW_ERROR_INJECTION(io_ctl_init);
 
 static void io_ctl_free(struct btrfs_io_ctl *io_ctl)
 {
include/asm-generic/error-injection.h new file +20 -0
new file mode 100644
index 000000000000..08352c9d9f97
@@ -0,0 +1,20 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _ASM_GENERIC_ERROR_INJECTION_H
+#define _ASM_GENERIC_ERROR_INJECTION_H
+
+#if defined(__KERNEL__) && !defined(__ASSEMBLY__)
+#ifdef CONFIG_FUNCTION_ERROR_INJECTION
+/*
+ * Whitelist ganerating macro. Specify functions which can be
+ * error-injectable using this macro.
+ */
+#define ALLOW_ERROR_INJECTION(fname)					\
+static unsigned long __used						\
+	__attribute__((__section__("_error_injection_whitelist")))	\
+	_eil_addr_##fname = (unsigned long)fname;
+#else
+#define ALLOW_ERROR_INJECTION(fname)
+#endif
+#endif
+
+#endif /* _ASM_GENERIC_ERROR_INJECTION_H */
include/asm-generic/vmlinux.lds.h +7 -7
index a2e8582d094a..fad8b8c4210c 100644
@@ -136,13 +136,13 @@
 #define KPROBE_BLACKLIST()
 #endif
 
-#ifdef CONFIG_BPF_KPROBE_OVERRIDE
-#define ERROR_INJECT_LIST()	. = ALIGN(8);						\
-				VMLINUX_SYMBOL(__start_kprobe_error_inject_list) = .;	\
-				KEEP(*(_kprobe_error_inject_list))			\
-				VMLINUX_SYMBOL(__stop_kprobe_error_inject_list) = .;
+#ifdef CONFIG_FUNCTION_ERROR_INJECTION
+#define ERROR_INJECT_WHITELIST()	. = ALIGN(8);					\
+				VMLINUX_SYMBOL(__start_error_injection_whitelist) = .;	\
+				KEEP(*(_error_injection_whitelist))			\
+				VMLINUX_SYMBOL(__stop_error_injection_whitelist) = .;
 #else
-#define ERROR_INJECT_LIST()
+#define ERROR_INJECT_WHITELIST()
 #endif
 
 #ifdef CONFIG_EVENT_TRACING
@@ -573,7 +573,7 @@
 	FTRACE_EVENTS()							\
 	TRACE_SYSCALLS()						\
 	KPROBE_BLACKLIST()						\
-	ERROR_INJECT_LIST()						\
+	ERROR_INJECT_WHITELIST()					\
 	MEM_DISCARD(init.rodata)					\
 	CLK_OF_TABLES()							\
 	RESERVEDMEM_OF_TABLES()						\
include/linux/bpf.h +1 -11
index da54ef644fcd..6426a6a81b3e 100644
@@ -16,6 +16,7 @@
 #include <linux/rbtree_latch.h>
 #include <linux/numa.h>
 #include <linux/wait.h>
+#include <linux/error-injection.h>
 
 struct perf_event;
 struct bpf_prog;
@@ -583,15 +584,4 @@ extern const struct bpf_func_proto bpf_sock_map_update_proto;
 void bpf_user_rnd_init_once(void);
 u64 bpf_user_rnd_u32(u64 r1, u64 r2, u64 r3, u64 r4, u64 r5);
 
-#if defined(__KERNEL__) && !defined(__ASSEMBLY__)
-#ifdef CONFIG_BPF_KPROBE_OVERRIDE
-#define BPF_ALLOW_ERROR_INJECTION(fname)				\
-static unsigned long __used						\
-	__attribute__((__section__("_kprobe_error_inject_list")))	\
-	_eil_addr_##fname = (unsigned long)fname;
-#else
-#define BPF_ALLOW_ERROR_INJECTION(fname)
-#endif
-#endif
-
 #endif /* _LINUX_BPF_H */
include/linux/error-injection.h new file +21 -0
new file mode 100644
index 000000000000..130a67c50dac
@@ -0,0 +1,21 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+#ifndef _LINUX_ERROR_INJECTION_H
+#define _LINUX_ERROR_INJECTION_H
+
+#ifdef CONFIG_FUNCTION_ERROR_INJECTION
+
+#include <asm/error-injection.h>
+
+extern bool within_error_injection_list(unsigned long addr);
+
+#else /* !CONFIG_FUNCTION_ERROR_INJECTION */
+
+#include <asm-generic/error-injection.h>
+static inline bool within_error_injection_list(unsigned long addr)
+{
+	return false;
+}
+
+#endif
+
+#endif /* _LINUX_ERROR_INJECTION_H */
include/linux/kprobes.h +0 -1
index 963fd364f3d6..9440a2fc8893 100644
@@ -271,7 +271,6 @@ extern bool arch_kprobe_on_func_entry(unsigned long offset);
 extern bool kprobe_on_func_entry(kprobe_opcode_t *addr, const char *sym, unsigned long offset);
 
 extern bool within_kprobe_blacklist(unsigned long addr);
-extern bool within_kprobe_error_injection_list(unsigned long addr);
 
 struct kprobe_insn_cache {
 	struct mutex mutex;
include/linux/module.h +3 -3
index 548fa09fa806..792e51d83bda 100644
@@ -476,9 +476,9 @@ struct module {
 	unsigned int num_ctors;
 #endif
 
-#ifdef CONFIG_BPF_KPROBE_OVERRIDE
-	unsigned int num_kprobe_ei_funcs;
-	unsigned long *kprobe_ei_funcs;
+#ifdef CONFIG_FUNCTION_ERROR_INJECTION
+	unsigned int num_ei_funcs;
+	unsigned long *ei_funcs;
 #endif
 } ____cacheline_aligned __randomize_layout;
 #ifndef MODULE_ARCH_INIT
kernel/kprobes.c +0 -163

205 lines not on this page · raw message

kernel/module.c +4 -4
index bd695bfdc5c4..588f86a1f9c3 100644
@@ -3118,10 +3118,10 @@ static int find_module_sections(struct module *mod, struct load_info *info)
 					     sizeof(*mod->ftrace_callsites),
 					     &mod->num_ftrace_callsites);
 #endif
-#ifdef CONFIG_BPF_KPROBE_OVERRIDE
-	mod->kprobe_ei_funcs = section_objs(info, "_kprobe_error_inject_list",
-					    sizeof(*mod->kprobe_ei_funcs),
-					    &mod->num_kprobe_ei_funcs);
+#ifdef CONFIG_FUNCTION_ERROR_INJECT
+	mod->ei_funcs = section_objs(info, "_error_injection_whitelist",
+					    sizeof(*mod->ei_funcs),
+					    &mod->num_ei_funcs);
 #endif
 	mod->extable = section_objs(info, "__ex_table",
 				    sizeof(*mod->extable), &mod->num_exentries);
kernel/trace/Kconfig +1 -1
index 6400e1bf97c5..a356b8c1f830 100644
@@ -533,7 +533,7 @@ config FUNCTION_PROFILER
 config BPF_KPROBE_OVERRIDE
 	bool "Enable BPF programs to override a kprobed function"
 	depends on BPF_EVENTS
-	depends on HAVE_KPROBE_OVERRIDE
+	depends on FUNCTION_ERROR_INJECT
 	default n
 	help
 	 Allows BPF to override the execution of a probed function and
kernel/trace/bpf_trace.c +1 -1
index cefa9b0e396c..460d5a899cef 100644
@@ -84,7 +84,7 @@ EXPORT_SYMBOL_GPL(trace_call_bpf);
 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
 {
 	regs_set_return_value(regs, rc);
-	arch_ftrace_kprobe_override_function(regs);
+	override_function_with_return(regs);
 	return 0;
 }
 
kernel/trace/trace_kprobe.c +2 -1
index a7c7035963f2..23f88a062965 100644
@@ -21,6 +21,7 @@
 #include <linux/module.h>
 #include <linux/uaccess.h>
 #include <linux/rculist.h>
+#include <linux/error-injection.h>
 
 #include "trace_probe.h"
 
@@ -106,7 +107,7 @@ bool trace_kprobe_error_injectable(struct trace_event_call *call)
 	} else {
 		addr = (unsigned long)tk->rp.kp.addr;
 	}
-	return within_kprobe_error_injection_list(addr);
+	return within_error_injection_list(addr);
 }
 
 static int register_kprobe_event(struct trace_kprobe *tk);
lib/Kconfig.debug +4 -0
index 9d5b78aad4c5..fe88ac0f003c 100644
@@ -1500,6 +1500,10 @@ config FAULT_INJECTION
 	  Provide fault-injection framework.
 	  For more details, see Documentation/fault-injection/.
 
+config FUNCTION_ERROR_INJECTION
+	def_bool y
+	depends on HAVE_FUNCTION_ERROR_INJECTION
+
 config FAILSLAB
 	bool "Fault-injection capability for kmalloc"
 	depends on FAULT_INJECTION
lib/Makefile +1 -0
index a6c8529dd9b2..75ec13778cd8 100644
@@ -149,6 +149,7 @@ obj-$(CONFIG_NETDEV_NOTIFIER_ERROR_INJECT) += netdev-notifier-error-inject.o
 obj-$(CONFIG_MEMORY_NOTIFIER_ERROR_INJECT) += memory-notifier-error-inject.o
 obj-$(CONFIG_OF_RECONFIG_NOTIFIER_ERROR_INJECT) += \
 	of-reconfig-notifier-error-inject.o
+obj-$(CONFIG_FUNCTION_ERROR_INJECTION) += error-inject.o
 
 lib-$(CONFIG_GENERIC_BUG) += bug.o
 
lib/error-inject.c new file +198 -0

198 lines not on this page · raw message

author maintainer
#19 lore raw

RFC PATCH v2 4/4 error-injection: Support fault injection framework

In reply to Masami Hiramatsu

Support in-kernel fault-injection framework via debugfs. This allows you to inject a conditional error to specified function using debugfs interfaces.

---
 Documentation/fault-injection/fault-injection.txt |    5 +
 kernel/Makefile                                   |    1 
 kernel/fail_function.c                            |  169 +++++++++++++++++++++
 lib/Kconfig.debug                                 |   10 +
 4 files changed, 185 insertions(+)
 create mode 100644 kernel/fail_function.c
Documentation/fault-injection/fault-injection.txt +5 -0
index 918972babcd8..6243a588dd71 100644
@@ -30,6 +30,11 @@ o fail_mmc_request
   injects MMC data errors on devices permitted by setting
   debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
 
+o fail_function
+
+  injects error return on specific functions by setting debugfs entries
+  under /sys/kernel/debug/fail_function. No boot option supported.
+
 Configure fault-injection capabilities behavior
 -----------------------------------------------
 
kernel/Makefile +1 -0
index 172d151d429c..f85ae5dfa474 100644
@@ -81,6 +81,7 @@ obj-$(CONFIG_AUDIT_TREE) += audit_tree.o
 obj-$(CONFIG_GCOV_KERNEL) += gcov/
 obj-$(CONFIG_KCOV) += kcov.o
 obj-$(CONFIG_KPROBES) += kprobes.o
+obj-$(CONFIG_FAIL_FUNCTION) += fail_function.o
 obj-$(CONFIG_KGDB) += debug/
 obj-$(CONFIG_DETECT_HUNG_TASK) += hung_task.o
 obj-$(CONFIG_LOCKUP_DETECTOR) += watchdog.o
kernel/fail_function.c new file +169 -0

169 lines not on this page · raw message

lib/Kconfig.debug +10 -0
index fe88ac0f003c..15c6609a1bb8 100644
@@ -1551,6 +1551,16 @@ config FAIL_FUTEX
 	help
 	  Provide fault-injection capability for futexes.
 
+config FAIL_FUNCTION
+	bool "Fault-injection capability for functions"
+	depends on FAULT_INJECTION_DEBUG_FS && FUNCTION_ERROR_INJECTION
+	help
+	  Provide function-based fault-injection capability.
+	  This will allow you to override a specific function with a return
+	  with given return value. As a result, function caller will see
+	  an error value and have to handle it. This is useful to test the
+	  error handling in various subsystems.
+
 config FAULT_INJECTION_DEBUG_FS
 	bool "Debugfs entries for fault-injection capabilities"
 	depends on FAULT_INJECTION && SYSFS && DEBUG_FS
#2 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

In reply to Masami Hiramatsu
Quoted 58 lines · Masami Hiramatsu

Check whether error injectable event is on function entry or not. Currently it checks the event is ftrace-based kprobes or not, but that is wrong. It should check if the event is on the entry of target function. Since error injection will override a function to just return with modified return value, that operation must be done before the target function starts making stackframe.

As a side effect, bpf error injection is no need to depend on function-tracer. It can work with sw-breakpoint based kprobe events too.

---
 kernel/trace/Kconfig        |    2 --
 kernel/trace/bpf_trace.c    |    6 +++---
 kernel/trace/trace_kprobe.c |    8 +++++---
 kernel/trace/trace_probe.h  |   12 ++++++------
 4 files changed, 14 insertions(+), 14 deletions(-)
kernel/trace/Kconfig +0 -2
index ae3a2d519e50..6400e1bf97c5 100644
@@ -533,9 +533,7 @@ config FUNCTION_PROFILER
 config BPF_KPROBE_OVERRIDE
 	bool "Enable BPF programs to override a kprobed function"
 	depends on BPF_EVENTS
-	depends on KPROBES_ON_FTRACE
 	depends on HAVE_KPROBE_OVERRIDE
-	depends on DYNAMIC_FTRACE_WITH_REGS
 	default n
 	help
 	 Allows BPF to override the execution of a probed function and
kernel/trace/bpf_trace.c +3 -3
index f6d2327ecb59..d663660f8392 100644
@@ -800,11 +800,11 @@ int perf_event_attach_bpf_prog(struct perf_event *event,
 	int ret = -EEXIST;
 
 	/*
-	 * Kprobe override only works for ftrace based kprobes, and only if they
-	 * are on the opt-in list.
+	 * Kprobe override only works if they are on the function entry,
+	 * and only if they are on the opt-in list.
 	 */
 	if (prog->kprobe_override &&
-	    (!trace_kprobe_ftrace(event->tp_event) ||
+	    (!trace_kprobe_on_func_entry(event->tp_event) ||
 	     !trace_kprobe_error_injectable(event->tp_event)))
 		return -EINVAL;
 
kernel/trace/trace_kprobe.c +0 -0
index 91f4b57dab82..265e3e27e8dc 100644
@@ -88,13 +88,15 @@ static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
 	return nhit;

kernel/trace/trace_kprobe.c static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk) · line 96

Quoted 10 lines
kernel/trace/trace_kprobe.c +4 -2
 }
 
-int trace_kprobe_ftrace(struct trace_event_call *call)
+bool trace_kprobe_on_func_entry(struct trace_event_call *call)
 {
 	struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
-	return kprobe_ftrace(&tk->rp.kp);
+
+	return kprobe_on_func_entry(tk->rp.kp.addr, tk->rp.kp.symbol_name,
+				    tk->rp.kp.offset);

That would be nice, but did you test this?
My understanding that kprobe will restore all regs and here we need to override return ip _and_ value. Could you add a patch with the test the way Josef did or describe the steps to test this new mode?

Acked-by on v2 2/4
#16 lore raw

Re: [RFC PATCH bpf-next v2 2/4] tracing/kprobe: bpf: Compare instruction pointer with original one

In reply to Masami Hiramatsu
Quoted 51 lines · Masami Hiramatsu

Compare instruction pointer with original one on the stack instead using per-cpu bpf_kprobe_override flag.

This patch also consolidates reset_current_kprobe() and preempt_enable_no_resched() blocks. Those can be done in one place.

---
 kernel/trace/bpf_trace.c    |    1 -
 kernel/trace/trace_kprobe.c |   21 +++++++--------------
 2 files changed, 7 insertions(+), 15 deletions(-)
kernel/trace/bpf_trace.c +0 -1
index d663660f8392..cefa9b0e396c 100644
@@ -83,7 +83,6 @@ EXPORT_SYMBOL_GPL(trace_call_bpf);
 #ifdef CONFIG_BPF_KPROBE_OVERRIDE
 BPF_CALL_2(bpf_override_return, struct pt_regs *, regs, unsigned long, rc)
 {
-	__this_cpu_write(bpf_kprobe_override, 1);
 	regs_set_return_value(regs, rc);
 	arch_ftrace_kprobe_override_function(regs);
 	return 0;
kernel/trace/trace_kprobe.c +1 -3
index 265e3e27e8dc..a7c7035963f2 100644
@@ -42,8 +42,6 @@ struct trace_kprobe {
 	(offsetof(struct trace_kprobe, tp.args) +	\
 	(sizeof(struct probe_arg) * (n)))
 
-DEFINE_PER_CPU(int, bpf_kprobe_override);
-
 static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
 {
 	return tk->rp.handler != NULL;
@@ -1204,6 +1202,7 @@ kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
 	int rctx;
 
 	if (bpf_prog_array_valid(call)) {
+		unsigned long orig_ip = instruction_pointer(regs);
 		int ret;
 
 		ret = trace_call_bpf(call, regs);
@@ -1211,12 +1210,13 @@ kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs)
 		/*
 		 * We need to check and see if we modified the pc of the
 		 * pt_regs, and if so clear the kprobe and return 1 so that we
-		 * don't do the instruction skipping.  Also reset our state so

kernel/trace/trace_kprobe.c kprobe_perf_func(struct trace_kprobe *tk, struct pt_regs *regs) · line 1219

Quoted 10 lines
kernel/trace/trace_kprobe.c +5 -3
-		 * we are clean the next pass through.
+		 * don't do the single stepping.
+		 * The ftrace kprobe handler leaves it up to us to re-enable
+		 * preemption here before returning if we've modified the ip.
 		 */
-		if (__this_cpu_read(bpf_kprobe_override)) {
-			__this_cpu_write(bpf_kprobe_override, 0);
+		if (orig_ip != instruction_pointer(regs)) {
 			reset_current_kprobe();
+			preempt_enable_no_resched();

This is great idea.

Acked-by on v2 3/4
#18 lore raw

Re: [RFC PATCH bpf-next v2 3/4] error-injection: Separate error-injection from kprobe

In reply to Masami Hiramatsu
Quoted 15 lines · Masami Hiramatsu

Since error-injection framework is not limited to be used by kprobes, nor bpf. Other kernel subsystems can use it freely for checking safeness of error-injection, e.g. livepatch, ftrace etc.
So this separate error-injection framework from kprobes.

Some differences has been made:

- "kprobe" word is removed from any APIs/structures.
- BPF_ALLOW_ERROR_INJECTION() is renamed to ALLOW_ERROR_INJECTION() since it is not limited for BPF too.
- CONFIG_FUNCTION_ERROR_INJECTION is the config item of this feature. It is automatically enabled if the arch supports error injection feature for kprobe or ftrace etc.

Quoted 6 lines

---
Changes in v2:
- Fix the override function name to override_function_with_return()
- Show only function name in the list, user don't have to care about it's size, since function override only happens at the entry.

looks like nice cleanup.

#20 lore raw

Re: [RFC PATCH bpf-next v2 4/4] error-injection: Support fault injection framework

In reply to Masami Hiramatsu
Quoted 18 lines · Masami Hiramatsu

Support in-kernel fault-injection framework via debugfs. This allows you to inject a conditional error to specified function using debugfs interfaces.

---
 Documentation/fault-injection/fault-injection.txt |    5 +
 kernel/Makefile                                   |    1 
 kernel/fail_function.c                            |  169 +++++++++++++++++++++
 lib/Kconfig.debug                                 |   10 +
 4 files changed, 185 insertions(+)
 create mode 100644 kernel/fail_function.c
Documentation/fault-injection/fault-injection.txt +0 -0
index 918972babcd8..6243a588dd71 100644

Documentation/fault-injection/fault-injection.txt o fail_mmc_request · line 36

Quoted 7 lines
Documentation/fault-injection/fault-injection.txt +4 -0
@@ -30,6 +30,11 @@ o fail_mmc_request
   injects MMC data errors on devices permitted by setting
   debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
 
+o fail_function
+
+  injects error return on specific functions by setting debugfs entries
+  under /sys/kernel/debug/fail_function. No boot option supported.

I like it.
Could you document it a bit better?
In particular retval is configurable, but without an example no one will be able to figure out how to use it.

I think you can drop RFC tag from the next version of these patches. Thanks!

author maintainer
#3 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

In reply to Alexei Starovoitov
Quoted 61 lines · On Tue, 26 Dec 2017 17:57:32 -0800 Alexei Starovoitov…
Quoted 60 lines · Masami Hiramatsu

Check whether error injectable event is on function entry or not. Currently it checks the event is ftrace-based kprobes or not, but that is wrong. It should check if the event is on the entry of target function. Since error injection will override a function to just return with modified return value, that operation must be done before the target function starts making stackframe.

As a side effect, bpf error injection is no need to depend on function-tracer. It can work with sw-breakpoint based kprobe events too.

---
 kernel/trace/Kconfig        |    2 --
 kernel/trace/bpf_trace.c    |    6 +++---
 kernel/trace/trace_kprobe.c |    8 +++++---
 kernel/trace/trace_probe.h  |   12 ++++++------
 4 files changed, 14 insertions(+), 14 deletions(-)
kernel/trace/Kconfig +0 -2
index ae3a2d519e50..6400e1bf97c5 100644
@@ -533,9 +533,7 @@ config FUNCTION_PROFILER
 config BPF_KPROBE_OVERRIDE
 	bool "Enable BPF programs to override a kprobed function"
 	depends on BPF_EVENTS
-	depends on KPROBES_ON_FTRACE
 	depends on HAVE_KPROBE_OVERRIDE
-	depends on DYNAMIC_FTRACE_WITH_REGS
 	default n
 	help
 	 Allows BPF to override the execution of a probed function and
kernel/trace/bpf_trace.c +3 -3
index f6d2327ecb59..d663660f8392 100644
@@ -800,11 +800,11 @@ int perf_event_attach_bpf_prog(struct perf_event *event,
 	int ret = -EEXIST;
 
 	/*
-	 * Kprobe override only works for ftrace based kprobes, and only if they
-	 * are on the opt-in list.
+	 * Kprobe override only works if they are on the function entry,
+	 * and only if they are on the opt-in list.
 	 */
 	if (prog->kprobe_override &&
-	    (!trace_kprobe_ftrace(event->tp_event) ||
+	    (!trace_kprobe_on_func_entry(event->tp_event) ||
 	     !trace_kprobe_error_injectable(event->tp_event)))
 		return -EINVAL;
 
kernel/trace/trace_kprobe.c +0 -0
index 91f4b57dab82..265e3e27e8dc 100644
@@ -88,13 +88,15 @@ static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
 	return nhit;
 }
 
Quoted 10 lines

kernel/trace/trace_kprobe.c static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk) · line 96

Quoted 8 lines
kernel/trace/trace_kprobe.c +4 -2
-int trace_kprobe_ftrace(struct trace_event_call *call)
+bool trace_kprobe_on_func_entry(struct trace_event_call *call)
 {
 	struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
-	return kprobe_ftrace(&tk->rp.kp);
+
+	return kprobe_on_func_entry(tk->rp.kp.addr, tk->rp.kp.symbol_name,
+				    tk->rp.kp.offset);

That would be nice, but did you test this?

Yes, because the jprobe, which was only official user of modifying execution path using kprobe, did same way to check. (and kretprobe also does it)

Quoted 2 lines

My understanding that kprobe will restore all regs and here we need to override return ip _and_ value.

yes, no problem. kprobe restore all regs from pt_regs, including regs->ip.

Quoted 2 lines

Could you add a patch with the test the way Josef did or describe the steps to test this new mode?

Would you mean below patch? If so, it should work without any change.

[PATCH v10 4/5] samples/bpf: add a test for bpf_override_return

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#21 lore raw

Re: [RFC PATCH bpf-next v2 4/4] error-injection: Support fault injection framework

In reply to Alexei Starovoitov
Quoted 22 lines · On Tue, 26 Dec 2017 18:12:56 -0800 Alexei Starovoitov…
Quoted 21 lines · Masami Hiramatsu

Support in-kernel fault-injection framework via debugfs. This allows you to inject a conditional error to specified function using debugfs interfaces.

---
 Documentation/fault-injection/fault-injection.txt |    5 +
 kernel/Makefile                                   |    1 
 kernel/fail_function.c                            |  169 +++++++++++++++++++++
 lib/Kconfig.debug                                 |   10 +
 4 files changed, 185 insertions(+)
 create mode 100644 kernel/fail_function.c
Documentation/fault-injection/fault-injection.txt +0 -0
index 918972babcd8..6243a588dd71 100644
@@ -30,6 +30,11 @@ o fail_mmc_request
   injects MMC data errors on devices permitted by setting
   debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
 
Quoted 7 lines

Documentation/fault-injection/fault-injection.txt o fail_mmc_request · line 36

Quoted 4 lines
Documentation/fault-injection/fault-injection.txt +4 -0
+o fail_function
+
+  injects error return on specific functions by setting debugfs entries
+  under /sys/kernel/debug/fail_function. No boot option supported.

I like it.
Could you document it a bit better?

Yes, I will do in next series.

Quoted 2 lines

In particular retval is configurable, but without an example no one will be able to figure out how to use it.

Ah, right. BTW, as I pointed in the covermail, should we store the expected error value range into the injectable list? e.g.

ALLOW_ERROR_INJECTION(open_ctree, -1, -MAX_ERRNO)

And provide APIs to check/get it.

const struct error_range *ei_get_error_range(unsigned long addr);

Quoted 3 lines

I think you can drop RFC tag from the next version of these patches. Thanks!

Thank you, I'll fix some errors came from configurations, and resend it.

Thanks!

Signature · 1 line
Masami Hiramatsu <[email protected]>
#4 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

In reply to Masami Hiramatsu
Quoted 81 lines · Masami Hiramatsu
Quoted 61 lines · On Tue, 26 Dec 2017 17:57:32 -0800 Alexei Starovoitov…
Quoted 60 lines · Masami Hiramatsu

Check whether error injectable event is on function entry or not. Currently it checks the event is ftrace-based kprobes or not, but that is wrong. It should check if the event is on the entry of target function. Since error injection will override a function to just return with modified return value, that operation must be done before the target function starts making stackframe.

As a side effect, bpf error injection is no need to depend on function-tracer. It can work with sw-breakpoint based kprobe events too.

---
 kernel/trace/Kconfig        |    2 --
 kernel/trace/bpf_trace.c    |    6 +++---
 kernel/trace/trace_kprobe.c |    8 +++++---
 kernel/trace/trace_probe.h  |   12 ++++++------
 4 files changed, 14 insertions(+), 14 deletions(-)
kernel/trace/Kconfig +0 -2
index ae3a2d519e50..6400e1bf97c5 100644
@@ -533,9 +533,7 @@ config FUNCTION_PROFILER
 config BPF_KPROBE_OVERRIDE
 	bool "Enable BPF programs to override a kprobed function"
 	depends on BPF_EVENTS
-	depends on KPROBES_ON_FTRACE
 	depends on HAVE_KPROBE_OVERRIDE
-	depends on DYNAMIC_FTRACE_WITH_REGS
 	default n
 	help
 	 Allows BPF to override the execution of a probed function and
kernel/trace/bpf_trace.c +3 -3
index f6d2327ecb59..d663660f8392 100644
@@ -800,11 +800,11 @@ int perf_event_attach_bpf_prog(struct perf_event *event,
 	int ret = -EEXIST;
 
 	/*
-	 * Kprobe override only works for ftrace based kprobes, and only if they
-	 * are on the opt-in list.
+	 * Kprobe override only works if they are on the function entry,
+	 * and only if they are on the opt-in list.
 	 */
 	if (prog->kprobe_override &&
-	    (!trace_kprobe_ftrace(event->tp_event) ||
+	    (!trace_kprobe_on_func_entry(event->tp_event) ||
 	     !trace_kprobe_error_injectable(event->tp_event)))
 		return -EINVAL;
 
kernel/trace/trace_kprobe.c +0 -0
index 91f4b57dab82..265e3e27e8dc 100644
@@ -88,13 +88,15 @@ static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
 	return nhit;
 }
 
Quoted 10 lines

kernel/trace/trace_kprobe.c static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk) · line 96

Quoted 8 lines
kernel/trace/trace_kprobe.c +4 -2
-int trace_kprobe_ftrace(struct trace_event_call *call)
+bool trace_kprobe_on_func_entry(struct trace_event_call *call)
 {
 	struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
-	return kprobe_ftrace(&tk->rp.kp);
+
+	return kprobe_on_func_entry(tk->rp.kp.addr, tk->rp.kp.symbol_name,
+				    tk->rp.kp.offset);

That would be nice, but did you test this?

Yes, because the jprobe, which was only official user of modifying execution path using kprobe, did same way to check. (and kretprobe also does it)

Quoted 2 lines

My understanding that kprobe will restore all regs and here we need to override return ip _and_ value.

Quoted 8 lines

yes, no problem. kprobe restore all regs from pt_regs, including regs->ip.

Quoted 2 lines

Could you add a patch with the test the way Josef did or describe the steps to test this new mode?

Would you mean below patch? If so, it should work without any change.

[PATCH v10 4/5] samples/bpf: add a test for bpf_override_return

yeah. I expect bpf_override_return test to work as-is. I'm asking for the test for new functionality added by this patch. In particular kprobe on func entry without ftrace.
How did you test it?
and how I can repeat the test?
I'm still not sure that it works correctly.

#22 lore raw

Re: [RFC PATCH bpf-next v2 4/4] error-injection: Support fault injection framework

In reply to Masami Hiramatsu
Quoted 38 lines · Masami Hiramatsu
Quoted 22 lines · On Tue, 26 Dec 2017 18:12:56 -0800 Alexei Starovoitov…
Quoted 21 lines · Masami Hiramatsu

Support in-kernel fault-injection framework via debugfs. This allows you to inject a conditional error to specified function using debugfs interfaces.

---
 Documentation/fault-injection/fault-injection.txt |    5 +
 kernel/Makefile                                   |    1
 kernel/fail_function.c                            |  169 +++++++++++++++++++++
 lib/Kconfig.debug                                 |   10 +
 4 files changed, 185 insertions(+)
 create mode 100644 kernel/fail_function.c
Documentation/fault-injection/fault-injection.txt +0 -0
index 918972babcd8..6243a588dd71 100644
@@ -30,6 +30,11 @@ o fail_mmc_request
   injects MMC data errors on devices permitted by setting
   debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
 
Quoted 7 lines

Documentation/fault-injection/fault-injection.txt o fail_mmc_request · line 36

Quoted 4 lines
Documentation/fault-injection/fault-injection.txt +4 -0
+o fail_function
+
+  injects error return on specific functions by setting debugfs entries
+  under /sys/kernel/debug/fail_function. No boot option supported.

I like it.
Could you document it a bit better?

Yes, I will do in next series.

Quoted 2 lines

In particular retval is configurable, but without an example no one will be able to figure out how to use it.

Quoted 6 lines

Ah, right. BTW, as I pointed in the covermail, should we store the expected error value range into the injectable list? e.g.

ALLOW_ERROR_INJECTION(open_ctree, -1, -MAX_ERRNO)

And provide APIs to check/get it.

I'm afraid such check would be too costly.
Right now we have only two functions marked but I expect hundreds more will be added in the near future as soon as developers realize the potential of such error injection.
All of ALLOW_ERROR_INJECTION marks add 8 byte overhead each to .data. Multiple by 1k and we have 8k of data spent on marks.
If we add max/min range marks that doubles it for very little use. I think marking function only is enough.

author maintainer
#23 lore raw

Re: [RFC PATCH bpf-next v2 4/4] error-injection: Support fault injection framework

Quoted 46 lines · On Wed, 27 Dec 2017 14:49:46 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 38 lines · Masami Hiramatsu
Quoted 22 lines · On Tue, 26 Dec 2017 18:12:56 -0800 Alexei Starovoitov…
Quoted 21 lines · Masami Hiramatsu

Support in-kernel fault-injection framework via debugfs. This allows you to inject a conditional error to specified function using debugfs interfaces.

---
 Documentation/fault-injection/fault-injection.txt |    5 +
 kernel/Makefile                                   |    1
 kernel/fail_function.c                            |  169 +++++++++++++++++++++
 lib/Kconfig.debug                                 |   10 +
 4 files changed, 185 insertions(+)
 create mode 100644 kernel/fail_function.c
Documentation/fault-injection/fault-injection.txt +0 -0
index 918972babcd8..6243a588dd71 100644
@@ -30,6 +30,11 @@ o fail_mmc_request
   injects MMC data errors on devices permitted by setting
   debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
 
Quoted 7 lines

Documentation/fault-injection/fault-injection.txt o fail_mmc_request · line 36

Quoted 4 lines
Documentation/fault-injection/fault-injection.txt +4 -0
+o fail_function
+
+  injects error return on specific functions by setting debugfs entries
+  under /sys/kernel/debug/fail_function. No boot option supported.

I like it.
Could you document it a bit better?

Yes, I will do in next series.

Quoted 2 lines

In particular retval is configurable, but without an example no one will be able to figure out how to use it.

Quoted 6 lines

Ah, right. BTW, as I pointed in the covermail, should we store the expected error value range into the injectable list? e.g.

ALLOW_ERROR_INJECTION(open_ctree, -1, -MAX_ERRNO)

And provide APIs to check/get it.

Quoted 8 lines

I'm afraid such check would be too costly.
Right now we have only two functions marked but I expect hundreds more will be added in the near future as soon as developers realize the potential of such error injection.
All of ALLOW_ERROR_INJECTION marks add 8 byte overhead each to .data. Multiple by 1k and we have 8k of data spent on marks.
If we add max/min range marks that doubles it for very little use. I think marking function only is enough.

Sorry, I don't think so.
Even if it takes 16 bytes more for each points, I don't think it is any overhead for machines in these days. Even if so, we can provide a kconfig to reduce it.
I mean, we are living in GB-order memory are, and it will be bigger in the future. Why we have to worry about hundreds of 16bytes memory pieces? It will take a few KB, and even if we mark thousands of functions, it never reaches 1MB, in GB memory pool. :)

Of course, for many small-footprint embedded devices (like having less than 128MB memory), this feature can be a overhead. But they can cut off the table by kconfig.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#5 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

In reply to Alexei Starovoitov
Quoted 87 lines · On Wed, 27 Dec 2017 14:46:24 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 86 lines · Masami Hiramatsu
Quoted 61 lines · On Tue, 26 Dec 2017 17:57:32 -0800 Alexei Starovoitov…
Quoted 60 lines · Masami Hiramatsu

Check whether error injectable event is on function entry or not. Currently it checks the event is ftrace-based kprobes or not, but that is wrong. It should check if the event is on the entry of target function. Since error injection will override a function to just return with modified return value, that operation must be done before the target function starts making stackframe.

As a side effect, bpf error injection is no need to depend on function-tracer. It can work with sw-breakpoint based kprobe events too.

---
 kernel/trace/Kconfig        |    2 --
 kernel/trace/bpf_trace.c    |    6 +++---
 kernel/trace/trace_kprobe.c |    8 +++++---
 kernel/trace/trace_probe.h  |   12 ++++++------
 4 files changed, 14 insertions(+), 14 deletions(-)
kernel/trace/Kconfig +0 -2
index ae3a2d519e50..6400e1bf97c5 100644
@@ -533,9 +533,7 @@ config FUNCTION_PROFILER
 config BPF_KPROBE_OVERRIDE
 	bool "Enable BPF programs to override a kprobed function"
 	depends on BPF_EVENTS
-	depends on KPROBES_ON_FTRACE
 	depends on HAVE_KPROBE_OVERRIDE
-	depends on DYNAMIC_FTRACE_WITH_REGS
 	default n
 	help
 	 Allows BPF to override the execution of a probed function and
kernel/trace/bpf_trace.c +3 -3
index f6d2327ecb59..d663660f8392 100644
@@ -800,11 +800,11 @@ int perf_event_attach_bpf_prog(struct perf_event *event,
 	int ret = -EEXIST;
 
 	/*
-	 * Kprobe override only works for ftrace based kprobes, and only if they
-	 * are on the opt-in list.
+	 * Kprobe override only works if they are on the function entry,
+	 * and only if they are on the opt-in list.
 	 */
 	if (prog->kprobe_override &&
-	    (!trace_kprobe_ftrace(event->tp_event) ||
+	    (!trace_kprobe_on_func_entry(event->tp_event) ||
 	     !trace_kprobe_error_injectable(event->tp_event)))
 		return -EINVAL;
 
kernel/trace/trace_kprobe.c +0 -0
index 91f4b57dab82..265e3e27e8dc 100644
@@ -88,13 +88,15 @@ static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
 	return nhit;
 }
 
Quoted 10 lines

kernel/trace/trace_kprobe.c static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk) · line 96

Quoted 8 lines
kernel/trace/trace_kprobe.c +4 -2
-int trace_kprobe_ftrace(struct trace_event_call *call)
+bool trace_kprobe_on_func_entry(struct trace_event_call *call)
 {
 	struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
-	return kprobe_ftrace(&tk->rp.kp);
+
+	return kprobe_on_func_entry(tk->rp.kp.addr, tk->rp.kp.symbol_name,
+				    tk->rp.kp.offset);

That would be nice, but did you test this?

Yes, because the jprobe, which was only official user of modifying execution path using kprobe, did same way to check. (and kretprobe also does it)

Quoted 2 lines

My understanding that kprobe will restore all regs and here we need to override return ip _and_ value.

yes, no problem. kprobe restore all regs from pt_regs, including regs->ip.

Quoted 2 lines

Could you add a patch with the test the way Josef did or describe the steps to test this new mode?

Quoted 8 lines
Quoted 3 lines

Would you mean below patch? If so, it should work without any change.

[PATCH v10 4/5] samples/bpf: add a test for bpf_override_return

yeah. I expect bpf_override_return test to work as-is. I'm asking for the test for new functionality added by this patch. In particular kprobe on func entry without ftrace.
How did you test it?

This function is used in kretprobe and jprobe. Jprobe was the user of "modifying instruction pointer to another function" in kprobes. If it doesn't work, jprobe also doesn't work, this means you can not modify IP by kprobes anymore.
Anyway, until linux-4.13, that was well tested by kprobe smoke test.

Quoted 2 lines

and how I can repeat the test?
I'm still not sure that it works correctly.

That works correctly because it checks given address is on the entry point (the 1st instruction) of a function, using kallsyms.

The reason why I made another flag for ftrace was, there are 2 modes for ftrace dynamic instrumentation, fentry and mcount. With new fentry mode, ftrace will be put on the first instruction of the function, so it will work as you expected.
With traditional gcc mcount, ftrace will be called after making call frame for _mcount(). This means if you modify ip, it will not work or cause a trouble because _mcount call frame is still on the stack.

So, current ftrace-based checker doesn't work, it depends on the case. Of course, in most case, kernel will be build in new gcc which supports fentry, but there is no guarantee.

Please follow what jprobe did if you want to change invoked function using kprobes. That has been well reviewed and discussed in more than 10 years.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#6 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

Quoted 119 lines · Masami Hiramatsu
Quoted 87 lines · On Wed, 27 Dec 2017 14:46:24 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 86 lines · Masami Hiramatsu
Quoted 61 lines · On Tue, 26 Dec 2017 17:57:32 -0800 Alexei Starovoitov…
Quoted 60 lines · Masami Hiramatsu

Check whether error injectable event is on function entry or not. Currently it checks the event is ftrace-based kprobes or not, but that is wrong. It should check if the event is on the entry of target function. Since error injection will override a function to just return with modified return value, that operation must be done before the target function starts making stackframe.

As a side effect, bpf error injection is no need to depend on function-tracer. It can work with sw-breakpoint based kprobe events too.

---
 kernel/trace/Kconfig        |    2 --
 kernel/trace/bpf_trace.c    |    6 +++---
 kernel/trace/trace_kprobe.c |    8 +++++---
 kernel/trace/trace_probe.h  |   12 ++++++------
 4 files changed, 14 insertions(+), 14 deletions(-)
kernel/trace/Kconfig +0 -2
index ae3a2d519e50..6400e1bf97c5 100644
@@ -533,9 +533,7 @@ config FUNCTION_PROFILER
 config BPF_KPROBE_OVERRIDE
 	bool "Enable BPF programs to override a kprobed function"
 	depends on BPF_EVENTS
-	depends on KPROBES_ON_FTRACE
 	depends on HAVE_KPROBE_OVERRIDE
-	depends on DYNAMIC_FTRACE_WITH_REGS
 	default n
 	help
 	 Allows BPF to override the execution of a probed function and
kernel/trace/bpf_trace.c +3 -3
index f6d2327ecb59..d663660f8392 100644
@@ -800,11 +800,11 @@ int perf_event_attach_bpf_prog(struct perf_event *event,
 	int ret = -EEXIST;
 
 	/*
-	 * Kprobe override only works for ftrace based kprobes, and only if they
-	 * are on the opt-in list.
+	 * Kprobe override only works if they are on the function entry,
+	 * and only if they are on the opt-in list.
 	 */
 	if (prog->kprobe_override &&
-	    (!trace_kprobe_ftrace(event->tp_event) ||
+	    (!trace_kprobe_on_func_entry(event->tp_event) ||
 	     !trace_kprobe_error_injectable(event->tp_event)))
 		return -EINVAL;
 
kernel/trace/trace_kprobe.c +0 -0
index 91f4b57dab82..265e3e27e8dc 100644
@@ -88,13 +88,15 @@ static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
 	return nhit;
 }
 
Quoted 10 lines

kernel/trace/trace_kprobe.c static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk) · line 96

Quoted 8 lines
kernel/trace/trace_kprobe.c +4 -2
-int trace_kprobe_ftrace(struct trace_event_call *call)
+bool trace_kprobe_on_func_entry(struct trace_event_call *call)
 {
 	struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
-	return kprobe_ftrace(&tk->rp.kp);
+
+	return kprobe_on_func_entry(tk->rp.kp.addr, tk->rp.kp.symbol_name,
+				    tk->rp.kp.offset);

That would be nice, but did you test this?

Yes, because the jprobe, which was only official user of modifying execution path using kprobe, did same way to check. (and kretprobe also does it)

Quoted 2 lines

My understanding that kprobe will restore all regs and here we need to override return ip _and_ value.

yes, no problem. kprobe restore all regs from pt_regs, including regs->ip.

Quoted 2 lines

Could you add a patch with the test the way Josef did or describe the steps to test this new mode?

Quoted 8 lines
Quoted 3 lines

Would you mean below patch? If so, it should work without any change.

[PATCH v10 4/5] samples/bpf: add a test for bpf_override_return

yeah. I expect bpf_override_return test to work as-is. I'm asking for the test for new functionality added by this patch. In particular kprobe on func entry without ftrace.
How did you test it?

This function is used in kretprobe and jprobe. Jprobe was the user of "modifying instruction pointer to another function" in kprobes. If it doesn't work, jprobe also doesn't work, this means you can not modify IP by kprobes anymore.
Anyway, until linux-4.13, that was well tested by kprobe smoke test.

Quoted 2 lines

and how I can repeat the test?
I'm still not sure that it works correctly.

That works correctly because it checks given address is on the entry point (the 1st instruction) of a function, using kallsyms.

The reason why I made another flag for ftrace was, there are 2 modes for ftrace dynamic instrumentation, fentry and mcount. With new fentry mode, ftrace will be put on the first instruction of the function, so it will work as you expected.
With traditional gcc mcount, ftrace will be called after making call frame for _mcount(). This means if you modify ip, it will not work or cause a trouble because _mcount call frame is still on the stack.

Quoted 3 lines

So, current ftrace-based checker doesn't work, it depends on the case. Of course, in most case, kernel will be build in new gcc which supports fentry, but there is no guarantee.

I don't think that's the case. My reading of current trace_kprobe_ftrace() -> arch_check_ftrace_location() is that it will not be true for old mcount case.

As far as the rest of your arguments it very much puzzles me that you claim that this patch suppose to work based on historical reasoning whereas you did NOT test it.

#24 lore raw

Re: [RFC PATCH bpf-next v2 4/4] error-injection: Support fault injection framework

In reply to Masami Hiramatsu
Quoted 67 lines · Masami Hiramatsu
Quoted 46 lines · On Wed, 27 Dec 2017 14:49:46 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 38 lines · Masami Hiramatsu
Quoted 22 lines · On Tue, 26 Dec 2017 18:12:56 -0800 Alexei Starovoitov…
Quoted 21 lines · Masami Hiramatsu

Support in-kernel fault-injection framework via debugfs. This allows you to inject a conditional error to specified function using debugfs interfaces.

---
 Documentation/fault-injection/fault-injection.txt |    5 +
 kernel/Makefile                                   |    1
 kernel/fail_function.c                            |  169 +++++++++++++++++++++
 lib/Kconfig.debug                                 |   10 +
 4 files changed, 185 insertions(+)
 create mode 100644 kernel/fail_function.c
Documentation/fault-injection/fault-injection.txt +0 -0
index 918972babcd8..6243a588dd71 100644
@@ -30,6 +30,11 @@ o fail_mmc_request
   injects MMC data errors on devices permitted by setting
   debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
 
Quoted 7 lines

Documentation/fault-injection/fault-injection.txt o fail_mmc_request · line 36

Quoted 4 lines
Documentation/fault-injection/fault-injection.txt +4 -0
+o fail_function
+
+  injects error return on specific functions by setting debugfs entries
+  under /sys/kernel/debug/fail_function. No boot option supported.

I like it.
Could you document it a bit better?

Yes, I will do in next series.

Quoted 2 lines

In particular retval is configurable, but without an example no one will be able to figure out how to use it.

Quoted 6 lines

Ah, right. BTW, as I pointed in the covermail, should we store the expected error value range into the injectable list? e.g.

ALLOW_ERROR_INJECTION(open_ctree, -1, -MAX_ERRNO)

And provide APIs to check/get it.

Quoted 8 lines

I'm afraid such check would be too costly.
Right now we have only two functions marked but I expect hundreds more will be added in the near future as soon as developers realize the potential of such error injection.
All of ALLOW_ERROR_INJECTION marks add 8 byte overhead each to .data. Multiple by 1k and we have 8k of data spent on marks.
If we add max/min range marks that doubles it for very little use. I think marking function only is enough.

Sorry, I don't think so.
Even if it takes 16 bytes more for each points, I don't think it is any overhead for machines in these days. Even if so, we can provide a kconfig to reduce it.
I mean, we are living in GB-order memory are, and it will be bigger in the future. Why we have to worry about hundreds of 16bytes memory pieces? It will take a few KB, and even if we mark thousands of functions, it never reaches 1MB, in GB memory pool. :)

Quoted 3 lines

Of course, for many small-footprint embedded devices (like having less than 128MB memory), this feature can be a overhead. But they can cut off the table by kconfig.

I still disagree on wasting 16-byte * num_of_funcs of .data here. The trade-off of usability vs memory just not worth it. Sorry. Please focus on testing your changes instead.

maintainer
#7 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

In reply to Alexei Starovoitov
Quoted 3 lines · On Wed, 27 Dec 2017 19:45:42 -0800 Alexei Starovoitov <[email protected]> wrote:

I don't think that's the case. My reading of current trace_kprobe_ftrace() -> arch_check_ftrace_location() is that it will not be true for old mcount case.

In the old mcount case, you can't use ftrace to return without calling the function. That is, no modification of the return ip, unless you created a trampoline that could handle arbitrary stack frames, and remove them from the stack before returning back to the function.

Quoted 4 lines

As far as the rest of your arguments it very much puzzles me that you claim that this patch suppose to work based on historical reasoning whereas you did NOT test it.

I believe that Masami is saying that the modification of the IP from kprobes has been very well tested. But I'm guessing that you still want a test case for using kprobes in this particular instance. It's not the implementation of modifying the IP that you are worried about, but the implementation of BPF using it in this case. Right?

-- Steve

#8 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

Quoted 11 lines · Steven Rostedt
Quoted 3 lines · On Wed, 27 Dec 2017 19:45:42 -0800 Alexei Starovoitov <[email protected]> wrote:

I don't think that's the case. My reading of current trace_kprobe_ftrace() -> arch_check_ftrace_location() is that it will not be true for old mcount case.

In the old mcount case, you can't use ftrace to return without calling the function. That is, no modification of the return ip, unless you created a trampoline that could handle arbitrary stack frames, and remove them from the stack before returning back to the function.

correct. I was saying that trace_kprobe_ftrace() won't let us do bpf_override_return with old mcount.

Quoted 10 lines
Quoted 4 lines

As far as the rest of your arguments it very much puzzles me that you claim that this patch suppose to work based on historical reasoning whereas you did NOT test it.

I believe that Masami is saying that the modification of the IP from kprobes has been very well tested. But I'm guessing that you still want a test case for using kprobes in this particular instance. It's not the implementation of modifying the IP that you are worried about, but the implementation of BPF using it in this case. Right?

exactly. No doubt that old code works.
But it doesn't mean that bpf_override_return() will continue to work in kprobes that are not ftrace based.
I suspect Josef's existing test case will cover this situation. Probably only special .config is needed to disable ftrace, so "kprobe on entry but not ftrace" check will kick in. But I didn't get an impression that this situation was tested. Instead I see only logical reasoning that it's _supposed_ to work. That's not enough.

author maintainer
#25 lore raw

Re: [RFC PATCH bpf-next v2 4/4] error-injection: Support fault injection framework

In reply to Alexei Starovoitov
Quoted 68 lines · On Wed, 27 Dec 2017 19:49:28 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 67 lines · Masami Hiramatsu
Quoted 46 lines · On Wed, 27 Dec 2017 14:49:46 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 38 lines · Masami Hiramatsu
Quoted 22 lines · On Tue, 26 Dec 2017 18:12:56 -0800 Alexei Starovoitov…
Quoted 21 lines · Masami Hiramatsu

Support in-kernel fault-injection framework via debugfs. This allows you to inject a conditional error to specified function using debugfs interfaces.

---
 Documentation/fault-injection/fault-injection.txt |    5 +
 kernel/Makefile                                   |    1
 kernel/fail_function.c                            |  169 +++++++++++++++++++++
 lib/Kconfig.debug                                 |   10 +
 4 files changed, 185 insertions(+)
 create mode 100644 kernel/fail_function.c
Documentation/fault-injection/fault-injection.txt +0 -0
index 918972babcd8..6243a588dd71 100644
@@ -30,6 +30,11 @@ o fail_mmc_request
   injects MMC data errors on devices permitted by setting
   debugfs entries under /sys/kernel/debug/mmc0/fail_mmc_request
 
Quoted 7 lines

Documentation/fault-injection/fault-injection.txt o fail_mmc_request · line 36

Quoted 4 lines
Documentation/fault-injection/fault-injection.txt +4 -0
+o fail_function
+
+  injects error return on specific functions by setting debugfs entries
+  under /sys/kernel/debug/fail_function. No boot option supported.

I like it.
Could you document it a bit better?

Yes, I will do in next series.

Quoted 2 lines

In particular retval is configurable, but without an example no one will be able to figure out how to use it.

Quoted 6 lines

Ah, right. BTW, as I pointed in the covermail, should we store the expected error value range into the injectable list? e.g.

ALLOW_ERROR_INJECTION(open_ctree, -1, -MAX_ERRNO)

And provide APIs to check/get it.

Quoted 8 lines

I'm afraid such check would be too costly.
Right now we have only two functions marked but I expect hundreds more will be added in the near future as soon as developers realize the potential of such error injection.
All of ALLOW_ERROR_INJECTION marks add 8 byte overhead each to .data. Multiple by 1k and we have 8k of data spent on marks.
If we add max/min range marks that doubles it for very little use. I think marking function only is enough.

Sorry, I don't think so.
Even if it takes 16 bytes more for each points, I don't think it is any overhead for machines in these days. Even if so, we can provide a kconfig to reduce it.
I mean, we are living in GB-order memory are, and it will be bigger in the future. Why we have to worry about hundreds of 16bytes memory pieces? It will take a few KB, and even if we mark thousands of functions, it never reaches 1MB, in GB memory pool. :)

Quoted 7 lines
Quoted 3 lines

Of course, for many small-footprint embedded devices (like having less than 128MB memory), this feature can be a overhead. But they can cut off the table by kconfig.

I still disagree on wasting 16-byte * num_of_funcs of .data here. The trade-off of usability vs memory just not worth it. Sorry. Please focus on testing your changes instead.

Then what happen if the user set invalid retval to those functions? even if we limit the injectable functions, it can cause a problem,

for example,

obj = func_return_object();

 if (!obj) {
    handling_error...;

}
obj->field = x;

In this case, obviously func_return_object() must return NULL if there is an error, not -ENOMEM. But without the correct retval information, how would you check the BPF code doesn't cause a trouble?
Currently it seems you are expecting only the functions which return error code.

ret = func_return_state();

 if (ret < 0) {
    handling_error...;

}

But how we can distinguish those?

If we have the error range for each function, we can ensure what is *correct* error code, NULL or errno, or any other error numbers. :)

At least fail_function needs this feature because it can check return value when setting it up.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#14 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

In reply to Alexei Starovoitov
Quoted 124 lines · On Wed, 27 Dec 2017 19:45:42 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 119 lines · Masami Hiramatsu
Quoted 87 lines · On Wed, 27 Dec 2017 14:46:24 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 86 lines · Masami Hiramatsu
Quoted 61 lines · On Tue, 26 Dec 2017 17:57:32 -0800 Alexei Starovoitov…
Quoted 60 lines · Masami Hiramatsu

Check whether error injectable event is on function entry or not. Currently it checks the event is ftrace-based kprobes or not, but that is wrong. It should check if the event is on the entry of target function. Since error injection will override a function to just return with modified return value, that operation must be done before the target function starts making stackframe.

As a side effect, bpf error injection is no need to depend on function-tracer. It can work with sw-breakpoint based kprobe events too.

---
 kernel/trace/Kconfig        |    2 --
 kernel/trace/bpf_trace.c    |    6 +++---
 kernel/trace/trace_kprobe.c |    8 +++++---
 kernel/trace/trace_probe.h  |   12 ++++++------
 4 files changed, 14 insertions(+), 14 deletions(-)
kernel/trace/Kconfig +0 -2
index ae3a2d519e50..6400e1bf97c5 100644
@@ -533,9 +533,7 @@ config FUNCTION_PROFILER
 config BPF_KPROBE_OVERRIDE
 	bool "Enable BPF programs to override a kprobed function"
 	depends on BPF_EVENTS
-	depends on KPROBES_ON_FTRACE
 	depends on HAVE_KPROBE_OVERRIDE
-	depends on DYNAMIC_FTRACE_WITH_REGS
 	default n
 	help
 	 Allows BPF to override the execution of a probed function and
kernel/trace/bpf_trace.c +3 -3
index f6d2327ecb59..d663660f8392 100644
@@ -800,11 +800,11 @@ int perf_event_attach_bpf_prog(struct perf_event *event,
 	int ret = -EEXIST;
 
 	/*
-	 * Kprobe override only works for ftrace based kprobes, and only if they
-	 * are on the opt-in list.
+	 * Kprobe override only works if they are on the function entry,
+	 * and only if they are on the opt-in list.
 	 */
 	if (prog->kprobe_override &&
-	    (!trace_kprobe_ftrace(event->tp_event) ||
+	    (!trace_kprobe_on_func_entry(event->tp_event) ||
 	     !trace_kprobe_error_injectable(event->tp_event)))
 		return -EINVAL;
 
kernel/trace/trace_kprobe.c +0 -0
index 91f4b57dab82..265e3e27e8dc 100644
@@ -88,13 +88,15 @@ static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk)
 	return nhit;
 }
 
Quoted 10 lines

kernel/trace/trace_kprobe.c static nokprobe_inline unsigned long trace_kprobe_nhit(struct trace_kprobe *tk) · line 96

Quoted 8 lines
kernel/trace/trace_kprobe.c +4 -2
-int trace_kprobe_ftrace(struct trace_event_call *call)
+bool trace_kprobe_on_func_entry(struct trace_event_call *call)
 {
 	struct trace_kprobe *tk = (struct trace_kprobe *)call->data;
-	return kprobe_ftrace(&tk->rp.kp);
+
+	return kprobe_on_func_entry(tk->rp.kp.addr, tk->rp.kp.symbol_name,
+				    tk->rp.kp.offset);

That would be nice, but did you test this?

Yes, because the jprobe, which was only official user of modifying execution path using kprobe, did same way to check. (and kretprobe also does it)

Quoted 2 lines

My understanding that kprobe will restore all regs and here we need to override return ip _and_ value.

yes, no problem. kprobe restore all regs from pt_regs, including regs->ip.

Quoted 2 lines

Could you add a patch with the test the way Josef did or describe the steps to test this new mode?

Quoted 8 lines
Quoted 3 lines

Would you mean below patch? If so, it should work without any change.

[PATCH v10 4/5] samples/bpf: add a test for bpf_override_return

yeah. I expect bpf_override_return test to work as-is. I'm asking for the test for new functionality added by this patch. In particular kprobe on func entry without ftrace.
How did you test it?

This function is used in kretprobe and jprobe. Jprobe was the user of "modifying instruction pointer to another function" in kprobes. If it doesn't work, jprobe also doesn't work, this means you can not modify IP by kprobes anymore.
Anyway, until linux-4.13, that was well tested by kprobe smoke test.

Quoted 2 lines

and how I can repeat the test?
I'm still not sure that it works correctly.

That works correctly because it checks given address is on the entry point (the 1st instruction) of a function, using kallsyms.

The reason why I made another flag for ftrace was, there are 2 modes for ftrace dynamic instrumentation, fentry and mcount. With new fentry mode, ftrace will be put on the first instruction of the function, so it will work as you expected.
With traditional gcc mcount, ftrace will be called after making call frame for _mcount(). This means if you modify ip, it will not work or cause a trouble because _mcount call frame is still on the stack.

Quoted 3 lines

So, current ftrace-based checker doesn't work, it depends on the case. Of course, in most case, kernel will be build in new gcc which supports fentry, but there is no guarantee.

Quoted 7 lines

I don't think that's the case. My reading of current trace_kprobe_ftrace() -> arch_check_ftrace_location() is that it will not be true for old mcount case.

As far as the rest of your arguments it very much puzzles me that you claim that this patch suppose to work based on historical reasoning whereas you did NOT test it.

No, even with this patch, I meant, you can still test it with Josef's testcase, since this is just backend change. No frontend changes.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#9 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

In reply to Alexei Starovoitov
Quoted 8 lines · On Wed, 27 Dec 2017 20:32:07 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 7 lines · Steven Rostedt
Quoted 3 lines · On Wed, 27 Dec 2017 19:45:42 -0800 Alexei Starovoitov <[email protected]> wrote:

I don't think that's the case. My reading of current trace_kprobe_ftrace() -> arch_check_ftrace_location() is that it will not be true for old mcount case.

Quoted 7 lines
Quoted 4 lines

In the old mcount case, you can't use ftrace to return without calling the function. That is, no modification of the return ip, unless you created a trampoline that could handle arbitrary stack frames, and remove them from the stack before returning back to the function.

correct. I was saying that trace_kprobe_ftrace() won't let us do bpf_override_return with old mcount.

No, trace_kprobe_ftrace() just checks the given address will be managed by ftrace. you can see arch_check_ftrace_location() in kernel/kprobes.c.

FYI, CONFIG_KPROBES_ON_FTRACE depends on DYNAMIC_FTRACE_WITH_REGS, and DYNAMIC_FTRACE_WITH_REGS doesn't depend on CC_USING_FENTRY. This means if you compile kernel with old gcc and enable DYNAMIC_FTRACE, kprobes uses ftrace on mcount address which is NOT the entry point of target function.

On the other hand, changing IP feature has been implemented originaly by kprobes with int3 (sw breakpoint). This means you can use kprobes at correct address (the entry address of the function) you can hijack the function, as jprobe did.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

As far as the rest of your arguments it very much puzzles me that you claim that this patch suppose to work based on historical reasoning whereas you did NOT test it.

I believe that Masami is saying that the modification of the IP from kprobes has been very well tested. But I'm guessing that you still want a test case for using kprobes in this particular instance. It's not the implementation of modifying the IP that you are worried about, but the implementation of BPF using it in this case. Right?

Quoted 6 lines

exactly. No doubt that old code works.
But it doesn't mean that bpf_override_return() will continue to work in kprobes that are not ftrace based.
I suspect Josef's existing test case will cover this situation. Probably only special .config is needed to disable ftrace, so "kprobe on entry but not ftrace" check will kick in.

Right. If you need to test it, you can run Josef's test case without CONFIG_DYNAMIC_FTRACE.

Quoted 3 lines

But I didn't get an impression that this situation was tested. Instead I see only logical reasoning that it's _supposed_ to work. That's not enough.

OK, so would you just ask me to run samples/bpf ?

Thanks,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#10 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

Quoted 19 lines · Masami Hiramatsu
Quoted 8 lines · On Wed, 27 Dec 2017 20:32:07 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 7 lines · Steven Rostedt
Quoted 3 lines · On Wed, 27 Dec 2017 19:45:42 -0800 Alexei Starovoitov <[email protected]> wrote:

I don't think that's the case. My reading of current trace_kprobe_ftrace() -> arch_check_ftrace_location() is that it will not be true for old mcount case.

Quoted 7 lines
Quoted 4 lines

In the old mcount case, you can't use ftrace to return without calling the function. That is, no modification of the return ip, unless you created a trampoline that could handle arbitrary stack frames, and remove them from the stack before returning back to the function.

correct. I was saying that trace_kprobe_ftrace() won't let us do bpf_override_return with old mcount.

Quoted 8 lines

No, trace_kprobe_ftrace() just checks the given address will be managed by ftrace. you can see arch_check_ftrace_location() in kernel/kprobes.c.

FYI, CONFIG_KPROBES_ON_FTRACE depends on DYNAMIC_FTRACE_WITH_REGS, and DYNAMIC_FTRACE_WITH_REGS doesn't depend on CC_USING_FENTRY. This means if you compile kernel with old gcc and enable DYNAMIC_FTRACE, kprobes uses ftrace on mcount address which is NOT the entry point of target function.

ok. fair enough. I think we can gate the feature to !mcount only.

Quoted 22 lines

On the other hand, changing IP feature has been implemented originaly by kprobes with int3 (sw breakpoint). This means you can use kprobes at correct address (the entry address of the function) you can hijack the function, as jprobe did.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

As far as the rest of your arguments it very much puzzles me that you claim that this patch suppose to work based on historical reasoning whereas you did NOT test it.

I believe that Masami is saying that the modification of the IP from kprobes has been very well tested. But I'm guessing that you still want a test case for using kprobes in this particular instance. It's not the implementation of modifying the IP that you are worried about, but the implementation of BPF using it in this case. Right?

Quoted 6 lines

exactly. No doubt that old code works.
But it doesn't mean that bpf_override_return() will continue to work in kprobes that are not ftrace based.
I suspect Josef's existing test case will cover this situation. Probably only special .config is needed to disable ftrace, so "kprobe on entry but not ftrace" check will kick in.

Quoted 2 lines

Right. If you need to test it, you can run Josef's test case without CONFIG_DYNAMIC_FTRACE.

It should be obvious that the person who submits the patch must run the tests.

Quoted 5 lines
Quoted 3 lines

But I didn't get an impression that this situation was tested. Instead I see only logical reasoning that it's _supposed_ to work. That's not enough.

OK, so would you just ask me to run samples/bpf ?

Please run Josef's test in the !ftrace setup.

#26 lore raw

Re: [RFC PATCH bpf-next v2 4/4] error-injection: Support fault injection framework

In reply to Masami Hiramatsu
Quoted 18 lines · Masami Hiramatsu

Then what happen if the user set invalid retval to those functions? even if we limit the injectable functions, it can cause a problem,

for example,

obj = func_return_object();

 if (!obj) {
    handling_error...;

}
obj->field = x;

In this case, obviously func_return_object() must return NULL if there is an error, not -ENOMEM. But without the correct retval information, how would you check the BPF code doesn't cause a trouble?
Currently it seems you are expecting only the functions which return error code.

ret = func_return_state();

Quoted 8 lines
 if (ret < 0) {
    handling_error...;

}

But how we can distinguish those?

If we have the error range for each function, we can ensure what is *correct* error code, NULL or errno, or any other error numbers. :)

messing up return values may cause problems and range check is not going to magically help.
The caller may handle only a certain set of errors or interpret some of them like EBUSY as a signal to retry.
It's plain impossible to make sure that kernel will be functional after error injection has been made.
Like kmalloc() unconditionally returning NULL will be deadly for the kernel, hence this patch 4/4 has very limited practical use. The bpf program need to make intelligent decisions when to return an error and what kind of error to return. Doing blank range check adds a false sense of additional safety. More so it wastes kilobytes of memory to do this check, hence nack.

author maintainer
#27 lore raw

Re: [RFC PATCH bpf-next v2 4/4] error-injection: Support fault injection framework

Quoted 28 lines · On Thu, 28 Dec 2017 17:11:31 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 18 lines · Masami Hiramatsu

Then what happen if the user set invalid retval to those functions? even if we limit the injectable functions, it can cause a problem,

for example,

obj = func_return_object();

 if (!obj) {
    handling_error...;

}
obj->field = x;

In this case, obviously func_return_object() must return NULL if there is an error, not -ENOMEM. But without the correct retval information, how would you check the BPF code doesn't cause a trouble?
Currently it seems you are expecting only the functions which return error code.

ret = func_return_state();

Quoted 8 lines
 if (ret < 0) {
    handling_error...;

}

But how we can distinguish those?

If we have the error range for each function, we can ensure what is *correct* error code, NULL or errno, or any other error numbers. :)

Quoted 6 lines

messing up return values may cause problems and range check is not going to magically help.
The caller may handle only a certain set of errors or interpret some of them like EBUSY as a signal to retry.
It's plain impossible to make sure that kernel will be functional after error injection has been made.

Hmm, if so, why we need this injectable table?
If we can not make sure the safeness of the error injection (of course, yes) why we need to limit the error injection on such limited functions? I think we don't need it anymore. Any function can be injectable, and no need to make sure the safeness.

Thank you,

Quoted 7 lines

Like kmalloc() unconditionally returning NULL will be deadly for the kernel, hence this patch 4/4 has very limited practical use. The bpf program need to make intelligent decisions when to return an error and what kind of error to return. Doing blank range check adds a false sense of additional safety. More so it wastes kilobytes of memory to do this check, hence nack.

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#11 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

In reply to Alexei Starovoitov
Quoted 59 lines · On Thu, 28 Dec 2017 17:03:24 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 19 lines · Masami Hiramatsu
Quoted 8 lines · On Wed, 27 Dec 2017 20:32:07 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 7 lines · Steven Rostedt
Quoted 3 lines · On Wed, 27 Dec 2017 19:45:42 -0800 Alexei Starovoitov <[email protected]> wrote:

I don't think that's the case. My reading of current trace_kprobe_ftrace() -> arch_check_ftrace_location() is that it will not be true for old mcount case.

Quoted 7 lines
Quoted 4 lines

In the old mcount case, you can't use ftrace to return without calling the function. That is, no modification of the return ip, unless you created a trampoline that could handle arbitrary stack frames, and remove them from the stack before returning back to the function.

correct. I was saying that trace_kprobe_ftrace() won't let us do bpf_override_return with old mcount.

Quoted 8 lines

No, trace_kprobe_ftrace() just checks the given address will be managed by ftrace. you can see arch_check_ftrace_location() in kernel/kprobes.c.

FYI, CONFIG_KPROBES_ON_FTRACE depends on DYNAMIC_FTRACE_WITH_REGS, and DYNAMIC_FTRACE_WITH_REGS doesn't depend on CC_USING_FENTRY. This means if you compile kernel with old gcc and enable DYNAMIC_FTRACE, kprobes uses ftrace on mcount address which is NOT the entry point of target function.

ok. fair enough. I think we can gate the feature to !mcount only.

Quoted 22 lines

On the other hand, changing IP feature has been implemented originaly by kprobes with int3 (sw breakpoint). This means you can use kprobes at correct address (the entry address of the function) you can hijack the function, as jprobe did.

Quoted 10 lines
Quoted 9 lines
Quoted 3 lines

As far as the rest of your arguments it very much puzzles me that you claim that this patch suppose to work based on historical reasoning whereas you did NOT test it.

I believe that Masami is saying that the modification of the IP from kprobes has been very well tested. But I'm guessing that you still want a test case for using kprobes in this particular instance. It's not the implementation of modifying the IP that you are worried about, but the implementation of BPF using it in this case. Right?

Quoted 6 lines

exactly. No doubt that old code works.
But it doesn't mean that bpf_override_return() will continue to work in kprobes that are not ftrace based.
I suspect Josef's existing test case will cover this situation. Probably only special .config is needed to disable ftrace, so "kprobe on entry but not ftrace" check will kick in.

Quoted 2 lines

Right. If you need to test it, you can run Josef's test case without CONFIG_DYNAMIC_FTRACE.

It should be obvious that the person who submits the patch must run the tests.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

But I didn't get an impression that this situation was tested. Instead I see only logical reasoning that it's _supposed_ to work. That's not enough.

OK, so would you just ask me to run samples/bpf ?

Please run Josef's test in the !ftrace setup.

Yes, I'll add the result of the test case.

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>
#28 lore raw
In reply to Masami Hiramatsu
Quoted 21 lines · Masami Hiramatsu

Hi Josef and Alexei,

Here are the 2nd version of patches to moving error injection table from kprobes. In this series I did a small fixes and add function-based fault injection.

Here is the previous version:

https://lkml.org/lkml/2017/12/22/554

There are 2 main reasons why I separate it from kprobes.

- kprobes users can modify execution path not only at error-injection whitelist functions but also other functions. I don't like to suggest user that such limitation is from kprobes itself.

- This error injection information is also useful for ftrace (function-hook) and livepatch. It should not be limited by CONFIG_KPROBES.

Quoted 6 lines

So I introduced CONFIG_FUNCTION_ERROR_INJECTION for this feature. Also CONFIG_FAIL_FUNCTION is added, which provides function-based error injection interface via debugfs following fault-injection framework. See [4/4].

Any thoughts?

Sorry Masami, I've been on vacation for the last two weeks. This approach is fine by me, if we want to allow other mechanisms other than bpf to use this functionality then hooray. I'll do a proper review when you post v3, just wanted to let you know I wasn't ignoring you. Thanks,

Josef

#12 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

In reply to Masami Hiramatsu
Quoted 2 lines · Masami Hiramatsu
Quoted 1 line

Please run Josef's test in the !ftrace setup.

Yes, I'll add the result of the test case.

if Josef's test is passing in !ftrace config, please resend your patches.
I think 2 and 3 were nice simplifications. and patch 1 is good too if it's passes the test. Would be great to get them in for this merge window.

Thanks!

author maintainer
#13 lore raw

Re: [RFC PATCH bpf-next v2 1/4] tracing/kprobe: bpf: Check error injectable event is on function entry

Quoted 9 lines · On Sun, 7 Jan 2018 19:01:57 -0800 Alexei Starovoitov <[email protected]> wrote:
Quoted 2 lines · Masami Hiramatsu
Quoted 1 line

Please run Josef's test in the !ftrace setup.

Yes, I'll add the result of the test case.

if Josef's test is passing in !ftrace config, please resend your patches.
I think 2 and 3 were nice simplifications. and patch 1 is good too if it's passes the test. Would be great to get them in for this merge window.

OK, I'll try to do that.

Thanks!

Quoted 3 lines

Thanks!

Signature · 1 line
Masami Hiramatsu <[email protected]>
author maintainer
#29 lore raw
In reply to Josef Bacik
Quoted 27 lines · On Thu, 4 Jan 2018 11:07:16 -0500 Josef Bacik <[email protected]> wrote:
Quoted 26 lines · Masami Hiramatsu

Hi Josef and Alexei,

Here are the 2nd version of patches to moving error injection table from kprobes. In this series I did a small fixes and add function-based fault injection.

Here is the previous version:

https://lkml.org/lkml/2017/12/22/554

There are 2 main reasons why I separate it from kprobes.

- kprobes users can modify execution path not only at error-injection whitelist functions but also other functions. I don't like to suggest user that such limitation is from kprobes itself.

- This error injection information is also useful for ftrace (function-hook) and livepatch. It should not be limited by CONFIG_KPROBES.

So I introduced CONFIG_FUNCTION_ERROR_INJECTION for this feature. Also CONFIG_FAIL_FUNCTION is added, which provides function-based error injection interface via debugfs following fault-injection framework. See [4/4].

Quoted 6 lines
Quoted 1 line

Any thoughts?

Sorry Masami, I've been on vacation for the last two weeks. This approach is fine by me, if we want to allow other mechanisms other than bpf to use this functionality then hooray. I'll do a proper review when you post v3, just wanted to let you know I wasn't ignoring you. Thanks,

Yeah, thank you for the kindful notice ;)

BTW, could you tell me how I can run your test case?

When I tried to build the tests (samples/bpf) I got below error and stopped.

[mhiramat@devbox bpf]$ LANG=C make
make -C ../../ /home/mhiramat/ksrc/linux/samples/bpf/ make[1]: Entering directory '/home/mhiramat/ksrc/linux'

  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CHK     include/generated/bounds.h
  CHK     include/generated/timeconst.h
  CHK     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh

DESCEND objtool
CHK scripts/mod/devicetable-offsets.h
HOSTCC /home/mhiramat/ksrc/linux/samples/bpf/test_lru_dist.o
/home/mhiramat/ksrc/linux/samples/bpf/test_lru_dist.c:39:8: error: redefinition of 'struct list_head'

 struct list_head {
        ^~~~~~~~~
In file included from /home/mhiramat/ksrc/linux/samples/bpf/test_lru_dist.c:9:0:
./tools/include/linux/types.h:69:8: note: originally defined here
 struct list_head {
        ^~~~~~~~~

make[2]: *** [scripts/Makefile.host:107: /home/mhiramat/ksrc/linux/samples/bpf/test_lru_dist.o] Error 1 make[1]: *** [Makefile:1675: /home/mhiramat/ksrc/linux/samples/bpf/] Error 2
make[1]: Leaving directory '/home/mhiramat/ksrc/linux'
make: *** [Makefile:204: all] Error 2

Thank you,

Signature · 1 line
Masami Hiramatsu <[email protected]>