public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
From: Oliver Upton <oupton@google.com>
To: kvm@vger.kernel.org
Cc: seanjc@google.com, pbonzini@redhat.com, maz@kernel.org,
	kvmarm@vger.kernel.org, Oliver Upton <oupton@google.com>
Subject: [PATCH 3/5] KVM: Get an fd before creating the VM
Date: Fri, 15 Apr 2022 20:15:40 +0000	[thread overview]
Message-ID: <20220415201542.1496582-4-oupton@google.com> (raw)
In-Reply-To: <20220415201542.1496582-1-oupton@google.com>

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(-)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index aaf8de62b897..1abbc6b07c19 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -4752,25 +4752,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;
 	}
@@ -4782,17 +4784,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;
 }
 
-- 
2.36.0.rc0.470.gd361397f0d-goog


  parent reply	other threads:[~2022-04-15 20:15 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-15 20:15 [PATCH 0/5] KVM: Clean up debugfs+stats init/destroy Oliver Upton
2022-04-15 20:15 ` [PATCH 1/5] KVM: Shove vm stats_id init into kvm_create_vm_debugfs() Oliver Upton
2022-05-16 20:58   ` Sean Christopherson
2022-04-15 20:15 ` [PATCH 2/5] KVM: Shove vcpu stats_id init into kvm_vcpu_create_debugfs() Oliver Upton
2022-05-16 21:01   ` Sean Christopherson
2022-05-16 22:26     ` Oliver Upton
2022-04-15 20:15 ` Oliver Upton [this message]
2022-04-15 20:15 ` [PATCH 4/5] KVM: Actually create debugfs in kvm_create_vm() Oliver Upton
2022-05-16 22:19   ` Sean Christopherson
2022-05-16 23:55     ` Oliver Upton
2022-04-15 20:15 ` [PATCH 5/5] KVM: Hoist debugfs_dentry init to kvm_create_vm_debugfs() (again) Oliver Upton
2022-04-15 20:19 ` [PATCH 0/5] KVM: Clean up debugfs+stats init/destroy Oliver Upton

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220415201542.1496582-4-oupton@google.com \
    --to=oupton@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=kvmarm@vger.kernel.org \
    --cc=maz@kernel.org \
    --cc=pbonzini@redhat.com \
    --cc=seanjc@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox