failslab.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/fault-inject.h>
  3. #include <linux/error-injection.h>
  4. #include <linux/debugfs.h>
  5. #include <linux/slab.h>
  6. #include <linux/mm.h>
  7. #include "slab.h"
  8. static struct {
  9. struct fault_attr attr;
  10. bool ignore_gfp_reclaim;
  11. bool cache_filter;
  12. } failslab = {
  13. .attr = FAULT_ATTR_INITIALIZER,
  14. .ignore_gfp_reclaim = true,
  15. .cache_filter = false,
  16. };
  17. int should_failslab(struct kmem_cache *s, gfp_t gfpflags)
  18. {
  19. int flags = 0;
  20. /* No fault-injection for bootstrap cache */
  21. if (unlikely(s == kmem_cache))
  22. return 0;
  23. if (gfpflags & __GFP_NOFAIL)
  24. return 0;
  25. if (failslab.ignore_gfp_reclaim &&
  26. (gfpflags & __GFP_DIRECT_RECLAIM))
  27. return 0;
  28. if (failslab.cache_filter && !(s->flags & SLAB_FAILSLAB))
  29. return 0;
  30. /*
  31. * In some cases, it expects to specify __GFP_NOWARN
  32. * to avoid printing any information(not just a warning),
  33. * thus avoiding deadlocks. See commit 6b9dbedbe349 for
  34. * details.
  35. */
  36. if (gfpflags & __GFP_NOWARN)
  37. flags |= FAULT_NOWARN;
  38. return should_fail_ex(&failslab.attr, s->object_size, flags) ? -ENOMEM : 0;
  39. }
  40. ALLOW_ERROR_INJECTION(should_failslab, ERRNO);
  41. static int __init setup_failslab(char *str)
  42. {
  43. return setup_fault_attr(&failslab.attr, str);
  44. }
  45. __setup("failslab=", setup_failslab);
  46. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  47. static int __init failslab_debugfs_init(void)
  48. {
  49. struct dentry *dir;
  50. umode_t mode = S_IFREG | 0600;
  51. dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr);
  52. if (IS_ERR(dir))
  53. return PTR_ERR(dir);
  54. debugfs_create_bool("ignore-gfp-wait", mode, dir,
  55. &failslab.ignore_gfp_reclaim);
  56. debugfs_create_bool("cache-filter", mode, dir,
  57. &failslab.cache_filter);
  58. return 0;
  59. }
  60. late_initcall(failslab_debugfs_init);
  61. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */