Skip to content

Commit 12b9a2c

Browse files
prati0100akpm00
authored andcommitted
kho: initialize tail pages for higher order folios properly
Currently, when restoring higher order folios, kho_restore_folio() only calls prep_compound_page() on all the pages. That is not enough to properly initialize the folios. The managed page count does not get updated, the reserved flag does not get dropped, and page count does not get initialized properly. Restoring a higher order folio with it results in the following BUG with CONFIG_DEBUG_VM when attempting to free the folio: BUG: Bad page state in process test pfn:104e2b page: refcount:1 mapcount:0 mapping:0000000000000000 index:0xffffffffffffffff pfn:0x104e2b flags: 0x2fffff80000000(node=0|zone=2|lastcpupid=0x1fffff) raw: 002fffff80000000 0000000000000000 00000000ffffffff 0000000000000000 raw: ffffffffffffffff 0000000000000000 00000001ffffffff 0000000000000000 page dumped because: nonzero _refcount [...] Call Trace: <TASK> dump_stack_lvl+0x4b/0x70 bad_page.cold+0x97/0xb2 __free_frozen_pages+0x616/0x850 [...] Combine the path for 0-order and higher order folios, initialize the tail pages with a count of zero, and call adjust_managed_page_count() to account for all the pages instead of just missing them. In addition, since all the KHO-preserved pages get marked with MEMBLOCK_RSRV_NOINIT by deserialize_bitmap(), the reserved flag is not actually set (as can also be seen from the flags of the dumped page in the logs above). So drop the ClearPageReserved() calls. [ptyadav@amazon.de: declare i in the loop instead of at the top] Link: https://lkml.kernel.org/r/20250613125916.39272-1-pratyush@kernel.org Link: https://lkml.kernel.org/r/20250605171143.76963-1-pratyush@kernel.org Fixes: fc33e4b ("kexec: enable KHO support for memory preservation") Signed-off-by: Pratyush Yadav <ptyadav@amazon.de> Reviewed-by: Pasha Tatashin <pasha.tatashin@soleen.com> Reviewed-by: Mike Rapoport (Microsoft) <rppt@kernel.org> Cc: Alexander Graf <graf@amazon.com> Cc: Baoquan He <bhe@redhat.com> Cc: Changyuan Lyu <changyuanl@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent 417d145 commit 12b9a2c

1 file changed

Lines changed: 17 additions & 12 deletions

File tree

kernel/kexec_handover.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,21 @@ static int __kho_preserve_order(struct kho_mem_track *track, unsigned long pfn,
164164
}
165165

166166
/* almost as free_reserved_page(), just don't free the page */
167-
static void kho_restore_page(struct page *page)
167+
static void kho_restore_page(struct page *page, unsigned int order)
168168
{
169-
ClearPageReserved(page);
170-
init_page_count(page);
171-
adjust_managed_page_count(page, 1);
169+
unsigned int nr_pages = (1 << order);
170+
171+
/* Head page gets refcount of 1. */
172+
set_page_count(page, 1);
173+
174+
/* For higher order folios, tail pages get a page count of zero. */
175+
for (unsigned int i = 1; i < nr_pages; i++)
176+
set_page_count(page + i, 0);
177+
178+
if (order > 0)
179+
prep_compound_page(page, order);
180+
181+
adjust_managed_page_count(page, nr_pages);
172182
}
173183

174184
/**
@@ -186,15 +196,10 @@ struct folio *kho_restore_folio(phys_addr_t phys)
186196
return NULL;
187197

188198
order = page->private;
189-
if (order) {
190-
if (order > MAX_PAGE_ORDER)
191-
return NULL;
192-
193-
prep_compound_page(page, order);
194-
} else {
195-
kho_restore_page(page);
196-
}
199+
if (order > MAX_PAGE_ORDER)
200+
return NULL;
197201

202+
kho_restore_page(page, order);
198203
return page_folio(page);
199204
}
200205
EXPORT_SYMBOL_GPL(kho_restore_folio);

0 commit comments

Comments
 (0)