pinctrl: uniphier: add System Bus pin-mux settings
[linux/fpc-iii.git] / mm / page_poison.c
blob1eae5fad2446b4b7b069fde3d0e5b373418ea7bb
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;
10 static bool want_page_poisoning __read_mostly;
12 static int early_page_poison_param(char *buf)
14 if (!buf)
15 return -EINVAL;
16 return strtobool(buf, &want_page_poisoning);
18 early_param("page_poison", early_page_poison_param);
20 bool page_poisoning_enabled(void)
22 return __page_poisoning_enabled;
25 static bool need_page_poisoning(void)
27 return want_page_poisoning;
30 static void init_page_poisoning(void)
33 * page poisoning is debug page alloc for some arches. If either
34 * of those options are enabled, enable poisoning
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;
44 __page_poisoning_enabled = true;
47 struct page_ext_operations page_poisoning_ops = {
48 .need = need_page_poisoning,
49 .init = init_page_poisoning,
52 static inline void set_page_poison(struct page *page)
54 struct page_ext *page_ext;
56 page_ext = lookup_page_ext(page);
57 __set_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
60 static inline void clear_page_poison(struct page *page)
62 struct page_ext *page_ext;
64 page_ext = lookup_page_ext(page);
65 __clear_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
68 bool page_is_poisoned(struct page *page)
70 struct page_ext *page_ext;
72 page_ext = lookup_page_ext(page);
73 if (!page_ext)
74 return false;
76 return test_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
79 static void poison_page(struct page *page)
81 void *addr = kmap_atomic(page);
83 set_page_poison(page);
84 memset(addr, PAGE_POISON, PAGE_SIZE);
85 kunmap_atomic(addr);
88 static void poison_pages(struct page *page, int n)
90 int i;
92 for (i = 0; i < n; i++)
93 poison_page(page + i);
96 static bool single_bit_flip(unsigned char a, unsigned char b)
98 unsigned char error = a ^ b;
100 return error && !(error & (error - 1));
103 static void check_poison_mem(unsigned char *mem, size_t bytes)
105 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
106 unsigned char *start;
107 unsigned char *end;
109 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
110 return;
112 start = memchr_inv(mem, PAGE_POISON, bytes);
113 if (!start)
114 return;
116 for (end = mem + bytes - 1; end > start; end--) {
117 if (*end != PAGE_POISON)
118 break;
121 if (!__ratelimit(&ratelimit))
122 return;
123 else if (start == end && single_bit_flip(*start, PAGE_POISON))
124 pr_err("pagealloc: single bit error\n");
125 else
126 pr_err("pagealloc: memory corruption\n");
128 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
129 end - start + 1, 1);
130 dump_stack();
133 static void unpoison_page(struct page *page)
135 void *addr;
137 if (!page_is_poisoned(page))
138 return;
140 addr = kmap_atomic(page);
141 check_poison_mem(addr, PAGE_SIZE);
142 clear_page_poison(page);
143 kunmap_atomic(addr);
146 static void unpoison_pages(struct page *page, int n)
148 int i;
150 for (i = 0; i < n; i++)
151 unpoison_page(page + i);
154 void kernel_poison_pages(struct page *page, int numpages, int enable)
156 if (!page_poisoning_enabled())
157 return;
159 if (enable)
160 unpoison_pages(page, numpages);
161 else
162 poison_pages(page, numpages);
165 #ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
166 void __kernel_map_pages(struct page *page, int numpages, int enable)
168 /* This function does nothing, all work is done via poison pages */
170 #endif