fail_page_alloc.c 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/fault-inject.h>
  3. #include <linux/debugfs.h>
  4. #include <linux/error-injection.h>
  5. #include <linux/mm.h>
  6. static struct {
  7. struct fault_attr attr;
  8. bool ignore_gfp_highmem;
  9. bool ignore_gfp_reclaim;
  10. u32 min_order;
  11. } fail_page_alloc = {
  12. .attr = FAULT_ATTR_INITIALIZER,
  13. .ignore_gfp_reclaim = true,
  14. .ignore_gfp_highmem = true,
  15. .min_order = 1,
  16. };
  17. static int __init setup_fail_page_alloc(char *str)
  18. {
  19. return setup_fault_attr(&fail_page_alloc.attr, str);
  20. }
  21. __setup("fail_page_alloc=", setup_fail_page_alloc);
  22. bool should_fail_alloc_page(gfp_t gfp_mask, unsigned int order)
  23. {
  24. int flags = 0;
  25. if (order < fail_page_alloc.min_order)
  26. return false;
  27. if (gfp_mask & __GFP_NOFAIL)
  28. return false;
  29. if (fail_page_alloc.ignore_gfp_highmem && (gfp_mask & __GFP_HIGHMEM))
  30. return false;
  31. if (fail_page_alloc.ignore_gfp_reclaim &&
  32. (gfp_mask & __GFP_DIRECT_RECLAIM))
  33. return false;
  34. /* See comment in __should_failslab() */
  35. if (gfp_mask & __GFP_NOWARN)
  36. flags |= FAULT_NOWARN;
  37. return should_fail_ex(&fail_page_alloc.attr, 1 << order, flags);
  38. }
  39. ALLOW_ERROR_INJECTION(should_fail_alloc_page, TRUE);
  40. #ifdef CONFIG_FAULT_INJECTION_DEBUG_FS
  41. static int __init fail_page_alloc_debugfs(void)
  42. {
  43. umode_t mode = S_IFREG | 0600;
  44. struct dentry *dir;
  45. dir = fault_create_debugfs_attr("fail_page_alloc", NULL,
  46. &fail_page_alloc.attr);
  47. debugfs_create_bool("ignore-gfp-wait", mode, dir,
  48. &fail_page_alloc.ignore_gfp_reclaim);
  49. debugfs_create_bool("ignore-gfp-highmem", mode, dir,
  50. &fail_page_alloc.ignore_gfp_highmem);
  51. debugfs_create_u32("min-order", mode, dir, &fail_page_alloc.min_order);
  52. return 0;
  53. }
  54. late_initcall(fail_page_alloc_debugfs);
  55. #endif /* CONFIG_FAULT_INJECTION_DEBUG_FS */