page_poison.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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/mmdebug.h>
  6. #include <linux/highmem.h>
  7. #include <linux/page_ext.h>
  8. #include <linux/poison.h>
  9. #include <linux/ratelimit.h>
  10. #include <linux/kasan.h>
  11. bool _page_poisoning_enabled_early;
  12. EXPORT_SYMBOL(_page_poisoning_enabled_early);
  13. DEFINE_STATIC_KEY_FALSE(_page_poisoning_enabled);
  14. EXPORT_SYMBOL(_page_poisoning_enabled);
  15. static int __init early_page_poison_param(char *buf)
  16. {
  17. return kstrtobool(buf, &_page_poisoning_enabled_early);
  18. }
  19. early_param("page_poison", early_page_poison_param);
  20. static void poison_page(struct page *page)
  21. {
  22. void *addr = kmap_atomic(page);
  23. /* KASAN still think the page is in-use, so skip it. */
  24. kasan_disable_current();
  25. memset(kasan_reset_tag(addr), PAGE_POISON, PAGE_SIZE);
  26. kasan_enable_current();
  27. kunmap_atomic(addr);
  28. }
  29. void __kernel_poison_pages(struct page *page, int n)
  30. {
  31. int i;
  32. for (i = 0; i < n; i++)
  33. poison_page(page + i);
  34. }
  35. static bool single_bit_flip(unsigned char a, unsigned char b)
  36. {
  37. unsigned char error = a ^ b;
  38. return error && !(error & (error - 1));
  39. }
  40. static void check_poison_mem(struct page *page, unsigned char *mem, size_t bytes)
  41. {
  42. static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
  43. unsigned char *start;
  44. unsigned char *end;
  45. start = memchr_inv(mem, PAGE_POISON, bytes);
  46. if (!start)
  47. return;
  48. for (end = mem + bytes - 1; end > start; end--) {
  49. if (*end != PAGE_POISON)
  50. break;
  51. }
  52. if (!__ratelimit(&ratelimit))
  53. return;
  54. else if (start == end && single_bit_flip(*start, PAGE_POISON))
  55. pr_err("pagealloc: single bit error\n");
  56. else
  57. pr_err("pagealloc: memory corruption\n");
  58. print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
  59. end - start + 1, 1);
  60. dump_stack();
  61. dump_page(page, "pagealloc: corrupted page details");
  62. }
  63. static void unpoison_page(struct page *page)
  64. {
  65. void *addr;
  66. addr = kmap_atomic(page);
  67. kasan_disable_current();
  68. /*
  69. * Page poisoning when enabled poisons each and every page
  70. * that is freed to buddy. Thus no extra check is done to
  71. * see if a page was poisoned.
  72. */
  73. check_poison_mem(page, kasan_reset_tag(addr), PAGE_SIZE);
  74. kasan_enable_current();
  75. kunmap_atomic(addr);
  76. }
  77. void __kernel_unpoison_pages(struct page *page, int n)
  78. {
  79. int i;
  80. for (i = 0; i < n; i++)
  81. unpoison_page(page + i);
  82. }
  83. #ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
  84. void __kernel_map_pages(struct page *page, int numpages, int enable)
  85. {
  86. /* This function does nothing, all work is done via poison pages */
  87. }
  88. #endif