From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 023701FF066 for ; Tue, 24 Nov 2026 17:33:51 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id BC395217FF; Tue, 24 Nov 2026 17:32:45 +0100 (CET) From: Jonas Theisen To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server v2 08/20] migrate: block migration if the target cannot run the virtual CPU Date: Tue, 24 Nov 2026 17:30:32 +0100 Message-ID: <20261124163136.320992-9-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261124163136.320992-1-j.theisen@proxmox.com> References: <20261124163136.320992-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: 1795537924094 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.076 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: 64CYAF7GUJST5QMWDHFRS4RZOKC2KBXU X-Message-ID-Hash: 64CYAF7GUJST5QMWDHFRS4RZOKC2KBXU 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, but 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. 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. 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 | 7 +- src/PVE/QemuMigrate.pm | 4 +- src/PVE/QemuMigrate/Helpers.pm | 100 +++++++++-- src/test/MigrationTest/QemuMigrateMock.pm | 13 +- src/test/run_qemu_migrate_tests.pl | 203 ++++++++++++++++++++++ 5 files changed, 307 insertions(+), 20 deletions(-) diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm index d51a52ce..ba1ac71c 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', @@ -5809,7 +5810,9 @@ __PACKAGE__->register_method({ my $cpu_issues_by_node = eval { my $targets = [grep { $_ ne $localnode } $nodelist->@*]; my ($issues, $unchecked_nodes) = - PVE::QemuMigrate::Helpers::check_target_cpu_compatibility($vmconf, $pid, $targets); + PVE::QemuMigrate::Helpers::check_target_cpu_compatibility( + $vmid, $vmconf, $pid, $targets, + ); my $message = "the node does not provide CPU information, for example as it runs" . " an older version"; for my $node ($unchecked_nodes->@*) { diff --git a/src/PVE/QemuMigrate.pm b/src/PVE/QemuMigrate.pm index 245b79d7..0b286f58 100644 --- a/src/PVE/QemuMigrate.pm +++ b/src/PVE/QemuMigrate.pm @@ -328,7 +328,9 @@ sub prepare { if (!$self->{opts}->{remote}) { my $node = $self->{node}; my ($cpu_issues, $unchecked_nodes) = - PVE::QemuMigrate::Helpers::check_target_cpu_compatibility($conf, $running, [$node]); + PVE::QemuMigrate::Helpers::check_target_cpu_compatibility( + $vmid, $conf, $running, [$node], + ); my @errors; for my $issue (($cpu_issues->{$node} // [])->@*) { diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm index 844bc250..48e4da5e 100644 --- a/src/PVE/QemuMigrate/Helpers.pm +++ b/src/PVE/QemuMigrate/Helpers.pm @@ -146,6 +146,13 @@ my sub get_understood_cpu_flags { return $flags ? { map { $_ => 1 } $flags->@* } : undef; } +# QEMU accepts both underscores and dashes in property names, and some aliases +my sub normalize_cpu_property { + my ($name) = @_; + $name =~ s/_/-/g; + return PVE::QemuServer::CPUFlags::normalize_cpu_flag($name); +} + # Parses a QEMU '-cpu' argument into the CPU model and its properties. Flags given without a value # are stored as 'on' or 'off'. my sub parse_qemu_cpu_string { @@ -156,9 +163,7 @@ my sub parse_qemu_cpu_string { my $props = {}; for my $option (@options) { my ($op, $name, $value) = $option =~ m/^([+-]?)([^=]+)(?:=(.*))?$/ or next; - # QEMU accepts both underscores and dashes in property names, and some aliases - $name =~ s/_/-/g; - $name = PVE::QemuServer::CPUFlags::normalize_cpu_flag($name); + $name = normalize_cpu_property($name); $value = $op eq '-' ? 'off' : 'on' if !defined($value); $props->{$name} = $value; } @@ -193,11 +198,14 @@ my sub format_flag_list { # Checks whether the CPU of a target node can run a running VM after a live migration. # # 'applies' returns true if the check is relevant for the VM's CPU, as parsed by -# parse_qemu_cpu_string(), with 'kvm' set if the VM uses KVM. 'needs' lists the CPU information -# of the target node the check requires, nodes lacking any of it are skipped. The CPU information -# is in the format of get_host_cpu_info(), plus the CPU flags KVM supports as 'kvm-flags' hash. -# The source node's information also has the CPU flags QEMU recognizes as 'understood-flags' hash, -# if known. 'check' returns why the target node is incompatible, if it is. With the 'error' +# 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, and the enabled flags as 'flags' hash. If 'enforce' is set, +# they include the flags the CPU model enables by default. 'needs' lists the CPU information of +# the target node the check requires, nodes lacking any of it are skipped. The CPU information is +# in the format of get_host_cpu_info(), plus the CPU flags KVM supports as 'kvm-flags' hash. The +# source node's information also has the CPU flags QEMU recognizes as 'understood-flags' hash, if +# known. 'check' returns why the target node is incompatible, if it is. With the 'error' # severity, an incompatibility prevents the migration, with 'warning' it is only reported. # 'override' names the migration option that allows ignoring an error. my $target_cpu_checks = [ @@ -300,6 +308,35 @@ 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, but with + # the same flags, so it must support them as well + return $cpu->{kept} || ($cpu->{kvm} && !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. + my @missing = sort grep { + ($cpu->{enforce} || m/^hv-/) && $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, and whether all checks that can prevent the migration @@ -323,6 +360,22 @@ my sub get_target_cpu_issues { return ($issues, $complete); } +# 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 }); + my $props = $expansion->{model}->{props} // {}; + + # the expansion also includes CPU properties that are no flags + return [ + grep { !$understood || $understood->{$_} } + map { normalize_cpu_property($_) } + grep { ($props->{$_} // '') eq JSON::true } keys $props->%* + ]; +} + =head3 is_running_cpu_kept my $kept = is_running_cpu_kept($conf); @@ -341,12 +394,13 @@ sub is_running_cpu_kept { =head3 check_target_cpu_compatibility - my ($issues_by_node, $unchecked_nodes) = check_target_cpu_compatibility($conf, $pid, $nodes); + my ($issues_by_node, $unchecked_nodes) = + check_target_cpu_compatibility($vmid, $conf, $pid, $nodes); -Checks whether the VM with the configuration C<$conf>, running with the process ID C<$pid>, can -run with its current virtual CPU on each node of C<$nodes>, based on the CPU information the -nodes broadcast. Only applies to running VMs, as stopped VMs are started with a virtual CPU -matching the target node. +Checks whether the VM C<$vmid> with the configuration C<$conf>, running with the process ID +C<$pid>, can run with its current virtual CPU on each node of C<$nodes>, based on the CPU +information the nodes broadcast. Only applies to running VMs, as stopped VMs are started with a +virtual CPU matching the target node. Returns a hash reference with the list of issues for each node that has any, and an array reference with the nodes that could not be checked for issues preventing the migration, because @@ -358,12 +412,28 @@ allows ignoring an error, C names it. =cut sub check_target_cpu_compatibility { - my ($conf, $pid, $nodes) = @_; + my ($vmid, $conf, $pid, $nodes) = @_; return ({}, []) if !$pid; my $cpu = parse_qemu_cpu_string(PVE::QemuServer::CPUConfig::get_cpu_from_running_vm($pid)); $cpu->{kvm} = $conf->{kvm} // 1; + $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->@*); @@ -374,7 +444,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, $unchecked_nodes) = ({}, []); for my $node ($nodes->@*) { diff --git a/src/test/MigrationTest/QemuMigrateMock.pm b/src/test/MigrationTest/QemuMigrateMock.pm index f8d30b05..6295736f 100644 --- a/src/test/MigrationTest/QemuMigrateMock.pm +++ b/src/test/MigrationTest/QemuMigrateMock.pm @@ -232,12 +232,21 @@ $qemu_server_cpuconfig_module->mock( $MigrationTest::Shared::qemu_migrate_helpers_module->mock( check_target_cpu_compatibility => sub { - my ($conf, $pid, $nodes) = @_; + my ($vmid, $conf, $pid, $nodes) = @_; # checking requires the running CPU, which most tests do not need to specify return ({}, []) if $pid && !defined($vm_status->{runningcpu}); 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_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl index 59b2ade2..9cd85990 100755 --- a/src/test/run_qemu_migrate_tests.pl +++ b/src/test/run_qemu_migrate_tests.pl @@ -1921,6 +1921,209 @@ my $tests = [ }, }, }, + { + name => '1033_running_kept_cpu_supported_flags', + target => 'pve1', + vmid => 1033, + vm_status => { + running => 1, + runningmachine => 'pc-q35-5.0+pve0', + runningcpu => + 'kvm64,enforce,+vmx,hv_evmcs,hv_relaxed,hv_spinlocks=0x1fff,+kvm_pv_eoi', + }, + cpu_info => { + source => { vendor => 'GenuineIntel' }, + nodes => { + pve1 => { vendor => 'GenuineIntel' }, + }, + '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', + }, + }, + opts => { + online => 1, + }, + config_patch => { + cpu => 'kvm64,flags=+nested-virt', + }, + expected_calls => $default_expected_calls_online, + expected => { + source_volids => {}, + target_volids => {}, + vm_config => get_patched_config(1033, { + cpu => 'kvm64,flags=+nested-virt', + }), + vm_status => { + running => 1, + runningmachine => 'pc-q35-5.0+pve0', + runningcpu => + 'kvm64,enforce,+vmx,hv_evmcs,hv_relaxed,hv_spinlocks=0x1fff,+kvm_pv_eoi', + }, + }, + }, + { + name => '1033_running_kept_cpu_unsupported_flags', + target => 'pve1', + vmid => 1033, + vm_status => { + running => 1, + runningmachine => 'pc-q35-5.0+pve0', + runningcpu => + 'kvm64,enforce,+vmx,hv_evmcs,hv_relaxed,hv_spinlocks=0x1fff,+kvm_pv_eoi', + }, + cpu_info => { + source => { vendor => 'GenuineIntel' }, + 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', + }, + }, + opts => { + online => 1, + }, + config_patch => { + cpu => 'kvm64,flags=+nested-virt', + }, + expected_calls => {}, + 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", + expected => { + source_volids => {}, + target_volids => {}, + vm_config => get_patched_config(1033, { + cpu => 'kvm64,flags=+nested-virt', + }), + vm_status => { + running => 1, + runningmachine => 'pc-q35-5.0+pve0', + runningcpu => + 'kvm64,enforce,+vmx,hv_evmcs,hv_relaxed,hv_spinlocks=0x1fff,+kvm_pv_eoi', + }, + }, + }, + { + name => '1033_running_builtin_cpu_missing_flags', + target => 'pve1', + vmid => 1033, + vm_status => { + running => 1, + runningmachine => 'pc-q35-5.0+pve0', + 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', + }, + }, + opts => { + online => 1, + }, + config_patch => { + cpu => 'x86-64-v3', + }, + expected_calls => {}, + 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", + expected => { + source_volids => {}, + target_volids => {}, + vm_config => get_patched_config(1033, { + cpu => 'x86-64-v3', + }), + vm_status => { + running => 1, + runningmachine => 'pc-q35-5.0+pve0', + runningcpu => + 'qemu64,enforce,+abm,+aes,+avx,+avx2,+kvm_pv_eoi,+sse4.2,vendor=GenuineIntel', + }, + }, + }, + { + name => '1033_running_named_cpu_missing_model_flags', + target => 'pve1', + vmid => 1033, + vm_status => { + running => 1, + runningmachine => 'pc-q35-5.0+pve0', + 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)], + }, + }, + opts => { + online => 1, + }, + config_patch => { + cpu => 'Skylake-Server', + }, + expected_calls => {}, + 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", + expected => { + source_volids => {}, + target_volids => {}, + vm_config => get_patched_config(1033, { + cpu => 'Skylake-Server', + }), + vm_status => { + running => 1, + runningmachine => 'pc-q35-5.0+pve0', + runningcpu => 'Skylake-Server,enforce,+kvm_pv_eoi,-avx512vl', + }, + }, + }, + { + name => '1033_running_named_cpu_supported_model_flags', + target => 'pve1', + vmid => 1033, + vm_status => { + running => 1, + runningmachine => 'pc-q35-5.0+pve0', + 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)], + }, + }, + opts => { + online => 1, + }, + config_patch => { + cpu => 'Skylake-Server', + }, + expected_calls => $default_expected_calls_online, + expected => { + source_volids => {}, + target_volids => {}, + vm_config => get_patched_config(1033, { + cpu => 'Skylake-Server', + }), + vm_status => { + running => 1, + runningmachine => 'pc-q35-5.0+pve0', + }, + }, + }, { name => '105_replicated_to_non_replication_target', target => 'pve1', -- 2.47.3