]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - mm/page_poison.c
mm/memory_hotplug: remove a wrapper for alloc_migration_target()
[mirror_ubuntu-jammy-kernel.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>
4117992d 9#include <linux/kasan.h>
6a11f75b 10
11c9c7ed 11static DEFINE_STATIC_KEY_FALSE_RO(want_page_poisoning);
e30825f1 12
14298d36 13static int __init early_page_poison_param(char *buf)
e30825f1 14{
11c9c7ed
MN
15 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;
8823b1db
LA
28}
29early_param("page_poison", early_page_poison_param);
30
d95f58f4
WW
31/**
32 * page_poisoning_enabled - check if page poisoning is enabled
33 *
34 * Return true if page poisoning is enabled, or false if not.
35 */
8823b1db 36bool page_poisoning_enabled(void)
e30825f1 37{
8823b1db 38 /*
bd33ef36 39 * Assumes that debug_pagealloc_enabled is set before
c6ffc5ca 40 * memblock_free_all.
bd33ef36
VM
41 * Page poisoning is debug page alloc for some arches. If
42 * either of those options are enabled, enable poisoning.
8823b1db 43 */
11c9c7ed 44 return (static_branch_unlikely(&want_page_poisoning) ||
bd33ef36
VM
45 (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
46 debug_pagealloc_enabled()));
6a11f75b 47}
d95f58f4 48EXPORT_SYMBOL_GPL(page_poisoning_enabled);
6a11f75b 49
6a11f75b
AM
50static void poison_page(struct page *page)
51{
64212ec5 52 void *addr = kmap_atomic(page);
6a11f75b 53
4117992d
QC
54 /* KASAN still think the page is in-use, so skip it. */
55 kasan_disable_current();
6a11f75b 56 memset(addr, PAGE_POISON, PAGE_SIZE);
4117992d 57 kasan_enable_current();
64212ec5 58 kunmap_atomic(addr);
6a11f75b
AM
59}
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{
77311139 78 static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
6a11f75b
AM
79 unsigned char *start;
80 unsigned char *end;
81
8823b1db
LA
82 if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
83 return;
84
8c5fb8ea
AM
85 start = memchr_inv(mem, PAGE_POISON, bytes);
86 if (!start)
6a11f75b
AM
87 return;
88
89 for (end = mem + bytes - 1; end > start; end--) {
90 if (*end != PAGE_POISON)
91 break;
92 }
93
77311139 94 if (!__ratelimit(&ratelimit))
6a11f75b
AM
95 return;
96 else if (start == end && single_bit_flip(*start, PAGE_POISON))
8823b1db 97 pr_err("pagealloc: single bit error\n");
6a11f75b 98 else
8823b1db 99 pr_err("pagealloc: memory corruption\n");
6a11f75b
AM
100
101 print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
102 end - start + 1, 1);
103 dump_stack();
104}
105
6a11f75b
AM
106static void unpoison_page(struct page *page)
107{
64212ec5
AM
108 void *addr;
109
64212ec5 110 addr = kmap_atomic(page);
bd33ef36
VM
111 /*
112 * Page poisoning when enabled poisons each and every page
113 * that is freed to buddy. Thus no extra check is done to
dbf7684e 114 * see if a page was poisoned.
bd33ef36 115 */
64212ec5 116 check_poison_mem(addr, PAGE_SIZE);
64212ec5 117 kunmap_atomic(addr);
6a11f75b
AM
118}
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
8823b1db 128void kernel_poison_pages(struct page *page, int numpages, int enable)
6a11f75b 129{
8823b1db 130 if (!page_poisoning_enabled())
e30825f1
JK
131 return;
132
6a11f75b
AM
133 if (enable)
134 unpoison_pages(page, numpages);
135 else
136 poison_pages(page, numpages);
137}
8823b1db
LA
138
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