blob: 1eae5fad2446b4b7b069fde3d0e5b373418ea7bb [file] [log] [blame]
Akinobu Mita6a11f752009-03-31 15:23:17 -07001#include <linux/kernel.h>
Akinobu Mita8c5fb8e2011-10-31 17:08:10 -07002#include <linux/string.h>
Akinobu Mita6a11f752009-03-31 15:23:17 -07003#include <linux/mm.h>
Akinobu Mita64212ec2011-10-31 17:08:38 -07004#include <linux/highmem.h>
Joonsoo Kime30825f2014-12-12 16:55:49 -08005#include <linux/page_ext.h>
Akinobu Mita6a11f752009-03-31 15:23:17 -07006#include <linux/poison.h>
Akinobu Mita77311132011-10-31 17:08:05 -07007#include <linux/ratelimit.h>
Akinobu Mita6a11f752009-03-31 15:23:17 -07008
Laura Abbott8823b1d2016-03-15 14:56:27 -07009static bool __page_poisoning_enabled __read_mostly;
10static bool want_page_poisoning __read_mostly;
11
12static int early_page_poison_param(char *buf)
13{
14 if (!buf)
15 return -EINVAL;
Minfei Huang2a138dc2016-05-20 16:58:13 -070016 return strtobool(buf, &want_page_poisoning);
Laura Abbott8823b1d2016-03-15 14:56:27 -070017}
18early_param("page_poison", early_page_poison_param);
19
20bool page_poisoning_enabled(void)
21{
22 return __page_poisoning_enabled;
23}
Joonsoo Kime30825f2014-12-12 16:55:49 -080024
25static bool need_page_poisoning(void)
26{
Laura Abbott8823b1d2016-03-15 14:56:27 -070027 return want_page_poisoning;
Joonsoo Kime30825f2014-12-12 16:55:49 -080028}
29
30static void init_page_poisoning(void)
31{
Laura Abbott8823b1d2016-03-15 14:56:27 -070032 /*
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 Kim031bc572014-12-12 16:55:52 -080043
Laura Abbott8823b1d2016-03-15 14:56:27 -070044 __page_poisoning_enabled = true;
Joonsoo Kime30825f2014-12-12 16:55:49 -080045}
46
47struct page_ext_operations page_poisoning_ops = {
48 .need = need_page_poisoning,
49 .init = init_page_poisoning,
50};
51
Akinobu Mita6a11f752009-03-31 15:23:17 -070052static inline void set_page_poison(struct page *page)
53{
Joonsoo Kime30825f2014-12-12 16:55:49 -080054 struct page_ext *page_ext;
55
56 page_ext = lookup_page_ext(page);
57 __set_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
Akinobu Mita6a11f752009-03-31 15:23:17 -070058}
59
60static inline void clear_page_poison(struct page *page)
61{
Joonsoo Kime30825f2014-12-12 16:55:49 -080062 struct page_ext *page_ext;
63
64 page_ext = lookup_page_ext(page);
65 __clear_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
Akinobu Mita6a11f752009-03-31 15:23:17 -070066}
67
Laura Abbott1414c7f2016-03-15 14:56:30 -070068bool page_is_poisoned(struct page *page)
Akinobu Mita6a11f752009-03-31 15:23:17 -070069{
Joonsoo Kime30825f2014-12-12 16:55:49 -080070 struct page_ext *page_ext;
71
72 page_ext = lookup_page_ext(page);
Laura Abbott1414c7f2016-03-15 14:56:30 -070073 if (!page_ext)
74 return false;
75
Joonsoo Kime30825f2014-12-12 16:55:49 -080076 return test_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
Akinobu Mita6a11f752009-03-31 15:23:17 -070077}
78
Akinobu Mita6a11f752009-03-31 15:23:17 -070079static void poison_page(struct page *page)
80{
Akinobu Mita64212ec2011-10-31 17:08:38 -070081 void *addr = kmap_atomic(page);
Akinobu Mita6a11f752009-03-31 15:23:17 -070082
Akinobu Mita6a11f752009-03-31 15:23:17 -070083 set_page_poison(page);
Akinobu Mita6a11f752009-03-31 15:23:17 -070084 memset(addr, PAGE_POISON, PAGE_SIZE);
Akinobu Mita64212ec2011-10-31 17:08:38 -070085 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -070086}
87
88static void poison_pages(struct page *page, int n)
89{
90 int i;
91
92 for (i = 0; i < n; i++)
93 poison_page(page + i);
94}
95
96static bool single_bit_flip(unsigned char a, unsigned char b)
97{
98 unsigned char error = a ^ b;
99
100 return error && !(error & (error - 1));
101}
102
103static void check_poison_mem(unsigned char *mem, size_t bytes)
104{
Akinobu Mita77311132011-10-31 17:08:05 -0700105 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
Akinobu Mita6a11f752009-03-31 15:23:17 -0700106 unsigned char *start;
107 unsigned char *end;
108
Laura Abbott8823b1d2016-03-15 14:56:27 -0700109 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
110 return;
111
Akinobu Mita8c5fb8e2011-10-31 17:08:10 -0700112 start = memchr_inv(mem, PAGE_POISON, bytes);
113 if (!start)
Akinobu Mita6a11f752009-03-31 15:23:17 -0700114 return;
115
116 for (end = mem + bytes - 1; end > start; end--) {
117 if (*end != PAGE_POISON)
118 break;
119 }
120
Akinobu Mita77311132011-10-31 17:08:05 -0700121 if (!__ratelimit(&ratelimit))
Akinobu Mita6a11f752009-03-31 15:23:17 -0700122 return;
123 else if (start == end && single_bit_flip(*start, PAGE_POISON))
Laura Abbott8823b1d2016-03-15 14:56:27 -0700124 pr_err("pagealloc: single bit error\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -0700125 else
Laura Abbott8823b1d2016-03-15 14:56:27 -0700126 pr_err("pagealloc: memory corruption\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -0700127
128 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
129 end - start + 1, 1);
130 dump_stack();
131}
132
Akinobu Mita6a11f752009-03-31 15:23:17 -0700133static void unpoison_page(struct page *page)
134{
Akinobu Mita64212ec2011-10-31 17:08:38 -0700135 void *addr;
Akinobu Mita6a11f752009-03-31 15:23:17 -0700136
Laura Abbott1414c7f2016-03-15 14:56:30 -0700137 if (!page_is_poisoned(page))
Akinobu Mita64212ec2011-10-31 17:08:38 -0700138 return;
139
140 addr = kmap_atomic(page);
141 check_poison_mem(addr, PAGE_SIZE);
142 clear_page_poison(page);
143 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -0700144}
145
146static void unpoison_pages(struct page *page, int n)
147{
148 int i;
149
150 for (i = 0; i < n; i++)
151 unpoison_page(page + i);
152}
153
Laura Abbott8823b1d2016-03-15 14:56:27 -0700154void kernel_poison_pages(struct page *page, int numpages, int enable)
Akinobu Mita6a11f752009-03-31 15:23:17 -0700155{
Laura Abbott8823b1d2016-03-15 14:56:27 -0700156 if (!page_poisoning_enabled())
Joonsoo Kime30825f2014-12-12 16:55:49 -0800157 return;
158
Akinobu Mita6a11f752009-03-31 15:23:17 -0700159 if (enable)
160 unpoison_pages(page, numpages);
161 else
162 poison_pages(page, numpages);
163}
Laura Abbott8823b1d2016-03-15 14:56:27 -0700164
165#ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
166void __kernel_map_pages(struct page *page, int numpages, int enable)
167{
168 /* This function does nothing, all work is done via poison pages */
169}
170#endif