When single-range CMA allocation fails with -ENOMEM (for example, across a memory hole) and falls back to cma_find_free_range(), any free memblock span starting below 4 GiB is skipped outright (!upper_32_bits(start)). On platforms without a hole just below 4 GiB (such as arm64 where DRAM can cross 4 GiB contiguously), that discards the usable >= 4 GiB portion of the first span and can fail with -ENOMEM. It also passes MEMBLOCK_NONE to for_each_free_mem_range() instead of choose_memblock_flags(). On KHO boots, hugetlb_cma_reserve() runs in mm_core_init_early() while kho_noprsrv_only is active and before kho_memory_init() reserves preserved pages in memblock.reserved, so ignoring choose_memblock_flags() can place multi-range CMA areas directly over KHO preserved memory. To fix this: - Clamp each candidate range's lower bound to 4 GiB before alignment while guarding against address wraparound near the top of physical memory. - Honor the active memblock allocation flags so KHO preserved memory is skipped, excluding mirror-only filtering since free-range iteration does not fall back to non-mirrored RAM. - Replace the runtime IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT) check with #ifdef CONFIG_PHYS_ADDR_T_64BIT so clamping to SZ_4G does not truncate on 32-bit phys_addr_t builds. Found by code inspection. Fixes: c009da4258f9 ("mm, cma: support multiple contiguous ranges, if requested") Fixes: d59f43b57480 ("memblock: add support for scratch memory") Signed-off-by: Suseendran Bhaskaran --- mm/cma.c | 21 ++++++++++----------- mm/memblock.c | 2 +- mm/mm_init.h | 2 ++ 3 files changed, 13 insertions(+), 12 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index 0c97aadcfea4..c0db52b4ae29 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -507,20 +507,16 @@ static int __init __cma_declare_contiguous_nid(phys_addr_t *basep, static phys_addr_t __init cma_find_free_range(phys_addr_t align, phys_addr_t size_align, int nid, phys_addr_t *basep) { - phys_addr_t start, end, size; +#ifdef CONFIG_PHYS_ADDR_T_64BIT + enum memblock_flags flags = choose_memblock_flags() & ~MEMBLOCK_MIRROR; + phys_addr_t start, end, size, orig_start; phys_addr_t max = 0; u64 i; - /* no point searching if the system can't address above 4G */ - if (!IS_ENABLED(CONFIG_PHYS_ADDR_T_64BIT)) - return 0; - - for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &start, &end, NULL) { - if (!upper_32_bits(start)) - continue; - - start = ALIGN(start, align); - if (start >= end) + for_each_free_mem_range(i, nid, flags, &start, &end, NULL) { + orig_start = max_t(phys_addr_t, start, SZ_4G); + start = ALIGN(orig_start, align); + if (start < orig_start || start >= end) continue; size = ALIGN_DOWN(end - start, size_align); @@ -531,6 +527,9 @@ static phys_addr_t __init cma_find_free_range(phys_addr_t align, } return max; +#else + return 0; +#endif } static int __init cma_range_cmp(const void *a, const void *b) diff --git a/mm/memblock.c b/mm/memblock.c index a450092a80fb..d81f2074e16e 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -172,7 +172,7 @@ bool __init_memblock memblock_has_mirror(void) return system_has_some_mirror; } -static enum memblock_flags __init_memblock choose_memblock_flags(void) +enum memblock_flags __init_memblock choose_memblock_flags(void) { /* only use KHO_NOPRSRV memory for kho early boot allocations */ if (kho_noprsrv_only) diff --git a/mm/mm_init.h b/mm/mm_init.h index 9ee9bcb8e672..245baa769add 100644 --- a/mm/mm_init.h +++ b/mm/mm_init.h @@ -10,6 +10,7 @@ #include #include +#include #include #include #include @@ -99,6 +100,7 @@ static inline void mminit_verify_zonelist(void) extern bool mirrored_kernelcore; bool memblock_has_mirror(void); +enum memblock_flags choose_memblock_flags(void); void memblock_free_all(void); void memmap_init_page(struct page *page, enum zone_type zone, int nid);