[PATCH 04/10] mm/mremap: cleanup post-processing stage of mremap

Lorenzo Stoakes posted 10 patches 3 months ago
There is a newer version of this series
[PATCH 04/10] mm/mremap: cleanup post-processing stage of mremap
Posted by Lorenzo Stoakes 3 months ago
Separate out the uffd bits so it clear's what's happening.

Don't bother setting vrm->mmap_locked after unlocking, because after this
we are done anyway.

The only time we drop the mmap lock is on VMA shrink, at which point
vrm->new_len will be < vrm->old_len and the operation will not be performed
anyway, so move this code out of the if (vrm->mmap_locked) block.

All addresses returned by mremap() are page-aligned, so the
offset_in_page() check on ret seems only to be incorrectly trying to detect
whether an error occurred - explicitly check for this.

No functional change intended.

Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
---
 mm/mremap.c | 22 +++++++++++++---------
 1 file changed, 13 insertions(+), 9 deletions(-)

diff --git a/mm/mremap.c b/mm/mremap.c
index 60eb0ac8634b..660bdb75e2f9 100644
--- a/mm/mremap.c
+++ b/mm/mremap.c
@@ -1729,6 +1729,15 @@ static int check_prep_vma(struct vma_remap_struct *vrm)
 	return 0;
 }
 
+static void notify_uffd(struct vma_remap_struct *vrm, unsigned long ret)
+{
+	struct mm_struct *mm = current->mm;
+
+	userfaultfd_unmap_complete(mm, vrm->uf_unmap_early);
+	mremap_userfaultfd_complete(vrm->uf, vrm->addr, ret, vrm->old_len);
+	userfaultfd_unmap_complete(mm, vrm->uf_unmap);
+}
+
 static unsigned long do_mremap(struct vma_remap_struct *vrm)
 {
 	struct mm_struct *mm = current->mm;
@@ -1754,18 +1763,13 @@ static unsigned long do_mremap(struct vma_remap_struct *vrm)
 	res = vrm_implies_new_addr(vrm) ? mremap_to(vrm) : mremap_at(vrm);
 
 out:
-	if (vrm->mmap_locked) {
+	if (vrm->mmap_locked)
 		mmap_write_unlock(mm);
-		vrm->mmap_locked = false;
-
-		if (!offset_in_page(res) && vrm->mlocked && vrm->new_len > vrm->old_len)
-			mm_populate(vrm->new_addr + vrm->old_len, vrm->delta);
-	}
 
-	userfaultfd_unmap_complete(mm, vrm->uf_unmap_early);
-	mremap_userfaultfd_complete(vrm->uf, vrm->addr, res, vrm->old_len);
-	userfaultfd_unmap_complete(mm, vrm->uf_unmap);
+	if (!IS_ERR_VALUE(res) && vrm->mlocked && vrm->new_len > vrm->old_len)
+		mm_populate(vrm->new_addr + vrm->old_len, vrm->delta);
 
+	notify_uffd(vrm, res);
 	return res;
 }
 
-- 
2.50.0
Re: [PATCH 04/10] mm/mremap: cleanup post-processing stage of mremap
Posted by Vlastimil Babka 2 months, 4 weeks ago
On 7/7/25 07:27, Lorenzo Stoakes wrote:
> Separate out the uffd bits so it clear's what's happening.
> 
> Don't bother setting vrm->mmap_locked after unlocking, because after this
> we are done anyway.
> 
> The only time we drop the mmap lock is on VMA shrink, at which point
> vrm->new_len will be < vrm->old_len and the operation will not be performed
> anyway, so move this code out of the if (vrm->mmap_locked) block.
> 
> All addresses returned by mremap() are page-aligned, so the
> offset_in_page() check on ret seems only to be incorrectly trying to detect

"incorrectly" to me implies there's a bug. But AFAIU there's not, so maybe
e.g. "inappropriately"?

> whether an error occurred - explicitly check for this.
> 
> No functional change intended.
> 
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

Reviewed-by: Vlastimil Babka <vbabka@suse.cz>

Just a nit:

> ---
>  mm/mremap.c | 22 +++++++++++++---------
>  1 file changed, 13 insertions(+), 9 deletions(-)
> 
> diff --git a/mm/mremap.c b/mm/mremap.c
> index 60eb0ac8634b..660bdb75e2f9 100644
> --- a/mm/mremap.c
> +++ b/mm/mremap.c
> @@ -1729,6 +1729,15 @@ static int check_prep_vma(struct vma_remap_struct *vrm)
>  	return 0;
>  }
>  
> +static void notify_uffd(struct vma_remap_struct *vrm, unsigned long ret)

"ret" not "res"? :) Or actually why not name it for what it is,
mremap_userfaultfd_complete() names the parameter "to". Maybe to_addr or
new_addr?

> +{
> +	struct mm_struct *mm = current->mm;
> +
> +	userfaultfd_unmap_complete(mm, vrm->uf_unmap_early);
> +	mremap_userfaultfd_complete(vrm->uf, vrm->addr, ret, vrm->old_len);
> +	userfaultfd_unmap_complete(mm, vrm->uf_unmap);
> +}
> +
>  static unsigned long do_mremap(struct vma_remap_struct *vrm)
>  {
>  	struct mm_struct *mm = current->mm;
> @@ -1754,18 +1763,13 @@ static unsigned long do_mremap(struct vma_remap_struct *vrm)
>  	res = vrm_implies_new_addr(vrm) ? mremap_to(vrm) : mremap_at(vrm);
>  
>  out:
> -	if (vrm->mmap_locked) {
> +	if (vrm->mmap_locked)
>  		mmap_write_unlock(mm);
> -		vrm->mmap_locked = false;
> -
> -		if (!offset_in_page(res) && vrm->mlocked && vrm->new_len > vrm->old_len)
> -			mm_populate(vrm->new_addr + vrm->old_len, vrm->delta);
> -	}
>  
> -	userfaultfd_unmap_complete(mm, vrm->uf_unmap_early);
> -	mremap_userfaultfd_complete(vrm->uf, vrm->addr, res, vrm->old_len);
> -	userfaultfd_unmap_complete(mm, vrm->uf_unmap);
> +	if (!IS_ERR_VALUE(res) && vrm->mlocked && vrm->new_len > vrm->old_len)
> +		mm_populate(vrm->new_addr + vrm->old_len, vrm->delta);
>  
> +	notify_uffd(vrm, res);
>  	return res;
>  }
>
Re: [PATCH 04/10] mm/mremap: cleanup post-processing stage of mremap
Posted by Lorenzo Stoakes 2 months, 4 weeks ago
On Thu, Jul 10, 2025 at 03:49:09PM +0200, Vlastimil Babka wrote:
> On 7/7/25 07:27, Lorenzo Stoakes wrote:
> > Separate out the uffd bits so it clear's what's happening.
> >
> > Don't bother setting vrm->mmap_locked after unlocking, because after this
> > we are done anyway.
> >
> > The only time we drop the mmap lock is on VMA shrink, at which point
> > vrm->new_len will be < vrm->old_len and the operation will not be performed
> > anyway, so move this code out of the if (vrm->mmap_locked) block.
> >
> > All addresses returned by mremap() are page-aligned, so the
> > offset_in_page() check on ret seems only to be incorrectly trying to detect
>
> "incorrectly" to me implies there's a bug. But AFAIU there's not, so maybe
> e.g. "inappropriately"?
>
> > whether an error occurred - explicitly check for this.
> >
> > No functional change intended.
> >
> > Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
>
> Reviewed-by: Vlastimil Babka <vbabka@suse.cz>

Thanks! :)

>
> Just a nit:
>
> > ---
> >  mm/mremap.c | 22 +++++++++++++---------
> >  1 file changed, 13 insertions(+), 9 deletions(-)
> >
> > diff --git a/mm/mremap.c b/mm/mremap.c
> > index 60eb0ac8634b..660bdb75e2f9 100644
> > --- a/mm/mremap.c
> > +++ b/mm/mremap.c
> > @@ -1729,6 +1729,15 @@ static int check_prep_vma(struct vma_remap_struct *vrm)
> >  	return 0;
> >  }
> >
> > +static void notify_uffd(struct vma_remap_struct *vrm, unsigned long ret)
>
> "ret" not "res"? :) Or actually why not name it for what it is,
> mremap_userfaultfd_complete() names the parameter "to". Maybe to_addr or
> new_addr?

Later in the series we eliminate this as you've seen, but still worth fixign up
I think, will do on respin!

>
> > +{
> > +	struct mm_struct *mm = current->mm;
> > +
> > +	userfaultfd_unmap_complete(mm, vrm->uf_unmap_early);
> > +	mremap_userfaultfd_complete(vrm->uf, vrm->addr, ret, vrm->old_len);
> > +	userfaultfd_unmap_complete(mm, vrm->uf_unmap);
> > +}
> > +
> >  static unsigned long do_mremap(struct vma_remap_struct *vrm)
> >  {
> >  	struct mm_struct *mm = current->mm;
> > @@ -1754,18 +1763,13 @@ static unsigned long do_mremap(struct vma_remap_struct *vrm)
> >  	res = vrm_implies_new_addr(vrm) ? mremap_to(vrm) : mremap_at(vrm);
> >
> >  out:
> > -	if (vrm->mmap_locked) {
> > +	if (vrm->mmap_locked)
> >  		mmap_write_unlock(mm);
> > -		vrm->mmap_locked = false;
> > -
> > -		if (!offset_in_page(res) && vrm->mlocked && vrm->new_len > vrm->old_len)
> > -			mm_populate(vrm->new_addr + vrm->old_len, vrm->delta);
> > -	}
> >
> > -	userfaultfd_unmap_complete(mm, vrm->uf_unmap_early);
> > -	mremap_userfaultfd_complete(vrm->uf, vrm->addr, res, vrm->old_len);
> > -	userfaultfd_unmap_complete(mm, vrm->uf_unmap);
> > +	if (!IS_ERR_VALUE(res) && vrm->mlocked && vrm->new_len > vrm->old_len)
> > +		mm_populate(vrm->new_addr + vrm->old_len, vrm->delta);
> >
> > +	notify_uffd(vrm, res);
> >  	return res;
> >  }
> >
>