1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Procedures for maintaining information about logical memory blocks. 4 * 5 * Peter Bergner, IBM Corp. June 2001. 6 * Copyright (C) 2001 Peter Bergner. 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/slab.h> 11 #include <linux/init.h> 12 #include <linux/bitops.h> 13 #include <linux/poison.h> 14 #include <linux/pfn.h> 15 #include <linux/debugfs.h> 16 #include <linux/kmemleak.h> 17 #include <linux/seq_file.h> 18 #include <linux/memblock.h> 19 #include <linux/mutex.h> 20 21 #include <asm/sections.h> 22 #include <linux/io.h> 23 24 #include "internal.h" 25 26 #define INIT_MEMBLOCK_REGIONS 128 27 #define INIT_PHYSMEM_REGIONS 4 28 29 #ifndef INIT_MEMBLOCK_RESERVED_REGIONS 30 # define INIT_MEMBLOCK_RESERVED_REGIONS INIT_MEMBLOCK_REGIONS 31 #endif 32 33 #ifndef INIT_MEMBLOCK_MEMORY_REGIONS 34 #define INIT_MEMBLOCK_MEMORY_REGIONS INIT_MEMBLOCK_REGIONS 35 #endif 36 37 /** 38 * DOC: memblock overview 39 * 40 * Memblock is a method of managing memory regions during the early 41 * boot period when the usual kernel memory allocators are not up and 42 * running. 43 * 44 * Memblock views the system memory as collections of contiguous 45 * regions. There are several types of these collections: 46 * 47 * * ``memory`` - describes the physical memory available to the 48 * kernel; this may differ from the actual physical memory installed 49 * in the system, for instance when the memory is restricted with 50 * ``mem=`` command line parameter 51 * * ``reserved`` - describes the regions that were allocated 52 * * ``physmem`` - describes the actual physical memory available during 53 * boot regardless of the possible restrictions and memory hot(un)plug; 54 * the ``physmem`` type is only available on some architectures. 55 * 56 * Each region is represented by struct memblock_region that 57 * defines the region extents, its attributes and NUMA node id on NUMA 58 * systems. Every memory type is described by the struct memblock_type 59 * which contains an array of memory regions along with 60 * the allocator metadata. The "memory" and "reserved" types are nicely 61 * wrapped with struct memblock. This structure is statically 62 * initialized at build time. The region arrays are initially sized to 63 * %INIT_MEMBLOCK_MEMORY_REGIONS for "memory" and 64 * %INIT_MEMBLOCK_RESERVED_REGIONS for "reserved". The region array 65 * for "physmem" is initially sized to %INIT_PHYSMEM_REGIONS. 66 * The memblock_allow_resize() enables automatic resizing of the region 67 * arrays during addition of new regions. This feature should be used 68 * with care so that memory allocated for the region array will not 69 * overlap with areas that should be reserved, for example initrd. 70 * 71 * The early architecture setup should tell memblock what the physical 72 * memory layout is by using memblock_add() or memblock_add_node() 73 * functions. The first function does not assign the region to a NUMA 74 * node and it is appropriate for UMA systems. Yet, it is possible to 75 * use it on NUMA systems as well and assign the region to a NUMA node 76 * later in the setup process using memblock_set_node(). The 77 * memblock_add_node() performs such an assignment directly. 78 * 79 * Once memblock is setup the memory can be allocated using one of the 80 * API variants: 81 * 82 * * memblock_phys_alloc*() - these functions return the **physical** 83 * address of the allocated memory 84 * * memblock_alloc*() - these functions return the **virtual** address 85 * of the allocated memory. 86 * 87 * Note, that both API variants use implicit assumptions about allowed 88 * memory ranges and the fallback methods. Consult the documentation 89 * of memblock_alloc_internal() and memblock_alloc_range_nid() 90 * functions for more elaborate description. 91 * 92 * As the system boot progresses, the architecture specific mem_init() 93 * function frees all the memory to the buddy page allocator. 94 * 95 * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the 96 * memblock data structures (except "physmem") will be discarded after the 97 * system initialization completes. 98 */ 99 100 #ifndef CONFIG_NUMA 101 struct pglist_data __refdata contig_page_data; 102 EXPORT_SYMBOL(contig_page_data); 103 #endif 104 105 unsigned long max_low_pfn; 106 unsigned long min_low_pfn; 107 unsigned long max_pfn; 108 unsigned long long max_possible_pfn; 109 110 static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_MEMORY_REGIONS] __initdata_memblock; 111 static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock; 112 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 113 static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS]; 114 #endif 115 116 struct memblock memblock __initdata_memblock = { 117 .memory.regions = memblock_memory_init_regions, 118 .memory.max = INIT_MEMBLOCK_MEMORY_REGIONS, 119 .memory.name = "memory", 120 121 .reserved.regions = memblock_reserved_init_regions, 122 .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS, 123 .reserved.name = "reserved", 124 125 .bottom_up = false, 126 .current_limit = MEMBLOCK_ALLOC_ANYWHERE, 127 }; 128 129 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 130 struct memblock_type physmem = { 131 .regions = memblock_physmem_init_regions, 132 .max = INIT_PHYSMEM_REGIONS, 133 .name = "physmem", 134 }; 135 #endif 136 137 /* 138 * keep a pointer to &memblock.memory in the text section to use it in 139 * __next_mem_range() and its helpers. 140 * For architectures that do not keep memblock data after init, this 141 * pointer will be reset to NULL at memblock_discard() 142 */ 143 static __refdata struct memblock_type *memblock_memory = &memblock.memory; 144 145 #define for_each_memblock_type(i, memblock_type, rgn) \ 146 for (i = 0, rgn = &memblock_type->regions[0]; \ 147 i < memblock_type->cnt; \ 148 i++, rgn = &memblock_type->regions[i]) 149 150 #define memblock_dbg(fmt, ...) \ 151 do { \ 152 if (memblock_debug) \ 153 pr_info(fmt, ##__VA_ARGS__); \ 154 } while (0) 155 156 static int memblock_debug __initdata_memblock; 157 static bool system_has_some_mirror __initdata_memblock; 158 static int memblock_can_resize __initdata_memblock; 159 static int memblock_memory_in_slab __initdata_memblock; 160 static int memblock_reserved_in_slab __initdata_memblock; 161 162 bool __init_memblock memblock_has_mirror(void) 163 { 164 return system_has_some_mirror; 165 } 166 167 static enum memblock_flags __init_memblock choose_memblock_flags(void) 168 { 169 return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE; 170 } 171 172 /* adjust *@size so that (@base + *@size) doesn't overflow, return new size */ 173 static inline phys_addr_t memblock_cap_size(phys_addr_t base, phys_addr_t *size) 174 { 175 return *size = min(*size, PHYS_ADDR_MAX - base); 176 } 177 178 /* 179 * Address comparison utilities 180 */ 181 unsigned long __init_memblock 182 memblock_addrs_overlap(phys_addr_t base1, phys_addr_t size1, phys_addr_t base2, 183 phys_addr_t size2) 184 { 185 return ((base1 < (base2 + size2)) && (base2 < (base1 + size1))); 186 } 187 188 bool __init_memblock memblock_overlaps_region(struct memblock_type *type, 189 phys_addr_t base, phys_addr_t size) 190 { 191 unsigned long i; 192 193 memblock_cap_size(base, &size); 194 195 for (i = 0; i < type->cnt; i++) 196 if (memblock_addrs_overlap(base, size, type->regions[i].base, 197 type->regions[i].size)) 198 return true; 199 return false; 200 } 201 202 /** 203 * __memblock_find_range_bottom_up - find free area utility in bottom-up 204 * @start: start of candidate range 205 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 206 * %MEMBLOCK_ALLOC_ACCESSIBLE 207 * @size: size of free area to find 208 * @align: alignment of free area to find 209 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 210 * @flags: pick from blocks based on memory attributes 211 * 212 * Utility called from memblock_find_in_range_node(), find free area bottom-up. 213 * 214 * Return: 215 * Found address on success, 0 on failure. 216 */ 217 static phys_addr_t __init_memblock 218 __memblock_find_range_bottom_up(phys_addr_t start, phys_addr_t end, 219 phys_addr_t size, phys_addr_t align, int nid, 220 enum memblock_flags flags) 221 { 222 phys_addr_t this_start, this_end, cand; 223 u64 i; 224 225 for_each_free_mem_range(i, nid, flags, &this_start, &this_end, NULL) { 226 this_start = clamp(this_start, start, end); 227 this_end = clamp(this_end, start, end); 228 229 cand = round_up(this_start, align); 230 if (cand < this_end && this_end - cand >= size) 231 return cand; 232 } 233 234 return 0; 235 } 236 237 /** 238 * __memblock_find_range_top_down - find free area utility, in top-down 239 * @start: start of candidate range 240 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 241 * %MEMBLOCK_ALLOC_ACCESSIBLE 242 * @size: size of free area to find 243 * @align: alignment of free area to find 244 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 245 * @flags: pick from blocks based on memory attributes 246 * 247 * Utility called from memblock_find_in_range_node(), find free area top-down. 248 * 249 * Return: 250 * Found address on success, 0 on failure. 251 */ 252 static phys_addr_t __init_memblock 253 __memblock_find_range_top_down(phys_addr_t start, phys_addr_t end, 254 phys_addr_t size, phys_addr_t align, int nid, 255 enum memblock_flags flags) 256 { 257 phys_addr_t this_start, this_end, cand; 258 u64 i; 259 260 for_each_free_mem_range_reverse(i, nid, flags, &this_start, &this_end, 261 NULL) { 262 this_start = clamp(this_start, start, end); 263 this_end = clamp(this_end, start, end); 264 265 if (this_end < size) 266 continue; 267 268 cand = round_down(this_end - size, align); 269 if (cand >= this_start) 270 return cand; 271 } 272 273 return 0; 274 } 275 276 /** 277 * memblock_find_in_range_node - find free area in given range and node 278 * @size: size of free area to find 279 * @align: alignment of free area to find 280 * @start: start of candidate range 281 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 282 * %MEMBLOCK_ALLOC_ACCESSIBLE 283 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 284 * @flags: pick from blocks based on memory attributes 285 * 286 * Find @size free area aligned to @align in the specified range and node. 287 * 288 * Return: 289 * Found address on success, 0 on failure. 290 */ 291 static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size, 292 phys_addr_t align, phys_addr_t start, 293 phys_addr_t end, int nid, 294 enum memblock_flags flags) 295 { 296 /* pump up @end */ 297 if (end == MEMBLOCK_ALLOC_ACCESSIBLE || 298 end == MEMBLOCK_ALLOC_NOLEAKTRACE) 299 end = memblock.current_limit; 300 301 /* avoid allocating the first page */ 302 start = max_t(phys_addr_t, start, PAGE_SIZE); 303 end = max(start, end); 304 305 if (memblock_bottom_up()) 306 return __memblock_find_range_bottom_up(start, end, size, align, 307 nid, flags); 308 else 309 return __memblock_find_range_top_down(start, end, size, align, 310 nid, flags); 311 } 312 313 /** 314 * memblock_find_in_range - find free area in given range 315 * @start: start of candidate range 316 * @end: end of candidate range, can be %MEMBLOCK_ALLOC_ANYWHERE or 317 * %MEMBLOCK_ALLOC_ACCESSIBLE 318 * @size: size of free area to find 319 * @align: alignment of free area to find 320 * 321 * Find @size free area aligned to @align in the specified range. 322 * 323 * Return: 324 * Found address on success, 0 on failure. 325 */ 326 static phys_addr_t __init_memblock memblock_find_in_range(phys_addr_t start, 327 phys_addr_t end, phys_addr_t size, 328 phys_addr_t align) 329 { 330 phys_addr_t ret; 331 enum memblock_flags flags = choose_memblock_flags(); 332 333 again: 334 ret = memblock_find_in_range_node(size, align, start, end, 335 NUMA_NO_NODE, flags); 336 337 if (!ret && (flags & MEMBLOCK_MIRROR)) { 338 pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n", 339 &size); 340 flags &= ~MEMBLOCK_MIRROR; 341 goto again; 342 } 343 344 return ret; 345 } 346 347 static void __init_memblock memblock_remove_region(struct memblock_type *type, unsigned long r) 348 { 349 type->total_size -= type->regions[r].size; 350 memmove(&type->regions[r], &type->regions[r + 1], 351 (type->cnt - (r + 1)) * sizeof(type->regions[r])); 352 type->cnt--; 353 354 /* Special case for empty arrays */ 355 if (type->cnt == 0) { 356 WARN_ON(type->total_size != 0); 357 type->regions[0].base = 0; 358 type->regions[0].size = 0; 359 type->regions[0].flags = 0; 360 memblock_set_region_node(&type->regions[0], MAX_NUMNODES); 361 } 362 } 363 364 #ifndef CONFIG_ARCH_KEEP_MEMBLOCK 365 /** 366 * memblock_discard - discard memory and reserved arrays if they were allocated 367 */ 368 void __init memblock_discard(void) 369 { 370 phys_addr_t addr, size; 371 372 if (memblock.reserved.regions != memblock_reserved_init_regions) { 373 addr = __pa(memblock.reserved.regions); 374 size = PAGE_ALIGN(sizeof(struct memblock_region) * 375 memblock.reserved.max); 376 if (memblock_reserved_in_slab) 377 kfree(memblock.reserved.regions); 378 else 379 memblock_free_late(addr, size); 380 } 381 382 if (memblock.memory.regions != memblock_memory_init_regions) { 383 addr = __pa(memblock.memory.regions); 384 size = PAGE_ALIGN(sizeof(struct memblock_region) * 385 memblock.memory.max); 386 if (memblock_memory_in_slab) 387 kfree(memblock.memory.regions); 388 else 389 memblock_free_late(addr, size); 390 } 391 392 memblock_memory = NULL; 393 } 394 #endif 395 396 /** 397 * memblock_double_array - double the size of the memblock regions array 398 * @type: memblock type of the regions array being doubled 399 * @new_area_start: starting address of memory range to avoid overlap with 400 * @new_area_size: size of memory range to avoid overlap with 401 * 402 * Double the size of the @type regions array. If memblock is being used to 403 * allocate memory for a new reserved regions array and there is a previously 404 * allocated memory range [@new_area_start, @new_area_start + @new_area_size] 405 * waiting to be reserved, ensure the memory used by the new array does 406 * not overlap. 407 * 408 * Return: 409 * 0 on success, -1 on failure. 410 */ 411 static int __init_memblock memblock_double_array(struct memblock_type *type, 412 phys_addr_t new_area_start, 413 phys_addr_t new_area_size) 414 { 415 struct memblock_region *new_array, *old_array; 416 phys_addr_t old_alloc_size, new_alloc_size; 417 phys_addr_t old_size, new_size, addr, new_end; 418 int use_slab = slab_is_available(); 419 int *in_slab; 420 421 /* We don't allow resizing until we know about the reserved regions 422 * of memory that aren't suitable for allocation 423 */ 424 if (!memblock_can_resize) 425 panic("memblock: cannot resize %s array\n", type->name); 426 427 /* Calculate new doubled size */ 428 old_size = type->max * sizeof(struct memblock_region); 429 new_size = old_size << 1; 430 /* 431 * We need to allocated new one align to PAGE_SIZE, 432 * so we can free them completely later. 433 */ 434 old_alloc_size = PAGE_ALIGN(old_size); 435 new_alloc_size = PAGE_ALIGN(new_size); 436 437 /* Retrieve the slab flag */ 438 if (type == &memblock.memory) 439 in_slab = &memblock_memory_in_slab; 440 else 441 in_slab = &memblock_reserved_in_slab; 442 443 /* Try to find some space for it */ 444 if (use_slab) { 445 new_array = kmalloc(new_size, GFP_KERNEL); 446 addr = new_array ? __pa(new_array) : 0; 447 } else { 448 /* only exclude range when trying to double reserved.regions */ 449 if (type != &memblock.reserved) 450 new_area_start = new_area_size = 0; 451 452 addr = memblock_find_in_range(new_area_start + new_area_size, 453 memblock.current_limit, 454 new_alloc_size, PAGE_SIZE); 455 if (!addr && new_area_size) 456 addr = memblock_find_in_range(0, 457 min(new_area_start, memblock.current_limit), 458 new_alloc_size, PAGE_SIZE); 459 460 new_array = addr ? __va(addr) : NULL; 461 } 462 if (!addr) { 463 pr_err("memblock: Failed to double %s array from %ld to %ld entries !\n", 464 type->name, type->max, type->max * 2); 465 return -1; 466 } 467 468 new_end = addr + new_size - 1; 469 memblock_dbg("memblock: %s is doubled to %ld at [%pa-%pa]", 470 type->name, type->max * 2, &addr, &new_end); 471 472 /* 473 * Found space, we now need to move the array over before we add the 474 * reserved region since it may be our reserved array itself that is 475 * full. 476 */ 477 memcpy(new_array, type->regions, old_size); 478 memset(new_array + type->max, 0, old_size); 479 old_array = type->regions; 480 type->regions = new_array; 481 type->max <<= 1; 482 483 /* Free old array. We needn't free it if the array is the static one */ 484 if (*in_slab) 485 kfree(old_array); 486 else if (old_array != memblock_memory_init_regions && 487 old_array != memblock_reserved_init_regions) 488 memblock_free(old_array, old_alloc_size); 489 490 /* 491 * Reserve the new array if that comes from the memblock. Otherwise, we 492 * needn't do it 493 */ 494 if (!use_slab) 495 BUG_ON(memblock_reserve(addr, new_alloc_size)); 496 497 /* Update slab flag */ 498 *in_slab = use_slab; 499 500 return 0; 501 } 502 503 /** 504 * memblock_merge_regions - merge neighboring compatible regions 505 * @type: memblock type to scan 506 * @start_rgn: start scanning from (@start_rgn - 1) 507 * @end_rgn: end scanning at (@end_rgn - 1) 508 * Scan @type and merge neighboring compatible regions in [@start_rgn - 1, @end_rgn) 509 */ 510 static void __init_memblock memblock_merge_regions(struct memblock_type *type, 511 unsigned long start_rgn, 512 unsigned long end_rgn) 513 { 514 int i = 0; 515 if (start_rgn) 516 i = start_rgn - 1; 517 end_rgn = min(end_rgn, type->cnt - 1); 518 while (i < end_rgn) { 519 struct memblock_region *this = &type->regions[i]; 520 struct memblock_region *next = &type->regions[i + 1]; 521 522 if (this->base + this->size != next->base || 523 memblock_get_region_node(this) != 524 memblock_get_region_node(next) || 525 this->flags != next->flags) { 526 BUG_ON(this->base + this->size > next->base); 527 i++; 528 continue; 529 } 530 531 this->size += next->size; 532 /* move forward from next + 1, index of which is i + 2 */ 533 memmove(next, next + 1, (type->cnt - (i + 2)) * sizeof(*next)); 534 type->cnt--; 535 end_rgn--; 536 } 537 } 538 539 /** 540 * memblock_insert_region - insert new memblock region 541 * @type: memblock type to insert into 542 * @idx: index for the insertion point 543 * @base: base address of the new region 544 * @size: size of the new region 545 * @nid: node id of the new region 546 * @flags: flags of the new region 547 * 548 * Insert new memblock region [@base, @base + @size) into @type at @idx. 549 * @type must already have extra room to accommodate the new region. 550 */ 551 static void __init_memblock memblock_insert_region(struct memblock_type *type, 552 int idx, phys_addr_t base, 553 phys_addr_t size, 554 int nid, 555 enum memblock_flags flags) 556 { 557 struct memblock_region *rgn = &type->regions[idx]; 558 559 BUG_ON(type->cnt >= type->max); 560 memmove(rgn + 1, rgn, (type->cnt - idx) * sizeof(*rgn)); 561 rgn->base = base; 562 rgn->size = size; 563 rgn->flags = flags; 564 memblock_set_region_node(rgn, nid); 565 type->cnt++; 566 type->total_size += size; 567 } 568 569 /** 570 * memblock_add_range - add new memblock region 571 * @type: memblock type to add new region into 572 * @base: base address of the new region 573 * @size: size of the new region 574 * @nid: nid of the new region 575 * @flags: flags of the new region 576 * 577 * Add new memblock region [@base, @base + @size) into @type. The new region 578 * is allowed to overlap with existing ones - overlaps don't affect already 579 * existing regions. @type is guaranteed to be minimal (all neighbouring 580 * compatible regions are merged) after the addition. 581 * 582 * Return: 583 * 0 on success, -errno on failure. 584 */ 585 static int __init_memblock memblock_add_range(struct memblock_type *type, 586 phys_addr_t base, phys_addr_t size, 587 int nid, enum memblock_flags flags) 588 { 589 bool insert = false; 590 phys_addr_t obase = base; 591 phys_addr_t end = base + memblock_cap_size(base, &size); 592 int idx, nr_new, start_rgn = -1, end_rgn; 593 struct memblock_region *rgn; 594 595 if (!size) 596 return 0; 597 598 /* special case for empty array */ 599 if (type->regions[0].size == 0) { 600 WARN_ON(type->cnt != 0 || type->total_size); 601 type->regions[0].base = base; 602 type->regions[0].size = size; 603 type->regions[0].flags = flags; 604 memblock_set_region_node(&type->regions[0], nid); 605 type->total_size = size; 606 type->cnt = 1; 607 return 0; 608 } 609 610 /* 611 * The worst case is when new range overlaps all existing regions, 612 * then we'll need type->cnt + 1 empty regions in @type. So if 613 * type->cnt * 2 + 1 is less than or equal to type->max, we know 614 * that there is enough empty regions in @type, and we can insert 615 * regions directly. 616 */ 617 if (type->cnt * 2 + 1 <= type->max) 618 insert = true; 619 620 repeat: 621 /* 622 * The following is executed twice. Once with %false @insert and 623 * then with %true. The first counts the number of regions needed 624 * to accommodate the new area. The second actually inserts them. 625 */ 626 base = obase; 627 nr_new = 0; 628 629 for_each_memblock_type(idx, type, rgn) { 630 phys_addr_t rbase = rgn->base; 631 phys_addr_t rend = rbase + rgn->size; 632 633 if (rbase >= end) 634 break; 635 if (rend <= base) 636 continue; 637 /* 638 * @rgn overlaps. If it separates the lower part of new 639 * area, insert that portion. 640 */ 641 if (rbase > base) { 642 #ifdef CONFIG_NUMA 643 WARN_ON(nid != memblock_get_region_node(rgn)); 644 #endif 645 WARN_ON(flags != rgn->flags); 646 nr_new++; 647 if (insert) { 648 if (start_rgn == -1) 649 start_rgn = idx; 650 end_rgn = idx + 1; 651 memblock_insert_region(type, idx++, base, 652 rbase - base, nid, 653 flags); 654 } 655 } 656 /* area below @rend is dealt with, forget about it */ 657 base = min(rend, end); 658 } 659 660 /* insert the remaining portion */ 661 if (base < end) { 662 nr_new++; 663 if (insert) { 664 if (start_rgn == -1) 665 start_rgn = idx; 666 end_rgn = idx + 1; 667 memblock_insert_region(type, idx, base, end - base, 668 nid, flags); 669 } 670 } 671 672 if (!nr_new) 673 return 0; 674 675 /* 676 * If this was the first round, resize array and repeat for actual 677 * insertions; otherwise, merge and return. 678 */ 679 if (!insert) { 680 while (type->cnt + nr_new > type->max) 681 if (memblock_double_array(type, obase, size) < 0) 682 return -ENOMEM; 683 insert = true; 684 goto repeat; 685 } else { 686 memblock_merge_regions(type, start_rgn, end_rgn); 687 return 0; 688 } 689 } 690 691 /** 692 * memblock_add_node - add new memblock region within a NUMA node 693 * @base: base address of the new region 694 * @size: size of the new region 695 * @nid: nid of the new region 696 * @flags: flags of the new region 697 * 698 * Add new memblock region [@base, @base + @size) to the "memory" 699 * type. See memblock_add_range() description for mode details 700 * 701 * Return: 702 * 0 on success, -errno on failure. 703 */ 704 int __init_memblock memblock_add_node(phys_addr_t base, phys_addr_t size, 705 int nid, enum memblock_flags flags) 706 { 707 phys_addr_t end = base + size - 1; 708 709 memblock_dbg("%s: [%pa-%pa] nid=%d flags=%x %pS\n", __func__, 710 &base, &end, nid, flags, (void *)_RET_IP_); 711 712 return memblock_add_range(&memblock.memory, base, size, nid, flags); 713 } 714 715 /** 716 * memblock_add - add new memblock region 717 * @base: base address of the new region 718 * @size: size of the new region 719 * 720 * Add new memblock region [@base, @base + @size) to the "memory" 721 * type. See memblock_add_range() description for mode details 722 * 723 * Return: 724 * 0 on success, -errno on failure. 725 */ 726 int __init_memblock memblock_add(phys_addr_t base, phys_addr_t size) 727 { 728 phys_addr_t end = base + size - 1; 729 730 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 731 &base, &end, (void *)_RET_IP_); 732 733 return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0); 734 } 735 736 /** 737 * memblock_validate_numa_coverage - check if amount of memory with 738 * no node ID assigned is less than a threshold 739 * @threshold_bytes: maximal memory size that can have unassigned node 740 * ID (in bytes). 741 * 742 * A buggy firmware may report memory that does not belong to any node. 743 * Check if amount of such memory is below @threshold_bytes. 744 * 745 * Return: true on success, false on failure. 746 */ 747 bool __init_memblock memblock_validate_numa_coverage(unsigned long threshold_bytes) 748 { 749 unsigned long nr_pages = 0; 750 unsigned long start_pfn, end_pfn, mem_size_mb; 751 int nid, i; 752 753 /* calculate lose page */ 754 for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { 755 if (!numa_valid_node(nid)) 756 nr_pages += end_pfn - start_pfn; 757 } 758 759 if ((nr_pages << PAGE_SHIFT) > threshold_bytes) { 760 mem_size_mb = memblock_phys_mem_size() >> 20; 761 pr_err("NUMA: no nodes coverage for %luMB of %luMB RAM\n", 762 (nr_pages << PAGE_SHIFT) >> 20, mem_size_mb); 763 return false; 764 } 765 766 return true; 767 } 768 769 770 /** 771 * memblock_isolate_range - isolate given range into disjoint memblocks 772 * @type: memblock type to isolate range for 773 * @base: base of range to isolate 774 * @size: size of range to isolate 775 * @start_rgn: out parameter for the start of isolated region 776 * @end_rgn: out parameter for the end of isolated region 777 * 778 * Walk @type and ensure that regions don't cross the boundaries defined by 779 * [@base, @base + @size). Crossing regions are split at the boundaries, 780 * which may create at most two more regions. The index of the first 781 * region inside the range is returned in *@start_rgn and the index of the 782 * first region after the range is returned in *@end_rgn. 783 * 784 * Return: 785 * 0 on success, -errno on failure. 786 */ 787 static int __init_memblock memblock_isolate_range(struct memblock_type *type, 788 phys_addr_t base, phys_addr_t size, 789 int *start_rgn, int *end_rgn) 790 { 791 phys_addr_t end = base + memblock_cap_size(base, &size); 792 int idx; 793 struct memblock_region *rgn; 794 795 *start_rgn = *end_rgn = 0; 796 797 if (!size) 798 return 0; 799 800 /* we'll create at most two more regions */ 801 while (type->cnt + 2 > type->max) 802 if (memblock_double_array(type, base, size) < 0) 803 return -ENOMEM; 804 805 for_each_memblock_type(idx, type, rgn) { 806 phys_addr_t rbase = rgn->base; 807 phys_addr_t rend = rbase + rgn->size; 808 809 if (rbase >= end) 810 break; 811 if (rend <= base) 812 continue; 813 814 if (rbase < base) { 815 /* 816 * @rgn intersects from below. Split and continue 817 * to process the next region - the new top half. 818 */ 819 rgn->base = base; 820 rgn->size -= base - rbase; 821 type->total_size -= base - rbase; 822 memblock_insert_region(type, idx, rbase, base - rbase, 823 memblock_get_region_node(rgn), 824 rgn->flags); 825 } else if (rend > end) { 826 /* 827 * @rgn intersects from above. Split and redo the 828 * current region - the new bottom half. 829 */ 830 rgn->base = end; 831 rgn->size -= end - rbase; 832 type->total_size -= end - rbase; 833 memblock_insert_region(type, idx--, rbase, end - rbase, 834 memblock_get_region_node(rgn), 835 rgn->flags); 836 } else { 837 /* @rgn is fully contained, record it */ 838 if (!*end_rgn) 839 *start_rgn = idx; 840 *end_rgn = idx + 1; 841 } 842 } 843 844 return 0; 845 } 846 847 static int __init_memblock memblock_remove_range(struct memblock_type *type, 848 phys_addr_t base, phys_addr_t size) 849 { 850 int start_rgn, end_rgn; 851 int i, ret; 852 853 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 854 if (ret) 855 return ret; 856 857 for (i = end_rgn - 1; i >= start_rgn; i--) 858 memblock_remove_region(type, i); 859 return 0; 860 } 861 862 int __init_memblock memblock_remove(phys_addr_t base, phys_addr_t size) 863 { 864 phys_addr_t end = base + size - 1; 865 866 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 867 &base, &end, (void *)_RET_IP_); 868 869 return memblock_remove_range(&memblock.memory, base, size); 870 } 871 872 /** 873 * memblock_free - free boot memory allocation 874 * @ptr: starting address of the boot memory allocation 875 * @size: size of the boot memory block in bytes 876 * 877 * Free boot memory block previously allocated by memblock_alloc_xx() API. 878 * The freeing memory will not be released to the buddy allocator. 879 */ 880 void __init_memblock memblock_free(void *ptr, size_t size) 881 { 882 if (ptr) 883 memblock_phys_free(__pa(ptr), size); 884 } 885 886 /** 887 * memblock_phys_free - free boot memory block 888 * @base: phys starting address of the boot memory block 889 * @size: size of the boot memory block in bytes 890 * 891 * Free boot memory block previously allocated by memblock_phys_alloc_xx() API. 892 * The freeing memory will not be released to the buddy allocator. 893 */ 894 int __init_memblock memblock_phys_free(phys_addr_t base, phys_addr_t size) 895 { 896 phys_addr_t end = base + size - 1; 897 898 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 899 &base, &end, (void *)_RET_IP_); 900 901 kmemleak_free_part_phys(base, size); 902 return memblock_remove_range(&memblock.reserved, base, size); 903 } 904 905 int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size) 906 { 907 phys_addr_t end = base + size - 1; 908 909 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 910 &base, &end, (void *)_RET_IP_); 911 912 return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0); 913 } 914 915 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 916 int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size) 917 { 918 phys_addr_t end = base + size - 1; 919 920 memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, 921 &base, &end, (void *)_RET_IP_); 922 923 return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0); 924 } 925 #endif 926 927 /** 928 * memblock_setclr_flag - set or clear flag for a memory region 929 * @type: memblock type to set/clear flag for 930 * @base: base address of the region 931 * @size: size of the region 932 * @set: set or clear the flag 933 * @flag: the flag to update 934 * 935 * This function isolates region [@base, @base + @size), and sets/clears flag 936 * 937 * Return: 0 on success, -errno on failure. 938 */ 939 static int __init_memblock memblock_setclr_flag(struct memblock_type *type, 940 phys_addr_t base, phys_addr_t size, int set, int flag) 941 { 942 int i, ret, start_rgn, end_rgn; 943 944 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 945 if (ret) 946 return ret; 947 948 for (i = start_rgn; i < end_rgn; i++) { 949 struct memblock_region *r = &type->regions[i]; 950 951 if (set) 952 r->flags |= flag; 953 else 954 r->flags &= ~flag; 955 } 956 957 memblock_merge_regions(type, start_rgn, end_rgn); 958 return 0; 959 } 960 961 /** 962 * memblock_mark_hotplug - Mark hotpluggable memory with flag MEMBLOCK_HOTPLUG. 963 * @base: the base phys addr of the region 964 * @size: the size of the region 965 * 966 * Return: 0 on success, -errno on failure. 967 */ 968 int __init_memblock memblock_mark_hotplug(phys_addr_t base, phys_addr_t size) 969 { 970 return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_HOTPLUG); 971 } 972 973 /** 974 * memblock_clear_hotplug - Clear flag MEMBLOCK_HOTPLUG for a specified region. 975 * @base: the base phys addr of the region 976 * @size: the size of the region 977 * 978 * Return: 0 on success, -errno on failure. 979 */ 980 int __init_memblock memblock_clear_hotplug(phys_addr_t base, phys_addr_t size) 981 { 982 return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_HOTPLUG); 983 } 984 985 /** 986 * memblock_mark_mirror - Mark mirrored memory with flag MEMBLOCK_MIRROR. 987 * @base: the base phys addr of the region 988 * @size: the size of the region 989 * 990 * Return: 0 on success, -errno on failure. 991 */ 992 int __init_memblock memblock_mark_mirror(phys_addr_t base, phys_addr_t size) 993 { 994 if (!mirrored_kernelcore) 995 return 0; 996 997 system_has_some_mirror = true; 998 999 return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_MIRROR); 1000 } 1001 1002 /** 1003 * memblock_mark_nomap - Mark a memory region with flag MEMBLOCK_NOMAP. 1004 * @base: the base phys addr of the region 1005 * @size: the size of the region 1006 * 1007 * The memory regions marked with %MEMBLOCK_NOMAP will not be added to the 1008 * direct mapping of the physical memory. These regions will still be 1009 * covered by the memory map. The struct page representing NOMAP memory 1010 * frames in the memory map will be PageReserved() 1011 * 1012 * Note: if the memory being marked %MEMBLOCK_NOMAP was allocated from 1013 * memblock, the caller must inform kmemleak to ignore that memory 1014 * 1015 * Return: 0 on success, -errno on failure. 1016 */ 1017 int __init_memblock memblock_mark_nomap(phys_addr_t base, phys_addr_t size) 1018 { 1019 return memblock_setclr_flag(&memblock.memory, base, size, 1, MEMBLOCK_NOMAP); 1020 } 1021 1022 /** 1023 * memblock_clear_nomap - Clear flag MEMBLOCK_NOMAP for a specified region. 1024 * @base: the base phys addr of the region 1025 * @size: the size of the region 1026 * 1027 * Return: 0 on success, -errno on failure. 1028 */ 1029 int __init_memblock memblock_clear_nomap(phys_addr_t base, phys_addr_t size) 1030 { 1031 return memblock_setclr_flag(&memblock.memory, base, size, 0, MEMBLOCK_NOMAP); 1032 } 1033 1034 /** 1035 * memblock_reserved_mark_noinit - Mark a reserved memory region with flag 1036 * MEMBLOCK_RSRV_NOINIT which results in the struct pages not being initialized 1037 * for this region. 1038 * @base: the base phys addr of the region 1039 * @size: the size of the region 1040 * 1041 * struct pages will not be initialized for reserved memory regions marked with 1042 * %MEMBLOCK_RSRV_NOINIT. 1043 * 1044 * Return: 0 on success, -errno on failure. 1045 */ 1046 int __init_memblock memblock_reserved_mark_noinit(phys_addr_t base, phys_addr_t size) 1047 { 1048 return memblock_setclr_flag(&memblock.reserved, base, size, 1, 1049 MEMBLOCK_RSRV_NOINIT); 1050 } 1051 1052 static bool should_skip_region(struct memblock_type *type, 1053 struct memblock_region *m, 1054 int nid, int flags) 1055 { 1056 int m_nid = memblock_get_region_node(m); 1057 1058 /* we never skip regions when iterating memblock.reserved or physmem */ 1059 if (type != memblock_memory) 1060 return false; 1061 1062 /* only memory regions are associated with nodes, check it */ 1063 if (numa_valid_node(nid) && nid != m_nid) 1064 return true; 1065 1066 /* skip hotpluggable memory regions if needed */ 1067 if (movable_node_is_enabled() && memblock_is_hotpluggable(m) && 1068 !(flags & MEMBLOCK_HOTPLUG)) 1069 return true; 1070 1071 /* if we want mirror memory skip non-mirror memory regions */ 1072 if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) 1073 return true; 1074 1075 /* skip nomap memory unless we were asked for it explicitly */ 1076 if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) 1077 return true; 1078 1079 /* skip driver-managed memory unless we were asked for it explicitly */ 1080 if (!(flags & MEMBLOCK_DRIVER_MANAGED) && memblock_is_driver_managed(m)) 1081 return true; 1082 1083 return false; 1084 } 1085 1086 /** 1087 * __next_mem_range - next function for for_each_free_mem_range() etc. 1088 * @idx: pointer to u64 loop variable 1089 * @nid: node selector, %NUMA_NO_NODE for all nodes 1090 * @flags: pick from blocks based on memory attributes 1091 * @type_a: pointer to memblock_type from where the range is taken 1092 * @type_b: pointer to memblock_type which excludes memory from being taken 1093 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 1094 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 1095 * @out_nid: ptr to int for nid of the range, can be %NULL 1096 * 1097 * Find the first area from *@idx which matches @nid, fill the out 1098 * parameters, and update *@idx for the next iteration. The lower 32bit of 1099 * *@idx contains index into type_a and the upper 32bit indexes the 1100 * areas before each region in type_b. For example, if type_b regions 1101 * look like the following, 1102 * 1103 * 0:[0-16), 1:[32-48), 2:[128-130) 1104 * 1105 * The upper 32bit indexes the following regions. 1106 * 1107 * 0:[0-0), 1:[16-32), 2:[48-128), 3:[130-MAX) 1108 * 1109 * As both region arrays are sorted, the function advances the two indices 1110 * in lockstep and returns each intersection. 1111 */ 1112 void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags, 1113 struct memblock_type *type_a, 1114 struct memblock_type *type_b, phys_addr_t *out_start, 1115 phys_addr_t *out_end, int *out_nid) 1116 { 1117 int idx_a = *idx & 0xffffffff; 1118 int idx_b = *idx >> 32; 1119 1120 for (; idx_a < type_a->cnt; idx_a++) { 1121 struct memblock_region *m = &type_a->regions[idx_a]; 1122 1123 phys_addr_t m_start = m->base; 1124 phys_addr_t m_end = m->base + m->size; 1125 int m_nid = memblock_get_region_node(m); 1126 1127 if (should_skip_region(type_a, m, nid, flags)) 1128 continue; 1129 1130 if (!type_b) { 1131 if (out_start) 1132 *out_start = m_start; 1133 if (out_end) 1134 *out_end = m_end; 1135 if (out_nid) 1136 *out_nid = m_nid; 1137 idx_a++; 1138 *idx = (u32)idx_a | (u64)idx_b << 32; 1139 return; 1140 } 1141 1142 /* scan areas before each reservation */ 1143 for (; idx_b < type_b->cnt + 1; idx_b++) { 1144 struct memblock_region *r; 1145 phys_addr_t r_start; 1146 phys_addr_t r_end; 1147 1148 r = &type_b->regions[idx_b]; 1149 r_start = idx_b ? r[-1].base + r[-1].size : 0; 1150 r_end = idx_b < type_b->cnt ? 1151 r->base : PHYS_ADDR_MAX; 1152 1153 /* 1154 * if idx_b advanced past idx_a, 1155 * break out to advance idx_a 1156 */ 1157 if (r_start >= m_end) 1158 break; 1159 /* if the two regions intersect, we're done */ 1160 if (m_start < r_end) { 1161 if (out_start) 1162 *out_start = 1163 max(m_start, r_start); 1164 if (out_end) 1165 *out_end = min(m_end, r_end); 1166 if (out_nid) 1167 *out_nid = m_nid; 1168 /* 1169 * The region which ends first is 1170 * advanced for the next iteration. 1171 */ 1172 if (m_end <= r_end) 1173 idx_a++; 1174 else 1175 idx_b++; 1176 *idx = (u32)idx_a | (u64)idx_b << 32; 1177 return; 1178 } 1179 } 1180 } 1181 1182 /* signal end of iteration */ 1183 *idx = ULLONG_MAX; 1184 } 1185 1186 /** 1187 * __next_mem_range_rev - generic next function for for_each_*_range_rev() 1188 * 1189 * @idx: pointer to u64 loop variable 1190 * @nid: node selector, %NUMA_NO_NODE for all nodes 1191 * @flags: pick from blocks based on memory attributes 1192 * @type_a: pointer to memblock_type from where the range is taken 1193 * @type_b: pointer to memblock_type which excludes memory from being taken 1194 * @out_start: ptr to phys_addr_t for start address of the range, can be %NULL 1195 * @out_end: ptr to phys_addr_t for end address of the range, can be %NULL 1196 * @out_nid: ptr to int for nid of the range, can be %NULL 1197 * 1198 * Finds the next range from type_a which is not marked as unsuitable 1199 * in type_b. 1200 * 1201 * Reverse of __next_mem_range(). 1202 */ 1203 void __init_memblock __next_mem_range_rev(u64 *idx, int nid, 1204 enum memblock_flags flags, 1205 struct memblock_type *type_a, 1206 struct memblock_type *type_b, 1207 phys_addr_t *out_start, 1208 phys_addr_t *out_end, int *out_nid) 1209 { 1210 int idx_a = *idx & 0xffffffff; 1211 int idx_b = *idx >> 32; 1212 1213 if (*idx == (u64)ULLONG_MAX) { 1214 idx_a = type_a->cnt - 1; 1215 if (type_b != NULL) 1216 idx_b = type_b->cnt; 1217 else 1218 idx_b = 0; 1219 } 1220 1221 for (; idx_a >= 0; idx_a--) { 1222 struct memblock_region *m = &type_a->regions[idx_a]; 1223 1224 phys_addr_t m_start = m->base; 1225 phys_addr_t m_end = m->base + m->size; 1226 int m_nid = memblock_get_region_node(m); 1227 1228 if (should_skip_region(type_a, m, nid, flags)) 1229 continue; 1230 1231 if (!type_b) { 1232 if (out_start) 1233 *out_start = m_start; 1234 if (out_end) 1235 *out_end = m_end; 1236 if (out_nid) 1237 *out_nid = m_nid; 1238 idx_a--; 1239 *idx = (u32)idx_a | (u64)idx_b << 32; 1240 return; 1241 } 1242 1243 /* scan areas before each reservation */ 1244 for (; idx_b >= 0; idx_b--) { 1245 struct memblock_region *r; 1246 phys_addr_t r_start; 1247 phys_addr_t r_end; 1248 1249 r = &type_b->regions[idx_b]; 1250 r_start = idx_b ? r[-1].base + r[-1].size : 0; 1251 r_end = idx_b < type_b->cnt ? 1252 r->base : PHYS_ADDR_MAX; 1253 /* 1254 * if idx_b advanced past idx_a, 1255 * break out to advance idx_a 1256 */ 1257 1258 if (r_end <= m_start) 1259 break; 1260 /* if the two regions intersect, we're done */ 1261 if (m_end > r_start) { 1262 if (out_start) 1263 *out_start = max(m_start, r_start); 1264 if (out_end) 1265 *out_end = min(m_end, r_end); 1266 if (out_nid) 1267 *out_nid = m_nid; 1268 if (m_start >= r_start) 1269 idx_a--; 1270 else 1271 idx_b--; 1272 *idx = (u32)idx_a | (u64)idx_b << 32; 1273 return; 1274 } 1275 } 1276 } 1277 /* signal end of iteration */ 1278 *idx = ULLONG_MAX; 1279 } 1280 1281 /* 1282 * Common iterator interface used to define for_each_mem_pfn_range(). 1283 */ 1284 void __init_memblock __next_mem_pfn_range(int *idx, int nid, 1285 unsigned long *out_start_pfn, 1286 unsigned long *out_end_pfn, int *out_nid) 1287 { 1288 struct memblock_type *type = &memblock.memory; 1289 struct memblock_region *r; 1290 int r_nid; 1291 1292 while (++*idx < type->cnt) { 1293 r = &type->regions[*idx]; 1294 r_nid = memblock_get_region_node(r); 1295 1296 if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) 1297 continue; 1298 if (!numa_valid_node(nid) || nid == r_nid) 1299 break; 1300 } 1301 if (*idx >= type->cnt) { 1302 *idx = -1; 1303 return; 1304 } 1305 1306 if (out_start_pfn) 1307 *out_start_pfn = PFN_UP(r->base); 1308 if (out_end_pfn) 1309 *out_end_pfn = PFN_DOWN(r->base + r->size); 1310 if (out_nid) 1311 *out_nid = r_nid; 1312 } 1313 1314 /** 1315 * memblock_set_node - set node ID on memblock regions 1316 * @base: base of area to set node ID for 1317 * @size: size of area to set node ID for 1318 * @type: memblock type to set node ID for 1319 * @nid: node ID to set 1320 * 1321 * Set the nid of memblock @type regions in [@base, @base + @size) to @nid. 1322 * Regions which cross the area boundaries are split as necessary. 1323 * 1324 * Return: 1325 * 0 on success, -errno on failure. 1326 */ 1327 int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, 1328 struct memblock_type *type, int nid) 1329 { 1330 #ifdef CONFIG_NUMA 1331 int start_rgn, end_rgn; 1332 int i, ret; 1333 1334 ret = memblock_isolate_range(type, base, size, &start_rgn, &end_rgn); 1335 if (ret) 1336 return ret; 1337 1338 for (i = start_rgn; i < end_rgn; i++) 1339 memblock_set_region_node(&type->regions[i], nid); 1340 1341 memblock_merge_regions(type, start_rgn, end_rgn); 1342 #endif 1343 return 0; 1344 } 1345 1346 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT 1347 /** 1348 * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone() 1349 * 1350 * @idx: pointer to u64 loop variable 1351 * @zone: zone in which all of the memory blocks reside 1352 * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL 1353 * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL 1354 * 1355 * This function is meant to be a zone/pfn specific wrapper for the 1356 * for_each_mem_range type iterators. Specifically they are used in the 1357 * deferred memory init routines and as such we were duplicating much of 1358 * this logic throughout the code. So instead of having it in multiple 1359 * locations it seemed like it would make more sense to centralize this to 1360 * one new iterator that does everything they need. 1361 */ 1362 void __init_memblock 1363 __next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone, 1364 unsigned long *out_spfn, unsigned long *out_epfn) 1365 { 1366 int zone_nid = zone_to_nid(zone); 1367 phys_addr_t spa, epa; 1368 1369 __next_mem_range(idx, zone_nid, MEMBLOCK_NONE, 1370 &memblock.memory, &memblock.reserved, 1371 &spa, &epa, NULL); 1372 1373 while (*idx != U64_MAX) { 1374 unsigned long epfn = PFN_DOWN(epa); 1375 unsigned long spfn = PFN_UP(spa); 1376 1377 /* 1378 * Verify the end is at least past the start of the zone and 1379 * that we have at least one PFN to initialize. 1380 */ 1381 if (zone->zone_start_pfn < epfn && spfn < epfn) { 1382 /* if we went too far just stop searching */ 1383 if (zone_end_pfn(zone) <= spfn) { 1384 *idx = U64_MAX; 1385 break; 1386 } 1387 1388 if (out_spfn) 1389 *out_spfn = max(zone->zone_start_pfn, spfn); 1390 if (out_epfn) 1391 *out_epfn = min(zone_end_pfn(zone), epfn); 1392 1393 return; 1394 } 1395 1396 __next_mem_range(idx, zone_nid, MEMBLOCK_NONE, 1397 &memblock.memory, &memblock.reserved, 1398 &spa, &epa, NULL); 1399 } 1400 1401 /* signal end of iteration */ 1402 if (out_spfn) 1403 *out_spfn = ULONG_MAX; 1404 if (out_epfn) 1405 *out_epfn = 0; 1406 } 1407 1408 #endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ 1409 1410 /** 1411 * memblock_alloc_range_nid - allocate boot memory block 1412 * @size: size of memory block to be allocated in bytes 1413 * @align: alignment of the region and block's size 1414 * @start: the lower bound of the memory region to allocate (phys address) 1415 * @end: the upper bound of the memory region to allocate (phys address) 1416 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1417 * @exact_nid: control the allocation fall back to other nodes 1418 * 1419 * The allocation is performed from memory region limited by 1420 * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE. 1421 * 1422 * If the specified node can not hold the requested memory and @exact_nid 1423 * is false, the allocation falls back to any node in the system. 1424 * 1425 * For systems with memory mirroring, the allocation is attempted first 1426 * from the regions with mirroring enabled and then retried from any 1427 * memory region. 1428 * 1429 * In addition, function using kmemleak_alloc_phys for allocated boot 1430 * memory block, it is never reported as leaks. 1431 * 1432 * Return: 1433 * Physical address of allocated memory block on success, %0 on failure. 1434 */ 1435 phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, 1436 phys_addr_t align, phys_addr_t start, 1437 phys_addr_t end, int nid, 1438 bool exact_nid) 1439 { 1440 enum memblock_flags flags = choose_memblock_flags(); 1441 phys_addr_t found; 1442 1443 /* 1444 * Detect any accidental use of these APIs after slab is ready, as at 1445 * this moment memblock may be deinitialized already and its 1446 * internal data may be destroyed (after execution of memblock_free_all) 1447 */ 1448 if (WARN_ON_ONCE(slab_is_available())) { 1449 void *vaddr = kzalloc_node(size, GFP_NOWAIT, nid); 1450 1451 return vaddr ? virt_to_phys(vaddr) : 0; 1452 } 1453 1454 if (!align) { 1455 /* Can't use WARNs this early in boot on powerpc */ 1456 dump_stack(); 1457 align = SMP_CACHE_BYTES; 1458 } 1459 1460 again: 1461 found = memblock_find_in_range_node(size, align, start, end, nid, 1462 flags); 1463 if (found && !memblock_reserve(found, size)) 1464 goto done; 1465 1466 if (numa_valid_node(nid) && !exact_nid) { 1467 found = memblock_find_in_range_node(size, align, start, 1468 end, NUMA_NO_NODE, 1469 flags); 1470 if (found && !memblock_reserve(found, size)) 1471 goto done; 1472 } 1473 1474 if (flags & MEMBLOCK_MIRROR) { 1475 flags &= ~MEMBLOCK_MIRROR; 1476 pr_warn_ratelimited("Could not allocate %pap bytes of mirrored memory\n", 1477 &size); 1478 goto again; 1479 } 1480 1481 return 0; 1482 1483 done: 1484 /* 1485 * Skip kmemleak for those places like kasan_init() and 1486 * early_pgtable_alloc() due to high volume. 1487 */ 1488 if (end != MEMBLOCK_ALLOC_NOLEAKTRACE) 1489 /* 1490 * Memblock allocated blocks are never reported as 1491 * leaks. This is because many of these blocks are 1492 * only referred via the physical address which is 1493 * not looked up by kmemleak. 1494 */ 1495 kmemleak_alloc_phys(found, size, 0); 1496 1497 /* 1498 * Some Virtual Machine platforms, such as Intel TDX or AMD SEV-SNP, 1499 * require memory to be accepted before it can be used by the 1500 * guest. 1501 * 1502 * Accept the memory of the allocated buffer. 1503 */ 1504 accept_memory(found, size); 1505 1506 return found; 1507 } 1508 1509 /** 1510 * memblock_phys_alloc_range - allocate a memory block inside specified range 1511 * @size: size of memory block to be allocated in bytes 1512 * @align: alignment of the region and block's size 1513 * @start: the lower bound of the memory region to allocate (physical address) 1514 * @end: the upper bound of the memory region to allocate (physical address) 1515 * 1516 * Allocate @size bytes in the between @start and @end. 1517 * 1518 * Return: physical address of the allocated memory block on success, 1519 * %0 on failure. 1520 */ 1521 phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size, 1522 phys_addr_t align, 1523 phys_addr_t start, 1524 phys_addr_t end) 1525 { 1526 memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n", 1527 __func__, (u64)size, (u64)align, &start, &end, 1528 (void *)_RET_IP_); 1529 return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE, 1530 false); 1531 } 1532 1533 /** 1534 * memblock_phys_alloc_try_nid - allocate a memory block from specified NUMA node 1535 * @size: size of memory block to be allocated in bytes 1536 * @align: alignment of the region and block's size 1537 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1538 * 1539 * Allocates memory block from the specified NUMA node. If the node 1540 * has no available memory, attempts to allocated from any node in the 1541 * system. 1542 * 1543 * Return: physical address of the allocated memory block on success, 1544 * %0 on failure. 1545 */ 1546 phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) 1547 { 1548 return memblock_alloc_range_nid(size, align, 0, 1549 MEMBLOCK_ALLOC_ACCESSIBLE, nid, false); 1550 } 1551 1552 /** 1553 * memblock_alloc_internal - allocate boot memory block 1554 * @size: size of memory block to be allocated in bytes 1555 * @align: alignment of the region and block's size 1556 * @min_addr: the lower bound of the memory region to allocate (phys address) 1557 * @max_addr: the upper bound of the memory region to allocate (phys address) 1558 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1559 * @exact_nid: control the allocation fall back to other nodes 1560 * 1561 * Allocates memory block using memblock_alloc_range_nid() and 1562 * converts the returned physical address to virtual. 1563 * 1564 * The @min_addr limit is dropped if it can not be satisfied and the allocation 1565 * will fall back to memory below @min_addr. Other constraints, such 1566 * as node and mirrored memory will be handled again in 1567 * memblock_alloc_range_nid(). 1568 * 1569 * Return: 1570 * Virtual address of allocated memory block on success, NULL on failure. 1571 */ 1572 static void * __init memblock_alloc_internal( 1573 phys_addr_t size, phys_addr_t align, 1574 phys_addr_t min_addr, phys_addr_t max_addr, 1575 int nid, bool exact_nid) 1576 { 1577 phys_addr_t alloc; 1578 1579 1580 if (max_addr > memblock.current_limit) 1581 max_addr = memblock.current_limit; 1582 1583 alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid, 1584 exact_nid); 1585 1586 /* retry allocation without lower limit */ 1587 if (!alloc && min_addr) 1588 alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid, 1589 exact_nid); 1590 1591 if (!alloc) 1592 return NULL; 1593 1594 return phys_to_virt(alloc); 1595 } 1596 1597 /** 1598 * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node 1599 * without zeroing memory 1600 * @size: size of memory block to be allocated in bytes 1601 * @align: alignment of the region and block's size 1602 * @min_addr: the lower bound of the memory region from where the allocation 1603 * is preferred (phys address) 1604 * @max_addr: the upper bound of the memory region from where the allocation 1605 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 1606 * allocate only from memory limited by memblock.current_limit value 1607 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1608 * 1609 * Public function, provides additional debug information (including caller 1610 * info), if enabled. Does not zero allocated memory. 1611 * 1612 * Return: 1613 * Virtual address of allocated memory block on success, NULL on failure. 1614 */ 1615 void * __init memblock_alloc_exact_nid_raw( 1616 phys_addr_t size, phys_addr_t align, 1617 phys_addr_t min_addr, phys_addr_t max_addr, 1618 int nid) 1619 { 1620 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 1621 __func__, (u64)size, (u64)align, nid, &min_addr, 1622 &max_addr, (void *)_RET_IP_); 1623 1624 return memblock_alloc_internal(size, align, min_addr, max_addr, nid, 1625 true); 1626 } 1627 1628 /** 1629 * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing 1630 * memory and without panicking 1631 * @size: size of memory block to be allocated in bytes 1632 * @align: alignment of the region and block's size 1633 * @min_addr: the lower bound of the memory region from where the allocation 1634 * is preferred (phys address) 1635 * @max_addr: the upper bound of the memory region from where the allocation 1636 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 1637 * allocate only from memory limited by memblock.current_limit value 1638 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1639 * 1640 * Public function, provides additional debug information (including caller 1641 * info), if enabled. Does not zero allocated memory, does not panic if request 1642 * cannot be satisfied. 1643 * 1644 * Return: 1645 * Virtual address of allocated memory block on success, NULL on failure. 1646 */ 1647 void * __init memblock_alloc_try_nid_raw( 1648 phys_addr_t size, phys_addr_t align, 1649 phys_addr_t min_addr, phys_addr_t max_addr, 1650 int nid) 1651 { 1652 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 1653 __func__, (u64)size, (u64)align, nid, &min_addr, 1654 &max_addr, (void *)_RET_IP_); 1655 1656 return memblock_alloc_internal(size, align, min_addr, max_addr, nid, 1657 false); 1658 } 1659 1660 /** 1661 * memblock_alloc_try_nid - allocate boot memory block 1662 * @size: size of memory block to be allocated in bytes 1663 * @align: alignment of the region and block's size 1664 * @min_addr: the lower bound of the memory region from where the allocation 1665 * is preferred (phys address) 1666 * @max_addr: the upper bound of the memory region from where the allocation 1667 * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to 1668 * allocate only from memory limited by memblock.current_limit value 1669 * @nid: nid of the free area to find, %NUMA_NO_NODE for any node 1670 * 1671 * Public function, provides additional debug information (including caller 1672 * info), if enabled. This function zeroes the allocated memory. 1673 * 1674 * Return: 1675 * Virtual address of allocated memory block on success, NULL on failure. 1676 */ 1677 void * __init memblock_alloc_try_nid( 1678 phys_addr_t size, phys_addr_t align, 1679 phys_addr_t min_addr, phys_addr_t max_addr, 1680 int nid) 1681 { 1682 void *ptr; 1683 1684 memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", 1685 __func__, (u64)size, (u64)align, nid, &min_addr, 1686 &max_addr, (void *)_RET_IP_); 1687 ptr = memblock_alloc_internal(size, align, 1688 min_addr, max_addr, nid, false); 1689 if (ptr) 1690 memset(ptr, 0, size); 1691 1692 return ptr; 1693 } 1694 1695 /** 1696 * __memblock_alloc_or_panic - Try to allocate memory and panic on failure 1697 * @size: size of memory block to be allocated in bytes 1698 * @align: alignment of the region and block's size 1699 * @func: caller func name 1700 * 1701 * This function attempts to allocate memory using memblock_alloc, 1702 * and in case of failure, it calls panic with the formatted message. 1703 * This function should not be used directly, please use the macro memblock_alloc_or_panic. 1704 */ 1705 void *__init __memblock_alloc_or_panic(phys_addr_t size, phys_addr_t align, 1706 const char *func) 1707 { 1708 void *addr = memblock_alloc(size, align); 1709 1710 if (unlikely(!addr)) 1711 panic("%s: Failed to allocate %pap bytes\n", func, &size); 1712 return addr; 1713 } 1714 1715 /** 1716 * memblock_free_late - free pages directly to buddy allocator 1717 * @base: phys starting address of the boot memory block 1718 * @size: size of the boot memory block in bytes 1719 * 1720 * This is only useful when the memblock allocator has already been torn 1721 * down, but we are still initializing the system. Pages are released directly 1722 * to the buddy allocator. 1723 */ 1724 void __init memblock_free_late(phys_addr_t base, phys_addr_t size) 1725 { 1726 phys_addr_t cursor, end; 1727 1728 end = base + size - 1; 1729 memblock_dbg("%s: [%pa-%pa] %pS\n", 1730 __func__, &base, &end, (void *)_RET_IP_); 1731 kmemleak_free_part_phys(base, size); 1732 cursor = PFN_UP(base); 1733 end = PFN_DOWN(base + size); 1734 1735 for (; cursor < end; cursor++) { 1736 memblock_free_pages(pfn_to_page(cursor), cursor, 0); 1737 totalram_pages_inc(); 1738 } 1739 } 1740 1741 /* 1742 * Remaining API functions 1743 */ 1744 1745 phys_addr_t __init_memblock memblock_phys_mem_size(void) 1746 { 1747 return memblock.memory.total_size; 1748 } 1749 1750 phys_addr_t __init_memblock memblock_reserved_size(void) 1751 { 1752 return memblock.reserved.total_size; 1753 } 1754 1755 /** 1756 * memblock_estimated_nr_free_pages - return estimated number of free pages 1757 * from memblock point of view 1758 * 1759 * During bootup, subsystems might need a rough estimate of the number of free 1760 * pages in the whole system, before precise numbers are available from the 1761 * buddy. Especially with CONFIG_DEFERRED_STRUCT_PAGE_INIT, the numbers 1762 * obtained from the buddy might be very imprecise during bootup. 1763 * 1764 * Return: 1765 * An estimated number of free pages from memblock point of view. 1766 */ 1767 unsigned long __init memblock_estimated_nr_free_pages(void) 1768 { 1769 return PHYS_PFN(memblock_phys_mem_size() - memblock_reserved_size()); 1770 } 1771 1772 /* lowest address */ 1773 phys_addr_t __init_memblock memblock_start_of_DRAM(void) 1774 { 1775 return memblock.memory.regions[0].base; 1776 } 1777 1778 phys_addr_t __init_memblock memblock_end_of_DRAM(void) 1779 { 1780 int idx = memblock.memory.cnt - 1; 1781 1782 return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); 1783 } 1784 1785 static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit) 1786 { 1787 phys_addr_t max_addr = PHYS_ADDR_MAX; 1788 struct memblock_region *r; 1789 1790 /* 1791 * translate the memory @limit size into the max address within one of 1792 * the memory memblock regions, if the @limit exceeds the total size 1793 * of those regions, max_addr will keep original value PHYS_ADDR_MAX 1794 */ 1795 for_each_mem_region(r) { 1796 if (limit <= r->size) { 1797 max_addr = r->base + limit; 1798 break; 1799 } 1800 limit -= r->size; 1801 } 1802 1803 return max_addr; 1804 } 1805 1806 void __init memblock_enforce_memory_limit(phys_addr_t limit) 1807 { 1808 phys_addr_t max_addr; 1809 1810 if (!limit) 1811 return; 1812 1813 max_addr = __find_max_addr(limit); 1814 1815 /* @limit exceeds the total size of the memory, do nothing */ 1816 if (max_addr == PHYS_ADDR_MAX) 1817 return; 1818 1819 /* truncate both memory and reserved regions */ 1820 memblock_remove_range(&memblock.memory, max_addr, 1821 PHYS_ADDR_MAX); 1822 memblock_remove_range(&memblock.reserved, max_addr, 1823 PHYS_ADDR_MAX); 1824 } 1825 1826 void __init memblock_cap_memory_range(phys_addr_t base, phys_addr_t size) 1827 { 1828 int start_rgn, end_rgn; 1829 int i, ret; 1830 1831 if (!size) 1832 return; 1833 1834 if (!memblock_memory->total_size) { 1835 pr_warn("%s: No memory registered yet\n", __func__); 1836 return; 1837 } 1838 1839 ret = memblock_isolate_range(&memblock.memory, base, size, 1840 &start_rgn, &end_rgn); 1841 if (ret) 1842 return; 1843 1844 /* remove all the MAP regions */ 1845 for (i = memblock.memory.cnt - 1; i >= end_rgn; i--) 1846 if (!memblock_is_nomap(&memblock.memory.regions[i])) 1847 memblock_remove_region(&memblock.memory, i); 1848 1849 for (i = start_rgn - 1; i >= 0; i--) 1850 if (!memblock_is_nomap(&memblock.memory.regions[i])) 1851 memblock_remove_region(&memblock.memory, i); 1852 1853 /* truncate the reserved regions */ 1854 memblock_remove_range(&memblock.reserved, 0, base); 1855 memblock_remove_range(&memblock.reserved, 1856 base + size, PHYS_ADDR_MAX); 1857 } 1858 1859 void __init memblock_mem_limit_remove_map(phys_addr_t limit) 1860 { 1861 phys_addr_t max_addr; 1862 1863 if (!limit) 1864 return; 1865 1866 max_addr = __find_max_addr(limit); 1867 1868 /* @limit exceeds the total size of the memory, do nothing */ 1869 if (max_addr == PHYS_ADDR_MAX) 1870 return; 1871 1872 memblock_cap_memory_range(0, max_addr); 1873 } 1874 1875 static int __init_memblock memblock_search(struct memblock_type *type, phys_addr_t addr) 1876 { 1877 unsigned int left = 0, right = type->cnt; 1878 1879 do { 1880 unsigned int mid = (right + left) / 2; 1881 1882 if (addr < type->regions[mid].base) 1883 right = mid; 1884 else if (addr >= (type->regions[mid].base + 1885 type->regions[mid].size)) 1886 left = mid + 1; 1887 else 1888 return mid; 1889 } while (left < right); 1890 return -1; 1891 } 1892 1893 bool __init_memblock memblock_is_reserved(phys_addr_t addr) 1894 { 1895 return memblock_search(&memblock.reserved, addr) != -1; 1896 } 1897 1898 bool __init_memblock memblock_is_memory(phys_addr_t addr) 1899 { 1900 return memblock_search(&memblock.memory, addr) != -1; 1901 } 1902 1903 bool __init_memblock memblock_is_map_memory(phys_addr_t addr) 1904 { 1905 int i = memblock_search(&memblock.memory, addr); 1906 1907 if (i == -1) 1908 return false; 1909 return !memblock_is_nomap(&memblock.memory.regions[i]); 1910 } 1911 1912 int __init_memblock memblock_search_pfn_nid(unsigned long pfn, 1913 unsigned long *start_pfn, unsigned long *end_pfn) 1914 { 1915 struct memblock_type *type = &memblock.memory; 1916 int mid = memblock_search(type, PFN_PHYS(pfn)); 1917 1918 if (mid == -1) 1919 return NUMA_NO_NODE; 1920 1921 *start_pfn = PFN_DOWN(type->regions[mid].base); 1922 *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size); 1923 1924 return memblock_get_region_node(&type->regions[mid]); 1925 } 1926 1927 /** 1928 * memblock_is_region_memory - check if a region is a subset of memory 1929 * @base: base of region to check 1930 * @size: size of region to check 1931 * 1932 * Check if the region [@base, @base + @size) is a subset of a memory block. 1933 * 1934 * Return: 1935 * 0 if false, non-zero if true 1936 */ 1937 bool __init_memblock memblock_is_region_memory(phys_addr_t base, phys_addr_t size) 1938 { 1939 int idx = memblock_search(&memblock.memory, base); 1940 phys_addr_t end = base + memblock_cap_size(base, &size); 1941 1942 if (idx == -1) 1943 return false; 1944 return (memblock.memory.regions[idx].base + 1945 memblock.memory.regions[idx].size) >= end; 1946 } 1947 1948 /** 1949 * memblock_is_region_reserved - check if a region intersects reserved memory 1950 * @base: base of region to check 1951 * @size: size of region to check 1952 * 1953 * Check if the region [@base, @base + @size) intersects a reserved 1954 * memory block. 1955 * 1956 * Return: 1957 * True if they intersect, false if not. 1958 */ 1959 bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) 1960 { 1961 return memblock_overlaps_region(&memblock.reserved, base, size); 1962 } 1963 1964 void __init_memblock memblock_trim_memory(phys_addr_t align) 1965 { 1966 phys_addr_t start, end, orig_start, orig_end; 1967 struct memblock_region *r; 1968 1969 for_each_mem_region(r) { 1970 orig_start = r->base; 1971 orig_end = r->base + r->size; 1972 start = round_up(orig_start, align); 1973 end = round_down(orig_end, align); 1974 1975 if (start == orig_start && end == orig_end) 1976 continue; 1977 1978 if (start < end) { 1979 r->base = start; 1980 r->size = end - start; 1981 } else { 1982 memblock_remove_region(&memblock.memory, 1983 r - memblock.memory.regions); 1984 r--; 1985 } 1986 } 1987 } 1988 1989 void __init_memblock memblock_set_current_limit(phys_addr_t limit) 1990 { 1991 memblock.current_limit = limit; 1992 } 1993 1994 phys_addr_t __init_memblock memblock_get_current_limit(void) 1995 { 1996 return memblock.current_limit; 1997 } 1998 1999 static void __init_memblock memblock_dump(struct memblock_type *type) 2000 { 2001 phys_addr_t base, end, size; 2002 enum memblock_flags flags; 2003 int idx; 2004 struct memblock_region *rgn; 2005 2006 pr_info(" %s.cnt = 0x%lx\n", type->name, type->cnt); 2007 2008 for_each_memblock_type(idx, type, rgn) { 2009 char nid_buf[32] = ""; 2010 2011 base = rgn->base; 2012 size = rgn->size; 2013 end = base + size - 1; 2014 flags = rgn->flags; 2015 #ifdef CONFIG_NUMA 2016 if (numa_valid_node(memblock_get_region_node(rgn))) 2017 snprintf(nid_buf, sizeof(nid_buf), " on node %d", 2018 memblock_get_region_node(rgn)); 2019 #endif 2020 pr_info(" %s[%#x]\t[%pa-%pa], %pa bytes%s flags: %#x\n", 2021 type->name, idx, &base, &end, &size, nid_buf, flags); 2022 } 2023 } 2024 2025 static void __init_memblock __memblock_dump_all(void) 2026 { 2027 pr_info("MEMBLOCK configuration:\n"); 2028 pr_info(" memory size = %pa reserved size = %pa\n", 2029 &memblock.memory.total_size, 2030 &memblock.reserved.total_size); 2031 2032 memblock_dump(&memblock.memory); 2033 memblock_dump(&memblock.reserved); 2034 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 2035 memblock_dump(&physmem); 2036 #endif 2037 } 2038 2039 void __init_memblock memblock_dump_all(void) 2040 { 2041 if (memblock_debug) 2042 __memblock_dump_all(); 2043 } 2044 2045 void __init memblock_allow_resize(void) 2046 { 2047 memblock_can_resize = 1; 2048 } 2049 2050 static int __init early_memblock(char *p) 2051 { 2052 if (p && strstr(p, "debug")) 2053 memblock_debug = 1; 2054 return 0; 2055 } 2056 early_param("memblock", early_memblock); 2057 2058 static void __init free_memmap(unsigned long start_pfn, unsigned long end_pfn) 2059 { 2060 struct page *start_pg, *end_pg; 2061 phys_addr_t pg, pgend; 2062 2063 /* 2064 * Convert start_pfn/end_pfn to a struct page pointer. 2065 */ 2066 start_pg = pfn_to_page(start_pfn - 1) + 1; 2067 end_pg = pfn_to_page(end_pfn - 1) + 1; 2068 2069 /* 2070 * Convert to physical addresses, and round start upwards and end 2071 * downwards. 2072 */ 2073 pg = PAGE_ALIGN(__pa(start_pg)); 2074 pgend = PAGE_ALIGN_DOWN(__pa(end_pg)); 2075 2076 /* 2077 * If there are free pages between these, free the section of the 2078 * memmap array. 2079 */ 2080 if (pg < pgend) 2081 memblock_phys_free(pg, pgend - pg); 2082 } 2083 2084 /* 2085 * The mem_map array can get very big. Free the unused area of the memory map. 2086 */ 2087 static void __init free_unused_memmap(void) 2088 { 2089 unsigned long start, end, prev_end = 0; 2090 int i; 2091 2092 if (!IS_ENABLED(CONFIG_HAVE_ARCH_PFN_VALID) || 2093 IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP)) 2094 return; 2095 2096 /* 2097 * This relies on each bank being in address order. 2098 * The banks are sorted previously in bootmem_init(). 2099 */ 2100 for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) { 2101 #ifdef CONFIG_SPARSEMEM 2102 /* 2103 * Take care not to free memmap entries that don't exist 2104 * due to SPARSEMEM sections which aren't present. 2105 */ 2106 start = min(start, ALIGN(prev_end, PAGES_PER_SECTION)); 2107 #endif 2108 /* 2109 * Align down here since many operations in VM subsystem 2110 * presume that there are no holes in the memory map inside 2111 * a pageblock 2112 */ 2113 start = pageblock_start_pfn(start); 2114 2115 /* 2116 * If we had a previous bank, and there is a space 2117 * between the current bank and the previous, free it. 2118 */ 2119 if (prev_end && prev_end < start) 2120 free_memmap(prev_end, start); 2121 2122 /* 2123 * Align up here since many operations in VM subsystem 2124 * presume that there are no holes in the memory map inside 2125 * a pageblock 2126 */ 2127 prev_end = pageblock_align(end); 2128 } 2129 2130 #ifdef CONFIG_SPARSEMEM 2131 if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION)) { 2132 prev_end = pageblock_align(end); 2133 free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION)); 2134 } 2135 #endif 2136 } 2137 2138 static void __init __free_pages_memory(unsigned long start, unsigned long end) 2139 { 2140 int order; 2141 2142 while (start < end) { 2143 /* 2144 * Free the pages in the largest chunks alignment allows. 2145 * 2146 * __ffs() behaviour is undefined for 0. start == 0 is 2147 * MAX_PAGE_ORDER-aligned, set order to MAX_PAGE_ORDER for 2148 * the case. 2149 */ 2150 if (start) 2151 order = min_t(int, MAX_PAGE_ORDER, __ffs(start)); 2152 else 2153 order = MAX_PAGE_ORDER; 2154 2155 while (start + (1UL << order) > end) 2156 order--; 2157 2158 memblock_free_pages(pfn_to_page(start), start, order); 2159 2160 start += (1UL << order); 2161 } 2162 } 2163 2164 static unsigned long __init __free_memory_core(phys_addr_t start, 2165 phys_addr_t end) 2166 { 2167 unsigned long start_pfn = PFN_UP(start); 2168 unsigned long end_pfn = PFN_DOWN(end); 2169 2170 if (!IS_ENABLED(CONFIG_HIGHMEM) && end_pfn > max_low_pfn) 2171 end_pfn = max_low_pfn; 2172 2173 if (start_pfn >= end_pfn) 2174 return 0; 2175 2176 __free_pages_memory(start_pfn, end_pfn); 2177 2178 return end_pfn - start_pfn; 2179 } 2180 2181 static void __init memmap_init_reserved_pages(void) 2182 { 2183 struct memblock_region *region; 2184 phys_addr_t start, end; 2185 int nid; 2186 unsigned long max_reserved; 2187 2188 /* 2189 * set nid on all reserved pages and also treat struct 2190 * pages for the NOMAP regions as PageReserved 2191 */ 2192 repeat: 2193 max_reserved = memblock.reserved.max; 2194 for_each_mem_region(region) { 2195 nid = memblock_get_region_node(region); 2196 start = region->base; 2197 end = start + region->size; 2198 2199 if (memblock_is_nomap(region)) 2200 reserve_bootmem_region(start, end, nid); 2201 2202 memblock_set_node(start, region->size, &memblock.reserved, nid); 2203 } 2204 /* 2205 * 'max' is changed means memblock.reserved has been doubled its 2206 * array, which may result a new reserved region before current 2207 * 'start'. Now we should repeat the procedure to set its node id. 2208 */ 2209 if (max_reserved != memblock.reserved.max) 2210 goto repeat; 2211 2212 /* 2213 * initialize struct pages for reserved regions that don't have 2214 * the MEMBLOCK_RSRV_NOINIT flag set 2215 */ 2216 for_each_reserved_mem_region(region) { 2217 if (!memblock_is_reserved_noinit(region)) { 2218 nid = memblock_get_region_node(region); 2219 start = region->base; 2220 end = start + region->size; 2221 2222 if (!numa_valid_node(nid)) 2223 nid = early_pfn_to_nid(PFN_DOWN(start)); 2224 2225 reserve_bootmem_region(start, end, nid); 2226 } 2227 } 2228 } 2229 2230 static unsigned long __init free_low_memory_core_early(void) 2231 { 2232 unsigned long count = 0; 2233 phys_addr_t start, end; 2234 u64 i; 2235 2236 memblock_clear_hotplug(0, -1); 2237 2238 memmap_init_reserved_pages(); 2239 2240 /* 2241 * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id 2242 * because in some case like Node0 doesn't have RAM installed 2243 * low ram will be on Node1 2244 */ 2245 for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end, 2246 NULL) 2247 count += __free_memory_core(start, end); 2248 2249 return count; 2250 } 2251 2252 static int reset_managed_pages_done __initdata; 2253 2254 static void __init reset_node_managed_pages(pg_data_t *pgdat) 2255 { 2256 struct zone *z; 2257 2258 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) 2259 atomic_long_set(&z->managed_pages, 0); 2260 } 2261 2262 void __init reset_all_zones_managed_pages(void) 2263 { 2264 struct pglist_data *pgdat; 2265 2266 if (reset_managed_pages_done) 2267 return; 2268 2269 for_each_online_pgdat(pgdat) 2270 reset_node_managed_pages(pgdat); 2271 2272 reset_managed_pages_done = 1; 2273 } 2274 2275 /** 2276 * memblock_free_all - release free pages to the buddy allocator 2277 */ 2278 void __init memblock_free_all(void) 2279 { 2280 unsigned long pages; 2281 2282 free_unused_memmap(); 2283 reset_all_zones_managed_pages(); 2284 2285 pages = free_low_memory_core_early(); 2286 totalram_pages_add(pages); 2287 } 2288 2289 /* Keep a table to reserve named memory */ 2290 #define RESERVE_MEM_MAX_ENTRIES 8 2291 #define RESERVE_MEM_NAME_SIZE 16 2292 struct reserve_mem_table { 2293 char name[RESERVE_MEM_NAME_SIZE]; 2294 phys_addr_t start; 2295 phys_addr_t size; 2296 }; 2297 static struct reserve_mem_table reserved_mem_table[RESERVE_MEM_MAX_ENTRIES]; 2298 static int reserved_mem_count; 2299 static DEFINE_MUTEX(reserve_mem_lock); 2300 2301 /* Add wildcard region with a lookup name */ 2302 static void __init reserved_mem_add(phys_addr_t start, phys_addr_t size, 2303 const char *name) 2304 { 2305 struct reserve_mem_table *map; 2306 2307 map = &reserved_mem_table[reserved_mem_count++]; 2308 map->start = start; 2309 map->size = size; 2310 strscpy(map->name, name); 2311 } 2312 2313 static struct reserve_mem_table *reserve_mem_find_by_name_nolock(const char *name) 2314 { 2315 struct reserve_mem_table *map; 2316 int i; 2317 2318 for (i = 0; i < reserved_mem_count; i++) { 2319 map = &reserved_mem_table[i]; 2320 if (!map->size) 2321 continue; 2322 if (strcmp(name, map->name) == 0) 2323 return map; 2324 } 2325 return NULL; 2326 } 2327 2328 /** 2329 * reserve_mem_find_by_name - Find reserved memory region with a given name 2330 * @name: The name that is attached to a reserved memory region 2331 * @start: If found, holds the start address 2332 * @size: If found, holds the size of the address. 2333 * 2334 * @start and @size are only updated if @name is found. 2335 * 2336 * Returns: 1 if found or 0 if not found. 2337 */ 2338 int reserve_mem_find_by_name(const char *name, phys_addr_t *start, phys_addr_t *size) 2339 { 2340 struct reserve_mem_table *map; 2341 2342 guard(mutex)(&reserve_mem_lock); 2343 map = reserve_mem_find_by_name_nolock(name); 2344 if (!map) 2345 return 0; 2346 2347 *start = map->start; 2348 *size = map->size; 2349 return 1; 2350 } 2351 EXPORT_SYMBOL_GPL(reserve_mem_find_by_name); 2352 2353 /** 2354 * reserve_mem_release_by_name - Release reserved memory region with a given name 2355 * @name: The name that is attatched to a reserved memory region 2356 * 2357 * Forcibly release the pages in the reserved memory region so that those memory 2358 * can be used as free memory. After released the reserved region size becomes 0. 2359 * 2360 * Returns: 1 if released or 0 if not found. 2361 */ 2362 int reserve_mem_release_by_name(const char *name) 2363 { 2364 char buf[RESERVE_MEM_NAME_SIZE + 12]; 2365 struct reserve_mem_table *map; 2366 void *start, *end; 2367 2368 guard(mutex)(&reserve_mem_lock); 2369 map = reserve_mem_find_by_name_nolock(name); 2370 if (!map) 2371 return 0; 2372 2373 start = phys_to_virt(map->start); 2374 end = start + map->size - 1; 2375 snprintf(buf, sizeof(buf), "reserve_mem:%s", name); 2376 free_reserved_area(start, end, 0, buf); 2377 map->size = 0; 2378 2379 return 1; 2380 } 2381 2382 /* 2383 * Parse reserve_mem=nn:align:name 2384 */ 2385 static int __init reserve_mem(char *p) 2386 { 2387 phys_addr_t start, size, align, tmp; 2388 char *name; 2389 char *oldp; 2390 int len; 2391 2392 if (!p) 2393 return -EINVAL; 2394 2395 /* Check if there's room for more reserved memory */ 2396 if (reserved_mem_count >= RESERVE_MEM_MAX_ENTRIES) 2397 return -EBUSY; 2398 2399 oldp = p; 2400 size = memparse(p, &p); 2401 if (!size || p == oldp) 2402 return -EINVAL; 2403 2404 if (*p != ':') 2405 return -EINVAL; 2406 2407 align = memparse(p+1, &p); 2408 if (*p != ':') 2409 return -EINVAL; 2410 2411 /* 2412 * memblock_phys_alloc() doesn't like a zero size align, 2413 * but it is OK for this command to have it. 2414 */ 2415 if (align < SMP_CACHE_BYTES) 2416 align = SMP_CACHE_BYTES; 2417 2418 name = p + 1; 2419 len = strlen(name); 2420 2421 /* name needs to have length but not too big */ 2422 if (!len || len >= RESERVE_MEM_NAME_SIZE) 2423 return -EINVAL; 2424 2425 /* Make sure that name has text */ 2426 for (p = name; *p; p++) { 2427 if (!isspace(*p)) 2428 break; 2429 } 2430 if (!*p) 2431 return -EINVAL; 2432 2433 /* Make sure the name is not already used */ 2434 if (reserve_mem_find_by_name(name, &start, &tmp)) 2435 return -EBUSY; 2436 2437 start = memblock_phys_alloc(size, align); 2438 if (!start) 2439 return -ENOMEM; 2440 2441 reserved_mem_add(start, size, name); 2442 2443 return 1; 2444 } 2445 __setup("reserve_mem=", reserve_mem); 2446 2447 #if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK) 2448 static const char * const flagname[] = { 2449 [ilog2(MEMBLOCK_HOTPLUG)] = "HOTPLUG", 2450 [ilog2(MEMBLOCK_MIRROR)] = "MIRROR", 2451 [ilog2(MEMBLOCK_NOMAP)] = "NOMAP", 2452 [ilog2(MEMBLOCK_DRIVER_MANAGED)] = "DRV_MNG", 2453 [ilog2(MEMBLOCK_RSRV_NOINIT)] = "RSV_NIT", 2454 }; 2455 2456 static int memblock_debug_show(struct seq_file *m, void *private) 2457 { 2458 struct memblock_type *type = m->private; 2459 struct memblock_region *reg; 2460 int i, j, nid; 2461 unsigned int count = ARRAY_SIZE(flagname); 2462 phys_addr_t end; 2463 2464 for (i = 0; i < type->cnt; i++) { 2465 reg = &type->regions[i]; 2466 end = reg->base + reg->size - 1; 2467 nid = memblock_get_region_node(reg); 2468 2469 seq_printf(m, "%4d: ", i); 2470 seq_printf(m, "%pa..%pa ", ®->base, &end); 2471 if (numa_valid_node(nid)) 2472 seq_printf(m, "%4d ", nid); 2473 else 2474 seq_printf(m, "%4c ", 'x'); 2475 if (reg->flags) { 2476 for (j = 0; j < count; j++) { 2477 if (reg->flags & (1U << j)) { 2478 seq_printf(m, "%s\n", flagname[j]); 2479 break; 2480 } 2481 } 2482 if (j == count) 2483 seq_printf(m, "%s\n", "UNKNOWN"); 2484 } else { 2485 seq_printf(m, "%s\n", "NONE"); 2486 } 2487 } 2488 return 0; 2489 } 2490 DEFINE_SHOW_ATTRIBUTE(memblock_debug); 2491 2492 static int __init memblock_init_debugfs(void) 2493 { 2494 struct dentry *root = debugfs_create_dir("memblock", NULL); 2495 2496 debugfs_create_file("memory", 0444, root, 2497 &memblock.memory, &memblock_debug_fops); 2498 debugfs_create_file("reserved", 0444, root, 2499 &memblock.reserved, &memblock_debug_fops); 2500 #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP 2501 debugfs_create_file("physmem", 0444, root, &physmem, 2502 &memblock_debug_fops); 2503 #endif 2504 2505 return 0; 2506 } 2507 __initcall(memblock_init_debugfs); 2508 2509 #endif /* CONFIG_DEBUG_FS */ 2510