page_poison.c 2.8 KB

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