bootmem.c 21 KB

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