Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 2 | #include <linux/kernel.h> |
Akinobu Mita | 8c5fb8e | 2011-10-31 17:08:10 -0700 | [diff] [blame] | 3 | #include <linux/string.h> |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 4 | #include <linux/mm.h> |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 5 | #include <linux/highmem.h> |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 6 | #include <linux/page_ext.h> |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 7 | #include <linux/poison.h> |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 8 | #include <linux/ratelimit.h> |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 9 | |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 10 | static bool want_page_poisoning __read_mostly; |
| 11 | |
Dou Liyang | 14298d3 | 2018-04-05 16:23:53 -0700 | [diff] [blame] | 12 | static int __init early_page_poison_param(char *buf) |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 13 | { |
| 14 | if (!buf) |
| 15 | return -EINVAL; |
Minfei Huang | 2a138dc | 2016-05-20 16:58:13 -0700 | [diff] [blame] | 16 | return strtobool(buf, &want_page_poisoning); |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 17 | } |
| 18 | early_param("page_poison", early_page_poison_param); |
| 19 | |
Wei Wang | d95f58f | 2018-08-27 09:32:18 +0800 | [diff] [blame] | 20 | /** |
| 21 | * page_poisoning_enabled - check if page poisoning is enabled |
| 22 | * |
| 23 | * Return true if page poisoning is enabled, or false if not. |
| 24 | */ |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 25 | bool page_poisoning_enabled(void) |
| 26 | { |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 27 | /* |
Vinayak Menon | bd33ef3 | 2017-05-03 14:54:42 -0700 | [diff] [blame] | 28 | * Assumes that debug_pagealloc_enabled is set before |
Mike Rapoport | c6ffc5c | 2018-10-30 15:09:30 -0700 | [diff] [blame] | 29 | * memblock_free_all. |
Vinayak Menon | bd33ef3 | 2017-05-03 14:54:42 -0700 | [diff] [blame] | 30 | * Page poisoning is debug page alloc for some arches. If |
| 31 | * either of those options are enabled, enable poisoning. |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 32 | */ |
Vinayak Menon | bd33ef3 | 2017-05-03 14:54:42 -0700 | [diff] [blame] | 33 | return (want_page_poisoning || |
| 34 | (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) && |
| 35 | debug_pagealloc_enabled())); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 36 | } |
Wei Wang | d95f58f | 2018-08-27 09:32:18 +0800 | [diff] [blame] | 37 | EXPORT_SYMBOL_GPL(page_poisoning_enabled); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 38 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 39 | static void poison_page(struct page *page) |
| 40 | { |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 41 | void *addr = kmap_atomic(page); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 42 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 43 | memset(addr, PAGE_POISON, PAGE_SIZE); |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 44 | kunmap_atomic(addr); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | static void poison_pages(struct page *page, int n) |
| 48 | { |
| 49 | int i; |
| 50 | |
| 51 | for (i = 0; i < n; i++) |
| 52 | poison_page(page + i); |
| 53 | } |
| 54 | |
| 55 | static bool single_bit_flip(unsigned char a, unsigned char b) |
| 56 | { |
| 57 | unsigned char error = a ^ b; |
| 58 | |
| 59 | return error && !(error & (error - 1)); |
| 60 | } |
| 61 | |
| 62 | static void check_poison_mem(unsigned char *mem, size_t bytes) |
| 63 | { |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 64 | static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 65 | unsigned char *start; |
| 66 | unsigned char *end; |
| 67 | |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 68 | if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY)) |
| 69 | return; |
| 70 | |
Akinobu Mita | 8c5fb8e | 2011-10-31 17:08:10 -0700 | [diff] [blame] | 71 | start = memchr_inv(mem, PAGE_POISON, bytes); |
| 72 | if (!start) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 73 | return; |
| 74 | |
| 75 | for (end = mem + bytes - 1; end > start; end--) { |
| 76 | if (*end != PAGE_POISON) |
| 77 | break; |
| 78 | } |
| 79 | |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 80 | if (!__ratelimit(&ratelimit)) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 81 | return; |
| 82 | else if (start == end && single_bit_flip(*start, PAGE_POISON)) |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 83 | pr_err("pagealloc: single bit error\n"); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 84 | else |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 85 | pr_err("pagealloc: memory corruption\n"); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 86 | |
| 87 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start, |
| 88 | end - start + 1, 1); |
| 89 | dump_stack(); |
| 90 | } |
| 91 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 92 | static void unpoison_page(struct page *page) |
| 93 | { |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 94 | void *addr; |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 95 | |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 96 | addr = kmap_atomic(page); |
Vinayak Menon | bd33ef3 | 2017-05-03 14:54:42 -0700 | [diff] [blame] | 97 | /* |
| 98 | * Page poisoning when enabled poisons each and every page |
| 99 | * that is freed to buddy. Thus no extra check is done to |
| 100 | * see if a page was posioned. |
| 101 | */ |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 102 | check_poison_mem(addr, PAGE_SIZE); |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 103 | kunmap_atomic(addr); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | static void unpoison_pages(struct page *page, int n) |
| 107 | { |
| 108 | int i; |
| 109 | |
| 110 | for (i = 0; i < n; i++) |
| 111 | unpoison_page(page + i); |
| 112 | } |
| 113 | |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 114 | void kernel_poison_pages(struct page *page, int numpages, int enable) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 115 | { |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 116 | if (!page_poisoning_enabled()) |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 117 | return; |
| 118 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 119 | if (enable) |
| 120 | unpoison_pages(page, numpages); |
| 121 | else |
| 122 | poison_pages(page, numpages); |
| 123 | } |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 124 | |
| 125 | #ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC |
| 126 | void __kernel_map_pages(struct page *page, int numpages, int enable) |
| 127 | { |
| 128 | /* This function does nothing, all work is done via poison pages */ |
| 129 | } |
| 130 | #endif |