KVM: Actually create debugfs in kvm_create_vm()
Doing debugfs creation after vm creation leaves things in a quasi-initialized state for a while. This is further complicated by the fact that we tear down debugfs from kvm_destroy_vm(). Align debugfs and stats init/destroy with the vm init/destroy pattern to avoid any headaches. Note the fix for a benign mistake in error handling for calls to kvm_arch_create_vm_debugfs() rolled in. Since all implementations of the function return 0 unconditionally it isn't actually a bug at the moment. Lastly, tear down debugfs/stats data in the kvm_create_vm_debugfs() error path. Previously it was safe to assume that kvm_destroy_vm() would take out the garbage, that is no longer the case. Signed-off-by: Oliver Upton <oupton@google.com> Message-Id: <20220720092259.3491733-6-oliver.upton@linux.dev> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
This commit is contained in:
parent
59f82aad59
commit
b74ed7a68e
|
@ -1028,7 +1028,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)
|
||||||
char dir_name[ITOA_MAX_LEN * 2];
|
char dir_name[ITOA_MAX_LEN * 2];
|
||||||
struct kvm_stat_data *stat_data;
|
struct kvm_stat_data *stat_data;
|
||||||
const struct _kvm_stats_desc *pdesc;
|
const struct _kvm_stats_desc *pdesc;
|
||||||
int i, ret;
|
int i, ret = -ENOMEM;
|
||||||
int kvm_debugfs_num_entries = kvm_vm_stats_header.num_desc +
|
int kvm_debugfs_num_entries = kvm_vm_stats_header.num_desc +
|
||||||
kvm_vcpu_stats_header.num_desc;
|
kvm_vcpu_stats_header.num_desc;
|
||||||
|
|
||||||
|
@ -1054,13 +1054,13 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)
|
||||||
sizeof(*kvm->debugfs_stat_data),
|
sizeof(*kvm->debugfs_stat_data),
|
||||||
GFP_KERNEL_ACCOUNT);
|
GFP_KERNEL_ACCOUNT);
|
||||||
if (!kvm->debugfs_stat_data)
|
if (!kvm->debugfs_stat_data)
|
||||||
return -ENOMEM;
|
goto out_err;
|
||||||
|
|
||||||
for (i = 0; i < kvm_vm_stats_header.num_desc; ++i) {
|
for (i = 0; i < kvm_vm_stats_header.num_desc; ++i) {
|
||||||
pdesc = &kvm_vm_stats_desc[i];
|
pdesc = &kvm_vm_stats_desc[i];
|
||||||
stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
|
stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
|
||||||
if (!stat_data)
|
if (!stat_data)
|
||||||
return -ENOMEM;
|
goto out_err;
|
||||||
|
|
||||||
stat_data->kvm = kvm;
|
stat_data->kvm = kvm;
|
||||||
stat_data->desc = pdesc;
|
stat_data->desc = pdesc;
|
||||||
|
@ -1075,7 +1075,7 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)
|
||||||
pdesc = &kvm_vcpu_stats_desc[i];
|
pdesc = &kvm_vcpu_stats_desc[i];
|
||||||
stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
|
stat_data = kzalloc(sizeof(*stat_data), GFP_KERNEL_ACCOUNT);
|
||||||
if (!stat_data)
|
if (!stat_data)
|
||||||
return -ENOMEM;
|
goto out_err;
|
||||||
|
|
||||||
stat_data->kvm = kvm;
|
stat_data->kvm = kvm;
|
||||||
stat_data->desc = pdesc;
|
stat_data->desc = pdesc;
|
||||||
|
@ -1087,12 +1087,13 @@ static int kvm_create_vm_debugfs(struct kvm *kvm, const char *fdname)
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = kvm_arch_create_vm_debugfs(kvm);
|
ret = kvm_arch_create_vm_debugfs(kvm);
|
||||||
if (ret) {
|
if (ret)
|
||||||
kvm_destroy_vm_debugfs(kvm);
|
goto out_err;
|
||||||
return i;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
out_err:
|
||||||
|
kvm_destroy_vm_debugfs(kvm);
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1123,7 +1124,7 @@ int __weak kvm_arch_create_vm_debugfs(struct kvm *kvm)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static struct kvm *kvm_create_vm(unsigned long type)
|
static struct kvm *kvm_create_vm(unsigned long type, const char *fdname)
|
||||||
{
|
{
|
||||||
struct kvm *kvm = kvm_arch_alloc_vm();
|
struct kvm *kvm = kvm_arch_alloc_vm();
|
||||||
struct kvm_memslots *slots;
|
struct kvm_memslots *slots;
|
||||||
|
@ -1212,7 +1213,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
|
||||||
|
|
||||||
r = kvm_arch_post_init_vm(kvm);
|
r = kvm_arch_post_init_vm(kvm);
|
||||||
if (r)
|
if (r)
|
||||||
goto out_err;
|
goto out_err_mmu_notifier;
|
||||||
|
|
||||||
mutex_lock(&kvm_lock);
|
mutex_lock(&kvm_lock);
|
||||||
list_add(&kvm->vm_list, &vm_list);
|
list_add(&kvm->vm_list, &vm_list);
|
||||||
|
@ -1228,12 +1229,18 @@ static struct kvm *kvm_create_vm(unsigned long type)
|
||||||
*/
|
*/
|
||||||
if (!try_module_get(kvm_chardev_ops.owner)) {
|
if (!try_module_get(kvm_chardev_ops.owner)) {
|
||||||
r = -ENODEV;
|
r = -ENODEV;
|
||||||
goto out_err;
|
goto out_err_mmu_notifier;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
r = kvm_create_vm_debugfs(kvm, fdname);
|
||||||
|
if (r)
|
||||||
|
goto out_err;
|
||||||
|
|
||||||
return kvm;
|
return kvm;
|
||||||
|
|
||||||
out_err:
|
out_err:
|
||||||
|
module_put(kvm_chardev_ops.owner);
|
||||||
|
out_err_mmu_notifier:
|
||||||
#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
|
#if defined(CONFIG_MMU_NOTIFIER) && defined(KVM_ARCH_WANT_MMU_NOTIFIER)
|
||||||
if (kvm->mmu_notifier.ops)
|
if (kvm->mmu_notifier.ops)
|
||||||
mmu_notifier_unregister(&kvm->mmu_notifier, current->mm);
|
mmu_notifier_unregister(&kvm->mmu_notifier, current->mm);
|
||||||
|
@ -4900,7 +4907,7 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)
|
||||||
|
|
||||||
snprintf(fdname, sizeof(fdname), "%d", fd);
|
snprintf(fdname, sizeof(fdname), "%d", fd);
|
||||||
|
|
||||||
kvm = kvm_create_vm(type);
|
kvm = kvm_create_vm(type, fdname);
|
||||||
if (IS_ERR(kvm)) {
|
if (IS_ERR(kvm)) {
|
||||||
r = PTR_ERR(kvm);
|
r = PTR_ERR(kvm);
|
||||||
goto put_fd;
|
goto put_fd;
|
||||||
|
@ -4923,11 +4930,6 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)
|
||||||
* cases it will be called by the final fput(file) and will take
|
* cases it will be called by the final fput(file) and will take
|
||||||
* care of doing kvm_put_kvm(kvm).
|
* care of doing kvm_put_kvm(kvm).
|
||||||
*/
|
*/
|
||||||
if (kvm_create_vm_debugfs(kvm, fdname) < 0) {
|
|
||||||
fput(file);
|
|
||||||
r = -ENOMEM;
|
|
||||||
goto put_fd;
|
|
||||||
}
|
|
||||||
kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
|
kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
|
||||||
|
|
||||||
fd_install(fd, file);
|
fd_install(fd, file);
|
||||||
|
|
Loading…
Reference in New Issue