Skip to contentSkip to conversation

virtio_net: refill buffer right after being used

RFC PATCH In review Shirley Ma 3 participants 6 messages Jul 29, 2011 – Aug 3, 2011

Subsystems (5)
lore
Share & tools
lore

Shirley Ma

author
#0 lore raw

RFC PATCH 1/1 virtio_net: refill buffer right after being used

To even the latency, refill buffer right after being used.

---

drivers/net/virtio_net.c +21 -12
index 0c7321c..c8201d4 100644
@@ -429,6 +429,22 @@ static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
 	return err;
 }
 
+static bool fill_one(struct virtio_net *vi, gfp_t gfp)
+{
+	int err;
+
+	if (vi->mergeable_rx_bufs)
+		err = add_recvbuf_mergeable(vi, gfp);
+	else if (vi->big_packets)
+		err = add_recvbuf_big(vi, gfp);
+	else
+		err = add_recvbuf_small(vi, gfp);
+
+	if (err >= 0)
+		++vi->num;
+	return err;
+}
+
 /* Returns false if we couldn't fill entirely (OOM). */
 static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
 {
@@ -436,17 +452,10 @@ static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
 	bool oom;
 
 	do {
-		if (vi->mergeable_rx_bufs)
-			err = add_recvbuf_mergeable(vi, gfp);
-		else if (vi->big_packets)
-			err = add_recvbuf_big(vi, gfp);
-		else
-			err = add_recvbuf_small(vi, gfp);
-
+		err = fill_one(vi, gfp);
 		oom = err == -ENOMEM;
 		if (err < 0)
 			break;
-		++vi->num;
 	} while (err > 0);
 	if (unlikely(vi->num > vi->max))
 		vi->max = vi->num;
@@ -506,13 +515,13 @@ again:
 		receive_buf(vi->dev, buf, len);
 		--vi->num;
 		received++;
-	}
-
-	if (vi->num < vi->max / 2) {
-		if (!try_fill_recv(vi, GFP_ATOMIC))
+		if (fill_one(vi, GFP_ATOMIC) < 0)
 			schedule_delayed_work(&vi->refill, 0);
 	}
 
+	/* notify buffers are refilled */
+	virtqueue_kick(vi->rvq);
+
 	/* Out of packets? */
 	if (received < budget) {
 		napi_complete(napi);
author
#1 lore raw

Resubmit it with a typo fix.

---

drivers/net/virtio_net.c +21 -12
index 0c7321c..c8201d4 100644
@@ -429,6 +429,22 @@ static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
 	return err;
 }
 
+static int fill_one(struct virtnet_info *vi, gfp_t gfp)
+{
+	int err;
+
+	if (vi->mergeable_rx_bufs)
+		err = add_recvbuf_mergeable(vi, gfp);
+	else if (vi->big_packets)
+		err = add_recvbuf_big(vi, gfp);
+	else
+		err = add_recvbuf_small(vi, gfp);
+
+	if (err >= 0)
+		++vi->num;
+	return err;
+}
+
 /* Returns false if we couldn't fill entirely (OOM). */
 static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
 {
@@ -436,17 +452,10 @@ static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
 	bool oom;
 
 	do {
-		if (vi->mergeable_rx_bufs)
-			err = add_recvbuf_mergeable(vi, gfp);
-		else if (vi->big_packets)
-			err = add_recvbuf_big(vi, gfp);
-		else
-			err = add_recvbuf_small(vi, gfp);
-
+		err = fill_one(vi, gfp);
 		oom = err == -ENOMEM;
 		if (err < 0)
 			break;
-		++vi->num;
 	} while (err > 0);
 	if (unlikely(vi->num > vi->max))
 		vi->max = vi->num;
@@ -506,13 +515,13 @@ again:
 		receive_buf(vi->dev, buf, len);
 		--vi->num;
 		received++;
-	}
-
-	if (vi->num < vi->max / 2) {
-		if (!try_fill_recv(vi, GFP_ATOMIC))
+		if (fill_one(vi, GFP_ATOMIC) < 0)
 			schedule_delayed_work(&vi->refill, 0);
 	}
 
+	/* notify buffers are refilled */
+	virtqueue_kick(vi->rvq);
+
 	/* Out of packets? */
 	if (received < budget) {
 		napi_complete(napi);
#2 lore raw
Quoted 56 lines · Shirley Ma

Resubmit it with a typo fix.

---

drivers/net/virtio_net.c +17 -9
index 0c7321c..c8201d4 100644
@@ -429,6 +429,22 @@ static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
        return err;
  }
 
+static int fill_one(struct virtnet_info *vi, gfp_t gfp)
+{
+       int err;
+
+       if (vi->mergeable_rx_bufs)
+               err = add_recvbuf_mergeable(vi, gfp);
+       else if (vi->big_packets)
+               err = add_recvbuf_big(vi, gfp);
+       else
+               err = add_recvbuf_small(vi, gfp);
+
+       if (err >= 0)
+               ++vi->num;
+       return err;
+}
+
  /* Returns false if we couldn't fill entirely (OOM). */
  static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
  {
@@ -436,17 +452,10 @@ static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
        bool oom;
 
        do {
-               if (vi->mergeable_rx_bufs)
-                       err = add_recvbuf_mergeable(vi, gfp);
-               else if (vi->big_packets)
-                       err = add_recvbuf_big(vi, gfp);
-               else
-                       err = add_recvbuf_small(vi, gfp);
-
+               err = fill_one(vi, gfp);
                oom = err == -ENOMEM;
                if (err < 0)
                        break;
-               ++vi->num;
        } while (err > 0);
        if (unlikely(vi->num > vi->max))
                vi->max = vi->num;
@@ -506,13 +515,13 @@ again:
                receive_buf(vi->dev, buf, len);
                --vi->num;
                received++;
-       }

drivers/net/virtio_net.c again: · line 524

Quoted 10 lines
drivers/net/virtio_net.c +4 -3
-
-       if (vi->num < vi->max / 2) {
-               if (!try_fill_recv(vi, GFP_ATOMIC))
+               if (fill_one(vi, GFP_ATOMIC) < 0)
                        schedule_delayed_work(&vi->refill, 0);
        }
 
+       /* notify buffers are refilled */
+       virtqueue_kick(vi->rvq);
+

How does this reduce latency? We are doing the same amount of work
in both cases, and in both cases the newly available buffers are not visible to the device until the virtqueue_kick..

Quoted 10 lines
drivers/net/virtio_net.c +0 -1
        /* Out of packets? */
        if (received < budget) {
                napi_complete(napi);
 
 
--

To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to [email protected]
More majordomo info at  http://vger.kernel.org/majordomo-info.html

author
#3 lore raw
Quoted 67 lines · Mike Waychison

drivers/net/virtio_net.c static int add_recvbuf_mergeable(struct

Quoted 10 lines · Shirley Ma

Resubmit it with a typo fix.

---

drivers/net/virtio_net.c +0 -0
index 0c7321c..c8201d4 100644
@@ -429,6 +429,22 @@ static int add_recvbuf_mergeable(struct

virtnet_info *vi, gfp_t gfp)

drivers/net/virtio_net.c static bool try_fill_recv(struct virtnet_info

Quoted 23 lines
drivers/net/virtio_net.c +16 -0
       return err;
 }
 
+static int fill_one(struct virtnet_info *vi, gfp_t gfp)
+{
+       int err;
+
+       if (vi->mergeable_rx_bufs)
+               err = add_recvbuf_mergeable(vi, gfp);
+       else if (vi->big_packets)
+               err = add_recvbuf_big(vi, gfp);
+       else
+               err = add_recvbuf_small(vi, gfp);
+
+       if (err >= 0)
+               ++vi->num;
+       return err;
+}
+
 /* Returns false if we couldn't fill entirely (OOM). */
 static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
 {
@@ -436,17 +452,10 @@ static bool try_fill_recv(struct virtnet_info

*vi, gfp_t gfp)

Quoted 30 lines
drivers/net/virtio_net.c +2 -12
       bool oom;
 
       do {
-               if (vi->mergeable_rx_bufs)
-                       err = add_recvbuf_mergeable(vi, gfp);
-               else if (vi->big_packets)
-                       err = add_recvbuf_big(vi, gfp);
-               else
-                       err = add_recvbuf_small(vi, gfp);
-
+               err = fill_one(vi, gfp);
               oom = err == -ENOMEM;
               if (err < 0)
                       break;
-               ++vi->num;
       } while (err > 0);
       if (unlikely(vi->num > vi->max))
               vi->max = vi->num;
@@ -506,13 +515,13 @@ again:
               receive_buf(vi->dev, buf, len);
               --vi->num;
               received++;
-       }
-
-       if (vi->num < vi->max / 2) {
-               if (!try_fill_recv(vi, GFP_ATOMIC))
+               if (fill_one(vi, GFP_ATOMIC) < 0)
                       schedule_delayed_work(&vi->refill, 0);
       }
 
Quoted 7 lines

drivers/net/virtio_net.c again: · line 524

Quoted 3 lines
drivers/net/virtio_net.c +3 -0
+       /* notify buffers are refilled */
+       virtqueue_kick(vi->rvq);
+

How does this reduce latency? We are doing the same amount of work
in both cases, and in both cases the newly available buffers are not visible to the device until the virtqueue_kick..

It averages the latency between each receive by filling only one set of buffers vs. either none buffers or 1/2 ring size buffers fill between receives.

Quoted 7 lines
Quoted 7 lines
drivers/net/virtio_net.c +0 -0
       /* Out of packets? */
       if (received < budget) {
               napi_complete(napi);
Signature · 1 line
-- 
#4 lore raw
Quoted 3 lines · Shirley Ma

It averages the latency between each receive by filling only one set of buffers vs. either none buffers or 1/2 ring size buffers fill between receives.

I see how the overhead of allocating memory is spread more evenly. Does this actually help some workloads?

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

Resubmit it with a typo fix.

---

drivers/net/virtio_net.c +17 -8
index 0c7321c..c8201d4 100644
@@ -429,6 +429,22 @@ static int add_recvbuf_mergeable(struct virtnet_info *vi, gfp_t gfp)
 	return err;
 }
 
+static int fill_one(struct virtnet_info *vi, gfp_t gfp)
+{
+	int err;
+
+	if (vi->mergeable_rx_bufs)
+		err = add_recvbuf_mergeable(vi, gfp);
+	else if (vi->big_packets)
+		err = add_recvbuf_big(vi, gfp);
+	else
+		err = add_recvbuf_small(vi, gfp);
+
+	if (err >= 0)
+		++vi->num;
+	return err;
+}
+
 /* Returns false if we couldn't fill entirely (OOM). */
 static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
 {
@@ -436,17 +452,10 @@ static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp)
 	bool oom;
 
 	do {
-		if (vi->mergeable_rx_bufs)
-			err = add_recvbuf_mergeable(vi, gfp);
-		else if (vi->big_packets)
-			err = add_recvbuf_big(vi, gfp);
-		else
-			err = add_recvbuf_small(vi, gfp);
-
+		err = fill_one(vi, gfp);
 		oom = err == -ENOMEM;
 		if (err < 0)
 			break;
-		++vi->num;
 	} while (err > 0);
 	if (unlikely(vi->num > vi->max))
 		vi->max = vi->num;

drivers/net/virtio_net.c again: · line 519

Quoted 9 lines
drivers/net/virtio_net.c +1 -4
@@ -506,13 +515,13 @@ again:
 		receive_buf(vi->dev, buf, len);
 		--vi->num;
 		received++;
-	}
-
-	if (vi->num < vi->max / 2) {
-		if (!try_fill_recv(vi, GFP_ATOMIC))
+		if (fill_one(vi, GFP_ATOMIC) < 0)
 			schedule_delayed_work(&vi->refill, 0);

If we get a large packet, we might add less that what we removed from the ring. Isn't this a problem?

Quoted 10 lines
drivers/net/virtio_net.c +3 -0
 	}
 
+	/* notify buffers are refilled */
+	virtqueue_kick(vi->rvq);
+
 	/* Out of packets? */
 	if (received < budget) {
 		napi_complete(napi);
Signature · 1 line
MST