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 return pfn_to_page(pfn); 568 } 569 570 #ifdef CONFIG_SPARSEMEM_VMEMMAP_PREINIT 571 /* 572 * This is called just before initializing sections for a NUMA node. 573 * Any special initialization that needs to be done before the 574 * generic initialization can be done from here. Sections that 575 * are initialized in hooks called from here will be skipped by 576 * the generic initialization. 577 */ 578 void __init sparse_vmemmap_init_nid_early(int nid) 579 { 580 hugetlb_vmemmap_init_early(nid); 581 } 582 583 /* 584 * This is called just before the initialization of page structures 585 * through memmap_init. Zones are now initialized, so any work that 586 * needs to be done that needs zone information can be done from 587 * here. 588 */ 589 void __init sparse_vmemmap_init_nid_late(int nid) 590 { 591 hugetlb_vmemmap_init_late(nid); 592 } 593 #endif 594 595 static void subsection_mask_set(unsigned long *map, unsigned long pfn, 596 unsigned long nr_pages) 597 { 598 int idx = subsection_map_index(pfn); 599 int end = subsection_map_index(pfn + nr_pages - 1); 600 601 bitmap_set(map, idx, end - idx + 1); 602 } 603 604 void __init sparse_init_subsection_map(unsigned long pfn, unsigned long nr_pages) 605 { 606 int end_sec_nr = pfn_to_section_nr(pfn + nr_pages - 1); 607 unsigned long nr, start_sec_nr = pfn_to_section_nr(pfn); 608 609 for (nr = start_sec_nr; nr <= end_sec_nr; nr++) { 610 struct mem_section *ms; 611 unsigned long pfns; 612 613 pfns = min(nr_pages, PAGES_PER_SECTION 614 - (pfn & ~PAGE_SECTION_MASK)); 615 ms = __nr_to_section(nr); 616 subsection_mask_set(ms->usage->subsection_map, pfn, pfns); 617 618 pr_debug("%s: sec: %lu pfns: %lu set(%d, %d)\n", __func__, nr, 619 pfns, subsection_map_index(pfn), 620 subsection_map_index(pfn + pfns - 1)); 621 622 pfn += pfns; 623 nr_pages -= pfns; 624 } 625 } 626 627 #ifdef CONFIG_MEMORY_HOTPLUG 628 629 /* Mark all memory sections within the pfn range as online */ 630 void online_mem_sections(unsigned long start_pfn, unsigned long end_pfn) 631 { 632 unsigned long pfn; 633 634 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 635 unsigned long section_nr = pfn_to_section_nr(pfn); 636 struct mem_section *ms = __nr_to_section(section_nr); 637 638 ms->section_mem_map |= SECTION_IS_ONLINE; 639 } 640 } 641 642 /* Mark all memory sections within the pfn range as offline */ 643 void offline_mem_sections(unsigned long start_pfn, unsigned long end_pfn) 644 { 645 unsigned long pfn; 646 647 for (pfn = start_pfn; pfn < end_pfn; pfn += PAGES_PER_SECTION) { 648 unsigned long section_nr = pfn_to_section_nr(pfn); 649 struct mem_section *ms = __nr_to_section(section_nr); 650 651 ms->section_mem_map &= ~SECTION_IS_ONLINE; 652 } 653 } 654 655 static int __meminit section_nr_vmemmap_pages(unsigned long pfn, unsigned long nr_pages, 656 struct vmem_altmap *altmap, struct dev_pagemap *pgmap) 657 { 658 const unsigned int order = pgmap ? pgmap->vmemmap_shift : 0; 659 const unsigned long pages_per_compound = 1UL << order; 660 661 VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SUBSECTION)); 662 VM_WARN_ON_ONCE(nr_pages > PAGES_PER_SECTION); 663 664 if (!vmemmap_can_optimize(altmap, pgmap)) 665 return DIV_ROUND_UP(nr_pages * sizeof(struct page), PAGE_SIZE); 666 667 if (order < PFN_SECTION_SHIFT) { 668 VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, pages_per_compound)); 669 return VMEMMAP_RESERVE_NR * nr_pages / pages_per_compound; 670 } 671 672 VM_WARN_ON_ONCE(!IS_ALIGNED(pfn | nr_pages, PAGES_PER_SECTION)); 673 674 if (IS_ALIGNED(pfn, pages_per_compound)) 675 return VMEMMAP_RESERVE_NR; 676 677 return 0; 678 } 679 680 static struct page * __meminit populate_section_memmap(unsigned long pfn, 681 unsigned long nr_pages, int nid, struct vmem_altmap *altmap, 682 struct dev_pagemap *pgmap) 683 { 684 struct page *page = __populate_section_memmap(pfn, nr_pages, nid, altmap, 685 pgmap); 686 687 memmap_pages_add(section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap)); 688 689 return page; 690 } 691 692 static void depopulate_section_memmap(unsigned long pfn, unsigned long nr_pages, 693 struct vmem_altmap *altmap, struct dev_pagemap *pgmap) 694 { 695 unsigned long start = (unsigned long) pfn_to_page(pfn); 696 unsigned long end = start + nr_pages * sizeof(struct page); 697 698 memmap_pages_add(-section_nr_vmemmap_pages(pfn, nr_pages, altmap, pgmap)); 699 vmemmap_free(start, end, altmap); 700 } 701 702 static void free_map_bootmem(struct page *memmap) 703 { 704 unsigned long start = (unsigned long)memmap; 705 unsigned long end = (unsigned long)(memmap + PAGES_PER_SECTION); 706 unsigned long pfn = page_to_pfn(memmap); 707 708 memmap_boot_pages_add(-section_nr_vmemmap_pages(pfn, PAGES_PER_SECTION, 709 NULL, NULL)); 710 vmemmap_free(start, end, NULL); 711 } 712 713 static int clear_subsection_map(unsigned long pfn, unsigned long nr_pages) 714 { 715 DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 }; 716 DECLARE_BITMAP(tmp, SUBSECTIONS_PER_SECTION) = { 0 }; 717 struct mem_section *ms = __pfn_to_section(pfn); 718 unsigned long *subsection_map = ms->usage 719 ? &ms->usage->subsection_map[0] : NULL; 720 721 subsection_mask_set(map, pfn, nr_pages); 722 if (subsection_map) 723 bitmap_and(tmp, map, subsection_map, SUBSECTIONS_PER_SECTION); 724 725 if (WARN(!subsection_map || !bitmap_equal(tmp, map, SUBSECTIONS_PER_SECTION), 726 "section already deactivated (%#lx + %ld)\n", 727 pfn, nr_pages)) 728 return -EINVAL; 729 730 bitmap_xor(subsection_map, map, subsection_map, SUBSECTIONS_PER_SECTION); 731 return 0; 732 } 733 734 static bool is_subsection_map_empty(struct mem_section *ms) 735 { 736 return bitmap_empty(&ms->usage->subsection_map[0], 737 SUBSECTIONS_PER_SECTION); 738 } 739 740 static int fill_subsection_map(unsigned long pfn, unsigned long nr_pages) 741 { 742 struct mem_section *ms = __pfn_to_section(pfn); 743 DECLARE_BITMAP(map, SUBSECTIONS_PER_SECTION) = { 0 }; 744 unsigned long *subsection_map; 745 int rc = 0; 746 747 subsection_mask_set(map, pfn, nr_pages); 748 749 subsection_map = &ms->usage->subsection_map[0]; 750 751 if (bitmap_empty(map, SUBSECTIONS_PER_SECTION)) 752 rc = -EINVAL; 753 else if (bitmap_intersects(map, subsection_map, SUBSECTIONS_PER_SECTION)) 754 rc = -EEXIST; 755 else 756 bitmap_or(subsection_map, map, subsection_map, 757 SUBSECTIONS_PER_SECTION); 758 759 return rc; 760 } 761 762 /* 763 * To deactivate a memory region, there are 3 cases to handle: 764 * 765 * 1. deactivation of a partial hot-added section: 766 * a) section was present at memory init. 767 * b) section was hot-added post memory init. 768 * 2. deactivation of a complete hot-added section. 769 * 3. deactivation of a complete section from memory init. 770 * 771 * For 1, when subsection_map does not empty we will not be freeing the 772 * usage map, but still need to free the vmemmap range. 773 */ 774 static void section_deactivate(unsigned long pfn, unsigned long nr_pages, 775 struct vmem_altmap *altmap, struct dev_pagemap *pgmap) 776 { 777 struct mem_section *ms = __pfn_to_section(pfn); 778 bool section_is_early = early_section(ms); 779 struct page *memmap = NULL; 780 bool empty; 781 782 if (clear_subsection_map(pfn, nr_pages)) 783 return; 784 785 empty = is_subsection_map_empty(ms); 786 if (empty) { 787 /* 788 * Mark the section invalid so that valid_section() 789 * return false. This prevents code from dereferencing 790 * ms->usage array. 791 */ 792 ms->section_mem_map &= ~SECTION_HAS_MEM_MAP; 793 794 /* 795 * When removing an early section, the usage map is kept (as the 796 * usage maps of other sections fall into the same page). It 797 * will be re-used when re-adding the section - which is then no 798 * longer an early section. If the usage map is PageReserved, it 799 * was allocated during boot. 800 */ 801 if (!PageReserved(virt_to_page(ms->usage))) { 802 kfree_rcu(ms->usage, rcu); 803 WRITE_ONCE(ms->usage, NULL); 804 } 805 memmap = pfn_to_page(SECTION_ALIGN_DOWN(pfn)); 806 } 807 808 /* 809 * The memmap of early sections is always fully populated. See 810 * section_activate() and pfn_valid() . 811 */ 812 if (!section_is_early) 813 depopulate_section_memmap(pfn, nr_pages, altmap, pgmap); 814 else if (memmap) 815 free_map_bootmem(memmap); 816 817 if (empty) 818 ms->section_mem_map = (unsigned long)NULL; 819 } 820 821 static struct page * __meminit section_activate(int nid, unsigned long pfn, 822 unsigned long nr_pages, struct vmem_altmap *altmap, 823 struct dev_pagemap *pgmap) 824 { 825 struct mem_section *ms = __pfn_to_section(pfn); 826 struct mem_section_usage *usage = NULL; 827 struct page *memmap; 828 int rc; 829 830 if (!ms->usage) { 831 usage = kzalloc(mem_section_usage_size(), GFP_KERNEL); 832 if (!usage) 833 return ERR_PTR(-ENOMEM); 834 ms->usage = usage; 835 } 836 837 rc = fill_subsection_map(pfn, nr_pages); 838 if (rc) { 839 if (usage) 840 ms->usage = NULL; 841 kfree(usage); 842 return ERR_PTR(rc); 843 } 844 845 /* 846 * The early init code does not consider partially populated 847 * initial sections, it simply assumes that memory will never be 848 * referenced. If we hot-add memory into such a section then we 849 * do not need to populate the memmap and can simply reuse what 850 * is already there. 851 */ 852 if (nr_pages < PAGES_PER_SECTION && early_section(ms)) 853 return pfn_to_page(pfn); 854 855 memmap = populate_section_memmap(pfn, nr_pages, nid, altmap, pgmap); 856 if (!memmap) { 857 section_deactivate(pfn, nr_pages, altmap, pgmap); 858 return ERR_PTR(-ENOMEM); 859 } 860 861 return memmap; 862 } 863 864 /** 865 * sparse_add_section - add a memory section, or populate an existing one 866 * @nid: The node to add section on 867 * @start_pfn: start pfn of the memory range 868 * @nr_pages: number of pfns to add in the section 869 * @altmap: alternate pfns to allocate the memmap backing store 870 * @pgmap: alternate compound page geometry for devmap mappings 871 * 872 * This is only intended for hotplug. 873 * 874 * Note that only VMEMMAP supports sub-section aligned hotplug, 875 * the proper alignment and size are gated by check_pfn_span(). 876 * 877 * 878 * Return: 879 * * 0 - On success. 880 * * -EEXIST - Section has been present. 881 * * -ENOMEM - Out of memory. 882 */ 883 int __meminit sparse_add_section(int nid, unsigned long start_pfn, 884 unsigned long nr_pages, struct vmem_altmap *altmap, 885 struct dev_pagemap *pgmap) 886 { 887 unsigned long section_nr = pfn_to_section_nr(start_pfn); 888 struct mem_section *ms; 889 struct page *memmap; 890 int ret; 891 892 ret = sparse_index_init(section_nr, nid); 893 if (ret < 0) 894 return ret; 895 896 memmap = section_activate(nid, start_pfn, nr_pages, altmap, pgmap); 897 if (IS_ERR(memmap)) 898 return PTR_ERR(memmap); 899 900 /* 901 * Poison uninitialized struct pages in order to catch invalid flags 902 * combinations. 903 */ 904 page_init_poison(memmap, sizeof(struct page) * nr_pages); 905 906 ms = __nr_to_section(section_nr); 907 __section_mark_present(ms, section_nr); 908 909 /* Align memmap to section boundary in the subsection case */ 910 if (section_nr_to_pfn(section_nr) != start_pfn) 911 memmap = pfn_to_page(section_nr_to_pfn(section_nr)); 912 sparse_init_one_section(ms, section_nr, memmap, ms->usage, 0); 913 914 return 0; 915 } 916 917 void sparse_remove_section(unsigned long pfn, unsigned long nr_pages, 918 struct vmem_altmap *altmap, struct dev_pagemap *pgmap) 919 { 920 struct mem_section *ms = __pfn_to_section(pfn); 921 922 if (WARN_ON_ONCE(!valid_section(ms))) 923 return; 924 925 section_deactivate(pfn, nr_pages, altmap, pgmap); 926 } 927 #endif /* CONFIG_MEMORY_HOTPLUG */ 928