hwpoison-inject.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Inject a hwpoison memory failure on a arbitrary pfn */
  3. #include <linux/module.h>
  4. #include <linux/debugfs.h>
  5. #include <linux/kernel.h>
  6. #include <linux/mm.h>
  7. #include <linux/swap.h>
  8. #include <linux/pagemap.h>
  9. #include <linux/hugetlb.h>
  10. #include <linux/page-flags.h>
  11. #include <linux/memcontrol.h>
  12. #include "internal.h"
  13. static u32 hwpoison_filter_enable;
  14. static u32 hwpoison_filter_dev_major = ~0U;
  15. static u32 hwpoison_filter_dev_minor = ~0U;
  16. static u64 hwpoison_filter_flags_mask;
  17. static u64 hwpoison_filter_flags_value;
  18. static int hwpoison_filter_dev(struct page *p)
  19. {
  20. struct folio *folio = page_folio(p);
  21. struct address_space *mapping;
  22. dev_t dev;
  23. if (hwpoison_filter_dev_major == ~0U &&
  24. hwpoison_filter_dev_minor == ~0U)
  25. return 0;
  26. mapping = folio_mapping(folio);
  27. if (mapping == NULL || mapping->host == NULL)
  28. return -EINVAL;
  29. dev = mapping->host->i_sb->s_dev;
  30. if (hwpoison_filter_dev_major != ~0U &&
  31. hwpoison_filter_dev_major != MAJOR(dev))
  32. return -EINVAL;
  33. if (hwpoison_filter_dev_minor != ~0U &&
  34. hwpoison_filter_dev_minor != MINOR(dev))
  35. return -EINVAL;
  36. return 0;
  37. }
  38. static int hwpoison_filter_flags(struct page *p)
  39. {
  40. if (!hwpoison_filter_flags_mask)
  41. return 0;
  42. if ((stable_page_flags(p) & hwpoison_filter_flags_mask) ==
  43. hwpoison_filter_flags_value)
  44. return 0;
  45. else
  46. return -EINVAL;
  47. }
  48. /*
  49. * This allows stress tests to limit test scope to a collection of tasks
  50. * by putting them under some memcg. This prevents killing unrelated/important
  51. * processes such as /sbin/init. Note that the target task may share clean
  52. * pages with init (eg. libc text), which is harmless. If the target task
  53. * share _dirty_ pages with another task B, the test scheme must make sure B
  54. * is also included in the memcg. At last, due to race conditions this filter
  55. * can only guarantee that the page either belongs to the memcg tasks, or is
  56. * a freed page.
  57. */
  58. #ifdef CONFIG_MEMCG
  59. static u64 hwpoison_filter_memcg;
  60. static int hwpoison_filter_task(struct page *p)
  61. {
  62. if (!hwpoison_filter_memcg)
  63. return 0;
  64. if (page_cgroup_ino(p) != hwpoison_filter_memcg)
  65. return -EINVAL;
  66. return 0;
  67. }
  68. #else
  69. static int hwpoison_filter_task(struct page *p) { return 0; }
  70. #endif
  71. static int hwpoison_filter(struct page *p)
  72. {
  73. if (!hwpoison_filter_enable)
  74. return 0;
  75. if (hwpoison_filter_dev(p))
  76. return -EINVAL;
  77. if (hwpoison_filter_flags(p))
  78. return -EINVAL;
  79. if (hwpoison_filter_task(p))
  80. return -EINVAL;
  81. return 0;
  82. }
  83. static struct dentry *hwpoison_dir;
  84. static int hwpoison_inject(void *data, u64 val)
  85. {
  86. unsigned long pfn = val;
  87. struct page *p;
  88. struct folio *folio;
  89. int err;
  90. if (!capable(CAP_SYS_ADMIN))
  91. return -EPERM;
  92. if (!pfn_valid(pfn))
  93. return -ENXIO;
  94. p = pfn_to_page(pfn);
  95. folio = page_folio(p);
  96. if (!hwpoison_filter_enable)
  97. goto inject;
  98. shake_folio(folio);
  99. /*
  100. * This implies unable to support non-LRU pages except free page.
  101. */
  102. if (!folio_test_lru(folio) && !folio_test_hugetlb(folio) &&
  103. !is_free_buddy_page(p))
  104. return 0;
  105. /*
  106. * do a racy check to make sure PG_hwpoison will only be set for
  107. * the targeted owner (or on a free page).
  108. * memory_failure() will redo the check reliably inside page lock.
  109. */
  110. err = hwpoison_filter(&folio->page);
  111. if (err)
  112. return 0;
  113. inject:
  114. pr_info("Injecting memory failure at pfn %#lx\n", pfn);
  115. err = memory_failure(pfn, MF_SW_SIMULATED);
  116. return (err == -EOPNOTSUPP) ? 0 : err;
  117. }
  118. static int hwpoison_unpoison(void *data, u64 val)
  119. {
  120. if (!capable(CAP_SYS_ADMIN))
  121. return -EPERM;
  122. return unpoison_memory(val);
  123. }
  124. DEFINE_DEBUGFS_ATTRIBUTE(hwpoison_fops, NULL, hwpoison_inject, "%lli\n");
  125. DEFINE_DEBUGFS_ATTRIBUTE(unpoison_fops, NULL, hwpoison_unpoison, "%lli\n");
  126. static void __exit pfn_inject_exit(void)
  127. {
  128. hwpoison_filter_enable = 0;
  129. hwpoison_filter_unregister();
  130. debugfs_remove_recursive(hwpoison_dir);
  131. }
  132. static int __init pfn_inject_init(void)
  133. {
  134. hwpoison_dir = debugfs_create_dir("hwpoison", NULL);
  135. /*
  136. * Note that the below poison/unpoison interfaces do not involve
  137. * hardware status change, hence do not require hardware support.
  138. * They are mainly for testing hwpoison in software level.
  139. */
  140. debugfs_create_file("corrupt-pfn", 0200, hwpoison_dir, NULL,
  141. &hwpoison_fops);
  142. debugfs_create_file("unpoison-pfn", 0200, hwpoison_dir, NULL,
  143. &unpoison_fops);
  144. debugfs_create_u32("corrupt-filter-enable", 0600, hwpoison_dir,
  145. &hwpoison_filter_enable);
  146. debugfs_create_u32("corrupt-filter-dev-major", 0600, hwpoison_dir,
  147. &hwpoison_filter_dev_major);
  148. debugfs_create_u32("corrupt-filter-dev-minor", 0600, hwpoison_dir,
  149. &hwpoison_filter_dev_minor);
  150. debugfs_create_u64("corrupt-filter-flags-mask", 0600, hwpoison_dir,
  151. &hwpoison_filter_flags_mask);
  152. debugfs_create_u64("corrupt-filter-flags-value", 0600, hwpoison_dir,
  153. &hwpoison_filter_flags_value);
  154. #ifdef CONFIG_MEMCG
  155. debugfs_create_u64("corrupt-filter-memcg", 0600, hwpoison_dir,
  156. &hwpoison_filter_memcg);
  157. #endif
  158. hwpoison_filter_register(hwpoison_filter);
  159. return 0;
  160. }
  161. module_init(pfn_inject_init);
  162. module_exit(pfn_inject_exit);
  163. MODULE_DESCRIPTION("HWPoison pages injector");
  164. MODULE_LICENSE("GPL");