mm: entirely reuse the whole anon mTHP in do_wp_page
Subsystems (2)
RFC PATCH 1/1 mm: entirely reuse the whole anon mTHP in do_wp_page
From: Barry Song <[email protected]>
On a physical phone, it's sometimes observed that deferred_split mTHPs account for over 15% of the total mTHPs. Profiling by Chuanhua indicates that the majority of these originate from the typical fork scenario.
When the child process either execs or exits, the parent process should ideally be able to reuse the entire mTHP. However, the current kernel lacks this capability and instead places the mTHP into split_deferred, performing a CoW (Copy-on-Write) on just a single subpage of the mTHP.
main()
{
#define SIZE 1024 * 1024UL
void *p = malloc(SIZE);
memset(p, 0x11, SIZE);
if (fork() == 0)
exec(....);
/* * this will trigger cow one subpage from
* mTHP and put mTHP into split_deferred
* list
*/
*(int *)(p + 10) = 10;
printf("done\n");
while(1);
}
This leads to two significant issues:
* Memory Waste: Before the mTHP is fully split by the shrinker, it wastes memory. In extreme cases, such as with a 64KB mTHP, the memory usage could be 64KB + 60KB until the last subpage is written, at which point the mTHP is freed.
* Fragmentation and Performance Loss: It destroys large folios (negating the performance benefits of CONT-PTE) and fragments memory.
To address this, we should aim to reuse the entire mTHP in such cases.
Hi David,
I’ve renamed wp_page_reuse() to wp_folio_reuse() and added an entirely_reuse argument because I’m not sure if there are still cases where we reuse a subpage within an mTHP. For now, I’m setting entirely_reuse to true only for the newly supported case, while all other cases still get false. Please let me know if this is incorrect—if we don’t reuse subpages at all, we could remove the argument.
Hi Ryan,
Ideally, I’d like to see ptep_set_access_flags_nr() support setting write-permission for the entire mTHP. Since we don’t currently have this capability, I’m doing it in a rather inefficient way—setting permissions one by one, which involves redundant unfolding and folding of CONTPTE. I wonder if we could collaborate on providing a batched ptep_set_access_flags_nr().
--- mm/memory.c | 91 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 25 deletions(-)
mm/memory.c +66 -25
@@ -3205,18 +3205,26 @@ static vm_fault_t fault_dirty_shared_page(struct vm_fault *vmf)return 0 ; } -/* +/* * Handle write page faults for pages that can be reused in the current vma * * This can happen either due to the mapping being with the VM_SHARED flag, * or due to us being the last reference standing to the page. In either * case, all we need to do here is to mark the page as writable and update * any related book-keeping. +* If entirely_reuse is true, we are reusing the whole large folio; otherwise, +* we are reusing a subpage even though folio might be large one. */ -static inline void wp_page_reuse (struct vm_fault *vmf,struct folio *folio) +static inline void wp_folio_reuse (struct vm_fault *vmf,struct folio *folio, +bool entirely_reuse) __releases(vmf->ptl) { +unsigned long idx = entirely_reuse ?folio_page_idx (folio, vmf->page) :0 ; +int nr = entirely_reuse ?folio_nr_pages (folio) :1 ; +unsigned long start = vmf->address - idx *PAGE_SIZE ; +unsigned long end = start + nr *PAGE_SIZE ;struct vm_area_struct *vma = vmf->vma; +pte_t *ptep = vmf->pte - idx;pte_t entry;VM_BUG_ON (!(vmf->flags &FAULT_FLAG_WRITE ));
@@ -3233,11 +3241,15 @@ static inline void wp_page_reuse(struct vm_fault *vmf, struct folio *folio)folio_xchg_last_cpupid (folio, (1 <<LAST_CPUPID_SHIFT ) -1 ); } -flush_cache_page (vma, vmf->address,pte_pfn (vmf->orig_pte)); - entry =pte_mkyoung (vmf->orig_pte); - entry =maybe_mkwrite (pte_mkdirty (entry), vma); -if (ptep_set_access_flags (vma, vmf->address, vmf->pte, entry,1 )) -update_mmu_cache_range (vmf, vma, vmf->address, vmf->pte,1 ); +flush_cache_range (vma, start, end); +for (int i =0 ; i < nr; i++) { + entry =ptep_get (ptep + i); + entry =pte_mkyoung (entry); + entry =maybe_mkwrite (pte_mkdirty (entry), vma); +if (ptep_set_access_flags (vma, start + i *PAGE_SIZE , + ptep + i, entry,1 )) +update_mmu_cache_range (vmf, vma, start, ptep + i,1 ); + }pte_unmap_unlock (vmf->pte, vmf->ptl);count_vm_event (PGREUSE ); }
@@ -3493,7 +3505,7 @@ static vm_fault_t finish_mkwrite_fault(struct vm_fault *vmf, struct folio *foliopte_unmap_unlock (vmf->pte, vmf->ptl);return VM_FAULT_NOPAGE ; } -wp_page_reuse (vmf, folio); +wp_folio_reuse (vmf, folio, false);return 0 ; }
@@ -3519,7 +3531,7 @@ static vm_fault_t wp_pfn_shared(struct vm_fault *vmf)return ret;return finish_mkwrite_fault (vmf,NULL ); } -wp_page_reuse (vmf,NULL ); +wp_folio_reuse (vmf,NULL , false);return 0 ; }
@@ -3554,7 +3566,7 @@ static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio)return tmp; } }else { -wp_page_reuse (vmf, folio); +wp_folio_reuse (vmf, folio, false);folio_lock (folio); } ret |=fault_dirty_shared_page (vmf);
@@ -3564,17 +3576,41 @@ static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio) }static bool wp_can_reuse_anon_folio (struct folio *folio, -struct vm_area_struct *vma) +struct vm_fault *vmf) { +struct vm_area_struct *vma = vmf->vma; +int nr =folio_nr_pages (folio); +/* -* We could currently only reuse a subpage of a large folio if no -* other subpages of the large folios are still mapped. However, -* let's just consistently not reuse subpages even if we could -* reuse in that scenario, and give back a large folio a bit -* sooner. +* reuse a large folio while it is entirely mapped and +* exclusive (mapcount == folio_nr_pages) */ -if (folio_test_large (folio)) -return false; +if (folio_test_large (folio)) { +unsigned long folio_start, folio_end, idx; +unsigned long address = vmf->address; +pte_t *folio_ptep; +pte_t folio_pte; +if (folio_likely_mapped_shared (folio)) +return false; + + idx =folio_page_idx (folio, vmf->page); + folio_start = address - idx *PAGE_SIZE ; + folio_end = folio_start + nr *PAGE_SIZE ; + +if (unlikely (folio_start <max (address &PMD_MASK , vma->vm_start))) +return false; +if (unlikely (folio_end >pmd_addr_end (address, vma->vm_end))) +return false; + folio_ptep = vmf->pte - idx; + folio_pte =ptep_get (folio_ptep); +if (!pte_present (folio_pte) ||pte_pfn (folio_pte) !=folio_pfn (folio)) +return false; +if (folio_pte_batch (folio, folio_start, folio_ptep, folio_pte, nr,0 , +NULL ,NULL ,NULL ) != nr) +return false; +if (folio_mapcount (folio) != nr) +return false; + }/* * We have to verify under folio lock: these early checks are
@@ -3583,7 +3619,7 @@ static bool wp_can_reuse_anon_folio(struct folio *folio,* * KSM doesn't necessarily raise the folio refcount. */ -if (folio_test_ksm (folio) ||folio_ref_count (folio) >3 ) +if (folio_test_ksm (folio) ||folio_ref_count (folio) >2 + nr)return false;if (!folio_test_lru (folio))/*
@@ -3591,13 +3627,13 @@ static bool wp_can_reuse_anon_folio(struct folio *folio,* remote LRU caches or references to LRU folios. */ lru_add_drain (); -if (folio_ref_count (folio) >1 +folio_test_swapcache (folio)) +if (folio_ref_count (folio) > nr +folio_test_swapcache (folio))return false;if (!folio_trylock (folio))return false;if (folio_test_swapcache (folio))folio_free_swap (folio); -if (folio_test_ksm (folio) ||folio_ref_count (folio) !=1 ) { +if (folio_test_ksm (folio) ||folio_ref_count (folio) != nr) {folio_unlock (folio);return false; }
@@ -3639,6 +3675,7 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)const bool unshare = vmf->flags &FAULT_FLAG_UNSHARE ;struct vm_area_struct *vma = vmf->vma;struct folio *folio =NULL ; +int nr =1 ;pte_t pte;if (likely (!unshare)) {
@@ -3702,14 +3739,18 @@ static vm_fault_t do_wp_page(struct vm_fault *vmf)* the page without further checks. */ if (folio &&folio_test_anon (folio) && - (PageAnonExclusive (vmf->page) ||wp_can_reuse_anon_folio (folio, vma))) { -if (!PageAnonExclusive (vmf->page)) -SetPageAnonExclusive (vmf->page); + (PageAnonExclusive (vmf->page) ||wp_can_reuse_anon_folio (folio, vmf))) { +/* this is the case we are going to reuse the entire folio */ +if (!PageAnonExclusive (vmf->page)) { + nr =folio_nr_pages (folio); +for (int i =0 ; i < nr; i++) +SetPageAnonExclusive (folio_page (folio, i)); + }if (unlikely (unshare)) {pte_unmap_unlock (vmf->pte, vmf->ptl);return 0 ; } -wp_page_reuse (vmf, folio); +wp_folio_reuse (vmf, folio, nr >1 );return 0 ; }/*
Signature · 1 line
2.39.3 (Apple Git-146)
Quoted 40 lines · Barry Song
From: Barry Song <[email protected]>
On a physical phone, it's sometimes observed that
deferred_splitmTHPs account for over 15% of the total mTHPs. Profiling by Chuanhua indicates that the majority of these originate from the typical fork scenario.
When the child process either execs or exits, the parent process should ideally be able to reuse the entire mTHP. However, the current kernel lacks this capability and instead places the mTHP intosplit_deferred, performing a CoW (Copy-on-Write) on just a single subpage of the mTHP.main() { #define SIZE 1024 * 1024UL void *p = malloc(SIZE); memset(p, 0x11, SIZE); if (fork() == 0) exec(....); /* * this will trigger cow one subpage from * mTHP and put mTHP into split_deferred * list */ *(int *)(p + 10) = 10; printf("done\n"); while(1); }This leads to two significant issues:
* Memory Waste: Before the mTHP is fully split by the shrinker, it wastes memory. In extreme cases, such as with a 64KB mTHP, the memory usage could be 64KB + 60KB until the last subpage is written, at which point the mTHP is freed.
* Fragmentation and Performance Loss: It destroys large folios (negating the performance benefits of CONT-PTE) and fragments memory.
To address this, we should aim to reuse the entire mTHP in such cases.
Quoted 8 lines
Hi David,
I’ve renamed
wp_page_reuse()towp_folio_reuse()and added anentirely_reuseargument because I’m not sure if there are still cases where we reuse a subpage within an mTHP. For now, I’m settingentirely_reuseto true only for the newly supported case, while all other cases still get false. Please let me know if this is incorrect—if we don’t reuse subpages at all, we could remove the argument.
See [1] I sent out this week, that is able to reuse even without scanning page tables. If we find the the folio is exclusive we could try processing surrounding PTEs that map the same folio.
[1] https://lkml.kernel.org/r/[email protected]
Signature · 3 lines
Cheers, David / dhildenb
Quoted 51 lines · David Hildenbrand
Quoted 40 lines · Barry Song
From: Barry Song <[email protected]>
On a physical phone, it's sometimes observed that
deferred_splitmTHPs account for over 15% of the total mTHPs. Profiling by Chuanhua indicates that the majority of these originate from the typical fork scenario.
When the child process either execs or exits, the parent process should ideally be able to reuse the entire mTHP. However, the current kernel lacks this capability and instead places the mTHP intosplit_deferred, performing a CoW (Copy-on-Write) on just a single subpage of the mTHP.main() { #define SIZE 1024 * 1024UL void *p = malloc(SIZE); memset(p, 0x11, SIZE); if (fork() == 0) exec(....); /* * this will trigger cow one subpage from * mTHP and put mTHP into split_deferred * list */ *(int *)(p + 10) = 10; printf("done\n"); while(1); }This leads to two significant issues:
* Memory Waste: Before the mTHP is fully split by the shrinker, it wastes memory. In extreme cases, such as with a 64KB mTHP, the memory usage could be 64KB + 60KB until the last subpage is written, at which point the mTHP is freed.
* Fragmentation and Performance Loss: It destroys large folios (negating the performance benefits of CONT-PTE) and fragments memory.
To address this, we should aim to reuse the entire mTHP in such cases.
Quoted 8 lines
Hi David,
I’ve renamed
wp_page_reuse()towp_folio_reuse()and added anentirely_reuseargument because I’m not sure if there are still cases where we reuse a subpage within an mTHP. For now, I’m settingentirely_reuseto true only for the newly supported case, while all other cases still get false. Please let me know if this is incorrect—if we don’t reuse subpages at all, we could remove the argument.
Quoted 5 lines
See [1] I sent out this week, that is able to reuse even without scanning page tables. If we find the the folio is exclusive we could try processing surrounding PTEs that map the same folio.
Great! It looks like I missed your patch again. Since you've implemented this in a better way, I’d prefer to use your patchset.
I’m curious about how you're handling ptep_set_access_flags_nr() or similar things because I couldn’t find the related code in your patch 10/17:
[PATCH v1 10/17] mm: COW reuse support for PTE-mapped THP with CONFIG_MM_ID
Am I missing something?
Quoted 6 lines
--
Cheers,David / dhildenb
Thanks
Barry
Quoted 52 lines · Barry Song
Quoted 51 lines · David Hildenbrand
Quoted 48 lines · Barry Song
From: Barry Song <[email protected]>
On a physical phone, it's sometimes observed that
deferred_splitmTHPs account for over 15% of the total mTHPs. Profiling by Chuanhua indicates that the majority of these originate from the typical fork scenario.
When the child process either execs or exits, the parent process should ideally be able to reuse the entire mTHP. However, the current kernel lacks this capability and instead places the mTHP intosplit_deferred, performing a CoW (Copy-on-Write) on just a single subpage of the mTHP.main() { #define SIZE 1024 * 1024UL void *p = malloc(SIZE); memset(p, 0x11, SIZE); if (fork() == 0) exec(....); /* * this will trigger cow one subpage from * mTHP and put mTHP into split_deferred * list */ *(int *)(p + 10) = 10; printf("done\n"); while(1); }This leads to two significant issues:
* Memory Waste: Before the mTHP is fully split by the shrinker, it wastes memory. In extreme cases, such as with a 64KB mTHP, the memory usage could be 64KB + 60KB until the last subpage is written, at which point the mTHP is freed.
* Fragmentation and Performance Loss: It destroys large folios (negating the performance benefits of CONT-PTE) and fragments memory.
To address this, we should aim to reuse the entire mTHP in such cases.
Hi David,
I’ve renamed
wp_page_reuse()towp_folio_reuse()and added anentirely_reuseargument because I’m not sure if there are still cases where we reuse a subpage within an mTHP. For now, I’m settingentirely_reuseto true only for the newly supported case, while all other cases still get false. Please let me know if this is incorrect—if we don’t reuse subpages at all, we could remove the argument.
Quoted 8 lines
Quoted 5 lines
See [1] I sent out this week, that is able to reuse even without scanning page tables. If we find the the folio is exclusive we could try processing surrounding PTEs that map the same folio.
Great! It looks like I missed your patch again. Since you've implemented this in a better way, I’d prefer to use your patchset.
I wouldn't say better, just more universally. And while taking care of properly sync'ing the mapcount vs. refcount :P
Quoted 7 lines
I’m curious about how you're handling
ptep_set_access_flags_nr()or similar things because I couldn’t find the related code in your patch 10/17:[PATCH v1 10/17] mm: COW reuse support for PTE-mapped THP with
CONFIG_MM_IDAm I missing something?
The idea is to keep individual write faults as fast as possible. So the patch set keeps it simple and only reuses a single PTE at a time, setting that one PAE and mapping it writable.
As the patch states, it might be reasonable to optimize some cases, maybe also only on some architectures. For example to fault-around and map the other ones writable as well. It might not always be desirable though, especially not for larger folios.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 70 lines · David Hildenbrand
Quoted 52 lines · Barry Song
Quoted 51 lines · David Hildenbrand
Quoted 48 lines · Barry Song
From: Barry Song <[email protected]>
On a physical phone, it's sometimes observed that
deferred_splitmTHPs account for over 15% of the total mTHPs. Profiling by Chuanhua indicates that the majority of these originate from the typical fork scenario.
When the child process either execs or exits, the parent process should ideally be able to reuse the entire mTHP. However, the current kernel lacks this capability and instead places the mTHP intosplit_deferred, performing a CoW (Copy-on-Write) on just a single subpage of the mTHP.main() { #define SIZE 1024 * 1024UL void *p = malloc(SIZE); memset(p, 0x11, SIZE); if (fork() == 0) exec(....); /* * this will trigger cow one subpage from * mTHP and put mTHP into split_deferred * list */ *(int *)(p + 10) = 10; printf("done\n"); while(1); }This leads to two significant issues:
* Memory Waste: Before the mTHP is fully split by the shrinker, it wastes memory. In extreme cases, such as with a 64KB mTHP, the memory usage could be 64KB + 60KB until the last subpage is written, at which point the mTHP is freed.
* Fragmentation and Performance Loss: It destroys large folios (negating the performance benefits of CONT-PTE) and fragments memory.
To address this, we should aim to reuse the entire mTHP in such cases.
Hi David,
I’ve renamed
wp_page_reuse()towp_folio_reuse()and added anentirely_reuseargument because I’m not sure if there are still cases where we reuse a subpage within an mTHP. For now, I’m settingentirely_reuseto true only for the newly supported case, while all other cases still get false. Please let me know if this is incorrect—if we don’t reuse subpages at all, we could remove the argument.Quoted 8 lines
Quoted 5 lines
See [1] I sent out this week, that is able to reuse even without scanning page tables. If we find the the folio is exclusive we could try processing surrounding PTEs that map the same folio.
Great! It looks like I missed your patch again. Since you've implemented this in a better way, I’d prefer to use your patchset.
I wouldn't say better, just more universally. And while taking care of properly sync'ing the mapcount vs. refcount :P
Quoted 4 lines
I’m curious about how you're handling
ptep_set_access_flags_nr()or similar things because I couldn’t find the related code in your patch 10/17:
Quoted 7 lines
Quoted 3 lines
[PATCH v1 10/17] mm: COW reuse support for PTE-mapped THP with
CONFIG_MM_IDAm I missing something?
The idea is to keep individual write faults as fast as possible. So the patch set keeps it simple and only reuses a single PTE at a time, setting that one PAE and mapping it writable.
I got your point, thanks! as anyway the mTHP has been exclusive, so the following nr-1 minor page faults will set their particular PTE to writable one by one.
Quoted 5 lines
As the patch states, it might be reasonable to optimize some cases, maybe also only on some architectures. For example to fault-around and map the other ones writable as well. It might not always be desirable though, especially not for larger folios.
as anyway, the mTHP has been entirely exclusive, setting all PTEs directly to writable should help reduce nr - 1 minor page faults and ideally help reduce CONTPTE unfold and fold?
What is the downside to doing that? I also don't think mapping them all together will waste memory?
Quoted 6 lines
--
Cheers,David / dhildenb
Thanks
Barry
Quoted 75 lines · Barry Song
Quoted 74 lines · David Hildenbrand
Quoted 52 lines · Barry Song
Quoted 51 lines · David Hildenbrand
Quoted 48 lines · Barry Song
From: Barry Song <[email protected]>
On a physical phone, it's sometimes observed that
deferred_splitmTHPs account for over 15% of the total mTHPs. Profiling by Chuanhua indicates that the majority of these originate from the typical fork scenario.
When the child process either execs or exits, the parent process should ideally be able to reuse the entire mTHP. However, the current kernel lacks this capability and instead places the mTHP intosplit_deferred, performing a CoW (Copy-on-Write) on just a single subpage of the mTHP.main() { #define SIZE 1024 * 1024UL void *p = malloc(SIZE); memset(p, 0x11, SIZE); if (fork() == 0) exec(....); /* * this will trigger cow one subpage from * mTHP and put mTHP into split_deferred * list */ *(int *)(p + 10) = 10; printf("done\n"); while(1); }This leads to two significant issues:
* Memory Waste: Before the mTHP is fully split by the shrinker, it wastes memory. In extreme cases, such as with a 64KB mTHP, the memory usage could be 64KB + 60KB until the last subpage is written, at which point the mTHP is freed.
* Fragmentation and Performance Loss: It destroys large folios (negating the performance benefits of CONT-PTE) and fragments memory.
To address this, we should aim to reuse the entire mTHP in such cases.
Hi David,
I’ve renamed
wp_page_reuse()towp_folio_reuse()and added anentirely_reuseargument because I’m not sure if there are still cases where we reuse a subpage within an mTHP. For now, I’m settingentirely_reuseto true only for the newly supported case, while all other cases still get false. Please let me know if this is incorrect—if we don’t reuse subpages at all, we could remove the argument.Quoted 8 lines
Quoted 5 lines
See [1] I sent out this week, that is able to reuse even without scanning page tables. If we find the the folio is exclusive we could try processing surrounding PTEs that map the same folio.
Great! It looks like I missed your patch again. Since you've implemented this in a better way, I’d prefer to use your patchset.
I wouldn't say better, just more universally. And while taking care of properly sync'ing the mapcount vs. refcount :P
Quoted 7 lines
I’m curious about how you're handling
ptep_set_access_flags_nr()or similar things because I couldn’t find the related code in your patch 10/17:[PATCH v1 10/17] mm: COW reuse support for PTE-mapped THP with
CONFIG_MM_IDAm I missing something?
Quoted 7 lines
Quoted 3 lines
The idea is to keep individual write faults as fast as possible. So the patch set keeps it simple and only reuses a single PTE at a time, setting that one PAE and mapping it writable.
I got your point, thanks! as anyway the mTHP has been exclusive, so the following nr-1 minor page faults will set their particular PTE to writable one by one.
Yes, assuming you would get these page faults, and assuming you would get them in the near future.
Quoted 10 lines
Quoted 6 lines
As the patch states, it might be reasonable to optimize some cases, maybe also only on some architectures. For example to fault-around and map the other ones writable as well. It might not always be desirable though, especially not for larger folios.
as anyway, the mTHP has been entirely exclusive, setting all PTEs directly to writable should help reduce nr - 1 minor page faults and ideally help reduce CONTPTE unfold and fold?
Yes, doing that on CONTPTE granularity would very likely make sense. For anything bigger than that, I am not sure.
Assuming we have a 1M folio mapped by PTEs. Trying to fault-around in aligned CONTPTE granularity likely makes sense. Bigger than that, I am not convinced.
Quoted 3 lines
What is the downside to doing that? I also don't think mapping them all together will waste memory?
No, it's all about increasing the latency of individual write faults.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 99 lines · David Hildenbrand
Quoted 75 lines · Barry Song
Quoted 74 lines · David Hildenbrand
Quoted 52 lines · Barry Song
Quoted 51 lines · David Hildenbrand
Quoted 48 lines · Barry Song
From: Barry Song <[email protected]>
On a physical phone, it's sometimes observed that
deferred_splitmTHPs account for over 15% of the total mTHPs. Profiling by Chuanhua indicates that the majority of these originate from the typical fork scenario.
When the child process either execs or exits, the parent process should ideally be able to reuse the entire mTHP. However, the current kernel lacks this capability and instead places the mTHP intosplit_deferred, performing a CoW (Copy-on-Write) on just a single subpage of the mTHP.main() { #define SIZE 1024 * 1024UL void *p = malloc(SIZE); memset(p, 0x11, SIZE); if (fork() == 0) exec(....); /* * this will trigger cow one subpage from * mTHP and put mTHP into split_deferred * list */ *(int *)(p + 10) = 10; printf("done\n"); while(1); }This leads to two significant issues:
* Memory Waste: Before the mTHP is fully split by the shrinker, it wastes memory. In extreme cases, such as with a 64KB mTHP, the memory usage could be 64KB + 60KB until the last subpage is written, at which point the mTHP is freed.
* Fragmentation and Performance Loss: It destroys large folios (negating the performance benefits of CONT-PTE) and fragments memory.
To address this, we should aim to reuse the entire mTHP in such cases.
Hi David,
I’ve renamed
wp_page_reuse()towp_folio_reuse()and added anentirely_reuseargument because I’m not sure if there are still cases where we reuse a subpage within an mTHP. For now, I’m settingentirely_reuseto true only for the newly supported case, while all other cases still get false. Please let me know if this is incorrect—if we don’t reuse subpages at all, we could remove the argument.Quoted 8 lines
Quoted 5 lines
See [1] I sent out this week, that is able to reuse even without scanning page tables. If we find the the folio is exclusive we could try processing surrounding PTEs that map the same folio.
Great! It looks like I missed your patch again. Since you've implemented this in a better way, I’d prefer to use your patchset.
I wouldn't say better, just more universally. And while taking care of properly sync'ing the mapcount vs. refcount :P
Quoted 7 lines
I’m curious about how you're handling
ptep_set_access_flags_nr()or similar things because I couldn’t find the related code in your patch 10/17:[PATCH v1 10/17] mm: COW reuse support for PTE-mapped THP with
CONFIG_MM_IDAm I missing something?
Quoted 7 lines
Quoted 3 lines
The idea is to keep individual write faults as fast as possible. So the patch set keeps it simple and only reuses a single PTE at a time, setting that one PAE and mapping it writable.
I got your point, thanks! as anyway the mTHP has been exclusive, so the following nr-1 minor page faults will set their particular PTE to writable one by one.
Yes, assuming you would get these page faults, and assuming you would get them in the near future.
Quoted 10 lines
Quoted 6 lines
As the patch states, it might be reasonable to optimize some cases, maybe also only on some architectures. For example to fault-around and map the other ones writable as well. It might not always be desirable though, especially not for larger folios.
as anyway, the mTHP has been entirely exclusive, setting all PTEs directly to writable should help reduce nr - 1 minor page faults and ideally help reduce CONTPTE unfold and fold?
Quoted 7 lines
Yes, doing that on CONTPTE granularity would very likely make sense. For anything bigger than that, I am not sure.
Assuming we have a 1M folio mapped by PTEs. Trying to fault-around in aligned CONTPTE granularity likely makes sense. Bigger than that, I am not convinced.
I see. maybe we can have something like:
static bool pte_fault_around_estimate(int nr)
{
if (nr / arch_batched_ptes_nr() < 16)
return true;
return false;
}if (pte_fault_around_estimate(folio_nr_pages(folio)))
set all ptes;
for arm64, arch_batched_ptes_nr() == 16. for
arch without cont-pte or similar things,arch_batched_ptes_nr() == 1.
Just some rough ideas; all the naming might be quite messy.
at least, we won't lose the benefit of reduced TLB miss before all nr_pages are written for aarch64 :-)
Quoted 6 lines
Quoted 3 lines
What is the downside to doing that? I also don't think mapping them all together will waste memory?
No, it's all about increasing the latency of individual write faults.
i see, i assume it won't be worse than the current case where we have to allocate small folios and copy? and folio allocation can even further incur direct reclamation?
Quoted 5 lines
--
Cheers,David / dhildenb
Thanks
Barry
Quoted 118 lines · Barry Song
Quoted 99 lines · David Hildenbrand
Quoted 75 lines · Barry Song
Quoted 74 lines · David Hildenbrand
Quoted 52 lines · Barry Song
Quoted 51 lines · David Hildenbrand
Quoted 48 lines · Barry Song
From: Barry Song <[email protected]>
On a physical phone, it's sometimes observed that
deferred_splitmTHPs account for over 15% of the total mTHPs. Profiling by Chuanhua indicates that the majority of these originate from the typical fork scenario.
When the child process either execs or exits, the parent process should ideally be able to reuse the entire mTHP. However, the current kernel lacks this capability and instead places the mTHP intosplit_deferred, performing a CoW (Copy-on-Write) on just a single subpage of the mTHP.main() { #define SIZE 1024 * 1024UL void *p = malloc(SIZE); memset(p, 0x11, SIZE); if (fork() == 0) exec(....); /* * this will trigger cow one subpage from * mTHP and put mTHP into split_deferred * list */ *(int *)(p + 10) = 10; printf("done\n"); while(1); }This leads to two significant issues:
* Memory Waste: Before the mTHP is fully split by the shrinker, it wastes memory. In extreme cases, such as with a 64KB mTHP, the memory usage could be 64KB + 60KB until the last subpage is written, at which point the mTHP is freed.
* Fragmentation and Performance Loss: It destroys large folios (negating the performance benefits of CONT-PTE) and fragments memory.
To address this, we should aim to reuse the entire mTHP in such cases.
Hi David,
I’ve renamed
wp_page_reuse()towp_folio_reuse()and added anentirely_reuseargument because I’m not sure if there are still cases where we reuse a subpage within an mTHP. For now, I’m settingentirely_reuseto true only for the newly supported case, while all other cases still get false. Please let me know if this is incorrect—if we don’t reuse subpages at all, we could remove the argument.Quoted 8 lines
Quoted 5 lines
See [1] I sent out this week, that is able to reuse even without scanning page tables. If we find the the folio is exclusive we could try processing surrounding PTEs that map the same folio.
Great! It looks like I missed your patch again. Since you've implemented this in a better way, I’d prefer to use your patchset.
I wouldn't say better, just more universally. And while taking care of properly sync'ing the mapcount vs. refcount :P
Quoted 7 lines
I’m curious about how you're handling
ptep_set_access_flags_nr()or similar things because I couldn’t find the related code in your patch 10/17:[PATCH v1 10/17] mm: COW reuse support for PTE-mapped THP with
CONFIG_MM_IDAm I missing something?
Quoted 7 lines
Quoted 3 lines
The idea is to keep individual write faults as fast as possible. So the patch set keeps it simple and only reuses a single PTE at a time, setting that one PAE and mapping it writable.
I got your point, thanks! as anyway the mTHP has been exclusive, so the following nr-1 minor page faults will set their particular PTE to writable one by one.
Yes, assuming you would get these page faults, and assuming you would get them in the near future.
Quoted 10 lines
Quoted 6 lines
As the patch states, it might be reasonable to optimize some cases, maybe also only on some architectures. For example to fault-around and map the other ones writable as well. It might not always be desirable though, especially not for larger folios.
as anyway, the mTHP has been entirely exclusive, setting all PTEs directly to writable should help reduce nr - 1 minor page faults and ideally help reduce CONTPTE unfold and fold?
Quoted 7 lines
Yes, doing that on CONTPTE granularity would very likely make sense. For anything bigger than that, I am not sure.
Assuming we have a 1M folio mapped by PTEs. Trying to fault-around in aligned CONTPTE granularity likely makes sense. Bigger than that, I am not convinced.
I see. maybe we can have something like:
static bool
pte_fault_around_estimate(int nr){ if (nr / arch_batched_ptes_nr() < 16) return true; return false; }
Quoted 6 lines
if (pte_fault_around_estimate(folio_nr_pages(folio)))
set all ptes;for arm64,
arch_batched_ptes_nr()== 16. for
arch without cont-pte or similar things,arch_batched_ptes_nr()== 1.
Yes, something like that would be my take.
After we know that we can reuse the large folio, we'll try scanning starting from the aligned PTE. If we find that we can batch, we'll batch that part. Otherwise we'll simply fallback to a single one.
Handling batching across VMAs is a bit harder. We might be able to batch, or might not ... We could have the CONT_PTE bit set across VMAs, but might not necessarily be able to batch (e.g., some VMAs are read-only).
Quoted 10 lines
Just some rough ideas; all the naming might be quite messy.
at least, we won't lose the benefit of reduced TLB miss before all
nr_pagesare written for aarch64 :-)Quoted 4 lines
Quoted 3 lines
What is the downside to doing that? I also don't think mapping them all together will waste memory?
Quoted 6 lines
Quoted 2 lines
No, it's all about increasing the latency of individual write faults.
i see, i assume it won't be worse than the current case where we have to allocate small folios and copy? and folio allocation can even further incur direct reclamation?
Yes, it would certainly better than what we currently have. Almost everything would likely be better than what we currently have. :)
Signature · 3 lines
Cheers, David / dhildenb
Quoted 8 lines
mm/memory.c +7
-0+ idx =folio_page_idx (folio, vmf->page); + folio_start = address - idx *PAGE_SIZE ; + folio_end = folio_start + nr *PAGE_SIZE ; + +if (unlikely (folio_start <max (address &PMD_MASK , vma->vm_start))) +return false; +if (unlikely (folio_end >pmd_addr_end (address, vma->vm_end)))
mm/memory.c static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio) · line 3612
Quoted 10 lines
mm/memory.c +10
-0+return false; + folio_ptep = vmf->pte - idx; + folio_pte =ptep_get (folio_ptep); +if (!pte_present (folio_pte) ||pte_pfn (folio_pte) !=folio_pfn (folio)) +return false; +if (folio_pte_batch (folio, folio_start, folio_ptep, folio_pte, nr,0 , +NULL ,NULL ,NULL ) != nr) +return false; +if (folio_mapcount (folio) != nr) +return false;
BTW, you're not checking against the refcount (and it's all a bit racy on concurrent unmapping!). So you're re-introducing the vmsplice child->parent attak.
Signature · 3 lines
Cheers, David / dhildenb
Quoted 20 lines · David Hildenbrand
Quoted 9 lines
mm/memory.c +7
-0+ idx =folio_page_idx (folio, vmf->page); + folio_start = address - idx *PAGE_SIZE ; + folio_end = folio_start + nr *PAGE_SIZE ; + +if (unlikely (folio_start <max (address &PMD_MASK , vma->vm_start))) +return false; +if (unlikely (folio_end >pmd_addr_end (address, vma->vm_end)))mm/memory.c static vm_fault_t wp_page_shared(struct vm_fault *vmf, struct folio *folio) · line 3612
Quoted 10 lines
mm/memory.c +10
-0+return false; + folio_ptep = vmf->pte - idx; + folio_pte =ptep_get (folio_ptep); +if (!pte_present (folio_pte) ||pte_pfn (folio_pte) !=folio_pfn (folio)) +return false; +if (folio_pte_batch (folio, folio_start, folio_ptep, folio_pte, nr,0 , +NULL ,NULL ,NULL ) != nr) +return false; +if (folio_mapcount (folio) != nr) +return false;
Quoted 3 lines
BTW, you're not checking against the refcount (and it's all a bit racy on concurrent unmapping!). So you're re-introducing the vmsplice
child->parentattak.
i don't quite understand this, you mean the below is not enough?
- if (folio_test_ksm(folio) || folio_ref_count(folio) > 3)
+ if (folio_test_ksm(folio) || folio_ref_count(folio) > 2 + nr)
return false;
if (!folio_test_lru(folio))
/*
@@ -3591,13 +3627,13 @@ static bool wp_can_reuse_anon_folio(struct folio *folio,
* remote LRU caches or references to LRU folios.
*/
lru_add_drain();
- if (folio_ref_count(folio) > 1 + folio_test_swapcache(folio))
+ if (folio_ref_count(folio) > nr + folio_test_swapcache(folio))
return false;
if (!folio_trylock(folio))
return false;
if (folio_test_swapcache(folio))
folio_free_swap(folio);
- if (folio_test_ksm(folio) || folio_ref_count(folio) != 1) {
+ if (folio_test_ksm(folio) || folio_ref_count(folio) != nr) {
folio_unlock(folio);
return false;Quoted 6 lines
--
Cheers,David / dhildenb
Quoted 21 lines · Barry Song
Quoted 20 lines · David Hildenbrand
Quoted 19 lines
mm/memory.c +17
-0+ idx =folio_page_idx (folio, vmf->page); + folio_start = address - idx *PAGE_SIZE ; + folio_end = folio_start + nr *PAGE_SIZE ; + +if (unlikely (folio_start <max (address &PMD_MASK , vma->vm_start))) +return false; +if (unlikely (folio_end >pmd_addr_end (address, vma->vm_end))) +return false; + folio_ptep = vmf->pte - idx; + folio_pte =ptep_get (folio_ptep); +if (!pte_present (folio_pte) ||pte_pfn (folio_pte) !=folio_pfn (folio)) +return false; +if (folio_pte_batch (folio, folio_start, folio_ptep, folio_pte, nr,0 , +NULL ,NULL ,NULL ) != nr) +return false; +if (folio_mapcount (folio) != nr) +return false;
Quoted 6 lines
Quoted 3 lines
BTW, you're not checking against the refcount (and it's all a bit racy on concurrent unmapping!). So you're re-introducing the vmsplice
child->parentattak.i don't quite understand this, you mean the below is not enough?
Ah! You use the fallthrough, sorry I missed that!
You're not handling the swapcache references "correctly" (would have one reference per page), but the final check would be correct.
Yes, that should not allow for false positives here.
Signature · 3 lines
Cheers, David / dhildenb
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Barry,
[This is a private test report for your RFC patch.] kernel test robot noticed the following build errors:
[auto build test ERROR on akpm-mm/mm-everything]
url: https://github.com/intel-lab-lkp/linux/commits/Barry-Song/mm-entirely-reuse-the-whole-anon-mTHP-in-do_wp_page/20240831-172425 base: https://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm.git mm-everything patch link: https://lore.kernel.org/r/20240831092339.66085-1-21cnbao%40gmail.com patch subject: [PATCH RFC] mm: entirely reuse the whole anon mTHP in do_wp_page config: parisc-allnoconfig (https://download.01.org/0day-ci/archive/20240903/[email protected]/config) compiler: hppa-linux-gcc (GCC) 14.1.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20240903/[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 >>):
In file included from include/linux/pgtable.h:6,
from arch/parisc/include/asm/io.h:6,
from include/linux/io.h:14,
from include/linux/irq.h:20,
from arch/parisc/include/asm/hardirq.h:13,
from include/linux/hardirq.h:11,
from include/linux/interrupt.h:11,
from include/linux/kernel_stat.h:8,
from mm/memory.c:43:
mm/memory.c: In function 'wp_folio_reuse':
>> mm/memory.c:3251:70: error: invalid operands to binary + (have 'pte_t' and 'int')
3251 | update_mmu_cache_range(vmf, vma, start, ptep + i, 1);
| ^ arch/parisc/include/asm/pgtable.h:406:74: note: in definition of macro 'update_mmu_cache_range'
406 | #define update_mmu_cache_range(vmf, vma, addr, ptep, nr) __update_cache(*ptep)
| ^~~~
vim +3251 mm/memory.c
3207
3208 /*
3209 * Handle write page faults for pages that can be reused in the current vma
3210 *
3211 * This can happen either due to the mapping being with the VM_SHARED flag,
3212 * or due to us being the last reference standing to the page. In either
3213 * case, all we need to do here is to mark the page as writable and update
3214 * any related book-keeping.
3215 * If entirely_reuse is true, we are reusing the whole large folio; otherwise,
3216 * we are reusing a subpage even though folio might be large one.
3217 */
3218 static inline void wp_folio_reuse(struct vm_fault *vmf, struct folio *folio,
3219 bool entirely_reuse)
3220 __releases(vmf->ptl)
3221 {
3222 unsigned long idx = entirely_reuse ? folio_page_idx(folio, vmf->page) : 0;
3223 int nr = entirely_reuse ? folio_nr_pages(folio) : 1;
3224 unsigned long start = vmf->address - idx * PAGE_SIZE;
3225 unsigned long end = start + nr * PAGE_SIZE;
3226 struct vm_area_struct *vma = vmf->vma;
3227 pte_t *ptep = vmf->pte - idx;
3228 pte_t entry;
3229
3230 VM_BUG_ON(!(vmf->flags & FAULT_FLAG_WRITE));
3231 VM_WARN_ON(is_zero_pfn(pte_pfn(vmf->orig_pte)));
3232
3233 if (folio) {
3234 VM_BUG_ON(folio_test_anon(folio) &&
3235 !PageAnonExclusive(vmf->page));
3236 /*
3237 * Clear the folio's cpupid information as the existing
3238 * information potentially belongs to a now completely
3239 * unrelated process.
3240 */
3241 folio_xchg_last_cpupid(folio, (1 << LAST_CPUPID_SHIFT) - 1);
3242 }
3243
3244 flush_cache_range(vma, start, end);
3245 for (int i = 0; i < nr; i++) {
3246 entry = ptep_get(ptep + i);
3247 entry = pte_mkyoung(entry);
3248 entry = maybe_mkwrite(pte_mkdirty(entry), vma);
3249 if (ptep_set_access_flags(vma, start + i * PAGE_SIZE,
3250 ptep + i, entry, 1))
> 3251 update_mmu_cache_range(vmf, vma, start, ptep + i, 1);
3252 }
3253 pte_unmap_unlock(vmf->pte, vmf->ptl);
3254 count_vm_event(PGREUSE);
3255 }
3256
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki