blob: 34b9181ee5d1344eb092c99596131ae5697b2402 [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 Cai41179922019-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
Wei Wangd95f58f2018-08-27 09:32:18 +080021/**
22 * page_poisoning_enabled - check if page poisoning is enabled
23 *
24 * Return true if page poisoning is enabled, or false if not.
25 */
Laura Abbott8823b1d2016-03-15 14:56:27 -070026bool page_poisoning_enabled(void)
27{
Laura Abbott8823b1d2016-03-15 14:56:27 -070028 /*
Vinayak Menonbd33ef32017-05-03 14:54:42 -070029 * Assumes that debug_pagealloc_enabled is set before
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -070030 * memblock_free_all.
Vinayak Menonbd33ef32017-05-03 14:54:42 -070031 * Page poisoning is debug page alloc for some arches. If
32 * either of those options are enabled, enable poisoning.
Laura Abbott8823b1d2016-03-15 14:56:27 -070033 */
Vinayak Menonbd33ef32017-05-03 14:54:42 -070034 return (want_page_poisoning ||
35 (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
36 debug_pagealloc_enabled()));
Akinobu Mita6a11f752009-03-31 15:23:17 -070037}
Wei Wangd95f58f2018-08-27 09:32:18 +080038EXPORT_SYMBOL_GPL(page_poisoning_enabled);
Akinobu Mita6a11f752009-03-31 15:23:17 -070039
Akinobu Mita6a11f752009-03-31 15:23:17 -070040static void poison_page(struct page *page)
41{
Akinobu Mita64212ec2011-10-31 17:08:38 -070042 void *addr = kmap_atomic(page);
Akinobu Mita6a11f752009-03-31 15:23:17 -070043
Qian Cai41179922019-03-05 15:41:24 -080044 /* KASAN still think the page is in-use, so skip it. */
45 kasan_disable_current();
Akinobu Mita6a11f752009-03-31 15:23:17 -070046 memset(addr, PAGE_POISON, PAGE_SIZE);
Qian Cai41179922019-03-05 15:41:24 -080047 kasan_enable_current();
Akinobu Mita64212ec2011-10-31 17:08:38 -070048 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -070049}
50
51static void poison_pages(struct page *page, int n)
52{
53 int i;
54
55 for (i = 0; i < n; i++)
56 poison_page(page + i);
57}
58
59static bool single_bit_flip(unsigned char a, unsigned char b)
60{
61 unsigned char error = a ^ b;
62
63 return error && !(error & (error - 1));
64}
65
66static void check_poison_mem(unsigned char *mem, size_t bytes)
67{
Akinobu Mita77311132011-10-31 17:08:05 -070068 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
Akinobu Mita6a11f752009-03-31 15:23:17 -070069 unsigned char *start;
70 unsigned char *end;
71
Laura Abbott8823b1d2016-03-15 14:56:27 -070072 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
73 return;
74
Akinobu Mita8c5fb8e2011-10-31 17:08:10 -070075 start = memchr_inv(mem, PAGE_POISON, bytes);
76 if (!start)
Akinobu Mita6a11f752009-03-31 15:23:17 -070077 return;
78
79 for (end = mem + bytes - 1; end > start; end--) {
80 if (*end != PAGE_POISON)
81 break;
82 }
83
Akinobu Mita77311132011-10-31 17:08:05 -070084 if (!__ratelimit(&ratelimit))
Akinobu Mita6a11f752009-03-31 15:23:17 -070085 return;
86 else if (start == end && single_bit_flip(*start, PAGE_POISON))
Laura Abbott8823b1d2016-03-15 14:56:27 -070087 pr_err("pagealloc: single bit error\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -070088 else
Laura Abbott8823b1d2016-03-15 14:56:27 -070089 pr_err("pagealloc: memory corruption\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -070090
91 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
92 end - start + 1, 1);
93 dump_stack();
94}
95
Akinobu Mita6a11f752009-03-31 15:23:17 -070096static void unpoison_page(struct page *page)
97{
Akinobu Mita64212ec2011-10-31 17:08:38 -070098 void *addr;
Akinobu Mita6a11f752009-03-31 15:23:17 -070099
Akinobu Mita64212ec2011-10-31 17:08:38 -0700100 addr = kmap_atomic(page);
Vinayak Menonbd33ef32017-05-03 14:54:42 -0700101 /*
102 * Page poisoning when enabled poisons each and every page
103 * that is freed to buddy. Thus no extra check is done to
Christophe JAILLETdbf76842019-09-23 15:34:19 -0700104 * see if a page was poisoned.
Vinayak Menonbd33ef32017-05-03 14:54:42 -0700105 */
Akinobu Mita64212ec2011-10-31 17:08:38 -0700106 check_poison_mem(addr, PAGE_SIZE);
Akinobu Mita64212ec2011-10-31 17:08:38 -0700107 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -0700108}
109
110static void unpoison_pages(struct page *page, int n)
111{
112 int i;
113
114 for (i = 0; i < n; i++)
115 unpoison_page(page + i);
116}
117
Laura Abbott8823b1d2016-03-15 14:56:27 -0700118void kernel_poison_pages(struct page *page, int numpages, int enable)
Akinobu Mita6a11f752009-03-31 15:23:17 -0700119{
Laura Abbott8823b1d2016-03-15 14:56:27 -0700120 if (!page_poisoning_enabled())
Joonsoo Kime30825f2014-12-12 16:55:49 -0800121 return;
122
Akinobu Mita6a11f752009-03-31 15:23:17 -0700123 if (enable)
124 unpoison_pages(page, numpages);
125 else
126 poison_pages(page, numpages);
127}
Laura Abbott8823b1d2016-03-15 14:56:27 -0700128
129#ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
130void __kernel_map_pages(struct page *page, int numpages, int enable)
131{
132 /* This function does nothing, all work is done via poison pages */
133}
134#endif