failslab.c 1.4 KB

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