^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/fault-inject.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/mm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include "slab.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) static struct {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) struct fault_attr attr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) bool ignore_gfp_reclaim;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) bool cache_filter;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) } failslab = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) .attr = FAULT_ATTR_INITIALIZER,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) .ignore_gfp_reclaim = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) .cache_filter = false,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) bool __should_failslab(struct kmem_cache *s, gfp_t gfpflags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) /* No fault-injection for bootstrap cache */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) if (unlikely(s == kmem_cache))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) if (gfpflags & __GFP_NOFAIL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) if (failslab.ignore_gfp_reclaim &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) (gfpflags & __GFP_DIRECT_RECLAIM))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) return false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return should_fail(&failslab.attr, s->object_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static int __init setup_failslab(char *str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) return setup_fault_attr(&failslab.attr, str);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) __setup("failslab=", setup_failslab);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) static int __init failslab_debugfs_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) struct dentry *dir;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) umode_t mode = S_IFREG | 0600;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) if (IS_ERR(dir))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) return PTR_ERR(dir);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) debugfs_create_bool("ignore-gfp-wait", mode, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) &failslab.ignore_gfp_reclaim);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) debugfs_create_bool("cache-filter", mode, dir,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) &failslab.cache_filter);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) late_initcall(failslab_debugfs_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */