Skip to contentSkip to conversation

mm/huge_memory: avoid transient none PMDs during lazyfree reclaim

PATCH In review Kyle Zeng 4 participants 9 messages Oct 9 – Oct 10

Subsystems (4)
lore
Share & tools
lore

Kyle Zeng

author
#0 lore raw

PATCH 1/1 mm/huge_memory: avoid transient none PMDs during lazyfree reclaim

__discard_anon_folio_pmd_locked() clears and flushes a huge PMD before checking whether the folio can be discarded. If the folio was redirtied or has unexpected references, it restores the original PMD. Reclaim holds the PMD lock and the anon_vma read lock, but not the owning mmap_lock.

Both zap_pmd_range() and mremap's get_old_pmd() can skip a none PMD without taking its lock. A concurrent munmap() or whole-VMA MREMAP_DONTUNMAP can therefore miss the PMD and later unlink the source VMA from its anon_vma, leaving a restored mapping behind. The anon_vma write lock taken by unlink_anon_vmas() waits for reclaim to finish, but does not repeat the skipped page-table walk. Once the remaining VMA links are removed, the folio's positive mapcount no longer guarantees a live anon_vma. Racing lazyfree reclaim against MREMAP_DONTUNMAP as an unprivileged user reproduces a KASAN use-after-free in folio_lock_anon_vma_read().

Use pmdp_invalidate() to keep a recognizable, non-none huge PMD while the discard can still fail. Concurrent unmap and move operations then have to synchronize on the PMD lock instead of skipping the mapping. Clear the invalidated PMD only after the dirty and reference checks have succeeded, before removing the rmap and withdrawing the deposited page table. The full invalidation retains the TLB flush required for the dirty and GUP-fast checks.

Fixes 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()")
Assisted-by Codex:gpt-6-astra
---
 mm/huge_memory.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
mm/huge_memory.c +9 -2
index 1e5d68acf62a..20262195385a 100644
@@ -3569,11 +3569,17 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
 		return false;
 	}
 
-	orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
+	/*
+	 * The discard may fail, so keep the PMD non-none until we're
+	 * committed to discarding it. Otherwise, concurrent munmap() or
+	 * mremap() can skip the PMD without taking the PTL and later unlink
+	 * the VMA from its anon_vma despite a restored mapping.
+	 */
+	orig_pmd = pmdp_invalidate(vma, addr, pmdp);
 
 	/*
 	 * Syncing against concurrent GUP-fast:
-	 * - clear PMD; barrier; read refcount
+	 * - invalidate PMD; barrier; read refcount
 	 * - inc refcount; barrier; read PMD
 	 */
 	smp_mb();
@@ -3607,6 +3613,7 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
 		return false;
 	}
 
+	pmdp_huge_get_and_clear(mm, addr, pmdp);
 	folio_remove_rmap_pmd(folio, pmd_page(orig_pmd), vma);
 	zap_deposited_table(mm, pmdp);
 	add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 37 lines · Kyle Zeng

__discard_anon_folio_pmd_locked() clears and flushes a huge PMD before checking whether the folio can be discarded. If the folio was redirtied or has unexpected references, it restores the original PMD. Reclaim holds the PMD lock and the anon_vma read lock, but not the owning mmap_lock.

Both zap_pmd_range() and mremap's get_old_pmd() can skip a none PMD without taking its lock. A concurrent munmap() or whole-VMA MREMAP_DONTUNMAP can therefore miss the PMD and later unlink the source VMA from its anon_vma, leaving a restored mapping behind. The anon_vma write lock taken by unlink_anon_vmas() waits for reclaim to finish, but does not repeat the skipped page-table walk. Once the remaining VMA links are removed, the folio's positive mapcount no longer guarantees a live anon_vma. Racing lazyfree reclaim against MREMAP_DONTUNMAP as an unprivileged user reproduces a KASAN use-after-free in folio_lock_anon_vma_read().

Use pmdp_invalidate() to keep a recognizable, non-none huge PMD while the discard can still fail. Concurrent unmap and move operations then have to synchronize on the PMD lock instead of skipping the mapping. Clear the invalidated PMD only after the dirty and reference checks have succeeded, before removing the rmap and withdrawing the deposited page table. The full invalidation retains the TLB flush required for the dirty and GUP-fast checks.

Fixes 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()")
Assisted-by Codex:gpt-6-astra
---
 mm/huge_memory.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 1e5d68acf62a..20262195385a 100644

mm/huge_memory.c static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma, · line 3577

Quoted 10 lines
mm/huge_memory.c +6 -1
@@ -3569,11 +3569,17 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
 		return false;
 	}
 
-	orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
+	/*
+	 * The discard may fail, so keep the PMD non-none until we're
+	 * committed to discarding it. Otherwise, concurrent munmap() or
+	 * mremap() can skip the PMD without taking the PTL and later unlink
+	 * the VMA from its anon_vma despite a restored mapping.
+	 */

You can drop the entire comment. This is what we should do in most cases where the mapping might be restored/changed, for example, in set_pmd_migration_entry().

Quoted 10 lines
mm/huge_memory.c +2 -1
+	orig_pmd = pmdp_invalidate(vma, addr, pmdp);
 
 	/*
 	 * Syncing against concurrent GUP-fast:
-	 * - clear PMD; barrier; read refcount
+	 * - invalidate PMD; barrier; read refcount
 	 * - inc refcount; barrier; read PMD
 	 */
 	smp_mb();

mm/huge_memory.c static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma, · line 3619

Quoted 7 lines
mm/huge_memory.c +1 -0
@@ -3607,6 +3613,7 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
 		return false;
 	}
 
+	pmdp_huge_get_and_clear(mm, addr, pmdp);
 	folio_remove_rmap_pmd(folio, pmd_page(orig_pmd), vma);
 	zap_deposited_table(mm, pmdp);
 	add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);

Thanks!

Signature · 3 lines
Cheers,

David
reviewer
#2 lore raw
In reply to Kyle Zeng
Quoted 38 lines · Kyle Zeng

__discard_anon_folio_pmd_locked() clears and flushes a huge PMD before checking whether the folio can be discarded. If the folio was redirtied or has unexpected references, it restores the original PMD. Reclaim holds the PMD lock and the anon_vma read lock, but not the owning mmap_lock.

Both zap_pmd_range() and mremap's get_old_pmd() can skip a none PMD without taking its lock. A concurrent munmap() or whole-VMA MREMAP_DONTUNMAP can therefore miss the PMD and later unlink the source VMA from its anon_vma, leaving a restored mapping behind. The anon_vma write lock taken by unlink_anon_vmas() waits for reclaim to finish, but does not repeat the skipped page-table walk. Once the remaining VMA links are removed, the folio's positive mapcount no longer guarantees a live anon_vma. Racing lazyfree reclaim against MREMAP_DONTUNMAP as an unprivileged user reproduces a KASAN use-after-free in folio_lock_anon_vma_read().

Use pmdp_invalidate() to keep a recognizable, non-none huge PMD while the discard can still fail. Concurrent unmap and move operations then have to synchronize on the PMD lock instead of skipping the mapping. Clear the invalidated PMD only after the dirty and reference checks have succeeded, before removing the rmap and withdrawing the deposited page table. The full invalidation retains the TLB flush required for the dirty and GUP-fast checks.

Fixes 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()")
Assisted-by Codex:gpt-6-astra
---
 mm/huge_memory.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 1e5d68acf62a..20262195385a 100644
@@ -3569,11 +3569,17 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
 		return false;

mm/huge_memory.c static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma, · line 3578

Quoted 10 lines
mm/huge_memory.c +7 -1
 	}
 
-	orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
+	/*
+	 * The discard may fail, so keep the PMD non-none until we're
+	 * committed to discarding it. Otherwise, concurrent munmap() or
+	 * mremap() can skip the PMD without taking the PTL and later unlink
+	 * the VMA from its anon_vma despite a restored mapping.
+	 */
+	orig_pmd = pmdp_invalidate(vma, addr, pmdp);

There are some gaps we need to close before getting this fix in:

1. GUP-fast cannot follow the invalidated PMD: riscv and LoongArch need pmd_access_permitted() that requires _PAGE_PRESENT; s390's pmdp_invalidate() needs a change.

2. sparc64's thp_pte_count can be imbalanced with this change (based on Lance's offlist feedback).

In addition, powerpc has a page table check issue similr to riscv, where riscv fixed it with commit 9f4a88b8d01a6. This is not related to this issue but discovered along with the investigation.

Quoted 16 lines
mm/huge_memory.c +2 -1
 	/*
 	 * Syncing against concurrent GUP-fast:
-	 * - clear PMD; barrier; read refcount
+	 * - invalidate PMD; barrier; read refcount
 	 * - inc refcount; barrier; read PMD
 	 */
 	smp_mb();
@@ -3607,6 +3613,7 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
 		return false;
 	}
 
+	pmdp_huge_get_and_clear(mm, addr, pmdp);
 	folio_remove_rmap_pmd(folio, pmd_page(orig_pmd), vma);
 	zap_deposited_table(mm, pmdp);
 	add_mm_counter(mm, MM_ANONPAGES, -HPAGE_PMD_NR);
Signature · 2 lines
Best Regards,
Yan, Zi
#3 lore raw
Quoted 52 lines · Zi Yan
Quoted 38 lines · Kyle Zeng

__discard_anon_folio_pmd_locked() clears and flushes a huge PMD before checking whether the folio can be discarded. If the folio was redirtied or has unexpected references, it restores the original PMD. Reclaim holds the PMD lock and the anon_vma read lock, but not the owning mmap_lock.

Both zap_pmd_range() and mremap's get_old_pmd() can skip a none PMD without taking its lock. A concurrent munmap() or whole-VMA MREMAP_DONTUNMAP can therefore miss the PMD and later unlink the source VMA from its anon_vma, leaving a restored mapping behind. The anon_vma write lock taken by unlink_anon_vmas() waits for reclaim to finish, but does not repeat the skipped page-table walk. Once the remaining VMA links are removed, the folio's positive mapcount no longer guarantees a live anon_vma. Racing lazyfree reclaim against MREMAP_DONTUNMAP as an unprivileged user reproduces a KASAN use-after-free in folio_lock_anon_vma_read().

Use pmdp_invalidate() to keep a recognizable, non-none huge PMD while the discard can still fail. Concurrent unmap and move operations then have to synchronize on the PMD lock instead of skipping the mapping. Clear the invalidated PMD only after the dirty and reference checks have succeeded, before removing the rmap and withdrawing the deposited page table. The full invalidation retains the TLB flush required for the dirty and GUP-fast checks.

Fixes 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()")
Assisted-by Codex:gpt-6-astra
---
 mm/huge_memory.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 1e5d68acf62a..20262195385a 100644
@@ -3569,11 +3569,17 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
 		return false;

mm/huge_memory.c static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma, · line 3578

Quoted 10 lines
mm/huge_memory.c +7 -1
 	}
 
-	orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
+	/*
+	 * The discard may fail, so keep the PMD non-none until we're
+	 * committed to discarding it. Otherwise, concurrent munmap() or
+	 * mremap() can skip the PMD without taking the PTL and later unlink
+	 * the VMA from its anon_vma despite a restored mapping.
+	 */
+	orig_pmd = pmdp_invalidate(vma, addr, pmdp);

There are some gaps we need to close before getting this fix in:

Quoted 3 lines

1. GUP-fast cannot follow the invalidated PMD: riscv and LoongArch need pmd_access_permitted() that requires _PAGE_PRESENT; s390's pmdp_invalidate() needs a change.

Ugh. We really need pmdp_invalidate() to have reasonable semantics. This whole PMD locking is a mess :(

Quoted 3 lines

2. sparc64's thp_pte_count can be imbalanced with this change (based on Lance's offlist feedback).

Ack.

Quoted 4 lines

In addition, powerpc has a page table check issue similr to riscv, where riscv fixed it with commit 9f4a88b8d01a6. This is not related to this issue but discovered along with the investigation.

Zi, do you have the capacity to take over this patch?

Signature · 3 lines
Cheers,

David
reviewer
#4 lore raw
Quoted 64 lines · David Hildenbrand (Arm)
Quoted 52 lines · Zi Yan
Quoted 38 lines · Kyle Zeng

__discard_anon_folio_pmd_locked() clears and flushes a huge PMD before checking whether the folio can be discarded. If the folio was redirtied or has unexpected references, it restores the original PMD. Reclaim holds the PMD lock and the anon_vma read lock, but not the owning mmap_lock.

Both zap_pmd_range() and mremap's get_old_pmd() can skip a none PMD without taking its lock. A concurrent munmap() or whole-VMA MREMAP_DONTUNMAP can therefore miss the PMD and later unlink the source VMA from its anon_vma, leaving a restored mapping behind. The anon_vma write lock taken by unlink_anon_vmas() waits for reclaim to finish, but does not repeat the skipped page-table walk. Once the remaining VMA links are removed, the folio's positive mapcount no longer guarantees a live anon_vma. Racing lazyfree reclaim against MREMAP_DONTUNMAP as an unprivileged user reproduces a KASAN use-after-free in folio_lock_anon_vma_read().

Use pmdp_invalidate() to keep a recognizable, non-none huge PMD while the discard can still fail. Concurrent unmap and move operations then have to synchronize on the PMD lock instead of skipping the mapping. Clear the invalidated PMD only after the dirty and reference checks have succeeded, before removing the rmap and withdrawing the deposited page table. The full invalidation retains the TLB flush required for the dirty and GUP-fast checks.

Fixes 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()")
Assisted-by Codex:gpt-6-astra
---
 mm/huge_memory.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
mm/huge_memory.c +0 -0
index 1e5d68acf62a..20262195385a 100644
@@ -3569,11 +3569,17 @@ static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma,
 		return false;

mm/huge_memory.c static bool __discard_anon_folio_pmd_locked(struct vm_area_struct *vma, · line 3578

Quoted 10 lines
mm/huge_memory.c +7 -1
 	}
 
-	orig_pmd = pmdp_huge_clear_flush(vma, addr, pmdp);
+	/*
+	 * The discard may fail, so keep the PMD non-none until we're
+	 * committed to discarding it. Otherwise, concurrent munmap() or
+	 * mremap() can skip the PMD without taking the PTL and later unlink
+	 * the VMA from its anon_vma despite a restored mapping.
+	 */
+	orig_pmd = pmdp_invalidate(vma, addr, pmdp);

There are some gaps we need to close before getting this fix in:

Quoted 3 lines

1. GUP-fast cannot follow the invalidated PMD: riscv and LoongArch need pmd_access_permitted() that requires _PAGE_PRESENT; s390's pmdp_invalidate() needs a change.

Ugh. We really need pmdp_invalidate() to have reasonable semantics. This whole PMD locking is a mess :(

Quoted 3 lines

2. sparc64's thp_pte_count can be imbalanced with this change (based on Lance's offlist feedback).

Quoted 8 lines

Ack.

Quoted 4 lines

In addition, powerpc has a page table check issue similr to riscv, where riscv fixed it with commit 9f4a88b8d01a6. This is not related to this issue but discovered along with the investigation.

Zi, do you have the capacity to take over this patch?

Yes, I can take over it. My plan is to send a series including patches for 1 and this patch as is. powerpc fix can be a separate one.

Best Regards,
Yan, Zi

#5 lore raw
Quoted 24 lines · Zi Yan
Quoted 22 lines · David Hildenbrand (Arm)
Quoted 6 lines · Zi Yan

There are some gaps we need to close before getting this fix in:

1. GUP-fast cannot follow the invalidated PMD: riscv and LoongArch need pmd_access_permitted() that requires _PAGE_PRESENT; s390's pmdp_invalidate() needs a change.

Ugh. We really need pmdp_invalidate() to have reasonable semantics. This whole PMD locking is a mess :(

Quoted 3 lines

2. sparc64's thp_pte_count can be imbalanced with this change (based on Lance's offlist feedback).

Ack.

Quoted 4 lines

In addition, powerpc has a page table check issue similr to riscv, where riscv fixed it with commit 9f4a88b8d01a6. This is not related to this issue but discovered along with the investigation.

Quoted 7 lines
Quoted 1 line

Zi, do you have the capacity to take over this patch?

Yes, I can take over it. My plan is to send a series including patches for 1 and this patch as is. powerpc fix can be a separate one.

Best Regards,
Yan, Zi

If we need a quick stable fix we could temporarily disable the whole thing until it is fixed, just a thought.

Signature · 3 lines
Cheers,

David
reviewer
#6 lore raw
Quoted 27 lines · David Hildenbrand (Arm)
Quoted 26 lines · Zi Yan
Quoted 23 lines · David Hildenbrand (Arm)
Quoted 6 lines · Zi Yan

There are some gaps we need to close before getting this fix in:

1. GUP-fast cannot follow the invalidated PMD: riscv and LoongArch need pmd_access_permitted() that requires _PAGE_PRESENT; s390's pmdp_invalidate() needs a change.

Ugh. We really need pmdp_invalidate() to have reasonable semantics. This whole PMD locking is a mess :(

Quoted 3 lines

2. sparc64's thp_pte_count can be imbalanced with this change (based on Lance's offlist feedback).

Ack.

Quoted 4 lines

In addition, powerpc has a page table check issue similr to riscv, where riscv fixed it with commit 9f4a88b8d01a6. This is not related to this issue but discovered along with the investigation.

Zi, do you have the capacity to take over this patch?

Quoted 8 lines
Quoted 5 lines

Yes, I can take over it. My plan is to send a series including patches for 1 and this patch as is. powerpc fix can be a separate one.

Best Regards,
Yan, Zi

If we need a quick stable fix we could temporarily disable the whole thing until it is fixed, just a thought.

+1 we could do that for now. A quick fix with minimal churn (correctness comes first).

reviewer
#7 lore raw
Quoted 33 lines · Lance Yang
Quoted 30 lines · David Hildenbrand (Arm)
Quoted 29 lines · Zi Yan
Quoted 15 lines · David Hildenbrand (Arm)
Quoted 6 lines · Zi Yan

There are some gaps we need to close before getting this fix in:

1. GUP-fast cannot follow the invalidated PMD: riscv and LoongArch need pmd_access_permitted() that requires _PAGE_PRESENT; s390's pmdp_invalidate() needs a change.

Ugh. We really need pmdp_invalidate() to have reasonable semantics. This whole PMD locking is a mess :(

Quoted 3 lines

2. sparc64's thp_pte_count can be imbalanced with this change (based on Lance's offlist feedback).

Quoted 8 lines

Ack.

Quoted 4 lines

In addition, powerpc has a page table check issue similr to riscv, where riscv fixed it with commit 9f4a88b8d01a6. This is not related to this issue but discovered along with the investigation.

Zi, do you have the capacity to take over this patch?

Yes, I can take over it. My plan is to send a series including patches for 1 and this patch as is. powerpc fix can be a separate one.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Best Regards,
Yan, Zi

If we need a quick stable fix we could temporarily disable the whole thing until it is fixed, just a thought.

+1 we could do that for now. A quick fix with minimal churn (correctness comes first).

How about the patch below? unmap_huge_pmd_locked() and __discard_anon_folio_pmd_locked() will be dead code to keep the patch small. Later, with arch fixes, the function can be re-enabled.

From 2204b9d08530796847a586428b1f782b987d071c Mon Sep 17 00:00:00 2001
From: Zi Yan <[email protected]>
Date: Fri, 9 Oct 2026 21:28:10 -0400
Subject: [PATCH] mm/rmap: don't discard lazyfree THPs at PMD level

__discard_anon_folio_pmd_locked() clears a lazyfree THP PMD before it knows whether the folio can be discarded, and restores it if the folio was redirtied or has extra references. A concurrent munmap() or MREMAP_DONTUNMAP skips the temporary none PMD and unlinks the VMA from its anon_vma, so the folio stays mapped after the anon_vma is freed and a later rmap walk uses the freed anon_vma.

Using an invalidated PMD instead of a cleared one requires additional arch code fixes. Instead, disable the PMD level discard of lazyfree THPs, as before commit 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()").

Fixes 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()")
Assisted-by LLM
---
 mm/rmap.c | 11 -----------
 1 file changed, 11 deletions(-)
mm/rmap.c +0 -11
index 805db93fe0428..1131b76bbbc28 100644
@@ -2275,17 +2275,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
 		}
 
 		if (!pvmw.pte) {
-			if (folio_test_lazyfree(folio)) {
-				if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
-					goto walk_done;
-				/*
-				 * unmap_huge_pmd_locked has either already marked
-				 * the folio as swap-backed or decided to retain it
-				 * due to GUP or speculative references.
-				 */
-				goto walk_abort;
-			}
-
 			if (flags & TTU_SPLIT_HUGE_PMD) {
 				/*
 				 * We temporarily have to drop the PTL and
Signature · 7 lines
2.53.0



-- 
Best Regards,
Yan, Zi
reviewer Reviewed-by
#8 lore raw
Quoted 40 lines · Zi Yan
Quoted 39 lines · Lance Yang
Quoted 35 lines · David Hildenbrand (Arm)
Quoted 24 lines · Zi Yan
Quoted 22 lines · David Hildenbrand (Arm)
Quoted 6 lines · Zi Yan

There are some gaps we need to close before getting this fix in:

1. GUP-fast cannot follow the invalidated PMD: riscv and LoongArch need pmd_access_permitted() that requires _PAGE_PRESENT; s390's pmdp_invalidate() needs a change.

Ugh. We really need pmdp_invalidate() to have reasonable semantics. This whole PMD locking is a mess :(

Quoted 3 lines

2. sparc64's thp_pte_count can be imbalanced with this change (based on Lance's offlist feedback).

Ack.

Quoted 4 lines

In addition, powerpc has a page table check issue similr to riscv, where riscv fixed it with commit 9f4a88b8d01a6. This is not related to this issue but discovered along with the investigation.

Quoted 7 lines
Quoted 1 line

Zi, do you have the capacity to take over this patch?

Yes, I can take over it. My plan is to send a series including patches for 1 and this patch as is. powerpc fix can be a separate one.

Best Regards,
Yan, Zi

If we need a quick stable fix we could temporarily disable the whole thing until it is fixed, just a thought.

Quoted 6 lines
Quoted 2 lines

+1 we could do that for now. A quick fix with minimal churn (correctness comes first).

How about the patch below? unmap_huge_pmd_locked() and __discard_anon_folio_pmd_locked() will be dead code to keep the patch small. Later, with arch fixes, the function can be re-enabled.

Looks good to me! Thanks for the quick fix!

Quoted 19 lines

From 2204b9d08530796847a586428b1f782b987d071c Mon Sep 17 00:00:00 2001
From: Zi Yan <[email protected]>
Date: Fri, 9 Oct 2026 21:28:10 -0400
Subject: [PATCH] mm/rmap: don't discard lazyfree THPs at PMD level

__discard_anon_folio_pmd_locked() clears a lazyfree THP PMD before it knows whether the folio can be discarded, and restores it if the folio was redirtied or has extra references. A concurrent munmap() or MREMAP_DONTUNMAP skips the temporary none PMD and unlinks the VMA from its anon_vma, so the folio stays mapped after the anon_vma is freed and a later rmap walk uses the freed anon_vma.

Using an invalidated PMD instead of a cleared one requires additional arch code fixes. Instead, disable the PMD level discard of lazyfree THPs, as before commit 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()").

Quoted 7 lines
Fixes 735ecdfaf4e8 ("mm/vmscan: avoid split lazyfree THP during shrink_folio_list()")
Assisted-by LLM

---

Quoted 25 lines
  mm/rmap.c | 11 -----------
  1 file changed, 11 deletions(-)
mm/rmap.c +0 -11
index 805db93fe0428..1131b76bbbc28 100644
@@ -2275,17 +2275,6 @@ static bool try_to_unmap_one(struct folio *folio, struct vm_area_struct *vma,
  		}
 
  		if (!pvmw.pte) {
-			if (folio_test_lazyfree(folio)) {
-				if (unmap_huge_pmd_locked(vma, pvmw.address, pvmw.pmd, folio))
-					goto walk_done;
-				/*
-				 * unmap_huge_pmd_locked has either already marked
-				 * the folio as swap-backed or decided to retain it
-				 * due to GUP or speculative references.
-				 */
-				goto walk_abort;
-			}
-
  			if (flags & TTU_SPLIT_HUGE_PMD) {
  				/*
  				 * We temporarily have to drop the PTL and