Commit | Line | Data |
---|---|---|
b2441318 | 1 | // SPDX-License-Identifier: GPL-2.0 |
773ff60e | 2 | #include <linux/fault-inject.h> |
a7526fe8 | 3 | #include <linux/error-injection.h> |
6ce2082f | 4 | #include <linux/debugfs.h> |
4c13dd3b | 5 | #include <linux/slab.h> |
fab9963a JDB |
6 | #include <linux/mm.h> |
7 | #include "slab.h" | |
773ff60e AM |
8 | |
9 | static struct { | |
10 | struct fault_attr attr; | |
71baba4b | 11 | bool ignore_gfp_reclaim; |
621a5f7a | 12 | bool cache_filter; |
773ff60e AM |
13 | } failslab = { |
14 | .attr = FAULT_ATTR_INITIALIZER, | |
71baba4b | 15 | .ignore_gfp_reclaim = true, |
621a5f7a | 16 | .cache_filter = false, |
773ff60e AM |
17 | }; |
18 | ||
a7526fe8 | 19 | int should_failslab(struct kmem_cache *s, gfp_t gfpflags) |
773ff60e | 20 | { |
ea4452de QZ |
21 | int flags = 0; |
22 | ||
fab9963a JDB |
23 | /* No fault-injection for bootstrap cache */ |
24 | if (unlikely(s == kmem_cache)) | |
a7526fe8 | 25 | return 0; |
fab9963a | 26 | |
773ff60e | 27 | if (gfpflags & __GFP_NOFAIL) |
a7526fe8 | 28 | return 0; |
773ff60e | 29 | |
a9659476 NB |
30 | if (failslab.ignore_gfp_reclaim && |
31 | (gfpflags & __GFP_DIRECT_RECLAIM)) | |
a7526fe8 | 32 | return 0; |
773ff60e | 33 | |
fab9963a | 34 | if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB)) |
a7526fe8 | 35 | return 0; |
4c13dd3b | 36 | |
ea4452de QZ |
37 | /* |
38 | * In some cases, it expects to specify __GFP_NOWARN | |
39 | * to avoid printing any information(not just a warning), | |
40 | * thus avoiding deadlocks. See commit 6b9dbedbe349 for | |
41 | * details. | |
42 | */ | |
3f913fc5 | 43 | if (gfpflags & __GFP_NOWARN) |
ea4452de | 44 | flags |= FAULT_NOWARN; |
3f913fc5 | 45 | |
a7526fe8 | 46 | return should_fail_ex(&failslab.attr, s->object_size, flags) ? -ENOMEM : 0; |
773ff60e | 47 | } |
a7526fe8 | 48 | ALLOW_ERROR_INJECTION(should_failslab, ERRNO); |
773ff60e AM |
49 | |
50 | static int __init setup_failslab(char *str) | |
51 | { | |
52 | return setup_fault_attr(&failslab.attr, str); | |
53 | } | |
54 | __setup("failslab=", setup_failslab); | |
55 | ||
56 | #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS | |
773ff60e AM |
57 | static int __init failslab_debugfs_init(void) |
58 | { | |
dd48c085 | 59 | struct dentry *dir; |
0825a6f9 | 60 | umode_t mode = S_IFREG | 0600; |
773ff60e | 61 | |
dd48c085 AM |
62 | dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr); |
63 | if (IS_ERR(dir)) | |
64 | return PTR_ERR(dir); | |
773ff60e | 65 | |
d9f7979c GKH |
66 | debugfs_create_bool("ignore-gfp-wait", mode, dir, |
67 | &failslab.ignore_gfp_reclaim); | |
68 | debugfs_create_bool("cache-filter", mode, dir, | |
69 | &failslab.cache_filter); | |
4c13dd3b | 70 | |
810f09b8 | 71 | return 0; |
773ff60e AM |
72 | } |
73 | ||
74 | late_initcall(failslab_debugfs_init); | |
75 | ||
76 | #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */ |