blob: b18fc2ff9ffea5df3d3377fc092bb8306d556fd2 [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;
Akinobu Mita329409a2006-12-08 02:39:48 -080075
Christoph Hellwigab1b6f02007-05-08 00:23:29 -070076 save_stack_trace(&trace);
Akinobu Mita329409a2006-12-08 02:39:48 -080077 for (n = 0; n < trace.nr_entries; n++) {
78 if (attr->reject_start <= entries[n] &&
79 entries[n] < attr->reject_end)
Don Mullis08b3df22006-12-08 02:39:51 -080080 return false;
Akinobu Mita329409a2006-12-08 02:39:48 -080081 if (attr->require_start <= entries[n] &&
82 entries[n] < attr->require_end)
Don Mullis08b3df22006-12-08 02:39:51 -080083 found = true;
Akinobu Mita329409a2006-12-08 02:39:48 -080084 }
85 return found;
86}
87
88#else
89
Don Mullis08b3df22006-12-08 02:39:51 -080090static inline bool fail_stacktrace(struct fault_attr *attr)
Akinobu Mita329409a2006-12-08 02:39:48 -080091{
Akinobu Mita1df49002007-02-20 13:57:56 -080092 return true;
Akinobu Mita329409a2006-12-08 02:39:48 -080093}
94
Akinobu Mita1df49002007-02-20 13:57:56 -080095#endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
Akinobu Mita329409a2006-12-08 02:39:48 -080096
Akinobu Mita6ff1cb32006-12-08 02:39:43 -080097/*
98 * This code is stolen from failmalloc-1.0
99 * http://www.nongnu.org/failmalloc/
100 */
101
Don Mullis08b3df22006-12-08 02:39:51 -0800102bool should_fail(struct fault_attr *attr, ssize_t size)
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800103{
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800104 if (attr->task_filter && !fail_task(attr, current))
Don Mullis08b3df22006-12-08 02:39:51 -0800105 return false;
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800106
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800107 if (atomic_read(&attr->times) == 0)
Don Mullis08b3df22006-12-08 02:39:51 -0800108 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800109
110 if (atomic_read(&attr->space) > size) {
111 atomic_sub(size, &attr->space);
Don Mullis08b3df22006-12-08 02:39:51 -0800112 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800113 }
114
115 if (attr->interval > 1) {
116 attr->count++;
117 if (attr->count % attr->interval)
Don Mullis08b3df22006-12-08 02:39:51 -0800118 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800119 }
120
Don Mullisf1729c22006-12-08 02:39:53 -0800121 if (attr->probability <= random32() % 100)
122 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800123
Don Mullisf1729c22006-12-08 02:39:53 -0800124 if (!fail_stacktrace(attr))
125 return false;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800126
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800127 fail_dump(attr);
128
129 if (atomic_read(&attr->times) != -1)
130 atomic_dec_not_zero(&attr->times);
131
Don Mullis08b3df22006-12-08 02:39:51 -0800132 return true;
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800133}
134
135#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
136
137static void debugfs_ul_set(void *data, u64 val)
138{
139 *(unsigned long *)data = val;
140}
141
Don Mullisa124c282006-12-08 02:39:52 -0800142static void debugfs_ul_set_MAX_STACK_TRACE_DEPTH(void *data, u64 val)
143{
144 *(unsigned long *)data =
145 val < MAX_STACK_TRACE_DEPTH ?
146 val : MAX_STACK_TRACE_DEPTH;
147}
148
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800149static u64 debugfs_ul_get(void *data)
150{
151 return *(unsigned long *)data;
152}
153
154DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
155
156static struct dentry *debugfs_create_ul(const char *name, mode_t mode,
157 struct dentry *parent, unsigned long *value)
158{
159 return debugfs_create_file(name, mode, parent, value, &fops_ul);
160}
161
Don Mullisa124c282006-12-08 02:39:52 -0800162DEFINE_SIMPLE_ATTRIBUTE(fops_ul_MAX_STACK_TRACE_DEPTH, debugfs_ul_get,
163 debugfs_ul_set_MAX_STACK_TRACE_DEPTH, "%llu\n");
164
165static struct dentry *debugfs_create_ul_MAX_STACK_TRACE_DEPTH(
166 const char *name, mode_t mode,
167 struct dentry *parent, unsigned long *value)
168{
169 return debugfs_create_file(name, mode, parent, value,
170 &fops_ul_MAX_STACK_TRACE_DEPTH);
171}
172
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800173static void debugfs_atomic_t_set(void *data, u64 val)
174{
175 atomic_set((atomic_t *)data, val);
176}
177
178static u64 debugfs_atomic_t_get(void *data)
179{
180 return atomic_read((atomic_t *)data);
181}
182
183DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t, debugfs_atomic_t_get,
184 debugfs_atomic_t_set, "%lld\n");
185
186static struct dentry *debugfs_create_atomic_t(const char *name, mode_t mode,
187 struct dentry *parent, atomic_t *value)
188{
189 return debugfs_create_file(name, mode, parent, value, &fops_atomic_t);
190}
191
192void cleanup_fault_attr_dentries(struct fault_attr *attr)
193{
194 debugfs_remove(attr->dentries.probability_file);
195 attr->dentries.probability_file = NULL;
196
197 debugfs_remove(attr->dentries.interval_file);
198 attr->dentries.interval_file = NULL;
199
200 debugfs_remove(attr->dentries.times_file);
201 attr->dentries.times_file = NULL;
202
203 debugfs_remove(attr->dentries.space_file);
204 attr->dentries.space_file = NULL;
205
206 debugfs_remove(attr->dentries.verbose_file);
207 attr->dentries.verbose_file = NULL;
208
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800209 debugfs_remove(attr->dentries.task_filter_file);
210 attr->dentries.task_filter_file = NULL;
211
Akinobu Mita1df49002007-02-20 13:57:56 -0800212#ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
213
Akinobu Mita329409a2006-12-08 02:39:48 -0800214 debugfs_remove(attr->dentries.stacktrace_depth_file);
215 attr->dentries.stacktrace_depth_file = NULL;
216
217 debugfs_remove(attr->dentries.require_start_file);
218 attr->dentries.require_start_file = NULL;
219
220 debugfs_remove(attr->dentries.require_end_file);
221 attr->dentries.require_end_file = NULL;
222
223 debugfs_remove(attr->dentries.reject_start_file);
224 attr->dentries.reject_start_file = NULL;
225
226 debugfs_remove(attr->dentries.reject_end_file);
227 attr->dentries.reject_end_file = NULL;
228
Akinobu Mita1df49002007-02-20 13:57:56 -0800229#endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
230
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800231 if (attr->dentries.dir)
232 WARN_ON(!simple_empty(attr->dentries.dir));
233
234 debugfs_remove(attr->dentries.dir);
235 attr->dentries.dir = NULL;
236}
237
238int init_fault_attr_dentries(struct fault_attr *attr, const char *name)
239{
240 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
241 struct dentry *dir;
242
243 memset(&attr->dentries, 0, sizeof(attr->dentries));
244
245 dir = debugfs_create_dir(name, NULL);
246 if (!dir)
247 goto fail;
248 attr->dentries.dir = dir;
249
250 attr->dentries.probability_file =
251 debugfs_create_ul("probability", mode, dir, &attr->probability);
252
253 attr->dentries.interval_file =
254 debugfs_create_ul("interval", mode, dir, &attr->interval);
255
256 attr->dentries.times_file =
257 debugfs_create_atomic_t("times", mode, dir, &attr->times);
258
259 attr->dentries.space_file =
260 debugfs_create_atomic_t("space", mode, dir, &attr->space);
261
262 attr->dentries.verbose_file =
263 debugfs_create_ul("verbose", mode, dir, &attr->verbose);
264
Akinobu Mitaf4f154f2006-12-08 02:39:47 -0800265 attr->dentries.task_filter_file = debugfs_create_bool("task-filter",
266 mode, dir, &attr->task_filter);
267
Akinobu Mita1df49002007-02-20 13:57:56 -0800268 if (!attr->dentries.probability_file || !attr->dentries.interval_file ||
269 !attr->dentries.times_file || !attr->dentries.space_file ||
270 !attr->dentries.verbose_file || !attr->dentries.task_filter_file)
271 goto fail;
272
273#ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
274
Akinobu Mita329409a2006-12-08 02:39:48 -0800275 attr->dentries.stacktrace_depth_file =
Don Mullisa124c282006-12-08 02:39:52 -0800276 debugfs_create_ul_MAX_STACK_TRACE_DEPTH(
277 "stacktrace-depth", mode, dir, &attr->stacktrace_depth);
Akinobu Mita329409a2006-12-08 02:39:48 -0800278
279 attr->dentries.require_start_file =
280 debugfs_create_ul("require-start", mode, dir, &attr->require_start);
281
282 attr->dentries.require_end_file =
283 debugfs_create_ul("require-end", mode, dir, &attr->require_end);
284
285 attr->dentries.reject_start_file =
286 debugfs_create_ul("reject-start", mode, dir, &attr->reject_start);
287
288 attr->dentries.reject_end_file =
289 debugfs_create_ul("reject-end", mode, dir, &attr->reject_end);
290
Akinobu Mita1df49002007-02-20 13:57:56 -0800291 if (!attr->dentries.stacktrace_depth_file ||
292 !attr->dentries.require_start_file ||
293 !attr->dentries.require_end_file ||
294 !attr->dentries.reject_start_file ||
295 !attr->dentries.reject_end_file)
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800296 goto fail;
297
Akinobu Mita1df49002007-02-20 13:57:56 -0800298#endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
299
Akinobu Mita6ff1cb32006-12-08 02:39:43 -0800300 return 0;
301fail:
302 cleanup_fault_attr_dentries(attr);
303 return -ENOMEM;
304}
305
306#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */