xref: /linux/lib/fault-inject.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/random.h>
5 #include <linux/debugfs.h>
6 #include <linux/sched.h>
7 #include <linux/stat.h>
8 #include <linux/types.h>
9 #include <linux/fs.h>
10 #include <linux/export.h>
11 #include <linux/interrupt.h>
12 #include <linux/stacktrace.h>
13 #include <linux/fault-inject.h>
14 
15 /*
16  * setup_fault_attr() is a helper function for various __setup handlers, so it
17  * returns 0 on error, because that is what __setup handlers do.
18  */
setup_fault_attr(struct fault_attr * attr,char * str)19 int setup_fault_attr(struct fault_attr *attr, char *str)
20 {
21 	unsigned long probability;
22 	unsigned long interval;
23 	int times;
24 	int space;
25 
26 	/* "<interval>,<probability>,<space>,<times>" */
27 	if (sscanf(str, "%lu,%lu,%d,%d",
28 			&interval, &probability, &space, &times) < 4) {
29 		printk(KERN_WARNING
30 			"FAULT_INJECTION: failed to parse arguments\n");
31 		return 0;
32 	}
33 
34 	attr->probability = probability;
35 	attr->interval = interval;
36 	atomic_set(&attr->times, times);
37 	atomic_set(&attr->space, space);
38 
39 	return 1;
40 }
41 EXPORT_SYMBOL_GPL(setup_fault_attr);
42 
fail_dump(struct fault_attr * attr)43 static void fail_dump(struct fault_attr *attr)
44 {
45 	if (attr->verbose > 0 && __ratelimit(&attr->ratelimit_state)) {
46 		printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure.\n"
47 		       "name %pd, interval %lu, probability %lu, "
48 		       "space %d, times %d\n", attr->dname,
49 		       attr->interval, attr->probability,
50 		       atomic_read(&attr->space),
51 		       atomic_read(&attr->times));
52 		if (attr->verbose > 1)
53 			dump_stack();
54 	}
55 }
56 
57 #define atomic_dec_not_zero(v)		atomic_add_unless((v), -1, 0)
58 
fail_task(struct fault_attr * attr,struct task_struct * task)59 static bool fail_task(struct fault_attr *attr, struct task_struct *task)
60 {
61 	return in_task() && task->make_it_fail;
62 }
63 
64 #define MAX_STACK_TRACE_DEPTH 32
65 
66 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
67 
fail_stacktrace(struct fault_attr * attr)68 static bool fail_stacktrace(struct fault_attr *attr)
69 {
70 	int depth = attr->stacktrace_depth;
71 	unsigned long entries[MAX_STACK_TRACE_DEPTH];
72 	int n, nr_entries;
73 	bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX);
74 
75 	if (depth == 0 || (found && !attr->reject_start && !attr->reject_end))
76 		return found;
77 
78 	nr_entries = stack_trace_save(entries, depth, 1);
79 	for (n = 0; n < nr_entries; n++) {
80 		if (attr->reject_start <= entries[n] &&
81 			       entries[n] < attr->reject_end)
82 			return false;
83 		if (attr->require_start <= entries[n] &&
84 			       entries[n] < attr->require_end)
85 			found = true;
86 	}
87 	return found;
88 }
89 
90 #else
91 
fail_stacktrace(struct fault_attr * attr)92 static inline bool fail_stacktrace(struct fault_attr *attr)
93 {
94 	return true;
95 }
96 
97 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
98 
99 /*
100  * This code is stolen from failmalloc-1.0
101  * http://www.nongnu.org/failmalloc/
102  */
103 
should_fail_ex(struct fault_attr * attr,ssize_t size,int flags)104 bool should_fail_ex(struct fault_attr *attr, ssize_t size, int flags)
105 {
106 	bool stack_checked = false;
107 
108 	if (in_task()) {
109 		unsigned int fail_nth = READ_ONCE(current->fail_nth);
110 
111 		if (fail_nth) {
112 			if (!fail_stacktrace(attr))
113 				return false;
114 
115 			stack_checked = true;
116 			fail_nth--;
117 			WRITE_ONCE(current->fail_nth, fail_nth);
118 			if (!fail_nth)
119 				goto fail;
120 
121 			return false;
122 		}
123 	}
124 
125 	/* No need to check any other properties if the probability is 0 */
126 	if (attr->probability == 0)
127 		return false;
128 
129 	if (attr->task_filter && !fail_task(attr, current))
130 		return false;
131 
132 	if (atomic_read(&attr->times) == 0)
133 		return false;
134 
135 	if (!stack_checked && !fail_stacktrace(attr))
136 		return false;
137 
138 	if (atomic_read(&attr->space) > size) {
139 		atomic_sub(size, &attr->space);
140 		return false;
141 	}
142 
143 	if (attr->interval > 1) {
144 		attr->count++;
145 		if (attr->count % attr->interval)
146 			return false;
147 	}
148 
149 	if (attr->probability <= get_random_u32_below(100))
150 		return false;
151 
152 fail:
153 	if (!(flags & FAULT_NOWARN))
154 		fail_dump(attr);
155 
156 	if (atomic_read(&attr->times) != -1)
157 		atomic_dec_not_zero(&attr->times);
158 
159 	return true;
160 }
161 
should_fail(struct fault_attr * attr,ssize_t size)162 bool should_fail(struct fault_attr *attr, ssize_t size)
163 {
164 	return should_fail_ex(attr, size, 0);
165 }
166 EXPORT_SYMBOL_GPL(should_fail);
167 
168 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
169 
debugfs_ul_set(void * data,u64 val)170 static int debugfs_ul_set(void *data, u64 val)
171 {
172 	*(unsigned long *)data = val;
173 	return 0;
174 }
175 
debugfs_ul_get(void * data,u64 * val)176 static int debugfs_ul_get(void *data, u64 *val)
177 {
178 	*val = *(unsigned long *)data;
179 	return 0;
180 }
181 
182 DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
183 
debugfs_create_ul(const char * name,umode_t mode,struct dentry * parent,unsigned long * value)184 static void debugfs_create_ul(const char *name, umode_t mode,
185 			      struct dentry *parent, unsigned long *value)
186 {
187 	debugfs_create_file(name, mode, parent, value, &fops_ul);
188 }
189 
190 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
191 
debugfs_stacktrace_depth_set(void * data,u64 val)192 static int debugfs_stacktrace_depth_set(void *data, u64 val)
193 {
194 	*(unsigned long *)data =
195 		min_t(unsigned long, val, MAX_STACK_TRACE_DEPTH);
196 
197 	return 0;
198 }
199 
200 DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get,
201 			debugfs_stacktrace_depth_set, "%llu\n");
202 
debugfs_create_stacktrace_depth(const char * name,umode_t mode,struct dentry * parent,unsigned long * value)203 static void debugfs_create_stacktrace_depth(const char *name, umode_t mode,
204 					    struct dentry *parent,
205 					    unsigned long *value)
206 {
207 	debugfs_create_file(name, mode, parent, value, &fops_stacktrace_depth);
208 }
209 
210 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
211 
fault_create_debugfs_attr(const char * name,struct dentry * parent,struct fault_attr * attr)212 struct dentry *fault_create_debugfs_attr(const char *name,
213 			struct dentry *parent, struct fault_attr *attr)
214 {
215 	umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
216 	struct dentry *dir;
217 
218 	dir = debugfs_create_dir(name, parent);
219 	if (IS_ERR(dir))
220 		return dir;
221 
222 	debugfs_create_ul("probability", mode, dir, &attr->probability);
223 	debugfs_create_ul("interval", mode, dir, &attr->interval);
224 	debugfs_create_atomic_t("times", mode, dir, &attr->times);
225 	debugfs_create_atomic_t("space", mode, dir, &attr->space);
226 	debugfs_create_ul("verbose", mode, dir, &attr->verbose);
227 	debugfs_create_u32("verbose_ratelimit_interval_ms", mode, dir,
228 			   &attr->ratelimit_state.interval);
229 	debugfs_create_u32("verbose_ratelimit_burst", mode, dir,
230 			   &attr->ratelimit_state.burst);
231 	debugfs_create_bool("task-filter", mode, dir, &attr->task_filter);
232 
233 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
234 	debugfs_create_stacktrace_depth("stacktrace-depth", mode, dir,
235 					&attr->stacktrace_depth);
236 	debugfs_create_xul("require-start", mode, dir, &attr->require_start);
237 	debugfs_create_xul("require-end", mode, dir, &attr->require_end);
238 	debugfs_create_xul("reject-start", mode, dir, &attr->reject_start);
239 	debugfs_create_xul("reject-end", mode, dir, &attr->reject_end);
240 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
241 
242 	attr->dname = dget(dir);
243 	return dir;
244 }
245 EXPORT_SYMBOL_GPL(fault_create_debugfs_attr);
246 
247 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
248 
249 #ifdef CONFIG_FAULT_INJECTION_CONFIGFS
250 
251 /* These configfs attribute utilities are copied from drivers/block/null_blk/main.c */
252 
fault_uint_attr_show(unsigned int val,char * page)253 static ssize_t fault_uint_attr_show(unsigned int val, char *page)
254 {
255 	return snprintf(page, PAGE_SIZE, "%u\n", val);
256 }
257 
fault_ulong_attr_show(unsigned long val,char * page)258 static ssize_t fault_ulong_attr_show(unsigned long val, char *page)
259 {
260 	return snprintf(page, PAGE_SIZE, "%lu\n", val);
261 }
262 
fault_bool_attr_show(bool val,char * page)263 static ssize_t fault_bool_attr_show(bool val, char *page)
264 {
265 	return snprintf(page, PAGE_SIZE, "%u\n", val);
266 }
267 
fault_atomic_t_attr_show(atomic_t val,char * page)268 static ssize_t fault_atomic_t_attr_show(atomic_t val, char *page)
269 {
270 	return snprintf(page, PAGE_SIZE, "%d\n", atomic_read(&val));
271 }
272 
fault_uint_attr_store(unsigned int * val,const char * page,size_t count)273 static ssize_t fault_uint_attr_store(unsigned int *val, const char *page, size_t count)
274 {
275 	unsigned int tmp;
276 	int result;
277 
278 	result = kstrtouint(page, 0, &tmp);
279 	if (result < 0)
280 		return result;
281 
282 	*val = tmp;
283 	return count;
284 }
285 
fault_ulong_attr_store(unsigned long * val,const char * page,size_t count)286 static ssize_t fault_ulong_attr_store(unsigned long *val, const char *page, size_t count)
287 {
288 	int result;
289 	unsigned long tmp;
290 
291 	result = kstrtoul(page, 0, &tmp);
292 	if (result < 0)
293 		return result;
294 
295 	*val = tmp;
296 	return count;
297 }
298 
fault_bool_attr_store(bool * val,const char * page,size_t count)299 static ssize_t fault_bool_attr_store(bool *val, const char *page, size_t count)
300 {
301 	bool tmp;
302 	int result;
303 
304 	result = kstrtobool(page, &tmp);
305 	if (result < 0)
306 		return result;
307 
308 	*val = tmp;
309 	return count;
310 }
311 
fault_atomic_t_attr_store(atomic_t * val,const char * page,size_t count)312 static ssize_t fault_atomic_t_attr_store(atomic_t *val, const char *page, size_t count)
313 {
314 	int tmp;
315 	int result;
316 
317 	result = kstrtoint(page, 0, &tmp);
318 	if (result < 0)
319 		return result;
320 
321 	atomic_set(val, tmp);
322 	return count;
323 }
324 
325 #define CONFIGFS_ATTR_NAMED(_pfx, _name, _attr_name)	\
326 static struct configfs_attribute _pfx##attr_##_name = {	\
327 	.ca_name	= _attr_name,			\
328 	.ca_mode	= 0644,				\
329 	.ca_owner	= THIS_MODULE,			\
330 	.show		= _pfx##_name##_show,		\
331 	.store		= _pfx##_name##_store,		\
332 }
333 
to_fault_config(struct config_item * item)334 static struct fault_config *to_fault_config(struct config_item *item)
335 {
336 	return container_of(to_config_group(item), struct fault_config, group);
337 }
338 
339 #define FAULT_CONFIGFS_ATTR_NAMED(NAME, ATTR_NAME, MEMBER, TYPE)				\
340 static ssize_t fault_##NAME##_show(struct config_item *item, char *page)			\
341 {												\
342 	return fault_##TYPE##_attr_show(to_fault_config(item)->attr.MEMBER, page);		\
343 }												\
344 static ssize_t fault_##NAME##_store(struct config_item *item, const char *page, size_t count)	\
345 {												\
346 	struct fault_config *config = to_fault_config(item);					\
347 	return fault_##TYPE##_attr_store(&config->attr.MEMBER, page, count);			\
348 }												\
349 CONFIGFS_ATTR_NAMED(fault_, NAME, ATTR_NAME)
350 
351 #define FAULT_CONFIGFS_ATTR(NAME, TYPE)	\
352 	FAULT_CONFIGFS_ATTR_NAMED(NAME, __stringify(NAME), NAME, TYPE)
353 
354 FAULT_CONFIGFS_ATTR(probability, ulong);
355 FAULT_CONFIGFS_ATTR(interval, ulong);
356 FAULT_CONFIGFS_ATTR(times, atomic_t);
357 FAULT_CONFIGFS_ATTR(space, atomic_t);
358 FAULT_CONFIGFS_ATTR(verbose, ulong);
359 FAULT_CONFIGFS_ATTR_NAMED(ratelimit_interval, "verbose_ratelimit_interval_ms",
360 		ratelimit_state.interval, uint);
361 FAULT_CONFIGFS_ATTR_NAMED(ratelimit_burst, "verbose_ratelimit_burst",
362 		ratelimit_state.burst, uint);
363 FAULT_CONFIGFS_ATTR_NAMED(task_filter, "task-filter", task_filter, bool);
364 
365 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
366 
fault_stacktrace_depth_show(struct config_item * item,char * page)367 static ssize_t fault_stacktrace_depth_show(struct config_item *item, char *page)
368 {
369 	return fault_ulong_attr_show(to_fault_config(item)->attr.stacktrace_depth, page);
370 }
371 
fault_stacktrace_depth_store(struct config_item * item,const char * page,size_t count)372 static ssize_t fault_stacktrace_depth_store(struct config_item *item, const char *page,
373 		size_t count)
374 {
375 	int result;
376 	unsigned long tmp;
377 
378 	result = kstrtoul(page, 0, &tmp);
379 	if (result < 0)
380 		return result;
381 
382 	to_fault_config(item)->attr.stacktrace_depth =
383 		min_t(unsigned long, tmp, MAX_STACK_TRACE_DEPTH);
384 
385 	return count;
386 }
387 
388 CONFIGFS_ATTR_NAMED(fault_, stacktrace_depth, "stacktrace-depth");
389 
fault_xul_attr_show(unsigned long val,char * page)390 static ssize_t fault_xul_attr_show(unsigned long val, char *page)
391 {
392 	return snprintf(page, PAGE_SIZE,
393 			sizeof(val) == sizeof(u32) ? "0x%08lx\n" : "0x%016lx\n", val);
394 }
395 
fault_xul_attr_store(unsigned long * val,const char * page,size_t count)396 static ssize_t fault_xul_attr_store(unsigned long *val, const char *page, size_t count)
397 {
398 	return fault_ulong_attr_store(val, page, count);
399 }
400 
401 FAULT_CONFIGFS_ATTR_NAMED(require_start, "require-start", require_start, xul);
402 FAULT_CONFIGFS_ATTR_NAMED(require_end, "require-end", require_end, xul);
403 FAULT_CONFIGFS_ATTR_NAMED(reject_start, "reject-start", reject_start, xul);
404 FAULT_CONFIGFS_ATTR_NAMED(reject_end, "reject-end", reject_end, xul);
405 
406 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
407 
408 static struct configfs_attribute *fault_config_attrs[] = {
409 	&fault_attr_probability,
410 	&fault_attr_interval,
411 	&fault_attr_times,
412 	&fault_attr_space,
413 	&fault_attr_verbose,
414 	&fault_attr_ratelimit_interval,
415 	&fault_attr_ratelimit_burst,
416 	&fault_attr_task_filter,
417 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
418 	&fault_attr_stacktrace_depth,
419 	&fault_attr_require_start,
420 	&fault_attr_require_end,
421 	&fault_attr_reject_start,
422 	&fault_attr_reject_end,
423 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
424 	NULL,
425 };
426 
427 static const struct config_item_type fault_config_type = {
428 	.ct_attrs	= fault_config_attrs,
429 	.ct_owner	= THIS_MODULE,
430 };
431 
fault_config_init(struct fault_config * config,const char * name)432 void fault_config_init(struct fault_config *config, const char *name)
433 {
434 	config_group_init_type_name(&config->group, name, &fault_config_type);
435 }
436 EXPORT_SYMBOL_GPL(fault_config_init);
437 
438 #endif /* CONFIG_FAULT_INJECTION_CONFIGFS */
439