bootmem.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869
  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. bdata->node_bootmem_map = NULL;
  203. bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
  204. return count;
  205. }
  206. static int reset_managed_pages_done __initdata;
  207. void reset_node_managed_pages(pg_data_t *pgdat)
  208. {
  209. struct zone *z;
  210. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  211. z->managed_pages = 0;
  212. }
  213. void __init reset_all_zones_managed_pages(void)
  214. {
  215. struct pglist_data *pgdat;
  216. if (reset_managed_pages_done)
  217. return;
  218. for_each_online_pgdat(pgdat)
  219. reset_node_managed_pages(pgdat);
  220. reset_managed_pages_done = 1;
  221. }
  222. /**
  223. * free_all_bootmem - release free pages to the buddy allocator
  224. *
  225. * Returns the number of pages actually released.
  226. */
  227. unsigned long __init free_all_bootmem(void)
  228. {
  229. unsigned long total_pages = 0;
  230. bootmem_data_t *bdata;
  231. reset_all_zones_managed_pages();
  232. list_for_each_entry(bdata, &bdata_list, list)
  233. total_pages += free_all_bootmem_core(bdata);
  234. totalram_pages += total_pages;
  235. return total_pages;
  236. }
  237. static void __init __free(bootmem_data_t *bdata,
  238. unsigned long sidx, unsigned long eidx)
  239. {
  240. unsigned long idx;
  241. bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
  242. sidx + bdata->node_min_pfn,
  243. eidx + bdata->node_min_pfn);
  244. if (WARN_ON(bdata->node_bootmem_map == NULL))
  245. return;
  246. if (bdata->hint_idx > sidx)
  247. bdata->hint_idx = sidx;
  248. for (idx = sidx; idx < eidx; idx++)
  249. if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
  250. BUG();
  251. }
  252. static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
  253. unsigned long eidx, int flags)
  254. {
  255. unsigned long idx;
  256. int exclusive = flags & BOOTMEM_EXCLUSIVE;
  257. bdebug("nid=%td start=%lx end=%lx flags=%x\n",
  258. bdata - bootmem_node_data,
  259. sidx + bdata->node_min_pfn,
  260. eidx + bdata->node_min_pfn,
  261. flags);
  262. if (WARN_ON(bdata->node_bootmem_map == NULL))
  263. return 0;
  264. for (idx = sidx; idx < eidx; idx++)
  265. if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
  266. if (exclusive) {
  267. __free(bdata, sidx, idx);
  268. return -EBUSY;
  269. }
  270. bdebug("silent double reserve of PFN %lx\n",
  271. idx + bdata->node_min_pfn);
  272. }
  273. return 0;
  274. }
  275. static int __init mark_bootmem_node(bootmem_data_t *bdata,
  276. unsigned long start, unsigned long end,
  277. int reserve, int flags)
  278. {
  279. unsigned long sidx, eidx;
  280. bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
  281. bdata - bootmem_node_data, start, end, reserve, flags);
  282. BUG_ON(start < bdata->node_min_pfn);
  283. BUG_ON(end > bdata->node_low_pfn);
  284. sidx = start - bdata->node_min_pfn;
  285. eidx = end - bdata->node_min_pfn;
  286. if (reserve)
  287. return __reserve(bdata, sidx, eidx, flags);
  288. else
  289. __free(bdata, sidx, eidx);
  290. return 0;
  291. }
  292. static int __init mark_bootmem(unsigned long start, unsigned long end,
  293. int reserve, int flags)
  294. {
  295. unsigned long pos;
  296. bootmem_data_t *bdata;
  297. pos = start;
  298. list_for_each_entry(bdata, &bdata_list, list) {
  299. int err;
  300. unsigned long max;
  301. if (pos < bdata->node_min_pfn ||
  302. pos >= bdata->node_low_pfn) {
  303. BUG_ON(pos != start);
  304. continue;
  305. }
  306. max = min(bdata->node_low_pfn, end);
  307. err = mark_bootmem_node(bdata, pos, max, reserve, flags);
  308. if (reserve && err) {
  309. mark_bootmem(start, pos, 0, 0);
  310. return err;
  311. }
  312. if (max == end)
  313. return 0;
  314. pos = bdata->node_low_pfn;
  315. }
  316. BUG();
  317. }
  318. /**
  319. * free_bootmem_node - mark a page range as usable
  320. * @pgdat: node the range resides on
  321. * @physaddr: starting address of the range
  322. * @size: size of the range in bytes
  323. *
  324. * Partial pages will be considered reserved and left as they are.
  325. *
  326. * The range must reside completely on the specified node.
  327. */
  328. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  329. unsigned long size)
  330. {
  331. unsigned long start, end;
  332. kmemleak_free_part(__va(physaddr), size);
  333. start = PFN_UP(physaddr);
  334. end = PFN_DOWN(physaddr + size);
  335. mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
  336. }
  337. /**
  338. * free_bootmem - mark a page range as usable
  339. * @addr: starting physical address of the range
  340. * @size: size of the range in bytes
  341. *
  342. * Partial pages will be considered reserved and left as they are.
  343. *
  344. * The range must be contiguous but may span node boundaries.
  345. */
  346. void __init free_bootmem(unsigned long physaddr, unsigned long size)
  347. {
  348. unsigned long start, end;
  349. kmemleak_free_part(__va(physaddr), size);
  350. start = PFN_UP(physaddr);
  351. end = PFN_DOWN(physaddr + size);
  352. mark_bootmem(start, end, 0, 0);
  353. }
  354. /**
  355. * reserve_bootmem_node - mark a page range as reserved
  356. * @pgdat: node the range resides on
  357. * @physaddr: starting address of the range
  358. * @size: size of the range in bytes
  359. * @flags: reservation flags (see linux/bootmem.h)
  360. *
  361. * Partial pages will be reserved.
  362. *
  363. * The range must reside completely on the specified node.
  364. */
  365. int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  366. unsigned long size, int flags)
  367. {
  368. unsigned long start, end;
  369. start = PFN_DOWN(physaddr);
  370. end = PFN_UP(physaddr + size);
  371. return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
  372. }
  373. /**
  374. * reserve_bootmem - mark a page range as reserved
  375. * @addr: starting address of the range
  376. * @size: size of the range in bytes
  377. * @flags: reservation flags (see linux/bootmem.h)
  378. *
  379. * Partial pages will be reserved.
  380. *
  381. * The range must be contiguous but may span node boundaries.
  382. */
  383. int __init reserve_bootmem(unsigned long addr, unsigned long size,
  384. int flags)
  385. {
  386. unsigned long start, end;
  387. start = PFN_DOWN(addr);
  388. end = PFN_UP(addr + size);
  389. return mark_bootmem(start, end, 1, flags);
  390. }
  391. static unsigned long __init align_idx(struct bootmem_data *bdata,
  392. unsigned long idx, unsigned long step)
  393. {
  394. unsigned long base = bdata->node_min_pfn;
  395. /*
  396. * Align the index with respect to the node start so that the
  397. * combination of both satisfies the requested alignment.
  398. */
  399. return ALIGN(base + idx, step) - base;
  400. }
  401. static unsigned long __init align_off(struct bootmem_data *bdata,
  402. unsigned long off, unsigned long align)
  403. {
  404. unsigned long base = PFN_PHYS(bdata->node_min_pfn);
  405. /* Same as align_idx for byte offsets */
  406. return ALIGN(base + off, align) - base;
  407. }
  408. static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata,
  409. unsigned long size, unsigned long align,
  410. unsigned long goal, unsigned long limit)
  411. {
  412. unsigned long fallback = 0;
  413. unsigned long min, max, start, sidx, midx, step;
  414. bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
  415. bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
  416. align, goal, limit);
  417. BUG_ON(!size);
  418. BUG_ON(align & (align - 1));
  419. BUG_ON(limit && goal + size > limit);
  420. if (!bdata->node_bootmem_map)
  421. return NULL;
  422. min = bdata->node_min_pfn;
  423. max = bdata->node_low_pfn;
  424. goal >>= PAGE_SHIFT;
  425. limit >>= PAGE_SHIFT;
  426. if (limit && max > limit)
  427. max = limit;
  428. if (max <= min)
  429. return NULL;
  430. step = max(align >> PAGE_SHIFT, 1UL);
  431. if (goal && min < goal && goal < max)
  432. start = ALIGN(goal, step);
  433. else
  434. start = ALIGN(min, step);
  435. sidx = start - bdata->node_min_pfn;
  436. midx = max - bdata->node_min_pfn;
  437. if (bdata->hint_idx > sidx) {
  438. /*
  439. * Handle the valid case of sidx being zero and still
  440. * catch the fallback below.
  441. */
  442. fallback = sidx + 1;
  443. sidx = align_idx(bdata, bdata->hint_idx, step);
  444. }
  445. while (1) {
  446. int merge;
  447. void *region;
  448. unsigned long eidx, i, start_off, end_off;
  449. find_block:
  450. sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
  451. sidx = align_idx(bdata, sidx, step);
  452. eidx = sidx + PFN_UP(size);
  453. if (sidx >= midx || eidx > midx)
  454. break;
  455. for (i = sidx; i < eidx; i++)
  456. if (test_bit(i, bdata->node_bootmem_map)) {
  457. sidx = align_idx(bdata, i, step);
  458. if (sidx == i)
  459. sidx += step;
  460. goto find_block;
  461. }
  462. if (bdata->last_end_off & (PAGE_SIZE - 1) &&
  463. PFN_DOWN(bdata->last_end_off) + 1 == sidx)
  464. start_off = align_off(bdata, bdata->last_end_off, align);
  465. else
  466. start_off = PFN_PHYS(sidx);
  467. merge = PFN_DOWN(start_off) < sidx;
  468. end_off = start_off + size;
  469. bdata->last_end_off = end_off;
  470. bdata->hint_idx = PFN_UP(end_off);
  471. /*
  472. * Reserve the area now:
  473. */
  474. if (__reserve(bdata, PFN_DOWN(start_off) + merge,
  475. PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
  476. BUG();
  477. region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
  478. start_off);
  479. memset(region, 0, size);
  480. /*
  481. * The min_count is set to 0 so that bootmem allocated blocks
  482. * are never reported as leaks.
  483. */
  484. kmemleak_alloc(region, size, 0, 0);
  485. return region;
  486. }
  487. if (fallback) {
  488. sidx = align_idx(bdata, fallback - 1, step);
  489. fallback = 0;
  490. goto find_block;
  491. }
  492. return NULL;
  493. }
  494. static void * __init alloc_bootmem_core(unsigned long size,
  495. unsigned long align,
  496. unsigned long goal,
  497. unsigned long limit)
  498. {
  499. bootmem_data_t *bdata;
  500. void *region;
  501. if (WARN_ON_ONCE(slab_is_available()))
  502. return kzalloc(size, GFP_NOWAIT);
  503. list_for_each_entry(bdata, &bdata_list, list) {
  504. if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
  505. continue;
  506. if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
  507. break;
  508. region = alloc_bootmem_bdata(bdata, size, align, goal, limit);
  509. if (region)
  510. return region;
  511. }
  512. return NULL;
  513. }
  514. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  515. unsigned long align,
  516. unsigned long goal,
  517. unsigned long limit)
  518. {
  519. void *ptr;
  520. restart:
  521. ptr = alloc_bootmem_core(size, align, goal, limit);
  522. if (ptr)
  523. return ptr;
  524. if (goal) {
  525. goal = 0;
  526. goto restart;
  527. }
  528. return NULL;
  529. }
  530. /**
  531. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  532. * @size: size of the request in bytes
  533. * @align: alignment of the region
  534. * @goal: preferred starting address of the region
  535. *
  536. * The goal is dropped if it can not be satisfied and the allocation will
  537. * fall back to memory below @goal.
  538. *
  539. * Allocation may happen on any node in the system.
  540. *
  541. * Returns NULL on failure.
  542. */
  543. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  544. unsigned long goal)
  545. {
  546. unsigned long limit = 0;
  547. return ___alloc_bootmem_nopanic(size, align, goal, limit);
  548. }
  549. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  550. unsigned long goal, unsigned long limit)
  551. {
  552. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  553. if (mem)
  554. return mem;
  555. /*
  556. * Whoops, we cannot satisfy the allocation request.
  557. */
  558. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  559. panic("Out of memory");
  560. return NULL;
  561. }
  562. /**
  563. * __alloc_bootmem - allocate boot memory
  564. * @size: size of the request in bytes
  565. * @align: alignment of the region
  566. * @goal: preferred starting address of the region
  567. *
  568. * The goal is dropped if it can not be satisfied and the allocation will
  569. * fall back to memory below @goal.
  570. *
  571. * Allocation may happen on any node in the system.
  572. *
  573. * The function panics if the request can not be satisfied.
  574. */
  575. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  576. unsigned long goal)
  577. {
  578. unsigned long limit = 0;
  579. return ___alloc_bootmem(size, align, goal, limit);
  580. }
  581. void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
  582. unsigned long size, unsigned long align,
  583. unsigned long goal, unsigned long limit)
  584. {
  585. void *ptr;
  586. if (WARN_ON_ONCE(slab_is_available()))
  587. return kzalloc(size, GFP_NOWAIT);
  588. again:
  589. /* do not panic in alloc_bootmem_bdata() */
  590. if (limit && goal + size > limit)
  591. limit = 0;
  592. ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit);
  593. if (ptr)
  594. return ptr;
  595. ptr = alloc_bootmem_core(size, align, goal, limit);
  596. if (ptr)
  597. return ptr;
  598. if (goal) {
  599. goal = 0;
  600. goto again;
  601. }
  602. return NULL;
  603. }
  604. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  605. unsigned long align, unsigned long goal)
  606. {
  607. if (WARN_ON_ONCE(slab_is_available()))
  608. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  609. return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
  610. }
  611. void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  612. unsigned long align, unsigned long goal,
  613. unsigned long limit)
  614. {
  615. void *ptr;
  616. ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
  617. if (ptr)
  618. return ptr;
  619. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  620. panic("Out of memory");
  621. return NULL;
  622. }
  623. /**
  624. * __alloc_bootmem_node - allocate boot memory from a specific node
  625. * @pgdat: node to allocate from
  626. * @size: size of the request in bytes
  627. * @align: alignment of the region
  628. * @goal: preferred starting address of the region
  629. *
  630. * The goal is dropped if it can not be satisfied and the allocation will
  631. * fall back to memory below @goal.
  632. *
  633. * Allocation may fall back to any node in the system if the specified node
  634. * can not hold the requested memory.
  635. *
  636. * The function panics if the request can not be satisfied.
  637. */
  638. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  639. unsigned long align, unsigned long goal)
  640. {
  641. if (WARN_ON_ONCE(slab_is_available()))
  642. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  643. return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
  644. }
  645. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  646. unsigned long align, unsigned long goal)
  647. {
  648. #ifdef MAX_DMA32_PFN
  649. unsigned long end_pfn;
  650. if (WARN_ON_ONCE(slab_is_available()))
  651. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  652. /* update goal according ...MAX_DMA32_PFN */
  653. end_pfn = pgdat_end_pfn(pgdat);
  654. if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
  655. (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
  656. void *ptr;
  657. unsigned long new_goal;
  658. new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
  659. ptr = alloc_bootmem_bdata(pgdat->bdata, size, align,
  660. new_goal, 0);
  661. if (ptr)
  662. return ptr;
  663. }
  664. #endif
  665. return __alloc_bootmem_node(pgdat, size, align, goal);
  666. }
  667. #ifndef ARCH_LOW_ADDRESS_LIMIT
  668. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  669. #endif
  670. /**
  671. * __alloc_bootmem_low - allocate low boot memory
  672. * @size: size of the request in bytes
  673. * @align: alignment of the region
  674. * @goal: preferred starting address of the region
  675. *
  676. * The goal is dropped if it can not be satisfied and the allocation will
  677. * fall back to memory below @goal.
  678. *
  679. * Allocation may happen on any node in the system.
  680. *
  681. * The function panics if the request can not be satisfied.
  682. */
  683. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  684. unsigned long goal)
  685. {
  686. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  687. }
  688. void * __init __alloc_bootmem_low_nopanic(unsigned long size,
  689. unsigned long align,
  690. unsigned long goal)
  691. {
  692. return ___alloc_bootmem_nopanic(size, align, goal,
  693. ARCH_LOW_ADDRESS_LIMIT);
  694. }
  695. /**
  696. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  697. * @pgdat: node to allocate from
  698. * @size: size of the request in bytes
  699. * @align: alignment of the region
  700. * @goal: preferred starting address of the region
  701. *
  702. * The goal is dropped if it can not be satisfied and the allocation will
  703. * fall back to memory below @goal.
  704. *
  705. * Allocation may fall back to any node in the system if the specified node
  706. * can not hold the requested memory.
  707. *
  708. * The function panics if the request can not be satisfied.
  709. */
  710. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  711. unsigned long align, unsigned long goal)
  712. {
  713. if (WARN_ON_ONCE(slab_is_available()))
  714. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  715. return ___alloc_bootmem_node(pgdat, size, align,
  716. goal, ARCH_LOW_ADDRESS_LIMIT);
  717. }