]> git.ipfire.org Git - thirdparty/kernel/stable.git/blame - mm/page_poison.c
Linux 4.20.17
[thirdparty/kernel/stable.git] / mm / page_poison.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
6a11f75b 2#include <linux/kernel.h>
8c5fb8ea 3#include <linux/string.h>
6a11f75b 4#include <linux/mm.h>
64212ec5 5#include <linux/highmem.h>
e30825f1 6#include <linux/page_ext.h>
6a11f75b 7#include <linux/poison.h>
77311139 8#include <linux/ratelimit.h>
6a11f75b 9
8823b1db 10static bool want_page_poisoning __read_mostly;
e30825f1 11
14298d36 12static int __init early_page_poison_param(char *buf)
e30825f1 13{
8823b1db
LA
14 if (!buf)
15 return -EINVAL;
2a138dc7 16 return strtobool(buf, &want_page_poisoning);
8823b1db
LA
17}
18early_param("page_poison", early_page_poison_param);
19
d95f58f4
WW
20/**
21 * page_poisoning_enabled - check if page poisoning is enabled
22 *
23 * Return true if page poisoning is enabled, or false if not.
24 */
8823b1db 25bool page_poisoning_enabled(void)
e30825f1 26{
8823b1db 27 /*
bd33ef36 28 * Assumes that debug_pagealloc_enabled is set before
c6ffc5ca 29 * memblock_free_all.
bd33ef36
VM
30 * Page poisoning is debug page alloc for some arches. If
31 * either of those options are enabled, enable poisoning.
8823b1db 32 */
bd33ef36
VM
33 return (want_page_poisoning ||
34 (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
35 debug_pagealloc_enabled()));
6a11f75b 36}
d95f58f4 37EXPORT_SYMBOL_GPL(page_poisoning_enabled);
6a11f75b 38
6a11f75b
AM
39static void poison_page(struct page *page)
40{
64212ec5 41 void *addr = kmap_atomic(page);
6a11f75b 42
6a11f75b 43 memset(addr, PAGE_POISON, PAGE_SIZE);
64212ec5 44 kunmap_atomic(addr);
6a11f75b
AM
45}
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{
77311139 64 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
6a11f75b
AM
65 unsigned char *start;
66 unsigned char *end;
67
8823b1db
LA
68 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
69 return;
70
8c5fb8ea
AM
71 start = memchr_inv(mem, PAGE_POISON, bytes);
72 if (!start)
6a11f75b
AM
73 return;
74
75 for (end = mem + bytes - 1; end > start; end--) {
76 if (*end != PAGE_POISON)
77 break;
78 }
79
77311139 80 if (!__ratelimit(&ratelimit))
6a11f75b
AM
81 return;
82 else if (start == end && single_bit_flip(*start, PAGE_POISON))
8823b1db 83 pr_err("pagealloc: single bit error\n");
6a11f75b 84 else
8823b1db 85 pr_err("pagealloc: memory corruption\n");
6a11f75b
AM
86
87 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
88 end - start + 1, 1);
89 dump_stack();
90}
91
6a11f75b
AM
92static void unpoison_page(struct page *page)
93{
64212ec5
AM
94 void *addr;
95
64212ec5 96 addr = kmap_atomic(page);
bd33ef36
VM
97 /*
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 */
64212ec5 102 check_poison_mem(addr, PAGE_SIZE);
64212ec5 103 kunmap_atomic(addr);
6a11f75b
AM
104}
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
8823b1db 114void kernel_poison_pages(struct page *page, int numpages, int enable)
6a11f75b 115{
8823b1db 116 if (!page_poisoning_enabled())
e30825f1
JK
117 return;
118
6a11f75b
AM
119 if (enable)
120 unpoison_pages(page, numpages);
121 else
122 poison_pages(page, numpages);
123}
8823b1db
LA
124
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