Skip to contentSkip to conversation

kvm tools: Fix VHOST_SET_MEM_TABLE failure

PATCH In review Asias He 3 participants 6 messages Jul 11, 2012 – Jul 12, 2012 KVM

lore
Share & tools
lore

Asias He

author
#0 lore raw

PATCH v2 1/2 kvm tools: Fix VHOST_SET_MEM_TABLE failure

VHOST_SET_MEM_TABLE failed: Operation not supported

In vhost_set_memory(), We have

        if (mem.padding)
                return -EOPNOTSUPP;

So, we need to zero struct vhost_memory.

---
 tools/kvm/virtio/net.c |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/kvm/virtio/net.c +1 -1
index ae17eb5..aa769d9 100644
@@ -471,7 +471,7 @@ static void virtio_net__vhost_init(struct kvm *kvm, struct net_dev *ndev)
 	if (ndev->vhost_fd < 0)
 		die_perror("Failed openning vhost-net device");
 
-	mem = malloc(sizeof(*mem) + sizeof(struct vhost_memory_region));
+	mem = calloc(1, sizeof(*mem) + sizeof(struct vhost_memory_region));
 	if (mem == NULL)
 		die("Failed allocating memory for vhost memory map");
 
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH v2 2/2 kvm tools: Do not poll ioeventfd if vhost is enabled

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.

---
 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(-)
tools/kvm/include/kvm/ioeventfd.h +1 -1
index 70cce9a..d71fa40 100644
@@ -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
tools/kvm/include/kvm/virtio.h +1 -0
index a957a5b..71b6bad 100644
@@ -72,6 +72,7 @@ enum virtio_trans {
 };
 
 struct virtio_device {
+	bool			use_vhost;
 	void			*virtio;
 	struct virtio_ops	*ops;
 };
tools/kvm/ioeventfd.c +4 -1
index 97deb06..226876f 100644
@@ -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,
tools/kvm/virtio/mmio.c +9 -1
index 8319954..1cf0815 100644
@@ -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;
 
tools/kvm/virtio/net.c +3 -0
index aa769d9..10420ae 100644
@@ -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);
 }
 
tools/kvm/virtio/pci.c +9 -1
index 8558341e..f17cd8a 100644
@@ -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;
 
Signature · 1 line
1.7.10.4
#2 lore raw
In reply to Asias He
Quoted 9 lines · Asias He

VHOST_SET_MEM_TABLE failed: Operation not supported

In vhost_set_memory(), We have

        if (mem.padding)
                return -EOPNOTSUPP;

So, we need to zero struct vhost_memory.

Is this due to a change in vhost?

Signature · 1 line
error compiling committee.c: too many arguments to function
author
#3 lore raw
Quoted 5 lines · Avi Kivity
Quoted 4 lines · Asias He

VHOST_SET_MEM_TABLE failed: Operation not supported

In vhost_set_memory(), We have

Quoted 7 lines
Quoted 5 lines
        if (mem.padding)
                return -EOPNOTSUPP;

So, we need to zero struct vhost_memory.

Is this due to a change in vhost?

Seems we have this bit in the very beginning (commit 3a4d5c94).

Signature · 1 line
Asias He
#4 lore raw
Quoted 9 lines · Asias He
Quoted 8 lines · Avi Kivity
Quoted 7 lines · Asias He

VHOST_SET_MEM_TABLE failed: Operation not supported

In vhost_set_memory(), We have

        if (mem.padding)
                return -EOPNOTSUPP;
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines

So, we need to zero struct vhost_memory.

Is this due to a change in vhost?

Seems we have this bit in the very beginning (commit 3a4d5c94).

Okay, so it's a documentation problem. Michael, where is the documentation for vhost-net?

Note we have to initialize it with memset(); presumably when we repurpose it the name will change, and anonymous unions are not very portable.

Signature · 1 line
error compiling committee.c: too many arguments to function
#5 lore raw
Quoted 13 lines · Avi Kivity
Quoted 12 lines · Asias He
Quoted 11 lines · Avi Kivity
Quoted 9 lines · Asias He

VHOST_SET_MEM_TABLE failed: Operation not supported

In vhost_set_memory(), We have

        if (mem.padding)
                return -EOPNOTSUPP;

So, we need to zero struct vhost_memory.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Is this due to a change in vhost?

Seems we have this bit in the very beginning (commit 3a4d5c94).

Okay, so it's a documentation problem. Michael, where is the documentation for vhost-net?

Most fields are documented in include/linux/vhost.h
Yes the approach vhost consistently takes is to require all unused fields to be zeroed out.

Quoted 1 line

Note we have to initialize it with memset();

We can also use = {} if we want to avoid naming it.

Quoted 3 lines

presumably when we
repurpose it the name will change, and anonymous unions are not very portable.

Looks like in the new C standard they are :)
I'm not sure what we'll do, exactly, if we need to reuse this padding for something else, but not breaking build for old userspace will be a priority.

Quoted 3 lines
Signature · 1 line
error compiling committee.c: too many arguments to function