failslab.c 1.42 KB
Newer Older
Akinobu Mita's avatar
Akinobu Mita committed
1
#include <linux/fault-inject.h>
2
#include <linux/slab.h>
3 4
#include <linux/mm.h>
#include "slab.h"
Akinobu Mita's avatar
Akinobu Mita committed
5 6 7

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

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

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

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

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

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

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)
{
43
	struct dentry *dir;
Al Viro's avatar
Al Viro committed
44
	umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
Akinobu Mita's avatar
Akinobu Mita committed
45

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

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

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

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

late_initcall(failslab_debugfs_init);

#endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */