page_poison.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/string.h>
  4. #include <linux/mm.h>
  5. #include <linux/highmem.h>
  6. #include <linux/page_ext.h>
  7. #include <linux/poison.h>
  8. #include <linux/ratelimit.h>
  9. static bool want_page_poisoning __read_mostly;
  10. static int __init early_page_poison_param(char *buf)
  11. {
  12. if (!buf)
  13. return -EINVAL;
  14. return strtobool(buf, &want_page_poisoning);
  15. }
  16. early_param("page_poison", early_page_poison_param);
  17. /**
  18. * page_poisoning_enabled - check if page poisoning is enabled
  19. *
  20. * Return true if page poisoning is enabled, or false if not.
  21. */
  22. bool page_poisoning_enabled(void)
  23. {
  24. /*
  25. * Assumes that debug_pagealloc_enabled is set before
  26. * memblock_free_all.
  27. * Page poisoning is debug page alloc for some arches. If
  28. * either of those options are enabled, enable poisoning.
  29. */
  30. return (want_page_poisoning ||
  31. (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
  32. debug_pagealloc_enabled()));
  33. }
  34. EXPORT_SYMBOL_GPL(page_poisoning_enabled);
  35. static void poison_page(struct page *page)
  36. {
  37. void *addr = kmap_atomic(page);
  38. memset(addr, PAGE_POISON, PAGE_SIZE);
  39. kunmap_atomic(addr);
  40. }
  41. static void poison_pages(struct page *page, int n)
  42. {
  43. int i;
  44. for (i = 0; i < n; i++)
  45. poison_page(page + i);
  46. }
  47. static bool single_bit_flip(unsigned char a, unsigned char b)
  48. {
  49. unsigned char error = a ^ b;
  50. return error && !(error & (error - 1));
  51. }
  52. static void check_poison_mem(unsigned char *mem, size_t bytes)
  53. {
  54. static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
  55. unsigned char *start;
  56. unsigned char *end;
  57. if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
  58. return;
  59. start = memchr_inv(mem, PAGE_POISON, bytes);
  60. if (!start)
  61. return;
  62. for (end = mem + bytes - 1; end > start; end--) {
  63. if (*end != PAGE_POISON)
  64. break;
  65. }
  66. if (!__ratelimit(&ratelimit))
  67. return;
  68. else if (start == end && single_bit_flip(*start, PAGE_POISON))
  69. pr_err("pagealloc: single bit error\n");
  70. else
  71. pr_err("pagealloc: memory corruption\n");
  72. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
  73. end - start + 1, 1);
  74. dump_stack();
  75. }
  76. static void unpoison_page(struct page *page)
  77. {
  78. void *addr;
  79. addr = kmap_atomic(page);
  80. /*
  81. * Page poisoning when enabled poisons each and every page
  82. * that is freed to buddy. Thus no extra check is done to
  83. * see if a page was posioned.
  84. */
  85. check_poison_mem(addr, PAGE_SIZE);
  86. kunmap_atomic(addr);
  87. }
  88. static void unpoison_pages(struct page *page, int n)
  89. {
  90. int i;
  91. for (i = 0; i < n; i++)
  92. unpoison_page(page + i);
  93. }
  94. void kernel_poison_pages(struct page *page, int numpages, int enable)
  95. {
  96. if (!page_poisoning_enabled())
  97. return;
  98. if (enable)
  99. unpoison_pages(page, numpages);
  100. else
  101. poison_pages(page, numpages);
  102. }
  103. #ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
  104. void __kernel_map_pages(struct page *page, int numpages, int enable)
  105. {
  106. /* This function does nothing, all work is done via poison pages */
  107. }
  108. #endif