From nobody Fri Apr 3 11:10:26 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CC2142D3ECF for ; Fri, 20 Feb 2026 16:52:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771606335; cv=none; b=YJY/mIrHgEracnEFGtyHANEI31Ovy0+d0wiceBqDnFQr1x0l73L8i6hiF8RMU6enpYFvumWxFY5ldhICNoxf/f2B4GUSINDldEgABQ//LrOVXj+Pv1mivFhhJsHupQgUhGblraSsJL6lqB9CmFSnukBG0pmgPFULFdkvZ+gGyVU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771606335; c=relaxed/simple; bh=m7ka/d1YRmZPGrNxJ7PEcq9nFxIXUV5ui/a4AzsotuY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=k8nnySdCYU5bBD6CJmXr5U0QoAcROtPYN8XAyz01rYsrfeLkpjrXnuZNJO9AYm2Kg96C38ZCBCsA9+KAvlFBNf5zMCHooDriAvBqrH/5KrYeD1b6frdq02ZT2158/VyOvazG3TVAn/3t76o0riSblkmWrW0PNRqhhuum3R4L9BE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mclapinski.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=grTz3aBC; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mclapinski.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="grTz3aBC" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4806cfffca6so22072095e9.2 for ; Fri, 20 Feb 2026 08:52:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771606332; x=1772211132; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=/cSTzAkt5CK3RH1LCsoiWKzafA/Onxi/2uJ8AKZyka0=; b=grTz3aBC7sNlCghPlRfpKOeuJUb1PawAcvn0IdOuafYsoF0++TXQvn+OUHpJxfB2Xg Ra+WmqdgzUa4mLg7oY21jck3hKVlYwwfNlbxhGsWx/TI9qmsEiI4BzIsuZrtlHWqVxjl IbVQgwWWvnPn/V5txg0vTfcFMTDL9sRQxulUeW6N1ZtJ66eVLxr5L+i8ID6UpsXw0pc+ Ij0g8oqb7R07d3mloyrWI60oeu+IyRJRQk6iIj19I2ZVFUMig3hqhFOuCMB4Q4yaktEF i+dxDldPmuIaL8BD8Zg8vzUFI2BYrg1hBTl0DM78F6+QPruQ1Gs0Q9juTYmZPP2SDSgh RpMg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771606332; x=1772211132; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=/cSTzAkt5CK3RH1LCsoiWKzafA/Onxi/2uJ8AKZyka0=; b=sncFSWN5EB92fuHkzWxOeCoTYoDv3MvVEXzxjMiGyU8gELAdLvWHRucqsfojNXs9nW 2Wwufhoy5Z0/GorRPj69z7jRxWq/rAeLAe3gCXc5Aooz7vY78acb/ahe62rRaU2ehP52 YIFseMk2bC6NTCnejkmMZKvzIkZehtpVrQ+yN0jL27ci2VlpCKURbHkCx0dOLkdsHWOn JJzYudLqfdSQDfqCw+AX/tgJks1ll0TaJvg/Sfu9KvDJge0xNPQqSIYHSbgxYQ6tK/2I BCoU0xIXLBSqpqv1V/TFaVGChVlhxk7H2dmj/u8K7ar7XV2JnVKVP9T3KJT9sMBtCv0r wwyg== X-Gm-Message-State: AOJu0Yw9scX1eKd269dIbpyekag28qJlh3aO5s/FUJ3uvwNsGTuYlCZv brd8Q4oPO0E7yssS0rFIlgtJkEqbSXYw79y2WTV3FLeKCjlLa86gntbesvmogG4hHnwRulBYu7K 10V7fUzKRXVQsBxZn7ZJ3uw== X-Received: from wmhn17.prod.google.com ([2002:a05:600c:3051:b0:480:4a03:7b79]) (user=mclapinski job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3153:b0:480:3b4e:41b8 with SMTP id 5b1f17b1804b1-483a9603c07mr2950695e9.33.1771606332067; Fri, 20 Feb 2026 08:52:12 -0800 (PST) Date: Fri, 20 Feb 2026 17:52:02 +0100 In-Reply-To: <20260220165203.3213375-1-mclapinski@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260220165203.3213375-1-mclapinski@google.com> X-Mailer: git-send-email 2.53.0.345.g96ddfc5eaa-goog Message-ID: <20260220165203.3213375-2-mclapinski@google.com> Subject: [PATCH v4 1/2] kho: fix deferred init of kho scratch From: Michal Clapinski To: Evangelos Petrongonas , Pasha Tatashin , Mike Rapoport , Pratyush Yadav , Alexander Graf , kexec@lists.infradead.org, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Michal Clapinski Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently, mm_core_init calls kho_memory_init, which calls kho_release_scratch. If DEFERRED is enabled, kho_release_scratch will first initialize the struct pages of kho scratch. This is not needed. We can just let page_alloc_init_late init it. Next, kho_release_scratch will mark scratch as MIGRATE_CMA. If DEFERRED is enabled, this will be overwritten later in deferred_free_pages. To fix this, I removed the whole kho_release_scratch. Marking the pageblocks as MIGRATE_CMA now happens in kho_init, which runs after deferred_free_pages. Signed-off-by: Michal Clapinski Reviewed-by: Mike Rapoport (Microsoft) --- include/linux/memblock.h | 2 -- kernel/liveupdate/kexec_handover.c | 43 ++++++++---------------------- mm/memblock.c | 22 --------------- 3 files changed, 11 insertions(+), 56 deletions(-) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 221118b5a16e..35d9cf6bbf7a 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -614,11 +614,9 @@ static inline void memtest_report_meminfo(struct seq_f= ile *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); #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) {} #endif =20 #endif /* _LINUX_MEMBLOCK_H */ diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_h= andover.c index b851b09a8e99..de167bfa2c8d 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -1377,11 +1377,6 @@ static __init int kho_init(void) if (err) goto err_free_fdt; =20 - if (fdt) { - kho_in_debugfs_init(&kho_in.dbg, fdt); - return 0; - } - for (int i =3D 0; i < kho_scratch_cnt; i++) { unsigned long base_pfn =3D PHYS_PFN(kho_scratch[i].addr); unsigned long count =3D kho_scratch[i].size >> PAGE_SHIFT; @@ -1397,8 +1392,17 @@ static __init int kho_init(void) */ kmemleak_ignore_phys(kho_scratch[i].addr); for (pfn =3D base_pfn; pfn < base_pfn + count; - pfn +=3D pageblock_nr_pages) - init_cma_reserved_pageblock(pfn_to_page(pfn)); + pfn +=3D pageblock_nr_pages) { + if (fdt) + init_cma_pageblock(pfn_to_page(pfn)); + else + init_cma_reserved_pageblock(pfn_to_page(pfn)); + } + } + + if (fdt) { + kho_in_debugfs_init(&kho_in.dbg, fdt); + return 0; } =20 WARN_ON_ONCE(kho_debugfs_fdt_add(&kho_out.dbg, "fdt", @@ -1421,35 +1425,10 @@ static __init int kho_init(void) } fs_initcall(kho_init); =20 -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 =3D pageblock_start_pfn(PFN_DOWN(start)); - ulong end_pfn =3D pageblock_align(PFN_UP(end)); - ulong pfn; - - for (pfn =3D start_pfn; pfn < end_pfn; pfn +=3D pageblock_nr_pages) - init_pageblock_migratetype(pfn_to_page(pfn), - MIGRATE_CMA, false); - } -} - void __init kho_memory_init(void) { if (kho_in.mem_map_phys) { kho_scratch =3D phys_to_virt(kho_in.scratch_phys); - kho_release_scratch(); kho_mem_deserialize(phys_to_virt(kho_in.mem_map_phys)); } else { kho_reserve_scratch(); diff --git a/mm/memblock.c b/mm/memblock.c index 6cff515d82f4..3eff19124fc0 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -959,28 +959,6 @@ __init void memblock_clear_kho_scratch_only(void) { kho_scratch_only =3D 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 - * reserve_bootmem_region() - */ - __for_each_mem_range(i, &memblock.memory, NULL, NUMA_NO_NODE, - MEMBLOCK_KHO_SCRATCH, &start, &end, &nid) { - for (pfn =3D PFN_UP(start); pfn < PFN_DOWN(end); pfn++) - init_deferred_page(pfn, nid); - } -} #endif =20 /** --=20 2.53.0.345.g96ddfc5eaa-goog From nobody Fri Apr 3 11:10:26 2026 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A1032BF00A for ; Fri, 20 Feb 2026 16:52:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771606338; cv=none; b=ZwU1o3j5lOH/JwQdX2F4VUQ4e4aquAm1Ttwje8HtgKKcKeMEEqfLxFjBCvDlgH8Ljxmd7E/PytGW54qzm/AQu21/U1lJgzNYInhz2n0Mq5X9SOQTqWmVnJl7DLT9bM0DU4k2QjrW1hLScX/PLRuqpuWlbuz8jsSQ6856GZyR5XM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1771606338; c=relaxed/simple; bh=ZUF5nDhfB0/PNuiaYDFcbcNaVNpdob8yeyF9VCGrv4c=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=nN0TulNaIlkNh7SZFWn241wKaSkzeCzyRpVpkdO+/ICNnmEcYgBwCbGAuZzhGXRW90U4dO5MKgfuTrLmzQnuT01C8HM+ey26nVs7GRyMWKyWfDpaewuUs9m+9/Du4rYWysPpHE0+exWufAxRXRMvjuo79+PKbyivxpaizmMRVO4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--mclapinski.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=0jhGj/+v; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--mclapinski.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="0jhGj/+v" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4837b7903f3so27479935e9.2 for ; Fri, 20 Feb 2026 08:52:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1771606335; x=1772211135; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=PT6A/oALwI3aAl9sXp0IZoMRZ6ospSVkKGQtoOfj3Zc=; b=0jhGj/+v1g1m+VKKH3LbcMYF4kHFev/jVdgSE8USK9yJOlN07RZ+PRP799lbumqiPN LQTkuOI3yNTeOHrcWJ8eP/lu/6W/RAuQVgLUtaNOy+0zT5Q3g7UuBFAej/MvmaD+udIm xEg/1TI3IwZkGS5tdSD4aLlgrE3o9uHSBq68AIHogKRpr8frMQwxVNO7LL3dzgxqfibA GYOwDNxXhTu1CxSx0rO99+cjSJDkil/fOFUm7PmsYvz+8jzY1EPAyUN1pdqmUz2FnD/J jBoDParNZz/WJQ0OSug0+Ya1cfH0K/qvYRhR7w10sEhsZldAihs/gZums/aNMEVdTIDT QZfQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1771606335; x=1772211135; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=PT6A/oALwI3aAl9sXp0IZoMRZ6ospSVkKGQtoOfj3Zc=; b=ZdW1fxWzBdOVIJD9Ud0YXAnmibw68HMLglebe+pmXEngugjHd99KYb49HkBneGKmMy rNpEErBz2ZOAUg7h1f5Tonor5JK4QXA+X6KJDE/ebSuvQa3O4R9XD3wZgugBliAmRuSs OrkijFBeEqW4KkmpJLaD9A0n2jEbaObWOSPVLjjzFHIL3L+n7gM7aa8yQvxFulWu+mVk AtYjOj79+KOhXStQp1jXjzmdKIxe26gvhuo9+ETx6CNHduFDaQgRxcmvO+luXpPnLuyY AW/Zzfn3rcseZ9rN3YmXU1iLbQ6q89kY6dsq/ANN46bQ1EL51AW2QAgM5FLniuv+rrO0 Yw+Q== X-Gm-Message-State: AOJu0YzZoSK96P9lKVdoWADDwIUUhJs+jAxbkqzGRO3Oa0dwc8jTQF3n yBnXV73KQhKRdSWGWCLYSUL4ZLr0Auax7efgz/gHTTAwl5YtmP/WMycQ0wHIzTC/tTQ5xRfr9qu q6a6JJCBbjFlBNWn1/yX5Tg== X-Received: from wmpu13.prod.google.com ([2002:a05:600c:4d0d:b0:483:a1ee:5eb8]) (user=mclapinski job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8b03:b0:477:54cd:200e with SMTP id 5b1f17b1804b1-483a95eb622mr2764805e9.1.1771606335280; Fri, 20 Feb 2026 08:52:15 -0800 (PST) Date: Fri, 20 Feb 2026 17:52:03 +0100 In-Reply-To: <20260220165203.3213375-1-mclapinski@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260220165203.3213375-1-mclapinski@google.com> X-Mailer: git-send-email 2.53.0.345.g96ddfc5eaa-goog Message-ID: <20260220165203.3213375-3-mclapinski@google.com> Subject: [PATCH v4 2/2] kho: make preserved pages compatible with deferred struct page init From: Michal Clapinski To: Evangelos Petrongonas , Pasha Tatashin , Mike Rapoport , Pratyush Yadav , Alexander Graf , kexec@lists.infradead.org, linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, Andrew Morton , Michal Clapinski Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Evangelos Petrongonas When CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, struct page initialization is deferred to parallel kthreads that run later in the boot process. During KHO restoration, deserialize_bitmap() writes metadata for each preserved memory region. However, if the struct page has not been initialized, this write targets uninitialized memory, potentially leading to errors like: BUG: unable to handle page fault for address: ... Fix this by introducing kho_get_preserved_page(), which ensures all struct pages in a preserved region are initialized by calling init_deferred_page() which is a no-op when deferred init is disabled or when the struct page is already initialized. Signed-off-by: Evangelos Petrongonas Co-developed-by: Michal Clapinski Signed-off-by: Michal Clapinski Reviewed-by: Pratyush Yadav (Google) Reviewed-by: Pasha Tatashin Reviewed-by: Mike Rapoport (Microsoft) --- I think we can't initialize those struct pages in kho_restore_page. I encountered this stack: page_zone(start_page) __pageblock_pfn_to_page set_zone_contiguous page_alloc_init_late So, at the end of page_alloc_init_late struct pages are expected to be already initialized. set_zone_contiguous() looks at the first and last struct page of each pageblock in each populated zone to figure out if the zone is contiguous. If a kho page lands on a pageblock boundary, this will lead to access of an uninitialized struct page. There is also page_ext_init that invokes pfn_to_nid, which calls page_to_nid for each section-aligned page. There might be other places that do something similar. Therefore, it's a good idea to initialize all struct pages by the end of deferred struct page init. That's why I'm resending Evangelos's patch. I also tried to implement Pratyush's idea, i.e. iterate over zones, then get node from zone. I didn't notice any performance difference even with 8GB of kho. --- kernel/liveupdate/Kconfig | 2 -- kernel/liveupdate/kexec_handover.c | 27 ++++++++++++++++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/kernel/liveupdate/Kconfig b/kernel/liveupdate/Kconfig index 1a8513f16ef7..c13af38ba23a 100644 --- a/kernel/liveupdate/Kconfig +++ b/kernel/liveupdate/Kconfig @@ -1,12 +1,10 @@ # SPDX-License-Identifier: GPL-2.0-only =20 menu "Live Update and Kexec HandOver" - depends on !DEFERRED_STRUCT_PAGE_INIT =20 config KEXEC_HANDOVER bool "kexec handover" depends on ARCH_SUPPORTS_KEXEC_HANDOVER && ARCH_SUPPORTS_KEXEC_FILE - depends on !DEFERRED_STRUCT_PAGE_INIT select MEMBLOCK_KHO_SCRATCH select KEXEC_FILE select LIBFDT diff --git a/kernel/liveupdate/kexec_handover.c b/kernel/liveupdate/kexec_h= andover.c index de167bfa2c8d..fe9c88fd2541 100644 --- a/kernel/liveupdate/kexec_handover.c +++ b/kernel/liveupdate/kexec_handover.c @@ -457,6 +457,31 @@ static int kho_mem_serialize(struct kho_out *kho_out) return err; } =20 +/* + * With CONFIG_DEFERRED_STRUCT_PAGE_INIT, struct pages in higher memory re= gions + * may not be initialized yet at the time KHO deserializes preserved memor= y. + * KHO uses the struct page to store metadata and a later initialization w= ould + * overwrite it. + * Ensure all the struct pages in the preservation are + * initialized. deserialize_bitmap() marks the reservation as noinit to ma= ke + * sure they don't get re-initialized later. + */ +static struct page *__init kho_get_preserved_page(phys_addr_t phys, + unsigned int order) +{ + unsigned long pfn =3D PHYS_PFN(phys); + int nid; + + if (!IS_ENABLED(CONFIG_DEFERRED_STRUCT_PAGE_INIT)) + return pfn_to_page(pfn); + + nid =3D early_pfn_to_nid(pfn); + for (unsigned long i =3D 0; i < (1UL << order); i++) + init_deferred_page(pfn + i, nid); + + return pfn_to_page(pfn); +} + static void __init deserialize_bitmap(unsigned int order, struct khoser_mem_bitmap_ptr *elm) { @@ -467,7 +492,7 @@ static void __init deserialize_bitmap(unsigned int orde= r, int sz =3D 1 << (order + PAGE_SHIFT); phys_addr_t phys =3D elm->phys_start + (bit << (order + PAGE_SHIFT)); - struct page *page =3D phys_to_page(phys); + struct page *page =3D kho_get_preserved_page(phys, order); union kho_page_info info; =20 memblock_reserve(phys, sz); --=20 2.53.0.345.g96ddfc5eaa-goog