blob: 58c66cc5056a7073aabf880116548a9572c2894b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Johannes Weiner57cfc292008-07-23 21:28:00 -07002 * bootmem - A boot-time physical memory allocator and configurator
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 1999 Ingo Molnar
Johannes Weiner57cfc292008-07-23 21:28:00 -07005 * 1999 Kanoj Sarcar, SGI
6 * 2008 Johannes Weiner
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Johannes Weiner57cfc292008-07-23 21:28:00 -07008 * Access to this subsystem has to be serialized externally (which is true
9 * for the boot process anyway).
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/init.h>
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070012#include <linux/pfn.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090013#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include <linux/bootmem.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
Catalin Marinasec3a3542009-07-07 10:33:01 +010016#include <linux/kmemleak.h>
Yinghai Lu08677212010-02-10 01:20:20 -080017#include <linux/range.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070018
19#include <asm/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <asm/io.h>
Heiko Carstensdfd54cb2006-09-25 23:31:33 -070021#include <asm/processor.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include "internal.h"
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025unsigned long max_low_pfn;
26unsigned long min_low_pfn;
27unsigned long max_pfn;
28
Vivek Goyal92aa63a2005-06-25 14:58:18 -070029#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 */
34unsigned long saved_max_pfn;
35#endif
36
Yinghai Lu08677212010-02-10 01:20:20 -080037#ifndef CONFIG_NO_BOOTMEM
Johannes Weinerb61bfa32008-07-23 21:26:55 -070038bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
39
Johannes Weiner636cc402008-07-23 21:28:03 -070040static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
41
Johannes Weiner2e5237d2008-07-23 21:28:02 -070042static int bootmem_debug;
43
44static int __init bootmem_debug_setup(char *buf)
45{
46 bootmem_debug = 1;
47 return 0;
48}
49early_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 Harrison80a914d2008-10-15 22:01:25 -070055 __func__, ## args); \
Johannes Weiner2e5237d2008-07-23 21:28:02 -070056})
57
Johannes Weinerdf049a52008-07-23 21:28:02 -070058static unsigned long __init bootmap_bytes(unsigned long pages)
Johannes Weiner223e8dc2008-07-23 21:28:00 -070059{
Johannes Weinerdf049a52008-07-23 21:28:02 -070060 unsigned long bytes = (pages + 7) / 8;
Johannes Weiner223e8dc2008-07-23 21:28:00 -070061
Johannes Weinerdf049a52008-07-23 21:28:02 -070062 return ALIGN(bytes, sizeof(long));
Johannes Weiner223e8dc2008-07-23 21:28:00 -070063}
64
Johannes Weinera66fd7d2008-07-23 21:28:01 -070065/**
66 * bootmem_bootmap_pages - calculate bitmap size in pages
67 * @pages: number of pages the bitmap has to represent
68 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070069unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Johannes Weinerdf049a52008-07-23 21:28:02 -070071 unsigned long bytes = bootmap_bytes(pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
Johannes Weinerdf049a52008-07-23 21:28:02 -070073 return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070075
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080076/*
77 * link bdata in order
78 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070079static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080080{
Johannes Weiner636cc402008-07-23 21:28:03 -070081 struct list_head *iter;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070082
Johannes Weiner636cc402008-07-23 21:28:03 -070083 list_for_each(iter, &bdata_list) {
84 bootmem_data_t *ent;
85
86 ent = list_entry(iter, bootmem_data_t, list);
Johannes Weiner3560e242008-07-23 21:28:09 -070087 if (bdata->node_min_pfn < ent->node_min_pfn)
Johannes Weiner636cc402008-07-23 21:28:03 -070088 break;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080089 }
Johannes Weiner636cc402008-07-23 21:28:03 -070090 list_add_tail(&bdata->list, iter);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080091}
92
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070093/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 * Called once to set up the allocator itself.
95 */
Johannes Weiner8ae04462008-07-23 21:26:59 -070096static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 unsigned long mapstart, unsigned long start, unsigned long end)
98{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070099 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700101 mminit_validate_memmodel_limits(&start, &end);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700102 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
Johannes Weiner3560e242008-07-23 21:28:09 -0700103 bdata->node_min_pfn = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800105 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /*
108 * Initially all pages are reserved - setup_arch() has to
109 * register free RAM areas explicitly.
110 */
Johannes Weinerdf049a52008-07-23 21:28:02 -0700111 mapsize = bootmap_bytes(end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 memset(bdata->node_bootmem_map, 0xff, mapsize);
113
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700114 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
115 bdata - bootmem_node_data, start, mapstart, end, mapsize);
116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 return mapsize;
118}
119
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700120/**
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 Weiner223e8dc2008-07-23 21:28:00 -0700129unsigned 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 Weinera66fd7d2008-07-23 21:28:01 -0700135/**
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 Weiner223e8dc2008-07-23 21:28:00 -0700142unsigned 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 Lu08677212010-02-10 01:20:20 -0800148#endif
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900149/*
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 */
158void __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 Lu08677212010-02-10 01:20:20 -0800173#ifdef CONFIG_NO_BOOTMEM
174static 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 Lu08677212010-02-10 01:20:20 -0800184 for (i = start; i < end; i++)
185 __free_pages_bootmem(pfn_to_page(i), 0);
186
187 return;
188 }
189
Yinghai Lu08677212010-02-10 01:20:20 -0800190 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
200unsigned 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 Weiner223e8dc2008-07-23 21:28:00 -0700220static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
221{
Johannes Weiner41546c12008-07-23 21:28:03 -0700222 int aligned;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700223 struct page *page;
Johannes Weiner41546c12008-07-23 21:28:03 -0700224 unsigned long start, end, pages, count = 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700225
Johannes Weiner41546c12008-07-23 21:28:03 -0700226 if (!bdata->node_bootmem_map)
227 return 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700228
Johannes Weiner3560e242008-07-23 21:28:09 -0700229 start = bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700230 end = bdata->node_low_pfn;
231
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700232 /*
Johannes Weiner41546c12008-07-23 21:28:03 -0700233 * If the start is aligned to the machines wordsize, we might
234 * be able to free pages in bulks of that order.
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700235 */
Johannes Weiner41546c12008-07-23 21:28:03 -0700236 aligned = !(start & (BITS_PER_LONG - 1));
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700237
Johannes Weiner41546c12008-07-23 21:28:03 -0700238 bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
239 bdata - bootmem_node_data, start, end, aligned);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700240
Johannes Weiner41546c12008-07-23 21:28:03 -0700241 while (start < end) {
242 unsigned long *map, idx, vec;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700243
Johannes Weiner41546c12008-07-23 21:28:03 -0700244 map = bdata->node_bootmem_map;
Johannes Weiner3560e242008-07-23 21:28:09 -0700245 idx = start - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700246 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 Weiner223e8dc2008-07-23 21:28:00 -0700252 count += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700253 } else {
Johannes Weiner41546c12008-07-23 21:28:03 -0700254 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 Weiner223e8dc2008-07-23 21:28:00 -0700265 }
Johannes Weiner41546c12008-07-23 21:28:03 -0700266 start += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700267 }
268
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700269 page = virt_to_page(bdata->node_bootmem_map);
Johannes Weiner3560e242008-07-23 21:28:09 -0700270 pages = bdata->node_low_pfn - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700271 pages = bootmem_bootmap_pages(pages);
272 count += pages;
273 while (pages--)
274 __free_pages_bootmem(page++, 0);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700275
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700276 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
277
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700278 return count;
279}
Yinghai Lu08677212010-02-10 01:20:20 -0800280#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700281
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700282/**
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 Weiner223e8dc2008-07-23 21:28:00 -0700288unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
289{
290 register_page_bootmem_info_node(pgdat);
Yinghai Lu08677212010-02-10 01:20:20 -0800291#ifdef CONFIG_NO_BOOTMEM
292 /* free_all_memory_core_early(MAX_NUMNODES) will be called later */
293 return 0;
294#else
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700295 return free_all_bootmem_core(pgdat->bdata);
Yinghai Lu08677212010-02-10 01:20:20 -0800296#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700297}
298
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700299/**
300 * free_all_bootmem - release free pages to the buddy allocator
301 *
302 * Returns the number of pages actually released.
303 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700304unsigned long __init free_all_bootmem(void)
305{
Yinghai Lu08677212010-02-10 01:20:20 -0800306#ifdef CONFIG_NO_BOOTMEM
Yinghai Lu33799852010-03-31 20:44:09 -0700307 /*
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 Lu08677212010-02-10 01:20:20 -0800315#else
Yinghai Luaa235fc2010-03-31 20:45:27 -0700316 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 Lu08677212010-02-10 01:20:20 -0800323#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700324}
325
Yinghai Lu08677212010-02-10 01:20:20 -0800326#ifndef CONFIG_NO_BOOTMEM
Johannes Weinerd747fa42008-07-23 21:28:05 -0700327static 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 Weiner3560e242008-07-23 21:28:09 -0700333 sidx + bdata->node_min_pfn,
334 eidx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700335
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700336 if (bdata->hint_idx > sidx)
337 bdata->hint_idx = sidx;
338
Johannes Weinerd747fa42008-07-23 21:28:05 -0700339 for (idx = sidx; idx < eidx; idx++)
340 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
341 BUG();
342}
343
344static 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 Weiner3560e242008-07-23 21:28:09 -0700352 sidx + bdata->node_min_pfn,
353 eidx + bdata->node_min_pfn,
Johannes Weinerd747fa42008-07-23 21:28:05 -0700354 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 Weiner3560e242008-07-23 21:28:09 -0700363 idx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700364 }
365 return 0;
366}
367
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700368static int __init mark_bootmem_node(bootmem_data_t *bdata,
369 unsigned long start, unsigned long end,
370 int reserve, int flags)
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700371{
372 unsigned long sidx, eidx;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700373
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700374 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
375 bdata - bootmem_node_data, start, end, reserve, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700376
Johannes Weiner3560e242008-07-23 21:28:09 -0700377 BUG_ON(start < bdata->node_min_pfn);
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700378 BUG_ON(end > bdata->node_low_pfn);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700379
Johannes Weiner3560e242008-07-23 21:28:09 -0700380 sidx = start - bdata->node_min_pfn;
381 eidx = end - bdata->node_min_pfn;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700382
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700383 if (reserve)
384 return __reserve(bdata, sidx, eidx, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700385 else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700386 __free(bdata, sidx, eidx);
387 return 0;
388}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700389
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700390static 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 Weiner223e8dc2008-07-23 21:28:00 -0700395
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700396 pos = start;
397 list_for_each_entry(bdata, &bdata_list, list) {
398 int err;
399 unsigned long max;
400
Johannes Weiner3560e242008-07-23 21:28:09 -0700401 if (pos < bdata->node_min_pfn ||
402 pos >= bdata->node_low_pfn) {
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700403 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 Weiner223e8dc2008-07-23 21:28:00 -0700420}
Yinghai Lu08677212010-02-10 01:20:20 -0800421#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700422
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700423/**
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 Weinere2bf3ca2008-07-23 21:28:06 -0700431 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700432 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700433void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
434 unsigned long size)
435{
Yinghai Lu08677212010-02-10 01:20:20 -0800436#ifdef CONFIG_NO_BOOTMEM
437 free_early(physaddr, physaddr + size);
Yinghai Lu08677212010-02-10 01:20:20 -0800438#else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700439 unsigned long start, end;
440
Catalin Marinasec3a3542009-07-07 10:33:01 +0100441 kmemleak_free_part(__va(physaddr), size);
442
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700443 start = PFN_UP(physaddr);
444 end = PFN_DOWN(physaddr + size);
445
446 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800447#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700448}
449
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700450/**
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 Weinere2bf3ca2008-07-23 21:28:06 -0700457 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700458 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700459void __init free_bootmem(unsigned long addr, unsigned long size)
460{
Yinghai Lu08677212010-02-10 01:20:20 -0800461#ifdef CONFIG_NO_BOOTMEM
462 free_early(addr, addr + size);
Yinghai Lu08677212010-02-10 01:20:20 -0800463#else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700464 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700465
Catalin Marinasec3a3542009-07-07 10:33:01 +0100466 kmemleak_free_part(__va(addr), size);
467
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700468 start = PFN_UP(addr);
469 end = PFN_DOWN(addr + size);
Yinghai Lua5645a62008-03-18 12:49:12 -0700470
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700471 mark_bootmem(start, end, 0, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800472#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473}
474
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700475/**
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 Weinere2bf3ca2008-07-23 21:28:06 -0700484 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700485 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700486int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
487 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Yinghai Lu08677212010-02-10 01:20:20 -0800489#ifdef CONFIG_NO_BOOTMEM
490 panic("no bootmem");
491 return 0;
492#else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700493 unsigned long start, end;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700494
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700495 start = PFN_DOWN(physaddr);
496 end = PFN_UP(physaddr + size);
497
498 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
Yinghai Lu08677212010-02-10 01:20:20 -0800499#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700502/**
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 Weinere2bf3ca2008-07-23 21:28:06 -0700510 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700511 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700512int __init reserve_bootmem(unsigned long addr, unsigned long size,
513 int flags)
514{
Yinghai Lu08677212010-02-10 01:20:20 -0800515#ifdef CONFIG_NO_BOOTMEM
516 panic("no bootmem");
517 return 0;
518#else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700519 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700520
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700521 start = PFN_DOWN(addr);
522 end = PFN_UP(addr + size);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700523
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700524 return mark_bootmem(start, end, 1, flags);
Yinghai Lu08677212010-02-10 01:20:20 -0800525#endif
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700526}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700527
Yinghai Lu08677212010-02-10 01:20:20 -0800528#ifndef CONFIG_NO_BOOTMEM
Jan Beulich8aa043d2009-12-14 17:59:46 -0800529static unsigned long __init align_idx(struct bootmem_data *bdata,
530 unsigned long idx, unsigned long step)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700531{
532 unsigned long base = bdata->node_min_pfn;
533
534 /*
535 * Align the index with respect to the node start so that the
536 * combination of both satisfies the requested alignment.
537 */
538
539 return ALIGN(base + idx, step) - base;
540}
541
Jan Beulich8aa043d2009-12-14 17:59:46 -0800542static unsigned long __init align_off(struct bootmem_data *bdata,
543 unsigned long off, unsigned long align)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700544{
545 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
546
547 /* Same as align_idx for byte offsets */
548
549 return ALIGN(base + off, align) - base;
550}
551
Tejun Heod0c4f572009-03-01 16:06:56 +0900552static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
553 unsigned long size, unsigned long align,
554 unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700556 unsigned long fallback = 0;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700557 unsigned long min, max, start, sidx, midx, step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Johannes Weiner594fe1a2009-01-06 14:40:32 -0800559 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
560 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
561 align, goal, limit);
562
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700563 BUG_ON(!size);
564 BUG_ON(align & (align - 1));
565 BUG_ON(limit && goal + size > limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566
Christian Krafft7c309a62006-12-06 20:32:41 -0800567 if (!bdata->node_bootmem_map)
568 return NULL;
569
Johannes Weiner3560e242008-07-23 21:28:09 -0700570 min = bdata->node_min_pfn;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700571 max = bdata->node_low_pfn;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700572
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700573 goal >>= PAGE_SHIFT;
574 limit >>= PAGE_SHIFT;
575
576 if (limit && max > limit)
577 max = limit;
578 if (max <= min)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700579 return NULL;
580
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700581 step = max(align >> PAGE_SHIFT, 1UL);
Yasunori Goto281dd252005-10-19 15:52:18 -0700582
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700583 if (goal && min < goal && goal < max)
584 start = ALIGN(goal, step);
585 else
586 start = ALIGN(min, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Johannes Weiner481ebd02008-08-20 14:09:15 -0700588 sidx = start - bdata->node_min_pfn;
Johannes Weiner3560e242008-07-23 21:28:09 -0700589 midx = max - bdata->node_min_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700591 if (bdata->hint_idx > sidx) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700592 /*
593 * Handle the valid case of sidx being zero and still
594 * catch the fallback below.
595 */
596 fallback = sidx + 1;
Johannes Weiner481ebd02008-08-20 14:09:15 -0700597 sidx = align_idx(bdata, bdata->hint_idx, step);
Yinghai Luad093152008-03-10 23:23:42 -0700598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700600 while (1) {
601 int merge;
602 void *region;
603 unsigned long eidx, i, start_off, end_off;
604find_block:
605 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
Johannes Weiner481ebd02008-08-20 14:09:15 -0700606 sidx = align_idx(bdata, sidx, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700607 eidx = sidx + PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700609 if (sidx >= midx || eidx > midx)
Haren Myneni66d43e92005-12-12 00:37:39 -0800610 break;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700611
612 for (i = sidx; i < eidx; i++)
613 if (test_bit(i, bdata->node_bootmem_map)) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700614 sidx = align_idx(bdata, i, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700615 if (sidx == i)
616 sidx += step;
617 goto find_block;
618 }
619
Mikulas Patocka627240a2008-08-15 00:40:17 -0700620 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700621 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700622 start_off = align_off(bdata, bdata->last_end_off, align);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700623 else
624 start_off = PFN_PHYS(sidx);
625
626 merge = PFN_DOWN(start_off) < sidx;
627 end_off = start_off + size;
628
629 bdata->last_end_off = end_off;
630 bdata->hint_idx = PFN_UP(end_off);
631
632 /*
633 * Reserve the area now:
634 */
Johannes Weinerd747fa42008-07-23 21:28:05 -0700635 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
636 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
637 BUG();
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700638
Johannes Weiner3560e242008-07-23 21:28:09 -0700639 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
640 start_off);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700641 memset(region, 0, size);
Catalin Marinas008139d2009-08-27 14:29:17 +0100642 /*
643 * The min_count is set to 0 so that bootmem allocated blocks
644 * are never reported as leaks.
645 */
646 kmemleak_alloc(region, size, 0, 0);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700647 return region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700650 if (fallback) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700651 sidx = align_idx(bdata, fallback - 1, step);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700652 fallback = 0;
653 goto find_block;
654 }
655
656 return NULL;
657}
658
Tejun Heod0c4f572009-03-01 16:06:56 +0900659static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
660 unsigned long size, unsigned long align,
661 unsigned long goal, unsigned long limit)
662{
Pekka Enberg441c7e02009-06-10 20:05:53 +0300663 if (WARN_ON_ONCE(slab_is_available()))
664 return kzalloc(size, GFP_NOWAIT);
665
Tejun Heod0c4f572009-03-01 16:06:56 +0900666#ifdef CONFIG_HAVE_ARCH_BOOTMEM
Joe Perches433f13a2009-06-18 16:48:58 -0700667 {
668 bootmem_data_t *p_bdata;
Tejun Heod0c4f572009-03-01 16:06:56 +0900669
Joe Perches433f13a2009-06-18 16:48:58 -0700670 p_bdata = bootmem_arch_preferred_node(bdata, size, align,
671 goal, limit);
672 if (p_bdata)
673 return alloc_bootmem_core(p_bdata, size, align,
674 goal, limit);
675 }
Tejun Heod0c4f572009-03-01 16:06:56 +0900676#endif
677 return NULL;
678}
Yinghai Lu08677212010-02-10 01:20:20 -0800679#endif
Tejun Heod0c4f572009-03-01 16:06:56 +0900680
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700681static void * __init ___alloc_bootmem_nopanic(unsigned long size,
682 unsigned long align,
683 unsigned long goal,
684 unsigned long limit)
685{
Yinghai Lu08677212010-02-10 01:20:20 -0800686#ifdef CONFIG_NO_BOOTMEM
687 void *ptr;
688
689 if (WARN_ON_ONCE(slab_is_available()))
690 return kzalloc(size, GFP_NOWAIT);
691
692restart:
693
694 ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
695
696 if (ptr)
697 return ptr;
698
699 if (goal != 0) {
700 goal = 0;
701 goto restart;
702 }
703
704 return NULL;
705#else
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700706 bootmem_data_t *bdata;
Tejun Heod0c4f572009-03-01 16:06:56 +0900707 void *region;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700708
709restart:
Tejun Heod0c4f572009-03-01 16:06:56 +0900710 region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
711 if (region)
712 return region;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700713
Tejun Heod0c4f572009-03-01 16:06:56 +0900714 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700715 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
716 continue;
Johannes Weiner3560e242008-07-23 21:28:09 -0700717 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700718 break;
719
720 region = alloc_bootmem_core(bdata, size, align, goal, limit);
721 if (region)
722 return region;
723 }
724
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700725 if (goal) {
726 goal = 0;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700727 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 }
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 return NULL;
Yinghai Lu08677212010-02-10 01:20:20 -0800731#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700734/**
735 * __alloc_bootmem_nopanic - allocate boot memory without panicking
736 * @size: size of the request in bytes
737 * @align: alignment of the region
738 * @goal: preferred starting address of the region
739 *
740 * The goal is dropped if it can not be satisfied and the allocation will
741 * fall back to memory below @goal.
742 *
743 * Allocation may happen on any node in the system.
744 *
745 * Returns NULL on failure.
746 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700747void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700748 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Yinghai Lu08677212010-02-10 01:20:20 -0800750 unsigned long limit = 0;
751
752#ifdef CONFIG_NO_BOOTMEM
753 limit = -1UL;
754#endif
755
756 return ___alloc_bootmem_nopanic(size, align, goal, limit);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700757}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700759static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
760 unsigned long goal, unsigned long limit)
761{
762 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
763
764 if (mem)
765 return mem;
766 /*
767 * Whoops, we cannot satisfy the allocation request.
768 */
769 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
770 panic("Out of memory");
Andi Kleena8062232006-04-07 19:49:21 +0200771 return NULL;
772}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700774/**
775 * __alloc_bootmem - allocate boot memory
776 * @size: size of the request in bytes
777 * @align: alignment of the region
778 * @goal: preferred starting address of the region
779 *
780 * The goal is dropped if it can not be satisfied and the allocation will
781 * fall back to memory below @goal.
782 *
783 * Allocation may happen on any node in the system.
784 *
785 * The function panics if the request can not be satisfied.
786 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700787void * __init __alloc_bootmem(unsigned long size, unsigned long align,
788 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200789{
Yinghai Lu08677212010-02-10 01:20:20 -0800790 unsigned long limit = 0;
791
792#ifdef CONFIG_NO_BOOTMEM
793 limit = -1UL;
794#endif
795
796 return ___alloc_bootmem(size, align, goal, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Yinghai Lu08677212010-02-10 01:20:20 -0800799#ifndef CONFIG_NO_BOOTMEM
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700800static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
801 unsigned long size, unsigned long align,
802 unsigned long goal, unsigned long limit)
803{
804 void *ptr;
805
Tejun Heod0c4f572009-03-01 16:06:56 +0900806 ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
807 if (ptr)
808 return ptr;
809
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700810 ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
811 if (ptr)
812 return ptr;
813
814 return ___alloc_bootmem(size, align, goal, limit);
815}
Yinghai Lu08677212010-02-10 01:20:20 -0800816#endif
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700817
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700818/**
819 * __alloc_bootmem_node - allocate boot memory from a specific node
820 * @pgdat: node to allocate from
821 * @size: size of the request in bytes
822 * @align: alignment of the region
823 * @goal: preferred starting address of the region
824 *
825 * The goal is dropped if it can not be satisfied and the allocation will
826 * fall back to memory below @goal.
827 *
828 * Allocation may fall back to any node in the system if the specified node
829 * can not hold the requested memory.
830 *
831 * The function panics if the request can not be satisfied.
832 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700833void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
834 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835{
Pekka Enbergc91c4772009-06-11 08:10:28 +0300836 if (WARN_ON_ONCE(slab_is_available()))
837 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
838
Yinghai Lu08677212010-02-10 01:20:20 -0800839#ifdef CONFIG_NO_BOOTMEM
840 return __alloc_memory_core_early(pgdat->node_id, size, align,
841 goal, -1ULL);
842#else
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700843 return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800844#endif
845}
846
847void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
848 unsigned long align, unsigned long goal)
849{
850#ifdef MAX_DMA32_PFN
851 unsigned long end_pfn;
852
853 if (WARN_ON_ONCE(slab_is_available()))
854 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
855
856 /* update goal according ...MAX_DMA32_PFN */
857 end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
858
859 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
860 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
861 void *ptr;
862 unsigned long new_goal;
863
864 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
865#ifdef CONFIG_NO_BOOTMEM
866 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
867 new_goal, -1ULL);
868#else
869 ptr = alloc_bootmem_core(pgdat->bdata, size, align,
870 new_goal, 0);
871#endif
872 if (ptr)
873 return ptr;
874 }
875#endif
876
877 return __alloc_bootmem_node(pgdat, size, align, goal);
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700881#ifdef CONFIG_SPARSEMEM
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700882/**
883 * alloc_bootmem_section - allocate boot memory from a specific section
884 * @size: size of the request in bytes
885 * @section_nr: sparse map section to allocate from
886 *
887 * Return NULL on failure.
888 */
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700889void * __init alloc_bootmem_section(unsigned long size,
890 unsigned long section_nr)
891{
Yinghai Lu08677212010-02-10 01:20:20 -0800892#ifdef CONFIG_NO_BOOTMEM
893 unsigned long pfn, goal, limit;
894
895 pfn = section_nr_to_pfn(section_nr);
896 goal = pfn << PAGE_SHIFT;
897 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
898
899 return __alloc_memory_core_early(early_pfn_to_nid(pfn), size,
900 SMP_CACHE_BYTES, goal, limit);
901#else
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700902 bootmem_data_t *bdata;
903 unsigned long pfn, goal, limit;
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700904
905 pfn = section_nr_to_pfn(section_nr);
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700906 goal = pfn << PAGE_SHIFT;
907 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
908 bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700909
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700910 return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
Yinghai Lu08677212010-02-10 01:20:20 -0800911#endif
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700912}
913#endif
914
Andi Kleenb54bbf72008-07-23 21:27:45 -0700915void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
916 unsigned long align, unsigned long goal)
917{
918 void *ptr;
919
Pekka Enbergc91c4772009-06-11 08:10:28 +0300920 if (WARN_ON_ONCE(slab_is_available()))
921 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
922
Yinghai Lu08677212010-02-10 01:20:20 -0800923#ifdef CONFIG_NO_BOOTMEM
924 ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
925 goal, -1ULL);
926#else
Tejun Heod0c4f572009-03-01 16:06:56 +0900927 ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
928 if (ptr)
929 return ptr;
930
Andi Kleenb54bbf72008-07-23 21:27:45 -0700931 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800932#endif
Andi Kleenb54bbf72008-07-23 21:27:45 -0700933 if (ptr)
934 return ptr;
935
936 return __alloc_bootmem_nopanic(size, align, goal);
937}
938
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700939#ifndef ARCH_LOW_ADDRESS_LIMIT
940#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
941#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800942
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700943/**
944 * __alloc_bootmem_low - allocate low boot memory
945 * @size: size of the request in bytes
946 * @align: alignment of the region
947 * @goal: preferred starting address of the region
948 *
949 * The goal is dropped if it can not be satisfied and the allocation will
950 * fall back to memory below @goal.
951 *
952 * Allocation may happen on any node in the system.
953 *
954 * The function panics if the request can not be satisfied.
955 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700956void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
957 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800958{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700959 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800960}
961
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700962/**
963 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
964 * @pgdat: node to allocate from
965 * @size: size of the request in bytes
966 * @align: alignment of the region
967 * @goal: preferred starting address of the region
968 *
969 * The goal is dropped if it can not be satisfied and the allocation will
970 * fall back to memory below @goal.
971 *
972 * Allocation may fall back to any node in the system if the specified node
973 * can not hold the requested memory.
974 *
975 * The function panics if the request can not be satisfied.
976 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800977void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
978 unsigned long align, unsigned long goal)
979{
Pekka Enbergc91c4772009-06-11 08:10:28 +0300980 if (WARN_ON_ONCE(slab_is_available()))
981 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
982
Yinghai Lu08677212010-02-10 01:20:20 -0800983#ifdef CONFIG_NO_BOOTMEM
984 return __alloc_memory_core_early(pgdat->node_id, size, align,
985 goal, ARCH_LOW_ADDRESS_LIMIT);
986#else
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700987 return ___alloc_bootmem_node(pgdat->bdata, size, align,
988 goal, ARCH_LOW_ADDRESS_LIMIT);
Yinghai Lu08677212010-02-10 01:20:20 -0800989#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800990}