failslab.c 1.45 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
Akinobu Mita's avatar
Akinobu Mita committed
2
#include <linux/fault-inject.h>
3
#include <linux/slab.h>
4
5
#include <linux/mm.h>
#include "slab.h"
Akinobu Mita's avatar
Akinobu Mita committed
6
7
8

static struct {
	struct fault_attr attr;
9
	bool ignore_gfp_reclaim;
10
	bool cache_filter;
Akinobu Mita's avatar
Akinobu Mita committed
11
12
} failslab = {
	.attr = FAULT_ATTR_INITIALIZER,
13
	.ignore_gfp_reclaim = true,
14
	.cache_filter = false,
Akinobu Mita's avatar
Akinobu Mita committed
15
16
};

17
bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags)
Akinobu Mita's avatar
Akinobu Mita committed
18
{
19
20
21
22
	/* No fault-injection for bootstrap cache */
	if (unlikely(s == kmem_cache))
		return false;

Akinobu Mita's avatar
Akinobu Mita committed
23
24
25
	if (gfpflags & __GFP_NOFAIL)
		return false;

26
	if (failslab.ignore_gfp_reclaim && (gfpflags & __GFP_RECLAIM))
Akinobu Mita's avatar
Akinobu Mita committed
27
28
		return false;

29
	if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB))
30
31
		return false;

32
	return should_fail(&failslab.attr, s->object_size);
Akinobu Mita's avatar
Akinobu Mita committed
33
34
35
36
37
38
39
40
41
42
43
}

static int __init setup_failslab(char *str)
{
	return setup_fault_attr(&failslab.attr, str);
}
__setup("failslab=", setup_failslab);

#ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
static int __init failslab_debugfs_init(void)
{
44
	struct dentry *dir;
45
	umode_t mode = S_IFREG | 0600;
Akinobu Mita's avatar
Akinobu Mita committed
46

47
48
49
	dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr);
	if (IS_ERR(dir))
		return PTR_ERR(dir);
Akinobu Mita's avatar
Akinobu Mita committed
50

51
	if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
52
				&failslab.ignore_gfp_reclaim))
53
		goto fail;
54
	if (!debugfs_create_bool("cache-filter", mode, dir,
55
56
				&failslab.cache_filter))
		goto fail;
57

58
59
	return 0;
fail:
60
	debugfs_remove_recursive(dir);
Akinobu Mita's avatar
Akinobu Mita committed
61

62
	return -ENOMEM;
Akinobu Mita's avatar
Akinobu Mita committed
63
64
65
66
67
}

late_initcall(failslab_debugfs_init);

#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */