mm/huge_memory: add folio_split_driver_managed()
Subsystems (6)
Matthew Brost
maintainerPATCH 1/3 mm/huge_memory: add folio_split_driver_managed()
Add a lightweight structural split primitive for large (compound) folios that a driver allocated with __GFP_COMP and manages entirely by itself, outside of the core mm's view.
The existing split paths - split_folio() and folio_split_unmapped() - are built for folios that the mm owns: they perform a refcount freeze, walk and remap the rmap, and take the anon_vma / i_mmap locks, and folio_split_unmapped() further assumes an anon, pagecache-style refcount model (nr_pages + 1). None of that applies to a folio that is:
- singly referenced (the caller holds the only reference),
- not mapped through the rmap (folio_mapped() == 0),
- not in the page cache or swap cache (folio->mapping == NULL),
- not on any LRU or the deferred-split list.
For such a folio the split is purely structural: because nothing else in the kernel can reach it, there is no need to freeze the refcount or touch any mapping. folio_split_driver_managed() therefore performs only the compound and split-accounting teardown via __split_unmapped_folio() and then hands each resulting order-@new_order folio its own reference, mirroring split_page() for compound folios. The caller keeps the original reference on the first resulting folio and is responsible for freeing all of them individually.
The immediate user is TTM's GPU page pool, which allocates higher-order compound pages, maps them into userspace via VM_PFNMAP (never through the rmap), and needs to split them into order-0 folios under memory pressure so pages can be backed up to shmem and freed one at a time.
A CONFIG_TRANSPARENT_HUGEPAGE=n stub is provided so callers can build without the split machinery; it warns and returns -EINVAL.
---
The patch is based on drm-tip rather than the core MM branches to facilitate Intel CI testing and initial review. It can be rebased onto the core MM branches in a subsequent revision.
--- include/linux/huge_mm.h | 8 ++++++ mm/huge_memory.c | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+)
include/linux/huge_mm.h +8 -0
@@ -402,6 +402,7 @@ enum split_type {int __split_huge_page_to_list_to_order (struct page *page,struct list_head *list,unsigned int new_order);int folio_split_unmapped (struct folio *folio,unsigned int new_order); +int folio_split_driver_managed (struct folio *folio,unsigned int new_order);unsigned int min_order_for_split (struct folio *folio);int split_folio_to_list (struct folio *folio,struct list_head *list); int folio_check_splittable(struct folio *folio, unsigned int new_order,
@@ -656,6 +657,13 @@ static inline int split_folio_to_list(struct folio *folio, struct list_head *lisreturn -EINVAL ; } +static inline int folio_split_driver_managed (struct folio *folio, +unsigned int new_order) +{ +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; +} +static inline int folio_split (struct folio *folio,unsigned int new_order,struct page *page,struct list_head *list) {
mm/huge_memory.c +63 -0
69 lines not on this page · raw message
Signature · 1 line
2.34.1
Matthew Brost
maintainerPATCH 2/3 drm/xe: select TRANSPARENT_HUGEPAGE
Xe is currently the only TTM driver that drives the TTM shrinker, and hence the only user of TTM's incremental swap path. That path splits the pool's higher-order compound folios one page at a time via folio_split_driver_managed() so individual pages can be backed up to shmem and freed under memory pressure.
folio_split_driver_managed() is part of the transparent hugepage split machinery in mm/huge_memory.c, which is only built when CONFIG_TRANSPARENT_HUGEPAGE is enabled. Select it from DRM_XE so the split primitive is always available for the driver that needs it, rather than forcing it on every TTM user.
--- drivers/gpu/drm/xe/Kconfig | 6 ++++++ 1 file changed, 6 insertions(+)
drivers/gpu/drm/xe/Kconfig +6 -0
@@ -11,6 +11,12 @@ config DRM_XE # the shmem_readpage() which depends upon tmpfs select SHMEM select TMPFS + # Xe is the only TTM driver that exercises the TTM shrinker's + # incremental swap path, which splits the pool's compound folios one + # page at a time via folio_split_driver_managed(). That helper lives + # in the THP split machinery, so pull it in here rather than for all + # TTM users. + select TRANSPARENT_HUGEPAGE select DRM_BUDDY select DRM_CLIENT_SELECTION select DRM_KMS_HELPER
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 2/3] drm/xe: select TRANSPARENT_HUGEPAGE
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Low] DRM_XE unconditionally selects TRANSPARENT_HUGEPAGE without inheriting its architecture and…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Low] DRM_XE unconditionally selects TRANSPARENT_HUGEPAGE without inheriting its architecture and PREEMPT_RT dependencies. --
--- Patch [2]: [PATCH 2/3] drm/xe: select TRANSPARENT_HUGEPAGE --- commit 039323cec9a41618d3b6161e1831b08e7ba4069a
Author: Matthew Brost <[email protected]>
drm/xe: select TRANSPARENT_HUGEPAGE
This commit selects TRANSPARENT_HUGEPAGE for the DRM_XE driver to ensure the THP split machinery is available for Xe's TTM shrinker incremental swap path.
Quoted 5 lines
drivers/gpu/drm/xe/Kconfig +0
-0
drivers/gpu/drm/xe/Kconfig config DRM_XE · line 19
Quoted 9 lines
drivers/gpu/drm/xe/Kconfig +6
-0@@ -11,6 +11,12 @@ config DRM_XE # the shmem_readpage() which depends upon tmpfs select SHMEM select TMPFS + # Xe is the only TTM driver that exercises the TTM shrinker's + # incremental swap path, which splits the pool's compound folios one + # page at a time via folio_split_driver_managed(). That helper lives + # in the THP split machinery, so pull it in here rather than for all + # TTM users. + select TRANSPARENT_HUGEPAGE
[Severity: Low]
Does this select statement need to inherit the dependencies for TRANSPARENT_HUGEPAGE?
TRANSPARENT_HUGEPAGE has specific architecture dependencies (like HAVE_ARCH_TRANSPARENT_HUGEPAGE) and can conflict with PREEMPT_RT. Selecting a config symbol without inheriting its dependencies can cause Kconfig unmet dependency warnings and potential build failures on configurations where those dependencies are not met (for example, on non-x86 architectures when building with COMPILE_TEST).
Should this use a conditional selection (e.g., select TRANSPARENT_HUGEPAGE if HAVE_ARCH_TRANSPARENT_HUGEPAGE) or be changed to a "depends on" to ensure it is only pulled in when its requirements are satisfied?
Quoted 3 lines
drivers/gpu/drm/xe/Kconfig +0
-0select DRM_BUDDY select DRM_CLIENT_SELECTION select DRM_KMS_HELPER
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Matthew Brost
maintainerPATCH 3/3 drm/ttm: allocate pool pages as compound (__GFP_COMP)
In reply to Matthew BrostHistorically ttm_pool_alloc_page() deliberately avoided __GFP_COMP for higher-order allocations and instead stashed the allocation order in page->private. The stated reason was that mapping a TTM page into a userspace process and having that process call put_page() on it would be illegal on a compound page, because the stray reference would fold into the compound head and could free the whole block behind TTM's back.
That hazard no longer applies to the non-DMA path. TTM faults its pages into userspace via VM_PFNMAP (see ttm_bo_vm_fault_reserved() / vmf_insert_pfn_prot()), so the core mm never takes a struct page reference on them and GUP rejects the range; a stray userspace put_page() cannot reach these pages at all.
Convert the !ttm_pool_uses_dma_alloc() path to allocate compound pages with __GFP_COMP and recover the order via folio_order() instead of page->private:
- ttm_pool_alloc_page(): add __GFP_COMP, drop the page->private write.
- ttm_pool_page_order() / ttm_pool_unmap_and_free(): read the order from folio_order() for the non-DMA case.
- ttm_pool_split_for_swap(): for compound folios, split via the new folio_split_driver_managed() helper rather than split_page(), which rejects compound pages. Each resulting order-0 folio is then freed individually as it is backed up.
- Drop the now-dead page->private = 0 clears on the purge and full-backup free paths.
The DMA path is intentionally left unchanged: dma_alloc_attrs() does not produce compound pages, so it keeps split_page() and the page->private order stash.
folio_split_driver_managed() lives in the THP split machinery (mm/huge_memory.c), which only builds when CONFIG_TRANSPARENT_HUGEPAGE is enabled. Drivers that drive the TTM shrinker and therefore reach the split path must select TRANSPARENT_HUGEPAGE.
--- drivers/gpu/drm/ttm/tests/ttm_pool_test.c | 23 +++++++++--- drivers/gpu/drm/ttm/ttm_backup.c | 8 ++-- drivers/gpu/drm/ttm/ttm_pool.c | 46 +++++++++++++++-------- 3 files changed, 53 insertions(+), 24 deletions(-)
drivers/gpu/drm/ttm/tests/ttm_pool_test.c +18 -5
@@ -169,7 +169,14 @@ static void ttm_pool_alloc_basic(struct kunit *test)KUNIT_ASSERT_NOT_NULL (test, (void *)fst_page->private);KUNIT_ASSERT_NOT_NULL (test, (void *)last_page->private); }else { -KUNIT_ASSERT_EQ (test, fst_page->private, params->order); +/* +* The non-DMA path allocates compound pages, so the +* order is recovered from the folio rather than from +* page->private. +*/ +KUNIT_ASSERT_EQ (test, +folio_order (page_folio (fst_page)), + params->order); } }else {if (ttm_pool_uses_dma_alloc (pool)) {
@@ -177,13 +184,19 @@ static void ttm_pool_alloc_basic(struct kunit *test)KUNIT_ASSERT_NULL (test, (void *)last_page->private); }else {/* -* We expect to alloc one big block, followed by -* order 0 blocks +* We expect to alloc one or more max-order compound +* blocks. page_folio() on any subpage resolves to the +* compound head, so both the first and last pages +* report the max block order. */ -KUNIT_ASSERT_EQ (test, fst_page->private, +KUNIT_ASSERT_EQ (test, +folio_order (page_folio (fst_page)), + min_t(unsigned int, MAX_PAGE_ORDER, + params->order)); +KUNIT_ASSERT_EQ (test, +folio_order (page_folio (last_page)), min_t(unsigned int, MAX_PAGE_ORDER, params->order)); -KUNIT_ASSERT_EQ (test, last_page->private,0 ); } }
drivers/gpu/drm/ttm/ttm_backup.c +5 -3
@@ -72,9 +72,11 @@ int ttm_backup_copy_page(struct file *backup, struct page *dst,* ttm_backup_backup_folio() - Backup a folio * @backup: The struct backup pointer to use. * @folio: The folio to back up. -* @order: The allocation order of @folio. Since TTM allocates higher-order -* pages without __GFP_COMP, folio_nr_pages(@folio) would always -* return 1; the caller must pass the true order explicitly. +* @order: The allocation order of @folio, passed explicitly. For the DMA +* path TTM allocates higher-order pages without __GFP_COMP, so +* folio_order(@folio) would return 0 rather than the true order; +* the caller therefore passes the order explicitly. (The non-DMA +* path allocates compound pages, for which the two agree.) * @writeback: Whether to perform immediate writeback of the folio's pages. * This may have performance implications. * @idx: A unique integer for the first page of the folio and each struct backup.
drivers/gpu/drm/ttm/ttm_pool.c +30 -16
93 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 3/3] drm/ttm: allocate pool pages as compound (__GFP_COMP)
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [Critical] ttm_pool_split_for_swap() ignores the return value of folio_split_driver_managed(), leading to…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [Critical] ttm_pool_split_for_swap() ignores the return value of folio_split_driver_managed(), leading to severe buddy allocator corruption if the split fails.
- [High] Kconfig hazard: Forcing TRANSPARENT_HUGEPAGE via select bypasses critical architectural dependencies.
--
--- Patch [3]: [PATCH 3/3] drm/ttm: allocate pool pages as compound (__GFP_COMP) --- commit dae665417eeff981f7bae0e75da08ad47a4dd0a0
Author: Matthew Brost <[email protected]>
drm/ttm: allocate pool pages as compound (__GFP_COMP)
This commit changes the non-DMA path in ttm_pool_alloc_page() to allocate compound pages with __GFP_COMP instead of stashing the order in page->private. It updates the backup, restore, and free paths to read the order via folio_order() and split compound folios using folio_split_driver_managed().
Quoted 4 lines
folio_split_driver_managed()lives in the THP split machinery (mm/huge_memory.c), which only builds whenCONFIG_TRANSPARENT_HUGEPAGEis enabled. Drivers that drive the TTM shrinker and therefore reach the split path must selectTRANSPARENT_HUGEPAGE.
[Severity: High]
Is it safe to force select TRANSPARENT_HUGEPAGE in drivers?
The Kconfig select directive forcefully enables the target symbol without evaluating its dependencies. TRANSPARENT_HUGEPAGE strictly depends on HAVE_ARCH_TRANSPARENT_HUGEPAGE and !PREEMPT_RT.
Using select could force THP on architectures that lack support, breaking the build, or bypass the !PREEMPT_RT dependency and violate strict latency requirements.
Quoted 4 lines
drivers/gpu/drm/ttm/ttm_pool.c +0
-0
[ ... ]
Quoted 24 lines
drivers/gpu/drm/ttm/ttm_pool.c +12
-4@@ -493,15 +493,31 @@ static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)static void ttm_pool_split_for_swap (struct ttm_pool *pool,struct page *p) {unsigned int order =ttm_pool_page_order (pool, p);if (!order)return ; -split_page (p, order); - nr =1UL << order; -while (nr--) - (p++)->private =0 ; +if (ttm_pool_uses_dma_alloc (pool)) { +pgoff_t nr; + +/* +* DMA-alloc pages are not compound; split the plain +* higher-order allocation and clear the per-page private +* (which held the order for the non-compound case). +*/ +split_page (p, order); + nr =1UL << order; +while (nr--) + (p++)->private =0 ;
drivers/gpu/drm/ttm/ttm_pool.c static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p) · line 521
Quoted 10 lines
drivers/gpu/drm/ttm/ttm_pool.c +9
-0+return ; + } + +/* +* The non-DMA path allocates compound folios (__GFP_COMP). Split the +* driver-owned, off-LRU, unmapped folio into order-0 folios so each +* page can be freed as soon as it has been backed up. +*/ +folio_split_driver_managed (page_folio (p),0 ); }
[Severity: Critical]
Does this code handle split failures correctly?
If CONFIG_TRANSPARENT_HUGEPAGE is disabled, the fallback stub for folio_split_driver_managed() unconditionally returns -EINVAL and the folio remains an intact compound page. Because the return value is ignored, callers like ttm_pool_backup() will incorrectly assume the split succeeded.
Will this corrupt the buddy allocator in ttm_pool_backup()? It follows the split with this loop:
ttm_pool_split_for_swap(pool, page); for (j = 0; j < nr_backed; j++) __free_pages_gpu_account(page + j, 0, false);
Calling __free_pages(page + j, 0) on a tail page of an active compound folio redirects the refcount decrement to the head page, rapidly underflowing it.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH 3/3] drm/ttm: allocate pool pages as compound (__GFP_COMP)
In reply to Matthew BrostQuoted 7 lines · Matthew Brost
Historically
ttm_pool_alloc_page()deliberately avoided__GFP_COMPfor higher-order allocations and instead stashed the allocation order inpage->private. The stated reason was that mapping a TTM page into a userspace process and having that process callput_page()on it would be illegal on a compound page, because the stray reference would fold into the compound head and could free the whole block behind TTM's back.
Quoted 5 lines
That hazard no longer applies to the non-DMA path. TTM faults its pages into userspace via
VM_PFNMAP(seettm_bo_vm_fault_reserved()/vmf_insert_pfn_prot()), so the core mm never takes a struct page reference on them and GUP rejects the range; a stray userspaceput_page()cannot reach these pages at all.
Yeah unfortunately I clearly have to reject that.
This is exactly what we have thought before but that doesn't hold true. Especially KVM completely broke our neck here.
But let's continue the discussion on the other thread.
Regards,
Christian.
Quoted 225 lines
Convert the !ttm_pool_uses_dma_alloc() path to allocate compound pages with
__GFP_COMPand recover the order viafolio_order()instead ofpage->private:-
ttm_pool_alloc_page(): add__GFP_COMP, drop thepage->privatewrite.
-ttm_pool_page_order()/ttm_pool_unmap_and_free(): read the order fromfolio_order()for the non-DMA case.
-ttm_pool_split_for_swap(): for compound folios, split via the newfolio_split_driver_managed()helper rather thansplit_page(), which rejects compound pages. Each resulting order-0 folio is then freed individually as it is backed up.
- Drop the now-deadpage->private= 0 clears on the purge and full-backup free paths.The DMA path is intentionally left unchanged:
dma_alloc_attrs()does not produce compound pages, so it keepssplit_page()and thepage->privateorder stash.
folio_split_driver_managed()lives in the THP split machinery (mm/huge_memory.c), which only builds whenCONFIG_TRANSPARENT_HUGEPAGEis enabled. Drivers that drive the TTM shrinker and therefore reach the split path must selectTRANSPARENT_HUGEPAGE.Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Christian Koenig <[email protected]>Cc Huang Rui <[email protected]>Cc Matthew Auld <[email protected]>Cc Matthew Brost <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc Nico Pache <[email protected]>Cc Ryan Roberts <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Lance Yang <[email protected]>Cc Tvrtko Ursulin <[email protected]>Cc Dave Airlie <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Matthew Brost <[email protected]>Assisted-by GitHub-Copilot:claude-opus-4.8--- drivers/gpu/drm/ttm/tests/ttm_pool_test.c | 23 +++++++++--- drivers/gpu/drm/ttm/ttm_backup.c | 8 ++-- drivers/gpu/drm/ttm/ttm_pool.c | 46 +++++++++++++++-------- 3 files changed, 53 insertions(+), 24 deletions(-)drivers/gpu/drm/ttm/tests/ttm_pool_test.c +18
-5@@ -169,7 +169,14 @@ static void ttm_pool_alloc_basic(struct kunit *test)KUNIT_ASSERT_NOT_NULL (test, (void *)fst_page->private);KUNIT_ASSERT_NOT_NULL (test, (void *)last_page->private); }else { -KUNIT_ASSERT_EQ (test, fst_page->private, params->order); +/* +* The non-DMA path allocates compound pages, so the +* order is recovered from the folio rather than from +* page->private. +*/ +KUNIT_ASSERT_EQ (test, +folio_order (page_folio (fst_page)), + params->order); } }else {if (ttm_pool_uses_dma_alloc (pool)) {@@ -177,13 +184,19 @@ static void ttm_pool_alloc_basic(struct kunit *test)KUNIT_ASSERT_NULL (test, (void *)last_page->private); }else {/* -* We expect to alloc one big block, followed by -* order 0 blocks +* We expect to alloc one or more max-order compound +* blocks. page_folio() on any subpage resolves to the +* compound head, so both the first and last pages +* report the max block order. */ -KUNIT_ASSERT_EQ (test, fst_page->private, +KUNIT_ASSERT_EQ (test, +folio_order (page_folio (fst_page)), + min_t(unsigned int, MAX_PAGE_ORDER, + params->order)); +KUNIT_ASSERT_EQ (test, +folio_order (page_folio (last_page)), min_t(unsigned int, MAX_PAGE_ORDER, params->order)); -KUNIT_ASSERT_EQ (test, last_page->private,0 ); } }drivers/gpu/drm/ttm/ttm_backup.c +5
-3@@ -72,9 +72,11 @@ int ttm_backup_copy_page(struct file *backup, struct page *dst,* ttm_backup_backup_folio() - Backup a folio * @backup: The struct backup pointer to use. * @folio: The folio to back up. -* @order: The allocation order of @folio. Since TTM allocates higher-order -* pages without __GFP_COMP, folio_nr_pages(@folio) would always -* return 1; the caller must pass the true order explicitly. +* @order: The allocation order of @folio, passed explicitly. For the DMA +* path TTM allocates higher-order pages without __GFP_COMP, so +* folio_order(@folio) would return 0 rather than the true order; +* the caller therefore passes the order explicitly. (The non-DMA +* path allocates compound pages, for which the two agree.) * @writeback: Whether to perform immediate writeback of the folio's pages. * This may have performance implications. * @idx: A unique integer for the first page of the folio and each struct backup. drivers/gpu/drm/ttm/ttm_pool.c +30
-16@@ -168,9 +168,11 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,struct page *p;void *vaddr; -/* Don't set the __GFP_COMP flag for higher order allocations. -* Mapping pages directly into an userspace process and calling -* put_page() on a TTM allocated page is illegal. +/* +* For higher-order allocations be a good citizen: don't dip into +* memory reserves, don't retry hard, don't warn on failure and stay +* on the local node. The non-DMA path additionally sets __GFP_COMP +* below; the DMA path allocates via dma_alloc_attrs(). */ if (order) gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN |@@ -189,11 +191,9 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags, }if (!ttm_pool_uses_dma_alloc (pool)) { - p =alloc_pages_node (pool->nid, gfp_flags, order); -if (p) { - p->private = order; + p =alloc_pages_node (pool->nid, gfp_flags | __GFP_COMP, order); +if (p)mod_lruvec_page_state (p,NR_GPU_ACTIVE ,1 << order); - }return p; }@@ -482,7 +482,7 @@ static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)return dma->vaddr & ~PAGE_MASK ; } -return p->private; +return folio_order (page_folio (p)); }/* @@ -493,15 +493,31 @@ static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)static void ttm_pool_split_for_swap (struct ttm_pool *pool,struct page *p) {unsigned int order =ttm_pool_page_order (pool, p); -pgoff_t nr;if (!order)return ; -split_page (p, order); - nr =1UL << order; -while (nr--) - (p++)->private =0 ; +if (ttm_pool_uses_dma_alloc (pool)) { +pgoff_t nr; + +/* +* DMA-alloc pages are not compound; split the plain +* higher-order allocation and clear the per-page private +* (which held the order for the non-compound case). +*/ +split_page (p, order); + nr =1UL << order; +while (nr--) + (p++)->private =0 ; +return ; + } + +/* +* The non-DMA path allocates compound folios (__GFP_COMP). Split the +* driver-owned, off-LRU, unmapped folio into order-0 folios so each +* page can be freed as soon as it has been backed up. +*/ +folio_split_driver_managed (page_folio (p),0 ); }/** @@ -548,7 +564,7 @@ static pgoff_t ttm_pool_unmap_and_free(struct ttm_pool *pool, struct page *page, pt =ttm_pool_select_type (pool, caching, order); }else { - order = page->private; + order =folio_order (page_folio (page)); nr = (1UL << order); }@@ -1124,7 +1140,6 @@ long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt, num_pages);if (flags->purge) { shrunken += num_pages; - page->private =0 ;__free_pages_gpu_account (page, order, false);memset (tt->pages + i,0 , num_pages *sizeof (*tt->pages));@@ -1214,7 +1229,6 @@ long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt, }/* Fully backed up: free at native order. */ - page->private =0 ;__free_pages_gpu_account (page, order, false); }
Matthew Brost
maintainerRe: [PATCH 3/3] drm/ttm: allocate pool pages as compound (__GFP_COMP)
Quoted 14 lines · Christian König
Quoted 7 lines · Matthew Brost
Historically
ttm_pool_alloc_page()deliberately avoided__GFP_COMPfor higher-order allocations and instead stashed the allocation order inpage->private. The stated reason was that mapping a TTM page into a userspace process and having that process callput_page()on it would be illegal on a compound page, because the stray reference would fold into the compound head and could free the whole block behind TTM's back.Quoted 5 lines
That hazard no longer applies to the non-DMA path. TTM faults its pages into userspace via
VM_PFNMAP(seettm_bo_vm_fault_reserved()/vmf_insert_pfn_prot()), so the core mm never takes a struct page reference on them and GUP rejects the range; a stray userspaceput_page()cannot reach these pages at all.
Quoted 4 lines
Yeah unfortunately I clearly have to reject that.
This is exactly what we have thought before but that doesn't hold true. Especially KVM completely broke our neck here.
I haven't tried KVM, but it's entirely possible that KVM or another part of the kernel breaks TTM usage here.
It didn't take me long to put this together after it was brought up in the other thread. It makes TTM a bit clearer and also simplifies my defrag series quite a bit, since that series included several extra steps because there's currently no reliable way to determine whether a page is the head page without native compound-page support or reinventing this in TTM.
Xe appeared to work correctly with these changes, which is why I decided to post them.
Quoted 2 lines
But let's continue the discussion on the other thread.
+1, I saw your response there. Hopefully we can figure it out, as I'd very much consider this a nice-to-have cleanup.
Matt
Quoted 230 lines
Regards,
Christian.Quoted 225 lines
Convert the !ttm_pool_uses_dma_alloc() path to allocate compound pages with
__GFP_COMPand recover the order viafolio_order()instead ofpage->private:-
ttm_pool_alloc_page(): add__GFP_COMP, drop thepage->privatewrite.
-ttm_pool_page_order()/ttm_pool_unmap_and_free(): read the order fromfolio_order()for the non-DMA case.
-ttm_pool_split_for_swap(): for compound folios, split via the newfolio_split_driver_managed()helper rather thansplit_page(), which rejects compound pages. Each resulting order-0 folio is then freed individually as it is backed up.
- Drop the now-deadpage->private= 0 clears on the purge and full-backup free paths.The DMA path is intentionally left unchanged:
dma_alloc_attrs()does not produce compound pages, so it keepssplit_page()and thepage->privateorder stash.
folio_split_driver_managed()lives in the THP split machinery (mm/huge_memory.c), which only builds whenCONFIG_TRANSPARENT_HUGEPAGEis enabled. Drivers that drive the TTM shrinker and therefore reach the split path must selectTRANSPARENT_HUGEPAGE.Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Christian Koenig <[email protected]>Cc Huang Rui <[email protected]>Cc Matthew Auld <[email protected]>Cc Matthew Brost <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc Nico Pache <[email protected]>Cc Ryan Roberts <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Lance Yang <[email protected]>Cc Tvrtko Ursulin <[email protected]>Cc Dave Airlie <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Matthew Brost <[email protected]>Assisted-by GitHub-Copilot:claude-opus-4.8--- drivers/gpu/drm/ttm/tests/ttm_pool_test.c | 23 +++++++++--- drivers/gpu/drm/ttm/ttm_backup.c | 8 ++-- drivers/gpu/drm/ttm/ttm_pool.c | 46 +++++++++++++++-------- 3 files changed, 53 insertions(+), 24 deletions(-)drivers/gpu/drm/ttm/tests/ttm_pool_test.c +18
-5@@ -169,7 +169,14 @@ static void ttm_pool_alloc_basic(struct kunit *test)KUNIT_ASSERT_NOT_NULL (test, (void *)fst_page->private);KUNIT_ASSERT_NOT_NULL (test, (void *)last_page->private); }else { -KUNIT_ASSERT_EQ (test, fst_page->private, params->order); +/* +* The non-DMA path allocates compound pages, so the +* order is recovered from the folio rather than from +* page->private. +*/ +KUNIT_ASSERT_EQ (test, +folio_order (page_folio (fst_page)), + params->order); } }else {if (ttm_pool_uses_dma_alloc (pool)) {@@ -177,13 +184,19 @@ static void ttm_pool_alloc_basic(struct kunit *test)KUNIT_ASSERT_NULL (test, (void *)last_page->private); }else {/* -* We expect to alloc one big block, followed by -* order 0 blocks +* We expect to alloc one or more max-order compound +* blocks. page_folio() on any subpage resolves to the +* compound head, so both the first and last pages +* report the max block order. */ -KUNIT_ASSERT_EQ (test, fst_page->private, +KUNIT_ASSERT_EQ (test, +folio_order (page_folio (fst_page)), + min_t(unsigned int, MAX_PAGE_ORDER, + params->order)); +KUNIT_ASSERT_EQ (test, +folio_order (page_folio (last_page)), min_t(unsigned int, MAX_PAGE_ORDER, params->order)); -KUNIT_ASSERT_EQ (test, last_page->private,0 ); } }drivers/gpu/drm/ttm/ttm_backup.c +5
-3@@ -72,9 +72,11 @@ int ttm_backup_copy_page(struct file *backup, struct page *dst,* ttm_backup_backup_folio() - Backup a folio * @backup: The struct backup pointer to use. * @folio: The folio to back up. -* @order: The allocation order of @folio. Since TTM allocates higher-order -* pages without __GFP_COMP, folio_nr_pages(@folio) would always -* return 1; the caller must pass the true order explicitly. +* @order: The allocation order of @folio, passed explicitly. For the DMA +* path TTM allocates higher-order pages without __GFP_COMP, so +* folio_order(@folio) would return 0 rather than the true order; +* the caller therefore passes the order explicitly. (The non-DMA +* path allocates compound pages, for which the two agree.) * @writeback: Whether to perform immediate writeback of the folio's pages. * This may have performance implications. * @idx: A unique integer for the first page of the folio and each struct backup. drivers/gpu/drm/ttm/ttm_pool.c +30
-16@@ -168,9 +168,11 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags,struct page *p;void *vaddr; -/* Don't set the __GFP_COMP flag for higher order allocations. -* Mapping pages directly into an userspace process and calling -* put_page() on a TTM allocated page is illegal. +/* +* For higher-order allocations be a good citizen: don't dip into +* memory reserves, don't retry hard, don't warn on failure and stay +* on the local node. The non-DMA path additionally sets __GFP_COMP +* below; the DMA path allocates via dma_alloc_attrs(). */ if (order) gfp_flags |= __GFP_NOMEMALLOC | __GFP_NORETRY | __GFP_NOWARN |@@ -189,11 +191,9 @@ static struct page *ttm_pool_alloc_page(struct ttm_pool *pool, gfp_t gfp_flags, }if (!ttm_pool_uses_dma_alloc (pool)) { - p =alloc_pages_node (pool->nid, gfp_flags, order); -if (p) { - p->private = order; + p =alloc_pages_node (pool->nid, gfp_flags | __GFP_COMP, order); +if (p)mod_lruvec_page_state (p,NR_GPU_ACTIVE ,1 << order); - }return p; }@@ -482,7 +482,7 @@ static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)return dma->vaddr & ~PAGE_MASK ; } -return p->private; +return folio_order (page_folio (p)); }/* @@ -493,15 +493,31 @@ static unsigned int ttm_pool_page_order(struct ttm_pool *pool, struct page *p)static void ttm_pool_split_for_swap (struct ttm_pool *pool,struct page *p) {unsigned int order =ttm_pool_page_order (pool, p); -pgoff_t nr;if (!order)return ; -split_page (p, order); - nr =1UL << order; -while (nr--) - (p++)->private =0 ; +if (ttm_pool_uses_dma_alloc (pool)) { +pgoff_t nr; + +/* +* DMA-alloc pages are not compound; split the plain +* higher-order allocation and clear the per-page private +* (which held the order for the non-compound case). +*/ +split_page (p, order); + nr =1UL << order; +while (nr--) + (p++)->private =0 ; +return ; + } + +/* +* The non-DMA path allocates compound folios (__GFP_COMP). Split the +* driver-owned, off-LRU, unmapped folio into order-0 folios so each +* page can be freed as soon as it has been backed up. +*/ +folio_split_driver_managed (page_folio (p),0 ); }/** @@ -548,7 +564,7 @@ static pgoff_t ttm_pool_unmap_and_free(struct ttm_pool *pool, struct page *page, pt =ttm_pool_select_type (pool, caching, order); }else { - order = page->private; + order =folio_order (page_folio (page)); nr = (1UL << order); }@@ -1124,7 +1140,6 @@ long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt, num_pages);if (flags->purge) { shrunken += num_pages; - page->private =0 ;__free_pages_gpu_account (page, order, false);memset (tt->pages + i,0 , num_pages *sizeof (*tt->pages));@@ -1214,7 +1229,6 @@ long ttm_pool_backup(struct ttm_pool *pool, struct ttm_tt *tt, }/* Fully backed up: free at native order. */ - page->private =0 ;__free_pages_gpu_account (page, order, false); }
Patchwork
CI✗ CI.KUnit: failure for series starting with [1/3] mm/huge_memory: add folio_split_driver_managed()
In reply to Matthew Brost== Series Details ==
== Series Details ==
Series: series starting with [1/3] mm/huge_memory: add folio_split_driver_managed()
URL : https://patchwork.freedesktop.org/series/170875/
State : failure
== Summary ==
+ trap cleanup EXIT
+ /kernel/tools/testing/kunit/kunit.py run --kunitconfig /kernel/drivers/gpu/drm/xe/.kunitconfig ERROR:root:In file included from ../include/linux/mm.h:31,
from ../include/linux/pid_namespace.h:7,
from ../include/linux/ptrace.h:10,
from ../include/linux/audit.h:13,
from ../arch/um/kernel/asm-offsets.c:8:
../include/linux/pgtable.h: In function ‘pmdp_test_and_clear_young’:
../include/linux/pgtable.h:553:17: error: implicit declaration of function ‘set_pmd_at’; did you mean ‘set_pte_at’? [-Werror=implicit-function-declaration]
553 | set_pmd_at(vma->vm_mm, address, pmdp, pmd_mkold(pmd));
| ^~~~~~~~~~
| set_pte_at
../include/linux/pgtable.h:553:55: error: implicit declaration of function ‘pmd_mkold’; did you mean ‘pte_mkold’? [-Werror=implicit-function-declaration]
553 | set_pmd_at(vma->vm_mm, address, pmdp, pmd_mkold(pmd));
| ^~~~~~~~~
| pte_mkold
../include/linux/pgtable.h: In function ‘pmd_mkwrite’:
../include/linux/pgtable.h:1060:16: error: implicit declaration of function ‘pmd_mkwrite_novma’; did you mean ‘pte_mkwrite_novma’? [-Werror=implicit-function-declaration]
1060 | return pmd_mkwrite_novma(pmd);
| ^~~~~~~~~~~~~~~~~
| pte_mkwrite_novma
../include/linux/pgtable.h:1060:16: error: incompatible types when returning type ‘int’ but ‘pmd_t’ was expected
1060 | return pmd_mkwrite_novma(pmd);
| ^~~~~~~~~~~~~~~~~~~~~~
../include/linux/pgtable.h: In function ‘pmdp_set_wrprotect’:
../include/linux/pgtable.h:1241:39: error: implicit declaration of function ‘pmd_wrprotect’; did you mean ‘pte_wrprotect’? [-Werror=implicit-function-declaration]
1241 | set_pmd_at(mm, address, pmdp, pmd_wrprotect(old_pmd));
| ^~~~~~~~~~~~~
| pte_wrprotect
In file included from ../include/linux/mm.h:1790:
../include/linux/huge_mm.h: In function ‘pmd_is_huge’:
../include/linux/huge_mm.h:449:24: error: implicit declaration of function ‘pmd_trans_huge’; did you mean ‘pud_trans_huge’? [-Werror=implicit-function-declaration]
449 | return pmd_trans_huge(pmd);
| ^~~~~~~~~~~~~~
| pud_trans_huge
../include/linux/mm.h: In function ‘folio_mk_pmd’:
../include/linux/mm.h:2608:16: error: implicit declaration of function ‘pmd_mkhuge’; did you mean ‘pmd_is_huge’? [-Werror=implicit-function-declaration]
2608 | return pmd_mkhuge(pfn_pmd(folio_pfn(folio), pgprot));
| ^~~~~~~~~~
| pmd_is_huge
../include/linux/mm.h:2608:16: error: incompatible types when returning type ‘int’ but ‘pmd_t’ was expected
2608 | return pmd_mkhuge(pfn_pmd(folio_pfn(folio), pgprot));
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~cc1: some warnings being treated as errors
make[3]: *** [../scripts/Makefile.build:184: arch/um/kernel/asm-offsets.s] Error 1 make[2]: *** [/kernel/Makefile:1405: prepare0] Error 2
make[1]: *** [/kernel/Makefile:248: __sub-make] Error 2
make: *** [Makefile:248: __sub-make] Error 2
[04:47:55] Configuring KUnit Kernel ...
Generating .config ...
Populating config with:
$ make ARCH=um O=.kunit olddefconfig [04:48:00] Building KUnit Kernel ... Populating config with: $ make ARCH=um O=.kunit olddefconfig Building with: $ make all compile_commands.json scripts_gdb ARCH=um O=.kunit --jobs=48 + cleanup ++ stat -c %u:%g /kernel + chown -R 1003:1003 /kernel
Zi Yan
reviewerQuoted 10 lines · Matthew Brost
Add a lightweight structural split primitive for large (compound) folios that a driver allocated with
__GFP_COMPand manages entirely by itself, outside of the core mm's view.The existing split paths -
split_folio()andfolio_split_unmapped()- are built for folios that the mm owns: they perform a refcount freeze, walk and remap the rmap, and take theanon_vma/i_mmaplocks, andfolio_split_unmapped()further assumes an anon, pagecache-style refcount model (nr_pages+ 1). None of that applies to a folio that is:
Quoted 8 lines
- singly referenced (the caller holds the only reference),
- not mapped through the rmap (folio_mapped()== 0),
- not in the page cache or swap cache (folio->mapping== NULL),
- not on any LRU or the deferred-split list.For such a folio the split is purely structural: because nothing else in the kernel can reach it, there is no need to freeze the refcount or touch any mapping.
folio_split_driver_managed()therefore performs only the
I do not think so. PFN scanners like memory compaction should be able to see them, unless you mean something else about "a driver allocated with __GFP_COMP". You will need to freeze it to prevent the to-be-split folios being touched by others.
Quoted 153 lines
compound and split-accounting teardown via
__split_unmapped_folio()and then hands each resulting order-@new_order folio its own reference, mirroringsplit_page()for compound folios. The caller keeps the original reference on the first resulting folio and is responsible for freeing all of them individually.The immediate user is TTM's GPU page pool, which allocates higher-order compound pages, maps them into userspace via
VM_PFNMAP(never through the rmap), and needs to split them into order-0 folios under memory pressure so pages can be backed up to shmem and freed one at a time.A
CONFIG_TRANSPARENT_HUGEPAGE=nstub is provided so callers can build without the split machinery; it warns and returns-EINVAL.Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Christian Koenig <[email protected]>Cc Huang Rui <[email protected]>Cc Matthew Auld <[email protected]>Cc Matthew Brost <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc Nico Pache <[email protected]>Cc Ryan Roberts <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Lance Yang <[email protected]>Cc Tvrtko Ursulin <[email protected]>Cc Dave Airlie <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Matthew Brost <[email protected]>Assisted-by GitHub-Copilot:claude-opus-4.8---
The patch is based on drm-tip rather than the core MM branches to facilitate Intel CI testing and initial review. It can be rebased onto the core MM branches in a subsequent revision.
--- include/linux/huge_mm.h | 8 ++++++ mm/huge_memory.c | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+)include/linux/huge_mm.h +8
-0@@ -402,6 +402,7 @@ enum split_type {int __split_huge_page_to_list_to_order (struct page *page,struct list_head *list,unsigned int new_order);int folio_split_unmapped (struct folio *folio,unsigned int new_order); +int folio_split_driver_managed (struct folio *folio,unsigned int new_order);unsigned int min_order_for_split (struct folio *folio);int split_folio_to_list (struct folio *folio,struct list_head *list); int folio_check_splittable(struct folio *folio, unsigned int new_order,@@ -656,6 +657,13 @@ static inline int split_folio_to_list(struct folio *folio, struct list_head *lisreturn -EINVAL ; } +static inline int folio_split_driver_managed (struct folio *folio, +unsigned int new_order) +{ +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; +} +static inline int folio_split (struct folio *folio,unsigned int new_order,struct page *page,struct list_head *list) {mm/huge_memory.c +63
-0@@ -4185,6 +4185,69 @@ int folio_split_unmapped(struct folio *folio, unsigned int new_order)return ret; } +/** +* folio_split_driver_managed() - split an exclusively-owned, off-LRU folio +* @folio: folio to split. Must be a large (compound) folio that is owned +* exclusively by the caller and is invisible to the core mm. +* @new_order: the order of the folios after the split. +* +* This is a lightweight structural split for folios that a driver allocated +* and manages itself (for example TTM's GPU page pool, which allocates +* higher-order compound pages with __GFP_COMP and maps them into userspace +* via VM_PFNMAP rather than through the rmap). Such folios are: +* +* - singly referenced (the caller holds the only reference), +* - not mapped through the rmap (folio_mapcount() == 0), +* - not in the page cache or swap cache (folio->mapping == NULL), +* - not on any LRU or the deferred-split list. +* +* Because nothing else in the kernel can reach the folio, this helper does +* not perform the refcount freeze / remap / anon_vma & i_mmap locking dance +* that split_folio() and folio_split_unmapped() require. It performs only +* the compound and split-accounting teardown and then hands each resulting +* folio its own reference, mirroring split_page() for compound folios. +* +* The caller is responsible for freeing the resulting folios individually. +* +* Context: caller holds the only reference and excludes concurrent access. +* Does not sleep. +* +* Return: 0 on success, negative errno on failure. +*/ +int folio_split_driver_managed (struct folio *folio,unsigned int new_order) +{ +unsigned int old_order =folio_order (folio); +unsigned int split_nr =1U << new_order; +unsigned int nr =1U << old_order; +unsigned int i; + +if (new_order >= old_order) +return -EINVAL ; + +VM_WARN_ON_ONCE_FOLIO (folio_ref_count (folio) !=1 , folio); +VM_WARN_ON_ONCE_FOLIO (folio_mapped (folio), folio); +VM_WARN_ON_ONCE_FOLIO (folio->mapping, folio); +VM_WARN_ON_ONCE_FOLIO (folio_test_swapcache (folio), folio); +VM_WARN_ON_ONCE_FOLIO (folio_test_lru (folio), folio); + +/* +* Structural + split-accounting teardown only. No mapping/xarray, no +* refcount freeze: the folio is frozen-by-ownership already. +*/ +__split_unmapped_folio (folio, new_order, &folio->page,NULL ,NULL , +SPLIT_TYPE_UNIFORM ); + +/* +* Give every resulting head folio its own reference. The original +* reference stays on the first one, exactly like split_page(). +*/ +for (i = split_nr; i < nr; i += split_nr) +set_page_refcounted (folio_page (folio, i)); + +return 0 ; +} +EXPORT_SYMBOL_GPL (folio_split_driver_managed); +/* * This function splits a large folio into smaller folios of order @new_order. * @page can point to any page of the large folio to split. The split operation
Signature · 2 lines
Best Regards, Yan, Zi
Zi Yan
reviewerQuoted 113 lines · Zi Yan
Quoted 10 lines · Matthew Brost
Add a lightweight structural split primitive for large (compound) folios that a driver allocated with
__GFP_COMPand manages entirely by itself, outside of the core mm's view.The existing split paths -
split_folio()andfolio_split_unmapped()- are built for folios that the mm owns: they perform a refcount freeze, walk and remap the rmap, and take theanon_vma/i_mmaplocks, andfolio_split_unmapped()further assumes an anon, pagecache-style refcount model (nr_pages+ 1). None of that applies to a folio that is:Quoted 8 lines
- singly referenced (the caller holds the only reference),
- not mapped through the rmap (folio_mapped()== 0),
- not in the page cache or swap cache (folio->mapping== NULL),
- not on any LRU or the deferred-split list.For such a folio the split is purely structural: because nothing else in the kernel can reach it, there is no need to freeze the refcount or touch any mapping.
folio_split_driver_managed()therefore performs only theI do not think so. PFN scanners like memory compaction should be able to see them, unless you mean something else about "a driver allocated with
__GFP_COMP". You will need to freeze it to prevent the to-be-split folios being touched by others.Quoted 87 lines
compound and split-accounting teardown via
__split_unmapped_folio()and then hands each resulting order-@new_order folio its own reference, mirroringsplit_page()for compound folios. The caller keeps the original reference on the first resulting folio and is responsible for freeing all of them individually.The immediate user is TTM's GPU page pool, which allocates higher-order compound pages, maps them into userspace via
VM_PFNMAP(never through the rmap), and needs to split them into order-0 folios under memory pressure so pages can be backed up to shmem and freed one at a time.A
CONFIG_TRANSPARENT_HUGEPAGE=nstub is provided so callers can build without the split machinery; it warns and returns-EINVAL.Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Christian Koenig <[email protected]>Cc Huang Rui <[email protected]>Cc Matthew Auld <[email protected]>Cc Matthew Brost <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc Nico Pache <[email protected]>Cc Ryan Roberts <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Lance Yang <[email protected]>Cc Tvrtko Ursulin <[email protected]>Cc Dave Airlie <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Matthew Brost <[email protected]>Assisted-by GitHub-Copilot:claude-opus-4.8---
The patch is based on drm-tip rather than the core MM branches to facilitate Intel CI testing and initial review. It can be rebased onto the core MM branches in a subsequent revision.
--- include/linux/huge_mm.h | 8 ++++++ mm/huge_memory.c | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+)include/linux/huge_mm.h +8
-0@@ -402,6 +402,7 @@ enum split_type {int __split_huge_page_to_list_to_order (struct page *page,struct list_head *list,unsigned int new_order);int folio_split_unmapped (struct folio *folio,unsigned int new_order); +int folio_split_driver_managed (struct folio *folio,unsigned int new_order);unsigned int min_order_for_split (struct folio *folio);int split_folio_to_list (struct folio *folio,struct list_head *list); int folio_check_splittable(struct folio *folio, unsigned int new_order,@@ -656,6 +657,13 @@ static inline int split_folio_to_list(struct folio *folio, struct list_head *lisreturn -EINVAL ; } +static inline int folio_split_driver_managed (struct folio *folio, +unsigned int new_order) +{ +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; +} +static inline int folio_split (struct folio *folio,unsigned int new_order,struct page *page,struct list_head *list) {mm/huge_memory.c +0
-0@@ -4185,6 +4185,69 @@ int folio_split_unmapped(struct folio *folio, unsigned int new_order)return ret; }
Quoted 10 lines
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4197
Quoted 10 lines
mm/huge_memory.c +10
-0+/** +* folio_split_driver_managed() - split an exclusively-owned, off-LRU folio +* @folio: folio to split. Must be a large (compound) folio that is owned +* exclusively by the caller and is invisible to the core mm. +* @new_order: the order of the folios after the split. +* +* This is a lightweight structural split for folios that a driver allocated +* and manages itself (for example TTM's GPU page pool, which allocates +* higher-order compound pages with __GFP_COMP and maps them into userspace +* via VM_PFNMAP rather than through the rmap). Such folios are:
Strickly speaking, these vm_insert*() compound pages are not folios, since folios are supposed to be rmappable and they are either anonymous memory or file-backed memory. I am working on separating them from rmappable folios by replacing PG_private with PG_folio and marking all pages in a folio with PG_folio in page_rmappable_folio().
Hopefully, we can find a better name, like refcounted_folio, later for these non-rmappable compound pages.
Quoted 56 lines
Quoted 56 lines
mm/huge_memory.c +53
-0+* +* - singly referenced (the caller holds the only reference), +* - not mapped through the rmap (folio_mapcount() == 0), +* - not in the page cache or swap cache (folio->mapping == NULL), +* - not on any LRU or the deferred-split list. +* +* Because nothing else in the kernel can reach the folio, this helper does +* not perform the refcount freeze / remap / anon_vma & i_mmap locking dance +* that split_folio() and folio_split_unmapped() require. It performs only +* the compound and split-accounting teardown and then hands each resulting +* folio its own reference, mirroring split_page() for compound folios. +* +* The caller is responsible for freeing the resulting folios individually. +* +* Context: caller holds the only reference and excludes concurrent access. +* Does not sleep. +* +* Return: 0 on success, negative errno on failure. +*/ +int folio_split_driver_managed (struct folio *folio,unsigned int new_order) +{ +unsigned int old_order =folio_order (folio); +unsigned int split_nr =1U << new_order; +unsigned int nr =1U << old_order; +unsigned int i; + +if (new_order >= old_order) +return -EINVAL ; + +VM_WARN_ON_ONCE_FOLIO (folio_ref_count (folio) !=1 , folio); +VM_WARN_ON_ONCE_FOLIO (folio_mapped (folio), folio); +VM_WARN_ON_ONCE_FOLIO (folio->mapping, folio); +VM_WARN_ON_ONCE_FOLIO (folio_test_swapcache (folio), folio); +VM_WARN_ON_ONCE_FOLIO (folio_test_lru (folio), folio); + +/* +* Structural + split-accounting teardown only. No mapping/xarray, no +* refcount freeze: the folio is frozen-by-ownership already. +*/ +__split_unmapped_folio (folio, new_order, &folio->page,NULL ,NULL , +SPLIT_TYPE_UNIFORM ); + +/* +* Give every resulting head folio its own reference. The original +* reference stays on the first one, exactly like split_page(). +*/ +for (i = split_nr; i < nr; i += split_nr) +set_page_refcounted (folio_page (folio, i)); + +return 0 ; +} +EXPORT_SYMBOL_GPL (folio_split_driver_managed); +/* * This function splits a large folio into smaller folios of order @new_order. * @page can point to any page of the large folio to split. The split operation
Signature · 2 lines
Best Regards, Yan, Zi
David Hildenbrand (Arm)
maintainerQuoted 131 lines · Zi Yan
Quoted 113 lines · Zi Yan
Quoted 10 lines · Matthew Brost
Add a lightweight structural split primitive for large (compound) folios that a driver allocated with
__GFP_COMPand manages entirely by itself, outside of the core mm's view.The existing split paths -
split_folio()andfolio_split_unmapped()- are built for folios that the mm owns: they perform a refcount freeze, walk and remap the rmap, and take theanon_vma/i_mmaplocks, andfolio_split_unmapped()further assumes an anon, pagecache-style refcount model (nr_pages+ 1). None of that applies to a folio that is:Quoted 8 lines
- singly referenced (the caller holds the only reference),
- not mapped through the rmap (folio_mapped()== 0),
- not in the page cache or swap cache (folio->mapping== NULL),
- not on any LRU or the deferred-split list.For such a folio the split is purely structural: because nothing else in the kernel can reach it, there is no need to freeze the refcount or touch any mapping.
folio_split_driver_managed()therefore performs only theI do not think so. PFN scanners like memory compaction should be able to see them, unless you mean something else about "a driver allocated with
__GFP_COMP". You will need to freeze it to prevent the to-be-split folios being touched by others.Quoted 87 lines
compound and split-accounting teardown via
__split_unmapped_folio()and then hands each resulting order-@new_order folio its own reference, mirroringsplit_page()for compound folios. The caller keeps the original reference on the first resulting folio and is responsible for freeing all of them individually.The immediate user is TTM's GPU page pool, which allocates higher-order compound pages, maps them into userspace via
VM_PFNMAP(never through the rmap), and needs to split them into order-0 folios under memory pressure so pages can be backed up to shmem and freed one at a time.A
CONFIG_TRANSPARENT_HUGEPAGE=nstub is provided so callers can build without the split machinery; it warns and returns-EINVAL.Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Christian Koenig <[email protected]>Cc Huang Rui <[email protected]>Cc Matthew Auld <[email protected]>Cc Matthew Brost <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc Nico Pache <[email protected]>Cc Ryan Roberts <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Lance Yang <[email protected]>Cc Tvrtko Ursulin <[email protected]>Cc Dave Airlie <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Matthew Brost <[email protected]>Assisted-by GitHub-Copilot:claude-opus-4.8---
The patch is based on drm-tip rather than the core MM branches to facilitate Intel CI testing and initial review. It can be rebased onto the core MM branches in a subsequent revision.
--- include/linux/huge_mm.h | 8 ++++++ mm/huge_memory.c | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+)include/linux/huge_mm.h +8
-0@@ -402,6 +402,7 @@ enum split_type {int __split_huge_page_to_list_to_order (struct page *page,struct list_head *list,unsigned int new_order);int folio_split_unmapped (struct folio *folio,unsigned int new_order); +int folio_split_driver_managed (struct folio *folio,unsigned int new_order);unsigned int min_order_for_split (struct folio *folio);int split_folio_to_list (struct folio *folio,struct list_head *list); int folio_check_splittable(struct folio *folio, unsigned int new_order,@@ -656,6 +657,13 @@ static inline int split_folio_to_list(struct folio *folio, struct list_head *lisreturn -EINVAL ; } +static inline int folio_split_driver_managed (struct folio *folio, +unsigned int new_order) +{ +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; +} +static inline int folio_split (struct folio *folio,unsigned int new_order,struct page *page,struct list_head *list) {mm/huge_memory.c +0
-0@@ -4185,6 +4185,69 @@ int folio_split_unmapped(struct folio *folio, unsigned int new_order)return ret; }Quoted 10 lines
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4197
Quoted 10 lines
mm/huge_memory.c +10
-0+/** +* folio_split_driver_managed() - split an exclusively-owned, off-LRU folio +* @folio: folio to split. Must be a large (compound) folio that is owned +* exclusively by the caller and is invisible to the core mm. +* @new_order: the order of the folios after the split. +* +* This is a lightweight structural split for folios that a driver allocated +* and manages itself (for example TTM's GPU page pool, which allocates +* higher-order compound pages with __GFP_COMP and maps them into userspace +* via VM_PFNMAP rather than through the rmap). Such folios are: Strickly speaking, these vm_insert*() compound pages are not folios, since folios are supposed to be rmappable and they are either anonymous memory or file-backed memory. I am working on separating them from rmappable folios by replacing
PG_privatewithPG_folioand marking all pages in a folio withPG_folioinpage_rmappable_folio().
Quoted 2 lines
Hopefully, we can find a better name, like
refcounted_folio, later for these non-rmappable compound pages.
They wouldn't really be folios, I guess. They would likely be a simple "refcounted" memtype that allows for compound pages.
But what is the conclusion here? It sounds like "folio_split_" is the entirely wrong interface for these compound pages.
Signature · 3 lines
Cheers, David
Zi Yan
reviewerQuoted 132 lines · David Hildenbrand (Arm)
Quoted 131 lines · Zi Yan
Quoted 113 lines · Zi Yan
Quoted 10 lines · Matthew Brost
Add a lightweight structural split primitive for large (compound) folios that a driver allocated with
__GFP_COMPand manages entirely by itself, outside of the core mm's view.The existing split paths -
split_folio()andfolio_split_unmapped()- are built for folios that the mm owns: they perform a refcount freeze, walk and remap the rmap, and take theanon_vma/i_mmaplocks, andfolio_split_unmapped()further assumes an anon, pagecache-style refcount model (nr_pages+ 1). None of that applies to a folio that is:Quoted 8 lines
- singly referenced (the caller holds the only reference),
- not mapped through the rmap (folio_mapped()== 0),
- not in the page cache or swap cache (folio->mapping== NULL),
- not on any LRU or the deferred-split list.For such a folio the split is purely structural: because nothing else in the kernel can reach it, there is no need to freeze the refcount or touch any mapping.
folio_split_driver_managed()therefore performs only theI do not think so. PFN scanners like memory compaction should be able to see them, unless you mean something else about "a driver allocated with
__GFP_COMP". You will need to freeze it to prevent the to-be-split folios being touched by others.Quoted 87 lines
compound and split-accounting teardown via
__split_unmapped_folio()and then hands each resulting order-@new_order folio its own reference, mirroringsplit_page()for compound folios. The caller keeps the original reference on the first resulting folio and is responsible for freeing all of them individually.The immediate user is TTM's GPU page pool, which allocates higher-order compound pages, maps them into userspace via
VM_PFNMAP(never through the rmap), and needs to split them into order-0 folios under memory pressure so pages can be backed up to shmem and freed one at a time.A
CONFIG_TRANSPARENT_HUGEPAGE=nstub is provided so callers can build without the split machinery; it warns and returns-EINVAL.Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Christian Koenig <[email protected]>Cc Huang Rui <[email protected]>Cc Matthew Auld <[email protected]>Cc Matthew Brost <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc Nico Pache <[email protected]>Cc Ryan Roberts <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Lance Yang <[email protected]>Cc Tvrtko Ursulin <[email protected]>Cc Dave Airlie <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Matthew Brost <[email protected]>Assisted-by GitHub-Copilot:claude-opus-4.8---
The patch is based on drm-tip rather than the core MM branches to facilitate Intel CI testing and initial review. It can be rebased onto the core MM branches in a subsequent revision.
--- include/linux/huge_mm.h | 8 ++++++ mm/huge_memory.c | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+)include/linux/huge_mm.h +8
-0@@ -402,6 +402,7 @@ enum split_type {int __split_huge_page_to_list_to_order (struct page *page,struct list_head *list,unsigned int new_order);int folio_split_unmapped (struct folio *folio,unsigned int new_order); +int folio_split_driver_managed (struct folio *folio,unsigned int new_order);unsigned int min_order_for_split (struct folio *folio);int split_folio_to_list (struct folio *folio,struct list_head *list); int folio_check_splittable(struct folio *folio, unsigned int new_order,@@ -656,6 +657,13 @@ static inline int split_folio_to_list(struct folio *folio, struct list_head *lisreturn -EINVAL ; } +static inline int folio_split_driver_managed (struct folio *folio, +unsigned int new_order) +{ +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; +} +static inline int folio_split (struct folio *folio,unsigned int new_order,struct page *page,struct list_head *list) {mm/huge_memory.c +0
-0@@ -4185,6 +4185,69 @@ int folio_split_unmapped(struct folio *folio, unsigned int new_order)return ret; }Quoted 10 lines
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4197
Quoted 10 lines
mm/huge_memory.c +10
-0+/** +* folio_split_driver_managed() - split an exclusively-owned, off-LRU folio +* @folio: folio to split. Must be a large (compound) folio that is owned +* exclusively by the caller and is invisible to the core mm. +* @new_order: the order of the folios after the split. +* +* This is a lightweight structural split for folios that a driver allocated +* and manages itself (for example TTM's GPU page pool, which allocates +* higher-order compound pages with __GFP_COMP and maps them into userspace +* via VM_PFNMAP rather than through the rmap). Such folios are: Strickly speaking, these vm_insert*() compound pages are not folios, since folios are supposed to be rmappable and they are either anonymous memory or file-backed memory. I am working on separating them from rmappable folios by replacing
PG_privatewithPG_folioand marking all pages in a folio withPG_folioinpage_rmappable_folio().
Quoted 5 lines
Quoted 2 lines
Hopefully, we can find a better name, like
refcounted_folio, later for these non-rmappable compound pages.They wouldn't really be folios, I guess. They would likely be a simple "refcounted" memtype that allows for compound pages.
Yes, they are not folios. But “folio” was started to replace “compound page” and slowly becomes rmappable anon and file-backed. People outside MM still thinks “folio” == “compound page”. But once I manage to remove PG_private and get us PG_folio, page_folio() will return NULL for non-folio compound pages and we will need a new type for them, “refcounted_XXX”. We can decide XXX when I get there. :)
Quoted 3 lines
But what is the conclusion here? It sounds like "
folio_split_" is the entirely wrong interface for these compound pages.
We probably would allow folio_split() to be used on compound pages now until we can make a clean distinction, e.g., using PG_folio, between them.
Yes, folio_split() and its helper functions are meant for rmappable anon and file-backed folios. But currently “folio” is de facto “compound page”, since for example prep_compound_head() initializes folio fields even if it is meant only for compound pages.
After folio and compound page are separate concepts in the code base, probably we can think about how to have two split functions for them and still keep maximum code reuse. Namely, we could have split_compound() does the compound page split (e.g., copying page flags, adjust compound head/tail/order, etc.) and split_folio() calls split_compound() and perform extra folio operations. The details are TBD.
Best Regards,
Yan, Zi
Matthew Brost
maintainerQuoted 159 lines · Zi Yan
Quoted 132 lines · David Hildenbrand (Arm)
Quoted 131 lines · Zi Yan
Quoted 113 lines · Zi Yan
Quoted 10 lines · Matthew Brost
Add a lightweight structural split primitive for large (compound) folios that a driver allocated with
__GFP_COMPand manages entirely by itself, outside of the core mm's view.The existing split paths -
split_folio()andfolio_split_unmapped()- are built for folios that the mm owns: they perform a refcount freeze, walk and remap the rmap, and take theanon_vma/i_mmaplocks, andfolio_split_unmapped()further assumes an anon, pagecache-style refcount model (nr_pages+ 1). None of that applies to a folio that is:Quoted 8 lines
- singly referenced (the caller holds the only reference),
- not mapped through the rmap (folio_mapped()== 0),
- not in the page cache or swap cache (folio->mapping== NULL),
- not on any LRU or the deferred-split list.For such a folio the split is purely structural: because nothing else in the kernel can reach it, there is no need to freeze the refcount or touch any mapping.
folio_split_driver_managed()therefore performs only theI do not think so. PFN scanners like memory compaction should be able to see them, unless you mean something else about "a driver allocated with
__GFP_COMP". You will need to freeze it to prevent the to-be-split folios being touched by others.Quoted 87 lines
compound and split-accounting teardown via
__split_unmapped_folio()and then hands each resulting order-@new_order folio its own reference, mirroringsplit_page()for compound folios. The caller keeps the original reference on the first resulting folio and is responsible for freeing all of them individually.The immediate user is TTM's GPU page pool, which allocates higher-order compound pages, maps them into userspace via
VM_PFNMAP(never through the rmap), and needs to split them into order-0 folios under memory pressure so pages can be backed up to shmem and freed one at a time.A
CONFIG_TRANSPARENT_HUGEPAGE=nstub is provided so callers can build without the split machinery; it warns and returns-EINVAL.Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Christian Koenig <[email protected]>Cc Huang Rui <[email protected]>Cc Matthew Auld <[email protected]>Cc Matthew Brost <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc Nico Pache <[email protected]>Cc Ryan Roberts <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Lance Yang <[email protected]>Cc Tvrtko Ursulin <[email protected]>Cc Dave Airlie <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Matthew Brost <[email protected]>Assisted-by GitHub-Copilot:claude-opus-4.8---
The patch is based on drm-tip rather than the core MM branches to facilitate Intel CI testing and initial review. It can be rebased onto the core MM branches in a subsequent revision.
--- include/linux/huge_mm.h | 8 ++++++ mm/huge_memory.c | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+)include/linux/huge_mm.h +8
-0@@ -402,6 +402,7 @@ enum split_type {int __split_huge_page_to_list_to_order (struct page *page,struct list_head *list,unsigned int new_order);int folio_split_unmapped (struct folio *folio,unsigned int new_order); +int folio_split_driver_managed (struct folio *folio,unsigned int new_order);unsigned int min_order_for_split (struct folio *folio);int split_folio_to_list (struct folio *folio,struct list_head *list); int folio_check_splittable(struct folio *folio, unsigned int new_order,@@ -656,6 +657,13 @@ static inline int split_folio_to_list(struct folio *folio, struct list_head *lisreturn -EINVAL ; } +static inline int folio_split_driver_managed (struct folio *folio, +unsigned int new_order) +{ +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ; +} +static inline int folio_split (struct folio *folio,unsigned int new_order,struct page *page,struct list_head *list) {mm/huge_memory.c +0
-0@@ -4185,6 +4185,69 @@ int folio_split_unmapped(struct folio *folio, unsigned int new_order)return ret; }Quoted 10 lines
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4197
Quoted 10 lines
mm/huge_memory.c +10
-0+/** +* folio_split_driver_managed() - split an exclusively-owned, off-LRU folio +* @folio: folio to split. Must be a large (compound) folio that is owned +* exclusively by the caller and is invisible to the core mm. +* @new_order: the order of the folios after the split. +* +* This is a lightweight structural split for folios that a driver allocated +* and manages itself (for example TTM's GPU page pool, which allocates +* higher-order compound pages with __GFP_COMP and maps them into userspace +* via VM_PFNMAP rather than through the rmap). Such folios are: Strickly speaking, these vm_insert*() compound pages are not folios, since folios are supposed to be rmappable and they are either anonymous memory or file-backed memory. I am working on separating them from rmappable folios by replacing
PG_privatewithPG_folioand marking all pages in a folio withPG_folioinpage_rmappable_folio().Quoted 5 lines
Quoted 2 lines
Hopefully, we can find a better name, like
refcounted_folio, later for these non-rmappable compound pages.They wouldn't really be folios, I guess. They would likely be a simple "refcounted" memtype that allows for compound pages.
Yes, they are not folios. But “folio” was started to replace “compound page” and slowly becomes rmappable anon and file-backed. People outside MM still thinks “folio” == “compound page”. But once I manage to remove
PG_privateand get usPG_folio,page_folio()will return NULL for non-folio compound pages and we will need a new type for them, “refcounted_XXX”. We can decide XXX when I get there. :)Quoted 3 lines
But what is the conclusion here? It sounds like "
folio_split_" is the entirely wrong interface for these compound pages.We probably would allow
folio_split()to be used on compound pages now until we can make a clean distinction, e.g., usingPG_folio, between them.Yes,
folio_split()and its helper functions are meant for rmappable anon and file-backed folios. But currently “folio” is de facto “compound page”, since for exampleprep_compound_head()initializes folio fields even if it is meant only for compound pages.
Quoted 6 lines
After folio and compound page are separate concepts in the code base, probably we can think about how to have two split functions for them and still keep maximum code reuse. Namely, we could have
split_compound()does the compound page split (e.g., copying page flags, adjust compound head/tail/order, etc.) andsplit_folio()callssplit_compound()and perform extra folio operations. The details are TBD.
I like the idea of split_compound, ideally in mm/page_alloc.c, similar to split_page(), which performs the mechanical splitting of compound pages managed on the driver side (i.e., not mappable, not on an LRU, protected by dma-resv in the case of DRM, etc.).
I got yelled at for not allocating these types of pages with GFP_COMP, and I basically found that this missing MM primitive is what has prevented that approach from being practical. Using GFP_COMP makes our life quite a bit easier for managing these types of pages too, so it would be great if we could get a solution for this.
So maybe I should drop this for now, keep an eye on the MM work, and circle back once the missing primitive is implemented?
Thanks the help / insight,
Matt
Quoted 3 lines
Best Regards,
Yan, Zi
David Hildenbrand (Arm)
maintainerQuoted 13 lines · Zi Yan
Quoted 11 lines · David Hildenbrand (Arm)
Quoted 9 lines · Zi Yan
Strickly speaking, these vm_insert*() compound pages are not folios, since folios are supposed to be rmappable and they are either anonymous memory or file-backed memory. I am working on separating them from rmappable folios by replacing
PG_privatewithPG_folioand marking all pages in a folio withPG_folioinpage_rmappable_folio().Hopefully, we can find a better name, like
refcounted_folio, later for these non-rmappable compound pages.
Quoted 6 lines
Quoted 2 lines
They wouldn't really be folios, I guess. They would likely be a simple "refcounted" memtype that allows for compound pages.
Yes, they are not folios. But “folio” was started to replace “compound page” and slowly becomes rmappable anon and file-backed. People outside MM still thinks “folio” == “compound page”.
Yes, it's a bit of a mess now.
Quoted 4 lines
But once I manage to remove
PG_private
and get usPG_folio,page_folio()will return NULL for non-folio compound pages and we will need a new type for them, “refcounted_XXX”. We can decide XXX when I get there. :)
So far willy did not document a type that just has a refcount.
https://kernelnewbies.org/MatthewWilcox/Memdescs
Maybe "Managed memory" (struct mgdesc) could be the right thing for some memory with a refcount.
The question is, if the use case at hand would even require a refcount, of if frozen pages would be good enough.
Quoted 5 lines
Quoted 4 lines
But what is the conclusion here? It sounds like "
folio_split_" is the entirely wrong interface for these compound pages.
Quoted 7 lines
We probably would allow
folio_split()to be used on compound pages now until we can make a clean distinction, e.g., usingPG_folio, between them.Yes,
folio_split()and its helper functions are meant for rmappable anon and file-backed folios. But currently “folio” is de facto “compound page”, since for exampleprep_compound_head()initializes folio fields even if it is meant only for compound pages.
If we can stop more abuse right from the start, that would be nice.
We do have split_page() that splits a non-compound higher-order page. Maybe we want a split_compound_page(), or allow for split_page() to accept compound pages.
Because splitting a folio is really something different than splitting just some compound page (no mapping/pagecache/whatever involved).
Signature · 3 lines
Cheers, David
Quoted 23 lines · David Hildenbrand (Arm)
Quoted 13 lines · Zi Yan
Quoted 11 lines · David Hildenbrand (Arm)
Quoted 9 lines · Zi Yan
Strickly speaking, these vm_insert*() compound pages are not folios, since folios are supposed to be rmappable and they are either anonymous memory or file-backed memory. I am working on separating them from rmappable folios by replacing
PG_privatewithPG_folioand marking all pages in a folio withPG_folioinpage_rmappable_folio().Hopefully, we can find a better name, like
refcounted_folio, later for these non-rmappable compound pages.Quoted 6 lines
Quoted 2 lines
They wouldn't really be folios, I guess. They would likely be a simple "refcounted" memtype that allows for compound pages.
Yes, they are not folios. But “folio” was started to replace “compound page” and slowly becomes rmappable anon and file-backed. People outside MM still thinks “folio” == “compound page”.
Yes, it's a bit of a mess now.
Quoted 8 lines
Quoted 4 lines
But once I manage to remove
PG_private
and get usPG_folio,page_folio()will return NULL for non-folio compound pages and we will need a new type for them, “refcounted_XXX”. We can decide XXX when I get there. :)So far willy did not document a type that just has a refcount.
I was going to say.
It seems a bit specific, because different things might have a different use for 'pages with refcounts that shouldn't be touched by core mm' and different semantics perhaps.
Obviously if it was a different memdesc type then it'd not be a folio.
Quoted 11 lines
Maybe "Managed memory" (struct mgdesc) could be the right thing for some memory with a refcount.
The question is, if the use case at hand would even require a refcount, of if frozen pages would be good enough.
Quoted 4 lines
Quoted 4 lines
But what is the conclusion here? It sounds like "
folio_split_" is the entirely wrong interface for these compound pages.
Yeah the thing is we need some clarification on what is a folio exactly.
A folio is a physically, virtually and logically contiguous set of bytes. It is a power-of-two in size, and it is aligned to that same power-of-two. It is at least as large as %PAGE_SIZE. If it is in the page cache, it is at a file offset which is a multiple of that power-of-two. It may be mapped into userspace at an address which is at an arbitrary page offset, but its kernel virtual address is aligned to its size.
By that definition a compound page _is_ a folio. And anything that _could_ be mapped into userland.
But if we are going to say folio == rmappable then can we actually update the description of folio to say so?
Or perhaps reference the fact that 'transitionally' it is as above, but in future will mean only rmappable.
Quoted 10 lines
Quoted 8 lines
We probably would allow
folio_split()to be used on compound pages now until we can make a clean distinction, e.g., usingPG_folio, between them.Yes,
folio_split()and its helper functions are meant for rmappable anon and file-backed folios. But currently “folio” is de facto “compound page”, since for exampleprep_compound_head()initializes folio fields even if it is meant only for compound pages.If we can stop more abuse right from the start, that would be nice.
Yes.
Quoted 3 lines
We do have
split_page()that splits a non-compound higher-order page. Maybe we want asplit_compound_page(), or allow forsplit_page()to accept compound pages.
Ha! I read this after making this very same suggestion. High.. 5?
But yeah obviously agree.
Quoted 3 lines
Because splitting a folio is really something different than splitting just some compound page (no mapping/pagecache/whatever involved).
Yup.
Quoted 5 lines
--
Cheers,David
Cheers, Lorenzo
Zi Yan
reviewerQuoted 33 lines · Lorenzo Stoakes (ARM)
Quoted 23 lines · David Hildenbrand (Arm)
Quoted 13 lines · Zi Yan
Quoted 11 lines · David Hildenbrand (Arm)
Quoted 9 lines · Zi Yan
Strickly speaking, these vm_insert*() compound pages are not folios, since folios are supposed to be rmappable and they are either anonymous memory or file-backed memory. I am working on separating them from rmappable folios by replacing
PG_privatewithPG_folioand marking all pages in a folio withPG_folioinpage_rmappable_folio().Hopefully, we can find a better name, like
refcounted_folio, later for these non-rmappable compound pages.Quoted 6 lines
Quoted 2 lines
They wouldn't really be folios, I guess. They would likely be a simple "refcounted" memtype that allows for compound pages.
Yes, they are not folios. But “folio” was started to replace “compound page” and slowly becomes rmappable anon and file-backed. People outside MM still thinks “folio” == “compound page”.
Yes, it's a bit of a mess now.
Quoted 8 lines
Quoted 4 lines
But once I manage to remove
PG_private
and get usPG_folio,page_folio()will return NULL for non-folio compound pages and we will need a new type for them, “refcounted_XXX”. We can decide XXX when I get there. :)So far willy did not document a type that just has a refcount.
Quoted 7 lines
I was going to say.
It seems a bit specific, because different things might have a different use for 'pages with refcounts that shouldn't be touched by core mm' and different semantics perhaps.
Obviously if it was a different memdesc type then it'd not be a folio.
Yes, this part is easy. Slab uses GFP_COMP and no one regards large slab pages as folios. The confusion comes from large pages without a page_type.
Quoted 4 lines
Quoted 4 lines
Maybe "Managed memory" (struct mgdesc) could be the right thing for some memory with a refcount.
For driver allocated memory, probably yes. I also see allocations listed below with GFP_KERNEL_ACCOUNT | __GFP_COMP, so AcctMem would also be a candidate.
1. arch/s390/kvm/dat.c
2. io_uring/memmap.c
3. drivers/dma-buf/heaps/system_heap.c when mem_accounting is enabled.
Quoted 3 lines
Quoted 3 lines
The question is, if the use case at hand would even require a refcount, of if frozen pages would be good enough.
I hope so and that could simplify our work without handling refcount. But I assume user outside MM would still want a refcount to do the page life time management work, otherwise they might need to build their own one(s).
Quoted 16 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
But what is the conclusion here? It sounds like "
folio_split_" is the entirely wrong interface for these compound pages.Yeah the thing is we need some clarification on what is a folio exactly.
A folio is a physically, virtually and logically contiguous set of bytes. It is a power-of-two in size, and it is aligned to that same power-of-two. It is at least as large as %PAGE_SIZE. If it is in the page cache, it is at a file offset which is a multiple of that power-of-two. It may be mapped into userspace at an address which is at an arbitrary page offset, but its kernel virtual address is aligned to its size.
Quoted 8 lines
By that definition a compound page _is_ a folio. And anything that _could_ be mapped into userland.
But if we are going to say folio == rmappable then can we actually update the description of folio to say so?
Or perhaps reference the fact that 'transitionally' it is as above, but in future will mean only rmappable.
I can come up with a patch clarifying the comment.
Quoted 20 lines
Quoted 11 lines
Quoted 9 lines
We probably would allow
folio_split()to be used on compound pages now until we can make a clean distinction, e.g., usingPG_folio, between them.Yes,
folio_split()and its helper functions are meant for rmappable anon and file-backed folios. But currently “folio” is de facto “compound page”, since for exampleprep_compound_head()initializes folio fields even if it is meant only for compound pages.If we can stop more abuse right from the start, that would be nice.
Yes.
Quoted 3 lines
We do have
split_page()that splits a non-compound higher-order page. Maybe we want asplit_compound_page(), or allow forsplit_page()to accept compound pages.Ha! I read this after making this very same suggestion. High.. 5?
Quoted 7 lines
But yeah obviously agree.
Quoted 3 lines
Because splitting a folio is really something different than splitting just some compound page (no mapping/pagecache/whatever involved).
Yup.
It sounds like split_compound_page() gets some love (David, Lorenzo, and Matthew all want it). I can add one to mm/page_alloc.c, so that Matthew can build his patchset on top of it.
Best Regards,
Yan, Zi
David Hildenbrand (Arm)
maintainerQuoted 9 lines
Quoted 5 lines
Quoted 3 lines
Because splitting a folio is really something different than splitting just some compound page (no mapping/pagecache/whatever involved).
Yup.
It sounds like
split_compound_page()gets some love (David, Lorenzo, and Matthew all want it). I can add one tomm/page_alloc.c, so that Matthew can build his patchset on top of it.
In there we should verify that it is neither slab nor a folio. For now, we can only test for folios indirectly by considering the large_rmappable flag and hugetlb page type.
Likely we should simply disallow any page type for now (and large_rmappable).
Signature · 3 lines
Cheers, David
I know different people have different approaches but PLEASE cc- everybody on the whole series for context :)
You may annoy the odd few who want less mail > context but that's the best way I think.
Quoted 3 lines · Matthew Brost
Add a lightweight structural split primitive for large (compound) folios that a driver allocated with
__GFP_COMPand manages entirely by itself, outside of the core mm's view.
I don't know why you are stuck on the driver thing here, 'compound page split' (with a clarification it's not about folios) is clearer.
Maybe split_compound_page() :)
Quoted 12 lines
The existing split paths -
split_folio()andfolio_split_unmapped()- are built for folios that the mm owns: they perform a refcount freeze, walk and remap the rmap, and take theanon_vma/i_mmaplocks, andfolio_split_unmapped()further assumes an anon, pagecache-style refcount model (nr_pages+ 1). None of that applies to a folio that is:- singly referenced (the caller holds the only reference),
- not mapped through the rmap (folio_mapped()== 0),
- not in the page cache or swap cache (folio->mapping== NULL),
- not on any LRU or the deferred-split list.
Quoted 8 lines
For such a folio the split is purely structural: because nothing else in the kernel can reach it, there is no need to freeze the refcount or touch any mapping.
folio_split_driver_managed()therefore performs only the compound and split-accounting teardown via__split_unmapped_folio()and then hands each resulting order-@new_order folio its own reference, mirroringsplit_page()for compound folios. The caller keeps the original reference on the first resulting folio and is responsible for freeing all of them individually.
Can we human-ify this wall of text please :) It's very dense.
Clarity and succinctness matter.
I see a wall of text like this (very common now in the LLM era) and I just don't want to read it, honestly.
Quoted 5 lines
The immediate user is TTM's GPU page pool, which allocates higher-order compound pages, maps them into userspace via
VM_PFNMAP(never through the rmap), and needs to split them into order-0 folios under memory pressure so pages can be backed up to shmem and freed one at a time.
OK so a user of the large pfnmap stuff?
Quoted 3 lines
A
CONFIG_TRANSPARENT_HUGEPAGE=nstub is provided so callers can build without the split machinery; it warns and returns-EINVAL.
Can build without the split machinery but get a kernel warning? Huh?
Quoted 21 lines
Cc Maarten Lankhorst <[email protected]>Cc Maxime Ripard <[email protected]>Cc Thomas Zimmermann <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Christian Koenig <[email protected]>Cc Huang Rui <[email protected]>Cc Matthew Auld <[email protected]>Cc Matthew Brost <[email protected]>Cc Andrew Morton <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lorenzo Stoakes <[email protected]>Cc Zi Yan <[email protected]>Cc Baolin Wang <[email protected]>Cc "Liam R. Howlett" <[email protected]>Cc Nico Pache <[email protected]>Cc Ryan Roberts <[email protected]>Cc Dev Jain <[email protected]>Cc Barry Song <[email protected]>Cc Lance Yang <[email protected]>
Quoted 8 lines
Cc Tvrtko Ursulin <[email protected]>Cc Dave Airlie <[email protected]>Suggested-by Matthew Wilcox <[email protected]>Signed-off-by Matthew Brost <[email protected]>Assisted-by GitHub-Copilot:claude-opus-4.8
<an aside>
Both? :)
Hopefully Brauner's patch lands and you could just put 'LLM' here honestly.
And ideally something like:
Or whatever the contribution was after the # :P
Quoted 6 lines
---
The patch is based on drm-tip rather than the core MM branches to facilitate Intel CI testing and initial review. It can be rebased onto the core MM branches in a subsequent revision.
This should have been sent as an RFC overall very clearly, but definitely if you're going to play around with bases like that even more so.
I wouldn't imagine making a core change to DRM and basing it on an mm tree :)
Quoted 18 lines
--- include/linux/huge_mm.h | 8 ++++++ mm/huge_memory.c | 63 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+)include/linux/huge_mm.h +1
-0@@ -402,6 +402,7 @@ enum split_type {int __split_huge_page_to_list_to_order (struct page *page,struct list_head *list,unsigned int new_order);int folio_split_unmapped (struct folio *folio,unsigned int new_order); +int folio_split_driver_managed (struct folio *folio,unsigned int new_order);unsigned int min_order_for_split (struct folio *folio);int split_folio_to_list (struct folio *folio,struct list_head *list); int folio_check_splittable(struct folio *folio, unsigned int new_order,
include/linux/huge_mm.h static inline int split_folio_to_list(struct folio *folio, struct list_head *lis · line 664
Quoted 8 lines
include/linux/huge_mm.h +5
-0@@ -656,6 +657,13 @@ static inline int split_folio_to_list(struct folio *folio, struct list_head *lisreturn -EINVAL ; } +static inline int folio_split_driver_managed (struct folio *folio, +unsigned int new_order) +{ +VM_WARN_ON_ONCE_FOLIO (1 , folio); +return -EINVAL ;
Going to fire a warning every call? You were saying that your own use case involves !CONFIG_THP builds?
And since this is being touched by drivers, drop the VM_ on the warnings.
Quoted 10 lines
include/linux/huge_mm.h +2
-0+} +static inline int folio_split (struct folio *folio,unsigned int new_order,struct page *page,struct list_head *list) {mm/huge_memory.c +0
-0
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4191
Quoted 7 lines
mm/huge_memory.c +4
-0@@ -4185,6 +4185,69 @@ int folio_split_unmapped(struct folio *folio, unsigned int new_order)return ret; } +/** +* folio_split_driver_managed() - split an exclusively-owned, off-LRU folio +* @folio: folio to split. Must be a large (compound) folio that is owned +* exclusively by the caller and is invisible to the core mm.
Yeah I find this 'invisible to core mm' aspect really sketchy.
In what way is this invisible to core mm? You mean !rmappable fundamentally right?
But it's reference counted?
It's clearly not invisible to mm except in specific ways?
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4195
Quoted 4 lines
mm/huge_memory.c +4
-0+* @new_order: the order of the folios after the split. +* +* This is a lightweight structural split for folios that a driver allocated +* and manages itself (for example TTM's GPU page pool, which allocates
Again no, please no. Maybe 'non-mm owned' but not 'driver managed'...
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4197
Quoted 2 lines
mm/huge_memory.c +2
-0+* higher-order compound pages with __GFP_COMP and maps them into userspace +* via VM_PFNMAP rather than through the rmap). Such folios are:
We don't need this example in the comment thanks.
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4199
Quoted 2 lines
mm/huge_memory.c +2
-0+* +* - singly referenced (the caller holds the only reference),
Again seems really super specific to your use case. We can't add a general mm feature for drm alone.
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4200
Quoted 1 line
mm/huge_memory.c +1
-0+* - not mapped through the rmap (folio_mapcount() == 0),
The parens bit is pointless and you're writing here what's in the code.
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4201
Quoted 1 line
mm/huge_memory.c +1
-0+* - not in the page cache or swap cache (folio->mapping == NULL),
Err what? folio->mapping is literally the link to the rmap (anon or file-backed).
Again the parens bit is useless.
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4202
Quoted 1 line
mm/huge_memory.c +1
-0+* - not on any LRU or the deferred-split list.
Thi sseems like a specific use case though? You're using PFN map presumably because in this case you have pages whose refcount you don't want touched by core mm but here you do want touched :)
I mean we need this to make sense for general classes of allocated memory.
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4210
Quoted 8 lines
mm/huge_memory.c +8
-0+* +* Because nothing else in the kernel can reach the folio, this helper does +* not perform the refcount freeze / remap / anon_vma & i_mmap locking dance +* that split_folio() and folio_split_unmapped() require. It performs only +* the compound and split-accounting teardown and then hands each resulting +* folio its own reference, mirroring split_page() for compound folios. +* +* The caller is responsible for freeing the resulting folios individually.
Overall this comment is a wall of LLM-ish dense text, please human-ify. Use paragraphs, try to be succinct clear etc.
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4215
Quoted 5 lines
mm/huge_memory.c +5
-0+* +* Context: caller holds the only reference and excludes concurrent access. +* Does not sleep. +* +* Return: 0 on success, negative errno on failure.
Nitty but 'error on failure' suffices I thnk :)
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4217
Quoted 2 lines
mm/huge_memory.c +2
-0+*/ +int folio_split_driver_managed (struct folio *folio,unsigned int new_order)
split_compound_page() still my preference :)
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4221
Quoted 4 lines
mm/huge_memory.c +4
-0+{ +unsigned int old_order =folio_order (folio); +unsigned int split_nr =1U << new_order; +unsigned int nr =1U << old_order;
nitty but could be folio_nr_pages(folio). Not a big deal.
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4227
Quoted 6 lines
mm/huge_memory.c +6
-0+unsigned int i; + +if (new_order >= old_order) +return -EINVAL ; + +VM_WARN_ON_ONCE_FOLIO (folio_ref_count (folio) !=1 , folio);
This is meant to be a general accessor, what if an owner of a folio happens to set the refcount > 1? You'd probably rather just return an error no?
This one doesn't seem to be in the same class of 'programmatic error' as the rest.
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4231
Quoted 4 lines
mm/huge_memory.c +4
-0+VM_WARN_ON_ONCE_FOLIO (folio_mapped (folio), folio); +VM_WARN_ON_ONCE_FOLIO (folio->mapping, folio); +VM_WARN_ON_ONCE_FOLIO (folio_test_swapcache (folio), folio); +VM_WARN_ON_ONCE_FOLIO (folio_test_lru (folio), folio);
Firstly, I think you should bail if any of these are not true, rather than just warning if CONFIG_DEBUG_VM and carrying on.
ESPECIALLY since drivers are involved.
Also some comments here would help! It's like too much above, not enough here :)
And I think these should be actual warnings, drivers might not ever enable CONFIG_DEBUG_VM, and in any case these VM_xxx() warnings are for internal mm stuff, not stuff controllable from a caller who might do crazy things (TM).
I think often for this kind of thing it makes sense to have a separate 'check_split_compound_folio_sane()' or something like that for this.
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4238
Quoted 7 lines
mm/huge_memory.c +7
-0+ +/* +* Structural + split-accounting teardown only. No mapping/xarray, no +* refcount freeze: the folio is frozen-by-ownership already. +*/ +__split_unmapped_folio (folio, new_order, &folio->page,NULL ,NULL , +SPLIT_TYPE_UNIFORM );
__split_unmapped_folio() -> __split_folio_to_order() ->
for (i = new_nr_pages; i < nr_pages; i += new_nr_pages) {
...
clear_compound_head(new_head);
if (new_order) {
prep_compound_page(new_head, new_order);
folio_set_large_rmappable(new_folio); <--- oops it's rmappable now
}So this is a problem.
Maybe you want to split to order-0 always? Otherwise this needs a real rethink.
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4242
Quoted 4 lines
mm/huge_memory.c +4
-0+ +/* +* Give every resulting head folio its own reference. The original +* reference stays on the first one, exactly like split_page().
Again, split_compound_page() :)
I'm not sure this reference to split_page() is useful.
How about:
/* Retain reference on first folio, set on new head folios. */
?
mm/huge_memory.c int folio_split_unmapped(struct folio *folio, unsigned int new_order) · line 4249
Quoted 8 lines
mm/huge_memory.c +7
-0+*/ +for (i = split_nr; i < nr; i += split_nr) +set_page_refcounted (folio_page (folio, i)); + +return 0 ; +} +EXPORT_SYMBOL_GPL (folio_split_driver_managed);
Presumably you absolutely require this for module-built DRM driver?
Quoted 6 lines
mm/huge_memory.c +1
-0+/* * This function splits a large folio into smaller folios of order @new_order. * @page can point to any page of the large folio to split. The split operation
--
2.34.
Cheers, Lorenzo
Quoted 5 lines · Lorenzo Stoakes (ARM)
I know different people have different approaches but PLEASE cc- everybody on the whole series for context :)
You may annoy the odd few who want less mail > context but that's the best way I think.
OK sorry either I wasn't paying attention, the mail sent slow, or I am blind :P but I see I'm cc'd on the rest.
Apologies!
Cheers, Lorenzo