bootmem.c 20 KB

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