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 5C4A81FF0B7 for ; Mon, 30 Nov 2026 19:12:14 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id AC7D121867; Mon, 30 Nov 2026 19:10:54 +0100 (CET) From: Jonas Theisen To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server v3 10/18] fix #2924: migrate: block live migration of host CPU VMs to nodes lacking flags Date: Mon, 30 Nov 2026 19:09:42 +0100 Message-ID: <20261130181007.596064-11-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: 1796062237079 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: JWR3K7AYR23DZJYH6NBJL22RSJ2XBOGJ X-Message-ID-Hash: JWR3K7AYR23DZJYH6NBJL22RSJ2XBOGJ 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: The 'host' and 'max' CPU models pass the flags of the host CPU through to the guest. If the target node's CPU lacks flags that the guest might use, the live migration often fails only near its end, with QEMU on the target failing to set the special registers of the vCPUs, or the guest runs into problems after the migration. Compare the CPU flags that KVM supports on the source and the target node, which pvestatd already broadcasts, and refuse the migration if the target lacks any. Flags explicitly disabled for the VM and Hyper-V enlightenments, which are not passed through, are ignored. As the flags are broadcast independently of the other CPU information, this also works for target nodes running an older version. The flags are normalized, as nodes that did not query them again since an upgrade can still broadcast aliases of some flags. Only flags that QEMU recognizes are compared, as the broadcast also includes other CPU properties, such as 'migratable' or 'l3-cache', which can differ between QEMU versions without affecting the guest. The flags of the source node are only an approximation of the flags of the VM, as it might have been started on another node, and the flags KVM supports also depend on the kernel version. So allow root to override the check with the new 'allow-missing-cpu-flags' option. The existing 'force' option is not reused for this, as the HA stack always sets it, which would skip the check for all HA-managed VMs. Instead, the new option is forwarded to the HA stack like other migration options, so that it also applies to HA-managed VMs. Fixes: https://bugzilla.proxmox.com/show_bug.cgi?id=2924 Signed-off-by: Jonas Theisen --- src/PVE/API2/Qemu.pm | 20 +++- src/PVE/QemuMigrate.pm | 2 +- src/PVE/QemuMigrate/Helpers.pm | 107 ++++++++++++++++++++-- src/test/MigrationTest/QemuMigrateMock.pm | 11 +++ src/test/run_api_tests.pl | 16 +++- src/test/run_qemu_migrate_tests.pl | 88 +++++++++++++++++- 6 files changed, 225 insertions(+), 19 deletions(-) diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm index 5549d090..e0c60d9f 100644 --- a/src/PVE/API2/Qemu.pm +++ b/src/PVE/API2/Qemu.pm @@ -155,6 +155,13 @@ my $migrate_json_properties = { . ' successfully by the target. Using this flag can lead to dropped' . ' configuration options if the target is too old to parse them.', }, + 'allow-missing-cpu-flags' => { + type => 'boolean', + optional => 1, + default => 0, + description => "Allow live migration of VMs with CPU type 'host' or 'max' to nodes" + . " lacking CPU flags of this node. Only root may use this option.", + }, }; # Properties that will be forwarded via the HA stack to the LRM, which will then use them for its @@ -163,6 +170,7 @@ my $migrate_json_properties = { my $ha_migrate_props = { 'with-conntrack-state' => 1, 'skip-config-check' => 1, + 'allow-missing-cpu-flags' => 1, }; sub ha_migrate_json_properties { @@ -5611,12 +5619,18 @@ __PACKAGE__->register_method({ type => { type => 'string', description => "The check that failed.", - enum => ['cpu-vendor'], + enum => ['cpu-vendor', 'host-cpu-flags'], }, message => { type => 'string', description => "Why the node's CPU is incompatible.", }, + override => { + type => 'string', + optional => 1, + description => "The migration option that allows" + . " ignoring the incompatibility.", + }, }, }, }, @@ -5818,6 +5832,7 @@ __PACKAGE__->register_method({ for my $issue (($cpu_issues_by_node->{$node} // [])->@*) { my $reason = { type => $issue->{type}, message => $issue->{message} }; if ($issue->{severity} eq 'error') { + $reason->{override} = $issue->{override} if defined($issue->{override}); push $res->{not_allowed_nodes}->{$node}->{'incompatible-cpu'}->@*, $reason; } else { push $res->{'cpu-warnings'}->{$node}->@*, $reason; @@ -5897,6 +5912,9 @@ __PACKAGE__->register_method({ raise_param_exc({ force => "Only root may use this option." }) if $param->{force} && $authuser ne 'root@pam'; + raise_param_exc({ 'allow-missing-cpu-flags' => "Only root may use this option." }) + if $param->{'allow-missing-cpu-flags'} && $authuser ne 'root@pam'; + raise_param_exc({ migration_type => "Only root may use this option." }) if $param->{migration_type} && $authuser ne 'root@pam'; diff --git a/src/PVE/QemuMigrate.pm b/src/PVE/QemuMigrate.pm index 419f1bba..5c85ec36 100644 --- a/src/PVE/QemuMigrate.pm +++ b/src/PVE/QemuMigrate.pm @@ -327,7 +327,7 @@ sub prepare { # nodes of other clusters do not broadcast their CPU information to this cluster if (!$self->{opts}->{remote}) { my $warnings = PVE::QemuMigrate::Helpers::assert_target_cpu_compatibility( - $vmid, $conf, $running, $self->{node}, + $vmid, $conf, $running, $self->{node}, $self->{opts}, ); log_warn($_) for $warnings->@*; } diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm index d8b3eb35..bc821dca 100644 --- a/src/PVE/QemuMigrate/Helpers.pm +++ b/src/PVE/QemuMigrate/Helpers.pm @@ -4,6 +4,7 @@ use strict; use warnings; use JSON; +use List::Util qw(min); use PVE::Cluster; use PVE::INotify; @@ -128,6 +129,23 @@ my sub get_node_cpu_info { return ref($info) eq 'HASH' ? $info : {}; } +# Returns the CPU flags KVM supports on the node, as broadcast by pvestatd, if known. +my sub get_node_kvm_cpu_flags { + my ($node) = @_; + + # an empty value means that pvestatd failed to detect the flags + my $raw = PVE::Cluster::get_node_kv('cpuflags-kvm', $node)->{$node} or return; + + return { map { $_ => 1 } PVE::QemuServer::CPUFlags::parse_node_cpu_flags($raw) }; +} + +# Returns the CPU flags QEMU recognizes for the host architecture, if known. +my sub get_understood_cpu_flags { + my $arch = PVE::QemuServer::Helpers::get_host_arch(); + my $flags = eval { PVE::QemuServer::CPUFlags::query_understood_cpu_flags($arch) }; + return $flags ? { map { $_ => 1 } $flags->@* } : undef; +} + # 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 { @@ -159,11 +177,23 @@ my sub vendors_differ { return defined($source_vendor) && defined($target_vendor) && $source_vendor ne $target_vendor; } +my sub format_flag_list { + my ($flags) = @_; + + my $max_listed = 10; + my $list = join(', ', $flags->@[0 .. min($max_listed, scalar($flags->@*)) - 1]); + $list .= " and " . (scalar($flags->@*) - $max_listed) . " more" + if scalar($flags->@*) > $max_listed; + + return $list; +} + # Checks whether the CPU of a target node can run a running VM after a live migration. Each check # has: # - 'type', identifying the check # - 'severity', 'error' if an incompatibility prevents the migration, 'warning' if it is only # reported +# - 'override', optionally, the migration option that allows ignoring an error # - 'needs', listing the CPU information of the target node the check requires, nodes lacking any # of it are skipped # - 'applies', returning true if the check is relevant for the VM's CPU @@ -172,7 +202,9 @@ my sub vendors_differ { # The VM's CPU is parsed by parse_qemu_cpu_string(), with: # - 'kvm' set if the VM uses KVM # -# The CPU information of the nodes is in the format of get_host_cpu_info(). +# 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 +# - for the source node, the CPU flags QEMU recognizes as 'understood-flags' hash, if known my $target_cpu_checks = [ { type => 'cpu-vendor', @@ -211,6 +243,45 @@ my $target_cpu_checks = [ . " to work, as guests can become unstable, even some time after the migration"; }, }, + { + type => 'host-cpu-flags', + severity => 'error', + # the flags are only known per node, the VM might have been started on another node + override => 'allow-missing-cpu-flags', + needs => ['kvm-flags'], + applies => sub { + my ($cpu) = @_; + return is_host_cpu_model($cpu); + }, + check => sub { + my ($cpu, $source, $target) = @_; + + # already reported by the 'cpu-vendor' check + return if vendors_differ($source, $target); + + my ($source_flags, $target_flags) = + ($source->{'kvm-flags'}, $target->{'kvm-flags'}); + return if !$source_flags; + + # The broadcast also includes CPU properties that are no flags, for example + # 'migratable', which can differ between QEMU versions without affecting the guest. + my $understood = $source->{'understood-flags'}; + my @flags = grep { !$understood || $understood->{$_} } keys $source_flags->%*; + + my @missing = sort grep { + # Hyper-V enlightenments are not passed through, but set explicitly + !m/^hv-/ + && !$target_flags->{$_} + && (PVE::JSONSchema::parse_boolean($cpu->{props}->{$_} // 'on') // 1) + } @flags; + return if !scalar(@missing); + + return + "CPU model '$cpu->{model}' passes through the flags of the host CPU, but the" + . " node lacks: " + . format_flag_list(\@missing); + }, + }, ]; # Returns the issues of the target node. If it lacks CPU information needed by a check that can @@ -229,8 +300,9 @@ my sub get_target_cpu_issues { } my $message = $check->{check}->($cpu, $source_info, $target_info) or next; - push $issues->@*, - { type => $check->{type}, severity => $check->{severity}, message => $message }; + my $issue = { type => $check->{type}, severity => $check->{severity}, message => $message }; + $issue->{override} = $check->{override} if defined($check->{override}); + push $issues->@*, $issue; } if (!$complete) { @@ -273,7 +345,7 @@ with a C identifying the check, a C of C if the VM cannot or C if it might not, and a human-readable C. Nodes that lack CPU information needed by a check that can prevent the migration, for example as they run an older version, have an issue of type C with severity C, unless this node lacks that -information as well. +information as well. If a migration option allows ignoring an error, C names it. =cut @@ -291,12 +363,16 @@ sub check_target_cpu_compatibility { # Use what this node broadcasts, as for the target nodes. The local pvestatd might not # broadcast it yet, for example if it was not restarted after an upgrade. - my $source_info = get_node_cpu_info(PVE::INotify::nodename()); + my $local_node = PVE::INotify::nodename(); + 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(); my $issues_by_node = {}; for my $node ($nodes->@*) { my $target_info = get_node_cpu_info($node); + $target_info->{'kvm-flags'} = get_node_kvm_cpu_flags($node); my $issues = get_target_cpu_issues($cpu, $checks, $source_info, $target_info); $issues_by_node->{$node} = $issues if scalar($issues->@*); @@ -307,33 +383,44 @@ sub check_target_cpu_compatibility { =head3 assert_target_cpu_compatibility - my $warnings = assert_target_cpu_compatibility($vmid, $conf, $pid, $node); + my $warnings = assert_target_cpu_compatibility($vmid, $conf, $pid, $node, $opts); Checks whether the running VM can run with its current virtual CPU on the node C<$node>, see -check_target_cpu_compatibility(). Dies if the node is incompatible. +check_target_cpu_compatibility(). Dies if the node is incompatible, unless the migration options +C<$opts> override all the incompatibilities. Returns an array reference with the warnings to log, about possible incompatibilities, including -missing CPU information of the node. +missing CPU information of the node, and about overridden incompatibilities. =cut sub assert_target_cpu_compatibility { - my ($vmid, $conf, $pid, $node) = @_; + my ($vmid, $conf, $pid, $node, $opts) = @_; my $issues_by_node = check_target_cpu_compatibility($vmid, $conf, $pid, [$node]); my ($errors, $warnings) = ([], []); for my $issue (($issues_by_node->{$node} // [])->@*) { + my $override = $issue->{override}; if ($issue->{severity} eq 'warning') { push $warnings->@*, "CPU of node '$node' might be incompatible: $issue->{message}"; + } elsif (defined($override) && $opts->{$override}) { + push $warnings->@*, + "ignoring incompatible CPU of node '$node' due to '$override': $issue->{message}"; } else { push $errors->@*, $issue; } } if (scalar($errors->@*)) { + my $hint = ''; + if (!grep { !defined($_->{override}) } $errors->@*) { + my %overrides = map { $_->{override} => 1 } $errors->@*; + $hint = " - use " . join(' and ', map { "'$_'" } sort keys %overrides) . " to override"; + } die "can't migrate VM to '$node' - incompatible CPU: " - . join('; ', map { $_->{message} } $errors->@*) . "\n"; + . join('; ', map { $_->{message} } $errors->@*) + . "$hint\n"; } return $warnings; diff --git a/src/test/MigrationTest/QemuMigrateMock.pm b/src/test/MigrationTest/QemuMigrateMock.pm index fb62e3b8..e35aba9f 100644 --- a/src/test/MigrationTest/QemuMigrateMock.pm +++ b/src/test/MigrationTest/QemuMigrateMock.pm @@ -243,6 +243,10 @@ $MigrationTest::Shared::qemu_migrate_helpers_module->mock( $MigrationTest::Shared::cluster_module->mock( get_node_kv => sub { my ($key, $node) = @_; + if ($key eq 'cpuflags-kvm') { + my $flags = $test_cpu_info->{'kvm-flags'}->{$node} or return {}; + return { $node => $flags }; + } if ($key eq 'host-cpu-info') { # the source node is 'pve0', without 'source' get_host_cpu_info() is used my $info = @@ -254,6 +258,13 @@ $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)]; + }, +); + my $qemu_server_helpers_module = Test::MockModule->new("PVE::QemuServer::Helpers"); $qemu_server_helpers_module->mock( get_host_cpu_info => sub { diff --git a/src/test/run_api_tests.pl b/src/test/run_api_tests.pl index 5ae6b58d..0c339f52 100755 --- a/src/test/run_api_tests.pl +++ b/src/test/run_api_tests.pl @@ -338,15 +338,17 @@ subtest 'migration preconditions of the CPU of a running VM' => sub { # the node key-value entries of the source node 'localhost' and of the target nodes, which # differ from the source as their names say, while 'no-cpu-info' broadcasts none my $entries = sub { - my ($info) = @_; - return { 'host-cpu-info' => encode_json($info) }; + my ($info, $kvm_flags) = @_; + return { 'host-cpu-info' => encode_json($info), 'cpuflags-kvm' => $kvm_flags }; }; my $source = { vendor => 'GenuineIntel' }; my $node_kv = { - localhost => $entries->($source), - 'other-vendor' => $entries->({ $source->%*, vendor => 'AuthenticAMD' }), + localhost => $entries->($source, 'avx512f fpu sse'), + 'other-vendor' => + $entries->({ $source->%*, vendor => 'AuthenticAMD' }, 'avx512f fpu sse'), + 'missing-flag' => $entries->($source, 'fpu sse'), }; - my $nodes = ['localhost', 'other-vendor', 'no-cpu-info']; + my $nodes = ['localhost', 'other-vendor', 'no-cpu-info', 'missing-flag']; my $inotify_module = Test::MockModule->new('PVE::INotify'); $inotify_module->mock(nodename => sub { return 'localhost'; }); @@ -358,6 +360,9 @@ subtest 'migration preconditions of the CPU of a running VM' => sub { return ([], {}, { map { $_ => [] } $nodes->@* }); }, ); + my $cpu_flags_module = Test::MockModule->new('PVE::QemuServer::CPUFlags'); + $cpu_flags_module->mock( + query_understood_cpu_flags => sub { return [qw(avx512f fpu sse)]; }); $qemu_config_module->mock(load_config => sub { return { memory => 512 }; }); $qemu_server_module->mock(check_running => sub { return 1234; }); my $running_cpu; @@ -402,6 +407,7 @@ subtest 'migration preconditions of the CPU of a running VM' => sub { $cpu_reasons->('host,+kvm_pv_eoi,+kvm_pv_unhalt'), { 'other-vendor' => ['cpu-vendor'], + 'missing-flag' => ['host-cpu-flags (allow-missing-cpu-flags)'], 'no-cpu-info' => ['warning: missing-cpu-info'], }, "reasons for 'host'", diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl index 02e54f41..7e71501e 100755 --- a/src/test/run_qemu_migrate_tests.pl +++ b/src/test/run_qemu_migrate_tests.pl @@ -547,8 +547,15 @@ my $default_cpu_info = { nodes => { pve1 => { vendor => 'GenuineIntel' }, }, + 'kvm-flags' => { + pve0 => 'fpu sse', + pve1 => 'fpu sse', + }, }; +my $lacks_avx512f = + "CPU model 'host' passes through the flags of the host CPU, but the node lacks: avx512f"; + # Returns a test of the CPU checks for a live migration of VM 1033 to 'pve1'. The VM is configured # with the CPU 'cpu' and further settings in 'config', and runs with the QEMU CPU 'runningcpu'. # 'cpu_info' overrides $default_cpu_info. Without 'expect_die', the migration must succeed, and the @@ -596,8 +603,8 @@ 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', see cpu_migration_test() for tests of - # the CPU checks + # 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 # 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 @@ -1611,6 +1618,83 @@ my $tests = [ . " GenuineIntel is not guaranteed to work", ], ), + cpu_migration_test( + # properties that are no CPU flags can differ between QEMU versions + name => '1033_running_host_cpu_other_properties', + cpu => 'host', + runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt', + cpu_info => { + 'kvm-flags' => { pve0 => 'fpu l3-cache migratable sse x-vendor-cpuid-only-v2' }, + }, + ), + cpu_migration_test( + name => '1033_running_host_cpu_missing_flags', + cpu => 'host', + runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt', + cpu_info => { + 'kvm-flags' => { pve0 => 'avx512f fpu hv-relaxed sse' }, + }, + expect_die => "can't migrate VM to 'pve1' - incompatible CPU: $lacks_avx512f - use" + . " 'allow-missing-cpu-flags' to override", + ), + cpu_migration_test( + # 'force', which the HA stack always sets, must not override the check + name => '1033_running_host_cpu_missing_flags_force', + cpu => 'host', + runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt', + opts => { force => 1 }, + cpu_info => { + 'kvm-flags' => { pve0 => 'avx512f fpu hv-relaxed sse' }, + }, + expect_die => "can't migrate VM to 'pve1' - incompatible CPU: $lacks_avx512f - use" + . " 'allow-missing-cpu-flags' to override", + ), + cpu_migration_test( + name => '1033_running_host_cpu_missing_flags_allowed', + cpu => 'host', + runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt', + opts => { 'allow-missing-cpu-flags' => 1 }, + cpu_info => { + 'kvm-flags' => { pve0 => 'avx512f fpu hv-relaxed sse' }, + }, + expect_log => [ + "ignoring incompatible CPU of node 'pve1' due to 'allow-missing-cpu-flags':" + . " $lacks_avx512f", + ], + ), + cpu_migration_test( + # nodes running an older version broadcast their KVM CPU flags, but no 'host-cpu-info' + name => '1033_running_host_cpu_missing_flags_without_cpu_info', + cpu => 'host', + runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt', + cpu_info => { + nodes => { pve1 => undef }, + 'kvm-flags' => { pve0 => 'avx512f fpu hv-relaxed sse' }, + }, + expect_die => "can't migrate VM to 'pve1' - incompatible CPU: $lacks_avx512f - use" + . " 'allow-missing-cpu-flags' to override", + ), + cpu_migration_test( + # the guest cannot use flags disabled for the VM + name => '1033_running_host_cpu_missing_flags_disabled', + cpu => 'host', + runningcpu => 'host,-avx512f,sse4.2=false,+kvm_pv_eoi,+kvm_pv_unhalt', + cpu_info => { + 'kvm-flags' => { pve0 => 'avx512f fpu hv-relaxed sse sse4.2' }, + }, + ), + cpu_migration_test( + # nodes that did not query their flags since an upgrade still broadcast aliases + name => '1033_running_host_cpu_missing_flag_alias', + cpu => 'host', + runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt', + cpu_info => { + 'kvm-flags' => { pve0 => 'fpu sse sse4_2' }, + }, + expect_die => "can't migrate VM to 'pve1' - incompatible CPU: CPU model 'host' passes" + . " through the flags of the host CPU, but the node lacks: sse4.2 - use" + . " 'allow-missing-cpu-flags' to override", + ), { name => '105_replicated_to_non_replication_target', target => 'pve1', -- 2.47.3