Re: [PATCH v2 2/3] KVM: TDX: Fix the exit reason handling

Xiaoyao Li <[email protected]>
Newsgroups dev.linux.lists.sashiko-reviews,org.kernel.vger.kvm
Message-ID <[email protected]>
On 8/10/2026 7:39 PM, [email protected] wrote:
>> @@ -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.
> 

Well, how about something below on top of this patch?

diff --git a/arch/x86/kvm/vmx/tdx.c b/arch/x86/kvm/vmx/tdx.c
index a89885d550c9..e3c8c92f6f71 100644
--- a/arch/x86/kvm/vmx/tdx.c
+++ b/arch/x86/kvm/vmx/tdx.c
@@ -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);
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.