hwpoison-inject.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /* Inject a hwpoison memory failure on a arbitrary pfn */
  2. #include <linux/module.h>
  3. #include <linux/debugfs.h>
  4. #include <linux/kernel.h>
  5. #include <linux/mm.h>
  6. #include <linux/swap.h>
  7. #include <linux/pagemap.h>
  8. #include <linux/hugetlb.h>
  9. #include "internal.h"
  10. static struct dentry *hwpoison_dir;
  11. static int hwpoison_inject(void *data, u64 val)
  12. {
  13. unsigned long pfn = val;
  14. struct page *p;
  15. struct page *hpage;
  16. int err;
  17. if (!capable(CAP_SYS_ADMIN))
  18. return -EPERM;
  19. if (!hwpoison_filter_enable)
  20. goto inject;
  21. if (!pfn_valid(pfn))
  22. return -ENXIO;
  23. p = pfn_to_page(pfn);
  24. hpage = compound_head(p);
  25. /*
  26. * This implies unable to support free buddy pages.
  27. */
  28. if (!get_page_unless_zero(hpage))
  29. return 0;
  30. if (!PageLRU(p) && !PageHuge(p))
  31. shake_page(p, 0);
  32. /*
  33. * This implies unable to support non-LRU pages.
  34. */
  35. if (!PageLRU(p) && !PageHuge(p))
  36. return 0;
  37. /*
  38. * do a racy check with elevated page count, to make sure PG_hwpoison
  39. * will only be set for the targeted owner (or on a free page).
  40. * We temporarily take page lock for try_get_mem_cgroup_from_page().
  41. * __memory_failure() will redo the check reliably inside page lock.
  42. */
  43. lock_page(hpage);
  44. err = hwpoison_filter(hpage);
  45. unlock_page(hpage);
  46. if (err)
  47. return 0;
  48. inject:
  49. printk(KERN_INFO "Injecting memory failure at pfn %lx\n", pfn);
  50. return __memory_failure(pfn, 18, MF_COUNT_INCREASED);
  51. }
  52. static int hwpoison_unpoison(void *data, u64 val)
  53. {
  54. if (!capable(CAP_SYS_ADMIN))
  55. return -EPERM;
  56. return unpoison_memory(val);
  57. }
  58. DEFINE_SIMPLE_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
  59. DEFINE_SIMPLE_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
  60. static void pfn_inject_exit(void)
  61. {
  62. if (hwpoison_dir)
  63. debugfs_remove_recursive(hwpoison_dir);
  64. }
  65. static int pfn_inject_init(void)
  66. {
  67. struct dentry *dentry;
  68. hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
  69. if (hwpoison_dir == NULL)
  70. return -ENOMEM;
  71. /*
  72. * Note that the below poison/unpoison interfaces do not involve
  73. * hardware status change, hence do not require hardware support.
  74. * They are mainly for testing hwpoison in software level.
  75. */
  76. dentry = debugfs_create_file("corrupt-pfn", 0600, hwpoison_dir,
  77. NULL, &hwpoison_fops);
  78. if (!dentry)
  79. goto fail;
  80. dentry = debugfs_create_file("unpoison-pfn", 0600, hwpoison_dir,
  81. NULL, &unpoison_fops);
  82. if (!dentry)
  83. goto fail;
  84. dentry = debugfs_create_u32("corrupt-filter-enable", 0600,
  85. hwpoison_dir, &hwpoison_filter_enable);
  86. if (!dentry)
  87. goto fail;
  88. dentry = debugfs_create_u32("corrupt-filter-dev-major", 0600,
  89. hwpoison_dir, &hwpoison_filter_dev_major);
  90. if (!dentry)
  91. goto fail;
  92. dentry = debugfs_create_u32("corrupt-filter-dev-minor", 0600,
  93. hwpoison_dir, &hwpoison_filter_dev_minor);
  94. if (!dentry)
  95. goto fail;
  96. dentry = debugfs_create_u64("corrupt-filter-flags-mask", 0600,
  97. hwpoison_dir, &hwpoison_filter_flags_mask);
  98. if (!dentry)
  99. goto fail;
  100. dentry = debugfs_create_u64("corrupt-filter-flags-value", 0600,
  101. hwpoison_dir, &hwpoison_filter_flags_value);
  102. if (!dentry)
  103. goto fail;
  104. #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
  105. dentry = debugfs_create_u64("corrupt-filter-memcg", 0600,
  106. hwpoison_dir, &hwpoison_filter_memcg);
  107. if (!dentry)
  108. goto fail;
  109. #endif
  110. return 0;
  111. fail:
  112. pfn_inject_exit();
  113. return -ENOMEM;
  114. }
  115. module_init(pfn_inject_init);
  116. module_exit(pfn_inject_exit);
  117. MODULE_LICENSE("GPL");