Thomas Gleixner | 457c899 | 2019-05-19 13:08:55 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/init.h> |
| 4 | #include <linux/random.h> |
Alexey Dobriyan | d43c36d | 2009-10-07 17:09:06 +0400 | [diff] [blame] | 5 | #include <linux/sched.h> |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 6 | #include <linux/stat.h> |
| 7 | #include <linux/types.h> |
| 8 | #include <linux/fs.h> |
Paul Gortmaker | 8bc3bcc | 2011-11-16 21:29:17 -0500 | [diff] [blame] | 9 | #include <linux/export.h> |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 10 | #include <linux/interrupt.h> |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 11 | #include <linux/stacktrace.h> |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 12 | #include <linux/fault-inject.h> |
| 13 | |
| 14 | /* |
| 15 | * setup_fault_attr() is a helper function for various __setup handlers, so it |
| 16 | * returns 0 on error, because that is what __setup handlers do. |
| 17 | */ |
Per Forlin | 6cba00e | 2011-09-13 23:03:28 +0200 | [diff] [blame] | 18 | int setup_fault_attr(struct fault_attr *attr, char *str) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 19 | { |
| 20 | unsigned long probability; |
| 21 | unsigned long interval; |
| 22 | int times; |
| 23 | int space; |
| 24 | |
| 25 | /* "<interval>,<probability>,<space>,<times>" */ |
| 26 | if (sscanf(str, "%lu,%lu,%d,%d", |
| 27 | &interval, &probability, &space, ×) < 4) { |
| 28 | printk(KERN_WARNING |
| 29 | "FAULT_INJECTION: failed to parse arguments\n"); |
| 30 | return 0; |
| 31 | } |
| 32 | |
| 33 | attr->probability = probability; |
| 34 | attr->interval = interval; |
| 35 | atomic_set(&attr->times, times); |
| 36 | atomic_set(&attr->space, space); |
| 37 | |
| 38 | return 1; |
| 39 | } |
Per Forlin | 6cba00e | 2011-09-13 23:03:28 +0200 | [diff] [blame] | 40 | EXPORT_SYMBOL_GPL(setup_fault_attr); |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 41 | |
| 42 | static void fail_dump(struct fault_attr *attr) |
| 43 | { |
Dmitry Monakhov | 6adc4a2 | 2014-12-12 16:58:00 -0800 | [diff] [blame] | 44 | if (attr->verbose > 0 && __ratelimit(&attr->ratelimit_state)) { |
| 45 | printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure.\n" |
| 46 | "name %pd, interval %lu, probability %lu, " |
| 47 | "space %d, times %d\n", attr->dname, |
Florian Westphal | bb38700 | 2015-10-22 13:32:27 -0700 | [diff] [blame] | 48 | attr->interval, attr->probability, |
Dmitry Monakhov | 6adc4a2 | 2014-12-12 16:58:00 -0800 | [diff] [blame] | 49 | atomic_read(&attr->space), |
| 50 | atomic_read(&attr->times)); |
| 51 | if (attr->verbose > 1) |
| 52 | dump_stack(); |
| 53 | } |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0) |
| 57 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 58 | static bool fail_task(struct fault_attr *attr, struct task_struct *task) |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 59 | { |
Dmitry Vyukov | f2ad37d | 2017-05-08 15:56:51 -0700 | [diff] [blame] | 60 | return in_task() && task->make_it_fail; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 61 | } |
| 62 | |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 63 | #define MAX_STACK_TRACE_DEPTH 32 |
| 64 | |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 65 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 66 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 67 | static bool fail_stacktrace(struct fault_attr *attr) |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 68 | { |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 69 | int depth = attr->stacktrace_depth; |
| 70 | unsigned long entries[MAX_STACK_TRACE_DEPTH]; |
Thomas Gleixner | 3019125 | 2019-04-25 11:45:04 +0200 | [diff] [blame] | 71 | int n, nr_entries; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 72 | bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX); |
| 73 | |
| 74 | if (depth == 0) |
| 75 | return found; |
| 76 | |
Thomas Gleixner | 3019125 | 2019-04-25 11:45:04 +0200 | [diff] [blame] | 77 | nr_entries = stack_trace_save(entries, depth, 1); |
| 78 | for (n = 0; n < nr_entries; n++) { |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 79 | if (attr->reject_start <= entries[n] && |
| 80 | entries[n] < attr->reject_end) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 81 | return false; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 82 | if (attr->require_start <= entries[n] && |
| 83 | entries[n] < attr->require_end) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 84 | found = true; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 85 | } |
| 86 | return found; |
| 87 | } |
| 88 | |
| 89 | #else |
| 90 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 91 | static inline bool fail_stacktrace(struct fault_attr *attr) |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 92 | { |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 93 | return true; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 96 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 97 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 98 | /* |
| 99 | * This code is stolen from failmalloc-1.0 |
| 100 | * http://www.nongnu.org/failmalloc/ |
| 101 | */ |
| 102 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 103 | bool should_fail(struct fault_attr *attr, ssize_t size) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 104 | { |
Akinobu Mita | 1203c8e | 2017-07-14 14:49:57 -0700 | [diff] [blame] | 105 | if (in_task()) { |
| 106 | unsigned int fail_nth = READ_ONCE(current->fail_nth); |
| 107 | |
Akinobu Mita | 9eeb52a | 2017-08-10 15:23:53 -0700 | [diff] [blame] | 108 | if (fail_nth) { |
Will Deacon | 9b4fb5c | 2019-12-19 17:40:21 +0000 | [diff] [blame] | 109 | fail_nth--; |
| 110 | WRITE_ONCE(current->fail_nth, fail_nth); |
| 111 | if (!fail_nth) |
Akinobu Mita | 9eeb52a | 2017-08-10 15:23:53 -0700 | [diff] [blame] | 112 | goto fail; |
Akinobu Mita | 1203c8e | 2017-07-14 14:49:57 -0700 | [diff] [blame] | 113 | |
Akinobu Mita | 9eeb52a | 2017-08-10 15:23:53 -0700 | [diff] [blame] | 114 | return false; |
| 115 | } |
Dmitry Vyukov | e41d5818 | 2017-07-12 14:34:35 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Anton Blanchard | f39cdae | 2012-06-20 12:53:03 -0700 | [diff] [blame] | 118 | /* No need to check any other properties if the probability is 0 */ |
| 119 | if (attr->probability == 0) |
| 120 | return false; |
| 121 | |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 122 | if (attr->task_filter && !fail_task(attr, current)) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 123 | return false; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 124 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 125 | if (atomic_read(&attr->times) == 0) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 126 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 127 | |
| 128 | if (atomic_read(&attr->space) > size) { |
| 129 | atomic_sub(size, &attr->space); |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 130 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | if (attr->interval > 1) { |
| 134 | attr->count++; |
| 135 | if (attr->count % attr->interval) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 136 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 137 | } |
| 138 | |
Akinobu Mita | f39fee5 | 2013-04-29 16:21:28 -0700 | [diff] [blame] | 139 | if (attr->probability <= prandom_u32() % 100) |
Don Mullis | f1729c28 | 2006-12-08 02:39:53 -0800 | [diff] [blame] | 140 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 141 | |
Don Mullis | f1729c28 | 2006-12-08 02:39:53 -0800 | [diff] [blame] | 142 | if (!fail_stacktrace(attr)) |
| 143 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 144 | |
Dmitry Vyukov | e41d5818 | 2017-07-12 14:34:35 -0700 | [diff] [blame] | 145 | fail: |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 146 | fail_dump(attr); |
| 147 | |
| 148 | if (atomic_read(&attr->times) != -1) |
| 149 | atomic_dec_not_zero(&attr->times); |
| 150 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 151 | return true; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 152 | } |
Per Forlin | df87ecb | 2011-08-19 14:52:36 +0200 | [diff] [blame] | 153 | EXPORT_SYMBOL_GPL(should_fail); |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 154 | |
| 155 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS |
| 156 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 157 | static int debugfs_ul_set(void *data, u64 val) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 158 | { |
| 159 | *(unsigned long *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 160 | return 0; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 161 | } |
| 162 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 163 | static int debugfs_ul_get(void *data, u64 *val) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 164 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 165 | *val = *(unsigned long *)data; |
| 166 | return 0; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n"); |
| 170 | |
Greg Kroah-Hartman | 053cf51 | 2019-06-12 11:58:28 +0200 | [diff] [blame] | 171 | static void debugfs_create_ul(const char *name, umode_t mode, |
| 172 | struct dentry *parent, unsigned long *value) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 173 | { |
Greg Kroah-Hartman | 053cf51 | 2019-06-12 11:58:28 +0200 | [diff] [blame] | 174 | debugfs_create_file(name, mode, parent, value, &fops_ul); |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 175 | } |
| 176 | |
Stephen Rothwell | 6ddb23c | 2007-07-23 18:43:55 -0700 | [diff] [blame] | 177 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 178 | |
Akinobu Mita | 8307fc2 | 2011-07-26 16:09:01 -0700 | [diff] [blame] | 179 | static int debugfs_stacktrace_depth_set(void *data, u64 val) |
| 180 | { |
| 181 | *(unsigned long *)data = |
| 182 | min_t(unsigned long, val, MAX_STACK_TRACE_DEPTH); |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get, |
| 188 | debugfs_stacktrace_depth_set, "%llu\n"); |
| 189 | |
Greg Kroah-Hartman | 053cf51 | 2019-06-12 11:58:28 +0200 | [diff] [blame] | 190 | static void debugfs_create_stacktrace_depth(const char *name, umode_t mode, |
| 191 | struct dentry *parent, |
| 192 | unsigned long *value) |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 193 | { |
Greg Kroah-Hartman | 053cf51 | 2019-06-12 11:58:28 +0200 | [diff] [blame] | 194 | debugfs_create_file(name, mode, parent, value, &fops_stacktrace_depth); |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 195 | } |
Akinobu Mita | 8307fc2 | 2011-07-26 16:09:01 -0700 | [diff] [blame] | 196 | |
Stephen Rothwell | 6ddb23c | 2007-07-23 18:43:55 -0700 | [diff] [blame] | 197 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 198 | |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 199 | struct dentry *fault_create_debugfs_attr(const char *name, |
| 200 | struct dentry *parent, struct fault_attr *attr) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 201 | { |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 202 | umode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 203 | struct dentry *dir; |
| 204 | |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 205 | dir = debugfs_create_dir(name, parent); |
Greg Kroah-Hartman | 053cf51 | 2019-06-12 11:58:28 +0200 | [diff] [blame] | 206 | if (IS_ERR(dir)) |
| 207 | return dir; |
Akinobu Mita | 7f5ddcc | 2011-07-26 16:09:02 -0700 | [diff] [blame] | 208 | |
Greg Kroah-Hartman | 053cf51 | 2019-06-12 11:58:28 +0200 | [diff] [blame] | 209 | debugfs_create_ul("probability", mode, dir, &attr->probability); |
| 210 | debugfs_create_ul("interval", mode, dir, &attr->interval); |
| 211 | debugfs_create_atomic_t("times", mode, dir, &attr->times); |
| 212 | debugfs_create_atomic_t("space", mode, dir, &attr->space); |
| 213 | debugfs_create_ul("verbose", mode, dir, &attr->verbose); |
| 214 | debugfs_create_u32("verbose_ratelimit_interval_ms", mode, dir, |
| 215 | &attr->ratelimit_state.interval); |
| 216 | debugfs_create_u32("verbose_ratelimit_burst", mode, dir, |
| 217 | &attr->ratelimit_state.burst); |
| 218 | debugfs_create_bool("task-filter", mode, dir, &attr->task_filter); |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 219 | |
| 220 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER |
Greg Kroah-Hartman | 053cf51 | 2019-06-12 11:58:28 +0200 | [diff] [blame] | 221 | debugfs_create_stacktrace_depth("stacktrace-depth", mode, dir, |
| 222 | &attr->stacktrace_depth); |
| 223 | debugfs_create_ul("require-start", mode, dir, &attr->require_start); |
| 224 | debugfs_create_ul("require-end", mode, dir, &attr->require_end); |
| 225 | debugfs_create_ul("reject-start", mode, dir, &attr->reject_start); |
| 226 | debugfs_create_ul("reject-end", mode, dir, &attr->reject_end); |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 227 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
| 228 | |
Dmitry Monakhov | 6adc4a2 | 2014-12-12 16:58:00 -0800 | [diff] [blame] | 229 | attr->dname = dget(dir); |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 230 | return dir; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 231 | } |
Per Forlin | df87ecb | 2011-08-19 14:52:36 +0200 | [diff] [blame] | 232 | EXPORT_SYMBOL_GPL(fault_create_debugfs_attr); |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 233 | |
| 234 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ |