[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20240904084022.32728-10-zhengqi.arch@bytedance.com>
Date: Wed, 4 Sep 2024 16:40:17 +0800
From: Qi Zheng <zhengqi.arch@...edance.com>
To: david@...hat.com,
hughd@...gle.com,
willy@...radead.org,
muchun.song@...ux.dev,
vbabka@...nel.org,
akpm@...ux-foundation.org,
rppt@...nel.org,
vishal.moola@...il.com,
peterx@...hat.com,
ryan.roberts@....com,
christophe.leroy2@...soprasteria.com
Cc: linux-kernel@...r.kernel.org,
linux-mm@...ck.org,
linux-arm-kernel@...ts.infradead.org,
linuxppc-dev@...ts.ozlabs.org,
Qi Zheng <zhengqi.arch@...edance.com>
Subject: [PATCH v3 09/14] mm: mremap: move_ptes() use pte_offset_map_rw_nolock()
In move_ptes(), we may modify the new_pte after acquiring the new_ptl, so
convert it to using pte_offset_map_rw_nolock(). Since we may free the PTE
page in retract_page_tables() without holding the read lock of mmap_lock,
so we still need to do a pmd_same() check after holding the PTL.
Signed-off-by: Qi Zheng <zhengqi.arch@...edance.com>
---
mm/mremap.c | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/mm/mremap.c b/mm/mremap.c
index 24712f8dbb6b5..16e54151395ad 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -143,6 +143,7 @@ static int move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
spinlock_t *old_ptl, *new_ptl;
bool force_flush = false;
unsigned long len = old_end - old_addr;
+ pmd_t pmdval;
int err = 0;
/*
@@ -175,14 +176,29 @@ static int move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
err = -EAGAIN;
goto out;
}
- new_pte = pte_offset_map_nolock(mm, new_pmd, new_addr, &new_ptl);
+ /*
+ * Since we may free the PTE page in retract_page_tables() without
+ * holding the read lock of mmap_lock, so we still need to do a
+ * pmd_same() check after holding the PTL.
+ */
+ new_pte = pte_offset_map_rw_nolock(mm, new_pmd, new_addr, &pmdval,
+ &new_ptl);
if (!new_pte) {
pte_unmap_unlock(old_pte, old_ptl);
err = -EAGAIN;
goto out;
}
- if (new_ptl != old_ptl)
+ if (new_ptl != old_ptl) {
spin_lock_nested(new_ptl, SINGLE_DEPTH_NESTING);
+
+ if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(new_pmd)))) {
+ pte_unmap_unlock(new_pte, new_ptl);
+ pte_unmap_unlock(old_pte, old_ptl);
+ err = -EAGAIN;
+ goto out;
+ }
+ }
+
flush_tlb_batched_pending(vma->vm_mm);
arch_enter_lazy_mmu_mode();
--
2.20.1
Powered by blists - more mailing lists