bootmem.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983
  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. /*
  256. * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id
  257. * because in some case like Node0 doesnt have RAM installed
  258. * low ram will be on Node1
  259. * Use MAX_NUMNODES will make sure all ranges in early_node_map[]
  260. * will be used instead of only Node0 related
  261. */
  262. return free_all_memory_core_early(MAX_NUMNODES);
  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. #else
  362. unsigned long start, end;
  363. kmemleak_free_part(__va(physaddr), size);
  364. start = PFN_UP(physaddr);
  365. end = PFN_DOWN(physaddr + size);
  366. mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
  367. #endif
  368. }
  369. /**
  370. * free_bootmem - mark a page range as usable
  371. * @addr: starting address of the range
  372. * @size: size of the range in bytes
  373. *
  374. * Partial pages will be considered reserved and left as they are.
  375. *
  376. * The range must be contiguous but may span node boundaries.
  377. */
  378. void __init free_bootmem(unsigned long addr, unsigned long size)
  379. {
  380. #ifdef CONFIG_NO_BOOTMEM
  381. free_early(addr, addr + size);
  382. #else
  383. unsigned long start, end;
  384. kmemleak_free_part(__va(addr), size);
  385. start = PFN_UP(addr);
  386. end = PFN_DOWN(addr + size);
  387. mark_bootmem(start, end, 0, 0);
  388. #endif
  389. }
  390. /**
  391. * reserve_bootmem_node - mark a page range as reserved
  392. * @pgdat: node the range resides on
  393. * @physaddr: starting address of the range
  394. * @size: size of the range in bytes
  395. * @flags: reservation flags (see linux/bootmem.h)
  396. *
  397. * Partial pages will be reserved.
  398. *
  399. * The range must reside completely on the specified node.
  400. */
  401. int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  402. unsigned long size, int flags)
  403. {
  404. #ifdef CONFIG_NO_BOOTMEM
  405. panic("no bootmem");
  406. return 0;
  407. #else
  408. unsigned long start, end;
  409. start = PFN_DOWN(physaddr);
  410. end = PFN_UP(physaddr + size);
  411. return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
  412. #endif
  413. }
  414. /**
  415. * reserve_bootmem - mark a page range as usable
  416. * @addr: starting address of the range
  417. * @size: size of the range in bytes
  418. * @flags: reservation flags (see linux/bootmem.h)
  419. *
  420. * Partial pages will be reserved.
  421. *
  422. * The range must be contiguous but may span node boundaries.
  423. */
  424. int __init reserve_bootmem(unsigned long addr, unsigned long size,
  425. int flags)
  426. {
  427. #ifdef CONFIG_NO_BOOTMEM
  428. panic("no bootmem");
  429. return 0;
  430. #else
  431. unsigned long start, end;
  432. start = PFN_DOWN(addr);
  433. end = PFN_UP(addr + size);
  434. return mark_bootmem(start, end, 1, flags);
  435. #endif
  436. }
  437. #ifndef CONFIG_NO_BOOTMEM
  438. static unsigned long __init align_idx(struct bootmem_data *bdata,
  439. unsigned long idx, unsigned long step)
  440. {
  441. unsigned long base = bdata->node_min_pfn;
  442. /*
  443. * Align the index with respect to the node start so that the
  444. * combination of both satisfies the requested alignment.
  445. */
  446. return ALIGN(base + idx, step) - base;
  447. }
  448. static unsigned long __init align_off(struct bootmem_data *bdata,
  449. unsigned long off, unsigned long align)
  450. {
  451. unsigned long base = PFN_PHYS(bdata->node_min_pfn);
  452. /* Same as align_idx for byte offsets */
  453. return ALIGN(base + off, align) - base;
  454. }
  455. static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
  456. unsigned long size, unsigned long align,
  457. unsigned long goal, unsigned long limit)
  458. {
  459. unsigned long fallback = 0;
  460. unsigned long min, max, start, sidx, midx, step;
  461. bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
  462. bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
  463. align, goal, limit);
  464. BUG_ON(!size);
  465. BUG_ON(align & (align - 1));
  466. BUG_ON(limit && goal + size > limit);
  467. if (!bdata->node_bootmem_map)
  468. return NULL;
  469. min = bdata->node_min_pfn;
  470. max = bdata->node_low_pfn;
  471. goal >>= PAGE_SHIFT;
  472. limit >>= PAGE_SHIFT;
  473. if (limit && max > limit)
  474. max = limit;
  475. if (max <= min)
  476. return NULL;
  477. step = max(align >> PAGE_SHIFT, 1UL);
  478. if (goal && min < goal && goal < max)
  479. start = ALIGN(goal, step);
  480. else
  481. start = ALIGN(min, step);
  482. sidx = start - bdata->node_min_pfn;
  483. midx = max - bdata->node_min_pfn;
  484. if (bdata->hint_idx > sidx) {
  485. /*
  486. * Handle the valid case of sidx being zero and still
  487. * catch the fallback below.
  488. */
  489. fallback = sidx + 1;
  490. sidx = align_idx(bdata, bdata->hint_idx, step);
  491. }
  492. while (1) {
  493. int merge;
  494. void *region;
  495. unsigned long eidx, i, start_off, end_off;
  496. find_block:
  497. sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
  498. sidx = align_idx(bdata, sidx, step);
  499. eidx = sidx + PFN_UP(size);
  500. if (sidx >= midx || eidx > midx)
  501. break;
  502. for (i = sidx; i < eidx; i++)
  503. if (test_bit(i, bdata->node_bootmem_map)) {
  504. sidx = align_idx(bdata, i, step);
  505. if (sidx == i)
  506. sidx += step;
  507. goto find_block;
  508. }
  509. if (bdata->last_end_off & (PAGE_SIZE - 1) &&
  510. PFN_DOWN(bdata->last_end_off) + 1 == sidx)
  511. start_off = align_off(bdata, bdata->last_end_off, align);
  512. else
  513. start_off = PFN_PHYS(sidx);
  514. merge = PFN_DOWN(start_off) < sidx;
  515. end_off = start_off + size;
  516. bdata->last_end_off = end_off;
  517. bdata->hint_idx = PFN_UP(end_off);
  518. /*
  519. * Reserve the area now:
  520. */
  521. if (__reserve(bdata, PFN_DOWN(start_off) + merge,
  522. PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
  523. BUG();
  524. region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
  525. start_off);
  526. memset(region, 0, size);
  527. /*
  528. * The min_count is set to 0 so that bootmem allocated blocks
  529. * are never reported as leaks.
  530. */
  531. kmemleak_alloc(region, size, 0, 0);
  532. return region;
  533. }
  534. if (fallback) {
  535. sidx = align_idx(bdata, fallback - 1, step);
  536. fallback = 0;
  537. goto find_block;
  538. }
  539. return NULL;
  540. }
  541. static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
  542. unsigned long size, unsigned long align,
  543. unsigned long goal, unsigned long limit)
  544. {
  545. if (WARN_ON_ONCE(slab_is_available()))
  546. return kzalloc(size, GFP_NOWAIT);
  547. #ifdef CONFIG_HAVE_ARCH_BOOTMEM
  548. {
  549. bootmem_data_t *p_bdata;
  550. p_bdata = bootmem_arch_preferred_node(bdata, size, align,
  551. goal, limit);
  552. if (p_bdata)
  553. return alloc_bootmem_core(p_bdata, size, align,
  554. goal, limit);
  555. }
  556. #endif
  557. return NULL;
  558. }
  559. #endif
  560. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  561. unsigned long align,
  562. unsigned long goal,
  563. unsigned long limit)
  564. {
  565. #ifdef CONFIG_NO_BOOTMEM
  566. void *ptr;
  567. if (WARN_ON_ONCE(slab_is_available()))
  568. return kzalloc(size, GFP_NOWAIT);
  569. restart:
  570. ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
  571. if (ptr)
  572. return ptr;
  573. if (goal != 0) {
  574. goal = 0;
  575. goto restart;
  576. }
  577. return NULL;
  578. #else
  579. bootmem_data_t *bdata;
  580. void *region;
  581. restart:
  582. region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
  583. if (region)
  584. return region;
  585. list_for_each_entry(bdata, &bdata_list, list) {
  586. if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
  587. continue;
  588. if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
  589. break;
  590. region = alloc_bootmem_core(bdata, size, align, goal, limit);
  591. if (region)
  592. return region;
  593. }
  594. if (goal) {
  595. goal = 0;
  596. goto restart;
  597. }
  598. return NULL;
  599. #endif
  600. }
  601. /**
  602. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  603. * @size: size of the request in bytes
  604. * @align: alignment of the region
  605. * @goal: preferred starting address of the region
  606. *
  607. * The goal is dropped if it can not be satisfied and the allocation will
  608. * fall back to memory below @goal.
  609. *
  610. * Allocation may happen on any node in the system.
  611. *
  612. * Returns NULL on failure.
  613. */
  614. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  615. unsigned long goal)
  616. {
  617. unsigned long limit = 0;
  618. #ifdef CONFIG_NO_BOOTMEM
  619. limit = -1UL;
  620. #endif
  621. return ___alloc_bootmem_nopanic(size, align, goal, limit);
  622. }
  623. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  624. unsigned long goal, unsigned long limit)
  625. {
  626. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  627. if (mem)
  628. return mem;
  629. /*
  630. * Whoops, we cannot satisfy the allocation request.
  631. */
  632. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  633. panic("Out of memory");
  634. return NULL;
  635. }
  636. /**
  637. * __alloc_bootmem - allocate 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(unsigned long size, unsigned long align,
  650. unsigned long goal)
  651. {
  652. unsigned long limit = 0;
  653. #ifdef CONFIG_NO_BOOTMEM
  654. limit = -1UL;
  655. #endif
  656. return ___alloc_bootmem(size, align, goal, limit);
  657. }
  658. #ifndef CONFIG_NO_BOOTMEM
  659. static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
  660. unsigned long size, unsigned long align,
  661. unsigned long goal, unsigned long limit)
  662. {
  663. void *ptr;
  664. ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
  665. if (ptr)
  666. return ptr;
  667. ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
  668. if (ptr)
  669. return ptr;
  670. return ___alloc_bootmem(size, align, goal, limit);
  671. }
  672. #endif
  673. /**
  674. * __alloc_bootmem_node - allocate boot memory from a specific node
  675. * @pgdat: node to allocate from
  676. * @size: size of the request in bytes
  677. * @align: alignment of the region
  678. * @goal: preferred starting address of the region
  679. *
  680. * The goal is dropped if it can not be satisfied and the allocation will
  681. * fall back to memory below @goal.
  682. *
  683. * Allocation may fall back to any node in the system if the specified node
  684. * can not hold the requested memory.
  685. *
  686. * The function panics if the request can not be satisfied.
  687. */
  688. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  689. unsigned long align, unsigned long goal)
  690. {
  691. if (WARN_ON_ONCE(slab_is_available()))
  692. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  693. #ifdef CONFIG_NO_BOOTMEM
  694. return __alloc_memory_core_early(pgdat->node_id, size, align,
  695. goal, -1ULL);
  696. #else
  697. return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
  698. #endif
  699. }
  700. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  701. unsigned long align, unsigned long goal)
  702. {
  703. #ifdef MAX_DMA32_PFN
  704. unsigned long end_pfn;
  705. if (WARN_ON_ONCE(slab_is_available()))
  706. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  707. /* update goal according ...MAX_DMA32_PFN */
  708. end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  709. if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
  710. (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
  711. void *ptr;
  712. unsigned long new_goal;
  713. new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
  714. #ifdef CONFIG_NO_BOOTMEM
  715. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  716. new_goal, -1ULL);
  717. #else
  718. ptr = alloc_bootmem_core(pgdat->bdata, size, align,
  719. new_goal, 0);
  720. #endif
  721. if (ptr)
  722. return ptr;
  723. }
  724. #endif
  725. return __alloc_bootmem_node(pgdat, size, align, goal);
  726. }
  727. #ifdef CONFIG_SPARSEMEM
  728. /**
  729. * alloc_bootmem_section - allocate boot memory from a specific section
  730. * @size: size of the request in bytes
  731. * @section_nr: sparse map section to allocate from
  732. *
  733. * Return NULL on failure.
  734. */
  735. void * __init alloc_bootmem_section(unsigned long size,
  736. unsigned long section_nr)
  737. {
  738. #ifdef CONFIG_NO_BOOTMEM
  739. unsigned long pfn, goal, limit;
  740. pfn = section_nr_to_pfn(section_nr);
  741. goal = pfn << PAGE_SHIFT;
  742. limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
  743. return __alloc_memory_core_early(early_pfn_to_nid(pfn), size,
  744. SMP_CACHE_BYTES, goal, limit);
  745. #else
  746. bootmem_data_t *bdata;
  747. unsigned long pfn, goal, limit;
  748. pfn = section_nr_to_pfn(section_nr);
  749. goal = pfn << PAGE_SHIFT;
  750. limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
  751. bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
  752. return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
  753. #endif
  754. }
  755. #endif
  756. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  757. unsigned long align, unsigned long goal)
  758. {
  759. void *ptr;
  760. if (WARN_ON_ONCE(slab_is_available()))
  761. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  762. #ifdef CONFIG_NO_BOOTMEM
  763. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  764. goal, -1ULL);
  765. #else
  766. ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
  767. if (ptr)
  768. return ptr;
  769. ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
  770. #endif
  771. if (ptr)
  772. return ptr;
  773. return __alloc_bootmem_nopanic(size, align, goal);
  774. }
  775. #ifndef ARCH_LOW_ADDRESS_LIMIT
  776. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  777. #endif
  778. /**
  779. * __alloc_bootmem_low - allocate low boot memory
  780. * @size: size of the request in bytes
  781. * @align: alignment of the region
  782. * @goal: preferred starting address of the region
  783. *
  784. * The goal is dropped if it can not be satisfied and the allocation will
  785. * fall back to memory below @goal.
  786. *
  787. * Allocation may happen on any node in the system.
  788. *
  789. * The function panics if the request can not be satisfied.
  790. */
  791. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  792. unsigned long goal)
  793. {
  794. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  795. }
  796. /**
  797. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  798. * @pgdat: node to allocate from
  799. * @size: size of the request in bytes
  800. * @align: alignment of the region
  801. * @goal: preferred starting address of the region
  802. *
  803. * The goal is dropped if it can not be satisfied and the allocation will
  804. * fall back to memory below @goal.
  805. *
  806. * Allocation may fall back to any node in the system if the specified node
  807. * can not hold the requested memory.
  808. *
  809. * The function panics if the request can not be satisfied.
  810. */
  811. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  812. unsigned long align, unsigned long goal)
  813. {
  814. if (WARN_ON_ONCE(slab_is_available()))
  815. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  816. #ifdef CONFIG_NO_BOOTMEM
  817. return __alloc_memory_core_early(pgdat->node_id, size, align,
  818. goal, ARCH_LOW_ADDRESS_LIMIT);
  819. #else
  820. return ___alloc_bootmem_node(pgdat->bdata, size, align,
  821. goal, ARCH_LOW_ADDRESS_LIMIT);
  822. #endif
  823. }