18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/kernel.h>
38c2ecf20Sopenharmony_ci#include <linux/string.h>
48c2ecf20Sopenharmony_ci#include <linux/mm.h>
58c2ecf20Sopenharmony_ci#include <linux/mmdebug.h>
68c2ecf20Sopenharmony_ci#include <linux/highmem.h>
78c2ecf20Sopenharmony_ci#include <linux/page_ext.h>
88c2ecf20Sopenharmony_ci#include <linux/poison.h>
98c2ecf20Sopenharmony_ci#include <linux/ratelimit.h>
108c2ecf20Sopenharmony_ci#include <linux/kasan.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_cistatic DEFINE_STATIC_KEY_FALSE_RO(want_page_poisoning);
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_cistatic int __init early_page_poison_param(char *buf)
158c2ecf20Sopenharmony_ci{
168c2ecf20Sopenharmony_ci	int ret;
178c2ecf20Sopenharmony_ci	bool tmp;
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci	ret = strtobool(buf, &tmp);
208c2ecf20Sopenharmony_ci	if (ret)
218c2ecf20Sopenharmony_ci		return ret;
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci	if (tmp)
248c2ecf20Sopenharmony_ci		static_branch_enable(&want_page_poisoning);
258c2ecf20Sopenharmony_ci	else
268c2ecf20Sopenharmony_ci		static_branch_disable(&want_page_poisoning);
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci	return 0;
298c2ecf20Sopenharmony_ci}
308c2ecf20Sopenharmony_ciearly_param("page_poison", early_page_poison_param);
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/**
338c2ecf20Sopenharmony_ci * page_poisoning_enabled - check if page poisoning is enabled
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * Return true if page poisoning is enabled, or false if not.
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_cibool page_poisoning_enabled(void)
388c2ecf20Sopenharmony_ci{
398c2ecf20Sopenharmony_ci	/*
408c2ecf20Sopenharmony_ci	 * Assumes that debug_pagealloc_enabled is set before
418c2ecf20Sopenharmony_ci	 * memblock_free_all.
428c2ecf20Sopenharmony_ci	 * Page poisoning is debug page alloc for some arches. If
438c2ecf20Sopenharmony_ci	 * either of those options are enabled, enable poisoning.
448c2ecf20Sopenharmony_ci	 */
458c2ecf20Sopenharmony_ci	return (static_branch_unlikely(&want_page_poisoning) ||
468c2ecf20Sopenharmony_ci		(!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
478c2ecf20Sopenharmony_ci		debug_pagealloc_enabled()));
488c2ecf20Sopenharmony_ci}
498c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(page_poisoning_enabled);
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic void poison_page(struct page *page)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	void *addr = kmap_atomic(page);
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	/* KASAN still think the page is in-use, so skip it. */
568c2ecf20Sopenharmony_ci	kasan_disable_current();
578c2ecf20Sopenharmony_ci	memset(addr, PAGE_POISON, PAGE_SIZE);
588c2ecf20Sopenharmony_ci	kasan_enable_current();
598c2ecf20Sopenharmony_ci	kunmap_atomic(addr);
608c2ecf20Sopenharmony_ci}
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_cistatic void poison_pages(struct page *page, int n)
638c2ecf20Sopenharmony_ci{
648c2ecf20Sopenharmony_ci	int i;
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++)
678c2ecf20Sopenharmony_ci		poison_page(page + i);
688c2ecf20Sopenharmony_ci}
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_cistatic bool single_bit_flip(unsigned char a, unsigned char b)
718c2ecf20Sopenharmony_ci{
728c2ecf20Sopenharmony_ci	unsigned char error = a ^ b;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_ci	return error && !(error & (error - 1));
758c2ecf20Sopenharmony_ci}
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic void check_poison_mem(struct page *page, unsigned char *mem, size_t bytes)
788c2ecf20Sopenharmony_ci{
798c2ecf20Sopenharmony_ci	static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
808c2ecf20Sopenharmony_ci	unsigned char *start;
818c2ecf20Sopenharmony_ci	unsigned char *end;
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
848c2ecf20Sopenharmony_ci		return;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci	start = memchr_inv(mem, PAGE_POISON, bytes);
878c2ecf20Sopenharmony_ci	if (!start)
888c2ecf20Sopenharmony_ci		return;
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	for (end = mem + bytes - 1; end > start; end--) {
918c2ecf20Sopenharmony_ci		if (*end != PAGE_POISON)
928c2ecf20Sopenharmony_ci			break;
938c2ecf20Sopenharmony_ci	}
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_ci	if (!__ratelimit(&ratelimit))
968c2ecf20Sopenharmony_ci		return;
978c2ecf20Sopenharmony_ci	else if (start == end && single_bit_flip(*start, PAGE_POISON))
988c2ecf20Sopenharmony_ci		pr_err("pagealloc: single bit error\n");
998c2ecf20Sopenharmony_ci	else
1008c2ecf20Sopenharmony_ci		pr_err("pagealloc: memory corruption\n");
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci	print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
1038c2ecf20Sopenharmony_ci			end - start + 1, 1);
1048c2ecf20Sopenharmony_ci	dump_stack();
1058c2ecf20Sopenharmony_ci	dump_page(page, "pagealloc: corrupted page details");
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic void unpoison_page(struct page *page)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	void *addr;
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci	addr = kmap_atomic(page);
1138c2ecf20Sopenharmony_ci	kasan_disable_current();
1148c2ecf20Sopenharmony_ci	/*
1158c2ecf20Sopenharmony_ci	 * Page poisoning when enabled poisons each and every page
1168c2ecf20Sopenharmony_ci	 * that is freed to buddy. Thus no extra check is done to
1178c2ecf20Sopenharmony_ci	 * see if a page was poisoned.
1188c2ecf20Sopenharmony_ci	 */
1198c2ecf20Sopenharmony_ci	check_poison_mem(page, kasan_reset_tag(addr), PAGE_SIZE);
1208c2ecf20Sopenharmony_ci	kasan_enable_current();
1218c2ecf20Sopenharmony_ci	kunmap_atomic(addr);
1228c2ecf20Sopenharmony_ci}
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic void unpoison_pages(struct page *page, int n)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	int i;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	for (i = 0; i < n; i++)
1298c2ecf20Sopenharmony_ci		unpoison_page(page + i);
1308c2ecf20Sopenharmony_ci}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_civoid kernel_poison_pages(struct page *page, int numpages, int enable)
1338c2ecf20Sopenharmony_ci{
1348c2ecf20Sopenharmony_ci	if (!page_poisoning_enabled())
1358c2ecf20Sopenharmony_ci		return;
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	if (enable)
1388c2ecf20Sopenharmony_ci		unpoison_pages(page, numpages);
1398c2ecf20Sopenharmony_ci	else
1408c2ecf20Sopenharmony_ci		poison_pages(page, numpages);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci#ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
1448c2ecf20Sopenharmony_civoid __kernel_map_pages(struct page *page, int numpages, int enable)
1458c2ecf20Sopenharmony_ci{
1468c2ecf20Sopenharmony_ci	/* This function does nothing, all work is done via poison pages */
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci#endif
149