[PATCHv6 09/17] mm/sparse: Check memmap alignment for compound_info_has_mask()

Kiryl Shutsemau
2026-02-02T15:57:59Z

If page->compound_info encodes a mask, it is expected that vmemmap to be
naturally aligned to the maximum folio size.

Add a VM_BUG_ON() to check the alignment.

Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
Acked-by: Zi Yan <ziy@nvidia.com>
---
mm/sparse.c | 7 +++++++
1 file changed, 7 insertions(+)

diff --git a/mm/sparse.c b/mm/sparse.c
index b5b2b6f7041b..6c9b62607f3f 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -600,6 +600,13 @@ void __init sparse_init(void)
BUILD_BUG_ON(!is_power_of_2(sizeof(struct mem_section)));
memblocks_present();

+ if (compound_info_has_mask()) {
+ unsigned long alignment;
+
+ alignment = MAX_FOLIO_NR_PAGES * sizeof(struct page);
+ VM_BUG_ON(!IS_ALIGNED((unsigned long) pfn_to_page(0), alignment));
+ }
+
pnum_begin = first_present_section_nr();
nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));

--
2.51.2