Re: [RFC] Improving userfaultfd scalability for live migration

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Dec 05, 2022, James Houghton wrote:
> On Mon, Dec 5, 2022 at 1:20 PM Sean Christopherson <seanjc@xxxxxxxxxx> wrote:
> >
> > On Mon, Dec 05, 2022, David Matlack wrote:
> > > On Mon, Dec 5, 2022 at 7:30 AM Peter Xu <peterx@xxxxxxxxxx> wrote:
> > > > > > == Getting the faulting GPA to userspace ==
> > > > > > KVM_EXIT_MEMORY_FAULT was introduced recently [1] (not yet merged),
> > > > > > and it provides the main functionality we need. We can extend it
> > > > > > easily to support our use case here, and I think we have at least two
> > > > > > options:
> > > > > > - Introduce something like KVM_CAP_MEM_FAULT_REPORTING, which causes
> > > > > > KVM_RUN to exit with exit reason KVM_EXIT_MEMORY_FAULT when it would
> > > > > > otherwise just return -EFAULT (i.e., when kvm_handle_bad_page returns
> > > > > > -EFAULT).
> > > > > > - We're already introducing a new CAP, so just tie the above behavior
> > > > > > to whether or not one of the CAPs (below) is being used.
> > > > >
> > > > > We might even be able to get away with a third option: unconditionally return
> > > > > KVM_EXIT_MEMORY_FAULT instead of -EFAULT when the error occurs when accessing
> > > > > guest memory.
> 
> Wouldn't we need a new CAP for this?

Maybe?  I did say "might" :-)  -EFAULT is sooo useless for userspace in these
cases that there's a chance we can get away with an unconditional change.  Probably
not worth the risk of breaking userspace though as KVM will likely end up with a
helper to fill in the exit info.

> > > > > diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c
> > > > > index 07c81ab3fd4d..7f66b56dd6e7 100644
> > > > > --- a/fs/userfaultfd.c
> > > > > +++ b/fs/userfaultfd.c
> > > > > @@ -394,7 +394,7 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason)
> > > > >          * shmem_vm_ops->fault method is invoked even during
> > > > >          * coredumping without mmap_lock and it ends up here.
> > > > >          */
> > > > > -       if (current->flags & (PF_EXITING|PF_DUMPCORE))
> > > > > +       if (current->flags & (PF_EXITING|PF_DUMPCORE|PF_NO_UFFD_WAIT))
> > > > >                 goto out;
> > > >
> > > > I'll have a closer read on the nested part, but note that this path already
> > > > has the mmap lock then it invalidates the goal if we want to avoid taking
> > > > it from the first place, or maybe we don't care?
> 
> Not taking the mmap lock would be helpful, but we still have to take
> it in UFFDIO_CONTINUE, so it's ok if we have to still take it here.

IIUC, Peter is suggesting that the kernel not even get to the point where UFFD
is involved.  The "fault" would get propagated to userspace by KVM, userspace
fixes the fault (gets the page from the source, does MADV_POPULATE_WRITE), and
resumes the vCPU.

> The main goal is to avoid the locks in the userfaultfd wait_queues. If
> we could completely avoid taking the mmap lock for reading in the
> common post-copy case, we would avoid potential latency spikes if
> someone (e.g. khugepaged) came around and grabbed the mmap lock for
> writing.
> 
> It seems pretty difficult to make UFFDIO_CONTINUE *not* take the mmap
> lock for reading, but I suppose it could be done with something like
> the per-VMA lock work [2]. If we could avoid taking the lock in
> UFFDIO_CONTINUE, then it seems plausible that we could avoid taking it
> in slow GUP too. So really whether or not we are taking the mmap lock
> (for reading) in the mem fault path isn't a huge deal by itself.

...

> > > I don't know what userspace would do in those situations to make forward progress.
> >
> > Access the page from userspace?  E.g. a "LOCK AND -1" would resolve read and write
> > faults without modifying guest memory.
> >
> > That won't work for guests backed by "restricted mem", a.k.a. UPM guests, but
> > restricted mem really should be able to prevent those types of faults in the first
> > place.  SEV guests are the one case I can think of where that approach won't work,
> > since writes will corrupt the guest.  SEV guests can likely be special cased though.
> 
> As I mentioned in the original email, I think MADV_POPULATE_WRITE
> would work here (Peter suggested this to me last week, thanks Peter!).
> It would basically call slow GUP for us. So instead of hva_to_pfn_fast
> (fails) -> hva_to_pfn_slow -> slow GUP, we do hva_to_pfn_fast (fails)
> -> exit to userspace -> MADV_POPULATE_WRITE (-> slow GUP) -> KVM_RUN
> -> hva_to_pfn_fast (succeeds).a

Ah, nice.  Missed that (obviously).



[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux