drm/vc4: unlock on error in vc4_hvs_get_fifo_frame_count()
[drm/drm-misc.git] / lib / fault-inject.c
blob52eb6ba29698446a39d80bed6a0e2bf4297234b5
1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/kernel.h>
3 #include <linux/init.h>
4 #include <linux/random.h>
5 #include <linux/debugfs.h>
6 #include <linux/sched.h>
7 #include <linux/stat.h>
8 #include <linux/types.h>
9 #include <linux/fs.h>
10 #include <linux/export.h>
11 #include <linux/interrupt.h>
12 #include <linux/stacktrace.h>
13 #include <linux/fault-inject.h>
16 * setup_fault_attr() is a helper function for various __setup handlers, so it
17 * returns 0 on error, because that is what __setup handlers do.
19 int setup_fault_attr(struct fault_attr *attr, char *str)
21 unsigned long probability;
22 unsigned long interval;
23 int times;
24 int space;
26 /* "<interval>,<probability>,<space>,<times>" */
27 if (sscanf(str, "%lu,%lu,%d,%d",
28 &interval, &probability, &space, &times) < 4) {
29 printk(KERN_WARNING
30 "FAULT_INJECTION: failed to parse arguments\n");
31 return 0;
34 attr->probability = probability;
35 attr->interval = interval;
36 atomic_set(&attr->times, times);
37 atomic_set(&attr->space, space);
39 return 1;
41 EXPORT_SYMBOL_GPL(setup_fault_attr);
43 static void fail_dump(struct fault_attr *attr)
45 if (attr->verbose > 0 && __ratelimit(&attr->ratelimit_state)) {
46 printk(KERN_NOTICE "FAULT_INJECTION: forcing a failure.\n"
47 "name %pd, interval %lu, probability %lu, "
48 "space %d, times %d\n", attr->dname,
49 attr->interval, attr->probability,
50 atomic_read(&attr->space),
51 atomic_read(&attr->times));
52 if (attr->verbose > 1)
53 dump_stack();
57 #define atomic_dec_not_zero(v) atomic_add_unless((v), -1, 0)
59 static bool fail_task(struct fault_attr *attr, struct task_struct *task)
61 return in_task() && task->make_it_fail;
64 #define MAX_STACK_TRACE_DEPTH 32
66 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
68 static bool fail_stacktrace(struct fault_attr *attr)
70 int depth = attr->stacktrace_depth;
71 unsigned long entries[MAX_STACK_TRACE_DEPTH];
72 int n, nr_entries;
73 bool found = (attr->require_start == 0 && attr->require_end == ULONG_MAX);
75 if (depth == 0 || (found && !attr->reject_start && !attr->reject_end))
76 return found;
78 nr_entries = stack_trace_save(entries, depth, 1);
79 for (n = 0; n < nr_entries; n++) {
80 if (attr->reject_start <= entries[n] &&
81 entries[n] < attr->reject_end)
82 return false;
83 if (attr->require_start <= entries[n] &&
84 entries[n] < attr->require_end)
85 found = true;
87 return found;
90 #else
92 static inline bool fail_stacktrace(struct fault_attr *attr)
94 return true;
97 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
100 * This code is stolen from failmalloc-1.0
101 * http://www.nongnu.org/failmalloc/
104 bool should_fail_ex(struct fault_attr *attr, ssize_t size, int flags)
106 bool stack_checked = false;
108 if (in_task()) {
109 unsigned int fail_nth = READ_ONCE(current->fail_nth);
111 if (fail_nth) {
112 if (!fail_stacktrace(attr))
113 return false;
115 stack_checked = true;
116 fail_nth--;
117 WRITE_ONCE(current->fail_nth, fail_nth);
118 if (!fail_nth)
119 goto fail;
121 return false;
125 /* No need to check any other properties if the probability is 0 */
126 if (attr->probability == 0)
127 return false;
129 if (attr->task_filter && !fail_task(attr, current))
130 return false;
132 if (atomic_read(&attr->times) == 0)
133 return false;
135 if (!stack_checked && !fail_stacktrace(attr))
136 return false;
138 if (atomic_read(&attr->space) > size) {
139 atomic_sub(size, &attr->space);
140 return false;
143 if (attr->interval > 1) {
144 attr->count++;
145 if (attr->count % attr->interval)
146 return false;
149 if (attr->probability <= get_random_u32_below(100))
150 return false;
152 fail:
153 if (!(flags & FAULT_NOWARN))
154 fail_dump(attr);
156 if (atomic_read(&attr->times) != -1)
157 atomic_dec_not_zero(&attr->times);
159 return true;
162 bool should_fail(struct fault_attr *attr, ssize_t size)
164 return should_fail_ex(attr, size, 0);
166 EXPORT_SYMBOL_GPL(should_fail);
168 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
170 static int debugfs_ul_set(void *data, u64 val)
172 *(unsigned long *)data = val;
173 return 0;
176 static int debugfs_ul_get(void *data, u64 *val)
178 *val = *(unsigned long *)data;
179 return 0;
182 DEFINE_SIMPLE_ATTRIBUTE(fops_ul, debugfs_ul_get, debugfs_ul_set, "%llu\n");
184 static void debugfs_create_ul(const char *name, umode_t mode,
185 struct dentry *parent, unsigned long *value)
187 debugfs_create_file(name, mode, parent, value, &fops_ul);
190 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
192 static int debugfs_stacktrace_depth_set(void *data, u64 val)
194 *(unsigned long *)data =
195 min_t(unsigned long, val, MAX_STACK_TRACE_DEPTH);
197 return 0;
200 DEFINE_SIMPLE_ATTRIBUTE(fops_stacktrace_depth, debugfs_ul_get,
201 debugfs_stacktrace_depth_set, "%llu\n");
203 static void debugfs_create_stacktrace_depth(const char *name, umode_t mode,
204 struct dentry *parent,
205 unsigned long *value)
207 debugfs_create_file(name, mode, parent, value, &fops_stacktrace_depth);
210 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
212 struct dentry *fault_create_debugfs_attr(const char *name,
213 struct dentry *parent, struct fault_attr *attr)
215 umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
216 struct dentry *dir;
218 dir = debugfs_create_dir(name, parent);
219 if (IS_ERR(dir))
220 return dir;
222 debugfs_create_ul("probability", mode, dir, &attr->probability);
223 debugfs_create_ul("interval", mode, dir, &attr->interval);
224 debugfs_create_atomic_t("times", mode, dir, &attr->times);
225 debugfs_create_atomic_t("space", mode, dir, &attr->space);
226 debugfs_create_ul("verbose", mode, dir, &attr->verbose);
227 debugfs_create_u32("verbose_ratelimit_interval_ms", mode, dir,
228 &attr->ratelimit_state.interval);
229 debugfs_create_u32("verbose_ratelimit_burst", mode, dir,
230 &attr->ratelimit_state.burst);
231 debugfs_create_bool("task-filter", mode, dir, &attr->task_filter);
233 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
234 debugfs_create_stacktrace_depth("stacktrace-depth", mode, dir,
235 &attr->stacktrace_depth);
236 debugfs_create_xul("require-start", mode, dir, &attr->require_start);
237 debugfs_create_xul("require-end", mode, dir, &attr->require_end);
238 debugfs_create_xul("reject-start", mode, dir, &attr->reject_start);
239 debugfs_create_xul("reject-end", mode, dir, &attr->reject_end);
240 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
242 attr->dname = dget(dir);
243 return dir;
245 EXPORT_SYMBOL_GPL(fault_create_debugfs_attr);
247 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
249 #ifdef CONFIG_FAULT_INJECTION_CONFIGFS
251 /* These configfs attribute utilities are copied from drivers/block/null_blk/main.c */
253 static ssize_t fault_uint_attr_show(unsigned int val, char *page)
255 return snprintf(page, PAGE_SIZE, "%u\n", val);
258 static ssize_t fault_ulong_attr_show(unsigned long val, char *page)
260 return snprintf(page, PAGE_SIZE, "%lu\n", val);
263 static ssize_t fault_bool_attr_show(bool val, char *page)
265 return snprintf(page, PAGE_SIZE, "%u\n", val);
268 static ssize_t fault_atomic_t_attr_show(atomic_t val, char *page)
270 return snprintf(page, PAGE_SIZE, "%d\n", atomic_read(&val));
273 static ssize_t fault_uint_attr_store(unsigned int *val, const char *page, size_t count)
275 unsigned int tmp;
276 int result;
278 result = kstrtouint(page, 0, &tmp);
279 if (result < 0)
280 return result;
282 *val = tmp;
283 return count;
286 static ssize_t fault_ulong_attr_store(unsigned long *val, const char *page, size_t count)
288 int result;
289 unsigned long tmp;
291 result = kstrtoul(page, 0, &tmp);
292 if (result < 0)
293 return result;
295 *val = tmp;
296 return count;
299 static ssize_t fault_bool_attr_store(bool *val, const char *page, size_t count)
301 bool tmp;
302 int result;
304 result = kstrtobool(page, &tmp);
305 if (result < 0)
306 return result;
308 *val = tmp;
309 return count;
312 static ssize_t fault_atomic_t_attr_store(atomic_t *val, const char *page, size_t count)
314 int tmp;
315 int result;
317 result = kstrtoint(page, 0, &tmp);
318 if (result < 0)
319 return result;
321 atomic_set(val, tmp);
322 return count;
325 #define CONFIGFS_ATTR_NAMED(_pfx, _name, _attr_name) \
326 static struct configfs_attribute _pfx##attr_##_name = { \
327 .ca_name = _attr_name, \
328 .ca_mode = 0644, \
329 .ca_owner = THIS_MODULE, \
330 .show = _pfx##_name##_show, \
331 .store = _pfx##_name##_store, \
334 static struct fault_config *to_fault_config(struct config_item *item)
336 return container_of(to_config_group(item), struct fault_config, group);
339 #define FAULT_CONFIGFS_ATTR_NAMED(NAME, ATTR_NAME, MEMBER, TYPE) \
340 static ssize_t fault_##NAME##_show(struct config_item *item, char *page) \
342 return fault_##TYPE##_attr_show(to_fault_config(item)->attr.MEMBER, page); \
344 static ssize_t fault_##NAME##_store(struct config_item *item, const char *page, size_t count) \
346 struct fault_config *config = to_fault_config(item); \
347 return fault_##TYPE##_attr_store(&config->attr.MEMBER, page, count); \
349 CONFIGFS_ATTR_NAMED(fault_, NAME, ATTR_NAME)
351 #define FAULT_CONFIGFS_ATTR(NAME, TYPE) \
352 FAULT_CONFIGFS_ATTR_NAMED(NAME, __stringify(NAME), NAME, TYPE)
354 FAULT_CONFIGFS_ATTR(probability, ulong);
355 FAULT_CONFIGFS_ATTR(interval, ulong);
356 FAULT_CONFIGFS_ATTR(times, atomic_t);
357 FAULT_CONFIGFS_ATTR(space, atomic_t);
358 FAULT_CONFIGFS_ATTR(verbose, ulong);
359 FAULT_CONFIGFS_ATTR_NAMED(ratelimit_interval, "verbose_ratelimit_interval_ms",
360 ratelimit_state.interval, uint);
361 FAULT_CONFIGFS_ATTR_NAMED(ratelimit_burst, "verbose_ratelimit_burst",
362 ratelimit_state.burst, uint);
363 FAULT_CONFIGFS_ATTR_NAMED(task_filter, "task-filter", task_filter, bool);
365 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
367 static ssize_t fault_stacktrace_depth_show(struct config_item *item, char *page)
369 return fault_ulong_attr_show(to_fault_config(item)->attr.stacktrace_depth, page);
372 static ssize_t fault_stacktrace_depth_store(struct config_item *item, const char *page,
373 size_t count)
375 int result;
376 unsigned long tmp;
378 result = kstrtoul(page, 0, &tmp);
379 if (result < 0)
380 return result;
382 to_fault_config(item)->attr.stacktrace_depth =
383 min_t(unsigned long, tmp, MAX_STACK_TRACE_DEPTH);
385 return count;
388 CONFIGFS_ATTR_NAMED(fault_, stacktrace_depth, "stacktrace-depth");
390 static ssize_t fault_xul_attr_show(unsigned long val, char *page)
392 return snprintf(page, PAGE_SIZE,
393 sizeof(val) == sizeof(u32) ? "0x%08lx\n" : "0x%016lx\n", val);
396 static ssize_t fault_xul_attr_store(unsigned long *val, const char *page, size_t count)
398 return fault_ulong_attr_store(val, page, count);
401 FAULT_CONFIGFS_ATTR_NAMED(require_start, "require-start", require_start, xul);
402 FAULT_CONFIGFS_ATTR_NAMED(require_end, "require-end", require_end, xul);
403 FAULT_CONFIGFS_ATTR_NAMED(reject_start, "reject-start", reject_start, xul);
404 FAULT_CONFIGFS_ATTR_NAMED(reject_end, "reject-end", reject_end, xul);
406 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
408 static struct configfs_attribute *fault_config_attrs[] = {
409 &fault_attr_probability,
410 &fault_attr_interval,
411 &fault_attr_times,
412 &fault_attr_space,
413 &fault_attr_verbose,
414 &fault_attr_ratelimit_interval,
415 &fault_attr_ratelimit_burst,
416 &fault_attr_task_filter,
417 #ifdef CONFIG_FAULT_INJECTION_STACKTRACE_FILTER
418 &fault_attr_stacktrace_depth,
419 &fault_attr_require_start,
420 &fault_attr_require_end,
421 &fault_attr_reject_start,
422 &fault_attr_reject_end,
423 #endif /* CONFIG_FAULT_INJECTION_STACKTRACE_FILTER */
424 NULL,
427 static const struct config_item_type fault_config_type = {
428 .ct_attrs = fault_config_attrs,
429 .ct_owner = THIS_MODULE,
432 void fault_config_init(struct fault_config *config, const char *name)
434 config_group_init_type_name(&config->group, name, &fault_config_type);
436 EXPORT_SYMBOL_GPL(fault_config_init);
438 #endif /* CONFIG_FAULT_INJECTION_CONFIGFS */