Skip to contentSkip to conversation

kvm: split kvm_arch_flush_shadow and move mmio spte invalidation to x86

PATCH Applied Marcelo Tosatti 6 participants 13 messages Aug 24, 2012 – Sep 6, 2012

Subsystems (7)
x86 KVM PowerPC s390 +3 more subsystems
lore
Share & tools
lore
#0 lore raw

cover kvm: split kvm_arch_flush_shadow and move mmio spte invalidation to x86

Paul, does that work for you.

Avi, Xiao, please review.

#1 lore raw

PATCH 1/3 KVM: split kvm_arch_flush_shadow

Introducing kvm_arch_flush_shadow_memslot, to invalidate the translations of a single memory slot.

include/linux/kvm_host.h +5 -1
Index: kvm/include/linux/kvm_host.h
===================================================================
@@ -458,7 +458,11 @@ void kvm_arch_commit_memory_region(struc
 				int user_alloc);
 bool kvm_largepages_enabled(void);
 void kvm_disable_largepages(void);
-void kvm_arch_flush_shadow(struct kvm *kvm);
+/* flush all memory translations */
+void kvm_arch_flush_shadow_all(struct kvm *kvm);
+/* flush memory translations pointing to 'slot' */
+void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
+				   struct kvm_memory_slot *slot);
 
 int gfn_to_page_many_atomic(struct kvm *kvm, gfn_t gfn, struct page **pages,
 			    int nr_pages);
virt/kvm/kvm_main.c +4 -4
Index: kvm/virt/kvm/kvm_main.c
===================================================================
@@ -408,7 +408,7 @@ static void kvm_mmu_notifier_release(str
 	int idx;
 
 	idx = srcu_read_lock(&kvm->srcu);
-	kvm_arch_flush_shadow(kvm);
+	kvm_arch_flush_shadow_all(kvm);
 	srcu_read_unlock(&kvm->srcu, idx);
 }
 
@@ -582,7 +582,7 @@ static void kvm_destroy_vm(struct kvm *k
 #if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
 	mmu_notifier_unregister(&kvm->mmu_notifier, kvm->mm);
 #else
-	kvm_arch_flush_shadow(kvm);
+	kvm_arch_flush_shadow_all(kvm);
 #endif
 	kvm_arch_destroy_vm(kvm);
 	kvm_free_physmem(kvm);
@@ -814,7 +814,7 @@ int __kvm_set_memory_region(struct kvm *
 		 * 	- gfn_to_hva (kvm_read_guest, gfn_to_pfn)
 		 * 	- kvm_is_visible_gfn (mmu_check_roots)
 		 */
-		kvm_arch_flush_shadow(kvm);
+		kvm_arch_flush_shadow_memslot(kvm, slot);
 		kfree(old_memslots);
 	}
 
@@ -854,7 +854,7 @@ int __kvm_set_memory_region(struct kvm *
 	 * mmio sptes.
 	 */
 	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
-		kvm_arch_flush_shadow(kvm);
+		kvm_arch_flush_shadow_all(kvm);
 
 	kvm_free_physmem_slot(&old, &new);
 	kfree(old_memslots);
arch/ia64/kvm/kvm-ia64.c +7 -1
Index: kvm/arch/ia64/kvm/kvm-ia64.c
===================================================================
@@ -1613,11 +1613,17 @@ void kvm_arch_commit_memory_region(struc
 	return;
 }
 
-void kvm_arch_flush_shadow(struct kvm *kvm)
+void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
 	kvm_flush_remote_tlbs(kvm);
 }
 
+void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
+				   struct kvm_memory_slot *slot)
+{
+	kvm_arch_flush_shadow_all();
+}
+
 long kvm_arch_dev_ioctl(struct file *filp,
 			unsigned int ioctl, unsigned long arg)
 {
arch/powerpc/kvm/powerpc.c +5 -1
Index: kvm/arch/powerpc/kvm/powerpc.c
===================================================================
@@ -334,8 +334,12 @@ void kvm_arch_commit_memory_region(struc
 	kvmppc_core_commit_memory_region(kvm, mem);
 }
 
+void kvm_arch_flush_shadow_all(struct kvm *kvm)
+{
+}
 
-void kvm_arch_flush_shadow(struct kvm *kvm)
+void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
+				   struct kvm_memory_slot *slot)
 {
 }
 
arch/s390/kvm/kvm-s390.c +6 -1
Index: kvm/arch/s390/kvm/kvm-s390.c
===================================================================
@@ -969,7 +969,12 @@ void kvm_arch_commit_memory_region(struc
 	return;
 }
 
-void kvm_arch_flush_shadow(struct kvm *kvm)
+void kvm_arch_flush_shadow_all(struct kvm *kvm)
+{
+}
+
+void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
+				   struct kvm_memory_slot *slot)
 {
 }
 
arch/x86/kvm/x86.c +7 -1
Index: kvm/arch/x86/kvm/x86.c
===================================================================
@@ -6457,12 +6457,18 @@ void kvm_arch_commit_memory_region(struc
 	spin_unlock(&kvm->mmu_lock);
 }
 
-void kvm_arch_flush_shadow(struct kvm *kvm)
+void kvm_arch_flush_shadow_all(struct kvm *kvm)
 {
 	kvm_mmu_zap_all(kvm);
 	kvm_reload_remote_mmus(kvm);
 }
 
+void kvm_arch_flush_shadow_memslot(struct kvm *kvm,
+				   struct kvm_memory_slot *slot)
+{
+	kvm_arch_flush_shadow_all(kvm);
+}
+
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 {
 	return (vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE &&
#2 lore raw

PATCH 2/3 KVM: perform an invalid memslot step for gpa base change

In reply to Marcelo Tosatti

PPC must flush all translations before the new memory slot is visible.

virt/kvm/kvm_main.c +3 -3
Index: kvm/virt/kvm/kvm_main.c
===================================================================
@@ -791,7 +791,7 @@ int __kvm_set_memory_region(struct kvm *
 		/* destroy any largepage mappings for dirty tracking */
 	}
 
-	if (!npages) {
+	if (!npages || base_gfn != old.base_gfn) {
 		struct kvm_memory_slot *slot;
 
 		r = -ENOMEM;
@@ -807,8 +807,8 @@ int __kvm_set_memory_region(struct kvm *
 		old_memslots = kvm->memslots;
 		rcu_assign_pointer(kvm->memslots, slots);
 		synchronize_srcu_expedited(&kvm->srcu);
-		/* From this point no new shadow pages pointing to a deleted
-		 * memslot will be created.
+		/* From this point no new shadow pages pointing to a deleted,
+ 		 * or moved, memslot will be created.
 		 *
 		 * validation of sp->gfn happens in:
 		 * 	- gfn_to_hva (kvm_read_guest, gfn_to_pfn)
#3 lore raw

PATCH 3/3 KVM: move postcommit flush to x86, as mmio sptes are x86 specific

In reply to Marcelo Tosatti

Other arches do not need this.

arch/x86/kvm/x86.c +8 -0
Index: kvm/arch/x86/kvm/x86.c
===================================================================
@@ -6455,6 +6455,14 @@ void kvm_arch_commit_memory_region(struc
 		kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
 	kvm_mmu_slot_remove_write_access(kvm, mem->slot);
 	spin_unlock(&kvm->mmu_lock);
+	/*
+	 * If the new memory slot is created, we need to clear all
+	 * mmio sptes.
+	 */
+	if (old.npages == 0 && npages) {
+		kvm_mmu_zap_all(kvm);
+		kvm_reload_remote_mmus(kvm);
+	}
 }
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
virt/kvm/kvm_main.c +0 -7
Index: kvm/virt/kvm/kvm_main.c
===================================================================
@@ -849,13 +849,6 @@ int __kvm_set_memory_region(struct kvm *
 
 	kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
 
-	/*
-	 * If the new memory slot is created, we need to clear all
-	 * mmio sptes.
-	 */
-	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
-		kvm_arch_flush_shadow_all(kvm);
-
 	kvm_free_physmem_slot(&old, &new);
 	kfree(old_memslots);
 
#4 lore raw

Re: [patch 3/3] KVM: move postcommit flush to x86, as mmio sptes are x86 specific

Quoted 10 lines · Marcelo Tosatti

Other arches do not need this.

Index: kvm/arch/x86/kvm/x86.c
===================================================================

arch/x86/kvm/x86.c +0 -0
@@ -6455,6 +6455,14 @@ void kvm_arch_commit_memory_region(struc
 		kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);

arch/x86/kvm/x86.c void kvm_arch_commit_memory_region(struc · line 6465

Quoted 10 lines
arch/x86/kvm/x86.c +8 -0
 	kvm_mmu_slot_remove_write_access(kvm, mem->slot);
 	spin_unlock(&kvm->mmu_lock);
+	/*
+	 * If the new memory slot is created, we need to clear all
+	 * mmio sptes.
+	 */
+	if (old.npages == 0 && npages) {
+		kvm_mmu_zap_all(kvm);
+		kvm_reload_remote_mmus(kvm);
+	}

Can not use kvm_arch_flush_shadow_all()?

Others are fine to me.

#5 lore raw

Re: [patch 3/3] KVM: move postcommit flush to x86, as mmio sptes are x86 specific

Quoted 15 lines · Xiao Guangrong
Quoted 14 lines · Marcelo Tosatti

Other arches do not need this.

Index: kvm/arch/x86/kvm/x86.c
===================================================================

arch/x86/kvm/x86.c +2 -0
@@ -6455,6 +6455,14 @@ void kvm_arch_commit_memory_region(struc
 		kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
 	kvm_mmu_slot_remove_write_access(kvm, mem->slot);
 	spin_unlock(&kvm->mmu_lock);
+	/*
+	 * If the new memory slot is created, we need to clear all
Quoted 8 lines

arch/x86/kvm/x86.c void kvm_arch_commit_memory_region(struc · line 6465

Quoted 6 lines
arch/x86/kvm/x86.c +6 -0
+	 * mmio sptes.
+	 */
+	if (old.npages == 0 && npages) {
+		kvm_mmu_zap_all(kvm);
+		kvm_reload_remote_mmus(kvm);
+	}

Can not use kvm_arch_flush_shadow_all()?

Better reduce it to necessary cases only.

#6 lore raw

Re: [patch 3/3] KVM: move postcommit flush to x86, as mmio sptes are x86 specific

In reply to Marcelo Tosatti
Quoted 11 lines · On Fri, 24 Aug 2012 15:54:59 -0300 Marcelo Tosatti <[email protected]> wrote:

Other arches do not need this.

Index: kvm/arch/x86/kvm/x86.c
===================================================================

arch/x86/kvm/x86.c +0 -0
@@ -6455,6 +6455,14 @@ void kvm_arch_commit_memory_region(struc
 		kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
 	kvm_mmu_slot_remove_write_access(kvm, mem->slot);

arch/x86/kvm/x86.c void kvm_arch_commit_memory_region(struc · line 6466

Quoted 10 lines
arch/x86/kvm/x86.c +8 -0
 	spin_unlock(&kvm->mmu_lock);
+	/*
+	 * If the new memory slot is created, we need to clear all
+	 * mmio sptes.
+	 */
+	if (old.npages == 0 && npages) {
+		kvm_mmu_zap_all(kvm);
+		kvm_reload_remote_mmus(kvm);
+	}
 }

Any explanation why (old.base_gfn != new.base_gfn) case can be omitted?

Takuya

Quoted 19 lines
arch/x86/kvm/x86.c +0 -0
 void kvm_arch_flush_shadow_all(struct kvm *kvm)

Index: kvm/virt/kvm/kvm_main.c
===================================================================

virt/kvm/kvm_main.c +0 -7
@@ -849,13 +849,6 @@ int __kvm_set_memory_region(struct kvm *
 
 	kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
 
-	/*
-	 * If the new memory slot is created, we need to clear all
-	 * mmio sptes.
-	 */
-	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
-		kvm_arch_flush_shadow_all(kvm);
-
 	kvm_free_physmem_slot(&old, &new);
 	kfree(old_memslots);
#7 lore raw

Re: [patch 3/3] KVM: move postcommit flush to x86, as mmio sptes are x86 specific

Quoted 19 lines · Takuya Yoshikawa
Quoted 16 lines · On Fri, 24 Aug 2012 15:54:59 -0300 Marcelo Tosatti <[email protected]> wrote:

Other arches do not need this.

Index: kvm/arch/x86/kvm/x86.c
===================================================================

arch/x86/kvm/x86.c +4 -0
@@ -6455,6 +6455,14 @@ void kvm_arch_commit_memory_region(struc
 		kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
 	kvm_mmu_slot_remove_write_access(kvm, mem->slot);
 	spin_unlock(&kvm->mmu_lock);
+	/*
+	 * If the new memory slot is created, we need to clear all
+	 * mmio sptes.
+	 */
Quoted 8 lines

arch/x86/kvm/x86.c void kvm_arch_commit_memory_region(struc · line 6466

Quoted 5 lines
arch/x86/kvm/x86.c +4 -0
+	if (old.npages == 0 && npages) {
+		kvm_mmu_zap_all(kvm);
+		kvm_reload_remote_mmus(kvm);
+	}
 }

Any explanation why (old.base_gfn != new.base_gfn) case can be omitted?

(old.base_gfn != new.base_gfn) check covers the cases

1. old.base_gfn = 0, new.base_gfn = !0 (slot creation)

and

x != 0, y != 0, x != y.
2. old.base_gfn = x, new.base_gfn = y (gpa base change)

Patch 2 covers case 2, so its only necessary to cover case 1 here.

Makes sense?

Quoted 21 lines

Takuya

Quoted 19 lines
arch/x86/kvm/x86.c +0 -0
 void kvm_arch_flush_shadow_all(struct kvm *kvm)

Index: kvm/virt/kvm/kvm_main.c
===================================================================

virt/kvm/kvm_main.c +0 -7
@@ -849,13 +849,6 @@ int __kvm_set_memory_region(struct kvm *
 
 	kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
 
-	/*
-	 * If the new memory slot is created, we need to clear all
-	 * mmio sptes.
-	 */
-	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
-		kvm_arch_flush_shadow_all(kvm);
-
 	kvm_free_physmem_slot(&old, &new);
 	kfree(old_memslots);
#8 lore raw

Re: [patch 3/3] KVM: move postcommit flush to x86, as mmio sptes are x86 specific

Quoted 9 lines · On Mon, 27 Aug 2012 16:06:01 -0300 Marcelo Tosatti <[email protected]> wrote:
Quoted 2 lines

Any explanation why (old.base_gfn != new.base_gfn) case can be omitted?

(old.base_gfn != new.base_gfn) check covers the cases

1. old.base_gfn = 0, new.base_gfn = !0 (slot creation)

and

Quoted 7 lines

x != 0, y != 0, x != y.
2. old.base_gfn = x, new.base_gfn = y (gpa base change)

Patch 2 covers case 2, so its only necessary to cover case 1 here.

Makes sense?

Yes.

But didn't you change the flush in the if block modified by patch 2 to kvm_arch_flush_shadow_memslot()?

Although current implementation flushes everything, this may trigger problem when we change it.

Takuya

#9 lore raw

Re: [patch 3/3] KVM: move postcommit flush to x86, as mmio sptes are x86 specific

Quoted 22 lines · Takuya Yoshikawa
Quoted 9 lines · On Mon, 27 Aug 2012 16:06:01 -0300 Marcelo Tosatti <[email protected]> wrote:
Quoted 2 lines

Any explanation why (old.base_gfn != new.base_gfn) case can be omitted?

(old.base_gfn != new.base_gfn) check covers the cases

1. old.base_gfn = 0, new.base_gfn = !0 (slot creation)

and

Quoted 7 lines

x != 0, y != 0, x != y.
2. old.base_gfn = x, new.base_gfn = y (gpa base change)

Patch 2 covers case 2, so its only necessary to cover case 1 here.

Makes sense?

Yes.

Quoted 7 lines

But didn't you change the flush in the if block modified by patch 2 to kvm_arch_flush_shadow_memslot()?

Although current implementation flushes everything, this may trigger problem when we change it.

Takuya

Yay, thanks.

#10 lore raw

PATCH v2 3/3 KVM: move postcommit flush to x86, as mmio sptes are x86 specific

In reply to Marcelo Tosatti

Other arches do not need this.

v2: fix incorrect deletion of mmio sptes on gpa move (noticed by Takuya)

arch/x86/kvm/x86.c +8 -0
Index: kvm/arch/x86/kvm/x86.c
===================================================================
@@ -6446,6 +6446,14 @@ void kvm_arch_commit_memory_region(struc
 		kvm_mmu_change_mmu_pages(kvm, nr_mmu_pages);
 	kvm_mmu_slot_remove_write_access(kvm, mem->slot);
 	spin_unlock(&kvm->mmu_lock);
+	/*
+	 * If memory slot is created, or moved, we need to clear all
+	 * mmio sptes.
+	 */
+	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT) {
+		kvm_mmu_zap_all(kvm);
+		kvm_reload_remote_mmus(kvm);
+	}
 }
 
 void kvm_arch_flush_shadow_all(struct kvm *kvm)
virt/kvm/kvm_main.c +0 -7
Index: kvm/virt/kvm/kvm_main.c
===================================================================
@@ -849,13 +849,6 @@ int __kvm_set_memory_region(struct kvm *
 
 	kvm_arch_commit_memory_region(kvm, mem, old, user_alloc);
 
-	/*
-	 * If the new memory slot is created, we need to clear all
-	 * mmio sptes.
-	 */
-	if (npages && old.base_gfn != mem->guest_phys_addr >> PAGE_SHIFT)
-		kvm_arch_flush_shadow_all(kvm);
-
 	kvm_free_physmem_slot(&old, &new);
 	kfree(old_memslots);
 
Applied
#12 lore raw
In reply to Marcelo Tosatti
Quoted 2 lines · Marcelo Tosatti

Paul, does that work for you.

Thanks, applied.

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