Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Johannes Weiner | 57cfc29 | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 2 | * bootmem - A boot-time physical memory allocator and configurator |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1999 Ingo Molnar |
Johannes Weiner | 57cfc29 | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 5 | * 1999 Kanoj Sarcar, SGI |
| 6 | * 2008 Johannes Weiner |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Johannes Weiner | 57cfc29 | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 8 | * Access to this subsystem has to be serialized externally (which is true |
| 9 | * for the boot process anyway). |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include <linux/init.h> |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 12 | #include <linux/pfn.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 13 | #include <linux/slab.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include <linux/bootmem.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Catalin Marinas | ec3a354 | 2009-07-07 10:33:01 +0100 | [diff] [blame] | 16 | #include <linux/kmemleak.h> |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 17 | #include <linux/range.h> |
Yinghai Lu | 72d7c3b | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 18 | #include <linux/memblock.h> |
Franck Bui-Huu | e786e86 | 2006-09-25 23:31:06 -0700 | [diff] [blame] | 19 | |
| 20 | #include <asm/bug.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <asm/io.h> |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 22 | #include <asm/processor.h> |
Franck Bui-Huu | e786e86 | 2006-09-25 23:31:06 -0700 | [diff] [blame] | 23 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "internal.h" |
| 25 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | unsigned long max_low_pfn; |
| 27 | unsigned long min_low_pfn; |
| 28 | unsigned long max_pfn; |
| 29 | |
Vivek Goyal | 92aa63a | 2005-06-25 14:58:18 -0700 | [diff] [blame] | 30 | #ifdef CONFIG_CRASH_DUMP |
| 31 | /* |
| 32 | * If we have booted due to a crash, max_pfn will be a very low value. We need |
| 33 | * to know the amount of memory that the previous kernel used. |
| 34 | */ |
| 35 | unsigned long saved_max_pfn; |
| 36 | #endif |
| 37 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 38 | #ifndef CONFIG_NO_BOOTMEM |
Johannes Weiner | b61bfa3 | 2008-07-23 21:26:55 -0700 | [diff] [blame] | 39 | bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata; |
| 40 | |
Johannes Weiner | 636cc40 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 41 | static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list); |
| 42 | |
Johannes Weiner | 2e5237d | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 43 | static int bootmem_debug; |
| 44 | |
| 45 | static int __init bootmem_debug_setup(char *buf) |
| 46 | { |
| 47 | bootmem_debug = 1; |
| 48 | return 0; |
| 49 | } |
| 50 | early_param("bootmem_debug", bootmem_debug_setup); |
| 51 | |
| 52 | #define bdebug(fmt, args...) ({ \ |
| 53 | if (unlikely(bootmem_debug)) \ |
| 54 | printk(KERN_INFO \ |
| 55 | "bootmem::%s " fmt, \ |
Harvey Harrison | 80a914d | 2008-10-15 22:01:25 -0700 | [diff] [blame] | 56 | __func__, ## args); \ |
Johannes Weiner | 2e5237d | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 57 | }) |
| 58 | |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 59 | static unsigned long __init bootmap_bytes(unsigned long pages) |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 60 | { |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 61 | unsigned long bytes = (pages + 7) / 8; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 62 | |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 63 | return ALIGN(bytes, sizeof(long)); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 66 | /** |
| 67 | * bootmem_bootmap_pages - calculate bitmap size in pages |
| 68 | * @pages: number of pages the bitmap has to represent |
| 69 | */ |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 70 | unsigned long __init bootmem_bootmap_pages(unsigned long pages) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | { |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 72 | unsigned long bytes = bootmap_bytes(pages); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 74 | return PAGE_ALIGN(bytes) >> PAGE_SHIFT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | } |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 76 | |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 77 | /* |
| 78 | * link bdata in order |
| 79 | */ |
Franck Bui-Huu | 69d49e6 | 2006-09-25 23:31:04 -0700 | [diff] [blame] | 80 | static void __init link_bootmem(bootmem_data_t *bdata) |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 81 | { |
Johannes Weiner | 636cc40 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 82 | struct list_head *iter; |
Franck Bui-Huu | f71bf0c | 2006-09-25 23:31:08 -0700 | [diff] [blame] | 83 | |
Johannes Weiner | 636cc40 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 84 | list_for_each(iter, &bdata_list) { |
| 85 | bootmem_data_t *ent; |
| 86 | |
| 87 | ent = list_entry(iter, bootmem_data_t, list); |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 88 | if (bdata->node_min_pfn < ent->node_min_pfn) |
Johannes Weiner | 636cc40 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 89 | break; |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 90 | } |
Johannes Weiner | 636cc40 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 91 | list_add_tail(&bdata->list, iter); |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 94 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 95 | * Called once to set up the allocator itself. |
| 96 | */ |
Johannes Weiner | 8ae0446 | 2008-07-23 21:26:59 -0700 | [diff] [blame] | 97 | static unsigned long __init init_bootmem_core(bootmem_data_t *bdata, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | unsigned long mapstart, unsigned long start, unsigned long end) |
| 99 | { |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 100 | unsigned long mapsize; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
Mel Gorman | 2dbb51c | 2008-07-23 21:26:52 -0700 | [diff] [blame] | 102 | mminit_validate_memmodel_limits(&start, &end); |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 103 | bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart)); |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 104 | bdata->node_min_pfn = start; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | bdata->node_low_pfn = end; |
KAMEZAWA Hiroyuki | 679bc9f | 2006-03-27 01:15:58 -0800 | [diff] [blame] | 106 | link_bootmem(bdata); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | * Initially all pages are reserved - setup_arch() has to |
| 110 | * register free RAM areas explicitly. |
| 111 | */ |
Johannes Weiner | df049a5 | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 112 | mapsize = bootmap_bytes(end - start); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | memset(bdata->node_bootmem_map, 0xff, mapsize); |
| 114 | |
Johannes Weiner | 2e5237d | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 115 | bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n", |
| 116 | bdata - bootmem_node_data, start, mapstart, end, mapsize); |
| 117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 118 | return mapsize; |
| 119 | } |
| 120 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 121 | /** |
| 122 | * init_bootmem_node - register a node as boot memory |
| 123 | * @pgdat: node to register |
| 124 | * @freepfn: pfn where the bitmap for this node is to be placed |
| 125 | * @startpfn: first pfn on the node |
| 126 | * @endpfn: first pfn after the node |
| 127 | * |
| 128 | * Returns the number of bytes needed to hold the bitmap for this node. |
| 129 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 130 | unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn, |
| 131 | unsigned long startpfn, unsigned long endpfn) |
| 132 | { |
| 133 | return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn); |
| 134 | } |
| 135 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 136 | /** |
| 137 | * init_bootmem - register boot memory |
| 138 | * @start: pfn where the bitmap is to be placed |
| 139 | * @pages: number of available physical pages |
| 140 | * |
| 141 | * Returns the number of bytes needed to hold the bitmap. |
| 142 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 143 | unsigned long __init init_bootmem(unsigned long start, unsigned long pages) |
| 144 | { |
| 145 | max_low_pfn = pages; |
| 146 | min_low_pfn = start; |
| 147 | return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages); |
| 148 | } |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 149 | #endif |
FUJITA Tomonori | 9f993ac | 2009-11-10 19:46:17 +0900 | [diff] [blame] | 150 | /* |
| 151 | * free_bootmem_late - free bootmem pages directly to page allocator |
| 152 | * @addr: starting address of the range |
| 153 | * @size: size of the range in bytes |
| 154 | * |
| 155 | * This is only useful when the bootmem allocator has already been torn |
| 156 | * down, but we are still initializing the system. Pages are given directly |
| 157 | * to the page allocator, no bootmem metadata is updated because it is gone. |
| 158 | */ |
| 159 | void __init free_bootmem_late(unsigned long addr, unsigned long size) |
| 160 | { |
| 161 | unsigned long cursor, end; |
| 162 | |
| 163 | kmemleak_free_part(__va(addr), size); |
| 164 | |
| 165 | cursor = PFN_UP(addr); |
| 166 | end = PFN_DOWN(addr + size); |
| 167 | |
| 168 | for (; cursor < end; cursor++) { |
| 169 | __free_pages_bootmem(pfn_to_page(cursor), 0); |
| 170 | totalram_pages++; |
| 171 | } |
| 172 | } |
| 173 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 174 | #ifdef CONFIG_NO_BOOTMEM |
| 175 | static void __init __free_pages_memory(unsigned long start, unsigned long end) |
| 176 | { |
| 177 | int i; |
| 178 | unsigned long start_aligned, end_aligned; |
| 179 | int order = ilog2(BITS_PER_LONG); |
| 180 | |
| 181 | start_aligned = (start + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1); |
| 182 | end_aligned = end & ~(BITS_PER_LONG - 1); |
| 183 | |
| 184 | if (end_aligned <= start_aligned) { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 185 | for (i = start; i < end; i++) |
| 186 | __free_pages_bootmem(pfn_to_page(i), 0); |
| 187 | |
| 188 | return; |
| 189 | } |
| 190 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 191 | for (i = start; i < start_aligned; i++) |
| 192 | __free_pages_bootmem(pfn_to_page(i), 0); |
| 193 | |
| 194 | for (i = start_aligned; i < end_aligned; i += BITS_PER_LONG) |
| 195 | __free_pages_bootmem(pfn_to_page(i), order); |
| 196 | |
| 197 | for (i = end_aligned; i < end; i++) |
| 198 | __free_pages_bootmem(pfn_to_page(i), 0); |
| 199 | } |
| 200 | |
| 201 | unsigned long __init free_all_memory_core_early(int nodeid) |
| 202 | { |
| 203 | int i; |
| 204 | u64 start, end; |
| 205 | unsigned long count = 0; |
| 206 | struct range *range = NULL; |
| 207 | int nr_range; |
| 208 | |
| 209 | nr_range = get_free_all_memory_range(&range, nodeid); |
| 210 | |
| 211 | for (i = 0; i < nr_range; i++) { |
| 212 | start = range[i].start; |
| 213 | end = range[i].end; |
| 214 | count += end - start; |
| 215 | __free_pages_memory(start, end); |
| 216 | } |
| 217 | |
| 218 | return count; |
| 219 | } |
| 220 | #else |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 221 | static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata) |
| 222 | { |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 223 | int aligned; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 224 | struct page *page; |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 225 | unsigned long start, end, pages, count = 0; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 226 | |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 227 | if (!bdata->node_bootmem_map) |
| 228 | return 0; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 229 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 230 | start = bdata->node_min_pfn; |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 231 | end = bdata->node_low_pfn; |
| 232 | |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 233 | /* |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 234 | * If the start is aligned to the machines wordsize, we might |
| 235 | * be able to free pages in bulks of that order. |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 236 | */ |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 237 | aligned = !(start & (BITS_PER_LONG - 1)); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 238 | |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 239 | bdebug("nid=%td start=%lx end=%lx aligned=%d\n", |
| 240 | bdata - bootmem_node_data, start, end, aligned); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 241 | |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 242 | while (start < end) { |
| 243 | unsigned long *map, idx, vec; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 244 | |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 245 | map = bdata->node_bootmem_map; |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 246 | idx = start - bdata->node_min_pfn; |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 247 | vec = ~map[idx / BITS_PER_LONG]; |
| 248 | |
| 249 | if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) { |
| 250 | int order = ilog2(BITS_PER_LONG); |
| 251 | |
| 252 | __free_pages_bootmem(pfn_to_page(start), order); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 253 | count += BITS_PER_LONG; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 254 | } else { |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 255 | unsigned long off = 0; |
| 256 | |
| 257 | while (vec && off < BITS_PER_LONG) { |
| 258 | if (vec & 1) { |
| 259 | page = pfn_to_page(start + off); |
| 260 | __free_pages_bootmem(page, 0); |
| 261 | count++; |
| 262 | } |
| 263 | vec >>= 1; |
| 264 | off++; |
| 265 | } |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 266 | } |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 267 | start += BITS_PER_LONG; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 268 | } |
| 269 | |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 270 | page = virt_to_page(bdata->node_bootmem_map); |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 271 | pages = bdata->node_low_pfn - bdata->node_min_pfn; |
Johannes Weiner | 41546c1 | 2008-07-23 21:28:03 -0700 | [diff] [blame] | 272 | pages = bootmem_bootmap_pages(pages); |
| 273 | count += pages; |
| 274 | while (pages--) |
| 275 | __free_pages_bootmem(page++, 0); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 276 | |
Johannes Weiner | 2e5237d | 2008-07-23 21:28:02 -0700 | [diff] [blame] | 277 | bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count); |
| 278 | |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 279 | return count; |
| 280 | } |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 281 | #endif |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 282 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 283 | /** |
| 284 | * free_all_bootmem_node - release a node's free pages to the buddy allocator |
| 285 | * @pgdat: node to be released |
| 286 | * |
| 287 | * Returns the number of pages actually released. |
| 288 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 289 | unsigned long __init free_all_bootmem_node(pg_data_t *pgdat) |
| 290 | { |
| 291 | register_page_bootmem_info_node(pgdat); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 292 | #ifdef CONFIG_NO_BOOTMEM |
| 293 | /* free_all_memory_core_early(MAX_NUMNODES) will be called later */ |
| 294 | return 0; |
| 295 | #else |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 296 | return free_all_bootmem_core(pgdat->bdata); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 297 | #endif |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 298 | } |
| 299 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 300 | /** |
| 301 | * free_all_bootmem - release free pages to the buddy allocator |
| 302 | * |
| 303 | * Returns the number of pages actually released. |
| 304 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 305 | unsigned long __init free_all_bootmem(void) |
| 306 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 307 | #ifdef CONFIG_NO_BOOTMEM |
Yinghai Lu | 3379985 | 2010-03-31 20:44:09 -0700 | [diff] [blame] | 308 | /* |
| 309 | * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id |
| 310 | * because in some case like Node0 doesnt have RAM installed |
| 311 | * low ram will be on Node1 |
| 312 | * Use MAX_NUMNODES will make sure all ranges in early_node_map[] |
| 313 | * will be used instead of only Node0 related |
| 314 | */ |
| 315 | return free_all_memory_core_early(MAX_NUMNODES); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 316 | #else |
Yinghai Lu | aa235fc | 2010-03-31 20:45:27 -0700 | [diff] [blame] | 317 | unsigned long total_pages = 0; |
| 318 | bootmem_data_t *bdata; |
| 319 | |
| 320 | list_for_each_entry(bdata, &bdata_list, list) |
| 321 | total_pages += free_all_bootmem_core(bdata); |
| 322 | |
| 323 | return total_pages; |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 324 | #endif |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 327 | #ifndef CONFIG_NO_BOOTMEM |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 328 | static void __init __free(bootmem_data_t *bdata, |
| 329 | unsigned long sidx, unsigned long eidx) |
| 330 | { |
| 331 | unsigned long idx; |
| 332 | |
| 333 | bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data, |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 334 | sidx + bdata->node_min_pfn, |
| 335 | eidx + bdata->node_min_pfn); |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 336 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 337 | if (bdata->hint_idx > sidx) |
| 338 | bdata->hint_idx = sidx; |
| 339 | |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 340 | for (idx = sidx; idx < eidx; idx++) |
| 341 | if (!test_and_clear_bit(idx, bdata->node_bootmem_map)) |
| 342 | BUG(); |
| 343 | } |
| 344 | |
| 345 | static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx, |
| 346 | unsigned long eidx, int flags) |
| 347 | { |
| 348 | unsigned long idx; |
| 349 | int exclusive = flags & BOOTMEM_EXCLUSIVE; |
| 350 | |
| 351 | bdebug("nid=%td start=%lx end=%lx flags=%x\n", |
| 352 | bdata - bootmem_node_data, |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 353 | sidx + bdata->node_min_pfn, |
| 354 | eidx + bdata->node_min_pfn, |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 355 | flags); |
| 356 | |
| 357 | for (idx = sidx; idx < eidx; idx++) |
| 358 | if (test_and_set_bit(idx, bdata->node_bootmem_map)) { |
| 359 | if (exclusive) { |
| 360 | __free(bdata, sidx, idx); |
| 361 | return -EBUSY; |
| 362 | } |
| 363 | bdebug("silent double reserve of PFN %lx\n", |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 364 | idx + bdata->node_min_pfn); |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 365 | } |
| 366 | return 0; |
| 367 | } |
| 368 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 369 | static int __init mark_bootmem_node(bootmem_data_t *bdata, |
| 370 | unsigned long start, unsigned long end, |
| 371 | int reserve, int flags) |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 372 | { |
| 373 | unsigned long sidx, eidx; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 374 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 375 | bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n", |
| 376 | bdata - bootmem_node_data, start, end, reserve, flags); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 377 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 378 | BUG_ON(start < bdata->node_min_pfn); |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 379 | BUG_ON(end > bdata->node_low_pfn); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 380 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 381 | sidx = start - bdata->node_min_pfn; |
| 382 | eidx = end - bdata->node_min_pfn; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 383 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 384 | if (reserve) |
| 385 | return __reserve(bdata, sidx, eidx, flags); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 386 | else |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 387 | __free(bdata, sidx, eidx); |
| 388 | return 0; |
| 389 | } |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 390 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 391 | static int __init mark_bootmem(unsigned long start, unsigned long end, |
| 392 | int reserve, int flags) |
| 393 | { |
| 394 | unsigned long pos; |
| 395 | bootmem_data_t *bdata; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 396 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 397 | pos = start; |
| 398 | list_for_each_entry(bdata, &bdata_list, list) { |
| 399 | int err; |
| 400 | unsigned long max; |
| 401 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 402 | if (pos < bdata->node_min_pfn || |
| 403 | pos >= bdata->node_low_pfn) { |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 404 | BUG_ON(pos != start); |
| 405 | continue; |
| 406 | } |
| 407 | |
| 408 | max = min(bdata->node_low_pfn, end); |
| 409 | |
| 410 | err = mark_bootmem_node(bdata, pos, max, reserve, flags); |
| 411 | if (reserve && err) { |
| 412 | mark_bootmem(start, pos, 0, 0); |
| 413 | return err; |
| 414 | } |
| 415 | |
| 416 | if (max == end) |
| 417 | return 0; |
| 418 | pos = bdata->node_low_pfn; |
| 419 | } |
| 420 | BUG(); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 421 | } |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 422 | #endif |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 423 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 424 | /** |
| 425 | * free_bootmem_node - mark a page range as usable |
| 426 | * @pgdat: node the range resides on |
| 427 | * @physaddr: starting address of the range |
| 428 | * @size: size of the range in bytes |
| 429 | * |
| 430 | * Partial pages will be considered reserved and left as they are. |
| 431 | * |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 432 | * The range must reside completely on the specified node. |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 433 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 434 | void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
| 435 | unsigned long size) |
| 436 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 437 | #ifdef CONFIG_NO_BOOTMEM |
Yinghai Lu | 72d7c3b | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 438 | kmemleak_free_part(__va(physaddr), size); |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 439 | memblock_x86_free_range(physaddr, physaddr + size); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 440 | #else |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 441 | unsigned long start, end; |
| 442 | |
Catalin Marinas | ec3a354 | 2009-07-07 10:33:01 +0100 | [diff] [blame] | 443 | kmemleak_free_part(__va(physaddr), size); |
| 444 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 445 | start = PFN_UP(physaddr); |
| 446 | end = PFN_DOWN(physaddr + size); |
| 447 | |
| 448 | mark_bootmem_node(pgdat->bdata, start, end, 0, 0); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 449 | #endif |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 450 | } |
| 451 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 452 | /** |
| 453 | * free_bootmem - mark a page range as usable |
| 454 | * @addr: starting address of the range |
| 455 | * @size: size of the range in bytes |
| 456 | * |
| 457 | * Partial pages will be considered reserved and left as they are. |
| 458 | * |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 459 | * The range must be contiguous but may span node boundaries. |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 460 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 461 | void __init free_bootmem(unsigned long addr, unsigned long size) |
| 462 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 463 | #ifdef CONFIG_NO_BOOTMEM |
Yinghai Lu | 72d7c3b | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 464 | kmemleak_free_part(__va(addr), size); |
Yinghai Lu | a9ce6bc | 2010-08-25 13:39:17 -0700 | [diff] [blame] | 465 | memblock_x86_free_range(addr, addr + size); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 466 | #else |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 467 | unsigned long start, end; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 468 | |
Catalin Marinas | ec3a354 | 2009-07-07 10:33:01 +0100 | [diff] [blame] | 469 | kmemleak_free_part(__va(addr), size); |
| 470 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 471 | start = PFN_UP(addr); |
| 472 | end = PFN_DOWN(addr + size); |
Yinghai Lu | a5645a6 | 2008-03-18 12:49:12 -0700 | [diff] [blame] | 473 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 474 | mark_bootmem(start, end, 0, 0); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 475 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | } |
| 477 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 478 | /** |
| 479 | * reserve_bootmem_node - mark a page range as reserved |
| 480 | * @pgdat: node the range resides on |
| 481 | * @physaddr: starting address of the range |
| 482 | * @size: size of the range in bytes |
| 483 | * @flags: reservation flags (see linux/bootmem.h) |
| 484 | * |
| 485 | * Partial pages will be reserved. |
| 486 | * |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 487 | * The range must reside completely on the specified node. |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 488 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 489 | int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr, |
| 490 | unsigned long size, int flags) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 492 | #ifdef CONFIG_NO_BOOTMEM |
| 493 | panic("no bootmem"); |
| 494 | return 0; |
| 495 | #else |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 496 | unsigned long start, end; |
Franck Bui-Huu | bbc7b92 | 2006-09-25 23:31:07 -0700 | [diff] [blame] | 497 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 498 | start = PFN_DOWN(physaddr); |
| 499 | end = PFN_UP(physaddr + size); |
| 500 | |
| 501 | return mark_bootmem_node(pgdat->bdata, start, end, 1, flags); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 502 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 505 | /** |
| 506 | * reserve_bootmem - mark a page range as usable |
| 507 | * @addr: starting address of the range |
| 508 | * @size: size of the range in bytes |
| 509 | * @flags: reservation flags (see linux/bootmem.h) |
| 510 | * |
| 511 | * Partial pages will be reserved. |
| 512 | * |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 513 | * The range must be contiguous but may span node boundaries. |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 514 | */ |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 515 | int __init reserve_bootmem(unsigned long addr, unsigned long size, |
| 516 | int flags) |
| 517 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 518 | #ifdef CONFIG_NO_BOOTMEM |
| 519 | panic("no bootmem"); |
| 520 | return 0; |
| 521 | #else |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 522 | unsigned long start, end; |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 523 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 524 | start = PFN_DOWN(addr); |
| 525 | end = PFN_UP(addr + size); |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 526 | |
Johannes Weiner | e2bf3ca | 2008-07-23 21:28:06 -0700 | [diff] [blame] | 527 | return mark_bootmem(start, end, 1, flags); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 528 | #endif |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 529 | } |
Johannes Weiner | 223e8dc | 2008-07-23 21:28:00 -0700 | [diff] [blame] | 530 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 531 | #ifndef CONFIG_NO_BOOTMEM |
Yinghai Lu | f88eff7 | 2010-08-25 13:39:15 -0700 | [diff] [blame] | 532 | int __weak __init reserve_bootmem_generic(unsigned long phys, unsigned long len, |
| 533 | int flags) |
| 534 | { |
| 535 | return reserve_bootmem(phys, len, flags); |
| 536 | } |
| 537 | |
Jan Beulich | 8aa043d | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 538 | static unsigned long __init align_idx(struct bootmem_data *bdata, |
| 539 | unsigned long idx, unsigned long step) |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 540 | { |
| 541 | unsigned long base = bdata->node_min_pfn; |
| 542 | |
| 543 | /* |
| 544 | * Align the index with respect to the node start so that the |
| 545 | * combination of both satisfies the requested alignment. |
| 546 | */ |
| 547 | |
| 548 | return ALIGN(base + idx, step) - base; |
| 549 | } |
| 550 | |
Jan Beulich | 8aa043d | 2009-12-14 17:59:46 -0800 | [diff] [blame] | 551 | static unsigned long __init align_off(struct bootmem_data *bdata, |
| 552 | unsigned long off, unsigned long align) |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 553 | { |
| 554 | unsigned long base = PFN_PHYS(bdata->node_min_pfn); |
| 555 | |
| 556 | /* Same as align_idx for byte offsets */ |
| 557 | |
| 558 | return ALIGN(base + off, align) - base; |
| 559 | } |
| 560 | |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 561 | static void * __init alloc_bootmem_core(struct bootmem_data *bdata, |
| 562 | unsigned long size, unsigned long align, |
| 563 | unsigned long goal, unsigned long limit) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | { |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 565 | unsigned long fallback = 0; |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 566 | unsigned long min, max, start, sidx, midx, step; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | |
Johannes Weiner | 594fe1a | 2009-01-06 14:40:32 -0800 | [diff] [blame] | 568 | bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n", |
| 569 | bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT, |
| 570 | align, goal, limit); |
| 571 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 572 | BUG_ON(!size); |
| 573 | BUG_ON(align & (align - 1)); |
| 574 | BUG_ON(limit && goal + size > limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 575 | |
Christian Krafft | 7c309a6 | 2006-12-06 20:32:41 -0800 | [diff] [blame] | 576 | if (!bdata->node_bootmem_map) |
| 577 | return NULL; |
| 578 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 579 | min = bdata->node_min_pfn; |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 580 | max = bdata->node_low_pfn; |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 581 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 582 | goal >>= PAGE_SHIFT; |
| 583 | limit >>= PAGE_SHIFT; |
| 584 | |
| 585 | if (limit && max > limit) |
| 586 | max = limit; |
| 587 | if (max <= min) |
Yinghai Lu | 9a2dc04 | 2008-03-18 12:44:48 -0700 | [diff] [blame] | 588 | return NULL; |
| 589 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 590 | step = max(align >> PAGE_SHIFT, 1UL); |
Yasunori Goto | 281dd25 | 2005-10-19 15:52:18 -0700 | [diff] [blame] | 591 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 592 | if (goal && min < goal && goal < max) |
| 593 | start = ALIGN(goal, step); |
| 594 | else |
| 595 | start = ALIGN(min, step); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 596 | |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 597 | sidx = start - bdata->node_min_pfn; |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 598 | midx = max - bdata->node_min_pfn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 600 | if (bdata->hint_idx > sidx) { |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 601 | /* |
| 602 | * Handle the valid case of sidx being zero and still |
| 603 | * catch the fallback below. |
| 604 | */ |
| 605 | fallback = sidx + 1; |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 606 | sidx = align_idx(bdata, bdata->hint_idx, step); |
Yinghai Lu | ad09315 | 2008-03-10 23:23:42 -0700 | [diff] [blame] | 607 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 608 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 609 | while (1) { |
| 610 | int merge; |
| 611 | void *region; |
| 612 | unsigned long eidx, i, start_off, end_off; |
| 613 | find_block: |
| 614 | sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx); |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 615 | sidx = align_idx(bdata, sidx, step); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 616 | eidx = sidx + PFN_UP(size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 618 | if (sidx >= midx || eidx > midx) |
Haren Myneni | 66d43e9 | 2005-12-12 00:37:39 -0800 | [diff] [blame] | 619 | break; |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 620 | |
| 621 | for (i = sidx; i < eidx; i++) |
| 622 | if (test_bit(i, bdata->node_bootmem_map)) { |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 623 | sidx = align_idx(bdata, i, step); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 624 | if (sidx == i) |
| 625 | sidx += step; |
| 626 | goto find_block; |
| 627 | } |
| 628 | |
Mikulas Patocka | 627240a | 2008-08-15 00:40:17 -0700 | [diff] [blame] | 629 | if (bdata->last_end_off & (PAGE_SIZE - 1) && |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 630 | PFN_DOWN(bdata->last_end_off) + 1 == sidx) |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 631 | start_off = align_off(bdata, bdata->last_end_off, align); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 632 | else |
| 633 | start_off = PFN_PHYS(sidx); |
| 634 | |
| 635 | merge = PFN_DOWN(start_off) < sidx; |
| 636 | end_off = start_off + size; |
| 637 | |
| 638 | bdata->last_end_off = end_off; |
| 639 | bdata->hint_idx = PFN_UP(end_off); |
| 640 | |
| 641 | /* |
| 642 | * Reserve the area now: |
| 643 | */ |
Johannes Weiner | d747fa4 | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 644 | if (__reserve(bdata, PFN_DOWN(start_off) + merge, |
| 645 | PFN_UP(end_off), BOOTMEM_EXCLUSIVE)) |
| 646 | BUG(); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 647 | |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 648 | region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) + |
| 649 | start_off); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 650 | memset(region, 0, size); |
Catalin Marinas | 008139d | 2009-08-27 14:29:17 +0100 | [diff] [blame] | 651 | /* |
| 652 | * The min_count is set to 0 so that bootmem allocated blocks |
| 653 | * are never reported as leaks. |
| 654 | */ |
| 655 | kmemleak_alloc(region, size, 0, 0); |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 656 | return region; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
| 658 | |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 659 | if (fallback) { |
Johannes Weiner | 481ebd0 | 2008-08-20 14:09:15 -0700 | [diff] [blame] | 660 | sidx = align_idx(bdata, fallback - 1, step); |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 661 | fallback = 0; |
| 662 | goto find_block; |
| 663 | } |
| 664 | |
| 665 | return NULL; |
| 666 | } |
| 667 | |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 668 | static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata, |
| 669 | unsigned long size, unsigned long align, |
| 670 | unsigned long goal, unsigned long limit) |
| 671 | { |
Pekka Enberg | 441c7e0 | 2009-06-10 20:05:53 +0300 | [diff] [blame] | 672 | if (WARN_ON_ONCE(slab_is_available())) |
| 673 | return kzalloc(size, GFP_NOWAIT); |
| 674 | |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 675 | #ifdef CONFIG_HAVE_ARCH_BOOTMEM |
Joe Perches | 433f13a | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 676 | { |
| 677 | bootmem_data_t *p_bdata; |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 678 | |
Joe Perches | 433f13a | 2009-06-18 16:48:58 -0700 | [diff] [blame] | 679 | p_bdata = bootmem_arch_preferred_node(bdata, size, align, |
| 680 | goal, limit); |
| 681 | if (p_bdata) |
| 682 | return alloc_bootmem_core(p_bdata, size, align, |
| 683 | goal, limit); |
| 684 | } |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 685 | #endif |
| 686 | return NULL; |
| 687 | } |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 688 | #endif |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 689 | |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 690 | static void * __init ___alloc_bootmem_nopanic(unsigned long size, |
| 691 | unsigned long align, |
| 692 | unsigned long goal, |
| 693 | unsigned long limit) |
| 694 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 695 | #ifdef CONFIG_NO_BOOTMEM |
| 696 | void *ptr; |
| 697 | |
| 698 | if (WARN_ON_ONCE(slab_is_available())) |
| 699 | return kzalloc(size, GFP_NOWAIT); |
| 700 | |
| 701 | restart: |
| 702 | |
| 703 | ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit); |
| 704 | |
| 705 | if (ptr) |
| 706 | return ptr; |
| 707 | |
| 708 | if (goal != 0) { |
| 709 | goal = 0; |
| 710 | goto restart; |
| 711 | } |
| 712 | |
| 713 | return NULL; |
| 714 | #else |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 715 | bootmem_data_t *bdata; |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 716 | void *region; |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 717 | |
| 718 | restart: |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 719 | region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit); |
| 720 | if (region) |
| 721 | return region; |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 722 | |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 723 | list_for_each_entry(bdata, &bdata_list, list) { |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 724 | if (goal && bdata->node_low_pfn <= PFN_DOWN(goal)) |
| 725 | continue; |
Johannes Weiner | 3560e24 | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 726 | if (limit && bdata->node_min_pfn >= PFN_DOWN(limit)) |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 727 | break; |
| 728 | |
| 729 | region = alloc_bootmem_core(bdata, size, align, goal, limit); |
| 730 | if (region) |
| 731 | return region; |
| 732 | } |
| 733 | |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 734 | if (goal) { |
| 735 | goal = 0; |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 736 | goto restart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | } |
Johannes Weiner | 5f2809e | 2008-07-23 21:28:05 -0700 | [diff] [blame] | 738 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 739 | return NULL; |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 740 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 741 | } |
| 742 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 743 | /** |
| 744 | * __alloc_bootmem_nopanic - allocate boot memory without panicking |
| 745 | * @size: size of the request in bytes |
| 746 | * @align: alignment of the region |
| 747 | * @goal: preferred starting address of the region |
| 748 | * |
| 749 | * The goal is dropped if it can not be satisfied and the allocation will |
| 750 | * fall back to memory below @goal. |
| 751 | * |
| 752 | * Allocation may happen on any node in the system. |
| 753 | * |
| 754 | * Returns NULL on failure. |
| 755 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 756 | void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align, |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 757 | unsigned long goal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 758 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 759 | unsigned long limit = 0; |
| 760 | |
| 761 | #ifdef CONFIG_NO_BOOTMEM |
| 762 | limit = -1UL; |
| 763 | #endif |
| 764 | |
| 765 | return ___alloc_bootmem_nopanic(size, align, goal, limit); |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 768 | static void * __init ___alloc_bootmem(unsigned long size, unsigned long align, |
| 769 | unsigned long goal, unsigned long limit) |
| 770 | { |
| 771 | void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit); |
| 772 | |
| 773 | if (mem) |
| 774 | return mem; |
| 775 | /* |
| 776 | * Whoops, we cannot satisfy the allocation request. |
| 777 | */ |
| 778 | printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size); |
| 779 | panic("Out of memory"); |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 780 | return NULL; |
| 781 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 782 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 783 | /** |
| 784 | * __alloc_bootmem - allocate boot memory |
| 785 | * @size: size of the request in bytes |
| 786 | * @align: alignment of the region |
| 787 | * @goal: preferred starting address of the region |
| 788 | * |
| 789 | * The goal is dropped if it can not be satisfied and the allocation will |
| 790 | * fall back to memory below @goal. |
| 791 | * |
| 792 | * Allocation may happen on any node in the system. |
| 793 | * |
| 794 | * The function panics if the request can not be satisfied. |
| 795 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 796 | void * __init __alloc_bootmem(unsigned long size, unsigned long align, |
| 797 | unsigned long goal) |
Andi Kleen | a806223 | 2006-04-07 19:49:21 +0200 | [diff] [blame] | 798 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 799 | unsigned long limit = 0; |
| 800 | |
| 801 | #ifdef CONFIG_NO_BOOTMEM |
| 802 | limit = -1UL; |
| 803 | #endif |
| 804 | |
| 805 | return ___alloc_bootmem(size, align, goal, limit); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | } |
| 807 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 808 | #ifndef CONFIG_NO_BOOTMEM |
Johannes Weiner | 4cc278b | 2008-07-23 21:28:08 -0700 | [diff] [blame] | 809 | static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata, |
| 810 | unsigned long size, unsigned long align, |
| 811 | unsigned long goal, unsigned long limit) |
| 812 | { |
| 813 | void *ptr; |
| 814 | |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 815 | ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit); |
| 816 | if (ptr) |
| 817 | return ptr; |
| 818 | |
Johannes Weiner | 4cc278b | 2008-07-23 21:28:08 -0700 | [diff] [blame] | 819 | ptr = alloc_bootmem_core(bdata, size, align, goal, limit); |
| 820 | if (ptr) |
| 821 | return ptr; |
| 822 | |
| 823 | return ___alloc_bootmem(size, align, goal, limit); |
| 824 | } |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 825 | #endif |
Johannes Weiner | 4cc278b | 2008-07-23 21:28:08 -0700 | [diff] [blame] | 826 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 827 | /** |
| 828 | * __alloc_bootmem_node - allocate boot memory from a specific node |
| 829 | * @pgdat: node to allocate from |
| 830 | * @size: size of the request in bytes |
| 831 | * @align: alignment of the region |
| 832 | * @goal: preferred starting address of the region |
| 833 | * |
| 834 | * The goal is dropped if it can not be satisfied and the allocation will |
| 835 | * fall back to memory below @goal. |
| 836 | * |
| 837 | * Allocation may fall back to any node in the system if the specified node |
| 838 | * can not hold the requested memory. |
| 839 | * |
| 840 | * The function panics if the request can not be satisfied. |
| 841 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 842 | void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size, |
| 843 | unsigned long align, unsigned long goal) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 844 | { |
Yinghai Lu | b8ab9f8 | 2010-07-20 13:24:31 -0700 | [diff] [blame] | 845 | void *ptr; |
| 846 | |
Pekka Enberg | c91c477 | 2009-06-11 08:10:28 +0300 | [diff] [blame] | 847 | if (WARN_ON_ONCE(slab_is_available())) |
| 848 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 849 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 850 | #ifdef CONFIG_NO_BOOTMEM |
Yinghai Lu | b8ab9f8 | 2010-07-20 13:24:31 -0700 | [diff] [blame] | 851 | ptr = __alloc_memory_core_early(pgdat->node_id, size, align, |
| 852 | goal, -1ULL); |
| 853 | if (ptr) |
| 854 | return ptr; |
| 855 | |
| 856 | ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 857 | goal, -1ULL); |
| 858 | #else |
Yinghai Lu | b8ab9f8 | 2010-07-20 13:24:31 -0700 | [diff] [blame] | 859 | ptr = ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 860 | #endif |
Yinghai Lu | b8ab9f8 | 2010-07-20 13:24:31 -0700 | [diff] [blame] | 861 | |
| 862 | return ptr; |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 863 | } |
| 864 | |
| 865 | void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size, |
| 866 | unsigned long align, unsigned long goal) |
| 867 | { |
| 868 | #ifdef MAX_DMA32_PFN |
| 869 | unsigned long end_pfn; |
| 870 | |
| 871 | if (WARN_ON_ONCE(slab_is_available())) |
| 872 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 873 | |
| 874 | /* update goal according ...MAX_DMA32_PFN */ |
| 875 | end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages; |
| 876 | |
| 877 | if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) && |
| 878 | (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) { |
| 879 | void *ptr; |
| 880 | unsigned long new_goal; |
| 881 | |
| 882 | new_goal = MAX_DMA32_PFN << PAGE_SHIFT; |
| 883 | #ifdef CONFIG_NO_BOOTMEM |
| 884 | ptr = __alloc_memory_core_early(pgdat->node_id, size, align, |
| 885 | new_goal, -1ULL); |
| 886 | #else |
| 887 | ptr = alloc_bootmem_core(pgdat->bdata, size, align, |
| 888 | new_goal, 0); |
| 889 | #endif |
| 890 | if (ptr) |
| 891 | return ptr; |
| 892 | } |
| 893 | #endif |
| 894 | |
| 895 | return __alloc_bootmem_node(pgdat, size, align, goal); |
| 896 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 897 | } |
| 898 | |
Yasunori Goto | e70260a | 2008-04-28 02:13:32 -0700 | [diff] [blame] | 899 | #ifdef CONFIG_SPARSEMEM |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 900 | /** |
| 901 | * alloc_bootmem_section - allocate boot memory from a specific section |
| 902 | * @size: size of the request in bytes |
| 903 | * @section_nr: sparse map section to allocate from |
| 904 | * |
| 905 | * Return NULL on failure. |
| 906 | */ |
Yasunori Goto | e70260a | 2008-04-28 02:13:32 -0700 | [diff] [blame] | 907 | void * __init alloc_bootmem_section(unsigned long size, |
| 908 | unsigned long section_nr) |
| 909 | { |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 910 | #ifdef CONFIG_NO_BOOTMEM |
| 911 | unsigned long pfn, goal, limit; |
| 912 | |
| 913 | pfn = section_nr_to_pfn(section_nr); |
| 914 | goal = pfn << PAGE_SHIFT; |
| 915 | limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT; |
| 916 | |
| 917 | return __alloc_memory_core_early(early_pfn_to_nid(pfn), size, |
| 918 | SMP_CACHE_BYTES, goal, limit); |
| 919 | #else |
Johannes Weiner | 75a56cf | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 920 | bootmem_data_t *bdata; |
| 921 | unsigned long pfn, goal, limit; |
Yasunori Goto | e70260a | 2008-04-28 02:13:32 -0700 | [diff] [blame] | 922 | |
| 923 | pfn = section_nr_to_pfn(section_nr); |
Johannes Weiner | 75a56cf | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 924 | goal = pfn << PAGE_SHIFT; |
| 925 | limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT; |
| 926 | bdata = &bootmem_node_data[early_pfn_to_nid(pfn)]; |
Yasunori Goto | e70260a | 2008-04-28 02:13:32 -0700 | [diff] [blame] | 927 | |
Johannes Weiner | 75a56cf | 2008-07-23 21:28:09 -0700 | [diff] [blame] | 928 | return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 929 | #endif |
Yasunori Goto | e70260a | 2008-04-28 02:13:32 -0700 | [diff] [blame] | 930 | } |
| 931 | #endif |
| 932 | |
Andi Kleen | b54bbf7 | 2008-07-23 21:27:45 -0700 | [diff] [blame] | 933 | void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size, |
| 934 | unsigned long align, unsigned long goal) |
| 935 | { |
| 936 | void *ptr; |
| 937 | |
Pekka Enberg | c91c477 | 2009-06-11 08:10:28 +0300 | [diff] [blame] | 938 | if (WARN_ON_ONCE(slab_is_available())) |
| 939 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 940 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 941 | #ifdef CONFIG_NO_BOOTMEM |
| 942 | ptr = __alloc_memory_core_early(pgdat->node_id, size, align, |
| 943 | goal, -1ULL); |
| 944 | #else |
Tejun Heo | d0c4f57 | 2009-03-01 16:06:56 +0900 | [diff] [blame] | 945 | ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0); |
| 946 | if (ptr) |
| 947 | return ptr; |
| 948 | |
Andi Kleen | b54bbf7 | 2008-07-23 21:27:45 -0700 | [diff] [blame] | 949 | ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 950 | #endif |
Andi Kleen | b54bbf7 | 2008-07-23 21:27:45 -0700 | [diff] [blame] | 951 | if (ptr) |
| 952 | return ptr; |
| 953 | |
| 954 | return __alloc_bootmem_nopanic(size, align, goal); |
| 955 | } |
| 956 | |
Heiko Carstens | dfd54cb | 2006-09-25 23:31:33 -0700 | [diff] [blame] | 957 | #ifndef ARCH_LOW_ADDRESS_LIMIT |
| 958 | #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL |
| 959 | #endif |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 960 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 961 | /** |
| 962 | * __alloc_bootmem_low - allocate low boot memory |
| 963 | * @size: size of the request in bytes |
| 964 | * @align: alignment of the region |
| 965 | * @goal: preferred starting address of the region |
| 966 | * |
| 967 | * The goal is dropped if it can not be satisfied and the allocation will |
| 968 | * fall back to memory below @goal. |
| 969 | * |
| 970 | * Allocation may happen on any node in the system. |
| 971 | * |
| 972 | * The function panics if the request can not be satisfied. |
| 973 | */ |
Franck Bui-Huu | bb0923a | 2006-09-25 23:31:05 -0700 | [diff] [blame] | 974 | void * __init __alloc_bootmem_low(unsigned long size, unsigned long align, |
| 975 | unsigned long goal) |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 976 | { |
Johannes Weiner | 0f3caba | 2008-07-23 21:28:07 -0700 | [diff] [blame] | 977 | return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT); |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 978 | } |
| 979 | |
Johannes Weiner | a66fd7d | 2008-07-23 21:28:01 -0700 | [diff] [blame] | 980 | /** |
| 981 | * __alloc_bootmem_low_node - allocate low boot memory from a specific node |
| 982 | * @pgdat: node to allocate from |
| 983 | * @size: size of the request in bytes |
| 984 | * @align: alignment of the region |
| 985 | * @goal: preferred starting address of the region |
| 986 | * |
| 987 | * The goal is dropped if it can not be satisfied and the allocation will |
| 988 | * fall back to memory below @goal. |
| 989 | * |
| 990 | * Allocation may fall back to any node in the system if the specified node |
| 991 | * can not hold the requested memory. |
| 992 | * |
| 993 | * The function panics if the request can not be satisfied. |
| 994 | */ |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 995 | void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size, |
| 996 | unsigned long align, unsigned long goal) |
| 997 | { |
Yinghai Lu | b8ab9f8 | 2010-07-20 13:24:31 -0700 | [diff] [blame] | 998 | void *ptr; |
| 999 | |
Pekka Enberg | c91c477 | 2009-06-11 08:10:28 +0300 | [diff] [blame] | 1000 | if (WARN_ON_ONCE(slab_is_available())) |
| 1001 | return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id); |
| 1002 | |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 1003 | #ifdef CONFIG_NO_BOOTMEM |
Yinghai Lu | b8ab9f8 | 2010-07-20 13:24:31 -0700 | [diff] [blame] | 1004 | ptr = __alloc_memory_core_early(pgdat->node_id, size, align, |
| 1005 | goal, ARCH_LOW_ADDRESS_LIMIT); |
| 1006 | if (ptr) |
| 1007 | return ptr; |
| 1008 | ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 1009 | goal, ARCH_LOW_ADDRESS_LIMIT); |
| 1010 | #else |
Yinghai Lu | b8ab9f8 | 2010-07-20 13:24:31 -0700 | [diff] [blame] | 1011 | ptr = ___alloc_bootmem_node(pgdat->bdata, size, align, |
Johannes Weiner | 4cc278b | 2008-07-23 21:28:08 -0700 | [diff] [blame] | 1012 | goal, ARCH_LOW_ADDRESS_LIMIT); |
Yinghai Lu | 0867721 | 2010-02-10 01:20:20 -0800 | [diff] [blame] | 1013 | #endif |
Yinghai Lu | b8ab9f8 | 2010-07-20 13:24:31 -0700 | [diff] [blame] | 1014 | return ptr; |
Ravikiran G Thirumalai | 008857c | 2006-01-06 00:11:01 -0800 | [diff] [blame] | 1015 | } |