kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Add missing trace points in emulator path
@ 2022-09-02  2:46 Hou Wenlong
  2022-09-02  2:47 ` [PATCH v2 1/2] KVM: x86: Return emulator error if RDMSR/WRMSR emulation failed Hou Wenlong
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Hou Wenlong @ 2022-09-02  2:46 UTC (permalink / raw)
  To: kvm

Some existed trace points are missing in emulator path, e.g.,
RDMSR/WRMSR emulation and CR read/write emulation. However,
if add those trace points in emulator common interfaces in
arch/x86/kvm/x86.c, other instruction emulation may use those
interfaces too and cause too much trace records. But add those
trace points in em_* functions in arch/x86/kvm/emulate.c seems
to be ugly. Luckily, RDMSR/WRMSR emulation uses a sepreate
interface, so add trace points for RDMSR/WRMSR in emulator
path is acceptable like normal path.

Changed from v1:
- As Sean suggested, use X86EMUL_PROPAGATE_FAULT instead of
  X86EMUL_UNHANDLEABLE for error path.
- As Sean suggested, move "r < 0" handling into the set helper,
  and add "r < 0" check in the get helper.

v1: https://lore.kernel.org/kvm/cover.1658913543.git.houwenlong.hwl@antgroup.com

Hou Wenlong (2):
  KVM: x86: Return emulator error if RDMSR/WRMSR emulation failed
  KVM: x86: Add missing trace points for RDMSR/WRMSR in emulator path

 arch/x86/kvm/emulate.c | 20 ++++++++------------
 arch/x86/kvm/x86.c     | 32 ++++++++++++++++++++++----------
 2 files changed, 30 insertions(+), 22 deletions(-)

--
2.31.1


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

* [PATCH v2 1/2] KVM: x86: Return emulator error if RDMSR/WRMSR emulation failed
  2022-09-02  2:46 [PATCH v2 0/2] Add missing trace points in emulator path Hou Wenlong
@ 2022-09-02  2:47 ` Hou Wenlong
  2022-09-02  2:47 ` [PATCH v2 2/2] KVM: x86: Add missing trace points for RDMSR/WRMSR in emulator path Hou Wenlong
  2022-09-20 22:12 ` [PATCH v2 0/2] Add missing trace points " Sean Christopherson
  2 siblings, 0 replies; 4+ messages in thread
From: Hou Wenlong @ 2022-09-02  2:47 UTC (permalink / raw)
  To: kvm
  Cc: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, linux-kernel

The return value of emulator_{get|set}_mst_with_filter()
is confused, since msr access error and emulator error
are mixed. Although, KVM_MSR_RET_* doesn't conflict with
X86EMUL_IO_NEEDED at present, it is better to convert
msr access error to emulator error if error value is
needed. So move "r < 0" handling for wrmsr emulation
into the set helper function, then only X86EMUL_* is
returned in the helper functions. Also add "r < 0"
check in the get helper function, although KVM doesn't
return -errno today, but assuming that will always hold
true is unnecessarily risking.

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
---
 arch/x86/kvm/emulate.c | 20 ++++++++------------
 arch/x86/kvm/x86.c     | 26 ++++++++++++++++----------
 2 files changed, 24 insertions(+), 22 deletions(-)

diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c
index f092c54d1a2f..d16f7d6a81bb 100644
--- a/arch/x86/kvm/emulate.c
+++ b/arch/x86/kvm/emulate.c
@@ -3645,13 +3645,10 @@ static int em_wrmsr(struct x86_emulate_ctxt *ctxt)
 		| ((u64)reg_read(ctxt, VCPU_REGS_RDX) << 32);
 	r = ctxt->ops->set_msr_with_filter(ctxt, msr_index, msr_data);
 
-	if (r == X86EMUL_IO_NEEDED)
-		return r;
-
-	if (r > 0)
+	if (r == X86EMUL_PROPAGATE_FAULT)
 		return emulate_gp(ctxt, 0);
 
-	return r < 0 ? X86EMUL_UNHANDLEABLE : X86EMUL_CONTINUE;
+	return r;
 }
 
 static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
@@ -3662,15 +3659,14 @@ static int em_rdmsr(struct x86_emulate_ctxt *ctxt)
 
 	r = ctxt->ops->get_msr_with_filter(ctxt, msr_index, &msr_data);
 
-	if (r == X86EMUL_IO_NEEDED)
-		return r;
-
-	if (r)
+	if (r == X86EMUL_PROPAGATE_FAULT)
 		return emulate_gp(ctxt, 0);
 
-	*reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
-	*reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
-	return X86EMUL_CONTINUE;
+	if (r == X86EMUL_CONTINUE) {
+		*reg_write(ctxt, VCPU_REGS_RAX) = (u32)msr_data;
+		*reg_write(ctxt, VCPU_REGS_RDX) = msr_data >> 32;
+	}
+	return r;
 }
 
 static int em_store_sreg(struct x86_emulate_ctxt *ctxt, int segment)
diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index d7374d768296..2596f5736743 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7920,14 +7920,17 @@ static int emulator_get_msr_with_filter(struct x86_emulate_ctxt *ctxt,
 	int r;
 
 	r = kvm_get_msr_with_filter(vcpu, msr_index, pdata);
+	if (r < 0)
+		return X86EMUL_UNHANDLEABLE;
 
-	if (r && kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_RDMSR, 0,
-				    complete_emulated_rdmsr, r)) {
-		/* Bounce to user space */
-		return X86EMUL_IO_NEEDED;
+	if (r) {
+		if (kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_RDMSR, 0,
+				       complete_emulated_rdmsr, r))
+			return X86EMUL_IO_NEEDED;
+		return X86EMUL_PROPAGATE_FAULT;
 	}
 
-	return r;
+	return X86EMUL_CONTINUE;
 }
 
 static int emulator_set_msr_with_filter(struct x86_emulate_ctxt *ctxt,
@@ -7937,14 +7940,17 @@ static int emulator_set_msr_with_filter(struct x86_emulate_ctxt *ctxt,
 	int r;
 
 	r = kvm_set_msr_with_filter(vcpu, msr_index, data);
+	if (r < 0)
+		return X86EMUL_UNHANDLEABLE;
 
-	if (r && kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_WRMSR, data,
-				    complete_emulated_msr_access, r)) {
-		/* Bounce to user space */
-		return X86EMUL_IO_NEEDED;
+	if (r) {
+		if (kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_WRMSR, data,
+				       complete_emulated_msr_access, r))
+			return X86EMUL_IO_NEEDED;
+		return X86EMUL_PROPAGATE_FAULT;
 	}
 
-	return r;
+	return X86EMUL_CONTINUE;
 }
 
 static int emulator_get_msr(struct x86_emulate_ctxt *ctxt,
-- 
2.31.1


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

* [PATCH v2 2/2] KVM: x86: Add missing trace points for RDMSR/WRMSR in emulator path
  2022-09-02  2:46 [PATCH v2 0/2] Add missing trace points in emulator path Hou Wenlong
  2022-09-02  2:47 ` [PATCH v2 1/2] KVM: x86: Return emulator error if RDMSR/WRMSR emulation failed Hou Wenlong
@ 2022-09-02  2:47 ` Hou Wenlong
  2022-09-20 22:12 ` [PATCH v2 0/2] Add missing trace points " Sean Christopherson
  2 siblings, 0 replies; 4+ messages in thread
From: Hou Wenlong @ 2022-09-02  2:47 UTC (permalink / raw)
  To: kvm
  Cc: Sean Christopherson, Paolo Bonzini, Thomas Gleixner, Ingo Molnar,
	Borislav Petkov, Dave Hansen, x86, H. Peter Anvin, linux-kernel

Since the RDMSR/WRMSR emulation uses a sepearte emualtor interface,
the trace points for RDMSR/WRMSR can be added in emulator path like
normal path.

Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
---
 arch/x86/kvm/x86.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 2596f5736743..1a224e575b0b 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7927,9 +7927,12 @@ static int emulator_get_msr_with_filter(struct x86_emulate_ctxt *ctxt,
 		if (kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_RDMSR, 0,
 				       complete_emulated_rdmsr, r))
 			return X86EMUL_IO_NEEDED;
+
+		trace_kvm_msr_read_ex(msr_index);
 		return X86EMUL_PROPAGATE_FAULT;
 	}
 
+	trace_kvm_msr_read(msr_index, *pdata);
 	return X86EMUL_CONTINUE;
 }
 
@@ -7947,9 +7950,12 @@ static int emulator_set_msr_with_filter(struct x86_emulate_ctxt *ctxt,
 		if (kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_WRMSR, data,
 				       complete_emulated_msr_access, r))
 			return X86EMUL_IO_NEEDED;
+
+		trace_kvm_msr_write_ex(msr_index, data);
 		return X86EMUL_PROPAGATE_FAULT;
 	}
 
+	trace_kvm_msr_write(msr_index, data);
 	return X86EMUL_CONTINUE;
 }
 
-- 
2.31.1


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

* Re: [PATCH v2 0/2] Add missing trace points in emulator path
  2022-09-02  2:46 [PATCH v2 0/2] Add missing trace points in emulator path Hou Wenlong
  2022-09-02  2:47 ` [PATCH v2 1/2] KVM: x86: Return emulator error if RDMSR/WRMSR emulation failed Hou Wenlong
  2022-09-02  2:47 ` [PATCH v2 2/2] KVM: x86: Add missing trace points for RDMSR/WRMSR in emulator path Hou Wenlong
@ 2022-09-20 22:12 ` Sean Christopherson
  2 siblings, 0 replies; 4+ messages in thread
From: Sean Christopherson @ 2022-09-20 22:12 UTC (permalink / raw)
  To: Hou Wenlong; +Cc: kvm

On Fri, Sep 02, 2022, Hou Wenlong wrote:
> Some existed trace points are missing in emulator path, e.g.,
> RDMSR/WRMSR emulation and CR read/write emulation. However,
> if add those trace points in emulator common interfaces in
> arch/x86/kvm/x86.c, other instruction emulation may use those
> interfaces too and cause too much trace records. But add those
> trace points in em_* functions in arch/x86/kvm/emulate.c seems
> to be ugly. Luckily, RDMSR/WRMSR emulation uses a sepreate
> interface, so add trace points for RDMSR/WRMSR in emulator
> path is acceptable like normal path.
> 
> Changed from v1:
> - As Sean suggested, use X86EMUL_PROPAGATE_FAULT instead of
>   X86EMUL_UNHANDLEABLE for error path.
> - As Sean suggested, move "r < 0" handling into the set helper,
>   and add "r < 0" check in the get helper.
> 
> v1: https://lore.kernel.org/kvm/cover.1658913543.git.houwenlong.hwl@antgroup.com
> 
> Hou Wenlong (2):
>   KVM: x86: Return emulator error if RDMSR/WRMSR emulation failed
>   KVM: x86: Add missing trace points for RDMSR/WRMSR in emulator path

Pushed to branch `for_paolo/6.1` at:

    https://github.com/sean-jc/linux.git

Unless you hear otherwise, it will make its way to kvm/queue "soon".

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

end of thread, other threads:[~2022-09-20 22:12 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-02  2:46 [PATCH v2 0/2] Add missing trace points in emulator path Hou Wenlong
2022-09-02  2:47 ` [PATCH v2 1/2] KVM: x86: Return emulator error if RDMSR/WRMSR emulation failed Hou Wenlong
2022-09-02  2:47 ` [PATCH v2 2/2] KVM: x86: Add missing trace points for RDMSR/WRMSR in emulator path Hou Wenlong
2022-09-20 22:12 ` [PATCH v2 0/2] Add missing trace points " Sean Christopherson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).