From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-alma10-1.taild15c8.ts.net [100.103.45.18]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 765D23F8232 for ; Sat, 25 Jul 2026 17:22:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=100.103.45.18 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785000150; cv=none; b=Rrvatb2I7QnhKvouhph40BrAK0Qgv+VH4xKVzl3Pb3w+wEnzHigacH+4PKc5Qt2OfeYcsVo80FpmUfnTaRk+0PuyruOV4kTN6uaQx0+85ZVP6v1/wS3Wy/E8ZTIZMCaZKFyDDzPejPGMy3IYwnPmuwXzN7zx/IETesfB5jJL1bM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1785000150; c=relaxed/simple; bh=QjSz5wOpPIPDaPH7rfXMvVmNesFPE+dT1uT8nuCno/Q=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=k2TyDPHd12Z4P0FY1jqTWUqHt+b6vwpo/9pw+onvegrqsU1Om6NJ7SRtBZwtQJSKapyto+ovCbWx4FnULONudYzPV23GvaKpDIJqtxQZnVQl6NC7LDpBn8cnmWexjdpHYBO7bQ4cu6xsjuKyhKgO7oXwECkq5WMdcjyBok1r9pg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=P7JILLlK; arc=none smtp.client-ip=100.103.45.18 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="P7JILLlK" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 341B31F00A3A; Sat, 25 Jul 2026 17:22:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1785000149; bh=Lp2MijrHUQTs4qI+jQGA5108xF0qDA+oVpojQlZRQQo=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=P7JILLlKvDMGYK6elNEgzs96y+VRFund1Nij9qRztKSbYEAoLr0TXv3dynp9SgBEO KOG7VFs8+ATWMagLlIO0IflGtF1brj9Y2/uFea92i0+LIOUD8jzvTuoqNGLMSJq50K tqKN5Skd7SYlFghNwxtOOtHsqmfudV22Cl2kbfkBfVzBZ6dc0snSkkCJMRBkhDKISA rv0S8/156kjOwAgQKNcoA6XONOyFV1MoWYXDzIcSOXabBr+st5G/0iNcZHgmpGNw1Y HpI7EqETc4aBRGosvv21cys1f//NSOo0KG4St5+JEB1Wk1J+VEWmhwTpQwZXqCYdFV G+yuDodAD373Q== From: Pratyush Yadav To: Mike Rapoport , Pasha Tatashin , Pratyush Yadav , Alexander Graf , Muchun Song , Oscar Salvador , David Hildenbrand , Andrew Morton , Jason Miu , Jork Loeser Cc: kexec@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH v4 17/21] mm/mm_init: don't rely on memblock to get KHO scratch migratetype Date: Sat, 25 Jul 2026 19:21:17 +0200 Message-ID: <20260725172133.4018491-18-pratyush@kernel.org> X-Mailer: git-send-email 2.55.0.229.g6434b31f56-goog In-Reply-To: <20260725172133.4018491-1-pratyush@kernel.org> References: <20260725172133.4018491-1-pratyush@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: "Pratyush Yadav (Google)" Currently struct page init via memmap_init() or deferred_init_memmap() only queries the migrate type from KHO for each discrete memory range. That works currently since KHO scratch memory has a different memory type so it is always it its own region. An upcoming patch will add support for discovering blocks of memory with no preservations and it will mark it as MEMBLOCK_KHO_SCRATCH to allow allocations from them. This can lead to the bootmem KHO scratch areas to be merged into larger free ranges. This merging breaks the selection of migrate type. Get rid of memblock_is_kho_scratch_memory(). Instead, use kho_scratch_overlap() to decide the migrate type of the PFN. Since kho_scratch_migratetype() only uses KHO functions, move it to kexec_handover.h. Instead of calling kho_scratch_migratetype() once for each free range, call it once for each pageblock. Update pageblock_migratetype_init_range() and memmap_init_range() to do so. Since the migrate type is now evaluated for each pageblock and not each free range, drop the migratetype arguments to deferred_free_pages() and memmap_init_zone_range() and use MIGRATE_MOVABLE directly. Signed-off-by: Pratyush Yadav (Google) --- include/linux/kexec_handover.h | 15 +++++++++++++++ include/linux/memblock.h | 19 ------------------- mm/memblock.c | 10 ---------- mm/mm_init.c | 27 +++++++++++++-------------- 4 files changed, 28 insertions(+), 43 deletions(-) diff --git a/include/linux/kexec_handover.h b/include/linux/kexec_handover.h index 18ba417f1ada..c83ec4a15fe7 100644 --- a/include/linux/kexec_handover.h +++ b/include/linux/kexec_handover.h @@ -5,6 +5,7 @@ #include #include #include +#include #include struct kho_vmalloc; @@ -39,6 +40,14 @@ void kho_populate(phys_addr_t fdt_phys, u64 fdt_len, phys_addr_t scratch_phys, u64 scratch_len); bool kho_scratch_overlap(phys_addr_t phys, size_t size); + +static inline enum migratetype kho_scratch_migratetype(unsigned long pfn, + enum migratetype mt) +{ + if (kho_scratch_overlap(PFN_PHYS(pfn), pageblock_nr_pages << PAGE_SHIFT)) + return MIGRATE_CMA; + return mt; +} #else static inline bool kho_is_enabled(void) { @@ -122,6 +131,12 @@ static inline bool kho_scratch_overlap(phys_addr_t phys, size_t size) { return false; } + +static inline enum migratetype kho_scratch_migratetype(unsigned long pfn, + enum migratetype mt) +{ + return mt; +} #endif /* CONFIG_KEXEC_HANDOVER */ #endif /* LINUX_KEXEC_HANDOVER_H */ diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 5afcd99aa8c1..f142c8a8cd5b 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -613,28 +613,9 @@ static inline void memtest_report_meminfo(struct seq_file *m) { } #ifdef CONFIG_MEMBLOCK_KHO_SCRATCH void memblock_set_kho_scratch_only(void); void memblock_clear_kho_scratch_only(void); -bool memblock_is_kho_scratch_memory(phys_addr_t addr); - -static inline enum migratetype kho_scratch_migratetype(unsigned long pfn, - enum migratetype mt) -{ - if (memblock_is_kho_scratch_memory(PFN_PHYS(pfn))) - return MIGRATE_CMA; - return mt; -} #else static inline void memblock_set_kho_scratch_only(void) { } static inline void memblock_clear_kho_scratch_only(void) { } -static inline bool memblock_is_kho_scratch_memory(phys_addr_t addr) -{ - return false; -} - -static inline enum migratetype kho_scratch_migratetype(unsigned long pfn, - enum migratetype mt) -{ - return mt; -} #endif #endif /* _LINUX_MEMBLOCK_H */ diff --git a/mm/memblock.c b/mm/memblock.c index 6349c48154f4..8b2e551435ae 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -2511,16 +2511,6 @@ __init void memblock_clear_kho_scratch_only(void) { kho_scratch_only = false; } - -bool __init_memblock memblock_is_kho_scratch_memory(phys_addr_t addr) -{ - int i = memblock_search(&memblock.memory, addr); - - if (i == -1) - return false; - - return memblock_is_kho_scratch(&memblock.memory.regions[i]); -} #endif #ifdef CONFIG_KEXEC_HANDOVER diff --git a/mm/mm_init.c b/mm/mm_init.c index 30355effe1f2..dc20d6814e48 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -681,7 +681,9 @@ static __meminit void pageblock_migratetype_init_range(unsigned long pfn, const unsigned long end = pfn + nr_pages; for (pfn = pageblock_align(pfn); pfn < end; pfn += pageblock_nr_pages) { - init_pageblock_migratetype(pfn_to_page(pfn), migratetype, false); + enum migratetype mt = kho_scratch_migratetype(pfn, migratetype); + + init_pageblock_migratetype(pfn_to_page(pfn), mt, false); if (!atomic && IS_ALIGNED(pfn, PAGES_PER_SECTION)) cond_resched(); } @@ -932,8 +934,9 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone * over the place during system boot. */ if (pageblock_aligned(pfn)) { - init_pageblock_migratetype(page, migratetype, - isolate_pageblock); + enum migratetype mt = kho_scratch_migratetype(pfn, migratetype); + + init_pageblock_migratetype(page, mt, isolate_pageblock); cond_resched(); } pfn++; @@ -943,8 +946,7 @@ void __meminit memmap_init_range(unsigned long size, int nid, unsigned long zone static void __init memmap_init_zone_range(struct zone *zone, unsigned long start_pfn, unsigned long end_pfn, - unsigned long *hole_pfn, - enum migratetype mt) + unsigned long *hole_pfn) { unsigned long zone_start_pfn = zone->zone_start_pfn; unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages; @@ -957,7 +959,8 @@ static void __init memmap_init_zone_range(struct zone *zone, return; memmap_init_range(end_pfn - start_pfn, nid, zone_id, start_pfn, - zone_end_pfn, MEMINIT_EARLY, NULL, mt, false); + zone_end_pfn, MEMINIT_EARLY, NULL, MIGRATE_MOVABLE, + false); if (*hole_pfn < start_pfn) init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid); @@ -973,8 +976,6 @@ static void __init memmap_init(void) for_each_mem_pfn_range(i, MAX_NUMNODES, &start_pfn, &end_pfn, &nid) { struct pglist_data *node = NODE_DATA(nid); - enum migratetype mt = - kho_scratch_migratetype(start_pfn, MIGRATE_MOVABLE); for (j = 0; j < MAX_NR_ZONES; j++) { struct zone *zone = node->node_zones + j; @@ -983,7 +984,7 @@ static void __init memmap_init(void) continue; memmap_init_zone_range(zone, start_pfn, end_pfn, - &hole_pfn, mt); + &hole_pfn); zone_id = j; } } @@ -1973,7 +1974,7 @@ unsigned long __init node_map_pfn_alignment(void) #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT static void __init deferred_free_pages(unsigned long pfn, - unsigned long nr_pages, enum migratetype mt) + unsigned long nr_pages) { struct page *page; unsigned long i; @@ -1981,7 +1982,7 @@ static void __init deferred_free_pages(unsigned long pfn, if (!nr_pages) return; - pageblock_migratetype_init_range(pfn, nr_pages, mt, true); + pageblock_migratetype_init_range(pfn, nr_pages, MIGRATE_MOVABLE, true); page = pfn_to_page(pfn); @@ -2051,8 +2052,6 @@ deferred_init_memmap_chunk(unsigned long start_pfn, unsigned long end_pfn, for_each_free_mem_range(i, nid, 0, &start, &end, NULL) { unsigned long spfn = PFN_UP(start); unsigned long epfn = PFN_DOWN(end); - enum migratetype mt = - kho_scratch_migratetype(spfn, MIGRATE_MOVABLE); if (spfn >= end_pfn) break; @@ -2065,7 +2064,7 @@ deferred_init_memmap_chunk(unsigned long start_pfn, unsigned long end_pfn, unsigned long chunk_end = min(mo_pfn, epfn); nr_pages += deferred_init_pages(zone, spfn, chunk_end); - deferred_free_pages(spfn, chunk_end - spfn, mt); + deferred_free_pages(spfn, chunk_end - spfn); spfn = chunk_end; -- 2.55.0.229.g6434b31f56-goog