Skip to contentSkip to conversation

Simplify and fix fetch()

PATCH Superseded Avi Kivity 2 participants 22 messages Jul 11, 2010 – Jul 12, 2010

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

Avi Kivity

author
#0 lore raw

cover Simplify and fix fetch()

fetch() has become a monster, with a zillion continues, breaks, and gotos. Simplify it before Xiao adds even more.

Also fix the gpte validation race.

Avi Kivity (7):
KVM: MMU: Add link_shadow_page() helper
KVM: MMU: Use __set_spte to link shadow pages
KVM: MMU: Add drop_spte_if_large() helper
KVM: MMU: Add validate_direct_spte() helper
KVM: MMU: Add validate_indirect_spte() helper
KVM: MMU: Simplify spte fetch() function
KVM: MMU: Validate all gptes during fetch, not just those used for
new pages

 arch/x86/kvm/mmu.c         |   41 +++++++++++++
 arch/x86/kvm/paging_tmpl.h |  135 +++++++++++++++++++++-----------------------
 2 files changed, 105 insertions(+), 71 deletions(-)
author
#1 lore raw

PATCH 1/7 KVM: MMU: Add link_shadow_page() helper

To simplify the process of fetching an spte, add a helper that links a shadow page to an spte.

---
 arch/x86/kvm/mmu.c         |   10 ++++++++++
 arch/x86/kvm/paging_tmpl.h |    7 ++-----
 2 files changed, 12 insertions(+), 5 deletions(-)
arch/x86/kvm/mmu.c +10 -0
index b93b94f..ae35853 100644
@@ -1482,6 +1482,16 @@ static void shadow_walk_next(struct kvm_shadow_walk_iterator *iterator)
 	--iterator->level;
 }
 
+static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
+{
+	u64 spte;
+
+	spte = __pa(sp->spt)
+		| PT_PRESENT_MASK | PT_ACCESSED_MASK
+		| PT_WRITABLE_MASK | PT_USER_MASK;
+	*sptep = spte;
+}
+
 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
 					 struct kvm_mmu_page *sp)
 {
arch/x86/kvm/paging_tmpl.h +2 -5
index 6daeacf..4606c88 100644
@@ -309,7 +309,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 {
 	unsigned access = gw->pt_access;
 	struct kvm_mmu_page *sp;
-	u64 spte, *sptep = NULL;
+	u64 *sptep = NULL;
 	int direct;
 	gfn_t table_gfn;
 	int r;
@@ -394,10 +394,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 			}
 		}
 
-		spte = __pa(sp->spt)
-			| PT_PRESENT_MASK | PT_ACCESSED_MASK
-			| PT_WRITABLE_MASK | PT_USER_MASK;
-		*sptep = spte;
+		link_shadow_page(sptep, sp);
 	}
 
 	return sptep;
Signature · 1 line
1.7.1
Reviewed-by on v1 1/7
#2 lore raw

Re: [PATCH 1/7] KVM: MMU: Add link_shadow_page() helper

Quoted 3 lines · Avi Kivity

To simplify the process of fetching an spte, add a helper that links a shadow page to an spte.

author
#3 lore raw

PATCH 2/7 KVM: MMU: Use __set_spte to link shadow pages

In reply to Avi Kivity

To avoid split accesses to 64 bit sptes on i386, use __set_spte() to link shadow pages together.

(not technically required since shadow pages are __GFP_KERNEL, so upper 32 bits are always clear)

---
 arch/x86/kvm/mmu.c |    2 +-
 1 files changed, 1 insertions(+), 1 deletions(-)
arch/x86/kvm/mmu.c +1 -1
index ae35853..75cfb79 100644
@@ -1489,7 +1489,7 @@ static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
 	spte = __pa(sp->spt)
 		| PT_PRESENT_MASK | PT_ACCESSED_MASK
 		| PT_WRITABLE_MASK | PT_USER_MASK;
-	*sptep = spte;
+	__set_spte(sptep, spte);
 }
 
 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
Signature · 1 line
1.7.1
Reviewed-by on v1 2/7
#4 lore raw

Re: [PATCH 2/7] KVM: MMU: Use __set_spte to link shadow pages

Quoted 6 lines · Avi Kivity

To avoid split accesses to 64 bit sptes on i386, use __set_spte() to link shadow pages together.

(not technically required since shadow pages are __GFP_KERNEL, so upper 32 bits are always clear)

author
#5 lore raw

PATCH 3/7 KVM: MMU: Add drop_spte_if_large() helper

In reply to Avi Kivity

To clarify spte fetching code, move large spte handling into a helper.

---
 arch/x86/kvm/mmu.c         |    8 ++++++++
 arch/x86/kvm/paging_tmpl.h |    5 +----
 2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu.c +8 -0
index 75cfb79..c02cbe1 100644
@@ -1492,6 +1492,14 @@ static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
 	__set_spte(sptep, spte);
 }
 
+static void drop_spte_if_large(struct kvm_vcpu *vcpu, u64 *sptep)
+{
+	if (is_large_pte(*sptep)) {
+		drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
+		kvm_flush_remote_tlbs(vcpu->kvm);
+	}
+}
+
 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
 					 struct kvm_mmu_page *sp)
 {
arch/x86/kvm/paging_tmpl.h +1 -4
index 4606c88..2befea7 100644
@@ -360,10 +360,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 			kvm_flush_remote_tlbs(vcpu->kvm);
 		}
 
-		if (is_large_pte(*sptep)) {
-			drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
-			kvm_flush_remote_tlbs(vcpu->kvm);
-		}
+		drop_spte_if_large(vcpu, sptep);
 
 		if (level <= gw->level) {
 			direct = 1;
Signature · 1 line
1.7.1
#6 lore raw

Re: [PATCH 3/7] KVM: MMU: Add drop_spte_if_large() helper

Quoted 14 lines · Avi Kivity

To clarify spte fetching code, move large spte handling into a helper.

---
 arch/x86/kvm/mmu.c         |    8 ++++++++
 arch/x86/kvm/paging_tmpl.h |    5 +----
 2 files changed, 9 insertions(+), 4 deletions(-)
arch/x86/kvm/mmu.c +0 -0
index 75cfb79..c02cbe1 100644
@@ -1492,6 +1492,14 @@ static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp)
 	__set_spte(sptep, spte);

arch/x86/kvm/mmu.c static void link_shadow_page(u64 *sptep, struct kvm_mmu_page *sp) · line 1502

Quoted 10 lines
arch/x86/kvm/mmu.c +8 -0
 }
 
+static void drop_spte_if_large(struct kvm_vcpu *vcpu, u64 *sptep)
+{
+	if (is_large_pte(*sptep)) {
+		drop_spte(vcpu->kvm, sptep, shadow_trap_nonpresent_pte);
+		kvm_flush_remote_tlbs(vcpu->kvm);
+	}
+}
+

Maybe drop_large_spte() is better?

author
#7 lore raw

PATCH 4/7 KVM: MMU: Add validate_direct_spte() helper

In reply to Avi Kivity

Add a helper to verify that a direct shadow page is valid wrt the required access permissions; drop the page if it is not valid.

---
 arch/x86/kvm/mmu.c         |   23 +++++++++++++++++++++++
 arch/x86/kvm/paging_tmpl.h |   27 ++++++---------------------
 2 files changed, 29 insertions(+), 21 deletions(-)
arch/x86/kvm/mmu.c +23 -0
index c02cbe1..e2dfdba 100644
@@ -1500,6 +1500,29 @@ static void drop_spte_if_large(struct kvm_vcpu *vcpu, u64 *sptep)
 	}
 }
 
+static void validate_direct_spte(struct kvm_vcpu *vcpu, u64 *sptep,
+				   unsigned direct_access)
+{
+	if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
+		struct kvm_mmu_page *child;
+
+		/*
+		 * For the direct sp, if the guest pte's dirty bit
+		 * changed form clean to dirty, it will corrupt the
+		 * sp's access: allow writable in the read-only sp,
+		 * so we should update the spte at this point to get
+		 * a new sp with the correct access.
+		 */
+		child = page_header(*sptep & PT64_BASE_ADDR_MASK);
+		if (child->role.access == direct_access)
+			return;
+
+		mmu_page_remove_parent_pte(child, sptep);
+		__set_spte(sptep, shadow_trap_nonpresent_pte);
+		kvm_flush_remote_tlbs(vcpu->kvm);
+	}
+}
+
 static void kvm_mmu_page_unlink_children(struct kvm *kvm,
 					 struct kvm_mmu_page *sp)
 {
arch/x86/kvm/paging_tmpl.h +6 -21
index 2befea7..cacb4f2 100644
@@ -338,30 +338,15 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 			break;
 		}
 
-		if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)) {
-			struct kvm_mmu_page *child;
-
-			if (level != gw->level)
-				continue;
-
-			/*
-			 * For the direct sp, if the guest pte's dirty bit
-			 * changed form clean to dirty, it will corrupt the
-			 * sp's access: allow writable in the read-only sp,
-			 * so we should update the spte at this point to get
-			 * a new sp with the correct access.
-			 */
-			child = page_header(*sptep & PT64_BASE_ADDR_MASK);
-			if (child->role.access == direct_access)
-				continue;
-
-			mmu_page_remove_parent_pte(child, sptep);
-			__set_spte(sptep, shadow_trap_nonpresent_pte);
-			kvm_flush_remote_tlbs(vcpu->kvm);
-		}
+		if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)
+		    && level == gw->level)
+			validate_direct_spte(vcpu, sptep, direct_access);
 
 		drop_spte_if_large(vcpu, sptep);
 
+		if (is_shadow_present_pte(*sptep))
+			continue;
+
 		if (level <= gw->level) {
 			direct = 1;
 			access = direct_access;
Signature · 1 line
1.7.1
Reviewed-by on v1 4/7
#8 lore raw

Re: [PATCH 4/7] KVM: MMU: Add validate_direct_spte() helper

Quoted 3 lines · Avi Kivity

Add a helper to verify that a direct shadow page is valid wrt the required access permissions; drop the page if it is not valid.

author
#9 lore raw

PATCH 5/7 KVM: MMU: Add validate_indirect_spte() helper

In reply to Avi Kivity

Move the code to validate an indirect shadow page (by verifying that the gpte has not changed since it was fetched) into a helper.

---
 arch/x86/kvm/paging_tmpl.h |   29 ++++++++++++++++++++---------
 1 files changed, 20 insertions(+), 9 deletions(-)
arch/x86/kvm/paging_tmpl.h +20 -9
index cacb4f2..72f54fe 100644
@@ -299,6 +299,23 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
 		     gpte_to_gfn(gpte), pfn, true, true);
 }
 
+static bool FNAME(validate_indirect_spte)(struct kvm_vcpu *vcpu,
+					  u64 *sptep, struct kvm_mmu_page *sp,
+					  struct guest_walker *gw, int level)
+{
+	int r;
+	pt_element_t curr_pte;
+
+	r = kvm_read_guest_atomic(vcpu->kvm,
+				  gw->pte_gpa[level - 2],
+				  &curr_pte, sizeof(curr_pte));
+	if (r || curr_pte != gw->ptes[level - 2]) {
+		kvm_mmu_put_page(sp, sptep);
+		return false;
+	}
+	return true;
+}
+
 /*
  * Fetch a shadow pte for a specific level in the paging hierarchy.
  */
@@ -312,11 +329,9 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 	u64 *sptep = NULL;
 	int direct;
 	gfn_t table_gfn;
-	int r;
 	int level;
 	bool dirty = is_dirty_gpte(gw->ptes[gw->level - 1]);
 	unsigned direct_access;
-	pt_element_t curr_pte;
 	struct kvm_shadow_walk_iterator iterator;
 
 	if (!is_present_gpte(gw->ptes[gw->level - 1]))
@@ -364,17 +379,13 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 		}
 		sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
 					       direct, access, sptep);
-		if (!direct) {
-			r = kvm_read_guest_atomic(vcpu->kvm,
-						  gw->pte_gpa[level - 2],
-						  &curr_pte, sizeof(curr_pte));
-			if (r || curr_pte != gw->ptes[level - 2]) {
-				kvm_mmu_put_page(sp, sptep);
+		if (!direct)
+			if (!FNAME(validate_indirect_spte)(vcpu, sptep, sp,
+							   gw, level)) {
 				kvm_release_pfn_clean(pfn);
 				sptep = NULL;
 				break;
 			}
-		}
 
 		link_shadow_page(sptep, sp);
 	}
Signature · 1 line
1.7.1
Reviewed-by on v1 5/7
#10 lore raw

Re: [PATCH 5/7] KVM: MMU: Add validate_indirect_spte() helper

Quoted 3 lines · Avi Kivity

Move the code to validate an indirect shadow page (by verifying that the gpte has not changed since it was fetched) into a helper.

#11 lore raw

Re: [PATCH 5/7] KVM: MMU: Add validate_indirect_spte() helper

In reply to Avi Kivity
Quoted 19 lines · Avi Kivity

Move the code to validate an indirect shadow page (by verifying that the gpte has not changed since it was fetched) into a helper.

---
 arch/x86/kvm/paging_tmpl.h |   29 ++++++++++++++++++++---------
 1 files changed, 20 insertions(+), 9 deletions(-)
arch/x86/kvm/paging_tmpl.h +3 -0
index cacb4f2..72f54fe 100644
@@ -299,6 +299,23 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
 		     gpte_to_gfn(gpte), pfn, true, true);
 }
 
+static bool FNAME(validate_indirect_spte)(struct kvm_vcpu *vcpu,
+					  u64 *sptep, struct kvm_mmu_page *sp,
+					  struct guest_walker *gw, int level)

arch/x86/kvm/paging_tmpl.h static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp, · line 314

Quoted 10 lines
arch/x86/kvm/paging_tmpl.h +10 -0
+{
+	int r;
+	pt_element_t curr_pte;
+
+	r = kvm_read_guest_atomic(vcpu->kvm,
+				  gw->pte_gpa[level - 2],
+				  &curr_pte, sizeof(curr_pte));
+	if (r || curr_pte != gw->ptes[level - 2]) {
+		kvm_mmu_put_page(sp, sptep);
+		return false;

I think it's 'level - 1' here for checking 'level''s mapping

author
#12 lore raw

Re: [PATCH 5/7] KVM: MMU: Add validate_indirect_spte() helper

Quoted 19 lines · Xiao Guangrong
Quoted 16 lines · Avi Kivity

Move the code to validate an indirect shadow page (by verifying that the gpte has not changed since it was fetched) into a helper.

---
  arch/x86/kvm/paging_tmpl.h |   29 ++++++++++++++++++++---------
  1 files changed, 20 insertions(+), 9 deletions(-)
arch/x86/kvm/paging_tmpl.h +0 -0
index cacb4f2..72f54fe 100644
@@ -299,6 +299,23 @@ static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,
  		     gpte_to_gfn(gpte), pfn, true, true);
  }
 
Quoted 16 lines
Quoted 4 lines
arch/x86/kvm/paging_tmpl.h +4 -0
+static bool FNAME(validate_indirect_spte)(struct kvm_vcpu *vcpu,
+					  u64 *sptep, struct kvm_mmu_page *sp,
+					  struct guest_walker *gw, int level)
+{

arch/x86/kvm/paging_tmpl.h static void FNAME(update_pte)(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp,

Quoted 10 lines
arch/x86/kvm/paging_tmpl.h +9 -0
+	int r;
+	pt_element_t curr_pte;
+
+	r = kvm_read_guest_atomic(vcpu->kvm,
+				  gw->pte_gpa[level - 2],
+				&curr_pte, sizeof(curr_pte));
+	if (r || curr_pte != gw->ptes[level - 2]) {
+		kvm_mmu_put_page(sp, sptep);
+		return false;
 

I think it's 'level - 1' here for checking 'level''s mapping

Well, I'm just moving code around. If it needs to be fixed, that's another patch.

I think the logic is, we just write-protected the shadow page for level L-1, so we need to verify that the gpte we write protected hasn't changed. That gpte is in level L - 1, and it is stored in gw->gptes[level - 2].

Of course, that logic is wrong, we need to check all gptes, since between the guest walk and FNAME(fetch)() the shadow pages and gptes may have changed several times. I'll fix that later.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#13 lore raw

PATCH 6/7 KVM: MMU: Simplify spte fetch() function

In reply to Avi Kivity

Paritition the function into three sections:

- fetching indirect shadow pages (host_level > guest_level)
- fetching direct shadow pages (page_level < host_level <= guest_level)
- the final spte (page_level == host_level)

Instead of the current spaghetti.

---
 arch/x86/kvm/paging_tmpl.h |   77 ++++++++++++++++++++++---------------------
 1 files changed, 39 insertions(+), 38 deletions(-)
arch/x86/kvm/paging_tmpl.h +39 -38
index 72f54fe..a7f8295 100644
@@ -327,9 +327,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 	unsigned access = gw->pt_access;
 	struct kvm_mmu_page *sp;
 	u64 *sptep = NULL;
-	int direct;
-	gfn_t table_gfn;
-	int level;
+	int uninitialized_var(level);
 	bool dirty = is_dirty_gpte(gw->ptes[gw->level - 1]);
 	unsigned direct_access;
 	struct kvm_shadow_walk_iterator iterator;
@@ -341,55 +339,58 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 	if (!dirty)
 		direct_access &= ~ACC_WRITE_MASK;
 
-	for_each_shadow_entry(vcpu, addr, iterator) {
+	for (shadow_walk_init(&iterator, vcpu, addr);
+	     shadow_walk_okay(&iterator) && iterator.level > gw->level;
+	     shadow_walk_next(&iterator)) {
+		gfn_t table_gfn;
+
 		level = iterator.level;
 		sptep = iterator.sptep;
-		if (iterator.level == hlevel) {
-			mmu_set_spte(vcpu, sptep, access,
-				     gw->pte_access & access,
-				     user_fault, write_fault,
-				     dirty, ptwrite, level,
-				     gw->gfn, pfn, false, true);
-			break;
+
+		drop_spte_if_large(vcpu, sptep);
+
+		if (is_shadow_present_pte(*sptep))
+			continue;
+
+		table_gfn = gw->table_gfn[level - 2];
+		sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
+				      false, access, sptep);
+
+		if (!FNAME(validate_indirect_spte)(vcpu, sptep, sp,
+						   gw, level)) {
+			kvm_release_pfn_clean(pfn);
+			return NULL;
 		}
 
-		if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)
-		    && level == gw->level)
-			validate_direct_spte(vcpu, sptep, direct_access);
+		link_shadow_page(sptep, sp);
+	}
+
+	for (;
+	     shadow_walk_okay(&iterator) && iterator.level > hlevel;
+	     shadow_walk_next(&iterator)) {
+		gfn_t direct_gfn;
+
+		level = iterator.level;
+		sptep = iterator.sptep;
 
 		drop_spte_if_large(vcpu, sptep);
 
 		if (is_shadow_present_pte(*sptep))
 			continue;
 
-		if (level <= gw->level) {
-			direct = 1;
-			access = direct_access;
-
-			/*
-			 * It is a large guest pages backed by small host pages,
-			 * So we set @direct(@sp->role.direct)=1, and set
-			 * @table_gfn(@sp->gfn)=the base page frame for linear
-			 * translations.
-			 */
-			table_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
-		} else {
-			direct = 0;
-			table_gfn = gw->table_gfn[level - 2];
-		}
-		sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
-					       direct, access, sptep);
-		if (!direct)
-			if (!FNAME(validate_indirect_spte)(vcpu, sptep, sp,
-							   gw, level)) {
-				kvm_release_pfn_clean(pfn);
-				sptep = NULL;
-				break;
-			}
+		validate_direct_spte(vcpu, sptep, direct_access);
+
+		direct_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
 
+		sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, level-1,
+				      true, direct_access, sptep);
 		link_shadow_page(sptep, sp);
 	}
 
+	mmu_set_spte(vcpu, sptep, access, gw->pte_access & access,
+		     user_fault, write_fault, dirty, ptwrite, level,
+		     gw->gfn, pfn, false, true);
+
 	return sptep;
 }
 
Signature · 1 line
1.7.1
author
#14 lore raw

Re: [PATCH 6/7] KVM: MMU: Simplify spte fetch() function

Quoted 47 lines · Avi Kivity

Paritition the function into three sections:

- fetching indirect shadow pages (host_level> guest_level)
- fetching direct shadow pages (page_level< host_level<= guest_level)
- the final spte (page_level == host_level)

Instead of the current spaghetti.

arch/x86/kvm/paging_tmpl.h +8 -22
+
+	for (;
+	     shadow_walk_okay(&iterator)&&  iterator.level>  hlevel;
+	     shadow_walk_next(&iterator)) {
+		gfn_t direct_gfn;
+
+		level = iterator.level;
+		sptep = iterator.sptep;
 
  		drop_spte_if_large(vcpu, sptep);
 
  		if (is_shadow_present_pte(*sptep))
  			continue;
 
-		if (level<= gw->level) {
-			direct = 1;
-			access = direct_access;
-
-			/*
-			 * It is a large guest pages backed by small host pages,
-			 * So we set @direct(@sp->role.direct)=1, and set
-			 * @table_gfn(@sp->gfn)=the base page frame for linear
-			 * translations.
-			 */
-			table_gfn = gw->gfn&  ~(KVM_PAGES_PER_HPAGE(level) - 1);
-		} else {
-			direct = 0;
-			table_gfn = gw->table_gfn[level - 2];
-		}
-		sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
-					       direct, access, sptep);
-		if (!direct)
-			if (!FNAME(validate_indirect_spte)(vcpu, sptep, sp,
-							   gw, level)) {
-				kvm_release_pfn_clean(pfn);
-				sptep = NULL;

arch/x86/kvm/paging_tmpl.h static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, · line 388

Quoted 10 lines
arch/x86/kvm/paging_tmpl.h +5 -2
-				break;
-			}
+		validate_direct_spte(vcpu, sptep, direct_access);
+
+		direct_gfn = gw->gfn&  ~(KVM_PAGES_PER_HPAGE(level) - 1);
 
+		sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, level-1,
+				      true, direct_access, sptep);
  		link_shadow_page(sptep, sp);
  	}

validate_direct_spte() should be before the 'continue' above, probably.

Signature · 1 line
error compiling committee.c: too many arguments to function
#15 lore raw

Re: [PATCH 6/7] KVM: MMU: Simplify spte fetch() function

Quoted 5 lines · Avi Kivity
Quoted 5 lines
arch/x86/kvm/paging_tmpl.h +3 -1
-            }
+        validate_direct_spte(vcpu, sptep, direct_access);
+
+        direct_gfn = gw->gfn&  ~(KVM_PAGES_PER_HPAGE(level) - 1);
Quoted 9 lines

arch/x86/kvm/paging_tmpl.h static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, · line 388

Quoted 6 lines
arch/x86/kvm/paging_tmpl.h +2 -0
+        sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, level-1,
+                      true, direct_access, sptep);
          link_shadow_page(sptep, sp);
      }

validate_direct_spte() should be before the 'continue' above, probably.

Sure :-)

author
#16 lore raw

Re: [PATCH 6/7] KVM: MMU: Simplify spte fetch() function

Quoted 16 lines · Xiao Guangrong
Quoted 12 lines · Avi Kivity
Quoted 12 lines
arch/x86/kvm/paging_tmpl.h +5 -1
-            }
+        validate_direct_spte(vcpu, sptep, direct_access);
+
+        direct_gfn = gw->gfn&   ~(KVM_PAGES_PER_HPAGE(level) - 1);
 
+        sp = kvm_mmu_get_page(vcpu, direct_gfn, addr, level-1,
+                      true, direct_access, sptep);
           link_shadow_page(sptep, sp);
       }
Quoted 5 lines
Quoted 3 lines

validate_direct_spte() should be before the 'continue' above, probably.

Sure :-)

Actually, I think this matches current code. So I'll change it in a separate patch.

Signature · 1 line
error compiling committee.c: too many arguments to function
#17 lore raw

Re: [PATCH 6/7] KVM: MMU: Simplify spte fetch() function

In reply to Avi Kivity
Quoted 3 lines · Avi Kivity
arch/x86/kvm/paging_tmpl.h +0 -3
-		if (is_shadow_present_pte(*sptep) && !is_large_pte(*sptep)
-		    && level == gw->level)
-			validate_direct_spte(vcpu, sptep, direct_access);

......

Quoted 31 lines
arch/x86/kvm/paging_tmpl.h +10 -15
+		link_shadow_page(sptep, sp);
+	}
+
+	for (;
+	     shadow_walk_okay(&iterator) && iterator.level > hlevel;
+	     shadow_walk_next(&iterator)) {
+		gfn_t direct_gfn;
+
+		level = iterator.level;
+		sptep = iterator.sptep;
 
 		drop_spte_if_large(vcpu, sptep);
 
 		if (is_shadow_present_pte(*sptep))
 			continue;
 
-		if (level <= gw->level) {
-			direct = 1;
-			access = direct_access;
-
-			/*
-			 * It is a large guest pages backed by small host pages,
-			 * So we set @direct(@sp->role.direct)=1, and set
-			 * @table_gfn(@sp->gfn)=the base page frame for linear
-			 * translations.
-			 */
-			table_gfn = gw->gfn & ~(KVM_PAGES_PER_HPAGE(level) - 1);
-		} else {
-			direct = 0;
-			table_gfn = gw->table_gfn[level - 2];
-		}

arch/x86/kvm/paging_tmpl.h static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, · line 381

Quoted 10 lines
arch/x86/kvm/paging_tmpl.h +1 -9
-		sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
-					       direct, access, sptep);
-		if (!direct)
-			if (!FNAME(validate_indirect_spte)(vcpu, sptep, sp,
-							   gw, level)) {
-				kvm_release_pfn_clean(pfn);
-				sptep = NULL;
-				break;
-			}
+		validate_direct_spte(vcpu, sptep, direct_access);

Need validate_direct_spte() only when 'level == gw->level'

author
#18 lore raw

Re: [PATCH 6/7] KVM: MMU: Simplify spte fetch() function

Quoted 46 lines · Xiao Guangrong
Quoted 4 lines · Avi Kivity
arch/x86/kvm/paging_tmpl.h +0 -3
-		if (is_shadow_present_pte(*sptep)&&  !is_large_pte(*sptep)
-		&&  level == gw->level)
-			validate_direct_spte(vcpu, sptep, direct_access);

......

Quoted 35 lines
arch/x86/kvm/paging_tmpl.h +10 -19
+		link_shadow_page(sptep, sp);
+	}
+
+	for (;
+	     shadow_walk_okay(&iterator)&&  iterator.level>  hlevel;
+	     shadow_walk_next(&iterator)) {
+		gfn_t direct_gfn;
+
+		level = iterator.level;
+		sptep = iterator.sptep;
 
  		drop_spte_if_large(vcpu, sptep);
 
  		if (is_shadow_present_pte(*sptep))
  			continue;
 
-		if (level<= gw->level) {
-			direct = 1;
-			access = direct_access;
-
-			/*
-			 * It is a large guest pages backed by small host pages,
-			 * So we set @direct(@sp->role.direct)=1, and set
-			 * @table_gfn(@sp->gfn)=the base page frame for linear
-			 * translations.
-			 */
-			table_gfn = gw->gfn&  ~(KVM_PAGES_PER_HPAGE(level) - 1);
-		} else {
-			direct = 0;
-			table_gfn = gw->table_gfn[level - 2];
-		}
-		sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
-					       direct, access, sptep);
-		if (!direct)
-			if (!FNAME(validate_indirect_spte)(vcpu, sptep, sp,
Quoted 10 lines

arch/x86/kvm/paging_tmpl.h static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, · line 381

Quoted 7 lines
arch/x86/kvm/paging_tmpl.h +1 -5
-							   gw, level)) {
-				kvm_release_pfn_clean(pfn);
-				sptep = NULL;
-				break;
-			}
+		validate_direct_spte(vcpu, sptep, direct_access);

Need validate_direct_spte() only when 'level == gw->level'

That is true (and this is a change from the current code), but level < gw->level will be very rare (1G pages backed by 4k pages) so I don't mind the extra check.

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#19 lore raw

PATCH 7/7 KVM: MMU: Validate all gptes during fetch, not just those used for new pages

In reply to Avi Kivity

Currently, when we fetch an spte, we only verify that gptes match those that the walker saw if we build new shadow pages for them.

However, this misses the following race:

vcpu1 vcpu2

walk
change gpte
walk
instantiate sp

fetch existing sp

Fix by validating every gpte, regardless of whether it is used for building a new sp or not.

---
 arch/x86/kvm/paging_tmpl.h |   18 ++++++++++--------
 1 files changed, 10 insertions(+), 8 deletions(-)
arch/x86/kvm/paging_tmpl.h +10 -8
index a7f8295..4bbd0c7 100644
@@ -325,7 +325,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 			 int *ptwrite, pfn_t pfn)
 {
 	unsigned access = gw->pt_access;
-	struct kvm_mmu_page *sp;
+	struct kvm_mmu_page *uninitialized_var(sp);
 	u64 *sptep = NULL;
 	int uninitialized_var(level);
 	bool dirty = is_dirty_gpte(gw->ptes[gw->level - 1]);
@@ -343,18 +343,19 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 	     shadow_walk_okay(&iterator) && iterator.level > gw->level;
 	     shadow_walk_next(&iterator)) {
 		gfn_t table_gfn;
+		bool new_page = false;
 
 		level = iterator.level;
 		sptep = iterator.sptep;
 
 		drop_spte_if_large(vcpu, sptep);
 
-		if (is_shadow_present_pte(*sptep))
-			continue;
-
-		table_gfn = gw->table_gfn[level - 2];
-		sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
-				      false, access, sptep);
+		if (!is_shadow_present_pte(*sptep)) {
+			table_gfn = gw->table_gfn[level - 2];
+			sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
+					      false, access, sptep);
+			new_page = true;
+		}
 
 		if (!FNAME(validate_indirect_spte)(vcpu, sptep, sp,
 						   gw, level)) {
@@ -362,7 +363,8 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
 			return NULL;
 		}
 
-		link_shadow_page(sptep, sp);
+		if (new_page)
+			link_shadow_page(sptep, sp);
 	}
 
 	for (;
Signature · 1 line
1.7.1
Reviewed-by on v1 7/7
#20 lore raw

Re: [PATCH 7/7] KVM: MMU: Validate all gptes during fetch, not just those used for new pages

Quoted 12 lines · Avi Kivity

Currently, when we fetch an spte, we only verify that gptes match those that the walker saw if we build new shadow pages for them.

However, this misses the following race:

vcpu1 vcpu2

walk
change gpte
walk
instantiate sp

Quoted 5 lines

fetch existing sp

Fix by validating every gpte, regardless of whether it is used for building a new sp or not.

author
#21 lore raw

Re: [PATCH 7/7] KVM: MMU: Validate all gptes during fetch, not just those used for new pages

In reply to Avi Kivity
Quoted 39 lines · Avi Kivity

Currently, when we fetch an spte, we only verify that gptes match those that the walker saw if we build new shadow pages for them.

However, this misses the following race:

vcpu1 vcpu2

walk
change gpte
walk
instantiate sp

fetch existing sp

Fix by validating every gpte, regardless of whether it is used for building a new sp or not.

---
  arch/x86/kvm/paging_tmpl.h |   18 ++++++++++--------
  1 files changed, 10 insertions(+), 8 deletions(-)
arch/x86/kvm/paging_tmpl.h +1 -1
index a7f8295..4bbd0c7 100644
@@ -325,7 +325,7 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  			 int *ptwrite, pfn_t pfn)
  {
  	unsigned access = gw->pt_access;
-	struct kvm_mmu_page *sp;
+	struct kvm_mmu_page *uninitialized_var(sp);
  	u64 *sptep = NULL;
  	int uninitialized_var(level);
  	bool dirty = is_dirty_gpte(gw->ptes[gw->level - 1]);
@@ -343,18 +343,19 @@ static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr,
  	     shadow_walk_okay(&iterator)&&  iterator.level>  gw->level;
  	     shadow_walk_next(&iterator)) {
  		gfn_t table_gfn;

arch/x86/kvm/paging_tmpl.h static u64 *FNAME(fetch)(struct kvm_vcpu *vcpu, gva_t addr, · line 353

Quoted 10 lines
arch/x86/kvm/paging_tmpl.h +1 -2
+		bool new_page = false;
 
  		level = iterator.level;
  		sptep = iterator.sptep;
 
  		drop_spte_if_large(vcpu, sptep);
 
-		if (is_shadow_present_pte(*sptep))
-			continue;
 

See, this gets dropped.

Quoted 12 lines
arch/x86/kvm/paging_tmpl.h +6 -4
-
-		table_gfn = gw->table_gfn[level - 2];
-		sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
-				      false, access, sptep);
+		if (!is_shadow_present_pte(*sptep)) {
+			table_gfn = gw->table_gfn[level - 2];
+			sp = kvm_mmu_get_page(vcpu, table_gfn, addr, level-1,
+					      false, access, sptep);
+			new_page = true;
+		}
 
  		if (!FNAME(validate_indirect_spte)(vcpu, sptep, sp,
Quoted 2 lines
  						   gw, level)) {

Now we need to change validate_indirect_spte() to account for all levels.

Signature · 1 line
error compiling committee.c: too many arguments to function