Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 1 | #include <linux/kernel.h> |
Akinobu Mita | 8c5fb8e | 2011-10-31 17:08:10 -0700 | [diff] [blame] | 2 | #include <linux/string.h> |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 3 | #include <linux/mm.h> |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 4 | #include <linux/highmem.h> |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 5 | #include <linux/page_ext.h> |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 6 | #include <linux/poison.h> |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 7 | #include <linux/ratelimit.h> |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 8 | |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 9 | static bool __page_poisoning_enabled __read_mostly; |
| 10 | static bool want_page_poisoning __read_mostly; |
| 11 | |
| 12 | static int early_page_poison_param(char *buf) |
| 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 | |
| 20 | bool page_poisoning_enabled(void) |
| 21 | { |
| 22 | return __page_poisoning_enabled; |
| 23 | } |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 24 | |
| 25 | static bool need_page_poisoning(void) |
| 26 | { |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 27 | return want_page_poisoning; |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | static void init_page_poisoning(void) |
| 31 | { |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 32 | /* |
| 33 | * page poisoning is debug page alloc for some arches. If either |
| 34 | * of those options are enabled, enable poisoning |
| 35 | */ |
| 36 | if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC)) { |
| 37 | if (!want_page_poisoning && !debug_pagealloc_enabled()) |
| 38 | return; |
| 39 | } else { |
| 40 | if (!want_page_poisoning) |
| 41 | return; |
| 42 | } |
Joonsoo Kim | 031bc57 | 2014-12-12 16:55:52 -0800 | [diff] [blame] | 43 | |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 44 | __page_poisoning_enabled = true; |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | struct page_ext_operations page_poisoning_ops = { |
| 48 | .need = need_page_poisoning, |
| 49 | .init = init_page_poisoning, |
| 50 | }; |
| 51 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 52 | static inline void set_page_poison(struct page *page) |
| 53 | { |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 54 | struct page_ext *page_ext; |
| 55 | |
| 56 | page_ext = lookup_page_ext(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 57 | if (unlikely(!page_ext)) |
| 58 | return; |
| 59 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 60 | __set_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | static inline void clear_page_poison(struct page *page) |
| 64 | { |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 65 | struct page_ext *page_ext; |
| 66 | |
| 67 | page_ext = lookup_page_ext(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 68 | if (unlikely(!page_ext)) |
| 69 | return; |
| 70 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 71 | __clear_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Laura Abbott | 1414c7f | 2016-03-15 14:56:30 -0700 | [diff] [blame] | 74 | bool page_is_poisoned(struct page *page) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 75 | { |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 76 | struct page_ext *page_ext; |
| 77 | |
| 78 | page_ext = lookup_page_ext(page); |
Yang Shi | f86e427 | 2016-06-03 14:55:38 -0700 | [diff] [blame] | 79 | if (unlikely(!page_ext)) |
Laura Abbott | 1414c7f | 2016-03-15 14:56:30 -0700 | [diff] [blame] | 80 | return false; |
| 81 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 82 | return test_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 85 | static void poison_page(struct page *page) |
| 86 | { |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 87 | void *addr = kmap_atomic(page); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 88 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 89 | set_page_poison(page); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 90 | memset(addr, PAGE_POISON, PAGE_SIZE); |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 91 | kunmap_atomic(addr); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | static void poison_pages(struct page *page, int n) |
| 95 | { |
| 96 | int i; |
| 97 | |
| 98 | for (i = 0; i < n; i++) |
| 99 | poison_page(page + i); |
| 100 | } |
| 101 | |
| 102 | static bool single_bit_flip(unsigned char a, unsigned char b) |
| 103 | { |
| 104 | unsigned char error = a ^ b; |
| 105 | |
| 106 | return error && !(error & (error - 1)); |
| 107 | } |
| 108 | |
Prasad Sodagudi | 87f4202 | 2016-02-25 13:01:18 +0530 | [diff] [blame] | 109 | static void check_poison_mem(struct page *page, |
| 110 | unsigned char *mem, size_t bytes) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 111 | { |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 112 | static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 113 | unsigned char *start; |
| 114 | unsigned char *end; |
| 115 | |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 116 | if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY)) |
| 117 | return; |
| 118 | |
Akinobu Mita | 8c5fb8e | 2011-10-31 17:08:10 -0700 | [diff] [blame] | 119 | start = memchr_inv(mem, PAGE_POISON, bytes); |
| 120 | if (!start) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 121 | return; |
| 122 | |
| 123 | for (end = mem + bytes - 1; end > start; end--) { |
| 124 | if (*end != PAGE_POISON) |
| 125 | break; |
| 126 | } |
| 127 | |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 128 | if (!__ratelimit(&ratelimit)) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 129 | return; |
| 130 | else if (start == end && single_bit_flip(*start, PAGE_POISON)) |
Prasad Sodagudi | 87f4202 | 2016-02-25 13:01:18 +0530 | [diff] [blame] | 131 | pr_err("pagealloc: single bit error on page with phys start 0x%lx\n", |
| 132 | (unsigned long)page_to_phys(page)); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 133 | else |
Prasad Sodagudi | 87f4202 | 2016-02-25 13:01:18 +0530 | [diff] [blame] | 134 | pr_err("pagealloc: memory corruption on page with phys start 0x%lx\n", |
| 135 | (unsigned long)page_to_phys(page)); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 136 | |
| 137 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start, |
| 138 | end - start + 1, 1); |
Prasad Sodagudi | 6ba925c | 2016-02-25 12:57:06 +0530 | [diff] [blame] | 139 | BUG_ON(PANIC_CORRUPTION); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 140 | dump_stack(); |
| 141 | } |
| 142 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 143 | static void unpoison_page(struct page *page) |
| 144 | { |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 145 | void *addr; |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 146 | |
Laura Abbott | 1414c7f | 2016-03-15 14:56:30 -0700 | [diff] [blame] | 147 | if (!page_is_poisoned(page)) |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 148 | return; |
| 149 | |
| 150 | addr = kmap_atomic(page); |
Prasad Sodagudi | 87f4202 | 2016-02-25 13:01:18 +0530 | [diff] [blame] | 151 | check_poison_mem(page, addr, PAGE_SIZE); |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 152 | clear_page_poison(page); |
| 153 | kunmap_atomic(addr); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void unpoison_pages(struct page *page, int n) |
| 157 | { |
| 158 | int i; |
| 159 | |
| 160 | for (i = 0; i < n; i++) |
| 161 | unpoison_page(page + i); |
| 162 | } |
| 163 | |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 164 | void kernel_poison_pages(struct page *page, int numpages, int enable) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 165 | { |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 166 | if (!page_poisoning_enabled()) |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 167 | return; |
| 168 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 169 | if (enable) |
| 170 | unpoison_pages(page, numpages); |
| 171 | else |
| 172 | poison_pages(page, numpages); |
| 173 | } |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 174 | |
| 175 | #ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC |
| 176 | void __kernel_map_pages(struct page *page, int numpages, int enable) |
| 177 | { |
| 178 | /* This function does nothing, all work is done via poison pages */ |
| 179 | } |
| 180 | #endif |