[RFC PATCH 4/5] mm: only use struct page in compound_nr() and compound_order()

From: Zi Yan

Date: Thu Jan 29 2026 - 22:54:12 EST


A compound page is not a folio. Using struct folio in compound_nr() and
compound_order() is misleading. Use struct page and refer to the right
subpage of a compound page to set compound page order. compound_nr() is
calculated using compound_order() instead of reading folio->_nr_pages.

Signed-off-by: Zi Yan <ziy@xxxxxxxxxx>
---
include/linux/mm.h | 12 ++++--------
1 file changed, 4 insertions(+), 8 deletions(-)

diff --git a/include/linux/mm.h b/include/linux/mm.h
index f8a8fd47399c..f1c54d9f4620 100644
--- a/include/linux/mm.h
+++ b/include/linux/mm.h
@@ -1428,11 +1428,9 @@ static inline unsigned long folio_large_nr_pages(const struct folio *folio)
*/
static inline unsigned int compound_order(const struct page *page)
{
- const struct folio *folio = (struct folio *)page;
-
- if (!test_bit(PG_head, &folio->flags.f))
+ if (!test_bit(PG_head, &page->flags.f))
return 0;
- return folio_large_order(folio);
+ return page[1].flags.f & 0xffUL;
}

/**
@@ -2514,11 +2512,9 @@ static inline unsigned long folio_nr_pages(const struct folio *folio)
*/
static inline unsigned long compound_nr(const struct page *page)
{
- const struct folio *folio = (struct folio *)page;
-
- if (!test_bit(PG_head, &folio->flags.f))
+ if (!test_bit(PG_head, &page->flags.f))
return 1;
- return folio_large_nr_pages(folio);
+ return 1 << compound_order(page);
}

/**
--
2.51.0