Skip to contentSkip to conversation

mprotect: drop overprotective lock_pte_protection()

PATCH In review Kirill A. Shutemov 4 participants 5 messages Feb 7, 2017 – Feb 8, 2017

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mprotect: drop overprotective lock_pte_protection()

lock_pte_protection() uses pmd_lock() to make sure that we have stable PTE page table before walking pte range.

That's not necessary. We only need to make sure that PTE page table is established. It cannot vanish under us as long as we hold mmap_sem at least for read.

And we already have helper for that -- pmd_trans_unstable().

---
 mm/mprotect.c | 43 ++++++++++++-------------------------------
 1 file changed, 12 insertions(+), 31 deletions(-)
mm/mprotect.c +12 -31
index f9c07f54dd62..e919e4613eab 100644
@@ -33,34 +33,6 @@
 
 #include "internal.h"
 
-/*
- * For a prot_numa update we only hold mmap_sem for read so there is a
- * potential race with faulting where a pmd was temporarily none. This
- * function checks for a transhuge pmd under the appropriate lock. It
- * returns a pte if it was successfully locked or NULL if it raced with
- * a transhuge insertion.
- */
-static pte_t *lock_pte_protection(struct vm_area_struct *vma, pmd_t *pmd,
-			unsigned long addr, int prot_numa, spinlock_t **ptl)
-{
-	pte_t *pte;
-	spinlock_t *pmdl;
-
-	/* !prot_numa is protected by mmap_sem held for write */
-	if (!prot_numa)
-		return pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
-
-	pmdl = pmd_lock(vma->vm_mm, pmd);
-	if (unlikely(pmd_trans_huge(*pmd) || pmd_none(*pmd))) {
-		spin_unlock(pmdl);
-		return NULL;
-	}
-
-	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
-	spin_unlock(pmdl);
-	return pte;
-}
-
 static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 		unsigned long addr, unsigned long end, pgprot_t newprot,
 		int dirty_accountable, int prot_numa)
@@ -71,7 +43,7 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 	unsigned long pages = 0;
 	int target_node = NUMA_NO_NODE;
 
-	pte = lock_pte_protection(vma, pmd, addr, prot_numa, &ptl);
+	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
 	if (!pte)
 		return 0;
 
@@ -177,8 +149,6 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
 		if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
 			if (next - addr != HPAGE_PMD_SIZE) {
 				__split_huge_pmd(vma, pmd, addr, false, NULL);
-				if (pmd_trans_unstable(pmd))
-					continue;
 			} else {
 				int nr_ptes = change_huge_pmd(vma, pmd, addr,
 						newprot, prot_numa);
@@ -195,6 +165,17 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
 			}
 			/* fall through, the trans huge pmd just split */
 		}
+
+		/*
+		 * For prot_numa update we only hold mmap_sem for read so there
+		 * is a potential race with faulting where a pmd was
+		 * temporarily none.
+		 * Make sure we have PTE page table, before moving forward.
+		 * Page tables cannot go away under us as long as we hold
+		 * mmap_sem at least for read.
+		 */
+		if (pmd_trans_unstable(pmd))
+			continue;
 		this_pages = change_pte_range(vma, pmd, addr, next, newprot,
 				 dirty_accountable, prot_numa);
 		pages += this_pages;
Signature · 7 lines
2.11.0

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw
Quoted 54 lines · Kirill A. Shutemov

lock_pte_protection() uses pmd_lock() to make sure that we have stable PTE page table before walking pte range.

That's not necessary. We only need to make sure that PTE page table is established. It cannot vanish under us as long as we hold mmap_sem at least for read.

And we already have helper for that -- pmd_trans_unstable().

---
 mm/mprotect.c | 43 ++++++++++++-------------------------------
 1 file changed, 12 insertions(+), 31 deletions(-)
mm/mprotect.c +0 -28
index f9c07f54dd62..e919e4613eab 100644
@@ -33,34 +33,6 @@
 
 #include "internal.h"
 
-/*
- * For a prot_numa update we only hold mmap_sem for read so there is a
- * potential race with faulting where a pmd was temporarily none. This
- * function checks for a transhuge pmd under the appropriate lock. It
- * returns a pte if it was successfully locked or NULL if it raced with
- * a transhuge insertion.
- */
-static pte_t *lock_pte_protection(struct vm_area_struct *vma, pmd_t *pmd,
-			unsigned long addr, int prot_numa, spinlock_t **ptl)
-{
-	pte_t *pte;
-	spinlock_t *pmdl;
-
-	/* !prot_numa is protected by mmap_sem held for write */
-	if (!prot_numa)
-		return pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
-
-	pmdl = pmd_lock(vma->vm_mm, pmd);
-	if (unlikely(pmd_trans_huge(*pmd) || pmd_none(*pmd))) {
-		spin_unlock(pmdl);
-		return NULL;
-	}
-
-	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, ptl);
-	spin_unlock(pmdl);
-	return pte;
-}
-
 static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 		unsigned long addr, unsigned long end, pgprot_t newprot,
 		int dirty_accountable, int prot_numa)

mm/mprotect.c static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd, · line 47

Quoted 6 lines
mm/mprotect.c +1 -1
@@ -71,7 +43,7 @@ static unsigned long change_pte_range(struct vm_area_struct *vma, pmd_t *pmd,
 	unsigned long pages = 0;
 	int target_node = NUMA_NO_NODE;
 
-	pte = lock_pte_protection(vma, pmd, addr, prot_numa, &ptl);
+	pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
 	if (!pte)

I cleaned it up too but I moved the pmd_trans_unstable in the caller above instead of the callee, otherwise it's the same.

mm/mprotect.c static inline unsigned long change_pmd_range(struct vm_area_struct *vma, · line 181

Quoted 7 lines
mm/mprotect.c +0 -2
@@ -177,8 +149,6 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
 		if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
 			if (next - addr != HPAGE_PMD_SIZE) {
 				__split_huge_pmd(vma, pmd, addr, false, NULL);
-				if (pmd_trans_unstable(pmd))
-					continue;

Agree it can be removed too, but I only removed lock_pte_protection in my version.

If you prefer this version to be merged so we don't have to cleanup the above superfluous check in a incremental patch that's fine of course, otherwise at runtime they're equivalent as far as I can tell. The version in -mm is here.

https://git.kernel.org/cgit/linux/kernel/git/mhocko/mm.git/commit/?h=auto-latest&id=d84ff4e4985f397ca4ecfe7ec029c45c6f2b9906

Thanks,
Andrea

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

maintainer
#2 lore raw
In reply to Kirill A. Shutemov
Quoted 8 lines · On Tue, 7 Feb 2017 17:33:47 +0300 "Kirill A. Shutemov"…

lock_pte_protection() uses pmd_lock() to make sure that we have stable PTE page table before walking pte range.

That's not necessary. We only need to make sure that PTE page table is established. It cannot vanish under us as long as we hold mmap_sem at least for read.

And we already have helper for that -- pmd_trans_unstable().

http://ozlabs.org/~akpm/mmots/broken-out/mm-mprotect-use-pmd_trans_unstable-instead-of-taking-the-pmd_lock.patch
already did this?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw
Quoted 5 lines · Andrew Morton
Quoted 3 lines · On Tue, 7 Feb 2017 17:33:47 +0300 "Kirill A. Shutemov"…

lock_pte_protection() uses pmd_lock() to make sure that we have stable PTE page table before walking pte range.

Quoted 8 lines
Quoted 5 lines

That's not necessary. We only need to make sure that PTE page table is established. It cannot vanish under us as long as we hold mmap_sem at least for read.

And we already have helper for that -- pmd_trans_unstable().

http://ozlabs.org/~akpm/mmots/broken-out/mm-mprotect-use-pmd_trans_unstable-instead-of-taking-the-pmd_lock.patch
already did this?

Right. Except, it doesn't drop unneeded pmd_trans_unstable(pmd) check after __split_huge_pmd().

Could you fold this part of my patch into Andrea's?

mm/mprotect.c +0 -2
index f9c07f54dd62..e919e4613eab 100644
@@ -177,8 +149,6 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
 		if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
 			if (next - addr != HPAGE_PMD_SIZE) {
 				__split_huge_pmd(vma, pmd, addr, false, NULL);
-				if (pmd_trans_unstable(pmd))
-					continue;
 			} else {
 				int nr_ptes = change_huge_pmd(vma, pmd, addr,
 						newprot, prot_numa);
Signature · 7 lines
 Kirill A. Shutemov

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Reviewed-by
#4 lore raw
Quoted 12 lines · Kirill A. Shutemov
Quoted 11 lines · Andrew Morton
Quoted 8 lines · On Tue, 7 Feb 2017 17:33:47 +0300 "Kirill A. Shutemov"…

lock_pte_protection() uses pmd_lock() to make sure that we have stable PTE page table before walking pte range.

That's not necessary. We only need to make sure that PTE page table is established. It cannot vanish under us as long as we hold mmap_sem at least for read.

And we already have helper for that -- pmd_trans_unstable().

Quoted 7 lines
Quoted 2 lines

http://ozlabs.org/~akpm/mmots/broken-out/mm-mprotect-use-pmd_trans_unstable-instead-of-taking-the-pmd_lock.patch
already did this?

Right. Except, it doesn't drop unneeded pmd_trans_unstable(pmd) check after __split_huge_pmd().

Could you fold this part of my patch into Andrea's?

Quoted 6 lines
mm/mprotect.c +0 -0
index f9c07f54dd62..e919e4613eab 100644

mm/mprotect.c static inline unsigned long change_pmd_range(struct vm_area_struct *vma, · line 154

Quoted 8 lines
mm/mprotect.c +0 -2
@@ -177,8 +149,6 @@ static inline unsigned long change_pmd_range(struct vm_area_struct *vma,
 		if (pmd_trans_huge(*pmd) || pmd_devmap(*pmd)) {
 			if (next - addr != HPAGE_PMD_SIZE) {
 				__split_huge_pmd(vma, pmd, addr, false, NULL);
-				if (pmd_trans_unstable(pmd))
-					continue;
 			} else {
 				int nr_ptes = change_huge_pmd(vma, pmd, addr,
 						newprot, prot_numa);

Yes this check was an harmless noop, but it's definitely good to clean up this bit too after the other more important change that has a positive runtime effect, or it could be a source of confusion to the reader if left in there.

Thanks!
Andrea

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>