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 45 /* 46 * Allocate a block of memory to be used to back the virtual memory map 47 * or to back the page tables that are used to create the mapping. 48 * Uses the main allocators if they are available, else bootmem. 49 */ 50 51 static void * __ref __earlyonly_bootmem_alloc(int node, 52 unsigned long size, 53 unsigned long align, 54 unsigned long goal) 55 { 56 return memmap_alloc(size, align, goal, node, false); 57 } 58 59 void * __meminit vmemmap_alloc_block(unsigned long size, int node) 60 { 61 /* If the main allocator is up use that, fallback to bootmem. */ 62 if (slab_is_available()) { 63 gfp_t gfp_mask = GFP_KERNEL|__GFP_RETRY_MAYFAIL|__GFP_NOWARN; 64 int order = get_order(size); 65 static bool warned __meminitdata; 66 struct page *page; 67 68 page = alloc_pages_node(node, gfp_mask, order); 69 if (page) 70 return page_address(page); 71 72 if (!warned) { 73 warn_alloc(gfp_mask & ~__GFP_NOWARN, NULL, 74 "vmemmap alloc failure: order:%u", order); 75 warned = true; 76 } 77 return NULL; 78 } else 79 return __earlyonly_bootmem_alloc(node, size, size, 80 __pa(MAX_DMA_ADDRESS)); 81 } 82 83 static void * __meminit altmap_alloc_block_buf(unsigned long size, 84 struct vmem_altmap *altmap); 85 86 /* need to make sure size is all the same during early stage */ 87 void * __meminit vmemmap_alloc_block_buf(unsigned long size, int node, 88 struct vmem_altmap *altmap) 89 { 90 if (altmap) 91 return altmap_alloc_block_buf(size, altmap); 92 93 return vmemmap_alloc_block(size, node); 94 } 95 96 static unsigned long __meminit vmem_altmap_next_pfn(struct vmem_altmap *altmap) 97 { 98 return altmap->base_pfn + altmap->reserve + altmap->alloc 99 + altmap->align; 100 } 101 102 static unsigned long __meminit vmem_altmap_nr_free(struct vmem_altmap *altmap) 103 { 104 unsigned long allocated = altmap->alloc + altmap->align; 105 106 if (altmap->free > allocated) 107 return altmap->free - allocated; 108 return 0; 109 } 110 111 static void * __meminit altmap_alloc_block_buf(unsigned long size, 112 struct vmem_altmap *altmap) 113 { 114 unsigned long pfn, nr_pfns, nr_align; 115 116 if (size & ~PAGE_MASK) { 117 pr_warn_once("%s: allocations must be multiple of PAGE_SIZE (%ld)\n", 118 __func__, size); 119 return NULL; 120 } 121 122 pfn = vmem_altmap_next_pfn(altmap); 123 nr_pfns = size >> PAGE_SHIFT; 124 nr_align = 1UL << find_first_bit(&nr_pfns, BITS_PER_LONG); 125 nr_align = ALIGN(pfn, nr_align) - pfn; 126 if (nr_pfns + nr_align > vmem_altmap_nr_free(altmap)) 127 return NULL; 128 129 altmap->alloc += nr_pfns; 130 altmap->align += nr_align; 131 pfn += nr_align; 132 133 pr_debug("%s: pfn: %#lx alloc: %ld align: %ld nr: %#lx\n", 134 __func__, pfn, altmap->alloc, altmap->align, nr_pfns); 135 return __va(__pfn_to_phys(pfn)); 136 } 137 138 void __meminit vmemmap_verify(pte_t *pte, int node, 139 unsigned long start, unsigned long end) 140 { 141 unsigned long pfn = pte_pfn(ptep_get(pte)); 142 int actual_node = early_pfn_to_nid(pfn); 143 144 if (node_distance(actual_node, node) > LOCAL_DISTANCE) 145 pr_warn_once("[%lx-%lx] potential offnode page_structs\n", 146 start, end - 1); 147 } 148 149 static pte_t * __meminit vmemmap_pte_populate(pmd_t *pmd, unsigned long addr, int node, 150 struct vmem_altmap *altmap, 151 unsigned long ptpfn, unsigned long flags) 152 { 153 pte_t *pte = pte_offset_kernel(pmd, addr); 154 if (pte_none(ptep_get(pte))) { 155 pte_t entry; 156 void *p; 157 158 if (ptpfn == (unsigned long)-1) { 159 p = vmemmap_alloc_block_buf(PAGE_SIZE, node, altmap); 160 if (!p) 161 return NULL; 162 ptpfn = PHYS_PFN(__pa(p)); 163 } else { 164 /* 165 * When a PTE/PMD entry is freed from the init_mm 166 * there's a free_pages() call to this page allocated 167 * above. Thus this get_page() is paired with the 168 * put_page_testzero() on the freeing path. 169 * This can only called by certain ZONE_DEVICE path, 170 * and through vmemmap_populate_compound_pages() when 171 * slab is available. 172 */ 173 if (flags & VMEMMAP_POPULATE_PAGEREF) 174 get_page(pfn_to_page(ptpfn)); 175 } 176 entry = pfn_pte(ptpfn, PAGE_KERNEL); 177 set_pte_at(&init_mm, addr, pte, entry); 178 } 179 return pte; 180 } 181 182 static void * __meminit vmemmap_alloc_block_zero(unsigned long size, int node) 183 { 184 void *p = vmemmap_alloc_block(size, node); 185 186 if (!p) 187 return NULL; 188 memset(p, 0, size); 189 190 return p; 191 } 192 193 static pmd_t * __meminit vmemmap_pmd_populate(pud_t *pud, unsigned long addr, int node) 194 { 195 pmd_t *pmd = pmd_offset(pud, addr); 196 if (pmd_none(*pmd)) { 197 void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node); 198 if (!p) 199 return NULL; 200 kernel_pte_init(p); 201 pmd_populate_kernel(&init_mm, pmd, p); 202 } 203 return pmd; 204 } 205 206 static pud_t * __meminit vmemmap_pud_populate(p4d_t *p4d, unsigned long addr, int node) 207 { 208 pud_t *pud = pud_offset(p4d, addr); 209 if (pud_none(*pud)) { 210 void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node); 211 if (!p) 212 return NULL; 213 pmd_init(p); 214 pud_populate(&init_mm, pud, p); 215 } 216 return pud; 217 } 218 219 static p4d_t * __meminit vmemmap_p4d_populate(pgd_t *pgd, unsigned long addr, int node) 220 { 221 p4d_t *p4d = p4d_offset(pgd, addr); 222 if (p4d_none(*p4d)) { 223 void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node); 224 if (!p) 225 return NULL; 226 pud_init(p); 227 p4d_populate_kernel(addr, p4d, p); 228 } 229 return p4d; 230 } 231 232 static pgd_t * __meminit vmemmap_pgd_populate(unsigned long addr, int node) 233 { 234 pgd_t *pgd = pgd_offset_k(addr); 235 if (pgd_none(*pgd)) { 236 void *p = vmemmap_alloc_block_zero(PAGE_SIZE, node); 237 if (!p) 238 return NULL; 239 pgd_populate_kernel(addr, pgd, p); 240 } 241 return pgd; 242 } 243 244 static pte_t * __meminit vmemmap_populate_address(unsigned long addr, int node, 245 struct vmem_altmap *altmap, 246 unsigned long ptpfn, 247 unsigned long flags) 248 { 249 pgd_t *pgd; 250 p4d_t *p4d; 251 pud_t *pud; 252 pmd_t *pmd; 253 pte_t *pte; 254 255 pgd = vmemmap_pgd_populate(addr, node); 256 if (!pgd) 257 return NULL; 258 p4d = vmemmap_p4d_populate(pgd, addr, node); 259 if (!p4d) 260 return NULL; 261 pud = vmemmap_pud_populate(p4d, addr, node); 262 if (!pud) 263 return NULL; 264 pmd = vmemmap_pmd_populate(pud, addr, node); 265 if (!pmd) 266 return NULL; 267 pte = vmemmap_pte_populate(pmd, addr, node, altmap, ptpfn, flags); 268 if (!pte) 269 return NULL; 270 vmemmap_verify(pte, node, addr, addr + PAGE_SIZE); 271 272 return pte; 273 } 274 275 static int __meminit vmemmap_populate_range(unsigned long start, 276 unsigned long end, int node, 277 struct vmem_altmap *altmap, 278 unsigned long ptpfn, 279 unsigned long flags) 280 { 281 unsigned long addr = start; 282 pte_t *pte; 283 284 for (; addr < end; addr += PAGE_SIZE) { 285 pte = vmemmap_populate_address(addr, node, altmap, 286 ptpfn, flags); 287 if (!pte) 288 return -ENOMEM; 289 } 290 291 return 0; 292 } 293 294 int __meminit vmemmap_populate_basepages(unsigned long start, unsigned long end, 295 int node, struct vmem_altmap *altmap) 296 { 297 return vmemmap_populate_range(start, end, node, altmap, -1, 0); 298 } 299 300 /* 301 * Write protect the mirrored tail page structs for HVO. This will be 302 * called from the hugetlb code when gathering and initializing the 303 * memblock allocated gigantic pages. The write protect can't be 304 * done earlier, since it can't be guaranteed that the reserved 305 * page structures will not be written to during initialization, 306 * even if CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled. 307 * 308 * The PTEs are known to exist, and nothing else should be touching 309 * these pages. The caller is responsible for any TLB flushing. 310 */ 311 void vmemmap_wrprotect_hvo(unsigned long addr, unsigned long end, 312 int node, unsigned long headsize) 313 { 314 unsigned long maddr; 315 pte_t *pte; 316 317 for (maddr = addr + headsize; maddr < end; maddr += PAGE_SIZE) { 318 pte = virt_to_kpte(maddr); 319 ptep_set_wrprotect(&init_mm, maddr, pte); 320 } 321 } 322 323 #ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP 324 static __meminit struct page *vmemmap_get_tail(unsigned int order, struct zone *zone) 325 { 326 struct page *p, *tail; 327 unsigned int idx; 328 int node = zone_to_nid(zone); 329 330 if (WARN_ON_ONCE(order < VMEMMAP_TAIL_MIN_ORDER)) 331 return NULL; 332 if (WARN_ON_ONCE(order > MAX_FOLIO_ORDER)) 333 return NULL; 334 335 idx = order - VMEMMAP_TAIL_MIN_ORDER; 336 tail = zone->vmemmap_tails[idx]; 337 if (tail) 338 return tail; 339 340 /* 341 * Only allocate the page, but do not initialize it. 342 * 343 * Any initialization done here will be overwritten by memmap_init(). 344 * 345 * hugetlb_vmemmap_init() will take care of initialization after 346 * memmap_init(). 347 */ 348 349 p = vmemmap_alloc_block_zero(PAGE_SIZE, node); 350 if (!p) 351 return NULL; 352 353 tail = virt_to_page(p); 354 zone->vmemmap_tails[idx] = tail; 355 356 return tail; 357 } 358 359 int __meminit vmemmap_populate_hvo(unsigned long addr, unsigned long end, 360 unsigned int order, struct zone *zone, 361 unsigned long headsize) 362 { 363 unsigned long maddr; 364 struct page *tail; 365 pte_t *pte; 366 int node = zone_to_nid(zone); 367 368 tail = vmemmap_get_tail(order, zone); 369 if (!tail) 370 return -ENOMEM; 371 372 for (maddr = addr; maddr < addr + headsize; maddr += PAGE_SIZE) { 373 pte = vmemmap_populate_address(maddr, node, NULL, -1, 0); 374 if (!pte) 375 return -ENOMEM; 376 } 377 378 /* 379 * Reuse the last page struct page mapped above for the rest. 380 */ 381 return vmemmap_populate_range(maddr, end, node, NULL, 382 page_to_pfn(tail), 0); 383 } 384 #endif 385 386 void __weak __meminit vmemmap_set_pmd(pmd_t *pmd, void *p, int node, 387 unsigned long addr, unsigned long next) 388 { 389 WARN_ON_ONCE(!pmd_set_huge(pmd, virt_to_phys(p), PAGE_KERNEL)); 390 } 391 392 int __weak __meminit vmemmap_check_pmd(pmd_t *pmd, int node, 393 unsigned long addr, unsigned long next) 394 { 395 if (!pmd_leaf(pmdp_get(pmd))) 396 return 0; 397 vmemmap_verify((pte_t *)pmd, node, addr, next); 398 399 return 1; 400 } 401 402 int __meminit vmemmap_populate_hugepages(unsigned long start, unsigned long end, 403 int node, struct vmem_altmap *altmap) 404 { 405 unsigned long addr; 406 unsigned long next; 407 pgd_t *pgd; 408 p4d_t *p4d; 409 pud_t *pud; 410 pmd_t *pmd; 411 412 for (addr = start; addr < end; addr = next) { 413 next = pmd_addr_end(addr, end); 414 415 pgd = vmemmap_pgd_populate(addr, node); 416 if (!pgd) 417 return -ENOMEM; 418 419 p4d = vmemmap_p4d_populate(pgd, addr, node); 420 if (!p4d) 421 return -ENOMEM; 422 423 pud = vmemmap_pud_populate(p4d, addr, node); 424 if (!pud) 425 return -ENOMEM; 426 427 pmd = pmd_offset(pud, addr); 428 if (pmd_none(pmdp_get(pmd))) { 429 void *p; 430 431 p = vmemmap_alloc_block_buf(PMD_SIZE, node, altmap); 432 if (p) { 433 vmemmap_set_pmd(pmd, p, node, addr, next); 434 continue; 435 } else if (altmap) { 436 /* 437 * No fallback: In any case we care about, the 438 * altmap should be reasonably sized and aligned 439 * such that vmemmap_alloc_block_buf() will always 440 * succeed. For consistency with the PTE case, 441 * return an error here as failure could indicate 442 * a configuration issue with the size of the altmap. 443 */ 444 return -ENOMEM; 445 } 446 } else if (vmemmap_check_pmd(pmd, node, addr, next)) 447 continue; 448 if (vmemmap_populate_basepages(addr, next, node, altmap)) 449 return -ENOMEM; 450 } 451 return 0; 452 } 453 454 #ifndef vmemmap_populate_compound_pages 455 /* 456 * For compound pages bigger than section size (e.g. x86 1G compound 457 * pages with 2M subsection size) fill the rest of sections as tail 458 * pages. 459 * 460 * Note that memremap_pages() resets @nr_range value and will increment 461 * it after each range successful onlining. Thus the value or @nr_range 462 * at section memmap populate corresponds to the in-progress range 463 * being onlined here. 464 */ 465 static bool __meminit reuse_compound_section(unsigned long start_pfn, 466 struct dev_pagemap *pgmap) 467 { 468 unsigned long nr_pages = pgmap_vmemmap_nr(pgmap); 469 unsigned long offset = start_pfn - 470 PHYS_PFN(pgmap->ranges[pgmap->nr_range].start); 471 472 return !IS_ALIGNED(offset, nr_pages) && nr_pages > PAGES_PER_SUBSECTION; 473 } 474 475 static pte_t * __meminit compound_section_tail_page(unsigned long addr) 476 { 477 pte_t *pte; 478 479 addr -= PAGE_SIZE; 480 481 /* 482 * Assuming sections are populated sequentially, the previous section's 483 * page data can be reused. 484 */ 485 pte = pte_offset_kernel(pmd_off_k(addr), addr); 486 if (!pte) 487 return NULL; 488 489 return pte; 490 } 491 492 static int __meminit vmemmap_populate_compound_pages(unsigned long start_pfn, 493 unsigned long start, 494 unsigned long end, int node, 495 struct dev_pagemap *pgmap) 496 { 497 unsigned long size, addr; 498 pte_t *pte; 499 int rc; 500 501 if (reuse_compound_section(start_pfn, pgmap)) { 502 pte = compound_section_tail_page(start); 503 if (!pte) 504 return -ENOMEM; 505 506 /* 507 * Reuse the page that was populated in the prior iteration 508 * with just tail struct pages. 509 */ 510 return vmemmap_populate_range(start, end, node, NULL, 511 pte_pfn(ptep_get(pte)), 512 VMEMMAP_POPULATE_PAGEREF); 513 } 514 515 size = min(end - start, pgmap_vmemmap_nr(pgmap) * sizeof(struct page)); 516 for (addr = start; addr < end; addr += size) { 517 unsigned long next, last = addr + size; 518 519 /* Populate the head page vmemmap page */ 520 pte = vmemmap_populate_address(addr, node, NULL, -1, 0); 521 if (!pte) 522 return -ENOMEM; 523 524 /* Populate the tail pages vmemmap page */ 525 next = addr + PAGE_SIZE; 526 pte = vmemmap_populate_address(next, node, NULL, -1, 0); 527 if (!pte) 528 return -ENOMEM; 529 530 /* 531 * Reuse the previous page for the rest of tail pages 532 * See layout diagram in Documentation/mm/vmemmap_dedup.rst 533 */ 534 next += PAGE_SIZE; 535 rc = vmemmap_populate_range(next, last, node, NULL, 536 pte_pfn(ptep_get(pte)), 537 VMEMMAP_POPULATE_PAGEREF); 538 if (rc) 539 return -ENOMEM; 540 } 541 542 return 0; 543 } 544 545 #endif 546 547 struct page * __meminit __populate_section_memmap(unsigned long pfn, 548 unsigned long nr_pages, int nid, struct vmem_altmap *altmap, 549 struct dev_pagemap *pgmap) 550 { 551 unsigned long start = (unsigned long) pfn_to_page(pfn); 552 unsigned long end = start + nr_pages * sizeof(struct page); 553 int r; 554 555 if (WARN_ON_ONCE(!IS_ALIGNED(pfn, PAGES_PER_SUBSECTION) || 556 !IS_ALIGNED(nr_pages, PAGES_PER_SUBSECTION))) 557 return NULL; 558 559 if (vmemmap_can_optimize(altmap, pgmap)) 560 r = vmemmap_populate_compound_pages(pfn, start, end, nid, pgmap); 561 else 562 r = vmemmap_populate(start, end, nid, altmap); 563 564 if (r < 0) 565 return NULL; 566 567 flush_cache_vmap(start, end); 568 569 return pfn_to_page(pfn); 570 } 571 572 #ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT 573 /* 574 * This is called just before initializing sections for a NUMA node. 575 * Any special initialization that needs to be done before the 576 * generic initialization can be done from here. Sections that 577 * are initialized in hooks called from here will be skipped by 578 * the generic initialization. 579 */ 580 void __init sparse_vmemmap_init_nid_early(int nid) 581 { 582 hugetlb_vmemmap_init_early(nid); 583 } 584 585 /* 586 * This is called just before the initialization of page structures 587 * through memmap_init. Zones are now initialized, so any work that 588 * needs to be done that needs zone information can be done from 589 * here. 590 */ 591 void __init sparse_vmemmap_init_nid_late(int nid) 592 { 593 hugetlb_vmemmap_init_late(nid); 594 } 595 #endif 596 597 static void subsection_mask_set(unsigned long *map, unsigned long pfn, 598 unsigned long nr_pages) 599 { 600 int idx = subsection_map_index(pfn); 601 int end = subsection_map_index(pfn + nr_pages - 1); 602 603 bitmap_set(map, idx, end - idx + 1); 604 } 605 606 void __init sparse_init_subsection_map(unsigned long pfn, unsigned long nr_pages) 607 { 608 int end_sec_nr = pfn_to_section_nr(pfn + nr_pages - 1); 609 unsigned long nr, start_sec_nr = pfn_to_section_nr(pfn); 610 611 for (nr = start_sec_nr; nr <= end_sec_nr; nr++) { 612 struct mem_section *ms; 613 unsigned long pfns; 614 615 pfns = min(nr_pages, PAGES_PER_SECTION 616 - (pfn & ~PAGE_SECTION_MASK)); 617 ms = __nr_to_section(nr); 618 subsection_mask_set(ms->usage->subsection_map, pfn, pfns); 619 620 pr_debug("%s: sec: %lu pfns: %lu set(%d, %d)\n", __func__, nr, 621 pfns, subsection_map_index(pfn), 622 subsection_map_index(pfn + pfns - 1)); 623 624 pfn += pfns; 625 nr_pages -= pfns; 626 } 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