KVM: TDX: Enable VM-DoS Prevention Features for TDX
Subsystems (4)
v2 cover KVM: TDX: Enable VM-DoS Prevention Features for TDX
Hi all,
This is v2 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.
This v2 tries to keep the series as small as possible and it adds one more patch, patch 02, because it's targeted for being backported to stable kenrels. The comments on v1 that are not addressed by this series will be handled in followup series separately, such as renaming notify vm exit to instruction timout, tdx fast exit handler and vp_enter_ret handling, etc.
Please refer to v1 for a full background.
v1: https://lore.kernel.org/all/[email protected]/
Xiaoyao Li (3):
KVM: TDX: Enable Notify VM exit
KVM: TDX: Fix the exit reason handling
KVM: TDX: Enable Bus Lock VM exit
arch/x86/kvm/vmx/common.h | 19 ++++++++++ arch/x86/kvm/vmx/tdx.c | 73 +++++++++++++++++++++++++++++++-------- arch/x86/kvm/vmx/vmx.c | 15 ++------ arch/x86/kvm/vmx/vmx.h | 1 + 4 files changed, 80 insertions(+), 28 deletions(-)
Signature · 1 line
2.43.0
PATCH v2 1/3 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 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 __vmx_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 __vmx_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 __vmx_handle_notify (vcpu, exit_qual); }static int vmx_get_msr_imm_reg (struct kvm_vcpu *vcpu)
Signature · 1 line
2.43.0
Re: [PATCH v2 1/3] 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 7 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]>
PATCH v2 2/3 KVM: TDX: Fix the exit reason handling
In reply to Xiaoyao LiGet and check the exit reason from the low 16 bits of vp_enter_ret, and store the synthesized/transformed exit reason in the "basic" field.
Some bits in the upper 16 bits in the exit reason have their own meanings and they might be 1. When handling the exit reason, only do handling on the lower 16 bits and keep the upper 16 bits unchanged. This change also helps remove the additional check in tdx_failed_vmentry().
Note, due to the synthesized invalid exit reason, -1, is changed to assigned to the "basic" field, adjust the checking in tdx_get_exit_info() accordingly.
---
Changes in v2:
- new patch
--- arch/x86/kvm/vmx/tdx.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-)
arch/x86/kvm/vmx/tdx.c +22 -13
@@ -921,10 +921,10 @@ 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 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;switch (tdx->vp_enter_ret &TDX_SEAMCALL_STATUS_MASK ) {case TDX_SUCCESS :
@@ -934,23 +934,33 @@ static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu)case TDX_NON_RECOVERABLE_TD_WRONG_APIC_MODE :break ;default : -return -1u ; +/* +* Synthesize an invalid bogus 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. +*/ +return (union vmx_exit_reason ) { + .basic =-1 , + }; } - 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 -1u ; +return (union vmx_exit_reason ) { + .basic =-1 , + };default :break ; }
@@ -967,7 +977,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;
@@ -981,8 +991,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)
@@ -2144,7 +2153,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->vt.exit_reason.basic !=-1 ) { *info1 =vmx_get_exit_qual (vcpu); *info2 = tdx->ext_exit_qualification; *intr_info =vmx_get_intr_info (vcpu);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] The condition `tdx->vt.exit_reason.basic != -1` in `tdx_get_exit_info()` is unconditionally true…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] The condition tdx->vt.exit_reason.basic != -1 in tdx_get_exit_info() is unconditionally true due to C integer promotion rules, defeating the check for synthesized invalid exit reasons and leaking uninitialized or host state to the kvm_exit tracepoint. --
--- Patch [2]: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling --- commit d21d0519253cbb288be7d1030cb7a0bb7547067a
Author: Xiaoyao Li <[email protected]>
KVM: TDX: Fix the exit reason handling
This patch modifies how KVM handles TDX exit reasons by isolating the lower 16 bits into the basic field and keeping the upper 16 bits unchanged. It synthesizes an invalid exit reason (-1) for specific TDX SEAMCALL statuses and updates tdx_get_exit_info() to check this field.
Quoted 4 lines
arch/x86/kvm/vmx/tdx.c +0
-0
[ ... ]
arch/x86/kvm/vmx/tdx.c void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, · line 2159
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +1
-1@@ -2147,7 +2156,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->vt.exit_reason.basic !=-1 ) {
[Severity: Medium]
Will this check always evaluate to true due to C integer promotion rules?
The basic field in union vmx_exit_reason is a 16-bit unsigned bitfield. When comparing it to -1, the unsigned 16-bit value is promoted to a signed 32-bit integer. If the value was set to -1 (65535), the comparison evaluates as 65535 != -1, which is always true.
Could this cause uninitialized or host state (such as tdvpr_pa passed via rcx to the TDX module) to leak to the kvm_exit tracepoint instead of falling back and safely zeroing the exit data?
Quoted 3 lines
arch/x86/kvm/vmx/tdx.c +0
-0*info1 =vmx_get_exit_qual (vcpu); *info2 = tdx->ext_exit_qualification; *intr_info =vmx_get_intr_info (vcpu);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Re: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 9 lines · On 8/10/2026 7:39 PM, [email protected] wrote:
Review void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, · line 2159
Quoted 6 lines
(quoted diff) +1
-1@@ -2147,7 +2156,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->vt.exit_reason.basic != -1) {[Severity: Medium]
Will this check always evaluate to true due to C integer promotion rules?
Quoted 5 lines
The basic field in union
vmx_exit_reasonis a 16-bit unsigned bitfield. When comparing it to -1, the unsigned 16-bit value is promoted to a signed 32-bit integer. If the value was set to -1 (65535), the comparison evaluates as 65535 != -1, which is always true.
Well, how about something below on top of this patch?
arch/x86/kvm/vmx/tdx.c +2 -0
@@ -59,6 +59,8 @@ module_param_named(tdx, enable_tdx, bool, 0444);#define TDX_SHARED_BIT_PWL_5 gpa_to_gfn(BIT_ULL(51))#define TDX_SHARED_BIT_PWL_4 gpa_to_gfn(BIT_ULL(47)) +#define TDX_INVALID_EXIT_REASON U16_MAX +static const struct tdx_sys_info *tdx_sysinfo; void tdh_vp_rd_failed(struct vcpu_tdx *tdx, char *uclass, u32 field,
u64 err)
@@ -940,7 +942,7 @@ static __always_inline union vmx_exit_reason tdx_to_vmx_exit_reason(struct kvm_v
* undefined, but this is NOT a failed VM-Enter.
*/
return (union vmx_exit_reason) {
- .basic = -1,
+ .basic = TDX_INVALID_EXIT_REASON,
};
}@@ -959,7 +961,7 @@ static __always_inline union vmx_exit_reason tdx_to_vmx_exit_reason(struct kvm_v
* non-instrumentable code with interrupts disabled.
*/
return (union vmx_exit_reason) {
- .basic = -1,
+ .basic = TDX_INVALID_EXIT_REASON,
};
default:
break;
@@ -2153,7 +2155,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 (tdx->vt.exit_reason.basic != -1) {
+ if (tdx->vt.exit_reason.basic != TDX_INVALID_EXIT_REASON) {
*info1 = vmx_get_exit_qual(vcpu);
*info2 = tdx->ext_exit_qualification;
*intr_info = vmx_get_intr_info(vcpu);Sean Christopherson
maintainerRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 9 lines · Xiaoyao Li
Quoted 8 lines · On 8/10/2026 7:39 PM, [email protected] wrote:
Review void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, · line 2158
Quoted 5 lines
(quoted diff) +1
-1@@ -2147,7 +2156,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->vt.exit_reason.basic != -1) {[Severity: Medium]
Will this check always evaluate to true due to C integer promotion rules?
Quoted 7 lines
Quoted 5 lines
The basic field in union
vmx_exit_reasonis a 16-bit unsigned bitfield. When comparing it to -1, the unsigned 16-bit value is promoted to a signed 32-bit integer. If the value was set to -1 (65535), the comparison evaluates as 65535 != -1, which is always true.Well, how about something below on top of this patch?
No, we need to not rely on magic exit_reason.basic values. Can't this be?
if ((tdx->vp_enter_ret & TDX_SW_ERROR) != TDX_SW_ERROR) {
<read values>
} else {
<zero values>
}We'd need to use the unsafe version if we go with my suggestion[*], but other than that wrinkle, the above seems like the obviously correct fix (maybe too obvious)?
Sean Christopherson
maintainerRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Xiaoyao Li
Quoted 13 lines · On 8/10/2026 7:39 PM, [email protected] wrote:
Review void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, · line 2158
Quoted 5 lines
(quoted diff) +1
-1@@ -2147,7 +2156,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->vt.exit_reason.basic != -1) {[Severity: Medium]
Will this check always evaluate to true due to C integer promotion rules?The basic field in union
vmx_exit_reasonis a 16-bit unsigned bitfield. When comparing it to -1, the unsigned 16-bit value is promoted to a signed 32-bit integer. If the value was set to -1 (65535), the comparison evaluates as 65535 != -1, which is always true.
Quoted 5 lines
Quoted 1 line
Well, how about something below on top of this patch?
No, we need to not rely on magic exit_reason.basic values. Can't this be?
if ((tdx->vp_enter_ret & TDX_SW_ERROR) != TDX_SW_ERROR) {
This would arguably be a bug fix as well, because the "real" EXIT_REASON_EPT_MISCONFIG path gets a false negative. E.g. when getting information for an EPT Misconfig for the tracepoint, KVM really should print all information, not zeros.
At a glance, this exact change can probably be a separate patch too.
Quoted 10 lines
<read values> } else { <zero values> }We'd need to use the unsafe version if we go with my suggestion[*], but other than that wrinkle, the above seems like the obviously correct fix (maybe too obvious)?
Re: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 21 lines · Sean Christopherson
Quoted 20 lines · Sean Christopherson
Quoted 9 lines · Xiaoyao Li
Quoted 8 lines · On 8/10/2026 7:39 PM, [email protected] wrote:
Review void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, · line 2158
Quoted 5 lines
(quoted diff) +1
-1@@ -2147,7 +2156,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->vt.exit_reason.basic != -1) {[Severity: Medium]
Will this check always evaluate to true due to C integer promotion rules?Quoted 7 lines
Quoted 5 lines
The basic field in union
vmx_exit_reasonis a 16-bit unsigned bitfield. When comparing it to -1, the unsigned 16-bit value is promoted to a signed 32-bit integer. If the value was set to -1 (65535), the comparison evaluates as 65535 != -1, which is always true.Well, how about something below on top of this patch?
No, we need to not rely on magic exit_reason.basic values. Can't this be?
Quoted 7 lines
Quoted 1 line
if ((tdx->vp_enter_ret & TDX_SW_ERROR) != TDX_SW_ERROR) {This would arguably be a bug fix as well, because the "real"
EXIT_REASON_EPT_MISCONFIGpath gets a false negative. E.g. when getting information for an EPT Misconfig for the tracepoint, KVM really should print all information, not zeros.At a glance, this exact change can probably be a separate patch too.
Yeah, this issue can be fixed by the patch which turns real EPT_MISCONFIG to TDX_SW_ERROR.
Side topic, tdx_to_vmx_exit_reason() can change EXIT_REASON_TDCALL to other Exit Reason. Do we want to print TDCALL instead of the transformed reason here?
Quoted 10 lines
Quoted 10 lines
<read values> } else { <zero values> }We'd need to use the unsafe version if we go with my suggestion[*], but other than that wrinkle, the above seems like the obviously correct fix (maybe too obvious)?
Re: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 29 lines · Xiaoyao Li
Quoted 28 lines · Sean Christopherson
Quoted 17 lines · Sean Christopherson
Quoted 16 lines · Xiaoyao Li
Quoted 14 lines · On 8/10/2026 7:39 PM, [email protected] wrote:
Review void tdx_get_exit_info(struct kvm_vcpu · line 2159
Quoted 6 lines
(quoted diff) +1
-1@@ -2147,7 +2156,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->vt.exit_reason.basic != -1) {[Severity: Medium]
Will this check always evaluate to true due to C integer promotion rules?The basic field in union
vmx_exit_reasonis a 16-bit unsigned bitfield. When comparing it to -1, the unsigned 16-bit value is promoted to a signed 32-bit integer. If the value was set to -1 (65535), the comparison evaluates as 65535 != -1, which is always true.Quoted 5 lines
Quoted 1 line
Well, how about something below on top of this patch?
No, we need to not rely on magic exit_reason.basic values. Can't this be?
if ((
tdx->vp_enter_ret&TDX_SW_ERROR) !=TDX_SW_ERROR) {This would arguably be a bug fix as well, because the "real"
EXIT_REASON_EPT_MISCONFIGpath gets a false negative. E.g. when getting information for an EPT Misconfig for the tracepoint, KVM really should print all information, not zeros.
Quoted 5 lines
Quoted 1 line
At a glance, this exact change can probably be a separate patch too.
Yeah, this issue can be fixed by the patch which turns real
EPT_MISCONFIGtoTDX_SW_ERROR.Side topic,
tdx_to_vmx_exit_reason()can changeEXIT_REASON_TDCALLto other Exit Reason. Do we want to print TDCALL instead of the transformed reason here?
Well, the more I think about this all, the more I dislike the fancy trick to turn TDCALL into other Exit Reason, in tdx_to_vmx_exit_reason(). Without it, it's straightforward to handle the EPT_MISCONFIG. And without it, the exit trace can also get the correct Exit Reason. How about something below? which is on top of kvm-x86/next
arch/x86/kvm/vmx/tdx.c +36 -29
@@ -924,7 +924,6 @@ static __always_inline u32 tdcall_to_vmx_exit_reason(struct kvm_vcpu *vcpu) static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) {struct vcpu_tdx *tdx =to_tdx (vcpu); -u32 exit_reason;switch (tdx->vp_enter_ret &TDX_SEAMCALL_STATUS_MASK ) {case TDX_SUCCESS :
@@ -932,30 +931,11 @@ static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu)case TDX_NON_RECOVERABLE_TD :case TDX_NON_RECOVERABLE_TD_NON_ACCESSIBLE :case TDX_NON_RECOVERABLE_TD_WRONG_APIC_MODE : -break ; +return (u32 )tdx->vp_enter_ret;default :return -1u ; } - exit_reason = tdx->vp_enter_ret; - -switch (exit_reason) { -case EXIT_REASON_TDCALL : -if (tdvmcall_exit_type (vcpu)) -return EXIT_REASON_VMCALL ; - -return tdcall_to_vmx_exit_reason (vcpu); -case EXIT_REASON_EPT_MISCONFIG : -/* -* Defer KVM_BUG_ON() until tdx_handle_exit() because this is in -* non-instrumentable code with interrupts disabled. -*/ -return -1u ; -default : -break ; - } - -return exit_reason; } static noinstr void tdx_vcpu_enter_exit(struct kvm_vcpu *vcpu)
@@ -1093,9 +1073,6 @@ fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags)kvm_clear_available_registers (vcpu, ~TDX_REGS_AVAIL_SET ); -if (unlikely (tdx->vp_enter_ret ==EXIT_REASON_EPT_MISCONFIG )) -return EXIT_FASTPATH_NONE ; -if (unlikely ((tdx->vp_enter_ret &TDX_SW_ERROR ) ==TDX_SW_ERROR ))return EXIT_FASTPATH_NONE ;
@@ -2027,6 +2004,41 @@ int tdx_complete_emulated_msr(struct kvm_vcpu *vcpu, int err)return 1 ; } +#define TDVMCALL_CPUID EXIT_REASON_CPUID +#define TDVMCALL_HLT EXIT_REASON_HLT +#define TDVMCALL_IO_INSTRUCTION EXIT_REASON_IO_INSTRUCTION +#define TDVMCALL_MSR_READ EXIT_REASON_MSR_READ +#define TDVMCALL_MSR_WRITE EXIT_REASON_MSR_WRITE +#define TDVMCALL_MMIO EXIT_REASON_EPT_VIOLATION + +static int handle_tdcall (struct kvm_vcpu *vcpu) +{ +struct vcpu_tdx *tdx =to_tdx (vcpu); + +if (tdvmcall_exit_type (vcpu)) +return tdx_emulate_vmcall (vcpu); + +switch (tdvmcall_leaf (vcpu)) { +case TDVMCALL_CPUID : +return tdx_emulate_cpuid (vcpu); +case TDVMCALL_HLT : +return kvm_emulate_halt_noskip (vcpu); +case TDVMCALL_IO_INSTRUCTION : +return tdx_emulate_io (vcpu); +case TDVMCALL_MSR_READ : +kvm_ecx_write (vcpu, tdx->vp_enter_args.r12); +return kvm_emulate_rdmsr (vcpu); +case TDVMCALL_MSR_WRITE : +kvm_ecx_write (vcpu, tdx->vp_enter_args.r12); +kvm_eax_write (vcpu, tdx->vp_enter_args.r13); +kvm_edx_write (vcpu, tdx->vp_enter_args.r13 >>32 ); +return kvm_emulate_wrmsr (vcpu); +case TDVMCALL_MMIO : +return tdx_emulate_mmio (vcpu); +default : +return handle_tdvmcall (vcpu); + } +}int tdx_handle_exit (struct kvm_vcpu *vcpu,fastpath_t fastpath) {
@@ -2037,11 +2049,6 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)if (fastpath !=EXIT_FASTPATH_NONE )return 1 ; -if (unlikely (vp_enter_ret ==EXIT_REASON_EPT_MISCONFIG )) { -KVM_BUG_ON (1 , vcpu->kvm); -return -EIO ; - } -/* * Handle TDX SW errors, including TDX_SEAMCALL_UD,
TDX_SEAMCALL_GP and
* TDX_SEAMCALL_VMFAILINVALID.
@@ -2083,26 +2090,12 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)
case EXIT_REASON_EXTERNAL_INTERRUPT:
++vcpu->stat.irq_exits;
return 1;- case EXIT_REASON_CPUID:
- return tdx_emulate_cpuid(vcpu);
- case EXIT_REASON_HLT:
- return kvm_emulate_halt_noskip(vcpu);
case EXIT_REASON_TDCALL:
- return handle_tdvmcall(vcpu);
- case EXIT_REASON_VMCALL:
- return tdx_emulate_vmcall(vcpu);
- case EXIT_REASON_IO_INSTRUCTION:
- return tdx_emulate_io(vcpu);
- case EXIT_REASON_MSR_READ:
- kvm_ecx_write(vcpu, tdx->vp_enter_args.r12);
- return kvm_emulate_rdmsr(vcpu);
- case EXIT_REASON_MSR_WRITE:
- kvm_ecx_write(vcpu, tdx->vp_enter_args.r12);
- kvm_eax_write(vcpu, tdx->vp_enter_args.r13);
- kvm_edx_write(vcpu, tdx->vp_enter_args.r13 >> 32);
- return kvm_emulate_wrmsr(vcpu);
+ return handle_tdcall(vcpu);
case EXIT_REASON_EPT_MISCONFIG:
- return tdx_emulate_mmio(vcpu);
+ /* EPT MISCONFIGs are *always* KVM/kernel bugs. */
+ KVM_BUG_ON(1, vcpu->kvm);
+ return -EIO;
case EXIT_REASON_EPT_VIOLATION:
return tdx_handle_ept_violation(vcpu);
case EXIT_REASON_OTHER_SMI:Re: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
In reply to Sean ChristophersonQuoted 16 lines · Sean Christopherson
Quoted 15 lines · Xiaoyao Li
Quoted 13 lines · On 8/10/2026 7:39 PM, [email protected] wrote:
Review void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, · line 2158
Quoted 5 lines
(quoted diff) +1
-1@@ -2147,7 +2156,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->vt.exit_reason.basic != -1) {[Severity: Medium]
Will this check always evaluate to true due to C integer promotion rules?The basic field in union
vmx_exit_reasonis a 16-bit unsigned bitfield. When comparing it to -1, the unsigned 16-bit value is promoted to a signed 32-bit integer. If the value was set to -1 (65535), the comparison evaluates as 65535 != -1, which is always true.
Quoted 5 lines
Quoted 1 line
Well, how about something below on top of this patch?
No, we need to not rely on magic exit_reason.basic values. Can't this be?
if ((tdx->vp_enter_ret & TDX_SW_ERROR) != TDX_SW_ERROR) {
I think the reason of checking if (*reason != -1u) in the original commit 095b71a03f49 ("KVM: TDX: Add a place holder to handle TDX VM exit") was that, -1u means there is no valid Exit Reason in the lower 32 bits in vp_enter_ret.
Change it to check TDX_SW_ERROR, doesn't look correct to me. Set the EPT_MISCONFIG magic handling aside, TDX_SW_ERROR only means the SEAMCALL instruction faults, e.g., hitting #UD, #GP, or VMFAILINVALID. Just a small subset of the cases where there is no valid Exit Reason.
Quoted 10 lines
<read values> } else { <zero values> }We'd need to use the unsafe version if we go with my suggestion[*], but other than that wrinkle, the above seems like the obviously correct fix (maybe too obvious)?
Sean Christopherson
maintainerRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 28 lines · Xiaoyao Li
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Xiaoyao Li
Quoted 13 lines · On 8/10/2026 7:39 PM, [email protected] wrote:
Review void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, · line 2158
Quoted 5 lines
(quoted diff) +1
-1@@ -2147,7 +2156,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->vt.exit_reason.basic != -1) {[Severity: Medium]
Will this check always evaluate to true due to C integer promotion rules?The basic field in union
vmx_exit_reasonis a 16-bit unsigned bitfield. When comparing it to -1, the unsigned 16-bit value is promoted to a signed 32-bit integer. If the value was set to -1 (65535), the comparison evaluates as 65535 != -1, which is always true.Quoted 5 lines
Quoted 1 line
Well, how about something below on top of this patch?
No, we need to not rely on magic exit_reason.basic values. Can't this be?
if ((tdx->vp_enter_ret & TDX_SW_ERROR) != TDX_SW_ERROR) {I think the reason of checking if (*reason != -1u) in the original commit
095b71a03f49("KVM: TDX: Add a place holder to handle TDX VM exit") was that, -1u means there is no valid Exit Reason in the lower 32 bits invp_enter_ret.
Quoted 4 lines
Change it to check
TDX_SW_ERROR, doesn't look correct to me. Set theEPT_MISCONFIGmagic handling aside,TDX_SW_ERRORonly means the SEAMCALL instruction faults, e.g., hitting #UD, #GP, or VMFAILINVALID. Just a small subset of the cases where there is no valid Exit Reason.
Surely we can enumerate those cases? I.e. why can't that be something like:
if (tdx_is_exit_reason_valid(vcpu))
where tdx_is_exit_reason_valid() decodes vp_enter_ret. Clobbering the entire exit reason and then _relying_ on that clobbered state is gross and brittle.
Re: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 34 lines · Sean Christopherson
Quoted 28 lines · Xiaoyao Li
Quoted 16 lines · Sean Christopherson
Quoted 15 lines · Xiaoyao Li
Quoted 13 lines · On 8/10/2026 7:39 PM, [email protected] wrote:
Review void tdx_get_exit_info(struct kvm_vcpu *vcpu, u32 *reason, · line 2158
Quoted 5 lines
(quoted diff) +1
-1@@ -2147,7 +2156,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->vt.exit_reason.basic != -1) {[Severity: Medium]
Will this check always evaluate to true due to C integer promotion rules?The basic field in union
vmx_exit_reasonis a 16-bit unsigned bitfield. When comparing it to -1, the unsigned 16-bit value is promoted to a signed 32-bit integer. If the value was set to -1 (65535), the comparison evaluates as 65535 != -1, which is always true.Quoted 5 lines
Quoted 1 line
Well, how about something below on top of this patch?
No, we need to not rely on magic exit_reason.basic values. Can't this be?
if ((tdx->vp_enter_ret & TDX_SW_ERROR) != TDX_SW_ERROR) {I think the reason of checking if (*reason != -1u) in the original commit
095b71a03f49("KVM: TDX: Add a place holder to handle TDX VM exit") was that, -1u means there is no valid Exit Reason in the lower 32 bits invp_enter_ret.Quoted 4 lines
Change it to check
TDX_SW_ERROR, doesn't look correct to me. Set theEPT_MISCONFIGmagic handling aside,TDX_SW_ERRORonly means the SEAMCALL instruction faults, e.g., hitting #UD, #GP, or VMFAILINVALID. Just a small subset of the cases where there is no valid Exit Reason.
Quoted 6 lines
Surely we can enumerate those cases? I.e. why can't that be something like:
if (
tdx_is_exit_reason_valid(vcpu))where
tdx_is_exit_reason_valid()decodesvp_enter_ret. Clobbering the entire exit reason and then _relying_ on that clobbered state is gross and brittle.
This a good suggestion. I implemented it as:
Author: Xiaoyao Li <[email protected]> Date: Thu Aug 13 09:42:15 2026 +0800
KVM: TDX: Check if there is valid exit info based on vp_enter_ret
Check 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 that real EPT MISCONFIG 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.
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);
Sean Christopherson
maintainerRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
In reply to Xiaoyao LiThe shortlog is way too generic, and the changelog is light on details. Over the weekend, I managed to forget why this was necessary, and it took me a few seconds to recall why we need to avoid setting bits 31:16. Of course, one could argue that says as much about me as it does the shortlog+changelog...
Oh, and shortlogs like "Fix the exit reason handling" sometimes lead to amusing follow-ups like "Really fix the exit reason handling". Don't be that person :-)
Something like:
KVM: TDX: Don't clobber exit_reason[31:16] when TDX-Module didn't try VM-Entry
and then in the changelog explain precisely why KVM needs to avoid clobbering those bits.
Quoted 11 lines · Xiaoyao Li
Get and check the exit reason from the low 16 bits of
vp_enter_ret, and store the synthesized/transformed exit reason in the "basic" field.Some bits in the upper 16 bits in the exit reason have their own meanings and they might be 1. When handling the exit reason, only do handling on the lower 16 bits and keep the upper 16 bits unchanged. This change also helps remove the additional check in
tdx_failed_vmentry().Note, due to the synthesized invalid exit reason, -1, is changed to assigned to the "basic" field, adjust the checking in
tdx_get_exit_info()accordingly.
Re: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 11 lines · Sean Christopherson
The shortlog is way too generic, and the changelog is light on details. Over the weekend, I managed to forget why this was necessary, and it took me a few seconds to recall why we need to avoid setting bits 31:16. Of course, one could argue that says as much about me as it does the shortlog+changelog...
Oh, and shortlogs like "Fix the exit reason handling" sometimes lead to amusing follow-ups like "Really fix the exit reason handling". Don't be that person :-)
Something like:
KVM: TDX: Don't clobber exit_reason[31:16] when TDX-Module didn't try VM-Entry
1. It's not only about clobbering the exit_reason[31:16], but also about how to interpret the basic exit, e.g.. the change
- switch (exit_reason) {
+ switch (exit_reason.basic) {
2. It's not only about TDX module doesn't try VM entry, bus also about a valid VM exit after successful VM entry. This patch also preserves the exit_reason[31:16] for a valid VM exit. (One could argue that existing code cannot clobber exit_reason[31;16] because the code to clobber it requires the [31:16] to be 0, because of switch (exit_reason))
3. For the case where TDX module doesn't try VM-Entry, I'm not sure if "clobber" is the correct word. There is no valid exit reason, so nothing to be really clobbered. Just don't synthesize a exit reason with non-zero bit 31:16.
My intent was just using one patch to handle them all, so as to make KVM behave correctly after enabling Bus Lock VM exit for TDX in the next patch. Because they are targeted for stable kernels.
What's your advice then? What do you think of spliting it into multiple so that it's easier to write a shortlog and changelog for each one.
Sean Christopherson
maintainerRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 16 lines · Xiaoyao Li
Quoted 11 lines · Sean Christopherson
The shortlog is way too generic, and the changelog is light on details. Over the weekend, I managed to forget why this was necessary, and it took me a few seconds to recall why we need to avoid setting bits 31:16. Of course, one could argue that says as much about me as it does the shortlog+changelog...
Oh, and shortlogs like "Fix the exit reason handling" sometimes lead to amusing follow-ups like "Really fix the exit reason handling". Don't be that person :-)
Something like:
KVM: TDX: Don't clobber exit_reason[31:16] when TDX-Module didn't try VM-Entry
1. It's not only about clobbering the exit_reason[31:16], but also about how to interpret the basic exit, e.g.. the change
arch/x86/kvm/vmx/tdx.c static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) · line 949
Quoted 2 lines
arch/x86/kvm/vmx/tdx.c +1
-1-switch (exit_reason) { +switch (exit_reason.basic) {
But the current TDX code doesn't care. The switch in tdx_to_vmx_exit_reason() operates on the raw vp_enter_ret value, not a synthesized/clobbered exit reason.
And the switch in tdx_handle_exit() isn't reachable for the clobbering case thanks to this check.
if (unlikely(vp_enter_ret == EXIT_REASON_EPT_MISCONFIG)) {
KVM_BUG_ON(1, vcpu->kvm);
return -EIO;
}I agree that not clobbering exit_reason[31:16] is useful for additional cleanups, but that isn't the main motiviation for the patch. The precision matters, because it changes this patch from a "nice to have cleanup" to "this is 100% mandatory to enable Instruction Timeout VM-Exits on TDX".
Quoted 11 lines
2. It's not only about TDX module doesn't try VM entry, bus also about a valid VM exit after successful VM entry. This patch also preserves the exit_reason[31:16] for a valid VM exit. (One could argue that existing code cannot clobber exit_reason[31;16] because the code to clobber it requires the [31:16] to be 0, because of switch (
exit_reason))3. For the case where TDX module doesn't try VM-Entry, I'm not sure if "clobber" is the correct word. There is no valid exit reason, so nothing to be really clobbered. Just don't synthesize a exit reason with non-zero bit 31:16.
Quoted 6 lines
My intent was just using one patch to handle them all, so as to make KVM behave correctly after enabling Bus Lock VM exit for TDX in the next patch. Because they are targeted for stable kernels.
What's your advice then? What do you think of spliting it into multiple so that it's easier to write a shortlog and changelog for each one.
Heh, my advice is pretty much always the same: when in doubt, split. Squashing patches together is trivial, disentangling changes after the fact is not.
Edgecombe, Rick P
maintainerRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
In reply to Xiaoyao LiQuoted 41 lines · Xiaoyao Li
Get and check the exit reason from the low 16 bits of
vp_enter_ret, and store the synthesized/transformed exit reason in the "basic" field.Some bits in the upper 16 bits in the exit reason have their own meanings and they might be 1. When handling the exit reason, only do handling on the lower 16 bits and keep the upper 16 bits unchanged. This change also helps remove the additional check in
tdx_failed_vmentry().Note, due to the synthesized invalid exit reason, -1, is changed to assigned to the "basic" field, adjust the checking in
tdx_get_exit_info()accordingly.Fixes 095b71a03f49 ("KVM: TDX: Add a place holder to handle TDX VM exit")Fixes c42856af8f70 ("KVM: TDX: Add a place holder for handler of TDX hypercalls (TDG.VP.VMCALL)")Signed-off-by Xiaoyao Li <[email protected]>---
Changes in v2:
- new patch--- arch/x86/kvm/vmx/tdx.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-)arch/x86/kvm/vmx/tdx.c +2
-2@@ -921,10 +921,10 @@ 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 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;switch (tdx->vp_enter_ret &TDX_SEAMCALL_STATUS_MASK ) {case TDX_SUCCESS :
arch/x86/kvm/vmx/tdx.c static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) · line 938
Quoted 6 lines
arch/x86/kvm/vmx/tdx.c +2
-1@@ -934,23 +934,33 @@ static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu)case TDX_NON_RECOVERABLE_TD_WRONG_APIC_MODE :break ;default : -return -1u ; +/* +* Synthesize an invalid bogus Exit Reason, as the TDX-Module
I think this blurb came from Sean, but can we standardize on "TDX module"? The code currently uses "TDX module" and "TDX-module" and "TDX-module" used much less. I also don't see why it needs the "-".
Quoted 21 lines
arch/x86/kvm/vmx/tdx.c +11
-5+* never attempted to run the vCPU, i.e. the Exit Reason is +* undefined, but this is NOT a failed VM-Enter. +*/ +return (union vmx_exit_reason ) { + .basic =-1 , + }; } - 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);
arch/x86/kvm/vmx/tdx.c static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) · line 963
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +4
-1+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 -1u ; +return (union vmx_exit_reason ) { + .basic =-1 , + };
We could make this return instead be a goto err; that returns this. Bonus is the comment on the other one can cover them both. What do you think?
Quoted 31 lines
arch/x86/kvm/vmx/tdx.c +3
-4default :break ; }@@ -967,7 +977,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;@@ -981,8 +991,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)@@ -2144,7 +2153,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->vt.exit_reason.basic !=-1 ) { *info1 =vmx_get_exit_qual (vcpu); *info2 = tdx->ext_exit_qualification; *intr_info =vmx_get_intr_info (vcpu);
Re: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 39 lines · Rick P
Quoted 38 lines · Xiaoyao Li
Get and check the exit reason from the low 16 bits of
vp_enter_ret, and store the synthesized/transformed exit reason in the "basic" field.Some bits in the upper 16 bits in the exit reason have their own meanings and they might be 1. When handling the exit reason, only do handling on the lower 16 bits and keep the upper 16 bits unchanged. This change also helps remove the additional check in
tdx_failed_vmentry().Note, due to the synthesized invalid exit reason, -1, is changed to assigned to the "basic" field, adjust the checking in
tdx_get_exit_info()accordingly.Fixes 095b71a03f49 ("KVM: TDX: Add a place holder to handle TDX VM exit")Fixes c42856af8f70 ("KVM: TDX: Add a place holder for handler of TDX hypercalls (TDG.VP.VMCALL)")Signed-off-by Xiaoyao Li <[email protected]>---
Changes in v2:
- new patch--- arch/x86/kvm/vmx/tdx.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-)arch/x86/kvm/vmx/tdx.c +2
-2@@ -921,10 +921,10 @@ 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 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;
Quoted 13 lines
arch/x86/kvm/vmx/tdx.c static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) · line 938
Quoted 9 lines
arch/x86/kvm/vmx/tdx.c +2
-1switch (tdx->vp_enter_ret &TDX_SEAMCALL_STATUS_MASK ) {case TDX_SUCCESS :@@ -934,23 +934,33 @@ static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu)case TDX_NON_RECOVERABLE_TD_WRONG_APIC_MODE :break ;default : -return -1u ; +/* +* Synthesize an invalid bogus Exit Reason, as the TDX-Module I think this blurb came from Sean, but can we standardize on "TDX module"? The code currently uses "TDX module" and "TDX-module" and "TDX-module" used much less. I also don't see why it needs the "-".
Will change it to "TDX module".
Quoted 26 lines
Quoted 26 lines
arch/x86/kvm/vmx/tdx.c +12
-5+* never attempted to run the vCPU, i.e. the Exit Reason is +* undefined, but this is NOT a failed VM-Enter. +*/ +return (union vmx_exit_reason ) { + .basic =-1 , + }; } - 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.
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) · line 963
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +3
-1*/ -return -1u ; +return (union vmx_exit_reason ) { + .basic =-1 , + };We could make this return instead be a goto err; that returns this. Bonus is the comment on the other one can cover them both. What do you think?
Given 1) this patch is here mainly to avoid the false-positive on bus_lock_detected after the next patch, 2) the whole series is targeted for stable kernels, and 3) this part will change as suggested by [1]
I think we can just leave it as-is to make the change as simple as possible.
Edgecombe, Rick P
maintainerRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 6 lines · Xiaoyao Li
Quoted 3 lines
We could make this return instead be a goto err; that returns this. Bonus is the
comment on the other one can cover them both. What do you think?Given 1) this patch is here mainly to avoid the false-positive on
bus_lock_detectedafter the next patch,
Hmm. Before this patch these are just one line returns. So a "goto err" would be unneeded. After this single line is expanded to be something more elaborate, and a comment is added around one of these more elaborate returns, but not the other. So I consider it a bit of a style regression. Which makes it appropriate to adjust it in the patch IMO.
Quoted 2 lines
2) the whole series is targeted
for stable kernels, and 3) this part will change as suggested by [1]
This is true.
Quoted 4 lines
I think we can just leave it as-is to make the change as simple as possible.
Re: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 5 lines · Rick P
Quoted 4 lines · Xiaoyao Li
Quoted 3 lines
We could make this return instead be a goto err; that returns this. Bonus is the
comment on the other one can cover them both. What do you think?
Quoted 8 lines
Quoted 2 lines
Given 1) this patch is here mainly to avoid the false-positive on
bus_lock_detectedafter the next patch,Hmm. Before this patch these are just one line returns. So a "goto err" would be unneeded. After this single line is expanded to be something more elaborate, and a comment is added around one of these more elaborate returns, but not the other. So I consider it a bit of a style regression. Which makes it appropriate to adjust it in the patch IMO.
Well, actually the added comment cannot be shared. For the EXIT_REASON_EPT_MISCONFIG case, the reason to return a synthesized invalid Exit Reason is to differentiate with the case of TDVMCALL(ept_violation), where the exit reason is updated to EPT_MISCONFIG.
In fact, we don't need such trick for real EPT_MISCONFIG, because there is handling for real EPT_MISCONFIG in tdx_handle_exit()
if (unlikely(vp_enter_ret == EXIT_REASON_EPT_MISCONFIG)) {
KVM_BUG_ON(1, vcpu->kvm);
return -EIO;
}before the handling of transformed EPT_MISCONFIG from TDVMCALL(ept_violation)
switch (exit_reason.basic) {
...
case EXIT_REASON_EPT_MISCONFIG:
return tdx_emulate_mmio(vcpu);But it's not urgent to fix it and it's not mandatory to enable Bus Lock VM exit on TDX. Let's leave it to the follow-up series.
Edgecombe, Rick P
maintainerRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 4 lines · Xiaoyao Li
Well, actually the added comment cannot be shared. For the
EXIT_REASON_EPT_MISCONFIGcase, the reason to return a synthesized invalid Exit Reason is to differentiate with the case ofTDVMCALL(ept_violation), where the exit reason is updated toEPT_MISCONFIG.
Ah, yea. You are right.
Quoted 12 lines
In fact, we don't need such trick for real
EPT_MISCONFIG, because there is handling for realEPT_MISCONFIGintdx_handle_exit()if (unlikely(vp_enter_ret == EXIT_REASON_EPT_MISCONFIG)) { KVM_BUG_ON(1, vcpu->kvm); return -EIO; }before the handling of transformed
EPT_MISCONFIGfromTDVMCALL(ept_violation)
Quoted 7 lines
switch (exit_reason.basic) { ... case EXIT_REASON_EPT_MISCONFIG: return tdx_emulate_mmio(vcpu);But it's not urgent to fix it and it's not mandatory to enable Bus Lock VM exit on TDX. Let's leave it to the follow-up series.
Ok.
Sean Christopherson
maintainerRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
In reply to Xiaoyao LiQuoted 24 lines · Xiaoyao Li
Quoted 23 lines · Rick P
Quoted 22 lines · Xiaoyao Li
--- arch/x86/kvm/vmx/tdx.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-)arch/x86/kvm/vmx/tdx.c +2
-2@@ -921,10 +921,10 @@ 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 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;switch (tdx->vp_enter_ret &TDX_SEAMCALL_STATUS_MASK ) {case TDX_SUCCESS :@@ -934,23 +934,33 @@ static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu)case TDX_NON_RECOVERABLE_TD_WRONG_APIC_MODE :break ;
Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) · line 938
Quoted 4 lines
arch/x86/kvm/vmx/tdx.c +2
-1default : -return -1u ; +/* +* Synthesize an invalid bogus Exit Reason, as the TDX-Module I think this blurb came from Sean, but can we standardize on "TDX module"? The code currently uses "TDX module" and "TDX-module" and "TDX-module" used much less. I also don't see why it needs the "-".
FWIW, I like TDX-Module to clearly identify it as _the_ TDX-Module, versus a potential TDX module provided the kernel, i.e. a tdx.ko module.
Re: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
Quoted 34 lines · Sean Christopherson
Quoted 24 lines · Xiaoyao Li
Quoted 23 lines · Rick P
Quoted 22 lines · Xiaoyao Li
--- arch/x86/kvm/vmx/tdx.c | 35 ++++++++++++++++++++++------------- 1 file changed, 22 insertions(+), 13 deletions(-)arch/x86/kvm/vmx/tdx.c +2
-2@@ -921,10 +921,10 @@ 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 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;switch (tdx->vp_enter_ret &TDX_SEAMCALL_STATUS_MASK ) {case TDX_SUCCESS :@@ -934,23 +934,33 @@ static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu)case TDX_NON_RECOVERABLE_TD_WRONG_APIC_MODE :break ;Quoted 8 lines
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c static __always_inline u32 tdx_to_vmx_exit_reason(struct kvm_vcpu *vcpu) · line 938
Quoted 4 lines
arch/x86/kvm/vmx/tdx.c +2
-1default : -return -1u ; +/* +* Synthesize an invalid bogus Exit Reason, as the TDX-Module I think this blurb came from Sean, but can we standardize on "TDX module"? The code currently uses "TDX module" and "TDX-module" and "TDX-module" used much less. I also don't see why it needs the "-".
Quoted 2 lines
FWIW, I like TDX-Module to clearly identify it as _the_ TDX-Module, versus a potential TDX module provided the kernel, i.e. a tdx.ko module.
# git grep "TDX-Module" | wc -l 13
# git grep "TDX module" | wc -l 162
#git grep "TDX-Module" | grep kvm | wc -l
9
#git grep "TDX module" | grep kvm | wc -l
42
There are 13 "TDX-Module" vs 162 "TDX module" in the kernel, and
9 "TDX-Module" vs 42 "TDX module" in KVM files. Given this, I changed to use "TDX module"
Edgecombe, Rick P
maintainerRe: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling
In reply to Sean ChristophersonQuoted 8 lines · Sean Christopherson
Quoted 5 lines
Quoted 5 lines
I think this blurb came from Sean, but can we standardize on "TDX module"? The
code currently uses "TDX module" and "TDX-module" and "TDX-module" used much
less. I also don't see why it needs the "-".FWIW, I like TDX-Module to clearly identify it as _the_ TDX-Module, versus a potential TDX module provided the kernel, i.e. a tdx.ko module.
Hmm, I see your point. I'm on board with taking that as the new target. I say we just pick something to go forward with for future changes, and be consistent. We can put fixing up the old non-conforming ones as a very low priority cleanup for the future.
PATCH v2 3/3 KVM: TDX: Enable Bus Lock VM exit
In reply to Xiaoyao LiEnable Bus Lock VM exit functionality for TDX guests.
Userspace can enable KVM_BUS_LOCK_DETECTION_EXIT for TDX guests without getting an error, but the feature is not actually enabled because KVM does not yet program the TDX execution control or handle the resulting exit.
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. Clear the bus_lock_detected bit to avoid being counted multiple times if it needs to return early for wait_for_sept_zap case in tdx_vcpu_run(). Since the wait_for_sept_zap case is expected to be rare, just do the clearing of bus_lock_detected unconditionally.
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 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 | 28 ++++++++++++++++++++++++++-- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/vmx.h | 1 + 3 files changed, 28 insertions(+), 3 deletions(-)
arch/x86/kvm/vmx/tdx.c +26 -2
@@ -1080,8 +1080,10 @@ 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))) { + vt->exit_reason.bus_lock_detected =0 ;return EXIT_FASTPATH_EXIT_HANDLED ; + }trace_kvm_entry (vcpu, run_flags &KVM_RUN_FORCE_IMMEDIATE_EXIT );
@@ -2037,7 +2039,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;
@@ -2138,6 +2140,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 __vmx_handle_notify (vcpu,vmx_get_exit_qual (vcpu)); +case EXIT_REASON_BUS_LOCK : +return handle_bus_lock_vmexit (vcpu);default :break ; }
@@ -2147,6 +2151,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) {
@@ -3173,6 +3193,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 ;
arch/x86/kvm/vmx/vmx.c +1 -1
@@ -6265,7 +6265,7 @@ static int handle_encls(struct kvm_vcpu *vcpu) }#endif /* CONFIG_X86_SGX_KVM */ -static int handle_bus_lock_vmexit (struct kvm_vcpu *vcpu) +int handle_bus_lock_vmexit (struct kvm_vcpu *vcpu) {/* * Hardware may or may not set the BUS_LOCK_DETECTED flag on BUS_LOCK
arch/x86/kvm/vmx/vmx.h +1 -0
@@ -379,6 +379,7 @@ bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned int flags);void vmx_ept_load_pdptrs (struct kvm_vcpu *vcpu);void vmx_set_intercept_for_msr (struct kvm_vcpu *vcpu,u32 msr,int type,bool set); +int handle_bus_lock_vmexit (struct kvm_vcpu *vcpu);static inline void vmx_disable_intercept_for_msr (struct kvm_vcpu *vcpu,u32 msr,int type)
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH v2 3/3] KVM: TDX: Enable Bus Lock VM exit
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:
- [High] The vmx_handle_exit() function silently drops negative error codes (fatal errors) when bus_lock_detected is true. --
--- Patch [3]: [PATCH v2 3/3] KVM: TDX: Enable Bus Lock VM exit --- commit 573aa824013208febeaa70c68e452e3344dd518c
Author: Xiaoyao Li <[email protected]>
KVM: TDX: Enable Bus Lock VM exit
This patch enables Bus Lock VM exit functionality for TDX guests by programming the BUS_LOCK_DETECTION control in the TD VMCS and adding the exit handler. It also handles clearing the bus_lock_detected bit to avoid issues during the wait_for_sept_zap case.
Quoted 4 lines
arch/x86/kvm/vmx/tdx.c +0
-0
[ ... ]
Quoted 4 lines
arch/x86/kvm/vmx/tdx.c +0
-0@@ -2150,6 +2154,22 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)return 0 ; }
arch/x86/kvm/vmx/tdx.c int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath) · line 2166
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-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 ; + }
[Severity: High]
This is a pre-existing issue, but since you fixed the negative return value handling here for tdx_handle_exit() in v2, should a similar fix be applied to the pre-existing vmx_handle_exit() in arch/x86/kvm/vmx/vmx.c?
Looking at vmx_handle_exit(), if __vmx_handle_exit() returns a negative error code (e.g. -ENOMEM) and bus_lock_detected is true, it unconditionally overrides the negative return value to 0:
if (vmx_get_exit_reason(vcpu).bus_lock_detected) {
if (ret > 0)
vcpu->run->exit_reason = KVM_EXIT_X86_BUS_LOCK;
vcpu->run->flags |= KVM_RUN_X86_BUS_LOCK;
return 0;
}Because ret > 0 is false, it skips setting vcpu->run->exit_reason but still returns 0 (success) to userspace.
Does this result in userspace receiving a successful return code but with a stale or uninitialized exit_reason, likely leading to a VMM crash or unexpected termination if a guest triggers a bus lock exit while KVM hits an internal error?
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+ + vcpu->run->flags |=KVM_RUN_X86_BUS_LOCK ; + } +return ret; +}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Re: [PATCH v2 3/3] KVM: TDX: Enable Bus Lock VM exit
Quoted 4 lines · On 8/10/2026 7:46 PM, [email protected] wrote:
Quoted 4 lines
arch/x86/kvm/vmx/tdx.c +0
-0@@ -2150,6 +2154,22 @@ int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath)return 0 ; }
Quoted 14 lines
arch/x86/kvm/vmx/tdx.c int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath) · line 2163
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-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 ; + }[Severity: High]
This is a pre-existing issue, but since you fixed the negative return value handling here fortdx_handle_exit()in v2, should a similar fix be applied to the pre-existingvmx_handle_exit()inarch/x86/kvm/vmx/vmx.c?
VMX fix was sent already:
Edgecombe, Rick P
maintainerRe: [PATCH v2 3/3] KVM: TDX: Enable Bus Lock VM exit
In reply to Xiaoyao LiQuoted 6 lines · Xiaoyao Li
Enable Bus Lock VM exit functionality for TDX guests.
Userspace can enable
KVM_BUS_LOCK_DETECTION_EXITfor TDX guests without getting an error, but the feature is not actually enabled because KVM does not yet program the TDX execution control or handle the resulting exit.
A bit run-on to me. Why not break it up like it's explained in patch 1.
Quoted 16 lines
Enable Bus Lock VM exit for TDX guests by programming the
BUS_LOCK_DETECTIONcontrol in the TD VMCS and by adding the exit handler. Clear thebus_lock_detectedbit to avoid being counted multiple times if it needs to return early forwait_for_sept_zapcase intdx_vcpu_run(). Since thewait_for_sept_zapcase is expected to be rare, just do the clearing ofbus_lock_detectedunconditionally.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]>
Quoted 7 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.
Ha! This is probably just being a bit funny. But let's treat AI review as suggestions only. If it is a good feedback, it can stand on it's own.
Quoted 11 lines
--- arch/x86/kvm/vmx/tdx.c | 28 ++++++++++++++++++++++++++-- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/vmx.h | 1 + 3 files changed, 28 insertions(+), 3 deletions(-)arch/x86/kvm/vmx/tdx.c +0
-0
arch/x86/kvm/vmx/tdx.c fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) · line 1086
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c +3
-1@@ -1080,8 +1080,10 @@ 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))) { + vt->exit_reason.bus_lock_detected =0 ;return EXIT_FASTPATH_EXIT_HANDLED ; + }
Hmm. Why is this the only part of exit_reason that we care about in this scenario?
I went and looked for similar scenarios on the VMX side to see what it did, and didn't find any. Same for you?
Quoted 30 lines
arch/x86/kvm/vmx/tdx.c +8
-1trace_kvm_entry (vcpu, run_flags &KVM_RUN_FORCE_IMMEDIATE_EXIT );@@ -2037,7 +2039,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;@@ -2138,6 +2140,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 __vmx_handle_notify (vcpu,vmx_get_exit_qual (vcpu)); +case EXIT_REASON_BUS_LOCK : +return handle_bus_lock_vmexit (vcpu);default :break ; }@@ -2147,6 +2151,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 */
arch/x86/kvm/vmx/tdx.c int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath) · line 2168
Quoted 10 lines
arch/x86/kvm/vmx/tdx.c +10
-0+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; +}
Ok, so the plan is to consolidate this duplication on top of the backportable fix.
Quoted 40 lines
arch/x86/kvm/vmx/tdx.c +5
-0+void tdx_get_exit_info (struct kvm_vcpu *vcpu,u32 *reason,u64 *info1,u64 *info2,u32 *intr_info,u32 *error_code) {@@ -3173,6 +3193,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 ;arch/x86/kvm/vmx/vmx.c +1
-1@@ -6265,7 +6265,7 @@ static int handle_encls(struct kvm_vcpu *vcpu) }#endif /* CONFIG_X86_SGX_KVM */ -static int handle_bus_lock_vmexit (struct kvm_vcpu *vcpu) +int handle_bus_lock_vmexit (struct kvm_vcpu *vcpu) {/* * Hardware may or may not set the BUS_LOCK_DETECTED flag on BUS_LOCK arch/x86/kvm/vmx/vmx.h +1
-0@@ -379,6 +379,7 @@ bool __vmx_vcpu_run(struct vcpu_vmx *vmx, unsigned int flags);void vmx_ept_load_pdptrs (struct kvm_vcpu *vcpu);void vmx_set_intercept_for_msr (struct kvm_vcpu *vcpu,u32 msr,int type,bool set); +int handle_bus_lock_vmexit (struct kvm_vcpu *vcpu);static inline void vmx_disable_intercept_for_msr (struct kvm_vcpu *vcpu,u32 msr,int type)
Re: [PATCH v2 3/3] KVM: TDX: Enable Bus Lock VM exit
Quoted 9 lines · Rick P
Quoted 6 lines · Xiaoyao Li
Enable Bus Lock VM exit functionality for TDX guests.
Userspace can enable
KVM_BUS_LOCK_DETECTION_EXITfor TDX guests without getting an error, but the feature is not actually enabled because KVM does not yet program the TDX execution control or handle the resulting exit.A bit run-on to me. Why not break it up like it's explained in patch 1.
Will update it to way how patch 1 describes.
Quoted 24 lines
Quoted 16 lines
Enable Bus Lock VM exit for TDX guests by programming the
BUS_LOCK_DETECTIONcontrol in the TD VMCS and by adding the exit handler. Clear thebus_lock_detectedbit to avoid being counted multiple times if it needs to return early forwait_for_sept_zapcase intdx_vcpu_run(). Since thewait_for_sept_zapcase is expected to be rare, just do the clearing ofbus_lock_detectedunconditionally.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]>Quoted 7 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.
Quoted 2 lines
Ha! This is probably just being a bit funny. But let's treat AI review as suggestions only. If it is a good feedback, it can stand on it's own.
yeah. Mostly for funny. I think the clarification itself makes sense.
Quoted 6 lines
Quoted 6 lines
--- arch/x86/kvm/vmx/tdx.c | 28 ++++++++++++++++++++++++++-- arch/x86/kvm/vmx/vmx.c | 2 +- arch/x86/kvm/vmx/vmx.h | 1 + 3 files changed, 28 insertions(+), 3 deletions(-)
Quoted 16 lines
arch/x86/kvm/vmx/tdx.c fastpath_t tdx_vcpu_run(struct kvm_vcpu *vcpu, u64 run_flags) · line 1086
Quoted 13 lines
arch/x86/kvm/vmx/tdx.c +3
-1@@ -1080,8 +1080,10 @@ 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))) { + vt->exit_reason.bus_lock_detected =0 ;return EXIT_FASTPATH_EXIT_HANDLED ; + }Hmm. Why is this the only part of
exit_reasonthat we care about in this scenario?
Because it's the only path on TDX that it returns without updating the vt->exit_reason. All the other cases go to tdx_vcpu_enter_exit() and tdx_vcpu_enter_exit() updates the vt->exit_reason.
Quoted 2 lines
I went and looked for similar scenarios on the VMX side to see what it did, and didn't find any. Same for you?
VMX can return early without reaching vmx_vcpu_enter_exit() as well. But VMX ensures vt->exit_reason is updated when it returns early.
Quoted 35 lines
Quoted 35 lines
arch/x86/kvm/vmx/tdx.c +13
-1trace_kvm_entry (vcpu, run_flags &KVM_RUN_FORCE_IMMEDIATE_EXIT );@@ -2037,7 +2039,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;@@ -2138,6 +2140,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 __vmx_handle_notify (vcpu,vmx_get_exit_qual (vcpu)); +case EXIT_REASON_BUS_LOCK : +return handle_bus_lock_vmexit (vcpu);default :break ; }@@ -2147,6 +2151,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 ; + }
Quoted 8 lines
arch/x86/kvm/vmx/tdx.c int tdx_handle_exit(struct kvm_vcpu *vcpu, fastpath_t fastpath) · line 2168
Quoted 5 lines
arch/x86/kvm/vmx/tdx.c +5
-0+ + vcpu->run->flags |=KVM_RUN_X86_BUS_LOCK ; + } +return ret; +}Ok, so the plan is to consolidate this duplication on top of the backportable fix.
yes. The consolidation also requires to first fix the VMX part[1]. So this series only contains the necessary things that need to be backported to stable kernels.