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 | 4f6923f | 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 | |
Nicolas Boichat | a965947 | 2019-07-11 20:55:03 -0700 | [diff] [blame] | 26 | if (failslab.ignore_gfp_reclaim && |
| 27 | (gfpflags & __GFP_DIRECT_RECLAIM)) |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 28 | return false; |
| 29 | |
Jesper Dangaard Brouer | fab9963 | 2016-03-15 14:53:38 -0700 | [diff] [blame] | 30 | if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB)) |
Dmitry Monakhov | 4c13dd3 | 2010-02-26 09:36:12 +0300 | [diff] [blame] | 31 | return false; |
| 32 | |
Jesper Dangaard Brouer | fab9963 | 2016-03-15 14:53:38 -0700 | [diff] [blame] | 33 | return should_fail(&failslab.attr, s->object_size); |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | static int __init setup_failslab(char *str) |
| 37 | { |
| 38 | return setup_fault_attr(&failslab.attr, str); |
| 39 | } |
| 40 | __setup("failslab=", setup_failslab); |
| 41 | |
| 42 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 43 | static int __init failslab_debugfs_init(void) |
| 44 | { |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 45 | struct dentry *dir; |
Joe Perches | 0825a6f | 2018-06-14 15:27:58 -0700 | [diff] [blame] | 46 | umode_t mode = S_IFREG | 0600; |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 47 | |
Akinobu Mita | dd48c08 | 2011-08-03 16:21:01 -0700 | [diff] [blame] | 48 | dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr); |
| 49 | if (IS_ERR(dir)) |
| 50 | return PTR_ERR(dir); |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 51 | |
Greg Kroah-Hartman | d9f7979 | 2019-03-05 15:46:09 -0800 | [diff] [blame] | 52 | debugfs_create_bool("ignore-gfp-wait", mode, dir, |
| 53 | &failslab.ignore_gfp_reclaim); |
| 54 | debugfs_create_bool("cache-filter", mode, dir, |
| 55 | &failslab.cache_filter); |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 56 | |
Akinobu Mita | 810f09b | 2011-07-26 16:09:02 -0700 | [diff] [blame] | 57 | return 0; |
Akinobu Mita | 773ff60 | 2008-12-23 19:37:01 +0900 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | late_initcall(failslab_debugfs_init); |
| 61 | |
| 62 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ |