Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 2 | #include <linux/fault-inject.h> |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 3 | #include <linux/slab.h> |
Jesper Dangaard Brouer | fab9963 | 2016-03-15 14:53:38 -0700 | [diff] [blame] | 4 | #include <linux/mm.h> |
| 5 | #include "slab.h" |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 6 | |
| 7 | static struct { |
| 8 | struct fault_attr attr; |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 9 | bool ignore_gfp_reclaim; |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 10 | bool cache_filter; |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 11 | } failslab = { |
| 12 | .attr = FAULT_ATTR_INITIALIZER, |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 13 | .ignore_gfp_reclaim = true, |
Viresh Kumar | 621a5f7 | 2015-09-26 15:04:07 -0700 | [diff] [blame] | 14 | .cache_filter = false, |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 15 | }; |
| 16 | |
Howard McLauchlan | 4f6923fb | 2018-04-05 16:23:57 -0700 | [diff] [blame^] | 17 | bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags) |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 18 | { |
Jesper Dangaard Brouer | fab9963 | 2016-03-15 14:53:38 -0700 | [diff] [blame] | 19 | /* No fault-injection for bootstrap cache */ |
| 20 | if (unlikely(s == kmem_cache)) |
| 21 | return false; |
| 22 | |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 23 | if (gfpflags & __GFP_NOFAIL) |
| 24 | return false; |
| 25 | |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 26 | if (failslab.ignore_gfp_reclaim && (gfpflags & __GFP_RECLAIM)) |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 27 | return false; |
| 28 | |
Jesper Dangaard Brouer | fab9963 | 2016-03-15 14:53:38 -0700 | [diff] [blame] | 29 | if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB)) |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 30 | return false; |
| 31 | |
Jesper Dangaard Brouer | fab9963 | 2016-03-15 14:53:38 -0700 | [diff] [blame] | 32 | return should_fail(&failslab.attr, s->object_size); |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | static int __init setup_failslab(char *str) |
| 36 | { |
| 37 | return setup_fault_attr(&failslab.attr, str); |
| 38 | } |
| 39 | __setup("failslab=", setup_failslab); |
| 40 | |
| 41 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 42 | static int __init failslab_debugfs_init(void) |
| 43 | { |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 44 | struct dentry *dir; |
Al Viro | f4ae40a6 | 2011-07-24 04:33:43 -0400 | [diff] [blame] | 45 | umode_t mode = S_IFREG | S_IRUSR | S_IWUSR; |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 46 | |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 47 | dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr); |
| 48 | if (IS_ERR(dir)) |
| 49 | return PTR_ERR(dir); |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 50 | |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 51 | if (!debugfs_create_bool("ignore-gfp-wait", mode, dir, |
Mel Gorman | 71baba4 | 2015-11-06 16:28:28 -0800 | [diff] [blame] | 52 | &failslab.ignore_gfp_reclaim)) |
Akinobu Mita | 810f09b | 2011-07-26 16:09:02 -0700 | [diff] [blame] | 53 | goto fail; |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 54 | if (!debugfs_create_bool("cache-filter", mode, dir, |
Akinobu Mita | 810f09b | 2011-07-26 16:09:02 -0700 | [diff] [blame] | 55 | &failslab.cache_filter)) |
| 56 | goto fail; |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 57 | |
Akinobu Mita | 810f09b | 2011-07-26 16:09:02 -0700 | [diff] [blame] | 58 | return 0; |
| 59 | fail: |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 60 | debugfs_remove_recursive(dir); |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 61 | |
Akinobu Mita | 810f09b | 2011-07-26 16:09:02 -0700 | [diff] [blame] | 62 | return -ENOMEM; |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | late_initcall(failslab_debugfs_init); |
| 66 | |
| 67 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ |