nobootmem.c 11 KB

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