bootmem.c 24 KB

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