blob: 0fabd12c39d79786e3bcf0ce1fa3bb68c48d01d9 [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
Don Mullisa124c282006-12-08 02:39:52 -080056#define MAX_STACK_TRACE_DEPTH 32
57
Akinobu Mita1df49002007-02-20 13:57:56 -080058#ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
Akinobu Mita329409a2006-12-08 02:39:48 -080059
Don Mullis08b3df22006-12-08 02:39:51 -080060static bool fail_stacktrace(struct fault_attr *attr)
Akinobu Mita329409a2006-12-08 02:39:48 -080061{
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 Mullisa124c282006-12-08 02:39:52 -080073 trace.max_entries = depth;
Akinobu Mita329409a2006-12-08 02:39:48 -080074 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 Mullis08b3df22006-12-08 02:39:51 -080081 return false;
Akinobu Mita329409a2006-12-08 02:39:48 -080082 if (attr->require_start <= entries[n] &&
83 entries[n] < attr->require_end)
Don Mullis08b3df22006-12-08 02:39:51 -080084 found = true;
Akinobu Mita329409a2006-12-08 02:39:48 -080085 }
86 return found;
87}
88
89#else
90
Don Mullis08b3df22006-12-08 02:39:51 -080091static inline bool fail_stacktrace(struct fault_attr *attr)
Akinobu Mita329409a2006-12-08 02:39:48 -080092{
Akinobu Mita1df49002007-02-20 13:57:56 -080093 return true;
Akinobu Mita329409a2006-12-08 02:39:48 -080094}
95
Akinobu Mita1df49002007-02-20 13:57:56 -080096#endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
Akinobu Mita329409a2006-12-08 02:39:48 -080097
Akinobu Mita6ff1cb32006-12-08 02:39:43 -080098/*
99 * This code is stolen from failmalloc-1.0
100 * http://www.nongnu.org/failmalloc/
101 */
102
Don Mullis08b3df22006-12-08 02:39:51 -0800103bool should_fail(struct fault_attr *attr, ssize_t size)
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800104{
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800105 if (attr->task_filter && !fail_task(attr, current))
Don Mullis08b3df22006-12-08 02:39:51 -0800106 return false;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800107
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800108 if (atomic_read(&attr->times) == 0)
Don Mullis08b3df22006-12-08 02:39:51 -0800109 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800110
111 if (atomic_read(&attr->space) > size) {
112 atomic_sub(size, &attr->space);
Don Mullis08b3df22006-12-08 02:39:51 -0800113 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800114 }
115
116 if (attr->interval > 1) {
117 attr->count++;
118 if (attr->count % attr->interval)
Don Mullis08b3df22006-12-08 02:39:51 -0800119 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800120 }
121
Don Mullisf1729c282006-12-08 02:39:53 -0800122 if (attr->probability <= random32() % 100)
123 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800124
Don Mullisf1729c282006-12-08 02:39:53 -0800125 if (!fail_stacktrace(attr))
126 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800127
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800128 fail_dump(attr);
129
130 if (atomic_read(&attr->times) != -1)
131 atomic_dec_not_zero(&attr->times);
132
Don Mullis08b3df22006-12-08 02:39:51 -0800133 return true;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800134}
135
136#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
137
138static void debugfs_ul_set(void *data, u64 val)
139{
140 *(unsigned long *)data = val;
141}
142
Don Mullisa124c282006-12-08 02:39:52 -0800143static void debugfs_ul_set_MAX_STACK_TRACE_DEPTH(void *data, u64 val)
144{
145 *(unsigned long *)data =
146 val < MAX_STACK_TRACE_DEPTH ?
147 val : MAX_STACK_TRACE_DEPTH;
148}
149
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800150static u64 debugfs_ul_get(void *data)
151{
152 return *(unsigned long *)data;
153}
154
155DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
156
157static struct dentry *debugfs_create_ul(const char *name, mode_t mode,
158 struct dentry *parent, unsigned long *value)
159{
160 return debugfs_create_file(name, mode, parent, value, &fops_ul);
161}
162
Don Mullisa124c282006-12-08 02:39:52 -0800163DEFINE_SIMPLE_ATTRIBUTE(fops_ul_MAX_STACK_TRACE_DEPTH, debugfs_ul_get,
164 debugfs_ul_set_MAX_STACK_TRACE_DEPTH, "%llu\n");
165
166static struct dentry *debugfs_create_ul_MAX_STACK_TRACE_DEPTH(
167 const char *name, mode_t mode,
168 struct dentry *parent, unsigned long *value)
169{
170 return debugfs_create_file(name, mode, parent, value,
171 &fops_ul_MAX_STACK_TRACE_DEPTH);
172}
173
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800174static void debugfs_atomic_t_set(void *data, u64 val)
175{
176 atomic_set((atomic_t *)data, val);
177}
178
179static u64 debugfs_atomic_t_get(void *data)
180{
181 return atomic_read((atomic_t *)data);
182}
183
184DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
185 debugfs_atomic_t_set, "%lld\n");
186
187static struct dentry *debugfs_create_atomic_t(const char *name, mode_t mode,
188 struct dentry *parent, atomic_t *value)
189{
190 return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
191}
192
193void cleanup_fault_attr_dentries(struct fault_attr *attr)
194{
195 debugfs_remove(attr->dentries.probability_file);
196 attr->dentries.probability_file = NULL;
197
198 debugfs_remove(attr->dentries.interval_file);
199 attr->dentries.interval_file = NULL;
200
201 debugfs_remove(attr->dentries.times_file);
202 attr->dentries.times_file = NULL;
203
204 debugfs_remove(attr->dentries.space_file);
205 attr->dentries.space_file = NULL;
206
207 debugfs_remove(attr->dentries.verbose_file);
208 attr->dentries.verbose_file = NULL;
209
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800210 debugfs_remove(attr->dentries.task_filter_file);
211 attr->dentries.task_filter_file = NULL;
212
Akinobu Mita1df49002007-02-20 13:57:56 -0800213#ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
214
Akinobu Mita329409a2006-12-08 02:39:48 -0800215 debugfs_remove(attr->dentries.stacktrace_depth_file);
216 attr->dentries.stacktrace_depth_file = NULL;
217
218 debugfs_remove(attr->dentries.require_start_file);
219 attr->dentries.require_start_file = NULL;
220
221 debugfs_remove(attr->dentries.require_end_file);
222 attr->dentries.require_end_file = NULL;
223
224 debugfs_remove(attr->dentries.reject_start_file);
225 attr->dentries.reject_start_file = NULL;
226
227 debugfs_remove(attr->dentries.reject_end_file);
228 attr->dentries.reject_end_file = NULL;
229
Akinobu Mita1df49002007-02-20 13:57:56 -0800230#endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
231
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800232 if (attr->dentries.dir)
233 WARN_ON(!simple_empty(attr->dentries.dir));
234
235 debugfs_remove(attr->dentries.dir);
236 attr->dentries.dir = NULL;
237}
238
239int init_fault_attr_dentries(struct fault_attr *attr, const char *name)
240{
241 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
242 struct dentry *dir;
243
244 memset(&attr->dentries, 0, sizeof(attr->dentries));
245
246 dir = debugfs_create_dir(name, NULL);
247 if (!dir)
248 goto fail;
249 attr->dentries.dir = dir;
250
251 attr->dentries.probability_file =
252 debugfs_create_ul("probability", mode, dir, &attr->probability);
253
254 attr->dentries.interval_file =
255 debugfs_create_ul("interval", mode, dir, &attr->interval);
256
257 attr->dentries.times_file =
258 debugfs_create_atomic_t("times", mode, dir, &attr->times);
259
260 attr->dentries.space_file =
261 debugfs_create_atomic_t("space", mode, dir, &attr->space);
262
263 attr->dentries.verbose_file =
264 debugfs_create_ul("verbose", mode, dir, &attr->verbose);
265
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800266 attr->dentries.task_filter_file = debugfs_create_bool("task-filter",
267 mode, dir, &attr->task_filter);
268
Akinobu Mita1df49002007-02-20 13:57:56 -0800269 if (!attr->dentries.probability_file || !attr->dentries.interval_file ||
270 !attr->dentries.times_file || !attr->dentries.space_file ||
271 !attr->dentries.verbose_file || !attr->dentries.task_filter_file)
272 goto fail;
273
274#ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
275
Akinobu Mita329409a2006-12-08 02:39:48 -0800276 attr->dentries.stacktrace_depth_file =
Don Mullisa124c282006-12-08 02:39:52 -0800277 debugfs_create_ul_MAX_STACK_TRACE_DEPTH(
278 "stacktrace-depth", mode, dir, &attr->stacktrace_depth);
Akinobu Mita329409a2006-12-08 02:39:48 -0800279
280 attr->dentries.require_start_file =
281 debugfs_create_ul("require-start", mode, dir, &attr->require_start);
282
283 attr->dentries.require_end_file =
284 debugfs_create_ul("require-end", mode, dir, &attr->require_end);
285
286 attr->dentries.reject_start_file =
287 debugfs_create_ul("reject-start", mode, dir, &attr->reject_start);
288
289 attr->dentries.reject_end_file =
290 debugfs_create_ul("reject-end", mode, dir, &attr->reject_end);
291
Akinobu Mita1df49002007-02-20 13:57:56 -0800292 if (!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)
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800297 goto fail;
298
Akinobu Mita1df49002007-02-20 13:57:56 -0800299#endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
300
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800301 return 0;
302fail:
303 cleanup_fault_attr_dentries(attr);
304 return -ENOMEM;
305}
306
307#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */