Skip to contentSkip to conversation

introduce pte_offset_map_{ro|rw}_nolock()

PATCH Superseded Qi Zheng 3 participants 29 messages Sep 24, 2024

Subsystems (13)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Qi Zheng

author
#0 lore raw

v4 cover introduce pte_offset_map_{ro|rw}_nolock()

Changes in v4:
- arm: adjust_pte() use pte_offset_map_rw_nolock()
(use ptl != vmf->ptl to check if we are using split PTE locks)
mm: khugepaged: collapse_pte_mapped_thp() use pte_offset_map_rw_nolock() (move the pte_unmap() backward)
mm: copy_pte_range() use pte_offset_map_rw_nolock()
(remove pmd_same() check)
mm: mremap: move_ptes() use pte_offset_map_rw_nolock() (remove pmd_same() check)
mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock() (move the assignment to pvmw->ptl backward)
- remove [PATCH v3 14/14] (will be sent as a separate patch)
- reorder patches
- collect the Reviewed-bys
- rebase onto the next-20240923

Changes in v3:
- change to use VM_WARN_ON_ONCE() instead of BUG_ON() in pte_offset_map_rw_nolock() (David Hildenbrand)
- modify the comment above the pte_offset_map_lock() in [PATCH v2 01/14] (David Hildenbrand and Muchun Song)
- modify the comment above the pte_offset_map_rw_nolock() in [PATCH v2 06/14] (David Hildenbrand and Muchun Song)
- also perform a pmd_same() check in [PATCH v2 08/14] and [PATCH v2 09/14] (since we may free the PTE page in retract_page_tables() without holding the read lock of mmap_lock)
- collect the Acked-bys and Reviewed-bys
- rebase onto the next-20240904

Changes in v2:
- rename pte_offset_map_{readonly|maywrite}_nolock() to pte_offset_map_{ro|rw}_nolock() (LEROY Christophe)
- make pte_offset_map_rw_nolock() not accept NULL parameters (David Hildenbrand)
- rebase onto the next-20240822

Hi all,

As proposed by David Hildenbrand [1], this series introduces the following two new helper functions to replace pte_offset_map_nolock().

1. pte_offset_map_ro_nolock()
2. pte_offset_map_rw_nolock()

As the name suggests, pte_offset_map_ro_nolock() is used for read-only case. In this case, only read-only operations will be performed on PTE page after the PTL is held. The RCU lock in pte_offset_map_nolock() will ensure that the PTE page will not be freed, and there is no need to worry about whether the pmd entry is modified. Therefore pte_offset_map_ro_nolock() is just a renamed version of pte_offset_map_nolock().

pte_offset_map_rw_nolock() is used for may-write case. In this case, the pte or pmd entry may be modified after the PTL is held, so we need to ensure that the pmd entry has not been modified concurrently. So in addition to the name change, it also outputs the pmdval when successful. The users should make sure the page table is stable like checking pte_same() or checking pmd_same() by using the output pmdval before performing the write operations.

This series will convert all pte_offset_map_nolock() into the above two helper functions one by one, and finally completely delete it.

This also a preparation for reclaiming the empty user PTE page table pages.

This series is based on the next-20240923.

Comments and suggestions are welcome!

Thanks,
Qi

Qi Zheng (13):
mm: pgtable: introduce pte_offset_map_{ro|rw}_nolock()
powerpc: assert_pte_locked() use pte_offset_map_ro_nolock()
mm: filemap: filemap_fault_recheck_pte_none() use
pte_offset_map_ro_nolock()
mm: khugepaged: __collapse_huge_page_swapin() use
pte_offset_map_ro_nolock()
arm: adjust_pte() use pte_offset_map_rw_nolock()
mm: handle_pte_fault() use pte_offset_map_rw_nolock()
mm: khugepaged: collapse_pte_mapped_thp() use
pte_offset_map_rw_nolock()
mm: copy_pte_range() use pte_offset_map_rw_nolock()
mm: mremap: move_ptes() use pte_offset_map_rw_nolock()
mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock()
mm: userfaultfd: move_pages_pte() use pte_offset_map_rw_nolock()
mm: multi-gen LRU: walk_pte_range() use pte_offset_map_rw_nolock()
mm: pgtable: remove pte_offset_map_nolock()

 Documentation/mm/split_page_table_lock.rst |  6 ++-
 arch/arm/mm/fault-armv.c                   | 53 +++++++++-------------
 arch/powerpc/mm/pgtable.c                  |  2 +-
 include/linux/mm.h                         |  7 ++-
 mm/filemap.c                               |  4 +-
 mm/khugepaged.c                            | 20 ++++++--
 mm/memory.c                                | 25 ++++++++--
 mm/mremap.c                                | 11 ++++-
 mm/page_vma_mapped.c                       | 25 ++++++++--
 mm/pgtable-generic.c                       | 43 +++++++++++++++---
 mm/userfaultfd.c                           | 15 ++++--
 mm/vmscan.c                                |  9 +++-
 12 files changed, 159 insertions(+), 61 deletions(-)
Signature · 1 line
2.20.1
author
#1 lore raw

PATCH v4 1/13 mm: pgtable: introduce pte_offset_map_{ro|rw}_nolock()

Currently, the usage of pte_offset_map_nolock() can be divided into the following two cases:

1) After acquiring PTL, only read-only operations are performed on the PTE page. In this case, the RCU lock in pte_offset_map_nolock() will ensure that the PTE page will not be freed, and there is no need to worry about whether the pmd entry is modified.

2) After acquiring PTL, the pte or pmd entries may be modified. At this time, we need to ensure that the pmd entry has not been modified concurrently.

To more clearing distinguish between these two cases, this commit introduces two new helper functions to replace pte_offset_map_nolock(). For 1), just rename it to pte_offset_map_ro_nolock(). For 2), in addition to changing the name to pte_offset_map_rw_nolock(), it also outputs the pmdval when successful. It is applicable for may-write cases where any modification operations to the page table may happen after the corresponding spinlock is held afterwards. But the users should make sure the page table is stable like checking pte_same() or checking pmd_same() by using the output pmdval before performing the write operations.

Note: "RO" / "RW" expresses the intended semantics, not that the *kmap* will be read-only/read-write protected.

Subsequent commits will convert pte_offset_map_nolock() into the above two functions one by one, and finally completely delete it.

---
 Documentation/mm/split_page_table_lock.rst |  7 +++
 include/linux/mm.h                         |  5 +++
 mm/pgtable-generic.c                       | 50 ++++++++++++++++++++++
 3 files changed, 62 insertions(+)
Documentation/mm/split_page_table_lock.rst +7 -0
index e4f6972eb6c04..08d0e706a32db 100644
@@ -19,6 +19,13 @@ There are helpers to lock/unlock a table and other accessor functions:
  - pte_offset_map_nolock()
 	maps PTE, returns pointer to PTE with pointer to its PTE table
 	lock (not taken), or returns NULL if no PTE table;
+ - pte_offset_map_ro_nolock()
+	maps PTE, returns pointer to PTE with pointer to its PTE table
+	lock (not taken), or returns NULL if no PTE table;
+ - pte_offset_map_rw_nolock()
+	maps PTE, returns pointer to PTE with pointer to its PTE table
+	lock (not taken) and the value of its pmd entry, or returns NULL
+	if no PTE table;
  - pte_offset_map()
 	maps PTE, returns pointer to PTE, or returns NULL if no PTE table;
  - pte_unmap()
include/linux/mm.h +5 -0
index 546a9406859ad..9a4550cd830c9 100644
@@ -3017,6 +3017,11 @@ static inline pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd,
 
 pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd,
 			unsigned long addr, spinlock_t **ptlp);
+pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
+				unsigned long addr, spinlock_t **ptlp);
+pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd,
+				unsigned long addr, pmd_t *pmdvalp,
+				spinlock_t **ptlp);
 
 #define pte_unmap_unlock(pte, ptl)	do {		\
 	spin_unlock(ptl);				\
mm/pgtable-generic.c +50 -0

62 lines not on this page · raw message

Signature · 1 line
2.20.1
Reviewed-by on v4 1/13
#2 lore raw

Re: [PATCH v4 01/13] mm: pgtable: introduce pte_offset_map_{ro|rw}_nolock()

Quoted 26 lines · Qi Zheng

Currently, the usage of pte_offset_map_nolock() can be divided into the following two cases:

1) After acquiring PTL, only read-only operations are performed on the PTE page. In this case, the RCU lock in pte_offset_map_nolock() will ensure that the PTE page will not be freed, and there is no need to worry about whether the pmd entry is modified.

2) After acquiring PTL, the pte or pmd entries may be modified. At this time, we need to ensure that the pmd entry has not been modified concurrently.

To more clearing distinguish between these two cases, this commit introduces two new helper functions to replace pte_offset_map_nolock(). For 1), just rename it to pte_offset_map_ro_nolock(). For 2), in addition to changing the name to pte_offset_map_rw_nolock(), it also outputs the pmdval when successful. It is applicable for may-write cases where any modification operations to the page table may happen after the corresponding spinlock is held afterwards. But the users should make sure the page table is stable like checking pte_same() or checking pmd_same() by using the output pmdval before performing the write operations.

Quoted 7 lines

Note: "RO" / "RW" expresses the intended semantics, not that the *kmap* will be read-only/read-write protected.

Subsequent commits will convert pte_offset_map_nolock() into the above two functions one by one, and finally completely delete it.

Acked-by on v4 1/13
#3 lore raw

Re: [PATCH v4 01/13] mm: pgtable: introduce pte_offset_map_{ro|rw}_nolock()

In reply to Qi Zheng
Quoted 22 lines · Qi Zheng

Currently, the usage of pte_offset_map_nolock() can be divided into the following two cases:

1) After acquiring PTL, only read-only operations are performed on the PTE page. In this case, the RCU lock in pte_offset_map_nolock() will ensure that the PTE page will not be freed, and there is no need to worry about whether the pmd entry is modified.

2) After acquiring PTL, the pte or pmd entries may be modified. At this time, we need to ensure that the pmd entry has not been modified concurrently.

To more clearing distinguish between these two cases, this commit introduces two new helper functions to replace pte_offset_map_nolock(). For 1), just rename it to pte_offset_map_ro_nolock(). For 2), in addition to changing the name to pte_offset_map_rw_nolock(), it also outputs the pmdval when successful. It is applicable for may-write cases where any modification operations to the page table may happen after the corresponding spinlock is held afterwards. But the users should make sure the page table is stable like checking pte_same() or checking pmd_same() by using the output pmdval before performing the write operations.

Quoted 8 lines

Note: "RO" / "RW" expresses the intended semantics, not that the *kmap* will be read-only/read-write protected.

Subsequent commits will convert pte_offset_map_nolock() into the above two functions one by one, and finally completely delete it.

---

Signature · 3 lines
Cheers,

David / dhildenb
author
#4 lore raw

PATCH v4 2/13 powerpc: assert_pte_locked() use pte_offset_map_ro_nolock()

In reply to Qi Zheng

In assert_pte_locked(), we just get the ptl and assert if it was already held, so convert it to using pte_offset_map_ro_nolock().

---
 arch/powerpc/mm/pgtable.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/powerpc/mm/pgtable.c +1 -1
index 7316396e452d8..61df5aed79894 100644
@@ -398,7 +398,7 @@ void assert_pte_locked(struct mm_struct *mm, unsigned long addr)
 	 */
 	if (pmd_none(*pmd))
 		return;
-	pte = pte_offset_map_nolock(mm, pmd, addr, &ptl);
+	pte = pte_offset_map_ro_nolock(mm, pmd, addr, &ptl);
 	BUG_ON(!pte);
 	assert_spin_locked(ptl);
 	pte_unmap(pte);
Signature · 1 line
2.20.1
author
#5 lore raw

PATCH v4 3/13 mm: filemap: filemap_fault_recheck_pte_none() use pte_offset_map_ro_nolock()

In reply to Qi Zheng

In filemap_fault_recheck_pte_none(), we just do pte_none() check, so convert it to using pte_offset_map_ro_nolock().

---
 mm/filemap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/filemap.c +2 -2
index 3e46ca45e13dc..6c6ff8722550f 100644
@@ -3271,8 +3271,8 @@ static vm_fault_t filemap_fault_recheck_pte_none(struct vm_fault *vmf)
 	if (!(vmf->flags & FAULT_FLAG_ORIG_PTE_VALID))
 		return 0;
 
-	ptep = pte_offset_map_nolock(vma->vm_mm, vmf->pmd, vmf->address,
-				     &vmf->ptl);
+	ptep = pte_offset_map_ro_nolock(vma->vm_mm, vmf->pmd, vmf->address,
+					&vmf->ptl);
 	if (unlikely(!ptep))
 		return VM_FAULT_NOPAGE;
 
Signature · 1 line
2.20.1
author
#6 lore raw

PATCH v4 4/13 mm: khugepaged: __collapse_huge_page_swapin() use pte_offset_map_ro_nolock()

In reply to Qi Zheng

In __collapse_huge_page_swapin(), we just use the ptl for pte_same() check in do_swap_page(). In other places, we directly use pte_offset_map_lock(), so convert it to using pte_offset_map_ro_nolock().

---
 mm/khugepaged.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
mm/khugepaged.c +5 -1
index f9c39898eaff6..6498721d4783a 100644
@@ -1011,7 +1011,11 @@ static int __collapse_huge_page_swapin(struct mm_struct *mm,
 		};
 
 		if (!pte++) {
-			pte = pte_offset_map_nolock(mm, pmd, address, &ptl);
+			/*
+			 * Here the ptl is only used to check pte_same() in
+			 * do_swap_page(), so readonly version is enough.
+			 */
+			pte = pte_offset_map_ro_nolock(mm, pmd, address, &ptl);
 			if (!pte) {
 				mmap_read_unlock(mm);
 				result = SCAN_PMD_NULL;
Signature · 1 line
2.20.1
author
#7 lore raw

PATCH v4 5/13 arm: adjust_pte() use pte_offset_map_rw_nolock()

In reply to Qi Zheng

In do_adjust_pte(), we may modify the pte entry. The corresponding pmd entry may have been modified concurrently. Therefore, in order to ensure the stability if pmd entry, use pte_offset_map_rw_nolock() to replace pte_offset_map_nolock(), and do pmd_same() check after holding the PTL.

All callers of update_mmu_cache_range() hold the vmf->ptl, so we can determined whether split PTE locks is being used by doing the following, just as we do elsewhere in the kernel.

	ptl != vmf->ptl

And then we can delete the do_pte_lock() and do_pte_unlock().

---
Hi David and Muchun, I did not remove your Acked-by and Reviewed-by tags since there is no functional change.

 arch/arm/mm/fault-armv.c | 53 +++++++++++++++++-----------------------
 1 file changed, 22 insertions(+), 31 deletions(-)
arch/arm/mm/fault-armv.c +22 -31
index 831793cd6ff94..2bec87c3327d2 100644
@@ -61,32 +61,8 @@ static int do_adjust_pte(struct vm_area_struct *vma, unsigned long address,
 	return ret;
 }
 
-#if defined(CONFIG_SPLIT_PTE_PTLOCKS)
-/*
- * If we are using split PTE locks, then we need to take the page
- * lock here.  Otherwise we are using shared mm->page_table_lock
- * which is already locked, thus cannot take it.
- */
-static inline void do_pte_lock(spinlock_t *ptl)
-{
-	/*
-	 * Use nested version here to indicate that we are already
-	 * holding one similar spinlock.
-	 */
-	spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
-}
-
-static inline void do_pte_unlock(spinlock_t *ptl)
-{
-	spin_unlock(ptl);
-}
-#else /* !defined(CONFIG_SPLIT_PTE_PTLOCKS) */
-static inline void do_pte_lock(spinlock_t *ptl) {}
-static inline void do_pte_unlock(spinlock_t *ptl) {}
-#endif /* defined(CONFIG_SPLIT_PTE_PTLOCKS) */
-
 static int adjust_pte(struct vm_area_struct *vma, unsigned long address,
-	unsigned long pfn)
+		      unsigned long pfn, struct vm_fault *vmf)
 {
 	spinlock_t *ptl;
 	pgd_t *pgd;
@@ -94,6 +70,7 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address,
 	pud_t *pud;
 	pmd_t *pmd;
 	pte_t *pte;
+	pmd_t pmdval;
 	int ret;
 
 	pgd = pgd_offset(vma->vm_mm, address);
@@ -112,20 +89,33 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address,
 	if (pmd_none_or_clear_bad(pmd))
 		return 0;
 
+again:
 	/*
 	 * This is called while another page table is mapped, so we
 	 * must use the nested version.  This also means we need to
 	 * open-code the spin-locking.
 	 */
-	pte = pte_offset_map_nolock(vma->vm_mm, pmd, address, &ptl);
+	pte = pte_offset_map_rw_nolock(vma->vm_mm, pmd, address, &pmdval, &ptl);
 	if (!pte)
 		return 0;
 
-	do_pte_lock(ptl);
+	/*
+	 * If we are using split PTE locks, then we need to take the page
+	 * lock here.  Otherwise we are using shared mm->page_table_lock
+	 * which is already locked, thus cannot take it.
+	 */
+	if (ptl != vmf->ptl) {
+		spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
+		if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pmd)))) {
+			pte_unmap_unlock(pte, ptl);
+			goto again;
+		}
+	}
 
 	ret = do_adjust_pte(vma, address, pfn, pte);
 
-	do_pte_unlock(ptl);
+	if (ptl != vmf->ptl)
+		spin_unlock(ptl);
 	pte_unmap(pte);
 
 	return ret;
@@ -133,7 +123,8 @@ static int adjust_pte(struct vm_area_struct *vma, unsigned long address,
 
 static void
 make_coherent(struct address_space *mapping, struct vm_area_struct *vma,
-	unsigned long addr, pte_t *ptep, unsigned long pfn)
+	      unsigned long addr, pte_t *ptep, unsigned long pfn,
+	      struct vm_fault *vmf)
 {
 	struct mm_struct *mm = vma->vm_mm;
 	struct vm_area_struct *mpnt;
@@ -160,7 +151,7 @@ make_coherent(struct address_space *mapping, struct vm_area_struct *vma,
 		if (!(mpnt->vm_flags & VM_MAYSHARE))
 			continue;
 		offset = (pgoff - mpnt->vm_pgoff) << PAGE_SHIFT;
-		aliases += adjust_pte(mpnt, mpnt->vm_start + offset, pfn);
+		aliases += adjust_pte(mpnt, mpnt->vm_start + offset, pfn, vmf);
 	}
 	flush_dcache_mmap_unlock(mapping);
 	if (aliases)
@@ -203,7 +194,7 @@ void update_mmu_cache_range(struct vm_fault *vmf, struct vm_area_struct *vma,
 		__flush_dcache_folio(mapping, folio);
 	if (mapping) {
 		if (cache_is_vivt())
-			make_coherent(mapping, vma, addr, ptep, pfn);
+			make_coherent(mapping, vma, addr, ptep, pfn, vmf);
 		else if (vma->vm_flags & VM_EXEC)
 			__flush_icache_all();
 	}
Signature · 1 line
2.20.1
author
#8 lore raw

PATCH v4 6/13 mm: handle_pte_fault() use pte_offset_map_rw_nolock()

In reply to Qi Zheng

In handle_pte_fault(), we may modify the vmf->pte after acquiring the vmf->ptl, so convert it to using pte_offset_map_rw_nolock(). But since we will do the pte_same() check, so there is no need to get pmdval to do pmd_same() check, just pass a dummy variable to it.

---
 mm/memory.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
mm/memory.c +12 -2
index a245d28787034..6432b636d1ba7 100644
@@ -5750,14 +5750,24 @@ static vm_fault_t handle_pte_fault(struct vm_fault *vmf)
 		vmf->pte = NULL;
 		vmf->flags &= ~FAULT_FLAG_ORIG_PTE_VALID;
 	} else {
+		pmd_t dummy_pmdval;
+
 		/*
 		 * A regular pmd is established and it can't morph into a huge
 		 * pmd by anon khugepaged, since that takes mmap_lock in write
 		 * mode; but shmem or file collapse to THP could still morph
 		 * it into a huge pmd: just retry later if so.
+		 *
+		 * Use the maywrite version to indicate that vmf->pte may be
+		 * modified, but since we will use pte_same() to detect the
+		 * change of the !pte_none() entry, there is no need to recheck
+		 * the pmdval. Here we chooes to pass a dummy variable instead
+		 * of NULL, which helps new user think about why this place is
+		 * special.
 		 */
-		vmf->pte = pte_offset_map_nolock(vmf->vma->vm_mm, vmf->pmd,
-						 vmf->address, &vmf->ptl);
+		vmf->pte = pte_offset_map_rw_nolock(vmf->vma->vm_mm, vmf->pmd,
+						    vmf->address, &dummy_pmdval,
+						    &vmf->ptl);
 		if (unlikely(!vmf->pte))
 			return 0;
 		vmf->orig_pte = ptep_get_lockless(vmf->pte);
Signature · 1 line
2.20.1
author
#9 lore raw

PATCH v4 7/13 mm: khugepaged: collapse_pte_mapped_thp() use pte_offset_map_rw_nolock()

In reply to Qi Zheng

In collapse_pte_mapped_thp(), we may modify the pte and pmd entry after acquring the ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the PTL held. So we should get pgt_pmd and do pmd_same() check after the ptl held.

---
 mm/khugepaged.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
mm/khugepaged.c +11 -3
index 6498721d4783a..8ab79c13d077f 100644
@@ -1605,7 +1605,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
 		pml = pmd_lock(mm, pmd);
 
-	start_pte = pte_offset_map_nolock(mm, pmd, haddr, &ptl);
+	start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl);
 	if (!start_pte)		/* mmap_lock + page lock should prevent this */
 		goto abort;
 	if (!pml)
@@ -1613,6 +1613,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	else if (ptl != pml)
 		spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
 
+	if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd))))
+		goto abort;
+
 	/* step 2: clear page table and adjust rmap */
 	for (i = 0, addr = haddr, pte = start_pte;
 	     i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
@@ -1645,7 +1648,6 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 		nr_ptes++;
 	}
 
-	pte_unmap(start_pte);
 	if (!pml)
 		spin_unlock(ptl);
 
@@ -1658,13 +1660,19 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
 	/* step 4: remove empty page table */
 	if (!pml) {
 		pml = pmd_lock(mm, pmd);
-		if (ptl != pml)
+		if (ptl != pml) {
 			spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
+			if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
+				spin_unlock(pml);
+				goto abort;
+			}
+		}
 	}
 	pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd);
 	pmdp_get_lockless_sync();
 	if (ptl != pml)
 		spin_unlock(ptl);
+	pte_unmap(start_pte);
 	spin_unlock(pml);
 
 	mmu_notifier_invalidate_range_end(&range);
Signature · 1 line
2.20.1
#10 lore raw

Re: [PATCH v4 07/13] mm: khugepaged: collapse_pte_mapped_thp() use pte_offset_map_rw_nolock()

Quoted 45 lines · Qi Zheng

In collapse_pte_mapped_thp(), we may modify the pte and pmd entry after acquring the ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the PTL held. So we should get pgt_pmd and do pmd_same() check after the ptl held.

---
mm/khugepaged.c | 14 +++++++++++---

1 file changed, 11 insertions(+), 3 deletions(-)
mm/khugepaged.c +4 -2
index 6498721d4783a..8ab79c13d077f 100644
@@ -1605,7 +1605,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
   if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
       pml = pmd_lock(mm, pmd);
 
-    start_pte = pte_offset_map_nolock(mm, pmd, haddr, &ptl);
+    start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl);
   if (!start_pte)        /* mmap_lock + page lock should prevent this */
       goto abort;
   if (!pml)
@@ -1613,6 +1613,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
   else if (ptl != pml)
       spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
 
+    if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd))))
+        goto abort;
+
   /* step 2: clear page table and adjust rmap */
   for (i = 0, addr = haddr, pte = start_pte;
        i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
@@ -1645,7 +1648,6 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
       nr_ptes++;
   }
 
-    pte_unmap(start_pte);
   if (!pml)
       spin_unlock(ptl);
 

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1667

Quoted 9 lines
mm/khugepaged.c +4 -1
@@ -1658,13 +1660,19 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
   /* step 4: remove empty page table */
   if (!pml) {
       pml = pmd_lock(mm, pmd);
-        if (ptl != pml)
+        if (ptl != pml) {
           spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
+            if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
+                spin_unlock(pml);
+                goto abort;

Drop the reference of folio and the mm counter twice at the label of abort and the step 3.

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1676

Quoted 9 lines
mm/khugepaged.c +3 -0
+            }
+        }
   }
   pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd);
   pmdp_get_lockless_sync();
   if (ptl != pml)
       spin_unlock(ptl);
+    pte_unmap(start_pte);
   spin_unlock(pml);

Why not?

pte_unmap_unlock(start_pte, ptl);
if (pml != ptl)

        spin_unlock(pml);
Quoted 4 lines
mm/khugepaged.c +0 -1
   mmu_notifier_invalidate_range_end(&range);
--

2.20.1

author
#11 lore raw

Re: [PATCH v4 07/13] mm: khugepaged: collapse_pte_mapped_thp() use pte_offset_map_rw_nolock()

Quoted 44 lines · Muchun Song
Quoted 44 lines · Qi Zheng

In collapse_pte_mapped_thp(), we may modify the pte and pmd entry after acquring the ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the PTL held. So we should get pgt_pmd and do pmd_same() check after the ptl held.

---
mm/khugepaged.c | 14 +++++++++++---

1 file changed, 11 insertions(+), 3 deletions(-)
mm/khugepaged.c +4 -2
index 6498721d4783a..8ab79c13d077f 100644
@@ -1605,7 +1605,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
    if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
        pml = pmd_lock(mm, pmd);
 
-    start_pte = pte_offset_map_nolock(mm, pmd, haddr, &ptl);
+    start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl);
    if (!start_pte)        /* mmap_lock + page lock should prevent this */
        goto abort;
    if (!pml)
@@ -1613,6 +1613,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
    else if (ptl != pml)
        spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
 
+    if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd))))
+        goto abort;
+
    /* step 2: clear page table and adjust rmap */
    for (i = 0, addr = haddr, pte = start_pte;
         i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
@@ -1645,7 +1648,6 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
        nr_ptes++;
    }
 
-    pte_unmap(start_pte);
    if (!pml)
        spin_unlock(ptl);
 
Quoted 12 lines

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1667

Quoted 10 lines
mm/khugepaged.c +4 -1
@@ -1658,13 +1660,19 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
    /* step 4: remove empty page table */
    if (!pml) {
        pml = pmd_lock(mm, pmd);
-        if (ptl != pml)
+        if (ptl != pml) {
            spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
+            if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
+                spin_unlock(pml);
+                goto abort;

Drop the reference of folio and the mm counter twice at the label of abort and the step 3.

My bad, should set nr_ptes to 0 and call flush_tlb_mm() here, right?

Quoted 11 lines

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1676

Quoted 10 lines
mm/khugepaged.c +3 -0
+            }
+        }
    }
    pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd);
    pmdp_get_lockless_sync();
    if (ptl != pml)
        spin_unlock(ptl);
+    pte_unmap(start_pte);
    spin_unlock(pml);
Quoted 5 lines

Why not?

pte_unmap_unlock(start_pte, ptl);
if (pml != ptl)

         spin_unlock(pml);

Both are fine, will do.

Thanks,
Qi

Quoted 5 lines
Quoted 5 lines
mm/khugepaged.c +0 -1
    mmu_notifier_invalidate_range_end(&range);
--

2.20.1

#12 lore raw

Re: [PATCH v4 07/13] mm: khugepaged: collapse_pte_mapped_thp() use pte_offset_map_rw_nolock()

Quoted 61 lines · Qi Zheng
Quoted 42 lines · Muchun Song
Quoted 42 lines · Qi Zheng

In collapse_pte_mapped_thp(), we may modify the pte and pmd entry after acquring the ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the PTL held. So we should get pgt_pmd and do pmd_same() check after the ptl held.

---
mm/khugepaged.c | 14 +++++++++++---

1 file changed, 11 insertions(+), 3 deletions(-)
mm/khugepaged.c +4 -2
index 6498721d4783a..8ab79c13d077f 100644
@@ -1605,7 +1605,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
   if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
       pml = pmd_lock(mm, pmd);
 
-    start_pte = pte_offset_map_nolock(mm, pmd, haddr, &ptl);
+    start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl);
   if (!start_pte)        /* mmap_lock + page lock should prevent this */
       goto abort;
   if (!pml)
@@ -1613,6 +1613,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
   else if (ptl != pml)
       spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
 
+    if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd))))
+        goto abort;
+
   /* step 2: clear page table and adjust rmap */
   for (i = 0, addr = haddr, pte = start_pte;
        i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
@@ -1645,7 +1648,6 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
       nr_ptes++;
   }
 
-    pte_unmap(start_pte);
   if (!pml)
       spin_unlock(ptl);
 
Quoted 11 lines

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1667

Quoted 10 lines
mm/khugepaged.c +4 -1
@@ -1658,13 +1660,19 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
   /* step 4: remove empty page table */
   if (!pml) {
       pml = pmd_lock(mm, pmd);
-        if (ptl != pml)
+        if (ptl != pml) {
           spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
+            if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
+                spin_unlock(pml);
+                goto abort;

Drop the reference of folio and the mm counter twice at the label of abort and the step 3.

Quoted 1 line

My bad, should set nr_ptes to 0 and call flush_tlb_mm() here, right?

Or add a new label "out" just below the "abort". Then go to out.

Quoted 24 lines
Quoted 10 lines

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1676

Quoted 10 lines
mm/khugepaged.c +3 -0
+            }
+        }
   }
   pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd);
   pmdp_get_lockless_sync();
   if (ptl != pml)
       spin_unlock(ptl);
+    pte_unmap(start_pte);
   spin_unlock(pml);
Quoted 4 lines

Why not?
pte_unmap_unlock(start_pte, ptl);
if (pml != ptl)

        spin_unlock(pml);

Both are fine, will do.

Thanks,
Qi

Quoted 4 lines
Quoted 4 lines
mm/khugepaged.c +0 -1
   mmu_notifier_invalidate_range_end(&range);
--

2.20.1

author
#13 lore raw

Re: [PATCH v4 07/13] mm: khugepaged: collapse_pte_mapped_thp() use pte_offset_map_rw_nolock()

Quoted 61 lines · Muchun Song
Quoted 61 lines · Qi Zheng
Quoted 53 lines · Muchun Song
Quoted 43 lines · Qi Zheng

In collapse_pte_mapped_thp(), we may modify the pte and pmd entry after acquring the ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the PTL held. So we should get pgt_pmd and do pmd_same() check after the ptl held.

---
mm/khugepaged.c | 14 +++++++++++---

1 file changed, 11 insertions(+), 3 deletions(-)
mm/khugepaged.c +4 -2
index 6498721d4783a..8ab79c13d077f 100644
@@ -1605,7 +1605,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
    if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
        pml = pmd_lock(mm, pmd);
 
-    start_pte = pte_offset_map_nolock(mm, pmd, haddr, &ptl);
+    start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl);
    if (!start_pte)        /* mmap_lock + page lock should prevent this */
        goto abort;
    if (!pml)
@@ -1613,6 +1613,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
    else if (ptl != pml)
        spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
 
+    if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd))))
+        goto abort;
+
    /* step 2: clear page table and adjust rmap */
    for (i = 0, addr = haddr, pte = start_pte;
         i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
@@ -1645,7 +1648,6 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
        nr_ptes++;
    }
 
-    pte_unmap(start_pte);
    if (!pml)
        spin_unlock(ptl);
 

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1667

Quoted 9 lines
mm/khugepaged.c +4 -1
@@ -1658,13 +1660,19 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
    /* step 4: remove empty page table */
    if (!pml) {
        pml = pmd_lock(mm, pmd);
-        if (ptl != pml)
+        if (ptl != pml) {
            spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
+            if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
+                spin_unlock(pml);
+                goto abort;

Drop the reference of folio and the mm counter twice at the label of abort and the step 3.

Quoted 3 lines
Quoted 1 line

My bad, should set nr_ptes to 0 and call flush_tlb_mm() here, right?

Or add a new label "out" just below the "abort". Then go to out.

For this way, we also need to call flush_tlb_mm() first, like the following:

if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
	spin_unlock(pml);
	flush_tlb_mm(mm);
	goto out;
}
Quoted 27 lines
Quoted 25 lines
Quoted 11 lines

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1676

Quoted 11 lines
mm/khugepaged.c +3 -0
+            }
+        }
    }
    pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd);
    pmdp_get_lockless_sync();
    if (ptl != pml)
        spin_unlock(ptl);
+    pte_unmap(start_pte);
    spin_unlock(pml);
Quoted 4 lines

Why not?
pte_unmap_unlock(start_pte, ptl);
if (pml != ptl)

         spin_unlock(pml);

Both are fine, will do.

Thanks,
Qi

Quoted 4 lines
Quoted 4 lines
mm/khugepaged.c +0 -1
    mmu_notifier_invalidate_range_end(&range);
--

2.20.1

#14 lore raw

Re: [PATCH v4 07/13] mm: khugepaged: collapse_pte_mapped_thp() use pte_offset_map_rw_nolock()

Quoted 69 lines · Qi Zheng
Quoted 59 lines · Muchun Song
Quoted 59 lines · Qi Zheng
Quoted 53 lines · Muchun Song
Quoted 43 lines · Qi Zheng

In collapse_pte_mapped_thp(), we may modify the pte and pmd entry after acquring the ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the PTL held. So we should get pgt_pmd and do pmd_same() check after the ptl held.

---
mm/khugepaged.c | 14 +++++++++++---

1 file changed, 11 insertions(+), 3 deletions(-)
mm/khugepaged.c +4 -2
index 6498721d4783a..8ab79c13d077f 100644
@@ -1605,7 +1605,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
   if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED))
       pml = pmd_lock(mm, pmd);
 
-    start_pte = pte_offset_map_nolock(mm, pmd, haddr, &ptl);
+    start_pte = pte_offset_map_rw_nolock(mm, pmd, haddr, &pgt_pmd, &ptl);
   if (!start_pte)        /* mmap_lock + page lock should prevent this */
       goto abort;
   if (!pml)
@@ -1613,6 +1613,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
   else if (ptl != pml)
       spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
 
+    if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd))))
+        goto abort;
+
   /* step 2: clear page table and adjust rmap */
   for (i = 0, addr = haddr, pte = start_pte;
        i < HPAGE_PMD_NR; i++, addr += PAGE_SIZE, pte++) {
@@ -1645,7 +1648,6 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
       nr_ptes++;
   }
 
-    pte_unmap(start_pte);
   if (!pml)
       spin_unlock(ptl);
 

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1667

Quoted 9 lines
mm/khugepaged.c +4 -1
@@ -1658,13 +1660,19 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr,
   /* step 4: remove empty page table */
   if (!pml) {
       pml = pmd_lock(mm, pmd);
-        if (ptl != pml)
+        if (ptl != pml) {
           spin_lock_nested(ptl, SINGLE_DEPTH_NESTING);
+            if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
+                spin_unlock(pml);
+                goto abort;

Drop the reference of folio and the mm counter twice at the label of abort and the step 3.

Quoted 2 lines
Quoted 1 line

My bad, should set nr_ptes to 0 and call flush_tlb_mm() here, right?

Or add a new label "out" just below the "abort". Then go to out.

Quoted 8 lines

For this way, we also need to call flush_tlb_mm() first, like the following:

if (unlikely(!pmd_same(pgt_pmd, pmdp_get_lockless(pmd)))) {
	spin_unlock(pml);
	flush_tlb_mm(mm);
	goto out;
}

Fine.

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 11 lines

mm/khugepaged.c int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, · line 1676

Quoted 11 lines
mm/khugepaged.c +3 -0
+            }
+        }
   }
   pgt_pmd = pmdp_collapse_flush(vma, haddr, pmd);
   pmdp_get_lockless_sync();
   if (ptl != pml)
       spin_unlock(ptl);
+    pte_unmap(start_pte);
   spin_unlock(pml);
Quoted 4 lines

Why not?
pte_unmap_unlock(start_pte, ptl);
if (pml != ptl)

        spin_unlock(pml);

Both are fine, will do.

Thanks,
Qi

Quoted 4 lines
Quoted 4 lines
mm/khugepaged.c +0 -1
   mmu_notifier_invalidate_range_end(&range);
--

2.20.1

author
#15 lore raw

PATCH v4 8/13 mm: copy_pte_range() use pte_offset_map_rw_nolock()

In reply to Qi Zheng

In copy_pte_range(), we may modify the src_pte entry after holding the src_ptl, so convert it to using pte_offset_map_rw_nolock(). Since we already hold the exclusive mmap_lock, and the copy_pte_range() and retract_page_tables() are using vma->anon_vma to be exclusive, so the PTE page is stable, there is no need to get pmdval and do pmd_same() check.

---
 mm/memory.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
mm/memory.c +10 -1
index 6432b636d1ba7..c19cf14e1c565 100644
@@ -1086,6 +1086,7 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
 	struct mm_struct *src_mm = src_vma->vm_mm;
 	pte_t *orig_src_pte, *orig_dst_pte;
 	pte_t *src_pte, *dst_pte;
+	pmd_t dummy_pmdval;
 	pte_t ptent;
 	spinlock_t *src_ptl, *dst_ptl;
 	int progress, max_nr, ret = 0;
@@ -1111,7 +1112,15 @@ copy_pte_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma,
 		ret = -ENOMEM;
 		goto out;
 	}
-	src_pte = pte_offset_map_nolock(src_mm, src_pmd, addr, &src_ptl);
+
+	/*
+	 * We already hold the exclusive mmap_lock, the copy_pte_range() and
+	 * retract_page_tables() are using vma->anon_vma to be exclusive, so
+	 * the PTE page is stable, and there is no need to get pmdval and do
+	 * pmd_same() check.
+	 */
+	src_pte = pte_offset_map_rw_nolock(src_mm, src_pmd, addr, &dummy_pmdval,
+					   &src_ptl);
 	if (!src_pte) {
 		pte_unmap_unlock(dst_pte, dst_ptl);
 		/* ret == 0 */
Signature · 1 line
2.20.1
Reviewed-by on v4 8/13
#16 lore raw

Re: [PATCH v4 08/13] mm: copy_pte_range() use pte_offset_map_rw_nolock()

Quoted 11 lines · Qi Zheng

In copy_pte_range(), we may modify the src_pte entry after holding the src_ptl, so convert it to using pte_offset_map_rw_nolock(). Since we already hold the exclusive mmap_lock, and the copy_pte_range() and retract_page_tables() are using vma->anon_vma to be exclusive, so the PTE page is stable, there is no need to get pmdval and do pmd_same() check.

author
#17 lore raw

PATCH v4 9/13 mm: mremap: move_ptes() use pte_offset_map_rw_nolock()

In reply to Qi Zheng

In move_ptes(), we may modify the new_pte after acquiring the new_ptl, so convert it to using pte_offset_map_rw_nolock(). Now new_pte is none, so hpage_collapse_scan_file() path can not find this by traversing file->f_mapping, so there is no concurrency with retract_page_tables(). In addition, we already hold the exclusive mmap_lock, so this new_pte page is stable, so there is no need to get pmdval and do pmd_same() check.

---
 mm/mremap.c | 11 ++++++++++-
 1 file changed, 10 insertions(+), 1 deletion(-)
mm/mremap.c +10 -1
index 24712f8dbb6b5..9dffd4a5b4d18 100644
@@ -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 dummy_pmdval;
 	int err = 0;
 
 	/*
@@ -175,7 +176,15 @@ 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);
+	/*
+	 * Now new_pte is none, so hpage_collapse_scan_file() path can not find
+	 * this by traversing file->f_mapping, so there is no concurrency with
+	 * retract_page_tables(). In addition, we already hold the exclusive
+	 * mmap_lock, so this new_pte page is stable, so there is no need to get
+	 * pmdval and do pmd_same() check.
+	 */
+	new_pte = pte_offset_map_rw_nolock(mm, new_pmd, new_addr, &dummy_pmdval,
+					   &new_ptl);
 	if (!new_pte) {
 		pte_unmap_unlock(old_pte, old_ptl);
 		err = -EAGAIN;
Signature · 1 line
2.20.1
Reviewed-by on v4 9/13
#18 lore raw

Re: [PATCH v4 09/13] mm: mremap: move_ptes() use pte_offset_map_rw_nolock()

Quoted 12 lines · Qi Zheng

In move_ptes(), we may modify the new_pte after acquiring the new_ptl, so convert it to using pte_offset_map_rw_nolock(). Now new_pte is none, so hpage_collapse_scan_file() path can not find this by traversing file->f_mapping, so there is no concurrency with retract_page_tables(). In addition, we already hold the exclusive mmap_lock, so this new_pte page is stable, so there is no need to get pmdval and do pmd_same() check.

author
#19 lore raw

PATCH v4 10/13 mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock()

In reply to Qi Zheng

In the caller of map_pte(), we may modify the pvmw->pte after acquiring the pvmw->ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the pvmw->ptl held, so we should get pmdval and do pmd_same() check to ensure the stability of pvmw->pmd.

---
 mm/page_vma_mapped.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)
mm/page_vma_mapped.c +20 -5
index ae5cc42aa2087..6410f29b37c1b 100644
@@ -13,9 +13,11 @@ static inline bool not_found(struct page_vma_mapped_walk *pvmw)
 	return false;
 }
 
-static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
+static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
+		    spinlock_t **ptlp)
 {
 	pte_t ptent;
+	pmd_t pmdval;
 
 	if (pvmw->flags & PVMW_SYNC) {
 		/* Use the stricter lookup */
@@ -25,6 +27,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
 		return !!pvmw->pte;
 	}
 
+again:
 	/*
 	 * It is important to return the ptl corresponding to pte,
 	 * in case *pvmw->pmd changes underneath us; so we need to
@@ -32,10 +35,11 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
 	 * proceeds to loop over next ptes, and finds a match later.
 	 * Though, in most cases, page lock already protects this.
 	 */
-	pvmw->pte = pte_offset_map_nolock(pvmw->vma->vm_mm, pvmw->pmd,
-					  pvmw->address, ptlp);
+	pvmw->pte = pte_offset_map_rw_nolock(pvmw->vma->vm_mm, pvmw->pmd,
+					     pvmw->address, &pmdval, ptlp);
 	if (!pvmw->pte)
 		return false;
+	*pmdvalp = pmdval;
 
 	ptent = ptep_get(pvmw->pte);
 
@@ -67,8 +71,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
 	} else if (!pte_present(ptent)) {
 		return false;
 	}
+	spin_lock(*ptlp);
+	if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pvmw->pmd)))) {
+		pte_unmap_unlock(pvmw->pte, *ptlp);
+		goto again;
+	}
 	pvmw->ptl = *ptlp;
-	spin_lock(pvmw->ptl);
+
 	return true;
 }
 
@@ -278,7 +287,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
 			step_forward(pvmw, PMD_SIZE);
 			continue;
 		}
-		if (!map_pte(pvmw, &ptl)) {
+		if (!map_pte(pvmw, &pmde, &ptl)) {
 			if (!pvmw->pte)
 				goto restart;
 			goto next_pte;
@@ -307,6 +316,12 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
 		if (!pvmw->ptl) {
 			pvmw->ptl = ptl;
 			spin_lock(pvmw->ptl);
+			if (unlikely(!pmd_same(pmde, pmdp_get_lockless(pvmw->pmd)))) {
+				pte_unmap_unlock(pvmw->pte, pvmw->ptl);
+				pvmw->ptl = NULL;
+				pvmw->pte = NULL;
+				goto restart;
+			}
 		}
 		goto this_pte;
 	} while (pvmw->address < end);
Signature · 1 line
2.20.1
#20 lore raw

Re: [PATCH v4 10/13] mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock()

Quoted 20 lines · Qi Zheng

In the caller of map_pte(), we may modify the pvmw->pte after acquiring the pvmw->ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the pvmw->ptl held, so we should get pmdval and do pmd_same() check to ensure the stability of pvmw->pmd.

---
mm/page_vma_mapped.c | 25 ++++++++++++++++++++-----

1 file changed, 20 insertions(+), 5 deletions(-)
mm/page_vma_mapped.c +0 -0
index ae5cc42aa2087..6410f29b37c1b 100644

mm/page_vma_mapped.c static inline bool not_found(struct page_vma_mapped_walk *pvmw) · line 20

Quoted 9 lines
mm/page_vma_mapped.c +3 -1
@@ -13,9 +13,11 @@ static inline bool not_found(struct page_vma_mapped_walk *pvmw)
   return false;
 }
 
-static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
+static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
+            spinlock_t **ptlp)
 {
   pte_t ptent;
+    pmd_t pmdval;

Why declare a new variable? Can’t we use *pmdvalp instead?

Quoted 65 lines
mm/page_vma_mapped.c +17 -5
   if (pvmw->flags & PVMW_SYNC) {
       /* Use the stricter lookup */
@@ -25,6 +27,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
       return !!pvmw->pte;
   }
 
+again:
   /*
    * It is important to return the ptl corresponding to pte,
    * in case *pvmw->pmd changes underneath us; so we need to
@@ -32,10 +35,11 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
    * proceeds to loop over next ptes, and finds a match later.
    * Though, in most cases, page lock already protects this.
    */
-    pvmw->pte = pte_offset_map_nolock(pvmw->vma->vm_mm, pvmw->pmd,
-                      pvmw->address, ptlp);
+    pvmw->pte = pte_offset_map_rw_nolock(pvmw->vma->vm_mm, pvmw->pmd,
+                         pvmw->address, &pmdval, ptlp);
   if (!pvmw->pte)
       return false;
+    *pmdvalp = pmdval;
 
   ptent = ptep_get(pvmw->pte);
 
@@ -67,8 +71,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
   } else if (!pte_present(ptent)) {
       return false;
   }
+    spin_lock(*ptlp);
+    if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pvmw->pmd)))) {
+        pte_unmap_unlock(pvmw->pte, *ptlp);
+        goto again;
+    }
   pvmw->ptl = *ptlp;
-    spin_lock(pvmw->ptl);
+
   return true;
 }
 
@@ -278,7 +287,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
           step_forward(pvmw, PMD_SIZE);
           continue;
       }
-        if (!map_pte(pvmw, &ptl)) {
+        if (!map_pte(pvmw, &pmde, &ptl)) {
           if (!pvmw->pte)
               goto restart;
           goto next_pte;
@@ -307,6 +316,12 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
       if (!pvmw->ptl) {
           pvmw->ptl = ptl;
           spin_lock(pvmw->ptl);
+            if (unlikely(!pmd_same(pmde, pmdp_get_lockless(pvmw->pmd)))) {
+                pte_unmap_unlock(pvmw->pte, pvmw->ptl);
+                pvmw->ptl = NULL;
+                pvmw->pte = NULL;
+                goto restart;
+            }
       }
       goto this_pte;
   } while (pvmw->address < end);
--

2.20.1

author
#21 lore raw

Re: [PATCH v4 10/13] mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock()

Quoted 23 lines · Muchun Song
Quoted 23 lines · Qi Zheng

In the caller of map_pte(), we may modify the pvmw->pte after acquiring the pvmw->ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the pvmw->ptl held, so we should get pmdval and do pmd_same() check to ensure the stability of pvmw->pmd.

---
mm/page_vma_mapped.c | 25 ++++++++++++++++++++-----

1 file changed, 20 insertions(+), 5 deletions(-)
mm/page_vma_mapped.c +0 -0
index ae5cc42aa2087..6410f29b37c1b 100644
@@ -13,9 +13,11 @@ static inline bool not_found(struct page_vma_mapped_walk *pvmw)
    return false;
 }
 
Quoted 8 lines

mm/page_vma_mapped.c static inline bool not_found(struct page_vma_mapped_walk *pvmw) · line 20

Quoted 6 lines
mm/page_vma_mapped.c +3 -1
-static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
+static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
+            spinlock_t **ptlp)
 {
    pte_t ptent;
+    pmd_t pmdval;

Why declare a new variable? Can’t we use *pmdvalp instead?

It's just a coding habit, both are fine for me.

Quoted 66 lines
Quoted 66 lines
mm/page_vma_mapped.c +17 -5
    if (pvmw->flags & PVMW_SYNC) {
        /* Use the stricter lookup */
@@ -25,6 +27,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
        return !!pvmw->pte;
    }
 
+again:
    /*
     * It is important to return the ptl corresponding to pte,
     * in case *pvmw->pmd changes underneath us; so we need to
@@ -32,10 +35,11 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
     * proceeds to loop over next ptes, and finds a match later.
     * Though, in most cases, page lock already protects this.
     */
-    pvmw->pte = pte_offset_map_nolock(pvmw->vma->vm_mm, pvmw->pmd,
-                      pvmw->address, ptlp);
+    pvmw->pte = pte_offset_map_rw_nolock(pvmw->vma->vm_mm, pvmw->pmd,
+                         pvmw->address, &pmdval, ptlp);
    if (!pvmw->pte)
        return false;
+    *pmdvalp = pmdval;
 
    ptent = ptep_get(pvmw->pte);
 
@@ -67,8 +71,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
    } else if (!pte_present(ptent)) {
        return false;
    }
+    spin_lock(*ptlp);
+    if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pvmw->pmd)))) {
+        pte_unmap_unlock(pvmw->pte, *ptlp);
+        goto again;
+    }
    pvmw->ptl = *ptlp;
-    spin_lock(pvmw->ptl);
+
    return true;
 }
 
@@ -278,7 +287,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
            step_forward(pvmw, PMD_SIZE);
            continue;
        }
-        if (!map_pte(pvmw, &ptl)) {
+        if (!map_pte(pvmw, &pmde, &ptl)) {
            if (!pvmw->pte)
                goto restart;
            goto next_pte;
@@ -307,6 +316,12 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
        if (!pvmw->ptl) {
            pvmw->ptl = ptl;
            spin_lock(pvmw->ptl);
+            if (unlikely(!pmd_same(pmde, pmdp_get_lockless(pvmw->pmd)))) {
+                pte_unmap_unlock(pvmw->pte, pvmw->ptl);
+                pvmw->ptl = NULL;
+                pvmw->pte = NULL;
+                goto restart;
+            }
        }
        goto this_pte;
    } while (pvmw->address < end);
--

2.20.1

#22 lore raw

Re: [PATCH v4 10/13] mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock()

Quoted 36 lines · Qi Zheng
Quoted 21 lines · Muchun Song
Quoted 21 lines · Qi Zheng

In the caller of map_pte(), we may modify the pvmw->pte after acquiring the pvmw->ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the pvmw->ptl held, so we should get pmdval and do pmd_same() check to ensure the stability of pvmw->pmd.

---
mm/page_vma_mapped.c | 25 ++++++++++++++++++++-----

1 file changed, 20 insertions(+), 5 deletions(-)
mm/page_vma_mapped.c +0 -0
index ae5cc42aa2087..6410f29b37c1b 100644
@@ -13,9 +13,11 @@ static inline bool not_found(struct page_vma_mapped_walk *pvmw)
   return false;
 }
 
Quoted 7 lines

mm/page_vma_mapped.c static inline bool not_found(struct page_vma_mapped_walk *pvmw) · line 20

Quoted 6 lines
mm/page_vma_mapped.c +3 -1
-static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
+static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
+            spinlock_t **ptlp)
 {
   pte_t ptent;
+    pmd_t pmdval;

Why declare a new variable? Can’t we use *pmdvalp instead?

Quoted 1 line

It's just a coding habit, both are fine for me.

Agree. But sometime it could make code look a little simpler.

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
mm/page_vma_mapped.c +0 -0
   if (pvmw->flags & PVMW_SYNC) {
       /* Use the stricter lookup */
@@ -25,6 +27,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
       return !!pvmw->pte;
   }
 
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
mm/page_vma_mapped.c +1 -0
+again:
   /*
    * It is important to return the ptl corresponding to pte,
    * in case *pvmw->pmd changes underneath us; so we need to
@@ -32,10 +35,11 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
    * proceeds to loop over next ptes, and finds a match later.
    * Though, in most cases, page lock already protects this.
Quoted 8 lines

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp) · line 42

Quoted 8 lines
mm/page_vma_mapped.c +3 -2
    */
-    pvmw->pte = pte_offset_map_nolock(pvmw->vma->vm_mm, pvmw->pmd,
-                      pvmw->address, ptlp);
+    pvmw->pte = pte_offset_map_rw_nolock(pvmw->vma->vm_mm, pvmw->pmd,
+                         pvmw->address, &pmdval, ptlp);
   if (!pvmw->pte)
       return false;
+    *pmdvalp = pmdval;

For instance, here, it is unnecessary if pmdvalp is passed directly to pte_offset_map_rw_nolock.

Quoted 42 lines
Quoted 42 lines
Quoted 42 lines
mm/page_vma_mapped.c +13 -3
   ptent = ptep_get(pvmw->pte);
 
@@ -67,8 +71,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
   } else if (!pte_present(ptent)) {
       return false;
   }
+    spin_lock(*ptlp);
+    if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pvmw->pmd)))) {
+        pte_unmap_unlock(pvmw->pte, *ptlp);
+        goto again;
+    }
   pvmw->ptl = *ptlp;
-    spin_lock(pvmw->ptl);
+
   return true;
 }
 
@@ -278,7 +287,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
           step_forward(pvmw, PMD_SIZE);
           continue;
       }
-        if (!map_pte(pvmw, &ptl)) {
+        if (!map_pte(pvmw, &pmde, &ptl)) {
           if (!pvmw->pte)
               goto restart;
           goto next_pte;
@@ -307,6 +316,12 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
       if (!pvmw->ptl) {
           pvmw->ptl = ptl;
           spin_lock(pvmw->ptl);
+            if (unlikely(!pmd_same(pmde, pmdp_get_lockless(pvmw->pmd)))) {
+                pte_unmap_unlock(pvmw->pte, pvmw->ptl);
+                pvmw->ptl = NULL;
+                pvmw->pte = NULL;
+                goto restart;
+            }
       }
       goto this_pte;
   } while (pvmw->address < end);
--

2.20.1

author
#23 lore raw

Re: [PATCH v4 10/13] mm: page_vma_mapped_walk: map_pte() use pte_offset_map_rw_nolock()

Quoted 58 lines · Muchun Song
Quoted 36 lines · Qi Zheng
Quoted 21 lines · Muchun Song
Quoted 21 lines · Qi Zheng

In the caller of map_pte(), we may modify the pvmw->pte after acquiring the pvmw->ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the pvmw->ptl held, so we should get pmdval and do pmd_same() check to ensure the stability of pvmw->pmd.

---
mm/page_vma_mapped.c | 25 ++++++++++++++++++++-----

1 file changed, 20 insertions(+), 5 deletions(-)
mm/page_vma_mapped.c +0 -0
index ae5cc42aa2087..6410f29b37c1b 100644
@@ -13,9 +13,11 @@ static inline bool not_found(struct page_vma_mapped_walk *pvmw)
    return false;
 }
 
Quoted 7 lines

mm/page_vma_mapped.c static inline bool not_found(struct page_vma_mapped_walk *pvmw) · line 20

Quoted 6 lines
mm/page_vma_mapped.c +3 -1
-static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
+static bool map_pte(struct page_vma_mapped_walk *pvmw, pmd_t *pmdvalp,
+            spinlock_t **ptlp)
 {
    pte_t ptent;
+    pmd_t pmdval;

Why declare a new variable? Can’t we use *pmdvalp instead?

Quoted 1 line

It's just a coding habit, both are fine for me.

Agree. But sometime it could make code look a little simpler.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
mm/page_vma_mapped.c +1 -2
    if (pvmw->flags & PVMW_SYNC) {
        /* Use the stricter lookup */
@@ -25,6 +27,7 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
        return !!pvmw->pte;
    }
 
+again:
    /*
     * It is important to return the ptl corresponding to pte,
     * in case *pvmw->pmd changes underneath us; so we need to
@@ -32,10 +35,11 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
     * proceeds to loop over next ptes, and finds a match later.
     * Though, in most cases, page lock already protects this.
     */
-    pvmw->pte = pte_offset_map_nolock(pvmw->vma->vm_mm, pvmw->pmd,
-                      pvmw->address, ptlp);
Quoted 8 lines
Quoted 5 lines
Quoted 5 lines

mm/page_vma_mapped.c static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp) · line 42

Quoted 5 lines
mm/page_vma_mapped.c +3 -0
+    pvmw->pte = pte_offset_map_rw_nolock(pvmw->vma->vm_mm, pvmw->pmd,
+                         pvmw->address, &pmdval, ptlp);
    if (!pvmw->pte)
        return false;
+    *pmdvalp = pmdval;

For instance, here, it is unnecessary if pmdvalp is passed directly to pte_offset_map_rw_nolock.

OK, will use pmdvalp directly. ;)

Quoted 45 lines
Quoted 43 lines
Quoted 43 lines
Quoted 43 lines
mm/page_vma_mapped.c +13 -3
    ptent = ptep_get(pvmw->pte);
 
@@ -67,8 +71,13 @@ static bool map_pte(struct page_vma_mapped_walk *pvmw, spinlock_t **ptlp)
    } else if (!pte_present(ptent)) {
        return false;
    }
+    spin_lock(*ptlp);
+    if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pvmw->pmd)))) {
+        pte_unmap_unlock(pvmw->pte, *ptlp);
+        goto again;
+    }
    pvmw->ptl = *ptlp;
-    spin_lock(pvmw->ptl);
+
    return true;
 }
 
@@ -278,7 +287,7 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
            step_forward(pvmw, PMD_SIZE);
            continue;
        }
-        if (!map_pte(pvmw, &ptl)) {
+        if (!map_pte(pvmw, &pmde, &ptl)) {
            if (!pvmw->pte)
                goto restart;
            goto next_pte;
@@ -307,6 +316,12 @@ bool page_vma_mapped_walk(struct page_vma_mapped_walk *pvmw)
        if (!pvmw->ptl) {
            pvmw->ptl = ptl;
            spin_lock(pvmw->ptl);
+            if (unlikely(!pmd_same(pmde, pmdp_get_lockless(pvmw->pmd)))) {
+                pte_unmap_unlock(pvmw->pte, pvmw->ptl);
+                pvmw->ptl = NULL;
+                pvmw->pte = NULL;
+                goto restart;
+            }
        }
        goto this_pte;
    } while (pvmw->address < end);
--

2.20.1

author
#24 lore raw

PATCH v4 11/13 mm: userfaultfd: move_pages_pte() use pte_offset_map_rw_nolock()

In reply to Qi Zheng

In move_pages_pte(), we may modify the dst_pte and src_pte after acquiring the ptl, so convert it to using pte_offset_map_rw_nolock(). But since we will use pte_same() to detect the change of the pte entry, there is no need to get pmdval, so just pass a dummy variable to it.

---
 mm/userfaultfd.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
mm/userfaultfd.c +12 -3
index ce13c40626472..48b87c62fc3dd 100644
@@ -1135,7 +1135,7 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd,
 	spinlock_t *src_ptl, *dst_ptl;
 	pte_t *src_pte = NULL;
 	pte_t *dst_pte = NULL;
-
+	pmd_t dummy_pmdval;
 	struct folio *src_folio = NULL;
 	struct anon_vma *src_anon_vma = NULL;
 	struct mmu_notifier_range range;
@@ -1146,7 +1146,14 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd,
 				src_addr, src_addr + PAGE_SIZE);
 	mmu_notifier_invalidate_range_start(&range);
 retry:
-	dst_pte = pte_offset_map_nolock(mm, dst_pmd, dst_addr, &dst_ptl);
+	/*
+	 * Use the maywrite version to indicate that dst_pte will be modified,
+	 * but since we will use pte_same() to detect the change of the pte
+	 * entry, there is no need to get pmdval, so just pass a dummy variable
+	 * to it.
+	 */
+	dst_pte = pte_offset_map_rw_nolock(mm, dst_pmd, dst_addr, &dummy_pmdval,
+					   &dst_ptl);
 
 	/* Retry if a huge pmd materialized from under us */
 	if (unlikely(!dst_pte)) {
@@ -1154,7 +1161,9 @@ static int move_pages_pte(struct mm_struct *mm, pmd_t *dst_pmd, pmd_t *src_pmd,
 		goto out;
 	}
 
-	src_pte = pte_offset_map_nolock(mm, src_pmd, src_addr, &src_ptl);
+	/* same as dst_pte */
+	src_pte = pte_offset_map_rw_nolock(mm, src_pmd, src_addr, &dummy_pmdval,
+					   &src_ptl);
 
 	/*
 	 * We held the mmap_lock for reading so MADV_DONTNEED
Signature · 1 line
2.20.1
author
#25 lore raw

PATCH v4 12/13 mm: multi-gen LRU: walk_pte_range() use pte_offset_map_rw_nolock()

In reply to Qi Zheng

In walk_pte_range(), we may modify the pte entry after holding the ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the ptl held, so we should get pmdval and do pmd_same() check to ensure the stability of pmd entry.

---
 mm/vmscan.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
mm/vmscan.c +8 -1
index 749cdc110c745..bdca94e663bc5 100644
@@ -3375,8 +3375,10 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
 	struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
 	DEFINE_MAX_SEQ(walk->lruvec);
 	int old_gen, new_gen = lru_gen_from_seq(max_seq);
+	pmd_t pmdval;
 
-	pte = pte_offset_map_nolock(args->mm, pmd, start & PMD_MASK, &ptl);
+	pte = pte_offset_map_rw_nolock(args->mm, pmd, start & PMD_MASK, &pmdval,
+				       &ptl);
 	if (!pte)
 		return false;
 	if (!spin_trylock(ptl)) {
@@ -3384,6 +3386,11 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
 		return false;
 	}
 
+	if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pmd)))) {
+		pte_unmap_unlock(pte, ptl);
+		return false;
+	}
+
 	arch_enter_lazy_mmu_mode();
 restart:
 	for (i = pte_index(start), addr = start; addr != end; i++, addr += PAGE_SIZE) {
Signature · 1 line
2.20.1
Acked-by on v4 12/13
#26 lore raw

Re: [PATCH v4 12/13] mm: multi-gen LRU: walk_pte_range() use pte_offset_map_rw_nolock()

Quoted 28 lines · Qi Zheng

In walk_pte_range(), we may modify the pte entry after holding the ptl, so convert it to using pte_offset_map_rw_nolock(). At this time, the pte_same() check is not performed after the ptl held, so we should get pmdval and do pmd_same() check to ensure the stability of pmd entry.

---
  mm/vmscan.c | 9 ++++++++-
  1 file changed, 8 insertions(+), 1 deletion(-)
mm/vmscan.c +3 -1
index 749cdc110c745..bdca94e663bc5 100644
@@ -3375,8 +3375,10 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
  	struct pglist_data *pgdat = lruvec_pgdat(walk->lruvec);
  	DEFINE_MAX_SEQ(walk->lruvec);
  	int old_gen, new_gen = lru_gen_from_seq(max_seq);
+	pmd_t pmdval;
 
-	pte = pte_offset_map_nolock(args->mm, pmd, start & PMD_MASK, &ptl);
+	pte = pte_offset_map_rw_nolock(args->mm, pmd, start & PMD_MASK, &pmdval,
+				       &ptl);
  	if (!pte)
  		return false;
  	if (!spin_trylock(ptl)) {

mm/vmscan.c static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end, · line 3392

Quoted 7 lines
mm/vmscan.c +4 -0
@@ -3384,6 +3386,11 @@ static bool walk_pte_range(pmd_t *pmd, unsigned long start, unsigned long end,
  		return false;
  	}
 
+	if (unlikely(!pmd_same(pmdval, pmdp_get_lockless(pmd)))) {
+		pte_unmap_unlock(pte, ptl);
+		return false;
+	}
Signature · 3 lines
Cheers,

David / dhildenb
author
#27 lore raw

PATCH v4 13/13 mm: pgtable: remove pte_offset_map_nolock()

In reply to Qi Zheng

Now no users are using the pte_offset_map_nolock(), remove it.

---
 Documentation/mm/split_page_table_lock.rst |  3 ---
 include/linux/mm.h                         |  2 --
 mm/pgtable-generic.c                       | 21 ---------------------
 3 files changed, 26 deletions(-)
Documentation/mm/split_page_table_lock.rst +0 -3
index 08d0e706a32db..581446d4a4eba 100644
@@ -16,9 +16,6 @@ There are helpers to lock/unlock a table and other accessor functions:
  - pte_offset_map_lock()
 	maps PTE and takes PTE table lock, returns pointer to PTE with
 	pointer to its PTE table lock, or returns NULL if no PTE table;
- - pte_offset_map_nolock()
-	maps PTE, returns pointer to PTE with pointer to its PTE table
-	lock (not taken), or returns NULL if no PTE table;
  - pte_offset_map_ro_nolock()
 	maps PTE, returns pointer to PTE with pointer to its PTE table
 	lock (not taken), or returns NULL if no PTE table;
include/linux/mm.h +0 -2
index 9a4550cd830c9..e2a4502ab019b 100644
@@ -3015,8 +3015,6 @@ static inline pte_t *pte_offset_map_lock(struct mm_struct *mm, pmd_t *pmd,
 	return pte;
 }
 
-pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd,
-			unsigned long addr, spinlock_t **ptlp);
 pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
 				unsigned long addr, spinlock_t **ptlp);
 pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd,
mm/pgtable-generic.c +0 -21
index 262b7065a5a2e..c68aa655b7872 100644
@@ -305,18 +305,6 @@ pte_t *__pte_offset_map(pmd_t *pmd, unsigned long addr, pmd_t *pmdvalp)
 	return NULL;
 }
 
-pte_t *pte_offset_map_nolock(struct mm_struct *mm, pmd_t *pmd,
-			     unsigned long addr, spinlock_t **ptlp)
-{
-	pmd_t pmdval;
-	pte_t *pte;
-
-	pte = __pte_offset_map(pmd, addr, &pmdval);
-	if (likely(pte))
-		*ptlp = pte_lockptr(mm, &pmdval);
-	return pte;
-}
-
 pte_t *pte_offset_map_ro_nolock(struct mm_struct *mm, pmd_t *pmd,
 				unsigned long addr, spinlock_t **ptlp)
 {
@@ -374,15 +362,6 @@ pte_t *pte_offset_map_rw_nolock(struct mm_struct *mm, pmd_t *pmd,
  * and disconnected table.  Until pte_unmap(pte) unmaps and rcu_read_unlock()s
  * afterwards.
  *
- * pte_offset_map_nolock(mm, pmd, addr, ptlp), above, is like pte_offset_map();
- * but when successful, it also outputs a pointer to the spinlock in ptlp - as
- * pte_offset_map_lock() does, but in this case without locking it.  This helps
- * the caller to avoid a later pte_lockptr(mm, *pmd), which might by that time
- * act on a changed *pmd: pte_offset_map_nolock() provides the correct spinlock
- * pointer for the page table that it returns.  In principle, the caller should
- * recheck *pmd once the lock is taken; in practice, no callsite needs that -
- * either the mmap_lock for write, or pte_same() check on contents, is enough.
- *
  * pte_offset_map_ro_nolock(mm, pmd, addr, ptlp), above, is like pte_offset_map();
  * but when successful, it also outputs a pointer to the spinlock in ptlp - as
  * pte_offset_map_lock() does, but in this case without locking it.  This helps
Signature · 1 line
2.20.1