qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Hanna Czenczek <hreitz@redhat.com>
To: Stefano Garzarella <sgarzare@redhat.com>, qemu-devel@nongnu.org
Cc: qemu-block@nongnu.org, Kevin Wolf <kwolf@redhat.com>,
	Stefan Hajnoczi <stefanha@redhat.com>
Subject: Re: [PATCH 1/2] block/blkio: close the fd when blkio_connect() fails
Date: Wed, 2 Aug 2023 13:15:40 +0200	[thread overview]
Message-ID: <00d6b251-a33a-96d4-f5fc-92558fb3f261@redhat.com> (raw)
In-Reply-To: <20230801160332.122564-2-sgarzare@redhat.com>

On 01.08.23 18:03, Stefano Garzarella wrote:
> libblkio drivers take ownership of `fd` only after a successful
> blkio_connect(), so if it fails, we are still the owners.
>
> Fixes: cad2ccc395 ("block/blkio: use qemu_open() to support fd passing for virtio-blk")
> Suggested-by: Hanna Czenczek <hreitz@redhat.com>
> Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
> ---
>   block/blkio.c | 9 +++++++++
>   1 file changed, 9 insertions(+)

Works, so:

Reviewed-by: Hanna Czenczek <hreitz@redhat.com>


But personally, instead of having `fd_supported` track whether we have a 
valid FD or not, I’d find it more intuitive to track ownership through 
the `fd` variable itself, i.e. initialize it to -1, and set it -1 when 
ownership is transferred, and then close it once we don’t need it 
anymore, but failed to transfer ownership to blkio.  The elaborate way 
would be something like

...
-int fd, ret;
+int fd = -1;
+int ret;
...
  ret = blkio_connect(s->blkio);
+if (!ret) {
+    /* If we had an FD, libblkio now has ownership of it */
+    fd = -1;
+}
+if (fd >= 0) {
+    /* We still have FD ownership, but no longer need it, so close it */
+    qemu_close(fd);
+    fd = -1;
+}
  /*
   * [...]
   */
  if (fd_supported && ret == -EINVAL) {
-    qemu_close(fd);
-
...


Or the shorter less-verbose version would be:

...
-int fd, ret;
+int fd = -1;
+int ret;
...
  ret = blkio_connect(s->blkio);
+if (fd >= 0 && ret < 0) {
+    /* Failed to give the FD to libblkio, close it */
+    qemu_close(fd);
+}
  /*
   * [...]
   */
  if (fd_supported && ret == -EINVAL) {
-    qemu_close(fd);
-
...



  reply	other threads:[~2023-08-02 11:16 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-01 16:03 [PATCH 0/2] block/blkio: fix fd leak and add more comments for the fd passing Stefano Garzarella
2023-08-01 16:03 ` [PATCH 1/2] block/blkio: close the fd when blkio_connect() fails Stefano Garzarella
2023-08-02 11:15   ` Hanna Czenczek [this message]
2023-08-02 13:58     ` Stefano Garzarella
2023-08-01 16:03 ` [PATCH 2/2] block/blkio: add more comments on the fd passing handling Stefano Garzarella
2023-08-02 12:02   ` Hanna Czenczek

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=00d6b251-a33a-96d4-f5fc-92558fb3f261@redhat.com \
    --to=hreitz@redhat.com \
    --cc=kwolf@redhat.com \
    --cc=qemu-block@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    --cc=sgarzare@redhat.com \
    --cc=stefanha@redhat.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;
as well as URLs for NNTP newsgroup(s).