bootmem.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790
  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/bootmem.h>
  14. #include <linux/module.h>
  15. #include <asm/bug.h>
  16. #include <asm/io.h>
  17. #include <asm/processor.h>
  18. #include "internal.h"
  19. unsigned long max_low_pfn;
  20. unsigned long min_low_pfn;
  21. unsigned long max_pfn;
  22. #ifdef CONFIG_CRASH_DUMP
  23. /*
  24. * If we have booted due to a crash, max_pfn will be a very low value. We need
  25. * to know the amount of memory that the previous kernel used.
  26. */
  27. unsigned long saved_max_pfn;
  28. #endif
  29. bootmem_data_t bootmem_node_data[MAX_NUMNODES] __initdata;
  30. static struct list_head bdata_list __initdata = LIST_HEAD_INIT(bdata_list);
  31. static int bootmem_debug;
  32. static int __init bootmem_debug_setup(char *buf)
  33. {
  34. bootmem_debug = 1;
  35. return 0;
  36. }
  37. early_param("bootmem_debug", bootmem_debug_setup);
  38. #define bdebug(fmt, args...) ({ \
  39. if (unlikely(bootmem_debug)) \
  40. printk(KERN_INFO \
  41. "bootmem::%s " fmt, \
  42. __FUNCTION__, ## args); \
  43. })
  44. static unsigned long __init bootmap_bytes(unsigned long pages)
  45. {
  46. unsigned long bytes = (pages + 7) / 8;
  47. return ALIGN(bytes, sizeof(long));
  48. }
  49. /**
  50. * bootmem_bootmap_pages - calculate bitmap size in pages
  51. * @pages: number of pages the bitmap has to represent
  52. */
  53. unsigned long __init bootmem_bootmap_pages(unsigned long pages)
  54. {
  55. unsigned long bytes = bootmap_bytes(pages);
  56. return PAGE_ALIGN(bytes) >> PAGE_SHIFT;
  57. }
  58. /*
  59. * link bdata in order
  60. */
  61. static void __init link_bootmem(bootmem_data_t *bdata)
  62. {
  63. struct list_head *iter;
  64. list_for_each(iter, &bdata_list) {
  65. bootmem_data_t *ent;
  66. ent = list_entry(iter, bootmem_data_t, list);
  67. if (bdata->node_boot_start < ent->node_boot_start)
  68. break;
  69. }
  70. list_add_tail(&bdata->list, iter);
  71. }
  72. /*
  73. * Called once to set up the allocator itself.
  74. */
  75. static unsigned long __init init_bootmem_core(bootmem_data_t *bdata,
  76. unsigned long mapstart, unsigned long start, unsigned long end)
  77. {
  78. unsigned long mapsize;
  79. mminit_validate_memmodel_limits(&start, &end);
  80. bdata->node_bootmem_map = phys_to_virt(PFN_PHYS(mapstart));
  81. bdata->node_boot_start = PFN_PHYS(start);
  82. bdata->node_low_pfn = end;
  83. link_bootmem(bdata);
  84. /*
  85. * Initially all pages are reserved - setup_arch() has to
  86. * register free RAM areas explicitly.
  87. */
  88. mapsize = bootmap_bytes(end - start);
  89. memset(bdata->node_bootmem_map, 0xff, mapsize);
  90. bdebug("nid=%td start=%lx map=%lx end=%lx mapsize=%lx\n",
  91. bdata - bootmem_node_data, start, mapstart, end, mapsize);
  92. return mapsize;
  93. }
  94. /**
  95. * init_bootmem_node - register a node as boot memory
  96. * @pgdat: node to register
  97. * @freepfn: pfn where the bitmap for this node is to be placed
  98. * @startpfn: first pfn on the node
  99. * @endpfn: first pfn after the node
  100. *
  101. * Returns the number of bytes needed to hold the bitmap for this node.
  102. */
  103. unsigned long __init init_bootmem_node(pg_data_t *pgdat, unsigned long freepfn,
  104. unsigned long startpfn, unsigned long endpfn)
  105. {
  106. return init_bootmem_core(pgdat->bdata, freepfn, startpfn, endpfn);
  107. }
  108. /**
  109. * init_bootmem - register boot memory
  110. * @start: pfn where the bitmap is to be placed
  111. * @pages: number of available physical pages
  112. *
  113. * Returns the number of bytes needed to hold the bitmap.
  114. */
  115. unsigned long __init init_bootmem(unsigned long start, unsigned long pages)
  116. {
  117. max_low_pfn = pages;
  118. min_low_pfn = start;
  119. return init_bootmem_core(NODE_DATA(0)->bdata, start, 0, pages);
  120. }
  121. static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
  122. {
  123. struct page *page;
  124. unsigned long pfn;
  125. unsigned long i, count;
  126. unsigned long idx, pages;
  127. unsigned long *map;
  128. int gofast = 0;
  129. BUG_ON(!bdata->node_bootmem_map);
  130. count = 0;
  131. /* first extant page of the node */
  132. pfn = PFN_DOWN(bdata->node_boot_start);
  133. idx = bdata->node_low_pfn - pfn;
  134. map = bdata->node_bootmem_map;
  135. /*
  136. * Check if we are aligned to BITS_PER_LONG pages. If so, we might
  137. * be able to free page orders of that size at once.
  138. */
  139. if (!(pfn & (BITS_PER_LONG-1)))
  140. gofast = 1;
  141. for (i = 0; i < idx; ) {
  142. unsigned long v = ~map[i / BITS_PER_LONG];
  143. if (gofast && v == ~0UL) {
  144. int order;
  145. page = pfn_to_page(pfn);
  146. count += BITS_PER_LONG;
  147. order = ffs(BITS_PER_LONG) - 1;
  148. __free_pages_bootmem(page, order);
  149. i += BITS_PER_LONG;
  150. page += BITS_PER_LONG;
  151. } else if (v) {
  152. unsigned long m;
  153. page = pfn_to_page(pfn);
  154. for (m = 1; m && i < idx; m<<=1, page++, i++) {
  155. if (v & m) {
  156. count++;
  157. __free_pages_bootmem(page, 0);
  158. }
  159. }
  160. } else {
  161. i += BITS_PER_LONG;
  162. }
  163. pfn += BITS_PER_LONG;
  164. }
  165. /*
  166. * Now free the allocator bitmap itself, it's not
  167. * needed anymore:
  168. */
  169. page = virt_to_page(bdata->node_bootmem_map);
  170. pages = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
  171. idx = bootmem_bootmap_pages(pages);
  172. for (i = 0; i < idx; i++, page++)
  173. __free_pages_bootmem(page, 0);
  174. count += i;
  175. bdata->node_bootmem_map = NULL;
  176. bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
  177. return count;
  178. }
  179. /**
  180. * free_all_bootmem_node - release a node's free pages to the buddy allocator
  181. * @pgdat: node to be released
  182. *
  183. * Returns the number of pages actually released.
  184. */
  185. unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
  186. {
  187. register_page_bootmem_info_node(pgdat);
  188. return free_all_bootmem_core(pgdat->bdata);
  189. }
  190. /**
  191. * free_all_bootmem - release free pages to the buddy allocator
  192. *
  193. * Returns the number of pages actually released.
  194. */
  195. unsigned long __init free_all_bootmem(void)
  196. {
  197. return free_all_bootmem_core(NODE_DATA(0)->bdata);
  198. }
  199. static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
  200. unsigned long size)
  201. {
  202. unsigned long sidx, eidx;
  203. unsigned long i;
  204. BUG_ON(!size);
  205. /* out range */
  206. if (addr + size < bdata->node_boot_start ||
  207. PFN_DOWN(addr) > bdata->node_low_pfn)
  208. return;
  209. /*
  210. * round down end of usable mem, partially free pages are
  211. * considered reserved.
  212. */
  213. if (addr >= bdata->node_boot_start && addr < bdata->last_success)
  214. bdata->last_success = addr;
  215. /*
  216. * Round up to index to the range.
  217. */
  218. if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
  219. sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
  220. else
  221. sidx = 0;
  222. eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
  223. if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
  224. eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
  225. bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
  226. sidx + PFN_DOWN(bdata->node_boot_start),
  227. eidx + PFN_DOWN(bdata->node_boot_start));
  228. for (i = sidx; i < eidx; i++) {
  229. if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
  230. BUG();
  231. }
  232. }
  233. /**
  234. * free_bootmem_node - mark a page range as usable
  235. * @pgdat: node the range resides on
  236. * @physaddr: starting address of the range
  237. * @size: size of the range in bytes
  238. *
  239. * Partial pages will be considered reserved and left as they are.
  240. *
  241. * Only physical pages that actually reside on @pgdat are marked.
  242. */
  243. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  244. unsigned long size)
  245. {
  246. free_bootmem_core(pgdat->bdata, physaddr, size);
  247. }
  248. /**
  249. * free_bootmem - mark a page range as usable
  250. * @addr: starting address of the range
  251. * @size: size of the range in bytes
  252. *
  253. * Partial pages will be considered reserved and left as they are.
  254. *
  255. * All physical pages within the range are marked, no matter what
  256. * node they reside on.
  257. */
  258. void __init free_bootmem(unsigned long addr, unsigned long size)
  259. {
  260. bootmem_data_t *bdata;
  261. list_for_each_entry(bdata, &bdata_list, list)
  262. free_bootmem_core(bdata, addr, size);
  263. }
  264. /*
  265. * Marks a particular physical memory range as unallocatable. Usable RAM
  266. * might be used for boot-time allocations - or it might get added
  267. * to the free page pool later on.
  268. */
  269. static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
  270. unsigned long addr, unsigned long size, int flags)
  271. {
  272. unsigned long sidx, eidx;
  273. unsigned long i;
  274. BUG_ON(!size);
  275. /* out of range, don't hold other */
  276. if (addr + size < bdata->node_boot_start ||
  277. PFN_DOWN(addr) > bdata->node_low_pfn)
  278. return 0;
  279. /*
  280. * Round up to index to the range.
  281. */
  282. if (addr > bdata->node_boot_start)
  283. sidx= PFN_DOWN(addr - bdata->node_boot_start);
  284. else
  285. sidx = 0;
  286. eidx = PFN_UP(addr + size - bdata->node_boot_start);
  287. if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
  288. eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
  289. for (i = sidx; i < eidx; i++) {
  290. if (test_bit(i, bdata->node_bootmem_map)) {
  291. if (flags & BOOTMEM_EXCLUSIVE)
  292. return -EBUSY;
  293. }
  294. }
  295. return 0;
  296. }
  297. static void __init reserve_bootmem_core(bootmem_data_t *bdata,
  298. unsigned long addr, unsigned long size, int flags)
  299. {
  300. unsigned long sidx, eidx;
  301. unsigned long i;
  302. BUG_ON(!size);
  303. /* out of range */
  304. if (addr + size < bdata->node_boot_start ||
  305. PFN_DOWN(addr) > bdata->node_low_pfn)
  306. return;
  307. /*
  308. * Round up to index to the range.
  309. */
  310. if (addr > bdata->node_boot_start)
  311. sidx= PFN_DOWN(addr - bdata->node_boot_start);
  312. else
  313. sidx = 0;
  314. eidx = PFN_UP(addr + size - bdata->node_boot_start);
  315. if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
  316. eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
  317. bdebug("nid=%td start=%lx end=%lx flags=%x\n",
  318. bdata - bootmem_node_data,
  319. sidx + PFN_DOWN(bdata->node_boot_start),
  320. eidx + PFN_DOWN(bdata->node_boot_start),
  321. flags);
  322. for (i = sidx; i < eidx; i++)
  323. if (test_and_set_bit(i, bdata->node_bootmem_map))
  324. bdebug("hm, page %lx reserved twice.\n",
  325. PFN_DOWN(bdata->node_boot_start) + i);
  326. }
  327. /**
  328. * reserve_bootmem_node - mark a page range as reserved
  329. * @pgdat: node the range resides on
  330. * @physaddr: starting address of the range
  331. * @size: size of the range in bytes
  332. * @flags: reservation flags (see linux/bootmem.h)
  333. *
  334. * Partial pages will be reserved.
  335. *
  336. * Only physical pages that actually reside on @pgdat are marked.
  337. */
  338. int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  339. unsigned long size, int flags)
  340. {
  341. int ret;
  342. ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
  343. if (ret < 0)
  344. return -ENOMEM;
  345. reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
  346. return 0;
  347. }
  348. #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
  349. /**
  350. * reserve_bootmem - mark a page range as usable
  351. * @addr: starting address of the range
  352. * @size: size of the range in bytes
  353. * @flags: reservation flags (see linux/bootmem.h)
  354. *
  355. * Partial pages will be reserved.
  356. *
  357. * All physical pages within the range are marked, no matter what
  358. * node they reside on.
  359. */
  360. int __init reserve_bootmem(unsigned long addr, unsigned long size,
  361. int flags)
  362. {
  363. bootmem_data_t *bdata;
  364. int ret;
  365. list_for_each_entry(bdata, &bdata_list, list) {
  366. ret = can_reserve_bootmem_core(bdata, addr, size, flags);
  367. if (ret < 0)
  368. return ret;
  369. }
  370. list_for_each_entry(bdata, &bdata_list, list)
  371. reserve_bootmem_core(bdata, addr, size, flags);
  372. return 0;
  373. }
  374. #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
  375. /*
  376. * We 'merge' subsequent allocations to save space. We might 'lose'
  377. * some fraction of a page if allocations cannot be satisfied due to
  378. * size constraints on boxes where there is physical RAM space
  379. * fragmentation - in these cases (mostly large memory boxes) this
  380. * is not a problem.
  381. *
  382. * On low memory boxes we get it right in 100% of the cases.
  383. *
  384. * alignment has to be a power of 2 value.
  385. *
  386. * NOTE: This function is _not_ reentrant.
  387. */
  388. static void * __init
  389. alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
  390. unsigned long align, unsigned long goal, unsigned long limit)
  391. {
  392. unsigned long areasize, preferred;
  393. unsigned long i, start = 0, incr, eidx, end_pfn;
  394. void *ret;
  395. unsigned long node_boot_start;
  396. void *node_bootmem_map;
  397. if (!size) {
  398. printk("alloc_bootmem_core(): zero-sized request\n");
  399. BUG();
  400. }
  401. BUG_ON(align & (align-1));
  402. /* on nodes without memory - bootmem_map is NULL */
  403. if (!bdata->node_bootmem_map)
  404. return NULL;
  405. bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
  406. bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
  407. align, goal, limit);
  408. /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
  409. node_boot_start = bdata->node_boot_start;
  410. node_bootmem_map = bdata->node_bootmem_map;
  411. if (align) {
  412. node_boot_start = ALIGN(bdata->node_boot_start, align);
  413. if (node_boot_start > bdata->node_boot_start)
  414. node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
  415. PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
  416. }
  417. if (limit && node_boot_start >= limit)
  418. return NULL;
  419. end_pfn = bdata->node_low_pfn;
  420. limit = PFN_DOWN(limit);
  421. if (limit && end_pfn > limit)
  422. end_pfn = limit;
  423. eidx = end_pfn - PFN_DOWN(node_boot_start);
  424. /*
  425. * We try to allocate bootmem pages above 'goal'
  426. * first, then we try to allocate lower pages.
  427. */
  428. preferred = 0;
  429. if (goal && PFN_DOWN(goal) < end_pfn) {
  430. if (goal > node_boot_start)
  431. preferred = goal - node_boot_start;
  432. if (bdata->last_success > node_boot_start &&
  433. bdata->last_success - node_boot_start >= preferred)
  434. if (!limit || (limit && limit > bdata->last_success))
  435. preferred = bdata->last_success - node_boot_start;
  436. }
  437. preferred = PFN_DOWN(ALIGN(preferred, align));
  438. areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
  439. incr = align >> PAGE_SHIFT ? : 1;
  440. restart_scan:
  441. for (i = preferred; i < eidx;) {
  442. unsigned long j;
  443. i = find_next_zero_bit(node_bootmem_map, eidx, i);
  444. i = ALIGN(i, incr);
  445. if (i >= eidx)
  446. break;
  447. if (test_bit(i, node_bootmem_map)) {
  448. i += incr;
  449. continue;
  450. }
  451. for (j = i + 1; j < i + areasize; ++j) {
  452. if (j >= eidx)
  453. goto fail_block;
  454. if (test_bit(j, node_bootmem_map))
  455. goto fail_block;
  456. }
  457. start = i;
  458. goto found;
  459. fail_block:
  460. i = ALIGN(j, incr);
  461. if (i == j)
  462. i += incr;
  463. }
  464. if (preferred > 0) {
  465. preferred = 0;
  466. goto restart_scan;
  467. }
  468. return NULL;
  469. found:
  470. bdata->last_success = PFN_PHYS(start) + node_boot_start;
  471. BUG_ON(start >= eidx);
  472. /*
  473. * Is the next page of the previous allocation-end the start
  474. * of this allocation's buffer? If yes then we can 'merge'
  475. * the previous partial page with this allocation.
  476. */
  477. if (align < PAGE_SIZE &&
  478. bdata->last_offset && bdata->last_pos+1 == start) {
  479. unsigned long offset, remaining_size;
  480. offset = ALIGN(bdata->last_offset, align);
  481. BUG_ON(offset > PAGE_SIZE);
  482. remaining_size = PAGE_SIZE - offset;
  483. if (size < remaining_size) {
  484. areasize = 0;
  485. /* last_pos unchanged */
  486. bdata->last_offset = offset + size;
  487. ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
  488. offset + node_boot_start);
  489. } else {
  490. remaining_size = size - remaining_size;
  491. areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
  492. ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
  493. offset + node_boot_start);
  494. bdata->last_pos = start + areasize - 1;
  495. bdata->last_offset = remaining_size;
  496. }
  497. bdata->last_offset &= ~PAGE_MASK;
  498. } else {
  499. bdata->last_pos = start + areasize - 1;
  500. bdata->last_offset = size & ~PAGE_MASK;
  501. ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
  502. }
  503. bdebug("nid=%td start=%lx end=%lx\n",
  504. bdata - bootmem_node_data,
  505. start + PFN_DOWN(bdata->node_boot_start),
  506. start + areasize + PFN_DOWN(bdata->node_boot_start));
  507. /*
  508. * Reserve the area now:
  509. */
  510. for (i = start; i < start + areasize; i++)
  511. if (unlikely(test_and_set_bit(i, node_bootmem_map)))
  512. BUG();
  513. memset(ret, 0, size);
  514. return ret;
  515. }
  516. /**
  517. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  518. * @size: size of the request in bytes
  519. * @align: alignment of the region
  520. * @goal: preferred starting address of the region
  521. *
  522. * The goal is dropped if it can not be satisfied and the allocation will
  523. * fall back to memory below @goal.
  524. *
  525. * Allocation may happen on any node in the system.
  526. *
  527. * Returns NULL on failure.
  528. */
  529. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  530. unsigned long goal)
  531. {
  532. bootmem_data_t *bdata;
  533. void *ptr;
  534. list_for_each_entry(bdata, &bdata_list, list) {
  535. ptr = alloc_bootmem_core(bdata, size, align, goal, 0);
  536. if (ptr)
  537. return ptr;
  538. }
  539. return NULL;
  540. }
  541. /**
  542. * __alloc_bootmem - allocate boot memory
  543. * @size: size of the request in bytes
  544. * @align: alignment of the region
  545. * @goal: preferred starting address of the region
  546. *
  547. * The goal is dropped if it can not be satisfied and the allocation will
  548. * fall back to memory below @goal.
  549. *
  550. * Allocation may happen on any node in the system.
  551. *
  552. * The function panics if the request can not be satisfied.
  553. */
  554. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  555. unsigned long goal)
  556. {
  557. void *mem = __alloc_bootmem_nopanic(size,align,goal);
  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_node - allocate boot memory from a specific node
  569. * @pgdat: node to allocate from
  570. * @size: size of the request in bytes
  571. * @align: alignment of the region
  572. * @goal: preferred starting address of the region
  573. *
  574. * The goal is dropped if it can not be satisfied and the allocation will
  575. * fall back to memory below @goal.
  576. *
  577. * Allocation may fall back to any node in the system if the specified node
  578. * can not hold the requested memory.
  579. *
  580. * The function panics if the request can not be satisfied.
  581. */
  582. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  583. unsigned long align, unsigned long goal)
  584. {
  585. void *ptr;
  586. ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
  587. if (ptr)
  588. return ptr;
  589. return __alloc_bootmem(size, align, goal);
  590. }
  591. #ifdef CONFIG_SPARSEMEM
  592. /**
  593. * alloc_bootmem_section - allocate boot memory from a specific section
  594. * @size: size of the request in bytes
  595. * @section_nr: sparse map section to allocate from
  596. *
  597. * Return NULL on failure.
  598. */
  599. void * __init alloc_bootmem_section(unsigned long size,
  600. unsigned long section_nr)
  601. {
  602. void *ptr;
  603. unsigned long limit, goal, start_nr, end_nr, pfn;
  604. struct pglist_data *pgdat;
  605. pfn = section_nr_to_pfn(section_nr);
  606. goal = PFN_PHYS(pfn);
  607. limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1;
  608. pgdat = NODE_DATA(early_pfn_to_nid(pfn));
  609. ptr = alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal,
  610. limit);
  611. if (!ptr)
  612. return NULL;
  613. start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr)));
  614. end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size));
  615. if (start_nr != section_nr || end_nr != section_nr) {
  616. printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n",
  617. section_nr);
  618. free_bootmem_core(pgdat->bdata, __pa(ptr), size);
  619. ptr = NULL;
  620. }
  621. return ptr;
  622. }
  623. #endif
  624. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  625. unsigned long align, unsigned long goal)
  626. {
  627. void *ptr;
  628. ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
  629. if (ptr)
  630. return ptr;
  631. return __alloc_bootmem_nopanic(size, align, goal);
  632. }
  633. #ifndef ARCH_LOW_ADDRESS_LIMIT
  634. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  635. #endif
  636. /**
  637. * __alloc_bootmem_low - allocate low boot memory
  638. * @size: size of the request in bytes
  639. * @align: alignment of the region
  640. * @goal: preferred starting address of the region
  641. *
  642. * The goal is dropped if it can not be satisfied and the allocation will
  643. * fall back to memory below @goal.
  644. *
  645. * Allocation may happen on any node in the system.
  646. *
  647. * The function panics if the request can not be satisfied.
  648. */
  649. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  650. unsigned long goal)
  651. {
  652. bootmem_data_t *bdata;
  653. void *ptr;
  654. list_for_each_entry(bdata, &bdata_list, list) {
  655. ptr = alloc_bootmem_core(bdata, size, align, goal,
  656. ARCH_LOW_ADDRESS_LIMIT);
  657. if (ptr)
  658. return ptr;
  659. }
  660. /*
  661. * Whoops, we cannot satisfy the allocation request.
  662. */
  663. printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
  664. panic("Out of low memory");
  665. return NULL;
  666. }
  667. /**
  668. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  669. * @pgdat: node to allocate from
  670. * @size: size of the request in bytes
  671. * @align: alignment of the region
  672. * @goal: preferred starting address of the region
  673. *
  674. * The goal is dropped if it can not be satisfied and the allocation will
  675. * fall back to memory below @goal.
  676. *
  677. * Allocation may fall back to any node in the system if the specified node
  678. * can not hold the requested memory.
  679. *
  680. * The function panics if the request can not be satisfied.
  681. */
  682. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  683. unsigned long align, unsigned long goal)
  684. {
  685. return alloc_bootmem_core(pgdat->bdata, size, align, goal,
  686. ARCH_LOW_ADDRESS_LIMIT);
  687. }