bootmem.c 20 KB

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