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