blob: e5e31ff1adba5cf1751b420e4ab634c50fa22b1d [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>
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080011
12/*
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
SeongJae Park75e13ba2021-09-07 19:56:40 -070061#if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
62static bool need_page_idle(void)
63{
64 return true;
65}
66struct page_ext_operations page_idle_ops = {
67 .need = need_page_idle,
68};
69#endif
70
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080071static struct page_ext_operations *page_ext_ops[] = {
Joonsoo Kim48c96a32014-12-12 16:56:01 -080072#ifdef CONFIG_PAGE_OWNER
73 &page_owner_ops,
74#endif
SeongJae Park75e13ba2021-09-07 19:56:40 -070075#if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
Vladimir Davydov33c3fc72015-09-09 15:35:45 -070076 &page_idle_ops,
77#endif
Minchan Kim6e12c5b2021-03-18 09:56:10 -070078#ifdef CONFIG_PAGE_PINNER
79 &page_pinner_ops,
80#endif
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080081};
82
Vlastimil Babka5556cfe2019-10-14 14:11:40 -070083unsigned long page_ext_size = sizeof(struct page_ext);
84
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080085static unsigned long total_usage;
86
87static bool __init invoke_need_callbacks(void)
88{
89 int i;
90 int entries = ARRAY_SIZE(page_ext_ops);
Joonsoo Kim980ac162016-10-07 16:58:27 -070091 bool need = false;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080092
93 for (i = 0; i < entries; i++) {
Joonsoo Kim980ac162016-10-07 16:58:27 -070094 if (page_ext_ops[i]->need && page_ext_ops[i]->need()) {
Vlastimil Babka5556cfe2019-10-14 14:11:40 -070095 page_ext_ops[i]->offset = page_ext_size;
96 page_ext_size += page_ext_ops[i]->size;
Joonsoo Kim980ac162016-10-07 16:58:27 -070097 need = true;
98 }
Joonsoo Kimeefa864b2014-12-12 16:55:46 -080099 }
100
Joonsoo Kim980ac162016-10-07 16:58:27 -0700101 return need;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800102}
103
104static void __init invoke_init_callbacks(void)
105{
106 int i;
107 int entries = ARRAY_SIZE(page_ext_ops);
108
109 for (i = 0; i < entries; i++) {
110 if (page_ext_ops[i]->init)
111 page_ext_ops[i]->init();
112 }
113}
114
Joonsoo Kim980ac162016-10-07 16:58:27 -0700115static inline struct page_ext *get_entry(void *base, unsigned long index)
116{
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700117 return base + page_ext_size * index;
Joonsoo Kim980ac162016-10-07 16:58:27 -0700118}
119
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800120#if !defined(CONFIG_SPARSEMEM)
121
122
123void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
124{
125 pgdat->node_page_ext = NULL;
126}
127
Kirill A. Shutemov10ed6342018-08-17 15:45:15 -0700128struct page_ext *lookup_page_ext(const struct page *page)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800129{
130 unsigned long pfn = page_to_pfn(page);
Joonsoo Kim0b06bb32016-10-07 16:58:24 -0700131 unsigned long index;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800132 struct page_ext *base;
133
134 base = NODE_DATA(page_to_nid(page))->node_page_ext;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800135 /*
136 * The sanity checks the page allocator does upon freeing a
137 * page can reach here before the page_ext arrays are
138 * allocated when feeding a range of pages to the allocator
139 * for the first time during bootup or memory hotplug.
140 */
141 if (unlikely(!base))
142 return NULL;
Joonsoo Kim0b06bb32016-10-07 16:58:24 -0700143 index = pfn - round_down(node_start_pfn(page_to_nid(page)),
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800144 MAX_ORDER_NR_PAGES);
Joonsoo Kim980ac162016-10-07 16:58:27 -0700145 return get_entry(base, index);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800146}
Vijayanand Jitta0a7166a2021-01-05 11:27:28 +0530147EXPORT_SYMBOL_GPL(lookup_page_ext);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800148
149static int __init alloc_node_page_ext(int nid)
150{
151 struct page_ext *base;
152 unsigned long table_size;
153 unsigned long nr_pages;
154
155 nr_pages = NODE_DATA(nid)->node_spanned_pages;
156 if (!nr_pages)
157 return 0;
158
159 /*
160 * Need extra space if node range is not aligned with
161 * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm
162 * checks buddy's status, range could be out of exact node range.
163 */
164 if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) ||
165 !IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES))
166 nr_pages += MAX_ORDER_NR_PAGES;
167
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700168 table_size = page_ext_size * nr_pages;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800169
Mike Rapoport26fb3da2019-03-11 23:30:42 -0700170 base = memblock_alloc_try_nid(
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800171 table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
Mike Rapoport97ad1082018-10-30 15:09:44 -0700172 MEMBLOCK_ALLOC_ACCESSIBLE, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800173 if (!base)
174 return -ENOMEM;
175 NODE_DATA(nid)->node_page_ext = base;
176 total_usage += table_size;
177 return 0;
178}
179
180void __init page_ext_init_flatmem(void)
181{
182
183 int nid, fail;
184
185 if (!invoke_need_callbacks())
186 return;
187
188 for_each_online_node(nid) {
189 fail = alloc_node_page_ext(nid);
190 if (fail)
191 goto fail;
192 }
193 pr_info("allocated %ld bytes of page_ext\n", total_usage);
194 invoke_init_callbacks();
195 return;
196
197fail:
198 pr_crit("allocation of page_ext failed.\n");
199 panic("Out of memory");
200}
201
202#else /* CONFIG_FLAT_NODE_MEM_MAP */
203
Kirill A. Shutemov10ed6342018-08-17 15:45:15 -0700204struct page_ext *lookup_page_ext(const struct page *page)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800205{
206 unsigned long pfn = page_to_pfn(page);
207 struct mem_section *section = __pfn_to_section(pfn);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800208 /*
209 * The sanity checks the page allocator does upon freeing a
210 * page can reach here before the page_ext arrays are
211 * allocated when feeding a range of pages to the allocator
212 * for the first time during bootup or memory hotplug.
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800213 */
214 if (!section->page_ext)
215 return NULL;
Joonsoo Kim980ac162016-10-07 16:58:27 -0700216 return get_entry(section->page_ext, pfn);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800217}
Vijayanand Jitta0a7166a2021-01-05 11:27:28 +0530218EXPORT_SYMBOL_GPL(lookup_page_ext);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800219
220static void *__meminit alloc_page_ext(size_t size, int nid)
221{
222 gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
223 void *addr = NULL;
224
225 addr = alloc_pages_exact_nid(nid, size, flags);
226 if (addr) {
227 kmemleak_alloc(addr, size, 1, flags);
228 return addr;
229 }
230
Michal Hockob95046b2017-09-06 16:20:41 -0700231 addr = vzalloc_node(size, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800232
233 return addr;
234}
235
236static int __meminit init_section_page_ext(unsigned long pfn, int nid)
237{
238 struct mem_section *section;
239 struct page_ext *base;
240 unsigned long table_size;
241
242 section = __pfn_to_section(pfn);
243
244 if (section->page_ext)
245 return 0;
246
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700247 table_size = page_ext_size * PAGES_PER_SECTION;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800248 base = alloc_page_ext(table_size, nid);
249
250 /*
251 * The value stored in section->page_ext is (base - pfn)
252 * and it does not point to the memory block allocated above,
253 * causing kmemleak false positives.
254 */
255 kmemleak_not_leak(base);
256
257 if (!base) {
258 pr_err("page ext allocation failure\n");
259 return -ENOMEM;
260 }
261
262 /*
263 * The passed "pfn" may not be aligned to SECTION. For the calculation
264 * we need to apply a mask.
265 */
266 pfn &= PAGE_SECTION_MASK;
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700267 section->page_ext = (void *)base - page_ext_size * pfn;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800268 total_usage += table_size;
269 return 0;
270}
271#ifdef CONFIG_MEMORY_HOTPLUG
272static void free_page_ext(void *addr)
273{
274 if (is_vmalloc_addr(addr)) {
275 vfree(addr);
276 } else {
277 struct page *page = virt_to_page(addr);
278 size_t table_size;
279
Vlastimil Babka5556cfe2019-10-14 14:11:40 -0700280 table_size = page_ext_size * PAGES_PER_SECTION;
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800281
282 BUG_ON(PageReserved(page));
Qian Cai0c815852019-03-05 15:49:46 -0800283 kmemleak_free(addr);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800284 free_pages_exact(addr, table_size);
285 }
286}
287
288static void __free_page_ext(unsigned long pfn)
289{
290 struct mem_section *ms;
291 struct page_ext *base;
292
293 ms = __pfn_to_section(pfn);
294 if (!ms || !ms->page_ext)
295 return;
Joonsoo Kim980ac162016-10-07 16:58:27 -0700296 base = get_entry(ms->page_ext, pfn);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800297 free_page_ext(base);
298 ms->page_ext = NULL;
299}
300
301static int __meminit online_page_ext(unsigned long start_pfn,
302 unsigned long nr_pages,
303 int nid)
304{
305 unsigned long start, end, pfn;
306 int fail = 0;
307
308 start = SECTION_ALIGN_DOWN(start_pfn);
309 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
310
Anshuman Khandual98fa15f2019-03-05 15:42:58 -0800311 if (nid == NUMA_NO_NODE) {
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800312 /*
313 * In this case, "nid" already exists and contains valid memory.
314 * "start_pfn" passed to us is a pfn which is an arg for
315 * online__pages(), and start_pfn should exist.
316 */
317 nid = pfn_to_nid(start_pfn);
318 VM_BUG_ON(!node_state(nid, N_ONLINE));
319 }
320
David Hildenbranddccacf82020-04-06 20:06:47 -0700321 for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800322 fail = init_section_page_ext(pfn, nid);
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800323 if (!fail)
324 return 0;
325
326 /* rollback */
327 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
328 __free_page_ext(pfn);
329
330 return -ENOMEM;
331}
332
333static int __meminit offline_page_ext(unsigned long start_pfn,
334 unsigned long nr_pages, int nid)
335{
336 unsigned long start, end, pfn;
337
338 start = SECTION_ALIGN_DOWN(start_pfn);
339 end = SECTION_ALIGN_UP(start_pfn + nr_pages);
340
341 for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
342 __free_page_ext(pfn);
343 return 0;
344
345}
346
347static int __meminit page_ext_callback(struct notifier_block *self,
348 unsigned long action, void *arg)
349{
350 struct memory_notify *mn = arg;
351 int ret = 0;
352
353 switch (action) {
354 case MEM_GOING_ONLINE:
355 ret = online_page_ext(mn->start_pfn,
356 mn->nr_pages, mn->status_change_nid);
357 break;
358 case MEM_OFFLINE:
359 offline_page_ext(mn->start_pfn,
360 mn->nr_pages, mn->status_change_nid);
361 break;
362 case MEM_CANCEL_ONLINE:
363 offline_page_ext(mn->start_pfn,
364 mn->nr_pages, mn->status_change_nid);
365 break;
366 case MEM_GOING_OFFLINE:
367 break;
368 case MEM_ONLINE:
369 case MEM_CANCEL_OFFLINE:
370 break;
371 }
372
373 return notifier_from_errno(ret);
374}
375
376#endif
377
378void __init page_ext_init(void)
379{
380 unsigned long pfn;
381 int nid;
382
383 if (!invoke_need_callbacks())
384 return;
385
386 for_each_node_state(nid, N_MEMORY) {
387 unsigned long start_pfn, end_pfn;
388
389 start_pfn = node_start_pfn(nid);
390 end_pfn = node_end_pfn(nid);
391 /*
392 * start_pfn and end_pfn may not be aligned to SECTION and the
393 * page->flags of out of node pages are not initialized. So we
394 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
395 */
396 for (pfn = start_pfn; pfn < end_pfn;
397 pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
398
399 if (!pfn_valid(pfn))
400 continue;
401 /*
402 * Nodes's pfns can be overlapping.
403 * We know some arch can have a nodes layout such as
404 * -------------pfn-------------->
405 * N0 | N1 | N2 | N0 | N1 | N2|....
406 */
Qian Cai2f1ee092019-02-12 15:36:03 -0800407 if (pfn_to_nid(pfn) != nid)
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800408 continue;
409 if (init_section_page_ext(pfn, nid))
410 goto oom;
Vlastimil Babka0fc542b2017-09-06 16:20:48 -0700411 cond_resched();
Joonsoo Kimeefa864b2014-12-12 16:55:46 -0800412 }
413 }
414 hotplug_memory_notifier(page_ext_callback, 0);
415 pr_info("allocated %ld bytes of page_ext\n", total_usage);
416 invoke_init_callbacks();
417 return;
418
419oom:
420 panic("Out of memory");
421}
422
423void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
424{
425}
426
427#endif