blob: e5b56394e2b419bcd67cf37c32042ad4fbb630ab [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
Joonsoo Kim980ac162016-10-07 16:58:27 -0700119static inline struct page_ext *get_entry(void *base, unsigned long index)
120{
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700121 return base + page_ext_size * index;
Joonsoo Kim980ac162016-10-07 16:58:27 -0700122}
123
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530124/**
125 * page_ext_get() - Get the extended information for a page.
126 * @page: The page we're interested in.
127 *
128 * Ensures that the page_ext will remain valid until page_ext_put()
129 * is called.
130 *
131 * Return: NULL if no page_ext exists for this page.
132 * Context: Any context. Caller may not sleep until they have called
133 * page_ext_put().
134 */
135struct page_ext *page_ext_get(struct page *page)
136{
137 struct page_ext *page_ext;
138
139 rcu_read_lock();
140 page_ext = lookup_page_ext(page);
141 if (!page_ext) {
142 rcu_read_unlock();
143 return NULL;
144 }
145
146 return page_ext;
147}
148
149/**
150 * page_ext_put() - Working with page extended information is done.
151 * @page_ext - Page extended information received from page_ext_get().
152 *
153 * The page extended information of the page may not be valid after this
154 * function is called.
155 *
156 * Return: None.
157 * Context: Any context with corresponding page_ext_get() is called.
158 */
159void page_ext_put(struct page_ext *page_ext)
160{
161 if (unlikely(!page_ext))
162 return;
163
164 rcu_read_unlock();
165}
166
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800167#if !defined(CONFIG_SPARSEMEM)
168
169
170void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
171{
172 pgdat->node_page_ext = NULL;
173}
174
Kirill A. Shutemov10ed6342018-08-17 15:45:15 -0700175struct page_ext *lookup_page_ext(const struct page *page)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800176{
177 unsigned long pfn = page_to_pfn(page);
Joonsoo Kim0b06bb32016-10-07 16:58:24 -0700178 unsigned long index;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800179 struct page_ext *base;
180
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530181 WARN_ON_ONCE(!rcu_read_lock_held());
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800182 base = NODE_DATA(page_to_nid(page))->node_page_ext;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800183 /*
184 * The sanity checks the page allocator does upon freeing a
185 * page can reach here before the page_ext arrays are
186 * allocated when feeding a range of pages to the allocator
187 * for the first time during bootup or memory hotplug.
188 */
189 if (unlikely(!base))
190 return NULL;
Joonsoo Kim0b06bb32016-10-07 16:58:24 -0700191 index = pfn - round_down(node_start_pfn(page_to_nid(page)),
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800192 MAX_ORDER_NR_PAGES);
Joonsoo Kim980ac162016-10-07 16:58:27 -0700193 return get_entry(base, index);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800194}
Vijayanand Jitta0a7166a2021-01-05 11:27:28 +0530195EXPORT_SYMBOL_GPL(lookup_page_ext);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800196
197static int __init alloc_node_page_ext(int nid)
198{
199 struct page_ext *base;
200 unsigned long table_size;
201 unsigned long nr_pages;
202
203 nr_pages = NODE_DATA(nid)->node_spanned_pages;
204 if (!nr_pages)
205 return 0;
206
207 /*
208 * Need extra space if node range is not aligned with
209 * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm
210 * checks buddy's status, range could be out of exact node range.
211 */
212 if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) ||
213 !IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES))
214 nr_pages += MAX_ORDER_NR_PAGES;
215
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700216 table_size = page_ext_size * nr_pages;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800217
Mike Rapoport26fb3da2019-03-11 23:30:42 -0700218 base = memblock_alloc_try_nid(
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800219 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
Mike Rapoport97ad1082018-10-30 15:09:44 -0700220 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800221 if (!base)
222 return -ENOMEM;
223 NODE_DATA(nid)->node_page_ext = base;
224 total_usage += table_size;
225 return 0;
226}
227
228void __init page_ext_init_flatmem(void)
229{
230
231 int nid, fail;
232
233 if (!invoke_need_callbacks())
234 return;
235
236 for_each_online_node(nid) {
237 fail = alloc_node_page_ext(nid);
238 if (fail)
239 goto fail;
240 }
241 pr_info("allocated %ld bytes of page_ext\n", total_usage);
242 invoke_init_callbacks();
243 return;
244
245fail:
246 pr_crit("allocation of page_ext failed.\n");
247 panic("Out of memory");
248}
249
250#else /* CONFIG_FLAT_NODE_MEM_MAP */
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530251static bool page_ext_invalid(struct page_ext *page_ext)
252{
253 return !page_ext || (((unsigned long)page_ext & PAGE_EXT_INVALID) == PAGE_EXT_INVALID);
254}
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800255
Kirill A. Shutemov10ed6342018-08-17 15:45:15 -0700256struct page_ext *lookup_page_ext(const struct page *page)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800257{
258 unsigned long pfn = page_to_pfn(page);
259 struct mem_section *section = __pfn_to_section(pfn);
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530260 struct page_ext *page_ext = READ_ONCE(section->page_ext);
261
262 WARN_ON_ONCE(!rcu_read_lock_held());
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800263 /*
264 * The sanity checks the page allocator does upon freeing a
265 * page can reach here before the page_ext arrays are
266 * allocated when feeding a range of pages to the allocator
267 * for the first time during bootup or memory hotplug.
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800268 */
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530269 if (page_ext_invalid(page_ext))
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800270 return NULL;
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530271 return get_entry(page_ext, pfn);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800272}
Vijayanand Jitta0a7166a2021-01-05 11:27:28 +0530273EXPORT_SYMBOL_GPL(lookup_page_ext);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800274
275static void *__meminit alloc_page_ext(size_t size, int nid)
276{
277 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
278 void *addr = NULL;
279
280 addr = alloc_pages_exact_nid(nid, size, flags);
281 if (addr) {
282 kmemleak_alloc(addr, size, 1, flags);
283 return addr;
284 }
285
Michal Hockob95046b2017-09-06 16:20:41 -0700286 addr = vzalloc_node(size, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800287
288 return addr;
289}
290
291static int __meminit init_section_page_ext(unsigned long pfn, int nid)
292{
293 struct mem_section *section;
294 struct page_ext *base;
295 unsigned long table_size;
296
297 section = __pfn_to_section(pfn);
298
299 if (section->page_ext)
300 return 0;
301
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700302 table_size = page_ext_size * PAGES_PER_SECTION;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800303 base = alloc_page_ext(table_size, nid);
304
305 /*
306 * The value stored in section->page_ext is (base - pfn)
307 * and it does not point to the memory block allocated above,
308 * causing kmemleak false positives.
309 */
310 kmemleak_not_leak(base);
311
312 if (!base) {
313 pr_err("page ext allocation failure\n");
314 return -ENOMEM;
315 }
316
317 /*
318 * The passed "pfn" may not be aligned to SECTION. For the calculation
319 * we need to apply a mask.
320 */
321 pfn &= PAGE_SECTION_MASK;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700322 section->page_ext = (void *)base - page_ext_size * pfn;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800323 total_usage += table_size;
324 return 0;
325}
326#ifdef CONFIG_MEMORY_HOTPLUG
327static void free_page_ext(void *addr)
328{
329 if (is_vmalloc_addr(addr)) {
330 vfree(addr);
331 } else {
332 struct page *page = virt_to_page(addr);
333 size_t table_size;
334
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700335 table_size = page_ext_size * PAGES_PER_SECTION;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800336
337 BUG_ON(PageReserved(page));
Qian Cai0c815852019-03-05 15:49:46 -0800338 kmemleak_free(addr);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800339 free_pages_exact(addr, table_size);
340 }
341}
342
343static void __free_page_ext(unsigned long pfn)
344{
345 struct mem_section *ms;
346 struct page_ext *base;
347
348 ms = __pfn_to_section(pfn);
349 if (!ms || !ms->page_ext)
350 return;
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530351
352 base = READ_ONCE(ms->page_ext);
353 /*
354 * page_ext here can be valid while doing the roll back
355 * operation in online_page_ext().
356 */
357 if (page_ext_invalid(base))
358 base = (void *)base - PAGE_EXT_INVALID;
359 WRITE_ONCE(ms->page_ext, NULL);
360
361 base = get_entry(base, pfn);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800362 free_page_ext(base);
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530363}
364
365static void __invalidate_page_ext(unsigned long pfn)
366{
367 struct mem_section *ms;
368 void *val;
369
370 ms = __pfn_to_section(pfn);
371 if (!ms || !ms->page_ext)
372 return;
373 val = (void *)ms->page_ext + PAGE_EXT_INVALID;
374 WRITE_ONCE(ms->page_ext, val);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800375}
376
377static int __meminit online_page_ext(unsigned long start_pfn,
378 unsigned long nr_pages,
379 int nid)
380{
381 unsigned long start, end, pfn;
382 int fail = 0;
383
384 start = SECTION_ALIGN_DOWN(start_pfn);
385 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
386
Anshuman Khandual98fa15f2019-03-05 15:42:58 -0800387 if (nid == NUMA_NO_NODE) {
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800388 /*
389 * In this case, "nid" already exists and contains valid memory.
390 * "start_pfn" passed to us is a pfn which is an arg for
391 * online__pages(), and start_pfn should exist.
392 */
393 nid = pfn_to_nid(start_pfn);
394 VM_BUG_ON(!node_state(nid, N_ONLINE));
395 }
396
David Hildenbranddccacf82020-04-06 20:06:47 -0700397 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800398 fail = init_section_page_ext(pfn, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800399 if (!fail)
400 return 0;
401
402 /* rollback */
403 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
404 __free_page_ext(pfn);
405
406 return -ENOMEM;
407}
408
409static int __meminit offline_page_ext(unsigned long start_pfn,
410 unsigned long nr_pages, int nid)
411{
412 unsigned long start, end, pfn;
413
414 start = SECTION_ALIGN_DOWN(start_pfn);
415 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
416
Charan Teja Kalla2b3f9b82022-08-18 19:20:00 +0530417 /*
418 * Freeing of page_ext is done in 3 steps to avoid
419 * use-after-free of it:
420 * 1) Traverse all the sections and mark their page_ext
421 * as invalid.
422 * 2) Wait for all the existing users of page_ext who
423 * started before invalidation to finish.
424 * 3) Free the page_ext.
425 */
426 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
427 __invalidate_page_ext(pfn);
428
429 synchronize_rcu();
430
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800431 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
432 __free_page_ext(pfn);
433 return 0;
434
435}
436
437static int __meminit page_ext_callback(struct notifier_block *self,
438 unsigned long action, void *arg)
439{
440 struct memory_notify *mn = arg;
441 int ret = 0;
442
443 switch (action) {
444 case MEM_GOING_ONLINE:
445 ret = online_page_ext(mn->start_pfn,
446 mn->nr_pages, mn->status_change_nid);
447 break;
448 case MEM_OFFLINE:
449 offline_page_ext(mn->start_pfn,
450 mn->nr_pages, mn->status_change_nid);
451 break;
452 case MEM_CANCEL_ONLINE:
453 offline_page_ext(mn->start_pfn,
454 mn->nr_pages, mn->status_change_nid);
455 break;
456 case MEM_GOING_OFFLINE:
457 break;
458 case MEM_ONLINE:
459 case MEM_CANCEL_OFFLINE:
460 break;
461 }
462
463 return notifier_from_errno(ret);
464}
465
466#endif
467
468void __init page_ext_init(void)
469{
470 unsigned long pfn;
471 int nid;
472
473 if (!invoke_need_callbacks())
474 return;
475
476 for_each_node_state(nid, N_MEMORY) {
477 unsigned long start_pfn, end_pfn;
478
479 start_pfn = node_start_pfn(nid);
480 end_pfn = node_end_pfn(nid);
481 /*
482 * start_pfn and end_pfn may not be aligned to SECTION and the
483 * page->flags of out of node pages are not initialized. So we
484 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
485 */
486 for (pfn = start_pfn; pfn < end_pfn;
487 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
488
489 if (!pfn_valid(pfn))
490 continue;
491 /*
492 * Nodes's pfns can be overlapping.
493 * We know some arch can have a nodes layout such as
494 * -------------pfn-------------->
495 * N0 | N1 | N2 | N0 | N1 | N2|....
496 */
Qian Cai2f1ee092019-02-12 15:36:03 -0800497 if (pfn_to_nid(pfn) != nid)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800498 continue;
499 if (init_section_page_ext(pfn, nid))
500 goto oom;
Vlastimil Babka0fc542b2017-09-06 16:20:48 -0700501 cond_resched();
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800502 }
503 }
504 hotplug_memory_notifier(page_ext_callback, 0);
505 pr_info("allocated %ld bytes of page_ext\n", total_usage);
506 invoke_init_callbacks();
507 return;
508
509oom:
510 panic("Out of memory");
511}
512
513void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
514{
515}
516
517#endif