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,
Suren Baghdasaryan <surenb@google.com>,
Matthew Wilcox <willy@infradead.org>,
Vlastimil Babka <vbabka@suse.cz>,
sidhartha.kumar@oracle.com, Bert Karwatzki <spasswolf@web.de>,
Jiri Olsa <olsajiri@gmail.com>, Kees Cook <kees@kernel.org>,
"Paul E . McKenney" <paulmck@kernel.org>
Subject: Re: [PATCH v6 06/20] mm/vma: Change munmap to use vma_munmap_struct() for accounting and surrounding vmas
Date: Wed, 21 Aug 2024 10:59:50 +0100 [thread overview]
Message-ID: <29bfe4bc-b60b-4d5d-a915-bc835312c989@lucifer.local> (raw)
In-Reply-To: <20240820235730.2852400-7-Liam.Howlett@oracle.com>
On Tue, Aug 20, 2024 at 07:57:15PM GMT, Liam R. Howlett wrote:
> From: "Liam R. Howlett" <Liam.Howlett@Oracle.com>
>
> Clean up the code by changing the munmap operation to use a structure
> for the accounting and munmap variables.
>
> Since remove_mt() is only called in one location and the contents will
> be reduced to almost nothing. The remains of the function can be added
> to vms_complete_munmap_vmas().
>
> Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
> Reviewed-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
> Reviewed-by: Suren Baghdasaryan <surenb@google.com>
> ---
> mm/vma.c | 79 ++++++++++++++++++++++++++++----------------------------
> mm/vma.h | 6 +++++
> 2 files changed, 46 insertions(+), 39 deletions(-)
>
> diff --git a/mm/vma.c b/mm/vma.c
> index e1aee43a3dc4..7b8b8b983399 100644
> --- a/mm/vma.c
> +++ b/mm/vma.c
[snip]
> mm = vms->mm;
> @@ -731,21 +708,26 @@ static void vms_complete_munmap_vmas(struct vma_munmap_struct *vms,
> if (vms->unlock)
> mmap_write_downgrade(mm);
>
> - prev = vma_iter_prev_range(vms->vmi);
> - next = vma_next(vms->vmi);
> - if (next)
> - vma_iter_prev_range(vms->vmi);
> -
> /*
> * We can free page tables without write-locking mmap_lock because VMAs
> * were isolated before we downgraded mmap_lock.
> */
> mas_set(mas_detach, 1);
> - unmap_region(mm, mas_detach, vms->vma, prev, next, vms->start, vms->end,
> - vms->vma_count, !vms->unlock);
> - /* Statistics and freeing VMAs */
> + unmap_region(mm, mas_detach, vms->vma, vms->prev, vms->next,
> + vms->start, vms->end, vms->vma_count, !vms->unlock);
> + /* Update high watermark before we lower total_vm */
> + update_hiwater_vm(mm);
> + /* Stat accounting */
> + WRITE_ONCE(mm->total_vm, READ_ONCE(mm->total_vm) - vms->nr_pages);
> + mm->exec_vm -= vms->exec_vm;
> + mm->stack_vm -= vms->stack_vm;
> + mm->data_vm -= vms->data_vm;
See below, but I bisected a bug to this patch that manifested because of
miscalculated accounting. So I wonder whether it'd be a good idea to take
this opportunity, when updating mm->... stats to add some:
VM_WARN_ON(vms->exec_vm > mm->exec_vm);
etc. for each of the fields updated. This would help catch any accounting
issues like this with CONFIG_DEBUG_VM switched on.
[snip]
> @@ -824,6 +807,22 @@ static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
> goto munmap_gather_failed;
>
> vma_mark_detached(next, true);
> + nrpages = vma_pages(next);
> +
> + vms->nr_pages += nrpages;
> + if (next->vm_flags & VM_LOCKED)
> + vms->locked_vm += nrpages;
So I bisected a self-test failure, memfd_secret specifically, to this
commit. This is because you are double-counting VM_LOCKED...
> +
> + if (next->vm_flags & VM_ACCOUNT)
> + vms->nr_accounted += nrpages;
> +
> + if (is_exec_mapping(next->vm_flags))
> + vms->exec_vm += nrpages;
> + else if (is_stack_mapping(next->vm_flags))
> + vms->stack_vm += nrpages;
> + else if (is_data_mapping(next->vm_flags))
> + vms->data_vm += nrpages;
> +
> if (next->vm_flags & VM_LOCKED)
> vms->locked_vm += vma_pages(next);
...the double counting being right here :) so I think we should drop the
above couple lines.
>
> @@ -847,7 +846,9 @@ static int vms_gather_munmap_vmas(struct vma_munmap_struct *vms,
> BUG_ON(next->vm_start < vms->start);
> BUG_ON(next->vm_start > vms->end);
> #endif
> - } for_each_vma_range(*(vms->vmi), next, vms->end);
> + }
> +
> + vms->next = vma_next(vms->vmi);
>
> #if defined(CONFIG_DEBUG_VM_MAPLE_TREE)
> /* Make sure no VMAs are about to be lost. */
> diff --git a/mm/vma.h b/mm/vma.h
> index cb67acf59012..cbf55e0e0c4f 100644
> --- a/mm/vma.h
> +++ b/mm/vma.h
> @@ -33,12 +33,18 @@ struct vma_munmap_struct {
> struct vma_iterator *vmi;
> struct mm_struct *mm;
> struct vm_area_struct *vma; /* The first vma to munmap */
> + struct vm_area_struct *prev; /* vma before the munmap area */
> + struct vm_area_struct *next; /* vma after the munmap area */
> struct list_head *uf; /* Userfaultfd list_head */
> unsigned long start; /* Aligned start addr (inclusive) */
> unsigned long end; /* Aligned end addr (exclusive) */
> int vma_count; /* Number of vmas that will be removed */
> unsigned long nr_pages; /* Number of pages being removed */
> unsigned long locked_vm; /* Number of locked pages */
> + unsigned long nr_accounted; /* Number of VM_ACCOUNT pages */
> + unsigned long exec_vm;
> + unsigned long stack_vm;
> + unsigned long data_vm;
> bool unlock; /* Unlock after the munmap */
> };
>
> --
> 2.43.0
>
next prev parent reply other threads:[~2024-08-21 10:00 UTC|newest]
Thread overview: 32+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-20 23:57 [PATCH v6 00/20] Avoid MAP_FIXED gap exposure Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 01/20] mm/vma: Correctly position vma_iterator in __split_vma() Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 02/20] mm/vma: Introduce abort_munmap_vmas() Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 03/20] mm/vma: Introduce vmi_complete_munmap_vmas() Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 04/20] mm/vma: Extract the gathering of vmas from do_vmi_align_munmap() Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 05/20] mm/vma: Introduce vma_munmap_struct for use in munmap operations Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 06/20] mm/vma: Change munmap to use vma_munmap_struct() for accounting and surrounding vmas Liam R. Howlett
2024-08-21 9:59 ` Lorenzo Stoakes [this message]
2024-08-21 13:17 ` Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 07/20] mm/vma: Extract validate_mm() from vma_complete() Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 08/20] mm/vma: Inline munmap operation in mmap_region() Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 09/20] mm/vma: Expand mmap_region() munmap call Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 10/20] mm/vma: Support vma == NULL in init_vma_munmap() Liam R. Howlett
2024-08-21 10:07 ` Lorenzo Stoakes
2024-08-21 13:18 ` Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 11/20] mm/mmap: Reposition vma iterator in mmap_region() Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 12/20] mm/vma: Track start and end for munmap in vma_munmap_struct Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 13/20] mm: Clean up unmap_region() argument list Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 14/20] mm/mmap: Avoid zeroing vma tree in mmap_region() Liam R. Howlett
2024-08-21 11:02 ` Lorenzo Stoakes
2024-08-21 15:09 ` Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 15/20] mm: Change failure of MAP_FIXED to restoring the gap on failure Liam R. Howlett
2024-08-21 11:56 ` Lorenzo Stoakes
2024-08-21 13:31 ` Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 16/20] mm/mmap: Use PHYS_PFN in mmap_region() Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 17/20] mm/mmap: Use vms accounted pages " Liam R. Howlett
2024-08-21 16:35 ` Paul Moore
2024-08-21 17:15 ` Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 18/20] ipc/shm, mm: Drop do_vma_munmap() Liam R. Howlett
2024-08-21 11:59 ` Lorenzo Stoakes
2024-08-20 23:57 ` [PATCH v6 19/20] mm: Move may_expand_vm() check in mmap_region() Liam R. Howlett
2024-08-20 23:57 ` [PATCH v6 20/20] mm/vma: Drop incorrect comment from vms_gather_munmap_vmas() Liam R. Howlett
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=29bfe4bc-b60b-4d5d-a915-bc835312c989@lucifer.local \
--to=lorenzo.stoakes@oracle.com \
--cc=Liam.Howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=olsajiri@gmail.com \
--cc=paulmck@kernel.org \
--cc=sidhartha.kumar@oracle.com \
--cc=spasswolf@web.de \
--cc=surenb@google.com \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
/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