From: Jonas Theisen <j.theisen@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH qemu-server v2 13/20] cpu config: set TSC frequency for invtsc to allow live migration
Date: Tue, 24 Nov 2026 17:30:37 +0100 [thread overview]
Message-ID: <20261124163136.320992-14-j.theisen@proxmox.com> (raw)
In-Reply-To: <20261124163136.320992-1-j.theisen@proxmox.com>
The invariant TSC flag can be enabled with custom CPU models. Unless
the TSC frequency is set explicitly, QEMU blocks live migration,
snapshots with RAM and hibernation of such VMs, as KVM does not emulate
the TSC frequency of the source host afterwards.
Set it to the host's TSC frequency, as for 'hv-reenlightenment', and
extend the check for a compatible TSC on the migration target to the
flag. As VMs with custom CPU models keep the CPU of the running
instance, the frequency stays the same across live migrations.
VMs that were started without an explicit TSC frequency still cannot
be migrated until they are restarted.
Signed-off-by: Jonas Theisen <j.theisen@proxmox.com>
---
src/PVE/QemuMigrate/Helpers.pm | 16 ++-
src/PVE/QemuServer/CPUConfig.pm | 31 ++++--
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 | 110 ++++++++++++++++++++
6 files changed, 184 insertions(+), 10 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 e69f7652..1ac0e7f9 100644
--- a/src/PVE/QemuMigrate/Helpers.pm
+++ b/src/PVE/QemuMigrate/Helpers.pm
@@ -184,6 +184,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) = @_;
@@ -346,7 +352,8 @@ my $target_cpu_checks = [
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) = @_;
@@ -354,8 +361,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";
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/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..61884c4b
--- /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'
\ No newline at end of file
diff --git a/src/test/run_qemu_migrate_tests.pl b/src/test/run_qemu_migrate_tests.pl
index 70fc2d87..c8ef0f00 100755
--- a/src/test/run_qemu_migrate_tests.pl
+++ b/src/test/run_qemu_migrate_tests.pl
@@ -2388,6 +2388,116 @@ my $tests = [
},
},
},
+ {
+ name => '1033_running_invtsc_tsc_scaling',
+ target => 'pve1',
+ vmid => 1033,
+ vm_status => {
+ running => 1,
+ runningmachine => 'pc-q35-5.0+pve0',
+ runningcpu => 'kvm64,enforce,+invtsc,tsc-frequency=2999999000',
+ },
+ cpu_info => {
+ nodes => {
+ pve1 => { vendor => 'GenuineIntel', 'tsc-khz' => 2400000, 'tsc-scaling' => 1 },
+ },
+ },
+ opts => {
+ online => 1,
+ },
+ config_patch => {
+ cpu => 'custom-invtsc',
+ },
+ expected_calls => $default_expected_calls_online,
+ expected => {
+ source_volids => {},
+ target_volids => {},
+ vm_config => get_patched_config(1033, {
+ cpu => 'custom-invtsc',
+ }),
+ vm_status => {
+ running => 1,
+ runningmachine => 'pc-q35-5.0+pve0',
+ runningcpu => 'kvm64,enforce,+invtsc,tsc-frequency=2999999000',
+ },
+ },
+ },
+ {
+ name => '1033_running_invtsc_tsc_mismatch',
+ target => 'pve1',
+ vmid => 1033,
+ vm_status => {
+ running => 1,
+ runningmachine => 'pc-q35-5.0+pve0',
+ runningcpu => 'kvm64,enforce,+invtsc,tsc-frequency=2999999000',
+ },
+ cpu_info => {
+ nodes => {
+ pve1 => { vendor => 'GenuineIntel', 'tsc-khz' => 2400000, 'tsc-scaling' => 0 },
+ },
+ },
+ opts => {
+ online => 1,
+ },
+ config_patch => {
+ cpu => 'custom-invtsc',
+ },
+ expected_calls => {},
+ 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",
+ expected => {
+ source_volids => {},
+ target_volids => {},
+ vm_config => get_patched_config(1033, {
+ cpu => 'custom-invtsc',
+ }),
+ vm_status => {
+ running => 1,
+ runningmachine => 'pc-q35-5.0+pve0',
+ runningcpu => 'kvm64,enforce,+invtsc,tsc-frequency=2999999000',
+ },
+ },
+ },
+ {
+ name => '1033_running_hv_reenlightenment_invtsc_tsc_mismatch',
+ target => 'pve1',
+ vmid => 1033,
+ vm_status => {
+ running => 1,
+ runningmachine => 'pc-q35-5.0+pve0',
+ runningcpu => 'kvm64,enforce,+hv-reenlightenment,+invtsc,tsc-frequency=2999999000',
+ },
+ cpu_info => {
+ nodes => {
+ pve1 => { vendor => 'GenuineIntel', 'tsc-khz' => 2400000, 'tsc-scaling' => 0 },
+ },
+ },
+ opts => {
+ online => 1,
+ },
+ config_patch => {
+ cpu => 'custom-invtsc,flags=+hv-reenlightenment',
+ },
+ expected_calls => {},
+ expect_die => "can't migrate VM to 'pve1' - incompatible CPU: 'hv-reenlightenment' and"
+ . " 'invtsc' require 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",
+ expected => {
+ source_volids => {},
+ target_volids => {},
+ vm_config => get_patched_config(1033, {
+ cpu => 'custom-invtsc,flags=+hv-reenlightenment',
+ }),
+ vm_status => {
+ running => 1,
+ runningmachine => 'pc-q35-5.0+pve0',
+ runningcpu =>
+ 'kvm64,enforce,+hv-reenlightenment,+invtsc,tsc-frequency=2999999000',
+ },
+ },
+ },
{
name => '105_replicated_to_non_replication_target',
target => 'pve1',
--
2.47.3
next prev parent reply other threads:[~2026-11-24 16:34 UTC|newest]
Thread overview: 21+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-24 16:30 [PATCH docs/manager/qemu-server v2 00/20] check the target node's CPU before live migrating a VM Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 01/20] test: migration: allow checking for warnings and errors in the log Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 02/20] migrate: block live migration of host CPU VMs between CPU vendors Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 03/20] fix #6535: migrate: warn about live migration " Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 04/20] cpu flags: factor out parsing the broadcast CPU flags of a node Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 05/20] fix #2924: migrate: block live migration of host CPU VMs to nodes lacking flags Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 06/20] migrate: warn about live migration of host CPU VMs between CPU models Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 07/20] migrate: factor out whether the running virtual CPU is kept Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 08/20] migrate: block migration if the target cannot run the virtual CPU Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 09/20] cpu flags: report invtsc support of nodes Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 10/20] helpers: add helper to query the host's TSC frequency and scaling support Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 11/20] cpu config: add hv-reenlightenment flag and set TSC frequency for it Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 12/20] migrate: block migration to nodes with incompatible TSC for re-enlightenment Jonas Theisen
2026-11-24 16:30 ` Jonas Theisen [this message]
2026-11-24 16:30 ` [PATCH qemu-server v2 14/20] migrate: factor out asserting the CPU compatibility of the target Jonas Theisen
2026-11-24 16:30 ` [PATCH qemu-server v2 15/20] api: migrate: check the CPU of the target before requesting HA migration Jonas Theisen
2026-11-24 16:30 ` [PATCH manager v2 16/20] pvestatd: broadcast host CPU information relevant for live migration Jonas Theisen
2026-11-24 16:30 ` [PATCH manager v2 17/20] ui: migrate: show why the CPU of the target node is incompatible Jonas Theisen
2026-11-24 16:30 ` [PATCH manager v2 18/20] fix #6535: ui: migrate: show CPU warnings for the target node Jonas Theisen
2026-11-24 16:30 ` [PATCH manager v2 19/20] api: nodes: migrateall: skip VMs whose CPU is incompatible with the target Jonas Theisen
2026-11-24 16:30 ` [PATCH docs v2 20/20] qm: document hv-reenlightenment, invtsc and CPU checks for migration Jonas Theisen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261124163136.320992-14-j.theisen@proxmox.com \
--to=j.theisen@proxmox.com \
--cc=pve-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.