]>
Commit | Line | Data |
---|---|---|
1 | #include <linux/kernel.h> | |
2 | #include <linux/mm.h> | |
3 | #include <linux/page-debug-flags.h> | |
4 | #include <linux/poison.h> | |
5 | #include <linux/ratelimit.h> | |
6 | ||
7 | static inline void set_page_poison(struct page *page) | |
8 | { | |
9 | __set_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags); | |
10 | } | |
11 | ||
12 | static inline void clear_page_poison(struct page *page) | |
13 | { | |
14 | __clear_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags); | |
15 | } | |
16 | ||
17 | static inline bool page_poison(struct page *page) | |
18 | { | |
19 | return test_bit(PAGE_DEBUG_FLAG_POISON, &page->debug_flags); | |
20 | } | |
21 | ||
22 | static void poison_highpage(struct page *page) | |
23 | { | |
24 | /* | |
25 | * Page poisoning for highmem pages is not implemented. | |
26 | * | |
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. | |
30 | */ | |
31 | } | |
32 | ||
33 | static void poison_page(struct page *page) | |
34 | { | |
35 | void *addr; | |
36 | ||
37 | if (PageHighMem(page)) { | |
38 | poison_highpage(page); | |
39 | return; | |
40 | } | |
41 | set_page_poison(page); | |
42 | addr = page_address(page); | |
43 | memset(addr, PAGE_POISON, PAGE_SIZE); | |
44 | } | |
45 | ||
46 | static void poison_pages(struct page *page, int n) | |
47 | { | |
48 | int i; | |
49 | ||
50 | for (i = 0; i < n; i++) | |
51 | poison_page(page + i); | |
52 | } | |
53 | ||
54 | static bool single_bit_flip(unsigned char a, unsigned char b) | |
55 | { | |
56 | unsigned char error = a ^ b; | |
57 | ||
58 | return error && !(error & (error - 1)); | |
59 | } | |
60 | ||
61 | static void check_poison_mem(unsigned char *mem, size_t bytes) | |
62 | { | |
63 | static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10); | |
64 | unsigned char *start; | |
65 | unsigned char *end; | |
66 | ||
67 | for (start = mem; start < mem + bytes; start++) { | |
68 | if (*start != PAGE_POISON) | |
69 | break; | |
70 | } | |
71 | if (start == mem + bytes) | |
72 | return; | |
73 | ||
74 | for (end = mem + bytes - 1; end > start; end--) { | |
75 | if (*end != PAGE_POISON) | |
76 | break; | |
77 | } | |
78 | ||
79 | if (!__ratelimit(&ratelimit)) | |
80 | return; | |
81 | else if (start == end && single_bit_flip(*start, PAGE_POISON)) | |
82 | printk(KERN_ERR "pagealloc: single bit error\n"); | |
83 | else | |
84 | printk(KERN_ERR "pagealloc: memory corruption\n"); | |
85 | ||
86 | print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start, | |
87 | end - start + 1, 1); | |
88 | dump_stack(); | |
89 | } | |
90 | ||
91 | static void unpoison_highpage(struct page *page) | |
92 | { | |
93 | /* | |
94 | * See comment in poison_highpage(). | |
95 | * Highmem pages should not be poisoned for now | |
96 | */ | |
97 | BUG_ON(page_poison(page)); | |
98 | } | |
99 | ||
100 | static void unpoison_page(struct page *page) | |
101 | { | |
102 | if (PageHighMem(page)) { | |
103 | unpoison_highpage(page); | |
104 | return; | |
105 | } | |
106 | if (page_poison(page)) { | |
107 | void *addr = page_address(page); | |
108 | ||
109 | check_poison_mem(addr, PAGE_SIZE); | |
110 | clear_page_poison(page); | |
111 | } | |
112 | } | |
113 | ||
114 | static 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 | ||
122 | void kernel_map_pages(struct page *page, int numpages, int enable) | |
123 | { | |
124 | if (!debug_pagealloc_enabled) | |
125 | return; | |
126 | ||
127 | if (enable) | |
128 | unpoison_pages(page, numpages); | |
129 | else | |
130 | poison_pages(page, numpages); | |
131 | } |