page_poison.c 2.4 KB

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