2022-07-28 08:36:04

by Hou Wenlong

[permalink] [raw]
Subject: [PATCH 2/2] KVM: x86: Add missing trace points for RDMSR/WRMSR in emulator path

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 <[email protected]>
---
arch/x86/kvm/x86.c | 20 ++++++++++++++------
1 file changed, 14 insertions(+), 6 deletions(-)

diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
index 8df89b9c212f..6e45b20ce9a4 100644
--- a/arch/x86/kvm/x86.c
+++ b/arch/x86/kvm/x86.c
@@ -7908,12 +7908,16 @@ 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) {
+ if (!r) {
+ trace_kvm_msr_read(msr_index, *pdata);
+ } else {
if (kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_RDMSR, 0,
- complete_emulated_rdmsr, r))
+ complete_emulated_rdmsr, r)) {
r = X86EMUL_IO_NEEDED;
- else
+ } else {
+ trace_kvm_msr_read_ex(msr_index);
r = X86EMUL_UNHANDLEABLE;
+ }
}

return r;
@@ -7926,12 +7930,16 @@ 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) {
+ if (!r) {
+ trace_kvm_msr_write(msr_index, data);
+ } else if (r > 0) {
if (kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_WRMSR, data,
- complete_emulated_msr_access, r))
+ complete_emulated_msr_access, r)) {
r = X86EMUL_IO_NEEDED;
- else
+ } else {
+ trace_kvm_msr_write_ex(msr_index, data);
r = X86EMUL_UNHANDLEABLE;
+ }
}

return r;
--
2.31.1


2022-08-30 19:33:17

by Sean Christopherson

[permalink] [raw]
Subject: Re: [PATCH 2/2] KVM: x86: Add missing trace points for RDMSR/WRMSR in emulator path

On Thu, Jul 28, 2022, Hou Wenlong wrote:
> 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 <[email protected]>
> ---
> arch/x86/kvm/x86.c | 20 ++++++++++++++------
> 1 file changed, 14 insertions(+), 6 deletions(-)
>
> diff --git a/arch/x86/kvm/x86.c b/arch/x86/kvm/x86.c
> index 8df89b9c212f..6e45b20ce9a4 100644
> --- a/arch/x86/kvm/x86.c
> +++ b/arch/x86/kvm/x86.c
> @@ -7908,12 +7908,16 @@ 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) {
> + if (!r) {
> + trace_kvm_msr_read(msr_index, *pdata);
> + } else {
> if (kvm_msr_user_space(vcpu, msr_index, KVM_EXIT_X86_RDMSR, 0,
> - complete_emulated_rdmsr, r))
> + complete_emulated_rdmsr, r)) {
> r = X86EMUL_IO_NEEDED;
> - else
> + } else {
> + trace_kvm_msr_read_ex(msr_index);

Drat, I suspected this patch would make adding a helper a mess. We could use
trace_kvm_msr() directly, using @exit_reason to select between "rdmsr" and "wrmsr",
but I think the end result is less readable and not worth the small amount
of deduplication. E.g. this is rather hard to read.

if (r < 0)
return X86EMUL_UNHANDLEABLE;

if (r) {
if (kvm_msr_user_space(vcpu, msr, exit_reason, data, comp, r))
return X86EMUL_IO_NEEDED;

trace_kvm_msr(exit_reason == KVM_EXIT_X86_WRMSR,
msr, data, true);
return X86EMUL_PROPAGATE_FAULT;
}

trace_kvm_msr(exit_reason == KVM_EXIT_X86_WRMSR, msr, data, false);
return X86EMUL_CONTINUE;

Aha! If there "error" paths return directly, then the "else" paths go away and
this is all (IMO) a bit cleaner. And the diff for this patch should be much
smaller since there won't be any curly brace changes.

How about this for a final product?

static int emulator_get_msr_with_filter(struct x86_emulate_ctxt *ctxt,
u32 msr_index, u64 *pdata)
{
struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
int r;

r = kvm_get_msr_with_filter(vcpu, msr_index, pdata);
if (r < 0)
return X86EMUL_UNHANDLEABLE;

if (r) {
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;
}

static int emulator_set_msr_with_filter(struct x86_emulate_ctxt *ctxt,
u32 msr_index, u64 data)
{
struct kvm_vcpu *vcpu = emul_to_vcpu(ctxt);
int r;

r = kvm_set_msr_with_filter(vcpu, msr_index, data);
if (r < 0)
return X86EMUL_UNHANDLEABLE;

if (r) {
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;
}