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>
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>
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.
18 int __init
setup_fault_attr(struct fault_attr
*attr
, char *str
)
20 unsigned long probability
;
21 unsigned long interval
;
25 /* "<interval>,<probability>,<space>,<times>" */
26 if (sscanf(str
, "%lu,%lu,%d,%d",
27 &interval
, &probability
, &space
, ×
) < 4) {
29 "FAULT_INJECTION: failed to parse arguments\n");
33 attr
->probability
= probability
;
34 attr
->interval
= interval
;
35 atomic_set(&attr
->times
, times
);
36 atomic_set(&attr
->space
, space
);
41 static void fail_dump(struct fault_attr
*attr
)
43 if (attr
->verbose
> 0)
44 printk(KERN_NOTICE
"FAULT_INJECTION: forcing a failure\n");
45 if (attr
->verbose
> 1)
49 #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
51 static bool fail_task(struct fault_attr
*attr
, struct task_struct
*task
)
53 return !in_interrupt() && task
->make_it_fail
;
56 #define MAX_STACK_TRACE_DEPTH 32
58 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
60 static bool fail_stacktrace(struct fault_attr
*attr
)
62 struct stack_trace trace
;
63 int depth
= attr
->stacktrace_depth
;
64 unsigned long entries
[MAX_STACK_TRACE_DEPTH
];
66 bool found
= (attr
->require_start
== 0 && attr
->require_end
== ULONG_MAX
);
72 trace
.entries
= entries
;
73 trace
.max_entries
= depth
;
75 trace
.all_contexts
= 0;
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
)
82 if (attr
->require_start
<= entries
[n
] &&
83 entries
[n
] < attr
->require_end
)
91 static inline bool fail_stacktrace(struct fault_attr
*attr
)
96 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
99 * This code is stolen from failmalloc-1.0
100 * http://www.nongnu.org/failmalloc/
103 bool should_fail(struct fault_attr
*attr
, ssize_t size
)
105 if (attr
->task_filter
&& !fail_task(attr
, current
))
108 if (atomic_read(&attr
->times
) == 0)
111 if (atomic_read(&attr
->space
) > size
) {
112 atomic_sub(size
, &attr
->space
);
116 if (attr
->interval
> 1) {
118 if (attr
->count
% attr
->interval
)
122 if (attr
->probability
<= random32() % 100)
125 if (!fail_stacktrace(attr
))
130 if (atomic_read(&attr
->times
) != -1)
131 atomic_dec_not_zero(&attr
->times
);
136 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
138 static void debugfs_ul_set(void *data
, u64 val
)
140 *(unsigned long *)data
= val
;
143 static void debugfs_ul_set_MAX_STACK_TRACE_DEPTH(void *data
, u64 val
)
145 *(unsigned long *)data
=
146 val
< MAX_STACK_TRACE_DEPTH
?
147 val
: MAX_STACK_TRACE_DEPTH
;
150 static u64
debugfs_ul_get(void *data
)
152 return *(unsigned long *)data
;
155 DEFINE_SIMPLE_ATTRIBUTE(fops_ul
, debugfs_ul_get
, debugfs_ul_set
, "%llu\n");
157 static struct dentry
*debugfs_create_ul(const char *name
, mode_t mode
,
158 struct dentry
*parent
, unsigned long *value
)
160 return debugfs_create_file(name
, mode
, parent
, value
, &fops_ul
);
163 DEFINE_SIMPLE_ATTRIBUTE(fops_ul_MAX_STACK_TRACE_DEPTH
, debugfs_ul_get
,
164 debugfs_ul_set_MAX_STACK_TRACE_DEPTH
, "%llu\n");
166 static struct dentry
*debugfs_create_ul_MAX_STACK_TRACE_DEPTH(
167 const char *name
, mode_t mode
,
168 struct dentry
*parent
, unsigned long *value
)
170 return debugfs_create_file(name
, mode
, parent
, value
,
171 &fops_ul_MAX_STACK_TRACE_DEPTH
);
174 static void debugfs_atomic_t_set(void *data
, u64 val
)
176 atomic_set((atomic_t
*)data
, val
);
179 static u64
debugfs_atomic_t_get(void *data
)
181 return atomic_read((atomic_t
*)data
);
184 DEFINE_SIMPLE_ATTRIBUTE(fops_atomic_t
, debugfs_atomic_t_get
,
185 debugfs_atomic_t_set
, "%lld\n");
187 static struct dentry
*debugfs_create_atomic_t(const char *name
, mode_t mode
,
188 struct dentry
*parent
, atomic_t
*value
)
190 return debugfs_create_file(name
, mode
, parent
, value
, &fops_atomic_t
);
193 void cleanup_fault_attr_dentries(struct fault_attr
*attr
)
195 debugfs_remove(attr
->dentries
.probability_file
);
196 attr
->dentries
.probability_file
= NULL
;
198 debugfs_remove(attr
->dentries
.interval_file
);
199 attr
->dentries
.interval_file
= NULL
;
201 debugfs_remove(attr
->dentries
.times_file
);
202 attr
->dentries
.times_file
= NULL
;
204 debugfs_remove(attr
->dentries
.space_file
);
205 attr
->dentries
.space_file
= NULL
;
207 debugfs_remove(attr
->dentries
.verbose_file
);
208 attr
->dentries
.verbose_file
= NULL
;
210 debugfs_remove(attr
->dentries
.task_filter_file
);
211 attr
->dentries
.task_filter_file
= NULL
;
213 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
215 debugfs_remove(attr
->dentries
.stacktrace_depth_file
);
216 attr
->dentries
.stacktrace_depth_file
= NULL
;
218 debugfs_remove(attr
->dentries
.require_start_file
);
219 attr
->dentries
.require_start_file
= NULL
;
221 debugfs_remove(attr
->dentries
.require_end_file
);
222 attr
->dentries
.require_end_file
= NULL
;
224 debugfs_remove(attr
->dentries
.reject_start_file
);
225 attr
->dentries
.reject_start_file
= NULL
;
227 debugfs_remove(attr
->dentries
.reject_end_file
);
228 attr
->dentries
.reject_end_file
= NULL
;
230 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
232 if (attr
->dentries
.dir
)
233 WARN_ON(!simple_empty(attr
->dentries
.dir
));
235 debugfs_remove(attr
->dentries
.dir
);
236 attr
->dentries
.dir
= NULL
;
239 int init_fault_attr_dentries(struct fault_attr
*attr
, const char *name
)
241 mode_t mode
= S_IFREG
| S_IRUSR
| S_IWUSR
;
244 memset(&attr
->dentries
, 0, sizeof(attr
->dentries
));
246 dir
= debugfs_create_dir(name
, NULL
);
249 attr
->dentries
.dir
= dir
;
251 attr
->dentries
.probability_file
=
252 debugfs_create_ul("probability", mode
, dir
, &attr
->probability
);
254 attr
->dentries
.interval_file
=
255 debugfs_create_ul("interval", mode
, dir
, &attr
->interval
);
257 attr
->dentries
.times_file
=
258 debugfs_create_atomic_t("times", mode
, dir
, &attr
->times
);
260 attr
->dentries
.space_file
=
261 debugfs_create_atomic_t("space", mode
, dir
, &attr
->space
);
263 attr
->dentries
.verbose_file
=
264 debugfs_create_ul("verbose", mode
, dir
, &attr
->verbose
);
266 attr
->dentries
.task_filter_file
= debugfs_create_bool("task-filter",
267 mode
, dir
, &attr
->task_filter
);
269 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
)
274 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
276 attr
->dentries
.stacktrace_depth_file
=
277 debugfs_create_ul_MAX_STACK_TRACE_DEPTH(
278 "stacktrace-depth", mode
, dir
, &attr
->stacktrace_depth
);
280 attr
->dentries
.require_start_file
=
281 debugfs_create_ul("require-start", mode
, dir
, &attr
->require_start
);
283 attr
->dentries
.require_end_file
=
284 debugfs_create_ul("require-end", mode
, dir
, &attr
->require_end
);
286 attr
->dentries
.reject_start_file
=
287 debugfs_create_ul("reject-start", mode
, dir
, &attr
->reject_start
);
289 attr
->dentries
.reject_end_file
=
290 debugfs_create_ul("reject-end", mode
, dir
, &attr
->reject_end
);
292 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
)
299 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
303 cleanup_fault_attr_dentries(attr
);
307 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */