xref: /linux/mm/page_ext.c (revision a1c613ae4c322ddd58d5a8539dbfba2a0380a8c0)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
2eefa864bSJoonsoo Kim #include <linux/mm.h>
3eefa864bSJoonsoo Kim #include <linux/mmzone.h>
457c8a661SMike Rapoport #include <linux/memblock.h>
5eefa864bSJoonsoo Kim #include <linux/page_ext.h>
6eefa864bSJoonsoo Kim #include <linux/memory.h>
7eefa864bSJoonsoo Kim #include <linux/vmalloc.h>
8eefa864bSJoonsoo Kim #include <linux/kmemleak.h>
948c96a36SJoonsoo Kim #include <linux/page_owner.h>
1033c3fc71SVladimir Davydov #include <linux/page_idle.h>
11df4e817bSPasha Tatashin #include <linux/page_table_check.h>
12b1d5488aSCharan Teja Kalla #include <linux/rcupdate.h>
13eefa864bSJoonsoo Kim 
14eefa864bSJoonsoo Kim /*
15eefa864bSJoonsoo Kim  * struct page extension
16eefa864bSJoonsoo Kim  *
17eefa864bSJoonsoo Kim  * This is the feature to manage memory for extended data per page.
18eefa864bSJoonsoo Kim  *
19eefa864bSJoonsoo Kim  * Until now, we must modify struct page itself to store extra data per page.
20eefa864bSJoonsoo Kim  * This requires rebuilding the kernel and it is really time consuming process.
21eefa864bSJoonsoo Kim  * And, sometimes, rebuild is impossible due to third party module dependency.
22eefa864bSJoonsoo Kim  * At last, enlarging struct page could cause un-wanted system behaviour change.
23eefa864bSJoonsoo Kim  *
24eefa864bSJoonsoo Kim  * This feature is intended to overcome above mentioned problems. This feature
25eefa864bSJoonsoo Kim  * allocates memory for extended data per page in certain place rather than
26eefa864bSJoonsoo Kim  * the struct page itself. This memory can be accessed by the accessor
27eefa864bSJoonsoo Kim  * functions provided by this code. During the boot process, it checks whether
28eefa864bSJoonsoo Kim  * allocation of huge chunk of memory is needed or not. If not, it avoids
29eefa864bSJoonsoo Kim  * allocating memory at all. With this advantage, we can include this feature
30eefa864bSJoonsoo Kim  * into the kernel in default and can avoid rebuild and solve related problems.
31eefa864bSJoonsoo Kim  *
32eefa864bSJoonsoo Kim  * To help these things to work well, there are two callbacks for clients. One
33eefa864bSJoonsoo Kim  * is the need callback which is mandatory if user wants to avoid useless
34eefa864bSJoonsoo Kim  * memory allocation at boot-time. The other is optional, init callback, which
35eefa864bSJoonsoo Kim  * is used to do proper initialization after memory is allocated.
36eefa864bSJoonsoo Kim  *
37eefa864bSJoonsoo Kim  * The need callback is used to decide whether extended memory allocation is
38eefa864bSJoonsoo Kim  * needed or not. Sometimes users want to deactivate some features in this
398958b249SHaitao Shi  * boot and extra memory would be unnecessary. In this case, to avoid
40eefa864bSJoonsoo Kim  * allocating huge chunk of memory, each clients represent their need of
41eefa864bSJoonsoo Kim  * extra memory through the need callback. If one of the need callbacks
42eefa864bSJoonsoo Kim  * returns true, it means that someone needs extra memory so that
43eefa864bSJoonsoo Kim  * page extension core should allocates memory for page extension. If
44eefa864bSJoonsoo Kim  * none of need callbacks return true, memory isn't needed at all in this boot
45eefa864bSJoonsoo Kim  * and page extension core can skip to allocate memory. As result,
46eefa864bSJoonsoo Kim  * none of memory is wasted.
47eefa864bSJoonsoo Kim  *
48980ac167SJoonsoo Kim  * When need callback returns true, page_ext checks if there is a request for
49980ac167SJoonsoo Kim  * extra memory through size in struct page_ext_operations. If it is non-zero,
50980ac167SJoonsoo Kim  * extra space is allocated for each page_ext entry and offset is returned to
51980ac167SJoonsoo Kim  * user through offset in struct page_ext_operations.
52980ac167SJoonsoo Kim  *
53eefa864bSJoonsoo Kim  * The init callback is used to do proper initialization after page extension
54eefa864bSJoonsoo Kim  * is completely initialized. In sparse memory system, extra memory is
55eefa864bSJoonsoo Kim  * allocated some time later than memmap is allocated. In other words, lifetime
56eefa864bSJoonsoo Kim  * of memory for page extension isn't same with memmap for struct page.
57eefa864bSJoonsoo Kim  * Therefore, clients can't store extra data until page extension is
58eefa864bSJoonsoo Kim  * initialized, even if pages are allocated and used freely. This could
59eefa864bSJoonsoo Kim  * cause inadequate state of extra data per page, so, to prevent it, client
60eefa864bSJoonsoo Kim  * can utilize this callback to initialize the state of it correctly.
61eefa864bSJoonsoo Kim  */
62eefa864bSJoonsoo Kim 
63b1d5488aSCharan Teja Kalla #ifdef CONFIG_SPARSEMEM
64b1d5488aSCharan Teja Kalla #define PAGE_EXT_INVALID       (0x1)
65b1d5488aSCharan Teja Kalla #endif
66b1d5488aSCharan Teja Kalla 
671c676e0dSSeongJae Park #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
need_page_idle(void)681c676e0dSSeongJae Park static bool need_page_idle(void)
691c676e0dSSeongJae Park {
701c676e0dSSeongJae Park 	return true;
711c676e0dSSeongJae Park }
72cab0a7c1STing Liu static struct page_ext_operations page_idle_ops __initdata = {
731c676e0dSSeongJae Park 	.need = need_page_idle,
746189eb82SPasha Tatashin 	.need_shared_flags = true,
751c676e0dSSeongJae Park };
761c676e0dSSeongJae Park #endif
771c676e0dSSeongJae Park 
78cab0a7c1STing Liu static struct page_ext_operations *page_ext_ops[] __initdata = {
7948c96a36SJoonsoo Kim #ifdef CONFIG_PAGE_OWNER
8048c96a36SJoonsoo Kim 	&page_owner_ops,
8148c96a36SJoonsoo Kim #endif
821c676e0dSSeongJae Park #if defined(CONFIG_PAGE_IDLE_FLAG) && !defined(CONFIG_64BIT)
8333c3fc71SVladimir Davydov 	&page_idle_ops,
8433c3fc71SVladimir Davydov #endif
85df4e817bSPasha Tatashin #ifdef CONFIG_PAGE_TABLE_CHECK
86df4e817bSPasha Tatashin 	&page_table_check_ops,
87df4e817bSPasha Tatashin #endif
88eefa864bSJoonsoo Kim };
89eefa864bSJoonsoo Kim 
906189eb82SPasha Tatashin unsigned long page_ext_size;
915556cfe8SVlastimil Babka 
92eefa864bSJoonsoo Kim static unsigned long total_usage;
93eefa864bSJoonsoo Kim 
947ec7096bSPasha Tatashin bool early_page_ext __meminitdata;
setup_early_page_ext(char * str)95c4f20f14SLi Zhe static int __init setup_early_page_ext(char *str)
96c4f20f14SLi Zhe {
97c4f20f14SLi Zhe 	early_page_ext = true;
98c4f20f14SLi Zhe 	return 0;
99c4f20f14SLi Zhe }
100c4f20f14SLi Zhe early_param("early_page_ext", setup_early_page_ext);
101c4f20f14SLi Zhe 
invoke_need_callbacks(void)102eefa864bSJoonsoo Kim static bool __init invoke_need_callbacks(void)
103eefa864bSJoonsoo Kim {
104eefa864bSJoonsoo Kim 	int i;
105eefa864bSJoonsoo Kim 	int entries = ARRAY_SIZE(page_ext_ops);
106980ac167SJoonsoo Kim 	bool need = false;
107eefa864bSJoonsoo Kim 
108eefa864bSJoonsoo Kim 	for (i = 0; i < entries; i++) {
1096189eb82SPasha Tatashin 		if (page_ext_ops[i]->need()) {
1106189eb82SPasha Tatashin 			if (page_ext_ops[i]->need_shared_flags) {
1116189eb82SPasha Tatashin 				page_ext_size = sizeof(struct page_ext);
1126189eb82SPasha Tatashin 				break;
1136189eb82SPasha Tatashin 			}
1146189eb82SPasha Tatashin 		}
1156189eb82SPasha Tatashin 	}
1166189eb82SPasha Tatashin 
1176189eb82SPasha Tatashin 	for (i = 0; i < entries; i++) {
1186189eb82SPasha Tatashin 		if (page_ext_ops[i]->need()) {
1195556cfe8SVlastimil Babka 			page_ext_ops[i]->offset = page_ext_size;
1205556cfe8SVlastimil Babka 			page_ext_size += page_ext_ops[i]->size;
121980ac167SJoonsoo Kim 			need = true;
122980ac167SJoonsoo Kim 		}
123eefa864bSJoonsoo Kim 	}
124eefa864bSJoonsoo Kim 
125980ac167SJoonsoo Kim 	return need;
126eefa864bSJoonsoo Kim }
127eefa864bSJoonsoo Kim 
invoke_init_callbacks(void)128eefa864bSJoonsoo Kim static void __init invoke_init_callbacks(void)
129eefa864bSJoonsoo Kim {
130eefa864bSJoonsoo Kim 	int i;
131eefa864bSJoonsoo Kim 	int entries = ARRAY_SIZE(page_ext_ops);
132eefa864bSJoonsoo Kim 
133eefa864bSJoonsoo Kim 	for (i = 0; i < entries; i++) {
134eefa864bSJoonsoo Kim 		if (page_ext_ops[i]->init)
135eefa864bSJoonsoo Kim 			page_ext_ops[i]->init();
136eefa864bSJoonsoo Kim 	}
137eefa864bSJoonsoo Kim }
138eefa864bSJoonsoo Kim 
get_entry(void * base,unsigned long index)139980ac167SJoonsoo Kim static inline struct page_ext *get_entry(void *base, unsigned long index)
140980ac167SJoonsoo Kim {
1415556cfe8SVlastimil Babka 	return base + page_ext_size * index;
142980ac167SJoonsoo Kim }
143980ac167SJoonsoo Kim 
1447fb7ab6dSZhenhua Huang #ifndef CONFIG_SPARSEMEM
page_ext_init_flatmem_late(void)145*eb0da7f6SKemeng Shi void __init page_ext_init_flatmem_late(void)
146*eb0da7f6SKemeng Shi {
147*eb0da7f6SKemeng Shi 	invoke_init_callbacks();
148*eb0da7f6SKemeng Shi }
149eefa864bSJoonsoo Kim 
pgdat_page_ext_init(struct pglist_data * pgdat)150eefa864bSJoonsoo Kim void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
151eefa864bSJoonsoo Kim {
152eefa864bSJoonsoo Kim 	pgdat->node_page_ext = NULL;
153eefa864bSJoonsoo Kim }
154eefa864bSJoonsoo Kim 
lookup_page_ext(const struct page * page)155b1d5488aSCharan Teja Kalla static struct page_ext *lookup_page_ext(const struct page *page)
156eefa864bSJoonsoo Kim {
157eefa864bSJoonsoo Kim 	unsigned long pfn = page_to_pfn(page);
1580b06bb3fSJoonsoo Kim 	unsigned long index;
159eefa864bSJoonsoo Kim 	struct page_ext *base;
160eefa864bSJoonsoo Kim 
161b1d5488aSCharan Teja Kalla 	WARN_ON_ONCE(!rcu_read_lock_held());
162eefa864bSJoonsoo Kim 	base = NODE_DATA(page_to_nid(page))->node_page_ext;
163eefa864bSJoonsoo Kim 	/*
164eefa864bSJoonsoo Kim 	 * The sanity checks the page allocator does upon freeing a
165eefa864bSJoonsoo Kim 	 * page can reach here before the page_ext arrays are
166eefa864bSJoonsoo Kim 	 * allocated when feeding a range of pages to the allocator
167eefa864bSJoonsoo Kim 	 * for the first time during bootup or memory hotplug.
168eefa864bSJoonsoo Kim 	 */
169eefa864bSJoonsoo Kim 	if (unlikely(!base))
170eefa864bSJoonsoo Kim 		return NULL;
1710b06bb3fSJoonsoo Kim 	index = pfn - round_down(node_start_pfn(page_to_nid(page)),
172eefa864bSJoonsoo Kim 					MAX_ORDER_NR_PAGES);
173980ac167SJoonsoo Kim 	return get_entry(base, index);
174eefa864bSJoonsoo Kim }
175eefa864bSJoonsoo Kim 
alloc_node_page_ext(int nid)176eefa864bSJoonsoo Kim static int __init alloc_node_page_ext(int nid)
177eefa864bSJoonsoo Kim {
178eefa864bSJoonsoo Kim 	struct page_ext *base;
179eefa864bSJoonsoo Kim 	unsigned long table_size;
180eefa864bSJoonsoo Kim 	unsigned long nr_pages;
181eefa864bSJoonsoo Kim 
182eefa864bSJoonsoo Kim 	nr_pages = NODE_DATA(nid)->node_spanned_pages;
183eefa864bSJoonsoo Kim 	if (!nr_pages)
184eefa864bSJoonsoo Kim 		return 0;
185eefa864bSJoonsoo Kim 
186eefa864bSJoonsoo Kim 	/*
187eefa864bSJoonsoo Kim 	 * Need extra space if node range is not aligned with
188eefa864bSJoonsoo Kim 	 * MAX_ORDER_NR_PAGES. When page allocator's buddy algorithm
189eefa864bSJoonsoo Kim 	 * checks buddy's status, range could be out of exact node range.
190eefa864bSJoonsoo Kim 	 */
191eefa864bSJoonsoo Kim 	if (!IS_ALIGNED(node_start_pfn(nid), MAX_ORDER_NR_PAGES) ||
192eefa864bSJoonsoo Kim 		!IS_ALIGNED(node_end_pfn(nid), MAX_ORDER_NR_PAGES))
193eefa864bSJoonsoo Kim 		nr_pages += MAX_ORDER_NR_PAGES;
194eefa864bSJoonsoo Kim 
1955556cfe8SVlastimil Babka 	table_size = page_ext_size * nr_pages;
196eefa864bSJoonsoo Kim 
19726fb3daeSMike Rapoport 	base = memblock_alloc_try_nid(
198eefa864bSJoonsoo Kim 			table_size, PAGE_SIZE, __pa(MAX_DMA_ADDRESS),
19997ad1087SMike Rapoport 			MEMBLOCK_ALLOC_ACCESSIBLE, nid);
200eefa864bSJoonsoo Kim 	if (!base)
201eefa864bSJoonsoo Kim 		return -ENOMEM;
202eefa864bSJoonsoo Kim 	NODE_DATA(nid)->node_page_ext = base;
203eefa864bSJoonsoo Kim 	total_usage += table_size;
204eefa864bSJoonsoo Kim 	return 0;
205eefa864bSJoonsoo Kim }
206eefa864bSJoonsoo Kim 
page_ext_init_flatmem(void)207eefa864bSJoonsoo Kim void __init page_ext_init_flatmem(void)
208eefa864bSJoonsoo Kim {
209eefa864bSJoonsoo Kim 
210eefa864bSJoonsoo Kim 	int nid, fail;
211eefa864bSJoonsoo Kim 
212eefa864bSJoonsoo Kim 	if (!invoke_need_callbacks())
213eefa864bSJoonsoo Kim 		return;
214eefa864bSJoonsoo Kim 
215eefa864bSJoonsoo Kim 	for_each_online_node(nid)  {
216eefa864bSJoonsoo Kim 		fail = alloc_node_page_ext(nid);
217eefa864bSJoonsoo Kim 		if (fail)
218eefa864bSJoonsoo Kim 			goto fail;
219eefa864bSJoonsoo Kim 	}
220eefa864bSJoonsoo Kim 	pr_info("allocated %ld bytes of page_ext\n", total_usage);
221eefa864bSJoonsoo Kim 	return;
222eefa864bSJoonsoo Kim 
223eefa864bSJoonsoo Kim fail:
224eefa864bSJoonsoo Kim 	pr_crit("allocation of page_ext failed.\n");
225eefa864bSJoonsoo Kim 	panic("Out of memory");
226eefa864bSJoonsoo Kim }
227eefa864bSJoonsoo Kim 
228d1fea155SYinan Zhang #else /* CONFIG_SPARSEMEM */
page_ext_invalid(struct page_ext * page_ext)229b1d5488aSCharan Teja Kalla static bool page_ext_invalid(struct page_ext *page_ext)
230b1d5488aSCharan Teja Kalla {
231b1d5488aSCharan Teja Kalla 	return !page_ext || (((unsigned long)page_ext & PAGE_EXT_INVALID) == PAGE_EXT_INVALID);
232b1d5488aSCharan Teja Kalla }
233eefa864bSJoonsoo Kim 
lookup_page_ext(const struct page * page)234b1d5488aSCharan Teja Kalla static struct page_ext *lookup_page_ext(const struct page *page)
235eefa864bSJoonsoo Kim {
236eefa864bSJoonsoo Kim 	unsigned long pfn = page_to_pfn(page);
237eefa864bSJoonsoo Kim 	struct mem_section *section = __pfn_to_section(pfn);
238b1d5488aSCharan Teja Kalla 	struct page_ext *page_ext = READ_ONCE(section->page_ext);
239b1d5488aSCharan Teja Kalla 
240b1d5488aSCharan Teja Kalla 	WARN_ON_ONCE(!rcu_read_lock_held());
241eefa864bSJoonsoo Kim 	/*
242eefa864bSJoonsoo Kim 	 * The sanity checks the page allocator does upon freeing a
243eefa864bSJoonsoo Kim 	 * page can reach here before the page_ext arrays are
244eefa864bSJoonsoo Kim 	 * allocated when feeding a range of pages to the allocator
245eefa864bSJoonsoo Kim 	 * for the first time during bootup or memory hotplug.
246eefa864bSJoonsoo Kim 	 */
247b1d5488aSCharan Teja Kalla 	if (page_ext_invalid(page_ext))
248eefa864bSJoonsoo Kim 		return NULL;
249b1d5488aSCharan Teja Kalla 	return get_entry(page_ext, pfn);
250eefa864bSJoonsoo Kim }
251eefa864bSJoonsoo Kim 
alloc_page_ext(size_t size,int nid)252eefa864bSJoonsoo Kim static void *__meminit alloc_page_ext(size_t size, int nid)
253eefa864bSJoonsoo Kim {
254eefa864bSJoonsoo Kim 	gfp_t flags = GFP_KERNEL | __GFP_ZERO | __GFP_NOWARN;
255eefa864bSJoonsoo Kim 	void *addr = NULL;
256eefa864bSJoonsoo Kim 
257eefa864bSJoonsoo Kim 	addr = alloc_pages_exact_nid(nid, size, flags);
258eefa864bSJoonsoo Kim 	if (addr) {
259eefa864bSJoonsoo Kim 		kmemleak_alloc(addr, size, 1, flags);
260eefa864bSJoonsoo Kim 		return addr;
261eefa864bSJoonsoo Kim 	}
262eefa864bSJoonsoo Kim 
263eefa864bSJoonsoo Kim 	addr = vzalloc_node(size, nid);
264eefa864bSJoonsoo Kim 
265eefa864bSJoonsoo Kim 	return addr;
266eefa864bSJoonsoo Kim }
267eefa864bSJoonsoo Kim 
init_section_page_ext(unsigned long pfn,int nid)268eefa864bSJoonsoo Kim static int __meminit init_section_page_ext(unsigned long pfn, int nid)
269eefa864bSJoonsoo Kim {
270eefa864bSJoonsoo Kim 	struct mem_section *section;
271eefa864bSJoonsoo Kim 	struct page_ext *base;
272eefa864bSJoonsoo Kim 	unsigned long table_size;
273eefa864bSJoonsoo Kim 
274eefa864bSJoonsoo Kim 	section = __pfn_to_section(pfn);
275eefa864bSJoonsoo Kim 
276eefa864bSJoonsoo Kim 	if (section->page_ext)
277eefa864bSJoonsoo Kim 		return 0;
278eefa864bSJoonsoo Kim 
2795556cfe8SVlastimil Babka 	table_size = page_ext_size * PAGES_PER_SECTION;
280eefa864bSJoonsoo Kim 	base = alloc_page_ext(table_size, nid);
281eefa864bSJoonsoo Kim 
282eefa864bSJoonsoo Kim 	/*
283eefa864bSJoonsoo Kim 	 * The value stored in section->page_ext is (base - pfn)
284eefa864bSJoonsoo Kim 	 * and it does not point to the memory block allocated above,
285eefa864bSJoonsoo Kim 	 * causing kmemleak false positives.
286eefa864bSJoonsoo Kim 	 */
287eefa864bSJoonsoo Kim 	kmemleak_not_leak(base);
288eefa864bSJoonsoo Kim 
289eefa864bSJoonsoo Kim 	if (!base) {
290eefa864bSJoonsoo Kim 		pr_err("page ext allocation failure\n");
291eefa864bSJoonsoo Kim 		return -ENOMEM;
292eefa864bSJoonsoo Kim 	}
293eefa864bSJoonsoo Kim 
294eefa864bSJoonsoo Kim 	/*
295eefa864bSJoonsoo Kim 	 * The passed "pfn" may not be aligned to SECTION.  For the calculation
296eefa864bSJoonsoo Kim 	 * we need to apply a mask.
297eefa864bSJoonsoo Kim 	 */
298eefa864bSJoonsoo Kim 	pfn &= PAGE_SECTION_MASK;
2995556cfe8SVlastimil Babka 	section->page_ext = (void *)base - page_ext_size * pfn;
300eefa864bSJoonsoo Kim 	total_usage += table_size;
301eefa864bSJoonsoo Kim 	return 0;
302eefa864bSJoonsoo Kim }
30376af6a05SDave Hansen 
free_page_ext(void * addr)304eefa864bSJoonsoo Kim static void free_page_ext(void *addr)
305eefa864bSJoonsoo Kim {
306eefa864bSJoonsoo Kim 	if (is_vmalloc_addr(addr)) {
307eefa864bSJoonsoo Kim 		vfree(addr);
308eefa864bSJoonsoo Kim 	} else {
309eefa864bSJoonsoo Kim 		struct page *page = virt_to_page(addr);
310eefa864bSJoonsoo Kim 		size_t table_size;
311eefa864bSJoonsoo Kim 
3125556cfe8SVlastimil Babka 		table_size = page_ext_size * PAGES_PER_SECTION;
313eefa864bSJoonsoo Kim 
314eefa864bSJoonsoo Kim 		BUG_ON(PageReserved(page));
3150c815854SQian Cai 		kmemleak_free(addr);
316eefa864bSJoonsoo Kim 		free_pages_exact(addr, table_size);
317eefa864bSJoonsoo Kim 	}
318eefa864bSJoonsoo Kim }
319eefa864bSJoonsoo Kim 
__free_page_ext(unsigned long pfn)320eefa864bSJoonsoo Kim static void __free_page_ext(unsigned long pfn)
321eefa864bSJoonsoo Kim {
322eefa864bSJoonsoo Kim 	struct mem_section *ms;
323eefa864bSJoonsoo Kim 	struct page_ext *base;
324eefa864bSJoonsoo Kim 
325eefa864bSJoonsoo Kim 	ms = __pfn_to_section(pfn);
326eefa864bSJoonsoo Kim 	if (!ms || !ms->page_ext)
327eefa864bSJoonsoo Kim 		return;
328b1d5488aSCharan Teja Kalla 
329b1d5488aSCharan Teja Kalla 	base = READ_ONCE(ms->page_ext);
330b1d5488aSCharan Teja Kalla 	/*
331b1d5488aSCharan Teja Kalla 	 * page_ext here can be valid while doing the roll back
332b1d5488aSCharan Teja Kalla 	 * operation in online_page_ext().
333b1d5488aSCharan Teja Kalla 	 */
334b1d5488aSCharan Teja Kalla 	if (page_ext_invalid(base))
335b1d5488aSCharan Teja Kalla 		base = (void *)base - PAGE_EXT_INVALID;
336b1d5488aSCharan Teja Kalla 	WRITE_ONCE(ms->page_ext, NULL);
337b1d5488aSCharan Teja Kalla 
338b1d5488aSCharan Teja Kalla 	base = get_entry(base, pfn);
339eefa864bSJoonsoo Kim 	free_page_ext(base);
340b1d5488aSCharan Teja Kalla }
341b1d5488aSCharan Teja Kalla 
__invalidate_page_ext(unsigned long pfn)342b1d5488aSCharan Teja Kalla static void __invalidate_page_ext(unsigned long pfn)
343b1d5488aSCharan Teja Kalla {
344b1d5488aSCharan Teja Kalla 	struct mem_section *ms;
345b1d5488aSCharan Teja Kalla 	void *val;
346b1d5488aSCharan Teja Kalla 
347b1d5488aSCharan Teja Kalla 	ms = __pfn_to_section(pfn);
348b1d5488aSCharan Teja Kalla 	if (!ms || !ms->page_ext)
349b1d5488aSCharan Teja Kalla 		return;
350b1d5488aSCharan Teja Kalla 	val = (void *)ms->page_ext + PAGE_EXT_INVALID;
351b1d5488aSCharan Teja Kalla 	WRITE_ONCE(ms->page_ext, val);
352eefa864bSJoonsoo Kim }
353eefa864bSJoonsoo Kim 
online_page_ext(unsigned long start_pfn,unsigned long nr_pages,int nid)354eefa864bSJoonsoo Kim static int __meminit online_page_ext(unsigned long start_pfn,
355eefa864bSJoonsoo Kim 				unsigned long nr_pages,
356eefa864bSJoonsoo Kim 				int nid)
357eefa864bSJoonsoo Kim {
358eefa864bSJoonsoo Kim 	unsigned long start, end, pfn;
359eefa864bSJoonsoo Kim 	int fail = 0;
360eefa864bSJoonsoo Kim 
361eefa864bSJoonsoo Kim 	start = SECTION_ALIGN_DOWN(start_pfn);
362eefa864bSJoonsoo Kim 	end = SECTION_ALIGN_UP(start_pfn + nr_pages);
363eefa864bSJoonsoo Kim 
36498fa15f3SAnshuman Khandual 	if (nid == NUMA_NO_NODE) {
365eefa864bSJoonsoo Kim 		/*
366eefa864bSJoonsoo Kim 		 * In this case, "nid" already exists and contains valid memory.
367eefa864bSJoonsoo Kim 		 * "start_pfn" passed to us is a pfn which is an arg for
368eefa864bSJoonsoo Kim 		 * online__pages(), and start_pfn should exist.
369eefa864bSJoonsoo Kim 		 */
370eefa864bSJoonsoo Kim 		nid = pfn_to_nid(start_pfn);
37130a51400SPeng Liu 		VM_BUG_ON(!node_online(nid));
372eefa864bSJoonsoo Kim 	}
373eefa864bSJoonsoo Kim 
374dccacf8dSDavid Hildenbrand 	for (pfn = start; !fail && pfn < end; pfn += PAGES_PER_SECTION)
375eefa864bSJoonsoo Kim 		fail = init_section_page_ext(pfn, nid);
376eefa864bSJoonsoo Kim 	if (!fail)
377eefa864bSJoonsoo Kim 		return 0;
378eefa864bSJoonsoo Kim 
379eefa864bSJoonsoo Kim 	/* rollback */
3803c09be5aSKemeng Shi 	end = pfn - PAGES_PER_SECTION;
381eefa864bSJoonsoo Kim 	for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
382eefa864bSJoonsoo Kim 		__free_page_ext(pfn);
383eefa864bSJoonsoo Kim 
384eefa864bSJoonsoo Kim 	return -ENOMEM;
385eefa864bSJoonsoo Kim }
386eefa864bSJoonsoo Kim 
offline_page_ext(unsigned long start_pfn,unsigned long nr_pages)387063ff7cdSKemeng Shi static void __meminit offline_page_ext(unsigned long start_pfn,
3887b5a0b66SCharan Teja Kalla 				unsigned long nr_pages)
389eefa864bSJoonsoo Kim {
390eefa864bSJoonsoo Kim 	unsigned long start, end, pfn;
391eefa864bSJoonsoo Kim 
392eefa864bSJoonsoo Kim 	start = SECTION_ALIGN_DOWN(start_pfn);
393eefa864bSJoonsoo Kim 	end = SECTION_ALIGN_UP(start_pfn + nr_pages);
394eefa864bSJoonsoo Kim 
395b1d5488aSCharan Teja Kalla 	/*
396b1d5488aSCharan Teja Kalla 	 * Freeing of page_ext is done in 3 steps to avoid
397b1d5488aSCharan Teja Kalla 	 * use-after-free of it:
398b1d5488aSCharan Teja Kalla 	 * 1) Traverse all the sections and mark their page_ext
399b1d5488aSCharan Teja Kalla 	 *    as invalid.
400b1d5488aSCharan Teja Kalla 	 * 2) Wait for all the existing users of page_ext who
401b1d5488aSCharan Teja Kalla 	 *    started before invalidation to finish.
402b1d5488aSCharan Teja Kalla 	 * 3) Free the page_ext.
403b1d5488aSCharan Teja Kalla 	 */
404b1d5488aSCharan Teja Kalla 	for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
405b1d5488aSCharan Teja Kalla 		__invalidate_page_ext(pfn);
406b1d5488aSCharan Teja Kalla 
407b1d5488aSCharan Teja Kalla 	synchronize_rcu();
408b1d5488aSCharan Teja Kalla 
409eefa864bSJoonsoo Kim 	for (pfn = start; pfn < end; pfn += PAGES_PER_SECTION)
410eefa864bSJoonsoo Kim 		__free_page_ext(pfn);
411eefa864bSJoonsoo Kim }
412eefa864bSJoonsoo Kim 
page_ext_callback(struct notifier_block * self,unsigned long action,void * arg)413eefa864bSJoonsoo Kim static int __meminit page_ext_callback(struct notifier_block *self,
414eefa864bSJoonsoo Kim 			       unsigned long action, void *arg)
415eefa864bSJoonsoo Kim {
416eefa864bSJoonsoo Kim 	struct memory_notify *mn = arg;
417eefa864bSJoonsoo Kim 	int ret = 0;
418eefa864bSJoonsoo Kim 
419eefa864bSJoonsoo Kim 	switch (action) {
420eefa864bSJoonsoo Kim 	case MEM_GOING_ONLINE:
421eefa864bSJoonsoo Kim 		ret = online_page_ext(mn->start_pfn,
422eefa864bSJoonsoo Kim 				   mn->nr_pages, mn->status_change_nid);
423eefa864bSJoonsoo Kim 		break;
424eefa864bSJoonsoo Kim 	case MEM_OFFLINE:
425eefa864bSJoonsoo Kim 		offline_page_ext(mn->start_pfn,
4267b5a0b66SCharan Teja Kalla 				mn->nr_pages);
427eefa864bSJoonsoo Kim 		break;
428eefa864bSJoonsoo Kim 	case MEM_CANCEL_ONLINE:
429eefa864bSJoonsoo Kim 		offline_page_ext(mn->start_pfn,
4307b5a0b66SCharan Teja Kalla 				mn->nr_pages);
431eefa864bSJoonsoo Kim 		break;
432eefa864bSJoonsoo Kim 	case MEM_GOING_OFFLINE:
433eefa864bSJoonsoo Kim 		break;
434eefa864bSJoonsoo Kim 	case MEM_ONLINE:
435eefa864bSJoonsoo Kim 	case MEM_CANCEL_OFFLINE:
436eefa864bSJoonsoo Kim 		break;
437eefa864bSJoonsoo Kim 	}
438eefa864bSJoonsoo Kim 
439eefa864bSJoonsoo Kim 	return notifier_from_errno(ret);
440eefa864bSJoonsoo Kim }
441eefa864bSJoonsoo Kim 
page_ext_init(void)442eefa864bSJoonsoo Kim void __init page_ext_init(void)
443eefa864bSJoonsoo Kim {
444eefa864bSJoonsoo Kim 	unsigned long pfn;
445eefa864bSJoonsoo Kim 	int nid;
446eefa864bSJoonsoo Kim 
447eefa864bSJoonsoo Kim 	if (!invoke_need_callbacks())
448eefa864bSJoonsoo Kim 		return;
449eefa864bSJoonsoo Kim 
450eefa864bSJoonsoo Kim 	for_each_node_state(nid, N_MEMORY) {
451eefa864bSJoonsoo Kim 		unsigned long start_pfn, end_pfn;
452eefa864bSJoonsoo Kim 
453eefa864bSJoonsoo Kim 		start_pfn = node_start_pfn(nid);
454eefa864bSJoonsoo Kim 		end_pfn = node_end_pfn(nid);
455eefa864bSJoonsoo Kim 		/*
456eefa864bSJoonsoo Kim 		 * start_pfn and end_pfn may not be aligned to SECTION and the
457eefa864bSJoonsoo Kim 		 * page->flags of out of node pages are not initialized.  So we
458eefa864bSJoonsoo Kim 		 * scan [start_pfn, the biggest section's pfn < end_pfn) here.
459eefa864bSJoonsoo Kim 		 */
460eefa864bSJoonsoo Kim 		for (pfn = start_pfn; pfn < end_pfn;
461eefa864bSJoonsoo Kim 			pfn = ALIGN(pfn + 1, PAGES_PER_SECTION)) {
462eefa864bSJoonsoo Kim 
463eefa864bSJoonsoo Kim 			if (!pfn_valid(pfn))
464eefa864bSJoonsoo Kim 				continue;
465eefa864bSJoonsoo Kim 			/*
466eefa864bSJoonsoo Kim 			 * Nodes's pfns can be overlapping.
467eefa864bSJoonsoo Kim 			 * We know some arch can have a nodes layout such as
468eefa864bSJoonsoo Kim 			 * -------------pfn-------------->
469eefa864bSJoonsoo Kim 			 * N0 | N1 | N2 | N0 | N1 | N2|....
470eefa864bSJoonsoo Kim 			 */
4712f1ee091SQian Cai 			if (pfn_to_nid(pfn) != nid)
472eefa864bSJoonsoo Kim 				continue;
473eefa864bSJoonsoo Kim 			if (init_section_page_ext(pfn, nid))
474eefa864bSJoonsoo Kim 				goto oom;
4750fc542b7SVlastimil Babka 			cond_resched();
476eefa864bSJoonsoo Kim 		}
477eefa864bSJoonsoo Kim 	}
4781eeaa4fdSLiu Shixin 	hotplug_memory_notifier(page_ext_callback, DEFAULT_CALLBACK_PRI);
479eefa864bSJoonsoo Kim 	pr_info("allocated %ld bytes of page_ext\n", total_usage);
480eefa864bSJoonsoo Kim 	invoke_init_callbacks();
481eefa864bSJoonsoo Kim 	return;
482eefa864bSJoonsoo Kim 
483eefa864bSJoonsoo Kim oom:
484eefa864bSJoonsoo Kim 	panic("Out of memory");
485eefa864bSJoonsoo Kim }
486eefa864bSJoonsoo Kim 
pgdat_page_ext_init(struct pglist_data * pgdat)487eefa864bSJoonsoo Kim void __meminit pgdat_page_ext_init(struct pglist_data *pgdat)
488eefa864bSJoonsoo Kim {
489eefa864bSJoonsoo Kim }
490eefa864bSJoonsoo Kim 
491eefa864bSJoonsoo Kim #endif
492*eb0da7f6SKemeng Shi 
493*eb0da7f6SKemeng Shi /**
494*eb0da7f6SKemeng Shi  * page_ext_get() - Get the extended information for a page.
495*eb0da7f6SKemeng Shi  * @page: The page we're interested in.
496*eb0da7f6SKemeng Shi  *
497*eb0da7f6SKemeng Shi  * Ensures that the page_ext will remain valid until page_ext_put()
498*eb0da7f6SKemeng Shi  * is called.
499*eb0da7f6SKemeng Shi  *
500*eb0da7f6SKemeng Shi  * Return: NULL if no page_ext exists for this page.
501*eb0da7f6SKemeng Shi  * Context: Any context.  Caller may not sleep until they have called
502*eb0da7f6SKemeng Shi  * page_ext_put().
503*eb0da7f6SKemeng Shi  */
page_ext_get(struct page * page)504*eb0da7f6SKemeng Shi struct page_ext *page_ext_get(struct page *page)
505*eb0da7f6SKemeng Shi {
506*eb0da7f6SKemeng Shi 	struct page_ext *page_ext;
507*eb0da7f6SKemeng Shi 
508*eb0da7f6SKemeng Shi 	rcu_read_lock();
509*eb0da7f6SKemeng Shi 	page_ext = lookup_page_ext(page);
510*eb0da7f6SKemeng Shi 	if (!page_ext) {
511*eb0da7f6SKemeng Shi 		rcu_read_unlock();
512*eb0da7f6SKemeng Shi 		return NULL;
513*eb0da7f6SKemeng Shi 	}
514*eb0da7f6SKemeng Shi 
515*eb0da7f6SKemeng Shi 	return page_ext;
516*eb0da7f6SKemeng Shi }
517*eb0da7f6SKemeng Shi 
518*eb0da7f6SKemeng Shi /**
519*eb0da7f6SKemeng Shi  * page_ext_put() - Working with page extended information is done.
520*eb0da7f6SKemeng Shi  * @page_ext: Page extended information received from page_ext_get().
521*eb0da7f6SKemeng Shi  *
522*eb0da7f6SKemeng Shi  * The page extended information of the page may not be valid after this
523*eb0da7f6SKemeng Shi  * function is called.
524*eb0da7f6SKemeng Shi  *
525*eb0da7f6SKemeng Shi  * Return: None.
526*eb0da7f6SKemeng Shi  * Context: Any context with corresponding page_ext_get() is called.
527*eb0da7f6SKemeng Shi  */
page_ext_put(struct page_ext * page_ext)528*eb0da7f6SKemeng Shi void page_ext_put(struct page_ext *page_ext)
529*eb0da7f6SKemeng Shi {
530*eb0da7f6SKemeng Shi 	if (unlikely(!page_ext))
531*eb0da7f6SKemeng Shi 		return;
532*eb0da7f6SKemeng Shi 
533*eb0da7f6SKemeng Shi 	rcu_read_unlock();
534*eb0da7f6SKemeng Shi }
535