1 // SPDX-License-Identifier: GPL-2.0
2 // error-inject.c: Function-level error injection table
3 #include <linux/error-injection.h>
4 #include <linux/debugfs.h>
5 #include <linux/kallsyms.h>
6 #include <linux/kprobes.h>
7 #include <linux/module.h>
8 #include <linux/mutex.h>
9 #include <linux/list.h>
10 #include <linux/slab.h>
11 #include <asm/sections.h>
13 /* Whitelist of symbols that can be overridden for error injection. */
14 static LIST_HEAD(error_injection_list);
15 static DEFINE_MUTEX(ei_mutex);
17 struct list_head list;
18 unsigned long start_addr;
19 unsigned long end_addr;
24 bool within_error_injection_list(unsigned long addr)
29 mutex_lock(&ei_mutex);
30 list_for_each_entry(ent, &error_injection_list, list) {
31 if (addr >= ent->start_addr && addr < ent->end_addr) {
36 mutex_unlock(&ei_mutex);
40 int get_injectable_error_type(unsigned long addr)
44 list_for_each_entry(ent, &error_injection_list, list) {
45 if (addr >= ent->start_addr && addr < ent->end_addr)
52 * Lookup and populate the error_injection_list.
54 * For safety reasons we only allow certain functions to be overridden with
55 * bpf_error_injection, so we need to populate the list of the symbols that have
56 * been marked as safe for overriding.
58 static void populate_error_injection_list(struct error_injection_entry *start,
59 struct error_injection_entry *end,
62 struct error_injection_entry *iter;
64 unsigned long entry, offset = 0, size = 0;
66 mutex_lock(&ei_mutex);
67 for (iter = start; iter < end; iter++) {
68 entry = (unsigned long)dereference_symbol_descriptor((void *)iter->addr);
70 if (!kernel_text_address(entry) ||
71 !kallsyms_lookup_size_offset(entry, &size, &offset)) {
72 pr_err("Failed to find error inject entry at %p\n",
77 ent = kmalloc(sizeof(*ent), GFP_KERNEL);
80 ent->start_addr = entry;
81 ent->end_addr = entry + size;
82 ent->etype = iter->etype;
84 INIT_LIST_HEAD(&ent->list);
85 list_add_tail(&ent->list, &error_injection_list);
87 mutex_unlock(&ei_mutex);
90 /* Markers of the _error_inject_whitelist section */
91 extern struct error_injection_entry __start_error_injection_whitelist[];
92 extern struct error_injection_entry __stop_error_injection_whitelist[];
94 static void __init populate_kernel_ei_list(void)
96 populate_error_injection_list(__start_error_injection_whitelist,
97 __stop_error_injection_whitelist,
101 #ifdef CONFIG_MODULES
102 static void module_load_ei_list(struct module *mod)
104 if (!mod->num_ei_funcs)
107 populate_error_injection_list(mod->ei_funcs,
108 mod->ei_funcs + mod->num_ei_funcs, mod);
111 static void module_unload_ei_list(struct module *mod)
113 struct ei_entry *ent, *n;
115 if (!mod->num_ei_funcs)
118 mutex_lock(&ei_mutex);
119 list_for_each_entry_safe(ent, n, &error_injection_list, list) {
120 if (ent->priv == mod) {
121 list_del_init(&ent->list);
125 mutex_unlock(&ei_mutex);
128 /* Module notifier call back, checking error injection table on the module */
129 static int ei_module_callback(struct notifier_block *nb,
130 unsigned long val, void *data)
132 struct module *mod = data;
134 if (val == MODULE_STATE_COMING)
135 module_load_ei_list(mod);
136 else if (val == MODULE_STATE_GOING)
137 module_unload_ei_list(mod);
142 static struct notifier_block ei_module_nb = {
143 .notifier_call = ei_module_callback,
147 static __init int module_ei_init(void)
149 return register_module_notifier(&ei_module_nb);
151 #else /* !CONFIG_MODULES */
152 #define module_ei_init() (0)
156 * error_injection/whitelist -- shows which functions can be overridden for
159 static void *ei_seq_start(struct seq_file *m, loff_t *pos)
161 mutex_lock(&ei_mutex);
162 return seq_list_start(&error_injection_list, *pos);
165 static void ei_seq_stop(struct seq_file *m, void *v)
167 mutex_unlock(&ei_mutex);
170 static void *ei_seq_next(struct seq_file *m, void *v, loff_t *pos)
172 return seq_list_next(v, &error_injection_list, pos);
175 static const char *error_type_string(int etype)
182 case EI_ETYPE_ERRNO_NULL:
191 static int ei_seq_show(struct seq_file *m, void *v)
193 struct ei_entry *ent = list_entry(v, struct ei_entry, list);
195 seq_printf(m, "%ps\t%s\n", (void *)ent->start_addr,
196 error_type_string(ent->etype));
200 static const struct seq_operations ei_seq_ops = {
201 .start = ei_seq_start,
207 static int ei_open(struct inode *inode, struct file *filp)
209 return seq_open(filp, &ei_seq_ops);
212 static const struct file_operations debugfs_ei_ops = {
216 .release = seq_release,
219 static int __init ei_debugfs_init(void)
221 struct dentry *dir, *file;
223 dir = debugfs_create_dir("error_injection", NULL);
227 file = debugfs_create_file("list", 0444, dir, NULL, &debugfs_ei_ops);
236 static int __init init_error_injection(void)
238 populate_kernel_ei_list();
240 if (!module_ei_init())
245 late_initcall(init_error_injection);