blob: bb41f98dd8b700d7ecf1765aa7e76e6cbd6ad97a [file] [log] [blame]
Akinobu Mita773ff602008-12-23 19:37:01 +09001#include <linux/fault-inject.h>
Pekka Enberg255d11b2009-03-24 11:59:21 +02002#include <linux/gfp.h>
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03003#include <linux/slab.h>
Akinobu Mita773ff602008-12-23 19:37:01 +09004
5static struct {
6 struct fault_attr attr;
7 u32 ignore_gfp_wait;
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03008 int cache_filter;
Akinobu Mita773ff602008-12-23 19:37:01 +09009#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
10 struct dentry *ignore_gfp_wait_file;
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +030011 struct dentry *cache_filter_file;
Akinobu Mita773ff602008-12-23 19:37:01 +090012#endif
13} failslab = {
14 .attr = FAULT_ATTR_INITIALIZER,
15 .ignore_gfp_wait = 1,
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +030016 .cache_filter = 0,
Akinobu Mita773ff602008-12-23 19:37:01 +090017};
18
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +030019bool should_failslab(size_t size, gfp_t gfpflags, unsigned long cache_flags)
Akinobu Mita773ff602008-12-23 19:37:01 +090020{
21 if (gfpflags & __GFP_NOFAIL)
22 return false;
23
24 if (failslab.ignore_gfp_wait && (gfpflags & __GFP_WAIT))
25 return false;
26
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +030027 if (failslab.cache_filter && !(cache_flags & SLAB_FAILSLAB))
28 return false;
29
Akinobu Mita773ff602008-12-23 19:37:01 +090030 return should_fail(&failslab.attr, size);
31}
32
33static int __init setup_failslab(char *str)
34{
35 return setup_fault_attr(&failslab.attr, str);
36}
37__setup("failslab=", setup_failslab);
38
39#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
Akinobu Mita773ff602008-12-23 19:37:01 +090040static int __init failslab_debugfs_init(void)
41{
42 mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
43 struct dentry *dir;
44 int err;
45
46 err = init_fault_attr_dentries(&failslab.attr, "failslab");
47 if (err)
48 return err;
49 dir = failslab.attr.dentries.dir;
50
51 failslab.ignore_gfp_wait_file =
52 debugfs_create_bool("ignore-gfp-wait", mode, dir,
53 &failslab.ignore_gfp_wait);
54
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +030055 failslab.cache_filter_file =
56 debugfs_create_bool("cache-filter", mode, dir,
57 &failslab.cache_filter);
58
59 if (!failslab.ignore_gfp_wait_file ||
60 !failslab.cache_filter_file) {
Akinobu Mita773ff602008-12-23 19:37:01 +090061 err = -ENOMEM;
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +030062 debugfs_remove(failslab.cache_filter_file);
Akinobu Mita773ff602008-12-23 19:37:01 +090063 debugfs_remove(failslab.ignore_gfp_wait_file);
64 cleanup_fault_attr_dentries(&failslab.attr);
65 }
66
67 return err;
68}
69
70late_initcall(failslab_debugfs_init);
71
72#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */