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