Skip to contentSkip to conversation

KVM: x86/MMU: Do not check unsync status for root SP.

PATCH Superseded Yu Zhang 2 participants 7 messages Feb 7, 2021 – Feb 9, 2021

Subsystems (3)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Yu Zhang

author
#0 lore raw

PATCH v2 1/1 KVM: x86/MMU: Do not check unsync status for root SP.

In shadow page table, only leaf SPs may be marked as unsync. And for non-leaf SPs, we use unsync_children to keep the number of the unsynced children. In kvm_mmu_sync_root(), sp->unsync shall always be zero for the root SP, , hence no need to check it. Instead, a warning inside mmu_sync_children() is added, in case someone incorrectly used it.

Also, clarify the mmu_need_write_protect(), by moving the warning into kvm_unsync_page().

---
Changes in V2:
- warnings added based on Sean's suggestion.

 arch/x86/kvm/mmu/mmu.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu/mmu.c +9 -3
index 86af582..c4797a00cc 100644
@@ -1995,6 +1995,12 @@ static void mmu_sync_children(struct kvm_vcpu *vcpu,
 	LIST_HEAD(invalid_list);
 	bool flush = false;
 
+	/*
+	 * Only 4k SPTEs can directly be made unsync, the parent pages
+	 * should never be unsyc'd.
+	 */
+	WARN_ON_ONCE(sp->unsync);
+
 	while (mmu_unsync_walk(parent, &pages)) {
 		bool protected = false;
 
@@ -2502,6 +2508,8 @@ int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
 
 static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
 {
+	WARN_ON(sp->role.level != PG_LEVEL_4K);
+
 	trace_kvm_mmu_unsync_page(sp);
 	++vcpu->kvm->stat.mmu_unsync;
 	sp->unsync = 1;
@@ -2524,7 +2532,6 @@ bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
 		if (sp->unsync)
 			continue;
 
-		WARN_ON(sp->role.level != PG_LEVEL_4K);
 		kvm_unsync_page(vcpu, sp);
 	}
 
@@ -3406,8 +3413,7 @@ void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
 		 * mmu_need_write_protect() describe what could go wrong if this
 		 * requirement isn't satisfied.
 		 */
-		if (!smp_load_acquire(&sp->unsync) &&
-		    !smp_load_acquire(&sp->unsync_children))
+		if (!smp_load_acquire(&sp->unsync_children))
 			return;
 
 		write_lock(&vcpu->kvm->mmu_lock);
Signature · 1 line
1.9.1
maintainer
#1 lore raw
Quoted 7 lines · Yu Zhang

In shadow page table, only leaf SPs may be marked as unsync. And for non-leaf SPs, we use unsync_children to keep the number of the unsynced children. In kvm_mmu_sync_root(), sp->unsync shall always be zero for the root SP, , hence no need to check it. Instead, a warning inside mmu_sync_children() is added, in case someone incorrectly used it.

Quoted 5 lines

Also, clarify the mmu_need_write_protect(), by moving the warning into kvm_unsync_page().

This should really be more of a Co-developed-by, and there are a couple adjustments that could be made in the commit message. I've queued the patch and I'll fix it up later.

Paolo

Quoted 52 lines

---
Changes in V2:
- warnings added based on Sean's suggestion.

  arch/x86/kvm/mmu/mmu.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu/mmu.c +9 -3
index 86af582..c4797a00cc 100644
@@ -1995,6 +1995,12 @@ static void mmu_sync_children(struct kvm_vcpu *vcpu,
  	LIST_HEAD(invalid_list);
  	bool flush = false;
 
+	/*
+	 * Only 4k SPTEs can directly be made unsync, the parent pages
+	 * should never be unsyc'd.
+	 */
+	WARN_ON_ONCE(sp->unsync);
+
  	while (mmu_unsync_walk(parent, &pages)) {
  		bool protected = false;
 
@@ -2502,6 +2508,8 @@ int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
 
  static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  {
+	WARN_ON(sp->role.level != PG_LEVEL_4K);
+
  	trace_kvm_mmu_unsync_page(sp);
  	++vcpu->kvm->stat.mmu_unsync;
  	sp->unsync = 1;
@@ -2524,7 +2532,6 @@ bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
  		if (sp->unsync)
  			continue;
 
-		WARN_ON(sp->role.level != PG_LEVEL_4K);
  		kvm_unsync_page(vcpu, sp);
  	}
 
@@ -3406,8 +3413,7 @@ void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
  		 * mmu_need_write_protect() describe what could go wrong if this
  		 * requirement isn't satisfied.
  		 */
-		if (!smp_load_acquire(&sp->unsync) &&
-		    !smp_load_acquire(&sp->unsync_children))
+		if (!smp_load_acquire(&sp->unsync_children))
  			return;
 
  		write_lock(&vcpu->kvm->mmu_lock);
author
#2 lore raw
Quoted 11 lines · Paolo Bonzini
Quoted 10 lines · Yu Zhang

In shadow page table, only leaf SPs may be marked as unsync. And for non-leaf SPs, we use unsync_children to keep the number of the unsynced children. In kvm_mmu_sync_root(), sp->unsync shall always be zero for the root SP, , hence no need to check it. Instead, a warning inside mmu_sync_children() is added, in case someone incorrectly used it.

Also, clarify the mmu_need_write_protect(), by moving the warning into kvm_unsync_page().

Quoted 6 lines
Quoted 2 lines

This should really be more of a Co-developed-by, and there are a couple adjustments that could be made in the commit message. I've queued the patch and I'll fix it up later.

Indeed. Thanks for the remind, and I'll pay attention in the future. :)

B.R.
Yu

Quoted 50 lines

Paolo

Quoted 46 lines

---
Changes in V2:
- warnings added based on Sean's suggestion.

  arch/x86/kvm/mmu/mmu.c | 12 +++++++++---
  1 file changed, 9 insertions(+), 3 deletions(-)
arch/x86/kvm/mmu/mmu.c +9 -3
index 86af582..c4797a00cc 100644
@@ -1995,6 +1995,12 @@ static void mmu_sync_children(struct kvm_vcpu *vcpu,
  	LIST_HEAD(invalid_list);
  	bool flush = false;
+	/*
+	 * Only 4k SPTEs can directly be made unsync, the parent pages
+	 * should never be unsyc'd.
+	 */
+	WARN_ON_ONCE(sp->unsync);
+
  	while (mmu_unsync_walk(parent, &pages)) {
  		bool protected = false;
@@ -2502,6 +2508,8 @@ int kvm_mmu_unprotect_page(struct kvm *kvm, gfn_t gfn)
  static void kvm_unsync_page(struct kvm_vcpu *vcpu, struct kvm_mmu_page *sp)
  {
+	WARN_ON(sp->role.level != PG_LEVEL_4K);
+
  	trace_kvm_mmu_unsync_page(sp);
  	++vcpu->kvm->stat.mmu_unsync;
  	sp->unsync = 1;
@@ -2524,7 +2532,6 @@ bool mmu_need_write_protect(struct kvm_vcpu *vcpu, gfn_t gfn,
  		if (sp->unsync)
  			continue;
-		WARN_ON(sp->role.level != PG_LEVEL_4K);
  		kvm_unsync_page(vcpu, sp);
  	}
@@ -3406,8 +3413,7 @@ void kvm_mmu_sync_roots(struct kvm_vcpu *vcpu)
  		 * mmu_need_write_protect() describe what could go wrong if this
  		 * requirement isn't satisfied.
  		 */
-		if (!smp_load_acquire(&sp->unsync) &&
-		    !smp_load_acquire(&sp->unsync_children))
+		if (!smp_load_acquire(&sp->unsync_children))
  			return;
  		write_lock(&vcpu->kvm->mmu_lock);
 
maintainer
#3 lore raw
Quoted 12 lines · Yu Zhang
Quoted 11 lines · Paolo Bonzini
Quoted 10 lines · Yu Zhang

In shadow page table, only leaf SPs may be marked as unsync. And for non-leaf SPs, we use unsync_children to keep the number of the unsynced children. In kvm_mmu_sync_root(), sp->unsync shall always be zero for the root SP, , hence no need to check it. Instead, a warning inside mmu_sync_children() is added, in case someone incorrectly used it.

Also, clarify the mmu_need_write_protect(), by moving the warning into kvm_unsync_page().

Quoted 8 lines
Quoted 6 lines
Quoted 2 lines

This should really be more of a Co-developed-by, and there are a couple adjustments that could be made in the commit message. I've queued the patch and I'll fix it up later.

Indeed. Thanks for the remind, and I'll pay attention in the future. :)

Also:

arch/x86/kvm/mmu/mmu.c: In function ‘mmu_sync_children’:
arch/x86/kvm/mmu/mmu.c:2002:17: error: ‘sp’ is used uninitialized in this function [-Werror=uninitialized]
WARN_ON_ONCE(sp->unsync);

so how was this tested?

Paolo

author
#4 lore raw
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines · Yu Zhang
Quoted 17 lines · Paolo Bonzini
Quoted 7 lines · Yu Zhang

In shadow page table, only leaf SPs may be marked as unsync. And for non-leaf SPs, we use unsync_children to keep the number of the unsynced children. In kvm_mmu_sync_root(), sp->unsync shall always be zero for the root SP, , hence no need to check it. Instead, a warning inside mmu_sync_children() is added, in case someone incorrectly used it.

Quoted 5 lines

Also, clarify the mmu_need_write_protect(), by moving the warning into kvm_unsync_page().

This should really be more of a Co-developed-by, and there are a couple adjustments that could be made in the commit message. I've queued the patch and I'll fix it up later.

Quoted 8 lines
Quoted 1 line

Indeed. Thanks for the remind, and I'll pay attention in the future. :)

Also:

arch/x86/kvm/mmu/mmu.c: In function ‘mmu_sync_children’:
arch/x86/kvm/mmu/mmu.c:2002:17: error: ‘sp’ is used uninitialized in this function [-Werror=uninitialized]
WARN_ON_ONCE(sp->unsync);

Oops. This is wrong. Should be WARN_ON_ONCE(parent->unsync);

Quoted 3 lines

so how was this tested?

I ran access test in kvm-unit-test for previous version, which hasn't this code(also in my local repo "enable_ept" was explicitly set to 0 in order to test the shadow mode). But I did not test this one. I'm truely sorry for the negligence - even trying to compile should make this happen!

Should we submit another version? Any suggestions on the test cases?

Thanks
Yu

Quoted 2 lines

Paolo

maintainer
#5 lore raw
Quoted 36 lines · Yu Zhang
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines · Yu Zhang
Quoted 17 lines · Paolo Bonzini
Quoted 7 lines · Yu Zhang

In shadow page table, only leaf SPs may be marked as unsync. And for non-leaf SPs, we use unsync_children to keep the number of the unsynced children. In kvm_mmu_sync_root(), sp->unsync shall always be zero for the root SP, , hence no need to check it. Instead, a warning inside mmu_sync_children() is added, in case someone incorrectly used it.

Quoted 5 lines

Also, clarify the mmu_need_write_protect(), by moving the warning into kvm_unsync_page().

This should really be more of a Co-developed-by, and there are a couple adjustments that could be made in the commit message. I've queued the patch and I'll fix it up later.

Quoted 8 lines
Quoted 1 line

Indeed. Thanks for the remind, and I'll pay attention in the future. :)

Also:

arch/x86/kvm/mmu/mmu.c: In function ‘mmu_sync_children’:
arch/x86/kvm/mmu/mmu.c:2002:17: error: ‘sp’ is used uninitialized in this function [-Werror=uninitialized]
WARN_ON_ONCE(sp->unsync);

Oops. This is wrong. Should be WARN_ON_ONCE(parent->unsync);

Quoted 3 lines

so how was this tested?

Quoted 7 lines

I ran access test in kvm-unit-test for previous version, which hasn't this code(also in my local repo "enable_ept" was explicitly set to 0 in order to test the shadow mode). But I did not test this one. I'm truely sorry for the negligence - even trying to compile should make this happen!

Should we submit another version? Any suggestions on the test cases?

Yes, please send v3.

The commit message can be:

In shadow page table, only leaf SPs may be marked as unsync; instead, for non-leaf SPs, we store the number of unsynced children in unsync_children. Therefore, in kvm_mmu_sync_root(), sp->unsync shall always be zero for the root SP and there is no need to check
it. Remove the check, and add a warning inside mmu_sync_children() to assert that the flags are used properly.

While at it, move the warning from mmu_need_write_protect() to kvm_unsync_page().

Paolo

author
#6 lore raw
Quoted 56 lines · Paolo Bonzini
Quoted 36 lines · Yu Zhang
Quoted 20 lines · Paolo Bonzini
Quoted 19 lines · Yu Zhang
Quoted 17 lines · Paolo Bonzini
Quoted 7 lines · Yu Zhang

In shadow page table, only leaf SPs may be marked as unsync. And for non-leaf SPs, we use unsync_children to keep the number of the unsynced children. In kvm_mmu_sync_root(), sp->unsync shall always be zero for the root SP, , hence no need to check it. Instead, a warning inside mmu_sync_children() is added, in case someone incorrectly used it.

Quoted 5 lines

Also, clarify the mmu_need_write_protect(), by moving the warning into kvm_unsync_page().

This should really be more of a Co-developed-by, and there are a couple adjustments that could be made in the commit message. I've queued the patch and I'll fix it up later.

Quoted 8 lines
Quoted 1 line

Indeed. Thanks for the remind, and I'll pay attention in the future. :)

Also:

arch/x86/kvm/mmu/mmu.c: In function ‘mmu_sync_children’:
arch/x86/kvm/mmu/mmu.c:2002:17: error: ‘sp’ is used uninitialized in this function [-Werror=uninitialized]
WARN_ON_ONCE(sp->unsync);

Oops. This is wrong. Should be WARN_ON_ONCE(parent->unsync);

Quoted 3 lines

so how was this tested?

Quoted 7 lines

I ran access test in kvm-unit-test for previous version, which hasn't this code(also in my local repo "enable_ept" was explicitly set to 0 in order to test the shadow mode). But I did not test this one. I'm truely sorry for the negligence - even trying to compile should make this happen!

Should we submit another version? Any suggestions on the test cases?

Yes, please send v3.

The commit message can be:

In shadow page table, only leaf SPs may be marked as unsync; instead, for non-leaf SPs, we store the number of unsynced children in unsync_children. Therefore, in kvm_mmu_sync_root(), sp->unsync
shall always be zero for the root SP and there is no need to check it. Remove the check, and add a warning inside mmu_sync_children() to assert that the flags are used properly.

Quoted 2 lines

While at it, move the warning from mmu_need_write_protect() to kvm_unsync_page().

Thanks Paolo. Will send out v3.

BTW, I just realized that mmu_sync_children() was not triggered by kvm-unit-test(the access.flat case), so I ran another test by running a regular VM using shadow, in which I witnessed the synchronization.

B.R.
Yu

Quoted 3 lines

Paolo