hwpoison-inject.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. shake_page(hpage, 0);
  31. /*
  32. * This implies unable to support non-LRU pages.
  33. */
  34. if (!PageLRU(hpage) && !PageHuge(p))
  35. goto put_out;
  36. /*
  37. * do a racy check with elevated page count, to make sure PG_hwpoison
  38. * will only be set for the targeted owner (or on a free page).
  39. * memory_failure() will redo the check reliably inside page lock.
  40. */
  41. err = hwpoison_filter(hpage);
  42. if (err)
  43. goto put_out;
  44. inject:
  45. pr_info("Injecting memory failure at pfn %#lx\n", pfn);
  46. return memory_failure(pfn, MF_COUNT_INCREASED);
  47. put_out:
  48. put_hwpoison_page(p);
  49. return 0;
  50. }
  51. static int hwpoison_unpoison(void *data, u64 val)
  52. {
  53. if (!capable(CAP_SYS_ADMIN))
  54. return -EPERM;
  55. return unpoison_memory(val);
  56. }
  57. DEFINE_SIMPLE_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
  58. DEFINE_SIMPLE_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
  59. static void pfn_inject_exit(void)
  60. {
  61. debugfs_remove_recursive(hwpoison_dir);
  62. }
  63. static int pfn_inject_init(void)
  64. {
  65. struct dentry *dentry;
  66. hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
  67. if (hwpoison_dir == NULL)
  68. return -ENOMEM;
  69. /*
  70. * Note that the below poison/unpoison interfaces do not involve
  71. * hardware status change, hence do not require hardware support.
  72. * They are mainly for testing hwpoison in software level.
  73. */
  74. dentry = debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir,
  75. NULL, &hwpoison_fops);
  76. if (!dentry)
  77. goto fail;
  78. dentry = debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir,
  79. NULL, &unpoison_fops);
  80. if (!dentry)
  81. goto fail;
  82. dentry = debugfs_create_u32("corrupt-filter-enable", 0600,
  83. hwpoison_dir, &hwpoison_filter_enable);
  84. if (!dentry)
  85. goto fail;
  86. dentry = debugfs_create_u32("corrupt-filter-dev-major", 0600,
  87. hwpoison_dir, &hwpoison_filter_dev_major);
  88. if (!dentry)
  89. goto fail;
  90. dentry = debugfs_create_u32("corrupt-filter-dev-minor", 0600,
  91. hwpoison_dir, &hwpoison_filter_dev_minor);
  92. if (!dentry)
  93. goto fail;
  94. dentry = debugfs_create_u64("corrupt-filter-flags-mask", 0600,
  95. hwpoison_dir, &hwpoison_filter_flags_mask);
  96. if (!dentry)
  97. goto fail;
  98. dentry = debugfs_create_u64("corrupt-filter-flags-value", 0600,
  99. hwpoison_dir, &hwpoison_filter_flags_value);
  100. if (!dentry)
  101. goto fail;
  102. #ifdef CONFIG_MEMCG
  103. dentry = debugfs_create_u64("corrupt-filter-memcg", 0600,
  104. hwpoison_dir, &hwpoison_filter_memcg);
  105. if (!dentry)
  106. goto fail;
  107. #endif
  108. return 0;
  109. fail:
  110. pfn_inject_exit();
  111. return -ENOMEM;
  112. }
  113. module_init(pfn_inject_init);
  114. module_exit(pfn_inject_exit);
  115. MODULE_LICENSE("GPL");