blob: 07aeb89e396ea140dbd7a28640aacb8fa2707b9a [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
Yinghai Lue782ab42011-02-24 14:43:06 +010026#ifndef CONFIG_NEED_MULTIPLE_NODES
27struct pglist_data __refdata contig_page_data = {
28 .bdata = &bootmem_node_data[0]
29};
30EXPORT_SYMBOL(contig_page_data);
31#endif
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033unsigned long max_low_pfn;
34unsigned long min_low_pfn;
35unsigned long max_pfn;
36
Vivek Goyal92aa63a2005-06-25 14:58:18 -070037#ifdef CONFIG_CRASH_DUMP
38/*
39 * If we have booted due to a crash, max_pfn will be a very low value. We need
40 * to know the amount of memory that the previous kernel used.
41 */
42unsigned long saved_max_pfn;
43#endif
44
Johannes Weinerb61bfa32008-07-23 21:26:55 -070045bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
46
Johannes Weiner636cc402008-07-23 21:28:03 -070047static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
48
Johannes Weiner2e5237d2008-07-23 21:28:02 -070049static int bootmem_debug;
50
51static int __init bootmem_debug_setup(char *buf)
52{
53 bootmem_debug = 1;
54 return 0;
55}
56early_param("bootmem_debug", bootmem_debug_setup);
57
58#define bdebug(fmt, args...) ({ \
59 if (unlikely(bootmem_debug)) \
60 printk(KERN_INFO \
61 "bootmem::%s " fmt, \
Harvey Harrison80a914d2008-10-15 22:01:25 -070062 __func__, ## args); \
Johannes Weiner2e5237d2008-07-23 21:28:02 -070063})
64
Johannes Weinerdf049a52008-07-23 21:28:02 -070065static unsigned long __init bootmap_bytes(unsigned long pages)
Johannes Weiner223e8dc2008-07-23 21:28:00 -070066{
Johannes Weinerdf049a52008-07-23 21:28:02 -070067 unsigned long bytes = (pages + 7) / 8;
Johannes Weiner223e8dc2008-07-23 21:28:00 -070068
Johannes Weinerdf049a52008-07-23 21:28:02 -070069 return ALIGN(bytes, sizeof(long));
Johannes Weiner223e8dc2008-07-23 21:28:00 -070070}
71
Johannes Weinera66fd7d2008-07-23 21:28:01 -070072/**
73 * bootmem_bootmap_pages - calculate bitmap size in pages
74 * @pages: number of pages the bitmap has to represent
75 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070076unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Johannes Weinerdf049a52008-07-23 21:28:02 -070078 unsigned long bytes = bootmap_bytes(pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Johannes Weinerdf049a52008-07-23 21:28:02 -070080 return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070082
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080083/*
84 * link bdata in order
85 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070086static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080087{
Johannes Weiner636cc402008-07-23 21:28:03 -070088 struct list_head *iter;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070089
Johannes Weiner636cc402008-07-23 21:28:03 -070090 list_for_each(iter, &bdata_list) {
91 bootmem_data_t *ent;
92
93 ent = list_entry(iter, bootmem_data_t, list);
Johannes Weiner3560e242008-07-23 21:28:09 -070094 if (bdata->node_min_pfn < ent->node_min_pfn)
Johannes Weiner636cc402008-07-23 21:28:03 -070095 break;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080096 }
Johannes Weiner636cc402008-07-23 21:28:03 -070097 list_add_tail(&bdata->list, iter);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080098}
99
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700100/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 * Called once to set up the allocator itself.
102 */
Johannes Weiner8ae04462008-07-23 21:26:59 -0700103static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 unsigned long mapstart, unsigned long start, unsigned long end)
105{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700106 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Mel Gorman2dbb51c2008-07-23 21:26:52 -0700108 mminit_validate_memmodel_limits(&start, &end);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700109 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
Johannes Weiner3560e242008-07-23 21:28:09 -0700110 bdata->node_min_pfn = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800112 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113
114 /*
115 * Initially all pages are reserved - setup_arch() has to
116 * register free RAM areas explicitly.
117 */
Johannes Weinerdf049a52008-07-23 21:28:02 -0700118 mapsize = bootmap_bytes(end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 memset(bdata->node_bootmem_map, 0xff, mapsize);
120
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700121 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
122 bdata - bootmem_node_data, start, mapstart, end, mapsize);
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return mapsize;
125}
126
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700127/**
128 * init_bootmem_node - register a node as boot memory
129 * @pgdat: node to register
130 * @freepfn: pfn where the bitmap for this node is to be placed
131 * @startpfn: first pfn on the node
132 * @endpfn: first pfn after the node
133 *
134 * Returns the number of bytes needed to hold the bitmap for this node.
135 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700136unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
137 unsigned long startpfn, unsigned long endpfn)
138{
139 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
140}
141
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700142/**
143 * init_bootmem - register boot memory
144 * @start: pfn where the bitmap is to be placed
145 * @pages: number of available physical pages
146 *
147 * Returns the number of bytes needed to hold the bitmap.
148 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700149unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
150{
151 max_low_pfn = pages;
152 min_low_pfn = start;
153 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
154}
Yinghai Lu09325872011-02-24 14:43:05 +0100155
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900156/*
157 * free_bootmem_late - free bootmem pages directly to page allocator
158 * @addr: starting address of the range
159 * @size: size of the range in bytes
160 *
161 * This is only useful when the bootmem allocator has already been torn
162 * down, but we are still initializing the system. Pages are given directly
163 * to the page allocator, no bootmem metadata is updated because it is gone.
164 */
165void __init free_bootmem_late(unsigned long addr, unsigned long size)
166{
167 unsigned long cursor, end;
168
169 kmemleak_free_part(__va(addr), size);
170
171 cursor = PFN_UP(addr);
172 end = PFN_DOWN(addr + size);
173
174 for (; cursor < end; cursor++) {
175 __free_pages_bootmem(pfn_to_page(cursor), 0);
176 totalram_pages++;
177 }
178}
179
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700180static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
181{
Johannes Weiner41546c12008-07-23 21:28:03 -0700182 int aligned;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700183 struct page *page;
Johannes Weiner41546c12008-07-23 21:28:03 -0700184 unsigned long start, end, pages, count = 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700185
Johannes Weiner41546c12008-07-23 21:28:03 -0700186 if (!bdata->node_bootmem_map)
187 return 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700188
Johannes Weiner3560e242008-07-23 21:28:09 -0700189 start = bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700190 end = bdata->node_low_pfn;
191
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700192 /*
Johannes Weiner41546c12008-07-23 21:28:03 -0700193 * If the start is aligned to the machines wordsize, we might
194 * be able to free pages in bulks of that order.
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700195 */
Johannes Weiner41546c12008-07-23 21:28:03 -0700196 aligned = !(start & (BITS_PER_LONG - 1));
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700197
Johannes Weiner41546c12008-07-23 21:28:03 -0700198 bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
199 bdata - bootmem_node_data, start, end, aligned);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700200
Johannes Weiner41546c12008-07-23 21:28:03 -0700201 while (start < end) {
202 unsigned long *map, idx, vec;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700203
Johannes Weiner41546c12008-07-23 21:28:03 -0700204 map = bdata->node_bootmem_map;
Johannes Weiner3560e242008-07-23 21:28:09 -0700205 idx = start - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700206 vec = ~map[idx / BITS_PER_LONG];
207
208 if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
209 int order = ilog2(BITS_PER_LONG);
210
211 __free_pages_bootmem(pfn_to_page(start), order);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700212 count += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700213 } else {
Johannes Weiner41546c12008-07-23 21:28:03 -0700214 unsigned long off = 0;
215
216 while (vec && off < BITS_PER_LONG) {
217 if (vec & 1) {
218 page = pfn_to_page(start + off);
219 __free_pages_bootmem(page, 0);
220 count++;
221 }
222 vec >>= 1;
223 off++;
224 }
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700225 }
Johannes Weiner41546c12008-07-23 21:28:03 -0700226 start += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700227 }
228
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700229 page = virt_to_page(bdata->node_bootmem_map);
Johannes Weiner3560e242008-07-23 21:28:09 -0700230 pages = bdata->node_low_pfn - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700231 pages = bootmem_bootmap_pages(pages);
232 count += pages;
233 while (pages--)
234 __free_pages_bootmem(page++, 0);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700235
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700236 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
237
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700238 return count;
239}
240
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700241/**
242 * free_all_bootmem_node - release a node's free pages to the buddy allocator
243 * @pgdat: node to be released
244 *
245 * Returns the number of pages actually released.
246 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700247unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
248{
249 register_page_bootmem_info_node(pgdat);
250 return free_all_bootmem_core(pgdat->bdata);
251}
252
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700253/**
254 * free_all_bootmem - release free pages to the buddy allocator
255 *
256 * Returns the number of pages actually released.
257 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700258unsigned long __init free_all_bootmem(void)
259{
Yinghai Luaa235fc2010-03-31 20:45:27 -0700260 unsigned long total_pages = 0;
261 bootmem_data_t *bdata;
262
263 list_for_each_entry(bdata, &bdata_list, list)
264 total_pages += free_all_bootmem_core(bdata);
265
266 return total_pages;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700267}
268
Johannes Weinerd747fa42008-07-23 21:28:05 -0700269static void __init __free(bootmem_data_t *bdata,
270 unsigned long sidx, unsigned long eidx)
271{
272 unsigned long idx;
273
274 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700275 sidx + bdata->node_min_pfn,
276 eidx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700277
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700278 if (bdata->hint_idx > sidx)
279 bdata->hint_idx = sidx;
280
Johannes Weinerd747fa42008-07-23 21:28:05 -0700281 for (idx = sidx; idx < eidx; idx++)
282 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
283 BUG();
284}
285
286static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
287 unsigned long eidx, int flags)
288{
289 unsigned long idx;
290 int exclusive = flags & BOOTMEM_EXCLUSIVE;
291
292 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
293 bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700294 sidx + bdata->node_min_pfn,
295 eidx + bdata->node_min_pfn,
Johannes Weinerd747fa42008-07-23 21:28:05 -0700296 flags);
297
298 for (idx = sidx; idx < eidx; idx++)
299 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
300 if (exclusive) {
301 __free(bdata, sidx, idx);
302 return -EBUSY;
303 }
304 bdebug("silent double reserve of PFN %lx\n",
Johannes Weiner3560e242008-07-23 21:28:09 -0700305 idx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700306 }
307 return 0;
308}
309
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700310static int __init mark_bootmem_node(bootmem_data_t *bdata,
311 unsigned long start, unsigned long end,
312 int reserve, int flags)
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700313{
314 unsigned long sidx, eidx;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700315
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700316 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
317 bdata - bootmem_node_data, start, end, reserve, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700318
Johannes Weiner3560e242008-07-23 21:28:09 -0700319 BUG_ON(start < bdata->node_min_pfn);
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700320 BUG_ON(end > bdata->node_low_pfn);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700321
Johannes Weiner3560e242008-07-23 21:28:09 -0700322 sidx = start - bdata->node_min_pfn;
323 eidx = end - bdata->node_min_pfn;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700324
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700325 if (reserve)
326 return __reserve(bdata, sidx, eidx, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700327 else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700328 __free(bdata, sidx, eidx);
329 return 0;
330}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700331
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700332static int __init mark_bootmem(unsigned long start, unsigned long end,
333 int reserve, int flags)
334{
335 unsigned long pos;
336 bootmem_data_t *bdata;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700337
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700338 pos = start;
339 list_for_each_entry(bdata, &bdata_list, list) {
340 int err;
341 unsigned long max;
342
Johannes Weiner3560e242008-07-23 21:28:09 -0700343 if (pos < bdata->node_min_pfn ||
344 pos >= bdata->node_low_pfn) {
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700345 BUG_ON(pos != start);
346 continue;
347 }
348
349 max = min(bdata->node_low_pfn, end);
350
351 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
352 if (reserve && err) {
353 mark_bootmem(start, pos, 0, 0);
354 return err;
355 }
356
357 if (max == end)
358 return 0;
359 pos = bdata->node_low_pfn;
360 }
361 BUG();
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700362}
363
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700364/**
365 * free_bootmem_node - mark a page range as usable
366 * @pgdat: node the range resides on
367 * @physaddr: starting address of the range
368 * @size: size of the range in bytes
369 *
370 * Partial pages will be considered reserved and left as they are.
371 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700372 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700373 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700374void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
375 unsigned long size)
376{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700377 unsigned long start, end;
378
Catalin Marinasec3a3542009-07-07 10:33:01 +0100379 kmemleak_free_part(__va(physaddr), size);
380
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700381 start = PFN_UP(physaddr);
382 end = PFN_DOWN(physaddr + size);
383
384 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700385}
386
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700387/**
388 * free_bootmem - mark a page range as usable
389 * @addr: starting address of the range
390 * @size: size of the range in bytes
391 *
392 * Partial pages will be considered reserved and left as they are.
393 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700394 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700395 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700396void __init free_bootmem(unsigned long addr, unsigned long size)
397{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700398 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700399
Catalin Marinasec3a3542009-07-07 10:33:01 +0100400 kmemleak_free_part(__va(addr), size);
401
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700402 start = PFN_UP(addr);
403 end = PFN_DOWN(addr + size);
Yinghai Lua5645a62008-03-18 12:49:12 -0700404
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700405 mark_bootmem(start, end, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406}
407
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700408/**
409 * reserve_bootmem_node - mark a page range as reserved
410 * @pgdat: node the range resides on
411 * @physaddr: starting address of the range
412 * @size: size of the range in bytes
413 * @flags: reservation flags (see linux/bootmem.h)
414 *
415 * Partial pages will be reserved.
416 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700417 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700418 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700419int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
420 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700422 unsigned long start, end;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700423
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700424 start = PFN_DOWN(physaddr);
425 end = PFN_UP(physaddr + size);
426
427 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700430/**
431 * reserve_bootmem - mark a page range as usable
432 * @addr: starting address of the range
433 * @size: size of the range in bytes
434 * @flags: reservation flags (see linux/bootmem.h)
435 *
436 * Partial pages will be reserved.
437 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700438 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700439 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700440int __init reserve_bootmem(unsigned long addr, unsigned long size,
441 int flags)
442{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700443 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700444
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700445 start = PFN_DOWN(addr);
446 end = PFN_UP(addr + size);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700447
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700448 return mark_bootmem(start, end, 1, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700449}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700450
Yinghai Luf88eff72010-08-25 13:39:15 -0700451int __weak __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
452 int flags)
453{
454 return reserve_bootmem(phys, len, flags);
455}
456
Jan Beulich8aa043d2009-12-14 17:59:46 -0800457static unsigned long __init align_idx(struct bootmem_data *bdata,
458 unsigned long idx, unsigned long step)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700459{
460 unsigned long base = bdata->node_min_pfn;
461
462 /*
463 * Align the index with respect to the node start so that the
464 * combination of both satisfies the requested alignment.
465 */
466
467 return ALIGN(base + idx, step) - base;
468}
469
Jan Beulich8aa043d2009-12-14 17:59:46 -0800470static unsigned long __init align_off(struct bootmem_data *bdata,
471 unsigned long off, unsigned long align)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700472{
473 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
474
475 /* Same as align_idx for byte offsets */
476
477 return ALIGN(base + off, align) - base;
478}
479
Tejun Heod0c4f572009-03-01 16:06:56 +0900480static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
481 unsigned long size, unsigned long align,
482 unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700484 unsigned long fallback = 0;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700485 unsigned long min, max, start, sidx, midx, step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Johannes Weiner594fe1a2009-01-06 14:40:32 -0800487 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
488 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
489 align, goal, limit);
490
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700491 BUG_ON(!size);
492 BUG_ON(align & (align - 1));
493 BUG_ON(limit && goal + size > limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494
Christian Krafft7c309a62006-12-06 20:32:41 -0800495 if (!bdata->node_bootmem_map)
496 return NULL;
497
Johannes Weiner3560e242008-07-23 21:28:09 -0700498 min = bdata->node_min_pfn;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700499 max = bdata->node_low_pfn;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700500
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700501 goal >>= PAGE_SHIFT;
502 limit >>= PAGE_SHIFT;
503
504 if (limit && max > limit)
505 max = limit;
506 if (max <= min)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700507 return NULL;
508
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700509 step = max(align >> PAGE_SHIFT, 1UL);
Yasunori Goto281dd252005-10-19 15:52:18 -0700510
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700511 if (goal && min < goal && goal < max)
512 start = ALIGN(goal, step);
513 else
514 start = ALIGN(min, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Johannes Weiner481ebd02008-08-20 14:09:15 -0700516 sidx = start - bdata->node_min_pfn;
Johannes Weiner3560e242008-07-23 21:28:09 -0700517 midx = max - bdata->node_min_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700519 if (bdata->hint_idx > sidx) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700520 /*
521 * Handle the valid case of sidx being zero and still
522 * catch the fallback below.
523 */
524 fallback = sidx + 1;
Johannes Weiner481ebd02008-08-20 14:09:15 -0700525 sidx = align_idx(bdata, bdata->hint_idx, step);
Yinghai Luad093152008-03-10 23:23:42 -0700526 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700528 while (1) {
529 int merge;
530 void *region;
531 unsigned long eidx, i, start_off, end_off;
532find_block:
533 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
Johannes Weiner481ebd02008-08-20 14:09:15 -0700534 sidx = align_idx(bdata, sidx, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700535 eidx = sidx + PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700537 if (sidx >= midx || eidx > midx)
Haren Myneni66d43e92005-12-12 00:37:39 -0800538 break;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700539
540 for (i = sidx; i < eidx; i++)
541 if (test_bit(i, bdata->node_bootmem_map)) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700542 sidx = align_idx(bdata, i, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700543 if (sidx == i)
544 sidx += step;
545 goto find_block;
546 }
547
Mikulas Patocka627240a2008-08-15 00:40:17 -0700548 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700549 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700550 start_off = align_off(bdata, bdata->last_end_off, align);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700551 else
552 start_off = PFN_PHYS(sidx);
553
554 merge = PFN_DOWN(start_off) < sidx;
555 end_off = start_off + size;
556
557 bdata->last_end_off = end_off;
558 bdata->hint_idx = PFN_UP(end_off);
559
560 /*
561 * Reserve the area now:
562 */
Johannes Weinerd747fa42008-07-23 21:28:05 -0700563 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
564 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
565 BUG();
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700566
Johannes Weiner3560e242008-07-23 21:28:09 -0700567 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
568 start_off);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700569 memset(region, 0, size);
Catalin Marinas008139d2009-08-27 14:29:17 +0100570 /*
571 * The min_count is set to 0 so that bootmem allocated blocks
572 * are never reported as leaks.
573 */
574 kmemleak_alloc(region, size, 0, 0);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700575 return region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
577
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700578 if (fallback) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700579 sidx = align_idx(bdata, fallback - 1, step);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700580 fallback = 0;
581 goto find_block;
582 }
583
584 return NULL;
585}
586
Tejun Heod0c4f572009-03-01 16:06:56 +0900587static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
588 unsigned long size, unsigned long align,
589 unsigned long goal, unsigned long limit)
590{
Pekka Enberg441c7e02009-06-10 20:05:53 +0300591 if (WARN_ON_ONCE(slab_is_available()))
592 return kzalloc(size, GFP_NOWAIT);
593
Tejun Heod0c4f572009-03-01 16:06:56 +0900594#ifdef CONFIG_HAVE_ARCH_BOOTMEM
Joe Perches433f13a2009-06-18 16:48:58 -0700595 {
596 bootmem_data_t *p_bdata;
Tejun Heod0c4f572009-03-01 16:06:56 +0900597
Joe Perches433f13a2009-06-18 16:48:58 -0700598 p_bdata = bootmem_arch_preferred_node(bdata, size, align,
599 goal, limit);
600 if (p_bdata)
601 return alloc_bootmem_core(p_bdata, size, align,
602 goal, limit);
603 }
Tejun Heod0c4f572009-03-01 16:06:56 +0900604#endif
605 return NULL;
606}
607
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700608static void * __init ___alloc_bootmem_nopanic(unsigned long size,
609 unsigned long align,
610 unsigned long goal,
611 unsigned long limit)
612{
613 bootmem_data_t *bdata;
Tejun Heod0c4f572009-03-01 16:06:56 +0900614 void *region;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700615
616restart:
Tejun Heod0c4f572009-03-01 16:06:56 +0900617 region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
618 if (region)
619 return region;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700620
Tejun Heod0c4f572009-03-01 16:06:56 +0900621 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700622 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
623 continue;
Johannes Weiner3560e242008-07-23 21:28:09 -0700624 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700625 break;
626
627 region = alloc_bootmem_core(bdata, size, align, goal, limit);
628 if (region)
629 return region;
630 }
631
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700632 if (goal) {
633 goal = 0;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700634 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 }
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700636
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700640/**
641 * __alloc_bootmem_nopanic - allocate boot memory without panicking
642 * @size: size of the request in bytes
643 * @align: alignment of the region
644 * @goal: preferred starting address of the region
645 *
646 * The goal is dropped if it can not be satisfied and the allocation will
647 * fall back to memory below @goal.
648 *
649 * Allocation may happen on any node in the system.
650 *
651 * Returns NULL on failure.
652 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700653void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700654 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655{
Yinghai Lu08677212010-02-10 01:20:20 -0800656 unsigned long limit = 0;
657
Yinghai Lu08677212010-02-10 01:20:20 -0800658 return ___alloc_bootmem_nopanic(size, align, goal, limit);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700659}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700661static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
662 unsigned long goal, unsigned long limit)
663{
664 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
665
666 if (mem)
667 return mem;
668 /*
669 * Whoops, we cannot satisfy the allocation request.
670 */
671 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
672 panic("Out of memory");
Andi Kleena8062232006-04-07 19:49:21 +0200673 return NULL;
674}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700676/**
677 * __alloc_bootmem - allocate boot memory
678 * @size: size of the request in bytes
679 * @align: alignment of the region
680 * @goal: preferred starting address of the region
681 *
682 * The goal is dropped if it can not be satisfied and the allocation will
683 * fall back to memory below @goal.
684 *
685 * Allocation may happen on any node in the system.
686 *
687 * The function panics if the request can not be satisfied.
688 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700689void * __init __alloc_bootmem(unsigned long size, unsigned long align,
690 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200691{
Yinghai Lu08677212010-02-10 01:20:20 -0800692 unsigned long limit = 0;
693
Yinghai Lu08677212010-02-10 01:20:20 -0800694 return ___alloc_bootmem(size, align, goal, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700697static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
698 unsigned long size, unsigned long align,
699 unsigned long goal, unsigned long limit)
700{
701 void *ptr;
702
Tejun Heod0c4f572009-03-01 16:06:56 +0900703 ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
704 if (ptr)
705 return ptr;
706
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700707 ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
708 if (ptr)
709 return ptr;
710
711 return ___alloc_bootmem(size, align, goal, limit);
712}
713
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700714/**
715 * __alloc_bootmem_node - allocate boot memory from a specific node
716 * @pgdat: node to allocate from
717 * @size: size of the request in bytes
718 * @align: alignment of the region
719 * @goal: preferred starting address of the region
720 *
721 * The goal is dropped if it can not be satisfied and the allocation will
722 * fall back to memory below @goal.
723 *
724 * Allocation may fall back to any node in the system if the specified node
725 * can not hold the requested memory.
726 *
727 * The function panics if the request can not be satisfied.
728 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700729void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
730 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731{
Pekka Enbergc91c4772009-06-11 08:10:28 +0300732 if (WARN_ON_ONCE(slab_is_available()))
733 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
734
Yinghai Lu09325872011-02-24 14:43:05 +0100735 return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800736}
737
738void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
739 unsigned long align, unsigned long goal)
740{
741#ifdef MAX_DMA32_PFN
742 unsigned long end_pfn;
743
744 if (WARN_ON_ONCE(slab_is_available()))
745 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
746
747 /* update goal according ...MAX_DMA32_PFN */
748 end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
749
750 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
751 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
752 void *ptr;
753 unsigned long new_goal;
754
755 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
Yinghai Lu08677212010-02-10 01:20:20 -0800756 ptr = alloc_bootmem_core(pgdat->bdata, size, align,
757 new_goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800758 if (ptr)
759 return ptr;
760 }
761#endif
762
763 return __alloc_bootmem_node(pgdat, size, align, goal);
764
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765}
766
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700767#ifdef CONFIG_SPARSEMEM
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700768/**
769 * alloc_bootmem_section - allocate boot memory from a specific section
770 * @size: size of the request in bytes
771 * @section_nr: sparse map section to allocate from
772 *
773 * Return NULL on failure.
774 */
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700775void * __init alloc_bootmem_section(unsigned long size,
776 unsigned long section_nr)
777{
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700778 bootmem_data_t *bdata;
779 unsigned long pfn, goal, limit;
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700780
781 pfn = section_nr_to_pfn(section_nr);
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700782 goal = pfn << PAGE_SHIFT;
783 limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
784 bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700785
Johannes Weiner75a56cf2008-07-23 21:28:09 -0700786 return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
Yasunori Gotoe70260a2008-04-28 02:13:32 -0700787}
788#endif
789
Andi Kleenb54bbf72008-07-23 21:27:45 -0700790void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
791 unsigned long align, unsigned long goal)
792{
793 void *ptr;
794
Pekka Enbergc91c4772009-06-11 08:10:28 +0300795 if (WARN_ON_ONCE(slab_is_available()))
796 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
797
Tejun Heod0c4f572009-03-01 16:06:56 +0900798 ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
799 if (ptr)
800 return ptr;
801
Andi Kleenb54bbf72008-07-23 21:27:45 -0700802 ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
803 if (ptr)
804 return ptr;
805
806 return __alloc_bootmem_nopanic(size, align, goal);
807}
808
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700809#ifndef ARCH_LOW_ADDRESS_LIMIT
810#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
811#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800812
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700813/**
814 * __alloc_bootmem_low - allocate low boot memory
815 * @size: size of the request in bytes
816 * @align: alignment of the region
817 * @goal: preferred starting address of the region
818 *
819 * The goal is dropped if it can not be satisfied and the allocation will
820 * fall back to memory below @goal.
821 *
822 * Allocation may happen on any node in the system.
823 *
824 * The function panics if the request can not be satisfied.
825 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700826void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
827 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800828{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700829 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800830}
831
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700832/**
833 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
834 * @pgdat: node to allocate from
835 * @size: size of the request in bytes
836 * @align: alignment of the region
837 * @goal: preferred starting address of the region
838 *
839 * The goal is dropped if it can not be satisfied and the allocation will
840 * fall back to memory below @goal.
841 *
842 * Allocation may fall back to any node in the system if the specified node
843 * can not hold the requested memory.
844 *
845 * The function panics if the request can not be satisfied.
846 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800847void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
848 unsigned long align, unsigned long goal)
849{
Pekka Enbergc91c4772009-06-11 08:10:28 +0300850 if (WARN_ON_ONCE(slab_is_available()))
851 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
852
Yinghai Lu09325872011-02-24 14:43:05 +0100853 return ___alloc_bootmem_node(pgdat->bdata, size, align,
Yinghai Lub8ab9f82010-07-20 13:24:31 -0700854 goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800855}