xref: /linux/mm/sparse-vmemmap.c (revision 1b78070aaef63512688aebfbc82365ef9d6660f1)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Virtual Memory Map support
4  *
5  * (C) 2007 sgi. Christoph Lameter.
6  *
7  * Virtual memory maps allow VM primitives pfn_to_page, page_to_pfn,
8  * virt_to_page, page_address() to be implemented as a base offset
9  * calculation without memory access.
10  *
11  * However, virtual mappings need a page table and TLBs. Many Linux
12  * architectures already map their physical space using 1-1 mappings
13  * via TLBs. For those arches the virtual memory map is essentially
14  * for free if we use the same page size as the 1-1 mappings. In that
15  * case the overhead consists of a few additional pages that are
16  * allocated to create a view of memory for vmemmap.
17  *
18  * The architecture is expected to provide a vmemmap_populate() function
19  * to instantiate the mapping.
20  */
21 #include <linux/mm.h>
22 #include <linux/mmzone.h>
23 #include <linux/memblock.h>
24 #include <linux/memremap.h>
25 #include <linux/highmem.h>
26 #include <linux/slab.h>
27 #include <linux/spinlock.h>
28 #include <linux/vmalloc.h>
29 #include <linux/sched.h>
30 #include <linux/pgalloc.h>
31 
32 #include <asm/dma.h>
33 #include <asm/tlbflush.h>
34 
35 #include "hugetlb_vmemmap.h"
36 
37 /*
38  * Flags for vmemmap_populate_range and friends.
39  */
40 /* Get a ref on the head page struct page, for ZONE_DEVICE compound pages */
41 #define VMEMMAP_POPULATE_PAGEREF	0x0001
42 
43 #include "internal.h"
44 #include "mm_init.h"
45 #include "sparse.h"
46 
47 /*
48  * Allocate a block of memory to be used to back the virtual memory map
49  * or to back the page tables that are used to create the mapping.
50  * Uses the main allocators if they are available, else bootmem.
51  */
52 
53 static void * __ref __earlyonly_bootmem_alloc(int node,
54 				unsigned long size,
55 				unsigned long align,
56 				unsigned long goal)
57 {
58 	return memmap_alloc(size, align, goal, node, false);
59 }
60 
61 void * __meminit vmemmap_alloc_block(unsigned long size, int node)
62 {
63 	/* If the main allocator is up use that, fallback to bootmem. */
64 	if (slab_is_available()) {
65 		gfp_t gfp_mask = GFP_KERNEL|__GFP_RETRY_MAYFAIL|__GFP_NOWARN;
66 		int order = get_order(size);
67 		static bool warned __meminitdata;
68 		struct page *page;
69 
70 		page = alloc_pages_node(node, gfp_mask, order);
71 		if (page)
72 			return page_address(page);
73 
74 		if (!warned) {
75 			warn_alloc(gfp_mask & ~__GFP_NOWARN, NULL,
76 				   "vmemmap alloc failure: order:%u", order);
77 			warned = true;
78 		}
79 		return NULL;
80 	} else
81 		return __earlyonly_bootmem_alloc(node, size, size,
82 				__pa(MAX_DMA_ADDRESS));
83 }
84 
85 static void * __meminit altmap_alloc_block_buf(unsigned long size,
86 					       struct vmem_altmap *altmap);
87 
88 /* need to make sure size is all the same during early stage */
89 void * __meminit vmemmap_alloc_block_buf(unsigned long size, int node,
90 					 struct vmem_altmap *altmap)
91 {
92 	if (altmap)
93 		return altmap_alloc_block_buf(size, altmap);
94 
95 	return vmemmap_alloc_block(size, node);
96 }
97 
98 static unsigned long __meminit vmem_altmap_next_pfn(struct vmem_altmap *altmap)
99 {
100 	return altmap->base_pfn + altmap->reserve + altmap->alloc
101 		+ altmap->align;
102 }
103 
104 static unsigned long __meminit vmem_altmap_nr_free(struct vmem_altmap *altmap)
105 {
106 	unsigned long allocated = altmap->alloc + altmap->align;
107 
108 	if (altmap->free > allocated)
109 		return altmap->free - allocated;
110 	return 0;
111 }
112 
113 static void * __meminit altmap_alloc_block_buf(unsigned long size,
114 					       struct vmem_altmap *altmap)
115 {
116 	unsigned long pfn, nr_pfns, nr_align;
117 
118 	if (size & ~PAGE_MASK) {
119 		pr_warn_once("%s: allocations must be multiple of PAGE_SIZE (%ld)\n",
120 				__func__, size);
121 		return NULL;
122 	}
123 
124 	pfn = vmem_altmap_next_pfn(altmap);
125 	nr_pfns = size >> PAGE_SHIFT;
126 	nr_align = 1UL << find_first_bit(&nr_pfns, BITS_PER_LONG);
127 	nr_align = ALIGN(pfn, nr_align) - pfn;
128 	if (nr_pfns + nr_align > vmem_altmap_nr_free(altmap))
129 		return NULL;
130 
131 	altmap->alloc += nr_pfns;
132 	altmap->align += nr_align;
133 	pfn += nr_align;
134 
135 	pr_debug("%s: pfn: %#lx alloc: %ld align: %ld nr: %#lx\n",
136 			__func__, pfn, altmap->alloc, altmap->align, nr_pfns);
137 	return __va(__pfn_to_phys(pfn));
138 }
139 
140 void __meminit vmemmap_verify(pte_t *pte, int node,
141 				unsigned long start, unsigned long end)
142 {
143 	unsigned long pfn = pte_pfn(ptep_get(pte));
144 	int actual_node = early_pfn_to_nid(pfn);
145 
146 	if (node_distance(actual_node, node) > LOCAL_DISTANCE)
147 		pr_warn_once("[%lx-%lx] potential offnode page_structs\n",
148 			start, end - 1);
149 }
150 
151 static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node,
152 				       struct vmem_altmap *altmap,
153 				       unsigned long ptpfn, unsigned long flags)
154 {
155 	pte_t *pte = pte_offset_kernel(pmd, addr);
156 	if (pte_none(ptep_get(pte))) {
157 		pte_t entry;
158 		void *p;
159 
160 		if (ptpfn == (unsigned long)-1) {
161 			p = vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap);
162 			if (!p)
163 				return NULL;
164 			ptpfn = PHYS_PFN(__pa(p));
165 		} else {
166 			/*
167 			 * When a PTE/PMD entry is freed from the init_mm
168 			 * there's a free_pages() call to this page allocated
169 			 * above. Thus this get_page() is paired with the
170 			 * put_page_testzero() on the freeing path.
171 			 * This can only called by certain ZONE_DEVICE path,
172 			 * and through vmemmap_populate_compound_pages() when
173 			 * slab is available.
174 			 */
175 			if (flags & VMEMMAP_POPULATE_PAGEREF)
176 				get_page(pfn_to_page(ptpfn));
177 		}
178 		entry = pfn_pte(ptpfn, PAGE_KERNEL);
179 		set_pte_at(&init_mm, addr, pte, entry);
180 	}
181 	return pte;
182 }
183 
184 static void * __meminit vmemmap_alloc_block_zero(unsigned long size, int node)
185 {
186 	void *p = vmemmap_alloc_block(size, node);
187 
188 	if (!p)
189 		return NULL;
190 	memset(p, 0, size);
191 
192 	return p;
193 }
194 
195 static pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node)
196 {
197 	pmd_t *pmd = pmd_offset(pud, addr);
198 	if (pmd_none(*pmd)) {
199 		void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
200 		if (!p)
201 			return NULL;
202 		kernel_pte_init(p);
203 		pmd_populate_kernel(&init_mm, pmd, p);
204 	}
205 	return pmd;
206 }
207 
208 static pud_t * __meminit vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node)
209 {
210 	pud_t *pud = pud_offset(p4d, addr);
211 	if (pud_none(*pud)) {
212 		void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
213 		if (!p)
214 			return NULL;
215 		pmd_init(p);
216 		pud_populate(&init_mm, pud, p);
217 	}
218 	return pud;
219 }
220 
221 static p4d_t * __meminit vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, int node)
222 {
223 	p4d_t *p4d = p4d_offset(pgd, addr);
224 	if (p4d_none(*p4d)) {
225 		void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
226 		if (!p)
227 			return NULL;
228 		pud_init(p);
229 		p4d_populate_kernel(addr, p4d, p);
230 	}
231 	return p4d;
232 }
233 
234 static pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node)
235 {
236 	pgd_t *pgd = pgd_offset_k(addr);
237 	if (pgd_none(*pgd)) {
238 		void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
239 		if (!p)
240 			return NULL;
241 		pgd_populate_kernel(addr, pgd, p);
242 	}
243 	return pgd;
244 }
245 
246 static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node,
247 					      struct vmem_altmap *altmap,
248 					      unsigned long ptpfn,
249 					      unsigned long flags)
250 {
251 	pgd_t *pgd;
252 	p4d_t *p4d;
253 	pud_t *pud;
254 	pmd_t *pmd;
255 	pte_t *pte;
256 
257 	pgd = vmemmap_pgd_populate(addr, node);
258 	if (!pgd)
259 		return NULL;
260 	p4d = vmemmap_p4d_populate(pgd, addr, node);
261 	if (!p4d)
262 		return NULL;
263 	pud = vmemmap_pud_populate(p4d, addr, node);
264 	if (!pud)
265 		return NULL;
266 	pmd = vmemmap_pmd_populate(pud, addr, node);
267 	if (!pmd)
268 		return NULL;
269 	pte = vmemmap_pte_populate(pmd, addr, node, altmap, ptpfn, flags);
270 	if (!pte)
271 		return NULL;
272 	vmemmap_verify(pte, node, addr, addr + PAGE_SIZE);
273 
274 	return pte;
275 }
276 
277 static int __meminit vmemmap_populate_range(unsigned long start,
278 					    unsigned long end, int node,
279 					    struct vmem_altmap *altmap,
280 					    unsigned long ptpfn,
281 					    unsigned long flags)
282 {
283 	unsigned long addr = start;
284 	pte_t *pte;
285 
286 	for (; addr < end; addr += PAGE_SIZE) {
287 		pte = vmemmap_populate_address(addr, node, altmap,
288 					       ptpfn, flags);
289 		if (!pte)
290 			return -ENOMEM;
291 	}
292 
293 	return 0;
294 }
295 
296 int __meminit vmemmap_populate_basepages(unsigned long start, unsigned long end,
297 					 int node, struct vmem_altmap *altmap)
298 {
299 	return vmemmap_populate_range(start, end, node, altmap, -1, 0);
300 }
301 
302 /*
303  * Write protect the mirrored tail page structs for HVO. This will be
304  * called from the hugetlb code when gathering and initializing the
305  * memblock allocated gigantic pages. The write protect can't be
306  * done earlier, since it can't be guaranteed that the reserved
307  * page structures will not be written to during initialization,
308  * even if CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled.
309  *
310  * The PTEs are known to exist, and nothing else should be touching
311  * these pages. The caller is responsible for any TLB flushing.
312  */
313 void vmemmap_wrprotect_hvo(unsigned long addr, unsigned long end,
314 				    int node, unsigned long headsize)
315 {
316 	unsigned long maddr;
317 	pte_t *pte;
318 
319 	for (maddr = addr + headsize; maddr < end; maddr += PAGE_SIZE) {
320 		pte = virt_to_kpte(maddr);
321 		ptep_set_wrprotect(&init_mm, maddr, pte);
322 	}
323 }
324 
325 #ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
326 static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone *zone)
327 {
328 	struct page *p, *tail;
329 	unsigned int idx;
330 	int node = zone_to_nid(zone);
331 
332 	if (WARN_ON_ONCE(order < VMEMMAP_TAIL_MIN_ORDER))
333 		return NULL;
334 	if (WARN_ON_ONCE(order > MAX_FOLIO_ORDER))
335 		return NULL;
336 
337 	idx = order - VMEMMAP_TAIL_MIN_ORDER;
338 	tail = zone->vmemmap_tails[idx];
339 	if (tail)
340 		return tail;
341 
342 	/*
343 	 * Only allocate the page, but do not initialize it.
344 	 *
345 	 * Any initialization done here will be overwritten by memmap_init().
346 	 *
347 	 * hugetlb_bootmem_struct_page_init() will take care of initialization
348 	 * after memmap_init().
349 	 */
350 
351 	p = vmemmap_alloc_block_zero(PAGE_SIZE, node);
352 	if (!p)
353 		return NULL;
354 
355 	tail = virt_to_page(p);
356 	zone->vmemmap_tails[idx] = tail;
357 
358 	return tail;
359 }
360 
361 int __meminit vmemmap_populate_hvo(unsigned long addr, unsigned long end,
362 				       unsigned int order, struct zone *zone,
363 				       unsigned long headsize)
364 {
365 	unsigned long maddr;
366 	struct page *tail;
367 	pte_t *pte;
368 	int node = zone_to_nid(zone);
369 
370 	tail = vmemmap_get_tail(order, zone);
371 	if (!tail)
372 		return -ENOMEM;
373 
374 	for (maddr = addr; maddr < addr + headsize; maddr += PAGE_SIZE) {
375 		pte = vmemmap_populate_address(maddr, node, NULL, -1, 0);
376 		if (!pte)
377 			return -ENOMEM;
378 	}
379 
380 	/*
381 	 * Reuse the last page struct page mapped above for the rest.
382 	 */
383 	return vmemmap_populate_range(maddr, end, node, NULL,
384 				      page_to_pfn(tail), 0);
385 }
386 #endif
387 
388 void __weak __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node,
389 				      unsigned long addr, unsigned long next)
390 {
391 	WARN_ON_ONCE(!pmd_set_huge(pmd, virt_to_phys(p), PAGE_KERNEL));
392 }
393 
394 int __weak __meminit vmemmap_check_pmd(pmd_t *pmd, int node,
395 				       unsigned long addr, unsigned long next)
396 {
397 	if (!pmd_leaf(pmdp_get(pmd)))
398 		return 0;
399 	vmemmap_verify((pte_t *)pmd, node, addr, next);
400 
401 	return 1;
402 }
403 
404 int __meminit vmemmap_populate_hugepages(unsigned long start, unsigned long end,
405 					 int node, struct vmem_altmap *altmap)
406 {
407 	unsigned long addr;
408 	unsigned long next;
409 	pgd_t *pgd;
410 	p4d_t *p4d;
411 	pud_t *pud;
412 	pmd_t *pmd;
413 
414 	for (addr = start; addr < end; addr = next) {
415 		next = pmd_addr_end(addr, end);
416 
417 		pgd = vmemmap_pgd_populate(addr, node);
418 		if (!pgd)
419 			return -ENOMEM;
420 
421 		p4d = vmemmap_p4d_populate(pgd, addr, node);
422 		if (!p4d)
423 			return -ENOMEM;
424 
425 		pud = vmemmap_pud_populate(p4d, addr, node);
426 		if (!pud)
427 			return -ENOMEM;
428 
429 		pmd = pmd_offset(pud, addr);
430 		if (pmd_none(pmdp_get(pmd))) {
431 			void *p;
432 
433 			p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap);
434 			if (p) {
435 				vmemmap_set_pmd(pmd, p, node, addr, next);
436 				continue;
437 			} else if (altmap) {
438 				/*
439 				 * No fallback: In any case we care about, the
440 				 * altmap should be reasonably sized and aligned
441 				 * such that vmemmap_alloc_block_buf() will always
442 				 * succeed. For consistency with the PTE case,
443 				 * return an error here as failure could indicate
444 				 * a configuration issue with the size of the altmap.
445 				 */
446 				return -ENOMEM;
447 			}
448 		} else if (vmemmap_check_pmd(pmd, node, addr, next))
449 			continue;
450 		if (vmemmap_populate_basepages(addr, next, node, altmap))
451 			return -ENOMEM;
452 	}
453 	return 0;
454 }
455 
456 #ifndef vmemmap_populate_compound_pages
457 /*
458  * For compound pages bigger than section size (e.g. x86 1G compound
459  * pages with 2M subsection size) fill the rest of sections as tail
460  * pages.
461  *
462  * Note that memremap_pages() resets @nr_range value and will increment
463  * it after each range successful onlining. Thus the value or @nr_range
464  * at section memmap populate corresponds to the in-progress range
465  * being onlined here.
466  */
467 static bool __meminit reuse_compound_section(unsigned long start_pfn,
468 					     struct dev_pagemap *pgmap)
469 {
470 	unsigned long nr_pages = pgmap_vmemmap_nr(pgmap);
471 	unsigned long offset = start_pfn -
472 		PHYS_PFN(pgmap->ranges[pgmap->nr_range].start);
473 
474 	return !IS_ALIGNED(offset, nr_pages) && nr_pages > PAGES_PER_SUBSECTION;
475 }
476 
477 static pte_t * __meminit compound_section_tail_page(unsigned long addr)
478 {
479 	pte_t *pte;
480 
481 	addr -= PAGE_SIZE;
482 
483 	/*
484 	 * Assuming sections are populated sequentially, the previous section's
485 	 * page data can be reused.
486 	 */
487 	pte = pte_offset_kernel(pmd_off_k(addr), addr);
488 	if (!pte)
489 		return NULL;
490 
491 	return pte;
492 }
493 
494 static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn,
495 						     unsigned long start,
496 						     unsigned long end, int node,
497 						     struct dev_pagemap *pgmap)
498 {
499 	unsigned long size, addr;
500 	pte_t *pte;
501 	int rc;
502 
503 	if (reuse_compound_section(start_pfn, pgmap)) {
504 		pte = compound_section_tail_page(start);
505 		if (!pte)
506 			return -ENOMEM;
507 
508 		/*
509 		 * Reuse the page that was populated in the prior iteration
510 		 * with just tail struct pages.
511 		 */
512 		return vmemmap_populate_range(start, end, node, NULL,
513 					      pte_pfn(ptep_get(pte)),
514 					      VMEMMAP_POPULATE_PAGEREF);
515 	}
516 
517 	size = min(end - start, pgmap_vmemmap_nr(pgmap) * sizeof(struct page));
518 	for (addr = start; addr < end; addr += size) {
519 		unsigned long next, last = addr + size;
520 
521 		/* Populate the head page vmemmap page */
522 		pte = vmemmap_populate_address(addr, node, NULL, -1, 0);
523 		if (!pte)
524 			return -ENOMEM;
525 
526 		/* Populate the tail pages vmemmap page */
527 		next = addr + PAGE_SIZE;
528 		pte = vmemmap_populate_address(next, node, NULL, -1, 0);
529 		if (!pte)
530 			return -ENOMEM;
531 
532 		/*
533 		 * Reuse the previous page for the rest of tail pages
534 		 * See layout diagram in Documentation/mm/vmemmap_dedup.rst
535 		 */
536 		next += PAGE_SIZE;
537 		rc = vmemmap_populate_range(next, last, node, NULL,
538 					    pte_pfn(ptep_get(pte)),
539 					    VMEMMAP_POPULATE_PAGEREF);
540 		if (rc)
541 			return -ENOMEM;
542 	}
543 
544 	return 0;
545 }
546 
547 #endif
548 
549 struct page * __meminit __populate_section_memmap(unsigned long pfn,
550 		unsigned long nr_pages, int nid, struct vmem_altmap *altmap,
551 		struct dev_pagemap *pgmap)
552 {
553 	unsigned long start = (unsigned long) pfn_to_page(pfn);
554 	unsigned long end = start + nr_pages * sizeof(struct page);
555 	int r;
556 
557 	if (WARN_ON_ONCE(!IS_ALIGNED(pfn, PAGES_PER_SUBSECTION) ||
558 		!IS_ALIGNED(nr_pages, PAGES_PER_SUBSECTION)))
559 		return NULL;
560 
561 	if (vmemmap_can_optimize(altmap, pgmap))
562 		r = vmemmap_populate_compound_pages(pfn, start, end, nid, pgmap);
563 	else
564 		r = vmemmap_populate(start, end, nid, altmap);
565 
566 	if (r < 0)
567 		return NULL;
568 
569 	flush_cache_vmap(start, end);
570 
571 	return pfn_to_page(pfn);
572 }
573 
574 #ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT
575 /*
576  * This is called just before initializing sections for a NUMA node.
577  * Any special initialization that needs to be done before the
578  * generic initialization can be done from here. Sections that
579  * are initialized in hooks called from here will be skipped by
580  * the generic initialization.
581  */
582 void __init sparse_vmemmap_init_nid_early(int nid)
583 {
584 	hugetlb_vmemmap_init_early(nid);
585 }
586 #endif
587 
588 static void subsection_mask_set(unsigned long *map, unsigned long pfn,
589 		unsigned long nr_pages)
590 {
591 	int idx = subsection_map_index(pfn);
592 	int end = subsection_map_index(pfn + nr_pages - 1);
593 
594 	bitmap_set(map, idx, end - idx + 1);
595 }
596 
597 static void __init sparse_init_subsection_map_range(unsigned long pfn, unsigned long nr_pages)
598 {
599 	int end_sec_nr = pfn_to_section_nr(pfn + nr_pages - 1);
600 	unsigned long nr, start_sec_nr = pfn_to_section_nr(pfn);
601 
602 	for (nr = start_sec_nr; nr <= end_sec_nr; nr++) {
603 		struct mem_section *ms;
604 		unsigned long pfns;
605 
606 		pfns = min(nr_pages, PAGES_PER_SECTION
607 				- (pfn & ~PAGE_SECTION_MASK));
608 		ms = __nr_to_section(nr);
609 		subsection_mask_set(ms->usage->subsection_map, pfn, pfns);
610 
611 		pr_debug("%s: sec: %lu pfns: %lu set(%d, %d)\n", __func__, nr,
612 				pfns, subsection_map_index(pfn),
613 				subsection_map_index(pfn + pfns - 1));
614 
615 		pfn += pfns;
616 		nr_pages -= pfns;
617 	}
618 }
619 
620 void __init sparse_init_subsection_map(void)
621 {
622 	int i, nid;
623 	unsigned long start, end;
624 
625 	for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, &nid)
626 		sparse_init_subsection_map_range(start, end - start);
627 }
628 
629 #ifdef CONFIG_MEMORY_HOTPLUG
630 
631 /* Mark all memory sections within the pfn range as online */
632 void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
633 {
634 	unsigned long pfn;
635 
636 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
637 		unsigned long section_nr = pfn_to_section_nr(pfn);
638 		struct mem_section *ms = __nr_to_section(section_nr);
639 
640 		ms->section_mem_map |= SECTION_IS_ONLINE;
641 	}
642 }
643 
644 /* Mark all memory sections within the pfn range as offline */
645 void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn)
646 {
647 	unsigned long pfn;
648 
649 	for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) {
650 		unsigned long section_nr = pfn_to_section_nr(pfn);
651 		struct mem_section *ms = __nr_to_section(section_nr);
652 
653 		ms->section_mem_map &= ~SECTION_IS_ONLINE;
654 	}
655 }
656 
657 static int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages,
658 		struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
659 {
660 	const unsigned int order = pgmap ? pgmap->vmemmap_shift : 0;
661 	const unsigned long pages_per_compound = 1UL << order;
662 
663 	VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION));
664 	VM_WARN_ON_ONCE(nr_pages > PAGES_PER_SECTION);
665 
666 	if (!vmemmap_can_optimize(altmap, pgmap))
667 		return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE);
668 
669 	if (order < PFN_SECTION_SHIFT) {
670 		VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, pages_per_compound));
671 		return VMEMMAP_RESERVE_NR * nr_pages / pages_per_compound;
672 	}
673 
674 	VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION));
675 
676 	if (IS_ALIGNED(pfn, pages_per_compound))
677 		return VMEMMAP_RESERVE_NR;
678 
679 	return 0;
680 }
681 
682 static struct page * __meminit populate_section_memmap(unsigned long pfn,
683 		unsigned long nr_pages, int nid, struct vmem_altmap *altmap,
684 		struct dev_pagemap *pgmap)
685 {
686 	struct page *page = __populate_section_memmap(pfn, nr_pages, nid, altmap,
687 						      pgmap);
688 
689 	memmap_pages_add(section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap));
690 
691 	return page;
692 }
693 
694 static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages,
695 		struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
696 {
697 	unsigned long start = (unsigned long) pfn_to_page(pfn);
698 	unsigned long end = start + nr_pages * sizeof(struct page);
699 
700 	memmap_pages_add(-section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap));
701 	vmemmap_free(start, end, altmap);
702 }
703 
704 static void free_map_bootmem(struct page *memmap)
705 {
706 	unsigned long start = (unsigned long)memmap;
707 	unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION);
708 	unsigned long pfn = page_to_pfn(memmap);
709 
710 	memmap_boot_pages_add(-section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION,
711 							NULL, NULL));
712 	vmemmap_free(start, end, NULL);
713 }
714 
715 static int clear_subsection_map(unsigned long pfn, unsigned long nr_pages)
716 {
717 	DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 };
718 	DECLARE_BITMAP(tmp, SUBSECTIONS_PER_SECTION) = { 0 };
719 	struct mem_section *ms = __pfn_to_section(pfn);
720 	unsigned long *subsection_map = ms->usage
721 		? &ms->usage->subsection_map[0] : NULL;
722 
723 	subsection_mask_set(map, pfn, nr_pages);
724 	if (subsection_map)
725 		bitmap_and(tmp, map, subsection_map, SUBSECTIONS_PER_SECTION);
726 
727 	if (WARN(!subsection_map || !bitmap_equal(tmp, map, SUBSECTIONS_PER_SECTION),
728 				"section already deactivated (%#lx + %ld)\n",
729 				pfn, nr_pages))
730 		return -EINVAL;
731 
732 	bitmap_xor(subsection_map, map, subsection_map, SUBSECTIONS_PER_SECTION);
733 	return 0;
734 }
735 
736 static bool is_subsection_map_empty(struct mem_section *ms)
737 {
738 	return bitmap_empty(&ms->usage->subsection_map[0],
739 			    SUBSECTIONS_PER_SECTION);
740 }
741 
742 static int fill_subsection_map(unsigned long pfn, unsigned long nr_pages)
743 {
744 	struct mem_section *ms = __pfn_to_section(pfn);
745 	DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 };
746 	unsigned long *subsection_map;
747 	int rc = 0;
748 
749 	subsection_mask_set(map, pfn, nr_pages);
750 
751 	subsection_map = &ms->usage->subsection_map[0];
752 
753 	if (bitmap_empty(map, SUBSECTIONS_PER_SECTION))
754 		rc = -EINVAL;
755 	else if (bitmap_intersects(map, subsection_map, SUBSECTIONS_PER_SECTION))
756 		rc = -EEXIST;
757 	else
758 		bitmap_or(subsection_map, map, subsection_map,
759 				SUBSECTIONS_PER_SECTION);
760 
761 	return rc;
762 }
763 
764 /*
765  * To deactivate a memory region, there are 3 cases to handle:
766  *
767  * 1. deactivation of a partial hot-added section:
768  *      a) section was present at memory init.
769  *      b) section was hot-added post memory init.
770  * 2. deactivation of a complete hot-added section.
771  * 3. deactivation of a complete section from memory init.
772  *
773  * For 1, when subsection_map does not empty we will not be freeing the
774  * usage map, but still need to free the vmemmap range.
775  */
776 static void section_deactivate(unsigned long pfn, unsigned long nr_pages,
777 		struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
778 {
779 	struct mem_section *ms = __pfn_to_section(pfn);
780 	bool section_is_early = early_section(ms);
781 	struct page *memmap = NULL;
782 	bool empty;
783 
784 	if (clear_subsection_map(pfn, nr_pages))
785 		return;
786 
787 	empty = is_subsection_map_empty(ms);
788 	if (empty) {
789 		/*
790 		 * Mark the section invalid so that valid_section()
791 		 * return false. This prevents code from dereferencing
792 		 * ms->usage array.
793 		 */
794 		ms->section_mem_map &= ~SECTION_HAS_MEM_MAP;
795 
796 		/*
797 		 * When removing an early section, the usage map is kept (as the
798 		 * usage maps of other sections fall into the same page). It
799 		 * will be re-used when re-adding the section - which is then no
800 		 * longer an early section. If the usage map is PageReserved, it
801 		 * was allocated during boot.
802 		 */
803 		if (!PageReserved(virt_to_page(ms->usage))) {
804 			kfree_rcu(ms->usage, rcu);
805 			WRITE_ONCE(ms->usage, NULL);
806 		}
807 		memmap = pfn_to_page(SECTION_ALIGN_DOWN(pfn));
808 	}
809 
810 	/*
811 	 * The memmap of early sections is always fully populated. See
812 	 * section_activate() and pfn_valid() .
813 	 */
814 	if (!section_is_early)
815 		depopulate_section_memmap(pfn, nr_pages, altmap, pgmap);
816 	else if (memmap)
817 		free_map_bootmem(memmap);
818 
819 	if (empty)
820 		ms->section_mem_map = (unsigned long)NULL;
821 }
822 
823 static struct page * __meminit section_activate(int nid, unsigned long pfn,
824 		unsigned long nr_pages, struct vmem_altmap *altmap,
825 		struct dev_pagemap *pgmap)
826 {
827 	struct mem_section *ms = __pfn_to_section(pfn);
828 	struct mem_section_usage *usage = NULL;
829 	struct page *memmap;
830 	int rc;
831 
832 	if (!ms->usage) {
833 		usage = kzalloc(mem_section_usage_size(), GFP_KERNEL);
834 		if (!usage)
835 			return ERR_PTR(-ENOMEM);
836 		ms->usage = usage;
837 	}
838 
839 	rc = fill_subsection_map(pfn, nr_pages);
840 	if (rc) {
841 		if (usage)
842 			ms->usage = NULL;
843 		kfree(usage);
844 		return ERR_PTR(rc);
845 	}
846 
847 	/*
848 	 * The early init code does not consider partially populated
849 	 * initial sections, it simply assumes that memory will never be
850 	 * referenced.  If we hot-add memory into such a section then we
851 	 * do not need to populate the memmap and can simply reuse what
852 	 * is already there.
853 	 */
854 	if (nr_pages < PAGES_PER_SECTION && early_section(ms))
855 		return pfn_to_page(pfn);
856 
857 	memmap = populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap);
858 	if (!memmap) {
859 		section_deactivate(pfn, nr_pages, altmap, pgmap);
860 		return ERR_PTR(-ENOMEM);
861 	}
862 
863 	return memmap;
864 }
865 
866 /**
867  * sparse_add_section - add a memory section, or populate an existing one
868  * @nid: The node to add section on
869  * @start_pfn: start pfn of the memory range
870  * @nr_pages: number of pfns to add in the section
871  * @altmap: alternate pfns to allocate the memmap backing store
872  * @pgmap: alternate compound page geometry for devmap mappings
873  *
874  * This is only intended for hotplug.
875  *
876  * Note that only VMEMMAP supports sub-section aligned hotplug,
877  * the proper alignment and size are gated by check_pfn_span().
878  *
879  *
880  * Return:
881  * * 0		- On success.
882  * * -EEXIST	- Section has been present.
883  * * -ENOMEM	- Out of memory.
884  */
885 int __meminit sparse_add_section(int nid, unsigned long start_pfn,
886 		unsigned long nr_pages, struct vmem_altmap *altmap,
887 		struct dev_pagemap *pgmap)
888 {
889 	unsigned long section_nr = pfn_to_section_nr(start_pfn);
890 	struct mem_section *ms;
891 	struct page *memmap;
892 	int ret;
893 
894 	ret = sparse_index_init(section_nr, nid);
895 	if (ret < 0)
896 		return ret;
897 
898 	memmap = section_activate(nid, start_pfn, nr_pages, altmap, pgmap);
899 	if (IS_ERR(memmap))
900 		return PTR_ERR(memmap);
901 
902 	/*
903 	 * Poison uninitialized struct pages in order to catch invalid flags
904 	 * combinations.
905 	 */
906 	page_init_poison(memmap, sizeof(struct page) * nr_pages);
907 
908 	ms = __nr_to_section(section_nr);
909 	__section_mark_present(ms, section_nr);
910 
911 	/* Align memmap to section boundary in the subsection case */
912 	if (section_nr_to_pfn(section_nr) != start_pfn)
913 		memmap = pfn_to_page(section_nr_to_pfn(section_nr));
914 	sparse_init_one_section(ms, section_nr, memmap, ms->usage, 0);
915 
916 	return 0;
917 }
918 
919 void sparse_remove_section(unsigned long pfn, unsigned long nr_pages,
920 		struct vmem_altmap *altmap, struct dev_pagemap *pgmap)
921 {
922 	struct mem_section *ms = __pfn_to_section(pfn);
923 
924 	if (WARN_ON_ONCE(!valid_section(ms)))
925 		return;
926 
927 	section_deactivate(pfn, nr_pages, altmap, pgmap);
928 }
929 #endif /* CONFIG_MEMORY_HOTPLUG */
930