diff mbox

[v2,2/2] kvm tools: Do not poll ioeventfd if vhost is enabled

Message ID 1342022894-1034-2-git-send-email-asias.hejun@gmail.com (mailing list archive)
State New, archived
Headers show

Commit Message

Asias He July 11, 2012, 4:08 p.m. UTC
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(-)
diff mbox

Patch

diff --git a/tools/kvm/include/kvm/ioeventfd.h b/tools/kvm/include/kvm/ioeventfd.h
index 70cce9a..d71fa40 100644
--- a/tools/kvm/include/kvm/ioeventfd.h
+++ b/tools/kvm/include/kvm/ioeventfd.h
@@ -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
diff --git a/tools/kvm/include/kvm/virtio.h b/tools/kvm/include/kvm/virtio.h
index a957a5b..71b6bad 100644
--- a/tools/kvm/include/kvm/virtio.h
+++ b/tools/kvm/include/kvm/virtio.h
@@ -72,6 +72,7 @@  enum virtio_trans {
 };
 
 struct virtio_device {
+	bool			use_vhost;
 	void			*virtio;
 	struct virtio_ops	*ops;
 };
diff --git a/tools/kvm/ioeventfd.c b/tools/kvm/ioeventfd.c
index 97deb06..226876f 100644
--- a/tools/kvm/ioeventfd.c
+++ b/tools/kvm/ioeventfd.c
@@ -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,
diff --git a/tools/kvm/virtio/mmio.c b/tools/kvm/virtio/mmio.c
index 8319954..1cf0815 100644
--- a/tools/kvm/virtio/mmio.c
+++ b/tools/kvm/virtio/mmio.c
@@ -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;
 
diff --git a/tools/kvm/virtio/net.c b/tools/kvm/virtio/net.c
index aa769d9..10420ae 100644
--- a/tools/kvm/virtio/net.c
+++ b/tools/kvm/virtio/net.c
@@ -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);
 }
 
diff --git a/tools/kvm/virtio/pci.c b/tools/kvm/virtio/pci.c
index 8558341e..f17cd8a 100644
--- a/tools/kvm/virtio/pci.c
+++ b/tools/kvm/virtio/pci.c
@@ -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;