mm: batched unmap lazyfree large folios during reclamation
Subsystems (6)
v2 cover mm: batched unmap lazyfree large folios during reclamation
From: Barry Song <[email protected]>
Commit 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()") prevents the splitting of MADV_FREE'd THP in madvise.c.
However, those folios are still added to the deferred_split list in try_to_unmap_one() because we are unmapping PTEs and removing rmap entries one by one.
Firstly, this has rendered the following counter somewhat confusing, /sys/kernel/mm/transparent_hugepage/hugepages-size/stats/split_deferred The split_deferred counter was originally designed to track operations such as partial unmap or madvise of large folios. However, in practice, most split_deferred cases arise from memory reclamation of aligned lazyfree mTHPs as observed by Tangquan. This discrepancy has made the split_deferred counter highly misleading.
Secondly, this approach is slow because it requires iterating through each PTE and removing the rmap one by one for a large folio. In fact, all PTEs of a pte-mapped large folio should be unmapped at once, and the entire folio should be removed from the rmap as a whole.
Thirdly, it also increases the risk of a race condition where lazyfree folios are incorrectly set back to swapbacked, as a speculative folio_get may occur in the shrinker's callback.deferred_split_scan() might call folio_try_get(folio) since we have added the folio to split_deferred list while removing rmap for the 1st subpage, and while we are scanning the 2nd to nr_pages PTEs of this folio in try_to_unmap_one(), the entire mTHP could be transitioned back to swap-backed because the reference count is incremented, which can make "ref_count == 1 + map_count" within try_to_unmap_one() false.
/*
* The only page refs must be one from isolation
* plus the rmap(s) (dropped by discard:).
*/
if (ref_count == 1 + map_count &&
(!folio_test_dirty(folio) ||
...
(vma->vm_flags & VM_DROPPABLE))) {
dec_mm_counter(mm, MM_ANONPAGES);
goto discard;
}This patchset resolves the issue by marking only genuinely dirty folios as swap-backed, as suggested by David, and transitioning to batched unmapping of entire folios in try_to_unmap_one(). Consequently, the deferred_split count drops to zero, and memory reclamation performance improves significantly — reclaiming 64KiB lazyfree large folios is now 2.5x faster(The specific data is embedded in the changelog of patch 3/4).
By the way, while the patchset is primarily aimed at PTE-mapped large folios, Baolin and Lance also found that try_to_unmap_one() handles lazyfree redirtied PMD-mapped large folios inefficiently — it splits the PMD into PTEs and iterates over them. This patchset removes the unnecessary splitting, enabling us to skip redirtied PMD-mapped large folios 3.5X faster during memory reclamation. (The specific data can be found in the changelog of patch 4/4).
-v2:
* describle backgrounds, problems more clearly in cover-letter per Lorenzo Stoakes;
* also handle redirtied pmd-mapped large folios per Baolin and Lance;
* handle some corner cases such as HWPOSION, pte_unused;
* riscv and x86 build issues.
-v1:
https://lore.kernel.org/linux-mm/[email protected]/
Barry Song (4):
mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one
mm: Support tlbbatch flush for a range of PTEs
mm: Support batched unmap for lazyfree large folios during reclamation
mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap
arch/arm64/include/asm/tlbflush.h | 26 +++---- arch/arm64/mm/contpte.c | 2 +- arch/riscv/include/asm/tlbflush.h | 3 +- arch/riscv/mm/tlbflush.c | 3 +- arch/x86/include/asm/tlbflush.h | 3 +- mm/huge_memory.c | 17 ++++- mm/rmap.c | 112 ++++++++++++++++++++---------- 7 files changed, 111 insertions(+), 55 deletions(-)
Signature · 1 line
2.39.3 (Apple Git-146)
PATCH v2 1/4 mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one
From: Barry Song <[email protected]>
The refcount may be temporarily or long-term increased, but this does not change the fundamental nature of the folio already being lazy- freed. Therefore, we only reset 'swapbacked' when we are certain the folio is dirty and not droppable.
--- mm/rmap.c | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-)
mm/rmap.c +22 -27
@@ -1868,34 +1868,29 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,*/ smp_rmb (); -/* -* The only page refs must be one from isolation -* plus the rmap(s) (dropped by discard:). -*/ -if (ref_count ==1 + map_count && - (!folio_test_dirty (folio) || -/* -* Unlike MADV_FREE mappings, VM_DROPPABLE -* ones can be dropped even if they've -* been dirtied. -*/ - (vma->vm_flags &VM_DROPPABLE ))) { -dec_mm_counter (mm,MM_ANONPAGES ); - goto discard; - } - -/* -* If the folio was redirtied, it cannot be -* discarded. Remap the page to page table. -*/ -set_pte_at (mm, address, pvmw.pte, pteval); -/* -* Unlike MADV_FREE mappings, VM_DROPPABLE ones -* never get swap backed on failure to drop. -*/ -if (!(vma->vm_flags &VM_DROPPABLE )) +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +/* +* redirtied either using the page table or a previously +* obtained GUP reference. +*/ +set_pte_at (mm, address, pvmw.pte, pteval);folio_set_swapbacked (folio); - goto walk_abort; + goto walk_abort; + }else if (ref_count !=1 + map_count) { +/* +* Additional reference. Could be a GUP reference or any +* speculative reference. GUP users must mark the folio +* dirty if there was a modification. This folio cannot be +* reclaimed right now either way, so act just like nothing +* happened. +* We'll come back here later and detect if the folio was +* dirtied when the additional reference is gone. +*/ +set_pte_at (mm, address, pvmw.pte, pteval); + goto walk_abort; + } +dec_mm_counter (mm,MM_ANONPAGES ); + goto discard; }if (swap_duplicate (entry) <0 ) {
Signature · 1 line
2.39.3 (Apple Git-146)
Re: [PATCH v2 1/4] mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one
Quoted 10 lines · Barry Song
From: Barry Song <[email protected]>
The refcount may be temporarily or long-term increased, but this does not change the fundamental nature of the folio already being lazy- freed. Therefore, we only reset 'swapbacked' when we are certain the folio is dirty and not droppable.
Suggested-by David Hildenbrand <[email protected]>Signed-off-by Barry Song <[email protected]>---
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v2 1/4] mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one
Quoted 8 lines · David Hildenbrand
Quoted 7 lines · Barry Song
From: Barry Song <[email protected]>
The refcount may be temporarily or long-term increased, but this does not change the fundamental nature of the folio already being lazy- freed. Therefore, we only reset 'swapbacked' when we are certain the folio is dirty and not droppable.
Quoted 6 lines
Quoted 3 lines
Suggested-by David Hildenbrand <[email protected]>Signed-off-by Barry Song <[email protected]>---
Ah, should this have a Fixes: ?
Because of a speculative reference, we might not reclaim MADV_FREE folios as we silently mark them as swapbacked again, which sounds fairly wrong.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v2 1/4] mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one
Quoted 10 lines · David Hildenbrand
Quoted 8 lines · David Hildenbrand
Quoted 7 lines · Barry Song
From: Barry Song <[email protected]>
The refcount may be temporarily or long-term increased, but this does not change the fundamental nature of the folio already being lazy- freed. Therefore, we only reset 'swapbacked' when we are certain the folio is dirty and not droppable.
Quoted 6 lines
Quoted 6 lines
Quoted 3 lines
Suggested-by David Hildenbrand <[email protected]>Signed-off-by Barry Song <[email protected]>---
Thanks!
Quoted 2 lines
Ah, should this have a Fixes: ?
That makes sense to me.
Quoted 5 lines
Because of a speculative reference, we might not reclaim
MADV_FREEfolios as we silently mark them as swapbacked again, which sounds fairly wrong.
I assume the tag should be applied to Mauricio's commit 6c8e2a256915a2 ("mm:
fix race between MADV_FREE reclaim and blkdev direct IO read") and also Mauricio is CC'ed.
Quoted 5 lines
--
Cheers,David / dhildenb
Thanks
Barry
Re: [PATCH v2 1/4] mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one
In reply to Barry SongQuoted 9 lines · Barry Song
From: Barry Song <[email protected]>
The refcount may be temporarily or long-term increased, but this does not change the fundamental nature of the folio already being lazy- freed. Therefore, we only reset 'swapbacked' when we are certain the folio is dirty and not droppable.
Suggested-by David Hildenbrand <[email protected]>Signed-off-by Barry Song <[email protected]>
LGTM. Feel free to add:
Lance Yang
Reviewed-byRe: [PATCH v2 1/4] mm: Set folio swapbacked iff folios are dirty in try_to_unmap_one
Quoted 11 lines · Baolin Wang
Quoted 7 lines · Barry Song
From: Barry Song <[email protected]>
The refcount may be temporarily or long-term increased, but this does not change the fundamental nature of the folio already being lazy- freed. Therefore, we only reset 'swapbacked' when we are certain the folio is dirty and not droppable.
Quoted 5 lines
Quoted 2 lines
Suggested-by David Hildenbrand <[email protected]>Signed-off-by Barry Song <[email protected]>LGTM. Feel free to add:
Reviewed-by Baolin Wang <[email protected]>
LGTM!
Thanks,
Lance
PATCH v2 2/4 mm: Support tlbbatch flush for a range of PTEs
In reply to Barry SongFrom: Barry Song <[email protected]>
This is a preparatory patch to support batch PTE unmapping in try_to_unmap_one. It first introduces range handling for tlbbatch flush. Currently, the range is always set to the size of PAGE_SIZE.
--- arch/arm64/include/asm/tlbflush.h | 26 ++++++++++++++------------ arch/arm64/mm/contpte.c | 2 +- arch/riscv/include/asm/tlbflush.h | 3 ++- arch/riscv/mm/tlbflush.c | 3 ++- arch/x86/include/asm/tlbflush.h | 3 ++- mm/rmap.c | 12 +++++++----- 6 files changed, 28 insertions(+), 21 deletions(-)
arch/arm64/include/asm/tlbflush.h +14 -12
@@ -322,13 +322,6 @@ static inline bool arch_tlbbatch_should_defer(struct mm_struct *mm)return true; } -static inline void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch, -struct mm_struct *mm, -unsigned long uaddr) -{ -__flush_tlb_page_nosync (mm, uaddr); -} -/* * If mprotect/munmap/etc occurs during TLB batched flushing, we need to * synchronise all the TLBI issued with a DSB to avoid the race mentioned in
@@ -448,7 +441,7 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,return false; } -static inline void __flush_tlb_range_nosync (struct vm_area_struct *vma, +static inline void __flush_tlb_range_nosync (struct mm_struct *mm,unsigned long start,unsigned long end,unsigned long stride,bool last_level,int tlb_level)
@@ -460,12 +453,12 @@ static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma, pages = (end - start) >>PAGE_SHIFT ;if (__flush_tlb_range_limit_excess (start, end, pages, stride)) { -flush_tlb_mm (vma->vm_mm); +flush_tlb_mm (mm);return ; }dsb (ishst); - asid =ASID (vma->vm_mm); + asid =ASID (mm);if (last_level) __flush_tlb_range_op(vale1is, start, pages, stride, asid,
@@ -474,7 +467,7 @@ static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma,__flush_tlb_range_op (vae1is, start, pages, stride, asid, tlb_level, true,lpa2_is_enabled ()); -mmu_notifier_arch_invalidate_secondary_tlbs (vma->vm_mm, start, end); +mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); } static inline void __flush_tlb_range(struct vm_area_struct *vma,
@@ -482,7 +475,7 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, unsigned long stride, bool last_level, int tlb_level) { -__flush_tlb_range_nosync (vma ,start ,end ,stride , + __flush_tlb_range_nosync(vma->vm_mm, start, end, stride,last_level ,tlb_level );dsb (ish); }
@@ -533,6 +526,15 @@ static inline void __flush_tlb_kernel_pgtable(unsigned long kaddr)dsb (ish);isb (); } + +static inline void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch, +struct mm_struct *mm, +unsigned long uaddr, +unsigned long size) +{ +__flush_tlb_range_nosync (mm, uaddr, uaddr + size, +PAGE_SIZE , true,3 ); +}#endif #endif
arch/arm64/mm/contpte.c +1 -1
@@ -335,7 +335,7 @@ int contpte_ptep_clear_flush_young(struct vm_area_struct *vma,* eliding the trailing DSB applies here. */ addr =ALIGN_DOWN (addr,CONT_PTE_SIZE ); -__flush_tlb_range_nosync (vma, addr, addr +CONT_PTE_SIZE , +__flush_tlb_range_nosync (vma->vm_mm, addr, addr +CONT_PTE_SIZE ,PAGE_SIZE , true,3 ); }
arch/riscv/include/asm/tlbflush.h +2 -1
@@ -61,7 +61,8 @@ void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,bool arch_tlbbatch_should_defer (struct mm_struct *mm);void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch,struct mm_struct *mm, -unsigned long uaddr); +unsigned long uaddr, +unsigned long size);void arch_flush_tlb_batched_pending (struct mm_struct *mm);void arch_tlbbatch_flush (struct arch_tlbflush_unmap_batch *batch);
arch/riscv/mm/tlbflush.c +2 -1
@@ -187,7 +187,8 @@ bool arch_tlbbatch_should_defer(struct mm_struct *mm)void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch,struct mm_struct *mm, -unsigned long uaddr) +unsigned long uaddr, +unsigned long size) {cpumask_or (&batch->cpumask, &batch->cpumask,mm_cpumask (mm)); }
arch/x86/include/asm/tlbflush.h +2 -1
@@ -279,7 +279,8 @@ static inline u64 inc_mm_tlb_gen(struct mm_struct *mm)static inline void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch,struct mm_struct *mm, -unsigned long uaddr) +unsigned long uaddr, +unsigned long size) {inc_mm_tlb_gen (mm);cpumask_or (&batch->cpumask, &batch->cpumask,mm_cpumask (mm));
mm/rmap.c +7 -5
@@ -672,7 +672,8 @@ void try_to_unmap_flush_dirty(void) (TLB_FLUSH_BATCH_PENDING_MASK / 2)static void set_tlb_ubc_flush_pending (struct mm_struct *mm,pte_t pteval, -unsigned long uaddr) +unsigned long uaddr, +unsigned long size) {struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc;int batch;
@@ -681,7 +682,7 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval,if (!pte_accessible (mm, pteval))return ; -arch_tlbbatch_add_pending (&tlb_ubc->arch, mm, uaddr); +arch_tlbbatch_add_pending (&tlb_ubc->arch, mm, uaddr, size); tlb_ubc->flush_required = true;/*
@@ -757,7 +758,8 @@ void flush_tlb_batched_pending(struct mm_struct *mm) }#else static void set_tlb_ubc_flush_pending (struct mm_struct *mm,pte_t pteval, -unsigned long uaddr) +unsigned long uaddr, +unsigned long size) { }
@@ -1792,7 +1794,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,*/ pteval =ptep_get_and_clear (mm, address, pvmw.pte); -set_tlb_ubc_flush_pending (mm, pteval, address); +set_tlb_ubc_flush_pending (mm, pteval, address,PAGE_SIZE ); }else { pteval =ptep_clear_flush (vma, address, pvmw.pte); }
@@ -2164,7 +2166,7 @@ static bool try_to_migrate_one(struct folio *folio, struct vm_area_struct *vma,*/ pteval =ptep_get_and_clear (mm, address, pvmw.pte); -set_tlb_ubc_flush_pending (mm, pteval, address); +set_tlb_ubc_flush_pending (mm, pteval, address,PAGE_SIZE ); }else { pteval =ptep_clear_flush (vma, address, pvmw.pte); }
Signature · 1 line
2.39.3 (Apple Git-146)
Re: [PATCH v2 2/4] mm: Support tlbbatch flush for a range of PTEs
Quoted 29 lines · Barry Song
From: Barry Song <[email protected]>
This is a preparatory patch to support batch PTE unmapping in
try_to_unmap_one. It first introduces range handling fortlbbatchflush. Currently, the range is always set to the size ofPAGE_SIZE.Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc Dave Hansen <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Anshuman Khandual <[email protected]>Cc Ryan Roberts <[email protected]>Cc Shaoqin Huang <[email protected]>Cc Gavin Shan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Mark Rutland <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lance Yang <[email protected]>Cc "Kirill A. Shutemov" <[email protected]>Cc Yosry Ahmed <[email protected]>Cc Paul Walmsley <[email protected]>Cc Palmer Dabbelt <[email protected]>Cc Albert Ou <[email protected]>Cc Yicong Yang <[email protected]>Signed-off-by Barry Song <[email protected]>
Quoted 8 lines
--- arch/arm64/include/asm/tlbflush.h | 26 ++++++++++++++------------ arch/arm64/mm/contpte.c | 2 +- arch/riscv/include/asm/tlbflush.h | 3 ++- arch/riscv/mm/tlbflush.c | 3 ++- arch/x86/include/asm/tlbflush.h | 3 ++- mm/rmap.c | 12 +++++++----- 6 files changed, 28 insertions(+), 21 deletions(-)
The arm64 bits look correct to me:
Will
Re: [PATCH v2 2/4] mm: Support tlbbatch flush for a range of PTEs
In reply to Barry SongQuoted 6 lines · Barry Song
From: Barry Song <[email protected]>
This is a preparatory patch to support batch PTE unmapping in
try_to_unmap_one. It first introduces range handling fortlbbatchflush. Currently, the range is always set to the size ofPAGE_SIZE.
You could have spelled out why you perform the VMA -> MM change.
Quoted 126 lines
Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc Dave Hansen <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Anshuman Khandual <[email protected]>Cc Ryan Roberts <[email protected]>Cc Shaoqin Huang <[email protected]>Cc Gavin Shan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Mark Rutland <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lance Yang <[email protected]>Cc "Kirill A. Shutemov" <[email protected]>Cc Yosry Ahmed <[email protected]>Cc Paul Walmsley <[email protected]>Cc Palmer Dabbelt <[email protected]>Cc Albert Ou <[email protected]>Cc Yicong Yang <[email protected]>Signed-off-by Barry Song <[email protected]>--- arch/arm64/include/asm/tlbflush.h | 26 ++++++++++++++------------ arch/arm64/mm/contpte.c | 2 +- arch/riscv/include/asm/tlbflush.h | 3 ++- arch/riscv/mm/tlbflush.c | 3 ++- arch/x86/include/asm/tlbflush.h | 3 ++- mm/rmap.c | 12 +++++++----- 6 files changed, 28 insertions(+), 21 deletions(-)arch/arm64/include/asm/tlbflush.h +14
-12@@ -322,13 +322,6 @@ static inline bool arch_tlbbatch_should_defer(struct mm_struct *mm)return true; } -static inline void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch, -struct mm_struct *mm, -unsigned long uaddr) -{ -__flush_tlb_page_nosync (mm, uaddr); -} -/* * If mprotect/munmap/etc occurs during TLB batched flushing, we need to * synchronise all the TLBI issued with a DSB to avoid the race mentioned in @@ -448,7 +441,7 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,return false; } -static inline void __flush_tlb_range_nosync (struct vm_area_struct *vma, +static inline void __flush_tlb_range_nosync (struct mm_struct *mm,unsigned long start,unsigned long end,unsigned long stride,bool last_level,int tlb_level)@@ -460,12 +453,12 @@ static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma, pages = (end - start) >>PAGE_SHIFT ;if (__flush_tlb_range_limit_excess (start, end, pages, stride)) { -flush_tlb_mm (vma->vm_mm); +flush_tlb_mm (mm);return ; }dsb (ishst); - asid =ASID (vma->vm_mm); + asid =ASID (mm);if (last_level) __flush_tlb_range_op(vale1is, start, pages, stride, asid,@@ -474,7 +467,7 @@ static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma,__flush_tlb_range_op (vae1is, start, pages, stride, asid, tlb_level, true,lpa2_is_enabled ()); -mmu_notifier_arch_invalidate_secondary_tlbs (vma->vm_mm, start, end); +mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); } static inline void __flush_tlb_range(struct vm_area_struct *vma,@@ -482,7 +475,7 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, unsigned long stride, bool last_level, int tlb_level) { -__flush_tlb_range_nosync (vma ,start ,end ,stride , + __flush_tlb_range_nosync(vma->vm_mm, start, end, stride,last_level ,tlb_level );dsb (ish); }@@ -533,6 +526,15 @@ static inline void __flush_tlb_kernel_pgtable(unsigned long kaddr)dsb (ish);isb (); } + +static inline void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch, +struct mm_struct *mm, +unsigned long uaddr, +unsigned long size) +{ +__flush_tlb_range_nosync (mm, uaddr, uaddr + size, +PAGE_SIZE , true,3 ); +}#endif #endif arch/arm64/mm/contpte.c +1
-1@@ -335,7 +335,7 @@ int contpte_ptep_clear_flush_young(struct vm_area_struct *vma,* eliding the trailing DSB applies here. */ addr =ALIGN_DOWN (addr,CONT_PTE_SIZE ); -__flush_tlb_range_nosync (vma, addr, addr +CONT_PTE_SIZE , +__flush_tlb_range_nosync (vma->vm_mm, addr, addr +CONT_PTE_SIZE ,PAGE_SIZE , true,3 ); }arch/riscv/include/asm/tlbflush.h +0
-0
arch/riscv/include/asm/tlbflush.h void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start, · line 65
Quoted 6 lines
arch/riscv/include/asm/tlbflush.h +2
-1@@ -61,7 +61,8 @@ void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,bool arch_tlbbatch_should_defer (struct mm_struct *mm);void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch,struct mm_struct *mm, -unsigned long uaddr); +unsigned long uaddr, +unsigned long size);
While we're at it, can we just convert this to the "two tabs" indentation starting on second parameter line" way of doing things? Same for all other cases. (at least in core code, if some arch wants their own weird rules on how to handle these things)
[...]
Quoted 12 lines
arch/riscv/include/asm/tlbflush.h +1
-1struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc;int batch;@@ -681,7 +682,7 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval,if (!pte_accessible (mm, pteval))return ; -arch_tlbbatch_add_pending (&tlb_ubc->arch, mm, uaddr); +arch_tlbbatch_add_pending (&tlb_ubc->arch, mm, uaddr, size); tlb_ubc->flush_required = true;/*
arch/riscv/include/asm/tlbflush.h void flush_tlb_batched_pending(struct mm_struct *mm) · line 762
Quoted 6 lines
arch/riscv/include/asm/tlbflush.h +2
-1@@ -757,7 +758,8 @@ void flush_tlb_batched_pending(struct mm_struct *mm) }#else static void set_tlb_ubc_flush_pending (struct mm_struct *mm,pte_t pteval, -unsigned long uaddr) +unsigned long uaddr, +unsigned long size)
I'll note that mist tlb functions seem to consume start+end instead of start+size, like
flush_tlb_mm_range()flush_tlb_kernel_range()
So I'm wondering if this should be start+end instead of uaddr+size.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v2 2/4] mm: Support tlbbatch flush for a range of PTEs
Quoted 10 lines · David Hildenbrand
Quoted 6 lines · Barry Song
From: Barry Song <[email protected]>
This is a preparatory patch to support batch PTE unmapping in
try_to_unmap_one. It first introduces range handling fortlbbatchflush. Currently, the range is always set to the size ofPAGE_SIZE.You could have spelled out why you perform the VMA -> MM change.
Sure, I’ll include some additional details in v3.
Quoted 122 lines
Quoted 122 lines
Cc Catalin Marinas <[email protected]>Cc Will Deacon <[email protected]>Cc Thomas Gleixner <[email protected]>Cc Ingo Molnar <[email protected]>Cc Borislav Petkov <[email protected]>Cc Dave Hansen <[email protected]>Cc "H. Peter Anvin" <[email protected]>Cc Anshuman Khandual <[email protected]>Cc Ryan Roberts <[email protected]>Cc Shaoqin Huang <[email protected]>Cc Gavin Shan <[email protected]>Cc Kefeng Wang <[email protected]>Cc Mark Rutland <[email protected]>Cc David Hildenbrand <[email protected]>Cc Lance Yang <[email protected]>Cc "Kirill A. Shutemov" <[email protected]>Cc Yosry Ahmed <[email protected]>Cc Paul Walmsley <[email protected]>Cc Palmer Dabbelt <[email protected]>Cc Albert Ou <[email protected]>Cc Yicong Yang <[email protected]>Signed-off-by Barry Song <[email protected]>--- arch/arm64/include/asm/tlbflush.h | 26 ++++++++++++++------------ arch/arm64/mm/contpte.c | 2 +- arch/riscv/include/asm/tlbflush.h | 3 ++- arch/riscv/mm/tlbflush.c | 3 ++- arch/x86/include/asm/tlbflush.h | 3 ++- mm/rmap.c | 12 +++++++----- 6 files changed, 28 insertions(+), 21 deletions(-)arch/arm64/include/asm/tlbflush.h +14
-12@@ -322,13 +322,6 @@ static inline bool arch_tlbbatch_should_defer(struct mm_struct *mm)return true; } -static inline void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch, -struct mm_struct *mm, -unsigned long uaddr) -{ -__flush_tlb_page_nosync (mm, uaddr); -} -/* * If mprotect/munmap/etc occurs during TLB batched flushing, we need to * synchronise all the TLBI issued with a DSB to avoid the race mentioned in @@ -448,7 +441,7 @@ static inline bool __flush_tlb_range_limit_excess(unsigned long start,return false; } -static inline void __flush_tlb_range_nosync (struct vm_area_struct *vma, +static inline void __flush_tlb_range_nosync (struct mm_struct *mm,unsigned long start,unsigned long end,unsigned long stride,bool last_level,int tlb_level)@@ -460,12 +453,12 @@ static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma, pages = (end - start) >>PAGE_SHIFT ;if (__flush_tlb_range_limit_excess (start, end, pages, stride)) { -flush_tlb_mm (vma->vm_mm); +flush_tlb_mm (mm);return ; }dsb (ishst); - asid =ASID (vma->vm_mm); + asid =ASID (mm);if (last_level) __flush_tlb_range_op(vale1is, start, pages, stride, asid,@@ -474,7 +467,7 @@ static inline void __flush_tlb_range_nosync(struct vm_area_struct *vma,__flush_tlb_range_op (vae1is, start, pages, stride, asid, tlb_level, true,lpa2_is_enabled ()); -mmu_notifier_arch_invalidate_secondary_tlbs (vma->vm_mm, start, end); +mmu_notifier_arch_invalidate_secondary_tlbs (mm, start, end); } static inline void __flush_tlb_range(struct vm_area_struct *vma,@@ -482,7 +475,7 @@ static inline void __flush_tlb_range(struct vm_area_struct *vma, unsigned long stride, bool last_level, int tlb_level) { -__flush_tlb_range_nosync (vma ,start ,end ,stride , + __flush_tlb_range_nosync(vma->vm_mm, start, end, stride,last_level ,tlb_level );dsb (ish); }@@ -533,6 +526,15 @@ static inline void __flush_tlb_kernel_pgtable(unsigned long kaddr)dsb (ish);isb (); } + +static inline void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch, +struct mm_struct *mm, +unsigned long uaddr, +unsigned long size) +{ +__flush_tlb_range_nosync (mm, uaddr, uaddr + size, +PAGE_SIZE , true,3 ); +}#endif #endif arch/arm64/mm/contpte.c +1
-1@@ -335,7 +335,7 @@ int contpte_ptep_clear_flush_young(struct vm_area_struct *vma,* eliding the trailing DSB applies here. */ addr =ALIGN_DOWN (addr,CONT_PTE_SIZE ); -__flush_tlb_range_nosync (vma, addr, addr +CONT_PTE_SIZE , +__flush_tlb_range_nosync (vma->vm_mm, addr, addr +CONT_PTE_SIZE ,PAGE_SIZE , true,3 ); }
Quoted 16 lines
arch/riscv/include/asm/tlbflush.h void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start, · line 65
Quoted 11 lines
arch/riscv/include/asm/tlbflush.h +2
-1@@ -61,7 +61,8 @@ void flush_pmd_tlb_range(struct vm_area_struct *vma, unsigned long start,bool arch_tlbbatch_should_defer (struct mm_struct *mm);void arch_tlbbatch_add_pending (struct arch_tlbflush_unmap_batch *batch,struct mm_struct *mm, -unsigned long uaddr); +unsigned long uaddr, +unsigned long size);While we're at it, can we just convert this to the "two tabs" indentation starting on second parameter line" way of doing things? Same for all other cases. (at least in core code, if some arch wants their own weird rules on how to handle these things)
Sure.
Quoted 25 lines
[...]
Quoted 12 lines
struct tlbflush_unmap_batch *tlb_ubc = ¤t->tlb_ubc; int batch;arch/riscv/include/asm/tlbflush.h +1
-1@@ -681,7 +682,7 @@ static void set_tlb_ubc_flush_pending(struct mm_struct *mm, pte_t pteval,if (!pte_accessible (mm, pteval))return ; -arch_tlbbatch_add_pending (&tlb_ubc->arch, mm, uaddr); +arch_tlbbatch_add_pending (&tlb_ubc->arch, mm, uaddr, size); tlb_ubc->flush_required = true;/* arch/riscv/include/asm/tlbflush.h void flush_tlb_batched_pending(struct mm_struct *mm) · line 762
Quoted 6 lines
arch/riscv/include/asm/tlbflush.h +2
-1@@ -757,7 +758,8 @@ void flush_tlb_batched_pending(struct mm_struct *mm) }#else static void set_tlb_ubc_flush_pending (struct mm_struct *mm,pte_t pteval, -unsigned long uaddr) +unsigned long uaddr, +unsigned long size)I'll note that mist tlb functions seem to consume start+end instead of start+size, like
Quoted 4 lines
flush_tlb_mm_range()flush_tlb_kernel_range()So I'm wondering if this should be start+end instead of uaddr+size.
For some reason, I can’t recall why I originally named it "uaddr" instead of "address" at:
https://lore.kernel.org/lkml/[email protected]/
The name seems a bit odd now.
It's probably because the arm64 functions listed below are using "uaddr":
static inline void __flush_tlb_page_nosync(struct mm_struct *mm,
unsigned long uaddr);
static inline void flush_tlb_page(struct vm_area_struct *vma,
unsigned long uaddr);
At that time, we only supported a single page for TLB deferred shootdown.
Regarding set_tlb_ubc_flush_pending(), I’m fine with either approach:
1. start, size
2. start, end
For option 1, it is eventually converted to (start, start + size) when calling __flush_tlb_range_nosync().
But if we convert to (start, end), it seems to align with
static inline void flush_tlb_range(struct vm_area_struct *vma,
unsigned long start, unsigned long end);
So if there is no objection from Will, I will move to (start, end) in v3.
Quoted 6 lines
--
Cheers,David / dhildenb
Thanks
Barry
PATCH v2 3/4 mm: Support batched unmap for lazyfree large folios during reclamation
In reply to Barry SongFrom: Barry Song <[email protected]>
Currently, the PTEs and rmap of a large folio are removed one at a time. This is not only slow but also causes the large folio to be unnecessarily added to deferred_split, which can lead to races between the deferred_split shrinker callback and memory reclamation. This patch releases all PTEs and rmap entries in a batch.
Currently, it only handles lazyfree large folios.
The below microbench tries to reclaim 128MB lazyfree large folios whose sizes are 64KiB:
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include <time.h>
#define SIZE 128*1024*1024 // 128 MB
unsigned long read_split_deferred()
{
FILE *file = fopen("/sys/kernel/mm/transparent_hugepage"
"/hugepages-64kB/stats/split_deferred", "r");
if (!file) {
perror("Error opening file");
return 0;
}
unsigned long value;
if (fscanf(file, "%lu", &value) != 1) {
perror("Error reading value");
fclose(file);
return 0;
}
fclose(file);
return value;
}
int main(int argc, char *argv[])
{
while(1) {
volatile int *p = mmap(0, SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
memset((void *)p, 1, SIZE);
madvise((void *)p, SIZE, MADV_FREE);
clock_t start_time = clock();
unsigned long start_split = read_split_deferred(); madvise((void *)p, SIZE, MADV_PAGEOUT); clock_t end_time = clock();
unsigned long end_split = read_split_deferred();
double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; printf("Time taken by reclamation: %f seconds, split_deferred: %ld\n",
elapsed_time, end_split - start_split);
munmap((void *)p, SIZE);
}
return 0;
}
w/o patch:
~ # ./a.out
Time taken by reclamation: 0.177418 seconds, split_deferred: 2048 Time taken by reclamation: 0.178348 seconds, split_deferred: 2048 Time taken by reclamation: 0.174525 seconds, split_deferred: 2048 Time taken by reclamation: 0.171620 seconds, split_deferred: 2048 Time taken by reclamation: 0.172241 seconds, split_deferred: 2048 Time taken by reclamation: 0.174003 seconds, split_deferred: 2048 Time taken by reclamation: 0.171058 seconds, split_deferred: 2048 Time taken by reclamation: 0.171993 seconds, split_deferred: 2048 Time taken by reclamation: 0.169829 seconds, split_deferred: 2048 Time taken by reclamation: 0.172895 seconds, split_deferred: 2048 Time taken by reclamation: 0.176063 seconds, split_deferred: 2048 Time taken by reclamation: 0.172568 seconds, split_deferred: 2048 Time taken by reclamation: 0.171185 seconds, split_deferred: 2048 Time taken by reclamation: 0.170632 seconds, split_deferred: 2048 Time taken by reclamation: 0.170208 seconds, split_deferred: 2048 Time taken by reclamation: 0.174192 seconds, split_deferred: 2048
...
w/ patch:
~ # ./a.out
Time taken by reclamation: 0.074231 seconds, split_deferred: 0 Time taken by reclamation: 0.071026 seconds, split_deferred: 0 Time taken by reclamation: 0.072029 seconds, split_deferred: 0 Time taken by reclamation: 0.071873 seconds, split_deferred: 0 Time taken by reclamation: 0.073573 seconds, split_deferred: 0 Time taken by reclamation: 0.071906 seconds, split_deferred: 0 Time taken by reclamation: 0.073604 seconds, split_deferred: 0 Time taken by reclamation: 0.075903 seconds, split_deferred: 0 Time taken by reclamation: 0.073191 seconds, split_deferred: 0 Time taken by reclamation: 0.071228 seconds, split_deferred: 0 Time taken by reclamation: 0.071391 seconds, split_deferred: 0 Time taken by reclamation: 0.071468 seconds, split_deferred: 0 Time taken by reclamation: 0.071896 seconds, split_deferred: 0 Time taken by reclamation: 0.072508 seconds, split_deferred: 0 Time taken by reclamation: 0.071884 seconds, split_deferred: 0 Time taken by reclamation: 0.072433 seconds, split_deferred: 0 Time taken by reclamation: 0.071939 seconds, split_deferred: 0
...
--- mm/rmap.c | 46 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 6 deletions(-)
mm/rmap.c +40 -6
@@ -1642,6 +1642,25 @@ void folio_remove_rmap_pmd(struct folio *folio, struct page *page,#endif } +/* We support batch unmapping of PTEs for lazyfree large folios */ +static inline bool can_batch_unmap_folio_ptes (unsigned long addr, +struct folio *folio,pte_t *ptep) +{ +const fpb_t fpb_flags =FPB_IGNORE_DIRTY |FPB_IGNORE_SOFT_DIRTY ; +int max_nr =folio_nr_pages (folio); +pte_t pte =ptep_get (ptep); + +if (!folio_test_anon (folio) ||folio_test_swapbacked (folio)) +return false; +if (pte_none (pte) ||pte_unused (pte) || !pte_present (pte)) +return false; +if (pte_pfn (pte) !=folio_pfn (folio)) +return false; + +return folio_pte_batch (folio, addr, ptep, pte, max_nr, fpb_flags,NULL , +NULL ,NULL ) == max_nr; +} +/* * @arg: enum ttu_flags will be passed to this argument */
@@ -1655,6 +1674,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,bool anon_exclusive, ret = true;struct mmu_notifier_range range;enum ttu_flags flags = (enum ttu_flags )(long )arg; +int nr_pages =1 ;unsigned long pfn;unsigned long hsz =0 ;
@@ -1780,6 +1800,15 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,hugetlb_vma_unlock_write (vma); } pteval =huge_ptep_clear_flush (vma, address, pvmw.pte); + }else if (folio_test_large (folio) && !(flags &TTU_HWPOISON ) && +can_batch_unmap_folio_ptes (address, folio, pvmw.pte)) { + nr_pages =folio_nr_pages (folio); +flush_cache_range (vma, range.start, range.end); + pteval =get_and_clear_full_ptes (mm, address, pvmw.pte, nr_pages,0 ); +if (should_defer_flush (mm, flags)) +set_tlb_ubc_flush_pending (mm, pteval, address,folio_size (folio)); +else +flush_tlb_range (vma, range.start, range.end); }else {flush_cache_page (vma, address, pfn);/* Nuke the page table entry. */
@@ -1875,7 +1904,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,* redirtied either using the page table or a previously * obtained GUP reference. */ -set_pte_at (mm, address, pvmw.pte, pteval); +set_ptes (mm, address, pvmw.pte, pteval, nr_pages);folio_set_swapbacked (folio); goto walk_abort; }else if (ref_count !=1 + map_count) {
@@ -1888,10 +1917,10 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,* We'll come back here later and detect if the folio was * dirtied when the additional reference is gone. */ -set_pte_at (mm, address, pvmw.pte, pteval); +set_ptes (mm, address, pvmw.pte, pteval, nr_pages); goto walk_abort; } -dec_mm_counter (mm,MM_ANONPAGES ); +add_mm_counter (mm,MM_ANONPAGES , -nr_pages); goto discard; }
@@ -1943,13 +1972,18 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,dec_mm_counter (mm,mm_counter_file (folio)); } discard: -if (unlikely (folio_test_hugetlb (folio))) +if (unlikely (folio_test_hugetlb (folio))) {hugetlb_remove_rmap (folio); -else -folio_remove_rmap_pte (folio, subpage, vma); + }else { +folio_remove_rmap_ptes (folio, subpage, nr_pages, vma); +folio_ref_sub (folio, nr_pages -1 ); + }if (vma->vm_flags &VM_LOCKED )mlock_drain_local ();folio_put (folio); +/* We have already batched the entire folio */ +if (nr_pages >1 ) + goto walk_done;continue ; walk_abort: ret = false;
Signature · 1 line
2.39.3 (Apple Git-146)
PATCH v2 4/4 mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap
In reply to Barry SongFrom: Barry Song <[email protected]>
The try_to_unmap_one() function currently handles PMD-mapped THPs inefficiently. It first splits the PMD into PTEs, copies the dirty state from the PMD to the PTEs, iterates over the PTEs to locate the dirty state, and then marks the THP as swap-backed. This process involves unnecessary PMD splitting and redundant iteration. Instead, this functionality can be efficiently managed in __discard_anon_folio_pmd_locked(), avoiding the extra steps and improving performance.
The following microbenchmark redirties folios after invoking MADV_FREE, then measures the time taken to perform memory reclamation (actually set those folios swapbacked again) on the redirtied folios.
#include <stdio.h>
#include <sys/mman.h>
#include <string.h>
#include <time.h>
#define SIZE 128*1024*1024 // 128 MB
int main(int argc, char *argv[])
{
while(1) {
volatile int *p = mmap(0, SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
memset((void *)p, 1, SIZE); madvise((void *)p, SIZE, MADV_FREE); /* redirty after MADV_FREE */ memset((void *)p, 1, SIZE);
clock_t start_time = clock();
madvise((void *)p, SIZE, MADV_PAGEOUT); clock_t end_time = clock();
double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; printf("Time taken by reclamation: %f seconds\n", elapsed_time);
munmap((void *)p, SIZE);
}
return 0;
}
Testing results are as below,
w/o patch:
~ # ./a.out
Time taken by reclamation: 0.007300 seconds
Time taken by reclamation: 0.007226 seconds
Time taken by reclamation: 0.007295 seconds
Time taken by reclamation: 0.007731 seconds
Time taken by reclamation: 0.007134 seconds
Time taken by reclamation: 0.007285 seconds
Time taken by reclamation: 0.007720 seconds
Time taken by reclamation: 0.007128 seconds
Time taken by reclamation: 0.007710 seconds
Time taken by reclamation: 0.007712 seconds
Time taken by reclamation: 0.007236 seconds
Time taken by reclamation: 0.007690 seconds
Time taken by reclamation: 0.007174 seconds
Time taken by reclamation: 0.007670 seconds
Time taken by reclamation: 0.007169 seconds
Time taken by reclamation: 0.007305 seconds
Time taken by reclamation: 0.007432 seconds
Time taken by reclamation: 0.007158 seconds
Time taken by reclamation: 0.007133 seconds
…
w/ patch
~ # ./a.out
Time taken by reclamation: 0.002124 seconds
Time taken by reclamation: 0.002116 seconds
Time taken by reclamation: 0.002150 seconds
Time taken by reclamation: 0.002261 seconds
Time taken by reclamation: 0.002137 seconds
Time taken by reclamation: 0.002173 seconds
Time taken by reclamation: 0.002063 seconds
Time taken by reclamation: 0.002088 seconds
Time taken by reclamation: 0.002169 seconds
Time taken by reclamation: 0.002124 seconds
Time taken by reclamation: 0.002111 seconds
Time taken by reclamation: 0.002224 seconds
Time taken by reclamation: 0.002297 seconds
Time taken by reclamation: 0.002260 seconds
Time taken by reclamation: 0.002246 seconds
Time taken by reclamation: 0.002272 seconds
Time taken by reclamation: 0.002277 seconds
Time taken by reclamation: 0.002462 seconds
…
This patch significantly speeds up try_to_unmap_one() by allowing it to skip redirtied THPs without splitting the PMD.
--- mm/huge_memory.c | 17 ++++++++++++++--- mm/rmap.c | 11 ++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-)
mm/huge_memory.c +14 -3
@@ -3070,8 +3070,12 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,int ref_count, map_count;pmd_t orig_pmd = *pmdp; -if (folio_test_dirty (folio) ||pmd_dirty (orig_pmd)) +if (pmd_dirty (orig_pmd)) +folio_set_dirty (folio); +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +folio_set_swapbacked (folio);return false; + } orig_pmd =pmdp_huge_clear_flush (vma, addr, pmdp);
@@ -3098,8 +3102,15 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,* * The only folio refs must be one from isolation plus the rmap(s). */ -if (folio_test_dirty (folio) ||pmd_dirty (orig_pmd) || - ref_count != map_count +1 ) { +if (pmd_dirty (orig_pmd)) +folio_set_dirty (folio); +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +folio_set_swapbacked (folio); +set_pmd_at (mm, addr, pmdp, orig_pmd); +return false; + } + +if (ref_count != map_count +1 ) {set_pmd_at (mm, addr, pmdp, orig_pmd);return false; }
mm/rmap.c +10 -1
@@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,DEFINE_FOLIO_VMA_WALK (pvmw, folio, vma, address,0 );pte_t pteval;struct page *subpage; -bool anon_exclusive, ret = true; +bool anon_exclusive, lazyfree, ret = true;struct mmu_notifier_range range;enum ttu_flags flags = (enum ttu_flags )(long )arg;int nr_pages =1 ;
@@ -1724,9 +1724,18 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, }if (!pvmw.pte) { + lazyfree =folio_test_anon (folio) && !folio_test_swapbacked (folio); +if (unmap_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, folio)) goto walk_done; +/* +* unmap_huge_pmd_locked has either already marked +* the folio as swap-backed or decided to retain it +* due to GUP or speculative references. +*/ +if (lazyfree) + goto walk_abort;if (flags &TTU_SPLIT_HUGE_PMD ) {/*
Signature · 1 line
2.39.3 (Apple Git-146)
Baolin Wang
reviewerRe: [PATCH v2 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap
Quoted 153 lines · Barry Song
From: Barry Song <[email protected]>
The
try_to_unmap_one()function currently handles PMD-mapped THPs inefficiently. It first splits the PMD into PTEs, copies the dirty state from the PMD to the PTEs, iterates over the PTEs to locate the dirty state, and then marks the THP as swap-backed. This process involves unnecessary PMD splitting and redundant iteration. Instead, this functionality can be efficiently managed in__discard_anon_folio_pmd_locked(), avoiding the extra steps and improving performance.The following microbenchmark redirties folios after invoking
MADV_FREE, then measures the time taken to perform memory reclamation (actually set those folios swapbacked again) on the redirtied folios.#include <stdio.h> #include <sys/mman.h> #include <string.h> #include <time.h> #define SIZE 128*1024*1024 // 128 MB int main(int argc, char *argv[]) { while(1) { volatile int *p = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); memset((void *)p, 1, SIZE); madvise((void *)p, SIZE, MADV_FREE); /* redirty after MADV_FREE */ memset((void *)p, 1, SIZE);clock_t start_time = clock();
madvise((void *)p, SIZE, MADV_PAGEOUT);clock_tend_time=clock();double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; printf("Time taken by reclamation: %f seconds\n", elapsed_time); munmap((void *)p, SIZE); } return 0; }Testing results are as below,
w/o patch:
~ # ./a.out
Time taken by reclamation: 0.007300 seconds
Time taken by reclamation: 0.007226 seconds
Time taken by reclamation: 0.007295 seconds
Time taken by reclamation: 0.007731 seconds
Time taken by reclamation: 0.007134 seconds
Time taken by reclamation: 0.007285 seconds
Time taken by reclamation: 0.007720 seconds
Time taken by reclamation: 0.007128 seconds
Time taken by reclamation: 0.007710 seconds
Time taken by reclamation: 0.007712 seconds
Time taken by reclamation: 0.007236 seconds
Time taken by reclamation: 0.007690 seconds
Time taken by reclamation: 0.007174 seconds
Time taken by reclamation: 0.007670 seconds
Time taken by reclamation: 0.007169 seconds
Time taken by reclamation: 0.007305 seconds
Time taken by reclamation: 0.007432 seconds
Time taken by reclamation: 0.007158 seconds
Time taken by reclamation: 0.007133 seconds
…w/ patch
~ # ./a.out
Time taken by reclamation: 0.002124 seconds
Time taken by reclamation: 0.002116 seconds
Time taken by reclamation: 0.002150 seconds
Time taken by reclamation: 0.002261 seconds
Time taken by reclamation: 0.002137 seconds
Time taken by reclamation: 0.002173 seconds
Time taken by reclamation: 0.002063 seconds
Time taken by reclamation: 0.002088 seconds
Time taken by reclamation: 0.002169 seconds
Time taken by reclamation: 0.002124 seconds
Time taken by reclamation: 0.002111 seconds
Time taken by reclamation: 0.002224 seconds
Time taken by reclamation: 0.002297 seconds
Time taken by reclamation: 0.002260 seconds
Time taken by reclamation: 0.002246 seconds
Time taken by reclamation: 0.002272 seconds
Time taken by reclamation: 0.002277 seconds
Time taken by reclamation: 0.002462 seconds
…This patch significantly speeds up
try_to_unmap_one()by allowing it to skip redirtied THPs without splitting the PMD.Suggested-by Baolin Wang <[email protected]>Suggested-by Lance Yang <[email protected]>Signed-off-by Barry Song <[email protected]>--- mm/huge_memory.c | 17 ++++++++++++++--- mm/rmap.c | 11 ++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-)mm/huge_memory.c +14
-3@@ -3070,8 +3070,12 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,int ref_count, map_count;pmd_t orig_pmd = *pmdp; -if (folio_test_dirty (folio) ||pmd_dirty (orig_pmd)) +if (pmd_dirty (orig_pmd)) +folio_set_dirty (folio); +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +folio_set_swapbacked (folio);return false; + } orig_pmd =pmdp_huge_clear_flush (vma, addr, pmdp);@@ -3098,8 +3102,15 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,* * The only folio refs must be one from isolation plus the rmap(s). */ -if (folio_test_dirty (folio) ||pmd_dirty (orig_pmd) || - ref_count != map_count +1 ) { +if (pmd_dirty (orig_pmd)) +folio_set_dirty (folio); +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +folio_set_swapbacked (folio); +set_pmd_at (mm, addr, pmdp, orig_pmd); +return false; + } + +if (ref_count != map_count +1 ) {set_pmd_at (mm, addr, pmdp, orig_pmd);return false; }mm/rmap.c +1
-1@@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,DEFINE_FOLIO_VMA_WALK (pvmw, folio, vma, address,0 );pte_t pteval;struct page *subpage; -bool anon_exclusive, ret = true; +bool anon_exclusive, lazyfree, ret = true;struct mmu_notifier_range range;enum ttu_flags flags = (enum ttu_flags )(long )arg;int nr_pages =1 ;
mm/rmap.c static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, · line 1727
Quoted 4 lines
mm/rmap.c +1
-0@@ -1724,9 +1724,18 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, }if (!pvmw.pte) { + lazyfree =folio_test_anon (folio) && !folio_test_swapbacked (folio);
You've checked lazyfree here, so can we remove the duplicate check in unmap_huge_pmd_locked()? Then the code should be:
if (lazyfree && unmap_huge_pmd_locked(...)) goto walk_done;
Quoted 13 lines
mm/rmap.c +7
-0if (unmap_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, folio)) goto walk_done; +/* +* unmap_huge_pmd_locked has either already marked +* the folio as swap-backed or decided to retain it +* due to GUP or speculative references. +*/ +if (lazyfree) + goto walk_abort;if (flags &TTU_SPLIT_HUGE_PMD ) {/*
Re: [PATCH v2 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap
Quoted 159 lines · Baolin Wang
Quoted 155 lines · Barry Song
From: Barry Song <[email protected]>
The
try_to_unmap_one()function currently handles PMD-mapped THPs inefficiently. It first splits the PMD into PTEs, copies the dirty state from the PMD to the PTEs, iterates over the PTEs to locate the dirty state, and then marks the THP as swap-backed. This process involves unnecessary PMD splitting and redundant iteration. Instead, this functionality can be efficiently managed in__discard_anon_folio_pmd_locked(), avoiding the extra steps and improving performance.The following microbenchmark redirties folios after invoking
MADV_FREE, then measures the time taken to perform memory reclamation (actually set those folios swapbacked again) on the redirtied folios.#include <stdio.h> #include <sys/mman.h> #include <string.h> #include <time.h> #define SIZE 128*1024*1024 // 128 MB int main(int argc, char *argv[]) { while(1) { volatile int *p = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);memset((void *)p, 1, SIZE); madvise((void *)p, SIZE, MADV_FREE); /* redirty after MADV_FREE */ memset((void *)p, 1, SIZE); clock_t start_time = clock(); madvise((void *)p, SIZE, MADV_PAGEOUT); clock_t end_time = clock(); double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; printf("Time taken by reclamation: %f seconds\n", elapsed_time); munmap((void *)p, SIZE); } return 0;}
Testing results are as below,
w/o patch:
~ # ./a.out
Time taken by reclamation: 0.007300 seconds
Time taken by reclamation: 0.007226 seconds
Time taken by reclamation: 0.007295 seconds
Time taken by reclamation: 0.007731 seconds
Time taken by reclamation: 0.007134 seconds
Time taken by reclamation: 0.007285 seconds
Time taken by reclamation: 0.007720 seconds
Time taken by reclamation: 0.007128 seconds
Time taken by reclamation: 0.007710 seconds
Time taken by reclamation: 0.007712 seconds
Time taken by reclamation: 0.007236 seconds
Time taken by reclamation: 0.007690 seconds
Time taken by reclamation: 0.007174 seconds
Time taken by reclamation: 0.007670 seconds
Time taken by reclamation: 0.007169 seconds
Time taken by reclamation: 0.007305 seconds
Time taken by reclamation: 0.007432 seconds
Time taken by reclamation: 0.007158 seconds
Time taken by reclamation: 0.007133 seconds
…w/ patch
~ # ./a.out
Time taken by reclamation: 0.002124 seconds
Time taken by reclamation: 0.002116 seconds
Time taken by reclamation: 0.002150 seconds
Time taken by reclamation: 0.002261 seconds
Time taken by reclamation: 0.002137 seconds
Time taken by reclamation: 0.002173 seconds
Time taken by reclamation: 0.002063 seconds
Time taken by reclamation: 0.002088 seconds
Time taken by reclamation: 0.002169 seconds
Time taken by reclamation: 0.002124 seconds
Time taken by reclamation: 0.002111 seconds
Time taken by reclamation: 0.002224 seconds
Time taken by reclamation: 0.002297 seconds
Time taken by reclamation: 0.002260 seconds
Time taken by reclamation: 0.002246 seconds
Time taken by reclamation: 0.002272 seconds
Time taken by reclamation: 0.002277 seconds
Time taken by reclamation: 0.002462 seconds
…This patch significantly speeds up
try_to_unmap_one()by allowing it to skip redirtied THPs without splitting the PMD.Suggested-by Baolin Wang <[email protected]>Suggested-by Lance Yang <[email protected]>Signed-off-by Barry Song <[email protected]>--- mm/huge_memory.c | 17 ++++++++++++++--- mm/rmap.c | 11 ++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-)mm/huge_memory.c +14
-3@@ -3070,8 +3070,12 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,int ref_count, map_count;pmd_t orig_pmd = *pmdp; -if (folio_test_dirty (folio) ||pmd_dirty (orig_pmd)) +if (pmd_dirty (orig_pmd)) +folio_set_dirty (folio); +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +folio_set_swapbacked (folio);return false; + } orig_pmd =pmdp_huge_clear_flush (vma, addr, pmdp);@@ -3098,8 +3102,15 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,* * The only folio refs must be one from isolation plus the rmap(s). */ -if (folio_test_dirty (folio) ||pmd_dirty (orig_pmd) || - ref_count != map_count +1 ) { +if (pmd_dirty (orig_pmd)) +folio_set_dirty (folio); +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +folio_set_swapbacked (folio); +set_pmd_at (mm, addr, pmdp, orig_pmd); +return false; + } + +if (ref_count != map_count +1 ) {set_pmd_at (mm, addr, pmdp, orig_pmd);return false; }mm/rmap.c +1
-1@@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,DEFINE_FOLIO_VMA_WALK (pvmw, folio, vma, address,0 );pte_t pteval;struct page *subpage; -bool anon_exclusive, ret = true; +bool anon_exclusive, lazyfree, ret = true;struct mmu_notifier_range range;enum ttu_flags flags = (enum ttu_flags )(long )arg;int nr_pages =1 ;@@ -1724,9 +1724,18 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, }
Quoted 8 lines
mm/rmap.c static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, · line 1727
Quoted 2 lines
mm/rmap.c +1
-0if (!pvmw.pte) { + lazyfree =folio_test_anon (folio) && !folio_test_swapbacked (folio);You've checked lazyfree here, so can we remove the duplicate check in
unmap_huge_pmd_locked()? Then the code should be:if (lazyfree && unmap_huge_pmd_locked(...)) goto walk_done;
right. it seems unmap_huge_pmd_locked() only handles lazyfree pmd-mapped thp. so i guess the code could be:
mm/huge_memory.c +3 -4
@@ -3131,11 +3131,10 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,VM_WARN_ON_FOLIO (!folio_test_pmd_mappable (folio), folio);VM_WARN_ON_FOLIO (!folio_test_locked (folio), folio);VM_WARN_ON_ONCE (!IS_ALIGNED (addr,HPAGE_PMD_SIZE )); +VM_WARN_ON_FOLIO (!folio_test_anon (folio), folio); +VM_WARN_ON_FOLIO (folio_test_swapbacked (folio), folio); -if (folio_test_anon (folio) && !folio_test_swapbacked (folio)) -return __discard_anon_folio_pmd_locked (vma, addr, pmdp, folio); - -return false; +return __discard_anon_folio_pmd_locked (vma, addr, pmdp, folio); }static void remap_page (struct folio *folio,unsigned long nr,int flags)
mm/rmap.c +10 -12
@@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,DEFINE_FOLIO_VMA_WALK (pvmw, folio, vma, address,0 );pte_t pteval;struct page *subpage; -bool anon_exclusive, lazyfree, ret = true; +bool anon_exclusive, ret = true;struct mmu_notifier_range range;enum ttu_flags flags = (enum ttu_flags )(long )arg;int nr_pages =1 ;
@@ -1724,18 +1724,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, }if (!pvmw.pte) { - lazyfree =folio_test_anon (folio) && !folio_test_swapbacked (folio); - -if (unmap_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, - folio)) - goto walk_done; -/* -* unmap_huge_pmd_locked has either already marked -* the folio as swap-backed or decided to retain it -* due to GUP or speculative references. -*/ -if (lazyfree) +if (folio_test_anon (folio) && !folio_test_swapbacked (folio)) { +if (unmap_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, folio)) + goto walk_done; +/* +* unmap_huge_pmd_locked has either already marked +* the folio as swap-backed or decided to retain it +* due to GUP or speculative references. +*/ goto walk_abort; + }if (flags &TTU_SPLIT_HUGE_PMD ) {/*
Quoted 14 lines
Quoted 14 lines
mm/rmap.c +7
-0if (unmap_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, folio)) goto walk_done; +/* +* unmap_huge_pmd_locked has either already marked +* the folio as swap-backed or decided to retain it +* due to GUP or speculative references. +*/ +if (lazyfree) + goto walk_abort;if (flags &TTU_SPLIT_HUGE_PMD ) {/*
Re: [PATCH v2 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap
Quoted 86 lines
Quoted 8 lines
Quoted 2 lines
if (!pvmw.pte) {
+ lazyfree =folio_test_anon(folio)&& !folio_test_swapbacked(folio);You've checked lazyfree here, so can we remove the duplicate check in
unmap_huge_pmd_locked()? Then the code should be:if (lazyfree &&
unmap_huge_pmd_locked(...)) gotowalk_done;right. it seems
unmap_huge_pmd_locked()only handles lazyfree pmd-mapped thp. so i guess the code could be:mm/huge_memory.c +3
-4@@ -3131,11 +3131,10 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, VM_WARN_ON_FOLIO(!folio_test_pmd_mappable(folio), folio); VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); VM_WARN_ON_ONCE(!IS_ALIGNED(addr, HPAGE_PMD_SIZE)); + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); + VM_WARN_ON_FOLIO(folio_test_swapbacked(folio), folio); - if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) - return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio); - - return false; + return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio); } static void remap_page(struct folio *folio, unsigned long nr, int flags)mm/rmap.c +10
-12@@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); pte_t pteval; struct page *subpage; - bool anon_exclusive, lazyfree, ret = true; + bool anon_exclusive, ret = true; struct mmu_notifier_range range; enum ttu_flags flags = (enum ttu_flags)(long)arg; int nr_pages = 1;@@ -1724,18 +1724,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, } if (!pvmw.pte) { - lazyfree = folio_test_anon(folio) && !folio_test_swapbacked(folio); - - if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, - folio)) - goto walk_done; - /* - * unmap_huge_pmd_locked has either already marked - * the folio as swap-backed or decided to retain it - * due to GUP or speculative references. - */ - if (lazyfree) + if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) { + if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio)) + goto walk_done; + /* + * unmap_huge_pmd_locked has either already marked + * the folio as swap-backed or decided to retain it + * due to GUP or speculative references. + */ goto walk_abort; + } if (flags & TTU_SPLIT_HUGE_PMD) { /*Quoted 13 lines
Quoted 13 lines
if (
unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
gotowalk_done;
+ /*
+ *unmap_huge_pmd_lockedhas either already marked
+ * the folio as swap-backed or decided to retain it
+ * due to GUP or speculative references.
+ */
+ if (lazyfree)
+ gotowalk_abort;
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
if (flags &
TTU_SPLIT_HUGE_PMD) { /*
The final diff is as follows.
Baolin, do you have any additional comments before I send out v3?
mm/huge_memory.c +17 -7
@@ -3070,8 +3070,12 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,int ref_count, map_count;pmd_t orig_pmd = *pmdp; -if (folio_test_dirty (folio) ||pmd_dirty (orig_pmd)) +if (pmd_dirty (orig_pmd)) +folio_set_dirty (folio); +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +folio_set_swapbacked (folio);return false; + } orig_pmd =pmdp_huge_clear_flush (vma, addr, pmdp);
@@ -3098,8 +3102,15 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,* * The only folio refs must be one from isolation plus the rmap(s). */ -if (folio_test_dirty (folio) ||pmd_dirty (orig_pmd) || - ref_count != map_count +1 ) { +if (pmd_dirty (orig_pmd)) +folio_set_dirty (folio); +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +folio_set_swapbacked (folio); +set_pmd_at (mm, addr, pmdp, orig_pmd); +return false; + } + +if (ref_count != map_count +1 ) {set_pmd_at (mm, addr, pmdp, orig_pmd);return false; }
@@ -3119,12 +3130,11 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, {VM_WARN_ON_FOLIO (!folio_test_pmd_mappable (folio), folio);VM_WARN_ON_FOLIO (!folio_test_locked (folio), folio); +VM_WARN_ON_FOLIO (!folio_test_anon (folio), folio); +VM_WARN_ON_FOLIO (folio_test_swapbacked (folio), folio);VM_WARN_ON_ONCE (!IS_ALIGNED (addr,HPAGE_PMD_SIZE )); -if (folio_test_anon (folio) && !folio_test_swapbacked (folio)) -return __discard_anon_folio_pmd_locked (vma, addr, pmdp, folio); - -return false; +return __discard_anon_folio_pmd_locked (vma, addr, pmdp, folio); }static void remap_page (struct folio *folio,unsigned long nr,int flags)
mm/rmap.c +10 -3
@@ -1724,9 +1724,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, }if (!pvmw.pte) { -if (unmap_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, - folio)) - goto walk_done; +if (folio_test_anon (folio) && !folio_test_swapbacked (folio)) { +if (unmap_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, folio)) + goto walk_done; +/* +* unmap_huge_pmd_locked has either already marked +* the folio as swap-backed or decided to retain it +* due to GUP or speculative references. +*/ + goto walk_abort; + }if (flags &TTU_SPLIT_HUGE_PMD ) {/*
Signature · 1 line
2.39.3 (Apple Git-146)
Baolin Wang
reviewerRe: [PATCH v2 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap
Quoted 86 lines · Barry Song
Quoted 86 lines
Quoted 8 lines
Quoted 2 lines
if (!pvmw.pte) {
+ lazyfree =folio_test_anon(folio)&& !folio_test_swapbacked(folio);You've checked lazyfree here, so can we remove the duplicate check in
unmap_huge_pmd_locked()? Then the code should be:if (lazyfree &&
unmap_huge_pmd_locked(...)) gotowalk_done;right. it seems
unmap_huge_pmd_locked()only handles lazyfree pmd-mapped thp. so i guess the code could be:mm/huge_memory.c +3
-4@@ -3131,11 +3131,10 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr, VM_WARN_ON_FOLIO(!folio_test_pmd_mappable(folio), folio); VM_WARN_ON_FOLIO(!folio_test_locked(folio), folio); VM_WARN_ON_ONCE(!IS_ALIGNED(addr, HPAGE_PMD_SIZE)); + VM_WARN_ON_FOLIO(!folio_test_anon(folio), folio); + VM_WARN_ON_FOLIO(folio_test_swapbacked(folio), folio); - if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) - return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio); - - return false; + return __discard_anon_folio_pmd_locked(vma, addr, pmdp, folio); } static void remap_page(struct folio *folio, unsigned long nr, int flags)mm/rmap.c +10
-12@@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0); pte_t pteval; struct page *subpage; - bool anon_exclusive, lazyfree, ret = true; + bool anon_exclusive, ret = true; struct mmu_notifier_range range; enum ttu_flags flags = (enum ttu_flags)(long)arg; int nr_pages = 1;@@ -1724,18 +1724,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, } if (!pvmw.pte) { - lazyfree = folio_test_anon(folio) && !folio_test_swapbacked(folio); - - if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, - folio)) - goto walk_done; - /* - * unmap_huge_pmd_locked has either already marked - * the folio as swap-backed or decided to retain it - * due to GUP or speculative references. - */ - if (lazyfree) + if (folio_test_anon(folio) && !folio_test_swapbacked(folio)) { + if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio)) + goto walk_done; + /* + * unmap_huge_pmd_locked has either already marked + * the folio as swap-backed or decided to retain it + * due to GUP or speculative references. + */ goto walk_abort; + } if (flags & TTU_SPLIT_HUGE_PMD) { /*Quoted 13 lines
Quoted 13 lines
if (
unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
gotowalk_done;
+ /*
+ *unmap_huge_pmd_lockedhas either already marked
+ * the folio as swap-backed or decided to retain it
+ * due to GUP or speculative references.
+ */
+ if (lazyfree)
+ gotowalk_abort;
Quoted 7 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
if (flags &
TTU_SPLIT_HUGE_PMD) { /*The final diff is as follows.
Baolin, do you have any additional comments before I send out v3?
No other comments. Look good to me.
Re: [PATCH v2 4/4] mm: Avoid splitting pmd for lazyfree pmd-mapped THP in try_to_unmap
In reply to Barry SongQuoted 167 lines · Barry Song
Quoted 165 lines · Baolin Wang
Quoted 153 lines · Barry Song
From: Barry Song <[email protected]>
The
try_to_unmap_one()function currently handles PMD-mapped THPs inefficiently. It first splits the PMD into PTEs, copies the dirty state from the PMD to the PTEs, iterates over the PTEs to locate the dirty state, and then marks the THP as swap-backed. This process involves unnecessary PMD splitting and redundant iteration. Instead, this functionality can be efficiently managed in__discard_anon_folio_pmd_locked(), avoiding the extra steps and improving performance.The following microbenchmark redirties folios after invoking
MADV_FREE, then measures the time taken to perform memory reclamation (actually set those folios swapbacked again) on the redirtied folios.#include <stdio.h> #include <sys/mman.h> #include <string.h> #include <time.h> #define SIZE 128*1024*1024 // 128 MB int main(int argc, char *argv[]) { while(1) { volatile int *p = mmap(0, SIZE, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);memset((void *)p, 1, SIZE); madvise((void *)p, SIZE, MADV_FREE); /* redirty after MADV_FREE */ memset((void *)p, 1, SIZE); clock_t start_time = clock(); madvise((void *)p, SIZE, MADV_PAGEOUT); clock_t end_time = clock(); double elapsed_time = (double)(end_time - start_time) / CLOCKS_PER_SEC; printf("Time taken by reclamation: %f seconds\n", elapsed_time); munmap((void *)p, SIZE); } return 0;}
Testing results are as below,
w/o patch:
~ # ./a.out
Time taken by reclamation: 0.007300 seconds
Time taken by reclamation: 0.007226 seconds
Time taken by reclamation: 0.007295 seconds
Time taken by reclamation: 0.007731 seconds
Time taken by reclamation: 0.007134 seconds
Time taken by reclamation: 0.007285 seconds
Time taken by reclamation: 0.007720 seconds
Time taken by reclamation: 0.007128 seconds
Time taken by reclamation: 0.007710 seconds
Time taken by reclamation: 0.007712 seconds
Time taken by reclamation: 0.007236 seconds
Time taken by reclamation: 0.007690 seconds
Time taken by reclamation: 0.007174 seconds
Time taken by reclamation: 0.007670 seconds
Time taken by reclamation: 0.007169 seconds
Time taken by reclamation: 0.007305 seconds
Time taken by reclamation: 0.007432 seconds
Time taken by reclamation: 0.007158 seconds
Time taken by reclamation: 0.007133 seconds
…w/ patch
~ # ./a.out
Time taken by reclamation: 0.002124 seconds
Time taken by reclamation: 0.002116 seconds
Time taken by reclamation: 0.002150 seconds
Time taken by reclamation: 0.002261 seconds
Time taken by reclamation: 0.002137 seconds
Time taken by reclamation: 0.002173 seconds
Time taken by reclamation: 0.002063 seconds
Time taken by reclamation: 0.002088 seconds
Time taken by reclamation: 0.002169 seconds
Time taken by reclamation: 0.002124 seconds
Time taken by reclamation: 0.002111 seconds
Time taken by reclamation: 0.002224 seconds
Time taken by reclamation: 0.002297 seconds
Time taken by reclamation: 0.002260 seconds
Time taken by reclamation: 0.002246 seconds
Time taken by reclamation: 0.002272 seconds
Time taken by reclamation: 0.002277 seconds
Time taken by reclamation: 0.002462 seconds
…This patch significantly speeds up
try_to_unmap_one()by allowing it to skip redirtied THPs without splitting the PMD.Suggested-by Baolin Wang <[email protected]>Suggested-by Lance Yang <[email protected]>Signed-off-by Barry Song <[email protected]>--- mm/huge_memory.c | 17 ++++++++++++++--- mm/rmap.c | 11 ++++++++++- 2 files changed, 24 insertions(+), 4 deletions(-)mm/huge_memory.c +14
-3@@ -3070,8 +3070,12 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,int ref_count, map_count;pmd_t orig_pmd = *pmdp; -if (folio_test_dirty (folio) ||pmd_dirty (orig_pmd)) +if (pmd_dirty (orig_pmd)) +folio_set_dirty (folio); +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +folio_set_swapbacked (folio);return false; + } orig_pmd =pmdp_huge_clear_flush (vma, addr, pmdp);@@ -3098,8 +3102,15 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,* * The only folio refs must be one from isolation plus the rmap(s). */ -if (folio_test_dirty (folio) ||pmd_dirty (orig_pmd) || - ref_count != map_count +1 ) { +if (pmd_dirty (orig_pmd)) +folio_set_dirty (folio); +if (folio_test_dirty (folio) && !(vma->vm_flags &VM_DROPPABLE )) { +folio_set_swapbacked (folio); +set_pmd_at (mm, addr, pmdp, orig_pmd); +return false; + } + +if (ref_count != map_count +1 ) {set_pmd_at (mm, addr, pmdp, orig_pmd);return false; }mm/rmap.c +1
-1@@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,DEFINE_FOLIO_VMA_WALK (pvmw, folio, vma, address,0 );pte_t pteval;struct page *subpage; -bool anon_exclusive, ret = true; +bool anon_exclusive, lazyfree, ret = true;struct mmu_notifier_range range;enum ttu_flags flags = (enum ttu_flags )(long )arg;int nr_pages =1 ;mm/rmap.c static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, · line 1727
Quoted 4 lines
mm/rmap.c +1
-0@@ -1724,9 +1724,18 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, }if (!pvmw.pte) { + lazyfree =folio_test_anon (folio) && !folio_test_swapbacked (folio);You've checked lazyfree here, so can we remove the duplicate check in
unmap_huge_pmd_locked()? Then the code should be:
Quoted 6 lines
Quoted 2 lines
if (lazyfree && unmap_huge_pmd_locked(...)) goto walk_done;right. it seems
unmap_huge_pmd_locked()only handles lazyfree pmd-mapped thp. so i guess the code could be:
Yep, it would be better to remove the duplicate check in unmap_huge_pmd_locked(), and we can extend unmap_huge_pmd_locked() in the future if needed.
Thanks,
Lance
Quoted 76 lines
mm/huge_memory.c +3
-4@@ -3131,11 +3131,10 @@ bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,VM_WARN_ON_FOLIO (!folio_test_pmd_mappable (folio), folio);VM_WARN_ON_FOLIO (!folio_test_locked (folio), folio);VM_WARN_ON_ONCE (!IS_ALIGNED (addr,HPAGE_PMD_SIZE )); +VM_WARN_ON_FOLIO (!folio_test_anon (folio), folio); +VM_WARN_ON_FOLIO (folio_test_swapbacked (folio), folio); -if (folio_test_anon (folio) && !folio_test_swapbacked (folio)) -return __discard_anon_folio_pmd_locked (vma, addr, pmdp, folio); - -return false; +return __discard_anon_folio_pmd_locked (vma, addr, pmdp, folio); }static void remap_page (struct folio *folio,unsigned long nr,int flags)mm/rmap.c +10
-12@@ -1671,7 +1671,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,DEFINE_FOLIO_VMA_WALK (pvmw, folio, vma, address,0 );pte_t pteval;struct page *subpage; -bool anon_exclusive, lazyfree, ret = true; +bool anon_exclusive, ret = true;struct mmu_notifier_range range;enum ttu_flags flags = (enum ttu_flags )(long )arg;int nr_pages =1 ;@@ -1724,18 +1724,16 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma, }if (!pvmw.pte) { - lazyfree =folio_test_anon (folio) && !folio_test_swapbacked (folio); - -if (unmap_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, - folio)) - goto walk_done; -/* -* unmap_huge_pmd_locked has either already marked -* the folio as swap-backed or decided to retain it -* due to GUP or speculative references. -*/ -if (lazyfree) +if (folio_test_anon (folio) && !folio_test_swapbacked (folio)) { +if (unmap_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, folio)) + goto walk_done; +/* +* unmap_huge_pmd_locked has either already marked +* the folio as swap-backed or decided to retain it +* due to GUP or speculative references. +*/ goto walk_abort; + }if (flags &TTU_SPLIT_HUGE_PMD ) {/* Quoted 15 lines
Quoted 15 lines
mm/rmap.c +7
-0if (unmap_huge_pmd_locked (vma, pvmw.address, pvmw.pmd, folio)) goto walk_done; +/* +* unmap_huge_pmd_locked has either already marked +* the folio as swap-backed or decided to retain it +* due to GUP or speculative references. +*/ +if (lazyfree) + goto walk_abort;if (flags &TTU_SPLIT_HUGE_PMD ) {/*