bootmem.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  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 <linux/kmemleak.h>
  16. #include <linux/range.h>
  17. #include <asm/bug.h>
  18. #include <asm/io.h>
  19. #include <asm/processor.h>
  20. #include "internal.h"
  21. unsigned long max_low_pfn;
  22. unsigned long min_low_pfn;
  23. unsigned long max_pfn;
  24. #ifdef CONFIG_CRASH_DUMP
  25. /*
  26. * If we have booted due to a crash, max_pfn will be a very low value. We need
  27. * to know the amount of memory that the previous kernel used.
  28. */
  29. unsigned long saved_max_pfn;
  30. #endif
  31. #ifndef CONFIG_NO_BOOTMEM
  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. #endif
  125. /*
  126. * free_bootmem_late - free bootmem pages directly to page allocator
  127. * @addr: starting address of the range
  128. * @size: size of the range in bytes
  129. *
  130. * This is only useful when the bootmem allocator has already been torn
  131. * down, but we are still initializing the system. Pages are given directly
  132. * to the page allocator, no bootmem metadata is updated because it is gone.
  133. */
  134. void __init free_bootmem_late(unsigned long addr, unsigned long size)
  135. {
  136. unsigned long cursor, end;
  137. kmemleak_free_part(__va(addr), size);
  138. cursor = PFN_UP(addr);
  139. end = PFN_DOWN(addr + size);
  140. for (; cursor < end; cursor++) {
  141. __free_pages_bootmem(pfn_to_page(cursor), 0);
  142. totalram_pages++;
  143. }
  144. }
  145. #ifdef CONFIG_NO_BOOTMEM
  146. static void __init __free_pages_memory(unsigned long start, unsigned long end)
  147. {
  148. int i;
  149. unsigned long start_aligned, end_aligned;
  150. int order = ilog2(BITS_PER_LONG);
  151. start_aligned = (start + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1);
  152. end_aligned = end & ~(BITS_PER_LONG - 1);
  153. if (end_aligned <= start_aligned) {
  154. for (i = start; i < end; i++)
  155. __free_pages_bootmem(pfn_to_page(i), 0);
  156. return;
  157. }
  158. for (i = start; i < start_aligned; i++)
  159. __free_pages_bootmem(pfn_to_page(i), 0);
  160. for (i = start_aligned; i < end_aligned; i += BITS_PER_LONG)
  161. __free_pages_bootmem(pfn_to_page(i), order);
  162. for (i = end_aligned; i < end; i++)
  163. __free_pages_bootmem(pfn_to_page(i), 0);
  164. }
  165. unsigned long __init free_all_memory_core_early(int nodeid)
  166. {
  167. int i;
  168. u64 start, end;
  169. unsigned long count = 0;
  170. struct range *range = NULL;
  171. int nr_range;
  172. nr_range = get_free_all_memory_range(&range, nodeid);
  173. for (i = 0; i < nr_range; i++) {
  174. start = range[i].start;
  175. end = range[i].end;
  176. count += end - start;
  177. __free_pages_memory(start, end);
  178. }
  179. return count;
  180. }
  181. #else
  182. static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
  183. {
  184. int aligned;
  185. struct page *page;
  186. unsigned long start, end, pages, count = 0;
  187. if (!bdata->node_bootmem_map)
  188. return 0;
  189. start = bdata->node_min_pfn;
  190. end = bdata->node_low_pfn;
  191. /*
  192. * If the start is aligned to the machines wordsize, we might
  193. * be able to free pages in bulks of that order.
  194. */
  195. aligned = !(start & (BITS_PER_LONG - 1));
  196. bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
  197. bdata - bootmem_node_data, start, end, aligned);
  198. while (start < end) {
  199. unsigned long *map, idx, vec;
  200. map = bdata->node_bootmem_map;
  201. idx = start - bdata->node_min_pfn;
  202. vec = ~map[idx / BITS_PER_LONG];
  203. if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
  204. int order = ilog2(BITS_PER_LONG);
  205. __free_pages_bootmem(pfn_to_page(start), order);
  206. count += BITS_PER_LONG;
  207. } else {
  208. unsigned long off = 0;
  209. while (vec && off < BITS_PER_LONG) {
  210. if (vec & 1) {
  211. page = pfn_to_page(start + off);
  212. __free_pages_bootmem(page, 0);
  213. count++;
  214. }
  215. vec >>= 1;
  216. off++;
  217. }
  218. }
  219. start += BITS_PER_LONG;
  220. }
  221. page = virt_to_page(bdata->node_bootmem_map);
  222. pages = bdata->node_low_pfn - bdata->node_min_pfn;
  223. pages = bootmem_bootmap_pages(pages);
  224. count += pages;
  225. while (pages--)
  226. __free_pages_bootmem(page++, 0);
  227. bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
  228. return count;
  229. }
  230. #endif
  231. /**
  232. * free_all_bootmem_node - release a node's free pages to the buddy allocator
  233. * @pgdat: node to be released
  234. *
  235. * Returns the number of pages actually released.
  236. */
  237. unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
  238. {
  239. register_page_bootmem_info_node(pgdat);
  240. #ifdef CONFIG_NO_BOOTMEM
  241. /* free_all_memory_core_early(MAX_NUMNODES) will be called later */
  242. return 0;
  243. #else
  244. return free_all_bootmem_core(pgdat->bdata);
  245. #endif
  246. }
  247. /**
  248. * free_all_bootmem - release free pages to the buddy allocator
  249. *
  250. * Returns the number of pages actually released.
  251. */
  252. unsigned long __init free_all_bootmem(void)
  253. {
  254. #ifdef CONFIG_NO_BOOTMEM
  255. return free_all_memory_core_early(NODE_DATA(0)->node_id);
  256. #else
  257. return free_all_bootmem_core(NODE_DATA(0)->bdata);
  258. #endif
  259. }
  260. #ifndef CONFIG_NO_BOOTMEM
  261. static void __init __free(bootmem_data_t *bdata,
  262. unsigned long sidx, unsigned long eidx)
  263. {
  264. unsigned long idx;
  265. bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
  266. sidx + bdata->node_min_pfn,
  267. eidx + bdata->node_min_pfn);
  268. if (bdata->hint_idx > sidx)
  269. bdata->hint_idx = sidx;
  270. for (idx = sidx; idx < eidx; idx++)
  271. if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
  272. BUG();
  273. }
  274. static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
  275. unsigned long eidx, int flags)
  276. {
  277. unsigned long idx;
  278. int exclusive = flags & BOOTMEM_EXCLUSIVE;
  279. bdebug("nid=%td start=%lx end=%lx flags=%x\n",
  280. bdata - bootmem_node_data,
  281. sidx + bdata->node_min_pfn,
  282. eidx + bdata->node_min_pfn,
  283. flags);
  284. for (idx = sidx; idx < eidx; idx++)
  285. if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
  286. if (exclusive) {
  287. __free(bdata, sidx, idx);
  288. return -EBUSY;
  289. }
  290. bdebug("silent double reserve of PFN %lx\n",
  291. idx + bdata->node_min_pfn);
  292. }
  293. return 0;
  294. }
  295. static int __init mark_bootmem_node(bootmem_data_t *bdata,
  296. unsigned long start, unsigned long end,
  297. int reserve, int flags)
  298. {
  299. unsigned long sidx, eidx;
  300. bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
  301. bdata - bootmem_node_data, start, end, reserve, flags);
  302. BUG_ON(start < bdata->node_min_pfn);
  303. BUG_ON(end > bdata->node_low_pfn);
  304. sidx = start - bdata->node_min_pfn;
  305. eidx = end - bdata->node_min_pfn;
  306. if (reserve)
  307. return __reserve(bdata, sidx, eidx, flags);
  308. else
  309. __free(bdata, sidx, eidx);
  310. return 0;
  311. }
  312. static int __init mark_bootmem(unsigned long start, unsigned long end,
  313. int reserve, int flags)
  314. {
  315. unsigned long pos;
  316. bootmem_data_t *bdata;
  317. pos = start;
  318. list_for_each_entry(bdata, &bdata_list, list) {
  319. int err;
  320. unsigned long max;
  321. if (pos < bdata->node_min_pfn ||
  322. pos >= bdata->node_low_pfn) {
  323. BUG_ON(pos != start);
  324. continue;
  325. }
  326. max = min(bdata->node_low_pfn, end);
  327. err = mark_bootmem_node(bdata, pos, max, reserve, flags);
  328. if (reserve && err) {
  329. mark_bootmem(start, pos, 0, 0);
  330. return err;
  331. }
  332. if (max == end)
  333. return 0;
  334. pos = bdata->node_low_pfn;
  335. }
  336. BUG();
  337. }
  338. #endif
  339. /**
  340. * free_bootmem_node - mark a page range as usable
  341. * @pgdat: node the range resides on
  342. * @physaddr: starting address of the range
  343. * @size: size of the range in bytes
  344. *
  345. * Partial pages will be considered reserved and left as they are.
  346. *
  347. * The range must reside completely on the specified node.
  348. */
  349. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  350. unsigned long size)
  351. {
  352. #ifdef CONFIG_NO_BOOTMEM
  353. free_early(physaddr, physaddr + size);
  354. #else
  355. unsigned long start, end;
  356. kmemleak_free_part(__va(physaddr), size);
  357. start = PFN_UP(physaddr);
  358. end = PFN_DOWN(physaddr + size);
  359. mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
  360. #endif
  361. }
  362. /**
  363. * free_bootmem - mark a page range as usable
  364. * @addr: starting address of the range
  365. * @size: size of the range in bytes
  366. *
  367. * Partial pages will be considered reserved and left as they are.
  368. *
  369. * The range must be contiguous but may span node boundaries.
  370. */
  371. void __init free_bootmem(unsigned long addr, unsigned long size)
  372. {
  373. #ifdef CONFIG_NO_BOOTMEM
  374. free_early(addr, addr + size);
  375. #else
  376. unsigned long start, end;
  377. kmemleak_free_part(__va(addr), size);
  378. start = PFN_UP(addr);
  379. end = PFN_DOWN(addr + size);
  380. mark_bootmem(start, end, 0, 0);
  381. #endif
  382. }
  383. /**
  384. * reserve_bootmem_node - mark a page range as reserved
  385. * @pgdat: node the range resides on
  386. * @physaddr: starting address of the range
  387. * @size: size of the range in bytes
  388. * @flags: reservation flags (see linux/bootmem.h)
  389. *
  390. * Partial pages will be reserved.
  391. *
  392. * The range must reside completely on the specified node.
  393. */
  394. int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  395. unsigned long size, int flags)
  396. {
  397. #ifdef CONFIG_NO_BOOTMEM
  398. panic("no bootmem");
  399. return 0;
  400. #else
  401. unsigned long start, end;
  402. start = PFN_DOWN(physaddr);
  403. end = PFN_UP(physaddr + size);
  404. return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
  405. #endif
  406. }
  407. /**
  408. * reserve_bootmem - mark a page range as usable
  409. * @addr: starting address of the range
  410. * @size: size of the range in bytes
  411. * @flags: reservation flags (see linux/bootmem.h)
  412. *
  413. * Partial pages will be reserved.
  414. *
  415. * The range must be contiguous but may span node boundaries.
  416. */
  417. int __init reserve_bootmem(unsigned long addr, unsigned long size,
  418. int flags)
  419. {
  420. #ifdef CONFIG_NO_BOOTMEM
  421. panic("no bootmem");
  422. return 0;
  423. #else
  424. unsigned long start, end;
  425. start = PFN_DOWN(addr);
  426. end = PFN_UP(addr + size);
  427. return mark_bootmem(start, end, 1, flags);
  428. #endif
  429. }
  430. #ifndef CONFIG_NO_BOOTMEM
  431. static unsigned long __init align_idx(struct bootmem_data *bdata,
  432. unsigned long idx, unsigned long step)
  433. {
  434. unsigned long base = bdata->node_min_pfn;
  435. /*
  436. * Align the index with respect to the node start so that the
  437. * combination of both satisfies the requested alignment.
  438. */
  439. return ALIGN(base + idx, step) - base;
  440. }
  441. static unsigned long __init align_off(struct bootmem_data *bdata,
  442. unsigned long off, unsigned long align)
  443. {
  444. unsigned long base = PFN_PHYS(bdata->node_min_pfn);
  445. /* Same as align_idx for byte offsets */
  446. return ALIGN(base + off, align) - base;
  447. }
  448. static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
  449. unsigned long size, unsigned long align,
  450. unsigned long goal, unsigned long limit)
  451. {
  452. unsigned long fallback = 0;
  453. unsigned long min, max, start, sidx, midx, step;
  454. bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
  455. bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
  456. align, goal, limit);
  457. BUG_ON(!size);
  458. BUG_ON(align & (align - 1));
  459. BUG_ON(limit && goal + size > limit);
  460. if (!bdata->node_bootmem_map)
  461. return NULL;
  462. min = bdata->node_min_pfn;
  463. max = bdata->node_low_pfn;
  464. goal >>= PAGE_SHIFT;
  465. limit >>= PAGE_SHIFT;
  466. if (limit && max > limit)
  467. max = limit;
  468. if (max <= min)
  469. return NULL;
  470. step = max(align >> PAGE_SHIFT, 1UL);
  471. if (goal && min < goal && goal < max)
  472. start = ALIGN(goal, step);
  473. else
  474. start = ALIGN(min, step);
  475. sidx = start - bdata->node_min_pfn;
  476. midx = max - bdata->node_min_pfn;
  477. if (bdata->hint_idx > sidx) {
  478. /*
  479. * Handle the valid case of sidx being zero and still
  480. * catch the fallback below.
  481. */
  482. fallback = sidx + 1;
  483. sidx = align_idx(bdata, bdata->hint_idx, step);
  484. }
  485. while (1) {
  486. int merge;
  487. void *region;
  488. unsigned long eidx, i, start_off, end_off;
  489. find_block:
  490. sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
  491. sidx = align_idx(bdata, sidx, step);
  492. eidx = sidx + PFN_UP(size);
  493. if (sidx >= midx || eidx > midx)
  494. break;
  495. for (i = sidx; i < eidx; i++)
  496. if (test_bit(i, bdata->node_bootmem_map)) {
  497. sidx = align_idx(bdata, i, step);
  498. if (sidx == i)
  499. sidx += step;
  500. goto find_block;
  501. }
  502. if (bdata->last_end_off & (PAGE_SIZE - 1) &&
  503. PFN_DOWN(bdata->last_end_off) + 1 == sidx)
  504. start_off = align_off(bdata, bdata->last_end_off, align);
  505. else
  506. start_off = PFN_PHYS(sidx);
  507. merge = PFN_DOWN(start_off) < sidx;
  508. end_off = start_off + size;
  509. bdata->last_end_off = end_off;
  510. bdata->hint_idx = PFN_UP(end_off);
  511. /*
  512. * Reserve the area now:
  513. */
  514. if (__reserve(bdata, PFN_DOWN(start_off) + merge,
  515. PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
  516. BUG();
  517. region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
  518. start_off);
  519. memset(region, 0, size);
  520. /*
  521. * The min_count is set to 0 so that bootmem allocated blocks
  522. * are never reported as leaks.
  523. */
  524. kmemleak_alloc(region, size, 0, 0);
  525. return region;
  526. }
  527. if (fallback) {
  528. sidx = align_idx(bdata, fallback - 1, step);
  529. fallback = 0;
  530. goto find_block;
  531. }
  532. return NULL;
  533. }
  534. static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
  535. unsigned long size, unsigned long align,
  536. unsigned long goal, unsigned long limit)
  537. {
  538. if (WARN_ON_ONCE(slab_is_available()))
  539. return kzalloc(size, GFP_NOWAIT);
  540. #ifdef CONFIG_HAVE_ARCH_BOOTMEM
  541. {
  542. bootmem_data_t *p_bdata;
  543. p_bdata = bootmem_arch_preferred_node(bdata, size, align,
  544. goal, limit);
  545. if (p_bdata)
  546. return alloc_bootmem_core(p_bdata, size, align,
  547. goal, limit);
  548. }
  549. #endif
  550. return NULL;
  551. }
  552. #endif
  553. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  554. unsigned long align,
  555. unsigned long goal,
  556. unsigned long limit)
  557. {
  558. #ifdef CONFIG_NO_BOOTMEM
  559. void *ptr;
  560. if (WARN_ON_ONCE(slab_is_available()))
  561. return kzalloc(size, GFP_NOWAIT);
  562. restart:
  563. ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
  564. if (ptr)
  565. return ptr;
  566. if (goal != 0) {
  567. goal = 0;
  568. goto restart;
  569. }
  570. return NULL;
  571. #else
  572. bootmem_data_t *bdata;
  573. void *region;
  574. restart:
  575. region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
  576. if (region)
  577. return region;
  578. list_for_each_entry(bdata, &bdata_list, list) {
  579. if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
  580. continue;
  581. if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
  582. break;
  583. region = alloc_bootmem_core(bdata, size, align, goal, limit);
  584. if (region)
  585. return region;
  586. }
  587. if (goal) {
  588. goal = 0;
  589. goto restart;
  590. }
  591. return NULL;
  592. #endif
  593. }
  594. /**
  595. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  596. * @size: size of the request in bytes
  597. * @align: alignment of the region
  598. * @goal: preferred starting address of the region
  599. *
  600. * The goal is dropped if it can not be satisfied and the allocation will
  601. * fall back to memory below @goal.
  602. *
  603. * Allocation may happen on any node in the system.
  604. *
  605. * Returns NULL on failure.
  606. */
  607. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  608. unsigned long goal)
  609. {
  610. unsigned long limit = 0;
  611. #ifdef CONFIG_NO_BOOTMEM
  612. limit = -1UL;
  613. #endif
  614. return ___alloc_bootmem_nopanic(size, align, goal, limit);
  615. }
  616. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  617. unsigned long goal, unsigned long limit)
  618. {
  619. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  620. if (mem)
  621. return mem;
  622. /*
  623. * Whoops, we cannot satisfy the allocation request.
  624. */
  625. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  626. panic("Out of memory");
  627. return NULL;
  628. }
  629. /**
  630. * __alloc_bootmem - allocate boot memory
  631. * @size: size of the request in bytes
  632. * @align: alignment of the region
  633. * @goal: preferred starting address of the region
  634. *
  635. * The goal is dropped if it can not be satisfied and the allocation will
  636. * fall back to memory below @goal.
  637. *
  638. * Allocation may happen on any node in the system.
  639. *
  640. * The function panics if the request can not be satisfied.
  641. */
  642. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  643. unsigned long goal)
  644. {
  645. unsigned long limit = 0;
  646. #ifdef CONFIG_NO_BOOTMEM
  647. limit = -1UL;
  648. #endif
  649. return ___alloc_bootmem(size, align, goal, limit);
  650. }
  651. #ifndef CONFIG_NO_BOOTMEM
  652. static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
  653. unsigned long size, unsigned long align,
  654. unsigned long goal, unsigned long limit)
  655. {
  656. void *ptr;
  657. ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
  658. if (ptr)
  659. return ptr;
  660. ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
  661. if (ptr)
  662. return ptr;
  663. return ___alloc_bootmem(size, align, goal, limit);
  664. }
  665. #endif
  666. /**
  667. * __alloc_bootmem_node - allocate boot memory from a specific node
  668. * @pgdat: node to allocate from
  669. * @size: size of the request in bytes
  670. * @align: alignment of the region
  671. * @goal: preferred starting address of the region
  672. *
  673. * The goal is dropped if it can not be satisfied and the allocation will
  674. * fall back to memory below @goal.
  675. *
  676. * Allocation may fall back to any node in the system if the specified node
  677. * can not hold the requested memory.
  678. *
  679. * The function panics if the request can not be satisfied.
  680. */
  681. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  682. unsigned long align, unsigned long goal)
  683. {
  684. if (WARN_ON_ONCE(slab_is_available()))
  685. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  686. #ifdef CONFIG_NO_BOOTMEM
  687. return __alloc_memory_core_early(pgdat->node_id, size, align,
  688. goal, -1ULL);
  689. #else
  690. return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
  691. #endif
  692. }
  693. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  694. unsigned long align, unsigned long goal)
  695. {
  696. #ifdef MAX_DMA32_PFN
  697. unsigned long end_pfn;
  698. if (WARN_ON_ONCE(slab_is_available()))
  699. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  700. /* update goal according ...MAX_DMA32_PFN */
  701. end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  702. if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
  703. (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
  704. void *ptr;
  705. unsigned long new_goal;
  706. new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
  707. #ifdef CONFIG_NO_BOOTMEM
  708. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  709. new_goal, -1ULL);
  710. #else
  711. ptr = alloc_bootmem_core(pgdat->bdata, size, align,
  712. new_goal, 0);
  713. #endif
  714. if (ptr)
  715. return ptr;
  716. }
  717. #endif
  718. return __alloc_bootmem_node(pgdat, size, align, goal);
  719. }
  720. #ifdef CONFIG_SPARSEMEM
  721. /**
  722. * alloc_bootmem_section - allocate boot memory from a specific section
  723. * @size: size of the request in bytes
  724. * @section_nr: sparse map section to allocate from
  725. *
  726. * Return NULL on failure.
  727. */
  728. void * __init alloc_bootmem_section(unsigned long size,
  729. unsigned long section_nr)
  730. {
  731. #ifdef CONFIG_NO_BOOTMEM
  732. unsigned long pfn, goal, limit;
  733. pfn = section_nr_to_pfn(section_nr);
  734. goal = pfn << PAGE_SHIFT;
  735. limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
  736. return __alloc_memory_core_early(early_pfn_to_nid(pfn), size,
  737. SMP_CACHE_BYTES, goal, limit);
  738. #else
  739. bootmem_data_t *bdata;
  740. unsigned long pfn, goal, limit;
  741. pfn = section_nr_to_pfn(section_nr);
  742. goal = pfn << PAGE_SHIFT;
  743. limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
  744. bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
  745. return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
  746. #endif
  747. }
  748. #endif
  749. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  750. unsigned long align, unsigned long goal)
  751. {
  752. void *ptr;
  753. if (WARN_ON_ONCE(slab_is_available()))
  754. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  755. #ifdef CONFIG_NO_BOOTMEM
  756. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  757. goal, -1ULL);
  758. #else
  759. ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
  760. if (ptr)
  761. return ptr;
  762. ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
  763. #endif
  764. if (ptr)
  765. return ptr;
  766. return __alloc_bootmem_nopanic(size, align, goal);
  767. }
  768. #ifndef ARCH_LOW_ADDRESS_LIMIT
  769. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  770. #endif
  771. /**
  772. * __alloc_bootmem_low - allocate low boot memory
  773. * @size: size of the request in bytes
  774. * @align: alignment of the region
  775. * @goal: preferred starting address of the region
  776. *
  777. * The goal is dropped if it can not be satisfied and the allocation will
  778. * fall back to memory below @goal.
  779. *
  780. * Allocation may happen on any node in the system.
  781. *
  782. * The function panics if the request can not be satisfied.
  783. */
  784. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  785. unsigned long goal)
  786. {
  787. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  788. }
  789. /**
  790. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  791. * @pgdat: node to allocate from
  792. * @size: size of the request in bytes
  793. * @align: alignment of the region
  794. * @goal: preferred starting address of the region
  795. *
  796. * The goal is dropped if it can not be satisfied and the allocation will
  797. * fall back to memory below @goal.
  798. *
  799. * Allocation may fall back to any node in the system if the specified node
  800. * can not hold the requested memory.
  801. *
  802. * The function panics if the request can not be satisfied.
  803. */
  804. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  805. unsigned long align, unsigned long goal)
  806. {
  807. if (WARN_ON_ONCE(slab_is_available()))
  808. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  809. #ifdef CONFIG_NO_BOOTMEM
  810. return __alloc_memory_core_early(pgdat->node_id, size, align,
  811. goal, ARCH_LOW_ADDRESS_LIMIT);
  812. #else
  813. return ___alloc_bootmem_node(pgdat->bdata, size, align,
  814. goal, ARCH_LOW_ADDRESS_LIMIT);
  815. #endif
  816. }