bootmem.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /*
  2. * bootmem - A boot-time physical memory allocator and configurator
  3. *
  4. * Copyright (C) 1999 Ingo Molnar
  5. * 1999 Kanoj Sarcar, SGI
  6. * 2008 Johannes Weiner
  7. *
  8. * Access to this subsystem has to be serialized externally (which is true
  9. * for the boot process anyway).
  10. */
  11. #include <linux/init.h>
  12. #include <linux/pfn.h>
  13. #include <linux/slab.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/export.h>
  16. #include <linux/kmemleak.h>
  17. #include <linux/range.h>
  18. #include <linux/memblock.h>
  19. #include <linux/bug.h>
  20. #include <linux/io.h>
  21. #include <asm/processor.h>
  22. #include "internal.h"
  23. #ifndef CONFIG_NEED_MULTIPLE_NODES
  24. struct pglist_data __refdata contig_page_data = {
  25. .bdata = &bootmem_node_data[0]
  26. };
  27. EXPORT_SYMBOL(contig_page_data);
  28. #endif
  29. unsigned long max_low_pfn;
  30. unsigned long min_low_pfn;
  31. unsigned long max_pfn;
  32. bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
  33. static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
  34. static int bootmem_debug;
  35. static int __init bootmem_debug_setup(char *buf)
  36. {
  37. bootmem_debug = 1;
  38. return 0;
  39. }
  40. early_param("bootmem_debug", bootmem_debug_setup);
  41. #define bdebug(fmt, args...) ({ \
  42. if (unlikely(bootmem_debug)) \
  43. printk(KERN_INFO \
  44. "bootmem::%s " fmt, \
  45. __func__, ## args); \
  46. })
  47. static unsigned long __init bootmap_bytes(unsigned long pages)
  48. {
  49. unsigned long bytes = DIV_ROUND_UP(pages, 8);
  50. return ALIGN(bytes, sizeof(long));
  51. }
  52. /**
  53. * bootmem_bootmap_pages - calculate bitmap size in pages
  54. * @pages: number of pages the bitmap has to represent
  55. */
  56. unsigned long __init bootmem_bootmap_pages(unsigned long pages)
  57. {
  58. unsigned long bytes = bootmap_bytes(pages);
  59. return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
  60. }
  61. /*
  62. * link bdata in order
  63. */
  64. static void __init link_bootmem(bootmem_data_t *bdata)
  65. {
  66. bootmem_data_t *ent;
  67. list_for_each_entry(ent, &bdata_list, list) {
  68. if (bdata->node_min_pfn < ent->node_min_pfn) {
  69. list_add_tail(&bdata->list, &ent->list);
  70. return;
  71. }
  72. }
  73. list_add_tail(&bdata->list, &bdata_list);
  74. }
  75. /*
  76. * Called once to set up the allocator itself.
  77. */
  78. static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
  79. unsigned long mapstart, unsigned long start, unsigned long end)
  80. {
  81. unsigned long mapsize;
  82. mminit_validate_memmodel_limits(&start, &end);
  83. bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
  84. bdata->node_min_pfn = start;
  85. bdata->node_low_pfn = end;
  86. link_bootmem(bdata);
  87. /*
  88. * Initially all pages are reserved - setup_arch() has to
  89. * register free RAM areas explicitly.
  90. */
  91. mapsize = bootmap_bytes(end - start);
  92. memset(bdata->node_bootmem_map, 0xff, mapsize);
  93. bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
  94. bdata - bootmem_node_data, start, mapstart, end, mapsize);
  95. return mapsize;
  96. }
  97. /**
  98. * init_bootmem_node - register a node as boot memory
  99. * @pgdat: node to register
  100. * @freepfn: pfn where the bitmap for this node is to be placed
  101. * @startpfn: first pfn on the node
  102. * @endpfn: first pfn after the node
  103. *
  104. * Returns the number of bytes needed to hold the bitmap for this node.
  105. */
  106. unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
  107. unsigned long startpfn, unsigned long endpfn)
  108. {
  109. return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
  110. }
  111. /**
  112. * init_bootmem - register boot memory
  113. * @start: pfn where the bitmap is to be placed
  114. * @pages: number of available physical pages
  115. *
  116. * Returns the number of bytes needed to hold the bitmap.
  117. */
  118. unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
  119. {
  120. max_low_pfn = pages;
  121. min_low_pfn = start;
  122. return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
  123. }
  124. /*
  125. * free_bootmem_late - free bootmem pages directly to page allocator
  126. * @addr: starting physical address of the range
  127. * @size: size of the range in bytes
  128. *
  129. * This is only useful when the bootmem allocator has already been torn
  130. * down, but we are still initializing the system. Pages are given directly
  131. * to the page allocator, no bootmem metadata is updated because it is gone.
  132. */
  133. void __init free_bootmem_late(unsigned long physaddr, unsigned long size)
  134. {
  135. unsigned long cursor, end;
  136. kmemleak_free_part(__va(physaddr), size);
  137. cursor = PFN_UP(physaddr);
  138. end = PFN_DOWN(physaddr + size);
  139. for (; cursor < end; cursor++) {
  140. __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
  141. totalram_pages++;
  142. }
  143. }
  144. static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
  145. {
  146. struct page *page;
  147. unsigned long *map, start, end, pages, cur, count = 0;
  148. if (!bdata->node_bootmem_map)
  149. return 0;
  150. map = bdata->node_bootmem_map;
  151. start = bdata->node_min_pfn;
  152. end = bdata->node_low_pfn;
  153. bdebug("nid=%td start=%lx end=%lx\n",
  154. bdata - bootmem_node_data, start, end);
  155. while (start < end) {
  156. unsigned long idx, vec;
  157. unsigned shift;
  158. idx = start - bdata->node_min_pfn;
  159. shift = idx & (BITS_PER_LONG - 1);
  160. /*
  161. * vec holds at most BITS_PER_LONG map bits,
  162. * bit 0 corresponds to start.
  163. */
  164. vec = ~map[idx / BITS_PER_LONG];
  165. if (shift) {
  166. vec >>= shift;
  167. if (end - start >= BITS_PER_LONG)
  168. vec |= ~map[idx / BITS_PER_LONG + 1] <<
  169. (BITS_PER_LONG - shift);
  170. }
  171. /*
  172. * If we have a properly aligned and fully unreserved
  173. * BITS_PER_LONG block of pages in front of us, free
  174. * it in one go.
  175. */
  176. if (IS_ALIGNED(start, BITS_PER_LONG) && vec == ~0UL) {
  177. int order = ilog2(BITS_PER_LONG);
  178. __free_pages_bootmem(pfn_to_page(start), start, order);
  179. count += BITS_PER_LONG;
  180. start += BITS_PER_LONG;
  181. } else {
  182. cur = start;
  183. start = ALIGN(start + 1, BITS_PER_LONG);
  184. while (vec && cur != start) {
  185. if (vec & 1) {
  186. page = pfn_to_page(cur);
  187. __free_pages_bootmem(page, cur, 0);
  188. count++;
  189. }
  190. vec >>= 1;
  191. ++cur;
  192. }
  193. }
  194. }
  195. cur = bdata->node_min_pfn;
  196. page = virt_to_page(bdata->node_bootmem_map);
  197. pages = bdata->node_low_pfn - bdata->node_min_pfn;
  198. pages = bootmem_bootmap_pages(pages);
  199. count += pages;
  200. while (pages--)
  201. __free_pages_bootmem(page++, cur++, 0);
  202. bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
  203. return count;
  204. }
  205. static int reset_managed_pages_done __initdata;
  206. void reset_node_managed_pages(pg_data_t *pgdat)
  207. {
  208. struct zone *z;
  209. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  210. z->managed_pages = 0;
  211. }
  212. void __init reset_all_zones_managed_pages(void)
  213. {
  214. struct pglist_data *pgdat;
  215. if (reset_managed_pages_done)
  216. return;
  217. for_each_online_pgdat(pgdat)
  218. reset_node_managed_pages(pgdat);
  219. reset_managed_pages_done = 1;
  220. }
  221. /**
  222. * free_all_bootmem - release free pages to the buddy allocator
  223. *
  224. * Returns the number of pages actually released.
  225. */
  226. unsigned long __init free_all_bootmem(void)
  227. {
  228. unsigned long total_pages = 0;
  229. bootmem_data_t *bdata;
  230. reset_all_zones_managed_pages();
  231. list_for_each_entry(bdata, &bdata_list, list)
  232. total_pages += free_all_bootmem_core(bdata);
  233. totalram_pages += total_pages;
  234. return total_pages;
  235. }
  236. static void __init __free(bootmem_data_t *bdata,
  237. unsigned long sidx, unsigned long eidx)
  238. {
  239. unsigned long idx;
  240. bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
  241. sidx + bdata->node_min_pfn,
  242. eidx + bdata->node_min_pfn);
  243. if (bdata->hint_idx > sidx)
  244. bdata->hint_idx = sidx;
  245. for (idx = sidx; idx < eidx; idx++)
  246. if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
  247. BUG();
  248. }
  249. static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
  250. unsigned long eidx, int flags)
  251. {
  252. unsigned long idx;
  253. int exclusive = flags & BOOTMEM_EXCLUSIVE;
  254. bdebug("nid=%td start=%lx end=%lx flags=%x\n",
  255. bdata - bootmem_node_data,
  256. sidx + bdata->node_min_pfn,
  257. eidx + bdata->node_min_pfn,
  258. flags);
  259. for (idx = sidx; idx < eidx; idx++)
  260. if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
  261. if (exclusive) {
  262. __free(bdata, sidx, idx);
  263. return -EBUSY;
  264. }
  265. bdebug("silent double reserve of PFN %lx\n",
  266. idx + bdata->node_min_pfn);
  267. }
  268. return 0;
  269. }
  270. static int __init mark_bootmem_node(bootmem_data_t *bdata,
  271. unsigned long start, unsigned long end,
  272. int reserve, int flags)
  273. {
  274. unsigned long sidx, eidx;
  275. bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
  276. bdata - bootmem_node_data, start, end, reserve, flags);
  277. BUG_ON(start < bdata->node_min_pfn);
  278. BUG_ON(end > bdata->node_low_pfn);
  279. sidx = start - bdata->node_min_pfn;
  280. eidx = end - bdata->node_min_pfn;
  281. if (reserve)
  282. return __reserve(bdata, sidx, eidx, flags);
  283. else
  284. __free(bdata, sidx, eidx);
  285. return 0;
  286. }
  287. static int __init mark_bootmem(unsigned long start, unsigned long end,
  288. int reserve, int flags)
  289. {
  290. unsigned long pos;
  291. bootmem_data_t *bdata;
  292. pos = start;
  293. list_for_each_entry(bdata, &bdata_list, list) {
  294. int err;
  295. unsigned long max;
  296. if (pos < bdata->node_min_pfn ||
  297. pos >= bdata->node_low_pfn) {
  298. BUG_ON(pos != start);
  299. continue;
  300. }
  301. max = min(bdata->node_low_pfn, end);
  302. err = mark_bootmem_node(bdata, pos, max, reserve, flags);
  303. if (reserve && err) {
  304. mark_bootmem(start, pos, 0, 0);
  305. return err;
  306. }
  307. if (max == end)
  308. return 0;
  309. pos = bdata->node_low_pfn;
  310. }
  311. BUG();
  312. }
  313. /**
  314. * free_bootmem_node - mark a page range as usable
  315. * @pgdat: node the range resides on
  316. * @physaddr: starting address of the range
  317. * @size: size of the range in bytes
  318. *
  319. * Partial pages will be considered reserved and left as they are.
  320. *
  321. * The range must reside completely on the specified node.
  322. */
  323. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  324. unsigned long size)
  325. {
  326. unsigned long start, end;
  327. kmemleak_free_part(__va(physaddr), size);
  328. start = PFN_UP(physaddr);
  329. end = PFN_DOWN(physaddr + size);
  330. mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
  331. }
  332. /**
  333. * free_bootmem - mark a page range as usable
  334. * @addr: starting physical address of the range
  335. * @size: size of the range in bytes
  336. *
  337. * Partial pages will be considered reserved and left as they are.
  338. *
  339. * The range must be contiguous but may span node boundaries.
  340. */
  341. void __init free_bootmem(unsigned long physaddr, unsigned long size)
  342. {
  343. unsigned long start, end;
  344. kmemleak_free_part(__va(physaddr), size);
  345. start = PFN_UP(physaddr);
  346. end = PFN_DOWN(physaddr + size);
  347. mark_bootmem(start, end, 0, 0);
  348. }
  349. /**
  350. * reserve_bootmem_node - mark a page range as reserved
  351. * @pgdat: node the range resides on
  352. * @physaddr: starting address of the range
  353. * @size: size of the range in bytes
  354. * @flags: reservation flags (see linux/bootmem.h)
  355. *
  356. * Partial pages will be reserved.
  357. *
  358. * The range must reside completely on the specified node.
  359. */
  360. int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  361. unsigned long size, int flags)
  362. {
  363. unsigned long start, end;
  364. start = PFN_DOWN(physaddr);
  365. end = PFN_UP(physaddr + size);
  366. return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
  367. }
  368. /**
  369. * reserve_bootmem - mark a page range as reserved
  370. * @addr: starting address of the range
  371. * @size: size of the range in bytes
  372. * @flags: reservation flags (see linux/bootmem.h)
  373. *
  374. * Partial pages will be reserved.
  375. *
  376. * The range must be contiguous but may span node boundaries.
  377. */
  378. int __init reserve_bootmem(unsigned long addr, unsigned long size,
  379. int flags)
  380. {
  381. unsigned long start, end;
  382. start = PFN_DOWN(addr);
  383. end = PFN_UP(addr + size);
  384. return mark_bootmem(start, end, 1, flags);
  385. }
  386. static unsigned long __init align_idx(struct bootmem_data *bdata,
  387. unsigned long idx, unsigned long step)
  388. {
  389. unsigned long base = bdata->node_min_pfn;
  390. /*
  391. * Align the index with respect to the node start so that the
  392. * combination of both satisfies the requested alignment.
  393. */
  394. return ALIGN(base + idx, step) - base;
  395. }
  396. static unsigned long __init align_off(struct bootmem_data *bdata,
  397. unsigned long off, unsigned long align)
  398. {
  399. unsigned long base = PFN_PHYS(bdata->node_min_pfn);
  400. /* Same as align_idx for byte offsets */
  401. return ALIGN(base + off, align) - base;
  402. }
  403. static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata,
  404. unsigned long size, unsigned long align,
  405. unsigned long goal, unsigned long limit)
  406. {
  407. unsigned long fallback = 0;
  408. unsigned long min, max, start, sidx, midx, step;
  409. bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
  410. bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
  411. align, goal, limit);
  412. BUG_ON(!size);
  413. BUG_ON(align & (align - 1));
  414. BUG_ON(limit && goal + size > limit);
  415. if (!bdata->node_bootmem_map)
  416. return NULL;
  417. min = bdata->node_min_pfn;
  418. max = bdata->node_low_pfn;
  419. goal >>= PAGE_SHIFT;
  420. limit >>= PAGE_SHIFT;
  421. if (limit && max > limit)
  422. max = limit;
  423. if (max <= min)
  424. return NULL;
  425. step = max(align >> PAGE_SHIFT, 1UL);
  426. if (goal && min < goal && goal < max)
  427. start = ALIGN(goal, step);
  428. else
  429. start = ALIGN(min, step);
  430. sidx = start - bdata->node_min_pfn;
  431. midx = max - bdata->node_min_pfn;
  432. if (bdata->hint_idx > sidx) {
  433. /*
  434. * Handle the valid case of sidx being zero and still
  435. * catch the fallback below.
  436. */
  437. fallback = sidx + 1;
  438. sidx = align_idx(bdata, bdata->hint_idx, step);
  439. }
  440. while (1) {
  441. int merge;
  442. void *region;
  443. unsigned long eidx, i, start_off, end_off;
  444. find_block:
  445. sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
  446. sidx = align_idx(bdata, sidx, step);
  447. eidx = sidx + PFN_UP(size);
  448. if (sidx >= midx || eidx > midx)
  449. break;
  450. for (i = sidx; i < eidx; i++)
  451. if (test_bit(i, bdata->node_bootmem_map)) {
  452. sidx = align_idx(bdata, i, step);
  453. if (sidx == i)
  454. sidx += step;
  455. goto find_block;
  456. }
  457. if (bdata->last_end_off & (PAGE_SIZE - 1) &&
  458. PFN_DOWN(bdata->last_end_off) + 1 == sidx)
  459. start_off = align_off(bdata, bdata->last_end_off, align);
  460. else
  461. start_off = PFN_PHYS(sidx);
  462. merge = PFN_DOWN(start_off) < sidx;
  463. end_off = start_off + size;
  464. bdata->last_end_off = end_off;
  465. bdata->hint_idx = PFN_UP(end_off);
  466. /*
  467. * Reserve the area now:
  468. */
  469. if (__reserve(bdata, PFN_DOWN(start_off) + merge,
  470. PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
  471. BUG();
  472. region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
  473. start_off);
  474. memset(region, 0, size);
  475. /*
  476. * The min_count is set to 0 so that bootmem allocated blocks
  477. * are never reported as leaks.
  478. */
  479. kmemleak_alloc(region, size, 0, 0);
  480. return region;
  481. }
  482. if (fallback) {
  483. sidx = align_idx(bdata, fallback - 1, step);
  484. fallback = 0;
  485. goto find_block;
  486. }
  487. return NULL;
  488. }
  489. static void * __init alloc_bootmem_core(unsigned long size,
  490. unsigned long align,
  491. unsigned long goal,
  492. unsigned long limit)
  493. {
  494. bootmem_data_t *bdata;
  495. void *region;
  496. if (WARN_ON_ONCE(slab_is_available()))
  497. return kzalloc(size, GFP_NOWAIT);
  498. list_for_each_entry(bdata, &bdata_list, list) {
  499. if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
  500. continue;
  501. if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
  502. break;
  503. region = alloc_bootmem_bdata(bdata, size, align, goal, limit);
  504. if (region)
  505. return region;
  506. }
  507. return NULL;
  508. }
  509. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  510. unsigned long align,
  511. unsigned long goal,
  512. unsigned long limit)
  513. {
  514. void *ptr;
  515. restart:
  516. ptr = alloc_bootmem_core(size, align, goal, limit);
  517. if (ptr)
  518. return ptr;
  519. if (goal) {
  520. goal = 0;
  521. goto restart;
  522. }
  523. return NULL;
  524. }
  525. /**
  526. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  527. * @size: size of the request in bytes
  528. * @align: alignment of the region
  529. * @goal: preferred starting address of the region
  530. *
  531. * The goal is dropped if it can not be satisfied and the allocation will
  532. * fall back to memory below @goal.
  533. *
  534. * Allocation may happen on any node in the system.
  535. *
  536. * Returns NULL on failure.
  537. */
  538. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  539. unsigned long goal)
  540. {
  541. unsigned long limit = 0;
  542. return ___alloc_bootmem_nopanic(size, align, goal, limit);
  543. }
  544. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  545. unsigned long goal, unsigned long limit)
  546. {
  547. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  548. if (mem)
  549. return mem;
  550. /*
  551. * Whoops, we cannot satisfy the allocation request.
  552. */
  553. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  554. panic("Out of memory");
  555. return NULL;
  556. }
  557. /**
  558. * __alloc_bootmem - allocate boot memory
  559. * @size: size of the request in bytes
  560. * @align: alignment of the region
  561. * @goal: preferred starting address of the region
  562. *
  563. * The goal is dropped if it can not be satisfied and the allocation will
  564. * fall back to memory below @goal.
  565. *
  566. * Allocation may happen on any node in the system.
  567. *
  568. * The function panics if the request can not be satisfied.
  569. */
  570. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  571. unsigned long goal)
  572. {
  573. unsigned long limit = 0;
  574. return ___alloc_bootmem(size, align, goal, limit);
  575. }
  576. void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
  577. unsigned long size, unsigned long align,
  578. unsigned long goal, unsigned long limit)
  579. {
  580. void *ptr;
  581. if (WARN_ON_ONCE(slab_is_available()))
  582. return kzalloc(size, GFP_NOWAIT);
  583. again:
  584. /* do not panic in alloc_bootmem_bdata() */
  585. if (limit && goal + size > limit)
  586. limit = 0;
  587. ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit);
  588. if (ptr)
  589. return ptr;
  590. ptr = alloc_bootmem_core(size, align, goal, limit);
  591. if (ptr)
  592. return ptr;
  593. if (goal) {
  594. goal = 0;
  595. goto again;
  596. }
  597. return NULL;
  598. }
  599. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  600. unsigned long align, unsigned long goal)
  601. {
  602. if (WARN_ON_ONCE(slab_is_available()))
  603. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  604. return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
  605. }
  606. void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  607. unsigned long align, unsigned long goal,
  608. unsigned long limit)
  609. {
  610. void *ptr;
  611. ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
  612. if (ptr)
  613. return ptr;
  614. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  615. panic("Out of memory");
  616. return NULL;
  617. }
  618. /**
  619. * __alloc_bootmem_node - allocate boot memory from a specific node
  620. * @pgdat: node to allocate from
  621. * @size: size of the request in bytes
  622. * @align: alignment of the region
  623. * @goal: preferred starting address of the region
  624. *
  625. * The goal is dropped if it can not be satisfied and the allocation will
  626. * fall back to memory below @goal.
  627. *
  628. * Allocation may fall back to any node in the system if the specified node
  629. * can not hold the requested memory.
  630. *
  631. * The function panics if the request can not be satisfied.
  632. */
  633. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  634. unsigned long align, unsigned long goal)
  635. {
  636. if (WARN_ON_ONCE(slab_is_available()))
  637. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  638. return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
  639. }
  640. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  641. unsigned long align, unsigned long goal)
  642. {
  643. #ifdef MAX_DMA32_PFN
  644. unsigned long end_pfn;
  645. if (WARN_ON_ONCE(slab_is_available()))
  646. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  647. /* update goal according ...MAX_DMA32_PFN */
  648. end_pfn = pgdat_end_pfn(pgdat);
  649. if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
  650. (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
  651. void *ptr;
  652. unsigned long new_goal;
  653. new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
  654. ptr = alloc_bootmem_bdata(pgdat->bdata, size, align,
  655. new_goal, 0);
  656. if (ptr)
  657. return ptr;
  658. }
  659. #endif
  660. return __alloc_bootmem_node(pgdat, size, align, goal);
  661. }
  662. #ifndef ARCH_LOW_ADDRESS_LIMIT
  663. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  664. #endif
  665. /**
  666. * __alloc_bootmem_low - allocate low boot memory
  667. * @size: size of the request in bytes
  668. * @align: alignment of the region
  669. * @goal: preferred starting address of the region
  670. *
  671. * The goal is dropped if it can not be satisfied and the allocation will
  672. * fall back to memory below @goal.
  673. *
  674. * Allocation may happen on any node in the system.
  675. *
  676. * The function panics if the request can not be satisfied.
  677. */
  678. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  679. unsigned long goal)
  680. {
  681. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  682. }
  683. void * __init __alloc_bootmem_low_nopanic(unsigned long size,
  684. unsigned long align,
  685. unsigned long goal)
  686. {
  687. return ___alloc_bootmem_nopanic(size, align, goal,
  688. ARCH_LOW_ADDRESS_LIMIT);
  689. }
  690. /**
  691. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  692. * @pgdat: node to allocate from
  693. * @size: size of the request in bytes
  694. * @align: alignment of the region
  695. * @goal: preferred starting address of the region
  696. *
  697. * The goal is dropped if it can not be satisfied and the allocation will
  698. * fall back to memory below @goal.
  699. *
  700. * Allocation may fall back to any node in the system if the specified node
  701. * can not hold the requested memory.
  702. *
  703. * The function panics if the request can not be satisfied.
  704. */
  705. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  706. unsigned long align, unsigned long goal)
  707. {
  708. if (WARN_ON_ONCE(slab_is_available()))
  709. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  710. return ___alloc_bootmem_node(pgdat, size, align,
  711. goal, ARCH_LOW_ADDRESS_LIMIT);
  712. }