diff mbox series

[v3,08/16] mm/mmap: Inline munmap operation in mmap_region()

Message ID 20240704182718.2653918-9-Liam.Howlett@oracle.com (mailing list archive)
State New
Headers show
Series Avoid MAP_FIXED gap exposure | expand

Commit Message

Liam R. Howlett July 4, 2024, 6:27 p.m. UTC
From: "Liam R. Howlett" <Liam.Howlett@Oracle.com>

mmap_region is already passed sanitized addr and len, so change the
call to do_vmi_munmap() to do_vmi_align_munmap() and inline the other
checks.

Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
---
 mm/mmap.c | 20 ++++++++++++++------
 1 file changed, 14 insertions(+), 6 deletions(-)

Comments

Lorenzo Stoakes July 5, 2024, 7:39 p.m. UTC | #1
On Thu, Jul 04, 2024 at 02:27:10PM GMT, Liam R. Howlett wrote:
> From: "Liam R. Howlett" <Liam.Howlett@Oracle.com>
>
> mmap_region is already passed sanitized addr and len, so change the
> call to do_vmi_munmap() to do_vmi_align_munmap() and inline the other
> checks.
>
> Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> ---
>  mm/mmap.c | 20 ++++++++++++++------
>  1 file changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/mm/mmap.c b/mm/mmap.c
> index 8d9be791997a..e9858ca8bbd4 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -2937,12 +2937,20 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>  			return -ENOMEM;
>  	}
>
> -	/* Unmap any existing mapping in the area */

This feels like an important comment, I know 'find the first overlapping
VMA' below alludes to it but whenever I've read through this function this
comment has actually been quite useful to know what the intent is.

Could we reinstate it?

> -	error = do_vmi_munmap(&vmi, mm, addr, len, uf, false);
> -	if (error == -EPERM)
> -		return error;
> -	else if (error)
> -		return -ENOMEM;
> +
> +	if (unlikely(!can_modify_mm(mm, addr, end)))
> +		return -EPERM;
> +
> +	 /* arch_unmap() might do unmaps itself.  */
> +	arch_unmap(mm, addr, end);
> +
> +	/* Find the first overlapping VMA */
> +	vma = vma_find(&vmi, end);
> +	if (vma) {
> +		if (do_vmi_align_munmap(&vmi, vma, mm, addr, end, uf, false))
> +			return -ENOMEM;
> +		vma = NULL;
> +	}
>
>  	/*
>  	 * Private writable mapping: check memory availability
> --
> 2.43.0
>
>

Other than trivial point above, LGTM.

Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Liam R. Howlett July 5, 2024, 8 p.m. UTC | #2
* Lorenzo Stoakes <lorenzo.stoakes@oracle.com> [240705 15:40]:
> On Thu, Jul 04, 2024 at 02:27:10PM GMT, Liam R. Howlett wrote:
> > From: "Liam R. Howlett" <Liam.Howlett@Oracle.com>
> >
> > mmap_region is already passed sanitized addr and len, so change the
> > call to do_vmi_munmap() to do_vmi_align_munmap() and inline the other
> > checks.
> >
> > Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> > ---
> >  mm/mmap.c | 20 ++++++++++++++------
> >  1 file changed, 14 insertions(+), 6 deletions(-)
> >
> > diff --git a/mm/mmap.c b/mm/mmap.c
> > index 8d9be791997a..e9858ca8bbd4 100644
> > --- a/mm/mmap.c
> > +++ b/mm/mmap.c
> > @@ -2937,12 +2937,20 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
> >  			return -ENOMEM;
> >  	}
> >
> > -	/* Unmap any existing mapping in the area */
> 
> This feels like an important comment, I know 'find the first overlapping
> VMA' below alludes to it but whenever I've read through this function this
> comment has actually been quite useful to know what the intent is.
> 
> Could we reinstate it?

Ah, sure.  I am going to have to move it around and it sort of loses its
meaning a bit once we gather and complete it (and not actually zero the
tree)..  I'll try to keep it with the correct code block.

> 
> > -	error = do_vmi_munmap(&vmi, mm, addr, len, uf, false);
> > -	if (error == -EPERM)
> > -		return error;
> > -	else if (error)
> > -		return -ENOMEM;
> > +
> > +	if (unlikely(!can_modify_mm(mm, addr, end)))
> > +		return -EPERM;
> > +
> > +	 /* arch_unmap() might do unmaps itself.  */
> > +	arch_unmap(mm, addr, end);
> > +
> > +	/* Find the first overlapping VMA */
> > +	vma = vma_find(&vmi, end);
> > +	if (vma) {
> > +		if (do_vmi_align_munmap(&vmi, vma, mm, addr, end, uf, false))
> > +			return -ENOMEM;
> > +		vma = NULL;
> > +	}
> >
> >  	/*
> >  	 * Private writable mapping: check memory availability
> > --
> > 2.43.0
> >
> >
> 
> Other than trivial point above, LGTM.
> 
> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

Thanks!
Suren Baghdasaryan July 10, 2024, 4:15 p.m. UTC | #3
On Thu, Jul 4, 2024 at 11:27 AM Liam R. Howlett <Liam.Howlett@oracle.com> wrote:
>
> From: "Liam R. Howlett" <Liam.Howlett@Oracle.com>
>
> mmap_region is already passed sanitized addr and len, so change the
> call to do_vmi_munmap() to do_vmi_align_munmap() and inline the other
> checks.

Hmm. I think such refactoring when you want to skip some checks would
be done a bit differently... You would introduce a __do_vmi_munmap()
function which is called at the end of do_vmi_munmap() after the
checks and then call __do_vmi_munmap() directly wherever you want to
skip the checks. That would avoid code duplication. Any reason that
can't be done here?

>
> Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> ---
>  mm/mmap.c | 20 ++++++++++++++------
>  1 file changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/mm/mmap.c b/mm/mmap.c
> index 8d9be791997a..e9858ca8bbd4 100644
> --- a/mm/mmap.c
> +++ b/mm/mmap.c
> @@ -2937,12 +2937,20 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
>                         return -ENOMEM;
>         }
>
> -       /* Unmap any existing mapping in the area */
> -       error = do_vmi_munmap(&vmi, mm, addr, len, uf, false);
> -       if (error == -EPERM)
> -               return error;
> -       else if (error)
> -               return -ENOMEM;
> +
> +       if (unlikely(!can_modify_mm(mm, addr, end)))
> +               return -EPERM;
> +
> +        /* arch_unmap() might do unmaps itself.  */
> +       arch_unmap(mm, addr, end);
> +
> +       /* Find the first overlapping VMA */
> +       vma = vma_find(&vmi, end);
> +       if (vma) {
> +               if (do_vmi_align_munmap(&vmi, vma, mm, addr, end, uf, false))
> +                       return -ENOMEM;
> +               vma = NULL;
> +       }
>
>         /*
>          * Private writable mapping: check memory availability
> --
> 2.43.0
>
Liam R. Howlett July 10, 2024, 4:35 p.m. UTC | #4
* Suren Baghdasaryan <surenb@google.com> [240710 12:16]:
> On Thu, Jul 4, 2024 at 11:27 AM Liam R. Howlett <Liam.Howlett@oracle.com> wrote:
> >
> > From: "Liam R. Howlett" <Liam.Howlett@Oracle.com>
> >
> > mmap_region is already passed sanitized addr and len, so change the
> > call to do_vmi_munmap() to do_vmi_align_munmap() and inline the other
> > checks.
> 
> Hmm. I think such refactoring when you want to skip some checks would
> be done a bit differently... You would introduce a __do_vmi_munmap()
> function which is called at the end of do_vmi_munmap() after the
> checks and then call __do_vmi_munmap() directly wherever you want to
> skip the checks. That would avoid code duplication. Any reason that
> can't be done here?

Because I'm going to completely change and remove the checks in future
patches.  I should have added this to the log that this is an internal
step for reviewing.

> 
> >
> > Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> > ---
> >  mm/mmap.c | 20 ++++++++++++++------
> >  1 file changed, 14 insertions(+), 6 deletions(-)
> >
> > diff --git a/mm/mmap.c b/mm/mmap.c
> > index 8d9be791997a..e9858ca8bbd4 100644
> > --- a/mm/mmap.c
> > +++ b/mm/mmap.c
> > @@ -2937,12 +2937,20 @@ unsigned long mmap_region(struct file *file, unsigned long addr,
> >                         return -ENOMEM;
> >         }
> >
> > -       /* Unmap any existing mapping in the area */
> > -       error = do_vmi_munmap(&vmi, mm, addr, len, uf, false);
> > -       if (error == -EPERM)
> > -               return error;
> > -       else if (error)
> > -               return -ENOMEM;
> > +
> > +       if (unlikely(!can_modify_mm(mm, addr, end)))
> > +               return -EPERM;
> > +
> > +        /* arch_unmap() might do unmaps itself.  */
> > +       arch_unmap(mm, addr, end);
> > +
> > +       /* Find the first overlapping VMA */
> > +       vma = vma_find(&vmi, end);
> > +       if (vma) {
> > +               if (do_vmi_align_munmap(&vmi, vma, mm, addr, end, uf, false))
> > +                       return -ENOMEM;
> > +               vma = NULL;
> > +       }
> >
> >         /*
> >          * Private writable mapping: check memory availability
> > --
> > 2.43.0
> >
>
diff mbox series

Patch

diff --git a/mm/mmap.c b/mm/mmap.c
index 8d9be791997a..e9858ca8bbd4 100644
--- a/mm/mmap.c
+++ b/mm/mmap.c
@@ -2937,12 +2937,20 @@  unsigned long mmap_region(struct file *file, unsigned long addr,
 			return -ENOMEM;
 	}
 
-	/* Unmap any existing mapping in the area */
-	error = do_vmi_munmap(&vmi, mm, addr, len, uf, false);
-	if (error == -EPERM)
-		return error;
-	else if (error)
-		return -ENOMEM;
+
+	if (unlikely(!can_modify_mm(mm, addr, end)))
+		return -EPERM;
+
+	 /* arch_unmap() might do unmaps itself.  */
+	arch_unmap(mm, addr, end);
+
+	/* Find the first overlapping VMA */
+	vma = vma_find(&vmi, end);
+	if (vma) {
+		if (do_vmi_align_munmap(&vmi, vma, mm, addr, end, uf, false))
+			return -ENOMEM;
+		vma = NULL;
+	}
 
 	/*
 	 * Private writable mapping: check memory availability