blob: b135ebb88b6f729e145430999591beb3db2371d2 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Akinobu Mita773ff602008-12-23 19:37:01 +09002#include <linux/fault-inject.h>
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +03003#include <linux/slab.h>
Jesper Dangaard Brouerfab99632016-03-15 14:53:38 -07004#include <linux/mm.h>
5#include "slab.h"
Akinobu Mita773ff602008-12-23 19:37:01 +09006
7static struct {
8 struct fault_attr attr;
Mel Gorman71baba42015-11-06 16:28:28 -08009 bool ignore_gfp_reclaim;
Viresh Kumar621a5f72015-09-26 15:04:07 -070010 bool cache_filter;
Akinobu Mita773ff602008-12-23 19:37:01 +090011} failslab = {
12 .attr = FAULT_ATTR_INITIALIZER,
Mel Gorman71baba42015-11-06 16:28:28 -080013 .ignore_gfp_reclaim = true,
Viresh Kumar621a5f72015-09-26 15:04:07 -070014 .cache_filter = false,
Akinobu Mita773ff602008-12-23 19:37:01 +090015};
16
Howard McLauchlan4f6923f2018-04-05 16:23:57 -070017bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags)
Akinobu Mita773ff602008-12-23 19:37:01 +090018{
Jesper Dangaard Brouerfab99632016-03-15 14:53:38 -070019 /* No fault-injection for bootstrap cache */
20 if (unlikely(s == kmem_cache))
21 return false;
22
Akinobu Mita773ff602008-12-23 19:37:01 +090023 if (gfpflags & __GFP_NOFAIL)
24 return false;
25
Mel Gorman71baba42015-11-06 16:28:28 -080026 if (failslab.ignore_gfp_reclaim && (gfpflags & __GFP_RECLAIM))
Akinobu Mita773ff602008-12-23 19:37:01 +090027 return false;
28
Jesper Dangaard Brouerfab99632016-03-15 14:53:38 -070029 if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB))
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +030030 return false;
31
Jesper Dangaard Brouerfab99632016-03-15 14:53:38 -070032 return should_fail(&failslab.attr, s->object_size);
Akinobu Mita773ff602008-12-23 19:37:01 +090033}
34
35static 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 Mita773ff602008-12-23 19:37:01 +090042static int __init failslab_debugfs_init(void)
43{
Akinobu Mitadd48c082011-08-03 16:21:01 -070044 struct dentry *dir;
Joe Perches0825a6f2018-06-14 15:27:58 -070045 umode_t mode = S_IFREG | 0600;
Akinobu Mita773ff602008-12-23 19:37:01 +090046
Akinobu Mitadd48c082011-08-03 16:21:01 -070047 dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr);
48 if (IS_ERR(dir))
49 return PTR_ERR(dir);
Akinobu Mita773ff602008-12-23 19:37:01 +090050
Akinobu Mitadd48c082011-08-03 16:21:01 -070051 if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
Mel Gorman71baba42015-11-06 16:28:28 -080052 &failslab.ignore_gfp_reclaim))
Akinobu Mita810f09b2011-07-26 16:09:02 -070053 goto fail;
Akinobu Mitadd48c082011-08-03 16:21:01 -070054 if (!debugfs_create_bool("cache-filter", mode, dir,
Akinobu Mita810f09b2011-07-26 16:09:02 -070055 &failslab.cache_filter))
56 goto fail;
Akinobu Mita773ff602008-12-23 19:37:01 +090057
Akinobu Mita810f09b2011-07-26 16:09:02 -070058 return 0;
59fail:
Akinobu Mitadd48c082011-08-03 16:21:01 -070060 debugfs_remove_recursive(dir);
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +030061
Akinobu Mita810f09b2011-07-26 16:09:02 -070062 return -ENOMEM;
Akinobu Mita773ff602008-12-23 19:37:01 +090063}
64
65late_initcall(failslab_debugfs_init);
66
67#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */