From: Jonas Theisen <j.theisen@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server v3 5/6] cpu config: set TSC frequency for invtsc to allow live migration
Date: Mon, 30 Nov 2026 19:34:12 +0100 [thread overview]
Message-ID: <20261130183418.617530-6-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261130183418.617530-1-j.theisen@proxmox.com>
The invariant TSC flag can be enabled with custom CPU models. Unless
the TSC frequency is set explicitly, QEMU blocks live migration,
snapshots with RAM and hibernation of such VMs, as KVM does not emulate
the TSC frequency of the source host afterwards.
Set it to the host's TSC frequency, as for 'hv-reenlightenment', and
extend the check for a compatible TSC on the migration target to the
flag. As VMs with custom CPU models keep the CPU of the running
instance, the frequency stays the same across live migrations.
VMs that were started with invtsc but without an explicit TSC frequency
still cannot be live migrated until they are restarted. Refuse this
upfront, as QEMU's migration blocker only aborts the migration after
the target instance was started and local disks were copied. Ask QEMU
whether the vCPUs have the flag, as the 'host' and 'max' CPU models
silently drop it if the host lacks it.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/QemuMigrate/Helpers.pm | 53 +++++++++++++++++++--
src/PVE/QemuServer/CPUConfig.pm | 31 +++++++++---
src/test/MigrationTest/QemuMigrateMock.pm | 10 ++++
src/test/QemuServerTest/CommandLineMocks.pm | 4 ++
src/test/cfg2cmd/cpu-invtsc.conf | 6 +++
src/test/cfg2cmd/cpu-invtsc.conf.cmd | 27 +++++++++++
src/test/run_qemu_migrate_tests.pl | 52 +++++++++++++++++++-
7 files changed, 171 insertions(+), 12 deletions(-)
create mode 100644 src/test/cfg2cmd/cpu-invtsc.conf
create mode 100644 src/test/cfg2cmd/cpu-invtsc.conf.cmd
diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index f14f0794..125c648b 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -177,6 +177,12 @@ my sub vendors_differ {
return defined($source_vendor) && defined($target_vendor) && $source_vendor ne $target_vendor;
}
+# Returns the enabled flags that make the VM run with the TSC frequency of the host it started on.
+my sub get_enabled_tsc_frequency_flags {
+ my ($cpu) = @_;
+ return grep { $cpu->{flags}->{$_} } PVE::QemuServer::CPUConfig::get_tsc_frequency_flags();
+}
+
my sub format_flag_list {
my ($flags) = @_;
@@ -205,6 +211,8 @@ my sub format_flag_list {
# - 'enforce' set if QEMU refuses to start without all enabled flags
# - the enabled flags as 'flags' hash, including the flags the CPU model enables by default if
# 'enforce' is set
+# - 'invtsc-blocker' set if QEMU blocks the migration, as the vCPUs have an invariant TSC but no
+# 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
@@ -345,13 +353,29 @@ my $target_cpu_checks = [
. format_flag_list(\@missing);
},
},
+ {
+ type => 'tsc-frequency',
+ severity => 'error',
+ needs => [],
+ applies => sub {
+ my ($cpu) = @_;
+ return $cpu->{'invtsc-blocker'};
+ },
+ check => sub {
+ # QEMU would only refuse late, for example after local disks were copied
+ return
+ "'invtsc' requires an explicit TSC frequency for live migration, but the VM was"
+ . " started without one - restart the VM to set it";
+ },
+ },
{
type => 'tsc-frequency',
severity => 'error',
needs => ['tsc-scaling'],
applies => sub {
my ($cpu) = @_;
- return $cpu->{flags}->{'hv-reenlightenment'} && $cpu->{props}->{'tsc-frequency'};
+ return
+ scalar(get_enabled_tsc_frequency_flags($cpu)) && $cpu->{props}->{'tsc-frequency'};
},
check => sub {
my ($cpu, $source, $target) = @_;
@@ -359,8 +383,11 @@ my $target_cpu_checks = [
return if $target->{'tsc-scaling'};
my $vm_khz = int($cpu->{props}->{'tsc-frequency'} / 1000);
- my $requirement = "'hv-reenlightenment' requires TSC scaling support or a TSC"
- . " frequency within 250 ppm of the VM's $vm_khz kHz";
+ my @flags = get_enabled_tsc_frequency_flags($cpu);
+ my $requirement =
+ join(' and ', map { "'$_'" } @flags)
+ . (scalar(@flags) > 1 ? " require" : " requires")
+ . " TSC scaling support or a TSC frequency within 250 ppm of the VM's $vm_khz kHz";
my $target_khz = $target->{'tsc-khz'}
or return "$requirement, but the node's TSC frequency is unknown";
@@ -406,6 +433,20 @@ my sub get_target_cpu_issues {
return $issues;
}
+# Returns whether QEMU blocks the migration of the running VM, as its vCPUs have an invariant TSC
+# but no explicit TSC frequency. Only asks QEMU if the CPU enables invtsc, as 'host' and 'max'
+# silently drop it if the host lacks it.
+my sub has_invtsc_migration_blocker {
+ my ($vmid, $cpu) = @_;
+
+ return 0 if !$cpu->{kvm} || !$cpu->{flags}->{invtsc} || $cpu->{props}->{'tsc-frequency'};
+
+ 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 => 'invtsc') ? 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) = @_;
@@ -477,6 +518,12 @@ sub check_target_cpu_compatibility {
$enabled ? ($cpu->{flags}->{$prop} = 1) : delete($cpu->{flags}->{$prop});
}
+ $cpu->{'invtsc-blocker'} = eval { has_invtsc_migration_blocker($vmid, $cpu) };
+ if (my $err = $@) {
+ warn "unable to query whether the vCPUs of VM $vmid use invtsc - $err";
+ $cpu->{'invtsc-blocker'} = 1; # the command line enables it
+ }
+
my $checks = [grep { $_->{applies}->($cpu) } $target_cpu_checks->@*];
return {} if !scalar($checks->@*);
diff --git a/src/PVE/QemuServer/CPUConfig.pm b/src/PVE/QemuServer/CPUConfig.pm
index d629a2c6..082f8328 100644
--- a/src/PVE/QemuServer/CPUConfig.pm
+++ b/src/PVE/QemuServer/CPUConfig.pm
@@ -864,6 +864,19 @@ my sub check_phys_bits_above_40_compat($bios, $cpu_type, $cpu_flags) {
}
}
+=head3 get_tsc_frequency_flags
+
+ my @flags = get_tsc_frequency_flags();
+
+Returns the CPU flags for which the TSC frequency of the VM is set to the one of the host it is
+started on, as QEMU cannot migrate VMs with these flags otherwise.
+
+=cut
+
+sub get_tsc_frequency_flags() {
+ return qw(hv-reenlightenment invtsc);
+}
+
# Calculate QEMU's '-cpu' argument from a given VM configuration
sub get_cpu_options(
$conf, $arch, $kvm, $kvm_off, $version_guard, $winversion, $gpu_passthrough,
@@ -969,17 +982,21 @@ sub get_cpu_options(
$pve_forced_flags,
);
- # Without an explicit TSC frequency, QEMU refuses to migrate a guest that enabled
- # re-enlightenment notifications, as KVM does not emulate the old frequency afterwards.
- my $reenlightenment = $resolved_flags->{'hv-reenlightenment'};
- if ($kvm && $arch eq 'x86_64' && $reenlightenment && $reenlightenment->{op} ne '-') {
+ # Without an explicit TSC frequency, QEMU refuses to migrate a guest with an invariant TSC or
+ # one that enabled re-enlightenment notifications, as KVM does not emulate the old frequency
+ # afterwards.
+ my @tsc_frequency_flags = grep {
+ my $flag = $resolved_flags->{$_};
+ $flag && $flag->{op} ne '-';
+ } get_tsc_frequency_flags();
+ if ($kvm && $arch eq 'x86_64' && scalar(@tsc_frequency_flags)) {
my $tsc_khz = PVE::QemuServer::Helpers::get_host_tsc_info()->{khz}
- or die "unable to determine the host TSC frequency required for"
- . " 'hv-reenlightenment'\n";
+ or die "unable to determine the host TSC frequency required for "
+ . join(' and ', map { "'$_'" } @tsc_frequency_flags) . "\n";
$resolved_flags->{'tsc-frequency'} = {
op => '',
value => $tsc_khz * 1000,
- reason => "required by hv-reenlightenment",
+ reason => "required by " . join(' & ', @tsc_frequency_flags),
};
}
diff --git a/src/test/MigrationTest/QemuMigrateMock.pm b/src/test/MigrationTest/QemuMigrateMock.pm
index 21a85d40..b136cec4 100644
--- a/src/test/MigrationTest/QemuMigrateMock.pm
+++ b/src/test/MigrationTest/QemuMigrateMock.pm
@@ -245,6 +245,16 @@ $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') {
+ # without 'vcpu-flags', the vCPUs have the flags the running CPU enables
+ my $flag = $params{property};
+ my $enabled = $test_cpu_info->{'vcpu-flags'}->{$flag}
+ // ($vm_status->{runningcpu} =~ m/(?:^|,)\+\Q$flag\E(?:,|$)/);
+ return $enabled ? JSON::true : JSON::false;
+ }
die "mon_cmd (mocked) - implement me: $command";
},
);
diff --git a/src/test/QemuServerTest/CommandLineMocks.pm b/src/test/QemuServerTest/CommandLineMocks.pm
index d0cd31c2..187215cf 100644
--- a/src/test/QemuServerTest/CommandLineMocks.pm
+++ b/src/test/QemuServerTest/CommandLineMocks.pm
@@ -483,6 +483,10 @@ cpu-model: qemu64
cpu-model: alldefault
+cpu-model: invtsc
+ reported-model kvm64
+ flags +invtsc
+
EOF
);
},
diff --git a/src/test/cfg2cmd/cpu-invtsc.conf b/src/test/cfg2cmd/cpu-invtsc.conf
new file mode 100644
index 00000000..d5c60fb4
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-invtsc.conf
@@ -0,0 +1,6 @@
+# TEST: Test that enabling invtsc sets the TSC frequency of the host
+cpu: custom-invtsc
+memory: 768
+numa: 0
+ostype: l26
+sockets: 1
diff --git a/src/test/cfg2cmd/cpu-invtsc.conf.cmd b/src/test/cfg2cmd/cpu-invtsc.conf.cmd
new file mode 100644
index 00000000..39269134
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-invtsc.conf.cmd
@@ -0,0 +1,27 @@
+/usr/bin/kvm \
+ -id 8006 \
+ -name vm8006 \
+ -no-shutdown \
+ -chardev 'socket,id=qmp,path=/var/run/qemu-server/8006.qmp,server=on,wait=off' \
+ -object 'monitor-qmp,chardev=qmp,id=monitor-qmp' \
+ -chardev 'socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect-ms=5000' \
+ -object 'monitor-qmp,chardev=qmp-event,id=monitor-qmp-event' \
+ -pidfile /var/run/qemu-server/8006.pid \
+ -daemonize \
+ -smp '1,sockets=1,cores=1,maxcpus=1' \
+ -nodefaults \
+ -boot 'menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg' \
+ -vnc 'unix:/var/run/qemu-server/8006.vnc,password=on' \
+ -cpu 'kvm64,enforce,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt,+lahf_lm,+sep,tsc-frequency=2999999000' \
+ -m 768 \
+ -global 'PIIX4_PM.disable_s3=1' \
+ -global 'PIIX4_PM.disable_s4=1' \
+ -device 'pci-bridge,id=pci.1,chassis_nr=1,bus=pci.0,addr=0x1e' \
+ -device 'pci-bridge,id=pci.2,chassis_nr=2,bus=pci.0,addr=0x1f' \
+ -device 'pci-bridge,id=pci.4,chassis_nr=4,bus=pci.1,addr=0x1c' \
+ -device 'piix3-usb-uhci,id=uhci,bus=pci.0,addr=0x1.0x2' \
+ -device 'usb-tablet,id=tablet,bus=uhci.0,port=1' \
+ -device 'VGA,id=vga,bus=pci.0,addr=0x2' \
+ -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3,free-page-reporting=on' \
+ -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \
+ -machine 'hpet=off,type=pc+pve0'
diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl
index c8f7976e..3404e2d8 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', see cpu_migration_test() for tests of the CPU checks
+ # 'model-flags'. 'vcpu-flags' overrides whether the vCPUs have a
+ # flag, by default they have the flags the running CPU enables.
+ # 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
@@ -1843,7 +1845,7 @@ my $tests = [
cpu_migration_test(
name => '1033_running_host_cpu_invtsc_enabled',
cpu => 'host',
- runningcpu => 'host,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt',
+ runningcpu => 'host,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt,tsc-frequency=2999999000',
cpu_info => {
'kvm-flags' => { pve0 => 'fpu invtsc sse' },
},
@@ -1918,6 +1920,52 @@ my $tests = [
. " information needed to check it",
],
),
+ cpu_migration_test(
+ name => '1033_running_invtsc_tsc_scaling',
+ cpu => 'custom-invtsc',
+ runningcpu => 'kvm64,enforce,+invtsc,tsc-frequency=2999999000',
+ cpu_kept => 1,
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => 2400000, 'tsc-scaling' => 1 } },
+ },
+ ),
+ cpu_migration_test(
+ name => '1033_running_invtsc_tsc_mismatch',
+ cpu => 'custom-invtsc',
+ runningcpu => 'kvm64,enforce,+invtsc,tsc-frequency=2999999000',
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => 2400000, 'tsc-scaling' => 0 } },
+ },
+ expect_die => "can't migrate VM to 'pve1' - incompatible CPU: 'invtsc' requires TSC"
+ . " scaling support or a TSC frequency within 250 ppm of the VM's 2999999 kHz, but"
+ . " the node has 2400000 kHz without TSC scaling",
+ ),
+ cpu_migration_test(
+ # for example, started before the TSC frequency was set for invtsc
+ name => '1033_running_invtsc_without_tsc_frequency',
+ cpu => 'custom-invtsc',
+ runningcpu => 'kvm64,enforce,+invtsc',
+ expect_die => "can't migrate VM to 'pve1' - incompatible CPU: 'invtsc' requires an"
+ . " explicit TSC frequency for live migration, but the VM was started without one"
+ . " - restart the VM to set it",
+ ),
+ cpu_migration_test(
+ # only KVM blocks the migration of an invariant TSC
+ name => '1033_running_invtsc_tcg_without_tsc_frequency',
+ cpu => 'custom-invtsc',
+ config => { kvm => 0 },
+ runningcpu => 'kvm64,+invtsc',
+ cpu_kept => 1,
+ ),
+ cpu_migration_test(
+ # 'host' drops invtsc if the host lacks it, so QEMU does not block the migration
+ name => '1033_running_host_cpu_invtsc_dropped',
+ cpu => 'host',
+ runningcpu => 'host,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt',
+ cpu_info => {
+ 'vcpu-flags' => { invtsc => 0 },
+ },
+ ),
{
name => '105_replicated_to_non_replication_target',
target => 'pve1',
--
2.47.3
next prev parent reply other threads:[~2026-11-30 18:35 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 18:34 [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 1/6] cpu flags: report invtsc support of nodes Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 2/6] helpers: add helper to query the host's TSC frequency and scaling support Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 3/6] cpu config: add hv-reenlightenment flag and set TSC frequency for it Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 4/6] migrate: block migration to nodes with incompatible TSC for re-enlightenment Jonas Theisen
2026-11-30 18:34 ` Jonas Theisen [this message]
2026-11-30 18:34 ` [PATCH docs v3 6/6] qm: document hv-reenlightenment and invtsc Jonas Theisen
2026-11-30 18:36 ` [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment 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=20261130183418.617530-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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.