hwpoison-inject.c 3.2 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 (!pfn_valid(pfn))
  20. return -ENXIO;
  21. p = pfn_to_page(pfn);
  22. hpage = compound_head(p);
  23. /*
  24. * This implies unable to support free buddy pages.
  25. */
  26. if (!get_hwpoison_page(p))
  27. return 0;
  28. if (!hwpoison_filter_enable)
  29. goto inject;
  30. if (!PageLRU(hpage) && !PageHuge(p))
  31. shake_page(hpage, 0);
  32. /*
  33. * This implies unable to support non-LRU pages.
  34. */
  35. if (!PageLRU(hpage) && !PageHuge(p))
  36. goto put_out;
  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. * memory_failure() will redo the check reliably inside page lock.
  41. */
  42. err = hwpoison_filter(hpage);
  43. if (err)
  44. goto put_out;
  45. inject:
  46. pr_info("Injecting memory failure at pfn %#lx\n", pfn);
  47. return memory_failure(pfn, 18, MF_COUNT_INCREASED);
  48. put_out:
  49. put_hwpoison_page(p);
  50. return 0;
  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. debugfs_remove_recursive(hwpoison_dir);
  63. }
  64. static int pfn_inject_init(void)
  65. {
  66. struct dentry *dentry;
  67. hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
  68. if (hwpoison_dir == NULL)
  69. return -ENOMEM;
  70. /*
  71. * Note that the below poison/unpoison interfaces do not involve
  72. * hardware status change, hence do not require hardware support.
  73. * They are mainly for testing hwpoison in software level.
  74. */
  75. dentry = debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir,
  76. NULL, &hwpoison_fops);
  77. if (!dentry)
  78. goto fail;
  79. dentry = debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir,
  80. NULL, &unpoison_fops);
  81. if (!dentry)
  82. goto fail;
  83. dentry = debugfs_create_u32("corrupt-filter-enable", 0600,
  84. hwpoison_dir, &hwpoison_filter_enable);
  85. if (!dentry)
  86. goto fail;
  87. dentry = debugfs_create_u32("corrupt-filter-dev-major", 0600,
  88. hwpoison_dir, &hwpoison_filter_dev_major);
  89. if (!dentry)
  90. goto fail;
  91. dentry = debugfs_create_u32("corrupt-filter-dev-minor", 0600,
  92. hwpoison_dir, &hwpoison_filter_dev_minor);
  93. if (!dentry)
  94. goto fail;
  95. dentry = debugfs_create_u64("corrupt-filter-flags-mask", 0600,
  96. hwpoison_dir, &hwpoison_filter_flags_mask);
  97. if (!dentry)
  98. goto fail;
  99. dentry = debugfs_create_u64("corrupt-filter-flags-value", 0600,
  100. hwpoison_dir, &hwpoison_filter_flags_value);
  101. if (!dentry)
  102. goto fail;
  103. #ifdef CONFIG_MEMCG
  104. dentry = debugfs_create_u64("corrupt-filter-memcg", 0600,
  105. hwpoison_dir, &hwpoison_filter_memcg);
  106. if (!dentry)
  107. goto fail;
  108. #endif
  109. return 0;
  110. fail:
  111. pfn_inject_exit();
  112. return -ENOMEM;
  113. }
  114. module_init(pfn_inject_init);
  115. module_exit(pfn_inject_exit);
  116. MODULE_LICENSE("GPL");