* [PATCH docs/qemu-server v4 0/7] live migrate VMs with an invariant TSC or re-enlightenment
@ 2026-12-02 17:45 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
` (6 more replies)
0 siblings, 7 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-12-02 17:45 UTC (permalink / raw)
To: pve-devel
This series sets an explicit TSC frequency for VMs whose guest relies on
a TSC frequency that does not change, so that they can be live
migrated, and checks upfront whether the target node can provide it.
It builds on the series "check the target node's CPU before live
migrating a VM" [0], from which it was split out in v3, and needs to
be applied on top of it.
The invariant TSC flag 'invtsc' can be enabled with custom CPU models.
Without an explicit TSC frequency, QEMU blocks live migration,
snapshots with RAM and hibernation of such VMs. Guests need the flag to
rely on the TSC as clock source, for example for applications reading
it directly or for nested hypervisors. In benchmarks of Windows guests
with virtualization-based security (VBS) and the 'host' CPU type, it
considerably reduced the performance cost of VBS, for example for
context switches and thread creation. Nodes now also report whether
they support invtsc. Previously, they never reported it, as QEMU omits
it from the migratable 'host' CPU model, so the "Supported On" column of
the flag selector of the custom CPU model editor was always empty for
it.
The 'hv-reenlightenment' flag is added as well. Together with
'hv-frequencies', which custom CPU models can enable, it lets nested
Hyper-V guests, for example Windows with WSL2 or VBS, pass a stable
clock source on to their own guests. Without 'hv-frequencies', guests
do not use it. As KVM does not fully implement re-enlightenment
notifications, the TSC frequency of such VMs must not change across
live migrations. This series does not enable 'hv-frequencies', it only
adds 'hv-reenlightenment' in preparation for it.
For both flags, the TSC frequency is set explicitly to the host's,
except for confidential VMs, which cannot be migrated anyway. It is
queried via KVM_GET_TSC_KHZ on a temporary VM, as upstream Linux
does not export it otherwise. Failed queries are retried after ten
minutes, as pvestatd queries this in each cycle. The 'host-cpu-info'
entry each node broadcasts additionally includes the TSC frequency and
whether KVM can scale the TSC, so the migration checks block:
- any explicit TSC frequency of the running VM, as set for both flags,
if the target can neither scale the TSC nor match the frequency
within 250 ppm of its own
- 'invtsc' on a VM that was started without an explicit TSC frequency,
for example before the update: QEMU blocks its migration anyway, but
only after the target instance was started and local disks were
copied. It is refused upfront for every target, until the VM is
stopped and started again. Whether QEMU blocks the migration is
taken from its 'blocked-reasons', which also covers
'host,migratable=off'.
- 'host'/'max' with an explicit TSC frequency, for example due to
'hv-reenlightenment', and target lacks invtsc: QEMU then passes
invtsc through if the source host has it, so the check of the flags
missing for 'host' includes it if the vCPUs have it. If the target
does not broadcast TSC information, which nodes report together with
invtsc, this only causes a warning, as its support is unknown.
A VM with a custom CPU model that already enabled 'hv-reenlightenment'
before the update runs without an explicit TSC frequency. If its guest
enabled the notifications, QEMU on the target refuses the migrated
state at the end of the migration. As this is not known upfront, only
a warning suggests to stop and start the VM.
The precondition API reports these with the new 'tsc-frequency' type,
the refusals in 'incompatible-cpu', and the warning in 'cpu-warnings'.
pve-manager needs no changes, as pvestatd broadcasts whatever
qemu-server provides.
[0] https://lore.proxmox.com/pve-devel/20261202151831.414110-1-j.theisen@proxmox.com/T/#t
Testing:
Upgrading the packages reloads pvestatd, so that it broadcasts the TSC
information and the 'cpuflags-kvm' entry including invtsc. Only when
deploying the code without the packages, restart pvestatd manually.
Start the test VMs only afterwards, as only VMs started with this
series get an explicit TSC frequency, without which the TSC check does
not apply.
The TSC check can be tested on a cluster with identical CPUs by making
the target node broadcast a different TSC frequency. pvestatd
broadcasts 'host-cpu-info' again whenever it differs, so stop it first.
The following only changes the TSC information, to a 'tsc-khz' clearly
different from the real one, as the check tolerates 250 ppm:
systemctl stop pvestatd
perl -MPVE::Cluster -MPVE::INotify -MJSON -e '
PVE::Cluster::cfs_update();
my $node = PVE::INotify::nodename();
my $info = decode_json(PVE::Cluster::get_node_kv("host-cpu-info", $node)->{$node});
$info->{"tsc-khz"} = 1000000;
$info->{"tsc-scaling"} = 0;
PVE::Cluster::broadcast_node_kv("host-cpu-info", encode_json($info));
'
Restarting pvestatd broadcasts the real values again:
systemctl restart pvestatd
What to expect for a running VM:
- 'tsc-scaling' 0 and a 'tsc-khz' more than 250 ppm off: blocked for
VMs with 'flags=+hv-reenlightenment' or a custom CPU model with
'+invtsc'
- 'invtsc' removed from 'cpuflags-kvm' on the target for a VM with
'host,flags=+hv-reenlightenment' on a source with invtsc: blocked,
with override
- 'invtsc' removed from 'cpuflags-kvm' on the target for a VM with
a custom CPU model with '+invtsc' and a reported model other than
'host', for example the default 'kvm64': blocked, without override.
With reported model 'host', it is blocked like other flags missing
for 'host', so it can be overridden. VMs with CPU type 'host' do not
have invtsc unless enabled explicitly or due to an explicit TSC
frequency, so they are not blocked otherwise.
- a VM with a custom CPU model with '+invtsc' that was started without
an explicit TSC frequency, for example with
'args: -cpu kvm64,enforce,+invtsc' on a node with an invariant TSC:
blocked for every target, until it is stopped and started without
'args'. The same applies to 'args: -cpu host,migratable=off'.
- a VM with a custom CPU model with '+hv-reenlightenment' started with
'args: -cpu kvm64,+hv-reenlightenment': a warning for every target
The invtsc support of a node requires a host CPU with an invariant TSC,
which nested nodes only have if their own CPU has 'invtsc' enabled.
After the upgrade, the "Supported On" column of the flag selector of
the custom CPU model editor lists such nodes for invtsc.
Changes since v3:
* TSC helper: keep the result of each query if the other fails, and do
not treat a failed capability check as a missing capability. Only
cache complete results, and retry failed queries after ten minutes,
as pvestatd now queries this in each cycle and each query creates a
KVM VM and logs a warning on failure. Without /dev/kvm, return
nothing and log nothing. Only query it on x86_64 hosts. Add tests.
* restructure the commits: a preparatory commit lets checks only need
information of the target, the generic handling of the TSC frequency
flags is introduced with 'hv-reenlightenment', and the invtsc
support of nodes is reported after their TSC information, so that the
invtsc commit only adds the flag and its migration blocker
* do not set an explicit TSC frequency for confidential VMs, as they
cannot be live migrated, snapshotted with RAM or hibernated anyway,
and QEMU refuses to start TDX VMs with a TSC frequency that is not a
multiple of 25 MHz. A TDX VM with a custom CPU model enabling invtsc
would otherwise fail to start after the upgrade.
* check the target's TSC for any explicit TSC frequency of a running
KVM VM, not only if 'hv-reenlightenment' or 'invtsc' is enabled, as
QEMU sets it regardless
* include invtsc in the check of the flags missing for 'host'/'max' if
the vCPUs have it due to an explicit TSC frequency, as QEMU then
passes it through
* warn about VMs with 'hv-reenlightenment' that were started without an
explicit TSC frequency, as their migration fails at the end if the
guest enabled re-enlightenment notifications
* detect the invtsc migration blocker via 'blocked-reasons' of
'query-migrate' instead of the CPU arguments, which miss
'host,migratable=off'. A failed query refuses the migration with a
suggestion to try again, instead of one to restart the VM, but only
if the vCPUs might have invtsc, as the CPU names it or as 'host' or
'max' run with 'migratable=off'. For other VMs, including 'host' and
'max' with the default 'migratable=on', it only warns. Only query it
for x86 VMs. Confidential VMs are not checked at all, as with v4 of
the series this builds on.
* fix the 'cpu-warnings' schema of the precondition API, which lacked
the 'tsc-frequency' type of the 'hv-reenlightenment' warning
* only warn if a VM with invtsc is migrated to a node that lacks
invtsc, but does not broadcast TSC information either, as it runs an
older version, which does not report invtsc support. This applies to
CPU models with 'enforce' as well as to 'host' and 'max'.
* the message about an incompatible TSC frequency no longer claims that
the tolerance is relative to the VM's frequency, as it is relative to
the target's, and only names the flags the frequency was set for
* if only the query for invtsc support fails, still broadcast the
other CPU flags
* report the TSC frequencies of the VM and the target as typed details
in the precondition API, like v4 of the series it builds on does for
the other checks. The TSC check does not need the TSC information of
the source node.
* docs: document the above, that affected VMs need to be stopped and
started again, that confidential VMs get no explicit TSC frequency,
and that targets without TSC information only cause a warning
* reword the flag description, docs and comments of
'hv-reenlightenment': the pinned frequency avoids frequency changes
instead of notifying the guest about them, and omitting
'hv-frequencies' does not lift the migration restriction
* docs: spell out the time stamp counter (TSC), and mention that a
'-cpu' option in 'args' must set the TSC frequency itself
* tests: check that the override does not apply to a missing invtsc
that QEMU enforces
* rebased onto v4 of the series "check the target node's CPU before
live migrating a VM"
Changes since v2 (as part of the series "check the target node's CPU
before live migrating a VM"):
* split out into this series
* new: refuse the live migration of VMs with invtsc that were started
without an explicit TSC frequency, for example before the update,
upfront. QEMU blocks their migration anyway, but only after the
target instance was started and local disks were copied. Whether the
vCPUs have invtsc is queried from QEMU, as 'host' and 'max' drop it
if the host lacks it.
* describe in the flag description, the commit message and the docs that
guests only use 'hv-reenlightenment' together with 'hv-frequencies'
* tests: use the helper for the migration tests of the CPU checks, and
add tests for the edge of the TSC tolerance and invtsc with TCG
* new: cfg2cmd test for a disabled 'hv-reenlightenment' flag
* docs: mention that VMs started without an explicit TSC frequency need
a restart before they can be live migrated
* fix the trailing newline of the invtsc cfg2cmd test
* rebased onto current master
Changes since v1 (as part of the series "check the target node's CPU
before live migrating a VM"):
* new: report the invtsc support of nodes
* new: set the TSC frequency for invtsc, as for 'hv-reenlightenment',
so that such VMs can be live migrated, and extend the TSC check of
the target node to it
* new: docs for 'hv-reenlightenment' and invtsc
qemu-server:
Jonas Theisen (6):
helpers: add helper to query the host's TSC frequency and scaling
support
migrate: allow CPU checks that only need information of the target
cpu config: add hv-reenlightenment flag and set TSC frequency for it
migrate: block migration to nodes that cannot provide the TSC
frequency
cpu flags: report invtsc support of nodes
cpu config: set TSC frequency for invtsc to allow live migration
src/PVE/API2/Qemu.pm | 9 +-
src/PVE/QemuMigrate/Helpers.pm | 232 ++++++++++++-
src/PVE/QemuServer.pm | 19 ++
src/PVE/QemuServer/CPUConfig.pm | 32 ++
src/PVE/QemuServer/CPUFlags.pm | 8 +
src/PVE/QemuServer/Helpers.pm | 106 ++++++
src/test/Makefile | 5 +-
src/test/MigrationTest/QemuMigrateMock.pm | 27 +-
src/test/QemuServerTest/CommandLineMocks.pm | 10 +
.../cpu-custom-hv-reenlightenment.conf | 6 +
.../cpu-custom-hv-reenlightenment.conf.cmd | 26 ++
.../cpu-hv-reenlightenment-disabled.conf | 6 +
.../cpu-hv-reenlightenment-disabled.conf.cmd | 26 ++
src/test/cfg2cmd/cpu-hv-reenlightenment.conf | 6 +
.../cfg2cmd/cpu-hv-reenlightenment.conf.cmd | 26 ++
src/test/cfg2cmd/cpu-invtsc.conf | 6 +
src/test/cfg2cmd/cpu-invtsc.conf.cmd | 27 ++
src/test/cfg2cmd/tdx-cpu-invtsc.conf | 9 +
src/test/cfg2cmd/tdx-cpu-invtsc.conf.cmd | 30 ++
src/test/cfg2cmd/tdx-hv-reenlightenment.conf | 9 +
.../cfg2cmd/tdx-hv-reenlightenment.conf.cmd | 30 ++
src/test/run_api_tests.pl | 42 ++-
src/test/run_host_tsc_tests.pl | 133 ++++++++
src/test/run_qemu_migrate_tests.pl | 315 +++++++++++++++++-
24 files changed, 1120 insertions(+), 25 deletions(-)
create mode 100644 src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf
create mode 100644 src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf.cmd
create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf
create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf.cmd
create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment.conf
create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment.conf.cmd
create mode 100644 src/test/cfg2cmd/cpu-invtsc.conf
create mode 100644 src/test/cfg2cmd/cpu-invtsc.conf.cmd
create mode 100644 src/test/cfg2cmd/tdx-cpu-invtsc.conf
create mode 100644 src/test/cfg2cmd/tdx-cpu-invtsc.conf.cmd
create mode 100644 src/test/cfg2cmd/tdx-hv-reenlightenment.conf
create mode 100644 src/test/cfg2cmd/tdx-hv-reenlightenment.conf.cmd
create mode 100755 src/test/run_host_tsc_tests.pl
docs:
Jonas Theisen (1):
qm: document hv-reenlightenment and invtsc
qm.adoc | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
Summary over all repositories:
25 files changed, 1149 insertions(+), 25 deletions(-)
--
Generated by murpp 0.12.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH qemu-server v4 1/7] helpers: add helper to query the host's TSC frequency and scaling support
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 ` 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
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-12-02 17:45 UTC (permalink / raw)
To: pve-devel
Keeping the time stamp counter (TSC) frequency of a VM across live
migrations requires the TSC frequency of the host the VM is started
on, and whether a target node can scale the TSC frequency for its
guests.
Query both directly from KVM, as nothing else exposes them reliably:
upstream Linux does not export the TSC frequency to user space, and
/proc/cpuinfo shows the TSC scaling flag in different places for Intel
and AMD CPUs, regardless of whether KVM uses it. KVM_GET_TSC_KHZ is a
vCPU ioctl, so a temporary VM with a single vCPU is created, which is
cheap, as no guest memory is set up.
If one query fails, the other value is still returned, and a failed
capability check is not mistaken for a missing capability, so that
consumers can tell unknown values from reported ones. The results are
cached, as they cannot change while the host runs, but only if all
queries succeeded. Failed queries are retried after ten minutes, as
callers like pvestatd query this regularly and each query creates a
KVM VM and logs a warning on failure. Without /dev/kvm, nothing is
returned or logged.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/QemuServer/Helpers.pm | 93 +++++++++++++++++++++++
src/test/Makefile | 5 +-
src/test/run_host_tsc_tests.pl | 133 +++++++++++++++++++++++++++++++++
3 files changed, 230 insertions(+), 1 deletion(-)
create mode 100755 src/test/run_host_tsc_tests.pl
diff --git a/src/PVE/QemuServer/Helpers.pm b/src/PVE/QemuServer/Helpers.pm
index 70f9c817..a871473e 100644
--- a/src/PVE/QemuServer/Helpers.pm
+++ b/src/PVE/QemuServer/Helpers.pm
@@ -3,6 +3,7 @@ package PVE::QemuServer::Helpers;
use strict;
use warnings;
+use Fcntl qw(O_RDWR);
use File::stat;
use IO::File;
@@ -364,6 +365,98 @@ sub get_host_phys_address_bits {
return; # undef, cannot really do anything..
}
+# KVM ioctl request numbers and capabilities, see include/uapi/linux/kvm.h in Linux
+my $KVM_CREATE_VM = 0xae01;
+my $KVM_CHECK_EXTENSION = 0xae03;
+my $KVM_CREATE_VCPU = 0xae41;
+my $KVM_GET_TSC_KHZ = 0xaea3;
+my $KVM_CAP_TSC_CONTROL = 60;
+my $KVM_CAP_GET_TSC_KHZ = 61;
+
+my $_host_tsc_info;
+# the incomplete result of the last failed query, and when to query again
+my ($_failed_host_tsc_info, $_host_tsc_info_retry_time);
+my $host_tsc_info_retry_delay_sec = 600;
+
+# KVM_GET_TSC_KHZ is a vCPU ioctl, so it needs a temporary VM with a single vCPU
+my sub query_vcpu_tsc_khz {
+ my ($kvm_fh) = @_;
+
+ my $vm_fd = ioctl($kvm_fh, $KVM_CREATE_VM, 0) // die "unable to create KVM VM - $!\n";
+ open(my $vm_fh, '+<&=', int($vm_fd)) or die "unable to open KVM VM fd - $!\n";
+
+ my $vcpu_fd = ioctl($vm_fh, $KVM_CREATE_VCPU, 0) // die "unable to create KVM vCPU - $!\n";
+ open(my $vcpu_fh, '+<&=', int($vcpu_fd)) or die "unable to open KVM vCPU fd - $!\n";
+
+ my $khz = ioctl($vcpu_fh, $KVM_GET_TSC_KHZ, 0) // die "KVM_GET_TSC_KHZ failed - $!\n";
+ return int($khz);
+}
+
+=head3 get_host_tsc_info
+
+ my $tsc_info = get_host_tsc_info();
+ my ($khz, $scaling) = $tsc_info->@{qw(khz scaling)};
+
+Returns a hash reference with information about the time stamp counter (TSC) of this host, as
+KVM reports it. C<khz> is the TSC frequency in kHz that KVM uses for new vCPUs. C<scaling> is true
+if KVM can run vCPUs with a different TSC frequency. A value is missing if KVM does not report it
+or querying it fails. Failed queries are logged as warnings, and do not affect the other value.
+
+The values are cached once all queries succeeded, as they cannot change while the host is running.
+After a failed query, the incomplete values are returned for ten minutes before querying again,
+as each query creates a KVM VM and callers like pvestatd query this regularly.
+
+Returns no values if C</dev/kvm> does not exist. Dies only if it exists, but cannot be opened.
+
+=cut
+
+sub get_host_tsc_info {
+ return $_host_tsc_info if defined($_host_tsc_info);
+ return $_failed_host_tsc_info
+ if defined($_host_tsc_info_retry_time)
+ && time() < $_host_tsc_info_retry_time;
+
+ my $kvm_fh;
+ if (!sysopen($kvm_fh, '/dev/kvm', O_RDWR)) {
+ return {} if $!{ENOENT}; # for example, KVM is not available on this host
+ die "unable to open /dev/kvm - $!\n";
+ }
+
+ my ($info, $complete) = ({}, 1);
+
+ # a failed check is not cached, unlike a capability KVM reports as unsupported
+ my $check_extension = sub {
+ my ($name, $cap) = @_;
+ my $res = ioctl($kvm_fh, $KVM_CHECK_EXTENSION, $cap);
+ return int($res) if defined($res);
+ warn "unable to check KVM capability $name - $!\n";
+ $complete = 0;
+ return;
+ };
+
+ my $tsc_control = $check_extension->('KVM_CAP_TSC_CONTROL', $KVM_CAP_TSC_CONTROL);
+ $info->{scaling} = $tsc_control ? 1 : 0 if defined($tsc_control);
+
+ if ($check_extension->('KVM_CAP_GET_TSC_KHZ', $KVM_CAP_GET_TSC_KHZ)) {
+ my $khz = eval { query_vcpu_tsc_khz($kvm_fh) };
+ if (my $err = $@) {
+ warn "unable to query the TSC frequency - $err";
+ $complete = 0;
+ } elsif ($khz > 0) {
+ $info->{khz} = $khz;
+ }
+ }
+
+ if ($complete) {
+ $_host_tsc_info = $info;
+ } else {
+ $_failed_host_tsc_info = $info;
+ $_host_tsc_info_retry_time = time() + $host_tsc_info_retry_delay_sec;
+ }
+
+ return $info;
+}
+
=head3 get_host_cpu_info
my $info = get_host_cpu_info();
diff --git a/src/test/Makefile b/src/test/Makefile
index 2d3a55da..706ce4fd 100644
--- a/src/test/Makefile
+++ b/src/test/Makefile
@@ -1,6 +1,6 @@
all: test
-test: test_snapshot test_cfg_to_cmd test_cfg_to_cmd_aarch64 test_hotplug test_hotplug_aarch64 test_pci_addr_conflicts test_pci_reservation test_qemu_img_convert test_migration test_restore_config test_parse_config test_memory test_agent test_raw_config_write test_api test_api_cloudinit_dump test_mtunnel test_guest_identity test_network
+test: test_snapshot test_cfg_to_cmd test_cfg_to_cmd_aarch64 test_hotplug test_hotplug_aarch64 test_pci_addr_conflicts test_pci_reservation test_qemu_img_convert test_migration test_restore_config test_parse_config test_memory test_agent test_raw_config_write test_api test_api_cloudinit_dump test_mtunnel test_guest_identity test_network test_host_tsc
test_snapshot: run_snapshot_tests.pl
./run_snapshot_tests.pl
@@ -66,6 +66,9 @@ test_mtunnel: run_mtunnel_tests.pl QemuServerTest/FakeCFS.pm QemuServerTest/MTun
test_guest_identity: run_guest_identity_tests.pl QemuServerTest/FakeCFS.pm
./run_guest_identity_tests.pl
+test_host_tsc: run_host_tsc_tests.pl
+ ./run_host_tsc_tests.pl
+
.PHONY: clean
clean:
rm -rf MigrationTest/run
diff --git a/src/test/run_host_tsc_tests.pl b/src/test/run_host_tsc_tests.pl
new file mode 100755
index 00000000..a1f21c78
--- /dev/null
+++ b/src/test/run_host_tsc_tests.pl
@@ -0,0 +1,133 @@
+#!/usr/bin/perl
+
+# Tests querying the TSC information of the host from KVM, with the KVM ioctls replaced by
+# predefined results.
+
+use v5.36;
+
+use lib qw(..);
+
+use Test::More;
+
+# The KVM ioctls of the helper are answered by $ioctl, by request number. /dev/kvm is replaced by
+# /dev/null, which also provides the file descriptors of the created VM and vCPU.
+my ($ioctl, $kvm_missing);
+# the time the helper sees, to test when it queries KVM again after a failed query
+my $now = 1_000_000;
+
+BEGIN {
+ *CORE::GLOBAL::time = sub { return $now; };
+ *CORE::GLOBAL::sysopen = sub : prototype(*$$;$) {
+ my (undef, $path) = @_;
+ die "unexpected sysopen of '$path'\n" if $path ne '/dev/kvm';
+ if ($kvm_missing) {
+ $! = 2; # ENOENT
+ return;
+ }
+ return open($_[0], '+<', '/dev/null');
+ };
+ *CORE::GLOBAL::ioctl = sub : prototype(*$$) {
+ my (undef, $request, $arg) = @_;
+ return $ioctl->($request, $arg);
+ };
+}
+
+use PVE::QemuServer::Helpers;
+
+open(my $null_fh, '+<', '/dev/null') or die "unable to open /dev/null - $!\n";
+
+my ($KVM_CREATE_VM, $KVM_CHECK_EXTENSION, $KVM_CREATE_VCPU, $KVM_GET_TSC_KHZ) =
+ (0xae01, 0xae03, 0xae41, 0xaea3);
+my ($KVM_CAP_TSC_CONTROL, $KVM_CAP_GET_TSC_KHZ) = (60, 61);
+
+# Each result is a number, or undef for an ioctl that fails with EIO. Perl's ioctl() returns
+# "0 but true" for 0.
+my sub mock_kvm {
+ my (%results) = @_;
+
+ $ioctl = sub {
+ my ($request, $arg) = @_;
+
+ my $res;
+ if ($request == $KVM_CHECK_EXTENSION) {
+ $res = $results{"cap-$arg"};
+ } elsif ($request == $KVM_CREATE_VM || $request == $KVM_CREATE_VCPU) {
+ $res = fileno($null_fh);
+ } elsif ($request == $KVM_GET_TSC_KHZ) {
+ $res = $results{khz};
+ } else {
+ die "unexpected ioctl $request\n";
+ }
+
+ if (!defined($res)) {
+ $! = 5; # EIO
+ return;
+ }
+ return $res == 0 ? "0 but true" : $res;
+ };
+}
+
+my sub tsc_info_test {
+ my ($name, $expected, $expected_warnings) = @_;
+
+ my @warnings;
+ local $SIG{__WARN__} = sub { push @warnings, $_[0] };
+
+ is_deeply(PVE::QemuServer::Helpers::get_host_tsc_info(), $expected, "$name: result");
+ is_deeply(
+ [map { s/\n$//r } @warnings], $expected_warnings // [], "$name: warnings",
+ );
+}
+
+# not cached, as KVM might become available later
+$kvm_missing = 1;
+tsc_info_test('no KVM', {});
+$kvm_missing = 0;
+
+# Results of failed queries are only kept until KVM is queried again after a delay, so these do
+# not affect each other.
+my $retry_delay = 600;
+
+mock_kvm("cap-$KVM_CAP_TSC_CONTROL" => 0, "cap-$KVM_CAP_GET_TSC_KHZ" => 1, khz => undef);
+tsc_info_test(
+ 'failing TSC frequency query keeps the reported lack of scaling',
+ { scaling => 0 },
+ ["unable to query the TSC frequency - KVM_GET_TSC_KHZ failed - Input/output error"],
+);
+
+$now += $retry_delay;
+mock_kvm("cap-$KVM_CAP_TSC_CONTROL" => undef, "cap-$KVM_CAP_GET_TSC_KHZ" => 1, khz => 2999999);
+tsc_info_test(
+ 'failing scaling check is not reported as lacking scaling',
+ { khz => 2999999 },
+ ["unable to check KVM capability KVM_CAP_TSC_CONTROL - Input/output error"],
+);
+
+$now += $retry_delay;
+mock_kvm("cap-$KVM_CAP_TSC_CONTROL" => 1, "cap-$KVM_CAP_GET_TSC_KHZ" => undef);
+tsc_info_test(
+ 'failing check for the TSC frequency query keeps the scaling support',
+ { scaling => 1 },
+ ["unable to check KVM capability KVM_CAP_GET_TSC_KHZ - Input/output error"],
+);
+
+$now += $retry_delay;
+mock_kvm("cap-$KVM_CAP_TSC_CONTROL" => undef, "cap-$KVM_CAP_GET_TSC_KHZ" => 1, khz => 0);
+tsc_info_test(
+ 'reported zero frequency is not a frequency',
+ {},
+ ["unable to check KVM capability KVM_CAP_TSC_CONTROL - Input/output error"],
+);
+
+# all queries succeed, so the results are cached, but only once KVM is queried again
+mock_kvm("cap-$KVM_CAP_TSC_CONTROL" => 1, "cap-$KVM_CAP_GET_TSC_KHZ" => 1, khz => 2999999);
+$now += $retry_delay - 1;
+tsc_info_test('failed query is not repeated before the delay', {});
+$now += 1;
+tsc_info_test('failed query is repeated after the delay', { scaling => 1, khz => 2999999 });
+
+mock_kvm();
+$now += $retry_delay;
+tsc_info_test('complete results are cached', { scaling => 1, khz => 2999999 });
+
+done_testing();
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH qemu-server v4 2/7] migrate: allow CPU checks that only need information of the target
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 ` 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
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-12-02 17:45 UTC (permalink / raw)
To: pve-devel
The CPU checks for live migration compare the CPU information of the
source and the target node, so they require it from both. Checks that
only depend on the target, for example whether it can run the VM with
the TSC frequency it was started with, do not need it from the source.
Allow marking such checks, so that they are not skipped with a warning
if only the source lacks the information, and a target lacking it is
reported regardless of the source.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/QemuMigrate/Helpers.pm | 19 ++++++++++++-------
1 file changed, 12 insertions(+), 7 deletions(-)
diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index 22ed96d7..685ce9e7 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -341,6 +341,7 @@ my sub get_missing_vm_cpu_flags {
# - '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
+# - 'target-only', set if the check does not need the CPU information of this node
# - 'applies', returning true if the check is relevant for the VM's CPU
# - 'check', returning why the target node is incompatible, if it is, and optionally the details
# of the incompatibility, see $cpu_issue_details
@@ -514,19 +515,23 @@ my $target_cpu_checks = [
# 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. Lost CPU flags of this node are reported, as it cannot determine them itself.
+# reported, unless the check only needs the information of the target node. 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, $source_complete) = ([], 1, 1);
for my $check ($checks->@*) {
- my $blocking = $check->{severity} eq 'error';
+ my ($blocking, $target_only) = ($check->{severity} eq 'error', $check->{'target-only'});
my @missing = grep { !defined($target_info->{$_}) } $check->{needs}->@*;
if (scalar(@missing)) {
- $target_complete = 0 if $blocking && grep { defined($source_info->{$_}) } @missing;
+ $target_complete = 0
+ if $blocking && ($target_only || grep { defined($source_info->{$_}) } @missing);
next;
}
- if (my @source_missing = grep { !defined($source_info->{$_}) } $check->{needs}->@*) {
+ my @source_missing =
+ $target_only ? () : grep { !defined($source_info->{$_}) } $check->{needs}->@*;
+ if (scalar(@source_missing)) {
# 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;
@@ -599,9 +604,9 @@ or C<warning> if it might not, a human-readable C<message>, and optionally C<det
values the message contains, see cpu_issue_details_schema(). 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<missing-cpu-info> with severity C<warning>, unless this node lacks that
-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<override>
-names it.
+information as well and the check compares it. 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<override> names it.
=cut
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH qemu-server v4 3/7] cpu config: add hv-reenlightenment flag and set TSC frequency for it
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 ` 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
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-12-02 17:45 UTC (permalink / raw)
To: pve-devel
Re-enlightenment notifications, together with the TSC frequency MSRs
of 'hv-frequencies', let nested Hyper-V guests, for example Windows
with WSL2 or VBS enabled, pass a stable clock source on to their own
guests across live migrations [0]. Without the frequency MSRs, guests
do not use the notifications, for example Linux then reports
"reenlightenment support is unavailable". This patch does not enable
'hv-frequencies', but custom CPU models can.
KVM does not emulate the old TSC frequency after a migration, so QEMU
refuses to load the migration stream of a guest that enabled these
notifications if no TSC frequency was set explicitly. Set it to the
host's TSC frequency, as reported by KVM, whenever the flag is enabled
for the VM or by a custom CPU model.
Do not set it for confidential VMs. These cannot be live migrated,
snapshotted with RAM or hibernated, so it would be of no use, and QEMU
refuses to start TDX VMs with a TSC frequency that is not a multiple
of 25 MHz.
Treat the CPU configuration as abstracted if the flag is set, so that
the TSC frequency of the running instance is kept for live migration,
snapshots with RAM and hibernation, as the host a VM is resumed on can
have a different frequency.
[0]: https://gitlab.com/qemu-project/qemu/-/blob/v11.1.0/docs/system/i386/hyperv.rst?plain=1#L145
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/QemuServer/CPUConfig.pm | 32 +++++++++++++++++++
src/PVE/QemuServer/CPUFlags.pm | 8 +++++
src/test/QemuServerTest/CommandLineMocks.pm | 6 ++++
.../cpu-custom-hv-reenlightenment.conf | 6 ++++
.../cpu-custom-hv-reenlightenment.conf.cmd | 26 +++++++++++++++
.../cpu-hv-reenlightenment-disabled.conf | 6 ++++
.../cpu-hv-reenlightenment-disabled.conf.cmd | 26 +++++++++++++++
src/test/cfg2cmd/cpu-hv-reenlightenment.conf | 6 ++++
.../cfg2cmd/cpu-hv-reenlightenment.conf.cmd | 26 +++++++++++++++
src/test/cfg2cmd/tdx-hv-reenlightenment.conf | 9 ++++++
.../cfg2cmd/tdx-hv-reenlightenment.conf.cmd | 30 +++++++++++++++++
11 files changed, 181 insertions(+)
create mode 100644 src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf
create mode 100644 src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf.cmd
create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf
create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf.cmd
create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment.conf
create mode 100644 src/test/cfg2cmd/cpu-hv-reenlightenment.conf.cmd
create mode 100644 src/test/cfg2cmd/tdx-hv-reenlightenment.conf
create mode 100644 src/test/cfg2cmd/tdx-hv-reenlightenment.conf.cmd
diff --git a/src/PVE/QemuServer/CPUConfig.pm b/src/PVE/QemuServer/CPUConfig.pm
index 689bfe89..3dde24a6 100644
--- a/src/PVE/QemuServer/CPUConfig.pm
+++ b/src/PVE/QemuServer/CPUConfig.pm
@@ -718,6 +718,8 @@ determined from the configuration alone. Possible abstractions:
=item abstract flag: for example, nested-virt changes with the host
+=item host-dependent setting: for example, the TSC frequency set for hv-reenlightenment
+
=back
=cut
@@ -732,6 +734,7 @@ sub is_abstracted($cpu_property_string) {
for my $flag (split(";", $cpu_conf->{flags})) {
if ($flag =~ $cpu_flag_supported_re) {
return 1 if $2 eq 'nested-virt';
+ return 1 if $2 eq 'hv-reenlightenment' && $1 eq '+';
}
}
return;
@@ -861,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, so that QEMU can migrate and save the state of VMs using these flags.
+
+=cut
+
+sub get_tsc_frequency_flags() {
+ return qw(hv-reenlightenment);
+}
+
# Calculate QEMU's '-cpu' argument from a given VM configuration
sub get_cpu_options(
$conf, $arch, $kvm, $kvm_off, $version_guard, $winversion, $gpu_passthrough,
@@ -965,6 +981,22 @@ sub get_cpu_options(
$vm_flags,
$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.
+ # Confidential VMs cannot be migrated or have their state saved anyway, and TDX only accepts
+ # multiples of 25 MHz.
+ my @tsc_frequency_flags = grep {
+ my $flag = $resolved_flags->{$_};
+ $flag && ($flag->{op} // '') ne '-';
+ } get_tsc_frequency_flags();
+ if ($kvm && $arch eq 'x86_64' && !get_cvm_type($conf) && 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 "
+ . join(' and ', map { "'$_'" } @tsc_frequency_flags) . "\n";
+ $resolved_flags->{'tsc-frequency'} = { op => '', value => $tsc_khz * 1000 };
+ }
+
$cpu_str .= print_cpu_flags($resolved_flags);
my $using_phys_bits_above_40;
diff --git a/src/PVE/QemuServer/CPUFlags.pm b/src/PVE/QemuServer/CPUFlags.pm
index 27b64da7..ef1f19d9 100644
--- a/src/PVE/QemuServer/CPUFlags.pm
+++ b/src/PVE/QemuServer/CPUFlags.pm
@@ -77,6 +77,14 @@ my $supported_vm_specific_cpu_flags_by_arch = {
description =>
"Improve performance for nested virtualization. Only supported on Intel" . " CPUs.",
},
+ {
+ name => 'hv-reenlightenment',
+ description =>
+ "Lets nested Hyper-V, for example with WSL2 or VBS, keep a stable clock for its"
+ . " guests across live migrations if 'hv-frequencies' is enabled via a custom CPU"
+ . " model. Sets the VM's TSC frequency, so live migration needs TSC scaling or a"
+ . " matching TSC frequency on the target.",
+ },
{
name => 'aes',
description => "Activate AES instruction set for HW acceleration.",
diff --git a/src/test/QemuServerTest/CommandLineMocks.pm b/src/test/QemuServerTest/CommandLineMocks.pm
index 7fd4cc23..c1462327 100644
--- a/src/test/QemuServerTest/CommandLineMocks.pm
+++ b/src/test/QemuServerTest/CommandLineMocks.pm
@@ -395,6 +395,9 @@ $qemu_server_helpers->mock(
get_host_phys_address_bits => sub {
return 46;
},
+ get_host_tsc_info => sub {
+ return { khz => 2999999, scaling => 1 };
+ },
kvm_user_version => sub {
return get_test_qemu_version();
},
@@ -480,6 +483,9 @@ cpu-model: qemu64
cpu-model: alldefault
+cpu-model: reenlightenment
+ flags +hv-reenlightenment
+
EOF
);
},
diff --git a/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf b/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf
new file mode 100644
index 00000000..b23d3222
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf
@@ -0,0 +1,6 @@
+# TEST: Test that a custom CPU model enabling hv-reenlightenment sets the VM's TSC frequency to the host's
+cpu: custom-reenlightenment
+memory: 768
+numa: 0
+ostype: win11
+sockets: 1
diff --git a/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf.cmd b/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf.cmd
new file mode 100644
index 00000000..3b822df6
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-custom-hv-reenlightenment.conf.cmd
@@ -0,0 +1,26 @@
+/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' \
+ -global 'kvm-pit.lost_tick_policy=discard' \
+ -cpu 'kvm64,-cet-ibt,-cet-ss,enforce,+hv-reenlightenment,hv_ipi,hv_relaxed,hv_reset,hv_runtime,hv_spinlocks=0x1fff,hv_stimer,hv_synic,hv_time,hv_vapic,hv_vpindex,+kvm_pv_eoi,+kvm_pv_unhalt,+lahf_lm,+sep,tsc-frequency=2999999000' \
+ -m 768 \
+ -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 '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,edid=off' \
+ -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3' \
+ -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \
+ -rtc 'driftfix=slew,base=localtime' \
+ -machine 'hpet=off,type=pc-i440fx-5.1+pve0'
diff --git a/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf b/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf
new file mode 100644
index 00000000..ebf69ea5
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf
@@ -0,0 +1,6 @@
+# TEST: Test that disabling hv-reenlightenment does not set the TSC frequency
+cpu: x86-64-v2-AES,flags=-hv-reenlightenment
+memory: 768
+numa: 0
+ostype: win11
+sockets: 1
diff --git a/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf.cmd b/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf.cmd
new file mode 100644
index 00000000..e0c70911
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-hv-reenlightenment-disabled.conf.cmd
@@ -0,0 +1,26 @@
+/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' \
+ -global 'kvm-pit.lost_tick_policy=discard' \
+ -cpu 'qemu64,+aes,-cet-ibt,-cet-ss,enforce,-hv-reenlightenment,hv_ipi,hv_relaxed,hv_reset,hv_runtime,hv_spinlocks=0x1fff,hv_stimer,hv_synic,hv_time,hv_vapic,hv_vpindex,+kvm_pv_eoi,+kvm_pv_unhalt,+pni,+popcnt,+sse4.1,+sse4.2,+ssse3' \
+ -m 768 \
+ -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 '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,edid=off' \
+ -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3' \
+ -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \
+ -rtc 'driftfix=slew,base=localtime' \
+ -machine 'hpet=off,type=pc-i440fx-5.1+pve0'
diff --git a/src/test/cfg2cmd/cpu-hv-reenlightenment.conf b/src/test/cfg2cmd/cpu-hv-reenlightenment.conf
new file mode 100644
index 00000000..b5f1814f
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-hv-reenlightenment.conf
@@ -0,0 +1,6 @@
+# TEST: Test that enabling hv-reenlightenment sets the VM's TSC frequency to the host's
+cpu: x86-64-v2-AES,flags=+hv-reenlightenment
+memory: 768
+numa: 0
+ostype: win11
+sockets: 1
diff --git a/src/test/cfg2cmd/cpu-hv-reenlightenment.conf.cmd b/src/test/cfg2cmd/cpu-hv-reenlightenment.conf.cmd
new file mode 100644
index 00000000..c3d8e49c
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-hv-reenlightenment.conf.cmd
@@ -0,0 +1,26 @@
+/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' \
+ -global 'kvm-pit.lost_tick_policy=discard' \
+ -cpu 'qemu64,+aes,-cet-ibt,-cet-ss,enforce,+hv-reenlightenment,hv_ipi,hv_relaxed,hv_reset,hv_runtime,hv_spinlocks=0x1fff,hv_stimer,hv_synic,hv_time,hv_vapic,hv_vpindex,+kvm_pv_eoi,+kvm_pv_unhalt,+pni,+popcnt,+sse4.1,+sse4.2,+ssse3,tsc-frequency=2999999000' \
+ -m 768 \
+ -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 '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,edid=off' \
+ -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3' \
+ -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \
+ -rtc 'driftfix=slew,base=localtime' \
+ -machine 'hpet=off,type=pc-i440fx-5.1+pve0'
diff --git a/src/test/cfg2cmd/tdx-hv-reenlightenment.conf b/src/test/cfg2cmd/tdx-hv-reenlightenment.conf
new file mode 100644
index 00000000..883802f6
--- /dev/null
+++ b/src/test/cfg2cmd/tdx-hv-reenlightenment.conf
@@ -0,0 +1,9 @@
+# TEST: Test that hv-reenlightenment does not set the TSC frequency for TDX
+# HW_CAPABILITIES: {"intel-tdx":{"tdx-support":true}}
+bios: ovmf
+cpu: x86-64-v2-AES,flags=+hv-reenlightenment
+intel-tdx: type=tdx
+machine: q35
+ostype: l26
+smbios1: uuid=7b10d7af-b932-4c66-b2c3-3996152ec465
+vmgenid: c773c261-d800-4348-9f5d-167fadd53cf8
diff --git a/src/test/cfg2cmd/tdx-hv-reenlightenment.conf.cmd b/src/test/cfg2cmd/tdx-hv-reenlightenment.conf.cmd
new file mode 100644
index 00000000..5300ef2d
--- /dev/null
+++ b/src/test/cfg2cmd/tdx-hv-reenlightenment.conf.cmd
@@ -0,0 +1,30 @@
+/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 \
+ -smbios 'type=1,uuid=7b10d7af-b932-4c66-b2c3-3996152ec465' \
+ -bios /usr/share/pve-edk2-firmware//OVMF_TDX_4M.ms.fd \
+ -global 'ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off' \
+ -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 qemu64,+aes,enforce,+hv-reenlightenment,+kvm_pv_eoi,+kvm_pv_unhalt,+pni,+popcnt,+sse4.1,+sse4.2,+ssse3 \
+ -m 512 \
+ -global 'ICH9-LPC.disable_s3=1' \
+ -global 'ICH9-LPC.disable_s4=1' \
+ -readconfig /usr/share/qemu-server/pve-q35-4.0.cfg \
+ -device 'pci-bridge,id=pci.4,chassis_nr=4,bus=pci.1,addr=0x1c' \
+ -device 'vmgenid,guid=c773c261-d800-4348-9f5d-167fadd53cf8' \
+ -device 'usb-tablet,id=tablet,bus=ehci.0,port=1' \
+ -device 'VGA,id=vga,bus=pcie.0,addr=0x1' \
+ -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' \
+ -object '{"id":"tdx0","qom-type":"tdx-guest","quote-generation-socket":{"cid":"2","port":"4050","type":"vsock"}}' \
+ -machine 'hpet=off,type=q35+pve0,confidential-guest-support=tdx0,kernel_irqchip=split'
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH qemu-server v4 4/7] migrate: block migration to nodes that cannot provide the TSC frequency
2026-12-02 17:45 [PATCH docs/qemu-server v4 0/7] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
` (2 preceding siblings ...)
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 ` Jonas Theisen
2026-12-02 17:45 ` [PATCH qemu-server v4 5/7] cpu flags: report invtsc support of nodes Jonas Theisen
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-12-02 17:45 UTC (permalink / raw)
To: pve-devel
A VM with 'hv-reenlightenment' runs with the TSC frequency of the host
it was started on. QEMU on the target can only use it if KVM there
supports TSC scaling, or if it is within 250 ppm of the target's TSC
frequency, which KVM accepts without scaling. QEMU hard-codes this
bound [0], matching the default of KVM's 'tsc_tolerance_ppm' module
parameter [1]. Otherwise, the target instance fails to start and the
migration is aborted only after its preparation, for example after
local disks were allocated on the target.
Broadcast the TSC frequency and scaling support as part of the node's
CPU information, and check this upfront for any explicit TSC frequency
of a running KVM VM, as QEMU on the target sets it regardless of why
it was specified. As for the other checks, targets that do not
broadcast this information yet are not blocked, but a warning is
logged. The information of the source node is not needed. The
precondition API reports the TSC frequencies of the VM and the target
as details.
Custom CPU models could already enable 'hv-reenlightenment' without an
explicit TSC frequency. If the guest enabled the notifications, QEMU
on the target refuses the migrated state at the end of the migration.
As this is not known upfront, only warn and suggest to stop and start
the VM.
[0]: https://gitlab.com/qemu-project/qemu/-/blob/v11.1.0/target/i386/kvm/kvm.c#L886
[1]: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/arch/x86/kvm/x86.c?h=v7.0#n164
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/API2/Qemu.pm | 9 ++-
src/PVE/QemuMigrate/Helpers.pm | 82 +++++++++++++++++++++++
src/PVE/QemuServer/Helpers.pm | 13 ++++
src/test/run_api_tests.pl | 42 +++++++++++-
src/test/run_qemu_migrate_tests.pl | 103 ++++++++++++++++++++++++++++-
5 files changed, 243 insertions(+), 6 deletions(-)
diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm
index 326a1e44..9a8bec4a 100644
--- a/src/PVE/API2/Qemu.pm
+++ b/src/PVE/API2/Qemu.pm
@@ -5626,8 +5626,12 @@ __PACKAGE__->register_method({
type => {
type => 'string',
description => "The check that failed.",
- enum =>
- ['cpu-vendor', 'host-cpu-flags', 'vm-cpu-flags'],
+ enum => [
+ 'cpu-vendor',
+ 'host-cpu-flags',
+ 'vm-cpu-flags',
+ 'tsc-frequency',
+ ],
},
message => {
type => 'string',
@@ -5739,6 +5743,7 @@ __PACKAGE__->register_method({
'host-cpu-flags',
'host-cpu-model',
'vm-cpu-flags',
+ 'tsc-frequency',
'cpu-check-failed',
],
},
diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index 685ce9e7..16d201fd 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -214,6 +214,16 @@ my $cpu_issue_details = {
optional => 1,
description => "The CPU model name of the target node.",
},
+ 'vm-tsc-khz' => {
+ type => 'integer',
+ optional => 1,
+ description => "The TSC frequency of the VM in kHz.",
+ },
+ 'target-tsc-khz' => {
+ type => 'integer',
+ optional => 1,
+ description => "The TSC frequency of the target node in kHz.",
+ },
};
my sub vendor_details {
@@ -333,6 +343,14 @@ my sub get_missing_vm_cpu_flags {
return ($missing, $unknown);
}
+# Returns the flags of the running VM that its TSC frequency was set for.
+my sub get_tsc_frequency_flags_of_vm {
+ my ($cpu) = @_;
+ return
+ grep { PVE::JSONSchema::parse_boolean($cpu->{props}->{$_} // 'off') }
+ PVE::QemuServer::CPUConfig::get_tsc_frequency_flags();
+}
+
# Checks whether the CPU of a target node can run a running VM after a live migration. Each check
# has:
# - 'type', identifying the check
@@ -510,6 +528,70 @@ my $target_cpu_checks = [
return ($message, { 'missing-flags' => $unknown });
},
},
+ {
+ type => 'tsc-frequency',
+ severity => 'error',
+ needs => ['tsc-scaling'],
+ 'target-only' => 1,
+ applies => sub {
+ my ($cpu) = @_;
+ # QEMU on the target sets the TSC frequency for any explicit one, not only for the
+ # flags it is generated for
+ return $cpu->{kvm} && $cpu->{props}->{'tsc-frequency'};
+ },
+ check => sub {
+ my ($cpu, $source, $target) = @_;
+
+ return if $target->{'tsc-scaling'};
+
+ my $vm_khz = int($cpu->{props}->{'tsc-frequency'} / 1000);
+ # copied before interpolating it into the message, which would make JSON encode it as
+ # a string
+ my $details = { 'vm-tsc-khz' => $vm_khz };
+ my @flags = get_tsc_frequency_flags_of_vm($cpu);
+ my $requirement = (
+ scalar(@flags)
+ ? join(' and ', map { "'$_'" } @flags)
+ : "the explicit TSC frequency"
+ )
+ . (scalar(@flags) > 1 ? " require" : " requires")
+ . " TSC scaling support or a TSC frequency close to the VM's $vm_khz kHz";
+
+ my $target_khz = $target->{'tsc-khz'}
+ or return ("$requirement, but the node's TSC frequency is unknown", $details);
+
+ # without TSC scaling, KVM can only set frequencies within 250 ppm, which QEMU
+ # considers to be within NTP correction bounds
+ my $tolerance = int($target_khz * 250 / 1_000_000);
+ return if abs($vm_khz - $target_khz) <= $tolerance;
+
+ $details->{'target-tsc-khz'} = $target_khz;
+ return (
+ "$requirement, but the node has $target_khz kHz without TSC scaling",
+ $details,
+ );
+ },
+ },
+ {
+ type => 'tsc-frequency',
+ severity => 'warning',
+ needs => [],
+ applies => sub {
+ my ($cpu) = @_;
+ # for example, started by an older version with a custom CPU model that enables it
+ return
+ $cpu->{kvm}
+ && $cpu->{flags}->{'hv-reenlightenment'}
+ && !$cpu->{props}->{'tsc-frequency'};
+ },
+ check => sub {
+ # QEMU does not know whether the guest enabled the notifications before the transfer
+ return
+ "if the guest enabled re-enlightenment notifications, the migration fails at"
+ . " the end, as the VM was started without an explicit TSC frequency - stop and"
+ . " start the VM to set it";
+ },
+ },
];
# Returns the issues of the target node. If it lacks CPU information needed by a check that can
diff --git a/src/PVE/QemuServer/Helpers.pm b/src/PVE/QemuServer/Helpers.pm
index a871473e..80854ac6 100644
--- a/src/PVE/QemuServer/Helpers.pm
+++ b/src/PVE/QemuServer/Helpers.pm
@@ -471,6 +471,10 @@ encoded as JSON. The returned hash contains:
=item C<model-name>: the CPU model name, for example C<AMD EPYC 7302 16-Core Processor>
+=item C<tsc-khz>: the TSC frequency in kHz that KVM uses for new vCPUs
+
+=item C<tsc-scaling>: whether KVM can run vCPUs with a different TSC frequency
+
=back
Properties that cannot be determined are missing. Consumers of the broadcast information must
@@ -488,6 +492,15 @@ sub get_host_cpu_info {
$info->{vendor} = $cpuinfo->{vendor} if $known->($cpuinfo->{vendor});
$info->{'model-name'} = $cpuinfo->{model} if $known->($cpuinfo->{model});
+ # the TSC is specific to x86
+ my $tsc_info = get_host_arch() eq 'x86_64' ? eval { get_host_tsc_info() } : undef;
+ warn "unable to query TSC information - $@" if $@;
+
+ if ($tsc_info) {
+ $info->{'tsc-khz'} = $tsc_info->{khz} if defined($tsc_info->{khz});
+ $info->{'tsc-scaling'} = $tsc_info->{scaling} if defined($tsc_info->{scaling});
+ }
+
return $info;
}
diff --git a/src/test/run_api_tests.pl b/src/test/run_api_tests.pl
index 36eddd33..5e6d4c1f 100755
--- a/src/test/run_api_tests.pl
+++ b/src/test/run_api_tests.pl
@@ -341,7 +341,12 @@ subtest 'migration preconditions of the CPU of a running VM' => sub {
my ($info, $kvm_flags) = @_;
return { 'host-cpu-info' => encode_json($info), 'cpuflags-kvm' => $kvm_flags };
};
- my $source = { vendor => 'GenuineIntel', 'model-name' => 'Intel Xeon Gold 6338' };
+ my $source = {
+ vendor => 'GenuineIntel',
+ 'model-name' => 'Intel Xeon Gold 6338',
+ 'tsc-khz' => 2999999,
+ 'tsc-scaling' => 1,
+ };
my $node_kv = {
localhost => $entries->($source, 'avx512f fpu sse'),
'other-vendor' => $entries->(
@@ -351,8 +356,13 @@ subtest 'migration preconditions of the CPU of a running VM' => sub {
'missing-flag' => $entries->($source, 'fpu sse'),
'other-model' =>
$entries->({ $source->%*, 'model-name' => 'Intel Core i9-14900K' }, 'avx512f fpu sse'),
+ 'other-tsc' => $entries->(
+ { $source->%*, 'tsc-khz' => 2400000, 'tsc-scaling' => 0 },
+ 'avx512f fpu sse',
+ ),
};
- my $nodes = ['localhost', 'other-vendor', 'no-cpu-info', 'missing-flag', 'other-model'];
+ my $nodes =
+ ['localhost', 'other-vendor', 'no-cpu-info', 'missing-flag', 'other-model', 'other-tsc'];
my $inotify_module = Test::MockModule->new('PVE::INotify');
$inotify_module->mock(nodename => sub { return 'localhost'; });
@@ -429,6 +439,34 @@ subtest 'migration preconditions of the CPU of a running VM' => sub {
},
'reasons for another CPU model',
);
+ is_deeply(
+ $cpu_reasons->('kvm64,tsc-frequency=2999999000'),
+ {
+ 'other-vendor' => ['warning: cpu-vendor'],
+ 'other-tsc' => ['tsc-frequency'],
+ 'no-cpu-info' => ['warning: missing-cpu-info'],
+ },
+ 'reasons for a fixed TSC frequency',
+ );
+ $running_cpu = 'kvm64,tsc-frequency=2999999000';
+ my $tsc_res =
+ PVE::API2::Qemu->migrate_vm_precondition({ node => 'localhost', vmid => 100 });
+ my $tsc_reason = $tsc_res->{not_allowed_nodes}->{'other-tsc'}->{'incompatible-cpu'}->[0];
+ is_deeply(
+ [$tsc_reason->@{qw(vm-tsc-khz target-tsc-khz)}],
+ [2999999, 2400000],
+ 'TSC frequencies included as details',
+ );
+ is(
+ encode_json([$tsc_reason->@{qw(vm-tsc-khz target-tsc-khz)}]),
+ '[2999999,2400000]',
+ 'TSC frequencies encoded as numbers',
+ );
+ my $reasons = $cpu_reasons->('kvm64,+hv-reenlightenment');
+ ok(
+ (grep { $_ eq 'warning: tsc-frequency' } $reasons->{'other-tsc'}->@*),
+ 'warning for re-enlightenment without TSC frequency',
+ );
# confidential VMs cannot be live migrated at all
$qemu_config_module->mock(
diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl
index 2b9f812a..894d9ae2 100755
--- a/src/test/run_qemu_migrate_tests.pl
+++ b/src/test/run_qemu_migrate_tests.pl
@@ -546,19 +546,26 @@ my $default_cpu_info = {
source => {
vendor => 'GenuineIntel',
'model-name' => 'Intel(R) Xeon(R) Gold 6338 CPU @ 2.00GHz',
+ 'tsc-khz' => 2999999,
+ 'tsc-scaling' => 1,
},
nodes => {
pve1 => {
vendor => 'GenuineIntel',
'model-name' => 'Intel(R) Xeon(R) Gold 6338 CPU @ 2.00GHz',
+ 'tsc-khz' => 2999999,
+ 'tsc-scaling' => 1,
},
},
'kvm-flags' => {
- pve0 => 'fpu sse',
- pve1 => 'fpu sse',
+ pve0 => 'fpu hv-reenlightenment sse',
+ pve1 => 'fpu hv-reenlightenment sse',
},
};
+my $reenlightenment_needs = "'hv-reenlightenment' requires TSC scaling support or a TSC frequency"
+ . " close to the VM's 2999999 kHz";
+
my $lacks_avx512f =
"CPU model 'host' passes through the flags of the host CPU, but the node lacks: avx512f";
@@ -2010,6 +2017,98 @@ my $tests = [
},
},
),
+ cpu_migration_test(
+ # as for QEMU, the tolerance is 250 ppm of the node's frequency, here 750 kHz, while 250 ppm
+ # of the VM's frequency would only be 749 kHz
+ name => '1033_running_hv_reenlightenment_tsc_within_tolerance',
+ cpu => 'kvm64,flags=+hv-reenlightenment',
+ runningcpu => 'kvm64,+hv-reenlightenment,tsc-frequency=2999999000',
+ cpu_kept => 1,
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => 3000749, 'tsc-scaling' => 0 } },
+ },
+ ),
+ cpu_migration_test(
+ # the tolerance is still 750 kHz
+ name => '1033_running_hv_reenlightenment_tsc_beyond_tolerance',
+ cpu => 'kvm64,flags=+hv-reenlightenment',
+ runningcpu => 'kvm64,+hv-reenlightenment,tsc-frequency=2999999000',
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => 3000750, 'tsc-scaling' => 0 } },
+ },
+ expect_die =>
+ "can't migrate VM to 'pve1' - incompatible CPU: $reenlightenment_needs, but"
+ . " the node has 3000750 kHz without TSC scaling",
+ ),
+ cpu_migration_test(
+ name => '1033_running_hv_reenlightenment_tsc_scaling',
+ cpu => 'kvm64,flags=+hv-reenlightenment',
+ runningcpu => 'kvm64,+hv-reenlightenment,tsc-frequency=2999999000',
+ cpu_kept => 1,
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => 2400000, 'tsc-scaling' => 1 } },
+ },
+ ),
+ cpu_migration_test(
+ name => '1033_running_hv_reenlightenment_tsc_mismatch',
+ cpu => 'kvm64,flags=+hv-reenlightenment',
+ runningcpu => 'kvm64,+hv-reenlightenment,tsc-frequency=2999999000',
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => 2400000, 'tsc-scaling' => 0 } },
+ },
+ expect_die =>
+ "can't migrate VM to 'pve1' - incompatible CPU: $reenlightenment_needs, but"
+ . " the node has 2400000 kHz without TSC scaling",
+ ),
+ cpu_migration_test(
+ name => '1033_running_hv_reenlightenment_tsc_unknown',
+ cpu => 'kvm64,flags=+hv-reenlightenment',
+ runningcpu => 'kvm64,+hv-reenlightenment,tsc-frequency=2999999000',
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => undef, 'tsc-scaling' => 0 } },
+ },
+ expect_die =>
+ "can't migrate VM to 'pve1' - incompatible CPU: $reenlightenment_needs, but"
+ . " the node's TSC frequency is unknown",
+ ),
+ cpu_migration_test(
+ # QEMU on the target sets any explicit TSC frequency
+ name => '1033_running_tsc_frequency_without_flags_mismatch',
+ cpu => 'kvm64',
+ runningcpu => 'kvm64,enforce,+kvm_pv_eoi,+kvm_pv_unhalt,tsc-frequency=2999999000',
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => 2400000, 'tsc-scaling' => 0 } },
+ },
+ expect_die =>
+ "can't migrate VM to 'pve1' - incompatible CPU: the explicit TSC frequency"
+ . " requires TSC scaling support or a TSC frequency close to the VM's 2999999 kHz, but"
+ . " the node has 2400000 kHz without TSC scaling",
+ ),
+ cpu_migration_test(
+ # for example, started by an older version with a custom CPU model that enables it
+ name => '1033_running_hv_reenlightenment_without_tsc_frequency',
+ cpu => 'kvm64,flags=+hv-reenlightenment',
+ runningcpu => 'kvm64,+hv-reenlightenment',
+ cpu_kept => 1,
+ expect_log => [
+ "CPU of node 'pve1' might be incompatible: if the guest enabled re-enlightenment"
+ . " notifications, the migration fails at the end, as the VM was started without"
+ . " an explicit TSC frequency - stop and start the VM to set it",
+ ],
+ ),
+ cpu_migration_test(
+ name => '1033_running_hv_reenlightenment_target_without_tsc_info',
+ cpu => 'kvm64,flags=+hv-reenlightenment',
+ runningcpu => 'kvm64,+hv-reenlightenment,tsc-frequency=2999999000',
+ cpu_kept => 1,
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => undef, 'tsc-scaling' => undef } },
+ },
+ expect_log => [
+ "CPU of node 'pve1' might be incompatible: the node does not provide the CPU"
+ . " information needed to check it",
+ ],
+ ),
{
name => '105_replicated_to_non_replication_target',
target => 'pve1',
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH qemu-server v4 5/7] cpu flags: report invtsc support of nodes
2026-12-02 17:45 [PATCH docs/qemu-server v4 0/7] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
` (3 preceding siblings ...)
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
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
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-12-02 17:45 UTC (permalink / raw)
To: pve-devel
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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH qemu-server v4 6/7] cpu config: set TSC frequency for invtsc to allow live migration
2026-12-02 17:45 [PATCH docs/qemu-server v4 0/7] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
` (4 preceding siblings ...)
2026-12-02 17:45 ` [PATCH qemu-server v4 5/7] cpu flags: report invtsc support of nodes Jonas Theisen
@ 2026-12-02 17:45 ` Jonas Theisen
2026-12-02 17:45 ` [PATCH docs v4 7/7] qm: document hv-reenlightenment and invtsc Jonas Theisen
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-12-02 17:45 UTC (permalink / raw)
To: pve-devel
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', which
also brings the check for a compatible TSC on the migration target. As
VMs with custom CPU models keep the CPU of the running instance, the
frequency stays the same across live migrations. It is not set for
confidential VMs either.
VMs started with invtsc but without an explicit TSC frequency still
cannot be live migrated until they are stopped and started again.
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 for its migration blockers, as the CPU arguments do
not reliably tell whether the vCPUs have the flag: 'host' and 'max'
silently drop it if the host lacks it, and pass it through without
naming it with 'migratable=off'.
If the query fails, refuse the migration as well if the vCPUs might
have invtsc, as the CPU names it, or as 'host' or 'max' run with
'migratable=off', but suggest to try again instead of restarting the
VM. Otherwise, QEMU does not block the migration because of invtsc, so
only warn, instead of refusing the live migration of every VM if QEMU
does not respond in time.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/QemuMigrate/Helpers.pm | 62 +++++++++++
src/PVE/QemuServer/CPUConfig.pm | 10 +-
src/test/MigrationTest/QemuMigrateMock.pm | 9 ++
src/test/QemuServerTest/CommandLineMocks.pm | 4 +
src/test/cfg2cmd/cpu-invtsc.conf | 6 +
src/test/cfg2cmd/cpu-invtsc.conf.cmd | 27 +++++
src/test/cfg2cmd/tdx-cpu-invtsc.conf | 9 ++
src/test/cfg2cmd/tdx-cpu-invtsc.conf.cmd | 30 +++++
src/test/run_qemu_migrate_tests.pl | 117 +++++++++++++++++++-
9 files changed, 266 insertions(+), 8 deletions(-)
create mode 100644 src/test/cfg2cmd/cpu-invtsc.conf
create mode 100644 src/test/cfg2cmd/cpu-invtsc.conf.cmd
create mode 100644 src/test/cfg2cmd/tdx-cpu-invtsc.conf
create mode 100644 src/test/cfg2cmd/tdx-cpu-invtsc.conf.cmd
diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index ef95c107..54a6cbe4 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -380,6 +380,9 @@ my sub get_tsc_frequency_flags_of_vm {
# 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, and invtsc if 'host' or 'max' pass it through due to an explicit TSC frequency
+# - 'invtsc-blocker' set if QEMU blocks the migration, as the vCPUs have an invariant TSC but no
+# explicit TSC frequency. If querying this failed, 'invtsc-blocker-error' is set if the vCPUs
+# might have invtsc, 'invtsc-blocker-unknown' otherwise.
#
# 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
@@ -559,6 +562,41 @@ my $target_cpu_checks = [
return ($message, { 'missing-flags' => $unknown });
},
},
+ {
+ type => 'tsc-frequency',
+ severity => 'error',
+ needs => [],
+ applies => sub {
+ my ($cpu) = @_;
+ return $cpu->{'invtsc-blocker'} || defined($cpu->{'invtsc-blocker-error'});
+ },
+ check => sub {
+ my ($cpu) = @_;
+
+ # QEMU would only refuse late, for example after local disks were copied
+ if (defined(my $err = $cpu->{'invtsc-blocker-error'})) {
+ return "unable to check whether QEMU blocks the live migration because of"
+ . " 'invtsc', try again - $err";
+ }
+ return
+ "'invtsc' requires an explicit TSC frequency for live migration, but the VM was"
+ . " started without one - stop and start the VM to set it";
+ },
+ },
+ {
+ type => 'tsc-frequency',
+ severity => 'warning',
+ needs => [],
+ applies => sub {
+ my ($cpu) = @_;
+ return defined($cpu->{'invtsc-blocker-unknown'});
+ },
+ check => sub {
+ my ($cpu) = @_;
+ return "unable to check whether QEMU blocks the live migration because of 'invtsc'"
+ . " - $cpu->{'invtsc-blocker-unknown'}";
+ },
+ },
{
type => 'tsc-frequency',
severity => 'error',
@@ -683,6 +721,21 @@ my sub vcpus_have_flag {
return mon_cmd($vmid, 'qom-get', path => $path, property => $flag) ? 1 : 0;
}
+# Returns whether QEMU blocks the migration of the running VM, as its vCPUs have an invariant TSC
+# but no explicit TSC frequency. QEMU reports this, as the CPU arguments do not tell reliably:
+# 'host' and 'max' silently drop invtsc if the host lacks it, and pass it through without naming
+# it with 'migratable=off'.
+my sub has_invtsc_migration_blocker {
+ my ($vmid, $conf, $cpu) = @_;
+
+ # QEMU only blocks the migration with KVM on x86, and without an explicit TSC frequency
+ return 0 if !$cpu->{kvm} || PVE::QemuServer::Helpers::get_vm_arch($conf) ne 'x86_64';
+ return 0 if $cpu->{props}->{'tsc-frequency'};
+
+ my $reasons = mon_cmd($vmid, 'query-migrate')->{'blocked-reasons'} // [];
+ return (grep { m/\(invtsc flag\)/ } $reasons->@*) ? 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) = @_;
@@ -776,6 +829,15 @@ sub check_target_cpu_compatibility {
$cpu->{flags}->{invtsc} = 1 if $invtsc;
}
+ $cpu->{'invtsc-blocker'} = eval { has_invtsc_migration_blocker($vmid, $conf, $cpu) };
+ if (my $err = $@) {
+ chomp($err);
+ # 'host' and 'max' only pass invtsc through without naming it with 'migratable=off'
+ my $migratable = PVE::JSONSchema::parse_boolean($cpu->{props}->{migratable} // 'on') // 1;
+ my $possible = $cpu->{flags}->{invtsc} || (is_host_cpu_model($cpu) && !$migratable);
+ $cpu->{ $possible ? 'invtsc-blocker-error' : 'invtsc-blocker-unknown' } = $err;
+ }
+
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 3dde24a6..ff8d2266 100644
--- a/src/PVE/QemuServer/CPUConfig.pm
+++ b/src/PVE/QemuServer/CPUConfig.pm
@@ -874,7 +874,7 @@ started on, so that QEMU can migrate and save the state of VMs using these flags
=cut
sub get_tsc_frequency_flags() {
- return qw(hv-reenlightenment);
+ return qw(hv-reenlightenment invtsc);
}
# Calculate QEMU's '-cpu' argument from a given VM configuration
@@ -982,10 +982,10 @@ 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.
- # Confidential VMs cannot be migrated or have their state saved anyway, and TDX only accepts
- # multiples of 25 MHz.
+ # 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. Confidential VMs cannot be migrated or have their state saved anyway, and TDX
+ # only accepts multiples of 25 MHz.
my @tsc_frequency_flags = grep {
my $flag = $resolved_flags->{$_};
$flag && ($flag->{op} // '') ne '-';
diff --git a/src/test/MigrationTest/QemuMigrateMock.pm b/src/test/MigrationTest/QemuMigrateMock.pm
index cd281c3a..2793d093 100644
--- a/src/test/MigrationTest/QemuMigrateMock.pm
+++ b/src/test/MigrationTest/QemuMigrateMock.pm
@@ -260,6 +260,15 @@ $MigrationTest::Shared::qemu_migrate_helpers_module->mock(
if ($command eq 'qom-get') {
return vcpus_have_flag($params{property}) ? JSON::true : JSON::false;
}
+ if ($command eq 'query-migrate') {
+ die $test_cpu_info->{'query-migrate-error'}
+ if defined($test_cpu_info->{'query-migrate-error'});
+ # QEMU blocks the migration of vCPUs with invtsc without an explicit TSC frequency
+ return {} if !vcpus_have_flag('invtsc');
+ return {} if $vm_status->{runningcpu} =~ m/(?:^|,)tsc-frequency=/;
+ my $reason = 'State blocked by non-migratable CPU device (invtsc flag)';
+ return { 'blocked-reasons' => [$reason] };
+ }
die "mon_cmd (mocked) - implement me: $command";
},
);
diff --git a/src/test/QemuServerTest/CommandLineMocks.pm b/src/test/QemuServerTest/CommandLineMocks.pm
index c1462327..683ed079 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
+
cpu-model: reenlightenment
flags +hv-reenlightenment
diff --git a/src/test/cfg2cmd/cpu-invtsc.conf b/src/test/cfg2cmd/cpu-invtsc.conf
new file mode 100644
index 00000000..ada88ec7
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-invtsc.conf
@@ -0,0 +1,6 @@
+# TEST: Test that enabling invtsc sets the VM's TSC frequency to the host's
+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/cfg2cmd/tdx-cpu-invtsc.conf b/src/test/cfg2cmd/tdx-cpu-invtsc.conf
new file mode 100644
index 00000000..2e2aec2d
--- /dev/null
+++ b/src/test/cfg2cmd/tdx-cpu-invtsc.conf
@@ -0,0 +1,9 @@
+# TEST: Test that invtsc does not set the TSC frequency for TDX
+# HW_CAPABILITIES: {"intel-tdx":{"tdx-support":true}}
+bios: ovmf
+cpu: custom-invtsc
+intel-tdx: type=tdx
+machine: q35
+ostype: l26
+smbios1: uuid=7b10d7af-b932-4c66-b2c3-3996152ec465
+vmgenid: c773c261-d800-4348-9f5d-167fadd53cf8
diff --git a/src/test/cfg2cmd/tdx-cpu-invtsc.conf.cmd b/src/test/cfg2cmd/tdx-cpu-invtsc.conf.cmd
new file mode 100644
index 00000000..427e6571
--- /dev/null
+++ b/src/test/cfg2cmd/tdx-cpu-invtsc.conf.cmd
@@ -0,0 +1,30 @@
+/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 \
+ -smbios 'type=1,uuid=7b10d7af-b932-4c66-b2c3-3996152ec465' \
+ -bios /usr/share/pve-edk2-firmware//OVMF_TDX_4M.ms.fd \
+ -global 'ICH9-LPC.acpi-pci-hotplug-with-bridge-support=off' \
+ -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 \
+ -m 512 \
+ -global 'ICH9-LPC.disable_s3=1' \
+ -global 'ICH9-LPC.disable_s4=1' \
+ -readconfig /usr/share/qemu-server/pve-q35-4.0.cfg \
+ -device 'pci-bridge,id=pci.4,chassis_nr=4,bus=pci.1,addr=0x1c' \
+ -device 'vmgenid,guid=c773c261-d800-4348-9f5d-167fadd53cf8' \
+ -device 'usb-tablet,id=tablet,bus=ehci.0,port=1' \
+ -device 'VGA,id=vga,bus=pcie.0,addr=0x1' \
+ -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' \
+ -object '{"id":"tdx0","qom-type":"tdx-guest","quote-generation-socket":{"cid":"2","port":"4050","type":"vsock"}}' \
+ -machine 'hpet=off,type=q35+pve0,confidential-guest-support=tdx0,kernel_irqchip=split'
diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl
index 80f33fef..db2afd7b 100755
--- a/src/test/run_qemu_migrate_tests.pl
+++ b/src/test/run_qemu_migrate_tests.pl
@@ -620,6 +620,7 @@ my $tests = [
# 'kvm-flags', and the flags each CPU model enables by default in
# 'model-flags'. 'vcpu-flags' overrides whether the vCPUs have a
# flag, by default they have the flags the running CPU enables.
+ # With 'query-migrate-error', QMP's 'query-migrate' fails with it.
# 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
@@ -2123,7 +2124,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' },
},
@@ -2184,7 +2185,7 @@ my $tests = [
# 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',
+ runningcpu => 'kvm64,enforce,+invtsc,tsc-frequency=2999999000',
opts => { 'allow-missing-cpu-flags' => 1 },
cpu_info => {
'kvm-flags' => { pve0 => 'fpu invtsc sse' },
@@ -2196,7 +2197,7 @@ my $tests = [
# 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',
+ runningcpu => 'kvm64,enforce,+invtsc,tsc-frequency=2999999000',
cpu_kept => 1,
cpu_info => {
nodes => { pve1 => { 'tsc-khz' => undef, 'tsc-scaling' => undef } },
@@ -2206,8 +2207,118 @@ my $tests = [
"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",
+ "CPU of node 'pve1' might be incompatible: the node does not provide the CPU"
+ . " 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 => {
+ 'kvm-flags' => { pve0 => 'fpu invtsc sse', pve1 => 'fpu invtsc sse' },
+ 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 => {
+ 'kvm-flags' => { pve0 => 'fpu invtsc sse', pve1 => 'fpu invtsc sse' },
+ 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 close to 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"
+ . " - stop and start the VM to set it",
+ ),
+ cpu_migration_test(
+ # 'host' passes invtsc through without naming it
+ name => '1033_running_host_cpu_not_migratable_without_tsc_frequency',
+ cpu => 'host',
+ runningcpu => 'host,migratable=off,+kvm_pv_eoi,+kvm_pv_unhalt',
+ cpu_info => {
+ 'vcpu-flags' => { invtsc => 1 },
+ },
+ 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"
+ . " - stop and start the VM to set it",
+ ),
+ cpu_migration_test(
+ name => '1033_running_invtsc_blocker_query_failed',
+ cpu => 'custom-invtsc',
+ runningcpu => 'kvm64,enforce,+invtsc',
+ cpu_info => {
+ 'query-migrate-error' => "got timeout\n",
+ },
+ expect_die =>
+ "can't migrate VM to 'pve1' - incompatible CPU: unable to check whether QEMU"
+ . " blocks the live migration because of 'invtsc', try again - got timeout",
+ ),
+ cpu_migration_test(
+ # vCPUs without invtsc cannot be blocked, so a failed query only warns
+ name => '1033_running_invtsc_blocker_query_failed_without_invtsc',
+ cpu => 'x86-64-v2-AES',
+ runningcpu => 'qemu64,+aes,enforce,+kvm_pv_eoi,+sse4.2',
+ cpu_info => {
+ 'query-migrate-error' => "got timeout\n",
+ },
+ expect_log => [
+ "CPU of node 'pve1' might be incompatible: unable to check whether QEMU blocks the"
+ . " live migration because of 'invtsc' - got timeout",
+ ],
+ ),
+ cpu_migration_test(
+ # migratable 'host' only passes invtsc through if named, so a failed query only warns
+ name => '1033_running_invtsc_blocker_query_failed_host_cpu',
+ cpu => 'host',
+ runningcpu => 'host,+kvm_pv_eoi,+kvm_pv_unhalt',
+ cpu_info => {
+ 'query-migrate-error' => "got timeout\n",
+ },
+ expect_log => [
+ "CPU of node 'pve1' might be incompatible: unable to check whether QEMU blocks the"
+ . " live migration because of 'invtsc' - got timeout",
+ ],
+ ),
+ cpu_migration_test(
+ name => '1033_running_invtsc_blocker_query_failed_host_cpu_not_migratable',
+ cpu => 'host',
+ runningcpu => 'host,migratable=off,+kvm_pv_eoi,+kvm_pv_unhalt',
+ cpu_info => {
+ 'query-migrate-error' => "got timeout\n",
+ },
+ expect_die =>
+ "can't migrate VM to 'pve1' - incompatible CPU: unable to check whether QEMU"
+ . " blocks the live migration because of 'invtsc', try again - got timeout",
+ ),
+ 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
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH docs v4 7/7] qm: document hv-reenlightenment and invtsc
2026-12-02 17:45 [PATCH docs/qemu-server v4 0/7] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
` (5 preceding siblings ...)
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 ` Jonas Theisen
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-12-02 17:45 UTC (permalink / raw)
To: pve-devel
Explain what the new 'hv-reenlightenment' CPU flag is for, that guests
only use it together with 'hv-frequencies', and that custom CPU types
with 'invtsc' can now be live migrated. As both restrict live
migration to nodes with a compatible TSC frequency, add this to the
refused live migrations, and document which running VMs need to be
stopped and started first.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
qm.adoc | 29 +++++++++++++++++++++++++++++
1 file changed, 29 insertions(+)
diff --git a/qm.adoc b/qm.adoc
index 3856b31..0eea939 100644
--- a/qm.adoc
+++ b/qm.adoc
@@ -571,6 +571,19 @@ listings, `Mapping.Modify` gates create/update/delete in the panel, and
enforced on VM create, VM update, and VM clone, so a user who can clone a VM
that uses a custom model still needs `Mapping.Use` on that model's ACL path.
+Custom CPU types can enable the invariant time stamp counter (TSC) flag
+`invtsc`, which guests need to use the TSC as clock source, for example for
+nested hypervisors or applications that read it directly. The host CPU must have
+an invariant TSC as well. Otherwise, QEMU refuses to start the VM, or starts it
+without the flag if the CPU type is based on `host`. Except for confidential
+VMs, {pve} sets the TSC frequency of such VMs to the one of the host they are
+started on, as QEMU requires it for live migration, snapshots including RAM, and
+hibernation. This restricts live migration to nodes with a compatible TSC
+frequency, see <<qm_migration,Migration>>. VMs started without an explicit TSC
+frequency, for example before an update, must be stopped and started again
+before they can be live migrated. A `-cpu` option in the `args` of a VM replaces
+the CPU generated by {pve}, so it must set `tsc-frequency` itself or be removed.
+
[[qm_meltdown_spectre]]
Meltdown / Spectre related CPU flags
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -699,6 +712,17 @@ guests on recent Intel hardware:
only applies for x86_64 vCPUs; it is silently ignored on other
architectures.
+Windows guests running nested Hyper-V, for example with WSL2 or
+virtualization-based security enabled, can pass a stable clock source on to
+their own guests if the VM has the `hv-reenlightenment` flag and the
+`hv-frequencies` flag, which custom CPU types can enable. As for `invtsc`, {pve}
+sets an explicit TSC frequency for VMs with `hv-reenlightenment`, because KVM
+does not fully implement its notifications about TSC frequency changes. With the
+CPU types `host` and `max`, this also passes through the `invtsc` flag of the
+host CPU, so the target node of a live migration must support it as well. If a
+custom CPU type enabled `hv-reenlightenment` before an update, stop and start
+the VM before migrating it, as its live migration can otherwise fail at the end.
+
NUMA
^^^^
@@ -1879,6 +1903,11 @@ if the VM cannot run there:
of the CPU type.
** All VMs require a node that supports their Hyper-V enlightenments, which
{pve} enables for Windows guests.
+** VMs with an explicit TSC frequency, as set for the `invtsc` and
+ `hv-reenlightenment` CPU flags, require a node that supports TSC scaling, or
+ whose TSC frequency differs from the VM's by at most 250 ppm of its own.
+** VMs with the `invtsc` CPU flag that were started without an explicit TSC
+ frequency cannot be live migrated until they are stopped and started again.
--
+
These checks only apply to VMs that use KVM hardware virtualization. The
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-12-02 17:47 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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 ` [PATCH qemu-server v4 5/7] cpu flags: report invtsc support of nodes Jonas Theisen
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
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox