1 // SPDX-License-Identifier: GPL-2.0
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/page_table_check.h>
14 * struct page extension
16 * This is the feature to manage memory for extended data per page.
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.
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.
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.
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
38 * boot and extra memory would be unnecessary. In this case, to avoid
39 * 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.
47 * 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.
52 * 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.
62 #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
63 static bool need_page_idle(void)
67 static struct page_ext_operations page_idle_ops __initdata = {
68 .need = need_page_idle,
72 static struct page_ext_operations *page_ext_ops[] __initdata = {
73 #ifdef CONFIG_PAGE_OWNER
76 #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
79 #ifdef CONFIG_PAGE_TABLE_CHECK
80 &page_table_check_ops,
84 unsigned long page_ext_size = sizeof(struct page_ext);
86 static unsigned long total_usage;
88 static bool __init invoke_need_callbacks(void)
91 int entries = ARRAY_SIZE(page_ext_ops);
94 for (i = 0; i < entries; i++) {
95 if (page_ext_ops[i]->need && page_ext_ops[i]->need()) {
96 page_ext_ops[i]->offset = page_ext_size;
97 page_ext_size += page_ext_ops[i]->size;
105 static void __init invoke_init_callbacks(void)
108 int entries = ARRAY_SIZE(page_ext_ops);
110 for (i = 0; i < entries; i++) {
111 if (page_ext_ops[i]->init)
112 page_ext_ops[i]->init();
116 #ifndef CONFIG_SPARSEMEM
117 void __init page_ext_init_flatmem_late(void)
119 invoke_init_callbacks();
123 static inline struct page_ext *get_entry(void *base, unsigned long index)
125 return base + page_ext_size * index;
128 #ifndef CONFIG_SPARSEMEM
131 void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
133 pgdat->node_page_ext = NULL;
136 struct page_ext *lookup_page_ext(const struct page *page)
138 unsigned long pfn = page_to_pfn(page);
140 struct page_ext *base;
142 base = NODE_DATA(page_to_nid(page))->node_page_ext;
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.
151 index = pfn - round_down(node_start_pfn(page_to_nid(page)),
153 return get_entry(base, index);
156 static int __init alloc_node_page_ext(int nid)
158 struct page_ext *base;
159 unsigned long table_size;
160 unsigned long nr_pages;
162 nr_pages = NODE_DATA(nid)->node_spanned_pages;
167 * Need extra space if node range is not aligned with
168 * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm
169 * checks buddy's status, range could be out of exact node range.
171 if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) ||
172 !IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES))
173 nr_pages += MAX_ORDER_NR_PAGES;
175 table_size = page_ext_size * nr_pages;
177 base = memblock_alloc_try_nid(
178 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
179 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
182 NODE_DATA(nid)->node_page_ext = base;
183 total_usage += table_size;
187 void __init page_ext_init_flatmem(void)
192 if (!invoke_need_callbacks())
195 for_each_online_node(nid) {
196 fail = alloc_node_page_ext(nid);
200 pr_info("allocated %ld bytes of page_ext\n", total_usage);
204 pr_crit("allocation of page_ext failed.\n");
205 panic("Out of memory");
208 #else /* CONFIG_SPARSEMEM */
210 struct page_ext *lookup_page_ext(const struct page *page)
212 unsigned long pfn = page_to_pfn(page);
213 struct mem_section *section = __pfn_to_section(pfn);
215 * The sanity checks the page allocator does upon freeing a
216 * page can reach here before the page_ext arrays are
217 * allocated when feeding a range of pages to the allocator
218 * for the first time during bootup or memory hotplug.
220 if (!section->page_ext)
222 return get_entry(section->page_ext, pfn);
225 static void *__meminit alloc_page_ext(size_t size, int nid)
227 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
230 addr = alloc_pages_exact_nid(nid, size, flags);
232 kmemleak_alloc(addr, size, 1, flags);
236 addr = vzalloc_node(size, nid);
241 static int __meminit init_section_page_ext(unsigned long pfn, int nid)
243 struct mem_section *section;
244 struct page_ext *base;
245 unsigned long table_size;
247 section = __pfn_to_section(pfn);
249 if (section->page_ext)
252 table_size = page_ext_size * PAGES_PER_SECTION;
253 base = alloc_page_ext(table_size, nid);
256 * The value stored in section->page_ext is (base - pfn)
257 * and it does not point to the memory block allocated above,
258 * causing kmemleak false positives.
260 kmemleak_not_leak(base);
263 pr_err("page ext allocation failure\n");
268 * The passed "pfn" may not be aligned to SECTION. For the calculation
269 * we need to apply a mask.
271 pfn &= PAGE_SECTION_MASK;
272 section->page_ext = (void *)base - page_ext_size * pfn;
273 total_usage += table_size;
277 static void free_page_ext(void *addr)
279 if (is_vmalloc_addr(addr)) {
282 struct page *page = virt_to_page(addr);
285 table_size = page_ext_size * PAGES_PER_SECTION;
287 BUG_ON(PageReserved(page));
289 free_pages_exact(addr, table_size);
293 static void __free_page_ext(unsigned long pfn)
295 struct mem_section *ms;
296 struct page_ext *base;
298 ms = __pfn_to_section(pfn);
299 if (!ms || !ms->page_ext)
301 base = get_entry(ms->page_ext, pfn);
306 static int __meminit online_page_ext(unsigned long start_pfn,
307 unsigned long nr_pages,
310 unsigned long start, end, pfn;
313 start = SECTION_ALIGN_DOWN(start_pfn);
314 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
316 if (nid == NUMA_NO_NODE) {
318 * In this case, "nid" already exists and contains valid memory.
319 * "start_pfn" passed to us is a pfn which is an arg for
320 * online__pages(), and start_pfn should exist.
322 nid = pfn_to_nid(start_pfn);
323 VM_BUG_ON(!node_online(nid));
326 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION)
327 fail = init_section_page_ext(pfn, nid);
332 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
333 __free_page_ext(pfn);
338 static int __meminit offline_page_ext(unsigned long start_pfn,
339 unsigned long nr_pages, int nid)
341 unsigned long start, end, pfn;
343 start = SECTION_ALIGN_DOWN(start_pfn);
344 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
346 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
347 __free_page_ext(pfn);
352 static int __meminit page_ext_callback(struct notifier_block *self,
353 unsigned long action, void *arg)
355 struct memory_notify *mn = arg;
359 case MEM_GOING_ONLINE:
360 ret = online_page_ext(mn->start_pfn,
361 mn->nr_pages, mn->status_change_nid);
364 offline_page_ext(mn->start_pfn,
365 mn->nr_pages, mn->status_change_nid);
367 case MEM_CANCEL_ONLINE:
368 offline_page_ext(mn->start_pfn,
369 mn->nr_pages, mn->status_change_nid);
371 case MEM_GOING_OFFLINE:
374 case MEM_CANCEL_OFFLINE:
378 return notifier_from_errno(ret);
381 void __init page_ext_init(void)
386 if (!invoke_need_callbacks())
389 for_each_node_state(nid, N_MEMORY) {
390 unsigned long start_pfn, end_pfn;
392 start_pfn = node_start_pfn(nid);
393 end_pfn = node_end_pfn(nid);
395 * start_pfn and end_pfn may not be aligned to SECTION and the
396 * page->flags of out of node pages are not initialized. So we
397 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
399 for (pfn = start_pfn; pfn < end_pfn;
400 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
405 * Nodes's pfns can be overlapping.
406 * We know some arch can have a nodes layout such as
407 * -------------pfn-------------->
408 * N0 | N1 | N2 | N0 | N1 | N2|....
410 if (pfn_to_nid(pfn) != nid)
412 if (init_section_page_ext(pfn, nid))
417 hotplug_memory_notifier(page_ext_callback, 0);
418 pr_info("allocated %ld bytes of page_ext\n", total_usage);
419 invoke_init_callbacks();
423 panic("Out of memory");
426 void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)