page_poison.c 3.51 KB
Newer Older
Akinobu Mita's avatar
Akinobu Mita committed
1
#include <linux/kernel.h>
2
#include <linux/string.h>
Akinobu Mita's avatar
Akinobu Mita committed
3
#include <linux/mm.h>
4
#include <linux/highmem.h>
5
#include <linux/page_ext.h>
Akinobu Mita's avatar
Akinobu Mita committed
6
#include <linux/poison.h>
7
#include <linux/ratelimit.h>
Akinobu Mita's avatar
Akinobu Mita committed
8

9 10
static bool __page_poisoning_enabled __read_mostly;
static bool want_page_poisoning __read_mostly;
11

12
static int early_page_poison_param(char *buf)
13
{
14 15
	if (!buf)
		return -EINVAL;
16
	return strtobool(buf, &want_page_poisoning);
17 18 19 20 21 22 23 24 25 26 27
}
early_param("page_poison", early_page_poison_param);

bool page_poisoning_enabled(void)
{
	return __page_poisoning_enabled;
}

static bool need_page_poisoning(void)
{
	return want_page_poisoning;
28 29 30 31
}

static void init_page_poisoning(void)
{
32 33 34 35 36 37 38 39 40 41 42
	/*
	 * page poisoning is debug page alloc for some arches. If either
	 * of those options are enabled, enable poisoning
	 */
	if (!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC)) {
		if (!want_page_poisoning && !debug_pagealloc_enabled())
			return;
	} else {
		if (!want_page_poisoning)
			return;
	}
43

44
	__page_poisoning_enabled = true;
45 46 47 48 49 50 51
}

struct page_ext_operations page_poisoning_ops = {
	.need = need_page_poisoning,
	.init = init_page_poisoning,
};

Akinobu Mita's avatar
Akinobu Mita committed
52 53
static inline void set_page_poison(struct page *page)
{
54 55 56
	struct page_ext *page_ext;

	page_ext = lookup_page_ext(page);
57 58 59
	if (unlikely(!page_ext))
		return;

60
	__set_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
Akinobu Mita's avatar
Akinobu Mita committed
61 62 63 64
}

static inline void clear_page_poison(struct page *page)
{
65 66 67
	struct page_ext *page_ext;

	page_ext = lookup_page_ext(page);
68 69 70
	if (unlikely(!page_ext))
		return;

71
	__clear_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
Akinobu Mita's avatar
Akinobu Mita committed
72 73
}

74
bool page_is_poisoned(struct page *page)
Akinobu Mita's avatar
Akinobu Mita committed
75
{
76 77 78
	struct page_ext *page_ext;

	page_ext = lookup_page_ext(page);
79
	if (unlikely(!page_ext))
80 81
		return false;

82
	return test_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
Akinobu Mita's avatar
Akinobu Mita committed
83 84 85 86
}

static void poison_page(struct page *page)
{
87
	void *addr = kmap_atomic(page);
Akinobu Mita's avatar
Akinobu Mita committed
88 89 90

	set_page_poison(page);
	memset(addr, PAGE_POISON, PAGE_SIZE);
91
	kunmap_atomic(addr);
Akinobu Mita's avatar
Akinobu Mita committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
}

static void poison_pages(struct page *page, int n)
{
	int i;

	for (i = 0; i < n; i++)
		poison_page(page + i);
}

static bool single_bit_flip(unsigned char a, unsigned char b)
{
	unsigned char error = a ^ b;

	return error && !(error & (error - 1));
}

static void check_poison_mem(unsigned char *mem, size_t bytes)
{
111
	static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
Akinobu Mita's avatar
Akinobu Mita committed
112 113 114
	unsigned char *start;
	unsigned char *end;

115 116 117
	if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
		return;

118 119
	start = memchr_inv(mem, PAGE_POISON, bytes);
	if (!start)
Akinobu Mita's avatar
Akinobu Mita committed
120 121 122 123 124 125 126
		return;

	for (end = mem + bytes - 1; end > start; end--) {
		if (*end != PAGE_POISON)
			break;
	}

127
	if (!__ratelimit(&ratelimit))
Akinobu Mita's avatar
Akinobu Mita committed
128 129
		return;
	else if (start == end && single_bit_flip(*start, PAGE_POISON))
130
		pr_err("pagealloc: single bit error\n");
Akinobu Mita's avatar
Akinobu Mita committed
131
	else
132
		pr_err("pagealloc: memory corruption\n");
Akinobu Mita's avatar
Akinobu Mita committed
133 134 135 136 137 138 139 140

	print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
			end - start + 1, 1);
	dump_stack();
}

static void unpoison_page(struct page *page)
{
141 142
	void *addr;

143
	if (!page_is_poisoned(page))
Akinobu Mita's avatar
Akinobu Mita committed
144 145
		return;

146 147 148 149
	addr = kmap_atomic(page);
	check_poison_mem(addr, PAGE_SIZE);
	clear_page_poison(page);
	kunmap_atomic(addr);
Akinobu Mita's avatar
Akinobu Mita committed
150 151 152 153 154 155 156 157 158 159
}

static void unpoison_pages(struct page *page, int n)
{
	int i;

	for (i = 0; i < n; i++)
		unpoison_page(page + i);
}

160
void kernel_poison_pages(struct page *page, int numpages, int enable)
Akinobu Mita's avatar
Akinobu Mita committed
161
{
162
	if (!page_poisoning_enabled())
163 164
		return;

Akinobu Mita's avatar
Akinobu Mita committed
165 166 167 168 169
	if (enable)
		unpoison_pages(page, numpages);
	else
		poison_pages(page, numpages);
}
170 171 172 173 174 175 176

#ifndef CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC
void __kernel_map_pages(struct page *page, int numpages, int enable)
{
	/* This function does nothing, all work is done via poison pages */
}
#endif