Linux-mm Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/5] mm/rmap: Refactor try_to_unmap_one
@ 2026-07-10  5:59 Dev Jain
  2026-07-10  5:59 ` [PATCH v2 1/5] mm/rmap: convert page -> folio for hwpoison checks Dev Jain
                   ` (4 more replies)
  0 siblings, 5 replies; 12+ messages in thread
From: Dev Jain @ 2026-07-10  5:59 UTC (permalink / raw)
  To: akpm, david, ljs, muchun.song, osalvador
  Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual

In preparation for batching anonymous large folio unmapping to optimize it,
refactor try_to_unmap_one. This series refactors hugetlb, anon-lazyfree
and anon-swapbacked logic into their own functions, significantly
reducing the length of the huge try_to_unmap_one.

---
Split out from
https://lore.kernel.org/all/20260526063635.61721-1-dev.jain@arm.com/

mm-selftests pass on arm64, built on arm64 and x86.

v1->v2:
 - constify ttu flags in try_to_unmap_hugetlb_one, move const variables up
 - Assert address passed to try_to_unmap_hugetlb_one equals pvmw.address
 - Patch 3: code and comment polishing, do the stat update in the function
 - Remove unnecessary inline hints, remove set_swp_pte_at, instead have
   swp_pte_prepare + set_ptes. Rename subpage -> page. constify
   anon_exclusive.
 - Do pte restoration in caller

Dev Jain (5):
  mm/rmap: convert page -> folio for hwpoison checks
  mm/rmap: Add try_to_unmap_hugetlb_one
  mm/rmap: refactor some code around lazyfree folio unmapping
  mm/rmap: refactor anon folio unmap in try_to_unmap_one
  mm/rmap: add anon folio unmap dispatcher

 include/linux/hugetlb.h |   1 +
 mm/rmap.c               | 420 ++++++++++++++++++++++------------------
 2 files changed, 237 insertions(+), 184 deletions(-)

-- 
2.43.0



^ permalink raw reply	[flat|nested] 12+ messages in thread

* [PATCH v2 1/5] mm/rmap: convert page -> folio for hwpoison checks
  2026-07-10  5:59 [PATCH v2 0/5] mm/rmap: Refactor try_to_unmap_one Dev Jain
@ 2026-07-10  5:59 ` Dev Jain
  2026-07-10 12:22   ` David Hildenbrand (Arm)
  2026-07-10  5:59 ` [PATCH v2 2/5] mm/rmap: Add try_to_unmap_hugetlb_one Dev Jain
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 12+ messages in thread
From: Dev Jain @ 2026-07-10  5:59 UTC (permalink / raw)
  To: akpm, david, ljs, muchun.song, osalvador
  Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual

try_to_unmap() receives hugetlb folios only from the hwpoison path.
hugetlb_update_hwpoison() sets the hugetlb folio's head-page
hwpoison bit, and page_vma_mapped_walk() reports the hugetlb mapping at
the head PFN, so the previous PageHWPoison(subpage) check happened to
work for hugetlb.

For non-hugetlb folios, unmap_poisoned_folio() currently rejects large
folios before calling try_to_unmap(). Hence it is always the case that
if try_to_unmap_one() handles an hwpoisoned folio, then the head page is
marked with the poison bit.

Therefore, convert the poisoned subpage checks to folio_test_hwpoison().

No functional change intended, except that, while at it,
convert VM_BUG_* to VM_WARN_*.

Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
 mm/rmap.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)

diff --git a/mm/rmap.c b/mm/rmap.c
index 9939400e77c79..dbb077f8443e3 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2122,10 +2122,11 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			bool anon = folio_test_anon(folio);
 
 			/*
-			 * The try_to_unmap() is only passed a hugetlb page
-			 * in the case where the hugetlb page is poisoned.
+			 * The try_to_unmap() is only passed a hugetlb folio
+			 * in the case where the hugetlb folio contains a
+			 * poisoned page.
 			 */
-			VM_BUG_ON_PAGE(!PageHWPoison(subpage), subpage);
+			VM_WARN_ON_FOLIO(!folio_test_hwpoison(folio), folio);
 			/*
 			 * huge_pmd_unshare may unmap an entire PMD page.
 			 * There is no way of knowing exactly which PMDs may
@@ -2204,7 +2205,11 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		/* Update high watermark before we lower rss */
 		update_hiwater_rss(mm);
 
-		if (PageHWPoison(subpage) && (flags & TTU_HWPOISON)) {
+		/*
+		 * With TTU_HWPOISON, we only expect small folios or hugetlb
+		 * folios here for now.
+		 */
+		if (folio_test_hwpoison(folio) && (flags & TTU_HWPOISON)) {
 			pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
 			if (folio_test_hugetlb(folio)) {
 				hugetlb_count_sub(folio_nr_pages(folio), mm);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v2 2/5] mm/rmap: Add try_to_unmap_hugetlb_one
  2026-07-10  5:59 [PATCH v2 0/5] mm/rmap: Refactor try_to_unmap_one Dev Jain
  2026-07-10  5:59 ` [PATCH v2 1/5] mm/rmap: convert page -> folio for hwpoison checks Dev Jain
@ 2026-07-10  5:59 ` Dev Jain
  2026-07-10 13:16   ` Dev Jain
  2026-07-10  5:59 ` [PATCH v2 3/5] mm/rmap: refactor some code around lazyfree folio unmapping Dev Jain
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 12+ messages in thread
From: Dev Jain @ 2026-07-10  5:59 UTC (permalink / raw)
  To: akpm, david, ljs, muchun.song, osalvador
  Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual

Simplify try_to_unmap_one() by separating the hugetlb parts into
try_to_unmap_hugetlb_one().

To understand the correctness of the refactoring, the following points
are noted:

1. try_to_unmap() is called for hugetlb folios only when they are
   hwpoisoned.

2. A hugetlb VMA cannot be mlocked.

3. page_vma_mapped_walk() returns at most one hugetlb mapping in a VMA,
   and that mapping points at the head PFN.

4. We won't ever process a softleaf entry that encodes a hugetlb folio;
   hugetlb folios are never swapped out, migration entries will be
   skipped (PVMW_MIGRATION not passed), and device-exclusive does not
   work for hugetlb.

5. The hwpoison entry is constructed from the poisoned folio, just as in
   the pre-refactor code. Any previous uffd-wp state is deliberately not
   preserved for the hwpoison entry.

6. TTU_HWPOISON is always present; for it to not be present, either the
   folio has to be in swapcache, or mapping_can_writeback() is true (see
   unmap_poisoned_folio), none of which is true for hugetlb folios.

7. Hugetlb uses separate counters from normal rss counters, therefore
   update_highwater_rss() need not be called.

While at it:

 - Change VM_BUG_* to VM_WARN_*.

 - Do not declare variables which are only used once.

 - Constify some variables.

 - Add some more VM_WARN_* to assert some invariants.

Except the above 4 points, no functional change intended.

Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
 include/linux/hugetlb.h |   1 +
 mm/rmap.c               | 178 ++++++++++++++++++++++------------------
 2 files changed, 97 insertions(+), 82 deletions(-)

diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
index 4115076e4922a..bf7e163e3779d 100644
--- a/include/linux/hugetlb.h
+++ b/include/linux/hugetlb.h
@@ -1271,6 +1271,7 @@ static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
 }
 
 pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
+unsigned long huge_pte_dirty(pte_t pte);
 
 static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
 					  unsigned long addr, pte_t *ptep)
diff --git a/mm/rmap.c b/mm/rmap.c
index dbb077f8443e3..cf02b315a2ead 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -1978,6 +1978,96 @@ static inline unsigned int folio_unmap_pte_batch(struct folio *folio,
 				     FPB_RESPECT_WRITE | FPB_RESPECT_SOFT_DIRTY);
 }
 
+static bool try_to_unmap_hugetlb_one(struct folio *folio,
+		struct vm_area_struct *vma, unsigned long address, void *arg)
+{
+	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);
+	const unsigned long hsz = huge_page_size(hstate_vma(vma));
+	const enum ttu_flags flags = (enum ttu_flags)(long)arg;
+	struct mm_struct *mm = vma->vm_mm;
+	struct mmu_notifier_range range;
+	bool ret = true;
+	pte_t pteval;
+
+	/*
+	 * The try_to_unmap() is only passed a hugetlb folio in the case
+	 * where the hugetlb folio is poisoned.
+	 */
+	VM_WARN_ON_FOLIO(!folio_test_hwpoison(folio), folio);
+	VM_WARN_ON_ONCE(!(flags & TTU_HWPOISON));
+
+	range.end = vma_address_end(&pvmw);
+	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
+				address, range.end);
+	adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
+	mmu_notifier_invalidate_range_start(&range);
+
+	/* There is only a single mapping in a VMA. */
+	if (!page_vma_mapped_walk(&pvmw))
+		goto range_end;
+
+	VM_WARN_ON_ONCE(address != pvmw.address);
+
+	pteval = huge_ptep_get(mm, address, pvmw.pte);
+	VM_WARN_ON_ONCE(!pte_present(pteval));
+	VM_WARN_ON_ONCE(pte_pfn(pteval) != folio_pfn(folio));
+
+	/*
+	 * huge_pmd_unshare may unmap an entire PMD page. There is no way of
+	 * knowing exactly which PMDs may be cached for this mm, so we must
+	 * flush them all. start/end were already adjusted above to cover this
+	 * range.
+	 */
+	flush_cache_range(vma, range.start, range.end);
+
+	/*
+	 * To call huge_pmd_unshare, i_mmap_rwsem must be held in write mode.
+	 * Caller needs to explicitly do this outside rmap routines.
+	 *
+	 * We also must hold hugetlb vma_lock in write mode. Lock order dictates
+	 * acquiring vma_lock BEFORE i_mmap_rwsem. We can only try lock here and
+	 * fail if unsuccessful.
+	 */
+	if (!folio_test_anon(folio)) {
+		struct mmu_gather tlb;
+
+		VM_WARN_ON(!(flags & TTU_RMAP_LOCKED));
+		if (!hugetlb_vma_trylock_write(vma)) {
+			ret = false;
+			goto walk_done;
+		}
+
+		tlb_gather_mmu_vma(&tlb, vma);
+		if (huge_pmd_unshare(&tlb, vma, address, pvmw.pte)) {
+			hugetlb_vma_unlock_write(vma);
+			huge_pmd_unshare_flush(&tlb, vma);
+			tlb_finish_mmu(&tlb);
+			/*
+			 * The PMD table was unmapped, consequently unmapping
+			 * the folio.
+			 */
+			goto walk_done;
+		}
+		hugetlb_vma_unlock_write(vma);
+		tlb_finish_mmu(&tlb);
+	}
+	pteval = huge_ptep_clear_flush(vma, address, pvmw.pte);
+	if (huge_pte_dirty(pteval))
+		folio_mark_dirty(folio);
+
+	pteval = swp_entry_to_pte(make_hwpoison_entry(folio_page(folio, 0)));
+	hugetlb_count_sub(folio_nr_pages(folio), mm);
+	set_huge_pte_at(mm, address, pvmw.pte, pteval, hsz);
+	hugetlb_remove_rmap(folio);
+	folio_put_refs(folio, 1);
+
+walk_done:
+	page_vma_mapped_walk_done(&pvmw);
+range_end:
+	mmu_notifier_invalidate_range_end(&range);
+	return ret;
+}
+
 /*
  * @arg: enum ttu_flags will be passed to this argument
  */
@@ -1993,7 +2083,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 	enum ttu_flags flags = (enum ttu_flags)(long)arg;
 	unsigned long nr_pages = 1, end_addr;
 	unsigned long pfn;
-	unsigned long hsz = 0;
 	int ptes = 0;
 
 	/*
@@ -2007,8 +2096,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 
 	/*
 	 * 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
-	 * invalidation in the case of pmd sharing.
 	 *
 	 * Note that the folio can not be freed in this function as call of
 	 * try_to_unmap() must hold a reference on the folio.
@@ -2016,17 +2103,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 	range.end = vma_address_end(&pvmw);
 	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
 				address, range.end);
-	if (folio_test_hugetlb(folio)) {
-		/*
-		 * If sharing is possible, start and end will be adjusted
-		 * accordingly.
-		 */
-		adjust_range_if_pmd_sharing_possible(vma, &range.start,
-						     &range.end);
-
-		/* We need the huge page size for set_huge_pte_at() */
-		hsz = huge_page_size(hstate_vma(vma));
-	}
 	mmu_notifier_invalidate_range_start(&range);
 
 	while (page_vma_mapped_walk(&pvmw)) {
@@ -2111,66 +2187,13 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			const softleaf_t entry = softleaf_from_pte(pteval);
 
 			pfn = softleaf_to_pfn(entry);
-			VM_WARN_ON_FOLIO(folio_test_hugetlb(folio), folio);
 		}
 
 		subpage = folio_page(folio, pfn - folio_pfn(folio));
 		anon_exclusive = folio_test_anon(folio) &&
 				 PageAnonExclusive(subpage);
 
-		if (folio_test_hugetlb(folio)) {
-			bool anon = folio_test_anon(folio);
-
-			/*
-			 * The try_to_unmap() is only passed a hugetlb folio
-			 * in the case where the hugetlb folio contains a
-			 * poisoned page.
-			 */
-			VM_WARN_ON_FOLIO(!folio_test_hwpoison(folio), folio);
-			/*
-			 * huge_pmd_unshare may unmap an entire PMD page.
-			 * There is no way of knowing exactly which PMDs may
-			 * be cached for this mm, so we must flush them all.
-			 * start/end were already adjusted above to cover this
-			 * range.
-			 */
-			flush_cache_range(vma, range.start, range.end);
-
-			/*
-			 * To call huge_pmd_unshare, i_mmap_rwsem must be
-			 * held in write mode.  Caller needs to explicitly
-			 * do this outside rmap routines.
-			 *
-			 * We also must hold hugetlb vma_lock in write mode.
-			 * Lock order dictates acquiring vma_lock BEFORE
-			 * i_mmap_rwsem.  We can only try lock here and fail
-			 * if unsuccessful.
-			 */
-			if (!anon) {
-				struct mmu_gather tlb;
-
-				VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
-				if (!hugetlb_vma_trylock_write(vma))
-					goto walk_abort;
-
-				tlb_gather_mmu_vma(&tlb, vma);
-				if (huge_pmd_unshare(&tlb, vma, address, pvmw.pte)) {
-					hugetlb_vma_unlock_write(vma);
-					huge_pmd_unshare_flush(&tlb, vma);
-					tlb_finish_mmu(&tlb);
-					/*
-					 * The PMD table was unmapped,
-					 * consequently unmapping the folio.
-					 */
-					goto walk_done;
-				}
-				hugetlb_vma_unlock_write(vma);
-				tlb_finish_mmu(&tlb);
-			}
-			pteval = huge_ptep_clear_flush(vma, address, pvmw.pte);
-			if (pte_dirty(pteval))
-				folio_mark_dirty(folio);
-		} else if (likely(pte_present(pteval))) {
+		if (likely(pte_present(pteval))) {
 			nr_pages = folio_unmap_pte_batch(folio, &pvmw, flags, pteval);
 			end_addr = address + nr_pages * PAGE_SIZE;
 			flush_cache_range(vma, address, end_addr);
@@ -2211,14 +2234,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		 */
 		if (folio_test_hwpoison(folio) && (flags & TTU_HWPOISON)) {
 			pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
-			if (folio_test_hugetlb(folio)) {
-				hugetlb_count_sub(folio_nr_pages(folio), mm);
-				set_huge_pte_at(mm, address, pvmw.pte, pteval,
-						hsz);
-			} else {
-				dec_mm_counter(mm, mm_counter(folio));
-				set_pte_at(mm, address, pvmw.pte, pteval);
-			}
+			dec_mm_counter(mm, mm_counter(folio));
+			set_pte_at(mm, address, pvmw.pte, pteval);
 		} else if (likely(pte_present(pteval)) && pte_unused(pteval) &&
 			   !userfaultfd_armed(vma)) {
 			/*
@@ -2346,11 +2363,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			add_mm_counter(mm, mm_counter_file(folio), -nr_pages);
 		}
 discard:
-		if (unlikely(folio_test_hugetlb(folio))) {
-			hugetlb_remove_rmap(folio);
-		} else {
-			folio_remove_rmap_ptes(folio, subpage, nr_pages, vma);
-		}
+		folio_remove_rmap_ptes(folio, subpage, nr_pages, vma);
 		if (vma->vm_flags & VM_LOCKED)
 			mlock_drain_local();
 		folio_put_refs(folio, nr_pages);
@@ -2398,7 +2411,8 @@ static int folio_not_mapped(struct folio *folio)
 void try_to_unmap(struct folio *folio, enum ttu_flags flags)
 {
 	struct rmap_walk_control rwc = {
-		.rmap_one = try_to_unmap_one,
+		.rmap_one = folio_test_hugetlb(folio) ?
+				try_to_unmap_hugetlb_one : try_to_unmap_one,
 		.arg = (void *)flags,
 		.done = folio_not_mapped,
 		.anon_lock = folio_lock_anon_vma_read,
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v2 3/5] mm/rmap: refactor some code around lazyfree folio unmapping
  2026-07-10  5:59 [PATCH v2 0/5] mm/rmap: Refactor try_to_unmap_one Dev Jain
  2026-07-10  5:59 ` [PATCH v2 1/5] mm/rmap: convert page -> folio for hwpoison checks Dev Jain
  2026-07-10  5:59 ` [PATCH v2 2/5] mm/rmap: Add try_to_unmap_hugetlb_one Dev Jain
@ 2026-07-10  5:59 ` Dev Jain
  2026-07-10  5:59 ` [PATCH v2 4/5] mm/rmap: refactor anon folio unmap in try_to_unmap_one Dev Jain
  2026-07-10  5:59 ` [PATCH v2 5/5] mm/rmap: add anon folio unmap dispatcher Dev Jain
  4 siblings, 0 replies; 12+ messages in thread
From: Dev Jain @ 2026-07-10  5:59 UTC (permalink / raw)
  To: akpm, david, ljs, muchun.song, osalvador
  Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual

For lazyfree folio unmapping, after clearing the ptes we must abort the
operation if the folio got dirtied or it has unexpected references.

Refactor this logic into a function which will return whether we need
to abort or not.

If we abort, we restore the ptes and bail out of try_to_unmap_one.
Otherwise adjust the rss stats of the mm and jump to a label.

Also rename that label from "discard" to "finish_unmap"; the former
is appropriate in the lazyfree context, but the code following the label
is executed for other successful unmap code paths too, so 'discard' does
not sound correct for them.

No functional change intended.

Acked-by: David Hildenbrand (Arm) <david@kernel.org>
Signed-off-by: Dev Jain <dev.jain@arm.com>
---
 mm/rmap.c | 87 ++++++++++++++++++++++++++++++-------------------------
 1 file changed, 48 insertions(+), 39 deletions(-)

diff --git a/mm/rmap.c b/mm/rmap.c
index cf02b315a2ead..4851c9f2a34e7 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2068,6 +2068,50 @@ static bool try_to_unmap_hugetlb_one(struct folio *folio,
 	return ret;
 }
 
+static bool ttu_anon_lazyfree_folio(struct vm_area_struct *vma,
+		struct folio *folio, unsigned long nr_pages)
+{
+	int ref_count, map_count;
+
+	/*
+	 * Synchronize with gup_pte_range():
+	 * - clear PTE; barrier; read refcount
+	 * - inc refcount; barrier; read PTE
+	 */
+	smp_mb();
+
+	ref_count = folio_ref_count(folio);
+	map_count = folio_mapcount(folio);
+
+	/*
+	 * Order reads for page refcount and dirty flag
+	 * (see comments in __remove_mapping()).
+	 */
+	smp_rmb();
+
+	if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) {
+		/*
+		 * redirtied either using the page table or a previously
+		 * obtained GUP reference.
+		 */
+		folio_set_swapbacked(folio);
+		return false;
+	}
+
+	/*
+	 * Additional references could be due to GUP or speculative lookups.
+	 * 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.
+	 */
+	if (ref_count != 1 + map_count)
+		return false;
+
+	add_mm_counter(vma->vm_mm, MM_ANONPAGES, -nr_pages);
+	return true;
+}
+
 /*
  * @arg: enum ttu_flags will be passed to this argument
  */
@@ -2264,47 +2308,12 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 
 			/* MADV_FREE page check */
 			if (!folio_test_swapbacked(folio)) {
-				int ref_count, map_count;
-
-				/*
-				 * Synchronize with gup_pte_range():
-				 * - clear PTE; barrier; read refcount
-				 * - inc refcount; barrier; read PTE
-				 */
-				smp_mb();
-
-				ref_count = folio_ref_count(folio);
-				map_count = folio_mapcount(folio);
-
-				/*
-				 * Order reads for page refcount and dirty flag
-				 * (see comments in __remove_mapping()).
-				 */
-				smp_rmb();
-
-				if (folio_test_dirty(folio) && !(vma->vm_flags & VM_DROPPABLE)) {
-					/*
-					 * redirtied either using the page table or a previously
-					 * obtained GUP reference.
-					 */
-					set_ptes(mm, address, pvmw.pte, pteval, nr_pages);
-					folio_set_swapbacked(folio);
-					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.
-					 */
+				if (!ttu_anon_lazyfree_folio(vma, folio,
+							     nr_pages)) {
 					set_ptes(mm, address, pvmw.pte, pteval, nr_pages);
 					goto walk_abort;
 				}
-				add_mm_counter(mm, MM_ANONPAGES, -nr_pages);
-				goto discard;
+				goto finish_unmap;
 			}
 
 			if (folio_dup_swap(folio, subpage) < 0) {
@@ -2362,7 +2371,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			 */
 			add_mm_counter(mm, mm_counter_file(folio), -nr_pages);
 		}
-discard:
+finish_unmap:
 		folio_remove_rmap_ptes(folio, subpage, nr_pages, vma);
 		if (vma->vm_flags & VM_LOCKED)
 			mlock_drain_local();
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v2 4/5] mm/rmap: refactor anon folio unmap in try_to_unmap_one
  2026-07-10  5:59 [PATCH v2 0/5] mm/rmap: Refactor try_to_unmap_one Dev Jain
                   ` (2 preceding siblings ...)
  2026-07-10  5:59 ` [PATCH v2 3/5] mm/rmap: refactor some code around lazyfree folio unmapping Dev Jain
@ 2026-07-10  5:59 ` Dev Jain
  2026-07-10  5:59 ` [PATCH v2 5/5] mm/rmap: add anon folio unmap dispatcher Dev Jain
  4 siblings, 0 replies; 12+ messages in thread
From: Dev Jain @ 2026-07-10  5:59 UTC (permalink / raw)
  To: akpm, david, ljs, muchun.song, osalvador
  Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual

Refactor anonymous swapbacked folio unmap to ttu_anon_swapbacked_folio().
While at it, rename the remaining subpage variables to page; folios have
pages, not subpages.

No functional change intended.

Signed-off-by: Dev Jain <dev.jain@arm.com>
---
 mm/rmap.c | 115 ++++++++++++++++++++++++++++++++----------------------
 1 file changed, 68 insertions(+), 47 deletions(-)

diff --git a/mm/rmap.c b/mm/rmap.c
index 4851c9f2a34e7..68459f23f5c0a 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2112,6 +2112,66 @@ static bool ttu_anon_lazyfree_folio(struct vm_area_struct *vma,
 	return true;
 }
 
+static pte_t swp_pte_prepare(swp_entry_t entry, pte_t old_pte,
+		bool anon_exclusive)
+{
+	pte_t swp_pte = swp_entry_to_pte(entry);
+
+	if (anon_exclusive)
+		swp_pte = pte_swp_mkexclusive(swp_pte);
+
+	if (likely(pte_present(old_pte))) {
+		if (pte_soft_dirty(old_pte))
+			swp_pte = pte_swp_mksoft_dirty(swp_pte);
+		if (pte_uffd_wp(old_pte))
+			swp_pte = pte_swp_mkuffd_wp(swp_pte);
+	} else {
+		/* Device-exclusive entry */
+		if (pte_swp_soft_dirty(old_pte))
+			swp_pte = pte_swp_mksoft_dirty(swp_pte);
+		if (pte_swp_uffd_wp(old_pte))
+			swp_pte = pte_swp_mkuffd_wp(swp_pte);
+	}
+
+	return swp_pte;
+}
+
+static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
+		struct folio *folio, struct page *page, unsigned long address,
+		pte_t *ptep, pte_t pteval)
+{
+	const bool anon_exclusive = folio_test_anon(folio) &&
+				    PageAnonExclusive(page);
+	swp_entry_t entry = page_swap_entry(page);
+	struct mm_struct *mm = vma->vm_mm;
+
+	if (folio_dup_swap(folio, page) < 0)
+		return false;
+
+	/*
+	 * arch_unmap_one() is expected to be a NOP on
+	 * architectures where we could have PFN swap PTEs,
+	 * so we'll not check/care.
+	 */
+	if (arch_unmap_one(mm, vma, address, pteval) < 0) {
+		folio_put_swap(folio, page);
+		return false;
+	}
+
+	/* See folio_try_share_anon_rmap(): clear PTE first. */
+	if (anon_exclusive && folio_try_share_anon_rmap_pte(folio, page)) {
+		folio_put_swap(folio, page);
+		return false;
+	}
+
+	mm_prepare_for_swap_entries(mm);
+	dec_mm_counter(mm, MM_ANONPAGES);
+	inc_mm_counter(mm, MM_SWAPENTS);
+	set_pte_at(mm, address, ptep,
+		   swp_pte_prepare(entry, pteval, anon_exclusive));
+	return true;
+}
+
 /*
  * @arg: enum ttu_flags will be passed to this argument
  */
@@ -2120,9 +2180,9 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 {
 	struct mm_struct *mm = vma->vm_mm;
 	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);
-	bool anon_exclusive, ret = true;
+	bool ret = true;
 	pte_t pteval;
-	struct page *subpage;
+	struct page *page;
 	struct mmu_notifier_range range;
 	enum ttu_flags flags = (enum ttu_flags)(long)arg;
 	unsigned long nr_pages = 1, end_addr;
@@ -2233,9 +2293,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			pfn = softleaf_to_pfn(entry);
 		}
 
-		subpage = folio_page(folio, pfn - folio_pfn(folio));
-		anon_exclusive = folio_test_anon(folio) &&
-				 PageAnonExclusive(subpage);
+		page = folio_page(folio, pfn - folio_pfn(folio));
 
 		if (likely(pte_present(pteval))) {
 			nr_pages = folio_unmap_pte_batch(folio, &pvmw, flags, pteval);
@@ -2277,7 +2335,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		 * folios here for now.
 		 */
 		if (folio_test_hwpoison(folio) && (flags & TTU_HWPOISON)) {
-			pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
+			pteval = swp_entry_to_pte(make_hwpoison_entry(page));
 			dec_mm_counter(mm, mm_counter(folio));
 			set_pte_at(mm, address, pvmw.pte, pteval);
 		} else if (likely(pte_present(pteval)) && pte_unused(pteval) &&
@@ -2294,8 +2352,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			 */
 			dec_mm_counter(mm, mm_counter(folio));
 		} else if (folio_test_anon(folio)) {
-			swp_entry_t entry = page_swap_entry(subpage);
-			pte_t swp_pte;
 			/*
 			 * Store the swap location in the pte.
 			 * See handle_pte_fault() ...
@@ -2316,47 +2372,12 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 				goto finish_unmap;
 			}
 
-			if (folio_dup_swap(folio, subpage) < 0) {
-				set_pte_at(mm, address, pvmw.pte, pteval);
-				goto walk_abort;
-			}
-
-			/*
-			 * arch_unmap_one() is expected to be a NOP on
-			 * architectures where we could have PFN swap PTEs,
-			 * so we'll not check/care.
-			 */
-			if (arch_unmap_one(mm, vma, address, pteval) < 0) {
-				folio_put_swap(folio, subpage);
-				set_pte_at(mm, address, pvmw.pte, pteval);
-				goto walk_abort;
-			}
-
-			/* See folio_try_share_anon_rmap(): clear PTE first. */
-			if (anon_exclusive &&
-			    folio_try_share_anon_rmap_pte(folio, subpage)) {
-				folio_put_swap(folio, subpage);
+			if (!ttu_anon_swapbacked_folio(vma, folio, page, address,
+						pvmw.pte, pteval)) {
 				set_pte_at(mm, address, pvmw.pte, pteval);
 				goto walk_abort;
 			}
-			mm_prepare_for_swap_entries(mm);
-			dec_mm_counter(mm, MM_ANONPAGES);
-			inc_mm_counter(mm, MM_SWAPENTS);
-			swp_pte = swp_entry_to_pte(entry);
-			if (anon_exclusive)
-				swp_pte = pte_swp_mkexclusive(swp_pte);
-			if (likely(pte_present(pteval))) {
-				if (pte_soft_dirty(pteval))
-					swp_pte = pte_swp_mksoft_dirty(swp_pte);
-				if (pte_uffd_wp(pteval))
-					swp_pte = pte_swp_mkuffd_wp(swp_pte);
-			} else {
-				if (pte_swp_soft_dirty(pteval))
-					swp_pte = pte_swp_mksoft_dirty(swp_pte);
-				if (pte_swp_uffd_wp(pteval))
-					swp_pte = pte_swp_mkuffd_wp(swp_pte);
-			}
-			set_pte_at(mm, address, pvmw.pte, swp_pte);
+			goto finish_unmap;
 		} else {
 			/*
 			 * This is a locked file-backed folio,
@@ -2372,7 +2393,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			add_mm_counter(mm, mm_counter_file(folio), -nr_pages);
 		}
 finish_unmap:
-		folio_remove_rmap_ptes(folio, subpage, nr_pages, vma);
+		folio_remove_rmap_ptes(folio, page, nr_pages, vma);
 		if (vma->vm_flags & VM_LOCKED)
 			mlock_drain_local();
 		folio_put_refs(folio, nr_pages);
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* [PATCH v2 5/5] mm/rmap: add anon folio unmap dispatcher
  2026-07-10  5:59 [PATCH v2 0/5] mm/rmap: Refactor try_to_unmap_one Dev Jain
                   ` (3 preceding siblings ...)
  2026-07-10  5:59 ` [PATCH v2 4/5] mm/rmap: refactor anon folio unmap in try_to_unmap_one Dev Jain
@ 2026-07-10  5:59 ` Dev Jain
  2026-07-10 12:27   ` David Hildenbrand (Arm)
  4 siblings, 1 reply; 12+ messages in thread
From: Dev Jain @ 2026-07-10  5:59 UTC (permalink / raw)
  To: akpm, david, ljs, muchun.song, osalvador
  Cc: Dev Jain, riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual

Add ttu_anon_folio() as the common entry point for anonymous folio
unmapping. It dispatches to the lazyfree or swapbacked helper as
appropriate.

Keep restoration of cleared PTEs in the caller, where the PTEs were
removed.

No functional change intended.

Signed-off-by: Dev Jain <dev.jain@arm.com>
---
 mm/rmap.c | 47 +++++++++++++++++++++++++----------------------
 1 file changed, 25 insertions(+), 22 deletions(-)

diff --git a/mm/rmap.c b/mm/rmap.c
index 68459f23f5c0a..55aa3ff8bf216 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2172,6 +2172,28 @@ static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
 	return true;
 }
 
+static bool ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio,
+		struct page *page, unsigned long address, pte_t *ptep,
+		pte_t pteval, unsigned long nr_pages)
+{
+	/*
+	 * Store the swap location in the pte.
+	 * See handle_pte_fault() ...
+	 */
+	if (unlikely(folio_test_swapbacked(folio) !=
+			folio_test_swapcache(folio))) {
+		WARN_ON_ONCE(1);
+		return false;
+	}
+
+	if (!folio_test_swapbacked(folio))
+		return ttu_anon_lazyfree_folio(vma, folio, nr_pages);
+
+	/* nr_pages > 1 not supported yet */
+	return ttu_anon_swapbacked_folio(vma, folio, page, address, ptep,
+					 pteval);
+}
+
 /*
  * @arg: enum ttu_flags will be passed to this argument
  */
@@ -2352,31 +2374,12 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 			 */
 			dec_mm_counter(mm, mm_counter(folio));
 		} else if (folio_test_anon(folio)) {
-			/*
-			 * Store the swap location in the pte.
-			 * See handle_pte_fault() ...
-			 */
-			if (unlikely(folio_test_swapbacked(folio) !=
-					folio_test_swapcache(folio))) {
-				WARN_ON_ONCE(1);
+			if (!ttu_anon_folio(vma, folio, page, address,
+					    pvmw.pte, pteval, nr_pages)) {
+				set_ptes(mm, address, pvmw.pte, pteval, nr_pages);
 				goto walk_abort;
 			}
 
-			/* MADV_FREE page check */
-			if (!folio_test_swapbacked(folio)) {
-				if (!ttu_anon_lazyfree_folio(vma, folio,
-							     nr_pages)) {
-					set_ptes(mm, address, pvmw.pte, pteval, nr_pages);
-					goto walk_abort;
-				}
-				goto finish_unmap;
-			}
-
-			if (!ttu_anon_swapbacked_folio(vma, folio, page, address,
-						pvmw.pte, pteval)) {
-				set_pte_at(mm, address, pvmw.pte, pteval);
-				goto walk_abort;
-			}
 			goto finish_unmap;
 		} else {
 			/*
-- 
2.43.0



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/5] mm/rmap: convert page -> folio for hwpoison checks
  2026-07-10  5:59 ` [PATCH v2 1/5] mm/rmap: convert page -> folio for hwpoison checks Dev Jain
@ 2026-07-10 12:22   ` David Hildenbrand (Arm)
  2026-07-10 13:14     ` Dev Jain
  0 siblings, 1 reply; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-07-10 12:22 UTC (permalink / raw)
  To: Dev Jain, akpm, ljs, muchun.song, osalvador
  Cc: riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual

On 7/10/26 07:59, Dev Jain wrote:
> try_to_unmap() receives hugetlb folios only from the hwpoison path.
> hugetlb_update_hwpoison() sets the hugetlb folio's head-page
> hwpoison bit, and page_vma_mapped_walk() reports the hugetlb mapping at
> the head PFN, so the previous PageHWPoison(subpage) check happened to
> work for hugetlb.
> 
> For non-hugetlb folios, unmap_poisoned_folio() currently rejects large
> folios before calling try_to_unmap(). Hence it is always the case that
> if try_to_unmap_one() handles an hwpoisoned folio, then the head page is
> marked with the poison bit.
> 
> Therefore, convert the poisoned subpage checks to folio_test_hwpoison().
> 
> No functional change intended, except that, while at it,
> convert VM_BUG_* to VM_WARN_*.
> 
> Acked-by: David Hildenbrand (Arm) <david@kernel.org>
> Signed-off-by: Dev Jain <dev.jain@arm.com>
> ---
>  mm/rmap.c | 13 +++++++++----
>  1 file changed, 9 insertions(+), 4 deletions(-)
> 
> diff --git a/mm/rmap.c b/mm/rmap.c
> index 9939400e77c79..dbb077f8443e3 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -2122,10 +2122,11 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>  			bool anon = folio_test_anon(folio);
>  
>  			/*
> -			 * The try_to_unmap() is only passed a hugetlb page
> -			 * in the case where the hugetlb page is poisoned.
> +			 * The try_to_unmap() is only passed a hugetlb folio
> +			 * in the case where the hugetlb folio contains a
> +			 * poisoned page.
>  			 */
> -			VM_BUG_ON_PAGE(!PageHWPoison(subpage), subpage);
> +			VM_WARN_ON_FOLIO(!folio_test_hwpoison(folio), folio);
>  			/*
>  			 * huge_pmd_unshare may unmap an entire PMD page.
>  			 * There is no way of knowing exactly which PMDs may
> @@ -2204,7 +2205,11 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>  		/* Update high watermark before we lower rss */
>  		update_hiwater_rss(mm);
>  
> -		if (PageHWPoison(subpage) && (flags & TTU_HWPOISON)) {
> +		/*
> +		 * With TTU_HWPOISON, we only expect small folios or hugetlb
> +		 * folios here for now.
> +		 */

Is this comment helpful given that you change that in patch #2, where you forget
to update the comment? :)

-- 
Cheers,

David


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 5/5] mm/rmap: add anon folio unmap dispatcher
  2026-07-10  5:59 ` [PATCH v2 5/5] mm/rmap: add anon folio unmap dispatcher Dev Jain
@ 2026-07-10 12:27   ` David Hildenbrand (Arm)
  2026-07-10 13:13     ` Dev Jain
  0 siblings, 1 reply; 12+ messages in thread
From: David Hildenbrand (Arm) @ 2026-07-10 12:27 UTC (permalink / raw)
  To: Dev Jain, akpm, ljs, muchun.song, osalvador
  Cc: riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual

On 7/10/26 07:59, Dev Jain wrote:
> Add ttu_anon_folio() as the common entry point for anonymous folio
> unmapping. It dispatches to the lazyfree or swapbacked helper as
> appropriate.
> 
> Keep restoration of cleared PTEs in the caller, where the PTEs were
> removed.
> 
> No functional change intended.
> 
> Signed-off-by: Dev Jain <dev.jain@arm.com>
> ---
>  mm/rmap.c | 47 +++++++++++++++++++++++++----------------------
>  1 file changed, 25 insertions(+), 22 deletions(-)
> 
> diff --git a/mm/rmap.c b/mm/rmap.c
> index 68459f23f5c0a..55aa3ff8bf216 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -2172,6 +2172,28 @@ static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
>  	return true;
>  }
>  
> +static bool ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio,
> +		struct page *page, unsigned long address, pte_t *ptep,
> +		pte_t pteval, unsigned long nr_pages)
> +{
> +	/*
> +	 * Store the swap location in the pte.
> +	 * See handle_pte_fault() ...
> +	 */
> +	if (unlikely(folio_test_swapbacked(folio) !=
> +			folio_test_swapcache(folio))) {
> +		WARN_ON_ONCE(1);

Can't you directly do

if (WARN_ON_ONCE(...))
	return false;

That even implies the unlikely() :)

> +		return false;
> +	}
> +
> +	if (!folio_test_swapbacked(folio))
> +		return ttu_anon_lazyfree_folio(vma, folio, nr_pages);
> +
> +	/* nr_pages > 1 not supported yet */
> +	return ttu_anon_swapbacked_folio(vma, folio, page, address, ptep,

LGTM

Acked-by: David Hildenbrand (Arm) <david@kernel.org>

-- 
Cheers,

David


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 5/5] mm/rmap: add anon folio unmap dispatcher
  2026-07-10 12:27   ` David Hildenbrand (Arm)
@ 2026-07-10 13:13     ` Dev Jain
  2026-07-11  0:35       ` Andrew Morton
  0 siblings, 1 reply; 12+ messages in thread
From: Dev Jain @ 2026-07-10 13:13 UTC (permalink / raw)
  To: David Hildenbrand (Arm), akpm, ljs, muchun.song, osalvador
  Cc: riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual



On 10/07/26 5:57 pm, David Hildenbrand (Arm) wrote:
> On 7/10/26 07:59, Dev Jain wrote:
>> Add ttu_anon_folio() as the common entry point for anonymous folio
>> unmapping. It dispatches to the lazyfree or swapbacked helper as
>> appropriate.
>>
>> Keep restoration of cleared PTEs in the caller, where the PTEs were
>> removed.
>>
>> No functional change intended.
>>
>> Signed-off-by: Dev Jain <dev.jain@arm.com>
>> ---
>>  mm/rmap.c | 47 +++++++++++++++++++++++++----------------------
>>  1 file changed, 25 insertions(+), 22 deletions(-)
>>
>> diff --git a/mm/rmap.c b/mm/rmap.c
>> index 68459f23f5c0a..55aa3ff8bf216 100644
>> --- a/mm/rmap.c
>> +++ b/mm/rmap.c
>> @@ -2172,6 +2172,28 @@ static bool ttu_anon_swapbacked_folio(struct vm_area_struct *vma,
>>  	return true;
>>  }
>>  
>> +static bool ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio,
>> +		struct page *page, unsigned long address, pte_t *ptep,
>> +		pte_t pteval, unsigned long nr_pages)
>> +{
>> +	/*
>> +	 * Store the swap location in the pte.
>> +	 * See handle_pte_fault() ...
>> +	 */
>> +	if (unlikely(folio_test_swapbacked(folio) !=
>> +			folio_test_swapcache(folio))) {
>> +		WARN_ON_ONCE(1);
> 
> Can't you directly do
> 
> if (WARN_ON_ONCE(...))
> 	return false;

Right. I didn't do it perhaps because I was playing with if (VM_WARN_ON_ONCE) somewhere
and that didn't build. But this is without the VM_ , this will work.

Andrew, could you you squash this diff:

diff --git a/mm/rmap.c b/mm/rmap.c
index 55aa3ff8bf216..41a049e7dd231 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2180,11 +2180,9 @@ static bool ttu_anon_folio(struct vm_area_struct *vma, struct folio *folio,
         * Store the swap location in the pte.
         * See handle_pte_fault() ...
         */
-       if (unlikely(folio_test_swapbacked(folio) !=
-                       folio_test_swapcache(folio))) {
-               WARN_ON_ONCE(1);
+       if (WARN_ON_ONCE(folio_test_swapbacked(folio) !=
+                        folio_test_swapcache(folio)))
                return false;
-       }

        if (!folio_test_swapbacked(folio))
                return ttu_anon_lazyfree_folio(vma, folio, nr_pages);


> 
> That even implies the unlikely() :)
> 
>> +		return false;
>> +	}
>> +
>> +	if (!folio_test_swapbacked(folio))
>> +		return ttu_anon_lazyfree_folio(vma, folio, nr_pages);
>> +
>> +	/* nr_pages > 1 not supported yet */
>> +	return ttu_anon_swapbacked_folio(vma, folio, page, address, ptep,
> 
> LGTM
> 
> Acked-by: David Hildenbrand (Arm) <david@kernel.org>
> 



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 1/5] mm/rmap: convert page -> folio for hwpoison checks
  2026-07-10 12:22   ` David Hildenbrand (Arm)
@ 2026-07-10 13:14     ` Dev Jain
  0 siblings, 0 replies; 12+ messages in thread
From: Dev Jain @ 2026-07-10 13:14 UTC (permalink / raw)
  To: David Hildenbrand (Arm), akpm, ljs, muchun.song, osalvador
  Cc: riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual



On 10/07/26 5:52 pm, David Hildenbrand (Arm) wrote:
> On 7/10/26 07:59, Dev Jain wrote:
>> try_to_unmap() receives hugetlb folios only from the hwpoison path.
>> hugetlb_update_hwpoison() sets the hugetlb folio's head-page
>> hwpoison bit, and page_vma_mapped_walk() reports the hugetlb mapping at
>> the head PFN, so the previous PageHWPoison(subpage) check happened to
>> work for hugetlb.
>>
>> For non-hugetlb folios, unmap_poisoned_folio() currently rejects large
>> folios before calling try_to_unmap(). Hence it is always the case that
>> if try_to_unmap_one() handles an hwpoisoned folio, then the head page is
>> marked with the poison bit.
>>
>> Therefore, convert the poisoned subpage checks to folio_test_hwpoison().
>>
>> No functional change intended, except that, while at it,
>> convert VM_BUG_* to VM_WARN_*.
>>
>> Acked-by: David Hildenbrand (Arm) <david@kernel.org>
>> Signed-off-by: Dev Jain <dev.jain@arm.com>
>> ---
>>  mm/rmap.c | 13 +++++++++----
>>  1 file changed, 9 insertions(+), 4 deletions(-)
>>
>> diff --git a/mm/rmap.c b/mm/rmap.c
>> index 9939400e77c79..dbb077f8443e3 100644
>> --- a/mm/rmap.c
>> +++ b/mm/rmap.c
>> @@ -2122,10 +2122,11 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>>  			bool anon = folio_test_anon(folio);
>>  
>>  			/*
>> -			 * The try_to_unmap() is only passed a hugetlb page
>> -			 * in the case where the hugetlb page is poisoned.
>> +			 * The try_to_unmap() is only passed a hugetlb folio
>> +			 * in the case where the hugetlb folio contains a
>> +			 * poisoned page.
>>  			 */
>> -			VM_BUG_ON_PAGE(!PageHWPoison(subpage), subpage);
>> +			VM_WARN_ON_FOLIO(!folio_test_hwpoison(folio), folio);
>>  			/*
>>  			 * huge_pmd_unshare may unmap an entire PMD page.
>>  			 * There is no way of knowing exactly which PMDs may
>> @@ -2204,7 +2205,11 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>>  		/* Update high watermark before we lower rss */
>>  		update_hiwater_rss(mm);
>>  
>> -		if (PageHWPoison(subpage) && (flags & TTU_HWPOISON)) {
>> +		/*
>> +		 * With TTU_HWPOISON, we only expect small folios or hugetlb
>> +		 * folios here for now.
>> +		 */
> 
> Is this comment helpful given that you change that in patch #2, where you forget
> to update the comment? :)

Nice spot, let me update this comment in patch 2.

> 



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 2/5] mm/rmap: Add try_to_unmap_hugetlb_one
  2026-07-10  5:59 ` [PATCH v2 2/5] mm/rmap: Add try_to_unmap_hugetlb_one Dev Jain
@ 2026-07-10 13:16   ` Dev Jain
  0 siblings, 0 replies; 12+ messages in thread
From: Dev Jain @ 2026-07-10 13:16 UTC (permalink / raw)
  To: akpm, david, ljs, muchun.song, osalvador
  Cc: riel, liam, vbabka, harry, jannh, lance.yang, linux-mm,
	linux-kernel, ryan.roberts, anshuman.khandual



On 10/07/26 11:29 am, Dev Jain wrote:
> Simplify try_to_unmap_one() by separating the hugetlb parts into
> try_to_unmap_hugetlb_one().
> 
> To understand the correctness of the refactoring, the following points
> are noted:
> 
> 1. try_to_unmap() is called for hugetlb folios only when they are
>    hwpoisoned.
> 
> 2. A hugetlb VMA cannot be mlocked.
> 
> 3. page_vma_mapped_walk() returns at most one hugetlb mapping in a VMA,
>    and that mapping points at the head PFN.
> 
> 4. We won't ever process a softleaf entry that encodes a hugetlb folio;
>    hugetlb folios are never swapped out, migration entries will be
>    skipped (PVMW_MIGRATION not passed), and device-exclusive does not
>    work for hugetlb.
> 
> 5. The hwpoison entry is constructed from the poisoned folio, just as in
>    the pre-refactor code. Any previous uffd-wp state is deliberately not
>    preserved for the hwpoison entry.
> 
> 6. TTU_HWPOISON is always present; for it to not be present, either the
>    folio has to be in swapcache, or mapping_can_writeback() is true (see
>    unmap_poisoned_folio), none of which is true for hugetlb folios.
> 
> 7. Hugetlb uses separate counters from normal rss counters, therefore
>    update_highwater_rss() need not be called.
> 
> While at it:
> 
>  - Change VM_BUG_* to VM_WARN_*.
> 
>  - Do not declare variables which are only used once.
> 
>  - Constify some variables.
> 
>  - Add some more VM_WARN_* to assert some invariants.
> 
> Except the above 4 points, no functional change intended.
> 
> Suggested-by: David Hildenbrand (Arm) <david@kernel.org>
> Acked-by: David Hildenbrand (Arm) <david@kernel.org>
> Signed-off-by: Dev Jain <dev.jain@arm.com>
> ---

Andrew, could you squash the following diff:

diff --git a/mm/rmap.c b/mm/rmap.c
index cf02b315a2ead..b61051b879c4c 100644
--- a/mm/rmap.c
+++ b/mm/rmap.c
@@ -2229,8 +2229,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
                update_hiwater_rss(mm);

                /*
-                * With TTU_HWPOISON, we only expect small folios or hugetlb
-                * folios here for now.
+                * With TTU_HWPOISON, we only expect small folios here.
                 */
                if (folio_test_hwpoison(folio) && (flags & TTU_HWPOISON)) {
                        pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));


>  include/linux/hugetlb.h |   1 +
>  mm/rmap.c               | 178 ++++++++++++++++++++++------------------
>  2 files changed, 97 insertions(+), 82 deletions(-)
> 
> diff --git a/include/linux/hugetlb.h b/include/linux/hugetlb.h
> index 4115076e4922a..bf7e163e3779d 100644
> --- a/include/linux/hugetlb.h
> +++ b/include/linux/hugetlb.h
> @@ -1271,6 +1271,7 @@ static inline void hugetlb_count_sub(long l, struct mm_struct *mm)
>  }
>  
>  pte_t huge_ptep_get(struct mm_struct *mm, unsigned long addr, pte_t *ptep);
> +unsigned long huge_pte_dirty(pte_t pte);
>  
>  static inline pte_t huge_ptep_clear_flush(struct vm_area_struct *vma,
>  					  unsigned long addr, pte_t *ptep)
> diff --git a/mm/rmap.c b/mm/rmap.c
> index dbb077f8443e3..cf02b315a2ead 100644
> --- a/mm/rmap.c
> +++ b/mm/rmap.c
> @@ -1978,6 +1978,96 @@ static inline unsigned int folio_unmap_pte_batch(struct folio *folio,
>  				     FPB_RESPECT_WRITE | FPB_RESPECT_SOFT_DIRTY);
>  }
>  
> +static bool try_to_unmap_hugetlb_one(struct folio *folio,
> +		struct vm_area_struct *vma, unsigned long address, void *arg)
> +{
> +	DEFINE_FOLIO_VMA_WALK(pvmw, folio, vma, address, 0);
> +	const unsigned long hsz = huge_page_size(hstate_vma(vma));
> +	const enum ttu_flags flags = (enum ttu_flags)(long)arg;
> +	struct mm_struct *mm = vma->vm_mm;
> +	struct mmu_notifier_range range;
> +	bool ret = true;
> +	pte_t pteval;
> +
> +	/*
> +	 * The try_to_unmap() is only passed a hugetlb folio in the case
> +	 * where the hugetlb folio is poisoned.
> +	 */
> +	VM_WARN_ON_FOLIO(!folio_test_hwpoison(folio), folio);
> +	VM_WARN_ON_ONCE(!(flags & TTU_HWPOISON));
> +
> +	range.end = vma_address_end(&pvmw);
> +	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
> +				address, range.end);
> +	adjust_range_if_pmd_sharing_possible(vma, &range.start, &range.end);
> +	mmu_notifier_invalidate_range_start(&range);
> +
> +	/* There is only a single mapping in a VMA. */
> +	if (!page_vma_mapped_walk(&pvmw))
> +		goto range_end;
> +
> +	VM_WARN_ON_ONCE(address != pvmw.address);
> +
> +	pteval = huge_ptep_get(mm, address, pvmw.pte);
> +	VM_WARN_ON_ONCE(!pte_present(pteval));
> +	VM_WARN_ON_ONCE(pte_pfn(pteval) != folio_pfn(folio));
> +
> +	/*
> +	 * huge_pmd_unshare may unmap an entire PMD page. There is no way of
> +	 * knowing exactly which PMDs may be cached for this mm, so we must
> +	 * flush them all. start/end were already adjusted above to cover this
> +	 * range.
> +	 */
> +	flush_cache_range(vma, range.start, range.end);
> +
> +	/*
> +	 * To call huge_pmd_unshare, i_mmap_rwsem must be held in write mode.
> +	 * Caller needs to explicitly do this outside rmap routines.
> +	 *
> +	 * We also must hold hugetlb vma_lock in write mode. Lock order dictates
> +	 * acquiring vma_lock BEFORE i_mmap_rwsem. We can only try lock here and
> +	 * fail if unsuccessful.
> +	 */
> +	if (!folio_test_anon(folio)) {
> +		struct mmu_gather tlb;
> +
> +		VM_WARN_ON(!(flags & TTU_RMAP_LOCKED));
> +		if (!hugetlb_vma_trylock_write(vma)) {
> +			ret = false;
> +			goto walk_done;
> +		}
> +
> +		tlb_gather_mmu_vma(&tlb, vma);
> +		if (huge_pmd_unshare(&tlb, vma, address, pvmw.pte)) {
> +			hugetlb_vma_unlock_write(vma);
> +			huge_pmd_unshare_flush(&tlb, vma);
> +			tlb_finish_mmu(&tlb);
> +			/*
> +			 * The PMD table was unmapped, consequently unmapping
> +			 * the folio.
> +			 */
> +			goto walk_done;
> +		}
> +		hugetlb_vma_unlock_write(vma);
> +		tlb_finish_mmu(&tlb);
> +	}
> +	pteval = huge_ptep_clear_flush(vma, address, pvmw.pte);
> +	if (huge_pte_dirty(pteval))
> +		folio_mark_dirty(folio);
> +
> +	pteval = swp_entry_to_pte(make_hwpoison_entry(folio_page(folio, 0)));
> +	hugetlb_count_sub(folio_nr_pages(folio), mm);
> +	set_huge_pte_at(mm, address, pvmw.pte, pteval, hsz);
> +	hugetlb_remove_rmap(folio);
> +	folio_put_refs(folio, 1);
> +
> +walk_done:
> +	page_vma_mapped_walk_done(&pvmw);
> +range_end:
> +	mmu_notifier_invalidate_range_end(&range);
> +	return ret;
> +}
> +
>  /*
>   * @arg: enum ttu_flags will be passed to this argument
>   */
> @@ -1993,7 +2083,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>  	enum ttu_flags flags = (enum ttu_flags)(long)arg;
>  	unsigned long nr_pages = 1, end_addr;
>  	unsigned long pfn;
> -	unsigned long hsz = 0;
>  	int ptes = 0;
>  
>  	/*
> @@ -2007,8 +2096,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>  
>  	/*
>  	 * 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
> -	 * invalidation in the case of pmd sharing.
>  	 *
>  	 * Note that the folio can not be freed in this function as call of
>  	 * try_to_unmap() must hold a reference on the folio.
> @@ -2016,17 +2103,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>  	range.end = vma_address_end(&pvmw);
>  	mmu_notifier_range_init(&range, MMU_NOTIFY_CLEAR, 0, vma->vm_mm,
>  				address, range.end);
> -	if (folio_test_hugetlb(folio)) {
> -		/*
> -		 * If sharing is possible, start and end will be adjusted
> -		 * accordingly.
> -		 */
> -		adjust_range_if_pmd_sharing_possible(vma, &range.start,
> -						     &range.end);
> -
> -		/* We need the huge page size for set_huge_pte_at() */
> -		hsz = huge_page_size(hstate_vma(vma));
> -	}
>  	mmu_notifier_invalidate_range_start(&range);
>  
>  	while (page_vma_mapped_walk(&pvmw)) {
> @@ -2111,66 +2187,13 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>  			const softleaf_t entry = softleaf_from_pte(pteval);
>  
>  			pfn = softleaf_to_pfn(entry);
> -			VM_WARN_ON_FOLIO(folio_test_hugetlb(folio), folio);
>  		}
>  
>  		subpage = folio_page(folio, pfn - folio_pfn(folio));
>  		anon_exclusive = folio_test_anon(folio) &&
>  				 PageAnonExclusive(subpage);
>  
> -		if (folio_test_hugetlb(folio)) {
> -			bool anon = folio_test_anon(folio);
> -
> -			/*
> -			 * The try_to_unmap() is only passed a hugetlb folio
> -			 * in the case where the hugetlb folio contains a
> -			 * poisoned page.
> -			 */
> -			VM_WARN_ON_FOLIO(!folio_test_hwpoison(folio), folio);
> -			/*
> -			 * huge_pmd_unshare may unmap an entire PMD page.
> -			 * There is no way of knowing exactly which PMDs may
> -			 * be cached for this mm, so we must flush them all.
> -			 * start/end were already adjusted above to cover this
> -			 * range.
> -			 */
> -			flush_cache_range(vma, range.start, range.end);
> -
> -			/*
> -			 * To call huge_pmd_unshare, i_mmap_rwsem must be
> -			 * held in write mode.  Caller needs to explicitly
> -			 * do this outside rmap routines.
> -			 *
> -			 * We also must hold hugetlb vma_lock in write mode.
> -			 * Lock order dictates acquiring vma_lock BEFORE
> -			 * i_mmap_rwsem.  We can only try lock here and fail
> -			 * if unsuccessful.
> -			 */
> -			if (!anon) {
> -				struct mmu_gather tlb;
> -
> -				VM_BUG_ON(!(flags & TTU_RMAP_LOCKED));
> -				if (!hugetlb_vma_trylock_write(vma))
> -					goto walk_abort;
> -
> -				tlb_gather_mmu_vma(&tlb, vma);
> -				if (huge_pmd_unshare(&tlb, vma, address, pvmw.pte)) {
> -					hugetlb_vma_unlock_write(vma);
> -					huge_pmd_unshare_flush(&tlb, vma);
> -					tlb_finish_mmu(&tlb);
> -					/*
> -					 * The PMD table was unmapped,
> -					 * consequently unmapping the folio.
> -					 */
> -					goto walk_done;
> -				}
> -				hugetlb_vma_unlock_write(vma);
> -				tlb_finish_mmu(&tlb);
> -			}
> -			pteval = huge_ptep_clear_flush(vma, address, pvmw.pte);
> -			if (pte_dirty(pteval))
> -				folio_mark_dirty(folio);
> -		} else if (likely(pte_present(pteval))) {
> +		if (likely(pte_present(pteval))) {
>  			nr_pages = folio_unmap_pte_batch(folio, &pvmw, flags, pteval);
>  			end_addr = address + nr_pages * PAGE_SIZE;
>  			flush_cache_range(vma, address, end_addr);
> @@ -2211,14 +2234,8 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>  		 */
>  		if (folio_test_hwpoison(folio) && (flags & TTU_HWPOISON)) {
>  			pteval = swp_entry_to_pte(make_hwpoison_entry(subpage));
> -			if (folio_test_hugetlb(folio)) {
> -				hugetlb_count_sub(folio_nr_pages(folio), mm);
> -				set_huge_pte_at(mm, address, pvmw.pte, pteval,
> -						hsz);
> -			} else {
> -				dec_mm_counter(mm, mm_counter(folio));
> -				set_pte_at(mm, address, pvmw.pte, pteval);
> -			}
> +			dec_mm_counter(mm, mm_counter(folio));
> +			set_pte_at(mm, address, pvmw.pte, pteval);
>  		} else if (likely(pte_present(pteval)) && pte_unused(pteval) &&
>  			   !userfaultfd_armed(vma)) {
>  			/*
> @@ -2346,11 +2363,7 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
>  			add_mm_counter(mm, mm_counter_file(folio), -nr_pages);
>  		}
>  discard:
> -		if (unlikely(folio_test_hugetlb(folio))) {
> -			hugetlb_remove_rmap(folio);
> -		} else {
> -			folio_remove_rmap_ptes(folio, subpage, nr_pages, vma);
> -		}
> +		folio_remove_rmap_ptes(folio, subpage, nr_pages, vma);
>  		if (vma->vm_flags & VM_LOCKED)
>  			mlock_drain_local();
>  		folio_put_refs(folio, nr_pages);
> @@ -2398,7 +2411,8 @@ static int folio_not_mapped(struct folio *folio)
>  void try_to_unmap(struct folio *folio, enum ttu_flags flags)
>  {
>  	struct rmap_walk_control rwc = {
> -		.rmap_one = try_to_unmap_one,
> +		.rmap_one = folio_test_hugetlb(folio) ?
> +				try_to_unmap_hugetlb_one : try_to_unmap_one,
>  		.arg = (void *)flags,
>  		.done = folio_not_mapped,
>  		.anon_lock = folio_lock_anon_vma_read,



^ permalink raw reply related	[flat|nested] 12+ messages in thread

* Re: [PATCH v2 5/5] mm/rmap: add anon folio unmap dispatcher
  2026-07-10 13:13     ` Dev Jain
@ 2026-07-11  0:35       ` Andrew Morton
  0 siblings, 0 replies; 12+ messages in thread
From: Andrew Morton @ 2026-07-11  0:35 UTC (permalink / raw)
  To: Dev Jain
  Cc: David Hildenbrand (Arm), ljs, muchun.song, osalvador, riel, liam,
	vbabka, harry, jannh, lance.yang, linux-mm, linux-kernel,
	ryan.roberts, anshuman.khandual

On Fri, 10 Jul 2026 18:43:14 +0530 Dev Jain <dev.jain@arm.com> wrote:

> Andrew, could you you squash this diff:

I think I'll await v3, please.  I'm getting a big reject in rmap.c and
methinks we're all a bit firehosed at present.

Are we all agreed that we'll be "batching anonymous large folio
unmapping to optimize it"?  If not, is this series pointful?



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2026-07-11  0:35 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-10  5:59 [PATCH v2 0/5] mm/rmap: Refactor try_to_unmap_one Dev Jain
2026-07-10  5:59 ` [PATCH v2 1/5] mm/rmap: convert page -> folio for hwpoison checks Dev Jain
2026-07-10 12:22   ` David Hildenbrand (Arm)
2026-07-10 13:14     ` Dev Jain
2026-07-10  5:59 ` [PATCH v2 2/5] mm/rmap: Add try_to_unmap_hugetlb_one Dev Jain
2026-07-10 13:16   ` Dev Jain
2026-07-10  5:59 ` [PATCH v2 3/5] mm/rmap: refactor some code around lazyfree folio unmapping Dev Jain
2026-07-10  5:59 ` [PATCH v2 4/5] mm/rmap: refactor anon folio unmap in try_to_unmap_one Dev Jain
2026-07-10  5:59 ` [PATCH v2 5/5] mm/rmap: add anon folio unmap dispatcher Dev Jain
2026-07-10 12:27   ` David Hildenbrand (Arm)
2026-07-10 13:13     ` Dev Jain
2026-07-11  0:35       ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox