[PATCH] fault injection: stacktrace filtering
[linux-3.10.git] / lib / fault-inject.c
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>
8 #include <linux/interrupt.h>
9 #include <linux/unwind.h>
10 #include <linux/stacktrace.h>
11 #include <linux/kallsyms.h>
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, &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
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
51 static int fail_task(struct fault_attr *attr, struct task_struct *task)
52 {
53         return !in_interrupt() && task->make_it_fail;
54 }
55
56 #ifdef CONFIG_STACK_UNWIND
57
58 static 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)
71                         return 0;
72                 if (attr->require_start <= UNW_PC(info) &&
73                                UNW_PC(info) < attr->require_end)
74                         found = 1;
75         }
76         return found;
77 }
78
79 static int fail_stacktrace(struct fault_attr *attr)
80 {
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
88 #define MAX_STACK_TRACE_DEPTH 32
89
90 static int fail_stacktrace(struct fault_attr *attr)
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;
103         trace.max_entries = (depth < MAX_STACK_TRACE_DEPTH) ?
104                                 depth : MAX_STACK_TRACE_DEPTH;
105         trace.skip = 1;
106         trace.all_contexts = 0;
107
108         save_stack_trace(&trace, NULL);
109         for (n = 0; n < trace.nr_entries; n++) {
110                 if (attr->reject_start <= entries[n] &&
111                                entries[n] < attr->reject_end)
112                         return 0;
113                 if (attr->require_start <= entries[n] &&
114                                entries[n] < attr->require_end)
115                         found = 1;
116         }
117         return found;
118 }
119
120 #else
121
122 static inline int fail_stacktrace(struct fault_attr *attr)
123 {
124         static int firsttime = 1;
125
126         if (firsttime) {
127                 printk(KERN_WARNING
128                 "This architecture does not implement save_stack_trace()\n");
129                 firsttime = 0;
130         }
131         return 0;
132 }
133
134 #endif
135
136 /*
137  * This code is stolen from failmalloc-1.0
138  * http://www.nongnu.org/failmalloc/
139  */
140
141 int should_fail(struct fault_attr *attr, ssize_t size)
142 {
143         if (attr->task_filter && !fail_task(attr, current))
144                 return 0;
145
146         if (!fail_stacktrace(attr))
147                 return 0;
148
149         if (atomic_read(&attr->times) == 0)
150                 return 0;
151
152         if (atomic_read(&attr->space) > size) {
153                 atomic_sub(size, &attr->space);
154                 return 0;
155         }
156
157         if (attr->interval > 1) {
158                 attr->count++;
159                 if (attr->count % attr->interval)
160                         return 0;
161         }
162
163         if (attr->probability > random32() % 100)
164                 goto fail;
165
166         return 0;
167
168 fail:
169         fail_dump(attr);
170
171         if (atomic_read(&attr->times) != -1)
172                 atomic_dec_not_zero(&attr->times);
173
174         return 1;
175 }
176
177 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
178
179 static void debugfs_ul_set(void *data, u64 val)
180 {
181         *(unsigned long *)data = val;
182 }
183
184 static u64 debugfs_ul_get(void *data)
185 {
186         return *(unsigned long *)data;
187 }
188
189 DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
190
191 static struct dentry *debugfs_create_ul(const char *name, mode_t mode,
192                                 struct dentry *parent, unsigned long *value)
193 {
194         return debugfs_create_file(name, mode, parent, value, &fops_ul);
195 }
196
197 static void debugfs_atomic_t_set(void *data, u64 val)
198 {
199         atomic_set((atomic_t *)data, val);
200 }
201
202 static u64 debugfs_atomic_t_get(void *data)
203 {
204         return atomic_read((atomic_t *)data);
205 }
206
207 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
208                         debugfs_atomic_t_set, "%lld\n");
209
210 static struct dentry *debugfs_create_atomic_t(const char *name, mode_t mode,
211                                 struct dentry *parent, atomic_t *value)
212 {
213         return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
214 }
215
216 void cleanup_fault_attr_dentries(struct fault_attr *attr)
217 {
218         debugfs_remove(attr->dentries.probability_file);
219         attr->dentries.probability_file = NULL;
220
221         debugfs_remove(attr->dentries.interval_file);
222         attr->dentries.interval_file = NULL;
223
224         debugfs_remove(attr->dentries.times_file);
225         attr->dentries.times_file = NULL;
226
227         debugfs_remove(attr->dentries.space_file);
228         attr->dentries.space_file = NULL;
229
230         debugfs_remove(attr->dentries.verbose_file);
231         attr->dentries.verbose_file = NULL;
232
233         debugfs_remove(attr->dentries.task_filter_file);
234         attr->dentries.task_filter_file = NULL;
235
236         debugfs_remove(attr->dentries.stacktrace_depth_file);
237         attr->dentries.stacktrace_depth_file = NULL;
238
239         debugfs_remove(attr->dentries.require_start_file);
240         attr->dentries.require_start_file = NULL;
241
242         debugfs_remove(attr->dentries.require_end_file);
243         attr->dentries.require_end_file = NULL;
244
245         debugfs_remove(attr->dentries.reject_start_file);
246         attr->dentries.reject_start_file = NULL;
247
248         debugfs_remove(attr->dentries.reject_end_file);
249         attr->dentries.reject_end_file = NULL;
250
251         if (attr->dentries.dir)
252                 WARN_ON(!simple_empty(attr->dentries.dir));
253
254         debugfs_remove(attr->dentries.dir);
255         attr->dentries.dir = NULL;
256 }
257
258 int init_fault_attr_dentries(struct fault_attr *attr, const char *name)
259 {
260         mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
261         struct dentry *dir;
262
263         memset(&attr->dentries, 0, sizeof(attr->dentries));
264
265         dir = debugfs_create_dir(name, NULL);
266         if (!dir)
267                 goto fail;
268         attr->dentries.dir = dir;
269
270         attr->dentries.probability_file =
271                 debugfs_create_ul("probability", mode, dir, &attr->probability);
272
273         attr->dentries.interval_file =
274                 debugfs_create_ul("interval", mode, dir, &attr->interval);
275
276         attr->dentries.times_file =
277                 debugfs_create_atomic_t("times", mode, dir, &attr->times);
278
279         attr->dentries.space_file =
280                 debugfs_create_atomic_t("space", mode, dir, &attr->space);
281
282         attr->dentries.verbose_file =
283                 debugfs_create_ul("verbose", mode, dir, &attr->verbose);
284
285         attr->dentries.task_filter_file = debugfs_create_bool("task-filter",
286                                                 mode, dir, &attr->task_filter);
287
288         attr->dentries.stacktrace_depth_file =
289                 debugfs_create_ul("stacktrace-depth", mode, dir,
290                                   &attr->stacktrace_depth);
291
292         attr->dentries.require_start_file =
293                 debugfs_create_ul("require-start", mode, dir, &attr->require_start);
294
295         attr->dentries.require_end_file =
296                 debugfs_create_ul("require-end", mode, dir, &attr->require_end);
297
298         attr->dentries.reject_start_file =
299                 debugfs_create_ul("reject-start", mode, dir, &attr->reject_start);
300
301         attr->dentries.reject_end_file =
302                 debugfs_create_ul("reject-end", mode, dir, &attr->reject_end);
303
304
305         if (!attr->dentries.probability_file || !attr->dentries.interval_file
306             || !attr->dentries.times_file || !attr->dentries.space_file
307             || !attr->dentries.verbose_file || !attr->dentries.task_filter_file
308             || !attr->dentries.stacktrace_depth_file
309             || !attr->dentries.require_start_file
310             || !attr->dentries.require_end_file
311             || !attr->dentries.reject_start_file
312             || !attr->dentries.reject_end_file
313             )
314                 goto fail;
315
316         return 0;
317 fail:
318         cleanup_fault_attr_dentries(attr);
319         return -ENOMEM;
320 }
321
322 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */