Commit | Line | Data |
---|---|---|
457c8996 | 1 | // SPDX-License-Identifier: GPL-2.0-only |
6ff1cb35 AM |
2 | #include <linux/kernel.h> |
3 | #include <linux/init.h> | |
4 | #include <linux/random.h> | |
6ce2082f | 5 | #include <linux/debugfs.h> |
d43c36dc | 6 | #include <linux/sched.h> |
6ff1cb35 AM |
7 | #include <linux/stat.h> |
8 | #include <linux/types.h> | |
9 | #include <linux/fs.h> | |
8bc3bcc9 | 10 | #include <linux/export.h> |
f4f154fd | 11 | #include <linux/interrupt.h> |
329409ae | 12 | #include <linux/stacktrace.h> |
6ff1cb35 AM |
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 | */ | |
6cba00ea | 19 | int setup_fault_attr(struct fault_attr *attr, char *str) |
6ff1cb35 AM |
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 | } | |
6cba00ea | 41 | EXPORT_SYMBOL_GPL(setup_fault_attr); |
6ff1cb35 AM |
42 | |
43 | static void fail_dump(struct fault_attr *attr) | |
44 | { | |
6adc4a22 DM |
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, | |
bb387002 | 49 | attr->interval, attr->probability, |
6adc4a22 DM |
50 | atomic_read(&attr->space), |
51 | atomic_read(&attr->times)); | |
52 | if (attr->verbose > 1) | |
53 | dump_stack(); | |
54 | } | |
6ff1cb35 AM |
55 | } |
56 | ||
57 | #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0) | |
58 | ||
08b3df2d | 59 | static bool fail_task(struct fault_attr *attr, struct task_struct *task) |
f4f154fd | 60 | { |
f2ad37da | 61 | return in_task() && task->make_it_fail; |
f4f154fd AM |
62 | } |
63 | ||
a124c28e DM |
64 | #define MAX_STACK_TRACE_DEPTH 32 |
65 | ||
1df49008 | 66 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER |
329409ae | 67 | |
08b3df2d | 68 | static bool fail_stacktrace(struct fault_attr *attr) |
329409ae | 69 | { |
329409ae AM |
70 | int depth = attr->stacktrace_depth; |
71 | unsigned long entries[MAX_STACK_TRACE_DEPTH]; | |
30191250 | 72 | int n, nr_entries; |
329409ae AM |
73 | bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX); |
74 | ||
4acb9e51 | 75 | if (depth == 0 || (found && !attr->reject_start && !attr->reject_end)) |
329409ae AM |
76 | return found; |
77 | ||
30191250 TG |
78 | nr_entries = stack_trace_save(entries, depth, 1); |
79 | for (n = 0; n < nr_entries; n++) { | |
329409ae AM |
80 | if (attr->reject_start <= entries[n] && |
81 | entries[n] < attr->reject_end) | |
08b3df2d | 82 | return false; |
329409ae AM |
83 | if (attr->require_start <= entries[n] && |
84 | entries[n] < attr->require_end) | |
08b3df2d | 85 | found = true; |
329409ae AM |
86 | } |
87 | return found; | |
88 | } | |
89 | ||
90 | #else | |
91 | ||
08b3df2d | 92 | static inline bool fail_stacktrace(struct fault_attr *attr) |
329409ae | 93 | { |
1df49008 | 94 | return true; |
329409ae AM |
95 | } |
96 | ||
1df49008 | 97 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
329409ae | 98 | |
6ff1cb35 AM |
99 | /* |
100 | * This code is stolen from failmalloc-1.0 | |
101 | * http://www.nongnu.org/failmalloc/ | |
102 | */ | |
103 | ||
ea4452de | 104 | bool should_fail_ex(struct fault_attr *attr, ssize_t size, int flags) |
6ff1cb35 | 105 | { |
f9eeef59 WY |
106 | bool stack_checked = false; |
107 | ||
1203c8e6 AM |
108 | if (in_task()) { |
109 | unsigned int fail_nth = READ_ONCE(current->fail_nth); | |
110 | ||
9eeb52ae | 111 | if (fail_nth) { |
f9eeef59 WY |
112 | if (!fail_stacktrace(attr)) |
113 | return false; | |
114 | ||
115 | stack_checked = true; | |
9b4fb5ce WD |
116 | fail_nth--; |
117 | WRITE_ONCE(current->fail_nth, fail_nth); | |
118 | if (!fail_nth) | |
9eeb52ae | 119 | goto fail; |
1203c8e6 | 120 | |
9eeb52ae AM |
121 | return false; |
122 | } | |
e41d5818 DV |
123 | } |
124 | ||
f39cdaeb AB |
125 | /* No need to check any other properties if the probability is 0 */ |
126 | if (attr->probability == 0) | |
127 | return false; | |
128 | ||
f4f154fd | 129 | if (attr->task_filter && !fail_task(attr, current)) |
08b3df2d | 130 | return false; |
f4f154fd | 131 | |
6ff1cb35 | 132 | if (atomic_read(&attr->times) == 0) |
08b3df2d | 133 | return false; |
6ff1cb35 | 134 | |
f9eeef59 WY |
135 | if (!stack_checked && !fail_stacktrace(attr)) |
136 | return false; | |
137 | ||
6ff1cb35 AM |
138 | if (atomic_read(&attr->space) > size) { |
139 | atomic_sub(size, &attr->space); | |
08b3df2d | 140 | return false; |
6ff1cb35 AM |
141 | } |
142 | ||
143 | if (attr->interval > 1) { | |
144 | attr->count++; | |
145 | if (attr->count % attr->interval) | |
08b3df2d | 146 | return false; |
6ff1cb35 AM |
147 | } |
148 | ||
8032bf12 | 149 | if (attr->probability <= get_random_u32_below(100)) |
f1729c28 | 150 | return false; |
6ff1cb35 | 151 | |
e41d5818 | 152 | fail: |
ea4452de QZ |
153 | if (!(flags & FAULT_NOWARN)) |
154 | fail_dump(attr); | |
6ff1cb35 AM |
155 | |
156 | if (atomic_read(&attr->times) != -1) | |
157 | atomic_dec_not_zero(&attr->times); | |
158 | ||
08b3df2d | 159 | return true; |
6ff1cb35 | 160 | } |
ea4452de QZ |
161 | |
162 | bool should_fail(struct fault_attr *attr, ssize_t size) | |
163 | { | |
164 | return should_fail_ex(attr, size, 0); | |
165 | } | |
df87ecbf | 166 | EXPORT_SYMBOL_GPL(should_fail); |
6ff1cb35 AM |
167 | |
168 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS | |
169 | ||
8b88b099 | 170 | static int debugfs_ul_set(void *data, u64 val) |
6ff1cb35 AM |
171 | { |
172 | *(unsigned long *)data = val; | |
8b88b099 | 173 | return 0; |
6ff1cb35 AM |
174 | } |
175 | ||
8b88b099 | 176 | static int debugfs_ul_get(void *data, u64 *val) |
6ff1cb35 | 177 | { |
8b88b099 CH |
178 | *val = *(unsigned long *)data; |
179 | return 0; | |
6ff1cb35 AM |
180 | } |
181 | ||
182 | DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n"); | |
183 | ||
053cf510 GKH |
184 | static void debugfs_create_ul(const char *name, umode_t mode, |
185 | struct dentry *parent, unsigned long *value) | |
6ff1cb35 | 186 | { |
053cf510 | 187 | debugfs_create_file(name, mode, parent, value, &fops_ul); |
6ff1cb35 AM |
188 | } |
189 | ||
6ddb23c7 | 190 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER |
a124c28e | 191 | |
8307fc25 AM |
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 | ||
053cf510 GKH |
203 | static void debugfs_create_stacktrace_depth(const char *name, umode_t mode, |
204 | struct dentry *parent, | |
205 | unsigned long *value) | |
a124c28e | 206 | { |
053cf510 | 207 | debugfs_create_file(name, mode, parent, value, &fops_stacktrace_depth); |
a124c28e | 208 | } |
8307fc25 | 209 | |
6ddb23c7 | 210 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
a124c28e | 211 | |
dd48c085 AM |
212 | struct dentry *fault_create_debugfs_attr(const char *name, |
213 | struct dentry *parent, struct fault_attr *attr) | |
6ff1cb35 | 214 | { |
f4ae40a6 | 215 | umode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
6ff1cb35 AM |
216 | struct dentry *dir; |
217 | ||
dd48c085 | 218 | dir = debugfs_create_dir(name, parent); |
053cf510 GKH |
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); | |
1df49008 AM |
232 | |
233 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER | |
053cf510 GKH |
234 | debugfs_create_stacktrace_depth("stacktrace-depth", mode, dir, |
235 | &attr->stacktrace_depth); | |
01999074 WY |
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); | |
1df49008 AM |
240 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
241 | ||
6adc4a22 | 242 | attr->dname = dget(dir); |
dd48c085 | 243 | return dir; |
6ff1cb35 | 244 | } |
df87ecbf | 245 | EXPORT_SYMBOL_GPL(fault_create_debugfs_attr); |
6ff1cb35 AM |
246 | |
247 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ | |
4668c7a2 AM |
248 | |
249 | #ifdef CONFIG_FAULT_INJECTION_CONFIGFS | |
250 | ||
251 | /* These configfs attribute utilities are copied from drivers/block/null_blk/main.c */ | |
252 | ||
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 | ||
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 | ||
263 | static ssize_t fault_bool_attr_show(bool val, char *page) | |
264 | { | |
265 | return snprintf(page, PAGE_SIZE, "%u\n", val); | |
266 | } | |
267 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 | ||
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 */ |