]> git.karo-electronics.de Git - karo-tx-linux.git/blob - mm/page_poison.c
ARM: dts: imx6ul: move tsc node to appropriate place in the DTB
[karo-tx-linux.git] / mm / page_poison.c
1 #include <linux/kernel.h>
2 #include <linux/string.h>
3 #include <linux/mm.h>
4 #include <linux/highmem.h>
5 #include <linux/page_ext.h>
6 #include <linux/poison.h>
7 #include <linux/ratelimit.h>
8
9 static bool __page_poisoning_enabled __read_mostly;
10 static bool want_page_poisoning __read_mostly =
11         !IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC);
12
13 static int early_page_poison_param(char *buf)
14 {
15         if (!buf)
16                 return -EINVAL;
17
18         if (strcmp(buf, "on") == 0)
19                 want_page_poisoning = true;
20         else if (strcmp(buf, "off") == 0)
21                 want_page_poisoning = false;
22
23         return 0;
24 }
25 early_param("page_poison", early_page_poison_param);
26
27 bool page_poisoning_enabled(void)
28 {
29         return __page_poisoning_enabled;
30 }
31
32 static bool need_page_poisoning(void)
33 {
34         return want_page_poisoning;
35 }
36
37 static void init_page_poisoning(void)
38 {
39         if (!want_page_poisoning)
40                 return;
41
42         __page_poisoning_enabled = true;
43 }
44
45 struct page_ext_operations page_poisoning_ops = {
46         .need = need_page_poisoning,
47         .init = init_page_poisoning,
48 };
49
50 static inline void set_page_poison(struct page *page)
51 {
52         struct page_ext *page_ext;
53
54         page_ext = lookup_page_ext(page);
55         __set_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
56 }
57
58 static inline void clear_page_poison(struct page *page)
59 {
60         struct page_ext *page_ext;
61
62         page_ext = lookup_page_ext(page);
63         __clear_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
64 }
65
66 static inline bool page_poison(struct page *page)
67 {
68         struct page_ext *page_ext;
69
70         page_ext = lookup_page_ext(page);
71         return test_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
72 }
73
74 static void poison_page(struct page *page)
75 {
76         void *addr = kmap_atomic(page);
77
78         set_page_poison(page);
79         memset(addr, PAGE_POISON, PAGE_SIZE);
80         kunmap_atomic(addr);
81 }
82
83 void poison_pages(struct page *page, int n)
84 {
85         int i;
86
87         for (i = 0; i < n; i++)
88                 poison_page(page + i);
89 }
90
91 static bool single_bit_flip(unsigned char a, unsigned char b)
92 {
93         unsigned char error = a ^ b;
94
95         return error && !(error & (error - 1));
96 }
97
98 static void check_poison_mem(unsigned char *mem, size_t bytes)
99 {
100         static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
101         unsigned char *start;
102         unsigned char *end;
103
104         if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
105                 return;
106
107         start = memchr_inv(mem, PAGE_POISON, bytes);
108         if (!start)
109                 return;
110
111         for (end = mem + bytes - 1; end > start; end--) {
112                 if (*end != PAGE_POISON)
113                         break;
114         }
115
116         if (!__ratelimit(&ratelimit))
117                 return;
118         else if (start == end && single_bit_flip(*start, PAGE_POISON))
119                 printk(KERN_ERR "pagealloc: single bit error\n");
120         else
121                 printk(KERN_ERR "pagealloc: memory corruption\n");
122
123         print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
124                         end - start + 1, 1);
125         dump_stack();
126 }
127
128 static void unpoison_page(struct page *page)
129 {
130         void *addr;
131
132         if (!page_poison(page))
133                 return;
134
135         addr = kmap_atomic(page);
136         check_poison_mem(addr, PAGE_SIZE);
137         clear_page_poison(page);
138         kunmap_atomic(addr);
139 }
140
141 void unpoison_pages(struct page *page, int n)
142 {
143         int i;
144
145         for (i = 0; i < n; i++)
146                 unpoison_page(page + i);
147 }
148
149 void kernel_poison_pages(struct page *page, int numpages, int enable)
150 {
151         if (!page_poisoning_enabled())
152                 return;
153
154         if (enable)
155                 unpoison_pages(page, numpages);
156         else
157                 poison_pages(page, numpages);
158 }