KVM: Prevent module exit until all VMs are freed
[platform/kernel/linux-starfive.git] / virt / kvm / kvm_main.c
index 69c318f..2447139 100644 (file)
@@ -117,6 +117,8 @@ EXPORT_SYMBOL_GPL(kvm_debugfs_dir);
 
 static const struct file_operations stat_fops_per_vm;
 
+static struct file_operations kvm_chardev_ops;
+
 static long kvm_vcpu_ioctl(struct file *file, unsigned int ioctl,
                           unsigned long arg);
 #ifdef CONFIG_KVM_COMPAT
@@ -1131,6 +1133,16 @@ static struct kvm *kvm_create_vm(unsigned long type)
        preempt_notifier_inc();
        kvm_init_pm_notifier(kvm);
 
+       /*
+        * When the fd passed to this ioctl() is opened it pins the module,
+        * but try_module_get() also prevents getting a reference if the module
+        * is in MODULE_STATE_GOING (e.g. if someone ran "rmmod --wait").
+        */
+       if (!try_module_get(kvm_chardev_ops.owner)) {
+               r = -ENODEV;
+               goto out_err;
+       }
+
        return kvm;
 
 out_err:
@@ -1220,6 +1232,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
        preempt_notifier_dec();
        hardware_disable_all();
        mmdrop(mm);
+       module_put(kvm_chardev_ops.owner);
 }
 
 void kvm_get_kvm(struct kvm *kvm)