all lists on lists.proxmox.com
 help / color / mirror / Atom feed
From: Jonas Theisen <j.theisen@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server v3 12/18] migrate: block live migration if the target cannot run the virtual CPU
Date: Mon, 30 Nov 2026 19:09:44 +0100	[thread overview]
Message-ID: <20261130181007.596064-13-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261130181007.596064-1-j.theisen@proxmox.com>

For VMs with an abstracted CPU configuration, for example with a custom
CPU model or the 'nested-virt' flag, the target is started with the
QEMU CPU of the running instance, instead of generating it again from
the configuration. That QEMU CPU can contain flags that KVM on the
target does not support, for example the Intel-only 'hv-evmcs'
enlightenment that is enabled for Windows 11 guests on Intel hosts.
QEMU on an AMD target then refuses to start:

  kvm: Hyper-V enlightened VMCS (hv-evmcs) is not supported by kernel

and the migration fails only after its preparation.

The same applies to all other CPU models that do not pass through the
host CPU, like 'x86-64-v3' or 'Skylake-Server'. Their QEMU CPU is
generated again from the configuration, mostly with the same flags,
and as 'enforce' is set for them, QEMU on the target refuses to start
if KVM lacks any flag the model enables, including those it enables by
default. One exception is 'hv-evmcs', which is only enabled on Intel
hosts, so it is not checked for these models. Without nested
virtualization, which makes the CPU configuration abstracted, the guest
cannot use it anyway.

Check upfront whether KVM on the target supports all enabled Hyper-V
enlightenments of the virtual CPU, which QEMU always requires, and all
of its other enabled flags if 'enforce' is set. This is not needed with
TCG, which emulates all flags. The flags a CPU model enables by default
are queried from QEMU of the running VM. This uses the CPU flags that
pvestatd already broadcasts for each node. Only flags that KVM supports
on the source node are considered, so that QEMU CPU properties that are
not CPU flags are not reported as missing.

Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
 src/PVE/API2/Qemu.pm                      |   3 +-
 src/PVE/QemuMigrate/Helpers.pm            |  67 +++++++++++++-
 src/test/MigrationTest/QemuMigrateMock.pm |  11 ++-
 src/test/run_api_tests.pl                 |   4 +
 src/test/run_qemu_migrate_tests.pl        | 104 +++++++++++++++++++++-
 5 files changed, 184 insertions(+), 5 deletions(-)

diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm
index 1fd46733..9824a59c 100644
--- a/src/PVE/API2/Qemu.pm
+++ b/src/PVE/API2/Qemu.pm
@@ -5619,7 +5619,8 @@ __PACKAGE__->register_method({
                                     type => {
                                         type => 'string',
                                         description => "The check that failed.",
-                                        enum => ['cpu-vendor', 'host-cpu-flags'],
+                                        enum =>
+                                            ['cpu-vendor', 'host-cpu-flags', 'vm-cpu-flags'],
                                     },
                                     message => {
                                         type => 'string',
diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index a131104e..8d255716 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -201,6 +201,10 @@ my sub format_flag_list {
 #
 # The VM's CPU is parsed by parse_qemu_cpu_string(), with:
 # - 'kvm' set if the VM uses KVM
+# - 'kept' set if the target is started with the same QEMU CPU, see is_running_cpu_kept()
+# - 'enforce' set if QEMU refuses to start without all enabled flags
+# - the enabled flags as 'flags' hash, including the flags the CPU model enables by default if
+#   'enforce' is set
 #
 # The CPU information of the nodes is in the format of get_host_cpu_info(), plus:
 # - the CPU flags KVM supports as 'kvm-flags' hash
@@ -305,6 +309,39 @@ my $target_cpu_checks = [
                 . " identical CPUs, but the node has '$target_model' instead of '$source_model'";
         },
     },
+    {
+        type => 'vm-cpu-flags',
+        severity => 'error',
+        needs => ['kvm-flags'],
+        applies => sub {
+            my ($cpu) = @_;
+            # Other models are generated again from the configuration on the target, mostly with
+            # the same flags, so it must support them as well. With TCG, all flags are emulated.
+            return $cpu->{kvm} && ($cpu->{kept} || !is_host_cpu_model($cpu));
+        },
+        check => sub {
+            my ($cpu, $source, $target) = @_;
+
+            my ($source_flags, $target_flags) =
+                ($source->{'kvm-flags'}, $target->{'kvm-flags'});
+            return if !$source_flags;
+
+            # QEMU refuses to start with unsupported Hyper-V enlightenments, and with any other
+            # unsupported flag only if 'enforce' is set. Requiring support on the source skips
+            # properties that are no CPU flags. hv-evmcs is only enabled on Intel hosts, so the
+            # target does not enable it if the CPU is generated again there.
+            my @missing = sort grep {
+                ($cpu->{enforce} || m/^hv-/)
+                    && ($cpu->{kept} || $_ ne 'hv-evmcs')
+                    && $source_flags->{$_}
+                    && !$target_flags->{$_}
+            } keys $cpu->{flags}->%*;
+            return if !scalar(@missing);
+
+            return "KVM on the node does not support flags of the VM's virtual CPU: "
+                . format_flag_list(\@missing);
+        },
+    },
 ];
 
 # Returns the issues of the target node. If it lacks CPU information needed by a check that can
@@ -338,6 +375,18 @@ my sub get_target_cpu_issues {
     return $issues;
 }
 
+# Returns the flags a CPU model enables by default, as QEMU of the running VM defines them.
+my sub get_cpu_model_flags {
+    my ($vmid, $model, $understood) = @_;
+
+    my $expansion =
+        mon_cmd($vmid, 'query-cpu-model-expansion', type => 'full', model => { name => $model });
+
+    # the expansion also includes CPU properties that are no flags
+    return [grep { !$understood || $understood->{$_} }
+        PVE::QemuServer::CPUFlags::get_cpu_expansion_flags($expansion)];
+}
+
 =head3 is_running_cpu_kept
 
     my $kept = is_running_cpu_kept($conf);
@@ -380,6 +429,22 @@ sub check_target_cpu_compatibility {
     my $cpu = parse_qemu_cpu_string(PVE::QemuServer::CPUConfig::get_cpu_from_running_vm($pid));
     my $arch = PVE::QemuServer::Helpers::get_vm_arch($conf);
     $cpu->{kvm} = $conf->{kvm} // PVE::QemuServer::CPUConfig::is_native_arch($arch);
+    $cpu->{kept} = is_running_cpu_kept($conf);
+    $cpu->{enforce} = PVE::JSONSchema::parse_boolean($cpu->{props}->{enforce} // 'off');
+
+    my $understood = get_understood_cpu_flags();
+
+    $cpu->{flags} = {};
+    if ($cpu->{enforce} && $cpu->{kvm} && !is_host_cpu_model($cpu)) {
+        my $model_flags = eval { get_cpu_model_flags($vmid, $cpu->{model}, $understood) };
+        warn "unable to query the flags of CPU model '$cpu->{model}' - $@" if $@;
+        $cpu->{flags}->{$_} = 1 for ($model_flags // [])->@*;
+    }
+    for my $prop (keys $cpu->{props}->%*) {
+        my $enabled = PVE::JSONSchema::parse_boolean($cpu->{props}->{$prop});
+        next if !defined($enabled);
+        $enabled ? ($cpu->{flags}->{$prop} = 1) : delete($cpu->{flags}->{$prop});
+    }
 
     my $checks = [grep { $_->{applies}->($cpu) } $target_cpu_checks->@*];
     return {} if !scalar($checks->@*);
@@ -390,7 +455,7 @@ sub check_target_cpu_compatibility {
     my $source_info = get_node_cpu_info($local_node);
     $source_info = PVE::QemuServer::Helpers::get_host_cpu_info() if !scalar(keys $source_info->%*);
     $source_info->{'kvm-flags'} = get_node_kvm_cpu_flags($local_node);
-    $source_info->{'understood-flags'} = get_understood_cpu_flags();
+    $source_info->{'understood-flags'} = $understood;
 
     my $issues_by_node = {};
     for my $node ($nodes->@*) {
diff --git a/src/test/MigrationTest/QemuMigrateMock.pm b/src/test/MigrationTest/QemuMigrateMock.pm
index e35aba9f..6f351375 100644
--- a/src/test/MigrationTest/QemuMigrateMock.pm
+++ b/src/test/MigrationTest/QemuMigrateMock.pm
@@ -238,6 +238,15 @@ $MigrationTest::Shared::qemu_migrate_helpers_module->mock(
         return $MigrationTest::Shared::qemu_migrate_helpers_module->original(
             'check_target_cpu_compatibility')->(@_);
     },
+    mon_cmd => sub {
+        my ($vmid, $command, %params) = @_;
+
+        if ($command eq 'query-cpu-model-expansion') {
+            my $flags = $test_cpu_info->{'model-flags'}->{ $params{model}->{name} } // [];
+            return { model => { props => { map { $_ => JSON::true } $flags->@* } } };
+        }
+        die "mon_cmd (mocked) - implement me: $command";
+    },
 );
 
 $MigrationTest::Shared::cluster_module->mock(
@@ -261,7 +270,7 @@ $MigrationTest::Shared::cluster_module->mock(
 my $qemu_server_cpuflags_module = Test::MockModule->new("PVE::QemuServer::CPUFlags");
 $qemu_server_cpuflags_module->mock(
     query_understood_cpu_flags => sub {
-        return [qw(avx512f fpu hv-relaxed lahf-lm sse sse4.2)];
+        return [qw(avx512f avx512vl fpu hv-relaxed lahf-lm sse sse4.2)];
     },
 );
 
diff --git a/src/test/run_api_tests.pl b/src/test/run_api_tests.pl
index 634b909a..8e1d80ad 100755
--- a/src/test/run_api_tests.pl
+++ b/src/test/run_api_tests.pl
@@ -363,6 +363,8 @@ subtest 'migration preconditions of the CPU of a running VM' => sub {
         check_local_resources => sub {
             return ([], {}, { map { $_ => [] } $nodes->@* });
         },
+        # the expansion of the CPU model, which enables no flags by default
+        mon_cmd => sub { return { model => { props => {} } }; },
     );
     my $cpu_flags_module = Test::MockModule->new('PVE::QemuServer::CPUFlags');
     $cpu_flags_module->mock(
@@ -422,6 +424,8 @@ subtest 'migration preconditions of the CPU of a running VM' => sub {
         $cpu_reasons->('kvm64,enforce,+avx512f,+kvm_pv_eoi,+kvm_pv_unhalt'),
         {
             'other-vendor' => ['warning: cpu-vendor'],
+            'missing-flag' => ['vm-cpu-flags'],
+            'no-cpu-info' => ['warning: missing-cpu-info'],
         },
         'reasons for another CPU model',
     );
diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl
index ffd62701..a08cac19 100755
--- a/src/test/run_qemu_migrate_tests.pl
+++ b/src/test/run_qemu_migrate_tests.pl
@@ -609,8 +609,9 @@ my $tests = [
     # target_volids  - hash of volids on the target at the beginning
     # vm_status      - hash with running, runningmachine and optionally runningcpu
     # cpu_info       - hash with the CPU information of the 'source' node and of each
-    #                  other node in 'nodes', and the KVM CPU flags of each node in
-    #                  'kvm-flags', see cpu_migration_test() for tests of the CPU checks
+    #                  other node in 'nodes', the KVM CPU flags of each node in
+    #                  'kvm-flags', and the flags each CPU model enables by default in
+    #                  'model-flags', see cpu_migration_test() for tests of the CPU checks
     # expected_calls - hash whose keys are calls which are required
     #                  to be made if the migration gets far enough
     # expect_die     - expect the migration call to fail, and an error message
@@ -1724,6 +1725,105 @@ my $tests = [
                 . " 2.00GHz'",
         ],
     ),
+    cpu_migration_test(
+        name => '1033_running_kept_cpu_supported_flags',
+        cpu => 'kvm64,flags=+nested-virt',
+        runningcpu => 'kvm64,enforce,+vmx,hv_evmcs,hv_relaxed,hv_spinlocks=0x1fff,+kvm_pv_eoi',
+        cpu_kept => 1,
+        cpu_info => {
+            'kvm-flags' => {
+                pve0 => 'fpu hv-evmcs hv-relaxed hv-spinlocks kvm-pv-eoi sse vmx',
+                pve1 => 'fpu hv-evmcs hv-relaxed kvm-pv-eoi sse vmx',
+            },
+        },
+    ),
+    cpu_migration_test(
+        name => '1033_running_kept_cpu_unsupported_flags',
+        cpu => 'kvm64,flags=+nested-virt',
+        runningcpu => 'kvm64,enforce,+vmx,hv_evmcs,hv_relaxed,hv_spinlocks=0x1fff,+kvm_pv_eoi',
+        cpu_info => {
+            nodes => { pve1 => { vendor => 'AuthenticAMD' } },
+            'kvm-flags' => {
+                pve0 => 'fpu hv-evmcs hv-relaxed hv-spinlocks kvm-pv-eoi sse vmx',
+                pve1 => 'fpu hv-relaxed kvm-pv-eoi sse svm',
+            },
+        },
+        expect_die => "can't migrate VM to 'pve1' - incompatible CPU: KVM on the node does not"
+            . " support flags of the VM's virtual CPU: hv-evmcs, vmx",
+    ),
+    cpu_migration_test(
+        # hv-evmcs is only enabled on Intel hosts, so the target generates the CPU without it
+        name => '1033_running_builtin_cpu_hv_evmcs_other_vendor',
+        cpu => 'x86-64-v2-AES',
+        config => { ostype => 'win11' },
+        runningcpu => 'qemu64,+aes,enforce,hv_evmcs,hv_relaxed,+kvm_pv_eoi,+sse4.2',
+        cpu_info => {
+            nodes => { pve1 => { vendor => 'AuthenticAMD' } },
+            'kvm-flags' => {
+                pve0 => 'aes fpu hv-evmcs hv-relaxed kvm-pv-eoi sse sse4.2 vmx',
+                pve1 => 'aes fpu hv-relaxed kvm-pv-eoi sse sse4.2 svm',
+            },
+        },
+        expect_log => [
+            "CPU of node 'pve1' might be incompatible: live migration from GenuineIntel to"
+                . " AuthenticAMD is not guaranteed to work",
+        ],
+    ),
+    cpu_migration_test(
+        # with TCG, all flags are emulated
+        name => '1033_running_kept_cpu_tcg_target_without_kvm_flags',
+        cpu => 'custom-mycpu',
+        config => { kvm => 0 },
+        runningcpu => 'kvm64,enforce,+sse4.2',
+        cpu_kept => 1,
+        cpu_info => {
+            'kvm-flags' => { pve1 => undef },
+        },
+    ),
+    cpu_migration_test(
+        name => '1033_running_builtin_cpu_missing_flags',
+        cpu => 'x86-64-v3',
+        runningcpu =>
+            'qemu64,enforce,+abm,+aes,+avx,+avx2,+kvm_pv_eoi,+sse4.2,vendor=GenuineIntel',
+        cpu_info => {
+            'kvm-flags' => {
+                pve0 => 'abm aes avx avx2 fpu kvm-pv-eoi sse sse4.2',
+                pve1 => 'abm aes avx fpu kvm-pv-eoi sse sse4.2',
+            },
+        },
+        expect_die => "can't migrate VM to 'pve1' - incompatible CPU: KVM on the node does not"
+            . " support flags of the VM's virtual CPU: avx2",
+    ),
+    cpu_migration_test(
+        name => '1033_running_named_cpu_missing_model_flags',
+        cpu => 'Skylake-Server',
+        runningcpu => 'Skylake-Server,enforce,+kvm_pv_eoi,-avx512vl',
+        cpu_info => {
+            'kvm-flags' => {
+                pve0 => 'avx512f avx512vl fpu kvm-pv-eoi migratable sse sse4.2',
+                pve1 => 'fpu kvm-pv-eoi sse',
+            },
+            'model-flags' => {
+                'Skylake-Server' => [qw(avx512f avx512vl fpu migratable sse sse4_2)],
+            },
+        },
+        expect_die => "can't migrate VM to 'pve1' - incompatible CPU: KVM on the node does not"
+            . " support flags of the VM's virtual CPU: avx512f, sse4.2",
+    ),
+    cpu_migration_test(
+        name => '1033_running_named_cpu_supported_model_flags',
+        cpu => 'Skylake-Server',
+        runningcpu => 'Skylake-Server,enforce,+kvm_pv_eoi,-avx512vl',
+        cpu_info => {
+            'kvm-flags' => {
+                pve0 => 'avx512f fpu kvm-pv-eoi migratable sse sse4.2',
+                pve1 => 'avx512f fpu kvm-pv-eoi sse sse4.2',
+            },
+            'model-flags' => {
+                'Skylake-Server' => [qw(avx512f avx512vl fpu migratable sse sse4_2)],
+            },
+        },
+    ),
     {
         name => '105_replicated_to_non_replication_target',
         target => 'pve1',
-- 
2.47.3





  parent reply	other threads:[~2026-11-30 18:12 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30 18:09 [PATCH docs/manager/qemu-server v3 00/18] check the target node's CPU before live migrating a VM Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 01/18] buildsys: ignore migration test runtime files Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 02/18] test: migration: allow checking for warnings and errors in the log Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 03/18] cpu flags: normalize underscores in all CPU flag names Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 04/18] test: api: factor out validating the migration preconditions Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 05/18] cpu flags: factor out parsing the broadcast CPU flags of a node Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 06/18] migrate: factor out whether the running virtual CPU is kept Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 07/18] cpu flags: factor out the flags of a CPU model expansion Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 08/18] migrate: block live migration of host CPU VMs between CPU vendors Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 09/18] fix #6535: migrate: warn about live migration " Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 10/18] fix #2924: migrate: block live migration of host CPU VMs to nodes lacking flags Jonas Theisen
2026-11-30 18:09 ` [PATCH qemu-server v3 11/18] migrate: warn about live migration of host CPU VMs between CPU models Jonas Theisen
2026-11-30 18:09 ` Jonas Theisen [this message]
2026-11-30 18:09 ` [PATCH qemu-server v3 13/18] api: migrate: check the CPU of the target before requesting HA migration Jonas Theisen
2026-11-30 18:09 ` [PATCH manager v3 14/18] pvestatd: broadcast host CPU information relevant for live migration Jonas Theisen
2026-11-30 18:09 ` [PATCH manager v3 15/18] ui: migrate: show why the CPU of the target node is incompatible Jonas Theisen
2026-11-30 18:09 ` [PATCH manager v3 16/18] fix #6535: ui: migrate: show CPU warnings for the target node Jonas Theisen
2026-11-30 18:09 ` [PATCH manager v3 17/18] api: nodes: migrateall: skip VMs whose CPU is incompatible with the target Jonas Theisen
2026-11-30 18:09 ` [PATCH docs v3 18/18] qm: document the CPU checks for live migration Jonas Theisen

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=20261130181007.596064-13-j.theisen@proxmox.com \
    --to=j.theisen@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal