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