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 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 9 | static bool page_poisoning_enabled __read_mostly; |
| 10 | |
| 11 | static bool need_page_poisoning(void) |
| 12 | { |
Joonsoo Kim | 031bc57 | 2014-12-12 16:55:52 -0800 | [diff] [blame] | 13 | if (!debug_pagealloc_enabled()) |
| 14 | return false; |
| 15 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 16 | return true; |
| 17 | } |
| 18 | |
| 19 | static void init_page_poisoning(void) |
| 20 | { |
Joonsoo Kim | 031bc57 | 2014-12-12 16:55:52 -0800 | [diff] [blame] | 21 | if (!debug_pagealloc_enabled()) |
| 22 | return; |
| 23 | |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 24 | page_poisoning_enabled = true; |
| 25 | } |
| 26 | |
| 27 | struct page_ext_operations page_poisoning_ops = { |
| 28 | .need = need_page_poisoning, |
| 29 | .init = init_page_poisoning, |
| 30 | }; |
| 31 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 32 | static inline void set_page_poison(struct page *page) |
| 33 | { |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 34 | struct page_ext *page_ext; |
| 35 | |
| 36 | page_ext = lookup_page_ext(page); |
| 37 | __set_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 38 | } |
| 39 | |
| 40 | static inline void clear_page_poison(struct page *page) |
| 41 | { |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 42 | struct page_ext *page_ext; |
| 43 | |
| 44 | page_ext = lookup_page_ext(page); |
| 45 | __clear_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | static inline bool page_poison(struct page *page) |
| 49 | { |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 50 | struct page_ext *page_ext; |
| 51 | |
| 52 | page_ext = lookup_page_ext(page); |
| 53 | return test_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 54 | } |
| 55 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 56 | static void poison_page(struct page *page) |
| 57 | { |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 58 | void *addr = kmap_atomic(page); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 59 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 60 | set_page_poison(page); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 61 | memset(addr, PAGE_POISON, PAGE_SIZE); |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 62 | kunmap_atomic(addr); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | static void poison_pages(struct page *page, int n) |
| 66 | { |
| 67 | int i; |
| 68 | |
| 69 | for (i = 0; i < n; i++) |
| 70 | poison_page(page + i); |
| 71 | } |
| 72 | |
| 73 | static bool single_bit_flip(unsigned char a, unsigned char b) |
| 74 | { |
| 75 | unsigned char error = a ^ b; |
| 76 | |
| 77 | return error && !(error & (error - 1)); |
| 78 | } |
| 79 | |
| 80 | static void check_poison_mem(unsigned char *mem, size_t bytes) |
| 81 | { |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 82 | static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 83 | unsigned char *start; |
| 84 | unsigned char *end; |
| 85 | |
Akinobu Mita | 8c5fb8e | 2011-10-31 17:08:10 -0700 | [diff] [blame] | 86 | start = memchr_inv(mem, PAGE_POISON, bytes); |
| 87 | if (!start) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 88 | return; |
| 89 | |
| 90 | for (end = mem + bytes - 1; end > start; end--) { |
| 91 | if (*end != PAGE_POISON) |
| 92 | break; |
| 93 | } |
| 94 | |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 95 | if (!__ratelimit(&ratelimit)) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 96 | return; |
| 97 | else if (start == end && single_bit_flip(*start, PAGE_POISON)) |
| 98 | printk(KERN_ERR "pagealloc: single bit error\n"); |
| 99 | else |
| 100 | printk(KERN_ERR "pagealloc: memory corruption\n"); |
| 101 | |
| 102 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start, |
| 103 | end - start + 1, 1); |
| 104 | dump_stack(); |
| 105 | } |
| 106 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 107 | static void unpoison_page(struct page *page) |
| 108 | { |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 109 | void *addr; |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 110 | |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 111 | if (!page_poison(page)) |
| 112 | return; |
| 113 | |
| 114 | addr = kmap_atomic(page); |
| 115 | check_poison_mem(addr, PAGE_SIZE); |
| 116 | clear_page_poison(page); |
| 117 | kunmap_atomic(addr); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void unpoison_pages(struct page *page, int n) |
| 121 | { |
| 122 | int i; |
| 123 | |
| 124 | for (i = 0; i < n; i++) |
| 125 | unpoison_page(page + i); |
| 126 | } |
| 127 | |
Joonsoo Kim | 031bc57 | 2014-12-12 16:55:52 -0800 | [diff] [blame] | 128 | void __kernel_map_pages(struct page *page, int numpages, int enable) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 129 | { |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 130 | if (!page_poisoning_enabled) |
| 131 | return; |
| 132 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 133 | if (enable) |
| 134 | unpoison_pages(page, numpages); |
| 135 | else |
| 136 | poison_pages(page, numpages); |
| 137 | } |