From: Suseendran Bhaskaran When single-range CMA allocation fails with -ENOMEM (for example, across a memory hole) and falls back to cma_find_free_range(), the search 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, honour 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. Found by code inspection. Fixes: d59f43b57480 ("memblock: add support for scratch memory") [cdown: update changelog for pared down patch and reduce to flags] Signed-off-by: Chris Down --- mm/cma.c | 12 +++++++++++- mm/memblock.c | 2 +- mm/mm_init.h | 2 ++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/mm/cma.c b/mm/cma.c index de35cfc02340..3e5f69f315db 100644 --- a/mm/cma.c +++ b/mm/cma.c @@ -508,11 +508,21 @@ 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) { + enum memblock_flags flags = choose_memblock_flags(); phys_addr_t start, end, size; phys_addr_t max = 0; u64 i; - for_each_free_mem_range(i, nid, MEMBLOCK_NONE, &start, &end, NULL) { + /* + * Only use memory that memblock itself can allocate from. After a KHO + * kexec, the rest may hold memory that the previous kernel preserved, + * and that memory is not reserved in memblock yet. Mirrored memory is + * only a preference for memblock allocations, so don't limit the search + * to it. + */ + flags &= ~MEMBLOCK_MIRROR; + + for_each_free_mem_range(i, nid, flags, &start, &end, NULL) { if (start < SZ_4G) start = SZ_4G; 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); -- 2.56.1