From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id 576081FF0B7 for ; Mon, 30 Nov 2026 19:12:23 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 1543921873; Mon, 30 Nov 2026 19:10:55 +0100 (CET) From: Jonas Theisen 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 Message-ID: <20261130181007.596064-13-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261130181007.596064-1-j.theisen@proxmox.com> References: <20261130181007.596064-1-j.theisen@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1796062241259 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.062 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: XYCGPJ4HB5NUWONYY47CRW7QMLFMW3J4 X-Message-ID-Hash: XYCGPJ4HB5NUWONYY47CRW7QMLFMW3J4 X-MailFrom: j.theisen@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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