bootmem.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853
  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 <asm/bug.h>
  20. #include <asm/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 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 free_bootmem_late(unsigned long addr, unsigned long size)
  134. {
  135. unsigned long cursor, end;
  136. kmemleak_free_part(__va(addr), size);
  137. cursor = PFN_UP(addr);
  138. end = PFN_DOWN(addr + size);
  139. for (; cursor < end; cursor++) {
  140. __free_pages_bootmem(pfn_to_page(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 start, end, pages, count = 0;
  148. if (!bdata->node_bootmem_map)
  149. return 0;
  150. start = bdata->node_min_pfn;
  151. end = bdata->node_low_pfn;
  152. bdebug("nid=%td start=%lx end=%lx\n",
  153. bdata - bootmem_node_data, start, end);
  154. while (start < end) {
  155. unsigned long *map, idx, vec;
  156. map = bdata->node_bootmem_map;
  157. idx = start - bdata->node_min_pfn;
  158. vec = ~map[idx / BITS_PER_LONG];
  159. /*
  160. * If we have a properly aligned and fully unreserved
  161. * BITS_PER_LONG block of pages in front of us, free
  162. * it in one go.
  163. */
  164. if (IS_ALIGNED(start, BITS_PER_LONG) && vec == ~0UL) {
  165. int order = ilog2(BITS_PER_LONG);
  166. __free_pages_bootmem(pfn_to_page(start), order);
  167. count += BITS_PER_LONG;
  168. start += BITS_PER_LONG;
  169. } else {
  170. unsigned long off = 0;
  171. vec >>= start & (BITS_PER_LONG - 1);
  172. while (vec) {
  173. if (vec & 1) {
  174. page = pfn_to_page(start + off);
  175. __free_pages_bootmem(page, 0);
  176. count++;
  177. }
  178. vec >>= 1;
  179. off++;
  180. }
  181. start = ALIGN(start + 1, BITS_PER_LONG);
  182. }
  183. }
  184. page = virt_to_page(bdata->node_bootmem_map);
  185. pages = bdata->node_low_pfn - bdata->node_min_pfn;
  186. pages = bootmem_bootmap_pages(pages);
  187. count += pages;
  188. while (pages--)
  189. __free_pages_bootmem(page++, 0);
  190. bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
  191. return count;
  192. }
  193. /**
  194. * free_all_bootmem_node - release a node's free pages to the buddy allocator
  195. * @pgdat: node to be released
  196. *
  197. * Returns the number of pages actually released.
  198. */
  199. unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
  200. {
  201. register_page_bootmem_info_node(pgdat);
  202. return free_all_bootmem_core(pgdat->bdata);
  203. }
  204. /**
  205. * free_all_bootmem - release free pages to the buddy allocator
  206. *
  207. * Returns the number of pages actually released.
  208. */
  209. unsigned long __init free_all_bootmem(void)
  210. {
  211. unsigned long total_pages = 0;
  212. bootmem_data_t *bdata;
  213. list_for_each_entry(bdata, &bdata_list, list)
  214. total_pages += free_all_bootmem_core(bdata);
  215. return total_pages;
  216. }
  217. static void __init __free(bootmem_data_t *bdata,
  218. unsigned long sidx, unsigned long eidx)
  219. {
  220. unsigned long idx;
  221. bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
  222. sidx + bdata->node_min_pfn,
  223. eidx + bdata->node_min_pfn);
  224. if (bdata->hint_idx > sidx)
  225. bdata->hint_idx = sidx;
  226. for (idx = sidx; idx < eidx; idx++)
  227. if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
  228. BUG();
  229. }
  230. static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
  231. unsigned long eidx, int flags)
  232. {
  233. unsigned long idx;
  234. int exclusive = flags & BOOTMEM_EXCLUSIVE;
  235. bdebug("nid=%td start=%lx end=%lx flags=%x\n",
  236. bdata - bootmem_node_data,
  237. sidx + bdata->node_min_pfn,
  238. eidx + bdata->node_min_pfn,
  239. flags);
  240. for (idx = sidx; idx < eidx; idx++)
  241. if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
  242. if (exclusive) {
  243. __free(bdata, sidx, idx);
  244. return -EBUSY;
  245. }
  246. bdebug("silent double reserve of PFN %lx\n",
  247. idx + bdata->node_min_pfn);
  248. }
  249. return 0;
  250. }
  251. static int __init mark_bootmem_node(bootmem_data_t *bdata,
  252. unsigned long start, unsigned long end,
  253. int reserve, int flags)
  254. {
  255. unsigned long sidx, eidx;
  256. bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
  257. bdata - bootmem_node_data, start, end, reserve, flags);
  258. BUG_ON(start < bdata->node_min_pfn);
  259. BUG_ON(end > bdata->node_low_pfn);
  260. sidx = start - bdata->node_min_pfn;
  261. eidx = end - bdata->node_min_pfn;
  262. if (reserve)
  263. return __reserve(bdata, sidx, eidx, flags);
  264. else
  265. __free(bdata, sidx, eidx);
  266. return 0;
  267. }
  268. static int __init mark_bootmem(unsigned long start, unsigned long end,
  269. int reserve, int flags)
  270. {
  271. unsigned long pos;
  272. bootmem_data_t *bdata;
  273. pos = start;
  274. list_for_each_entry(bdata, &bdata_list, list) {
  275. int err;
  276. unsigned long max;
  277. if (pos < bdata->node_min_pfn ||
  278. pos >= bdata->node_low_pfn) {
  279. BUG_ON(pos != start);
  280. continue;
  281. }
  282. max = min(bdata->node_low_pfn, end);
  283. err = mark_bootmem_node(bdata, pos, max, reserve, flags);
  284. if (reserve && err) {
  285. mark_bootmem(start, pos, 0, 0);
  286. return err;
  287. }
  288. if (max == end)
  289. return 0;
  290. pos = bdata->node_low_pfn;
  291. }
  292. BUG();
  293. }
  294. /**
  295. * free_bootmem_node - mark a page range as usable
  296. * @pgdat: node the range resides on
  297. * @physaddr: starting address of the range
  298. * @size: size of the range in bytes
  299. *
  300. * Partial pages will be considered reserved and left as they are.
  301. *
  302. * The range must reside completely on the specified node.
  303. */
  304. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  305. unsigned long size)
  306. {
  307. unsigned long start, end;
  308. kmemleak_free_part(__va(physaddr), size);
  309. start = PFN_UP(physaddr);
  310. end = PFN_DOWN(physaddr + size);
  311. mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
  312. }
  313. /**
  314. * free_bootmem - mark a page range as usable
  315. * @addr: starting address of the range
  316. * @size: size of the range in bytes
  317. *
  318. * Partial pages will be considered reserved and left as they are.
  319. *
  320. * The range must be contiguous but may span node boundaries.
  321. */
  322. void __init free_bootmem(unsigned long addr, unsigned long size)
  323. {
  324. unsigned long start, end;
  325. kmemleak_free_part(__va(addr), size);
  326. start = PFN_UP(addr);
  327. end = PFN_DOWN(addr + size);
  328. mark_bootmem(start, end, 0, 0);
  329. }
  330. /**
  331. * reserve_bootmem_node - mark a page range as reserved
  332. * @pgdat: node the range resides on
  333. * @physaddr: starting address of the range
  334. * @size: size of the range in bytes
  335. * @flags: reservation flags (see linux/bootmem.h)
  336. *
  337. * Partial pages will be reserved.
  338. *
  339. * The range must reside completely on the specified node.
  340. */
  341. int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  342. unsigned long size, int flags)
  343. {
  344. unsigned long start, end;
  345. start = PFN_DOWN(physaddr);
  346. end = PFN_UP(physaddr + size);
  347. return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
  348. }
  349. /**
  350. * reserve_bootmem - mark a page range as usable
  351. * @addr: starting address of the range
  352. * @size: size of the range in bytes
  353. * @flags: reservation flags (see linux/bootmem.h)
  354. *
  355. * Partial pages will be reserved.
  356. *
  357. * The range must be contiguous but may span node boundaries.
  358. */
  359. int __init reserve_bootmem(unsigned long addr, unsigned long size,
  360. int flags)
  361. {
  362. unsigned long start, end;
  363. start = PFN_DOWN(addr);
  364. end = PFN_UP(addr + size);
  365. return mark_bootmem(start, end, 1, flags);
  366. }
  367. int __weak __init reserve_bootmem_generic(unsigned long phys, unsigned long len,
  368. int flags)
  369. {
  370. return reserve_bootmem(phys, len, flags);
  371. }
  372. static unsigned long __init align_idx(struct bootmem_data *bdata,
  373. unsigned long idx, unsigned long step)
  374. {
  375. unsigned long base = bdata->node_min_pfn;
  376. /*
  377. * Align the index with respect to the node start so that the
  378. * combination of both satisfies the requested alignment.
  379. */
  380. return ALIGN(base + idx, step) - base;
  381. }
  382. static unsigned long __init align_off(struct bootmem_data *bdata,
  383. unsigned long off, unsigned long align)
  384. {
  385. unsigned long base = PFN_PHYS(bdata->node_min_pfn);
  386. /* Same as align_idx for byte offsets */
  387. return ALIGN(base + off, align) - base;
  388. }
  389. static void * __init alloc_bootmem_bdata(struct bootmem_data *bdata,
  390. unsigned long size, unsigned long align,
  391. unsigned long goal, unsigned long limit)
  392. {
  393. unsigned long fallback = 0;
  394. unsigned long min, max, start, sidx, midx, step;
  395. bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
  396. bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
  397. align, goal, limit);
  398. BUG_ON(!size);
  399. BUG_ON(align & (align - 1));
  400. BUG_ON(limit && goal + size > limit);
  401. if (!bdata->node_bootmem_map)
  402. return NULL;
  403. min = bdata->node_min_pfn;
  404. max = bdata->node_low_pfn;
  405. goal >>= PAGE_SHIFT;
  406. limit >>= PAGE_SHIFT;
  407. if (limit && max > limit)
  408. max = limit;
  409. if (max <= min)
  410. return NULL;
  411. step = max(align >> PAGE_SHIFT, 1UL);
  412. if (goal && min < goal && goal < max)
  413. start = ALIGN(goal, step);
  414. else
  415. start = ALIGN(min, step);
  416. sidx = start - bdata->node_min_pfn;
  417. midx = max - bdata->node_min_pfn;
  418. if (bdata->hint_idx > sidx) {
  419. /*
  420. * Handle the valid case of sidx being zero and still
  421. * catch the fallback below.
  422. */
  423. fallback = sidx + 1;
  424. sidx = align_idx(bdata, bdata->hint_idx, step);
  425. }
  426. while (1) {
  427. int merge;
  428. void *region;
  429. unsigned long eidx, i, start_off, end_off;
  430. find_block:
  431. sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
  432. sidx = align_idx(bdata, sidx, step);
  433. eidx = sidx + PFN_UP(size);
  434. if (sidx >= midx || eidx > midx)
  435. break;
  436. for (i = sidx; i < eidx; i++)
  437. if (test_bit(i, bdata->node_bootmem_map)) {
  438. sidx = align_idx(bdata, i, step);
  439. if (sidx == i)
  440. sidx += step;
  441. goto find_block;
  442. }
  443. if (bdata->last_end_off & (PAGE_SIZE - 1) &&
  444. PFN_DOWN(bdata->last_end_off) + 1 == sidx)
  445. start_off = align_off(bdata, bdata->last_end_off, align);
  446. else
  447. start_off = PFN_PHYS(sidx);
  448. merge = PFN_DOWN(start_off) < sidx;
  449. end_off = start_off + size;
  450. bdata->last_end_off = end_off;
  451. bdata->hint_idx = PFN_UP(end_off);
  452. /*
  453. * Reserve the area now:
  454. */
  455. if (__reserve(bdata, PFN_DOWN(start_off) + merge,
  456. PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
  457. BUG();
  458. region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
  459. start_off);
  460. memset(region, 0, size);
  461. /*
  462. * The min_count is set to 0 so that bootmem allocated blocks
  463. * are never reported as leaks.
  464. */
  465. kmemleak_alloc(region, size, 0, 0);
  466. return region;
  467. }
  468. if (fallback) {
  469. sidx = align_idx(bdata, fallback - 1, step);
  470. fallback = 0;
  471. goto find_block;
  472. }
  473. return NULL;
  474. }
  475. static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
  476. unsigned long size, unsigned long align,
  477. unsigned long goal, unsigned long limit)
  478. {
  479. if (WARN_ON_ONCE(slab_is_available()))
  480. return kzalloc(size, GFP_NOWAIT);
  481. #ifdef CONFIG_HAVE_ARCH_BOOTMEM
  482. {
  483. bootmem_data_t *p_bdata;
  484. p_bdata = bootmem_arch_preferred_node(bdata, size, align,
  485. goal, limit);
  486. if (p_bdata)
  487. return alloc_bootmem_bdata(p_bdata, size, align,
  488. goal, limit);
  489. }
  490. #endif
  491. return NULL;
  492. }
  493. static void * __init alloc_bootmem_core(unsigned long size,
  494. unsigned long align,
  495. unsigned long goal,
  496. unsigned long limit)
  497. {
  498. bootmem_data_t *bdata;
  499. void *region;
  500. region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
  501. if (region)
  502. return region;
  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. static 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. again:
  587. ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size,
  588. align, goal, limit);
  589. if (ptr)
  590. return ptr;
  591. ptr = alloc_bootmem_bdata(pgdat->bdata, size, align, goal, limit);
  592. if (ptr)
  593. return ptr;
  594. ptr = alloc_bootmem_core(size, align, goal, limit);
  595. if (ptr)
  596. return ptr;
  597. if (goal) {
  598. goal = 0;
  599. goto again;
  600. }
  601. return NULL;
  602. }
  603. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  604. unsigned long align, unsigned long goal)
  605. {
  606. if (WARN_ON_ONCE(slab_is_available()))
  607. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  608. return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
  609. }
  610. void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  611. unsigned long align, unsigned long goal,
  612. unsigned long limit)
  613. {
  614. void *ptr;
  615. ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
  616. if (ptr)
  617. return ptr;
  618. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  619. panic("Out of memory");
  620. return NULL;
  621. }
  622. /**
  623. * __alloc_bootmem_node - allocate boot memory from a specific node
  624. * @pgdat: node to allocate from
  625. * @size: size of the request in bytes
  626. * @align: alignment of the region
  627. * @goal: preferred starting address of the region
  628. *
  629. * The goal is dropped if it can not be satisfied and the allocation will
  630. * fall back to memory below @goal.
  631. *
  632. * Allocation may fall back to any node in the system if the specified node
  633. * can not hold the requested memory.
  634. *
  635. * The function panics if the request can not be satisfied.
  636. */
  637. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  638. unsigned long align, unsigned long goal)
  639. {
  640. if (WARN_ON_ONCE(slab_is_available()))
  641. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  642. return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
  643. }
  644. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  645. unsigned long align, unsigned long goal)
  646. {
  647. #ifdef MAX_DMA32_PFN
  648. unsigned long end_pfn;
  649. if (WARN_ON_ONCE(slab_is_available()))
  650. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  651. /* update goal according ...MAX_DMA32_PFN */
  652. end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  653. if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
  654. (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
  655. void *ptr;
  656. unsigned long new_goal;
  657. new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
  658. ptr = alloc_bootmem_bdata(pgdat->bdata, size, align,
  659. new_goal, 0);
  660. if (ptr)
  661. return ptr;
  662. }
  663. #endif
  664. return __alloc_bootmem_node(pgdat, size, align, goal);
  665. }
  666. #ifndef ARCH_LOW_ADDRESS_LIMIT
  667. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  668. #endif
  669. /**
  670. * __alloc_bootmem_low - allocate low boot memory
  671. * @size: size of the request in bytes
  672. * @align: alignment of the region
  673. * @goal: preferred starting address of the region
  674. *
  675. * The goal is dropped if it can not be satisfied and the allocation will
  676. * fall back to memory below @goal.
  677. *
  678. * Allocation may happen on any node in the system.
  679. *
  680. * The function panics if the request can not be satisfied.
  681. */
  682. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  683. unsigned long goal)
  684. {
  685. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  686. }
  687. /**
  688. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  689. * @pgdat: node to allocate from
  690. * @size: size of the request in bytes
  691. * @align: alignment of the region
  692. * @goal: preferred starting address of the region
  693. *
  694. * The goal is dropped if it can not be satisfied and the allocation will
  695. * fall back to memory below @goal.
  696. *
  697. * Allocation may fall back to any node in the system if the specified node
  698. * can not hold the requested memory.
  699. *
  700. * The function panics if the request can not be satisfied.
  701. */
  702. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  703. unsigned long align, unsigned long goal)
  704. {
  705. if (WARN_ON_ONCE(slab_is_available()))
  706. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  707. return ___alloc_bootmem_node(pgdat, size, align,
  708. goal, ARCH_LOW_ADDRESS_LIMIT);
  709. }