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