KVM: Register /dev/kvm as the _very_ last thing during initialization
[ Upstream commit 2b01281273738bf2d6551da48d65db2df3f28998 ] Register /dev/kvm, i.e. expose KVM to userspace, only after all other setup has completed. Once /dev/kvm is exposed, userspace can start invoking KVM ioctls, creating VMs, etc... If userspace creates a VM before KVM is done with its configuration, bad things may happen, e.g. KVM will fail to properly migrate vCPU state if a VM is created before KVM has registered preemption notifiers. Cc: stable@vger.kernel.org Signed-off-by: Sean Christopherson <seanjc@google.com> Message-Id: <20221130230934.1014142-2-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com> Signed-off-by: Sasha Levin <sashal@kernel.org>
This commit is contained in:

committed by
Greg Kroah-Hartman

parent
7958663668
commit
a43f7d0628
@@ -5020,12 +5020,6 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
|
|||||||
kvm_vm_fops.owner = module;
|
kvm_vm_fops.owner = module;
|
||||||
kvm_vcpu_fops.owner = module;
|
kvm_vcpu_fops.owner = module;
|
||||||
|
|
||||||
r = misc_register(&kvm_dev);
|
|
||||||
if (r) {
|
|
||||||
pr_err("kvm: misc device register failed\n");
|
|
||||||
goto out_unreg;
|
|
||||||
}
|
|
||||||
|
|
||||||
register_syscore_ops(&kvm_syscore_ops);
|
register_syscore_ops(&kvm_syscore_ops);
|
||||||
|
|
||||||
kvm_preempt_ops.sched_in = kvm_sched_in;
|
kvm_preempt_ops.sched_in = kvm_sched_in;
|
||||||
@@ -5034,11 +5028,24 @@ int kvm_init(void *opaque, unsigned vcpu_size, unsigned vcpu_align,
|
|||||||
kvm_init_debug();
|
kvm_init_debug();
|
||||||
|
|
||||||
r = kvm_vfio_ops_init();
|
r = kvm_vfio_ops_init();
|
||||||
WARN_ON(r);
|
if (WARN_ON_ONCE(r))
|
||||||
|
goto err_vfio;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Registration _must_ be the very last thing done, as this exposes
|
||||||
|
* /dev/kvm to userspace, i.e. all infrastructure must be setup!
|
||||||
|
*/
|
||||||
|
r = misc_register(&kvm_dev);
|
||||||
|
if (r) {
|
||||||
|
pr_err("kvm: misc device register failed\n");
|
||||||
|
goto err_register;
|
||||||
|
}
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
out_unreg:
|
err_register:
|
||||||
|
kvm_vfio_ops_exit();
|
||||||
|
err_vfio:
|
||||||
kvm_async_pf_deinit();
|
kvm_async_pf_deinit();
|
||||||
out_free_5:
|
out_free_5:
|
||||||
for_each_possible_cpu(cpu)
|
for_each_possible_cpu(cpu)
|
||||||
@@ -5065,8 +5072,14 @@ void kvm_exit(void)
|
|||||||
{
|
{
|
||||||
int cpu;
|
int cpu;
|
||||||
|
|
||||||
debugfs_remove_recursive(kvm_debugfs_dir);
|
/*
|
||||||
|
* Note, unregistering /dev/kvm doesn't strictly need to come first,
|
||||||
|
* fops_get(), a.k.a. try_module_get(), prevents acquiring references
|
||||||
|
* to KVM while the module is being stopped.
|
||||||
|
*/
|
||||||
misc_deregister(&kvm_dev);
|
misc_deregister(&kvm_dev);
|
||||||
|
|
||||||
|
debugfs_remove_recursive(kvm_debugfs_dir);
|
||||||
for_each_possible_cpu(cpu)
|
for_each_possible_cpu(cpu)
|
||||||
free_cpumask_var(per_cpu(cpu_kick_mask, cpu));
|
free_cpumask_var(per_cpu(cpu_kick_mask, cpu));
|
||||||
kmem_cache_destroy(kvm_vcpu_cache);
|
kmem_cache_destroy(kvm_vcpu_cache);
|
||||||
|
Reference in New Issue
Block a user