xref: /linux/mm/fail_page_alloc.c (revision c532de5a67a70f8533d495f8f2aaa9a0491c3ad0)
1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/fault-inject.h>
3 #include <linux/debugfs.h>
4 #include <linux/error-injection.h>
5 #include <linux/mm.h>
6 
7 static struct {
8 	struct fault_attr attr;
9 
10 	bool ignore_gfp_highmem;
11 	bool ignore_gfp_reclaim;
12 	u32 min_order;
13 } fail_page_alloc = {
14 	.attr = FAULT_ATTR_INITIALIZER,
15 	.ignore_gfp_reclaim = true,
16 	.ignore_gfp_highmem = true,
17 	.min_order = 1,
18 };
19 
20 static int __init setup_fail_page_alloc(char *str)
21 {
22 	return setup_fault_attr(&fail_page_alloc.attr, str);
23 }
24 __setup("fail_page_alloc=", setup_fail_page_alloc);
25 
26 bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
27 {
28 	int flags = 0;
29 
30 	if (order < fail_page_alloc.min_order)
31 		return false;
32 	if (gfp_mask & __GFP_NOFAIL)
33 		return false;
34 	if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
35 		return false;
36 	if (fail_page_alloc.ignore_gfp_reclaim &&
37 			(gfp_mask & __GFP_DIRECT_RECLAIM))
38 		return false;
39 
40 	/* See comment in __should_failslab() */
41 	if (gfp_mask & __GFP_NOWARN)
42 		flags |= FAULT_NOWARN;
43 
44 	return should_fail_ex(&fail_page_alloc.attr, 1 << order, flags);
45 }
46 ALLOW_ERROR_INJECTION(should_fail_alloc_page, TRUE);
47 
48 #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
49 
50 static int __init fail_page_alloc_debugfs(void)
51 {
52 	umode_t mode = S_IFREG | 0600;
53 	struct dentry *dir;
54 
55 	dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
56 					&fail_page_alloc.attr);
57 
58 	debugfs_create_bool("ignore-gfp-wait", mode, dir,
59 			    &fail_page_alloc.ignore_gfp_reclaim);
60 	debugfs_create_bool("ignore-gfp-highmem", mode, dir,
61 			    &fail_page_alloc.ignore_gfp_highmem);
62 	debugfs_create_u32("min-order", mode, dir, &fail_page_alloc.min_order);
63 
64 	return 0;
65 }
66 
67 late_initcall(fail_page_alloc_debugfs);
68 
69 #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */
70