Separate compound page from folio
Subsystems (7)
Zi Yan
reviewercover Separate compound page from folio
Hi all,
Based on my discussion with Jason about device private folio reinitialization[1], I realize that the concepts of compound page and folio are mixed together and confusing, as people think a compound page is equal to a folio. This is not true, since a compound page means a group of pages is managed as a whole and it can be something other than a folio, for example, a slab page. To avoid further confusing people, this patchset separates compound page from folio by moving any folio related code out of compound page functions.
The code is on top of mm-new (2026-01-28-20-27) and all mm selftests passed.
The key change is that a compound page no longer sets:
1. folio->_nr_pages,
2. folio->_large_mapcount,
3. folio->_nr_pages_mapped,
4. folio->_mm_ids,
5. folio->_mm_id_mapcount,
6. folio->_pincount,
7. folio->_entire_mapcount,
8. folio->_deferred_list.
Since these fields are only used by folios that are rmappable. The code setting these fields is moved to page_rmappable_folio(). To make the code move, this patchset also needs to changes several places, where folio and compound page are used interchangably or unusual folio use:
1. in io_mem_alloc_compound(), a compound page is allocated, but later it is mapped via vm_insert_pages() like a rmappable folio;
2. __split_folio_to_order() sets large_rmappable flag directly instead of using page_rmappable_folio() for after-split folios;
3. hugetlb unsets large_rmappable to escape deferred_list unqueue operation.
At last, the page freeing path is also changed to have different checks for compound page and folio.
One thing to note is that for compound page, I do not store compound order in folio->_nr_pages, which overlaps with page[1].memcg_data and use 1 << compound_order() instead, since I do not want to add a new union to struct page and compound_nr() is not as widely used as folio_nr_pages(). But let me know if there is a performance concern for this.
Comments and suggestions are welcome.
Zi Yan (5):
io_uring: allocate folio in io_mem_alloc_compound() and function
rename
mm/huge_memory: use page_rmappable_folio() to convert after-split
folios
mm/hugetlb: set large_rmappable on hugetlb and avoid deferred_list
handling
mm: only use struct page in compound_nr() and compound_order()
mm: code separation for compound page and folio
include/linux/mm.h | 12 ++++-------- io_uring/memmap.c | 12 ++++++------ mm/huge_memory.c | 5 ++--- mm/hugetlb.c | 8 ++++---- mm/hugetlb_cma.c | 2 +- mm/internal.h | 47 +++++++++++++++++++++++++++------------------- mm/mm_init.c | 2 +- mm/page_alloc.c | 23 ++++++++++++++++++----- 8 files changed, 64 insertions(+), 47 deletions(-)
Signature · 1 line
2.51.0
Zi Yan
reviewerRFC PATCH 1/5 io_uring: allocate folio in io_mem_alloc_compound() and function rename
The page allocated in io_mem_alloc_compound() is actually used as a folio later in io_region_mmap(). So allocate a folio instead of a compound page and rename io_mem_alloc_compound() to io_mem_alloc_folio().
This prepares for code separation of compound page and folio in a follow-up commit.
--- io_uring/memmap.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
io_uring/memmap.c +6 -6
@@ -15,10 +15,10 @@#include "rsrc.h" #include "zcrx.h" -static bool io_mem_alloc_compound (struct page **pages,int nr_pages, +static bool io_mem_alloc_folio (struct page **pages,int nr_pages,size_t size,gfp_t gfp) { -struct page *page; +struct folio *folio;int i, order; order =get_order (size);
@@ -27,12 +27,12 @@ static bool io_mem_alloc_compound(struct page **pages, int nr_pages,else if (order) gfp |= __GFP_COMP; - page =alloc_pages (gfp, order); -if (!page) + folio =folio_alloc (gfp, order); +if (!folio)return false;for (i =0 ; i < nr_pages; i++) - pages[i] = page + i; + pages[i] =folio_page (folio, i);return true; }
@@ -162,7 +162,7 @@ static int io_region_allocate_pages(struct io_mapped_region *mr,if (!pages)return -ENOMEM ; -if (io_mem_alloc_compound (pages, mr->nr_pages, size, gfp)) { +if (io_mem_alloc_folio (pages, mr->nr_pages, size, gfp)) { mr->flags |=IO_REGION_F_SINGLE_REF ; goto done; }
Signature · 1 line
2.51.0
Lance Yang
reviewerRe: [RFC PATCH 1/5] io_uring: allocate folio in io_mem_alloc_compound() and function rename
io_uring/memmap.c static bool io_mem_alloc_compound(struct page **pages, int nr_pages,
Quoted 30 lines · Zi Yan
The page allocated in
io_mem_alloc_compound()is actually used as a folio later inio_region_mmap(). So allocate a folio instead of a compound page and renameio_mem_alloc_compound()toio_mem_alloc_folio().This prepares for code separation of compound page and folio in a follow-up commit.
Signed-off-by Zi Yan <[email protected]>--- io_uring/memmap.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)io_uring/memmap.c +2
-2@@ -15,10 +15,10 @@#include "rsrc.h" #include "zcrx.h" -static bool io_mem_alloc_compound (struct page **pages,int nr_pages, +static bool io_mem_alloc_folio (struct page **pages,int nr_pages,size_t size,gfp_t gfp) { -struct page *page; +struct folio *folio;int i, order; order =get_order (size);@@ -27,12 +27,12 @@ static bool io_mem_alloc_compound(struct page **pages, int nr_pages,
Nit:
io_uring/memmap.c static bool io_mem_alloc_compound(struct page **pages, int nr_pages, · line 28
Quoted 2 lines
io_uring/memmap.c +0
-0else if (order) gfp |= __GFP_COMP;
Since we're switching to folio_alloc(), which already adds __GFP_COMP internally, the "else if (order)" part above can be dropped while at it.
IIUC, for order == 0, __GFP_COMP gets ignored anyway:
- prep_new_page() won't call prep_compound_page() (since order is zero)
- page_rmappable_folio() sees a non-compound page and does nothing
So no behavior change there :)
Quoted 22 lines
io_uring/memmap.c +4
-4- page =alloc_pages (gfp, order); -if (!page) + folio =folio_alloc (gfp, order); +if (!folio)return false;for (i =0 ; i < nr_pages; i++) - pages[i] = page + i; + pages[i] =folio_page (folio, i);return true; }@@ -162,7 +162,7 @@ static int io_region_allocate_pages(struct io_mapped_region *mr,if (!pages)return -ENOMEM ; -if (io_mem_alloc_compound (pages, mr->nr_pages, size, gfp)) { +if (io_mem_alloc_folio (pages, mr->nr_pages, size, gfp)) { mr->flags |=IO_REGION_F_SINGLE_REF ; goto done; }
Zi Yan
reviewerRe: [RFC PATCH 1/5] io_uring: allocate folio in io_mem_alloc_compound() and function rename
Quoted 38 lines · Lance Yang
io_uring/memmap.c static bool io_mem_alloc_compound(struct page **pages, int nr_pages,
Quoted 28 lines · Zi Yan
The page allocated in
io_mem_alloc_compound()is actually used as a folio later inio_region_mmap(). So allocate a folio instead of a compound page and renameio_mem_alloc_compound()toio_mem_alloc_folio().This prepares for code separation of compound page and folio in a follow-up commit.
Signed-off-by Zi Yan <[email protected]>--- io_uring/memmap.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)io_uring/memmap.c +2
-1@@ -15,10 +15,10 @@#include "rsrc.h" #include "zcrx.h" -static bool io_mem_alloc_compound(struct page **pages, int nr_pages, +static bool io_mem_alloc_folio (struct page **pages,int nr_pages, size_t size, gfp_t gfp) { -struct page *page; +struct folio *folio;int i, order; order =get_order (size);@@ -27,12 +27,12 @@ static bool io_mem_alloc_compound(struct page **pages, int nr_pages,Nit:
io_uring/memmap.c static bool io_mem_alloc_compound(struct page **pages, int nr_pages, · line 28
Quoted 2 lines
io_uring/memmap.c +0
-0else if (order) gfp |= __GFP_COMP;Since we're switching to
folio_alloc(), which already adds__GFP_COMPinternally, the "else if (order)" part above can be dropped while at it.
Quoted 7 lines
IIUC, for order == 0,
__GFP_COMPgets ignored anyway:-
prep_new_page()won't callprep_compound_page()(since order is zero)
-page_rmappable_folio()sees a non-compound page and does nothingSo no behavior change there :)
Sure. Will update it in the next version. Thanks.
--
Best Regards,
Yan, Zi
Zi Yan
reviewerRFC PATCH 2/5 mm/huge_memory: use page_rmappable_folio() to convert after-split folios
In reply to Zi YanCurrent code uses folio_set_large_rmappable() on after-split folios, but these folios should be treated as compound pages and converted to folios with page_rmappable_folio().
This prepares for code separation of compound page and folio in a follow-up commit.
--- mm/huge_memory.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
mm/huge_memory.c +2 -3
@@ -3558,10 +3558,9 @@ static void __split_folio_to_order(struct folio *folio, int old_order,* which needs correct compound_head(). */ clear_compound_head (new_head); -if (new_order) { +if (new_order)prep_compound_page (new_head, new_order); -folio_set_large_rmappable (new_folio); - } +page_rmappable_folio (new_head);if (folio_test_young (folio))folio_set_young (new_folio);
Signature · 1 line
2.51.0
Zi Yan
reviewerRFC PATCH 3/5 mm/hugetlb: set large_rmappable on hugetlb and avoid deferred_list handling
In reply to Zi YanCommit f708f6970cc9 ("mm/hugetlb: fix kernel NULL pointer dereference when migrating hugetlb folio") fixed a NULL pointer dereference when folio_undo_large_rmappable(), now folio_unqueue_deferred_list(), is used on hugetlb to clear deferred_list. It cleared large_rmappable flag on hugetlb. hugetlb is rmappable, thus clearing large_rmappable flag looks misleading. Instead, reject hugetlb in folio_unqueue_deferred_list() to avoid the issue.
This prepares for code separation of compound page and folio in a follow-up commit.
--- mm/hugetlb.c | 6 +++--- mm/hugetlb_cma.c | 2 +- mm/internal.h | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-)
mm/hugetlb.c +3 -3
@@ -1422,8 +1422,8 @@ static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask,if (hugetlb_cma_exclusive_alloc ())return NULL ; - folio = (struct folio *)alloc_contig_frozen_pages (1 << order, gfp_mask, - nid, nodemask); + folio =page_rmappable_folio (alloc_contig_frozen_pages (1 << order, gfp_mask, + nid, nodemask)); return folio; }#else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE || !CONFIG_CONTIG_ALLOC */
@@ -1859,7 +1859,7 @@ static struct folio *alloc_buddy_frozen_folio(int order, gfp_t gfp_mask,if (alloc_try_hard) gfp_mask |= __GFP_RETRY_MAYFAIL; - folio = (struct folio *)__alloc_frozen_pages (gfp_mask, order, nid, nmask); + folio =page_rmappable_folio (__alloc_frozen_pages (gfp_mask, order, nid, nmask));/* * If we did not specify __GFP_RETRY_MAYFAIL, but still got a
mm/hugetlb_cma.c +1 -1
@@ -51,7 +51,7 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,if (!page)return NULL ; - folio =page_folio (page); + folio =page_rmappable_folio (page);folio_set_hugetlb_cma (folio);return folio; }
mm/internal.h +2 -1
@@ -835,7 +835,8 @@ static inline void folio_set_order(struct folio *folio, unsigned int order)bool __folio_unqueue_deferred_split (struct folio *folio);static inline bool folio_unqueue_deferred_split (struct folio *folio) { -if (folio_order (folio) <=1 || !folio_test_large_rmappable (folio)) +if (folio_order (folio) <=1 || !folio_test_large_rmappable (folio) || +folio_test_hugetlb (folio))return false;/*
Signature · 1 line
2.51.0
Baolin Wang
reviewerRe: [RFC PATCH 3/5] mm/hugetlb: set large_rmappable on hugetlb and avoid deferred_list handling
Quoted 47 lines · Zi Yan
Commit
f708f6970cc9("mm/hugetlb: fix kernel NULL pointer dereference when migrating hugetlb folio") fixed a NULL pointer dereference whenfolio_undo_large_rmappable(), nowfolio_unqueue_deferred_list(), is used on hugetlb to cleardeferred_list. It clearedlarge_rmappableflag on hugetlb. hugetlb is rmappable, thus clearinglarge_rmappableflag looks misleading. Instead, reject hugetlb infolio_unqueue_deferred_list()to avoid the issue.This prepares for code separation of compound page and folio in a follow-up commit.
Signed-off-by Zi Yan <[email protected]>--- mm/hugetlb.c | 6 +++--- mm/hugetlb_cma.c | 2 +- mm/internal.h | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-)mm/hugetlb.c +3
-3@@ -1422,8 +1422,8 @@ static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask,if (hugetlb_cma_exclusive_alloc ())return NULL ; - folio = (struct folio *)alloc_contig_frozen_pages (1 << order, gfp_mask, - nid, nodemask); + folio =page_rmappable_folio (alloc_contig_frozen_pages (1 << order, gfp_mask, + nid, nodemask)); return folio; }#else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE || !CONFIG_CONTIG_ALLOC */ @@ -1859,7 +1859,7 @@ static struct folio *alloc_buddy_frozen_folio(int order, gfp_t gfp_mask,if (alloc_try_hard) gfp_mask |= __GFP_RETRY_MAYFAIL; - folio = (struct folio *)__alloc_frozen_pages (gfp_mask, order, nid, nmask); + folio =page_rmappable_folio (__alloc_frozen_pages (gfp_mask, order, nid, nmask));/* * If we did not specify __GFP_RETRY_MAYFAIL, but still got a mm/hugetlb_cma.c +0
-0
mm/hugetlb_cma.c struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask, · line 57
Quoted 8 lines
mm/hugetlb_cma.c +1
-1@@ -51,7 +51,7 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,if (!page)return NULL ; - folio =page_folio (page); + folio =page_rmappable_folio (page);folio_set_hugetlb_cma (folio);return folio; }
IIUC, this will break the semantics of the is_transparent_hugepage() and might trigger a split of a hugetlb folio, right?
static inline bool is_transparent_hugepage(const struct folio *folio)
{
if (!folio_test_large(folio))
return false;
return is_huge_zero_folio(folio) ||
folio_test_large_rmappable(folio);
}Zi Yan
reviewerRe: [RFC PATCH 3/5] mm/hugetlb: set large_rmappable on hugetlb and avoid deferred_list handling
Quoted 55 lines · Baolin Wang
Quoted 44 lines · Zi Yan
Commit
f708f6970cc9("mm/hugetlb: fix kernel NULL pointer dereference when migrating hugetlb folio") fixed a NULL pointer dereference whenfolio_undo_large_rmappable(), nowfolio_unqueue_deferred_list(), is used on hugetlb to cleardeferred_list. It clearedlarge_rmappableflag on hugetlb. hugetlb is rmappable, thus clearinglarge_rmappableflag looks misleading. Instead, reject hugetlb infolio_unqueue_deferred_list()to avoid the issue.This prepares for code separation of compound page and folio in a follow-up commit.
Signed-off-by Zi Yan <[email protected]>--- mm/hugetlb.c | 6 +++--- mm/hugetlb_cma.c | 2 +- mm/internal.h | 3 ++- 3 files changed, 6 insertions(+), 5 deletions(-)mm/hugetlb.c +3
-1@@ -1422,8 +1422,8 @@ static struct folio *alloc_gigantic_frozen_folio(int order, gfp_t gfp_mask,if (hugetlb_cma_exclusive_alloc ())return NULL ; - folio = (struct folio *)alloc_contig_frozen_pages (1 << order, gfp_mask, - nid, nodemask); + folio =page_rmappable_folio (alloc_contig_frozen_pages (1 << order, gfp_mask, + nid, nodemask)); return folio; }#else /* !CONFIG_ARCH_HAS_GIGANTIC_PAGE || !CONFIG_CONTIG_ALLOC */ @@ -1859,7 +1859,7 @@ static struct folio *alloc_buddy_frozen_folio(int order, gfp_t gfp_mask,if (alloc_try_hard) gfp_mask |= __GFP_RETRY_MAYFAIL; - folio = (struct folio *)__alloc_frozen_pages (gfp_mask, order, nid, nmask); + folio =page_rmappable_folio (__alloc_frozen_pages (gfp_mask, order, nid, nmask));/* * If we did not specify __GFP_RETRY_MAYFAIL, but still got a mm/hugetlb_cma.c +0
-0mm/hugetlb_cma.c struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask, · line 57
Quoted 7 lines
mm/hugetlb_cma.c +1
-0@@ -51,7 +51,7 @@ struct folio *hugetlb_cma_alloc_frozen_folio(int order, gfp_t gfp_mask,if (!page)return NULL ; - folio =page_folio (page); + folio =page_rmappable_folio (page);folio_set_hugetlb_cma (folio);return folio; }IIUC, this will break the semantics of the
is_transparent_hugepage()and might trigger a split of a hugetlb folio, right?
Quoted 8 lines
static inline bool
is_transparent_hugepage(const struct folio *folio){ if (!folio_test_large(folio)) return false; return is_huge_zero_folio(folio) || folio_test_large_rmappable(folio); }
Oh, I missed this. I will check all folio_test_large_rmappable() callers and filter out hugetlb if necessary.
Thank you for pointing this out.
Best Regards,
Yan, Zi
Zi Yan
reviewerRFC PATCH 4/5 mm: only use struct page in compound_nr() and compound_order()
In reply to Zi YanA 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.
--- include/linux/mm.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
include/linux/mm.h +4 -8
@@ -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); }/**
Signature · 1 line
2.51.0
Zi Yan
reviewerRFC PATCH 5/5 mm: code separation for compound page and folio
In reply to Zi YanA compound page is not a folio. Using struct folio in prep_compound_head() causes confusion, since the input page is not a folio. The compound page to folio conversion happens in page_rmappable_folio(). So move folio code from prep_compound_head() to page_rmappable_folio().
After the change, a compound page no longer has the following folio field set:
1. folio->_nr_pages,
2. folio->_large_mapcount,
3. folio->_nr_pages_mapped,
4. folio->_mm_ids,
5. folio->_mm_id_mapcount,
6. folio->_pincount,
7. folio->_entire_mapcount,
8. folio->_deferred_list.
The page freeing path for compound pages does not need to check these fields and now just checks ->mapping == TAIL_MAPPING for all subpages. So free_tail_page_prepare() has a new large_rmappable input to distinguish between a compound page and a folio.
--- mm/hugetlb.c | 2 +- mm/internal.h | 44 ++++++++++++++++++++++++++------------------ mm/mm_init.c | 2 +- mm/page_alloc.c | 23 ++++++++++++++++++----- 4 files changed, 46 insertions(+), 25 deletions(-)
mm/hugetlb.c +1 -1
@@ -3204,7 +3204,7 @@ static void __init hugetlb_folio_init_vmemmap(struct folio *folio, ret =folio_ref_freeze (folio,1 );VM_BUG_ON (!ret);hugetlb_folio_init_tail_vmemmap (folio,1 , nr_pages); -prep_compound_head (&folio->page,huge_page_order (h)); +set_compound_order (&folio->page,huge_page_order (h)); }static bool __init hugetlb_bootmem_page_prehvo (struct huge_bootmem_page *m)
mm/internal.h +26 -18
56 lines not on this page · raw message
mm/mm_init.c +1 -1
@@ -1102,7 +1102,7 @@ static void __ref memmap_init_compound(struct page *head,prep_compound_tail (head, pfn - head_pfn);set_page_count (page,0 ); } -prep_compound_head (head, order); +set_compound_order (head, order); } void __ref memmap_init_zone_device(struct zone *zone,
mm/page_alloc.c +18 -5
@@ -746,7 +746,7 @@ void prep_compound_page(struct page *page, unsigned int order)for (i =1 ; i < nr_pages; i++)prep_compound_tail (page, i); -prep_compound_head (page, order); +set_compound_order (page, order); }static inline void set_buddy_order (struct page *page,unsigned int order)
@@ -1126,7 +1126,8 @@ static inline bool is_check_pages_enabled(void)return static_branch_unlikely (&check_pages_enabled); } -static int free_tail_page_prepare (struct page *head_page,struct page *page) +static int free_tail_page_prepare (struct page *head_page,struct page *page, +bool large_rmappable) {struct folio *folio = (struct folio *)head_page;int ret =1 ;
@@ -1141,6 +1142,13 @@ static int free_tail_page_prepare(struct page *head_page, struct page *page) ret =0 ; goto out; } +if (!large_rmappable) { +if (page->mapping !=TAIL_MAPPING ) { +bad_page (page,"corrupted mapping in compound page's tail page" ); + goto out; + } + goto skip_rmappable_checks; + }switch (page - head_page) {case 1 :/* the first tail page: these may be in place of ->mapping */
@@ -1198,11 +1206,12 @@ static int free_tail_page_prepare(struct page *head_page, struct page *page) fallthrough;default :if (page->mapping !=TAIL_MAPPING ) { -bad_page (page,"corrupted mapping in tail page" ); +bad_page (page,"corrupted mapping in folio's tail page" ); goto out; }break ; } +skip_rmappable_checks:if (unlikely (!PageTail (page))) {bad_page (page,"PageTail not set" ); goto out;
@@ -1392,17 +1401,21 @@ __always_inline bool free_pages_prepare(struct page *page,* avoid checking PageCompound for order-0 pages. */ if (unlikely (order)) { +bool large_rmappable = false;int i;if (compound) { + large_rmappable =folio_test_large_rmappable (folio); +/* clear compound order */ page[1 ].flags.f &= ~PAGE_FLAGS_SECOND ;#ifdef NR_PAGES_IN_LARGE_FOLIO - folio->_nr_pages =0 ; +if (large_rmappable) + folio->_nr_pages =0 ;#endif }for (i =1 ; i < (1 << order); i++) {if (compound) - bad +=free_tail_page_prepare (page, page + i); + bad +=free_tail_page_prepare (page, page + i, large_rmappable);if (is_check_pages_enabled ()) {if (free_page_is_bad (page + i)) { bad++;
Signature · 1 line
2.51.0
syzbot ci has tested the following series
syzbot ci has tested the following series
[v1] Separate compound page from folio
https://lore.kernel.org/all/[email protected]
* [RFC PATCH 1/5] io_uring: allocate folio in io_mem_alloc_compound() and function rename
* [RFC PATCH 2/5] mm/huge_memory: use page_rmappable_folio() to convert after-split folios
* [RFC PATCH 3/5] mm/hugetlb: set large_rmappable on hugetlb and avoid deferred_list handling
* [RFC PATCH 4/5] mm: only use struct page in compound_nr() and compound_order()
* [RFC PATCH 5/5] mm: code separation for compound page and folio
and found the following issue:
WARNING in __folio_large_mapcount_sanity_checks
Full report is available here:
https://ci.syzbot.org/series/f64f0297-d388-4cfa-b3be-f05819d0ce34
***
WARNING in __folio_large_mapcount_sanity_checks
tree: mm-new URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/akpm/mm.git base: 0241748f8b68fc2bf637f4901b9d7ca660d177ca arch: amd64 compiler: Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8 config: https://ci.syzbot.org/builds/76dc5ea6-0ff5-410b-8b1f-72e5607a704e/config C repro: https://ci.syzbot.org/findings/a308f1d6-69e2-4ebc-80a9-b51d9dc02851/c_repro syz repro: https://ci.syzbot.org/findings/a308f1d6-69e2-4ebc-80a9-b51d9dc02851/syz_repro
------------[ cut here ]------------ diff > folio_large_nr_pages(folio) WARNING: ./include/linux/rmap.h:148 at __folio_large_mapcount_sanity_checks+0x499/0x6b0 include/linux/rmap.h:148, CPU#1: syz.0.17/5988 Modules linked in: CPU: 1 UID: 0 PID: 5988 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:__folio_large_mapcount_sanity_checks+0x499/0x6b0 include/linux/rmap.h:148 Code: 5f 5d e9 4a 4e 64 09 cc e8 84 d8 aa ff 90 0f 0b 90 e9 82 fc ff ff e8 76 d8 aa ff 90 0f 0b 90 e9 8f fc ff ff e8 68 d8 aa ff 90 <0f> 0b 90 e9 b8 fc ff ff e8 5a d8 aa ff 90 0f 0b 90 e9 f2 fc ff ff RSP: 0018:ffffc900040e72f8 EFLAGS: 00010293 RAX: ffffffff8217c0f8 RBX: ffffea0006ef5c00 RCX: ffff888105fdba80 RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000000 RBP: 0000000000000001 R08: ffffea0006ef5c07 R09: 1ffffd4000ddeb80 R10: dffffc0000000000 R11: fffff94000ddeb81 R12: 0000000000000001 R13: 0000000000000000 R14: 1ffffd4000ddeb8f R15: ffffea0006ef5c78 FS: 00005555867b3500(0000) GS:ffff8882a9923000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00002000000000c0 CR3: 0000000103ab0000 CR4: 00000000000006f0 Call Trace: <TASK> folio_add_return_large_mapcount include/linux/rmap.h:184 [inline] __folio_add_rmap mm/rmap.c:1377 [inline] __folio_add_file_rmap mm/rmap.c:1696 [inline] folio_add_file_rmap_ptes+0x4c2/0xe60 mm/rmap.c:1722 insert_page_into_pte_locked+0x5ab/0x910 mm/memory.c:2378 insert_page+0x186/0x2d0 mm/memory.c:2398 packet_mmap+0x360/0x530 net/packet/af_packet.c:4622 vfs_mmap include/linux/fs.h:2053 [inline] mmap_file mm/internal.h:167 [inline] __mmap_new_file_vma mm/vma.c:2468 [inline] __mmap_new_vma mm/vma.c:2532 [inline] __mmap_region mm/vma.c:2759 [inline] mmap_region+0x18fe/0x2240 mm/vma.c:2837 do_mmap+0xc39/0x10c0 mm/mmap.c:559 vm_mmap_pgoff+0x2c9/0x4f0 mm/util.c:581 ksys_mmap_pgoff+0x51e/0x760 mm/mmap.c:605 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f5d7399acb9 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007ffe9f3eea78 EFLAGS: 00000246 ORIG_RAX: 0000000000000009 RAX: ffffffffffffffda RBX: 00007f5d73c15fa0 RCX: 00007f5d7399acb9 RDX: 0000000000000002 RSI: 0000000000030000 RDI: 0000200000000000 RBP: 00007f5d73a08bf7 R08: 0000000000000003 R09: 0000000000000000 R10: 0000000000000011 R11: 0000000000000246 R12: 0000000000000000 R13: 00007f5d73c15fac R14: 00007f5d73c15fa0 R15: 00007f5d73c15fa0 </TASK>
***
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].
Zi Yan
reviewerQuoted 80 lines · syzbot ci
syzbot ci has tested the following series
[v1] Separate compound page from folio
https://lore.kernel.org/all/[email protected]
* [RFC PATCH 1/5]io_uring: allocate folio inio_mem_alloc_compound()and function rename
* [RFC PATCH 2/5]mm/huge_memory: usepage_rmappable_folio()to convert after-split folios
* [RFC PATCH 3/5] mm/hugetlb: setlarge_rmappableon hugetlb and avoiddeferred_listhandling
* [RFC PATCH 4/5] mm: only use struct page incompound_nr()andcompound_order()
* [RFC PATCH 5/5] mm: code separation for compound page and folioand found the following issue:
WARNING in__folio_large_mapcount_sanity_checksFull report is available here:
https://ci.syzbot.org/series/f64f0297-d388-4cfa-b3be-f05819d0ce34***
WARNING in
__folio_large_mapcount_sanity_checkstree: mm-new URL: https://kernel.googlesource.com/pub/scm/linux/kernel/git/akpm/mm.git base: 0241748f8b68fc2bf637f4901b9d7ca660d177ca arch: amd64 compiler: Debian clang version 21.1.8 (++20251221033036+2078da43e25a-1~exp1~20251221153213.50), Debian LLD 21.1.8 config: https://ci.syzbot.org/builds/76dc5ea6-0ff5-410b-8b1f-72e5607a704e/config C repro: https://ci.syzbot.org/findings/a308f1d6-69e2-4ebc-80a9-b51d9dc02851/c_repro syz repro: https://ci.syzbot.org/findings/a308f1d6-69e2-4ebc-80a9-b51d9dc02851/syz_repro------------[ cut here ]------------ diff > folio_large_nr_pages(folio) WARNING: ./include/linux/rmap.h:148 at __folio_large_mapcount_sanity_checks+0x499/0x6b0 include/linux/rmap.h:148, CPU#1: syz.0.17/5988 Modules linked in: CPU: 1 UID: 0 PID: 5988 Comm: syz.0.17 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.2-debian-1.16.2-1 04/01/2014 RIP: 0010:__folio_large_mapcount_sanity_checks+0x499/0x6b0 include/linux/rmap.h:148 Code: 5f 5d e9 4a 4e 64 09 cc e8 84 d8 aa ff 90 0f 0b 90 e9 82 fc ff ff e8 76 d8 aa ff 90 0f 0b 90 e9 8f fc ff ff e8 68 d8 aa ff 90 <0f> 0b 90 e9 b8 fc ff ff e8 5a d8 aa ff 90 0f 0b 90 e9 f2 fc ff ff RSP: 0018:ffffc900040e72f8 EFLAGS: 00010293 RAX: ffffffff8217c0f8 RBX: ffffea0006ef5c00 RCX: ffff888105fdba80 RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000000 RBP: 0000000000000001 R08: ffffea0006ef5c07 R09: 1ffffd4000ddeb80 R10: dffffc0000000000 R11: fffff94000ddeb81 R12: 0000000000000001 R13: 0000000000000000 R14: 1ffffd4000ddeb8f R15: ffffea0006ef5c78 FS: 00005555867b3500(0000) GS:ffff8882a9923000(0000) knlGS:0000000000000000 CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00002000000000c0 CR3: 0000000103ab0000 CR4: 00000000000006f0 Call Trace: <TASK> folio_add_return_large_mapcount include/linux/rmap.h:184 [inline] __folio_add_rmap mm/rmap.c:1377 [inline] __folio_add_file_rmap mm/rmap.c:1696 [inline] folio_add_file_rmap_ptes+0x4c2/0xe60 mm/rmap.c:1722 insert_page_into_pte_locked+0x5ab/0x910 mm/memory.c:2378 insert_page+0x186/0x2d0 mm/memory.c:2398 packet_mmap+0x360/0x530 net/packet/af_packet.c:4622 vfs_mmap include/linux/fs.h:2053 [inline] mmap_file mm/internal.h:167 [inline] __mmap_new_file_vma mm/vma.c:2468 [inline] __mmap_new_vma mm/vma.c:2532 [inline] __mmap_region mm/vma.c:2759 [inline] mmap_region+0x18fe/0x2240 mm/vma.c:2837 do_mmap+0xc39/0x10c0 mm/mmap.c:559 vm_mmap_pgoff+0x2c9/0x4f0 mm/util.c:581 ksys_mmap_pgoff+0x51e/0x760 mm/mmap.c:605 do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline] do_syscall_64+0xe2/0xf80 arch/x86/entry/syscall_64.c:94 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7f5d7399acb9 Code: ff c3 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 44 00 00 48 89 f8 48 89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d 01 f0 ff ff 73 01 c3 48 c7 c1 e8 ff ff ff f7 d8 64 89 01 48 RSP: 002b:00007ffe9f3eea78 EFLAGS: 00000246 ORIG_RAX: 0000000000000009 RAX: ffffffffffffffda RBX: 00007f5d73c15fa0 RCX: 00007f5d7399acb9 RDX: 0000000000000002 RSI: 0000000000030000 RDI: 0000200000000000 RBP: 00007f5d73a08bf7 R08: 0000000000000003 R09: 0000000000000000 R10: 0000000000000011 R11: 0000000000000246 R12: 0000000000000000 R13: 00007f5d73c15fac R14: 00007f5d73c15fa0 R15: 00007f5d73c15fa0 </TASK>***
Quoted 7 lines
If these findings have caused you to resend the series or submit a separate fix, please add the following tag to your commit message:
Tested-by [email protected]---
This report is generated by a bot. It may contain errors. syzbot ci engineers can be reached at [email protected].
The issue comes from alloc_one_pg_vec_page() in net/packet/af_packet.c. It allocates a compound page with __GFP_COMP, but latter does vm_insert_page() in packet_mmap(), using it as a folio.
The fix below is a hack. We will need a get_free_folios() instead. I will check all __GFP_COMP callers to find out which ones are using it as a folio and which ones are using it as a compound page. I suspect most are using it as a folio.
#syz test
mm/page_alloc.c +2 -0
@@ -5311,6 +5311,8 @@ unsigned long get_free_pages_noprof(gfp_t gfp_mask, unsigned int order) page =alloc_pages_noprof (gfp_mask & ~__GFP_HIGHMEM, order);if (!page)return 0 ; +if (gfp_mask & __GFP_COMP) +return (unsigned long )folio_address (page_rmappable_folio (page));return (unsigned long )page_address (page); }EXPORT_SYMBOL (get_free_pages_noprof);
Best Regards,
Yan, Zi
syzbot ci
syzbotUnknown command
Unknown command
Quoted 36 lines · Zi Yan
Hi all,
Based on my discussion with Jason about device private folio reinitialization[1], I realize that the concepts of compound page and folio are mixed together and confusing, as people think a compound page is equal to a folio. This is not true, since a compound page means a group of pages is managed as a whole and it can be something other than a folio, for example, a slab page. To avoid further confusing people, this patchset separates compound page from folio by moving any folio related code out of compound page functions.
The code is on top of mm-new (2026-01-28-20-27) and all mm selftests passed.
The key change is that a compound page no longer sets:
1.folio->_nr_pages,
2.folio->_large_mapcount,
3.folio->_nr_pages_mapped,
4.folio->_mm_ids,
5.folio->_mm_id_mapcount,
6.folio->_pincount,
7.folio->_entire_mapcount,
8.folio->_deferred_list.Since these fields are only used by folios that are rmappable. The code setting these fields is moved to
page_rmappable_folio(). To make the code move, this patchset also needs to changes several places, where folio and compound page are used interchangably or unusual folio use:1. in
io_mem_alloc_compound(), a compound page is allocated, but later it is mapped viavm_insert_pages()like a rmappable folio;
2.__split_folio_to_order()setslarge_rmappableflag directly instead of usingpage_rmappable_folio()for after-split folios;
3. hugetlb unsetslarge_rmappableto escapedeferred_listunqueue operation.
Quoted 3 lines
At last, the page freeing path is also changed to have different checks for compound page and folio.
Thanks for doing this!
Quoted 9 lines
One thing to note is that for compound page, I do not store compound order in
folio->_nr_pages, which overlaps with page[1].memcg_data and use 1 <<compound_order()instead, since I do not want to add a new union to struct page andcompound_nr()is not as widely used asfolio_nr_pages(). But let me know if there is a performance concern for this.Comments and suggestions are welcome.
What does this mean for treating compound pages as folios, does this break code that makes any assumptions about their interop?
Quoted 24 lines
Zi Yan (5):
io_uring: allocate folio inio_mem_alloc_compound()and function
rename
mm/huge_memory: usepage_rmappable_folio()to convert after-split
folios
mm/hugetlb: setlarge_rmappableon hugetlb and avoiddeferred_list
handling
mm: only use struct page incompound_nr()andcompound_order()
mm: code separation for compound page and folioinclude/linux/mm.h | 12 ++++-------- io_uring/memmap.c | 12 ++++++------ mm/huge_memory.c | 5 ++--- mm/hugetlb.c | 8 ++++---- mm/hugetlb_cma.c | 2 +- mm/internal.h | 47 +++++++++++++++++++++++++++------------------- mm/mm_init.c | 2 +- mm/page_alloc.c | 23 ++++++++++++++++++----- 8 files changed, 64 insertions(+), 47 deletions(-)
Zi Yan
reviewerQuoted 51 lines · Balbir Singh
Quoted 36 lines · Zi Yan
Hi all,
Based on my discussion with Jason about device private folio reinitialization[1], I realize that the concepts of compound page and folio are mixed together and confusing, as people think a compound page is equal to a folio. This is not true, since a compound page means a group of pages is managed as a whole and it can be something other than a folio, for example, a slab page. To avoid further confusing people, this patchset separates compound page from folio by moving any folio related code out of compound page functions.
The code is on top of mm-new (2026-01-28-20-27) and all mm selftests passed.
The key change is that a compound page no longer sets:
1.folio->_nr_pages,
2.folio->_large_mapcount,
3.folio->_nr_pages_mapped,
4.folio->_mm_ids,
5.folio->_mm_id_mapcount,
6.folio->_pincount,
7.folio->_entire_mapcount,
8.folio->_deferred_list.Since these fields are only used by folios that are rmappable. The code setting these fields is moved to
page_rmappable_folio(). To make the code move, this patchset also needs to changes several places, where folio and compound page are used interchangably or unusual folio use:1. in
io_mem_alloc_compound(), a compound page is allocated, but later it is mapped viavm_insert_pages()like a rmappable folio;
2.__split_folio_to_order()setslarge_rmappableflag directly instead of usingpage_rmappable_folio()for after-split folios;
3. hugetlb unsetslarge_rmappableto escapedeferred_listunqueue operation.Quoted 3 lines
At last, the page freeing path is also changed to have different checks for compound page and folio.
Thanks for doing this!
Quoted 7 lines
One thing to note is that for compound page, I do not store compound order in
folio->_nr_pages, which overlaps with page[1].memcg_data and use 1 <<compound_order()instead, since I do not want to add a new union to struct page andcompound_nr()is not as widely used asfolio_nr_pages(). But let me know if there is a performance concern for this.
Quoted 5 lines
Quoted 2 lines
Comments and suggestions are welcome.
What does this mean for treating compound pages as folios, does this break code that makes any assumptions about their interop?
Yes. All folio initialization code is moved from prep_compound_page() to page_rmappable_folio(), so such users will see warnings on some folio fields are not set properly. They should call page_rmappable_folio() on compound pages they are planning to use as folios. A common use case is to vm_insert_page(s)() on subpages of a folio.
For in-tree users, I am converting them all in this series.
Quoted 15 lines
Quoted 15 lines
Zi Yan (5):
io_uring: allocate folio inio_mem_alloc_compound()and function
rename
mm/huge_memory: usepage_rmappable_folio()to convert after-split
folios
mm/hugetlb: setlarge_rmappableon hugetlb and avoiddeferred_list
handling
mm: only use struct page incompound_nr()andcompound_order()
mm: code separation for compound page and folio
Quoted 10 lines
Quoted 10 lines
include/linux/mm.h | 12 ++++-------- io_uring/memmap.c | 12 ++++++------ mm/huge_memory.c | 5 ++--- mm/hugetlb.c | 8 ++++---- mm/hugetlb_cma.c | 2 +- mm/internal.h | 47 +++++++++++++++++++++++++++------------------- mm/mm_init.c | 2 +- mm/page_alloc.c | 23 ++++++++++++++++++----- 8 files changed, 64 insertions(+), 47 deletions(-)
Best Regards,
Yan, Zi
Zi Yan
reviewerQuoted 59 lines · Zi Yan
Quoted 51 lines · Balbir Singh
Quoted 36 lines · Zi Yan
Hi all,
Based on my discussion with Jason about device private folio reinitialization[1], I realize that the concepts of compound page and folio are mixed together and confusing, as people think a compound page is equal to a folio. This is not true, since a compound page means a group of pages is managed as a whole and it can be something other than a folio, for example, a slab page. To avoid further confusing people, this patchset separates compound page from folio by moving any folio related code out of compound page functions.
The code is on top of mm-new (2026-01-28-20-27) and all mm selftests passed.
The key change is that a compound page no longer sets:
1.folio->_nr_pages,
2.folio->_large_mapcount,
3.folio->_nr_pages_mapped,
4.folio->_mm_ids,
5.folio->_mm_id_mapcount,
6.folio->_pincount,
7.folio->_entire_mapcount,
8.folio->_deferred_list.Since these fields are only used by folios that are rmappable. The code setting these fields is moved to
page_rmappable_folio(). To make the code move, this patchset also needs to changes several places, where folio and compound page are used interchangably or unusual folio use:1. in
io_mem_alloc_compound(), a compound page is allocated, but later it is mapped viavm_insert_pages()like a rmappable folio;
2.__split_folio_to_order()setslarge_rmappableflag directly instead of usingpage_rmappable_folio()for after-split folios;
3. hugetlb unsetslarge_rmappableto escapedeferred_listunqueue operation.Quoted 3 lines
At last, the page freeing path is also changed to have different checks for compound page and folio.
Thanks for doing this!
Quoted 7 lines
One thing to note is that for compound page, I do not store compound order in
folio->_nr_pages, which overlaps with page[1].memcg_data and use 1 <<compound_order()instead, since I do not want to add a new union to struct page andcompound_nr()is not as widely used asfolio_nr_pages(). But let me know if there is a performance concern for this.Quoted 5 lines
Quoted 2 lines
Comments and suggestions are welcome.
What does this mean for treating compound pages as folios, does this break code that makes any assumptions about their interop?
Quoted 7 lines
Yes. All folio initialization code is moved from
prep_compound_page()topage_rmappable_folio(), so such users will see warnings on some folio fields are not set properly. They should callpage_rmappable_folio()on compound pages they are planning to use as folios. A common use case is to vm_insert_page(s)() on subpages of a folio.For in-tree users, I am converting them all in this series.
Considering a recent report[1], where drivers/scsi/sg.c allocates compound pages with __GFP_COMP and maps them into userspace via sg_vma_fault(), I guess almost all __GFP_COMP users are really using folios instead of compound pages.
Quoted 28 lines
Quoted 16 lines
Quoted 16 lines
Zi Yan (5):
io_uring: allocate folio inio_mem_alloc_compound()and function
rename
mm/huge_memory: usepage_rmappable_folio()to convert after-split
folios
mm/hugetlb: setlarge_rmappableon hugetlb and avoiddeferred_list
handling
mm: only use struct page incompound_nr()andcompound_order()
mm: code separation for compound page and folioQuoted 10 lines
Quoted 10 lines
include/linux/mm.h | 12 ++++-------- io_uring/memmap.c | 12 ++++++------ mm/huge_memory.c | 5 ++--- mm/hugetlb.c | 8 ++++---- mm/hugetlb_cma.c | 2 +- mm/internal.h | 47 +++++++++++++++++++++++++++------------------- mm/mm_init.c | 2 +- mm/page_alloc.c | 23 ++++++++++++++++++----- 8 files changed, 64 insertions(+), 47 deletions(-)
Quoted 2 lines
Best Regards,
Yan, Zi
Best Regards,
Yan, Zi
Quoted 6 lines · Zi Yan
Quoted 1 line
For in-tree users, I am converting them all in this series.
Considering a recent report[1], where
drivers/scsi/sg.callocates compound pages with__GFP_COMPand maps them into userspace viasg_vma_fault(), I guess almost all__GFP_COMPusers are really using folios instead of compound pages.
This would be my guess, and it seems like a good cleanup to make them actually create fully proper folios if they are being mmaped.
I suspect the only places not using "folios" are frozen page users (or places yet to be converted to frozen pages)
Which is back to my previous remarks that having a good definition for what struct page memory is allowed to be used by a frozen page user would be helpful. If the mm can retain some of the tail page memory for itself you don't need to make several of the changes here.
Jason
David Hildenbrand (Arm)
maintainerQuoted 1 line · Zi Yan
Hi all,
Hi,
sorry for only going over that now.
Quoted 13 lines
Based on my discussion with Jason about device private folio reinitialization[1], I realize that the concepts of compound page and folio are mixed together and confusing, as people think a compound page is equal to a folio. This is not true, since a compound page means a group of pages is managed as a whole and it can be something other than a folio, for example, a slab page. To avoid further confusing people, this patchset separates compound page from folio by moving any folio related code out of compound page functions.
The code is on top of mm-new (2026-01-28-20-27) and all mm selftests passed.
Quoted 9 lines
The key change is that a compound page no longer sets:
1.folio->_nr_pages,
2.folio->_large_mapcount,
3.folio->_nr_pages_mapped,
4.folio->_mm_ids,
5.folio->_mm_id_mapcount,
6.folio->_pincount,
7.folio->_entire_mapcount,
8.folio->_deferred_list.
Noble goal! :)
As discussed, the issue is still that interpret non-folio page allocations as folios, which can also be compound pages.
Now, there are PFN walkers that do that, but also page table handling code.
Most prominently, when mapping such pages through vm_insert_pages(), we will call into folio_add_file_rmap_pte() and essentially touch mapcount related stuff.
Once in the page tables, users can GUP them and modify the pincount. Other page table walkers can just similarly find them and look them up.
To stop messing with mapcounts is easy once we can reliably identify such pages when mapping/unmapping them.
GUP and other page table walkers are more problematic and need more thought (and work :( ).
Essentially, vm_normal_folio() would have to fail on these pages. But what to do about vm_normal_page() users? The page_folio() would have to fail. But then we must keep some page table walkers working.
And we have to figure out what to do with GUP.
So compound pages are just the tip of the iceberg :)
We could maybe forbid mapping them through vm_insert_pages() in the first place, requiring all callers to do order-0 page allocations. Hm.
Then at least they would not end up in user page tables.
But there is other code where compound pages are interpreted as folios and the other way around that must be sorted out.
Signature · 3 lines
Cheers, David
Zi Yan
reviewerQuoted 41 lines · David Hildenbrand (Arm)
Quoted 1 line · Zi Yan
Hi all,
Hi,
sorry for only going over that now.
Quoted 13 lines
Based on my discussion with Jason about device private folio reinitialization[1], I realize that the concepts of compound page and folio are mixed together and confusing, as people think a compound page is equal to a folio. This is not true, since a compound page means a group of pages is managed as a whole and it can be something other than a folio, for example, a slab page. To avoid further confusing people, this patchset separates compound page from folio by moving any folio related code out of compound page functions.
The code is on top of mm-new (2026-01-28-20-27) and all mm selftests passed.
Quoted 9 lines
The key change is that a compound page no longer sets:
1.folio->_nr_pages,
2.folio->_large_mapcount,
3.folio->_nr_pages_mapped,
4.folio->_mm_ids,
5.folio->_mm_id_mapcount,
6.folio->_pincount,
7.folio->_entire_mapcount,
8.folio->_deferred_list.Noble goal! :)
As discussed, the issue is still that interpret non-folio page allocations as folios, which can also be compound pages.
Now, there are PFN walkers that do that, but also page table handling code.
Most prominently, when mapping such pages through
vm_insert_pages(), we will call intofolio_add_file_rmap_pte()and essentially touch mapcount related stuff.
Quoted 5 lines
Once in the page tables, users can GUP them and modify the pincount. Other page table walkers can just similarly find them and look them up.
To stop messing with mapcounts is easy once we can reliably identify such pages when mapping/unmapping them.
My current way of doing that is to mark every page “NotRmappable” page_type in post_alloc_hook() and clear this page_type at page_rmappable_folio(). Any user wants to set their own page_type can overwrite “NotRmappable”. And folio_test_rmappable() is just !folio_has_type(). One exception is hugetlb, since it has page_type and is rmappable. Fortunately or unfortunately, rmap.c has special handling code for hugetlb, so there should be no problem.
I did some test using io_uring (via nvim), which uses compound page instead of folio and does vm_insert*(). At least no crash was present.
Quoted 9 lines
GUP and other page table walkers are more problematic and need more thought (and work :( ).
Essentially,
vm_normal_folio()would have to fail on these pages. But what to do aboutvm_normal_page()users? Thepage_folio()would have to fail. But then we must keep some page table walkers working.And we have to figure out what to do with GUP.
Since _pincount will not be present after my change, GUP cannot be applied on these pages.
OK, my memory comes back. I think my original proposal of separating compound page from folio might not be right, since that defeats the purpose of folio, which is a group of pages managed as a whole.
Basically a compound page should still be regarded as a folio, but rmappable related fields (e.g., _large_mapcount, _nr_pages_mapped, _mm_ids) should not be initialized and user is free to use them differently. In this way, _pincount can be a common folio field to initialize and use.
Quoted 8 lines
So compound pages are just the tip of the iceberg :)
We could maybe forbid mapping them through
vm_insert_pages()in the first place, requiring all callers to do order-0 page allocations. Hm.Then at least they would not end up in user page tables.
Will it kill performance? If only order-0 pages are allowed.
Quoted 3 lines
But there is other code where compound pages are interpreted as folios and the other way around that must be sorted out.
I think we might want to have some sub-class of folios, like rmappable folios, not rmappable folios, and others, otherwise, we are going back
to mixing page and folio.
Best Regards,
Yan, Zi
David Hildenbrand (Arm)
maintainerSorry for the late reply on this.
Quoted 23 lines
Quoted 9 lines
As discussed, the issue is still that interpret non-folio page allocations as folios, which can also be compound pages.
Now, there are PFN walkers that do that, but also page table handling code.
Most prominently, when mapping such pages through
vm_insert_pages(), we will call intofolio_add_file_rmap_pte()and essentially touch mapcount related stuff.Quoted 5 lines
Once in the page tables, users can GUP them and modify the pincount. Other page table walkers can just similarly find them and look them up.
To stop messing with mapcounts is easy once we can reliably identify such pages when mapping/unmapping them.
My current way of doing that is to mark every page “NotRmappable”
page_typeinpost_alloc_hook()and clear thispage_typeatpage_rmappable_folio(). Any user wants to set their ownpage_typecan overwrite “NotRmappable”. Andfolio_test_rmappable()is just !folio_has_type(). One exception is hugetlb, since it haspage_typeand is rmappable. Fortunately or unfortunately,rmap.chas special handling code for hugetlb, so there should be no problem.
Quoted 3 lines
I did some test using
io_uring(via nvim), which uses compound page instead of folio and does vm_insert*(). At least no crash was present.
We discussed that in the meantime elsewhere. :)
Quoted 13 lines
Quoted 9 lines
GUP and other page table walkers are more problematic and need more thought (and work :( ).
Essentially,
vm_normal_folio()would have to fail on these pages. But what to do aboutvm_normal_page()users? Thepage_folio()would have to fail. But then we must keep some page table walkers working.And we have to figure out what to do with GUP.
Since
_pincountwill not be present after my change, GUP cannot be applied on these pages.
Quoted 8 lines
OK, my memory comes back. I think my original proposal of separating compound page from folio might not be right, since that defeats the purpose of folio, which is a group of pages managed as a whole.
Basically a compound page should still be regarded as a folio, but rmappable related fields (e.g.,
_large_mapcount,_nr_pages_mapped,_mm_ids) should not be initialized and user is free to use them differently. In this way,_pincountcan be a common folio field to initialize and use.
At least long term a non-folio page should not be regarded a folio.
There are quite some changes required to teach page table walkers (incl. GUP) about that.
In GUP code, we would not mess with the _pincount for non-folio things. It will be a bit tricky.
Handling the rmap (skip it) as a first step might be easier.
Quoted 11 lines
Quoted 9 lines
So compound pages are just the tip of the iceberg :)
We could maybe forbid mapping them through
vm_insert_pages()in the first place, requiring all callers to do order-0 page allocations. Hm.Then at least they would not end up in user page tables.
Will it kill performance? If only order-0 pages are allowed.
I don't think this is really performance-relevant for the vm_insert_pages() interface. We never get PMD-THPs either way.
But yea, if we could keep that compound pages working that would also make our life easier.
Quoted 8 lines
Quoted 4 lines
But there is other code where compound pages are interpreted as folios and the other way around that must be sorted out.
I think we might want to have some sub-class of folios, like rmappable folios, not rmappable folios, and others, otherwise, we are going back
to mixing page and folio.
I think it's pretty clear that a folio will be mappable.
If you want something that is not mappable, then you shouldn't be allcoating a folio that carries metadata for storing all that information.
Signature · 3 lines
Cheers, David