Commit | Line | Data |
---|---|---|
eefa864b JK |
1 | #include <linux/mm.h> |
2 | #include <linux/mmzone.h> | |
3 | #include <linux/bootmem.h> | |
4 | #include <linux/page_ext.h> | |
5 | #include <linux/memory.h> | |
6 | #include <linux/vmalloc.h> | |
7 | #include <linux/kmemleak.h> | |
8 | ||
9 | /* | |
10 | * struct page extension | |
11 | * | |
12 | * This is the feature to manage memory for extended data per page. | |
13 | * | |
14 | * Until now, we must modify struct page itself to store extra data per page. | |
15 | * This requires rebuilding the kernel and it is really time consuming process. | |
16 | * And, sometimes, rebuild is impossible due to third party module dependency. | |
17 | * At last, enlarging struct page could cause un-wanted system behaviour change. | |
18 | * | |
19 | * This feature is intended to overcome above mentioned problems. This feature | |
20 | * allocates memory for extended data per page in certain place rather than | |
21 | * the struct page itself. This memory can be accessed by the accessor | |
22 | * functions provided by this code. During the boot process, it checks whether | |
23 | * allocation of huge chunk of memory is needed or not. If not, it avoids | |
24 | * allocating memory at all. With this advantage, we can include this feature | |
25 | * into the kernel in default and can avoid rebuild and solve related problems. | |
26 | * | |
27 | * To help these things to work well, there are two callbacks for clients. One | |
28 | * is the need callback which is mandatory if user wants to avoid useless | |
29 | * memory allocation at boot-time. The other is optional, init callback, which | |
30 | * is used to do proper initialization after memory is allocated. | |
31 | * | |
32 | * The need callback is used to decide whether extended memory allocation is | |
33 | * needed or not. Sometimes users want to deactivate some features in this | |
34 | * boot and extra memory would be unneccessary. In this case, to avoid | |
35 | * allocating huge chunk of memory, each clients represent their need of | |
36 | * extra memory through the need callback. If one of the need callbacks | |
37 | * returns true, it means that someone needs extra memory so that | |
38 | * page extension core should allocates memory for page extension. If | |
39 | * none of need callbacks return true, memory isn't needed at all in this boot | |
40 | * and page extension core can skip to allocate memory. As result, | |
41 | * none of memory is wasted. | |
42 | * | |
43 | * The init callback is used to do proper initialization after page extension | |
44 | * is completely initialized. In sparse memory system, extra memory is | |
45 | * allocated some time later than memmap is allocated. In other words, lifetime | |
46 | * of memory for page extension isn't same with memmap for struct page. | |
47 | * Therefore, clients can't store extra data until page extension is | |
48 | * initialized, even if pages are allocated and used freely. This could | |
49 | * cause inadequate state of extra data per page, so, to prevent it, client | |
50 | * can utilize this callback to initialize the state of it correctly. | |
51 | */ | |
52 | ||
53 | static struct page_ext_operations *page_ext_ops[] = { | |
e30825f1 JK |
54 | &debug_guardpage_ops, |
55 | #ifdef CONFIG_PAGE_POISONING | |
56 | &page_poisoning_ops, | |
57 | #endif | |
eefa864b JK |
58 | }; |
59 | ||
60 | static unsigned long total_usage; | |
61 | ||
62 | static bool __init invoke_need_callbacks(void) | |
63 | { | |
64 | int i; | |
65 | int entries = ARRAY_SIZE(page_ext_ops); | |
66 | ||
67 | for (i = 0; i < entries; i++) { | |
68 | if (page_ext_ops[i]->need && page_ext_ops[i]->need()) | |
69 | return true; | |
70 | } | |
71 | ||
72 | return false; | |
73 | } | |
74 | ||
75 | static void __init invoke_init_callbacks(void) | |
76 | { | |
77 | int i; | |
78 | int entries = ARRAY_SIZE(page_ext_ops); | |
79 | ||
80 | for (i = 0; i < entries; i++) { | |
81 | if (page_ext_ops[i]->init) | |
82 | page_ext_ops[i]->init(); | |
83 | } | |
84 | } | |
85 | ||
86 | #if !defined(CONFIG_SPARSEMEM) | |
87 | ||
88 | ||
89 | void __meminit pgdat_page_ext_init(struct pglist_data *pgdat) | |
90 | { | |
91 | pgdat->node_page_ext = NULL; | |
92 | } | |
93 | ||
94 | struct page_ext *lookup_page_ext(struct page *page) | |
95 | { | |
96 | unsigned long pfn = page_to_pfn(page); | |
97 | unsigned long offset; | |
98 | struct page_ext *base; | |
99 | ||
100 | base = NODE_DATA(page_to_nid(page))->node_page_ext; | |
101 | #ifdef CONFIG_DEBUG_VM | |
102 | /* | |
103 | * The sanity checks the page allocator does upon freeing a | |
104 | * page can reach here before the page_ext arrays are | |
105 | * allocated when feeding a range of pages to the allocator | |
106 | * for the first time during bootup or memory hotplug. | |
107 | */ | |
108 | if (unlikely(!base)) | |
109 | return NULL; | |
110 | #endif | |
111 | offset = pfn - round_down(node_start_pfn(page_to_nid(page)), | |
112 | MAX_ORDER_NR_PAGES); | |
113 | return base + offset; | |
114 | } | |
115 | ||
116 | static int __init alloc_node_page_ext(int nid) | |
117 | { | |
118 | struct page_ext *base; | |
119 | unsigned long table_size; | |
120 | unsigned long nr_pages; | |
121 | ||
122 | nr_pages = NODE_DATA(nid)->node_spanned_pages; | |
123 | if (!nr_pages) | |
124 | return 0; | |
125 | ||
126 | /* | |
127 | * Need extra space if node range is not aligned with | |
128 | * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm | |
129 | * checks buddy's status, range could be out of exact node range. | |
130 | */ | |
131 | if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) || | |
132 | !IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES)) | |
133 | nr_pages += MAX_ORDER_NR_PAGES; | |
134 | ||
135 | table_size = sizeof(struct page_ext) * nr_pages; | |
136 | ||
137 | base = memblock_virt_alloc_try_nid_nopanic( | |
138 | table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS), | |
139 | BOOTMEM_ALLOC_ACCESSIBLE, nid); | |
140 | if (!base) | |
141 | return -ENOMEM; | |
142 | NODE_DATA(nid)->node_page_ext = base; | |
143 | total_usage += table_size; | |
144 | return 0; | |
145 | } | |
146 | ||
147 | void __init page_ext_init_flatmem(void) | |
148 | { | |
149 | ||
150 | int nid, fail; | |
151 | ||
152 | if (!invoke_need_callbacks()) | |
153 | return; | |
154 | ||
155 | for_each_online_node(nid) { | |
156 | fail = alloc_node_page_ext(nid); | |
157 | if (fail) | |
158 | goto fail; | |
159 | } | |
160 | pr_info("allocated %ld bytes of page_ext\n", total_usage); | |
161 | invoke_init_callbacks(); | |
162 | return; | |
163 | ||
164 | fail: | |
165 | pr_crit("allocation of page_ext failed.\n"); | |
166 | panic("Out of memory"); | |
167 | } | |
168 | ||
169 | #else /* CONFIG_FLAT_NODE_MEM_MAP */ | |
170 | ||
171 | struct page_ext *lookup_page_ext(struct page *page) | |
172 | { | |
173 | unsigned long pfn = page_to_pfn(page); | |
174 | struct mem_section *section = __pfn_to_section(pfn); | |
175 | #ifdef CONFIG_DEBUG_VM | |
176 | /* | |
177 | * The sanity checks the page allocator does upon freeing a | |
178 | * page can reach here before the page_ext arrays are | |
179 | * allocated when feeding a range of pages to the allocator | |
180 | * for the first time during bootup or memory hotplug. | |
181 | */ | |
182 | if (!section->page_ext) | |
183 | return NULL; | |
184 | #endif | |
185 | return section->page_ext + pfn; | |
186 | } | |
187 | ||
188 | static void *__meminit alloc_page_ext(size_t size, int nid) | |
189 | { | |
190 | gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN; | |
191 | void *addr = NULL; | |
192 | ||
193 | addr = alloc_pages_exact_nid(nid, size, flags); | |
194 | if (addr) { | |
195 | kmemleak_alloc(addr, size, 1, flags); | |
196 | return addr; | |
197 | } | |
198 | ||
199 | if (node_state(nid, N_HIGH_MEMORY)) | |
200 | addr = vzalloc_node(size, nid); | |
201 | else | |
202 | addr = vzalloc(size); | |
203 | ||
204 | return addr; | |
205 | } | |
206 | ||
207 | static int __meminit init_section_page_ext(unsigned long pfn, int nid) | |
208 | { | |
209 | struct mem_section *section; | |
210 | struct page_ext *base; | |
211 | unsigned long table_size; | |
212 | ||
213 | section = __pfn_to_section(pfn); | |
214 | ||
215 | if (section->page_ext) | |
216 | return 0; | |
217 | ||
218 | table_size = sizeof(struct page_ext) * PAGES_PER_SECTION; | |
219 | base = alloc_page_ext(table_size, nid); | |
220 | ||
221 | /* | |
222 | * The value stored in section->page_ext is (base - pfn) | |
223 | * and it does not point to the memory block allocated above, | |
224 | * causing kmemleak false positives. | |
225 | */ | |
226 | kmemleak_not_leak(base); | |
227 | ||
228 | if (!base) { | |
229 | pr_err("page ext allocation failure\n"); | |
230 | return -ENOMEM; | |
231 | } | |
232 | ||
233 | /* | |
234 | * The passed "pfn" may not be aligned to SECTION. For the calculation | |
235 | * we need to apply a mask. | |
236 | */ | |
237 | pfn &= PAGE_SECTION_MASK; | |
238 | section->page_ext = base - pfn; | |
239 | total_usage += table_size; | |
240 | return 0; | |
241 | } | |
242 | #ifdef CONFIG_MEMORY_HOTPLUG | |
243 | static void free_page_ext(void *addr) | |
244 | { | |
245 | if (is_vmalloc_addr(addr)) { | |
246 | vfree(addr); | |
247 | } else { | |
248 | struct page *page = virt_to_page(addr); | |
249 | size_t table_size; | |
250 | ||
251 | table_size = sizeof(struct page_ext) * PAGES_PER_SECTION; | |
252 | ||
253 | BUG_ON(PageReserved(page)); | |
254 | free_pages_exact(addr, table_size); | |
255 | } | |
256 | } | |
257 | ||
258 | static void __free_page_ext(unsigned long pfn) | |
259 | { | |
260 | struct mem_section *ms; | |
261 | struct page_ext *base; | |
262 | ||
263 | ms = __pfn_to_section(pfn); | |
264 | if (!ms || !ms->page_ext) | |
265 | return; | |
266 | base = ms->page_ext + pfn; | |
267 | free_page_ext(base); | |
268 | ms->page_ext = NULL; | |
269 | } | |
270 | ||
271 | static int __meminit online_page_ext(unsigned long start_pfn, | |
272 | unsigned long nr_pages, | |
273 | int nid) | |
274 | { | |
275 | unsigned long start, end, pfn; | |
276 | int fail = 0; | |
277 | ||
278 | start = SECTION_ALIGN_DOWN(start_pfn); | |
279 | end = SECTION_ALIGN_UP(start_pfn + nr_pages); | |
280 | ||
281 | if (nid == -1) { | |
282 | /* | |
283 | * In this case, "nid" already exists and contains valid memory. | |
284 | * "start_pfn" passed to us is a pfn which is an arg for | |
285 | * online__pages(), and start_pfn should exist. | |
286 | */ | |
287 | nid = pfn_to_nid(start_pfn); | |
288 | VM_BUG_ON(!node_state(nid, N_ONLINE)); | |
289 | } | |
290 | ||
291 | for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION) { | |
292 | if (!pfn_present(pfn)) | |
293 | continue; | |
294 | fail = init_section_page_ext(pfn, nid); | |
295 | } | |
296 | if (!fail) | |
297 | return 0; | |
298 | ||
299 | /* rollback */ | |
300 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) | |
301 | __free_page_ext(pfn); | |
302 | ||
303 | return -ENOMEM; | |
304 | } | |
305 | ||
306 | static int __meminit offline_page_ext(unsigned long start_pfn, | |
307 | unsigned long nr_pages, int nid) | |
308 | { | |
309 | unsigned long start, end, pfn; | |
310 | ||
311 | start = SECTION_ALIGN_DOWN(start_pfn); | |
312 | end = SECTION_ALIGN_UP(start_pfn + nr_pages); | |
313 | ||
314 | for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION) | |
315 | __free_page_ext(pfn); | |
316 | return 0; | |
317 | ||
318 | } | |
319 | ||
320 | static int __meminit page_ext_callback(struct notifier_block *self, | |
321 | unsigned long action, void *arg) | |
322 | { | |
323 | struct memory_notify *mn = arg; | |
324 | int ret = 0; | |
325 | ||
326 | switch (action) { | |
327 | case MEM_GOING_ONLINE: | |
328 | ret = online_page_ext(mn->start_pfn, | |
329 | mn->nr_pages, mn->status_change_nid); | |
330 | break; | |
331 | case MEM_OFFLINE: | |
332 | offline_page_ext(mn->start_pfn, | |
333 | mn->nr_pages, mn->status_change_nid); | |
334 | break; | |
335 | case MEM_CANCEL_ONLINE: | |
336 | offline_page_ext(mn->start_pfn, | |
337 | mn->nr_pages, mn->status_change_nid); | |
338 | break; | |
339 | case MEM_GOING_OFFLINE: | |
340 | break; | |
341 | case MEM_ONLINE: | |
342 | case MEM_CANCEL_OFFLINE: | |
343 | break; | |
344 | } | |
345 | ||
346 | return notifier_from_errno(ret); | |
347 | } | |
348 | ||
349 | #endif | |
350 | ||
351 | void __init page_ext_init(void) | |
352 | { | |
353 | unsigned long pfn; | |
354 | int nid; | |
355 | ||
356 | if (!invoke_need_callbacks()) | |
357 | return; | |
358 | ||
359 | for_each_node_state(nid, N_MEMORY) { | |
360 | unsigned long start_pfn, end_pfn; | |
361 | ||
362 | start_pfn = node_start_pfn(nid); | |
363 | end_pfn = node_end_pfn(nid); | |
364 | /* | |
365 | * start_pfn and end_pfn may not be aligned to SECTION and the | |
366 | * page->flags of out of node pages are not initialized. So we | |
367 | * scan [start_pfn, the biggest section's pfn < end_pfn) here. | |
368 | */ | |
369 | for (pfn = start_pfn; pfn < end_pfn; | |
370 | pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) { | |
371 | ||
372 | if (!pfn_valid(pfn)) | |
373 | continue; | |
374 | /* | |
375 | * Nodes's pfns can be overlapping. | |
376 | * We know some arch can have a nodes layout such as | |
377 | * -------------pfn--------------> | |
378 | * N0 | N1 | N2 | N0 | N1 | N2|.... | |
379 | */ | |
380 | if (pfn_to_nid(pfn) != nid) | |
381 | continue; | |
382 | if (init_section_page_ext(pfn, nid)) | |
383 | goto oom; | |
384 | } | |
385 | } | |
386 | hotplug_memory_notifier(page_ext_callback, 0); | |
387 | pr_info("allocated %ld bytes of page_ext\n", total_usage); | |
388 | invoke_init_callbacks(); | |
389 | return; | |
390 | ||
391 | oom: | |
392 | panic("Out of memory"); | |
393 | } | |
394 | ||
395 | void __meminit pgdat_page_ext_init(struct pglist_data *pgdat) | |
396 | { | |
397 | } | |
398 | ||
399 | #endif |