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 | |
Vinayak Menon | 8275f1d | 2017-04-05 10:49:14 +0530 | [diff] [blame] | 9 | static bool want_page_poisoning __read_mostly |
| 10 | = IS_ENABLED(CONFIG_PAGE_POISONING_ENABLE_DEFAULT); |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 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 | { |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 22 | /* |
Vinayak Menon | 9282168 | 2017-03-31 11:13:06 +1100 | [diff] [blame] | 23 | * Assumes that debug_pagealloc_enabled is set before |
| 24 | * free_all_bootmem. |
| 25 | * Page poisoning is debug page alloc for some arches. If |
| 26 | * either of those options are enabled, enable poisoning. |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 27 | */ |
Vinayak Menon | 9282168 | 2017-03-31 11:13:06 +1100 | [diff] [blame] | 28 | return (want_page_poisoning || |
| 29 | (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) && |
| 30 | debug_pagealloc_enabled())); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 31 | } |
| 32 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 33 | static void poison_page(struct page *page) |
| 34 | { |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 35 | void *addr = kmap_atomic(page); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 36 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 37 | memset(addr, PAGE_POISON, PAGE_SIZE); |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 38 | kunmap_atomic(addr); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static void poison_pages(struct page *page, int n) |
| 42 | { |
| 43 | int i; |
| 44 | |
| 45 | for (i = 0; i < n; i++) |
| 46 | poison_page(page + i); |
| 47 | } |
| 48 | |
| 49 | static bool single_bit_flip(unsigned char a, unsigned char b) |
| 50 | { |
| 51 | unsigned char error = a ^ b; |
| 52 | |
| 53 | return error && !(error & (error - 1)); |
| 54 | } |
| 55 | |
Prasad Sodagudi | 87f4202 | 2016-02-25 13:01:18 +0530 | [diff] [blame] | 56 | static void check_poison_mem(struct page *page, |
| 57 | unsigned char *mem, size_t bytes) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 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 | |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 63 | if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY)) |
| 64 | return; |
| 65 | |
Akinobu Mita | 8c5fb8e | 2011-10-31 17:08:10 -0700 | [diff] [blame] | 66 | start = memchr_inv(mem, PAGE_POISON, bytes); |
| 67 | if (!start) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 68 | return; |
| 69 | |
| 70 | for (end = mem + bytes - 1; end > start; end--) { |
| 71 | if (*end != PAGE_POISON) |
| 72 | break; |
| 73 | } |
| 74 | |
Akinobu Mita | 7731113 | 2011-10-31 17:08:05 -0700 | [diff] [blame] | 75 | if (!__ratelimit(&ratelimit)) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 76 | return; |
| 77 | else if (start == end && single_bit_flip(*start, PAGE_POISON)) |
Prasad Sodagudi | 87f4202 | 2016-02-25 13:01:18 +0530 | [diff] [blame] | 78 | pr_err("pagealloc: single bit error on page with phys start 0x%lx\n", |
| 79 | (unsigned long)page_to_phys(page)); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 80 | else |
Prasad Sodagudi | 87f4202 | 2016-02-25 13:01:18 +0530 | [diff] [blame] | 81 | pr_err("pagealloc: memory corruption on page with phys start 0x%lx\n", |
| 82 | (unsigned long)page_to_phys(page)); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 83 | |
| 84 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start, |
| 85 | end - start + 1, 1); |
Prasad Sodagudi | 6ba925c | 2016-02-25 12:57:06 +0530 | [diff] [blame] | 86 | BUG_ON(PANIC_CORRUPTION); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 87 | dump_stack(); |
| 88 | } |
| 89 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 90 | static void unpoison_page(struct page *page) |
| 91 | { |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 92 | void *addr; |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 93 | |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 94 | addr = kmap_atomic(page); |
Vinayak Menon | 9282168 | 2017-03-31 11:13:06 +1100 | [diff] [blame] | 95 | /* |
| 96 | * Page poisoning when enabled poisons each and every page |
| 97 | * that is freed to buddy. Thus no extra check is done to |
| 98 | * see if a page was posioned. |
| 99 | */ |
Prasad Sodagudi | 87f4202 | 2016-02-25 13:01:18 +0530 | [diff] [blame] | 100 | check_poison_mem(page, addr, PAGE_SIZE); |
Akinobu Mita | 64212ec | 2011-10-31 17:08:38 -0700 | [diff] [blame] | 101 | kunmap_atomic(addr); |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static void unpoison_pages(struct page *page, int n) |
| 105 | { |
| 106 | int i; |
| 107 | |
| 108 | for (i = 0; i < n; i++) |
| 109 | unpoison_page(page + i); |
| 110 | } |
| 111 | |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 112 | void kernel_poison_pages(struct page *page, int numpages, int enable) |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 113 | { |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 114 | if (!page_poisoning_enabled()) |
Joonsoo Kim | e30825f | 2014-12-12 16:55:49 -0800 | [diff] [blame] | 115 | return; |
| 116 | |
Akinobu Mita | 6a11f75 | 2009-03-31 15:23:17 -0700 | [diff] [blame] | 117 | if (enable) |
| 118 | unpoison_pages(page, numpages); |
| 119 | else |
| 120 | poison_pages(page, numpages); |
| 121 | } |
Laura Abbott | 8823b1d | 2016-03-15 14:56:27 -0700 | [diff] [blame] | 122 | |
| 123 | #ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC |
| 124 | void __kernel_map_pages(struct page *page, int numpages, int enable) |
| 125 | { |
| 126 | /* This function does nothing, all work is done via poison pages */ |
| 127 | } |
| 128 | #endif |