public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH 0/2] KVM: x86: Correct use of kvm_rip_read()
@ 2025-04-15 10:48 Adrian Hunter
  2025-04-15 10:48 ` [PATCH 1/2] KVM: x86: Do not use kvm_rip_read() unconditionally in KVM tracepoints Adrian Hunter
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Adrian Hunter @ 2025-04-15 10:48 UTC (permalink / raw)
  To: pbonzini, seanjc
  Cc: kvm, rick.p.edgecombe, kirill.shutemov, kai.huang,
	reinette.chatre, xiaoyao.li, tony.lindgren, binbin.wu,
	isaku.yamahata, linux-kernel, yan.y.zhao, chao.gao

Hi

Here are a couple of tweaks to dodge kvm_rip_read() in tracepoints and
KVM_PROFILING when guest_state_protected.

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


Adrian Hunter (2):
      KVM: x86: Do not use kvm_rip_read() unconditionally in KVM tracepoints
      KVM: x86: Do not use kvm_rip_read() unconditionally for KVM_PROFILING

 arch/x86/kvm/trace.h | 13 ++++++++++---
 arch/x86/kvm/x86.c   |  3 ++-
 2 files changed, 12 insertions(+), 4 deletions(-)


Regards
Adrian

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 1/2] KVM: x86: Do not use kvm_rip_read() unconditionally in KVM tracepoints
  2025-04-15 10:48 [PATCH 0/2] KVM: x86: Correct use of kvm_rip_read() Adrian Hunter
@ 2025-04-15 10:48 ` Adrian Hunter
  2025-04-15 10:48 ` [PATCH 2/2] KVM: x86: Do not use kvm_rip_read() unconditionally for KVM_PROFILING Adrian Hunter
  2025-04-15 11:31 ` [PATCH 0/2] KVM: x86: Correct use of kvm_rip_read() Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Adrian Hunter @ 2025-04-15 10:48 UTC (permalink / raw)
  To: pbonzini, seanjc
  Cc: kvm, rick.p.edgecombe, kirill.shutemov, kai.huang,
	reinette.chatre, xiaoyao.li, tony.lindgren, binbin.wu,
	isaku.yamahata, linux-kernel, yan.y.zhao, chao.gao

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>
---
 arch/x86/kvm/trace.h | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

diff --git a/arch/x86/kvm/trace.h b/arch/x86/kvm/trace.h
index ccda95e53f62..ba736cbb0587 100644
--- a/arch/x86/kvm/trace.h
+++ b/arch/x86/kvm/trace.h
@@ -11,6 +11,13 @@
 #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;
 	),
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* [PATCH 2/2] KVM: x86: Do not use kvm_rip_read() unconditionally for KVM_PROFILING
  2025-04-15 10:48 [PATCH 0/2] KVM: x86: Correct use of kvm_rip_read() Adrian Hunter
  2025-04-15 10:48 ` [PATCH 1/2] KVM: x86: Do not use kvm_rip_read() unconditionally in KVM tracepoints Adrian Hunter
@ 2025-04-15 10:48 ` Adrian Hunter
  2025-04-15 11:31 ` [PATCH 0/2] KVM: x86: Correct use of kvm_rip_read() Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Adrian Hunter @ 2025-04-15 10:48 UTC (permalink / raw)
  To: pbonzini, seanjc
  Cc: kvm, rick.p.edgecombe, kirill.shutemov, kai.huang,
	reinette.chatre, xiaoyao.li, tony.lindgren, binbin.wu,
	isaku.yamahata, linux-kernel, yan.y.zhao, chao.gao

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>
---
 arch/x86/kvm/x86.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 844e81ee1d96..8758f8cba488 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -11130,7 +11130,8 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
 	/*
 	 * Profile KVM exit RIPs:
 	 */
-	if (unlikely(prof_on == KVM_PROFILING)) {
+	if (unlikely(prof_on == KVM_PROFILING &&
+		     !vcpu->arch.guest_state_protected)) {
 		unsigned long rip = kvm_rip_read(vcpu);
 		profile_hit(KVM_PROFILING, (void *)rip);
 	}
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH 0/2] KVM: x86: Correct use of kvm_rip_read()
  2025-04-15 10:48 [PATCH 0/2] KVM: x86: Correct use of kvm_rip_read() Adrian Hunter
  2025-04-15 10:48 ` [PATCH 1/2] KVM: x86: Do not use kvm_rip_read() unconditionally in KVM tracepoints Adrian Hunter
  2025-04-15 10:48 ` [PATCH 2/2] KVM: x86: Do not use kvm_rip_read() unconditionally for KVM_PROFILING Adrian Hunter
@ 2025-04-15 11:31 ` Paolo Bonzini
  2 siblings, 0 replies; 4+ messages in thread
From: Paolo Bonzini @ 2025-04-15 11:31 UTC (permalink / raw)
  To: Adrian Hunter
  Cc: pbonzini, seanjc, kvm, rick.p.edgecombe, kirill.shutemov,
	kai.huang, reinette.chatre, xiaoyao.li, tony.lindgren, binbin.wu,
	isaku.yamahata, linux-kernel, yan.y.zhao, chao.gao

Queued, thanks.

Paolo



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2025-04-15 11:32 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-15 10:48 [PATCH 0/2] KVM: x86: Correct use of kvm_rip_read() Adrian Hunter
2025-04-15 10:48 ` [PATCH 1/2] KVM: x86: Do not use kvm_rip_read() unconditionally in KVM tracepoints Adrian Hunter
2025-04-15 10:48 ` [PATCH 2/2] KVM: x86: Do not use kvm_rip_read() unconditionally for KVM_PROFILING Adrian Hunter
2025-04-15 11:31 ` [PATCH 0/2] KVM: x86: Correct use of kvm_rip_read() Paolo Bonzini

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox