KVM: x86: Do not use kvm_rip_read() unconditionally in KVM tracepoints
authorAdrian Hunter <adrian.hunter@intel.com>
Tue, 15 Apr 2025 10:48:20 +0000 (13:48 +0300)
committerPaolo Bonzini <pbonzini@redhat.com>
Thu, 24 Apr 2025 13:52:31 +0000 (09:52 -0400)
Not all VMs allow access to RIP.  Check guest_state_protected before
calling kvm_rip_read().

This avoids, for example, hitting WARN_ON_ONCE in vt_cache_reg() for
TDX VMs.

Fixes: 81bf912b2c15 ("KVM: TDX: Implement TDX vcpu enter/exit path")
Signed-off-by: Adrian Hunter <adrian.hunter@intel.com>
Message-ID: <20250415104821.247234-2-adrian.hunter@intel.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
arch/x86/kvm/trace.h

index ccda95e53f626dcb472cf0964c14e71e56e5b5dd..ba736cbb0587cd2d7417065e35f37d7ffd5fbfb1 100644 (file)
 #undef TRACE_SYSTEM
 #define TRACE_SYSTEM kvm
 
+#ifdef CREATE_TRACE_POINTS
+#define tracing_kvm_rip_read(vcpu) ({                                  \
+       typeof(vcpu) __vcpu = vcpu;                                     \
+       __vcpu->arch.guest_state_protected ? 0 : kvm_rip_read(__vcpu);  \
+       })
+#endif
+
 /*
  * Tracepoint for guest mode entry.
  */
@@ -28,7 +35,7 @@ TRACE_EVENT(kvm_entry,
 
        TP_fast_assign(
                __entry->vcpu_id        = vcpu->vcpu_id;
-               __entry->rip            = kvm_rip_read(vcpu);
+               __entry->rip            = tracing_kvm_rip_read(vcpu);
                __entry->immediate_exit = force_immediate_exit;
 
                kvm_x86_call(get_entry_info)(vcpu, &__entry->intr_info,
@@ -319,7 +326,7 @@ TRACE_EVENT(name,                                                        \
        ),                                                                   \
                                                                             \
        TP_fast_assign(                                                      \
-               __entry->guest_rip      = kvm_rip_read(vcpu);                \
+               __entry->guest_rip      = tracing_kvm_rip_read(vcpu);                \
                __entry->isa            = isa;                               \
                __entry->vcpu_id        = vcpu->vcpu_id;                     \
                __entry->requests       = READ_ONCE(vcpu->requests);         \
@@ -423,7 +430,7 @@ TRACE_EVENT(kvm_page_fault,
 
        TP_fast_assign(
                __entry->vcpu_id        = vcpu->vcpu_id;
-               __entry->guest_rip      = kvm_rip_read(vcpu);
+               __entry->guest_rip      = tracing_kvm_rip_read(vcpu);
                __entry->fault_address  = fault_address;
                __entry->error_code     = error_code;
        ),