Skip to contentSkip to conversation

Reclaim lazyfree THP without splitting

PATCH RESEND Superseded Lance Yang 2 participants 11 messages May 13, 2024 – May 14, 2024

Subsystems (3)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Lance Yang

author
#0 lore raw

v5 cover Reclaim lazyfree THP without splitting

Hi all,

This series adds support for reclaiming PMD-mapped THP marked as lazyfree without needing to first split the large folio via split_huge_pmd_address().

When the user no longer requires the pages, they would use madvise(MADV_FREE) to mark the pages as lazy free. Subsequently, they typically would not re-write to that memory again.

During memory reclaim, if we detect that the large folio and its PMD are both still marked as clean and there are no unexpected references(such as GUP), so we can just discard the memory lazily, improving the efficiency of memory reclamation in this case.

Performance Testing
===================

On an Intel i5 CPU, reclaiming 1GiB of lazyfree THPs using mem_cgroup_force_empty() results in the following runtimes in seconds (shorter is better):

--------------------------------------------
| Old | New | Change |
--------------------------------------------
| 0.683426 | 0.049197 | -92.80% |
--------------------------------------------

---

Changes since v4 [4]
====================
- mm/rmap: remove duplicated exit code in pagewalk loop
- Pick RB from Zi Yan - thanks!
- mm/rmap: integrate PMD-mapped folio splitting into pagewalk loop
- Remove the redundant alignment (per Baolin Wang)
- Set pvmw.ptl to NULL after unlocking the PTL (per Baolin Wang)
- mm/mlock: check for THP missing the mlock in try_to_unmap_one()
- Check whether the mlock of PMD-mapped THP was missed (suggested by Baolin Wang)
- mm/vmscan: avoid split lazyfree THP during shrink_folio_list()
- No need to check the TTU_SPLIT_HUGE_PMD flag for unmap_huge_pmd_locked() (per Zi Yan)
- Drain the local mlock batch after folio_remove_rmap_pmd() (per Baolin Wang)

Changes since v3 [3]
====================
- mm/rmap: integrate PMD-mapped folio splitting into pagewalk loop
- Resolve compilation errors by handling the case where CONFIG_PGTABLE_HAS_HUGE_LEAVES is undefined (thanks to SeongJae Park)
- mm/vmscan: avoid split lazyfree THP during shrink_folio_list()
- Remove the unnecessary conditional compilation directives (thanks to Barry Song)
- Resolve compilation errors due to undefined references to unmap_huge_pmd_locked and split_huge_pmd_locked (thanks to Barry)

Changes since v2 [2]
====================
- Update the changelog (thanks to David Hildenbrand)
- Support try_to_unmap_one() to unmap PMD-mapped folios (thanks a lot to David Hildenbrand and Zi Yan)

Changes since v1 [1]
====================
- Update the changelog
- Follow the exact same logic as in try_to_unmap_one() (per David Hildenbrand)
- Remove the extra code from rmap.c (per Matthew Wilcox)

[1] https://lore.kernel.org/linux-mm/[email protected]
[2] https://lore.kernel.org/linux-mm/[email protected]
[3] https://lore.kernel.org/linux-mm/[email protected]
[4] https://lore.kernel.org/linux-mm/[email protected]

Lance Yang (4):
mm/rmap: remove duplicated exit code in pagewalk loop mm/rmap: integrate PMD-mapped folio splitting into pagewalk loop mm/mlock: check for THP missing the mlock in try_to_unmap_one() mm/vmscan: avoid split lazyfree THP during shrink_folio_list()

 include/linux/huge_mm.h |  15 ++++++
 mm/huge_memory.c        | 117 +++++++++++++++++++++++++++++++++-------
 mm/rmap.c               |  73 ++++++++++++++-----------
 3 files changed, 153 insertions(+), 52 deletions(-)
Signature · 1 line
2.33.1
author
#1 lore raw

PATCH RESEND v5 1/4 mm/rmap: remove duplicated exit code in pagewalk loop

Introduce the labels walk_done and walk_done_err as exit points to eliminate duplicated exit code in the pagewalk loop.

---
 mm/rmap.c | 40 +++++++++++++++-------------------------
 1 file changed, 15 insertions(+), 25 deletions(-)
mm/rmap.c +15 -25
index e8fc5ecb59b2..ddffa30c79fb 100644
@@ -1679,9 +1679,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			/* Restore the mlock which got missed */
 			if (!folio_test_large(folio))
 				mlock_vma_folio(folio, vma);
-			page_vma_mapped_walk_done(&pvmw);
-			ret = false;
-			break;
+			goto walk_done_err;
 		}
 
 		pfn = pte_pfn(ptep_get(pvmw.pte));
@@ -1719,11 +1717,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			 */
 			if (!anon) {
 				VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
-				if (!hugetlb_vma_trylock_write(vma)) {
-					page_vma_mapped_walk_done(&pvmw);
-					ret = false;
-					break;
-				}
+				if (!hugetlb_vma_trylock_write(vma))
+					goto walk_done_err;
 				if (huge_pmd_unshare(mm, vma, address, pvmw.pte)) {
 					hugetlb_vma_unlock_write(vma);
 					flush_tlb_range(vma,
@@ -1738,8 +1733,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 					 * actual page and drop map count
 					 * to zero.
 					 */
-					page_vma_mapped_walk_done(&pvmw);
-					break;
+					goto walk_done;
 				}
 				hugetlb_vma_unlock_write(vma);
 			}
@@ -1811,9 +1805,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			if (unlikely(folio_test_swapbacked(folio) !=
 					folio_test_swapcache(folio))) {
 				WARN_ON_ONCE(1);
-				ret = false;
-				page_vma_mapped_walk_done(&pvmw);
-				break;
+				goto walk_done_err;
 			}
 
 			/* MADV_FREE page check */
@@ -1852,23 +1844,17 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 				 */
 				set_pte_at(mm, address, pvmw.pte, pteval);
 				folio_set_swapbacked(folio);
-				ret = false;
-				page_vma_mapped_walk_done(&pvmw);
-				break;
+				goto walk_done_err;
 			}
 
 			if (swap_duplicate(entry) < 0) {
 				set_pte_at(mm, address, pvmw.pte, pteval);
-				ret = false;
-				page_vma_mapped_walk_done(&pvmw);
-				break;
+				goto walk_done_err;
 			}
 			if (arch_unmap_one(mm, vma, address, pteval) < 0) {
 				swap_free(entry);
 				set_pte_at(mm, address, pvmw.pte, pteval);
-				ret = false;
-				page_vma_mapped_walk_done(&pvmw);
-				break;
+				goto walk_done_err;
 			}
 
 			/* See folio_try_share_anon_rmap(): clear PTE first. */
@@ -1876,9 +1862,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			    folio_try_share_anon_rmap_pte(folio, subpage)) {
 				swap_free(entry);
 				set_pte_at(mm, address, pvmw.pte, pteval);
-				ret = false;
-				page_vma_mapped_walk_done(&pvmw);
-				break;
+				goto walk_done_err;
 			}
 			if (list_empty(&mm->mmlist)) {
 				spin_lock(&mmlist_lock);
@@ -1918,6 +1902,12 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		if (vma->vm_flags & VM_LOCKED)
 			mlock_drain_local();
 		folio_put(folio);
+		continue;
+walk_done_err:
+		ret = false;
+walk_done:
+		page_vma_mapped_walk_done(&pvmw);
+		break;
 	}
 
 	mmu_notifier_invalidate_range_end(&range);
Signature · 1 line
2.33.1
reviewer Reviewed-by on v5 1/4
#2 lore raw

Re: [PATCH RESEND v5 1/4] mm/rmap: remove duplicated exit code in pagewalk loop

Quoted 5 lines · Lance Yang

Introduce the labels walk_done and walk_done_err as exit points to eliminate duplicated exit code in the pagewalk loop.

LGTM.

Quoted 105 lines
---
  mm/rmap.c | 40 +++++++++++++++-------------------------
  1 file changed, 15 insertions(+), 25 deletions(-)
mm/rmap.c +15 -25
index e8fc5ecb59b2..ddffa30c79fb 100644
@@ -1679,9 +1679,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  			/* Restore the mlock which got missed */
  			if (!folio_test_large(folio))
  				mlock_vma_folio(folio, vma);
-			page_vma_mapped_walk_done(&pvmw);
-			ret = false;
-			break;
+			goto walk_done_err;
  		}
 
  		pfn = pte_pfn(ptep_get(pvmw.pte));
@@ -1719,11 +1717,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  			 */
  			if (!anon) {
  				VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
-				if (!hugetlb_vma_trylock_write(vma)) {
-					page_vma_mapped_walk_done(&pvmw);
-					ret = false;
-					break;
-				}
+				if (!hugetlb_vma_trylock_write(vma))
+					goto walk_done_err;
  				if (huge_pmd_unshare(mm, vma, address, pvmw.pte)) {
  					hugetlb_vma_unlock_write(vma);
  					flush_tlb_range(vma,
@@ -1738,8 +1733,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  					 * actual page and drop map count
  					 * to zero.
  					 */
-					page_vma_mapped_walk_done(&pvmw);
-					break;
+					goto walk_done;
  				}
  				hugetlb_vma_unlock_write(vma);
  			}
@@ -1811,9 +1805,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  			if (unlikely(folio_test_swapbacked(folio) !=
  					folio_test_swapcache(folio))) {
  				WARN_ON_ONCE(1);
-				ret = false;
-				page_vma_mapped_walk_done(&pvmw);
-				break;
+				goto walk_done_err;
  			}
 
  			/* MADV_FREE page check */
@@ -1852,23 +1844,17 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  				 */
  				set_pte_at(mm, address, pvmw.pte, pteval);
  				folio_set_swapbacked(folio);
-				ret = false;
-				page_vma_mapped_walk_done(&pvmw);
-				break;
+				goto walk_done_err;
  			}
 
  			if (swap_duplicate(entry) < 0) {
  				set_pte_at(mm, address, pvmw.pte, pteval);
-				ret = false;
-				page_vma_mapped_walk_done(&pvmw);
-				break;
+				goto walk_done_err;
  			}
  			if (arch_unmap_one(mm, vma, address, pteval) < 0) {
  				swap_free(entry);
  				set_pte_at(mm, address, pvmw.pte, pteval);
-				ret = false;
-				page_vma_mapped_walk_done(&pvmw);
-				break;
+				goto walk_done_err;
  			}
 
  			/* See folio_try_share_anon_rmap(): clear PTE first. */
@@ -1876,9 +1862,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  			    folio_try_share_anon_rmap_pte(folio, subpage)) {
  				swap_free(entry);
  				set_pte_at(mm, address, pvmw.pte, pteval);
-				ret = false;
-				page_vma_mapped_walk_done(&pvmw);
-				break;
+				goto walk_done_err;
  			}
  			if (list_empty(&mm->mmlist)) {
  				spin_lock(&mmlist_lock);
@@ -1918,6 +1902,12 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  		if (vma->vm_flags & VM_LOCKED)
  			mlock_drain_local();
  		folio_put(folio);
+		continue;
+walk_done_err:
+		ret = false;
+walk_done:
+		page_vma_mapped_walk_done(&pvmw);
+		break;
  	}
 
  	mmu_notifier_invalidate_range_end(&range);
author
#3 lore raw

Re: [PATCH RESEND v5 1/4] mm/rmap: remove duplicated exit code in pagewalk loop

Hi Baolin,

Thanks for taking time to review!

Best,
Lance

Quoted 118 lines · Baolin Wang
Quoted 5 lines · Lance Yang

Introduce the labels walk_done and walk_done_err as exit points to eliminate duplicated exit code in the pagewalk loop.

LGTM.

Quoted 105 lines
---
  mm/rmap.c | 40 +++++++++++++++-------------------------
  1 file changed, 15 insertions(+), 25 deletions(-)
mm/rmap.c +15 -25
index e8fc5ecb59b2..ddffa30c79fb 100644
@@ -1679,9 +1679,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
                      /* Restore the mlock which got missed */
                      if (!folio_test_large(folio))
                              mlock_vma_folio(folio, vma);
-                     page_vma_mapped_walk_done(&pvmw);
-                     ret = false;
-                     break;
+                     goto walk_done_err;
              }
 
              pfn = pte_pfn(ptep_get(pvmw.pte));
@@ -1719,11 +1717,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
                       */
                      if (!anon) {
                              VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
-                             if (!hugetlb_vma_trylock_write(vma)) {
-                                     page_vma_mapped_walk_done(&pvmw);
-                                     ret = false;
-                                     break;
-                             }
+                             if (!hugetlb_vma_trylock_write(vma))
+                                     goto walk_done_err;
                              if (huge_pmd_unshare(mm, vma, address, pvmw.pte)) {
                                      hugetlb_vma_unlock_write(vma);
                                      flush_tlb_range(vma,
@@ -1738,8 +1733,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
                                       * actual page and drop map count
                                       * to zero.
                                       */
-                                     page_vma_mapped_walk_done(&pvmw);
-                                     break;
+                                     goto walk_done;
                              }
                              hugetlb_vma_unlock_write(vma);
                      }
@@ -1811,9 +1805,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
                      if (unlikely(folio_test_swapbacked(folio) !=
                                      folio_test_swapcache(folio))) {
                              WARN_ON_ONCE(1);
-                             ret = false;
-                             page_vma_mapped_walk_done(&pvmw);
-                             break;
+                             goto walk_done_err;
                      }
 
                      /* MADV_FREE page check */
@@ -1852,23 +1844,17 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
                               */
                              set_pte_at(mm, address, pvmw.pte, pteval);
                              folio_set_swapbacked(folio);
-                             ret = false;
-                             page_vma_mapped_walk_done(&pvmw);
-                             break;
+                             goto walk_done_err;
                      }
 
                      if (swap_duplicate(entry) < 0) {
                              set_pte_at(mm, address, pvmw.pte, pteval);
-                             ret = false;
-                             page_vma_mapped_walk_done(&pvmw);
-                             break;
+                             goto walk_done_err;
                      }
                      if (arch_unmap_one(mm, vma, address, pteval) < 0) {
                              swap_free(entry);
                              set_pte_at(mm, address, pvmw.pte, pteval);
-                             ret = false;
-                             page_vma_mapped_walk_done(&pvmw);
-                             break;
+                             goto walk_done_err;
                      }
 
                      /* See folio_try_share_anon_rmap(): clear PTE first. */
@@ -1876,9 +1862,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
                          folio_try_share_anon_rmap_pte(folio, subpage)) {
                              swap_free(entry);
                              set_pte_at(mm, address, pvmw.pte, pteval);
-                             ret = false;
-                             page_vma_mapped_walk_done(&pvmw);
-                             break;
+                             goto walk_done_err;
                      }
                      if (list_empty(&mm->mmlist)) {
                              spin_lock(&mmlist_lock);
@@ -1918,6 +1902,12 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
              if (vma->vm_flags & VM_LOCKED)
                      mlock_drain_local();
              folio_put(folio);
+             continue;
+walk_done_err:
+             ret = false;
+walk_done:
+             page_vma_mapped_walk_done(&pvmw);
+             break;
      }
 
      mmu_notifier_invalidate_range_end(&range);
author
#4 lore raw

PATCH RESEND v5 2/4 mm/rmap: integrate PMD-mapped folio splitting into pagewalk loop

In reply to Lance Yang

In preparation for supporting try_to_unmap_one() to unmap PMD-mapped folios, start the pagewalk first, then call split_huge_pmd_address() to split the folio.

---
 include/linux/huge_mm.h |  6 ++++++
 mm/huge_memory.c        | 42 +++++++++++++++++++++--------------------
 mm/rmap.c               | 23 ++++++++++++++++------
 3 files changed, 45 insertions(+), 26 deletions(-)
include/linux/huge_mm.h +6 -0
index c8d3ec116e29..9fcb0b0b6ed1 100644
@@ -409,6 +409,9 @@ static inline bool thp_migration_supported(void)
 	return IS_ENABLED(CONFIG_ARCH_ENABLE_THP_MIGRATION);
 }
 
+void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address,
+			   pmd_t *pmd, bool freeze, struct folio *folio);
+
 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
 
 static inline bool folio_test_pmd_mappable(struct folio *folio)
@@ -471,6 +474,9 @@ static inline void __split_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
 		unsigned long address, bool freeze, struct folio *folio) {}
 static inline void split_huge_pmd_address(struct vm_area_struct *vma,
 		unsigned long address, bool freeze, struct folio *folio) {}
+static inline void split_huge_pmd_locked(struct vm_area_struct *vma,
+					 unsigned long address, pmd_t *pmd,
+					 bool freeze, struct folio *folio) {}
 
 #define split_huge_pud(__vma, __pmd, __address)	\
 	do { } while (0)
mm/huge_memory.c +22 -20

54 lines not on this page · raw message

mm/rmap.c +17 -6
index ddffa30c79fb..4c4d14325f2e 100644
@@ -1640,9 +1640,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 	if (flags & TTU_SYNC)
 		pvmw.flags = PVMW_SYNC;
 
-	if (flags & TTU_SPLIT_HUGE_PMD)
-		split_huge_pmd_address(vma, address, false, folio);
-
 	/*
 	 * For THP, we have to assume the worse case ie pmd for invalidation.
 	 * For hugetlb, it could be much worse if we need to do pud
@@ -1668,9 +1665,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 	mmu_notifier_invalidate_range_start(&range);
 
 	while (page_vma_mapped_walk(&pvmw)) {
-		/* Unexpected PMD-mapped THP? */
-		VM_BUG_ON_FOLIO(!pvmw.pte, folio);
-
 		/*
 		 * If the folio is in an mlock()d vma, we must not swap it out.
 		 */
@@ -1682,6 +1676,23 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			goto walk_done_err;
 		}
 
+		if (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)) {
+			/*
+			 * We temporarily have to drop the PTL and start once
+			 * again from that now-PTE-mapped page table.
+			 */
+			split_huge_pmd_locked(vma, range.start, pvmw.pmd, false,
+					      folio);
+			pvmw.pmd = NULL;
+			spin_unlock(pvmw.ptl);
+			pvmw.ptl = NULL;
+			flags &= ~TTU_SPLIT_HUGE_PMD;
+			continue;
+		}
+
+		/* Unexpected PMD-mapped THP? */
+		VM_BUG_ON_FOLIO(!pvmw.pte, folio);
+
 		pfn = pte_pfn(ptep_get(pvmw.pte));
 		subpage = folio_page(folio, pfn - folio_pfn(folio));
 		address = pvmw.address;
Signature · 1 line
2.33.1
author
#5 lore raw

PATCH RESEND v5 3/4 mm/mlock: check for THP missing the mlock in try_to_unmap_one()

In reply to Lance Yang

The TTU_SPLIT_HUGE_PMD will no longer perform immediately, so we might encounter a PMD-mapped THP missing the mlock in the VM_LOCKED range during the pagewalk. It's likely necessary to mlock this THP to prevent it from being picked up during page reclaim.

---
 mm/rmap.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
mm/rmap.c +2 -1
index 4c4d14325f2e..08a93347f283 100644
@@ -1671,7 +1671,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		if (!(flags & TTU_IGNORE_MLOCK) &&
 		    (vma->vm_flags & VM_LOCKED)) {
 			/* Restore the mlock which got missed */
-			if (!folio_test_large(folio))
+			if (!folio_test_large(folio) ||
+			    (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)))
 				mlock_vma_folio(folio, vma);
 			goto walk_done_err;
 		}
Signature · 1 line
2.33.1
reviewer
#6 lore raw

Re: [PATCH RESEND v5 3/4] mm/mlock: check for THP missing the mlock in try_to_unmap_one()

Quoted 7 lines · Lance Yang

The TTU_SPLIT_HUGE_PMD will no longer perform immediately, so we might encounter a PMD-mapped THP missing the mlock in the VM_LOCKED range during the pagewalk. It's likely necessary to mlock this THP to prevent it from being picked up during page reclaim.

IMO, this patch should be merged into patch 2, otherwise patch 2 is buggy. Quote the description in the 'submission patches.rst' document:

"When dividing your change into a series of patches, take special care to ensure that the kernel builds and runs properly after each patch in the series. Developers using ``git bisect`` to track down a problem can end up splitting your patch series at any point; they will not thank you if you introduce bugs in the middle."

Quoted 18 lines
---
  mm/rmap.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
mm/rmap.c +2 -1
index 4c4d14325f2e..08a93347f283 100644
@@ -1671,7 +1671,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  		if (!(flags & TTU_IGNORE_MLOCK) &&
  		    (vma->vm_flags & VM_LOCKED)) {
  			/* Restore the mlock which got missed */
-			if (!folio_test_large(folio))
+			if (!folio_test_large(folio) ||
+			    (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)))
  				mlock_vma_folio(folio, vma);
  			goto walk_done_err;
  		}
author
#7 lore raw

Re: [PATCH RESEND v5 3/4] mm/mlock: check for THP missing the mlock in try_to_unmap_one()

Quoted 12 lines · Baolin Wang
Quoted 7 lines · Lance Yang

The TTU_SPLIT_HUGE_PMD will no longer perform immediately, so we might encounter a PMD-mapped THP missing the mlock in the VM_LOCKED range during the pagewalk. It's likely necessary to mlock this THP to prevent it from being picked up during page reclaim.

Quoted 8 lines

IMO, this patch should be merged into patch 2, otherwise patch 2 is buggy. Quote the description in the 'submission patches.rst' document:

"When dividing your change into a series of patches, take special care to ensure that the kernel builds and runs properly after each patch in the series. Developers using ``git bisect`` to track down a problem can end up splitting your patch series at any point; they will not thank you if you introduce bugs in the middle."

Thanks for bringing this up!

I completely agree that this patch should be merged into patch2.

Thanks,
Lance

Quoted 19 lines
Quoted 19 lines
---
  mm/rmap.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
mm/rmap.c +2 -1
index 4c4d14325f2e..08a93347f283 100644
@@ -1671,7 +1671,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
              if (!(flags & TTU_IGNORE_MLOCK) &&
                  (vma->vm_flags & VM_LOCKED)) {
                      /* Restore the mlock which got missed */
-                     if (!folio_test_large(folio))
+                     if (!folio_test_large(folio) ||
+                         (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)))
                              mlock_vma_folio(folio, vma);
                      goto walk_done_err;
              }
author
#8 lore raw

PATCH RESEND v5 4/4 mm/vmscan: avoid split lazyfree THP during shrink_folio_list()

In reply to Lance Yang

When the user no longer requires the pages, they would use madvise(MADV_FREE) to mark the pages as lazy free. Subsequently, they typically would not re-write to that memory again.

During memory reclaim, if we detect that the large folio and its PMD are both still marked as clean and there are no unexpected references (such as GUP), so we can just discard the memory lazily, improving the efficiency of memory reclamation in this case.

On an Intel i5 CPU, reclaiming 1GiB of lazyfree THPs using mem_cgroup_force_empty() results in the following runtimes in seconds (shorter is better):

--------------------------------------------
| Old | New | Change |
--------------------------------------------
| 0.683426 | 0.049197 | -92.80% |
--------------------------------------------

---
 include/linux/huge_mm.h |  9 +++++
 mm/huge_memory.c        | 75 +++++++++++++++++++++++++++++++++++++++++
 mm/rmap.c               | 31 ++++++++++-------
 3 files changed, 103 insertions(+), 12 deletions(-)
include/linux/huge_mm.h +9 -0
index 9fcb0b0b6ed1..cfd7ec2b6d0a 100644
@@ -411,6 +411,8 @@ static inline bool thp_migration_supported(void)
 
 void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address,
 			   pmd_t *pmd, bool freeze, struct folio *folio);
+bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
+			   pmd_t *pmdp, struct folio *folio);
 
 #else /* CONFIG_TRANSPARENT_HUGEPAGE */
 
@@ -478,6 +480,13 @@ static inline void split_huge_pmd_locked(struct vm_area_struct *vma,
 					 unsigned long address, pmd_t *pmd,
 					 bool freeze, struct folio *folio) {}
 
+static inline bool unmap_huge_pmd_locked(struct vm_area_struct *vma,
+					 unsigned long addr, pmd_t *pmdp,
+					 struct folio *folio)
+{
+	return false;
+}
+
 #define split_huge_pud(__vma, __pmd, __address)	\
 	do { } while (0)
 
mm/huge_memory.c +75 -0

81 lines not on this page · raw message

mm/rmap.c +19 -12
index 08a93347f283..e09f2141b8dc 100644
@@ -1677,18 +1677,25 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			goto walk_done_err;
 		}
 
-		if (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)) {
-			/*
-			 * We temporarily have to drop the PTL and start once
-			 * again from that now-PTE-mapped page table.
-			 */
-			split_huge_pmd_locked(vma, range.start, pvmw.pmd, false,
-					      folio);
-			pvmw.pmd = NULL;
-			spin_unlock(pvmw.ptl);
-			pvmw.ptl = NULL;
-			flags &= ~TTU_SPLIT_HUGE_PMD;
-			continue;
+		if (!pvmw.pte) {
+			if (unmap_huge_pmd_locked(vma, range.start, pvmw.pmd,
+						  folio))
+				goto walk_done;
+
+			if (flags & TTU_SPLIT_HUGE_PMD) {
+				/*
+				 * We temporarily have to drop the PTL and start
+				 * once again from that now-PTE-mapped page
+				 * table.
+				 */
+				split_huge_pmd_locked(vma, range.start,
+						      pvmw.pmd, false, folio);
+				pvmw.pmd = NULL;
+				spin_unlock(pvmw.ptl);
+				pvmw.ptl = NULL;
+				flags &= ~TTU_SPLIT_HUGE_PMD;
+				continue;
+			}
 		}
 
 		/* Unexpected PMD-mapped THP? */
Signature · 1 line
2.33.1
reviewer
#9 lore raw

Re: [PATCH RESEND v5 4/4] mm/vmscan: avoid split lazyfree THP during shrink_folio_list()

Quoted 71 lines · Lance Yang

When the user no longer requires the pages, they would use madvise(MADV_FREE) to mark the pages as lazy free. Subsequently, they typically would not re-write to that memory again.

During memory reclaim, if we detect that the large folio and its PMD are both still marked as clean and there are no unexpected references (such as GUP), so we can just discard the memory lazily, improving the efficiency of memory reclamation in this case.

On an Intel i5 CPU, reclaiming 1GiB of lazyfree THPs using mem_cgroup_force_empty() results in the following runtimes in seconds (shorter is better):

--------------------------------------------
| Old | New | Change |
--------------------------------------------
| 0.683426 | 0.049197 | -92.80% |
--------------------------------------------

---
  include/linux/huge_mm.h |  9 +++++
  mm/huge_memory.c        | 75 +++++++++++++++++++++++++++++++++++++++++
  mm/rmap.c               | 31 ++++++++++-------
  3 files changed, 103 insertions(+), 12 deletions(-)
include/linux/huge_mm.h +9 -0
index 9fcb0b0b6ed1..cfd7ec2b6d0a 100644
@@ -411,6 +411,8 @@ static inline bool thp_migration_supported(void)
 
  void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address,
  			   pmd_t *pmd, bool freeze, struct folio *folio);
+bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
+			   pmd_t *pmdp, struct folio *folio);
 
  #else /* CONFIG_TRANSPARENT_HUGEPAGE */
 
@@ -478,6 +480,13 @@ static inline void split_huge_pmd_locked(struct vm_area_struct *vma,
  					 unsigned long address, pmd_t *pmd,
  					 bool freeze, struct folio *folio) {}
 
+static inline bool unmap_huge_pmd_locked(struct vm_area_struct *vma,
+					 unsigned long addr, pmd_t *pmdp,
+					 struct folio *folio)
+{
+	return false;
+}
+
  #define split_huge_pud(__vma, __pmd, __address)	\
  	do { } while (0)
 
mm/huge_memory.c +8 -0
index 425272c6c50b..3ceeeb2f42d4 100644
@@ -2687,6 +2687,81 @@ static void unmap_folio(struct folio *folio)
  	try_to_unmap_flush();
  }
 
+static bool __discard_trans_pmd_locked(struct vm_area_struct *vma,
+				       unsigned long addr, pmd_t *pmdp,
+				       struct folio *folio)
+{
+	struct mm_struct *mm = vma->vm_mm;
+	int ref_count, map_count;
+	pmd_t orig_pmd = *pmdp;
+	struct mmu_gather tlb;

mm/huge_memory.c static void unmap_folio(struct folio *folio) · line 2707

Quoted 10 lines
mm/huge_memory.c +10 -0
+	struct page *page;
+
+	if (pmd_dirty(orig_pmd) || folio_test_dirty(folio))
+		return false;
+	if (unlikely(!pmd_present(orig_pmd) || !pmd_trans_huge(orig_pmd)))
+		return false;
+
+	page = pmd_page(orig_pmd);
+	if (unlikely(page_folio(page) != folio))
+		return false;

The function is called under the ptl lock, so I have no idea why the pmd value can be changed, seems above validation is useless.

mm/huge_memory.c static void unmap_folio(struct folio *folio) · line 2709

Quoted 2 lines
mm/huge_memory.c +2 -0
+
+	tlb_gather_mmu(&tlb, mm);

You missed tlb_finish_mmu() to do tlb flushing, and ...

mm/huge_memory.c static void unmap_folio(struct folio *folio) · line 2711

Quoted 2 lines
mm/huge_memory.c +2 -0
+	orig_pmd = pmdp_huge_get_and_clear(mm, addr, pmdp);
+	tlb_remove_pmd_tlb_entry(&tlb, pmdp, addr);

I don't think tlb gather is helpful here, since you just flush one PMD entry. Just using pmdp_huge_clear_flush() seems enough.

Quoted 17 lines
mm/huge_memory.c +17 -0
+
+	/*
+	 * Syncing against concurrent GUP-fast:
+	 * - clear PMD; barrier; read refcount
+	 * - inc refcount; barrier; read PMD
+	 */
+	smp_mb();
+
+	ref_count = folio_ref_count(folio);
+	map_count = folio_mapcount(folio);
+
+	/*
+	 * Order reads for folio refcount and dirty flag
+	 * (see comments in __remove_mapping()).
+	 */
+	smp_rmb();
+

mm/huge_memory.c static void unmap_folio(struct folio *folio) · line 2738

Quoted 10 lines
mm/huge_memory.c +10 -0
+	/*
+	 * If the PMD or folio is redirtied at this point, or if there are
+	 * unexpected references, we will give up to discard this folio
+	 * and remap it.
+	 *
+	 * The only folio refs must be one from isolation plus the rmap(s).
+	 */
+	if (ref_count != map_count + 1 || folio_test_dirty(folio) ||
+	    pmd_dirty(orig_pmd)) {
+		set_pmd_at(mm, addr, pmdp, orig_pmd);

Should we also call 'folio_set_swapbacked()' if the folio was redirtied?

Quoted 71 lines
mm/huge_memory.c +26 -0
+		return false;
+	}
+
+	folio_remove_rmap_pmd(folio, page, vma);
+	zap_deposited_table(mm, pmdp);
+	add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
+	if (vma->vm_flags & VM_LOCKED)
+		mlock_drain_local();
+	folio_put(folio);
+
+	return true;
+}
+
+bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
+			   pmd_t *pmdp, struct folio *folio)
+{
+	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));
+
+	if (folio_test_anon(folio) && !folio_test_swapbacked(folio))
+		return __discard_trans_pmd_locked(vma, addr, pmdp, folio);
+
+	return false;
+}
+
  static void remap_page(struct folio *folio, unsigned long nr)
  {
  	int i = 0;
mm/rmap.c +19 -12
index 08a93347f283..e09f2141b8dc 100644
@@ -1677,18 +1677,25 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  			goto walk_done_err;
  		}
 
-		if (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)) {
-			/*
-			 * We temporarily have to drop the PTL and start once
-			 * again from that now-PTE-mapped page table.
-			 */
-			split_huge_pmd_locked(vma, range.start, pvmw.pmd, false,
-					      folio);
-			pvmw.pmd = NULL;
-			spin_unlock(pvmw.ptl);
-			pvmw.ptl = NULL;
-			flags &= ~TTU_SPLIT_HUGE_PMD;
-			continue;
+		if (!pvmw.pte) {
+			if (unmap_huge_pmd_locked(vma, range.start, pvmw.pmd,
+						  folio))
+				goto walk_done;
+
+			if (flags & TTU_SPLIT_HUGE_PMD) {
+				/*
+				 * We temporarily have to drop the PTL and start
+				 * once again from that now-PTE-mapped page
+				 * table.
+				 */
+				split_huge_pmd_locked(vma, range.start,
+						      pvmw.pmd, false, folio);
+				pvmw.pmd = NULL;
+				spin_unlock(pvmw.ptl);
+				pvmw.ptl = NULL;
+				flags &= ~TTU_SPLIT_HUGE_PMD;
+				continue;
+			}
  		}
 
  		/* Unexpected PMD-mapped THP? */
author
#10 lore raw

Re: [PATCH RESEND v5 4/4] mm/vmscan: avoid split lazyfree THP during shrink_folio_list()

Hi Baolin,

Thanks for taking time to review!

Quoted 80 lines · Baolin Wang
Quoted 76 lines · Lance Yang

When the user no longer requires the pages, they would use madvise(MADV_FREE) to mark the pages as lazy free. Subsequently, they typically would not re-write to that memory again.

During memory reclaim, if we detect that the large folio and its PMD are both still marked as clean and there are no unexpected references (such as GUP), so we can just discard the memory lazily, improving the efficiency of memory reclamation in this case.

On an Intel i5 CPU, reclaiming 1GiB of lazyfree THPs using mem_cgroup_force_empty() results in the following runtimes in seconds (shorter is better):

--------------------------------------------
| Old | New | Change |
--------------------------------------------
| 0.683426 | 0.049197 | -92.80% |
--------------------------------------------

---
  include/linux/huge_mm.h |  9 +++++
  mm/huge_memory.c        | 75 +++++++++++++++++++++++++++++++++++++++++
  mm/rmap.c               | 31 ++++++++++-------
  3 files changed, 103 insertions(+), 12 deletions(-)
include/linux/huge_mm.h +9 -0
index 9fcb0b0b6ed1..cfd7ec2b6d0a 100644
@@ -411,6 +411,8 @@ static inline bool thp_migration_supported(void)
 
  void split_huge_pmd_locked(struct vm_area_struct *vma, unsigned long address,
                         pmd_t *pmd, bool freeze, struct folio *folio);
+bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
+                        pmd_t *pmdp, struct folio *folio);
 
  #else /* CONFIG_TRANSPARENT_HUGEPAGE */
 
@@ -478,6 +480,13 @@ static inline void split_huge_pmd_locked(struct vm_area_struct *vma,
                                       unsigned long address, pmd_t *pmd,
                                       bool freeze, struct folio *folio) {}
 
+static inline bool unmap_huge_pmd_locked(struct vm_area_struct *vma,
+                                      unsigned long addr, pmd_t *pmdp,
+                                      struct folio *folio)
+{
+     return false;
+}
+
  #define split_huge_pud(__vma, __pmd, __address)     \
      do { } while (0)
 
mm/huge_memory.c +13 -0
index 425272c6c50b..3ceeeb2f42d4 100644
@@ -2687,6 +2687,81 @@ static void unmap_folio(struct folio *folio)
      try_to_unmap_flush();
  }
 
+static bool __discard_trans_pmd_locked(struct vm_area_struct *vma,
+                                    unsigned long addr, pmd_t *pmdp,
+                                    struct folio *folio)
+{
+     struct mm_struct *mm = vma->vm_mm;
+     int ref_count, map_count;
+     pmd_t orig_pmd = *pmdp;
+     struct mmu_gather tlb;
+     struct page *page;
+
+     if (pmd_dirty(orig_pmd) || folio_test_dirty(folio))
+             return false;
+     if (unlikely(!pmd_present(orig_pmd) || !pmd_trans_huge(orig_pmd)))
Quoted 8 lines

mm/huge_memory.c static void unmap_folio(struct folio *folio) · line 2707

Quoted 5 lines
mm/huge_memory.c +5 -0
+             return false;
+
+     page = pmd_page(orig_pmd);
+     if (unlikely(page_folio(page) != folio))
+             return false;

The function is called under the ptl lock, so I have no idea why the pmd value can be changed, seems above validation is useless.

Yep. For the page reclaim path, page_vma_mapped_walk() will require the PTL, and make sure the PMD mapping is unchanged via check_pmd().

But, IMO, we cannot assume that all callers always do the PMD mapping check. I suggest keeping it to prepare for corner cases :)

Quoted 5 lines

mm/huge_memory.c static void unmap_folio(struct folio *folio) · line 2709

Quoted 3 lines
mm/huge_memory.c +2 -0
+
+     tlb_gather_mmu(&tlb, mm);

You missed tlb_finish_mmu() to do tlb flushing, and ...

Good spot!

Quoted 6 lines

mm/huge_memory.c static void unmap_folio(struct folio *folio) · line 2711

Quoted 3 lines
mm/huge_memory.c +2 -0
+     orig_pmd = pmdp_huge_get_and_clear(mm, addr, pmdp);
+     tlb_remove_pmd_tlb_entry(&tlb, pmdp, addr);

I don't think tlb gather is helpful here, since you just flush one PMD entry. Just using pmdp_huge_clear_flush() seems enough.

Nice, thanks for the suggestion!

I completely agree that using pmdp_huge_clear_flush() is sufficient here :)

Quoted 22 lines
Quoted 22 lines
mm/huge_memory.c +21 -0
+
+     /*
+      * Syncing against concurrent GUP-fast:
+      * - clear PMD; barrier; read refcount
+      * - inc refcount; barrier; read PMD
+      */
+     smp_mb();
+
+     ref_count = folio_ref_count(folio);
+     map_count = folio_mapcount(folio);
+
+     /*
+      * Order reads for folio refcount and dirty flag
+      * (see comments in __remove_mapping()).
+      */
+     smp_rmb();
+
+     /*
+      * If the PMD or folio is redirtied at this point, or if there are
+      * unexpected references, we will give up to discard this folio
+      * and remap it.
Quoted 8 lines

mm/huge_memory.c static void unmap_folio(struct folio *folio) · line 2738

Quoted 6 lines
mm/huge_memory.c +6 -0
+      *
+      * The only folio refs must be one from isolation plus the rmap(s).
+      */
+     if (ref_count != map_count + 1 || folio_test_dirty(folio) ||
+         pmd_dirty(orig_pmd)) {
+             set_pmd_at(mm, addr, pmdp, orig_pmd);

Should we also call 'folio_set_swapbacked()' if the folio was redirtied?

Yes. I forgot about that :)

Thanks again for the review!
Lance

Quoted 72 lines
Quoted 72 lines
mm/huge_memory.c +26 -0
+             return false;
+     }
+
+     folio_remove_rmap_pmd(folio, page, vma);
+     zap_deposited_table(mm, pmdp);
+     add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
+     if (vma->vm_flags & VM_LOCKED)
+             mlock_drain_local();
+     folio_put(folio);
+
+     return true;
+}
+
+bool unmap_huge_pmd_locked(struct vm_area_struct *vma, unsigned long addr,
+                        pmd_t *pmdp, struct folio *folio)
+{
+     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));
+
+     if (folio_test_anon(folio) && !folio_test_swapbacked(folio))
+             return __discard_trans_pmd_locked(vma, addr, pmdp, folio);
+
+     return false;
+}
+
  static void remap_page(struct folio *folio, unsigned long nr)
  {
      int i = 0;
mm/rmap.c +19 -12
index 08a93347f283..e09f2141b8dc 100644
@@ -1677,18 +1677,25 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
                      goto walk_done_err;
              }
 
-             if (!pvmw.pte && (flags & TTU_SPLIT_HUGE_PMD)) {
-                     /*
-                      * We temporarily have to drop the PTL and start once
-                      * again from that now-PTE-mapped page table.
-                      */
-                     split_huge_pmd_locked(vma, range.start, pvmw.pmd, false,
-                                           folio);
-                     pvmw.pmd = NULL;
-                     spin_unlock(pvmw.ptl);
-                     pvmw.ptl = NULL;
-                     flags &= ~TTU_SPLIT_HUGE_PMD;
-                     continue;
+             if (!pvmw.pte) {
+                     if (unmap_huge_pmd_locked(vma, range.start, pvmw.pmd,
+                                               folio))
+                             goto walk_done;
+
+                     if (flags & TTU_SPLIT_HUGE_PMD) {
+                             /*
+                              * We temporarily have to drop the PTL and start
+                              * once again from that now-PTE-mapped page
+                              * table.
+                              */
+                             split_huge_pmd_locked(vma, range.start,
+                                                   pvmw.pmd, false, folio);
+                             pvmw.pmd = NULL;
+                             spin_unlock(pvmw.ptl);
+                             pvmw.ptl = NULL;
+                             flags &= ~TTU_SPLIT_HUGE_PMD;
+                             continue;
+                     }
              }
 
              /* Unexpected PMD-mapped THP? */