diff mbox

[RFC,7/9] make kvm_get_kvm() more robust

Message ID 20100615135527.262CFEA2@kernel.beaverton.ibm.com (mailing list archive)
State New, archived
Headers show

Commit Message

David Hansen June 15, 2010, 1:55 p.m. UTC
None
diff mbox

Patch

diff -puN include/linux/kvm_host.h~make-kvm_get_kvm-more-robust include/linux/kvm_host.h
--- linux-2.6.git/include/linux/kvm_host.h~make-kvm_get_kvm-more-robust	2010-06-11 08:39:16.000000000 -0700
+++ linux-2.6.git-dave/include/linux/kvm_host.h	2010-06-11 08:39:16.000000000 -0700
@@ -247,7 +247,7 @@  int kvm_init(void *opaque, unsigned vcpu
 		  struct module *module);
 void kvm_exit(void);
 
-void kvm_get_kvm(struct kvm *kvm);
+int kvm_get_kvm(struct kvm *kvm);
 void kvm_put_kvm(struct kvm *kvm);
 
 static inline struct kvm_memslots *kvm_memslots(struct kvm *kvm)
diff -puN virt/kvm/kvm_main.c~make-kvm_get_kvm-more-robust virt/kvm/kvm_main.c
--- linux-2.6.git/virt/kvm/kvm_main.c~make-kvm_get_kvm-more-robust	2010-06-11 08:39:16.000000000 -0700
+++ linux-2.6.git-dave/virt/kvm/kvm_main.c	2010-06-11 08:39:16.000000000 -0700
@@ -496,9 +496,30 @@  static void kvm_destroy_vm(struct kvm *k
 	mmdrop(mm);
 }
 
-void kvm_get_kvm(struct kvm *kvm)
+/*
+ * Once the counter goes to 0, we destroy the
+ * kvm object.  Do not allow additional refs
+ * to be obtained once this occurs.
+ *
+ * Any calls which are done via the kvm fd
+ * could use atomic_inc().  That is because
+ * ->users_count is set to 1 when the kvm fd
+ * is created, and stays at least 1 while
+ * the fd exists.
+ *
+ * But, those calls are currently rare, so do
+ * this (more expensive) atomic_add_unless()
+ * to keep the number of functions down.
+ *
+ * Returns 0 if the reference was obtained
+ * successfully.
+ */
+int kvm_get_kvm(struct kvm *kvm)
 {
-	atomic_inc(&kvm->users_count);
+	int did_add = atomic_add_unless(&kvm->users_count, 1, 0);
+	if (did_add)
+		return 0;
+	return -EBUSY;
 }
 EXPORT_SYMBOL_GPL(kvm_get_kvm);
 
@@ -1332,7 +1353,12 @@  static int kvm_vm_ioctl_create_vcpu(stru
 	BUG_ON(kvm->vcpus[atomic_read(&kvm->online_vcpus)]);
 
 	/* Now it's all set up, let userspace reach it */
-	kvm_get_kvm(kvm);
+	r = kvm_get_kvm(kvm);
+	/*
+	 * Getting called via the kvm fd _should_ guarantee
+	 * that we can always get a reference.
+	 */
+	WARN_ON(r);
 	r = create_vcpu_fd(vcpu);
 	if (r < 0) {
 		kvm_put_kvm(kvm);