Skip to contentSkip to conversation

vhost_net: conditionally enable tx polling

PATCH In review Jason Wang 2 participants 7 messages Oct 31, 2017 – Nov 2, 2017

Subsystems (5)
lore
Share & tools
lore

Jason Wang

author
#0 lore raw

PATCH 1/1 vhost_net: conditionally enable tx polling

We always poll tx for socket, this is sub optimal since:

- we only want to be notified when sndbuf is available
- this will slightly increase the waitqueue traversing time and more important, vhost could not benefit from commit
commit 9e641bdcfa4e
("net-tun: restructure tun_do_read for better sleep/wakeup efficiency") even if we've stopped rx polling during handle_rx() since tx poll were still left in the waitqueue.

Pktgen from a remote host to VM over mlx4 shows 5.5% improvements on rx PPS. (from 1.27Mpps to 1.34Mpps)

Cc Wei Xu <[email protected]>
Cc Matthew Rosato <[email protected]>
---
 drivers/vhost/net.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/vhost/net.c +8 -3
index 68677d9..286c3e4 100644
@@ -471,6 +471,7 @@ static void handle_tx(struct vhost_net *net)
 		goto out;
 
 	vhost_disable_notify(&net->dev, vq);
+	vhost_net_disable_vq(net, vq);
 
 	hdr_size = nvq->vhost_hlen;
 	zcopy = nvq->ubufs;
@@ -556,6 +557,8 @@ static void handle_tx(struct vhost_net *net)
 					% UIO_MAXIOV;
 			}
 			vhost_discard_vq_desc(vq, 1);
+			if (err == -EAGAIN)
+				vhost_net_enable_vq(net, vq);
 			break;
 		}
 		if (err != len)
@@ -1145,9 +1148,11 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
 		r = vhost_vq_init_access(vq);
 		if (r)
 			goto err_used;
-		r = vhost_net_enable_vq(n, vq);
-		if (r)
-			goto err_used;
+		if (index == VHOST_NET_VQ_RX) {
+			r = vhost_net_enable_vq(n, vq);
+			if (r)
+				goto err_used;
+		}
 
 		oldubufs = nvq->ubufs;
 		nvq->ubufs = ubufs;
Signature · 1 line
2.7.4
#1 lore raw
Quoted 10 lines · Jason Wang

We always poll tx for socket, this is sub optimal since:

- we only want to be notified when sndbuf is available
- this will slightly increase the waitqueue traversing time and more important, vhost could not benefit from commit
commit 9e641bdcfa4e
("net-tun: restructure tun_do_read for better sleep/wakeup efficiency") even if we've stopped rx polling during handle_rx() since tx poll were still left in the waitqueue.

Quoted 7 lines

Pktgen from a remote host to VM over mlx4 shows 5.5% improvements on rx PPS. (from 1.27Mpps to 1.34Mpps)

Cc Wei Xu <[email protected]>
Cc Matthew Rosato <[email protected]>

---

Now that vhost_poll_stop happens on data path
a lot, I'd say

        if (poll->wqh)

there should be unlikely().

Quoted 16 lines
 drivers/vhost/net.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/vhost/net.c +1 -0
index 68677d9..286c3e4 100644
@@ -471,6 +471,7 @@ static void handle_tx(struct vhost_net *net)
 		goto out;
 
 	vhost_disable_notify(&net->dev, vq);
+	vhost_net_disable_vq(net, vq);
 
 	hdr_size = nvq->vhost_hlen;
 	zcopy = nvq->ubufs;

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

Quoted 8 lines
drivers/vhost/net.c +2 -0
@@ -556,6 +557,8 @@ static void handle_tx(struct vhost_net *net)
 					% UIO_MAXIOV;
 			}
 			vhost_discard_vq_desc(vq, 1);
+			if (err == -EAGAIN)
+				vhost_net_enable_vq(net, vq);
 			break;
 		}
 		if (err != len)

I would probably just enable it unconditionally here. Why not?

Quoted 5 lines
drivers/vhost/net.c +0 -1
@@ -1145,9 +1148,11 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
 		r = vhost_vq_init_access(vq);
 		if (r)
 			goto err_used;
-		r = vhost_net_enable_vq(n, vq);

drivers/vhost/net.c static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) · line 1158

Quoted 10 lines
drivers/vhost/net.c +5 -2
-		if (r)
-			goto err_used;
+		if (index == VHOST_NET_VQ_RX) {
+			r = vhost_net_enable_vq(n, vq);
+			if (r)
+				goto err_used;
+		}
 
 		oldubufs = nvq->ubufs;
 		nvq->ubufs = ubufs;

This last chunk seems questionable. If queue has stuff in it when we connect the backend, we'll miss a wakeup. I suspect this can happen during migration.

Quoted 2 lines
Signature · 1 line
2.7.4
author
#2 lore raw
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Jason Wang

We always poll tx for socket, this is sub optimal since:

- we only want to be notified when sndbuf is available
- this will slightly increase the waitqueue traversing time and more important, vhost could not benefit from commit
commit 9e641bdcfa4e
("net-tun: restructure tun_do_read for better sleep/wakeup efficiency") even if we've stopped rx polling during handle_rx() since tx poll were still left in the waitqueue.

Pktgen from a remote host to VM over mlx4 shows 5.5% improvements on rx PPS. (from 1.27Mpps to 1.34Mpps)

Quoted 8 lines
Quoted 4 lines
Cc Wei Xu <[email protected]>
Cc Matthew Rosato <[email protected]>

---

Now that vhost_poll_stop happens on data path
a lot, I'd say

         if (poll->wqh)

there should be unlikely().

It has been there since 8241a1e466cd ("vhost_net: stop polling socket during rx processing"). So it will be used for rx path too which unlikely() does not work as well as the case in tx.

Quoted 15 lines
Quoted 15 lines

  drivers/vhost/net.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
drivers/vhost/net.c +1 -0
index 68677d9..286c3e4 100644
@@ -471,6 +471,7 @@ static void handle_tx(struct vhost_net *net)
  		goto out;
 
  	vhost_disable_notify(&net->dev, vq);
+	vhost_net_disable_vq(net, vq);
 
Quoted 13 lines

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

Quoted 11 lines
drivers/vhost/net.c +2 -0
  	hdr_size = nvq->vhost_hlen;
  	zcopy = nvq->ubufs;
@@ -556,6 +557,8 @@ static void handle_tx(struct vhost_net *net)
  					% UIO_MAXIOV;
  			}
  			vhost_discard_vq_desc(vq, 1);
+			if (err == -EAGAIN)
+				vhost_net_enable_vq(net, vq);
  			break;
  		}
  		if (err != len)

I would probably just enable it unconditionally here. Why not?

I thought we only care about the case of tun_sock_write_space() and for the errors other than -EAGAIN, they have nothing to do with polling.

Quoted 13 lines
Quoted 13 lines
drivers/vhost/net.c +5 -3
@@ -1145,9 +1148,11 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  		r = vhost_vq_init_access(vq);
  		if (r)
  			goto err_used;
-		r = vhost_net_enable_vq(n, vq);
-		if (r)
-			goto err_used;
+		if (index == VHOST_NET_VQ_RX) {
+			r = vhost_net_enable_vq(n, vq);
+			if (r)
+				goto err_used;
+		}
 
Quoted 5 lines

drivers/vhost/net.c static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) · line 1158

Quoted 2 lines
drivers/vhost/net.c +0 -0
  		oldubufs = nvq->ubufs;
  		nvq->ubufs = ubufs;

This last chunk seems questionable. If queue has stuff in it when we connect the backend, we'll miss a wakeup. I suspect this can happen during migration.

Unless qemu pass a tap which s already had pending tx packets.

I can remove this chuck, but if guest does not transmit any packet, rx can't benefit from this.

Thanks

Quoted 4 lines
Quoted 4 lines
Signature · 2 lines
-- 
2.7.4

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#3 lore raw
Quoted 21 lines · Jason Wang
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Jason Wang

We always poll tx for socket, this is sub optimal since:

- we only want to be notified when sndbuf is available
- this will slightly increase the waitqueue traversing time and more important, vhost could not benefit from commit
commit 9e641bdcfa4e
("net-tun: restructure tun_do_read for better sleep/wakeup efficiency") even if we've stopped rx polling during handle_rx() since tx poll were still left in the waitqueue.

Pktgen from a remote host to VM over mlx4 shows 5.5% improvements on rx PPS. (from 1.27Mpps to 1.34Mpps)

Cc Wei Xu <[email protected]>
Cc Matthew Rosato <[email protected]>

---

Quoted 8 lines
Quoted 4 lines

Now that vhost_poll_stop happens on data path
a lot, I'd say

         if (poll->wqh)

there should be unlikely().

It has been there since 8241a1e466cd ("vhost_net: stop polling socket during rx processing"). So it will be used for rx path too which unlikely() does not work as well as the case in tx.

Worth testing, does not have to block this patch.

Quoted 28 lines
Quoted 18 lines
Quoted 18 lines


  drivers/vhost/net.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
drivers/vhost/net.c +1 -0
index 68677d9..286c3e4 100644
@@ -471,6 +471,7 @@ static void handle_tx(struct vhost_net *net)
  		goto out;
  	vhost_disable_notify(&net->dev, vq);
+	vhost_net_disable_vq(net, vq);
  	hdr_size = nvq->vhost_hlen;
  	zcopy = nvq->ubufs;
@@ -556,6 +557,8 @@ static void handle_tx(struct vhost_net *net)
  					% UIO_MAXIOV;
Quoted 9 lines

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

Quoted 7 lines
drivers/vhost/net.c +2 -0
  			}
  			vhost_discard_vq_desc(vq, 1);
+			if (err == -EAGAIN)
+				vhost_net_enable_vq(net, vq);
  			break;
  		}
  		if (err != len)

I would probably just enable it unconditionally here. Why not?

Quoted 2 lines

I thought we only care about the case of tun_sock_write_space() and for the errors other than -EAGAIN, they have nothing to do with polling.

We could thinkably get ENOMEM I guess. If we miss a code things get stuck - It's just easier not to add extra code IMHO.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/vhost/net.c +2 -3
@@ -1145,9 +1148,11 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  		r = vhost_vq_init_access(vq);
  		if (r)
  			goto err_used;
-		r = vhost_net_enable_vq(n, vq);
-		if (r)
-			goto err_used;
+		if (index == VHOST_NET_VQ_RX) {
+			r = vhost_net_enable_vq(n, vq);
Quoted 8 lines

drivers/vhost/net.c static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) · line 1158

Quoted 5 lines
drivers/vhost/net.c +3 -0
+			if (r)
+				goto err_used;
+		}
  		oldubufs = nvq->ubufs;
  		nvq->ubufs = ubufs;

This last chunk seems questionable. If queue has stuff in it when we connect the backend, we'll miss a wakeup. I suspect this can happen during migration.

Quoted 6 lines

Unless qemu pass a tap which s already had pending tx packets.

I can remove this chuck, but if guest does not transmit any packet, rx can't benefit from this.

Thanks

Not sure I understand the last sentence. vhost will stay polling the socket - why is that a problem?

Quoted 9 lines
Quoted 4 lines
Quoted 4 lines
Signature · 2 lines
-- 
2.7.4

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

author
#4 lore raw
Quoted 21 lines · Michael S. Tsirkin
Quoted 20 lines · Jason Wang
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Jason Wang

We always poll tx for socket, this is sub optimal since:

- we only want to be notified when sndbuf is available
- this will slightly increase the waitqueue traversing time and more important, vhost could not benefit from commit
commit 9e641bdcfa4e
("net-tun: restructure tun_do_read for better sleep/wakeup efficiency") even if we've stopped rx polling during handle_rx() since tx poll were still left in the waitqueue.

Pktgen from a remote host to VM over mlx4 shows 5.5% improvements on rx PPS. (from 1.27Mpps to 1.34Mpps)

Cc Wei Xu <[email protected]>
Cc Matthew Rosato <[email protected]>

---

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

Now that vhost_poll_stop happens on data path
a lot, I'd say

          if (poll->wqh)

there should be unlikely().

It has been there since 8241a1e466cd ("vhost_net: stop polling socket during rx processing"). So it will be used for rx path too which unlikely() does not work as well as the case in tx.

Worth testing, does not have to block this patch.

Ok.

Quoted 25 lines
Quoted 25 lines
Quoted 25 lines
Quoted 15 lines
   drivers/vhost/net.c | 11 ++++++++---
   1 file changed, 8 insertions(+), 3 deletions(-)
drivers/vhost/net.c +1 -0
index 68677d9..286c3e4 100644
@@ -471,6 +471,7 @@ static void handle_tx(struct vhost_net *net)
   		goto out;
   	vhost_disable_notify(&net->dev, vq);
+	vhost_net_disable_vq(net, vq);
   	hdr_size = nvq->vhost_hlen;
   	zcopy = nvq->ubufs;

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

Quoted 8 lines
drivers/vhost/net.c +2 -0
@@ -556,6 +557,8 @@ static void handle_tx(struct vhost_net *net)
   					% UIO_MAXIOV;
   			}
   			vhost_discard_vq_desc(vq, 1);
+			if (err == -EAGAIN)
+				vhost_net_enable_vq(net, vq);
   			break;
   		}
   		if (err != len)

I would probably just enable it unconditionally here. Why not?

Quoted 4 lines
Quoted 2 lines

I thought we only care about the case of tun_sock_write_space() and for the errors other than -EAGAIN, they have nothing to do with polling.

We could thinkably get ENOMEM I guess. If we miss a code things get stuck - It's just easier not to add extra code IMHO.

ENOBUFS actually. I agree to remove the specific checking of -EAGAIN here.

Quoted 18 lines
Quoted 18 lines
Quoted 18 lines
Quoted 5 lines
drivers/vhost/net.c +0 -0
@@ -1145,9 +1148,11 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
   		r = vhost_vq_init_access(vq);
   		if (r)
   			goto err_used;

drivers/vhost/net.c static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) · line 1157

Quoted 10 lines
drivers/vhost/net.c +5 -3
-		r = vhost_net_enable_vq(n, vq);
-		if (r)
-			goto err_used;
+		if (index == VHOST_NET_VQ_RX) {
+			r = vhost_net_enable_vq(n, vq);
+			if (r)
+				goto err_used;
+		}
   		oldubufs = nvq->ubufs;
   		nvq->ubufs = ubufs;

This last chunk seems questionable. If queue has stuff in it when we connect the backend, we'll miss a wakeup. I suspect this can happen during migration.

Quoted 8 lines
Quoted 6 lines

Unless qemu pass a tap which s already had pending tx packets.

I can remove this chuck, but if guest does not transmit any packet, rx can't benefit from this.

Thanks

Not sure I understand the last sentence. vhost will stay polling the socket - why is that a problem?

No function problem at all. If guest does not transmit any packet, the patch does not have any effect.

Will remove this chunk in next version.

Thanks

Quoted 8 lines
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Signature · 2 lines
-- 
2.7.4

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#5 lore raw
In reply to Jason Wang
Quoted 21 lines · Jason Wang
Quoted 18 lines · Michael S. Tsirkin
Quoted 17 lines · Jason Wang

We always poll tx for socket, this is sub optimal since:

- we only want to be notified when sndbuf is available
- this will slightly increase the waitqueue traversing time and more important, vhost could not benefit from commit
commit 9e641bdcfa4e
("net-tun: restructure tun_do_read for better sleep/wakeup efficiency") even if we've stopped rx polling during handle_rx() since tx poll were still left in the waitqueue.

Pktgen from a remote host to VM over mlx4 shows 5.5% improvements on rx PPS. (from 1.27Mpps to 1.34Mpps)

Cc Wei Xu <[email protected]>
Cc Matthew Rosato <[email protected]>

---

Quoted 8 lines
Quoted 4 lines

Now that vhost_poll_stop happens on data path
a lot, I'd say

         if (poll->wqh)

there should be unlikely().

It has been there since 8241a1e466cd ("vhost_net: stop polling socket during rx processing"). So it will be used for rx path too which unlikely() does not work as well as the case in tx.

Worth testing, does not have to block this patch.

Quoted 28 lines
Quoted 18 lines
Quoted 18 lines


  drivers/vhost/net.c | 11 ++++++++---
  1 file changed, 8 insertions(+), 3 deletions(-)
drivers/vhost/net.c +1 -0
index 68677d9..286c3e4 100644
@@ -471,6 +471,7 @@ static void handle_tx(struct vhost_net *net)
  		goto out;
  	vhost_disable_notify(&net->dev, vq);
+	vhost_net_disable_vq(net, vq);
  	hdr_size = nvq->vhost_hlen;
  	zcopy = nvq->ubufs;
@@ -556,6 +557,8 @@ static void handle_tx(struct vhost_net *net)
  					% UIO_MAXIOV;
Quoted 9 lines

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

Quoted 7 lines
drivers/vhost/net.c +2 -0
  			}
  			vhost_discard_vq_desc(vq, 1);
+			if (err == -EAGAIN)
+				vhost_net_enable_vq(net, vq);
  			break;
  		}
  		if (err != len)

I would probably just enable it unconditionally here. Why not?

Quoted 2 lines

I thought we only care about the case of tun_sock_write_space() and for the errors other than -EAGAIN, they have nothing to do with polling.

We could thinkably get ENOMEM I guess. If we miss a code things get stuck - It's just easier not to add extra code IMHO.

Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
drivers/vhost/net.c +2 -3
@@ -1145,9 +1148,11 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
  		r = vhost_vq_init_access(vq);
  		if (r)
  			goto err_used;
-		r = vhost_net_enable_vq(n, vq);
-		if (r)
-			goto err_used;
+		if (index == VHOST_NET_VQ_RX) {
+			r = vhost_net_enable_vq(n, vq);
Quoted 8 lines

drivers/vhost/net.c static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) · line 1158

Quoted 5 lines
drivers/vhost/net.c +3 -0
+			if (r)
+				goto err_used;
+		}
  		oldubufs = nvq->ubufs;
  		nvq->ubufs = ubufs;

This last chunk seems questionable. If queue has stuff in it when we connect the backend, we'll miss a wakeup. I suspect this can happen during migration.

Quoted 6 lines

Unless qemu pass a tap which s already had pending tx packets.

I can remove this chuck, but if guest does not transmit any packet, rx can't benefit from this.

Thanks

Not sure I understand the last sentence. vhost will stay polling the socket - why is that a problem?

Quoted 9 lines
Quoted 4 lines
Quoted 4 lines
Signature · 2 lines
-- 
2.7.4

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

_______________________________________________
Virtualization mailing list
[email protected]
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

#6 lore raw
In reply to Jason Wang
Quoted 10 lines · Jason Wang

We always poll tx for socket, this is sub optimal since:

- we only want to be notified when sndbuf is available
- this will slightly increase the waitqueue traversing time and more important, vhost could not benefit from commit
commit 9e641bdcfa4e
("net-tun: restructure tun_do_read for better sleep/wakeup efficiency") even if we've stopped rx polling during handle_rx() since tx poll were still left in the waitqueue.

Quoted 7 lines

Pktgen from a remote host to VM over mlx4 shows 5.5% improvements on rx PPS. (from 1.27Mpps to 1.34Mpps)

Cc Wei Xu <[email protected]>
Cc Matthew Rosato <[email protected]>

---

Now that vhost_poll_stop happens on data path
a lot, I'd say

        if (poll->wqh)

there should be unlikely().

Quoted 16 lines
 drivers/vhost/net.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
drivers/vhost/net.c +1 -0
index 68677d9..286c3e4 100644
@@ -471,6 +471,7 @@ static void handle_tx(struct vhost_net *net)
 		goto out;
 
 	vhost_disable_notify(&net->dev, vq);
+	vhost_net_disable_vq(net, vq);
 
 	hdr_size = nvq->vhost_hlen;
 	zcopy = nvq->ubufs;

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

Quoted 8 lines
drivers/vhost/net.c +2 -0
@@ -556,6 +557,8 @@ static void handle_tx(struct vhost_net *net)
 					% UIO_MAXIOV;
 			}
 			vhost_discard_vq_desc(vq, 1);
+			if (err == -EAGAIN)
+				vhost_net_enable_vq(net, vq);
 			break;
 		}
 		if (err != len)

I would probably just enable it unconditionally here. Why not?

Quoted 5 lines
drivers/vhost/net.c +0 -1
@@ -1145,9 +1148,11 @@ static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd)
 		r = vhost_vq_init_access(vq);
 		if (r)
 			goto err_used;
-		r = vhost_net_enable_vq(n, vq);

drivers/vhost/net.c static long vhost_net_set_backend(struct vhost_net *n, unsigned index, int fd) · line 1158

Quoted 10 lines
drivers/vhost/net.c +5 -2
-		if (r)
-			goto err_used;
+		if (index == VHOST_NET_VQ_RX) {
+			r = vhost_net_enable_vq(n, vq);
+			if (r)
+				goto err_used;
+		}
 
 		oldubufs = nvq->ubufs;
 		nvq->ubufs = ubufs;

This last chunk seems questionable. If queue has stuff in it when we connect the backend, we'll miss a wakeup. I suspect this can happen during migration.

Quoted 2 lines
Signature · 1 line
2.7.4