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