Blame view

mm/page_poison.c 2.66 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>
6a11f75b6   Akinobu Mita   generic debug pag...
9

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

8823b1dbc   Laura Abbott   mm/page_poison.c:...
12
  static int early_page_poison_param(char *buf)
e30825f18   Joonsoo Kim   mm/debug-pageallo...
13
  {
8823b1dbc   Laura Abbott   mm/page_poison.c:...
14
15
  	if (!buf)
  		return -EINVAL;
2a138dc7e   Minfei Huang   mm: use existing ...
16
  	return strtobool(buf, &want_page_poisoning);
8823b1dbc   Laura Abbott   mm/page_poison.c:...
17
18
19
20
21
  }
  early_param("page_poison", early_page_poison_param);
  
  bool page_poisoning_enabled(void)
  {
8823b1dbc   Laura Abbott   mm/page_poison.c:...
22
  	/*
bd33ef368   Vinayak Menon   mm: enable page p...
23
24
25
26
  	 * Assumes that debug_pagealloc_enabled is set before
  	 * free_all_bootmem.
  	 * 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:...
27
  	 */
bd33ef368   Vinayak Menon   mm: enable page p...
28
29
30
  	return (want_page_poisoning ||
  		(!IS_ENABLED(CONFIG_ARCH_SUPPORTS_DEBUG_PAGEALLOC) &&
  		debug_pagealloc_enabled()));
6a11f75b6   Akinobu Mita   generic debug pag...
31
  }
6a11f75b6   Akinobu Mita   generic debug pag...
32
33
  static void poison_page(struct page *page)
  {
64212ec56   Akinobu Mita   debug-pagealloc: ...
34
  	void *addr = kmap_atomic(page);
6a11f75b6   Akinobu Mita   generic debug pag...
35

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