blob: 6cfa8e7d72136502e4ec53d3565a16f8498854ce [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Akinobu Mita6a11f752009-03-31 15:23:17 -07002#include <linux/kernel.h>
Akinobu Mita8c5fb8e2011-10-31 17:08:10 -07003#include <linux/string.h>
Akinobu Mita6a11f752009-03-31 15:23:17 -07004#include <linux/mm.h>
Akinobu Mita64212ec2011-10-31 17:08:38 -07005#include <linux/highmem.h>
Joonsoo Kime30825f2014-12-12 16:55:49 -08006#include <linux/page_ext.h>
Akinobu Mita6a11f752009-03-31 15:23:17 -07007#include <linux/poison.h>
Akinobu Mita77311132011-10-31 17:08:05 -07008#include <linux/ratelimit.h>
Qian Caia6c56bf2019-03-05 15:41:24 -08009#include <linux/kasan.h>
Akinobu Mita6a11f752009-03-31 15:23:17 -070010
Laura Abbott8823b1d2016-03-15 14:56:27 -070011static bool want_page_poisoning __read_mostly;
12
Dou Liyang14298d32018-04-05 16:23:53 -070013static int __init early_page_poison_param(char *buf)
Laura Abbott8823b1d2016-03-15 14:56:27 -070014{
15 if (!buf)
16 return -EINVAL;
Minfei Huang2a138dc2016-05-20 16:58:13 -070017 return strtobool(buf, &want_page_poisoning);
Laura Abbott8823b1d2016-03-15 14:56:27 -070018}
19early_param("page_poison", early_page_poison_param);
20
21bool page_poisoning_enabled(void)
22{
Laura Abbott8823b1d2016-03-15 14:56:27 -070023 /*
Vinayak Menonbd33ef32017-05-03 14:54:42 -070024 * Assumes that debug_pagealloc_enabled is set before
25 * free_all_bootmem.
26 * Page poisoning is debug page alloc for some arches. If
27 * either of those options are enabled, enable poisoning.
Laura Abbott8823b1d2016-03-15 14:56:27 -070028 */
Vinayak Menonbd33ef32017-05-03 14:54:42 -070029 return (want_page_poisoning ||
30 (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
31 debug_pagealloc_enabled()));
Akinobu Mita6a11f752009-03-31 15:23:17 -070032}
33
Akinobu Mita6a11f752009-03-31 15:23:17 -070034static void poison_page(struct page *page)
35{
Akinobu Mita64212ec2011-10-31 17:08:38 -070036 void *addr = kmap_atomic(page);
Akinobu Mita6a11f752009-03-31 15:23:17 -070037
Qian Caia6c56bf2019-03-05 15:41:24 -080038 /* KASAN still think the page is in-use, so skip it. */
39 kasan_disable_current();
Akinobu Mita6a11f752009-03-31 15:23:17 -070040 memset(addr, PAGE_POISON, PAGE_SIZE);
Qian Caia6c56bf2019-03-05 15:41:24 -080041 kasan_enable_current();
Akinobu Mita64212ec2011-10-31 17:08:38 -070042 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -070043}
44
45static void poison_pages(struct page *page, int n)
46{
47 int i;
48
49 for (i = 0; i < n; i++)
50 poison_page(page + i);
51}
52
53static bool single_bit_flip(unsigned char a, unsigned char b)
54{
55 unsigned char error = a ^ b;
56
57 return error && !(error & (error - 1));
58}
59
60static void check_poison_mem(unsigned char *mem, size_t bytes)
61{
Akinobu Mita77311132011-10-31 17:08:05 -070062 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
Akinobu Mita6a11f752009-03-31 15:23:17 -070063 unsigned char *start;
64 unsigned char *end;
65
Laura Abbott8823b1d2016-03-15 14:56:27 -070066 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
67 return;
68
Akinobu Mita8c5fb8e2011-10-31 17:08:10 -070069 start = memchr_inv(mem, PAGE_POISON, bytes);
70 if (!start)
Akinobu Mita6a11f752009-03-31 15:23:17 -070071 return;
72
73 for (end = mem + bytes - 1; end > start; end--) {
74 if (*end != PAGE_POISON)
75 break;
76 }
77
Akinobu Mita77311132011-10-31 17:08:05 -070078 if (!__ratelimit(&ratelimit))
Akinobu Mita6a11f752009-03-31 15:23:17 -070079 return;
80 else if (start == end && single_bit_flip(*start, PAGE_POISON))
Laura Abbott8823b1d2016-03-15 14:56:27 -070081 pr_err("pagealloc: single bit error\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -070082 else
Laura Abbott8823b1d2016-03-15 14:56:27 -070083 pr_err("pagealloc: memory corruption\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -070084
85 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
86 end - start + 1, 1);
87 dump_stack();
88}
89
Akinobu Mita6a11f752009-03-31 15:23:17 -070090static void unpoison_page(struct page *page)
91{
Akinobu Mita64212ec2011-10-31 17:08:38 -070092 void *addr;
Akinobu Mita6a11f752009-03-31 15:23:17 -070093
Akinobu Mita64212ec2011-10-31 17:08:38 -070094 addr = kmap_atomic(page);
Vinayak Menonbd33ef32017-05-03 14:54:42 -070095 /*
96 * Page poisoning when enabled poisons each and every page
97 * that is freed to buddy. Thus no extra check is done to
98 * see if a page was posioned.
99 */
Akinobu Mita64212ec2011-10-31 17:08:38 -0700100 check_poison_mem(addr, PAGE_SIZE);
Akinobu Mita64212ec2011-10-31 17:08:38 -0700101 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -0700102}
103
104static void unpoison_pages(struct page *page, int n)
105{
106 int i;
107
108 for (i = 0; i < n; i++)
109 unpoison_page(page + i);
110}
111
Laura Abbott8823b1d2016-03-15 14:56:27 -0700112void kernel_poison_pages(struct page *page, int numpages, int enable)
Akinobu Mita6a11f752009-03-31 15:23:17 -0700113{
Laura Abbott8823b1d2016-03-15 14:56:27 -0700114 if (!page_poisoning_enabled())
Joonsoo Kime30825f2014-12-12 16:55:49 -0800115 return;
116
Akinobu Mita6a11f752009-03-31 15:23:17 -0700117 if (enable)
118 unpoison_pages(page, numpages);
119 else
120 poison_pages(page, numpages);
121}
Laura Abbott8823b1d2016-03-15 14:56:27 -0700122
123#ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
124void __kernel_map_pages(struct page *page, int numpages, int enable)
125{
126 /* This function does nothing, all work is done via poison pages */
127}
128#endif