bootmem.c 21 KB

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