* [PATCH v2 1/2] block/blkio: close the fd when blkio_connect() fails
2023-08-03 8:28 [PATCH v2 0/2] block/blkio: fix fd leak and add more comments for the fd passing Stefano Garzarella
@ 2023-08-03 8:28 ` Stefano Garzarella
2023-08-03 13:50 ` Hanna Czenczek
2023-08-03 8:28 ` [PATCH v2 2/2] block/blkio: add more comments on the fd passing handling Stefano Garzarella
2023-08-03 15:28 ` [PATCH v2 0/2] block/blkio: fix fd leak and add more comments for the fd passing Stefan Hajnoczi
2 siblings, 1 reply; 5+ messages in thread
From: Stefano Garzarella @ 2023-08-03 8:28 UTC (permalink / raw)
To: qemu-devel
Cc: Hanna Reitz, Stefan Hajnoczi, qemu-block, Kevin Wolf,
Stefano Garzarella
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>
---
Notes:
v2:
- avoid to use `fd_supported` to track a valid fd [Hanna]
block/blkio.c | 11 ++++++++---
1 file changed, 8 insertions(+), 3 deletions(-)
diff --git a/block/blkio.c b/block/blkio.c
index 8e7ce42c79..baba2f0b67 100644
--- a/block/blkio.c
+++ b/block/blkio.c
@@ -678,7 +678,7 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options,
const char *path = qdict_get_try_str(options, "path");
BDRVBlkioState *s = bs->opaque;
bool fd_supported = false;
- int fd, ret;
+ int fd = -1, ret;
if (!path) {
error_setg(errp, "missing 'path' option");
@@ -719,6 +719,7 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options,
if (ret < 0) {
fd_supported = false;
qemu_close(fd);
+ fd = -1;
}
}
}
@@ -733,14 +734,18 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options,
}
ret = blkio_connect(s->blkio);
+ if (ret < 0 && fd >= 0) {
+ /* Failed to give the FD to libblkio, close it */
+ qemu_close(fd);
+ fd = -1;
+ }
+
/*
* If the libblkio driver doesn't support the `fd` property, blkio_connect()
* will fail with -EINVAL. So let's try calling blkio_connect() again by
* directly setting `path`.
*/
if (fd_supported && ret == -EINVAL) {
- qemu_close(fd);
-
/*
* We need to clear the `fd` property we set previously by setting
* it to -1.
--
2.41.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 1/2] block/blkio: close the fd when blkio_connect() fails
2023-08-03 8:28 ` [PATCH v2 1/2] block/blkio: close the fd when blkio_connect() fails Stefano Garzarella
@ 2023-08-03 13:50 ` Hanna Czenczek
0 siblings, 0 replies; 5+ messages in thread
From: Hanna Czenczek @ 2023-08-03 13:50 UTC (permalink / raw)
To: Stefano Garzarella, qemu-devel; +Cc: Stefan Hajnoczi, qemu-block, Kevin Wolf
On 03.08.23 10:28, 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>
> ---
>
> Notes:
> v2:
> - avoid to use `fd_supported` to track a valid fd [Hanna]
>
> block/blkio.c | 11 ++++++++---
> 1 file changed, 8 insertions(+), 3 deletions(-)
Thanks!
Reviewed-by: Hanna Czenczek <hreitz@redhat.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
* [PATCH v2 2/2] block/blkio: add more comments on the fd passing handling
2023-08-03 8:28 [PATCH v2 0/2] block/blkio: fix fd leak and add more comments for the fd passing Stefano Garzarella
2023-08-03 8:28 ` [PATCH v2 1/2] block/blkio: close the fd when blkio_connect() fails Stefano Garzarella
@ 2023-08-03 8:28 ` Stefano Garzarella
2023-08-03 15:28 ` [PATCH v2 0/2] block/blkio: fix fd leak and add more comments for the fd passing Stefan Hajnoczi
2 siblings, 0 replies; 5+ messages in thread
From: Stefano Garzarella @ 2023-08-03 8:28 UTC (permalink / raw)
To: qemu-devel
Cc: Hanna Reitz, Stefan Hajnoczi, qemu-block, Kevin Wolf,
Stefano Garzarella
As Hanna pointed out, it is not clear in the code why qemu_open()
can fail, and why blkio_set_int("fd") is not enough to discover
the `fd` property support.
Let's fix them by adding more details in the code comments.
Suggested-by: Hanna Czenczek <hreitz@redhat.com>
Reviewed-by: Hanna Czenczek <hreitz@redhat.com>
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>
---
block/blkio.c | 15 ++++++++++++---
1 file changed, 12 insertions(+), 3 deletions(-)
diff --git a/block/blkio.c b/block/blkio.c
index baba2f0b67..1dd495617c 100644
--- a/block/blkio.c
+++ b/block/blkio.c
@@ -713,6 +713,12 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options,
*/
fd = qemu_open(path, O_RDWR, NULL);
if (fd < 0) {
+ /*
+ * qemu_open() can fail if the user specifies a path that is not
+ * a file or device, for example in the case of Unix Domain Socket
+ * for the virtio-blk-vhost-user driver. In such cases let's have
+ * libblkio open the path directly.
+ */
fd_supported = false;
} else {
ret = blkio_set_int(s->blkio, "fd", fd);
@@ -741,9 +747,12 @@ static int blkio_virtio_blk_connect(BlockDriverState *bs, QDict *options,
}
/*
- * If the libblkio driver doesn't support the `fd` property, blkio_connect()
- * will fail with -EINVAL. So let's try calling blkio_connect() again by
- * directly setting `path`.
+ * Before https://gitlab.com/libblkio/libblkio/-/merge_requests/208
+ * (libblkio <= v1.3.0), setting the `fd` property is not enough to check
+ * whether the driver supports the `fd` property or not. In that case,
+ * blkio_connect() will fail with -EINVAL.
+ * So let's try calling blkio_connect() again by directly setting `path`
+ * to cover this scenario.
*/
if (fd_supported && ret == -EINVAL) {
/*
--
2.41.0
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH v2 0/2] block/blkio: fix fd leak and add more comments for the fd passing
2023-08-03 8:28 [PATCH v2 0/2] block/blkio: fix fd leak and add more comments for the fd passing Stefano Garzarella
2023-08-03 8:28 ` [PATCH v2 1/2] block/blkio: close the fd when blkio_connect() fails Stefano Garzarella
2023-08-03 8:28 ` [PATCH v2 2/2] block/blkio: add more comments on the fd passing handling Stefano Garzarella
@ 2023-08-03 15:28 ` Stefan Hajnoczi
2 siblings, 0 replies; 5+ messages in thread
From: Stefan Hajnoczi @ 2023-08-03 15:28 UTC (permalink / raw)
To: Stefano Garzarella; +Cc: qemu-devel, Hanna Reitz, qemu-block, Kevin Wolf
[-- Attachment #1: Type: text/plain, Size: 709 bytes --]
On Thu, Aug 03, 2023 at 10:28:23AM +0200, Stefano Garzarella wrote:
> Hanna discovered an fd leak in the error path, and a few comments to
> improve in the code.
>
> v2:
> - avoid to use `fd_supported` to track a valid fd [Hanna]
>
> v1: https://lore.kernel.org/qemu-devel/20230801160332.122564-1-sgarzare@redhat.com/
>
> Stefano Garzarella (2):
> block/blkio: close the fd when blkio_connect() fails
> block/blkio: add more comments on the fd passing handling
>
> block/blkio.c | 26 ++++++++++++++++++++------
> 1 file changed, 20 insertions(+), 6 deletions(-)
>
> --
> 2.41.0
>
Thanks, applied to my block tree:
https://gitlab.com/stefanha/qemu/commits/block
Stefan
[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread