blob: ffc420c0e767f097380e587cb833eb495a484d49 [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 McLauchlan4f6923fb2018-04-05 16:23:57 -070017bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags)
Akinobu Mita773ff602008-12-23 19:37:01 +090018{
Qi Zhengea4452d2022-11-18 18:00:11 +080019 int flags = 0;
20
Jesper Dangaard Brouerfab99632016-03-15 14:53:38 -070021 /* No fault-injection for bootstrap cache */
22 if (unlikely(s == kmem_cache))
23 return false;
24
Akinobu Mita773ff602008-12-23 19:37:01 +090025 if (gfpflags & __GFP_NOFAIL)
26 return false;
27
Nicolas Boichata9659472019-07-11 20:55:03 -070028 if (failslab.ignore_gfp_reclaim &&
29 (gfpflags & __GFP_DIRECT_RECLAIM))
Akinobu Mita773ff602008-12-23 19:37:01 +090030 return false;
31
Jesper Dangaard Brouerfab99632016-03-15 14:53:38 -070032 if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB))
Dmitry Monakhov4c13dd32010-02-26 09:36:12 +030033 return false;
34
Qi Zhengea4452d2022-11-18 18:00:11 +080035 /*
36 * In some cases, it expects to specify __GFP_NOWARN
37 * to avoid printing any information(not just a warning),
38 * thus avoiding deadlocks. See commit 6b9dbedbe349 for
39 * details.
40 */
Qi Zheng3f913fc2022-05-19 14:08:55 -070041 if (gfpflags & __GFP_NOWARN)
Qi Zhengea4452d2022-11-18 18:00:11 +080042 flags |= FAULT_NOWARN;
Qi Zheng3f913fc2022-05-19 14:08:55 -070043
Qi Zhengea4452d2022-11-18 18:00:11 +080044 return should_fail_ex(&failslab.attr, s->object_size, flags);
Akinobu Mita773ff602008-12-23 19:37:01 +090045}
46
47static int __init setup_failslab(char *str)
48{
49 return setup_fault_attr(&failslab.attr, str);
50}
51__setup("failslab=", setup_failslab);
52
53#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
Akinobu Mita773ff602008-12-23 19:37:01 +090054static int __init failslab_debugfs_init(void)
55{
Akinobu Mitadd48c082011-08-03 16:21:01 -070056 struct dentry *dir;
Joe Perches0825a6f2018-06-14 15:27:58 -070057 umode_t mode = S_IFREG | 0600;
Akinobu Mita773ff602008-12-23 19:37:01 +090058
Akinobu Mitadd48c082011-08-03 16:21:01 -070059 dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr);
60 if (IS_ERR(dir))
61 return PTR_ERR(dir);
Akinobu Mita773ff602008-12-23 19:37:01 +090062
Greg Kroah-Hartmand9f79792019-03-05 15:46:09 -080063 debugfs_create_bool("ignore-gfp-wait", mode, dir,
64 &failslab.ignore_gfp_reclaim);
65 debugfs_create_bool("cache-filter", mode, dir,
66 &failslab.cache_filter);
Akinobu Mita773ff602008-12-23 19:37:01 +090067
Akinobu Mita810f09b2011-07-26 16:09:02 -070068 return 0;
Akinobu Mita773ff602008-12-23 19:37:01 +090069}
70
71late_initcall(failslab_debugfs_init);
72
73#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */