Skip to content

Commit

Permalink
libvhost-user: Open userfaultfd
Browse files Browse the repository at this point in the history
Open a userfaultfd (on a postcopy_advise) and send it back in
the reply to the qemu for it to monitor.

Signed-off-by: Dr. David Alan Gilbert <[email protected]>
Reviewed-by: Marc-André Lureau <[email protected]>
Reviewed-by: Michael S. Tsirkin <[email protected]>
Signed-off-by: Michael S. Tsirkin <[email protected]>
  • Loading branch information
dagrh authored and mstsirkin committed Mar 20, 2018
1 parent d6e4771 commit 2a84ffc
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
42 changes: 40 additions & 2 deletions contrib/libvhost-user/libvhost-user.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,20 @@
#include <sys/socket.h>
#include <sys/eventfd.h>
#include <sys/mman.h>
#include "qemu/compiler.h"

#if defined(__linux__)
#include <sys/syscall.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/vhost.h>

#include "qemu/compiler.h"
#ifdef __NR_userfaultfd
#include <linux/userfaultfd.h>
#endif

#endif

#include "qemu/atomic.h"

#include "libvhost-user.h"
Expand Down Expand Up @@ -888,8 +899,35 @@ vu_set_config(VuDev *dev, VhostUserMsg *vmsg)
static bool
vu_set_postcopy_advise(VuDev *dev, VhostUserMsg *vmsg)
{
/* TODO: Open ufd, pass it back in the request */
dev->postcopy_ufd = -1;
#ifdef UFFDIO_API
struct uffdio_api api_struct;

dev->postcopy_ufd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK);
vmsg->size = 0;
#endif

if (dev->postcopy_ufd == -1) {
vu_panic(dev, "Userfaultfd not available: %s", strerror(errno));
goto out;
}

#ifdef UFFDIO_API
api_struct.api = UFFD_API;
api_struct.features = 0;
if (ioctl(dev->postcopy_ufd, UFFDIO_API, &api_struct)) {
vu_panic(dev, "Failed UFFDIO_API: %s", strerror(errno));
close(dev->postcopy_ufd);
dev->postcopy_ufd = -1;
goto out;
}
/* TODO: Stash feature flags somewhere */
#endif

out:
/* Return a ufd to the QEMU */
vmsg->fd_num = 1;
vmsg->fds[0] = dev->postcopy_ufd;
return true; /* = send a reply */
}

Expand Down
3 changes: 3 additions & 0 deletions contrib/libvhost-user/libvhost-user.h
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ struct VuDev {
* re-initialize */
vu_panic_cb panic;
const VuDevIface *iface;

/* Postcopy data */
int postcopy_ufd;
};

typedef struct VuVirtqElement {
Expand Down

0 comments on commit 2a84ffc

Please sign in to comment.