Skip to content

Commit dcbe5a4

Browse files
kaihuangsean-jc
authored andcommitted
KVM: x86: Reject KVM_SET_TSC_KHZ VM ioctl when vCPUs have been created
Reject the KVM_SET_TSC_KHZ VM ioctl when vCPUs have been created and update the documentation to reflect it. The VM scope KVM_SET_TSC_KHZ ioctl is used to set up the default TSC frequency that all subsequently created vCPUs can use. It is only intended to be called before any vCPU is created. Allowing it to be called after that only results in confusion but nothing good. Note this is an ABI change. But currently in Qemu (the de facto userspace VMM) only TDX uses this VM ioctl, and it is only called once before creating any vCPU, therefore the risk of breaking userspace is pretty low. Suggested-by: Sean Christopherson <seanjc@google.com> Signed-off-by: Kai Huang <kai.huang@intel.com> Reviewed-by: Xiaoyao Li <xiaoyao.li@intel.com> Reviewed-by: Chao Gao <chao.gao@intel.com> Reviewed-by: Nikunj A Dadhania <nikunj@amd.com> Link: https://lore.kernel.org/r/135a35223ce8d01cea06b6cef30bfe494ec85827.1752444335.git.kai.huang@intel.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 95826e1 commit dcbe5a4

2 files changed

Lines changed: 7 additions & 4 deletions

File tree

Documentation/virt/kvm/api.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2006,7 +2006,7 @@ frequency is KHz.
20062006

20072007
If the KVM_CAP_VM_TSC_CONTROL capability is advertised, this can also
20082008
be used as a vm ioctl to set the initial tsc frequency of subsequently
2009-
created vCPUs.
2009+
created vCPUs. Note, the vm ioctl is only allowed prior to creating vCPUs.
20102010

20112011
4.56 KVM_GET_TSC_KHZ
20122012
--------------------

arch/x86/kvm/x86.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7326,9 +7326,12 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)
73267326
if (user_tsc_khz == 0)
73277327
user_tsc_khz = tsc_khz;
73287328

7329-
WRITE_ONCE(kvm->arch.default_tsc_khz, user_tsc_khz);
7330-
r = 0;
7331-
7329+
mutex_lock(&kvm->lock);
7330+
if (!kvm->created_vcpus) {
7331+
WRITE_ONCE(kvm->arch.default_tsc_khz, user_tsc_khz);
7332+
r = 0;
7333+
}
7334+
mutex_unlock(&kvm->lock);
73327335
goto out;
73337336
}
73347337
case KVM_GET_TSC_KHZ: {

0 commit comments

Comments
 (0)