nobootmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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/module.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. #ifdef CONFIG_CRASH_DUMP
  31. /*
  32. * If we have booted due to a crash, max_pfn will be a very low value. We need
  33. * to know the amount of memory that the previous kernel used.
  34. */
  35. unsigned long saved_max_pfn;
  36. #endif
  37. static void * __init __alloc_memory_core_early(int nid, u64 size, u64 align,
  38. u64 goal, u64 limit)
  39. {
  40. void *ptr;
  41. u64 addr;
  42. if (limit > memblock.current_limit)
  43. limit = memblock.current_limit;
  44. addr = find_memory_core_early(nid, size, align, goal, limit);
  45. if (addr == MEMBLOCK_ERROR)
  46. return NULL;
  47. ptr = phys_to_virt(addr);
  48. memset(ptr, 0, size);
  49. memblock_x86_reserve_range(addr, addr + size, "BOOTMEM");
  50. /*
  51. * The min_count is set to 0 so that bootmem allocated blocks
  52. * are never reported as leaks.
  53. */
  54. kmemleak_alloc(ptr, size, 0, 0);
  55. return ptr;
  56. }
  57. /*
  58. * free_bootmem_late - free bootmem pages directly to page allocator
  59. * @addr: starting address of the range
  60. * @size: size of the range in bytes
  61. *
  62. * This is only useful when the bootmem allocator has already been torn
  63. * down, but we are still initializing the system. Pages are given directly
  64. * to the page allocator, no bootmem metadata is updated because it is gone.
  65. */
  66. void __init free_bootmem_late(unsigned long addr, unsigned long size)
  67. {
  68. unsigned long cursor, end;
  69. kmemleak_free_part(__va(addr), size);
  70. cursor = PFN_UP(addr);
  71. end = PFN_DOWN(addr + size);
  72. for (; cursor < end; cursor++) {
  73. __free_pages_bootmem(pfn_to_page(cursor), 0);
  74. totalram_pages++;
  75. }
  76. }
  77. static void __init __free_pages_memory(unsigned long start, unsigned long end)
  78. {
  79. int i;
  80. unsigned long start_aligned, end_aligned;
  81. int order = ilog2(BITS_PER_LONG);
  82. start_aligned = (start + (BITS_PER_LONG - 1)) & ~(BITS_PER_LONG - 1);
  83. end_aligned = end & ~(BITS_PER_LONG - 1);
  84. if (end_aligned <= start_aligned) {
  85. for (i = start; i < end; i++)
  86. __free_pages_bootmem(pfn_to_page(i), 0);
  87. return;
  88. }
  89. for (i = start; i < start_aligned; i++)
  90. __free_pages_bootmem(pfn_to_page(i), 0);
  91. for (i = start_aligned; i < end_aligned; i += BITS_PER_LONG)
  92. __free_pages_bootmem(pfn_to_page(i), order);
  93. for (i = end_aligned; i < end; i++)
  94. __free_pages_bootmem(pfn_to_page(i), 0);
  95. }
  96. unsigned long __init free_all_memory_core_early(int nodeid)
  97. {
  98. int i;
  99. u64 start, end;
  100. unsigned long count = 0;
  101. struct range *range = NULL;
  102. int nr_range;
  103. nr_range = get_free_all_memory_range(&range, nodeid);
  104. for (i = 0; i < nr_range; i++) {
  105. start = range[i].start;
  106. end = range[i].end;
  107. count += end - start;
  108. __free_pages_memory(start, end);
  109. }
  110. return count;
  111. }
  112. /**
  113. * free_all_bootmem_node - release a node's free pages to the buddy allocator
  114. * @pgdat: node to be released
  115. *
  116. * Returns the number of pages actually released.
  117. */
  118. unsigned long __init free_all_bootmem_node(pg_data_t *pgdat)
  119. {
  120. register_page_bootmem_info_node(pgdat);
  121. /* free_all_memory_core_early(MAX_NUMNODES) will be called later */
  122. return 0;
  123. }
  124. /**
  125. * free_all_bootmem - release free pages to the buddy allocator
  126. *
  127. * Returns the number of pages actually released.
  128. */
  129. unsigned long __init free_all_bootmem(void)
  130. {
  131. /*
  132. * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id
  133. * because in some case like Node0 doesnt have RAM installed
  134. * low ram will be on Node1
  135. * Use MAX_NUMNODES will make sure all ranges in early_node_map[]
  136. * will be used instead of only Node0 related
  137. */
  138. return free_all_memory_core_early(MAX_NUMNODES);
  139. }
  140. /**
  141. * free_bootmem_node - mark a page range as usable
  142. * @pgdat: node the range resides on
  143. * @physaddr: starting address of the range
  144. * @size: size of the range in bytes
  145. *
  146. * Partial pages will be considered reserved and left as they are.
  147. *
  148. * The range must reside completely on the specified node.
  149. */
  150. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  151. unsigned long size)
  152. {
  153. kmemleak_free_part(__va(physaddr), size);
  154. memblock_x86_free_range(physaddr, physaddr + size);
  155. }
  156. /**
  157. * free_bootmem - mark a page range as usable
  158. * @addr: starting address of the range
  159. * @size: size of the range in bytes
  160. *
  161. * Partial pages will be considered reserved and left as they are.
  162. *
  163. * The range must be contiguous but may span node boundaries.
  164. */
  165. void __init free_bootmem(unsigned long addr, unsigned long size)
  166. {
  167. kmemleak_free_part(__va(addr), size);
  168. memblock_x86_free_range(addr, addr + size);
  169. }
  170. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  171. unsigned long align,
  172. unsigned long goal,
  173. unsigned long limit)
  174. {
  175. void *ptr;
  176. if (WARN_ON_ONCE(slab_is_available()))
  177. return kzalloc(size, GFP_NOWAIT);
  178. restart:
  179. ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
  180. if (ptr)
  181. return ptr;
  182. if (goal != 0) {
  183. goal = 0;
  184. goto restart;
  185. }
  186. return NULL;
  187. }
  188. /**
  189. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  190. * @size: size of the request in bytes
  191. * @align: alignment of the region
  192. * @goal: preferred starting address of the region
  193. *
  194. * The goal is dropped if it can not be satisfied and the allocation will
  195. * fall back to memory below @goal.
  196. *
  197. * Allocation may happen on any node in the system.
  198. *
  199. * Returns NULL on failure.
  200. */
  201. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  202. unsigned long goal)
  203. {
  204. unsigned long limit = -1UL;
  205. return ___alloc_bootmem_nopanic(size, align, goal, limit);
  206. }
  207. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  208. unsigned long goal, unsigned long limit)
  209. {
  210. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  211. if (mem)
  212. return mem;
  213. /*
  214. * Whoops, we cannot satisfy the allocation request.
  215. */
  216. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  217. panic("Out of memory");
  218. return NULL;
  219. }
  220. /**
  221. * __alloc_bootmem - allocate boot memory
  222. * @size: size of the request in bytes
  223. * @align: alignment of the region
  224. * @goal: preferred starting address of the region
  225. *
  226. * The goal is dropped if it can not be satisfied and the allocation will
  227. * fall back to memory below @goal.
  228. *
  229. * Allocation may happen on any node in the system.
  230. *
  231. * The function panics if the request can not be satisfied.
  232. */
  233. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  234. unsigned long goal)
  235. {
  236. unsigned long limit = -1UL;
  237. return ___alloc_bootmem(size, align, goal, limit);
  238. }
  239. /**
  240. * __alloc_bootmem_node - allocate boot memory from a specific node
  241. * @pgdat: node to allocate from
  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 fall back to any node in the system if the specified node
  250. * can not hold the requested memory.
  251. *
  252. * The function panics if the request can not be satisfied.
  253. */
  254. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  255. unsigned long align, unsigned long goal)
  256. {
  257. void *ptr;
  258. if (WARN_ON_ONCE(slab_is_available()))
  259. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  260. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  261. goal, -1ULL);
  262. if (ptr)
  263. return ptr;
  264. return __alloc_memory_core_early(MAX_NUMNODES, size, align,
  265. goal, -1ULL);
  266. }
  267. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  268. unsigned long align, unsigned long goal)
  269. {
  270. #ifdef MAX_DMA32_PFN
  271. unsigned long end_pfn;
  272. if (WARN_ON_ONCE(slab_is_available()))
  273. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  274. /* update goal according ...MAX_DMA32_PFN */
  275. end_pfn = pgdat->node_start_pfn + pgdat->node_spanned_pages;
  276. if (end_pfn > MAX_DMA32_PFN + (128 >> (20 - PAGE_SHIFT)) &&
  277. (goal >> PAGE_SHIFT) < MAX_DMA32_PFN) {
  278. void *ptr;
  279. unsigned long new_goal;
  280. new_goal = MAX_DMA32_PFN << PAGE_SHIFT;
  281. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  282. new_goal, -1ULL);
  283. if (ptr)
  284. return ptr;
  285. }
  286. #endif
  287. return __alloc_bootmem_node(pgdat, size, align, goal);
  288. }
  289. #ifdef CONFIG_SPARSEMEM
  290. /**
  291. * alloc_bootmem_section - allocate boot memory from a specific section
  292. * @size: size of the request in bytes
  293. * @section_nr: sparse map section to allocate from
  294. *
  295. * Return NULL on failure.
  296. */
  297. void * __init alloc_bootmem_section(unsigned long size,
  298. unsigned long section_nr)
  299. {
  300. unsigned long pfn, goal, limit;
  301. pfn = section_nr_to_pfn(section_nr);
  302. goal = pfn << PAGE_SHIFT;
  303. limit = section_nr_to_pfn(section_nr + 1) << PAGE_SHIFT;
  304. return __alloc_memory_core_early(early_pfn_to_nid(pfn), size,
  305. SMP_CACHE_BYTES, goal, limit);
  306. }
  307. #endif
  308. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  309. unsigned long align, unsigned long goal)
  310. {
  311. void *ptr;
  312. if (WARN_ON_ONCE(slab_is_available()))
  313. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  314. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  315. goal, -1ULL);
  316. if (ptr)
  317. return ptr;
  318. return __alloc_bootmem_nopanic(size, align, goal);
  319. }
  320. #ifndef ARCH_LOW_ADDRESS_LIMIT
  321. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  322. #endif
  323. /**
  324. * __alloc_bootmem_low - allocate low boot memory
  325. * @size: size of the request in bytes
  326. * @align: alignment of the region
  327. * @goal: preferred starting address of the region
  328. *
  329. * The goal is dropped if it can not be satisfied and the allocation will
  330. * fall back to memory below @goal.
  331. *
  332. * Allocation may happen on any node in the system.
  333. *
  334. * The function panics if the request can not be satisfied.
  335. */
  336. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  337. unsigned long goal)
  338. {
  339. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  340. }
  341. /**
  342. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  343. * @pgdat: node to allocate from
  344. * @size: size of the request in bytes
  345. * @align: alignment of the region
  346. * @goal: preferred starting address of the region
  347. *
  348. * The goal is dropped if it can not be satisfied and the allocation will
  349. * fall back to memory below @goal.
  350. *
  351. * Allocation may fall back to any node in the system if the specified node
  352. * can not hold the requested memory.
  353. *
  354. * The function panics if the request can not be satisfied.
  355. */
  356. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  357. unsigned long align, unsigned long goal)
  358. {
  359. void *ptr;
  360. if (WARN_ON_ONCE(slab_is_available()))
  361. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  362. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  363. goal, ARCH_LOW_ADDRESS_LIMIT);
  364. if (ptr)
  365. return ptr;
  366. return __alloc_memory_core_early(MAX_NUMNODES, size, align,
  367. goal, ARCH_LOW_ADDRESS_LIMIT);
  368. }