KVM: TDX: Enable VM-DoS Prevention Features for TDX
Subsystems (4)
v4 cover KVM: TDX: Enable VM-DoS Prevention Features for TDX
Hi all,
This is v4 of the series to enable the Notify VM Exit and Bus Lock VM exit for TDX, which fixes the KVM CAP issue related with them and allow userspace to actually enable the features.
Compared to v3, this v4 adds 5 more patches. The first 8 patches target for stable kernels while only the patch 9 doesn't have to. Patch 1 is a single patch to enable Notify VM exit for TDX. Except patch 6, patch 2-7 are mandatory for enabling Bus Lock VM exit for TDX in patch 8.
Patch 6 itself is a fix for VMX and is OK for cc stable though maybe not necessary. Patch 6 is added to this series since it can help stop Sashiko repeating its finding of VMX's pre-existing issue, and it's also necessary for patch 9 to consilidate the exit handler for VMX and TDX.
There are other issues of existing code found during previous review, like the EPT MISCONFIG handling. Given they are not mandatory for enabling the Notify VM exit and Bus Lock VM exit for TDX, the plan is to address them in a follow-up series separately.
Please refer to v1 for a full background.
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Xiaoyao Li (9):
KVM: TDX: Enable Notify VM exit
KVM: TDX: Check if there is valid exit infos based on vp_enter_ret
KVM: TDX: Set bits 31:16 to 0 for the synthesized Exit Reason
KVM: TDX: Don't assume exit_reason[31:16] is all-0 in
tdx_to_vmx_exit_reason()
KVM: TDX: Update exit_reason on wait_for_sept_zap return
KVM: VMX: Preserve negative return value in vmx_handle_exit() with bus
lock detected
KVM: VMX: Make handle_bus_lock_vmexit() a shared helper
KVM: TDX: Enable Bus Lock VM exit
KVM: VMX: Consolidate the exit handler for VMX and TDX
arch/x86/kvm/vmx/common.h | 20 ++++++++++ arch/x86/kvm/vmx/main.c | 49 ++++++++++++++++++----- arch/x86/kvm/vmx/tdx.c | 82 ++++++++++++++++++++++++++++++--------- arch/x86/kvm/vmx/vmx.c | 46 ++-------------------- 4 files changed, 126 insertions(+), 71 deletions(-)
base-commit: 1b731e5ded480bd1e5546aed35584238661ce72e
Signature · 1 line
2.43.0
PATCH v4 1/9 KVM: TDX: Enable Notify VM exit
Enable Notify VM exit functionality for TDX guests.
Notify VM exit is an existing feature supported by KVM. Userspace can enable Notify VM exit through KVM_CAP_X86_NOTIFY_VMEXIT when it's reported as supported. However, KVM reports the support of this CAP just based on the hardware capability but doesn't differentiate between VMX and TDX. This leads to the issue that userspace can enable this cap for TDX guests without getting an error, but the feature is not actually enabled because KVM doesn't call the TDX module API to program the relevant TD VMCS fields.
Enable Notify VM exit for TDX guests by:
- Invoking TDX module API calls to set NOTIFY_VM_EXITING and Notify Window in TD VMCS. It's done in tdx_vcpu_init() where other TD VMCS bits are set. Since TDX vCPU cannot be reset, it only needs to be configured once when initializing the TDX vCPU.
- Adding corresponding exit handler for TDX Notify VM Exit.
Notify VM exit can happen when executing the IRET instruction. If the IRET unblocks the NMI blocking state, bit 12 of the exit qualification is set. In this case, the VMM needs to restore the "blocked by NMI" state when it decides to re-enter the guest. For TDX, KVM cannot manage the GUEST_INTERRUPTIBILITY_INFO and it's TDX module's responsibility to handle it. Extract the common part without NMI blocking handling into a helper in common.h so that it can be shared between VMX and TDX.
Note, KVM uses "pre-production" terminology for the feature formally called Notify VM-Exit. All public versions of the SDM refer to the feature as Instruction Timeout. This will be remedied in the near future, for now, use KVM's terminology for consistency.
Note, #2, there is no enumeration bit for Notify VM exit by TDX module because all TDX modules support it, and allow to set the corresponding TD VMCS fields as long as the hardware supports the feature.
---
Changes in v4:
- rename __vmx_handle_notify() to __vt_handle_notify(), to better reflect it is a shared helper for both VMX and TDX.
Changes in v3:
- Collect R-b tag from Rick.
Changes in v2:
- Mention the feature name mismatch between KVM and SDM in changelog and leave the renaming to future, since this patch is targeted for stable
- Extract the common handling into a helper, and put the helper in common.h instead of refactorin the existing handle_notify() in vmx.h
- Add a note to clarify the feature is always supported by TDX module, to make Sashiko happy.
--- arch/x86/kvm/vmx/common.h | 19 +++++++++++++++++++ arch/x86/kvm/vmx/tdx.c | 10 ++++++++++ arch/x86/kvm/vmx/vmx.c | 13 +------------ 3 files changed, 30 insertions(+), 12 deletions(-)
arch/x86/kvm/vmx/common.h +19 -0
@@ -4,6 +4,7 @@#include <linux/kvm_host.h> #include <asm/posted_intr.h> +#include <asm/vmx.h> #include "mmu.h"
@@ -183,6 +184,24 @@ static inline void __vmx_deliver_posted_interrupt(struct kvm_vcpu *vcpu,kvm_vcpu_trigger_posted_interrupt (vcpu,POSTED_INTR_VECTOR ); } +static inline int __vt_handle_notify (struct kvm_vcpu *vcpu, +unsigned long exit_qual) +{ +bool context_invalid = exit_qual &NOTIFY_VM_CONTEXT_INVALID ; + + ++vcpu->stat.notify_window_exits; + +if (vcpu->kvm->arch.notify_vmexit_flags &KVM_X86_NOTIFY_VMEXIT_USER || + context_invalid) { + vcpu->run->exit_reason =KVM_EXIT_NOTIFY ; + vcpu->run->notify.flags = context_invalid ? +KVM_NOTIFY_CONTEXT_INVALID :0 ; +return 0 ; + } + +return 1 ; +} + noinstr void vmx_handle_nmi(struct kvm_vcpu *vcpu);#endif /* __KVM_X86_VMX_COMMON_H */
arch/x86/kvm/vmx/tdx.c +10 -0
@@ -2126,6 +2126,9 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)* - If it's not an MSMI, no need to do anything here. */ return 1 ; +case EXIT_REASON_NOTIFY : +/* NMI blocking state is handled by TDX module */ +return __vt_handle_notify (vcpu,vmx_get_exit_qual (vcpu));default :break ; }
@@ -3154,6 +3157,13 @@ static int tdx_vcpu_init(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *cmd)td_vmcs_write64 (tdx,POSTED_INTR_DESC_ADDR ,__pa (&tdx->vt.pi_desc));td_vmcs_setbit32 (tdx,PIN_BASED_VM_EXEC_CONTROL ,PIN_BASED_POSTED_INTR ); +if (kvm_notify_vmexit_enabled (vcpu->kvm)) { +td_vmcs_setbit32 (tdx,SECONDARY_VM_EXEC_CONTROL , +SECONDARY_EXEC_NOTIFY_VM_EXITING ); +td_vmcs_write32 (tdx,NOTIFY_WINDOW , + vcpu->kvm->arch.notify_window); + } + tdx->state =VCPU_TD_STATE_INITIALIZED ;return 0 ;
arch/x86/kvm/vmx/vmx.c +1 -12
@@ -6279,9 +6279,6 @@ static int handle_bus_lock_vmexit(struct kvm_vcpu *vcpu)static int handle_notify (struct kvm_vcpu *vcpu) {unsigned long exit_qual =vmx_get_exit_qual (vcpu); -bool context_invalid = exit_qual &NOTIFY_VM_CONTEXT_INVALID ; - - ++vcpu->stat.notify_window_exits;/* * Notify VM exit happened while executing iret from NMI,
@@ -6291,15 +6288,7 @@ static int handle_notify(struct kvm_vcpu *vcpu)vmcs_set_bits (GUEST_INTERRUPTIBILITY_INFO ,GUEST_INTR_STATE_NMI ); -if (vcpu->kvm->arch.notify_vmexit_flags &KVM_X86_NOTIFY_VMEXIT_USER || - context_invalid) { - vcpu->run->exit_reason =KVM_EXIT_NOTIFY ; - vcpu->run->notify.flags = context_invalid ? -KVM_NOTIFY_CONTEXT_INVALID :0 ; -return 0 ; - } - -return 1 ; +return __vt_handle_notify (vcpu, exit_qual); }static int vmx_get_msr_imm_reg (struct kvm_vcpu *vcpu)
Signature · 1 line
2.43.0
Re: [PATCH v4 1/9] KVM: TDX: Enable Notify VM exit
Quoted 33 lines · Xiaoyao Li
Enable Notify VM exit functionality for TDX guests.
Notify VM exit is an existing feature supported by KVM. Userspace can enable Notify VM exit through
KVM_CAP_X86_NOTIFY_VMEXITwhen it's reported as supported. However, KVM reports the support of this CAP just based on the hardware capability but doesn't differentiate between VMX and TDX. This leads to the issue that userspace can enable this cap for TDX guests without getting an error, but the feature is not actually enabled because KVM doesn't call the TDX module API to program the relevant TD VMCS fields.Enable Notify VM exit for TDX guests by:
- Invoking TDX module API calls to set
NOTIFY_VM_EXITINGand Notify Window in TD VMCS. It's done intdx_vcpu_init()where other TD VMCS bits are set. Since TDX vCPU cannot be reset, it only needs to be configured once when initializing the TDX vCPU.- Adding corresponding exit handler for TDX Notify VM Exit.
Notify VM exit can happen when executing the IRET instruction. If the IRET unblocks the NMI blocking state, bit 12 of the exit qualification is set. In this case, the VMM needs to restore the "blocked by NMI" state when it decides to re-enter the guest. For TDX, KVM cannot manage the
GUEST_INTERRUPTIBILITY_INFOand it's TDX module's responsibility to handle it. Extract the common part without NMI blocking handling into a helper incommon.hso that it can be shared between VMX and TDX.Note, KVM uses "pre-production" terminology for the feature formally called Notify VM-Exit. All public versions of the SDM refer to the feature as Instruction Timeout. This will be remedied in the near future, for now, use KVM's terminology for consistency.
Quoted 8 lines
Note, #2, there is no enumeration bit for Notify VM exit by TDX module because all TDX modules support it, and allow to set the corresponding TD VMCS fields as long as the hardware supports the feature.
Fixes 161d34609f9b ("KVM: TDX: Make TDX VM type supported")Signed-off-by Xiaoyao Li <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
PATCH v4 2/9 KVM: TDX: Check if there is valid exit infos based on vp_enter_ret
In reply to Xiaoyao LiCheck if there is valid exit info based on vp_enter_ret instead of relying on the clobbered Exit Reason, in tdx_get_exit_info().
Current KVM uses "Exit Reason is not equal to the synthesized invalid Exit Reason, -1u," as the condition to identify there is a real TD Exit and valid exit infos. However, there is one issue with this approach:
KVM updates the Exit Reason to the synthesized invalid Exit Reason for real EPT MISCONFIG as well. This is a false positive for real EPT MISCONFIG, which has valid exit infos.
Though the issue can be addressed by changing the handling for real EPT MISCONFIG to not update the Exit Reason to the synthesized one, relying on the clobbered Exit Reason itself is brittle. Instead, check vp_enter_ret directly to identify if it is a valid Exit Reason.
---
Changes in v4:
- new patch.
--- arch/x86/kvm/vmx/tdx.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
arch/x86/kvm/vmx/tdx.c +14 -8
@@ -921,21 +921,27 @@ static __always_inline u32 tdcall_to_vmx_exit_reason(struct kvm_vcpu *vcpu)return EXIT_REASON_TDCALL ; } -static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) +static __always_inline bool tdx_is_exit_reason_valid(u64 vp_enter_ret) { -struct vcpu_tdx *tdx =to_tdx (vcpu); -u32 exit_reason; - -switch (tdx->vp_enter_ret &TDX_SEAMCALL_STATUS_MASK ) { +switch (vp_enter_ret &TDX_SEAMCALL_STATUS_MASK ) {case TDX_SUCCESS :case TDX_NON_RECOVERABLE_VCPU :case TDX_NON_RECOVERABLE_TD :case TDX_NON_RECOVERABLE_TD_NON_ACCESSIBLE :case TDX_NON_RECOVERABLE_TD_WRONG_APIC_MODE : -break ; +return true;default : -return -1u ; +return false; } +} + +static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); +u32 exit_reason; + +if (!tdx_is_exit_reason_valid (tdx->vp_enter_ret)) +return -1u ; exit_reason = tdx->vp_enter_ret;
@@ -2144,7 +2150,7 @@ void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason,struct vcpu_tdx *tdx =to_tdx (vcpu); *reason = tdx->vt.exit_reason.full; -if (*reason !=-1u ) { +if (tdx_is_exit_reason_valid (tdx->vp_enter_ret)) { *info1 =vmx_get_exit_qual (vcpu); *info2 = tdx->ext_exit_qualification; *intr_info =vmx_get_intr_info (vcpu);
Signature · 1 line
2.43.0
Edgecombe, Rick P
maintainerRe: [PATCH v4 2/9] KVM: TDX: Check if there is valid exit infos based on vp_enter_ret
Quoted 15 lines · Xiaoyao Li
Check if there is valid exit info based on
vp_enter_retinstead of relying on the clobbered Exit Reason, intdx_get_exit_info().Current KVM uses "Exit Reason is not equal to the synthesized invalid Exit Reason, -1u," as the condition to identify there is a real TD Exit and valid exit infos. However, there is one issue with this approach:
KVM updates the Exit Reason to the synthesized invalid Exit Reason for real EPT MISCONFIG as well. This is a false positive for real EPT MISCONFIG, which has valid exit infos.Though the issue can be addressed by changing the handling for real EPT MISCONFIG to not update the Exit Reason to the synthesized one, relying on the clobbered Exit Reason itself is brittle. Instead, check
vp_enter_retdirectly to identify if it is a valid Exit Reason.
Quoted 5 lines
Fixes da407fe45908 ("KVM: TDX: Handle EPT violation/misconfig exit")Suggested-by Sean Christopherson <[email protected]>Signed-off-by Xiaoyao Li <[email protected]>---
This series has grown unfortunately, and I wasn't able to catch up on the threads from yesterday. Can we really not put this one in the non-stable patches at the end? Or maybe you can explain why it's needed?
Also "KVM: VMX: Preserve negative return value in vmx_handle_exit() with bus lock detected"?
Re: [PATCH v4 2/9] KVM: TDX: Check if there is valid exit infos based on vp_enter_ret
Quoted 22 lines · Rick P
Quoted 15 lines · Xiaoyao Li
Check if there is valid exit info based on
vp_enter_retinstead of relying on the clobbered Exit Reason, intdx_get_exit_info().Current KVM uses "Exit Reason is not equal to the synthesized invalid Exit Reason, -1u," as the condition to identify there is a real TD Exit and valid exit infos. However, there is one issue with this approach:
KVM updates the Exit Reason to the synthesized invalid Exit Reason for real EPT MISCONFIG as well. This is a false positive for real EPT MISCONFIG, which has valid exit infos.Though the issue can be addressed by changing the handling for real EPT MISCONFIG to not update the Exit Reason to the synthesized one, relying on the clobbered Exit Reason itself is brittle. Instead, check
vp_enter_retdirectly to identify if it is a valid Exit Reason.Quoted 5 lines
Fixes da407fe45908 ("KVM: TDX: Handle EPT violation/misconfig exit")Suggested-by Sean Christopherson <[email protected]>Signed-off-by Xiaoyao Li <[email protected]>---
Quoted 3 lines
This series has grown unfortunately, and I wasn't able to catch up on the threads from yesterday. Can we really not put this one in the non-stable patches at the end? Or maybe you can explain why it's needed?
It was suggested[1] (or requested) by Sean.
I agree that it's not mandatory for enabling Bus Lock VM exit for TDX. But considering that without this patch, both patch 3 and patch 4 need to change the line
if (*reason != -1u)
in tdx_get_exit_info(), which brings unnecessary code churn. And given the patch number needs to increase anyway in this v4. So I just put this patch in to make patch 3 and patch 4 simpler.
[1] https://lore.kernel.org/all/[email protected]/
Quoted 2 lines
Also "KVM: VMX: Preserve negative return value in
vmx_handle_exit()with bus lock detected"?
It was suggested[2] by Sean.
(Since there are discussions happening in the cover letter thread about it. Let's continue the discussion there.)
Re: [PATCH v4 2/9] KVM: TDX: Check if there is valid exit infos based on vp_enter_ret
In reply to Xiaoyao LiQuoted 9 lines · Xiaoyao Li
Check if there is valid exit info based on
vp_enter_retinstead of relying on the clobbered Exit Reason, intdx_get_exit_info().Current KVM uses "Exit Reason is not equal to the synthesized invalid Exit Reason, -1u," as the condition to identify there is a real TD Exit and valid exit infos. However, there is one issue with this approach:
KVM updates the Exit Reason to the synthesized invalid Exit Reason for real EPT MISCONFIG as well. This is a false positive for real EPT MISCONFIG, which has valid exit infos.
Nit:
Is it better to add the info the real EPT MISCONFIG is not expected for TDX case, so that readers can know the severity of the bug?
Quoted 9 lines
Quoted 1 line
Though the issue can be addressed by changing the handling for real EPT
MISCONFIG to not update the Exit Reason to the synthesized one, relying on the clobbered Exit Reason itself is brittle. Instead, check
vp_enter_retdirectly to identify if it is a valid Exit Reason.Fixes da407fe45908 ("KVM: TDX: Handle EPT violation/misconfig exit")Suggested-by Sean Christopherson <[email protected]>Signed-off-by Xiaoyao Li <[email protected]>
PATCH v4 3/9 KVM: TDX: Set bits 31:16 to 0 for the synthesized Exit Reason
In reply to Xiaoyao LiSet bits 31:16 to 0 instead of all-1s for KVM's synthesized Exit Reason.
KVM is going to support Bus Lock VM exit for TDX, after which bit 26 of the Exit Reason becomes meaningful and indicates that a bus lock happened. The existing synthesized Exit Reason, -1u, will cause a false positive in that case. Change the synthesized Exit Reason from -1u to U16_MAX, so that bits 31:16 are set to 0. This also avoids the potential issues when other bits in 31:16 become valid in the future.
As a bonus, the check for synthesized Exit Reason in tdx_failed_vmentry() becomes unnecessary. Just drop it.
---
Note, the checking of tdx_failed_vmentry() looks to miss the case where a real EPT_MISCONFIG happens with failed_vmentry being set. First, in practice, EPT_MISCONFIG cannot happen with failed_vmentry being set. Second, even if it can, this is a pre-existing issue and the next patch can address it.
Changes in v4:
- Collect R-b from Rick.
Changes in v3:
- split from the patch 2 in v2.
- define a MARCO for the synthesized invalid Exit Reason.
--- arch/x86/kvm/vmx/tdx.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)
arch/x86/kvm/vmx/tdx.c +11 -4
@@ -935,13 +935,21 @@ static __always_inline bool tdx_is_exit_reason_valid(u64 vp_enter_ret) } } +/* Synthesized invalid Exit Reason */ +#define TDX_INVALID_EXIT_REASON U16_MAX + static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu);u32 exit_reason; +/* +* Return the synthesized invalid Exit Reason, as the TDX module +* never attempted to run the vCPU, i.e. the Exit Reason is undefined, +* but this is NOT a failed VM-Enter. +*/ if (!tdx_is_exit_reason_valid (tdx->vp_enter_ret)) -return -1u ; +return TDX_INVALID_EXIT_REASON ; exit_reason = tdx->vp_enter_ret;
@@ -956,7 +964,7 @@ static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu)* Defer KVM_BUG_ON() until tdx_handle_exit() because this is in * non-instrumentable code with interrupts disabled. */ -return -1u ; +return TDX_INVALID_EXIT_REASON ;default :break ; }
@@ -987,8 +995,7 @@ static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu)static bool tdx_failed_vmentry (struct kvm_vcpu *vcpu) { -return vmx_get_exit_reason (vcpu).failed_vmentry && -vmx_get_exit_reason (vcpu).full !=-1u ; +return vmx_get_exit_reason (vcpu).failed_vmentry; }static fastpath_t tdx_exit_handlers_fastpath (struct kvm_vcpu *vcpu)
Signature · 1 line
2.43.0
Re: [PATCH v4 3/9] KVM: TDX: Set bits 31:16 to 0 for the synthesized Exit Reason
Quoted 9 lines · Xiaoyao Li
Set bits 31:16 to 0 instead of all-1s for KVM's synthesized Exit Reason.
KVM is going to support Bus Lock VM exit for TDX, after which bit 26 of the Exit Reason becomes meaningful and indicates that a bus lock happened. The existing synthesized Exit Reason, -1u, will cause a false positive in that case. Change the synthesized Exit Reason from -1u to
U16_MAX, so that bits 31:16 are set to 0. This also avoids the potential issues when other bits in 31:16 become valid in the future.
Quoted 6 lines
As a bonus, the check for synthesized Exit Reason in
tdx_failed_vmentry()becomes unnecessary. Just drop it.Signed-off-by Xiaoyao Li <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>
Sean Christopherson
maintainerRe: [PATCH v4 3/9] KVM: TDX: Set bits 31:16 to 0 for the synthesized Exit Reason
In reply to Xiaoyao LiQuoted 37 lines · Xiaoyao Li
Set bits 31:16 to 0 instead of all-1s for KVM's synthesized Exit Reason.
KVM is going to support Bus Lock VM exit for TDX, after which bit 26 of the Exit Reason becomes meaningful and indicates that a bus lock happened. The existing synthesized Exit Reason, -1u, will cause a false positive in that case. Change the synthesized Exit Reason from -1u to
U16_MAX, so that bits 31:16 are set to 0. This also avoids the potential issues when other bits in 31:16 become valid in the future.As a bonus, the check for synthesized Exit Reason in
tdx_failed_vmentry()becomes unnecessary. Just drop it.Signed-off-by Xiaoyao Li <[email protected]>Reviewed-by Rick Edgecombe <[email protected]>---
Note, the checking oftdx_failed_vmentry()looks to miss the case where a realEPT_MISCONFIGhappens withfailed_vmentrybeing set. First, in practice,EPT_MISCONFIGcannot happen withfailed_vmentrybeing set. Second, even if it can, this is a pre-existing issue and the next patch can address it.Changes in v4:
- Collect R-b from Rick.Changes in v3:
- split from the patch 2 in v2.
- define a MARCO for the synthesized invalid Exit Reason.--- arch/x86/kvm/vmx/tdx.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-)arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c static __always_inline bool tdx_is_exit_reason_valid(u64 vp_enter_ret) · line 939
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +2
-0@@ -935,13 +935,21 @@ static __always_inline bool tdx_is_exit_reason_valid(u64 vp_enter_ret) } } +/* Synthesized invalid Exit Reason */ +#define TDX_INVALID_EXIT_REASON U16_MAX
I very strongly prefer exposing VMX's magic "0xdead", i.e. not having two different magic values when the exit reason is undefined. I'll post a standalone patch, and fixup this series when applying.
x86/include/asm/vmx.h +8 -0
@@ -20,6 +20,14 @@#include <asm/trapnr.h> #include <asm/vmxfeatures.h> +/* +* UNDEFINED is a KVM-defined value used to indicate that the basic exit reason +* is undefined/invalid, e.g. on VM-Fail or if TDX never attempted VM-Enter. +* The value itself is arbitrary, it just needs to not collide with a real exit +* reason. +*/ +#define EXIT_REASON_UNDEFINED 0xdead +struct vmcs_hdr {u32 revision_id:31 ;u32 shadow_vmcs:1 ;
x86/kvm/vmx/vmx.c +3 -1
@@ -6388,6 +6388,8 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = {static const int kvm_vmx_max_exit_handlers =ARRAY_SIZE (kvm_vmx_exit_handlers); +static_assert (EXIT_REASON_UNDEFINED >=ARRAY_SIZE (kvm_vmx_exit_handlers)); +void vmx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason,u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) {
@@ -7474,7 +7476,7 @@ static noinstr void vmx_vcpu_enter_exit(struct kvm_vcpu *vcpu,vmx_enable_fb_clear (vmx);if (unlikely (vmx->fail)) { - vmx->vt.exit_reason.full =0xdead ; + vmx->vt.exit_reason.full =EXIT_REASON_UNDEFINED ; goto out; }
PATCH v4 4/9 KVM: TDX: Don't assume exit_reason[31:16] is all-0 in tdx_to_vmx_exit_reason()
In reply to Xiaoyao LiWhen handling the real Exit Reason, don't assume the upper 16 bits as all-0 in tdx_to_vmx_exit_reason(), in preparation for enabling Bus Lock VM exit.
When Bus Lock VM exit is enabled, the bit 26 of Exit Reason becomes valid and it can be 1 with various exit reasons. Change the logic in tdx_to_vmx_exit_reason() to check the 'basic' Exit Reason for correctness. Also preserve bit[31:16] when changing the (basic) Exit Reason, to not lose the information in bit[31:16].
Change the return type of tdx_to_vmx_exit_reason() to "union vmx_exit_reason" for the convenience of manipulating the basic field.
---
Changes in v3:
- new patch split from patch 2 of v2.
--- arch/x86/kvm/vmx/tdx.c | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-)
arch/x86/kvm/vmx/tdx.c +14 -10
@@ -938,10 +938,10 @@ static __always_inline bool tdx_is_exit_reason_valid(u64 vp_enter_ret)/* Synthesized invalid Exit Reason */ #define TDX_INVALID_EXIT_REASON U16_MAX -static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) +static __always_inline union vmx_exit_reason tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu); -u32 exit_reason; +union vmx_exit_reason exit_reason;/* * Return the synthesized invalid Exit Reason, as the TDX module
@@ -949,22 +949,26 @@ static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu)* but this is NOT a failed VM-Enter. */ if (!tdx_is_exit_reason_valid (tdx->vp_enter_ret)) -return TDX_INVALID_EXIT_REASON ; +return (union vmx_exit_reason ) { + .basic =TDX_INVALID_EXIT_REASON , + }; - exit_reason = tdx->vp_enter_ret; + exit_reason.full = (u32 )tdx->vp_enter_ret; -switch (exit_reason) { +switch (exit_reason.basic) {case EXIT_REASON_TDCALL :if (tdvmcall_exit_type (vcpu)) -return EXIT_REASON_VMCALL ; - -return tdcall_to_vmx_exit_reason (vcpu); + exit_reason.basic =EXIT_REASON_VMCALL ; +else + exit_reason.basic =tdcall_to_vmx_exit_reason (vcpu); +break ;case EXIT_REASON_EPT_MISCONFIG :/* * Defer KVM_BUG_ON() until tdx_handle_exit() because this is in * non-instrumentable code with interrupts disabled. */ -return TDX_INVALID_EXIT_REASON ; + exit_reason.basic =TDX_INVALID_EXIT_REASON ; +break ;default :break ; }
@@ -981,7 +985,7 @@ static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu) tdx->vp_enter_ret =tdh_vp_enter (&tdx->vp, &tdx->vp_enter_args); - vt->exit_reason.full =tdx_to_vmx_exit_reason (vcpu); + vt->exit_reason =tdx_to_vmx_exit_reason (vcpu); vt->exit_qualification = tdx->vp_enter_args.rcx; tdx->ext_exit_qualification = tdx->vp_enter_args.rdx;
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v4 4/9] KVM: TDX: Don't assume exit_reason[31:16] is all-0 in tdx_to_vmx_exit_reason()
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [Medium] Strict equality checks on the raw 64-bit vp_enter_ret fail when Bus Lock VM exit sets bit 26, bypassing the intended KVM_BUG_ON() and fastpath logic. --
--- Patch [4]: [PATCH v4 4/9] KVM: TDX: Don't assume exit_reason[31:16] is all-0 in tdx_to_vmx_exit_reason() --- commit 48df7e5971adbfae6378e9f4d893dc4fe8eb125f
Author: Xiaoyao Li <[email protected]>
KVM: TDX: Don't assume exit_reason[31:16] is all-0 in tdx_to_vmx_exit_reason()
This commit changes tdx_to_vmx_exit_reason() to handle the upper 16 bits of the exit reason instead of assuming they are all zero. It changes the return type to union vmx_exit_reason to make manipulating the basic field more convenient while preserving the upper bits for when Bus Lock VM exit is enabled.
Quoted 4 lines
arch/x86/kvm/vmx/tdx.c +0
-0
[ ... ]
Quoted 21 lines
arch/x86/kvm/vmx/tdx.c +7
-6@@ -949,22 +949,26 @@ static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu)* but this is NOT a failed VM-Enter. */ if (!tdx_is_exit_reason_valid (tdx->vp_enter_ret)) -return TDX_INVALID_EXIT_REASON ; +return (union vmx_exit_reason ) { + .basic =TDX_INVALID_EXIT_REASON , + }; - exit_reason = tdx->vp_enter_ret; + exit_reason.full = (u32 )tdx->vp_enter_ret; -switch (exit_reason) { +switch (exit_reason.basic) {case EXIT_REASON_TDCALL :if (tdvmcall_exit_type (vcpu)) -return EXIT_REASON_VMCALL ; - -return tdcall_to_vmx_exit_reason (vcpu); + exit_reason.basic =EXIT_REASON_VMCALL ; +else
arch/x86/kvm/vmx/tdx.c static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) · line 971
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +4
-1+ exit_reason.basic =tdcall_to_vmx_exit_reason (vcpu); +break ;case EXIT_REASON_EPT_MISCONFIG :/* * Defer KVM_BUG_ON() until tdx_handle_exit() because this is in * non-instrumentable code with interrupts disabled. */ -return TDX_INVALID_EXIT_REASON ; + exit_reason.basic =TDX_INVALID_EXIT_REASON ; +break ;
[Severity: Medium]
This is a pre-existing issue, but since we are preparing for Bus Lock VM exits which can set bit 26 in the exit reason, will the deferred checks in other functions still work correctly?
Looking at tdx_handle_exit() in arch/x86/kvm/vmx/tdx.c, it checks the raw vp_enter_ret directly against EXIT_REASON_EPT_MISCONFIG:
if (unlikely(vp_enter_ret == EXIT_REASON_EPT_MISCONFIG)) {
KVM_BUG_ON(1, vcpu->kvm);
return -EIO;
}Similarly, tdx_vcpu_run() also performs a strict equality check:
if (unlikely(tdx->vp_enter_ret == EXIT_REASON_EPT_MISCONFIG)) return EXIT_FASTPATH_NONE;
If a bus lock VM exit occurs concurrently with an EPT misconfig, setting bit 26 in vp_enter_ret, won't these strict equality checks fail and bypass the intended bug-handling paths? Should these checks be updated to mask the upper bits or use the basic exit reason before enabling Bus Lock VM exit?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v4 5/9 KVM: TDX: Update exit_reason on wait_for_sept_zap return
In reply to Xiaoyao LiUpdate exit_reason to a value with bit[31:16] cleared and bit[15:0] set to TDX_INVALID_EXIT_REASON when it needs to return early due to wait_for_sept_zap in tdx_vcpu_run(). This avoids the stale exit_reason of the previous Exit being consumed twice.
---
Changes in v4:
- new patch;
--- arch/x86/kvm/vmx/tdx.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/tdx.c +14 -1
@@ -1088,8 +1088,21 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)* allowing vCPU entry to avoid contention with tdh_vp_enter() and * TDCALLs. */ -if (unlikely (READ_ONCE (to_kvm_tdx (vcpu->kvm)->wait_for_sept_zap))) +if (unlikely (READ_ONCE (to_kvm_tdx (vcpu->kvm)->wait_for_sept_zap))) { +/* +* The vCPU never entered the guest, but this looks like a +* handled exit to the caller. Synthesize an invalid exit +* reason so the previous exit's stale value isn't consumed +* a second time. +* +* Make it super clear that bit[31:16] is cleared to 0 and only +* basic exit reason (bit[15:0]) is set to the synthesized +* invalid exit reason. +*/ + vt->exit_reason.full =0 ; + vt->exit_reason.basic =TDX_INVALID_EXIT_REASON ;return EXIT_FASTPATH_EXIT_HANDLED ; + }trace_kvm_entry (vcpu, run_flags &KVM_RUN_FORCE_IMMEDIATE_EXIT );
Signature · 1 line
2.43.0
Edgecombe, Rick P
maintainerRe: [PATCH v4 5/9] KVM: TDX: Update exit_reason on wait_for_sept_zap return
Quoted 4 lines · Xiaoyao Li
Update
exit_reasonto a value with bit[31:16] cleared and bit[15:0] set toTDX_INVALID_EXIT_REASONwhen it needs to return early due towait_for_sept_zapin tdx_vcpu_run(). This avoids the staleexit_reasonof the previous Exit being consumed twice.
Would be nice to describe a bit more on the failure mode.
Quoted 9 lines
Fixes 4b2abc49712b ("KVM: TDX: Kick off vCPUs when SEAMCALL is busy during TDpage removal")
Signed-off-by Xiaoyao Li <[email protected]>---
Changes in v4:
- new patch;
---
Code seems much clearer to me than the single bit clear.
PATCH v4 6/9 KVM: VMX: Preserve negative return value in vmx_handle_exit() with bus lock detected
In reply to Xiaoyao LiPreserve the negative return value from __vmx_handle_exit() when a bus lock is detected, instead of always overwriting it with 0.
The purpose of bus_lock_detected handling is to force a userspace exit to inform userspace that a bus lock happened. The negative return value can achieve this purpose, and changing the negative value to 0 fails to return an error to userspace. So, preserve the negative return value.
---
I'm not sure on the Closes: link, since Sashiko didn't find the VMX issue directly.
Changes in v4
- grabbed from https://lore.kernel.org/all/[email protected]/
--- arch/x86/kvm/vmx/vmx.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx/vmx.c +3 -2
@@ -6860,11 +6860,12 @@ int vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)* a bus lock in guest. */ if (vmx_get_exit_reason (vcpu).bus_lock_detected) { -if (ret >0 ) +if (ret >0 ) { vcpu->run->exit_reason =KVM_EXIT_X86_BUS_LOCK ; + ret =0 ; + } vcpu->run->flags |=KVM_RUN_X86_BUS_LOCK ; -return 0 ; }return ret; }
Signature · 1 line
2.43.0
PATCH v4 7/9 KVM: VMX: Make handle_bus_lock_vmexit() a shared helper
In reply to Xiaoyao LiMove handle_bus_lock_vmexit() from vmx.c to main.c to make it a shared helper so that TDX can use it for Bus Lock VM exit handling as well. Rename it to add vt_ prefix to reflect that it is a helper for both VMX and TDX.
---
Changes in v4:
- split from next patch
--- arch/x86/kvm/vmx/common.h | 1 + arch/x86/kvm/vmx/main.c | 11 +++++++++++ arch/x86/kvm/vmx/vmx.c | 13 +------------ 3 files changed, 13 insertions(+), 12 deletions(-)
arch/x86/kvm/vmx/common.h +1 -0
@@ -203,5 +203,6 @@ static inline int __vt_handle_notify(struct kvm_vcpu *vcpu, } noinstr void vmx_handle_nmi(struct kvm_vcpu *vcpu); +int vt_handle_bus_lock_vmexit (struct kvm_vcpu *vcpu);#endif /* __KVM_X86_VMX_COMMON_H */
arch/x86/kvm/vmx/main.c +11 -0
@@ -876,6 +876,17 @@ static int vt_gmem_max_mapping_level(struct kvm *kvm, kvm_pfn_t pfn,#define vt_op_tdx_only (name) NULL#endif /* CONFIG_KVM_INTEL_TDX */ +int vt_handle_bus_lock_vmexit (struct kvm_vcpu *vcpu) +{ +/* +* Hardware may or may not set the BUS_LOCK_DETECTED flag on BUS_LOCK +* VM-Exits. Unconditionally set the flag here and leave the handling +* to .handle_exit() callback. +*/ +to_vt (vcpu)->exit_reason.bus_lock_detected = true; +return 1 ; +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLED) | \ BIT(APICV_INHIBIT_REASON_ABSENT) | \
arch/x86/kvm/vmx/vmx.c +1 -12
@@ -6265,17 +6265,6 @@ static int handle_encls(struct kvm_vcpu *vcpu) }#endif /* CONFIG_X86_SGX_KVM */ -static int handle_bus_lock_vmexit (struct kvm_vcpu *vcpu) -{ -/* -* Hardware may or may not set the BUS_LOCK_DETECTED flag on BUS_LOCK -* VM-Exits. Unconditionally set the flag here and leave the handling to -* vmx_handle_exit(). -*/ -to_vt (vcpu)->exit_reason.bus_lock_detected = true; -return 1 ; -} -static int handle_notify (struct kvm_vcpu *vcpu) {unsigned long exit_qual =vmx_get_exit_qual (vcpu);
@@ -6364,7 +6353,7 @@ static int (*kvm_vmx_exit_handlers[])(struct kvm_vcpu *vcpu) = { [EXIT_REASON_VMFUNC] = handle_vmx_instruction, [EXIT_REASON_PREEMPTION_TIMER] = handle_preemption_timer, [EXIT_REASON_ENCLS] = handle_encls, - [EXIT_REASON_BUS_LOCK] = handle_bus_lock_vmexit, + [EXIT_REASON_BUS_LOCK] = vt_handle_bus_lock_vmexit, [EXIT_REASON_NOTIFY] = handle_notify, [EXIT_REASON_SEAMCALL] = handle_tdx_instruction, [EXIT_REASON_TDCALL] = handle_tdx_instruction,
Signature · 1 line
2.43.0
Re: [PATCH v4 7/9] KVM: VMX: Make handle_bus_lock_vmexit() a shared helper
Quoted 7 lines · Xiaoyao Li
Move
handle_bus_lock_vmexit()fromvmx.ctomain.cto make it a shared helper so that TDX can use it for Bus Lock VM exit handling as well. Rename it to addvt_prefix to reflect that it is a helper for both VMX and TDX.Signed-off-by Xiaoyao Li <[email protected]>
PATCH v4 8/9 KVM: TDX: Enable Bus Lock VM exit
In reply to Xiaoyao LiEnable Bus Lock VM exit functionality for TDX guests.
Bus Lock VM exit is an existing feature supported by KVM. Userspace can enable Bus Lock VM exit through KVM_BUS_LOCK_DETECTION_EXIT when it's reported as supported. However, KVM reports the support of this CAP just based on the hardware capability but doesn't differentiate between VMX and TDX. This leads to the issue that userspace can enable this cap for TDX guests without getting an error, but the feature is not actually enabled because KVM doesn't call the TDX module API to program the relevant TD VMCS fields.
Enable Bus Lock VM exit for TDX guests by programming the BUS_LOCK_DETECTION control in the TD VMCS and by adding the exit handler.
Note, there is no enumeration bit for this feature by TDX module because all TDX modules support it and allow to set the TD VMCS as long as the hardware supports the feature.
---
Changes in 4:
- The code to make handle_bus_lock_vmexit() a shared helper is split as a separate patch.
- The handling for wait_for_sept_zap is no longer needed since a general handling for it is added as a separate patch.
Changes in v3:
- Refine the changelog. (Rick)
Changes in v2:
- Don't overwrite the negative return value to 0. (Sashiko)
- Clear the bus_lock_detected bit when it returns early for wait_for_sept_zap case.
- Add a note to clarify the feature is always supported by the TDX module, to make Sashiko happy.
--- arch/x86/kvm/vmx/tdx.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
arch/x86/kvm/vmx/tdx.c +23 -1
@@ -2058,7 +2058,7 @@ int tdx_complete_emulated_msr(struct kvm_vcpu *vcpu, int err) } -int tdx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t fastpath) +static int __tdx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t fastpath) {struct vcpu_tdx *tdx =to_tdx (vcpu);u64 vp_enter_ret = tdx->vp_enter_ret;
@@ -2159,6 +2159,8 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)case EXIT_REASON_NOTIFY :/* NMI blocking state is handled by TDX module */ return __vt_handle_notify (vcpu,vmx_get_exit_qual (vcpu)); +case EXIT_REASON_BUS_LOCK : +return vt_handle_bus_lock_vmexit (vcpu);default :break ; }
@@ -2168,6 +2170,22 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)return 0 ; } +int tdx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t fastpath) +{ +int ret =__tdx_handle_exit (vcpu, fastpath); + +/* Exit to user space when bus lock was detected */ +if (vmx_get_exit_reason (vcpu).bus_lock_detected) { +if (ret >0 ) { + vcpu->run->exit_reason =KVM_EXIT_X86_BUS_LOCK ; + ret =0 ; + } + + vcpu->run->flags |=KVM_RUN_X86_BUS_LOCK ; + } +return ret; +} +void tdx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason,u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) {
@@ -3194,6 +3212,10 @@ static int tdx_vcpu_init(struct kvm_vcpu *vcpu, struct kvm_tdx_cmd *cmd) vcpu->kvm->arch.notify_window); } +if (vcpu->kvm->arch.bus_lock_detection_enabled) +td_vmcs_setbit32 (tdx,SECONDARY_VM_EXEC_CONTROL , +SECONDARY_EXEC_BUS_LOCK_DETECTION ); + tdx->state =VCPU_TD_STATE_INITIALIZED ;return 0 ;
Signature · 1 line
2.43.0
Re: [PATCH v4 8/9] KVM: TDX: Enable Bus Lock VM exit
Quoted 32 lines · Xiaoyao Li
Enable Bus Lock VM exit functionality for TDX guests.
Bus Lock VM exit is an existing feature supported by KVM. Userspace can enable Bus Lock VM exit through
KVM_BUS_LOCK_DETECTION_EXITwhen it's reported as supported. However, KVM reports the support of this CAP just based on the hardware capability but doesn't differentiate between VMX and TDX. This leads to the issue that userspace can enable this cap for TDX guests without getting an error, but the feature is not actually enabled because KVM doesn't call the TDX module API to program the relevant TD VMCS fields.Enable Bus Lock VM exit for TDX guests by programming the
BUS_LOCK_DETECTIONcontrol in the TD VMCS and by adding the exit handler.Note, there is no enumeration bit for this feature by TDX module because all TDX modules support it and allow to set the TD VMCS as long as the hardware supports the feature.
Fixes 161d34609f9b ("KVM: TDX: Make TDX VM type supported")Originally-by Chenyi Qiang <[email protected]>Signed-off-by Xiaoyao Li <[email protected]>---
Changes in 4:
- The code to makehandle_bus_lock_vmexit()a shared helper is split as a separate patch.
- The handling forwait_for_sept_zapis no longer needed since a general handling for it is added as a separate patch.Changes in v3:
- Refine the changelog. (Rick)
Quoted 6 lines
Changes in v2:
- Don't overwrite the negative return value to 0. (Sashiko)
- Clear thebus_lock_detectedbit when it returns early forwait_for_sept_zapcase.
- Add a note to clarify the feature is always supported by the TDX module, to make Sashiko happy.
PATCH v4 9/9 KVM: VMX: Consolidate the exit handler for VMX and TDX
In reply to Xiaoyao LiThe exit handlers for VMX and TDX have the similar pattern. Consolidate them into a single vt_handle_exit() helper.
Changes in v4:
- new patch.
--- arch/x86/kvm/vmx/main.c | 38 ++++++++++++++++++++++++++++---------- arch/x86/kvm/vmx/tdx.c | 18 +----------------- arch/x86/kvm/vmx/vmx.c | 21 +-------------------- 3 files changed, 30 insertions(+), 47 deletions(-)
arch/x86/kvm/vmx/main.c +28 -10
@@ -154,15 +154,6 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)return vmx_vcpu_run (vcpu, run_flags); } -static int vt_handle_exit (struct kvm_vcpu *vcpu, -enum exit_fastpath_completion fastpath) -{ -if (is_td_vcpu (vcpu)) -return tdx_handle_exit (vcpu, fastpath); - -return vmx_handle_exit (vcpu, fastpath); -} -static bool vt_unhandleable_emulation_required (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu)) {
@@ -887,6 +878,33 @@ int vt_handle_bus_lock_vmexit(struct kvm_vcpu *vcpu)return 1 ; } +static int vt_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath) +{ +int ret; + +#ifdef CONFIG_KVM_INTEL_TDX +if (is_td_vcpu (vcpu)) + ret =tdx_handle_exit (vcpu, fastpath); + else +#endif + ret =vmx_handle_exit (vcpu, fastpath); + +/* +* Exit to user space when bus lock detected to inform that there is +* a bus lock in guest. +*/ +if (vmx_get_exit_reason (vcpu).bus_lock_detected) { +if (ret >0 ) { + vcpu->run->exit_reason =KVM_EXIT_X86_BUS_LOCK ; + ret =0 ; + } + + vcpu->run->flags |=KVM_RUN_X86_BUS_LOCK ; + } +return ret; +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLED) | \ BIT(APICV_INHIBIT_REASON_ABSENT) | \
@@ -960,7 +978,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_needs_initialization = vt_op_tdx_only(vcpu_needs_initialization), .vcpu_run = vt_op(vcpu_run), - .handle_exit = vt_op(handle_exit), + .handle_exit = vt_handle_exit, .skip_emulated_instruction = vmx_skip_emulated_instruction, .update_emulated_instruction = vmx_update_emulated_instruction, .unhandleable_emulation_required = vt_op(unhandleable_emulation_required),
arch/x86/kvm/vmx/tdx.c +1 -17
@@ -2058,7 +2058,7 @@ int tdx_complete_emulated_msr(struct kvm_vcpu *vcpu, int err) } -static int __tdx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t fastpath) +int tdx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t fastpath) {struct vcpu_tdx *tdx =to_tdx (vcpu);u64 vp_enter_ret = tdx->vp_enter_ret;
@@ -2170,22 +2170,6 @@ static int __tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)return 0 ; } -int tdx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t fastpath) -{ -int ret =__tdx_handle_exit (vcpu, fastpath); - -/* Exit to user space when bus lock was detected */ -if (vmx_get_exit_reason (vcpu).bus_lock_detected) { -if (ret >0 ) { - vcpu->run->exit_reason =KVM_EXIT_X86_BUS_LOCK ; - ret =0 ; - } - - vcpu->run->flags |=KVM_RUN_X86_BUS_LOCK ; - } -return ret; -} -void tdx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason,u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) {
arch/x86/kvm/vmx/vmx.c +1 -20
@@ -6681,7 +6681,7 @@ void dump_vmcs(struct kvm_vcpu *vcpu)* The guest has exited. See if we can fix it or if we need userspace * assistance. */ -static int __vmx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t exit_fastpath) +int vmx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t exit_fastpath) {struct vcpu_vmx *vmx =to_vmx (vcpu);union vmx_exit_reason exit_reason =vmx_get_exit_reason (vcpu);
@@ -6840,25 +6840,6 @@ static int __vmx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)return 0 ; } -int vmx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t exit_fastpath) -{ -int ret =__vmx_handle_exit (vcpu, exit_fastpath); - -/* -* Exit to user space when bus lock detected to inform that there is -* a bus lock in guest. -*/ -if (vmx_get_exit_reason (vcpu).bus_lock_detected) { -if (ret >0 ) { - vcpu->run->exit_reason =KVM_EXIT_X86_BUS_LOCK ; - ret =0 ; - } - - vcpu->run->flags |=KVM_RUN_X86_BUS_LOCK ; - } -return ret; -} -void vmx_update_cr8_intercept (struct kvm_vcpu *vcpu,int tpr,int irr) {int tpr_threshold;
Signature · 1 line
2.43.0
Re: [PATCH v4 9/9] KVM: VMX: Consolidate the exit handler for VMX and TDX
Quoted 6 lines · Xiaoyao Li
The exit handlers for VMX and TDX have the similar pattern. Consolidate them into a single
vt_handle_exit()helper.Signed-off-by Xiaoyao Li <[email protected]>Changes in v4:
- new patch.
I created essentially the the same code locally after I read the previous patch as the code duplication was killing!
Re: [PATCH v4 9/9] KVM: VMX: Consolidate the exit handler for VMX and TDX
In reply to Xiaoyao LiQuoted 67 lines · Xiaoyao Li
The exit handlers for VMX and TDX have the similar pattern. Consolidate them into a single
vt_handle_exit()helper.Signed-off-by Xiaoyao Li <[email protected]>Changes in v4:
- new patch.--- arch/x86/kvm/vmx/main.c | 38 ++++++++++++++++++++++++++++---------- arch/x86/kvm/vmx/tdx.c | 18 +----------------- arch/x86/kvm/vmx/vmx.c | 21 +-------------------- 3 files changed, 30 insertions(+), 47 deletions(-)arch/x86/kvm/vmx/main.c +27
-9@@ -154,15 +154,6 @@ static fastpath_t vt_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)return vmx_vcpu_run (vcpu, run_flags); } -static int vt_handle_exit (struct kvm_vcpu *vcpu, -enum exit_fastpath_completion fastpath) -{ -if (is_td_vcpu (vcpu)) -return tdx_handle_exit (vcpu, fastpath); - -return vmx_handle_exit (vcpu, fastpath); -} -static bool vt_unhandleable_emulation_required (struct kvm_vcpu *vcpu) {if (is_td_vcpu (vcpu)) {@@ -887,6 +878,33 @@ int vt_handle_bus_lock_vmexit(struct kvm_vcpu *vcpu)return 1 ; } +static int vt_handle_exit (struct kvm_vcpu *vcpu, +enum exit_fastpath_completion fastpath) +{ +int ret; + +#ifdef CONFIG_KVM_INTEL_TDX +if (is_td_vcpu (vcpu)) + ret =tdx_handle_exit (vcpu, fastpath); + else +#endif + ret =vmx_handle_exit (vcpu, fastpath); + +/* +* Exit to user space when bus lock detected to inform that there is +* a bus lock in guest. +*/ +if (vmx_get_exit_reason (vcpu).bus_lock_detected) { +if (ret >0 ) { + vcpu->run->exit_reason =KVM_EXIT_X86_BUS_LOCK ; + ret =0 ; + } + + vcpu->run->flags |=KVM_RUN_X86_BUS_LOCK ; + } +return ret; +} +#define VMX_REQUIRED_APICV_INHIBITS \ (BIT(APICV_INHIBIT_REASON_DISABLED) | \ BIT(APICV_INHIBIT_REASON_ABSENT) | \
arch/x86/kvm/vmx/main.c struct kvm_x86_ops vt_x86_ops __initdata = { · line 981
Quoted 5 lines
arch/x86/kvm/vmx/main.c +1
-1@@ -960,7 +978,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_needs_initialization = vt_op_tdx_only(vcpu_needs_initialization), .vcpu_run = vt_op(vcpu_run), - .handle_exit = vt_op(handle_exit), + .handle_exit = vt_handle_exit,
nit: Why isn't this vt_op() for the sake of consistency and introduce needless churn ?
<snip>
Re: [PATCH v4 9/9] KVM: VMX: Consolidate the exit handler for VMX and TDX
Quoted 7 lines · Nikolay Borisov
arch/x86/kvm/vmx/main.c struct kvm_x86_ops vt_x86_ops __initdata = { · line 980
Quoted 5 lines
arch/x86/kvm/vmx/main.c +1
-1@@ -960,7 +978,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_needs_initialization = vt_op_tdx_only(vcpu_needs_initialization), .vcpu_run = vt_op(vcpu_run), - .handle_exit = vt_op(handle_exit), + .handle_exit = vt_handle_exit,nit: Why isn't this
vt_op()for the sake of consistency and introduce needless churn ?
I didn't see how to make vt_op() still work here while dedupping the code. Do you have a better idea?
Re: [PATCH v4 9/9] KVM: VMX: Consolidate the exit handler for VMX and TDX
Quoted 10 lines · Xiaoyao Li
Quoted 7 lines · Nikolay Borisov
arch/x86/kvm/vmx/main.c struct kvm_x86_ops vt_x86_ops __initdata = { · line 980
Quoted 5 lines
arch/x86/kvm/vmx/main.c +1
-1@@ -960,7 +978,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = { .vcpu_needs_initialization = vt_op_tdx_only(vcpu_needs_initialization), .vcpu_run = vt_op(vcpu_run), - .handle_exit = vt_op(handle_exit), + .handle_exit = vt_handle_exit,nit: Why isn't this
vt_op()for the sake of consistency and introduce needless churn ?I didn't see how to make
vt_op()still work here while dedupping the code. Do you have a better idea?
Indeed, I saw that vt_op is actually different for the TDX/!TDX case. I guess that function is always bound to have some quirk - i.e it has an #ifdef guard inside even though is_td_vcpu() is defined for the !TDX case but tdx_handle_exit() isn't. It's named VT, even though it's defined in the "vmx section" ...
Oh well... My RB still stands.
Sean Christopherson
maintainerRe: [PATCH v4 9/9] KVM: VMX: Consolidate the exit handler for VMX and TDX
Quoted 14 lines · Nikolay Borisov
Quoted 11 lines · Xiaoyao Li
Quoted 9 lines · Nikolay Borisov
Quoted 6 lines
(quoted diff) +0
-0@@ -960,7 +978,7 @@ struct kvm_x86_ops vt_x86_ops __initdata = {.vcpu_needs_initialization =
vt_op_tdx_only(vcpu_needs_initialization),
.vcpu_run =vt_op(vcpu_run),
- .handle_exit =vt_op(handle_exit),
+ .handle_exit =vt_handle_exit,nit: Why isn't this
vt_op()for the sake of consistency and introduce needless churn ?
Quoted 8 lines
Quoted 2 lines
I didn't see how to make
vt_op()still work here while dedupping the code. Do you have a better idea?Indeed, I saw that
vt_opis actually different for the TDX/!TDX case. I guess that function is always bound to have some quirk - i.e it has an #ifdef guard inside even thoughis_td_vcpu()is defined for the !TDX case buttdx_handle_exit()isn't. It's named VT, even though it's defined in the "vmx section" ...
Because the name and the implementation are two separate things. The scope is "vt_" to communicate that the operation is common to all flavors of VT, i.e. to both VMX and TDX. But when support for TDX is disabled, there's no need to trampoline through the routing code and so the vt_op() implementation wires the hooks up directly to the VMX functions.
.handle_exit doesn't use vt_op() because the implementation of vt_handle_exit() itself is completely common to VMX and TDX, i.e. there is no routing code and thus no need to define a wrapper via vt_op().
Edgecombe, Rick P
maintainerQuoted 4 lines · Xiaoyao Li
Patch 6 itself is a fix for VMX and is OK for cc stable though maybe not necessary. Patch 6 is added to this series since it can help stop Sashiko repeating its finding of VMX's pre-existing issue, and it's also necessary for patch 9 to consilidate the exit handler for VMX and TDX.
To me this is not a valid reason. Sashiko is great, but we can't let false positives drive the patches. Given that the series is so big now, I'd think it would be better to leave 6 and 9 for follow up, so we can focus on the core thing.
Sean Christopherson
maintainerQuoted 10 lines · Rick P Edgecombe
Quoted 4 lines · Xiaoyao Li
Patch 6 itself is a fix for VMX and is OK for cc stable though maybe not necessary. Patch 6 is added to this series since it can help stop Sashiko repeating its finding of VMX's pre-existing issue, and it's also necessary for patch 9 to consilidate the exit handler for VMX and TDX.
To me this is not a valid reason. Sashiko is great, but we can't let false positives drive the patches. Given that the series is so big now, I'd think it would be better to leave 6 and 9 for follow up, so we can focus on the core thing.
Hmm, I disagree. If the consolidation weren't here, I'd absolutely ask for it. This is new feature enabling. Yeah, it happens to be tagged for stable, but at the end of the day, it's new feature enabling. And it's standard operation procedure to do cleanups and dedup code as part of new feature enabling.
If anything patch 6 should be patch 1, but that's a minor detail I can sort out when applying (assuming another version isn't required).
Edgecombe, Rick P
maintainerQuoted 5 lines · Sean Christopherson
Quoted 4 lines
To me this is not a valid reason. Sashiko is great, but we can't let false positives drive the patches. Given that the series is so big now, I'd think it would be better to leave 6 and 9 for follow up, so we can focus on the core thing.
Quoted 8 lines
Hmm, I disagree. If the consolidation weren't here, I'd absolutely ask for it. This is new feature enabling. Yeah, it happens to be tagged for stable, but at the end of the day, it's new feature enabling. And it's standard operation procedure to do cleanups and dedup code as part of new feature enabling.
If anything patch 6 should be patch 1, but that's a minor detail I can sort out when applying (assuming another version isn't required).
Ok. But you disagree with dropping the patch? Or that avoiding sashiko reports of existing issues is an invalid reason to change the series?
I think it's good to discuss a bit how to handle sashiko scenarios.
Sean Christopherson
maintainerQuoted 12 lines · Rick P Edgecombe
Quoted 11 lines · Sean Christopherson
Quoted 4 lines
To me this is not a valid reason. Sashiko is great, but we can't let false positives drive the patches. Given that the series is so big now, I'd think it would be better to leave 6 and 9 for follow up, so we can focus on the core thing.
Hmm, I disagree. If the consolidation weren't here, I'd absolutely ask for it. This is new feature enabling. Yeah, it happens to be tagged for stable, but at the end of the day, it's new feature enabling. And it's standard operation procedure to do cleanups and dedup code as part of new feature enabling.
Quoted 4 lines
Quoted 2 lines
If anything patch 6 should be patch 1, but that's a minor detail I can sort out when applying (assuming another version isn't required).
Ok. But you disagree with dropping the patch?
Dropping the patch.
Quoted 4 lines
Or that avoiding sashiko reports of existing issues is an invalid reason to change the series?
I think it's good to discuss a bit how to handle sashiko scenarios.
Like we do any other code review: use common sense and follow established best practices. If a human reviewer pointed out an existing bug, we would analyze the situation and make a judgment call as to whether it's better to send a standalone fix or roll a fix into a new version of the series. If we decided to fix the issue separately, and then a human brought up the same pre-existing issue in a future revision, we would point them at the fix or the previous discussion (or if it was the same human, (politely?) tell them to go away).
The only differences is that Sashiko is noisier because is doesn't (yet?) remember what feedback it gave in the past, often doesn't look at the patches later in the series, and doesn't (yet?) respond to emails so telling Sashiko to shut up about a particular pre-existing issue isn't effective.
But to be very explicit: don't include a patch *purely* to suppress Sashiko's rediscovery of existing issues.
Quoted 13 lines · Xiaoyao Li
Xiaoyao Li (9):
KVM: TDX: Enable Notify VM exit
KVM: TDX: Check if there is valid exit infos based onvp_enter_ret
KVM: TDX: Set bits 31:16 to 0 for the synthesized Exit Reason
KVM: TDX: Don't assume exit_reason[31:16] is all-0 in
tdx_to_vmx_exit_reason()
KVM: TDX: Updateexit_reasononwait_for_sept_zapreturn
KVM: VMX: Preserve negative return value invmx_handle_exit()with bus
lock detected
KVM: VMX: Makehandle_bus_lock_vmexit()a shared helper
KVM: TDX: Enable Bus Lock VM exit
KVM: VMX: Consolidate the exit handler for VMX and TDX
Quoted 13 lines
arch/x86/kvm/vmx/common.h | 20 ++++++++++ arch/x86/kvm/vmx/main.c | 49 ++++++++++++++++++----- arch/x86/kvm/vmx/tdx.c | 82 ++++++++++++++++++++++++++++++--------- arch/x86/kvm/vmx/vmx.c | 46 ++-------------------- 4 files changed, 126 insertions(+), 71 deletions(-)base-commit:
1b731e5ded480bd1e5546aed35584238661ce72e--
2.43.0
Quoted 21 lines · Xiaoyao Li
Hi all,
This is v4 of the series to enable the Notify VM Exit and Bus Lock VM exit for TDX, which fixes the KVM CAP issue related with them and allow userspace to actually enable the features.
Compared to v3, this v4 adds 5 more patches. The first 8 patches target for stable kernels while only the patch 9 doesn't have to. Patch 1 is a single patch to enable Notify VM exit for TDX. Except patch 6, patch 2-7 are mandatory for enabling Bus Lock VM exit for TDX in patch 8.
Patch 6 itself is a fix for VMX and is OK for cc stable though maybe not necessary. Patch 6 is added to this series since it can help stop Sashiko repeating its finding of VMX's pre-existing issue, and it's also necessary for patch 9 to consilidate the exit handler for VMX and TDX.
There are other issues of existing code found during previous review, like the EPT MISCONFIG handling. Given they are not mandatory for enabling the Notify VM exit and Bus Lock VM exit for TDX, the plan is to address them in a follow-up series separately.
Quoted 5 lines
Please refer to v1 for a full background.
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Hi Sean,
What's your plan on this series? Can you queue it if it looks good to you? Or need me to spin a new version to address Binbin and Rick's comments on the commit message?
Thanks,
-Xiayao
Sean Christopherson
maintainerQuoted 28 lines · Xiaoyao Li
Quoted 21 lines · Xiaoyao Li
Hi all,
This is v4 of the series to enable the Notify VM Exit and Bus Lock VM exit for TDX, which fixes the KVM CAP issue related with them and allow userspace to actually enable the features.
Compared to v3, this v4 adds 5 more patches. The first 8 patches target for stable kernels while only the patch 9 doesn't have to. Patch 1 is a single patch to enable Notify VM exit for TDX. Except patch 6, patch 2-7 are mandatory for enabling Bus Lock VM exit for TDX in patch 8.
Patch 6 itself is a fix for VMX and is OK for cc stable though maybe not necessary. Patch 6 is added to this series since it can help stop Sashiko repeating its finding of VMX's pre-existing issue, and it's also necessary for patch 9 to consilidate the exit handler for VMX and TDX.
There are other issues of existing code found during previous review, like the EPT MISCONFIG handling. Given they are not mandatory for enabling the Notify VM exit and Bus Lock VM exit for TDX, the plan is to address them in a follow-up series separately.
Quoted 5 lines
Please refer to v1 for a full background.
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Quoted 5 lines
Hi Sean,
What's your plan on this series? Can you queue it if it looks good to you? Or need me to spin a new version to address Binbin and Rick's comments on the commit message?
Get it applied, with some tweaks. I.e. no v5 necessary.
Quoted 29 lines · Sean Christopherson
Quoted 28 lines · Xiaoyao Li
Quoted 26 lines · Xiaoyao Li
Hi all,
This is v4 of the series to enable the Notify VM Exit and Bus Lock VM exit for TDX, which fixes the KVM CAP issue related with them and allow userspace to actually enable the features.
Compared to v3, this v4 adds 5 more patches. The first 8 patches target for stable kernels while only the patch 9 doesn't have to. Patch 1 is a single patch to enable Notify VM exit for TDX. Except patch 6, patch 2-7 are mandatory for enabling Bus Lock VM exit for TDX in patch 8.
Patch 6 itself is a fix for VMX and is OK for cc stable though maybe not necessary. Patch 6 is added to this series since it can help stop Sashiko repeating its finding of VMX's pre-existing issue, and it's also necessary for patch 9 to consilidate the exit handler for VMX and TDX.
There are other issues of existing code found during previous review, like the EPT MISCONFIG handling. Given they are not mandatory for enabling the Notify VM exit and Bus Lock VM exit for TDX, the plan is to address them in a follow-up series separately.
Please refer to v1 for a full background.
v3: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v1: https://lore.kernel.org/all/[email protected]/
Quoted 7 lines
Quoted 5 lines
Hi Sean,
What's your plan on this series? Can you queue it if it looks good to you? Or need me to spin a new version to address Binbin and Rick's comments on the commit message?
Get it applied, with some tweaks. I.e. no v5 necessary.
Thanks!
Quoted 10 lines · Xiaoyao Li
This is v4 of the series to enable the Notify VM Exit and Bus Lock VM exit for TDX, which fixes the KVM CAP issue related with them and allow userspace to actually enable the features.
Compared to v3, this v4 adds 5 more patches. The first 8 patches target for stable kernels while only the patch 9 doesn't have to. Patch 1 is a single patch to enable Notify VM exit for TDX. Except patch 6, patch 2-7 are mandatory for enabling Bus Lock VM exit for TDX in patch 8.
[...]
Applied to kvm-x86 vmx, thanks!
[1/9] KVM: TDX: Enable Notify VM exit
https://github.com/kvm-x86/linux/commit/d93395058c19 [2/9] KVM: TDX: Check if there is valid exit infos based on vp_enter_ret https://github.com/kvm-x86/linux/commit/0966c881ea13 [3/9] KVM: TDX: Set bits 31:16 to 0 for the synthesized Exit Reason https://github.com/kvm-x86/linux/commit/b43c520b198a [4/9] KVM: TDX: Don't assume exit_reason[31:16] is all-0 in tdx_to_vmx_exit_reason() https://github.com/kvm-x86/linux/commit/bc335ce04408 [5/9] KVM: TDX: Update exit_reason on wait_for_sept_zap return https://github.com/kvm-x86/linux/commit/e5f1d62c59ae [6/9] KVM: VMX: Preserve negative return value in vmx_handle_exit() with bus lock detected https://github.com/kvm-x86/linux/commit/cef48693da1e [7/9] KVM: VMX: Make handle_bus_lock_vmexit() a shared helper https://github.com/kvm-x86/linux/commit/326fa2c9b4d3 [8/9] KVM: TDX: Enable Bus Lock VM exit https://github.com/kvm-x86/linux/commit/63d196e2e204 [9/9] KVM: VMX: Consolidate the exit handler for VMX and TDX https://github.com/kvm-x86/linux/commit/213bbf18f07a