@@ -22,7 +22,7 @@ struct ioevent {
int ioeventfd__init(struct kvm *kvm);
int ioeventfd__exit(struct kvm *kvm);
-int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio);
+int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio, bool poll_in_userspace);
int ioeventfd__del_event(u64 addr, u64 datamatch);
#endif
@@ -72,6 +72,7 @@ enum virtio_trans {
};
struct virtio_device {
+ bool use_vhost;
void *virtio;
struct virtio_ops *ops;
};
@@ -117,7 +117,7 @@ int ioeventfd__exit(struct kvm *kvm)
return 0;
}
-int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio)
+int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio, bool poll_in_userspace)
{
struct kvm_ioeventfd kvm_ioevent;
struct epoll_event epoll_event;
@@ -151,6 +151,9 @@ int ioeventfd__add_event(struct ioevent *ioevent, bool is_pio)
goto cleanup;
}
+ if (!poll_in_userspace)
+ return 0;
+
epoll_event = (struct epoll_event) {
.events = EPOLLIN,
.data.ptr = new_ioevent,
@@ -48,7 +48,15 @@ static int virtio_mmio_init_ioeventfd(struct kvm *kvm,
.fd = eventfd(0, 0),
};
- err = ioeventfd__add_event(&ioevent, false);
+ if (vdev->use_vhost)
+ /*
+ * Vhost will poll the eventfd in host kernel side,
+ * no need to poll in userspace.
+ */
+ err = ioeventfd__add_event(&ioevent, true, false);
+ else
+ /* Need to poll in userspace. */
+ err = ioeventfd__add_event(&ioevent, true, true);
if (err)
return err;
@@ -492,6 +492,9 @@ static void virtio_net__vhost_init(struct kvm *kvm, struct net_dev *ndev)
r = ioctl(ndev->vhost_fd, VHOST_SET_MEM_TABLE, mem);
if (r != 0)
die_perror("VHOST_SET_MEM_TABLE failed");
+
+ ndev->vdev.use_vhost = true;
+
free(mem);
}
@@ -40,7 +40,15 @@ static int virtio_pci__init_ioeventfd(struct kvm *kvm, struct virtio_device *vde
.fd = eventfd(0, 0),
};
- r = ioeventfd__add_event(&ioevent, true);
+ if (vdev->use_vhost)
+ /*
+ * Vhost will poll the eventfd in host kernel side,
+ * no need to poll in userspace.
+ */
+ r = ioeventfd__add_event(&ioevent, true, false);
+ else
+ /* Need to poll in userspace. */
+ r = ioeventfd__add_event(&ioevent, true, true);
if (r)
return r;
If vhost is enabled for a virtio device, vhost will poll the ioeventfd in kernel side and there is no need to poll it in userspace. Otherwise, both vhost kernel and userspace will race to poll. Signed-off-by: Asias He <asias.hejun@gmail.com> --- tools/kvm/include/kvm/ioeventfd.h | 2 +- tools/kvm/include/kvm/virtio.h | 1 + tools/kvm/ioeventfd.c | 5 ++++- tools/kvm/virtio/mmio.c | 10 +++++++++- tools/kvm/virtio/net.c | 3 +++ tools/kvm/virtio/pci.c | 10 +++++++++- 6 files changed, 27 insertions(+), 4 deletions(-)