]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
kho: fix deferred initialization of scratch areas
authorMichal Clapinski <mclapinski@google.com>
Thu, 23 Apr 2026 12:25:36 +0000 (14:25 +0200)
committerMike Rapoport (Microsoft) <rppt@kernel.org>
Sun, 31 May 2026 23:31:37 +0000 (02:31 +0300)
Currently, if CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled,
kho_release_scratch() will initialize the struct pages and set migratetype
of KHO scratch. Unless the whole scratch fits below first_deferred_pfn,
some of that will be overwritten either by deferred_init_pages() or
memmap_init_reserved_range().

To fix it, make memmap_init_range(), deferred_init_memmap_chunk() and
__init_page_from_nid() recognize KHO scratch regions and set
migratetype of pageblocks in those regions to MIGRATE_CMA.

Co-developed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Signed-off-by: Michal Clapinski <mclapinski@google.com>
Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com>
Reviewed-by: Pratyush Yadav (Google) <pratyush@kernel.org>
Link: https://patch.msgid.link/20260423122538.140993-2-mclapinski@google.com
Signed-off-by: Mike Rapoport (Microsoft) <rppt@kernel.org>
include/linux/memblock.h
kernel/liveupdate/kexec_handover.c
mm/memblock.c
mm/mm_init.c

index b0f750d22a7b5e3056d2288625be9e276b9622fb..5afcd99aa8c144076dca9dd4d8b575b87a6f920e 100644 (file)
@@ -613,11 +613,28 @@ 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);
-void memmap_init_kho_scratch_pages(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 void memmap_init_kho_scratch_pages(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 */
index 1b592d86dc4898858adfc35b9dfa2ae551acabf3..a0aa8281dba16d0491af7c559419dce61549a0c6 100644 (file)
@@ -1584,35 +1584,10 @@ err_free_scratch:
 }
 fs_initcall(kho_init);
 
-static void __init kho_release_scratch(void)
-{
-       phys_addr_t start, end;
-       u64 i;
-
-       memmap_init_kho_scratch_pages();
-
-       /*
-        * Mark scratch mem as CMA before we return it. That way we
-        * ensure that no kernel allocations happen on it. That means
-        * we can reuse it as scratch memory again later.
-        */
-       __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE,
-                            MEMBLOCK_KHO_SCRATCH, &start, &end, NULL) {
-               ulong start_pfn = pageblock_start_pfn(PFN_DOWN(start));
-               ulong end_pfn = pageblock_align(PFN_UP(end));
-               ulong pfn;
-
-               for (pfn = start_pfn; pfn < end_pfn; pfn += pageblock_nr_pages)
-                       init_pageblock_migratetype(pfn_to_page(pfn),
-                                                  MIGRATE_CMA, false);
-       }
-}
-
 void __init kho_memory_init(void)
 {
        if (kho_in.scratch_phys) {
                kho_scratch = phys_to_virt(kho_in.scratch_phys);
-               kho_release_scratch();
 
                if (kho_mem_retrieve(kho_get_fdt()))
                        kho_in.fdt_phys = 0;
index ccd43f3abb82d131d46826b5db6e7641367b1ca4..6349c48154f4b19f30bb2dae5802742ada1afb63 100644 (file)
@@ -1028,40 +1028,6 @@ int __init_memblock memblock_physmem_add(phys_addr_t base, phys_addr_t size)
 }
 #endif
 
-#ifdef CONFIG_MEMBLOCK_KHO_SCRATCH
-__init void memblock_set_kho_scratch_only(void)
-{
-       kho_scratch_only = true;
-}
-
-__init void memblock_clear_kho_scratch_only(void)
-{
-       kho_scratch_only = false;
-}
-
-__init void memmap_init_kho_scratch_pages(void)
-{
-       phys_addr_t start, end;
-       unsigned long pfn;
-       int nid;
-       u64 i;
-
-       if (!IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT))
-               return;
-
-       /*
-        * Initialize struct pages for free scratch memory.
-        * The struct pages for reserved scratch memory will be set up in
-        * memmap_init_reserved_pages()
-        */
-       __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE,
-                            MEMBLOCK_KHO_SCRATCH, &start, &end, &nid) {
-               for (pfn = PFN_UP(start); pfn < PFN_DOWN(end); pfn++)
-                       init_deferred_page(pfn, nid);
-       }
-}
-#endif
-
 /**
  * memblock_setclr_flag - set or clear flag for a memory region
  * @type: memblock type to set/clear flag for
@@ -2535,6 +2501,28 @@ int reserve_mem_release_by_name(const char *name)
        return 1;
 }
 
+#ifdef CONFIG_MEMBLOCK_KHO_SCRATCH
+__init void memblock_set_kho_scratch_only(void)
+{
+       kho_scratch_only = true;
+}
+
+__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
 
 static int __init reserved_mem_preserve(void)
index f9f8e1af921cda91d812cffdad2f1a0698cfd6ac..eddc0f03a7792b93015517e975ecc4e71fc4966f 100644 (file)
@@ -692,9 +692,11 @@ void __meminit __init_page_from_nid(unsigned long pfn, int nid)
        }
        __init_single_page(pfn_to_page(pfn), pfn, zid, nid);
 
-       if (pageblock_aligned(pfn))
-               init_pageblock_migratetype(pfn_to_page(pfn), MIGRATE_MOVABLE,
-                               false);
+       if (pageblock_aligned(pfn)) {
+               enum migratetype mt =
+                       kho_scratch_migratetype(pfn, MIGRATE_MOVABLE);
+               init_pageblock_migratetype(pfn_to_page(pfn), mt, false);
+       }
 }
 
 #ifdef CONFIG_DEFERRED_STRUCT_PAGE_INIT
@@ -927,7 +929,8 @@ 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)
+                                         unsigned long *hole_pfn,
+                                         enum migratetype mt)
 {
        unsigned long zone_start_pfn = zone->zone_start_pfn;
        unsigned long zone_end_pfn = zone_start_pfn + zone->spanned_pages;
@@ -940,8 +943,7 @@ 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, MIGRATE_MOVABLE,
-                         false);
+                         zone_end_pfn, MEMINIT_EARLY, NULL, mt, false);
 
        if (*hole_pfn < start_pfn)
                init_unavailable_range(*hole_pfn, start_pfn, zone_id, nid);
@@ -957,6 +959,8 @@ 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;
@@ -965,7 +969,7 @@ static void __init memmap_init(void)
                                continue;
 
                        memmap_init_zone_range(zone, start_pfn, end_pfn,
-                                              &hole_pfn);
+                                              &hole_pfn, mt);
                        zone_id = j;
                }
        }
@@ -1970,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)
+               unsigned long nr_pages, enum migratetype mt)
 {
        struct page *page;
        unsigned long i;
@@ -1983,8 +1987,7 @@ static void __init deferred_free_pages(unsigned long pfn,
        /* Free a large naturally-aligned chunk if possible */
        if (nr_pages == MAX_ORDER_NR_PAGES && IS_MAX_ORDER_ALIGNED(pfn)) {
                for (i = 0; i < nr_pages; i += pageblock_nr_pages)
-                       init_pageblock_migratetype(page + i, MIGRATE_MOVABLE,
-                                       false);
+                       init_pageblock_migratetype(page + i, mt, false);
                __free_pages_core(page, MAX_PAGE_ORDER, MEMINIT_EARLY);
                return;
        }
@@ -1994,8 +1997,7 @@ static void __init deferred_free_pages(unsigned long pfn,
 
        for (i = 0; i < nr_pages; i++, page++, pfn++) {
                if (pageblock_aligned(pfn))
-                       init_pageblock_migratetype(page, MIGRATE_MOVABLE,
-                                       false);
+                       init_pageblock_migratetype(page, mt, false);
                __free_pages_core(page, 0, MEMINIT_EARLY);
        }
 }
@@ -2053,6 +2055,8 @@ 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 +2069,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);
+                       deferred_free_pages(spfn, chunk_end - spfn, mt);
 
                        spfn = chunk_end;