failslab.c 1.4 KB

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