public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Jonas Theisen <j.theisen@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server v4 5/7] cpu flags: report invtsc support of nodes
Date: Wed,  2 Dec 2026 18:45:22 +0100	[thread overview]
Message-ID: <20261202174530.565487-6-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261202174530.565487-1-j.theisen@proxmox.com>

QEMU only considers the invariant TSC flag migratable if the TSC
frequency is set explicitly. As the 'host' CPU model is migratable by
default, its expansion never includes invtsc, so no node is reported
to support it, for example in the 'Supported On' column of the CPU
flag selector.

Query the expansion once more with 'migratable' disabled and report
invtsc if the host supports it. Only invtsc is taken from it, as other
features QEMU considers unmigratable must not be reported for the
migratable 'host' model. If only this query fails, the other flags are
still reported.

The live migration check of the flags passed through by 'host' and
'max' must not require invtsc on the target if the VM lacks it, also
as nodes with an older version do not report it. These models only
pass it through if it is enabled explicitly, or with an explicit TSC
frequency, as QEMU then considers it migratable. In the latter case,
ask QEMU whether the vCPUs have it, as it is only passed through if
the host CPU has it.

If the VM has invtsc, a target that does not broadcast TSC information,
which nodes report together with invtsc, only causes a warning if it
lacks invtsc, as its support is unknown. This applies to 'host' and
'max' as well as to CPU models with 'enforce'.

Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
 src/PVE/QemuMigrate/Helpers.pm            |  69 +++++++++++++--
 src/PVE/QemuServer.pm                     |  19 ++++
 src/test/MigrationTest/QemuMigrateMock.pm |  18 +++-
 src/test/run_qemu_migrate_tests.pl        | 101 +++++++++++++++++++++-
 4 files changed, 196 insertions(+), 11 deletions(-)

diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index 16d201fd..ef95c107 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -270,12 +270,13 @@ my $vulnerability_info_flags = {
 };
 
 # 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.
+# separately those that only inform the guest about CPU vulnerabilities, and those the target node
+# might lack, as it does not report them.
 my sub get_missing_host_cpu_flags {
     my ($cpu, $source, $target) = @_;
 
     # already reported by the 'cpu-vendor' check
-    return ([], []) if vendors_differ($source, $target);
+    return ([], [], []) if vendors_differ($source, $target);
 
     my ($source_flags, $target_flags) = ($source->{'kvm-flags'}, $target->{'kvm-flags'});
 
@@ -287,16 +288,22 @@ my sub get_missing_host_cpu_flags {
     my @missing = sort grep {
         # Hyper-V enlightenments are not passed through, but set explicitly. Flags that QEMU
         # requires due to 'enforce' are reported by the 'vm-cpu-flags' check, as overriding it
-        # would only let QEMU on the target refuse to start.
+        # would only let QEMU on the target refuse to start. As 'host' is migratable by default,
+        # invtsc is only passed through if enabled explicitly or with an explicit TSC frequency.
         !m/^hv-/
+            && ($_ ne 'invtsc' || $cpu->{flags}->{invtsc})
             && !($cpu->{enforce} && $cpu->{flags}->{$_})
             && !$target_flags->{$_}
             && (PVE::JSONSchema::parse_boolean($cpu->{props}->{$_} // 'on') // 1)
     } @flags;
 
+    # nodes report invtsc together with their TSC information
+    my $unknown = [grep { $_ eq 'invtsc' && !defined($target->{'tsc-scaling'}) } @missing];
+    my %unknown = map { $_ => 1 } $unknown->@*;
+
     my $info = [grep { $vulnerability_info_flags->{$_} } @missing];
-    my $other = [grep { !$vulnerability_info_flags->{$_} } @missing];
-    return ($other, $info);
+    my $other = [grep { !$vulnerability_info_flags->{$_} && !$unknown{$_} } @missing];
+    return ($other, $info, $unknown);
 }
 
 # Returns the flags of the VM's virtual CPU that KVM on the target node does not support, and those
@@ -335,8 +342,10 @@ my sub get_missing_vm_cpu_flags {
             && (!$understood || $understood->{$flag})
         ) {
             # Other flags only with 'enforce'. Like for the CPU model's flags, this skips
-            # properties that are no CPU flags, such as 'host-phys-bits'.
-            push $missing->@*, $flag;
+            # properties that are no CPU flags, such as 'host-phys-bits'. Nodes report invtsc
+            # together with their TSC information.
+            my $unknown_invtsc = $flag eq 'invtsc' && !defined($target->{'tsc-scaling'});
+            push(($unknown_invtsc ? $unknown : $missing)->@*, $flag);
         }
     }
 
@@ -370,7 +379,7 @@ my sub get_tsc_frequency_flags_of_vm {
 # - 'automatic-evmcs' set if hv-evmcs is enabled automatically and the CPU is generated again on
 #   the target, which then enables it depending on its CPU vendor
 # - the enabled flags as 'flags' hash, including the flags the CPU model enables by default if
-#   'enforce' is set
+#   'enforce' is set, and invtsc if 'host' or 'max' pass it through due to an explicit TSC frequency
 #
 # 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
@@ -460,6 +469,28 @@ my $target_cpu_checks = [
             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, undef, $unknown) = get_missing_host_cpu_flags($cpu, $source, $target);
+            return if !scalar($unknown->@*);
+
+            my $message =
+                "CPU model '$cpu->{model}' passes through the flags of the host CPU, but the"
+                . " node might lack some, as it does not report them, for example as it runs an"
+                . " older version: "
+                . format_flag_list($unknown);
+            return ($message, { 'missing-flags' => $unknown });
+        },
+    },
     {
         type => 'host-cpu-model',
         severity => 'warning',
@@ -642,6 +673,16 @@ my sub get_target_cpu_issues {
     return $issues;
 }
 
+# Returns whether the vCPUs of the running VM have the CPU flag.
+my sub vcpus_have_flag {
+    my ($vmid, $flag) = @_;
+
+    my $vcpus = mon_cmd($vmid, 'query-cpus-fast');
+    my $path = $vcpus->[0]->{'qom-path'} or die "no vCPU found\n";
+
+    return mon_cmd($vmid, 'qom-get', path => $path, property => $flag) ? 1 : 0;
+}
+
 # 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) = @_;
@@ -723,6 +764,18 @@ sub check_target_cpu_compatibility {
         $cpu->{'automatic-evmcs'} = 1 if !grep { $_ eq '+hv-evmcs' } split(/;/, $vm_flags);
     }
 
+    # With an explicit TSC frequency, QEMU considers invtsc migratable, so 'host' and 'max' pass
+    # it through as well, if the host CPU has it.
+    my $props = $cpu->{props};
+    if (is_host_cpu_model($cpu) && $props->{'tsc-frequency'} && !defined($props->{invtsc})) {
+        my $invtsc = eval { vcpus_have_flag($vmid, 'invtsc') };
+        if (my $err = $@) {
+            warn "unable to query whether the vCPUs of VM $vmid have invtsc, assuming so - $err";
+            $invtsc = 1;
+        }
+        $cpu->{flags}->{invtsc} = 1 if $invtsc;
+    }
+
     my $checks = [grep { $_->{applies}->($cpu) } $target_cpu_checks->@*];
     return {} if !scalar($checks->@*);
 
diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index f8b3094f..0b67a548 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -3040,6 +3040,25 @@ sub query_supported_cpu_flags {
 
             $flags->{$_} = 1
                 for PVE::QemuServer::CPUFlags::get_cpu_expansion_flags($cmd_result);
+
+            # 'host' omits invtsc while it is migratable, as QEMU only considers invtsc
+            # migratable with an explicit TSC frequency. It can still be enabled explicitly.
+            if ($kvm && $arch eq 'x86_64') {
+                $model->{props}->{migratable} = JSON::false;
+                my $unmigratable = eval {
+                    mon_cmd(
+                        $fakevmid,
+                        'query-cpu-model-expansion',
+                        type => 'full',
+                        model => $model,
+                    );
+                };
+                # do not lose the other flags if only this fails
+                warn "unable to query whether KVM supports invtsc - $@" if $@;
+                $flags->{invtsc} = 1
+                    if $unmigratable && grep { $_ eq 'invtsc' }
+                    PVE::QemuServer::CPUFlags::get_cpu_expansion_flags($unmigratable);
+            }
         };
         my $err = $@;
 
diff --git a/src/test/MigrationTest/QemuMigrateMock.pm b/src/test/MigrationTest/QemuMigrateMock.pm
index 6554c37e..cd281c3a 100644
--- a/src/test/MigrationTest/QemuMigrateMock.pm
+++ b/src/test/MigrationTest/QemuMigrateMock.pm
@@ -32,6 +32,14 @@ my $test_vmid = $migrate_params->{vmid};
 my $test_target = $migrate_params->{target};
 my $test_opts = $migrate_params->{opts};
 my $test_cpu_info = $migrate_params->{cpu_info} // {};
+
+# Returns whether the vCPUs have the flag, by default if the running CPU enables it.
+my sub vcpus_have_flag {
+    my ($flag) = @_;
+    return $test_cpu_info->{'vcpu-flags'}->{$flag}
+        // ($vm_status->{runningcpu} =~ m/(?:^|,)\+\Q$flag\E(?:,|$)/);
+}
+
 my $current_log = '';
 
 my $vm_stop_executed = 0;
@@ -246,6 +254,12 @@ $MigrationTest::Shared::qemu_migrate_helpers_module->mock(
             my $flags = $test_cpu_info->{'model-flags'}->{ $params{model}->{name} } // [];
             return { model => { props => { map { $_ => JSON::true } $flags->@* } } };
         }
+        if ($command eq 'query-cpus-fast') {
+            return [{ 'cpu-index' => 0, 'qom-path' => '/machine/unattached/device[0]' }];
+        }
+        if ($command eq 'qom-get') {
+            return vcpus_have_flag($params{property}) ? JSON::true : JSON::false;
+        }
         die "mon_cmd (mocked) - implement me: $command";
     },
 );
@@ -272,8 +286,8 @@ my $qemu_server_cpuflags_module = Test::MockModule->new("PVE::QemuServer::CPUFla
 $qemu_server_cpuflags_module->mock(
     query_understood_cpu_flags => sub {
         return [qw(
-            abm aes avx avx2 avx512f avx512vl fpu gds-no hv-relaxed its-no kvm-pv-eoi lahf-lm pcid
-            srso-no sse sse4.2 svm tsa-l1-no tsx-ctrl vmx
+            abm aes avx avx2 avx512f avx512vl fpu gds-no hv-relaxed invtsc its-no kvm-pv-eoi lahf-lm
+            pcid srso-no sse sse4.2 svm tsa-l1-no tsx-ctrl vmx
         )];
     },
 );
diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl
index 894d9ae2..80f33fef 100755
--- a/src/test/run_qemu_migrate_tests.pl
+++ b/src/test/run_qemu_migrate_tests.pl
@@ -618,7 +618,9 @@ my $tests = [
     # cpu_info       - hash with the CPU information of the 'source' node and of each
     #                  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'. With 'check-error', the CPU check fails with it.
+    #                  'model-flags'. 'vcpu-flags' overrides whether the vCPUs have a
+    #                  flag, by default they have the flags the running CPU enables.
+    #                  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
@@ -2109,6 +2111,103 @@ my $tests = [
                 . " information needed to check it",
         ],
     ),
+    cpu_migration_test(
+        # 'host' only has invtsc if enabled explicitly, older nodes do not broadcast it
+        name => '1033_running_host_cpu_invtsc_not_enabled',
+        cpu => 'host',
+        runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt',
+        cpu_info => {
+            'kvm-flags' => { pve0 => 'fpu invtsc sse' },
+        },
+    ),
+    cpu_migration_test(
+        name => '1033_running_host_cpu_invtsc_enabled',
+        cpu => 'host',
+        runningcpu => 'host,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt',
+        cpu_info => {
+            'kvm-flags' => { pve0 => 'fpu invtsc 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: invtsc - use"
+            . " 'allow-missing-cpu-flags' to override",
+    ),
+    cpu_migration_test(
+        # with an explicit TSC frequency, 'host' passes invtsc through if the host has it
+        name => '1033_running_host_cpu_invtsc_tsc_frequency',
+        cpu => 'host,flags=+hv-reenlightenment',
+        runningcpu =>
+            'host,+hv-reenlightenment,+kvm_pv_eoi,+kvm_pv_unhalt,tsc-frequency=2999999000',
+        cpu_info => {
+            'kvm-flags' => { pve0 => 'fpu hv-reenlightenment invtsc sse' },
+            'vcpu-flags' => { invtsc => 1 },
+        },
+        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: invtsc - use"
+            . " 'allow-missing-cpu-flags' to override",
+    ),
+    cpu_migration_test(
+        # nodes report invtsc support together with their TSC information
+        name => '1033_running_host_cpu_invtsc_target_without_tsc_info',
+        cpu => 'host,flags=+hv-reenlightenment',
+        runningcpu =>
+            'host,+hv-reenlightenment,+kvm_pv_eoi,+kvm_pv_unhalt,tsc-frequency=2999999000',
+        cpu_kept => 1,
+        cpu_info => {
+            nodes => { pve1 => { 'tsc-khz' => undef, 'tsc-scaling' => undef } },
+            'kvm-flags' => {
+                pve0 => 'fpu hv-reenlightenment invtsc sse',
+                pve1 => 'fpu hv-reenlightenment sse',
+            },
+            'vcpu-flags' => { invtsc => 1 },
+        },
+        expect_log => [
+            "CPU of node 'pve1' might be incompatible: the node does not provide the CPU"
+                . " information needed to check it, for example as it runs an older version or"
+                . " pvestatd did not broadcast it yet",
+            "CPU of node 'pve1' might be incompatible: CPU model 'host' passes through the flags"
+                . " of the host CPU, but the node might lack some, as it does not report them, for"
+                . " example as it runs an older version: invtsc",
+        ],
+    ),
+    cpu_migration_test(
+        name => '1033_running_host_cpu_invtsc_tsc_frequency_dropped',
+        cpu => 'host,flags=+hv-reenlightenment',
+        runningcpu =>
+            'host,+hv-reenlightenment,+kvm_pv_eoi,+kvm_pv_unhalt,tsc-frequency=2999999000',
+        cpu_kept => 1,
+        cpu_info => {
+            'kvm-flags' => { pve0 => 'fpu hv-reenlightenment invtsc sse' },
+            'vcpu-flags' => { invtsc => 0 },
+        },
+    ),
+    cpu_migration_test(
+        # QEMU on the target requires flags set with 'enforce', regardless of the override
+        name => '1033_running_kept_cpu_invtsc_missing',
+        cpu => 'custom-mycpu',
+        runningcpu => 'kvm64,enforce,+invtsc',
+        opts => { 'allow-missing-cpu-flags' => 1 },
+        cpu_info => {
+            'kvm-flags' => { pve0 => 'fpu invtsc sse' },
+        },
+        expect_die => "can't migrate VM to 'pve1' - incompatible CPU: KVM on the node does not"
+            . " support flags of the VM's virtual CPU: invtsc",
+    ),
+    cpu_migration_test(
+        # nodes report invtsc support together with their TSC information
+        name => '1033_running_kept_cpu_invtsc_target_without_tsc_info',
+        cpu => 'custom-mycpu',
+        runningcpu => 'kvm64,enforce,+invtsc',
+        cpu_kept => 1,
+        cpu_info => {
+            nodes => { pve1 => { 'tsc-khz' => undef, 'tsc-scaling' => undef } },
+            'kvm-flags' => { pve0 => 'fpu invtsc sse' },
+        },
+        expect_log => [
+            "CPU of node 'pve1' might be incompatible: KVM on the node might not support flags"
+                . " of the VM's virtual CPU, as the node does not report them, for example as they"
+                . " were detected by an older version: invtsc",
+        ],
+    ),
     {
         name => '105_replicated_to_non_replication_target',
         target => 'pve1',
-- 
2.47.3





  parent reply	other threads:[~2026-12-02 17:46 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 17:45 [PATCH docs/qemu-server v4 0/7] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
2026-12-02 17:45 ` [PATCH qemu-server v4 1/7] helpers: add helper to query the host's TSC frequency and scaling support Jonas Theisen
2026-12-02 17:45 ` [PATCH qemu-server v4 2/7] migrate: allow CPU checks that only need information of the target Jonas Theisen
2026-12-02 17:45 ` [PATCH qemu-server v4 3/7] cpu config: add hv-reenlightenment flag and set TSC frequency for it Jonas Theisen
2026-12-02 17:45 ` [PATCH qemu-server v4 4/7] migrate: block migration to nodes that cannot provide the TSC frequency Jonas Theisen
2026-12-02 17:45 ` Jonas Theisen [this message]
2026-12-02 17:45 ` [PATCH qemu-server v4 6/7] cpu config: set TSC frequency for invtsc to allow live migration Jonas Theisen
2026-12-02 17:45 ` [PATCH docs v4 7/7] qm: document hv-reenlightenment and invtsc Jonas Theisen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202174530.565487-6-j.theisen@proxmox.com \
    --to=j.theisen@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

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

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