All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Dr. David Alan Gilbert" <dgilbert@redhat.com>
To: "Michael S. Tsirkin" <mst@redhat.com>
Cc: "David Hildenbrand" <david@redhat.com>,
	"Juan Quintela" <quintela@redhat.com>,
	qemu-stable@nongnu.org,
	"Alexander Duyck" <alexander.duyck@gmail.com>,
	qemu-devel@nongnu.org, "Wei Wang" <wei.w.wang@intel.com>,
	"Peter Xu" <peterx@redhat.com>,
	"Philippe Mathieu-Daudé" <philmd@redhat.com>
Subject: Re: [PATCH v1 2/2] virtio-balloon: disallow postcopy with VIRTIO_BALLOON_F_FREE_PAGE_HINT
Date: Thu, 8 Jul 2021 20:07:44 +0100	[thread overview]
Message-ID: <YOdNAMoAKkiSyuqR@work-vm> (raw)
In-Reply-To: <20210707155413-mutt-send-email-mst@kernel.org>

* Michael S. Tsirkin (mst@redhat.com) wrote:
> On Wed, Jul 07, 2021 at 09:47:31PM +0200, David Hildenbrand wrote:
> > On 07.07.21 21:19, Michael S. Tsirkin wrote:
> > > On Wed, Jul 07, 2021 at 09:14:00PM +0200, David Hildenbrand wrote:
> > > > On 07.07.21 21:05, Michael S. Tsirkin wrote:
> > > > > On Wed, Jul 07, 2021 at 04:06:55PM +0200, David Hildenbrand wrote:
> > > > > > Postcopy never worked properly with 'free-page-hint=on', as there are
> > > > > > at least two issues:
> > > > > > 
> > > > > > 1) With postcopy, the guest will never receive a VIRTIO_BALLOON_CMD_ID_DONE
> > > > > >      and consequently won't release free pages back to the OS once
> > > > > >      migration finishes.
> > > > > > 
> > > > > >      The issue is that for postcopy, we won't do a final bitmap sync while
> > > > > >      the guest is stopped on the source and
> > > > > >      virtio_balloon_free_page_hint_notify() will only call
> > > > > >      virtio_balloon_free_page_done() on the source during
> > > > > >      PRECOPY_NOTIFY_CLEANUP, after the VM state was already migrated to
> > > > > >      the destination.
> > > > > > 
> > > > > > 2) Once the VM touches a page on the destination that has been excluded
> > > > > >      from migration on the source via qemu_guest_free_page_hint() while
> > > > > >      postcopy is active, that thread will stall until postcopy finishes
> > > > > >      and all threads are woken up. (with older Linux kernels that won't
> > > > > >      retry faults when woken up via userfaultfd, we might actually get a
> > > > > >      SEGFAULT)
> > > > > > 
> > > > > >      The issue is that the source will refuse to migrate any pages that
> > > > > >      are not marked as dirty in the dirty bmap -- for example, because the
> > > > > >      page might just have been sent. Consequently, the faulting thread will
> > > > > >      stall, waiting for the page to be migrated -- which could take quite
> > > > > >      a while and result in guest OS issues.
> > > > > 
> > > > > OK so if source gets a request for a page which is not dirty
> > > > > it does not respond immediately? Why not just teach it to
> > > > > respond? It would seem that if destination wants a page we
> > > > > should just give it to the destination ...
> > > > 
> > > > The source does not know if a page has already been sent (e.g., via the
> > > > background migration thread that moves all data over) vs. the page has not
> > > > been send because the page was hinted. This is the part where we'd need
> > > > additional tracking on the source to actually know that.
> > > > 
> > > > We must not send a page twice, otherwise bad things can happen when placing
> > > > pages that already have been migrated, because that scenario can easily
> > > > happen with ordinary postcopy (page has already been sent and we're dealing
> > > > with a stale request from the destination).
> > > 
> > > OK let me get this straight
> > > 
> > > A. source sends page
> > > B. destination requests page
> > > C. destination changes page
> > > D. source sends page
> > > E. destination overwrites page
> > > 
> > > this is what you are worried about right?
> > 
> > IIRC E. is with recent kernels:
> > 
> > E. placing the page fails with -EEXIST and postcopy migration fails
> > 
> > However, the man page (man ioctl_userfaultfd) doesn't describe what is
> > actually supposed to happen when double-placing. Could be that it's
> > "undefined behavior".
> > 
> > I did not try, though.
> > 
> > 
> > This is how it works today:
> > 
> > A. source sends page and marks it clean
> > B. destination requests page
> > C. destination receives page and places it
> > D. source ignores request as page is clean
> 
> If it's actually -EEXIST then we could just resend it
> and teach destination to ignore -EEXIST errors right?
> 
> Will actually make things a bit more robust: destination
> handles its own consistency instead of relying on source.

You have to be careful of a few things;
  a) If the destination has modified the page, then you must
definitely not under any circumstances lose those modifications
and replace them by an old version from the source.

  b) With postcopy recovery I think there is a bitmap to track some
of this; but you have to be careful since you don't know whether
pages that were sent were actually received.

Dave

> 
> 
> > > 
> > > the fix is to mark page clean in A.
> > > then in D to not send page if it's clean?
> > > 
> > > And the problem with hinting is this:
> > > 
> > > A. page is marked clean
> > > B. destination requests page
> > > C. destination changes page
> > > D. source sends page <- does not happen, page is clean!
> > > E. destination overwrites page
> > 
> > Simplified it's
> > 
> > A. page is marked clean by hinting code
> > B. destination requests page
> > D. source ignores request as page is clean
> > E. destination stalls until postcopy unregisters uffd
> > 
> > 
> > Some thoughts
> > 
> > 1. We do have a a recv bitmap where we track received pages on the
> > destination (e.g., ramblock_recv_bitmap_test()), however we only use it to
> > avoid sending duplicate requests to the hypervisor AFAIKs, and don't check
> > it when placing pages.
> > 
> > 2. Changing the migration behavior unconditionally on the source will break
> > migration to old QEMU binaries that cannot handle this change.
> 
> We can always make this depend on new machine types.
> 
> 
> > 3. I think the current behavior is in place to make debugging easier. If
> > only a single instance of a page will ever be migrated from source to
> > destination, there cannot be silent data corruption. Further, we avoid
> > migrating unnecessarily pages twice.
> > 
> 
> Likely does not matter much for performance, it seems unlikely that
> the race is all that common.
> 
> > Maybe Dave and Peter can spot any flaws in my understanding.
> > 
> > -- 
> > Thanks,
> > 
> > David / dhildenb
> 
-- 
Dr. David Alan Gilbert / dgilbert@redhat.com / Manchester, UK



  parent reply	other threads:[~2021-07-08 19:25 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-07 14:06 [PATCH v1 0/2] virtio-balloon: disallow postcopy with VIRTIO_BALLOON_F_FREE_PAGE_HINT David Hildenbrand
2021-07-07 14:06 ` [PATCH v1 1/2] migration/postcopy-ram: define type for "struct PostcopyNotifyData" David Hildenbrand
2021-07-07 20:30   ` Pankaj Gupta
2021-07-07 14:06 ` [PATCH v1 2/2] virtio-balloon: disallow postcopy with VIRTIO_BALLOON_F_FREE_PAGE_HINT David Hildenbrand
2021-07-07 18:02   ` Peter Xu
2021-07-07 18:57     ` David Hildenbrand
2021-07-07 19:07       ` Michael S. Tsirkin
2021-07-07 20:08       ` Peter Xu
2021-07-07 21:22         ` Alexander Duyck
2021-07-07 22:40           ` Peter Xu
2021-07-08  7:14             ` David Hildenbrand
2021-07-08  7:23           ` David Hildenbrand
2021-07-07 19:05   ` Michael S. Tsirkin
2021-07-07 19:14     ` David Hildenbrand
2021-07-07 19:19       ` Michael S. Tsirkin
2021-07-07 19:47         ` David Hildenbrand
2021-07-07 19:57           ` Michael S. Tsirkin
2021-07-08  8:19             ` David Hildenbrand
2021-07-08 19:07             ` Dr. David Alan Gilbert [this message]
2021-07-09 11:27               ` Michael S. Tsirkin

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=YOdNAMoAKkiSyuqR@work-vm \
    --to=dgilbert@redhat.com \
    --cc=alexander.duyck@gmail.com \
    --cc=david@redhat.com \
    --cc=mst@redhat.com \
    --cc=peterx@redhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    --cc=quintela@redhat.com \
    --cc=wei.w.wang@intel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.