bootmem.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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. static unsigned long __init free_all_bootmem_core(bootmem_data_t *bdata)
  123. {
  124. int aligned;
  125. struct page *page;
  126. unsigned long start, end, pages, count = 0;
  127. if (!bdata->node_bootmem_map)
  128. return 0;
  129. start = bdata->node_min_pfn;
  130. end = bdata->node_low_pfn;
  131. /*
  132. * If the start is aligned to the machines wordsize, we might
  133. * be able to free pages in bulks of that order.
  134. */
  135. aligned = !(start & (BITS_PER_LONG - 1));
  136. bdebug("nid=%td start=%lx end=%lx aligned=%d\n",
  137. bdata - bootmem_node_data, start, end, aligned);
  138. while (start < end) {
  139. unsigned long *map, idx, vec;
  140. map = bdata->node_bootmem_map;
  141. idx = start - bdata->node_min_pfn;
  142. vec = ~map[idx / BITS_PER_LONG];
  143. if (aligned && vec == ~0UL && start + BITS_PER_LONG < end) {
  144. int order = ilog2(BITS_PER_LONG);
  145. __free_pages_bootmem(pfn_to_page(start), order);
  146. count += BITS_PER_LONG;
  147. } else {
  148. unsigned long off = 0;
  149. while (vec && off < BITS_PER_LONG) {
  150. if (vec & 1) {
  151. page = pfn_to_page(start + off);
  152. __free_pages_bootmem(page, 0);
  153. count++;
  154. }
  155. vec >>= 1;
  156. off++;
  157. }
  158. }
  159. start += BITS_PER_LONG;
  160. }
  161. page = virt_to_page(bdata->node_bootmem_map);
  162. pages = bdata->node_low_pfn - bdata->node_min_pfn;
  163. pages = bootmem_bootmap_pages(pages);
  164. count += pages;
  165. while (pages--)
  166. __free_pages_bootmem(page++, 0);
  167. bdebug("nid=%td released=%lx\n", bdata - bootmem_node_data, count);
  168. return count;
  169. }
  170. /**
  171. * free_all_bootmem_node - release a node's free pages to the buddy allocator
  172. * @pgdat: node to be released
  173. *
  174. * Returns the number of pages actually released.
  175. */
  176. unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
  177. {
  178. register_page_bootmem_info_node(pgdat);
  179. return free_all_bootmem_core(pgdat->bdata);
  180. }
  181. /**
  182. * free_all_bootmem - release free pages to the buddy allocator
  183. *
  184. * Returns the number of pages actually released.
  185. */
  186. unsigned long __init free_all_bootmem(void)
  187. {
  188. return free_all_bootmem_core(NODE_DATA(0)->bdata);
  189. }
  190. static void __init __free(bootmem_data_t *bdata,
  191. unsigned long sidx, unsigned long eidx)
  192. {
  193. unsigned long idx;
  194. bdebug("nid=%td start=%lx end=%lx\n", bdata - bootmem_node_data,
  195. sidx + bdata->node_min_pfn,
  196. eidx + bdata->node_min_pfn);
  197. if (bdata->hint_idx > sidx)
  198. bdata->hint_idx = sidx;
  199. for (idx = sidx; idx < eidx; idx++)
  200. if (!test_and_clear_bit(idx, bdata->node_bootmem_map))
  201. BUG();
  202. }
  203. static int __init __reserve(bootmem_data_t *bdata, unsigned long sidx,
  204. unsigned long eidx, int flags)
  205. {
  206. unsigned long idx;
  207. int exclusive = flags & BOOTMEM_EXCLUSIVE;
  208. bdebug("nid=%td start=%lx end=%lx flags=%x\n",
  209. bdata - bootmem_node_data,
  210. sidx + bdata->node_min_pfn,
  211. eidx + bdata->node_min_pfn,
  212. flags);
  213. for (idx = sidx; idx < eidx; idx++)
  214. if (test_and_set_bit(idx, bdata->node_bootmem_map)) {
  215. if (exclusive) {
  216. __free(bdata, sidx, idx);
  217. return -EBUSY;
  218. }
  219. bdebug("silent double reserve of PFN %lx\n",
  220. idx + bdata->node_min_pfn);
  221. }
  222. return 0;
  223. }
  224. static int __init mark_bootmem_node(bootmem_data_t *bdata,
  225. unsigned long start, unsigned long end,
  226. int reserve, int flags)
  227. {
  228. unsigned long sidx, eidx;
  229. bdebug("nid=%td start=%lx end=%lx reserve=%d flags=%x\n",
  230. bdata - bootmem_node_data, start, end, reserve, flags);
  231. BUG_ON(start < bdata->node_min_pfn);
  232. BUG_ON(end > bdata->node_low_pfn);
  233. sidx = start - bdata->node_min_pfn;
  234. eidx = end - bdata->node_min_pfn;
  235. if (reserve)
  236. return __reserve(bdata, sidx, eidx, flags);
  237. else
  238. __free(bdata, sidx, eidx);
  239. return 0;
  240. }
  241. static int __init mark_bootmem(unsigned long start, unsigned long end,
  242. int reserve, int flags)
  243. {
  244. unsigned long pos;
  245. bootmem_data_t *bdata;
  246. pos = start;
  247. list_for_each_entry(bdata, &bdata_list, list) {
  248. int err;
  249. unsigned long max;
  250. if (pos < bdata->node_min_pfn ||
  251. pos >= bdata->node_low_pfn) {
  252. BUG_ON(pos != start);
  253. continue;
  254. }
  255. max = min(bdata->node_low_pfn, end);
  256. err = mark_bootmem_node(bdata, pos, max, reserve, flags);
  257. if (reserve && err) {
  258. mark_bootmem(start, pos, 0, 0);
  259. return err;
  260. }
  261. if (max == end)
  262. return 0;
  263. pos = bdata->node_low_pfn;
  264. }
  265. BUG();
  266. }
  267. /**
  268. * free_bootmem_node - mark a page range as usable
  269. * @pgdat: node the range resides on
  270. * @physaddr: starting address of the range
  271. * @size: size of the range in bytes
  272. *
  273. * Partial pages will be considered reserved and left as they are.
  274. *
  275. * The range must reside completely on the specified node.
  276. */
  277. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  278. unsigned long size)
  279. {
  280. unsigned long start, end;
  281. kmemleak_free_part(__va(physaddr), size);
  282. start = PFN_UP(physaddr);
  283. end = PFN_DOWN(physaddr + size);
  284. mark_bootmem_node(pgdat->bdata, start, end, 0, 0);
  285. }
  286. /**
  287. * free_bootmem - mark a page range as usable
  288. * @addr: starting address of the range
  289. * @size: size of the range in bytes
  290. *
  291. * Partial pages will be considered reserved and left as they are.
  292. *
  293. * The range must be contiguous but may span node boundaries.
  294. */
  295. void __init free_bootmem(unsigned long addr, unsigned long size)
  296. {
  297. unsigned long start, end;
  298. kmemleak_free_part(__va(addr), size);
  299. start = PFN_UP(addr);
  300. end = PFN_DOWN(addr + size);
  301. mark_bootmem(start, end, 0, 0);
  302. }
  303. /**
  304. * reserve_bootmem_node - mark a page range as reserved
  305. * @pgdat: node the range resides on
  306. * @physaddr: starting address of the range
  307. * @size: size of the range in bytes
  308. * @flags: reservation flags (see linux/bootmem.h)
  309. *
  310. * Partial pages will be reserved.
  311. *
  312. * The range must reside completely on the specified node.
  313. */
  314. int __init reserve_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  315. unsigned long size, int flags)
  316. {
  317. unsigned long start, end;
  318. start = PFN_DOWN(physaddr);
  319. end = PFN_UP(physaddr + size);
  320. return mark_bootmem_node(pgdat->bdata, start, end, 1, flags);
  321. }
  322. /**
  323. * reserve_bootmem - mark a page range as usable
  324. * @addr: starting address of the range
  325. * @size: size of the range in bytes
  326. * @flags: reservation flags (see linux/bootmem.h)
  327. *
  328. * Partial pages will be reserved.
  329. *
  330. * The range must be contiguous but may span node boundaries.
  331. */
  332. int __init reserve_bootmem(unsigned long addr, unsigned long size,
  333. int flags)
  334. {
  335. unsigned long start, end;
  336. start = PFN_DOWN(addr);
  337. end = PFN_UP(addr + size);
  338. return mark_bootmem(start, end, 1, flags);
  339. }
  340. static unsigned long align_idx(struct bootmem_data *bdata, unsigned long idx,
  341. unsigned long step)
  342. {
  343. unsigned long base = bdata->node_min_pfn;
  344. /*
  345. * Align the index with respect to the node start so that the
  346. * combination of both satisfies the requested alignment.
  347. */
  348. return ALIGN(base + idx, step) - base;
  349. }
  350. static unsigned long align_off(struct bootmem_data *bdata, unsigned long off,
  351. unsigned long align)
  352. {
  353. unsigned long base = PFN_PHYS(bdata->node_min_pfn);
  354. /* Same as align_idx for byte offsets */
  355. return ALIGN(base + off, align) - base;
  356. }
  357. static void * __init alloc_bootmem_core(struct bootmem_data *bdata,
  358. unsigned long size, unsigned long align,
  359. unsigned long goal, unsigned long limit)
  360. {
  361. unsigned long fallback = 0;
  362. unsigned long min, max, start, sidx, midx, step;
  363. bdebug("nid=%td size=%lx [%lu pages] align=%lx goal=%lx limit=%lx\n",
  364. bdata - bootmem_node_data, size, PAGE_ALIGN(size) >> PAGE_SHIFT,
  365. align, goal, limit);
  366. BUG_ON(!size);
  367. BUG_ON(align & (align - 1));
  368. BUG_ON(limit && goal + size > limit);
  369. if (!bdata->node_bootmem_map)
  370. return NULL;
  371. min = bdata->node_min_pfn;
  372. max = bdata->node_low_pfn;
  373. goal >>= PAGE_SHIFT;
  374. limit >>= PAGE_SHIFT;
  375. if (limit && max > limit)
  376. max = limit;
  377. if (max <= min)
  378. return NULL;
  379. step = max(align >> PAGE_SHIFT, 1UL);
  380. if (goal && min < goal && goal < max)
  381. start = ALIGN(goal, step);
  382. else
  383. start = ALIGN(min, step);
  384. sidx = start - bdata->node_min_pfn;
  385. midx = max - bdata->node_min_pfn;
  386. if (bdata->hint_idx > sidx) {
  387. /*
  388. * Handle the valid case of sidx being zero and still
  389. * catch the fallback below.
  390. */
  391. fallback = sidx + 1;
  392. sidx = align_idx(bdata, bdata->hint_idx, step);
  393. }
  394. while (1) {
  395. int merge;
  396. void *region;
  397. unsigned long eidx, i, start_off, end_off;
  398. find_block:
  399. sidx = find_next_zero_bit(bdata->node_bootmem_map, midx, sidx);
  400. sidx = align_idx(bdata, sidx, step);
  401. eidx = sidx + PFN_UP(size);
  402. if (sidx >= midx || eidx > midx)
  403. break;
  404. for (i = sidx; i < eidx; i++)
  405. if (test_bit(i, bdata->node_bootmem_map)) {
  406. sidx = align_idx(bdata, i, step);
  407. if (sidx == i)
  408. sidx += step;
  409. goto find_block;
  410. }
  411. if (bdata->last_end_off & (PAGE_SIZE - 1) &&
  412. PFN_DOWN(bdata->last_end_off) + 1 == sidx)
  413. start_off = align_off(bdata, bdata->last_end_off, align);
  414. else
  415. start_off = PFN_PHYS(sidx);
  416. merge = PFN_DOWN(start_off) < sidx;
  417. end_off = start_off + size;
  418. bdata->last_end_off = end_off;
  419. bdata->hint_idx = PFN_UP(end_off);
  420. /*
  421. * Reserve the area now:
  422. */
  423. if (__reserve(bdata, PFN_DOWN(start_off) + merge,
  424. PFN_UP(end_off), BOOTMEM_EXCLUSIVE))
  425. BUG();
  426. region = phys_to_virt(PFN_PHYS(bdata->node_min_pfn) +
  427. start_off);
  428. memset(region, 0, size);
  429. kmemleak_alloc(region, size, 1, 0);
  430. return region;
  431. }
  432. if (fallback) {
  433. sidx = align_idx(bdata, fallback - 1, step);
  434. fallback = 0;
  435. goto find_block;
  436. }
  437. return NULL;
  438. }
  439. static void * __init alloc_arch_preferred_bootmem(bootmem_data_t *bdata,
  440. unsigned long size, unsigned long align,
  441. unsigned long goal, unsigned long limit)
  442. {
  443. if (WARN_ON_ONCE(slab_is_available()))
  444. return kzalloc(size, GFP_NOWAIT);
  445. #ifdef CONFIG_HAVE_ARCH_BOOTMEM
  446. {
  447. bootmem_data_t *p_bdata;
  448. p_bdata = bootmem_arch_preferred_node(bdata, size, align,
  449. goal, limit);
  450. if (p_bdata)
  451. return alloc_bootmem_core(p_bdata, size, align,
  452. goal, limit);
  453. }
  454. #endif
  455. return NULL;
  456. }
  457. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  458. unsigned long align,
  459. unsigned long goal,
  460. unsigned long limit)
  461. {
  462. bootmem_data_t *bdata;
  463. void *region;
  464. restart:
  465. region = alloc_arch_preferred_bootmem(NULL, size, align, goal, limit);
  466. if (region)
  467. return region;
  468. list_for_each_entry(bdata, &bdata_list, list) {
  469. if (goal && bdata->node_low_pfn <= PFN_DOWN(goal))
  470. continue;
  471. if (limit && bdata->node_min_pfn >= PFN_DOWN(limit))
  472. break;
  473. region = alloc_bootmem_core(bdata, size, align, goal, limit);
  474. if (region)
  475. return region;
  476. }
  477. if (goal) {
  478. goal = 0;
  479. goto restart;
  480. }
  481. return NULL;
  482. }
  483. /**
  484. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  485. * @size: size of the request in bytes
  486. * @align: alignment of the region
  487. * @goal: preferred starting address of the region
  488. *
  489. * The goal is dropped if it can not be satisfied and the allocation will
  490. * fall back to memory below @goal.
  491. *
  492. * Allocation may happen on any node in the system.
  493. *
  494. * Returns NULL on failure.
  495. */
  496. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  497. unsigned long goal)
  498. {
  499. return ___alloc_bootmem_nopanic(size, align, goal, 0);
  500. }
  501. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  502. unsigned long goal, unsigned long limit)
  503. {
  504. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  505. if (mem)
  506. return mem;
  507. /*
  508. * Whoops, we cannot satisfy the allocation request.
  509. */
  510. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  511. panic("Out of memory");
  512. return NULL;
  513. }
  514. /**
  515. * __alloc_bootmem - allocate boot memory
  516. * @size: size of the request in bytes
  517. * @align: alignment of the region
  518. * @goal: preferred starting address of the region
  519. *
  520. * The goal is dropped if it can not be satisfied and the allocation will
  521. * fall back to memory below @goal.
  522. *
  523. * Allocation may happen on any node in the system.
  524. *
  525. * The function panics if the request can not be satisfied.
  526. */
  527. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  528. unsigned long goal)
  529. {
  530. return ___alloc_bootmem(size, align, goal, 0);
  531. }
  532. static void * __init ___alloc_bootmem_node(bootmem_data_t *bdata,
  533. unsigned long size, unsigned long align,
  534. unsigned long goal, unsigned long limit)
  535. {
  536. void *ptr;
  537. ptr = alloc_arch_preferred_bootmem(bdata, size, align, goal, limit);
  538. if (ptr)
  539. return ptr;
  540. ptr = alloc_bootmem_core(bdata, size, align, goal, limit);
  541. if (ptr)
  542. return ptr;
  543. return ___alloc_bootmem(size, align, goal, limit);
  544. }
  545. /**
  546. * __alloc_bootmem_node - allocate boot memory from a specific node
  547. * @pgdat: node to allocate from
  548. * @size: size of the request in bytes
  549. * @align: alignment of the region
  550. * @goal: preferred starting address of the region
  551. *
  552. * The goal is dropped if it can not be satisfied and the allocation will
  553. * fall back to memory below @goal.
  554. *
  555. * Allocation may fall back to any node in the system if the specified node
  556. * can not hold the requested memory.
  557. *
  558. * The function panics if the request can not be satisfied.
  559. */
  560. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  561. unsigned long align, unsigned long goal)
  562. {
  563. if (WARN_ON_ONCE(slab_is_available()))
  564. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  565. return ___alloc_bootmem_node(pgdat->bdata, size, align, goal, 0);
  566. }
  567. #ifdef CONFIG_SPARSEMEM
  568. /**
  569. * alloc_bootmem_section - allocate boot memory from a specific section
  570. * @size: size of the request in bytes
  571. * @section_nr: sparse map section to allocate from
  572. *
  573. * Return NULL on failure.
  574. */
  575. void * __init alloc_bootmem_section(unsigned long size,
  576. unsigned long section_nr)
  577. {
  578. bootmem_data_t *bdata;
  579. unsigned long pfn, goal, limit;
  580. pfn = section_nr_to_pfn(section_nr);
  581. goal = pfn << PAGE_SHIFT;
  582. limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
  583. bdata = &bootmem_node_data[early_pfn_to_nid(pfn)];
  584. return alloc_bootmem_core(bdata, size, SMP_CACHE_BYTES, goal, limit);
  585. }
  586. #endif
  587. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  588. unsigned long align, unsigned long goal)
  589. {
  590. void *ptr;
  591. if (WARN_ON_ONCE(slab_is_available()))
  592. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  593. ptr = alloc_arch_preferred_bootmem(pgdat->bdata, size, align, goal, 0);
  594. if (ptr)
  595. return ptr;
  596. ptr = alloc_bootmem_core(pgdat->bdata, size, align, goal, 0);
  597. if (ptr)
  598. return ptr;
  599. return __alloc_bootmem_nopanic(size, align, goal);
  600. }
  601. #ifndef ARCH_LOW_ADDRESS_LIMIT
  602. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  603. #endif
  604. /**
  605. * __alloc_bootmem_low - allocate low boot memory
  606. * @size: size of the request in bytes
  607. * @align: alignment of the region
  608. * @goal: preferred starting address of the region
  609. *
  610. * The goal is dropped if it can not be satisfied and the allocation will
  611. * fall back to memory below @goal.
  612. *
  613. * Allocation may happen on any node in the system.
  614. *
  615. * The function panics if the request can not be satisfied.
  616. */
  617. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  618. unsigned long goal)
  619. {
  620. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  621. }
  622. /**
  623. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  624. * @pgdat: node to allocate from
  625. * @size: size of the request in bytes
  626. * @align: alignment of the region
  627. * @goal: preferred starting address of the region
  628. *
  629. * The goal is dropped if it can not be satisfied and the allocation will
  630. * fall back to memory below @goal.
  631. *
  632. * Allocation may fall back to any node in the system if the specified node
  633. * can not hold the requested memory.
  634. *
  635. * The function panics if the request can not be satisfied.
  636. */
  637. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  638. unsigned long align, unsigned long goal)
  639. {
  640. if (WARN_ON_ONCE(slab_is_available()))
  641. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  642. return ___alloc_bootmem_node(pgdat->bdata, size, align,
  643. goal, ARCH_LOW_ADDRESS_LIMIT);
  644. }