blob: 4403e2fbc13ddfbe1d34741caf7379a3789636b4 [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>
Yinghai Lu72d7c3b2010-08-25 13:39:17 -070018#include <linux/memblock.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070019
20#include <asm/bug.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <asm/io.h>
Heiko Carstensdfd54cb2006-09-25 23:31:33 -070022#include <asm/processor.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "internal.h"
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026unsigned long max_low_pfn;
27unsigned long min_low_pfn;
28unsigned long max_pfn;
29
Vivek Goyal92aa63a2005-06-25 14:58:18 -070030#ifdef CONFIG_CRASH_DUMP
31/*
32 * If we have booted due to a crash, max_pfn will be a very low value. We need
33 * to know the amount of memory that the previous kernel used.
34 */
35unsigned long saved_max_pfn;
36#endif
37
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 Lu09325872011-02-24 14:43:05 +0100148
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
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700173static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
174{
Johannes Weiner41546c12008-07-23 21:28:03 -0700175 int aligned;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700176 struct page *page;
Johannes Weiner41546c12008-07-23 21:28:03 -0700177 unsigned long start, end, pages, count = 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700178
Johannes Weiner41546c12008-07-23 21:28:03 -0700179 if (!bdata->node_bootmem_map)
180 return 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700181
Johannes Weiner3560e242008-07-23 21:28:09 -0700182 start = bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700183 end = bdata->node_low_pfn;
184
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700185 /*
Johannes Weiner41546c12008-07-23 21:28:03 -0700186 * If the start is aligned to the machines wordsize, we might
187 * be able to free pages in bulks of that order.
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700188 */
Johannes Weiner41546c12008-07-23 21:28:03 -0700189 aligned = !(start & (BITS_PER_LONG - 1));
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700190
Johannes Weiner41546c12008-07-23 21:28:03 -0700191 bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
192 bdata - bootmem_node_data, start, end, aligned);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700193
Johannes Weiner41546c12008-07-23 21:28:03 -0700194 while (start < end) {
195 unsigned long *map, idx, vec;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700196
Johannes Weiner41546c12008-07-23 21:28:03 -0700197 map = bdata->node_bootmem_map;
Johannes Weiner3560e242008-07-23 21:28:09 -0700198 idx = start - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700199 vec = ~map[idx / BITS_PER_LONG];
200
201 if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
202 int order = ilog2(BITS_PER_LONG);
203
204 __free_pages_bootmem(pfn_to_page(start), order);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700205 count += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700206 } else {
Johannes Weiner41546c12008-07-23 21:28:03 -0700207 unsigned long off = 0;
208
209 while (vec && off < BITS_PER_LONG) {
210 if (vec & 1) {
211 page = pfn_to_page(start + off);
212 __free_pages_bootmem(page, 0);
213 count++;
214 }
215 vec >>= 1;
216 off++;
217 }
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700218 }
Johannes Weiner41546c12008-07-23 21:28:03 -0700219 start += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700220 }
221
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700222 page = virt_to_page(bdata->node_bootmem_map);
Johannes Weiner3560e242008-07-23 21:28:09 -0700223 pages = bdata->node_low_pfn - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700224 pages = bootmem_bootmap_pages(pages);
225 count += pages;
226 while (pages--)
227 __free_pages_bootmem(page++, 0);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700228
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700229 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
230
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700231 return count;
232}
233
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700234/**
235 * free_all_bootmem_node - release a node's free pages to the buddy allocator
236 * @pgdat: node to be released
237 *
238 * Returns the number of pages actually released.
239 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700240unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
241{
242 register_page_bootmem_info_node(pgdat);
243 return free_all_bootmem_core(pgdat->bdata);
244}
245
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700246/**
247 * free_all_bootmem - release free pages to the buddy allocator
248 *
249 * Returns the number of pages actually released.
250 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700251unsigned long __init free_all_bootmem(void)
252{
Yinghai Luaa235fc2010-03-31 20:45:27 -0700253 unsigned long total_pages = 0;
254 bootmem_data_t *bdata;
255
256 list_for_each_entry(bdata, &bdata_list, list)
257 total_pages += free_all_bootmem_core(bdata);
258
259 return total_pages;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700260}
261
Johannes Weinerd747fa42008-07-23 21:28:05 -0700262static void __init __free(bootmem_data_t *bdata,
263 unsigned long sidx, unsigned long eidx)
264{
265 unsigned long idx;
266
267 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700268 sidx + bdata->node_min_pfn,
269 eidx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700270
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700271 if (bdata->hint_idx > sidx)
272 bdata->hint_idx = sidx;
273
Johannes Weinerd747fa42008-07-23 21:28:05 -0700274 for (idx = sidx; idx < eidx; idx++)
275 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
276 BUG();
277}
278
279static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
280 unsigned long eidx, int flags)
281{
282 unsigned long idx;
283 int exclusive = flags & BOOTMEM_EXCLUSIVE;
284
285 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
286 bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700287 sidx + bdata->node_min_pfn,
288 eidx + bdata->node_min_pfn,
Johannes Weinerd747fa42008-07-23 21:28:05 -0700289 flags);
290
291 for (idx = sidx; idx < eidx; idx++)
292 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
293 if (exclusive) {
294 __free(bdata, sidx, idx);
295 return -EBUSY;
296 }
297 bdebug("silent double reserve of PFN %lx\n",
Johannes Weiner3560e242008-07-23 21:28:09 -0700298 idx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700299 }
300 return 0;
301}
302
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700303static int __init mark_bootmem_node(bootmem_data_t *bdata,
304 unsigned long start, unsigned long end,
305 int reserve, int flags)
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700306{
307 unsigned long sidx, eidx;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700308
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700309 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
310 bdata - bootmem_node_data, start, end, reserve, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700311
Johannes Weiner3560e242008-07-23 21:28:09 -0700312 BUG_ON(start < bdata->node_min_pfn);
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700313 BUG_ON(end > bdata->node_low_pfn);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700314
Johannes Weiner3560e242008-07-23 21:28:09 -0700315 sidx = start - bdata->node_min_pfn;
316 eidx = end - bdata->node_min_pfn;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700317
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700318 if (reserve)
319 return __reserve(bdata, sidx, eidx, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700320 else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700321 __free(bdata, sidx, eidx);
322 return 0;
323}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700324
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700325static int __init mark_bootmem(unsigned long start, unsigned long end,
326 int reserve, int flags)
327{
328 unsigned long pos;
329 bootmem_data_t *bdata;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700330
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700331 pos = start;
332 list_for_each_entry(bdata, &bdata_list, list) {
333 int err;
334 unsigned long max;
335
Johannes Weiner3560e242008-07-23 21:28:09 -0700336 if (pos < bdata->node_min_pfn ||
337 pos >= bdata->node_low_pfn) {
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700338 BUG_ON(pos != start);
339 continue;
340 }
341
342 max = min(bdata->node_low_pfn, end);
343
344 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
345 if (reserve && err) {
346 mark_bootmem(start, pos, 0, 0);
347 return err;
348 }
349
350 if (max == end)
351 return 0;
352 pos = bdata->node_low_pfn;
353 }
354 BUG();
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700355}
356
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700357/**
358 * free_bootmem_node - mark a page range as usable
359 * @pgdat: node the range resides on
360 * @physaddr: starting address of the range
361 * @size: size of the range in bytes
362 *
363 * Partial pages will be considered reserved and left as they are.
364 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700365 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700366 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700367void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
368 unsigned long size)
369{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700370 unsigned long start, end;
371
Catalin Marinasec3a3542009-07-07 10:33:01 +0100372 kmemleak_free_part(__va(physaddr), size);
373
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700374 start = PFN_UP(physaddr);
375 end = PFN_DOWN(physaddr + size);
376
377 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700378}
379
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700380/**
381 * free_bootmem - mark a page range as usable
382 * @addr: starting address of the range
383 * @size: size of the range in bytes
384 *
385 * Partial pages will be considered reserved and left as they are.
386 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700387 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700388 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700389void __init free_bootmem(unsigned long addr, unsigned long size)
390{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700391 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700392
Catalin Marinasec3a3542009-07-07 10:33:01 +0100393 kmemleak_free_part(__va(addr), size);
394
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700395 start = PFN_UP(addr);
396 end = PFN_DOWN(addr + size);
Yinghai Lua5645a62008-03-18 12:49:12 -0700397
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700398 mark_bootmem(start, end, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
400
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700401/**
402 * reserve_bootmem_node - mark a page range as reserved
403 * @pgdat: node the range resides on
404 * @physaddr: starting address of the range
405 * @size: size of the range in bytes
406 * @flags: reservation flags (see linux/bootmem.h)
407 *
408 * Partial pages will be reserved.
409 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700410 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700411 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700412int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
413 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700415 unsigned long start, end;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700416
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700417 start = PFN_DOWN(physaddr);
418 end = PFN_UP(physaddr + size);
419
420 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421}
422
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700423/**
424 * reserve_bootmem - mark a page range as usable
425 * @addr: starting address of the range
426 * @size: size of the range in bytes
427 * @flags: reservation flags (see linux/bootmem.h)
428 *
429 * Partial pages will be reserved.
430 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700431 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700432 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700433int __init reserve_bootmem(unsigned long addr, unsigned long size,
434 int flags)
435{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700436 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700437
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700438 start = PFN_DOWN(addr);
439 end = PFN_UP(addr + size);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700440
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700441 return mark_bootmem(start, end, 1, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700442}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700443
Yinghai Luf88eff72010-08-25 13:39:15 -0700444int __weak __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
445 int flags)
446{
447 return reserve_bootmem(phys, len, flags);
448}
449
Jan Beulich8aa043d2009-12-14 17:59:46 -0800450static unsigned long __init align_idx(struct bootmem_data *bdata,
451 unsigned long idx, unsigned long step)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700452{
453 unsigned long base = bdata->node_min_pfn;
454
455 /*
456 * Align the index with respect to the node start so that the
457 * combination of both satisfies the requested alignment.
458 */
459
460 return ALIGN(base + idx, step) - base;
461}
462
Jan Beulich8aa043d2009-12-14 17:59:46 -0800463static unsigned long __init align_off(struct bootmem_data *bdata,
464 unsigned long off, unsigned long align)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700465{
466 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
467
468 /* Same as align_idx for byte offsets */
469
470 return ALIGN(base + off, align) - base;
471}
472
Tejun Heod0c4f572009-03-01 16:06:56 +0900473static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
474 unsigned long size, unsigned long align,
475 unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700477 unsigned long fallback = 0;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700478 unsigned long min, max, start, sidx, midx, step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Johannes Weiner594fe1a2009-01-06 14:40:32 -0800480 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
481 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
482 align, goal, limit);
483
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700484 BUG_ON(!size);
485 BUG_ON(align & (align - 1));
486 BUG_ON(limit && goal + size > limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Christian Krafft7c309a62006-12-06 20:32:41 -0800488 if (!bdata->node_bootmem_map)
489 return NULL;
490
Johannes Weiner3560e242008-07-23 21:28:09 -0700491 min = bdata->node_min_pfn;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700492 max = bdata->node_low_pfn;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700493
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700494 goal >>= PAGE_SHIFT;
495 limit >>= PAGE_SHIFT;
496
497 if (limit && max > limit)
498 max = limit;
499 if (max <= min)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700500 return NULL;
501
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700502 step = max(align >> PAGE_SHIFT, 1UL);
Yasunori Goto281dd252005-10-19 15:52:18 -0700503
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700504 if (goal && min < goal && goal < max)
505 start = ALIGN(goal, step);
506 else
507 start = ALIGN(min, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508
Johannes Weiner481ebd02008-08-20 14:09:15 -0700509 sidx = start - bdata->node_min_pfn;
Johannes Weiner3560e242008-07-23 21:28:09 -0700510 midx = max - bdata->node_min_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700512 if (bdata->hint_idx > sidx) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700513 /*
514 * Handle the valid case of sidx being zero and still
515 * catch the fallback below.
516 */
517 fallback = sidx + 1;
Johannes Weiner481ebd02008-08-20 14:09:15 -0700518 sidx = align_idx(bdata, bdata->hint_idx, step);
Yinghai Luad093152008-03-10 23:23:42 -0700519 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700521 while (1) {
522 int merge;
523 void *region;
524 unsigned long eidx, i, start_off, end_off;
525find_block:
526 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
Johannes Weiner481ebd02008-08-20 14:09:15 -0700527 sidx = align_idx(bdata, sidx, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700528 eidx = sidx + PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700530 if (sidx >= midx || eidx > midx)
Haren Myneni66d43e92005-12-12 00:37:39 -0800531 break;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700532
533 for (i = sidx; i < eidx; i++)
534 if (test_bit(i, bdata->node_bootmem_map)) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700535 sidx = align_idx(bdata, i, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700536 if (sidx == i)
537 sidx += step;
538 goto find_block;
539 }
540
Mikulas Patocka627240a2008-08-15 00:40:17 -0700541 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700542 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700543 start_off = align_off(bdata, bdata->last_end_off, align);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700544 else
545 start_off = PFN_PHYS(sidx);
546
547 merge = PFN_DOWN(start_off) < sidx;
548 end_off = start_off + size;
549
550 bdata->last_end_off = end_off;
551 bdata->hint_idx = PFN_UP(end_off);
552
553 /*
554 * Reserve the area now:
555 */
Johannes Weinerd747fa42008-07-23 21:28:05 -0700556 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
557 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
558 BUG();
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700559
Johannes Weiner3560e242008-07-23 21:28:09 -0700560 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
561 start_off);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700562 memset(region, 0, size);
Catalin Marinas008139d2009-08-27 14:29:17 +0100563 /*
564 * The min_count is set to 0 so that bootmem allocated blocks
565 * are never reported as leaks.
566 */
567 kmemleak_alloc(region, size, 0, 0);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700568 return region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 }
570
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700571 if (fallback) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700572 sidx = align_idx(bdata, fallback - 1, step);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700573 fallback = 0;
574 goto find_block;
575 }
576
577 return NULL;
578}
579
Tejun Heod0c4f572009-03-01 16:06:56 +0900580static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
581 unsigned long size, unsigned long align,
582 unsigned long goal, unsigned long limit)
583{
Pekka Enberg441c7e02009-06-10 20:05:53 +0300584 if (WARN_ON_ONCE(slab_is_available()))
585 return kzalloc(size, GFP_NOWAIT);
586
Tejun Heod0c4f572009-03-01 16:06:56 +0900587#ifdef CONFIG_HAVE_ARCH_BOOTMEM
Joe Perches433f13a2009-06-18 16:48:58 -0700588 {
589 bootmem_data_t *p_bdata;
Tejun Heod0c4f572009-03-01 16:06:56 +0900590
Joe Perches433f13a2009-06-18 16:48:58 -0700591 p_bdata = bootmem_arch_preferred_node(bdata, size, align,
592 goal, limit);
593 if (p_bdata)
594 return alloc_bootmem_core(p_bdata, size, align,
595 goal, limit);
596 }
Tejun Heod0c4f572009-03-01 16:06:56 +0900597#endif
598 return NULL;
599}
600
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700601static void * __init ___alloc_bootmem_nopanic(unsigned long size,
602 unsigned long align,
603 unsigned long goal,
604 unsigned long limit)
605{
606 bootmem_data_t *bdata;
Tejun Heod0c4f572009-03-01 16:06:56 +0900607 void *region;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700608
609restart:
Tejun Heod0c4f572009-03-01 16:06:56 +0900610 region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
611 if (region)
612 return region;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700613
Tejun Heod0c4f572009-03-01 16:06:56 +0900614 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700615 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
616 continue;
Johannes Weiner3560e242008-07-23 21:28:09 -0700617 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700618 break;
619
620 region = alloc_bootmem_core(bdata, size, align, goal, limit);
621 if (region)
622 return region;
623 }
624
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700625 if (goal) {
626 goal = 0;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700627 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 }
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631}
632
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700633/**
634 * __alloc_bootmem_nopanic - allocate boot memory without panicking
635 * @size: size of the request in bytes
636 * @align: alignment of the region
637 * @goal: preferred starting address of the region
638 *
639 * The goal is dropped if it can not be satisfied and the allocation will
640 * fall back to memory below @goal.
641 *
642 * Allocation may happen on any node in the system.
643 *
644 * Returns NULL on failure.
645 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700646void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700647 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Yinghai Lu08677212010-02-10 01:20:20 -0800649 unsigned long limit = 0;
650
Yinghai Lu08677212010-02-10 01:20:20 -0800651 return ___alloc_bootmem_nopanic(size, align, goal, limit);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700652}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700654static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
655 unsigned long goal, unsigned long limit)
656{
657 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
658
659 if (mem)
660 return mem;
661 /*
662 * Whoops, we cannot satisfy the allocation request.
663 */
664 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
665 panic("Out of memory");
Andi Kleena8062232006-04-07 19:49:21 +0200666 return NULL;
667}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700669/**
670 * __alloc_bootmem - allocate boot memory
671 * @size: size of the request in bytes
672 * @align: alignment of the region
673 * @goal: preferred starting address of the region
674 *
675 * The goal is dropped if it can not be satisfied and the allocation will
676 * fall back to memory below @goal.
677 *
678 * Allocation may happen on any node in the system.
679 *
680 * The function panics if the request can not be satisfied.
681 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700682void * __init __alloc_bootmem(unsigned long size, unsigned long align,
683 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200684{
Yinghai Lu08677212010-02-10 01:20:20 -0800685 unsigned long limit = 0;
686
Yinghai Lu08677212010-02-10 01:20:20 -0800687 return ___alloc_bootmem(size, align, goal, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688}
689
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700690static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
691 unsigned long size, unsigned long align,
692 unsigned long goal, unsigned long limit)
693{
694 void *ptr;
695
Tejun Heod0c4f572009-03-01 16:06:56 +0900696 ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
697 if (ptr)
698 return ptr;
699
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700700 ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
701 if (ptr)
702 return ptr;
703
704 return ___alloc_bootmem(size, align, goal, limit);
705}
706
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700707/**
708 * __alloc_bootmem_node - allocate boot memory from a specific node
709 * @pgdat: node to allocate from
710 * @size: size of the request in bytes
711 * @align: alignment of the region
712 * @goal: preferred starting address of the region
713 *
714 * The goal is dropped if it can not be satisfied and the allocation will
715 * fall back to memory below @goal.
716 *
717 * Allocation may fall back to any node in the system if the specified node
718 * can not hold the requested memory.
719 *
720 * The function panics if the request can not be satisfied.
721 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700722void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
723 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724{
Pekka Enbergc91c4772009-06-11 08:10:28 +0300725 if (WARN_ON_ONCE(slab_is_available()))
726 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
727
Yinghai Lu09325872011-02-24 14:43:05 +0100728 return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800729}
730
731void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
732 unsigned long align, unsigned long goal)
733{
734#ifdef MAX_DMA32_PFN
735 unsigned long end_pfn;
736
737 if (WARN_ON_ONCE(slab_is_available()))
738 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
739
740 /* update goal according ...MAX_DMA32_PFN */
741 end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
742
743 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
744 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
745 void *ptr;
746 unsigned long new_goal;
747
748 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
Yinghai Lu08677212010-02-10 01:20:20 -0800749 ptr = alloc_bootmem_core(pgdat->bdata, size, align,
750 new_goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800751 if (ptr)
752 return ptr;
753 }
754#endif
755
756 return __alloc_bootmem_node(pgdat, size, align, goal);
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758}
759
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700760#ifdef CONFIG_SPARSEMEM
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700761/**
762 * alloc_bootmem_section - allocate boot memory from a specific section
763 * @size: size of the request in bytes
764 * @section_nr: sparse map section to allocate from
765 *
766 * Return NULL on failure.
767 */
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700768void * __init alloc_bootmem_section(unsigned long size,
769 unsigned long section_nr)
770{
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700771 bootmem_data_t *bdata;
772 unsigned long pfn, goal, limit;
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700773
774 pfn = section_nr_to_pfn(section_nr);
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700775 goal = pfn << PAGE_SHIFT;
776 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
777 bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700778
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700779 return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700780}
781#endif
782
Andi Kleenb54bbf72008-07-23 21:27:45 -0700783void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
784 unsigned long align, unsigned long goal)
785{
786 void *ptr;
787
Pekka Enbergc91c4772009-06-11 08:10:28 +0300788 if (WARN_ON_ONCE(slab_is_available()))
789 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
790
Tejun Heod0c4f572009-03-01 16:06:56 +0900791 ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
792 if (ptr)
793 return ptr;
794
Andi Kleenb54bbf72008-07-23 21:27:45 -0700795 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
796 if (ptr)
797 return ptr;
798
799 return __alloc_bootmem_nopanic(size, align, goal);
800}
801
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700802#ifndef ARCH_LOW_ADDRESS_LIMIT
803#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
804#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800805
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700806/**
807 * __alloc_bootmem_low - allocate low boot memory
808 * @size: size of the request in bytes
809 * @align: alignment of the region
810 * @goal: preferred starting address of the region
811 *
812 * The goal is dropped if it can not be satisfied and the allocation will
813 * fall back to memory below @goal.
814 *
815 * Allocation may happen on any node in the system.
816 *
817 * The function panics if the request can not be satisfied.
818 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700819void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
820 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800821{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700822 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800823}
824
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700825/**
826 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
827 * @pgdat: node to allocate from
828 * @size: size of the request in bytes
829 * @align: alignment of the region
830 * @goal: preferred starting address of the region
831 *
832 * The goal is dropped if it can not be satisfied and the allocation will
833 * fall back to memory below @goal.
834 *
835 * Allocation may fall back to any node in the system if the specified node
836 * can not hold the requested memory.
837 *
838 * The function panics if the request can not be satisfied.
839 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800840void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
841 unsigned long align, unsigned long goal)
842{
Pekka Enbergc91c4772009-06-11 08:10:28 +0300843 if (WARN_ON_ONCE(slab_is_available()))
844 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
845
Yinghai Lu09325872011-02-24 14:43:05 +0100846 return ___alloc_bootmem_node(pgdat->bdata, size, align,
Yinghai Lub8ab9f82010-07-20 13:24:31 -0700847 goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800848}