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 | |
Linus Torvalds | d1526e2 | 2006-12-15 08:43:13 -0800 | [diff] [blame] | 58 | #if defined(CONFIG_STACKTRACE) |
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; |
| 75 | trace.all_contexts = 0; |
| 76 | |
| 77 | save_stack_trace(&trace, NULL); |
| 78 | for (n = 0; n < trace.nr_entries; n++) { |
| 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 | { |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 93 | static bool firsttime = true; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 94 | |
| 95 | if (firsttime) { |
| 96 | printk(KERN_WARNING |
| 97 | "This architecture does not implement save_stack_trace()\n"); |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 98 | firsttime = false; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 99 | } |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 100 | return false; |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | #endif |
| 104 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 105 | /* |
| 106 | * This code is stolen from failmalloc-1.0 |
| 107 | * http://www.nongnu.org/failmalloc/ |
| 108 | */ |
| 109 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 110 | bool should_fail(struct fault_attr *attr, ssize_t size) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 111 | { |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 112 | if (attr->task_filter && !fail_task(attr, current)) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 113 | return false; |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 114 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 115 | if (atomic_read(&attr->times) == 0) |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 116 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 117 | |
| 118 | if (atomic_read(&attr->space) > size) { |
| 119 | atomic_sub(size, &attr->space); |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 120 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | if (attr->interval > 1) { |
| 124 | attr->count++; |
| 125 | if (attr->count % attr->interval) |
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 | |
Don Mullis | f1729c28 | 2006-12-08 02:39:53 -0800 | [diff] [blame] | 129 | if (attr->probability <= random32() % 100) |
| 130 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 131 | |
Don Mullis | f1729c28 | 2006-12-08 02:39:53 -0800 | [diff] [blame] | 132 | if (!fail_stacktrace(attr)) |
| 133 | return false; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 134 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 135 | fail_dump(attr); |
| 136 | |
| 137 | if (atomic_read(&attr->times) != -1) |
| 138 | atomic_dec_not_zero(&attr->times); |
| 139 | |
Don Mullis | 08b3df2 | 2006-12-08 02:39:51 -0800 | [diff] [blame] | 140 | return true; |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS |
| 144 | |
| 145 | static void debugfs_ul_set(void *data, u64 val) |
| 146 | { |
| 147 | *(unsigned long *)data = val; |
| 148 | } |
| 149 | |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 150 | static void debugfs_ul_set_MAX_STACK_TRACE_DEPTH(void *data, u64 val) |
| 151 | { |
| 152 | *(unsigned long *)data = |
| 153 | val < MAX_STACK_TRACE_DEPTH ? |
| 154 | val : MAX_STACK_TRACE_DEPTH; |
| 155 | } |
| 156 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 157 | static u64 debugfs_ul_get(void *data) |
| 158 | { |
| 159 | return *(unsigned long *)data; |
| 160 | } |
| 161 | |
| 162 | DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n"); |
| 163 | |
| 164 | static struct dentry *debugfs_create_ul(const char *name, mode_t mode, |
| 165 | struct dentry *parent, unsigned long *value) |
| 166 | { |
| 167 | return debugfs_create_file(name, mode, parent, value, &fops_ul); |
| 168 | } |
| 169 | |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 170 | DEFINE_SIMPLE_ATTRIBUTE(fops_ul_MAX_STACK_TRACE_DEPTH, debugfs_ul_get, |
| 171 | debugfs_ul_set_MAX_STACK_TRACE_DEPTH, "%llu\n"); |
| 172 | |
| 173 | static struct dentry *debugfs_create_ul_MAX_STACK_TRACE_DEPTH( |
| 174 | const char *name, mode_t mode, |
| 175 | struct dentry *parent, unsigned long *value) |
| 176 | { |
| 177 | return debugfs_create_file(name, mode, parent, value, |
| 178 | &fops_ul_MAX_STACK_TRACE_DEPTH); |
| 179 | } |
| 180 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 181 | static void debugfs_atomic_t_set(void *data, u64 val) |
| 182 | { |
| 183 | atomic_set((atomic_t *)data, val); |
| 184 | } |
| 185 | |
| 186 | static u64 debugfs_atomic_t_get(void *data) |
| 187 | { |
| 188 | return atomic_read((atomic_t *)data); |
| 189 | } |
| 190 | |
| 191 | DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get, |
| 192 | debugfs_atomic_t_set, "%lld\n"); |
| 193 | |
| 194 | static struct dentry *debugfs_create_atomic_t(const char *name, mode_t mode, |
| 195 | struct dentry *parent, atomic_t *value) |
| 196 | { |
| 197 | return debugfs_create_file(name, mode, parent, value, &fops_atomic_t); |
| 198 | } |
| 199 | |
| 200 | void cleanup_fault_attr_dentries(struct fault_attr *attr) |
| 201 | { |
| 202 | debugfs_remove(attr->dentries.probability_file); |
| 203 | attr->dentries.probability_file = NULL; |
| 204 | |
| 205 | debugfs_remove(attr->dentries.interval_file); |
| 206 | attr->dentries.interval_file = NULL; |
| 207 | |
| 208 | debugfs_remove(attr->dentries.times_file); |
| 209 | attr->dentries.times_file = NULL; |
| 210 | |
| 211 | debugfs_remove(attr->dentries.space_file); |
| 212 | attr->dentries.space_file = NULL; |
| 213 | |
| 214 | debugfs_remove(attr->dentries.verbose_file); |
| 215 | attr->dentries.verbose_file = NULL; |
| 216 | |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 217 | debugfs_remove(attr->dentries.task_filter_file); |
| 218 | attr->dentries.task_filter_file = NULL; |
| 219 | |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 220 | debugfs_remove(attr->dentries.stacktrace_depth_file); |
| 221 | attr->dentries.stacktrace_depth_file = NULL; |
| 222 | |
| 223 | debugfs_remove(attr->dentries.require_start_file); |
| 224 | attr->dentries.require_start_file = NULL; |
| 225 | |
| 226 | debugfs_remove(attr->dentries.require_end_file); |
| 227 | attr->dentries.require_end_file = NULL; |
| 228 | |
| 229 | debugfs_remove(attr->dentries.reject_start_file); |
| 230 | attr->dentries.reject_start_file = NULL; |
| 231 | |
| 232 | debugfs_remove(attr->dentries.reject_end_file); |
| 233 | attr->dentries.reject_end_file = NULL; |
| 234 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 235 | if (attr->dentries.dir) |
| 236 | WARN_ON(!simple_empty(attr->dentries.dir)); |
| 237 | |
| 238 | debugfs_remove(attr->dentries.dir); |
| 239 | attr->dentries.dir = NULL; |
| 240 | } |
| 241 | |
| 242 | int init_fault_attr_dentries(struct fault_attr *attr, const char *name) |
| 243 | { |
| 244 | mode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
| 245 | struct dentry *dir; |
| 246 | |
| 247 | memset(&attr->dentries, 0, sizeof(attr->dentries)); |
| 248 | |
| 249 | dir = debugfs_create_dir(name, NULL); |
| 250 | if (!dir) |
| 251 | goto fail; |
| 252 | attr->dentries.dir = dir; |
| 253 | |
| 254 | attr->dentries.probability_file = |
| 255 | debugfs_create_ul("probability", mode, dir, &attr->probability); |
| 256 | |
| 257 | attr->dentries.interval_file = |
| 258 | debugfs_create_ul("interval", mode, dir, &attr->interval); |
| 259 | |
| 260 | attr->dentries.times_file = |
| 261 | debugfs_create_atomic_t("times", mode, dir, &attr->times); |
| 262 | |
| 263 | attr->dentries.space_file = |
| 264 | debugfs_create_atomic_t("space", mode, dir, &attr->space); |
| 265 | |
| 266 | attr->dentries.verbose_file = |
| 267 | debugfs_create_ul("verbose", mode, dir, &attr->verbose); |
| 268 | |
Akinobu Mita | f4f154f | 2006-12-08 02:39:47 -0800 | [diff] [blame] | 269 | attr->dentries.task_filter_file = debugfs_create_bool("task-filter", |
| 270 | mode, dir, &attr->task_filter); |
| 271 | |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 272 | attr->dentries.stacktrace_depth_file = |
Don Mullis | a124c28 | 2006-12-08 02:39:52 -0800 | [diff] [blame] | 273 | debugfs_create_ul_MAX_STACK_TRACE_DEPTH( |
| 274 | "stacktrace-depth", mode, dir, &attr->stacktrace_depth); |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 275 | |
| 276 | attr->dentries.require_start_file = |
| 277 | debugfs_create_ul("require-start", mode, dir, &attr->require_start); |
| 278 | |
| 279 | attr->dentries.require_end_file = |
| 280 | debugfs_create_ul("require-end", mode, dir, &attr->require_end); |
| 281 | |
| 282 | attr->dentries.reject_start_file = |
| 283 | debugfs_create_ul("reject-start", mode, dir, &attr->reject_start); |
| 284 | |
| 285 | attr->dentries.reject_end_file = |
| 286 | debugfs_create_ul("reject-end", mode, dir, &attr->reject_end); |
| 287 | |
| 288 | |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 289 | if (!attr->dentries.probability_file || !attr->dentries.interval_file |
| 290 | || !attr->dentries.times_file || !attr->dentries.space_file |
Akinobu Mita | 329409a | 2006-12-08 02:39:48 -0800 | [diff] [blame] | 291 | || !attr->dentries.verbose_file || !attr->dentries.task_filter_file |
| 292 | || !attr->dentries.stacktrace_depth_file |
| 293 | || !attr->dentries.require_start_file |
| 294 | || !attr->dentries.require_end_file |
| 295 | || !attr->dentries.reject_start_file |
| 296 | || !attr->dentries.reject_end_file |
| 297 | ) |
Akinobu Mita | 6ff1cb3 | 2006-12-08 02:39:43 -0800 | [diff] [blame] | 298 | goto fail; |
| 299 | |
| 300 | return 0; |
| 301 | fail: |
| 302 | cleanup_fault_attr_dentries(attr); |
| 303 | return -ENOMEM; |
| 304 | } |
| 305 | |
| 306 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ |