]>
git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/blob - mm/page_ext.c
2 #include <linux/mmzone.h>
3 #include <linux/bootmem.h>
4 #include <linux/page_ext.h>
5 #include <linux/memory.h>
6 #include <linux/vmalloc.h>
7 #include <linux/kmemleak.h>
10 * struct page extension
12 * This is the feature to manage memory for extended data per page.
14 * Until now, we must modify struct page itself to store extra data per page.
15 * This requires rebuilding the kernel and it is really time consuming process.
16 * And, sometimes, rebuild is impossible due to third party module dependency.
17 * At last, enlarging struct page could cause un-wanted system behaviour change.
19 * This feature is intended to overcome above mentioned problems. This feature
20 * allocates memory for extended data per page in certain place rather than
21 * the struct page itself. This memory can be accessed by the accessor
22 * functions provided by this code. During the boot process, it checks whether
23 * allocation of huge chunk of memory is needed or not. If not, it avoids
24 * allocating memory at all. With this advantage, we can include this feature
25 * into the kernel in default and can avoid rebuild and solve related problems.
27 * To help these things to work well, there are two callbacks for clients. One
28 * is the need callback which is mandatory if user wants to avoid useless
29 * memory allocation at boot-time. The other is optional, init callback, which
30 * is used to do proper initialization after memory is allocated.
32 * The need callback is used to decide whether extended memory allocation is
33 * needed or not. Sometimes users want to deactivate some features in this
34 * boot and extra memory would be unneccessary. In this case, to avoid
35 * allocating huge chunk of memory, each clients represent their need of
36 * extra memory through the need callback. If one of the need callbacks
37 * returns true, it means that someone needs extra memory so that
38 * page extension core should allocates memory for page extension. If
39 * none of need callbacks return true, memory isn't needed at all in this boot
40 * and page extension core can skip to allocate memory. As result,
41 * none of memory is wasted.
43 * The init callback is used to do proper initialization after page extension
44 * is completely initialized. In sparse memory system, extra memory is
45 * allocated some time later than memmap is allocated. In other words, lifetime
46 * of memory for page extension isn't same with memmap for struct page.
47 * Therefore, clients can't store extra data until page extension is
48 * initialized, even if pages are allocated and used freely. This could
49 * cause inadequate state of extra data per page, so, to prevent it, client
50 * can utilize this callback to initialize the state of it correctly.
53 static struct page_ext_operations
*page_ext_ops
[] = {
55 #ifdef CONFIG_PAGE_POISONING
60 static unsigned long total_usage
;
62 static bool __init
invoke_need_callbacks(void)
65 int entries
= ARRAY_SIZE(page_ext_ops
);
67 for (i
= 0; i
< entries
; i
++) {
68 if (page_ext_ops
[i
]->need
&& page_ext_ops
[i
]->need())
75 static void __init
invoke_init_callbacks(void)
78 int entries
= ARRAY_SIZE(page_ext_ops
);
80 for (i
= 0; i
< entries
; i
++) {
81 if (page_ext_ops
[i
]->init
)
82 page_ext_ops
[i
]->init();
86 #if !defined(CONFIG_SPARSEMEM)
89 void __meminit
pgdat_page_ext_init(struct pglist_data
*pgdat
)
91 pgdat
->node_page_ext
= NULL
;
94 struct page_ext
*lookup_page_ext(struct page
*page
)
96 unsigned long pfn
= page_to_pfn(page
);
98 struct page_ext
*base
;
100 base
= NODE_DATA(page_to_nid(page
))->node_page_ext
;
101 #ifdef CONFIG_DEBUG_VM
103 * The sanity checks the page allocator does upon freeing a
104 * page can reach here before the page_ext arrays are
105 * allocated when feeding a range of pages to the allocator
106 * for the first time during bootup or memory hotplug.
111 offset
= pfn
- round_down(node_start_pfn(page_to_nid(page
)),
113 return base
+ offset
;
116 static int __init
alloc_node_page_ext(int nid
)
118 struct page_ext
*base
;
119 unsigned long table_size
;
120 unsigned long nr_pages
;
122 nr_pages
= NODE_DATA(nid
)->node_spanned_pages
;
127 * Need extra space if node range is not aligned with
128 * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm
129 * checks buddy's status, range could be out of exact node range.
131 if (!IS_ALIGNED(node_start_pfn(nid
), MAX_ORDER_NR_PAGES
) ||
132 !IS_ALIGNED(node_end_pfn(nid
), MAX_ORDER_NR_PAGES
))
133 nr_pages
+= MAX_ORDER_NR_PAGES
;
135 table_size
= sizeof(struct page_ext
) * nr_pages
;
137 base
= memblock_virt_alloc_try_nid_nopanic(
138 table_size
, PAGE_SIZE
, __pa(MAX_DMA_ADDRESS
),
139 BOOTMEM_ALLOC_ACCESSIBLE
, nid
);
142 NODE_DATA(nid
)->node_page_ext
= base
;
143 total_usage
+= table_size
;
147 void __init
page_ext_init_flatmem(void)
152 if (!invoke_need_callbacks())
155 for_each_online_node(nid
) {
156 fail
= alloc_node_page_ext(nid
);
160 pr_info("allocated %ld bytes of page_ext\n", total_usage
);
161 invoke_init_callbacks();
165 pr_crit("allocation of page_ext failed.\n");
166 panic("Out of memory");
169 #else /* CONFIG_FLAT_NODE_MEM_MAP */
171 struct page_ext
*lookup_page_ext(struct page
*page
)
173 unsigned long pfn
= page_to_pfn(page
);
174 struct mem_section
*section
= __pfn_to_section(pfn
);
175 #ifdef CONFIG_DEBUG_VM
177 * The sanity checks the page allocator does upon freeing a
178 * page can reach here before the page_ext arrays are
179 * allocated when feeding a range of pages to the allocator
180 * for the first time during bootup or memory hotplug.
182 if (!section
->page_ext
)
185 return section
->page_ext
+ pfn
;
188 static void *__meminit
alloc_page_ext(size_t size
, int nid
)
190 gfp_t flags
= GFP_KERNEL
| __GFP_ZERO
| __GFP_NOWARN
;
193 addr
= alloc_pages_exact_nid(nid
, size
, flags
);
195 kmemleak_alloc(addr
, size
, 1, flags
);
199 if (node_state(nid
, N_HIGH_MEMORY
))
200 addr
= vzalloc_node(size
, nid
);
202 addr
= vzalloc(size
);
207 static int __meminit
init_section_page_ext(unsigned long pfn
, int nid
)
209 struct mem_section
*section
;
210 struct page_ext
*base
;
211 unsigned long table_size
;
213 section
= __pfn_to_section(pfn
);
215 if (section
->page_ext
)
218 table_size
= sizeof(struct page_ext
) * PAGES_PER_SECTION
;
219 base
= alloc_page_ext(table_size
, nid
);
222 * The value stored in section->page_ext is (base - pfn)
223 * and it does not point to the memory block allocated above,
224 * causing kmemleak false positives.
226 kmemleak_not_leak(base
);
229 pr_err("page ext allocation failure\n");
234 * The passed "pfn" may not be aligned to SECTION. For the calculation
235 * we need to apply a mask.
237 pfn
&= PAGE_SECTION_MASK
;
238 section
->page_ext
= base
- pfn
;
239 total_usage
+= table_size
;
242 #ifdef CONFIG_MEMORY_HOTPLUG
243 static void free_page_ext(void *addr
)
245 if (is_vmalloc_addr(addr
)) {
248 struct page
*page
= virt_to_page(addr
);
251 table_size
= sizeof(struct page_ext
) * PAGES_PER_SECTION
;
253 BUG_ON(PageReserved(page
));
254 free_pages_exact(addr
, table_size
);
258 static void __free_page_ext(unsigned long pfn
)
260 struct mem_section
*ms
;
261 struct page_ext
*base
;
263 ms
= __pfn_to_section(pfn
);
264 if (!ms
|| !ms
->page_ext
)
266 base
= ms
->page_ext
+ pfn
;
271 static int __meminit
online_page_ext(unsigned long start_pfn
,
272 unsigned long nr_pages
,
275 unsigned long start
, end
, pfn
;
278 start
= SECTION_ALIGN_DOWN(start_pfn
);
279 end
= SECTION_ALIGN_UP(start_pfn
+ nr_pages
);
283 * In this case, "nid" already exists and contains valid memory.
284 * "start_pfn" passed to us is a pfn which is an arg for
285 * online__pages(), and start_pfn should exist.
287 nid
= pfn_to_nid(start_pfn
);
288 VM_BUG_ON(!node_state(nid
, N_ONLINE
));
291 for (pfn
= start
; !fail
&& pfn
< end
; pfn
+= PAGES_PER_SECTION
) {
292 if (!pfn_present(pfn
))
294 fail
= init_section_page_ext(pfn
, nid
);
300 for (pfn
= start
; pfn
< end
; pfn
+= PAGES_PER_SECTION
)
301 __free_page_ext(pfn
);
306 static int __meminit
offline_page_ext(unsigned long start_pfn
,
307 unsigned long nr_pages
, int nid
)
309 unsigned long start
, end
, pfn
;
311 start
= SECTION_ALIGN_DOWN(start_pfn
);
312 end
= SECTION_ALIGN_UP(start_pfn
+ nr_pages
);
314 for (pfn
= start
; pfn
< end
; pfn
+= PAGES_PER_SECTION
)
315 __free_page_ext(pfn
);
320 static int __meminit
page_ext_callback(struct notifier_block
*self
,
321 unsigned long action
, void *arg
)
323 struct memory_notify
*mn
= arg
;
327 case MEM_GOING_ONLINE
:
328 ret
= online_page_ext(mn
->start_pfn
,
329 mn
->nr_pages
, mn
->status_change_nid
);
332 offline_page_ext(mn
->start_pfn
,
333 mn
->nr_pages
, mn
->status_change_nid
);
335 case MEM_CANCEL_ONLINE
:
336 offline_page_ext(mn
->start_pfn
,
337 mn
->nr_pages
, mn
->status_change_nid
);
339 case MEM_GOING_OFFLINE
:
342 case MEM_CANCEL_OFFLINE
:
346 return notifier_from_errno(ret
);
351 void __init
page_ext_init(void)
356 if (!invoke_need_callbacks())
359 for_each_node_state(nid
, N_MEMORY
) {
360 unsigned long start_pfn
, end_pfn
;
362 start_pfn
= node_start_pfn(nid
);
363 end_pfn
= node_end_pfn(nid
);
365 * start_pfn and end_pfn may not be aligned to SECTION and the
366 * page->flags of out of node pages are not initialized. So we
367 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
369 for (pfn
= start_pfn
; pfn
< end_pfn
;
370 pfn
= ALIGN(pfn
+ 1, PAGES_PER_SECTION
)) {
375 * Nodes's pfns can be overlapping.
376 * We know some arch can have a nodes layout such as
377 * -------------pfn-------------->
378 * N0 | N1 | N2 | N0 | N1 | N2|....
380 if (pfn_to_nid(pfn
) != nid
)
382 if (init_section_page_ext(pfn
, nid
))
386 hotplug_memory_notifier(page_ext_callback
, 0);
387 pr_info("allocated %ld bytes of page_ext\n", total_usage
);
388 invoke_init_callbacks();
392 panic("Out of memory");
395 void __meminit
pgdat_page_ext_init(struct pglist_data
*pgdat
)