diff options
-rw-r--r-- | include/linux/kvm_host.h | 4 | ||||
-rw-r--r-- | virt/kvm/kvm_main.c | 17 |
2 files changed, 20 insertions, 1 deletions
diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f4e143621e35..a2ceb51b4274 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h | |||
@@ -114,6 +114,7 @@ struct kvm { | |||
114 | struct kvm_io_bus pio_bus; | 114 | struct kvm_io_bus pio_bus; |
115 | struct kvm_vm_stat stat; | 115 | struct kvm_vm_stat stat; |
116 | struct kvm_arch arch; | 116 | struct kvm_arch arch; |
117 | atomic_t users_count; | ||
117 | }; | 118 | }; |
118 | 119 | ||
119 | /* The guest did something we don't support. */ | 120 | /* The guest did something we don't support. */ |
@@ -140,6 +141,9 @@ int kvm_init(void *opaque, unsigned int vcpu_size, | |||
140 | struct module *module); | 141 | struct module *module); |
141 | void kvm_exit(void); | 142 | void kvm_exit(void); |
142 | 143 | ||
144 | void kvm_get_kvm(struct kvm *kvm); | ||
145 | void kvm_put_kvm(struct kvm *kvm); | ||
146 | |||
143 | #define HPA_MSB ((sizeof(hpa_t) * 8) - 1) | 147 | #define HPA_MSB ((sizeof(hpa_t) * 8) - 1) |
144 | #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB) | 148 | #define HPA_ERR_MASK ((hpa_t)1 << HPA_MSB) |
145 | static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; } | 149 | static inline int is_error_hpa(hpa_t hpa) { return hpa >> HPA_MSB; } |
diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 41d4b6519136..93ed78b015c0 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c | |||
@@ -193,6 +193,7 @@ static struct kvm *kvm_create_vm(void) | |||
193 | mutex_init(&kvm->lock); | 193 | mutex_init(&kvm->lock); |
194 | kvm_io_bus_init(&kvm->mmio_bus); | 194 | kvm_io_bus_init(&kvm->mmio_bus); |
195 | init_rwsem(&kvm->slots_lock); | 195 | init_rwsem(&kvm->slots_lock); |
196 | atomic_set(&kvm->users_count, 1); | ||
196 | spin_lock(&kvm_lock); | 197 | spin_lock(&kvm_lock); |
197 | list_add(&kvm->vm_list, &vm_list); | 198 | list_add(&kvm->vm_list, &vm_list); |
198 | spin_unlock(&kvm_lock); | 199 | spin_unlock(&kvm_lock); |
@@ -242,11 +243,25 @@ static void kvm_destroy_vm(struct kvm *kvm) | |||
242 | mmdrop(mm); | 243 | mmdrop(mm); |
243 | } | 244 | } |
244 | 245 | ||
246 | void kvm_get_kvm(struct kvm *kvm) | ||
247 | { | ||
248 | atomic_inc(&kvm->users_count); | ||
249 | } | ||
250 | EXPORT_SYMBOL_GPL(kvm_get_kvm); | ||
251 | |||
252 | void kvm_put_kvm(struct kvm *kvm) | ||
253 | { | ||
254 | if (atomic_dec_and_test(&kvm->users_count)) | ||
255 | kvm_destroy_vm(kvm); | ||
256 | } | ||
257 | EXPORT_SYMBOL_GPL(kvm_put_kvm); | ||
258 | |||
259 | |||
245 | static int kvm_vm_release(struct inode *inode, struct file *filp) | 260 | static int kvm_vm_release(struct inode *inode, struct file *filp) |
246 | { | 261 | { |
247 | struct kvm *kvm = filp->private_data; | 262 | struct kvm *kvm = filp->private_data; |
248 | 263 | ||
249 | kvm_destroy_vm(kvm); | 264 | kvm_put_kvm(kvm); |
250 | return 0; | 265 | return 0; |
251 | } | 266 | } |
252 | 267 | ||