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

From: Kiryl Shutsemau

Date: Wed Jan 28 2026 - 08:57:01 EST


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

Trigger a BUG() for CONFIG_DEBUG_VM=y or WARN() otherwise.

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

diff --git a/mm/sparse.c b/mm/sparse.c
index b5b2b6f7041b..9c0f4015778c 100644
--- a/mm/sparse.c
+++ b/mm/sparse.c
@@ -600,6 +600,19 @@ 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;
+ bool aligned;
+
+ alignment = MAX_FOLIO_NR_PAGES * sizeof(struct page);
+ aligned = IS_ALIGNED((unsigned long) pfn_to_page(0), alignment);
+
+ if (IS_ENABLED(CONFIG_DEBUG_VM))
+ BUG_ON(!aligned);
+ else
+ WARN_ON(!aligned);
+ }
+
pnum_begin = first_present_section_nr();
nid_begin = sparse_early_nid(__nr_to_section(pnum_begin));

--
2.51.2