nobootmem.c 11 KB

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