page_poison.c 2.7 KB

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