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> |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 5 | #include <linux/page-debug-flags.h> |
| 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 | 47346984 | 2014-04-14 20:09:44 -0700 | [diff] [blame] | 9 | #ifndef mark_addr_rdonly |
| 10 | #define mark_addr_rdonly(a) |
| 11 | #endif |
| 12 | |
| 13 | #ifndef mark_addr_rdwrite |
| 14 | #define mark_addr_rdwrite(a) |
| 15 | #endif |
| 16 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 17 | static inline void set_page_poison(struct page *page) |
| 18 | { |
| 19 | __set_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags); |
| 20 | } |
| 21 | |
| 22 | static inline void clear_page_poison(struct page *page) |
| 23 | { |
| 24 | __clear_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags); |
| 25 | } |
| 26 | |
| 27 | static inline bool page_poison(struct page *page) |
| 28 | { |
| 29 | return test_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags); |
| 30 | } |
| 31 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 32 | static void poison_page(struct page *page) |
| 33 | { |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 34 | void *addr = kmap_atomic(page); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 35 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 36 | set_page_poison(page); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 37 | memset(addr, PAGE_POISON, PAGE_SIZE); |
Laura Abbott | 47346984 | 2014-04-14 20:09:44 -0700 | [diff] [blame] | 38 | mark_addr_rdonly(addr); |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 39 | kunmap_atomic(addr); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | static void poison_pages(struct page *page, int n) |
| 43 | { |
| 44 | int i; |
| 45 | |
| 46 | for (i = 0; i < n; i++) |
| 47 | poison_page(page + i); |
| 48 | } |
| 49 | |
| 50 | static bool single_bit_flip(unsigned char a, unsigned char b) |
| 51 | { |
| 52 | unsigned char error = a ^ b; |
| 53 | |
| 54 | return error && !(error & (error - 1)); |
| 55 | } |
| 56 | |
| 57 | static void check_poison_mem(unsigned char *mem, size_t bytes) |
| 58 | { |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 59 | static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 60 | unsigned char *start; |
| 61 | unsigned char *end; |
| 62 | |
Akinobu Mita | 8c5fb8e | 2011-10-31 17:08:10 -0700 | [diff] [blame] | 63 | start = memchr_inv(mem, PAGE_POISON, bytes); |
| 64 | if (!start) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 65 | return; |
| 66 | |
| 67 | for (end = mem + bytes - 1; end > start; end--) { |
| 68 | if (*end != PAGE_POISON) |
| 69 | break; |
| 70 | } |
| 71 | |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 72 | if (!__ratelimit(&ratelimit)) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 73 | return; |
| 74 | else if (start == end && single_bit_flip(*start, PAGE_POISON)) |
| 75 | printk(KERN_ERR "pagealloc: single bit error\n"); |
| 76 | else |
| 77 | printk(KERN_ERR "pagealloc: memory corruption\n"); |
| 78 | |
| 79 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start, |
| 80 | end - start + 1, 1); |
Subbaraman Narayanamurthy | 6639973 | 2013-09-20 15:23:56 -0700 | [diff] [blame] | 81 | BUG_ON(PANIC_CORRUPTION); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 82 | dump_stack(); |
| 83 | } |
| 84 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 85 | static void unpoison_page(struct page *page) |
| 86 | { |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 87 | void *addr; |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 88 | |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 89 | if (!page_poison(page)) |
| 90 | return; |
| 91 | |
| 92 | addr = kmap_atomic(page); |
| 93 | check_poison_mem(addr, PAGE_SIZE); |
Laura Abbott | 47346984 | 2014-04-14 20:09:44 -0700 | [diff] [blame] | 94 | mark_addr_rdwrite(addr); |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 95 | clear_page_poison(page); |
| 96 | kunmap_atomic(addr); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | static void unpoison_pages(struct page *page, int n) |
| 100 | { |
| 101 | int i; |
| 102 | |
| 103 | for (i = 0; i < n; i++) |
| 104 | unpoison_page(page + i); |
| 105 | } |
| 106 | |
| 107 | void kernel_map_pages(struct page *page, int numpages, int enable) |
| 108 | { |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 109 | if (enable) |
| 110 | unpoison_pages(page, numpages); |
| 111 | else |
| 112 | poison_pages(page, numpages); |
| 113 | } |