blob: f0c15e9017c02236e56cb71948d992c584226d0c [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>
Akinobu Mita6a11f752009-03-31 15:23:17 -07009
Laura Abbott8823b1d2016-03-15 14:56:27 -070010static bool want_page_poisoning __read_mostly;
11
Dou Liyang14298d32018-04-05 16:23:53 -070012static int __init early_page_poison_param(char *buf)
Laura Abbott8823b1d2016-03-15 14:56:27 -070013{
14 if (!buf)
15 return -EINVAL;
Minfei Huang2a138dc2016-05-20 16:58:13 -070016 return strtobool(buf, &want_page_poisoning);
Laura Abbott8823b1d2016-03-15 14:56:27 -070017}
18early_param("page_poison", early_page_poison_param);
19
Wei Wangd95f58f2018-08-27 09:32:18 +080020/**
21 * page_poisoning_enabled - check if page poisoning is enabled
22 *
23 * Return true if page poisoning is enabled, or false if not.
24 */
Laura Abbott8823b1d2016-03-15 14:56:27 -070025bool page_poisoning_enabled(void)
26{
Laura Abbott8823b1d2016-03-15 14:56:27 -070027 /*
Vinayak Menonbd33ef32017-05-03 14:54:42 -070028 * Assumes that debug_pagealloc_enabled is set before
Mike Rapoportc6ffc5c2018-10-30 15:09:30 -070029 * memblock_free_all.
Vinayak Menonbd33ef32017-05-03 14:54:42 -070030 * Page poisoning is debug page alloc for some arches. If
31 * either of those options are enabled, enable poisoning.
Laura Abbott8823b1d2016-03-15 14:56:27 -070032 */
Vinayak Menonbd33ef32017-05-03 14:54:42 -070033 return (want_page_poisoning ||
34 (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
35 debug_pagealloc_enabled()));
Akinobu Mita6a11f752009-03-31 15:23:17 -070036}
Wei Wangd95f58f2018-08-27 09:32:18 +080037EXPORT_SYMBOL_GPL(page_poisoning_enabled);
Akinobu Mita6a11f752009-03-31 15:23:17 -070038
Akinobu Mita6a11f752009-03-31 15:23:17 -070039static void poison_page(struct page *page)
40{
Akinobu Mita64212ec2011-10-31 17:08:38 -070041 void *addr = kmap_atomic(page);
Akinobu Mita6a11f752009-03-31 15:23:17 -070042
Akinobu Mita6a11f752009-03-31 15:23:17 -070043 memset(addr, PAGE_POISON, PAGE_SIZE);
Akinobu Mita64212ec2011-10-31 17:08:38 -070044 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -070045}
46
47static void poison_pages(struct page *page, int n)
48{
49 int i;
50
51 for (i = 0; i < n; i++)
52 poison_page(page + i);
53}
54
55static bool single_bit_flip(unsigned char a, unsigned char b)
56{
57 unsigned char error = a ^ b;
58
59 return error && !(error & (error - 1));
60}
61
62static void check_poison_mem(unsigned char *mem, size_t bytes)
63{
Akinobu Mita77311132011-10-31 17:08:05 -070064 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
Akinobu Mita6a11f752009-03-31 15:23:17 -070065 unsigned char *start;
66 unsigned char *end;
67
Laura Abbott8823b1d2016-03-15 14:56:27 -070068 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
69 return;
70
Akinobu Mita8c5fb8e2011-10-31 17:08:10 -070071 start = memchr_inv(mem, PAGE_POISON, bytes);
72 if (!start)
Akinobu Mita6a11f752009-03-31 15:23:17 -070073 return;
74
75 for (end = mem + bytes - 1; end > start; end--) {
76 if (*end != PAGE_POISON)
77 break;
78 }
79
Akinobu Mita77311132011-10-31 17:08:05 -070080 if (!__ratelimit(&ratelimit))
Akinobu Mita6a11f752009-03-31 15:23:17 -070081 return;
82 else if (start == end && single_bit_flip(*start, PAGE_POISON))
Laura Abbott8823b1d2016-03-15 14:56:27 -070083 pr_err("pagealloc: single bit error\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -070084 else
Laura Abbott8823b1d2016-03-15 14:56:27 -070085 pr_err("pagealloc: memory corruption\n");
Akinobu Mita6a11f752009-03-31 15:23:17 -070086
87 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
88 end - start + 1, 1);
89 dump_stack();
90}
91
Akinobu Mita6a11f752009-03-31 15:23:17 -070092static void unpoison_page(struct page *page)
93{
Akinobu Mita64212ec2011-10-31 17:08:38 -070094 void *addr;
Akinobu Mita6a11f752009-03-31 15:23:17 -070095
Akinobu Mita64212ec2011-10-31 17:08:38 -070096 addr = kmap_atomic(page);
Vinayak Menonbd33ef32017-05-03 14:54:42 -070097 /*
98 * Page poisoning when enabled poisons each and every page
99 * that is freed to buddy. Thus no extra check is done to
100 * see if a page was posioned.
101 */
Akinobu Mita64212ec2011-10-31 17:08:38 -0700102 check_poison_mem(addr, PAGE_SIZE);
Akinobu Mita64212ec2011-10-31 17:08:38 -0700103 kunmap_atomic(addr);
Akinobu Mita6a11f752009-03-31 15:23:17 -0700104}
105
106static void unpoison_pages(struct page *page, int n)
107{
108 int i;
109
110 for (i = 0; i < n; i++)
111 unpoison_page(page + i);
112}
113
Laura Abbott8823b1d2016-03-15 14:56:27 -0700114void kernel_poison_pages(struct page *page, int numpages, int enable)
Akinobu Mita6a11f752009-03-31 15:23:17 -0700115{
Laura Abbott8823b1d2016-03-15 14:56:27 -0700116 if (!page_poisoning_enabled())
Joonsoo Kime30825f2014-12-12 16:55:49 -0800117 return;
118
Akinobu Mita6a11f752009-03-31 15:23:17 -0700119 if (enable)
120 unpoison_pages(page, numpages);
121 else
122 poison_pages(page, numpages);
123}
Laura Abbott8823b1d2016-03-15 14:56:27 -0700124
125#ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
126void __kernel_map_pages(struct page *page, int numpages, int enable)
127{
128 /* This function does nothing, all work is done via poison pages */
129}
130#endif