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 4FA0D1FF0BC for ; Wed, 02 Dec 2026 16:22:07 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 17C2221871; Wed, 02 Dec 2026 16:20:04 +0100 (CET) From: Jonas Theisen To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server v4 11/21] fix #2924: migrate: block live migration of host CPU VMs to nodes lacking flags Date: Wed, 2 Dec 2026 16:18:07 +0100 Message-ID: <20261202151831.414110-12-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261202151831.414110-1-j.theisen@proxmox.com> References: <20261202151831.414110-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: 1796224741139 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.054 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: M2GOVYVO6WN5IJ5WNALETZCV4426OPTE X-Message-ID-Hash: M2GOVYVO6WN5IJ5WNALETZCV4426OPTE 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 CPU lacks flags 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 afterwards. Compare the CPU flags KVM supports on the source and the target, which pvestatd already broadcasts, so this also works for targets running an older version, and refuse the migration if the target lacks any. Ignored are flags disabled for the VM, Hyper-V enlightenments, which are not passed through, and properties QEMU does not recognize as flags, like 'migratable' or 'l3-cache', which the broadcast includes and which can differ between QEMU versions. The message lists at most ten missing flags, the precondition API reports all of them as details. Flags that only tell the guest which CPU vulnerabilities do not affect it, those of the IA32_ARCH_CAPABILITIES MSR on Intel, like 'its-no' or 'gds-no', and CPUID flags on AMD, like 'srso-no' or 'tsa-l1-no', only cause a warning, as KVM reports some of them only with newer kernels. A guest that saw them might have skipped a mitigation, which only matters if the target CPU is affected, but it keeps running. 'tsx-ctrl' is still checked, as it allows the guest to access an MSR. Warn if the broadcast flags of this node are missing, for example as the entry got lost when pmxcfs was restarted, instead of silently skipping the check. The flags of the source node only approximate those of the VM, as it might have been started on another node, and they depend on the kernel version. So allow overriding the check with the new 'allow-missing-cpu-flags' option. It only risks a failed migration or a crash of the migrated guest, so it needs no privilege beyond 'VM.Migrate'. 'force' is not reused, as the HA stack always sets it. The option is forwarded to the HA stack like other migration options. This requires pve-manager 9.2.31 or newer on this node, otherwise the precondition API does not offer it for HA-managed VMs. As the HA stack passes it on to VMs in positive affinity, forwarding it requires 'VM.Migrate' on these as well. Fixes: https://bugzilla.proxmox.com/show_bug.cgi?id=2924 Signed-off-by: Jonas Theisen --- src/PVE/API2/Qemu.pm | 53 ++++++- src/PVE/QemuMigrate.pm | 2 +- src/PVE/QemuMigrate/Helpers.pm | 183 ++++++++++++++++++++-- src/test/MigrationTest/QemuMigrateMock.pm | 12 ++ src/test/run_api_tests.pl | 108 ++++++++++++- src/test/run_qemu_migrate_tests.pl | 142 ++++++++++++++++- 6 files changed, 473 insertions(+), 27 deletions(-) diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm index 2e916bb2..5227a0cc 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.", + }, }; # Properties that will be forwarded via the HA stack to the LRM, which will then use them for its @@ -163,8 +170,16 @@ my $migrate_json_properties = { my $ha_migrate_props = { 'with-conntrack-state' => 1, 'skip-config-check' => 1, + 'allow-missing-cpu-flags' => 1, }; +# Only nodes with pve-manager 9.2.31 or newer have an ha-manager that accepts additional migration +# options. +my sub ha_forwards_migrate_options { + my ($node) = @_; + return PVE::Cluster::node_pvecfg_version_at_least($node, 9, 2, 31); +} + sub ha_migrate_json_properties { my $forwarded_props = {}; $forwarded_props->{$_} = $migrate_json_properties->{$_} for keys $ha_migrate_props->%*; @@ -5611,12 +5626,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.", + }, PVE::QemuMigrate::Helpers::cpu_issue_details_schema()->%*, }, }, @@ -5711,7 +5732,12 @@ __PACKAGE__->register_method({ type => { type => 'string', description => "The check that reported the warning.", - enum => ['cpu-vendor', 'missing-cpu-info', 'cpu-check-failed'], + enum => [ + 'cpu-vendor', + 'missing-cpu-info', + 'host-cpu-flags', + 'cpu-check-failed', + ], }, message => { type => 'string', @@ -5782,12 +5808,14 @@ __PACKAGE__->register_method({ PVE::QemuServer::check_local_storage_availability($vmconf, $storecfg); my $blocking_ha_resources_by_node = {}; + my $offers_overrides = 1; if (PVE::HA::Config::vm_is_ha_managed($vmid)) { (my $dependent_ha_resource, $blocking_ha_resources_by_node) = PVE::HA::Config::get_resource_motion_info("vm:$vmid"); $res->{'dependent-ha-resources'} = $dependent_ha_resource // []; + $offers_overrides = ha_forwards_migrate_options($localnode); } my $has_identity = PVE::QemuConfig->has_stored_identity($vmid); @@ -5833,6 +5861,8 @@ __PACKAGE__->register_method({ ($issue->{details} // {})->%*, }; if ($issue->{severity} eq 'error') { + $reason->{override} = $issue->{override} + if defined($issue->{override}) && $offers_overrides; push $res->{not_allowed_nodes}->{$node}->{'incompatible-cpu'}->@*, $reason; } else { push $res->{'cpu-warnings'}->{$node}->@*, $reason; @@ -5976,15 +6006,14 @@ __PACKAGE__->register_method({ } if (PVE::HA::Config::vm_is_ha_managed($vmid) && $rpcenv->{type} ne 'ha') { + my $forwards_options = ha_forwards_migrate_options($localnode); my $hacmd = sub { my $upid = shift; my $extra_opts = ''; my $cmd = ['ha-manager', 'migrate', "vm:$vmid", $target]; - # Only nodes with pve-manager 9.2.31 or newer have an ha-manager that accepts - # additional migration options. - if (PVE::Cluster::node_pvecfg_version_at_least($localnode, 9, 2, 31)) { + if ($forwards_options) { for my $prop (sort keys $ha_migrate_props->%*) { if (defined(my $value = $param->{$prop})) { push($cmd->@*, "--$prop", $value); @@ -6002,6 +6031,20 @@ __PACKAGE__->register_method({ return; }; + # The HA stack passes the options on to the VMs in positive affinity with this one. + if ($forwards_options && $param->{'allow-missing-cpu-flags'}) { + my ($dependent_resources) = + PVE::HA::Config::get_resource_motion_info("vm:$vmid"); + for my $sid ($dependent_resources->@*) { + my ($type, $id) = PVE::HA::Config::parse_sid($sid); + next if $type ne 'vm'; + raise_perm_exc( + "/vms/$id, VM.Migrate - 'allow-missing-cpu-flags' also applies" + . " to HA resource '$sid' in positive affinity") + if !$rpcenv->check_vm_perm($authuser, $id, undef, ['VM.Migrate'], 0, 1); + } + } + return $rpcenv->fork_worker('hamigrate', $vmid, $authuser, $hacmd); } else { diff --git a/src/PVE/QemuMigrate.pm b/src/PVE/QemuMigrate.pm index d450cd23..21c0a73d 100644 --- a/src/PVE/QemuMigrate.pm +++ b/src/PVE/QemuMigrate.pm @@ -255,7 +255,7 @@ sub prepare { # before setting up the conntrack state helper, as a failing preparation is not cleaned up. if (!$self->{opts}->{remote}) { my $warnings = PVE::QemuMigrate::Helpers::assert_target_cpu_compatibility( - $vmid, $conf, $pid, $self->{node}, + $vmid, $conf, $pid, $self->{node}, $self->{opts}, ); log_warn($_) for $warnings->@*; } diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm index 583d77bc..c346128c 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 Storable qw(dclone); use PVE::Cluster; @@ -129,6 +130,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'. As in QEMU, flags with '+' override properties, and flags with '-' # override both, regardless of their position. @@ -180,6 +198,12 @@ my $cpu_issue_details = { optional => 1, description => "The CPU vendor of the target node.", }, + 'missing-flags' => { + type => 'array', + optional => 1, + description => "The CPU flags the message refers to, as it might only list some.", + items => { type => 'string' }, + }, }; my sub vendor_details { @@ -200,11 +224,64 @@ sub cpu_issue_details_schema { return dclone($cpu_issue_details); } +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; +} + +# Bits of the IA32_ARCH_CAPABILITIES MSR on Intel that only tell the guest which CPU +# vulnerabilities do not affect it or how to mitigate them, and CPUID bits on AMD that only tell it +# which do not affect it. KVM reports some of them only with newer kernels. A guest that saw them +# might skip mitigations, but does not need them to keep running. tsx-ctrl is not included, as it +# allows the guest to access an MSR. +my $vulnerability_info_flags = { + map { $_ => 1 } + qw( + amd-no-ssb bhi-no fb-clear fbsdp-no gds-no ibrs-all its-no mds-no pbrsb-no pschange-mc-no + psdp-no rdctl-no rfds-clear rfds-no rsba sbdr-ssdp-no skip-l1dfl-vmentry srso-no + srso-user-kernel-no ssb-no taa-no tsa-l1-no tsa-sq-no + ) +}; + +# Returns the flags of the source node that the target node lacks and that the VM might use, and +# separately those that only inform the guest about CPU vulnerabilities. +my sub get_missing_host_cpu_flags { + 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'}); + + # 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; + + my $info = [grep { $vulnerability_info_flags->{$_} } @missing]; + my $other = [grep { !$vulnerability_info_flags->{$_} } @missing]; + return ($other, $info); +} + # 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 the check requires of the target node and, to compare # them, of this node, nodes lacking any of it are skipped # - 'applies', returning true if the check is relevant for the VM's CPU @@ -214,7 +291,9 @@ sub cpu_issue_details_schema { # 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', @@ -255,33 +334,91 @@ my $target_cpu_checks = [ return ($message, vendor_details($source, $target)); }, }, + { + 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) = @_; + + my ($missing) = get_missing_host_cpu_flags($cpu, $source, $target); + return if !scalar($missing->@*); + + my $message = + "CPU model '$cpu->{model}' passes through the flags of the host CPU, but the" + . " node lacks: " + . format_flag_list($missing); + return ($message, { 'missing-flags' => $missing }); + }, + }, + { + type => 'host-cpu-flags', + severity => 'warning', + needs => ['kvm-flags'], + applies => sub { + my ($cpu) = @_; + return is_host_cpu_model($cpu); + }, + check => sub { + my ($cpu, $source, $target) = @_; + + my (undef, $missing) = get_missing_host_cpu_flags($cpu, $source, $target); + return if !scalar($missing->@*); + + my $message = + "the node does not report CPU flags that tell the guest which CPU" + . " vulnerabilities do not affect it, for example as it runs another kernel" + . " version, so the guest might not mitigate them: " + . format_flag_list($missing); + return ($message, { 'missing-flags' => $missing }); + }, + }, ]; # Returns the issues of the target node. If it lacks CPU information needed by a check that can # prevent the migration, that is an issue as well, but only a warning, as the node is not refused. # Information this node lacks as well, for example the vendor of an unknown ARM CPU, is not -# reported. +# reported. Lost CPU flags of this node are reported, as it cannot determine them itself. my sub get_target_cpu_issues { my ($cpu, $checks, $source_info, $target_info) = @_; - my ($issues, $target_complete) = ([], 1); + my ($issues, $target_complete, $source_complete) = ([], 1, 1); for my $check ($checks->@*) { + my $blocking = $check->{severity} eq 'error'; my @missing = grep { !defined($target_info->{$_}) } $check->{needs}->@*; if (scalar(@missing)) { - $target_complete = 0 - if $check->{severity} eq 'error' && grep { defined($source_info->{$_}) } @missing; + $target_complete = 0 if $blocking && grep { defined($source_info->{$_}) } @missing; + next; + } + if (my @source_missing = grep { !defined($source_info->{$_}) } $check->{needs}->@*) { + # This node determines its other information itself if it does not broadcast it, but + # its CPU flags can get lost, for example as pmxcfs was restarted. + $source_complete = 0 if $blocking && grep { $_ eq 'kvm-flags' } @source_missing; next; } my ($message, $details) = $check->{check}->($cpu, $source_info, $target_info) or next; my $issue = { type => $check->{type}, severity => $check->{severity}, message => $message }; $issue->{details} = $details if $details; + $issue->{override} = $check->{override} if defined($check->{override}); push $issues->@*, $issue; } if (!$target_complete) { my $message = "the node does not provide the CPU information needed to check it, for" - . " example as it runs an older version"; + . " example as it runs an older version or pvestatd did not broadcast it yet"; + push $issues->@*, + { type => 'missing-cpu-info', severity => 'warning', message => $message }; + } + if (!$source_complete) { + my $message = "the source node does not provide the CPU information needed to check it," + . " for example until pvestatd broadcasts it again after a restart of pmxcfs"; push $issues->@*, { type => 'missing-cpu-info', severity => 'warning', message => $message }; } @@ -320,7 +457,9 @@ or C if it might not, a human-readable C, and optionally C with severity C, unless this node lacks that -information as well. +information as well. If this node lacks its broadcast CPU flags, all nodes have such an issue, as +the checks needing them are skipped. If a migration option allows ignoring an error, C +names it. =cut @@ -339,12 +478,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->@*); @@ -355,19 +498,21 @@ 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. If the check itself fails, the node is not refused, but a -warning about the failure is returned, as the check only guards migrations that work without it. +missing CPU information of the node, and about overridden incompatibilities. If the check itself +fails, the node is not refused, but a warning about the failure is returned, as the check only +guards migrations that work without it. =cut sub assert_target_cpu_compatibility { - my ($vmid, $conf, $pid, $node) = @_; + my ($vmid, $conf, $pid, $node, $opts) = @_; my $issues_by_node = eval { check_target_cpu_compatibility($vmid, $conf, $pid, [$node]) }; if (my $err = $@) { @@ -379,16 +524,26 @@ sub assert_target_cpu_compatibility { 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 bfccef5a..b6e035b8 100644 --- a/src/test/MigrationTest/QemuMigrateMock.pm +++ b/src/test/MigrationTest/QemuMigrateMock.pm @@ -244,6 +244,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 = @@ -255,6 +259,14 @@ $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 gds-no hv-relaxed its-no lahf-lm srso-no sse sse4.2 tsa-l1-no tsx-ctrl)]; + }, +); + 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 8a546ac0..00c5bbdf 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'", @@ -432,6 +438,12 @@ subtest 'migration preconditions of the CPU of a running VM' => sub { ['GenuineIntel', 'AuthenticAMD'], 'reasons include their values as details', ); + is_deeply( + $res->{not_allowed_nodes}->{'missing-flag'}->{'incompatible-cpu'}->[0] + ->{'missing-flags'}, + ['avx512f'], + 'reasons include all missing flags', + ); $running_cpu = 'kvm64,enforce,+kvm_pv_eoi,+kvm_pv_unhalt'; $res = PVE::API2::Qemu->migrate_vm_precondition({ node => 'localhost', vmid => 100 }); @@ -442,6 +454,24 @@ subtest 'migration preconditions of the CPU of a running VM' => sub { 'warnings include their values as details', ); + # overrides only take effect for HA-managed VMs if the HA stack gets them + $ha_config_module->mock( + vm_is_ha_managed => sub { return 1; }, + get_resource_motion_info => sub { return ([], {}); }, + ); + for my $version ('9.2.30', '9.2.31') { + $node_versions->{localhost} = $version; + my $forwarded = $version eq '9.2.31'; + is_deeply( + $cpu_reasons->('host,+kvm_pv_eoi,+kvm_pv_unhalt')->{'missing-flag'}, + [$forwarded ? 'host-cpu-flags (allow-missing-cpu-flags)' : 'host-cpu-flags'], + "HA-managed VM, pve-manager $version: override " + . ($forwarded ? '' : 'not ') + . 'offered', + ); + } + $node_versions->{localhost} = undef; + # the migration only warns about a failed check as well $cpu_config_module->mock( get_cpu_from_running_vm => @@ -459,6 +489,74 @@ subtest 'migration preconditions of the CPU of a running VM' => sub { mock_unreachable_config(); }; +subtest 'override for HA-managed VMs requires VM.Migrate on VMs in positive affinity' => sub { + my $inotify_module = Test::MockModule->new('PVE::INotify'); + $inotify_module->mock(nodename => sub { return 'localhost'; }); + $cluster_module->mock(get_config => sub { return $raw_without_identity; }); + $qemu_config_module->mock( + load_config => sub { return { memory => 512 }; }, + check_lock => sub { return; }, + ); + $replication_config_module->mock(find_local_replication_job => sub { return; }); + $qemu_server_module->mock( + check_running => sub { return 1234; }, + check_storage_availability => sub { return; }, + ); + my $rpcenv_module = Test::MockModule->new('PVE::RPCEnvironment'); + my $permitted = { 102 => 0 }; + $rpcenv_module->mock( + fork_worker => sub { die "reached fork_worker\n"; }, + check_vm_perm => sub { + my ($self, $user, $vmid) = @_; + die "unexpected permission check of VM $vmid\n" if !exists($permitted->{$vmid}); + return $permitted->{$vmid}; + }, + ); + my $ha_config_module = Test::MockModule->new('PVE::HA::Config'); + $ha_config_module->mock( + vm_is_ha_managed => sub { return 1; }, + get_resource_motion_info => sub { return (['ct:101', 'vm:102'], {}); }, + ); + # nodes with this version forward the option to the HA stack + $node_versions->{localhost} = '9.2.31'; + + my $migrate_error = sub { + my ($param) = @_; + eval { PVE::API2::Qemu->migrate_vm({ node => 'localhost', vmid => 100, $param->%* }) }; + return "$@"; + }; + like( + $migrate_error->({ target => 'pve2', online => 1, 'allow-missing-cpu-flags' => 1 }), + qr{^403 Permission check failed \(/vms/102, VM\.Migrate - }, + 'override refused without VM.Migrate on a VM in positive affinity', + ); + is( + $migrate_error->({ target => 'pve2', online => 1 }), + "reached fork_worker\n", + 'VMs in positive affinity are not checked without override', + ); + $permitted->{102} = 1; + is( + $migrate_error->({ target => 'pve2', online => 1, 'allow-missing-cpu-flags' => 1 }), + "reached fork_worker\n", + 'override allowed with VM.Migrate on the VMs in positive affinity', + ); + ($permitted->{102}, $node_versions->{localhost}) = (0, '9.2.30'); + is( + $migrate_error->({ target => 'pve2', online => 1, 'allow-missing-cpu-flags' => 1 }), + "reached fork_worker\n", + 'VMs in positive affinity are not checked if the option is not forwarded', + ); + + $node_versions->{localhost} = undef; + $qemu_server_module->mock(check_running => sub { return 0; }); + $qemu_server_module->unmock('check_storage_availability'); + $replication_config_module->unmock('find_local_replication_job'); + $qemu_config_module->unmock('check_lock'); + $cluster_module->unmock('get_config'); + mock_unreachable_config(); +}; + subtest 'clone target nodes' => sub { my $inotify_module = Test::MockModule->new('PVE::INotify'); $inotify_module->mock(nodename => sub { return 'pve1'; }); diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl index 41475b81..b76bef88 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. @@ -595,8 +602,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'. With 'check-error', the CPU check fails - # with it. 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'. With 'check-error', the CPU check fails with it. + # 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 @@ -1638,6 +1646,136 @@ 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( + # older kernels do not report all flags about unaffected vulnerabilities + name => '1033_running_host_cpu_missing_vulnerability_info_flags', + cpu => 'host', + runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt', + cpu_info => { + 'kvm-flags' => { pve0 => 'fpu gds-no its-no sse', pve1 => 'fpu gds-no sse' }, + }, + expect_log => [ + "CPU of node 'pve1' might be incompatible: the node does not report CPU flags that" + . " tell the guest which CPU vulnerabilities do not affect it, for example as it" + . " runs another kernel version, so the guest might not mitigate them: its-no", + ], + ), + cpu_migration_test( + # AMD reports unaffected vulnerabilities via CPUID + name => '1033_running_host_cpu_missing_amd_vulnerability_info_flags', + cpu => 'host', + runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt', + cpu_info => { + 'kvm-flags' => { pve0 => 'fpu srso-no sse tsa-l1-no', pve1 => 'fpu sse tsa-l1-no' }, + }, + expect_log => [ + "CPU of node 'pve1' might be incompatible: the node does not report CPU flags that" + . " tell the guest which CPU vulnerabilities do not affect it, for example as it" + . " runs another kernel version, so the guest might not mitigate them: srso-no", + ], + ), + cpu_migration_test( + # tsx-ctrl allows the guest to access an MSR + name => '1033_running_host_cpu_missing_tsx_ctrl', + cpu => 'host', + runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt', + cpu_info => { + 'kvm-flags' => { pve0 => 'fpu its-no sse tsx-ctrl', pve1 => 'fpu sse' }, + }, + 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: tsx-ctrl - use" + . " 'allow-missing-cpu-flags' to override", + ), + cpu_migration_test( + # for example, the entry got lost as pmxcfs was restarted + name => '1033_running_host_cpu_source_without_kvm_flags', + cpu => 'host', + runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt', + cpu_info => { + 'kvm-flags' => { pve0 => undef, pve1 => 'fpu' }, + }, + expect_log => [ + "CPU of node 'pve1' might be incompatible: the source node does not provide the" + . " CPU information needed to check it", + ], + ), + 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