public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Sean Christopherson <seanjc@google.com>
To: James Houghton <jthoughton@google.com>
Cc: David Matlack <dmatlack@google.com>, Peter Xu <peterx@redhat.com>,
	Andrea Arcangeli <aarcange@redhat.com>,
	Paolo Bonzini <pbonzini@redhat.com>,
	Axel Rasmussen <axelrasmussen@google.com>,
	Linux MM <linux-mm@kvack.org>, kvm <kvm@vger.kernel.org>,
	chao.p.peng@linux.intel.com
Subject: Re: [RFC] Improving userfaultfd scalability for live migration
Date: Tue, 6 Dec 2022 01:06:09 +0000	[thread overview]
Message-ID: <Y46VgQRU+do50iuv@google.com> (raw)
In-Reply-To: <CADrL8HV_8=ssHSumpQX5bVm2h2J01swdB=+at8=xLr+KtW79MQ@mail.gmail.com>

On Mon, Dec 05, 2022, James Houghton wrote:
> On Mon, Dec 5, 2022 at 1:20 PM Sean Christopherson <seanjc@google.com> wrote:
> >
> > On Mon, Dec 05, 2022, David Matlack wrote:
> > > On Mon, Dec 5, 2022 at 7:30 AM Peter Xu <peterx@redhat.com> 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).

  reply	other threads:[~2022-12-06  1:06 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-01 19:37 [RFC] Improving userfaultfd scalability for live migration James Houghton
2022-12-03  1:03 ` Sean Christopherson
2022-12-05 15:27   ` Peter Xu
2022-12-05 17:31     ` David Matlack
2022-12-05 18:03       ` David Matlack
2022-12-05 18:23         ` Sean Christopherson
2022-12-05 18:20       ` Sean Christopherson
2022-12-05 21:19         ` James Houghton
2022-12-06  1:06           ` Sean Christopherson [this message]
2022-12-06 17:35             ` James Houghton
2022-12-06 18:00               ` Sean Christopherson
2022-12-06 20:41                 ` James Houghton
2022-12-08  1:56                   ` David Matlack
2022-12-08 17:50                     ` James Houghton
2023-01-04  0:57                       ` Sean Christopherson
2023-01-04  1:05                         ` James Houghton

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=Y46VgQRU+do50iuv@google.com \
    --to=seanjc@google.com \
    --cc=aarcange@redhat.com \
    --cc=axelrasmussen@google.com \
    --cc=chao.p.peng@linux.intel.com \
    --cc=dmatlack@google.com \
    --cc=jthoughton@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.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