From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [45.144.208.40]) by lore.proxmox.com (Postfix) with ESMTPS id B12741FF066 for ; Tue, 24 Nov 2026 17:34:10 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 8FFC921843; Tue, 24 Nov 2026 17:32:46 +0100 (CET) From: Jonas Theisen 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 Message-ID: <20261124163136.320992-14-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261124163136.320992-1-j.theisen@proxmox.com> References: <20261124163136.320992-1-j.theisen@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1795537934541 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.074 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 3KEDLDVEBJVPQJ2PGDRPQDKZOYXYHRA6 X-Message-ID-Hash: 3KEDLDVEBJVPQJ2PGDRPQDKZOYXYHRA6 X-MailFrom: j.theisen@proxmox.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: 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 --- 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