nobootmem.c 11 KB

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