.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Procedures for maintaining information about logical memory blocks. |
---|
3 | 4 | * |
---|
4 | 5 | * Peter Bergner, IBM Corp. June 2001. |
---|
5 | 6 | * Copyright (C) 2001 Peter Bergner. |
---|
6 | | - * |
---|
7 | | - * This program is free software; you can redistribute it and/or |
---|
8 | | - * modify it under the terms of the GNU General Public License |
---|
9 | | - * as published by the Free Software Foundation; either version |
---|
10 | | - * 2 of the License, or (at your option) any later version. |
---|
11 | 7 | */ |
---|
12 | 8 | |
---|
13 | 9 | #include <linux/kernel.h> |
---|
.. | .. |
---|
20 | 16 | #include <linux/kmemleak.h> |
---|
21 | 17 | #include <linux/seq_file.h> |
---|
22 | 18 | #include <linux/memblock.h> |
---|
23 | | -#include <linux/bootmem.h> |
---|
24 | 19 | |
---|
25 | 20 | #include <asm/sections.h> |
---|
26 | 21 | #include <linux/io.h> |
---|
27 | 22 | |
---|
28 | 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 |
---|
29 | 31 | |
---|
30 | 32 | /** |
---|
31 | 33 | * DOC: memblock overview |
---|
.. | .. |
---|
42 | 44 | * in the system, for instance when the memory is restricted with |
---|
43 | 45 | * ``mem=`` command line parameter |
---|
44 | 46 | * * ``reserved`` - describes the regions that were allocated |
---|
45 | | - * * ``physmap`` - describes the actual physical memory regardless of |
---|
46 | | - * the possible restrictions; the ``physmap`` type is only available |
---|
47 | | - * on some architectures. |
---|
| 47 | + * * ``physmem`` - describes the actual physical memory available during |
---|
| 48 | + * boot regardless of the possible restrictions and memory hot(un)plug; |
---|
| 49 | + * the ``physmem`` type is only available on some architectures. |
---|
48 | 50 | * |
---|
49 | | - * Each region is represented by :c:type:`struct memblock_region` that |
---|
| 51 | + * Each region is represented by struct memblock_region that |
---|
50 | 52 | * defines the region extents, its attributes and NUMA node id on NUMA |
---|
51 | | - * systems. Every memory type is described by the :c:type:`struct |
---|
52 | | - * memblock_type` which contains an array of memory regions along with |
---|
53 | | - * the allocator metadata. The memory types are nicely wrapped with |
---|
54 | | - * :c:type:`struct memblock`. This structure is statically initialzed |
---|
55 | | - * at build time. The region arrays for the "memory" and "reserved" |
---|
56 | | - * types are initially sized to %INIT_MEMBLOCK_REGIONS and for the |
---|
57 | | - * "physmap" type to %INIT_PHYSMEM_REGIONS. |
---|
58 | | - * The :c:func:`memblock_allow_resize` enables automatic resizing of |
---|
59 | | - * the region arrays during addition of new regions. This feature |
---|
60 | | - * should be used with care so that memory allocated for the region |
---|
61 | | - * array will not overlap with areas that should be reserved, for |
---|
62 | | - * example initrd. |
---|
| 53 | + * systems. Every memory type is described by the struct memblock_type |
---|
| 54 | + * which contains an array of memory regions along with |
---|
| 55 | + * the allocator metadata. The "memory" and "reserved" types are nicely |
---|
| 56 | + * wrapped with struct memblock. This structure is statically |
---|
| 57 | + * initialized at build time. The region arrays are initially sized to |
---|
| 58 | + * %INIT_MEMBLOCK_REGIONS for "memory" and %INIT_MEMBLOCK_RESERVED_REGIONS |
---|
| 59 | + * for "reserved". The region array for "physmem" is initially sized to |
---|
| 60 | + * %INIT_PHYSMEM_REGIONS. |
---|
| 61 | + * The memblock_allow_resize() enables automatic resizing of the region |
---|
| 62 | + * arrays during addition of new regions. This feature should be used |
---|
| 63 | + * with care so that memory allocated for the region array will not |
---|
| 64 | + * overlap with areas that should be reserved, for example initrd. |
---|
63 | 65 | * |
---|
64 | 66 | * The early architecture setup should tell memblock what the physical |
---|
65 | | - * memory layout is by using :c:func:`memblock_add` or |
---|
66 | | - * :c:func:`memblock_add_node` functions. The first function does not |
---|
67 | | - * assign the region to a NUMA node and it is appropriate for UMA |
---|
68 | | - * systems. Yet, it is possible to use it on NUMA systems as well and |
---|
69 | | - * assign the region to a NUMA node later in the setup process using |
---|
70 | | - * :c:func:`memblock_set_node`. The :c:func:`memblock_add_node` |
---|
71 | | - * performs such an assignment directly. |
---|
| 67 | + * memory layout is by using memblock_add() or memblock_add_node() |
---|
| 68 | + * functions. The first function does not assign the region to a NUMA |
---|
| 69 | + * node and it is appropriate for UMA systems. Yet, it is possible to |
---|
| 70 | + * use it on NUMA systems as well and assign the region to a NUMA node |
---|
| 71 | + * later in the setup process using memblock_set_node(). The |
---|
| 72 | + * memblock_add_node() performs such an assignment directly. |
---|
72 | 73 | * |
---|
73 | | - * Once memblock is setup the memory can be allocated using either |
---|
74 | | - * memblock or bootmem APIs. |
---|
| 74 | + * Once memblock is setup the memory can be allocated using one of the |
---|
| 75 | + * API variants: |
---|
75 | 76 | * |
---|
76 | | - * As the system boot progresses, the architecture specific |
---|
77 | | - * :c:func:`mem_init` function frees all the memory to the buddy page |
---|
78 | | - * allocator. |
---|
| 77 | + * * memblock_phys_alloc*() - these functions return the **physical** |
---|
| 78 | + * address of the allocated memory |
---|
| 79 | + * * memblock_alloc*() - these functions return the **virtual** address |
---|
| 80 | + * of the allocated memory. |
---|
79 | 81 | * |
---|
80 | | - * If an architecure enables %CONFIG_ARCH_DISCARD_MEMBLOCK, the |
---|
81 | | - * memblock data structures will be discarded after the system |
---|
82 | | - * initialization compltes. |
---|
| 82 | + * Note, that both API variants use implicit assumptions about allowed |
---|
| 83 | + * memory ranges and the fallback methods. Consult the documentation |
---|
| 84 | + * of memblock_alloc_internal() and memblock_alloc_range_nid() |
---|
| 85 | + * functions for more elaborate description. |
---|
| 86 | + * |
---|
| 87 | + * As the system boot progresses, the architecture specific mem_init() |
---|
| 88 | + * function frees all the memory to the buddy page allocator. |
---|
| 89 | + * |
---|
| 90 | + * Unless an architecture enables %CONFIG_ARCH_KEEP_MEMBLOCK, the |
---|
| 91 | + * memblock data structures (except "physmem") will be discarded after the |
---|
| 92 | + * system initialization completes. |
---|
83 | 93 | */ |
---|
84 | 94 | |
---|
| 95 | +#ifndef CONFIG_NEED_MULTIPLE_NODES |
---|
| 96 | +struct pglist_data __refdata contig_page_data; |
---|
| 97 | +EXPORT_SYMBOL(contig_page_data); |
---|
| 98 | +#endif |
---|
| 99 | + |
---|
| 100 | +#if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_SMP) |
---|
| 101 | +static unsigned long defer_start __initdata; |
---|
| 102 | +static unsigned long defer_end __initdata; |
---|
| 103 | + |
---|
| 104 | +#define DEFAULT_DEFER_FREE_BLOCK_SIZE SZ_256M |
---|
| 105 | +static unsigned long defer_free_block_size __initdata = |
---|
| 106 | + DEFAULT_DEFER_FREE_BLOCK_SIZE; |
---|
| 107 | + |
---|
| 108 | +static int __init early_defer_free_block_size(char *p) |
---|
| 109 | +{ |
---|
| 110 | + defer_free_block_size = memparse(p, &p); |
---|
| 111 | + |
---|
| 112 | + pr_debug("defer_free_block_size = 0x%lx\n", defer_free_block_size); |
---|
| 113 | + |
---|
| 114 | + return 0; |
---|
| 115 | +} |
---|
| 116 | + |
---|
| 117 | +early_param("defer_free_block_size", early_defer_free_block_size); |
---|
| 118 | +#endif |
---|
| 119 | + |
---|
| 120 | +unsigned long max_low_pfn; |
---|
| 121 | +unsigned long min_low_pfn; |
---|
| 122 | +unsigned long max_pfn; |
---|
| 123 | +unsigned long long max_possible_pfn; |
---|
| 124 | + |
---|
85 | 125 | static struct memblock_region memblock_memory_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; |
---|
86 | | -static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_REGIONS] __initdata_memblock; |
---|
| 126 | +static struct memblock_region memblock_reserved_init_regions[INIT_MEMBLOCK_RESERVED_REGIONS] __initdata_memblock; |
---|
87 | 127 | #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
---|
88 | | -static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS] __initdata_memblock; |
---|
| 128 | +static struct memblock_region memblock_physmem_init_regions[INIT_PHYSMEM_REGIONS]; |
---|
89 | 129 | #endif |
---|
90 | 130 | |
---|
91 | 131 | struct memblock memblock __initdata_memblock = { |
---|
.. | .. |
---|
96 | 136 | |
---|
97 | 137 | .reserved.regions = memblock_reserved_init_regions, |
---|
98 | 138 | .reserved.cnt = 1, /* empty dummy entry */ |
---|
99 | | - .reserved.max = INIT_MEMBLOCK_REGIONS, |
---|
| 139 | + .reserved.max = INIT_MEMBLOCK_RESERVED_REGIONS, |
---|
100 | 140 | .reserved.name = "reserved", |
---|
101 | | - |
---|
102 | | -#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
---|
103 | | - .physmem.regions = memblock_physmem_init_regions, |
---|
104 | | - .physmem.cnt = 1, /* empty dummy entry */ |
---|
105 | | - .physmem.max = INIT_PHYSMEM_REGIONS, |
---|
106 | | - .physmem.name = "physmem", |
---|
107 | | -#endif |
---|
108 | 141 | |
---|
109 | 142 | .bottom_up = false, |
---|
110 | 143 | .current_limit = MEMBLOCK_ALLOC_ANYWHERE, |
---|
111 | 144 | }; |
---|
112 | 145 | |
---|
113 | | -int memblock_debug __initdata_memblock; |
---|
| 146 | +#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
---|
| 147 | +struct memblock_type physmem = { |
---|
| 148 | + .regions = memblock_physmem_init_regions, |
---|
| 149 | + .cnt = 1, /* empty dummy entry */ |
---|
| 150 | + .max = INIT_PHYSMEM_REGIONS, |
---|
| 151 | + .name = "physmem", |
---|
| 152 | +}; |
---|
| 153 | +#endif |
---|
| 154 | + |
---|
| 155 | +/* |
---|
| 156 | + * keep a pointer to &memblock.memory in the text section to use it in |
---|
| 157 | + * __next_mem_range() and its helpers. |
---|
| 158 | + * For architectures that do not keep memblock data after init, this |
---|
| 159 | + * pointer will be reset to NULL at memblock_discard() |
---|
| 160 | + */ |
---|
| 161 | +static __refdata struct memblock_type *memblock_memory = &memblock.memory; |
---|
| 162 | + |
---|
| 163 | +#define for_each_memblock_type(i, memblock_type, rgn) \ |
---|
| 164 | + for (i = 0, rgn = &memblock_type->regions[0]; \ |
---|
| 165 | + i < memblock_type->cnt; \ |
---|
| 166 | + i++, rgn = &memblock_type->regions[i]) |
---|
| 167 | + |
---|
| 168 | +#define memblock_dbg(fmt, ...) \ |
---|
| 169 | + do { \ |
---|
| 170 | + if (memblock_debug) \ |
---|
| 171 | + pr_info(fmt, ##__VA_ARGS__); \ |
---|
| 172 | + } while (0) |
---|
| 173 | + |
---|
| 174 | +static int memblock_debug __initdata_memblock; |
---|
| 175 | +static bool memblock_nomap_remove __initdata_memblock; |
---|
114 | 176 | static bool system_has_some_mirror __initdata_memblock = false; |
---|
115 | 177 | static int memblock_can_resize __initdata_memblock; |
---|
116 | 178 | static int memblock_memory_in_slab __initdata_memblock = 0; |
---|
117 | 179 | static int memblock_reserved_in_slab __initdata_memblock = 0; |
---|
118 | 180 | |
---|
119 | | -enum memblock_flags __init_memblock choose_memblock_flags(void) |
---|
| 181 | +static enum memblock_flags __init_memblock choose_memblock_flags(void) |
---|
120 | 182 | { |
---|
121 | 183 | return system_has_some_mirror ? MEMBLOCK_MIRROR : MEMBLOCK_NONE; |
---|
122 | 184 | } |
---|
.. | .. |
---|
140 | 202 | phys_addr_t base, phys_addr_t size) |
---|
141 | 203 | { |
---|
142 | 204 | unsigned long i; |
---|
| 205 | + |
---|
| 206 | + memblock_cap_size(base, &size); |
---|
143 | 207 | |
---|
144 | 208 | for (i = 0; i < type->cnt; i++) |
---|
145 | 209 | if (memblock_addrs_overlap(base, size, type->regions[i].base, |
---|
.. | .. |
---|
237 | 301 | * Return: |
---|
238 | 302 | * Found address on success, 0 on failure. |
---|
239 | 303 | */ |
---|
240 | | -phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size, |
---|
| 304 | +static phys_addr_t __init_memblock memblock_find_in_range_node(phys_addr_t size, |
---|
241 | 305 | phys_addr_t align, phys_addr_t start, |
---|
242 | 306 | phys_addr_t end, int nid, |
---|
243 | 307 | enum memblock_flags flags) |
---|
.. | .. |
---|
311 | 375 | } |
---|
312 | 376 | } |
---|
313 | 377 | |
---|
314 | | -#ifdef CONFIG_ARCH_DISCARD_MEMBLOCK |
---|
| 378 | +#ifndef CONFIG_ARCH_KEEP_MEMBLOCK |
---|
315 | 379 | /** |
---|
316 | 380 | * memblock_discard - discard memory and reserved arrays if they were allocated |
---|
317 | 381 | */ |
---|
.. | .. |
---|
338 | 402 | else |
---|
339 | 403 | __memblock_free_late(addr, size); |
---|
340 | 404 | } |
---|
| 405 | + |
---|
| 406 | + memblock_memory = NULL; |
---|
341 | 407 | } |
---|
342 | 408 | #endif |
---|
343 | 409 | |
---|
.. | .. |
---|
388 | 454 | else |
---|
389 | 455 | in_slab = &memblock_reserved_in_slab; |
---|
390 | 456 | |
---|
391 | | - /* Try to find some space for it. |
---|
392 | | - * |
---|
393 | | - * WARNING: We assume that either slab_is_available() and we use it or |
---|
394 | | - * we use MEMBLOCK for allocations. That means that this is unsafe to |
---|
395 | | - * use when bootmem is currently active (unless bootmem itself is |
---|
396 | | - * implemented on top of MEMBLOCK which isn't the case yet) |
---|
397 | | - * |
---|
398 | | - * This should however not be an issue for now, as we currently only |
---|
399 | | - * call into MEMBLOCK while it's still active, or much later when slab |
---|
400 | | - * is active for memory hotplug operations |
---|
401 | | - */ |
---|
| 457 | + /* Try to find some space for it */ |
---|
402 | 458 | if (use_slab) { |
---|
403 | 459 | new_array = kmalloc(new_size, GFP_KERNEL); |
---|
404 | 460 | addr = new_array ? __pa(new_array) : 0; |
---|
.. | .. |
---|
535 | 591 | * Return: |
---|
536 | 592 | * 0 on success, -errno on failure. |
---|
537 | 593 | */ |
---|
538 | | -int __init_memblock memblock_add_range(struct memblock_type *type, |
---|
| 594 | +static int __init_memblock memblock_add_range(struct memblock_type *type, |
---|
539 | 595 | phys_addr_t base, phys_addr_t size, |
---|
540 | 596 | int nid, enum memblock_flags flags) |
---|
541 | 597 | { |
---|
.. | .. |
---|
580 | 636 | * area, insert that portion. |
---|
581 | 637 | */ |
---|
582 | 638 | if (rbase > base) { |
---|
583 | | -#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
---|
| 639 | +#ifdef CONFIG_NEED_MULTIPLE_NODES |
---|
584 | 640 | WARN_ON(nid != memblock_get_region_node(rgn)); |
---|
585 | 641 | #endif |
---|
586 | 642 | WARN_ON(flags != rgn->flags); |
---|
.. | .. |
---|
654 | 710 | { |
---|
655 | 711 | phys_addr_t end = base + size - 1; |
---|
656 | 712 | |
---|
657 | | - memblock_dbg("memblock_add: [%pa-%pa] %pF\n", |
---|
| 713 | + memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, |
---|
658 | 714 | &base, &end, (void *)_RET_IP_); |
---|
659 | 715 | |
---|
660 | 716 | return memblock_add_range(&memblock.memory, base, size, MAX_NUMNODES, 0); |
---|
.. | .. |
---|
755 | 811 | { |
---|
756 | 812 | phys_addr_t end = base + size - 1; |
---|
757 | 813 | |
---|
758 | | - memblock_dbg("memblock_remove: [%pa-%pa] %pS\n", |
---|
| 814 | + memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, |
---|
759 | 815 | &base, &end, (void *)_RET_IP_); |
---|
760 | 816 | |
---|
761 | 817 | return memblock_remove_range(&memblock.memory, base, size); |
---|
762 | 818 | } |
---|
763 | 819 | |
---|
764 | | - |
---|
| 820 | +/** |
---|
| 821 | + * memblock_free - free boot memory block |
---|
| 822 | + * @base: phys starting address of the boot memory block |
---|
| 823 | + * @size: size of the boot memory block in bytes |
---|
| 824 | + * |
---|
| 825 | + * Free boot memory block previously allocated by memblock_alloc_xx() API. |
---|
| 826 | + * The freeing memory will not be released to the buddy allocator. |
---|
| 827 | + */ |
---|
765 | 828 | int __init_memblock memblock_free(phys_addr_t base, phys_addr_t size) |
---|
766 | 829 | { |
---|
767 | 830 | phys_addr_t end = base + size - 1; |
---|
768 | 831 | |
---|
769 | | - memblock_dbg(" memblock_free: [%pa-%pa] %pF\n", |
---|
| 832 | + memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, |
---|
770 | 833 | &base, &end, (void *)_RET_IP_); |
---|
771 | 834 | |
---|
772 | 835 | kmemleak_free_part_phys(base, size); |
---|
773 | 836 | return memblock_remove_range(&memblock.reserved, base, size); |
---|
774 | 837 | } |
---|
| 838 | +#ifdef CONFIG_ARCH_KEEP_MEMBLOCK |
---|
775 | 839 | EXPORT_SYMBOL_GPL(memblock_free); |
---|
| 840 | +#endif |
---|
776 | 841 | |
---|
777 | 842 | int __init_memblock memblock_reserve(phys_addr_t base, phys_addr_t size) |
---|
778 | 843 | { |
---|
779 | 844 | phys_addr_t end = base + size - 1; |
---|
780 | 845 | |
---|
781 | | - memblock_dbg("memblock_reserve: [%pa-%pa] %pF\n", |
---|
| 846 | + memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, |
---|
782 | 847 | &base, &end, (void *)_RET_IP_); |
---|
783 | 848 | |
---|
784 | 849 | return memblock_add_range(&memblock.reserved, base, size, MAX_NUMNODES, 0); |
---|
785 | 850 | } |
---|
| 851 | + |
---|
| 852 | +#ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
---|
| 853 | +int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size) |
---|
| 854 | +{ |
---|
| 855 | + phys_addr_t end = base + size - 1; |
---|
| 856 | + |
---|
| 857 | + memblock_dbg("%s: [%pa-%pa] %pS\n", __func__, |
---|
| 858 | + &base, &end, (void *)_RET_IP_); |
---|
| 859 | + |
---|
| 860 | + return memblock_add_range(&physmem, base, size, MAX_NUMNODES, 0); |
---|
| 861 | +} |
---|
| 862 | +#endif |
---|
786 | 863 | |
---|
787 | 864 | /** |
---|
788 | 865 | * memblock_setclr_flag - set or clear flag for a memory region |
---|
.. | .. |
---|
805 | 882 | if (ret) |
---|
806 | 883 | return ret; |
---|
807 | 884 | |
---|
808 | | - for (i = start_rgn; i < end_rgn; i++) |
---|
| 885 | + for (i = start_rgn; i < end_rgn; i++) { |
---|
| 886 | + struct memblock_region *r = &type->regions[i]; |
---|
| 887 | + |
---|
809 | 888 | if (set) |
---|
810 | | - memblock_set_region_flags(&type->regions[i], flag); |
---|
| 889 | + r->flags |= flag; |
---|
811 | 890 | else |
---|
812 | | - memblock_clear_region_flags(&type->regions[i], flag); |
---|
| 891 | + r->flags &= ~flag; |
---|
| 892 | + } |
---|
813 | 893 | |
---|
814 | 894 | memblock_merge_regions(type); |
---|
815 | 895 | return 0; |
---|
.. | .. |
---|
877 | 957 | return memblock_setclr_flag(base, size, 0, MEMBLOCK_NOMAP); |
---|
878 | 958 | } |
---|
879 | 959 | |
---|
880 | | -/** |
---|
881 | | - * __next_reserved_mem_region - next function for for_each_reserved_region() |
---|
882 | | - * @idx: pointer to u64 loop variable |
---|
883 | | - * @out_start: ptr to phys_addr_t for start address of the region, can be %NULL |
---|
884 | | - * @out_end: ptr to phys_addr_t for end address of the region, can be %NULL |
---|
885 | | - * |
---|
886 | | - * Iterate over all reserved memory regions. |
---|
887 | | - */ |
---|
888 | | -void __init_memblock __next_reserved_mem_region(u64 *idx, |
---|
889 | | - phys_addr_t *out_start, |
---|
890 | | - phys_addr_t *out_end) |
---|
| 960 | +static bool should_skip_region(struct memblock_type *type, |
---|
| 961 | + struct memblock_region *m, |
---|
| 962 | + int nid, int flags) |
---|
891 | 963 | { |
---|
892 | | - struct memblock_type *type = &memblock.reserved; |
---|
| 964 | + int m_nid = memblock_get_region_node(m); |
---|
893 | 965 | |
---|
894 | | - if (*idx < type->cnt) { |
---|
895 | | - struct memblock_region *r = &type->regions[*idx]; |
---|
896 | | - phys_addr_t base = r->base; |
---|
897 | | - phys_addr_t size = r->size; |
---|
| 966 | + /* we never skip regions when iterating memblock.reserved or physmem */ |
---|
| 967 | + if (type != memblock_memory) |
---|
| 968 | + return false; |
---|
898 | 969 | |
---|
899 | | - if (out_start) |
---|
900 | | - *out_start = base; |
---|
901 | | - if (out_end) |
---|
902 | | - *out_end = base + size - 1; |
---|
| 970 | + /* only memory regions are associated with nodes, check it */ |
---|
| 971 | + if (nid != NUMA_NO_NODE && nid != m_nid) |
---|
| 972 | + return true; |
---|
903 | 973 | |
---|
904 | | - *idx += 1; |
---|
905 | | - return; |
---|
906 | | - } |
---|
| 974 | + /* skip hotpluggable memory regions if needed */ |
---|
| 975 | + if (movable_node_is_enabled() && memblock_is_hotpluggable(m) && |
---|
| 976 | + !(flags & MEMBLOCK_HOTPLUG)) |
---|
| 977 | + return true; |
---|
907 | 978 | |
---|
908 | | - /* signal end of iteration */ |
---|
909 | | - *idx = ULLONG_MAX; |
---|
| 979 | + /* if we want mirror memory skip non-mirror memory regions */ |
---|
| 980 | + if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) |
---|
| 981 | + return true; |
---|
| 982 | + |
---|
| 983 | + /* skip nomap memory unless we were asked for it explicitly */ |
---|
| 984 | + if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) |
---|
| 985 | + return true; |
---|
| 986 | + |
---|
| 987 | + return false; |
---|
910 | 988 | } |
---|
911 | 989 | |
---|
912 | 990 | /** |
---|
913 | | - * __next__mem_range - next function for for_each_free_mem_range() etc. |
---|
| 991 | + * __next_mem_range - next function for for_each_free_mem_range() etc. |
---|
914 | 992 | * @idx: pointer to u64 loop variable |
---|
915 | 993 | * @nid: node selector, %NUMA_NO_NODE for all nodes |
---|
916 | 994 | * @flags: pick from blocks based on memory attributes |
---|
.. | .. |
---|
935 | 1013 | * As both region arrays are sorted, the function advances the two indices |
---|
936 | 1014 | * in lockstep and returns each intersection. |
---|
937 | 1015 | */ |
---|
938 | | -void __init_memblock __next_mem_range(u64 *idx, int nid, |
---|
939 | | - enum memblock_flags flags, |
---|
940 | | - struct memblock_type *type_a, |
---|
941 | | - struct memblock_type *type_b, |
---|
942 | | - phys_addr_t *out_start, |
---|
943 | | - phys_addr_t *out_end, int *out_nid) |
---|
| 1016 | +void __next_mem_range(u64 *idx, int nid, enum memblock_flags flags, |
---|
| 1017 | + struct memblock_type *type_a, |
---|
| 1018 | + struct memblock_type *type_b, phys_addr_t *out_start, |
---|
| 1019 | + phys_addr_t *out_end, int *out_nid) |
---|
944 | 1020 | { |
---|
945 | 1021 | int idx_a = *idx & 0xffffffff; |
---|
946 | 1022 | int idx_b = *idx >> 32; |
---|
.. | .. |
---|
956 | 1032 | phys_addr_t m_end = m->base + m->size; |
---|
957 | 1033 | int m_nid = memblock_get_region_node(m); |
---|
958 | 1034 | |
---|
959 | | - /* only memory regions are associated with nodes, check it */ |
---|
960 | | - if (nid != NUMA_NO_NODE && nid != m_nid) |
---|
961 | | - continue; |
---|
962 | | - |
---|
963 | | - /* skip hotpluggable memory regions if needed */ |
---|
964 | | - if (movable_node_is_enabled() && memblock_is_hotpluggable(m)) |
---|
965 | | - continue; |
---|
966 | | - |
---|
967 | | - /* if we want mirror memory skip non-mirror memory regions */ |
---|
968 | | - if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) |
---|
969 | | - continue; |
---|
970 | | - |
---|
971 | | - /* skip nomap memory unless we were asked for it explicitly */ |
---|
972 | | - if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) |
---|
| 1035 | + if (should_skip_region(type_a, m, nid, flags)) |
---|
973 | 1036 | continue; |
---|
974 | 1037 | |
---|
975 | 1038 | if (!type_b) { |
---|
.. | .. |
---|
1073 | 1136 | phys_addr_t m_end = m->base + m->size; |
---|
1074 | 1137 | int m_nid = memblock_get_region_node(m); |
---|
1075 | 1138 | |
---|
1076 | | - /* only memory regions are associated with nodes, check it */ |
---|
1077 | | - if (nid != NUMA_NO_NODE && nid != m_nid) |
---|
1078 | | - continue; |
---|
1079 | | - |
---|
1080 | | - /* skip hotpluggable memory regions if needed */ |
---|
1081 | | - if (movable_node_is_enabled() && memblock_is_hotpluggable(m)) |
---|
1082 | | - continue; |
---|
1083 | | - |
---|
1084 | | - /* if we want mirror memory skip non-mirror memory regions */ |
---|
1085 | | - if ((flags & MEMBLOCK_MIRROR) && !memblock_is_mirror(m)) |
---|
1086 | | - continue; |
---|
1087 | | - |
---|
1088 | | - /* skip nomap memory unless we were asked for it explicitly */ |
---|
1089 | | - if (!(flags & MEMBLOCK_NOMAP) && memblock_is_nomap(m)) |
---|
| 1139 | + if (should_skip_region(type_a, m, nid, flags)) |
---|
1090 | 1140 | continue; |
---|
1091 | 1141 | |
---|
1092 | 1142 | if (!type_b) { |
---|
.. | .. |
---|
1139 | 1189 | *idx = ULLONG_MAX; |
---|
1140 | 1190 | } |
---|
1141 | 1191 | |
---|
1142 | | -#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
---|
1143 | 1192 | /* |
---|
1144 | | - * Common iterator interface used to define for_each_mem_range(). |
---|
| 1193 | + * Common iterator interface used to define for_each_mem_pfn_range(). |
---|
1145 | 1194 | */ |
---|
1146 | 1195 | void __init_memblock __next_mem_pfn_range(int *idx, int nid, |
---|
1147 | 1196 | unsigned long *out_start_pfn, |
---|
.. | .. |
---|
1149 | 1198 | { |
---|
1150 | 1199 | struct memblock_type *type = &memblock.memory; |
---|
1151 | 1200 | struct memblock_region *r; |
---|
| 1201 | + int r_nid; |
---|
1152 | 1202 | |
---|
1153 | 1203 | while (++*idx < type->cnt) { |
---|
1154 | 1204 | r = &type->regions[*idx]; |
---|
| 1205 | + r_nid = memblock_get_region_node(r); |
---|
1155 | 1206 | |
---|
1156 | 1207 | if (PFN_UP(r->base) >= PFN_DOWN(r->base + r->size)) |
---|
1157 | 1208 | continue; |
---|
1158 | | - if (nid == MAX_NUMNODES || nid == r->nid) |
---|
| 1209 | + if (nid == MAX_NUMNODES || nid == r_nid) |
---|
1159 | 1210 | break; |
---|
1160 | 1211 | } |
---|
1161 | 1212 | if (*idx >= type->cnt) { |
---|
.. | .. |
---|
1168 | 1219 | if (out_end_pfn) |
---|
1169 | 1220 | *out_end_pfn = PFN_DOWN(r->base + r->size); |
---|
1170 | 1221 | if (out_nid) |
---|
1171 | | - *out_nid = r->nid; |
---|
| 1222 | + *out_nid = r_nid; |
---|
1172 | 1223 | } |
---|
1173 | 1224 | |
---|
1174 | 1225 | /** |
---|
.. | .. |
---|
1187 | 1238 | int __init_memblock memblock_set_node(phys_addr_t base, phys_addr_t size, |
---|
1188 | 1239 | struct memblock_type *type, int nid) |
---|
1189 | 1240 | { |
---|
| 1241 | +#ifdef CONFIG_NEED_MULTIPLE_NODES |
---|
1190 | 1242 | int start_rgn, end_rgn; |
---|
1191 | 1243 | int i, ret; |
---|
1192 | 1244 | |
---|
.. | .. |
---|
1198 | 1250 | memblock_set_region_node(&type->regions[i], nid); |
---|
1199 | 1251 | |
---|
1200 | 1252 | memblock_merge_regions(type); |
---|
1201 | | - return 0; |
---|
1202 | | -} |
---|
1203 | | -#endif /* CONFIG_HAVE_MEMBLOCK_NODE_MAP */ |
---|
1204 | | - |
---|
1205 | | -static phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, |
---|
1206 | | - phys_addr_t align, phys_addr_t start, |
---|
1207 | | - phys_addr_t end, int nid, |
---|
1208 | | - enum memblock_flags flags) |
---|
1209 | | -{ |
---|
1210 | | - phys_addr_t found; |
---|
1211 | | - |
---|
1212 | | - if (!align) |
---|
1213 | | - align = SMP_CACHE_BYTES; |
---|
1214 | | - |
---|
1215 | | - found = memblock_find_in_range_node(size, align, start, end, nid, |
---|
1216 | | - flags); |
---|
1217 | | - if (found && !memblock_reserve(found, size)) { |
---|
1218 | | - /* |
---|
1219 | | - * The min_count is set to 0 so that memblock allocations are |
---|
1220 | | - * never reported as leaks. |
---|
1221 | | - */ |
---|
1222 | | - kmemleak_alloc_phys(found, size, 0, 0); |
---|
1223 | | - return found; |
---|
1224 | | - } |
---|
| 1253 | +#endif |
---|
1225 | 1254 | return 0; |
---|
1226 | 1255 | } |
---|
1227 | 1256 | |
---|
1228 | | -phys_addr_t __init memblock_alloc_range(phys_addr_t size, phys_addr_t align, |
---|
1229 | | - phys_addr_t start, phys_addr_t end, |
---|
1230 | | - enum memblock_flags flags) |
---|
1231 | | -{ |
---|
1232 | | - return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE, |
---|
1233 | | - flags); |
---|
1234 | | -} |
---|
1235 | | - |
---|
1236 | | -phys_addr_t __init memblock_alloc_base_nid(phys_addr_t size, |
---|
1237 | | - phys_addr_t align, phys_addr_t max_addr, |
---|
1238 | | - int nid, enum memblock_flags flags) |
---|
1239 | | -{ |
---|
1240 | | - return memblock_alloc_range_nid(size, align, 0, max_addr, nid, flags); |
---|
1241 | | -} |
---|
1242 | | - |
---|
1243 | | -phys_addr_t __init memblock_alloc_nid(phys_addr_t size, phys_addr_t align, int nid) |
---|
1244 | | -{ |
---|
1245 | | - enum memblock_flags flags = choose_memblock_flags(); |
---|
1246 | | - phys_addr_t ret; |
---|
1247 | | - |
---|
1248 | | -again: |
---|
1249 | | - ret = memblock_alloc_base_nid(size, align, MEMBLOCK_ALLOC_ACCESSIBLE, |
---|
1250 | | - nid, flags); |
---|
1251 | | - |
---|
1252 | | - if (!ret && (flags & MEMBLOCK_MIRROR)) { |
---|
1253 | | - flags &= ~MEMBLOCK_MIRROR; |
---|
1254 | | - goto again; |
---|
1255 | | - } |
---|
1256 | | - return ret; |
---|
1257 | | -} |
---|
1258 | | - |
---|
1259 | | -phys_addr_t __init __memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
---|
1260 | | -{ |
---|
1261 | | - return memblock_alloc_base_nid(size, align, max_addr, NUMA_NO_NODE, |
---|
1262 | | - MEMBLOCK_NONE); |
---|
1263 | | -} |
---|
1264 | | - |
---|
1265 | | -phys_addr_t __init memblock_alloc_base(phys_addr_t size, phys_addr_t align, phys_addr_t max_addr) |
---|
1266 | | -{ |
---|
1267 | | - phys_addr_t alloc; |
---|
1268 | | - |
---|
1269 | | - alloc = __memblock_alloc_base(size, align, max_addr); |
---|
1270 | | - |
---|
1271 | | - if (alloc == 0) |
---|
1272 | | - panic("ERROR: Failed to allocate %pa bytes below %pa.\n", |
---|
1273 | | - &size, &max_addr); |
---|
1274 | | - |
---|
1275 | | - return alloc; |
---|
1276 | | -} |
---|
1277 | | - |
---|
1278 | | -phys_addr_t __init memblock_alloc(phys_addr_t size, phys_addr_t align) |
---|
1279 | | -{ |
---|
1280 | | - return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
---|
1281 | | -} |
---|
1282 | | - |
---|
1283 | | -phys_addr_t __init memblock_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) |
---|
1284 | | -{ |
---|
1285 | | - phys_addr_t res = memblock_alloc_nid(size, align, nid); |
---|
1286 | | - |
---|
1287 | | - if (res) |
---|
1288 | | - return res; |
---|
1289 | | - return memblock_alloc_base(size, align, MEMBLOCK_ALLOC_ACCESSIBLE); |
---|
1290 | | -} |
---|
1291 | | - |
---|
1292 | | -#if defined(CONFIG_NO_BOOTMEM) |
---|
| 1257 | +#ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT |
---|
1293 | 1258 | /** |
---|
1294 | | - * memblock_virt_alloc_internal - allocate boot memory block |
---|
| 1259 | + * __next_mem_pfn_range_in_zone - iterator for for_each_*_range_in_zone() |
---|
| 1260 | + * |
---|
| 1261 | + * @idx: pointer to u64 loop variable |
---|
| 1262 | + * @zone: zone in which all of the memory blocks reside |
---|
| 1263 | + * @out_spfn: ptr to ulong for start pfn of the range, can be %NULL |
---|
| 1264 | + * @out_epfn: ptr to ulong for end pfn of the range, can be %NULL |
---|
| 1265 | + * |
---|
| 1266 | + * This function is meant to be a zone/pfn specific wrapper for the |
---|
| 1267 | + * for_each_mem_range type iterators. Specifically they are used in the |
---|
| 1268 | + * deferred memory init routines and as such we were duplicating much of |
---|
| 1269 | + * this logic throughout the code. So instead of having it in multiple |
---|
| 1270 | + * locations it seemed like it would make more sense to centralize this to |
---|
| 1271 | + * one new iterator that does everything they need. |
---|
| 1272 | + */ |
---|
| 1273 | +void __init_memblock |
---|
| 1274 | +__next_mem_pfn_range_in_zone(u64 *idx, struct zone *zone, |
---|
| 1275 | + unsigned long *out_spfn, unsigned long *out_epfn) |
---|
| 1276 | +{ |
---|
| 1277 | + int zone_nid = zone_to_nid(zone); |
---|
| 1278 | + phys_addr_t spa, epa; |
---|
| 1279 | + int nid; |
---|
| 1280 | + |
---|
| 1281 | + __next_mem_range(idx, zone_nid, MEMBLOCK_NONE, |
---|
| 1282 | + &memblock.memory, &memblock.reserved, |
---|
| 1283 | + &spa, &epa, &nid); |
---|
| 1284 | + |
---|
| 1285 | + while (*idx != U64_MAX) { |
---|
| 1286 | + unsigned long epfn = PFN_DOWN(epa); |
---|
| 1287 | + unsigned long spfn = PFN_UP(spa); |
---|
| 1288 | + |
---|
| 1289 | + /* |
---|
| 1290 | + * Verify the end is at least past the start of the zone and |
---|
| 1291 | + * that we have at least one PFN to initialize. |
---|
| 1292 | + */ |
---|
| 1293 | + if (zone->zone_start_pfn < epfn && spfn < epfn) { |
---|
| 1294 | + /* if we went too far just stop searching */ |
---|
| 1295 | + if (zone_end_pfn(zone) <= spfn) { |
---|
| 1296 | + *idx = U64_MAX; |
---|
| 1297 | + break; |
---|
| 1298 | + } |
---|
| 1299 | + |
---|
| 1300 | + if (out_spfn) |
---|
| 1301 | + *out_spfn = max(zone->zone_start_pfn, spfn); |
---|
| 1302 | + if (out_epfn) |
---|
| 1303 | + *out_epfn = min(zone_end_pfn(zone), epfn); |
---|
| 1304 | + |
---|
| 1305 | + return; |
---|
| 1306 | + } |
---|
| 1307 | + |
---|
| 1308 | + __next_mem_range(idx, zone_nid, MEMBLOCK_NONE, |
---|
| 1309 | + &memblock.memory, &memblock.reserved, |
---|
| 1310 | + &spa, &epa, &nid); |
---|
| 1311 | + } |
---|
| 1312 | + |
---|
| 1313 | + /* signal end of iteration */ |
---|
| 1314 | + if (out_spfn) |
---|
| 1315 | + *out_spfn = ULONG_MAX; |
---|
| 1316 | + if (out_epfn) |
---|
| 1317 | + *out_epfn = 0; |
---|
| 1318 | +} |
---|
| 1319 | + |
---|
| 1320 | +#endif /* CONFIG_DEFERRED_STRUCT_PAGE_INIT */ |
---|
| 1321 | + |
---|
| 1322 | +/** |
---|
| 1323 | + * memblock_alloc_range_nid - allocate boot memory block |
---|
1295 | 1324 | * @size: size of memory block to be allocated in bytes |
---|
1296 | 1325 | * @align: alignment of the region and block's size |
---|
1297 | | - * @min_addr: the lower bound of the memory region to allocate (phys address) |
---|
1298 | | - * @max_addr: the upper bound of the memory region to allocate (phys address) |
---|
| 1326 | + * @start: the lower bound of the memory region to allocate (phys address) |
---|
| 1327 | + * @end: the upper bound of the memory region to allocate (phys address) |
---|
1299 | 1328 | * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
---|
1300 | | - * |
---|
1301 | | - * The @min_addr limit is dropped if it can not be satisfied and the allocation |
---|
1302 | | - * will fall back to memory below @min_addr. Also, allocation may fall back |
---|
1303 | | - * to any node in the system if the specified node can not |
---|
1304 | | - * hold the requested memory. |
---|
| 1329 | + * @exact_nid: control the allocation fall back to other nodes |
---|
1305 | 1330 | * |
---|
1306 | 1331 | * The allocation is performed from memory region limited by |
---|
1307 | | - * memblock.current_limit if @max_addr == %BOOTMEM_ALLOC_ACCESSIBLE. |
---|
| 1332 | + * memblock.current_limit if @end == %MEMBLOCK_ALLOC_ACCESSIBLE. |
---|
1308 | 1333 | * |
---|
1309 | | - * The memory block is aligned on %SMP_CACHE_BYTES if @align == 0. |
---|
| 1334 | + * If the specified node can not hold the requested memory and @exact_nid |
---|
| 1335 | + * is false, the allocation falls back to any node in the system. |
---|
1310 | 1336 | * |
---|
1311 | | - * The phys address of allocated boot memory block is converted to virtual and |
---|
1312 | | - * allocated memory is reset to 0. |
---|
| 1337 | + * For systems with memory mirroring, the allocation is attempted first |
---|
| 1338 | + * from the regions with mirroring enabled and then retried from any |
---|
| 1339 | + * memory region. |
---|
1313 | 1340 | * |
---|
1314 | | - * In addition, function sets the min_count to 0 using kmemleak_alloc for |
---|
| 1341 | + * In addition, function sets the min_count to 0 using kmemleak_alloc_phys for |
---|
1315 | 1342 | * allocated boot memory block, so that it is never reported as leaks. |
---|
1316 | 1343 | * |
---|
1317 | 1344 | * Return: |
---|
1318 | | - * Virtual address of allocated memory block on success, NULL on failure. |
---|
| 1345 | + * Physical address of allocated memory block on success, %0 on failure. |
---|
1319 | 1346 | */ |
---|
1320 | | -static void * __init memblock_virt_alloc_internal( |
---|
1321 | | - phys_addr_t size, phys_addr_t align, |
---|
1322 | | - phys_addr_t min_addr, phys_addr_t max_addr, |
---|
1323 | | - int nid) |
---|
| 1347 | +phys_addr_t __init memblock_alloc_range_nid(phys_addr_t size, |
---|
| 1348 | + phys_addr_t align, phys_addr_t start, |
---|
| 1349 | + phys_addr_t end, int nid, |
---|
| 1350 | + bool exact_nid) |
---|
1324 | 1351 | { |
---|
1325 | | - phys_addr_t alloc; |
---|
1326 | | - void *ptr; |
---|
1327 | 1352 | enum memblock_flags flags = choose_memblock_flags(); |
---|
| 1353 | + phys_addr_t found; |
---|
1328 | 1354 | |
---|
1329 | 1355 | if (WARN_ONCE(nid == MAX_NUMNODES, "Usage of MAX_NUMNODES is deprecated. Use NUMA_NO_NODE instead\n")) |
---|
1330 | 1356 | nid = NUMA_NO_NODE; |
---|
1331 | 1357 | |
---|
1332 | | - /* |
---|
1333 | | - * Detect any accidental use of these APIs after slab is ready, as at |
---|
1334 | | - * this moment memblock may be deinitialized already and its |
---|
1335 | | - * internal data may be destroyed (after execution of free_all_bootmem) |
---|
1336 | | - */ |
---|
1337 | | - if (WARN_ON_ONCE(slab_is_available())) |
---|
1338 | | - return kzalloc_node(size, GFP_NOWAIT, nid); |
---|
1339 | | - |
---|
1340 | | - if (!align) |
---|
| 1358 | + if (!align) { |
---|
| 1359 | + /* Can't use WARNs this early in boot on powerpc */ |
---|
| 1360 | + dump_stack(); |
---|
1341 | 1361 | align = SMP_CACHE_BYTES; |
---|
1342 | | - |
---|
1343 | | - if (max_addr > memblock.current_limit) |
---|
1344 | | - max_addr = memblock.current_limit; |
---|
1345 | | -again: |
---|
1346 | | - alloc = memblock_find_in_range_node(size, align, min_addr, max_addr, |
---|
1347 | | - nid, flags); |
---|
1348 | | - if (alloc && !memblock_reserve(alloc, size)) |
---|
1349 | | - goto done; |
---|
1350 | | - |
---|
1351 | | - if (nid != NUMA_NO_NODE) { |
---|
1352 | | - alloc = memblock_find_in_range_node(size, align, min_addr, |
---|
1353 | | - max_addr, NUMA_NO_NODE, |
---|
1354 | | - flags); |
---|
1355 | | - if (alloc && !memblock_reserve(alloc, size)) |
---|
1356 | | - goto done; |
---|
1357 | 1362 | } |
---|
1358 | 1363 | |
---|
1359 | | - if (min_addr) { |
---|
1360 | | - min_addr = 0; |
---|
1361 | | - goto again; |
---|
| 1364 | +again: |
---|
| 1365 | + found = memblock_find_in_range_node(size, align, start, end, nid, |
---|
| 1366 | + flags); |
---|
| 1367 | + if (found && !memblock_reserve(found, size)) |
---|
| 1368 | + goto done; |
---|
| 1369 | + |
---|
| 1370 | + if (nid != NUMA_NO_NODE && !exact_nid) { |
---|
| 1371 | + found = memblock_find_in_range_node(size, align, start, |
---|
| 1372 | + end, NUMA_NO_NODE, |
---|
| 1373 | + flags); |
---|
| 1374 | + if (found && !memblock_reserve(found, size)) |
---|
| 1375 | + goto done; |
---|
1362 | 1376 | } |
---|
1363 | 1377 | |
---|
1364 | 1378 | if (flags & MEMBLOCK_MIRROR) { |
---|
.. | .. |
---|
1368 | 1382 | goto again; |
---|
1369 | 1383 | } |
---|
1370 | 1384 | |
---|
1371 | | - return NULL; |
---|
1372 | | -done: |
---|
1373 | | - ptr = phys_to_virt(alloc); |
---|
| 1385 | + return 0; |
---|
1374 | 1386 | |
---|
| 1387 | +done: |
---|
1375 | 1388 | /* Skip kmemleak for kasan_init() due to high volume. */ |
---|
1376 | | - if (max_addr != MEMBLOCK_ALLOC_KASAN) |
---|
| 1389 | + if (end != MEMBLOCK_ALLOC_KASAN) |
---|
1377 | 1390 | /* |
---|
1378 | | - * The min_count is set to 0 so that bootmem allocated |
---|
| 1391 | + * The min_count is set to 0 so that memblock allocated |
---|
1379 | 1392 | * blocks are never reported as leaks. This is because many |
---|
1380 | 1393 | * of these blocks are only referred via the physical |
---|
1381 | 1394 | * address which is not looked up by kmemleak. |
---|
1382 | 1395 | */ |
---|
1383 | | - kmemleak_alloc(ptr, size, 0, 0); |
---|
| 1396 | + kmemleak_alloc_phys(found, size, 0, 0); |
---|
| 1397 | + |
---|
| 1398 | + return found; |
---|
| 1399 | +} |
---|
| 1400 | + |
---|
| 1401 | +/** |
---|
| 1402 | + * memblock_phys_alloc_range - allocate a memory block inside specified range |
---|
| 1403 | + * @size: size of memory block to be allocated in bytes |
---|
| 1404 | + * @align: alignment of the region and block's size |
---|
| 1405 | + * @start: the lower bound of the memory region to allocate (physical address) |
---|
| 1406 | + * @end: the upper bound of the memory region to allocate (physical address) |
---|
| 1407 | + * |
---|
| 1408 | + * Allocate @size bytes in the between @start and @end. |
---|
| 1409 | + * |
---|
| 1410 | + * Return: physical address of the allocated memory block on success, |
---|
| 1411 | + * %0 on failure. |
---|
| 1412 | + */ |
---|
| 1413 | +phys_addr_t __init memblock_phys_alloc_range(phys_addr_t size, |
---|
| 1414 | + phys_addr_t align, |
---|
| 1415 | + phys_addr_t start, |
---|
| 1416 | + phys_addr_t end) |
---|
| 1417 | +{ |
---|
| 1418 | + memblock_dbg("%s: %llu bytes align=0x%llx from=%pa max_addr=%pa %pS\n", |
---|
| 1419 | + __func__, (u64)size, (u64)align, &start, &end, |
---|
| 1420 | + (void *)_RET_IP_); |
---|
| 1421 | + return memblock_alloc_range_nid(size, align, start, end, NUMA_NO_NODE, |
---|
| 1422 | + false); |
---|
| 1423 | +} |
---|
| 1424 | + |
---|
| 1425 | +/** |
---|
| 1426 | + * memblock_phys_alloc_try_nid - allocate a memory block from specified MUMA node |
---|
| 1427 | + * @size: size of memory block to be allocated in bytes |
---|
| 1428 | + * @align: alignment of the region and block's size |
---|
| 1429 | + * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
---|
| 1430 | + * |
---|
| 1431 | + * Allocates memory block from the specified NUMA node. If the node |
---|
| 1432 | + * has no available memory, attempts to allocated from any node in the |
---|
| 1433 | + * system. |
---|
| 1434 | + * |
---|
| 1435 | + * Return: physical address of the allocated memory block on success, |
---|
| 1436 | + * %0 on failure. |
---|
| 1437 | + */ |
---|
| 1438 | +phys_addr_t __init memblock_phys_alloc_try_nid(phys_addr_t size, phys_addr_t align, int nid) |
---|
| 1439 | +{ |
---|
| 1440 | + return memblock_alloc_range_nid(size, align, 0, |
---|
| 1441 | + MEMBLOCK_ALLOC_ACCESSIBLE, nid, false); |
---|
| 1442 | +} |
---|
| 1443 | + |
---|
| 1444 | +/** |
---|
| 1445 | + * memblock_alloc_internal - allocate boot memory block |
---|
| 1446 | + * @size: size of memory block to be allocated in bytes |
---|
| 1447 | + * @align: alignment of the region and block's size |
---|
| 1448 | + * @min_addr: the lower bound of the memory region to allocate (phys address) |
---|
| 1449 | + * @max_addr: the upper bound of the memory region to allocate (phys address) |
---|
| 1450 | + * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
---|
| 1451 | + * @exact_nid: control the allocation fall back to other nodes |
---|
| 1452 | + * |
---|
| 1453 | + * Allocates memory block using memblock_alloc_range_nid() and |
---|
| 1454 | + * converts the returned physical address to virtual. |
---|
| 1455 | + * |
---|
| 1456 | + * The @min_addr limit is dropped if it can not be satisfied and the allocation |
---|
| 1457 | + * will fall back to memory below @min_addr. Other constraints, such |
---|
| 1458 | + * as node and mirrored memory will be handled again in |
---|
| 1459 | + * memblock_alloc_range_nid(). |
---|
| 1460 | + * |
---|
| 1461 | + * Return: |
---|
| 1462 | + * Virtual address of allocated memory block on success, NULL on failure. |
---|
| 1463 | + */ |
---|
| 1464 | +static void * __init memblock_alloc_internal( |
---|
| 1465 | + phys_addr_t size, phys_addr_t align, |
---|
| 1466 | + phys_addr_t min_addr, phys_addr_t max_addr, |
---|
| 1467 | + int nid, bool exact_nid) |
---|
| 1468 | +{ |
---|
| 1469 | + phys_addr_t alloc; |
---|
| 1470 | + |
---|
| 1471 | + /* |
---|
| 1472 | + * Detect any accidental use of these APIs after slab is ready, as at |
---|
| 1473 | + * this moment memblock may be deinitialized already and its |
---|
| 1474 | + * internal data may be destroyed (after execution of memblock_free_all) |
---|
| 1475 | + */ |
---|
| 1476 | + if (WARN_ON_ONCE(slab_is_available())) |
---|
| 1477 | + return kzalloc_node(size, GFP_NOWAIT, nid); |
---|
| 1478 | + |
---|
| 1479 | + if (max_addr > memblock.current_limit) |
---|
| 1480 | + max_addr = memblock.current_limit; |
---|
| 1481 | + |
---|
| 1482 | + alloc = memblock_alloc_range_nid(size, align, min_addr, max_addr, nid, |
---|
| 1483 | + exact_nid); |
---|
| 1484 | + |
---|
| 1485 | + /* retry allocation without lower limit */ |
---|
| 1486 | + if (!alloc && min_addr) |
---|
| 1487 | + alloc = memblock_alloc_range_nid(size, align, 0, max_addr, nid, |
---|
| 1488 | + exact_nid); |
---|
| 1489 | + |
---|
| 1490 | + if (!alloc) |
---|
| 1491 | + return NULL; |
---|
| 1492 | + |
---|
| 1493 | + return phys_to_virt(alloc); |
---|
| 1494 | +} |
---|
| 1495 | + |
---|
| 1496 | +/** |
---|
| 1497 | + * memblock_alloc_exact_nid_raw - allocate boot memory block on the exact node |
---|
| 1498 | + * without zeroing memory |
---|
| 1499 | + * @size: size of memory block to be allocated in bytes |
---|
| 1500 | + * @align: alignment of the region and block's size |
---|
| 1501 | + * @min_addr: the lower bound of the memory region from where the allocation |
---|
| 1502 | + * is preferred (phys address) |
---|
| 1503 | + * @max_addr: the upper bound of the memory region from where the allocation |
---|
| 1504 | + * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to |
---|
| 1505 | + * allocate only from memory limited by memblock.current_limit value |
---|
| 1506 | + * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
---|
| 1507 | + * |
---|
| 1508 | + * Public function, provides additional debug information (including caller |
---|
| 1509 | + * info), if enabled. Does not zero allocated memory. |
---|
| 1510 | + * |
---|
| 1511 | + * Return: |
---|
| 1512 | + * Virtual address of allocated memory block on success, NULL on failure. |
---|
| 1513 | + */ |
---|
| 1514 | +void * __init memblock_alloc_exact_nid_raw( |
---|
| 1515 | + phys_addr_t size, phys_addr_t align, |
---|
| 1516 | + phys_addr_t min_addr, phys_addr_t max_addr, |
---|
| 1517 | + int nid) |
---|
| 1518 | +{ |
---|
| 1519 | + void *ptr; |
---|
| 1520 | + |
---|
| 1521 | + memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", |
---|
| 1522 | + __func__, (u64)size, (u64)align, nid, &min_addr, |
---|
| 1523 | + &max_addr, (void *)_RET_IP_); |
---|
| 1524 | + |
---|
| 1525 | + ptr = memblock_alloc_internal(size, align, |
---|
| 1526 | + min_addr, max_addr, nid, true); |
---|
| 1527 | + if (ptr && size > 0) |
---|
| 1528 | + page_init_poison(ptr, size); |
---|
1384 | 1529 | |
---|
1385 | 1530 | return ptr; |
---|
1386 | 1531 | } |
---|
1387 | 1532 | |
---|
1388 | 1533 | /** |
---|
1389 | | - * memblock_virt_alloc_try_nid_raw - allocate boot memory block without zeroing |
---|
| 1534 | + * memblock_alloc_try_nid_raw - allocate boot memory block without zeroing |
---|
1390 | 1535 | * memory and without panicking |
---|
1391 | 1536 | * @size: size of memory block to be allocated in bytes |
---|
1392 | 1537 | * @align: alignment of the region and block's size |
---|
1393 | 1538 | * @min_addr: the lower bound of the memory region from where the allocation |
---|
1394 | 1539 | * is preferred (phys address) |
---|
1395 | 1540 | * @max_addr: the upper bound of the memory region from where the allocation |
---|
1396 | | - * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to |
---|
| 1541 | + * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to |
---|
1397 | 1542 | * allocate only from memory limited by memblock.current_limit value |
---|
1398 | 1543 | * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
---|
1399 | 1544 | * |
---|
.. | .. |
---|
1404 | 1549 | * Return: |
---|
1405 | 1550 | * Virtual address of allocated memory block on success, NULL on failure. |
---|
1406 | 1551 | */ |
---|
1407 | | -void * __init memblock_virt_alloc_try_nid_raw( |
---|
| 1552 | +void * __init memblock_alloc_try_nid_raw( |
---|
1408 | 1553 | phys_addr_t size, phys_addr_t align, |
---|
1409 | 1554 | phys_addr_t min_addr, phys_addr_t max_addr, |
---|
1410 | 1555 | int nid) |
---|
1411 | 1556 | { |
---|
1412 | 1557 | void *ptr; |
---|
1413 | 1558 | |
---|
1414 | | - memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n", |
---|
| 1559 | + memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", |
---|
1415 | 1560 | __func__, (u64)size, (u64)align, nid, &min_addr, |
---|
1416 | 1561 | &max_addr, (void *)_RET_IP_); |
---|
1417 | 1562 | |
---|
1418 | | - ptr = memblock_virt_alloc_internal(size, align, |
---|
1419 | | - min_addr, max_addr, nid); |
---|
1420 | | -#ifdef CONFIG_DEBUG_VM |
---|
| 1563 | + ptr = memblock_alloc_internal(size, align, |
---|
| 1564 | + min_addr, max_addr, nid, false); |
---|
1421 | 1565 | if (ptr && size > 0) |
---|
1422 | | - memset(ptr, PAGE_POISON_PATTERN, size); |
---|
1423 | | -#endif |
---|
| 1566 | + page_init_poison(ptr, size); |
---|
| 1567 | + |
---|
1424 | 1568 | return ptr; |
---|
1425 | 1569 | } |
---|
1426 | 1570 | |
---|
1427 | 1571 | /** |
---|
1428 | | - * memblock_virt_alloc_try_nid_nopanic - allocate boot memory block |
---|
| 1572 | + * memblock_alloc_try_nid - allocate boot memory block |
---|
1429 | 1573 | * @size: size of memory block to be allocated in bytes |
---|
1430 | 1574 | * @align: alignment of the region and block's size |
---|
1431 | 1575 | * @min_addr: the lower bound of the memory region from where the allocation |
---|
1432 | 1576 | * is preferred (phys address) |
---|
1433 | 1577 | * @max_addr: the upper bound of the memory region from where the allocation |
---|
1434 | | - * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to |
---|
| 1578 | + * is preferred (phys address), or %MEMBLOCK_ALLOC_ACCESSIBLE to |
---|
1435 | 1579 | * allocate only from memory limited by memblock.current_limit value |
---|
1436 | 1580 | * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
---|
1437 | 1581 | * |
---|
.. | .. |
---|
1441 | 1585 | * Return: |
---|
1442 | 1586 | * Virtual address of allocated memory block on success, NULL on failure. |
---|
1443 | 1587 | */ |
---|
1444 | | -void * __init memblock_virt_alloc_try_nid_nopanic( |
---|
1445 | | - phys_addr_t size, phys_addr_t align, |
---|
1446 | | - phys_addr_t min_addr, phys_addr_t max_addr, |
---|
1447 | | - int nid) |
---|
1448 | | -{ |
---|
1449 | | - void *ptr; |
---|
1450 | | - |
---|
1451 | | - memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n", |
---|
1452 | | - __func__, (u64)size, (u64)align, nid, &min_addr, |
---|
1453 | | - &max_addr, (void *)_RET_IP_); |
---|
1454 | | - |
---|
1455 | | - ptr = memblock_virt_alloc_internal(size, align, |
---|
1456 | | - min_addr, max_addr, nid); |
---|
1457 | | - if (ptr) |
---|
1458 | | - memset(ptr, 0, size); |
---|
1459 | | - return ptr; |
---|
1460 | | -} |
---|
1461 | | - |
---|
1462 | | -/** |
---|
1463 | | - * memblock_virt_alloc_try_nid - allocate boot memory block with panicking |
---|
1464 | | - * @size: size of memory block to be allocated in bytes |
---|
1465 | | - * @align: alignment of the region and block's size |
---|
1466 | | - * @min_addr: the lower bound of the memory region from where the allocation |
---|
1467 | | - * is preferred (phys address) |
---|
1468 | | - * @max_addr: the upper bound of the memory region from where the allocation |
---|
1469 | | - * is preferred (phys address), or %BOOTMEM_ALLOC_ACCESSIBLE to |
---|
1470 | | - * allocate only from memory limited by memblock.current_limit value |
---|
1471 | | - * @nid: nid of the free area to find, %NUMA_NO_NODE for any node |
---|
1472 | | - * |
---|
1473 | | - * Public panicking version of memblock_virt_alloc_try_nid_nopanic() |
---|
1474 | | - * which provides debug information (including caller info), if enabled, |
---|
1475 | | - * and panics if the request can not be satisfied. |
---|
1476 | | - * |
---|
1477 | | - * Return: |
---|
1478 | | - * Virtual address of allocated memory block on success, NULL on failure. |
---|
1479 | | - */ |
---|
1480 | | -void * __init memblock_virt_alloc_try_nid( |
---|
| 1588 | +void * __init memblock_alloc_try_nid( |
---|
1481 | 1589 | phys_addr_t size, phys_addr_t align, |
---|
1482 | 1590 | phys_addr_t min_addr, phys_addr_t max_addr, |
---|
1483 | 1591 | int nid) |
---|
1484 | 1592 | { |
---|
1485 | 1593 | void *ptr; |
---|
1486 | 1594 | |
---|
1487 | | - memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pF\n", |
---|
| 1595 | + memblock_dbg("%s: %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa %pS\n", |
---|
1488 | 1596 | __func__, (u64)size, (u64)align, nid, &min_addr, |
---|
1489 | 1597 | &max_addr, (void *)_RET_IP_); |
---|
1490 | | - ptr = memblock_virt_alloc_internal(size, align, |
---|
1491 | | - min_addr, max_addr, nid); |
---|
1492 | | - if (ptr) { |
---|
| 1598 | + ptr = memblock_alloc_internal(size, align, |
---|
| 1599 | + min_addr, max_addr, nid, false); |
---|
| 1600 | + if (ptr) |
---|
1493 | 1601 | memset(ptr, 0, size); |
---|
1494 | | - return ptr; |
---|
1495 | | - } |
---|
1496 | 1602 | |
---|
1497 | | - panic("%s: Failed to allocate %llu bytes align=0x%llx nid=%d from=%pa max_addr=%pa\n", |
---|
1498 | | - __func__, (u64)size, (u64)align, nid, &min_addr, &max_addr); |
---|
1499 | | - return NULL; |
---|
1500 | | -} |
---|
1501 | | -#endif |
---|
1502 | | - |
---|
1503 | | -/** |
---|
1504 | | - * __memblock_free_early - free boot memory block |
---|
1505 | | - * @base: phys starting address of the boot memory block |
---|
1506 | | - * @size: size of the boot memory block in bytes |
---|
1507 | | - * |
---|
1508 | | - * Free boot memory block previously allocated by memblock_virt_alloc_xx() API. |
---|
1509 | | - * The freeing memory will not be released to the buddy allocator. |
---|
1510 | | - */ |
---|
1511 | | -void __init __memblock_free_early(phys_addr_t base, phys_addr_t size) |
---|
1512 | | -{ |
---|
1513 | | - memblock_free(base, size); |
---|
| 1603 | + return ptr; |
---|
1514 | 1604 | } |
---|
1515 | 1605 | |
---|
1516 | 1606 | /** |
---|
1517 | | - * __memblock_free_late - free bootmem block pages directly to buddy allocator |
---|
| 1607 | + * __memblock_free_late - free pages directly to buddy allocator |
---|
1518 | 1608 | * @base: phys starting address of the boot memory block |
---|
1519 | 1609 | * @size: size of the boot memory block in bytes |
---|
1520 | 1610 | * |
---|
1521 | | - * This is only useful when the bootmem allocator has already been torn |
---|
| 1611 | + * This is only useful when the memblock allocator has already been torn |
---|
1522 | 1612 | * down, but we are still initializing the system. Pages are released directly |
---|
1523 | | - * to the buddy allocator, no bootmem metadata is updated because it is gone. |
---|
| 1613 | + * to the buddy allocator. |
---|
1524 | 1614 | */ |
---|
1525 | 1615 | void __init __memblock_free_late(phys_addr_t base, phys_addr_t size) |
---|
1526 | 1616 | { |
---|
1527 | 1617 | phys_addr_t cursor, end; |
---|
1528 | 1618 | |
---|
1529 | 1619 | end = base + size - 1; |
---|
1530 | | - memblock_dbg("%s: [%pa-%pa] %pF\n", |
---|
| 1620 | + memblock_dbg("%s: [%pa-%pa] %pS\n", |
---|
1531 | 1621 | __func__, &base, &end, (void *)_RET_IP_); |
---|
1532 | 1622 | kmemleak_free_part_phys(base, size); |
---|
1533 | 1623 | cursor = PFN_UP(base); |
---|
1534 | 1624 | end = PFN_DOWN(base + size); |
---|
1535 | 1625 | |
---|
1536 | 1626 | for (; cursor < end; cursor++) { |
---|
1537 | | - __free_pages_bootmem(pfn_to_page(cursor), cursor, 0); |
---|
1538 | | - totalram_pages++; |
---|
| 1627 | + memblock_free_pages(pfn_to_page(cursor), cursor, 0); |
---|
| 1628 | + totalram_pages_inc(); |
---|
1539 | 1629 | } |
---|
1540 | 1630 | } |
---|
1541 | 1631 | |
---|
.. | .. |
---|
1553 | 1643 | return memblock.reserved.total_size; |
---|
1554 | 1644 | } |
---|
1555 | 1645 | |
---|
1556 | | -phys_addr_t __init memblock_mem_size(unsigned long limit_pfn) |
---|
1557 | | -{ |
---|
1558 | | - unsigned long pages = 0; |
---|
1559 | | - struct memblock_region *r; |
---|
1560 | | - unsigned long start_pfn, end_pfn; |
---|
1561 | | - |
---|
1562 | | - for_each_memblock(memory, r) { |
---|
1563 | | - start_pfn = memblock_region_memory_base_pfn(r); |
---|
1564 | | - end_pfn = memblock_region_memory_end_pfn(r); |
---|
1565 | | - start_pfn = min_t(unsigned long, start_pfn, limit_pfn); |
---|
1566 | | - end_pfn = min_t(unsigned long, end_pfn, limit_pfn); |
---|
1567 | | - pages += end_pfn - start_pfn; |
---|
1568 | | - } |
---|
1569 | | - |
---|
1570 | | - return PFN_PHYS(pages); |
---|
1571 | | -} |
---|
1572 | | - |
---|
1573 | 1646 | /* lowest address */ |
---|
1574 | 1647 | phys_addr_t __init_memblock memblock_start_of_DRAM(void) |
---|
1575 | 1648 | { |
---|
.. | .. |
---|
1582 | 1655 | |
---|
1583 | 1656 | return (memblock.memory.regions[idx].base + memblock.memory.regions[idx].size); |
---|
1584 | 1657 | } |
---|
| 1658 | +EXPORT_SYMBOL_GPL(memblock_end_of_DRAM); |
---|
1585 | 1659 | |
---|
1586 | 1660 | static phys_addr_t __init_memblock __find_max_addr(phys_addr_t limit) |
---|
1587 | 1661 | { |
---|
.. | .. |
---|
1593 | 1667 | * the memory memblock regions, if the @limit exceeds the total size |
---|
1594 | 1668 | * of those regions, max_addr will keep original value PHYS_ADDR_MAX |
---|
1595 | 1669 | */ |
---|
1596 | | - for_each_memblock(memory, r) { |
---|
| 1670 | + for_each_mem_region(r) { |
---|
1597 | 1671 | if (limit <= r->size) { |
---|
1598 | 1672 | max_addr = r->base + limit; |
---|
1599 | 1673 | break; |
---|
.. | .. |
---|
1606 | 1680 | |
---|
1607 | 1681 | void __init memblock_enforce_memory_limit(phys_addr_t limit) |
---|
1608 | 1682 | { |
---|
1609 | | - phys_addr_t max_addr = PHYS_ADDR_MAX; |
---|
| 1683 | + phys_addr_t max_addr; |
---|
1610 | 1684 | |
---|
1611 | 1685 | if (!limit) |
---|
1612 | 1686 | return; |
---|
.. | .. |
---|
1686 | 1760 | return -1; |
---|
1687 | 1761 | } |
---|
1688 | 1762 | |
---|
1689 | | -bool __init memblock_is_reserved(phys_addr_t addr) |
---|
| 1763 | +bool __init_memblock memblock_is_reserved(phys_addr_t addr) |
---|
1690 | 1764 | { |
---|
1691 | 1765 | return memblock_search(&memblock.reserved, addr) != -1; |
---|
1692 | 1766 | } |
---|
.. | .. |
---|
1705 | 1779 | return !memblock_is_nomap(&memblock.memory.regions[i]); |
---|
1706 | 1780 | } |
---|
1707 | 1781 | |
---|
1708 | | -#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
---|
1709 | 1782 | int __init_memblock memblock_search_pfn_nid(unsigned long pfn, |
---|
1710 | 1783 | unsigned long *start_pfn, unsigned long *end_pfn) |
---|
1711 | 1784 | { |
---|
.. | .. |
---|
1718 | 1791 | *start_pfn = PFN_DOWN(type->regions[mid].base); |
---|
1719 | 1792 | *end_pfn = PFN_DOWN(type->regions[mid].base + type->regions[mid].size); |
---|
1720 | 1793 | |
---|
1721 | | - return type->regions[mid].nid; |
---|
| 1794 | + return memblock_get_region_node(&type->regions[mid]); |
---|
1722 | 1795 | } |
---|
1723 | | -#endif |
---|
1724 | 1796 | |
---|
1725 | 1797 | /** |
---|
1726 | 1798 | * memblock_is_region_memory - check if a region is a subset of memory |
---|
.. | .. |
---|
1743 | 1815 | memblock.memory.regions[idx].size) >= end; |
---|
1744 | 1816 | } |
---|
1745 | 1817 | |
---|
1746 | | -bool __init_memblock memblock_overlaps_memory(phys_addr_t base, |
---|
1747 | | - phys_addr_t size) |
---|
1748 | | -{ |
---|
1749 | | - memblock_cap_size(base, &size); |
---|
1750 | | - |
---|
1751 | | - return memblock_overlaps_region(&memblock.memory, base, size); |
---|
1752 | | -} |
---|
1753 | | -EXPORT_SYMBOL_GPL(memblock_overlaps_memory); |
---|
1754 | | - |
---|
1755 | 1818 | /** |
---|
1756 | 1819 | * memblock_is_region_reserved - check if a region intersects reserved memory |
---|
1757 | 1820 | * @base: base of region to check |
---|
.. | .. |
---|
1765 | 1828 | */ |
---|
1766 | 1829 | bool __init_memblock memblock_is_region_reserved(phys_addr_t base, phys_addr_t size) |
---|
1767 | 1830 | { |
---|
1768 | | - memblock_cap_size(base, &size); |
---|
1769 | 1831 | return memblock_overlaps_region(&memblock.reserved, base, size); |
---|
1770 | 1832 | } |
---|
1771 | 1833 | |
---|
.. | .. |
---|
1774 | 1836 | phys_addr_t start, end, orig_start, orig_end; |
---|
1775 | 1837 | struct memblock_region *r; |
---|
1776 | 1838 | |
---|
1777 | | - for_each_memblock(memory, r) { |
---|
| 1839 | + for_each_mem_region(r) { |
---|
1778 | 1840 | orig_start = r->base; |
---|
1779 | 1841 | orig_end = r->base + r->size; |
---|
1780 | 1842 | start = round_up(orig_start, align); |
---|
.. | .. |
---|
1820 | 1882 | size = rgn->size; |
---|
1821 | 1883 | end = base + size - 1; |
---|
1822 | 1884 | flags = rgn->flags; |
---|
1823 | | -#ifdef CONFIG_HAVE_MEMBLOCK_NODE_MAP |
---|
| 1885 | +#ifdef CONFIG_NEED_MULTIPLE_NODES |
---|
1824 | 1886 | if (memblock_get_region_node(rgn) != MAX_NUMNODES) |
---|
1825 | 1887 | snprintf(nid_buf, sizeof(nid_buf), " on node %d", |
---|
1826 | 1888 | memblock_get_region_node(rgn)); |
---|
.. | .. |
---|
1830 | 1892 | } |
---|
1831 | 1893 | } |
---|
1832 | 1894 | |
---|
1833 | | -void __init_memblock __memblock_dump_all(void) |
---|
| 1895 | +static void __init_memblock __memblock_dump_all(void) |
---|
1834 | 1896 | { |
---|
1835 | 1897 | pr_info("MEMBLOCK configuration:\n"); |
---|
1836 | 1898 | pr_info(" memory size = %pa reserved size = %pa\n", |
---|
.. | .. |
---|
1840 | 1902 | memblock_dump(&memblock.memory); |
---|
1841 | 1903 | memblock_dump(&memblock.reserved); |
---|
1842 | 1904 | #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
---|
1843 | | - memblock_dump(&memblock.physmem); |
---|
| 1905 | + memblock_dump(&physmem); |
---|
1844 | 1906 | #endif |
---|
| 1907 | +} |
---|
| 1908 | + |
---|
| 1909 | +void __init_memblock memblock_dump_all(void) |
---|
| 1910 | +{ |
---|
| 1911 | + if (memblock_debug) |
---|
| 1912 | + __memblock_dump_all(); |
---|
1845 | 1913 | } |
---|
1846 | 1914 | |
---|
1847 | 1915 | void __init memblock_allow_resize(void) |
---|
.. | .. |
---|
1857 | 1925 | } |
---|
1858 | 1926 | early_param("memblock", early_memblock); |
---|
1859 | 1927 | |
---|
1860 | | -#if defined(CONFIG_DEBUG_FS) && !defined(CONFIG_ARCH_DISCARD_MEMBLOCK) |
---|
| 1928 | +static int __init early_memblock_nomap(char *str) |
---|
| 1929 | +{ |
---|
| 1930 | + return kstrtobool(str, &memblock_nomap_remove); |
---|
| 1931 | +} |
---|
| 1932 | +early_param("android12_only.will_be_removed_soon.memblock_nomap_remove", early_memblock_nomap); |
---|
| 1933 | + |
---|
| 1934 | +bool __init memblock_is_nomap_remove(void) |
---|
| 1935 | +{ |
---|
| 1936 | + return memblock_nomap_remove; |
---|
| 1937 | +} |
---|
| 1938 | + |
---|
| 1939 | +static void __init __free_pages_memory(unsigned long start, unsigned long end) |
---|
| 1940 | +{ |
---|
| 1941 | + int order; |
---|
| 1942 | + |
---|
| 1943 | + while (start < end) { |
---|
| 1944 | + order = min(MAX_ORDER - 1UL, __ffs(start)); |
---|
| 1945 | + |
---|
| 1946 | + while (start + (1UL << order) > end) |
---|
| 1947 | + order--; |
---|
| 1948 | + |
---|
| 1949 | + memblock_free_pages(pfn_to_page(start), start, order); |
---|
| 1950 | + |
---|
| 1951 | + start += (1UL << order); |
---|
| 1952 | + } |
---|
| 1953 | +} |
---|
| 1954 | + |
---|
| 1955 | +#if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_SMP) |
---|
| 1956 | +int __init defer_free_memblock(void *unused) |
---|
| 1957 | +{ |
---|
| 1958 | + if (defer_start == 0) |
---|
| 1959 | + return 0; |
---|
| 1960 | + |
---|
| 1961 | + pr_debug("start = %ld, end = %ld\n", defer_start, defer_end); |
---|
| 1962 | + |
---|
| 1963 | + __free_pages_memory(defer_start, defer_end); |
---|
| 1964 | + |
---|
| 1965 | + totalram_pages_add(defer_end - defer_start); |
---|
| 1966 | + |
---|
| 1967 | + pr_info("%s: size %luM free %luM [%luM - %luM] total %luM\n", __func__, |
---|
| 1968 | + defer_free_block_size >> 20, |
---|
| 1969 | + (defer_end - defer_start) >> (20 - PAGE_SHIFT), |
---|
| 1970 | + defer_end >> (20 - PAGE_SHIFT), |
---|
| 1971 | + defer_start >> (20 - PAGE_SHIFT), |
---|
| 1972 | + totalram_pages() >> (20 - PAGE_SHIFT)); |
---|
| 1973 | + return 0; |
---|
| 1974 | +} |
---|
| 1975 | +#endif |
---|
| 1976 | + |
---|
| 1977 | +static unsigned long __init __free_memory_core(phys_addr_t start, |
---|
| 1978 | + phys_addr_t end) |
---|
| 1979 | +{ |
---|
| 1980 | + unsigned long start_pfn = PFN_UP(start); |
---|
| 1981 | + unsigned long end_pfn = min_t(unsigned long, |
---|
| 1982 | + PFN_DOWN(end), max_low_pfn); |
---|
| 1983 | + |
---|
| 1984 | + if (start_pfn >= end_pfn) |
---|
| 1985 | + return 0; |
---|
| 1986 | + |
---|
| 1987 | +#if defined(CONFIG_ROCKCHIP_THUNDER_BOOT) && defined(CONFIG_SMP) |
---|
| 1988 | + if ((end - start) > defer_free_block_size) { |
---|
| 1989 | + defer_start = start_pfn; |
---|
| 1990 | + defer_end = end_pfn; |
---|
| 1991 | + |
---|
| 1992 | + return 0; |
---|
| 1993 | + } |
---|
| 1994 | +#endif |
---|
| 1995 | + |
---|
| 1996 | + __free_pages_memory(start_pfn, end_pfn); |
---|
| 1997 | + |
---|
| 1998 | + return end_pfn - start_pfn; |
---|
| 1999 | +} |
---|
| 2000 | + |
---|
| 2001 | +static unsigned long __init free_low_memory_core_early(void) |
---|
| 2002 | +{ |
---|
| 2003 | + unsigned long count = 0; |
---|
| 2004 | + phys_addr_t start, end; |
---|
| 2005 | + u64 i; |
---|
| 2006 | + |
---|
| 2007 | + memblock_clear_hotplug(0, -1); |
---|
| 2008 | + |
---|
| 2009 | + for_each_reserved_mem_range(i, &start, &end) |
---|
| 2010 | + reserve_bootmem_region(start, end); |
---|
| 2011 | + |
---|
| 2012 | + /* |
---|
| 2013 | + * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id |
---|
| 2014 | + * because in some case like Node0 doesn't have RAM installed |
---|
| 2015 | + * low ram will be on Node1 |
---|
| 2016 | + */ |
---|
| 2017 | + for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end, |
---|
| 2018 | + NULL) |
---|
| 2019 | + count += __free_memory_core(start, end); |
---|
| 2020 | + |
---|
| 2021 | + return count; |
---|
| 2022 | +} |
---|
| 2023 | + |
---|
| 2024 | +static int reset_managed_pages_done __initdata; |
---|
| 2025 | + |
---|
| 2026 | +void reset_node_managed_pages(pg_data_t *pgdat) |
---|
| 2027 | +{ |
---|
| 2028 | + struct zone *z; |
---|
| 2029 | + |
---|
| 2030 | + for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) |
---|
| 2031 | + atomic_long_set(&z->managed_pages, 0); |
---|
| 2032 | +} |
---|
| 2033 | + |
---|
| 2034 | +void __init reset_all_zones_managed_pages(void) |
---|
| 2035 | +{ |
---|
| 2036 | + struct pglist_data *pgdat; |
---|
| 2037 | + |
---|
| 2038 | + if (reset_managed_pages_done) |
---|
| 2039 | + return; |
---|
| 2040 | + |
---|
| 2041 | + for_each_online_pgdat(pgdat) |
---|
| 2042 | + reset_node_managed_pages(pgdat); |
---|
| 2043 | + |
---|
| 2044 | + reset_managed_pages_done = 1; |
---|
| 2045 | +} |
---|
| 2046 | + |
---|
| 2047 | +/** |
---|
| 2048 | + * memblock_free_all - release free pages to the buddy allocator |
---|
| 2049 | + * |
---|
| 2050 | + * Return: the number of pages actually released. |
---|
| 2051 | + */ |
---|
| 2052 | +unsigned long __init memblock_free_all(void) |
---|
| 2053 | +{ |
---|
| 2054 | + unsigned long pages; |
---|
| 2055 | + |
---|
| 2056 | + reset_all_zones_managed_pages(); |
---|
| 2057 | + |
---|
| 2058 | + pages = free_low_memory_core_early(); |
---|
| 2059 | + totalram_pages_add(pages); |
---|
| 2060 | + |
---|
| 2061 | + return pages; |
---|
| 2062 | +} |
---|
| 2063 | + |
---|
| 2064 | +#if defined(CONFIG_DEBUG_FS) && defined(CONFIG_ARCH_KEEP_MEMBLOCK) |
---|
1861 | 2065 | |
---|
1862 | 2066 | static int memblock_debug_show(struct seq_file *m, void *private) |
---|
1863 | 2067 | { |
---|
.. | .. |
---|
1880 | 2084 | static int __init memblock_init_debugfs(void) |
---|
1881 | 2085 | { |
---|
1882 | 2086 | struct dentry *root = debugfs_create_dir("memblock", NULL); |
---|
1883 | | - if (!root) |
---|
1884 | | - return -ENXIO; |
---|
| 2087 | + |
---|
1885 | 2088 | debugfs_create_file("memory", 0444, root, |
---|
1886 | 2089 | &memblock.memory, &memblock_debug_fops); |
---|
1887 | 2090 | debugfs_create_file("reserved", 0444, root, |
---|
1888 | 2091 | &memblock.reserved, &memblock_debug_fops); |
---|
1889 | 2092 | #ifdef CONFIG_HAVE_MEMBLOCK_PHYS_MAP |
---|
1890 | | - debugfs_create_file("physmem", 0444, root, |
---|
1891 | | - &memblock.physmem, &memblock_debug_fops); |
---|
| 2093 | + debugfs_create_file("physmem", 0444, root, &physmem, |
---|
| 2094 | + &memblock_debug_fops); |
---|
1892 | 2095 | #endif |
---|
1893 | 2096 | |
---|
1894 | 2097 | return 0; |
---|