bootmem.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  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. int aligned;
  124. struct page *page;
  125. unsigned long start, end, pages, count = 0;
  126. if (!bdata->node_bootmem_map)
  127. return 0;
  128. start = PFN_DOWN(bdata->node_boot_start);
  129. end = bdata->node_low_pfn;
  130. /*
  131. * If the start is aligned to the machines wordsize, we might
  132. * be able to free pages in bulks of that order.
  133. */
  134. aligned = !(start & (BITS_PER_LONG - 1));
  135. bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
  136. bdata - bootmem_node_data, start, end, aligned);
  137. while (start < end) {
  138. unsigned long *map, idx, vec;
  139. map = bdata->node_bootmem_map;
  140. idx = start - PFN_DOWN(bdata->node_boot_start);
  141. vec = ~map[idx / BITS_PER_LONG];
  142. if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
  143. int order = ilog2(BITS_PER_LONG);
  144. __free_pages_bootmem(pfn_to_page(start), order);
  145. count += BITS_PER_LONG;
  146. } else {
  147. unsigned long off = 0;
  148. while (vec && off < BITS_PER_LONG) {
  149. if (vec & 1) {
  150. page = pfn_to_page(start + off);
  151. __free_pages_bootmem(page, 0);
  152. count++;
  153. }
  154. vec >>= 1;
  155. off++;
  156. }
  157. }
  158. start += BITS_PER_LONG;
  159. }
  160. page = virt_to_page(bdata->node_bootmem_map);
  161. pages = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
  162. pages = bootmem_bootmap_pages(pages);
  163. count += pages;
  164. while (pages--)
  165. __free_pages_bootmem(page++, 0);
  166. bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
  167. return count;
  168. }
  169. /**
  170. * free_all_bootmem_node - release a node's free pages to the buddy allocator
  171. * @pgdat: node to be released
  172. *
  173. * Returns the number of pages actually released.
  174. */
  175. unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
  176. {
  177. register_page_bootmem_info_node(pgdat);
  178. return free_all_bootmem_core(pgdat->bdata);
  179. }
  180. /**
  181. * free_all_bootmem - release free pages to the buddy allocator
  182. *
  183. * Returns the number of pages actually released.
  184. */
  185. unsigned long __init free_all_bootmem(void)
  186. {
  187. return free_all_bootmem_core(NODE_DATA(0)->bdata);
  188. }
  189. static void __init free_bootmem_core(bootmem_data_t *bdata, unsigned long addr,
  190. unsigned long size)
  191. {
  192. unsigned long sidx, eidx;
  193. unsigned long i;
  194. BUG_ON(!size);
  195. /* out range */
  196. if (addr + size < bdata->node_boot_start ||
  197. PFN_DOWN(addr) > bdata->node_low_pfn)
  198. return;
  199. /*
  200. * round down end of usable mem, partially free pages are
  201. * considered reserved.
  202. */
  203. if (addr >= bdata->node_boot_start && addr < bdata->last_success)
  204. bdata->last_success = addr;
  205. /*
  206. * Round up to index to the range.
  207. */
  208. if (PFN_UP(addr) > PFN_DOWN(bdata->node_boot_start))
  209. sidx = PFN_UP(addr) - PFN_DOWN(bdata->node_boot_start);
  210. else
  211. sidx = 0;
  212. eidx = PFN_DOWN(addr + size - bdata->node_boot_start);
  213. if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
  214. eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
  215. bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
  216. sidx + PFN_DOWN(bdata->node_boot_start),
  217. eidx + PFN_DOWN(bdata->node_boot_start));
  218. for (i = sidx; i < eidx; i++) {
  219. if (unlikely(!test_and_clear_bit(i, bdata->node_bootmem_map)))
  220. BUG();
  221. }
  222. }
  223. /**
  224. * free_bootmem_node - mark a page range as usable
  225. * @pgdat: node the range resides on
  226. * @physaddr: starting address of the range
  227. * @size: size of the range in bytes
  228. *
  229. * Partial pages will be considered reserved and left as they are.
  230. *
  231. * Only physical pages that actually reside on @pgdat are marked.
  232. */
  233. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  234. unsigned long size)
  235. {
  236. free_bootmem_core(pgdat->bdata, physaddr, size);
  237. }
  238. /**
  239. * free_bootmem - mark a page range as usable
  240. * @addr: starting address of the range
  241. * @size: size of the range in bytes
  242. *
  243. * Partial pages will be considered reserved and left as they are.
  244. *
  245. * All physical pages within the range are marked, no matter what
  246. * node they reside on.
  247. */
  248. void __init free_bootmem(unsigned long addr, unsigned long size)
  249. {
  250. bootmem_data_t *bdata;
  251. list_for_each_entry(bdata, &bdata_list, list)
  252. free_bootmem_core(bdata, addr, size);
  253. }
  254. /*
  255. * Marks a particular physical memory range as unallocatable. Usable RAM
  256. * might be used for boot-time allocations - or it might get added
  257. * to the free page pool later on.
  258. */
  259. static int __init can_reserve_bootmem_core(bootmem_data_t *bdata,
  260. unsigned long addr, unsigned long size, int flags)
  261. {
  262. unsigned long sidx, eidx;
  263. unsigned long i;
  264. BUG_ON(!size);
  265. /* out of range, don't hold other */
  266. if (addr + size < bdata->node_boot_start ||
  267. PFN_DOWN(addr) > bdata->node_low_pfn)
  268. return 0;
  269. /*
  270. * Round up to index to the range.
  271. */
  272. if (addr > bdata->node_boot_start)
  273. sidx= PFN_DOWN(addr - bdata->node_boot_start);
  274. else
  275. sidx = 0;
  276. eidx = PFN_UP(addr + size - bdata->node_boot_start);
  277. if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
  278. eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
  279. for (i = sidx; i < eidx; i++) {
  280. if (test_bit(i, bdata->node_bootmem_map)) {
  281. if (flags & BOOTMEM_EXCLUSIVE)
  282. return -EBUSY;
  283. }
  284. }
  285. return 0;
  286. }
  287. static void __init reserve_bootmem_core(bootmem_data_t *bdata,
  288. unsigned long addr, unsigned long size, int flags)
  289. {
  290. unsigned long sidx, eidx;
  291. unsigned long i;
  292. BUG_ON(!size);
  293. /* out of range */
  294. if (addr + size < bdata->node_boot_start ||
  295. PFN_DOWN(addr) > bdata->node_low_pfn)
  296. return;
  297. /*
  298. * Round up to index to the range.
  299. */
  300. if (addr > bdata->node_boot_start)
  301. sidx= PFN_DOWN(addr - bdata->node_boot_start);
  302. else
  303. sidx = 0;
  304. eidx = PFN_UP(addr + size - bdata->node_boot_start);
  305. if (eidx > bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start))
  306. eidx = bdata->node_low_pfn - PFN_DOWN(bdata->node_boot_start);
  307. bdebug("nid=%td start=%lx end=%lx flags=%x\n",
  308. bdata - bootmem_node_data,
  309. sidx + PFN_DOWN(bdata->node_boot_start),
  310. eidx + PFN_DOWN(bdata->node_boot_start),
  311. flags);
  312. for (i = sidx; i < eidx; i++)
  313. if (test_and_set_bit(i, bdata->node_bootmem_map))
  314. bdebug("hm, page %lx reserved twice.\n",
  315. PFN_DOWN(bdata->node_boot_start) + i);
  316. }
  317. /**
  318. * reserve_bootmem_node - mark a page range as reserved
  319. * @pgdat: node the range resides on
  320. * @physaddr: starting address of the range
  321. * @size: size of the range in bytes
  322. * @flags: reservation flags (see linux/bootmem.h)
  323. *
  324. * Partial pages will be reserved.
  325. *
  326. * Only physical pages that actually reside on @pgdat are marked.
  327. */
  328. int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  329. unsigned long size, int flags)
  330. {
  331. int ret;
  332. ret = can_reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
  333. if (ret < 0)
  334. return -ENOMEM;
  335. reserve_bootmem_core(pgdat->bdata, physaddr, size, flags);
  336. return 0;
  337. }
  338. #ifndef CONFIG_HAVE_ARCH_BOOTMEM_NODE
  339. /**
  340. * reserve_bootmem - mark a page range as usable
  341. * @addr: starting address of the range
  342. * @size: size of the range in bytes
  343. * @flags: reservation flags (see linux/bootmem.h)
  344. *
  345. * Partial pages will be reserved.
  346. *
  347. * All physical pages within the range are marked, no matter what
  348. * node they reside on.
  349. */
  350. int __init reserve_bootmem(unsigned long addr, unsigned long size,
  351. int flags)
  352. {
  353. bootmem_data_t *bdata;
  354. int ret;
  355. list_for_each_entry(bdata, &bdata_list, list) {
  356. ret = can_reserve_bootmem_core(bdata, addr, size, flags);
  357. if (ret < 0)
  358. return ret;
  359. }
  360. list_for_each_entry(bdata, &bdata_list, list)
  361. reserve_bootmem_core(bdata, addr, size, flags);
  362. return 0;
  363. }
  364. #endif /* !CONFIG_HAVE_ARCH_BOOTMEM_NODE */
  365. /*
  366. * We 'merge' subsequent allocations to save space. We might 'lose'
  367. * some fraction of a page if allocations cannot be satisfied due to
  368. * size constraints on boxes where there is physical RAM space
  369. * fragmentation - in these cases (mostly large memory boxes) this
  370. * is not a problem.
  371. *
  372. * On low memory boxes we get it right in 100% of the cases.
  373. *
  374. * alignment has to be a power of 2 value.
  375. *
  376. * NOTE: This function is _not_ reentrant.
  377. */
  378. static void * __init
  379. alloc_bootmem_core(struct bootmem_data *bdata, unsigned long size,
  380. unsigned long align, unsigned long goal, unsigned long limit)
  381. {
  382. unsigned long areasize, preferred;
  383. unsigned long i, start = 0, incr, eidx, end_pfn;
  384. void *ret;
  385. unsigned long node_boot_start;
  386. void *node_bootmem_map;
  387. if (!size) {
  388. printk("alloc_bootmem_core(): zero-sized request\n");
  389. BUG();
  390. }
  391. BUG_ON(align & (align-1));
  392. /* on nodes without memory - bootmem_map is NULL */
  393. if (!bdata->node_bootmem_map)
  394. return NULL;
  395. bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
  396. bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
  397. align, goal, limit);
  398. /* bdata->node_boot_start is supposed to be (12+6)bits alignment on x86_64 ? */
  399. node_boot_start = bdata->node_boot_start;
  400. node_bootmem_map = bdata->node_bootmem_map;
  401. if (align) {
  402. node_boot_start = ALIGN(bdata->node_boot_start, align);
  403. if (node_boot_start > bdata->node_boot_start)
  404. node_bootmem_map = (unsigned long *)bdata->node_bootmem_map +
  405. PFN_DOWN(node_boot_start - bdata->node_boot_start)/BITS_PER_LONG;
  406. }
  407. if (limit && node_boot_start >= limit)
  408. return NULL;
  409. end_pfn = bdata->node_low_pfn;
  410. limit = PFN_DOWN(limit);
  411. if (limit && end_pfn > limit)
  412. end_pfn = limit;
  413. eidx = end_pfn - PFN_DOWN(node_boot_start);
  414. /*
  415. * We try to allocate bootmem pages above 'goal'
  416. * first, then we try to allocate lower pages.
  417. */
  418. preferred = 0;
  419. if (goal && PFN_DOWN(goal) < end_pfn) {
  420. if (goal > node_boot_start)
  421. preferred = goal - node_boot_start;
  422. if (bdata->last_success > node_boot_start &&
  423. bdata->last_success - node_boot_start >= preferred)
  424. if (!limit || (limit && limit > bdata->last_success))
  425. preferred = bdata->last_success - node_boot_start;
  426. }
  427. preferred = PFN_DOWN(ALIGN(preferred, align));
  428. areasize = (size + PAGE_SIZE-1) / PAGE_SIZE;
  429. incr = align >> PAGE_SHIFT ? : 1;
  430. restart_scan:
  431. for (i = preferred; i < eidx;) {
  432. unsigned long j;
  433. i = find_next_zero_bit(node_bootmem_map, eidx, i);
  434. i = ALIGN(i, incr);
  435. if (i >= eidx)
  436. break;
  437. if (test_bit(i, node_bootmem_map)) {
  438. i += incr;
  439. continue;
  440. }
  441. for (j = i + 1; j < i + areasize; ++j) {
  442. if (j >= eidx)
  443. goto fail_block;
  444. if (test_bit(j, node_bootmem_map))
  445. goto fail_block;
  446. }
  447. start = i;
  448. goto found;
  449. fail_block:
  450. i = ALIGN(j, incr);
  451. if (i == j)
  452. i += incr;
  453. }
  454. if (preferred > 0) {
  455. preferred = 0;
  456. goto restart_scan;
  457. }
  458. return NULL;
  459. found:
  460. bdata->last_success = PFN_PHYS(start) + node_boot_start;
  461. BUG_ON(start >= eidx);
  462. /*
  463. * Is the next page of the previous allocation-end the start
  464. * of this allocation's buffer? If yes then we can 'merge'
  465. * the previous partial page with this allocation.
  466. */
  467. if (align < PAGE_SIZE &&
  468. bdata->last_offset && bdata->last_pos+1 == start) {
  469. unsigned long offset, remaining_size;
  470. offset = ALIGN(bdata->last_offset, align);
  471. BUG_ON(offset > PAGE_SIZE);
  472. remaining_size = PAGE_SIZE - offset;
  473. if (size < remaining_size) {
  474. areasize = 0;
  475. /* last_pos unchanged */
  476. bdata->last_offset = offset + size;
  477. ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
  478. offset + node_boot_start);
  479. } else {
  480. remaining_size = size - remaining_size;
  481. areasize = (remaining_size + PAGE_SIZE-1) / PAGE_SIZE;
  482. ret = phys_to_virt(bdata->last_pos * PAGE_SIZE +
  483. offset + node_boot_start);
  484. bdata->last_pos = start + areasize - 1;
  485. bdata->last_offset = remaining_size;
  486. }
  487. bdata->last_offset &= ~PAGE_MASK;
  488. } else {
  489. bdata->last_pos = start + areasize - 1;
  490. bdata->last_offset = size & ~PAGE_MASK;
  491. ret = phys_to_virt(start * PAGE_SIZE + node_boot_start);
  492. }
  493. bdebug("nid=%td start=%lx end=%lx\n",
  494. bdata - bootmem_node_data,
  495. start + PFN_DOWN(bdata->node_boot_start),
  496. start + areasize + PFN_DOWN(bdata->node_boot_start));
  497. /*
  498. * Reserve the area now:
  499. */
  500. for (i = start; i < start + areasize; i++)
  501. if (unlikely(test_and_set_bit(i, node_bootmem_map)))
  502. BUG();
  503. memset(ret, 0, size);
  504. return ret;
  505. }
  506. /**
  507. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  508. * @size: size of the request in bytes
  509. * @align: alignment of the region
  510. * @goal: preferred starting address of the region
  511. *
  512. * The goal is dropped if it can not be satisfied and the allocation will
  513. * fall back to memory below @goal.
  514. *
  515. * Allocation may happen on any node in the system.
  516. *
  517. * Returns NULL on failure.
  518. */
  519. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  520. unsigned long goal)
  521. {
  522. bootmem_data_t *bdata;
  523. void *ptr;
  524. list_for_each_entry(bdata, &bdata_list, list) {
  525. ptr = alloc_bootmem_core(bdata, size, align, goal, 0);
  526. if (ptr)
  527. return ptr;
  528. }
  529. return NULL;
  530. }
  531. /**
  532. * __alloc_bootmem - allocate boot memory
  533. * @size: size of the request in bytes
  534. * @align: alignment of the region
  535. * @goal: preferred starting address of the region
  536. *
  537. * The goal is dropped if it can not be satisfied and the allocation will
  538. * fall back to memory below @goal.
  539. *
  540. * Allocation may happen on any node in the system.
  541. *
  542. * The function panics if the request can not be satisfied.
  543. */
  544. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  545. unsigned long goal)
  546. {
  547. void *mem = __alloc_bootmem_nopanic(size,align,goal);
  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_node - allocate boot memory from a specific node
  559. * @pgdat: node to allocate from
  560. * @size: size of the request in bytes
  561. * @align: alignment of the region
  562. * @goal: preferred starting address of the region
  563. *
  564. * The goal is dropped if it can not be satisfied and the allocation will
  565. * fall back to memory below @goal.
  566. *
  567. * Allocation may fall back to any node in the system if the specified node
  568. * can not hold the requested memory.
  569. *
  570. * The function panics if the request can not be satisfied.
  571. */
  572. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  573. unsigned long align, unsigned long goal)
  574. {
  575. void *ptr;
  576. ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
  577. if (ptr)
  578. return ptr;
  579. return __alloc_bootmem(size, align, goal);
  580. }
  581. #ifdef CONFIG_SPARSEMEM
  582. /**
  583. * alloc_bootmem_section - allocate boot memory from a specific section
  584. * @size: size of the request in bytes
  585. * @section_nr: sparse map section to allocate from
  586. *
  587. * Return NULL on failure.
  588. */
  589. void * __init alloc_bootmem_section(unsigned long size,
  590. unsigned long section_nr)
  591. {
  592. void *ptr;
  593. unsigned long limit, goal, start_nr, end_nr, pfn;
  594. struct pglist_data *pgdat;
  595. pfn = section_nr_to_pfn(section_nr);
  596. goal = PFN_PHYS(pfn);
  597. limit = PFN_PHYS(section_nr_to_pfn(section_nr + 1)) - 1;
  598. pgdat = NODE_DATA(early_pfn_to_nid(pfn));
  599. ptr = alloc_bootmem_core(pgdat->bdata, size, SMP_CACHE_BYTES, goal,
  600. limit);
  601. if (!ptr)
  602. return NULL;
  603. start_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr)));
  604. end_nr = pfn_to_section_nr(PFN_DOWN(__pa(ptr) + size));
  605. if (start_nr != section_nr || end_nr != section_nr) {
  606. printk(KERN_WARNING "alloc_bootmem failed on section %ld.\n",
  607. section_nr);
  608. free_bootmem_core(pgdat->bdata, __pa(ptr), size);
  609. ptr = NULL;
  610. }
  611. return ptr;
  612. }
  613. #endif
  614. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  615. unsigned long align, unsigned long goal)
  616. {
  617. void *ptr;
  618. ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
  619. if (ptr)
  620. return ptr;
  621. return __alloc_bootmem_nopanic(size, align, goal);
  622. }
  623. #ifndef ARCH_LOW_ADDRESS_LIMIT
  624. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  625. #endif
  626. /**
  627. * __alloc_bootmem_low - allocate low boot memory
  628. * @size: size of the request in bytes
  629. * @align: alignment of the region
  630. * @goal: preferred starting address of the region
  631. *
  632. * The goal is dropped if it can not be satisfied and the allocation will
  633. * fall back to memory below @goal.
  634. *
  635. * Allocation may happen on any node in the system.
  636. *
  637. * The function panics if the request can not be satisfied.
  638. */
  639. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  640. unsigned long goal)
  641. {
  642. bootmem_data_t *bdata;
  643. void *ptr;
  644. list_for_each_entry(bdata, &bdata_list, list) {
  645. ptr = alloc_bootmem_core(bdata, size, align, goal,
  646. ARCH_LOW_ADDRESS_LIMIT);
  647. if (ptr)
  648. return ptr;
  649. }
  650. /*
  651. * Whoops, we cannot satisfy the allocation request.
  652. */
  653. printk(KERN_ALERT "low bootmem alloc of %lu bytes failed!\n", size);
  654. panic("Out of low memory");
  655. return NULL;
  656. }
  657. /**
  658. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  659. * @pgdat: node to allocate from
  660. * @size: size of the request in bytes
  661. * @align: alignment of the region
  662. * @goal: preferred starting address of the region
  663. *
  664. * The goal is dropped if it can not be satisfied and the allocation will
  665. * fall back to memory below @goal.
  666. *
  667. * Allocation may fall back to any node in the system if the specified node
  668. * can not hold the requested memory.
  669. *
  670. * The function panics if the request can not be satisfied.
  671. */
  672. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  673. unsigned long align, unsigned long goal)
  674. {
  675. return alloc_bootmem_core(pgdat->bdata, size, align, goal,
  676. ARCH_LOW_ADDRESS_LIMIT);
  677. }