hwpoison-inject.c 3.2 KB

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