Skip to contentSkip to conversation

vhost: vhost TX zero-copy support

PATCH Superseded Shirley Ma 2 participants 23 messages May 16, 2011 – May 18, 2011

Subsystems (4)

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore

Shirley Ma

author
#0 lore raw

PATCH v5 4/6 vhost: vhost TX zero-copy support

This patch maintains the outstanding userspace buffers in the sequence it is delivered to vhost. The outstanding userspace buffers will be marked as done once the lower device buffers DMA has finished. This is monitored through last reference of kfree_skb callback. Two buffer index are used for this purpose.

The vhost passes the userspace buffers info to lower device skb through message control. Since there will be some done DMAs when entering vhost handle_tx. The worse case is all buffers in the vq are in pending/done status, so we need to notify guest to release DMA done buffers first before get any new buffers from the vq.

---

 drivers/vhost/net.c   |   39 +++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.c |   58 +++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/vhost/vhost.h |   12 ++++++++++
 3 files changed, 107 insertions(+), 2 deletions(-)
drivers/vhost/net.c +37 -2
index 2f7c76a..c964000 100644
@@ -32,6 +32,10 @@
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_NET_WEIGHT 0x80000
 
+/* MAX number of TX used buffers for outstanding zerocopy */
+#define VHOST_MAX_PEND 128
+#define VHOST_GOODCOPY_LEN 256
+
 enum {
 	VHOST_NET_VQ_RX = 0,
 	VHOST_NET_VQ_TX = 1,
@@ -129,6 +133,7 @@ static void handle_tx(struct vhost_net *net)
 	int err, wmem;
 	size_t hdr_size;
 	struct socket *sock;
+	struct skb_ubuf_info pend;
 
 	/* TODO: check that we are running from vhost_worker? */
 	sock = rcu_dereference_check(vq->private_data, 1);
@@ -151,6 +156,10 @@ static void handle_tx(struct vhost_net *net)
 	hdr_size = vq->vhost_hlen;
 
 	for (;;) {
+		/* Release DMAs done buffers first */
+		if (atomic_read(&vq->refcnt) > VHOST_MAX_ZEROCOPY_PEND)
+			vhost_zerocopy_signal_used(vq, false);
+
 		head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
 					 ARRAY_SIZE(vq->iov),
 					 &out, &in,
@@ -166,6 +175,12 @@ static void handle_tx(struct vhost_net *net)
 				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
 				break;
 			}
+			/* If more outstanding DMAs, queue the work */
+			if (atomic_read(&vq->refcnt) > VHOST_MAX_PEND) {
+				tx_poll_start(net, sock);
+				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
+				break;
+			}
 			if (unlikely(vhost_enable_notify(vq))) {
 				vhost_disable_notify(vq);
 				continue;
@@ -188,17 +203,37 @@ static void handle_tx(struct vhost_net *net)
 			       iov_length(vq->hdr, s), hdr_size);
 			break;
 		}
+		/* use msg_control to pass vhost zerocopy ubuf info to skb */
+		if (sock_flag(sock->sk, SOCK_ZEROCOPY)) {
+			vq->heads[vq->upend_idx].id = head;
+			if (len <= VHOST_GOODCOPY_LEN)
+				/* copy don't need to wait for DMA done */
+				vq->heads[vq->upend_idx].len =
+							VHOST_DMA_DONE_LEN;
+			else {
+				vq->heads[vq->upend_idx].len = len;
+				pend.callback = vhost_zerocopy_callback;
+				pend.arg = vq;
+				pend.desc = vq->upend_idx;
+				msg.msg_control = &pend;
+				msg.msg_controllen = sizeof(pend);
+			}
+			atomic_inc(&vq->refcnt);
+			vq->upend_idx = (vq->upend_idx + 1) % UIO_MAXIOV;
+		}
 		/* TODO: Check specific error and bomb out unless ENOBUFS? */
 		err = sock->ops->sendmsg(NULL, sock, &msg, len);
 		if (unlikely(err < 0)) {
-			vhost_discard_vq_desc(vq, 1);
+			if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+				vhost_discard_vq_desc(vq, 1);
 			tx_poll_start(net, sock);
 			break;
 		}
 		if (err != len)
 			pr_debug("Truncated TX packet: "
 				 " len %d != %zd\n", err, len);
-		vhost_add_used_and_signal(&net->dev, vq, head, 0);
+		if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+			vhost_add_used_and_signal(&net->dev, vq, head, 0);
 		total_len += len;
 		if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
 			vhost_poll_queue(&vq->poll);
drivers/vhost/vhost.c +58 -0

83 lines not on this page · raw message

drivers/vhost/vhost.h +12 -0
index b3363ae..8e3ecc7 100644
@@ -13,6 +13,10 @@
 #include <linux/virtio_ring.h>
 #include <asm/atomic.h>
 
+/* This is for zerocopy, used buffer len is set to 1 when lower device DMA
+ * done */
+#define VHOST_DMA_DONE_LEN	1
+
 struct vhost_device;
 
 struct vhost_work;
@@ -108,6 +112,12 @@ struct vhost_virtqueue {
 	/* Log write descriptors */
 	void __user *log_base;
 	struct vhost_log *log;
+	/* vhost zerocopy support */
+	atomic_t refcnt; /* num of outstanding zerocopy DMAs */
+	/* copy of avail idx to monitor outstanding DMA zerocopy buffers */
+	int upend_idx;
+	/* copy of used idx to monintor DMA done zerocopy buffers */
+	int done_idx;
 };
 
 struct vhost_dev {
@@ -154,6 +164,8 @@ bool vhost_enable_notify(struct vhost_virtqueue *);
 
 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
 		    unsigned int log_num, u64 len);
+void vhost_zerocopy_callback(struct sk_buff *skb);
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool shutdown);
 
 #define vq_err(vq, fmt, ...) do {                                  \
 		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
#1 lore raw
Quoted 5 lines

+/* Since we need to keep the order of used_idx as avail_idx, it's possible that
+ * DMA done not in order in lower device driver for some reason. To prevent
+ * used_idx out of order, upend_idx is used to track avail_idx order, done_idx
+ * is used to track used_idx order. Once lower device DMA done, then upend_idx
+ * can move to done_idx.

Could you clarify this please? virtio explicitly allows out of order completion of requests. Does it simplify code that we try to keep used index updates in-order? Because if not, this is not really a requirement.

Signature · 1 line
MST
author
#2 lore raw
Quoted 6 lines · Michael S. Tsirkin
Quoted 1 line

+/* Since we need to keep the order of used_idx as avail_idx, it's

possible that

Quoted 1 line

+ * DMA done not in order in lower device driver for some reason. To

prevent

Quoted 1 line

+ * used_idx out of order, upend_idx is used to track avail_idx

order, done_idx

Quoted 8 lines
Quoted 1 line

+ * is used to track used_idx order. Once lower device DMA done,

then upend_idx

Quoted 1 line

+ * can move to done_idx.

Could you clarify this please? virtio explicitly allows out of order completion of requests. Does it simplify code that we try to keep used index updates in-order? Because if not, this is not really a requirement.

Hello Mike,

Based on my testing, vhost_add_used() must be in order from vhost_get_vq_desc(). Otherwise, virtio_net ring seems get double freed. I didn't spend time on debugging this.

in virtqueue_get_buf

        if (unlikely(!vq->data[i])) {
                BAD_RING(vq, "id %u is not a head!\n", i);
                return NULL;
        }

That's the reason I created the upend_idx and done_idx.

Thanks
Shirley

#3 lore raw
Quoted 16 lines · Shirley Ma
Quoted 6 lines · Michael S. Tsirkin
Quoted 1 line

+/* Since we need to keep the order of used_idx as avail_idx, it's

possible that

Quoted 1 line

+ * DMA done not in order in lower device driver for some reason. To

prevent

Quoted 1 line

+ * used_idx out of order, upend_idx is used to track avail_idx

order, done_idx

Quoted 8 lines
Quoted 1 line

+ * is used to track used_idx order. Once lower device DMA done,

then upend_idx

Quoted 1 line

+ * can move to done_idx.

Could you clarify this please? virtio explicitly allows out of order completion of requests. Does it simplify code that we try to keep used index updates in-order? Because if not, this is not really a requirement.

Quoted 5 lines

Hello Mike,

Based on my testing, vhost_add_used() must be in order from vhost_get_vq_desc(). Otherwise, virtio_net ring seems get double freed.

Double-freed or you get NULL below?

Quoted 8 lines

I didn't spend time on debugging this.

in virtqueue_get_buf

        if (unlikely(!vq->data[i])) {
                BAD_RING(vq, "id %u is not a head!\n", i);
                return NULL;
        }

Yes but i used here is the head that we read from the ring, not the ring index itself.

	i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id

we must complete any id only once, but in any order.

Quoted 4 lines

That's the reason I created the upend_idx and done_idx.

Thanks
Shirley

Very strange, it sounds like a bug, but I can't tell where: in host or in guest. If it's in the guest, we must fix it. If in host, we should only fix it if it makes life simpler for us. Could you try to nail it down pls? Another question: will code get simpler or more complex if that restriction's removed?

Signature · 1 line
MST
author
#4 lore raw
Quoted 5 lines · Michael S. Tsirkin

Very strange, it sounds like a bug, but I can't tell where: in host or in guest. If it's in the guest, we must fix it. If in host, we should only fix it if it makes life simpler for us. Could you try to nail it down pls? Another question: will code get simpler or more complex if that restriction's removed?

It should be similar. We still need to maintain the pending list, and mark the DMA done ids.

I can make a try to narrow this issue down.

Thanks
Shirley

author
#5 lore raw
In reply to Michael S. Tsirkin
Quoted 20 lines · Michael S. Tsirkin
Quoted 2 lines · Shirley Ma
Quoted 1 line · Michael S. Tsirkin
Quoted 1 line

+/* Since we need to keep the order of used_idx as avail_idx,

it's

Quoted 2 lines
Quoted 2 lines

possible that

Quoted 1 line

+ * DMA done not in order in lower device driver for some

reason. To

Quoted 8 lines
Quoted 8 lines

prevent

Quoted 1 line

+ * used_idx out of order, upend_idx is used to track avail_idx

order, done_idx

Quoted 1 line

+ * is used to track used_idx order. Once lower device DMA done,

then upend_idx

Quoted 1 line

+ * can move to done_idx.

Could you clarify this please? virtio explicitly allows out of

order

Quoted 4 lines
Quoted 3 lines

completion of requests. Does it simplify code that we try to keep used index updates in-order? Because if not, this is not really a requirement.

Quoted 7 lines
Quoted 5 lines

Hello Mike,

Based on my testing, vhost_add_used() must be in order from vhost_get_vq_desc(). Otherwise, virtio_net ring seems get double freed.

Double-freed or you get NULL below?

More likely is NULL.

Quoted 9 lines
Quoted 8 lines

I didn't spend time on debugging this.

in virtqueue_get_buf

        if (unlikely(!vq->data[i])) {
                BAD_RING(vq, "id %u is not a head!\n", i);
                return NULL;
        }
Quoted 4 lines

Yes but i used here is the head that we read from the ring, not the ring index itself.

        i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id

we must complete any id only once, but in any order.

It is in any order of ring id, but must be in the order of "head" returns from vhost_get_vq_desc(), any clue?

Thanks
Shirley

#6 lore raw
Quoted 45 lines · Shirley Ma
Quoted 20 lines · Michael S. Tsirkin
Quoted 2 lines · Shirley Ma
Quoted 1 line · Michael S. Tsirkin
Quoted 1 line

+/* Since we need to keep the order of used_idx as avail_idx,

it's

Quoted 2 lines
Quoted 2 lines

possible that

Quoted 1 line

+ * DMA done not in order in lower device driver for some

reason. To

Quoted 8 lines
Quoted 8 lines

prevent

Quoted 1 line

+ * used_idx out of order, upend_idx is used to track avail_idx

order, done_idx

Quoted 1 line

+ * is used to track used_idx order. Once lower device DMA done,

then upend_idx

Quoted 1 line

+ * can move to done_idx.

Could you clarify this please? virtio explicitly allows out of

order

Quoted 4 lines
Quoted 3 lines

completion of requests. Does it simplify code that we try to keep used index updates in-order? Because if not, this is not really a requirement.

Quoted 7 lines
Quoted 5 lines

Hello Mike,

Based on my testing, vhost_add_used() must be in order from vhost_get_vq_desc(). Otherwise, virtio_net ring seems get double freed.

Double-freed or you get NULL below?

More likely is NULL.

Quoted 9 lines
Quoted 8 lines

I didn't spend time on debugging this.

in virtqueue_get_buf

        if (unlikely(!vq->data[i])) {
                BAD_RING(vq, "id %u is not a head!\n", i);
                return NULL;
        }
Quoted 4 lines

Yes but i used here is the head that we read from the ring, not the ring index itself.

        i = vq->vring.used->ring[vq->last_used_idx%vq->vring.num].id

we must complete any id only once, but in any order.

Quoted 6 lines

It is in any order of ring id, but must be in the order of "head" returns from vhost_get_vq_desc(), any clue?

Thanks
Shirley

Something in your patch that overwrites the id in vhost and makes it put the wrong id in the used ring?

By the way, need to keep in mind that a guest can give us the same head twice, need to make sure this at least does not corrupt host memory.

Signature · 1 line
MST
author
#7 lore raw
Quoted 6 lines · Michael S. Tsirkin

Something in your patch that overwrites the id in vhost and makes it put the wrong id in the used ring?

By the way, need to keep in mind that a guest can give us the same head twice, need to make sure this at least does not corrupt host memory.

I think I didn't explain the problem very well here.

This patch doesn't overwrite the id. It just keeps the same coming sequence from "head return vhost_get_vq_desc()" to pass to vhost_add_used.

The same ids can be used many times once it passes to guest from vhost_add_used. There is no problem. The zero copy patch doesn't have any issue.

The problem is the order of head from return vhost_get_vq_desc should be in sequence when it passes to vhost_add_used.

The original code has no problem, because it gets one head and pass that head to vhost_add_used one by one once done the copy. So it's in sequence.

This issue can easily recreate without zerocopy patch by simply changing the order from "head return vhost_get_vq_desc" when passing to vhost_add_used.

Thanks
Shirley

#8 lore raw
Quoted 14 lines · Shirley Ma
Quoted 6 lines · Michael S. Tsirkin

Something in your patch that overwrites the id in vhost and makes it put the wrong id in the used ring?

By the way, need to keep in mind that a guest can give us the same head twice, need to make sure this at least does not corrupt host memory.

I think I didn't explain the problem very well here.

This patch doesn't overwrite the id. It just keeps the same coming sequence from "head return vhost_get_vq_desc()" to pass to vhost_add_used.

Quoted 6 lines

The same ids can be used many times once it passes to guest from vhost_add_used. There is no problem. The zero copy patch doesn't have any issue.

The problem is the order of head from return vhost_get_vq_desc should be in sequence when it passes to vhost_add_used.

Which is the order the descriptors are put on avail ring. By design, guest should not depend on used ring entries being in order with avail ring (and btw with virtio block, they aren't). If it does, it's a bug I think.

Quoted 10 lines

The original code has no problem, because it gets one head and pass that head to vhost_add_used one by one once done the copy. So it's in sequence.

This issue can easily recreate without zerocopy patch by simply changing the order from "head return vhost_get_vq_desc" when passing to vhost_add_used.

Thanks
Shirley

Ah, did you try that? Could you post this patch pls? This seems to imply a bug in guest virtio.

Signature · 1 line
MST
author
#9 lore raw
Quoted 4 lines · Michael S. Tsirkin

Which is the order the descriptors are put on avail ring. By design, guest should not depend on used ring entries being in order with avail ring (and btw with virtio block, they aren't). If it does, it's a bug I think.

Ok, I thought, the order should be maintained.

Quoted 7 lines
Quoted 1 line

The original code has no problem, because it gets one head and pass

that

Quoted 4 lines

head to vhost_add_used one by one once done the copy. So it's in sequence.

This issue can easily recreate without zerocopy patch by simply

changing

Quoted 8 lines
Quoted 5 lines

the order from "head return vhost_get_vq_desc" when passing to vhost_add_used.

Thanks
Shirley

Ah, did you try that? Could you post this patch pls? This seems to imply a bug in guest virtio.

I am creating the patch against net-next for you to test today.

Thanks
Shirley

author
#10 lore raw

PATCH 1/1 vhost: accumulate multiple used and sigal in vhost TX test

Hello Michael,

Here is the patch I used to test out of order before: add used in a pend array, and swap the last two ids.

I used to hit an issue, but now it seems working well.

This won't impact zero-copy patch since we need to maintain the pend used ids anyway.

---
 drivers/vhost/net.c   |   24 +++++++++++++++++++++++-
 drivers/vhost/vhost.c |   11 +++++++++++
 drivers/vhost/vhost.h |    1 +
 3 files changed, 35 insertions(+), 1 deletions(-)
drivers/vhost/net.c +23 -1
index 2f7c76a..19e1baa 100644
@@ -32,6 +32,8 @@
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_NET_WEIGHT 0x80000
 
+#define VHOST_MAX_PEND	128
+
 enum {
 	VHOST_NET_VQ_RX = 0,
 	VHOST_NET_VQ_TX = 1,
@@ -198,13 +200,33 @@ static void handle_tx(struct vhost_net *net)
 		if (err != len)
 			pr_debug("Truncated TX packet: "
 				 " len %d != %zd\n", err, len);
-		vhost_add_used_and_signal(&net->dev, vq, head, 0);
+		vq->heads[vq->pend_idx].id = head;
+		vq->heads[vq->pend_idx].len = 0;
+		++vq->pend_idx;
+		if (vq->pend_idx >= VHOST_MAX_PEND) {
+			int id;
+			id = vq->heads[vq->pend_idx-1].id;
+			vq->heads[vq->pend_idx-1].id = vq->heads[vq->pend_idx-2].id; 
+			vq->heads[vq->pend_idx-2].id = id;
+			vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
+						    vq->pend_idx);
+			vq->pend_idx = 0;
+		}
 		total_len += len;
 		if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
 			vhost_poll_queue(&vq->poll);
 			break;
 		}
 	}
+	if (vq->pend_idx >= VHOST_MAX_PEND) {
+		int id;
+		id = vq->heads[vq->pend_idx-1].id;
+		vq->heads[vq->pend_idx-1].id = vq->heads[vq->pend_idx-2].id;
+		vq->heads[vq->pend_idx-2].id = id;
+		vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
+					    vq->pend_idx);
+		vq->pend_idx = 0;
+	}
 
 	mutex_unlock(&vq->mutex);
 }
drivers/vhost/vhost.c +11 -0
index 2ab2912..7eea6b3 100644
@@ -174,6 +174,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
 	vq->call_ctx = NULL;
 	vq->call = NULL;
 	vq->log_ctx = NULL;
+	vq->pend_idx = 0;
 }
 
 static int vhost_worker(void *data)
@@ -395,6 +396,11 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
 			vhost_poll_stop(&dev->vqs[i].poll);
 			vhost_poll_flush(&dev->vqs[i].poll);
 		}
+		if (dev->vqs[i].pend_idx != 0) {
+			vhost_add_used_and_signal_n(dev, &dev->vqs[i],
+				dev->vqs[i].heads, dev->vqs[i].pend_idx);
+			dev->vqs[i].pend_idx = 0;
+		}
 		if (dev->vqs[i].error_ctx)
 			eventfd_ctx_put(dev->vqs[i].error_ctx);
 		if (dev->vqs[i].error)
@@ -603,6 +609,11 @@ static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
 
 	mutex_lock(&vq->mutex);
 
+	if (vq->pend_idx != 0) {
+		vhost_add_used_and_signal_n(d, vq, vq->heads, vq->pend_idx);
+		vq->pend_idx = 0;
+	}
+
 	switch (ioctl) {
 	case VHOST_SET_VRING_NUM:
 		/* Resizing ring with an active backend?
drivers/vhost/vhost.h +1 -0
index b3363ae..44a412d 100644
@@ -108,6 +108,7 @@ struct vhost_virtqueue {
 	/* Log write descriptors */
 	void __user *log_base;
 	struct vhost_log *log;
+	int pend_idx;
 };
 
 struct vhost_dev {
#11 lore raw

Re: [TEST PATCH net-next] vhost: accumulate multiple used and sigal in vhost TX test

Quoted 6 lines · Shirley Ma

Hello Michael,

Here is the patch I used to test out of order before: add used in a pend array, and swap the last two ids.

I used to hit an issue, but now it seems working well.

Aha, so if I apply this guest will *not* crash? :)

Quoted 2 lines

This won't impact zero-copy patch since we need to maintain the pend used ids anyway.

Yes, but now we can mark them used immediately as they are completed - and I am guessing this will relieve the pressure on tx ring that you see?

Quoted 104 lines
---
 drivers/vhost/net.c   |   24 +++++++++++++++++++++++-
 drivers/vhost/vhost.c |   11 +++++++++++
 drivers/vhost/vhost.h |    1 +
 3 files changed, 35 insertions(+), 1 deletions(-)
drivers/vhost/net.c +23 -1
index 2f7c76a..19e1baa 100644
@@ -32,6 +32,8 @@
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_NET_WEIGHT 0x80000
 
+#define VHOST_MAX_PEND	128
+
 enum {
 	VHOST_NET_VQ_RX = 0,
 	VHOST_NET_VQ_TX = 1,
@@ -198,13 +200,33 @@ static void handle_tx(struct vhost_net *net)
 		if (err != len)
 			pr_debug("Truncated TX packet: "
 				 " len %d != %zd\n", err, len);
-		vhost_add_used_and_signal(&net->dev, vq, head, 0);
+		vq->heads[vq->pend_idx].id = head;
+		vq->heads[vq->pend_idx].len = 0;
+		++vq->pend_idx;
+		if (vq->pend_idx >= VHOST_MAX_PEND) {
+			int id;
+			id = vq->heads[vq->pend_idx-1].id;
+			vq->heads[vq->pend_idx-1].id = vq->heads[vq->pend_idx-2].id; 
+			vq->heads[vq->pend_idx-2].id = id;
+			vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
+						    vq->pend_idx);
+			vq->pend_idx = 0;
+		}
 		total_len += len;
 		if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
 			vhost_poll_queue(&vq->poll);
 			break;
 		}
 	}
+	if (vq->pend_idx >= VHOST_MAX_PEND) {
+		int id;
+		id = vq->heads[vq->pend_idx-1].id;
+		vq->heads[vq->pend_idx-1].id = vq->heads[vq->pend_idx-2].id;
+		vq->heads[vq->pend_idx-2].id = id;
+		vhost_add_used_and_signal_n(&net->dev, vq, vq->heads,
+					    vq->pend_idx);
+		vq->pend_idx = 0;
+	}
 
 	mutex_unlock(&vq->mutex);
 }
drivers/vhost/vhost.c +11 -0
index 2ab2912..7eea6b3 100644
@@ -174,6 +174,7 @@ static void vhost_vq_reset(struct vhost_dev *dev,
 	vq->call_ctx = NULL;
 	vq->call = NULL;
 	vq->log_ctx = NULL;
+	vq->pend_idx = 0;
 }
 
 static int vhost_worker(void *data)
@@ -395,6 +396,11 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
 			vhost_poll_stop(&dev->vqs[i].poll);
 			vhost_poll_flush(&dev->vqs[i].poll);
 		}
+		if (dev->vqs[i].pend_idx != 0) {
+			vhost_add_used_and_signal_n(dev, &dev->vqs[i],
+				dev->vqs[i].heads, dev->vqs[i].pend_idx);
+			dev->vqs[i].pend_idx = 0;
+		}
 		if (dev->vqs[i].error_ctx)
 			eventfd_ctx_put(dev->vqs[i].error_ctx);
 		if (dev->vqs[i].error)
@@ -603,6 +609,11 @@ static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
 
 	mutex_lock(&vq->mutex);
 
+	if (vq->pend_idx != 0) {
+		vhost_add_used_and_signal_n(d, vq, vq->heads, vq->pend_idx);
+		vq->pend_idx = 0;
+	}
+
 	switch (ioctl) {
 	case VHOST_SET_VRING_NUM:
 		/* Resizing ring with an active backend?
drivers/vhost/vhost.h +1 -0
index b3363ae..44a412d 100644
@@ -108,6 +108,7 @@ struct vhost_virtqueue {
 	/* Log write descriptors */
 	void __user *log_base;
 	struct vhost_log *log;
+	int pend_idx;
 };
 
 struct vhost_dev {
author
#12 lore raw
In reply to Shirley Ma

Resubmit the patch with most update. This patch passed some live-migration test against RHEL6.2. I will run more stress test w/i live migration.

---

 drivers/vhost/net.c   |   37 +++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.h |   12 ++++++++++
 3 files changed, 101 insertions(+), 3 deletions(-)
drivers/vhost/net.c +35 -2
index 2f7c76a..6bd6e28 100644
@@ -32,6 +32,9 @@
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_NET_WEIGHT 0x80000
 
+/* MAX number of TX used buffers for outstanding zerocopy */
+#define VHOST_MAX_ZEROCOPY_PEND 128 
+
 enum {
 	VHOST_NET_VQ_RX = 0,
 	VHOST_NET_VQ_TX = 1,
@@ -129,6 +132,7 @@ static void handle_tx(struct vhost_net *net)
 	int err, wmem;
 	size_t hdr_size;
 	struct socket *sock;
+	struct skb_ubuf_info pend;
 
 	/* TODO: check that we are running from vhost_worker? */
 	sock = rcu_dereference_check(vq->private_data, 1);
@@ -151,6 +155,10 @@ static void handle_tx(struct vhost_net *net)
 	hdr_size = vq->vhost_hlen;
 
 	for (;;) {
+		/* Release DMAs done buffers first */
+		if (atomic_read(&vq->refcnt) > VHOST_MAX_ZEROCOPY_PEND)
+			vhost_zerocopy_signal_used(vq, false);
+
 		head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
 					 ARRAY_SIZE(vq->iov),
 					 &out, &in,
@@ -166,6 +174,13 @@ static void handle_tx(struct vhost_net *net)
 				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
 				break;
 			}
+			/* If more outstanding DMAs, queue the work */
+			if (sock_flag(sock->sk, SOCK_ZEROCOPY) &&
+			    (atomic_read(&vq->refcnt) > VHOST_MAX_ZEROCOPY_PEND)) {
+				tx_poll_start(net, sock);
+				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
+				break;
+			}
 			if (unlikely(vhost_enable_notify(vq))) {
 				vhost_disable_notify(vq);
 				continue;
@@ -188,17 +203,35 @@ static void handle_tx(struct vhost_net *net)
 			       iov_length(vq->hdr, s), hdr_size);
 			break;
 		}
+		/* use msg_control to pass vhost zerocopy ubuf info to skb */
+		if (sock_flag(sock->sk, SOCK_ZEROCOPY)) {
+			vq->heads[vq->upend_idx].id = head;
+			if (len <= 128)
+				vq->heads[vq->upend_idx].len = VHOST_DMA_DONE_LEN;
+			else {
+				vq->heads[vq->upend_idx].len = len;
+				pend.callback = vhost_zerocopy_callback;
+				pend.arg = vq;
+				pend.desc = vq->upend_idx;
+				msg.msg_control = &pend;
+				msg.msg_controllen = sizeof(pend);
+			}
+			atomic_inc(&vq->refcnt);
+			vq->upend_idx = (vq->upend_idx + 1) % UIO_MAXIOV;
+		}
 		/* TODO: Check specific error and bomb out unless ENOBUFS? */
 		err = sock->ops->sendmsg(NULL, sock, &msg, len);
 		if (unlikely(err < 0)) {
-			vhost_discard_vq_desc(vq, 1);
+			if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+				vhost_discard_vq_desc(vq, 1);
 			tx_poll_start(net, sock);
 			break;
 		}
 		if (err != len)
 			pr_debug("Truncated TX packet: "
 				 " len %d != %zd\n", err, len);
-		vhost_add_used_and_signal(&net->dev, vq, head, 0);
+		if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+			vhost_add_used_and_signal(&net->dev, vq, head, 0);
 		total_len += len;
 		if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
 			vhost_poll_queue(&vq->poll);
drivers/vhost/vhost.c +54 -1

88 lines not on this page · raw message

drivers/vhost/vhost.h +12 -0
index b3363ae..8e3ecc7 100644
@@ -13,6 +13,10 @@
 #include <linux/virtio_ring.h>
 #include <asm/atomic.h>
 
+/* This is for zerocopy, used buffer len is set to 1 when lower device DMA
+ * done */
+#define VHOST_DMA_DONE_LEN	1
+
 struct vhost_device;
 
 struct vhost_work;
@@ -108,6 +112,12 @@ struct vhost_virtqueue {
 	/* Log write descriptors */
 	void __user *log_base;
 	struct vhost_log *log;
+	/* vhost zerocopy support */
+	atomic_t refcnt; /* num of outstanding zerocopy DMAs */
+	/* copy of avail idx to monitor outstanding DMA zerocopy buffers */
+	int upend_idx;
+	/* copy of used idx to monintor DMA done zerocopy buffers */
+	int done_idx;
 };
 
 struct vhost_dev {
@@ -154,6 +164,8 @@ bool vhost_enable_notify(struct vhost_virtqueue *);
 
 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
 		    unsigned int log_num, u64 len);
+void vhost_zerocopy_callback(struct sk_buff *skb);
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool shutdown);
 
 #define vq_err(vq, fmt, ...) do {                                  \
 		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
#13 lore raw
Quoted 3 lines · Shirley Ma

Resubmit the patch with most update. This patch passed some live-migration test against RHEL6.2. I will run more stress test w/i live migration.

What changed from the last version?

Quoted 229 lines

---

 drivers/vhost/net.c   |   37 +++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.h |   12 ++++++++++
 3 files changed, 101 insertions(+), 3 deletions(-)
drivers/vhost/net.c +35 -2
index 2f7c76a..6bd6e28 100644
@@ -32,6 +32,9 @@
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_NET_WEIGHT 0x80000
 
+/* MAX number of TX used buffers for outstanding zerocopy */
+#define VHOST_MAX_ZEROCOPY_PEND 128 
+
 enum {
 	VHOST_NET_VQ_RX = 0,
 	VHOST_NET_VQ_TX = 1,
@@ -129,6 +132,7 @@ static void handle_tx(struct vhost_net *net)
 	int err, wmem;
 	size_t hdr_size;
 	struct socket *sock;
+	struct skb_ubuf_info pend;
 
 	/* TODO: check that we are running from vhost_worker? */
 	sock = rcu_dereference_check(vq->private_data, 1);
@@ -151,6 +155,10 @@ static void handle_tx(struct vhost_net *net)
 	hdr_size = vq->vhost_hlen;
 
 	for (;;) {
+		/* Release DMAs done buffers first */
+		if (atomic_read(&vq->refcnt) > VHOST_MAX_ZEROCOPY_PEND)
+			vhost_zerocopy_signal_used(vq, false);
+
 		head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
 					 ARRAY_SIZE(vq->iov),
 					 &out, &in,
@@ -166,6 +174,13 @@ static void handle_tx(struct vhost_net *net)
 				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
 				break;
 			}
+			/* If more outstanding DMAs, queue the work */
+			if (sock_flag(sock->sk, SOCK_ZEROCOPY) &&
+			    (atomic_read(&vq->refcnt) > VHOST_MAX_ZEROCOPY_PEND)) {
+				tx_poll_start(net, sock);
+				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
+				break;
+			}
 			if (unlikely(vhost_enable_notify(vq))) {
 				vhost_disable_notify(vq);
 				continue;
@@ -188,17 +203,35 @@ static void handle_tx(struct vhost_net *net)
 			       iov_length(vq->hdr, s), hdr_size);
 			break;
 		}
+		/* use msg_control to pass vhost zerocopy ubuf info to skb */
+		if (sock_flag(sock->sk, SOCK_ZEROCOPY)) {
+			vq->heads[vq->upend_idx].id = head;
+			if (len <= 128)
+				vq->heads[vq->upend_idx].len = VHOST_DMA_DONE_LEN;
+			else {
+				vq->heads[vq->upend_idx].len = len;
+				pend.callback = vhost_zerocopy_callback;
+				pend.arg = vq;
+				pend.desc = vq->upend_idx;
+				msg.msg_control = &pend;
+				msg.msg_controllen = sizeof(pend);
+			}
+			atomic_inc(&vq->refcnt);
+			vq->upend_idx = (vq->upend_idx + 1) % UIO_MAXIOV;
+		}
 		/* TODO: Check specific error and bomb out unless ENOBUFS? */
 		err = sock->ops->sendmsg(NULL, sock, &msg, len);
 		if (unlikely(err < 0)) {
-			vhost_discard_vq_desc(vq, 1);
+			if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+				vhost_discard_vq_desc(vq, 1);
 			tx_poll_start(net, sock);
 			break;
 		}
 		if (err != len)
 			pr_debug("Truncated TX packet: "
 				 " len %d != %zd\n", err, len);
-		vhost_add_used_and_signal(&net->dev, vq, head, 0);
+		if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+			vhost_add_used_and_signal(&net->dev, vq, head, 0);
 		total_len += len;
 		if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
 			vhost_poll_queue(&vq->poll);
drivers/vhost/vhost.c +54 -1
index 2ab2912..ce799d6 100644
@@ -174,6 +174,9 @@ static void vhost_vq_reset(struct vhost_dev *dev,
 	vq->call_ctx = NULL;
 	vq->call = NULL;
 	vq->log_ctx = NULL;
+	vq->upend_idx = 0;
+	vq->done_idx = 0;
+	atomic_set(&vq->refcnt, 0);
 }
 
 static int vhost_worker(void *data)
@@ -230,7 +233,7 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
 					       UIO_MAXIOV, GFP_KERNEL);
 		dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
 					  GFP_KERNEL);
-		dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
+		dev->vqs[i].heads = kzalloc(sizeof *dev->vqs[i].heads *
 					    UIO_MAXIOV, GFP_KERNEL);
 
 		if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
@@ -385,6 +388,38 @@ long vhost_dev_reset_owner(struct vhost_dev *dev)
 	return 0;
 }
 
+/* 
+	comments
+*/
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool shutdown)
+{
+	int i, j = 0;
+
+	i = vq->done_idx;
+	while (i != vq->upend_idx) {
+		if ((vq->heads[i].len == VHOST_DMA_DONE_LEN) || shutdown) {
+			/* reset len = 0 */
+			vq->heads[i].len = 0;
+			i = (i + 1) % UIO_MAXIOV;
+			++j;
+		} else
+			break;
+	}
+	if (j) {
+		/* comments */
+		if (i > vq->done_idx)
+			vhost_add_used_n(vq, &vq->heads[vq->done_idx], j);
+		else {
+			vhost_add_used_n(vq, &vq->heads[vq->done_idx],
+					 UIO_MAXIOV - vq->done_idx);
+			vhost_add_used_n(vq, vq->heads, i);
+		}
+		vq->done_idx = i;
+		vhost_signal(vq->dev, vq);
+		atomic_sub(j, &vq->refcnt);
+	}
+}
+
 /* Caller should have device mutex */
 void vhost_dev_cleanup(struct vhost_dev *dev)
 {
@@ -395,6 +430,11 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
 			vhost_poll_stop(&dev->vqs[i].poll);
 			vhost_poll_flush(&dev->vqs[i].poll);
 		}
+		/* wait for all lower device DMAs done, then notify guest */
+		if (atomic_read(&dev->vqs[i].refcnt)) {
+			msleep(1000);
+			vhost_zerocopy_signal_used(&dev->vqs[i], true);
+		}
 		if (dev->vqs[i].error_ctx)
 			eventfd_ctx_put(dev->vqs[i].error_ctx);
 		if (dev->vqs[i].error)
@@ -603,6 +643,10 @@ static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
 
 	mutex_lock(&vq->mutex);
 
+	/* force all lower device DMAs done */
+	if (atomic_read(&vq->refcnt)) 
+		vhost_zerocopy_signal_used(vq, true);
+
 	switch (ioctl) {
 	case VHOST_SET_VRING_NUM:
 		/* Resizing ring with an active backend?
@@ -1416,3 +1460,12 @@ void vhost_disable_notify(struct vhost_virtqueue *vq)
 		vq_err(vq, "Failed to enable notification at %p: %d\n",
 		       &vq->used->flags, r);
 }
+
+void vhost_zerocopy_callback(struct sk_buff *skb)
+{
+	int idx = skb_shinfo(skb)->ubuf.desc;
+	struct vhost_virtqueue *vq = skb_shinfo(skb)->ubuf.arg;
+
+	/* set len = 1 to mark this desc buffers done DMA */
+	vq->heads[idx].len = VHOST_DMA_DONE_LEN;
+}
drivers/vhost/vhost.h +12 -0
index b3363ae..8e3ecc7 100644
@@ -13,6 +13,10 @@
 #include <linux/virtio_ring.h>
 #include <asm/atomic.h>
 
+/* This is for zerocopy, used buffer len is set to 1 when lower device DMA
+ * done */
+#define VHOST_DMA_DONE_LEN	1
+
 struct vhost_device;
 
 struct vhost_work;
@@ -108,6 +112,12 @@ struct vhost_virtqueue {
 	/* Log write descriptors */
 	void __user *log_base;
 	struct vhost_log *log;
+	/* vhost zerocopy support */
+	atomic_t refcnt; /* num of outstanding zerocopy DMAs */
+	/* copy of avail idx to monitor outstanding DMA zerocopy buffers */
+	int upend_idx;
+	/* copy of used idx to monintor DMA done zerocopy buffers */
+	int done_idx;
 };
 
 struct vhost_dev {
@@ -154,6 +164,8 @@ bool vhost_enable_notify(struct vhost_virtqueue *);
 
 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
 		    unsigned int log_num, u64 len);
+void vhost_zerocopy_callback(struct sk_buff *skb);
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool shutdown);
 
 #define vq_err(vq, fmt, ...) do {                                  \
 		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
author
#14 lore raw
Quoted 6 lines · Michael S. Tsirkin
Quoted 3 lines · Shirley Ma

Resubmit the patch with most update. This patch passed some live-migration test against RHEL6.2. I will run more stress test w/i live migration.

What changed from the last version?

Sorry, I forgot to mention: add clean up pending before set_vring.

Thanks
Shirley

#15 lore raw
In reply to Shirley Ma
Quoted 5 lines · Shirley Ma

Resubmit the patch with most update. This patch passed some live-migration test against RHEL6.2. I will run more stress test w/i live migration.

Cool. cleanup path needs a fix - are you use you can not use kobj or some other existing refcounting?

Is perf regressiion caused by tx ring overrun gone now?

I added some comments about how we might be aqble to complete requests out of order but it's not a must.

Quoted 55 lines

---

 drivers/vhost/net.c   |   37 +++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.c |   55 ++++++++++++++++++++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.h |   12 ++++++++++
 3 files changed, 101 insertions(+), 3 deletions(-)
drivers/vhost/net.c +15 -0
index 2f7c76a..6bd6e28 100644
@@ -32,6 +32,9 @@
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_NET_WEIGHT 0x80000
 
+/* MAX number of TX used buffers for outstanding zerocopy */
+#define VHOST_MAX_ZEROCOPY_PEND 128 
+
 enum {
 	VHOST_NET_VQ_RX = 0,
 	VHOST_NET_VQ_TX = 1,
@@ -129,6 +132,7 @@ static void handle_tx(struct vhost_net *net)
 	int err, wmem;
 	size_t hdr_size;
 	struct socket *sock;
+	struct skb_ubuf_info pend;
 
 	/* TODO: check that we are running from vhost_worker? */
 	sock = rcu_dereference_check(vq->private_data, 1);
@@ -151,6 +155,10 @@ static void handle_tx(struct vhost_net *net)
 	hdr_size = vq->vhost_hlen;
 
 	for (;;) {
+		/* Release DMAs done buffers first */
+		if (atomic_read(&vq->refcnt) > VHOST_MAX_ZEROCOPY_PEND)
+			vhost_zerocopy_signal_used(vq, false);
+
 		head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
 					 ARRAY_SIZE(vq->iov),
 					 &out, &in,
@@ -166,6 +174,13 @@ static void handle_tx(struct vhost_net *net)
 				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
 				break;
 			}
+			/* If more outstanding DMAs, queue the work */
+			if (sock_flag(sock->sk, SOCK_ZEROCOPY) &&
+			    (atomic_read(&vq->refcnt) > VHOST_MAX_ZEROCOPY_PEND)) {
+				tx_poll_start(net, sock);
+				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
+				break;
+			}
 			if (unlikely(vhost_enable_notify(vq))) {
 				vhost_disable_notify(vq);
 				continue;

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 209

Quoted 7 lines
drivers/vhost/net.c +4 -0
@@ -188,17 +203,35 @@ static void handle_tx(struct vhost_net *net)
 			       iov_length(vq->hdr, s), hdr_size);
 			break;
 		}
+		/* use msg_control to pass vhost zerocopy ubuf info to skb */
+		if (sock_flag(sock->sk, SOCK_ZEROCOPY)) {
+			vq->heads[vq->upend_idx].id = head;
+			if (len <= 128)

I thought we have a constant for that?

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 220

Quoted 11 lines
drivers/vhost/net.c +11 -0
+				vq->heads[vq->upend_idx].len = VHOST_DMA_DONE_LEN;
+			else {
+				vq->heads[vq->upend_idx].len = len;
+				pend.callback = vhost_zerocopy_callback;
+				pend.arg = vq;
+				pend.desc = vq->upend_idx;
+				msg.msg_control = &pend;
+				msg.msg_controllen = sizeof(pend);
+			}
+			atomic_inc(&vq->refcnt);
+			vq->upend_idx = (vq->upend_idx + 1) % UIO_MAXIOV;

Ok, so we deal with a cyclic ring apparently? What guarantees we don't overrun it?

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 226

Quoted 7 lines
drivers/vhost/net.c +3 -1
+		}
 		/* TODO: Check specific error and bomb out unless ENOBUFS? */
 		err = sock->ops->sendmsg(NULL, sock, &msg, len);
 		if (unlikely(err < 0)) {
-			vhost_discard_vq_desc(vq, 1);
+			if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+				vhost_discard_vq_desc(vq, 1);

How are errors handled with zerocopy?

Quoted 27 lines
drivers/vhost/net.c +2 -1
 			tx_poll_start(net, sock);
 			break;
 		}
 		if (err != len)
 			pr_debug("Truncated TX packet: "
 				 " len %d != %zd\n", err, len);
-		vhost_add_used_and_signal(&net->dev, vq, head, 0);
+		if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+			vhost_add_used_and_signal(&net->dev, vq, head, 0);
 		total_len += len;
 		if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
 			vhost_poll_queue(&vq->poll);
drivers/vhost/vhost.c +3 -0
index 2ab2912..ce799d6 100644
@@ -174,6 +174,9 @@ static void vhost_vq_reset(struct vhost_dev *dev,
 	vq->call_ctx = NULL;
 	vq->call = NULL;
 	vq->log_ctx = NULL;
+	vq->upend_idx = 0;
+	vq->done_idx = 0;
+	atomic_set(&vq->refcnt, 0);
 }
 
 static int vhost_worker(void *data)

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct vhost_dev *dev) · line 237

Quoted 6 lines
drivers/vhost/vhost.c +1 -1
@@ -230,7 +233,7 @@ static long vhost_dev_alloc_iovecs(struct vhost_dev *dev)
 					       UIO_MAXIOV, GFP_KERNEL);
 		dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log * UIO_MAXIOV,
 					  GFP_KERNEL);
-		dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads *
+		dev->vqs[i].heads = kzalloc(sizeof *dev->vqs[i].heads *
 					    UIO_MAXIOV, GFP_KERNEL);

Which fields need to be initialized actually?

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 393

Quoted 9 lines
drivers/vhost/vhost.c +3 -0
 		if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
@@ -385,6 +388,38 @@ long vhost_dev_reset_owner(struct vhost_dev *dev)
 	return 0;
 }
 
+/* 
+	comments
+*/

Hmm.

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 399

Quoted 6 lines
drivers/vhost/vhost.c +6 -0
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool shutdown)
+{
+	int i, j = 0;
+
+	i = vq->done_idx;
+	while (i != vq->upend_idx) {

A for loop might be clearer.

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 400

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+		if ((vq->heads[i].len == VHOST_DMA_DONE_LEN) || shutdown) {

On shutdown, we signal all buffers used to the guest? Why?

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 401

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+			/* reset len = 0 */

comment not very helpful.
Could you explain what this does instead?
Or better use some constant instead of 0 ...

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 406

Quoted 5 lines
drivers/vhost/vhost.c +5 -0
+			vq->heads[i].len = 0;
+			i = (i + 1) % UIO_MAXIOV;
+			++j;
+		} else
+			break;

Hmm so if the 1st entry does not complete, you do not signal anything?

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 407

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+	}

Looking at this loop, done idx is the consumer and used idx is the producer, right?

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 409

Quoted 2 lines
drivers/vhost/vhost.c +2 -0
+	if (j) {
+		/* comments */

Yes?

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 419

Quoted 10 lines
drivers/vhost/vhost.c +10 -0
+		if (i > vq->done_idx)
+			vhost_add_used_n(vq, &vq->heads[vq->done_idx], j);
+		else {
+			vhost_add_used_n(vq, &vq->heads[vq->done_idx],
+					 UIO_MAXIOV - vq->done_idx);
+			vhost_add_used_n(vq, vq->heads, i);
+		}
+		vq->done_idx = i;
+		vhost_signal(vq->dev, vq);
+		atomic_sub(j, &vq->refcnt);

Code will likely be simpler if you call vhost_add_used once for each head in the first loop. Possibly add_used_signal might be a good idea too.

Quoted 7 lines
drivers/vhost/vhost.c +3 -0
+	}
+}
+
 /* Caller should have device mutex */
 void vhost_dev_cleanup(struct vhost_dev *dev)
 {

drivers/vhost/vhost.c void vhost_dev_cleanup(struct vhost_dev *dev) · line 437

Quoted 8 lines
drivers/vhost/vhost.c +5 -0
@@ -395,6 +430,11 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
 			vhost_poll_stop(&dev->vqs[i].poll);
 			vhost_poll_flush(&dev->vqs[i].poll);
 		}
+		/* wait for all lower device DMAs done, then notify guest */
+		if (atomic_read(&dev->vqs[i].refcnt)) {
+			msleep(1000);
+			vhost_zerocopy_signal_used(&dev->vqs[i], true);
+		}

This needs to be fixed somehow. Use a completion object and wait on it?

Quoted 15 lines
drivers/vhost/vhost.c +4 -0
 		if (dev->vqs[i].error_ctx)
 			eventfd_ctx_put(dev->vqs[i].error_ctx);
 		if (dev->vqs[i].error)
@@ -603,6 +643,10 @@ static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
 
 	mutex_lock(&vq->mutex);
 
+	/* force all lower device DMAs done */
+	if (atomic_read(&vq->refcnt)) 
+		vhost_zerocopy_signal_used(vq, true);
+
 	switch (ioctl) {
 	case VHOST_SET_VRING_NUM:
 		/* Resizing ring with an active backend?

drivers/vhost/vhost.c void vhost_disable_notify(struct vhost_virtqueue *vq) · line 1469

Quoted 10 lines
drivers/vhost/vhost.c +7 -0
@@ -1416,3 +1460,12 @@ void vhost_disable_notify(struct vhost_virtqueue *vq)
 		vq_err(vq, "Failed to enable notification at %p: %d\n",
 		       &vq->used->flags, r);
 }
+
+void vhost_zerocopy_callback(struct sk_buff *skb)
+{
+	int idx = skb_shinfo(skb)->ubuf.desc;
+	struct vhost_virtqueue *vq = skb_shinfo(skb)->ubuf.arg;
+
+	/* set len = 1 to mark this desc buffers done DMA */

this comment can now go.

Quoted 18 lines
drivers/vhost/vhost.c +2 -0
+	vq->heads[idx].len = VHOST_DMA_DONE_LEN;
+}
drivers/vhost/vhost.h +4 -0
index b3363ae..8e3ecc7 100644
@@ -13,6 +13,10 @@
 #include <linux/virtio_ring.h>
 #include <asm/atomic.h>
 
+/* This is for zerocopy, used buffer len is set to 1 when lower device DMA
+ * done */
+#define VHOST_DMA_DONE_LEN	1
+
 struct vhost_device;
 
 struct vhost_work;

drivers/vhost/vhost.h struct vhost_virtqueue { · line 116

Quoted 5 lines
drivers/vhost/vhost.h +2 -0
@@ -108,6 +112,12 @@ struct vhost_virtqueue {
 	/* Log write descriptors */
 	void __user *log_base;
 	struct vhost_log *log;
+	/* vhost zerocopy support */
+	atomic_t refcnt; /* num of outstanding zerocopy DMAs */

future enhancement idea: this is used apparently under vq lock so no need for an atomic?

drivers/vhost/vhost.h struct vhost_virtqueue { · line 117

Quoted 1 line
drivers/vhost/vhost.h +1 -0
+	/* copy of avail idx to monitor outstanding DMA zerocopy buffers */

looking at code upend_idx seems to be calculated independently of guest avail idx - could you clarify pls?

drivers/vhost/vhost.h struct vhost_virtqueue { · line 119

Quoted 2 lines
drivers/vhost/vhost.h +2 -0
+	int upend_idx;
+	/* copy of used idx to monintor DMA done zerocopy buffers */

monitor

drivers/vhost/vhost.h struct vhost_virtqueue { · line 120

Quoted 1 line
drivers/vhost/vhost.h +1 -0
+	int done_idx;

I think in reality these are just producer and consumer in the head structure which for zero copy is used

Quoted 13 lines
drivers/vhost/vhost.h +2 -0
 };
 
 struct vhost_dev {
@@ -154,6 +164,8 @@ bool vhost_enable_notify(struct vhost_virtqueue *);
 
 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
 		    unsigned int log_num, u64 len);
+void vhost_zerocopy_callback(struct sk_buff *skb);
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool shutdown);
 
 #define vq_err(vq, fmt, ...) do {                                  \
 		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
author
#16 lore raw
Quoted 7 lines · Michael S. Tsirkin
Quoted 5 lines · Shirley Ma

Resubmit the patch with most update. This patch passed some live-migration test against RHEL6.2. I will run more stress test w/i live migration.

Quoted 7 lines

Cool. cleanup path needs a fix - are you use you can not use kobj or some other existing refcounting?

Is perf regressiion caused by tx ring overrun gone now?

I added some comments about how we might be aqble to complete requests out of order but it's not a must.

Oops, I used the old version vhost patch instead of most update one.

I will integrate your comments below and submit V6 patch instead.

thanks
Shirley

author
#17 lore raw
In reply to Michael S. Tsirkin
Quoted 9 lines · Michael S. Tsirkin
Quoted 5 lines · Shirley Ma

Resubmit the patch with most update. This patch passed some live-migration test against RHEL6.2. I will run more stress test w/i live migration.

Cool. cleanup path needs a fix - are you use you can not use kobj or some other existing refcounting?

I will look at this.

Quoted 1 line

Is perf regressiion caused by tx ring overrun gone now?

Nope.

Quoted 2 lines

I added some comments about how we might be aqble to complete requests out of order but it's not a must.

Ok.

Quoted 65 lines
Quoted 4 lines

---

 drivers/vhost/net.c   |   37 +++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.c |   55

++++++++++++++++++++++++++++++++++++++++++++++++-

Quoted 26 lines
 drivers/vhost/vhost.h |   12 ++++++++++
 3 files changed, 101 insertions(+), 3 deletions(-)
drivers/vhost/net.c +4 -0
index 2f7c76a..6bd6e28 100644
@@ -32,6 +32,9 @@
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_NET_WEIGHT 0x80000
 
+/* MAX number of TX used buffers for outstanding zerocopy */
+#define VHOST_MAX_ZEROCOPY_PEND 128 
+
 enum {
      VHOST_NET_VQ_RX = 0,
      VHOST_NET_VQ_TX = 1,
@@ -129,6 +132,7 @@ static void handle_tx(struct vhost_net *net)
      int err, wmem;
      size_t hdr_size;
      struct socket *sock;
+     struct skb_ubuf_info pend;
 
      /* TODO: check that we are running from vhost_worker? */
      sock = rcu_dereference_check(vq->private_data, 1);

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 159

Quoted 5 lines
drivers/vhost/net.c +2 -0
@@ -151,6 +155,10 @@ static void handle_tx(struct vhost_net *net)
      hdr_size = vq->vhost_hlen;
 
      for (;;) {
+             /* Release DMAs done buffers first */
+             if (atomic_read(&vq->refcnt) >

VHOST_MAX_ZEROCOPY_PEND)

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 174

Quoted 7 lines
drivers/vhost/net.c +2 -0
+                     vhost_zerocopy_signal_used(vq, false);
+
              head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
                                       ARRAY_SIZE(vq->iov),
                                       &out, &in,
@@ -166,6 +174,13 @@ static void handle_tx(struct vhost_net *net)
                              set_bit(SOCK_ASYNC_NOSPACE,

&sock->flags);

drivers/vhost/net.c static void handle_tx(struct vhost_net *net)

Quoted 5 lines
drivers/vhost/net.c +3 -0
                              break;
                      }
+                     /* If more outstanding DMAs, queue the work */
+                     if (sock_flag(sock->sk, SOCK_ZEROCOPY) &&
+                         (atomic_read(&vq->refcnt) >

VHOST_MAX_ZEROCOPY_PEND)) {

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 181

Quoted 2 lines
drivers/vhost/net.c +2 -0
+                             tx_poll_start(net, sock);
+                             set_bit(SOCK_ASYNC_NOSPACE,

&sock->flags);

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 206

Quoted 10 lines
drivers/vhost/net.c +3 -0
+                             break;
+                     }
                      if (unlikely(vhost_enable_notify(vq))) {
                              vhost_disable_notify(vq);
                              continue;
@@ -188,17 +203,35 @@ static void handle_tx(struct vhost_net *net)
                             iov_length(vq->hdr, s), hdr_size);
                      break;
              }
+             /* use msg_control to pass vhost zerocopy ubuf info to

skb */

Quoted 5 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 209

Quoted 3 lines
drivers/vhost/net.c +3 -0
+             if (sock_flag(sock->sk, SOCK_ZEROCOPY)) {
+                     vq->heads[vq->upend_idx].id = head;
+                     if (len <= 128)

I thought we have a constant for that?

Yes, fixed it already. I might change it to PAGE_SIZE for now since the small message sizes regression issue.

Quoted 13 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 210

Quoted 1 line
drivers/vhost/net.c +1 -0
+                             vq->heads[vq->upend_idx].len =

VHOST_DMA_DONE_LEN;

drivers/vhost/net.c static void handle_tx(struct vhost_net *net)

Quoted 3 lines
drivers/vhost/net.c +3 -0
+                     else {
+                             vq->heads[vq->upend_idx].len = len;
+                             pend.callback =

vhost_zerocopy_callback;

drivers/vhost/net.c static void handle_tx(struct vhost_net *net)

Quoted 7 lines
drivers/vhost/net.c +7 -0
+                             pend.arg = vq;
+                             pend.desc = vq->upend_idx;
+                             msg.msg_control = &pend;
+                             msg.msg_controllen = sizeof(pend);
+                     }
+                     atomic_inc(&vq->refcnt);
+                     vq->upend_idx = (vq->upend_idx + 1) %
Quoted 4 lines

UIO_MAXIOV;

Ok, so we deal with a cyclic ring apparently? What guarantees we don't overrun it?

VHOST_MAX_PEND (which is 128) should prevent it from overrun normally. In the case of none DMAs can be done, the maximum entries are the ring size, which is much smaller or equal to UIO_MAXIOV for current implementation.

Maybe I should add some condition check if it is overrun then exits?

Quoted 11 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 222

Quoted 3 lines
drivers/vhost/net.c +1 -0
+             }
              /* TODO: Check specific error and bomb out unless

ENOBUFS? */

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 226

Quoted 5 lines
drivers/vhost/net.c +2 -1
              err = sock->ops->sendmsg(NULL, sock, &msg, len);
              if (unlikely(err < 0)) {
-                     vhost_discard_vq_desc(vq, 1);
+                     if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+                             vhost_discard_vq_desc(vq, 1);

How are errors handled with zerocopy?

I thought about it before: if error after macvtap skb is allocated, then that skb has a callback which will set DMA done for add_used, if the error before macvtap skb is allocated, then it can reverse as copy case. To avoid the complexity check, I decided to not handle it.

Quoted 35 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net *net)

Quoted 10 lines
drivers/vhost/net.c +2 -1
                      tx_poll_start(net, sock);
                      break;
              }
              if (err != len)
                      pr_debug("Truncated TX packet: "
                               " len %d != %zd\n", err, len);
-             vhost_add_used_and_signal(&net->dev, vq, head, 0);
+             if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+                     vhost_add_used_and_signal(&net->dev, vq, head,

0);

drivers/vhost/vhost.c static void vhost_vq_reset(struct vhost_dev

Quoted 8 lines
drivers/vhost/net.c +0 -0
              total_len += len;
              if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
                      vhost_poll_queue(&vq->poll);
drivers/vhost/vhost.c +0 -0
index 2ab2912..ce799d6 100644
@@ -174,6 +174,9 @@ static void vhost_vq_reset(struct vhost_dev

*dev,

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct

Quoted 10 lines
drivers/vhost/vhost.c +3 -0
      vq->call_ctx = NULL;
      vq->call = NULL;
      vq->log_ctx = NULL;
+     vq->upend_idx = 0;
+     vq->done_idx = 0;
+     atomic_set(&vq->refcnt, 0);
 }
 
 static int vhost_worker(void *data)
@@ -230,7 +233,7 @@ static long vhost_dev_alloc_iovecs(struct

vhost_dev *dev)

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct · line 233

Quoted 1 line
drivers/vhost/vhost.c +0 -0
                                             UIO_MAXIOV,

GFP_KERNEL);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct · line 234

Quoted 1 line
drivers/vhost/vhost.c +0 -0
              dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log *

UIO_MAXIOV,

Quoted 8 lines

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct · line 233

Quoted 2 lines
drivers/vhost/vhost.c +0 -1
                                        GFP_KERNEL);
-             dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads

*

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct · line 236

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+             dev->vqs[i].heads = kzalloc(sizeof *dev->vqs[i].heads

*

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct · line 237

Quoted 1 line
drivers/vhost/vhost.c +0 -0
                                          UIO_MAXIOV, GFP_KERNEL);

Which fields need to be initialized actually?

Nope, already fixed it with kmalloc.

Quoted 4 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev

Quoted 3 lines
drivers/vhost/vhost.c +0 -0
              if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
@@ -385,6 +388,38 @@ long vhost_dev_reset_owner(struct vhost_dev

*dev)

Quoted 8 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 393

Quoted 6 lines
drivers/vhost/vhost.c +3 -0
      return 0;
 }
 
+/* 
+     comments
+*/

Hmm.

Fixed it in previous patch already.

Quoted 9 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev · line 394

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool

shutdown)

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 399

Quoted 5 lines
drivers/vhost/vhost.c +5 -0
+{
+     int i, j = 0;
+
+     i = vq->done_idx;
+     while (i != vq->upend_idx) {

A for loop might be clearer.

Ok.

Quoted 5 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev · line 400

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+             if ((vq->heads[i].len == VHOST_DMA_DONE_LEN) ||

shutdown) {

On shutdown, we signal all buffers used to the guest? Why?

We signal all outstand DMAs in the case of driver has some DMA issue to prevent us from shutting down. I am afraid vhost cleanup could wait forever.

Quoted 5 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 401

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+                     /* reset len = 0 */

comment not very helpful.
Could you explain what this does instead?
Or better use some constant instead of 0 ...

Fixed it already.

Quoted 7 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 406

Quoted 5 lines
drivers/vhost/vhost.c +5 -0
+                     vq->heads[i].len = 0;
+                     i = (i + 1) % UIO_MAXIOV;
+                     ++j;
+             } else
+                     break;

Hmm so if the 1st entry does not complete, you do not signal anything?

No used buffers to guest, should we signal still?

Quoted 4 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev · line 407

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+     }

Looking at this loop, done idx is the consumer and used idx is the producer, right?

Yes.

Quoted 17 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 409

Quoted 2 lines
drivers/vhost/vhost.c +2 -0
+     if (j) {
+             /* comments */

Yes?

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 413

Quoted 2 lines
drivers/vhost/vhost.c +2 -0
+             if (i > vq->done_idx)
+                     vhost_add_used_n(vq, &vq->heads[vq->done_idx],

j);

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 419

Quoted 8 lines
drivers/vhost/vhost.c +8 -0
+             else {
+                     vhost_add_used_n(vq, &vq->heads[vq->done_idx],
+                                      UIO_MAXIOV - vq->done_idx);
+                     vhost_add_used_n(vq, vq->heads, i);
+             }
+             vq->done_idx = i;
+             vhost_signal(vq->dev, vq);
+             atomic_sub(j, &vq->refcnt);
Quoted 3 lines

Code will likely be simpler if you call vhost_add_used once for each head in the first loop. Possibly add_used_signal might be a good idea too.

Ok.

Quoted 12 lines

drivers/vhost/vhost.c void vhost_dev_cleanup(struct vhost_dev *dev) · line 433

Quoted 11 lines
drivers/vhost/vhost.c +4 -0
+     }
+}
+
 /* Caller should have device mutex */
 void vhost_dev_cleanup(struct vhost_dev *dev)
 {
@@ -395,6 +430,11 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
                      vhost_poll_stop(&dev->vqs[i].poll);
                      vhost_poll_flush(&dev->vqs[i].poll);
              }
+             /* wait for all lower device DMAs done, then notify

guest */

Quoted 8 lines

drivers/vhost/vhost.c void vhost_dev_cleanup(struct vhost_dev *dev)

Quoted 3 lines
drivers/vhost/vhost.c +3 -0
+             if (atomic_read(&dev->vqs[i].refcnt)) {
+                     msleep(1000);
+                     vhost_zerocopy_signal_used(&dev->vqs[i],

true);

drivers/vhost/vhost.c void vhost_dev_cleanup(struct vhost_dev *dev) · line 437

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+             }

This needs to be fixed somehow. Use a completion object and wait on it?

Worried about what if the driver has some DMAs issue, which would prevent vhost from shutting down.

Quoted 19 lines

drivers/vhost/vhost.c static long vhost_set_vring(struct vhost_dev

Quoted 4 lines
drivers/vhost/vhost.c +0 -0
              if (dev->vqs[i].error_ctx)
                      eventfd_ctx_put(dev->vqs[i].error_ctx);
              if (dev->vqs[i].error)
@@ -603,6 +643,10 @@ static long vhost_set_vring(struct vhost_dev

*d, int ioctl, void __user *argp)

drivers/vhost/vhost.c void vhost_disable_notify(struct

Quoted 11 lines
drivers/vhost/vhost.c +4 -0
      mutex_lock(&vq->mutex);
 
+     /* force all lower device DMAs done */
+     if (atomic_read(&vq->refcnt)) 
+             vhost_zerocopy_signal_used(vq, true);
+
      switch (ioctl) {
      case VHOST_SET_VRING_NUM:
              /* Resizing ring with an active backend?
@@ -1416,3 +1460,12 @@ void vhost_disable_notify(struct

vhost_virtqueue *vq)

drivers/vhost/vhost.c void vhost_disable_notify(struct · line 1460

Quoted 1 line
drivers/vhost/vhost.c +0 -0
              vq_err(vq, "Failed to enable notification at %p: %d

\n",

Quoted 11 lines

drivers/vhost/vhost.c void vhost_disable_notify(struct vhost_virtqueue *vq) · line 1469

Quoted 9 lines
drivers/vhost/vhost.c +7 -0
                     &vq->used->flags, r);
 }
+
+void vhost_zerocopy_callback(struct sk_buff *skb)
+{
+     int idx = skb_shinfo(skb)->ubuf.desc;
+     struct vhost_virtqueue *vq = skb_shinfo(skb)->ubuf.arg;
+
+     /* set len = 1 to mark this desc buffers done DMA */

this comment can now go.

Yes, it's gone already.

Quoted 17 lines

drivers/vhost/vhost.h line 16

Quoted 11 lines
drivers/vhost/vhost.c +2 -0
+     vq->heads[idx].len = VHOST_DMA_DONE_LEN;
+}
drivers/vhost/vhost.h +1 -0
index b3363ae..8e3ecc7 100644
@@ -13,6 +13,10 @@
 #include <linux/virtio_ring.h>
 #include <asm/atomic.h>
 
+/* This is for zerocopy, used buffer len is set to 1 when lower

device DMA

Quoted 5 lines
drivers/vhost/vhost.h +3 -0
+ * done */
+#define VHOST_DMA_DONE_LEN   1
+
 struct vhost_device;
 
Quoted 10 lines

drivers/vhost/vhost.h struct vhost_virtqueue { · line 116

Quoted 7 lines
drivers/vhost/vhost.h +2 -0
 struct vhost_work;
@@ -108,6 +112,12 @@ struct vhost_virtqueue {
      /* Log write descriptors */
      void __user *log_base;
      struct vhost_log *log;
+     /* vhost zerocopy support */
+     atomic_t refcnt; /* num of outstanding zerocopy DMAs */

future enhancement idea: this is used apparently under vq lock so no need for an atomic?

It is also used in skb vhost zerocopy callback.

Quoted 5 lines

drivers/vhost/vhost.h struct vhost_virtqueue { · line 117

Quoted 1 line
drivers/vhost/vhost.h +1 -0
+     /* copy of avail idx to monitor outstanding DMA zerocopy

buffers */

looking at code upend_idx seems to be calculated independently of guest avail idx - could you clarify pls?

Yes, you are right. Should change it to: upend_idx is used to track vring ids for outstanding zero-copy DMA buffers?

Quoted 4 lines

drivers/vhost/vhost.h struct vhost_virtqueue { · line 119

Quoted 2 lines
drivers/vhost/vhost.h +2 -0
+     int upend_idx;
+     /* copy of used idx to monintor DMA done zerocopy buffers */

monitor

Ok.

Quoted 5 lines

drivers/vhost/vhost.h struct vhost_virtqueue { · line 120

Quoted 1 line
drivers/vhost/vhost.h +1 -0
+     int done_idx;

I think in reality these are just producer and consumer in the head structure which for zero copy is used

Yes.

Quoted 18 lines

drivers/vhost/vhost.h bool vhost_enable_notify(struct vhost_virtqueue

Quoted 5 lines
drivers/vhost/vhost.h +0 -0
 };
 
 struct vhost_dev {
@@ -154,6 +164,8 @@ bool vhost_enable_notify(struct vhost_virtqueue

*);

drivers/vhost/vhost.h bool vhost_enable_notify(struct vhost_virtqueue · line 164

Quoted 2 lines
drivers/vhost/vhost.h +0 -0
 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log

*log,

drivers/vhost/vhost.h bool vhost_enable_notify(struct vhost_virtqueue *);

Quoted 3 lines
drivers/vhost/vhost.h +2 -0
                  unsigned int log_num, u64 len);
+void vhost_zerocopy_callback(struct sk_buff *skb);
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool

shutdown);

drivers/vhost/vhost.h bool vhost_enable_notify(struct vhost_virtqueue · line 168

Quoted 2 lines
drivers/vhost/vhost.h +0 -0
 #define vq_err(vq, fmt, ...) do {

\

Quoted 2 lines
drivers/vhost/vhost.h +0 -0
              pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
 
author
#18 lore raw

Hello Michael,

Here is the update the patch based on all of your review comments except the completion/wait for cleanup since I am worried about outstanding DMAs would prevent vhost from shutting down. I am sending out this for your review, and test it out later.

For error handling, I update macvtap.c so we can discard the desc even in zero-copy case.

---
 drivers/vhost/net.c   |   42 +++++++++++++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/vhost/vhost.h |   13 +++++++++++++
 3 files changed, 103 insertions(+), 1 deletions(-)
drivers/vhost/net.c +41 -1
index 2f7c76a..e87a1f8 100644
@@ -32,6 +32,10 @@
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_NET_WEIGHT 0x80000
 
+/* MAX number of TX used buffers for outstanding zerocopy */
+#define VHOST_MAX_PEND 128
+#define VHOST_GOODCOPY_LEN PAGE_SIZE
+
 enum {
 	VHOST_NET_VQ_RX = 0,
 	VHOST_NET_VQ_TX = 1,
@@ -129,6 +133,7 @@ static void handle_tx(struct vhost_net *net)
 	int err, wmem;
 	size_t hdr_size;
 	struct socket *sock;
+	struct skb_ubuf_info pend;
 
 	/* TODO: check that we are running from vhost_worker? */
 	sock = rcu_dereference_check(vq->private_data, 1);
@@ -151,6 +156,10 @@ static void handle_tx(struct vhost_net *net)
 	hdr_size = vq->vhost_hlen;
 
 	for (;;) {
+		/* Release DMAs done buffers first */
+		if (atomic_read(&vq->refcnt) > VHOST_MAX_PEND)
+			vhost_zerocopy_signal_used(vq, false);
+
 		head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
 					 ARRAY_SIZE(vq->iov),
 					 &out, &in,
@@ -166,6 +175,12 @@ static void handle_tx(struct vhost_net *net)
 				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
 				break;
 			}
+			/* If more outstanding DMAs, queue the work */
+			if (atomic_read(&vq->refcnt) > VHOST_MAX_PEND) {
+				tx_poll_start(net, sock);
+				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
+				break;
+			}
 			if (unlikely(vhost_enable_notify(vq))) {
 				vhost_disable_notify(vq);
 				continue;
@@ -188,6 +203,30 @@ static void handle_tx(struct vhost_net *net)
 			       iov_length(vq->hdr, s), hdr_size);
 			break;
 		}
+		/* use msg_control to pass vhost zerocopy ubuf info to skb */
+		if (sock_flag(sock->sk, SOCK_ZEROCOPY)) {
+			vq->heads[vq->upend_idx].id = head;
+			if (len < VHOST_GOODCOPY_LEN)
+				/* copy don't need to wait for DMA done */
+				vq->heads[vq->upend_idx].len =
+							VHOST_DMA_DONE_LEN;
+			else {
+				vq->heads[vq->upend_idx].len = len;
+				pend.callback = vhost_zerocopy_callback;
+				pend.arg = vq;
+				pend.desc = vq->upend_idx;
+				msg.msg_control = &pend;
+				msg.msg_controllen = sizeof(pend);
+			}
+			atomic_inc(&vq->refcnt);
+			vq->upend_idx = (vq->upend_idx + 1) % UIO_MAXIOV;
+			/* if upend_idx is full, then wait for free more */
+			if (vq->upend_idx == vq->done_idx) {
+				tx_poll_start(net, sock);
+				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
+				break;
+			}
+		}
 		/* TODO: Check specific error and bomb out unless ENOBUFS? */
 		err = sock->ops->sendmsg(NULL, sock, &msg, len);
 		if (unlikely(err < 0)) {
@@ -198,7 +237,8 @@ static void handle_tx(struct vhost_net *net)
 		if (err != len)
 			pr_debug("Truncated TX packet: "
 				 " len %d != %zd\n", err, len);
-		vhost_add_used_and_signal(&net->dev, vq, head, 0);
+		if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+			vhost_add_used_and_signal(&net->dev, vq, head, 0);
 		total_len += len;
 		if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
 			vhost_poll_queue(&vq->poll);
drivers/vhost/vhost.c +49 -0

80 lines not on this page · raw message

drivers/vhost/vhost.h +13 -0
index b3363ae..d0e7ac6 100644
@@ -13,6 +13,11 @@
 #include <linux/virtio_ring.h>
 #include <asm/atomic.h>
 
+/* This is for zerocopy, used buffer len is set to 1 when lower device DMA
+ * done */
+#define VHOST_DMA_DONE_LEN	1
+#define VHOST_DMA_CLEAR_LEN	0
+
 struct vhost_device;
 
 struct vhost_work;
@@ -108,6 +113,12 @@ struct vhost_virtqueue {
 	/* Log write descriptors */
 	void __user *log_base;
 	struct vhost_log *log;
+	/* vhost zerocopy support */
+	atomic_t refcnt; /* num of outstanding zerocopy DMAs */
+	/* last used idx for outstanding DMA zerocopy buffers */
+	int upend_idx;
+	/* first used idx for DMA done zerocopy buffers */
+	int done_idx;
 };
 
 struct vhost_dev {
@@ -154,6 +165,8 @@ bool vhost_enable_notify(struct vhost_virtqueue *);
 
 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
 		    unsigned int log_num, u64 len);
+void vhost_zerocopy_callback(struct sk_buff *skb);
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool shutdown);
 
 #define vq_err(vq, fmt, ...) do {                                  \
 		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
#19 lore raw
Quoted 5 lines · Shirley Ma

Hello Michael,

Here is the update the patch based on all of your review comments except the completion/wait for cleanup since I am worried about outstanding DMAs would prevent vhost from shutting down.

Don't see what you are worried about. Doing this in a timely fashion is just one of the things driver commits to when it published the zcopy flag. Otherwise guest networking will get stuck as entries in the tx ring don't free up, which is also a problem. Let's just add a comment documenting this.

Quoted 1 line

I am sending out this for your review, and test it out later.

So let's use a completion to cleanly flush outstanding requests. Any chance kref can be reused? It's not a must.

One other piece that is currently missing is flushing requests on memory table update. Maybe we can stick some info in high bits of the desc field for that?

Quoted 174 lines

For error handling, I update macvtap.c so we can discard the desc even in zero-copy case.

---
 drivers/vhost/net.c   |   42 +++++++++++++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.c |   49 +++++++++++++++++++++++++++++++++++++++++++++++++
 drivers/vhost/vhost.h |   13 +++++++++++++
 3 files changed, 103 insertions(+), 1 deletions(-)
drivers/vhost/net.c +41 -1
index 2f7c76a..e87a1f8 100644
@@ -32,6 +32,10 @@
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_NET_WEIGHT 0x80000
 
+/* MAX number of TX used buffers for outstanding zerocopy */
+#define VHOST_MAX_PEND 128
+#define VHOST_GOODCOPY_LEN PAGE_SIZE
+
 enum {
 	VHOST_NET_VQ_RX = 0,
 	VHOST_NET_VQ_TX = 1,
@@ -129,6 +133,7 @@ static void handle_tx(struct vhost_net *net)
 	int err, wmem;
 	size_t hdr_size;
 	struct socket *sock;
+	struct skb_ubuf_info pend;
 
 	/* TODO: check that we are running from vhost_worker? */
 	sock = rcu_dereference_check(vq->private_data, 1);
@@ -151,6 +156,10 @@ static void handle_tx(struct vhost_net *net)
 	hdr_size = vq->vhost_hlen;
 
 	for (;;) {
+		/* Release DMAs done buffers first */
+		if (atomic_read(&vq->refcnt) > VHOST_MAX_PEND)
+			vhost_zerocopy_signal_used(vq, false);
+
 		head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
 					 ARRAY_SIZE(vq->iov),
 					 &out, &in,
@@ -166,6 +175,12 @@ static void handle_tx(struct vhost_net *net)
 				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
 				break;
 			}
+			/* If more outstanding DMAs, queue the work */
+			if (atomic_read(&vq->refcnt) > VHOST_MAX_PEND) {
+				tx_poll_start(net, sock);
+				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
+				break;
+			}
 			if (unlikely(vhost_enable_notify(vq))) {
 				vhost_disable_notify(vq);
 				continue;
@@ -188,6 +203,30 @@ static void handle_tx(struct vhost_net *net)
 			       iov_length(vq->hdr, s), hdr_size);
 			break;
 		}
+		/* use msg_control to pass vhost zerocopy ubuf info to skb */
+		if (sock_flag(sock->sk, SOCK_ZEROCOPY)) {
+			vq->heads[vq->upend_idx].id = head;
+			if (len < VHOST_GOODCOPY_LEN)
+				/* copy don't need to wait for DMA done */
+				vq->heads[vq->upend_idx].len =
+							VHOST_DMA_DONE_LEN;
+			else {
+				vq->heads[vq->upend_idx].len = len;
+				pend.callback = vhost_zerocopy_callback;
+				pend.arg = vq;
+				pend.desc = vq->upend_idx;
+				msg.msg_control = &pend;
+				msg.msg_controllen = sizeof(pend);
+			}
+			atomic_inc(&vq->refcnt);
+			vq->upend_idx = (vq->upend_idx + 1) % UIO_MAXIOV;
+			/* if upend_idx is full, then wait for free more */
+			if (vq->upend_idx == vq->done_idx) {
+				tx_poll_start(net, sock);
+				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
+				break;
+			}
+		}
 		/* TODO: Check specific error and bomb out unless ENOBUFS? */
 		err = sock->ops->sendmsg(NULL, sock, &msg, len);
 		if (unlikely(err < 0)) {
@@ -198,7 +237,8 @@ static void handle_tx(struct vhost_net *net)
 		if (err != len)
 			pr_debug("Truncated TX packet: "
 				 " len %d != %zd\n", err, len);
-		vhost_add_used_and_signal(&net->dev, vq, head, 0);
+		if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+			vhost_add_used_and_signal(&net->dev, vq, head, 0);
 		total_len += len;
 		if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
 			vhost_poll_queue(&vq->poll);
drivers/vhost/vhost.c +40 -0
index 2ab2912..f4c2730 100644
@@ -174,6 +174,9 @@ static void vhost_vq_reset(struct vhost_dev *dev,
 	vq->call_ctx = NULL;
 	vq->call = NULL;
 	vq->log_ctx = NULL;
+	vq->upend_idx = 0;
+	vq->done_idx = 0;
+	atomic_set(&vq->refcnt, 0);
 }
 
 static int vhost_worker(void *data)
@@ -385,16 +388,49 @@ long vhost_dev_reset_owner(struct vhost_dev *dev)
 	return 0;
 }
 
+/* In case of DMA done not in order in lower device driver for some reason.
+ * upend_idx is used to track end of used idx, done_idx is used to track head
+ * of used idx. Once lower device DMA done contiguously, we will signal KVM
+ * guest used idx.
+ */
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool shutdown)
+{
+	int i, j = 0;
+
+	for (i = vq->done_idx; i != vq->upend_idx; i = i++ % UIO_MAXIOV) {
+		if ((vq->heads[i].len == VHOST_DMA_DONE_LEN) || shutdown) {
+			vq->heads[i].len = VHOST_DMA_CLEAR_LEN;
+			vhost_add_used_and_signal(vq->dev, vq,
+						  vq->heads[i].id, 0);
+			++j;
+		} else
+			break;
+	}
+	if (j) {
+		vq->done_idx = i;
+		atomic_sub(j, &vq->refcnt);
+	}
+}
+
 /* Caller should have device mutex */
 void vhost_dev_cleanup(struct vhost_dev *dev)
 {
 	int i;
+	unsigned long begin = jiffies;
 
 	for (i = 0; i < dev->nvqs; ++i) {
 		if (dev->vqs[i].kick && dev->vqs[i].handle_kick) {
 			vhost_poll_stop(&dev->vqs[i].poll);
 			vhost_poll_flush(&dev->vqs[i].poll);
 		}
+		/* Wait for all lower device DMAs done, then notify virtio_net
+		 * or just notify it without waiting for all DMA done here ?
+		 * in case of DMAs never done for some reason */
+		if (atomic_read(&dev->vqs[i].refcnt)) {
+			/* how long should we wait ? */
+			msleep(1000);
+			vhost_zerocopy_signal_used(&dev->vqs[i], true);
+		}
 		if (dev->vqs[i].error_ctx)
 			eventfd_ctx_put(dev->vqs[i].error_ctx);
 		if (dev->vqs[i].error)
@@ -603,6 +639,10 @@ static long vhost_set_vring(struct vhost_dev *d, int ioctl, void __user *argp)
 
 	mutex_lock(&vq->mutex);
 
+	/* clean up lower device outstanding DMAs, before setting ring */
+	if (atomic_read(&vq->refcnt))
+		vhost_zerocopy_signal_used(vq, true);
+
 	switch (ioctl) {
 	case VHOST_SET_VRING_NUM:
 		/* Resizing ring with an active backend?

drivers/vhost/vhost.c void vhost_disable_notify(struct vhost_virtqueue *vq) · line 1463

Quoted 8 lines
drivers/vhost/vhost.c +5 -0
@@ -1416,3 +1456,12 @@ void vhost_disable_notify(struct vhost_virtqueue *vq)
 		vq_err(vq, "Failed to enable notification at %p: %d\n",
 		       &vq->used->flags, r);
 }
+
+void vhost_zerocopy_callback(struct sk_buff *skb)
+{
+	int idx = skb_shinfo(skb)->ubuf.desc;
+	struct vhost_virtqueue *vq = skb_shinfo(skb)->ubuf.arg;

Let's do some sanity checking of idx here in case
there's a driver bug.

Quoted 44 lines
drivers/vhost/vhost.c +4 -0
+
+	/* set len = 1 to mark this desc buffers done DMA */
+	vq->heads[idx].len = VHOST_DMA_DONE_LEN;
+}
drivers/vhost/vhost.h +13 -0
index b3363ae..d0e7ac6 100644
@@ -13,6 +13,11 @@
 #include <linux/virtio_ring.h>
 #include <asm/atomic.h>
 
+/* This is for zerocopy, used buffer len is set to 1 when lower device DMA
+ * done */
+#define VHOST_DMA_DONE_LEN	1
+#define VHOST_DMA_CLEAR_LEN	0
+
 struct vhost_device;
 
 struct vhost_work;
@@ -108,6 +113,12 @@ struct vhost_virtqueue {
 	/* Log write descriptors */
 	void __user *log_base;
 	struct vhost_log *log;
+	/* vhost zerocopy support */
+	atomic_t refcnt; /* num of outstanding zerocopy DMAs */
+	/* last used idx for outstanding DMA zerocopy buffers */
+	int upend_idx;
+	/* first used idx for DMA done zerocopy buffers */
+	int done_idx;
 };
 
 struct vhost_dev {
@@ -154,6 +165,8 @@ bool vhost_enable_notify(struct vhost_virtqueue *);
 
 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log *log,
 		    unsigned int log_num, u64 len);
+void vhost_zerocopy_callback(struct sk_buff *skb);
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool shutdown);
 
 #define vq_err(vq, fmt, ...) do {                                  \
 		pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
#20 lore raw
In reply to Shirley Ma
Quoted 111 lines · Shirley Ma
Quoted 9 lines · Michael S. Tsirkin
Quoted 5 lines · Shirley Ma

Resubmit the patch with most update. This patch passed some live-migration test against RHEL6.2. I will run more stress test w/i live migration.

Cool. cleanup path needs a fix - are you use you can not use kobj or some other existing refcounting?

I will look at this.

Quoted 1 line

Is perf regressiion caused by tx ring overrun gone now?

Nope.

Quoted 2 lines

I added some comments about how we might be aqble to complete requests out of order but it's not a must.

Ok.

Quoted 65 lines
Quoted 4 lines

---

 drivers/vhost/net.c   |   37 +++++++++++++++++++++++++++++++-
 drivers/vhost/vhost.c |   55

++++++++++++++++++++++++++++++++++++++++++++++++-

Quoted 26 lines
 drivers/vhost/vhost.h |   12 ++++++++++
 3 files changed, 101 insertions(+), 3 deletions(-)
drivers/vhost/net.c +4 -0
index 2f7c76a..6bd6e28 100644
@@ -32,6 +32,9 @@
  * Using this limit prevents one virtqueue from starving others. */
 #define VHOST_NET_WEIGHT 0x80000
 
+/* MAX number of TX used buffers for outstanding zerocopy */
+#define VHOST_MAX_ZEROCOPY_PEND 128 
+
 enum {
      VHOST_NET_VQ_RX = 0,
      VHOST_NET_VQ_TX = 1,
@@ -129,6 +132,7 @@ static void handle_tx(struct vhost_net *net)
      int err, wmem;
      size_t hdr_size;
      struct socket *sock;
+     struct skb_ubuf_info pend;
 
      /* TODO: check that we are running from vhost_worker? */
      sock = rcu_dereference_check(vq->private_data, 1);

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 159

Quoted 5 lines
drivers/vhost/net.c +2 -0
@@ -151,6 +155,10 @@ static void handle_tx(struct vhost_net *net)
      hdr_size = vq->vhost_hlen;
 
      for (;;) {
+             /* Release DMAs done buffers first */
+             if (atomic_read(&vq->refcnt) >

VHOST_MAX_ZEROCOPY_PEND)

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 174

Quoted 7 lines
drivers/vhost/net.c +2 -0
+                     vhost_zerocopy_signal_used(vq, false);
+
              head = vhost_get_vq_desc(&net->dev, vq, vq->iov,
                                       ARRAY_SIZE(vq->iov),
                                       &out, &in,
@@ -166,6 +174,13 @@ static void handle_tx(struct vhost_net *net)
                              set_bit(SOCK_ASYNC_NOSPACE,

&sock->flags);

drivers/vhost/net.c static void handle_tx(struct vhost_net *net)

Quoted 5 lines
drivers/vhost/net.c +3 -0
                              break;
                      }
+                     /* If more outstanding DMAs, queue the work */
+                     if (sock_flag(sock->sk, SOCK_ZEROCOPY) &&
+                         (atomic_read(&vq->refcnt) >

VHOST_MAX_ZEROCOPY_PEND)) {

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 181

Quoted 2 lines
drivers/vhost/net.c +2 -0
+                             tx_poll_start(net, sock);
+                             set_bit(SOCK_ASYNC_NOSPACE,

&sock->flags);

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 206

Quoted 10 lines
drivers/vhost/net.c +3 -0
+                             break;
+                     }
                      if (unlikely(vhost_enable_notify(vq))) {
                              vhost_disable_notify(vq);
                              continue;
@@ -188,17 +203,35 @@ static void handle_tx(struct vhost_net *net)
                             iov_length(vq->hdr, s), hdr_size);
                      break;
              }
+             /* use msg_control to pass vhost zerocopy ubuf info to

skb */

Quoted 5 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 209

Quoted 3 lines
drivers/vhost/net.c +3 -0
+             if (sock_flag(sock->sk, SOCK_ZEROCOPY)) {
+                     vq->heads[vq->upend_idx].id = head;
+                     if (len <= 128)

I thought we have a constant for that?

Yes, fixed it already. I might change it to PAGE_SIZE for now since the small message sizes regression issue.

Quoted 13 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 210

Quoted 1 line
drivers/vhost/net.c +1 -0
+                             vq->heads[vq->upend_idx].len =

VHOST_DMA_DONE_LEN;

drivers/vhost/net.c static void handle_tx(struct vhost_net *net)

Quoted 3 lines
drivers/vhost/net.c +3 -0
+                     else {
+                             vq->heads[vq->upend_idx].len = len;
+                             pend.callback =

vhost_zerocopy_callback;

drivers/vhost/net.c static void handle_tx(struct vhost_net *net)

Quoted 7 lines
drivers/vhost/net.c +7 -0
+                             pend.arg = vq;
+                             pend.desc = vq->upend_idx;
+                             msg.msg_control = &pend;
+                             msg.msg_controllen = sizeof(pend);
+                     }
+                     atomic_inc(&vq->refcnt);
+                     vq->upend_idx = (vq->upend_idx + 1) %
Quoted 4 lines

UIO_MAXIOV;

Ok, so we deal with a cyclic ring apparently? What guarantees we don't overrun it?

Quoted 6 lines

VHOST_MAX_PEND (which is 128) should prevent it from overrun normally. In the case of none DMAs can be done, the maximum entries are the ring size, which is much smaller or equal to UIO_MAXIOV for current implementation.

Maybe I should add some condition check if it is overrun then exits?

If this can't happen, maybe add a comment why. A check + WARN_ON can't hurt either.

Quoted 10 lines
Quoted 10 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net *net) · line 222

Quoted 3 lines
drivers/vhost/net.c +1 -0
+             }
              /* TODO: Check specific error and bomb out unless

ENOBUFS? */

Quoted 5 lines
drivers/vhost/net.c +2 -1
              err = sock->ops->sendmsg(NULL, sock, &msg, len);
              if (unlikely(err < 0)) {
-                     vhost_discard_vq_desc(vq, 1);
+                     if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+                             vhost_discard_vq_desc(vq, 1);
Quoted 6 lines
Quoted 1 line

How are errors handled with zerocopy?

I thought about it before: if error after macvtap skb is allocated, then that skb has a callback which will set DMA done for add_used, if the error before macvtap skb is allocated, then it can reverse as copy case. To avoid the complexity check, I decided to not handle it.

I think we need to handle it, errors do happen.
macvtap should either don't invoke callback on error or return success. Then it's simple.

Quoted 73 lines
Quoted 35 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net *net)

Quoted 10 lines
drivers/vhost/net.c +2 -1
                      tx_poll_start(net, sock);
                      break;
              }
              if (err != len)
                      pr_debug("Truncated TX packet: "
                               " len %d != %zd\n", err, len);
-             vhost_add_used_and_signal(&net->dev, vq, head, 0);
+             if (!sock_flag(sock->sk, SOCK_ZEROCOPY))
+                     vhost_add_used_and_signal(&net->dev, vq, head,

0);

drivers/vhost/vhost.c static void vhost_vq_reset(struct vhost_dev

Quoted 8 lines
drivers/vhost/net.c +0 -0
              total_len += len;
              if (unlikely(total_len >= VHOST_NET_WEIGHT)) {
                      vhost_poll_queue(&vq->poll);
drivers/vhost/vhost.c +0 -0
index 2ab2912..ce799d6 100644
@@ -174,6 +174,9 @@ static void vhost_vq_reset(struct vhost_dev

*dev,

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct

Quoted 10 lines
drivers/vhost/vhost.c +3 -0
      vq->call_ctx = NULL;
      vq->call = NULL;
      vq->log_ctx = NULL;
+     vq->upend_idx = 0;
+     vq->done_idx = 0;
+     atomic_set(&vq->refcnt, 0);
 }
 
 static int vhost_worker(void *data)
@@ -230,7 +233,7 @@ static long vhost_dev_alloc_iovecs(struct

vhost_dev *dev)

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct · line 233

Quoted 1 line
drivers/vhost/vhost.c +0 -0
                                             UIO_MAXIOV,

GFP_KERNEL);

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct · line 234

Quoted 1 line
drivers/vhost/vhost.c +0 -0
              dev->vqs[i].log = kmalloc(sizeof *dev->vqs[i].log *

UIO_MAXIOV,

Quoted 8 lines

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct · line 233

Quoted 2 lines
drivers/vhost/vhost.c +0 -1
                                        GFP_KERNEL);
-             dev->vqs[i].heads = kmalloc(sizeof *dev->vqs[i].heads

*

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct · line 236

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+             dev->vqs[i].heads = kzalloc(sizeof *dev->vqs[i].heads

*

drivers/vhost/vhost.c static long vhost_dev_alloc_iovecs(struct · line 237

Quoted 1 line
drivers/vhost/vhost.c +0 -0
                                          UIO_MAXIOV, GFP_KERNEL);

Which fields need to be initialized actually?

Nope, already fixed it with kmalloc.

Quoted 4 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev

Quoted 3 lines
drivers/vhost/vhost.c +0 -0
              if (!dev->vqs[i].indirect || !dev->vqs[i].log ||
@@ -385,6 +388,38 @@ long vhost_dev_reset_owner(struct vhost_dev

*dev)

Quoted 8 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 393

Quoted 6 lines
drivers/vhost/vhost.c +3 -0
      return 0;
 }
 
+/* 
+     comments
+*/

Hmm.

Fixed it in previous patch already.

Quoted 9 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev · line 394

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool

shutdown)

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 399

Quoted 5 lines
drivers/vhost/vhost.c +5 -0
+{
+     int i, j = 0;
+
+     i = vq->done_idx;
+     while (i != vq->upend_idx) {

A for loop might be clearer.

Ok.

Quoted 1 line
Quoted 1 line
drivers/vhost/vhost.c +1 -0
+             if ((vq->heads[i].len == VHOST_DMA_DONE_LEN) ||
Quoted 8 lines
Quoted 4 lines

shutdown) {

On shutdown, we signal all buffers used to the guest? Why?

We signal all outstand DMAs in the case of driver has some DMA issue to prevent us from shutting down. I am afraid vhost cleanup could wait forever.

Yes but then
1. guest can reuse the buffers for something else, before device reads them
2. vhost-net module can go away
etc

IMO that this happens in finite time is one of the things driver should guarantee when it sets the zcopy flags.

BTW we must flush these on memory table change too, right?

Quoted 14 lines
Quoted 5 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 401

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+                     /* reset len = 0 */

comment not very helpful.
Could you explain what this does instead?
Or better use some constant instead of 0 ...

Fixed it already.

Quoted 6 lines
Quoted 5 lines
drivers/vhost/vhost.c +5 -0
+                     vq->heads[i].len = 0;
+                     i = (i + 1) % UIO_MAXIOV;
+                     ++j;
+             } else
+                     break;
Quoted 3 lines
Quoted 1 line

Hmm so if the 1st entry does not complete, you do not signal anything?

No used buffers to guest, should we signal still?

This is just myself noting that we still force used entries to be in order. It's ok from correctness pov but likely is at least part of the reason you still see
TX ring overruns.

Quoted 45 lines
Quoted 4 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev · line 407

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+     }

Looking at this loop, done idx is the consumer and used idx is the producer, right?

Yes.

Quoted 17 lines

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 409

Quoted 2 lines
drivers/vhost/vhost.c +2 -0
+     if (j) {
+             /* comments */

Yes?

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 413

Quoted 2 lines
drivers/vhost/vhost.c +2 -0
+             if (i > vq->done_idx)
+                     vhost_add_used_n(vq, &vq->heads[vq->done_idx],

j);

drivers/vhost/vhost.c long vhost_dev_reset_owner(struct vhost_dev *dev) · line 419

Quoted 8 lines
drivers/vhost/vhost.c +8 -0
+             else {
+                     vhost_add_used_n(vq, &vq->heads[vq->done_idx],
+                                      UIO_MAXIOV - vq->done_idx);
+                     vhost_add_used_n(vq, vq->heads, i);
+             }
+             vq->done_idx = i;
+             vhost_signal(vq->dev, vq);
+             atomic_sub(j, &vq->refcnt);
Quoted 3 lines

Code will likely be simpler if you call vhost_add_used once for each head in the first loop. Possibly add_used_signal might be a good idea too.

Ok.

Quoted 15 lines

drivers/vhost/vhost.c void vhost_dev_cleanup(struct vhost_dev *dev) · line 433

Quoted 11 lines
drivers/vhost/vhost.c +4 -0
+     }
+}
+
 /* Caller should have device mutex */
 void vhost_dev_cleanup(struct vhost_dev *dev)
 {
@@ -395,6 +430,11 @@ void vhost_dev_cleanup(struct vhost_dev *dev)
                      vhost_poll_stop(&dev->vqs[i].poll);
                      vhost_poll_flush(&dev->vqs[i].poll);
              }
+             /* wait for all lower device DMAs done, then notify

guest */

Quoted 3 lines
drivers/vhost/vhost.c +3 -0
+             if (atomic_read(&dev->vqs[i].refcnt)) {
+                     msleep(1000);
+                     vhost_zerocopy_signal_used(&dev->vqs[i],
Quoted 8 lines
Quoted 5 lines

true);

drivers/vhost/vhost.c void vhost_dev_cleanup(struct vhost_dev *dev) · line 437

Quoted 1 line
drivers/vhost/vhost.c +1 -0
+             }

This needs to be fixed somehow. Use a completion object and wait on it?

Worried about what if the driver has some DMAs issue, which would prevent vhost from shutting down.

This needs to be fixed in the driver then.

Quoted 63 lines
Quoted 19 lines

drivers/vhost/vhost.c static long vhost_set_vring(struct vhost_dev

Quoted 4 lines
drivers/vhost/vhost.c +0 -0
              if (dev->vqs[i].error_ctx)
                      eventfd_ctx_put(dev->vqs[i].error_ctx);
              if (dev->vqs[i].error)
@@ -603,6 +643,10 @@ static long vhost_set_vring(struct vhost_dev

*d, int ioctl, void __user *argp)

drivers/vhost/vhost.c void vhost_disable_notify(struct

Quoted 11 lines
drivers/vhost/vhost.c +4 -0
      mutex_lock(&vq->mutex);
 
+     /* force all lower device DMAs done */
+     if (atomic_read(&vq->refcnt)) 
+             vhost_zerocopy_signal_used(vq, true);
+
      switch (ioctl) {
      case VHOST_SET_VRING_NUM:
              /* Resizing ring with an active backend?
@@ -1416,3 +1460,12 @@ void vhost_disable_notify(struct

vhost_virtqueue *vq)

drivers/vhost/vhost.c void vhost_disable_notify(struct · line 1460

Quoted 1 line
drivers/vhost/vhost.c +0 -0
              vq_err(vq, "Failed to enable notification at %p: %d

\n",

Quoted 11 lines

drivers/vhost/vhost.c void vhost_disable_notify(struct vhost_virtqueue *vq) · line 1469

Quoted 9 lines
drivers/vhost/vhost.c +7 -0
                     &vq->used->flags, r);
 }
+
+void vhost_zerocopy_callback(struct sk_buff *skb)
+{
+     int idx = skb_shinfo(skb)->ubuf.desc;
+     struct vhost_virtqueue *vq = skb_shinfo(skb)->ubuf.arg;
+
+     /* set len = 1 to mark this desc buffers done DMA */

this comment can now go.

Yes, it's gone already.

Quoted 17 lines

drivers/vhost/vhost.h line 16

Quoted 11 lines
drivers/vhost/vhost.c +2 -0
+     vq->heads[idx].len = VHOST_DMA_DONE_LEN;
+}
drivers/vhost/vhost.h +1 -0
index b3363ae..8e3ecc7 100644
@@ -13,6 +13,10 @@
 #include <linux/virtio_ring.h>
 #include <asm/atomic.h>
 
+/* This is for zerocopy, used buffer len is set to 1 when lower

device DMA

Quoted 5 lines
drivers/vhost/vhost.h +3 -0
+ * done */
+#define VHOST_DMA_DONE_LEN   1
+
 struct vhost_device;
 
Quoted 10 lines

drivers/vhost/vhost.h struct vhost_virtqueue { · line 116

Quoted 7 lines
drivers/vhost/vhost.h +2 -0
 struct vhost_work;
@@ -108,6 +112,12 @@ struct vhost_virtqueue {
      /* Log write descriptors */
      void __user *log_base;
      struct vhost_log *log;
+     /* vhost zerocopy support */
+     atomic_t refcnt; /* num of outstanding zerocopy DMAs */

future enhancement idea: this is used apparently under vq lock so no need for an atomic?

It is also used in skb vhost zerocopy callback.

Quoted 8 lines
Quoted 5 lines

drivers/vhost/vhost.h struct vhost_virtqueue { · line 117

Quoted 1 line
drivers/vhost/vhost.h +1 -0
+     /* copy of avail idx to monitor outstanding DMA zerocopy

buffers */

looking at code upend_idx seems to be calculated independently of guest avail idx - could you clarify pls?

Yes, you are right. Should change it to: upend_idx is used to track vring ids for outstanding zero-copy DMA buffers?

mention producer-consumer index in a head array used as a circular ring datastructure.

Quoted 32 lines
Quoted 4 lines

drivers/vhost/vhost.h struct vhost_virtqueue { · line 119

Quoted 2 lines
drivers/vhost/vhost.h +2 -0
+     int upend_idx;
+     /* copy of used idx to monintor DMA done zerocopy buffers */

monitor

Ok.

Quoted 5 lines

drivers/vhost/vhost.h struct vhost_virtqueue { · line 120

Quoted 1 line
drivers/vhost/vhost.h +1 -0
+     int done_idx;

I think in reality these are just producer and consumer in the head structure which for zero copy is used

Yes.

Quoted 18 lines

drivers/vhost/vhost.h bool vhost_enable_notify(struct vhost_virtqueue

Quoted 5 lines
drivers/vhost/vhost.h +0 -0
 };
 
 struct vhost_dev {
@@ -154,6 +164,8 @@ bool vhost_enable_notify(struct vhost_virtqueue

*);

drivers/vhost/vhost.h bool vhost_enable_notify(struct vhost_virtqueue · line 164

Quoted 2 lines
drivers/vhost/vhost.h +0 -0
 int vhost_log_write(struct vhost_virtqueue *vq, struct vhost_log

*log,

drivers/vhost/vhost.h bool vhost_enable_notify(struct vhost_virtqueue *);

Quoted 3 lines
drivers/vhost/vhost.h +2 -0
                  unsigned int log_num, u64 len);
+void vhost_zerocopy_callback(struct sk_buff *skb);
+void vhost_zerocopy_signal_used(struct vhost_virtqueue *vq, bool

shutdown);

drivers/vhost/vhost.h bool vhost_enable_notify(struct vhost_virtqueue · line 168

Quoted 2 lines
drivers/vhost/vhost.h +0 -0
 #define vq_err(vq, fmt, ...) do {

\

Quoted 2 lines
drivers/vhost/vhost.h +0 -0
              pr_debug(pr_fmt(fmt), ##__VA_ARGS__);       \
 
#21 lore raw
In reply to Shirley Ma
Quoted 2 lines · Shirley Ma

Yes, fixed it already. I might change it to PAGE_SIZE for now since the small message sizes regression issue.

If that fixes it, let's do that for now.

Signature · 1 line
MST
#22 lore raw
In reply to Shirley Ma
Quoted 29 lines · Shirley Ma
Quoted 6 lines · Michael S. Tsirkin
Quoted 1 line

+/* Since we need to keep the order of used_idx as avail_idx, it's

possible that

Quoted 1 line

+ * DMA done not in order in lower device driver for some reason. To

prevent

Quoted 1 line

+ * used_idx out of order, upend_idx is used to track avail_idx

order, done_idx

Quoted 8 lines
Quoted 1 line

+ * is used to track used_idx order. Once lower device DMA done,

then upend_idx

Quoted 1 line

+ * can move to done_idx.

Could you clarify this please? virtio explicitly allows out of order completion of requests. Does it simplify code that we try to keep used index updates in-order? Because if not, this is not really a requirement.

Hello Mike,

Based on my testing, vhost_add_used() must be in order from vhost_get_vq_desc(). Otherwise, virtio_net ring seems get double freed. I didn't spend time on debugging this.

in virtqueue_get_buf

        if (unlikely(!vq->data[i])) {
                BAD_RING(vq, "id %u is not a head!\n", i);
                return NULL;
        }
Quoted 4 lines

That's the reason I created the upend_idx and done_idx.

Thanks
Shirley

One thing of note: it's possible that this actually works better than trying to complete out of order, as the ring just keeps going which should be good for cache utilization. OTOH, this might explain why
you are over-running the TX ring much more with this patch.

So I don't say this should block merging the patch, but I very much would like to understand the issue, and it's interesting to experiment with fixing it and seeing what it does to performance and to code size.

Signature · 1 line
MST