Skip to contentSkip to conversation

vhost: fix condition check for # of outstanding dma buffers

PATCH Superseded Shirley Ma 2 participants 7 messages Jul 19, 2011 – Jul 20, 2011

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Shirley Ma

author
#0 lore raw

PATCH 1/1 vhost: fix condition check for # of outstanding dma buffers

---

 drivers/vhost/net.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/net.c +4 -2
index 70ac604..83cb738 100644
@@ -189,8 +189,10 @@ static void handle_tx(struct vhost_net *net)
 				break;
 			}
 			/* If more outstanding DMAs, queue the work */
-			if (unlikely(vq->upend_idx - vq->done_idx >
-				     VHOST_MAX_PEND)) {
+			if (unlikely((vq->upend_idx - vq->done_idx >
+					VHOST_MAX_PEND) ||
+				     (vq->upend_idx - vq->done_idx >
+					 VHOST_MAX_PEND - UIO_MAXIOV))) {
 				tx_poll_start(net, sock);
 				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
 				break;
#1 lore raw
Quoted 11 lines · Shirley Ma

---

 drivers/vhost/net.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/net.c +0 -0
index 70ac604..83cb738 100644

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

Quoted 9 lines
drivers/vhost/net.c +4 -2
@@ -189,8 +189,10 @@ static void handle_tx(struct vhost_net *net)
 				break;
 			}
 			/* If more outstanding DMAs, queue the work */
-			if (unlikely(vq->upend_idx - vq->done_idx >
-				     VHOST_MAX_PEND)) {
+			if (unlikely((vq->upend_idx - vq->done_idx >
+					VHOST_MAX_PEND) ||
+				     (vq->upend_idx - vq->done_idx >
+					 VHOST_MAX_PEND - UIO_MAXIOV))) {

Could you please explain why this makes sense please? VHOST_MAX_PEND is 128 UIO_MAXIOV is 1024 so the result is negative?

I thought upend_idx - done_idx is exactly the number of buffers, so once we get too many we stop until one gets freed?

Quoted 4 lines
drivers/vhost/net.c +0 -0
 				tx_poll_start(net, sock);
 				set_bit(SOCK_ASYNC_NOSPACE, &sock->flags);
 				break;
author
#2 lore raw
Quoted 23 lines · Michael S. Tsirkin
Quoted 11 lines · Shirley Ma

---

 drivers/vhost/net.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/net.c +0 -0
index 70ac604..83cb738 100644

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

Quoted 9 lines
drivers/vhost/net.c +4 -2
@@ -189,8 +189,10 @@ static void handle_tx(struct vhost_net *net)
                              break;
                      }
                      /* If more outstanding DMAs, queue the work */
-                     if (unlikely(vq->upend_idx - vq->done_idx >
-                                  VHOST_MAX_PEND)) {
+                     if (unlikely((vq->upend_idx - vq->done_idx >
+                                     VHOST_MAX_PEND) ||
+                                  (vq->upend_idx - vq->done_idx >
+                                      VHOST_MAX_PEND -

UIO_MAXIOV))) {

Quoted 3 lines

Could you please explain why this makes sense please? VHOST_MAX_PEND is 128 UIO_MAXIOV is 1024 so the result is negative?

I thought it is equal to:

if (vq->upend_idx > vq->done_idx)

	check vq->upend_idx - vq->done_idx > VHOST_MAX_PEND
if (vq->upend_idx < vq->done_idx)
	check vq->upend_idx + UIO_MAXIOV - vq->done_idx > VHOST_MAX_PEND
Quoted 3 lines

I thought upend_idx - done_idx is exactly the number of buffers, so once we get too many we stop until one gets freed?

They are index, so in vhost zerocopy callback, we can get the idx right away.

Quoted 6 lines

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

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

&sock->flags);

Quoted 2 lines
drivers/vhost/net.c +0 -0
                              break;
#3 lore raw
Quoted 28 lines · Shirley Ma
Quoted 23 lines · Michael S. Tsirkin
Quoted 11 lines · Shirley Ma

---

 drivers/vhost/net.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/net.c +0 -0
index 70ac604..83cb738 100644

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

Quoted 9 lines
drivers/vhost/net.c +4 -2
@@ -189,8 +189,10 @@ static void handle_tx(struct vhost_net *net)
                              break;
                      }
                      /* If more outstanding DMAs, queue the work */
-                     if (unlikely(vq->upend_idx - vq->done_idx >
-                                  VHOST_MAX_PEND)) {
+                     if (unlikely((vq->upend_idx - vq->done_idx >
+                                     VHOST_MAX_PEND) ||
+                                  (vq->upend_idx - vq->done_idx >
+                                      VHOST_MAX_PEND -

UIO_MAXIOV))) {

Quoted 3 lines

Could you please explain why this makes sense please? VHOST_MAX_PEND is 128 UIO_MAXIOV is 1024 so the result is negative?

Quoted 7 lines

I thought it is equal to:

if (vq->upend_idx > vq->done_idx)

	check vq->upend_idx - vq->done_idx > VHOST_MAX_PEND
if (vq->upend_idx < vq->done_idx)
	check vq->upend_idx + UIO_MAXIOV - vq->done_idx > VHOST_MAX_PEND

Check it out: upend_idx == done_idx == 0 does not satisfy the above conditions but does trigger in your code, right?

Better keep it simple. Maybe:

	if (unlikely(vq->upend_idx - vq->done_idx > VHOST_MAX_PEND) ||
		(unlikely(vq->upend_idx < vq->done_idx) &&
	 	unlikely(vq->upend_idx + UIO_MAXIOV - vq->done_idx >
			 VHOST_MAX_PEND)))

?

Also, please add commit log documenting what does the patch
fix: something like:
'the test for # of outstanding buffers returned
incorrect results when due to wrap around, upend_idx < done_idx'?

Quoted 13 lines
Quoted 3 lines

I thought upend_idx - done_idx is exactly the number of buffers, so once we get too many we stop until one gets freed?

They are index, so in vhost zerocopy callback, we can get the idx right away.

Quoted 6 lines

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

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

&sock->flags);

Quoted 2 lines
drivers/vhost/net.c +0 -0
                              break;
author
#4 lore raw
Quoted 40 lines · Michael S. Tsirkin
Quoted 5 lines · Shirley Ma
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Shirley Ma

---

Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
 drivers/vhost/net.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/net.c +0 -0
index 70ac604..83cb738 100644
@@ -189,8 +189,10 @@ static void handle_tx(struct vhost_net

*net)

Quoted 3 lines
Quoted 3 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net · line 191

Quoted 3 lines
drivers/vhost/net.c +0 -0
                              break;
                      }
                      /* If more outstanding DMAs, queue the

work */

Quoted 15 lines
Quoted 8 lines
Quoted 7 lines
drivers/vhost/net.c +3 -2
-                     if (unlikely(vq->upend_idx - vq->done_idx
 
-                                  VHOST_MAX_PEND)) {
+                     if (unlikely((vq->upend_idx - vq->done_idx
 
+                                     VHOST_MAX_PEND) ||
+                                  (vq->upend_idx - vq->done_idx
Quoted 6 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net · line 197

Quoted 1 line
drivers/vhost/net.c +1 -0
+                                      VHOST_MAX_PEND -

UIO_MAXIOV))) {

Could you please explain why this makes sense please? VHOST_MAX_PEND is 128 UIO_MAXIOV is 1024 so the result is negative?

Quoted 6 lines

I thought it is equal to:

if (vq->upend_idx > vq->done_idx)

      check vq->upend_idx - vq->done_idx > VHOST_MAX_PEND
if (vq->upend_idx < vq->done_idx)
      check vq->upend_idx + UIO_MAXIOV - vq->done_idx >
Quoted 5 lines

VHOST_MAX_PEND

Quoted 1 line

Check it out: upend_idx == done_idx == 0 does not satisfy the above conditions but does trigger in your code, right?

We don't hit upend_idx == done_idx == 0. Only upend_idx == done_idx == UIO_MAXIOV could happen if the lower device has issue and never DMA any packets out.

Quoted 7 lines

Better keep it simple. Maybe:

        if (unlikely(vq->upend_idx - vq->done_idx > VHOST_MAX_PEND) ||
                (unlikely(vq->upend_idx < vq->done_idx) &&
                unlikely(vq->upend_idx + UIO_MAXIOV - vq->done_idx >
                         VHOST_MAX_PEND)))
Quoted 7 lines

?

Also, please add commit log documenting what does the patch
fix: something like:
'the test for # of outstanding buffers returned
incorrect results when due to wrap around, upend_idx < done_idx'?

Sure, will modify it and resubmit.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

I thought upend_idx - done_idx is exactly the number of buffers, so once we get too many we stop until one gets freed?

Quoted 10 lines
Quoted 1 line

They are index, so in vhost zerocopy callback, we can get the idx

right

Quoted 8 lines

away.

Quoted 6 lines
Quoted 3 lines
drivers/vhost/net.c +0 -0
                              tx_poll_start(net, sock);

set_bit(SOCK_ASYNC_NOSPACE,

&sock->flags);

Quoted 2 lines
                              break;

Thanks
Shirley

#5 lore raw
Quoted 44 lines · Shirley Ma
Quoted 43 lines · Michael S. Tsirkin
Quoted 5 lines · Shirley Ma
Quoted 4 lines · Michael S. Tsirkin
Quoted 3 lines · Shirley Ma

---

Quoted 8 lines
Quoted 8 lines

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

Quoted 8 lines
 drivers/vhost/net.c |    6 ++++--
 1 files changed, 4 insertions(+), 2 deletions(-)
drivers/vhost/net.c +0 -0
index 70ac604..83cb738 100644
@@ -189,8 +189,10 @@ static void handle_tx(struct vhost_net

*net)

Quoted 3 lines
Quoted 3 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net · line 191

Quoted 3 lines
drivers/vhost/net.c +0 -0
                              break;
                      }
                      /* If more outstanding DMAs, queue the

work */

Quoted 15 lines
Quoted 8 lines
Quoted 7 lines
drivers/vhost/net.c +3 -2
-                     if (unlikely(vq->upend_idx - vq->done_idx
 
-                                  VHOST_MAX_PEND)) {
+                     if (unlikely((vq->upend_idx - vq->done_idx
 
+                                     VHOST_MAX_PEND) ||
+                                  (vq->upend_idx - vq->done_idx
Quoted 6 lines

drivers/vhost/net.c static void handle_tx(struct vhost_net · line 197

Quoted 1 line
drivers/vhost/net.c +1 -0
+                                      VHOST_MAX_PEND -

UIO_MAXIOV))) {

Could you please explain why this makes sense please? VHOST_MAX_PEND is 128 UIO_MAXIOV is 1024 so the result is negative?

Quoted 6 lines

I thought it is equal to:

if (vq->upend_idx > vq->done_idx)

      check vq->upend_idx - vq->done_idx > VHOST_MAX_PEND
if (vq->upend_idx < vq->done_idx)
      check vq->upend_idx + UIO_MAXIOV - vq->done_idx >

VHOST_MAX_PEND

Quoted 1 line
Quoted 6 lines
Quoted 2 lines

Check it out: upend_idx == done_idx == 0 does not satisfy the above conditions but does trigger in your code, right?

We don't hit upend_idx == done_idx == 0. Only upend_idx == done_idx == UIO_MAXIOV could happen if the lower device has issue and never DMA any packets out.

My point was that the logic isn't the same, even though you said 'it is equal to'.
Same applies to upend_idx == 1, done_idx == 0.

Quoted 34 lines
Quoted 7 lines

Better keep it simple. Maybe:

        if (unlikely(vq->upend_idx - vq->done_idx > VHOST_MAX_PEND) ||
                (unlikely(vq->upend_idx < vq->done_idx) &&
                unlikely(vq->upend_idx + UIO_MAXIOV - vq->done_idx >
                         VHOST_MAX_PEND)))
Quoted 7 lines

?

Also, please add commit log documenting what does the patch
fix: something like:
'the test for # of outstanding buffers returned
incorrect results when due to wrap around, upend_idx < done_idx'?

Sure, will modify it and resubmit.

Quoted 4 lines
Quoted 4 lines
Quoted 3 lines

I thought upend_idx - done_idx is exactly the number of buffers, so once we get too many we stop until one gets freed?

Quoted 10 lines
Quoted 1 line

They are index, so in vhost zerocopy callback, we can get the idx

right

Quoted 8 lines

away.

Quoted 6 lines
Quoted 3 lines
drivers/vhost/net.c +0 -0
                              tx_poll_start(net, sock);

set_bit(SOCK_ASYNC_NOSPACE,

&sock->flags);

Quoted 2 lines
                              break;

Thanks
Shirley

author
#6 lore raw
Quoted 3 lines · Michael S. Tsirkin

My point was that the logic isn't the same, even though you said 'it is equal to'.
Same applies to upend_idx == 1, done_idx == 0.

:) not sure what I was thinking.

I will resubmit the patch.

Thanks
Shirley