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