Re: [PATCH v5 10/12] KVM: X86: Rename and move the function vmx_handle_memory_failure to x86.c

2020-08-26 Thread Jim Mattson
On Wed, Aug 26, 2020 at 12:15 PM Babu Moger  wrote:
>
> Handling of kvm_read/write_guest_virt*() errors can be moved to common
> code. The same code can be used by both VMX and SVM.
>
> Signed-off-by: Babu Moger 
Reviewed-by: Jim Mattson 


[PATCH v5 10/12] KVM: X86: Rename and move the function vmx_handle_memory_failure to x86.c

2020-08-26 Thread Babu Moger
Handling of kvm_read/write_guest_virt*() errors can be moved to common
code. The same code can be used by both VMX and SVM.

Signed-off-by: Babu Moger 
---
 arch/x86/kvm/vmx/nested.c |   12 ++--
 arch/x86/kvm/vmx/vmx.c|   29 +
 arch/x86/kvm/vmx/vmx.h|2 --
 arch/x86/kvm/x86.c|   28 
 arch/x86/kvm/x86.h|2 ++
 5 files changed, 37 insertions(+), 36 deletions(-)

diff --git a/arch/x86/kvm/vmx/nested.c b/arch/x86/kvm/vmx/nested.c
index 23b58c28a1c9..28becd22d9d9 100644
--- a/arch/x86/kvm/vmx/nested.c
+++ b/arch/x86/kvm/vmx/nested.c
@@ -4688,7 +4688,7 @@ static int nested_vmx_get_vmptr(struct kvm_vcpu *vcpu, 
gpa_t *vmpointer,
 
r = kvm_read_guest_virt(vcpu, gva, vmpointer, sizeof(*vmpointer), );
if (r != X86EMUL_CONTINUE) {
-   *ret = vmx_handle_memory_failure(vcpu, r, );
+   *ret = kvm_handle_memory_failure(vcpu, r, );
return -EINVAL;
}
 
@@ -4995,7 +4995,7 @@ static int handle_vmread(struct kvm_vcpu *vcpu)
/* _system ok, nested_vmx_check_permission has verified cpl=0 */
r = kvm_write_guest_virt_system(vcpu, gva, , len, );
if (r != X86EMUL_CONTINUE)
-   return vmx_handle_memory_failure(vcpu, r, );
+   return kvm_handle_memory_failure(vcpu, r, );
}
 
return nested_vmx_succeed(vcpu);
@@ -5068,7 +5068,7 @@ static int handle_vmwrite(struct kvm_vcpu *vcpu)
return 1;
r = kvm_read_guest_virt(vcpu, gva, , len, );
if (r != X86EMUL_CONTINUE)
-   return vmx_handle_memory_failure(vcpu, r, );
+   return kvm_handle_memory_failure(vcpu, r, );
}
 
field = kvm_register_readl(vcpu, (((instr_info) >> 28) & 0xf));
@@ -5230,7 +5230,7 @@ static int handle_vmptrst(struct kvm_vcpu *vcpu)
r = kvm_write_guest_virt_system(vcpu, gva, (void *)_vmptr,
sizeof(gpa_t), );
if (r != X86EMUL_CONTINUE)
-   return vmx_handle_memory_failure(vcpu, r, );
+   return kvm_handle_memory_failure(vcpu, r, );
 
return nested_vmx_succeed(vcpu);
 }
@@ -5283,7 +5283,7 @@ static int handle_invept(struct kvm_vcpu *vcpu)
return 1;
r = kvm_read_guest_virt(vcpu, gva, , sizeof(operand), );
if (r != X86EMUL_CONTINUE)
-   return vmx_handle_memory_failure(vcpu, r, );
+   return kvm_handle_memory_failure(vcpu, r, );
 
/*
 * Nested EPT roots are always held through guest_mmu,
@@ -5365,7 +5365,7 @@ static int handle_invvpid(struct kvm_vcpu *vcpu)
return 1;
r = kvm_read_guest_virt(vcpu, gva, , sizeof(operand), );
if (r != X86EMUL_CONTINUE)
-   return vmx_handle_memory_failure(vcpu, r, );
+   return kvm_handle_memory_failure(vcpu, r, );
 
if (operand.vpid >> 16)
return nested_vmx_fail(vcpu,
diff --git a/arch/x86/kvm/vmx/vmx.c b/arch/x86/kvm/vmx/vmx.c
index 46ba2e03a892..b15b4c6e3b46 100644
--- a/arch/x86/kvm/vmx/vmx.c
+++ b/arch/x86/kvm/vmx/vmx.c
@@ -1598,33 +1598,6 @@ static int skip_emulated_instruction(struct kvm_vcpu 
*vcpu)
return 1;
 }
 
-/*
- * Handles kvm_read/write_guest_virt*() result and either injects #PF or 
returns
- * KVM_EXIT_INTERNAL_ERROR for cases not currently handled by KVM. Return value
- * indicates whether exit to userspace is needed.
- */
-int vmx_handle_memory_failure(struct kvm_vcpu *vcpu, int r,
- struct x86_exception *e)
-{
-   if (r == X86EMUL_PROPAGATE_FAULT) {
-   kvm_inject_emulated_page_fault(vcpu, e);
-   return 1;
-   }
-
-   /*
-* In case kvm_read/write_guest_virt*() failed with X86EMUL_IO_NEEDED
-* while handling a VMX instruction KVM could've handled the request
-* correctly by exiting to userspace and performing I/O but there
-* doesn't seem to be a real use-case behind such requests, just return
-* KVM_EXIT_INTERNAL_ERROR for now.
-*/
-   vcpu->run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
-   vcpu->run->internal.suberror = KVM_INTERNAL_ERROR_EMULATION;
-   vcpu->run->internal.ndata = 0;
-
-   return 0;
-}
-
 /*
  * Recognizes a pending MTF VM-exit and records the nested state for later
  * delivery.
@@ -5558,7 +5531,7 @@ static int handle_invpcid(struct kvm_vcpu *vcpu)
 
r = kvm_read_guest_virt(vcpu, gva, , sizeof(operand), );
if (r != X86EMUL_CONTINUE)
-   return vmx_handle_memory_failure(vcpu, r, );
+   return kvm_handle_memory_failure(vcpu, r, );
 
if (operand.pcid >> 12 != 0) {
kvm_inject_gp(vcpu, 0);
diff --git a/arch/x86/kvm/vmx/vmx.h b/arch/x86/kvm/vmx/vmx.h
index 26175a4759fa..7c578564a8fc 100644
--- a/arch/x86/kvm/vmx/vmx.h