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