mm: support device-private THP
Subsystems (15)
v6 cover mm: support device-private THP
This patch series introduces support for Transparent Huge Page (THP) migration in zone device-private memory. The implementation enables efficient migration of large folios between system memory and device-private memory
Background
Current zone device-private memory implementation only supports PAGE_SIZE granularity, leading to:
- Increased TLB pressure
- Inefficient migration between CPU and device memory
This series extends the existing zone device-private infrastructure to support THP, leading to:
- Reduced page table overhead
- Improved memory bandwidth utilization
- Seamless fallback to base pages when needed
In my local testing (using lib/test_hmm) and a throughput test, the series shows a 350% improvement in data transfer throughput and a 80% improvement in latency
These patches build on the earlier posts by Ralph Campbell [1]
Two new flags are added in vma_migration to select and mark compound pages. migrate_vma_setup(), migrate_vma_pages() and migrate_vma_finalize() support migration of these pages when MIGRATE_VMA_SELECT_COMPOUND is passed in as arguments.
The series also adds zone device awareness to (m)THP pages along with fault handling of large zone device private pages. page vma walk and the rmap code is also zone device aware. Support has also been added for folios that might need to be split in the middle of migration (when the src and dst do not agree on MIGRATE_PFN_COMPOUND), that occurs when src side of the migration can migrate large pages, but the destination has not been able to allocate large pages. The code supported and used folio_split() when migrating THP pages, this is used when MIGRATE_VMA_SELECT_COMPOUND is not passed as an argument to migrate_vma_setup().
The test infrastructure lib/test_hmm.c has been enhanced to support THP migration. A new ioctl to emulate failure of large page allocations has been added to test the folio split code path. hmm-tests.c has new test cases for huge page migration and to test the folio split path. A new throughput test has been added as well.
The nouveau dmem code has been enhanced to use the new THP migration capability.
mTHP support:
The patches hard code, HPAGE_PMD_NR in a few places, but the code has been kept generic to support various order sizes. With additional refactoring of the code support of different order sizes should be possible.
The future plan is to post enhancements to support mTHP with a rough design as follows:
1. Add the notion of allowable thp orders to the HMM based test driver
2. For non PMD based THP paths in migrate_device.c, check to see if a suitable order is found and supported by the driver
3. Iterate across orders to check the highest supported order for migration
4. Migrate and finalize
The mTHP patches can be built on top of this series, the key design elements that need to be worked out are infrastructure and driver support for multiple ordered pages and their migration.
HMM support for large folios:
Currently in mm-unstable [4]
References:
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://lore.kernel.org/linux-mm/[email protected]/T/
[3] https://lore.kernel.org/lkml/[email protected]/
[4] https://lkml.kernel.org/r/[email protected]
[5] https://lore.kernel.org/lkml/[email protected]/
[6] https://lore.kernel.org/lkml/[email protected]/
[7] https://lore.kernel.org/lkml/[email protected]/
[8] https://lore.kernel.org/all/[email protected]/
These patches are built on top of mm/mm-new
Changelog v6 [8]:
- Rebased against mm/mm-new after fixing the following
- Two issues reported by kernel test robot
- m68k requires an lvalue for pmd_present()
- BUILD_BUG_ON() issues when THP is disabled
- kernel doc warnings reported on linux-next
- Thanks Stephen Rothwell!
- smatch fixes and issues reported
- Fix issue with potential NULL page
- Report about young being uninitialized for device-private pages in __split_huge_pmd_locked()
- Several Review comments from David Hildenbrand
- Indentation changes and style improvements
- Removal of some unwanted extra lines
- Introduction of new helper function is_pmd_non_present_folio_entry() to represent migration and device private pmd's
- Code flow refactoring into migration and device private paths
- More consistent use of helper function is_pmd_device_private()
- Review comments from Mika Penttilä
- folio_get() is not required for huge_pmd prior to split
Changelog v5 [7] :
- Rebased against mm/mm-new (resolved conflict caused by MIGRATEPAGE_SUCCESS removal)
- Fixed a kernel-doc warning reported by kernel test robot
Changelog v4 [6] :
- Addressed review comments
- Split patch 2 into a smaller set of patches
- PVMW_THP_DEVICE_PRIVATE flag is no longer present
- damon/page_idle and other page_vma_mapped_walk paths are aware of device-private folios
- No more flush for non-present entries in set_pmd_migration_entry
- Implemented a helper function for migrate_vma_split_folio() which splits large folios if seen during a pte walk
- Removed the controversial change for folio_ref_freeze using folio_expected_ref_count()
- Removed functions invoked from with VM_WARN_ON
- New test cases and fixes from Matthew Brost
- Fixed bugs reported by kernel test robot (Thanks!)
- Several fixes for THP support in nouveau driver
Changelog v3 [5] :
- Addressed review comments
- No more split_device_private_folio() helper
- Device private large folios do not end up on deferred scan lists
- Removed THP size order checks when initializing zone device folio
- Fixed bugs reported by kernel test robot (Thanks!)
Changelog v2 [3] :
- Several review comments from David Hildenbrand were addressed, Mika, Zi, Matthew also provided helpful review comments
- In paths where it makes sense a new helper is_pmd_device_private_entry() is used
- anon_exclusive handling of zone device private pages in split_huge_pmd_locked() has been fixed
- Patches that introduced helpers have been folded into where they are used
- Zone device handling in mm/huge_memory.c has benefited from the code and testing of Matthew Brost, he helped find bugs related to copy_huge_pmd() and partial unmapping of folios.
- Zone device THP PMD support via page_vma_mapped_walk() is restricted to try_to_migrate_one()
- There is a new dedicated helper to split large zone device folios
Changelog v1 [2]:
- Support for handling fault_folio and using trylock in the fault path
- A new test case has been added to measure the throughput improvement
- General refactoring of code to keep up with the changes in mm
- New split folio callback when the entire split is complete/done. The callback is used to know when the head order needs to be reset.
Testing:
- Testing was done with ZONE_DEVICE private pages on an x86 VM
Balbir Singh (14):
mm/zone_device: support large zone device private folios mm/huge_memory: add device-private THP support to PMD operations mm/rmap: extend rmap and migration support device-private entries mm/huge_memory: implement device-private THP splitting mm/migrate_device: handle partially mapped folios during collection mm/migrate_device: implement THP migration of zone device pages mm/memory/fault: add THP fault handling for zone device private pages lib/test_hmm: add zone device private THP test infrastructure mm/memremap: add driver callback support for folio splitting mm/migrate_device: add THP splitting during migration lib/test_hmm: add large page allocation failure testing selftests/mm/hmm-tests: new tests for zone device THP migration selftests/mm/hmm-tests: new throughput tests including THP gpu/drm/nouveau: enable THP support for GPU memory migration
Matthew Brost (1):
selftests/mm/hmm-tests: partial unmap, mremap and anon_write tests
drivers/gpu/drm/nouveau/nouveau_dmem.c | 304 +++++--- drivers/gpu/drm/nouveau/nouveau_svm.c | 6 +- drivers/gpu/drm/nouveau/nouveau_svm.h | 3 +- include/linux/huge_mm.h | 18 +- include/linux/memremap.h | 51 +- include/linux/migrate.h | 2 + include/linux/mm.h | 1 + include/linux/swapops.h | 32 + lib/test_hmm.c | 443 +++++++++--- lib/test_hmm_uapi.h | 3 + mm/damon/ops-common.c | 20 +- mm/huge_memory.c | 292 ++++++-- mm/memory.c | 5 +- mm/memremap.c | 34 +- mm/migrate_device.c | 611 ++++++++++++++-- mm/page_idle.c | 7 +- mm/page_vma_mapped.c | 7 + mm/pgtable-generic.c | 2 +- mm/rmap.c | 27 +- tools/testing/selftests/mm/hmm-tests.c | 919 +++++++++++++++++++++++-- 20 files changed, 2392 insertions(+), 395 deletions(-)
Signature · 1 line
2.50.1
PATCH v6 1/15 mm/zone_device: support large zone device private folios
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing page_free() callback in pgmap is called when the folio is freed, this is true for both PAGE_SIZE and higher order pages.
Zone device private large folios do not support deferred split and scan like normal THP folios.
--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)
include/linux/memremap.h +9 -1
@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);
@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 ); +} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)
mm/memremap.c +21 -13
@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/*
@@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :
@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 ); + +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ -WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + } } -EXPORT_SYMBOL_GPL (zone_device_page_init); +EXPORT_SYMBOL_GPL (zone_device_folio_init);
mm/rmap.c +5 -1
@@ -1769,9 +1769,13 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,* the folio is unmapped and at least one page is still mapped. * * Check partially_mapped first to ensure it is a large folio. +* +* Device private folios do not support deferred splitting and +* shrinker based scanning of the folios to free. */ if (partially_mapped &&folio_test_anon (folio) && - !folio_test_partially_mapped (folio)) + !folio_test_partially_mapped (folio) && + !folio_is_device_private (folio))deferred_split_folio (folio, true);__folio_mod_stat (folio, -nr, -nr_pmdmapped);
Signature · 1 line
2.50.1
Zi Yan
reviewerRe: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 56 lines · Balbir Singh
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);
include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );
I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio) above it would be useful to detect misuse.
Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );
It is strange to see a folio is converted back to page in a function called zone_device_folio_init().
Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }
OK, so basically, @folio is not a compound page yet when zone_device_folio_init() is called.
I feel that your zone_device_page_init() and zone_device_folio_init() implementations are inverse. They should follow the same pattern as __alloc_pages_noprof() and __folio_alloc_noprof(), where zone_device_page_init() does the actual initialization and zone_device_folio_init() just convert a page to folio.
Something like:
void zone_device_page_init(struct page *page, unsigned int order)
{
VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES);
/*
* Drivers shouldn't be allocating pages after calling
* memunmap_pages().
*/
WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order));
/*
* anonymous folio does not support order-1, high order file-backed folio
* is not supported at all.
*/
VM_WARN_ON_ONCE(order == 1);
if (order > 1)
prep_compound_page(page, order);
/* page has to be compound head here */
set_page_count(page, 1);
lock_page(page);
}void zone_device_folio_init(struct folio *folio, unsigned int order)
{
struct page *page = folio_page(folio, 0);
zone_device_page_init(page, order);
page_rmappable_folio(page);
}Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{
zone_device_page_init(page, order);
return page_rmappable_folio(page);
}Then, it comes to free_zone_device_folio() above,
I feel that pgmap->ops->page_free() should take an additional order parameter to free a compound page like free_frozen_pages().
This is my impression after reading the patch and zone device page code.
Alistair and David can correct me if this is wrong, since I am new to zone device page code.
Quoted 24 lines
mm/memremap.c +1
-1} -EXPORT_SYMBOL_GPL (zone_device_page_init); +EXPORT_SYMBOL_GPL (zone_device_folio_init);mm/rmap.c +5
-1@@ -1769,9 +1769,13 @@ static __always_inline void __folio_remove_rmap(struct folio *folio,* the folio is unmapped and at least one page is still mapped. * * Check partially_mapped first to ensure it is a large folio. +* +* Device private folios do not support deferred splitting and +* shrinker based scanning of the folios to free. */ if (partially_mapped &&folio_test_anon (folio) && - !folio_test_partially_mapped (folio)) + !folio_test_partially_mapped (folio) && + !folio_is_device_private (folio))deferred_split_folio (folio, true);__folio_mod_stat (folio, -nr, -nr_pmdmapped);Signature · 1 line
2.50.1
--
Best Regards,
Yan, Zi
Re: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 209 lines · Zi Yan
Quoted 56 lines · Balbir Singh
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().
Quoted 5 lines
This is my impression after reading the patch and zone device page code.
Alistair and David can correct me if this is wrong, since I am new to zone device page code.
Thanks, I did not want to change zone_device_page_init() for several drivers (outside my test scope) that already assume it has an order size of 0.
Balbir Singh
Zi Yan
reviewerRe: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 210 lines · Balbir Singh
Quoted 209 lines · Zi Yan
Quoted 56 lines · Balbir Singh
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().
Quoted 8 lines
Quoted 5 lines
This is my impression after reading the patch and zone device page code.
Alistair and David can correct me if this is wrong, since I am new to zone device page code.
Thanks, I did not want to change
zone_device_page_init()for several drivers (outside my test scope) that already assume it has an order size of 0.
But my proposed zone_device_page_init() should still work for order-0 pages. You just need to change call site to add 0 as a new parameter.
One strange thing I found in the original zone_device_page_init() is the use of page_pgmap() in WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)). page_pgmap() calls page_folio() on the given page to access pgmap field. And pgmap field is only available in struct folio. The code initializes struct page, but in middle it suddenly finds the page is actually a folio, then treat it as a page afterwards. I wonder if it can be done better.
This might be a question to Alistair, since he made the change.
Best Regards,
Yan, Zi
Re: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 218 lines · Zi Yan
Quoted 216 lines · Balbir Singh
Quoted 214 lines · Zi Yan
Quoted 56 lines · Balbir Singh
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().This is my impression after reading the patch and zone device page code.
Alistair and David can correct me if this is wrong, since I am new to zone device page code.
Quoted 6 lines
Quoted 2 lines
Thanks, I did not want to change
zone_device_page_init()for several drivers (outside my test scope) that already assume it has an order size of 0.But my proposed
zone_device_page_init()should still work for order-0 pages. You just need to change call site to add 0 as a new parameter.
I did not want to change existing callers (increases testing impact) without a strong reason.
Quoted 11 lines
One strange thing I found in the original
zone_device_page_init()is the use ofpage_pgmap()inWARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)).page_pgmap()callspage_folio()on the given page to access pgmap field. And pgmap field is only available in struct folio. The code initializes struct page, but in middle it suddenly finds the page is actually a folio, then treat it as a page afterwards. I wonder if it can be done better.This might be a question to Alistair, since he made the change.
I'll let him answer it :)
Thanks for the review
Balbir
Re: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 238 lines · Balbir Singh
Quoted 218 lines · Zi Yan
Quoted 216 lines · Balbir Singh
Quoted 214 lines · Zi Yan
Quoted 56 lines · Balbir Singh
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().This is my impression after reading the patch and zone device page code.
Alistair and David can correct me if this is wrong, since I am new to zone device page code.
Quoted 6 lines
Quoted 2 lines
Thanks, I did not want to change
zone_device_page_init()for several drivers (outside my test scope) that already assume it has an order size of 0.But my proposed
zone_device_page_init()should still work for order-0 pages. You just need to change call site to add 0 as a new parameter.I did not want to change existing callers (increases testing impact) without a strong reason.
Quoted 9 lines
One strange thing I found in the original
zone_device_page_init()is the use ofpage_pgmap()inWARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)).page_pgmap()callspage_folio()on the given page to access pgmap field. And pgmap field is only available in struct folio. The code initializes struct page, but in middle it suddenly finds the page is actually a folio, then treat it as a page afterwards. I wonder if it can be done better.
Quoted 4 lines
Quoted 2 lines
This might be a question to Alistair, since he made the change.
I'll let him answer it :)
Not him, but I think this goes back to my question raised in my other reply: When would we allocate "struct folio" in the future.
If it's "always" then actually most of the zone-device code would only ever operate on folios and never on pages in the future.
I recall during a discussion at LSF/MM I raised that, and the answer was (IIRC) that we will allocate "struct folio" as we will initialize the memmap for dax.
So essentially, we'd always have folios and would never really have to operate on pages.
Signature · 3 lines
Cheers David / dhildenb
Zi Yan
reviewerRe: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 244 lines · David Hildenbrand
Quoted 238 lines · Balbir Singh
Quoted 218 lines · Zi Yan
Quoted 216 lines · Balbir Singh
Quoted 214 lines · Zi Yan
Quoted 56 lines · Balbir Singh
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().This is my impression after reading the patch and zone device page code.
Alistair and David can correct me if this is wrong, since I am new to zone device page code.
Quoted 6 lines
Quoted 2 lines
Thanks, I did not want to change
zone_device_page_init()for several drivers (outside my test scope) that already assume it has an order size of 0.But my proposed
zone_device_page_init()should still work for order-0 pages. You just need to change call site to add 0 as a new parameter.I did not want to change existing callers (increases testing impact) without a strong reason.
Quoted 9 lines
One strange thing I found in the original
zone_device_page_init()is the use ofpage_pgmap()inWARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)).page_pgmap()callspage_folio()on the given page to access pgmap field. And pgmap field is only available in struct folio. The code initializes struct page, but in middle it suddenly finds the page is actually a folio, then treat it as a page afterwards. I wonder if it can be done better.Quoted 4 lines
Quoted 2 lines
This might be a question to Alistair, since he made the change.
I'll let him answer it :)
Quoted 7 lines
Not him, but I think this goes back to my question raised in my other reply: When would we allocate "struct folio" in the future.
If it's "always" then actually most of the zone-device code would only ever operate on folios and never on pages in the future.
I recall during a discussion at LSF/MM I raised that, and the answer was (IIRC) that we will allocate "struct folio" as we will initialize the memmap for dax.
So essentially, we'd always have folios and would never really have to operate on pages.
Hmm, then what is the point of having “struct folio”, which originally is added to save compound_head() calls, where everything is a folio in device private world? We might need DAX people to explain the rationale of “always struct folio”.
Best Regards,
Yan, Zi
Alistair Popple
reviewerRe: [v6 01/15] mm/zone_device: support large zone device private folios
On 2025-09-25 at 03:49 +1000, Zi Yan <[email protected]> wrote...
Quoted 140 lines · David Hildenbrand
Quoted 139 lines · Balbir Singh
Quoted 137 lines · Zi Yan
Quoted 136 lines · Balbir Singh
Quoted 135 lines · Zi Yan
Quoted 133 lines · Balbir Singh
Quoted 57 lines
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().
Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 496
Quoted 8 lines
mm/memremap.c +4
-2*/ -WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) {
Why is this only called for order > 1 rather than order > 0 ?
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 3 lines
mm/memremap.c +3
-0+prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */
I guess that answers my question :-)
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
This is my impression after reading the patch and zone device page code.
Alistair and David can correct me if this is wrong, since I am new to zone device page code.
Thanks, I did not want to change
zone_device_page_init()for several drivers (outside my test scope) that already assume it has an order size of 0.
It's a trivial change, so I don't think avoiding changes to other drivers should be a concern.
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
But my proposed
zone_device_page_init()should still work for order-0 pages. You just need to change call site to add 0 as a new parameter.I did not want to change existing callers (increases testing impact) without a strong reason.
Quoted 9 lines
One strange thing I found in the original
zone_device_page_init()is the use ofpage_pgmap()inWARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)).page_pgmap()callspage_folio()on the given page to access pgmap field. And pgmap field is only available in struct folio. The code initializes struct page, but in middle it suddenly finds the page is actually a folio, then treat it as a page afterwards. I wonder if it can be done better.
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
This might be a question to Alistair, since he made the change.
Hello! I might be him :)
I think this situation is just historical - when I originally wrote zone_device_page_init() the pgmap was stored on the page rather than the folio. That only changed fairly recently with commit 82ba975e4c43 ("mm: allow compound zone device pages").
The reason pgmap is now only available on the folio is described in the commit log. The TLDR is switching FS DAX to use compound pages required page->compound_head to be available for use, and that was being shared with page->pgmap. So the solution was to move pgmap to the folio freeing up page->compound_head for use on tail pages.
The whole percpu pgmap->ref could actually now go away - I've debated removing it but haven't found the motivation as it provides a small advantage on driver tear down. Basically it just tracks how many pages are allocated in the pgmap so drivers could use that to determine if they need to trigger migrations before tearing down the pgmap.
The alternative is just to loop over every page in the pgmap to ensure the folio/page refcounts are 0 before tear down.
Quoted 9 lines
Quoted 9 lines
Quoted 3 lines
Quoted 1 line
I'll let him answer it :)
Not him, but I think this goes back to my question raised in my other reply: When would we allocate "struct folio" in the future.
If it's "always" then actually most of the zone-device code would only ever operate on folios and never on pages in the future.
I recall during a discussion at LSF/MM I raised that, and the answer was (IIRC) that we will allocate "struct folio" as we will initialize the memmap for dax.
Sounds about right.
Quoted 1 line
Quoted 1 line
So essentially, we'd always have folios and would never really have to operate on pages.
Yeah, I think I mentioned to Matthew at LSF/MM that I thought ZONE_DEVICE (and in particular ZONE_DEVICE_PRIVATE) might be a good candidate to experiment with removing struct pages entirely and switching to memdesc's or whatever. Because we should, in theory at least, only need to operate on folio's. But I'm still a little vague on the details how that would actually work. It's been on my TODO list for a while, so myabe I will try and look at it for LPC as a healthy bit of conference driven development.
Quoted 4 lines
Hmm, then what is the point of having “struct folio”, which originally is added to save
compound_head()calls, where everything is a folio in device private world? We might need DAX people to explain the rationale of “always struct folio”.
Longer term isn't there an aim to remove struct page? So I assumed moving to folio's was part of that effort. As you say though many of the clean-ups thus far related to switching ZONE_DEVICE to folios have indeed just been about removing compound_head() calls.
- Alistair
Quoted 2 lines
Best Regards,
Yan, Zi
Zi Yan
reviewerRe: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 298 lines · Alistair Popple
On 2025-09-25 at 03:49 +1000, Zi Yan <[email protected]> wrote...
Quoted 140 lines · David Hildenbrand
Quoted 139 lines · Balbir Singh
Quoted 137 lines · Zi Yan
Quoted 136 lines · Balbir Singh
Quoted 135 lines · Zi Yan
Quoted 133 lines · Balbir Singh
Quoted 57 lines
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 496
Quoted 8 lines
mm/memremap.c +4
-2*/ -WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) {Why is this only called for order > 1 rather than order > 0 ?
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 3 lines
mm/memremap.c +3
-0+prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */I guess that answers my question :-)
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
This is my impression after reading the patch and zone device page code.
Alistair and David can correct me if this is wrong, since I am new to zone device page code.
Thanks, I did not want to change
zone_device_page_init()for several drivers (outside my test scope) that already assume it has an order size of 0.It's a trivial change, so I don't think avoiding changes to other drivers should be a concern.
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
But my proposed
zone_device_page_init()should still work for order-0 pages. You just need to change call site to add 0 as a new parameter.I did not want to change existing callers (increases testing impact) without a strong reason.
Quoted 9 lines
One strange thing I found in the original
zone_device_page_init()is the use ofpage_pgmap()inWARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)).page_pgmap()callspage_folio()on the given page to access pgmap field. And pgmap field is only available in struct folio. The code initializes struct page, but in middle it suddenly finds the page is actually a folio, then treat it as a page afterwards. I wonder if it can be done better.Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
This might be a question to Alistair, since he made the change.
Hello! I might be him :)
I think this situation is just historical - when I originally wrote
zone_device_page_init()the pgmap was stored on the page rather than the folio. That only changed fairly recently with commit82ba975e4c43("mm: allow compound zone device pages").The reason pgmap is now only available on the folio is described in the commit log. The TLDR is switching FS DAX to use compound pages required
page->compound_headto be available for use, and that was being shared withpage->pgmap. So the solution was to move pgmap to the folio freeing uppage->compound_headfor use on tail pages.The whole percpu
pgmap->refcould actually now go away - I've debated removing it but haven't found the motivation as it provides a small advantage on driver tear down. Basically it just tracks how many pages are allocated in the pgmap so drivers could use that to determine if they need to trigger migrations before tearing down the pgmap.The alternative is just to loop over every page in the pgmap to ensure the folio/page refcounts are 0 before tear down.
Quoted 9 lines
Quoted 9 lines
Quoted 3 lines
Quoted 1 line
I'll let him answer it :)
Not him, but I think this goes back to my question raised in my other reply: When would we allocate "struct folio" in the future.
If it's "always" then actually most of the zone-device code would only ever operate on folios and never on pages in the future.
I recall during a discussion at LSF/MM I raised that, and the answer was (IIRC) that we will allocate "struct folio" as we will initialize the memmap for dax.
Sounds about right.
Quoted 1 line
Quoted 1 line
So essentially, we'd always have folios and would never really have to operate on pages.
Yeah, I think I mentioned to Matthew at LSF/MM that I thought
ZONE_DEVICE(and in particularZONE_DEVICE_PRIVATE) might be a good candidate to experiment with removing struct pages entirely and switching to memdesc's or whatever. Because we should, in theory at least, only need to operate on folio's. But I'm still a little vague on the details how that would actually work. It's been on my TODO list for a while, so myabe I will try and look at it for LPC as a healthy bit of conference driven development.
Quoted 6 lines
Quoted 4 lines
Hmm, then what is the point of having “struct folio”, which originally is added to save
compound_head()calls, where everything is a folio in device private world? We might need DAX people to explain the rationale of “always struct folio”.Longer term isn't there an aim to remove struct page? So I assumed moving to
Right. But my current impression based on my code reading and this patchset is that every device private page is a folio. To form a high order folio, each device private folio is converted to page, prep_compound*()’d, then converted back to folio. Based on what you said above, this weird conversion might be temporary until the code is switched to memdesc.
I am looking forward to more details on how device private will be switched to memdesc from you. :)
Quoted 3 lines
folio's was part of that effort. As you say though many of the clean-ups thus far related to switching
ZONE_DEVICEto folios have indeed just been about removingcompound_head()calls.
Best Regards,
Yan, Zi
Alistair Popple
reviewerRe: [v6 01/15] mm/zone_device: support large zone device private folios
On 2025-09-26 at 01:27 +1000, Zi Yan <[email protected]> wrote...
Quoted 307 lines · Alistair Popple
Quoted 299 lines
On 2025-09-25 at 03:49 +1000, Zi Yan <[email protected]> wrote...
Quoted 140 lines · David Hildenbrand
Quoted 139 lines · Balbir Singh
Quoted 137 lines · Zi Yan
Quoted 136 lines · Balbir Singh
Quoted 135 lines · Zi Yan
Quoted 133 lines · Balbir Singh
Quoted 57 lines
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 14 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 496
Quoted 8 lines
mm/memremap.c +4
-2*/ -WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) {Why is this only called for order > 1 rather than order > 0 ?
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 3 lines
mm/memremap.c +3
-0+prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
Quoted 6 lines
WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */I guess that answers my question :-)
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
Quoted 32 lines
VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines
This is my impression after reading the patch and zone device page code.
Alistair and David can correct me if this is wrong, since I am new to zone device page code.
Thanks, I did not want to change
zone_device_page_init()for several drivers (outside my test scope) that already assume it has an order size of 0.It's a trivial change, so I don't think avoiding changes to other drivers should be a concern.
Quoted 17 lines
Quoted 17 lines
Quoted 17 lines
Quoted 4 lines
But my proposed
zone_device_page_init()should still work for order-0 pages. You just need to change call site to add 0 as a new parameter.I did not want to change existing callers (increases testing impact) without a strong reason.
Quoted 9 lines
One strange thing I found in the original
zone_device_page_init()is the use ofpage_pgmap()inWARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)).page_pgmap()callspage_folio()on the given page to access pgmap field. And pgmap field is only available in struct folio. The code initializes struct page, but in middle it suddenly finds the page is actually a folio, then treat it as a page afterwards. I wonder if it can be done better.Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
This might be a question to Alistair, since he made the change.
Hello! I might be him :)
I think this situation is just historical - when I originally wrote
zone_device_page_init()the pgmap was stored on the page rather than the folio. That only changed fairly recently with commit82ba975e4c43("mm: allow compound zone device pages").The reason pgmap is now only available on the folio is described in the commit log. The TLDR is switching FS DAX to use compound pages required
page->compound_headto be available for use, and that was being shared withpage->pgmap. So the solution was to move pgmap to the folio freeing uppage->compound_headfor use on tail pages.The whole percpu
pgmap->refcould actually now go away - I've debated removing it but haven't found the motivation as it provides a small advantage on driver tear down. Basically it just tracks how many pages are allocated in the pgmap so drivers could use that to determine if they need to trigger migrations before tearing down the pgmap.The alternative is just to loop over every page in the pgmap to ensure the folio/page refcounts are 0 before tear down.
Quoted 9 lines
Quoted 9 lines
Quoted 3 lines
Quoted 1 line
I'll let him answer it :)
Not him, but I think this goes back to my question raised in my other reply: When would we allocate "struct folio" in the future.
If it's "always" then actually most of the zone-device code would only ever operate on folios and never on pages in the future.
I recall during a discussion at LSF/MM I raised that, and the answer was (IIRC) that we will allocate "struct folio" as we will initialize the memmap for dax.
Sounds about right.
Quoted 1 line
Quoted 1 line
So essentially, we'd always have folios and would never really have to operate on pages.
Yeah, I think I mentioned to Matthew at LSF/MM that I thought
ZONE_DEVICE(and in particularZONE_DEVICE_PRIVATE) might be a good candidate to experiment with removing struct pages entirely and switching to memdesc's or whatever. Because we should, in theory at least, only need to operate on folio's. But I'm still a little vague on the details how that would actually work. It's been on my TODO list for a while, so myabe I will try and look at it for LPC as a healthy bit of conference driven development.Quoted 6 lines
Quoted 4 lines
Hmm, then what is the point of having “struct folio”, which originally is added to save
compound_head()calls, where everything is a folio in device private world? We might need DAX people to explain the rationale of “always struct folio”.Longer term isn't there an aim to remove struct page? So I assumed moving to
Quoted 8 lines
Right. But my current impression based on my code reading and this patchset is that every device private page is a folio. To form a high order folio, each device private folio is converted to page, prep_compound*()’d, then converted back to folio. Based on what you said above, this weird conversion might be temporary until the code is switched to memdesc.
I am looking forward to more details on how device private will be switched to memdesc from you. :)
Thanks, so am I :-P
For device private I think the first step is to move away from using pfn_to_page()/page_to_pfn() and instead create a "device pfn" that doesn't exist in the physical direct map. That in itself would solve some problems (such as supporting device private pages on ARM) and I hope to have something posted in the next couple of weeks.
Quoted 8 lines
Quoted 3 lines
folio's was part of that effort. As you say though many of the clean-ups thus far related to switching
ZONE_DEVICEto folios have indeed just been about removingcompound_head()calls.Best Regards,
Yan, Zi
Re: [v6 01/15] mm/zone_device: support large zone device private folios
In reply to Zi YanQuoted 195 lines · Zi Yan
Quoted 56 lines · Balbir Singh
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }
Quoted 7 lines
Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }
I think the problem is that it will all be weird once we dynamically allocate "struct folio".
I have not yet a clear understanding on how that would really work.
For example, should it be pgmap->ops->page_folio() ?
Who allocates the folio? Do we allocate all order-0 folios initially, to then merge them when constructing large folios? How do we manage the "struct folio" during such merging splitting?
With that in mind, I don't really know what the proper interface should be today.
zone_device_folio_init(struct page *page, unsigned int order)
looks cleaner, agreed.
Quoted 6 lines
Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().
IIRC free_frozen_pages() does not operate on compound pages. If we know that we are operating on a compound page (or single page) then passing in the page (or better the folio) should work.
Signature · 3 lines
Cheers David / dhildenb
Zi Yan
reviewerRe: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 204 lines · David Hildenbrand
Quoted 195 lines · Zi Yan
Quoted 56 lines · Balbir Singh
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Quoted 7 lines
Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }
Quoted 7 lines
I think the problem is that it will all be weird once we dynamically allocate "struct folio".
I have not yet a clear understanding on how that would really work.
For example, should it be
pgmap->ops->page_folio()?Who allocates the folio? Do we allocate all order-0 folios initially, to then merge them when constructing large folios? How do we manage the "struct folio" during such merging splitting?
Right. Either we would waste memory by simply concatenating all “struct folio” and putting paddings at the end, or we would free tail “struct folio” first, then allocate tail “struct page”. Both are painful and do not match core mm’s memdesc pattern, where “struct folio” is allocated when caller is asking for a folio. If “struct folio” is always allocated, there is no difference between “struct folio” and “struct page”.
Quoted 10 lines
With that in mind, I don't really know what the proper interface should be today.
zone_device_folio_init(struct page *page, unsigned int order)looks cleaner, agreed.
Quoted 2 lines
Quoted 6 lines
Quoted 4 lines
Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().IIRC
free_frozen_pages()does not operate on compound pages. If we know that we are operating on a compound page (or single page) then passing in the page (or better the folio) should work.
free_pages_prepare() in __free_frozen_pages(), called by free_frozen_pages(), checks if compound_order(page) matches the given order, in case folio field corrupts. I suppose it is useful. But I do not have a strong opinion about this one.
Best Regards,
Yan, Zi
Alistair Popple
reviewerRe: [v6 01/15] mm/zone_device: support large zone device private folios
On 2025-09-25 at 03:36 +1000, Zi Yan <[email protected]> wrote...
Quoted 212 lines · David Hildenbrand
Quoted 211 lines · Zi Yan
Quoted 195 lines · Balbir Singh
Quoted 57 lines
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Quoted 7 lines
Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }I think the problem is that it will all be weird once we dynamically allocate "struct folio".
I have not yet a clear understanding on how that would really work.
For example, should it be
pgmap->ops->page_folio()?
Quoted 8 lines
Quoted 1 line
Who allocates the folio? Do we allocate all order-0 folios initially, to then merge them when constructing large folios? How do we manage the "struct folio" during such merging splitting?
Right. Either we would waste memory by simply concatenating all “struct folio” and putting paddings at the end, or we would free tail “struct folio” first, then allocate tail “struct page”. Both are painful and do not match core mm’s memdesc pattern, where “struct folio” is allocated when caller is asking for a folio. If “struct folio” is always allocated, there is no difference between “struct folio” and “struct page”.
As mentioned in my other reply I need to investigate this some more, but I don't think we _need_ to always allocate folios (or pages for that matter). The ZONE_DEVICE code just uses folios/pages for interacting with the core mm, not for managing the device memory itself, so we should be able to make it more closely match the memdesc pattern. It's just I'm still a bit unsure what that pattern will actually look like.
Quoted 7 lines
Quoted 7 lines
With that in mind, I don't really know what the proper interface should be today.
zone_device_folio_init(struct page *page, unsigned int order)looks cleaner, agreed.
Agreed.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().
Where would the order parameter come from? Presumably folio_order(compound_head(page)) in which case shouldn't the op actually just be pgmap->ops->folio_free()?
- Alistair
Quoted 13 lines
Quoted 3 lines
Quoted 1 line
IIRC
free_frozen_pages()does not operate on compound pages. If we know that we are operating on a compound page (or single page) then passing in the page (or better the folio) should work.
free_pages_prepare()in__free_frozen_pages(), called byfree_frozen_pages(), checks ifcompound_order(page)matches the given order, in case folio field corrupts. I suppose it is useful. But I do not have a strong opinion about this one.Best Regards,
Yan, Zi
Re: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 241 lines · Alistair Popple
On 2025-09-25 at 03:36 +1000, Zi Yan <[email protected]> wrote...
Quoted 212 lines · David Hildenbrand
Quoted 211 lines · Zi Yan
Quoted 195 lines · Balbir Singh
Quoted 57 lines
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Quoted 7 lines
Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }I think the problem is that it will all be weird once we dynamically allocate "struct folio".
I have not yet a clear understanding on how that would really work.
For example, should it be
pgmap->ops->page_folio()?Quoted 8 lines
Quoted 1 line
Who allocates the folio? Do we allocate all order-0 folios initially, to then merge them when constructing large folios? How do we manage the "struct folio" during such merging splitting?
Right. Either we would waste memory by simply concatenating all “struct folio” and putting paddings at the end, or we would free tail “struct folio” first, then allocate tail “struct page”. Both are painful and do not match core mm’s memdesc pattern, where “struct folio” is allocated when caller is asking for a folio. If “struct folio” is always allocated, there is no difference between “struct folio” and “struct page”.
As mentioned in my other reply I need to investigate this some more, but I don't think we _need_ to always allocate folios (or pages for that matter). The
ZONE_DEVICEcode just uses folios/pages for interacting with the core mm, not for managing the device memory itself, so we should be able to make it more closely match the memdesc pattern. It's just I'm still a bit unsure what that pattern will actually look like.Quoted 7 lines
Quoted 7 lines
With that in mind, I don't really know what the proper interface should be today.
zone_device_folio_init(struct page *page, unsigned int order)looks cleaner, agreed.
Agreed.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().Where would the order parameter come from? Presumably folio_order(compound_head(page)) in which case shouldn't the op actually just be
pgmap->ops->folio_free()?
->page_free() can detect if the page is of large order. The patchset was designed to make folios and opt-in and avoid unnecessary changes to existing drivers. But I can revisit that thought process if it helps with cleaner code.
Balbir
Zi Yan
reviewerRe: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 246 lines · Balbir Singh
Quoted 245 lines · Alistair Popple
On 2025-09-25 at 03:36 +1000, Zi Yan <[email protected]> wrote...
Quoted 212 lines · David Hildenbrand
Quoted 211 lines · Zi Yan
Quoted 195 lines · Balbir Singh
Quoted 57 lines
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Quoted 7 lines
Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }I think the problem is that it will all be weird once we dynamically allocate "struct folio".
I have not yet a clear understanding on how that would really work.
For example, should it be
pgmap->ops->page_folio()?Quoted 8 lines
Quoted 1 line
Who allocates the folio? Do we allocate all order-0 folios initially, to then merge them when constructing large folios? How do we manage the "struct folio" during such merging splitting?
Right. Either we would waste memory by simply concatenating all “struct folio” and putting paddings at the end, or we would free tail “struct folio” first, then allocate tail “struct page”. Both are painful and do not match core mm’s memdesc pattern, where “struct folio” is allocated when caller is asking for a folio. If “struct folio” is always allocated, there is no difference between “struct folio” and “struct page”.
As mentioned in my other reply I need to investigate this some more, but I don't think we _need_ to always allocate folios (or pages for that matter). The
ZONE_DEVICEcode just uses folios/pages for interacting with the core mm, not for managing the device memory itself, so we should be able to make it more closely match the memdesc pattern. It's just I'm still a bit unsure what that pattern will actually look like.Quoted 7 lines
Quoted 7 lines
With that in mind, I don't really know what the proper interface should be today.
zone_device_folio_init(struct page *page, unsigned int order)looks cleaner, agreed.
Agreed.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().
Quoted 7 lines
Quoted 4 lines
Where would the order parameter come from? Presumably folio_order(compound_head(page)) in which case shouldn't the op actually just be
pgmap->ops->folio_free()?->page_free() can detect if the page is of large order. The patchset was designed to make folios and opt-in and avoid unnecessary changes to existing drivers. But I can revisit that thought process if it helps with cleaner code.
That would be very helpful. It is strange to see page_free(folio_page(folio, 0)). If folio is present, converting it back to page makes me think the code frees the first page of the folio.
Best Regards,
Yan, Zi
Re: [v6 01/15] mm/zone_device: support large zone device private folios
In reply to Alistair PoppleQuoted 222 lines · Alistair Popple
On 2025-09-25 at 03:36 +1000, Zi Yan <[email protected]> wrote...
Quoted 212 lines · David Hildenbrand
Quoted 211 lines · Zi Yan
Quoted 195 lines · Balbir Singh
Quoted 57 lines
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/memremap.h | 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++- 3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) }#ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order);void *memremap_pages (struct dev_pagemap *pgmap,int nid);void memunmap_pages (struct dev_pagemap *pgmap);void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn);bool pgmap_pfn_valid (struct dev_pagemap *pgmap,unsigned long pfn);unsigned long memremap_compat_align (void ); + +static inline void zone_device_page_init (struct page *page) +{ +struct folio *folio =page_folio (page); + +zone_device_folio_init (folio,0 );I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)mm/memremap.c +8
-9@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned long nr =folio_nr_pages (folio); +int i;if (WARN_ON_ONCE (!pgmap))return ;mem_cgroup_uncharge (folio); -/* -* Note: we don't expect anonymous compound pages yet. Once supported -* and we could PTE-map them similar to THP, we'd have to clear -* PG_anon_exclusive on all tail pages. -*/ if (folio_test_anon (folio)) { -VM_BUG_ON_FOLIO (folio_test_large (folio), folio); -__ClearPageAnonExclusive (folio_page (folio,0 )); +for (i =0 ; i < nr; i++) +__ClearPageAnonExclusive (folio_page (folio, i)); + }else { +VM_WARN_ON_ONCE (folio_test_large (folio)); }/* @@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case MEMORY_DEVICE_COHERENT :if (WARN_ON_ONCE (!pgmap->ops || !pgmap->ops->page_free))break ; - pgmap->ops->page_free (folio_page (folio,0 )); -put_dev_pagemap (pgmap); + pgmap->ops->page_free (&folio->page); +percpu_ref_put_many (&folio->pgmap->ref, nr);break ;case MEMORY_DEVICE_GENERIC :mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { +struct page *page =folio_page (folio,0 );It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ +VM_WARN_ON_ONCE (order >MAX_ORDER_NR_PAGES ); +/* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2-WARN_ON_ONCE (!percpu_ref_tryget_live (&page_pgmap (page)->ref)); -set_page_count (page,1 ); +WARN_ON_ONCE (!percpu_ref_tryget_many (&page_pgmap (page)->ref,1 << order)); +folio_set_count (folio,1 );lock_page (page); + +if (order >1 ) { +prep_compound_page (page, order); +folio_set_large_rmappable (folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){ VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); /* * Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); /* * anonymous folio does not support order-1, high order file-backed folio * is not supported at all. */ VM_WARN_ON_ONCE(order == 1); if (order > 1) prep_compound_page(page, order); /* page has to be compound head here */ set_page_count(page, 1); lock_page(page); }void
zone_device_folio_init(struct folio *folio, unsigned int order){ struct page *page = folio_page(folio, 0); zone_device_page_init(page, order); page_rmappable_folio(page); }Quoted 7 lines
Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order)
{ zone_device_page_init(page, order); return page_rmappable_folio(page); }I think the problem is that it will all be weird once we dynamically allocate "struct folio".
I have not yet a clear understanding on how that would really work.
For example, should it be
pgmap->ops->page_folio()?Quoted 8 lines
Quoted 1 line
Who allocates the folio? Do we allocate all order-0 folios initially, to then merge them when constructing large folios? How do we manage the "struct folio" during such merging splitting?
Right. Either we would waste memory by simply concatenating all “struct folio” and putting paddings at the end, or we would free tail “struct folio” first, then allocate tail “struct page”. Both are painful and do not match core mm’s memdesc pattern, where “struct folio” is allocated when caller is asking for a folio. If “struct folio” is always allocated, there is no difference between “struct folio” and “struct page”.
Quoted 6 lines
As mentioned in my other reply I need to investigate this some more, but I don't think we _need_ to always allocate folios (or pages for that matter). The
ZONE_DEVICEcode just uses folios/pages for interacting with the core mm, not for managing the device memory itself, so we should be able to make it more closely match the memdesc pattern. It's just I'm still a bit unsure what that pattern will actually look like.
I think one reason might be that in contrast to ordinary pages, zone-device memory is only ever used to be used for folios, right?
Would there be a user that just allocates pages and not wants a folio associated with it?
It's a good question of that would look like when we have dynamically allocated struct folio ...
Quoted 13 lines
Quoted 8 lines
Quoted 8 lines
With that in mind, I don't really know what the proper interface should be today.
zone_device_folio_init(struct page *page, unsigned int order)looks cleaner, agreed.
Agreed.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().Where would the order parameter come from? Presumably folio_order(compound_head(page)) in which case shouldn't the op actually just be
pgmap->ops->folio_free()?
Yeah, that's also what I thought.
Signature · 3 lines
Cheers David / dhildenb
Re: [v6 01/15] mm/zone_device: support large zone device private folios
Quoted 230 lines · David Hildenbrand
Quoted 222 lines · Alistair Popple
On 2025-09-25 at 03:36 +1000, Zi Yan <[email protected]> wrote...
Quoted 212 lines · David Hildenbrand
Quoted 211 lines · Zi Yan
Quoted 195 lines · Balbir Singh
Quoted 57 lines
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>---
include/linux/memremap.h| 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++-3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) } #ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order); void *memremap_pages(struct dev_pagemap *pgmap, int nid); void memunmap_pages(struct dev_pagemap *pgmap); void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn); bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn); unsigned long memremap_compat_align(void); + +static inline void zone_device_page_init (struct page *page) +{ + struct folio *folio = page_folio(page); + + zone_device_folio_init(folio, 0);I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} + #else static inline void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)mm/memremap.c +6
-7@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap); void free_zone_device_folio(struct folio *folio) { struct dev_pagemap *pgmap = folio->pgmap; + unsigned long nr = folio_nr_pages(folio); + int i; if (WARN_ON_ONCE(!pgmap)) return; mem_cgroup_uncharge(folio); - /* - * Note: we don't expect anonymous compound pages yet. Once supported - * and we could PTE-map them similar to THP, we'd have to clear - * PG_anon_exclusive on all tail pages. - */ if (folio_test_anon(folio)) { - VM_BUG_ON_FOLIO(folio_test_large(folio), folio); - __ClearPageAnonExclusive(folio_page(folio, 0)); + for (i = 0; i < nr; i++) + __ClearPageAnonExclusive(folio_page(folio, i)); + } else { + VM_WARN_ON_ONCE(folio_test_large(folio)); } /*@@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case
MEMORY_DEVICE_COHERENT:
if (WARN_ON_ONCE(!pgmap->ops || !pgmap->ops->page_free)) break;
-pgmap->ops->page_free(folio_page(folio, 0));
-put_dev_pagemap(pgmap);
+pgmap->ops->page_free(&folio->page);
+percpu_ref_put_many(&folio->pgmap->ref, nr); break;case
MEMORY_DEVICE_GENERIC:mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { + struct page *page = folio_page(folio, 0);It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ + VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); + /** Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2- WARN_ON_ONCE(!percpu_ref_tryget_live(&page_pgmap(page)->ref)); - set_page_count(page, 1); + WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); + folio_set_count(folio, 1); lock_page(page); + + if (order > 1) { + prep_compound_page(page, order); + folio_set_large_rmappable(folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){
VM_WARN_ON_ONCE(order >MAX_ORDER_NR_PAGES);/*
* Drivers shouldn't be allocating pages after calling
*memunmap_pages().
*/
WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order));
/*
* anonymous folio does not support order-1, high order file-backed folio
* is not supported at all.
*/
VM_WARN_ON_ONCE(order == 1);if (order > 1)
prep_compound_page(page, order);/* page has to be compound head here */
set_page_count(page, 1);
lock_page(page);
}void
zone_device_folio_init(struct folio *folio, unsigned int order){
struct page *page =folio_page(folio, 0);zone_device_page_init(page, order);
page_rmappable_folio(page);
}Quoted 7 lines
Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order) {
zone_device_page_init(page, order);
returnpage_rmappable_folio(page);
}I think the problem is that it will all be weird once we dynamically allocate "struct folio".
I have not yet a clear understanding on how that would really work.
For example, should it be
pgmap->ops->page_folio()?Quoted 8 lines
Quoted 1 line
Who allocates the folio? Do we allocate all order-0 folios initially, to then merge them when constructing large folios? How do we manage the "struct folio" during such merging splitting?
Right. Either we would waste memory by simply concatenating all “struct folio” and putting paddings at the end, or we would free tail “struct folio” first, then allocate tail “struct page”. Both are painful and do not match core mm’s memdesc pattern, where “struct folio” is allocated when caller is asking for a folio. If “struct folio” is always allocated, there is no difference between “struct folio” and “struct page”.
Quoted 6 lines
As mentioned in my other reply I need to investigate this some more, but I don't think we _need_ to always allocate folios (or pages for that matter). The
ZONE_DEVICEcode just uses folios/pages for interacting with the core mm, not for managing the device memory itself, so we should be able to make it more closely match the memdesc pattern. It's just I'm still a bit unsure what that pattern will actually look like.
Quoted 4 lines
I think one reason might be that in contrast to ordinary pages, zone-device memory is only ever used to be used for folios, right?
Would there be a user that just allocates pages and not wants a folio associated with it?
A non-THP aware driver use case would be a potential use case for zero order folios (also pages at the moment).
Quoted 1 line
It's a good question of that would look like when we have dynamically allocated struct folio ...
I think for dynamically allocated folios we could probably do away with pages, but not 100% sure at the moment.
Quoted 18 lines
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
With that in mind, I don't really know what the proper interface should be today.
zone_device_folio_init(struct page *page, unsigned int order)looks cleaner, agreed.
Agreed.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().
Quoted 6 lines
Quoted 3 lines
Where would the order parameter come from? Presumably folio_order(compound_head(page)) in which case shouldn't the op actually just be
pgmap->ops->folio_free()?Yeah, that's also what I thought.
Balbir
Alistair Popple
reviewerRe: [v6 01/15] mm/zone_device: support large zone device private folios
On 2025-09-25 at 22:02 +1000, Balbir Singh <[email protected]> wrote...
Quoted 231 lines · David Hildenbrand
Quoted 230 lines · Alistair Popple
Quoted 228 lines
On 2025-09-25 at 03:36 +1000, Zi Yan <[email protected]> wrote...
Quoted 212 lines · David Hildenbrand
Quoted 211 lines · Zi Yan
Quoted 195 lines · Balbir Singh
Quoted 57 lines
Add routines to support allocation of large order zone device folios and helper functions for zone device folios, to check if a folio is device private and helpers for setting zone device data.
When large folios are used, the existing
page_free()callback in pgmap is called when the folio is freed, this is true for bothPAGE_SIZEand higher order pages.Zone device private large folios do not support deferred split and scan like normal THP folios.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>---
include/linux/memremap.h| 10 +++++++++- mm/memremap.c | 34 +++++++++++++++++++++------------- mm/rmap.c | 6 +++++-3 files changed, 35 insertions(+), 15 deletions(-)include/linux/memremap.h +1
-1@@ -206,7 +206,7 @@ static inline bool is_fsdax_page(const struct page *page) } #ifdef CONFIG_ZONE_DEVICE -void zone_device_page_init (struct page *page); +void zone_device_folio_init (struct folio *folio,unsigned int order); void *memremap_pages(struct dev_pagemap *pgmap, int nid); void memunmap_pages(struct dev_pagemap *pgmap); void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap);include/linux/memremap.h struct dev_pagemap *get_dev_pagemap(unsigned long pfn); · line 223
Quoted 9 lines
include/linux/memremap.h +6
-0@@ -215,6 +215,14 @@ struct dev_pagemap *get_dev_pagemap(unsigned long pfn); bool pgmap_pfn_valid(struct dev_pagemap *pgmap, unsigned long pfn); unsigned long memremap_compat_align(void); + +static inline void zone_device_page_init (struct page *page) +{ + struct folio *folio = page_folio(page); + + zone_device_folio_init(folio, 0);I assume it is for legacy code, where only non-compound page exists?
It seems that you assume @page is always order-0, but there is no check for it. Adding
VM_WARN_ON_ONCE_FOLIO(folio_order(folio) != 0, folio)above it would be useful to detect misuse.Quoted 48 lines
include/linux/memremap.h +2
-0+} + #else static inline void *devm_memremap_pages(struct device *dev, struct dev_pagemap *pgmap)mm/memremap.c +6
-7@@ -416,20 +416,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap); void free_zone_device_folio(struct folio *folio) { struct dev_pagemap *pgmap = folio->pgmap; + unsigned long nr = folio_nr_pages(folio); + int i; if (WARN_ON_ONCE(!pgmap)) return; mem_cgroup_uncharge(folio); - /* - * Note: we don't expect anonymous compound pages yet. Once supported - * and we could PTE-map them similar to THP, we'd have to clear - * PG_anon_exclusive on all tail pages. - */ if (folio_test_anon(folio)) { - VM_BUG_ON_FOLIO(folio_test_large(folio), folio); - __ClearPageAnonExclusive(folio_page(folio, 0)); + for (i = 0; i < nr; i++) + __ClearPageAnonExclusive(folio_page(folio, i)); + } else { + VM_WARN_ON_ONCE(folio_test_large(folio)); } /*@@ -456,8 +455,8 @@ void free_zone_device_folio(struct folio *folio)case
MEMORY_DEVICE_COHERENT:
if (WARN_ON_ONCE(!pgmap->ops || !pgmap->ops->page_free)) break;
-pgmap->ops->page_free(folio_page(folio, 0));
-put_dev_pagemap(pgmap);
+pgmap->ops->page_free(&folio->page);
+percpu_ref_put_many(&folio->pgmap->ref, nr); break;case
MEMORY_DEVICE_GENERIC:mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 484
Quoted 7 lines
mm/memremap.c +2
-1@@ -480,14 +479,23 @@ void free_zone_device_folio(struct folio *folio) } } -void zone_device_page_init (struct page *page) +void zone_device_folio_init (struct folio *folio,unsigned int order) { + struct page *page = folio_page(folio, 0);It is strange to see a folio is converted back to page in a function called
zone_device_folio_init().Quoted 7 lines
mm/memremap.c +3
-0+ + VM_WARN_ON_ONCE(order > MAX_ORDER_NR_PAGES); + /** Drivers shouldn't be allocating pages after calling * memunmap_pages(). */ mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 499
Quoted 10 lines
mm/memremap.c +7
-2- WARN_ON_ONCE(!percpu_ref_tryget_live(&page_pgmap(page)->ref)); - set_page_count(page, 1); + WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order)); + folio_set_count(folio, 1); lock_page(page); + + if (order > 1) { + prep_compound_page(page, order); + folio_set_large_rmappable(folio); + }OK, so basically, @folio is not a compound page yet when
zone_device_folio_init()is called.I feel that your
zone_device_page_init()andzone_device_folio_init()implementations are inverse. They should follow the same pattern as__alloc_pages_noprof()and__folio_alloc_noprof(), wherezone_device_page_init()does the actual initialization andzone_device_folio_init()just convert a page to folio.Something like:
void
zone_device_page_init(struct page *page, unsigned int order){
VM_WARN_ON_ONCE(order >MAX_ORDER_NR_PAGES);/*
* Drivers shouldn't be allocating pages after calling
*memunmap_pages().
*/
WARN_ON_ONCE(!percpu_ref_tryget_many(&page_pgmap(page)->ref, 1 << order));
/*
* anonymous folio does not support order-1, high order file-backed folio
* is not supported at all.
*/
VM_WARN_ON_ONCE(order == 1);if (order > 1)
prep_compound_page(page, order);/* page has to be compound head here */
set_page_count(page, 1);
lock_page(page);
}void
zone_device_folio_init(struct folio *folio, unsigned int order){
struct page *page =folio_page(folio, 0);zone_device_page_init(page, order);
page_rmappable_folio(page);
}Quoted 7 lines
Or
struct folio *zone_device_folio_init(struct page *page, unsigned int order) {
zone_device_page_init(page, order);
returnpage_rmappable_folio(page);
}I think the problem is that it will all be weird once we dynamically allocate "struct folio".
I have not yet a clear understanding on how that would really work.
For example, should it be
pgmap->ops->page_folio()?Quoted 8 lines
Quoted 1 line
Who allocates the folio? Do we allocate all order-0 folios initially, to then merge them when constructing large folios? How do we manage the "struct folio" during such merging splitting?
Right. Either we would waste memory by simply concatenating all “struct folio” and putting paddings at the end, or we would free tail “struct folio” first, then allocate tail “struct page”. Both are painful and do not match core mm’s memdesc pattern, where “struct folio” is allocated when caller is asking for a folio. If “struct folio” is always allocated, there is no difference between “struct folio” and “struct page”.
As mentioned in my other reply I need to investigate this some more, but I don't think we _need_ to always allocate folios (or pages for that matter). The
ZONE_DEVICEcode just uses folios/pages for interacting with the core mm, not for managing the device memory itself, so we should be able to make it more closely match the memdesc pattern. It's just I'm still a bit unsure what that pattern will actually look like.
Quoted 3 lines
Quoted 3 lines
I think one reason might be that in contrast to ordinary pages, zone-device memory is only ever used to be used for folios, right?
Would there be a user that just allocates pages and not wants a folio associated with it?
I don't think so, other than of course zero order folios. There's probably just some confusion due to a page and zero order folio are not being different at the moment.
Quoted 7 lines
Quoted 1 line
A non-THP aware driver use case would be a potential use case for zero order folios (also pages at the moment).
Quoted 1 line
It's a good question of that would look like when we have dynamically allocated struct folio ...
I think for dynamically allocated folios we could probably do away with pages, but not 100% sure at the moment.
Yeah, I'm not 100% sure either but that sounds about right.
Quoted 26 lines
Quoted 18 lines
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
With that in mind, I don't really know what the proper interface should be today.
zone_device_folio_init(struct page *page, unsigned int order)looks cleaner, agreed.
Agreed.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Then, it comes to
free_zone_device_folio()above,
I feel thatpgmap->ops->page_free()should take an additional order parameter to free a compound page likefree_frozen_pages().Quoted 6 lines
Quoted 3 lines
Where would the order parameter come from? Presumably folio_order(compound_head(page)) in which case shouldn't the op actually just be
pgmap->ops->folio_free()?Yeah, that's also what I thought.
Balbir
PATCH v6 2/15 mm/huge_memory: add device-private THP support to PMD operations
In reply to Balbir SinghExtend core huge page management functions to handle device-private THP entries. This enables proper handling of large device-private folios in fundamental MM operations.
The following functions have been updated:
- copy_huge_pmd(): Handle device-private entries during fork/clone
- zap_huge_pmd(): Properly free device-private THP during munmap
- change_huge_pmd(): Support protection changes on device-private THP
- __pte_offset_map(): Add device-private entry awareness
--- include/linux/swapops.h | 32 +++++++++++++++++++++++ mm/huge_memory.c | 56 ++++++++++++++++++++++++++++++++++------- mm/pgtable-generic.c | 2 +- 3 files changed, 80 insertions(+), 10 deletions(-)
include/linux/swapops.h +32 -0
@@ -594,10 +594,42 @@ static inline int is_pmd_migration_entry(pmd_t pmd) }#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ +#if defined(CONFIG_ZONE_DEVICE ) && defined(CONFIG_ARCH_ENABLE_THP_MIGRATION ) + +/** +* is_pmd_device_private_entry() - Check if PMD contains a device private swap entry +* @pmd: The PMD to check +* +* Returns true if the PMD contains a swap entry that represents a device private +* page mapping. This is used for zone device private pages that have been +* swapped out but still need special handling during various memory management +* operations. +* +* Return: 1 if PMD contains device private entry, 0 otherwise +*/ +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return is_swap_pmd (pmd) &&is_device_private_entry (pmd_to_swp_entry (pmd)); +} + +#else /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ + +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return 0 ; +} + +#endif /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ +static inline int non_swap_entry (swp_entry_t entry) {return swp_type (entry) >=MAX_SWAPFILES ; } +static inline int is_pmd_non_present_folio_entry (pmd_t pmd) +{ +return is_pmd_migration_entry (pmd) ||is_pmd_device_private_entry (pmd); +} +#endif /* CONFIG_MMU */ #endif /* _LINUX_SWAPOPS_H */
mm/huge_memory.c +47 -9
98 lines not on this page · raw message
mm/pgtable-generic.c +1 -1
@@ -290,7 +290,7 @@ pte_t *___pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)if (pmdvalp) *pmdvalp = pmdval; -if (unlikely (pmd_none (pmdval) ||is_pmd_migration_entry (pmdval))) +if (unlikely (pmd_none (pmdval) || !pmd_present (pmdval))) goto nomap;if (unlikely (pmd_trans_huge (pmdval))) goto nomap;
Signature · 1 line
2.50.1
Zi Yan
reviewerRe: [v6 02/15] mm/huge_memory: add device-private THP support to PMD operations
Quoted 78 lines · Balbir Singh
Extend core huge page management functions to handle device-private THP entries. This enables proper handling of large device-private folios in fundamental MM operations.
The following functions have been updated:
-
copy_huge_pmd(): Handle device-private entries during fork/clone
-zap_huge_pmd(): Properly free device-private THP during munmap
-change_huge_pmd(): Support protection changes on device-private THP
-__pte_offset_map(): Add device-private entry awarenessSigned-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/swapops.h | 32 +++++++++++++++++++++++ mm/huge_memory.c | 56 ++++++++++++++++++++++++++++++++++------- mm/pgtable-generic.c | 2 +- 3 files changed, 80 insertions(+), 10 deletions(-)include/linux/swapops.h +27
-0@@ -594,10 +594,42 @@ static inline int is_pmd_migration_entry(pmd_t pmd) }#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ +#if defined(CONFIG_ZONE_DEVICE ) && defined(CONFIG_ARCH_ENABLE_THP_MIGRATION ) + +/** +* is_pmd_device_private_entry() - Check if PMD contains a device private swap entry +* @pmd: The PMD to check +* +* Returns true if the PMD contains a swap entry that represents a device private +* page mapping. This is used for zone device private pages that have been +* swapped out but still need special handling during various memory management +* operations. +* +* Return: 1 if PMD contains device private entry, 0 otherwise +*/ +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return is_swap_pmd (pmd) &&is_device_private_entry (pmd_to_swp_entry (pmd)); +} + +#else /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ + +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return 0 ; +} + +#endif /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ +
include/linux/swapops.h static inline int is_pmd_migration_entry(pmd_t pmd) · line 633
Quoted 10 lines
include/linux/swapops.h +5
-0static inline int non_swap_entry (swp_entry_t entry) {return swp_type (entry) >=MAX_SWAPFILES ; } +static inline int is_pmd_non_present_folio_entry (pmd_t pmd) +{ +return is_pmd_migration_entry (pmd) ||is_pmd_device_private_entry (pmd); +} +
non_present seems too vague. Maybe just open code it.
Quoted 41 lines
include/linux/swapops.h +0
-0#endif /* CONFIG_MMU */ #endif /* _LINUX_SWAPOPS_H */ mm/huge_memory.c +21
-4@@ -1703,17 +1703,45 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,if (unlikely (is_swap_pmd (pmd))) {swp_entry_t entry =pmd_to_swp_entry (pmd); -VM_BUG_ON (!is_pmd_migration_entry (pmd)); -if (!is_readable_migration_entry (entry)) { - entry =make_readable_migration_entry ( -swp_offset (entry)); +VM_WARN_ON (!is_pmd_non_present_folio_entry (pmd)); + +if (is_writable_migration_entry (entry) || +is_readable_exclusive_migration_entry (entry)) { + entry =make_readable_migration_entry (swp_offset (entry)); pmd =swp_entry_to_pmd (entry);if (pmd_swp_soft_dirty (*src_pmd)) pmd =pmd_swp_mksoft_dirty (pmd);if (pmd_swp_uffd_wp (*src_pmd)) pmd =pmd_swp_mkuffd_wp (pmd);set_pmd_at (src_mm, addr, src_pmd, pmd); + }else if (is_device_private_entry (entry)) { +/* +* For device private entries, since there are no +* read exclusive entries, writable = !readable +*/ +if (is_writable_device_private_entry (entry)) { + entry =make_readable_device_private_entry (swp_offset (entry)); + pmd =swp_entry_to_pmd (entry); + +if (pmd_swp_soft_dirty (*src_pmd)) + pmd =pmd_swp_mksoft_dirty (pmd); +if (pmd_swp_uffd_wp (*src_pmd)) + pmd =pmd_swp_mkuffd_wp (pmd); +set_pmd_at (src_mm, addr, src_pmd, pmd); + } +
mm/huge_memory.c int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, · line 1742
Quoted 10 lines
mm/huge_memory.c +10
-0+ src_folio =pfn_swap_entry_folio (entry); +VM_WARN_ON (!folio_test_large (src_folio)); + +folio_get (src_folio); +/* +* folio_try_dup_anon_rmap_pmd does not fail for +* device private entries. +*/ +folio_try_dup_anon_rmap_pmd (src_folio, &src_folio->page, + dst_vma, src_vma);’
folio_get() and folio_try_dup_anon_rmap_pmd() are needed, because contrary to the migration entry case, this folio exists as a device private one.
Quoted 6 lines
mm/huge_memory.c +1
-0} +add_mm_counter (dst_mm,MM_ANONPAGES ,HPAGE_PMD_NR );mm_inc_nr_ptes (dst_mm);pgtable_trans_huge_deposit (dst_mm, dst_pmd, pgtable);
mm/huge_memory.c int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 2217
Quoted 8 lines
mm/huge_memory.c +1
-2@@ -2211,15 +2239,16 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,folio_remove_rmap_pmd (folio, page, vma);WARN_ON_ONCE (folio_mapcount (folio) <0 );VM_BUG_ON_PAGE (!PageHead (page), page); - }else if (thp_migration_supported ()) { + }else if (is_pmd_non_present_folio_entry (orig_pmd)) {swp_entry_t entry; -VM_BUG_ON (!is_pmd_migration_entry (orig_pmd));
It implies thp_migration_supported() is true here. We could have VM_WARN_ONCE_ON(!thp_migration_supported()), but that might be too much.
Quoted 59 lines
mm/huge_memory.c +14
-3entry =pmd_to_swp_entry (orig_pmd); folio =pfn_swap_entry_folio (entry); flush_needed =0 ; - }else -WARN_ONCE (1 ,"Non present huge pmd without pmd migration enabled!" ); + +if (!thp_migration_supported ()) +WARN_ONCE (1 ,"Non present huge pmd without pmd migration enabled!" ); + }if (folio_test_anon (folio)) {zap_deposited_table (tlb->mm, pmd);@@ -2239,6 +2268,12 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,folio_mark_accessed (folio); } +if (folio_is_device_private (folio)) { +folio_remove_rmap_pmd (folio, &folio->page, vma); +WARN_ON_ONCE (folio_mapcount (folio) <0 ); +folio_put (folio); + } +spin_unlock (ptl);if (flush_needed)tlb_remove_page_size (tlb, &folio->page,HPAGE_PMD_SIZE );@@ -2367,7 +2402,7 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,struct folio *folio =pfn_swap_entry_folio (entry);pmd_t newpmd; -VM_BUG_ON (!is_pmd_migration_entry (*pmd)); +VM_WARN_ON (!is_pmd_non_present_folio_entry (*pmd));if (is_writable_migration_entry (entry)) {/* * A protection check is difficult so @@ -2380,6 +2415,9 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, newpmd =swp_entry_to_pmd (entry);if (pmd_swp_soft_dirty (*pmd)) newpmd =pmd_swp_mksoft_dirty (newpmd); + }else if (is_writable_device_private_entry (entry)) { + entry =make_readable_device_private_entry (swp_offset (entry)); + newpmd =swp_entry_to_pmd (entry); }else { newpmd = *pmd; }mm/pgtable-generic.c +1
-1@@ -290,7 +290,7 @@ pte_t *___pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)if (pmdvalp) *pmdvalp = pmdval; -if (unlikely (pmd_none (pmdval) ||is_pmd_migration_entry (pmdval))) +if (unlikely (pmd_none (pmdval) || !pmd_present (pmdval))) goto nomap;if (unlikely (pmd_trans_huge (pmdval))) goto nomap;Signature · 1 line
2.50.1
Otherwise, LGTM. Acked-by: Zi Yan <[email protected]>
Best Regards,
Yan, Zi
Re: [v6 02/15] mm/huge_memory: add device-private THP support to PMD operations
Quoted 85 lines · Zi Yan
Quoted 83 lines · Balbir Singh
Extend core huge page management functions to handle device-private THP entries. This enables proper handling of large device-private folios in fundamental MM operations.
The following functions have been updated:
-
copy_huge_pmd(): Handle device-private entries during fork/clone
-zap_huge_pmd(): Properly free device-private THP during munmap
-change_huge_pmd(): Support protection changes on device-private THP
-__pte_offset_map(): Add device-private entry awarenessSigned-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/swapops.h | 32 +++++++++++++++++++++++ mm/huge_memory.c | 56 ++++++++++++++++++++++++++++++++++------- mm/pgtable-generic.c | 2 +- 3 files changed, 80 insertions(+), 10 deletions(-)include/linux/swapops.h +27
-0@@ -594,10 +594,42 @@ static inline int is_pmd_migration_entry(pmd_t pmd) }#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ +#if defined(CONFIG_ZONE_DEVICE ) && defined(CONFIG_ARCH_ENABLE_THP_MIGRATION ) + +/** +* is_pmd_device_private_entry() - Check if PMD contains a device private swap entry +* @pmd: The PMD to check +* +* Returns true if the PMD contains a swap entry that represents a device private +* page mapping. This is used for zone device private pages that have been +* swapped out but still need special handling during various memory management +* operations. +* +* Return: 1 if PMD contains device private entry, 0 otherwise +*/ +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return is_swap_pmd (pmd) &&is_device_private_entry (pmd_to_swp_entry (pmd)); +} + +#else /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ + +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return 0 ; +} + +#endif /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ +static inline int non_swap_entry (swp_entry_t entry) {return swp_type (entry) >=MAX_SWAPFILES ; }
Quoted 7 lines
include/linux/swapops.h static inline int is_pmd_migration_entry(pmd_t pmd) · line 633
Quoted 5 lines
include/linux/swapops.h +5
-0+static inline int is_pmd_non_present_folio_entry (pmd_t pmd) +{ +return is_pmd_migration_entry (pmd) ||is_pmd_device_private_entry (pmd); +} +
non_presentseems too vague. Maybe just open code it.
This was David's suggestion from the previous posting, there is is_swap_pfn_entry() but it's much larger than we would like for our use case.
Quoted 49 lines
Quoted 49 lines
include/linux/swapops.h +0
-0#endif /* CONFIG_MMU */ #endif /* _LINUX_SWAPOPS_H */ mm/huge_memory.c +27
-4@@ -1703,17 +1703,45 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,if (unlikely (is_swap_pmd (pmd))) {swp_entry_t entry =pmd_to_swp_entry (pmd); -VM_BUG_ON (!is_pmd_migration_entry (pmd)); -if (!is_readable_migration_entry (entry)) { - entry =make_readable_migration_entry ( -swp_offset (entry)); +VM_WARN_ON (!is_pmd_non_present_folio_entry (pmd)); + +if (is_writable_migration_entry (entry) || +is_readable_exclusive_migration_entry (entry)) { + entry =make_readable_migration_entry (swp_offset (entry)); pmd =swp_entry_to_pmd (entry);if (pmd_swp_soft_dirty (*src_pmd)) pmd =pmd_swp_mksoft_dirty (pmd);if (pmd_swp_uffd_wp (*src_pmd)) pmd =pmd_swp_mkuffd_wp (pmd);set_pmd_at (src_mm, addr, src_pmd, pmd); + }else if (is_device_private_entry (entry)) { +/* +* For device private entries, since there are no +* read exclusive entries, writable = !readable +*/ +if (is_writable_device_private_entry (entry)) { + entry =make_readable_device_private_entry (swp_offset (entry)); + pmd =swp_entry_to_pmd (entry); + +if (pmd_swp_soft_dirty (*src_pmd)) + pmd =pmd_swp_mksoft_dirty (pmd); +if (pmd_swp_uffd_wp (*src_pmd)) + pmd =pmd_swp_mkuffd_wp (pmd); +set_pmd_at (src_mm, addr, src_pmd, pmd); + } + + src_folio =pfn_swap_entry_folio (entry); +VM_WARN_ON (!folio_test_large (src_folio)); + +folio_get (src_folio); +/* +* folio_try_dup_anon_rmap_pmd does not fail for
Quoted 9 lines
mm/huge_memory.c int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,
Quoted 4 lines
mm/huge_memory.c +4
-0+* device private entries. +*/ +folio_try_dup_anon_rmap_pmd (src_folio, &src_folio->page, + dst_vma, src_vma);’
folio_get()andfolio_try_dup_anon_rmap_pmd()are needed, because contrary to the migration entry case, this folio exists as a device private one.
Is that a question?
Quoted 13 lines
Quoted 13 lines
mm/huge_memory.c +2
-1} +add_mm_counter (dst_mm,MM_ANONPAGES ,HPAGE_PMD_NR );mm_inc_nr_ptes (dst_mm);pgtable_trans_huge_deposit (dst_mm, dst_pmd, pgtable);@@ -2211,15 +2239,16 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,folio_remove_rmap_pmd (folio, page, vma);WARN_ON_ONCE (folio_mapcount (folio) <0 );VM_BUG_ON_PAGE (!PageHead (page), page); - }else if (thp_migration_supported ()) { + }else if (is_pmd_non_present_folio_entry (orig_pmd)) {swp_entry_t entry;
Quoted 5 lines
mm/huge_memory.c int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, · line 2217
Quoted 1 line
mm/huge_memory.c +0
-1-VM_BUG_ON (!is_pmd_migration_entry (orig_pmd));It implies
thp_migration_supported()is true here. We could have VM_WARN_ONCE_ON(!thp_migration_supported()), but that might be too much.
Yes, since we've validated that this is a pmd migration or device private entry.
Quoted 50 lines
Quoted 50 lines
mm/huge_memory.c +14
-3entry =pmd_to_swp_entry (orig_pmd); folio =pfn_swap_entry_folio (entry); flush_needed =0 ; - }else -WARN_ONCE (1 ,"Non present huge pmd without pmd migration enabled!" ); + +if (!thp_migration_supported ()) +WARN_ONCE (1 ,"Non present huge pmd without pmd migration enabled!" ); + }if (folio_test_anon (folio)) {zap_deposited_table (tlb->mm, pmd);@@ -2239,6 +2268,12 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,folio_mark_accessed (folio); } +if (folio_is_device_private (folio)) { +folio_remove_rmap_pmd (folio, &folio->page, vma); +WARN_ON_ONCE (folio_mapcount (folio) <0 ); +folio_put (folio); + } +spin_unlock (ptl);if (flush_needed)tlb_remove_page_size (tlb, &folio->page,HPAGE_PMD_SIZE );@@ -2367,7 +2402,7 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,struct folio *folio =pfn_swap_entry_folio (entry);pmd_t newpmd; -VM_BUG_ON (!is_pmd_migration_entry (*pmd)); +VM_WARN_ON (!is_pmd_non_present_folio_entry (*pmd));if (is_writable_migration_entry (entry)) {/* * A protection check is difficult so @@ -2380,6 +2415,9 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, newpmd =swp_entry_to_pmd (entry);if (pmd_swp_soft_dirty (*pmd)) newpmd =pmd_swp_mksoft_dirty (newpmd); + }else if (is_writable_device_private_entry (entry)) { + entry =make_readable_device_private_entry (swp_offset (entry)); + newpmd =swp_entry_to_pmd (entry); }else { newpmd = *pmd; }mm/pgtable-generic.c +0
-0@@ -290,7 +290,7 @@ pte_t *___pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
Quoted 12 lines
Quoted 9 lines
mm/pgtable-generic.c +1
-1if (pmdvalp) *pmdvalp = pmdval; -if (unlikely (pmd_none (pmdval) ||is_pmd_migration_entry (pmdval))) +if (unlikely (pmd_none (pmdval) || !pmd_present (pmdval))) goto nomap;if (unlikely (pmd_trans_huge (pmdval))) goto nomap;Signature · 1 line
2.50.1Otherwise, LGTM. Acked-by: Zi Yan <[email protected]>
Thanks Zi!
Balbir
Re: [v6 02/15] mm/huge_memory: add device-private THP support to PMD operations
Quoted 6 lines
Quoted 3 lines
non_presentseems too vague. Maybe just open code it.This was David's suggestion from the previous posting, there is
is_swap_pfn_entry()but it's much larger than we would like for our use case.
Right. If we can find a better name, great, but open coding this turned out nasty.
Signature · 3 lines
Cheers David / dhildenb
Alistair Popple
reviewerRe: [v6 02/15] mm/huge_memory: add device-private THP support to PMD operations
In reply to Balbir SinghOn 2025-09-16 at 22:21 +1000, Balbir Singh <[email protected]> wrote...
Quoted 74 lines
Extend core huge page management functions to handle device-private THP entries. This enables proper handling of large device-private folios in fundamental MM operations.
The following functions have been updated:
-
copy_huge_pmd(): Handle device-private entries during fork/clone
-zap_huge_pmd(): Properly free device-private THP during munmap
-change_huge_pmd(): Support protection changes on device-private THP
-__pte_offset_map(): Add device-private entry awarenessSigned-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/swapops.h | 32 +++++++++++++++++++++++ mm/huge_memory.c | 56 ++++++++++++++++++++++++++++++++++------- mm/pgtable-generic.c | 2 +- 3 files changed, 80 insertions(+), 10 deletions(-)include/linux/swapops.h +23
-0@@ -594,10 +594,42 @@ static inline int is_pmd_migration_entry(pmd_t pmd) }#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ +#if defined(CONFIG_ZONE_DEVICE ) && defined(CONFIG_ARCH_ENABLE_THP_MIGRATION ) + +/** +* is_pmd_device_private_entry() - Check if PMD contains a device private swap entry +* @pmd: The PMD to check +* +* Returns true if the PMD contains a swap entry that represents a device private +* page mapping. This is used for zone device private pages that have been +* swapped out but still need special handling during various memory management +* operations. +* +* Return: 1 if PMD contains device private entry, 0 otherwise +*/ +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return is_swap_pmd (pmd) &&is_device_private_entry (pmd_to_swp_entry (pmd)); +} + +#else /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ + +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return 0 ;
include/linux/swapops.h static inline int is_pmd_migration_entry(pmd_t pmd) · line 629
Quoted 10 lines
include/linux/swapops.h +5
-0+} + +#endif /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ +static inline int non_swap_entry (swp_entry_t entry) {return swp_type (entry) >=MAX_SWAPFILES ; } +static inline int is_pmd_non_present_folio_entry (pmd_t pmd)
I can't think of a better name either although I am curious why open-coding it was so nasty given we don't have the equivalent for pte entries. Will go read the previous discussion.
Quoted 42 lines
include/linux/swapops.h +4
-0+{ +return is_pmd_migration_entry (pmd) ||is_pmd_device_private_entry (pmd); +} +#endif /* CONFIG_MMU */ #endif /* _LINUX_SWAPOPS_H */ mm/huge_memory.c +18
-4@@ -1703,17 +1703,45 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm,if (unlikely (is_swap_pmd (pmd))) {swp_entry_t entry =pmd_to_swp_entry (pmd); -VM_BUG_ON (!is_pmd_migration_entry (pmd)); -if (!is_readable_migration_entry (entry)) { - entry =make_readable_migration_entry ( -swp_offset (entry)); +VM_WARN_ON (!is_pmd_non_present_folio_entry (pmd)); + +if (is_writable_migration_entry (entry) || +is_readable_exclusive_migration_entry (entry)) { + entry =make_readable_migration_entry (swp_offset (entry)); pmd =swp_entry_to_pmd (entry);if (pmd_swp_soft_dirty (*src_pmd)) pmd =pmd_swp_mksoft_dirty (pmd);if (pmd_swp_uffd_wp (*src_pmd)) pmd =pmd_swp_mkuffd_wp (pmd);set_pmd_at (src_mm, addr, src_pmd, pmd); + }else if (is_device_private_entry (entry)) { +/* +* For device private entries, since there are no +* read exclusive entries, writable = !readable +*/ +if (is_writable_device_private_entry (entry)) { + entry =make_readable_device_private_entry (swp_offset (entry)); + pmd =swp_entry_to_pmd (entry); + +if (pmd_swp_soft_dirty (*src_pmd)) + pmd =pmd_swp_mksoft_dirty (pmd); +if (pmd_swp_uffd_wp (*src_pmd)) + pmd =pmd_swp_mkuffd_wp (pmd);
mm/huge_memory.c int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, · line 1739
Quoted 10 lines
mm/huge_memory.c +10
-0+set_pmd_at (src_mm, addr, src_pmd, pmd); + } + + src_folio =pfn_swap_entry_folio (entry); +VM_WARN_ON (!folio_test_large (src_folio)); + +folio_get (src_folio); +/* +* folio_try_dup_anon_rmap_pmd does not fail for +* device private entries.
Not today. But maybe wrapping this in WARN_ON_ONCE() might be nice in case that ever changes.
Quoted 66 lines
mm/huge_memory.c +19
-5+*/ +folio_try_dup_anon_rmap_pmd (src_folio, &src_folio->page, + dst_vma, src_vma); } +add_mm_counter (dst_mm,MM_ANONPAGES ,HPAGE_PMD_NR );mm_inc_nr_ptes (dst_mm);pgtable_trans_huge_deposit (dst_mm, dst_pmd, pgtable);@@ -2211,15 +2239,16 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,folio_remove_rmap_pmd (folio, page, vma);WARN_ON_ONCE (folio_mapcount (folio) <0 );VM_BUG_ON_PAGE (!PageHead (page), page); - }else if (thp_migration_supported ()) { + }else if (is_pmd_non_present_folio_entry (orig_pmd)) {swp_entry_t entry; -VM_BUG_ON (!is_pmd_migration_entry (orig_pmd)); entry =pmd_to_swp_entry (orig_pmd); folio =pfn_swap_entry_folio (entry); flush_needed =0 ; - }else -WARN_ONCE (1 ,"Non present huge pmd without pmd migration enabled!" ); + +if (!thp_migration_supported ()) +WARN_ONCE (1 ,"Non present huge pmd without pmd migration enabled!" ); + }if (folio_test_anon (folio)) {zap_deposited_table (tlb->mm, pmd);@@ -2239,6 +2268,12 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,folio_mark_accessed (folio); } +if (folio_is_device_private (folio)) { +folio_remove_rmap_pmd (folio, &folio->page, vma); +WARN_ON_ONCE (folio_mapcount (folio) <0 ); +folio_put (folio); + } +spin_unlock (ptl);if (flush_needed)tlb_remove_page_size (tlb, &folio->page,HPAGE_PMD_SIZE );@@ -2367,7 +2402,7 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,struct folio *folio =pfn_swap_entry_folio (entry);pmd_t newpmd; -VM_BUG_ON (!is_pmd_migration_entry (*pmd)); +VM_WARN_ON (!is_pmd_non_present_folio_entry (*pmd));if (is_writable_migration_entry (entry)) {/* * A protection check is difficult so @@ -2380,6 +2415,9 @@ int change_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, newpmd =swp_entry_to_pmd (entry);if (pmd_swp_soft_dirty (*pmd)) newpmd =pmd_swp_mksoft_dirty (newpmd); + }else if (is_writable_device_private_entry (entry)) { + entry =make_readable_device_private_entry (swp_offset (entry)); + newpmd =swp_entry_to_pmd (entry); }else { newpmd = *pmd; }mm/pgtable-generic.c +0
-0
mm/pgtable-generic.c pte_t *___pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) · line 293
Quoted 5 lines
mm/pgtable-generic.c +1
-1@@ -290,7 +290,7 @@ pte_t *___pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)if (pmdvalp) *pmdvalp = pmdval; -if (unlikely (pmd_none (pmdval) ||is_pmd_migration_entry (pmdval))) +if (unlikely (pmd_none (pmdval) || !pmd_present (pmdval)))
Why isn't is_pmd_non_present_folio_entry() used here?
Quoted 6 lines
mm/pgtable-generic.c +0
-0goto nomap;if (unlikely (pmd_trans_huge (pmdval))) goto nomap;Signature · 1 line
2.50.1
Re: [v6 02/15] mm/huge_memory: add device-private THP support to PMD operations
Quoted 84 lines · Alistair Popple
On 2025-09-16 at 22:21 +1000, Balbir Singh <[email protected]> wrote...
Quoted 83 lines
Extend core huge page management functions to handle device-private THP entries. This enables proper handling of large device-private folios in fundamental MM operations.
The following functions have been updated:
-
copy_huge_pmd(): Handle device-private entries during fork/clone
-zap_huge_pmd(): Properly free device-private THP during munmap
-change_huge_pmd(): Support protection changes on device-private THP
-__pte_offset_map(): Add device-private entry awarenessSigned-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/swapops.h | 32 +++++++++++++++++++++++ mm/huge_memory.c | 56 ++++++++++++++++++++++++++++++++++------- mm/pgtable-generic.c | 2 +- 3 files changed, 80 insertions(+), 10 deletions(-)include/linux/swapops.h +27
-0@@ -594,10 +594,42 @@ static inline int is_pmd_migration_entry(pmd_t pmd) }#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ +#if defined(CONFIG_ZONE_DEVICE ) && defined(CONFIG_ARCH_ENABLE_THP_MIGRATION ) + +/** +* is_pmd_device_private_entry() - Check if PMD contains a device private swap entry +* @pmd: The PMD to check +* +* Returns true if the PMD contains a swap entry that represents a device private +* page mapping. This is used for zone device private pages that have been +* swapped out but still need special handling during various memory management +* operations. +* +* Return: 1 if PMD contains device private entry, 0 otherwise +*/ +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return is_swap_pmd (pmd) &&is_device_private_entry (pmd_to_swp_entry (pmd)); +} + +#else /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ + +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return 0 ; +} + +#endif /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ +static inline int non_swap_entry (swp_entry_t entry) {return swp_type (entry) >=MAX_SWAPFILES ; }
Quoted 5 lines
include/linux/swapops.h static inline int is_pmd_migration_entry(pmd_t pmd) · line 629
Quoted 1 line
include/linux/swapops.h +1
-0+static inline int is_pmd_non_present_folio_entry (pmd_t pmd)I can't think of a better name either although I am curious why open-coding it was so nasty given we don't have the equivalent for pte entries. Will go read the previous discussion.
I think for PTEs we just handle all cases (markers, hwpoison etc) properly, manye not being supported yet on the PMD level. See copy_nonpresent_pte() as an example.
We don't even have helpers like is_pte_migration_entry().
Quoted 6 lines
Quoted 6 lines
mm/pgtable-generic.c +0
-0@@ -290,7 +290,7 @@ pte_t *___pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
Quoted 6 lines
mm/pgtable-generic.c pte_t *___pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) · line 293
Quoted 4 lines
mm/pgtable-generic.c +1
-1if (pmdvalp) *pmdvalp = pmdval; -if (unlikely (pmd_none (pmdval) ||is_pmd_migration_entry (pmdval))) +if (unlikely (pmd_none (pmdval) || !pmd_present (pmdval)))Why isn't
is_pmd_non_present_folio_entry()used here?
I thought I argued that
if (!pmd_present(pmdval)))
Should be sufficient here in my last review?
We want to detect page tables we can map after all.
Signature · 3 lines
Cheers David / dhildenb
Alistair Popple
reviewerRe: [v6 02/15] mm/huge_memory: add device-private THP support to PMD operations
On 2025-09-25 at 19:53 +1000, David Hildenbrand <[email protected]> wrote...
Quoted 108 lines · Alistair Popple
Quoted 79 lines
On 2025-09-16 at 22:21 +1000, Balbir Singh <[email protected]> wrote...
Quoted 78 lines
Extend core huge page management functions to handle device-private THP entries. This enables proper handling of large device-private folios in fundamental MM operations.
The following functions have been updated:
-
copy_huge_pmd(): Handle device-private entries during fork/clone
-zap_huge_pmd(): Properly free device-private THP during munmap
-change_huge_pmd(): Support protection changes on device-private THP
-__pte_offset_map(): Add device-private entry awarenessSigned-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/swapops.h | 32 +++++++++++++++++++++++ mm/huge_memory.c | 56 ++++++++++++++++++++++++++++++++++------- mm/pgtable-generic.c | 2 +- 3 files changed, 80 insertions(+), 10 deletions(-)include/linux/swapops.h +27
-0@@ -594,10 +594,42 @@ static inline int is_pmd_migration_entry(pmd_t pmd) }#endif /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ +#if defined(CONFIG_ZONE_DEVICE ) && defined(CONFIG_ARCH_ENABLE_THP_MIGRATION ) + +/** +* is_pmd_device_private_entry() - Check if PMD contains a device private swap entry +* @pmd: The PMD to check +* +* Returns true if the PMD contains a swap entry that represents a device private +* page mapping. This is used for zone device private pages that have been +* swapped out but still need special handling during various memory management +* operations. +* +* Return: 1 if PMD contains device private entry, 0 otherwise +*/ +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return is_swap_pmd (pmd) &&is_device_private_entry (pmd_to_swp_entry (pmd)); +} + +#else /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ + +static inline int is_pmd_device_private_entry (pmd_t pmd) +{ +return 0 ; +} + +#endif /* CONFIG_ZONE_DEVICE && CONFIG_ARCH_ENABLE_THP_MIGRATION */ +static inline int non_swap_entry (swp_entry_t entry)Quoted 8 lines
include/linux/swapops.h static inline int is_pmd_migration_entry(pmd_t pmd) · line 629
Quoted 4 lines
include/linux/swapops.h +1
-0{return swp_type (entry) >=MAX_SWAPFILES ; } +static inline int is_pmd_non_present_folio_entry (pmd_t pmd)I can't think of a better name either although I am curious why open-coding it was so nasty given we don't have the equivalent for pte entries. Will go read the previous discussion.
I think for PTEs we just handle all cases (markers, hwpoison etc) properly, manye not being supported yet on the PMD level. See
copy_nonpresent_pte()as an example.We don't even have helpers like
is_pte_migration_entry().Quoted 11 lines
mm/pgtable-generic.c pte_t *___pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) · line 292
Quoted 9 lines
mm/pgtable-generic.c +1
-1@@ -290,7 +290,7 @@ pte_t *___pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)if (pmdvalp) *pmdvalp = pmdval; -if (unlikely (pmd_none (pmdval) ||is_pmd_migration_entry (pmdval))) +if (unlikely (pmd_none (pmdval) || !pmd_present (pmdval)))Why isn't
is_pmd_non_present_folio_entry()used here?
Quoted 5 lines
I thought I argued that
if (!pmd_present(pmdval)))Should be sufficient here in my last review?
My bad, I'm a bit behind catching up on the last review comments. But agree it's sufficient, was just curious why it wasn't used so will go read your previous comments! Thanks.
Quoted 6 lines
We want to detect page tables we can map after all.
Signature · 3 lines
Cheers David / dhildenb
PATCH v6 3/15 mm/rmap: extend rmap and migration support device-private entries
In reply to Balbir SinghAdd device-private THP support to reverse mapping infrastructure, enabling proper handling during migration and walk operations.
The key changes are:
- add_migration_pmd()/remove_migration_pmd(): Handle device-private entries during folio migration and splitting
- page_vma_mapped_walk(): Recognize device-private THP entries during VMA traversal operations
This change supports folio splitting and migration operations on device-private entries.
--- mm/damon/ops-common.c | 20 +++++++++++++++++--- mm/huge_memory.c | 16 +++++++++++++++- mm/page_idle.c | 7 +++++-- mm/page_vma_mapped.c | 7 +++++++ mm/rmap.c | 21 +++++++++++++++++---- 5 files changed, 61 insertions(+), 10 deletions(-)
mm/damon/ops-common.c +17 -3
@@ -75,12 +75,24 @@ void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addrvoid damon_pmdp_mkold (pmd_t *pmd,struct vm_area_struct *vma,unsigned long addr) {#ifdef CONFIG_TRANSPARENT_HUGEPAGE -struct folio *folio =damon_get_folio (pmd_pfn (pmdp_get (pmd))); +pmd_t pmdval =pmdp_get (pmd); +struct folio *folio; +bool young = false; +unsigned long pfn; + +if (likely (pmd_present (pmdval))) + pfn =pmd_pfn (pmdval); +else + pfn =swp_offset_pfn (pmd_to_swp_entry (pmdval)); + folio =damon_get_folio (pfn);if (!folio)return ; -if (pmdp_clear_young_notify (vma, addr, pmd)) +if (likely (pmd_present (pmdval))) + young |=pmdp_clear_young_notify (vma, addr, pmd); + young |=mmu_notifier_clear_young (vma->vm_mm, addr, addr +PAGE_SIZE ); +if (young)folio_set_young (folio);folio_set_idle (folio);
@@ -203,7 +215,9 @@ static bool damon_folio_young_one(struct folio *folio,mmu_notifier_test_young (vma->vm_mm, addr); }else {#ifdef CONFIG_TRANSPARENT_HUGEPAGE - *accessed =pmd_young (pmdp_get (pvmw.pmd)) || +pmd_t pmd =pmdp_get (pvmw.pmd); + + *accessed = (pmd_present (pmd) &&pmd_young (pmd)) || !folio_test_idle (folio) ||mmu_notifier_test_young (vma->vm_mm, addr);#else
mm/huge_memory.c +15 -1
@@ -4637,7 +4637,10 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,return 0 ;flush_cache_range (vma, address, address +HPAGE_PMD_SIZE ); - pmdval =pmdp_invalidate (vma, address, pvmw->pmd); +if (unlikely (!pmd_present (*pvmw->pmd))) + pmdval =pmdp_huge_get_and_clear (vma->vm_mm, address, pvmw->pmd); +else + pmdval =pmdp_invalidate (vma, address, pvmw->pmd);/* See folio_try_share_anon_rmap_pmd(): invalidate PMD first. */ anon_exclusive =folio_test_anon (folio) &&PageAnonExclusive (page);
@@ -4687,6 +4690,17 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new) entry =pmd_to_swp_entry (*pvmw->pmd);folio_get (folio); pmde =folio_mk_pmd (folio,READ_ONCE (vma->vm_page_prot)); + +if (folio_is_device_private (folio)) { +if (pmd_write (pmde)) + entry =make_writable_device_private_entry ( +page_to_pfn (new)); +else + entry =make_readable_device_private_entry ( +page_to_pfn (new)); + pmde =swp_entry_to_pmd (entry); + } +if (pmd_swp_soft_dirty (*pvmw->pmd)) pmde =pmd_mksoft_dirty (pmde);if (is_writable_migration_entry (entry))
mm/page_idle.c +5 -2
@@ -71,8 +71,11 @@ static bool page_idle_clear_pte_refs_one(struct folio *folio, referenced |=ptep_test_and_clear_young (vma, addr, pvmw.pte); referenced |=mmu_notifier_clear_young (vma->vm_mm, addr, addr +PAGE_SIZE ); }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { -if (pmdp_clear_young_notify (vma, addr, pvmw.pmd)) - referenced = true; +pmd_t pmdval =pmdp_get (pvmw.pmd); + +if (likely (pmd_present (pmdval))) + referenced |=pmdp_clear_young_notify (vma, addr, pvmw.pmd); + referenced |=mmu_notifier_clear_young (vma->vm_mm, addr, addr +PAGE_SIZE ); }else {/* unexpected pmd-mapped page? */ WARN_ON_ONCE (1 );
mm/page_vma_mapped.c +7 -0
@@ -277,6 +277,13 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)* cannot return prematurely, while zap_huge_pmd() has * cleared *pmd but not decremented compound_mapcount(). */ +swp_entry_t entry =pmd_to_swp_entry (pmde); + +if (is_device_private_entry (entry)) { + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +return true; + } + if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&
mm/rmap.c +17 -4
@@ -1063,9 +1063,11 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw) }else {#ifdef CONFIG_TRANSPARENT_HUGEPAGE pmd_t *pmd = pvmw->pmd; -pmd_t entry; +pmd_t entry =pmdp_get (pmd); -if (!pmd_dirty (*pmd) && !pmd_write (*pmd)) +if (!pmd_present (entry)) +continue ; +if (!pmd_dirty (entry) && !pmd_write (entry))continue ; flush_cache_range(vma, address,
@@ -2330,6 +2332,11 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,while (page_vma_mapped_walk (&pvmw)) {/* PMD-mapped THP migration entry */ if (!pvmw.pte) { +#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION +unsigned long pfn; +pmd_t pmdval; +#endif +if (flags &TTU_SPLIT_HUGE_PMD ) {split_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, true);
@@ -2338,8 +2345,14 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,break ; }#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION - subpage =folio_page (folio, -pmd_pfn (*pvmw.pmd) -folio_pfn (folio)); + pmdval =pmdp_get (pvmw.pmd); +if (likely (pmd_present (pmdval))) + pfn =pmd_pfn (pmdval); +else + pfn =swp_offset_pfn (pmd_to_swp_entry (pmdval)); + + subpage =folio_page (folio, pfn -folio_pfn (folio)); +VM_BUG_ON_FOLIO (folio_test_hugetlb (folio) || !folio_test_pmd_mappable (folio), folio);
Signature · 1 line
2.50.1
Zi Yan
reviewerRe: [v6 03/15] mm/rmap: extend rmap and migration support device-private entries
Quoted 63 lines · Balbir Singh
Add device-private THP support to reverse mapping infrastructure, enabling proper handling during migration and walk operations.
The key changes are:
- add_migration_pmd()/remove_migration_pmd(): Handle device-private entries during folio migration and splitting
-page_vma_mapped_walk(): Recognize device-private THP entries during VMA traversal operationsThis change supports folio splitting and migration operations on device-private entries.
Signed-off-by Balbir Singh <[email protected]>Reviewed-by SeongJae Park <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/damon/ops-common.c | 20 +++++++++++++++++--- mm/huge_memory.c | 16 +++++++++++++++- mm/page_idle.c | 7 +++++-- mm/page_vma_mapped.c | 7 +++++++ mm/rmap.c | 21 +++++++++++++++++---- 5 files changed, 61 insertions(+), 10 deletions(-)mm/damon/ops-common.c +8
-1@@ -75,12 +75,24 @@ void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addrvoid damon_pmdp_mkold (pmd_t *pmd,struct vm_area_struct *vma,unsigned long addr) {#ifdef CONFIG_TRANSPARENT_HUGEPAGE -struct folio *folio =damon_get_folio (pmd_pfn (pmdp_get (pmd))); +pmd_t pmdval =pmdp_get (pmd); +struct folio *folio; +bool young = false; +unsigned long pfn; + +if (likely (pmd_present (pmdval))) + pfn =pmd_pfn (pmdval); +else
mm/damon/ops-common.c void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addr · line 94
Quoted 10 lines
mm/damon/ops-common.c +5
-1+ pfn =swp_offset_pfn (pmd_to_swp_entry (pmdval)); + folio =damon_get_folio (pfn);if (!folio)return ; -if (pmdp_clear_young_notify (vma, addr, pmd)) +if (likely (pmd_present (pmdval))) + young |=pmdp_clear_young_notify (vma, addr, pmd); + young |=mmu_notifier_clear_young (vma->vm_mm, addr, addr +PAGE_SIZE );
This should be HPAGE_PMD_SIZE (it is guarded in CONFIG_TRANSPARENT_HUGEPAGE, so HPAGE_PMD_SIZE will not trigger a build bug like the one below).
Quoted 54 lines
mm/damon/ops-common.c +4
-1+if (young)folio_set_young (folio);folio_set_idle (folio);@@ -203,7 +215,9 @@ static bool damon_folio_young_one(struct folio *folio,mmu_notifier_test_young (vma->vm_mm, addr); }else {#ifdef CONFIG_TRANSPARENT_HUGEPAGE - *accessed =pmd_young (pmdp_get (pvmw.pmd)) || +pmd_t pmd =pmdp_get (pvmw.pmd); + + *accessed = (pmd_present (pmd) &&pmd_young (pmd)) || !folio_test_idle (folio) ||mmu_notifier_test_young (vma->vm_mm, addr);#else mm/huge_memory.c +15
-1@@ -4637,7 +4637,10 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,return 0 ;flush_cache_range (vma, address, address +HPAGE_PMD_SIZE ); - pmdval =pmdp_invalidate (vma, address, pvmw->pmd); +if (unlikely (!pmd_present (*pvmw->pmd))) + pmdval =pmdp_huge_get_and_clear (vma->vm_mm, address, pvmw->pmd); +else + pmdval =pmdp_invalidate (vma, address, pvmw->pmd);/* See folio_try_share_anon_rmap_pmd(): invalidate PMD first. */ anon_exclusive =folio_test_anon (folio) &&PageAnonExclusive (page);@@ -4687,6 +4690,17 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new) entry =pmd_to_swp_entry (*pvmw->pmd);folio_get (folio); pmde =folio_mk_pmd (folio,READ_ONCE (vma->vm_page_prot)); + +if (folio_is_device_private (folio)) { +if (pmd_write (pmde)) + entry =make_writable_device_private_entry ( +page_to_pfn (new)); +else + entry =make_readable_device_private_entry ( +page_to_pfn (new)); + pmde =swp_entry_to_pmd (entry); + } +if (pmd_swp_soft_dirty (*pvmw->pmd)) pmde =pmd_mksoft_dirty (pmde);if (is_writable_migration_entry (entry))mm/page_idle.c +0
-0
mm/page_idle.c static bool page_idle_clear_pte_refs_one(struct folio *folio, · line 78
Quoted 10 lines
mm/page_idle.c +5
-2@@ -71,8 +71,11 @@ static bool page_idle_clear_pte_refs_one(struct folio *folio, referenced |=ptep_test_and_clear_young (vma, addr, pvmw.pte); referenced |=mmu_notifier_clear_young (vma->vm_mm, addr, addr +PAGE_SIZE ); }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { -if (pmdp_clear_young_notify (vma, addr, pvmw.pmd)) - referenced = true; +pmd_t pmdval =pmdp_get (pvmw.pmd); + +if (likely (pmd_present (pmdval))) + referenced |=pmdp_clear_young_notify (vma, addr, pvmw.pmd); + referenced |=mmu_notifier_clear_young (vma->vm_mm, addr, addr +PAGE_SIZE );
This should be HPAGE_PMD_SIZE (or PMD_SIZE, since the code is not compiled out when CONFIG_TRANSPARENT_HUGEPAGE is not selected and HPAGE_PMD_SIZE will cause a build bug when CONFIG_PGTABLE_HAS_HUGE_LEAVES is not selected).
Quoted 26 lines
mm/page_idle.c +0
-0}else {/* unexpected pmd-mapped page? */ WARN_ON_ONCE (1 );mm/page_vma_mapped.c +7
-0@@ -277,6 +277,13 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)* cannot return prematurely, while zap_huge_pmd() has * cleared *pmd but not decremented compound_mapcount(). */ +swp_entry_t entry =pmd_to_swp_entry (pmde); + +if (is_device_private_entry (entry)) { + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +return true; + } + if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&mm/rmap.c +0
-0
mm/rmap.c static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw) · line 1068
Quoted 7 lines
mm/rmap.c +1
-2@@ -1063,9 +1063,11 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw) }else {#ifdef CONFIG_TRANSPARENT_HUGEPAGE pmd_t *pmd = pvmw->pmd; -pmd_t entry; +pmd_t entry =pmdp_get (pmd); -if (!pmd_dirty (*pmd) && !pmd_write (*pmd))
It is better to add a similar comment as the one above !pte_present(). Something like:
PFN swap PMDs, such as ...
Quoted 7 lines
mm/rmap.c +3
-0+if (!pmd_present (entry)) +continue ; +if (!pmd_dirty (entry) && !pmd_write (entry))continue ; flush_cache_range(vma, address,
mm/rmap.c static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, · line 2339
Quoted 8 lines
mm/rmap.c +5
-0@@ -2330,6 +2332,11 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,while (page_vma_mapped_walk (&pvmw)) {/* PMD-mapped THP migration entry */ if (!pvmw.pte) { +#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION +unsigned long pfn; +pmd_t pmdval; +#endif +
This looks ugly. IIRC, we now can put variable definition in the middle. Maybe for this case, these two can be moved to the below ifdef region.
Quoted 22 lines
mm/rmap.c +8
-2if (flags &TTU_SPLIT_HUGE_PMD ) {split_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, true);@@ -2338,8 +2345,14 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,break ; }#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION - subpage =folio_page (folio, -pmd_pfn (*pvmw.pmd) -folio_pfn (folio)); + pmdval =pmdp_get (pvmw.pmd); +if (likely (pmd_present (pmdval))) + pfn =pmd_pfn (pmdval); +else + pfn =swp_offset_pfn (pmd_to_swp_entry (pmdval)); + + subpage =folio_page (folio, pfn -folio_pfn (folio)); +VM_BUG_ON_FOLIO (folio_test_hugetlb (folio) || !folio_test_pmd_mappable (folio), folio);Signature · 1 line
2.50.1
Otherwise, LGTM. Acked-by: Zi Yan <[email protected]>
Best Regards,
Yan, Zi
Re: [v6 03/15] mm/rmap: extend rmap and migration support device-private entries
Quoted 139 lines · Zi Yan
Quoted 63 lines · Balbir Singh
Add device-private THP support to reverse mapping infrastructure, enabling proper handling during migration and walk operations.
The key changes are:
- add_migration_pmd()/remove_migration_pmd(): Handle device-private entries during folio migration and splitting
-page_vma_mapped_walk(): Recognize device-private THP entries during VMA traversal operationsThis change supports folio splitting and migration operations on device-private entries.
Signed-off-by Balbir Singh <[email protected]>Reviewed-by SeongJae Park <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/damon/ops-common.c | 20 +++++++++++++++++--- mm/huge_memory.c | 16 +++++++++++++++- mm/page_idle.c | 7 +++++-- mm/page_vma_mapped.c | 7 +++++++ mm/rmap.c | 21 +++++++++++++++++---- 5 files changed, 61 insertions(+), 10 deletions(-)mm/damon/ops-common.c +8
-1@@ -75,12 +75,24 @@ void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addrvoid damon_pmdp_mkold (pmd_t *pmd,struct vm_area_struct *vma,unsigned long addr) {#ifdef CONFIG_TRANSPARENT_HUGEPAGE -struct folio *folio =damon_get_folio (pmd_pfn (pmdp_get (pmd))); +pmd_t pmdval =pmdp_get (pmd); +struct folio *folio; +bool young = false; +unsigned long pfn; + +if (likely (pmd_present (pmdval))) + pfn =pmd_pfn (pmdval); +else mm/damon/ops-common.c void damon_ptep_mkold(pte_t *pte, struct vm_area_struct *vma, unsigned long addr · line 94
Quoted 10 lines
mm/damon/ops-common.c +5
-1+ pfn =swp_offset_pfn (pmd_to_swp_entry (pmdval)); + folio =damon_get_folio (pfn);if (!folio)return ; -if (pmdp_clear_young_notify (vma, addr, pmd)) +if (likely (pmd_present (pmdval))) + young |=pmdp_clear_young_notify (vma, addr, pmd); + young |=mmu_notifier_clear_young (vma->vm_mm, addr, addr +PAGE_SIZE );This should be
HPAGE_PMD_SIZE(it is guarded inCONFIG_TRANSPARENT_HUGEPAGE, soHPAGE_PMD_SIZEwill not trigger a build bug like the one below).Quoted 60 lines
mm/damon/ops-common.c +4
-1+if (young)folio_set_young (folio);folio_set_idle (folio);@@ -203,7 +215,9 @@ static bool damon_folio_young_one(struct folio *folio,mmu_notifier_test_young (vma->vm_mm, addr); }else {#ifdef CONFIG_TRANSPARENT_HUGEPAGE - *accessed =pmd_young (pmdp_get (pvmw.pmd)) || +pmd_t pmd =pmdp_get (pvmw.pmd); + + *accessed = (pmd_present (pmd) &&pmd_young (pmd)) || !folio_test_idle (folio) ||mmu_notifier_test_young (vma->vm_mm, addr);#else mm/huge_memory.c +15
-1@@ -4637,7 +4637,10 @@ int set_pmd_migration_entry(struct page_vma_mapped_walk *pvmw,return 0 ;flush_cache_range (vma, address, address +HPAGE_PMD_SIZE ); - pmdval =pmdp_invalidate (vma, address, pvmw->pmd); +if (unlikely (!pmd_present (*pvmw->pmd))) + pmdval =pmdp_huge_get_and_clear (vma->vm_mm, address, pvmw->pmd); +else + pmdval =pmdp_invalidate (vma, address, pvmw->pmd);/* See folio_try_share_anon_rmap_pmd(): invalidate PMD first. */ anon_exclusive =folio_test_anon (folio) &&PageAnonExclusive (page);@@ -4687,6 +4690,17 @@ void remove_migration_pmd(struct page_vma_mapped_walk *pvmw, struct page *new) entry =pmd_to_swp_entry (*pvmw->pmd);folio_get (folio); pmde =folio_mk_pmd (folio,READ_ONCE (vma->vm_page_prot)); + +if (folio_is_device_private (folio)) { +if (pmd_write (pmde)) + entry =make_writable_device_private_entry ( +page_to_pfn (new)); +else + entry =make_readable_device_private_entry ( +page_to_pfn (new)); + pmde =swp_entry_to_pmd (entry); + } +if (pmd_swp_soft_dirty (*pvmw->pmd)) pmde =pmd_mksoft_dirty (pmde);if (is_writable_migration_entry (entry))mm/page_idle.c +1
-2@@ -71,8 +71,11 @@ static bool page_idle_clear_pte_refs_one(struct folio *folio, referenced |=ptep_test_and_clear_young (vma, addr, pvmw.pte); referenced |=mmu_notifier_clear_young (vma->vm_mm, addr, addr +PAGE_SIZE ); }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { -if (pmdp_clear_young_notify (vma, addr, pvmw.pmd)) - referenced = true; +pmd_t pmdval =pmdp_get (pvmw.pmd);
Quoted 8 lines
mm/page_idle.c static bool page_idle_clear_pte_refs_one(struct folio *folio, · line 78
Quoted 4 lines
mm/page_idle.c +4
-0+ +if (likely (pmd_present (pmdval))) + referenced |=pmdp_clear_young_notify (vma, addr, pvmw.pmd); + referenced |=mmu_notifier_clear_young (vma->vm_mm, addr, addr +PAGE_SIZE );This should be
HPAGE_PMD_SIZE(orPMD_SIZE, since the code is not compiled out whenCONFIG_TRANSPARENT_HUGEPAGEis not selected andHPAGE_PMD_SIZEwill cause a build bug whenCONFIG_PGTABLE_HAS_HUGE_LEAVESis not selected).
I'll protect it accordingly, thanks!
Quoted 33 lines
Quoted 33 lines
mm/page_idle.c +0
-0}else {/* unexpected pmd-mapped page? */ WARN_ON_ONCE (1 );mm/page_vma_mapped.c +7
-0@@ -277,6 +277,13 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)* cannot return prematurely, while zap_huge_pmd() has * cleared *pmd but not decremented compound_mapcount(). */ +swp_entry_t entry =pmd_to_swp_entry (pmde); + +if (is_device_private_entry (entry)) { + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +return true; + } + if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&mm/rmap.c +1
-1@@ -1063,9 +1063,11 @@ static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw) }else {#ifdef CONFIG_TRANSPARENT_HUGEPAGE pmd_t *pmd = pvmw->pmd; -pmd_t entry; +pmd_t entry =pmdp_get (pmd);
Quoted 7 lines
mm/rmap.c static int page_vma_mkclean_one(struct page_vma_mapped_walk *pvmw) · line 1068
Quoted 1 line
mm/rmap.c +0
-1-if (!pmd_dirty (*pmd) && !pmd_write (*pmd))It is better to add a similar comment as the one above !pte_present(). Something like:
PFN swap PMDs, such as ...
Sure, can do and repeat the comment or just say look at the comments for !pte_present() :)
Quoted 5 lines
Quoted 5 lines
mm/rmap.c +3
-0+if (!pmd_present (entry)) +continue ; +if (!pmd_dirty (entry) && !pmd_write (entry))continue ;
Quoted 14 lines
mm/rmap.c static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, · line 2339
Quoted 10 lines
mm/rmap.c +5
-0flush_cache_range(vma, address,@@ -2330,6 +2332,11 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,while (page_vma_mapped_walk (&pvmw)) {/* PMD-mapped THP migration entry */ if (!pvmw.pte) { +#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION +unsigned long pfn; +pmd_t pmdval; +#endif +This looks ugly. IIRC, we now can put variable definition in the middle. Maybe for this case, these two can be moved to the below ifdef region.
I can't find any examples of mixing declarations and could not find any clear guidance in the coding style
Quoted 20 lines
Quoted 20 lines
mm/rmap.c +8
-2if (flags &TTU_SPLIT_HUGE_PMD ) {split_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, true);@@ -2338,8 +2345,14 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,break ; }#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION - subpage =folio_page (folio, -pmd_pfn (*pvmw.pmd) -folio_pfn (folio)); + pmdval =pmdp_get (pvmw.pmd); +if (likely (pmd_present (pmdval))) + pfn =pmd_pfn (pmdval); +else + pfn =swp_offset_pfn (pmd_to_swp_entry (pmdval)); + + subpage =folio_page (folio, pfn -folio_pfn (folio)); +VM_BUG_ON_FOLIO (folio_test_hugetlb (folio) || !folio_test_pmd_mappable (folio), folio);
Quoted 4 lines
Quoted 2 lines
Signature · 1 line
2.50.1Otherwise, LGTM. Acked-by: Zi Yan <[email protected]>
Thanks for the review,
Balbir
Re: [v6 03/15] mm/rmap: extend rmap and migration support device-private entries
Quoted 21 lines
Quoted 6 lines
Quoted 6 lines
mm/rmap.c +3
-0+if (!pmd_present (entry)) +continue ; +if (!pmd_dirty (entry) && !pmd_write (entry))continue ;Quoted 14 lines
mm/rmap.c static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, · line 2339
Quoted 10 lines
flush_cache_range(vma, address,
mm/rmap.c +5
-0@@ -2330,6 +2332,11 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,while (page_vma_mapped_walk (&pvmw)) {/* PMD-mapped THP migration entry */ if (!pvmw.pte) { +#ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION +unsigned long pfn; +pmd_t pmdval; +#endif +This looks ugly. IIRC, we now can put variable definition in the middle. Maybe for this case, these two can be moved to the below ifdef region.
Quoted 2 lines
I can't find any examples of mixing declarations and could not find any clear guidance in the coding style
Rather not do it :)
__maybe_unsed might help avoiding the ifdef.
Signature · 3 lines
Cheers David / dhildenb
PATCH v6 4/15 mm/huge_memory: implement device-private THP splitting
In reply to Balbir SinghAdd support for splitting device-private THP folios, enabling fallback to smaller page sizes when large page allocation or migration fails.
Key changes:
- split_huge_pmd(): Handle device-private PMD entries during splitting
- Preserve RMAP_EXCLUSIVE semantics for anonymous exclusive folios
- Skip RMP_USE_SHARED_ZEROPAGE for device-private entries as they don't support shared zero page semantics
--- mm/huge_memory.c | 138 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 40 deletions(-)
mm/huge_memory.c +98 -40
@@ -2872,16 +2872,18 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,struct page *page;pgtable_t pgtable;pmd_t old_pmd, _pmd; -bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false; -bool anon_exclusive = false, dirty = false; +bool soft_dirty, uffd_wp = false, young = false, write = false; +bool anon_exclusive = false, dirty = false, present = false;unsigned long addr;pte_t *pte;int i; +swp_entry_t swp_entry;VM_BUG_ON (haddr & ~HPAGE_PMD_MASK );VM_BUG_ON_VMA (vma->vm_start > haddr, vma);VM_BUG_ON_VMA (vma->vm_end < haddr +HPAGE_PMD_SIZE , vma); -VM_BUG_ON (!is_pmd_migration_entry (*pmd) && !pmd_trans_huge (*pmd)); + +VM_WARN_ON (!is_pmd_non_present_folio_entry (*pmd) && !pmd_trans_huge (*pmd));count_vm_event (THP_SPLIT_PMD );
@@ -2929,20 +2931,47 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,return __split_huge_zero_page_pmd (vma, haddr, pmd); } - pmd_migration =is_pmd_migration_entry (*pmd); -if (unlikely (pmd_migration)) { -swp_entry_t entry; + present =pmd_present (*pmd); +if (is_pmd_migration_entry (*pmd)) { old_pmd = *pmd; - entry =pmd_to_swp_entry (old_pmd); - page =pfn_swap_entry_to_page (entry); - write =is_writable_migration_entry (entry); + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_migration_entry (swp_entry);if (PageAnon (page)) - anon_exclusive =is_readable_exclusive_migration_entry (entry); - young =is_migration_entry_young (entry); - dirty =is_migration_entry_dirty (entry); + anon_exclusive =is_readable_exclusive_migration_entry (swp_entry); + young =is_migration_entry_young (swp_entry); + dirty =is_migration_entry_dirty (swp_entry); + }else if (is_pmd_device_private_entry (*pmd)) { + old_pmd = *pmd; + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + soft_dirty =pmd_swp_soft_dirty (old_pmd); uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_device_private_entry (swp_entry); + anon_exclusive =PageAnonExclusive (page); + +if (freeze && anon_exclusive && +folio_try_share_anon_rmap_pmd (folio, page)) + freeze = false; +if (!freeze) { +rmap_t rmap_flags =RMAP_NONE ; + +folio_ref_add (folio,HPAGE_PMD_NR -1 ); +if (anon_exclusive) + rmap_flags |=RMAP_EXCLUSIVE ; + +folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , + vma, haddr, rmap_flags); + } }else {/* * Up to this point the pmd is present and huge and userland has
@@ -3026,32 +3055,57 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,* Note that NUMA hinting access restrictions are not transferred to * avoid any possibility of altering permissions across VMAs. */ -if (freeze || pmd_migration) { -for (i =0 , addr = haddr; i <HPAGE_PMD_NR ; i++, addr +=PAGE_SIZE ) { -pte_t entry; -swp_entry_t swp_entry; - -if (write) - swp_entry =make_writable_migration_entry ( -page_to_pfn (page + i)); -else if (anon_exclusive) - swp_entry =make_readable_exclusive_migration_entry ( -page_to_pfn (page + i)); -else - swp_entry =make_readable_migration_entry ( -page_to_pfn (page + i)); -if (young) - swp_entry =make_migration_entry_young (swp_entry); -if (dirty) - swp_entry =make_migration_entry_dirty (swp_entry); - entry =swp_entry_to_pte (swp_entry); -if (soft_dirty) - entry =pte_swp_mksoft_dirty (entry); -if (uffd_wp) - entry =pte_swp_mkuffd_wp (entry); +if (freeze || !present) { +pte_t entry; -VM_WARN_ON (!pte_none (ptep_get (pte + i))); -set_pte_at (mm, addr, pte + i, entry); +if (freeze ||is_migration_entry (swp_entry)) { +for (i =0 , addr = haddr; i <HPAGE_PMD_NR ; i++, addr +=PAGE_SIZE ) { +if (write) + swp_entry =make_writable_migration_entry ( +page_to_pfn (page + i)); +else if (anon_exclusive) + swp_entry =make_readable_exclusive_migration_entry ( +page_to_pfn (page + i)); +else + swp_entry =make_readable_migration_entry ( +page_to_pfn (page + i)); +if (young) + swp_entry =make_migration_entry_young (swp_entry); +if (dirty) + swp_entry =make_migration_entry_dirty (swp_entry); + + entry =swp_entry_to_pte (swp_entry); +if (soft_dirty) + entry =pte_swp_mksoft_dirty (entry); +if (uffd_wp) + entry =pte_swp_mkuffd_wp (entry); +VM_WARN_ON (!pte_none (ptep_get (pte + i))); +set_pte_at (mm, addr, pte + i, entry); + } + }else { +for (i =0 , addr = haddr; i <HPAGE_PMD_NR ; i++, addr +=PAGE_SIZE ) { +/* +* anon_exclusive was already propagated to the relevant +* pages corresponding to the pte entries when freeze +* is false. +*/ +if (write) + swp_entry =make_writable_device_private_entry ( +page_to_pfn (page + i)); +else + swp_entry =make_readable_device_private_entry ( +page_to_pfn (page + i)); +/* +* Young and dirty bits are not progated via swp_entry +*/ + entry =swp_entry_to_pte (swp_entry); +if (soft_dirty) + entry =pte_swp_mksoft_dirty (entry); +if (uffd_wp) + entry =pte_swp_mkuffd_wp (entry); +VM_WARN_ON (!pte_none (ptep_get (pte + i))); +set_pte_at (mm, addr, pte + i, entry); + } } }else {pte_t entry;
@@ -3076,7 +3130,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, }pte_unmap (pte); -if (!pmd_migration) +if (!is_pmd_migration_entry (*pmd))folio_remove_rmap_pmd (folio, page, vma);if (freeze)put_page (page);
@@ -3089,7 +3143,7 @@ void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze) {VM_WARN_ON_ONCE (!IS_ALIGNED (address,HPAGE_PMD_SIZE )); -if (pmd_trans_huge (*pmd) ||is_pmd_migration_entry (*pmd)) +if (pmd_trans_huge (*pmd) ||is_pmd_non_present_folio_entry (*pmd))__split_huge_pmd_locked (vma, pmd, address, freeze); }
@@ -3268,6 +3322,9 @@ static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,VM_BUG_ON_FOLIO (folio_test_lru (new_folio), folio);lockdep_assert_held (&lruvec->lru_lock); +if (folio_is_device_private (folio)) +return ; +if (list) {/* page reclaim is reclaiming a huge page */ VM_WARN_ON (folio_test_lru (folio));
@@ -3885,8 +3942,9 @@ static int __folio_split(struct folio *folio, unsigned int new_order,if (nr_shmem_dropped)shmem_uncharge (mapping->host, nr_shmem_dropped); -if (!ret && is_anon) +if (!ret && is_anon && !folio_is_device_private (folio)) remap_flags =RMP_USE_SHARED_ZEROPAGE ; +remap_page (folio,1 << order, remap_flags);/*
Signature · 1 line
2.50.1
Zi Yan
reviewerRe: [v6 04/15] mm/huge_memory: implement device-private THP splitting
Quoted 98 lines · Balbir Singh
Add support for splitting device-private THP folios, enabling fallback to smaller page sizes when large page allocation or migration fails.
Key changes:
-split_huge_pmd(): Handle device-private PMD entries during splitting
- PreserveRMAP_EXCLUSIVEsemantics for anonymous exclusive folios
- SkipRMP_USE_SHARED_ZEROPAGEfor device-private entries as they don't support shared zero page semanticsSigned-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/huge_memory.c | 138 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 40 deletions(-)mm/huge_memory.c +23
-12@@ -2872,16 +2872,18 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,struct page *page;pgtable_t pgtable;pmd_t old_pmd, _pmd; -bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false; -bool anon_exclusive = false, dirty = false; +bool soft_dirty, uffd_wp = false, young = false, write = false; +bool anon_exclusive = false, dirty = false, present = false;unsigned long addr;pte_t *pte;int i; +swp_entry_t swp_entry;VM_BUG_ON (haddr & ~HPAGE_PMD_MASK );VM_BUG_ON_VMA (vma->vm_start > haddr, vma);VM_BUG_ON_VMA (vma->vm_end < haddr +HPAGE_PMD_SIZE , vma); -VM_BUG_ON (!is_pmd_migration_entry (*pmd) && !pmd_trans_huge (*pmd)); + +VM_WARN_ON (!is_pmd_non_present_folio_entry (*pmd) && !pmd_trans_huge (*pmd));count_vm_event (THP_SPLIT_PMD );@@ -2929,20 +2931,47 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,return __split_huge_zero_page_pmd (vma, haddr, pmd); } - pmd_migration =is_pmd_migration_entry (*pmd); -if (unlikely (pmd_migration)) { -swp_entry_t entry; + present =pmd_present (*pmd); +if (is_pmd_migration_entry (*pmd)) { old_pmd = *pmd; - entry =pmd_to_swp_entry (old_pmd); - page =pfn_swap_entry_to_page (entry); - write =is_writable_migration_entry (entry); + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_migration_entry (swp_entry);if (PageAnon (page)) - anon_exclusive =is_readable_exclusive_migration_entry (entry); - young =is_migration_entry_young (entry); - dirty =is_migration_entry_dirty (entry); + anon_exclusive =is_readable_exclusive_migration_entry (swp_entry); + young =is_migration_entry_young (swp_entry); + dirty =is_migration_entry_dirty (swp_entry); + }else if (is_pmd_device_private_entry (*pmd)) { + old_pmd = *pmd; + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page);
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 2964
Quoted 10 lines
mm/huge_memory.c +8
-0+ soft_dirty =pmd_swp_soft_dirty (old_pmd); uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_device_private_entry (swp_entry); + anon_exclusive =PageAnonExclusive (page); + +if (freeze && anon_exclusive && +folio_try_share_anon_rmap_pmd (folio, page)) + freeze = false;
Why is it OK to change the freeze request? OK, it is replicating the code for present PMD folios. Either add a comment to point to the explanation in the comment below, or move
“if (is_pmd_device_private_entry(*pmd))“ branch in the else below to deduplicate this code.
Quoted 47 lines
mm/huge_memory.c +13
-25+if (!freeze) { +rmap_t rmap_flags =RMAP_NONE ; + +folio_ref_add (folio,HPAGE_PMD_NR -1 ); +if (anon_exclusive) + rmap_flags |=RMAP_EXCLUSIVE ; + +folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , + vma, haddr, rmap_flags); + } }else {/* * Up to this point the pmd is present and huge and userland has @@ -3026,32 +3055,57 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,* Note that NUMA hinting access restrictions are not transferred to * avoid any possibility of altering permissions across VMAs. */ -if (freeze || pmd_migration) { -for (i =0 , addr = haddr; i <HPAGE_PMD_NR ; i++, addr +=PAGE_SIZE ) { -pte_t entry; -swp_entry_t swp_entry; - -if (write) - swp_entry =make_writable_migration_entry ( -page_to_pfn (page + i)); -else if (anon_exclusive) - swp_entry =make_readable_exclusive_migration_entry ( -page_to_pfn (page + i)); -else - swp_entry =make_readable_migration_entry ( -page_to_pfn (page + i)); -if (young) - swp_entry =make_migration_entry_young (swp_entry); -if (dirty) - swp_entry =make_migration_entry_dirty (swp_entry); - entry =swp_entry_to_pte (swp_entry); -if (soft_dirty) - entry =pte_swp_mksoft_dirty (entry); -if (uffd_wp) - entry =pte_swp_mkuffd_wp (entry); +if (freeze || !present) { +pte_t entry; -VM_WARN_ON (!pte_none (ptep_get (pte + i))); -set_pte_at (mm, addr, pte + i, entry); +if (freeze ||is_migration_entry (swp_entry)) {
<snip>
Quoted 1 line
mm/huge_memory.c +1
-0+ }else {
<snip>
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3111
Quoted 3 lines
mm/huge_memory.c +0
-0} }else {pte_t entry;
David already pointed this out in v5. It can be done such as:
if (freeze || pmd_migration) {
...
} else if (is_pmd_device_private_entry(old_pmd)) {
...
} else {
/* for present, non freeze case */
}
Quoted 29 lines
mm/huge_memory.c +5
-2@@ -3076,7 +3130,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, }pte_unmap (pte); -if (!pmd_migration) +if (!is_pmd_migration_entry (*pmd))folio_remove_rmap_pmd (folio, page, vma);if (freeze)put_page (page);@@ -3089,7 +3143,7 @@ void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze) {VM_WARN_ON_ONCE (!IS_ALIGNED (address,HPAGE_PMD_SIZE )); -if (pmd_trans_huge (*pmd) ||is_pmd_migration_entry (*pmd)) +if (pmd_trans_huge (*pmd) ||is_pmd_non_present_folio_entry (*pmd))__split_huge_pmd_locked (vma, pmd, address, freeze); }@@ -3268,6 +3322,9 @@ static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,VM_BUG_ON_FOLIO (folio_test_lru (new_folio), folio);lockdep_assert_held (&lruvec->lru_lock); +if (folio_is_device_private (folio)) +return ; +if (list) {/* page reclaim is reclaiming a huge page */ VM_WARN_ON (folio_test_lru (folio));
mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3947
Quoted 7 lines
mm/huge_memory.c +2
-1@@ -3885,8 +3942,9 @@ static int __folio_split(struct folio *folio, unsigned int new_order,if (nr_shmem_dropped)shmem_uncharge (mapping->host, nr_shmem_dropped); -if (!ret && is_anon) +if (!ret && is_anon && !folio_is_device_private (folio)) remap_flags =RMP_USE_SHARED_ZEROPAGE ; +
You should remove this and add
if (folio_is_device_private(folio))
return false;
in try_to_map_unused_to_zeropage(). Otherwise, no one would know device private folios need to be excluded from mapping unused to zero page.
Quoted 5 lines
mm/huge_memory.c +0
-0remap_page (folio,1 << order, remap_flags);/* Signature · 1 line
2.50.1
Best Regards,
Yan, Zi
Re: [v6 04/15] mm/huge_memory: implement device-private THP splitting
Quoted 108 lines · Zi Yan
Quoted 106 lines · Balbir Singh
Add support for splitting device-private THP folios, enabling fallback to smaller page sizes when large page allocation or migration fails.
Key changes:
-split_huge_pmd(): Handle device-private PMD entries during splitting
- PreserveRMAP_EXCLUSIVEsemantics for anonymous exclusive folios
- SkipRMP_USE_SHARED_ZEROPAGEfor device-private entries as they don't support shared zero page semanticsSigned-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/huge_memory.c | 138 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 40 deletions(-)mm/huge_memory.c +29
-12@@ -2872,16 +2872,18 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,struct page *page;pgtable_t pgtable;pmd_t old_pmd, _pmd; -bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false; -bool anon_exclusive = false, dirty = false; +bool soft_dirty, uffd_wp = false, young = false, write = false; +bool anon_exclusive = false, dirty = false, present = false;unsigned long addr;pte_t *pte;int i; +swp_entry_t swp_entry;VM_BUG_ON (haddr & ~HPAGE_PMD_MASK );VM_BUG_ON_VMA (vma->vm_start > haddr, vma);VM_BUG_ON_VMA (vma->vm_end < haddr +HPAGE_PMD_SIZE , vma); -VM_BUG_ON (!is_pmd_migration_entry (*pmd) && !pmd_trans_huge (*pmd)); + +VM_WARN_ON (!is_pmd_non_present_folio_entry (*pmd) && !pmd_trans_huge (*pmd));count_vm_event (THP_SPLIT_PMD );@@ -2929,20 +2931,47 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,return __split_huge_zero_page_pmd (vma, haddr, pmd); } - pmd_migration =is_pmd_migration_entry (*pmd); -if (unlikely (pmd_migration)) { -swp_entry_t entry; + present =pmd_present (*pmd); +if (is_pmd_migration_entry (*pmd)) { old_pmd = *pmd; - entry =pmd_to_swp_entry (old_pmd); - page =pfn_swap_entry_to_page (entry); - write =is_writable_migration_entry (entry); + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_migration_entry (swp_entry);if (PageAnon (page)) - anon_exclusive =is_readable_exclusive_migration_entry (entry); - young =is_migration_entry_young (entry); - dirty =is_migration_entry_dirty (entry); + anon_exclusive =is_readable_exclusive_migration_entry (swp_entry); + young =is_migration_entry_young (swp_entry); + dirty =is_migration_entry_dirty (swp_entry); + }else if (is_pmd_device_private_entry (*pmd)) { + old_pmd = *pmd; + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + soft_dirty =pmd_swp_soft_dirty (old_pmd); uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_device_private_entry (swp_entry); + anon_exclusive =PageAnonExclusive (page); + + if (freeze && anon_exclusive &&
Quoted 8 lines
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 2964
Quoted 2 lines
mm/huge_memory.c +2
-0+ folio_try_share_anon_rmap_pmd(folio, page)) + freeze = false;Why is it OK to change the freeze request? OK, it is replicating the code for present PMD folios. Either add a comment to point to the explanation in the comment below, or move
“if (is_pmd_device_private_entry(*pmd))“ branch in the else below to deduplicate this code.
Similar to the code for present pages, ideally folio_try_share_anon_rmap_pmd() should never fail.
Quoted 55 lines
Quoted 48 lines
mm/huge_memory.c +13
-25+if (!freeze) { +rmap_t rmap_flags =RMAP_NONE ; + +folio_ref_add (folio,HPAGE_PMD_NR -1 ); +if (anon_exclusive) + rmap_flags |=RMAP_EXCLUSIVE ; + +folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , + vma, haddr, rmap_flags); + } }else {/* * Up to this point the pmd is present and huge and userland has @@ -3026,32 +3055,57 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,* Note that NUMA hinting access restrictions are not transferred to * avoid any possibility of altering permissions across VMAs. */ -if (freeze || pmd_migration) { -for (i =0 , addr = haddr; i <HPAGE_PMD_NR ; i++, addr +=PAGE_SIZE ) { -pte_t entry; -swp_entry_t swp_entry; - -if (write) - swp_entry =make_writable_migration_entry ( -page_to_pfn (page + i)); -else if (anon_exclusive) - swp_entry =make_readable_exclusive_migration_entry ( -page_to_pfn (page + i)); -else - swp_entry =make_readable_migration_entry ( -page_to_pfn (page + i)); -if (young) - swp_entry =make_migration_entry_young (swp_entry); -if (dirty) - swp_entry =make_migration_entry_dirty (swp_entry); - entry =swp_entry_to_pte (swp_entry); -if (soft_dirty) - entry =pte_swp_mksoft_dirty (entry); -if (uffd_wp) - entry =pte_swp_mkuffd_wp (entry); +if (freeze || !present) { +pte_t entry; -VM_WARN_ON (!pte_none (ptep_get (pte + i))); -set_pte_at (mm, addr, pte + i, entry); +if (freeze ||is_migration_entry (swp_entry)) {<snip>
Quoted 1 line
mm/huge_memory.c +1
-0+ }else {<snip>
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3111
Quoted 3 lines
mm/huge_memory.c +0
-0} }else {pte_t entry;
Quoted 6 lines
David already pointed this out in v5. It can be done such as:
if (freeze ||
pmd_migration) {
...
} else if (is_pmd_device_private_entry(old_pmd)) {
...
No.. freeze can be true for device private entries as well
Quoted 43 lines
} else {
/* for present, non freeze case */
}Quoted 29 lines
mm/huge_memory.c +5
-2@@ -3076,7 +3130,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, }pte_unmap (pte); -if (!pmd_migration) +if (!is_pmd_migration_entry (*pmd))folio_remove_rmap_pmd (folio, page, vma);if (freeze)put_page (page);@@ -3089,7 +3143,7 @@ void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze) {VM_WARN_ON_ONCE (!IS_ALIGNED (address,HPAGE_PMD_SIZE )); -if (pmd_trans_huge (*pmd) ||is_pmd_migration_entry (*pmd)) +if (pmd_trans_huge (*pmd) ||is_pmd_non_present_folio_entry (*pmd))__split_huge_pmd_locked (vma, pmd, address, freeze); }@@ -3268,6 +3322,9 @@ static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,VM_BUG_ON_FOLIO (folio_test_lru (new_folio), folio);lockdep_assert_held (&lruvec->lru_lock); +if (folio_is_device_private (folio)) +return ; +if (list) {/* page reclaim is reclaiming a huge page */ VM_WARN_ON (folio_test_lru (folio));mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3947
Quoted 7 lines
mm/huge_memory.c +2
-1@@ -3885,8 +3942,9 @@ static int __folio_split(struct folio *folio, unsigned int new_order,if (nr_shmem_dropped)shmem_uncharge (mapping->host, nr_shmem_dropped); -if (!ret && is_anon) +if (!ret && is_anon && !folio_is_device_private (folio)) remap_flags =RMP_USE_SHARED_ZEROPAGE ; +You should remove this and add
Quoted 7 lines
if (
folio_is_device_private(folio))return false;in
try_to_map_unused_to_zeropage(). Otherwise, no one would know device private folios need to be excluded from mapping unused to zero page.
I had that upto v2 and then David asked me to remove it. FYI, this is the only call site for RMP_USE_SHARED_ZEROPAGE
Quoted 7 lines
Quoted 5 lines
mm/huge_memory.c +0
-0remap_page (folio,1 << order, remap_flags);/* Signature · 1 line
2.50.1
Thanks for the review
Balbir
Zi Yan
reviewerRe: [v6 04/15] mm/huge_memory: implement device-private THP splitting
Quoted 118 lines · Balbir Singh
Quoted 108 lines · Zi Yan
Quoted 106 lines · Balbir Singh
Add support for splitting device-private THP folios, enabling fallback to smaller page sizes when large page allocation or migration fails.
Key changes:
-split_huge_pmd(): Handle device-private PMD entries during splitting
- PreserveRMAP_EXCLUSIVEsemantics for anonymous exclusive folios
- SkipRMP_USE_SHARED_ZEROPAGEfor device-private entries as they don't support shared zero page semanticsSigned-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/huge_memory.c | 138 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 40 deletions(-)mm/huge_memory.c +29
-12@@ -2872,16 +2872,18 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,struct page *page;pgtable_t pgtable;pmd_t old_pmd, _pmd; -bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false; -bool anon_exclusive = false, dirty = false; +bool soft_dirty, uffd_wp = false, young = false, write = false; +bool anon_exclusive = false, dirty = false, present = false;unsigned long addr;pte_t *pte;int i; +swp_entry_t swp_entry;VM_BUG_ON (haddr & ~HPAGE_PMD_MASK );VM_BUG_ON_VMA (vma->vm_start > haddr, vma);VM_BUG_ON_VMA (vma->vm_end < haddr +HPAGE_PMD_SIZE , vma); -VM_BUG_ON (!is_pmd_migration_entry (*pmd) && !pmd_trans_huge (*pmd)); + +VM_WARN_ON (!is_pmd_non_present_folio_entry (*pmd) && !pmd_trans_huge (*pmd));count_vm_event (THP_SPLIT_PMD );@@ -2929,20 +2931,47 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,return __split_huge_zero_page_pmd (vma, haddr, pmd); } - pmd_migration =is_pmd_migration_entry (*pmd); -if (unlikely (pmd_migration)) { -swp_entry_t entry; + present =pmd_present (*pmd); +if (is_pmd_migration_entry (*pmd)) { old_pmd = *pmd; - entry =pmd_to_swp_entry (old_pmd); - page =pfn_swap_entry_to_page (entry); - write =is_writable_migration_entry (entry); + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_migration_entry (swp_entry);if (PageAnon (page)) - anon_exclusive =is_readable_exclusive_migration_entry (entry); - young =is_migration_entry_young (entry); - dirty =is_migration_entry_dirty (entry); + anon_exclusive =is_readable_exclusive_migration_entry (swp_entry); + young =is_migration_entry_young (swp_entry); + dirty =is_migration_entry_dirty (swp_entry); + }else if (is_pmd_device_private_entry (*pmd)) { + old_pmd = *pmd; + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + soft_dirty =pmd_swp_soft_dirty (old_pmd); uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_device_private_entry (swp_entry); + anon_exclusive =PageAnonExclusive (page); + + if (freeze && anon_exclusive &&Quoted 8 lines
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 2964
Quoted 2 lines
mm/huge_memory.c +2
-0+ folio_try_share_anon_rmap_pmd(folio, page)) + freeze = false;Why is it OK to change the freeze request? OK, it is replicating the code for present PMD folios. Either add a comment to point to the explanation in the comment below, or move
“if (is_pmd_device_private_entry(*pmd))“ branch in the else below to deduplicate this code.
Quoted 2 lines
Similar to the code for present pages, ideally
folio_try_share_anon_rmap_pmd()should never fail.
anon_exclusive = PageAnonExclusive(page);
if (freeze && anon_exclusive &&
folio_try_share_anon_rmap_pmd(folio, page))
freeze = false;
if (!freeze) {
rmap_t rmap_flags = RMAP_NONE;
folio_ref_add(folio, HPAGE_PMD_NR - 1);
if (anon_exclusive)
rmap_flags |= RMAP_EXCLUSIVE;
folio_add_anon_rmap_ptes(folio, page, HPAGE_PMD_NR,
vma, haddr, rmap_flags);
}are the same for both device private and present. Can it be deduplicated by doing below?
if (is_pmd_migration_entry(*pmd)) {
...
} else {
if (is_pmd_device_private_entry(*pmd)) {
...
} else if (pmd_present()) {
...
}
/* the above code */
}If not, at least adding a comment in the device private copy of the code pointing to the present copy's comment.
Quoted 56 lines
Quoted 56 lines
Quoted 49 lines
mm/huge_memory.c +13
-25+if (!freeze) { +rmap_t rmap_flags =RMAP_NONE ; + +folio_ref_add (folio,HPAGE_PMD_NR -1 ); +if (anon_exclusive) + rmap_flags |=RMAP_EXCLUSIVE ; + +folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , + vma, haddr, rmap_flags); + } }else {/* * Up to this point the pmd is present and huge and userland has @@ -3026,32 +3055,57 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,* Note that NUMA hinting access restrictions are not transferred to * avoid any possibility of altering permissions across VMAs. */ -if (freeze || pmd_migration) { -for (i =0 , addr = haddr; i <HPAGE_PMD_NR ; i++, addr +=PAGE_SIZE ) { -pte_t entry; -swp_entry_t swp_entry; - -if (write) - swp_entry =make_writable_migration_entry ( -page_to_pfn (page + i)); -else if (anon_exclusive) - swp_entry =make_readable_exclusive_migration_entry ( -page_to_pfn (page + i)); -else - swp_entry =make_readable_migration_entry ( -page_to_pfn (page + i)); -if (young) - swp_entry =make_migration_entry_young (swp_entry); -if (dirty) - swp_entry =make_migration_entry_dirty (swp_entry); - entry =swp_entry_to_pte (swp_entry); -if (soft_dirty) - entry =pte_swp_mksoft_dirty (entry); -if (uffd_wp) - entry =pte_swp_mkuffd_wp (entry); +if (freeze || !present) { +pte_t entry; -VM_WARN_ON (!pte_none (ptep_get (pte + i))); -set_pte_at (mm, addr, pte + i, entry); +if (freeze ||is_migration_entry (swp_entry)) {<snip>
Quoted 1 line
mm/huge_memory.c +1
-0+ }else {<snip>
Quoted 3 lines
mm/huge_memory.c +0
-0} }else {pte_t entry;
Quoted 8 lines
Quoted 6 lines
David already pointed this out in v5. It can be done such as:
if (freeze ||
pmd_migration) {
...
} else if (is_pmd_device_private_entry(old_pmd)) {
...No.. freeze can be true for device private entries as well
When freeze is true, migration entry is installed in place of device private entry, since the "if (freeze || pmd_migration)" branch is taken. This proposal is same as your code. What is the difference?
Quoted 47 lines
Quoted 47 lines
} else {
/* for present, non freeze case */
}Quoted 29 lines
mm/huge_memory.c +5
-2@@ -3076,7 +3130,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, }pte_unmap (pte); -if (!pmd_migration) +if (!is_pmd_migration_entry (*pmd))folio_remove_rmap_pmd (folio, page, vma);if (freeze)put_page (page);@@ -3089,7 +3143,7 @@ void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze) {VM_WARN_ON_ONCE (!IS_ALIGNED (address,HPAGE_PMD_SIZE )); -if (pmd_trans_huge (*pmd) ||is_pmd_migration_entry (*pmd)) +if (pmd_trans_huge (*pmd) ||is_pmd_non_present_folio_entry (*pmd))__split_huge_pmd_locked (vma, pmd, address, freeze); }@@ -3268,6 +3322,9 @@ static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,VM_BUG_ON_FOLIO (folio_test_lru (new_folio), folio);lockdep_assert_held (&lruvec->lru_lock); +if (folio_is_device_private (folio)) +return ; +if (list) {/* page reclaim is reclaiming a huge page */ VM_WARN_ON (folio_test_lru (folio));mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3947
Quoted 7 lines
mm/huge_memory.c +2
-1@@ -3885,8 +3942,9 @@ static int __folio_split(struct folio *folio, unsigned int new_order,if (nr_shmem_dropped)shmem_uncharge (mapping->host, nr_shmem_dropped); -if (!ret && is_anon) +if (!ret && is_anon && !folio_is_device_private (folio)) remap_flags =RMP_USE_SHARED_ZEROPAGE ; +You should remove this and add
if (
folio_is_device_private(folio))return false;
Quoted 7 lines
Quoted 4 lines
in
try_to_map_unused_to_zeropage(). Otherwise, no one would know device private folios need to be excluded from mapping unused to zero page.I had that upto v2 and then David asked me to remove it. FYI, this is the only call site for
RMP_USE_SHARED_ZEROPAGE
Can you provide a link?
Even if this is the only call site, there is no guarantee that there will be none in the future. I am not sure why we want caller to handle this special case. Who is going to tell the next user of RMP_USE_SHARED_ZEROPAGE or caller to try_to_map_unused_to_zeropage() that device private is incompatible with them?
Quoted 11 lines
Quoted 8 lines
Quoted 6 lines
mm/huge_memory.c +0
-0remap_page (folio,1 << order, remap_flags);/* Signature · 1 line
2.50.1Thanks for the review
Balbir
--
Best Regards,
Yan, Zi
Re: [v6 04/15] mm/huge_memory: implement device-private THP splitting
Quoted 218 lines · Zi Yan
Quoted 118 lines · Balbir Singh
Quoted 108 lines · Zi Yan
Quoted 106 lines · Balbir Singh
Add support for splitting device-private THP folios, enabling fallback to smaller page sizes when large page allocation or migration fails.
Key changes:
-split_huge_pmd(): Handle device-private PMD entries during splitting
- PreserveRMAP_EXCLUSIVEsemantics for anonymous exclusive folios
- SkipRMP_USE_SHARED_ZEROPAGEfor device-private entries as they don't support shared zero page semanticsSigned-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/huge_memory.c | 138 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 40 deletions(-)mm/huge_memory.c +29
-12@@ -2872,16 +2872,18 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,struct page *page;pgtable_t pgtable;pmd_t old_pmd, _pmd; -bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false; -bool anon_exclusive = false, dirty = false; +bool soft_dirty, uffd_wp = false, young = false, write = false; +bool anon_exclusive = false, dirty = false, present = false;unsigned long addr;pte_t *pte;int i; +swp_entry_t swp_entry;VM_BUG_ON (haddr & ~HPAGE_PMD_MASK );VM_BUG_ON_VMA (vma->vm_start > haddr, vma);VM_BUG_ON_VMA (vma->vm_end < haddr +HPAGE_PMD_SIZE , vma); -VM_BUG_ON (!is_pmd_migration_entry (*pmd) && !pmd_trans_huge (*pmd)); + +VM_WARN_ON (!is_pmd_non_present_folio_entry (*pmd) && !pmd_trans_huge (*pmd));count_vm_event (THP_SPLIT_PMD );@@ -2929,20 +2931,47 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,return __split_huge_zero_page_pmd (vma, haddr, pmd); } - pmd_migration =is_pmd_migration_entry (*pmd); -if (unlikely (pmd_migration)) { -swp_entry_t entry; + present =pmd_present (*pmd); +if (is_pmd_migration_entry (*pmd)) { old_pmd = *pmd; - entry =pmd_to_swp_entry (old_pmd); - page =pfn_swap_entry_to_page (entry); - write =is_writable_migration_entry (entry); + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_migration_entry (swp_entry);if (PageAnon (page)) - anon_exclusive =is_readable_exclusive_migration_entry (entry); - young =is_migration_entry_young (entry); - dirty =is_migration_entry_dirty (entry); + anon_exclusive =is_readable_exclusive_migration_entry (swp_entry); + young =is_migration_entry_young (swp_entry); + dirty =is_migration_entry_dirty (swp_entry); + }else if (is_pmd_device_private_entry (*pmd)) { + old_pmd = *pmd; + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + soft_dirty =pmd_swp_soft_dirty (old_pmd); uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_device_private_entry (swp_entry); + anon_exclusive =PageAnonExclusive (page); + + if (freeze && anon_exclusive &&Quoted 8 lines
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 2964
Quoted 2 lines
mm/huge_memory.c +2
-0+ folio_try_share_anon_rmap_pmd(folio, page)) + freeze = false;Why is it OK to change the freeze request? OK, it is replicating the code for present PMD folios. Either add a comment to point to the explanation in the comment below, or move
“if (is_pmd_device_private_entry(*pmd))“ branch in the else below to deduplicate this code.Quoted 2 lines
Similar to the code for present pages, ideally
folio_try_share_anon_rmap_pmd()should never fail.
anon_exclusive= PageAnonExclusive(page);
if (freeze &&anon_exclusive&&folio_try_share_anon_rmap_pmd(folio, page)) freeze = false; if (!freeze) { rmap_t rmap_flags = RMAP_NONE; folio_ref_add(folio, HPAGE_PMD_NR - 1); if (anon_exclusive) rmap_flags |= RMAP_EXCLUSIVE; folio_add_anon_rmap_ptes(folio, page, HPAGE_PMD_NR, vma, haddr, rmap_flags); }are the same for both device private and present. Can it be deduplicated by doing below?
if (
is_pmd_migration_entry(*pmd)) {
...} else { if (is_pmd_device_private_entry(*pmd)) { ... } else if (pmd_present()) { ... } /* the above code */ }If not, at least adding a comment in the device private copy of the code pointing to the present copy's comment.
Quoted 63 lines
Quoted 62 lines
Quoted 49 lines
mm/huge_memory.c +13
-25+if (!freeze) { +rmap_t rmap_flags =RMAP_NONE ; + +folio_ref_add (folio,HPAGE_PMD_NR -1 ); +if (anon_exclusive) + rmap_flags |=RMAP_EXCLUSIVE ; + +folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , + vma, haddr, rmap_flags); + } }else {/* * Up to this point the pmd is present and huge and userland has @@ -3026,32 +3055,57 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,* Note that NUMA hinting access restrictions are not transferred to * avoid any possibility of altering permissions across VMAs. */ -if (freeze || pmd_migration) { -for (i =0 , addr = haddr; i <HPAGE_PMD_NR ; i++, addr +=PAGE_SIZE ) { -pte_t entry; -swp_entry_t swp_entry; - -if (write) - swp_entry =make_writable_migration_entry ( -page_to_pfn (page + i)); -else if (anon_exclusive) - swp_entry =make_readable_exclusive_migration_entry ( -page_to_pfn (page + i)); -else - swp_entry =make_readable_migration_entry ( -page_to_pfn (page + i)); -if (young) - swp_entry =make_migration_entry_young (swp_entry); -if (dirty) - swp_entry =make_migration_entry_dirty (swp_entry); - entry =swp_entry_to_pte (swp_entry); -if (soft_dirty) - entry =pte_swp_mksoft_dirty (entry); -if (uffd_wp) - entry =pte_swp_mkuffd_wp (entry); +if (freeze || !present) { +pte_t entry; -VM_WARN_ON (!pte_none (ptep_get (pte + i))); -set_pte_at (mm, addr, pte + i, entry); +if (freeze ||is_migration_entry (swp_entry)) {<snip>
Quoted 1 line
mm/huge_memory.c +1
-0+ }else {<snip>
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3111
Quoted 3 lines
mm/huge_memory.c +0
-0} }else {pte_t entry;David already pointed this out in v5. It can be done such as:
if (freeze ||
pmd_migration) {
...
} else if (is_pmd_device_private_entry(old_pmd)) {
...
Quoted 7 lines
Quoted 1 line
No.. freeze can be true for device private entries as well
When freeze is true, migration entry is installed in place of device private entry, since the "if (freeze ||
pmd_migration)" branch is taken. This proposal is same as your code. What is the difference?
I read the else if incorrectly, I'll simplify
Quoted 52 lines
Quoted 52 lines
Quoted 51 lines
} else {
/* for present, non freeze case */
}Quoted 29 lines
mm/huge_memory.c +5
-2@@ -3076,7 +3130,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, }pte_unmap (pte); -if (!pmd_migration) +if (!is_pmd_migration_entry (*pmd))folio_remove_rmap_pmd (folio, page, vma);if (freeze)put_page (page);@@ -3089,7 +3143,7 @@ void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze) {VM_WARN_ON_ONCE (!IS_ALIGNED (address,HPAGE_PMD_SIZE )); -if (pmd_trans_huge (*pmd) ||is_pmd_migration_entry (*pmd)) +if (pmd_trans_huge (*pmd) ||is_pmd_non_present_folio_entry (*pmd))__split_huge_pmd_locked (vma, pmd, address, freeze); }@@ -3268,6 +3322,9 @@ static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,VM_BUG_ON_FOLIO (folio_test_lru (new_folio), folio);lockdep_assert_held (&lruvec->lru_lock); +if (folio_is_device_private (folio)) +return ; +if (list) {/* page reclaim is reclaiming a huge page */ VM_WARN_ON (folio_test_lru (folio));mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3947
Quoted 7 lines
mm/huge_memory.c +2
-1@@ -3885,8 +3942,9 @@ static int __folio_split(struct folio *folio, unsigned int new_order,if (nr_shmem_dropped)shmem_uncharge (mapping->host, nr_shmem_dropped); -if (!ret && is_anon) +if (!ret && is_anon && !folio_is_device_private (folio)) remap_flags =RMP_USE_SHARED_ZEROPAGE ; +You should remove this and add
if (
folio_is_device_private(folio))return false;in
try_to_map_unused_to_zeropage(). Otherwise, no one would know device private folios need to be excluded from mapping unused to zero page.
Quoted 5 lines
Quoted 2 lines
I had that upto v2 and then David asked me to remove it. FYI, this is the only call site for
RMP_USE_SHARED_ZEROPAGECan you provide a link?
Please see https://lore.kernel.org/linux-mm/[email protected]/T/
Quoted 6 lines
Even if this is the only call site, there is no guarantee that there will be none in the future. I am not sure why we want caller to handle this special case. Who is going to tell the next user of
RMP_USE_SHARED_ZEROPAGEor caller totry_to_map_unused_to_zeropage()that device private is incompatible with them?
I don't disagree, but the question was why are device private pages even making it to try_to_map_unused_to_zeropage()>>
Quoted 10 lines
Quoted 10 lines
Quoted 7 lines
Quoted 5 lines
mm/huge_memory.c +0
-0remap_page (folio,1 << order, remap_flags);/* Signature · 1 line
2.50.1Thanks for the review
Balbir
Thanks,
Balbir
Zi Yan
reviewerRe: [v6 04/15] mm/huge_memory: implement device-private THP splitting
Quoted 281 lines · Balbir Singh
Quoted 218 lines · Zi Yan
Quoted 118 lines · Balbir Singh
Quoted 108 lines · Zi Yan
Quoted 106 lines · Balbir Singh
Add support for splitting device-private THP folios, enabling fallback to smaller page sizes when large page allocation or migration fails.
Key changes:
-split_huge_pmd(): Handle device-private PMD entries during splitting
- PreserveRMAP_EXCLUSIVEsemantics for anonymous exclusive folios
- SkipRMP_USE_SHARED_ZEROPAGEfor device-private entries as they don't support shared zero page semanticsSigned-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/huge_memory.c | 138 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 40 deletions(-)mm/huge_memory.c +29
-12@@ -2872,16 +2872,18 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,struct page *page;pgtable_t pgtable;pmd_t old_pmd, _pmd; -bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false; -bool anon_exclusive = false, dirty = false; +bool soft_dirty, uffd_wp = false, young = false, write = false; +bool anon_exclusive = false, dirty = false, present = false;unsigned long addr;pte_t *pte;int i; +swp_entry_t swp_entry;VM_BUG_ON (haddr & ~HPAGE_PMD_MASK );VM_BUG_ON_VMA (vma->vm_start > haddr, vma);VM_BUG_ON_VMA (vma->vm_end < haddr +HPAGE_PMD_SIZE , vma); -VM_BUG_ON (!is_pmd_migration_entry (*pmd) && !pmd_trans_huge (*pmd)); + +VM_WARN_ON (!is_pmd_non_present_folio_entry (*pmd) && !pmd_trans_huge (*pmd));count_vm_event (THP_SPLIT_PMD );@@ -2929,20 +2931,47 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,return __split_huge_zero_page_pmd (vma, haddr, pmd); } - pmd_migration =is_pmd_migration_entry (*pmd); -if (unlikely (pmd_migration)) { -swp_entry_t entry; + present =pmd_present (*pmd); +if (is_pmd_migration_entry (*pmd)) { old_pmd = *pmd; - entry =pmd_to_swp_entry (old_pmd); - page =pfn_swap_entry_to_page (entry); - write =is_writable_migration_entry (entry); + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + + soft_dirty =pmd_swp_soft_dirty (old_pmd); + uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_migration_entry (swp_entry);if (PageAnon (page)) - anon_exclusive =is_readable_exclusive_migration_entry (entry); - young =is_migration_entry_young (entry); - dirty =is_migration_entry_dirty (entry); + anon_exclusive =is_readable_exclusive_migration_entry (swp_entry); + young =is_migration_entry_young (swp_entry); + dirty =is_migration_entry_dirty (swp_entry); + }else if (is_pmd_device_private_entry (*pmd)) { + old_pmd = *pmd; + swp_entry =pmd_to_swp_entry (old_pmd); + page =pfn_swap_entry_to_page (swp_entry); + folio =page_folio (page); + soft_dirty =pmd_swp_soft_dirty (old_pmd); uffd_wp =pmd_swp_uffd_wp (old_pmd); + + write =is_writable_device_private_entry (swp_entry); + anon_exclusive =PageAnonExclusive (page); + + if (freeze && anon_exclusive &&Quoted 8 lines
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 2964
Quoted 2 lines
mm/huge_memory.c +2
-0+ folio_try_share_anon_rmap_pmd(folio, page)) + freeze = false;Why is it OK to change the freeze request? OK, it is replicating the code for present PMD folios. Either add a comment to point to the explanation in the comment below, or move
“if (is_pmd_device_private_entry(*pmd))“ branch in the else below to deduplicate this code.Quoted 2 lines
Similar to the code for present pages, ideally
folio_try_share_anon_rmap_pmd()should never fail.
anon_exclusive= PageAnonExclusive(page);
if (freeze &&anon_exclusive&&folio_try_share_anon_rmap_pmd(folio, page)) freeze = false; if (!freeze) { rmap_t rmap_flags = RMAP_NONE; folio_ref_add(folio, HPAGE_PMD_NR - 1); if (anon_exclusive) rmap_flags |= RMAP_EXCLUSIVE; folio_add_anon_rmap_ptes(folio, page, HPAGE_PMD_NR, vma, haddr, rmap_flags); }are the same for both device private and present. Can it be deduplicated by doing below?
if (
is_pmd_migration_entry(*pmd)) {
...} else { if (is_pmd_device_private_entry(*pmd)) { ... } else if (pmd_present()) { ... } /* the above code */ }If not, at least adding a comment in the device private copy of the code pointing to the present copy's comment.
Quoted 63 lines
Quoted 62 lines
Quoted 49 lines
mm/huge_memory.c +13
-25+if (!freeze) { +rmap_t rmap_flags =RMAP_NONE ; + +folio_ref_add (folio,HPAGE_PMD_NR -1 ); +if (anon_exclusive) + rmap_flags |=RMAP_EXCLUSIVE ; + +folio_add_anon_rmap_ptes (folio, page,HPAGE_PMD_NR , + vma, haddr, rmap_flags); + } }else {/* * Up to this point the pmd is present and huge and userland has @@ -3026,32 +3055,57 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,* Note that NUMA hinting access restrictions are not transferred to * avoid any possibility of altering permissions across VMAs. */ -if (freeze || pmd_migration) { -for (i =0 , addr = haddr; i <HPAGE_PMD_NR ; i++, addr +=PAGE_SIZE ) { -pte_t entry; -swp_entry_t swp_entry; - -if (write) - swp_entry =make_writable_migration_entry ( -page_to_pfn (page + i)); -else if (anon_exclusive) - swp_entry =make_readable_exclusive_migration_entry ( -page_to_pfn (page + i)); -else - swp_entry =make_readable_migration_entry ( -page_to_pfn (page + i)); -if (young) - swp_entry =make_migration_entry_young (swp_entry); -if (dirty) - swp_entry =make_migration_entry_dirty (swp_entry); - entry =swp_entry_to_pte (swp_entry); -if (soft_dirty) - entry =pte_swp_mksoft_dirty (entry); -if (uffd_wp) - entry =pte_swp_mkuffd_wp (entry); +if (freeze || !present) { +pte_t entry; -VM_WARN_ON (!pte_none (ptep_get (pte + i))); -set_pte_at (mm, addr, pte + i, entry); +if (freeze ||is_migration_entry (swp_entry)) {<snip>
Quoted 1 line
mm/huge_memory.c +1
-0+ }else {<snip>
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 3111
Quoted 3 lines
mm/huge_memory.c +0
-0} }else {pte_t entry;David already pointed this out in v5. It can be done such as:
if (freeze ||
pmd_migration) {
...
} else if (is_pmd_device_private_entry(old_pmd)) {
...Quoted 7 lines
Quoted 1 line
No.. freeze can be true for device private entries as well
When freeze is true, migration entry is installed in place of device private entry, since the "if (freeze ||
pmd_migration)" branch is taken. This proposal is same as your code. What is the difference?I read the else if incorrectly, I'll simplify
Quoted 52 lines
Quoted 52 lines
Quoted 51 lines
} else {
/* for present, non freeze case */
}Quoted 29 lines
mm/huge_memory.c +5
-2@@ -3076,7 +3130,7 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, }pte_unmap (pte); -if (!pmd_migration) +if (!is_pmd_migration_entry (*pmd))folio_remove_rmap_pmd (folio, page, vma);if (freeze)put_page (page);@@ -3089,7 +3143,7 @@ void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address, pmd_t *pmd, bool freeze) {VM_WARN_ON_ONCE (!IS_ALIGNED (address,HPAGE_PMD_SIZE )); -if (pmd_trans_huge (*pmd) ||is_pmd_migration_entry (*pmd)) +if (pmd_trans_huge (*pmd) ||is_pmd_non_present_folio_entry (*pmd))__split_huge_pmd_locked (vma, pmd, address, freeze); }@@ -3268,6 +3322,9 @@ static void lru_add_split_folio(struct folio *folio, struct folio *new_folio,VM_BUG_ON_FOLIO (folio_test_lru (new_folio), folio);lockdep_assert_held (&lruvec->lru_lock); +if (folio_is_device_private (folio)) +return ; +if (list) {/* page reclaim is reclaiming a huge page */ VM_WARN_ON (folio_test_lru (folio));mm/huge_memory.c static int __folio_split(struct folio *folio, unsigned int new_order, · line 3947
Quoted 7 lines
mm/huge_memory.c +2
-1@@ -3885,8 +3942,9 @@ static int __folio_split(struct folio *folio, unsigned int new_order,if (nr_shmem_dropped)shmem_uncharge (mapping->host, nr_shmem_dropped); -if (!ret && is_anon) +if (!ret && is_anon && !folio_is_device_private (folio)) remap_flags =RMP_USE_SHARED_ZEROPAGE ; +You should remove this and add
if (
folio_is_device_private(folio))return false;in
try_to_map_unused_to_zeropage(). Otherwise, no one would know device private folios need to be excluded from mapping unused to zero page.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
I had that upto v2 and then David asked me to remove it. FYI, this is the only call site for
RMP_USE_SHARED_ZEROPAGECan you provide a link?
Please see https://lore.kernel.org/linux-mm/[email protected]/T/
I do not see any comment on removing device private folio check in try_to_map_unused_to_zeropage(). Can you try again?
Quoted 10 lines
Quoted 7 lines
Even if this is the only call site, there is no guarantee that there will be none in the future. I am not sure why we want caller to handle this special case. Who is going to tell the next user of
RMP_USE_SHARED_ZEROPAGEor caller totry_to_map_unused_to_zeropage()that device private is incompatible with them?I don't disagree, but the question was why are device private pages even making it to
try_to_map_unused_to_zeropage()>>
Then, it could be done in remove_migration_pte():
if (rmap_walk_arg->map_unused_to_zeropage &&
!folio_is_device_private(folio) && try_to_map_unused_to_zeropage(&pvmw, folio, idx)) continue;
Maybe I am too hung up on this and someone else could pat on my back and tell me it is OK to just do this at the only caller instead. :)
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
mm/huge_memory.c +0
-0remap_page (folio,1 << order, remap_flags);/* Signature · 1 line
2.50.1
Quoted 5 lines
Quoted 2 lines
Quoted 2 lines
Thanks for the review
BalbirThanks,
Balbir
Best Regards,
Yan, Zi
Re: [v6 04/15] mm/huge_memory: implement device-private THP splitting
Quoted 12 lines
Quoted 9 lines
Quoted 6 lines
Even if this is the only call site, there is no guarantee that there will be none in the future. I am not sure why we want caller to handle this special case. Who is going to tell the next user of
RMP_USE_SHARED_ZEROPAGEor caller totry_to_map_unused_to_zeropage()that device private is incompatible with them?I don't disagree, but the question was why are device private pages even making it to
try_to_map_unused_to_zeropage()>>Then, it could be done in
remove_migration_pte():
Quoted 7 lines
if (
rmap_walk_arg->map_unused_to_zeropage&&!folio_is_device_private(folio) && try_to_map_unused_to_zeropage(&pvmw, folio, idx)) continue;Maybe I am too hung up on this and someone else could pat on my back and tell me it is OK to just do this at the only caller instead. :)
I think we shouldn't set a flag for a folio that does not make any sense. Just like we don't set the flag for non-anon folios?
In addition, we could add a VM_WARN_ON_ONCE(folio_is_device_private(folio)) in try_to_map_unused_to_zeropage(), to catch any future abuse.
Signature · 3 lines
Cheers David / dhildenb
Re: [v6 04/15] mm/huge_memory: implement device-private THP splitting
In reply to Balbir SinghQuoted 44 lines · Balbir Singh
Add support for splitting device-private THP folios, enabling fallback to smaller page sizes when large page allocation or migration fails.
Key changes:
-split_huge_pmd(): Handle device-private PMD entries during splitting
- PreserveRMAP_EXCLUSIVEsemantics for anonymous exclusive folios
- SkipRMP_USE_SHARED_ZEROPAGEfor device-private entries as they don't support shared zero page semanticsSigned-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/huge_memory.c | 138 +++++++++++++++++++++++++++++++++-------------- 1 file changed, 98 insertions(+), 40 deletions(-)mm/huge_memory.c +0
-0@@ -2872,16 +2872,18 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd,struct page *page;
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 2880
Quoted 10 lines
mm/huge_memory.c +3
-2pgtable_t pgtable;pmd_t old_pmd, _pmd; -bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false; -bool anon_exclusive = false, dirty = false; +bool soft_dirty, uffd_wp = false, young = false, write = false; +bool anon_exclusive = false, dirty = false, present = false;unsigned long addr;pte_t *pte;int i; +swp_entry_t swp_entry;
Not renaming this variable avoids a lot of churn below. So please keep it called "entry" in this patch.
Signature · 3 lines
Cheers David / dhildenb
Re: [v6 04/15] mm/huge_memory: implement device-private THP splitting
Quoted 49 lines · David Hildenbrand
Quoted 48 lines · Balbir Singh
Add support for splitting device-private THP folios, enabling fallback to smaller page sizes when large page allocation or migration fails.
Key changes:
-split_huge_pmd(): Handle device-private PMD entries during splitting
- PreserveRMAP_EXCLUSIVEsemantics for anonymous exclusive folios
- SkipRMP_USE_SHARED_ZEROPAGEfor device-private entries as they don't support shared zero page semanticsSigned-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>---
mm/huge_memory.c| 138 +++++++++++++++++++++++++++++++++--------------1 file changed, 98 insertions(+), 40 deletions(-)mm/huge_memory.c +0
-2@@ -2872,16 +2872,18 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, struct page *page; pgtable_t pgtable; pmd_t old_pmd, _pmd; - bool young, write, soft_dirty, pmd_migration = false, uffd_wp = false; - bool anon_exclusive = false, dirty = false;
Quoted 9 lines
mm/huge_memory.c static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, · line 2880
Quoted 6 lines
mm/huge_memory.c +3
-0+ bool soft_dirty, uffd_wp = false, young = false, write = false; + bool anon_exclusive = false, dirty = false, present = false; unsigned long addr; pte_t *pte; int i; + swp_entry_t swp_entry;Not renaming this variable avoids a lot of churn below. So please keep it called "entry" in this patch.
Ack, will fix
Thanks,
Balbir
PATCH v6 5/15 mm/migrate_device: handle partially mapped folios during collection
In reply to Balbir SinghExtend migrate_vma_collect_pmd() to handle partially mapped large folios that require splitting before migration can proceed.
During PTE walk in the collection phase, if a large folio is only partially mapped in the migration range, it must be split to ensure the folio is correctly migrated.
--- mm/migrate_device.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)
mm/migrate_device.c +82 -0
@@ -54,6 +54,53 @@ static int migrate_vma_collect_hole(unsigned long start,return 0 ; } +/** +* migrate_vma_split_folio() - Helper function to split a THP folio +* @folio: the folio to split +* @fault_page: struct page associated with the fault if any +* +* Returns 0 on success +*/ +static int migrate_vma_split_folio (struct folio *folio, +struct page *fault_page) +{ +int ret; +struct folio *fault_folio = fault_page ?page_folio (fault_page) :NULL ; +struct folio *new_fault_folio =NULL ; + +if (folio != fault_folio) { +folio_get (folio); +folio_lock (folio); + } + + ret =split_folio (folio); +if (ret) { +if (folio != fault_folio) { +folio_unlock (folio); +folio_put (folio); + } +return ret; + } + + new_fault_folio = fault_page ?page_folio (fault_page) :NULL ; + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (!new_fault_folio) { +folio_unlock (folio); +folio_put (folio); + }else if (folio != new_fault_folio) { +folio_get (new_fault_folio); +folio_lock (new_fault_folio); +folio_unlock (folio); +folio_put (folio); + } + +return 0 ; +} + static int migrate_vma_collect_pmd(pmd_t *pmdp, unsigned long start, unsigned long end,
@@ -136,6 +183,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,* page table entry. Other special swap entries are not * migratable, and we ignore regular swapped page. */ +struct folio *folio; + entry =pte_to_swp_entry (pte);if (!is_device_private_entry (entry)) goto next;
@@ -147,6 +196,23 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, pgmap->owner != migrate->pgmap_owner) goto next; + folio =page_folio (page); +if (folio_test_large (folio)) { +int ret; + +pte_unmap_unlock (ptep, ptl); + ret =migrate_vma_split_folio (folio, + migrate->fault_page); + +if (ret) { + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } + + addr = start; + goto again; + } + mpfn =migrate_pfn (page_to_pfn (page)) |MIGRATE_PFN_MIGRATE ;if (is_writable_device_private_entry (entry))
@@ -171,6 +237,22 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, pgmap->owner != migrate->pgmap_owner) goto next; } + folio = page ?page_folio (page) :NULL ; +if (folio &&folio_test_large (folio)) { +int ret; + +pte_unmap_unlock (ptep, ptl); + ret =migrate_vma_split_folio (folio, + migrate->fault_page); + +if (ret) { + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } + + addr = start; + goto again; + } mpfn =migrate_pfn (pfn) |MIGRATE_PFN_MIGRATE ; mpfn |=pte_write (pte) ?MIGRATE_PFN_WRITE :0 ; }
Signature · 1 line
2.50.1
Zi Yan
reviewerRe: [v6 05/15] mm/migrate_device: handle partially mapped folios during collection
Quoted 113 lines · Balbir Singh
Extend
migrate_vma_collect_pmd()to handle partially mapped large folios that require splitting before migration can proceed.During PTE walk in the collection phase, if a large folio is only partially mapped in the migration range, it must be split to ensure the folio is correctly migrated.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/migrate_device.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)mm/migrate_device.c +55
-0@@ -54,6 +54,53 @@ static int migrate_vma_collect_hole(unsigned long start,return 0 ; } +/** +* migrate_vma_split_folio() - Helper function to split a THP folio +* @folio: the folio to split +* @fault_page: struct page associated with the fault if any +* +* Returns 0 on success +*/ +static int migrate_vma_split_folio (struct folio *folio, +struct page *fault_page) +{ +int ret; +struct folio *fault_folio = fault_page ?page_folio (fault_page) :NULL ; +struct folio *new_fault_folio =NULL ; + +if (folio != fault_folio) { +folio_get (folio); +folio_lock (folio); + } + + ret =split_folio (folio); +if (ret) { +if (folio != fault_folio) { +folio_unlock (folio); +folio_put (folio); + } +return ret; + } + + new_fault_folio = fault_page ?page_folio (fault_page) :NULL ; + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (!new_fault_folio) { +folio_unlock (folio); +folio_put (folio); + }else if (folio != new_fault_folio) { +folio_get (new_fault_folio); +folio_lock (new_fault_folio); +folio_unlock (folio); +folio_put (folio); + } + +return 0 ; +} + static int migrate_vma_collect_pmd(pmd_t *pmdp, unsigned long start, unsigned long end,@@ -136,6 +183,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,* page table entry. Other special swap entries are not * migratable, and we ignore regular swapped page. */ +struct folio *folio; + entry =pte_to_swp_entry (pte);if (!is_device_private_entry (entry)) goto next;@@ -147,6 +196,23 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, pgmap->owner != migrate->pgmap_owner) goto next; + folio =page_folio (page); +if (folio_test_large (folio)) { +int ret; + +pte_unmap_unlock (ptep, ptl); + ret = migrate_vma_split_folio(folio,
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 214
Quoted 10 lines
mm/migrate_device.c +10
-0+ migrate->fault_page); + +if (ret) { + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } + + addr = start; + goto again; + }
This does not look right to me.
The folio here is device private, but migrate_vma_split_folio() calls split_folio(), which cannot handle device private folios yet. Your change to split_folio() is in Patch 10 and should be moved before this patch.
Quoted 29 lines
mm/migrate_device.c +17
-0+ mpfn =migrate_pfn (page_to_pfn (page)) |MIGRATE_PFN_MIGRATE ;if (is_writable_device_private_entry (entry))@@ -171,6 +237,22 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, pgmap->owner != migrate->pgmap_owner) goto next; } + folio = page ?page_folio (page) :NULL ; +if (folio &&folio_test_large (folio)) { +int ret; + +pte_unmap_unlock (ptep, ptl); + ret =migrate_vma_split_folio (folio, + migrate->fault_page); + +if (ret) { + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } + + addr = start; + goto again; + } mpfn =migrate_pfn (pfn) |MIGRATE_PFN_MIGRATE ; mpfn |=pte_write (pte) ?MIGRATE_PFN_WRITE :0 ; }Signature · 1 line
2.50.1
--
Best Regards,
Yan, Zi
Re: [v6 05/15] mm/migrate_device: handle partially mapped folios during collection
Quoted 128 lines · Zi Yan
Quoted 113 lines · Balbir Singh
Extend
migrate_vma_collect_pmd()to handle partially mapped large folios that require splitting before migration can proceed.During PTE walk in the collection phase, if a large folio is only partially mapped in the migration range, it must be split to ensure the folio is correctly migrated.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/migrate_device.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)mm/migrate_device.c +55
-0@@ -54,6 +54,53 @@ static int migrate_vma_collect_hole(unsigned long start,return 0 ; } +/** +* migrate_vma_split_folio() - Helper function to split a THP folio +* @folio: the folio to split +* @fault_page: struct page associated with the fault if any +* +* Returns 0 on success +*/ +static int migrate_vma_split_folio (struct folio *folio, +struct page *fault_page) +{ +int ret; +struct folio *fault_folio = fault_page ?page_folio (fault_page) :NULL ; +struct folio *new_fault_folio =NULL ; + +if (folio != fault_folio) { +folio_get (folio); +folio_lock (folio); + } + + ret =split_folio (folio); +if (ret) { +if (folio != fault_folio) { +folio_unlock (folio); +folio_put (folio); + } +return ret; + } + + new_fault_folio = fault_page ?page_folio (fault_page) :NULL ; + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (!new_fault_folio) { +folio_unlock (folio); +folio_put (folio); + }else if (folio != new_fault_folio) { +folio_get (new_fault_folio); +folio_lock (new_fault_folio); +folio_unlock (folio); +folio_put (folio); + } + +return 0 ; +} + static int migrate_vma_collect_pmd(pmd_t *pmdp, unsigned long start, unsigned long end,@@ -136,6 +183,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,* page table entry. Other special swap entries are not * migratable, and we ignore regular swapped page. */ +struct folio *folio; + entry =pte_to_swp_entry (pte);if (!is_device_private_entry (entry)) goto next;@@ -147,6 +196,23 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, pgmap->owner != migrate->pgmap_owner) goto next; + folio =page_folio (page); +if (folio_test_large (folio)) { +int ret; + +pte_unmap_unlock (ptep, ptl); + ret = migrate_vma_split_folio(folio,mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 214
Quoted 10 lines
mm/migrate_device.c +10
-0+ migrate->fault_page); + +if (ret) { + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } + + addr = start; + goto again; + }This does not look right to me.
Quoted 5 lines
The folio here is device private, but
migrate_vma_split_folio()callssplit_folio(), which cannot handle device private folios yet. Your change tosplit_folio()is in Patch 10 and should be moved before this patch.
Patch 10 is to split the folio in the middle of migration (when we have converted the entries to migration entries). This patch relies on the changes in patch 4. I agree the names are confusing, I'll reword the functions
Thanks for the review
Balbir
Re: [v6 05/15] mm/migrate_device: handle partially mapped folios during collection
Quoted 135 lines · Balbir Singh
Quoted 128 lines · Zi Yan
Quoted 113 lines · Balbir Singh
Extend
migrate_vma_collect_pmd()to handle partially mapped large folios that require splitting before migration can proceed.During PTE walk in the collection phase, if a large folio is only partially mapped in the migration range, it must be split to ensure the folio is correctly migrated.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/migrate_device.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)mm/migrate_device.c +55
-0@@ -54,6 +54,53 @@ static int migrate_vma_collect_hole(unsigned long start,return 0 ; } +/** +* migrate_vma_split_folio() - Helper function to split a THP folio +* @folio: the folio to split +* @fault_page: struct page associated with the fault if any +* +* Returns 0 on success +*/ +static int migrate_vma_split_folio (struct folio *folio, +struct page *fault_page) +{ +int ret; +struct folio *fault_folio = fault_page ?page_folio (fault_page) :NULL ; +struct folio *new_fault_folio =NULL ; + +if (folio != fault_folio) { +folio_get (folio); +folio_lock (folio); + } + + ret =split_folio (folio); +if (ret) { +if (folio != fault_folio) { +folio_unlock (folio); +folio_put (folio); + } +return ret; + } + + new_fault_folio = fault_page ?page_folio (fault_page) :NULL ; + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (!new_fault_folio) { +folio_unlock (folio); +folio_put (folio); + }else if (folio != new_fault_folio) { +folio_get (new_fault_folio); +folio_lock (new_fault_folio); +folio_unlock (folio); +folio_put (folio); + } + +return 0 ; +} + static int migrate_vma_collect_pmd(pmd_t *pmdp, unsigned long start, unsigned long end,@@ -136,6 +183,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,* page table entry. Other special swap entries are not * migratable, and we ignore regular swapped page. */ +struct folio *folio; + entry =pte_to_swp_entry (pte);if (!is_device_private_entry (entry)) goto next;@@ -147,6 +196,23 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, pgmap->owner != migrate->pgmap_owner) goto next; + folio =page_folio (page); +if (folio_test_large (folio)) { +int ret; + +pte_unmap_unlock (ptep, ptl); + ret = migrate_vma_split_folio(folio,mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 214
Quoted 10 lines
mm/migrate_device.c +10
-0+ migrate->fault_page); + +if (ret) { + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } + + addr = start; + goto again; + }This does not look right to me.
Quoted 5 lines
The folio here is device private, but
migrate_vma_split_folio()callssplit_folio(), which cannot handle device private folios yet. Your change tosplit_folio()is in Patch 10 and should be moved before this patch.
Quoted 4 lines
Patch 10 is to split the folio in the middle of migration (when we have converted the entries to migration entries). This patch relies on the changes in patch 4. I agree the names are confusing, I'll reword the functions
Hi Balbir,
I am still reviewing the patches, but I think I agree with Zi here.
split_folio() will replace the PMD mappings of the huge folio with PTE mappings, but will also split the folio into smaller folios. The former is ok with this patch, but the latter is probably not correct if the folio is a zone device folio. The driver needs to know about the change, as usually the driver will have some sort of mapping between GPU physical memory chunks and their corresponding zone device pages.
Re: [v6 05/15] mm/migrate_device: handle partially mapped folios during collection
Quoted 143 lines · Karim Manaouil
Quoted 135 lines · Balbir Singh
Quoted 128 lines · Zi Yan
Quoted 113 lines · Balbir Singh
Extend
migrate_vma_collect_pmd()to handle partially mapped large folios that require splitting before migration can proceed.During PTE walk in the collection phase, if a large folio is only partially mapped in the migration range, it must be split to ensure the folio is correctly migrated.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/migrate_device.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)mm/migrate_device.c +55
-0@@ -54,6 +54,53 @@ static int migrate_vma_collect_hole(unsigned long start,return 0 ; } +/** +* migrate_vma_split_folio() - Helper function to split a THP folio +* @folio: the folio to split +* @fault_page: struct page associated with the fault if any +* +* Returns 0 on success +*/ +static int migrate_vma_split_folio (struct folio *folio, +struct page *fault_page) +{ +int ret; +struct folio *fault_folio = fault_page ?page_folio (fault_page) :NULL ; +struct folio *new_fault_folio =NULL ; + +if (folio != fault_folio) { +folio_get (folio); +folio_lock (folio); + } + + ret =split_folio (folio); +if (ret) { +if (folio != fault_folio) { +folio_unlock (folio); +folio_put (folio); + } +return ret; + } + + new_fault_folio = fault_page ?page_folio (fault_page) :NULL ; + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (!new_fault_folio) { +folio_unlock (folio); +folio_put (folio); + }else if (folio != new_fault_folio) { +folio_get (new_fault_folio); +folio_lock (new_fault_folio); +folio_unlock (folio); +folio_put (folio); + } + +return 0 ; +} + static int migrate_vma_collect_pmd(pmd_t *pmdp, unsigned long start, unsigned long end,@@ -136,6 +183,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,* page table entry. Other special swap entries are not * migratable, and we ignore regular swapped page. */ +struct folio *folio; + entry =pte_to_swp_entry (pte);if (!is_device_private_entry (entry)) goto next;@@ -147,6 +196,23 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, pgmap->owner != migrate->pgmap_owner) goto next; + folio =page_folio (page); +if (folio_test_large (folio)) { +int ret; + +pte_unmap_unlock (ptep, ptl); + ret = migrate_vma_split_folio(folio,mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 214
Quoted 10 lines
mm/migrate_device.c +10
-0+ migrate->fault_page); + +if (ret) { + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } + + addr = start; + goto again; + }This does not look right to me.
Quoted 5 lines
The folio here is device private, but
migrate_vma_split_folio()callssplit_folio(), which cannot handle device private folios yet. Your change tosplit_folio()is in Patch 10 and should be moved before this patch.Quoted 4 lines
Patch 10 is to split the folio in the middle of migration (when we have converted the entries to migration entries). This patch relies on the changes in patch 4. I agree the names are confusing, I'll reword the functions
Hi Balbir,
Quoted 9 lines
I am still reviewing the patches, but I think I agree with Zi here.
split_folio()will replace the PMD mappings of the huge folio with PTE mappings, but will also split the folio into smaller folios. The former is ok with this patch, but the latter is probably not correct if the folio is a zone device folio. The driver needs to know about the change, as usually the driver will have some sort of mapping between GPU physical memory chunks and their corresponding zone device pages.
Yes, at this point there is no support for split folio callback. I can move this bit to a later patch or move the entire patch to after patch 10. I suspect this is a theoretical bisection concern for a future driver using large folios?
Thanks,
Balbir
Re: [v6 05/15] mm/migrate_device: handle partially mapped folios during collection
In reply to Karim ManaouilQuoted 143 lines · Karim Manaouil
Quoted 135 lines · Balbir Singh
Quoted 128 lines · Zi Yan
Quoted 113 lines · Balbir Singh
Extend
migrate_vma_collect_pmd()to handle partially mapped large folios that require splitting before migration can proceed.During PTE walk in the collection phase, if a large folio is only partially mapped in the migration range, it must be split to ensure the folio is correctly migrated.
Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- mm/migrate_device.c | 82 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+)mm/migrate_device.c +55
-0@@ -54,6 +54,53 @@ static int migrate_vma_collect_hole(unsigned long start,return 0 ; } +/** +* migrate_vma_split_folio() - Helper function to split a THP folio +* @folio: the folio to split +* @fault_page: struct page associated with the fault if any +* +* Returns 0 on success +*/ +static int migrate_vma_split_folio (struct folio *folio, +struct page *fault_page) +{ +int ret; +struct folio *fault_folio = fault_page ?page_folio (fault_page) :NULL ; +struct folio *new_fault_folio =NULL ; + +if (folio != fault_folio) { +folio_get (folio); +folio_lock (folio); + } + + ret =split_folio (folio); +if (ret) { +if (folio != fault_folio) { +folio_unlock (folio); +folio_put (folio); + } +return ret; + } + + new_fault_folio = fault_page ?page_folio (fault_page) :NULL ; + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (!new_fault_folio) { +folio_unlock (folio); +folio_put (folio); + }else if (folio != new_fault_folio) { +folio_get (new_fault_folio); +folio_lock (new_fault_folio); +folio_unlock (folio); +folio_put (folio); + } + +return 0 ; +} + static int migrate_vma_collect_pmd(pmd_t *pmdp, unsigned long start, unsigned long end,@@ -136,6 +183,8 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp,* page table entry. Other special swap entries are not * migratable, and we ignore regular swapped page. */ +struct folio *folio; + entry =pte_to_swp_entry (pte);if (!is_device_private_entry (entry)) goto next;@@ -147,6 +196,23 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, pgmap->owner != migrate->pgmap_owner) goto next; + folio =page_folio (page); +if (folio_test_large (folio)) { +int ret; + +pte_unmap_unlock (ptep, ptl); + ret = migrate_vma_split_folio(folio,mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 214
Quoted 10 lines
mm/migrate_device.c +10
-0+ migrate->fault_page); + +if (ret) { + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } + + addr = start; + goto again; + }This does not look right to me.
Quoted 5 lines
The folio here is device private, but
migrate_vma_split_folio()callssplit_folio(), which cannot handle device private folios yet. Your change tosplit_folio()is in Patch 10 and should be moved before this patch.Quoted 4 lines
Patch 10 is to split the folio in the middle of migration (when we have converted the entries to migration entries). This patch relies on the changes in patch 4. I agree the names are confusing, I'll reword the functions
Hi Balbir,
Quoted 9 lines
I am still reviewing the patches, but I think I agree with Zi here.
split_folio()will replace the PMD mappings of the huge folio with PTE mappings, but will also split the folio into smaller folios. The former is ok with this patch, but the latter is probably not correct if the folio is a zone device folio. The driver needs to know about the change, as usually the driver will have some sort of mapping between GPU physical memory chunks and their corresponding zone device pages.
On further thought, there should be no driver in the tree affected by this, but I'll definitely give it a further look
Thanks,
Balbir
PATCH v6 6/15 mm/migrate_device: implement THP migration of zone device pages
In reply to Balbir SinghMIGRATE_VMA_SELECT_COMPOUND will be used to select THP pages during migrate_vma_setup() and MIGRATE_PFN_COMPOUND will make migrating device pages as compound pages during device pfn migration.
migrate_device code paths go through the collect, setup and finalize phases of migration.
The entries in src and dst arrays passed to these functions still remain at a PAGE_SIZE granularity. When a compound page is passed, the first entry has the PFN along with MIGRATE_PFN_COMPOUND and other flags set (MIGRATE_PFN_MIGRATE, MIGRATE_PFN_VALID), the remaining entries (HPAGE_PMD_NR - 1) are filled with 0's. This representation allows for the compound page to be split into smaller page sizes.
migrate_vma_collect_hole(), migrate_vma_collect_pmd() are now THP page aware. Two new helper functions migrate_vma_collect_huge_pmd() and migrate_vma_insert_huge_pmd_page() have been added.
migrate_vma_collect_huge_pmd() can collect THP pages, but if for some reason this fails, there is fallback support to split the folio and migrate it.
migrate_vma_insert_huge_pmd_page() closely follows the logic of migrate_vma_insert_page()
Support for splitting pages as needed for migration will follow in later patches in this series.
--- include/linux/migrate.h | 2 + mm/migrate_device.c | 468 ++++++++++++++++++++++++++++++++++------ 2 files changed, 407 insertions(+), 63 deletions(-)
include/linux/migrate.h +2 -0
@@ -125,6 +125,7 @@ static inline int migrate_misplaced_folio(struct folio *folio, int node)#define MIGRATE_PFN_VALID (1UL << 0)#define MIGRATE_PFN_MIGRATE (1UL << 1)#define MIGRATE_PFN_WRITE (1UL << 3) +#define MIGRATE_PFN_COMPOUND (1UL << 4)#define MIGRATE_PFN_SHIFT 6static inline struct page *migrate_pfn_to_page (unsigned long mpfn)
@@ -143,6 +144,7 @@ enum migrate_vma_direction {MIGRATE_VMA_SELECT_SYSTEM =1 <<0 ,MIGRATE_VMA_SELECT_DEVICE_PRIVATE =1 <<1 ,MIGRATE_VMA_SELECT_DEVICE_COHERENT =1 <<2 , + MIGRATE_VMA_SELECT_COMPOUND = 1 << 3, }; struct migrate_vma {
mm/migrate_device.c +405 -63
631 lines not on this page · raw message
Signature · 1 line
2.50.1
PATCH v6 7/15 mm/memory/fault: add THP fault handling for zone device private pages
In reply to Balbir SinghImplement CPU fault handling for zone device THP entries through do_huge_pmd_device_private(), enabling transparent migration of device-private large pages back to system memory on CPU access.
When the CPU accesses a zone device THP entry, the fault handler calls the device driver's migrate_to_ram() callback to migrate the entire large page back to system memory.
--- include/linux/huge_mm.h | 7 +++++++ mm/huge_memory.c | 36 ++++++++++++++++++++++++++++++++++++ mm/memory.c | 5 +++-- 3 files changed, 46 insertions(+), 2 deletions(-)
include/linux/huge_mm.h +7 -0
@@ -496,6 +496,8 @@ static inline bool folio_test_pmd_mappable(struct folio *folio)vm_fault_t do_huge_pmd_numa_page (struct vm_fault *vmf); +vm_fault_t do_huge_pmd_device_private (struct vm_fault *vmf); +extern struct folio *huge_zero_folio;extern unsigned long huge_zero_pfn;
@@ -671,6 +673,11 @@ static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)return 0 ; } +static inline vm_fault_t do_huge_pmd_device_private (struct vm_fault *vmf) +{ +return 0 ; +} +static inline bool is_huge_zero_folio (const struct folio *folio) {return false;
mm/huge_memory.c +36 -0
@@ -1287,6 +1287,42 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf) } +vm_fault_t do_huge_pmd_device_private (struct vm_fault *vmf) +{ +struct vm_area_struct *vma = vmf->vma; +vm_fault_t ret =0 ; +spinlock_t *ptl; +swp_entry_t swp_entry; +struct page *page; + +if (vmf->flags &FAULT_FLAG_VMA_LOCK ) { +vma_end_read (vma); +return VM_FAULT_RETRY ; + } + + ptl =pmd_lock (vma->vm_mm, vmf->pmd); +if (unlikely (!pmd_same (*vmf->pmd, vmf->orig_pmd))) { +spin_unlock (ptl); +return 0 ; + } + + swp_entry =pmd_to_swp_entry (vmf->orig_pmd); + page =pfn_swap_entry_to_page (swp_entry); + vmf->page = page; + vmf->pte =NULL ; +if (trylock_page (vmf->page)) { +get_page (page); +spin_unlock (ptl); + ret =page_pgmap (page)->ops->migrate_to_ram (vmf); +unlock_page (vmf->page); +put_page (page); + }else { +spin_unlock (ptl); + } + +return ret; +} +/* * always: directly stall for all thp allocations * defer: wake kswapd and fail if not immediately available
mm/memory.c +3 -2
@@ -6332,8 +6332,9 @@ static vm_fault_t __handle_mm_fault(struct vm_area_struct *vma, vmf.orig_pmd =pmdp_get_lockless (vmf.pmd);if (unlikely (is_swap_pmd (vmf.orig_pmd))) { -VM_BUG_ON (thp_migration_supported () && - !is_pmd_migration_entry (vmf.orig_pmd)); +if (is_pmd_device_private_entry (vmf.orig_pmd)) +return do_huge_pmd_device_private (&vmf); +if (is_pmd_migration_entry (vmf.orig_pmd))pmd_migration_entry_wait (mm, vmf.pmd);return 0 ;
Signature · 1 line
2.50.1
Re: [v6 07/15] mm/memory/fault: add THP fault handling for zone device private pages
Quoted 86 lines · Balbir Singh
Implement CPU fault handling for zone device THP entries through
do_huge_pmd_device_private(), enabling transparent migration of device-private large pages back to system memory on CPU access.When the CPU accesses a zone device THP entry, the fault handler calls the device driver's
migrate_to_ram()callback to migrate the entire large page back to system memory.Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>--- include/linux/huge_mm.h | 7 +++++++ mm/huge_memory.c | 36 ++++++++++++++++++++++++++++++++++++ mm/memory.c | 5 +++-- 3 files changed, 46 insertions(+), 2 deletions(-)include/linux/huge_mm.h +7
-0@@ -496,6 +496,8 @@ static inline bool folio_test_pmd_mappable(struct folio *folio)vm_fault_t do_huge_pmd_numa_page (struct vm_fault *vmf); +vm_fault_t do_huge_pmd_device_private (struct vm_fault *vmf); +extern struct folio *huge_zero_folio;extern unsigned long huge_zero_pfn;@@ -671,6 +673,11 @@ static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf)return 0 ; } +static inline vm_fault_t do_huge_pmd_device_private (struct vm_fault *vmf) +{ +return 0 ; +} +static inline bool is_huge_zero_folio (const struct folio *folio) {return false;mm/huge_memory.c +14
-0@@ -1287,6 +1287,42 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf) } +vm_fault_t do_huge_pmd_device_private (struct vm_fault *vmf) +{ +struct vm_area_struct *vma = vmf->vma; +vm_fault_t ret =0 ; +spinlock_t *ptl; +swp_entry_t swp_entry; +struct page *page; + +if (vmf->flags &FAULT_FLAG_VMA_LOCK ) { +vma_end_read (vma); +return VM_FAULT_RETRY ; + } + + ptl =pmd_lock (vma->vm_mm, vmf->pmd);
mm/huge_memory.c static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1313
Quoted 10 lines
mm/huge_memory.c +10
-0+if (unlikely (!pmd_same (*vmf->pmd, vmf->orig_pmd))) { +spin_unlock (ptl); +return 0 ; + } + + swp_entry =pmd_to_swp_entry (vmf->orig_pmd); + page =pfn_swap_entry_to_page (swp_entry); + vmf->page = page; + vmf->pte =NULL ; +if (trylock_page (vmf->page)) {
We should be operating on a folio here. folio_trylock() + folio_get() + folio_unlock() + folio_put().
mm/huge_memory.c static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1316
Quoted 3 lines
mm/huge_memory.c +3
-0+get_page (page); +spin_unlock (ptl); + ret =page_pgmap (page)->ops->migrate_to_ram (vmf);
BTW, I was wondering whether it is really the right design to pass the vmf here. Likely the const vma+addr+folio could be sufficient. I did not look into all callbaks, though.
Signature · 3 lines
Cheers David / dhildenb
Re: [v6 07/15] mm/memory/fault: add THP fault handling for zone device private pages
Quoted 94 lines · David Hildenbrand
Quoted 80 lines · Balbir Singh
Implement CPU fault handling for zone device THP entries through
do_huge_pmd_device_private(), enabling transparent migration of device-private large pages back to system memory on CPU access.When the CPU accesses a zone device THP entry, the fault handler calls the device driver's
migrate_to_ram()callback to migrate the entire large page back to system memory.Signed-off-by Balbir Singh <[email protected]>Cc David Hildenbrand <[email protected]>Cc Zi Yan <[email protected]>Cc Joshua Hahn <[email protected]>Cc Rakie Kim <[email protected]>Cc Byungchul Park <[email protected]>Cc Gregory Price <[email protected]>Cc Ying Huang <[email protected]>Cc Alistair Popple <[email protected]>Cc Oscar Salvador <[email protected]>Cc Lorenzo Stoakes <[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 Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>---
include/linux/huge_mm.h| 7 +++++++
mm/huge_memory.c | 36 ++++++++++++++++++++++++++++++++++++ mm/memory.c | 5 +++--3 files changed, 46 insertions(+), 2 deletions(-)include/linux/huge_mm.h +1
-0@@ -496,6 +496,8 @@ static inline bool folio_test_pmd_mappable(struct folio *folio) vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf); +vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf); + extern struct folio *huge_zero_folio; extern unsigned long huge_zero_pfn; @@ -671,6 +673,11 @@ static inline vm_fault_t do_huge_pmd_numa_page(struct vm_fault *vmf) return 0;}
+static inlinevm_fault_tdo_huge_pmd_device_private(struct vm_fault *vmf)include/linux/huge_mm.h +4
-0+{ + return 0; +} +static inline bool
is_huge_zero_folio(const struct folio *folio){
return false;mm/huge_memory.c +13
-0@@ -1287,6 +1287,42 @@ static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf) } +vm_fault_t do_huge_pmd_device_private(struct vm_fault *vmf) +{ + struct vm_area_struct *vma = vmf->vma; + vm_fault_t ret = 0; + spinlock_t *ptl; + swp_entry_t swp_entry; + struct page *page; + + if (vmf->flags & FAULT_FLAG_VMA_LOCK) { + vma_end_read(vma); + return VM_FAULT_RETRY; + } + + ptl = pmd_lock(vma->vm_mm, vmf->pmd);mm/huge_memory.c static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1311
Quoted 10 lines
mm/huge_memory.c +10
-0+ if (unlikely(!pmd_same(*vmf->pmd, vmf->orig_pmd))) { + spin_unlock(ptl); + return 0; + } + + swp_entry = pmd_to_swp_entry(vmf->orig_pmd); + page = pfn_swap_entry_to_page(swp_entry); + vmf->page = page; + vmf->pte = NULL; + if (trylock_page(vmf->page)) {We should be operating on a folio here.
folio_trylock()+folio_get()+folio_unlock()+folio_put().
Quoted 6 lines
mm/huge_memory.c static vm_fault_t __do_huge_pmd_anonymous_page(struct vm_fault *vmf) · line 1316
Quoted 3 lines
mm/huge_memory.c +3
-0+ get_page(page); + spin_unlock(ptl); + ret = page_pgmap(page)->ops->migrate_to_ram(vmf);BTW, I was wondering whether it is really the right design to pass the vmf here. Likely the const vma+addr+folio could be sufficient. I did not look into all callbaks, though.
The vmf is used for address and other bits. FYI, this is no different from pte fault handling and migrate_to_ram(). I can do the folio conversions
Balbir
PATCH v6 8/15 lib/test_hmm: add zone device private THP test infrastructure
In reply to Balbir SinghEnhance the hmm test driver (lib/test_hmm) with support for THP pages.
A new pool of free_folios() has now been added to the dmirror device, which can be allocated when a request for a THP zone device private page is made.
Add compound page awareness to the allocation function during normal migration and fault based migration. These routines also copy folio_nr_pages() when moving data between system memory and device memory.
args.src and args.dst used to hold migration entries are now dynamically allocated (as they need to hold HPAGE_PMD_NR entries or more).
Split and migrate support will be added in future patches in this series.
--- include/linux/memremap.h | 12 ++ lib/test_hmm.c | 368 +++++++++++++++++++++++++++++++-------- 2 files changed, 304 insertions(+), 76 deletions(-)
include/linux/memremap.h +12 -0
@@ -177,6 +177,18 @@ static inline bool folio_is_pci_p2pdma(const struct folio *folio) folio->pgmap->type ==MEMORY_DEVICE_PCI_P2PDMA ; } +static inline void *folio_zone_device_data (const struct folio *folio) +{ +VM_WARN_ON_FOLIO (!folio_is_device_private (folio), folio); +return folio->page.zone_device_data; +} + +static inline void folio_set_zone_device_data (struct folio *folio,void *data) +{ +VM_WARN_ON_FOLIO (!folio_is_device_private (folio), folio); + folio->page.zone_device_data = data; +} +static inline bool is_pci_p2pdma_page (const struct page *page) { return IS_ENABLED(CONFIG_PCI_P2PDMA) &&
lib/test_hmm.c +292 -76
650 lines not on this page · raw message
Signature · 1 line
2.50.1
PATCH v6 9/15 mm/memremap: add driver callback support for folio splitting
In reply to Balbir SinghWhen a zone device page is split (via huge pmd folio split). The driver callback for folio_split is invoked to let the device driver know that the folio size has been split into a smaller order.
Provide a default implementation for drivers that do not provide this callback that copies the pgmap and mapping fields for the split folios.
Update the HMM test driver to handle the split.
--- include/linux/memremap.h | 29 +++++++++++++++++++++++++++++ include/linux/mm.h | 1 + lib/test_hmm.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+)
include/linux/memremap.h +29 -0
@@ -100,6 +100,13 @@ struct dev_pagemap_ops {*/ int (*memory_failure)(struct dev_pagemap *pgmap,unsigned long pfn,unsigned long nr_pages,int mf_flags); + +/* +* Used for private (un-addressable) device memory only. +* This callback is used when a folio is split into +* a smaller folio +*/ +void (*folio_split)(struct folio *head,struct folio *tail); };#define PGMAP_ALTMAP_VALID (1 << 0)
@@ -235,6 +242,23 @@ static inline void zone_device_page_init(struct page *page) zone_device_folio_init(folio, 0); } +static inline void zone_device_private_split_cb (struct folio *original_folio, +struct folio *new_folio) +{ +if (folio_is_device_private (original_folio)) { +if (!original_folio->pgmap->ops->folio_split) { +if (new_folio) { + new_folio->pgmap = original_folio->pgmap; + new_folio->page.mapping = + original_folio->page.mapping; + } + }else { + original_folio->pgmap->ops->folio_split (original_folio, + new_folio); + } + } +} +#else static inline void *devm_memremap_pages (struct device *dev,struct dev_pagemap *pgmap)
@@ -268,6 +292,11 @@ static inline unsigned long memremap_compat_align(void) {return PAGE_SIZE ; } + +static inline void zone_device_private_split_cb (struct folio *original_folio, +struct folio *new_folio) +{ +}#endif /* CONFIG_ZONE_DEVICE */ static inline void put_dev_pagemap (struct dev_pagemap *pgmap)
include/linux/mm.h +1 -0
@@ -1265,6 +1265,7 @@ static inline struct folio *virt_to_folio(const void *x)void __folio_put (struct folio *folio);void split_page (struct page *page,unsigned int order); +void prep_compound_page (struct page *page,unsigned int order);void folio_copy (struct folio *dst,struct folio *src);int folio_mc_copy (struct folio *dst,struct folio *src);
lib/test_hmm.c +35 -0
@@ -1653,9 +1653,44 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)return ret; } +static void dmirror_devmem_folio_split (struct folio *head,struct folio *tail) +{ +struct page *rpage =BACKING_PAGE (folio_page (head,0 )); +struct page *rpage_tail; +struct folio *rfolio; +unsigned long offset =0 ; + +if (!rpage) { + tail->page.zone_device_data =NULL ; +return ; + } + + rfolio =page_folio (rpage); + +if (tail ==NULL ) { +folio_reset_order (rfolio); + rfolio->mapping =NULL ; +folio_set_count (rfolio,1 ); +return ; + } + + offset =folio_pfn (tail) -folio_pfn (head); + + rpage_tail =folio_page (rfolio, offset); + tail->page.zone_device_data = rpage_tail; + rpage_tail->zone_device_data = rpage->zone_device_data; +clear_compound_head (rpage_tail); + rpage_tail->mapping =NULL ; + +folio_page (tail,0 )->mapping =folio_page (head,0 )->mapping; + tail->pgmap = head->pgmap; +folio_set_count (page_folio (rpage_tail),1 ); +} +static const struct dev_pagemap_ops dmirror_devmem_ops = { .page_free = dmirror_devmem_free, .migrate_to_ram = dmirror_devmem_fault, + .folio_split = dmirror_devmem_folio_split, };static int dmirror_device_init (struct dmirror_device *mdevice,int id)
Signature · 1 line
2.50.1
PATCH v6 10/15 mm/migrate_device: add THP splitting during migration
In reply to Balbir SinghImplement migrate_vma_split_pages() to handle THP splitting during the migration process when destination cannot allocate compound pages.
This addresses the common scenario where migrate_vma_setup() succeeds with MIGRATE_PFN_COMPOUND pages, but the destination device cannot allocate large pages during the migration phase.
Key changes:
- migrate_vma_split_pages(): Split already-isolated pages during migration
- Enhanced folio_split() and __split_unmapped_folio() with isolated parameter to avoid redundant unmap/remap operations
This provides a fallback mechansim to ensure migration succeeds even when large page allocation fails at the destination.
--- include/linux/huge_mm.h | 11 +++++-- lib/test_hmm.c | 9 ++++++ mm/huge_memory.c | 46 ++++++++++++++------------- mm/migrate_device.c | 69 ++++++++++++++++++++++++++++++++++------- 4 files changed, 101 insertions(+), 34 deletions(-)
include/linux/huge_mm.h +9 -2
@@ -365,8 +365,8 @@ unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long add vm_flags_t vm_flags);bool can_split_folio (struct folio *folio,int caller_pins,int *pextra_pins); -int split_huge_page_to_list_to_order (struct page *page,struct list_head *list, -unsigned int new_order); +int __split_huge_page_to_list_to_order (struct page *page,struct list_head *list, +unsigned int new_order,bool unmapped);int min_order_for_split (struct folio *folio);int split_folio_to_list (struct folio *folio,struct list_head *list); bool uniform_split_supported(struct folio *folio, unsigned int new_order,
@@ -375,6 +375,13 @@ bool non_uniform_split_supported(struct folio *folio, unsigned int new_order, bool warns);int folio_split (struct folio *folio,unsigned int new_order,struct page *page,struct list_head *list); + +static inline int split_huge_page_to_list_to_order (struct page *page,struct list_head *list, +unsigned int new_order) +{ +return __split_huge_page_to_list_to_order (page, list, new_order, false); +} +/* * try_folio_split - try to split a @folio at @page using non uniform split. * @folio: folio to be split
lib/test_hmm.c +9 -0
@@ -1611,6 +1611,15 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf) order =folio_order (page_folio (vmf->page)); nr =1 << order; +/* +* When folios are partially mapped, we can't rely on the folio +* order of vmf->page as the folio might not be fully split yet +*/ +if (vmf->pte) { + order =0 ; + nr =1 ; + } +/* * Consider a per-cpu cache of src and dst pfns, but with * large number of cpus that might not scale well.
mm/huge_memory.c +25 -21
114 lines not on this page · raw message
mm/migrate_device.c +58 -11
108 lines not on this page · raw message
Signature · 1 line
2.50.1
PATCH v6 11/15 lib/test_hmm: add large page allocation failure testing
In reply to Balbir SinghAdd HMM_DMIRROR_FLAG_FAIL_ALLOC flag to simulate large page allocation failures, enabling testing of split migration code paths.
This test flag allows validation of the fallback behavior when destination device cannot allocate compound pages. This is useful for testing the split migration functionality.
--- lib/test_hmm.c | 61 ++++++++++++++++++++++++++++++--------------- lib/test_hmm_uapi.h | 3 +++ 2 files changed, 44 insertions(+), 20 deletions(-)
lib/test_hmm.c +41 -20
@@ -92,6 +92,7 @@ struct dmirror {struct xarray pt;struct mmu_interval_notifier notifier;struct mutex mutex; +__u64 flags; };/*
@@ -699,7 +700,12 @@ static void dmirror_migrate_alloc_and_copy(struct migrate_vma *args, page_to_pfn(spage))) goto next; - dpage =dmirror_devmem_alloc_page (dmirror, is_large); +if (dmirror->flags &HMM_DMIRROR_FLAG_FAIL_ALLOC ) { + dmirror->flags &= ~HMM_DMIRROR_FLAG_FAIL_ALLOC ; + dpage =NULL ; + }else + dpage =dmirror_devmem_alloc_page (dmirror, is_large); +if (!dpage) {struct folio *folio;unsigned long i;
@@ -959,44 +965,55 @@ static vm_fault_t dmirror_devmem_fault_alloc_and_copy(struct migrate_vma *args, spage =BACKING_PAGE (spage); order =folio_order (page_folio (spage)); -if (order) + *dst =MIGRATE_PFN_COMPOUND ; +if (*src &MIGRATE_PFN_WRITE ) + *dst |=MIGRATE_PFN_WRITE ; + +if (dmirror->flags &HMM_DMIRROR_FLAG_FAIL_ALLOC ) { + dmirror->flags &= ~HMM_DMIRROR_FLAG_FAIL_ALLOC ; + *dst &= ~MIGRATE_PFN_COMPOUND ; + dpage =NULL ; + }else if (order) { dpage =folio_page (vma_alloc_folio (GFP_HIGHUSER_MOVABLE , order, args->vma, addr),0 ); -else - dpage =alloc_page_vma (GFP_HIGHUSER_MOVABLE , args->vma, addr); - -/* Try with smaller pages if large allocation fails */ -if (!dpage && order) { + }else { dpage =alloc_page_vma (GFP_HIGHUSER_MOVABLE , args->vma, addr); -if (!dpage) -return VM_FAULT_OOM ; - order =0 ; } +if (!dpage && !order) +return VM_FAULT_OOM ; +pr_debug ("migrating from sys to dev pfn src: 0x%lx pfn dst: 0x%lx\n" ,page_to_pfn (spage),page_to_pfn (dpage)); -lock_page (dpage); -xa_erase (&dmirror->pt, addr >>PAGE_SHIFT ); -copy_highpage (dpage, spage); - *dst =migrate_pfn (page_to_pfn (dpage)); -if (*src &MIGRATE_PFN_WRITE ) - *dst |=MIGRATE_PFN_WRITE ; -if (order) - *dst |=MIGRATE_PFN_COMPOUND ; + +if (dpage) { +lock_page (dpage); + *dst |=migrate_pfn (page_to_pfn (dpage)); + }for (i =0 ; i < (1 << order); i++) {struct page *src_page;struct page *dst_page; +/* Try with smaller pages if large allocation fails */ +if (!dpage && order) { + dpage =alloc_page_vma (GFP_HIGHUSER_MOVABLE , args->vma, addr); +lock_page (dpage); + dst[i] =migrate_pfn (page_to_pfn (dpage)); + dst_page =pfn_to_page (page_to_pfn (dpage)); + dpage =NULL ;/* For the next iteration */ + }else { + dst_page =pfn_to_page (page_to_pfn (dpage) + i); + } + src_page =pfn_to_page (page_to_pfn (spage) + i); - dst_page =pfn_to_page (page_to_pfn (dpage) + i);xa_erase (&dmirror->pt, addr >>PAGE_SHIFT ); + addr +=PAGE_SIZE ;copy_highpage (dst_page, src_page); } next: - addr +=PAGE_SIZE << order; src +=1 << order; dst +=1 << order; }
@@ -1514,6 +1531,10 @@ static long dmirror_fops_unlocked_ioctl(struct file *filp,dmirror_device_remove_chunks (dmirror->mdevice); ret =0 ;break ; +case HMM_DMIRROR_FLAGS : + dmirror->flags = cmd.npages; + ret =0 ; +break ;default :return -EINVAL ;
lib/test_hmm_uapi.h +3 -0
@@ -37,6 +37,9 @@ struct hmm_dmirror_cmd {#define HMM_DMIRROR_EXCLUSIVE _IOWR('H', 0x05, struct hmm_dmirror_cmd)#define HMM_DMIRROR_CHECK_EXCLUSIVE _IOWR('H', 0x06, struct hmm_dmirror_cmd)#define HMM_DMIRROR_RELEASE _IOWR('H', 0x07, struct hmm_dmirror_cmd) +#define HMM_DMIRROR_FLAGS _IOWR('H', 0x08, struct hmm_dmirror_cmd) + +#define HMM_DMIRROR_FLAG_FAIL_ALLOC (1ULL << 0)/* * Values returned in hmm_dmirror_cmd.ptr for HMM_DMIRROR_SNAPSHOT.
Signature · 1 line
2.50.1
PATCH v6 12/15 selftests/mm/hmm-tests: new tests for zone device THP migration
In reply to Balbir SinghAdd new tests for migrating anon THP pages, including anon_huge, anon_huge_zero and error cases involving forced splitting of pages during migration.
--- tools/testing/selftests/mm/hmm-tests.c | 410 +++++++++++++++++++++++++ 1 file changed, 410 insertions(+)
tools/testing/selftests/mm/hmm-tests.c +410 -0
@@ -2055,4 +2055,414 @@ TEST_F(hmm, hmm_cow_in_device)hmm_buffer_free (buffer); } + +/* +* Migrate private anonymous huge empty page. +*/ +TEST_F(hmm, migrate_anon_huge_empty) +{ +struct hmm_buffer *buffer; +unsigned long npages; +unsigned long size; +unsigned long i; +void *old_ptr; +void *map; +int *ptr; +int ret; + + size =TWOMEG ; + + buffer =malloc (sizeof (*buffer)); +ASSERT_NE (buffer,NULL ); + + buffer->fd =-1 ; + buffer->size =2 * size; + buffer->mirror =malloc (size); +ASSERT_NE (buffer->mirror,NULL ); +memset (buffer->mirror,0xFF , size); + + buffer->ptr =mmap (NULL ,2 * size, +PROT_READ , +MAP_PRIVATE |MAP_ANONYMOUS , + buffer->fd,0 ); +ASSERT_NE (buffer->ptr,MAP_FAILED ); + + npages = size >> self->page_shift; + map = (void *)ALIGN ((uintptr_t )buffer->ptr, size); + ret =madvise (map, size,MADV_HUGEPAGE ); +ASSERT_EQ (ret,0 ); + old_ptr = buffer->ptr; + buffer->ptr = map; + +/* Migrate memory to device. */ + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + +/* Check what the device read. */ +for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i],0 ); + + buffer->ptr = old_ptr; +hmm_buffer_free (buffer); +} + +/* +* Migrate private anonymous huge zero page. +*/ +TEST_F(hmm, migrate_anon_huge_zero) +{ +struct hmm_buffer *buffer; +unsigned long npages; +unsigned long size; +unsigned long i; +void *old_ptr; +void *map; +int *ptr; +int ret; +int val; + + size =TWOMEG ; + + buffer =malloc (sizeof (*buffer)); +ASSERT_NE (buffer,NULL ); + + buffer->fd =-1 ; + buffer->size =2 * size; + buffer->mirror =malloc (size); +ASSERT_NE (buffer->mirror,NULL ); +memset (buffer->mirror,0xFF , size); + + buffer->ptr =mmap (NULL ,2 * size, +PROT_READ , +MAP_PRIVATE |MAP_ANONYMOUS , + buffer->fd,0 ); +ASSERT_NE (buffer->ptr,MAP_FAILED ); + + npages = size >> self->page_shift; + map = (void *)ALIGN ((uintptr_t )buffer->ptr, size); + ret =madvise (map, size,MADV_HUGEPAGE ); +ASSERT_EQ (ret,0 ); + old_ptr = buffer->ptr; + buffer->ptr = map; + +/* Initialize a read-only zero huge page. */ + val = *(int *)buffer->ptr; +ASSERT_EQ (val,0 ); + +/* Migrate memory to device. */ + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + +/* Check what the device read. */ +for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i],0 ); + +/* Fault pages back to system memory and check them. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) { +ASSERT_EQ (ptr[i],0 ); +/* If it asserts once, it probably will 500,000 times */ +if (ptr[i] !=0 ) +break ; + } + + buffer->ptr = old_ptr; +hmm_buffer_free (buffer); +} + +/* +* Migrate private anonymous huge page and free. +*/ +TEST_F(hmm, migrate_anon_huge_free) +{ +struct hmm_buffer *buffer; +unsigned long npages; +unsigned long size; +unsigned long i; +void *old_ptr; +void *map; +int *ptr; +int ret; + + size =TWOMEG ; + + buffer =malloc (sizeof (*buffer)); +ASSERT_NE (buffer,NULL ); + + buffer->fd =-1 ; + buffer->size =2 * size; + buffer->mirror =malloc (size); +ASSERT_NE (buffer->mirror,NULL ); +memset (buffer->mirror,0xFF , size); + + buffer->ptr =mmap (NULL ,2 * size, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS , + buffer->fd,0 ); +ASSERT_NE (buffer->ptr,MAP_FAILED ); + + npages = size >> self->page_shift; + map = (void *)ALIGN ((uintptr_t )buffer->ptr, size); + ret =madvise (map, size,MADV_HUGEPAGE ); +ASSERT_EQ (ret,0 ); + old_ptr = buffer->ptr; + buffer->ptr = map; + +/* Initialize buffer in system memory. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) + ptr[i] = i; + +/* Migrate memory to device. */ + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + +/* Check what the device read. */ +for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], i); + +/* Try freeing it. */ + ret =madvise (map, size,MADV_FREE ); +ASSERT_EQ (ret,0 ); + + buffer->ptr = old_ptr; +hmm_buffer_free (buffer); +} + +/* +* Migrate private anonymous huge page and fault back to sysmem. +*/ +TEST_F(hmm, migrate_anon_huge_fault) +{ +struct hmm_buffer *buffer; +unsigned long npages; +unsigned long size; +unsigned long i; +void *old_ptr; +void *map; +int *ptr; +int ret; + + size =TWOMEG ; + + buffer =malloc (sizeof (*buffer)); +ASSERT_NE (buffer,NULL ); + + buffer->fd =-1 ; + buffer->size =2 * size; + buffer->mirror =malloc (size); +ASSERT_NE (buffer->mirror,NULL ); +memset (buffer->mirror,0xFF , size); + + buffer->ptr =mmap (NULL ,2 * size, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS , + buffer->fd,0 ); +ASSERT_NE (buffer->ptr,MAP_FAILED ); + + npages = size >> self->page_shift; + map = (void *)ALIGN ((uintptr_t )buffer->ptr, size); + ret =madvise (map, size,MADV_HUGEPAGE ); +ASSERT_EQ (ret,0 ); + old_ptr = buffer->ptr; + buffer->ptr = map; + +/* Initialize buffer in system memory. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) + ptr[i] = i; + +/* Migrate memory to device. */ + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + +/* Check what the device read. */ +for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], i); + +/* Fault pages back to system memory and check them. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], i); + + buffer->ptr = old_ptr; +hmm_buffer_free (buffer); +} + +/* +* Migrate private anonymous huge page with allocation errors. +*/ +TEST_F(hmm, migrate_anon_huge_err) +{ +struct hmm_buffer *buffer; +unsigned long npages; +unsigned long size; +unsigned long i; +void *old_ptr; +void *map; +int *ptr; +int ret; + + size =TWOMEG ; + + buffer =malloc (sizeof (*buffer)); +ASSERT_NE (buffer,NULL ); + + buffer->fd =-1 ; + buffer->size =2 * size; + buffer->mirror =malloc (2 * size); +ASSERT_NE (buffer->mirror,NULL ); +memset (buffer->mirror,0xFF ,2 * size); + + old_ptr =mmap (NULL ,2 * size,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS , buffer->fd,0 ); +ASSERT_NE (old_ptr,MAP_FAILED ); + + npages = size >> self->page_shift; + map = (void *)ALIGN ((uintptr_t )old_ptr, size); + ret =madvise (map, size,MADV_HUGEPAGE ); +ASSERT_EQ (ret,0 ); + buffer->ptr = map; + +/* Initialize buffer in system memory. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) + ptr[i] = i; + +/* Migrate memory to device but force a THP allocation error. */ + ret =hmm_dmirror_cmd (self->fd,HMM_DMIRROR_FLAGS , buffer, +HMM_DMIRROR_FLAG_FAIL_ALLOC ); +ASSERT_EQ (ret,0 ); + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + +/* Check what the device read. */ +for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) { +ASSERT_EQ (ptr[i], i); +if (ptr[i] != i) +break ; + } + +/* Try faulting back a single (PAGE_SIZE) page. */ + ptr = buffer->ptr; +ASSERT_EQ (ptr[2048 ],2048 ); + +/* unmap and remap the region to reset things. */ + ret =munmap (old_ptr,2 * size); +ASSERT_EQ (ret,0 ); + old_ptr =mmap (NULL ,2 * size,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS , buffer->fd,0 ); +ASSERT_NE (old_ptr,MAP_FAILED ); + map = (void *)ALIGN ((uintptr_t )old_ptr, size); + ret =madvise (map, size,MADV_HUGEPAGE ); +ASSERT_EQ (ret,0 ); + buffer->ptr = map; + +/* Initialize buffer in system memory. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) + ptr[i] = i; + +/* Migrate THP to device. */ + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + +/* +* Force an allocation error when faulting back a THP resident in the +* device. +*/ + ret =hmm_dmirror_cmd (self->fd,HMM_DMIRROR_FLAGS , buffer, +HMM_DMIRROR_FLAG_FAIL_ALLOC ); +ASSERT_EQ (ret,0 ); + + ret =hmm_migrate_dev_to_sys (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); + ptr = buffer->ptr; +ASSERT_EQ (ptr[2048 ],2048 ); + + buffer->ptr = old_ptr; +hmm_buffer_free (buffer); +} + +/* +* Migrate private anonymous huge zero page with allocation errors. +*/ +TEST_F(hmm, migrate_anon_huge_zero_err) +{ +struct hmm_buffer *buffer; +unsigned long npages; +unsigned long size; +unsigned long i; +void *old_ptr; +void *map; +int *ptr; +int ret; + + size =TWOMEG ; + + buffer =malloc (sizeof (*buffer)); +ASSERT_NE (buffer,NULL ); + + buffer->fd =-1 ; + buffer->size =2 * size; + buffer->mirror =malloc (2 * size); +ASSERT_NE (buffer->mirror,NULL ); +memset (buffer->mirror,0xFF ,2 * size); + + old_ptr =mmap (NULL ,2 * size,PROT_READ , +MAP_PRIVATE |MAP_ANONYMOUS , buffer->fd,0 ); +ASSERT_NE (old_ptr,MAP_FAILED ); + + npages = size >> self->page_shift; + map = (void *)ALIGN ((uintptr_t )old_ptr, size); + ret =madvise (map, size,MADV_HUGEPAGE ); +ASSERT_EQ (ret,0 ); + buffer->ptr = map; + +/* Migrate memory to device but force a THP allocation error. */ + ret =hmm_dmirror_cmd (self->fd,HMM_DMIRROR_FLAGS , buffer, +HMM_DMIRROR_FLAG_FAIL_ALLOC ); +ASSERT_EQ (ret,0 ); + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + +/* Check what the device read. */ +for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i],0 ); + +/* Try faulting back a single (PAGE_SIZE) page. */ + ptr = buffer->ptr; +ASSERT_EQ (ptr[2048 ],0 ); + +/* unmap and remap the region to reset things. */ + ret =munmap (old_ptr,2 * size); +ASSERT_EQ (ret,0 ); + old_ptr =mmap (NULL ,2 * size,PROT_READ , +MAP_PRIVATE |MAP_ANONYMOUS , buffer->fd,0 ); +ASSERT_NE (old_ptr,MAP_FAILED ); + map = (void *)ALIGN ((uintptr_t )old_ptr, size); + ret =madvise (map, size,MADV_HUGEPAGE ); +ASSERT_EQ (ret,0 ); + buffer->ptr = map; + +/* Initialize buffer in system memory (zero THP page). */ + ret = ptr[0 ]; +ASSERT_EQ (ret,0 ); + +/* Migrate memory to device but force a THP allocation error. */ + ret =hmm_dmirror_cmd (self->fd,HMM_DMIRROR_FLAGS , buffer, +HMM_DMIRROR_FLAG_FAIL_ALLOC ); +ASSERT_EQ (ret,0 ); + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + +/* Fault the device memory back and check it. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i],0 ); + + buffer->ptr = old_ptr; +hmm_buffer_free (buffer); +}TEST_HARNESS_MAIN
Signature · 1 line
2.50.1
PATCH v6 13/15 selftests/mm/hmm-tests: partial unmap, mremap and anon_write tests
In reply to Balbir SinghFrom: Matthew Brost <[email protected]>
Add partial unmap test case which munmaps memory while in the device.
Add tests exercising mremap on faulted-in memory (CPU and GPU) at various offsets and verify correctness.
Update anon_write_child to read device memory after fork verifying this flow works in the kernel.
Both THP and non-THP cases are updated.
--- tools/testing/selftests/mm/hmm-tests.c | 312 ++++++++++++++++++++----- 1 file changed, 252 insertions(+), 60 deletions(-)
tools/testing/selftests/mm/hmm-tests.c +252 -60
@@ -50,6 +50,8 @@ enum { HMM_COHERENCE_DEVICE_TWO, }; +#define ONEKB (1 << 10) +#define ONEMEG (1 << 20)#define TWOMEG (1 << 21)#define HMM_BUFFER_SIZE (1024 << 12)#define HMM_PATH_MAX 64
@@ -525,6 +527,8 @@ TEST_F(hmm, anon_write_prot)/* * Check that a device writing an anonymous private mapping * will copy-on-write if a child process inherits the mapping. +* +* Also verifies after fork() memory the device can be read by child. */ TEST_F(hmm, anon_write_child) {
@@ -532,72 +536,101 @@ TEST_F(hmm, anon_write_child)unsigned long npages;unsigned long size;unsigned long i; +void *old_ptr; +void *map;int *ptr;pid_t pid;int child_fd; -int ret; - - npages =ALIGN (HMM_BUFFER_SIZE , self->page_size) >> self->page_shift; -ASSERT_NE (npages,0 ); - size = npages << self->page_shift; - - buffer =malloc (sizeof (*buffer)); -ASSERT_NE (buffer,NULL ); - - buffer->fd =-1 ; - buffer->size = size; - buffer->mirror =malloc (size); -ASSERT_NE (buffer->mirror,NULL ); - - buffer->ptr =mmap (NULL , size, -PROT_READ |PROT_WRITE , -MAP_PRIVATE |MAP_ANONYMOUS , - buffer->fd,0 ); -ASSERT_NE (buffer->ptr,MAP_FAILED ); - -/* Initialize buffer->ptr so we can tell if it is written. */ -for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) - ptr[i] = i; +int ret, use_thp, migrate; + +for (migrate =0 ; migrate <2 ; ++migrate) { +for (use_thp =0 ; use_thp <2 ; ++use_thp) { + npages =ALIGN (use_thp ?TWOMEG :HMM_BUFFER_SIZE , + self->page_size) >> self->page_shift; +ASSERT_NE (npages,0 ); + size = npages << self->page_shift; + + buffer =malloc (sizeof (*buffer)); +ASSERT_NE (buffer,NULL ); + + buffer->fd =-1 ; + buffer->size = size *2 ; + buffer->mirror =malloc (size); +ASSERT_NE (buffer->mirror,NULL ); + + buffer->ptr =mmap (NULL , size *2 , +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS , + buffer->fd,0 ); +ASSERT_NE (buffer->ptr,MAP_FAILED ); + + old_ptr = buffer->ptr; +if (use_thp) { + map = (void *)ALIGN ((uintptr_t )buffer->ptr, size); + ret =madvise (map, size,MADV_HUGEPAGE ); +ASSERT_EQ (ret,0 ); + buffer->ptr = map; + } + +/* Initialize buffer->ptr so we can tell if it is written. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) + ptr[i] = i; + +/* Initialize data that the device will write to buffer->ptr. */ +for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) + ptr[i] = -i; + +if (migrate) { + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + + } + + pid =fork (); +if (pid ==-1 ) +ASSERT_EQ (pid,0 ); +if (pid !=0 ) { +waitpid (pid, &ret,0 ); +ASSERT_EQ (WIFEXITED (ret),1 ); + +/* Check that the parent's buffer did not change. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], i); + + buffer->ptr = old_ptr; +hmm_buffer_free (buffer); +continue ; + } + +/* Check that we see the parent's values. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], i); +if (!migrate) { +for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], -i); + } + +/* The child process needs its own mirror to its own mm. */ + child_fd =hmm_open (0 ); +ASSERT_GE (child_fd,0 ); + +/* Simulate a device writing system memory. */ + ret =hmm_dmirror_cmd (child_fd,HMM_DMIRROR_WRITE , buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); +ASSERT_EQ (buffer->faults,1 ); -/* Initialize data that the device will write to buffer->ptr. */ -for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) - ptr[i] = -i; +/* Check what the device wrote. */ +if (!migrate) { +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], -i); + } - pid =fork (); -if (pid ==-1 ) -ASSERT_EQ (pid,0 ); -if (pid !=0 ) { -waitpid (pid, &ret,0 ); -ASSERT_EQ (WIFEXITED (ret),1 ); - -/* Check that the parent's buffer did not change. */ -for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) -ASSERT_EQ (ptr[i], i); -return ; +close (child_fd); +exit (0 ); + } } - -/* Check that we see the parent's values. */ -for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) -ASSERT_EQ (ptr[i], i); -for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) -ASSERT_EQ (ptr[i], -i); - -/* The child process needs its own mirror to its own mm. */ - child_fd =hmm_open (0 ); -ASSERT_GE (child_fd,0 ); - -/* Simulate a device writing system memory. */ - ret =hmm_dmirror_cmd (child_fd,HMM_DMIRROR_WRITE , buffer, npages); -ASSERT_EQ (ret,0 ); -ASSERT_EQ (buffer->cpages, npages); -ASSERT_EQ (buffer->faults,1 ); - -/* Check what the device wrote. */ -for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) -ASSERT_EQ (ptr[i], -i); - -close (child_fd); -exit (0 ); }/*
@@ -2289,6 +2322,165 @@ TEST_F(hmm, migrate_anon_huge_fault)hmm_buffer_free (buffer); } +/* +* Migrate memory and fault back to sysmem after partially unmapping. +*/ +TEST_F(hmm, migrate_partial_unmap_fault) +{ +struct hmm_buffer *buffer; +unsigned long npages; +unsigned long size =TWOMEG ; +unsigned long i; +void *old_ptr; +void *map; +int *ptr; +int ret, j, use_thp; +int offsets[] = {0 ,512 *ONEKB ,ONEMEG }; + +for (use_thp =0 ; use_thp <2 ; ++use_thp) { +for (j =0 ; j <ARRAY_SIZE (offsets); ++j) { + buffer =malloc (sizeof (*buffer)); +ASSERT_NE (buffer,NULL ); + + buffer->fd =-1 ; + buffer->size =2 * size; + buffer->mirror =malloc (size); +ASSERT_NE (buffer->mirror,NULL ); +memset (buffer->mirror,0xFF , size); + + buffer->ptr =mmap (NULL ,2 * size, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS , + buffer->fd,0 ); +ASSERT_NE (buffer->ptr,MAP_FAILED ); + + npages = size >> self->page_shift; + map = (void *)ALIGN ((uintptr_t )buffer->ptr, size); +if (use_thp) + ret =madvise (map, size,MADV_HUGEPAGE ); +else + ret =madvise (map, size,MADV_NOHUGEPAGE ); +ASSERT_EQ (ret,0 ); + old_ptr = buffer->ptr; + buffer->ptr = map; + +/* Initialize buffer in system memory. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) + ptr[i] = i; + +/* Migrate memory to device. */ + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + +/* Check what the device read. */ +for (i =0 , ptr = buffer->mirror; i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], i); + +munmap (buffer->ptr + offsets[j],ONEMEG ); + +/* Fault pages back to system memory and check them. */ +for (i =0 , ptr = buffer->ptr; i < size /sizeof (*ptr); ++i) +if (i *sizeof (int ) < offsets[j] || + i *sizeof (int ) >= offsets[j] +ONEMEG ) +ASSERT_EQ (ptr[i], i); + + buffer->ptr = old_ptr; +hmm_buffer_free (buffer); + } + } +} + +TEST_F(hmm, migrate_remap_fault) +{ +struct hmm_buffer *buffer; +unsigned long npages; +unsigned long size =TWOMEG ; +unsigned long i; +void *old_ptr, *new_ptr =NULL ; +void *map; +int *ptr; +int ret, j, use_thp, dont_unmap, before; +int offsets[] = {0 ,512 *ONEKB ,ONEMEG }; + +for (before =0 ; before <2 ; ++before) { +for (dont_unmap =0 ; dont_unmap <2 ; ++dont_unmap) { +for (use_thp =0 ; use_thp <2 ; ++use_thp) { +for (j =0 ; j <ARRAY_SIZE (offsets); ++j) { +int flags =MREMAP_MAYMOVE |MREMAP_FIXED ; + +if (dont_unmap) + flags |=MREMAP_DONTUNMAP ; + + buffer =malloc (sizeof (*buffer)); +ASSERT_NE (buffer,NULL ); + + buffer->fd =-1 ; + buffer->size =8 * size; + buffer->mirror =malloc (size); +ASSERT_NE (buffer->mirror,NULL ); +memset (buffer->mirror,0xFF , size); + + buffer->ptr =mmap (NULL , buffer->size, +PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS , + buffer->fd,0 ); +ASSERT_NE (buffer->ptr,MAP_FAILED ); + + npages = size >> self->page_shift; + map = (void *)ALIGN ((uintptr_t )buffer->ptr, size); +if (use_thp) + ret =madvise (map, size,MADV_HUGEPAGE ); +else + ret =madvise (map, size,MADV_NOHUGEPAGE ); +ASSERT_EQ (ret,0 ); + old_ptr = buffer->ptr; +munmap (map + size, size *2 ); + buffer->ptr = map; + +/* Initialize buffer in system memory. */ +for (i =0 , ptr = buffer->ptr; + i < size /sizeof (*ptr); ++i) + ptr[i] = i; + +if (before) { + new_ptr =mremap ((void *)map, size, size, flags, + map + size + offsets[j]); +ASSERT_NE (new_ptr,MAP_FAILED ); + buffer->ptr = new_ptr; + } + +/* Migrate memory to device. */ + ret =hmm_migrate_sys_to_dev (self->fd, buffer, npages); +ASSERT_EQ (ret,0 ); +ASSERT_EQ (buffer->cpages, npages); + +/* Check what the device read. */ +for (i =0 , ptr = buffer->mirror; + i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], i); + +if (!before) { + new_ptr =mremap ((void *)map, size, size, flags, + map + size + offsets[j]); +ASSERT_NE (new_ptr,MAP_FAILED ); + buffer->ptr = new_ptr; + } + +/* Fault pages back to system memory and check them. */ +for (i =0 , ptr = buffer->ptr; + i < size /sizeof (*ptr); ++i) +ASSERT_EQ (ptr[i], i); + +munmap (new_ptr, size); + buffer->ptr = old_ptr; +hmm_buffer_free (buffer); + } + } + } + } +} +/* * Migrate private anonymous huge page with allocation errors. */
Signature · 1 line
2.50.1
PATCH v6 14/15 selftests/mm/hmm-tests: new throughput tests including THP
In reply to Balbir SinghAdd new benchmark style support to test transfer bandwidth for zone device memory operations.
--- tools/testing/selftests/mm/hmm-tests.c | 197 ++++++++++++++++++++++++- 1 file changed, 196 insertions(+), 1 deletion(-)
tools/testing/selftests/mm/hmm-tests.c +196 -1
@@ -25,6 +25,7 @@#include <sys/stat.h> #include <sys/mman.h> #include <sys/ioctl.h> +#include <sys/time.h> /*
@@ -209,8 +210,10 @@ static void hmm_buffer_free(struct hmm_buffer *buffer)if (buffer ==NULL )return ; -if (buffer->ptr) +if (buffer->ptr) {munmap (buffer->ptr, buffer->size); + buffer->ptr =NULL ; + }free (buffer->mirror);free (buffer); }
@@ -2657,4 +2660,196 @@ TEST_F(hmm, migrate_anon_huge_zero_err) buffer->ptr = old_ptr;hmm_buffer_free (buffer); } + +struct benchmark_results { +double sys_to_dev_time; +double dev_to_sys_time; +double throughput_s2d; +double throughput_d2s; +}; + +static double get_time_ms (void ) +{ +struct timeval tv; + +gettimeofday (&tv,NULL ); +return (tv.tv_sec *1000.0 ) + (tv.tv_usec /1000.0 ); +} + +static inline struct hmm_buffer *hmm_buffer_alloc (unsigned long size) +{ +struct hmm_buffer *buffer; + + buffer =malloc (sizeof (*buffer)); + + buffer->fd =-1 ; + buffer->size = size; + buffer->mirror =malloc (size); +memset (buffer->mirror,0xFF , size); +return buffer; +} + +static void print_benchmark_results (const char *test_name,size_t buffer_size, +struct benchmark_results *thp, +struct benchmark_results *regular) +{ +double s2d_improvement = ((regular->sys_to_dev_time - thp->sys_to_dev_time) / + regular->sys_to_dev_time) *100.0 ; +double d2s_improvement = ((regular->dev_to_sys_time - thp->dev_to_sys_time) / + regular->dev_to_sys_time) *100.0 ; +double throughput_s2d_improvement = ((thp->throughput_s2d - regular->throughput_s2d) / + regular->throughput_s2d) *100.0 ; +double throughput_d2s_improvement = ((thp->throughput_d2s - regular->throughput_d2s) / + regular->throughput_d2s) *100.0 ; + +printf ("\n=== %s (%.1f MB) ===\n" , test_name, buffer_size / (1024.0 *1024.0 )); +printf (" | With THP | Without THP | Improvement\n" ); +printf ("---------------------------------------------------------------------\n" ); +printf ("Sys->Dev Migration | %.3f ms | %.3f ms | %.1f%%\n" , + thp->sys_to_dev_time, regular->sys_to_dev_time, s2d_improvement); +printf ("Dev->Sys Migration | %.3f ms | %.3f ms | %.1f%%\n" , + thp->dev_to_sys_time, regular->dev_to_sys_time, d2s_improvement); +printf ("S->D Throughput | %.2f GB/s | %.2f GB/s | %.1f%%\n" , + thp->throughput_s2d, regular->throughput_s2d, throughput_s2d_improvement); +printf ("D->S Throughput | %.2f GB/s | %.2f GB/s | %.1f%%\n" , + thp->throughput_d2s, regular->throughput_d2s, throughput_d2s_improvement); +} + +/* +* Run a single migration benchmark +* fd: file descriptor for hmm device +* use_thp: whether to use THP +* buffer_size: size of buffer to allocate +* iterations: number of iterations +* results: where to store results +*/ +static inline int run_migration_benchmark (int fd,int use_thp,size_t buffer_size, +int iterations,struct benchmark_results *results) +{ +struct hmm_buffer *buffer; +unsigned long npages = buffer_size /sysconf (_SC_PAGESIZE); +double start, end; +double s2d_total =0 , d2s_total =0 ; +int ret, i; +int *ptr; + + buffer =hmm_buffer_alloc (buffer_size); + +/* Map memory */ + buffer->ptr =mmap (NULL , buffer_size,PROT_READ |PROT_WRITE , +MAP_PRIVATE |MAP_ANONYMOUS ,-1 ,0 ); + +if (!buffer->ptr) +return -1 ; + +/* Apply THP hint if requested */ +if (use_thp) + ret =madvise (buffer->ptr, buffer_size,MADV_HUGEPAGE ); +else + ret =madvise (buffer->ptr, buffer_size,MADV_NOHUGEPAGE ); + +if (ret) +return ret; + +/* Initialize memory to make sure pages are allocated */ + ptr = (int *)buffer->ptr; +for (i =0 ; i < buffer_size /sizeof (int ); i++) + ptr[i] = i &0xFF ; + +/* Warmup iteration */ + ret =hmm_migrate_sys_to_dev (fd, buffer, npages); +if (ret) +return ret; + + ret =hmm_migrate_dev_to_sys (fd, buffer, npages); +if (ret) +return ret; + +/* Benchmark iterations */ +for (i =0 ; i < iterations; i++) { +/* System to device migration */ + start =get_time_ms (); + + ret =hmm_migrate_sys_to_dev (fd, buffer, npages); +if (ret) +return ret; + + end =get_time_ms (); + s2d_total += (end - start); + +/* Device to system migration */ + start =get_time_ms (); + + ret =hmm_migrate_dev_to_sys (fd, buffer, npages); +if (ret) +return ret; + + end =get_time_ms (); + d2s_total += (end - start); + } + +/* Calculate average times and throughput */ + results->sys_to_dev_time = s2d_total / iterations; + results->dev_to_sys_time = d2s_total / iterations; + results->throughput_s2d = (buffer_size / (1024.0 *1024.0 *1024.0 )) / + (results->sys_to_dev_time /1000.0 ); + results->throughput_d2s = (buffer_size / (1024.0 *1024.0 *1024.0 )) / + (results->dev_to_sys_time /1000.0 ); + +/* Cleanup */ +hmm_buffer_free (buffer); +return 0 ; +} + +/* +* Benchmark THP migration with different buffer sizes +*/ +TEST_F_TIMEOUT(hmm, benchmark_thp_migration, 120) +{ +struct benchmark_results thp_results, regular_results; +size_t thp_size =2 *1024 *1024 ;/* 2MB - typical THP size */ +int iterations =5 ; + +printf ("\nHMM THP Migration Benchmark\n" ); +printf ("---------------------------\n" ); +printf ("System page size: %ld bytes\n" ,sysconf (_SC_PAGESIZE)); + +/* Test different buffer sizes */ +size_t test_sizes[] = { + thp_size /4 ,/* 512KB - smaller than THP */ + thp_size /2 ,/* 1MB - half THP */ + thp_size,/* 2MB - single THP */ + thp_size *2 ,/* 4MB - two THPs */ + thp_size *4 ,/* 8MB - four THPs */ + thp_size *8 ,/* 16MB - eight THPs */ + thp_size *128 ,/* 256MB - one twenty eight THPs */ + }; + +static const char *const test_names[] = { +"Small Buffer (512KB)" , +"Half THP Size (1MB)" , +"Single THP Size (2MB)" , +"Two THP Size (4MB)" , +"Four THP Size (8MB)" , +"Eight THP Size (16MB)" , +"One twenty eight THP Size (256MB)" + }; + +int num_tests =ARRAY_SIZE (test_sizes); + +/* Run all tests */ +for (int i =0 ; i < num_tests; i++) { +/* Test with THP */ +ASSERT_EQ (run_migration_benchmark (self->fd,1 , test_sizes[i], + iterations, &thp_results),0 ); + +/* Test without THP */ +ASSERT_EQ (run_migration_benchmark (self->fd,0 , test_sizes[i], + iterations, ®ular_results),0 ); + +/* Print results */ +print_benchmark_results (test_names[i], test_sizes[i], + &thp_results, ®ular_results); + } +}TEST_HARNESS_MAIN
Signature · 1 line
2.50.1
PATCH v6 15/15 gpu/drm/nouveau: enable THP support for GPU memory migration
In reply to Balbir SinghEnable MIGRATE_VMA_SELECT_COMPOUND support in nouveau driver to take advantage of THP zone device migration capabilities.
Update migration and eviction code paths to handle compound page sizes appropriately, improving memory bandwidth utilization and reducing migration overhead for large GPU memory allocations.
--- drivers/gpu/drm/nouveau/nouveau_dmem.c | 304 ++++++++++++++++++------- drivers/gpu/drm/nouveau/nouveau_svm.c | 6 +- drivers/gpu/drm/nouveau/nouveau_svm.h | 3 +- 3 files changed, 230 insertions(+), 83 deletions(-)
drivers/gpu/drm/nouveau/nouveau_dmem.c +224 -80
@@ -50,6 +50,7 @@*/ #define DMEM_CHUNK_SIZE (2UL << 20)#define DMEM_CHUNK_NPAGES (DMEM_CHUNK_SIZE >> PAGE_SHIFT) +#define NR_CHUNKS (128)enum nouveau_aper {NOUVEAU_APER_VIRT ,
@@ -83,9 +84,15 @@ struct nouveau_dmem {struct list_head chunks;struct mutex mutex;struct page *free_pages; +struct folio *free_folios;spinlock_t lock; }; +struct nouveau_dmem_dma_info { +dma_addr_t dma_addr; +size_t size; +}; +static struct nouveau_dmem_chunk *nouveau_page_to_chunk (struct page *page) { return container_of(page_pgmap(page), struct nouveau_dmem_chunk,
@@ -112,10 +119,16 @@ static void nouveau_dmem_page_free(struct page *page) {struct nouveau_dmem_chunk *chunk =nouveau_page_to_chunk (page);struct nouveau_dmem *dmem = chunk->drm->dmem; +struct folio *folio =page_folio (page);spin_lock (&dmem->lock); - page->zone_device_data = dmem->free_pages; - dmem->free_pages = page; +if (folio_order (folio)) { + page->zone_device_data = dmem->free_folios; + dmem->free_folios = folio; + }else { + page->zone_device_data = dmem->free_pages; + dmem->free_pages = page; + }WARN_ON (!chunk->callocated); chunk->callocated--;
@@ -139,20 +152,28 @@ static void nouveau_dmem_fence_done(struct nouveau_fence **fence) } } -static int nouveau_dmem_copy_one (struct nouveau_drm *drm,struct page *spage, -struct page *dpage,dma_addr_t *dma_addr) +static int nouveau_dmem_copy_folio (struct nouveau_drm *drm, +struct folio *sfolio,struct folio *dfolio, +struct nouveau_dmem_dma_info *dma_info) {struct device *dev = drm->dev->dev; +struct page *dpage =folio_page (dfolio,0 ); +struct page *spage =folio_page (sfolio,0 ); -lock_page (dpage); +folio_lock (dfolio); - *dma_addr =dma_map_page (dev, dpage,0 ,PAGE_SIZE ,DMA_BIDIRECTIONAL ); -if (dma_mapping_error (dev, *dma_addr)) + dma_info->dma_addr =dma_map_page (dev, dpage,0 ,page_size (dpage), +DMA_BIDIRECTIONAL ); + dma_info->size =page_size (dpage); +if (dma_mapping_error (dev, dma_info->dma_addr))return -EIO ; -if (drm->dmem->migrate.copy_func (drm,1 ,NOUVEAU_APER_HOST , *dma_addr, -NOUVEAU_APER_VRAM ,nouveau_dmem_page_addr (spage))) { -dma_unmap_page (dev, *dma_addr,PAGE_SIZE ,DMA_BIDIRECTIONAL ); +if (drm->dmem->migrate.copy_func (drm,folio_nr_pages (sfolio), +NOUVEAU_APER_HOST , dma_info->dma_addr, +NOUVEAU_APER_VRAM , +nouveau_dmem_page_addr (spage))) { +dma_unmap_page (dev, dma_info->dma_addr,page_size (dpage), +DMA_BIDIRECTIONAL );return -EIO ; }
@@ -165,21 +186,47 @@ static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf)struct nouveau_dmem *dmem = drm->dmem;struct nouveau_fence *fence;struct nouveau_svmm *svmm; -struct page *spage, *dpage; -unsigned long src =0 , dst =0 ; -dma_addr_t dma_addr =0 ; +struct page *dpage;vm_fault_t ret =0 ;struct migrate_vma args = { .vma = vmf->vma, - .start = vmf->address, - .end = vmf->address +PAGE_SIZE , - .src = &src, - .dst = &dst, .pgmap_owner = drm->dev, .fault_page = vmf->page, - .flags =MIGRATE_VMA_SELECT_DEVICE_PRIVATE , + .flags =MIGRATE_VMA_SELECT_DEVICE_PRIVATE | +MIGRATE_VMA_SELECT_COMPOUND , + .src =NULL , + .dst =NULL , }; +unsigned int order, nr; +struct folio *sfolio, *dfolio; +struct nouveau_dmem_dma_info dma_info; + + sfolio =page_folio (vmf->page); + order =folio_order (sfolio); + nr =1 << order; + +/* +* Handle partial unmap faults, where the folio is large, but +* the pmd is split. +*/ +if (vmf->pte) { + order =0 ; + nr =1 ; + } + +if (order) + args.flags |=MIGRATE_VMA_SELECT_COMPOUND ; + args.start =ALIGN_DOWN (vmf->address, (PAGE_SIZE << order)); + args.vma = vmf->vma; + args.end = args.start + (PAGE_SIZE << order); + args.src =kcalloc (nr,sizeof (*args.src),GFP_KERNEL ); + args.dst =kcalloc (nr,sizeof (*args.dst),GFP_KERNEL ); + +if (!args.src || !args.dst) { + ret =VM_FAULT_OOM ; + goto err; + }/* * FIXME what we really want is to find some heuristic to migrate more * than just one page on CPU fault. When such fault happens it is very
@@ -190,20 +237,26 @@ static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf)if (!args.cpages)return 0 ; - spage =migrate_pfn_to_page (src); -if (!spage || !(src &MIGRATE_PFN_MIGRATE )) - goto done; - - dpage =alloc_page_vma (GFP_HIGHUSER | __GFP_ZERO, vmf->vma, vmf->address); -if (!dpage) +if (order) + dpage =folio_page (vma_alloc_folio (GFP_HIGHUSER | __GFP_ZERO, + order, vmf->vma, vmf->address),0 ); +else + dpage =alloc_page_vma (GFP_HIGHUSER | __GFP_ZERO, vmf->vma, + vmf->address); +if (!dpage) { + ret =VM_FAULT_OOM ; goto done; + } - dst =migrate_pfn (page_to_pfn (dpage)); + args.dst[0 ] =migrate_pfn (page_to_pfn (dpage)); +if (order) + args.dst[0 ] |=MIGRATE_PFN_COMPOUND ; + dfolio =page_folio (dpage); - svmm = spage->zone_device_data; + svmm =folio_zone_device_data (sfolio);mutex_lock (&svmm->mutex);nouveau_svmm_invalidate (svmm, args.start, args.end); - ret =nouveau_dmem_copy_one (drm, spage, dpage, &dma_addr); + ret =nouveau_dmem_copy_folio (drm, sfolio, dfolio, &dma_info);mutex_unlock (&svmm->mutex);if (ret) { ret =VM_FAULT_SIGBUS ;
@@ -213,25 +266,40 @@ static vm_fault_t nouveau_dmem_migrate_to_ram(struct vm_fault *vmf)nouveau_fence_new (&fence, dmem->migrate.chan);migrate_vma_pages (&args);nouveau_dmem_fence_done (&fence); -dma_unmap_page (drm->dev->dev, dma_addr,PAGE_SIZE ,DMA_BIDIRECTIONAL ); +dma_unmap_page (drm->dev->dev, dma_info.dma_addr,PAGE_SIZE , +DMA_BIDIRECTIONAL ); done:migrate_vma_finalize (&args); +err: +kfree (args.src); +kfree (args.dst);return ret; } +static void nouveau_dmem_folio_split (struct folio *head,struct folio *tail) +{ +if (tail ==NULL ) +return ; + tail->pgmap = head->pgmap; + tail->mapping = head->mapping; +folio_set_zone_device_data (tail,folio_zone_device_data (head)); +} +static const struct dev_pagemap_ops nouveau_dmem_pagemap_ops = { .page_free = nouveau_dmem_page_free, .migrate_to_ram = nouveau_dmem_migrate_to_ram, + .folio_split = nouveau_dmem_folio_split, };static int -nouveau_dmem_chunk_alloc (struct nouveau_drm *drm,struct page **ppage) +nouveau_dmem_chunk_alloc (struct nouveau_drm *drm,struct page **ppage, +bool is_large) {struct nouveau_dmem_chunk *chunk;struct resource *res;struct page *page;void *ptr; -unsigned long i, pfn_first; +unsigned long i, pfn_first, pfn;int ret; chunk =kzalloc (sizeof (*chunk),GFP_KERNEL );
@@ -241,7 +309,7 @@ nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage) }/* Allocate unused physical address space for device private pages. */ - res =request_free_mem_region (&iomem_resource,DMEM_CHUNK_SIZE , + res =request_free_mem_region (&iomem_resource,DMEM_CHUNK_SIZE *NR_CHUNKS ,"nouveau_dmem" );if (IS_ERR (res)) { ret =PTR_ERR (res);
@@ -274,16 +342,40 @@ nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage) pfn_first = chunk->pagemap.range.start >>PAGE_SHIFT ; page =pfn_to_page (pfn_first);spin_lock (&drm->dmem->lock); -for (i =0 ; i <DMEM_CHUNK_NPAGES -1 ; ++i, ++page) { - page->zone_device_data = drm->dmem->free_pages; - drm->dmem->free_pages = page; + + pfn = pfn_first; +for (i =0 ; i <NR_CHUNKS ; i++) { +int j; + +if (!IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE ) || !is_large) { +for (j =0 ; j <DMEM_CHUNK_NPAGES -1 ; j++, pfn++) { + page =pfn_to_page (pfn); + page->zone_device_data = drm->dmem->free_pages; + drm->dmem->free_pages = page; + } + }else { + page =pfn_to_page (pfn); + page->zone_device_data = drm->dmem->free_folios; + drm->dmem->free_folios =page_folio (page); + pfn +=DMEM_CHUNK_NPAGES ; + } } - *ppage = page; + +/* Move to next page */ +if (is_large) { + *ppage = &drm->dmem->free_folios->page; + drm->dmem->free_folios = (*ppage)->zone_device_data; + }else { + *ppage = drm->dmem->free_pages; + drm->dmem->free_pages = (*ppage)->zone_device_data; + } + chunk->callocated++;spin_unlock (&drm->dmem->lock); -NV_INFO (drm,"DMEM: registered %ldMB of device memory\n" , -DMEM_CHUNK_SIZE >>20 ); +NV_INFO (drm,"DMEM: registered %ldMB of %sdevice memory %lx %lx\n" , +NR_CHUNKS *DMEM_CHUNK_SIZE >>20 , is_large ?"THP " :"" , pfn_first, +nouveau_dmem_page_addr (page));return 0 ;
@@ -298,27 +390,41 @@ nouveau_dmem_chunk_alloc(struct nouveau_drm *drm, struct page **ppage) }static struct page * -nouveau_dmem_page_alloc_locked (struct nouveau_drm *drm) +nouveau_dmem_page_alloc_locked (struct nouveau_drm *drm,bool is_large) {struct nouveau_dmem_chunk *chunk;struct page *page =NULL ; +struct folio *folio =NULL ;int ret; +unsigned int order =0 ;spin_lock (&drm->dmem->lock); -if (drm->dmem->free_pages) { +if (is_large && drm->dmem->free_folios) { + folio = drm->dmem->free_folios; + page = &folio->page; + drm->dmem->free_folios = page->zone_device_data; + chunk =nouveau_page_to_chunk (&folio->page); + chunk->callocated++; +spin_unlock (&drm->dmem->lock); + order =ilog2 (DMEM_CHUNK_NPAGES ); + }else if (!is_large && drm->dmem->free_pages) { page = drm->dmem->free_pages; drm->dmem->free_pages = page->zone_device_data; chunk =nouveau_page_to_chunk (page); chunk->callocated++;spin_unlock (&drm->dmem->lock); + folio =page_folio (page); }else {spin_unlock (&drm->dmem->lock); - ret =nouveau_dmem_chunk_alloc (drm, &page); + ret =nouveau_dmem_chunk_alloc (drm, &page, is_large);if (ret)return NULL ; + folio =page_folio (page); +if (is_large) + order =ilog2 (DMEM_CHUNK_NPAGES ); } -zone_device_page_init (page); +zone_device_folio_init (folio, order);return page; }
@@ -369,12 +475,12 @@ nouveau_dmem_evict_chunk(struct nouveau_dmem_chunk *chunk) {unsigned long i, npages =range_len (&chunk->pagemap.range) >>PAGE_SHIFT ;unsigned long *src_pfns, *dst_pfns; -dma_addr_t *dma_addrs; +struct nouveau_dmem_dma_info *dma_info;struct nouveau_fence *fence; src_pfns =kvcalloc (npages,sizeof (*src_pfns),GFP_KERNEL | __GFP_NOFAIL); dst_pfns =kvcalloc (npages,sizeof (*dst_pfns),GFP_KERNEL | __GFP_NOFAIL); - dma_addrs =kvcalloc (npages,sizeof (*dma_addrs),GFP_KERNEL | __GFP_NOFAIL); + dma_info =kvcalloc (npages,sizeof (*dma_info),GFP_KERNEL | __GFP_NOFAIL);migrate_device_range (src_pfns, chunk->pagemap.range.start >>PAGE_SHIFT , npages);
@@ -382,17 +488,28 @@ nouveau_dmem_evict_chunk(struct nouveau_dmem_chunk *chunk)for (i =0 ; i < npages; i++) {if (src_pfns[i] &MIGRATE_PFN_MIGRATE ) {struct page *dpage; +struct folio *folio =page_folio ( +migrate_pfn_to_page (src_pfns[i])); +unsigned int order =folio_order (folio); + +if (src_pfns[i] &MIGRATE_PFN_COMPOUND ) { + dpage =folio_page ( +folio_alloc ( +GFP_HIGHUSER_MOVABLE , order),0 ); + }else { +/* +* _GFP_NOFAIL because the GPU is going away and there +* is nothing sensible we can do if we can't copy the +* data back. +*/ + dpage =alloc_page (GFP_HIGHUSER | __GFP_NOFAIL); + } -/* -* _GFP_NOFAIL because the GPU is going away and there -* is nothing sensible we can do if we can't copy the -* data back. -*/ - dpage =alloc_page (GFP_HIGHUSER | __GFP_NOFAIL); dst_pfns[i] =migrate_pfn (page_to_pfn (dpage)); -nouveau_dmem_copy_one (chunk->drm, -migrate_pfn_to_page (src_pfns[i]), dpage, - &dma_addrs[i]); +nouveau_dmem_copy_folio (chunk->drm, +page_folio (migrate_pfn_to_page (src_pfns[i])), +page_folio (dpage), + &dma_info[i]); } }
@@ -403,8 +520,9 @@ nouveau_dmem_evict_chunk(struct nouveau_dmem_chunk *chunk)kvfree (src_pfns);kvfree (dst_pfns);for (i =0 ; i < npages; i++) -dma_unmap_page (chunk->drm->dev->dev, dma_addrs[i],PAGE_SIZE ,DMA_BIDIRECTIONAL ); -kvfree (dma_addrs); +dma_unmap_page (chunk->drm->dev->dev, dma_info[i].dma_addr, + dma_info[i].size,DMA_BIDIRECTIONAL ); +kvfree (dma_info); }void
@@ -607,31 +725,36 @@ nouveau_dmem_init(struct nouveau_drm *drm)static unsigned long nouveau_dmem_migrate_copy_one (struct nouveau_drm *drm,struct nouveau_svmm *svmm,unsigned long src, -dma_addr_t *dma_addr,u64 *pfn) +struct nouveau_dmem_dma_info *dma_info,u64 *pfn) {struct device *dev = drm->dev->dev;struct page *dpage, *spage;unsigned long paddr; +bool is_large = false; +unsigned long mpfn; spage =migrate_pfn_to_page (src);if (!(src &MIGRATE_PFN_MIGRATE )) goto out; - dpage =nouveau_dmem_page_alloc_locked (drm); + is_large = src &MIGRATE_PFN_COMPOUND ; + dpage =nouveau_dmem_page_alloc_locked (drm, is_large);if (!dpage) goto out; paddr =nouveau_dmem_page_addr (dpage);if (spage) { - *dma_addr =dma_map_page (dev, spage,0 ,page_size (spage), + dma_info->dma_addr =dma_map_page (dev, spage,0 ,page_size (spage),DMA_BIDIRECTIONAL ); -if (dma_mapping_error (dev, *dma_addr)) + dma_info->size =page_size (spage); +if (dma_mapping_error (dev, dma_info->dma_addr)) goto out_free_page; -if (drm->dmem->migrate.copy_func (drm,1 , -NOUVEAU_APER_VRAM , paddr,NOUVEAU_APER_HOST , *dma_addr)) +if (drm->dmem->migrate.copy_func (drm,folio_nr_pages (page_folio (spage)), +NOUVEAU_APER_VRAM , paddr,NOUVEAU_APER_HOST , + dma_info->dma_addr)) goto out_dma_unmap; }else { - *dma_addr =DMA_MAPPING_ERROR ; + dma_info->dma_addr =DMA_MAPPING_ERROR ;if (drm->dmem->migrate.clear_func (drm,page_size (dpage),NOUVEAU_APER_VRAM , paddr)) goto out_free_page;
@@ -642,10 +765,13 @@ static unsigned long nouveau_dmem_migrate_copy_one(struct nouveau_drm *drm, ((paddr >>PAGE_SHIFT ) <<NVIF_VMM_PFNMAP_V0_ADDR_SHIFT );if (src &MIGRATE_PFN_WRITE ) *pfn |=NVIF_VMM_PFNMAP_V0_W ; -return migrate_pfn (page_to_pfn (dpage)); + mpfn =migrate_pfn (page_to_pfn (dpage)); +if (folio_order (page_folio (dpage))) + mpfn |=MIGRATE_PFN_COMPOUND ; +return mpfn; out_dma_unmap: -dma_unmap_page (dev, *dma_addr,PAGE_SIZE ,DMA_BIDIRECTIONAL ); +dma_unmap_page (dev, dma_info->dma_addr,PAGE_SIZE ,DMA_BIDIRECTIONAL ); out_free_page:nouveau_dmem_page_free_locked (drm, dpage); out:
@@ -655,27 +781,38 @@ static unsigned long nouveau_dmem_migrate_copy_one(struct nouveau_drm *drm,static void nouveau_dmem_migrate_chunk (struct nouveau_drm *drm,struct nouveau_svmm *svmm,struct migrate_vma *args, -dma_addr_t *dma_addrs,u64 *pfns) +struct nouveau_dmem_dma_info *dma_info,u64 *pfns) {struct nouveau_fence *fence;unsigned long addr = args->start, nr_dma =0 , i; +unsigned long order =0 ; + +for (i =0 ; addr < args->end; ) { +struct folio *folio; -for (i =0 ; addr < args->end; i++) { args->dst[i] =nouveau_dmem_migrate_copy_one (drm, svmm, - args->src[i], dma_addrs + nr_dma, pfns + i); -if (!dma_mapping_error (drm->dev->dev, dma_addrs[nr_dma])) + args->src[i], dma_info + nr_dma, pfns + i); +if (!args->dst[i]) { + i++; + addr +=PAGE_SIZE ; +continue ; + } +if (!dma_mapping_error (drm->dev->dev, dma_info[nr_dma].dma_addr)) nr_dma++; - addr +=PAGE_SIZE ; + folio =page_folio (migrate_pfn_to_page (args->dst[i])); + order =folio_order (folio); + i +=1 << order; + addr += (1 << order) *PAGE_SIZE ; }nouveau_fence_new (&fence, drm->dmem->migrate.chan);migrate_vma_pages (args);nouveau_dmem_fence_done (&fence); -nouveau_pfns_map (svmm, args->vma->vm_mm, args->start, pfns, i); +nouveau_pfns_map (svmm, args->vma->vm_mm, args->start, pfns, i, order);while (nr_dma--) { -dma_unmap_page (drm->dev->dev, dma_addrs[nr_dma],PAGE_SIZE , -DMA_BIDIRECTIONAL ); +dma_unmap_page (drm->dev->dev, dma_info[nr_dma].dma_addr, + dma_info[nr_dma].size,DMA_BIDIRECTIONAL ); }migrate_vma_finalize (args); }
@@ -688,20 +825,27 @@ nouveau_dmem_migrate_vma(struct nouveau_drm *drm, unsigned long end) {unsigned long npages = (end - start) >>PAGE_SHIFT ; -unsigned long max =min (SG_MAX_SINGLE_ALLOC , npages); -dma_addr_t *dma_addrs; +unsigned long max = npages;struct migrate_vma args = { .vma = vma, .start = start, .pgmap_owner = drm->dev, - .flags =MIGRATE_VMA_SELECT_SYSTEM , + .flags =MIGRATE_VMA_SELECT_SYSTEM + |MIGRATE_VMA_SELECT_COMPOUND , };unsigned long i;u64 *pfns;int ret = -ENOMEM ; +struct nouveau_dmem_dma_info *dma_info; -if (drm->dmem ==NULL ) -return -ENODEV ; +if (drm->dmem ==NULL ) { + ret = -ENODEV ; + goto out; + } + +if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) +if (max > (unsigned long )HPAGE_PMD_NR ) + max = (unsigned long )HPAGE_PMD_NR ; args.src =kcalloc (max,sizeof (*args.src),GFP_KERNEL );if (!args.src)
@@ -710,8 +854,8 @@ nouveau_dmem_migrate_vma(struct nouveau_drm *drm,if (!args.dst) goto out_free_src; - dma_addrs =kmalloc_array (max,sizeof (*dma_addrs),GFP_KERNEL ); -if (!dma_addrs) + dma_info =kmalloc_array (max,sizeof (*dma_info),GFP_KERNEL ); +if (!dma_info) goto out_free_dst; pfns =nouveau_pfns_alloc (max);
@@ -729,7 +873,7 @@ nouveau_dmem_migrate_vma(struct nouveau_drm *drm, goto out_free_pfns;if (args.cpages) -nouveau_dmem_migrate_chunk (drm, svmm, &args, dma_addrs, +nouveau_dmem_migrate_chunk (drm, svmm, &args, dma_info, pfns); args.start = args.end; }
@@ -738,7 +882,7 @@ nouveau_dmem_migrate_vma(struct nouveau_drm *drm, out_free_pfns:nouveau_pfns_free (pfns); out_free_dma: -kfree (dma_addrs); +kfree (dma_info); out_free_dst:kfree (args.dst); out_free_src:
drivers/gpu/drm/nouveau/nouveau_svm.c +4 -2
@@ -921,12 +921,14 @@ nouveau_pfns_free(u64 *pfns)void nouveau_pfns_map (struct nouveau_svmm *svmm,struct mm_struct *mm, -unsigned long addr,u64 *pfns,unsigned long npages) +unsigned long addr,u64 *pfns,unsigned long npages, +unsigned int page_shift) {struct nouveau_pfnmap_args *args =nouveau_pfns_to_args (pfns); args->p.addr = addr; - args->p.size = npages <<PAGE_SHIFT ; + args->p.size = npages << page_shift; + args->p.page = page_shift;mutex_lock (&svmm->mutex);
drivers/gpu/drm/nouveau/nouveau_svm.h +2 -1
@@ -33,7 +33,8 @@ void nouveau_svmm_invalidate(struct nouveau_svmm *svmm, u64 start, u64 limit);u64 *nouveau_pfns_alloc (unsigned long npages);void nouveau_pfns_free (u64 *pfns);void nouveau_pfns_map (struct nouveau_svmm *svmm,struct mm_struct *mm, -unsigned long addr,u64 *pfns,unsigned long npages); +unsigned long addr,u64 *pfns,unsigned long npages, +unsigned int page_shift);#else /* IS_ENABLED(CONFIG_DRM_NOUVEAU_SVM) */ static inline void nouveau_svm_init (struct nouveau_drm *drm) {}static inline void nouveau_svm_fini (struct nouveau_drm *drm) {}
Signature · 1 line
2.50.1