All of lore.kernel.org
 help / color / mirror / Atom feed
From: Peter Xu <peterx@redhat.com>
To: Aadeshveer Singh <aadeshveer07@gmail.com>
Cc: qemu-devel@nongnu.org, farosas@suse.de, pbonzini@redhat.com,
	philmd@mailo.com, lvivier@redhat.com, ayoub@saferwall.com,
	pierrick.bouvier@oss.qualcomm.com
Subject: Re: [PATCH v4 07/11] migration: add support for fault thread to load pages from disk
Date: Wed, 12 Aug 2026 13:54:54 -0400	[thread overview]
Message-ID: <anyzbkQxrIuAo2Jf@x1.local> (raw)
In-Reply-To: <CA++cPvJ=nx=4EWHQX5yYO46Dq2Ng7-EjG5J6bhWapTtumeXdPg@mail.gmail.com>

On Wed, Aug 12, 2026 at 10:08:38PM +0530, Aadeshveer Singh wrote:
> On Mon, Aug 10, 2026 at 9:10 PM Peter Xu <peterx@redhat.com> wrote:
> >
> > Aadeshveer,
> >
> > Thanks for looking into this problem.  Below solution should work in
> > general, but there's still one problem..
> >
> > On Sat, Aug 08, 2026 at 10:04:48AM +0530, Aadeshveer Singh wrote:
> > > A subtle concurrency bug exists in the current patch; this small patch
> > > should fix it.
> > >
> > > Current patch uses bitmap_test_and_clear_atomic assuming it performs
> > > an atomic operation over a range, which as pointer out by Peter is not
> > > the case. It performs atomic word by word operations making the
> > > overall operation unsafe.
> > > Therefore, keeping the same bitmap would require protection by a mutex
> > > lock which might not be as efficient. Hence this small patch resizes
> > > the pending_bmap to have exactly one bit per load operation(loading
> > > the larger of host page and guest page).
> > >
> > > Thank you,
> > > Aadeshveer Singh
> > >
> > > diff --git a/migration/postcopy-ram.c b/migration/postcopy-ram.c
> > > index e01ac628f5..0bf0f837ad 100644
> > > --- a/migration/postcopy-ram.c
> > > +++ b/migration/postcopy-ram.c
> > > @@ -1031,9 +1031,13 @@ static bool
> > > postcopy_mapped_ram_load_page(MigrationIncomingState *mis,
> > >      host_page = rb_offset / qemu_ram_pagesize(rb);
> > >      page = rb_offset >> qemu_target_page_bits();
> > >
> > > -    if (bitmap_test_and_clear_atomic(
> > > -            rb->pending_bmap, host_page,
> > > -            MAX(1, qemu_target_page_size() / qemu_ram_pagesize(rb)))) {
> > > +    /*
> > > +     * pending_bmap needs the index of host or guest page based on which is
> > > +     * larger. As page index is inversely proportional to page size we use the
> > > +     * minimum of both.
> > > +     */
> > > +    if (bitmap_test_and_clear_atomic(rb->pending_bmap, MIN(host_page, page),
> > > +                                     1)) {
> > >          if (find_next_bit(rb->file_bmap, page + guest_pages_to_load, page) ==
> > >              page + guest_pages_to_load) {
> > >              /* It is efficient to use UFFDIO_ZERO if all pages are zero */
> >
> > Consider the case where guest psize > host psize, here the current code
> > will invoke postcopy_place_page_zero() or postcopy_place_page() only once
> > for each guest page. But IIUC that's not enough: we'll need to loop over
> > the few host pages that is covered by the same guest page.
> I believe I missed it, confusing the page sizes. One of the solutions
> can be a loop or a cleaner solution might be to give larger length to
> ioctl command to handle multiple pages at once. This should be
> efficient but might require some changes to basic postcopy code as
> most of it hardcoded length as page size to most userfaultfd_ioctl
> calls.

Yes the helpers were for one host page only.

Since you mentioned, I found that maybe the generic postcopy code has issue
with such special psize setup too.. see qemu_ufd_copy_ioctl() where it has:

        ramblock_recv_bitmap_set_range(rb, host_addr,
                                       pagesize / qemu_target_page_size());

Here pagesize should be host psize, which means if guest psize is larger
then it'll have nr==0, so QEMU is not properly updating the receivedmap in
postcopy phase.  It's highly likely nobody ran postcopy with such setup
before.

> >
> > I confess this is really a rare corner case..  so you can decide how to
> > "fix" it.  There's always the option to disable this feature for now when
> > guest psize is larger than host psize (OTOH, host psize > guest psize is
> > much more common, because that's normally how huge page backed VMs work on
> > linux systems).
> >
> > Or just provide the loops over host pages, I think it will start working
> > and IIUC it's indeed the most efficient.  But then, to make it slightly
> > easier to future readers (I still think the bitmap definition will be
> > slightly hard to grasp for future readers.. your comments all over
> > hopefully will help), maybe we can also rename "page"; it implies guest
> > page index but it's not obvious.  We can make it "guest_page" to match
> > "host_page".  The "haddr" seems fine.
> I believe it should be simple to implement using direct syscalls,
> however for a cleaner solution current functions need to be made more
> modular. I will try the latter and if the changes go out of hand might
> resort to direct syscalls or loops.

Let's avoid direct syscalls; it's not good for long term maintenability.

Please try to see if it's easy to fix this problem.  If it's time
consuming, let's fallback to fail the enablement of postcopy when guest
psize is larger, and leave it for later; such breakage should apply to
remote postcopy too.

Thanks,

-- 
Peter Xu



  reply	other threads:[~2026-08-12 17:55 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-01  2:36 [PATCH v4 00/11] migration: fast snapshot load Aadeshveer Singh
2026-08-01  2:36 ` [PATCH v4 01/11] migration: Propagate error in postcopy setup functions Aadeshveer Singh
2026-08-10 15:08   ` Juraj Marcin
2026-08-01  2:36 ` [PATCH v4 02/11] migration: Extract blocktime marking helper Aadeshveer Singh
2026-08-10 15:08   ` Juraj Marcin
2026-08-01  2:36 ` [PATCH v4 03/11] migration: Rename postcopy_listen_thread_bh Aadeshveer Singh
2026-08-10 15:09   ` Juraj Marcin
2026-08-01  2:36 ` [PATCH v4 04/11] migration: Use file_bmap for RAMBlock during incoming file load Aadeshveer Singh
2026-08-10 15:09   ` Juraj Marcin
2026-08-01  2:36 ` [PATCH v4 05/11] migration: Make qemu_get_buffer_at() thread-safe Aadeshveer Singh
2026-08-10 14:14   ` Peter Xu
2026-08-10 15:10   ` Juraj Marcin
2026-08-01  2:36 ` [PATCH v4 06/11] migration: add RAMBlock field and helper for fast snapshot load Aadeshveer Singh
2026-08-10 15:12   ` Juraj Marcin
2026-08-01  2:36 ` [PATCH v4 07/11] migration: add support for fault thread to load pages from disk Aadeshveer Singh
2026-08-08  4:34   ` Aadeshveer Singh
2026-08-10 15:40     ` Peter Xu
2026-08-12 16:38       ` Aadeshveer Singh
2026-08-12 17:54         ` Peter Xu [this message]
2026-08-01  2:36 ` [PATCH v4 08/11] migration: add eager load thread and setup for fast snapshot load Aadeshveer Singh
2026-08-10 15:48   ` Juraj Marcin
2026-08-01  2:36 ` [PATCH v4 09/11] migration: update capability conflict test for postcopy-ram+mapped-ram Aadeshveer Singh
2026-08-10 15:49   ` Juraj Marcin
2026-08-10 18:23   ` Peter Xu
2026-08-01  2:36 ` [PATCH v4 10/11] migration/tests: Add test for fast snapshot load Aadeshveer Singh
2026-08-10 15:50   ` Juraj Marcin
2026-08-01  2:36 ` [PATCH v4 11/11] docs/migration: Add documentation for fast snapshot load feature Aadeshveer Singh
2026-08-10 18:24   ` Peter Xu
2026-08-10 19:02 ` [PATCH v4 00/11] migration: fast snapshot load Peter Xu
2026-08-12 16:38   ` Aadeshveer Singh

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=anyzbkQxrIuAo2Jf@x1.local \
    --to=peterx@redhat.com \
    --cc=aadeshveer07@gmail.com \
    --cc=ayoub@saferwall.com \
    --cc=farosas@suse.de \
    --cc=lvivier@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@mailo.com \
    --cc=pierrick.bouvier@oss.qualcomm.com \
    --cc=qemu-devel@nongnu.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 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.