Skip to contentSkip to conversation

kho: add support for deferred struct page init

PATCH In review Evangelos Petrongonas 5 participants 30 messages Dec 16, 2025 – Feb 5

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kho: add support for deferred struct page init

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.

Fixes 8b66ed2c3f42 ("kho: mm: don't allow deferred struct page with KHO")

---
### Notes
@Jason, this patch should act as a temporary fix to make KHO play nice with deferred struct page init until you post your ideas about splitting "Physical Reservation" from "Metadata Restoration".

### Testing
In order to test the fix, I modified the KHO selftest, to allocate more memory and do so from higher memory to trigger the incompatibility. The branch with those changes can be found in:
https://git.infradead.org/?p=users/vpetrog/linux.git;a=shortlog;h=refs/heads/kho-deferred-struct-page-init

In future patches, we might want to enhance the selftest to cover this case as well. However, properly adopting the test for this is much more work than the actual fix, therefore it can be deferred to a follow-up series.

In addition attempting to run the selftest for arm (without my changes) fails with:

```
ERROR:target/arm/internals.h:767:regime_is_user: code should not be reached
Bail out! ERROR:target/arm/internals.h:767:regime_is_user: code should not be reached
./tools/testing/selftests/kho/vmtest.sh: line 113: 61609 Aborted
```

I have not looked it up further, but can also do so as part of a selftest follow-up.

 kernel/liveupdate/Kconfig          |  2 --
 kernel/liveupdate/kexec_handover.c | 19 ++++++++++++++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)
kernel/liveupdate/Kconfig +0 -2
index d2aeaf13c3ac..9394a608f939 100644
@@ -1,12 +1,10 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 menu "Live Update and Kexec HandOver"
-	depends on !DEFERRED_STRUCT_PAGE_INIT
 
 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
kernel/liveupdate/kexec_handover.c +18 -1
index 9dc51fab604f..78cfe71e6107 100644
@@ -439,6 +439,23 @@ static int kho_mem_serialize(struct kho_out *kho_out)
 	return err;
 }
 
+/*
+ * With CONFIG_DEFERRED_STRUCT_PAGE_INIT, struct pages in higher memory
+ * regions may not be initialized yet at the time KHO deserializes preserved
+ * memory. This function ensures all struct pages in the region are initialized.
+ */
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+						  unsigned int order)
+{
+	unsigned long pfn = PHYS_PFN(phys);
+	int nid = early_pfn_to_nid(pfn);
+
+	for (int i = 0; i < (1 << 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)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
 		int sz = 1 << (order + PAGE_SHIFT);
 		phys_addr_t phys =
 			elm->phys_start + (bit << (order + PAGE_SHIFT));
-		struct page *page = phys_to_page(phys);
+		struct page *page = kho_get_preserved_page(phys, order);
 		union kho_page_info info;
 
 		memblock_reserve(phys, sz);
Signature · 12 lines
2.43.0




Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
maintainer
#1 lore raw
Quoted 13 lines · 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: ...
```
Quoted 6 lines

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.

Fixes 8b66ed2c3f42 ("kho: mm: don't allow deferred struct page with KHO")

You are adding a new feature. Backporting this to stable is not needed.

Quoted 78 lines

---
### Notes
@Jason, this patch should act as a temporary fix to make KHO play nice with deferred struct page init until you post your ideas about splitting "Physical Reservation" from "Metadata Restoration".

### Testing
In order to test the fix, I modified the KHO selftest, to allocate more memory and do so from higher memory to trigger the incompatibility. The branch with those changes can be found in:
https://git.infradead.org/?p=users/vpetrog/linux.git;a=shortlog;h=refs/heads/kho-deferred-struct-page-init

In future patches, we might want to enhance the selftest to cover this case as well. However, properly adopting the test for this is much more work than the actual fix, therefore it can be deferred to a follow-up series.

In addition attempting to run the selftest for arm (without my changes) fails with:

```
ERROR:target/arm/internals.h:767:regime_is_user: code should not be reached
Bail out! ERROR:target/arm/internals.h:767:regime_is_user: code should not be reached
./tools/testing/selftests/kho/vmtest.sh: line 113: 61609 Aborted
```

I have not looked it up further, but can also do so as part of a selftest follow-up.

 kernel/liveupdate/Kconfig          |  2 --
 kernel/liveupdate/kexec_handover.c | 19 ++++++++++++++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)
kernel/liveupdate/Kconfig +0 -2
index d2aeaf13c3ac..9394a608f939 100644
@@ -1,12 +1,10 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 menu "Live Update and Kexec HandOver"
-       depends on !DEFERRED_STRUCT_PAGE_INIT
 
 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
kernel/liveupdate/kexec_handover.c +17 -0
index 9dc51fab604f..78cfe71e6107 100644
@@ -439,6 +439,23 @@ static int kho_mem_serialize(struct kho_out *kho_out)
        return err;
 }
 
+/*
+ * With CONFIG_DEFERRED_STRUCT_PAGE_INIT, struct pages in higher memory
+ * regions may not be initialized yet at the time KHO deserializes preserved
+ * memory. This function ensures all struct pages in the region are initialized.
+ */
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+                                                 unsigned int order)
+{
+       unsigned long pfn = PHYS_PFN(phys);
+       int nid = early_pfn_to_nid(pfn);
+
+       for (int i = 0; i < (1 << 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)
 {

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 472

Quoted 8 lines
kernel/liveupdate/kexec_handover.c +1 -1
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
                int sz = 1 << (order + PAGE_SHIFT);
                phys_addr_t phys =
                        elm->phys_start + (bit << (order + PAGE_SHIFT));
-               struct page *page = phys_to_page(phys);
+               struct page *page = kho_get_preserved_page(phys, order);
                union kho_page_info info;
 
                memblock_reserve(phys, sz);

In deferred_init_memmap_chunk() we initialize deferred struct pages in this iterator:

for_each_free_mem_range(i, nid, 0, &start, &end, NULL) {
init_deferred_page()
}

However, since, memblock_reserve() is called, the memory is not going to be part of the for_each_free_mem_range() iterator. So, I think the proposed patch should work.

Pratyush, what happens if we deserialize a HugeTLB with HVO? Since HVO optimizes out the unique backing struct pages for tail pages, blindly iterating and initializing them via init_deferred_page() might corrupt the shared struct page mapping.

Pasha

maintainer
#2 lore raw
In reply to Evangelos Petrongonas

Hi Evangelos,

Quoted 1 line · Evangelos Petrongonas

When CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, struct page

No need for markup formatting in the changelog.

Quoted 19 lines

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.

Fixes 8b66ed2c3f42 ("kho: mm: don't allow deferred struct page with KHO")

---

...

kernel/liveupdate/kexec_handover.c static int kho_mem_serialize(struct kho_out *kho_out) · line 454

Quoted 8 lines
kernel/liveupdate/kexec_handover.c +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+						  unsigned int order)
+{
+	unsigned long pfn = PHYS_PFN(phys);
+	int nid = early_pfn_to_nid(pfn);
+
+	for (int i = 0; i < (1 << order); i++)
+		init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 13 lines
kernel/liveupdate/kexec_handover.c +5 -1
+
+	return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
 				      struct khoser_mem_bitmap_ptr *elm)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
 		int sz = 1 << (order + PAGE_SHIFT);
 		phys_addr_t phys =
 			elm->phys_start + (bit << (order + PAGE_SHIFT));
-		struct page *page = phys_to_page(phys);
+		struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does heavy lifting of memblock_reserve()s. Delaying struct page initialization until restore makes it at least run in parallel with other initialization tasks.

I started to work on this just before plumbers and I have something untested here:

https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git/log/?h=kho/deferred-page/v0.1

Quoted 17 lines
kernel/liveupdate/kexec_handover.c +0 -0
 		union kho_page_info info;
 
 		memblock_reserve(phys, sz);
Signature · 12 lines
2.43.0




Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
Signature · 2 lines
Sincerely yours,
Mike.
#3 lore raw
Quoted 7 lines · On Tue, Dec 16, 2025 at 01:57:19PM +0200 Mike Rapoport wrote:

Hi Evangelos,

Quoted 1 line · Evangelos Petrongonas

When CONFIG_DEFERRED_STRUCT_PAGE_INIT is enabled, struct page

No need for markup formatting in the changelog.

ack

Quoted 22 lines
Quoted 19 lines

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.

Fixes 8b66ed2c3f42 ("kho: mm: don't allow deferred struct page with KHO")

---

...

Quoted 12 lines

kernel/liveupdate/kexec_handover.c static int kho_mem_serialize(struct kho_out *kho_out) · line 454

Quoted 8 lines
kernel/liveupdate/kexec_handover.c +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+						  unsigned int order)
+{
+	unsigned long pfn = PHYS_PFN(phys);
+	int nid = early_pfn_to_nid(pfn);
+
+	for (int i = 0; i < (1 << order); i++)
+		init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

Right, __init_page_from_nid() unconditionally initializes the page.

Quoted 20 lines

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 13 lines
kernel/liveupdate/kexec_handover.c +5 -1
+
+	return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
 				      struct khoser_mem_bitmap_ptr *elm)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
 		int sz = 1 << (order + PAGE_SHIFT);
 		phys_addr_t phys =
 			elm->phys_start + (bit << (order + PAGE_SHIFT));
-		struct page *page = phys_to_page(phys);
+		struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does heavy lifting of memblock_reserve()s. Delaying struct page initialization until restore makes it at least run in parallel with other initialization tasks.

Quoted 5 lines

I started to work on this just before plumbers and I have something untested here:

https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git/log/?h=kho/deferred-page/v0.1

Nice suggestion! I looked at your branch and I agree, your approach seems better.

I also noticed your debug check:

```
	if (IS_ENABLED(CONFIG_KEXEC_HANDOVER_DEBUG))
		WARN_ON(nid != early_pfn_to_nid(pfn + i));
```

This catches, or at least allows for easier debugging of, another potential (although unlinkely (?)) issue that my patch missed:
preserved pages spanning multiple NUMA nodes within a single higher-order allocation. Nice to have this :)

I am happy to drop my patch in favor of yours. FWIW I have quickly tested it both using the modified selftest and a custom payload and it seems to be working fine. Please let me know once you post the patches.

Quoted 9 lines
Quoted 9 lines
kernel/liveupdate/kexec_handover.c +0 -0
 		union kho_page_info info;
 
 		memblock_reserve(phys, sz);
Signature · 1 line
2.43.0
Quoted 12 lines
Quoted 8 lines

Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597

Signature · 2 lines
Sincerely yours,
Mike.

Kind Regards,
Evangelos

Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597

maintainer
#4 lore raw
In reply to Mike Rapoport
Quoted 11 lines

kernel/liveupdate/kexec_handover.c static int kho_mem_serialize(struct kho_out *kho_out) · line 454

Quoted 8 lines
kernel/liveupdate/kexec_handover.c +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+                                               unsigned int order)
+{
+     unsigned long pfn = PHYS_PFN(phys);
+     int nid = early_pfn_to_nid(pfn);
+
+     for (int i = 0; i < (1 << order); i++)
+             init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

Mike, but those struct pages should be initialized early anyway. If they are not yet initialized we have a problem, as they are going to be re-initialized later.

Quoted 17 lines
Quoted 9 lines
kernel/liveupdate/kexec_handover.c +4 -0
+
+     return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
                                    struct khoser_mem_bitmap_ptr *elm)
 {

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 5 lines
kernel/liveupdate/kexec_handover.c +1 -1
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
              int sz = 1 << (order + PAGE_SHIFT);
              phys_addr_t phys =
                      elm->phys_start + (bit << (order + PAGE_SHIFT));
-             struct page *page = phys_to_page(phys);
+             struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does

The KHO memory should still be accessible early in boot, right?

Quoted 30 lines

heavy lifting of memblock_reserve()s. Delaying struct page initialization until restore makes it at least run in parallel with other initialization tasks.

I started to work on this just before plumbers and I have something untested here:

https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git/log/?h=kho/deferred-page/v0.1

Quoted 9 lines
kernel/liveupdate/kexec_handover.c +0 -1
              union kho_page_info info;
 
              memblock_reserve(phys, sz);
--

2.43.0

Quoted 8 lines

Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597

--
Sincerely yours,
Mike.

maintainer
#5 lore raw
Quoted 12 lines · Pasha Tatashin
Quoted 11 lines

kernel/liveupdate/kexec_handover.c static int kho_mem_serialize(struct kho_out *kho_out) · line 454

Quoted 8 lines
kernel/liveupdate/kexec_handover.c +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+                                               unsigned int order)
+{
+     unsigned long pfn = PHYS_PFN(phys);
+     int nid = early_pfn_to_nid(pfn);
+
+     for (int i = 0; i < (1 << order); i++)
+             init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

Quoted 3 lines

Mike, but those struct pages should be initialized early anyway. If they are not yet initialized we have a problem, as they are going to be re-initialized later.

Can say I understand your point. Which pages should be initialized earlt? And which pages will be reinitialized?

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
kernel/liveupdate/kexec_handover.c +4 -0
+
+     return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
                                    struct khoser_mem_bitmap_ptr *elm)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
Quoted 8 lines

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 5 lines
kernel/liveupdate/kexec_handover.c +1 -1
              int sz = 1 << (order + PAGE_SHIFT);
              phys_addr_t phys =
                      elm->phys_start + (bit << (order + PAGE_SHIFT));
-             struct page *page = phys_to_page(phys);
+             struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does

Quoted 1 line

The KHO memory should still be accessible early in boot, right?

The memory is accessible. And we anyway should not use struct page for preserved memory before kho_restore_{folio,pages}.

Quoted 13 lines
Quoted 13 lines

heavy lifting of memblock_reserve()s. Delaying struct page initialization until restore makes it at least run in parallel with other initialization tasks.

I started to work on this just before plumbers and I have something untested here:

https://git.kernel.org/pub/scm/linux/kernel/git/rppt/linux.git/log/?h=kho/deferred-page/v0.1

Quoted 4 lines
kernel/liveupdate/kexec_handover.c +0 -1
              union kho_page_info info;
 
              memblock_reserve(phys, sz);
--
Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#6 lore raw
Quoted 14 lines · Mike Rapoport
Quoted 12 lines · Pasha Tatashin
Quoted 11 lines

kernel/liveupdate/kexec_handover.c static int kho_mem_serialize(struct kho_out *kho_out) · line 454

Quoted 8 lines
kernel/liveupdate/kexec_handover.c +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+                                               unsigned int order)
+{
+     unsigned long pfn = PHYS_PFN(phys);
+     int nid = early_pfn_to_nid(pfn);
+
+     for (int i = 0; i < (1 << order); i++)
+             init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

Quoted 5 lines
Quoted 3 lines

Mike, but those struct pages should be initialized early anyway. If they are not yet initialized we have a problem, as they are going to be re-initialized later.

Can say I understand your point. Which pages should be initialized earlt?

All pages below node->first_deferred_pfn.

Quoted 1 line

And which pages will be reinitialized?

kho_memory_init() is called after free_area_init() (which calls memmap_init_range to initialize low memory struct pages). So, if we use __init_page_from_nid() as suggested, we would be blindly running __init_single_page() again on those low-memory pages that memmap_init_range() already set up. This would cause double initialization and corruptions due to losing the order information.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 13 lines
kernel/liveupdate/kexec_handover.c +5 -1
+
+     return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
                                    struct khoser_mem_bitmap_ptr *elm)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
              int sz = 1 << (order + PAGE_SHIFT);
              phys_addr_t phys =
                      elm->phys_start + (bit << (order + PAGE_SHIFT));
-             struct page *page = phys_to_page(phys);
+             struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does

Quoted 4 lines
Quoted 1 line

The KHO memory should still be accessible early in boot, right?

The memory is accessible. And we anyway should not use struct page for preserved memory before kho_restore_{folio,pages}.

This makes sense, what happens if someone calls kho_restore_folio() before deferred pages are initialized?

maintainer
#7 lore raw
Quoted 50 lines · Pasha Tatashin
Quoted 14 lines · Mike Rapoport
Quoted 12 lines · Pasha Tatashin
Quoted 11 lines

kernel/liveupdate/kexec_handover.c static int kho_mem_serialize(struct kho_out *kho_out) · line 454

Quoted 8 lines
kernel/liveupdate/kexec_handover.c +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+                                               unsigned int order)
+{
+     unsigned long pfn = PHYS_PFN(phys);
+     int nid = early_pfn_to_nid(pfn);
+
+     for (int i = 0; i < (1 << order); i++)
+             init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

Quoted 5 lines
Quoted 3 lines

Mike, but those struct pages should be initialized early anyway. If they are not yet initialized we have a problem, as they are going to be re-initialized later.

Can say I understand your point. Which pages should be initialized earlt?

All pages below node->first_deferred_pfn.

Quoted 1 line

And which pages will be reinitialized?

kho_memory_init() is called after free_area_init() (which calls memmap_init_range to initialize low memory struct pages). So, if we use __init_page_from_nid() as suggested, we would be blindly running __init_single_page() again on those low-memory pages that memmap_init_range() already set up. This would cause double initialization and corruptions due to losing the order information.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 13 lines
kernel/liveupdate/kexec_handover.c +5 -1
+
+     return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
                                    struct khoser_mem_bitmap_ptr *elm)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
              int sz = 1 << (order + PAGE_SHIFT);
              phys_addr_t phys =
                      elm->phys_start + (bit << (order + PAGE_SHIFT));
-             struct page *page = phys_to_page(phys);
+             struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The KHO memory should still be accessible early in boot, right?

The memory is accessible. And we anyway should not use struct page for preserved memory before kho_restore_{folio,pages}.

This makes sense, what happens if someone calls kho_restore_folio() before deferred pages are initialized?

I looked at your repo. I think what you're proposing makes sense, and indeed it will provide a performance boost if some of the folios are restored in parallel. Just kho_init_deferred_pages() should be using init_deferred_page() to avoid re-initializing the lower memory pages. Also, I am still wondering how it will work with HVO, but I need to take a look at Pratyuh's series for that.

Thanks,
Pasha

maintainer
#8 lore raw
Quoted 60 lines · Pasha Tatashin
Quoted 50 lines · Pasha Tatashin
Quoted 14 lines · Mike Rapoport
Quoted 12 lines · Pasha Tatashin
Quoted 11 lines

kernel/liveupdate/kexec_handover.c static int kho_mem_serialize(struct kho_out *kho_out) · line 454

Quoted 8 lines
kernel/liveupdate/kexec_handover.c +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+                                               unsigned int order)
+{
+     unsigned long pfn = PHYS_PFN(phys);
+     int nid = early_pfn_to_nid(pfn);
+
+     for (int i = 0; i < (1 << order); i++)
+             init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

Quoted 5 lines
Quoted 3 lines

Mike, but those struct pages should be initialized early anyway. If they are not yet initialized we have a problem, as they are going to be re-initialized later.

Can say I understand your point. Which pages should be initialized earlt?

All pages below node->first_deferred_pfn.

Quoted 1 line

And which pages will be reinitialized?

kho_memory_init() is called after free_area_init() (which calls memmap_init_range to initialize low memory struct pages). So, if we use __init_page_from_nid() as suggested, we would be blindly running __init_single_page() again on those low-memory pages that memmap_init_range() already set up. This would cause double initialization and corruptions due to losing the order information.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 13 lines
kernel/liveupdate/kexec_handover.c +5 -1
+
+     return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
                                    struct khoser_mem_bitmap_ptr *elm)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
              int sz = 1 << (order + PAGE_SHIFT);
              phys_addr_t phys =
                      elm->phys_start + (bit << (order + PAGE_SHIFT));
-             struct page *page = phys_to_page(phys);
+             struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The KHO memory should still be accessible early in boot, right?

The memory is accessible. And we anyway should not use struct page for preserved memory before kho_restore_{folio,pages}.

This makes sense, what happens if someone calls kho_restore_folio() before deferred pages are initialized?

Quoted 6 lines

I looked at your repo. I think what you're proposing makes sense, and indeed it will provide a performance boost if some of the folios are restored in parallel. Just kho_init_deferred_pages() should be using init_deferred_page() to avoid re-initializing the lower memory pages. Also, I am still wondering how it will work with HVO, but I need to take a look at Pratyuh's series for that.

The HVO optimization happens when the file is retrieved, after all the folios are restored. So that is long after deferred page init. For my series both approaches should work.

Signature · 2 lines
Regards,
Pratyush Yadav
maintainer
#9 lore raw
In reply to Pasha Tatashin
Quoted 49 lines · Pasha Tatashin
Quoted 14 lines · Mike Rapoport
Quoted 12 lines · Pasha Tatashin
Quoted 11 lines

kernel/liveupdate/kexec_handover.c static int kho_mem_serialize(struct kho_out *kho_out) · line 454

Quoted 8 lines
kernel/liveupdate/kexec_handover.c +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+                                               unsigned int order)
+{
+     unsigned long pfn = PHYS_PFN(phys);
+     int nid = early_pfn_to_nid(pfn);
+
+     for (int i = 0; i < (1 << order); i++)
+             init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

Quoted 5 lines
Quoted 3 lines

Mike, but those struct pages should be initialized early anyway. If they are not yet initialized we have a problem, as they are going to be re-initialized later.

Can say I understand your point. Which pages should be initialized earlt?

All pages below node->first_deferred_pfn.

Quoted 1 line

And which pages will be reinitialized?

kho_memory_init() is called after free_area_init() (which calls memmap_init_range to initialize low memory struct pages). So, if we use __init_page_from_nid() as suggested, we would be blindly running __init_single_page() again on those low-memory pages that memmap_init_range() already set up. This would cause double initialization and corruptions due to losing the order information.

Quoted 17 lines
Quoted 17 lines
Quoted 16 lines

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 13 lines
kernel/liveupdate/kexec_handover.c +5 -1
+
+     return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
                                    struct khoser_mem_bitmap_ptr *elm)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
              int sz = 1 << (order + PAGE_SHIFT);
              phys_addr_t phys =
                      elm->phys_start + (bit << (order + PAGE_SHIFT));
-             struct page *page = phys_to_page(phys);
+             struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does

Quoted 7 lines
Quoted 4 lines
Quoted 1 line

The KHO memory should still be accessible early in boot, right?

The memory is accessible. And we anyway should not use struct page for preserved memory before kho_restore_{folio,pages}.

This makes sense, what happens if someone calls kho_restore_folio() before deferred pages are initialized?

That's fine, because this memory is still memblock_reserve()ed and deferred init skips reserved ranges.
There is a problem however with the calls to kho_restore_{pages,folio}() after memblock is gone because we can't use early_pfn_to_nid() then.

I think we can start with Evangelos' approach that initializes struct pages at deserialize time and then we'll see how to optimize it.

Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#10 lore raw
Quoted 56 lines · Mike Rapoport
Quoted 54 lines · Pasha Tatashin
Quoted 14 lines · Mike Rapoport
Quoted 12 lines · Pasha Tatashin
Quoted 11 lines

kernel/liveupdate/kexec_handover.c static int kho_mem_serialize(struct kho_out *kho_out) · line 454

Quoted 8 lines
kernel/liveupdate/kexec_handover.c +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+                                               unsigned int order)
+{
+     unsigned long pfn = PHYS_PFN(phys);
+     int nid = early_pfn_to_nid(pfn);
+
+     for (int i = 0; i < (1 << order); i++)
+             init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

Quoted 5 lines
Quoted 3 lines

Mike, but those struct pages should be initialized early anyway. If they are not yet initialized we have a problem, as they are going to be re-initialized later.

Can say I understand your point. Which pages should be initialized earlt?

All pages below node->first_deferred_pfn.

Quoted 1 line

And which pages will be reinitialized?

kho_memory_init() is called after free_area_init() (which calls memmap_init_range to initialize low memory struct pages). So, if we use __init_page_from_nid() as suggested, we would be blindly running __init_single_page() again on those low-memory pages that memmap_init_range() already set up. This would cause double initialization and corruptions due to losing the order information.

Quoted 21 lines
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
kernel/liveupdate/kexec_handover.c +4 -0
+
+     return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
                                    struct khoser_mem_bitmap_ptr *elm)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
Quoted 8 lines

kernel/liveupdate/kexec_handover.c static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 5 lines
kernel/liveupdate/kexec_handover.c +1 -1
              int sz = 1 << (order + PAGE_SHIFT);
              phys_addr_t phys =
                      elm->phys_start + (bit << (order + PAGE_SHIFT));
-             struct page *page = phys_to_page(phys);
+             struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does

Quoted 1 line

The KHO memory should still be accessible early in boot, right?

The memory is accessible. And we anyway should not use struct page for preserved memory before kho_restore_{folio,pages}.

Quoted 7 lines
Quoted 2 lines

This makes sense, what happens if someone calls kho_restore_folio() before deferred pages are initialized?

That's fine, because this memory is still memblock_reserve()ed and deferred init skips reserved ranges.
There is a problem however with the calls to kho_restore_{pages,folio}() after memblock is gone because we can't use early_pfn_to_nid() then.

I agree with the regarding memblock and early_pfn_to_nid(), but I don't think we need to rely on early_pfn_to_nid() during the restore phase.

Quoted 2 lines

I think we can start with Evangelos' approach that initializes struct pages at deserialize time and then we'll see how to optimize it.

Let's do the lazy tail initialization that I proposed to you in a
chat: we initialize only the head struct page during deserialize_bitmap(). Since this happens while memblock is still active, we can safely use early_pfn_to_nid() to set the nid in the head page's flags, and also preserve order as we do today.

Then, we can defer the initialization of all tail pages to kho_restore_folio(). At that stage, we no longer need memblock or early_pfn_to_nid(); we can simply inherit the nid from the head page using page_to_nid(head).

This approach seems to give us the best of both worlds: It avoids the memblock dependency during restoration. It keeps the serial work in deserialize_bitmap() to a minimum (O(1)O(1) per region). It allows the heavy lifting of tail page initialization to be done later in the boot process, potentially in parallel, as you suggested.

Thanks,
Pasha

maintainer
#11 lore raw
Quoted 57 lines · Pasha Tatashin
Quoted 56 lines · Mike Rapoport
Quoted 54 lines · Pasha Tatashin
Quoted 14 lines · Mike Rapoport
Quoted 12 lines · Pasha Tatashin
Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+                                               unsigned int order)
+{
+     unsigned long pfn = PHYS_PFN(phys);
+     int nid = early_pfn_to_nid(pfn);
+
+     for (int i = 0; i < (1 << order); i++)
+             init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

Quoted 5 lines
Quoted 3 lines

Mike, but those struct pages should be initialized early anyway. If they are not yet initialized we have a problem, as they are going to be re-initialized later.

Can say I understand your point. Which pages should be initialized earlt?

All pages below node->first_deferred_pfn.

Quoted 1 line

And which pages will be reinitialized?

kho_memory_init() is called after free_area_init() (which calls memmap_init_range to initialize low memory struct pages). So, if we use __init_page_from_nid() as suggested, we would be blindly running __init_single_page() again on those low-memory pages that memmap_init_range() already set up. This would cause double initialization and corruptions due to losing the order information.

Quoted 21 lines
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +4 -0
+
+     return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
                                    struct khoser_mem_bitmap_ptr *elm)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
Quoted 8 lines

Review static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 5 lines
(quoted diff) +1 -1
              int sz = 1 << (order + PAGE_SHIFT);
              phys_addr_t phys =
                      elm->phys_start + (bit << (order + PAGE_SHIFT));
-             struct page *page = phys_to_page(phys);
+             struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does

Quoted 1 line

The KHO memory should still be accessible early in boot, right?

The memory is accessible. And we anyway should not use struct page for preserved memory before kho_restore_{folio,pages}.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

This makes sense, what happens if someone calls kho_restore_folio() before deferred pages are initialized?

That's fine, because this memory is still memblock_reserve()ed and deferred init skips reserved ranges.
There is a problem however with the calls to kho_restore_{pages,folio}() after memblock is gone because we can't use early_pfn_to_nid() then.

I suppose we can select CONFIG_ARCH_KEEP_MEMBLOCK with CONFIG_KEXEC_HANDOVER. But that comes with its own set of problems like wasting memory, especially when there are a lot of scattered preserved pages

I don't think this is a very good idea, just throwing it out there as an option.

Quoted 14 lines

I agree with the regarding memblock and early_pfn_to_nid(), but I don't think we need to rely on early_pfn_to_nid() during the restore phase.

Quoted 2 lines

I think we can start with Evangelos' approach that initializes struct pages at deserialize time and then we'll see how to optimize it.

Let's do the lazy tail initialization that I proposed to you in a
chat: we initialize only the head struct page during deserialize_bitmap(). Since this happens while memblock is still active, we can safely use early_pfn_to_nid() to set the nid in the head page's flags, and also preserve order as we do today.

Quoted 4 lines

Then, we can defer the initialization of all tail pages to kho_restore_folio(). At that stage, we no longer need memblock or early_pfn_to_nid(); we can simply inherit the nid from the head page using page_to_nid(head).

Does that assumption always hold? Does every contiguous chunk of memory always have to be in the same node? For folios it would hold, but what about kho_preserve_pages()?

Quoted 6 lines

This approach seems to give us the best of both worlds: It avoids the memblock dependency during restoration. It keeps the serial work in deserialize_bitmap() to a minimum (O(1)O(1) per region). It allows the heavy lifting of tail page initialization to be done later in the boot process, potentially in parallel, as you suggested.

Here's another idea I have been thinking about, but never dug deep enough to figure out if it actually works.

__init_page_from_nid() loops through all the zones for the node to find the zone id for the page. We can flip it the other way round and loop through all zones (on all nodes) to find out if the PFN spans that zone. Once we find the zone, we can directly call __init_single_page() on it. If a contiguous chunk of preserved memory lands in one zone, we can batch the init to save some time.

Something like the below (completely untested):

	static void kho_init_page(struct page *page)
	{
		unsigned long pfn = page_to_pfn(page);
		struct zone *zone;
	
		for_each_zone(zone) {
			if (zone_spans_pfn(zone, pfn))
				break;
		}
	
		__init_single_page(page, pfn, zone_idx(zone), zone_to_nid(zone));
	}

It doesn't do the batching I mentioned, but I think it at least gets the point across. And I think even this simple version would be a good first step.

This lets us initialize the page from kho_restore_folio() without having to rely of memblock being alive, and saves us from doing work during early boot. We should only have a handful of zones and nodes in practice, so I think it should perform fairly well too.

We would of course need to see how it performs in practice. If it works, I think it would be cleaner and simpler than splitting the initialization into two separate parts.

Evangelos, would you mind giving it a try to see if the idea works and how well it performs?

Signature · 2 lines
Regards,
Pratyush Yadav
maintainer
#12 lore raw
Quoted 90 lines · Pratyush Yadav
Quoted 57 lines · Pasha Tatashin
Quoted 56 lines · Mike Rapoport
Quoted 54 lines · Pasha Tatashin
Quoted 14 lines · Mike Rapoport
Quoted 12 lines · Pasha Tatashin
Quoted 11 lines

Review

Quoted 8 lines
(quoted diff) +8 -0
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+                                               unsigned int order)
+{
+     unsigned long pfn = PHYS_PFN(phys);
+     int nid = early_pfn_to_nid(pfn);
+
+     for (int i = 0; i < (1 << order); i++)
+             init_deferred_page(pfn + i, nid);

This will skip pages below node->first_deferred_pfn, we need to use __init_page_from_nid() here.

Quoted 5 lines
Quoted 3 lines

Mike, but those struct pages should be initialized early anyway. If they are not yet initialized we have a problem, as they are going to be re-initialized later.

Can say I understand your point. Which pages should be initialized earlt?

All pages below node->first_deferred_pfn.

Quoted 1 line

And which pages will be reinitialized?

kho_memory_init() is called after free_area_init() (which calls memmap_init_range to initialize low memory struct pages). So, if we use __init_page_from_nid() as suggested, we would be blindly running __init_single_page() again on those low-memory pages that memmap_init_range() already set up. This would cause double initialization and corruptions due to losing the order information.

Quoted 21 lines
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
(quoted diff) +4 -0
+
+     return pfn_to_page(pfn);
+}
+
 static void __init deserialize_bitmap(unsigned int order,
                                    struct khoser_mem_bitmap_ptr *elm)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
Quoted 8 lines

Review static void __init deserialize_bitmap(unsigned int order, · line 469

Quoted 5 lines
(quoted diff) +1 -1
              int sz = 1 << (order + PAGE_SHIFT);
              phys_addr_t phys =
                      elm->phys_start + (bit << (order + PAGE_SHIFT));
-             struct page *page = phys_to_page(phys);
+             struct page *page = kho_get_preserved_page(phys, order);

I think it's better to initialize deferred struct pages later in kho_restore_page. deserialize_bitmap() runs before SMP and it already does

Quoted 1 line

The KHO memory should still be accessible early in boot, right?

The memory is accessible. And we anyway should not use struct page for preserved memory before kho_restore_{folio,pages}.

Quoted 7 lines
Quoted 7 lines
Quoted 2 lines

This makes sense, what happens if someone calls kho_restore_folio() before deferred pages are initialized?

That's fine, because this memory is still memblock_reserve()ed and deferred init skips reserved ranges.
There is a problem however with the calls to kho_restore_{pages,folio}() after memblock is gone because we can't use early_pfn_to_nid() then.

I suppose we can select CONFIG_ARCH_KEEP_MEMBLOCK with CONFIG_KEXEC_HANDOVER. But that comes with its own set of problems like wasting memory, especially when there are a lot of scattered preserved pages

I don't think this is a very good idea, just throwing it out there as an option.

Quoted 14 lines

I agree with the regarding memblock and early_pfn_to_nid(), but I don't think we need to rely on early_pfn_to_nid() during the restore phase.

Quoted 2 lines

I think we can start with Evangelos' approach that initializes struct pages at deserialize time and then we'll see how to optimize it.

Let's do the lazy tail initialization that I proposed to you in a
chat: we initialize only the head struct page during deserialize_bitmap(). Since this happens while memblock is still active, we can safely use early_pfn_to_nid() to set the nid in the head page's flags, and also preserve order as we do today.

Quoted 8 lines
Quoted 4 lines

Then, we can defer the initialization of all tail pages to kho_restore_folio(). At that stage, we no longer need memblock or early_pfn_to_nid(); we can simply inherit the nid from the head page using page_to_nid(head).

Does that assumption always hold? Does every contiguous chunk of memory always have to be in the same node? For folios it would hold, but what about kho_preserve_pages()?

NUMA node boundaries are SECTION_SIZE aligned. Since SECTION_SIZE is larger than MAX_PAGE_ORDER it is mathematically impossible for a single chunk to span multiple nodes.

Quoted 36 lines
Quoted 5 lines

This approach seems to give us the best of both worlds: It avoids the memblock dependency during restoration. It keeps the serial work in deserialize_bitmap() to a minimum (O(1)O(1) per region). It allows the heavy lifting of tail page initialization to be done later in the boot process, potentially in parallel, as you suggested.

Here's another idea I have been thinking about, but never dug deep enough to figure out if it actually works.

__init_page_from_nid() loops through all the zones for the node to find the zone id for the page. We can flip it the other way round and loop through all zones (on all nodes) to find out if the PFN spans that zone. Once we find the zone, we can directly call __init_single_page() on it. If a contiguous chunk of preserved memory lands in one zone, we can batch the init to save some time.

Something like the below (completely untested):

        static void kho_init_page(struct page *page)
        {
                unsigned long pfn = page_to_pfn(page);
                struct zone *zone;

                for_each_zone(zone) {
                        if (zone_spans_pfn(zone, pfn))
                                break;
                }

                __init_single_page(page, pfn, zone_idx(zone), zone_to_nid(zone));
        }

It doesn't do the batching I mentioned, but I think it at least gets the point across. And I think even this simple version would be a good first step.

Quoted 8 lines

This lets us initialize the page from kho_restore_folio() without having to rely of memblock being alive, and saves us from doing work during early boot. We should only have a handful of zones and nodes in practice, so I think it should perform fairly well too.

We would of course need to see how it performs in practice. If it works, I think it would be cleaner and simpler than splitting the initialization into two separate parts.

I think your idea is clever and would work. However, consider the cache efficiency: in deserialize_bitmap(), we must write to the head struct page anyway to preserve the order. Since we are already bringing that 64-byte cacheline in and dirtying it, and since memblock is available and fast at this stage, it makes sense to fully initialize the head page right then.
If we do that, we get the nid for "free" (cache-wise) and we avoid the overhead of iterating zones during the restore phase. We can then simply inherit the nid from the head page when initializing the tail pages later.

Pasha

maintainer
#13 lore raw
Quoted 3 lines · Pasha Tatashin
Quoted 2 lines · Pratyush Yadav

On Fri, Dec 19 2025, Pasha Tatashin wrote:

[...]

Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

Let's do the lazy tail initialization that I proposed to you in a
chat: we initialize only the head struct page during deserialize_bitmap(). Since this happens while memblock is still active, we can safely use early_pfn_to_nid() to set the nid in the head page's flags, and also preserve order as we do today.

Then, we can defer the initialization of all tail pages to kho_restore_folio(). At that stage, we no longer need memblock or early_pfn_to_nid(); we can simply inherit the nid from the head page using page_to_nid(head).

Quoted 7 lines
Quoted 3 lines

Does that assumption always hold? Does every contiguous chunk of memory always have to be in the same node? For folios it would hold, but what about kho_preserve_pages()?

NUMA node boundaries are SECTION_SIZE aligned. Since SECTION_SIZE is larger than MAX_PAGE_ORDER it is mathematically impossible for a single chunk to span multiple nodes.

For folios, yes. The whole folio should only be in a single node. But we also have kho_preserve_pages() (formerly kho_preserve_phys()) which can be used to preserve an arbitrary size of memory and _that_ doesn't have to be in the same section. And if the memory is properly aligned, then it will end up being just one higher-order preservation in KHO.

Quoted 46 lines
Quoted 37 lines
Quoted 6 lines

This approach seems to give us the best of both worlds: It avoids the memblock dependency during restoration. It keeps the serial work in deserialize_bitmap() to a minimum (O(1)O(1) per region). It allows the heavy lifting of tail page initialization to be done later in the boot process, potentially in parallel, as you suggested.

Here's another idea I have been thinking about, but never dug deep enough to figure out if it actually works.

__init_page_from_nid() loops through all the zones for the node to find the zone id for the page. We can flip it the other way round and loop through all zones (on all nodes) to find out if the PFN spans that zone. Once we find the zone, we can directly call __init_single_page() on it. If a contiguous chunk of preserved memory lands in one zone, we can batch the init to save some time.

Something like the below (completely untested):

        static void kho_init_page(struct page *page)
        {
                unsigned long pfn = page_to_pfn(page);
                struct zone *zone;

                for_each_zone(zone) {
                        if (zone_spans_pfn(zone, pfn))
                                break;
                }

                __init_single_page(page, pfn, zone_idx(zone), zone_to_nid(zone));
        }

It doesn't do the batching I mentioned, but I think it at least gets the point across. And I think even this simple version would be a good first step.

Quoted 8 lines

This lets us initialize the page from kho_restore_folio() without having to rely of memblock being alive, and saves us from doing work during early boot. We should only have a handful of zones and nodes in practice, so I think it should perform fairly well too.

We would of course need to see how it performs in practice. If it works, I think it would be cleaner and simpler than splitting the initialization into two separate parts.

Quoted 6 lines

I think your idea is clever and would work. However, consider the cache efficiency: in deserialize_bitmap(), we must write to the head struct page anyway to preserve the order. Since we are already bringing that 64-byte cacheline in and dirtying it, and since memblock is available and fast at this stage, it makes sense to fully initialize the head page right then.

You will also bring in the cache line and dirty it during kho_restore_folio() since you need to write the page refcounts. So I don't think the cache efficiency makes any difference between either approach.

Quoted 4 lines

If we do that, we get the nid for "free" (cache-wise) and we avoid the overhead of iterating zones during the restore phase. We can then simply inherit the nid from the head page when initializing the tail pages later.

To get the nid, you would need to call early_pfn_to_nid(). This takes a spinlock and searches through all memblock memory regions. I don't think it is too expensive, but it isn't free either. And all this would be done serially. With the zone search, you at least have some room for concurrency.

I think either approach only makes a difference when we have a large number of low-order preservations. If we have a handful of high-order preservations, I suppose the overhead of nid search would be negligible.

Long term, I think we should hook this into page_alloc_init_late() so that all the KHO pages also get initalized along with all the other pages. This will result in better integration of KHO with rest of MM init, and also have more consistent page restore performance.

Jason's radix tree patches will make that a bit easier to do I think. The zone search will scale better I reckon.

Signature · 2 lines
Regards,
Pratyush Yadav
maintainer
#14 lore raw
Quoted 9 lines
Quoted 3 lines

NUMA node boundaries are SECTION_SIZE aligned. Since SECTION_SIZE is larger than MAX_PAGE_ORDER it is mathematically impossible for a single chunk to span multiple nodes.

For folios, yes. The whole folio should only be in a single node. But we also have kho_preserve_pages() (formerly kho_preserve_phys()) which can be used to preserve an arbitrary size of memory and _that_ doesn't have to be in the same section. And if the memory is properly aligned, then it will end up being just one higher-order preservation in KHO.

Both restore pages and folios we use: kho_restore_page() which has the following:

/*
* deserialize_bitmap() only sets the magic on the head page. This magic
* check also implicitly makes sure phys is order-aligned since for
* non-order-aligned phys addresses, magic will never be set.
*/
if (WARN_ON_ONCE(info.magic != KHO_PAGE_MAGIC || info.order > MAX_PAGE_ORDER)) return NULL;

My understanding the head page can never be more than MAX_PAGE_ORDER hence why I am saying it will be less than SECTION_SIZE. With HugeTLB the order can be more than MAX_PAGE_ORDER, but in that case it still has to be within a single NID, since a huge page cannot be split across multiple nodes.

Quoted 68 lines
Quoted 45 lines
Quoted 36 lines
Quoted 5 lines

This approach seems to give us the best of both worlds: It avoids the memblock dependency during restoration. It keeps the serial work in deserialize_bitmap() to a minimum (O(1)O(1) per region). It allows the heavy lifting of tail page initialization to be done later in the boot process, potentially in parallel, as you suggested.

Here's another idea I have been thinking about, but never dug deep enough to figure out if it actually works.

__init_page_from_nid() loops through all the zones for the node to find the zone id for the page. We can flip it the other way round and loop through all zones (on all nodes) to find out if the PFN spans that zone. Once we find the zone, we can directly call __init_single_page() on it. If a contiguous chunk of preserved memory lands in one zone, we can batch the init to save some time.

Something like the below (completely untested):

        static void kho_init_page(struct page *page)
        {
                unsigned long pfn = page_to_pfn(page);
                struct zone *zone;

                for_each_zone(zone) {
                        if (zone_spans_pfn(zone, pfn))
                                break;
                }

                __init_single_page(page, pfn, zone_idx(zone), zone_to_nid(zone));
        }

It doesn't do the batching I mentioned, but I think it at least gets the point across. And I think even this simple version would be a good first step.

Quoted 8 lines

This lets us initialize the page from kho_restore_folio() without having to rely of memblock being alive, and saves us from doing work during early boot. We should only have a handful of zones and nodes in practice, so I think it should perform fairly well too.

We would of course need to see how it performs in practice. If it works, I think it would be cleaner and simpler than splitting the initialization into two separate parts.

Quoted 6 lines

I think your idea is clever and would work. However, consider the cache efficiency: in deserialize_bitmap(), we must write to the head struct page anyway to preserve the order. Since we are already bringing that 64-byte cacheline in and dirtying it, and since memblock is available and fast at this stage, it makes sense to fully initialize the head page right then.

You will also bring in the cache line and dirty it during kho_restore_folio() since you need to write the page refcounts. So I don't think the cache efficiency makes any difference between either approach.

Quoted 4 lines

If we do that, we get the nid for "free" (cache-wise) and we avoid the overhead of iterating zones during the restore phase. We can then simply inherit the nid from the head page when initializing the tail pages later.

To get the nid, you would need to call early_pfn_to_nid(). This takes a spinlock and searches through all memblock memory regions. I don't think it is too expensive, but it isn't free either. And all this would be done serially. With the zone search, you at least have some room for concurrency.

Quoted 3 lines

I think either approach only makes a difference when we have a large number of low-order preservations. If we have a handful of high-order preservations, I suppose the overhead of nid search would be negligible.

We should be targeting a situation where the vast majority of the preserved memory is HugeTLB, but I am still worried about lower order preservation efficiency for IOMMU page tables, etc.

Quoted 4 lines

Long term, I think we should hook this into page_alloc_init_late() so that all the KHO pages also get initalized along with all the other pages. This will result in better integration of KHO with rest of MM init, and also have more consistent page restore performance.

But we keep KHO as reserved memory, and hooking it up into page_alloc_init_late() would make it very different, since that memory is part of the buddy allocator memory...

Quoted 2 lines

Jason's radix tree patches will make that a bit easier to do I think. The zone search will scale better I reckon.

It could, perhaps early in boot we should reserve the radix tree, and use it as a source of truth look-ups later in boot?

maintainer
#15 lore raw
Quoted 13 lines · Pasha Tatashin
Quoted 9 lines
Quoted 3 lines

NUMA node boundaries are SECTION_SIZE aligned. Since SECTION_SIZE is larger than MAX_PAGE_ORDER it is mathematically impossible for a single chunk to span multiple nodes.

For folios, yes. The whole folio should only be in a single node. But we also have kho_preserve_pages() (formerly kho_preserve_phys()) which can be used to preserve an arbitrary size of memory and _that_ doesn't have to be in the same section. And if the memory is properly aligned, then it will end up being just one higher-order preservation in KHO.

Both restore pages and folios we use: kho_restore_page() which has the following:

Quoted 7 lines

/*
* deserialize_bitmap() only sets the magic on the head page. This magic
* check also implicitly makes sure phys is order-aligned since for
* non-order-aligned phys addresses, magic will never be set.
*/
if (WARN_ON_ONCE(info.magic != KHO_PAGE_MAGIC || info.order > MAX_PAGE_ORDER)) return NULL;

See my patch that drops this restriction:
https://lore.kernel.org/linux-mm/[email protected]/

I think it was wrong to add it in the first place.

Quoted 6 lines

My understanding the head page can never be more than MAX_PAGE_ORDER hence why I am saying it will be less than SECTION_SIZE. With HugeTLB the order can be more than MAX_PAGE_ORDER, but in that case it still has to be within a single NID, since a huge page cannot be split across multiple nodes.

For a "proper" page/folio, that either comes from the page allocator or from HugeTLB, you are right. But see again how kho_preserve_pages() works:

	while (pfn < end_pfn) {
		const unsigned int order =
			min(count_trailing_zeros(pfn), ilog2(end_pfn - pfn));
	
		err = __kho_preserve_order(track, pfn, order);
		[...]

It combines contiguous order-aligned pages into one KHO preservation.

So say I have two nodes, each 64G. If I call kho_preserve_pages() for 62G to 66G, I will get _one_ 4G preservation at 62G. kho_restore_page() will split it into 0-order pages on restore.

Quoted 69 lines
Quoted 69 lines
Quoted 46 lines
Quoted 37 lines
Quoted 6 lines

This approach seems to give us the best of both worlds: It avoids the memblock dependency during restoration. It keeps the serial work in deserialize_bitmap() to a minimum (O(1)O(1) per region). It allows the heavy lifting of tail page initialization to be done later in the boot process, potentially in parallel, as you suggested.

Here's another idea I have been thinking about, but never dug deep enough to figure out if it actually works.

__init_page_from_nid() loops through all the zones for the node to find the zone id for the page. We can flip it the other way round and loop through all zones (on all nodes) to find out if the PFN spans that zone. Once we find the zone, we can directly call __init_single_page() on it. If a contiguous chunk of preserved memory lands in one zone, we can batch the init to save some time.

Something like the below (completely untested):

        static void kho_init_page(struct page *page)
        {
                unsigned long pfn = page_to_pfn(page);
                struct zone *zone;

                for_each_zone(zone) {
                        if (zone_spans_pfn(zone, pfn))
                                break;
                }

                __init_single_page(page, pfn, zone_idx(zone), zone_to_nid(zone));
        }

It doesn't do the batching I mentioned, but I think it at least gets the point across. And I think even this simple version would be a good first step.

Quoted 8 lines

This lets us initialize the page from kho_restore_folio() without having to rely of memblock being alive, and saves us from doing work during early boot. We should only have a handful of zones and nodes in practice, so I think it should perform fairly well too.

We would of course need to see how it performs in practice. If it works, I think it would be cleaner and simpler than splitting the initialization into two separate parts.

Quoted 6 lines

I think your idea is clever and would work. However, consider the cache efficiency: in deserialize_bitmap(), we must write to the head struct page anyway to preserve the order. Since we are already bringing that 64-byte cacheline in and dirtying it, and since memblock is available and fast at this stage, it makes sense to fully initialize the head page right then.

You will also bring in the cache line and dirty it during kho_restore_folio() since you need to write the page refcounts. So I don't think the cache efficiency makes any difference between either approach.

Quoted 4 lines

If we do that, we get the nid for "free" (cache-wise) and we avoid the overhead of iterating zones during the restore phase. We can then simply inherit the nid from the head page when initializing the tail pages later.

To get the nid, you would need to call early_pfn_to_nid(). This takes a spinlock and searches through all memblock memory regions. I don't think it is too expensive, but it isn't free either. And all this would be done serially. With the zone search, you at least have some room for concurrency.

Quoted 7 lines
Quoted 3 lines

I think either approach only makes a difference when we have a large number of low-order preservations. If we have a handful of high-order preservations, I suppose the overhead of nid search would be negligible.

We should be targeting a situation where the vast majority of the preserved memory is HugeTLB, but I am still worried about lower order preservation efficiency for IOMMU page tables, etc.

Yep. Plus we might get VMMs stashing some of their state in a memfd too.

Quoted 9 lines
Quoted 5 lines

Long term, I think we should hook this into page_alloc_init_late() so that all the KHO pages also get initalized along with all the other pages. This will result in better integration of KHO with rest of MM init, and also have more consistent page restore performance.

But we keep KHO as reserved memory, and hooking it up into page_alloc_init_late() would make it very different, since that memory is part of the buddy allocator memory...

The idea I have is to have a separate call in page_alloc_init_late() that initalizes KHO pages. It would traverse the radix tree (probably in parallel by distributing the address space across multiple threads?) and initialize all the pages. Then kho_restore_page() would only have to double-check the magic and it can directly return the page.

Radix tree makes parallelism easier than the linked lists we have now.

Quoted 6 lines
Quoted 3 lines

Jason's radix tree patches will make that a bit easier to do I think. The zone search will scale better I reckon.

It could, perhaps early in boot we should reserve the radix tree, and use it as a source of truth look-ups later in boot?

Yep. I think the radix tree should mark its own pages as preserved too so they stick around later in boot.

Signature · 2 lines
Regards,
Pratyush Yadav
maintainer
#16 lore raw
Quoted 7 lines
Quoted 2 lines

if (WARN_ON_ONCE(info.magic != KHO_PAGE_MAGIC || info.order > MAX_PAGE_ORDER)) return NULL;

See my patch that drops this restriction:
https://lore.kernel.org/linux-mm/[email protected]/

I think it was wrong to add it in the first place.

Agree, the restriction can be removed. Indeed, it is wrong as it is not enforced during preservation.

However, I think we are going to be in a world of pain if we allow preserving memory from different topologies within the same order. In kho_preserve_pages(), we have to check if the first and last page are from the same nid; if not, reduce the order by 1 and repeat until they are. It is just wrong to intermix different memory into the same order, so in addition to removing that restriction, I think we should implement this enforcement.

Also, perhaps we should pass the NID in the Jason's radix tree together with the order. We could have a single tree that encodes both order and NID information in the top level, or we can have one tree per NID. It does not really matter to me, but that should help us with faster struct page initialization.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines

To get the nid, you would need to call early_pfn_to_nid(). This takes a spinlock and searches through all memblock memory regions. I don't think it is too expensive, but it isn't free either. And all this would be done serially. With the zone search, you at least have some room for concurrency.

I think either approach only makes a difference when we have a large number of low-order preservations. If we have a handful of high-order preservations, I suppose the overhead of nid search would be negligible.

Quoted 5 lines
Quoted 3 lines

We should be targeting a situation where the vast majority of the preserved memory is HugeTLB, but I am still worried about lower order preservation efficiency for IOMMU page tables, etc.

Yep. Plus we might get VMMs stashing some of their state in a memfd too.

Yes, that is true, but hopefully those are tiny compared to everything else.

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

Long term, I think we should hook this into page_alloc_init_late() so that all the KHO pages also get initalized along with all the other pages. This will result in better integration of KHO with rest of MM init, and also have more consistent page restore performance.

But we keep KHO as reserved memory, and hooking it up into page_alloc_init_late() would make it very different, since that memory is part of the buddy allocator memory...

Quoted 5 lines

The idea I have is to have a separate call in page_alloc_init_late() that initalizes KHO pages. It would traverse the radix tree (probably in parallel by distributing the address space across multiple threads?) and initialize all the pages. Then kho_restore_page() would only have to double-check the magic and it can directly return the page.

I kind of do not like relying on magic to decide whether to initialize the struct page. I would prefer to avoid this magic marker altogether:
i.e. struct page is either initialized or not, not halfway initialized, etc.

Magic is not reliable. During machine reset in many firmware implementations, and in every kexec reboot, memory is not zeroed. The kernel usually allocates vmemmap using exactly the same pages, so there is just too high a chance of getting magic values accidentally inherited from the previous boot.

Quoted 1 line

Radix tree makes parallelism easier than the linked lists we have now.

Agree, radix tree can absolutely help with parallelism.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Jason's radix tree patches will make that a bit easier to do I think. The zone search will scale better I reckon.

It could, perhaps early in boot we should reserve the radix tree, and use it as a source of truth look-ups later in boot?

Yep. I think the radix tree should mark its own pages as preserved too so they stick around later in boot.

Unfortunately, this can only be done in the new kernel, not in the old kernel; otherwise we can end up with a recursive dependency that may never be satisfied.

Pasha

maintainer
#17 lore raw
Quoted 11 lines · Pasha Tatashin
Quoted 7 lines
Quoted 2 lines

if (WARN_ON_ONCE(info.magic != KHO_PAGE_MAGIC || info.order > MAX_PAGE_ORDER)) return NULL;

See my patch that drops this restriction:
https://lore.kernel.org/linux-mm/[email protected]/

I think it was wrong to add it in the first place.

Agree, the restriction can be removed. Indeed, it is wrong as it is not enforced during preservation.

Quoted 7 lines

However, I think we are going to be in a world of pain if we allow preserving memory from different topologies within the same order. In kho_preserve_pages(), we have to check if the first and last page are from the same nid; if not, reduce the order by 1 and repeat until they are. It is just wrong to intermix different memory into the same order, so in addition to removing that restriction, I think we should implement this enforcement.

Sure, makes sense.

Quoted 6 lines

Also, perhaps we should pass the NID in the Jason's radix tree together with the order. We could have a single tree that encodes both order and NID information in the top level, or we can have one tree per NID. It does not really matter to me, but that should help us with faster struct page initialization.

Can we use NIDs in ABI? Do they stay stable across reboots? I never looked at how NIDs actually get assigned.

Not sure if we should target it for the initial merge of the radix tree, but I think this is something we can try to figure out later down the line.

Quoted 34 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

To get the nid, you would need to call early_pfn_to_nid(). This takes a spinlock and searches through all memblock memory regions. I don't think it is too expensive, but it isn't free either. And all this would be done serially. With the zone search, you at least have some room for concurrency.

I think either approach only makes a difference when we have a large number of low-order preservations. If we have a handful of high-order preservations, I suppose the overhead of nid search would be negligible.

Quoted 5 lines
Quoted 3 lines

We should be targeting a situation where the vast majority of the preserved memory is HugeTLB, but I am still worried about lower order preservation efficiency for IOMMU page tables, etc.

Yep. Plus we might get VMMs stashing some of their state in a memfd too.

Yes, that is true, but hopefully those are tiny compared to everything else.

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

Long term, I think we should hook this into page_alloc_init_late() so that all the KHO pages also get initalized along with all the other pages. This will result in better integration of KHO with rest of MM init, and also have more consistent page restore performance.

But we keep KHO as reserved memory, and hooking it up into page_alloc_init_late() would make it very different, since that memory is part of the buddy allocator memory...

Quoted 5 lines

The idea I have is to have a separate call in page_alloc_init_late() that initalizes KHO pages. It would traverse the radix tree (probably in parallel by distributing the address space across multiple threads?) and initialize all the pages. Then kho_restore_page() would only have to double-check the magic and it can directly return the page.

Quoted 4 lines

I kind of do not like relying on magic to decide whether to initialize the struct page. I would prefer to avoid this magic marker altogether:
i.e. struct page is either initialized or not, not halfway initialized, etc.

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

Quoted 6 lines

Magic is not reliable. During machine reset in many firmware implementations, and in every kexec reboot, memory is not zeroed. The kernel usually allocates vmemmap using exactly the same pages, so there is just too high a chance of getting magic values accidentally inherited from the previous boot.

I don't think that can happen. All the pages are zeroed when initialized, which will clear the magic. We should only be setting the magic on an initialized struct page.

Quoted 11 lines
Quoted 2 lines

Radix tree makes parallelism easier than the linked lists we have now.

Agree, radix tree can absolutely help with parallelism.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Jason's radix tree patches will make that a bit easier to do I think. The zone search will scale better I reckon.

It could, perhaps early in boot we should reserve the radix tree, and use it as a source of truth look-ups later in boot?

Quoted 6 lines
Quoted 2 lines

Yep. I think the radix tree should mark its own pages as preserved too so they stick around later in boot.

Unfortunately, this can only be done in the new kernel, not in the old kernel; otherwise we can end up with a recursive dependency that may never be satisfied.

Right. It shouldn't be too hard to do in the new kernel though. We will walk the whole tree anyway.

Signature · 2 lines
Regards,
Pratyush Yadav
maintainer
#18 lore raw
Quoted 77 lines · Pratyush Yadav
Quoted 11 lines · Pasha Tatashin
Quoted 7 lines
Quoted 2 lines

if (WARN_ON_ONCE(info.magic != KHO_PAGE_MAGIC || info.order > MAX_PAGE_ORDER)) return NULL;

See my patch that drops this restriction:
https://lore.kernel.org/linux-mm/[email protected]/

I think it was wrong to add it in the first place.

Agree, the restriction can be removed. Indeed, it is wrong as it is not enforced during preservation.

Quoted 7 lines

However, I think we are going to be in a world of pain if we allow preserving memory from different topologies within the same order. In kho_preserve_pages(), we have to check if the first and last page are from the same nid; if not, reduce the order by 1 and repeat until they are. It is just wrong to intermix different memory into the same order, so in addition to removing that restriction, I think we should implement this enforcement.

Sure, makes sense.

Quoted 6 lines

Also, perhaps we should pass the NID in the Jason's radix tree together with the order. We could have a single tree that encodes both order and NID information in the top level, or we can have one tree per NID. It does not really matter to me, but that should help us with faster struct page initialization.

Can we use NIDs in ABI? Do they stay stable across reboots? I never looked at how NIDs actually get assigned.

Not sure if we should target it for the initial merge of the radix tree, but I think this is something we can try to figure out later down the line.

Quoted 34 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

To get the nid, you would need to call early_pfn_to_nid(). This takes a spinlock and searches through all memblock memory regions. I don't think it is too expensive, but it isn't free either. And all this would be done serially. With the zone search, you at least have some room for concurrency.

I think either approach only makes a difference when we have a large number of low-order preservations. If we have a handful of high-order preservations, I suppose the overhead of nid search would be negligible.

Quoted 5 lines
Quoted 3 lines

We should be targeting a situation where the vast majority of the preserved memory is HugeTLB, but I am still worried about lower order preservation efficiency for IOMMU page tables, etc.

Yep. Plus we might get VMMs stashing some of their state in a memfd too.

Yes, that is true, but hopefully those are tiny compared to everything else.

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

Long term, I think we should hook this into page_alloc_init_late() so that all the KHO pages also get initalized along with all the other pages. This will result in better integration of KHO with rest of MM init, and also have more consistent page restore performance.

But we keep KHO as reserved memory, and hooking it up into page_alloc_init_late() would make it very different, since that memory is part of the buddy allocator memory...

Quoted 5 lines

The idea I have is to have a separate call in page_alloc_init_late() that initalizes KHO pages. It would traverse the radix tree (probably in parallel by distributing the address space across multiple threads?) and initialize all the pages. Then kho_restore_page() would only have to double-check the magic and it can directly return the page.

Quoted 4 lines

I kind of do not like relying on magic to decide whether to initialize the struct page. I would prefer to avoid this magic marker altogether:
i.e. struct page is either initialized or not, not halfway initialized, etc.

Quoted 7 lines

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

It is part of a critical hotpath during blackout, should really be behind CONFIG_KEXEC_HANDOVER_DEBUG

Quoted 9 lines
Quoted 5 lines

Magic is not reliable. During machine reset in many firmware implementations, and in every kexec reboot, memory is not zeroed. The kernel usually allocates vmemmap using exactly the same pages, so there is just too high a chance of getting magic values accidentally inherited from the previous boot.

I don't think that can happen. All the pages are zeroed when initialized, which will clear the magic. We should only be setting the magic on an initialized struct page.

This can happen due to bugs when we use a partially initialized "struct page", something that Mike have been looking to do. So, pass some information in a struct page before it is fully initialized.

Quoted 23 lines
Quoted 10 lines
Quoted 1 line

Radix tree makes parallelism easier than the linked lists we have now.

Agree, radix tree can absolutely help with parallelism.

Quoted 6 lines
Quoted 5 lines
Quoted 2 lines

Jason's radix tree patches will make that a bit easier to do I think. The zone search will scale better I reckon.

It could, perhaps early in boot we should reserve the radix tree, and use it as a source of truth look-ups later in boot?

Quoted 6 lines
Quoted 2 lines

Yep. I think the radix tree should mark its own pages as preserved too so they stick around later in boot.

Unfortunately, this can only be done in the new kernel, not in the old kernel; otherwise we can end up with a recursive dependency that may never be satisfied.

Right. It shouldn't be too hard to do in the new kernel though. We will walk the whole tree anyway.

--
Regards,
Pratyush Yadav

maintainer
#19 lore raw
Quoted 10 lines · Pasha Tatashin
Quoted 8 lines · Pratyush Yadav

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

Quoted 2 lines

It is part of a critical hotpath during blackout, should really be behind CONFIG_KEXEC_HANDOVER_DEBUG

Do you have the numbers? ;-)

Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#20 lore raw
Quoted 12 lines · Mike Rapoport
Quoted 10 lines · Pasha Tatashin
Quoted 8 lines · Pratyush Yadav

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

Quoted 4 lines
Quoted 2 lines

It is part of a critical hotpath during blackout, should really be behind CONFIG_KEXEC_HANDOVER_DEBUG

Do you have the numbers? ;-)

The fastest reboot we can achieve is ~0.4s on ARM (shutdown+purgatory+boot), let's not add anything to regress, as every microsecond counts during blackout.

Pasha

maintainer
#21 lore raw
Quoted 13 lines · Pasha Tatashin
Quoted 12 lines · Mike Rapoport
Quoted 10 lines · Pasha Tatashin
Quoted 8 lines · Pratyush Yadav

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

It is part of a critical hotpath during blackout, should really be behind CONFIG_KEXEC_HANDOVER_DEBUG

Do you have the numbers? ;-)

The fastest reboot we can achieve is ~0.4s on ARM

I meant the difference between assigning info.magic and skipping it.

Quoted 2 lines

(shutdown+purgatory+boot), let's not add anything to regress, as every microsecond counts during blackout.

Any added functionality adds cycles, this is inevitable. And neither KHO nor LUO are near the completion, so we'll have to add functionality to both of them. And the added functionality should be correct first and foremost. And magic sanity check seems pretty useful and presumably cheap enough to always keep it unless you see a real slowdown because of it.

Quoted 1 line

Pasha

Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#22 lore raw
Quoted 15 lines · Mike Rapoport
Quoted 13 lines · Pasha Tatashin
Quoted 12 lines · Mike Rapoport
Quoted 10 lines · Pasha Tatashin
Quoted 8 lines · Pratyush Yadav

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

It is part of a critical hotpath during blackout, should really be behind CONFIG_KEXEC_HANDOVER_DEBUG

Do you have the numbers? ;-)

The fastest reboot we can achieve is ~0.4s on ARM

I meant the difference between assigning info.magic and skipping it.

It is proportional to the amount of preserved memory. Extra assignment for each page. In our fleet we have observed IOMMU page tables to be 20G in size. So, let's just assume it is 20G. That is: 20 * 1024^3 / 4096 = 5.24 million pages. If we access "struct page" only for the magic purpose, we fetch full 64-byte cacheline, which is 5.24 million
* 64 bytes = 335 M, that is ~13ms with ~25G/s DRAM; and also each TLB miss will add some latency, 5.2M * 10ns = ~50ms. In total we can get 15ms ~ 50ms regression compared to 400ms, that is 4-12%. It will be less if we also access "struct page" for another reason at the same time, but still it adds up.

Quoted 9 lines
Quoted 3 lines

(shutdown+purgatory+boot), let's not add anything to regress, as every microsecond counts during blackout.

Any added functionality adds cycles, this is inevitable. And neither KHO nor LUO are near the completion, so we'll have to add functionality to both of them. And the added functionality should be correct first and foremost. And magic sanity check seems pretty useful and presumably cheap enough to always keep it unless you see a real slowdown because of it.

Magic check is proportional to the amount of preserved memory. It is not a required functionality, only a sanity checking. I really do not see a reason to enable it in production. All other sanity struct page, and pg_flags related sanity checking are usually enabled with CONFIG_DEBUG_VM, so enabling it only with CONFIG_KEXEC_HANDOVER_DEBUG is better.

Pasha

Quoted 6 lines
Quoted 2 lines

Pasha

--
Sincerely yours,
Mike.

maintainer
#23 lore raw
Quoted 21 lines · Pasha Tatashin
Quoted 20 lines · Mike Rapoport
Quoted 18 lines · Pasha Tatashin
Quoted 16 lines · Mike Rapoport
Quoted 10 lines · Pasha Tatashin
Quoted 8 lines · Pratyush Yadav

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

Quoted 2 lines

It is part of a critical hotpath during blackout, should really be behind CONFIG_KEXEC_HANDOVER_DEBUG

Do you have the numbers? ;-)

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

The fastest reboot we can achieve is ~0.4s on ARM

I meant the difference between assigning info.magic and skipping it.

It is proportional to the amount of preserved memory. Extra assignment for each page. In our fleet we have observed IOMMU page tables to be 20G in size. So, let's just assume it is 20G. That is: 20 * 1024^3 /

Do you see 400ms reboot times on machines that have 20G of IOMMU page tables? That's impressive presuming the overall size of those machines.

Quoted 7 lines

4096 = 5.24 million pages. If we access "struct page" only for the magic purpose, we fetch full 64-byte cacheline, which is 5.24 million
* 64 bytes = 335 M, that is ~13ms with ~25G/s DRAM; and also each TLB miss will add some latency, 5.2M * 10ns = ~50ms. In total we can get 15ms ~ 50ms regression compared to 400ms, that is 4-12%. It will be less if we also access "struct page" for another reason at the same time, but still it adds up.

Your overhead calculations are based on the assumption that we don't access struct page, but we do. We assign page->private during deserialization and then initialize struct page during restore. We get the hit of cache fetches and TLB misses anyway.

It would be interesting to see the difference *measured* on those large systems.

Quoted 9 lines
Quoted 8 lines
Quoted 2 lines

(shutdown+purgatory+boot), let's not add anything to regress, as every microsecond counts during blackout.

Any added functionality adds cycles, this is inevitable. And neither KHO nor LUO are near the completion, so we'll have to add functionality to both of them. And the added functionality should be correct first and foremost. And magic sanity check seems pretty useful and presumably cheap enough to always keep it unless you see a real slowdown because of it.

Quoted 6 lines

Magic check is proportional to the amount of preserved memory. It is not a required functionality, only a sanity checking. I really do not see a reason to enable it in production. All other sanity struct page, and pg_flags related sanity checking are usually enabled with CONFIG_DEBUG_VM, so enabling it only with CONFIG_KEXEC_HANDOVER_DEBUG is better.

Having sanity checks in production could be useful because some errors could be hard to reproduce in controlled environment with debug kernel.

Just last cycle there was commit 83c8f7b5e194 ("mm/mm_init: Introduce a boot parameter for check_pages") that allows enabling page sanity checks in production.

So my take is to keep the magic check until KHO/LUO mature at least.

Signature · 2 lines
Sincerely yours,
Mike.
maintainer
#24 lore raw
Quoted 22 lines · Mike Rapoport
Quoted 21 lines · Pasha Tatashin
Quoted 20 lines · Mike Rapoport
Quoted 18 lines · Pasha Tatashin
Quoted 16 lines · Mike Rapoport
Quoted 10 lines · Pasha Tatashin
Quoted 8 lines · Pratyush Yadav

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

Quoted 2 lines

It is part of a critical hotpath during blackout, should really be behind CONFIG_KEXEC_HANDOVER_DEBUG

Do you have the numbers? ;-)

Quoted 7 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

The fastest reboot we can achieve is ~0.4s on ARM

I meant the difference between assigning info.magic and skipping it.

It is proportional to the amount of preserved memory. Extra assignment for each page. In our fleet we have observed IOMMU page tables to be 20G in size. So, let's just assume it is 20G. That is: 20 * 1024^3 /

The magic check is done for each preservation, not for each page. So if the 20G of preserved memory is 1G huge pages, then you only need 20 to check the magic 20 times.

Quoted 12 lines

Do you see 400ms reboot times on machines that have 20G of IOMMU page tables? That's impressive presuming the overall size of those machines.

Quoted 7 lines

4096 = 5.24 million pages. If we access "struct page" only for the magic purpose, we fetch full 64-byte cacheline, which is 5.24 million
* 64 bytes = 335 M, that is ~13ms with ~25G/s DRAM; and also each TLB miss will add some latency, 5.2M * 10ns = ~50ms. In total we can get 15ms ~ 50ms regression compared to 400ms, that is 4-12%. It will be less if we also access "struct page" for another reason at the same time, but still it adds up.

Quoted 4 lines

Your overhead calculations are based on the assumption that we don't access struct page, but we do. We assign page->private during deserialization and then initialize struct page during restore. We get the hit of cache fetches and TLB misses anyway.

Exactly. The cache line will be fetched anyway. So I think the real overhead is a fetch and compare.

Quoted 4 lines

It would be interesting to see the difference *measured* on those large systems.

[...]

Signature · 2 lines
Regards,
Pratyush Yadav
maintainer
#25 lore raw
In reply to Pasha Tatashin
Quoted 3 lines · Pasha Tatashin
Quoted 2 lines · Pratyush Yadav

On Tue, Dec 23 2025, Pasha Tatashin wrote:

[...]

Quoted 23 lines
Quoted 6 lines
Quoted 5 lines

I kind of do not like relying on magic to decide whether to initialize the struct page. I would prefer to avoid this magic marker altogether:
i.e. struct page is either initialized or not, not halfway initialized, etc.

Quoted 7 lines

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

It is part of a critical hotpath during blackout, should really be behind CONFIG_KEXEC_HANDOVER_DEBUG

Quoted 6 lines
Quoted 5 lines

Magic is not reliable. During machine reset in many firmware implementations, and in every kexec reboot, memory is not zeroed. The kernel usually allocates vmemmap using exactly the same pages, so there is just too high a chance of getting magic values accidentally inherited from the previous boot.

Quoted 7 lines
Quoted 3 lines

I don't think that can happen. All the pages are zeroed when initialized, which will clear the magic. We should only be setting the magic on an initialized struct page.

This can happen due to bugs when we use a partially initialized "struct page", something that Mike have been looking to do. So, pass some information in a struct page before it is fully initialized.

The magic is checked at restore time though, and by then all non-KHO pages should be properly initialized and have their magic cleared.

Also, the magic is cleared by kho_restore_folio(), so this can only ever happen for pages that were preserved but not restored in the previous boot. I don't think that is a common use case in the first place.

[...]

Signature · 2 lines
Regards,
Pratyush Yadav
maintainer
#26 lore raw
In reply to Pratyush Yadav
Quoted 21 lines · Pratyush Yadav
Quoted 11 lines · Pasha Tatashin
Quoted 7 lines
Quoted 2 lines

if (WARN_ON_ONCE(info.magic != KHO_PAGE_MAGIC || info.order > MAX_PAGE_ORDER)) return NULL;

See my patch that drops this restriction:
https://lore.kernel.org/linux-mm/[email protected]/

I think it was wrong to add it in the first place.

Agree, the restriction can be removed. Indeed, it is wrong as it is not enforced during preservation.

Quoted 7 lines

However, I think we are going to be in a world of pain if we allow preserving memory from different topologies within the same order. In kho_preserve_pages(), we have to check if the first and last page are from the same nid; if not, reduce the order by 1 and repeat until they are. It is just wrong to intermix different memory into the same order, so in addition to removing that restriction, I think we should implement this enforcement.

Quoted 7 lines

Sure, makes sense.

Quoted 6 lines

Also, perhaps we should pass the NID in the Jason's radix tree together with the order. We could have a single tree that encodes both order and NID information in the top level, or we can have one tree per NID. It does not really matter to me, but that should help us with faster struct page initialization.

To setup page links we need nid and zone. AFAIR we have 7 or 8 upper bits free in the radix tree, so to support the general case of up to 3 bits per zone and up to 10 bits per node we'll need to implement two versions of detection of zone and node for a page.
I'd wait with this optimization for a while.

Quoted 2 lines

Can we use NIDs in ABI? Do they stay stable across reboots? I never looked at how NIDs actually get assigned.

Node ids are assigned by the firmware, so unless firmware changes or there are hotplugged/hotremoved memory they are stable.
And we can't really do hotplug/hotremove with KHO/LUO anyway :)

Quoted 37 lines

Not sure if we should target it for the initial merge of the radix tree, but I think this is something we can try to figure out later down the line.

Quoted 33 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

To get the nid, you would need to call early_pfn_to_nid(). This takes a spinlock and searches through all memblock memory regions. I don't think it is too expensive, but it isn't free either. And all this would be done serially. With the zone search, you at least have some room for concurrency.

I think either approach only makes a difference when we have a large number of low-order preservations. If we have a handful of high-order preservations, I suppose the overhead of nid search would be negligible.

Quoted 5 lines
Quoted 3 lines

We should be targeting a situation where the vast majority of the preserved memory is HugeTLB, but I am still worried about lower order preservation efficiency for IOMMU page tables, etc.

Yep. Plus we might get VMMs stashing some of their state in a memfd too.

Yes, that is true, but hopefully those are tiny compared to everything else.

Quoted 14 lines
Quoted 8 lines
Quoted 4 lines

Long term, I think we should hook this into page_alloc_init_late() so that all the KHO pages also get initalized along with all the other pages. This will result in better integration of KHO with rest of MM init, and also have more consistent page restore performance.

But we keep KHO as reserved memory, and hooking it up into page_alloc_init_late() would make it very different, since that memory is part of the buddy allocator memory...

The idea I have is to have a separate call in page_alloc_init_late() that initalizes KHO pages. It would traverse the radix tree (probably in parallel by distributing the address space across multiple threads?) and initialize all the pages. Then kho_restore_page() would only have to double-check the magic and it can directly return the page.

page_alloc_init_late() is probably too late and some subsystems might need to call kho_restore_*() before it.

Quoted 20 lines
Quoted 4 lines

I kind of do not like relying on magic to decide whether to initialize the struct page. I would prefer to avoid this magic marker altogether:
i.e. struct page is either initialized or not, not halfway initialized, etc.

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

Quoted 6 lines

Magic is not reliable. During machine reset in many firmware implementations, and in every kexec reboot, memory is not zeroed. The kernel usually allocates vmemmap using exactly the same pages, so there is just too high a chance of getting magic values accidentally inherited from the previous boot.

Quoted 3 lines

I don't think that can happen. All the pages are zeroed when initialized, which will clear the magic. We should only be setting the magic on an initialized struct page.

Currently we set the magic on an initialized struct page because we don't support deferred struct page initialization. If we want to enable it, lots of struct pages are uninitialized by the time kho_mem_deserialize() runs.

To ensure there are no concerns with the stale data in the memory map we either need to initialize struct pages in kho_mem_deserialize() before setting page->private or let memmap_init_reserved_pages() initialize them (e.g by splitting memblock_reserve() out of kho_mem_deserialize() and calling it before memmap_init_reserved_pages())

It seems that hugetlb support anyway requires moving of memblock_reserve() earlier, so maybe we can do it as a part of deferred initialization work.

Quoted 3 lines
Signature · 2 lines
Regards,
Pratyush Yadav
Signature · 2 lines
Sincerely yours,
Mike.
#27 lore raw
In reply to Pratyush Yadav
Quoted 22 lines · Pratyush Yadav
Quoted 11 lines · Pasha Tatashin
Quoted 7 lines
Quoted 2 lines

if (WARN_ON_ONCE(info.magic != KHO_PAGE_MAGIC || info.order > MAX_PAGE_ORDER)) return NULL;

See my patch that drops this restriction:
https://lore.kernel.org/linux-mm/[email protected]/

I think it was wrong to add it in the first place.

Agree, the restriction can be removed. Indeed, it is wrong as it is not enforced during preservation.

Quoted 7 lines

However, I think we are going to be in a world of pain if we allow preserving memory from different topologies within the same order. In kho_preserve_pages(), we have to check if the first and last page are from the same nid; if not, reduce the order by 1 and repeat until they are. It is just wrong to intermix different memory into the same order, so in addition to removing that restriction, I think we should implement this enforcement.

Quoted 2 lines

Sure, makes sense.

Yes I think this makes the life easier and simplifies NID retrieval, as I mentioned below.

Quoted 10 lines
Quoted 6 lines

Also, perhaps we should pass the NID in the Jason's radix tree together with the order. We could have a single tree that encodes both order and NID information in the top level, or we can have one tree per NID. It does not really matter to me, but that should help us with faster struct page initialization.

Can we use NIDs in ABI? Do they stay stable across reboots? I never looked at how NIDs actually get assigned.

To encode the NID in to a single radix tree I think we can use the prefix bits before the order bit. For a preserved order-0 page the order bit is at 52 so we can use the bits 63-53 to encode the NID for each order. But I tend to have one tree for NID, which makes the process easier and more scalable. The cost will be 1 extra page because of the array pointing to each radix tree.

When traversing the radix tree during early boot we must initialize the head pages. We can have the page.private containing the order and NID info, like:

```
union kho_page_info {
  unsigned long page_private;
  struct {
    u16_t nid;
    u16_t order;
    unsigned int magic;
  };
};
```

early_pfn_to_nid() should work here. If we ensure that pages in the same order do not cross the zone boundary as Pasha mentioned above, we can set the tail pages with the same NID later, when calling kho_restore_page() after deferred struct page init.

With the union kho_page_info, this will be part of the ABI.

Quoted 4 lines

Not sure if we should target it for the initial merge of the radix tree, but I think this is something we can try to figure out later down the line.

I suggest keeping the initial merge of the radix tree simpler, ensuring it has the same feature set as the current implementation. We can add the new deferred struct page support as a separate feature later. =)

Quoted 39 lines
Quoted 34 lines
Quoted 11 lines
Quoted 11 lines
Quoted 10 lines

To get the nid, you would need to call early_pfn_to_nid(). This takes a spinlock and searches through all memblock memory regions. I don't think it is too expensive, but it isn't free either. And all this would be done serially. With the zone search, you at least have some room for concurrency.

I think either approach only makes a difference when we have a large number of low-order preservations. If we have a handful of high-order preservations, I suppose the overhead of nid search would be negligible.

Quoted 5 lines
Quoted 3 lines

We should be targeting a situation where the vast majority of the preserved memory is HugeTLB, but I am still worried about lower order preservation efficiency for IOMMU page tables, etc.

Yep. Plus we might get VMMs stashing some of their state in a memfd too.

Yes, that is true, but hopefully those are tiny compared to everything else.

Quoted 9 lines
Quoted 8 lines
Quoted 4 lines

Long term, I think we should hook this into page_alloc_init_late() so that all the KHO pages also get initalized along with all the other pages. This will result in better integration of KHO with rest of MM init, and also have more consistent page restore performance.

But we keep KHO as reserved memory, and hooking it up into page_alloc_init_late() would make it very different, since that memory is part of the buddy allocator memory...

Quoted 5 lines

The idea I have is to have a separate call in page_alloc_init_late() that initalizes KHO pages. It would traverse the radix tree (probably in parallel by distributing the address space across multiple threads?) and initialize all the pages. Then kho_restore_page() would only have to double-check the magic and it can directly return the page.

Quoted 4 lines

I kind of do not like relying on magic to decide whether to initialize the struct page. I would prefer to avoid this magic marker altogether:
i.e. struct page is either initialized or not, not halfway initialized, etc.

Quoted 8 lines

The magic is purely sanity checking. It is not used to decide anything other than to make sure this is actually a KHO page. I don't intend to change that. My point is, if we make sure the KHO pages are properly initialized during MM init, then restoring can actually be a very cheap operation, where you only do the sanity checking. You can even put the magic check behind CONFIG_KEXEC_HANDOVER_DEBUG if you want, but I think it is useful enough to keep in production systems too.

I think we should traverse the radix tree once during the early boot-up to reserve pages in memblock. But if we can hook the radix tree walking in page_alloc_init_late(), we can further defer the page structs (including head and tail) here. Having one radix tree per NID seems handy here if page_alloc_init_late() allocates one thread per NID for parallelization.

Quoted 25 lines
Quoted 6 lines

Magic is not reliable. During machine reset in many firmware implementations, and in every kexec reboot, memory is not zeroed. The kernel usually allocates vmemmap using exactly the same pages, so there is just too high a chance of getting magic values accidentally inherited from the previous boot.

I don't think that can happen. All the pages are zeroed when initialized, which will clear the magic. We should only be setting the magic on an initialized struct page.

Quoted 14 lines
Quoted 2 lines

Radix tree makes parallelism easier than the linked lists we have now.

Agree, radix tree can absolutely help with parallelism.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Jason's radix tree patches will make that a bit easier to do I think. The zone search will scale better I reckon.

It could, perhaps early in boot we should reserve the radix tree, and use it as a source of truth look-ups later in boot?

Yep. I think the radix tree should mark its own pages as preserved too so they stick around later in boot.

Quoted 6 lines
Quoted 3 lines

Unfortunately, this can only be done in the new kernel, not in the old kernel; otherwise we can end up with a recursive dependency that may never be satisfied.

Right. It shouldn't be too hard to do in the new kernel though. We will walk the whole tree anyway.

Yes, we can preserve the pages used for radix tree nodes in the new kernel. I think we should un-preserve them once LUO is done? Since KHO can be used by other clients we can add APIs to preserve/unpreserve the radix tree nodes.

--
Jason Miu

maintainer
#28 lore raw
In reply to Evangelos Petrongonas

Hi Evangelos,

Are you planning to send a v2?

Quoted 118 lines · 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.

Fixes 8b66ed2c3f42 ("kho: mm: don't allow deferred struct page with KHO")

---
### Notes
@Jason, this patch should act as a temporary fix to make KHO play nice with deferred struct page init until you post your ideas about splitting "Physical Reservation" from "Metadata Restoration".

### Testing
In order to test the fix, I modified the KHO selftest, to allocate more memory and do so from higher memory to trigger the incompatibility. The branch with those changes can be found in:
https://git.infradead.org/?p=users/vpetrog/linux.git;a=shortlog;h=refs/heads/kho-deferred-struct-page-init

In future patches, we might want to enhance the selftest to cover this case as well. However, properly adopting the test for this is much more work than the actual fix, therefore it can be deferred to a follow-up series.

In addition attempting to run the selftest for arm (without my changes) fails with:

```
ERROR:target/arm/internals.h:767:regime_is_user: code should not be reached
Bail out! ERROR:target/arm/internals.h:767:regime_is_user: code should not be reached
./tools/testing/selftests/kho/vmtest.sh: line 113: 61609 Aborted
```

I have not looked it up further, but can also do so as part of a selftest follow-up.

 kernel/liveupdate/Kconfig          |  2 --
 kernel/liveupdate/kexec_handover.c | 19 ++++++++++++++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)
kernel/liveupdate/Kconfig +0 -2
index d2aeaf13c3ac..9394a608f939 100644
@@ -1,12 +1,10 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 menu "Live Update and Kexec HandOver"
-	depends on !DEFERRED_STRUCT_PAGE_INIT
 
 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
kernel/liveupdate/kexec_handover.c +18 -1
index 9dc51fab604f..78cfe71e6107 100644
@@ -439,6 +439,23 @@ static int kho_mem_serialize(struct kho_out *kho_out)
 	return err;
 }
 
+/*
+ * With CONFIG_DEFERRED_STRUCT_PAGE_INIT, struct pages in higher memory
+ * regions may not be initialized yet at the time KHO deserializes preserved
+ * memory. This function ensures all struct pages in the region are initialized.
+ */
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+						  unsigned int order)
+{
+	unsigned long pfn = PHYS_PFN(phys);
+	int nid = early_pfn_to_nid(pfn);
+
+	for (int i = 0; i < (1 << 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)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
 		int sz = 1 << (order + PAGE_SHIFT);
 		phys_addr_t phys =
 			elm->phys_start + (bit << (order + PAGE_SHIFT));
-		struct page *page = phys_to_page(phys);
+		struct page *page = kho_get_preserved_page(phys, order);
 		union kho_page_info info;
 
 		memblock_reserve(phys, sz);
Signature · 12 lines
2.43.0




Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
Signature · 2 lines
Sincerely yours,
Mike.
#29 lore raw
Quoted 5 lines · On Wed, Feb 04, 2026 at 08:44:48PM +0200 Mike Rapoport wrote:

Hi Evangelos,

Are you planning to send a v2?

On Tue, Dec 16, 2025 at 08:49:12AM +0000, Evangelos Petrongonas wrote:

Hi Mike,
I am leaving soon for some AFK time, therefore I don't believe I will post a v2 anytime before the beginning of March. Feel free (you or anyone else) to send a patch. If a patch is sent until then I am happy, to test it when I am back.

Quoted 110 lines
Quoted 110 lines

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.

Fixes 8b66ed2c3f42 ("kho: mm: don't allow deferred struct page with KHO")

---
### Notes
@Jason, this patch should act as a temporary fix to make KHO play nice with deferred struct page init until you post your ideas about splitting "Physical Reservation" from "Metadata Restoration".

### Testing
In order to test the fix, I modified the KHO selftest, to allocate more memory and do so from higher memory to trigger the incompatibility. The branch with those changes can be found in:
https://git.infradead.org/?p=users/vpetrog/linux.git;a=shortlog;h=refs/heads/kho-deferred-struct-page-init

In future patches, we might want to enhance the selftest to cover this case as well. However, properly adopting the test for this is much more work than the actual fix, therefore it can be deferred to a follow-up series.

In addition attempting to run the selftest for arm (without my changes) fails with:

```
ERROR:target/arm/internals.h:767:regime_is_user: code should not be reached
Bail out! ERROR:target/arm/internals.h:767:regime_is_user: code should not be reached
./tools/testing/selftests/kho/vmtest.sh: line 113: 61609 Aborted
```

I have not looked it up further, but can also do so as part of a selftest follow-up.

 kernel/liveupdate/Kconfig          |  2 --
 kernel/liveupdate/kexec_handover.c | 19 ++++++++++++++++++-
 2 files changed, 18 insertions(+), 3 deletions(-)
kernel/liveupdate/Kconfig +0 -2
index d2aeaf13c3ac..9394a608f939 100644
@@ -1,12 +1,10 @@
 # SPDX-License-Identifier: GPL-2.0-only
 
 menu "Live Update and Kexec HandOver"
-	depends on !DEFERRED_STRUCT_PAGE_INIT
 
 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
kernel/liveupdate/kexec_handover.c +18 -1
index 9dc51fab604f..78cfe71e6107 100644
@@ -439,6 +439,23 @@ static int kho_mem_serialize(struct kho_out *kho_out)
 	return err;
 }
 
+/*
+ * With CONFIG_DEFERRED_STRUCT_PAGE_INIT, struct pages in higher memory
+ * regions may not be initialized yet at the time KHO deserializes preserved
+ * memory. This function ensures all struct pages in the region are initialized.
+ */
+static struct page *__init kho_get_preserved_page(phys_addr_t phys,
+						  unsigned int order)
+{
+	unsigned long pfn = PHYS_PFN(phys);
+	int nid = early_pfn_to_nid(pfn);
+
+	for (int i = 0; i < (1 << 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)
 {
@@ -449,7 +466,7 @@ static void __init deserialize_bitmap(unsigned int order,
 		int sz = 1 << (order + PAGE_SHIFT);
 		phys_addr_t phys =
 			elm->phys_start + (bit << (order + PAGE_SHIFT));
-		struct page *page = phys_to_page(phys);
+		struct page *page = kho_get_preserved_page(phys, order);
 		union kho_page_info info;
 
 		memblock_reserve(phys, sz);
Signature · 1 line
2.43.0
Quoted 12 lines
Quoted 8 lines

Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597

Signature · 2 lines
Sincerely yours,
Mike.

--
Kind Regards,
Evangelos

Amazon Web Services Development Center Germany GmbH Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597