[PATCH] trace/vvmx: Correctly report TRC_HVM_NESTEDFLAG flag

Teddy Astie posted 1 patch 1 month, 1 week ago
xen/arch/x86/hvm/vmx/vmx.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
[PATCH] trace/vvmx: Correctly report TRC_HVM_NESTEDFLAG flag
Posted by Teddy Astie 1 month, 1 week ago
Currently all VMEXITs are reported as if it occured "non-nested",
even when the vCPU is in "guest mode". Making distinguishing
"nested virt" VMEXIT to regular ones much harder.

Fix this by adding TRC_HVM_NESTEDFLAG flag when the guest is in guest
mode, matching the svm logic.

Fixes: ??? (???)
Signed-off-by: Teddy Astie <teddy.astie@vates.tech>
---
I can't really find the commit that introduced vVMX. But I guess it
would be the candidate for the fixes tag.

 xen/arch/x86/hvm/vmx/vmx.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/xen/arch/x86/hvm/vmx/vmx.c b/xen/arch/x86/hvm/vmx/vmx.c
index da657187bc..d13603254d 100644
--- a/xen/arch/x86/hvm/vmx/vmx.c
+++ b/xen/arch/x86/hvm/vmx/vmx.c
@@ -4209,9 +4209,13 @@ void asmlinkage vmx_vmexit_handler(struct cpu_user_regs *regs)
     __vmread(VM_EXIT_REASON, &exit_reason);
 
     if ( hvm_long_mode_active(v) )
-        TRACE_TIME(TRC_HVM_VMX_EXIT64, exit_reason, regs->rip, regs->rip >> 32);
+        TRACE_TIME(TRC_HVM_VMX_EXIT64 |
+                   (nestedhvm_vcpu_in_guestmode(v) ? TRC_HVM_NESTEDFLAG : 0),
+                   exit_reason, regs->rip, regs->rip >> 32);
     else
-        TRACE_TIME(TRC_HVM_VMX_EXIT, exit_reason, regs->eip);
+        TRACE_TIME(TRC_HVM_VMX_EXIT |
+                   (nestedhvm_vcpu_in_guestmode(v) ? TRC_HVM_NESTEDFLAG : 0),
+                   exit_reason, regs->eip);
 
     perfc_incra(vmexits, (uint16_t)exit_reason);
 
@@ -5025,7 +5029,8 @@ bool asmlinkage vmx_vmenter_helper(const struct cpu_user_regs *regs)
     if ( unlikely(curr->arch.hvm.vmx.lbr_flags & LBR_FIXUP_MASK) )
         lbr_fixup();
 
-    TRACE_TIME(TRC_HVM_VMENTRY);
+    TRACE_TIME(TRC_HVM_VMENTRY  |
+               (nestedhvm_vcpu_in_guestmode(curr) ? TRC_HVM_NESTEDFLAG : 0));
 
     __vmwrite(GUEST_RIP,    regs->rip);
     __vmwrite(GUEST_RSP,    regs->rsp);
-- 
2.56.0



-- 
Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
Re: [PATCH] trace/vvmx: Correctly report TRC_HVM_NESTEDFLAG flag
Posted by Jan Beulich 1 month, 1 week ago
On 13.10.2026 11:09, Teddy Astie wrote:
> Currently all VMEXITs are reported as if it occured "non-nested",

Nit: s/ it / they /

> even when the vCPU is in "guest mode". Making distinguishing
> "nested virt" VMEXIT to regular ones much harder.

Same for entries, as it looks.

> Fix this by adding TRC_HVM_NESTEDFLAG flag when the guest is in guest
> mode, matching the svm logic.
> 
> Fixes: ??? (???)
> Signed-off-by: Teddy Astie <teddy.astie@vates.tech>
> ---
> I can't really find the commit that introduced vVMX. But I guess it
> would be the candidate for the fixes tag.

For the exit aspect I'd suggest 3bafd4a90995 ("Nested VMX: VM exit handler of
n2-guest"). Changes to vmx_vmenter_helper() came later, so I'd leave it at
that.

> --- a/xen/arch/x86/hvm/vmx/vmx.c
> +++ b/xen/arch/x86/hvm/vmx/vmx.c
> @@ -4209,9 +4209,13 @@ void asmlinkage vmx_vmexit_handler(struct cpu_user_regs *regs)
>      __vmread(VM_EXIT_REASON, &exit_reason);
>  
>      if ( hvm_long_mode_active(v) )
> -        TRACE_TIME(TRC_HVM_VMX_EXIT64, exit_reason, regs->rip, regs->rip >> 32);
> +        TRACE_TIME(TRC_HVM_VMX_EXIT64 |
> +                   (nestedhvm_vcpu_in_guestmode(v) ? TRC_HVM_NESTEDFLAG : 0),
> +                   exit_reason, regs->rip, regs->rip >> 32);
>      else
> -        TRACE_TIME(TRC_HVM_VMX_EXIT, exit_reason, regs->eip);
> +        TRACE_TIME(TRC_HVM_VMX_EXIT |
> +                   (nestedhvm_vcpu_in_guestmode(v) ? TRC_HVM_NESTEDFLAG : 0),
> +                   exit_reason, regs->eip);

SVM code uses a local boolean to avoid the separate (and now also repeated,
considering the later use) nestedhvm_vcpu_in_guestmode() invocations. Thoughts
towards introducing such a local here as well? (I don't quite understand why
they also use nestedhvm_enabled() when setting that boolean:
nestedhvm_vcpu_in_guestmode() would better return false for domains with
nested not enabled.)

Jan
Re: [PATCH] trace/vvmx: Correctly report TRC_HVM_NESTEDFLAG flag
Posted by Teddy Astie 1 month, 1 week ago
Le 13/10/2026 à 11:42, Jan Beulich a écrit :
> On 13.10.2026 11:09, Teddy Astie wrote:
>> Currently all VMEXITs are reported as if it occured "non-nested",
> 
> Nit: s/ it / they /
> 
>> even when the vCPU is in "guest mode". Making distinguishing
>> "nested virt" VMEXIT to regular ones much harder.
> 
> Same for entries, as it looks.
> 
>> Fix this by adding TRC_HVM_NESTEDFLAG flag when the guest is in guest
>> mode, matching the svm logic.
>>
>> Fixes: ??? (???)
>> Signed-off-by: Teddy Astie <teddy.astie@vates.tech>
>> ---
>> I can't really find the commit that introduced vVMX. But I guess it
>> would be the candidate for the fixes tag.
> 
> For the exit aspect I'd suggest 3bafd4a90995 ("Nested VMX: VM exit handler of
> n2-guest"). Changes to vmx_vmenter_helper() came later, so I'd leave it at
> that.
> 

Looks good to me.

>> --- a/xen/arch/x86/hvm/vmx/vmx.c
>> +++ b/xen/arch/x86/hvm/vmx/vmx.c
>> @@ -4209,9 +4209,13 @@ void asmlinkage vmx_vmexit_handler(struct cpu_user_regs *regs)
>>       __vmread(VM_EXIT_REASON, &exit_reason);
>>   
>>       if ( hvm_long_mode_active(v) )
>> -        TRACE_TIME(TRC_HVM_VMX_EXIT64, exit_reason, regs->rip, regs->rip >> 32);
>> +        TRACE_TIME(TRC_HVM_VMX_EXIT64 |
>> +                   (nestedhvm_vcpu_in_guestmode(v) ? TRC_HVM_NESTEDFLAG : 0),
>> +                   exit_reason, regs->rip, regs->rip >> 32);
>>       else
>> -        TRACE_TIME(TRC_HVM_VMX_EXIT, exit_reason, regs->eip);
>> +        TRACE_TIME(TRC_HVM_VMX_EXIT |
>> +                   (nestedhvm_vcpu_in_guestmode(v) ? TRC_HVM_NESTEDFLAG : 0),
>> +                   exit_reason, regs->eip);
> 
> SVM code uses a local boolean to avoid the separate (and now also repeated,
> considering the later use) nestedhvm_vcpu_in_guestmode() invocations. Thoughts
> towards introducing such a local here as well? (I don't quite understand why
> they also use nestedhvm_enabled() when setting that boolean:
> nestedhvm_vcpu_in_guestmode() would better return false for domains with
> nested not enabled.)
> 

More generally, we can consider doing a bit of refactoring around 
`nestedhvm_vcpu_in_guestmode()` (like making it a static inline), as 
it's just returning `vcpu_nestedhvm(v).nv_guestmode`, but hidden behind 
a function call of nestedhvm.c so it can't inline.

> Jan

Teddy