bootmem.c 21 KB

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