page_poison.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include <linux/kernel.h>
  2. #include <linux/string.h>
  3. #include <linux/mm.h>
  4. #include <linux/highmem.h>
  5. #include <linux/page_ext.h>
  6. #include <linux/poison.h>
  7. #include <linux/ratelimit.h>
  8. static bool want_page_poisoning __read_mostly
  9. = IS_ENABLED(CONFIG_PAGE_POISONING_ENABLE_DEFAULT);
  10. static int 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(struct page *page,
  47. unsigned char *mem, size_t bytes)
  48. {
  49. static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
  50. unsigned char *start;
  51. unsigned char *end;
  52. if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
  53. return;
  54. start = memchr_inv(mem, PAGE_POISON, bytes);
  55. if (!start)
  56. return;
  57. for (end = mem + bytes - 1; end > start; end--) {
  58. if (*end != PAGE_POISON)
  59. break;
  60. }
  61. if (!__ratelimit(&ratelimit))
  62. return;
  63. else if (start == end && single_bit_flip(*start, PAGE_POISON))
  64. pr_err("pagealloc: single bit error on page with phys start 0x%lx\n",
  65. (unsigned long)page_to_phys(page));
  66. else
  67. pr_err("pagealloc: memory corruption on page with phys start 0x%lx\n",
  68. (unsigned long)page_to_phys(page));
  69. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
  70. end - start + 1, 1);
  71. BUG_ON(PANIC_CORRUPTION);
  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(page, 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