nobootmem.c 11 KB

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