bootmem.c 24 KB

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