nobootmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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/memblock.h>
  19. #include <linux/bootmem.h>
  20. #include <asm/bug.h>
  21. #include <asm/io.h>
  22. #include "internal.h"
  23. #ifndef CONFIG_HAVE_MEMBLOCK
  24. #error CONFIG_HAVE_MEMBLOCK not defined
  25. #endif
  26. #ifndef CONFIG_NEED_MULTIPLE_NODES
  27. struct pglist_data __refdata contig_page_data;
  28. EXPORT_SYMBOL(contig_page_data);
  29. #endif
  30. unsigned long max_low_pfn;
  31. unsigned long min_low_pfn;
  32. unsigned long max_pfn;
  33. unsigned long long max_possible_pfn;
  34. static void * __init __alloc_memory_core_early(int nid, u64 size, u64 align,
  35. u64 goal, u64 limit)
  36. {
  37. void *ptr;
  38. u64 addr;
  39. enum memblock_flags flags = choose_memblock_flags();
  40. if (limit > memblock.current_limit)
  41. limit = memblock.current_limit;
  42. again:
  43. addr = memblock_find_in_range_node(size, align, goal, limit, nid,
  44. flags);
  45. if (!addr && (flags & MEMBLOCK_MIRROR)) {
  46. flags &= ~MEMBLOCK_MIRROR;
  47. pr_warn("Could not allocate %pap bytes of mirrored memory\n",
  48. &size);
  49. goto again;
  50. }
  51. if (!addr)
  52. return NULL;
  53. if (memblock_reserve(addr, size))
  54. return NULL;
  55. ptr = phys_to_virt(addr);
  56. memset(ptr, 0, size);
  57. /*
  58. * The min_count is set to 0 so that bootmem allocated blocks
  59. * are never reported as leaks.
  60. */
  61. kmemleak_alloc(ptr, size, 0, 0);
  62. return ptr;
  63. }
  64. /**
  65. * free_bootmem_late - free bootmem pages directly to page allocator
  66. * @addr: starting address of the range
  67. * @size: size of the range in bytes
  68. *
  69. * This is only useful when the bootmem allocator has already been torn
  70. * down, but we are still initializing the system. Pages are given directly
  71. * to the page allocator, no bootmem metadata is updated because it is gone.
  72. */
  73. void __init free_bootmem_late(unsigned long addr, unsigned long size)
  74. {
  75. unsigned long cursor, end;
  76. kmemleak_free_part_phys(addr, size);
  77. cursor = PFN_UP(addr);
  78. end = PFN_DOWN(addr + size);
  79. for (; cursor < end; cursor++) {
  80. __free_pages_bootmem(pfn_to_page(cursor), cursor, 0);
  81. totalram_pages++;
  82. }
  83. }
  84. static void __init __free_pages_memory(unsigned long start, unsigned long end)
  85. {
  86. int order;
  87. while (start < end) {
  88. order = min(MAX_ORDER - 1UL, __ffs(start));
  89. while (start + (1UL << order) > end)
  90. order--;
  91. __free_pages_bootmem(pfn_to_page(start), start, order);
  92. start += (1UL << order);
  93. }
  94. }
  95. static unsigned long __init __free_memory_core(phys_addr_t start,
  96. phys_addr_t end)
  97. {
  98. unsigned long start_pfn = PFN_UP(start);
  99. unsigned long end_pfn = min_t(unsigned long,
  100. PFN_DOWN(end), max_low_pfn);
  101. if (start_pfn >= end_pfn)
  102. return 0;
  103. __free_pages_memory(start_pfn, end_pfn);
  104. return end_pfn - start_pfn;
  105. }
  106. static unsigned long __init free_low_memory_core_early(void)
  107. {
  108. unsigned long count = 0;
  109. phys_addr_t start, end;
  110. u64 i;
  111. memblock_clear_hotplug(0, -1);
  112. for_each_reserved_mem_region(i, &start, &end)
  113. reserve_bootmem_region(start, end);
  114. /*
  115. * We need to use NUMA_NO_NODE instead of NODE_DATA(0)->node_id
  116. * because in some case like Node0 doesn't have RAM installed
  117. * low ram will be on Node1
  118. */
  119. for_each_free_mem_range(i, NUMA_NO_NODE, MEMBLOCK_NONE, &start, &end,
  120. NULL)
  121. count += __free_memory_core(start, end);
  122. return count;
  123. }
  124. static int reset_managed_pages_done __initdata;
  125. void reset_node_managed_pages(pg_data_t *pgdat)
  126. {
  127. struct zone *z;
  128. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  129. z->managed_pages = 0;
  130. }
  131. void __init reset_all_zones_managed_pages(void)
  132. {
  133. struct pglist_data *pgdat;
  134. if (reset_managed_pages_done)
  135. return;
  136. for_each_online_pgdat(pgdat)
  137. reset_node_managed_pages(pgdat);
  138. reset_managed_pages_done = 1;
  139. }
  140. /**
  141. * free_all_bootmem - release free pages to the buddy allocator
  142. *
  143. * Return: the number of pages actually released.
  144. */
  145. unsigned long __init free_all_bootmem(void)
  146. {
  147. unsigned long pages;
  148. reset_all_zones_managed_pages();
  149. pages = free_low_memory_core_early();
  150. totalram_pages += pages;
  151. return pages;
  152. }
  153. /**
  154. * free_bootmem_node - mark a page range as usable
  155. * @pgdat: node the range resides on
  156. * @physaddr: starting physical address of the range
  157. * @size: size of the range in bytes
  158. *
  159. * Partial pages will be considered reserved and left as they are.
  160. *
  161. * The range must reside completely on the specified node.
  162. */
  163. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  164. unsigned long size)
  165. {
  166. memblock_free(physaddr, size);
  167. }
  168. /**
  169. * free_bootmem - mark a page range as usable
  170. * @addr: starting physical address of the range
  171. * @size: size of the range in bytes
  172. *
  173. * Partial pages will be considered reserved and left as they are.
  174. *
  175. * The range must be contiguous but may span node boundaries.
  176. */
  177. void __init free_bootmem(unsigned long addr, unsigned long size)
  178. {
  179. memblock_free(addr, size);
  180. }
  181. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  182. unsigned long align,
  183. unsigned long goal,
  184. unsigned long limit)
  185. {
  186. void *ptr;
  187. if (WARN_ON_ONCE(slab_is_available()))
  188. return kzalloc(size, GFP_NOWAIT);
  189. restart:
  190. ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align, goal, limit);
  191. if (ptr)
  192. return ptr;
  193. if (goal != 0) {
  194. goal = 0;
  195. goto restart;
  196. }
  197. return NULL;
  198. }
  199. /**
  200. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  201. * @size: size of the request in bytes
  202. * @align: alignment of the region
  203. * @goal: preferred starting address of the region
  204. *
  205. * The goal is dropped if it can not be satisfied and the allocation will
  206. * fall back to memory below @goal.
  207. *
  208. * Allocation may happen on any node in the system.
  209. *
  210. * Return: address of the allocated region or %NULL on failure.
  211. */
  212. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  213. unsigned long goal)
  214. {
  215. unsigned long limit = -1UL;
  216. return ___alloc_bootmem_nopanic(size, align, goal, limit);
  217. }
  218. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  219. unsigned long goal, unsigned long limit)
  220. {
  221. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  222. if (mem)
  223. return mem;
  224. /*
  225. * Whoops, we cannot satisfy the allocation request.
  226. */
  227. pr_alert("bootmem alloc of %lu bytes failed!\n", size);
  228. panic("Out of memory");
  229. return NULL;
  230. }
  231. /**
  232. * __alloc_bootmem - allocate boot memory
  233. * @size: size of the request in bytes
  234. * @align: alignment of the region
  235. * @goal: preferred starting address of the region
  236. *
  237. * The goal is dropped if it can not be satisfied and the allocation will
  238. * fall back to memory below @goal.
  239. *
  240. * Allocation may happen on any node in the system.
  241. *
  242. * The function panics if the request can not be satisfied.
  243. *
  244. * Return: address of the allocated region.
  245. */
  246. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  247. unsigned long goal)
  248. {
  249. unsigned long limit = -1UL;
  250. return ___alloc_bootmem(size, align, goal, limit);
  251. }
  252. void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
  253. unsigned long size,
  254. unsigned long align,
  255. unsigned long goal,
  256. unsigned long limit)
  257. {
  258. void *ptr;
  259. again:
  260. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  261. goal, limit);
  262. if (ptr)
  263. return ptr;
  264. ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align,
  265. goal, limit);
  266. if (ptr)
  267. return ptr;
  268. if (goal) {
  269. goal = 0;
  270. goto again;
  271. }
  272. return NULL;
  273. }
  274. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  275. unsigned long align, unsigned long goal)
  276. {
  277. if (WARN_ON_ONCE(slab_is_available()))
  278. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  279. return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
  280. }
  281. static void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  282. unsigned long align, unsigned long goal,
  283. unsigned long limit)
  284. {
  285. void *ptr;
  286. ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, limit);
  287. if (ptr)
  288. return ptr;
  289. pr_alert("bootmem alloc of %lu bytes failed!\n", size);
  290. panic("Out of memory");
  291. return NULL;
  292. }
  293. /**
  294. * __alloc_bootmem_node - allocate boot memory from a specific node
  295. * @pgdat: node to allocate from
  296. * @size: size of the request in bytes
  297. * @align: alignment of the region
  298. * @goal: preferred starting address of the region
  299. *
  300. * The goal is dropped if it can not be satisfied and the allocation will
  301. * fall back to memory below @goal.
  302. *
  303. * Allocation may fall back to any node in the system if the specified node
  304. * can not hold the requested memory.
  305. *
  306. * The function panics if the request can not be satisfied.
  307. *
  308. * Return: address of the allocated region.
  309. */
  310. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  311. unsigned long align, unsigned long goal)
  312. {
  313. if (WARN_ON_ONCE(slab_is_available()))
  314. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  315. return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
  316. }
  317. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  318. unsigned long align, unsigned long goal)
  319. {
  320. return __alloc_bootmem_node(pgdat, size, align, goal);
  321. }
  322. /**
  323. * __alloc_bootmem_low - allocate low boot memory
  324. * @size: size of the request in bytes
  325. * @align: alignment of the region
  326. * @goal: preferred starting address of the region
  327. *
  328. * The goal is dropped if it can not be satisfied and the allocation will
  329. * fall back to memory below @goal.
  330. *
  331. * Allocation may happen on any node in the system.
  332. *
  333. * The function panics if the request can not be satisfied.
  334. *
  335. * Return: address of the allocated region.
  336. */
  337. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  338. unsigned long goal)
  339. {
  340. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  341. }
  342. void * __init __alloc_bootmem_low_nopanic(unsigned long size,
  343. unsigned long align,
  344. unsigned long goal)
  345. {
  346. return ___alloc_bootmem_nopanic(size, align, goal,
  347. ARCH_LOW_ADDRESS_LIMIT);
  348. }
  349. /**
  350. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  351. * @pgdat: node to allocate from
  352. * @size: size of the request in bytes
  353. * @align: alignment of the region
  354. * @goal: preferred starting address of the region
  355. *
  356. * The goal is dropped if it can not be satisfied and the allocation will
  357. * fall back to memory below @goal.
  358. *
  359. * Allocation may fall back to any node in the system if the specified node
  360. * can not hold the requested memory.
  361. *
  362. * The function panics if the request can not be satisfied.
  363. *
  364. * Return: address of the allocated region.
  365. */
  366. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  367. unsigned long align, unsigned long goal)
  368. {
  369. if (WARN_ON_ONCE(slab_is_available()))
  370. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  371. return ___alloc_bootmem_node(pgdat, size, align, goal,
  372. ARCH_LOW_ADDRESS_LIMIT);
  373. }