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