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