init.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Based on arch/arm/mm/init.c
  4. *
  5. * Copyright (C) 1995-2005 Russell King
  6. * Copyright (C) 2012 ARM Ltd.
  7. */
  8. #include <linux/kernel.h>
  9. #include <linux/export.h>
  10. #include <linux/errno.h>
  11. #include <linux/swap.h>
  12. #include <linux/init.h>
  13. #include <linux/cache.h>
  14. #include <linux/mman.h>
  15. #include <linux/nodemask.h>
  16. #include <linux/initrd.h>
  17. #include <linux/gfp.h>
  18. #include <linux/memblock.h>
  19. #include <linux/sort.h>
  20. #include <linux/of.h>
  21. #include <linux/of_fdt.h>
  22. #include <linux/dma-direct.h>
  23. #include <linux/dma-map-ops.h>
  24. #include <linux/efi.h>
  25. #include <linux/swiotlb.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/mm.h>
  28. #include <linux/kexec.h>
  29. #include <linux/crash_dump.h>
  30. #include <linux/hugetlb.h>
  31. #include <linux/acpi_iort.h>
  32. #include <asm/boot.h>
  33. #include <asm/fixmap.h>
  34. #include <asm/kasan.h>
  35. #include <asm/kernel-pgtable.h>
  36. #include <asm/kvm_host.h>
  37. #include <asm/memory.h>
  38. #include <asm/numa.h>
  39. #include <asm/sections.h>
  40. #include <asm/setup.h>
  41. #include <linux/sizes.h>
  42. #include <asm/tlb.h>
  43. #include <asm/alternative.h>
  44. /*
  45. * We need to be able to catch inadvertent references to memstart_addr
  46. * that occur (potentially in generic code) before arm64_memblock_init()
  47. * executes, which assigns it its actual value. So use a default value
  48. * that cannot be mistaken for a real physical address.
  49. */
  50. s64 memstart_addr __ro_after_init = -1;
  51. EXPORT_SYMBOL(memstart_addr);
  52. /*
  53. * If the corresponding config options are enabled, we create both ZONE_DMA
  54. * and ZONE_DMA32. By default ZONE_DMA covers the 32-bit addressable memory
  55. * unless restricted on specific platforms (e.g. 30-bit on Raspberry Pi 4).
  56. * In such case, ZONE_DMA32 covers the rest of the 32-bit addressable memory,
  57. * otherwise it is empty.
  58. *
  59. * Memory reservation for crash kernel either done early or deferred
  60. * depending on DMA memory zones configs (ZONE_DMA) --
  61. *
  62. * In absence of ZONE_DMA configs arm64_dma_phys_limit initialized
  63. * here instead of max_zone_phys(). This lets early reservation of
  64. * crash kernel memory which has a dependency on arm64_dma_phys_limit.
  65. * Reserving memory early for crash kernel allows linear creation of block
  66. * mappings (greater than page-granularity) for all the memory bank rangs.
  67. * In this scheme a comparatively quicker boot is observed.
  68. *
  69. * If ZONE_DMA configs are defined, crash kernel memory reservation
  70. * is delayed until DMA zone memory range size initilazation performed in
  71. * zone_sizes_init(). The defer is necessary to steer clear of DMA zone
  72. * memory range to avoid overlap allocation. So crash kernel memory boundaries
  73. * are not known when mapping all bank memory ranges, which otherwise means
  74. * not possible to exclude crash kernel range from creating block mappings
  75. * so page-granularity mappings are created for the entire memory range.
  76. * Hence a slightly slower boot is observed.
  77. *
  78. * Note: Page-granularity mapppings are necessary for crash kernel memory
  79. * range for shrinking its size via /sys/kernel/kexec_crash_size interface.
  80. */
  81. #if IS_ENABLED(CONFIG_ZONE_DMA) || IS_ENABLED(CONFIG_ZONE_DMA32)
  82. phys_addr_t __ro_after_init arm64_dma_phys_limit;
  83. #else
  84. phys_addr_t __ro_after_init arm64_dma_phys_limit = PHYS_MASK + 1;
  85. #endif
  86. /*
  87. * Provide a run-time mean of disabling ZONE_DMA32 if it is enabled via
  88. * CONFIG_ZONE_DMA32.
  89. */
  90. static bool disable_dma32 __ro_after_init;
  91. #ifdef CONFIG_KEXEC_CORE
  92. /*
  93. * reserve_crashkernel() - reserves memory for crash kernel
  94. *
  95. * This function reserves memory area given in "crashkernel=" kernel command
  96. * line parameter. The memory reserved is used by dump capture kernel when
  97. * primary kernel is crashing.
  98. */
  99. static void __init reserve_crashkernel(void)
  100. {
  101. unsigned long long crash_base, crash_size;
  102. int ret;
  103. ret = parse_crashkernel(boot_command_line, memblock_phys_mem_size(),
  104. &crash_size, &crash_base);
  105. /* no crashkernel= or invalid value specified */
  106. if (ret || !crash_size)
  107. return;
  108. crash_size = PAGE_ALIGN(crash_size);
  109. if (crash_base == 0) {
  110. /* Current arm64 boot protocol requires 2MB alignment */
  111. crash_base = memblock_find_in_range(0, arm64_dma_phys_limit,
  112. crash_size, SZ_2M);
  113. if (crash_base == 0) {
  114. pr_warn("cannot allocate crashkernel (size:0x%llx)\n",
  115. crash_size);
  116. return;
  117. }
  118. } else {
  119. /* User specifies base address explicitly. */
  120. if (!memblock_is_region_memory(crash_base, crash_size)) {
  121. pr_warn("cannot reserve crashkernel: region is not memory\n");
  122. return;
  123. }
  124. if (memblock_is_region_reserved(crash_base, crash_size)) {
  125. pr_warn("cannot reserve crashkernel: region overlaps reserved memory\n");
  126. return;
  127. }
  128. if (!IS_ALIGNED(crash_base, SZ_2M)) {
  129. pr_warn("cannot reserve crashkernel: base address is not 2MB aligned\n");
  130. return;
  131. }
  132. }
  133. memblock_reserve(crash_base, crash_size);
  134. pr_info("crashkernel reserved: 0x%016llx - 0x%016llx (%lld MB)\n",
  135. crash_base, crash_base + crash_size, crash_size >> 20);
  136. crashk_res.start = crash_base;
  137. crashk_res.end = crash_base + crash_size - 1;
  138. }
  139. #else
  140. static void __init reserve_crashkernel(void)
  141. {
  142. }
  143. #endif /* CONFIG_KEXEC_CORE */
  144. #ifdef CONFIG_CRASH_DUMP
  145. static int __init early_init_dt_scan_elfcorehdr(unsigned long node,
  146. const char *uname, int depth, void *data)
  147. {
  148. const __be32 *reg;
  149. int len;
  150. if (depth != 1 || strcmp(uname, "chosen") != 0)
  151. return 0;
  152. reg = of_get_flat_dt_prop(node, "linux,elfcorehdr", &len);
  153. if (!reg || (len < (dt_root_addr_cells + dt_root_size_cells)))
  154. return 1;
  155. elfcorehdr_addr = dt_mem_next_cell(dt_root_addr_cells, &reg);
  156. elfcorehdr_size = dt_mem_next_cell(dt_root_size_cells, &reg);
  157. return 1;
  158. }
  159. /*
  160. * reserve_elfcorehdr() - reserves memory for elf core header
  161. *
  162. * This function reserves the memory occupied by an elf core header
  163. * described in the device tree. This region contains all the
  164. * information about primary kernel's core image and is used by a dump
  165. * capture kernel to access the system memory on primary kernel.
  166. */
  167. static void __init reserve_elfcorehdr(void)
  168. {
  169. of_scan_flat_dt(early_init_dt_scan_elfcorehdr, NULL);
  170. if (!elfcorehdr_size)
  171. return;
  172. if (memblock_is_region_reserved(elfcorehdr_addr, elfcorehdr_size)) {
  173. pr_warn("elfcorehdr is overlapped\n");
  174. return;
  175. }
  176. memblock_reserve(elfcorehdr_addr, elfcorehdr_size);
  177. pr_info("Reserving %lldKB of memory at 0x%llx for elfcorehdr\n",
  178. elfcorehdr_size >> 10, elfcorehdr_addr);
  179. }
  180. #else
  181. static void __init reserve_elfcorehdr(void)
  182. {
  183. }
  184. #endif /* CONFIG_CRASH_DUMP */
  185. /*
  186. * Return the maximum physical address for a zone accessible by the given bits
  187. * limit. If DRAM starts above 32-bit, expand the zone to the maximum
  188. * available memory, otherwise cap it at 32-bit.
  189. */
  190. static phys_addr_t __init max_zone_phys(unsigned int zone_bits)
  191. {
  192. phys_addr_t zone_mask = DMA_BIT_MASK(zone_bits);
  193. phys_addr_t phys_start = memblock_start_of_DRAM();
  194. if (phys_start > U32_MAX)
  195. zone_mask = PHYS_ADDR_MAX;
  196. else if (phys_start > zone_mask)
  197. zone_mask = U32_MAX;
  198. return min(zone_mask, memblock_end_of_DRAM() - 1) + 1;
  199. }
  200. static void __init zone_sizes_init(unsigned long min, unsigned long max)
  201. {
  202. unsigned long max_zone_pfns[MAX_NR_ZONES] = {0};
  203. unsigned int __maybe_unused acpi_zone_dma_bits;
  204. unsigned int __maybe_unused dt_zone_dma_bits;
  205. phys_addr_t __maybe_unused dma32_phys_limit = max_zone_phys(32);
  206. #ifdef CONFIG_ZONE_DMA
  207. acpi_zone_dma_bits = fls64(acpi_iort_dma_get_max_cpu_address());
  208. dt_zone_dma_bits = fls64(of_dma_get_max_cpu_address(NULL));
  209. zone_dma_bits = min3(32U, dt_zone_dma_bits, acpi_zone_dma_bits);
  210. arm64_dma_phys_limit = max_zone_phys(zone_dma_bits);
  211. max_zone_pfns[ZONE_DMA] = PFN_DOWN(arm64_dma_phys_limit);
  212. #endif
  213. #ifdef CONFIG_ZONE_DMA32
  214. max_zone_pfns[ZONE_DMA32] = disable_dma32 ? 0 : PFN_DOWN(dma32_phys_limit);
  215. if (!arm64_dma_phys_limit)
  216. arm64_dma_phys_limit = dma32_phys_limit;
  217. #endif
  218. max_zone_pfns[ZONE_NORMAL] = max;
  219. free_area_init(max_zone_pfns);
  220. }
  221. static int __init early_disable_dma32(char *buf)
  222. {
  223. if (!buf)
  224. return -EINVAL;
  225. if (!strcmp(buf, "on"))
  226. disable_dma32 = true;
  227. return 0;
  228. }
  229. early_param("disable_dma32", early_disable_dma32);
  230. int pfn_valid(unsigned long pfn)
  231. {
  232. phys_addr_t addr = pfn << PAGE_SHIFT;
  233. if ((addr >> PAGE_SHIFT) != pfn)
  234. return 0;
  235. #ifdef CONFIG_SPARSEMEM
  236. if (pfn_to_section_nr(pfn) >= NR_MEM_SECTIONS)
  237. return 0;
  238. if (!valid_section(__pfn_to_section(pfn)))
  239. return 0;
  240. /*
  241. * ZONE_DEVICE memory does not have the memblock entries.
  242. * memblock_is_map_memory() check for ZONE_DEVICE based
  243. * addresses will always fail. Even the normal hotplugged
  244. * memory will never have MEMBLOCK_NOMAP flag set in their
  245. * memblock entries. Skip memblock search for all non early
  246. * memory sections covering all of hotplug memory including
  247. * both normal and ZONE_DEVICE based.
  248. */
  249. if (!early_section(__pfn_to_section(pfn)))
  250. return pfn_section_valid(__pfn_to_section(pfn), pfn);
  251. #endif
  252. return memblock_is_map_memory(addr);
  253. }
  254. EXPORT_SYMBOL(pfn_valid);
  255. static phys_addr_t memory_limit = PHYS_ADDR_MAX;
  256. /*
  257. * Limit the memory size that was specified via FDT.
  258. */
  259. static int __init early_mem(char *p)
  260. {
  261. if (!p)
  262. return 1;
  263. memory_limit = memparse(p, &p) & PAGE_MASK;
  264. pr_notice("Memory limited to %lldMB\n", memory_limit >> 20);
  265. return 0;
  266. }
  267. early_param("mem", early_mem);
  268. static int __init early_init_dt_scan_usablemem(unsigned long node,
  269. const char *uname, int depth, void *data)
  270. {
  271. struct memblock_region *usablemem = data;
  272. const __be32 *reg;
  273. int len;
  274. if (depth != 1 || strcmp(uname, "chosen") != 0)
  275. return 0;
  276. reg = of_get_flat_dt_prop(node, "linux,usable-memory-range", &len);
  277. if (!reg || (len < (dt_root_addr_cells + dt_root_size_cells)))
  278. return 1;
  279. usablemem->base = dt_mem_next_cell(dt_root_addr_cells, &reg);
  280. usablemem->size = dt_mem_next_cell(dt_root_size_cells, &reg);
  281. return 1;
  282. }
  283. static void __init fdt_enforce_memory_region(void)
  284. {
  285. struct memblock_region reg = {
  286. .size = 0,
  287. };
  288. of_scan_flat_dt(early_init_dt_scan_usablemem, &reg);
  289. if (reg.size)
  290. memblock_cap_memory_range(reg.base, reg.size);
  291. }
  292. void __init arm64_memblock_init(void)
  293. {
  294. const s64 linear_region_size = BIT(vabits_actual - 1);
  295. /* Handle linux,usable-memory-range property */
  296. fdt_enforce_memory_region();
  297. /* Remove memory above our supported physical address size */
  298. memblock_remove(1ULL << PHYS_MASK_SHIFT, ULLONG_MAX);
  299. /*
  300. * Select a suitable value for the base of physical memory.
  301. */
  302. memstart_addr = round_down(memblock_start_of_DRAM(),
  303. ARM64_MEMSTART_ALIGN);
  304. /*
  305. * Remove the memory that we will not be able to cover with the
  306. * linear mapping. Take care not to clip the kernel which may be
  307. * high in memory.
  308. */
  309. memblock_remove(max_t(u64, memstart_addr + linear_region_size,
  310. __pa_symbol(_end)), ULLONG_MAX);
  311. if (memstart_addr + linear_region_size < memblock_end_of_DRAM()) {
  312. /* ensure that memstart_addr remains sufficiently aligned */
  313. memstart_addr = round_up(memblock_end_of_DRAM() - linear_region_size,
  314. ARM64_MEMSTART_ALIGN);
  315. memblock_remove(0, memstart_addr);
  316. }
  317. /*
  318. * If we are running with a 52-bit kernel VA config on a system that
  319. * does not support it, we have to place the available physical
  320. * memory in the 48-bit addressable part of the linear region, i.e.,
  321. * we have to move it upward. Since memstart_addr represents the
  322. * physical address of PAGE_OFFSET, we have to *subtract* from it.
  323. */
  324. if (IS_ENABLED(CONFIG_ARM64_VA_BITS_52) && (vabits_actual != 52))
  325. memstart_addr -= _PAGE_OFFSET(48) - _PAGE_OFFSET(52);
  326. /*
  327. * Apply the memory limit if it was set. Since the kernel may be loaded
  328. * high up in memory, add back the kernel region that must be accessible
  329. * via the linear mapping.
  330. */
  331. if (memory_limit != PHYS_ADDR_MAX) {
  332. memblock_mem_limit_remove_map(memory_limit);
  333. memblock_add(__pa_symbol(_text), (u64)(_end - _text));
  334. }
  335. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) {
  336. /*
  337. * Add back the memory we just removed if it results in the
  338. * initrd to become inaccessible via the linear mapping.
  339. * Otherwise, this is a no-op
  340. */
  341. u64 base = phys_initrd_start & PAGE_MASK;
  342. u64 size = PAGE_ALIGN(phys_initrd_start + phys_initrd_size) - base;
  343. /*
  344. * We can only add back the initrd memory if we don't end up
  345. * with more memory than we can address via the linear mapping.
  346. * It is up to the bootloader to position the kernel and the
  347. * initrd reasonably close to each other (i.e., within 32 GB of
  348. * each other) so that all granule/#levels combinations can
  349. * always access both.
  350. */
  351. if (WARN(base < memblock_start_of_DRAM() ||
  352. base + size > memblock_start_of_DRAM() +
  353. linear_region_size,
  354. "initrd not fully accessible via the linear mapping -- please check your bootloader ...\n")) {
  355. phys_initrd_size = 0;
  356. } else {
  357. memblock_remove(base, size); /* clear MEMBLOCK_ flags */
  358. memblock_add(base, size);
  359. memblock_reserve(base, size);
  360. }
  361. }
  362. if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) {
  363. extern u16 memstart_offset_seed;
  364. u64 mmfr0 = read_cpuid(ID_AA64MMFR0_EL1);
  365. int parange = cpuid_feature_extract_unsigned_field(
  366. mmfr0, ID_AA64MMFR0_PARANGE_SHIFT);
  367. s64 range = linear_region_size -
  368. BIT(id_aa64mmfr0_parange_to_phys_shift(parange));
  369. /*
  370. * If the size of the linear region exceeds, by a sufficient
  371. * margin, the size of the region that the physical memory can
  372. * span, randomize the linear region as well.
  373. */
  374. if (memstart_offset_seed > 0 && range >= (s64)ARM64_MEMSTART_ALIGN) {
  375. range /= ARM64_MEMSTART_ALIGN;
  376. memstart_addr -= ARM64_MEMSTART_ALIGN *
  377. ((range * memstart_offset_seed) >> 16);
  378. }
  379. }
  380. /*
  381. * Register the kernel text, kernel data, initrd, and initial
  382. * pagetables with memblock.
  383. */
  384. memblock_reserve(__pa_symbol(_text), _end - _text);
  385. if (IS_ENABLED(CONFIG_BLK_DEV_INITRD) && phys_initrd_size) {
  386. /* the generic initrd code expects virtual addresses */
  387. initrd_start = __phys_to_virt(phys_initrd_start);
  388. initrd_end = initrd_start + phys_initrd_size;
  389. }
  390. early_init_fdt_scan_reserved_mem();
  391. reserve_elfcorehdr();
  392. if (!IS_ENABLED(CONFIG_ZONE_DMA) && !IS_ENABLED(CONFIG_ZONE_DMA32))
  393. reserve_crashkernel();
  394. high_memory = __va(memblock_end_of_DRAM() - 1) + 1;
  395. }
  396. void __init bootmem_init(void)
  397. {
  398. unsigned long min, max;
  399. min = PFN_UP(memblock_start_of_DRAM());
  400. max = PFN_DOWN(memblock_end_of_DRAM());
  401. early_memtest(min << PAGE_SHIFT, max << PAGE_SHIFT);
  402. max_pfn = max_low_pfn = max;
  403. min_low_pfn = min;
  404. arm64_numa_init();
  405. /*
  406. * must be done after arm64_numa_init() which calls numa_init() to
  407. * initialize node_online_map that gets used in hugetlb_cma_reserve()
  408. * while allocating required CMA size across online nodes.
  409. */
  410. #if defined(CONFIG_HUGETLB_PAGE) && defined(CONFIG_CMA)
  411. arm64_hugetlb_cma_reserve();
  412. #endif
  413. dma_pernuma_cma_reserve();
  414. kvm_hyp_reserve();
  415. /*
  416. * sparse_init() tries to allocate memory from memblock, so must be
  417. * done after the fixed reservations
  418. */
  419. sparse_init();
  420. zone_sizes_init(min, max);
  421. /*
  422. * Reserve the CMA area after arm64_dma_phys_limit was initialised.
  423. */
  424. dma_contiguous_reserve(arm64_dma_phys_limit);
  425. /*
  426. * request_standard_resources() depends on crashkernel's memory being
  427. * reserved, so do it here.
  428. */
  429. if (IS_ENABLED(CONFIG_ZONE_DMA) || IS_ENABLED(CONFIG_ZONE_DMA32))
  430. reserve_crashkernel();
  431. memblock_dump_all();
  432. }
  433. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  434. static inline void free_memmap(unsigned long start_pfn, unsigned long end_pfn)
  435. {
  436. struct page *start_pg, *end_pg;
  437. unsigned long pg, pgend;
  438. /*
  439. * Convert start_pfn/end_pfn to a struct page pointer.
  440. */
  441. start_pg = pfn_to_page(start_pfn - 1) + 1;
  442. end_pg = pfn_to_page(end_pfn - 1) + 1;
  443. /*
  444. * Convert to physical addresses, and round start upwards and end
  445. * downwards.
  446. */
  447. pg = (unsigned long)PAGE_ALIGN(__pa(start_pg));
  448. pgend = (unsigned long)__pa(end_pg) & PAGE_MASK;
  449. /*
  450. * If there are free pages between these, free the section of the
  451. * memmap array.
  452. */
  453. if (pg < pgend)
  454. memblock_free(pg, pgend - pg);
  455. }
  456. /*
  457. * The mem_map array can get very big. Free the unused area of the memory map.
  458. */
  459. static void __init free_unused_memmap(void)
  460. {
  461. unsigned long start, end, prev_end = 0;
  462. int i;
  463. for_each_mem_pfn_range(i, MAX_NUMNODES, &start, &end, NULL) {
  464. #ifdef CONFIG_SPARSEMEM
  465. /*
  466. * Take care not to free memmap entries that don't exist due
  467. * to SPARSEMEM sections which aren't present.
  468. */
  469. start = min(start, ALIGN(prev_end, PAGES_PER_SECTION));
  470. #endif
  471. /*
  472. * If we had a previous bank, and there is a space between the
  473. * current bank and the previous, free it.
  474. */
  475. if (prev_end && prev_end < start)
  476. free_memmap(prev_end, start);
  477. /*
  478. * Align up here since the VM subsystem insists that the
  479. * memmap entries are valid from the bank end aligned to
  480. * MAX_ORDER_NR_PAGES.
  481. */
  482. prev_end = ALIGN(end, MAX_ORDER_NR_PAGES);
  483. }
  484. #ifdef CONFIG_SPARSEMEM
  485. if (!IS_ALIGNED(prev_end, PAGES_PER_SECTION))
  486. free_memmap(prev_end, ALIGN(prev_end, PAGES_PER_SECTION));
  487. #endif
  488. }
  489. #endif /* !CONFIG_SPARSEMEM_VMEMMAP */
  490. /*
  491. * mem_init() marks the free areas in the mem_map and tells us how much memory
  492. * is free. This is done after various parts of the system have claimed their
  493. * memory after the kernel image.
  494. */
  495. void __init mem_init(void)
  496. {
  497. if (swiotlb_force == SWIOTLB_FORCE ||
  498. max_pfn > PFN_DOWN(arm64_dma_phys_limit))
  499. swiotlb_init(1);
  500. else
  501. swiotlb_force = SWIOTLB_NO_FORCE;
  502. set_max_mapnr(max_pfn - PHYS_PFN_OFFSET);
  503. #ifndef CONFIG_SPARSEMEM_VMEMMAP
  504. free_unused_memmap();
  505. #endif
  506. /* this will put all unused low memory onto the freelists */
  507. memblock_free_all();
  508. mem_init_print_info(NULL);
  509. /*
  510. * Check boundaries twice: Some fundamental inconsistencies can be
  511. * detected at build time already.
  512. */
  513. #ifdef CONFIG_COMPAT
  514. BUILD_BUG_ON(TASK_SIZE_32 > DEFAULT_MAP_WINDOW_64);
  515. #endif
  516. if (PAGE_SIZE >= 16384 && get_num_physpages() <= 128) {
  517. extern int sysctl_overcommit_memory;
  518. /*
  519. * On a machine this small we won't get anywhere without
  520. * overcommit, so turn it on by default.
  521. */
  522. sysctl_overcommit_memory = OVERCOMMIT_ALWAYS;
  523. }
  524. }
  525. void free_initmem(void)
  526. {
  527. free_reserved_area(lm_alias(__init_begin),
  528. lm_alias(__init_end),
  529. POISON_FREE_INITMEM, "unused kernel");
  530. /*
  531. * Unmap the __init region but leave the VM area in place. This
  532. * prevents the region from being reused for kernel modules, which
  533. * is not supported by kallsyms.
  534. */
  535. unmap_kernel_range((u64)__init_begin, (u64)(__init_end - __init_begin));
  536. }
  537. void dump_mem_limit(void)
  538. {
  539. if (memory_limit != PHYS_ADDR_MAX) {
  540. pr_emerg("Memory Limit: %llu MB\n", memory_limit >> 20);
  541. } else {
  542. pr_emerg("Memory Limit: none\n");
  543. }
  544. }