1 #include <linux/kernel.h>
3 #include <linux/page-debug-flags.h>
4 #include <linux/poison.h>
5 #include <linux/ratelimit.h>
7 static inline void set_page_poison(struct page *page)
9 __set_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags);
12 static inline void clear_page_poison(struct page *page)
14 __clear_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags);
17 static inline bool page_poison(struct page *page)
19 return test_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags);
22 static void poison_highpage(struct page *page)
25 * Page poisoning for highmem pages is not implemented.
27 * This can be called from interrupt contexts.
28 * So we need to create a new kmap_atomic slot for this
29 * application and it will need interrupt protection.
33 static void poison_page(struct page *page)
37 if (PageHighMem(page)) {
38 poison_highpage(page);
41 set_page_poison(page);
42 addr = page_address(page);
43 memset(addr, PAGE_POISON, PAGE_SIZE);
46 static void poison_pages(struct page *page, int n)
50 for (i = 0; i < n; i++)
51 poison_page(page + i);
54 static bool single_bit_flip(unsigned char a, unsigned char b)
56 unsigned char error = a ^ b;
58 return error && !(error & (error - 1));
61 static void check_poison_mem(unsigned char *mem, size_t bytes)
63 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
67 for (start = mem; start < mem + bytes; start++) {
68 if (*start != PAGE_POISON)
71 if (start == mem + bytes)
74 for (end = mem + bytes - 1; end > start; end--) {
75 if (*end != PAGE_POISON)
79 if (!__ratelimit(&ratelimit))
81 else if (start == end && single_bit_flip(*start, PAGE_POISON))
82 printk(KERN_ERR "pagealloc: single bit error\n");
84 printk(KERN_ERR "pagealloc: memory corruption\n");
86 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
91 static void unpoison_highpage(struct page *page)
94 * See comment in poison_highpage().
95 * Highmem pages should not be poisoned for now
97 BUG_ON(page_poison(page));
100 static void unpoison_page(struct page *page)
102 if (PageHighMem(page)) {
103 unpoison_highpage(page);
106 if (page_poison(page)) {
107 void *addr = page_address(page);
109 check_poison_mem(addr, PAGE_SIZE);
110 clear_page_poison(page);
114 static void unpoison_pages(struct page *page, int n)
118 for (i = 0; i < n; i++)
119 unpoison_page(page + i);
122 void kernel_map_pages(struct page *page, int numpages, int enable)
124 if (!debug_pagealloc_enabled)
128 unpoison_pages(page, numpages);
130 poison_pages(page, numpages);