blob: be19e989ccff51f667c9698c9cb8fea3d5303f8e [file] [log] [blame]
Akinobu Mita6a11f752009-03-31 15:23:17 -07001#include <linux/kernel.h>
Akinobu Mita8c5fb8e2011-10-31 17:08:10 -07002#include <linux/string.h>
Akinobu Mita6a11f752009-03-31 15:23:17 -07003#include <linux/mm.h>
Akinobu Mita64212ec2011-10-31 17:08:38 -07004#include <linux/highmem.h>
Joonsoo Kime30825f2014-12-12 16:55:49 -08005#include <linux/page_ext.h>
Akinobu Mita6a11f752009-03-31 15:23:17 -07006#include <linux/poison.h>
Akinobu Mita77311132011-10-31 17:08:05 -07007#include <linux/ratelimit.h>
Akinobu Mita6a11f752009-03-31 15:23:17 -07008
Laura Abbott8823b1d2016-03-15 14:56:27 -07009static bool want_page_poisoning __read_mostly;
10
11static int early_page_poison_param(char *buf)
12{
13 if (!buf)
14 return -EINVAL;
Minfei Huang2a138dc2016-05-20 16:58:13 -070015 return strtobool(buf, &want_page_poisoning);
Laura Abbott8823b1d2016-03-15 14:56:27 -070016}
17early_param("page_poison", early_page_poison_param);
18
19bool page_poisoning_enabled(void)
20{
Laura Abbott8823b1d2016-03-15 14:56:27 -070021 /*
Vinayak Menonbd33ef32017-05-03 14:54:42 -070022 * Assumes that debug_pagealloc_enabled is set before
23 * free_all_bootmem.
24 * Page poisoning is debug page alloc for some arches. If
25 * either of those options are enabled, enable poisoning.
Laura Abbott8823b1d2016-03-15 14:56:27 -070026 */
Vinayak Menonbd33ef32017-05-03 14:54:42 -070027 return (want_page_poisoning ||
28 (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
29 debug_pagealloc_enabled()));
Akinobu Mita6a11f752009-03-31 15:23:17 -070030}
31
Akinobu Mita6a11f752009-03-31 15:23:17 -070032static void poison_page(struct page *page)
33{
Akinobu Mita64212ec2011-10-31 17:08:38 -070034 void *addr = kmap_atomic(page);
Akinobu Mita6a11f752009-03-31 15:23:17 -070035
Akinobu Mita6a11f752009-03-31 15:23:17 -070036 memset(addr, PAGE_POISON, PAGE_SIZE);
Akinobu Mita64212ec2011-10-31 17:08:38 -070037 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -070038}
39
40static void poison_pages(struct page *page, int n)
41{
42 int i;
43
44 for (i = 0; i < n; i++)
45 poison_page(page + i);
46}
47
48static bool single_bit_flip(unsigned char a, unsigned char b)
49{
50 unsigned char error = a ^ b;
51
52 return error && !(error & (error - 1));
53}
54
55static void check_poison_mem(unsigned char *mem, size_t bytes)
56{
Akinobu Mita77311132011-10-31 17:08:05 -070057 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
Akinobu Mita6a11f752009-03-31 15:23:17 -070058 unsigned char *start;
59 unsigned char *end;
60
Laura Abbott8823b1d2016-03-15 14:56:27 -070061 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
62 return;
63
Akinobu Mita8c5fb8e2011-10-31 17:08:10 -070064 start = memchr_inv(mem, PAGE_POISON, bytes);
65 if (!start)
Akinobu Mita6a11f752009-03-31 15:23:17 -070066 return;
67
68 for (end = mem + bytes - 1; end > start; end--) {
69 if (*end != PAGE_POISON)
70 break;
71 }
72
Akinobu Mita77311132011-10-31 17:08:05 -070073 if (!__ratelimit(&ratelimit))
Akinobu Mita6a11f752009-03-31 15:23:17 -070074 return;
75 else if (start == end && single_bit_flip(*start, PAGE_POISON))
Laura Abbott8823b1d2016-03-15 14:56:27 -070076 pr_err("pagealloc: single bit error\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -070077 else
Laura Abbott8823b1d2016-03-15 14:56:27 -070078 pr_err("pagealloc: memory corruption\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -070079
80 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
81 end - start + 1, 1);
82 dump_stack();
83}
84
Akinobu Mita6a11f752009-03-31 15:23:17 -070085static void unpoison_page(struct page *page)
86{
Akinobu Mita64212ec2011-10-31 17:08:38 -070087 void *addr;
Akinobu Mita6a11f752009-03-31 15:23:17 -070088
Akinobu Mita64212ec2011-10-31 17:08:38 -070089 addr = kmap_atomic(page);
Vinayak Menonbd33ef32017-05-03 14:54:42 -070090 /*
91 * Page poisoning when enabled poisons each and every page
92 * that is freed to buddy. Thus no extra check is done to
93 * see if a page was posioned.
94 */
Akinobu Mita64212ec2011-10-31 17:08:38 -070095 check_poison_mem(addr, PAGE_SIZE);
Akinobu Mita64212ec2011-10-31 17:08:38 -070096 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -070097}
98
99static void unpoison_pages(struct page *page, int n)
100{
101 int i;
102
103 for (i = 0; i < n; i++)
104 unpoison_page(page + i);
105}
106
Laura Abbott8823b1d2016-03-15 14:56:27 -0700107void kernel_poison_pages(struct page *page, int numpages, int enable)
Akinobu Mita6a11f752009-03-31 15:23:17 -0700108{
Laura Abbott8823b1d2016-03-15 14:56:27 -0700109 if (!page_poisoning_enabled())
Joonsoo Kime30825f2014-12-12 16:55:49 -0800110 return;
111
Akinobu Mita6a11f752009-03-31 15:23:17 -0700112 if (enable)
113 unpoison_pages(page, numpages);
114 else
115 poison_pages(page, numpages);
116}
Laura Abbott8823b1d2016-03-15 14:56:27 -0700117
118#ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
119void __kernel_map_pages(struct page *page, int numpages, int enable)
120{
121 /* This function does nothing, all work is done via poison pages */
122}
123#endif