nobootmem.c 11 KB

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