From: "Liam R. Howlett" <Liam.Howlett@oracle.com>
To: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
Suren Baghdasaryan <surenb@google.com>,
Vlastimil Babka <vbabka@suse.cz>,
Lorenzo Stoakes <lstoakes@gmail.com>,
Matthew Wilcox <willy@infradead.org>,
sidhartha.kumar@oracle.com,
"Paul E . McKenney" <paulmck@kernel.org>,
Bert Karwatzki <spasswolf@web.de>, Jiri Olsa <olsajiri@gmail.com>,
linux-kernel@vger.kernel.org, Kees Cook <kees@kernel.org>,
Jeff Xu <jeffxu@chromium.org>
Subject: Re: [PATCH v5 14/21] mm/mmap: Avoid zeroing vma tree in mmap_region()
Date: Tue, 23 Jul 2024 10:15:55 -0400 [thread overview]
Message-ID: <onro3fbguuhy2r3o2isb3acalujp2xf65orxreuxuxeyfjtc7f@4aev34wc5pvb> (raw)
In-Reply-To: <0928cf11-ccf9-4065-8768-4216916c6223@lucifer.local>
* Lorenzo Stoakes <lorenzo.stoakes@oracle.com> [240722 14:43]:
> On Wed, Jul 17, 2024 at 04:07:02PM GMT, Liam R. Howlett wrote:
> > From: "Liam R. Howlett" <Liam.Howlett@Oracle.com>
> >
> > Instead of zeroing the vma tree and then overwriting the area, let the
> > area be overwritten and then clean up the gathered vmas using
> > vms_complete_munmap_vmas().
> >
> > If a driver is mapping over an existing vma, then clear the ptes before
> > the call_mmap() invocation. If the vma has a vm_ops->close(), then call
> > the close() function. This is done using the vms_clear_ptes() and
> > vms_close_vmas() helpers. This has the side effect of needing to call
> > open() on the vmas if the mmap_region() fails later on.
> >
> > Temporarily keep track of the number of pages that will be removed and
> > reduce the charged amount.
> >
> > This commit drops the validate_mm() call in the vma_expand() function.
> > It is necessary to drop the validate as it would fail since the mm
> > map_count would be incorrect during a vma expansion, prior to the
> > cleanup from vms_complete_munmap_vmas().
> >
> > Clean up the error handing of the vms_gather_munmap_vmas() by calling
> > the verification within the function.
> >
> > Note that before this change, a MAP_FIXED could fail and leave a gap in
> > the vma tree. With this change, a MAP_FIXED failure will fail without
> > creating a gap and leave *a* vma in the area (may have been split) and
> > attept to restore them to an operational state (re-attached and
> > vm_ops->open()'ed if they were vm_ops->close()'d).
> >
> > Signed-off-by: Liam R. Howlett <Liam.Howlett@Oracle.com>
> > ---
> > mm/internal.h | 2 +
> > mm/mmap.c | 119 +++++++++++++++++++++++++++++++-------------------
> > 2 files changed, 76 insertions(+), 45 deletions(-)
> >
> > diff --git a/mm/internal.h b/mm/internal.h
> > index ec8441362c28..5bd60cb9fcbb 100644
> > --- a/mm/internal.h
> > +++ b/mm/internal.h
> > @@ -1503,6 +1503,8 @@ struct vma_munmap_struct {
> > unsigned long stack_vm;
> > unsigned long data_vm;
> > bool unlock; /* Unlock after the munmap */
> > + bool clear_ptes; /* If there are outstanding PTE to be cleared */
> > + bool closed; /* vma->vm_ops->close() called already */
> > };
> >
> > void __meminit __init_single_page(struct page *page, unsigned long pfn,
> > diff --git a/mm/mmap.c b/mm/mmap.c
> > index 20da0d039c95..0b7aa2c46cec 100644
> > --- a/mm/mmap.c
> > +++ b/mm/mmap.c
> > @@ -170,10 +170,11 @@ void unlink_file_vma_batch_final(struct unlink_vma_file_batch *vb)
> > /*
> > * Close a vm structure and free it.
> > */
> > -static void remove_vma(struct vm_area_struct *vma, bool unreachable)
> > +static
> > +void remove_vma(struct vm_area_struct *vma, bool unreachable, bool closed)
> > {
> > might_sleep();
> > - if (vma->vm_ops && vma->vm_ops->close)
> > + if (!closed && vma->vm_ops && vma->vm_ops->close)
> > vma->vm_ops->close(vma);
> > if (vma->vm_file)
> > fput(vma->vm_file);
> > @@ -401,17 +402,21 @@ anon_vma_interval_tree_post_update_vma(struct vm_area_struct *vma)
> > }
> >
> > static unsigned long count_vma_pages_range(struct mm_struct *mm,
> > - unsigned long addr, unsigned long end)
> > + unsigned long addr, unsigned long end,
> > + unsigned long *nr_accounted)
> > {
> > VMA_ITERATOR(vmi, mm, addr);
> > struct vm_area_struct *vma;
> > unsigned long nr_pages = 0;
> >
> > + *nr_accounted = 0;
> > for_each_vma_range(vmi, vma, end) {
> > unsigned long vm_start = max(addr, vma->vm_start);
> > unsigned long vm_end = min(end, vma->vm_end);
> >
> > nr_pages += PHYS_PFN(vm_end - vm_start);
> > + if (vma->vm_flags & VM_ACCOUNT)
> > + *nr_accounted += PHYS_PFN(vm_end - vm_start);
> > }
> >
> > return nr_pages;
> > @@ -527,6 +532,8 @@ static inline void init_vma_munmap(struct vma_munmap_struct *vms,
> > vms->exec_vm = vms->stack_vm = vms->data_vm = 0;
> > vms->unmap_start = FIRST_USER_ADDRESS;
> > vms->unmap_end = USER_PGTABLES_CEILING;
> > + vms->clear_ptes = false; /* No PTEs to clear yet */
> > + vms->closed = false;
> > }
> >
> > /*
> > @@ -735,7 +742,6 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma,
> > vma_iter_store(vmi, vma);
> >
> > vma_complete(&vp, vmi, vma->vm_mm);
> > - validate_mm(vma->vm_mm);
> > return 0;
> >
> > nomem:
> > @@ -2597,23 +2603,31 @@ struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi,
> > *
> > * Reattach any detached vmas and free up the maple tree used to track the vmas.
> > */
> > -static inline void abort_munmap_vmas(struct ma_state *mas_detach)
> > +static inline void abort_munmap_vmas(struct ma_state *mas_detach, bool closed)
> > {
> > struct vm_area_struct *vma;
> >
> > mas_set(mas_detach, 0);
> > - mas_for_each(mas_detach, vma, ULONG_MAX)
> > + mas_for_each(mas_detach, vma, ULONG_MAX) {
> > + if (closed && vma->vm_ops && vma->vm_ops->close &&
> > + vma->vm_ops->open)
> > + vma->vm_ops->open(vma);
> > +
>
> Unfortunately I think this is broken. While in theory this should probably
> be semantically correct, I think drivers get this wrong.
>
> For instance, in the devio driver, usbdev_vm_ops assigns custom VMA
> open/close functionality in usbdev_vm_open() and usbdev_vm_close().
>
> usbdev_vm_open() simply increments a 'vma_use_count' counter, whereas
> usbdev_vm_close() calls dec_usb_memory_use_count(), which, if the count
> reaches zero, frees a bunch of objects.
>
> I've not tested it directly, but it's conceivable that we could end up with
> an entirely broken mapping that might result in a kernel NULL pointer deref
> or some such other hideous, possibly exploitable (at least for DoS), scenario.
>
> Also since this is up to drivers, we can't really control whether people do
> stupid things here or otherwise assume this close/reopen scenario cannot
> happen.
>
> I think the fact we _might_ cause inconsistent kernel state here rules this
> out as an approach unfortunately.
>
> We can't simply do what this code did before (that is, leaving a hole) as
> this might require allocations to clear a range in the maple tree (as you
> pointed out in the open VMA scalability call earlier today).
>
> However, as I suggested in the call, it seems that the case of us
> performing a MAP_FIXED mapping _and_ removing underlying VMAs _and_ those
> VMAs having custom close() operators is very niche, so in this instance it
> seems sensible to simply preallocate memory to allow us this out, and
> clearing the range + returning an error if this occurs in this scenario
> only.
>
> Since this is such an edge case it'll mean we almost never preallocate,
> only doing so in this rare instance.
>
> It's ugly, but it seems there is no really 'pretty' solution to this
> problem, and we don't want this to block this series!
>
I will add a patch to this series that will preallocate to leave a gap
(as we do today in the impossible-to-undo failure cases).
Suren suggested I keep the change in its own patch so people see why we
cannot do the Right Thing(tm).
Thanks,
Liam
...
next prev parent reply other threads:[~2024-07-23 14:16 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-07-17 20:06 [PATCH v5 00/21] Avoid MAP_FIXED gap exposure Liam R. Howlett
2024-07-17 20:06 ` [PATCH v5 01/21] mm/mmap: Correctly position vma_iterator in __split_vma() Liam R. Howlett
2024-07-17 20:06 ` [PATCH v5 02/21] mm/mmap: Introduce abort_munmap_vmas() Liam R. Howlett
2024-07-17 20:06 ` [PATCH v5 03/21] mm/mmap: Introduce vmi_complete_munmap_vmas() Liam R. Howlett
2024-07-17 20:06 ` [PATCH v5 04/21] mm/mmap: Extract the gathering of vmas from do_vmi_align_munmap() Liam R. Howlett
2024-07-17 20:06 ` [PATCH v5 05/21] mm/mmap: Introduce vma_munmap_struct for use in munmap operations Liam R. Howlett
2024-07-17 20:06 ` [PATCH v5 06/21] mm/mmap: Change munmap to use vma_munmap_struct() for accounting and surrounding vmas Liam R. Howlett
2024-07-17 20:06 ` [PATCH v5 07/21] mm/mmap: Extract validate_mm() from vma_complete() Liam R. Howlett
2024-07-17 20:06 ` [PATCH v5 08/21] mm/mmap: Inline munmap operation in mmap_region() Liam R. Howlett
2024-07-17 20:06 ` [PATCH v5 09/21] mm/mmap: Expand mmap_region() munmap call Liam R. Howlett
2024-07-17 20:06 ` [PATCH v5 10/21] mm/mmap: Support vma == NULL in init_vma_munmap() Liam R. Howlett
2024-07-22 13:14 ` Lorenzo Stoakes
2024-07-17 20:06 ` [PATCH v5 11/21] mm/mmap: Reposition vma iterator in mmap_region() Liam R. Howlett
2024-07-17 20:07 ` [PATCH v5 12/21] mm/mmap: Track start and end of munmap in vma_munmap_struct Liam R. Howlett
2024-07-17 20:07 ` [PATCH v5 13/21] mm/mmap: Clean up unmap_region() argument list Liam R. Howlett
2024-07-17 20:07 ` [PATCH v5 14/21] mm/mmap: Avoid zeroing vma tree in mmap_region() Liam R. Howlett
2024-07-22 18:42 ` Lorenzo Stoakes
2024-07-23 14:15 ` Liam R. Howlett [this message]
2024-07-17 20:07 ` [PATCH v5 15/21] mm/mmap: Use PHYS_PFN " Liam R. Howlett
2024-07-17 20:07 ` [PATCH v5 16/21] mm/mmap: Use vms accounted pages " Liam R. Howlett
2024-07-24 20:23 ` Paul Moore
2024-07-17 20:07 ` [PATCH v5 17/21] mm/mmap: Relocate arch_unmap() to vms_complete_munmap_vmas() Liam R. Howlett
2024-07-22 14:25 ` Lorenzo Stoakes
2024-07-23 14:11 ` Liam R. Howlett
2024-07-17 20:07 ` [PATCH v5 18/21] mm/mmap: Move can_modify_mm() check down the stack Liam R. Howlett
2024-07-22 14:41 ` Lorenzo Stoakes
2024-07-24 16:29 ` Jeff Xu
2024-07-17 20:07 ` [PATCH v5 19/21] ipc/shm, mm: Drop do_vma_munmap() Liam R. Howlett
2024-07-22 14:58 ` Lorenzo Stoakes
2024-07-17 20:07 ` [PATCH v5 20/21] mm/mmap: Move may_expand_vm() check in mmap_region() Liam R. Howlett
2024-07-17 20:07 ` [PATCH v5 21/21] mm/mmap: 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=onro3fbguuhy2r3o2isb3acalujp2xf65orxreuxuxeyfjtc7f@4aev34wc5pvb \
--to=liam.howlett@oracle.com \
--cc=akpm@linux-foundation.org \
--cc=jeffxu@chromium.org \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=lorenzo.stoakes@oracle.com \
--cc=lstoakes@gmail.com \
--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