bootmem.c 21 KB

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