Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 1 | #include <linux/kernel.h> |
| 2 | #include <linux/init.h> |
| 3 | #include <linux/random.h> |
| 4 | #include <linux/stat.h> |
| 5 | #include <linux/types.h> |
| 6 | #include <linux/fs.h> |
| 7 | #include <linux/module.h> |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 8 | #include <linux/interrupt.h> |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 9 | #include <linux/unwind.h> |
| 10 | #include <linux/stacktrace.h> |
| 11 | #include <linux/kallsyms.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 | */ |
| 18 | int __init setup_fault_attr(struct fault_attr *attr, char *str) |
| 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 | } |
| 40 | |
| 41 | static 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 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 51 | static bool fail_task(struct fault_attr *attr, struct task_struct *task) |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 52 | { |
| 53 | return !in_interrupt() && task->make_it_fail; |
| 54 | } |
| 55 | |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 56 | #define MAX_STACK_TRACE_DEPTH 32 |
| 57 | |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 58 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 59 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 60 | static bool fail_stacktrace(struct fault_attr *attr) |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 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; |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 73 | trace.max_entries = depth; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 74 | trace.skip = 1; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 75 | |
Christoph Hellwig | ab1b6f0 | 2007-05-08 00:23:29 -0700 | [diff] [blame] | 76 | save_stack_trace(&trace); |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 77 | for (n = 0; n < trace.nr_entries; n++) { |
| 78 | if (attr->reject_start <= entries[n] && |
| 79 | entries[n] < attr->reject_end) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 80 | return false; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 81 | if (attr->require_start <= entries[n] && |
| 82 | entries[n] < attr->require_end) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 83 | found = true; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 84 | } |
| 85 | return found; |
| 86 | } |
| 87 | |
| 88 | #else |
| 89 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 90 | static inline bool fail_stacktrace(struct fault_attr *attr) |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 91 | { |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 92 | return true; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 93 | } |
| 94 | |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 95 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 96 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 97 | /* |
| 98 | * This code is stolen from failmalloc-1.0 |
| 99 | * http://www.nongnu.org/failmalloc/ |
| 100 | */ |
| 101 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 102 | bool should_fail(struct fault_attr *attr, ssize_t size) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 103 | { |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 104 | if (attr->task_filter && !fail_task(attr, current)) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 105 | return false; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 106 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 107 | if (atomic_read(&attr->times) == 0) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 108 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 109 | |
| 110 | if (atomic_read(&attr->space) > size) { |
| 111 | atomic_sub(size, &attr->space); |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 112 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | if (attr->interval > 1) { |
| 116 | attr->count++; |
| 117 | if (attr->count % attr->interval) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 118 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Don Mullis | f1729c2 | 2006-12-08 02:39:53 -0800 | [diff] [blame] | 121 | if (attr->probability <= random32() % 100) |
| 122 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 123 | |
Don Mullis | f1729c2 | 2006-12-08 02:39:53 -0800 | [diff] [blame] | 124 | if (!fail_stacktrace(attr)) |
| 125 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 126 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 127 | fail_dump(attr); |
| 128 | |
| 129 | if (atomic_read(&attr->times) != -1) |
| 130 | atomic_dec_not_zero(&attr->times); |
| 131 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 132 | return true; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS |
| 136 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 137 | static int debugfs_ul_set(void *data, u64 val) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 138 | { |
| 139 | *(unsigned long *)data = val; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 140 | return 0; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Stephen Rothwell | 6ddb23c | 2007-07-23 18:43:55 -0700 | [diff] [blame] | 143 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 144 | static int debugfs_ul_set_MAX_STACK_TRACE_DEPTH(void *data, u64 val) |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 145 | { |
| 146 | *(unsigned long *)data = |
| 147 | val < MAX_STACK_TRACE_DEPTH ? |
| 148 | val : MAX_STACK_TRACE_DEPTH; |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 149 | return 0; |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 150 | } |
Stephen Rothwell | 6ddb23c | 2007-07-23 18:43:55 -0700 | [diff] [blame] | 151 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 152 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 153 | static int debugfs_ul_get(void *data, u64 *val) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 154 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 155 | *val = *(unsigned long *)data; |
| 156 | return 0; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n"); |
| 160 | |
| 161 | static struct dentry *debugfs_create_ul(const char *name, mode_t mode, |
| 162 | struct dentry *parent, unsigned long *value) |
| 163 | { |
| 164 | return debugfs_create_file(name, mode, parent, value, &fops_ul); |
| 165 | } |
| 166 | |
Stephen Rothwell | 6ddb23c | 2007-07-23 18:43:55 -0700 | [diff] [blame] | 167 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 168 | DEFINE_SIMPLE_ATTRIBUTE(fops_ul_MAX_STACK_TRACE_DEPTH, debugfs_ul_get, |
| 169 | debugfs_ul_set_MAX_STACK_TRACE_DEPTH, "%llu\n"); |
| 170 | |
| 171 | static struct dentry *debugfs_create_ul_MAX_STACK_TRACE_DEPTH( |
| 172 | const char *name, mode_t mode, |
| 173 | struct dentry *parent, unsigned long *value) |
| 174 | { |
| 175 | return debugfs_create_file(name, mode, parent, value, |
| 176 | &fops_ul_MAX_STACK_TRACE_DEPTH); |
| 177 | } |
Stephen Rothwell | 6ddb23c | 2007-07-23 18:43:55 -0700 | [diff] [blame] | 178 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 179 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 180 | static int debugfs_atomic_t_set(void *data, u64 val) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 181 | { |
| 182 | atomic_set((atomic_t *)data, val); |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 183 | return 0; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 186 | static int debugfs_atomic_t_get(void *data, u64 *val) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 187 | { |
Christoph Hellwig | 8b88b09 | 2008-02-08 04:20:26 -0800 | [diff] [blame] | 188 | *val = atomic_read((atomic_t *)data); |
| 189 | return 0; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, |
| 193 | debugfs_atomic_t_set, "%lld\n"); |
| 194 | |
| 195 | static struct dentry *debugfs_create_atomic_t(const char *name, mode_t mode, |
| 196 | struct dentry *parent, atomic_t *value) |
| 197 | { |
| 198 | return debugfs_create_file(name, mode, parent, value, &fops_atomic_t); |
| 199 | } |
| 200 | |
| 201 | void cleanup_fault_attr_dentries(struct fault_attr *attr) |
| 202 | { |
| 203 | debugfs_remove(attr->dentries.probability_file); |
| 204 | attr->dentries.probability_file = NULL; |
| 205 | |
| 206 | debugfs_remove(attr->dentries.interval_file); |
| 207 | attr->dentries.interval_file = NULL; |
| 208 | |
| 209 | debugfs_remove(attr->dentries.times_file); |
| 210 | attr->dentries.times_file = NULL; |
| 211 | |
| 212 | debugfs_remove(attr->dentries.space_file); |
| 213 | attr->dentries.space_file = NULL; |
| 214 | |
| 215 | debugfs_remove(attr->dentries.verbose_file); |
| 216 | attr->dentries.verbose_file = NULL; |
| 217 | |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 218 | debugfs_remove(attr->dentries.task_filter_file); |
| 219 | attr->dentries.task_filter_file = NULL; |
| 220 | |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 221 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER |
| 222 | |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 223 | debugfs_remove(attr->dentries.stacktrace_depth_file); |
| 224 | attr->dentries.stacktrace_depth_file = NULL; |
| 225 | |
| 226 | debugfs_remove(attr->dentries.require_start_file); |
| 227 | attr->dentries.require_start_file = NULL; |
| 228 | |
| 229 | debugfs_remove(attr->dentries.require_end_file); |
| 230 | attr->dentries.require_end_file = NULL; |
| 231 | |
| 232 | debugfs_remove(attr->dentries.reject_start_file); |
| 233 | attr->dentries.reject_start_file = NULL; |
| 234 | |
| 235 | debugfs_remove(attr->dentries.reject_end_file); |
| 236 | attr->dentries.reject_end_file = NULL; |
| 237 | |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 238 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
| 239 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 240 | if (attr->dentries.dir) |
| 241 | WARN_ON(!simple_empty(attr->dentries.dir)); |
| 242 | |
| 243 | debugfs_remove(attr->dentries.dir); |
| 244 | attr->dentries.dir = NULL; |
| 245 | } |
| 246 | |
| 247 | int init_fault_attr_dentries(struct fault_attr *attr, const char *name) |
| 248 | { |
| 249 | mode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
| 250 | struct dentry *dir; |
| 251 | |
| 252 | memset(&attr->dentries, 0, sizeof(attr->dentries)); |
| 253 | |
| 254 | dir = debugfs_create_dir(name, NULL); |
| 255 | if (!dir) |
| 256 | goto fail; |
| 257 | attr->dentries.dir = dir; |
| 258 | |
| 259 | attr->dentries.probability_file = |
| 260 | debugfs_create_ul("probability", mode, dir, &attr->probability); |
| 261 | |
| 262 | attr->dentries.interval_file = |
| 263 | debugfs_create_ul("interval", mode, dir, &attr->interval); |
| 264 | |
| 265 | attr->dentries.times_file = |
| 266 | debugfs_create_atomic_t("times", mode, dir, &attr->times); |
| 267 | |
| 268 | attr->dentries.space_file = |
| 269 | debugfs_create_atomic_t("space", mode, dir, &attr->space); |
| 270 | |
| 271 | attr->dentries.verbose_file = |
| 272 | debugfs_create_ul("verbose", mode, dir, &attr->verbose); |
| 273 | |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 274 | attr->dentries.task_filter_file = debugfs_create_bool("task-filter", |
| 275 | mode, dir, &attr->task_filter); |
| 276 | |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 277 | if (!attr->dentries.probability_file || !attr->dentries.interval_file || |
| 278 | !attr->dentries.times_file || !attr->dentries.space_file || |
| 279 | !attr->dentries.verbose_file || !attr->dentries.task_filter_file) |
| 280 | goto fail; |
| 281 | |
| 282 | #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER |
| 283 | |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 284 | attr->dentries.stacktrace_depth_file = |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 285 | debugfs_create_ul_MAX_STACK_TRACE_DEPTH( |
| 286 | "stacktrace-depth", mode, dir, &attr->stacktrace_depth); |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 287 | |
| 288 | attr->dentries.require_start_file = |
| 289 | debugfs_create_ul("require-start", mode, dir, &attr->require_start); |
| 290 | |
| 291 | attr->dentries.require_end_file = |
| 292 | debugfs_create_ul("require-end", mode, dir, &attr->require_end); |
| 293 | |
| 294 | attr->dentries.reject_start_file = |
| 295 | debugfs_create_ul("reject-start", mode, dir, &attr->reject_start); |
| 296 | |
| 297 | attr->dentries.reject_end_file = |
| 298 | debugfs_create_ul("reject-end", mode, dir, &attr->reject_end); |
| 299 | |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 300 | if (!attr->dentries.stacktrace_depth_file || |
| 301 | !attr->dentries.require_start_file || |
| 302 | !attr->dentries.require_end_file || |
| 303 | !attr->dentries.reject_start_file || |
| 304 | !attr->dentries.reject_end_file) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 305 | goto fail; |
| 306 | |
Akinobu Mita | 1df4900 | 2007-02-20 13:57:56 -0800 | [diff] [blame] | 307 | #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */ |
| 308 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 309 | return 0; |
| 310 | fail: |
| 311 | cleanup_fault_attr_dentries(attr); |
| 312 | return -ENOMEM; |
| 313 | } |
| 314 | |
| 315 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ |