bootmem.c 24 KB

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