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
)
43 int ei_type
= -EINVAL
;
45 mutex_lock(&ei_mutex
);
46 list_for_each_entry(ent
, &error_injection_list
, list
) {
47 if (addr
>= ent
->start_addr
&& addr
< ent
->end_addr
) {
52 mutex_unlock(&ei_mutex
);
58 * Lookup and populate the error_injection_list.
60 * For safety reasons we only allow certain functions to be overridden with
61 * bpf_error_injection, so we need to populate the list of the symbols that have
62 * been marked as safe for overriding.
64 static void populate_error_injection_list(struct error_injection_entry
*start
,
65 struct error_injection_entry
*end
,
68 struct error_injection_entry
*iter
;
70 unsigned long entry
, offset
= 0, size
= 0;
72 mutex_lock(&ei_mutex
);
73 for (iter
= start
; iter
< end
; iter
++) {
74 entry
= (unsigned long)dereference_symbol_descriptor((void *)iter
->addr
);
76 if (!kernel_text_address(entry
) ||
77 !kallsyms_lookup_size_offset(entry
, &size
, &offset
)) {
78 pr_err("Failed to find error inject entry at %p\n",
83 ent
= kmalloc(sizeof(*ent
), GFP_KERNEL
);
86 ent
->start_addr
= entry
;
87 ent
->end_addr
= entry
+ size
;
88 ent
->etype
= iter
->etype
;
90 INIT_LIST_HEAD(&ent
->list
);
91 list_add_tail(&ent
->list
, &error_injection_list
);
93 mutex_unlock(&ei_mutex
);
96 /* Markers of the _error_inject_whitelist section */
97 extern struct error_injection_entry __start_error_injection_whitelist
[];
98 extern struct error_injection_entry __stop_error_injection_whitelist
[];
100 static void __init
populate_kernel_ei_list(void)
102 populate_error_injection_list(__start_error_injection_whitelist
,
103 __stop_error_injection_whitelist
,
107 #ifdef CONFIG_MODULES
108 static void module_load_ei_list(struct module
*mod
)
110 if (!mod
->num_ei_funcs
)
113 populate_error_injection_list(mod
->ei_funcs
,
114 mod
->ei_funcs
+ mod
->num_ei_funcs
, mod
);
117 static void module_unload_ei_list(struct module
*mod
)
119 struct ei_entry
*ent
, *n
;
121 if (!mod
->num_ei_funcs
)
124 mutex_lock(&ei_mutex
);
125 list_for_each_entry_safe(ent
, n
, &error_injection_list
, list
) {
126 if (ent
->priv
== mod
) {
127 list_del_init(&ent
->list
);
131 mutex_unlock(&ei_mutex
);
134 /* Module notifier call back, checking error injection table on the module */
135 static int ei_module_callback(struct notifier_block
*nb
,
136 unsigned long val
, void *data
)
138 struct module
*mod
= data
;
140 if (val
== MODULE_STATE_COMING
)
141 module_load_ei_list(mod
);
142 else if (val
== MODULE_STATE_GOING
)
143 module_unload_ei_list(mod
);
148 static struct notifier_block ei_module_nb
= {
149 .notifier_call
= ei_module_callback
,
153 static __init
int module_ei_init(void)
155 return register_module_notifier(&ei_module_nb
);
157 #else /* !CONFIG_MODULES */
158 #define module_ei_init() (0)
162 * error_injection/whitelist -- shows which functions can be overridden for
165 static void *ei_seq_start(struct seq_file
*m
, loff_t
*pos
)
167 mutex_lock(&ei_mutex
);
168 return seq_list_start(&error_injection_list
, *pos
);
171 static void ei_seq_stop(struct seq_file
*m
, void *v
)
173 mutex_unlock(&ei_mutex
);
176 static void *ei_seq_next(struct seq_file
*m
, void *v
, loff_t
*pos
)
178 return seq_list_next(v
, &error_injection_list
, pos
);
181 static const char *error_type_string(int etype
)
188 case EI_ETYPE_ERRNO_NULL
:
197 static int ei_seq_show(struct seq_file
*m
, void *v
)
199 struct ei_entry
*ent
= list_entry(v
, struct ei_entry
, list
);
201 seq_printf(m
, "%ps\t%s\n", (void *)ent
->start_addr
,
202 error_type_string(ent
->etype
));
206 static const struct seq_operations ei_sops
= {
207 .start
= ei_seq_start
,
213 DEFINE_SEQ_ATTRIBUTE(ei
);
215 static int __init
ei_debugfs_init(void)
217 struct dentry
*dir
, *file
;
219 dir
= debugfs_create_dir("error_injection", NULL
);
221 file
= debugfs_create_file("list", 0444, dir
, NULL
, &ei_fops
);
230 static int __init
init_error_injection(void)
232 populate_kernel_ei_list();
234 if (!module_ei_init())
239 late_initcall(init_error_injection
);