[PATCHv7 07/18] mm: Rework compound_head() for power-of-2 sizeof(struct page)

Kiryl Shutsemau (Meta)
2026-02-27T19:30:44Z

From: Kiryl Shutsemau <kas@kernel.org>

For tail pages, the kernel uses the 'compound_info' field to get to the
head page. The bit 0 of the field indicates whether the page is a
tail page, and if set, the remaining bits represent a pointer to the
head page.

For cases when size of struct page is power-of-2, change the encoding of
compound_info to store a mask that can be applied to the virtual address
of the tail page in order to access the head page. It is possible
because struct page of the head page is naturally aligned with regards
to order of the page.

The significant impact of this modification is that all tail pages of
the same order will now have identical 'compound_info', regardless of
the compound page they are associated with. This paves the way for
eliminating fake heads.

The HugeTLB Vmemmap Optimization (HVO) creates fake heads and it is only
applied when the sizeof(struct page) is power-of-2. Having identical
tail pages allows the same page to be mapped into the vmemmap of all
pages, maintaining memory savings without fake heads.

If sizeof(struct page) is not power-of-2, there is no functional
changes.

Limit mask usage to HugeTLB vmemmap optimization (HVO) where it makes
a difference. The approach with mask would work in the wider set of
conditions, but it requires validating that struct pages are naturally
aligned for all orders up to the MAX_FOLIO_ORDER, which can be tricky.

Signed-off-by: Kiryl Shutsemau <kas@kernel.org>
Reviewed-by: Muchun Song <muchun.song@linux.dev>
Reviewed-by: Zi Yan <ziy@nvidia.com>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Acked-by: Usama Arif <usamaarif642@gmail.com>
Reviewed-by: Vlastimil Babka <vbabka@suse.cz>
---
include/linux/page-flags.h | 81 ++++++++++++++++++++++++++++++++++----
mm/slab.h | 16 ++++++--
mm/util.c | 16 ++++++--
3 files changed, 97 insertions(+), 16 deletions(-)

diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h
index 42bf8ed02a29..01970bd38bff 100644
--- a/include/linux/page-flags.h
+++ b/include/linux/page-flags.h
@@ -198,6 +198,29 @@ enum pageflags {

#ifndef __GENERATING_BOUNDS_H

+/*
+ * For tail pages, if the size of struct page is power-of-2 ->compound_info
+ * encodes the mask that converts the address of the tail page address to
+ * the head page address.
+ *
+ * Otherwise, ->compound_info has direct pointer to head pages.
+ */
+static __always_inline bool compound_info_has_mask(void)
+{
+ /*
+ * Limit mask usage to HugeTLB vmemmap optimization (HVO) where it
+ * makes a difference.
+ *
+ * The approach with mask would work in the wider set of conditions,
+ * but it requires validating that struct pages are naturally aligned
+ * for all orders up to the MAX_FOLIO_ORDER, which can be tricky.
+ */
+ if (!IS_ENABLED(CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP))
+ return false;
+
+ return is_power_of_2(sizeof(struct page));
+}
+
#ifdef CONFIG_HUGETLB_PAGE_OPTIMIZE_VMEMMAP
DECLARE_STATIC_KEY_FALSE(hugetlb_optimize_vmemmap_key);

@@ -207,6 +230,10 @@ DECLARE_STATIC_KEY_FALSE(hugetlb_optimize_vmemmap_key);
*/
static __always_inline const struct page *page_fixed_fake_head(const struct page *page)
{
+ /* Fake heads only exists if compound_info_has_mask() is true */
+ if (!compound_info_has_mask())
+ return page;
+
if (!static_branch_unlikely(&hugetlb_optimize_vmemmap_key))
return page;

@@ -223,10 +250,14 @@ static __always_inline const struct page *page_fixed_fake_head(const struct page
* because the @page is a compound page composed with at least
* two contiguous pages.
*/
- unsigned long head = READ_ONCE(page[1].compound_info);
+ unsigned long info = READ_ONCE(page[1].compound_info);

- if (likely(head & 1))
- return (const struct page *)(head - 1);
+ /* See set_compound_head() */
+ if (likely(info & 1)) {
+ unsigned long p = (unsigned long)page;
+
+ return (const struct page *)(p & info);
+ }
}
return page;
}
@@ -281,11 +312,26 @@ static __always_inline int page_is_fake_head(const struct page *page)

static __always_inline unsigned long _compound_head(const struct page *page)
{
- unsigned long head = READ_ONCE(page->compound_info);
+ unsigned long info = READ_ONCE(page->compound_info);

- if (unlikely(head & 1))
- return head - 1;
- return (unsigned long)page_fixed_fake_head(page);
+ /* Bit 0 encodes PageTail() */
+ if (!(info & 1))
+ return (unsigned long)page_fixed_fake_head(page);
+
+ /*
+ * If compound_info_has_mask() is false, the rest of compound_info is
+ * the pointer to the head page.
+ */
+ if (!compound_info_has_mask())
+ return info - 1;
+
+ /*
+ * If compound_info_has_mask() is true the rest of the info encodes
+ * the mask that converts the address of the tail page to the head page.
+ *
+ * No need to clear bit 0 in the mask as 'page' always has it clear.
+ */
+ return (unsigned long)page & info;
}

#define compound_head(page) ((typeof(page))_compound_head(page))
@@ -293,7 +339,26 @@ static __always_inline unsigned long _compound_head(const struct page *page)
static __always_inline void set_compound_head(struct page *tail,
const struct page *head, unsigned int order)
{
- WRITE_ONCE(tail->compound_info, (unsigned long)head + 1);
+ unsigned int shift;
+ unsigned long mask;
+
+ if (!compound_info_has_mask()) {
+ WRITE_ONCE(tail->compound_info, (unsigned long)head | 1);
+ return;
+ }
+
+ /*
+ * If the size of struct page is power-of-2, bits [shift:0] of the
+ * virtual address of compound head are zero.
+ *
+ * Calculate mask that can be applied to the virtual address of
+ * the tail page to get address of the head page.
+ */
+ shift = order + order_base_2(sizeof(struct page));
+ mask = GENMASK(BITS_PER_LONG - 1, shift);
+
+ /* Bit 0 encodes PageTail() */
+ WRITE_ONCE(tail->compound_info, mask | 1);
}

static __always_inline void clear_compound_head(struct page *page)
diff --git a/mm/slab.h b/mm/slab.h
index 62dfa50c1f01..1a1b3758df05 100644
--- a/mm/slab.h
+++ b/mm/slab.h
@@ -131,11 +131,19 @@ static_assert(IS_ALIGNED(offsetof(struct slab, freelist), sizeof(struct freelist
*/
static inline struct slab *page_slab(const struct page *page)
{
- unsigned long head;
+ unsigned long info;
+
+ info = READ_ONCE(page->compound_info);
+ if (info & 1) {
+ /* See compound_head() */
+ if (compound_info_has_mask()) {
+ unsigned long p = (unsigned long)page;
+ page = (struct page *)(p & info);
+ } else {
+ page = (struct page *)(info - 1);
+ }
+ }

- head = READ_ONCE(page->compound_head);
- if (head & 1)
- page = (struct page *)(head - 1);
if (data_race(page->page_type >> 24) != PGTY_slab)
page = NULL;

diff --git a/mm/util.c b/mm/util.c
index 3ebcb9e6035c..20dccf2881d7 100644
--- a/mm/util.c
+++ b/mm/util.c
@@ -1237,7 +1237,7 @@ static void set_ps_flags(struct page_snapshot *ps, const struct folio *folio,
*/
void snapshot_page(struct page_snapshot *ps, const struct page *page)
{
- unsigned long head, nr_pages = 1;
+ unsigned long info, nr_pages = 1;
struct folio *foliop;
int loops = 5;

@@ -1247,8 +1247,8 @@ void snapshot_page(struct page_snapshot *ps, const struct page *page)
again:
memset(&ps->folio_snapshot, 0, sizeof(struct folio));
memcpy(&ps->page_snapshot, page, sizeof(*page));
- head = ps->page_snapshot.compound_info;
- if ((head & 1) == 0) {
+ info = ps->page_snapshot.compound_info;
+ if (!(info & 1)) {
ps->idx = 0;
foliop = (struct folio *)&ps->page_snapshot;
if (!folio_test_large(foliop)) {
@@ -1259,7 +1259,15 @@ void snapshot_page(struct page_snapshot *ps, const struct page *page)
}
foliop = (struct folio *)page;
} else {
- foliop = (struct folio *)(head - 1);
+ /* See compound_head() */
+ if (compound_info_has_mask()) {
+ unsigned long p = (unsigned long)page;
+
+ foliop = (struct folio *)(p & info);
+ } else {
+ foliop = (struct folio *)(info - 1);
+ }
+
ps->idx = folio_page_idx(foliop, page);
}

--
2.51.2