nobootmem.c 11 KB

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