All of lore.kernel.org
 help / color / mirror / Atom feed
From: mtosatti@redhat.com
To: kvm@vger.kernel.org
Cc: avi@redhat.com, Marcelo Tosatti <mtosatti@redhat.com>
Subject: [patch 3/4] KVM: introduce kvm_arch_can_free_memslot, disallow slot deletion if cached cr3
Date: Thu, 07 May 2009 18:03:34 -0300	[thread overview]
Message-ID: <20090507210534.058747069@amt.cnet> (raw)
In-Reply-To: 20090507210331.370806285@amt.cnet

[-- Attachment #1: verify-cr3 --]
[-- Type: text/plain, Size: 4286 bytes --]

Disallow the deletion of memory slots (and aliases, for x86 case), if a
vcpu contains a cr3 that points to such slot/alias.

This complements commit 6c20e1442bb1c62914bb85b7f4a38973d2a423ba.

v2:
- set KVM_REQ_TRIPLE_FAULT
- use __KVM_HAVE_ARCH_CAN_FREE_MEMSLOT to avoid duplication of stub

Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>

Index: kvm-pending/arch/x86/kvm/x86.c
===================================================================
--- kvm-pending.orig/arch/x86/kvm/x86.c
+++ kvm-pending/arch/x86/kvm/x86.c
@@ -1636,6 +1636,29 @@ gfn_t unalias_gfn(struct kvm *kvm, gfn_t
 	return gfn;
 }
 
+static int kvm_root_gfn_in_range(struct kvm *kvm, gfn_t base_gfn,
+				 gfn_t end_gfn, bool unalias)
+{
+	struct kvm_vcpu *vcpu;
+	gfn_t root_gfn;
+	int i;
+
+	for (i = 0; i < KVM_MAX_VCPUS; ++i) {
+		vcpu = kvm->vcpus[i];
+		if (!vcpu)
+			continue;
+		root_gfn = vcpu->arch.cr3 >> PAGE_SHIFT;
+		if (unalias)
+			root_gfn = unalias_gfn(kvm, root_gfn);
+		if (root_gfn >= base_gfn && root_gfn <= end_gfn) {
+			set_bit(KVM_REQ_TRIPLE_FAULT, &vcpu->requests);
+			return 1;
+		}
+	}
+
+	return 0;
+}
+
 /*
  * Set a new alias region.  Aliases map a portion of physical memory into
  * another portion.  This is useful for memory windows, for example the PC
@@ -1666,6 +1689,19 @@ static int kvm_vm_ioctl_set_memory_alias
 	spin_lock(&kvm->mmu_lock);
 
 	p = &kvm->arch.aliases[alias->slot];
+
+	/* FIXME: either disallow shrinking alias slots or disable
+ 	 * size changes as done with memslots
+ 	 */
+	if (!alias->memory_size) {
+		r = -EBUSY;
+		if (kvm_root_gfn_in_range(kvm, p->base_gfn,
+					       p->base_gfn + p->npages - 1,
+					       false))
+			goto out_unlock;
+	}
+
+
 	p->base_gfn = alias->guest_phys_addr >> PAGE_SHIFT;
 	p->npages = alias->memory_size >> PAGE_SHIFT;
 	p->target_gfn = alias->target_phys_addr >> PAGE_SHIFT;
@@ -1682,6 +1718,9 @@ static int kvm_vm_ioctl_set_memory_alias
 
 	return 0;
 
+out_unlock:
+	spin_unlock(&kvm->mmu_lock);
+	up_write(&kvm->slots_lock);
 out:
 	return r;
 }
@@ -4552,6 +4591,15 @@ void kvm_arch_flush_shadow(struct kvm *k
 	kvm_mmu_zap_all(kvm);
 }
 
+int kvm_arch_can_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
+{
+	int ret;
+
+	ret = kvm_root_gfn_in_range(kvm, slot->base_gfn,
+				    slot->base_gfn + slot->npages - 1, true);
+	return !ret;
+}
+
 int kvm_arch_vcpu_runnable(struct kvm_vcpu *vcpu)
 {
 	return vcpu->arch.mp_state == KVM_MP_STATE_RUNNABLE
Index: kvm-pending/include/linux/kvm_host.h
===================================================================
--- kvm-pending.orig/include/linux/kvm_host.h
+++ kvm-pending/include/linux/kvm_host.h
@@ -200,6 +200,7 @@ int kvm_arch_set_memory_region(struct kv
 				struct kvm_memory_slot old,
 				int user_alloc);
 void kvm_arch_flush_shadow(struct kvm *kvm);
+int kvm_arch_can_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot);
 gfn_t unalias_gfn(struct kvm *kvm, gfn_t gfn);
 struct page *gfn_to_page(struct kvm *kvm, gfn_t gfn);
 unsigned long gfn_to_hva(struct kvm *kvm, gfn_t gfn);
Index: kvm-pending/virt/kvm/kvm_main.c
===================================================================
--- kvm-pending.orig/virt/kvm/kvm_main.c
+++ kvm-pending/virt/kvm/kvm_main.c
@@ -1061,6 +1061,13 @@ static int kvm_vm_release(struct inode *
 	return 0;
 }
 
+#ifndef __KVM_HAVE_ARCH_CAN_FREE_MEMSLOT
+int kvm_arch_can_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot)
+{
+	return 1;
+}
+#endif
+
 /*
  * Allocate some memory and give it an address in the guest physical address
  * space.
@@ -1179,8 +1186,13 @@ int __kvm_set_memory_region(struct kvm *
 	}
 #endif /* not defined CONFIG_S390 */
 
-	if (!npages)
+	if (!npages) {
 		kvm_arch_flush_shadow(kvm);
+		if (!kvm_arch_can_free_memslot(kvm, memslot)) {
+			r = -EBUSY;
+			goto out_free;
+		}
+	}
 
 	spin_lock(&kvm->mmu_lock);
 	if (mem->slot >= kvm->nmemslots)
Index: kvm-pending/arch/x86/include/asm/kvm.h
===================================================================
--- kvm-pending.orig/arch/x86/include/asm/kvm.h
+++ kvm-pending/arch/x86/include/asm/kvm.h
@@ -18,6 +18,8 @@
 #define __KVM_HAVE_GUEST_DEBUG
 #define __KVM_HAVE_MSIX
 
+#define __KVM_HAVE_ARCH_CAN_FREE_MEMSLOT
+
 /* Architectural interrupt line count. */
 #define KVM_NR_INTERRUPTS 256
 



  parent reply	other threads:[~2009-05-07 21:10 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-04-27 20:06 [patch 0/4] set_memory_region locking fixes / vcpu->arch.cr3 + removal of memslots mtosatti
2009-04-27 20:06 ` [patch 1/4] KVM: MMU: protect kvm_mmu_change_mmu_pages with mmu_lock mtosatti
2009-04-27 20:06 ` [patch 2/4] KVM: take mmu_lock when updating a deleted slot mtosatti
2009-04-27 20:06 ` [patch 3/4] KVM: introduce kvm_arch_can_free_memslot, disallow slot deletion if cached cr3 mtosatti
2009-05-07 14:16   ` Avi Kivity
2009-05-07 18:58     ` Marcelo Tosatti
2009-05-07 21:03     ` [patch 0/4] set_memory_region locking fixes / cr3 vs removal of memslots v2 mtosatti
2009-05-07 21:03       ` [patch 1/4] KVM: MMU: protect kvm_mmu_change_mmu_pages with mmu_lock mtosatti
2009-05-07 21:03       ` [patch 2/4] KVM: take mmu_lock when updating a deleted slot mtosatti
2009-05-07 21:03       ` mtosatti [this message]
2009-05-10 16:40         ` [patch 3/4] KVM: introduce kvm_arch_can_free_memslot, disallow slot deletion if cached cr3 Avi Kivity
2009-05-12 21:55           ` [patch 0/3] locking fixes / cr3 validation v3 mtosatti
2009-05-12 21:55             ` [patch 1/3] KVM: MMU: protect kvm_mmu_change_mmu_pages with mmu_lock mtosatti
2009-05-12 21:55             ` [patch 2/3] KVM: take mmu_lock when updating a deleted slot mtosatti
2009-05-12 21:55             ` [patch 3/3] KVM: x86: check for cr3 validity in mmu_alloc_roots mtosatti
2009-05-13  7:40             ` [patch 0/3] locking fixes / cr3 validation v3 Avi Kivity
2009-05-07 21:03       ` [patch 4/4] KVM: x86: disallow changing a slots size mtosatti
2009-04-27 20:06 ` mtosatti

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20090507210534.058747069@amt.cnet \
    --to=mtosatti@redhat.com \
    --cc=avi@redhat.com \
    --cc=kvm@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.