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>
12 /* Whitelist of symbols that can be overridden for error injection. */
13 static LIST_HEAD(error_injection_list
);
14 static DEFINE_MUTEX(ei_mutex
);
16 struct list_head list
;
17 unsigned long start_addr
;
18 unsigned long end_addr
;
23 bool within_error_injection_list(unsigned long addr
)
28 mutex_lock(&ei_mutex
);
29 list_for_each_entry(ent
, &error_injection_list
, list
) {
30 if (addr
>= ent
->start_addr
&& addr
< ent
->end_addr
) {
35 mutex_unlock(&ei_mutex
);
39 int get_injectable_error_type(unsigned long addr
)
43 list_for_each_entry(ent
, &error_injection_list
, list
) {
44 if (addr
>= ent
->start_addr
&& addr
< ent
->end_addr
)
51 * Lookup and populate the error_injection_list.
53 * For safety reasons we only allow certain functions to be overridden with
54 * bpf_error_injection, so we need to populate the list of the symbols that have
55 * been marked as safe for overriding.
57 static void populate_error_injection_list(struct error_injection_entry
*start
,
58 struct error_injection_entry
*end
,
61 struct error_injection_entry
*iter
;
63 unsigned long entry
, offset
= 0, size
= 0;
65 mutex_lock(&ei_mutex
);
66 for (iter
= start
; iter
< end
; iter
++) {
67 entry
= arch_deref_entry_point((void *)iter
->addr
);
69 if (!kernel_text_address(entry
) ||
70 !kallsyms_lookup_size_offset(entry
, &size
, &offset
)) {
71 pr_err("Failed to find error inject entry at %p\n",
76 ent
= kmalloc(sizeof(*ent
), GFP_KERNEL
);
79 ent
->start_addr
= entry
;
80 ent
->end_addr
= entry
+ size
;
81 ent
->etype
= iter
->etype
;
83 INIT_LIST_HEAD(&ent
->list
);
84 list_add_tail(&ent
->list
, &error_injection_list
);
86 mutex_unlock(&ei_mutex
);
89 /* Markers of the _error_inject_whitelist section */
90 extern struct error_injection_entry __start_error_injection_whitelist
[];
91 extern struct error_injection_entry __stop_error_injection_whitelist
[];
93 static void __init
populate_kernel_ei_list(void)
95 populate_error_injection_list(__start_error_injection_whitelist
,
96 __stop_error_injection_whitelist
,
100 #ifdef CONFIG_MODULES
101 static void module_load_ei_list(struct module
*mod
)
103 if (!mod
->num_ei_funcs
)
106 populate_error_injection_list(mod
->ei_funcs
,
107 mod
->ei_funcs
+ mod
->num_ei_funcs
, mod
);
110 static void module_unload_ei_list(struct module
*mod
)
112 struct ei_entry
*ent
, *n
;
114 if (!mod
->num_ei_funcs
)
117 mutex_lock(&ei_mutex
);
118 list_for_each_entry_safe(ent
, n
, &error_injection_list
, list
) {
119 if (ent
->priv
== mod
) {
120 list_del_init(&ent
->list
);
124 mutex_unlock(&ei_mutex
);
127 /* Module notifier call back, checking error injection table on the module */
128 static int ei_module_callback(struct notifier_block
*nb
,
129 unsigned long val
, void *data
)
131 struct module
*mod
= data
;
133 if (val
== MODULE_STATE_COMING
)
134 module_load_ei_list(mod
);
135 else if (val
== MODULE_STATE_GOING
)
136 module_unload_ei_list(mod
);
141 static struct notifier_block ei_module_nb
= {
142 .notifier_call
= ei_module_callback
,
146 static __init
int module_ei_init(void)
148 return register_module_notifier(&ei_module_nb
);
150 #else /* !CONFIG_MODULES */
151 #define module_ei_init() (0)
155 * error_injection/whitelist -- shows which functions can be overridden for
158 static void *ei_seq_start(struct seq_file
*m
, loff_t
*pos
)
160 mutex_lock(&ei_mutex
);
161 return seq_list_start(&error_injection_list
, *pos
);
164 static void ei_seq_stop(struct seq_file
*m
, void *v
)
166 mutex_unlock(&ei_mutex
);
169 static void *ei_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
171 return seq_list_next(v
, &error_injection_list
, pos
);
174 static const char *error_type_string(int etype
)
181 case EI_ETYPE_ERRNO_NULL
:
188 static int ei_seq_show(struct seq_file
*m
, void *v
)
190 struct ei_entry
*ent
= list_entry(v
, struct ei_entry
, list
);
192 seq_printf(m
, "%ps\t%s\n", (void *)ent
->start_addr
,
193 error_type_string(ent
->etype
));
197 static const struct seq_operations ei_seq_ops
= {
198 .start
= ei_seq_start
,
204 static int ei_open(struct inode
*inode
, struct file
*filp
)
206 return seq_open(filp
, &ei_seq_ops
);
209 static const struct file_operations debugfs_ei_ops
= {
213 .release
= seq_release
,
216 static int __init
ei_debugfs_init(void)
218 struct dentry
*dir
, *file
;
220 dir
= debugfs_create_dir("error_injection", NULL
);
224 file
= debugfs_create_file("list", 0444, dir
, NULL
, &debugfs_ei_ops
);
233 static int __init
init_error_injection(void)
235 populate_kernel_ei_list();
237 if (!module_ei_init())
242 late_initcall(init_error_injection
);