Linux Audio

Check our new training course

Loading...
v5.14.15
  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
 12/* Whitelist of symbols that can be overridden for error injection. */
 13static LIST_HEAD(error_injection_list);
 14static DEFINE_MUTEX(ei_mutex);
 15struct ei_entry {
 16	struct list_head list;
 17	unsigned long start_addr;
 18	unsigned long end_addr;
 19	int etype;
 20	void *priv;
 21};
 22
 23bool within_error_injection_list(unsigned long addr)
 24{
 25	struct ei_entry *ent;
 26	bool ret = false;
 27
 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) {
 31			ret = true;
 32			break;
 33		}
 34	}
 35	mutex_unlock(&ei_mutex);
 36	return ret;
 37}
 38
 39int get_injectable_error_type(unsigned long addr)
 40{
 41	struct ei_entry *ent;
 
 42
 
 43	list_for_each_entry(ent, &error_injection_list, list) {
 44		if (addr >= ent->start_addr && addr < ent->end_addr)
 45			return ent->etype;
 
 
 46	}
 47	return EI_ETYPE_NONE;
 
 
 48}
 49
 50/*
 51 * Lookup and populate the error_injection_list.
 52 *
 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.
 56 */
 57static void populate_error_injection_list(struct error_injection_entry *start,
 58					  struct error_injection_entry *end,
 59					  void *priv)
 60{
 61	struct error_injection_entry *iter;
 62	struct ei_entry *ent;
 63	unsigned long entry, offset = 0, size = 0;
 64
 65	mutex_lock(&ei_mutex);
 66	for (iter = start; iter < end; iter++) {
 67		entry = arch_deref_entry_point((void *)iter->addr);
 68
 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",
 72				(void *)entry);
 73			continue;
 74		}
 75
 76		ent = kmalloc(sizeof(*ent), GFP_KERNEL);
 77		if (!ent)
 78			break;
 79		ent->start_addr = entry;
 80		ent->end_addr = entry + size;
 81		ent->etype = iter->etype;
 82		ent->priv = priv;
 83		INIT_LIST_HEAD(&ent->list);
 84		list_add_tail(&ent->list, &error_injection_list);
 85	}
 86	mutex_unlock(&ei_mutex);
 87}
 88
 89/* Markers of the _error_inject_whitelist section */
 90extern struct error_injection_entry __start_error_injection_whitelist[];
 91extern struct error_injection_entry __stop_error_injection_whitelist[];
 92
 93static void __init populate_kernel_ei_list(void)
 94{
 95	populate_error_injection_list(__start_error_injection_whitelist,
 96				      __stop_error_injection_whitelist,
 97				      NULL);
 98}
 99
100#ifdef CONFIG_MODULES
101static void module_load_ei_list(struct module *mod)
102{
103	if (!mod->num_ei_funcs)
104		return;
105
106	populate_error_injection_list(mod->ei_funcs,
107				      mod->ei_funcs + mod->num_ei_funcs, mod);
108}
109
110static void module_unload_ei_list(struct module *mod)
111{
112	struct ei_entry *ent, *n;
113
114	if (!mod->num_ei_funcs)
115		return;
116
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);
121			kfree(ent);
122		}
123	}
124	mutex_unlock(&ei_mutex);
125}
126
127/* Module notifier call back, checking error injection table on the module */
128static int ei_module_callback(struct notifier_block *nb,
129			      unsigned long val, void *data)
130{
131	struct module *mod = data;
132
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);
137
138	return NOTIFY_DONE;
139}
140
141static struct notifier_block ei_module_nb = {
142	.notifier_call = ei_module_callback,
143	.priority = 0
144};
145
146static __init int module_ei_init(void)
147{
148	return register_module_notifier(&ei_module_nb);
149}
150#else /* !CONFIG_MODULES */
151#define module_ei_init()	(0)
152#endif
153
154/*
155 * error_injection/whitelist -- shows which functions can be overridden for
156 * error injection.
157 */
158static void *ei_seq_start(struct seq_file *m, loff_t *pos)
159{
160	mutex_lock(&ei_mutex);
161	return seq_list_start(&error_injection_list, *pos);
162}
163
164static void ei_seq_stop(struct seq_file *m, void *v)
165{
166	mutex_unlock(&ei_mutex);
167}
168
169static void *ei_seq_next(struct seq_file *m, void *v, loff_t *pos)
170{
171	return seq_list_next(v, &error_injection_list, pos);
172}
173
174static const char *error_type_string(int etype)
175{
176	switch (etype) {
177	case EI_ETYPE_NULL:
178		return "NULL";
179	case EI_ETYPE_ERRNO:
180		return "ERRNO";
181	case EI_ETYPE_ERRNO_NULL:
182		return "ERRNO_NULL";
183	case EI_ETYPE_TRUE:
184		return "TRUE";
185	default:
186		return "(unknown)";
187	}
188}
189
190static int ei_seq_show(struct seq_file *m, void *v)
191{
192	struct ei_entry *ent = list_entry(v, struct ei_entry, list);
193
194	seq_printf(m, "%ps\t%s\n", (void *)ent->start_addr,
195		   error_type_string(ent->etype));
196	return 0;
197}
198
199static const struct seq_operations ei_seq_ops = {
200	.start = ei_seq_start,
201	.next  = ei_seq_next,
202	.stop  = ei_seq_stop,
203	.show  = ei_seq_show,
204};
205
206static int ei_open(struct inode *inode, struct file *filp)
207{
208	return seq_open(filp, &ei_seq_ops);
209}
210
211static const struct file_operations debugfs_ei_ops = {
212	.open           = ei_open,
213	.read           = seq_read,
214	.llseek         = seq_lseek,
215	.release        = seq_release,
216};
217
218static int __init ei_debugfs_init(void)
219{
220	struct dentry *dir, *file;
221
222	dir = debugfs_create_dir("error_injection", NULL);
223	if (!dir)
224		return -ENOMEM;
225
226	file = debugfs_create_file("list", 0444, dir, NULL, &debugfs_ei_ops);
227	if (!file) {
228		debugfs_remove(dir);
229		return -ENOMEM;
230	}
231
232	return 0;
233}
234
235static int __init init_error_injection(void)
236{
237	populate_kernel_ei_list();
238
239	if (!module_ei_init())
240		ei_debugfs_init();
241
242	return 0;
243}
244late_initcall(init_error_injection);
v6.13.7
  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>
 12
 13/* Whitelist of symbols that can be overridden for error injection. */
 14static LIST_HEAD(error_injection_list);
 15static DEFINE_MUTEX(ei_mutex);
 16struct ei_entry {
 17	struct list_head list;
 18	unsigned long start_addr;
 19	unsigned long end_addr;
 20	int etype;
 21	void *priv;
 22};
 23
 24bool within_error_injection_list(unsigned long addr)
 25{
 26	struct ei_entry *ent;
 27	bool ret = false;
 28
 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) {
 32			ret = true;
 33			break;
 34		}
 35	}
 36	mutex_unlock(&ei_mutex);
 37	return ret;
 38}
 39
 40int get_injectable_error_type(unsigned long addr)
 41{
 42	struct ei_entry *ent;
 43	int ei_type = -EINVAL;
 44
 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) {
 48			ei_type = ent->etype;
 49			break;
 50		}
 51	}
 52	mutex_unlock(&ei_mutex);
 53
 54	return ei_type;
 55}
 56
 57/*
 58 * Lookup and populate the error_injection_list.
 59 *
 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.
 63 */
 64static void populate_error_injection_list(struct error_injection_entry *start,
 65					  struct error_injection_entry *end,
 66					  void *priv)
 67{
 68	struct error_injection_entry *iter;
 69	struct ei_entry *ent;
 70	unsigned long entry, offset = 0, size = 0;
 71
 72	mutex_lock(&ei_mutex);
 73	for (iter = start; iter < end; iter++) {
 74		entry = (unsigned long)dereference_symbol_descriptor((void *)iter->addr);
 75
 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",
 79				(void *)entry);
 80			continue;
 81		}
 82
 83		ent = kmalloc(sizeof(*ent), GFP_KERNEL);
 84		if (!ent)
 85			break;
 86		ent->start_addr = entry;
 87		ent->end_addr = entry + size;
 88		ent->etype = iter->etype;
 89		ent->priv = priv;
 90		INIT_LIST_HEAD(&ent->list);
 91		list_add_tail(&ent->list, &error_injection_list);
 92	}
 93	mutex_unlock(&ei_mutex);
 94}
 95
 96/* Markers of the _error_inject_whitelist section */
 97extern struct error_injection_entry __start_error_injection_whitelist[];
 98extern struct error_injection_entry __stop_error_injection_whitelist[];
 99
100static void __init populate_kernel_ei_list(void)
101{
102	populate_error_injection_list(__start_error_injection_whitelist,
103				      __stop_error_injection_whitelist,
104				      NULL);
105}
106
107#ifdef CONFIG_MODULES
108static void module_load_ei_list(struct module *mod)
109{
110	if (!mod->num_ei_funcs)
111		return;
112
113	populate_error_injection_list(mod->ei_funcs,
114				      mod->ei_funcs + mod->num_ei_funcs, mod);
115}
116
117static void module_unload_ei_list(struct module *mod)
118{
119	struct ei_entry *ent, *n;
120
121	if (!mod->num_ei_funcs)
122		return;
123
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);
128			kfree(ent);
129		}
130	}
131	mutex_unlock(&ei_mutex);
132}
133
134/* Module notifier call back, checking error injection table on the module */
135static int ei_module_callback(struct notifier_block *nb,
136			      unsigned long val, void *data)
137{
138	struct module *mod = data;
139
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);
144
145	return NOTIFY_DONE;
146}
147
148static struct notifier_block ei_module_nb = {
149	.notifier_call = ei_module_callback,
150	.priority = 0
151};
152
153static __init int module_ei_init(void)
154{
155	return register_module_notifier(&ei_module_nb);
156}
157#else /* !CONFIG_MODULES */
158#define module_ei_init()	(0)
159#endif
160
161/*
162 * error_injection/whitelist -- shows which functions can be overridden for
163 * error injection.
164 */
165static void *ei_seq_start(struct seq_file *m, loff_t *pos)
166{
167	mutex_lock(&ei_mutex);
168	return seq_list_start(&error_injection_list, *pos);
169}
170
171static void ei_seq_stop(struct seq_file *m, void *v)
172{
173	mutex_unlock(&ei_mutex);
174}
175
176static void *ei_seq_next(struct seq_file *m, void *v, loff_t *pos)
177{
178	return seq_list_next(v, &error_injection_list, pos);
179}
180
181static const char *error_type_string(int etype)
182{
183	switch (etype) {
184	case EI_ETYPE_NULL:
185		return "NULL";
186	case EI_ETYPE_ERRNO:
187		return "ERRNO";
188	case EI_ETYPE_ERRNO_NULL:
189		return "ERRNO_NULL";
190	case EI_ETYPE_TRUE:
191		return "TRUE";
192	default:
193		return "(unknown)";
194	}
195}
196
197static int ei_seq_show(struct seq_file *m, void *v)
198{
199	struct ei_entry *ent = list_entry(v, struct ei_entry, list);
200
201	seq_printf(m, "%ps\t%s\n", (void *)ent->start_addr,
202		   error_type_string(ent->etype));
203	return 0;
204}
205
206static const struct seq_operations ei_sops = {
207	.start = ei_seq_start,
208	.next  = ei_seq_next,
209	.stop  = ei_seq_stop,
210	.show  = ei_seq_show,
211};
212
213DEFINE_SEQ_ATTRIBUTE(ei);
 
 
 
 
 
 
 
 
 
 
214
215static int __init ei_debugfs_init(void)
216{
217	struct dentry *dir, *file;
218
219	dir = debugfs_create_dir("error_injection", NULL);
 
 
220
221	file = debugfs_create_file("list", 0444, dir, NULL, &ei_fops);
222	if (!file) {
223		debugfs_remove(dir);
224		return -ENOMEM;
225	}
226
227	return 0;
228}
229
230static int __init init_error_injection(void)
231{
232	populate_kernel_ei_list();
233
234	if (!module_ei_init())
235		ei_debugfs_init();
236
237	return 0;
238}
239late_initcall(init_error_injection);