Separate error injection table from kprobes
Subsystems (16)
Masami Hiramatsu
maintainerv3 cover Separate error injection table from kprobes
Hi,
Here are the 3rd version of patches to moving error injection table from kprobes. This series includes some fixes and error type descriptions which we discussed in the previous thread.
Here is the previous version:
https://lkml.org/lkml/2017/12/26/26
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.
- 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.
Unfortunately currently CONFIG_FUNCTION_ERROR_INJECTION depends on CONFIG_KPROBES because of arch_deref_entry_point(), which should be provided by asm/types.h as ppc64 and ia64 do or kallsyms subsystem. Anyway, since that is another story, I will make another series to fix it.
For [1/5], I tested it by test_override_return.sh on the kernel with CONFIG_DYNAMIC_FTRACE=n, and succeeded as below. (actually I'v found some bugs and fixed, thanks Alexei!)
======
# LANG=C ./test_override_return.sh
1+0 records in
1+0 records out
1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.00200037 s, 524 MB/s btrfs-progs v4.9.1
See http://btrfs.wiki.kernel.org for more information.
Performing full device TRIM /dev/loop1 (1001.00MiB) ...
Label: (null)
UUID: 02635ea7-f6d1-4ee8-a677-4354a38f6930
Node size: 16384
Sector size: 4096
Filesystem size: 1001.00MiB
Block group profiles:
Data: single 8.00MiB
Metadata: DUP 50.00MiB
System: DUP 8.00MiB
SSD detected: no
Incompat features: extref, skinny-metadata
Number of devices: 1 Devices:
ID SIZE PATH
1 1001.00MiB /dev/loop1
mount: /opt/samples/bpf/tmpmnt: mount(2) system call failed: Cannot allocate memory. SUCCESS!
======
I've removed RFC from this series, but actually, [4/5] and [5/5] will be need more discussion. So please feel free to merge [1/5] to [3/5] separately.
[4/5] introduces error return types for describing what error values the function callers must expect.
For the purpose of error injection test is to find out hidden problems which caller doesn't handle errors correctly. So if the caller is programmed correctly, even if we inject an error, it continues to work as expected. However, if we inject a succeed return value even though the callee doesn't do anything, the caller does wrong processing (and end up with kernel panic, or even worse data corruption). That is not what we want to do with error injection.
[5/5] introduces function-based error injection interface via debugfs, which can cause "success injection", So I coupled it with [4/5] to ensure the expected error types for the target function.
Changes in v3:
- [1/5] Move arch_ftrace_kprobe_override_function() to core.c to remove CONFIG_KPROBE_ON_FTRACE dependency, and fix a bug in trace_kprobe_on_func_entry().
- [3/5] Fix a build error and typos, separate CONFIG_MODULES dependent code, add CONFIG_KPROBES dependency, and call error-injection init function in late_initcall stage.
- [4/5] Newly added
- [5/5] Check and adjust error value for each target function and add more documents and example.
Thank you,
---
Masami Hiramatsu (5):
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: Add injectable error types
error-injection: Support fault injection framework
Documentation/fault-injection/fault-injection.txt | 62 +++++ arch/Kconfig | 2 arch/x86/Kconfig | 2 arch/x86/include/asm/error-injection.h | 13 + arch/x86/include/asm/kprobes.h | 4 arch/x86/kernel/kprobes/ftrace.c | 14 - arch/x86/lib/Makefile | 1 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 | 35 +++ include/asm-generic/vmlinux.lds.h | 14 + include/linux/bpf.h | 12 - include/linux/error-injection.h | 27 ++ include/linux/kprobes.h | 1 include/linux/module.h | 7 - kernel/Makefile | 1 kernel/fail_function.c | 217 +++++++++++++++++++ kernel/kprobes.c | 163 -------------- kernel/module.c | 8 - kernel/trace/Kconfig | 4 kernel/trace/bpf_trace.c | 9 - kernel/trace/trace_kprobe.c | 33 +-- kernel/trace/trace_probe.h | 12 + lib/Kconfig.debug | 14 + lib/Makefile | 1 lib/error-inject.c | 242 +++++++++++++++++++++ 27 files changed, 679 insertions(+), 242 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)
Masami Hiramatsu
maintainerPATCH v3 1/5 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.
---
Changes in v3:
- Move arch_ftrace_kprobe_override_function() to core.c because it is no longer depending on ftrace.
- Fix a bug to skip passing kprobes target name to kprobe_on_func_entry(). Passing both @addr and @symbol to that function will result in failure.
--- arch/x86/include/asm/kprobes.h | 4 +--- arch/x86/kernel/kprobes/core.c | 14 ++++++++++++++ arch/x86/kernel/kprobes/ftrace.c | 14 -------------- kernel/trace/Kconfig | 2 -- kernel/trace/bpf_trace.c | 8 ++++---- kernel/trace/trace_kprobe.c | 9 ++++++--- kernel/trace/trace_probe.h | 12 ++++++------ 7 files changed, 31 insertions(+), 32 deletions(-)
arch/x86/include/asm/kprobes.h +1 -3
@@ -67,9 +67,7 @@ extern const int kretprobe_blacklist_size;void arch_remove_kprobe (struct kprobe *p); asmlinkage void kretprobe_trampoline(void); -#ifdef CONFIG_KPROBES_ON_FTRACE -extern void arch_ftrace_kprobe_override_function (struct pt_regs *regs); -#endif +extern void arch_kprobe_override_function (struct pt_regs *regs);/* Architecture specific copy of original instruction*/ struct arch_specific_insn {
arch/x86/kernel/kprobes/core.c +14 -0
@@ -1183,3 +1183,17 @@ int arch_trampoline_kprobe(struct kprobe *p) {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_kprobe_override_function (struct pt_regs *regs) +{ + regs->ip = (unsigned long )&override_func; +} +NOKPROBE_SYMBOL (arch_kprobe_override_function);
arch/x86/kernel/kprobes/ftrace.c +0 -14
@@ -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);
kernel/trace/Kconfig +0 -2
@@ -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 +4 -4
@@ -85,7 +85,7 @@ 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); +arch_kprobe_override_function (regs);return 0 ; }
@@ -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 +6 -3
@@ -88,13 +88,16 @@ 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.addr ?NULL : tk->rp.kp.symbol_name, + tk->rp.kp.addr ?0 : 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
@@ -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 */
Masami Hiramatsu
maintainerPATCH v3 2/5 tracing/kprobe: bpf: Compare instruction pointer with original one
In reply to Masami HiramatsuCompare 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
@@ -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_kprobe_override_function (regs);return 0 ;
kernel/trace/trace_kprobe.c +7 -14
@@ -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 ;
@@ -1205,6 +1203,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);
@@ -1212,12 +1211,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)
@@ -1325,15 +1325,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; }
Masami Hiramatsu
maintainerPATCH v3 3/5 error-injection: Separate error-injection from kprobe
In reply to Masami HiramatsuSince 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 v3:
- Fix a build error for asmlinkage on i386 by including compiler.h
- Fix "CONFIG_FUNCTION_ERROR_INJECT" typo.
- Separate CONFIG_MODULES dependent code
- Add CONFIG_KPROBES dependency for arch_deref_entry_point()
- Call error-injection init function in late_initcall stage.
- Fix read-side mutex lock
- Some cosmetic cleanups
--- arch/Kconfig | 2 arch/x86/Kconfig | 2 arch/x86/include/asm/error-injection.h | 13 ++ arch/x86/kernel/kprobes/core.c | 14 -- arch/x86/lib/Makefile | 1 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 | 213 ++++++++++++++++++++++++++++++++ 22 files changed, 315 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
@@ -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
@@ -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 +13 -0
@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_ERROR_INJECTION_H +#define _ASM_ERROR_INJECTION_H + +#include <linux/compiler.h> +#include <linux/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/core.c +0 -14
@@ -1183,17 +1183,3 @@ int arch_trampoline_kprobe(struct kprobe *p) {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_kprobe_override_function (struct pt_regs *regs) -{ - regs->ip = (unsigned long )&override_func; -} -NOKPROBE_SYMBOL (arch_kprobe_override_function);
arch/x86/lib/Makefile +1 -0
@@ -26,6 +26,7 @@ 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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -16,6 +16,7 @@#include <linux/rbtree_latch.h> #include <linux/numa.h> #include <linux/wait.h> +#include <linux/error-injection.h> struct bpf_verifier_env ;struct perf_event ;
@@ -593,15 +594,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
@@ -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
@@ -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
@@ -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
@@ -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_INJECTION + 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
@@ -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_INJECTION default n help Allows BPF to override the execution of a probed function and
kernel/trace/bpf_trace.c +1 -1
@@ -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_kprobe_override_function (regs); +override_function_with_return (regs);return 0 ; }
kernel/trace/trace_kprobe.c +2 -1
@@ -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"
@@ -107,7 +108,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
@@ -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 && KPROBES + config FAILSLAB bool "Fault-injection capability for kmalloc" depends on FAULT_INJECTION
lib/Makefile +1 -0
@@ -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 +213 -0
213 lines not on this page · raw message
Masami Hiramatsu
maintainerPATCH v3 4/5 error-injection: Add injectable error types
In reply to Masami HiramatsuAdd injectable error types for each error-injectable function.
One motivation of error injection test is to find software flaws, mistakes or mis-handlings of expectable errors. If we find such flaws by the test, that is a program bug, so we need to fix it.
But if the tester miss input the error (e.g. just return success code without processing anything), it causes unexpected behavior even if the caller is correctly programmed to handle any errors. That is not what we want to test by error injection.
To clarify what type of errors the caller must expect for each injectable function, this introduces injectable error types:
- EI_ETYPE_NULL : means the function will return NULL if it fails. No ERR_PTR, just a NULL.
- EI_ETYPE_ERRNO : means the function will return -ERRNO if it fails.
- EI_ETYPE_ERRNO_NULL : means the function will return -ERRNO (ERR_PTR) or NULL.
ALLOW_ERROR_INJECTION() macro is expanded to get one of NULL, ERRNO, ERRNO_NULL to record the error type for each function. e.g.
ALLOW_ERROR_INJECTION(open_ctree, ERRNO)
This error types are shown in debugfs as below.
====
/ # cat /sys/kernel/debug/error_injection/list
open_ctree [btrfs] ERRNO
io_ctl_init [btrfs] ERRNO
====
--- fs/btrfs/disk-io.c | 2 +- fs/btrfs/free-space-cache.c | 2 +- include/asm-generic/error-injection.h | 23 +++++++++++++++--- include/asm-generic/vmlinux.lds.h | 2 +- include/linux/error-injection.h | 6 +++++ include/linux/module.h | 3 ++ lib/error-inject.c | 43 ++++++++++++++++++++++++++++----- 7 files changed, 66 insertions(+), 15 deletions(-)
fs/btrfs/disk-io.c +1 -1
@@ -3124,7 +3124,7 @@ int open_ctree(struct super_block *sb, goto fail_block_groups; goto retry_root_backup; } -ALLOW_ERROR_INJECTION (open_ctree); +ALLOW_ERROR_INJECTION (open_ctree,ERRNO );static void btrfs_end_buffer_write_sync (struct buffer_head *bh,int uptodate) {
fs/btrfs/free-space-cache.c +1 -1
@@ -333,7 +333,7 @@ static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,return 0 ; } -ALLOW_ERROR_INJECTION (io_ctl_init); +ALLOW_ERROR_INJECTION (io_ctl_init,ERRNO );static void io_ctl_free (struct btrfs_io_ctl *io_ctl) {
include/asm-generic/error-injection.h +19 -4
@@ -3,17 +3,32 @@#define _ASM_GENERIC_ERROR_INJECTION_H#if defined(__KERNEL__) && !defined(__ASSEMBLY__) +enum { +EI_ETYPE_NONE ,/* Dummy value for undefined case */ +EI_ETYPE_NULL ,/* Return NULL if failure */ +EI_ETYPE_ERRNO ,/* Return -ERRNO if failure */ +EI_ETYPE_ERRNO_NULL ,/* Return -ERRNO or NULL if failure */ +}; + +struct error_injection_entry { +unsigned long addr; +int etype; +}; +#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 \ +#define ALLOW_ERROR_INJECTION (fname, _etype) \ +static struct error_injection_entry __used \ __attribute__((__section__("_error_injection_whitelist"))) \ - _eil_addr_##fname = (unsigned long)fname; + _eil_addr_##fname = { \ + .addr = (unsigned long)fname, \ + .etype = EI_ETYPE_##_etype, \ + };#else -#define ALLOW_ERROR_INJECTION (fname) +#define ALLOW_ERROR_INJECTION (fname, _etype)#endif #endif
include/asm-generic/vmlinux.lds.h +1 -1
@@ -137,7 +137,7 @@#endif #ifdef CONFIG_FUNCTION_ERROR_INJECTION -#define ERROR_INJECT_WHITELIST () . = ALIGN(8); \ +#define ERROR_INJECT_WHITELIST () STRUCT_ALIGN(); \ VMLINUX_SYMBOL(__start_error_injection_whitelist) = .;\ KEEP(*(_error_injection_whitelist)) \ VMLINUX_SYMBOL(__stop_error_injection_whitelist) = .;
include/linux/error-injection.h +6 -0
@@ -7,6 +7,7 @@#include <asm/error-injection.h> extern bool within_error_injection_list (unsigned long addr); +extern int get_injectable_error_type (unsigned long addr);#else /* !CONFIG_FUNCTION_ERROR_INJECTION */
@@ -16,6 +17,11 @@ static inline bool within_error_injection_list(unsigned long addr)return false; } +static inline int get_injectable_error_type (unsigned long addr) +{ +return EI_ETYPE_NONE ; +} +#endif #endif /* _LINUX_ERROR_INJECTION_H */
include/linux/module.h +2 -1
@@ -19,6 +19,7 @@#include <linux/jump_label.h> #include <linux/export.h> #include <linux/rbtree_latch.h> +#include <linux/error-injection.h> #include <linux/percpu.h> #include <asm/module.h>
@@ -477,8 +478,8 @@ struct module {#endif #ifdef CONFIG_FUNCTION_ERROR_INJECTION +struct error_injection_entry *ei_funcs;unsigned int num_ei_funcs; -unsigned long *ei_funcs;#endif }____cacheline_aligned __randomize_layout;#ifndef MODULE_ARCH_INIT
lib/error-inject.c +36 -7
89 lines not on this page · raw message
Masami Hiramatsu
maintainerPATCH v3 5/5 error-injection: Support fault injection framework
In reply to Masami HiramatsuSupport in-kernel fault-injection framework via debugfs. This allows you to inject a conditional error to specified function using debugfs interfaces.
Here is the result of test script described in Documentation/fault-injection/fault-injection.txt
===========
# ./test_fail_function.sh 1+0 records in 1+0 records out 1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.0227404 s, 46.1 MB/s btrfs-progs v4.4 See http://btrfs.wiki.kernel.org for more information.
Label: (null)
UUID: bfa96010-12e9-4360-aed0-42eec7af5798
Node size: 16384
Sector size: 4096
Filesystem size: 1001.00MiB
Block group profiles:
Data: single 8.00MiB
Metadata: DUP 58.00MiB
System: DUP 12.00MiB
SSD detected: no
Incompat features: extref, skinny-metadata
Number of devices: 1 Devices:
ID SIZE PATH
1 1001.00MiB /dev/loop2
mount: mount /dev/loop2 on /opt/tmpmnt failed: Cannot allocate memory SUCCESS!
===========
---
Changes in v3:
- Check and adjust error value for each target function
- Clear kporbe flag for reuse
- Add more documents and example
--- Documentation/fault-injection/fault-injection.txt | 62 ++++++ kernel/Makefile | 1 kernel/fail_function.c | 217 +++++++++++++++++++++ lib/Kconfig.debug | 10 + 4 files changed, 290 insertions(+) create mode 100644 kernel/fail_function.c
Documentation/fault-injection/fault-injection.txt +62 -0
@@ -30,6 +30,12 @@ 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, which are marked by + ALLOW_ERROR_INJECTION() macro, by setting debugfs entries + under /sys/kernel/debug/fail_function. No boot option supported. + Configure fault-injection capabilities behavior -----------------------------------------------
@@ -123,6 +129,24 @@ configuration of fault-injection capabilities. default is 'N', setting it to 'Y' will disable failure injections when dealing with private (address space) futexes. +- /sys/kernel/debug/fail_function/inject: + + specifies the target function of error injection by name. + +- /sys/kernel/debug/fail_function/retval: + + specifies the "error" return value to inject to the given + function. + +- /sys/kernel/debug/fail_function/injectable: + + (read only) shows error injectable functions and what type of + error values can be specified. The error type will be one of + below; + - NULL: retval must be 0. + - ERRNO: retval must be -1 to -MAX_ERRNO (-4096). + - ERR_NULL: retval must be 0 or -1 to -MAX_ERRNO (-4096). + o Boot option In order to inject faults while debugfs is not available (early boot time),
@@ -268,6 +292,44 @@ trap "echo 0 > /sys/kernel/debug/$FAILTYPE/probability" SIGINT SIGTERM EXIT echo "Injecting errors into the module $module... (interrupt to stop)" sleep 1000000 +------------------------------------------------------------------------------ + +o Inject open_ctree error while btrfs mount + +#!/bin/bash + +rm -f testfile.img +dd if=/dev/zero of=testfile.img bs=1M seek=1000 count=1 +DEVICE=$(losetup --show -f testfile.img) +mkfs.btrfs -f $DEVICE +mkdir -p tmpmnt + +FAILTYPE=fail_function +echo open_ctree > /sys/kernel/debug/$FAILTYPE/inject +echo -12 > /sys/kernel/debug/$FAILTYPE/retval +echo N > /sys/kernel/debug/$FAILTYPE/task-filter +echo 100 > /sys/kernel/debug/$FAILTYPE/probability +echo 0 > /sys/kernel/debug/$FAILTYPE/interval +echo -1 > /sys/kernel/debug/$FAILTYPE/times +echo 0 > /sys/kernel/debug/$FAILTYPE/space +echo 1 > /sys/kernel/debug/$FAILTYPE/verbose + +mount -t btrfs $DEVICE tmpmnt +if [ $? -ne 0 ] +then + echo "SUCCESS!" +else + echo "FAILED!" + umount tmpmnt +fi + +echo > /sys/kernel/debug/$FAILTYPE/inject + +rmdir tmpmnt +losetup -d $DEVICE +rm testfile.img + + Tool to run command with failslab or fail_page_alloc ---------------------------------------------------- In order to make it easier to accomplish the tasks mentioned above, we can use
kernel/Makefile +1 -0
@@ -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 +217 -0
217 lines not on this page · raw message
lib/Kconfig.debug +10 -0
@@ -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
Re: [PATCH bpf-next v3 1/5] tracing/kprobe: bpf: Check error injectable event is on function entry
In reply to Masami HiramatsuQuoted 7 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.
Quoted 5 lines
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.
Signed-off-by Masami Hiramatsu <[email protected]>
Thanks,
Josef
Re: [PATCH bpf-next v3 2/5] tracing/kprobe: bpf: Compare instruction pointer with original one
In reply to Masami HiramatsuQuoted 8 lines · Masami Hiramatsu
Compare instruction pointer with original one on the stack instead using per-cpu
bpf_kprobe_overrideflag.This patch also consolidates
reset_current_kprobe()andpreempt_enable_no_resched()blocks. Those can be done in one place.Signed-off-by Masami Hiramatsu <[email protected]>
Thanks,
Josef
Re: [PATCH bpf-next v3 3/5] error-injection: Separate error-injection from kprobe
In reply to Masami HiramatsuQuoted 249 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 toALLOW_ERROR_INJECTION()since it is not limited for BPF too.
-CONFIG_FUNCTION_ERROR_INJECTIONis the config item of this feature. It is automatically enabled if the arch supports error injection feature for kprobe or ftrace etc.Signed-off-by Masami Hiramatsu <[email protected]>---
Changes in v3:
- Fix a build error for asmlinkage on i386 by includingcompiler.h
- Fix "CONFIG_FUNCTION_ERROR_INJECT" typo.
- SeparateCONFIG_MODULESdependent code
- AddCONFIG_KPROBESdependency forarch_deref_entry_point()
- Call error-injection init function inlate_initcallstage.
- Fix read-side mutex lock
- Some cosmetic cleanups--- arch/Kconfig | 2 arch/x86/Kconfig | 2 arch/x86/include/asm/error-injection.h | 13 ++ arch/x86/kernel/kprobes/core.c | 14 -- arch/x86/lib/Makefile | 1 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 | 213 ++++++++++++++++++++++++++++++++ 22 files changed, 315 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.carch/Kconfig +1
-1@@ -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_NMIarch/x86/Kconfig +1
-1@@ -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_64arch/x86/include/asm/error-injection.h new file +13
-0@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_ERROR_INJECTION_H +#define _ASM_ERROR_INJECTION_H + +#include <linux/compiler.h> +#include <linux/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/core.c +0
-14@@ -1183,17 +1183,3 @@ int arch_trampoline_kprobe(struct kprobe *p) {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_kprobe_override_function (struct pt_regs *regs) -{ - regs->ip = (unsigned long )&override_func; -} -NOKPROBE_SYMBOL (arch_kprobe_override_function);arch/x86/lib/Makefile +1
-0@@ -26,6 +26,7 @@ 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.oarch/x86/lib/error-inject.c new file +19
-0@@ -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@@ -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@@ -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@@ -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@@ -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 +0
-0
include/linux/bpf.h line 20
Quoted 5 lines
include/linux/bpf.h +1
-0@@ -16,6 +16,7 @@#include <linux/rbtree_latch.h> #include <linux/numa.h> #include <linux/wait.h> +#include <linux/error-injection.h>
I assume you did this because we include linux/bpf.h for the BPF_ALLOW_ERROR_INJECTION() stuff in btrfs. Can we just drop this include here, and change the users of ALLOW_ERROR_INJECTION() to include error-injection.h instead?
<snip>
Quoted 15 lines
include/linux/bpf.h +15
-0+/* +* error_injection/whitelist -- shows which functions can be overridden for +* error injection. +*/ +static void *ei_seq_start (struct seq_file *m,loff_t *pos) +{ +mutex_lock (&ei_mutex); +return seq_list_start (&error_injection_list, *pos); +} + +static void ei_seq_stop (struct seq_file *m,void *v) +{ +mutex_unlock (&ei_mutex); +} +
include/linux/bpf.h
Quoted 10 lines
include/linux/bpf.h +10
-0+static void *ei_seq_next (struct seq_file *m,void *v,loff_t *pos) +{ +return seq_list_next (v, &error_injection_list, pos); +} + +static int ei_seq_show (struct seq_file *m,void *v) +{ + struct ei_entry *ent = list_entry(v, struct ei_entry, list); + +seq_printf (m,"%pf\n" , (void *)ent->start_addr);
Can we bring back the sprint_symbol() thing I did originally here so it's nice and easy to sanity check stuff is working? Thanks
Josef
Re: [PATCH bpf-next v3 4/5] error-injection: Add injectable error types
In reply to Masami HiramatsuQuoted 260 lines · Masami Hiramatsu
Add injectable error types for each error-injectable function.
One motivation of error injection test is to find software flaws, mistakes or mis-handlings of expectable errors. If we find such flaws by the test, that is a program bug, so we need to fix it.
But if the tester miss input the error (e.g. just return success code without processing anything), it causes unexpected behavior even if the caller is correctly programmed to handle any errors. That is not what we want to test by error injection.
To clarify what type of errors the caller must expect for each injectable function, this introduces injectable error types:
-
EI_ETYPE_NULL: means the function will return NULL if it fails. NoERR_PTR, just a NULL.
-EI_ETYPE_ERRNO: means the function will return-ERRNOif it fails.
-EI_ETYPE_ERRNO_NULL: means the function will return-ERRNO(ERR_PTR) or NULL.
ALLOW_ERROR_INJECTION()macro is expanded to get one of NULL, ERRNO,ERRNO_NULLto record the error type for each function. e.g.
ALLOW_ERROR_INJECTION(open_ctree, ERRNO)This error types are shown in debugfs as below.
====
/ # cat/sys/kernel/debug/error_injection/list
open_ctree[btrfs] ERRNO
io_ctl_init[btrfs] ERRNO
====Signed-off-by Masami Hiramatsu <[email protected]>--- fs/btrfs/disk-io.c | 2 +- fs/btrfs/free-space-cache.c | 2 +- include/asm-generic/error-injection.h | 23 +++++++++++++++--- include/asm-generic/vmlinux.lds.h | 2 +- include/linux/error-injection.h | 6 +++++ include/linux/module.h | 3 ++ lib/error-inject.c | 43 ++++++++++++++++++++++++++++----- 7 files changed, 66 insertions(+), 15 deletions(-)fs/btrfs/disk-io.c +1
-1@@ -3124,7 +3124,7 @@ int open_ctree(struct super_block *sb, goto fail_block_groups; goto retry_root_backup; } -ALLOW_ERROR_INJECTION (open_ctree); +ALLOW_ERROR_INJECTION (open_ctree,ERRNO );static void btrfs_end_buffer_write_sync (struct buffer_head *bh,int uptodate) {fs/btrfs/free-space-cache.c +1
-1@@ -333,7 +333,7 @@ static int io_ctl_init(struct btrfs_io_ctl *io_ctl, struct inode *inode,return 0 ; } -ALLOW_ERROR_INJECTION (io_ctl_init); +ALLOW_ERROR_INJECTION (io_ctl_init,ERRNO );static void io_ctl_free (struct btrfs_io_ctl *io_ctl) {include/asm-generic/error-injection.h +19
-4@@ -3,17 +3,32 @@#define _ASM_GENERIC_ERROR_INJECTION_H#if defined(__KERNEL__) && !defined(__ASSEMBLY__) +enum { +EI_ETYPE_NONE ,/* Dummy value for undefined case */ +EI_ETYPE_NULL ,/* Return NULL if failure */ +EI_ETYPE_ERRNO ,/* Return -ERRNO if failure */ +EI_ETYPE_ERRNO_NULL ,/* Return -ERRNO or NULL if failure */ +}; + +struct error_injection_entry { +unsigned long addr; +int etype; +}; +#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 \ +#define ALLOW_ERROR_INJECTION (fname, _etype) \ +static struct error_injection_entry __used \ __attribute__((__section__("_error_injection_whitelist"))) \ - _eil_addr_##fname = (unsigned long)fname; + _eil_addr_##fname = { \ + .addr = (unsigned long)fname, \ + .etype = EI_ETYPE_##_etype, \ + };#else -#define ALLOW_ERROR_INJECTION (fname) +#define ALLOW_ERROR_INJECTION (fname, _etype)#endif #endif include/asm-generic/vmlinux.lds.h +1
-1@@ -137,7 +137,7 @@#endif #ifdef CONFIG_FUNCTION_ERROR_INJECTION -#define ERROR_INJECT_WHITELIST () . = ALIGN(8); \ +#define ERROR_INJECT_WHITELIST () STRUCT_ALIGN(); \ VMLINUX_SYMBOL(__start_error_injection_whitelist) = .;\ KEEP(*(_error_injection_whitelist)) \ VMLINUX_SYMBOL(__stop_error_injection_whitelist) = .;include/linux/error-injection.h +6
-0@@ -7,6 +7,7 @@#include <asm/error-injection.h> extern bool within_error_injection_list (unsigned long addr); +extern int get_injectable_error_type (unsigned long addr);#else /* !CONFIG_FUNCTION_ERROR_INJECTION */ @@ -16,6 +17,11 @@ static inline bool within_error_injection_list(unsigned long addr)return false; } +static inline int get_injectable_error_type (unsigned long addr) +{ +return EI_ETYPE_NONE ; +} +#endif #endif /* _LINUX_ERROR_INJECTION_H */ include/linux/module.h +2
-1@@ -19,6 +19,7 @@#include <linux/jump_label.h> #include <linux/export.h> #include <linux/rbtree_latch.h> +#include <linux/error-injection.h> #include <linux/percpu.h> #include <asm/module.h> @@ -477,8 +478,8 @@ struct module {#endif #ifdef CONFIG_FUNCTION_ERROR_INJECTION +struct error_injection_entry *ei_funcs;unsigned int num_ei_funcs; -unsigned long *ei_funcs;#endif }____cacheline_aligned __randomize_layout;#ifndef MODULE_ARCH_INIT lib/error-inject.c +33
-6@@ -16,6 +16,7 @@ struct ei_entry {struct list_head list;unsigned long start_addr;unsigned long end_addr; +int etype;void *priv; };@@ -35,6 +36,17 @@ bool within_error_injection_list(unsigned long addr)return false; } +int get_injectable_error_type (unsigned long addr) +{ +struct ei_entry *ent; + +list_for_each_entry (ent, &error_injection_list, list) { +if (addr >= ent->start_addr && addr < ent->end_addr) +return ent->etype; + } +return EI_ETYPE_NONE ; +} +/* * Lookup and populate the error_injection_list. * @@ -42,16 +54,17 @@ bool within_error_injection_list(unsigned long addr)* bpf_error_injection, so we need to populate the list of the symbols that have * been marked as safe for overriding. */ -static void populate_error_injection_list (unsigned long *start, -unsigned long *end,void *priv) +static void populate_error_injection_list (struct error_injection_entry *start, +struct error_injection_entry *end, +void *priv) { -unsigned long *iter; +struct error_injection_entry *iter;struct ei_entry *ent;unsigned long entry, offset =0 , size =0 ;mutex_lock (&ei_mutex);for (iter = start; iter < end; iter++) { - entry =arch_deref_entry_point ((void *)*iter); + entry =arch_deref_entry_point ((void *)iter->addr);if (!kernel_text_address (entry) || !kallsyms_lookup_size_offset (entry, &size, &offset)) {@@ -65,6 +78,7 @@ static void populate_error_injection_list(unsigned long *start,break ; ent->start_addr = entry; ent->end_addr = entry + size; + ent->etype = iter->etype; ent->priv = priv;INIT_LIST_HEAD (&ent->list);list_add_tail (&ent->list, &error_injection_list);@@ -73,8 +87,8 @@ static void populate_error_injection_list(unsigned long *start, }/* Markers of the _error_inject_whitelist section */ -extern unsigned long __start_error_injection_whitelist[]; -extern unsigned long __stop_error_injection_whitelist[]; +extern struct error_injection_entry __start_error_injection_whitelist[]; +extern struct error_injection_entry __stop_error_injection_whitelist[];static void __init populate_kernel_ei_list (void ) {@@ -157,11 +171,26 @@ static void *ei_seq_next(struct seq_file *m, void *v, loff_t *pos)return seq_list_next (v, &error_injection_list, pos); } +static const char *error_type_string (int etype) +{ +switch (etype) { +case EI_ETYPE_NULL : +return "NULL" ; +case EI_ETYPE_ERRNO : +return "ERRNO" ; +case EI_ETYPE_ERRNO_NULL : +return "ERRNO_NULL" ; +default : +return "(unknown)" ; + } +}
lib/error-inject.c static void *ei_seq_next(struct seq_file *m, void *v, loff_t *pos) · line 195
Quoted 10 lines
lib/error-inject.c +3
-1+static int ei_seq_show (struct seq_file *m,void *v) { struct ei_entry *ent = list_entry(v, struct ei_entry, list); -seq_printf (m,"%pf\n" , (void *)ent->start_addr); +seq_printf (m,"%pf\t%s\n" , (void *)ent->start_addr, +error_type_string (ent->etype));return 0 ; }
Lol ignore the comment in my previous review about this part. Also I love this,
Thanks,
Josef
Re: [PATCH bpf-next v3 5/5] error-injection: Support fault injection framework
In reply to Masami HiramatsuQuoted 31 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.
Here is the result of test script described in
Documentation/fault-injection/fault-injection.txt===========
# ./test_fail_function.sh 1+0 records in 1+0 records out 1048576 bytes (1.0 MB, 1.0 MiB) copied, 0.0227404 s, 46.1 MB/s btrfs-progs v4.4 See http://btrfs.wiki.kernel.org for more information.Label: (null) UUID: bfa96010-12e9-4360-aed0-42eec7af5798 Node size: 16384 Sector size: 4096 Filesystem size: 1001.00MiB Block group profiles: Data: single 8.00MiB Metadata: DUP 58.00MiB System: DUP 12.00MiB SSD detected: no Incompat features: extref, skinny-metadata Number of devices: 1Devices:
ID SIZE PATH
1 1001.00MiB/dev/loop2
Quoted 6 lines
mount: mount
/dev/loop2on/opt/tmpmntfailed: Cannot allocate memory SUCCESS!
===========Signed-off-by Masami Hiramatsu <[email protected]>
Thanks,
Josef
Masami Hiramatsu
maintainerRe: [PATCH bpf-next v3 3/5] error-injection: Separate error-injection from kprobe
In reply to Josef BacikQuoted 252 lines · On Wed, 10 Jan 2018 10:36:15 -0500 Josef Bacik <[email protected]> wrote:
Quoted 251 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 toALLOW_ERROR_INJECTION()since it is not limited for BPF too.
-CONFIG_FUNCTION_ERROR_INJECTIONis the config item of this feature. It is automatically enabled if the arch supports error injection feature for kprobe or ftrace etc.Signed-off-by Masami Hiramatsu <[email protected]>---
Changes in v3:
- Fix a build error for asmlinkage on i386 by includingcompiler.h
- Fix "CONFIG_FUNCTION_ERROR_INJECT" typo.
- SeparateCONFIG_MODULESdependent code
- AddCONFIG_KPROBESdependency forarch_deref_entry_point()
- Call error-injection init function inlate_initcallstage.
- Fix read-side mutex lock
- Some cosmetic cleanups--- arch/Kconfig | 2 arch/x86/Kconfig | 2 arch/x86/include/asm/error-injection.h | 13 ++ arch/x86/kernel/kprobes/core.c | 14 -- arch/x86/lib/Makefile | 1 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 | 213 ++++++++++++++++++++++++++++++++ 22 files changed, 315 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.carch/Kconfig +1
-1@@ -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_NMIarch/x86/Kconfig +1
-1@@ -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_64arch/x86/include/asm/error-injection.h new file +13
-0@@ -0,0 +1,13 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _ASM_ERROR_INJECTION_H +#define _ASM_ERROR_INJECTION_H + +#include <linux/compiler.h> +#include <linux/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/core.c +0
-14@@ -1183,17 +1183,3 @@ int arch_trampoline_kprobe(struct kprobe *p) {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_kprobe_override_function (struct pt_regs *regs) -{ - regs->ip = (unsigned long )&override_func; -} -NOKPROBE_SYMBOL (arch_kprobe_override_function);arch/x86/lib/Makefile +1
-0@@ -26,6 +26,7 @@ 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.oarch/x86/lib/error-inject.c new file +19
-0@@ -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@@ -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@@ -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@@ -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@@ -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 +0
-0@@ -16,6 +16,7 @@#include <linux/rbtree_latch.h> #include <linux/numa.h>
Quoted 8 lines
include/linux/bpf.h line 20
Quoted 3 lines
include/linux/bpf.h +1
-0#include <linux/wait.h> +#include <linux/error-injection.h> I assume you did this because we include
linux/bpf.hfor theBPF_ALLOW_ERROR_INJECTION()stuff in btrfs. Can we just drop this include here, and change the users ofALLOW_ERROR_INJECTION()to includeerror-injection.hinstead?
Indeed, since the macro is no longer have BPF_* prefix, user source file should include the definition header directly.
Thank you,
Quoted 33 lines
<snip>
Quoted 15 lines
include/linux/bpf.h +15
-0+/* +* error_injection/whitelist -- shows which functions can be overridden for +* error injection. +*/ +static void *ei_seq_start (struct seq_file *m,loff_t *pos) +{ +mutex_lock (&ei_mutex); +return seq_list_start (&error_injection_list, *pos); +} + +static void ei_seq_stop (struct seq_file *m,void *v) +{ +mutex_unlock (&ei_mutex); +} +include/linux/bpf.h
Quoted 10 lines
include/linux/bpf.h +10
-0+static void *ei_seq_next (struct seq_file *m,void *v,loff_t *pos) +{ +return seq_list_next (v, &error_injection_list, pos); +} + +static int ei_seq_show (struct seq_file *m,void *v) +{ + struct ei_entry *ent = list_entry(v, struct ei_entry, list); + +seq_printf (m,"%pf\n" , (void *)ent->start_addr);Can we bring back the
sprint_symbol()thing I did originally here so it's nice and easy to sanity check stuff is working? ThanksJosef
Signature · 1 line
Masami Hiramatsu <[email protected]>