18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci#include <linux/mm.h>
38c2ecf20Sopenharmony_ci#include <linux/mmzone.h>
48c2ecf20Sopenharmony_ci#include <linux/memblock.h>
58c2ecf20Sopenharmony_ci#include <linux/page_ext.h>
68c2ecf20Sopenharmony_ci#include <linux/memory.h>
78c2ecf20Sopenharmony_ci#include <linux/vmalloc.h>
88c2ecf20Sopenharmony_ci#include <linux/kmemleak.h>
98c2ecf20Sopenharmony_ci#include <linux/page_owner.h>
108c2ecf20Sopenharmony_ci#include <linux/page_idle.h>
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci/*
138c2ecf20Sopenharmony_ci * struct page extension
148c2ecf20Sopenharmony_ci *
158c2ecf20Sopenharmony_ci * This is the feature to manage memory for extended data per page.
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * Until now, we must modify struct page itself to store extra data per page.
188c2ecf20Sopenharmony_ci * This requires rebuilding the kernel and it is really time consuming process.
198c2ecf20Sopenharmony_ci * And, sometimes, rebuild is impossible due to third party module dependency.
208c2ecf20Sopenharmony_ci * At last, enlarging struct page could cause un-wanted system behaviour change.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * This feature is intended to overcome above mentioned problems. This feature
238c2ecf20Sopenharmony_ci * allocates memory for extended data per page in certain place rather than
248c2ecf20Sopenharmony_ci * the struct page itself. This memory can be accessed by the accessor
258c2ecf20Sopenharmony_ci * functions provided by this code. During the boot process, it checks whether
268c2ecf20Sopenharmony_ci * allocation of huge chunk of memory is needed or not. If not, it avoids
278c2ecf20Sopenharmony_ci * allocating memory at all. With this advantage, we can include this feature
288c2ecf20Sopenharmony_ci * into the kernel in default and can avoid rebuild and solve related problems.
298c2ecf20Sopenharmony_ci *
308c2ecf20Sopenharmony_ci * To help these things to work well, there are two callbacks for clients. One
318c2ecf20Sopenharmony_ci * is the need callback which is mandatory if user wants to avoid useless
328c2ecf20Sopenharmony_ci * memory allocation at boot-time. The other is optional, init callback, which
338c2ecf20Sopenharmony_ci * is used to do proper initialization after memory is allocated.
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci * The need callback is used to decide whether extended memory allocation is
368c2ecf20Sopenharmony_ci * needed or not. Sometimes users want to deactivate some features in this
378c2ecf20Sopenharmony_ci * boot and extra memory would be unneccessary. In this case, to avoid
388c2ecf20Sopenharmony_ci * allocating huge chunk of memory, each clients represent their need of
398c2ecf20Sopenharmony_ci * extra memory through the need callback. If one of the need callbacks
408c2ecf20Sopenharmony_ci * returns true, it means that someone needs extra memory so that
418c2ecf20Sopenharmony_ci * page extension core should allocates memory for page extension. If
428c2ecf20Sopenharmony_ci * none of need callbacks return true, memory isn't needed at all in this boot
438c2ecf20Sopenharmony_ci * and page extension core can skip to allocate memory. As result,
448c2ecf20Sopenharmony_ci * none of memory is wasted.
458c2ecf20Sopenharmony_ci *
468c2ecf20Sopenharmony_ci * When need callback returns true, page_ext checks if there is a request for
478c2ecf20Sopenharmony_ci * extra memory through size in struct page_ext_operations. If it is non-zero,
488c2ecf20Sopenharmony_ci * extra space is allocated for each page_ext entry and offset is returned to
498c2ecf20Sopenharmony_ci * user through offset in struct page_ext_operations.
508c2ecf20Sopenharmony_ci *
518c2ecf20Sopenharmony_ci * The init callback is used to do proper initialization after page extension
528c2ecf20Sopenharmony_ci * is completely initialized. In sparse memory system, extra memory is
538c2ecf20Sopenharmony_ci * allocated some time later than memmap is allocated. In other words, lifetime
548c2ecf20Sopenharmony_ci * of memory for page extension isn't same with memmap for struct page.
558c2ecf20Sopenharmony_ci * Therefore, clients can't store extra data until page extension is
568c2ecf20Sopenharmony_ci * initialized, even if pages are allocated and used freely. This could
578c2ecf20Sopenharmony_ci * cause inadequate state of extra data per page, so, to prevent it, client
588c2ecf20Sopenharmony_ci * can utilize this callback to initialize the state of it correctly.
598c2ecf20Sopenharmony_ci */
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_cistatic struct page_ext_operations *page_ext_ops[] = {
628c2ecf20Sopenharmony_ci#ifdef CONFIG_PAGE_OWNER
638c2ecf20Sopenharmony_ci	&page_owner_ops,
648c2ecf20Sopenharmony_ci#endif
658c2ecf20Sopenharmony_ci#if defined(CONFIG_IDLE_PAGE_TRACKING) && !defined(CONFIG_64BIT)
668c2ecf20Sopenharmony_ci	&page_idle_ops,
678c2ecf20Sopenharmony_ci#endif
688c2ecf20Sopenharmony_ci};
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ciunsigned long page_ext_size = sizeof(struct page_ext);
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_cistatic unsigned long total_usage;
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic bool __init invoke_need_callbacks(void)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	int i;
778c2ecf20Sopenharmony_ci	int entries = ARRAY_SIZE(page_ext_ops);
788c2ecf20Sopenharmony_ci	bool need = false;
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_ci	for (i = 0; i < entries; i++) {
818c2ecf20Sopenharmony_ci		if (page_ext_ops[i]->need && page_ext_ops[i]->need()) {
828c2ecf20Sopenharmony_ci			page_ext_ops[i]->offset = page_ext_size;
838c2ecf20Sopenharmony_ci			page_ext_size += page_ext_ops[i]->size;
848c2ecf20Sopenharmony_ci			need = true;
858c2ecf20Sopenharmony_ci		}
868c2ecf20Sopenharmony_ci	}
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci	return need;
898c2ecf20Sopenharmony_ci}
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_cistatic void __init invoke_init_callbacks(void)
928c2ecf20Sopenharmony_ci{
938c2ecf20Sopenharmony_ci	int i;
948c2ecf20Sopenharmony_ci	int entries = ARRAY_SIZE(page_ext_ops);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	for (i = 0; i < entries; i++) {
978c2ecf20Sopenharmony_ci		if (page_ext_ops[i]->init)
988c2ecf20Sopenharmony_ci			page_ext_ops[i]->init();
998c2ecf20Sopenharmony_ci	}
1008c2ecf20Sopenharmony_ci}
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_ci#ifndef CONFIG_SPARSEMEM
1038c2ecf20Sopenharmony_civoid __init page_ext_init_flatmem_late(void)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	invoke_init_callbacks();
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci#endif
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistatic inline struct page_ext *get_entry(void *base, unsigned long index)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	return base + page_ext_size * index;
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_ci#ifndef CONFIG_SPARSEMEM
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_civoid __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
1188c2ecf20Sopenharmony_ci{
1198c2ecf20Sopenharmony_ci	pgdat->node_page_ext = NULL;
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_cistruct page_ext *lookup_page_ext(const struct page *page)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	unsigned long pfn = page_to_pfn(page);
1258c2ecf20Sopenharmony_ci	unsigned long index;
1268c2ecf20Sopenharmony_ci	struct page_ext *base;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	base = NODE_DATA(page_to_nid(page))->node_page_ext;
1298c2ecf20Sopenharmony_ci	/*
1308c2ecf20Sopenharmony_ci	 * The sanity checks the page allocator does upon freeing a
1318c2ecf20Sopenharmony_ci	 * page can reach here before the page_ext arrays are
1328c2ecf20Sopenharmony_ci	 * allocated when feeding a range of pages to the allocator
1338c2ecf20Sopenharmony_ci	 * for the first time during bootup or memory hotplug.
1348c2ecf20Sopenharmony_ci	 */
1358c2ecf20Sopenharmony_ci	if (unlikely(!base))
1368c2ecf20Sopenharmony_ci		return NULL;
1378c2ecf20Sopenharmony_ci	index = pfn - round_down(node_start_pfn(page_to_nid(page)),
1388c2ecf20Sopenharmony_ci					MAX_ORDER_NR_PAGES);
1398c2ecf20Sopenharmony_ci	return get_entry(base, index);
1408c2ecf20Sopenharmony_ci}
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_cistatic int __init alloc_node_page_ext(int nid)
1438c2ecf20Sopenharmony_ci{
1448c2ecf20Sopenharmony_ci	struct page_ext *base;
1458c2ecf20Sopenharmony_ci	unsigned long table_size;
1468c2ecf20Sopenharmony_ci	unsigned long nr_pages;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	nr_pages = NODE_DATA(nid)->node_spanned_pages;
1498c2ecf20Sopenharmony_ci	if (!nr_pages)
1508c2ecf20Sopenharmony_ci		return 0;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	/*
1538c2ecf20Sopenharmony_ci	 * Need extra space if node range is not aligned with
1548c2ecf20Sopenharmony_ci	 * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm
1558c2ecf20Sopenharmony_ci	 * checks buddy's status, range could be out of exact node range.
1568c2ecf20Sopenharmony_ci	 */
1578c2ecf20Sopenharmony_ci	if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) ||
1588c2ecf20Sopenharmony_ci		!IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES))
1598c2ecf20Sopenharmony_ci		nr_pages += MAX_ORDER_NR_PAGES;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	table_size = page_ext_size * nr_pages;
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	base = memblock_alloc_try_nid(
1648c2ecf20Sopenharmony_ci			table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
1658c2ecf20Sopenharmony_ci			MEMBLOCK_ALLOC_ACCESSIBLE, nid);
1668c2ecf20Sopenharmony_ci	if (!base)
1678c2ecf20Sopenharmony_ci		return -ENOMEM;
1688c2ecf20Sopenharmony_ci	NODE_DATA(nid)->node_page_ext = base;
1698c2ecf20Sopenharmony_ci	total_usage += table_size;
1708c2ecf20Sopenharmony_ci	return 0;
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_civoid __init page_ext_init_flatmem(void)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	int nid, fail;
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	if (!invoke_need_callbacks())
1798c2ecf20Sopenharmony_ci		return;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	for_each_online_node(nid)  {
1828c2ecf20Sopenharmony_ci		fail = alloc_node_page_ext(nid);
1838c2ecf20Sopenharmony_ci		if (fail)
1848c2ecf20Sopenharmony_ci			goto fail;
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci	pr_info("allocated %ld bytes of page_ext\n", total_usage);
1878c2ecf20Sopenharmony_ci	return;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_cifail:
1908c2ecf20Sopenharmony_ci	pr_crit("allocation of page_ext failed.\n");
1918c2ecf20Sopenharmony_ci	panic("Out of memory");
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci#else /* CONFIG_FLAT_NODE_MEM_MAP */
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_cistruct page_ext *lookup_page_ext(const struct page *page)
1978c2ecf20Sopenharmony_ci{
1988c2ecf20Sopenharmony_ci	unsigned long pfn = page_to_pfn(page);
1998c2ecf20Sopenharmony_ci	struct mem_section *section = __pfn_to_section(pfn);
2008c2ecf20Sopenharmony_ci	/*
2018c2ecf20Sopenharmony_ci	 * The sanity checks the page allocator does upon freeing a
2028c2ecf20Sopenharmony_ci	 * page can reach here before the page_ext arrays are
2038c2ecf20Sopenharmony_ci	 * allocated when feeding a range of pages to the allocator
2048c2ecf20Sopenharmony_ci	 * for the first time during bootup or memory hotplug.
2058c2ecf20Sopenharmony_ci	 */
2068c2ecf20Sopenharmony_ci	if (!section->page_ext)
2078c2ecf20Sopenharmony_ci		return NULL;
2088c2ecf20Sopenharmony_ci	return get_entry(section->page_ext, pfn);
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic void *__meminit alloc_page_ext(size_t size, int nid)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
2148c2ecf20Sopenharmony_ci	void *addr = NULL;
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	addr = alloc_pages_exact_nid(nid, size, flags);
2178c2ecf20Sopenharmony_ci	if (addr) {
2188c2ecf20Sopenharmony_ci		kmemleak_alloc(addr, size, 1, flags);
2198c2ecf20Sopenharmony_ci		return addr;
2208c2ecf20Sopenharmony_ci	}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	addr = vzalloc_node(size, nid);
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_ci	return addr;
2258c2ecf20Sopenharmony_ci}
2268c2ecf20Sopenharmony_ci
2278c2ecf20Sopenharmony_cistatic int __meminit init_section_page_ext(unsigned long pfn, int nid)
2288c2ecf20Sopenharmony_ci{
2298c2ecf20Sopenharmony_ci	struct mem_section *section;
2308c2ecf20Sopenharmony_ci	struct page_ext *base;
2318c2ecf20Sopenharmony_ci	unsigned long table_size;
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	section = __pfn_to_section(pfn);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	if (section->page_ext)
2368c2ecf20Sopenharmony_ci		return 0;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	table_size = page_ext_size * PAGES_PER_SECTION;
2398c2ecf20Sopenharmony_ci	base = alloc_page_ext(table_size, nid);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ci	/*
2428c2ecf20Sopenharmony_ci	 * The value stored in section->page_ext is (base - pfn)
2438c2ecf20Sopenharmony_ci	 * and it does not point to the memory block allocated above,
2448c2ecf20Sopenharmony_ci	 * causing kmemleak false positives.
2458c2ecf20Sopenharmony_ci	 */
2468c2ecf20Sopenharmony_ci	kmemleak_not_leak(base);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	if (!base) {
2498c2ecf20Sopenharmony_ci		pr_err("page ext allocation failure\n");
2508c2ecf20Sopenharmony_ci		return -ENOMEM;
2518c2ecf20Sopenharmony_ci	}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	/*
2548c2ecf20Sopenharmony_ci	 * The passed "pfn" may not be aligned to SECTION.  For the calculation
2558c2ecf20Sopenharmony_ci	 * we need to apply a mask.
2568c2ecf20Sopenharmony_ci	 */
2578c2ecf20Sopenharmony_ci	pfn &= PAGE_SECTION_MASK;
2588c2ecf20Sopenharmony_ci	section->page_ext = (void *)base - page_ext_size * pfn;
2598c2ecf20Sopenharmony_ci	total_usage += table_size;
2608c2ecf20Sopenharmony_ci	return 0;
2618c2ecf20Sopenharmony_ci}
2628c2ecf20Sopenharmony_ci#ifdef CONFIG_MEMORY_HOTPLUG
2638c2ecf20Sopenharmony_cistatic void free_page_ext(void *addr)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	if (is_vmalloc_addr(addr)) {
2668c2ecf20Sopenharmony_ci		vfree(addr);
2678c2ecf20Sopenharmony_ci	} else {
2688c2ecf20Sopenharmony_ci		struct page *page = virt_to_page(addr);
2698c2ecf20Sopenharmony_ci		size_t table_size;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		table_size = page_ext_size * PAGES_PER_SECTION;
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci		BUG_ON(PageReserved(page));
2748c2ecf20Sopenharmony_ci		kmemleak_free(addr);
2758c2ecf20Sopenharmony_ci		free_pages_exact(addr, table_size);
2768c2ecf20Sopenharmony_ci	}
2778c2ecf20Sopenharmony_ci}
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_cistatic void __free_page_ext(unsigned long pfn)
2808c2ecf20Sopenharmony_ci{
2818c2ecf20Sopenharmony_ci	struct mem_section *ms;
2828c2ecf20Sopenharmony_ci	struct page_ext *base;
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	ms = __pfn_to_section(pfn);
2858c2ecf20Sopenharmony_ci	if (!ms || !ms->page_ext)
2868c2ecf20Sopenharmony_ci		return;
2878c2ecf20Sopenharmony_ci	base = get_entry(ms->page_ext, pfn);
2888c2ecf20Sopenharmony_ci	free_page_ext(base);
2898c2ecf20Sopenharmony_ci	ms->page_ext = NULL;
2908c2ecf20Sopenharmony_ci}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic int __meminit online_page_ext(unsigned long start_pfn,
2938c2ecf20Sopenharmony_ci				unsigned long nr_pages,
2948c2ecf20Sopenharmony_ci				int nid)
2958c2ecf20Sopenharmony_ci{
2968c2ecf20Sopenharmony_ci	unsigned long start, end, pfn;
2978c2ecf20Sopenharmony_ci	int fail = 0;
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	start = SECTION_ALIGN_DOWN(start_pfn);
3008c2ecf20Sopenharmony_ci	end = SECTION_ALIGN_UP(start_pfn + nr_pages);
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	if (nid == NUMA_NO_NODE) {
3038c2ecf20Sopenharmony_ci		/*
3048c2ecf20Sopenharmony_ci		 * In this case, "nid" already exists and contains valid memory.
3058c2ecf20Sopenharmony_ci		 * "start_pfn" passed to us is a pfn which is an arg for
3068c2ecf20Sopenharmony_ci		 * online__pages(), and start_pfn should exist.
3078c2ecf20Sopenharmony_ci		 */
3088c2ecf20Sopenharmony_ci		nid = pfn_to_nid(start_pfn);
3098c2ecf20Sopenharmony_ci		VM_BUG_ON(!node_state(nid, N_ONLINE));
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_ci	for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION)
3138c2ecf20Sopenharmony_ci		fail = init_section_page_ext(pfn, nid);
3148c2ecf20Sopenharmony_ci	if (!fail)
3158c2ecf20Sopenharmony_ci		return 0;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	/* rollback */
3188c2ecf20Sopenharmony_ci	for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
3198c2ecf20Sopenharmony_ci		__free_page_ext(pfn);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	return -ENOMEM;
3228c2ecf20Sopenharmony_ci}
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_cistatic int __meminit offline_page_ext(unsigned long start_pfn,
3258c2ecf20Sopenharmony_ci				unsigned long nr_pages, int nid)
3268c2ecf20Sopenharmony_ci{
3278c2ecf20Sopenharmony_ci	unsigned long start, end, pfn;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	start = SECTION_ALIGN_DOWN(start_pfn);
3308c2ecf20Sopenharmony_ci	end = SECTION_ALIGN_UP(start_pfn + nr_pages);
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
3338c2ecf20Sopenharmony_ci		__free_page_ext(pfn);
3348c2ecf20Sopenharmony_ci	return 0;
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_ci}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_cistatic int __meminit page_ext_callback(struct notifier_block *self,
3398c2ecf20Sopenharmony_ci			       unsigned long action, void *arg)
3408c2ecf20Sopenharmony_ci{
3418c2ecf20Sopenharmony_ci	struct memory_notify *mn = arg;
3428c2ecf20Sopenharmony_ci	int ret = 0;
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	switch (action) {
3458c2ecf20Sopenharmony_ci	case MEM_GOING_ONLINE:
3468c2ecf20Sopenharmony_ci		ret = online_page_ext(mn->start_pfn,
3478c2ecf20Sopenharmony_ci				   mn->nr_pages, mn->status_change_nid);
3488c2ecf20Sopenharmony_ci		break;
3498c2ecf20Sopenharmony_ci	case MEM_OFFLINE:
3508c2ecf20Sopenharmony_ci		offline_page_ext(mn->start_pfn,
3518c2ecf20Sopenharmony_ci				mn->nr_pages, mn->status_change_nid);
3528c2ecf20Sopenharmony_ci		break;
3538c2ecf20Sopenharmony_ci	case MEM_CANCEL_ONLINE:
3548c2ecf20Sopenharmony_ci		offline_page_ext(mn->start_pfn,
3558c2ecf20Sopenharmony_ci				mn->nr_pages, mn->status_change_nid);
3568c2ecf20Sopenharmony_ci		break;
3578c2ecf20Sopenharmony_ci	case MEM_GOING_OFFLINE:
3588c2ecf20Sopenharmony_ci		break;
3598c2ecf20Sopenharmony_ci	case MEM_ONLINE:
3608c2ecf20Sopenharmony_ci	case MEM_CANCEL_OFFLINE:
3618c2ecf20Sopenharmony_ci		break;
3628c2ecf20Sopenharmony_ci	}
3638c2ecf20Sopenharmony_ci
3648c2ecf20Sopenharmony_ci	return notifier_from_errno(ret);
3658c2ecf20Sopenharmony_ci}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci#endif
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_civoid __init page_ext_init(void)
3708c2ecf20Sopenharmony_ci{
3718c2ecf20Sopenharmony_ci	unsigned long pfn;
3728c2ecf20Sopenharmony_ci	int nid;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	if (!invoke_need_callbacks())
3758c2ecf20Sopenharmony_ci		return;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	for_each_node_state(nid, N_MEMORY) {
3788c2ecf20Sopenharmony_ci		unsigned long start_pfn, end_pfn;
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci		start_pfn = node_start_pfn(nid);
3818c2ecf20Sopenharmony_ci		end_pfn = node_end_pfn(nid);
3828c2ecf20Sopenharmony_ci		/*
3838c2ecf20Sopenharmony_ci		 * start_pfn and end_pfn may not be aligned to SECTION and the
3848c2ecf20Sopenharmony_ci		 * page->flags of out of node pages are not initialized.  So we
3858c2ecf20Sopenharmony_ci		 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
3868c2ecf20Sopenharmony_ci		 */
3878c2ecf20Sopenharmony_ci		for (pfn = start_pfn; pfn < end_pfn;
3888c2ecf20Sopenharmony_ci			pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci			if (!pfn_valid(pfn))
3918c2ecf20Sopenharmony_ci				continue;
3928c2ecf20Sopenharmony_ci			/*
3938c2ecf20Sopenharmony_ci			 * Nodes's pfns can be overlapping.
3948c2ecf20Sopenharmony_ci			 * We know some arch can have a nodes layout such as
3958c2ecf20Sopenharmony_ci			 * -------------pfn-------------->
3968c2ecf20Sopenharmony_ci			 * N0 | N1 | N2 | N0 | N1 | N2|....
3978c2ecf20Sopenharmony_ci			 */
3988c2ecf20Sopenharmony_ci			if (pfn_to_nid(pfn) != nid)
3998c2ecf20Sopenharmony_ci				continue;
4008c2ecf20Sopenharmony_ci			if (init_section_page_ext(pfn, nid))
4018c2ecf20Sopenharmony_ci				goto oom;
4028c2ecf20Sopenharmony_ci			cond_resched();
4038c2ecf20Sopenharmony_ci		}
4048c2ecf20Sopenharmony_ci	}
4058c2ecf20Sopenharmony_ci	hotplug_memory_notifier(page_ext_callback, 0);
4068c2ecf20Sopenharmony_ci	pr_info("allocated %ld bytes of page_ext\n", total_usage);
4078c2ecf20Sopenharmony_ci	invoke_init_callbacks();
4088c2ecf20Sopenharmony_ci	return;
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cioom:
4118c2ecf20Sopenharmony_ci	panic("Out of memory");
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_civoid __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci}
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci#endif
419