bootmem.c 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  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. static unsigned long __init align_idx(struct bootmem_data *bdata,
  444. unsigned long idx, unsigned long step)
  445. {
  446. unsigned long base = bdata->node_min_pfn;
  447. /*
  448. * Align the index with respect to the node start so that the
  449. * combination of both satisfies the requested alignment.
  450. */
  451. return ALIGN(base + idx, step) - base;
  452. }
  453. static unsigned long __init align_off(struct bootmem_data *bdata,
  454. unsigned long off, unsigned long align)
  455. {
  456. unsigned long base = PFN_PHYS(bdata->node_min_pfn);
  457. /* Same as align_idx for byte offsets */
  458. return ALIGN(base + off, align) - base;
  459. }
  460. static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
  461. unsigned long size, unsigned long align,
  462. unsigned long goal, unsigned long limit)
  463. {
  464. unsigned long fallback = 0;
  465. unsigned long min, max, start, sidx, midx, step;
  466. bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
  467. bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
  468. align, goal, limit);
  469. BUG_ON(!size);
  470. BUG_ON(align & (align - 1));
  471. BUG_ON(limit && goal + size > limit);
  472. if (!bdata->node_bootmem_map)
  473. return NULL;
  474. min = bdata->node_min_pfn;
  475. max = bdata->node_low_pfn;
  476. goal >>= PAGE_SHIFT;
  477. limit >>= PAGE_SHIFT;
  478. if (limit && max > limit)
  479. max = limit;
  480. if (max <= min)
  481. return NULL;
  482. step = max(align >> PAGE_SHIFT, 1UL);
  483. if (goal && min < goal && goal < max)
  484. start = ALIGN(goal, step);
  485. else
  486. start = ALIGN(min, step);
  487. sidx = start - bdata->node_min_pfn;
  488. midx = max - bdata->node_min_pfn;
  489. if (bdata->hint_idx > sidx) {
  490. /*
  491. * Handle the valid case of sidx being zero and still
  492. * catch the fallback below.
  493. */
  494. fallback = sidx + 1;
  495. sidx = align_idx(bdata, bdata->hint_idx, step);
  496. }
  497. while (1) {
  498. int merge;
  499. void *region;
  500. unsigned long eidx, i, start_off, end_off;
  501. find_block:
  502. sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
  503. sidx = align_idx(bdata, sidx, step);
  504. eidx = sidx + PFN_UP(size);
  505. if (sidx >= midx || eidx > midx)
  506. break;
  507. for (i = sidx; i < eidx; i++)
  508. if (test_bit(i, bdata->node_bootmem_map)) {
  509. sidx = align_idx(bdata, i, step);
  510. if (sidx == i)
  511. sidx += step;
  512. goto find_block;
  513. }
  514. if (bdata->last_end_off & (PAGE_SIZE - 1) &&
  515. PFN_DOWN(bdata->last_end_off) + 1 == sidx)
  516. start_off = align_off(bdata, bdata->last_end_off, align);
  517. else
  518. start_off = PFN_PHYS(sidx);
  519. merge = PFN_DOWN(start_off) < sidx;
  520. end_off = start_off + size;
  521. bdata->last_end_off = end_off;
  522. bdata->hint_idx = PFN_UP(end_off);
  523. /*
  524. * Reserve the area now:
  525. */
  526. if (__reserve(bdata, PFN_DOWN(start_off) + merge,
  527. PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
  528. BUG();
  529. region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
  530. start_off);
  531. memset(region, 0, size);
  532. /*
  533. * The min_count is set to 0 so that bootmem allocated blocks
  534. * are never reported as leaks.
  535. */
  536. kmemleak_alloc(region, size, 0, 0);
  537. return region;
  538. }
  539. if (fallback) {
  540. sidx = align_idx(bdata, fallback - 1, step);
  541. fallback = 0;
  542. goto find_block;
  543. }
  544. return NULL;
  545. }
  546. static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
  547. unsigned long size, unsigned long align,
  548. unsigned long goal, unsigned long limit)
  549. {
  550. if (WARN_ON_ONCE(slab_is_available()))
  551. return kzalloc(size, GFP_NOWAIT);
  552. #ifdef CONFIG_HAVE_ARCH_BOOTMEM
  553. {
  554. bootmem_data_t *p_bdata;
  555. p_bdata = bootmem_arch_preferred_node(bdata, size, align,
  556. goal, limit);
  557. if (p_bdata)
  558. return alloc_bootmem_core(p_bdata, size, align,
  559. goal, limit);
  560. }
  561. #endif
  562. return NULL;
  563. }
  564. #endif
  565. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  566. unsigned long align,
  567. unsigned long goal,
  568. unsigned long limit)
  569. {
  570. #ifdef CONFIG_NO_BOOTMEM
  571. void *ptr;
  572. if (WARN_ON_ONCE(slab_is_available()))
  573. return kzalloc(size, GFP_NOWAIT);
  574. restart:
  575. ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
  576. if (ptr)
  577. return ptr;
  578. if (goal != 0) {
  579. goal = 0;
  580. goto restart;
  581. }
  582. return NULL;
  583. #else
  584. bootmem_data_t *bdata;
  585. void *region;
  586. restart:
  587. region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
  588. if (region)
  589. return region;
  590. list_for_each_entry(bdata, &bdata_list, list) {
  591. if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
  592. continue;
  593. if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
  594. break;
  595. region = alloc_bootmem_core(bdata, size, align, goal, limit);
  596. if (region)
  597. return region;
  598. }
  599. if (goal) {
  600. goal = 0;
  601. goto restart;
  602. }
  603. return NULL;
  604. #endif
  605. }
  606. /**
  607. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  608. * @size: size of the request in bytes
  609. * @align: alignment of the region
  610. * @goal: preferred starting address of the region
  611. *
  612. * The goal is dropped if it can not be satisfied and the allocation will
  613. * fall back to memory below @goal.
  614. *
  615. * Allocation may happen on any node in the system.
  616. *
  617. * Returns NULL on failure.
  618. */
  619. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  620. unsigned long goal)
  621. {
  622. unsigned long limit = 0;
  623. #ifdef CONFIG_NO_BOOTMEM
  624. limit = -1UL;
  625. #endif
  626. return ___alloc_bootmem_nopanic(size, align, goal, limit);
  627. }
  628. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  629. unsigned long goal, unsigned long limit)
  630. {
  631. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  632. if (mem)
  633. return mem;
  634. /*
  635. * Whoops, we cannot satisfy the allocation request.
  636. */
  637. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  638. panic("Out of memory");
  639. return NULL;
  640. }
  641. /**
  642. * __alloc_bootmem - allocate boot memory
  643. * @size: size of the request in bytes
  644. * @align: alignment of the region
  645. * @goal: preferred starting address of the region
  646. *
  647. * The goal is dropped if it can not be satisfied and the allocation will
  648. * fall back to memory below @goal.
  649. *
  650. * Allocation may happen on any node in the system.
  651. *
  652. * The function panics if the request can not be satisfied.
  653. */
  654. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  655. unsigned long goal)
  656. {
  657. unsigned long limit = 0;
  658. #ifdef CONFIG_NO_BOOTMEM
  659. limit = -1UL;
  660. #endif
  661. return ___alloc_bootmem(size, align, goal, limit);
  662. }
  663. #ifndef CONFIG_NO_BOOTMEM
  664. static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
  665. unsigned long size, unsigned long align,
  666. unsigned long goal, unsigned long limit)
  667. {
  668. void *ptr;
  669. ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
  670. if (ptr)
  671. return ptr;
  672. ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
  673. if (ptr)
  674. return ptr;
  675. return ___alloc_bootmem(size, align, goal, limit);
  676. }
  677. #endif
  678. /**
  679. * __alloc_bootmem_node - allocate boot memory from a specific node
  680. * @pgdat: node to allocate from
  681. * @size: size of the request in bytes
  682. * @align: alignment of the region
  683. * @goal: preferred starting address of the region
  684. *
  685. * The goal is dropped if it can not be satisfied and the allocation will
  686. * fall back to memory below @goal.
  687. *
  688. * Allocation may fall back to any node in the system if the specified node
  689. * can not hold the requested memory.
  690. *
  691. * The function panics if the request can not be satisfied.
  692. */
  693. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  694. unsigned long align, unsigned long goal)
  695. {
  696. void *ptr;
  697. if (WARN_ON_ONCE(slab_is_available()))
  698. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  699. #ifdef CONFIG_NO_BOOTMEM
  700. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  701. goal, -1ULL);
  702. if (ptr)
  703. return ptr;
  704. ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align,
  705. goal, -1ULL);
  706. #else
  707. ptr = ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
  708. #endif
  709. return ptr;
  710. }
  711. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  712. unsigned long align, unsigned long goal)
  713. {
  714. #ifdef MAX_DMA32_PFN
  715. unsigned long end_pfn;
  716. if (WARN_ON_ONCE(slab_is_available()))
  717. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  718. /* update goal according ...MAX_DMA32_PFN */
  719. end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  720. if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
  721. (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
  722. void *ptr;
  723. unsigned long new_goal;
  724. new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
  725. #ifdef CONFIG_NO_BOOTMEM
  726. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  727. new_goal, -1ULL);
  728. #else
  729. ptr = alloc_bootmem_core(pgdat->bdata, size, align,
  730. new_goal, 0);
  731. #endif
  732. if (ptr)
  733. return ptr;
  734. }
  735. #endif
  736. return __alloc_bootmem_node(pgdat, size, align, goal);
  737. }
  738. #ifdef CONFIG_SPARSEMEM
  739. /**
  740. * alloc_bootmem_section - allocate boot memory from a specific section
  741. * @size: size of the request in bytes
  742. * @section_nr: sparse map section to allocate from
  743. *
  744. * Return NULL on failure.
  745. */
  746. void * __init alloc_bootmem_section(unsigned long size,
  747. unsigned long section_nr)
  748. {
  749. #ifdef CONFIG_NO_BOOTMEM
  750. unsigned long pfn, goal, limit;
  751. pfn = section_nr_to_pfn(section_nr);
  752. goal = pfn << PAGE_SHIFT;
  753. limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
  754. return __alloc_memory_core_early(early_pfn_to_nid(pfn), size,
  755. SMP_CACHE_BYTES, goal, limit);
  756. #else
  757. bootmem_data_t *bdata;
  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. bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
  763. return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
  764. #endif
  765. }
  766. #endif
  767. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  768. unsigned long align, unsigned long goal)
  769. {
  770. void *ptr;
  771. if (WARN_ON_ONCE(slab_is_available()))
  772. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  773. #ifdef CONFIG_NO_BOOTMEM
  774. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  775. goal, -1ULL);
  776. #else
  777. ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
  778. if (ptr)
  779. return ptr;
  780. ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
  781. #endif
  782. if (ptr)
  783. return ptr;
  784. return __alloc_bootmem_nopanic(size, align, goal);
  785. }
  786. #ifndef ARCH_LOW_ADDRESS_LIMIT
  787. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  788. #endif
  789. /**
  790. * __alloc_bootmem_low - allocate low boot memory
  791. * @size: size of the request in bytes
  792. * @align: alignment of the region
  793. * @goal: preferred starting address of the region
  794. *
  795. * The goal is dropped if it can not be satisfied and the allocation will
  796. * fall back to memory below @goal.
  797. *
  798. * Allocation may happen on any node in the system.
  799. *
  800. * The function panics if the request can not be satisfied.
  801. */
  802. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  803. unsigned long goal)
  804. {
  805. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  806. }
  807. /**
  808. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  809. * @pgdat: node to allocate from
  810. * @size: size of the request in bytes
  811. * @align: alignment of the region
  812. * @goal: preferred starting address of the region
  813. *
  814. * The goal is dropped if it can not be satisfied and the allocation will
  815. * fall back to memory below @goal.
  816. *
  817. * Allocation may fall back to any node in the system if the specified node
  818. * can not hold the requested memory.
  819. *
  820. * The function panics if the request can not be satisfied.
  821. */
  822. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  823. unsigned long align, unsigned long goal)
  824. {
  825. void *ptr;
  826. if (WARN_ON_ONCE(slab_is_available()))
  827. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  828. #ifdef CONFIG_NO_BOOTMEM
  829. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  830. goal, ARCH_LOW_ADDRESS_LIMIT);
  831. if (ptr)
  832. return ptr;
  833. ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align,
  834. goal, ARCH_LOW_ADDRESS_LIMIT);
  835. #else
  836. ptr = ___alloc_bootmem_node(pgdat->bdata, size, align,
  837. goal, ARCH_LOW_ADDRESS_LIMIT);
  838. #endif
  839. return ptr;
  840. }