From nobody Wed May  7 21:47:42 2025
Delivered-To: importer@patchew.org
Authentication-Results: mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass(p=none dis=none)  header.from=redhat.com
ARC-Seal: i=1; a=rsa-sha256; t=1733526638; cv=none;
	d=zohomail.com; s=zohoarc;
	b=Yzu+G0ZZ5D1TuDSJ2PL09tQ3SvyB7K8q3Piw+rY5dkg+B1y45bfMF6RWbz2gzcFH3FdSHvqm3Tn7L/SKkcuPgXUcrw/xvnbgfgBuRuQ3F5K7+GT/NGIEq5Lrydnty7ZWkpiX0OtHd9Cq/WJBPKVHVZiCob6HMdahzKmHs+3dLjE=
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com;
 s=zohoarc;
	t=1733526638;
 h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To;
	bh=YeSfgc7Jyo9nB/kprDl4lAEUoTylMcAzUPDJAOUL/0Y=;
	b=UOzwvkoGd9yoftzxIJfWs0ysn9BCrVZ1sph1jyIO5RhW87cGXPBPTbUYjs6BdJCVO9p+QGmgNQo+dmVkTQ/bDZijR++2Ryb0jKdFni+KSRqj5gxGZWMe94AD8M8nerjQ0tcLDKwq0mNJN1U171KZdThy22u0tJRviNDy3rbGNlY=
ARC-Authentication-Results: i=1; mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass header.from=<peterx@redhat.com> (p=none dis=none)
Return-Path: <qemu-devel-bounces+importer=patchew.org@nongnu.org>
Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by
 mx.zohomail.com
	with SMTPS id 1733526638416513.8473707711454;
 Fri, 6 Dec 2024 15:10:38 -0800 (PST)
Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <qemu-devel-bounces@nongnu.org>)
	id 1tJhRL-00031q-3D; Fri, 06 Dec 2024 18:08:51 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRJ-0002zy-2w
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:49 -0500
Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRH-0005oW-27
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:48 -0500
Received: from mail-io1-f69.google.com (mail-io1-f69.google.com
 [209.85.166.69]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id
 us-mta-524-mEmT4ZSsMmmh76IKKtBB0w-1; Fri, 06 Dec 2024 18:08:44 -0500
Received: by mail-io1-f69.google.com with SMTP id
 ca18e2360f4ac-8418612c3b5so233686039f.3
 for <qemu-devel@nongnu.org>; Fri, 06 Dec 2024 15:08:44 -0800 (PST)
Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com.
 [99.254.114.190]) by smtp.gmail.com with ESMTPSA id
 8926c6da1cb9f-4e2861d9a8dsm1038895173.86.2024.12.06.15.08.40
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 06 Dec 2024 15:08:41 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1733526526;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:cc:mime-version:mime-version:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=YeSfgc7Jyo9nB/kprDl4lAEUoTylMcAzUPDJAOUL/0Y=;
 b=QrETPzxfsPiPpl04J3ieuCEfn+05sl8TMxxL2kohQ7PHc3Wx/CuYOHDeUw/7YOIbfuEpyu
 RQinNp3tVR/N3THYxHPAzDhRumaYPZlfCNyFz55ca4gUSYnXkLsleIc31TtsJ3C3gcFkUr
 GW5+KKeJkEseyCH86fuDZ5Zxcc9wi2U=
X-MC-Unique: mEmT4ZSsMmmh76IKKtBB0w-1
X-Mimecast-MFC-AGG-ID: mEmT4ZSsMmmh76IKKtBB0w
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1733526523; x=1734131323;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=YeSfgc7Jyo9nB/kprDl4lAEUoTylMcAzUPDJAOUL/0Y=;
 b=LS91gTDPlvKmYNImtUS3rcTYEbqbuD0ivo7Bh+4a6KEFXedDk938sSTR1anCNJjdzf
 4tVJQnjWEDsVKhpao2qqTfP+ZPPAfxfvteptIKutedHMG8N7R5bcppFOidRuJ1/jJzmu
 /8VIxG9/M+ThmT3SkVZEvw99hoeX3VBDDyKe+/ZADM3U3AbSpBxvAyAkV+GUFAINq4GY
 gmwF0xF83Pyl53uALi0QIy6WKabZk7kJk0OXYQy39GW3itHbKjJ33HgZpw3iHYoPOqqI
 4a2JJN6p9p/rBqg83oajqpt6RJpReEV9Y5AUmas7BSEFsokkn3pD/3UkvEOGZuhWKIwW
 F4ow==
X-Gm-Message-State: AOJu0YyJhhpop7PKi8cDidauG3x2raVmFEQTHp6YmhUmgV04BgM9DH45
 rnaLGxG/xKvKjQ1EBZS5gPDVy0QdtObbtdMIsyLOYsMBw06azkaY3rsIDiNGtTeG1bpJxPiAiu/
 pc/ogCuDQ1j+1OBbW2eRllvc/yJhx7dQb0CrjPIQuye/drqpiCFpRGMepzGb9NCyYFZ+MHK/ZxU
 VF5pqFI9gBG+sQ/NAVs5tFGEWSVzxBQUHwkg==
X-Gm-Gg: ASbGncv1j1rns5hEZMEXqnglEIlCHwuys3YApoKq/HWiYFUF9VbWRw1jr0LUyL6o4lS
 cc9GJl1vKMPZ3d4/TrEAPwnWJB2r6EnW2VlOO+w1x347tqlGNnB9n7CMiGUnXM/yRZKDCa13IHN
 vJWezvGE7m1C8se8mvug2eACg5HDw+XQt3uiJ8RTL3Sf/jOWhfreUR+eIhdo6pG0zkwXFxljZhG
 JAYiBxXFy4d3JPvYgUd98p5ANKzPSdFR1t7L87j6SAjYAnjazx7f3wbz7eovHcLoO3WfrJ8mP9Z
 LpejSZgEbN9PdDlipJUbam7OBA==
X-Received: by 2002:a05:6e02:1c43:b0:3a7:c3aa:a82b with SMTP id
 e9e14a558f8ab-3a811dae3b7mr61956095ab.1.1733526522842;
 Fri, 06 Dec 2024 15:08:42 -0800 (PST)
X-Google-Smtp-Source: 
 AGHT+IHtpW+lr+3gAKOHU9LGhBgNRJRDHf+1H5T+17CLUh7VcjL60p2b8j+a/CiPIF24GgDa2oiXIw==
X-Received: by 2002:a05:6e02:1c43:b0:3a7:c3aa:a82b with SMTP id
 e9e14a558f8ab-3a811dae3b7mr61955835ab.1.1733526522455;
 Fri, 06 Dec 2024 15:08:42 -0800 (PST)
From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: peterx@redhat.com, qemu-block@nongnu.org,
 Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>,
 Stefan Hajnoczi <stefanha@redhat.com>, Fabiano Rosas <farosas@suse.de>,
 Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>,
 Eric Blake <eblake@redhat.com>,
 "Dr . David Alan Gilbert" <dave@treblig.org>,
 Kevin Wolf <kwolf@redhat.com>,
 =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= <berrange@redhat.com>
Subject: [PATCH v2 1/6] migration: Add helper to get target runstate
Date: Fri,  6 Dec 2024 18:08:33 -0500
Message-ID: <20241206230838.1111496-2-peterx@redhat.com>
X-Mailer: git-send-email 2.47.0
In-Reply-To: <20241206230838.1111496-1-peterx@redhat.com>
References: <20241206230838.1111496-1-peterx@redhat.com>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17
 as permitted sender) client-ip=209.51.188.17;
 envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org;
 helo=lists.gnu.org;
Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com;
 helo=us-smtp-delivery-124.mimecast.com
X-Spam_score_int: -50
X-Spam_score: -5.1
X-Spam_bar: -----
X-Spam_report: (-5.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996,
 DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001,
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001,
 RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: qemu-devel@nongnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: <qemu-devel.nongnu.org>
List-Unsubscribe: <https://lists.nongnu.org/mailman/options/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>
List-Archive: <https://lists.nongnu.org/archive/html/qemu-devel>
List-Post: <mailto:qemu-devel@nongnu.org>
List-Help: <mailto:qemu-devel-request@nongnu.org?subject=help>
List-Subscribe: <https://lists.nongnu.org/mailman/listinfo/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=subscribe>
Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org
Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org
X-ZohoMail-DKIM: pass (identity @redhat.com)
X-ZM-MESSAGEID: 1733526640240116600
Content-Type: text/plain; charset="utf-8"

In 99% cases, after QEMU migrates to dest host, it tries to detect the
target VM runstate using global_state_get_runstate().

There's one outlier so far which is Xen that won't send global state.
That's the major reason why global_state_received() check was always there
together with global_state_get_runstate().

However it's utterly confusing why global_state_received() has anything to
do with "let's start VM or not".

Provide a helper to explain it, then we have an unified entry for getting
the target dest QEMU runstate after migration.

Suggested-by: Fabiano Rosas <farosas@suse.de>
Signed-off-by: Peter Xu <peterx@redhat.com>
---
 migration/migration.c | 21 +++++++++++++++++----
 1 file changed, 17 insertions(+), 4 deletions(-)

diff --git a/migration/migration.c b/migration/migration.c
index 8c5bd0a75c..d2a6b939cf 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -135,6 +135,21 @@ static bool migration_needs_multiple_sockets(void)
     return migrate_multifd() || migrate_postcopy_preempt();
 }
=20
+static RunState migration_get_target_runstate(void)
+{
+    /*
+     * When the global state is not migrated, it means we don't know the
+     * runstate of the src QEMU.  We don't have much choice but assuming
+     * the VM is running.  NOTE: this is pretty rare case, so far only Xen
+     * uses it.
+     */
+    if (!global_state_received()) {
+        return RUN_STATE_RUNNING;
+    }
+
+    return global_state_get_runstate();
+}
+
 static bool transport_supports_multi_channels(MigrationAddress *addr)
 {
     if (addr->transport =3D=3D MIGRATION_ADDRESS_TYPE_SOCKET) {
@@ -735,8 +750,7 @@ static void process_incoming_migration_bh(void *opaque)
      * unless we really are starting the VM.
      */
     if (!migrate_late_block_activate() ||
-         (autostart && (!global_state_received() ||
-            runstate_is_live(global_state_get_runstate())))) {
+        (autostart && runstate_is_live(migration_get_target_runstate()))) {
         /* Make sure all file formats throw away their mutable metadata.
          * If we get an error here, just don't restart the VM yet. */
         bdrv_activate_all(&local_err);
@@ -759,8 +773,7 @@ static void process_incoming_migration_bh(void *opaque)
=20
     dirty_bitmap_mig_before_vm_start();
=20
-    if (!global_state_received() ||
-        runstate_is_live(global_state_get_runstate())) {
+    if (runstate_is_live(migration_get_target_runstate())) {
         if (autostart) {
             vm_start();
         } else {
--=20
2.47.0
From nobody Wed May  7 21:47:42 2025
Delivered-To: importer@patchew.org
Authentication-Results: mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass(p=none dis=none)  header.from=redhat.com
ARC-Seal: i=1; a=rsa-sha256; t=1733526635; cv=none;
	d=zohomail.com; s=zohoarc;
	b=C4llLAYL+RVDt0USumCY3p6IU3iNL8E6xc6XKopBZt1hJ7LGkxMqTL0bn3uI3ms5uOYbD18wXTLLpaK3vwXFXTh4Z/e8y49XGZEXhsAxeQnTgzBoGSmYx4jm4+DalYlYnjmXKsQYFiK7sU0+G0BjWHTnrLp8b/npApjeDIL0ICM=
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com;
 s=zohoarc;
	t=1733526635;
 h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To;
	bh=ei3jTCfsOHArePmV6xGiFJK6jrCceSkuxpU8srZa3AM=;
	b=S+V8kbP7FV8EdlbrdWbp6IHMbMO0+JOSecBaV3DRL0vI70Et5Ygqsjfeox6m4dHjUxN3Kwjuau+g9I5iWRRxjsVFTv+uJgCGlLuiUTKGHo8xsvVNAhCe7jh+Oe9/tamKHUxuvvE9C4wvqaf7INDtJVN00d+U8iQwXMCzkNjzEJw=
ARC-Authentication-Results: i=1; mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass header.from=<peterx@redhat.com> (p=none dis=none)
Return-Path: <qemu-devel-bounces+importer=patchew.org@nongnu.org>
Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by
 mx.zohomail.com
	with SMTPS id 1733526635691206.33832942343395;
 Fri, 6 Dec 2024 15:10:35 -0800 (PST)
Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <qemu-devel-bounces@nongnu.org>)
	id 1tJhRL-00032A-I1; Fri, 06 Dec 2024 18:08:51 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRJ-00030P-Bs
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:49 -0500
Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRH-0005od-P9
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:49 -0500
Received: from mail-io1-f71.google.com (mail-io1-f71.google.com
 [209.85.166.71]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id
 us-mta-551-v7NFAoYIM12E8tAYHP2k5w-1; Fri, 06 Dec 2024 18:08:46 -0500
Received: by mail-io1-f71.google.com with SMTP id
 ca18e2360f4ac-843e5314cb8so236175239f.0
 for <qemu-devel@nongnu.org>; Fri, 06 Dec 2024 15:08:45 -0800 (PST)
Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com.
 [99.254.114.190]) by smtp.gmail.com with ESMTPSA id
 8926c6da1cb9f-4e2861d9a8dsm1038895173.86.2024.12.06.15.08.42
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 06 Dec 2024 15:08:43 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1733526527;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:cc:mime-version:mime-version:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=ei3jTCfsOHArePmV6xGiFJK6jrCceSkuxpU8srZa3AM=;
 b=bW1wyex20bRPv3t5ZAtZVop2fTB5Nmc561hfy/GaY90qi+96nLpkT61aXAIHXFY7BfP3dt
 rDLIqfdI61xvR5Z9FRLSGUG+KYT6AShsAk+CnjTes5b1CgQojaNsuB9HzcndcOiD4L8sbC
 7E9wtkf06SWM0HhCJK5oxN3fu1j36Qk=
X-MC-Unique: v7NFAoYIM12E8tAYHP2k5w-1
X-Mimecast-MFC-AGG-ID: v7NFAoYIM12E8tAYHP2k5w
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1733526524; x=1734131324;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=ei3jTCfsOHArePmV6xGiFJK6jrCceSkuxpU8srZa3AM=;
 b=DK1bZXJuEWdFoUDonAHY9lHiI8TXPfSBis/HxuJpytcyRHhFlJdL9WF5lP8/UP3TwC
 uu/QgbkQz0J8+grcWSfC+CLCXGVWvyIfSYXku0jJTTW80ClNfJrYEk28VQIDxJluDLhJ
 R56ZJj6/7uf9/eIbd4PRy9M1pt+QEd/fs053VjayYpic3I9Byc8K9XKFaMnMYTiL38fC
 sS2CMdgeNtTS/y6PAcB18UhqanYMU76HBIqxalwOw4Ng79ezEG9ffrzsn5KqGhEtOP44
 vE+nA1g1KbSZqQGHL5+HQ2UVeXJARGnbgbPClBxmBVNzNFsmmgUJWFciDzlX4XgvEa7D
 strw==
X-Gm-Message-State: AOJu0Yz8PD8SROuR+urs73jXuG/Zy6khoijGjXo3idjWbgGYxBP+X9Zl
 foPVwPn+RkWLwrjIQDm9nt0jF4fcfee9g1hvy2Bscm94HHeYqdqyBWzUUkA2wymbLOxT2R6ainl
 EXpjGXlV6eJDCd3zKkMAa00jZ/aa9/YbvuvhvbdNGFirlprdfeZ+kOwNsyBUlS9nKDn5kcXkCRY
 mBPSlOXd2wkode0Ob+QkOhh1nbbEIZ4F17nw==
X-Gm-Gg: ASbGncuCJzE3dX7tRLC+jnfypKZ3VUz/GokoF9V9UNA6l7AHX9ZcXrg8bll/3brINcw
 te1zqA7lsSIPv0JJPG/15WBy5cAKXzJg4i0ozOllNzgwuAE6rN/lYZ6qu7WMSnNajJXQj3TDDik
 00RzTuq3IuD+gv+8Qu8K7QaIcDe5ST9zg7qvlrtF8/vL20bY6kQ50XrhPrrU7d/ji/o+Ecq0EEt
 IRXEvJR2XcL8za93a/pI2ehN2mv0y50tBsQcNJR0osxWcqb3Fwmoe0uld4YiGmqsXWdCr9DkI86
 iJ09O03oF7wqtKt0bl6RfsJb1Q==
X-Received: by 2002:a05:6602:6b10:b0:83d:ff89:218c with SMTP id
 ca18e2360f4ac-8447e23c40bmr558746439f.7.1733526524543;
 Fri, 06 Dec 2024 15:08:44 -0800 (PST)
X-Google-Smtp-Source: 
 AGHT+IElYceywDKXSww4XW5Xm8jXV1AUqUnfAH+yucG7kZdg7Rnp3sbDc05RnxLyAqlVKujDTb2a+w==
X-Received: by 2002:a05:6602:6b10:b0:83d:ff89:218c with SMTP id
 ca18e2360f4ac-8447e23c40bmr558742639f.7.1733526524065;
 Fri, 06 Dec 2024 15:08:44 -0800 (PST)
From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: peterx@redhat.com, qemu-block@nongnu.org,
 Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>,
 Stefan Hajnoczi <stefanha@redhat.com>, Fabiano Rosas <farosas@suse.de>,
 Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>,
 Eric Blake <eblake@redhat.com>,
 "Dr . David Alan Gilbert" <dave@treblig.org>,
 Kevin Wolf <kwolf@redhat.com>,
 =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= <berrange@redhat.com>,
 Markus Armbruster <armbru@redhat.com>
Subject: [PATCH v2 2/6] qmp/cont: Only activate disks if migration completed
Date: Fri,  6 Dec 2024 18:08:34 -0500
Message-ID: <20241206230838.1111496-3-peterx@redhat.com>
X-Mailer: git-send-email 2.47.0
In-Reply-To: <20241206230838.1111496-1-peterx@redhat.com>
References: <20241206230838.1111496-1-peterx@redhat.com>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17
 as permitted sender) client-ip=209.51.188.17;
 envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org;
 helo=lists.gnu.org;
Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com;
 helo=us-smtp-delivery-124.mimecast.com
X-Spam_score_int: -50
X-Spam_score: -5.1
X-Spam_bar: -----
X-Spam_report: (-5.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996,
 DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001,
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001,
 RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no
X-Spam_action: no action
X-BeenThere: qemu-devel@nongnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: <qemu-devel.nongnu.org>
List-Unsubscribe: <https://lists.nongnu.org/mailman/options/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>
List-Archive: <https://lists.nongnu.org/archive/html/qemu-devel>
List-Post: <mailto:qemu-devel@nongnu.org>
List-Help: <mailto:qemu-devel-request@nongnu.org?subject=help>
List-Subscribe: <https://lists.nongnu.org/mailman/listinfo/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=subscribe>
Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org
Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org
X-ZohoMail-DKIM: pass (identity @redhat.com)
X-ZM-MESSAGEID: 1733526636367116600
Content-Type: text/plain; charset="utf-8"

As the comment says, the activation of disks is for the case where
migration has completed, rather than when QEMU is still during
migration (RUN_STATE_INMIGRATE).

Move the code over to reflect what the comment is describing.

Cc: Kevin Wolf <kwolf@redhat.com>
Cc: Markus Armbruster <armbru@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Fabiano Rosas <farosas@suse.de>
---
 monitor/qmp-cmds.c | 26 ++++++++++++++------------
 1 file changed, 14 insertions(+), 12 deletions(-)

diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c
index f84a0dc523..76f21e8af3 100644
--- a/monitor/qmp-cmds.c
+++ b/monitor/qmp-cmds.c
@@ -96,21 +96,23 @@ void qmp_cont(Error **errp)
         }
     }
=20
-    /* Continuing after completed migration. Images have been inactivated =
to
-     * allow the destination to take control. Need to get control back now.
-     *
-     * If there are no inactive block nodes (e.g. because the VM was just
-     * paused rather than completing a migration), bdrv_inactivate_all() s=
imply
-     * doesn't do anything. */
-    bdrv_activate_all(&local_err);
-    if (local_err) {
-        error_propagate(errp, local_err);
-        return;
-    }
-
     if (runstate_check(RUN_STATE_INMIGRATE)) {
         autostart =3D 1;
     } else {
+        /*
+         * Continuing after completed migration. Images have been
+         * inactivated to allow the destination to take control. Need to
+         * get control back now.
+         *
+         * If there are no inactive block nodes (e.g. because the VM was
+         * just paused rather than completing a migration),
+         * bdrv_inactivate_all() simply doesn't do anything.
+         */
+        bdrv_activate_all(&local_err);
+        if (local_err) {
+            error_propagate(errp, local_err);
+            return;
+        }
         vm_start();
     }
 }
--=20
2.47.0
From nobody Wed May  7 21:47:42 2025
Delivered-To: importer@patchew.org
Authentication-Results: mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass(p=none dis=none)  header.from=redhat.com
ARC-Seal: i=1; a=rsa-sha256; t=1733526642; cv=none;
	d=zohomail.com; s=zohoarc;
	b=g9UdUZZ/Piv/ewrzx0ohcZHBRik6kHgEGMGMjYJ0U4CrNpECbXo8plaE9FDF66Hn+mgdyCwQhx9taZ8rze9JEfSDqWK5J1/IsjxHdyJtpbx2efvisHKYCiEyVMAWW6VGPTvshCvJC6r0LR0vjQaoULfSabi9GK1rbfja1oF7uqA=
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com;
 s=zohoarc;
	t=1733526642;
 h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To;
	bh=bj8MGswrXWKOgEfQ2x9IDB2aXSksN2FVHW5jduhyU+Q=;
	b=JSLXnV27mk5kMzVLYWCMYXQCKkoJKhBwYkSp+U4KTXUVgBALc/oaAbQZfeobNLSSG1M+ORrvCtXKpOCSH3OIW6aGyxOzLa2cAvm9tiX652UDPKafqzl0AAXAAene0uwI1VULTh2r/iAZv6TLaDiFJm7HuED0zgpxvSe7hmbybGk=
ARC-Authentication-Results: i=1; mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass header.from=<peterx@redhat.com> (p=none dis=none)
Return-Path: <qemu-devel-bounces+importer=patchew.org@nongnu.org>
Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by
 mx.zohomail.com
	with SMTPS id 1733526642439802.7226963435076;
 Fri, 6 Dec 2024 15:10:42 -0800 (PST)
Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <qemu-devel-bounces@nongnu.org>)
	id 1tJhRN-00032c-6F; Fri, 06 Dec 2024 18:08:53 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRL-00031p-1g
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:51 -0500
Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRJ-0005pD-Ia
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:50 -0500
Received: from mail-io1-f70.google.com (mail-io1-f70.google.com
 [209.85.166.70]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id
 us-mta-94-hli23DxeNsWWZEWRLgWGpA-1; Fri, 06 Dec 2024 18:08:47 -0500
Received: by mail-io1-f70.google.com with SMTP id
 ca18e2360f4ac-843eac1b9dbso231716439f.0
 for <qemu-devel@nongnu.org>; Fri, 06 Dec 2024 15:08:47 -0800 (PST)
Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com.
 [99.254.114.190]) by smtp.gmail.com with ESMTPSA id
 8926c6da1cb9f-4e2861d9a8dsm1038895173.86.2024.12.06.15.08.44
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 06 Dec 2024 15:08:44 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1733526529;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:cc:mime-version:mime-version:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=bj8MGswrXWKOgEfQ2x9IDB2aXSksN2FVHW5jduhyU+Q=;
 b=A8r04v2/BpN4jrt1Il3eSaFeoIzXc9dg6zDOTeqaSIMX335/JdMZ9nDxVOg8CyTTC2Nn9I
 mPxTYstsBPmFGCBwDZ+A9o9oTFtcI8Gu7+Em3MkyAPlVXVbJ4SE4Hn8Dv9p5380J4Fw54U
 /zU2+lz/7RUBMZFAMFQBoG87WnPn4yg=
X-MC-Unique: hli23DxeNsWWZEWRLgWGpA-1
X-Mimecast-MFC-AGG-ID: hli23DxeNsWWZEWRLgWGpA
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1733526526; x=1734131326;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=bj8MGswrXWKOgEfQ2x9IDB2aXSksN2FVHW5jduhyU+Q=;
 b=IZqpu3qqvPRZUJGZvYGcgGS+UStqeuJ2G6mUCMdkSWcQfKekrSUGhTaLg4XkXmi8+X
 a1VNQZKjT4qPSc7mdPVT2eT4tJ7rpSQlyPlBn0hkcLFoOqCGRK2PM/olYpLZ8WaymTzn
 dlN7QR0N3PAdlo50y12QFNDn3RgxKjjUB1RndUcAOi4aSsb4dgE+OneL8lo430kLwzvJ
 DJcWmeNcMw/Sg2ubihfAoNP1djkteLG4ERaEGUrJXKPNIzgXsr/sUagw4yOiMWBdNXVU
 xYlzPeAMdJiqbNDjNmsnsZsCDBJeX5+9dwipCSk5UphbYm4OhOVJ5eYh2wJU39McDKjE
 x5Mw==
X-Gm-Message-State: AOJu0YyObsqsF8lEBbr/VVkF4XShSnXMZVrWg2j9vuk8sceyBOseaMVa
 P9k+xO8JiJzNl0HXs5SVdi8Xjs5vPmq+Kz+zy5KN478UvMBJI3AOv7OPlE7AsQXq2fFYlBYWLoc
 Yx54pImf7GVYaGRSgK6UfDphzjzJLnhUb6+AIaROp5f09qvtjDJvm9ynGOwQ/LUjUmNeSI7lERz
 uA551Yh22uUYDoISkAbc2lJc9b4gP1Nr9eZw==
X-Gm-Gg: ASbGnct361u5/QFvrSUzSrSgBn9Aln12cWPLhoZdZj64ayBV/TUH3DcK1GpNhBBAY/E
 X7qljPCOXeysiIk9uD+5z0BL6WvmwTW4QLZuchnhgwMYfpSSW3YNTXzPZAce8G0eMk5fXFWXT31
 /gnBLzAlCW//5tjlKLyKy0UyQV5gzu0nIEIQpcEjKIvRIcbd3cJVH1WPyx1KbJ540HVyG1Z5T3M
 FGJUSniiebwPRcrdp8N9A4pL5F1MTn8QOC5tXXCA/KupjHDglOdN++n2rErgFOo4MiyrvtH+NCa
 k0FDd6cZt8VK6j5bzSjDnlUQuA==
X-Received: by 2002:a05:6e02:1c43:b0:3a7:c3aa:a82b with SMTP id
 e9e14a558f8ab-3a811dae3b7mr61957695ab.1.1733526526336;
 Fri, 06 Dec 2024 15:08:46 -0800 (PST)
X-Google-Smtp-Source: 
 AGHT+IFwTFoqMI0boqnnrTCMSIJ/MaBO3VmAH2bAWjST7Ns0G37MN56nMw+NdMUHYywLIoKDyN2qlQ==
X-Received: by 2002:a05:6e02:1c43:b0:3a7:c3aa:a82b with SMTP id
 e9e14a558f8ab-3a811dae3b7mr61957445ab.1.1733526526004;
 Fri, 06 Dec 2024 15:08:46 -0800 (PST)
From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: peterx@redhat.com, qemu-block@nongnu.org,
 Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>,
 Stefan Hajnoczi <stefanha@redhat.com>, Fabiano Rosas <farosas@suse.de>,
 Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>,
 Eric Blake <eblake@redhat.com>,
 "Dr . David Alan Gilbert" <dave@treblig.org>,
 Kevin Wolf <kwolf@redhat.com>,
 =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= <berrange@redhat.com>
Subject: [PATCH v2 3/6] migration/block: Make late-block-active the default
Date: Fri,  6 Dec 2024 18:08:35 -0500
Message-ID: <20241206230838.1111496-4-peterx@redhat.com>
X-Mailer: git-send-email 2.47.0
In-Reply-To: <20241206230838.1111496-1-peterx@redhat.com>
References: <20241206230838.1111496-1-peterx@redhat.com>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17
 as permitted sender) client-ip=209.51.188.17;
 envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org;
 helo=lists.gnu.org;
Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com;
 helo=us-smtp-delivery-124.mimecast.com
X-Spam_score_int: -50
X-Spam_score: -5.1
X-Spam_bar: -----
X-Spam_report: (-5.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996,
 DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001,
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001,
 RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no
X-Spam_action: no action
X-BeenThere: qemu-devel@nongnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: <qemu-devel.nongnu.org>
List-Unsubscribe: <https://lists.nongnu.org/mailman/options/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>
List-Archive: <https://lists.nongnu.org/archive/html/qemu-devel>
List-Post: <mailto:qemu-devel@nongnu.org>
List-Help: <mailto:qemu-devel-request@nongnu.org?subject=help>
List-Subscribe: <https://lists.nongnu.org/mailman/listinfo/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=subscribe>
Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org
Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org
X-ZohoMail-DKIM: pass (identity @redhat.com)
X-ZM-MESSAGEID: 1733526644642116600
Content-Type: text/plain; charset="utf-8"

Migration capability 'late-block-active' controls when the block drives
will be activated.  If enabled, block drives will only be activated until
VM starts, either src runstate was "live" (RUNNING, or SUSPENDED), or it'll
be postponed until qmp_cont().

Let's do this unconditionally.  There's no harm to delay activation of
block drives.  Meanwhile there's no ABI breakage if dest does it, because
src QEMU has nothing to do with it, so it's no concern on ABI breakage.

IIUC we could avoid introducing this cap when introducing it before, but
now it's still not too late to just always do it.  Cap now prone to
removal, but it'll be for later patches.

Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Fabiano Rosas <farosas@suse.de>
---
 migration/migration.c | 38 +++++++++++++++++++-------------------
 1 file changed, 19 insertions(+), 19 deletions(-)

diff --git a/migration/migration.c b/migration/migration.c
index d2a6b939cf..e6db9cfc50 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -743,24 +743,6 @@ static void process_incoming_migration_bh(void *opaque)
=20
     trace_vmstate_downtime_checkpoint("dst-precopy-bh-enter");
=20
-    /* If capability late_block_activate is set:
-     * Only fire up the block code now if we're going to restart the
-     * VM, else 'cont' will do it.
-     * This causes file locking to happen; so we don't want it to happen
-     * unless we really are starting the VM.
-     */
-    if (!migrate_late_block_activate() ||
-        (autostart && runstate_is_live(migration_get_target_runstate()))) {
-        /* Make sure all file formats throw away their mutable metadata.
-         * If we get an error here, just don't restart the VM yet. */
-        bdrv_activate_all(&local_err);
-        if (local_err) {
-            error_report_err(local_err);
-            local_err =3D NULL;
-            autostart =3D false;
-        }
-    }
-
     /*
      * This must happen after all error conditions are dealt with and
      * we're sure the VM is going to be running on this host.
@@ -775,7 +757,25 @@ static void process_incoming_migration_bh(void *opaque)
=20
     if (runstate_is_live(migration_get_target_runstate())) {
         if (autostart) {
-            vm_start();
+            /*
+             * Block activation is always delayed until VM starts, either
+             * here (which means we need to start the dest VM right now..),
+             * or until qmp_cont() later.
+             *
+             * We used to have cap 'late-block-activate' but now we do this
+             * unconditionally, as it has no harm but only benefit.  E.g.,
+             * it's not part of migration ABI on the time of disk activati=
on.
+             *
+             * Make sure all file formats throw away their mutable
+             * metadata.  If error, don't restart the VM yet.
+             */
+            bdrv_activate_all(&local_err);
+            if (local_err) {
+                error_report_err(local_err);
+                local_err =3D NULL;
+            } else {
+                vm_start();
+            }
         } else {
             runstate_set(RUN_STATE_PAUSED);
         }
--=20
2.47.0
From nobody Wed May  7 21:47:42 2025
Delivered-To: importer@patchew.org
Authentication-Results: mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass(p=none dis=none)  header.from=redhat.com
ARC-Seal: i=1; a=rsa-sha256; t=1733526628; cv=none;
	d=zohomail.com; s=zohoarc;
	b=mgLYB+LpJUR97N1Sm9Rw0e/7hRc8Gsqw4Zysl5PTP43iGETyJOfqC5bWCN0zsKXzD/hMUgqs4Sn+j7voyooY0jnd1sGBFCeTq9QrX76ax66qjLcPBZirG36pqlM0HIzHaZmDNebPY6mFy9TpejGuCHnFnkA0y1nTLmxzMSSnLMI=
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com;
 s=zohoarc;
	t=1733526628;
 h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To;
	bh=1QzNXwvN8FcYg5cJKrcMuUTC36P3aYUb4tIAI6qAx58=;
	b=YANGSdPvq8vXCdRBVsn5hZIOhJhWWRsnFm4U1HuSA3Jia/uLIBDBL8mXFQpDhukIGxGWxsYD6mxtI0iiv1IiInQbt7xQyAPj2yTIuDj0TxNPs192R8C2bAxDNiKHmUCxGEgeuhSenYRiKcrLj+TqKTMuXs1smrwH6XYd6JAjxsU=
ARC-Authentication-Results: i=1; mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass header.from=<peterx@redhat.com> (p=none dis=none)
Return-Path: <qemu-devel-bounces+importer=patchew.org@nongnu.org>
Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by
 mx.zohomail.com
	with SMTPS id 1733526628596355.7652865045344;
 Fri, 6 Dec 2024 15:10:28 -0800 (PST)
Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <qemu-devel-bounces@nongnu.org>)
	id 1tJhRO-00033J-2Q; Fri, 06 Dec 2024 18:08:54 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRM-00032Y-VZ
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:53 -0500
Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRL-0005pl-Ey
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:52 -0500
Received: from mail-il1-f197.google.com (mail-il1-f197.google.com
 [209.85.166.197]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id
 us-mta-612-qJDFWSbgM7eowUFssCAiTg-1; Fri, 06 Dec 2024 18:08:49 -0500
Received: by mail-il1-f197.google.com with SMTP id
 e9e14a558f8ab-3a815ab079cso18592735ab.0
 for <qemu-devel@nongnu.org>; Fri, 06 Dec 2024 15:08:49 -0800 (PST)
Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com.
 [99.254.114.190]) by smtp.gmail.com with ESMTPSA id
 8926c6da1cb9f-4e2861d9a8dsm1038895173.86.2024.12.06.15.08.46
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 06 Dec 2024 15:08:47 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1733526530;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:cc:mime-version:mime-version:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=1QzNXwvN8FcYg5cJKrcMuUTC36P3aYUb4tIAI6qAx58=;
 b=g+mhfu12pllRmpVaE0QO5DvLzk+zQgaLws98WfEQyxbma84GOhy7G3aihtiGvIvabC1hNH
 QZTVmVVqGM0Cx8whkmNOgJ6K+3AY5t1kVXgI58RY1tVPKULpLQUD83jE9v7hc8WYnRc4PH
 oMpSLA5lYhIsk+S7Y7d7tfvSmlrdbPs=
X-MC-Unique: qJDFWSbgM7eowUFssCAiTg-1
X-Mimecast-MFC-AGG-ID: qJDFWSbgM7eowUFssCAiTg
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1733526528; x=1734131328;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=1QzNXwvN8FcYg5cJKrcMuUTC36P3aYUb4tIAI6qAx58=;
 b=iDh3KvF9f2yZIDM6oO231pCRvKRWd+4tDZY3RQSlY79EP//a8KbxWGFcEDnAQ1cB5+
 TBNML2LCRQYj1u/v4yvsxmXmfh9nedrskHriynSrZIRDcKAnsc8bYQdjK7OjrZgo56xY
 Mmm48b1ojE/6VnYcOGp6h4dWLWioI4HAj55XvtKjkTAY5dR/3MYDrs8W4j6kaIHYVEV+
 xp21LdLg20R+NJt9x4xEinDQfPHTBajwtczLelyclZ0tIzt91o/NxCsuMcPKtSm2v+ov
 MNLgkmoDP0Jq7yuF2Yn02nTodimVIj8gCzowsJwygEXtFrAgkZY81zrzTwV+zweDArZJ
 DMUA==
X-Gm-Message-State: AOJu0YzILb5d3QaTcT1P3ZMlH1dtGZ0cs8hVRS1t0E2xE99FGaL99K6u
 W5WF8Hw0nzUUiqEOJabcLImvlJc6CvABBlz7cOkHLWv1PyWgIfbYYmYhpEdnxrfEmMcZwx1i2gc
 m48iJC1xZWO4EgmFK9DlWo0FX3P6YK7PjRr6OZPVsOGCE2IA5tXYC/z0VQ7eILK4CcYeUeJFun+
 O9uyxH5PBAwTkAgPaTrk3bBY5RbZdu6ZNWvQ==
X-Gm-Gg: ASbGncvItxrK9Imj4uXlaGrKIZQLzgIs2W+M9/QvDsrUvtUDV7jU4LuWMFvpbWD5UC2
 ZjALS2m25Zl7n1PBeDTjgdSmYBniRwcRvLPORvv74xti0yxc4jqTTOkARg0v3i4UGrp7j3Ejsz7
 q9jf6l4NjHcBlfhCKlSN7Lns6BSktFAZDzVWfCDcJtmRq7T+uCae4Hp7f0ZTm3FQ0bLmHusoBH7
 wMtwAbnTzY+LXOf5m/DbZBzrGgsYxiSBwkJZ1H06Rg1BJ3nJLe9r/5xSW1EqNW8ind2UCXFwHJk
 cJFQWWNd3fKKw1WIF35pMPhTAQ==
X-Received: by 2002:a05:6e02:198f:b0:3a7:81a4:a557 with SMTP id
 e9e14a558f8ab-3a811e4c7cfmr54055035ab.24.1733526528225;
 Fri, 06 Dec 2024 15:08:48 -0800 (PST)
X-Google-Smtp-Source: 
 AGHT+IFqzzi7EnrSVeA07zTtFB2kdjmIihOv7I0bHFSYYg/pDqbw3SO8Idu6w5MSZ+popfsnQt9Vdw==
X-Received: by 2002:a05:6e02:198f:b0:3a7:81a4:a557 with SMTP id
 e9e14a558f8ab-3a811e4c7cfmr54054765ab.24.1733526527895;
 Fri, 06 Dec 2024 15:08:47 -0800 (PST)
From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: peterx@redhat.com, qemu-block@nongnu.org,
 Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>,
 Stefan Hajnoczi <stefanha@redhat.com>, Fabiano Rosas <farosas@suse.de>,
 Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>,
 Eric Blake <eblake@redhat.com>,
 "Dr . David Alan Gilbert" <dave@treblig.org>,
 Kevin Wolf <kwolf@redhat.com>,
 =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= <berrange@redhat.com>
Subject: [PATCH v2 4/6] migration/block: Apply late-block-active behavior to
 postcopy
Date: Fri,  6 Dec 2024 18:08:36 -0500
Message-ID: <20241206230838.1111496-5-peterx@redhat.com>
X-Mailer: git-send-email 2.47.0
In-Reply-To: <20241206230838.1111496-1-peterx@redhat.com>
References: <20241206230838.1111496-1-peterx@redhat.com>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17
 as permitted sender) client-ip=209.51.188.17;
 envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org;
 helo=lists.gnu.org;
Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com;
 helo=us-smtp-delivery-124.mimecast.com
X-Spam_score_int: -50
X-Spam_score: -5.1
X-Spam_bar: -----
X-Spam_report: (-5.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996,
 DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001,
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001,
 RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no
X-Spam_action: no action
X-BeenThere: qemu-devel@nongnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: <qemu-devel.nongnu.org>
List-Unsubscribe: <https://lists.nongnu.org/mailman/options/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>
List-Archive: <https://lists.nongnu.org/archive/html/qemu-devel>
List-Post: <mailto:qemu-devel@nongnu.org>
List-Help: <mailto:qemu-devel-request@nongnu.org?subject=help>
List-Subscribe: <https://lists.nongnu.org/mailman/listinfo/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=subscribe>
Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org
Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org
X-ZohoMail-DKIM: pass (identity @redhat.com)
X-ZM-MESSAGEID: 1733526630502116600
Content-Type: text/plain; charset="utf-8"

Postcopy never cared about late-block-active.  However there's no mention
in the capability that it doesn't apply to postcopy.

Considering that we _assumed_ late activation is always good, do that too
for postcopy unconditionally, just like precopy.  After this patch, we
should have unified the behavior across all.

Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Fabiano Rosas <farosas@suse.de>
---
 migration/savevm.c | 25 ++++++++++++-------------
 1 file changed, 12 insertions(+), 13 deletions(-)

diff --git a/migration/savevm.c b/migration/savevm.c
index 98821c8120..80726726fe 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -2137,22 +2137,21 @@ static void loadvm_postcopy_handle_run_bh(void *opa=
que)
=20
     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-announced");
=20
-    /* Make sure all file formats throw away their mutable metadata.
-     * If we get an error here, just don't restart the VM yet. */
-    bdrv_activate_all(&local_err);
-    if (local_err) {
-        error_report_err(local_err);
-        local_err =3D NULL;
-        autostart =3D false;
-    }
-
-    trace_vmstate_downtime_checkpoint("dst-postcopy-bh-cache-invalidated");
-
     dirty_bitmap_mig_before_vm_start();
=20
     if (autostart) {
-        /* Hold onto your hats, starting the CPU */
-        vm_start();
+        /*
+         * Make sure all file formats throw away their mutable metadata.
+         * If we get an error here, just don't restart the VM yet.
+         */
+        bdrv_activate_all(&local_err);
+        trace_vmstate_downtime_checkpoint("dst-postcopy-bh-cache-invalidat=
ed");
+        if (local_err) {
+            error_report_err(local_err);
+            local_err =3D NULL;
+        } else {
+            vm_start();
+        }
     } else {
         /* leave it paused and let management decide when to start the CPU=
 */
         runstate_set(RUN_STATE_PAUSED);
--=20
2.47.0
From nobody Wed May  7 21:47:42 2025
Delivered-To: importer@patchew.org
Authentication-Results: mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass(p=none dis=none)  header.from=redhat.com
ARC-Seal: i=1; a=rsa-sha256; t=1733526637; cv=none;
	d=zohomail.com; s=zohoarc;
	b=W05mGotid7ulJw9Pw1hGlRUsj8HAt8rY3hvxs1ZpFxHnp33hd7McWycypk4I0xj7EAXSmVijwpwOigksUC7sUxGDq26ouxF8F4FiYI++i9DpPtg/5Rn4hyENlAtFjKshbcc7VnYMHxB+D5NAi5zZOVJQPxROOThcQp2bTXXdvj4=
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com;
 s=zohoarc;
	t=1733526637;
 h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To;
	bh=bS9WUYBupHARWAmcXHaiZdxJvm4+ljW4MwpDEWQXwEI=;
	b=ODdpKKQYnkJ1qMWIaraspIVIDc2faWvieZdzC/S599spTGNufNuFkSFuFA18AxQVMEH8UGXlLJCAzKa7JNA9vGnaCT7PfhulMckk/0t8euIgojM/MqkZ1P0Kst3egztbV7Obaw11uUJCxoR8MR1T5QjBl1enZZscjysaewQRU3s=
ARC-Authentication-Results: i=1; mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass header.from=<peterx@redhat.com> (p=none dis=none)
Return-Path: <qemu-devel-bounces+importer=patchew.org@nongnu.org>
Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by
 mx.zohomail.com
	with SMTPS id 1733526637588365.51933048040826;
 Fri, 6 Dec 2024 15:10:37 -0800 (PST)
Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <qemu-devel-bounces@nongnu.org>)
	id 1tJhRP-00034C-0m; Fri, 06 Dec 2024 18:08:55 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRO-00033S-7h
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:54 -0500
Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRM-0005qM-PW
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:53 -0500
Received: from mail-io1-f70.google.com (mail-io1-f70.google.com
 [209.85.166.70]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id
 us-mta-608-ksRqld-zOh6vfAD1CKCScg-1; Fri, 06 Dec 2024 18:08:51 -0500
Received: by mail-io1-f70.google.com with SMTP id
 ca18e2360f4ac-8419946e077so428694239f.2
 for <qemu-devel@nongnu.org>; Fri, 06 Dec 2024 15:08:51 -0800 (PST)
Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com.
 [99.254.114.190]) by smtp.gmail.com with ESMTPSA id
 8926c6da1cb9f-4e2861d9a8dsm1038895173.86.2024.12.06.15.08.48
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 06 Dec 2024 15:08:48 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1733526532;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:cc:mime-version:mime-version:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=bS9WUYBupHARWAmcXHaiZdxJvm4+ljW4MwpDEWQXwEI=;
 b=eytdUVlLHmqddYD3Km+htAqOnnvmoIk4QJCuKQ0NpEV6V3R9SJ+a3WQ97W1vD+yDqUk7hz
 HUbP7PDKSeCYsY6/dJ8SKNv2UKxZEN7yuHhUZDvSLjSoKv8lSax0Gcpciqnme/mLFZC47h
 u+1u3jyNVO5BzkiYjfy8cWy4S8llUuk=
X-MC-Unique: ksRqld-zOh6vfAD1CKCScg-1
X-Mimecast-MFC-AGG-ID: ksRqld-zOh6vfAD1CKCScg
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1733526530; x=1734131330;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=bS9WUYBupHARWAmcXHaiZdxJvm4+ljW4MwpDEWQXwEI=;
 b=DeGOcFxXuxO73GN7lsvo0OvKsj9t3V/ojq7Um/7CjdU5MtqRw3V8Pr7LlGTZrZJYVP
 k4guUOzcf9zNILHhgqp5Rsg4tFWwvtKm0vjjot4DzVDuYnfaq8j9MJlDEmzjaP1APXqe
 Q/fZCcJu/GU2LVx253ZYRgh6ZySWxZOlLWgKIaw/6DqTx8dFu1oH6LptlXyPyaUOc0Qh
 ptfuvJN6yLvOxuI/Jl/Yf0KyySHlGBCiKADCquUqRom/1DMtmEdQk+Pbs8MZhgDgCYpS
 0VLiIrBwmI2Kpy+wgc9nlHI9G3xCm+Hqni0wfvDlbGRf8tOZg7RoFIiy4TSOtKKV9d2A
 02RQ==
X-Gm-Message-State: AOJu0YwC+1YpJH8kYAjsk9PQhE81Gwp85armr5Kk33JgahYYlorL+mzG
 ZCBEqNLXmzlElt/NPVyuZvsAZb/ixJJZVXPl9iUYG6sIGNfHY/syHdffnIIBsYdGpknUySIKx9D
 dhsWbTZ1L/kDAamM/S601AzPYpTQ1yYP6TgblUo/saxwALsj8jAZ/N0LAjFhAEkWb8SSCY/BHU0
 j8AtSm6zDcL/hIrIdGfX7zj8CDUMBBIwUvHw==
X-Gm-Gg: ASbGnctYmjQCmeSeU3Pto/HuXsbK1g7Jos0cTeBDMJcfvkRto+iPNFtb/dZ/6aTTkv2
 gygQJ1yfy27jjzENgPylEkTDWuI0qd1DskqDsVTKek+s1sbNMKqnB2RmLlaqgS7VhuoE3nV7XfE
 r2VNAUE3VqyIhzzOOVZN0iwE/1rXLZVAs5ajbKNeTSnZW17p93qUZRICh1IBrT4mx7RxemkRP8V
 2AA93hZSq/f8yd+5mfxpvg2LTop12Ci4Dv0Vg0zBUvufum4kcebaJFdbbEIVx85L8aHj0e8xO7s
 xB1tDl6oK36LCtHnKUl23pqmzw==
X-Received: by 2002:a05:6602:641c:b0:83b:2b89:a14e with SMTP id
 ca18e2360f4ac-8447e2c3059mr585271039f.13.1733526530055;
 Fri, 06 Dec 2024 15:08:50 -0800 (PST)
X-Google-Smtp-Source: 
 AGHT+IH1O0DiEPkcMsR47gYbS8NeUHYNgBIGz6sHzHftMJtOlco6RqciKtr/eHPZsSsobJC/ker4cg==
X-Received: by 2002:a05:6602:641c:b0:83b:2b89:a14e with SMTP id
 ca18e2360f4ac-8447e2c3059mr585268339f.13.1733526529709;
 Fri, 06 Dec 2024 15:08:49 -0800 (PST)
From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: peterx@redhat.com, qemu-block@nongnu.org,
 Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>,
 Stefan Hajnoczi <stefanha@redhat.com>, Fabiano Rosas <farosas@suse.de>,
 Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>,
 Eric Blake <eblake@redhat.com>,
 "Dr . David Alan Gilbert" <dave@treblig.org>,
 Kevin Wolf <kwolf@redhat.com>,
 =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= <berrange@redhat.com>
Subject: [PATCH v2 5/6] migration/block: Fix possible race with block_inactive
Date: Fri,  6 Dec 2024 18:08:37 -0500
Message-ID: <20241206230838.1111496-6-peterx@redhat.com>
X-Mailer: git-send-email 2.47.0
In-Reply-To: <20241206230838.1111496-1-peterx@redhat.com>
References: <20241206230838.1111496-1-peterx@redhat.com>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17
 as permitted sender) client-ip=209.51.188.17;
 envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org;
 helo=lists.gnu.org;
Received-SPF: pass client-ip=170.10.133.124; envelope-from=peterx@redhat.com;
 helo=us-smtp-delivery-124.mimecast.com
X-Spam_score_int: -50
X-Spam_score: -5.1
X-Spam_bar: -----
X-Spam_report: (-5.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996,
 DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001,
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001,
 RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no
X-Spam_action: no action
X-BeenThere: qemu-devel@nongnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: <qemu-devel.nongnu.org>
List-Unsubscribe: <https://lists.nongnu.org/mailman/options/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>
List-Archive: <https://lists.nongnu.org/archive/html/qemu-devel>
List-Post: <mailto:qemu-devel@nongnu.org>
List-Help: <mailto:qemu-devel-request@nongnu.org?subject=help>
List-Subscribe: <https://lists.nongnu.org/mailman/listinfo/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=subscribe>
Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org
Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org
X-ZohoMail-DKIM: pass (identity @redhat.com)
X-ZM-MESSAGEID: 1733526638608116600
Content-Type: text/plain; charset="utf-8"

Src QEMU sets block_inactive=3Dtrue very early before the invalidation takes
place.  It means if something wrong happened during setting the flag but
before reaching qemu_savevm_state_complete_precopy_non_iterable() where it
did the invalidation work, it'll make block_inactive flag inconsistent.

For example, think about when qemu_savevm_state_complete_precopy_iterable()
can fail: it will have block_inactive set to true even if all block drives
are active.

Fix that by only update the flag after the invalidation is done.

No Fixes for any commit, because it's not an issue if bdrv_activate_all()
is re-entrant upon all-active disks - false positive block_inactive can
bring nothing more than "trying to active the blocks but they're already
active".  However let's still do it right to avoid the inconsistent flag
v.s. reality.

Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Fabiano Rosas <farosas@suse.de>
---
 migration/migration.c | 9 +++------
 migration/savevm.c    | 2 ++
 2 files changed, 5 insertions(+), 6 deletions(-)

diff --git a/migration/migration.c b/migration/migration.c
index e6db9cfc50..ba5deec5bc 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -2749,14 +2749,11 @@ static int migration_completion_precopy(MigrationSt=
ate *s,
         goto out_unlock;
     }
=20
-    /*
-     * Inactivate disks except in COLO, and track that we have done so in =
order
-     * to remember to reactivate them if migration fails or is cancelled.
-     */
-    s->block_inactive =3D !migrate_colo();
     migration_rate_set(RATE_LIMIT_DISABLED);
+
+    /* Inactivate disks except in COLO */
     ret =3D qemu_savevm_state_complete_precopy(s->to_dst_file, false,
-                                             s->block_inactive);
+                                             !migrate_colo());
 out_unlock:
     bql_unlock();
     return ret;
diff --git a/migration/savevm.c b/migration/savevm.c
index 80726726fe..706b77ffab 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -1558,6 +1558,8 @@ int qemu_savevm_state_complete_precopy_non_iterable(Q=
EMUFile *f,
             qemu_file_set_error(f, ret);
             return ret;
         }
+        /* Remember that we did this */
+        s->block_inactive =3D true;
     }
     if (!in_postcopy) {
         /* Postcopy stream will still be going */
--=20
2.47.0
From nobody Wed May  7 21:47:42 2025
Delivered-To: importer@patchew.org
Authentication-Results: mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass(p=none dis=none)  header.from=redhat.com
ARC-Seal: i=1; a=rsa-sha256; t=1733526637; cv=none;
	d=zohomail.com; s=zohoarc;
	b=gB/kOyj2dldaxQkyUmhJdv1RHr2RJ2jLeFF5U2+kr/ty8zFri8ZvazaxYHSOluCr9Wa4ckmjHe2blzg37z51neGN2itExhQ88HTca719m/DRydtqYYgOXNyuwFLR/mqCj/+HZAAvU5E/Ahl+EVNSCajY/Te7k4RH80O4XbNpRlg=
ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com;
 s=zohoarc;
	t=1733526637;
 h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:To:To:Message-Id:Reply-To;
	bh=wzJbEwR/amI3Osl7VJKBOkOySNjtCXDra3pLIrdt9sU=;
	b=J3adQ7lIem6/tlbKksWjURNs3SoHHS4VsAfDIr5OfE9qTosEU53t+1t0Ll7jjUs2q6w7N5+eFMR/leooot7ebL0yzGR59tl2oG5U5EfB51lBOR0NUY1Q3J4TnRidyVKAUvAtc0Qu+wo7AB9aUxoZH3Rafaljmq2pXzodOKzWMPo=
ARC-Authentication-Results: i=1; mx.zohomail.com;
	dkim=pass;
	spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as
 permitted sender)
  smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org;
	dmarc=pass header.from=<peterx@redhat.com> (p=none dis=none)
Return-Path: <qemu-devel-bounces+importer=patchew.org@nongnu.org>
Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by
 mx.zohomail.com
	with SMTPS id 1733526637307529.3565632266246;
 Fri, 6 Dec 2024 15:10:37 -0800 (PST)
Received: from localhost ([::1] helo=lists1p.gnu.org)
	by lists.gnu.org with esmtp (Exim 4.90_1)
	(envelope-from <qemu-devel-bounces@nongnu.org>)
	id 1tJhRT-000352-Kt; Fri, 06 Dec 2024 18:08:59 -0500
Received: from eggs.gnu.org ([2001:470:142:3::10])
 by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRS-00034r-5G
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:58 -0500
Received: from us-smtp-delivery-124.mimecast.com ([170.10.129.124])
 by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256)
 (Exim 4.90_1) (envelope-from <peterx@redhat.com>) id 1tJhRP-0005qs-L7
 for qemu-devel@nongnu.org; Fri, 06 Dec 2024 18:08:57 -0500
Received: from mail-io1-f72.google.com (mail-io1-f72.google.com
 [209.85.166.72]) by relay.mimecast.com with ESMTP with STARTTLS
 (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id
 us-mta-627-ZnX9JAggPfmADL0_SWeAFw-1; Fri, 06 Dec 2024 18:08:53 -0500
Received: by mail-io1-f72.google.com with SMTP id
 ca18e2360f4ac-843dde4b616so243691439f.0
 for <qemu-devel@nongnu.org>; Fri, 06 Dec 2024 15:08:53 -0800 (PST)
Received: from x1n.redhat.com (pool-99-254-114-190.cpe.net.cable.rogers.com.
 [99.254.114.190]) by smtp.gmail.com with ESMTPSA id
 8926c6da1cb9f-4e2861d9a8dsm1038895173.86.2024.12.06.15.08.49
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Fri, 06 Dec 2024 15:08:50 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com;
 s=mimecast20190719; t=1733526534;
 h=from:from:reply-to:subject:subject:date:date:message-id:message-id:
 to:to:cc:cc:mime-version:mime-version:
 content-transfer-encoding:content-transfer-encoding:
 in-reply-to:in-reply-to:references:references;
 bh=wzJbEwR/amI3Osl7VJKBOkOySNjtCXDra3pLIrdt9sU=;
 b=BCWRREOu4F+ARkg+gxL0VjdguBN2yfkApy1x7PVyt4dOA0VC6lRtzMTfjEjmbT90JBXWih
 HjN/V2orvCpP6/Aw7y7xsHeLRHTxS4MUSEApqQdPPKtO4ujo7rb4qrEeoPWskIZXE5y2sv
 FMgoodVcyQRkatv3NN6rYGKsvyBZNrM=
X-MC-Unique: ZnX9JAggPfmADL0_SWeAFw-1
X-Mimecast-MFC-AGG-ID: ZnX9JAggPfmADL0_SWeAFw
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1733526532; x=1734131332;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=wzJbEwR/amI3Osl7VJKBOkOySNjtCXDra3pLIrdt9sU=;
 b=CSAC0jKGeSObMJBl7dWhqBOBuspe9EI+53L1Z8A8u/eEu0WWL0mzRm7aYy6xLeVi0S
 iHIBV96q4av41jOvGSot0GljMFgTGqpZMvjVSG3wlwGlFbnSSXHs6bumkUrBrVv9HhMq
 rLpKkwQElhy05EK1AZrAgUKp2iTadnE2I/c2USa+uBvKbFc6mxUdtY8FJFo7LinLrydJ
 UM5jWxkn9GxJK8grzLfKuj41QcziA3uGEmN9hGdEdOpTaC0Y9+Sy3nYqX6HjJDLhfRg7
 ttfJ4y2fF8OgB/I9nHGbnLY1xRv3/I5F+XLIB0aR37t40UYHmpaBLOOKm0iysMWnZaT2
 VlPg==
X-Gm-Message-State: AOJu0YzXZ0yqN3t1hmhKD5VBzCSTP6uKCrfuVmtikhYDFOfj3hAHWmeh
 BQxwpoxwR1oHCaDxxCyDTvt5XhfZKj0n6qEvsnAEl+5c1cxEgSWZXAIXv/nEL4OKTqBDKZkf+s6
 mPIdbAOejyyvBO/NewAdqaTRq+m/Z8Qdo+XU93uxFf76OZYu705kqXFNRLfhj9IIKRR/TUNT8d+
 CUPksVORDG//MMBId9362Sn2q1mkL+c92I1A==
X-Gm-Gg: ASbGnctp8ky5tp/GiuUYoXeLJDxNgRo4cKihYfws5ioaSbkZPsPdACsxNep6J2a5KoQ
 Wm8k03JLNU0ddX+yB+QYr3glVNTDXnf5Gr/d/2xipzMyj82bITFY0Jh8j+G7ACPA/RAO2CW+72N
 BqwDOxYw7rMPvwwHb37UOSk84OeWlF8yRjoDpQ6W+mSJjo8AfS1PUi1UlQLlnj7nMlxclxMPtpG
 mQy99b7xk92oGNyNituTXPbtrFAVRojzKc8DH8ot9ngo7/N9rL6pHt24MlujyAVmslAfv8RPLo9
 hQvZvE8D2PH4ZH2InPnPjAqJtg==
X-Received: by 2002:a5d:9947:0:b0:835:3dfc:5ba5 with SMTP id
 ca18e2360f4ac-844710e5ddfmr924740339f.5.1733526532191;
 Fri, 06 Dec 2024 15:08:52 -0800 (PST)
X-Google-Smtp-Source: 
 AGHT+IF+yu12rwxiwAYUSjQpwzZP+Gilm2wV63K/v1whAUIP4XSbyi3kvua8mw41zGSCbRfZQxWuIg==
X-Received: by 2002:a5d:9947:0:b0:835:3dfc:5ba5 with SMTP id
 ca18e2360f4ac-844710e5ddfmr924737339f.5.1733526531487;
 Fri, 06 Dec 2024 15:08:51 -0800 (PST)
From: Peter Xu <peterx@redhat.com>
To: qemu-devel@nongnu.org
Cc: peterx@redhat.com, qemu-block@nongnu.org,
 Vladimir Sementsov-Ogievskiy <vsementsov@yandex-team.ru>,
 Stefan Hajnoczi <stefanha@redhat.com>, Fabiano Rosas <farosas@suse.de>,
 Andrey Drobyshev <andrey.drobyshev@virtuozzo.com>,
 Eric Blake <eblake@redhat.com>,
 "Dr . David Alan Gilbert" <dave@treblig.org>,
 Kevin Wolf <kwolf@redhat.com>,
 =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= <berrange@redhat.com>
Subject: [PATCH v2 6/6] migration/block: Rewrite disk activation
Date: Fri,  6 Dec 2024 18:08:38 -0500
Message-ID: <20241206230838.1111496-7-peterx@redhat.com>
X-Mailer: git-send-email 2.47.0
In-Reply-To: <20241206230838.1111496-1-peterx@redhat.com>
References: <20241206230838.1111496-1-peterx@redhat.com>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17
 as permitted sender) client-ip=209.51.188.17;
 envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org;
 helo=lists.gnu.org;
Received-SPF: pass client-ip=170.10.129.124; envelope-from=peterx@redhat.com;
 helo=us-smtp-delivery-124.mimecast.com
X-Spam_score_int: -50
X-Spam_score: -5.1
X-Spam_bar: -----
X-Spam_report: (-5.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-2.996,
 DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
 RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H2=-0.001,
 RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001,
 RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001,
 SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no
X-Spam_action: no action
X-BeenThere: qemu-devel@nongnu.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: <qemu-devel.nongnu.org>
List-Unsubscribe: <https://lists.nongnu.org/mailman/options/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=unsubscribe>
List-Archive: <https://lists.nongnu.org/archive/html/qemu-devel>
List-Post: <mailto:qemu-devel@nongnu.org>
List-Help: <mailto:qemu-devel-request@nongnu.org?subject=help>
List-Subscribe: <https://lists.nongnu.org/mailman/listinfo/qemu-devel>,
 <mailto:qemu-devel-request@nongnu.org?subject=subscribe>
Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org
Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org
X-ZohoMail-DKIM: pass (identity @redhat.com)
X-ZM-MESSAGEID: 1733526638827116600
Content-Type: text/plain; charset="utf-8"

This patch proposes a flag to maintain disk activation status globally.  It
mostly rewrites disk activation mgmt for QEMU, including COLO and QMP
command xen_save_devices_state.

Backgrounds
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

We have two problems on disk activations, one resolved, one not.

Problem 1: disk activation recover (for switchover interruptions)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

When migration is either cancelled or failed during switchover, especially
when after the disks are inactivated, QEMU needs to remember re-activate
the disks again before vm starts.

It used to be done separately in two paths: one in qmp_migrate_cancel(),
the other one in the failure path of migration_completion().

It used to be fixed in different commits, all over the places in QEMU.  So
these are the relevant changes I saw, I'm not sure if it's complete list:

 - In 2016, commit fe904ea824 ("migration: regain control of images when
   migration fails to complete")

 - In 2017, commit 1d2acc3162 ("migration: re-active images while migration
   been canceled after inactive them")

 - In 2023, commit 6dab4c93ec ("migration: Attempt disk reactivation in
   more failure scenarios")

Now since we have a slightly better picture maybe we can unify the
reactivation in a single path.

One side benefit of doing so is, we can move the disk operation outside QMP
command "migrate_cancel".  It's possible that in the future we may want to
make "migrate_cancel" be OOB-compatible, while that requires the command
doesn't need BQL in the first place.  This will already do that and make
migrate_cancel command lightweight.

Problem 2: disk invalidation on top of invalidated disks
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

This is an unresolved bug for current QEMU.  Link in "Resolves:" at the
end.  It turns out besides the src switchover phase (problem 1 above), QEMU
also needs to remember block activation on destination.

Consider two continuous migration in a row, where the VM was always paused.
In that scenario, the disks are not activated even until migration
completed in the 1st round.  When the 2nd round starts, if QEMU doesn't
know the status of the disks, it needs to try inactivate the disk again.

Here the issue is the block layer API bdrv_inactivate_all() will crash a
QEMU if invoked on already inactive disks for the 2nd migration.  For
detail, see the bug link at the end.

Implementation
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D

This patch proposes to maintain disk activation with a global flag, so we
know:

  - If we used to inactivate disks for migration, but migration got
  cancelled, or failed, QEMU will know it should reactivate the disks.

  - On incoming side, if the disks are never activated but then another
  migration is triggered, QEMU should be able to tell that inactivate is
  not needed for the 2nd migration.

We used to have disk_inactive, but it only solves the 1st issue, not the
2nd.  Also, it's done in completely separate paths so it's extremely hard
to follow either how the flag changes, or the duration that the flag is
valid, and when we will reactivate the disks.

Convert the existing disk_inactive flag into that global flag (also invert
its naming), and maintain the disk activation status for the whole
lifecycle of qemu.  That includes the incoming QEMU.

Put both of the error cases of source migration (failure, cancelled)
together into migration_iteration_finish(), which will be invoked for
either of the scenario.  So from that part QEMU should behave the same as
before.  However with such global maintenance on disk activation status, we
not only cleanup quite a few temporary paths that we try to maintain the
disk activation status (e.g. in postcopy code), meanwhile it fixes the
crash for problem 2 in one shot.

For freshly started QEMU, the flag is initialized to TRUE showing that the
QEMU owns the disks by default.

For incoming migrated QEMU, the flag will be initialized to FALSE once and
for all showing that the dest QEMU doesn't own the disks until switchover.
That is guaranteed by the "once" variable.

Resolves: https://gitlab.com/qemu-project/qemu/-/issues/2395
Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: Fabiano Rosas <farosas@suse.de>
---
 include/migration/misc.h |  4 ++
 migration/migration.h    |  6 +--
 migration/block-active.c | 94 ++++++++++++++++++++++++++++++++++++++++
 migration/colo.c         |  2 +-
 migration/migration.c    | 80 ++++++++--------------------------
 migration/savevm.c       | 33 ++++++--------
 monitor/qmp-cmds.c       |  8 +---
 migration/meson.build    |  1 +
 migration/trace-events   |  3 ++
 9 files changed, 140 insertions(+), 91 deletions(-)
 create mode 100644 migration/block-active.c

diff --git a/include/migration/misc.h b/include/migration/misc.h
index 804eb23c06..e68a473feb 100644
--- a/include/migration/misc.h
+++ b/include/migration/misc.h
@@ -106,4 +106,8 @@ bool migration_incoming_postcopy_advised(void);
 /* True if background snapshot is active */
 bool migration_in_bg_snapshot(void);
=20
+/* Wrapper for block active/inactive operations */
+bool migration_block_activate(Error **errp);
+bool migration_block_inactivate(void);
+
 #endif
diff --git a/migration/migration.h b/migration/migration.h
index 3857905c0e..fab3cad2b9 100644
--- a/migration/migration.h
+++ b/migration/migration.h
@@ -370,9 +370,6 @@ struct MigrationState {
     /* Flag set once the migration thread is running (and needs joining) */
     bool migration_thread_running;
=20
-    /* Flag set once the migration thread called bdrv_inactivate_all */
-    bool block_inactive;
-
     /* Migration is waiting for guest to unplug device */
     QemuSemaphore wait_unplug_sem;
=20
@@ -556,4 +553,7 @@ void migration_bitmap_sync_precopy(bool last_stage);
 /* migration/block-dirty-bitmap.c */
 void dirty_bitmap_mig_init(void);
=20
+/* migration/block-active.c */
+void migration_block_active_setup(bool active);
+
 #endif
diff --git a/migration/block-active.c b/migration/block-active.c
new file mode 100644
index 0000000000..d477cf8182
--- /dev/null
+++ b/migration/block-active.c
@@ -0,0 +1,94 @@
+/*
+ * Block activation tracking for migration purpose
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ *
+ * Copyright (C) 2024 Red Hat, Inc.
+ */
+#include "qemu/osdep.h"
+#include "block/block.h"
+#include "qapi/error.h"
+#include "migration/migration.h"
+#include "qemu/error-report.h"
+#include "trace.h"
+
+/*
+ * Migration-only cache to remember the block layer activation status.
+ * Protected by BQL.
+ *
+ * We need this because..
+ *
+ * - Migration can fail after block devices are invalidated (during
+ *   switchover phase).  When that happens, we need to be able to recover
+ *   the block drive status by re-activating them.
+ *
+ * - Currently bdrv_inactivate_all() is not safe to be invoked on top of
+ *   invalidated drives (even if bdrv_activate_all() is actually safe to be
+ *   called any time!).  It means remembering this could help migration to
+ *   make sure it won't invalidate twice in a row, crashing QEMU.  It can
+ *   happen when we migrate a PAUSED VM from host1 to host2, then migrate
+ *   again to host3 without starting it.  TODO: a cleaner solution is to
+ *   allow safe invoke of bdrv_inactivate_all() at anytime, like
+ *   bdrv_activate_all().
+ *
+ * For freshly started QEMU, the flag is initialized to TRUE reflecting the
+ * scenario where QEMU owns block device ownerships.
+ *
+ * For incoming QEMU taking a migration stream, the flag is initialized to
+ * FALSE reflecting that the incoming side doesn't own the block devices,
+ * not until switchover happens.
+ */
+static bool migration_block_active;
+
+/* Setup the disk activation status */
+void migration_block_active_setup(bool active)
+{
+    migration_block_active =3D active;
+}
+
+bool migration_block_activate(Error **errp)
+{
+    ERRP_GUARD();
+
+    assert(bql_locked());
+
+    if (migration_block_active) {
+        trace_migration_block_activation("active-skipped");
+        return true;
+    }
+
+    trace_migration_block_activation("active");
+
+    bdrv_activate_all(errp);
+    if (*errp) {
+        error_report_err(error_copy(*errp));
+        return false;
+    }
+
+    migration_block_active =3D true;
+    return true;
+}
+
+bool migration_block_inactivate(void)
+{
+    int ret;
+
+    assert(bql_locked());
+
+    if (!migration_block_active) {
+        trace_migration_block_activation("inactive-skipped");
+        return true;
+    }
+
+    trace_migration_block_activation("inactive");
+
+    ret =3D bdrv_inactivate_all();
+    if (ret) {
+        error_report("%s: bdrv_inactivate_all() failed: %d",
+                     __func__, ret);
+        return false;
+    }
+
+    migration_block_active =3D false;
+    return true;
+}
diff --git a/migration/colo.c b/migration/colo.c
index 9590f281d0..ae3387a7a4 100644
--- a/migration/colo.c
+++ b/migration/colo.c
@@ -836,7 +836,7 @@ static void *colo_process_incoming_thread(void *opaque)
=20
     /* Make sure all file formats throw away their mutable metadata */
     bql_lock();
-    bdrv_activate_all(&local_err);
+    migration_block_activate(&local_err);
     bql_unlock();
     if (local_err) {
         error_report_err(local_err);
diff --git a/migration/migration.c b/migration/migration.c
index ba5deec5bc..d755ccb03d 100644
--- a/migration/migration.c
+++ b/migration/migration.c
@@ -738,7 +738,6 @@ static void qemu_start_incoming_migration(const char *u=
ri, bool has_channels,
=20
 static void process_incoming_migration_bh(void *opaque)
 {
-    Error *local_err =3D NULL;
     MigrationIncomingState *mis =3D opaque;
=20
     trace_vmstate_downtime_checkpoint("dst-precopy-bh-enter");
@@ -769,11 +768,7 @@ static void process_incoming_migration_bh(void *opaque)
              * Make sure all file formats throw away their mutable
              * metadata.  If error, don't restart the VM yet.
              */
-            bdrv_activate_all(&local_err);
-            if (local_err) {
-                error_report_err(local_err);
-                local_err =3D NULL;
-            } else {
+            if (migration_block_activate(NULL)) {
                 vm_start();
             }
         } else {
@@ -1552,16 +1547,6 @@ static void migrate_fd_cancel(MigrationState *s)
             }
         }
     }
-    if (s->state =3D=3D MIGRATION_STATUS_CANCELLING && s->block_inactive) {
-        Error *local_err =3D NULL;
-
-        bdrv_activate_all(&local_err);
-        if (local_err) {
-            error_report_err(local_err);
-        } else {
-            s->block_inactive =3D false;
-        }
-    }
 }
=20
 void migration_add_notifier_mode(NotifierWithReturn *notify,
@@ -1860,6 +1845,12 @@ void qmp_migrate_incoming(const char *uri, bool has_=
channels,
         return;
     }
=20
+    /*
+     * Newly setup incoming QEMU.  Mark the block active state to reflect
+     * that the src currently owns the disks.
+     */
+    migration_block_active_setup(false);
+
     once =3D false;
 }
=20
@@ -2512,7 +2503,6 @@ static int postcopy_start(MigrationState *ms, Error *=
*errp)
     QIOChannelBuffer *bioc;
     QEMUFile *fb;
     uint64_t bandwidth =3D migrate_max_postcopy_bandwidth();
-    bool restart_block =3D false;
     int cur_state =3D MIGRATION_STATUS_ACTIVE;
=20
     if (migrate_postcopy_preempt()) {
@@ -2548,13 +2538,10 @@ static int postcopy_start(MigrationState *ms, Error=
 **errp)
         goto fail;
     }
=20
-    ret =3D bdrv_inactivate_all();
-    if (ret < 0) {
-        error_setg_errno(errp, -ret, "%s: Failed in bdrv_inactivate_all()",
-                         __func__);
+    if (!migration_block_inactivate()) {
+        error_setg(errp, "%s: Failed in bdrv_inactivate_all()", __func__);
         goto fail;
     }
-    restart_block =3D true;
=20
     /*
      * Cause any non-postcopiable, but iterative devices to
@@ -2624,8 +2611,6 @@ static int postcopy_start(MigrationState *ms, Error *=
*errp)
         goto fail_closefb;
     }
=20
-    restart_block =3D false;
-
     /* Now send that blob */
     if (qemu_savevm_send_packaged(ms->to_dst_file, bioc->data, bioc->usage=
)) {
         error_setg(errp, "%s: Failed to send packaged data", __func__);
@@ -2670,17 +2655,7 @@ fail_closefb:
 fail:
     migrate_set_state(&ms->state, MIGRATION_STATUS_POSTCOPY_ACTIVE,
                           MIGRATION_STATUS_FAILED);
-    if (restart_block) {
-        /* A failure happened early enough that we know the destination ha=
sn't
-         * accessed block devices, so we're safe to recover.
-         */
-        Error *local_err =3D NULL;
-
-        bdrv_activate_all(&local_err);
-        if (local_err) {
-            error_report_err(local_err);
-        }
-    }
+    migration_block_activate(NULL);
     migration_call_notifiers(ms, MIG_EVENT_PRECOPY_FAILED, NULL);
     bql_unlock();
     return -1;
@@ -2778,31 +2753,6 @@ static void migration_completion_postcopy(MigrationS=
tate *s)
     trace_migration_completion_postcopy_end_after_complete();
 }
=20
-static void migration_completion_failed(MigrationState *s,
-                                        int current_active_state)
-{
-    if (s->block_inactive && (s->state =3D=3D MIGRATION_STATUS_ACTIVE ||
-                              s->state =3D=3D MIGRATION_STATUS_DEVICE)) {
-        /*
-         * If not doing postcopy, vm_start() will be called: let's
-         * regain control on images.
-         */
-        Error *local_err =3D NULL;
-
-        bql_lock();
-        bdrv_activate_all(&local_err);
-        if (local_err) {
-            error_report_err(local_err);
-        } else {
-            s->block_inactive =3D false;
-        }
-        bql_unlock();
-    }
-
-    migrate_set_state(&s->state, current_active_state,
-                      MIGRATION_STATUS_FAILED);
-}
-
 /**
  * migration_completion: Used by migration_thread when there's not much le=
ft.
  *   The caller 'breaks' the loop when this returns.
@@ -2856,7 +2806,8 @@ fail:
         error_free(local_err);
     }
=20
-    migration_completion_failed(s, current_active_state);
+    migrate_set_state(&s->state, current_active_state,
+                      MIGRATION_STATUS_FAILED);
 }
=20
 /**
@@ -3286,6 +3237,11 @@ static void migration_iteration_finish(MigrationStat=
e *s)
     case MIGRATION_STATUS_FAILED:
     case MIGRATION_STATUS_CANCELLED:
     case MIGRATION_STATUS_CANCELLING:
+        /*
+         * Re-activate the block drives if they're inactivated.  Note, COLO
+         * shouldn't use block_active at all, so it should be no-op there.
+         */
+        migration_block_activate(NULL);
         if (runstate_is_live(s->vm_old_state)) {
             if (!runstate_check(RUN_STATE_SHUTDOWN)) {
                 vm_start();
@@ -3858,6 +3814,8 @@ static void migration_instance_init(Object *obj)
     ms->state =3D MIGRATION_STATUS_NONE;
     ms->mbps =3D -1;
     ms->pages_per_second =3D -1;
+    /* Freshly started QEMU owns all the block devices */
+    migration_block_active_setup(true);
     qemu_sem_init(&ms->pause_sem, 0);
     qemu_mutex_init(&ms->error_mutex);
=20
diff --git a/migration/savevm.c b/migration/savevm.c
index 706b77ffab..969a994a85 100644
--- a/migration/savevm.c
+++ b/migration/savevm.c
@@ -1547,19 +1547,18 @@ int qemu_savevm_state_complete_precopy_non_iterable=
(QEMUFile *f,
     }
=20
     if (inactivate_disks) {
-        /* Inactivate before sending QEMU_VM_EOF so that the
-         * bdrv_activate_all() on the other end won't fail. */
-        ret =3D bdrv_inactivate_all();
-        if (ret) {
-            error_setg(&local_err, "%s: bdrv_inactivate_all() failed (%d)",
-                       __func__, ret);
+        /*
+         * Inactivate before sending QEMU_VM_EOF so that the
+         * bdrv_activate_all() on the other end won't fail.
+         */
+        if (!migration_block_inactivate()) {
+            error_setg(&local_err, "%s: bdrv_inactivate_all() failed",
+                       __func__);
             migrate_set_error(ms, local_err);
             error_report_err(local_err);
-            qemu_file_set_error(f, ret);
+            qemu_file_set_error(f, -EFAULT);
             return ret;
         }
-        /* Remember that we did this */
-        s->block_inactive =3D true;
     }
     if (!in_postcopy) {
         /* Postcopy stream will still be going */
@@ -2123,7 +2122,6 @@ static int loadvm_postcopy_handle_listen(MigrationInc=
omingState *mis)
=20
 static void loadvm_postcopy_handle_run_bh(void *opaque)
 {
-    Error *local_err =3D NULL;
     MigrationIncomingState *mis =3D opaque;
=20
     trace_vmstate_downtime_checkpoint("dst-postcopy-bh-enter");
@@ -2146,12 +2144,11 @@ static void loadvm_postcopy_handle_run_bh(void *opa=
que)
          * Make sure all file formats throw away their mutable metadata.
          * If we get an error here, just don't restart the VM yet.
          */
-        bdrv_activate_all(&local_err);
+        bool success =3D migration_block_activate(NULL);
+
         trace_vmstate_downtime_checkpoint("dst-postcopy-bh-cache-invalidat=
ed");
-        if (local_err) {
-            error_report_err(local_err);
-            local_err =3D NULL;
-        } else {
+
+        if (success) {
             vm_start();
         }
     } else {
@@ -3193,11 +3190,7 @@ void qmp_xen_save_devices_state(const char *filename=
, bool has_live, bool live,
          * side of the migration take control of the images.
          */
         if (live && !saved_vm_running) {
-            ret =3D bdrv_inactivate_all();
-            if (ret) {
-                error_setg(errp, "%s: bdrv_inactivate_all() failed (%d)",
-                           __func__, ret);
-            }
+            migration_block_inactivate();
         }
     }
=20
diff --git a/monitor/qmp-cmds.c b/monitor/qmp-cmds.c
index 76f21e8af3..6f76d9beaf 100644
--- a/monitor/qmp-cmds.c
+++ b/monitor/qmp-cmds.c
@@ -31,6 +31,7 @@
 #include "qapi/type-helpers.h"
 #include "hw/mem/memory-device.h"
 #include "hw/intc/intc.h"
+#include "migration/misc.h"
=20
 NameInfo *qmp_query_name(Error **errp)
 {
@@ -103,13 +104,8 @@ void qmp_cont(Error **errp)
          * Continuing after completed migration. Images have been
          * inactivated to allow the destination to take control. Need to
          * get control back now.
-         *
-         * If there are no inactive block nodes (e.g. because the VM was
-         * just paused rather than completing a migration),
-         * bdrv_inactivate_all() simply doesn't do anything.
          */
-        bdrv_activate_all(&local_err);
-        if (local_err) {
+        if (!migration_block_activate(&local_err)) {
             error_propagate(errp, local_err);
             return;
         }
diff --git a/migration/meson.build b/migration/meson.build
index d53cf3417a..dac687ee3a 100644
--- a/migration/meson.build
+++ b/migration/meson.build
@@ -11,6 +11,7 @@ migration_files =3D files(
=20
 system_ss.add(files(
   'block-dirty-bitmap.c',
+  'block-active.c',
   'channel.c',
   'channel-block.c',
   'cpu-throttle.c',
diff --git a/migration/trace-events b/migration/trace-events
index bb0e0cc6dc..b82a1c5e40 100644
--- a/migration/trace-events
+++ b/migration/trace-events
@@ -383,3 +383,6 @@ migration_pagecache_insert(void) "Error allocating page"
 # cpu-throttle.c
 cpu_throttle_set(int new_throttle_pct)  "set guest CPU throttled by %d%%"
 cpu_throttle_dirty_sync(void) ""
+
+# block-active.c
+migration_block_activation(const char *name) "%s"
--=20
2.47.0