Skip to contentSkip to conversation

mm/mremap: cleanup move_page_tables() a little

PATCH In review Wei Yang 2 participants 7 messages Jan 29, 2020 – Jan 30, 2020

Subsystems (3)
lore
Share & tools
lore

Wei Yang

author
#0 lore raw

v2 cover mm/mremap: cleanup move_page_tables() a little

move_page_tables() tries to move page table by PMD or PTE.

The root reason is if it tries to move PMD, both old and new range should be PMD aligned. But current code calculate old range and new range separately. This leads to some redundant check and calculation.

This cleanup tries to consolidate the range check in one place to reduce some extra range handling.

v2:
* remove 3rd patch which doesn't work on ARM platform. Thanks report from Dmitry Osipenko

Wei Yang (4):
mm/mremap: format the check in move_normal_pmd() same as

    move_huge_pmd()

mm/mremap: it is sure to have enough space when extent meets
requirement
mm/mremap: calculate extent in one place mm/mremap: start addresses are properly aligned

 include/linux/huge_mm.h |  2 +-
 mm/huge_memory.c        |  8 +-------
 mm/mremap.c             | 17 ++++++-----------
 3 files changed, 8 insertions(+), 19 deletions(-)
Signature · 1 line
2.17.1
author
#1 lore raw

PATCH v2 1/4 mm/mremap: format the check in move_normal_pmd() same as move_huge_pmd()

No functional change, just improve the readability and prepare for following cleanup.

---
 mm/mremap.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/mremap.c +3 -2
index 122938dcec15..bcc7aa62f2d9 100644
@@ -200,8 +200,9 @@ static bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr,
 	struct mm_struct *mm = vma->vm_mm;
 	pmd_t pmd;
 
-	if ((old_addr & ~PMD_MASK) || (new_addr & ~PMD_MASK)
-	    || old_end - old_addr < PMD_SIZE)
+	if ((old_addr & ~PMD_MASK) ||
+	    (new_addr & ~PMD_MASK) ||
+	    old_end - old_addr < PMD_SIZE)
 		return false;
 
 	/*
Signature · 1 line
2.17.1
author
#2 lore raw

PATCH v2 2/4 mm/mremap: it is sure to have enough space when extent meets requirement

In reply to Wei Yang

old_end is passed to these two function to check whether there is enough space to do the move, while this check is done before invoking these functions.

These two functions only would be invoked when extent meets the requirement and there is one check before invoking these functions:

if (extent > old_end - old_addr)
extent = old_end - old_addr;

This implies (old_end - old_addr) won't fail the check in these two functions.

---
 include/linux/huge_mm.h |  2 +-
 mm/huge_memory.c        |  7 ++-----
 mm/mremap.c             | 11 ++++-------
 3 files changed, 7 insertions(+), 13 deletions(-)
include/linux/huge_mm.h +1 -1
index 0b84e13e88e2..2a5281ca46c8 100644
@@ -42,7 +42,7 @@ extern int mincore_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
 			unsigned long addr, unsigned long end,
 			unsigned char *vec);
 extern bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
-			 unsigned long new_addr, unsigned long old_end,
+			 unsigned long new_addr,
 			 pmd_t *old_pmd, pmd_t *new_pmd);
 extern int change_huge_pmd(struct vm_area_struct *vma, pmd_t *pmd,
 			unsigned long addr, pgprot_t newprot,
mm/huge_memory.c +2 -5
index 5b2876942639..8f1bbbf01f5b 100644
@@ -1871,17 +1871,14 @@ static pmd_t move_soft_dirty_pmd(pmd_t pmd)
 }
 
 bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
-		  unsigned long new_addr, unsigned long old_end,
-		  pmd_t *old_pmd, pmd_t *new_pmd)
+		  unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
 {
 	spinlock_t *old_ptl, *new_ptl;
 	pmd_t pmd;
 	struct mm_struct *mm = vma->vm_mm;
 	bool force_flush = false;
 
-	if ((old_addr & ~HPAGE_PMD_MASK) ||
-	    (new_addr & ~HPAGE_PMD_MASK) ||
-	    old_end - old_addr < HPAGE_PMD_SIZE)
+	if ((old_addr & ~HPAGE_PMD_MASK) || (new_addr & ~HPAGE_PMD_MASK))
 		return false;
 
 	/*
mm/mremap.c +4 -7
index bcc7aa62f2d9..c2af8ba4ba43 100644
@@ -193,16 +193,13 @@ static void move_ptes(struct vm_area_struct *vma, pmd_t *old_pmd,
 
 #ifdef CONFIG_HAVE_MOVE_PMD
 static bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr,
-		  unsigned long new_addr, unsigned long old_end,
-		  pmd_t *old_pmd, pmd_t *new_pmd)
+		  unsigned long new_addr, pmd_t *old_pmd, pmd_t *new_pmd)
 {
 	spinlock_t *old_ptl, *new_ptl;
 	struct mm_struct *mm = vma->vm_mm;
 	pmd_t pmd;
 
-	if ((old_addr & ~PMD_MASK) ||
-	    (new_addr & ~PMD_MASK) ||
-	    old_end - old_addr < PMD_SIZE)
+	if ((old_addr & ~PMD_MASK) || (new_addr & ~PMD_MASK))
 		return false;
 
 	/*
@@ -274,7 +271,7 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
 				if (need_rmap_locks)
 					take_rmap_locks(vma);
 				moved = move_huge_pmd(vma, old_addr, new_addr,
-						    old_end, old_pmd, new_pmd);
+						      old_pmd, new_pmd);
 				if (need_rmap_locks)
 					drop_rmap_locks(vma);
 				if (moved)
@@ -294,7 +291,7 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
 			if (need_rmap_locks)
 				take_rmap_locks(vma);
 			moved = move_normal_pmd(vma, old_addr, new_addr,
-					old_end, old_pmd, new_pmd);
+						old_pmd, new_pmd);
 			if (need_rmap_locks)
 				drop_rmap_locks(vma);
 			if (moved)
Signature · 1 line
2.17.1
author
#3 lore raw

PATCH v2 3/4 mm/mremap: calculate extent in one place

In reply to Wei Yang

Page tables is moved on the base of PMD. This requires both source and destination range should meet the requirement.

Current code works well since move_huge_pmd() and move_normal_pmd() would check old_addr and new_addr again. And then return to move_ptes() if the either of them is not aligned.

In stead of calculating the extent separately, it is better to calculate in one place, so we know it is not necessary to try move pmd. By doing so, the logic seems a little clear.

---
 mm/mremap.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/mremap.c +3 -3
index c2af8ba4ba43..b2f3344d090a 100644
@@ -258,6 +258,9 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
 		extent = next - old_addr;
 		if (extent > old_end - old_addr)
 			extent = old_end - old_addr;
+		next = (new_addr + PMD_SIZE) & PMD_MASK;
+		if (extent > next - new_addr)
+			extent = next - new_addr;
 		old_pmd = get_old_pmd(vma->vm_mm, old_addr);
 		if (!old_pmd)
 			continue;
@@ -301,9 +304,6 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
 
 		if (pte_alloc(new_vma->vm_mm, new_pmd))
 			break;
-		next = (new_addr + PMD_SIZE) & PMD_MASK;
-		if (extent > next - new_addr)
-			extent = next - new_addr;
 		move_ptes(vma, old_pmd, old_addr, old_addr + extent, new_vma,
 			  new_pmd, new_addr, need_rmap_locks);
 	}
Signature · 1 line
2.17.1
author
#4 lore raw

PATCH v2 4/4 mm/mremap: start addresses are properly aligned

In reply to Wei Yang

After previous cleanup, extent is the minimal step for both source and destination. This means when extent is HPAGE_PMD_SIZE or PMD_SIZE, old_addr and new_addr are properly aligned too.

Since these two functions are only invoked in move_page_tables, it is safe to remove the check now.

---
 mm/huge_memory.c | 3 ---
 mm/mremap.c      | 3 ---
 2 files changed, 6 deletions(-)
mm/huge_memory.c +0 -3
index 8f1bbbf01f5b..cc98d0f07d0a 100644
@@ -1878,9 +1878,6 @@ bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr,
 	struct mm_struct *mm = vma->vm_mm;
 	bool force_flush = false;
 
-	if ((old_addr & ~HPAGE_PMD_MASK) || (new_addr & ~HPAGE_PMD_MASK))
-		return false;
-
 	/*
 	 * The destination pmd shouldn't be established, free_pgtables()
 	 * should have release it.
mm/mremap.c +0 -3
index b2f3344d090a..7510f4e03fca 100644
@@ -199,9 +199,6 @@ static bool move_normal_pmd(struct vm_area_struct *vma, unsigned long old_addr,
 	struct mm_struct *mm = vma->vm_mm;
 	pmd_t pmd;
 
-	if ((old_addr & ~PMD_MASK) || (new_addr & ~PMD_MASK))
-		return false;
-
 	/*
 	 * The destination pmd shouldn't be established, free_pgtables()
 	 * should have release it.
Signature · 1 line
2.17.1
Tested-by on the v2 series
#5 lore raw
In reply to Wei Yang

29.01.2020 03:26, Wei Yang пишет:

Quoted 21 lines

move_page_tables() tries to move page table by PMD or PTE.

The root reason is if it tries to move PMD, both old and new range should be PMD aligned. But current code calculate old range and new range separately. This leads to some redundant check and calculation.

This cleanup tries to consolidate the range check in one place to reduce some extra range handling.

v2:
* remove 3rd patch which doesn't work on ARM platform. Thanks report from Dmitry Osipenko

Wei Yang (4):
mm/mremap: format the check in move_normal_pmd() same as

    move_huge_pmd()

mm/mremap: it is sure to have enough space when extent meets
requirement
mm/mremap: calculate extent in one place mm/mremap: start addresses are properly aligned

Quoted 5 lines
 include/linux/huge_mm.h |  2 +-
 mm/huge_memory.c        |  8 +-------
 mm/mremap.c             | 17 ++++++-----------
 3 files changed, 8 insertions(+), 19 deletions(-)

Hello Wei,

I haven't noticed any problems using the v2. Thank you very much for addressing the problem!

author
#6 lore raw
Quoted 28 lines · Dmitry Osipenko

29.01.2020 03:26, Wei Yang пишет:

Quoted 21 lines

move_page_tables() tries to move page table by PMD or PTE.

The root reason is if it tries to move PMD, both old and new range should be PMD aligned. But current code calculate old range and new range separately. This leads to some redundant check and calculation.

This cleanup tries to consolidate the range check in one place to reduce some extra range handling.

v2:
* remove 3rd patch which doesn't work on ARM platform. Thanks report from Dmitry Osipenko

Wei Yang (4):
mm/mremap: format the check in move_normal_pmd() same as

    move_huge_pmd()

mm/mremap: it is sure to have enough space when extent meets
requirement
mm/mremap: calculate extent in one place mm/mremap: start addresses are properly aligned

Quoted 5 lines
 include/linux/huge_mm.h |  2 +-
 mm/huge_memory.c        |  8 +-------
 mm/mremap.c             | 17 ++++++-----------
 3 files changed, 8 insertions(+), 19 deletions(-)
Quoted 5 lines

Hello Wei,

I haven't noticed any problems using the v2. Thank you very much for addressing the problem!

Glad to hear this.

Actually, really thanks for your patience on testing and reporting the problem.

Have a nice day :-)

Quoted 1 line
Signature · 2 lines
Wei Yang
Help you, Help me