bootmem.c 21 KB

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