Loading...
1#include <linux/kernel.h>
2#include <linux/init.h>
3#include <linux/random.h>
4#include <linux/sched.h>
5#include <linux/stat.h>
6#include <linux/types.h>
7#include <linux/fs.h>
8#include <linux/export.h>
9#include <linux/interrupt.h>
10#include <linux/stacktrace.h>
11#include <linux/fault-inject.h>
12
13/*
14 * setup_fault_attr() is a helper function for various __setup handlers, so it
15 * returns 0 on error, because that is what __setup handlers do.
16 */
17int setup_fault_attr(struct fault_attr *attr, char *str)
18{
19 unsigned long probability;
20 unsigned long interval;
21 int times;
22 int space;
23
24 /* "<interval>,<probability>,<space>,<times>" */
25 if (sscanf(str, "%lu,%lu,%d,%d",
26 &interval, &probability, &space, ×) < 4) {
27 printk(KERN_WARNING
28 "FAULT_INJECTION: failed to parse arguments\n");
29 return 0;
30 }
31
32 attr->probability = probability;
33 attr->interval = interval;
34 atomic_set(&attr->times, times);
35 atomic_set(&attr->space, space);
36
37 return 1;
38}
39EXPORT_SYMBOL_GPL(setup_fault_attr);
40
41static void fail_dump(struct fault_attr *attr)
42{
43 if (attr->verbose > 0)
44 printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure\n");
45 if (attr->verbose > 1)
46 dump_stack();
47}
48
49#define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
50
51static bool fail_task(struct fault_attr *attr, struct task_struct *task)
52{
53 return !in_interrupt() && task->make_it_fail;
54}
55
56#define MAX_STACK_TRACE_DEPTH 32
57
58#ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
59
60static bool fail_stacktrace(struct fault_attr *attr)
61{
62 struct stack_trace trace;
63 int depth = attr->stacktrace_depth;
64 unsigned long entries[MAX_STACK_TRACE_DEPTH];
65 int n;
66 bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX);
67
68 if (depth == 0)
69 return found;
70
71 trace.nr_entries = 0;
72 trace.entries = entries;
73 trace.max_entries = depth;
74 trace.skip = 1;
75
76 save_stack_trace(&trace);
77 for (n = 0; n < trace.nr_entries; n++) {
78 if (attr->reject_start <= entries[n] &&
79 entries[n] < attr->reject_end)
80 return false;
81 if (attr->require_start <= entries[n] &&
82 entries[n] < attr->require_end)
83 found = true;
84 }
85 return found;
86}
87
88#else
89
90static inline bool fail_stacktrace(struct fault_attr *attr)
91{
92 return true;
93}
94
95#endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
96
97/*
98 * This code is stolen from failmalloc-1.0
99 * http://www.nongnu.org/failmalloc/
100 */
101
102bool should_fail(struct fault_attr *attr, ssize_t size)
103{
104 /* No need to check any other properties if the probability is 0 */
105 if (attr->probability == 0)
106 return false;
107
108 if (attr->task_filter && !fail_task(attr, current))
109 return false;
110
111 if (atomic_read(&attr->times) == 0)
112 return false;
113
114 if (atomic_read(&attr->space) > size) {
115 atomic_sub(size, &attr->space);
116 return false;
117 }
118
119 if (attr->interval > 1) {
120 attr->count++;
121 if (attr->count % attr->interval)
122 return false;
123 }
124
125 if (attr->probability <= prandom_u32() % 100)
126 return false;
127
128 if (!fail_stacktrace(attr))
129 return false;
130
131 fail_dump(attr);
132
133 if (atomic_read(&attr->times) != -1)
134 atomic_dec_not_zero(&attr->times);
135
136 return true;
137}
138EXPORT_SYMBOL_GPL(should_fail);
139
140#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
141
142static int debugfs_ul_set(void *data, u64 val)
143{
144 *(unsigned long *)data = val;
145 return 0;
146}
147
148static int debugfs_ul_get(void *data, u64 *val)
149{
150 *val = *(unsigned long *)data;
151 return 0;
152}
153
154DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
155
156static struct dentry *debugfs_create_ul(const char *name, umode_t mode,
157 struct dentry *parent, unsigned long *value)
158{
159 return debugfs_create_file(name, mode, parent, value, &fops_ul);
160}
161
162#ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
163
164static int debugfs_stacktrace_depth_set(void *data, u64 val)
165{
166 *(unsigned long *)data =
167 min_t(unsigned long, val, MAX_STACK_TRACE_DEPTH);
168
169 return 0;
170}
171
172DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get,
173 debugfs_stacktrace_depth_set, "%llu\n");
174
175static struct dentry *debugfs_create_stacktrace_depth(
176 const char *name, umode_t mode,
177 struct dentry *parent, unsigned long *value)
178{
179 return debugfs_create_file(name, mode, parent, value,
180 &fops_stacktrace_depth);
181}
182
183#endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
184
185struct dentry *fault_create_debugfs_attr(const char *name,
186 struct dentry *parent, struct fault_attr *attr)
187{
188 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
189 struct dentry *dir;
190
191 dir = debugfs_create_dir(name, parent);
192 if (!dir)
193 return ERR_PTR(-ENOMEM);
194
195 if (!debugfs_create_ul("probability", mode, dir, &attr->probability))
196 goto fail;
197 if (!debugfs_create_ul("interval", mode, dir, &attr->interval))
198 goto fail;
199 if (!debugfs_create_atomic_t("times", mode, dir, &attr->times))
200 goto fail;
201 if (!debugfs_create_atomic_t("space", mode, dir, &attr->space))
202 goto fail;
203 if (!debugfs_create_ul("verbose", mode, dir, &attr->verbose))
204 goto fail;
205 if (!debugfs_create_bool("task-filter", mode, dir, &attr->task_filter))
206 goto fail;
207
208#ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
209
210 if (!debugfs_create_stacktrace_depth("stacktrace-depth", mode, dir,
211 &attr->stacktrace_depth))
212 goto fail;
213 if (!debugfs_create_ul("require-start", mode, dir,
214 &attr->require_start))
215 goto fail;
216 if (!debugfs_create_ul("require-end", mode, dir, &attr->require_end))
217 goto fail;
218 if (!debugfs_create_ul("reject-start", mode, dir, &attr->reject_start))
219 goto fail;
220 if (!debugfs_create_ul("reject-end", mode, dir, &attr->reject_end))
221 goto fail;
222
223#endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
224
225 return dir;
226fail:
227 debugfs_remove_recursive(dir);
228
229 return ERR_PTR(-ENOMEM);
230}
231EXPORT_SYMBOL_GPL(fault_create_debugfs_attr);
232
233#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
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 */
19int 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, ×) < 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}
41EXPORT_SYMBOL_GPL(setup_fault_attr);
42
43static 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
59static 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
68static 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
92static 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
104bool 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
152fail:
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
162bool should_fail(struct fault_attr *attr, ssize_t size)
163{
164 return should_fail_ex(attr, size, 0);
165}
166EXPORT_SYMBOL_GPL(should_fail);
167
168#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
169
170static int debugfs_ul_set(void *data, u64 val)
171{
172 *(unsigned long *)data = val;
173 return 0;
174}
175
176static int debugfs_ul_get(void *data, u64 *val)
177{
178 *val = *(unsigned long *)data;
179 return 0;
180}
181
182DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
183
184static 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
192static 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
200DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get,
201 debugfs_stacktrace_depth_set, "%llu\n");
202
203static 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
212struct 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}
245EXPORT_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
253static ssize_t fault_uint_attr_show(unsigned int val, char *page)
254{
255 return snprintf(page, PAGE_SIZE, "%u\n", val);
256}
257
258static ssize_t fault_ulong_attr_show(unsigned long val, char *page)
259{
260 return snprintf(page, PAGE_SIZE, "%lu\n", val);
261}
262
263static ssize_t fault_bool_attr_show(bool val, char *page)
264{
265 return snprintf(page, PAGE_SIZE, "%u\n", val);
266}
267
268static 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
273static 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
286static 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
299static 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
312static 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) \
326static 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
334static 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) \
340static 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} \
344static 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} \
349CONFIGFS_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
354FAULT_CONFIGFS_ATTR(probability, ulong);
355FAULT_CONFIGFS_ATTR(interval, ulong);
356FAULT_CONFIGFS_ATTR(times, atomic_t);
357FAULT_CONFIGFS_ATTR(space, atomic_t);
358FAULT_CONFIGFS_ATTR(verbose, ulong);
359FAULT_CONFIGFS_ATTR_NAMED(ratelimit_interval, "verbose_ratelimit_interval_ms",
360 ratelimit_state.interval, uint);
361FAULT_CONFIGFS_ATTR_NAMED(ratelimit_burst, "verbose_ratelimit_burst",
362 ratelimit_state.burst, uint);
363FAULT_CONFIGFS_ATTR_NAMED(task_filter, "task-filter", task_filter, bool);
364
365#ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
366
367static 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
372static 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
388CONFIGFS_ATTR_NAMED(fault_, stacktrace_depth, "stacktrace-depth");
389
390static 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
396static 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
401FAULT_CONFIGFS_ATTR_NAMED(require_start, "require-start", require_start, xul);
402FAULT_CONFIGFS_ATTR_NAMED(require_end, "require-end", require_end, xul);
403FAULT_CONFIGFS_ATTR_NAMED(reject_start, "reject-start", reject_start, xul);
404FAULT_CONFIGFS_ATTR_NAMED(reject_end, "reject-end", reject_end, xul);
405
406#endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
407
408static 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
427static const struct config_item_type fault_config_type = {
428 .ct_attrs = fault_config_attrs,
429 .ct_owner = THIS_MODULE,
430};
431
432void fault_config_init(struct fault_config *config, const char *name)
433{
434 config_group_init_type_name(&config->group, name, &fault_config_type);
435}
436EXPORT_SYMBOL_GPL(fault_config_init);
437
438#endif /* CONFIG_FAULT_INJECTION_CONFIGFS */