blob: 4f81320f62cef171333c9f45de48dd810d42d4ce [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>
Minchan Kime12acd32022-09-20 14:42:50 -070010#include <linux/page_pinner.h>
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070011#include <linux/page_idle.h>
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080012
13/*
14 * struct page extension
15 *
16 * This is the feature to manage memory for extended data per page.
17 *
18 * Until now, we must modify struct page itself to store extra data per page.
19 * This requires rebuilding the kernel and it is really time consuming process.
20 * And, sometimes, rebuild is impossible due to third party module dependency.
21 * At last, enlarging struct page could cause un-wanted system behaviour change.
22 *
23 * This feature is intended to overcome above mentioned problems. This feature
24 * allocates memory for extended data per page in certain place rather than
25 * the struct page itself. This memory can be accessed by the accessor
26 * functions provided by this code. During the boot process, it checks whether
27 * allocation of huge chunk of memory is needed or not. If not, it avoids
28 * allocating memory at all. With this advantage, we can include this feature
29 * into the kernel in default and can avoid rebuild and solve related problems.
30 *
31 * To help these things to work well, there are two callbacks for clients. One
32 * is the need callback which is mandatory if user wants to avoid useless
33 * memory allocation at boot-time. The other is optional, init callback, which
34 * is used to do proper initialization after memory is allocated.
35 *
36 * The need callback is used to decide whether extended memory allocation is
37 * needed or not. Sometimes users want to deactivate some features in this
Haitao Shi8958b242020-12-15 20:47:26 -080038 * boot and extra memory would be unnecessary. In this case, to avoid
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080039 * allocating huge chunk of memory, each clients represent their need of
40 * extra memory through the need callback. If one of the need callbacks
41 * returns true, it means that someone needs extra memory so that
42 * page extension core should allocates memory for page extension. If
43 * none of need callbacks return true, memory isn't needed at all in this boot
44 * and page extension core can skip to allocate memory. As result,
45 * none of memory is wasted.
46 *
Joonsoo Kim980ac162016-10-07 16:58:27 -070047 * When need callback returns true, page_ext checks if there is a request for
48 * extra memory through size in struct page_ext_operations. If it is non-zero,
49 * extra space is allocated for each page_ext entry and offset is returned to
50 * user through offset in struct page_ext_operations.
51 *
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080052 * The init callback is used to do proper initialization after page extension
53 * is completely initialized. In sparse memory system, extra memory is
54 * allocated some time later than memmap is allocated. In other words, lifetime
55 * of memory for page extension isn't same with memmap for struct page.
56 * Therefore, clients can't store extra data until page extension is
57 * initialized, even if pages are allocated and used freely. This could
58 * cause inadequate state of extra data per page, so, to prevent it, client
59 * can utilize this callback to initialize the state of it correctly.
60 */
61
SeongJae Park1c676e02021-09-07 19:56:40 -070062#if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
63static bool need_page_idle(void)
64{
65 return true;
66}
67struct page_ext_operations page_idle_ops = {
68 .need = need_page_idle,
69};
70#endif
71
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080072static struct page_ext_operations *page_ext_ops[] = {
Joonsoo Kim48c96a32014-12-12 16:56:01 -080073#ifdef CONFIG_PAGE_OWNER
74 &page_owner_ops,
75#endif
SeongJae Park1c676e02021-09-07 19:56:40 -070076#if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070077 &page_idle_ops,
78#endif
Minchan Kime12acd32022-09-20 14:42:50 -070079#ifdef CONFIG_PAGE_PINNER
80 &page_pinner_ops,
81#endif
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080082};
83
Vlastimil Babka5556cfe2019-10-14 14:11:40 -070084unsigned long page_ext_size = sizeof(struct page_ext);
85
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080086static unsigned long total_usage;
87
88static bool __init invoke_need_callbacks(void)
89{
90 int i;
91 int entries = ARRAY_SIZE(page_ext_ops);
Joonsoo Kim980ac162016-10-07 16:58:27 -070092 bool need = false;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080093
94 for (i = 0; i < entries; i++) {
Joonsoo Kim980ac162016-10-07 16:58:27 -070095 if (page_ext_ops[i]->need && page_ext_ops[i]->need()) {
Vlastimil Babka5556cfe2019-10-14 14:11:40 -070096 page_ext_ops[i]->offset = page_ext_size;
97 page_ext_size += page_ext_ops[i]->size;
Joonsoo Kim980ac162016-10-07 16:58:27 -070098 need = true;
99 }
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800100 }
101
Joonsoo Kim980ac162016-10-07 16:58:27 -0700102 return need;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800103}
104
105static void __init invoke_init_callbacks(void)
106{
107 int i;
108 int entries = ARRAY_SIZE(page_ext_ops);
109
110 for (i = 0; i < entries; i++) {
111 if (page_ext_ops[i]->init)
112 page_ext_ops[i]->init();
113 }
114}
115
Zhenhua Huang7fb7ab62020-12-14 19:04:46 -0800116#ifndef CONFIG_SPARSEMEM
117void __init page_ext_init_flatmem_late(void)
118{
119 invoke_init_callbacks();
120}
121#endif
122
Joonsoo Kim980ac162016-10-07 16:58:27 -0700123static inline struct page_ext *get_entry(void *base, unsigned long index)
124{
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700125 return base + page_ext_size * index;
Joonsoo Kim980ac162016-10-07 16:58:27 -0700126}
127
Zhenhua Huang7fb7ab62020-12-14 19:04:46 -0800128#ifndef CONFIG_SPARSEMEM
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800129
130
131void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
132{
133 pgdat->node_page_ext = NULL;
134}
135
Kirill A. Shutemov10ed6342018-08-17 15:45:15 -0700136struct page_ext *lookup_page_ext(const struct page *page)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800137{
138 unsigned long pfn = page_to_pfn(page);
Joonsoo Kim0b06bb32016-10-07 16:58:24 -0700139 unsigned long index;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800140 struct page_ext *base;
141
142 base = NODE_DATA(page_to_nid(page))->node_page_ext;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800143 /*
144 * The sanity checks the page allocator does upon freeing a
145 * page can reach here before the page_ext arrays are
146 * allocated when feeding a range of pages to the allocator
147 * for the first time during bootup or memory hotplug.
148 */
149 if (unlikely(!base))
150 return NULL;
Joonsoo Kim0b06bb32016-10-07 16:58:24 -0700151 index = pfn - round_down(node_start_pfn(page_to_nid(page)),
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800152 MAX_ORDER_NR_PAGES);
Joonsoo Kim980ac162016-10-07 16:58:27 -0700153 return get_entry(base, index);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800154}
Vijayanand Jittae619b192021-01-05 11:27:28 +0530155EXPORT_SYMBOL_NS_GPL(lookup_page_ext, MINIDUMP);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800156
157static int __init alloc_node_page_ext(int nid)
158{
159 struct page_ext *base;
160 unsigned long table_size;
161 unsigned long nr_pages;
162
163 nr_pages = NODE_DATA(nid)->node_spanned_pages;
164 if (!nr_pages)
165 return 0;
166
167 /*
168 * Need extra space if node range is not aligned with
169 * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm
170 * checks buddy's status, range could be out of exact node range.
171 */
172 if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) ||
173 !IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES))
174 nr_pages += MAX_ORDER_NR_PAGES;
175
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700176 table_size = page_ext_size * nr_pages;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800177
Mike Rapoport26fb3da2019-03-11 23:30:42 -0700178 base = memblock_alloc_try_nid(
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800179 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
Mike Rapoport97ad1082018-10-30 15:09:44 -0700180 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800181 if (!base)
182 return -ENOMEM;
183 NODE_DATA(nid)->node_page_ext = base;
184 total_usage += table_size;
185 return 0;
186}
187
188void __init page_ext_init_flatmem(void)
189{
190
191 int nid, fail;
192
193 if (!invoke_need_callbacks())
194 return;
195
196 for_each_online_node(nid) {
197 fail = alloc_node_page_ext(nid);
198 if (fail)
199 goto fail;
200 }
201 pr_info("allocated %ld bytes of page_ext\n", total_usage);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800202 return;
203
204fail:
205 pr_crit("allocation of page_ext failed.\n");
206 panic("Out of memory");
207}
208
Mike Rapoport43b02ba2021-06-28 19:43:05 -0700209#else /* CONFIG_FLATMEM */
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800210
Kirill A. Shutemov10ed6342018-08-17 15:45:15 -0700211struct page_ext *lookup_page_ext(const struct page *page)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800212{
213 unsigned long pfn = page_to_pfn(page);
214 struct mem_section *section = __pfn_to_section(pfn);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800215 /*
216 * The sanity checks the page allocator does upon freeing a
217 * page can reach here before the page_ext arrays are
218 * allocated when feeding a range of pages to the allocator
219 * for the first time during bootup or memory hotplug.
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800220 */
221 if (!section->page_ext)
222 return NULL;
Joonsoo Kim980ac162016-10-07 16:58:27 -0700223 return get_entry(section->page_ext, pfn);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800224}
Vijayanand Jittae619b192021-01-05 11:27:28 +0530225EXPORT_SYMBOL_NS_GPL(lookup_page_ext, MINIDUMP);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800226
227static void *__meminit alloc_page_ext(size_t size, int nid)
228{
229 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
230 void *addr = NULL;
231
232 addr = alloc_pages_exact_nid(nid, size, flags);
233 if (addr) {
234 kmemleak_alloc(addr, size, 1, flags);
235 return addr;
236 }
237
Michal Hockob95046b2017-09-06 16:20:41 -0700238 addr = vzalloc_node(size, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800239
240 return addr;
241}
242
243static int __meminit init_section_page_ext(unsigned long pfn, int nid)
244{
245 struct mem_section *section;
246 struct page_ext *base;
247 unsigned long table_size;
248
249 section = __pfn_to_section(pfn);
250
251 if (section->page_ext)
252 return 0;
253
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700254 table_size = page_ext_size * PAGES_PER_SECTION;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800255 base = alloc_page_ext(table_size, nid);
256
257 /*
258 * The value stored in section->page_ext is (base - pfn)
259 * and it does not point to the memory block allocated above,
260 * causing kmemleak false positives.
261 */
262 kmemleak_not_leak(base);
263
264 if (!base) {
265 pr_err("page ext allocation failure\n");
266 return -ENOMEM;
267 }
268
269 /*
270 * The passed "pfn" may not be aligned to SECTION. For the calculation
271 * we need to apply a mask.
272 */
273 pfn &= PAGE_SECTION_MASK;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700274 section->page_ext = (void *)base - page_ext_size * pfn;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800275 total_usage += table_size;
276 return 0;
277}
Dave Hansen76af6a02021-10-18 15:15:32 -0700278
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800279static void free_page_ext(void *addr)
280{
281 if (is_vmalloc_addr(addr)) {
282 vfree(addr);
283 } else {
284 struct page *page = virt_to_page(addr);
285 size_t table_size;
286
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700287 table_size = page_ext_size * PAGES_PER_SECTION;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800288
289 BUG_ON(PageReserved(page));
Qian Cai0c815852019-03-05 15:49:46 -0800290 kmemleak_free(addr);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800291 free_pages_exact(addr, table_size);
292 }
293}
294
295static void __free_page_ext(unsigned long pfn)
296{
297 struct mem_section *ms;
298 struct page_ext *base;
299
300 ms = __pfn_to_section(pfn);
301 if (!ms || !ms->page_ext)
302 return;
Joonsoo Kim980ac162016-10-07 16:58:27 -0700303 base = get_entry(ms->page_ext, pfn);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800304 free_page_ext(base);
305 ms->page_ext = NULL;
306}
307
308static int __meminit online_page_ext(unsigned long start_pfn,
309 unsigned long nr_pages,
310 int nid)
311{
312 unsigned long start, end, pfn;
313 int fail = 0;
314
315 start = SECTION_ALIGN_DOWN(start_pfn);
316 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
317
Anshuman Khandual98fa15f2019-03-05 15:42:58 -0800318 if (nid == NUMA_NO_NODE) {
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800319 /*
320 * In this case, "nid" already exists and contains valid memory.
321 * "start_pfn" passed to us is a pfn which is an arg for
322 * online__pages(), and start_pfn should exist.
323 */
324 nid = pfn_to_nid(start_pfn);
325 VM_BUG_ON(!node_state(nid, N_ONLINE));
326 }
327
David Hildenbranddccacf82020-04-06 20:06:47 -0700328 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800329 fail = init_section_page_ext(pfn, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800330 if (!fail)
331 return 0;
332
333 /* rollback */
334 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
335 __free_page_ext(pfn);
336
337 return -ENOMEM;
338}
339
340static int __meminit offline_page_ext(unsigned long start_pfn,
341 unsigned long nr_pages, int nid)
342{
343 unsigned long start, end, pfn;
344
345 start = SECTION_ALIGN_DOWN(start_pfn);
346 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
347
348 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
349 __free_page_ext(pfn);
350 return 0;
351
352}
353
354static int __meminit page_ext_callback(struct notifier_block *self,
355 unsigned long action, void *arg)
356{
357 struct memory_notify *mn = arg;
358 int ret = 0;
359
360 switch (action) {
361 case MEM_GOING_ONLINE:
362 ret = online_page_ext(mn->start_pfn,
363 mn->nr_pages, mn->status_change_nid);
364 break;
365 case MEM_OFFLINE:
366 offline_page_ext(mn->start_pfn,
367 mn->nr_pages, mn->status_change_nid);
368 break;
369 case MEM_CANCEL_ONLINE:
370 offline_page_ext(mn->start_pfn,
371 mn->nr_pages, mn->status_change_nid);
372 break;
373 case MEM_GOING_OFFLINE:
374 break;
375 case MEM_ONLINE:
376 case MEM_CANCEL_OFFLINE:
377 break;
378 }
379
380 return notifier_from_errno(ret);
381}
382
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800383void __init page_ext_init(void)
384{
385 unsigned long pfn;
386 int nid;
387
388 if (!invoke_need_callbacks())
389 return;
390
391 for_each_node_state(nid, N_MEMORY) {
392 unsigned long start_pfn, end_pfn;
393
394 start_pfn = node_start_pfn(nid);
395 end_pfn = node_end_pfn(nid);
396 /*
397 * start_pfn and end_pfn may not be aligned to SECTION and the
398 * page->flags of out of node pages are not initialized. So we
399 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
400 */
401 for (pfn = start_pfn; pfn < end_pfn;
402 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
403
404 if (!pfn_valid(pfn))
405 continue;
406 /*
407 * Nodes's pfns can be overlapping.
408 * We know some arch can have a nodes layout such as
409 * -------------pfn-------------->
410 * N0 | N1 | N2 | N0 | N1 | N2|....
411 */
Qian Cai2f1ee092019-02-12 15:36:03 -0800412 if (pfn_to_nid(pfn) != nid)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800413 continue;
414 if (init_section_page_ext(pfn, nid))
415 goto oom;
Vlastimil Babka0fc542b2017-09-06 16:20:48 -0700416 cond_resched();
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800417 }
418 }
419 hotplug_memory_notifier(page_ext_callback, 0);
420 pr_info("allocated %ld bytes of page_ext\n", total_usage);
421 invoke_init_callbacks();
422 return;
423
424oom:
425 panic("Out of memory");
426}
427
428void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
429{
430}
431
432#endif