]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - mm/page_poison.c
kdump: fix dmesg gdbmacro to work with record based printk
[mirror_ubuntu-zesty-kernel.git] / mm / page_poison.c
CommitLineData
6a11f75b 1#include <linux/kernel.h>
8c5fb8ea 2#include <linux/string.h>
6a11f75b 3#include <linux/mm.h>
64212ec5 4#include <linux/highmem.h>
e30825f1 5#include <linux/page_ext.h>
6a11f75b 6#include <linux/poison.h>
77311139 7#include <linux/ratelimit.h>
6a11f75b 8
8823b1db
LA
9static bool __page_poisoning_enabled __read_mostly;
10static bool want_page_poisoning __read_mostly;
e30825f1 11
8823b1db 12static int early_page_poison_param(char *buf)
e30825f1 13{
8823b1db
LA
14 if (!buf)
15 return -EINVAL;
2a138dc7 16 return strtobool(buf, &want_page_poisoning);
8823b1db
LA
17}
18early_param("page_poison", early_page_poison_param);
19
20bool page_poisoning_enabled(void)
21{
22 return __page_poisoning_enabled;
23}
24
25static bool need_page_poisoning(void)
26{
27 return want_page_poisoning;
e30825f1
JK
28}
29
30static void init_page_poisoning(void)
31{
8823b1db
LA
32 /*
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 }
031bc574 43
8823b1db 44 __page_poisoning_enabled = true;
e30825f1
JK
45}
46
47struct page_ext_operations page_poisoning_ops = {
48 .need = need_page_poisoning,
49 .init = init_page_poisoning,
50};
51
6a11f75b
AM
52static inline void set_page_poison(struct page *page)
53{
e30825f1
JK
54 struct page_ext *page_ext;
55
56 page_ext = lookup_page_ext(page);
57 __set_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
6a11f75b
AM
58}
59
60static inline void clear_page_poison(struct page *page)
61{
e30825f1
JK
62 struct page_ext *page_ext;
63
64 page_ext = lookup_page_ext(page);
65 __clear_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
6a11f75b
AM
66}
67
1414c7f4 68bool page_is_poisoned(struct page *page)
6a11f75b 69{
e30825f1
JK
70 struct page_ext *page_ext;
71
72 page_ext = lookup_page_ext(page);
1414c7f4
LA
73 if (!page_ext)
74 return false;
75
e30825f1 76 return test_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
6a11f75b
AM
77}
78
6a11f75b
AM
79static void poison_page(struct page *page)
80{
64212ec5 81 void *addr = kmap_atomic(page);
6a11f75b 82
6a11f75b 83 set_page_poison(page);
6a11f75b 84 memset(addr, PAGE_POISON, PAGE_SIZE);
64212ec5 85 kunmap_atomic(addr);
6a11f75b
AM
86}
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{
77311139 105 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
6a11f75b
AM
106 unsigned char *start;
107 unsigned char *end;
108
8823b1db
LA
109 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
110 return;
111
8c5fb8ea
AM
112 start = memchr_inv(mem, PAGE_POISON, bytes);
113 if (!start)
6a11f75b
AM
114 return;
115
116 for (end = mem + bytes - 1; end > start; end--) {
117 if (*end != PAGE_POISON)
118 break;
119 }
120
77311139 121 if (!__ratelimit(&ratelimit))
6a11f75b
AM
122 return;
123 else if (start == end && single_bit_flip(*start, PAGE_POISON))
8823b1db 124 pr_err("pagealloc: single bit error\n");
6a11f75b 125 else
8823b1db 126 pr_err("pagealloc: memory corruption\n");
6a11f75b
AM
127
128 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
129 end - start + 1, 1);
130 dump_stack();
131}
132
6a11f75b
AM
133static void unpoison_page(struct page *page)
134{
64212ec5
AM
135 void *addr;
136
1414c7f4 137 if (!page_is_poisoned(page))
6a11f75b 138 return;
6a11f75b 139
64212ec5
AM
140 addr = kmap_atomic(page);
141 check_poison_mem(addr, PAGE_SIZE);
142 clear_page_poison(page);
143 kunmap_atomic(addr);
6a11f75b
AM
144}
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
8823b1db 154void kernel_poison_pages(struct page *page, int numpages, int enable)
6a11f75b 155{
8823b1db 156 if (!page_poisoning_enabled())
e30825f1
JK
157 return;
158
6a11f75b
AM
159 if (enable)
160 unpoison_pages(page, numpages);
161 else
162 poison_pages(page, numpages);
163}
8823b1db
LA
164
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