hwpoison-inject.c 3.2 KB

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