nobootmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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. static unsigned long __init __free_memory_core(phys_addr_t start,
  89. phys_addr_t end)
  90. {
  91. unsigned long start_pfn = PFN_UP(start);
  92. unsigned long end_pfn = min_t(unsigned long,
  93. PFN_DOWN(end), max_low_pfn);
  94. if (start_pfn > end_pfn)
  95. return 0;
  96. __free_pages_memory(start_pfn, end_pfn);
  97. return end_pfn - start_pfn;
  98. }
  99. unsigned long __init free_low_memory_core_early(int nodeid)
  100. {
  101. unsigned long count = 0;
  102. phys_addr_t start, end, size;
  103. u64 i;
  104. for_each_free_mem_range(i, MAX_NUMNODES, &start, &end, NULL)
  105. count += __free_memory_core(start, end);
  106. /* free range that is used for reserved array if we allocate it */
  107. size = get_allocated_memblock_reserved_regions_info(&start);
  108. if (size)
  109. count += __free_memory_core(start, start + size);
  110. return count;
  111. }
  112. static void reset_node_lowmem_managed_pages(pg_data_t *pgdat)
  113. {
  114. struct zone *z;
  115. /*
  116. * In free_area_init_core(), highmem zone's managed_pages is set to
  117. * present_pages, and bootmem allocator doesn't allocate from highmem
  118. * zones. So there's no need to recalculate managed_pages because all
  119. * highmem pages will be managed by the buddy system. Here highmem
  120. * zone also includes highmem movable zone.
  121. */
  122. for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++)
  123. if (!is_highmem(z))
  124. z->managed_pages = 0;
  125. }
  126. /**
  127. * free_all_bootmem - release free pages to the buddy allocator
  128. *
  129. * Returns the number of pages actually released.
  130. */
  131. unsigned long __init free_all_bootmem(void)
  132. {
  133. struct pglist_data *pgdat;
  134. for_each_online_pgdat(pgdat)
  135. reset_node_lowmem_managed_pages(pgdat);
  136. /*
  137. * We need to use MAX_NUMNODES instead of NODE_DATA(0)->node_id
  138. * because in some case like Node0 doesn't have RAM installed
  139. * low ram will be on Node1
  140. */
  141. return free_low_memory_core_early(MAX_NUMNODES);
  142. }
  143. /**
  144. * free_bootmem_node - mark a page range as usable
  145. * @pgdat: node the range resides on
  146. * @physaddr: starting address of the range
  147. * @size: size of the range in bytes
  148. *
  149. * Partial pages will be considered reserved and left as they are.
  150. *
  151. * The range must reside completely on the specified node.
  152. */
  153. void __init free_bootmem_node(pg_data_t *pgdat, unsigned long physaddr,
  154. unsigned long size)
  155. {
  156. kmemleak_free_part(__va(physaddr), size);
  157. memblock_free(physaddr, size);
  158. }
  159. /**
  160. * free_bootmem - mark a page range as usable
  161. * @addr: starting address of the range
  162. * @size: size of the range in bytes
  163. *
  164. * Partial pages will be considered reserved and left as they are.
  165. *
  166. * The range must be contiguous but may span node boundaries.
  167. */
  168. void __init free_bootmem(unsigned long addr, unsigned long size)
  169. {
  170. kmemleak_free_part(__va(addr), size);
  171. memblock_free(addr, size);
  172. }
  173. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  174. unsigned long align,
  175. unsigned long goal,
  176. unsigned long limit)
  177. {
  178. void *ptr;
  179. if (WARN_ON_ONCE(slab_is_available()))
  180. return kzalloc(size, GFP_NOWAIT);
  181. restart:
  182. ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align, goal, limit);
  183. if (ptr)
  184. return ptr;
  185. if (goal != 0) {
  186. goal = 0;
  187. goto restart;
  188. }
  189. return NULL;
  190. }
  191. /**
  192. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  193. * @size: size of the request in bytes
  194. * @align: alignment of the region
  195. * @goal: preferred starting address of the region
  196. *
  197. * The goal is dropped if it can not be satisfied and the allocation will
  198. * fall back to memory below @goal.
  199. *
  200. * Allocation may happen on any node in the system.
  201. *
  202. * Returns NULL on failure.
  203. */
  204. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  205. unsigned long goal)
  206. {
  207. unsigned long limit = -1UL;
  208. return ___alloc_bootmem_nopanic(size, align, goal, limit);
  209. }
  210. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  211. unsigned long goal, unsigned long limit)
  212. {
  213. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  214. if (mem)
  215. return mem;
  216. /*
  217. * Whoops, we cannot satisfy the allocation request.
  218. */
  219. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  220. panic("Out of memory");
  221. return NULL;
  222. }
  223. /**
  224. * __alloc_bootmem - allocate boot memory
  225. * @size: size of the request in bytes
  226. * @align: alignment of the region
  227. * @goal: preferred starting address of the region
  228. *
  229. * The goal is dropped if it can not be satisfied and the allocation will
  230. * fall back to memory below @goal.
  231. *
  232. * Allocation may happen on any node in the system.
  233. *
  234. * The function panics if the request can not be satisfied.
  235. */
  236. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  237. unsigned long goal)
  238. {
  239. unsigned long limit = -1UL;
  240. return ___alloc_bootmem(size, align, goal, limit);
  241. }
  242. void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
  243. unsigned long size,
  244. unsigned long align,
  245. unsigned long goal,
  246. unsigned long limit)
  247. {
  248. void *ptr;
  249. again:
  250. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  251. goal, limit);
  252. if (ptr)
  253. return ptr;
  254. ptr = __alloc_memory_core_early(MAX_NUMNODES, size, align,
  255. goal, limit);
  256. if (ptr)
  257. return ptr;
  258. if (goal) {
  259. goal = 0;
  260. goto again;
  261. }
  262. return NULL;
  263. }
  264. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  265. unsigned long align, unsigned long goal)
  266. {
  267. if (WARN_ON_ONCE(slab_is_available()))
  268. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  269. return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
  270. }
  271. void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  272. unsigned long align, unsigned long goal,
  273. unsigned long limit)
  274. {
  275. void *ptr;
  276. ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, limit);
  277. if (ptr)
  278. return ptr;
  279. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  280. panic("Out of memory");
  281. return NULL;
  282. }
  283. /**
  284. * __alloc_bootmem_node - allocate boot memory from a specific node
  285. * @pgdat: node to allocate from
  286. * @size: size of the request in bytes
  287. * @align: alignment of the region
  288. * @goal: preferred starting address of the region
  289. *
  290. * The goal is dropped if it can not be satisfied and the allocation will
  291. * fall back to memory below @goal.
  292. *
  293. * Allocation may fall back to any node in the system if the specified node
  294. * can not hold the requested memory.
  295. *
  296. * The function panics if the request can not be satisfied.
  297. */
  298. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  299. unsigned long align, unsigned long goal)
  300. {
  301. if (WARN_ON_ONCE(slab_is_available()))
  302. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  303. return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
  304. }
  305. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  306. unsigned long align, unsigned long goal)
  307. {
  308. return __alloc_bootmem_node(pgdat, size, align, goal);
  309. }
  310. #ifndef ARCH_LOW_ADDRESS_LIMIT
  311. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  312. #endif
  313. /**
  314. * __alloc_bootmem_low - allocate low boot memory
  315. * @size: size of the request in bytes
  316. * @align: alignment of the region
  317. * @goal: preferred starting address of the region
  318. *
  319. * The goal is dropped if it can not be satisfied and the allocation will
  320. * fall back to memory below @goal.
  321. *
  322. * Allocation may happen on any node in the system.
  323. *
  324. * The function panics if the request can not be satisfied.
  325. */
  326. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  327. unsigned long goal)
  328. {
  329. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  330. }
  331. void * __init __alloc_bootmem_low_nopanic(unsigned long size,
  332. unsigned long align,
  333. unsigned long goal)
  334. {
  335. return ___alloc_bootmem_nopanic(size, align, goal,
  336. ARCH_LOW_ADDRESS_LIMIT);
  337. }
  338. /**
  339. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  340. * @pgdat: node to allocate from
  341. * @size: size of the request in bytes
  342. * @align: alignment of the region
  343. * @goal: preferred starting address of the region
  344. *
  345. * The goal is dropped if it can not be satisfied and the allocation will
  346. * fall back to memory below @goal.
  347. *
  348. * Allocation may fall back to any node in the system if the specified node
  349. * can not hold the requested memory.
  350. *
  351. * The function panics if the request can not be satisfied.
  352. */
  353. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  354. unsigned long align, unsigned long goal)
  355. {
  356. if (WARN_ON_ONCE(slab_is_available()))
  357. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  358. return ___alloc_bootmem_node(pgdat, size, align, goal,
  359. ARCH_LOW_ADDRESS_LIMIT);
  360. }