blob: f92fed91ac2360aa9dafef57f773b882ab6c4251 [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
Nicolas Boichata9659472019-07-11 20:55:03 -070026 if (failslab.ignore_gfp_reclaim &&
27 (gfpflags & __GFP_DIRECT_RECLAIM))
Akinobu Mita773ff602008-12-23 19:37:01 +090028 return false;
29
Jesper Dangaard Brouerfab99632016-03-15 14:53:38 -070030 if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB))
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +030031 return false;
32
Jesper Dangaard Brouerfab99632016-03-15 14:53:38 -070033 return should_fail(&failslab.attr, s->object_size);
Akinobu Mita773ff602008-12-23 19:37:01 +090034}
35
36static 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 Mita773ff602008-12-23 19:37:01 +090043static int __init failslab_debugfs_init(void)
44{
Akinobu Mitadd48c082011-08-03 16:21:01 -070045 struct dentry *dir;
Joe Perches0825a6f2018-06-14 15:27:58 -070046 umode_t mode = S_IFREG | 0600;
Akinobu Mita773ff602008-12-23 19:37:01 +090047
Akinobu Mitadd48c082011-08-03 16:21:01 -070048 dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr);
49 if (IS_ERR(dir))
50 return PTR_ERR(dir);
Akinobu Mita773ff602008-12-23 19:37:01 +090051
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -080052 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 Mita773ff602008-12-23 19:37:01 +090056
Akinobu Mita810f09b2011-07-26 16:09:02 -070057 return 0;
Akinobu Mita773ff602008-12-23 19:37:01 +090058}
59
60late_initcall(failslab_debugfs_init);
61
62#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */