nobootmem.c 11 KB

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