Skip to contentSkip to conversation

KVM/VMX: Check ept_pointer before flushing ept tlb

PATCH In review [email protected] 2 participants 4 messages Dec 6, 2018 – Dec 17, 2018

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/2 KVM/VMX: Check ept_pointer before flushing ept tlb

From: Lan Tianyu <[email protected]>

This patch is to initialize ept_pointer to INVALID_PAGE and check it before flushing ept tlb. If ept_pointer is invalid, bypass the flush request.

---
 arch/x86/kvm/vmx.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +11 -2
index c379d0bfdcba..6577ec8cbb0f 100644
@@ -1582,11 +1582,18 @@ static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
 	/*
 	 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
 	 * base of EPT PML4 table, strip off EPT configuration information.
+	 * If ept_pointer is invalid pointer, bypass the flush request.
 	 */
 	if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
-		kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
+
+			if (!VALID_PAGE(ept_pointer))
+				continue;
+
 			ret |= hyperv_flush_guest_mapping(
-				to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
+				ept_pointer & PAGE_MASK);
+		}
 	} else {
 		ret = hyperv_flush_guest_mapping(
 				to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
@@ -11614,6 +11621,8 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 	vmx->pi_desc.nv = POSTED_INTR_VECTOR;
 	vmx->pi_desc.sn = 1;
 
+	vmx->ept_pointer = INVALID_PAGE;
+
 	return &vmx->vcpu;
 
 free_vmcs:
Signature · 1 line
2.14.4
#1 lore raw

PATCH v2 2/2 KVM/VMX: Fix max line length problem in the vmx_hv_remote_flush_tlb()

From: Lan Tianyu <[email protected]>

Fix max line length problem.

---
 arch/x86/kvm/vmx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/vmx.c +1 -1
index 6577ec8cbb0f..2356118ea440 100644
@@ -1596,7 +1596,7 @@ static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
 		}
 	} else {
 		ret = hyperv_flush_guest_mapping(
-				to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
+			to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
 	}
 
 	spin_unlock(&to_kvm_vmx(kvm)->ept_pointer_lock);
Signature · 1 line
2.14.4
maintainer
#2 lore raw
In reply to [email protected]
Quoted 7 lines · On 06/12/18 08:34, [email protected] wrote:

From: Lan Tianyu <[email protected]>

This patch is to initialize ept_pointer to INVALID_PAGE and check it before flushing ept tlb. If ept_pointer is invalid, bypass the flush request.

Can you explain better *why* this patch is needed? Also, should vmx->ept_pointer be cleared at reset time, rather than vCPU creation?

Thanks,

Paolo

Quoted 39 lines
---
 arch/x86/kvm/vmx.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +11 -2
index c379d0bfdcba..6577ec8cbb0f 100644
@@ -1582,11 +1582,18 @@ static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
 	/*
 	 * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
 	 * base of EPT PML4 table, strip off EPT configuration information.
+	 * If ept_pointer is invalid pointer, bypass the flush request.
 	 */
 	if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
-		kvm_for_each_vcpu(i, vcpu, kvm)
+		kvm_for_each_vcpu(i, vcpu, kvm) {
+			u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
+
+			if (!VALID_PAGE(ept_pointer))
+				continue;
+
 			ret |= hyperv_flush_guest_mapping(
-				to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
+				ept_pointer & PAGE_MASK);
+		}
 	} else {
 		ret = hyperv_flush_guest_mapping(
 				to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
@@ -11614,6 +11621,8 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
 	vmx->pi_desc.nv = POSTED_INTR_VECTOR;
 	vmx->pi_desc.sn = 1;
 
+	vmx->ept_pointer = INVALID_PAGE;
+
 	return &vmx->vcpu;
 
 free_vmcs:
author
#3 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 7 lines · On 06/12/18 08:34, [email protected] wrote:

From: Lan Tianyu <[email protected]>

This patch is to initialize ept_pointer to INVALID_PAGE and check it before flushing ept tlb. If ept_pointer is invalid, bypass the flush request.

Can you explain better *why* this patch is needed?

Yes, hypercall still maybe called when ept_pointers aren't initialized. Such case happens
during guest boot up when BP works at first and APs aren't activated.

Quoted 2 lines

Also, should vmx->ept_pointer be cleared at reset time, rather than vCPU creation?

Yes, that make sense. Thanks for suggestion.

Quoted 44 lines

Thanks,

Paolo

Quoted 39 lines
---
 arch/x86/kvm/vmx.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)
arch/x86/kvm/vmx.c +11 -2
index c379d0bfdcba..6577ec8cbb0f 100644
@@ -1582,11 +1582,18 @@ static int vmx_hv_remote_flush_tlb(struct kvm *kvm)
      /*
       * FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE hypercall needs the address of the
       * base of EPT PML4 table, strip off EPT configuration information.
+      * If ept_pointer is invalid pointer, bypass the flush request.
       */
      if (to_kvm_vmx(kvm)->ept_pointers_match != EPT_POINTERS_MATCH) {
-             kvm_for_each_vcpu(i, vcpu, kvm)
+             kvm_for_each_vcpu(i, vcpu, kvm) {
+                     u64 ept_pointer = to_vmx(vcpu)->ept_pointer;
+
+                     if (!VALID_PAGE(ept_pointer))
+                             continue;
+
                      ret |= hyperv_flush_guest_mapping(
-                             to_vmx(kvm_get_vcpu(kvm, i))->ept_pointer & PAGE_MASK);
+                             ept_pointer & PAGE_MASK);
+             }
      } else {
              ret = hyperv_flush_guest_mapping(
                              to_vmx(kvm_get_vcpu(kvm, 0))->ept_pointer & PAGE_MASK);
@@ -11614,6 +11621,8 @@ static struct kvm_vcpu *vmx_create_vcpu(struct kvm *kvm, unsigned int id)
      vmx->pi_desc.nv = POSTED_INTR_VECTOR;
      vmx->pi_desc.sn = 1;
 
+     vmx->ept_pointer = INVALID_PAGE;
+
      return &vmx->vcpu;
 
 free_vmcs:
Signature · 2 lines
Best regards
Tianyu Lan