blob: 91e32bc8517ffe458f667a19a346ebd16ff2d0aa [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>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040015#include <linux/export.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>
Paul McQuaded85fbee2014-10-09 15:29:05 -070019#include <linux/bug.h>
20#include <linux/io.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070021
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;
Igor Mammedov8dd33032015-12-04 14:07:05 +010036unsigned long long max_possible_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
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{
Uwe Kleine-König9571a982012-01-10 15:08:00 -080060 unsigned long bytes = DIV_ROUND_UP(pages, 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{
Gavin Shan5c2b8a12012-05-29 15:06:46 -070081 bootmem_data_t *ent;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070082
Gavin Shan5c2b8a12012-05-29 15:06:46 -070083 list_for_each_entry(ent, &bdata_list, list) {
84 if (bdata->node_min_pfn < ent->node_min_pfn) {
85 list_add_tail(&bdata->list, &ent->list);
86 return;
87 }
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080088 }
Gavin Shan5c2b8a12012-05-29 15:06:46 -070089
90 list_add_tail(&bdata->list, &bdata_list);
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
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800151 * @addr: starting physical address of the range
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900152 * @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 */
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800158void __init free_bootmem_late(unsigned long physaddr, unsigned long size)
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900159{
160 unsigned long cursor, end;
161
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800162 kmemleak_free_part(__va(physaddr), size);
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900163
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800164 cursor = PFN_UP(physaddr);
165 end = PFN_DOWN(physaddr + size);
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900166
167 for (; cursor < end; cursor++) {
Mel Gormand70ddd72015-06-30 14:56:52 -0700168 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900169 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{
175 struct page *page;
Mel Gormand70ddd72015-06-30 14:56:52 -0700176 unsigned long *map, start, end, pages, cur, count = 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700177
Johannes Weiner41546c12008-07-23 21:28:03 -0700178 if (!bdata->node_bootmem_map)
179 return 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700180
Daeseok Youn4a099fb2013-11-12 15:08:14 -0800181 map = bdata->node_bootmem_map;
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 Weiner799f9332012-01-10 15:08:15 -0800185 bdebug("nid=%td start=%lx end=%lx\n",
186 bdata - bootmem_node_data, start, end);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700187
Johannes Weiner41546c12008-07-23 21:28:03 -0700188 while (start < end) {
Daeseok Youn4a099fb2013-11-12 15:08:14 -0800189 unsigned long idx, vec;
Max Filippov10d73e62013-01-11 14:31:52 -0800190 unsigned shift;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700191
Johannes Weiner3560e242008-07-23 21:28:09 -0700192 idx = start - bdata->node_min_pfn;
Max Filippov10d73e62013-01-11 14:31:52 -0800193 shift = idx & (BITS_PER_LONG - 1);
194 /*
195 * vec holds at most BITS_PER_LONG map bits,
196 * bit 0 corresponds to start.
197 */
Johannes Weiner41546c12008-07-23 21:28:03 -0700198 vec = ~map[idx / BITS_PER_LONG];
Max Filippov10d73e62013-01-11 14:31:52 -0800199
200 if (shift) {
201 vec >>= shift;
202 if (end - start >= BITS_PER_LONG)
203 vec |= ~map[idx / BITS_PER_LONG + 1] <<
204 (BITS_PER_LONG - shift);
205 }
Johannes Weiner799f9332012-01-10 15:08:15 -0800206 /*
207 * If we have a properly aligned and fully unreserved
208 * BITS_PER_LONG block of pages in front of us, free
209 * it in one go.
210 */
211 if (IS_ALIGNED(start, BITS_PER_LONG) && vec == ~0UL) {
Johannes Weiner41546c12008-07-23 21:28:03 -0700212 int order = ilog2(BITS_PER_LONG);
213
Mel Gormand70ddd72015-06-30 14:56:52 -0700214 __free_pages_bootmem(pfn_to_page(start), start, order);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700215 count += BITS_PER_LONG;
Johannes Weiner799f9332012-01-10 15:08:15 -0800216 start += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700217 } else {
Mel Gormand70ddd72015-06-30 14:56:52 -0700218 cur = start;
Johannes Weiner41546c12008-07-23 21:28:03 -0700219
Max Filippov10d73e62013-01-11 14:31:52 -0800220 start = ALIGN(start + 1, BITS_PER_LONG);
221 while (vec && cur != start) {
Johannes Weiner41546c12008-07-23 21:28:03 -0700222 if (vec & 1) {
Max Filippov10d73e62013-01-11 14:31:52 -0800223 page = pfn_to_page(cur);
Mel Gormand70ddd72015-06-30 14:56:52 -0700224 __free_pages_bootmem(page, cur, 0);
Johannes Weiner41546c12008-07-23 21:28:03 -0700225 count++;
226 }
227 vec >>= 1;
Max Filippov10d73e62013-01-11 14:31:52 -0800228 ++cur;
Johannes Weiner41546c12008-07-23 21:28:03 -0700229 }
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700230 }
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700231 }
232
Mel Gormand70ddd72015-06-30 14:56:52 -0700233 cur = bdata->node_min_pfn;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700234 page = virt_to_page(bdata->node_bootmem_map);
Johannes Weiner3560e242008-07-23 21:28:09 -0700235 pages = bdata->node_low_pfn - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700236 pages = bootmem_bootmap_pages(pages);
237 count += pages;
Andrew Morton55766462012-11-16 14:15:06 -0800238 while (pages--)
Mel Gormand70ddd72015-06-30 14:56:52 -0700239 __free_pages_bootmem(page++, cur++, 0);
Chris Metcalf1b4ace42015-09-08 15:02:12 -0700240 bdata->node_bootmem_map = NULL;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700241
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700242 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
243
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700244 return count;
245}
246
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700247static int reset_managed_pages_done __initdata;
248
Tang Chenf784a3f2014-11-13 15:19:39 -0800249void reset_node_managed_pages(pg_data_t *pgdat)
Jiang Liu9feedc92012-12-12 13:52:12 -0800250{
251 struct zone *z;
252
Jiang Liu9feedc92012-12-12 13:52:12 -0800253 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700254 z->managed_pages = 0;
255}
256
257void __init reset_all_zones_managed_pages(void)
258{
259 struct pglist_data *pgdat;
260
Tang Chenf784a3f2014-11-13 15:19:39 -0800261 if (reset_managed_pages_done)
262 return;
263
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700264 for_each_online_pgdat(pgdat)
265 reset_node_managed_pages(pgdat);
Tang Chenf784a3f2014-11-13 15:19:39 -0800266
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700267 reset_managed_pages_done = 1;
Jiang Liu9feedc92012-12-12 13:52:12 -0800268}
269
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700270/**
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700271 * free_all_bootmem - release free pages to the buddy allocator
272 *
273 * Returns the number of pages actually released.
274 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700275unsigned long __init free_all_bootmem(void)
276{
Yinghai Luaa235fc2010-03-31 20:45:27 -0700277 unsigned long total_pages = 0;
278 bootmem_data_t *bdata;
Jiang Liu9feedc92012-12-12 13:52:12 -0800279
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700280 reset_all_zones_managed_pages();
Yinghai Luaa235fc2010-03-31 20:45:27 -0700281
282 list_for_each_entry(bdata, &bdata_list, list)
283 total_pages += free_all_bootmem_core(bdata);
284
Jiang Liu0c988532013-07-03 15:03:24 -0700285 totalram_pages += total_pages;
286
Yinghai Luaa235fc2010-03-31 20:45:27 -0700287 return total_pages;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700288}
289
Johannes Weinerd747fa42008-07-23 21:28:05 -0700290static void __init __free(bootmem_data_t *bdata,
291 unsigned long sidx, unsigned long eidx)
292{
293 unsigned long idx;
294
295 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700296 sidx + bdata->node_min_pfn,
297 eidx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700298
Chris Metcalf1b4ace42015-09-08 15:02:12 -0700299 if (WARN_ON(bdata->node_bootmem_map == NULL))
300 return;
301
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700302 if (bdata->hint_idx > sidx)
303 bdata->hint_idx = sidx;
304
Johannes Weinerd747fa42008-07-23 21:28:05 -0700305 for (idx = sidx; idx < eidx; idx++)
306 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
307 BUG();
308}
309
310static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
311 unsigned long eidx, int flags)
312{
313 unsigned long idx;
314 int exclusive = flags & BOOTMEM_EXCLUSIVE;
315
316 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
317 bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700318 sidx + bdata->node_min_pfn,
319 eidx + bdata->node_min_pfn,
Johannes Weinerd747fa42008-07-23 21:28:05 -0700320 flags);
321
Chris Metcalf1b4ace42015-09-08 15:02:12 -0700322 if (WARN_ON(bdata->node_bootmem_map == NULL))
323 return 0;
324
Johannes Weinerd747fa42008-07-23 21:28:05 -0700325 for (idx = sidx; idx < eidx; idx++)
326 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
327 if (exclusive) {
328 __free(bdata, sidx, idx);
329 return -EBUSY;
330 }
331 bdebug("silent double reserve of PFN %lx\n",
Johannes Weiner3560e242008-07-23 21:28:09 -0700332 idx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700333 }
334 return 0;
335}
336
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700337static int __init mark_bootmem_node(bootmem_data_t *bdata,
338 unsigned long start, unsigned long end,
339 int reserve, int flags)
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700340{
341 unsigned long sidx, eidx;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700342
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700343 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
344 bdata - bootmem_node_data, start, end, reserve, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700345
Johannes Weiner3560e242008-07-23 21:28:09 -0700346 BUG_ON(start < bdata->node_min_pfn);
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700347 BUG_ON(end > bdata->node_low_pfn);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700348
Johannes Weiner3560e242008-07-23 21:28:09 -0700349 sidx = start - bdata->node_min_pfn;
350 eidx = end - bdata->node_min_pfn;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700351
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700352 if (reserve)
353 return __reserve(bdata, sidx, eidx, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700354 else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700355 __free(bdata, sidx, eidx);
356 return 0;
357}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700358
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700359static int __init mark_bootmem(unsigned long start, unsigned long end,
360 int reserve, int flags)
361{
362 unsigned long pos;
363 bootmem_data_t *bdata;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700364
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700365 pos = start;
366 list_for_each_entry(bdata, &bdata_list, list) {
367 int err;
368 unsigned long max;
369
Johannes Weiner3560e242008-07-23 21:28:09 -0700370 if (pos < bdata->node_min_pfn ||
371 pos >= bdata->node_low_pfn) {
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700372 BUG_ON(pos != start);
373 continue;
374 }
375
376 max = min(bdata->node_low_pfn, end);
377
378 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
379 if (reserve && err) {
380 mark_bootmem(start, pos, 0, 0);
381 return err;
382 }
383
384 if (max == end)
385 return 0;
386 pos = bdata->node_low_pfn;
387 }
388 BUG();
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700389}
390
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700391/**
392 * free_bootmem_node - mark a page range as usable
393 * @pgdat: node the range resides on
394 * @physaddr: starting address of the range
395 * @size: size of the range in bytes
396 *
397 * Partial pages will be considered reserved and left as they are.
398 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700399 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700400 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700401void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
402 unsigned long size)
403{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700404 unsigned long start, end;
405
Catalin Marinasec3a3542009-07-07 10:33:01 +0100406 kmemleak_free_part(__va(physaddr), size);
407
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700408 start = PFN_UP(physaddr);
409 end = PFN_DOWN(physaddr + size);
410
411 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700412}
413
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700414/**
415 * free_bootmem - mark a page range as usable
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800416 * @addr: starting physical address of the range
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700417 * @size: size of the range in bytes
418 *
419 * Partial pages will be considered reserved and left as they are.
420 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700421 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700422 */
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800423void __init free_bootmem(unsigned long physaddr, unsigned long size)
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700424{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700425 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700426
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800427 kmemleak_free_part(__va(physaddr), size);
Catalin Marinasec3a3542009-07-07 10:33:01 +0100428
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800429 start = PFN_UP(physaddr);
430 end = PFN_DOWN(physaddr + size);
Yinghai Lua5645a62008-03-18 12:49:12 -0700431
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700432 mark_bootmem(start, end, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700435/**
436 * reserve_bootmem_node - mark a page range as reserved
437 * @pgdat: node the range resides on
438 * @physaddr: starting address of the range
439 * @size: size of the range in bytes
440 * @flags: reservation flags (see linux/bootmem.h)
441 *
442 * Partial pages will be reserved.
443 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700444 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700445 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700446int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
447 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700449 unsigned long start, end;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700450
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700451 start = PFN_DOWN(physaddr);
452 end = PFN_UP(physaddr + size);
453
454 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455}
456
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700457/**
Javi Merino0d4ba4d2012-08-24 18:09:31 +0100458 * reserve_bootmem - mark a page range as reserved
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700459 * @addr: starting address of the range
460 * @size: size of the range in bytes
461 * @flags: reservation flags (see linux/bootmem.h)
462 *
463 * Partial pages will be reserved.
464 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700465 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700466 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700467int __init reserve_bootmem(unsigned long addr, unsigned long size,
468 int flags)
469{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700470 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700471
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700472 start = PFN_DOWN(addr);
473 end = PFN_UP(addr + size);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700474
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700475 return mark_bootmem(start, end, 1, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700476}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700477
Jan Beulich8aa043d2009-12-14 17:59:46 -0800478static unsigned long __init align_idx(struct bootmem_data *bdata,
479 unsigned long idx, unsigned long step)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700480{
481 unsigned long base = bdata->node_min_pfn;
482
483 /*
484 * Align the index with respect to the node start so that the
485 * combination of both satisfies the requested alignment.
486 */
487
488 return ALIGN(base + idx, step) - base;
489}
490
Jan Beulich8aa043d2009-12-14 17:59:46 -0800491static unsigned long __init align_off(struct bootmem_data *bdata,
492 unsigned long off, unsigned long align)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700493{
494 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
495
496 /* Same as align_idx for byte offsets */
497
498 return ALIGN(base + off, align) - base;
499}
500
Johannes Weinerc6785b62012-05-29 15:06:33 -0700501static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata,
Tejun Heod0c4f572009-03-01 16:06:56 +0900502 unsigned long size, unsigned long align,
503 unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700505 unsigned long fallback = 0;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700506 unsigned long min, max, start, sidx, midx, step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Johannes Weiner594fe1a2009-01-06 14:40:32 -0800508 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
509 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
510 align, goal, limit);
511
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700512 BUG_ON(!size);
513 BUG_ON(align & (align - 1));
514 BUG_ON(limit && goal + size > limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515
Christian Krafft7c309a62006-12-06 20:32:41 -0800516 if (!bdata->node_bootmem_map)
517 return NULL;
518
Johannes Weiner3560e242008-07-23 21:28:09 -0700519 min = bdata->node_min_pfn;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700520 max = bdata->node_low_pfn;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700521
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700522 goal >>= PAGE_SHIFT;
523 limit >>= PAGE_SHIFT;
524
525 if (limit && max > limit)
526 max = limit;
527 if (max <= min)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700528 return NULL;
529
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700530 step = max(align >> PAGE_SHIFT, 1UL);
Yasunori Goto281dd252005-10-19 15:52:18 -0700531
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700532 if (goal && min < goal && goal < max)
533 start = ALIGN(goal, step);
534 else
535 start = ALIGN(min, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Johannes Weiner481ebd02008-08-20 14:09:15 -0700537 sidx = start - bdata->node_min_pfn;
Johannes Weiner3560e242008-07-23 21:28:09 -0700538 midx = max - bdata->node_min_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700540 if (bdata->hint_idx > sidx) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700541 /*
542 * Handle the valid case of sidx being zero and still
543 * catch the fallback below.
544 */
545 fallback = sidx + 1;
Johannes Weiner481ebd02008-08-20 14:09:15 -0700546 sidx = align_idx(bdata, bdata->hint_idx, step);
Yinghai Luad093152008-03-10 23:23:42 -0700547 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700549 while (1) {
550 int merge;
551 void *region;
552 unsigned long eidx, i, start_off, end_off;
553find_block:
554 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
Johannes Weiner481ebd02008-08-20 14:09:15 -0700555 sidx = align_idx(bdata, sidx, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700556 eidx = sidx + PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700558 if (sidx >= midx || eidx > midx)
Haren Myneni66d43e92005-12-12 00:37:39 -0800559 break;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700560
561 for (i = sidx; i < eidx; i++)
562 if (test_bit(i, bdata->node_bootmem_map)) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700563 sidx = align_idx(bdata, i, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700564 if (sidx == i)
565 sidx += step;
566 goto find_block;
567 }
568
Mikulas Patocka627240a2008-08-15 00:40:17 -0700569 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700570 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700571 start_off = align_off(bdata, bdata->last_end_off, align);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700572 else
573 start_off = PFN_PHYS(sidx);
574
575 merge = PFN_DOWN(start_off) < sidx;
576 end_off = start_off + size;
577
578 bdata->last_end_off = end_off;
579 bdata->hint_idx = PFN_UP(end_off);
580
581 /*
582 * Reserve the area now:
583 */
Johannes Weinerd747fa42008-07-23 21:28:05 -0700584 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
585 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
586 BUG();
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700587
Johannes Weiner3560e242008-07-23 21:28:09 -0700588 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
589 start_off);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700590 memset(region, 0, size);
Catalin Marinas008139d2009-08-27 14:29:17 +0100591 /*
592 * The min_count is set to 0 so that bootmem allocated blocks
593 * are never reported as leaks.
594 */
595 kmemleak_alloc(region, size, 0, 0);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700596 return region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700599 if (fallback) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700600 sidx = align_idx(bdata, fallback - 1, step);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700601 fallback = 0;
602 goto find_block;
603 }
604
605 return NULL;
606}
607
Johannes Weinerc12ab502012-05-29 15:06:33 -0700608static void * __init alloc_bootmem_core(unsigned long size,
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700609 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
Joonsoo Kim3f7dfe22012-12-12 13:50:43 -0800616 if (WARN_ON_ONCE(slab_is_available()))
617 return kzalloc(size, GFP_NOWAIT);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700618
Tejun Heod0c4f572009-03-01 16:06:56 +0900619 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700620 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
621 continue;
Johannes Weiner3560e242008-07-23 21:28:09 -0700622 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700623 break;
624
Johannes Weinerc6785b62012-05-29 15:06:33 -0700625 region = alloc_bootmem_bdata(bdata, size, align, goal, limit);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700626 if (region)
627 return region;
628 }
629
Johannes Weinerc12ab502012-05-29 15:06:33 -0700630 return NULL;
631}
632
633static void * __init ___alloc_bootmem_nopanic(unsigned long size,
634 unsigned long align,
635 unsigned long goal,
636 unsigned long limit)
637{
638 void *ptr;
639
640restart:
641 ptr = alloc_bootmem_core(size, align, goal, limit);
642 if (ptr)
643 return ptr;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700644 if (goal) {
645 goal = 0;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700646 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 }
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650}
651
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700652/**
653 * __alloc_bootmem_nopanic - allocate boot memory without panicking
654 * @size: size of the request in bytes
655 * @align: alignment of the region
656 * @goal: preferred starting address of the region
657 *
658 * The goal is dropped if it can not be satisfied and the allocation will
659 * fall back to memory below @goal.
660 *
661 * Allocation may happen on any node in the system.
662 *
663 * Returns NULL on failure.
664 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700665void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700666 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Yinghai Lu08677212010-02-10 01:20:20 -0800668 unsigned long limit = 0;
669
Yinghai Lu08677212010-02-10 01:20:20 -0800670 return ___alloc_bootmem_nopanic(size, align, goal, limit);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700671}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700673static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
674 unsigned long goal, unsigned long limit)
675{
676 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
677
678 if (mem)
679 return mem;
680 /*
681 * Whoops, we cannot satisfy the allocation request.
682 */
683 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
684 panic("Out of memory");
Andi Kleena8062232006-04-07 19:49:21 +0200685 return NULL;
686}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700688/**
689 * __alloc_bootmem - allocate boot memory
690 * @size: size of the request in bytes
691 * @align: alignment of the region
692 * @goal: preferred starting address of the region
693 *
694 * The goal is dropped if it can not be satisfied and the allocation will
695 * fall back to memory below @goal.
696 *
697 * Allocation may happen on any node in the system.
698 *
699 * The function panics if the request can not be satisfied.
700 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700701void * __init __alloc_bootmem(unsigned long size, unsigned long align,
702 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200703{
Yinghai Lu08677212010-02-10 01:20:20 -0800704 unsigned long limit = 0;
705
Yinghai Lu08677212010-02-10 01:20:20 -0800706 return ___alloc_bootmem(size, align, goal, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700709void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700710 unsigned long size, unsigned long align,
711 unsigned long goal, unsigned long limit)
712{
713 void *ptr;
714
Joonsoo Kim3f7dfe22012-12-12 13:50:43 -0800715 if (WARN_ON_ONCE(slab_is_available()))
716 return kzalloc(size, GFP_NOWAIT);
Johannes Weinerab381842012-05-29 15:06:34 -0700717again:
Tejun Heod0c4f572009-03-01 16:06:56 +0900718
Yinghai Luc8f4a2d2012-07-17 15:47:51 -0700719 /* do not panic in alloc_bootmem_bdata() */
720 if (limit && goal + size > limit)
721 limit = 0;
722
Johannes Weinere9079912012-05-29 15:06:36 -0700723 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit);
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700724 if (ptr)
725 return ptr;
726
Johannes Weinerab381842012-05-29 15:06:34 -0700727 ptr = alloc_bootmem_core(size, align, goal, limit);
728 if (ptr)
729 return ptr;
730
731 if (goal) {
732 goal = 0;
733 goto again;
734 }
735
Johannes Weiner421456e2012-05-29 15:06:34 -0700736 return NULL;
737}
738
739void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
740 unsigned long align, unsigned long goal)
741{
742 if (WARN_ON_ONCE(slab_is_available()))
743 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
744
Johannes Weinere9079912012-05-29 15:06:36 -0700745 return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
Johannes Weiner421456e2012-05-29 15:06:34 -0700746}
747
Johannes Weinere9079912012-05-29 15:06:36 -0700748void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
Johannes Weiner421456e2012-05-29 15:06:34 -0700749 unsigned long align, unsigned long goal,
750 unsigned long limit)
751{
752 void *ptr;
753
Johannes Weinere9079912012-05-29 15:06:36 -0700754 ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
Johannes Weiner421456e2012-05-29 15:06:34 -0700755 if (ptr)
756 return ptr;
757
Johannes Weinerab381842012-05-29 15:06:34 -0700758 printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
759 panic("Out of memory");
760 return NULL;
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700761}
762
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700763/**
764 * __alloc_bootmem_node - allocate boot memory from a specific node
765 * @pgdat: node to allocate from
766 * @size: size of the request in bytes
767 * @align: alignment of the region
768 * @goal: preferred starting address of the region
769 *
770 * The goal is dropped if it can not be satisfied and the allocation will
771 * fall back to memory below @goal.
772 *
773 * Allocation may fall back to any node in the system if the specified node
774 * can not hold the requested memory.
775 *
776 * The function panics if the request can not be satisfied.
777 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700778void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
779 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780{
Pekka Enbergc91c4772009-06-11 08:10:28 +0300781 if (WARN_ON_ONCE(slab_is_available()))
782 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
783
Johannes Weinere9079912012-05-29 15:06:36 -0700784 return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800785}
786
787void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
788 unsigned long align, unsigned long goal)
789{
790#ifdef MAX_DMA32_PFN
791 unsigned long end_pfn;
792
793 if (WARN_ON_ONCE(slab_is_available()))
794 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
795
796 /* update goal according ...MAX_DMA32_PFN */
Xishi Qiu83285c72013-11-12 15:07:19 -0800797 end_pfn = pgdat_end_pfn(pgdat);
Yinghai Lu08677212010-02-10 01:20:20 -0800798
799 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
800 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
801 void *ptr;
802 unsigned long new_goal;
803
804 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
Johannes Weinerc6785b62012-05-29 15:06:33 -0700805 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align,
Yinghai Lu08677212010-02-10 01:20:20 -0800806 new_goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800807 if (ptr)
808 return ptr;
809 }
810#endif
811
812 return __alloc_bootmem_node(pgdat, size, align, goal);
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814}
815
Heiko Carstensdfd54cb2006-09-25 23:31:33 -0700816#ifndef ARCH_LOW_ADDRESS_LIMIT
817#define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
818#endif
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800819
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700820/**
821 * __alloc_bootmem_low - allocate low boot memory
822 * @size: size of the request in bytes
823 * @align: alignment of the region
824 * @goal: preferred starting address of the region
825 *
826 * The goal is dropped if it can not be satisfied and the allocation will
827 * fall back to memory below @goal.
828 *
829 * Allocation may happen on any node in the system.
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_low(unsigned long size, unsigned long align,
834 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800835{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700836 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800837}
838
Yinghai Lu38fa4172013-01-24 12:20:15 -0800839void * __init __alloc_bootmem_low_nopanic(unsigned long size,
840 unsigned long align,
841 unsigned long goal)
842{
843 return ___alloc_bootmem_nopanic(size, align, goal,
844 ARCH_LOW_ADDRESS_LIMIT);
845}
846
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700847/**
848 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
849 * @pgdat: node to allocate from
850 * @size: size of the request in bytes
851 * @align: alignment of the region
852 * @goal: preferred starting address of the region
853 *
854 * The goal is dropped if it can not be satisfied and the allocation will
855 * fall back to memory below @goal.
856 *
857 * Allocation may fall back to any node in the system if the specified node
858 * can not hold the requested memory.
859 *
860 * The function panics if the request can not be satisfied.
861 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800862void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
863 unsigned long align, unsigned long goal)
864{
Pekka Enbergc91c4772009-06-11 08:10:28 +0300865 if (WARN_ON_ONCE(slab_is_available()))
866 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
867
Johannes Weinere9079912012-05-29 15:06:36 -0700868 return ___alloc_bootmem_node(pgdat, size, align,
869 goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800870}