* [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment
@ 2026-11-30 18:34 Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 1/6] cpu flags: report invtsc support of nodes Jonas Theisen
` (6 more replies)
0 siblings, 7 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-11-30 18:34 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. It notifies nested
Hyper-V guests, for example Windows with WSL2 or VBS, about TSC
frequency changes from live migrations. Together with 'hv-frequencies',
which custom CPU models can enable, this lets them keep a stable clock
source for their own guests. Without it, guests do not use the
notifications. 'hv-frequencies' is not enabled by this series. The flag
is added in preparation for 'hv-frequencies' to become usable.
For both flags, the TSC frequency is set explicitly to the host's,
which is queried via KVM_GET_TSC_KHZ on a temporary VM, as upstream
Linux does not export it otherwise. 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:
- 'hv-reenlightenment' or 'invtsc', target can neither scale the TSC
nor match the VM's TSC frequency within 250 ppm
- '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
restarted.
The precondition API reports both with the new 'tsc-frequency' type in
'incompatible-cpu'. pve-manager needs no changes, as pvestatd
broadcasts whatever qemu-server provides.
[0] cover letter of "[PATCH docs/manager/qemu-server v3 00/18] check
the target node's CPU before live migrating a VM"
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
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, so they are not blocked.
- 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 restarted without 'args'
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 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 (5):
cpu flags: report invtsc support of nodes
helpers: add helper to query the host's TSC frequency and scaling
support
cpu config: add hv-reenlightenment flag and set TSC frequency for it
migrate: block migration to nodes with incompatible TSC for
re-enlightenment
cpu config: set TSC frequency for invtsc to allow live migration
src/PVE/API2/Qemu.pm | 8 +-
src/PVE/QemuMigrate/Helpers.pm | 82 +++++++++-
src/PVE/QemuServer.pm | 15 ++
src/PVE/QemuServer/CPUConfig.pm | 35 +++++
src/PVE/QemuServer/CPUFlags.pm | 8 +
src/PVE/QemuServer/Helpers.pm | 65 ++++++++
src/test/MigrationTest/QemuMigrateMock.pm | 12 +-
src/test/QemuServerTest/CommandLineMocks.pm | 7 +
.../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/run_api_tests.pl | 23 ++-
src/test/run_qemu_migrate_tests.pl | 144 +++++++++++++++++-
16 files changed, 488 insertions(+), 8 deletions(-)
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
docs:
Jonas Theisen (1):
qm: document hv-reenlightenment and invtsc
qm.adoc | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
Summary over all repositories:
17 files changed, 512 insertions(+), 8 deletions(-)
--
Generated by murpp 0.12.1
^ permalink raw reply [flat|nested] 8+ messages in thread
* [PATCH qemu-server v3 1/6] cpu flags: report invtsc support of nodes
2026-11-30 18:34 [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
@ 2026-11-30 18:34 ` Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 2/6] helpers: add helper to query the host's TSC frequency and scaling support Jonas Theisen
` (5 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-11-30 18:34 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.
The live migration check for the 'host' CPU model must not require
invtsc on the target if it was not enabled explicitly, as the VM does
not have it then, and nodes with an older version do not report it.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/QemuMigrate/Helpers.pm | 7 +++++--
src/PVE/QemuServer.pm | 15 +++++++++++++++
src/test/MigrationTest/QemuMigrateMock.pm | 2 +-
src/test/run_qemu_migrate_tests.pl | 20 ++++++++++++++++++++
4 files changed, 41 insertions(+), 3 deletions(-)
diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index 8d255716..8d086d72 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -272,11 +272,14 @@ my $target_cpu_checks = [
my $understood = $source->{'understood-flags'};
my @flags = grep { !$understood || $understood->{$_} } keys $source_flags->%*;
+ my $props = $cpu->{props};
my @missing = sort grep {
- # Hyper-V enlightenments are not passed through, but set explicitly
+ # Hyper-V enlightenments are not passed through, but set explicitly. As
+ # 'host' is migratable by default, invtsc is only enabled explicitly, too.
!m/^hv-/
+ && ($_ ne 'invtsc' || $cpu->{flags}->{invtsc})
&& !$target_flags->{$_}
- && (PVE::JSONSchema::parse_boolean($cpu->{props}->{$_} // 'on') // 1)
+ && (PVE::JSONSchema::parse_boolean($props->{$_} // 'on') // 1)
} @flags;
return if !scalar(@missing);
diff --git a/src/PVE/QemuServer.pm b/src/PVE/QemuServer.pm
index f8b3094f..ce281b26 100644
--- a/src/PVE/QemuServer.pm
+++ b/src/PVE/QemuServer.pm
@@ -3040,6 +3040,21 @@ 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 = mon_cmd(
+ $fakevmid,
+ 'query-cpu-model-expansion',
+ type => 'full',
+ model => $model,
+ );
+ $flags->{invtsc} = 1
+ if 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 6f351375..21a85d40 100644
--- a/src/test/MigrationTest/QemuMigrateMock.pm
+++ b/src/test/MigrationTest/QemuMigrateMock.pm
@@ -270,7 +270,7 @@ $MigrationTest::Shared::cluster_module->mock(
my $qemu_server_cpuflags_module = Test::MockModule->new("PVE::QemuServer::CPUFlags");
$qemu_server_cpuflags_module->mock(
query_understood_cpu_flags => sub {
- return [qw(avx512f avx512vl fpu hv-relaxed lahf-lm sse sse4.2)];
+ return [qw(avx512f avx512vl fpu hv-relaxed invtsc lahf-lm sse sse4.2)];
},
);
diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl
index a08cac19..92b96d8e 100755
--- a/src/test/run_qemu_migrate_tests.pl
+++ b/src/test/run_qemu_migrate_tests.pl
@@ -1824,6 +1824,26 @@ my $tests = [
},
},
),
+ 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",
+ ),
{
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 v3 2/6] helpers: add helper to query the host's TSC frequency and scaling support
2026-11-30 18:34 [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 1/6] cpu flags: report invtsc support of nodes Jonas Theisen
@ 2026-11-30 18:34 ` Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 3/6] cpu config: add hv-reenlightenment flag and set TSC frequency for it Jonas Theisen
` (4 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-11-30 18:34 UTC (permalink / raw)
To: pve-devel
Guests whose time stamp counter (TSC) frequency must not change across
a live migration need to know the TSC frequency of the host they are
started on, and whether the hosts in a cluster can scale the TSC
frequency for guests.
Query both directly from KVM, as there is no other interface that
reliably exposes them: upstream Linux does not export the TSC frequency
to user space, and the TSC scaling CPU flag is shown in different
places of /proc/cpuinfo 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. This is cheap, as no guest memory is set up, and the
result is cached, as it cannot change while the host is running.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/QemuServer/Helpers.pm | 53 +++++++++++++++++++++++++++++++++++
1 file changed, 53 insertions(+)
diff --git a/src/PVE/QemuServer/Helpers.pm b/src/PVE/QemuServer/Helpers.pm
index 70f9c817..95a3d23b 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,58 @@ 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;
+
+=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, or undefined if
+KVM cannot report it. C<scaling> is true if KVM can run vCPUs with a different TSC frequency.
+
+The values are cached, as they cannot change while the host is running.
+
+Dies if C</dev/kvm> is not available.
+
+=cut
+
+sub get_host_tsc_info {
+ return $_host_tsc_info if defined($_host_tsc_info);
+
+ sysopen(my $kvm_fh, '/dev/kvm', O_RDWR) or die "unable to open /dev/kvm - $!\n";
+
+ my $check_extension = sub {
+ my ($cap) = @_;
+ return int(ioctl($kvm_fh, $KVM_CHECK_EXTENSION, $cap) // 0);
+ };
+
+ my $info = { scaling => $check_extension->($KVM_CAP_TSC_CONTROL) ? 1 : 0 };
+
+ # KVM_GET_TSC_KHZ is a vCPU ioctl, so it needs a temporary VM with a single vCPU
+ if ($check_extension->($KVM_CAP_GET_TSC_KHZ)) {
+ 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 "unable to get TSC frequency - $!\n";
+ $info->{khz} = int($khz) if int($khz) > 0;
+ }
+
+ return $_host_tsc_info = $info;
+}
+
=head3 get_host_cpu_info
my $info = get_host_cpu_info();
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH qemu-server v3 3/6] cpu config: add hv-reenlightenment flag and set TSC frequency for it
2026-11-30 18:34 [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 1/6] cpu flags: report invtsc support of nodes Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 2/6] helpers: add helper to query the host's TSC frequency and scaling support Jonas Theisen
@ 2026-11-30 18:34 ` Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 4/6] migrate: block migration to nodes with incompatible TSC for re-enlightenment Jonas Theisen
` (3 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-11-30 18:34 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". 'hv-frequencies' is not enabled by this patch,
but custom CPU models can enable it.
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.
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 frequency of the host a VM
is resumed on can differ.
[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 | 18 +++++++++++++
src/PVE/QemuServer/CPUFlags.pm | 8 ++++++
src/test/QemuServerTest/CommandLineMocks.pm | 3 +++
.../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 +++++++++++++++++++
7 files changed, 93 insertions(+)
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
diff --git a/src/PVE/QemuServer/CPUConfig.pm b/src/PVE/QemuServer/CPUConfig.pm
index 414682a6..d629a2c6 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, hv-reenlightenment sets the TSC frequency of the host
+
=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;
@@ -965,6 +968,21 @@ 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.
+ my $reenlightenment = $resolved_flags->{'hv-reenlightenment'};
+ if ($kvm && $arch eq 'x86_64' && $reenlightenment && $reenlightenment->{op} ne '-') {
+ my $tsc_khz = PVE::QemuServer::Helpers::get_host_tsc_info()->{khz}
+ or die "unable to determine the host TSC frequency required for"
+ . " 'hv-reenlightenment'\n";
+ $resolved_flags->{'tsc-frequency'} = {
+ op => '',
+ value => $tsc_khz * 1000,
+ reason => "required by hv-reenlightenment",
+ };
+ }
+
$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 c188c30a..84c53f58 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. Requires 'hv-frequencies' via a custom CPU"
+ . " model. Live migration then 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..d0cd31c2 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();
},
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..7e8150df
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-hv-reenlightenment.conf
@@ -0,0 +1,6 @@
+# TEST: Test that enabling hv-reenlightenment sets the TSC frequency of the host
+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'
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH qemu-server v3 4/6] migrate: block migration to nodes with incompatible TSC for re-enlightenment
2026-11-30 18:34 [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
` (2 preceding siblings ...)
2026-11-30 18:34 ` [PATCH qemu-server v3 3/6] cpu config: add hv-reenlightenment flag and set TSC frequency for it Jonas Theisen
@ 2026-11-30 18:34 ` Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 5/6] cpu config: set TSC frequency for invtsc to allow live migration Jonas Theisen
` (2 subsequent siblings)
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-11-30 18:34 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 node 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 already allocated on the target.
Broadcast the TSC frequency and scaling support as part of the node's
CPU information, and check this upfront. Target nodes that do not
broadcast this information yet are not blocked, but a warning is
logged, as for the other checks.
[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 | 8 +++-
src/PVE/QemuMigrate/Helpers.pm | 28 +++++++++++
src/PVE/QemuServer/Helpers.pm | 12 +++++
src/test/run_api_tests.pl | 23 +++++++++-
src/test/run_qemu_migrate_tests.pl | 74 ++++++++++++++++++++++++++++++
5 files changed, 141 insertions(+), 4 deletions(-)
diff --git a/src/PVE/API2/Qemu.pm b/src/PVE/API2/Qemu.pm
index 3f520125..cd3498a5 100644
--- a/src/PVE/API2/Qemu.pm
+++ b/src/PVE/API2/Qemu.pm
@@ -5619,8 +5619,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',
diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index 8d086d72..f14f0794 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -345,6 +345,34 @@ my $target_cpu_checks = [
. format_flag_list(\@missing);
},
},
+ {
+ type => 'tsc-frequency',
+ severity => 'error',
+ needs => ['tsc-scaling'],
+ applies => sub {
+ my ($cpu) = @_;
+ return $cpu->{flags}->{'hv-reenlightenment'} && $cpu->{props}->{'tsc-frequency'};
+ },
+ check => sub {
+ my ($cpu, $source, $target) = @_;
+
+ return if $target->{'tsc-scaling'};
+
+ my $vm_khz = int($cpu->{props}->{'tsc-frequency'} / 1000);
+ my $requirement = "'hv-reenlightenment' requires TSC scaling support or a TSC"
+ . " frequency within 250 ppm of the VM's $vm_khz kHz";
+
+ my $target_khz = $target->{'tsc-khz'}
+ or return "$requirement, but the node's TSC frequency is unknown";
+
+ # 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;
+
+ return "$requirement, but the node has $target_khz kHz without TSC scaling";
+ },
+ },
];
# 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 95a3d23b..f82daf12 100644
--- a/src/PVE/QemuServer/Helpers.pm
+++ b/src/PVE/QemuServer/Helpers.pm
@@ -431,6 +431,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
@@ -448,6 +452,14 @@ sub get_host_cpu_info {
$info->{vendor} = $cpuinfo->{vendor} if $known->($cpuinfo->{vendor});
$info->{'model-name'} = $cpuinfo->{model} if $known->($cpuinfo->{model});
+ my $tsc_info = eval { get_host_tsc_info() };
+ 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};
+ }
+
return $info;
}
diff --git a/src/test/run_api_tests.pl b/src/test/run_api_tests.pl
index 5eeb7e83..44e3851e 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,15 @@ subtest 'migration preconditions of the CPU of a running VM' => sub {
},
'reasons for another CPU model',
);
+ is_deeply(
+ $cpu_reasons->('kvm64,+hv-reenlightenment,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',
+ );
$qemu_server_module->mock(check_running => sub { return 0; });
$cluster_module->unmock('get_nodelist', 'get_config', 'get_node_kv');
diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl
index 92b96d8e..c8f7976e 100755
--- a/src/test/run_qemu_migrate_tests.pl
+++ b/src/test/run_qemu_migrate_tests.pl
@@ -546,11 +546,15 @@ 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' => {
@@ -559,6 +563,9 @@ my $default_cpu_info = {
},
};
+my $reenlightenment_needs = "'hv-reenlightenment' requires TSC scaling support or a TSC frequency"
+ . " within 250 ppm of the VM's 2999999 kHz";
+
my $lacks_avx512f =
"CPU model 'host' passes through the flags of the host CPU, but the node lacks: avx512f";
@@ -1844,6 +1851,73 @@ my $tests = [
. " through the flags of the host CPU, but the node lacks: invtsc - use"
. " 'allow-missing-cpu-flags' to override",
),
+ 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(
+ 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 v3 5/6] cpu config: set TSC frequency for invtsc to allow live migration
2026-11-30 18:34 [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
` (3 preceding siblings ...)
2026-11-30 18:34 ` [PATCH qemu-server v3 4/6] migrate: block migration to nodes with incompatible TSC for re-enlightenment Jonas Theisen
@ 2026-11-30 18:34 ` Jonas Theisen
2026-11-30 18:34 ` [PATCH docs v3 6/6] qm: document hv-reenlightenment and invtsc Jonas Theisen
2026-11-30 18:36 ` [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-11-30 18:34 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', and
extend the check for a compatible TSC on the migration target to the
flag. As VMs with custom CPU models keep the CPU of the running
instance, the frequency stays the same across live migrations.
VMs that were started with invtsc but without an explicit TSC frequency
still cannot be live migrated until they are restarted. Refuse this
upfront, as QEMU's migration blocker only aborts the migration after
the target instance was started and local disks were copied. Ask QEMU
whether the vCPUs have the flag, as the 'host' and 'max' CPU models
silently drop it if the host lacks it.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/QemuMigrate/Helpers.pm | 53 +++++++++++++++++++--
src/PVE/QemuServer/CPUConfig.pm | 31 +++++++++---
src/test/MigrationTest/QemuMigrateMock.pm | 10 ++++
src/test/QemuServerTest/CommandLineMocks.pm | 4 ++
src/test/cfg2cmd/cpu-invtsc.conf | 6 +++
src/test/cfg2cmd/cpu-invtsc.conf.cmd | 27 +++++++++++
src/test/run_qemu_migrate_tests.pl | 52 +++++++++++++++++++-
7 files changed, 171 insertions(+), 12 deletions(-)
create mode 100644 src/test/cfg2cmd/cpu-invtsc.conf
create mode 100644 src/test/cfg2cmd/cpu-invtsc.conf.cmd
diff --git a/src/PVE/QemuMigrate/Helpers.pm b/src/PVE/QemuMigrate/Helpers.pm
index f14f0794..125c648b 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -177,6 +177,12 @@ my sub vendors_differ {
return defined($source_vendor) && defined($target_vendor) && $source_vendor ne $target_vendor;
}
+# Returns the enabled flags that make the VM run with the TSC frequency of the host it started on.
+my sub get_enabled_tsc_frequency_flags {
+ my ($cpu) = @_;
+ return grep { $cpu->{flags}->{$_} } PVE::QemuServer::CPUConfig::get_tsc_frequency_flags();
+}
+
my sub format_flag_list {
my ($flags) = @_;
@@ -205,6 +211,8 @@ my sub format_flag_list {
# - 'enforce' set if QEMU refuses to start without all enabled flags
# - the enabled flags as 'flags' hash, including the flags the CPU model enables by default if
# 'enforce' is set
+# - 'invtsc-blocker' set if QEMU blocks the migration, as the vCPUs have an invariant TSC but no
+# explicit TSC frequency
#
# The CPU information of the nodes is in the format of get_host_cpu_info(), plus:
# - the CPU flags KVM supports as 'kvm-flags' hash
@@ -345,13 +353,29 @@ my $target_cpu_checks = [
. format_flag_list(\@missing);
},
},
+ {
+ type => 'tsc-frequency',
+ severity => 'error',
+ needs => [],
+ applies => sub {
+ my ($cpu) = @_;
+ return $cpu->{'invtsc-blocker'};
+ },
+ check => sub {
+ # QEMU would only refuse late, for example after local disks were copied
+ return
+ "'invtsc' requires an explicit TSC frequency for live migration, but the VM was"
+ . " started without one - restart the VM to set it";
+ },
+ },
{
type => 'tsc-frequency',
severity => 'error',
needs => ['tsc-scaling'],
applies => sub {
my ($cpu) = @_;
- return $cpu->{flags}->{'hv-reenlightenment'} && $cpu->{props}->{'tsc-frequency'};
+ return
+ scalar(get_enabled_tsc_frequency_flags($cpu)) && $cpu->{props}->{'tsc-frequency'};
},
check => sub {
my ($cpu, $source, $target) = @_;
@@ -359,8 +383,11 @@ my $target_cpu_checks = [
return if $target->{'tsc-scaling'};
my $vm_khz = int($cpu->{props}->{'tsc-frequency'} / 1000);
- my $requirement = "'hv-reenlightenment' requires TSC scaling support or a TSC"
- . " frequency within 250 ppm of the VM's $vm_khz kHz";
+ my @flags = get_enabled_tsc_frequency_flags($cpu);
+ my $requirement =
+ join(' and ', map { "'$_'" } @flags)
+ . (scalar(@flags) > 1 ? " require" : " requires")
+ . " TSC scaling support or a TSC frequency within 250 ppm of the VM's $vm_khz kHz";
my $target_khz = $target->{'tsc-khz'}
or return "$requirement, but the node's TSC frequency is unknown";
@@ -406,6 +433,20 @@ my sub get_target_cpu_issues {
return $issues;
}
+# Returns whether QEMU blocks the migration of the running VM, as its vCPUs have an invariant TSC
+# but no explicit TSC frequency. Only asks QEMU if the CPU enables invtsc, as 'host' and 'max'
+# silently drop it if the host lacks it.
+my sub has_invtsc_migration_blocker {
+ my ($vmid, $cpu) = @_;
+
+ return 0 if !$cpu->{kvm} || !$cpu->{flags}->{invtsc} || $cpu->{props}->{'tsc-frequency'};
+
+ my $vcpus = mon_cmd($vmid, 'query-cpus-fast');
+ my $path = $vcpus->[0]->{'qom-path'} or die "no vCPU found\n";
+
+ return mon_cmd($vmid, 'qom-get', path => $path, property => 'invtsc') ? 1 : 0;
+}
+
# Returns the flags a CPU model enables by default, as QEMU of the running VM defines them.
my sub get_cpu_model_flags {
my ($vmid, $model, $understood) = @_;
@@ -477,6 +518,12 @@ sub check_target_cpu_compatibility {
$enabled ? ($cpu->{flags}->{$prop} = 1) : delete($cpu->{flags}->{$prop});
}
+ $cpu->{'invtsc-blocker'} = eval { has_invtsc_migration_blocker($vmid, $cpu) };
+ if (my $err = $@) {
+ warn "unable to query whether the vCPUs of VM $vmid use invtsc - $err";
+ $cpu->{'invtsc-blocker'} = 1; # the command line enables it
+ }
+
my $checks = [grep { $_->{applies}->($cpu) } $target_cpu_checks->@*];
return {} if !scalar($checks->@*);
diff --git a/src/PVE/QemuServer/CPUConfig.pm b/src/PVE/QemuServer/CPUConfig.pm
index d629a2c6..082f8328 100644
--- a/src/PVE/QemuServer/CPUConfig.pm
+++ b/src/PVE/QemuServer/CPUConfig.pm
@@ -864,6 +864,19 @@ my sub check_phys_bits_above_40_compat($bios, $cpu_type, $cpu_flags) {
}
}
+=head3 get_tsc_frequency_flags
+
+ my @flags = get_tsc_frequency_flags();
+
+Returns the CPU flags for which the TSC frequency of the VM is set to the one of the host it is
+started on, as QEMU cannot migrate VMs with these flags otherwise.
+
+=cut
+
+sub get_tsc_frequency_flags() {
+ return qw(hv-reenlightenment invtsc);
+}
+
# Calculate QEMU's '-cpu' argument from a given VM configuration
sub get_cpu_options(
$conf, $arch, $kvm, $kvm_off, $version_guard, $winversion, $gpu_passthrough,
@@ -969,17 +982,21 @@ sub get_cpu_options(
$pve_forced_flags,
);
- # Without an explicit TSC frequency, QEMU refuses to migrate a guest that enabled
- # re-enlightenment notifications, as KVM does not emulate the old frequency afterwards.
- my $reenlightenment = $resolved_flags->{'hv-reenlightenment'};
- if ($kvm && $arch eq 'x86_64' && $reenlightenment && $reenlightenment->{op} ne '-') {
+ # Without an explicit TSC frequency, QEMU refuses to migrate a guest with an invariant TSC or
+ # one that enabled re-enlightenment notifications, as KVM does not emulate the old frequency
+ # afterwards.
+ my @tsc_frequency_flags = grep {
+ my $flag = $resolved_flags->{$_};
+ $flag && $flag->{op} ne '-';
+ } get_tsc_frequency_flags();
+ if ($kvm && $arch eq 'x86_64' && scalar(@tsc_frequency_flags)) {
my $tsc_khz = PVE::QemuServer::Helpers::get_host_tsc_info()->{khz}
- or die "unable to determine the host TSC frequency required for"
- . " 'hv-reenlightenment'\n";
+ or die "unable to determine the host TSC frequency required for "
+ . join(' and ', map { "'$_'" } @tsc_frequency_flags) . "\n";
$resolved_flags->{'tsc-frequency'} = {
op => '',
value => $tsc_khz * 1000,
- reason => "required by hv-reenlightenment",
+ reason => "required by " . join(' & ', @tsc_frequency_flags),
};
}
diff --git a/src/test/MigrationTest/QemuMigrateMock.pm b/src/test/MigrationTest/QemuMigrateMock.pm
index 21a85d40..b136cec4 100644
--- a/src/test/MigrationTest/QemuMigrateMock.pm
+++ b/src/test/MigrationTest/QemuMigrateMock.pm
@@ -245,6 +245,16 @@ $MigrationTest::Shared::qemu_migrate_helpers_module->mock(
my $flags = $test_cpu_info->{'model-flags'}->{ $params{model}->{name} } // [];
return { model => { props => { map { $_ => JSON::true } $flags->@* } } };
}
+ if ($command eq 'query-cpus-fast') {
+ return [{ 'cpu-index' => 0, 'qom-path' => '/machine/unattached/device[0]' }];
+ }
+ if ($command eq 'qom-get') {
+ # without 'vcpu-flags', the vCPUs have the flags the running CPU enables
+ my $flag = $params{property};
+ my $enabled = $test_cpu_info->{'vcpu-flags'}->{$flag}
+ // ($vm_status->{runningcpu} =~ m/(?:^|,)\+\Q$flag\E(?:,|$)/);
+ return $enabled ? JSON::true : JSON::false;
+ }
die "mon_cmd (mocked) - implement me: $command";
},
);
diff --git a/src/test/QemuServerTest/CommandLineMocks.pm b/src/test/QemuServerTest/CommandLineMocks.pm
index d0cd31c2..187215cf 100644
--- a/src/test/QemuServerTest/CommandLineMocks.pm
+++ b/src/test/QemuServerTest/CommandLineMocks.pm
@@ -483,6 +483,10 @@ cpu-model: qemu64
cpu-model: alldefault
+cpu-model: invtsc
+ reported-model kvm64
+ flags +invtsc
+
EOF
);
},
diff --git a/src/test/cfg2cmd/cpu-invtsc.conf b/src/test/cfg2cmd/cpu-invtsc.conf
new file mode 100644
index 00000000..d5c60fb4
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-invtsc.conf
@@ -0,0 +1,6 @@
+# TEST: Test that enabling invtsc sets the TSC frequency of the host
+cpu: custom-invtsc
+memory: 768
+numa: 0
+ostype: l26
+sockets: 1
diff --git a/src/test/cfg2cmd/cpu-invtsc.conf.cmd b/src/test/cfg2cmd/cpu-invtsc.conf.cmd
new file mode 100644
index 00000000..39269134
--- /dev/null
+++ b/src/test/cfg2cmd/cpu-invtsc.conf.cmd
@@ -0,0 +1,27 @@
+/usr/bin/kvm \
+ -id 8006 \
+ -name vm8006 \
+ -no-shutdown \
+ -chardev 'socket,id=qmp,path=/var/run/qemu-server/8006.qmp,server=on,wait=off' \
+ -object 'monitor-qmp,chardev=qmp,id=monitor-qmp' \
+ -chardev 'socket,id=qmp-event,path=/var/run/qmeventd.sock,reconnect-ms=5000' \
+ -object 'monitor-qmp,chardev=qmp-event,id=monitor-qmp-event' \
+ -pidfile /var/run/qemu-server/8006.pid \
+ -daemonize \
+ -smp '1,sockets=1,cores=1,maxcpus=1' \
+ -nodefaults \
+ -boot 'menu=on,strict=on,reboot-timeout=1000,splash=/usr/share/qemu-server/bootsplash.jpg' \
+ -vnc 'unix:/var/run/qemu-server/8006.vnc,password=on' \
+ -cpu 'kvm64,enforce,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt,+lahf_lm,+sep,tsc-frequency=2999999000' \
+ -m 768 \
+ -global 'PIIX4_PM.disable_s3=1' \
+ -global 'PIIX4_PM.disable_s4=1' \
+ -device 'pci-bridge,id=pci.1,chassis_nr=1,bus=pci.0,addr=0x1e' \
+ -device 'pci-bridge,id=pci.2,chassis_nr=2,bus=pci.0,addr=0x1f' \
+ -device 'pci-bridge,id=pci.4,chassis_nr=4,bus=pci.1,addr=0x1c' \
+ -device 'piix3-usb-uhci,id=uhci,bus=pci.0,addr=0x1.0x2' \
+ -device 'usb-tablet,id=tablet,bus=uhci.0,port=1' \
+ -device 'VGA,id=vga,bus=pci.0,addr=0x2' \
+ -device 'virtio-balloon-pci,id=balloon0,bus=pci.0,addr=0x3,free-page-reporting=on' \
+ -iscsi 'initiator-name=iqn.1993-08.org.debian:01:aabbccddeeff' \
+ -machine 'hpet=off,type=pc+pve0'
diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl
index c8f7976e..3404e2d8 100755
--- a/src/test/run_qemu_migrate_tests.pl
+++ b/src/test/run_qemu_migrate_tests.pl
@@ -618,7 +618,9 @@ my $tests = [
# cpu_info - hash with the CPU information of the 'source' node and of each
# other node in 'nodes', the KVM CPU flags of each node in
# 'kvm-flags', and the flags each CPU model enables by default in
- # 'model-flags', see cpu_migration_test() for tests of the CPU checks
+ # 'model-flags'. 'vcpu-flags' overrides whether the vCPUs have a
+ # flag, by default they have the flags the running CPU enables.
+ # See cpu_migration_test() for tests of the CPU checks.
# expected_calls - hash whose keys are calls which are required
# to be made if the migration gets far enough
# expect_die - expect the migration call to fail, and an error message
@@ -1843,7 +1845,7 @@ my $tests = [
cpu_migration_test(
name => '1033_running_host_cpu_invtsc_enabled',
cpu => 'host',
- runningcpu => 'host,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt',
+ runningcpu => 'host,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt,tsc-frequency=2999999000',
cpu_info => {
'kvm-flags' => { pve0 => 'fpu invtsc sse' },
},
@@ -1918,6 +1920,52 @@ my $tests = [
. " information needed to check it",
],
),
+ cpu_migration_test(
+ name => '1033_running_invtsc_tsc_scaling',
+ cpu => 'custom-invtsc',
+ runningcpu => 'kvm64,enforce,+invtsc,tsc-frequency=2999999000',
+ cpu_kept => 1,
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => 2400000, 'tsc-scaling' => 1 } },
+ },
+ ),
+ cpu_migration_test(
+ name => '1033_running_invtsc_tsc_mismatch',
+ cpu => 'custom-invtsc',
+ runningcpu => 'kvm64,enforce,+invtsc,tsc-frequency=2999999000',
+ cpu_info => {
+ nodes => { pve1 => { 'tsc-khz' => 2400000, 'tsc-scaling' => 0 } },
+ },
+ expect_die => "can't migrate VM to 'pve1' - incompatible CPU: 'invtsc' requires TSC"
+ . " scaling support or a TSC frequency within 250 ppm of the VM's 2999999 kHz, but"
+ . " the node has 2400000 kHz without TSC scaling",
+ ),
+ cpu_migration_test(
+ # for example, started before the TSC frequency was set for invtsc
+ name => '1033_running_invtsc_without_tsc_frequency',
+ cpu => 'custom-invtsc',
+ runningcpu => 'kvm64,enforce,+invtsc',
+ expect_die => "can't migrate VM to 'pve1' - incompatible CPU: 'invtsc' requires an"
+ . " explicit TSC frequency for live migration, but the VM was started without one"
+ . " - restart the VM to set it",
+ ),
+ cpu_migration_test(
+ # only KVM blocks the migration of an invariant TSC
+ name => '1033_running_invtsc_tcg_without_tsc_frequency',
+ cpu => 'custom-invtsc',
+ config => { kvm => 0 },
+ runningcpu => 'kvm64,+invtsc',
+ cpu_kept => 1,
+ ),
+ cpu_migration_test(
+ # 'host' drops invtsc if the host lacks it, so QEMU does not block the migration
+ name => '1033_running_host_cpu_invtsc_dropped',
+ cpu => 'host',
+ runningcpu => 'host,+invtsc,+kvm_pv_eoi,+kvm_pv_unhalt',
+ cpu_info => {
+ 'vcpu-flags' => { invtsc => 0 },
+ },
+ ),
{
name => '105_replicated_to_non_replication_target',
target => 'pve1',
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* [PATCH docs v3 6/6] qm: document hv-reenlightenment and invtsc
2026-11-30 18:34 [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
` (4 preceding siblings ...)
2026-11-30 18:34 ` [PATCH qemu-server v3 5/6] cpu config: set TSC frequency for invtsc to allow live migration Jonas Theisen
@ 2026-11-30 18:34 ` Jonas Theisen
2026-11-30 18:36 ` [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-11-30 18:34 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. Both flags restrict the nodes a
VM can be live migrated to, as the target's TSC frequency must be
compatible, so add this to the live migrations that are refused.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
qm.adoc | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/qm.adoc b/qm.adoc
index 102966b..7a08391 100644
--- a/qm.adoc
+++ b/qm.adoc
@@ -571,6 +571,18 @@ 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 TSC flag `invtsc`, which guests need
+to rely on the TSC as clock source, for example for applications reading it
+directly or for nested hypervisors. The host CPU must have an invariant TSC as
+well. For CPU types based on `host`, QEMU otherwise starts the VM without the
+flag and only logs a warning. {pve} sets the TSC frequency of such VMs to the
+frequency of the host they are started on, as QEMU requires it for live
+migration, snapshots including RAM, and hibernation. Such a VM can only be live
+migrated to nodes that support `invtsc`, and either support TSC scaling or
+have a TSC frequency within 250 ppm of the VM's. VMs that were started without
+an explicit TSC frequency, for example before an update, need to be restarted
+before they can be live migrated.
+
[[qm_meltdown_spectre]]
Meltdown / Spectre related CPU flags
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -699,6 +711,16 @@ guests on recent Intel hardware:
only applies for x86_64 vCPUs; it is silently ignored on other
architectures.
+* The `hv-reenlightenment` flag notifies nested Hyper-V guests, for example
+ Windows with WSL2 or virtualization-based security enabled, about changes of
+ the TSC frequency from live migrations. Together with the `hv-frequencies`
+ flag, which custom CPU types can enable, this lets them keep a stable clock
+ source for their own guests across live migrations. Without it, guests do
+ not use the notifications. {pve} sets the TSC frequency of VMs with
+ `hv-reenlightenment` to the frequency of the host they are started on. Such
+ a VM can only be live migrated to nodes that support TSC scaling, or whose
+ TSC frequency is within 250 ppm of the VM's.
+
NUMA
^^^^
@@ -1878,6 +1900,8 @@ For Live Migration to work, there are some things required:
type, to a node that does not support all flags of the VM's virtual CPU,
including the flags the CPU type enables by default. QEMU would refuse to
start the VM on such a node.
+** VMs with the `hv-reenlightenment` or `invtsc` CPU flag to a node with an
+ incompatible TSC frequency.
--
+
The migration dialog and `qm migrate` show the reason, and migrating all VMs
--
2.47.3
^ permalink raw reply related [flat|nested] 8+ messages in thread
* Re: [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment
2026-11-30 18:34 [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
` (5 preceding siblings ...)
2026-11-30 18:34 ` [PATCH docs v3 6/6] qm: document hv-reenlightenment and invtsc Jonas Theisen
@ 2026-11-30 18:36 ` Jonas Theisen
6 siblings, 0 replies; 8+ messages in thread
From: Jonas Theisen @ 2026-11-30 18:36 UTC (permalink / raw)
To: pve-devel
On 11/30/26 19:34, Jonas Theisen wrote:
> <snip>
>
> [0] cover letter of "[PATCH docs/manager/qemu-server v3 00/18] check
> the target node's CPU before live migrating a VM"
The reference here should have been:
[0]
https://lore.proxmox.com/pve-devel/20261130181007.596064-1-j.theisen@proxmox.com/T/#t
>
> <snip>
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2026-11-30 18:36 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 18:34 [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 1/6] cpu flags: report invtsc support of nodes Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 2/6] helpers: add helper to query the host's TSC frequency and scaling support Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 3/6] cpu config: add hv-reenlightenment flag and set TSC frequency for it Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 4/6] migrate: block migration to nodes with incompatible TSC for re-enlightenment Jonas Theisen
2026-11-30 18:34 ` [PATCH qemu-server v3 5/6] cpu config: set TSC frequency for invtsc to allow live migration Jonas Theisen
2026-11-30 18:34 ` [PATCH docs v3 6/6] qm: document hv-reenlightenment and invtsc Jonas Theisen
2026-11-30 18:36 ` [PATCH docs/qemu-server v3 0/6] live migrate VMs with an invariant TSC or re-enlightenment Jonas Theisen
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox