blob: 0072f2c533313fe9e086788e74663f796eab163d [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
Joonsoo Kime30825f2014-12-12 16:55:49 -08009static bool page_poisoning_enabled __read_mostly;
10
11static bool need_page_poisoning(void)
12{
13 return true;
14}
15
16static void init_page_poisoning(void)
17{
18 page_poisoning_enabled = true;
19}
20
21struct page_ext_operations page_poisoning_ops = {
22 .need = need_page_poisoning,
23 .init = init_page_poisoning,
24};
25
Akinobu Mita6a11f752009-03-31 15:23:17 -070026static inline void set_page_poison(struct page *page)
27{
Joonsoo Kime30825f2014-12-12 16:55:49 -080028 struct page_ext *page_ext;
29
30 page_ext = lookup_page_ext(page);
31 __set_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
Akinobu Mita6a11f752009-03-31 15:23:17 -070032}
33
34static inline void clear_page_poison(struct page *page)
35{
Joonsoo Kime30825f2014-12-12 16:55:49 -080036 struct page_ext *page_ext;
37
38 page_ext = lookup_page_ext(page);
39 __clear_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
Akinobu Mita6a11f752009-03-31 15:23:17 -070040}
41
42static inline bool page_poison(struct page *page)
43{
Joonsoo Kime30825f2014-12-12 16:55:49 -080044 struct page_ext *page_ext;
45
46 page_ext = lookup_page_ext(page);
47 return test_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
Akinobu Mita6a11f752009-03-31 15:23:17 -070048}
49
Akinobu Mita6a11f752009-03-31 15:23:17 -070050static void poison_page(struct page *page)
51{
Akinobu Mita64212ec2011-10-31 17:08:38 -070052 void *addr = kmap_atomic(page);
Akinobu Mita6a11f752009-03-31 15:23:17 -070053
Akinobu Mita6a11f752009-03-31 15:23:17 -070054 set_page_poison(page);
Akinobu Mita6a11f752009-03-31 15:23:17 -070055 memset(addr, PAGE_POISON, PAGE_SIZE);
Akinobu Mita64212ec2011-10-31 17:08:38 -070056 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -070057}
58
59static void poison_pages(struct page *page, int n)
60{
61 int i;
62
63 for (i = 0; i < n; i++)
64 poison_page(page + i);
65}
66
67static bool single_bit_flip(unsigned char a, unsigned char b)
68{
69 unsigned char error = a ^ b;
70
71 return error && !(error & (error - 1));
72}
73
74static void check_poison_mem(unsigned char *mem, size_t bytes)
75{
Akinobu Mita77311132011-10-31 17:08:05 -070076 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
Akinobu Mita6a11f752009-03-31 15:23:17 -070077 unsigned char *start;
78 unsigned char *end;
79
Akinobu Mita8c5fb8e2011-10-31 17:08:10 -070080 start = memchr_inv(mem, PAGE_POISON, bytes);
81 if (!start)
Akinobu Mita6a11f752009-03-31 15:23:17 -070082 return;
83
84 for (end = mem + bytes - 1; end > start; end--) {
85 if (*end != PAGE_POISON)
86 break;
87 }
88
Akinobu Mita77311132011-10-31 17:08:05 -070089 if (!__ratelimit(&ratelimit))
Akinobu Mita6a11f752009-03-31 15:23:17 -070090 return;
91 else if (start == end && single_bit_flip(*start, PAGE_POISON))
92 printk(KERN_ERR "pagealloc: single bit error\n");
93 else
94 printk(KERN_ERR "pagealloc: memory corruption\n");
95
96 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
97 end - start + 1, 1);
98 dump_stack();
99}
100
Akinobu Mita6a11f752009-03-31 15:23:17 -0700101static void unpoison_page(struct page *page)
102{
Akinobu Mita64212ec2011-10-31 17:08:38 -0700103 void *addr;
Akinobu Mita6a11f752009-03-31 15:23:17 -0700104
Akinobu Mita64212ec2011-10-31 17:08:38 -0700105 if (!page_poison(page))
106 return;
107
108 addr = kmap_atomic(page);
109 check_poison_mem(addr, PAGE_SIZE);
110 clear_page_poison(page);
111 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -0700112}
113
114static void unpoison_pages(struct page *page, int n)
115{
116 int i;
117
118 for (i = 0; i < n; i++)
119 unpoison_page(page + i);
120}
121
122void kernel_map_pages(struct page *page, int numpages, int enable)
123{
Joonsoo Kime30825f2014-12-12 16:55:49 -0800124 if (!page_poisoning_enabled)
125 return;
126
Akinobu Mita6a11f752009-03-31 15:23:17 -0700127 if (enable)
128 unpoison_pages(page, numpages);
129 else
130 poison_pages(page, numpages);
131}