blob: e8a55a3c9febae80fab4627bc8f0846f4326c56a [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>
Paul Gortmakerb95f1b312011-10-16 02:01:52 -040014#include <linux/export.h>
Catalin Marinasec3a3542009-07-07 10:33:01 +010015#include <linux/kmemleak.h>
Yinghai Lu08677212010-02-10 01:20:20 -080016#include <linux/range.h>
Paul McQuaded85fbee2014-10-09 15:29:05 -070017#include <linux/bug.h>
18#include <linux/io.h>
zijun_hu1d8bf922016-10-07 16:59:27 -070019#include <linux/bootmem.h>
Franck Bui-Huue786e862006-09-25 23:31:06 -070020
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "internal.h"
22
Yinghai Lue782ab42011-02-24 14:43:06 +010023#ifndef CONFIG_NEED_MULTIPLE_NODES
24struct pglist_data __refdata contig_page_data = {
25 .bdata = &bootmem_node_data[0]
26};
27EXPORT_SYMBOL(contig_page_data);
28#endif
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030unsigned long max_low_pfn;
31unsigned long min_low_pfn;
32unsigned long max_pfn;
Igor Mammedov8dd33032015-12-04 14:07:05 +010033unsigned long long max_possible_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Johannes Weinerb61bfa32008-07-23 21:26:55 -070035bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
36
Johannes Weiner636cc402008-07-23 21:28:03 -070037static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
38
Johannes Weiner2e5237d2008-07-23 21:28:02 -070039static int bootmem_debug;
40
41static int __init bootmem_debug_setup(char *buf)
42{
43 bootmem_debug = 1;
44 return 0;
45}
46early_param("bootmem_debug", bootmem_debug_setup);
47
48#define bdebug(fmt, args...) ({ \
49 if (unlikely(bootmem_debug)) \
Joe Perches11705322016-03-17 14:19:50 -070050 pr_info("bootmem::%s " fmt, \
Harvey Harrison80a914d2008-10-15 22:01:25 -070051 __func__, ## args); \
Johannes Weiner2e5237d2008-07-23 21:28:02 -070052})
53
Johannes Weinerdf049a52008-07-23 21:28:02 -070054static unsigned long __init bootmap_bytes(unsigned long pages)
Johannes Weiner223e8dc2008-07-23 21:28:00 -070055{
Uwe Kleine-König9571a982012-01-10 15:08:00 -080056 unsigned long bytes = DIV_ROUND_UP(pages, 8);
Johannes Weiner223e8dc2008-07-23 21:28:00 -070057
Johannes Weinerdf049a52008-07-23 21:28:02 -070058 return ALIGN(bytes, sizeof(long));
Johannes Weiner223e8dc2008-07-23 21:28:00 -070059}
60
Johannes Weinera66fd7d2008-07-23 21:28:01 -070061/**
62 * bootmem_bootmap_pages - calculate bitmap size in pages
63 * @pages: number of pages the bitmap has to represent
64 */
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070065unsigned long __init bootmem_bootmap_pages(unsigned long pages)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Johannes Weinerdf049a52008-07-23 21:28:02 -070067 unsigned long bytes = bootmap_bytes(pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Johannes Weinerdf049a52008-07-23 21:28:02 -070069 return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
Linus Torvalds1da177e2005-04-16 15:20:36 -070070}
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070071
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080072/*
73 * link bdata in order
74 */
Franck Bui-Huu69d49e62006-09-25 23:31:04 -070075static void __init link_bootmem(bootmem_data_t *bdata)
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080076{
Gavin Shan5c2b8a12012-05-29 15:06:46 -070077 bootmem_data_t *ent;
Franck Bui-Huuf71bf0c2006-09-25 23:31:08 -070078
Gavin Shan5c2b8a12012-05-29 15:06:46 -070079 list_for_each_entry(ent, &bdata_list, list) {
80 if (bdata->node_min_pfn < ent->node_min_pfn) {
81 list_add_tail(&bdata->list, &ent->list);
82 return;
83 }
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080084 }
Gavin Shan5c2b8a12012-05-29 15:06:46 -070085
86 list_add_tail(&bdata->list, &bdata_list);
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -080087}
88
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070089/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 * Called once to set up the allocator itself.
91 */
Johannes Weiner8ae04462008-07-23 21:26:59 -070092static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 unsigned long mapstart, unsigned long start, unsigned long end)
94{
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070095 unsigned long mapsize;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Mel Gorman2dbb51c2008-07-23 21:26:52 -070097 mminit_validate_memmodel_limits(&start, &end);
Franck Bui-Huubbc7b922006-09-25 23:31:07 -070098 bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
Johannes Weiner3560e242008-07-23 21:28:09 -070099 bdata->node_min_pfn = start;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 bdata->node_low_pfn = end;
KAMEZAWA Hiroyuki679bc9f2006-03-27 01:15:58 -0800101 link_bootmem(bdata);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103 /*
104 * Initially all pages are reserved - setup_arch() has to
105 * register free RAM areas explicitly.
106 */
Johannes Weinerdf049a52008-07-23 21:28:02 -0700107 mapsize = bootmap_bytes(end - start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 memset(bdata->node_bootmem_map, 0xff, mapsize);
109
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700110 bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
111 bdata - bootmem_node_data, start, mapstart, end, mapsize);
112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 return mapsize;
114}
115
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700116/**
117 * init_bootmem_node - register a node as boot memory
118 * @pgdat: node to register
119 * @freepfn: pfn where the bitmap for this node is to be placed
120 * @startpfn: first pfn on the node
121 * @endpfn: first pfn after the node
122 *
123 * Returns the number of bytes needed to hold the bitmap for this node.
124 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700125unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
126 unsigned long startpfn, unsigned long endpfn)
127{
128 return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
129}
130
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700131/**
132 * init_bootmem - register boot memory
133 * @start: pfn where the bitmap is to be placed
134 * @pages: number of available physical pages
135 *
136 * Returns the number of bytes needed to hold the bitmap.
137 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700138unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
139{
140 max_low_pfn = pages;
141 min_low_pfn = start;
142 return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
143}
Yinghai Lu09325872011-02-24 14:43:05 +0100144
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900145/*
146 * free_bootmem_late - free bootmem pages directly to page allocator
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800147 * @addr: starting physical address of the range
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900148 * @size: size of the range in bytes
149 *
150 * This is only useful when the bootmem allocator has already been torn
151 * down, but we are still initializing the system. Pages are given directly
152 * to the page allocator, no bootmem metadata is updated because it is gone.
153 */
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800154void __init free_bootmem_late(unsigned long physaddr, unsigned long size)
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900155{
156 unsigned long cursor, end;
157
Catalin Marinas9099dae2016-10-11 13:55:11 -0700158 kmemleak_free_part_phys(physaddr, size);
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900159
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800160 cursor = PFN_UP(physaddr);
161 end = PFN_DOWN(physaddr + size);
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900162
163 for (; cursor < end; cursor++) {
Mel Gormand70ddd72015-06-30 14:56:52 -0700164 __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
FUJITA Tomonori9f993ac2009-11-10 19:46:17 +0900165 totalram_pages++;
166 }
167}
168
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700169static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
170{
171 struct page *page;
Mel Gormand70ddd72015-06-30 14:56:52 -0700172 unsigned long *map, start, end, pages, cur, count = 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700173
Johannes Weiner41546c12008-07-23 21:28:03 -0700174 if (!bdata->node_bootmem_map)
175 return 0;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700176
Daeseok Youn4a099fb2013-11-12 15:08:14 -0800177 map = bdata->node_bootmem_map;
Johannes Weiner3560e242008-07-23 21:28:09 -0700178 start = bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700179 end = bdata->node_low_pfn;
180
Johannes Weiner799f9332012-01-10 15:08:15 -0800181 bdebug("nid=%td start=%lx end=%lx\n",
182 bdata - bootmem_node_data, start, end);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700183
Johannes Weiner41546c12008-07-23 21:28:03 -0700184 while (start < end) {
Daeseok Youn4a099fb2013-11-12 15:08:14 -0800185 unsigned long idx, vec;
Max Filippov10d73e62013-01-11 14:31:52 -0800186 unsigned shift;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700187
Johannes Weiner3560e242008-07-23 21:28:09 -0700188 idx = start - bdata->node_min_pfn;
Max Filippov10d73e62013-01-11 14:31:52 -0800189 shift = idx & (BITS_PER_LONG - 1);
190 /*
191 * vec holds at most BITS_PER_LONG map bits,
192 * bit 0 corresponds to start.
193 */
Johannes Weiner41546c12008-07-23 21:28:03 -0700194 vec = ~map[idx / BITS_PER_LONG];
Max Filippov10d73e62013-01-11 14:31:52 -0800195
196 if (shift) {
197 vec >>= shift;
198 if (end - start >= BITS_PER_LONG)
199 vec |= ~map[idx / BITS_PER_LONG + 1] <<
200 (BITS_PER_LONG - shift);
201 }
Johannes Weiner799f9332012-01-10 15:08:15 -0800202 /*
203 * If we have a properly aligned and fully unreserved
204 * BITS_PER_LONG block of pages in front of us, free
205 * it in one go.
206 */
207 if (IS_ALIGNED(start, BITS_PER_LONG) && vec == ~0UL) {
Johannes Weiner41546c12008-07-23 21:28:03 -0700208 int order = ilog2(BITS_PER_LONG);
209
Mel Gormand70ddd72015-06-30 14:56:52 -0700210 __free_pages_bootmem(pfn_to_page(start), start, order);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700211 count += BITS_PER_LONG;
Johannes Weiner799f9332012-01-10 15:08:15 -0800212 start += BITS_PER_LONG;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700213 } else {
Mel Gormand70ddd72015-06-30 14:56:52 -0700214 cur = start;
Johannes Weiner41546c12008-07-23 21:28:03 -0700215
Max Filippov10d73e62013-01-11 14:31:52 -0800216 start = ALIGN(start + 1, BITS_PER_LONG);
217 while (vec && cur != start) {
Johannes Weiner41546c12008-07-23 21:28:03 -0700218 if (vec & 1) {
Max Filippov10d73e62013-01-11 14:31:52 -0800219 page = pfn_to_page(cur);
Mel Gormand70ddd72015-06-30 14:56:52 -0700220 __free_pages_bootmem(page, cur, 0);
Johannes Weiner41546c12008-07-23 21:28:03 -0700221 count++;
222 }
223 vec >>= 1;
Max Filippov10d73e62013-01-11 14:31:52 -0800224 ++cur;
Johannes Weiner41546c12008-07-23 21:28:03 -0700225 }
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700226 }
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700227 }
228
Mel Gormand70ddd72015-06-30 14:56:52 -0700229 cur = bdata->node_min_pfn;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700230 page = virt_to_page(bdata->node_bootmem_map);
Johannes Weiner3560e242008-07-23 21:28:09 -0700231 pages = bdata->node_low_pfn - bdata->node_min_pfn;
Johannes Weiner41546c12008-07-23 21:28:03 -0700232 pages = bootmem_bootmap_pages(pages);
233 count += pages;
Andrew Morton55766462012-11-16 14:15:06 -0800234 while (pages--)
Mel Gormand70ddd72015-06-30 14:56:52 -0700235 __free_pages_bootmem(page++, cur++, 0);
Chris Metcalf1b4ace42015-09-08 15:02:12 -0700236 bdata->node_bootmem_map = NULL;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700237
Johannes Weiner2e5237d2008-07-23 21:28:02 -0700238 bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
239
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700240 return count;
241}
242
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700243static int reset_managed_pages_done __initdata;
244
Tang Chenf784a3f2014-11-13 15:19:39 -0800245void reset_node_managed_pages(pg_data_t *pgdat)
Jiang Liu9feedc92012-12-12 13:52:12 -0800246{
247 struct zone *z;
248
Jiang Liu9feedc92012-12-12 13:52:12 -0800249 for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700250 z->managed_pages = 0;
251}
252
253void __init reset_all_zones_managed_pages(void)
254{
255 struct pglist_data *pgdat;
256
Tang Chenf784a3f2014-11-13 15:19:39 -0800257 if (reset_managed_pages_done)
258 return;
259
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700260 for_each_online_pgdat(pgdat)
261 reset_node_managed_pages(pgdat);
Tang Chenf784a3f2014-11-13 15:19:39 -0800262
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700263 reset_managed_pages_done = 1;
Jiang Liu9feedc92012-12-12 13:52:12 -0800264}
265
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700266/**
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700267 * free_all_bootmem - release free pages to the buddy allocator
268 *
269 * Returns the number of pages actually released.
270 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700271unsigned long __init free_all_bootmem(void)
272{
Yinghai Luaa235fc2010-03-31 20:45:27 -0700273 unsigned long total_pages = 0;
274 bootmem_data_t *bdata;
Jiang Liu9feedc92012-12-12 13:52:12 -0800275
Jiang Liu7b4b2a02013-07-03 15:03:11 -0700276 reset_all_zones_managed_pages();
Yinghai Luaa235fc2010-03-31 20:45:27 -0700277
278 list_for_each_entry(bdata, &bdata_list, list)
279 total_pages += free_all_bootmem_core(bdata);
280
Jiang Liu0c988532013-07-03 15:03:24 -0700281 totalram_pages += total_pages;
282
Yinghai Luaa235fc2010-03-31 20:45:27 -0700283 return total_pages;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700284}
285
Johannes Weinerd747fa42008-07-23 21:28:05 -0700286static void __init __free(bootmem_data_t *bdata,
287 unsigned long sidx, unsigned long eidx)
288{
289 unsigned long idx;
290
291 bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700292 sidx + bdata->node_min_pfn,
293 eidx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700294
Chris Metcalf1b4ace42015-09-08 15:02:12 -0700295 if (WARN_ON(bdata->node_bootmem_map == NULL))
296 return;
297
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700298 if (bdata->hint_idx > sidx)
299 bdata->hint_idx = sidx;
300
Johannes Weinerd747fa42008-07-23 21:28:05 -0700301 for (idx = sidx; idx < eidx; idx++)
302 if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
303 BUG();
304}
305
306static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
307 unsigned long eidx, int flags)
308{
309 unsigned long idx;
310 int exclusive = flags & BOOTMEM_EXCLUSIVE;
311
312 bdebug("nid=%td start=%lx end=%lx flags=%x\n",
313 bdata - bootmem_node_data,
Johannes Weiner3560e242008-07-23 21:28:09 -0700314 sidx + bdata->node_min_pfn,
315 eidx + bdata->node_min_pfn,
Johannes Weinerd747fa42008-07-23 21:28:05 -0700316 flags);
317
Chris Metcalf1b4ace42015-09-08 15:02:12 -0700318 if (WARN_ON(bdata->node_bootmem_map == NULL))
319 return 0;
320
Johannes Weinerd747fa42008-07-23 21:28:05 -0700321 for (idx = sidx; idx < eidx; idx++)
322 if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
323 if (exclusive) {
324 __free(bdata, sidx, idx);
325 return -EBUSY;
326 }
327 bdebug("silent double reserve of PFN %lx\n",
Johannes Weiner3560e242008-07-23 21:28:09 -0700328 idx + bdata->node_min_pfn);
Johannes Weinerd747fa42008-07-23 21:28:05 -0700329 }
330 return 0;
331}
332
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700333static int __init mark_bootmem_node(bootmem_data_t *bdata,
334 unsigned long start, unsigned long end,
335 int reserve, int flags)
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700336{
337 unsigned long sidx, eidx;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700338
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700339 bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
340 bdata - bootmem_node_data, start, end, reserve, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700341
Johannes Weiner3560e242008-07-23 21:28:09 -0700342 BUG_ON(start < bdata->node_min_pfn);
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700343 BUG_ON(end > bdata->node_low_pfn);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700344
Johannes Weiner3560e242008-07-23 21:28:09 -0700345 sidx = start - bdata->node_min_pfn;
346 eidx = end - bdata->node_min_pfn;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700347
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700348 if (reserve)
349 return __reserve(bdata, sidx, eidx, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700350 else
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700351 __free(bdata, sidx, eidx);
352 return 0;
353}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700354
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700355static int __init mark_bootmem(unsigned long start, unsigned long end,
356 int reserve, int flags)
357{
358 unsigned long pos;
359 bootmem_data_t *bdata;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700360
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700361 pos = start;
362 list_for_each_entry(bdata, &bdata_list, list) {
363 int err;
364 unsigned long max;
365
Johannes Weiner3560e242008-07-23 21:28:09 -0700366 if (pos < bdata->node_min_pfn ||
367 pos >= bdata->node_low_pfn) {
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700368 BUG_ON(pos != start);
369 continue;
370 }
371
372 max = min(bdata->node_low_pfn, end);
373
374 err = mark_bootmem_node(bdata, pos, max, reserve, flags);
375 if (reserve && err) {
376 mark_bootmem(start, pos, 0, 0);
377 return err;
378 }
379
380 if (max == end)
381 return 0;
382 pos = bdata->node_low_pfn;
383 }
384 BUG();
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700385}
386
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700387/**
388 * free_bootmem_node - mark a page range as usable
389 * @pgdat: node the range resides on
390 * @physaddr: starting address of the range
391 * @size: size of the range in bytes
392 *
393 * Partial pages will be considered reserved and left as they are.
394 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700395 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700396 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700397void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
398 unsigned long size)
399{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700400 unsigned long start, end;
401
Catalin Marinas9099dae2016-10-11 13:55:11 -0700402 kmemleak_free_part_phys(physaddr, size);
Catalin Marinasec3a3542009-07-07 10:33:01 +0100403
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700404 start = PFN_UP(physaddr);
405 end = PFN_DOWN(physaddr + size);
406
407 mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700408}
409
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700410/**
411 * free_bootmem - mark a page range as usable
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800412 * @addr: starting physical address of the range
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700413 * @size: size of the range in bytes
414 *
415 * Partial pages will be considered reserved and left as they are.
416 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700417 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700418 */
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800419void __init free_bootmem(unsigned long physaddr, unsigned long size)
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700420{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700421 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700422
Catalin Marinas9099dae2016-10-11 13:55:11 -0700423 kmemleak_free_part_phys(physaddr, size);
Catalin Marinasec3a3542009-07-07 10:33:01 +0100424
Joonsoo Kim81df9bf2012-12-11 16:03:10 -0800425 start = PFN_UP(physaddr);
426 end = PFN_DOWN(physaddr + size);
Yinghai Lua5645a62008-03-18 12:49:12 -0700427
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700428 mark_bootmem(start, end, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700431/**
432 * reserve_bootmem_node - mark a page range as reserved
433 * @pgdat: node the range resides on
434 * @physaddr: starting address of the range
435 * @size: size of the range in bytes
436 * @flags: reservation flags (see linux/bootmem.h)
437 *
438 * Partial pages will be reserved.
439 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700440 * The range must reside completely on the specified node.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700441 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700442int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
443 unsigned long size, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700445 unsigned long start, end;
Franck Bui-Huubbc7b922006-09-25 23:31:07 -0700446
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700447 start = PFN_DOWN(physaddr);
448 end = PFN_UP(physaddr + size);
449
450 return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700453/**
Javi Merino0d4ba4d2012-08-24 18:09:31 +0100454 * reserve_bootmem - mark a page range as reserved
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700455 * @addr: starting address of the range
456 * @size: size of the range in bytes
457 * @flags: reservation flags (see linux/bootmem.h)
458 *
459 * Partial pages will be reserved.
460 *
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700461 * The range must be contiguous but may span node boundaries.
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700462 */
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700463int __init reserve_bootmem(unsigned long addr, unsigned long size,
464 int flags)
465{
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700466 unsigned long start, end;
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700467
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700468 start = PFN_DOWN(addr);
469 end = PFN_UP(addr + size);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700470
Johannes Weinere2bf3ca2008-07-23 21:28:06 -0700471 return mark_bootmem(start, end, 1, flags);
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700472}
Johannes Weiner223e8dc2008-07-23 21:28:00 -0700473
Jan Beulich8aa043d2009-12-14 17:59:46 -0800474static unsigned long __init align_idx(struct bootmem_data *bdata,
475 unsigned long idx, unsigned long step)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700476{
477 unsigned long base = bdata->node_min_pfn;
478
479 /*
480 * Align the index with respect to the node start so that the
481 * combination of both satisfies the requested alignment.
482 */
483
484 return ALIGN(base + idx, step) - base;
485}
486
Jan Beulich8aa043d2009-12-14 17:59:46 -0800487static unsigned long __init align_off(struct bootmem_data *bdata,
488 unsigned long off, unsigned long align)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700489{
490 unsigned long base = PFN_PHYS(bdata->node_min_pfn);
491
492 /* Same as align_idx for byte offsets */
493
494 return ALIGN(base + off, align) - base;
495}
496
Johannes Weinerc6785b62012-05-29 15:06:33 -0700497static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata,
Tejun Heod0c4f572009-03-01 16:06:56 +0900498 unsigned long size, unsigned long align,
499 unsigned long goal, unsigned long limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700501 unsigned long fallback = 0;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700502 unsigned long min, max, start, sidx, midx, step;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
Johannes Weiner594fe1a2009-01-06 14:40:32 -0800504 bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
505 bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
506 align, goal, limit);
507
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700508 BUG_ON(!size);
509 BUG_ON(align & (align - 1));
510 BUG_ON(limit && goal + size > limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Christian Krafft7c309a62006-12-06 20:32:41 -0800512 if (!bdata->node_bootmem_map)
513 return NULL;
514
Johannes Weiner3560e242008-07-23 21:28:09 -0700515 min = bdata->node_min_pfn;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700516 max = bdata->node_low_pfn;
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700517
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700518 goal >>= PAGE_SHIFT;
519 limit >>= PAGE_SHIFT;
520
521 if (limit && max > limit)
522 max = limit;
523 if (max <= min)
Yinghai Lu9a2dc042008-03-18 12:44:48 -0700524 return NULL;
525
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700526 step = max(align >> PAGE_SHIFT, 1UL);
Yasunori Goto281dd252005-10-19 15:52:18 -0700527
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700528 if (goal && min < goal && goal < max)
529 start = ALIGN(goal, step);
530 else
531 start = ALIGN(min, step);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
Johannes Weiner481ebd02008-08-20 14:09:15 -0700533 sidx = start - bdata->node_min_pfn;
Johannes Weiner3560e242008-07-23 21:28:09 -0700534 midx = max - bdata->node_min_pfn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700536 if (bdata->hint_idx > sidx) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700537 /*
538 * Handle the valid case of sidx being zero and still
539 * catch the fallback below.
540 */
541 fallback = sidx + 1;
Johannes Weiner481ebd02008-08-20 14:09:15 -0700542 sidx = align_idx(bdata, bdata->hint_idx, step);
Yinghai Luad093152008-03-10 23:23:42 -0700543 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700545 while (1) {
546 int merge;
547 void *region;
548 unsigned long eidx, i, start_off, end_off;
549find_block:
550 sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
Johannes Weiner481ebd02008-08-20 14:09:15 -0700551 sidx = align_idx(bdata, sidx, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700552 eidx = sidx + PFN_UP(size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700554 if (sidx >= midx || eidx > midx)
Haren Myneni66d43e92005-12-12 00:37:39 -0800555 break;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700556
557 for (i = sidx; i < eidx; i++)
558 if (test_bit(i, bdata->node_bootmem_map)) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700559 sidx = align_idx(bdata, i, step);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700560 if (sidx == i)
561 sidx += step;
562 goto find_block;
563 }
564
Mikulas Patocka627240a2008-08-15 00:40:17 -0700565 if (bdata->last_end_off & (PAGE_SIZE - 1) &&
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700566 PFN_DOWN(bdata->last_end_off) + 1 == sidx)
Johannes Weiner481ebd02008-08-20 14:09:15 -0700567 start_off = align_off(bdata, bdata->last_end_off, align);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700568 else
569 start_off = PFN_PHYS(sidx);
570
571 merge = PFN_DOWN(start_off) < sidx;
572 end_off = start_off + size;
573
574 bdata->last_end_off = end_off;
575 bdata->hint_idx = PFN_UP(end_off);
576
577 /*
578 * Reserve the area now:
579 */
Johannes Weinerd747fa42008-07-23 21:28:05 -0700580 if (__reserve(bdata, PFN_DOWN(start_off) + merge,
581 PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
582 BUG();
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700583
Johannes Weiner3560e242008-07-23 21:28:09 -0700584 region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
585 start_off);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700586 memset(region, 0, size);
Catalin Marinas008139d2009-08-27 14:29:17 +0100587 /*
588 * The min_count is set to 0 so that bootmem allocated blocks
589 * are never reported as leaks.
590 */
591 kmemleak_alloc(region, size, 0, 0);
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700592 return region;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 }
594
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700595 if (fallback) {
Johannes Weiner481ebd02008-08-20 14:09:15 -0700596 sidx = align_idx(bdata, fallback - 1, step);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700597 fallback = 0;
598 goto find_block;
599 }
600
601 return NULL;
602}
603
Johannes Weinerc12ab502012-05-29 15:06:33 -0700604static void * __init alloc_bootmem_core(unsigned long size,
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700605 unsigned long align,
606 unsigned long goal,
607 unsigned long limit)
608{
609 bootmem_data_t *bdata;
Tejun Heod0c4f572009-03-01 16:06:56 +0900610 void *region;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700611
Joonsoo Kim3f7dfe22012-12-12 13:50:43 -0800612 if (WARN_ON_ONCE(slab_is_available()))
613 return kzalloc(size, GFP_NOWAIT);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700614
Tejun Heod0c4f572009-03-01 16:06:56 +0900615 list_for_each_entry(bdata, &bdata_list, list) {
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700616 if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
617 continue;
Johannes Weiner3560e242008-07-23 21:28:09 -0700618 if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700619 break;
620
Johannes Weinerc6785b62012-05-29 15:06:33 -0700621 region = alloc_bootmem_bdata(bdata, size, align, goal, limit);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700622 if (region)
623 return region;
624 }
625
Johannes Weinerc12ab502012-05-29 15:06:33 -0700626 return NULL;
627}
628
629static void * __init ___alloc_bootmem_nopanic(unsigned long size,
630 unsigned long align,
631 unsigned long goal,
632 unsigned long limit)
633{
634 void *ptr;
635
636restart:
637 ptr = alloc_bootmem_core(size, align, goal, limit);
638 if (ptr)
639 return ptr;
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700640 if (goal) {
641 goal = 0;
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700642 goto restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 }
Johannes Weiner5f2809e2008-07-23 21:28:05 -0700644
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700648/**
649 * __alloc_bootmem_nopanic - allocate boot memory without panicking
650 * @size: size of the request in bytes
651 * @align: alignment of the region
652 * @goal: preferred starting address of the region
653 *
654 * The goal is dropped if it can not be satisfied and the allocation will
655 * fall back to memory below @goal.
656 *
657 * Allocation may happen on any node in the system.
658 *
659 * Returns NULL on failure.
660 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700661void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700662 unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
Yinghai Lu08677212010-02-10 01:20:20 -0800664 unsigned long limit = 0;
665
Yinghai Lu08677212010-02-10 01:20:20 -0800666 return ___alloc_bootmem_nopanic(size, align, goal, limit);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700667}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700669static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
670 unsigned long goal, unsigned long limit)
671{
672 void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
673
674 if (mem)
675 return mem;
676 /*
677 * Whoops, we cannot satisfy the allocation request.
678 */
Joe Perches11705322016-03-17 14:19:50 -0700679 pr_alert("bootmem alloc of %lu bytes failed!\n", size);
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700680 panic("Out of memory");
Andi Kleena8062232006-04-07 19:49:21 +0200681 return NULL;
682}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700684/**
685 * __alloc_bootmem - allocate boot memory
686 * @size: size of the request in bytes
687 * @align: alignment of the region
688 * @goal: preferred starting address of the region
689 *
690 * The goal is dropped if it can not be satisfied and the allocation will
691 * fall back to memory below @goal.
692 *
693 * Allocation may happen on any node in the system.
694 *
695 * The function panics if the request can not be satisfied.
696 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700697void * __init __alloc_bootmem(unsigned long size, unsigned long align,
698 unsigned long goal)
Andi Kleena8062232006-04-07 19:49:21 +0200699{
Yinghai Lu08677212010-02-10 01:20:20 -0800700 unsigned long limit = 0;
701
Yinghai Lu08677212010-02-10 01:20:20 -0800702 return ___alloc_bootmem(size, align, goal, limit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703}
704
Yinghai Lu99ab7b12012-07-11 14:02:53 -0700705void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700706 unsigned long size, unsigned long align,
707 unsigned long goal, unsigned long limit)
708{
709 void *ptr;
710
Joonsoo Kim3f7dfe22012-12-12 13:50:43 -0800711 if (WARN_ON_ONCE(slab_is_available()))
zijun_hu1d8bf922016-10-07 16:59:27 -0700712 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
Johannes Weinerab381842012-05-29 15:06:34 -0700713again:
Tejun Heod0c4f572009-03-01 16:06:56 +0900714
Yinghai Luc8f4a2d2012-07-17 15:47:51 -0700715 /* do not panic in alloc_bootmem_bdata() */
716 if (limit && goal + size > limit)
717 limit = 0;
718
Johannes Weinere9079912012-05-29 15:06:36 -0700719 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit);
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700720 if (ptr)
721 return ptr;
722
Johannes Weinerab381842012-05-29 15:06:34 -0700723 ptr = alloc_bootmem_core(size, align, goal, limit);
724 if (ptr)
725 return ptr;
726
727 if (goal) {
728 goal = 0;
729 goto again;
730 }
731
Johannes Weiner421456e2012-05-29 15:06:34 -0700732 return NULL;
733}
734
735void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
736 unsigned long align, unsigned long goal)
737{
Johannes Weinere9079912012-05-29 15:06:36 -0700738 return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
Johannes Weiner421456e2012-05-29 15:06:34 -0700739}
740
Johannes Weinere9079912012-05-29 15:06:36 -0700741void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
Johannes Weiner421456e2012-05-29 15:06:34 -0700742 unsigned long align, unsigned long goal,
743 unsigned long limit)
744{
745 void *ptr;
746
Johannes Weinere9079912012-05-29 15:06:36 -0700747 ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
Johannes Weiner421456e2012-05-29 15:06:34 -0700748 if (ptr)
749 return ptr;
750
Joe Perches11705322016-03-17 14:19:50 -0700751 pr_alert("bootmem alloc of %lu bytes failed!\n", size);
Johannes Weinerab381842012-05-29 15:06:34 -0700752 panic("Out of memory");
753 return NULL;
Johannes Weiner4cc278b2008-07-23 21:28:08 -0700754}
755
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700756/**
757 * __alloc_bootmem_node - allocate boot memory from a specific node
758 * @pgdat: node to allocate from
759 * @size: size of the request in bytes
760 * @align: alignment of the region
761 * @goal: preferred starting address of the region
762 *
763 * The goal is dropped if it can not be satisfied and the allocation will
764 * fall back to memory below @goal.
765 *
766 * Allocation may fall back to any node in the system if the specified node
767 * can not hold the requested memory.
768 *
769 * The function panics if the request can not be satisfied.
770 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700771void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
772 unsigned long align, unsigned long goal)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
Pekka Enbergc91c4772009-06-11 08:10:28 +0300774 if (WARN_ON_ONCE(slab_is_available()))
775 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
776
Johannes Weinere9079912012-05-29 15:06:36 -0700777 return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800778}
779
780void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
781 unsigned long align, unsigned long goal)
782{
783#ifdef MAX_DMA32_PFN
784 unsigned long end_pfn;
785
786 if (WARN_ON_ONCE(slab_is_available()))
787 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
788
789 /* update goal according ...MAX_DMA32_PFN */
Xishi Qiu83285c72013-11-12 15:07:19 -0800790 end_pfn = pgdat_end_pfn(pgdat);
Yinghai Lu08677212010-02-10 01:20:20 -0800791
792 if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
793 (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
794 void *ptr;
795 unsigned long new_goal;
796
797 new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
Johannes Weinerc6785b62012-05-29 15:06:33 -0700798 ptr = alloc_bootmem_bdata(pgdat->bdata, size, align,
Yinghai Lu08677212010-02-10 01:20:20 -0800799 new_goal, 0);
Yinghai Lu08677212010-02-10 01:20:20 -0800800 if (ptr)
801 return ptr;
802 }
803#endif
804
805 return __alloc_bootmem_node(pgdat, size, align, goal);
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
808
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700809/**
810 * __alloc_bootmem_low - allocate low boot memory
811 * @size: size of the request in bytes
812 * @align: alignment of the region
813 * @goal: preferred starting address of the region
814 *
815 * The goal is dropped if it can not be satisfied and the allocation will
816 * fall back to memory below @goal.
817 *
818 * Allocation may happen on any node in the system.
819 *
820 * The function panics if the request can not be satisfied.
821 */
Franck Bui-Huubb0923a2006-09-25 23:31:05 -0700822void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
823 unsigned long goal)
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800824{
Johannes Weiner0f3caba2008-07-23 21:28:07 -0700825 return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800826}
827
Yinghai Lu38fa4172013-01-24 12:20:15 -0800828void * __init __alloc_bootmem_low_nopanic(unsigned long size,
829 unsigned long align,
830 unsigned long goal)
831{
832 return ___alloc_bootmem_nopanic(size, align, goal,
833 ARCH_LOW_ADDRESS_LIMIT);
834}
835
Johannes Weinera66fd7d2008-07-23 21:28:01 -0700836/**
837 * __alloc_bootmem_low_node - allocate low boot memory from a specific node
838 * @pgdat: node to allocate from
839 * @size: size of the request in bytes
840 * @align: alignment of the region
841 * @goal: preferred starting address of the region
842 *
843 * The goal is dropped if it can not be satisfied and the allocation will
844 * fall back to memory below @goal.
845 *
846 * Allocation may fall back to any node in the system if the specified node
847 * can not hold the requested memory.
848 *
849 * The function panics if the request can not be satisfied.
850 */
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800851void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
852 unsigned long align, unsigned long goal)
853{
Pekka Enbergc91c4772009-06-11 08:10:28 +0300854 if (WARN_ON_ONCE(slab_is_available()))
855 return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
856
Johannes Weinere9079912012-05-29 15:06:36 -0700857 return ___alloc_bootmem_node(pgdat, size, align,
858 goal, ARCH_LOW_ADDRESS_LIMIT);
Ravikiran G Thirumalai008857c2006-01-06 00:11:01 -0800859}