Blame view

mm/page_poison.c 3.13 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

11c9c7eda   Mateusz Nosek   mm/page_poison.c:...
11
  static DEFINE_STATIC_KEY_FALSE_RO(want_page_poisoning);
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
  {
11c9c7eda   Mateusz Nosek   mm/page_poison.c:...
15
16
17
18
19
20
21
22
23
24
25
26
27
  	int ret;
  	bool tmp;
  
  	ret = strtobool(buf, &tmp);
  	if (ret)
  		return ret;
  
  	if (tmp)
  		static_branch_enable(&want_page_poisoning);
  	else
  		static_branch_disable(&want_page_poisoning);
  
  	return 0;
8823b1dbc   Laura Abbott   mm/page_poison.c:...
28
29
  }
  early_param("page_poison", early_page_poison_param);
d95f58f4a   Wei Wang   mm/page_poison: e...
30
31
32
33
34
  /**
   * 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:...
35
36
  bool page_poisoning_enabled(void)
  {
8823b1dbc   Laura Abbott   mm/page_poison.c:...
37
  	/*
bd33ef368   Vinayak Menon   mm: enable page p...
38
  	 * Assumes that debug_pagealloc_enabled is set before
c6ffc5ca8   Mike Rapoport   memblock: rename ...
39
  	 * memblock_free_all.
bd33ef368   Vinayak Menon   mm: enable page p...
40
41
  	 * 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:...
42
  	 */
11c9c7eda   Mateusz Nosek   mm/page_poison.c:...
43
  	return (static_branch_unlikely(&want_page_poisoning) ||
bd33ef368   Vinayak Menon   mm: enable page p...
44
45
  		(!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
  		debug_pagealloc_enabled()));
6a11f75b6   Akinobu Mita   generic debug pag...
46
  }
d95f58f4a   Wei Wang   mm/page_poison: e...
47
  EXPORT_SYMBOL_GPL(page_poisoning_enabled);
6a11f75b6   Akinobu Mita   generic debug pag...
48

6a11f75b6   Akinobu Mita   generic debug pag...
49
50
  static void poison_page(struct page *page)
  {
64212ec56   Akinobu Mita   debug-pagealloc: ...
51
  	void *addr = kmap_atomic(page);
6a11f75b6   Akinobu Mita   generic debug pag...
52

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