Blame view

mm/page_poison.c 3.51 KB
6a11f75b6   Akinobu Mita   generic debug pag...
1
  #include <linux/kernel.h>
8c5fb8ead   Akinobu Mita   mm/debug-pageallo...
2
  #include <linux/string.h>
6a11f75b6   Akinobu Mita   generic debug pag...
3
  #include <linux/mm.h>
64212ec56   Akinobu Mita   debug-pagealloc: ...
4
  #include <linux/highmem.h>
e30825f18   Joonsoo Kim   mm/debug-pageallo...
5
  #include <linux/page_ext.h>
6a11f75b6   Akinobu Mita   generic debug pag...
6
  #include <linux/poison.h>
77311139f   Akinobu Mita   mm/debug-pageallo...
7
  #include <linux/ratelimit.h>
6a11f75b6   Akinobu Mita   generic debug pag...
8

8823b1dbc   Laura Abbott   mm/page_poison.c:...
9
10
  static bool __page_poisoning_enabled __read_mostly;
  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
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;
e30825f18   Joonsoo Kim   mm/debug-pageallo...
28
29
30
31
  }
  
  static void init_page_poisoning(void)
  {
8823b1dbc   Laura Abbott   mm/page_poison.c:...
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;
  	}
031bc5743   Joonsoo Kim   mm/debug-pageallo...
43

8823b1dbc   Laura Abbott   mm/page_poison.c:...
44
  	__page_poisoning_enabled = true;
e30825f18   Joonsoo Kim   mm/debug-pageallo...
45
46
47
48
49
50
  }
  
  struct page_ext_operations page_poisoning_ops = {
  	.need = need_page_poisoning,
  	.init = init_page_poisoning,
  };
6a11f75b6   Akinobu Mita   generic debug pag...
51
52
  static inline void set_page_poison(struct page *page)
  {
e30825f18   Joonsoo Kim   mm/debug-pageallo...
53
54
55
  	struct page_ext *page_ext;
  
  	page_ext = lookup_page_ext(page);
f86e42719   Yang Shi   mm: check the ret...
56
57
  	if (unlikely(!page_ext))
  		return;
e30825f18   Joonsoo Kim   mm/debug-pageallo...
58
  	__set_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
6a11f75b6   Akinobu Mita   generic debug pag...
59
60
61
62
  }
  
  static inline void clear_page_poison(struct page *page)
  {
e30825f18   Joonsoo Kim   mm/debug-pageallo...
63
64
65
  	struct page_ext *page_ext;
  
  	page_ext = lookup_page_ext(page);
f86e42719   Yang Shi   mm: check the ret...
66
67
  	if (unlikely(!page_ext))
  		return;
e30825f18   Joonsoo Kim   mm/debug-pageallo...
68
  	__clear_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
6a11f75b6   Akinobu Mita   generic debug pag...
69
  }
1414c7f4f   Laura Abbott   mm/page_poisoning...
70
  bool page_is_poisoned(struct page *page)
6a11f75b6   Akinobu Mita   generic debug pag...
71
  {
e30825f18   Joonsoo Kim   mm/debug-pageallo...
72
73
74
  	struct page_ext *page_ext;
  
  	page_ext = lookup_page_ext(page);
f86e42719   Yang Shi   mm: check the ret...
75
  	if (unlikely(!page_ext))
1414c7f4f   Laura Abbott   mm/page_poisoning...
76
  		return false;
e30825f18   Joonsoo Kim   mm/debug-pageallo...
77
  	return test_bit(PAGE_EXT_DEBUG_POISON, &page_ext->flags);
6a11f75b6   Akinobu Mita   generic debug pag...
78
  }
6a11f75b6   Akinobu Mita   generic debug pag...
79
80
  static void poison_page(struct page *page)
  {
64212ec56   Akinobu Mita   debug-pagealloc: ...
81
  	void *addr = kmap_atomic(page);
6a11f75b6   Akinobu Mita   generic debug pag...
82

6a11f75b6   Akinobu Mita   generic debug pag...
83
  	set_page_poison(page);
6a11f75b6   Akinobu Mita   generic debug pag...
84
  	memset(addr, PAGE_POISON, PAGE_SIZE);
64212ec56   Akinobu Mita   debug-pagealloc: ...
85
  	kunmap_atomic(addr);
6a11f75b6   Akinobu Mita   generic debug pag...
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  }
  
  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...
105
  	static DEFINE_RATELIMIT_STATE(ratelimit, 5 * HZ, 10);
6a11f75b6   Akinobu Mita   generic debug pag...
106
107
  	unsigned char *start;
  	unsigned char *end;
8823b1dbc   Laura Abbott   mm/page_poison.c:...
108
109
  	if (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY))
  		return;
8c5fb8ead   Akinobu Mita   mm/debug-pageallo...
110
111
  	start = memchr_inv(mem, PAGE_POISON, bytes);
  	if (!start)
6a11f75b6   Akinobu Mita   generic debug pag...
112
113
114
115
116
117
  		return;
  
  	for (end = mem + bytes - 1; end > start; end--) {
  		if (*end != PAGE_POISON)
  			break;
  	}
77311139f   Akinobu Mita   mm/debug-pageallo...
118
  	if (!__ratelimit(&ratelimit))
6a11f75b6   Akinobu Mita   generic debug pag...
119
120
  		return;
  	else if (start == end && single_bit_flip(*start, PAGE_POISON))
8823b1dbc   Laura Abbott   mm/page_poison.c:...
121
122
  		pr_err("pagealloc: single bit error
  ");
6a11f75b6   Akinobu Mita   generic debug pag...
123
  	else
8823b1dbc   Laura Abbott   mm/page_poison.c:...
124
125
  		pr_err("pagealloc: memory corruption
  ");
6a11f75b6   Akinobu Mita   generic debug pag...
126
127
128
129
130
  
  	print_hex_dump(KERN_ERR, "", DUMP_PREFIX_ADDRESS, 16, 1, start,
  			end - start + 1, 1);
  	dump_stack();
  }
6a11f75b6   Akinobu Mita   generic debug pag...
131
132
  static void unpoison_page(struct page *page)
  {
64212ec56   Akinobu Mita   debug-pagealloc: ...
133
  	void *addr;
1414c7f4f   Laura Abbott   mm/page_poisoning...
134
  	if (!page_is_poisoned(page))
6a11f75b6   Akinobu Mita   generic debug pag...
135
  		return;
6a11f75b6   Akinobu Mita   generic debug pag...
136

64212ec56   Akinobu Mita   debug-pagealloc: ...
137
138
139
140
  	addr = kmap_atomic(page);
  	check_poison_mem(addr, PAGE_SIZE);
  	clear_page_poison(page);
  	kunmap_atomic(addr);
6a11f75b6   Akinobu Mita   generic debug pag...
141
142
143
144
145
146
147
148
149
  }
  
  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:...
150
  void kernel_poison_pages(struct page *page, int numpages, int enable)
6a11f75b6   Akinobu Mita   generic debug pag...
151
  {
8823b1dbc   Laura Abbott   mm/page_poison.c:...
152
  	if (!page_poisoning_enabled())
e30825f18   Joonsoo Kim   mm/debug-pageallo...
153
  		return;
6a11f75b6   Akinobu Mita   generic debug pag...
154
155
156
157
158
  	if (enable)
  		unpoison_pages(page, numpages);
  	else
  		poison_pages(page, numpages);
  }
8823b1dbc   Laura Abbott   mm/page_poison.c:...
159
160
161
162
163
164
165
  
  #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