From: Fabiano Rosas <farosas@suse.de>
To: Peter Xu <peterx@redhat.com>
Cc: "Denis V. Lunev" <den@virtuozzo.com>,
"Denis V. Lunev" <den@openvz.org>,
qemu-devel@nongnu.org,
"Marc-André Lureau" <marcandre.lureau@redhat.com>,
"Laurent Vivier" <lvivier@redhat.com>
Subject: Re: [PATCH 0/2] migration: fix a source hang on cancel before the channel connects
Date: Fri, 27 Nov 2026 09:49:50 -0300 [thread overview]
Message-ID: <87v75iqw0h.fsf@suse.de> (raw)
In-Reply-To: <awl2GemtkJBdjcXk@zhexu-thinkpadt14gen5.rmtcaon.csb>
Peter Xu <peterx@redhat.com> writes:
> On Thu, Nov 26, 2026 at 06:20:03PM -0300, Fabiano Rosas wrote:
>> Peter Xu <peterx@redhat.com> writes:
>>
>> > On Thu, Nov 26, 2026 at 11:38:14AM -0300, Fabiano Rosas wrote:
>> >> "Denis V. Lunev" <den@virtuozzo.com> writes:
>> >>
>> >> > On 11/26/26 00:26, Fabiano Rosas wrote:
>> >> >> "Denis V. Lunev" <den@openvz.org> writes:
>> >> >>
>> >> >> What a coincidence, I just spent the afternoon fixing this.
>> >> >>
>> >> >>> Cancelling a migration right after starting it can hang the source
>> >> >>> QEMU forever. This happens when the cancel arrives while the source is
>> >> >>> still connecting to the destination and the connection then succeeds.
>> >> >>> The source goes on as if nothing was cancelled, then waits for a reply
>> >> >>> from the destination that never comes. It waits with the BQL held, so
>> >> >>> the monitor stops answering too, and the only way out is to kill the
>> >> >>> source. We hit this in an automated test that aborts a live migration
>> >> >>> right after starting it.
>> >> >> How does this test look like? I'm thinking that instead of writing a new
>> >> >> test for this bug (haven't read your patch 2 yet), what we need it to
>> >> >> remove the usage of migrate_wait_for_dirty_mem from the multifd cancel
>> >> >> test. I think that's an artificial delay that's there for no good reason
>> >> >> in this case. If your CI manages to hit the issue on a real-ish
>> >> >> scenario, that's even more reason to remove that code.
>> >> > This is old fashioned product testing over OpenStack cluster
>> >> > which specifically performing cancels in a row. Tests are run
>> >> > inside VMs inside the product and thus L0 host delays comes
>> >> > as a bit of fuzzing.
>> >> >
>> >>
>> >> That is good to know because I was not sure whether this issue could
>> >> happen outside of migration-test.
>> >>
>> >> > Tests are running non-stop inside development cycle :-)
>> >> >
>> >> > That is why I am coming with this or that problems on paths
>> >> > like that. But this is not very good in terms of QEMU level
>> >> > specific tests and that is why patch 2 comes with a pointing
>> >> > tests.
>> >> >
>> >> > NOTE: both your are my patches are fixed the problem as stated
>> >> > and the test passes without both your and my variants.
>> >> >
>> >> >>> Patch 2 adds a qtest for this window, which Peter left for later when
>> >> >>> fixing the early-cancel crash in 4f08b870f8 [1].
>> >> >>>
>> >> >>> [1] https://lore.kernel.org/r/20260421175820.302795-1-peterx@redhat.com
>> >> >>>
>> >> >>> Signed-off-by: Denis V. Lunev <den@openvz.org>
>> >> >>> Cc: Peter Xu <peterx@redhat.com>
>> >> >>> Cc: Fabiano Rosas <farosas@suse.de>
>> >> >>> Cc: Marc-André Lureau <marcandre.lureau@redhat.com>
>> >> >>> Cc: Laurent Vivier <lvivier@redhat.com>
>> >> >>>
>> >> >>> Denis V. Lunev (2):
>> >> >>> migration: fix source hang on cancel before the channel connects
>> >> >> Here's my version. It's very opinionated due to my long-standing grudge
>> >> >> with this code.
>> >> >> https://wiki.qemu.org/ToDo/LiveMigration#Migration_cancel_concurrency
>> >> >>
>> >> >> -- >8 --
>> >> >> From a27909503af9749f2cf6d8b901d675ee24c66d2c Mon Sep 17 00:00:00 2001
>> >> >> From: Fabiano Rosas <farosas@suse.de>
>> >> >> Date: Wed, 25 Nov 2026 18:43:57 -0300
>> >> >> Subject: [PATCH 1/2] migration: Avoid early cancel causing a hang
>> >> >>
>> >> >> If migration is cancelled at just the right moment when the main
>> >> >> channel transport has finished connecting, it's possible that the
>> >> >> shutdown()s issued by migrate_cancel() miss the (unset) QEMUFiles
>> >> >> entirely. A moment later, migration_channel_connect_outgoing() carries
>> >> >> along with the migration and initializes the QEMUFiles for the first
>> >> >> time.
>> >> >>
>> >> >> Migration then hangs because the source goes into migration_cleanup()
>> >> >> due to its state being CANCELLING and hangs waiting for the return
>> >> >> path thread to join, which never happens because the destination is
>> >> >> unaware and never sends any SHUT message. The cleanup itself also
>> >> >> doesn't kick the RP thread because commit 64763e8ca61 ("migration:
>> >> >> Keep s->rp_state.from_dst_file open until migration ends") has changed
>> >> >> the code to keep the QEMUFile until after the RP thread exits. The
>> >> >> only other way to kick it would be from
>> >> >> stop_return_path_thread_on_source() if an error had occurred, but no
>> >> >> error has occurred.
>> >> >>
>> >> >> This situation is not surprising because the migration cancelling has
>> >> >> never had any synchronization mechanism aside from the BQL, which in
>> >> >> this case is not enough to protect from the race. It has also never
>> >> >> had a way to inform the rest of the code in a thread safe manner about
>> >> >> the cancellation.
>> >> >>
>> >> >> To start moving in the correct direction, make the qmp_migrate_cancel
>> >> >> command (under BQL) set the migration error, which is protected by the
>> >> >> error_mutex and suitable to be used as a way to inform the other
>> >> >> threads of the cancellation. This avoids the need to check the
>> >> >> MigrationStatus which is already fragile in terms of concurrency.
>> >> >>
>> >> >> Note that the QMP handler for migrate_cancel is used instead of the
>> >> >> internal function because the latter is used from other parts of the
>> >> >> code that might have set s->error already.
>> >> >>
>> >> >> In the case where qmp_migrate_cancel() sets s->error and some other
>> >> >> code hits an error due to the shutdown()s, the
>> >> >> migrate_error_propagate() call chain will simply ignore the error set
>> >> >> for last. This is the correct behavior because errors after cancelling
>> >> >> are very likely to be caused by the cancellation itself. This has the
>> >> >> extra benefit that the migration cancellation now shows up in the
>> >> >> logs, which solves a confusing situation when the user sees "Broken
>> >> >> pipe" and thinks it's due to an error.
>> >> >>
>> >> >> For this specific bug, check that the asynchronous entry to
>> >> >> migration_channel_connect_outgoing() doesn't bring an already set
>> >> >> s->error and call migration_connect_error_propagate() under the BQL if
>> >> >> that's the case. We need the whole migration_connect_error_propagate()
>> >> >> here to handle the postcopy resume case as well as making sure
>> >> >> migration_cleanup() is reached.
>> >> >>
>> >> >> Reproduced with /x86_64/migration/multifd+postcopy/tcp/plain/cancel
>> >> >> and my stress branch, which basically skips
>> >> >> migrate_wait_for_dirty_mem().
>> >> >>
>> >> >> Signed-off-by: Fabiano Rosas <farosas@suse.de>
>> >> >> ---
>> >> >> migration/channel.c | 5 +++++
>> >> >> migration/migration.c | 8 ++++++++
>> >> >> 2 files changed, 13 insertions(+)
>> >> >>
>> >> >> diff --git a/migration/channel.c b/migration/channel.c
>> >> >> index 1aeb8814858..12ff68fc38c 100644
>> >> >> --- a/migration/channel.c
>> >> >> +++ b/migration/channel.c
>> >> >> @@ -563,6 +563,11 @@ void migration_channel_connect_outgoing(MigrationState *s, QIOChannel *ioc)
>> >> >> {
>> >> >> trace_migration_outgoing_channel_set(ioc, object_get_typename(OBJECT(ioc)));
>> >> >>
>> >> >> + if (migrate_has_error(s)) {
>> >> >> + migration_connect_error_propagate(s, error_copy(s->error));
>> >
>> > [1]
>> >
>> >> >> + return;
>> >> >> + }
>> >> >> +
>> >> >> if (migrate_channel_requires_tls_upgrade(ioc)) {
>> >> >> Error *local_err = NULL;
>> >> >>
>> >> >> diff --git a/migration/migration.c b/migration/migration.c
>> >> >> index 5686209ab11..40c3f21d520 100644
>> >> >> --- a/migration/migration.c
>> >> >> +++ b/migration/migration.c
>> >> >> @@ -1890,6 +1890,8 @@ void qmp_migrate_incoming(const char *uri, bool has_channels,
>> >> >> return;
>> >> >> }
>> >> >>
>> >> >> + migrate_error_free(migrate_get_current());
>> >
>> > Is this line somewhat separate from the issue? When s->error will be
>> > non-NULL?
>> >
>>
>> One of the cancel tests issues migrate_cancel(to), that puts the error
>> in s->error and since QEMU keeps running, a subsequent migration
>> crashes. We free the error at migrate_init() and migrate_prepare() for
>> similar reasons I think.
>
> But this is migrate_incoming, which means if something got wrong, instead
> of retrying, we should kill the qemu and re-initiate? I don't know if we
> could have other loopholes on this path to be invoked more than once.. as I
> never expected it to run twice (either success and take over, or halted for
> query error only, or killed).
>
I think this is only about calling cancel _without_ any migration:
$ (echo "migrate_cancel"; echo "quit") | ~/qemu-system-x86_64 -S -monitor stdio -incoming defer -trace migration_cancel
QEMU 11.1.92 monitor - type 'help' for more information
(qemu) migrate_cancel
migration_cancel
(qemu) quit
migration_cancel
So a state check in there could be good enough.
>>
>> >> >> +
>> >> >> if (!yank_register_instance(MIGRATION_YANK_INSTANCE, errp)) {
>> >> >> return;
>> >> >> }
>> >> >> @@ -2218,6 +2220,9 @@ out:
>> >> >>
>> >> >> void qmp_migrate_cancel(Error **errp)
>> >> >> {
>> >> >> + MigrationState *s = migrate_get_current();
>> >> >> + Error *local_err = NULL;
>> >> >> +
>> >> >> /*
>> >> >> * After postcopy migration has started, the source machine is not
>> >> >> * recoverable in case of a migration error. This also means the
>> >> >> @@ -2229,6 +2234,9 @@ void qmp_migrate_cancel(Error **errp)
>> >> >> return;
>> >> >> }
>> >> >>
>> >> >> + error_setg(&local_err, "Migration was cancelled by the user.");
>> >> >> + migrate_error_propagate(s, local_err);
>> >> >> +
>> >> >> migration_cancel();
>> >> >> }
>> >> >>
>> >> > I have spend a bit of time trying to weight against the approaches
>> >> > and the only item I found is that "cancel" is actually not a error
>> >> > while we are reusing field as a error state.
>> >> >
>> >>
>> >> That cat is out of the bag already since commit dc487044d5c ("migration:
>> >> Make migration_has_failed() work even for CANCELLING"). If
>> >> migration_has_failed() includes cancel states, then I think it's
>> >> actually a bug that migrate_cancel() doesn't set an error. Otherwise
>> >> code that calls migration_has_failed() will have to check
>> >> migrate_has_error() to find out if the "failure was an error". That's a
>> >> worse pattern in my opinion.
>> >
>> > Indeed, at least for qemufile it's the same, where qemu_file_shutdown()
>> > sets error for the qemufile error (PS: every time I see these bunch of
>> > error flags I headache..).
>> >
>> >>
>> >> That's how I came up with this patch actually because I was trying to
>> >> avoid checking SETUP specifically, in case something else races and sets
>> >> another state in the mean time. As I mentioned briefly in the commit
>> >> message, the MigrationStatus is not thread-safe, that cmpxchg only
>> >> protects the migration event against a duplicated state change.
>> >>
>> >> > Both are working. If you will take your one - pls consider
>> >> >
>> >> > Tested-by: Denis V. Lunev <den@openvz.org>
>> >> > Reviewed-by: Denis V. Lunev <den@openvz.org>
>> >> >
>> >> > I would say - up to you :-)
>> >> >
>> >>
>> >> Thank you. Let's see what Peter thinks about it.
>> >
>> > I agree with you that if we already have code treating cancellation as part
>> > of error, then setting error may make things more aligned.
>> >
>> > My only complaint might be that above [1], it has "if there is an error,
>> > duplicate that error and try to inject the same error, even if we're
>> > pretty sure is already there, so the dup one will be freed". It was almost
>> > a malloc/free unit test..
>> >
>>
>> Hmm, maybe NULL is indeed better, let me check. We just need to make
>> sure the error is set because the RP closing path then gets one more
>> chance to call shutdown(). But yeah, migrate_has_error() already
>> guarantees that.
>
> Yep.
>
>>
>> > So.. maybe Fabiano's patch, but use the migrate_cleanup() from Denis
>> > instead only at line [1]?
>>
>> As you noticed, it has to be migration_connect_error_propagate() because
>> it handles postcopy. I also don't want to have one more loose call to
>> migration_cleanup(), it makes it difficult to think about concurrency
>> bugs. The more we can centralize on a single error path, the better.
>
> Yes I agree.
next prev parent reply other threads:[~2026-11-27 12:50 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-25 19:31 [PATCH 0/2] migration: fix a source hang on cancel before the channel connects Denis V. Lunev
2026-11-25 19:31 ` [PATCH 1/2] migration: fix " Denis V. Lunev
2026-11-25 19:31 ` [PATCH 2/2] tests/qtest/migration: test " Denis V. Lunev
2026-11-26 19:33 ` Peter Xu
2026-11-25 23:26 ` [PATCH 0/2] migration: fix a source hang on " Fabiano Rosas
2026-11-26 14:02 ` Denis V. Lunev
2026-11-26 14:38 ` Fabiano Rosas
2026-11-26 19:32 ` Peter Xu
2026-11-26 19:39 ` Peter Xu
2026-11-26 21:20 ` Fabiano Rosas
2026-11-27 12:22 ` Peter Xu
2026-11-27 12:49 ` Fabiano Rosas [this message]
2026-11-27 13:28 ` Peter Xu
2026-11-27 14:21 ` Fabiano Rosas
2026-11-27 14:59 ` Peter Xu
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=87v75iqw0h.fsf@suse.de \
--to=farosas@suse.de \
--cc=den@openvz.org \
--cc=den@virtuozzo.com \
--cc=lvivier@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=peterx@redhat.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.