Blame view

mm/page_poison.c 3.52 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
16
17
18
19
20
  	if (!buf)
  		return -EINVAL;
  
  	if (strcmp(buf, "on") == 0)
  		want_page_poisoning = true;
  	else if (strcmp(buf, "off") == 0)
  		want_page_poisoning = false;
031bc5743   Joonsoo Kim   mm/debug-pageallo...
21

8823b1dbc   Laura Abbott   mm/page_poison.c:...
22
23
24
25
26
27
28
29
30
31
32
33
  	return 0;
  }
  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...
34
35
36
37
  }
  
  static void init_page_poisoning(void)
  {
8823b1dbc   Laura Abbott   mm/page_poison.c:...
38
39
40
41
42
43
44
45
46
47
48
  	/*
  	 * 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...
49

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

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

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