failslab.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #include <linux/fault-inject.h>
  2. #include <linux/slab.h>
  3. static struct {
  4. struct fault_attr attr;
  5. u32 ignore_gfp_wait;
  6. int cache_filter;
  7. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  8. struct dentry *ignore_gfp_wait_file;
  9. struct dentry *cache_filter_file;
  10. #endif
  11. } failslab = {
  12. .attr = FAULT_ATTR_INITIALIZER,
  13. .ignore_gfp_wait = 1,
  14. .cache_filter = 0,
  15. };
  16. bool should_failslab(size_t size, gfp_t gfpflags, unsigned long cache_flags)
  17. {
  18. if (gfpflags & __GFP_NOFAIL)
  19. return false;
  20. if (failslab.ignore_gfp_wait && (gfpflags & __GFP_WAIT))
  21. return false;
  22. if (failslab.cache_filter && !(cache_flags & SLAB_FAILSLAB))
  23. return false;
  24. return should_fail(&failslab.attr, size);
  25. }
  26. static int __init setup_failslab(char *str)
  27. {
  28. return setup_fault_attr(&failslab.attr, str);
  29. }
  30. __setup("failslab=", setup_failslab);
  31. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  32. static int __init failslab_debugfs_init(void)
  33. {
  34. mode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  35. struct dentry *dir;
  36. int err;
  37. err = init_fault_attr_dentries(&failslab.attr, "failslab");
  38. if (err)
  39. return err;
  40. dir = failslab.attr.dentries.dir;
  41. failslab.ignore_gfp_wait_file =
  42. debugfs_create_bool("ignore-gfp-wait", mode, dir,
  43. &failslab.ignore_gfp_wait);
  44. failslab.cache_filter_file =
  45. debugfs_create_bool("cache-filter", mode, dir,
  46. &failslab.cache_filter);
  47. if (!failslab.ignore_gfp_wait_file ||
  48. !failslab.cache_filter_file) {
  49. err = -ENOMEM;
  50. debugfs_remove(failslab.cache_filter_file);
  51. debugfs_remove(failslab.ignore_gfp_wait_file);
  52. cleanup_fault_attr_dentries(&failslab.attr);
  53. }
  54. return err;
  55. }
  56. late_initcall(failslab_debugfs_init);
  57. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */