1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/mm.h>
3 #include <linux/mmzone.h>
4 #include <linux/memblock.h>
5 #include <linux/page_ext.h>
6 #include <linux/memory.h>
7 #include <linux/vmalloc.h>
8 #include <linux/kmemleak.h>
9 #include <linux/page_owner.h>
10 #include <linux/page_idle.h>
11 #include <linux/rcupdate.h>
12 /*
13 * struct page extension
14 *
15 * This is the feature to manage memory for extended data per page.
16 *
17 * Until now, we must modify struct page itself to store extra data per page.
18 * This requires rebuilding the kernel and it is really time consuming process.
19 * And, sometimes, rebuild is impossible due to third party module dependency.
20 * At last, enlarging struct page could cause un-wanted system behaviour change.
21 *
22 * This feature is intended to overcome above mentioned problems. This feature
23 * allocates memory for extended data per page in certain place rather than
24 * the struct page itself. This memory can be accessed by the accessor
25 * functions provided by this code. During the boot process, it checks whether
26 * allocation of huge chunk of memory is needed or not. If not, it avoids
27 * allocating memory at all. With this advantage, we can include this feature
28 * into the kernel in default and can avoid rebuild and solve related problems.
29 *
30 * To help these things to work well, there are two callbacks for clients. One
31 * is the need callback which is mandatory if user wants to avoid useless
32 * memory allocation at boot-time. The other is optional, init callback, which
33 * is used to do proper initialization after memory is allocated.
34 *
35 * The need callback is used to decide whether extended memory allocation is
36 * needed or not. Sometimes users want to deactivate some features in this
37 * boot and extra memory would be unneccessary. In this case, to avoid
38 * allocating huge chunk of memory, each clients represent their need of
39 * extra memory through the need callback. If one of the need callbacks
40 * returns true, it means that someone needs extra memory so that
41 * page extension core should allocates memory for page extension. If
42 * none of need callbacks return true, memory isn't needed at all in this boot
43 * and page extension core can skip to allocate memory. As result,
44 * none of memory is wasted.
45 *
46 * When need callback returns true, page_ext checks if there is a request for
47 * extra memory through size in struct page_ext_operations. If it is non-zero,
48 * extra space is allocated for each page_ext entry and offset is returned to
49 * user through offset in struct page_ext_operations.
50 *
51 * The init callback is used to do proper initialization after page extension
52 * is completely initialized. In sparse memory system, extra memory is
53 * allocated some time later than memmap is allocated. In other words, lifetime
54 * of memory for page extension isn't same with memmap for struct page.
55 * Therefore, clients can't store extra data until page extension is
56 * initialized, even if pages are allocated and used freely. This could
57 * cause inadequate state of extra data per page, so, to prevent it, client
58 * can utilize this callback to initialize the state of it correctly.
59 */
60
61 #ifdef CONFIG_SPARSEMEM
62 #define PAGE_EXT_INVALID (0x1)
63 #endif
64
65 #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
need_page_idle(void)66 static bool need_page_idle(void)
67 {
68 return true;
69 }
70 struct page_ext_operations page_idle_ops = {
71 .need = need_page_idle,
72 };
73 #endif
74
75 static struct page_ext_operations *page_ext_ops[] = {
76 #ifdef CONFIG_PAGE_OWNER
77 &page_owner_ops,
78 #endif
79 #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
80 &page_idle_ops,
81 #endif
82 #ifdef CONFIG_PAGE_PINNER
83 &page_pinner_ops,
84 #endif
85 };
86
87 unsigned long page_ext_size = sizeof(struct page_ext);
88
89 static unsigned long total_usage;
90
invoke_need_callbacks(void)91 static bool __init invoke_need_callbacks(void)
92 {
93 int i;
94 int entries = ARRAY_SIZE(page_ext_ops);
95 bool need = false;
96
97 for (i = 0; i < entries; i++) {
98 if (page_ext_ops[i]->need && page_ext_ops[i]->need()) {
99 page_ext_ops[i]->offset = page_ext_size;
100 page_ext_size += page_ext_ops[i]->size;
101 need = true;
102 }
103 }
104
105 return need;
106 }
107
invoke_init_callbacks(void)108 static void __init invoke_init_callbacks(void)
109 {
110 int i;
111 int entries = ARRAY_SIZE(page_ext_ops);
112
113 for (i = 0; i < entries; i++) {
114 if (page_ext_ops[i]->init)
115 page_ext_ops[i]->init();
116 }
117 }
118
119 #ifndef CONFIG_SPARSEMEM
page_ext_init_flatmem_late(void)120 void __init page_ext_init_flatmem_late(void)
121 {
122 invoke_init_callbacks();
123 }
124 #endif
125
get_entry(void * base,unsigned long index)126 static inline struct page_ext *get_entry(void *base, unsigned long index)
127 {
128 return base + page_ext_size * index;
129 }
130
131 /**
132 * page_ext_get() - Get the extended information for a page.
133 * @page: The page we're interested in.
134 *
135 * Ensures that the page_ext will remain valid until page_ext_put()
136 * is called.
137 *
138 * Return: NULL if no page_ext exists for this page.
139 * Context: Any context. Caller may not sleep until they have called
140 * page_ext_put().
141 */
page_ext_get(struct page * page)142 struct page_ext *page_ext_get(struct page *page)
143 {
144 struct page_ext *page_ext;
145
146 rcu_read_lock();
147 page_ext = lookup_page_ext(page);
148 if (!page_ext) {
149 rcu_read_unlock();
150 return NULL;
151 }
152
153 return page_ext;
154 }
155
156 /**
157 * page_ext_put() - Working with page extended information is done.
158 * @page_ext - Page extended information received from page_ext_get().
159 *
160 * The page extended information of the page may not be valid after this
161 * function is called.
162 *
163 * Return: None.
164 * Context: Any context with corresponding page_ext_get() is called.
165 */
page_ext_put(struct page_ext * page_ext)166 void page_ext_put(struct page_ext *page_ext)
167 {
168 if (unlikely(!page_ext))
169 return;
170
171 rcu_read_unlock();
172 }
173 #ifndef CONFIG_SPARSEMEM
174
175
pgdat_page_ext_init(struct pglist_data * pgdat)176 void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
177 {
178 pgdat->node_page_ext = NULL;
179 }
180
lookup_page_ext(const struct page * page)181 struct page_ext *lookup_page_ext(const struct page *page)
182 {
183 unsigned long pfn = page_to_pfn(page);
184 unsigned long index;
185 struct page_ext *base;
186
187 WARN_ON_ONCE(!rcu_read_lock_held());
188 base = NODE_DATA(page_to_nid(page))->node_page_ext;
189 /*
190 * The sanity checks the page allocator does upon freeing a
191 * page can reach here before the page_ext arrays are
192 * allocated when feeding a range of pages to the allocator
193 * for the first time during bootup or memory hotplug.
194 */
195 if (unlikely(!base))
196 return NULL;
197 index = pfn - round_down(node_start_pfn(page_to_nid(page)),
198 MAX_ORDER_NR_PAGES);
199 return get_entry(base, index);
200 }
201 EXPORT_SYMBOL_GPL(lookup_page_ext);
202
alloc_node_page_ext(int nid)203 static int __init alloc_node_page_ext(int nid)
204 {
205 struct page_ext *base;
206 unsigned long table_size;
207 unsigned long nr_pages;
208
209 nr_pages = NODE_DATA(nid)->node_spanned_pages;
210 if (!nr_pages)
211 return 0;
212
213 /*
214 * Need extra space if node range is not aligned with
215 * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm
216 * checks buddy's status, range could be out of exact node range.
217 */
218 if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) ||
219 !IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES))
220 nr_pages += MAX_ORDER_NR_PAGES;
221
222 table_size = page_ext_size * nr_pages;
223
224 base = memblock_alloc_try_nid(
225 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
226 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
227 if (!base)
228 return -ENOMEM;
229 NODE_DATA(nid)->node_page_ext = base;
230 total_usage += table_size;
231 return 0;
232 }
233
page_ext_init_flatmem(void)234 void __init page_ext_init_flatmem(void)
235 {
236
237 int nid, fail;
238
239 if (!invoke_need_callbacks())
240 return;
241
242 for_each_online_node(nid) {
243 fail = alloc_node_page_ext(nid);
244 if (fail)
245 goto fail;
246 }
247 pr_info("allocated %ld bytes of page_ext\n", total_usage);
248 return;
249
250 fail:
251 pr_crit("allocation of page_ext failed.\n");
252 panic("Out of memory");
253 }
254
255 #else /* CONFIG_FLAT_NODE_MEM_MAP */
page_ext_invalid(struct page_ext * page_ext)256 static bool page_ext_invalid(struct page_ext *page_ext)
257 {
258 return !page_ext || (((unsigned long)page_ext & PAGE_EXT_INVALID) == PAGE_EXT_INVALID);
259 }
260
lookup_page_ext(const struct page * page)261 struct page_ext *lookup_page_ext(const struct page *page)
262 {
263 unsigned long pfn = page_to_pfn(page);
264 struct mem_section *section = __pfn_to_section(pfn);
265 struct page_ext *page_ext = READ_ONCE(section->page_ext);
266
267 WARN_ON_ONCE(!rcu_read_lock_held());
268 /*
269 * The sanity checks the page allocator does upon freeing a
270 * page can reach here before the page_ext arrays are
271 * allocated when feeding a range of pages to the allocator
272 * for the first time during bootup or memory hotplug.
273 */
274 if (page_ext_invalid(page_ext))
275 return NULL;
276 return get_entry(page_ext, pfn);
277 }
278 EXPORT_SYMBOL_GPL(lookup_page_ext);
279
alloc_page_ext(size_t size,int nid)280 static void *__meminit alloc_page_ext(size_t size, int nid)
281 {
282 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
283 void *addr = NULL;
284
285 addr = alloc_pages_exact_nid(nid, size, flags);
286 if (addr) {
287 kmemleak_alloc(addr, size, 1, flags);
288 return addr;
289 }
290
291 addr = vzalloc_node(size, nid);
292
293 return addr;
294 }
295
init_section_page_ext(unsigned long pfn,int nid)296 static int __meminit init_section_page_ext(unsigned long pfn, int nid)
297 {
298 struct mem_section *section;
299 struct page_ext *base;
300 unsigned long table_size;
301
302 section = __pfn_to_section(pfn);
303
304 if (section->page_ext)
305 return 0;
306
307 table_size = page_ext_size * PAGES_PER_SECTION;
308 base = alloc_page_ext(table_size, nid);
309
310 /*
311 * The value stored in section->page_ext is (base - pfn)
312 * and it does not point to the memory block allocated above,
313 * causing kmemleak false positives.
314 */
315 kmemleak_not_leak(base);
316
317 if (!base) {
318 pr_err("page ext allocation failure\n");
319 return -ENOMEM;
320 }
321
322 /*
323 * The passed "pfn" may not be aligned to SECTION. For the calculation
324 * we need to apply a mask.
325 */
326 pfn &= PAGE_SECTION_MASK;
327 section->page_ext = (void *)base - page_ext_size * pfn;
328 total_usage += table_size;
329 return 0;
330 }
331 #ifdef CONFIG_MEMORY_HOTPLUG
free_page_ext(void * addr)332 static void free_page_ext(void *addr)
333 {
334 if (is_vmalloc_addr(addr)) {
335 vfree(addr);
336 } else {
337 struct page *page = virt_to_page(addr);
338 size_t table_size;
339
340 table_size = page_ext_size * PAGES_PER_SECTION;
341
342 BUG_ON(PageReserved(page));
343 kmemleak_free(addr);
344 free_pages_exact(addr, table_size);
345 }
346 }
347
__free_page_ext(unsigned long pfn)348 static void __free_page_ext(unsigned long pfn)
349 {
350 struct mem_section *ms;
351 struct page_ext *base;
352
353 ms = __pfn_to_section(pfn);
354 if (!ms || !ms->page_ext)
355 return;
356
357 base = READ_ONCE(ms->page_ext);
358 /*
359 * page_ext here can be valid while doing the roll back
360 * operation in online_page_ext().
361 */
362 if (page_ext_invalid(base))
363 base = (void *)base - PAGE_EXT_INVALID;
364 WRITE_ONCE(ms->page_ext, NULL);
365
366 base = get_entry(base, pfn);
367 free_page_ext(base);
368 }
369
__invalidate_page_ext(unsigned long pfn)370 static void __invalidate_page_ext(unsigned long pfn)
371 {
372 struct mem_section *ms;
373 void *val;
374
375 ms = __pfn_to_section(pfn);
376 if (!ms || !ms->page_ext)
377 return;
378 val = (void *)ms->page_ext + PAGE_EXT_INVALID;
379 WRITE_ONCE(ms->page_ext, val);
380 }
381
online_page_ext(unsigned long start_pfn,unsigned long nr_pages,int nid)382 static int __meminit online_page_ext(unsigned long start_pfn,
383 unsigned long nr_pages,
384 int nid)
385 {
386 unsigned long start, end, pfn;
387 int fail = 0;
388
389 start = SECTION_ALIGN_DOWN(start_pfn);
390 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
391
392 if (nid == NUMA_NO_NODE) {
393 /*
394 * In this case, "nid" already exists and contains valid memory.
395 * "start_pfn" passed to us is a pfn which is an arg for
396 * online__pages(), and start_pfn should exist.
397 */
398 nid = pfn_to_nid(start_pfn);
399 VM_BUG_ON(!node_state(nid, N_ONLINE));
400 }
401
402 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION)
403 fail = init_section_page_ext(pfn, nid);
404 if (!fail)
405 return 0;
406
407 /* rollback */
408 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
409 __free_page_ext(pfn);
410
411 return -ENOMEM;
412 }
413
offline_page_ext(unsigned long start_pfn,unsigned long nr_pages,int nid)414 static int __meminit offline_page_ext(unsigned long start_pfn,
415 unsigned long nr_pages, int nid)
416 {
417 unsigned long start, end, pfn;
418
419 start = SECTION_ALIGN_DOWN(start_pfn);
420 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
421
422 /*
423 * Freeing of page_ext is done in 3 steps to avoid
424 * use-after-free of it:
425 * 1) Traverse all the sections and mark their page_ext
426 * as invalid.
427 * 2) Wait for all the existing users of page_ext who
428 * started before invalidation to finish.
429 * 3) Free the page_ext.
430 */
431 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
432 __invalidate_page_ext(pfn);
433
434 synchronize_rcu();
435
436 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
437 __free_page_ext(pfn);
438 return 0;
439
440 }
441
page_ext_callback(struct notifier_block * self,unsigned long action,void * arg)442 static int __meminit page_ext_callback(struct notifier_block *self,
443 unsigned long action, void *arg)
444 {
445 struct memory_notify *mn = arg;
446 int ret = 0;
447
448 switch (action) {
449 case MEM_GOING_ONLINE:
450 ret = online_page_ext(mn->start_pfn,
451 mn->nr_pages, mn->status_change_nid);
452 break;
453 case MEM_OFFLINE:
454 offline_page_ext(mn->start_pfn,
455 mn->nr_pages, mn->status_change_nid);
456 break;
457 case MEM_CANCEL_ONLINE:
458 offline_page_ext(mn->start_pfn,
459 mn->nr_pages, mn->status_change_nid);
460 break;
461 case MEM_GOING_OFFLINE:
462 break;
463 case MEM_ONLINE:
464 case MEM_CANCEL_OFFLINE:
465 break;
466 }
467
468 return notifier_from_errno(ret);
469 }
470
471 #endif
472
page_ext_init(void)473 void __init page_ext_init(void)
474 {
475 unsigned long pfn;
476 int nid;
477
478 if (!invoke_need_callbacks())
479 return;
480
481 for_each_node_state(nid, N_MEMORY) {
482 unsigned long start_pfn, end_pfn;
483
484 start_pfn = node_start_pfn(nid);
485 end_pfn = node_end_pfn(nid);
486 /*
487 * start_pfn and end_pfn may not be aligned to SECTION and the
488 * page->flags of out of node pages are not initialized. So we
489 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
490 */
491 for (pfn = start_pfn; pfn < end_pfn;
492 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
493
494 if (!pfn_valid(pfn))
495 continue;
496 /*
497 * Nodes's pfns can be overlapping.
498 * We know some arch can have a nodes layout such as
499 * -------------pfn-------------->
500 * N0 | N1 | N2 | N0 | N1 | N2|....
501 */
502 if (pfn_to_nid(pfn) != nid)
503 continue;
504 if (init_section_page_ext(pfn, nid))
505 goto oom;
506 cond_resched();
507 }
508 }
509 hotplug_memory_notifier(page_ext_callback, 0);
510 pr_info("allocated %ld bytes of page_ext\n", total_usage);
511 invoke_init_callbacks();
512 return;
513
514 oom:
515 panic("Out of memory");
516 }
517
pgdat_page_ext_init(struct pglist_data * pgdat)518 void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
519 {
520 }
521
522 #endif
523