From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 127F81FF0C0 for ; Wed, 18 Nov 2026 16:43:11 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 8351021889; Wed, 18 Nov 2026 16:42:43 +0100 (CET) From: Jonas Theisen To: pve-devel@lists.proxmox.com Subject: [PATCH qemu-server 09/15] helpers: add helper to query the host's TSC frequency and scaling support Date: Wed, 18 Nov 2026 16:40:40 +0100 Message-ID: <20261118154207.424260-10-j.theisen@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261118154207.424260-1-j.theisen@proxmox.com> References: <20261118154207.424260-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: 1795016557379 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.087 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: NCBZBBR5K6UZEB2PJP2PWVGO4OVKIAGO X-Message-ID-Hash: NCBZBBR5K6UZEB2PJP2PWVGO4OVKIAGO 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: 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 --- 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 3932eb12..098d71eb 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; @@ -359,6 +360,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 is the TSC frequency in kHz that KVM uses for new vCPUs, or undefined if +KVM cannot report it. C 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 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