Improve KVM_SET_TSC_KHZ handling for CoCo VMs
Subsystems (4)
v2 cover Improve KVM_SET_TSC_KHZ handling for CoCo VMs
This series follows Sean's suggestions [1][2] to:
- Reject vCPU scope KVM_SET_TSC_KHZ ioctl for TSC protected vCPU
- Reject VM scope KVM_SET_TSC_KHZ ioctl when vCPUs have been created
.. in the discussion of SEV-SNP Secure TSC support series.
v1 -> v2:
- Protect kvm->create_vcpus check using kvm->lock. - Chao.
- Add documentation update to each patch. -Nikunj.
- Collect RB (Xiaoyao, Nikunj).
- Switched the patch order to make documentation update easier.
v1: https://lore.kernel.org/kvm/[email protected]/
Hi Xiaoyao, Nikunj, I added your RB anyway, so let me know if you have concern :-)
This series has been sanity tested with TDX guests using today's Qemu:
- With this series Qemu can still run TDX guests successfully.
- With some hack to the Qemu, both VM and vCPU scope KVM_SET_TSC_KHZ ioctls failed as expected.
Kai Huang (2):
KVM: x86: Reject KVM_SET_TSC_KHZ VM ioctl when vCPUs have been created
KVM: x86: Reject KVM_SET_TSC_KHZ vCPU ioctl for TSC protected guest
Documentation/virt/kvm/api.rst | 9 ++++++++- arch/x86/kvm/x86.c | 13 ++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-)
base-commit: 6c7ecd725e503bf2ca69ff52c6cc48bb650b1f11
Signature · 1 line
2.50.0
PATCH v2 1/2 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.
--- Documentation/virt/kvm/api.rst | 2 +- arch/x86/kvm/x86.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-)
Documentation/virt/kvm/api.rst +1 -1
@@ -2006,7 +2006,7 @@ frequency is KHz. If the KVM_CAP_VM_TSC_CONTROL capability is advertised, this can also be used as a vm ioctl to set the initial tsc frequency of subsequently -created vCPUs. +created vCPUs. The vm ioctl must be called before any vCPU is created. 4.56 KVM_GET_TSC_KHZ --------------------
arch/x86/kvm/x86.c +6 -3
@@ -7199,9 +7199,12 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)if (user_tsc_khz ==0 ) user_tsc_khz = tsc_khz; -WRITE_ONCE (kvm->arch.default_tsc_khz, user_tsc_khz); - r =0 ; - +mutex_lock (&kvm->lock); +if (!kvm->created_vcpus) { +WRITE_ONCE (kvm->arch.default_tsc_khz, user_tsc_khz); + r =0 ; + } +mutex_unlock (&kvm->lock); goto out; }case KVM_GET_TSC_KHZ : {
Signature · 1 line
2.50.0
Re: [PATCH v2 1/2] KVM: x86: Reject KVM_SET_TSC_KHZ VM ioctl when vCPUs have been created
Quoted 8 lines · Kai Huang
Reject the
KVM_SET_TSC_KHZVM ioctl when vCPUs have been created and update the documentation to reflect it.The VM scope
KVM_SET_TSC_KHZioctl 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.
Quoted 8 lines
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 <[email protected]>Signed-off-by Kai Huang <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>
LTGM:
Quoted 38 lines
--- Documentation/virt/kvm/api.rst | 2 +- arch/x86/kvm/x86.c | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-)Documentation/virt/kvm/api.rst +1
-1@@ -2006,7 +2006,7 @@ frequency is KHz. If the KVM_CAP_VM_TSC_CONTROL capability is advertised, this can also be used as a vm ioctl to set the initial tsc frequency of subsequently -created vCPUs. +created vCPUs. The vm ioctl must be called before any vCPU is created. 4.56 KVM_GET_TSC_KHZ --------------------arch/x86/kvm/x86.c +6
-3@@ -7199,9 +7199,12 @@ int kvm_arch_vm_ioctl(struct file *filp, unsigned int ioctl, unsigned long arg)if (user_tsc_khz ==0 ) user_tsc_khz = tsc_khz; -WRITE_ONCE (kvm->arch.default_tsc_khz, user_tsc_khz); - r =0 ; - +mutex_lock (&kvm->lock); +if (!kvm->created_vcpus) { +WRITE_ONCE (kvm->arch.default_tsc_khz, user_tsc_khz); + r =0 ; + } +mutex_unlock (&kvm->lock); goto out; }case KVM_GET_TSC_KHZ : {
Re: [PATCH v2 1/2] KVM: x86: Reject KVM_SET_TSC_KHZ VM ioctl when vCPUs have been created
In reply to Kai HuangQuoted 8 lines · Kai Huang
Reject the
KVM_SET_TSC_KHZVM ioctl when vCPUs have been created and update the documentation to reflect it.The VM scope
KVM_SET_TSC_KHZioctl 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.
Quoted 8 lines
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 <[email protected]>Signed-off-by Kai Huang <[email protected]>Reviewed-by Xiaoyao Li <[email protected]>
PATCH v2 2/2 KVM: x86: Reject KVM_SET_TSC_KHZ vCPU ioctl for TSC protected guest
In reply to Kai HuangReject KVM_SET_TSC_KHZ vCPU ioctl if guest's TSC is protected and not changeable by KVM, and update the documentation to reflect it.
For such TSC protected guests, e.g. TDX guests, typically the TSC is configured once at VM level before any vCPU are created and remains unchanged during VM's lifetime. KVM provides the KVM_SET_TSC_KHZ VM scope ioctl to allow the userspace VMM to configure the TSC of such VM. After that the userspace VMM is not supposed to call the KVM_SET_TSC_KHZ vCPU scope ioctl anymore when creating the vCPU.
The de facto userspace VMM Qemu does this for TDX guests. The upcoming SEV-SNP guests with Secure TSC should follow.
Note this could be a break of ABI. But for now only TDX guests are TSC protected and only Qemu supports TDX, thus in practice this should not break any existing userspace.
--- Documentation/virt/kvm/api.rst | 7 +++++++ arch/x86/kvm/x86.c | 4 ++++ 2 files changed, 11 insertions(+)
Documentation/virt/kvm/api.rst +7 -0
@@ -2008,6 +2008,13 @@ If the KVM_CAP_VM_TSC_CONTROL capability is advertised, this can also be used as a vm ioctl to set the initial tsc frequency of subsequently created vCPUs. The vm ioctl must be called before any vCPU is created. +For TSC protected Confidential Computing (CoCo) VMs where TSC frequency +is configured once at VM scope and remains unchanged during VM's +lifetime, the vm ioctl should be used to configure the TSC frequency +and the vcpu ioctl is not supported. + +Example of such CoCo VMs: TDX guests. + 4.56 KVM_GET_TSC_KHZ --------------------
arch/x86/kvm/x86.c +4 -0
@@ -6186,6 +6186,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp,u32 user_tsc_khz; r = -EINVAL ; + +if (vcpu->arch.guest_tsc_protected) + goto out; + user_tsc_khz = (u32 )arg; if (kvm_caps.has_tsc_control &&
Signature · 1 line
2.50.0
Quoted 8 lines · Kai Huang
This series follows Sean's suggestions [1][2] to:
- Reject vCPU scope
KVM_SET_TSC_KHZioctl for TSC protected vCPU
- Reject VM scopeKVM_SET_TSC_KHZioctl when vCPUs have been created.. in the discussion of SEV-SNP Secure TSC support series.
[...]
Applied patch 2 to kvm-x86 fixes, with a tweaked changelog to call out that TDX support hasn't yet been released, i.e. that there is no established ABI to break.
Applied patch 1 to kvm-x86 misc, with tweaked documentation to not imply that userspace "must" invoke the ioctl. I think this is the last patch I'll throw into misc for 6.17? So in theory, if it breaks userspace, I can simply truncate it from the pull request.
[1/2] KVM: x86: Reject KVM_SET_TSC_KHZ VM ioctl when vCPUs have been created
https://github.com/kvm-x86/linux/commit/dcbe5a466c12 [2/2] KVM: x86: Reject KVM_SET_TSC_KHZ vCPU ioctl for TSC protected guest https://github.com/kvm-x86/linux/commit/e51cf184d90c
Quoted 10 lines · Sean Christopherson
Quoted 8 lines · Kai Huang
This series follows Sean's suggestions [1][2] to:
- Reject vCPU scope
KVM_SET_TSC_KHZioctl for TSC protected vCPU
- Reject VM scopeKVM_SET_TSC_KHZioctl when vCPUs have been created.. in the discussion of SEV-SNP Secure TSC support series.
[...]
Quoted 8 lines
Applied patch 2 to kvm-x86 fixes, with a tweaked changelog to call out that TDX support hasn't yet been released, i.e. that there is no established ABI to break.
Applied patch 1 to kvm-x86 misc, with tweaked documentation to not imply that userspace "must" invoke the ioctl. I think this is the last patch I'll throw into misc for 6.17? So in theory, if it breaks userspace, I can simply truncate it from the pull request.
Thanks!
Quoted 5 lines
[1/2] KVM: x86: Reject
KVM_SET_TSC_KHZVM ioctl when vCPUs have been createdhttps://github.com/kvm-x86/linux/commit/dcbe5a466c12 [2/2] KVM: x86: Reject KVM_SET_TSC_KHZ vCPU ioctl for TSC protected guest https://github.com/kvm-x86/linux/commit/e51cf184d90c
Btw, in the second patch it seems you have:
Fixes; adafea1 ("KVM: x86: Add infrastructure for secure TSC")
Shouldn't we follow the standard format, i.e.,
?
Sean Christopherson
maintainerQuoted 28 lines · Kai Huang
Quoted 10 lines · Sean Christopherson
Quoted 8 lines · Kai Huang
This series follows Sean's suggestions [1][2] to:
- Reject vCPU scope
KVM_SET_TSC_KHZioctl for TSC protected vCPU
- Reject VM scopeKVM_SET_TSC_KHZioctl when vCPUs have been created.. in the discussion of SEV-SNP Secure TSC support series.
[...]
Quoted 8 lines
Applied patch 2 to kvm-x86 fixes, with a tweaked changelog to call out that TDX support hasn't yet been released, i.e. that there is no established ABI to break.
Applied patch 1 to kvm-x86 misc, with tweaked documentation to not imply that userspace "must" invoke the ioctl. I think this is the last patch I'll throw into misc for 6.17? So in theory, if it breaks userspace, I can simply truncate it from the pull request.
Thanks!
Quoted 5 lines
[1/2] KVM: x86: Reject
KVM_SET_TSC_KHZVM ioctl when vCPUs have been createdhttps://github.com/kvm-x86/linux/commit/dcbe5a466c12 [2/2] KVM: x86: Reject KVM_SET_TSC_KHZ vCPU ioctl for TSC protected guest https://github.com/kvm-x86/linux/commit/e51cf184d90c
Quoted 7 lines
Btw, in the second patch it seems you have:
Fixes; adafea1 ("KVM: x86: Add infrastructure for secure TSC")
Shouldn't we follow the standard format, i.e.,
Fixes adafea110600 ("KVM: x86: Add infrastructure for secure TSC")
Ugh, yes, the semi-colon is just a typo. New hash: