failslab.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #include <linux/fault-inject.h>
  2. #include <linux/slab.h>
  3. static struct {
  4. struct fault_attr attr;
  5. bool ignore_gfp_reclaim;
  6. bool cache_filter;
  7. } failslab = {
  8. .attr = FAULT_ATTR_INITIALIZER,
  9. .ignore_gfp_reclaim = true,
  10. .cache_filter = false,
  11. };
  12. bool should_failslab(size_t size, gfp_t gfpflags, unsigned long cache_flags)
  13. {
  14. if (gfpflags & __GFP_NOFAIL)
  15. return false;
  16. if (failslab.ignore_gfp_reclaim && (gfpflags & __GFP_RECLAIM))
  17. return false;
  18. if (failslab.cache_filter && !(cache_flags & SLAB_FAILSLAB))
  19. return false;
  20. return should_fail(&failslab.attr, size);
  21. }
  22. static int __init setup_failslab(char *str)
  23. {
  24. return setup_fault_attr(&failslab.attr, str);
  25. }
  26. __setup("failslab=", setup_failslab);
  27. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  28. static int __init failslab_debugfs_init(void)
  29. {
  30. struct dentry *dir;
  31. umode_t mode = S_IFREG | S_IRUSR | S_IWUSR;
  32. dir = fault_create_debugfs_attr("failslab", NULL, &failslab.attr);
  33. if (IS_ERR(dir))
  34. return PTR_ERR(dir);
  35. if (!debugfs_create_bool("ignore-gfp-wait", mode, dir,
  36. &failslab.ignore_gfp_reclaim))
  37. goto fail;
  38. if (!debugfs_create_bool("cache-filter", mode, dir,
  39. &failslab.cache_filter))
  40. goto fail;
  41. return 0;
  42. fail:
  43. debugfs_remove_recursive(dir);
  44. return -ENOMEM;
  45. }
  46. late_initcall(failslab_debugfs_init);
  47. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */