blob: 7cd4e37dfea21eca7c46ce947bc3c3cd41d8d00a [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Joonsoo Kimeefa864b2014-12-12 16:55:46 -08002#include <linux/mm.h>
3#include <linux/mmzone.h>
Mike Rapoport57c8a662018-10-30 15:09:49 -07004#include <linux/memblock.h>
Joonsoo Kimeefa864b2014-12-12 16:55:46 -08005#include <linux/page_ext.h>
6#include <linux/memory.h>
7#include <linux/vmalloc.h>
8#include <linux/kmemleak.h>
Joonsoo Kim48c96a32014-12-12 16:56:01 -08009#include <linux/page_owner.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070010#include <linux/page_idle.h>
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +053011#include <linux/rcupdate.h>
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080012/*
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 *
Joonsoo Kim980ac162016-10-07 16:58:27 -070046 * 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 *
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080051 * 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
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +053061#ifdef CONFIG_SPARSEMEM
62#define PAGE_EXT_INVALID (0x1)
63#endif
64
SeongJae Park75e13ba2021-09-07 19:56:40 -070065#if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
66static bool need_page_idle(void)
67{
68 return true;
69}
70struct page_ext_operations page_idle_ops = {
71 .need = need_page_idle,
72};
73#endif
74
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080075static struct page_ext_operations *page_ext_ops[] = {
Joonsoo Kim48c96a32014-12-12 16:56:01 -080076#ifdef CONFIG_PAGE_OWNER
77 &page_owner_ops,
78#endif
SeongJae Park75e13ba2021-09-07 19:56:40 -070079#if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070080 &page_idle_ops,
81#endif
Minchan Kim6e12c5b2021-03-18 09:56:10 -070082#ifdef CONFIG_PAGE_PINNER
83 &page_pinner_ops,
84#endif
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080085};
86
Vlastimil Babka5556cfe2019-10-14 14:11:40 -070087unsigned long page_ext_size = sizeof(struct page_ext);
88
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080089static unsigned long total_usage;
90
91static bool __init invoke_need_callbacks(void)
92{
93 int i;
94 int entries = ARRAY_SIZE(page_ext_ops);
Joonsoo Kim980ac162016-10-07 16:58:27 -070095 bool need = false;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080096
97 for (i = 0; i < entries; i++) {
Joonsoo Kim980ac162016-10-07 16:58:27 -070098 if (page_ext_ops[i]->need && page_ext_ops[i]->need()) {
Vlastimil Babka5556cfe2019-10-14 14:11:40 -070099 page_ext_ops[i]->offset = page_ext_size;
100 page_ext_size += page_ext_ops[i]->size;
Joonsoo Kim980ac162016-10-07 16:58:27 -0700101 need = true;
102 }
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800103 }
104
Joonsoo Kim980ac162016-10-07 16:58:27 -0700105 return need;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800106}
107
108static 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
Zhenhua Huang1fe13c12020-12-14 19:04:46 -0800119#ifndef CONFIG_SPARSEMEM
120void __init page_ext_init_flatmem_late(void)
121{
122 invoke_init_callbacks();
123}
124#endif
125
Joonsoo Kim980ac162016-10-07 16:58:27 -0700126static inline struct page_ext *get_entry(void *base, unsigned long index)
127{
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700128 return base + page_ext_size * index;
Joonsoo Kim980ac162016-10-07 16:58:27 -0700129}
130
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530131/**
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 */
142struct 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.
Charan Teja Kalla9a595402022-11-08 10:46:22 +0530158 * @page_ext: Page extended information received from page_ext_get().
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530159 *
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 */
166void page_ext_put(struct page_ext *page_ext)
167{
168 if (unlikely(!page_ext))
169 return;
170
171 rcu_read_unlock();
172}
Zhenhua Huang1fe13c12020-12-14 19:04:46 -0800173#ifndef CONFIG_SPARSEMEM
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800174
175
176void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
177{
178 pgdat->node_page_ext = NULL;
179}
180
Kirill A. Shutemov10ed6342018-08-17 15:45:15 -0700181struct page_ext *lookup_page_ext(const struct page *page)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800182{
183 unsigned long pfn = page_to_pfn(page);
Joonsoo Kim0b06bb32016-10-07 16:58:24 -0700184 unsigned long index;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800185 struct page_ext *base;
186
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530187 WARN_ON_ONCE(!rcu_read_lock_held());
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800188 base = NODE_DATA(page_to_nid(page))->node_page_ext;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800189 /*
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;
Joonsoo Kim0b06bb32016-10-07 16:58:24 -0700197 index = pfn - round_down(node_start_pfn(page_to_nid(page)),
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800198 MAX_ORDER_NR_PAGES);
Joonsoo Kim980ac162016-10-07 16:58:27 -0700199 return get_entry(base, index);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800200}
Vijayanand Jitta0a7166a2021-01-05 11:27:28 +0530201EXPORT_SYMBOL_GPL(lookup_page_ext);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800202
203static 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
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700222 table_size = page_ext_size * nr_pages;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800223
Mike Rapoport26fb3da2019-03-11 23:30:42 -0700224 base = memblock_alloc_try_nid(
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800225 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
Mike Rapoport97ad1082018-10-30 15:09:44 -0700226 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800227 if (!base)
228 return -ENOMEM;
229 NODE_DATA(nid)->node_page_ext = base;
230 total_usage += table_size;
231 return 0;
232}
233
234void __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);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800248 return;
249
250fail:
251 pr_crit("allocation of page_ext failed.\n");
252 panic("Out of memory");
253}
254
255#else /* CONFIG_FLAT_NODE_MEM_MAP */
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530256static 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}
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800260
Kirill A. Shutemov10ed6342018-08-17 15:45:15 -0700261struct page_ext *lookup_page_ext(const struct page *page)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800262{
263 unsigned long pfn = page_to_pfn(page);
264 struct mem_section *section = __pfn_to_section(pfn);
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530265 struct page_ext *page_ext = READ_ONCE(section->page_ext);
266
267 WARN_ON_ONCE(!rcu_read_lock_held());
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800268 /*
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.
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800273 */
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530274 if (page_ext_invalid(page_ext))
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800275 return NULL;
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530276 return get_entry(page_ext, pfn);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800277}
Vijayanand Jitta0a7166a2021-01-05 11:27:28 +0530278EXPORT_SYMBOL_GPL(lookup_page_ext);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800279
280static 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
Michal Hockob95046b2017-09-06 16:20:41 -0700291 addr = vzalloc_node(size, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800292
293 return addr;
294}
295
296static 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
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700307 table_size = page_ext_size * PAGES_PER_SECTION;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800308 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;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700327 section->page_ext = (void *)base - page_ext_size * pfn;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800328 total_usage += table_size;
329 return 0;
330}
331#ifdef CONFIG_MEMORY_HOTPLUG
332static 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
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700340 table_size = page_ext_size * PAGES_PER_SECTION;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800341
342 BUG_ON(PageReserved(page));
Qian Cai0c815852019-03-05 15:49:46 -0800343 kmemleak_free(addr);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800344 free_pages_exact(addr, table_size);
345 }
346}
347
348static 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;
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530356
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);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800367 free_page_ext(base);
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530368}
369
370static 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);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800380}
381
382static 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
Anshuman Khandual98fa15f2019-03-05 15:42:58 -0800392 if (nid == NUMA_NO_NODE) {
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800393 /*
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
David Hildenbranddccacf82020-04-06 20:06:47 -0700402 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800403 fail = init_section_page_ext(pfn, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800404 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
414static 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
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530422 /*
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
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800436 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
437 __free_page_ext(pfn);
438 return 0;
439
440}
441
442static 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
473void __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 */
Qian Cai2f1ee092019-02-12 15:36:03 -0800502 if (pfn_to_nid(pfn) != nid)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800503 continue;
504 if (init_section_page_ext(pfn, nid))
505 goto oom;
Vlastimil Babka0fc542b2017-09-06 16:20:48 -0700506 cond_resched();
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800507 }
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
514oom:
515 panic("Out of memory");
516}
517
518void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
519{
520}
521
522#endif