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