hwpoison-inject.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * 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. goto put_out;
  48. inject:
  49. pr_info("Injecting memory failure at pfn %#lx\n", pfn);
  50. return memory_failure(pfn, 18, MF_COUNT_INCREASED);
  51. put_out:
  52. put_page(p);
  53. return 0;
  54. }
  55. static int hwpoison_unpoison(void *data, u64 val)
  56. {
  57. if (!capable(CAP_SYS_ADMIN))
  58. return -EPERM;
  59. return unpoison_memory(val);
  60. }
  61. DEFINE_SIMPLE_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
  62. DEFINE_SIMPLE_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
  63. static void pfn_inject_exit(void)
  64. {
  65. debugfs_remove_recursive(hwpoison_dir);
  66. }
  67. static int pfn_inject_init(void)
  68. {
  69. struct dentry *dentry;
  70. hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
  71. if (hwpoison_dir == NULL)
  72. return -ENOMEM;
  73. /*
  74. * Note that the below poison/unpoison interfaces do not involve
  75. * hardware status change, hence do not require hardware support.
  76. * They are mainly for testing hwpoison in software level.
  77. */
  78. dentry = debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir,
  79. NULL, &hwpoison_fops);
  80. if (!dentry)
  81. goto fail;
  82. dentry = debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir,
  83. NULL, &unpoison_fops);
  84. if (!dentry)
  85. goto fail;
  86. dentry = debugfs_create_u32("corrupt-filter-enable", 0600,
  87. hwpoison_dir, &hwpoison_filter_enable);
  88. if (!dentry)
  89. goto fail;
  90. dentry = debugfs_create_u32("corrupt-filter-dev-major", 0600,
  91. hwpoison_dir, &hwpoison_filter_dev_major);
  92. if (!dentry)
  93. goto fail;
  94. dentry = debugfs_create_u32("corrupt-filter-dev-minor", 0600,
  95. hwpoison_dir, &hwpoison_filter_dev_minor);
  96. if (!dentry)
  97. goto fail;
  98. dentry = debugfs_create_u64("corrupt-filter-flags-mask", 0600,
  99. hwpoison_dir, &hwpoison_filter_flags_mask);
  100. if (!dentry)
  101. goto fail;
  102. dentry = debugfs_create_u64("corrupt-filter-flags-value", 0600,
  103. hwpoison_dir, &hwpoison_filter_flags_value);
  104. if (!dentry)
  105. goto fail;
  106. #ifdef CONFIG_MEMCG_SWAP
  107. dentry = debugfs_create_u64("corrupt-filter-memcg", 0600,
  108. hwpoison_dir, &hwpoison_filter_memcg);
  109. if (!dentry)
  110. goto fail;
  111. #endif
  112. return 0;
  113. fail:
  114. pfn_inject_exit();
  115. return -ENOMEM;
  116. }
  117. module_init(pfn_inject_init);
  118. module_exit(pfn_inject_exit);
  119. MODULE_LICENSE("GPL");