linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little
@ 2020-06-26 13:52 Wei Yang
  2020-06-26 13:52 ` [RESEND Patch v2 1/4] mm/mremap: format the check in move_normal_pmd() same as move_huge_pmd() Wei Yang
                   ` (4 more replies)
  0 siblings, 5 replies; 13+ messages in thread
From: Wei Yang @ 2020-06-26 13:52 UTC (permalink / raw)
  To: akpm, kirill.shutemov, yang.shi, vbabka, willy, thomas_os,
	thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken
  Cc: linux-kernel, linux-mm, digetx, Wei Yang

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 and
    test 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(-)

-- 
2.20.1 (Apple Git-117)



^ permalink raw reply	[flat|nested] 13+ messages in thread

* [RESEND Patch v2 1/4] mm/mremap: format the check in move_normal_pmd() same as move_huge_pmd()
  2020-06-26 13:52 [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little Wei Yang
@ 2020-06-26 13:52 ` Wei Yang
  2020-07-06 10:06   ` Kirill A. Shutemov
  2020-06-26 13:52 ` [RESEND Patch v2 2/4] mm/mremap: it is sure to have enough space when extent meets requirement Wei Yang
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 13+ messages in thread
From: Wei Yang @ 2020-06-26 13:52 UTC (permalink / raw)
  To: akpm, kirill.shutemov, yang.shi, vbabka, willy, thomas_os,
	thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken
  Cc: linux-kernel, linux-mm, digetx, Wei Yang

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

Signed-off-by: Wei Yang <richard.weiyang@linux.alibaba.com>
Tested-by: Dmitry Osipenko <digetx@gmail.com>
---
 mm/mremap.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/mm/mremap.c b/mm/mremap.c
index 5dd572d57ca9..97bf9a2a8bd5 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -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;
 
 	/*
-- 
2.20.1 (Apple Git-117)



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [RESEND Patch v2 2/4] mm/mremap: it is sure to have enough space when extent meets requirement
  2020-06-26 13:52 [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little Wei Yang
  2020-06-26 13:52 ` [RESEND Patch v2 1/4] mm/mremap: format the check in move_normal_pmd() same as move_huge_pmd() Wei Yang
@ 2020-06-26 13:52 ` Wei Yang
  2020-06-26 13:52 ` [RESEND Patch v2 3/4] mm/mremap: calculate extent in one place Wei Yang
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 13+ messages in thread
From: Wei Yang @ 2020-06-26 13:52 UTC (permalink / raw)
  To: akpm, kirill.shutemov, yang.shi, vbabka, willy, thomas_os,
	thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken
  Cc: linux-kernel, linux-mm, digetx, 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.

Signed-off-by: Wei Yang <richard.weiyang@linux.alibaba.com>
Tested-by: Dmitry Osipenko <digetx@gmail.com>
---
 include/linux/huge_mm.h |  2 +-
 mm/huge_memory.c        |  7 ++-----
 mm/mremap.c             | 11 ++++-------
 3 files changed, 7 insertions(+), 13 deletions(-)

diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h
index 71f20776b06c..17c4c4975145 100644
--- a/include/linux/huge_mm.h
+++ b/include/linux/huge_mm.h
@@ -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,
diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 78c84bee7e29..1e580fdad4d0 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1722,17 +1722,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;
 
 	/*
diff --git a/mm/mremap.c b/mm/mremap.c
index 97bf9a2a8bd5..de27b12c8a5a 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -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)
-- 
2.20.1 (Apple Git-117)



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [RESEND Patch v2 3/4] mm/mremap: calculate extent in one place
  2020-06-26 13:52 [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little Wei Yang
  2020-06-26 13:52 ` [RESEND Patch v2 1/4] mm/mremap: format the check in move_normal_pmd() same as move_huge_pmd() Wei Yang
  2020-06-26 13:52 ` [RESEND Patch v2 2/4] mm/mremap: it is sure to have enough space when extent meets requirement Wei Yang
@ 2020-06-26 13:52 ` Wei Yang
  2020-07-06 10:07   ` Kirill A. Shutemov
  2020-06-26 13:52 ` [RESEND Patch v2 4/4] mm/mremap: start addresses are properly aligned Wei Yang
  2020-07-06 10:08 ` [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little Kirill A. Shutemov
  4 siblings, 1 reply; 13+ messages in thread
From: Wei Yang @ 2020-06-26 13:52 UTC (permalink / raw)
  To: akpm, kirill.shutemov, yang.shi, vbabka, willy, thomas_os,
	thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken
  Cc: linux-kernel, linux-mm, digetx, 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.

Signed-off-by: Wei Yang <richard.weiyang@linux.alibaba.com>
Tested-by: Dmitry Osipenko <digetx@gmail.com>
---
 mm/mremap.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/mm/mremap.c b/mm/mremap.c
index de27b12c8a5a..a30b3e86cc99 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -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);
 	}
-- 
2.20.1 (Apple Git-117)



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* [RESEND Patch v2 4/4] mm/mremap: start addresses are properly aligned
  2020-06-26 13:52 [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little Wei Yang
                   ` (2 preceding siblings ...)
  2020-06-26 13:52 ` [RESEND Patch v2 3/4] mm/mremap: calculate extent in one place Wei Yang
@ 2020-06-26 13:52 ` Wei Yang
  2020-07-06 10:08 ` [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little Kirill A. Shutemov
  4 siblings, 0 replies; 13+ messages in thread
From: Wei Yang @ 2020-06-26 13:52 UTC (permalink / raw)
  To: akpm, kirill.shutemov, yang.shi, vbabka, willy, thomas_os,
	thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken
  Cc: linux-kernel, linux-mm, digetx, 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.

Signed-off-by: Wei Yang <richard.weiyang@linux.alibaba.com>
Tested-by: Dmitry Osipenko <digetx@gmail.com>
---
 mm/huge_memory.c | 3 ---
 mm/mremap.c      | 3 ---
 2 files changed, 6 deletions(-)

diff --git a/mm/huge_memory.c b/mm/huge_memory.c
index 1e580fdad4d0..462a7dbd6350 100644
--- a/mm/huge_memory.c
+++ b/mm/huge_memory.c
@@ -1729,9 +1729,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.
diff --git a/mm/mremap.c b/mm/mremap.c
index a30b3e86cc99..f5f17d050617 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -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.
-- 
2.20.1 (Apple Git-117)



^ permalink raw reply related	[flat|nested] 13+ messages in thread

* Re: [RESEND Patch v2 1/4] mm/mremap: format the check in move_normal_pmd() same as move_huge_pmd()
  2020-06-26 13:52 ` [RESEND Patch v2 1/4] mm/mremap: format the check in move_normal_pmd() same as move_huge_pmd() Wei Yang
@ 2020-07-06 10:06   ` Kirill A. Shutemov
  0 siblings, 0 replies; 13+ messages in thread
From: Kirill A. Shutemov @ 2020-07-06 10:06 UTC (permalink / raw)
  To: Wei Yang
  Cc: akpm, kirill.shutemov, yang.shi, vbabka, willy, thomas_os,
	thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken, linux-kernel, linux-mm, digetx

On Fri, Jun 26, 2020 at 09:52:13PM +0800, Wei Yang wrote:
> No functional change, just improve the readability and prepare for
> following cleanup.

It's pretty much redundant. It doesn't provide any meaningful
readability improvement. Please drop the patch.

-- 
 Kirill A. Shutemov


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RESEND Patch v2 3/4] mm/mremap: calculate extent in one place
  2020-06-26 13:52 ` [RESEND Patch v2 3/4] mm/mremap: calculate extent in one place Wei Yang
@ 2020-07-06 10:07   ` Kirill A. Shutemov
  2020-07-07  1:38     ` Wei Yang
  0 siblings, 1 reply; 13+ messages in thread
From: Kirill A. Shutemov @ 2020-07-06 10:07 UTC (permalink / raw)
  To: Wei Yang
  Cc: akpm, kirill.shutemov, yang.shi, vbabka, willy, thomas_os,
	thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken, linux-kernel, linux-mm, digetx

On Fri, Jun 26, 2020 at 09:52:15PM +0800, Wei Yang wrote:
> 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.
> 
> Signed-off-by: Wei Yang <richard.weiyang@linux.alibaba.com>
> Tested-by: Dmitry Osipenko <digetx@gmail.com>
> ---
>  mm/mremap.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/mm/mremap.c b/mm/mremap.c
> index de27b12c8a5a..a30b3e86cc99 100644
> --- a/mm/mremap.c
> +++ b/mm/mremap.c
> @@ -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;

Please use round_up() for both 'next' calculations.

> +		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);
>  	}
> -- 
> 2.20.1 (Apple Git-117)
> 

-- 
 Kirill A. Shutemov


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little
  2020-06-26 13:52 [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little Wei Yang
                   ` (3 preceding siblings ...)
  2020-06-26 13:52 ` [RESEND Patch v2 4/4] mm/mremap: start addresses are properly aligned Wei Yang
@ 2020-07-06 10:08 ` Kirill A. Shutemov
  2020-07-06 22:06   ` Wei Yang
  4 siblings, 1 reply; 13+ messages in thread
From: Kirill A. Shutemov @ 2020-07-06 10:08 UTC (permalink / raw)
  To: Wei Yang
  Cc: akpm, kirill.shutemov, yang.shi, vbabka, willy, thomas_os,
	thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken, linux-kernel, linux-mm, digetx

On Fri, Jun 26, 2020 at 09:52:12PM +0800, Wei Yang wrote:
> 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.

The patchet looks good to me. I have few nits, but nothing substantial.

Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>

-- 
 Kirill A. Shutemov


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little
  2020-07-06 10:08 ` [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little Kirill A. Shutemov
@ 2020-07-06 22:06   ` Wei Yang
  2020-07-06 23:04     ` Andrew Morton
  0 siblings, 1 reply; 13+ messages in thread
From: Wei Yang @ 2020-07-06 22:06 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: Wei Yang, akpm, kirill.shutemov, yang.shi, vbabka, willy,
	thomas_os, thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken, linux-kernel, linux-mm, digetx

On Mon, Jul 06, 2020 at 01:08:19PM +0300, Kirill A. Shutemov wrote:
>On Fri, Jun 26, 2020 at 09:52:12PM +0800, Wei Yang wrote:
>> 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.
>
>The patchet looks good to me. I have few nits, but nothing substantial.
>
>Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
>

Hi, Kirill

Thanks for your review.

Andrew,

Do you want me to send another version or you would like to adjust it
directly?

>-- 
> Kirill A. Shutemov

-- 
Wei Yang
Help you, Help me


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little
  2020-07-06 22:06   ` Wei Yang
@ 2020-07-06 23:04     ` Andrew Morton
  0 siblings, 0 replies; 13+ messages in thread
From: Andrew Morton @ 2020-07-06 23:04 UTC (permalink / raw)
  To: Wei Yang
  Cc: Kirill A. Shutemov, Wei Yang, kirill.shutemov, yang.shi, vbabka,
	willy, thomas_os, thellstrom, anshuman.khandual,
	sean.j.christopherson, aneesh.kumar, peterx, walken,
	linux-kernel, linux-mm, digetx

On Mon, 6 Jul 2020 22:06:48 +0000 Wei Yang <richard.weiyang@gmail.com> wrote:

> >The patchet looks good to me. I have few nits, but nothing substantial.
> >
> >Acked-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
> >
> 
> Hi, Kirill
> 
> Thanks for your review.
> 
> Andrew,
> 
> Do you want me to send another version or you would like to adjust it
> directly?

Please resend?


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RESEND Patch v2 3/4] mm/mremap: calculate extent in one place
  2020-07-06 10:07   ` Kirill A. Shutemov
@ 2020-07-07  1:38     ` Wei Yang
  2020-07-07 10:47       ` Kirill A. Shutemov
  0 siblings, 1 reply; 13+ messages in thread
From: Wei Yang @ 2020-07-07  1:38 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: Wei Yang, akpm, kirill.shutemov, yang.shi, vbabka, willy,
	thomas_os, thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken, linux-kernel, linux-mm, digetx

On Mon, Jul 06, 2020 at 01:07:29PM +0300, Kirill A. Shutemov wrote:
>On Fri, Jun 26, 2020 at 09:52:15PM +0800, Wei Yang wrote:
>> 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.
>> 
>> Signed-off-by: Wei Yang <richard.weiyang@linux.alibaba.com>
>> Tested-by: Dmitry Osipenko <digetx@gmail.com>
>> ---
>>  mm/mremap.c | 6 +++---
>>  1 file changed, 3 insertions(+), 3 deletions(-)
>> 
>> diff --git a/mm/mremap.c b/mm/mremap.c
>> index de27b12c8a5a..a30b3e86cc99 100644
>> --- a/mm/mremap.c
>> +++ b/mm/mremap.c
>> @@ -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;
>
>Please use round_up() for both 'next' calculations.
>

I took another close look into this, seems this is not a good suggestion.

   round_up(new_addr, PMD_SIZE)

would be new_addr when new_addr is PMD_SIZE aligned, which is not what we
expect.

>> +		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);
>>  	}
>> -- 
>> 2.20.1 (Apple Git-117)
>> 
>
>-- 
> Kirill A. Shutemov

-- 
Wei Yang
Help you, Help me


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RESEND Patch v2 3/4] mm/mremap: calculate extent in one place
  2020-07-07  1:38     ` Wei Yang
@ 2020-07-07 10:47       ` Kirill A. Shutemov
  2020-07-07 12:53         ` Wei Yang
  0 siblings, 1 reply; 13+ messages in thread
From: Kirill A. Shutemov @ 2020-07-07 10:47 UTC (permalink / raw)
  To: Wei Yang
  Cc: akpm, kirill.shutemov, yang.shi, vbabka, willy, thomas_os,
	thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken, linux-kernel, linux-mm, digetx

On Tue, Jul 07, 2020 at 09:38:56AM +0800, Wei Yang wrote:
> On Mon, Jul 06, 2020 at 01:07:29PM +0300, Kirill A. Shutemov wrote:
> >On Fri, Jun 26, 2020 at 09:52:15PM +0800, Wei Yang wrote:
> >> 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.
> >> 
> >> Signed-off-by: Wei Yang <richard.weiyang@linux.alibaba.com>
> >> Tested-by: Dmitry Osipenko <digetx@gmail.com>
> >> ---
> >>  mm/mremap.c | 6 +++---
> >>  1 file changed, 3 insertions(+), 3 deletions(-)
> >> 
> >> diff --git a/mm/mremap.c b/mm/mremap.c
> >> index de27b12c8a5a..a30b3e86cc99 100644
> >> --- a/mm/mremap.c
> >> +++ b/mm/mremap.c
> >> @@ -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;
> >
> >Please use round_up() for both 'next' calculations.
> >
> 
> I took another close look into this, seems this is not a good suggestion.
> 
>    round_up(new_addr, PMD_SIZE)
> 
> would be new_addr when new_addr is PMD_SIZE aligned, which is not what we
> expect.

Maybe round_down(new_addr + PMD_SIZE, PMD_SIZE)?

-- 
 Kirill A. Shutemov


^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [RESEND Patch v2 3/4] mm/mremap: calculate extent in one place
  2020-07-07 10:47       ` Kirill A. Shutemov
@ 2020-07-07 12:53         ` Wei Yang
  0 siblings, 0 replies; 13+ messages in thread
From: Wei Yang @ 2020-07-07 12:53 UTC (permalink / raw)
  To: Kirill A. Shutemov
  Cc: Wei Yang, akpm, kirill.shutemov, yang.shi, vbabka, willy,
	thomas_os, thellstrom, anshuman.khandual, sean.j.christopherson,
	aneesh.kumar, peterx, walken, linux-kernel, linux-mm, digetx

On Tue, Jul 07, 2020 at 01:47:22PM +0300, Kirill A. Shutemov wrote:
>On Tue, Jul 07, 2020 at 09:38:56AM +0800, Wei Yang wrote:
>> On Mon, Jul 06, 2020 at 01:07:29PM +0300, Kirill A. Shutemov wrote:
>> >On Fri, Jun 26, 2020 at 09:52:15PM +0800, Wei Yang wrote:
>> >> 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.
>> >> 
>> >> Signed-off-by: Wei Yang <richard.weiyang@linux.alibaba.com>
>> >> Tested-by: Dmitry Osipenko <digetx@gmail.com>
>> >> ---
>> >>  mm/mremap.c | 6 +++---
>> >>  1 file changed, 3 insertions(+), 3 deletions(-)
>> >> 
>> >> diff --git a/mm/mremap.c b/mm/mremap.c
>> >> index de27b12c8a5a..a30b3e86cc99 100644
>> >> --- a/mm/mremap.c
>> >> +++ b/mm/mremap.c
>> >> @@ -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;
>> >
>> >Please use round_up() for both 'next' calculations.
>> >
>> 
>> I took another close look into this, seems this is not a good suggestion.
>> 
>>    round_up(new_addr, PMD_SIZE)
>> 
>> would be new_addr when new_addr is PMD_SIZE aligned, which is not what we
>> expect.
>
>Maybe round_down(new_addr + PMD_SIZE, PMD_SIZE)?
>

To be honest, I don't like this which makes the code not that straight
forward. And when you look into the definition of pxd_addr_end(), they use the
format of ((addr + PXD_SIZE) & PXD_MASK) too.

I have another alternative to clean up this part with the help of
pmd_addr_end(). If you agree, I would like to append the following change in
next version to cleanup the next/extent staff especially.

Author: Wei Yang <richard.weiyang@linux.alibaba.com>
Date:   Tue Jul 7 17:42:49 2020 +0800

    mm/mremap: use pmd_addr_end to calculate extent

diff --git a/mm/mremap.c b/mm/mremap.c
index f5f17d050617..76e7fdf567c3 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -237,11 +237,12 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
 		unsigned long new_addr, unsigned long len,
 		bool need_rmap_locks)
 {
-	unsigned long extent, next, old_end;
+	unsigned long extent, old_next, new_next, old_end, new_end;
 	struct mmu_notifier_range range;
 	pmd_t *old_pmd, *new_pmd;
 
 	old_end = old_addr + len;
+	new_end = new_addr + len;
 	flush_cache_range(vma, old_addr, old_end);
 
 	mmu_notifier_range_init(&range, MMU_NOTIFY_UNMAP, 0, vma, vma->vm_mm,
@@ -250,14 +251,11 @@ unsigned long move_page_tables(struct vm_area_struct *vma,
 
 	for (; old_addr < old_end; old_addr += extent, new_addr += extent) {
 		cond_resched();
-		next = (old_addr + PMD_SIZE) & PMD_MASK;
-		/* even if next overflowed, extent below will be ok */
-		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_next = pmd_addr_end(old_addr, old_end);
+		new_next = pmd_addr_end(new_addr, new_end);
+		extent = min((old_next - old_addr), (new_next - new_addr));
+
 		old_pmd = get_old_pmd(vma->vm_mm, old_addr);
 		if (!old_pmd)
 			continue;

>-- 
> Kirill A. Shutemov

-- 
Wei Yang
Help you, Help me


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2020-07-07 12:53 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-06-26 13:52 [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little Wei Yang
2020-06-26 13:52 ` [RESEND Patch v2 1/4] mm/mremap: format the check in move_normal_pmd() same as move_huge_pmd() Wei Yang
2020-07-06 10:06   ` Kirill A. Shutemov
2020-06-26 13:52 ` [RESEND Patch v2 2/4] mm/mremap: it is sure to have enough space when extent meets requirement Wei Yang
2020-06-26 13:52 ` [RESEND Patch v2 3/4] mm/mremap: calculate extent in one place Wei Yang
2020-07-06 10:07   ` Kirill A. Shutemov
2020-07-07  1:38     ` Wei Yang
2020-07-07 10:47       ` Kirill A. Shutemov
2020-07-07 12:53         ` Wei Yang
2020-06-26 13:52 ` [RESEND Patch v2 4/4] mm/mremap: start addresses are properly aligned Wei Yang
2020-07-06 10:08 ` [RESEND Patch v2 0/4] mm/mremap: cleanup move_page_tables() a little Kirill A. Shutemov
2020-07-06 22:06   ` Wei Yang
2020-07-06 23:04     ` Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).