Separate error injection framework from kprobes
Subsystems (14)
Masami Hiramatsu
maintainercover Separate error injection framework from kprobes
Hi Josef and Alexei,
Here are the patches which describe what I think more "natural" introduction of error injection APIs. Basically what I did on this series is to separate error injection from kprobes and put it on new error-injection small subsystem which is currently provide whitelists and just-return function stub.
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.
This series also have some improvement suggestions.
- [1/3] "kprobe override function" feature is not limited by ftrace-based kprobe, but also you can use it on sw-breakpoint based kprobe too. Also, you must check the kprobe is on the entry of function right before setting up the stackframe.
- [2/3] If we store original instruction pointer and compare it with regs->ip, we don't need per-cpu bpf_kprobe_override. Also, reset_current_kprobe() and preempt_enable_no_resched() are no need to separate.
Any thoughts?
If it is good, I also add MAINTAINERS entry for this feature and add some testcases using kprobes and ftrace to inject error. (And maybe we also need a document how to use)
BTW, it seems there are many error injection frameworks in lib/. We may also consider these distinctions.
Thank you,
---
Masami Hiramatsu (3):
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
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 | 4 - kernel/trace/bpf_trace.c | 9 + kernel/trace/trace_kprobe.c | 32 ++--- kernel/trace/trace_probe.h | 12 +- lib/Kconfig.debug | 4 + lib/Makefile | 1 lib/error-inject.c | 200 ++++++++++++++++++++++++++++++++ 23 files changed, 323 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 lib/error-inject.c
Masami Hiramatsu
maintainerRFC PATCH 1/3 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
@@ -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
@@ -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
@@ -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
@@ -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
maintainerRFC PATCH 2/3 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_ftrace_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 ;
@@ -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; }
Masami Hiramatsu
maintainerRFC PATCH 3/3 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.
--- 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 | 200 ++++++++++++++++++++++++++++++++ 22 files changed, 302 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 +12 -0
@@ -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_to_return (struct pt_regs *regs); + +#endif /* _ASM_ERROR_INJECTION_H */
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);
arch/x86/lib/Makefile +2 -0
@@ -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
@@ -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_to_return (struct pt_regs *regs) +{ + regs->ip = (unsigned long )&just_return_func; +} +NOKPROBE_SYMBOL (override_function_to_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 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
@@ -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_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
@@ -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
@@ -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_to_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"
@@ -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
@@ -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
@@ -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 +200 -0
200 lines not on this page · raw message