blob: e6c994af7518d89167546308b2b759d4bd5c7731 [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
Mateusz Nosek11c9c7e2020-10-15 20:07:33 -070011static DEFINE_STATIC_KEY_FALSE_RO(want_page_poisoning);
Laura Abbott8823b1d2016-03-15 14:56:27 -070012
Dou Liyang14298d32018-04-05 16:23:53 -070013static int __init early_page_poison_param(char *buf)
Laura Abbott8823b1d2016-03-15 14:56:27 -070014{
Mateusz Nosek11c9c7e2020-10-15 20:07:33 -070015 int ret;
16 bool tmp;
17
18 ret = strtobool(buf, &tmp);
19 if (ret)
20 return ret;
21
22 if (tmp)
23 static_branch_enable(&want_page_poisoning);
24 else
25 static_branch_disable(&want_page_poisoning);
26
27 return 0;
Laura Abbott8823b1d2016-03-15 14:56:27 -070028}
29early_param("page_poison", early_page_poison_param);
30
Wei Wangd95f58f2018-08-27 09:32:18 +080031/**
32 * page_poisoning_enabled - check if page poisoning is enabled
33 *
34 * Return true if page poisoning is enabled, or false if not.
35 */
Laura Abbott8823b1d2016-03-15 14:56:27 -070036bool page_poisoning_enabled(void)
37{
Laura Abbott8823b1d2016-03-15 14:56:27 -070038 /*
Vinayak Menonbd33ef32017-05-03 14:54:42 -070039 * Assumes that debug_pagealloc_enabled is set before
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -070040 * memblock_free_all.
Vinayak Menonbd33ef32017-05-03 14:54:42 -070041 * Page poisoning is debug page alloc for some arches. If
42 * either of those options are enabled, enable poisoning.
Laura Abbott8823b1d2016-03-15 14:56:27 -070043 */
Mateusz Nosek11c9c7e2020-10-15 20:07:33 -070044 return (static_branch_unlikely(&want_page_poisoning) ||
Vinayak Menonbd33ef32017-05-03 14:54:42 -070045 (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
46 debug_pagealloc_enabled()));
Akinobu Mita6a11f752009-03-31 15:23:17 -070047}
Wei Wangd95f58f2018-08-27 09:32:18 +080048EXPORT_SYMBOL_GPL(page_poisoning_enabled);
Akinobu Mita6a11f752009-03-31 15:23:17 -070049
Akinobu Mita6a11f752009-03-31 15:23:17 -070050static void poison_page(struct page *page)
51{
Akinobu Mita64212ec2011-10-31 17:08:38 -070052 void *addr = kmap_atomic(page);
Akinobu Mita6a11f752009-03-31 15:23:17 -070053
Qian Cai41179922019-03-05 15:41:24 -080054 /* KASAN still think the page is in-use, so skip it. */
55 kasan_disable_current();
Andrey Konovalov8d4b6fc2020-12-22 12:02:17 -080056 memset(kasan_reset_tag(addr), PAGE_POISON, PAGE_SIZE);
Qian Cai41179922019-03-05 15:41:24 -080057 kasan_enable_current();
Akinobu Mita64212ec2011-10-31 17:08:38 -070058 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -070059}
60
61static void poison_pages(struct page *page, int n)
62{
63 int i;
64
65 for (i = 0; i < n; i++)
66 poison_page(page + i);
67}
68
69static bool single_bit_flip(unsigned char a, unsigned char b)
70{
71 unsigned char error = a ^ b;
72
73 return error && !(error & (error - 1));
74}
75
76static void check_poison_mem(unsigned char *mem, size_t bytes)
77{
Akinobu Mita77311132011-10-31 17:08:05 -070078 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
Akinobu Mita6a11f752009-03-31 15:23:17 -070079 unsigned char *start;
80 unsigned char *end;
81
Laura Abbott8823b1d2016-03-15 14:56:27 -070082 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
83 return;
84
Akinobu Mita8c5fb8e2011-10-31 17:08:10 -070085 start = memchr_inv(mem, PAGE_POISON, bytes);
86 if (!start)
Akinobu Mita6a11f752009-03-31 15:23:17 -070087 return;
88
89 for (end = mem + bytes - 1; end > start; end--) {
90 if (*end != PAGE_POISON)
91 break;
92 }
93
Akinobu Mita77311132011-10-31 17:08:05 -070094 if (!__ratelimit(&ratelimit))
Akinobu Mita6a11f752009-03-31 15:23:17 -070095 return;
96 else if (start == end && single_bit_flip(*start, PAGE_POISON))
Laura Abbott8823b1d2016-03-15 14:56:27 -070097 pr_err("pagealloc: single bit error\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -070098 else
Laura Abbott8823b1d2016-03-15 14:56:27 -070099 pr_err("pagealloc: memory corruption\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -0700100
101 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
102 end - start + 1, 1);
103 dump_stack();
104}
105
Akinobu Mita6a11f752009-03-31 15:23:17 -0700106static void unpoison_page(struct page *page)
107{
Akinobu Mita64212ec2011-10-31 17:08:38 -0700108 void *addr;
Akinobu Mita6a11f752009-03-31 15:23:17 -0700109
Akinobu Mita64212ec2011-10-31 17:08:38 -0700110 addr = kmap_atomic(page);
Vinayak Menonbd33ef32017-05-03 14:54:42 -0700111 /*
112 * Page poisoning when enabled poisons each and every page
113 * that is freed to buddy. Thus no extra check is done to
Christophe JAILLETdbf76842019-09-23 15:34:19 -0700114 * see if a page was poisoned.
Vinayak Menonbd33ef32017-05-03 14:54:42 -0700115 */
Akinobu Mita64212ec2011-10-31 17:08:38 -0700116 check_poison_mem(addr, PAGE_SIZE);
Akinobu Mita64212ec2011-10-31 17:08:38 -0700117 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -0700118}
119
120static void unpoison_pages(struct page *page, int n)
121{
122 int i;
123
124 for (i = 0; i < n; i++)
125 unpoison_page(page + i);
126}
127
Laura Abbott8823b1d2016-03-15 14:56:27 -0700128void kernel_poison_pages(struct page *page, int numpages, int enable)
Akinobu Mita6a11f752009-03-31 15:23:17 -0700129{
Laura Abbott8823b1d2016-03-15 14:56:27 -0700130 if (!page_poisoning_enabled())
Joonsoo Kime30825f2014-12-12 16:55:49 -0800131 return;
132
Akinobu Mita6a11f752009-03-31 15:23:17 -0700133 if (enable)
134 unpoison_pages(page, numpages);
135 else
136 poison_pages(page, numpages);
137}
Laura Abbott8823b1d2016-03-15 14:56:27 -0700138
139#ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
140void __kernel_map_pages(struct page *page, int numpages, int enable)
141{
142 /* This function does nothing, all work is done via poison pages */
143}
144#endif