diff mbox series

[v2,3/5] KVM: Get an fd before creating the VM

Message ID 20220518175811.2758661-4-oupton@google.com (mailing list archive)
State New, archived
Headers show
Series KVM: Clean up debugfs+stats init/destroy | expand

Commit Message

Oliver Upton May 18, 2022, 5:58 p.m. UTC
Hoist fd init to the very beginning of kvm_create_vm() so we can make
use of it in other init routines.

Signed-off-by: Oliver Upton <oupton@google.com>
---
 virt/kvm/kvm_main.c | 28 ++++++++++++++++------------
 1 file changed, 16 insertions(+), 12 deletions(-)

Comments

Sean Christopherson June 16, 2022, 5:54 p.m. UTC | #1
On Wed, May 18, 2022, Oliver Upton wrote:
> Hoist fd init to the very beginning of kvm_create_vm() so we can make

"init" is a bit ambiguous, e.g. from a "can I use the fd" perspective the fd is
still very much not initialized.  Also, it's at the beginning of
kvm_dev_ioctl_create_vm(), _before_ kvm_create_vm().

  Allocate a VM's fd at the very beginning of kvm_dev_ioctl_create_vm()
  so that KVM can use the fd value to generate strings, e.g. for debugfs,
  when creating and initializing the VM.

> use of it in other init routines.
> 
> Signed-off-by: Oliver Upton <oupton@google.com>
> ---
diff mbox series

Patch

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 778151333ac0..87ccab74dc80 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4774,25 +4774,27 @@  EXPORT_SYMBOL_GPL(file_is_kvm);
 
 static int kvm_dev_ioctl_create_vm(unsigned long type)
 {
-	int r;
+	int r, fd;
 	struct kvm *kvm;
 	struct file *file;
 
+	fd = get_unused_fd_flags(O_CLOEXEC);
+	if (fd < 0)
+		return fd;
+
 	kvm = kvm_create_vm(type);
-	if (IS_ERR(kvm))
-		return PTR_ERR(kvm);
+	if (IS_ERR(kvm)) {
+		r = PTR_ERR(kvm);
+		goto put_fd;
+	}
+
 #ifdef CONFIG_KVM_MMIO
 	r = kvm_coalesced_mmio_init(kvm);
 	if (r < 0)
 		goto put_kvm;
 #endif
-	r = get_unused_fd_flags(O_CLOEXEC);
-	if (r < 0)
-		goto put_kvm;
-
 	file = anon_inode_getfile("kvm-vm", &kvm_vm_fops, kvm, O_RDWR);
 	if (IS_ERR(file)) {
-		put_unused_fd(r);
 		r = PTR_ERR(file);
 		goto put_kvm;
 	}
@@ -4804,17 +4806,19 @@  static int kvm_dev_ioctl_create_vm(unsigned long type)
 	 * care of doing kvm_put_kvm(kvm).
 	 */
 	if (kvm_create_vm_debugfs(kvm, r) < 0) {
-		put_unused_fd(r);
 		fput(file);
-		return -ENOMEM;
+		r = -ENOMEM;
+		goto put_fd;
 	}
 	kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
 
-	fd_install(r, file);
-	return r;
+	fd_install(fd, file);
+	return fd;
 
 put_kvm:
 	kvm_put_kvm(kvm);
+put_fd:
+	put_unused_fd(fd);
 	return r;
 }