THP support for zone device page migration
Subsystems (14)
v2 cover THP support for zone device page migration
This patch series adds support for THP migration of zone device pages. To do so, the patches implement support for folio zone device pages by adding support for setting up larger order pages. Larger order pages provide a speedup in throughput and latency.
In my local testing (using lib/test_hmm) and a throughput test, the series shows a 350% improvement in data transfer throughput and a 500% 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:
Francois Dugast posted patches support for HMM handling [4], the proposed changes can build on top of this series to provide support for HMM fault handling.
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://lore.kernel.org/lkml/[email protected]/
These patches are built on top of mm/mm-stable
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 (11):
mm/zone_device: support large zone device private folios mm/thp: zone_device awareness in THP handling code mm/migrate_device: THP migration of zone device pages mm/memory/fault: add support for zone device THP fault handling lib/test_hmm: test cases and support for zone device private THP mm/memremap: add folio_split support
mm/thp: add split during migration support lib/test_hmm: add test case for split pages selftests/mm/hmm-tests: new tests for zone device THP migration gpu/drm/nouveau: add THP migration support selftests/mm/hmm-tests: new throughput tests including THP
drivers/gpu/drm/nouveau/nouveau_dmem.c | 246 +++++++--- drivers/gpu/drm/nouveau/nouveau_svm.c | 6 +- drivers/gpu/drm/nouveau/nouveau_svm.h | 3 +- include/linux/huge_mm.h | 19 +- include/linux/memremap.h | 51 ++- include/linux/migrate.h | 2 + include/linux/mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 + lib/test_hmm.c | 432 ++++++++++++++---- lib/test_hmm_uapi.h | 3 + mm/huge_memory.c | 358 ++++++++++++--- mm/memory.c | 6 +- mm/memremap.c | 48 +- mm/migrate_device.c | 517 ++++++++++++++++++--- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +- tools/testing/selftests/mm/hmm-tests.c | 607 ++++++++++++++++++++++++- 19 files changed, 2040 insertions(+), 319 deletions(-)
Signature · 1 line
2.50.1
PATCH v2 1/11 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.
--- include/linux/memremap.h | 10 ++++++++- mm/memremap.c | 48 +++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 14 deletions(-)
include/linux/memremap.h +9 -1
@@ -199,7 +199,7 @@ static inline bool folio_is_fsdax(const struct folio *folio) }#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);
@@ -209,6 +209,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 +35 -13
80 lines not on this page · raw message
Signature · 1 line
2.50.1
Re: [v2 01/11] mm/zone_device: support large zone device private folios
Quoted 70 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.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/memremap.h | 10 ++++++++- mm/memremap.c | 48 +++++++++++++++++++++++++++++----------- 2 files changed, 44 insertions(+), 14 deletions(-)include/linux/memremap.h +9
-1@@ -199,7 +199,7 @@ static inline bool folio_is_fsdax(const struct folio *folio) }#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);@@ -209,6 +209,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 +0
-0
mm/memremap.c EXPORT_SYMBOL_GPL(get_dev_pagemap); · line 431
Quoted 5 lines
mm/memremap.c +2
-0@@ -427,20 +427,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap);void free_zone_device_folio (struct folio *folio) {struct dev_pagemap *pgmap = folio->pgmap; +unsigned int nr =folio_nr_pages (folio); +int i;
"unsigned long" is to be future-proof.
(folio_nr_pages() returns long and probably soon unsigned long)
[ I'd probably all it "nr_pages" ]
Quoted 22 lines
mm/memremap.c +4
-7if (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)); }/*
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 466
Quoted 4 lines
mm/memremap.c +1
-0@@ -464,11 +463,20 @@ void free_zone_device_folio(struct folio *folio)switch (pgmap->type) {case MEMORY_DEVICE_PRIVATE : +if (folio_test_large (folio)) {
Could do "nr > 1" if we already have that value around.
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 467
Quoted 1 line
mm/memremap.c +1
-0+folio_unqueue_deferred_split (folio);
I think I asked that already but maybe missed the reply: Should these folios ever be added to the deferred split queue and is there any value in splitting them under memory pressure in the shrinker?
My gut feeling is "No", because the buddy cannot make use of these folios, but maybe there is an interesting case where we want that behavior?
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 472
Quoted 5 lines
mm/memremap.c +5
-0+ +percpu_ref_put_many (&folio->pgmap->ref, nr -1 ); + } + pgmap->ops->page_free (&folio->page); +percpu_ref_put (&folio->pgmap->ref);
Coold you simply do a
percpu_ref_put_many(&folio->pgmap->ref, nr);
here, or would that be problematic?
Quoted 16 lines
mm/memremap.c +4
-2+ folio->page.mapping =NULL ; +break ;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 (&folio->pgmap->ref);break ;case MEMORY_DEVICE_GENERIC :@@ -491,14 +499,28 @@ void free_zone_device_folio(struct folio *folio) } }
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 510
Quoted 10 lines
mm/memremap.c +8
-1-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 ); + +/* +* Only PMD level migration is supported for THP migration +*/
Talking about something that does not exist yet (and is very specific) sounds a bit weird.
Should this go into a different patch, or could we rephrase the comment to be a bit more generic?
In this patch here, nothing would really object to "order" being intermediate.
(also, this is a device_private limitation? shouldn't that check go somehwere where we can perform this device-private limitation check?)
Quoted 19 lines
mm/memremap.c +10
-3+WARN_ON_ONCE (order && order !=HPAGE_PMD_ORDER ); +/* * 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);
Signature · 3 lines
Cheers, David / dhildenb
Re: [v2 01/11] mm/zone_device: support large zone device private folios
Quoted 68 lines · David Hildenbrand
Quoted 67 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.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Signed-off-by Balbir Singh <[email protected]>---
include/linux/memremap.h| 10 ++++++++-
mm/memremap.c | 48 +++++++++++++++++++++++++++++-----------2 files changed, 44 insertions(+), 14 deletions(-)include/linux/memremap.h +9
-1@@ -199,7 +199,7 @@ static inline bool folio_is_fsdax(const struct folio *folio) } #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);@@ -209,6 +209,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 +0
-0
Quoted 8 lines
mm/memremap.c EXPORT_SYMBOL_GPL(get_dev_pagemap); · line 431
Quoted 6 lines
mm/memremap.c +2
-0@@ -427,20 +427,19 @@ EXPORT_SYMBOL_GPL(get_dev_pagemap); void free_zone_device_folio(struct folio *folio) { struct dev_pagemap *pgmap = folio->pgmap; + unsigned int nr = folio_nr_pages(folio); + int i;"unsigned long" is to be future-proof.
Will change this for v3
Quoted 4 lines
(
folio_nr_pages()returns long and probably soon unsigned long)[ I'd probably all it "
nr_pages" ]
Ack
Quoted 16 lines
Quoted 16 lines
mm/memremap.c +0
-0if (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_exclusiveon 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));
Quoted 9 lines
Quoted 6 lines
}
/*mm/memremap.c +0
-0@@ -464,11 +463,20 @@ void free_zone_device_folio(struct folio *folio) switch (pgmap->type) {case
MEMORY_DEVICE_PRIVATE:
+ if (folio_test_large(folio)) {Could do "nr > 1" if we already have that value around.
Ack
Quoted 12 lines
Quoted 1 line
+
folio_unqueue_deferred_split(folio);I think I asked that already but maybe missed the reply: Should these folios ever be added to the deferred split queue and is there any value in splitting them under memory pressure in the shrinker?
My gut feeling is "No", because the buddy cannot make use of these folios, but maybe there is an interesting case where we want that behavior?
Quoted 5 lines
+
+percpu_ref_put_many(&folio->pgmap->ref, nr - 1); + }
+pgmap->ops->page_free(&folio->page);
+percpu_ref_put(&folio->pgmap->ref);
Quoted 6 lines
Coold you simply do a
percpu_ref_put_many(&folio->pgmap->ref, nr);
here, or would that be problematic?
I can definitely try that
Quoted 27 lines
Quoted 14 lines
+
folio->page.mapping= NULL;
+ break;
caseMEMORY_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(&folio->pgmap->ref);
break;
caseMEMORY_DEVICE_GENERIC:mm/memremap.c +0
-0@@ -491,14 +499,28 @@ void free_zone_device_folio(struct folio *folio) } }mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 510
Quoted 10 lines
mm/memremap.c +8
-0-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); + + /* +* Only PMD level migration is supported for THP migration +*/ Talking about something that does not exist yet (and is very specific) sounds a bit weird.
Quoted 6 lines
Should this go into a different patch, or could we rephrase the comment to be a bit more generic?
In this patch here, nothing would really object to "order" being intermediate.
(also, this is a
device_privatelimitation? shouldn't that check go somehwere where we can perform this device-private limitation check?)
I can remove the limitation and keep it generic
Quoted 11 lines
Quoted 11 lines
mm/memremap.c +4
-2+ WARN_ON_ONCE(order && order != HPAGE_PMD_ORDER); + /* * 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);
Quoted 10 lines
mm/memremap.c void free_zone_device_folio(struct folio *folio) · line 526
Quoted 8 lines
mm/memremap.c +6
-1+ + 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);
Thanks,
Balbir
Re: [v2 01/11] mm/zone_device: support large zone device private folios
In reply to David HildenbrandQuoted 4 lines · David Hildenbrand
I think I asked that already but maybe missed the reply: Should these folios ever be added to the deferred split queue and is there any value in splitting them under memory pressure in the shrinker?
My gut feeling is "No", because the buddy cannot make use of these folios, but maybe there is an interesting case where we want that behavior?
I realized I did not answer this
deferred_split() is the default action when partial unmaps take place. Anything that does folio_rmap_remove_ptes can cause the folio to be deferred split if it gets partially unmapped.
We can optimize for this later if needed
Balbir Singh
Re: [v2 01/11] mm/zone_device: support large zone device private folios
Quoted 4 lines · Balbir Singh
Quoted 2 lines · David Hildenbrand
I think I asked that already but maybe missed the reply: Should these folios ever be added to the deferred split queue and is there any value in splitting them under memory pressure in the shrinker?
Quoted 8 lines
Quoted 2 lines
My gut feeling is "No", because the buddy cannot make use of these folios, but maybe there is an interesting case where we want that behavior?
I realized I did not answer this
deferred_split()is the default action when partial unmaps take place. Anything that doesfolio_rmap_remove_ptescan cause the folio to be deferred split if it gets partially unmapped.
Right, but it's easy to exclude zone-device folios here. So the real question is: do you want to deal with deferred splits or not?
If not, then just disable it right from the start.
Signature · 3 lines
Cheers, David / dhildenb
Re: [v2 01/11] mm/zone_device: support large zone device private folios
Quoted 10 lines · David Hildenbrand
Quoted 9 lines · Balbir Singh
Quoted 4 lines · David Hildenbrand
I think I asked that already but maybe missed the reply: Should these folios ever be added to the deferred split queue and is there any value in splitting them under memory pressure in the shrinker?
My gut feeling is "No", because the buddy cannot make use of these folios, but maybe there is an interesting case where we want that behavior?
I realized I did not answer this
Quoted 8 lines
Quoted 3 lines
deferred_split()is the default action when partial unmaps take place. Anything that doesfolio_rmap_remove_ptescan cause the folio to be deferred split if it gets partially unmapped.Right, but it's easy to exclude zone-device folios here. So the real question is: do you want to deal with deferred splits or not?
If not, then just disable it right from the start.
I agree, I was trying to avoid special casing device private folios unless needed to the extent possible
Balbir
Re: [v2 01/11] mm/zone_device: support large zone device private folios
Quoted 15 lines · Balbir Singh
Quoted 14 lines · David Hildenbrand
Quoted 12 lines · Balbir Singh
Quoted 4 lines · David Hildenbrand
I think I asked that already but maybe missed the reply: Should these folios ever be added to the deferred split queue and is there any value in splitting them under memory pressure in the shrinker?
My gut feeling is "No", because the buddy cannot make use of these folios, but maybe there is an interesting case where we want that behavior?
I realized I did not answer this
deferred_split()is the default action when partial unmaps take place. Anything that doesfolio_rmap_remove_ptescan cause the folio to be deferred split if it gets partially unmapped.
Quoted 6 lines
Quoted 4 lines
Right, but it's easy to exclude zone-device folios here. So the real question is: do you want to deal with deferred splits or not?
If not, then just disable it right from the start.
I agree, I was trying to avoid special casing device private folios unless needed to the extent possible
By introducing a completely separate split logic :P
Jokes aside, we have plenty of zone_device special-casing already, no harm in adding one more folio_is_zone_device() there.
Deferred splitting is all weird already that you can call yourself fortunate if you don't have to mess with that for zone-device folios.
Again, unless there is a benefit in having it.
Signature · 3 lines
Cheers, David / dhildenb
Matthew Brost
reviewerRe: [v2 01/11] mm/zone_device: support large zone device private folios
Quoted 25 lines · David Hildenbrand
Quoted 15 lines · Balbir Singh
Quoted 14 lines · David Hildenbrand
Quoted 12 lines · Balbir Singh
Quoted 4 lines · David Hildenbrand
I think I asked that already but maybe missed the reply: Should these folios ever be added to the deferred split queue and is there any value in splitting them under memory pressure in the shrinker?
My gut feeling is "No", because the buddy cannot make use of these folios, but maybe there is an interesting case where we want that behavior?
I realized I did not answer this
deferred_split()is the default action when partial unmaps take place. Anything that doesfolio_rmap_remove_ptescan cause the folio to be deferred split if it gets partially unmapped.Quoted 6 lines
Quoted 4 lines
Right, but it's easy to exclude zone-device folios here. So the real question is: do you want to deal with deferred splits or not?
If not, then just disable it right from the start.
I agree, I was trying to avoid special casing device private folios unless needed to the extent possible
By introducing a completely separate split logic :P
Quoted 7 lines
Jokes aside, we have plenty of
zone_devicespecial-casing already, no harm in adding one morefolio_is_zone_device()there.Deferred splitting is all weird already that you can call yourself fortunate if you don't have to mess with that for zone-device folios.
Again, unless there is a benefit in having it.
+1 on no deferred split for device folios.
Matt
Quoted 6 lines
Signature · 4 lines
-- Cheers, David / dhildenb
Re: [v2 01/11] mm/zone_device: support large zone device private folios
Quoted 29 lines · Matthew Brost
Quoted 28 lines · David Hildenbrand
Quoted 15 lines · Balbir Singh
Quoted 14 lines · David Hildenbrand
Quoted 12 lines · Balbir Singh
Quoted 4 lines · David Hildenbrand
I think I asked that already but maybe missed the reply: Should these folios ever be added to the deferred split queue and is there any value in splitting them under memory pressure in the shrinker?
My gut feeling is "No", because the buddy cannot make use of these folios, but maybe there is an interesting case where we want that behavior?
I realized I did not answer this
deferred_split()is the default action when partial unmaps take place. Anything that doesfolio_rmap_remove_ptescan cause the folio to be deferred split if it gets partially unmapped.Quoted 6 lines
Quoted 4 lines
Right, but it's easy to exclude zone-device folios here. So the real question is: do you want to deal with deferred splits or not?
If not, then just disable it right from the start.
I agree, I was trying to avoid special casing device private folios unless needed to the extent possible
By introducing a completely separate split logic :P
Jokes aside, we have plenty of
zone_devicespecial-casing already, no harm in adding one morefolio_is_zone_device()there.
Quoted 8 lines
Quoted 4 lines
Deferred splitting is all weird already that you can call yourself fortunate if you don't have to mess with that for zone-device folios.
Again, unless there is a benefit in having it.
+1 on no deferred split for device folios.
I'll add it to v3 to check that we do not do deferred splits on zone device folios
Balbir
PATCH v2 2/11 mm/thp: zone_device awareness in THP handling code
In reply to Balbir SinghMake THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to HPAGE_PMD_NR.
Modify page_vma_mapped_walk() to return true when a zone device huge entry is present, enabling try_to_migrate() and other code migration paths to appropriately process the entry. page_vma_mapped_walk() will return true for zone device private large folios only when PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag is try_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn() does not work well with zone device entries, use pfn_pmd_entry_to_swap() for checking and comparison as for zone device entries.
Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but calling folio_split() will cause lock recursion, the __split_unmapped_folio() code is used with a new helper to wrap the code split_device_private_folio(), which skips the checks around folio->mapping, swapcache and the need to go through unmap and remap folio.
--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)
include/linux/huge_mm.h +1 -0
@@ -345,6 +345,7 @@ unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addbool 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_device_private_folio (struct folio *folio);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,
include/linux/rmap.h +2 -0
@@ -905,6 +905,8 @@ struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr,#define PVMW_SYNC (1 << 0)/* Look for migration entries rather than present PTEs */ #define PVMW_MIGRATION (1 << 1) +/* Look for device private THP entries */ +#define PVMW_THP_DEVICE_PRIVATE (1 << 2)struct page_vma_mapped_walk {unsigned long pfn;
include/linux/swapops.h +17 -0
@@ -563,6 +563,7 @@ static inline int is_pmd_migration_entry(pmd_t pmd) {return is_swap_pmd (pmd) &&is_migration_entry (pmd_to_swp_entry (pmd)); } +#else /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ static inline int set_pmd_migration_entry (struct page_vma_mapped_walk *pvmw,struct page *page)
@@ -594,6 +595,22 @@ 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 ) + +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 ;
mm/huge_memory.c +222 -46
393 lines not on this page · raw message
mm/page_vma_mapped.c +11 -2
@@ -250,12 +250,11 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw) pvmw->ptl =pmd_lock (mm, pvmw->pmd); pmde = *pvmw->pmd;if (!pmd_present (pmde)) { -swp_entry_t entry; +swp_entry_t entry =pmd_to_swp_entry (pmde);if (!thp_migration_supported () || !(pvmw->flags &PVMW_MIGRATION ))return not_found (pvmw); - entry =pmd_to_swp_entry (pmde);if (!is_migration_entry (entry) || !check_pmd (swp_offset_pfn (entry), pvmw))return not_found (pvmw);
@@ -277,6 +276,16 @@ 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; + + entry =pmd_to_swp_entry (pmde); + +if (is_device_private_entry (entry) && + (pvmw->flags &PVMW_THP_DEVICE_PRIVATE )) { + pvmw->ptl =pmd_lock (mm, pvmw->pmd); +return true; + } + if ((pvmw->flags & PVMW_SYNC) && thp_vma_suitable_order(vma, pvmw->address, PMD_ORDER) &&
mm/pgtable-generic.c +6 -0
@@ -292,6 +292,12 @@ pte_t *___pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp) *pmdvalp = pmdval;if (unlikely (pmd_none (pmdval) ||is_pmd_migration_entry (pmdval))) goto nomap; +if (is_swap_pmd (pmdval)) { +swp_entry_t entry =pmd_to_swp_entry (pmdval); + +if (is_device_private_entry (entry)) + goto nomap; + }if (unlikely (pmd_trans_huge (pmdval))) goto nomap;if (unlikely (pmd_bad (pmdval))) {
mm/rmap.c +19 -3
@@ -2281,7 +2281,8 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma, unsigned long address, void *arg) { struct mm_struct *mm = vma->vm_mm; -DEFINE_FOLIO_VMA_WALK (pvmw, folio, vma, address,0 ); +DEFINE_FOLIO_VMA_WALK (pvmw, folio, vma, address, +PVMW_THP_DEVICE_PRIVATE );bool anon_exclusive, writable, ret = true;pte_t pteval;struct page *subpage;
@@ -2326,6 +2327,8 @@ 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) { +unsigned long pfn; +if (flags &TTU_SPLIT_HUGE_PMD ) {split_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, true);
@@ -2334,8 +2337,21 @@ 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)); +/* +* Zone device private folios do not work well with +* pmd_pfn() on some architectures due to pte +* inversion. +*/ +if (is_pmd_device_private_entry (*pvmw.pmd)) { +swp_entry_t entry =pmd_to_swp_entry (*pvmw.pmd); + + pfn =swp_offset_pfn (entry); + }else { + pfn =pmd_pfn (*pvmw.pmd); + } + + 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
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Hi,
Quoted 273 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)include/linux/huge_mm.h +1
-0@@ -345,6 +345,7 @@ unsigned long thp_get_unmapped_area_vmflags(struct file *filp, unsigned long addbool 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_device_private_folio (struct folio *folio);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,include/linux/rmap.h +2
-0@@ -905,6 +905,8 @@ struct page *make_device_exclusive(struct mm_struct *mm, unsigned long addr,#define PVMW_SYNC (1 << 0)/* Look for migration entries rather than present PTEs */ #define PVMW_MIGRATION (1 << 1) +/* Look for device private THP entries */ +#define PVMW_THP_DEVICE_PRIVATE (1 << 2)struct page_vma_mapped_walk {unsigned long pfn;include/linux/swapops.h +17
-0@@ -563,6 +563,7 @@ static inline int is_pmd_migration_entry(pmd_t pmd) {return is_swap_pmd (pmd) &&is_migration_entry (pmd_to_swp_entry (pmd)); } +#else /* CONFIG_ARCH_ENABLE_THP_MIGRATION */ static inline int set_pmd_migration_entry (struct page_vma_mapped_walk *pvmw,struct page *page)@@ -594,6 +595,22 @@ 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 ) + +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 ;mm/huge_memory.c +78
-7@@ -72,6 +72,10 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); +static int __split_unmapped_folio (struct folio *folio,int new_order, +struct page *split_at,struct xa_state *xas, +struct address_space *mapping,bool uniform_split); +static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -1711,8 +1715,11 @@ 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)) { +VM_WARN_ON (!is_pmd_migration_entry (pmd) && + !is_pmd_device_private_entry (pmd)); + +if (is_migration_entry (entry) && +is_writable_migration_entry (entry)) { entry =make_readable_migration_entry (swp_offset (entry)); pmd =swp_entry_to_pmd (entry);@@ -1722,6 +1729,32 @@ int copy_huge_pmd(struct mm_struct *dst_mm, struct mm_struct *src_mm, pmd =pmd_swp_mkuffd_wp (pmd);set_pmd_at (src_mm, addr, src_pmd, pmd); } + +if (is_device_private_entry (entry)) { +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 +* device private entries. +*/ +VM_WARN_ON (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);@@ -2219,15 +2252,22 @@ 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_migration_entry (orig_pmd) || +is_pmd_device_private_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 (is_pmd_device_private_entry (orig_pmd)) { +folio_remove_rmap_pmd (folio, &folio->page, vma); +WARN_ON_ONCE (folio_mapcount (folio) <0 ); + } + }if (folio_test_anon (folio)) {zap_deposited_table (tlb->mm, pmd);@@ -2247,6 +2287,15 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma,folio_mark_accessed (folio); } +/* +* Do a folio put on zone device private pages after +* changes to mm_counter, because the folio_put() will +* clean folio->mapping and the folio_test_anon() check +* will not be usable. +*/ +if (folio_is_device_private (folio)) +folio_put (folio); +spin_unlock (ptl);if (flush_needed)tlb_remove_page_size (tlb, &folio->page,HPAGE_PMD_SIZE );@@ -2375,7 +2424,8 @@ 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_migration_entry (*pmd) && + !folio_is_device_private (folio));if (is_writable_migration_entry (entry)) {/* * A protection check is difficult so @@ -2388,6 +2438,10 @@ 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; }@@ -2834,6 +2888,44 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } +/** +* split_huge_device_private_folio - split a huge device private folio into +* smaller pages (of order 0), currently used by migrate_device logic to +* split folios for pages that are partially mapped +* +* @folio: the folio to split +* +* The caller has to hold the folio_lock and a reference via folio_get +*/ +int split_device_private_folio (struct folio *folio) +{ +struct folio *end_folio =folio_next (folio); +struct folio *new_folio; +int ret =0 ; + +/* +* Split the folio now. In the case of device
mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2917
Quoted 10 lines
mm/huge_memory.c +10
-0+* private pages, this path is executed when +* the pmd is split and since freeze is not true +* it is likely the folio will be deferred_split. +* +* With device private pages, deferred splits of +* folios should be handled here to prevent partial +* unmaps from causing issues later on in migration +* and fault handling flows. +*/ +folio_ref_freeze (folio,1 +folio_expected_ref_count (folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2918
Quoted 1 line
mm/huge_memory.c +1
-0+ ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true);
Confusing to __split_unmapped_folio() if folio is mapped...
--Mika
Zi Yan
reviewerRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)
<snip>
Quoted 21 lines
Quoted 21 lines
mm/huge_memory.c +21
-0+/** +* split_huge_device_private_folio - split a huge device private folio into +* smaller pages (of order 0), currently used by migrate_device logic to +* split folios for pages that are partially mapped +* +* @folio: the folio to split +* +* The caller has to hold the folio_lock and a reference via folio_get +*/ +int split_device_private_folio (struct folio *folio) +{ +struct folio *end_folio =folio_next (folio); +struct folio *new_folio; +int ret =0 ; + +/* +* Split the folio now. In the case of device +* private pages, this path is executed when +* the pmd is split and since freeze is not true +* it is likely the folio will be deferred_split. +*
Quoted 8 lines
mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2917
Quoted 6 lines
mm/huge_memory.c +6
-0+* With device private pages, deferred splits of +* folios should be handled here to prevent partial +* unmaps from causing issues later on in migration +* and fault handling flows. +*/ +folio_ref_freeze (folio,1 +folio_expected_ref_count (folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device. folio_ref_freeeze() is not aware of device side mapping.
Quoted 4 lines
mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2918
Quoted 2 lines
mm/huge_memory.c +1
-0+ ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true);Confusing to
__split_unmapped_folio()if folio is mapped...
From driver point of view, __split_unmapped_folio() probably should be renamed to __split_cpu_unmapped_folio(), since it is only dealing with CPU side folio meta data for split.
Best Regards,
Yan, Zi
Zi Yan
reviewerRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 104 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 21 lines
Quoted 21 lines
mm/huge_memory.c +21
-0+/** +* split_huge_device_private_folio - split a huge device private folio into +* smaller pages (of order 0), currently used by migrate_device logic to +* split folios for pages that are partially mapped +* +* @folio: the folio to split +* +* The caller has to hold the folio_lock and a reference via folio_get +*/ +int split_device_private_folio (struct folio *folio) +{ +struct folio *end_folio =folio_next (folio); +struct folio *new_folio; +int ret =0 ; + +/* +* Split the folio now. In the case of device +* private pages, this path is executed when +* the pmd is split and since freeze is not true +* it is likely the folio will be deferred_split. +* Quoted 8 lines
mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2917
Quoted 6 lines
mm/huge_memory.c +6
-0+* With device private pages, deferred splits of +* folios should be handled here to prevent partial +* unmaps from causing issues later on in migration +* and fault handling flows. +*/ +folio_ref_freeze (folio,1 +folio_expected_ref_count (folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Quoted 3 lines
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Quoted 6 lines
Quoted 5 lines
mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2918
Quoted 3 lines
mm/huge_memory.c +1
-0+ ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true);Confusing to
__split_unmapped_folio()if folio is mapped...
Quoted 7 lines
From driver point of view,
__split_unmapped_folio()probably should be renamed to__split_cpu_unmapped_folio(), since it is only dealing with CPU side folio meta data for split.Best Regards,
Yan, Zi
Best Regards,
Yan, Zi
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 103 lines · Zi Yan
Quoted 101 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 28 lines
Quoted 17 lines
mm/huge_memory.c +17
-0+/** +* split_huge_device_private_folio - split a huge device private folio into +* smaller pages (of order 0), currently used by migrate_device logic to +* split folios for pages that are partially mapped +* +* @folio: the folio to split +* +* The caller has to hold the folio_lock and a reference via folio_get +*/ +int split_device_private_folio (struct folio *folio) +{ +struct folio *end_folio =folio_next (folio); +struct folio *new_folio; +int ret =0 ; + +/* +* Split the folio now. In the case of device mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2917
Quoted 10 lines
mm/huge_memory.c +10
-0+* private pages, this path is executed when +* the pmd is split and since freeze is not true +* it is likely the folio will be deferred_split. +* +* With device private pages, deferred splits of +* folios should be handled here to prevent partial +* unmaps from causing issues later on in migration +* and fault handling flows. +*/ +folio_ref_freeze (folio,1 +folio_expected_ref_count (folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Quoted 7 lines
Quoted 3 lines
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Ah ok this was about device private page obviously here, nevermind..
Quoted 13 lines
Quoted 9 lines
Quoted 2 lines
mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2918
Quoted 1 line
mm/huge_memory.c +1
-0+ ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true);Confusing to
__split_unmapped_folio()if folio is mapped...From driver point of view,
__split_unmapped_folio()probably should be renamed to__split_cpu_unmapped_folio(), since it is only dealing with CPU side folio meta data for split.Best Regards,
Yan, ZiBest Regards,
Yan, Zi
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 104 lines · Mika Penttilä
Quoted 103 lines · Zi Yan
Quoted 101 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 28 lines
Quoted 17 lines
mm/huge_memory.c +17
-0+/** +* split_huge_device_private_folio - split a huge device private folio into +* smaller pages (of order 0), currently used by migrate_device logic to +* split folios for pages that are partially mapped +* +* @folio: the folio to split +* +* The caller has to hold the folio_lock and a reference via folio_get +*/ +int split_device_private_folio (struct folio *folio) +{ +struct folio *end_folio =folio_next (folio); +struct folio *new_folio; +int ret =0 ; + +/* +* Split the folio now. In the case of device mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2917
Quoted 10 lines
mm/huge_memory.c +10
-0+* private pages, this path is executed when +* the pmd is split and since freeze is not true +* it is likely the folio will be deferred_split. +* +* With device private pages, deferred splits of +* folios should be handled here to prevent partial +* unmaps from causing issues later on in migration +* and fault handling flows. +*/ +folio_ref_freeze (folio,1 +folio_expected_ref_count (folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from split_huge_pmd() paths and folio is mapped to CPU page tables as a huge device page by one or more task?
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 3 lines
mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2918
Quoted 2 lines
mm/huge_memory.c +1
-0+ ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true);Confusing to
__split_unmapped_folio()if folio is mapped...From driver point of view,
__split_unmapped_folio()probably should be renamed to__split_cpu_unmapped_folio(), since it is only dealing with CPU side folio meta data for split.
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Best Regards,
Yan, ZiBest Regards,
Yan, Zi
--Mika
Zi Yan
reviewerRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 20 lines
Quoted 20 lines
mm/huge_memory.c +20
-0+/** +* split_huge_device_private_folio - split a huge device private folio into +* smaller pages (of order 0), currently used by migrate_device logic to +* split folios for pages that are partially mapped +* +* @folio: the folio to split +* +* The caller has to hold the folio_lock and a reference via folio_get +*/ +int split_device_private_folio (struct folio *folio) +{ +struct folio *end_folio =folio_next (folio); +struct folio *new_folio; +int ret =0 ; + +/* +* Split the folio now. In the case of device +* private pages, this path is executed when +* the pmd is split and since freeze is not true +* it is likely the folio will be deferred_split. Quoted 8 lines
mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2917
Quoted 7 lines
mm/huge_memory.c +7
-0+* +* With device private pages, deferred splits of +* folios should be handled here to prevent partial +* unmaps from causing issues later on in migration +* and fault handling flows. +*/ +folio_ref_freeze (folio,1 +folio_expected_ref_count (folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.
Quoted 7 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?
The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The unmap_folio() used by __folio_split() unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.
The migration entry is an invalid CPU page table entry, so it is not a CPU mapping, IIUC.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 4 lines
mm/huge_memory.c static void __split_huge_zero_page_pmd(struct vm_area_struct *vma, · line 2918
Quoted 3 lines
mm/huge_memory.c +1
-0+ ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true);Confusing to
__split_unmapped_folio()if folio is mapped...From driver point of view,
__split_unmapped_folio()probably should be renamed to__split_cpu_unmapped_folio(), since it is only dealing with CPU side folio meta data for split.
Quoted 7 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Best Regards,
Yan, ZiBest Regards,
Yan, Zi--Mika
Best Regards,
Yan, Zi
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 114 lines · Zi Yan
Quoted 112 lines · Mika Penttilä
Quoted 111 lines · Mika Penttilä
Quoted 110 lines · Zi Yan
Quoted 101 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 20 lines
Quoted 20 lines
(quoted diff) +20
-0+/** + * split_huge_device_private_folio - split a huge device private folio into + * smaller pages (of order 0), currently used by migrate_device logic to + * split folios for pages that are partially mapped + * + * @folio: the folio to split + * + * The caller has to hold the folio_lock and a reference via folio_get + */ +int split_device_private_folio(struct folio *folio) +{ + struct folio *end_folio = folio_next(folio); + struct folio *new_folio; + int ret = 0; + + /* + * Split the folio now. In the case of device + * private pages, this path is executed when + * the pmd is split and since freeze is not true + * it is likely the folio will be deferred_split.Quoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * + * With device private pages, deferred splits of + * folios should be handled here to prevent partial + * unmaps from causing issues later on in migration + * and fault handling flows. + */ + folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Quoted 3 lines
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Quoted 8 lines
Quoted 2 lines
Quoted 1 line
Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio() is called for device private entry, not migrate entry afaics. And it is called from split_huge_pmd() with freeze == false, not from folio split but pmd split.
Quoted 11 lines
mapping, IIUC.
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
Quoted 2 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ ret = __split_unmapped_folio(folio, 0, &folio->page, NULL, NULL, true);Confusing to
__split_unmapped_folio()if folio is mapped...From driver point of view,
__split_unmapped_folio()probably should be renamed to__split_cpu_unmapped_folio(), since it is only dealing with CPU side folio meta data for split.Best Regards,
Yan, Zi
Quoted 8 lines
Quoted 4 lines
Quoted 3 lines
Quoted 3 lines
Best Regards,
Yan, Zi--Mika
Best Regards,
Yan, Zi
--Mika
Zi Yan
reviewerRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 117 lines · Mika Penttilä
Quoted 116 lines · Zi Yan
Quoted 114 lines · Mika Penttilä
Quoted 104 lines · Mika Penttilä
Quoted 103 lines · Zi Yan
Quoted 101 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 28 lines
Quoted 17 lines
(quoted diff) +17
-0+/** + * split_huge_device_private_folio - split a huge device private folio into + * smaller pages (of order 0), currently used by migrate_device logic to + * split folios for pages that are partially mapped + * + * @folio: the folio to split + * + * The caller has to hold the folio_lock and a reference via folio_get + */ +int split_device_private_folio(struct folio *folio) +{ + struct folio *end_folio = folio_next(folio); + struct folio *new_folio; + int ret = 0; + + /* + * Split the folio now. In the case of deviceReview
Quoted 10 lines
(quoted diff) +10
-0+ * private pages, this path is executed when + * the pmd is split and since freeze is not true + * it is likely the folio will be deferred_split. + * + * With device private pages, deferred splits of + * folios should be handled here to prevent partial + * unmaps from causing issues later on in migration + * and fault handling flows. + */ + folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?
Quoted 8 lines
Quoted 6 lines
The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.
Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Quoted 1 line
And it is called from
split_huge_pmd()with freeze == false, not from folio split but pmd split.
I am not sure that is the right timing of splitting a folio. The device private folio can be kept without splitting at split_huge_pmd() time.
But from CPU perspective, a device private folio has no CPU mapping, no other CPU can access or manipulate the folio. It should be OK to split it.
Quoted 15 lines
Quoted 15 lines
mapping, IIUC.
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
Quoted 2 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ ret = __split_unmapped_folio(folio, 0, &folio->page, NULL, NULL, true);Confusing to
__split_unmapped_folio()if folio is mapped...From driver point of view,
__split_unmapped_folio()probably should be renamed to__split_cpu_unmapped_folio(), since it is only dealing with CPU side folio meta data for split.Best Regards,
Yan, ZiBest Regards,
Yan, Zi
Quoted 6 lines
Quoted 5 lines
Quoted 1 line
--Mika
Best Regards,
Yan, Zi--Mika
Best Regards,
Yan, Zi
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 20 lines
Quoted 20 lines
(quoted diff) +20
-0+/** + * split_huge_device_private_folio - split a huge device private folio into + * smaller pages (of order 0), currently used by migrate_device logic to + * split folios for pages that are partially mapped + * + * @folio: the folio to split + * + * The caller has to hold the folio_lock and a reference via folio_get + */ +int split_device_private_folio(struct folio *folio) +{ + struct folio *end_folio = folio_next(folio); + struct folio *new_folio; + int ret = 0; + + /* + * Split the folio now. In the case of device + * private pages, this path is executed when + * the pmd is split and since freeze is not true + * it is likely the folio will be deferred_split.Quoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * + * With device private pages, deferred splits of + * folios should be handled here to prevent partial + * unmaps from causing issues later on in migration + * and fault handling flows. + */ + folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Also which might confuse is that v1 of the series had only
migrate_vma_split_pages()
which operated only on truly unmapped (mapcount wise) folios. Which was a motivation for split_unmapped_folio().. Now,
split_device_private_folio()
operates on mapcount != 0 folios.
Quoted 5 lines
Quoted 3 lines
And it is called from
split_huge_pmd()with freeze == false, not from folio split but pmd split.I am not sure that is the right timing of splitting a folio. The device private folio can be kept without splitting at
split_huge_pmd()time.
Yes this doesn't look quite right, and also
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
looks suspicious
Maybe split_device_private_folio() tries to solve some corner case but maybe good to elaborate more the exact conditions, there might be a better fix.
Quoted 18 lines
But from CPU perspective, a device private folio has no CPU mapping, no other CPU can access or manipulate the folio. It should be OK to split it.
Quoted 14 lines
Quoted 14 lines
mapping, IIUC.
Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
Quoted 2 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ ret = __split_unmapped_folio(folio, 0, &folio->page, NULL, NULL, true);Confusing to
__split_unmapped_folio()if folio is mapped...From driver point of view,
__split_unmapped_folio()probably should be renamed to__split_cpu_unmapped_folio(), since it is only dealing with CPU side folio meta data for split.Best Regards,
Yan, ZiBest Regards,
Yan, Zi
Quoted 9 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line
--Mika
Best Regards,
Yan, Zi--Mika
Best Regards,
Yan, Zi
--Mika
Zi Yan
reviewerRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 20 lines
Quoted 20 lines
(quoted diff) +20
-0+/** + * split_huge_device_private_folio - split a huge device private folio into + * smaller pages (of order 0), currently used by migrate_device logic to + * split folios for pages that are partially mapped + * + * @folio: the folio to split + * + * The caller has to hold the folio_lock and a reference via folio_get + */ +int split_device_private_folio(struct folio *folio) +{ + struct folio *end_folio = folio_next(folio); + struct folio *new_folio; + int ret = 0; + + /* + * Split the folio now. In the case of device + * private pages, this path is executed when + * the pmd is split and since freeze is not true + * it is likely the folio will be deferred_split.Quoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * + * With device private pages, deferred splits of + * folios should be handled here to prevent partial + * unmaps from causing issues later on in migration + * and fault handling flows. + */ + folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in __folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.
Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Quoted 15 lines
Also which might confuse is that v1 of the series had only
migrate_vma_split_pages()
which operated only on truly unmapped (mapcount wise) folios. Which was a motivation forsplit_unmapped_folio().. Now,
split_device_private_folio()
operates on mapcount != 0 folios.Quoted 5 lines
Quoted 3 lines
And it is called from
split_huge_pmd()with freeze == false, not from folio split but pmd split.I am not sure that is the right timing of splitting a folio. The device private folio can be kept without splitting at
split_huge_pmd()time.Yes this doesn't look quite right, and also
Review
Quoted 1 line
(quoted diff) +1
-0+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
I wonder if we need to freeze a device private folio. Can anyone other than device driver change its refcount? Since CPU just sees it as an unmapped folio.
Quoted 17 lines
looks suspicious
Maybe
split_device_private_folio()tries to solve some corner case but maybe good to elaborate more the exact conditions, there might be a better fix.Quoted 11 lines
But from CPU perspective, a device private folio has no CPU mapping, no other CPU can access or manipulate the folio. It should be OK to split it.
Quoted 7 lines
Quoted 7 lines
mapping, IIUC.
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
Quoted 2 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ ret = __split_unmapped_folio(folio, 0, &folio->page, NULL, NULL, true);Confusing to
__split_unmapped_folio()if folio is mapped...From driver point of view,
__split_unmapped_folio()probably should be renamed to__split_cpu_unmapped_folio(), since it is only dealing with CPU side folio meta data for split.
Best Regards,
Yan, Zi
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 20 lines
Quoted 20 lines
(quoted diff) +20
-0+/** + * split_huge_device_private_folio - split a huge device private folio into + * smaller pages (of order 0), currently used by migrate_device logic to + * split folios for pages that are partially mapped + * + * @folio: the folio to split + * + * The caller has to hold the folio_lock and a reference via folio_get + */ +int split_device_private_folio(struct folio *folio) +{ + struct folio *end_folio = folio_next(folio); + struct folio *new_folio; + int ret = 0; + + /* + * Split the folio now. In the case of device + * private pages, this path is executed when + * the pmd is split and since freeze is not true + * it is likely the folio will be deferred_split.Quoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * + * With device private pages, deferred splits of + * folios should be handled here to prevent partial + * unmaps from causing issues later on in migration + * and fault handling flows. + */ + folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?
Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.
Quoted 7 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Quoted 28 lines
Review
Quoted 13 lines
Also which might confuse is that v1 of the series had only
migrate_vma_split_pages()
which operated only on truly unmapped (mapcount wise) folios. Which was a motivation forsplit_unmapped_folio().. Now,
split_device_private_folio()
operates on mapcount != 0 folios.Quoted 4 lines
Quoted 2 lines
And it is called from
split_huge_pmd()with freeze == false, not from folio split but pmd split.I am not sure that is the right timing of splitting a folio. The device private folio can be kept without splitting at
split_huge_pmd()time.Yes this doesn't look quite right, and also
(quoted diff) +1
-0+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));I wonder if we need to freeze a device private folio. Can anyone other than device driver change its refcount? Since CPU just sees it as an unmapped folio.
Quoted 12 lines
looks suspicious
Maybe
split_device_private_folio()tries to solve some corner case but maybe good to elaborate more the exact conditions, there might be a better fix.Quoted 7 lines
But from CPU perspective, a device private folio has no CPU mapping, no other CPU can access or manipulate the folio. It should be OK to split it.
Quoted 4 lines
Quoted 4 lines
mapping, IIUC.
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Review
Quoted 1 line
(quoted diff) +1
-0+ ret = __split_unmapped_folio(folio, 0, &folio->page, NULL, NULL, true);Confusing to
__split_unmapped_folio()if folio is mapped...
Quoted 8 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
From driver point of view,
__split_unmapped_folio()probably should be renamed to__split_cpu_unmapped_folio(), since it is only dealing with CPU side folio meta data for split.Best Regards,
Yan, Zi
--Mika
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 152 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone device pages. Although the code is designed to be generic when it comes to handling splitting of pages, the code is designed to work for THP page sizes corresponding to
HPAGE_PMD_NR.Modify
page_vma_mapped_walk()to return true when a zone device huge entry is present, enablingtry_to_migrate()and other code migration paths to appropriately process the entry.page_vma_mapped_walk()will return true for zone device private large folios only whenPVMW_THP_DEVICE_PRIVATEis passed. This is to prevent locations that are not zone device private pages from having to add awareness. The key callback that needs this flag istry_to_migrate_one(). The other callbacks page idle, damon use it for setting young/dirty bits, which is not significant when it comes to pmd level bit harvesting.
pmd_pfn()does not work well with zone device entries, usepfn_pmd_entry_to_swap()for checking and comparison as for zone device entries.Zone device private entries when split via munmap go through pmd split, but need to go through a folio split, deferred split does not work if a fault is encountered because fault handling involves migration entries (via
folio_migrate_mapping) and the folio sizes are expected to be the same there. This introduces the need to split the folio while handling the pmd split. Because the folio is still mapped, but callingfolio_split()will cause lock recursion, the__split_unmapped_folio()code is used with a new helper to wrap the codesplit_device_private_folio(), which skips the checks aroundfolio->mapping, swapcache and the need to go through unmap and remap folio.Cc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>Cc Ralph Campbell <[email protected]>Signed-off-by Matthew Brost <[email protected]>Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 +++ mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++------- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +++- 7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 20 lines
Quoted 20 lines
(quoted diff) +20
-0+/** + * split_huge_device_private_folio - split a huge device private folio into + * smaller pages (of order 0), currently used by migrate_device logic to + * split folios for pages that are partially mapped + * + * @folio: the folio to split + * + * The caller has to hold the folio_lock and a reference via folio_get + */ +int split_device_private_folio(struct folio *folio) +{ + struct folio *end_folio = folio_next(folio); + struct folio *new_folio; + int ret = 0; + + /* + * Split the folio now. In the case of device + * private pages, this path is executed when + * the pmd is split and since freeze is not true + * it is likely the folio will be deferred_split.Quoted 8 lines
Review
Quoted 7 lines
(quoted diff) +7
-0+ * + * With device private pages, deferred splits of + * folios should be handled here to prevent partial + * unmaps from causing issues later on in migration + * and fault handling flows. + */ + folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.
Maybe device-private could just be PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...
So consider device-private entries just like PROT_NONE present page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.
Signature · 3 lines
Cheers, David / dhildenb
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 159 lines · David Hildenbrand
Quoted 152 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 20 lines
Quoted 20 lines
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.Quoted 8 lines
Quoted 7 lines
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Quoted 9 lines
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.
Thanks for clarifying on my behalf, just catching up with the discussion
When I was referring to mapped with Zi, I was talking about how touching the entry will cause a fault and migration back and they can be considered as unmapped in that sense, because they are mapped to the device. Device private entries are mapped into the page tables and have a refcount associated with the page/folio that represents the device entries.
Balbir Singh
Zi Yan
reviewerRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
In reply to David HildenbrandQuoted 159 lines · David Hildenbrand
Quoted 152 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
Quoted 66 lines · Balbir Singh
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 20 lines
Quoted 20 lines
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.Quoted 8 lines
Quoted 7 lines
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Quoted 8 lines
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.
Thanks for the clarification.
So folio_mapcount() for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through __folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?
Best Regards,
Yan, Zi
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 170 lines · Zi Yan
Quoted 159 lines · David Hildenbrand
Quoted 152 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 28 lines
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Quoted 8 lines
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.
Quoted 7 lines
Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?
That is what I would expect: Replace device-private by migration entries, perform the migration/split/whatever, restore migration entries to device-private entries.
That will drive the mapcount to 0.
Signature · 3 lines
Cheers, David / dhildenb
Zi Yan
reviewerRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 179 lines · David Hildenbrand
Quoted 170 lines · Zi Yan
Quoted 159 lines · David Hildenbrand
Quoted 152 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Quoted 8 lines
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Quoted 7 lines
Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?
Quoted 3 lines
That is what I would expect: Replace device-private by migration entries, perform the migration/split/whatever, restore migration entries to device-private entries.
That will drive the mapcount to 0.
Great. That matches my expectations as well. One potential optimization could be since device private entry is already CPU inaccessible TLB flush can be avoided.
Best Regards,
Yan, Zi
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 181 lines · Zi Yan
Quoted 179 lines · David Hildenbrand
Quoted 177 lines · Zi Yan
Quoted 159 lines · David Hildenbrand
Quoted 152 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
On 30 Jul 2025, at 7:27, Zi Yan wrote:
On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Quoted 8 lines
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?
Quoted 7 lines
Quoted 3 lines
That is what I would expect: Replace device-private by migration entries, perform the migration/split/whatever, restore migration entries to device-private entries.
That will drive the mapcount to 0.
Great. That matches my expectations as well. One potential optimization could be since device private entry is already CPU inaccessible TLB flush can be avoided.
Right, I would assume that is already done, or could easily be added.
Not using proper migration entries sounds like a hack that we shouldn't start with. We should start with as little special cases as possible in core-mm.
For example, as you probably implied, there is nothing stopping concurrent fork() or zap to mess with the refcount+mapcount.
Signature · 3 lines
Cheers, David / dhildenb
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
In reply to Zi YanQuoted 170 lines · Zi Yan
Quoted 159 lines · David Hildenbrand
Quoted 152 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
Quoted 69 lines · Mika Penttilä
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
Quoted 28 lines
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?Based on my off-list conversation with Balbir, the folio is unmapped in CPU side but mapped in the device.
folio_ref_freeeze()is not aware of device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Quoted 8 lines
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.
Quoted 8 lines
Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?
That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Balbir Singh
Zi Yan
reviewerRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 180 lines · Balbir Singh
Quoted 170 lines · Zi Yan
Quoted 159 lines · David Hildenbrand
Quoted 152 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Quoted 8 lines
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Quoted 8 lines
Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?
Quoted 5 lines
That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
In this case, all device private entries pointing to this device private folio should be turned into migration entries and folio_mapcount() should be 0. The split_device_private_folio() is handling this situation, although the function name is not very descriptive. You might want to add a comment to this function about its use and add a check to make sure folio_mapcount() is 0.
Quoted 10 lines
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (
split_huge_pmd_locked)For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
For partial unmap case, you should be able to handle it in the same way as normal PTE-mapped large folio. Since like David said, each device private entry can be seen as a PROTNONE entry. At PMD split, PMD page table page should be filled with device private PTEs. Each of them points to the corresponding subpage. When device unmaps some of the PTEs, rmap code should take care of the folio_mapcount().
Best Regards,
Yan, Zi
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 206 lines · Zi Yan
Quoted 180 lines · Balbir Singh
Quoted 170 lines · Zi Yan
Quoted 159 lines · David Hildenbrand
Quoted 152 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
On 30 Jul 2025, at 7:27, Zi Yan wrote:
On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Quoted 8 lines
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Quoted 8 lines
Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?Quoted 5 lines
That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
In this case, all device private entries pointing to this device private folio should be turned into migration entries and
folio_mapcount()should be 0. Thesplit_device_private_folio()is handling this situation, although the function name is not very descriptive. You might want to add a comment to this function about its use and add a check to make surefolio_mapcount()is 0.Quoted 10 lines
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (
split_huge_pmd_locked)For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 6 lines
For partial unmap case, you should be able to handle it in the same way as normal PTE-mapped large folio. Since like David said, each device private entry can be seen as a PROTNONE entry. At PMD split, PMD page table page should be filled with device private PTEs. Each of them points to the corresponding subpage. When device unmaps some of the PTEs, rmap code should take care of the
folio_mapcount().
Right. In general, no splitting of any THP with a mapcount > 0 (folio_mapped()). It's a clear indication that you are doing something wrong.
Signature · 3 lines
Cheers, David / dhildenb
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
In reply to Balbir SinghHi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
Quoted 104 lines · Zi Yan
On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)
Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
1) is totally fine. This was in v1 and lead to Zi's split_unmapped_folio()
2) is a problem because folio is mapped. split_huge_pmd() can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:
folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
Also, didn't quite follow what kind of lock recursion did you encounter doing proper split_folio() instead?
Quoted 2 lines
Balbir Singh
--Mika
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 195 lines · Mika Penttilä
Hi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
Quoted 106 lines · Zi Yan
On 30 Jul 2025, at 7:27, Zi Yan wrote:
On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.Quoted 6 lines
Quoted 5 lines
Quoted 4 lines
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 7 lines
1) is totally fine. This was in v1 and lead to Zi's
split_unmapped_folio()2) is a problem because folio is mapped.
split_huge_pmd()can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
I can add checks to make sure that the call does succeed.
Quoted 2 lines
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
So after the partial unmap, the folio ends up in a little strange situation, the folio is large, but not mapped (since large_mapcount can be 0, after all the folio_rmap_remove_ptes). Calling folio_split() on partially unmapped fails because folio_get_anon_vma() fails due to the folio_mapped() failures related to folio_large_mapcount. There is also additional complexity with ref counts and mapping.
Quoted 4 lines
Also, didn't quite follow what kind of lock recursion did you encounter doing proper
split_folio()instead?
Splitting during partial unmap causes recursive locking issues with anon_vma when invoked from split_huge_pmd_locked() path. Deferred splits do not work for device private pages, due to the migration requirements for fault handling.
Balbir Singh
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 206 lines · Balbir Singh
Quoted 195 lines · Mika Penttilä
Hi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
On 7/30/25 14:30, Zi Yan wrote:
On 30 Jul 2025, at 7:27, Zi Yan wrote:On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.Quoted 6 lines
Quoted 5 lines
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Ah ok this was about device private page obviously here, nevermind..Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 7 lines
1) is totally fine. This was in v1 and lead to Zi's
split_unmapped_folio()2) is a problem because folio is mapped.
split_huge_pmd()can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));I can add checks to make sure that the call does succeed.
Quoted 8 lines
Quoted 2 lines
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
So after the partial unmap, the folio ends up in a little strange situation, the folio is large, but not mapped (since
large_mapcountcan be 0, after all thefolio_rmap_remove_ptes). Callingfolio_split()on partially unmapped fails becausefolio_get_anon_vma()fails due to thefolio_mapped()failures related tofolio_large_mapcount. There is also additional complexity with ref counts and mapping.
Let me get back to you on this with data, I was playing around with CONFIG_MM_IDS and might have different data from it.
Quoted 6 lines
Quoted 5 lines
Also, didn't quite follow what kind of lock recursion did you encounter doing proper
split_folio()instead?
Quoted 6 lines
Splitting during partial unmap causes recursive locking issues with
anon_vmawhen invoked fromsplit_huge_pmd_locked()path. Deferred splits do not work for device private pages, due to the migration requirements for fault handling.Balbir Singh
Balbir
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
In reply to Balbir SinghQuoted 202 lines · Balbir Singh
Quoted 194 lines · Mika Penttilä
Hi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
On 7/30/25 14:30, Zi Yan wrote:
On 30 Jul 2025, at 7:27, Zi Yan wrote:On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.Quoted 6 lines
Quoted 5 lines
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Ah ok this was about device private page obviously here, nevermind..Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 7 lines
1) is totally fine. This was in v1 and lead to Zi's
split_unmapped_folio()2) is a problem because folio is mapped.
split_huge_pmd()can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Quoted 8 lines
I can add checks to make sure that the call does succeed.
Quoted 2 lines
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
So after the partial unmap, the folio ends up in a little strange situation, the folio is large, but not mapped (since
large_mapcountcan be 0, after all thefolio_rmap_remove_ptes). Callingfolio_split()on partially unmapped fails becausefolio_get_anon_vma()fails due to thefolio_mapped()failures related tofolio_large_mapcount. There is also additional complexity with ref counts and mapping.
Is this after the deferred split -> map_unused_to_zeropage flow which would leave the page unmapped? Maybe disable that for device pages?
Quoted 11 lines
Quoted 6 lines
Also, didn't quite follow what kind of lock recursion did you encounter doing proper
split_folio()instead?Splitting during partial unmap causes recursive locking issues with
anon_vmawhen invoked fromsplit_huge_pmd_locked()path. Deferred splits do not work for device private pages, due to the migration requirements for fault handling.Balbir Singh
--Mika
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
In reply to Balbir SinghQuoted 206 lines · Balbir Singh
Quoted 195 lines · Mika Penttilä
Hi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 108 lines · Mika Penttilä
Quoted 107 lines · Mika Penttilä
On 7/30/25 14:30, Zi Yan wrote:
On 30 Jul 2025, at 7:27, Zi Yan wrote:On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.Quoted 6 lines
Quoted 5 lines
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Ah ok this was about device private page obviously here, nevermind..Still, isn't this reachable from
split_huge_pmd()paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 7 lines
1) is totally fine. This was in v1 and lead to Zi's
split_unmapped_folio()2) is a problem because folio is mapped.
split_huge_pmd()can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));I can add checks to make sure that the call does succeed.
Quoted 7 lines
Quoted 2 lines
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
So after the partial unmap, the folio ends up in a little strange situation, the folio is large, but not mapped (since
large_mapcountcan be 0, after all thefolio_rmap_remove_ptes). Callingfolio_split()on partially unmapped fails becausefolio_get_anon_vma()fails due to thefolio_mapped()failures related tofolio_large_mapcount. There is also additional complexity with ref counts and mapping.
I think you mean "Calling folio_split() on a *fully* unmapped folio fails ..."
A partially mapped folio still has folio_mapcount() > 0 -> folio_mapped() == true.
Quoted 9 lines
Quoted 6 lines
Also, didn't quite follow what kind of lock recursion did you encounter doing proper
split_folio()instead?Splitting during partial unmap causes recursive locking issues with
anon_vmawhen invoked fromsplit_huge_pmd_locked()path.
Yes, that's very complicated.
Quoted 2 lines
Deferred splits do not work for device private pages, due to the migration requirements for fault handling.
Can you elaborate on that?
Signature · 3 lines
Cheers, David / dhildenb
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 210 lines · David Hildenbrand
Quoted 209 lines · Balbir Singh
Quoted 195 lines · Mika Penttilä
Hi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
Quoted 114 lines · Mika Penttilä
On 7/30/25 14:42, Mika Penttilä wrote:
On 7/30/25 14:30, Zi Yan wrote:
On 30 Jul 2025, at 7:27, Zi Yan wrote:On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from split_huge_pmd() paths and folio is mapped to CPU page tables as a huge device page by one or more task?The folio only has migration entries pointing to it. From CPU perspective, it is not mapped. The
unmap_folio()used by__folio_split()unmaps a to-be-split folio by replacing existing page table entries with migration entries and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 7 lines
1) is totally fine. This was in v1 and lead to Zi's
split_unmapped_folio()2) is a problem because folio is mapped.
split_huge_pmd()can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:
folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));I can add checks to make sure that the call does succeed.
Quoted 2 lines
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
Quoted 9 lines
Quoted 4 lines
So after the partial unmap, the folio ends up in a little strange situation, the folio is large, but not mapped (since
large_mapcountcan be 0, after all thefolio_rmap_remove_ptes). Callingfolio_split()on partially unmapped fails becausefolio_get_anon_vma()fails due to thefolio_mapped()failures related tofolio_large_mapcount. There is also additional complexity with ref counts and mapping.I think you mean "Calling
folio_split()on a *fully* unmapped folio fails ..."A partially mapped folio still has
folio_mapcount()> 0 ->folio_mapped()== true.
Looking into this again at my end
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Also, didn't quite follow what kind of lock recursion did you encounter doing proper
split_folio()instead?
Quoted 5 lines
Quoted 2 lines
Splitting during partial unmap causes recursive locking issues with
anon_vmawhen invoked fromsplit_huge_pmd_locked()path.Yes, that's very complicated.
Yes and I want to avoid going down that path.
Quoted 5 lines
Quoted 2 lines
Deferred splits do not work for device private pages, due to the migration requirements for fault handling.
Can you elaborate on that?
If a folio is under deferred_split() and is still pending a split. When a fault is handled on a partially mapped folio, the expectation is that as a part of fault handling during migration, the code in migrate_folio_mapping() assumes that the folio sizes are the same (via check for reference and mapcount)
Balbir
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 241 lines · Balbir Singh
Quoted 210 lines · David Hildenbrand
Quoted 209 lines · Balbir Singh
Quoted 195 lines · Mika Penttilä
Hi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
Quoted 121 lines · Zi Yan
On 30 Jul 2025, at 8:08, Mika Penttilä wrote:
On 7/30/25 14:42, Mika Penttilä wrote:
On 7/30/25 14:30, Zi Yan wrote:
On 30 Jul 2025, at 7:27, Zi Yan wrote:On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from split_huge_pmd() paths and folio is mapped to CPU page tables as a huge device page by one or more task?
The folio only has migration entries pointing to it. From CPU perspective,
it is not mapped. The unmap_folio() used by __folio_split() unmaps a to-be-split
folio by replacing existing page table entries with migration entries
and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
Quoted 1 line
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio()is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 7 lines
1) is totally fine. This was in v1 and lead to Zi's
split_unmapped_folio()2) is a problem because folio is mapped.
split_huge_pmd()can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:
folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));I can add checks to make sure that the call does succeed.
Quoted 2 lines
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
Quoted 9 lines
Quoted 4 lines
So after the partial unmap, the folio ends up in a little strange situation, the folio is large, but not mapped (since
large_mapcountcan be 0, after all thefolio_rmap_remove_ptes). Callingfolio_split()on partially unmapped fails becausefolio_get_anon_vma()fails due to thefolio_mapped()failures related tofolio_large_mapcount. There is also additional complexity with ref counts and mapping.I think you mean "Calling
folio_split()on a *fully* unmapped folio fails ..."A partially mapped folio still has
folio_mapcount()> 0 ->folio_mapped()== true.Looking into this again at my end
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Also, didn't quite follow what kind of lock recursion did you encounter doing proper
split_folio()instead?Quoted 5 lines
Quoted 2 lines
Splitting during partial unmap causes recursive locking issues with
anon_vmawhen invoked fromsplit_huge_pmd_locked()path.Yes, that's very complicated.
Yes and I want to avoid going down that path.
Quoted 3 lines
Quoted 2 lines
Deferred splits do not work for device private pages, due to the migration requirements for fault handling.
Quoted 6 lines
Quoted 2 lines
Can you elaborate on that?
If a folio is under
deferred_split()and is still pending a split. When a fault is handled on a partially mapped folio, the expectation is that as a part of fault handling during migration, the code inmigrate_folio_mapping()assumes that the folio sizes are the same (via check for reference and mapcount)
If you hit a partially-mapped folio, instead of migrating, you would actually want to split and then migrate I assume.
Signature · 3 lines
Cheers, David / dhildenb
Zi Yan
reviewerRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 242 lines · David Hildenbrand
Quoted 241 lines · Balbir Singh
Quoted 210 lines · David Hildenbrand
Quoted 209 lines · Balbir Singh
Quoted 195 lines · Mika Penttilä
Hi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
Quoted 122 lines · Mika Penttilä
On 7/30/25 15:25, Zi Yan wrote:
On 30 Jul 2025, at 8:08, Mika Penttilä wrote:On 7/30/25 14:42, Mika Penttilä wrote:
On 7/30/25 14:30, Zi Yan wrote:
On 30 Jul 2025, at 7:27, Zi Yan wrote:On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from split_huge_pmd() paths and folio is mapped to CPU page tables as a huge device page by one or more task?
The folio only has migration entries pointing to it. From CPU perspective,
it is not mapped. The unmap_folio() used by __folio_split() unmaps a to-be-split
folio by replacing existing page table entries with migration entries
and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
Quoted 2 lines
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio() is called for device private entry, not migrate entry afaics.Yes, but from CPU perspective, both device private entry and migration entry are invalid CPU page table entries, so the device private folio is “unmapped” at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 7 lines
1) is totally fine. This was in v1 and lead to Zi's
split_unmapped_folio()2) is a problem because folio is mapped.
split_huge_pmd()can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:
folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));I can add checks to make sure that the call does succeed.
Quoted 2 lines
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
Quoted 9 lines
Quoted 4 lines
So after the partial unmap, the folio ends up in a little strange situation, the folio is large, but not mapped (since
large_mapcountcan be 0, after all thefolio_rmap_remove_ptes). Callingfolio_split()on partially unmapped fails becausefolio_get_anon_vma()fails due to thefolio_mapped()failures related tofolio_large_mapcount. There is also additional complexity with ref counts and mapping.I think you mean "Calling
folio_split()on a *fully* unmapped folio fails ..."A partially mapped folio still has
folio_mapcount()> 0 ->folio_mapped()== true.Looking into this again at my end
Quoted 7 lines
Quoted 7 lines
Quoted 6 lines
Also, didn't quite follow what kind of lock recursion did you encounter doing proper
split_folio()instead?Quoted 5 lines
Quoted 2 lines
Splitting during partial unmap causes recursive locking issues with
anon_vmawhen invoked fromsplit_huge_pmd_locked()path.Yes, that's very complicated.
Yes and I want to avoid going down that path.
Quoted 3 lines
Quoted 2 lines
Deferred splits do not work for device private pages, due to the migration requirements for fault handling.
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
Can you elaborate on that?
If a folio is under
deferred_split()and is still pending a split. When a fault is handled on a partially mapped folio, the expectation is that as a part of fault handling during migration, the code inmigrate_folio_mapping()assumes that the folio sizes are the same (via check for reference and mapcount)If you hit a partially-mapped folio, instead of migrating, you would actually want to split and then migrate I assume.
Yes, that is exactly what migrate_pages() does. And if split fails, the migration fails too. Device private folio probably should do the same thing, assuming splitting device private folio would always succeed.
Best Regards,
Yan, Zi
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 236 lines · Zi Yan
Quoted 234 lines · David Hildenbrand
Quoted 233 lines · Balbir Singh
Quoted 207 lines · David Hildenbrand
Quoted 206 lines · Balbir Singh
Quoted 194 lines · Mika Penttilä
Hi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 125 lines · Mika Penttilä
Quoted 124 lines · Zi Yan
On 30 Jul 2025, at 8:49, Mika Penttilä wrote:
On 7/30/25 15:25, Zi Yan wrote:
On 30 Jul 2025, at 8:08, Mika Penttilä wrote:On 7/30/25 14:42, Mika Penttilä wrote:
On 7/30/25 14:30, Zi Yan wrote:
On 30 Jul 2025, at 7:27, Zi Yan wrote:On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from split_huge_pmd() paths and folio is mapped to CPU page tables as a huge device page by one or more task?
The folio only has migration entries pointing to it. From CPU perspective,
it is not mapped. The unmap_folio() used by __folio_split() unmaps a to-be-split
folio by replacing existing page table entries with migration entries
and after that the folio is regarded as “unmapped”.Quoted 6 lines
Quoted 5 lines
The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio() is called for device private entry, not migrate entry afaics.
Yes, but from CPU perspective, both device private entry and migration entry
are invalid CPU page table entries, so the device private folio is “unmapped”
at CPU side.Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 7 lines
1) is totally fine. This was in v1 and lead to Zi's
split_unmapped_folio()2) is a problem because folio is mapped.
split_huge_pmd()can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:
folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));I can add checks to make sure that the call does succeed.
Quoted 2 lines
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
Quoted 8 lines
Quoted 4 lines
So after the partial unmap, the folio ends up in a little strange situation, the folio is large, but not mapped (since
large_mapcountcan be 0, after all thefolio_rmap_remove_ptes). Callingfolio_split()on partially unmapped fails becausefolio_get_anon_vma()fails due to thefolio_mapped()failures related tofolio_large_mapcount. There is also additional complexity with ref counts and mapping.I think you mean "Calling
folio_split()on a *fully* unmapped folio fails ..."A partially mapped folio still has
folio_mapcount()> 0 ->folio_mapped()== true.Looking into this again at my end
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Also, didn't quite follow what kind of lock recursion did you encounter doing proper
split_folio()instead?Splitting during partial unmap causes recursive locking issues with
anon_vmawhen invoked fromsplit_huge_pmd_locked()path.Yes, that's very complicated.
Yes and I want to avoid going down that path.
Quoted 4 lines
Quoted 2 lines
Deferred splits do not work for device private pages, due to the migration requirements for fault handling.
Can you elaborate on that?
Quoted 7 lines
Quoted 4 lines
Quoted 3 lines
If a folio is under
deferred_split()and is still pending a split. When a fault is handled on a partially mapped folio, the expectation is that as a part of fault handling during migration, the code inmigrate_folio_mapping()assumes that the folio sizes are the same (via check for reference and mapcount)If you hit a partially-mapped folio, instead of migrating, you would actually want to split and then migrate I assume.
Yes, that is exactly what
migrate_pages()does. And if split fails, the migration fails too. Device private folio probably should do the same thing, assuming splitting device private folio would always succeed.
hmm afaics the normal folio_split wants to use RMP_USE_SHARED_ZEROPAGE when splitting and remapping device private pages, that can't work..
Quoted 4 lines
Best Regards,
Yan, Zi
--Mika
Zi Yan
reviewerRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 240 lines · Mika Penttilä
Quoted 239 lines · Zi Yan
Quoted 237 lines · David Hildenbrand
Quoted 236 lines · Balbir Singh
Quoted 207 lines · David Hildenbrand
Quoted 206 lines · Balbir Singh
Quoted 194 lines · Mika Penttilä
Hi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 133 lines · Zi Yan
Quoted 131 lines · Mika Penttilä
On 7/30/25 18:10, Zi Yan wrote:
On 30 Jul 2025, at 8:49, Mika Penttilä wrote:On 7/30/25 15:25, Zi Yan wrote:
On 30 Jul 2025, at 8:08, Mika Penttilä wrote:On 7/30/25 14:42, Mika Penttilä wrote:
On 7/30/25 14:30, Zi Yan wrote:
On 30 Jul 2025, at 7:27, Zi Yan wrote:On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from split_huge_pmd() paths and folio is mapped to CPU page tables as a huge device page by one or more task?
The folio only has migration entries pointing to it. From CPU perspective,
it is not mapped. The unmap_folio() used by __folio_split() unmaps a to-be-split
folio by replacing existing page table entries with migration entries
and after that the folio is regarded as “unmapped”.The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio() is called for device private entry, not migrate entry afaics.
Yes, but from CPU perspective, both device private entry and migration entry
are invalid CPU page table entries, so the device private folio is “unmapped”
at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.Quoted 8 lines
Right. That confused me when I was talking to Balbir and looking at v1. When a device private folio is processed in
__folio_split(), Balbir needed to add code to skip CPU mapping handling code. Basically device private folios are CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than the device driver manipulate it assuming core-mm just skips device private folios (barring the CPU access fault handling)?
Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios by CPU and only device driver manipulates their mappings?
Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 7 lines
1) is totally fine. This was in v1 and lead to Zi's
split_unmapped_folio()2) is a problem because folio is mapped.
split_huge_pmd()can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:
folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));I can add checks to make sure that the call does succeed.
Quoted 2 lines
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
Quoted 8 lines
Quoted 4 lines
So after the partial unmap, the folio ends up in a little strange situation, the folio is large, but not mapped (since
large_mapcountcan be 0, after all thefolio_rmap_remove_ptes). Callingfolio_split()on partially unmapped fails becausefolio_get_anon_vma()fails due to thefolio_mapped()failures related tofolio_large_mapcount. There is also additional complexity with ref counts and mapping.I think you mean "Calling
folio_split()on a *fully* unmapped folio fails ..."A partially mapped folio still has
folio_mapcount()> 0 ->folio_mapped()== true.Looking into this again at my end
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Also, didn't quite follow what kind of lock recursion did you encounter doing proper
split_folio()instead?Splitting during partial unmap causes recursive locking issues with
anon_vmawhen invoked fromsplit_huge_pmd_locked()path.Yes, that's very complicated.
Yes and I want to avoid going down that path.
Quoted 4 lines
Quoted 2 lines
Deferred splits do not work for device private pages, due to the migration requirements for fault handling.
Can you elaborate on that?
If a folio is under
deferred_split()and is still pending a split. When a fault is handled on a partially mapped folio, the expectation is that as a part of fault handling during migration, the code inmigrate_folio_mapping()assumes that the folio sizes are the same (via check for reference and mapcount)
Quoted 7 lines
Quoted 4 lines
Quoted 1 line
If you hit a partially-mapped folio, instead of migrating, you would actually want to split and then migrate I assume.
Yes, that is exactly what
migrate_pages()does. And if split fails, the migration fails too. Device private folio probably should do the same thing, assuming splitting device private folio would always succeed.hmm afaics the normal
folio_splitwants to useRMP_USE_SHARED_ZEROPAGEwhen splitting and remapping device private pages, that can't work..
It is fine to exclude device private folio to use RMP_USE_SHARED_ZEROPAGE like:
mm/huge_memory.c +1 -1
@@ -3858,7 +3858,7 @@ 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);
Or it can be done in remove_migration_pte().
Best Regards,
Yan, Zi
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 265 lines · Zi Yan
Quoted 240 lines · Mika Penttilä
Quoted 239 lines · Zi Yan
Quoted 237 lines · David Hildenbrand
Quoted 236 lines · Balbir Singh
Quoted 207 lines · David Hildenbrand
Quoted 206 lines · Balbir Singh
Quoted 194 lines · Mika Penttilä
Hi,
Quoted 186 lines · Balbir Singh
Quoted 165 lines · Zi Yan
Quoted 163 lines · David Hildenbrand
Quoted 150 lines · Mika Penttilä
Quoted 141 lines · Zi Yan
On 30 Jul 2025, at 11:40, Mika Penttilä wrote:
On 7/30/25 18:10, Zi Yan wrote:
On 30 Jul 2025, at 8:49, Mika Penttilä wrote:On 7/30/25 15:25, Zi Yan wrote:
On 30 Jul 2025, at 8:08, Mika Penttilä wrote:On 7/30/25 14:42, Mika Penttilä wrote:
On 7/30/25 14:30, Zi Yan wrote:
On 30 Jul 2025, at 7:27, Zi Yan wrote:On 30 Jul 2025, at 7:16, Mika Penttilä wrote:
Hi,
On 7/30/25 12:21, Balbir Singh wrote:
Make THP handling code in the mm subsystem for THP pages aware of zone
device pages. Although the code is designed to be generic when it comes
to handling splitting of pages, the code is designed to work for THP
page sizes corresponding to HPAGE_PMD_NR.Modify page_vma_mapped_walk() to return true when a zone device huge
entry is present, enabling try_to_migrate() and other code migration
paths to appropriately process the entry. page_vma_mapped_walk() will
return true for zone device private large folios only when
PVMW_THP_DEVICE_PRIVATE is passed. This is to prevent locations that are
not zone device private pages from having to add awareness. The key
callback that needs this flag is try_to_migrate_one(). The other
callbacks page idle, damon use it for setting young/dirty bits, which is
not significant when it comes to pmd level bit harvesting.pmd_pfn() does not work well with zone device entries, use
pfn_pmd_entry_to_swap() for checking and comparison as for zone device
entries.Zone device private entries when split via munmap go through pmd split,
but need to go through a folio split, deferred split does not work if a
fault is encountered because fault handling involves migration entries
(via folio_migrate_mapping) and the folio sizes are expected to be the
same there. This introduces the need to split the folio while handling
the pmd split. Because the folio is still mapped, but calling
folio_split() will cause lock recursion, the __split_unmapped_folio()
code is used with a new helper to wrap the code
split_device_private_folio(), which skips the checks around
folio->mapping, swapcache and the need to go through unmap and remap
folio.Cc: Karol Herbst <[email protected]>
Cc: Lyude Paul <[email protected]>
Cc: Danilo Krummrich <[email protected]>
Cc: David Airlie <[email protected]>
Cc: Simona Vetter <[email protected]>
Cc: "Jérôme Glisse" <[email protected]>
Cc: Shuah Khan <[email protected]>
Cc: David Hildenbrand <[email protected]>
Cc: Barry Song <[email protected]>
Cc: Baolin Wang <[email protected]>
Cc: Ryan Roberts <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: Peter Xu <[email protected]>
Cc: Zi Yan <[email protected]>
Cc: Kefeng Wang <[email protected]>
Cc: Jane Chu <[email protected]>
Cc: Alistair Popple <[email protected]>
Cc: Donet Tom <[email protected]>
Cc: Mika Penttilä <[email protected]>
Cc: Matthew Brost <[email protected]>
Cc: Francois Dugast <[email protected]>
Cc: Ralph Campbell <[email protected]>Signed-off-by: Matthew Brost <[email protected]>
Signed-off-by: Balbir Singh <[email protected]>
---
include/linux/huge_mm.h | 1 +
include/linux/rmap.h | 2 +
include/linux/swapops.h | 17 +++
mm/huge_memory.c | 268 +++++++++++++++++++++++++++++++++-------
mm/page_vma_mapped.c | 13 +-
mm/pgtable-generic.c | 6 +
mm/rmap.c | 22 +++-
7 files changed, 278 insertions(+), 51 deletions(-)<snip>
+/**
+ * split_huge_device_private_folio - split a huge device private folio into
+ * smaller pages (of order 0), currently used by migrate_device logic to
+ * split folios for pages that are partially mapped
+ *
+ * @folio: the folio to split
+ *
+ * The caller has to hold the folio_lock and a reference via folio_get
+ */
+int split_device_private_folio(struct folio *folio)
+{
+ struct folio *end_folio = folio_next(folio);
+ struct folio *new_folio;
+ int ret = 0;
+
+ /*
+ * Split the folio now. In the case of device
+ * private pages, this path is executed when
+ * the pmd is split and since freeze is not true
+ * it is likely the folio will be deferred_split.
+ *
+ * With device private pages, deferred splits of
+ * folios should be handled here to prevent partial
+ * unmaps from causing issues later on in migration
+ * and fault handling flows.
+ */
+ folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));
Why can't this freeze fail? The folio is still mapped afaics, why can't there be other references in addition to the caller?
Based on my off-list conversation with Balbir, the folio is unmapped in
CPU side but mapped in the device. folio_ref_freeeze() is not aware of
device side mapping.
Maybe we should make it aware of device private mapping? So that the
process mirrors CPU side folio split: 1) unmap device private mapping,
2) freeze device private folio, 3) split unmapped folio, 4) unfreeze,
5) remap device private mapping.
Ah ok this was about device private page obviously here, nevermind..
Still, isn't this reachable from split_huge_pmd() paths and folio is mapped to CPU page tables as a huge device page by one or more task?
The folio only has migration entries pointing to it. From CPU perspective,
it is not mapped. The unmap_folio() used by __folio_split() unmaps a to-be-split
folio by replacing existing page table entries with migration entries
and after that the folio is regarded as “unmapped”.The migration entry is an invalid CPU page table entry, so it is not a CPU
split_device_private_folio() is called for device private entry, not migrate entry afaics.
Yes, but from CPU perspective, both device private entry and migration entry
are invalid CPU page table entries, so the device private folio is “unmapped”
at CPU side.
Yes both are "swap entries" but there's difference, the device private ones contribute to mapcount and refcount.
Right. That confused me when I was talking to Balbir and looking at v1.
When a device private folio is processed in __folio_split(), Balbir needed to
add code to skip CPU mapping handling code. Basically device private folios are
CPU unmapped and device mapped.Here are my questions on device private folios:
1. How is mapcount used for device private folios? Why is it needed from CPU
perspective? Can it be stored in a device private specific data structure?Mostly like for normal folios, for instance rmap when doing migrate. I think it would make common code more messy if not done that way but sure possible.
And not consuming pfns (address space) at all would have benefits.Quoted 4 lines
2. When a device private folio is mapped on device, can someone other than
the device driver manipulate it assuming core-mm just skips device private
folios (barring the CPU access fault handling)?Quoted 5 lines
Quoted 3 lines
Where I am going is that can device private folios be treated as unmapped folios
by CPU and only device driver manipulates their mappings?Yes not present by CPU but mm has bookkeeping on them. The private page has no content someone could change while in device, it's just pfn.
Just to clarify: a device-private entry, like a device-exclusive entry, is a *page table mapping* tracked through the rmap -- even though they are not present page table entries.
It would be better if they would be present page table entries that are
PROT_NONE, but it's tricky to mark them as being "special" device-private, device-exclusive etc. Maybe there are ways to do that in the future.Maybe device-private could just be
PROT_NONE, because we can identify the entry type based on the folio. device-exclusive is harder ...Quoted 9 lines
Quoted 1 line
So consider device-private entries just like
PROT_NONEpresent page table entries. Refcount and mapcount is adjusted accordingly by rmap functions.Thanks for the clarification.
So
folio_mapcount()for device private folios should be treated the same as normal folios, even if the corresponding PTEs are not accessible from CPUs. Then I wonder if the device private large folio split should go through__folio_split(), the same as normal folios: unmap, freeze, split, unfreeze, remap. Otherwise, how can we prevent rmap changes during the split?That is true in general, the special cases I mentioned are:
1. split during migration (where we the sizes on source/destination do not match) and so we need to split in the middle of migration. The entries there are already unmapped and hence the special handling
2. Partial unmap case, where we need to split in the context of the unmap due to the isses mentioned in the patch. I expanded the folio split code for device private can be expanded into its own helper, which does not need to do the xas/mapped/lru folio handling. During partial unmap the original folio does get replaced by new anon rmap ptes (split_huge_pmd_locked)Quoted 4 lines
For (2), I spent some time examining the implications of not unmapping the folios prior to split and in the partial unmap path, once we split the PMD the folios diverge. I did not run into any particular race either with the tests.
Quoted 7 lines
1) is totally fine. This was in v1 and lead to Zi's
split_unmapped_folio()2) is a problem because folio is mapped.
split_huge_pmd()can be reached also from other than unmap path. It is vulnerable to races by rmap. And for instance this does not look right without checking:
folio_ref_freeze(folio, 1 + folio_expected_ref_count(folio));I can add checks to make sure that the call does succeed.
Quoted 2 lines
You mention 2) is needed because of some later problems in fault path after pmd split. Would it be possible to split the folio at fault time then?
Quoted 8 lines
Quoted 4 lines
So after the partial unmap, the folio ends up in a little strange situation, the folio is large, but not mapped (since
large_mapcountcan be 0, after all thefolio_rmap_remove_ptes). Callingfolio_split()on partially unmapped fails becausefolio_get_anon_vma()fails due to thefolio_mapped()failures related tofolio_large_mapcount. There is also additional complexity with ref counts and mapping.I think you mean "Calling
folio_split()on a *fully* unmapped folio fails ..."A partially mapped folio still has
folio_mapcount()> 0 ->folio_mapped()== true.Looking into this again at my end
Quoted 9 lines
Quoted 7 lines
Quoted 5 lines
Also, didn't quite follow what kind of lock recursion did you encounter doing proper
split_folio()instead?Splitting during partial unmap causes recursive locking issues with
anon_vmawhen invoked fromsplit_huge_pmd_locked()path.Yes, that's very complicated.
Yes and I want to avoid going down that path.
Quoted 4 lines
Quoted 2 lines
Deferred splits do not work for device private pages, due to the migration requirements for fault handling.
Can you elaborate on that?
If a folio is under
deferred_split()and is still pending a split. When a fault is handled on a partially mapped folio, the expectation is that as a part of fault handling during migration, the code inmigrate_folio_mapping()assumes that the folio sizes are the same (via check for reference and mapcount)Quoted 7 lines
Quoted 4 lines
Quoted 1 line
If you hit a partially-mapped folio, instead of migrating, you would actually want to split and then migrate I assume.
Yes, that is exactly what
migrate_pages()does. And if split fails, the migration fails too. Device private folio probably should do the same thing, assuming splitting device private folio would always succeed.hmm afaics the normal
folio_splitwants to useRMP_USE_SHARED_ZEROPAGEwhen splitting and remapping device private pages, that can't work..It is fine to exclude device private folio to use
RMP_USE_SHARED_ZEROPAGElike:mm/huge_memory.c +1
-1@@ -3858,7 +3858,7 @@ 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);
Quoted 1 line
Or it can be done in
remove_migration_pte().
I have the same set of changes plus more to see if the logic can be simplified and well known paths be taken.
Balbir Singh
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
In reply to Zi YanFYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)
include/linux/huge_mm.h +0 -1
@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);
lib/test_hmm.c +10 -1
@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6 -70
104 lines not on this page · raw message
mm/migrate_device.c +51 -0
63 lines not on this page · raw message
Signature · 1 line
2.50.1
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Hi,
Quoted 5 lines · Balbir Singh
FYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
I think this looks much better!
Quoted 190 lines
Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)include/linux/huge_mm.h +0
-1@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);lib/test_hmm.c +10
-1@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6
-70@@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); -static int __split_unmapped_folio (struct folio *folio,int new_order, -struct page *split_at,struct xa_state *xas, -struct address_space *mapping,bool uniform_split); -static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } -/** -* split_huge_device_private_folio - split a huge device private folio into -* smaller pages (of order 0), currently used by migrate_device logic to -* split folios for pages that are partially mapped -* -* @folio: the folio to split -* -* The caller has to hold the folio_lock and a reference via folio_get -*/ -int split_device_private_folio (struct folio *folio) -{ -struct folio *end_folio =folio_next (folio); -struct folio *new_folio; -int ret =0 ; - -/* -* Split the folio now. In the case of device -* private pages, this path is executed when -* the pmd is split and since freeze is not true -* it is likely the folio will be deferred_split. -* -* With device private pages, deferred splits of -* folios should be handled here to prevent partial -* unmaps from causing issues later on in migration -* and fault handling flows. -*/ -folio_ref_freeze (folio,1 +folio_expected_ref_count (folio)); - ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true); -VM_WARN_ON (ret); -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio =folio_next (new_folio)) { -zone_device_private_split_cb (folio, new_folio); -folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( - new_folio)); - } - -/* -* Mark the end of the folio split for device private THP -* split -*/ -zone_device_private_split_cb (folio,NULL ); -folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio)); -return ret; -} -static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {@@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, freeze = false;if (!freeze) {rmap_t rmap_flags =RMAP_NONE ; -unsigned long addr = haddr; -struct folio *new_folio; -struct folio *end_folio =folio_next (folio);if (anon_exclusive) rmap_flags |=RMAP_EXCLUSIVE ; -folio_lock (folio); -folio_get (folio); - -split_device_private_folio (folio); - -for (new_folio =folio_next (folio); - new_folio != end_folio; - new_folio =folio_next (new_folio)) { - addr +=PAGE_SIZE ; -folio_unlock (new_folio); -folio_add_anon_rmap_ptes (new_folio, - &new_folio->page,1 , - vma, addr, rmap_flags); - } -folio_unlock (folio); -folio_add_anon_rmap_ptes (folio, &folio->page, -1 , vma, haddr, rmap_flags); +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); } }@@ -4065,7 +4001,7 @@ 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);mm/migrate_device.c +15
-0@@ -248,6 +248,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;@@ -259,6 +261,55 @@ 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)) { +struct folio *new_folio; +struct folio *new_fault_folio; + +/* +* The reason for finding pmd present with a +* device private pte and a large folio for the +* pte is partial unmaps. Split the folio now +* for the migration to be handled correctly +*/ +pte_unmap_unlock (ptep, ptl); +
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 286
Quoted 10 lines
mm/migrate_device.c +10
-0+folio_get (folio); +if (folio != fault_folio) +folio_lock (folio); +if (split_folio (folio)) { +if (folio != fault_folio) +folio_unlock (folio); + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } +
The nouveau migrate_to_ram handler needs adjustment also if split happens.
Quoted 11 lines
mm/migrate_device.c +11
-0+/* +* After the split, get back the extra reference +* on the fault_page, this reference is checked during +* folio_migrate_mapping() +*/ +if (migrate->fault_page) { + new_fault_folio =page_folio (migrate->fault_page); +folio_get (new_fault_folio); + } + + new_folio =page_folio (page);
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 307
Quoted 10 lines
mm/migrate_device.c +10
-0+ pfn =page_to_pfn (page); + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (folio != new_folio) { +folio_unlock (folio); +folio_lock (new_folio); + }
Maybe careful not to unlock fault_page ?
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 315
Quoted 8 lines
mm/migrate_device.c +5
-0+folio_put (folio); + addr = start; + goto again; + } + mpfn =migrate_pfn (page_to_pfn (page)) |MIGRATE_PFN_MIGRATE ;if (is_writable_device_private_entry (entry))
--Mika
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 10 lines · Mika Penttilä
Hi,
Quoted 5 lines · Balbir Singh
FYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
I think this looks much better!
Thanks!
Quoted 194 lines
Quoted 194 lines
Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)include/linux/huge_mm.h +0
-1@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);lib/test_hmm.c +10
-1@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6
-70@@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); -static int __split_unmapped_folio (struct folio *folio,int new_order, -struct page *split_at,struct xa_state *xas, -struct address_space *mapping,bool uniform_split); -static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } -/** -* split_huge_device_private_folio - split a huge device private folio into -* smaller pages (of order 0), currently used by migrate_device logic to -* split folios for pages that are partially mapped -* -* @folio: the folio to split -* -* The caller has to hold the folio_lock and a reference via folio_get -*/ -int split_device_private_folio (struct folio *folio) -{ -struct folio *end_folio =folio_next (folio); -struct folio *new_folio; -int ret =0 ; - -/* -* Split the folio now. In the case of device -* private pages, this path is executed when -* the pmd is split and since freeze is not true -* it is likely the folio will be deferred_split. -* -* With device private pages, deferred splits of -* folios should be handled here to prevent partial -* unmaps from causing issues later on in migration -* and fault handling flows. -*/ -folio_ref_freeze (folio,1 +folio_expected_ref_count (folio)); - ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true); -VM_WARN_ON (ret); -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio =folio_next (new_folio)) { -zone_device_private_split_cb (folio, new_folio); -folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( - new_folio)); - } - -/* -* Mark the end of the folio split for device private THP -* split -*/ -zone_device_private_split_cb (folio,NULL ); -folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio)); -return ret; -} -static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {@@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, freeze = false;if (!freeze) {rmap_t rmap_flags =RMAP_NONE ; -unsigned long addr = haddr; -struct folio *new_folio; -struct folio *end_folio =folio_next (folio);if (anon_exclusive) rmap_flags |=RMAP_EXCLUSIVE ; -folio_lock (folio); -folio_get (folio); - -split_device_private_folio (folio); - -for (new_folio =folio_next (folio); - new_folio != end_folio; - new_folio =folio_next (new_folio)) { - addr +=PAGE_SIZE ; -folio_unlock (new_folio); -folio_add_anon_rmap_ptes (new_folio, - &new_folio->page,1 , - vma, addr, rmap_flags); - } -folio_unlock (folio); -folio_add_anon_rmap_ptes (folio, &folio->page, -1 , vma, haddr, rmap_flags); +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); } }@@ -4065,7 +4001,7 @@ 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);mm/migrate_device.c +19
-0@@ -248,6 +248,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;@@ -259,6 +261,55 @@ 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)) { +struct folio *new_folio; +struct folio *new_fault_folio; + +/* +* The reason for finding pmd present with a +* device private pte and a large folio for the +* pte is partial unmaps. Split the folio now +* for the migration to be handled correctly +*/ +pte_unmap_unlock (ptep, ptl); + +folio_get (folio); +if (folio != fault_folio) +folio_lock (folio); +if (split_folio (folio)) {
Quoted 9 lines
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 286
Quoted 6 lines
mm/migrate_device.c +6
-0+if (folio != fault_folio) +folio_unlock (folio); + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } +The nouveau
migrate_to_ramhandler needs adjustment also if split happens.
test_hmm needs adjustment because of the way the backup folios are setup.
Quoted 15 lines
Quoted 15 lines
mm/migrate_device.c +15
-0+/* +* After the split, get back the extra reference +* on the fault_page, this reference is checked during +* folio_migrate_mapping() +*/ +if (migrate->fault_page) { + new_fault_folio =page_folio (migrate->fault_page); +folio_get (new_fault_folio); + } + + new_folio =page_folio (page); + pfn =page_to_pfn (page); + +/* +* Ensure the lock is held on the correct
Quoted 9 lines
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 307
Quoted 6 lines
mm/migrate_device.c +6
-0+* folio after the split +*/ +if (folio != new_folio) { +folio_unlock (folio); +folio_lock (new_folio); + }Maybe careful not to unlock
fault_page?
split_page will unlock everything but the original folio, the code takes the lock on the folio corresponding to the new folio
Quoted 9 lines
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 315
Quoted 8 lines
mm/migrate_device.c +5
-0+folio_put (folio); + addr = start; + goto again; + } + mpfn =migrate_pfn (page_to_pfn (page)) |MIGRATE_PFN_MIGRATE ;if (is_writable_device_private_entry (entry))
Balbir
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Hi,
Quoted 215 lines · Balbir Singh
Quoted 10 lines · Mika Penttilä
Hi,
Quoted 5 lines · Balbir Singh
FYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
I think this looks much better!
Thanks!
Quoted 193 lines
Quoted 193 lines
Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)include/linux/huge_mm.h +0
-1@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);lib/test_hmm.c +10
-1@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6
-70@@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); -static int __split_unmapped_folio (struct folio *folio,int new_order, -struct page *split_at,struct xa_state *xas, -struct address_space *mapping,bool uniform_split); -static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } -/** -* split_huge_device_private_folio - split a huge device private folio into -* smaller pages (of order 0), currently used by migrate_device logic to -* split folios for pages that are partially mapped -* -* @folio: the folio to split -* -* The caller has to hold the folio_lock and a reference via folio_get -*/ -int split_device_private_folio (struct folio *folio) -{ -struct folio *end_folio =folio_next (folio); -struct folio *new_folio; -int ret =0 ; - -/* -* Split the folio now. In the case of device -* private pages, this path is executed when -* the pmd is split and since freeze is not true -* it is likely the folio will be deferred_split. -* -* With device private pages, deferred splits of -* folios should be handled here to prevent partial -* unmaps from causing issues later on in migration -* and fault handling flows. -*/ -folio_ref_freeze (folio,1 +folio_expected_ref_count (folio)); - ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true); -VM_WARN_ON (ret); -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio =folio_next (new_folio)) { -zone_device_private_split_cb (folio, new_folio); -folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( - new_folio)); - } - -/* -* Mark the end of the folio split for device private THP -* split -*/ -zone_device_private_split_cb (folio,NULL ); -folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio)); -return ret; -} -static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {@@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, freeze = false;if (!freeze) {rmap_t rmap_flags =RMAP_NONE ; -unsigned long addr = haddr; -struct folio *new_folio; -struct folio *end_folio =folio_next (folio);if (anon_exclusive) rmap_flags |=RMAP_EXCLUSIVE ; -folio_lock (folio); -folio_get (folio); - -split_device_private_folio (folio); - -for (new_folio =folio_next (folio); - new_folio != end_folio; - new_folio =folio_next (new_folio)) { - addr +=PAGE_SIZE ; -folio_unlock (new_folio); -folio_add_anon_rmap_ptes (new_folio, - &new_folio->page,1 , - vma, addr, rmap_flags); - } -folio_unlock (folio); -folio_add_anon_rmap_ptes (folio, &folio->page, -1 , vma, haddr, rmap_flags); +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); } }@@ -4065,7 +4001,7 @@ 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);mm/migrate_device.c +18
-0@@ -248,6 +248,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;@@ -259,6 +261,55 @@ 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)) { +struct folio *new_folio; +struct folio *new_fault_folio; + +/* +* The reason for finding pmd present with a +* device private pte and a large folio for the +* pte is partial unmaps. Split the folio now +* for the migration to be handled correctly +*/ +pte_unmap_unlock (ptep, ptl); + +folio_get (folio); +if (folio != fault_folio) +folio_lock (folio);Quoted 9 lines
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 286
Quoted 7 lines
mm/migrate_device.c +7
-0+if (split_folio (folio)) { +if (folio != fault_folio) +folio_unlock (folio); + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } +The nouveau
migrate_to_ramhandler needs adjustment also if split happens.
Quoted 1 line
test_hmmneeds adjustment because of the way the backup folios are setup.
nouveau should check the folio order after the possible split happens.
Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
mm/migrate_device.c +14
-0+/* +* After the split, get back the extra reference +* on the fault_page, this reference is checked during +* folio_migrate_mapping() +*/ +if (migrate->fault_page) { + new_fault_folio =page_folio (migrate->fault_page); +folio_get (new_fault_folio); + } + + new_folio =page_folio (page); + pfn =page_to_pfn (page); + +/* Quoted 9 lines
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 307
Quoted 7 lines
mm/migrate_device.c +7
-0+* Ensure the lock is held on the correct +* folio after the split +*/ +if (folio != new_folio) { +folio_unlock (folio); +folio_lock (new_folio); + }Maybe careful not to unlock
fault_page?
Quoted 2 lines
split_pagewill unlock everything but the original folio, the code takes the lock on the folio corresponding to the new folio
I mean do_swap_page() unlocks folio of fault_page and expects it to remain locked.
Quoted 11 lines
Quoted 9 lines
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 315
Quoted 9 lines
mm/migrate_device.c +5
-0+folio_put (folio); + addr = start; + goto again; + } + mpfn =migrate_pfn (page_to_pfn (page)) |MIGRATE_PFN_MIGRATE ;if (is_writable_device_private_entry (entry))Balbir
--Mika
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 218 lines · Mika Penttilä
Hi,
Quoted 215 lines · Balbir Singh
Quoted 10 lines · Mika Penttilä
Hi,
Quoted 5 lines · Balbir Singh
FYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
I think this looks much better!
Thanks!
Quoted 202 lines
Quoted 190 lines
Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)include/linux/huge_mm.h +0
-1@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);lib/test_hmm.c +10
-1@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6
-70@@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); -static int __split_unmapped_folio (struct folio *folio,int new_order, -struct page *split_at,struct xa_state *xas, -struct address_space *mapping,bool uniform_split); -static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } -/** -* split_huge_device_private_folio - split a huge device private folio into -* smaller pages (of order 0), currently used by migrate_device logic to -* split folios for pages that are partially mapped -* -* @folio: the folio to split -* -* The caller has to hold the folio_lock and a reference via folio_get -*/ -int split_device_private_folio (struct folio *folio) -{ -struct folio *end_folio =folio_next (folio); -struct folio *new_folio; -int ret =0 ; - -/* -* Split the folio now. In the case of device -* private pages, this path is executed when -* the pmd is split and since freeze is not true -* it is likely the folio will be deferred_split. -* -* With device private pages, deferred splits of -* folios should be handled here to prevent partial -* unmaps from causing issues later on in migration -* and fault handling flows. -*/ -folio_ref_freeze (folio,1 +folio_expected_ref_count (folio)); - ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true); -VM_WARN_ON (ret); -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio =folio_next (new_folio)) { -zone_device_private_split_cb (folio, new_folio); -folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( - new_folio)); - } - -/* -* Mark the end of the folio split for device private THP -* split -*/ -zone_device_private_split_cb (folio,NULL ); -folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio)); -return ret; -} -static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {@@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, freeze = false;if (!freeze) {rmap_t rmap_flags =RMAP_NONE ; -unsigned long addr = haddr; -struct folio *new_folio; -struct folio *end_folio =folio_next (folio);if (anon_exclusive) rmap_flags |=RMAP_EXCLUSIVE ; -folio_lock (folio); -folio_get (folio); - -split_device_private_folio (folio); - -for (new_folio =folio_next (folio); - new_folio != end_folio; - new_folio =folio_next (new_folio)) { - addr +=PAGE_SIZE ; -folio_unlock (new_folio); -folio_add_anon_rmap_ptes (new_folio, - &new_folio->page,1 , - vma, addr, rmap_flags); - } -folio_unlock (folio); -folio_add_anon_rmap_ptes (folio, &folio->page, -1 , vma, haddr, rmap_flags); +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); } }@@ -4065,7 +4001,7 @@ 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);mm/migrate_device.c +15
-0@@ -248,6 +248,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;@@ -259,6 +261,55 @@ 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)) { +struct folio *new_folio; +struct folio *new_fault_folio; + +/* +* The reason for finding pmd present with a +* device private pte and a large folio for the +* pte is partial unmaps. Split the folio now +* for the migration to be handled correctly +*/ +pte_unmap_unlock (ptep, ptl); +mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 286
Quoted 10 lines
mm/migrate_device.c +10
-0+folio_get (folio); +if (folio != fault_folio) +folio_lock (folio); +if (split_folio (folio)) { +if (folio != fault_folio) +folio_unlock (folio); + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } +The nouveau
migrate_to_ramhandler needs adjustment also if split happens.
Quoted 4 lines
Quoted 1 line
test_hmmneeds adjustment because of the way the backup folios are setup.nouveau should check the folio order after the possible split happens.
You mean the folio_split callback?
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 12 lines
mm/migrate_device.c +11
-0+/* +* After the split, get back the extra reference +* on the fault_page, this reference is checked during +* folio_migrate_mapping() +*/ +if (migrate->fault_page) { + new_fault_folio =page_folio (migrate->fault_page); +folio_get (new_fault_folio); + } + + new_folio =page_folio (page);mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 307
Quoted 10 lines
mm/migrate_device.c +10
-0+ pfn =page_to_pfn (page); + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (folio != new_folio) { +folio_unlock (folio); +folio_lock (new_folio); + }Maybe careful not to unlock
fault_page?
Quoted 5 lines
Quoted 2 lines
split_pagewill unlock everything but the original folio, the code takes the lock on the folio corresponding to the new folioI mean
do_swap_page()unlocks folio offault_pageand expects it to remain locked.
Not sure I follow what you're trying to elaborate on here
Balbir
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Hi,
Quoted 219 lines · Balbir Singh
Quoted 218 lines · Mika Penttilä
Hi,
Quoted 215 lines · Balbir Singh
Quoted 10 lines · Mika Penttilä
Hi,
Quoted 5 lines · Balbir Singh
FYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
I think this looks much better!
Thanks!
Quoted 202 lines
Quoted 190 lines
Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)include/linux/huge_mm.h +0
-1@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);lib/test_hmm.c +10
-1@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6
-70@@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); -static int __split_unmapped_folio (struct folio *folio,int new_order, -struct page *split_at,struct xa_state *xas, -struct address_space *mapping,bool uniform_split); -static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } -/** -* split_huge_device_private_folio - split a huge device private folio into -* smaller pages (of order 0), currently used by migrate_device logic to -* split folios for pages that are partially mapped -* -* @folio: the folio to split -* -* The caller has to hold the folio_lock and a reference via folio_get -*/ -int split_device_private_folio (struct folio *folio) -{ -struct folio *end_folio =folio_next (folio); -struct folio *new_folio; -int ret =0 ; - -/* -* Split the folio now. In the case of device -* private pages, this path is executed when -* the pmd is split and since freeze is not true -* it is likely the folio will be deferred_split. -* -* With device private pages, deferred splits of -* folios should be handled here to prevent partial -* unmaps from causing issues later on in migration -* and fault handling flows. -*/ -folio_ref_freeze (folio,1 +folio_expected_ref_count (folio)); - ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true); -VM_WARN_ON (ret); -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio =folio_next (new_folio)) { -zone_device_private_split_cb (folio, new_folio); -folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( - new_folio)); - } - -/* -* Mark the end of the folio split for device private THP -* split -*/ -zone_device_private_split_cb (folio,NULL ); -folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio)); -return ret; -} -static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {@@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, freeze = false;if (!freeze) {rmap_t rmap_flags =RMAP_NONE ; -unsigned long addr = haddr; -struct folio *new_folio; -struct folio *end_folio =folio_next (folio);if (anon_exclusive) rmap_flags |=RMAP_EXCLUSIVE ; -folio_lock (folio); -folio_get (folio); - -split_device_private_folio (folio); - -for (new_folio =folio_next (folio); - new_folio != end_folio; - new_folio =folio_next (new_folio)) { - addr +=PAGE_SIZE ; -folio_unlock (new_folio); -folio_add_anon_rmap_ptes (new_folio, - &new_folio->page,1 , - vma, addr, rmap_flags); - } -folio_unlock (folio); -folio_add_anon_rmap_ptes (folio, &folio->page, -1 , vma, haddr, rmap_flags); +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); } }@@ -4065,7 +4001,7 @@ 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);mm/migrate_device.c +15
-0@@ -248,6 +248,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;@@ -259,6 +261,55 @@ 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)) { +struct folio *new_folio; +struct folio *new_fault_folio; + +/* +* The reason for finding pmd present with a +* device private pte and a large folio for the +* pte is partial unmaps. Split the folio now +* for the migration to be handled correctly +*/ +pte_unmap_unlock (ptep, ptl); +mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 286
Quoted 10 lines
mm/migrate_device.c +10
-0+folio_get (folio); +if (folio != fault_folio) +folio_lock (folio); +if (split_folio (folio)) { +if (folio != fault_folio) +folio_unlock (folio); + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } +The nouveau
migrate_to_ramhandler needs adjustment also if split happens.
Quoted 4 lines
Quoted 3 lines
Quoted 1 line
test_hmmneeds adjustment because of the way the backup folios are setup.nouveau should check the folio order after the possible split happens.
You mean the
folio_splitcallback?
no, nouveau_dmem_migrate_to_ram():
..
sfolio = page_folio(vmf->page);
order = folio_order(sfolio);...
migrate_vma_setup()
..
if sfolio is split order still reflects the pre-split order
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 24 lines
Quoted 12 lines
mm/migrate_device.c +11
-0+/* +* After the split, get back the extra reference +* on the fault_page, this reference is checked during +* folio_migrate_mapping() +*/ +if (migrate->fault_page) { + new_fault_folio =page_folio (migrate->fault_page); +folio_get (new_fault_folio); + } + + new_folio =page_folio (page);mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 307
Quoted 10 lines
mm/migrate_device.c +10
-0+ pfn =page_to_pfn (page); + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (folio != new_folio) { +folio_unlock (folio); +folio_lock (new_folio); + }Maybe careful not to unlock
fault_page?
Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
split_pagewill unlock everything but the original folio, the code takes the lock on the folio corresponding to the new folioI mean
do_swap_page()unlocks folio offault_pageand expects it to remain locked.Not sure I follow what you're trying to elaborate on here
do_swap_page:
..
if (trylock_page(vmf->page)) {
ret = pgmap->ops->migrate_to_ram(vmf);
<- vmf->page should be locked here even after split
unlock_page(vmf->page);Quoted 2 lines
Balbir
--Mika
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 226 lines · Mika Penttilä
Hi,
Quoted 219 lines · Balbir Singh
Quoted 218 lines · Mika Penttilä
Hi,
Quoted 215 lines · Balbir Singh
Quoted 10 lines · Mika Penttilä
Hi,
Quoted 5 lines · Balbir Singh
FYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
I think this looks much better!
Thanks!
Quoted 202 lines
Quoted 190 lines
Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)include/linux/huge_mm.h +0
-1@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);lib/test_hmm.c +10
-1@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6
-70@@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); -static int __split_unmapped_folio (struct folio *folio,int new_order, -struct page *split_at,struct xa_state *xas, -struct address_space *mapping,bool uniform_split); -static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } -/** -* split_huge_device_private_folio - split a huge device private folio into -* smaller pages (of order 0), currently used by migrate_device logic to -* split folios for pages that are partially mapped -* -* @folio: the folio to split -* -* The caller has to hold the folio_lock and a reference via folio_get -*/ -int split_device_private_folio (struct folio *folio) -{ -struct folio *end_folio =folio_next (folio); -struct folio *new_folio; -int ret =0 ; - -/* -* Split the folio now. In the case of device -* private pages, this path is executed when -* the pmd is split and since freeze is not true -* it is likely the folio will be deferred_split. -* -* With device private pages, deferred splits of -* folios should be handled here to prevent partial -* unmaps from causing issues later on in migration -* and fault handling flows. -*/ -folio_ref_freeze (folio,1 +folio_expected_ref_count (folio)); - ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true); -VM_WARN_ON (ret); -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio =folio_next (new_folio)) { -zone_device_private_split_cb (folio, new_folio); -folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( - new_folio)); - } - -/* -* Mark the end of the folio split for device private THP -* split -*/ -zone_device_private_split_cb (folio,NULL ); -folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio)); -return ret; -} -static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {@@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, freeze = false;if (!freeze) {rmap_t rmap_flags =RMAP_NONE ; -unsigned long addr = haddr; -struct folio *new_folio; -struct folio *end_folio =folio_next (folio);if (anon_exclusive) rmap_flags |=RMAP_EXCLUSIVE ; -folio_lock (folio); -folio_get (folio); - -split_device_private_folio (folio); - -for (new_folio =folio_next (folio); - new_folio != end_folio; - new_folio =folio_next (new_folio)) { - addr +=PAGE_SIZE ; -folio_unlock (new_folio); -folio_add_anon_rmap_ptes (new_folio, - &new_folio->page,1 , - vma, addr, rmap_flags); - } -folio_unlock (folio); -folio_add_anon_rmap_ptes (folio, &folio->page, -1 , vma, haddr, rmap_flags); +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); } }@@ -4065,7 +4001,7 @@ 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);mm/migrate_device.c +15
-0@@ -248,6 +248,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;@@ -259,6 +261,55 @@ 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)) { +struct folio *new_folio; +struct folio *new_fault_folio; + +/* +* The reason for finding pmd present with a +* device private pte and a large folio for the +* pte is partial unmaps. Split the folio now +* for the migration to be handled correctly +*/ +pte_unmap_unlock (ptep, ptl); +mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 286
Quoted 10 lines
mm/migrate_device.c +10
-0+folio_get (folio); +if (folio != fault_folio) +folio_lock (folio); +if (split_folio (folio)) { +if (folio != fault_folio) +folio_unlock (folio); + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } +The nouveau
migrate_to_ramhandler needs adjustment also if split happens.Quoted 4 lines
Quoted 3 lines
Quoted 1 line
test_hmmneeds adjustment because of the way the backup folios are setup.nouveau should check the folio order after the possible split happens.
You mean the
folio_splitcallback?
Quoted 38 lines
no,
nouveau_dmem_migrate_to_ram():
..sfolio = page_folio(vmf->page); order = folio_order(sfolio);...
migrate_vma_setup()
..
if sfolio is split order still reflects the pre-split orderQuoted 28 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
mm/migrate_device.c +11
-0+/* +* After the split, get back the extra reference +* on the fault_page, this reference is checked during +* folio_migrate_mapping() +*/ +if (migrate->fault_page) { + new_fault_folio =page_folio (migrate->fault_page); +folio_get (new_fault_folio); + } + + new_folio =page_folio (page);mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 307
Quoted 10 lines
mm/migrate_device.c +10
-0+ pfn =page_to_pfn (page); + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (folio != new_folio) { +folio_unlock (folio); +folio_lock (new_folio); + }Maybe careful not to unlock
fault_page?Quoted 4 lines
Quoted 2 lines
split_pagewill unlock everything but the original folio, the code takes the lock on the folio corresponding to the new folioI mean
do_swap_page()unlocks folio offault_pageand expects it to remain locked.Not sure I follow what you're trying to elaborate on here
Actually fault_folio should be fine but should we have:
if (fault_folio)
if(folio != new_folio)) {
folio_unlock(folio);
folio_lock(new_folio);
}
else
folio_unlock(folio);Quoted 4 lines
Quoted 2 lines
Balbir
--Mika
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
In reply to Mika PenttiläQuoted 227 lines · Mika Penttilä
Hi,
Quoted 219 lines · Balbir Singh
Quoted 218 lines · Mika Penttilä
Hi,
Quoted 215 lines · Balbir Singh
Quoted 10 lines · Mika Penttilä
Hi,
Quoted 5 lines · Balbir Singh
FYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
I think this looks much better!
Thanks!
Quoted 202 lines
Quoted 190 lines
Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)include/linux/huge_mm.h +0
-1@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);lib/test_hmm.c +10
-1@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6
-70@@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); -static int __split_unmapped_folio (struct folio *folio,int new_order, -struct page *split_at,struct xa_state *xas, -struct address_space *mapping,bool uniform_split); -static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } -/** -* split_huge_device_private_folio - split a huge device private folio into -* smaller pages (of order 0), currently used by migrate_device logic to -* split folios for pages that are partially mapped -* -* @folio: the folio to split -* -* The caller has to hold the folio_lock and a reference via folio_get -*/ -int split_device_private_folio (struct folio *folio) -{ -struct folio *end_folio =folio_next (folio); -struct folio *new_folio; -int ret =0 ; - -/* -* Split the folio now. In the case of device -* private pages, this path is executed when -* the pmd is split and since freeze is not true -* it is likely the folio will be deferred_split. -* -* With device private pages, deferred splits of -* folios should be handled here to prevent partial -* unmaps from causing issues later on in migration -* and fault handling flows. -*/ -folio_ref_freeze (folio,1 +folio_expected_ref_count (folio)); - ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true); -VM_WARN_ON (ret); -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio =folio_next (new_folio)) { -zone_device_private_split_cb (folio, new_folio); -folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( - new_folio)); - } - -/* -* Mark the end of the folio split for device private THP -* split -*/ -zone_device_private_split_cb (folio,NULL ); -folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio)); -return ret; -} -static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {@@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, freeze = false;if (!freeze) {rmap_t rmap_flags =RMAP_NONE ; -unsigned long addr = haddr; -struct folio *new_folio; -struct folio *end_folio =folio_next (folio);if (anon_exclusive) rmap_flags |=RMAP_EXCLUSIVE ; -folio_lock (folio); -folio_get (folio); - -split_device_private_folio (folio); - -for (new_folio =folio_next (folio); - new_folio != end_folio; - new_folio =folio_next (new_folio)) { - addr +=PAGE_SIZE ; -folio_unlock (new_folio); -folio_add_anon_rmap_ptes (new_folio, - &new_folio->page,1 , - vma, addr, rmap_flags); - } -folio_unlock (folio); -folio_add_anon_rmap_ptes (folio, &folio->page, -1 , vma, haddr, rmap_flags); +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); } }@@ -4065,7 +4001,7 @@ 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);mm/migrate_device.c +15
-0@@ -248,6 +248,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;@@ -259,6 +261,55 @@ 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)) { +struct folio *new_folio; +struct folio *new_fault_folio; + +/* +* The reason for finding pmd present with a +* device private pte and a large folio for the +* pte is partial unmaps. Split the folio now +* for the migration to be handled correctly +*/ +pte_unmap_unlock (ptep, ptl); +mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 286
Quoted 10 lines
mm/migrate_device.c +10
-0+folio_get (folio); +if (folio != fault_folio) +folio_lock (folio); +if (split_folio (folio)) { +if (folio != fault_folio) +folio_unlock (folio); + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } +The nouveau
migrate_to_ramhandler needs adjustment also if split happens.Quoted 4 lines
Quoted 3 lines
Quoted 1 line
test_hmmneeds adjustment because of the way the backup folios are setup.nouveau should check the folio order after the possible split happens.
You mean the
folio_splitcallback?
Quoted 9 lines
no,
nouveau_dmem_migrate_to_ram():
..sfolio = page_folio(vmf->page); order = folio_order(sfolio);...
migrate_vma_setup()
..
if sfolio is split order still reflects the pre-split order
Will fix, good catch!
Quoted 28 lines
Quoted 28 lines
Quoted 28 lines
Quoted 24 lines
Quoted 15 lines
Quoted 15 lines
mm/migrate_device.c +14
-0+/* +* After the split, get back the extra reference +* on the fault_page, this reference is checked during +* folio_migrate_mapping() +*/ +if (migrate->fault_page) { + new_fault_folio =page_folio (migrate->fault_page); +folio_get (new_fault_folio); + } + + new_folio =page_folio (page); + pfn =page_to_pfn (page); + +/* Quoted 9 lines
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 307
Quoted 7 lines
mm/migrate_device.c +7
-0+* Ensure the lock is held on the correct +* folio after the split +*/ +if (folio != new_folio) { +folio_unlock (folio); +folio_lock (new_folio); + }Maybe careful not to unlock
fault_page?Quoted 2 lines
split_pagewill unlock everything but the original folio, the code takes the lock on the folio corresponding to the new folioI mean
do_swap_page()unlocks folio offault_pageand expects it to remain locked.
Quoted 9 lines
Quoted 1 line
Not sure I follow what you're trying to elaborate on here
do_swap_page:
..if (trylock_page(vmf->page)) { ret = pgmap->ops->migrate_to_ram(vmf); <- vmf->page should be locked here even after split unlock_page(vmf->page);
Yep, the split will unlock all tail folios, leaving the just head folio locked and this the change, the lock we need to hold is the folio lock associated with fault_page, pte entry and not unlock when the cause is a fault. The code seems to do the right thing there, let me double check
Balbir
and the code does the right thing there.
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 273 lines · Balbir Singh
Quoted 226 lines · Mika Penttilä
Hi,
Quoted 219 lines · Balbir Singh
Quoted 218 lines · Mika Penttilä
Hi,
Quoted 215 lines · Balbir Singh
Quoted 10 lines · Mika Penttilä
Hi,
Quoted 5 lines · Balbir Singh
FYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
I think this looks much better!
Thanks!
Quoted 202 lines
Quoted 190 lines
Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)include/linux/huge_mm.h +0
-1@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);lib/test_hmm.c +10
-1@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6
-70@@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); -static int __split_unmapped_folio (struct folio *folio,int new_order, -struct page *split_at,struct xa_state *xas, -struct address_space *mapping,bool uniform_split); -static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } -/** -* split_huge_device_private_folio - split a huge device private folio into -* smaller pages (of order 0), currently used by migrate_device logic to -* split folios for pages that are partially mapped -* -* @folio: the folio to split -* -* The caller has to hold the folio_lock and a reference via folio_get -*/ -int split_device_private_folio (struct folio *folio) -{ -struct folio *end_folio =folio_next (folio); -struct folio *new_folio; -int ret =0 ; - -/* -* Split the folio now. In the case of device -* private pages, this path is executed when -* the pmd is split and since freeze is not true -* it is likely the folio will be deferred_split. -* -* With device private pages, deferred splits of -* folios should be handled here to prevent partial -* unmaps from causing issues later on in migration -* and fault handling flows. -*/ -folio_ref_freeze (folio,1 +folio_expected_ref_count (folio)); - ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true); -VM_WARN_ON (ret); -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio =folio_next (new_folio)) { -zone_device_private_split_cb (folio, new_folio); -folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( - new_folio)); - } - -/* -* Mark the end of the folio split for device private THP -* split -*/ -zone_device_private_split_cb (folio,NULL ); -folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio)); -return ret; -} -static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {@@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, freeze = false;if (!freeze) {rmap_t rmap_flags =RMAP_NONE ; -unsigned long addr = haddr; -struct folio *new_folio; -struct folio *end_folio =folio_next (folio);if (anon_exclusive) rmap_flags |=RMAP_EXCLUSIVE ; -folio_lock (folio); -folio_get (folio); - -split_device_private_folio (folio); - -for (new_folio =folio_next (folio); - new_folio != end_folio; - new_folio =folio_next (new_folio)) { - addr +=PAGE_SIZE ; -folio_unlock (new_folio); -folio_add_anon_rmap_ptes (new_folio, - &new_folio->page,1 , - vma, addr, rmap_flags); - } -folio_unlock (folio); -folio_add_anon_rmap_ptes (folio, &folio->page, -1 , vma, haddr, rmap_flags); +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); } }@@ -4065,7 +4001,7 @@ 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);mm/migrate_device.c +15
-0@@ -248,6 +248,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;@@ -259,6 +261,55 @@ 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)) { +struct folio *new_folio; +struct folio *new_fault_folio; + +/* +* The reason for finding pmd present with a +* device private pte and a large folio for the +* pte is partial unmaps. Split the folio now +* for the migration to be handled correctly +*/ +pte_unmap_unlock (ptep, ptl); +mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 286
Quoted 10 lines
mm/migrate_device.c +10
-0+folio_get (folio); +if (folio != fault_folio) +folio_lock (folio); +if (split_folio (folio)) { +if (folio != fault_folio) +folio_unlock (folio); + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } +The nouveau
migrate_to_ramhandler needs adjustment also if split happens.Quoted 4 lines
Quoted 3 lines
Quoted 1 line
test_hmmneeds adjustment because of the way the backup folios are setup.nouveau should check the folio order after the possible split happens.
You mean the
folio_splitcallback?Quoted 9 lines
no,
nouveau_dmem_migrate_to_ram():
..sfolio = page_folio(vmf->page); order = folio_order(sfolio);...
migrate_vma_setup()
..
if sfolio is split order still reflects the pre-split orderWill fix, good catch!
Quoted 27 lines
Quoted 27 lines
Quoted 27 lines
Quoted 23 lines
Quoted 14 lines
Quoted 14 lines
mm/migrate_device.c +14
-0+/* +* After the split, get back the extra reference +* on the fault_page, this reference is checked during +* folio_migrate_mapping() +*/ +if (migrate->fault_page) { + new_fault_folio =page_folio (migrate->fault_page); +folio_get (new_fault_folio); + } + + new_folio =page_folio (page); + pfn =page_to_pfn (page); + +/* Quoted 9 lines
mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 307
Quoted 7 lines
mm/migrate_device.c +7
-0+* Ensure the lock is held on the correct +* folio after the split +*/ +if (folio != new_folio) { +folio_unlock (folio); +folio_lock (new_folio); + }Maybe careful not to unlock
fault_page?Quoted 2 lines
split_pagewill unlock everything but the original folio, the code takes the lock on the folio corresponding to the new folioI mean
do_swap_page()unlocks folio offault_pageand expects it to remain locked.Quoted 8 lines
Quoted 1 line
Not sure I follow what you're trying to elaborate on here
do_swap_page:
..if (trylock_page(vmf->page)) { ret = pgmap->ops->migrate_to_ram(vmf); <- vmf->page should be locked here even after split unlock_page(vmf->page);
Quoted 4 lines
Yep, the split will unlock all tail folios, leaving the just head folio locked and this the change, the lock we need to hold is the folio lock associated with
fault_page, pte entry and not unlock when the cause is a fault. The code seems to do the right thing there, let me double check
Yes the fault case is ok. But if migrate not for a fault, we should not leave any page locked
Quoted 4 lines
Balbir
and the code does the right thing there.
--Mika
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 276 lines · Mika Penttilä
Quoted 273 lines · Balbir Singh
Quoted 226 lines · Mika Penttilä
Hi,
Quoted 219 lines · Balbir Singh
Quoted 218 lines · Mika Penttilä
Hi,
Quoted 215 lines · Balbir Singh
Quoted 10 lines · Mika Penttilä
Hi,
Quoted 5 lines · Balbir Singh
FYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
I think this looks much better!
Thanks!
Quoted 202 lines
Quoted 190 lines
Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)include/linux/huge_mm.h +0
-1@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);lib/test_hmm.c +10
-1@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6
-70@@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); -static int __split_unmapped_folio (struct folio *folio,int new_order, -struct page *split_at,struct xa_state *xas, -struct address_space *mapping,bool uniform_split); -static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } -/** -* split_huge_device_private_folio - split a huge device private folio into -* smaller pages (of order 0), currently used by migrate_device logic to -* split folios for pages that are partially mapped -* -* @folio: the folio to split -* -* The caller has to hold the folio_lock and a reference via folio_get -*/ -int split_device_private_folio (struct folio *folio) -{ -struct folio *end_folio =folio_next (folio); -struct folio *new_folio; -int ret =0 ; - -/* -* Split the folio now. In the case of device -* private pages, this path is executed when -* the pmd is split and since freeze is not true -* it is likely the folio will be deferred_split. -* -* With device private pages, deferred splits of -* folios should be handled here to prevent partial -* unmaps from causing issues later on in migration -* and fault handling flows. -*/ -folio_ref_freeze (folio,1 +folio_expected_ref_count (folio)); - ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true); -VM_WARN_ON (ret); -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio =folio_next (new_folio)) { -zone_device_private_split_cb (folio, new_folio); -folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( - new_folio)); - } - -/* -* Mark the end of the folio split for device private THP -* split -*/ -zone_device_private_split_cb (folio,NULL ); -folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio)); -return ret; -} -static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {@@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, freeze = false;if (!freeze) {rmap_t rmap_flags =RMAP_NONE ; -unsigned long addr = haddr; -struct folio *new_folio; -struct folio *end_folio =folio_next (folio);if (anon_exclusive) rmap_flags |=RMAP_EXCLUSIVE ; -folio_lock (folio); -folio_get (folio); - -split_device_private_folio (folio); - -for (new_folio =folio_next (folio); - new_folio != end_folio; - new_folio =folio_next (new_folio)) { - addr +=PAGE_SIZE ; -folio_unlock (new_folio); -folio_add_anon_rmap_ptes (new_folio, - &new_folio->page,1 , - vma, addr, rmap_flags); - } -folio_unlock (folio); -folio_add_anon_rmap_ptes (folio, &folio->page, -1 , vma, haddr, rmap_flags); +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); } }@@ -4065,7 +4001,7 @@ 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);mm/migrate_device.c +15
-0@@ -248,6 +248,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;@@ -259,6 +261,55 @@ 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)) { +struct folio *new_folio; +struct folio *new_fault_folio; + +/* +* The reason for finding pmd present with a +* device private pte and a large folio for the +* pte is partial unmaps. Split the folio now +* for the migration to be handled correctly +*/ +pte_unmap_unlock (ptep, ptl); +mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 286
Quoted 10 lines
mm/migrate_device.c +10
-0+folio_get (folio); +if (folio != fault_folio) +folio_lock (folio); +if (split_folio (folio)) { +if (folio != fault_folio) +folio_unlock (folio); + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } +The nouveau
migrate_to_ramhandler needs adjustment also if split happens.Quoted 4 lines
Quoted 3 lines
Quoted 1 line
test_hmmneeds adjustment because of the way the backup folios are setup.nouveau should check the folio order after the possible split happens.
You mean the
folio_splitcallback?Quoted 9 lines
no,
nouveau_dmem_migrate_to_ram():
..sfolio = page_folio(vmf->page); order = folio_order(sfolio);...
migrate_vma_setup()
..
if sfolio is split order still reflects the pre-split orderWill fix, good catch!
Quoted 35 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
mm/migrate_device.c +11
-0+/* +* After the split, get back the extra reference +* on the fault_page, this reference is checked during +* folio_migrate_mapping() +*/ +if (migrate->fault_page) { + new_fault_folio =page_folio (migrate->fault_page); +folio_get (new_fault_folio); + } + + new_folio =page_folio (page);mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 307
Quoted 10 lines
mm/migrate_device.c +10
-0+ pfn =page_to_pfn (page); + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (folio != new_folio) { +folio_unlock (folio); +folio_lock (new_folio); + }Maybe careful not to unlock
fault_page?Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
split_pagewill unlock everything but the original folio, the code takes the lock on the folio corresponding to the new folioI mean
do_swap_page()unlocks folio offault_pageand expects it to remain locked.Not sure I follow what you're trying to elaborate on here
do_swap_page:
..if (trylock_page(vmf->page)) { ret = pgmap->ops->migrate_to_ram(vmf); <- vmf->page should be locked here even after split unlock_page(vmf->page);
Quoted 7 lines
Quoted 4 lines
Yep, the split will unlock all tail folios, leaving the just head folio locked and this the change, the lock we need to hold is the folio lock associated with
fault_page, pte entry and not unlock when the cause is a fault. The code seems to do the right thing there, let me double checkYes the fault case is ok. But if migrate not for a fault, we should not leave any page locked
migrate_vma_finalize() handles this
Balbir
Re: [v2 02/11] mm/thp: zone_device awareness in THP handling code
Quoted 276 lines · Balbir Singh
Quoted 275 lines · Mika Penttilä
Quoted 273 lines · Balbir Singh
Quoted 226 lines · Mika Penttilä
Hi,
Quoted 219 lines · Balbir Singh
Quoted 218 lines · Mika Penttilä
Hi,
Quoted 215 lines · Balbir Singh
Quoted 10 lines · Mika Penttilä
Hi,
Quoted 5 lines · Balbir Singh
FYI:
I have the following patch on top of my series that seems to make it work without requiring the helper to split device private folios
I think this looks much better!
Thanks!
Quoted 202 lines
Quoted 190 lines
Signed-off-by Balbir Singh <[email protected]>--- include/linux/huge_mm.h | 1 - lib/test_hmm.c | 11 +++++- mm/huge_memory.c | 76 ++++------------------------------------- mm/migrate_device.c | 51 +++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 72 deletions(-)include/linux/huge_mm.h +0
-1@@ -343,7 +343,6 @@ 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_device_private_folio (struct folio *folio);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);lib/test_hmm.c +10
-1@@ -1625,13 +1625,22 @@ static vm_fault_t dmirror_devmem_fault(struct vm_fault *vmf)* the mirror but here we use it to hold the page for the simulated * device memory and that page holds the pointer to the mirror. */ - rpage = vmf->page->zone_device_data; + rpage =folio_page (page_folio (vmf->page),0 )->zone_device_data; dmirror = rpage->zone_device_data;/* FIXME demonstrate how we can adjust migrate range */ 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 +6
-70@@ -72,10 +72,6 @@ static unsigned long deferred_split_count(struct shrinker *shrink, struct shrink_control *sc);static unsigned long deferred_split_scan (struct shrinker *shrink,struct shrink_control *sc); -static int __split_unmapped_folio (struct folio *folio,int new_order, -struct page *split_at,struct xa_state *xas, -struct address_space *mapping,bool uniform_split); -static bool split_underused_thp = true;static atomic_t huge_zero_refcount;@@ -2924,51 +2920,6 @@ static void __split_huge_zero_page_pmd(struct vm_area_struct *vma,pmd_populate (mm, pmd, pgtable); } -/** -* split_huge_device_private_folio - split a huge device private folio into -* smaller pages (of order 0), currently used by migrate_device logic to -* split folios for pages that are partially mapped -* -* @folio: the folio to split -* -* The caller has to hold the folio_lock and a reference via folio_get -*/ -int split_device_private_folio (struct folio *folio) -{ -struct folio *end_folio =folio_next (folio); -struct folio *new_folio; -int ret =0 ; - -/* -* Split the folio now. In the case of device -* private pages, this path is executed when -* the pmd is split and since freeze is not true -* it is likely the folio will be deferred_split. -* -* With device private pages, deferred splits of -* folios should be handled here to prevent partial -* unmaps from causing issues later on in migration -* and fault handling flows. -*/ -folio_ref_freeze (folio,1 +folio_expected_ref_count (folio)); - ret =__split_unmapped_folio (folio,0 , &folio->page,NULL ,NULL , true); -VM_WARN_ON (ret); -for (new_folio =folio_next (folio); new_folio != end_folio; - new_folio =folio_next (new_folio)) { -zone_device_private_split_cb (folio, new_folio); -folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( - new_folio)); - } - -/* -* Mark the end of the folio split for device private THP -* split -*/ -zone_device_private_split_cb (folio,NULL ); -folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio)); -return ret; -} -static void __split_huge_pmd_locked (struct vm_area_struct *vma,pmd_t *pmd,unsigned long haddr,bool freeze) {@@ -3064,30 +3015,15 @@ static void __split_huge_pmd_locked(struct vm_area_struct *vma, pmd_t *pmd, freeze = false;if (!freeze) {rmap_t rmap_flags =RMAP_NONE ; -unsigned long addr = haddr; -struct folio *new_folio; -struct folio *end_folio =folio_next (folio);if (anon_exclusive) rmap_flags |=RMAP_EXCLUSIVE ; -folio_lock (folio); -folio_get (folio); - -split_device_private_folio (folio); - -for (new_folio =folio_next (folio); - new_folio != end_folio; - new_folio =folio_next (new_folio)) { - addr +=PAGE_SIZE ; -folio_unlock (new_folio); -folio_add_anon_rmap_ptes (new_folio, - &new_folio->page,1 , - vma, addr, rmap_flags); - } -folio_unlock (folio); -folio_add_anon_rmap_ptes (folio, &folio->page, -1 , vma, haddr, rmap_flags); +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); } }@@ -4065,7 +4001,7 @@ 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);mm/migrate_device.c +15
-0@@ -248,6 +248,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;@@ -259,6 +261,55 @@ 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)) { +struct folio *new_folio; +struct folio *new_fault_folio; + +/* +* The reason for finding pmd present with a +* device private pte and a large folio for the +* pte is partial unmaps. Split the folio now +* for the migration to be handled correctly +*/ +pte_unmap_unlock (ptep, ptl); +mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 286
Quoted 10 lines
mm/migrate_device.c +10
-0+folio_get (folio); +if (folio != fault_folio) +folio_lock (folio); +if (split_folio (folio)) { +if (folio != fault_folio) +folio_unlock (folio); + ptep =pte_offset_map_lock (mm, pmdp, addr, &ptl); + goto next; + } +The nouveau
migrate_to_ramhandler needs adjustment also if split happens.Quoted 4 lines
Quoted 3 lines
Quoted 1 line
test_hmmneeds adjustment because of the way the backup folios are setup.nouveau should check the folio order after the possible split happens.
You mean the
folio_splitcallback?Quoted 9 lines
no,
nouveau_dmem_migrate_to_ram():
..sfolio = page_folio(vmf->page); order = folio_order(sfolio);...
migrate_vma_setup()
..
if sfolio is split order still reflects the pre-split orderWill fix, good catch!
Quoted 35 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Quoted 11 lines
mm/migrate_device.c +11
-0+/* +* After the split, get back the extra reference +* on the fault_page, this reference is checked during +* folio_migrate_mapping() +*/ +if (migrate->fault_page) { + new_fault_folio =page_folio (migrate->fault_page); +folio_get (new_fault_folio); + } + + new_folio =page_folio (page);mm/migrate_device.c static int migrate_vma_collect_pmd(pmd_t *pmdp, · line 307
Quoted 10 lines
mm/migrate_device.c +10
-0+ pfn =page_to_pfn (page); + +/* +* Ensure the lock is held on the correct +* folio after the split +*/ +if (folio != new_folio) { +folio_unlock (folio); +folio_lock (new_folio); + }Maybe careful not to unlock
fault_page?Quoted 5 lines
Quoted 4 lines
Quoted 2 lines
split_pagewill unlock everything but the original folio, the code takes the lock on the folio corresponding to the new folioI mean
do_swap_page()unlocks folio offault_pageand expects it to remain locked.Not sure I follow what you're trying to elaborate on here
do_swap_page:
..if (trylock_page(vmf->page)) { ret = pgmap->ops->migrate_to_ram(vmf); <- vmf->page should be locked here even after split unlock_page(vmf->page);
Quoted 7 lines
Quoted 6 lines
Quoted 4 lines
Yep, the split will unlock all tail folios, leaving the just head folio locked and this the change, the lock we need to hold is the folio lock associated with
fault_page, pte entry and not unlock when the cause is a fault. The code seems to do the right thing there, let me double checkYes the fault case is ok. But if migrate not for a fault, we should not leave any page locked
migrate_vma_finalize()handles this
But we are in migrate_vma_collect_pmd() after the split, and try to collect the pte, locking the page again. So needs to be unlocked after the split.
Quoted 3 lines
Balbir
kernel test robot
build botRe: [v2 02/11] mm/thp: zone_device awareness in THP handling code
In reply to Balbir Singhkernel test robot noticed the following build warnings:
Hi Balbir,
kernel test robot noticed the following build warnings:
[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on next-20250730]
[cannot apply to akpm-mm/mm-nonmm-unstable shuah-kselftest/next shuah-kselftest/fixes linus/master v6.16] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Balbir-Singh/mm-zone_device-support-large-zone-device-private-folios/20250730-172600 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20250730092139.3890844-3-balbirs%40nvidia.com patch subject: [v2 02/11] mm/thp: zone_device awareness in THP handling code config: i386-buildonly-randconfig-001-20250731 (https://download.01.org/0day-ci/archive/20250731/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14+deb12u1) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250731/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
mm/rmap.c: In function 'try_to_migrate_one':
>> mm/rmap.c:2330:39: warning: unused variable 'pfn' [-Wunused-variable]
2330 | unsigned long pfn;
| ^~~vim +/pfn +2330 mm/rmap.c
2273
2274 /*
2275 * @arg: enum ttu_flags will be passed to this argument.
2276 *
2277 * If TTU_SPLIT_HUGE_PMD is specified any PMD mappings will be split into PTEs
2278 * containing migration entries.
2279 */
2280 static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,
2281 unsigned long address, void *arg)
2282 {
2283 struct mm_struct *mm = vma->vm_mm;
2284 DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address,
2285 PVMW_THP_DEVICE_PRIVATE);
2286 bool anon_exclusive, writable, ret = true;
2287 pte_t pteval;
2288 struct page *subpage;
2289 struct mmu_notifier_range range;
2290 enum ttu_flags flags = (enum ttu_flags)(long)arg;
2291 unsigned long pfn;
2292 unsigned long hsz = 0;
2293
2294 /*
2295 * When racing against e.g. zap_pte_range() on another cpu,
2296 * in between its ptep_get_and_clear_full() and folio_remove_rmap_*(),
2297 * try_to_migrate() may return before page_mapped() has become false,
2298 * if page table locking is skipped: use TTU_SYNC to wait for that.
2299 */
2300 if (flags & TTU_SYNC)
2301 pvmw.flags = PVMW_SYNC;
2302
2303 /*
2304 * For THP, we have to assume the worse case ie pmd for invalidation.
2305 * For hugetlb, it could be much worse if we need to do pud
2306 * invalidation in the case of pmd sharing.
2307 *
2308 * Note that the page can not be free in this function as call of
2309 * try_to_unmap() must hold a reference on the page.
2310 */
2311 range.end = vma_address_end(&pvmw);
2312 mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
2313 address, range.end);
2314 if (folio_test_hugetlb(folio)) {
2315 /*
2316 * If sharing is possible, start and end will be adjusted
2317 * accordingly.
2318 */
2319 adjust_range_if_pmd_sharing_possible(vma, &range.start,
2320 &range.end);
2321
2322 /* We need the huge page size for set_huge_pte_at() */
2323 hsz = huge_page_size(hstate_vma(vma));
2324 }
2325 mmu_notifier_invalidate_range_start(&range);
2326
2327 while (page_vma_mapped_walk(&pvmw)) {
2328 /* PMD-mapped THP migration entry */
2329 if (!pvmw.pte) {
> 2330 unsigned long pfn;
2331
2332 if (flags & TTU_SPLIT_HUGE_PMD) {
2333 split_huge_pmd_locked(vma, pvmw.address,
2334 pvmw.pmd, true);
2335 ret = false;
2336 page_vma_mapped_walk_done(&pvmw);
2337 break;
2338 }
2339 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
2340 /*
2341 * Zone device private folios do not work well with
2342 * pmd_pfn() on some architectures due to pte
2343 * inversion.
2344 */
2345 if (is_pmd_device_private_entry(*pvmw.pmd)) {
2346 swp_entry_t entry = pmd_to_swp_entry(*pvmw.pmd);
2347
2348 pfn = swp_offset_pfn(entry);
2349 } else {
2350 pfn = pmd_pfn(*pvmw.pmd);
2351 }
2352
2353 subpage = folio_page(folio, pfn - folio_pfn(folio));
2354
2355 VM_BUG_ON_FOLIO(folio_test_hugetlb(folio) ||
2356 !folio_test_pmd_mappable(folio), folio);
2357
2358 if (set_pmd_migration_entry(&pvmw, subpage)) {
2359 ret = false;
2360 page_vma_mapped_walk_done(&pvmw);
2361 break;
2362 }
2363 continue;
2364 #endif
2365 }
2366
2367 /* Unexpected PMD-mapped THP? */
2368 VM_BUG_ON_FOLIO(!pvmw.pte, folio);
2369
2370 /*
2371 * Handle PFN swap PTEs, such as device-exclusive ones, that
2372 * actually map pages.
2373 */
2374 pteval = ptep_get(pvmw.pte);
2375 if (likely(pte_present(pteval))) {
2376 pfn = pte_pfn(pteval);
2377 } else {
2378 pfn = swp_offset_pfn(pte_to_swp_entry(pteval));
2379 VM_WARN_ON_FOLIO(folio_test_hugetlb(folio), folio);
2380 }
2381
2382 subpage = folio_page(folio, pfn - folio_pfn(folio));
2383 address = pvmw.address;
2384 anon_exclusive = folio_test_anon(folio) &&
2385 PageAnonExclusive(subpage);
2386
2387 if (folio_test_hugetlb(folio)) {
2388 bool anon = folio_test_anon(folio);
2389
2390 /*
2391 * huge_pmd_unshare may unmap an entire PMD page.
2392 * There is no way of knowing exactly which PMDs may
2393 * be cached for this mm, so we must flush them all.
2394 * start/end were already adjusted above to cover this
2395 * range.
2396 */
2397 flush_cache_range(vma, range.start, range.end);
2398
2399 /*
2400 * To call huge_pmd_unshare, i_mmap_rwsem must be
2401 * held in write mode. Caller needs to explicitly
2402 * do this outside rmap routines.
2403 *
2404 * We also must hold hugetlb vma_lock in write mode.
2405 * Lock order dictates acquiring vma_lock BEFORE
2406 * i_mmap_rwsem. We can only try lock here and
2407 * fail if unsuccessful.
2408 */
2409 if (!anon) {
2410 VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
2411 if (!hugetlb_vma_trylock_write(vma)) {
2412 page_vma_mapped_walk_done(&pvmw);
2413 ret = false;
2414 break;
2415 }
2416 if (huge_pmd_unshare(mm, vma, address, pvmw.pte)) {
2417 hugetlb_vma_unlock_write(vma);
2418 flush_tlb_range(vma,
2419 range.start, range.end);
2420
2421 /*
2422 * The ref count of the PMD page was
2423 * dropped which is part of the way map
2424 * counting is done for shared PMDs.
2425 * Return 'true' here. When there is
2426 * no other sharing, huge_pmd_unshare
2427 * returns false and we will unmap the
2428 * actual page and drop map count
2429 * to zero.
2430 */
2431 page_vma_mapped_walk_done(&pvmw);
2432 break;
2433 }
2434 hugetlb_vma_unlock_write(vma);
2435 }
2436 /* Nuke the hugetlb page table entry */
2437 pteval = huge_ptep_clear_flush(vma, address, pvmw.pte);
2438 if (pte_dirty(pteval))
2439 folio_mark_dirty(folio);
2440 writable = pte_write(pteval);
2441 } else if (likely(pte_present(pteval))) {
2442 flush_cache_page(vma, address, pfn);
2443 /* Nuke the page table entry. */
2444 if (should_defer_flush(mm, flags)) {
2445 /*
2446 * We clear the PTE but do not flush so potentially
2447 * a remote CPU could still be writing to the folio.
2448 * If the entry was previously clean then the
2449 * architecture must guarantee that a clear->dirty
2450 * transition on a cached TLB entry is written through
2451 * and traps if the PTE is unmapped.
2452 */
2453 pteval = ptep_get_and_clear(mm, address, pvmw.pte);
2454
2455 set_tlb_ubc_flush_pending(mm, pteval, address, address + PAGE_SIZE);
2456 } else {
2457 pteval = ptep_clear_flush(vma, address, pvmw.pte);
2458 }
2459 if (pte_dirty(pteval))
2460 folio_mark_dirty(folio);
2461 writable = pte_write(pteval);
2462 } else {
2463 pte_clear(mm, address, pvmw.pte);
2464 writable = is_writable_device_private_entry(pte_to_swp_entry(pteval));
2465 }
2466
2467 VM_WARN_ON_FOLIO(writable && folio_test_anon(folio) &&
2468 !anon_exclusive, folio);
2469
2470 /* Update high watermark before we lower rss */
2471 update_hiwater_rss(mm);
2472
2473 if (PageHWPoison(subpage)) {
2474 VM_WARN_ON_FOLIO(folio_is_device_private(folio), folio);
2475
2476 pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
2477 if (folio_test_hugetlb(folio)) {
2478 hugetlb_count_sub(folio_nr_pages(folio), mm);
2479 set_huge_pte_at(mm, address, pvmw.pte, pteval,
2480 hsz);
2481 } else {
2482 dec_mm_counter(mm, mm_counter(folio));
2483 set_pte_at(mm, address, pvmw.pte, pteval);
2484 }
2485 } else if (likely(pte_present(pteval)) && pte_unused(pteval) &&
2486 !userfaultfd_armed(vma)) {
2487 /*
2488 * The guest indicated that the page content is of no
2489 * interest anymore. Simply discard the pte, vmscan
2490 * will take care of the rest.
2491 * A future reference will then fault in a new zero
2492 * page. When userfaultfd is active, we must not drop
2493 * this page though, as its main user (postcopy
2494 * migration) will not expect userfaults on already
2495 * copied pages.
2496 */
2497 dec_mm_counter(mm, mm_counter(folio));
2498 } else {
2499 swp_entry_t entry;
2500 pte_t swp_pte;
2501
2502 /*
2503 * arch_unmap_one() is expected to be a NOP on
2504 * architectures where we could have PFN swap PTEs,
2505 * so we'll not check/care.
2506 */
2507 if (arch_unmap_one(mm, vma, address, pteval) < 0) {
2508 if (folio_test_hugetlb(folio))
2509 set_huge_pte_at(mm, address, pvmw.pte,
2510 pteval, hsz);
2511 else
2512 set_pte_at(mm, address, pvmw.pte, pteval);
2513 ret = false;
2514 page_vma_mapped_walk_done(&pvmw);
2515 break;
2516 }
2517
2518 /* See folio_try_share_anon_rmap_pte(): clear PTE first. */
2519 if (folio_test_hugetlb(folio)) {
2520 if (anon_exclusive &&
2521 hugetlb_try_share_anon_rmap(folio)) {
2522 set_huge_pte_at(mm, address, pvmw.pte,
2523 pteval, hsz);
2524 ret = false;
2525 page_vma_mapped_walk_done(&pvmw);
2526 break;
2527 }
2528 } else if (anon_exclusive &&
2529 folio_try_share_anon_rmap_pte(folio, subpage)) {
2530 set_pte_at(mm, address, pvmw.pte, pteval);
2531 ret = false;
2532 page_vma_mapped_walk_done(&pvmw);
2533 break;
2534 }
2535
2536 /*
2537 * Store the pfn of the page in a special migration
2538 * pte. do_swap_page() will wait until the migration
2539 * pte is removed and then restart fault handling.
2540 */
2541 if (writable)
2542 entry = make_writable_migration_entry(
2543 page_to_pfn(subpage));
2544 else if (anon_exclusive)
2545 entry = make_readable_exclusive_migration_entry(
2546 page_to_pfn(subpage));
2547 else
2548 entry = make_readable_migration_entry(
2549 page_to_pfn(subpage));
2550 if (likely(pte_present(pteval))) {
2551 if (pte_young(pteval))
2552 entry = make_migration_entry_young(entry);
2553 if (pte_dirty(pteval))
2554 entry = make_migration_entry_dirty(entry);
2555 swp_pte = swp_entry_to_pte(entry);
2556 if (pte_soft_dirty(pteval))
2557 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2558 if (pte_uffd_wp(pteval))
2559 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2560 } else {
2561 swp_pte = swp_entry_to_pte(entry);
2562 if (pte_swp_soft_dirty(pteval))
2563 swp_pte = pte_swp_mksoft_dirty(swp_pte);
2564 if (pte_swp_uffd_wp(pteval))
2565 swp_pte = pte_swp_mkuffd_wp(swp_pte);
2566 }
2567 if (folio_test_hugetlb(folio))
2568 set_huge_pte_at(mm, address, pvmw.pte, swp_pte,
2569 hsz);
2570 else
2571 set_pte_at(mm, address, pvmw.pte, swp_pte);
2572 trace_set_migration_pte(address, pte_val(swp_pte),
2573 folio_order(folio));
2574 /*
2575 * No need to invalidate here it will synchronize on
2576 * against the special swap migration pte.
2577 */
2578 }
2579
2580 if (unlikely(folio_test_hugetlb(folio)))
2581 hugetlb_remove_rmap(folio);
2582 else
2583 folio_remove_rmap_pte(folio, subpage, vma);
2584 if (vma->vm_flags & VM_LOCKED)
2585 mlock_drain_local();
2586 folio_put(folio);
2587 }
2588
2589 mmu_notifier_invalidate_range_end(&range);
2590
2591 return ret;
2592 }
2593
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 3/11 mm/migrate_device: 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 | 456 ++++++++++++++++++++++++++++++++++------ 2 files changed, 395 insertions(+), 63 deletions(-)
include/linux/migrate.h +2 -0
@@ -129,6 +129,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)
@@ -147,6 +148,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 +393 -63
619 lines not on this page · raw message
Signature · 1 line
2.50.1
kernel test robot
build botRe: [v2 03/11] mm/migrate_device: THP migration of zone device pages
kernel test robot noticed the following build warnings:
Hi Balbir,
kernel test robot noticed the following build warnings:
[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on next-20250731]
[cannot apply to akpm-mm/mm-nonmm-unstable shuah-kselftest/next shuah-kselftest/fixes linus/master v6.16] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Balbir-Singh/mm-zone_device-support-large-zone-device-private-folios/20250730-172600 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20250730092139.3890844-4-balbirs%40nvidia.com patch subject: [v2 03/11] mm/migrate_device: THP migration of zone device pages config: x86_64-randconfig-122-20250731 (https://download.01.org/0day-ci/archive/20250731/[email protected]/config) compiler: gcc-12 (Debian 12.2.0-14+deb12u1) 12.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250731/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
sparse warnings: (new ones prefixed by >>)
>> mm/migrate_device.c:769:13: sparse: sparse: incorrect type in assignment (different base types) @@ expected int [assigned] ret @@ got restricted vm_fault_t @@
mm/migrate_device.c:769:13: sparse: expected int [assigned] ret
mm/migrate_device.c:769:13: sparse: got restricted vm_fault_t
mm/migrate_device.c:130:25: sparse: sparse: context imbalance in 'migrate_vma_collect_huge_pmd' - unexpected unlock mm/migrate_device.c:815:16: sparse: sparse: context imbalance in 'migrate_vma_insert_huge_pmd_page' - different lock contexts for basic block
vim +769 mm/migrate_device.c
689
690 #ifdef CONFIG_ARCH_ENABLE_THP_MIGRATION
691 /**
692 * migrate_vma_insert_huge_pmd_page: Insert a huge folio into @migrate->vma->vm_mm
693 * at @addr. folio is already allocated as a part of the migration process with
694 * large page.
695 *
696 * @folio needs to be initialized and setup after it's allocated. The code bits
697 * here follow closely the code in __do_huge_pmd_anonymous_page(). This API does
698 * not support THP zero pages.
699 *
700 * @migrate: migrate_vma arguments
701 * @addr: address where the folio will be inserted
702 * @folio: folio to be inserted at @addr
703 * @src: src pfn which is being migrated
704 * @pmdp: pointer to the pmd
705 */
706 static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
707 unsigned long addr,
708 struct page *page,
709 unsigned long *src,
710 pmd_t *pmdp)
711 {
712 struct vm_area_struct *vma = migrate->vma;
713 gfp_t gfp = vma_thp_gfp_mask(vma);
714 struct folio *folio = page_folio(page);
715 int ret;
716 spinlock_t *ptl;
717 pgtable_t pgtable;
718 pmd_t entry;
719 bool flush = false;
720 unsigned long i;
721
722 VM_WARN_ON_FOLIO(!folio, folio);
723 VM_WARN_ON_ONCE(!pmd_none(*pmdp) && !is_huge_zero_pmd(*pmdp));
724
725 if (!thp_vma_suitable_order(vma, addr, HPAGE_PMD_ORDER))
726 return -EINVAL;
727
728 ret = anon_vma_prepare(vma);
729 if (ret)
730 return ret;
731
732 folio_set_order(folio, HPAGE_PMD_ORDER);
733 folio_set_large_rmappable(folio);
734
735 if (mem_cgroup_charge(folio, migrate->vma->vm_mm, gfp)) {
736 count_vm_event(THP_FAULT_FALLBACK);
737 count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_FALLBACK_CHARGE);
738 ret = -ENOMEM;
739 goto abort;
740 }
741
742 __folio_mark_uptodate(folio);
743
744 pgtable = pte_alloc_one(vma->vm_mm);
745 if (unlikely(!pgtable))
746 goto abort;
747
748 if (folio_is_device_private(folio)) {
749 swp_entry_t swp_entry;
750
751 if (vma->vm_flags & VM_WRITE)
752 swp_entry = make_writable_device_private_entry(
753 page_to_pfn(page));
754 else
755 swp_entry = make_readable_device_private_entry(
756 page_to_pfn(page));
757 entry = swp_entry_to_pmd(swp_entry);
758 } else {
759 if (folio_is_zone_device(folio) &&
760 !folio_is_device_coherent(folio)) {
761 goto abort;
762 }
763 entry = folio_mk_pmd(folio, vma->vm_page_prot);
764 if (vma->vm_flags & VM_WRITE)
765 entry = pmd_mkwrite(pmd_mkdirty(entry), vma);
766 }
767
768 ptl = pmd_lock(vma->vm_mm, pmdp);
> 769 ret = check_stable_address_space(vma->vm_mm);
770 if (ret)
771 goto abort;
772
773 /*
774 * Check for userfaultfd but do not deliver the fault. Instead,
775 * just back off.
776 */
777 if (userfaultfd_missing(vma))
778 goto unlock_abort;
779
780 if (!pmd_none(*pmdp)) {
781 if (!is_huge_zero_pmd(*pmdp))
782 goto unlock_abort;
783 flush = true;
784 } else if (!pmd_none(*pmdp))
785 goto unlock_abort;
786
787 add_mm_counter(vma->vm_mm, MM_ANONPAGES, HPAGE_PMD_NR);
788 folio_add_new_anon_rmap(folio, vma, addr, RMAP_EXCLUSIVE);
789 if (!folio_is_zone_device(folio))
790 folio_add_lru_vma(folio, vma);
791 folio_get(folio);
792
793 if (flush) {
794 pte_free(vma->vm_mm, pgtable);
795 flush_cache_page(vma, addr, addr + HPAGE_PMD_SIZE);
796 pmdp_invalidate(vma, addr, pmdp);
797 } else {
798 pgtable_trans_huge_deposit(vma->vm_mm, pmdp, pgtable);
799 mm_inc_nr_ptes(vma->vm_mm);
800 }
801 set_pmd_at(vma->vm_mm, addr, pmdp, entry);
802 update_mmu_cache_pmd(vma, addr, pmdp);
803
804 spin_unlock(ptl);
805
806 count_vm_event(THP_FAULT_ALLOC);
807 count_mthp_stat(HPAGE_PMD_ORDER, MTHP_STAT_ANON_FAULT_ALLOC);
808 count_memcg_event_mm(vma->vm_mm, THP_FAULT_ALLOC);
809
810 return 0;
811
812 unlock_abort:
813 spin_unlock(ptl);
814 abort:
815 for (i = 0; i < HPAGE_PMD_NR; i++)
816 src[i] &= ~MIGRATE_PFN_MIGRATE;
817 return 0;
818 }
819 #else /* !CONFIG_ARCH_ENABLE_THP_MIGRATION */
820 static int migrate_vma_insert_huge_pmd_page(struct migrate_vma *migrate,
821 unsigned long addr,
822 struct page *page,
823 unsigned long *src,
824 pmd_t *pmdp)
825 {
826 return 0;
827 }
828 #endif
829
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 4/11 mm/memory/fault: add support for zone device THP fault handling
In reply to Balbir SinghWhen the CPU touches a zone device THP entry, the data needs to be migrated back to the CPU, call migrate_to_ram() on these pages via do_huge_pmd_device_private() fault handling helper.
--- include/linux/huge_mm.h | 7 +++++++ mm/huge_memory.c | 36 ++++++++++++++++++++++++++++++++++++ mm/memory.c | 6 ++++-- 3 files changed, 47 insertions(+), 2 deletions(-)
include/linux/huge_mm.h +7 -0
@@ -475,6 +475,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;
@@ -633,6 +635,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
@@ -1271,6 +1271,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 +4 -2
@@ -6152,8 +6152,10 @@ 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_device_private_entry ( +pmd_to_swp_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
PATCH v2 5/11 lib/test_hmm: test cases and support for zone device private THP
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 | 366 +++++++++++++++++++++++++++++++-------- 2 files changed, 303 insertions(+), 75 deletions(-)
include/linux/memremap.h +12 -0
@@ -169,6 +169,18 @@ static inline bool folio_is_device_private(const struct folio *folio)return is_device_private_page (&folio->page); } +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 +291 -75
645 lines not on this page · raw message
Signature · 1 line
2.50.1
kernel test robot
build botRe: [v2 05/11] lib/test_hmm: test cases and support for zone device private THP
kernel test robot noticed the following build warnings:
Hi Balbir,
kernel test robot noticed the following build warnings:
[auto build test WARNING on akpm-mm/mm-everything]
[also build test WARNING on next-20250731]
[cannot apply to akpm-mm/mm-nonmm-unstable shuah-kselftest/next shuah-kselftest/fixes linus/master v6.16] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Balbir-Singh/mm-zone_device-support-large-zone-device-private-folios/20250730-172600 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20250730092139.3890844-6-balbirs%40nvidia.com patch subject: [v2 05/11] lib/test_hmm: test cases and support for zone device private THP config: loongarch-allyesconfig (https://download.01.org/0day-ci/archive/20250731/[email protected]/config) compiler: clang version 22.0.0git (https://github.com/llvm/llvm-project 8f09b03aebb71c154f3bbe725c29e3f47d37c26e) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250731/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> lib/test_hmm.c:1111:6: warning: variable 'dst_pfns' is used uninitialized whenever 'if' condition is true [-Wsometimes-uninitialized]
1111 | if (!src_pfns)
| ^~~~~~~~~
lib/test_hmm.c:1176:8: note: uninitialized use occurs here
1176 | kfree(dst_pfns);
| ^~~~~~~~
lib/test_hmm.c:1111:2: note: remove the 'if' if its condition is always false
1111 | if (!src_pfns)
| ^~~~~~~~~~~~~~
1112 | goto free_mem;
| ~~~~~~~~~~~~~
lib/test_hmm.c:1097:25: note: initialize the variable 'dst_pfns' to silence this warning
1097 | unsigned long *dst_pfns;
| ^
| = NULL1 warning generated.
vim +1111 lib/test_hmm.c
1084
1085 static int dmirror_migrate_to_device(struct dmirror *dmirror,
1086 struct hmm_dmirror_cmd *cmd)
1087 {
1088 unsigned long start, end, addr;
1089 unsigned long size = cmd->npages << PAGE_SHIFT;
1090 struct mm_struct *mm = dmirror->notifier.mm;
1091 struct vm_area_struct *vma;
1092 struct dmirror_bounce bounce;
1093 struct migrate_vma args = { 0 };
1094 unsigned long next;
1095 int ret;
1096 unsigned long *src_pfns;
1097 unsigned long *dst_pfns;
1098
1099 start = cmd->addr;
1100 end = start + size;
1101 if (end < start)
1102 return -EINVAL;
1103
1104 /* Since the mm is for the mirrored process, get a reference first. */
1105 if (!mmget_not_zero(mm))
1106 return -EINVAL;
1107
1108 ret = -ENOMEM;
1109 src_pfns = kvcalloc(PTRS_PER_PTE, sizeof(*src_pfns),
1110 GFP_KERNEL | __GFP_NOFAIL);
> 1111 if (!src_pfns)
1112 goto free_mem;
1113
1114 dst_pfns = kvcalloc(PTRS_PER_PTE, sizeof(*dst_pfns),
1115 GFP_KERNEL | __GFP_NOFAIL);
1116 if (!dst_pfns)
1117 goto free_mem;
1118
1119 ret = 0;
1120 mmap_read_lock(mm);
1121 for (addr = start; addr < end; addr = next) {
1122 vma = vma_lookup(mm, addr);
1123 if (!vma || !(vma->vm_flags & VM_READ)) {
1124 ret = -EINVAL;
1125 goto out;
1126 }
1127 next = min(end, addr + (PTRS_PER_PTE << PAGE_SHIFT));
1128 if (next > vma->vm_end)
1129 next = vma->vm_end;
1130
1131 args.vma = vma;
1132 args.src = src_pfns;
1133 args.dst = dst_pfns;
1134 args.start = addr;
1135 args.end = next;
1136 args.pgmap_owner = dmirror->mdevice;
1137 args.flags = MIGRATE_VMA_SELECT_SYSTEM |
1138 MIGRATE_VMA_SELECT_COMPOUND;
1139 ret = migrate_vma_setup(&args);
1140 if (ret)
1141 goto out;
1142
1143 pr_debug("Migrating from sys mem to device mem\n");
1144 dmirror_migrate_alloc_and_copy(&args, dmirror);
1145 migrate_vma_pages(&args);
1146 dmirror_migrate_finalize_and_map(&args, dmirror);
1147 migrate_vma_finalize(&args);
1148 }
1149 mmap_read_unlock(mm);
1150 mmput(mm);
1151
1152 /*
1153 * Return the migrated data for verification.
1154 * Only for pages in device zone
1155 */
1156 ret = dmirror_bounce_init(&bounce, start, size);
1157 if (ret)
1158 goto free_mem;
1159 mutex_lock(&dmirror->mutex);
1160 ret = dmirror_do_read(dmirror, start, end, &bounce);
1161 mutex_unlock(&dmirror->mutex);
1162 if (ret == 0) {
1163 if (copy_to_user(u64_to_user_ptr(cmd->ptr), bounce.ptr,
1164 bounce.size))
1165 ret = -EFAULT;
1166 }
1167 cmd->cpages = bounce.cpages;
1168 dmirror_bounce_fini(&bounce);
1169 goto free_mem;
1170
1171 out:
1172 mmap_read_unlock(mm);
1173 mmput(mm);
1174 free_mem:
1175 kfree(src_pfns);
1176 kfree(dst_pfns);
1177 return ret;
1178 }
1179
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 6/11 mm/memremap: add folio_split support
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.
The HMM test driver has been updated to handle the split, since the test driver uses backing pages, it requires a mechanism of reorganizing the backing pages (backing pages are used to create a mirror device) again into the right sized order pages. This is supported by exporting prep_compound_page().
--- include/linux/memremap.h | 29 +++++++++++++++++++++++++++++ include/linux/mm.h | 1 + lib/test_hmm.c | 35 +++++++++++++++++++++++++++++++++++ mm/huge_memory.c | 9 ++++++++- 4 files changed, 73 insertions(+), 1 deletion(-)
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)
@@ -229,6 +236,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)
@@ -263,6 +287,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
@@ -1183,6 +1183,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)
mm/huge_memory.c +8 -1
@@ -2955,9 +2955,16 @@ int split_device_private_folio(struct folio *folio)VM_WARN_ON (ret);for (new_folio =folio_next (folio); new_folio != end_folio; new_folio =folio_next (new_folio)) { +zone_device_private_split_cb (folio, new_folio);folio_ref_unfreeze (new_folio,1 +folio_expected_ref_count ( new_folio)); } + +/* +* Mark the end of the folio split for device private THP +* split +*/ +zone_device_private_split_cb (folio,NULL );folio_ref_unfreeze (folio,1 +folio_expected_ref_count (folio));return ret; }
@@ -3979,7 +3986,6 @@ static int __folio_split(struct folio *folio, unsigned int new_order, ret =__split_unmapped_folio (folio, new_order, split_at, &xas, mapping, uniform_split); -/* * Unfreeze after-split folios and put them back to the right * list. @folio should be kept frozon until page cache
@@ -4030,6 +4036,7 @@ static int __folio_split(struct folio *folio, unsigned int new_order,__filemap_remove_folio (new_folio,NULL );folio_put_refs (new_folio, nr_pages); } +/* * Unfreeze @folio only after all page cache entries, which * used to point to it, have been updated with new folios.
Signature · 1 line
2.50.1
PATCH v2 7/11 mm/thp: add split during migration support
In reply to Balbir SinghSupport splitting pages during THP zone device migration as needed. The common case that arises is that after setup, during migrate the destination might not be able to allocate MIGRATE_PFN_COMPOUND pages.
Add a new routine migrate_vma_split_pages() to support the splitting of already isolated pages. The pages being migrated are already unmapped and marked for migration during setup (via unmap). folio_split() and __split_unmapped_folio() take additional isolated arguments, to avoid unmapping and remaping these pages and unlocking/putting the folio.
--- include/linux/huge_mm.h | 11 +++++-- mm/huge_memory.c | 46 ++++++++++++++------------- mm/migrate_device.c | 69 ++++++++++++++++++++++++++++++++++------- 3 files changed, 91 insertions(+), 35 deletions(-)
include/linux/huge_mm.h +9 -2
@@ -343,9 +343,9 @@ 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_device_private_folio (struct folio *folio); +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,
@@ -354,6 +354,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
mm/huge_memory.c +25 -21
119 lines not on this page · raw message
mm/migrate_device.c +57 -12
108 lines not on this page · raw message
Signature · 1 line
2.50.1
kernel test robot
build botRe: [v2 07/11] mm/thp: add split during migration support
kernel test robot noticed the following build errors:
Hi Balbir,
kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
[also build test ERROR on next-20250731]
[cannot apply to akpm-mm/mm-nonmm-unstable shuah-kselftest/next shuah-kselftest/fixes linus/master v6.16] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Balbir-Singh/mm-zone_device-support-large-zone-device-private-folios/20250730-172600 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20250730092139.3890844-8-balbirs%40nvidia.com patch subject: [v2 07/11] mm/thp: add split during migration support config: x86_64-randconfig-071-20250731 (https://download.01.org/0day-ci/archive/20250731/[email protected]/config) compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20250731/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> mm/migrate_device.c:1082:5: error: statement requires expression of scalar type ('void' invalid)
1082 | if (migrate_vma_split_pages(migrate, i, addr,
| ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1083 | folio)) {
| ~~~~~~1 error generated.
vim +1082 mm/migrate_device.c
999
1000 static void __migrate_device_pages(unsigned long *src_pfns,
1001 unsigned long *dst_pfns, unsigned long npages,
1002 struct migrate_vma *migrate)
1003 {
1004 struct mmu_notifier_range range;
1005 unsigned long i, j;
1006 bool notified = false;
1007 unsigned long addr;
1008
1009 for (i = 0; i < npages; ) {
1010 struct page *newpage = migrate_pfn_to_page(dst_pfns[i]);
1011 struct page *page = migrate_pfn_to_page(src_pfns[i]);
1012 struct address_space *mapping;
1013 struct folio *newfolio, *folio;
1014 int r, extra_cnt = 0;
1015 unsigned long nr = 1;
1016
1017 if (!newpage) {
1018 src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
1019 goto next;
1020 }
1021
1022 if (!page) {
1023 unsigned long addr;
1024
1025 if (!(src_pfns[i] & MIGRATE_PFN_MIGRATE))
1026 goto next;
1027
1028 /*
1029 * The only time there is no vma is when called from
1030 * migrate_device_coherent_folio(). However this isn't
1031 * called if the page could not be unmapped.
1032 */
1033 VM_BUG_ON(!migrate);
1034 addr = migrate->start + i*PAGE_SIZE;
1035 if (!notified) {
1036 notified = true;
1037
1038 mmu_notifier_range_init_owner(&range,
1039 MMU_NOTIFY_MIGRATE, 0,
1040 migrate->vma->vm_mm, addr, migrate->end,
1041 migrate->pgmap_owner);
1042 mmu_notifier_invalidate_range_start(&range);
1043 }
1044
1045 if ((src_pfns[i] & MIGRATE_PFN_COMPOUND) &&
1046 (!(dst_pfns[i] & MIGRATE_PFN_COMPOUND))) {
1047 nr = HPAGE_PMD_NR;
1048 src_pfns[i] &= ~MIGRATE_PFN_COMPOUND;
1049 } else {
1050 nr = 1;
1051 }
1052
1053 for (j = 0; j < nr && i + j < npages; j++) {
1054 src_pfns[i+j] |= MIGRATE_PFN_MIGRATE;
1055 migrate_vma_insert_page(migrate,
1056 addr + j * PAGE_SIZE,
1057 &dst_pfns[i+j], &src_pfns[i+j]);
1058 }
1059 goto next;
1060 }
1061
1062 newfolio = page_folio(newpage);
1063 folio = page_folio(page);
1064 mapping = folio_mapping(folio);
1065
1066 /*
1067 * If THP migration is enabled, check if both src and dst
1068 * can migrate large pages
1069 */
1070 if (thp_migration_supported()) {
1071 if ((src_pfns[i] & MIGRATE_PFN_MIGRATE) &&
1072 (src_pfns[i] & MIGRATE_PFN_COMPOUND) &&
1073 !(dst_pfns[i] & MIGRATE_PFN_COMPOUND)) {
1074
1075 if (!migrate) {
1076 src_pfns[i] &= ~(MIGRATE_PFN_MIGRATE |
1077 MIGRATE_PFN_COMPOUND);
1078 goto next;
1079 }
1080 nr = 1 << folio_order(folio);
1081 addr = migrate->start + i * PAGE_SIZE;
> 1082 if (migrate_vma_split_pages(migrate, i, addr,
1083 folio)) {
1084 src_pfns[i] &= ~(MIGRATE_PFN_MIGRATE |
1085 MIGRATE_PFN_COMPOUND);
1086 goto next;
1087 }
1088 } else if ((src_pfns[i] & MIGRATE_PFN_MIGRATE) &&
1089 (dst_pfns[i] & MIGRATE_PFN_COMPOUND) &&
1090 !(src_pfns[i] & MIGRATE_PFN_COMPOUND)) {
1091 src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
1092 }
1093 }
1094
1095
1096 if (folio_is_device_private(newfolio) ||
1097 folio_is_device_coherent(newfolio)) {
1098 if (mapping) {
1099 /*
1100 * For now only support anonymous memory migrating to
1101 * device private or coherent memory.
1102 *
1103 * Try to get rid of swap cache if possible.
1104 */
1105 if (!folio_test_anon(folio) ||
1106 !folio_free_swap(folio)) {
1107 src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
1108 goto next;
1109 }
1110 }
1111 } else if (folio_is_zone_device(newfolio)) {
1112 /*
1113 * Other types of ZONE_DEVICE page are not supported.
1114 */
1115 src_pfns[i] &= ~MIGRATE_PFN_MIGRATE;
1116 goto next;
1117 }
1118
1119 BUG_ON(folio_test_writeback(folio));
1120
1121 if (migrate && migrate->fault_page == page)
1122 extra_cnt++;
1123 for (j = 0; j < nr && i + j < npages; j++) {
1124 folio = page_folio(migrate_pfn_to_page(src_pfns[i+j]));
1125 newfolio = page_folio(migrate_pfn_to_page(dst_pfns[i+j]));
1126
1127 r = folio_migrate_mapping(mapping, newfolio, folio, extra_cnt);
1128 if (r != MIGRATEPAGE_SUCCESS)
1129 src_pfns[i+j] &= ~MIGRATE_PFN_MIGRATE;
1130 else
1131 folio_migrate_flags(newfolio, folio);
1132 }
1133 next:
1134 i += nr;
1135 }
1136
1137 if (notified)
1138 mmu_notifier_invalidate_range_end(&range);
1139 }
1140
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH v2 8/11 lib/test_hmm: add test case for split pages
In reply to Balbir SinghAdd a new flag HMM_DMIRROR_FLAG_FAIL_ALLOC to emulate failure of allocating a large page. This tests the code paths involving split migration.
--- 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 v2 9/11 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
@@ -2056,4 +2056,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 v2 10/11 gpu/drm/nouveau: add THP migration support
In reply to Balbir SinghChange the code to add support for MIGRATE_VMA_SELECT_COMPOUND and appropriately handling page sizes in the migrate/evict code paths.
--- drivers/gpu/drm/nouveau/nouveau_dmem.c | 246 +++++++++++++++++-------- drivers/gpu/drm/nouveau/nouveau_svm.c | 6 +- drivers/gpu/drm/nouveau/nouveau_svm.h | 3 +- 3 files changed, 178 insertions(+), 77 deletions(-)
drivers/gpu/drm/nouveau/nouveau_dmem.c +172 -74
@@ -83,9 +83,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 +118,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)) { +folio_set_zone_device_data (folio, 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 +151,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 +185,38 @@ 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; + +if (order) + args.flags |=MIGRATE_VMA_SELECT_COMPOUND ; + + args.start =ALIGN_DOWN (vmf->address, (1 << (PAGE_SHIFT + 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 +227,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,19 +256,33 @@ 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; +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;
@@ -274,16 +331,21 @@ 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; + +if (!IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE ) || !is_large) { +for (i =0 ; i <DMEM_CHUNK_NPAGES -1 ; ++i, ++page) { + page->zone_device_data = drm->dmem->free_pages; + drm->dmem->free_pages = page; + } } + *ppage = page; 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" , +DMEM_CHUNK_SIZE >>20 , is_large ?"THP " :"" , pfn_first, +nouveau_dmem_page_addr (page));return 0 ;
@@ -298,27 +360,37 @@ 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; + drm->dmem->free_folios =folio_zone_device_data (folio); + chunk =nouveau_page_to_chunk (page); + chunk->callocated++; +spin_unlock (&drm->dmem->lock); + order =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 ; } -zone_device_page_init (page); +zone_device_folio_init (folio, order);return page; }
@@ -369,12 +441,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 +454,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 +486,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 +691,35 @@ 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; 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;
@@ -645,7 +733,7 @@ static unsigned long nouveau_dmem_migrate_copy_one(struct nouveau_drm *drm,return migrate_pfn (page_to_pfn (dpage)); 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 +743,33 @@ 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; i++) { +for (i =0 ; addr < args->end; ) { +struct folio *folio; + + folio =page_folio (migrate_pfn_to_page (args->dst[i])); + order =folio_order (folio); 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 (!dma_mapping_error (drm->dev->dev, dma_info[nr_dma].dma_addr)) nr_dma++; - addr +=PAGE_SIZE ; + 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); }
@@ -689,20 +783,24 @@ nouveau_dmem_migrate_vma(struct nouveau_drm *drm, {unsigned long npages = (end - start) >>PAGE_SHIFT ;unsigned long max =min (SG_MAX_SINGLE_ALLOC , npages); -dma_addr_t *dma_addrs;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 (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) + max =max (HPAGE_PMD_NR , max); + args.src =kcalloc (max,sizeof (*args.src),GFP_KERNEL );if (!args.src) goto out;
@@ -710,8 +808,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 +827,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 +836,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
PATCH v2 11/11 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> /*
@@ -207,8 +208,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); }
@@ -2466,4 +2469,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
On 30.07.25 11:21, Balbir Singh wrote:
BTW, I keep getting confused by the topic.
Isn't this essentially
"mm: support device-private THP"
and the support for migration is just a necessary requirement to *enable* device private?
Signature · 3 lines
Cheers, David / dhildenb
Alistair Popple
reviewerQuoted 10 lines · David Hildenbrand
On 30.07.25 11:21, Balbir Singh wrote:
BTW, I keep getting confused by the topic.
Isn't this essentially
"mm: support device-private THP"
and the support for migration is just a necessary requirement to *enable* device private?
Yes, that's a good point. Migration is one component but there is also fault handling, etc. so I think calling this "support device-private THP" makes sense.
- Alistair
Quoted 5 lines
Signature · 3 lines
Cheers, David / dhildenb
Quoted 10 lines · David Hildenbrand
On 30.07.25 11:21, Balbir Singh wrote:
BTW, I keep getting confused by the topic.
Isn't this essentially
"mm: support device-private THP"
and the support for migration is just a necessary requirement to *enable* device private?
I agree, I can change the title, but the focus of the use case is to support THP migration for improved latency and throughput. All of that involves support of device-private THP
Balbir Singh
Quoted 7 lines · Balbir Singh
Quoted 6 lines · David Hildenbrand
On 30.07.25 11:21, Balbir Singh wrote:
BTW, I keep getting confused by the topic.
Isn't this essentially
Quoted 8 lines
Quoted 4 lines
"mm: support device-private THP"
and the support for migration is just a necessary requirement to *enable* device private?
I agree, I can change the title, but the focus of the use case is to support THP migration for improved latency and throughput. All of that involves support of device-private THP
Well, the subject as is makes one believe that THP support for zone-device pages would already be there, and that you are adding migration support.
That was the confusing part to me, because in the very first patch you add ... THP support for (selected/private) zone device pages.
Signature · 3 lines
Cheers, David / dhildenb
Matthew Brost
reviewerQuoted 93 lines · Balbir Singh
This patch series adds support for THP migration of zone device pages. To do so, the patches implement support for folio zone device pages by adding support for setting up larger order pages. Larger order pages provide a speedup in throughput and latency.
In my local testing (using
lib/test_hmm) and a throughput test, the series shows a 350% improvement in data transfer throughput and a 500% improvement in latencyThese patches build on the earlier posts by Ralph Campbell [1]
Two new flags are added in
vma_migrationto select and mark compound pages.migrate_vma_setup(),migrate_vma_pages()andmigrate_vma_finalize()support migration of these pages whenMIGRATE_VMA_SELECT_COMPOUNDis 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 usedfolio_split()when migrating THP pages, this is used whenMIGRATE_VMA_SELECT_COMPOUNDis not passed as an argument tomigrate_vma_setup().The test infrastructure
lib/test_hmm.chas 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.chas 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_NRin 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 inmigrate_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 finalizeThe 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:
Francois Dugast posted patches support for HMM handling [4], the proposed changes can build on top of this series to provide support for HMM fault handling.
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://lore.kernel.org/lkml/[email protected]/These patches are built on top of
mm/mm-stableCc Karol Herbst <[email protected]>Cc Lyude Paul <[email protected]>Cc Danilo Krummrich <[email protected]>Cc David Airlie <[email protected]>Cc Simona Vetter <[email protected]>Cc "Jérôme Glisse" <[email protected]>Cc Shuah Khan <[email protected]>Cc David Hildenbrand <[email protected]>Cc Barry Song <[email protected]>Cc Baolin Wang <[email protected]>Cc Ryan Roberts <[email protected]>Cc Matthew Wilcox <[email protected]>Cc Peter Xu <[email protected]>Cc Zi Yan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Jane Chu <[email protected]>Cc Alistair Popple <[email protected]>Cc Donet Tom <[email protected]>Cc Ralph Campbell <[email protected]>Cc Mika Penttilä <[email protected]>Cc Matthew Brost <[email protected]>Cc Francois Dugast <[email protected]>
Quoted 12 lines
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 helperis_pmd_device_private_entry()is used
-anon_exclusivehandling of zone device private pages insplit_huge_pmd_locked()has been fixed
- Patches that introduced helpers have been folded into where they are used
- Zone device handling inmm/huge_memory.chas benefited from the code and testing of Matthew Brost, he helped find bugs related tocopy_huge_pmd()and partial unmapping of folios.
I see a ton of discussion on this series, particularly patch 2. It looks like you have landed on a different solution for partial unmaps. I wanted to pull this series into in for testing but if this is actively being refactored, likely best to hold off until next post or test off a WIP branch if you have one.
Matt
Quoted 51 lines
- Zone device THP PMD support via
page_vma_mapped_walk()is restricted totry_to_migrate_one()
- There is a new dedicated helper to split large zone device foliosChangelog v1 [2]:
- Support for handlingfault_folioand 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 withZONE_DEVICEprivate pages on an x86 VMBalbir Singh (11):
mm/zone_device: support large zone device private folios mm/thp:zone_deviceawareness in THP handling codemm/migrate_device: THP migration of zone device pagesmm/memory/fault: add support for zone device THP fault handlinglib/test_hmm: test cases and support for zone device private THP mm/memremap: addfolio_splitsupport
mm/thp: add split during migration supportlib/test_hmm: add test case for split pages selftests/mm/hmm-tests: new tests for zone device THP migration gpu/drm/nouveau: add THP migration support selftests/mm/hmm-tests: new throughput tests including THPdrivers/gpu/drm/nouveau/nouveau_dmem.c | 246 +++++++--- drivers/gpu/drm/nouveau/nouveau_svm.c | 6 +- drivers/gpu/drm/nouveau/nouveau_svm.h | 3 +- include/linux/huge_mm.h | 19 +- include/linux/memremap.h | 51 ++- include/linux/migrate.h | 2 + include/linux/mm.h | 1 + include/linux/rmap.h | 2 + include/linux/swapops.h | 17 + lib/test_hmm.c | 432 ++++++++++++++---- lib/test_hmm_uapi.h | 3 + mm/huge_memory.c | 358 ++++++++++++--- mm/memory.c | 6 +- mm/memremap.c | 48 +- mm/migrate_device.c | 517 ++++++++++++++++++--- mm/page_vma_mapped.c | 13 +- mm/pgtable-generic.c | 6 + mm/rmap.c | 22 +- tools/testing/selftests/mm/hmm-tests.c | 607 ++++++++++++++++++++++++- 19 files changed, 2040 insertions(+), 319 deletions(-)Signature · 1 line
2.50.1