All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v3] KVM: MMU: Fix guest stuck during boot due to read/write emulation against GVA
@ 2017-08-11  6:46 Wanpeng Li
  2017-08-11  6:53 ` David Hildenbrand
  2017-08-11 15:01 ` Paolo Bonzini
  0 siblings, 2 replies; 3+ messages in thread
From: Wanpeng Li @ 2017-08-11  6:46 UTC (permalink / raw)
  To: linux-kernel, kvm
  Cc: Paolo Bonzini, Radim Krčmář,
	Wanpeng Li, Brijesh Singh, David Hildenbrand

From: Wanpeng Li <wanpeng.li@hotmail.com>

Commit c016004494b0 (KVM: x86: Avoid guest page table walk when gpa_available 
is set) avoids the page table walk when cr2 has already contained a valid GPA. 
However, that is not the truth if ept == 0 and shadow page table is used. In 
this scenario cr2 can just contains a valid GVA. The commit results in guest 
stuck during boot due to read/write emulation against GVA instead of GPA of 
the guest.

This patch fixes it by setting the gpa_available flag under direct mode.  

Cc: Paolo Bonzini <pbonzini@redhat.com>
Cc: Radim Krčmář <rkrcmar@redhat.com>
Cc: Brijesh Singh <brijesh.singh@amd.com>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
---
 arch/x86/kvm/mmu.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
index 849312d..4780348 100644
--- a/arch/x86/kvm/mmu.c
+++ b/arch/x86/kvm/mmu.c
@@ -3791,8 +3791,11 @@ int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
 
 		if (need_unprotect && kvm_event_needs_reinjection(vcpu))
 			kvm_mmu_unprotect_page_virt(vcpu, fault_address);
-		vcpu->arch.gpa_available = true;
-		vcpu->arch.gpa_val = fault_address;
+		/* With shadow page tables, cr2 could contain a GVA, not a GPA */
+		if (vcpu->arch.mmu.direct_map) {
+			vcpu->arch.gpa_available = true;
+			vcpu->arch.gpa_val = fault_address;
+		}
 		r = kvm_mmu_page_fault(vcpu, fault_address, error_code, insn,
 				insn_len);
 		break;
-- 
2.7.4

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

* Re: [PATCH v3] KVM: MMU: Fix guest stuck during boot due to read/write emulation against GVA
  2017-08-11  6:46 [PATCH v3] KVM: MMU: Fix guest stuck during boot due to read/write emulation against GVA Wanpeng Li
@ 2017-08-11  6:53 ` David Hildenbrand
  2017-08-11 15:01 ` Paolo Bonzini
  1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand @ 2017-08-11  6:53 UTC (permalink / raw)
  To: Wanpeng Li, linux-kernel, kvm
  Cc: Paolo Bonzini, Radim Krčmář, Wanpeng Li, Brijesh Singh

On 11.08.2017 08:46, Wanpeng Li wrote:
> From: Wanpeng Li <wanpeng.li@hotmail.com>
> 
> Commit c016004494b0 (KVM: x86: Avoid guest page table walk when gpa_available 
> is set) avoids the page table walk when cr2 has already contained a valid GPA. 
> However, that is not the truth if ept == 0 and shadow page table is used. In 
> this scenario cr2 can just contains a valid GVA. The commit results in guest 
> stuck during boot due to read/write emulation against GVA instead of GPA of 
> the guest.
> 
> This patch fixes it by setting the gpa_available flag under direct mode.  
> 
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Radim Krčmář <rkrcmar@redhat.com>
> Cc: Brijesh Singh <brijesh.singh@amd.com>
> Cc: David Hildenbrand <david@redhat.com>
> Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
> ---
>  arch/x86/kvm/mmu.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
> index 849312d..4780348 100644
> --- a/arch/x86/kvm/mmu.c
> +++ b/arch/x86/kvm/mmu.c
> @@ -3791,8 +3791,11 @@ int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
>  
>  		if (need_unprotect && kvm_event_needs_reinjection(vcpu))
>  			kvm_mmu_unprotect_page_virt(vcpu, fault_address);
> -		vcpu->arch.gpa_available = true;
> -		vcpu->arch.gpa_val = fault_address;
> +		/* With shadow page tables, cr2 could contain a GVA, not a GPA */
> +		if (vcpu->arch.mmu.direct_map) {
> +			vcpu->arch.gpa_available = true;
> +			vcpu->arch.gpa_val = fault_address;
> +		}
>  		r = kvm_mmu_page_fault(vcpu, fault_address, error_code, insn,
>  				insn_len);
>  		break;
> 

Reviewed-by: David Hildenbrand <david@redhat.com>

-- 

Thanks,

David

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

* Re: [PATCH v3] KVM: MMU: Fix guest stuck during boot due to read/write emulation against GVA
  2017-08-11  6:46 [PATCH v3] KVM: MMU: Fix guest stuck during boot due to read/write emulation against GVA Wanpeng Li
  2017-08-11  6:53 ` David Hildenbrand
@ 2017-08-11 15:01 ` Paolo Bonzini
  1 sibling, 0 replies; 3+ messages in thread
From: Paolo Bonzini @ 2017-08-11 15:01 UTC (permalink / raw)
  To: Wanpeng Li, linux-kernel, kvm
  Cc: Radim Krčmář,
	Wanpeng Li, Brijesh Singh, David Hildenbrand

On 11/08/2017 08:46, Wanpeng Li wrote:
> From: Wanpeng Li <wanpeng.li@hotmail.com>
> 
> Commit c016004494b0 (KVM: x86: Avoid guest page table walk when gpa_available 
> is set) avoids the page table walk when cr2 has already contained a valid GPA. 
> However, that is not the truth if ept == 0 and shadow page table is used. In 
> this scenario cr2 can just contains a valid GVA. The commit results in guest 
> stuck during boot due to read/write emulation against GVA instead of GPA of 
> the guest.
> 
> This patch fixes it by setting the gpa_available flag under direct mode.  
> 
> Cc: Paolo Bonzini <pbonzini@redhat.com>
> Cc: Radim Krčmář <rkrcmar@redhat.com>
> Cc: Brijesh Singh <brijesh.singh@amd.com>
> Cc: David Hildenbrand <david@redhat.com>
> Signed-off-by: Wanpeng Li <wanpeng.li@hotmail.com>
> ---
>  arch/x86/kvm/mmu.c | 7 +++++--
>  1 file changed, 5 insertions(+), 2 deletions(-)
> 
> diff --git a/arch/x86/kvm/mmu.c b/arch/x86/kvm/mmu.c
> index 849312d..4780348 100644
> --- a/arch/x86/kvm/mmu.c
> +++ b/arch/x86/kvm/mmu.c
> @@ -3791,8 +3791,11 @@ int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
>  
>  		if (need_unprotect && kvm_event_needs_reinjection(vcpu))
>  			kvm_mmu_unprotect_page_virt(vcpu, fault_address);
> -		vcpu->arch.gpa_available = true;
> -		vcpu->arch.gpa_val = fault_address;
> +		/* With shadow page tables, cr2 could contain a GVA, not a GPA */

A GVA or a nested GPA, more precisely.

> +		if (vcpu->arch.mmu.direct_map) {
> +			vcpu->arch.gpa_available = true;
> +			vcpu->arch.gpa_val = fault_address;
> +		}
>  		r = kvm_mmu_page_fault(vcpu, fault_address, error_code, insn,
>  				insn_len);
>  		break;
> 


Also not enough, we need

diff --git a/arch/x86/kvm/vmx.c b/arch/x86/kvm/vmx.c
index 5fd7ecbf7284..283e94de3853 100644
--- a/arch/x86/kvm/vmx.c
+++ b/arch/x86/kvm/vmx.c
@@ -6393,7 +6393,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)
 	error_code |= (exit_qualification & 0x100) != 0 ?
 	       PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
 
-	vcpu->arch.gpa_available = true;
+	vcpu->arch.gpa_available = !is_guest_mode(vcpu);
 	vcpu->arch.gpa_val = gpa;
 	vcpu->arch.exit_qualification = exit_qualification;
 
@@ -6412,7 +6412,7 @@ static int handle_ept_misconfig(struct kvm_vcpu *vcpu)
 	}
 
 	ret = handle_mmio_page_fault(vcpu, gpa, true);
-	vcpu->arch.gpa_available = true;
+	vcpu->arch.gpa_available = !is_guest_mode(vcpu);
 	vcpu->arch.gpa_val = gpa;
 	if (likely(ret == RET_MMIO_PF_EMULATE))
 		return x86_emulate_instruction(vcpu, gpa, 0, NULL, 0) ==

Paolo

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

end of thread, other threads:[~2017-08-11 15:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-11  6:46 [PATCH v3] KVM: MMU: Fix guest stuck during boot due to read/write emulation against GVA Wanpeng Li
2017-08-11  6:53 ` David Hildenbrand
2017-08-11 15:01 ` Paolo Bonzini

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.