Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 1 | #include <linux/fault-inject.h> |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 2 | #include <linux/slab.h> |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 3 | |
| 4 | static struct { |
| 5 | struct fault_attr attr; |
| 6 | u32 ignore_gfp_wait; |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 7 | int cache_filter; |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 8 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS |
| 9 | struct dentry *ignore_gfp_wait_file; |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 10 | struct dentry *cache_filter_file; |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 11 | #endif |
| 12 | } failslab = { |
| 13 | .attr = FAULT_ATTR_INITIALIZER, |
| 14 | .ignore_gfp_wait = 1, |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 15 | .cache_filter = 0, |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 16 | }; |
| 17 | |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 18 | bool should_failslab(size_t size, gfp_t gfpflags, unsigned long cache_flags) |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 19 | { |
| 20 | if (gfpflags & __GFP_NOFAIL) |
| 21 | return false; |
| 22 | |
| 23 | if (failslab.ignore_gfp_wait && (gfpflags & __GFP_WAIT)) |
| 24 | return false; |
| 25 | |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 26 | if (failslab.cache_filter && !(cache_flags & SLAB_FAILSLAB)) |
| 27 | return false; |
| 28 | |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 29 | return should_fail(&failslab.attr, size); |
| 30 | } |
| 31 | |
| 32 | static int __init setup_failslab(char *str) |
| 33 | { |
| 34 | return setup_fault_attr(&failslab.attr, str); |
| 35 | } |
| 36 | __setup("failslab=", setup_failslab); |
| 37 | |
| 38 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 39 | static int __init failslab_debugfs_init(void) |
| 40 | { |
| 41 | mode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
| 42 | struct dentry *dir; |
| 43 | int err; |
| 44 | |
| 45 | err = init_fault_attr_dentries(&failslab.attr, "failslab"); |
| 46 | if (err) |
| 47 | return err; |
| 48 | dir = failslab.attr.dentries.dir; |
| 49 | |
| 50 | failslab.ignore_gfp_wait_file = |
| 51 | debugfs_create_bool("ignore-gfp-wait", mode, dir, |
| 52 | &failslab.ignore_gfp_wait); |
| 53 | |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 54 | failslab.cache_filter_file = |
| 55 | debugfs_create_bool("cache-filter", mode, dir, |
| 56 | &failslab.cache_filter); |
| 57 | |
| 58 | if (!failslab.ignore_gfp_wait_file || |
| 59 | !failslab.cache_filter_file) { |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 60 | err = -ENOMEM; |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 61 | debugfs_remove(failslab.cache_filter_file); |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 62 | debugfs_remove(failslab.ignore_gfp_wait_file); |
| 63 | cleanup_fault_attr_dentries(&failslab.attr); |
| 64 | } |
| 65 | |
| 66 | return err; |
| 67 | } |
| 68 | |
| 69 | late_initcall(failslab_debugfs_init); |
| 70 | |
| 71 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ |