Skip to contentSkip to conversation

Some MMU related fix/clean up

PATCH Applied Sheng Yang 3 participants 7 messages May 12, 2010 – May 14, 2010 1 message not in the archive

Subsystems (3)
lore
Share & tools
lore

Sheng Yang

author
#0 lore raw

v2 cover Some MMU related fix/clean up

Sheng Yang (3):
KVM: x86: Check LMA bit before set_efer
KVM: Clean up duplicate assignment
VMX: x86: Only reset MMU when necessary

 arch/x86/kvm/mmu.c |    5 ++---
 arch/x86/kvm/x86.c |   22 ++++++++++++++++------
 2 files changed, 18 insertions(+), 9 deletions(-)
author
#1 lore raw

PATCH v2 1/3 KVM: x86: Check LMA bit before set_efer

kvm_x86_ops->set_efer() would execute vcpu->arch.efer = efer, so the checking of LMA bit didn't work.

---
 arch/x86/kvm/x86.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +2 -2
index cd8a606..2cae460 100644
@@ -715,11 +715,11 @@ static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
 			return 1;
 	}
 
-	kvm_x86_ops->set_efer(vcpu, efer);
-
 	efer &= ~EFER_LMA;
 	efer |= vcpu->arch.efer & EFER_LMA;
 
+	kvm_x86_ops->set_efer(vcpu, efer);
+
 	vcpu->arch.efer = efer;
 
 	vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
Signature · 1 line
1.7.0.1
author
#2 lore raw

PATCH v2 2/3 KVM: Clean up duplicate assignment

In reply to Sheng Yang

mmu.free() already set root_hpa to INVALID_PAGE, no need to do it again in the destory_kvm_mmu().

kvm_x86_ops->set_cr4() and set_efer() already assign cr4/efer to vcpu->arch.cr4/efer, no need to do it again later.

---
 arch/x86/kvm/mmu.c |    5 ++---
 arch/x86/kvm/x86.c |    4 +---
 2 files changed, 3 insertions(+), 6 deletions(-)
arch/x86/kvm/mmu.c +2 -3
index 95bee9d..5412185 100644
@@ -2450,10 +2450,9 @@ static int init_kvm_mmu(struct kvm_vcpu *vcpu)
 static void destroy_kvm_mmu(struct kvm_vcpu *vcpu)
 {
 	ASSERT(vcpu);
-	if (VALID_PAGE(vcpu->arch.mmu.root_hpa)) {
+	if (VALID_PAGE(vcpu->arch.mmu.root_hpa))
+		/* mmu.free() should set root_hpa = INVALID_PAGE */
 		vcpu->arch.mmu.free(vcpu);
-		vcpu->arch.mmu.root_hpa = INVALID_PAGE;
-	}
 }
 
 int kvm_mmu_reset_context(struct kvm_vcpu *vcpu)
arch/x86/kvm/x86.c +1 -3
index 2cae460..764f89b 100644
@@ -486,7 +486,7 @@ int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
 		return 1;
 
 	kvm_x86_ops->set_cr4(vcpu, cr4);
-	vcpu->arch.cr4 = cr4;
+
 	kvm_mmu_reset_context(vcpu);
 
 	return 0;
@@ -720,8 +720,6 @@ static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
 
 	kvm_x86_ops->set_efer(vcpu, efer);
 
-	vcpu->arch.efer = efer;
-
 	vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
 	kvm_mmu_reset_context(vcpu);
 
Signature · 1 line
1.7.0.1
author
#3 lore raw

PATCH v2 3/3 VMX: x86: Only reset MMU when necessary

In reply to Sheng Yang

Only modifying some bits of CR0/CR4 needs paging mode switch.

Modify EFER.NXE bit would result in reserved bit updates.

---
 arch/x86/kvm/x86.c |   16 ++++++++++++++--
 1 files changed, 14 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +14 -2
index 764f89b..f442da2 100644
@@ -416,6 +416,10 @@ out:
 
 static int __kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
 {
+	unsigned long old_cr0 = kvm_read_cr0(vcpu);
+	unsigned long update_bits = X86_CR0_PG | X86_CR0_WP |
+				    X86_CR0_CD | X86_CR0_NW;
+
 	cr0 |= X86_CR0_ET;
 
 #ifdef CONFIG_X86_64
@@ -449,7 +453,8 @@ static int __kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
 
 	kvm_x86_ops->set_cr0(vcpu, cr0);
 
-	kvm_mmu_reset_context(vcpu);
+	if ((cr0 ^ old_cr0) & update_bits)
+		kvm_mmu_reset_context(vcpu);
 	return 0;
 }
 
@@ -487,7 +492,8 @@ int __kvm_set_cr4(struct kvm_vcpu *vcpu, unsigned long cr4)
 
 	kvm_x86_ops->set_cr4(vcpu, cr4);
 
-	kvm_mmu_reset_context(vcpu);
+	if ((cr4 ^ old_cr4) & pdptr_bits)
+		kvm_mmu_reset_context(vcpu);
 
 	return 0;
 }
@@ -692,6 +698,8 @@ static u32 emulated_msrs[] = {
 
 static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
 {
+	u64 old_efer = vcpu->arch.efer;
+
 	if (efer & efer_reserved_bits)
 		return 1;
 
@@ -723,6 +731,10 @@ static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
 	vcpu->arch.mmu.base_role.nxe = (efer & EFER_NX) && !tdp_enabled;
 	kvm_mmu_reset_context(vcpu);
 
+	/* Update reserved bits */
+	if ((efer ^ old_efer) & EFER_NX)
+		kvm_mmu_reset_context(vcpu);
+
 	return 0;
 }
 
Signature · 1 line
1.7.0.1
#4 lore raw

Re: [PATCH][v2 3/3] VMX: x86: Only reset MMU when necessary

Quoted 14 lines · Sheng Yang

Only modifying some bits of CR0/CR4 needs paging mode switch.

Modify EFER.NXE bit would result in reserved bit updates.

---
  arch/x86/kvm/x86.c |   16 ++++++++++++++--
  1 files changed, 14 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 764f89b..f442da2 100644

arch/x86/kvm/x86.c out: · line 423

Quoted 8 lines
arch/x86/kvm/x86.c +4 -0
@@ -416,6 +416,10 @@ out:
 
  static int __kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
  {
+	unsigned long old_cr0 = kvm_read_cr0(vcpu);
+	unsigned long update_bits = X86_CR0_PG | X86_CR0_WP |
+				    X86_CR0_CD | X86_CR0_NW;
+
 

CR0_WP...

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#6 lore raw

Re: [PATCH][v2 3/3] VMX: x86: Only reset MMU when necessary

Quoted 23 lines · Sheng Yang
Quoted 21 lines · On Wednesday 12 May 2010 16:47:36 you wrote:
Quoted 19 lines · Sheng Yang

Only modifying some bits of CR0/CR4 needs paging mode switch.

Modify EFER.NXE bit would result in reserved bit updates.

---

   arch/x86/kvm/x86.c |   16 ++++++++++++++--
   1 files changed, 14 insertions(+), 2 deletions(-)
arch/x86/kvm/x86.c +0 -0
index 764f89b..f442da2 100644
@@ -416,6 +416,10 @@ out:
   static int __kvm_set_cr0(struct kvm_vcpu *vcpu, unsigned long cr0)
   {
 
Quoted 10 lines
Quoted 7 lines

arch/x86/kvm/x86.c out: · line 424

Quoted 5 lines
arch/x86/kvm/x86.c +4 -0
+	unsigned long old_cr0 = kvm_read_cr0(vcpu);
+	unsigned long update_bits = X86_CR0_PG | X86_CR0_WP |
+				    X86_CR0_CD | X86_CR0_NW;
+
 

CR0_WP...

?

Can't read, sorry.

Signature · 1 line
Do not meddle in the internals of kernels, for they are subtle and quick to panic.
#7 lore raw
In reply to Sheng Yang
Quoted 8 lines · Sheng Yang

Sheng Yang (3):
KVM: x86: Check LMA bit before set_efer
KVM: Clean up duplicate assignment
VMX: x86: Only reset MMU when necessary

 arch/x86/kvm/mmu.c |    5 ++---
 arch/x86/kvm/x86.c |   22 ++++++++++++++++------
 2 files changed, 18 insertions(+), 9 deletions(-)

Applied, thanks.