From: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
To: "Liam R. Howlett" <Liam.Howlett@oracle.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
linux-mm@kvack.org, linux-kernel@vger.kernel.org,
Jann Horn <jannh@google.com>,
David Hildenbrand <david@redhat.com>,
Qi Zheng <zhengqi.arch@bytedance.com>,
Kefeng Wang <wangkefeng.wang@huawei.com>,
Jeff Xu <jeffxu@chromium.org>,
Pedro Falcato <pedro.falcato@gmail.com>
Subject: Re: [PATCH v2 1/2] mm/mremap: Clean up vma_to_resize()
Date: Mon, 21 Oct 2024 11:03:30 +0100 [thread overview]
Message-ID: <8ea11588-02ac-419f-b531-90e8cc3b330f@lucifer.local> (raw)
In-Reply-To: <20241018174114.2871880-2-Liam.Howlett@oracle.com>
On Fri, Oct 18, 2024 at 01:41:13PM -0400, Liam R. Howlett wrote:
> From: "Liam R. Howlett" <Liam.Howlett@Oracle.com>
>
> vma_to_resize() is used in two locations to find and validate the vma
> for the mremap location. One of the two locations already has the vma,
> which is then re-found to validate the same vma.
>
> This code can be simplified by moving the vma_lookup() from
> vma_to_resize() to mremap_to() and changing the return type to an int
> error.
>
> Since the function now just validates the vma, the function is renamed
> to resize_is_valid() to better reflect what it is doing.
>
> This commit also adds documentation about the function.
>
> Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
git-icdiff helped here a lot :)
This LGTM,
Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Generally a really nice clean up in any case, thanks for doing this!
> ---
> mm/mremap.c | 53 +++++++++++++++++++++++++++++++----------------------
> 1 file changed, 31 insertions(+), 22 deletions(-)
>
> diff --git a/mm/mremap.c b/mm/mremap.c
> index 5917feafe8cc..e781ec4573ca 100644
> --- a/mm/mremap.c
> +++ b/mm/mremap.c
> @@ -826,17 +826,24 @@ static unsigned long move_vma(struct vm_area_struct *vma,
> return new_addr;
> }
>
> -static struct vm_area_struct *vma_to_resize(unsigned long addr,
> +/*
> + * resize_is_valid() - Ensure the vma can be resized to the new length at the give
> + * address.
> + *
> + * @vma: The vma to resize
> + * @addr: The old address
> + * @old_len: The current size
> + * @new_len: The desired size
> + * @flags: The vma flags
> + *
> + * Return 0 on success, error otherwise.
> + */
> +static int resize_is_valid(struct vm_area_struct *vma, unsigned long addr,
> unsigned long old_len, unsigned long new_len, unsigned long flags)
> {
> struct mm_struct *mm = current->mm;
> - struct vm_area_struct *vma;
> unsigned long pgoff;
>
> - vma = vma_lookup(mm, addr);
> - if (!vma)
> - return ERR_PTR(-EFAULT);
> -
> /*
> * !old_len is a special case where an attempt is made to 'duplicate'
> * a mapping. This makes no sense for private mappings as it will
> @@ -847,37 +854,37 @@ static struct vm_area_struct *vma_to_resize(unsigned long addr,
> */
> if (!old_len && !(vma->vm_flags & (VM_SHARED | VM_MAYSHARE))) {
> pr_warn_once("%s (%d): attempted to duplicate a private mapping with mremap. This is not supported.\n", current->comm, current->pid);
> - return ERR_PTR(-EINVAL);
> + return -EINVAL;
> }
>
> if ((flags & MREMAP_DONTUNMAP) &&
> (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP)))
> - return ERR_PTR(-EINVAL);
> + return -EINVAL;
>
> /* We can't remap across vm area boundaries */
> if (old_len > vma->vm_end - addr)
> - return ERR_PTR(-EFAULT);
> + return -EFAULT;
>
> if (new_len == old_len)
> - return vma;
> + return 0;
>
> /* Need to be careful about a growing mapping */
> pgoff = (addr - vma->vm_start) >> PAGE_SHIFT;
> pgoff += vma->vm_pgoff;
> if (pgoff + (new_len >> PAGE_SHIFT) < pgoff)
> - return ERR_PTR(-EINVAL);
> + return -EINVAL;
>
> if (vma->vm_flags & (VM_DONTEXPAND | VM_PFNMAP))
> - return ERR_PTR(-EFAULT);
> + return -EFAULT;
>
> if (!mlock_future_ok(mm, vma->vm_flags, new_len - old_len))
> - return ERR_PTR(-EAGAIN);
> + return -EAGAIN;
>
> if (!may_expand_vm(mm, vma->vm_flags,
> (new_len - old_len) >> PAGE_SHIFT))
> - return ERR_PTR(-ENOMEM);
> + return -ENOMEM;
>
> - return vma;
> + return 0;
> }
>
> static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
> @@ -936,12 +943,16 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
> old_len = new_len;
> }
>
> - vma = vma_to_resize(addr, old_len, new_len, flags);
> - if (IS_ERR(vma)) {
> - ret = PTR_ERR(vma);
> + vma = vma_lookup(mm, addr);
> + if (!vma) {
> + ret = -EFAULT;
> goto out;
> }
>
> + ret = resize_is_valid(vma, addr, old_len, new_len, flags);
> + if (ret)
> + goto out;
> +
> /* MREMAP_DONTUNMAP expands by old_len since old_len == new_len */
> if (flags & MREMAP_DONTUNMAP &&
> !may_expand_vm(mm, vma->vm_flags, old_len >> PAGE_SHIFT)) {
> @@ -1114,11 +1125,9 @@ SYSCALL_DEFINE5(mremap, unsigned long, addr, unsigned long, old_len,
> /*
> * Ok, we need to grow..
> */
> - vma = vma_to_resize(addr, old_len, new_len, flags);
> - if (IS_ERR(vma)) {
> - ret = PTR_ERR(vma);
> + ret = resize_is_valid(vma, addr, old_len, new_len, flags);
> + if (ret)
> goto out;
> - }
>
> /* old_len exactly to the end of the area..
> */
> --
> 2.43.0
>
next prev parent reply other threads:[~2024-10-21 10:04 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-10-18 17:41 [PATCH v2 0/2] mm/mremap: Remove extra vma tree walk Liam R. Howlett
2024-10-18 17:41 ` [PATCH v2 1/2] mm/mremap: Clean up vma_to_resize() Liam R. Howlett
2024-10-21 10:03 ` Lorenzo Stoakes [this message]
2024-10-22 22:08 ` Pedro Falcato
2024-10-23 2:10 ` Liam R. Howlett
2024-10-18 17:41 ` [PATCH v2 2/2] mm/mremap: Remove goto from mremap_to() Liam R. Howlett
2024-10-21 10:19 ` Lorenzo Stoakes
2024-10-22 22:10 ` Pedro Falcato
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=8ea11588-02ac-419f-b531-90e8cc3b330f@lucifer.local \
--to=lorenzo.stoakes@oracle.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=david@redhat.com \
--cc=jannh@google.com \
--cc=jeffxu@chromium.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=pedro.falcato@gmail.com \
--cc=wangkefeng.wang@huawei.com \
--cc=zhengqi.arch@bytedance.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox