dm thin metadata: fix __udivdi3 undefined on 32-bit
[linux/fpc-iii.git] / mm / debug-pagealloc.c
blob3b8f1b83610eae2fa04f314c900e008a1d380fe5
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>
9 static bool page_poisoning_enabled __read_mostly;
11 static bool need_page_poisoning(void)
13 if (!debug_pagealloc_enabled())
14 return false;
16 return true;
19 static void init_page_poisoning(void)
21 if (!debug_pagealloc_enabled())
22 return;
24 page_poisoning_enabled = true;
27 struct page_ext_operations page_poisoning_ops = {
28 .need = need_page_poisoning,
29 .init = init_page_poisoning,
32 static inline void set_page_poison(struct page *page)
34 struct page_ext *page_ext;
36 page_ext = lookup_page_ext(page);
37 if (!page_ext)
38 return;
39 __set_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
42 static inline void clear_page_poison(struct page *page)
44 struct page_ext *page_ext;
46 page_ext = lookup_page_ext(page);
47 if (!page_ext)
48 return;
49 __clear_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
52 static inline bool page_poison(struct page *page)
54 struct page_ext *page_ext;
56 page_ext = lookup_page_ext(page);
57 if (!page_ext)
58 return false;
59 return test_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
62 static void poison_page(struct page *page)
64 void *addr = kmap_atomic(page);
66 set_page_poison(page);
67 memset(addr, PAGE_POISON, PAGE_SIZE);
68 kunmap_atomic(addr);
71 static void poison_pages(struct page *page, int n)
73 int i;
75 for (i = 0; i < n; i++)
76 poison_page(page + i);
79 static bool single_bit_flip(unsigned char a, unsigned char b)
81 unsigned char error = a ^ b;
83 return error && !(error & (error - 1));
86 static void check_poison_mem(unsigned char *mem, size_t bytes)
88 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
89 unsigned char *start;
90 unsigned char *end;
92 start = memchr_inv(mem, PAGE_POISON, bytes);
93 if (!start)
94 return;
96 for (end = mem + bytes - 1; end > start; end--) {
97 if (*end != PAGE_POISON)
98 break;
101 if (!__ratelimit(&ratelimit))
102 return;
103 else if (start == end && single_bit_flip(*start, PAGE_POISON))
104 printk(KERN_ERR "pagealloc: single bit error\n");
105 else
106 printk(KERN_ERR "pagealloc: memory corruption\n");
108 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
109 end - start + 1, 1);
110 dump_stack();
113 static void unpoison_page(struct page *page)
115 void *addr;
117 if (!page_poison(page))
118 return;
120 addr = kmap_atomic(page);
121 check_poison_mem(addr, PAGE_SIZE);
122 clear_page_poison(page);
123 kunmap_atomic(addr);
126 static void unpoison_pages(struct page *page, int n)
128 int i;
130 for (i = 0; i < n; i++)
131 unpoison_page(page + i);
134 void __kernel_map_pages(struct page *page, int numpages, int enable)
136 if (!page_poisoning_enabled)
137 return;
139 if (enable)
140 unpoison_pages(page, numpages);
141 else
142 poison_pages(page, numpages);