[PATCH v2 6/7] mm/khugepaged: unmap pte before releasing vma write lock

From: Nico Pache

Date: Tue Jul 14 2026 - 23:03:02 EST


We are currently dropping the anon_vma write lock before unmapping the
PTE. Although this is safe, due to us still holding the mmap_write_lock,
its safer and less confusing to switch the order of these two operations.

Suggested-by: David Hildenbrand <david@xxxxxxxxxx>
Signed-off-by: Nico Pache <npache@xxxxxxxxxx>
---
mm/khugepaged.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/mm/khugepaged.c b/mm/khugepaged.c
index 655dbc90535a..40125dcb4de9 100644
--- a/mm/khugepaged.c
+++ b/mm/khugepaged.c
@@ -1546,10 +1546,10 @@ static enum scan_result collapse_huge_page(struct mm_struct *mm, unsigned long s

result = SCAN_SUCCEED;
out_up_write:
- if (anon_vma_locked)
- anon_vma_unlock_write(vma->anon_vma);
if (pte)
pte_unmap(pte);
+ if (anon_vma_locked)
+ anon_vma_unlock_write(vma->anon_vma);
mmap_write_unlock(mm);
out_nolock:
if (folio)
--
2.54.0