1 #include <linux/kernel.h>
2 #include <linux/string.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
)
17 if (strcmp(buf
, "on") == 0)
18 want_page_poisoning
= true;
19 else if (strcmp(buf
, "off") == 0)
20 want_page_poisoning
= false;
24 early_param("page_poison", early_page_poison_param
);
26 bool page_poisoning_enabled(void)
28 return __page_poisoning_enabled
;
31 static bool need_page_poisoning(void)
33 return want_page_poisoning
;
36 static void init_page_poisoning(void)
39 * page poisoning is debug page alloc for some arches. If either
40 * of those options are enabled, enable poisoning
42 if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
)) {
43 if (!want_page_poisoning
&& !debug_pagealloc_enabled())
46 if (!want_page_poisoning
)
50 __page_poisoning_enabled
= true;
53 struct page_ext_operations page_poisoning_ops
= {
54 .need
= need_page_poisoning
,
55 .init
= init_page_poisoning
,
58 static inline void set_page_poison(struct page
*page
)
60 struct page_ext
*page_ext
;
62 page_ext
= lookup_page_ext(page
);
63 __set_bit(PAGE_EXT_DEBUG_POISON
, &page_ext
->flags
);
66 static inline void clear_page_poison(struct page
*page
)
68 struct page_ext
*page_ext
;
70 page_ext
= lookup_page_ext(page
);
71 __clear_bit(PAGE_EXT_DEBUG_POISON
, &page_ext
->flags
);
74 bool page_is_poisoned(struct page
*page
)
76 struct page_ext
*page_ext
;
78 page_ext
= lookup_page_ext(page
);
82 return test_bit(PAGE_EXT_DEBUG_POISON
, &page_ext
->flags
);
85 static void poison_page(struct page
*page
)
87 void *addr
= kmap_atomic(page
);
89 set_page_poison(page
);
90 memset(addr
, PAGE_POISON
, PAGE_SIZE
);
94 static void poison_pages(struct page
*page
, int n
)
98 for (i
= 0; i
< n
; i
++)
99 poison_page(page
+ i
);
102 static bool single_bit_flip(unsigned char a
, unsigned char b
)
104 unsigned char error
= a
^ b
;
106 return error
&& !(error
& (error
- 1));
109 static void check_poison_mem(unsigned char *mem
, size_t bytes
)
111 static DEFINE_RATELIMIT_STATE(ratelimit
, 5 * HZ
, 10);
112 unsigned char *start
;
115 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY
))
118 start
= memchr_inv(mem
, PAGE_POISON
, bytes
);
122 for (end
= mem
+ bytes
- 1; end
> start
; end
--) {
123 if (*end
!= PAGE_POISON
)
127 if (!__ratelimit(&ratelimit
))
129 else if (start
== end
&& single_bit_flip(*start
, PAGE_POISON
))
130 pr_err("pagealloc: single bit error\n");
132 pr_err("pagealloc: memory corruption\n");
134 print_hex_dump(KERN_ERR
, "", DUMP_PREFIX_ADDRESS
, 16, 1, start
,
139 static void unpoison_page(struct page
*page
)
143 if (!page_is_poisoned(page
))
146 addr
= kmap_atomic(page
);
147 check_poison_mem(addr
, PAGE_SIZE
);
148 clear_page_poison(page
);
152 static void unpoison_pages(struct page
*page
, int n
)
156 for (i
= 0; i
< n
; i
++)
157 unpoison_page(page
+ i
);
160 void kernel_poison_pages(struct page
*page
, int numpages
, int enable
)
162 if (!page_poisoning_enabled())
166 unpoison_pages(page
, numpages
);
168 poison_pages(page
, numpages
);
171 #ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
172 void __kernel_map_pages(struct page
*page
, int numpages
, int enable
)
174 /* This function does nothing, all work is done via poison pages */