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