nobootmem.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  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. memblock_free(physaddr, size);
  163. }
  164. /**
  165. * free_bootmem - mark a page range as usable
  166. * @addr: starting address of the range
  167. * @size: size of the range in bytes
  168. *
  169. * Partial pages will be considered reserved and left as they are.
  170. *
  171. * The range must be contiguous but may span node boundaries.
  172. */
  173. void __init free_bootmem(unsigned long addr, unsigned long size)
  174. {
  175. memblock_free(addr, size);
  176. }
  177. static void * __init ___alloc_bootmem_nopanic(unsigned long size,
  178. unsigned long align,
  179. unsigned long goal,
  180. unsigned long limit)
  181. {
  182. void *ptr;
  183. if (WARN_ON_ONCE(slab_is_available()))
  184. return kzalloc(size, GFP_NOWAIT);
  185. restart:
  186. ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align, goal, limit);
  187. if (ptr)
  188. return ptr;
  189. if (goal != 0) {
  190. goal = 0;
  191. goto restart;
  192. }
  193. return NULL;
  194. }
  195. /**
  196. * __alloc_bootmem_nopanic - allocate boot memory without panicking
  197. * @size: size of the request in bytes
  198. * @align: alignment of the region
  199. * @goal: preferred starting address of the region
  200. *
  201. * The goal is dropped if it can not be satisfied and the allocation will
  202. * fall back to memory below @goal.
  203. *
  204. * Allocation may happen on any node in the system.
  205. *
  206. * Returns NULL on failure.
  207. */
  208. void * __init __alloc_bootmem_nopanic(unsigned long size, unsigned long align,
  209. unsigned long goal)
  210. {
  211. unsigned long limit = -1UL;
  212. return ___alloc_bootmem_nopanic(size, align, goal, limit);
  213. }
  214. static void * __init ___alloc_bootmem(unsigned long size, unsigned long align,
  215. unsigned long goal, unsigned long limit)
  216. {
  217. void *mem = ___alloc_bootmem_nopanic(size, align, goal, limit);
  218. if (mem)
  219. return mem;
  220. /*
  221. * Whoops, we cannot satisfy the allocation request.
  222. */
  223. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  224. panic("Out of memory");
  225. return NULL;
  226. }
  227. /**
  228. * __alloc_bootmem - allocate boot memory
  229. * @size: size of the request in bytes
  230. * @align: alignment of the region
  231. * @goal: preferred starting address of the region
  232. *
  233. * The goal is dropped if it can not be satisfied and the allocation will
  234. * fall back to memory below @goal.
  235. *
  236. * Allocation may happen on any node in the system.
  237. *
  238. * The function panics if the request can not be satisfied.
  239. */
  240. void * __init __alloc_bootmem(unsigned long size, unsigned long align,
  241. unsigned long goal)
  242. {
  243. unsigned long limit = -1UL;
  244. return ___alloc_bootmem(size, align, goal, limit);
  245. }
  246. void * __init ___alloc_bootmem_node_nopanic(pg_data_t *pgdat,
  247. unsigned long size,
  248. unsigned long align,
  249. unsigned long goal,
  250. unsigned long limit)
  251. {
  252. void *ptr;
  253. again:
  254. ptr = __alloc_memory_core_early(pgdat->node_id, size, align,
  255. goal, limit);
  256. if (ptr)
  257. return ptr;
  258. ptr = __alloc_memory_core_early(NUMA_NO_NODE, size, align,
  259. goal, limit);
  260. if (ptr)
  261. return ptr;
  262. if (goal) {
  263. goal = 0;
  264. goto again;
  265. }
  266. return NULL;
  267. }
  268. void * __init __alloc_bootmem_node_nopanic(pg_data_t *pgdat, unsigned long size,
  269. unsigned long align, unsigned long goal)
  270. {
  271. if (WARN_ON_ONCE(slab_is_available()))
  272. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  273. return ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, 0);
  274. }
  275. static void * __init ___alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  276. unsigned long align, unsigned long goal,
  277. unsigned long limit)
  278. {
  279. void *ptr;
  280. ptr = ___alloc_bootmem_node_nopanic(pgdat, size, align, goal, limit);
  281. if (ptr)
  282. return ptr;
  283. printk(KERN_ALERT "bootmem alloc of %lu bytes failed!\n", size);
  284. panic("Out of memory");
  285. return NULL;
  286. }
  287. /**
  288. * __alloc_bootmem_node - allocate boot memory from a specific node
  289. * @pgdat: node to allocate from
  290. * @size: size of the request in bytes
  291. * @align: alignment of the region
  292. * @goal: preferred starting address of the region
  293. *
  294. * The goal is dropped if it can not be satisfied and the allocation will
  295. * fall back to memory below @goal.
  296. *
  297. * Allocation may fall back to any node in the system if the specified node
  298. * can not hold the requested memory.
  299. *
  300. * The function panics if the request can not be satisfied.
  301. */
  302. void * __init __alloc_bootmem_node(pg_data_t *pgdat, unsigned long size,
  303. unsigned long align, unsigned long goal)
  304. {
  305. if (WARN_ON_ONCE(slab_is_available()))
  306. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  307. return ___alloc_bootmem_node(pgdat, size, align, goal, 0);
  308. }
  309. void * __init __alloc_bootmem_node_high(pg_data_t *pgdat, unsigned long size,
  310. unsigned long align, unsigned long goal)
  311. {
  312. return __alloc_bootmem_node(pgdat, size, align, goal);
  313. }
  314. #ifndef ARCH_LOW_ADDRESS_LIMIT
  315. #define ARCH_LOW_ADDRESS_LIMIT 0xffffffffUL
  316. #endif
  317. /**
  318. * __alloc_bootmem_low - allocate low boot memory
  319. * @size: size of the request in bytes
  320. * @align: alignment of the region
  321. * @goal: preferred starting address of the region
  322. *
  323. * The goal is dropped if it can not be satisfied and the allocation will
  324. * fall back to memory below @goal.
  325. *
  326. * Allocation may happen on any node in the system.
  327. *
  328. * The function panics if the request can not be satisfied.
  329. */
  330. void * __init __alloc_bootmem_low(unsigned long size, unsigned long align,
  331. unsigned long goal)
  332. {
  333. return ___alloc_bootmem(size, align, goal, ARCH_LOW_ADDRESS_LIMIT);
  334. }
  335. void * __init __alloc_bootmem_low_nopanic(unsigned long size,
  336. unsigned long align,
  337. unsigned long goal)
  338. {
  339. return ___alloc_bootmem_nopanic(size, align, goal,
  340. ARCH_LOW_ADDRESS_LIMIT);
  341. }
  342. /**
  343. * __alloc_bootmem_low_node - allocate low boot memory from a specific node
  344. * @pgdat: node to allocate from
  345. * @size: size of the request in bytes
  346. * @align: alignment of the region
  347. * @goal: preferred starting address of the region
  348. *
  349. * The goal is dropped if it can not be satisfied and the allocation will
  350. * fall back to memory below @goal.
  351. *
  352. * Allocation may fall back to any node in the system if the specified node
  353. * can not hold the requested memory.
  354. *
  355. * The function panics if the request can not be satisfied.
  356. */
  357. void * __init __alloc_bootmem_low_node(pg_data_t *pgdat, unsigned long size,
  358. unsigned long align, unsigned long goal)
  359. {
  360. if (WARN_ON_ONCE(slab_is_available()))
  361. return kzalloc_node(size, GFP_NOWAIT, pgdat->node_id);
  362. return ___alloc_bootmem_node(pgdat, size, align, goal,
  363. ARCH_LOW_ADDRESS_LIMIT);
  364. }