Blame view

mm/page_poison.c 2.97 KB
b24413180   Greg Kroah-Hartman   License cleanup: ...
1
  // SPDX-License-Identifier: GPL-2.0
6a11f75b6   Akinobu Mita   generic debug pag...
2
  #include <linux/kernel.h>
8c5fb8ead   Akinobu Mita   mm/debug-pageallo...
3
  #include <linux/string.h>
6a11f75b6   Akinobu Mita   generic debug pag...
4
  #include <linux/mm.h>
64212ec56   Akinobu Mita   debug-pagealloc: ...
5
  #include <linux/highmem.h>
e30825f18   Joonsoo Kim   mm/debug-pageallo...
6
  #include <linux/page_ext.h>
6a11f75b6   Akinobu Mita   generic debug pag...
7
  #include <linux/poison.h>
77311139f   Akinobu Mita   mm/debug-pageallo...
8
  #include <linux/ratelimit.h>
4117992df   Qian Cai   page_poison: play...
9
  #include <linux/kasan.h>
6a11f75b6   Akinobu Mita   generic debug pag...
10

8823b1dbc   Laura Abbott   mm/page_poison.c:...
11
  static bool want_page_poisoning __read_mostly;
e30825f18   Joonsoo Kim   mm/debug-pageallo...
12

14298d366   Dou Liyang   mm/page_poison.c:...
13
  static int __init early_page_poison_param(char *buf)
e30825f18   Joonsoo Kim   mm/debug-pageallo...
14
  {
8823b1dbc   Laura Abbott   mm/page_poison.c:...
15
16
  	if (!buf)
  		return -EINVAL;
2a138dc7e   Minfei Huang   mm: use existing ...
17
  	return strtobool(buf, &want_page_poisoning);
8823b1dbc   Laura Abbott   mm/page_poison.c:...
18
19
  }
  early_param("page_poison", early_page_poison_param);
d95f58f4a   Wei Wang   mm/page_poison: e...
20
21
22
23
24
  /**
   * page_poisoning_enabled - check if page poisoning is enabled
   *
   * Return true if page poisoning is enabled, or false if not.
   */
8823b1dbc   Laura Abbott   mm/page_poison.c:...
25
26
  bool page_poisoning_enabled(void)
  {
8823b1dbc   Laura Abbott   mm/page_poison.c:...
27
  	/*
bd33ef368   Vinayak Menon   mm: enable page p...
28
  	 * Assumes that debug_pagealloc_enabled is set before
c6ffc5ca8   Mike Rapoport   memblock: rename ...
29
  	 * memblock_free_all.
bd33ef368   Vinayak Menon   mm: enable page p...
30
31
  	 * Page poisoning is debug page alloc for some arches. If
  	 * either of those options are enabled, enable poisoning.
8823b1dbc   Laura Abbott   mm/page_poison.c:...
32
  	 */
bd33ef368   Vinayak Menon   mm: enable page p...
33
34
35
  	return (want_page_poisoning ||
  		(!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
  		debug_pagealloc_enabled()));
6a11f75b6   Akinobu Mita   generic debug pag...
36
  }
d95f58f4a   Wei Wang   mm/page_poison: e...
37
  EXPORT_SYMBOL_GPL(page_poisoning_enabled);
6a11f75b6   Akinobu Mita   generic debug pag...
38

6a11f75b6   Akinobu Mita   generic debug pag...
39
40
  static void poison_page(struct page *page)
  {
64212ec56   Akinobu Mita   debug-pagealloc: ...
41
  	void *addr = kmap_atomic(page);
6a11f75b6   Akinobu Mita   generic debug pag...
42

4117992df   Qian Cai   page_poison: play...
43
44
  	/* KASAN still think the page is in-use, so skip it. */
  	kasan_disable_current();
6a11f75b6   Akinobu Mita   generic debug pag...
45
  	memset(addr, PAGE_POISON, PAGE_SIZE);
4117992df   Qian Cai   page_poison: play...
46
  	kasan_enable_current();
64212ec56   Akinobu Mita   debug-pagealloc: ...
47
  	kunmap_atomic(addr);
6a11f75b6   Akinobu Mita   generic debug pag...
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  }
  
  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)
  {
77311139f   Akinobu Mita   mm/debug-pageallo...
67
  	static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
6a11f75b6   Akinobu Mita   generic debug pag...
68
69
  	unsigned char *start;
  	unsigned char *end;
8823b1dbc   Laura Abbott   mm/page_poison.c:...
70
71
  	if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
  		return;
8c5fb8ead   Akinobu Mita   mm/debug-pageallo...
72
73
  	start = memchr_inv(mem, PAGE_POISON, bytes);
  	if (!start)
6a11f75b6   Akinobu Mita   generic debug pag...
74
75
76
77
78
79
  		return;
  
  	for (end = mem + bytes - 1; end > start; end--) {
  		if (*end != PAGE_POISON)
  			break;
  	}
77311139f   Akinobu Mita   mm/debug-pageallo...
80
  	if (!__ratelimit(&ratelimit))
6a11f75b6   Akinobu Mita   generic debug pag...
81
82
  		return;
  	else if (start == end && single_bit_flip(*start, PAGE_POISON))
8823b1dbc   Laura Abbott   mm/page_poison.c:...
83
84
  		pr_err("pagealloc: single bit error
  ");
6a11f75b6   Akinobu Mita   generic debug pag...
85
  	else
8823b1dbc   Laura Abbott   mm/page_poison.c:...
86
87
  		pr_err("pagealloc: memory corruption
  ");
6a11f75b6   Akinobu Mita   generic debug pag...
88
89
90
91
92
  
  	print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
  			end - start + 1, 1);
  	dump_stack();
  }
6a11f75b6   Akinobu Mita   generic debug pag...
93
94
  static void unpoison_page(struct page *page)
  {
64212ec56   Akinobu Mita   debug-pagealloc: ...
95
  	void *addr;
64212ec56   Akinobu Mita   debug-pagealloc: ...
96
  	addr = kmap_atomic(page);
bd33ef368   Vinayak Menon   mm: enable page p...
97
98
99
  	/*
  	 * Page poisoning when enabled poisons each and every page
  	 * that is freed to buddy. Thus no extra check is done to
dbf7684e2   Christophe JAILLET   mm/page_poison.c:...
100
  	 * see if a page was poisoned.
bd33ef368   Vinayak Menon   mm: enable page p...
101
  	 */
64212ec56   Akinobu Mita   debug-pagealloc: ...
102
  	check_poison_mem(addr, PAGE_SIZE);
64212ec56   Akinobu Mita   debug-pagealloc: ...
103
  	kunmap_atomic(addr);
6a11f75b6   Akinobu Mita   generic debug pag...
104
105
106
107
108
109
110
111
112
  }
  
  static void unpoison_pages(struct page *page, int n)
  {
  	int i;
  
  	for (i = 0; i < n; i++)
  		unpoison_page(page + i);
  }
8823b1dbc   Laura Abbott   mm/page_poison.c:...
113
  void kernel_poison_pages(struct page *page, int numpages, int enable)
6a11f75b6   Akinobu Mita   generic debug pag...
114
  {
8823b1dbc   Laura Abbott   mm/page_poison.c:...
115
  	if (!page_poisoning_enabled())
e30825f18   Joonsoo Kim   mm/debug-pageallo...
116
  		return;
6a11f75b6   Akinobu Mita   generic debug pag...
117
118
119
120
121
  	if (enable)
  		unpoison_pages(page, numpages);
  	else
  		poison_pages(page, numpages);
  }
8823b1dbc   Laura Abbott   mm/page_poison.c:...
122
123
124
125
126
127
128
  
  #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