blob: 244782940349ba8aea5c9d4750bfc028a506b9a0 [file] [log] [blame]
Akinobu Mita6ff1cb32006-12-08 02:39:43 -08001#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 Mitaf4f154f2006-12-08 02:39:47 -08008#include <linux/interrupt.h>
Akinobu Mita329409a2006-12-08 02:39:48 -08009#include <linux/unwind.h>
10#include <linux/stacktrace.h>
11#include <linux/kallsyms.h>
Akinobu Mita6ff1cb32006-12-08 02:39:43 -080012#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 */
18int __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, &times) < 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
41static 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 Mullis08b3df22006-12-08 02:39:51 -080051static bool fail_task(struct fault_attr *attr, struct task_struct *task)
Akinobu Mitaf4f154f2006-12-08 02:39:47 -080052{
53 return !in_interrupt() && task->make_it_fail;
54}
55
Akinobu Mita329409a2006-12-08 02:39:48 -080056#ifdef CONFIG_STACK_UNWIND
57
58static asmlinkage int fail_stacktrace_callback(struct unwind_frame_info *info,
59 void *arg)
60{
61 int depth;
62 struct fault_attr *attr = arg;
63 bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX);
64
65 for (depth = 0; depth < attr->stacktrace_depth
66 && unwind(info) == 0 && UNW_PC(info); depth++) {
67 if (arch_unw_user_mode(info))
68 break;
69 if (attr->reject_start <= UNW_PC(info) &&
70 UNW_PC(info) < attr->reject_end)
Don Mullis08b3df22006-12-08 02:39:51 -080071 return false;
Akinobu Mita329409a2006-12-08 02:39:48 -080072 if (attr->require_start <= UNW_PC(info) &&
73 UNW_PC(info) < attr->require_end)
Don Mullis08b3df22006-12-08 02:39:51 -080074 found = true;
Akinobu Mita329409a2006-12-08 02:39:48 -080075 }
76 return found;
77}
78
Don Mullis08b3df22006-12-08 02:39:51 -080079static bool fail_stacktrace(struct fault_attr *attr)
Akinobu Mita329409a2006-12-08 02:39:48 -080080{
81 struct unwind_frame_info info;
82
83 return unwind_init_running(&info, fail_stacktrace_callback, attr);
84}
85
86#elif defined(CONFIG_STACKTRACE)
87
Don Mullis08b3df22006-12-08 02:39:51 -080088static bool fail_stacktrace(struct fault_attr *attr)
Akinobu Mita329409a2006-12-08 02:39:48 -080089{
90 struct stack_trace trace;
91 int depth = attr->stacktrace_depth;
92 unsigned long entries[MAX_STACK_TRACE_DEPTH];
93 int n;
94 bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX);
95
96 if (depth == 0)
97 return found;
98
99 trace.nr_entries = 0;
100 trace.entries = entries;
101 trace.max_entries = (depth < MAX_STACK_TRACE_DEPTH) ?
102 depth : MAX_STACK_TRACE_DEPTH;
103 trace.skip = 1;
104 trace.all_contexts = 0;
105
106 save_stack_trace(&trace, NULL);
107 for (n = 0; n < trace.nr_entries; n++) {
108 if (attr->reject_start <= entries[n] &&
109 entries[n] < attr->reject_end)
Don Mullis08b3df22006-12-08 02:39:51 -0800110 return false;
Akinobu Mita329409a2006-12-08 02:39:48 -0800111 if (attr->require_start <= entries[n] &&
112 entries[n] < attr->require_end)
Don Mullis08b3df22006-12-08 02:39:51 -0800113 found = true;
Akinobu Mita329409a2006-12-08 02:39:48 -0800114 }
115 return found;
116}
117
118#else
119
Don Mullis08b3df22006-12-08 02:39:51 -0800120static inline bool fail_stacktrace(struct fault_attr *attr)
Akinobu Mita329409a2006-12-08 02:39:48 -0800121{
Don Mullis08b3df22006-12-08 02:39:51 -0800122 static bool firsttime = true;
Akinobu Mita329409a2006-12-08 02:39:48 -0800123
124 if (firsttime) {
125 printk(KERN_WARNING
126 "This architecture does not implement save_stack_trace()\n");
Don Mullis08b3df22006-12-08 02:39:51 -0800127 firsttime = false;
Akinobu Mita329409a2006-12-08 02:39:48 -0800128 }
Don Mullis08b3df22006-12-08 02:39:51 -0800129 return false;
Akinobu Mita329409a2006-12-08 02:39:48 -0800130}
131
132#endif
133
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800134/*
135 * This code is stolen from failmalloc-1.0
136 * http://www.nongnu.org/failmalloc/
137 */
138
Don Mullis08b3df22006-12-08 02:39:51 -0800139bool should_fail(struct fault_attr *attr, ssize_t size)
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800140{
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800141 if (attr->task_filter && !fail_task(attr, current))
Don Mullis08b3df22006-12-08 02:39:51 -0800142 return false;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800143
Akinobu Mita329409a2006-12-08 02:39:48 -0800144 if (!fail_stacktrace(attr))
Don Mullis08b3df22006-12-08 02:39:51 -0800145 return false;
Akinobu Mita329409a2006-12-08 02:39:48 -0800146
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800147 if (atomic_read(&attr->times) == 0)
Don Mullis08b3df22006-12-08 02:39:51 -0800148 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800149
150 if (atomic_read(&attr->space) > size) {
151 atomic_sub(size, &attr->space);
Don Mullis08b3df22006-12-08 02:39:51 -0800152 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800153 }
154
155 if (attr->interval > 1) {
156 attr->count++;
157 if (attr->count % attr->interval)
Don Mullis08b3df22006-12-08 02:39:51 -0800158 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800159 }
160
161 if (attr->probability > random32() % 100)
162 goto fail;
163
Don Mullis08b3df22006-12-08 02:39:51 -0800164 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800165
166fail:
167 fail_dump(attr);
168
169 if (atomic_read(&attr->times) != -1)
170 atomic_dec_not_zero(&attr->times);
171
Don Mullis08b3df22006-12-08 02:39:51 -0800172 return true;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800173}
174
175#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
176
177static void debugfs_ul_set(void *data, u64 val)
178{
179 *(unsigned long *)data = val;
180}
181
182static u64 debugfs_ul_get(void *data)
183{
184 return *(unsigned long *)data;
185}
186
187DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
188
189static struct dentry *debugfs_create_ul(const char *name, mode_t mode,
190 struct dentry *parent, unsigned long *value)
191{
192 return debugfs_create_file(name, mode, parent, value, &fops_ul);
193}
194
195static void debugfs_atomic_t_set(void *data, u64 val)
196{
197 atomic_set((atomic_t *)data, val);
198}
199
200static u64 debugfs_atomic_t_get(void *data)
201{
202 return atomic_read((atomic_t *)data);
203}
204
205DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
206 debugfs_atomic_t_set, "%lld\n");
207
208static struct dentry *debugfs_create_atomic_t(const char *name, mode_t mode,
209 struct dentry *parent, atomic_t *value)
210{
211 return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
212}
213
214void cleanup_fault_attr_dentries(struct fault_attr *attr)
215{
216 debugfs_remove(attr->dentries.probability_file);
217 attr->dentries.probability_file = NULL;
218
219 debugfs_remove(attr->dentries.interval_file);
220 attr->dentries.interval_file = NULL;
221
222 debugfs_remove(attr->dentries.times_file);
223 attr->dentries.times_file = NULL;
224
225 debugfs_remove(attr->dentries.space_file);
226 attr->dentries.space_file = NULL;
227
228 debugfs_remove(attr->dentries.verbose_file);
229 attr->dentries.verbose_file = NULL;
230
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800231 debugfs_remove(attr->dentries.task_filter_file);
232 attr->dentries.task_filter_file = NULL;
233
Akinobu Mita329409a2006-12-08 02:39:48 -0800234 debugfs_remove(attr->dentries.stacktrace_depth_file);
235 attr->dentries.stacktrace_depth_file = NULL;
236
237 debugfs_remove(attr->dentries.require_start_file);
238 attr->dentries.require_start_file = NULL;
239
240 debugfs_remove(attr->dentries.require_end_file);
241 attr->dentries.require_end_file = NULL;
242
243 debugfs_remove(attr->dentries.reject_start_file);
244 attr->dentries.reject_start_file = NULL;
245
246 debugfs_remove(attr->dentries.reject_end_file);
247 attr->dentries.reject_end_file = NULL;
248
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800249 if (attr->dentries.dir)
250 WARN_ON(!simple_empty(attr->dentries.dir));
251
252 debugfs_remove(attr->dentries.dir);
253 attr->dentries.dir = NULL;
254}
255
256int init_fault_attr_dentries(struct fault_attr *attr, const char *name)
257{
258 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
259 struct dentry *dir;
260
261 memset(&attr->dentries, 0, sizeof(attr->dentries));
262
263 dir = debugfs_create_dir(name, NULL);
264 if (!dir)
265 goto fail;
266 attr->dentries.dir = dir;
267
268 attr->dentries.probability_file =
269 debugfs_create_ul("probability", mode, dir, &attr->probability);
270
271 attr->dentries.interval_file =
272 debugfs_create_ul("interval", mode, dir, &attr->interval);
273
274 attr->dentries.times_file =
275 debugfs_create_atomic_t("times", mode, dir, &attr->times);
276
277 attr->dentries.space_file =
278 debugfs_create_atomic_t("space", mode, dir, &attr->space);
279
280 attr->dentries.verbose_file =
281 debugfs_create_ul("verbose", mode, dir, &attr->verbose);
282
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800283 attr->dentries.task_filter_file = debugfs_create_bool("task-filter",
284 mode, dir, &attr->task_filter);
285
Akinobu Mita329409a2006-12-08 02:39:48 -0800286 attr->dentries.stacktrace_depth_file =
287 debugfs_create_ul("stacktrace-depth", mode, dir,
288 &attr->stacktrace_depth);
289
290 attr->dentries.require_start_file =
291 debugfs_create_ul("require-start", mode, dir, &attr->require_start);
292
293 attr->dentries.require_end_file =
294 debugfs_create_ul("require-end", mode, dir, &attr->require_end);
295
296 attr->dentries.reject_start_file =
297 debugfs_create_ul("reject-start", mode, dir, &attr->reject_start);
298
299 attr->dentries.reject_end_file =
300 debugfs_create_ul("reject-end", mode, dir, &attr->reject_end);
301
302
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800303 if (!attr->dentries.probability_file || !attr->dentries.interval_file
304 || !attr->dentries.times_file || !attr->dentries.space_file
Akinobu Mita329409a2006-12-08 02:39:48 -0800305 || !attr->dentries.verbose_file || !attr->dentries.task_filter_file
306 || !attr->dentries.stacktrace_depth_file
307 || !attr->dentries.require_start_file
308 || !attr->dentries.require_end_file
309 || !attr->dentries.reject_start_file
310 || !attr->dentries.reject_end_file
311 )
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800312 goto fail;
313
314 return 0;
315fail:
316 cleanup_fault_attr_dentries(attr);
317 return -ENOMEM;
318}
319
320#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */