nobootmem.c 11 KB

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