page_poison.c 2.6 KB

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