In Meta production, we have seen KHO set aside about 30 GiB of bootmem on 64 GiB hosts and about 42 GiB on 256 GiB hosts, all of which boot with hugetlb_cma=6G. Those totals are from a kernel without "kho: fix bootmem sizing", where each GiB of hugetlb_cma costs 4 GiB of bootmem, so about 24 GiB of the 30 GiB on the 64 GiB hosts comes from the area. With that fix each GiB costs 2 GiB, which is about 12 GiB for hugetlb_cma=6G. On a one-node test guest with hugetlb_cma=1G and that fix, the total bootmem is 2564 MiB without this patch and 516 MiB with it, the same as with no area. Bootmem is released to the page allocator as MIGRATE_CMA, so unmovable allocations can't make proper use of it. In our case we have seen allocation failures, and in at least one of them memory was stranded in CMA. To fix this, let's allocate the area with memblock_phys_alloc_hugetlb(). cma_declare_contiguous_multi() only exists for hugetlb_cma, so the single range that it tries first is allocated that way. This has two effects. On a KHO boot the area avoids the bootmem that the previous kernel handed over, since the allocation retries when a candidate overlaps it. The reservation is also marked MEMBLOCK_RSRV_HUGETLB, which KHO leaves out when it sizes the bootmem for the next kernel. The area also stops preferring mirrored memory, with or without KHO, as memblock_alloc_hugetlb() already does for HugeTLB pages. On a mirror-capable system that doesn't use KHO, hugetlb_cma can therefore be placed somewhere else. That is intended, since the memory is for userspace. The multi-range fallback is not changed, so its ranges are still sized as bootmem. But the single range can no longer overlap bootmem, so a KHO boot reaches the fallback more often. That is only safe with "mm/cma: honour memblock flags in multi-range search" applied first, because without it the fallback can build the area over preserved memory. Making the fallback skip bootmem as well would fail the reservation in a case that works without this series, so it is left as it is. Signed-off-by: Chris Down --- mm/cma.c | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index 3e5f69f315db..c035e67e9353 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -362,8 +362,24 @@ static int __init cma_fixed_reserve(phys_addr_t base, phys_addr_t size) return 0; } +/* + * Importantly, we mark the reservation as MEMBLOCK_RSRV_HUGETLB which keeps the + * area out of KHO bootmem. + */ +static phys_addr_t __init cma_memblock_alloc(phys_addr_t size, + phys_addr_t align, phys_addr_t start, phys_addr_t end, + int nid, bool hugetlb) +{ + if (hugetlb) + return memblock_phys_alloc_hugetlb(size, align, start, end, + nid, true); + + return memblock_alloc_range_nid(size, align, start, end, nid, true); +} + static phys_addr_t __init cma_alloc_mem(phys_addr_t base, phys_addr_t size, - phys_addr_t align, phys_addr_t limit, int nid) + phys_addr_t align, phys_addr_t limit, int nid, + bool hugetlb) { phys_addr_t addr = 0; @@ -378,8 +394,8 @@ static phys_addr_t __init cma_alloc_mem(phys_addr_t base, phys_addr_t size, #ifdef CONFIG_PHYS_ADDR_T_64BIT if (!memblock_bottom_up() && limit >= SZ_4G + size) { memblock_set_bottom_up(true); - addr = memblock_alloc_range_nid(size, align, SZ_4G, limit, - nid, true); + addr = cma_memblock_alloc(size, align, SZ_4G, limit, nid, + hugetlb); memblock_set_bottom_up(false); } #endif @@ -398,15 +414,15 @@ static phys_addr_t __init cma_alloc_mem(phys_addr_t base, phys_addr_t size, * memory in case of failure. */ if (base < highmem && limit > highmem) { - addr = memblock_alloc_range_nid(size, align, highmem, - limit, nid, true); + addr = cma_memblock_alloc(size, align, highmem, limit, + nid, hugetlb); limit = highmem; } } if (!addr) - addr = memblock_alloc_range_nid(size, align, base, limit, nid, - true); + addr = cma_memblock_alloc(size, align, base, limit, nid, + hugetlb); return addr; } @@ -414,8 +430,8 @@ static phys_addr_t __init cma_alloc_mem(phys_addr_t base, phys_addr_t size, static int __init __cma_declare_contiguous_nid(phys_addr_t *basep, phys_addr_t size, phys_addr_t limit, phys_addr_t alignment, unsigned int order_per_bit, - bool fixed, const char *name, struct cma **res_cma, - int nid) + bool fixed, bool hugetlb, const char *name, + struct cma **res_cma, int nid) { phys_addr_t memblock_end = memblock_end_of_DRAM(); phys_addr_t base = *basep; @@ -477,7 +493,8 @@ static int __init __cma_declare_contiguous_nid(phys_addr_t *basep, if (ret) return ret; } else { - base = cma_alloc_mem(base, size, alignment, limit, nid); + base = cma_alloc_mem(base, size, alignment, limit, nid, + hugetlb); if (!base) return -ENOMEM; @@ -560,6 +577,10 @@ static int __init cma_range_cmp(const void *a, const void *b) * Create CMA areas with a total size of @total_size. A normal allocation * for one area is tried first. If that fails, the area is assembled from * up to CMA_MAX_RANGES of the biggest free memblock ranges above 4G. + * + * This is for areas that HugeTLB pages are allocated from, so the one range is + * allocated as HugeTLB memory, see cma_memblock_alloc(). The multiple ranges + * are not, so they can come from KHO bootmem. */ int __init cma_declare_contiguous_multi(phys_addr_t total_size, phys_addr_t align, unsigned int order_per_bit, @@ -577,7 +598,7 @@ int __init cma_declare_contiguous_multi(phys_addr_t total_size, * First, try it the normal way, producing just one range. */ ret = __cma_declare_contiguous_nid(&start, total_size, 0, align, - order_per_bit, false, name, res_cma, nid); + order_per_bit, false, true, name, res_cma, nid); if (!ret) { pr_info("Reserved %lu MiB at %pa\n", size_mb, &start); return 0; @@ -683,7 +704,7 @@ int __init cma_declare_contiguous_nid(phys_addr_t base, int ret; ret = __cma_declare_contiguous_nid(&base, size, limit, alignment, - order_per_bit, fixed, name, res_cma, nid); + order_per_bit, fixed, false, name, res_cma, nid); if (ret != 0) pr_err("Failed to reserve %ld MiB\n", (unsigned long)size / SZ_1M); -- 2.56.1