xref: /openbmc/linux/mm/failslab.c (revision 2fa5ebe3)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fault-inject.h>
3 #include <linux/slab.h>
4 #include <linux/mm.h>
5 #include "slab.h"
6 
7 static struct {
8 	struct fault_attr attr;
9 	bool ignore_gfp_reclaim;
10 	bool cache_filter;
11 } failslab = {
12 	.attr = FAULT_ATTR_INITIALIZER,
13 	.ignore_gfp_reclaim = true,
14 	.cache_filter = false,
15 };
16 
17 bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags)
18 {
19 	int flags = 0;
20 
21 	/* No fault-injection for bootstrap cache */
22 	if (unlikely(s == kmem_cache))
23 		return false;
24 
25 	if (gfpflags & __GFP_NOFAIL)
26 		return false;
27 
28 	if (failslab.ignore_gfp_reclaim &&
29 			(gfpflags & __GFP_DIRECT_RECLAIM))
30 		return false;
31 
32 	if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB))
33 		return false;
34 
35 	/*
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 	 */
41 	if (gfpflags & __GFP_NOWARN)
42 		flags |= FAULT_NOWARN;
43 
44 	return should_fail_ex(&failslab.attr, s->object_size, flags);
45 }
46 
47 static 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
54 static int __init failslab_debugfs_init(void)
55 {
56 	struct dentry *dir;
57 	umode_t mode = S_IFREG | 0600;
58 
59 	dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr);
60 	if (IS_ERR(dir))
61 		return PTR_ERR(dir);
62 
63 	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);
67 
68 	return 0;
69 }
70 
71 late_initcall(failslab_debugfs_init);
72 
73 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
74