nobootmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. return count;
  122. }
  123. static int reset_managed_pages_done __initdata;
  124. void reset_node_managed_pages(pg_data_t *pgdat)
  125. {
  126. struct zone *z;
  127. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  128. z->managed_pages = 0;
  129. }
  130. void __init reset_all_zones_managed_pages(void)
  131. {
  132. struct pglist_data *pgdat;
  133. if (reset_managed_pages_done)
  134. return;
  135. for_each_online_pgdat(pgdat)
  136. reset_node_managed_pages(pgdat);
  137. reset_managed_pages_done = 1;
  138. }
  139. /**
  140. * free_all_bootmem - release free pages to the buddy allocator
  141. *
  142. * Returns the number of pages actually released.
  143. */
  144. unsigned long __init free_all_bootmem(void)
  145. {
  146. unsigned long pages;
  147. reset_all_zones_managed_pages();
  148. pages = free_low_memory_core_early();
  149. totalram_pages += pages;
  150. return pages;
  151. }
  152. /**
  153. * free_bootmem_node - mark a page range as usable
  154. * @pgdat: node the range resides on
  155. * @physaddr: starting address of the range
  156. * @size: size of the range in bytes
  157. *
  158. * Partial pages will be considered reserved and left as they are.
  159. *
  160. * The range must reside completely on the specified node.
  161. */
  162. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  163. unsigned long size)
  164. {
  165. memblock_free(physaddr, size);
  166. }
  167. /**
  168. * free_bootmem - mark a page range as usable
  169. * @addr: starting address of the range
  170. * @size: size of the range in bytes
  171. *
  172. * Partial pages will be considered reserved and left as they are.
  173. *
  174. * The range must be contiguous but may span node boundaries.
  175. */
  176. void __init free_bootmem(unsigned long addr, unsigned long size)
  177. {
  178. memblock_free(addr, size);
  179. }
  180. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  181. unsigned long align,
  182. unsigned long goal,
  183. unsigned long limit)
  184. {
  185. void *ptr;
  186. if (WARN_ON_ONCE(slab_is_available()))
  187. return kzalloc(size, GFP_NOWAIT);
  188. restart:
  189. ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align, goal, limit);
  190. if (ptr)
  191. return ptr;
  192. if (goal != 0) {
  193. goal = 0;
  194. goto restart;
  195. }
  196. return NULL;
  197. }
  198. /**
  199. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  200. * @size: size of the request in bytes
  201. * @align: alignment of the region
  202. * @goal: preferred starting address of the region
  203. *
  204. * The goal is dropped if it can not be satisfied and the allocation will
  205. * fall back to memory below @goal.
  206. *
  207. * Allocation may happen on any node in the system.
  208. *
  209. * Returns NULL on failure.
  210. */
  211. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  212. unsigned long goal)
  213. {
  214. unsigned long limit = -1UL;
  215. return ___alloc_bootmem_nopanic(size, align, goal, limit);
  216. }
  217. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  218. unsigned long goal, unsigned long limit)
  219. {
  220. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  221. if (mem)
  222. return mem;
  223. /*
  224. * Whoops, we cannot satisfy the allocation request.
  225. */
  226. pr_alert("bootmem alloc of %lu bytes failed!\n", size);
  227. panic("Out of memory");
  228. return NULL;
  229. }
  230. /**
  231. * __alloc_bootmem - allocate boot memory
  232. * @size: size of the request in bytes
  233. * @align: alignment of the region
  234. * @goal: preferred starting address of the region
  235. *
  236. * The goal is dropped if it can not be satisfied and the allocation will
  237. * fall back to memory below @goal.
  238. *
  239. * Allocation may happen on any node in the system.
  240. *
  241. * The function panics if the request can not be satisfied.
  242. */
  243. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  244. unsigned long goal)
  245. {
  246. unsigned long limit = -1UL;
  247. return ___alloc_bootmem(size, align, goal, limit);
  248. }
  249. void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
  250. unsigned long size,
  251. unsigned long align,
  252. unsigned long goal,
  253. unsigned long limit)
  254. {
  255. void *ptr;
  256. again:
  257. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  258. goal, limit);
  259. if (ptr)
  260. return ptr;
  261. ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align,
  262. goal, limit);
  263. if (ptr)
  264. return ptr;
  265. if (goal) {
  266. goal = 0;
  267. goto again;
  268. }
  269. return NULL;
  270. }
  271. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  272. unsigned long align, unsigned long goal)
  273. {
  274. if (WARN_ON_ONCE(slab_is_available()))
  275. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  276. return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
  277. }
  278. static void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  279. unsigned long align, unsigned long goal,
  280. unsigned long limit)
  281. {
  282. void *ptr;
  283. ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, limit);
  284. if (ptr)
  285. return ptr;
  286. pr_alert("bootmem alloc of %lu bytes failed!\n", size);
  287. panic("Out of memory");
  288. return NULL;
  289. }
  290. /**
  291. * __alloc_bootmem_node - allocate boot memory from a specific node
  292. * @pgdat: node to allocate from
  293. * @size: size of the request in bytes
  294. * @align: alignment of the region
  295. * @goal: preferred starting address of the region
  296. *
  297. * The goal is dropped if it can not be satisfied and the allocation will
  298. * fall back to memory below @goal.
  299. *
  300. * Allocation may fall back to any node in the system if the specified node
  301. * can not hold the requested memory.
  302. *
  303. * The function panics if the request can not be satisfied.
  304. */
  305. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  306. unsigned long align, unsigned long goal)
  307. {
  308. if (WARN_ON_ONCE(slab_is_available()))
  309. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  310. return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
  311. }
  312. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  313. unsigned long align, unsigned long goal)
  314. {
  315. return __alloc_bootmem_node(pgdat, size, align, goal);
  316. }
  317. /**
  318. * __alloc_bootmem_low - allocate low boot memory
  319. * @size: size of the request in bytes
  320. * @align: alignment of the region
  321. * @goal: preferred starting address of the region
  322. *
  323. * The goal is dropped if it can not be satisfied and the allocation will
  324. * fall back to memory below @goal.
  325. *
  326. * Allocation may happen on any node in the system.
  327. *
  328. * The function panics if the request can not be satisfied.
  329. */
  330. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  331. unsigned long goal)
  332. {
  333. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  334. }
  335. void * __init __alloc_bootmem_low_nopanic(unsigned long size,
  336. unsigned long align,
  337. unsigned long goal)
  338. {
  339. return ___alloc_bootmem_nopanic(size, align, goal,
  340. ARCH_LOW_ADDRESS_LIMIT);
  341. }
  342. /**
  343. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  344. * @pgdat: node to allocate from
  345. * @size: size of the request in bytes
  346. * @align: alignment of the region
  347. * @goal: preferred starting address of the region
  348. *
  349. * The goal is dropped if it can not be satisfied and the allocation will
  350. * fall back to memory below @goal.
  351. *
  352. * Allocation may fall back to any node in the system if the specified node
  353. * can not hold the requested memory.
  354. *
  355. * The function panics if the request can not be satisfied.
  356. */
  357. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  358. unsigned long align, unsigned long goal)
  359. {
  360. if (WARN_ON_ONCE(slab_is_available()))
  361. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  362. return ___alloc_bootmem_node(pgdat, size, align, goal,
  363. ARCH_LOW_ADDRESS_LIMIT);
  364. }