bootmem.c 21 KB

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