qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: Stefan Hajnoczi <stefanha@redhat.com>
To: Jagannathan Raman <jag.raman@oracle.com>
Cc: elena.ufimtseva@oracle.com, john.g.johnson@oracle.com,
	thuth@redhat.com, swapnil.ingle@nutanix.com,
	john.levon@nutanix.com, philmd@redhat.com, qemu-devel@nongnu.org,
	alex.williamson@redhat.com, marcandre.lureau@gmail.com,
	thanos.makatos@nutanix.com, pbonzini@redhat.com,
	alex.bennee@linaro.org
Subject: Re: [PATCH v3 06/12] vfio-user: run vfio-user context
Date: Wed, 27 Oct 2021 17:21:30 +0100	[thread overview]
Message-ID: <YXl8ilTXICU+0EHk@stefanha-x1.localdomain> (raw)
In-Reply-To: <489671ef49381437a03917a87dc143dd9fc90559.1633929457.git.jag.raman@oracle.com>

[-- Attachment #1: Type: text/plain, Size: 5630 bytes --]

On Mon, Oct 11, 2021 at 01:31:11AM -0400, Jagannathan Raman wrote:
> Setup a handler to run vfio-user context. The context is driven by
> messages to the file descriptor associated with it - get the fd for
> the context and hook up the handler with it
> 
> Signed-off-by: Elena Ufimtseva <elena.ufimtseva@oracle.com>
> Signed-off-by: John G Johnson <john.g.johnson@oracle.com>
> Signed-off-by: Jagannathan Raman <jag.raman@oracle.com>
> ---
>  hw/remote/vfio-user-obj.c | 75 ++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 74 insertions(+), 1 deletion(-)
> 
> diff --git a/hw/remote/vfio-user-obj.c b/hw/remote/vfio-user-obj.c
> index 7ce4e5b256..05f7fff19c 100644
> --- a/hw/remote/vfio-user-obj.c
> +++ b/hw/remote/vfio-user-obj.c
> @@ -42,6 +42,7 @@
>  #include "qapi/error.h"
>  #include "qapi/qapi-visit-sockets.h"
>  #include "qemu/notify.h"
> +#include "qemu/thread.h"
>  #include "sysemu/sysemu.h"
>  #include "libvfio-user.h"
>  #include "hw/qdev-core.h"
> @@ -72,6 +73,8 @@ struct VfuObject {
>      vfu_ctx_t *vfu_ctx;
>  
>      PCIDevice *pci_dev;
> +
> +    int vfu_poll_fd;
>  };
>  
>  static void vfu_object_set_socket(Object *obj, Visitor *v, const char *name,
> @@ -105,6 +108,58 @@ static void vfu_object_set_device(Object *obj, const char *str, Error **errp)
>      trace_vfu_prop("device", str);
>  }
>  
> +static void vfu_object_ctx_run(void *opaque)
> +{
> +    VfuObject *o = opaque;
> +    int ret = -1;
> +
> +    while (ret != 0) {
> +        ret = vfu_run_ctx(o->vfu_ctx);
> +        if (ret < 0) {
> +            if (errno == EINTR) {
> +                continue;
> +            } else if (errno == ENOTCONN) {
> +                qemu_set_fd_handler(o->vfu_poll_fd, NULL, NULL, NULL);
> +                o->vfu_poll_fd = -1;
> +                object_unparent(OBJECT(o));
> +                break;
> +            } else {
> +                error_setg(&error_abort, "vfu: Failed to run device %s - %s",
> +                           o->device, strerror(errno));
> +                 break;
> +            }

The libvfio-user.h doc comments say this function can return -1 (EAGAIN)
when LIBVFIO_USER_FLAG_ATTACH_NB was used. Is the doc comment wrong
since this patch seems to rely on vfu_run_ctx() returning 0 when there
are no more commands to process?

> +        }
> +    }
> +}
> +
> +static void vfu_object_attach_ctx(void *opaque)
> +{
> +    VfuObject *o = opaque;
> +    int ret;
> +
> +    qemu_set_fd_handler(o->vfu_poll_fd, NULL, NULL, NULL);
> +    o->vfu_poll_fd = -1;
> +
> +retry_attach:
> +    ret = vfu_attach_ctx(o->vfu_ctx);
> +    if (ret < 0 && (errno == EAGAIN || errno == EWOULDBLOCK)) {
> +        goto retry_attach;

Can we wait for the poll fd to become readable instead of spinning? I
don't know the libvfio-user APIs so I'm not sure, but it would be nice
to avoid a busy loop.

> +    } else if (ret < 0) {
> +        error_setg(&error_abort,

error_abort is not appropriate for hotplugged objects, where it's less
likely that the user wants to terminate the process when a failure
occurs. If asynchronous errors occur then QMP Events should be raised so
the QMP client gets notified and can deal with them.

> +                   "vfu: Failed to attach device %s to context - %s",
> +                   o->device, strerror(errno));
> +        return;
> +    }
> +
> +    o->vfu_poll_fd = vfu_get_poll_fd(o->vfu_ctx);
> +    if (o->vfu_poll_fd < 0) {
> +        error_setg(&error_abort, "vfu: Failed to get poll fd %s", o->device);
> +        return;
> +    }
> +
> +    qemu_set_fd_handler(o->vfu_poll_fd, vfu_object_ctx_run, NULL, o);
> +}
> +
>  /*
>   * vfio-user-server depends on the availability of the 'socket' and 'device'
>   * properties. It also depends on devices instantiated in QEMU. These
> @@ -120,7 +175,8 @@ static void vfu_object_machine_done(Notifier *notifier, void *data)
>      vfu_pci_type_t pci_type = VFU_PCI_TYPE_CONVENTIONAL;
>      int ret;
>  
> -    o->vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, o->socket->u.q_unix.path, 0,
> +    o->vfu_ctx = vfu_create_ctx(VFU_TRANS_SOCK, o->socket->u.q_unix.path,
> +                                LIBVFIO_USER_FLAG_ATTACH_NB,
>                                  o, VFU_DEV_TYPE_PCI);
>      if (o->vfu_ctx == NULL) {
>          error_setg(&error_abort, "vfu: Failed to create context - %s",
> @@ -152,6 +208,21 @@ static void vfu_object_machine_done(Notifier *notifier, void *data)
>                     o->device, strerror(errno));
>          return;
>      }
> +
> +    ret = vfu_realize_ctx(o->vfu_ctx);
> +    if (ret < 0) {
> +        error_setg(&error_abort, "vfu: Failed to realize device %s- %s",
> +                   o->device, strerror(errno));
> +        return;
> +    }
> +
> +    o->vfu_poll_fd = vfu_get_poll_fd(o->vfu_ctx);
> +    if (o->vfu_poll_fd < 0) {
> +        error_setg(&error_abort, "vfu: Failed to get poll fd %s", o->device);
> +        return;
> +    }
> +
> +    qemu_set_fd_handler(o->vfu_poll_fd, vfu_object_attach_ctx, NULL, o);
>  }
>  
>  static void vfu_object_init(Object *obj)
> @@ -178,6 +249,8 @@ static void vfu_object_init(Object *obj)
>  
>      o->machine_done.notify = vfu_object_machine_done;
>      qemu_add_machine_init_done_notifier(&o->machine_done);
> +
> +    o->vfu_poll_fd = -1;

This must be done before the qemu_add_machine_init_done_notifier() call
in the hotplug case. qemu_add_machine_init_done_notifier() invokes the
notifier callback immediately if machine init was already done.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

  reply	other threads:[~2021-10-27 16:52 UTC|newest]

Thread overview: 44+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-10-11  5:31 [PATCH v3 00/12] vfio-user server in QEMU Jagannathan Raman
2021-10-11  5:31 ` [PATCH v3 01/12] configure, meson: override C compiler for cmake Jagannathan Raman
2021-10-12 10:44   ` Paolo Bonzini
2021-10-11  5:31 ` [PATCH v3 02/12] vfio-user: build library Jagannathan Raman
2021-10-27 15:17   ` Stefan Hajnoczi
2021-10-29 14:17     ` Jag Raman
2021-11-01  9:56       ` Stefan Hajnoczi
2021-10-11  5:31 ` [PATCH v3 03/12] vfio-user: define vfio-user-server object Jagannathan Raman
2021-10-27 15:40   ` Stefan Hajnoczi
2021-10-29 14:42     ` Jag Raman
2021-11-01 10:34       ` Stefan Hajnoczi
2021-11-04 12:13         ` Markus Armbruster
2021-11-04 14:39           ` Kevin Wolf
2021-11-05 10:08             ` Markus Armbruster
2021-11-05 13:19               ` Kevin Wolf
2021-11-05 13:54                 ` Peter Krempa
2021-11-06  6:34                 ` Markus Armbruster
2021-11-08 12:05                   ` Kevin Wolf
2021-11-08 12:54                     ` Peter Krempa
2021-11-04 16:48           ` Stefan Hajnoczi
2021-10-11  5:31 ` [PATCH v3 04/12] vfio-user: instantiate vfio-user context Jagannathan Raman
2021-10-27 15:59   ` Stefan Hajnoczi
2021-10-29 14:59     ` Jag Raman
2021-11-01 10:35       ` Stefan Hajnoczi
2021-10-11  5:31 ` [PATCH v3 05/12] vfio-user: find and init PCI device Jagannathan Raman
2021-10-27 16:05   ` Stefan Hajnoczi
2021-10-29 15:58     ` Jag Raman
2021-11-01 10:38       ` Stefan Hajnoczi
2021-10-11  5:31 ` [PATCH v3 06/12] vfio-user: run vfio-user context Jagannathan Raman
2021-10-27 16:21   ` Stefan Hajnoczi [this message]
2021-10-28 21:55     ` John Levon
2021-10-11  5:31 ` [PATCH v3 07/12] vfio-user: handle PCI config space accesses Jagannathan Raman
2021-10-27 16:35   ` Stefan Hajnoczi
2021-10-11  5:31 ` [PATCH v3 08/12] vfio-user: handle DMA mappings Jagannathan Raman
2021-10-11  5:31 ` [PATCH v3 09/12] vfio-user: handle PCI BAR accesses Jagannathan Raman
2021-10-27 16:38   ` Stefan Hajnoczi
2021-10-11  5:31 ` [PATCH v3 10/12] vfio-user: handle device interrupts Jagannathan Raman
2021-10-11  5:31 ` [PATCH v3 11/12] vfio-user: register handlers to facilitate migration Jagannathan Raman
2021-10-27 18:30   ` Stefan Hajnoczi
2021-12-15 15:49     ` Jag Raman
2021-10-11  5:31 ` [PATCH v3 12/12] vfio-user: acceptance test Jagannathan Raman
2021-10-11 22:26   ` Philippe Mathieu-Daudé
2021-10-27 16:42   ` Stefan Hajnoczi
2021-10-27 18:33 ` [PATCH v3 00/12] vfio-user server in QEMU Stefan Hajnoczi

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=YXl8ilTXICU+0EHk@stefanha-x1.localdomain \
    --to=stefanha@redhat.com \
    --cc=alex.bennee@linaro.org \
    --cc=alex.williamson@redhat.com \
    --cc=elena.ufimtseva@oracle.com \
    --cc=jag.raman@oracle.com \
    --cc=john.g.johnson@oracle.com \
    --cc=john.levon@nutanix.com \
    --cc=marcandre.lureau@gmail.com \
    --cc=pbonzini@redhat.com \
    --cc=philmd@redhat.com \
    --cc=qemu-devel@nongnu.org \
    --cc=swapnil.ingle@nutanix.com \
    --cc=thanos.makatos@nutanix.com \
    --cc=thuth@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).