From: Greg Kurz <groug@kaod.org>
To: Vivek Goyal <vgoyal@redhat.com>
Cc: mst@redhat.com, qemu-devel@nongnu.org, dgilbert@redhat.com,
virtio-fs@redhat.com, stefanha@redhat.com,
marcandre.lureau@redhat.com
Subject: Re: [PATCH 3/6] vhost-user: Return error code from slave_read()
Date: Fri, 29 Jan 2021 10:45:07 +0100 [thread overview]
Message-ID: <20210129104507.1ade37a7@bahia.lan> (raw)
In-Reply-To: <20210125180115.22936-4-vgoyal@redhat.com>
On Mon, 25 Jan 2021 13:01:12 -0500
Vivek Goyal <vgoyal@redhat.com> wrote:
> Right now slave_read() is called through main event loop and does not
> return error. In next few patches I want to call slave_read() from
> vhost device shutdown path as well and want to know if an error
> happened so that caller can give up and return error accordingly.
>
> Hence, create helper function do_slave_read(), which returns an
> integer. Success is 0 and negative number is error code. slave_read()
> calls do_slave_read() and ignores error code.
>
> Signed-off-by: Vivek Goyal <vgoyal@redhat.com>
> ---
> hw/virtio/vhost-user.c | 43 ++++++++++++++++++++++++++++++++++++------
> 1 file changed, 37 insertions(+), 6 deletions(-)
>
> diff --git a/hw/virtio/vhost-user.c b/hw/virtio/vhost-user.c
> index d95dbc39e3..867cac034f 100644
> --- a/hw/virtio/vhost-user.c
> +++ b/hw/virtio/vhost-user.c
> @@ -1401,7 +1401,7 @@ static uint64_t vhost_user_slave_handle_vring_host_notifier(
> return false;
> }
>
> -static void slave_read(void *opaque)
> +static int do_slave_read(void *opaque)
> {
> struct vhost_dev *dev = opaque;
> struct vhost_user *u = dev->opaque;
> @@ -1432,13 +1432,22 @@ static void slave_read(void *opaque)
> size = recvmsg(u->slave_fd, &msgh, 0);
> } while (size < 0 && (errno == EINTR || errno == EAGAIN));
>
> - if (size != VHOST_USER_HDR_SIZE) {
> + if (size < 0) {
> + ret = -errno;
> error_report("Failed to read from slave.");
> goto err;
> }
>
> + if (size != VHOST_USER_HDR_SIZE) {
> + error_report("Failed to read %lu bytes from slave.",
> + VHOST_USER_HDR_SIZE);
Maybe also print size ?
And, question from a newbie : any idea why short reads are
considered as errors instead of retrying ? Same question
stands for the other locations where we check the numbers
of read/written bytes in this function.
> + ret = -EBADMSG;
> + goto err;
> + }
> +
> if (msgh.msg_flags & MSG_CTRUNC) {
> error_report("Truncated message.");
> + ret = -EBADMSG;
> goto err;
> }
>
> @@ -1456,6 +1465,7 @@ static void slave_read(void *opaque)
> error_report("Failed to read msg header."
> " Size %d exceeds the maximum %zu.", hdr.size,
> VHOST_USER_PAYLOAD_SIZE);
> + ret = -EBADMSG;
> goto err;
> }
>
> @@ -1464,8 +1474,15 @@ static void slave_read(void *opaque)
> size = read(u->slave_fd, &payload, hdr.size);
> } while (size < 0 && (errno == EINTR || errno == EAGAIN));
>
> - if (size != hdr.size) {
> + if (size == -1) {
Maybe make it (size < 0) for consistency with the error checking
added above ? And this seems to be the preferred way in the QEMU
tree :)
> error_report("Failed to read payload from slave.");
> + ret = errno;
ret = -errno;
And this should be done before error_report() to ensure errno
isn't cloberred.
> + goto err;
> + }
> +
> + if (size != hdr.size) {
> + error_report("Failed to read %d payload bytes from slave.", hdr.size);
> + ret = -EBADMSG;
> goto err;
> }
>
> @@ -1529,13 +1546,22 @@ static void slave_read(void *opaque)
> size = writev(u->slave_fd, iovec, ARRAY_SIZE(iovec));
> } while (size < 0 && (errno == EINTR || errno == EAGAIN));
>
> - if (size != VHOST_USER_HDR_SIZE + hdr.size) {
> + if (size == -1) {
size < 0
> error_report("Failed to send msg reply to slave.");
> + ret = -errno;
Move before error_report()
> + goto err;
> + }
> +
> + if (size != VHOST_USER_HDR_SIZE + hdr.size) {
> + error_report("Failed to send msg reply to slave. Wrote %d bytes"
> + " expected %lu bytes.", size,
> + VHOST_USER_HDR_SIZE + hdr.size);
> + ret = -EIO;
> goto err;
> }
> }
>
> - return;
> + return 0;
>
> err:
> qemu_set_fd_handler(u->slave_fd, NULL, NULL, NULL);
> @@ -1546,7 +1572,12 @@ err:
> close(fd[i]);
> }
> }
> - return;
> + return ret;
> +}
> +
> +static void slave_read(void *opaque)
> +{
> + do_slave_read(opaque);
> }
>
> static int vhost_setup_slave_channel(struct vhost_dev *dev)
next prev parent reply other threads:[~2021-01-29 9:46 UTC|newest]
Thread overview: 26+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-01-25 18:01 [RFC PATCH 0/6] vhost-user: Shutdown/Flush slave channel properly Vivek Goyal
2021-01-25 18:01 ` [PATCH 1/6] virtiofsd: Drop ->vu_dispatch_rwlock while waiting for thread to exit Vivek Goyal
2021-01-26 15:56 ` Greg Kurz
2021-01-26 18:33 ` Vivek Goyal
2021-01-29 12:03 ` Greg Kurz
2021-01-29 15:04 ` Vivek Goyal
2021-01-25 18:01 ` [PATCH 2/6] libvhost-user: Use slave_mutex in all slave messages Vivek Goyal
2021-01-28 14:31 ` Greg Kurz
2021-01-28 14:48 ` Vivek Goyal
2021-01-28 15:06 ` Greg Kurz
2021-01-25 18:01 ` [PATCH 3/6] vhost-user: Return error code from slave_read() Vivek Goyal
2021-01-29 9:45 ` Greg Kurz [this message]
2021-01-29 15:02 ` Vivek Goyal
2021-01-25 18:01 ` [PATCH 4/6] qemu, vhost-user: Extend protocol to start/stop/flush slave channel Vivek Goyal
2021-01-28 16:52 ` Stefan Hajnoczi
2021-01-29 14:16 ` Vivek Goyal
2021-01-29 15:11 ` Vivek Goyal
2021-02-08 17:41 ` Stefan Hajnoczi
2021-01-25 18:01 ` [PATCH 5/6] libvhost-user: Add support " Vivek Goyal
2021-01-25 18:01 ` [PATCH 6/6] virtiofsd: Opt in for slave start/stop/shutdown functionality Vivek Goyal
2021-02-10 21:39 ` [RFC PATCH 0/6] vhost-user: Shutdown/Flush slave channel properly Michael S. Tsirkin
2021-02-10 22:15 ` Vivek Goyal
2021-02-23 14:14 ` Michael S. Tsirkin
2021-02-23 14:23 ` Vivek Goyal
2021-03-14 22:21 ` Michael S. Tsirkin
2021-03-14 22:26 ` Vivek Goyal
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=20210129104507.1ade37a7@bahia.lan \
--to=groug@kaod.org \
--cc=dgilbert@redhat.com \
--cc=marcandre.lureau@redhat.com \
--cc=mst@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=stefanha@redhat.com \
--cc=vgoyal@redhat.com \
--cc=virtio-fs@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).