Amit Shah
2009-Aug-24 07:37 UTC
[PATCH] virtio_net: Check for room in the vq before adding buffer
Saves us one cycle of alloc-add-free if the queue was full. Signed-off-by: Amit Shah <amit.shah at redhat.com> --- drivers/net/virtio_net.c | 18 ++++++++++-------- 1 files changed, 10 insertions(+), 8 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index e94f817..1f45a10 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -276,11 +276,12 @@ static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp) { struct sk_buff *skb; struct scatterlist sg[2+MAX_SKB_FRAGS]; - int num, err, i; + int num, ret, i; bool oom = false; sg_init_table(sg, 2+MAX_SKB_FRAGS); - for (;;) { + ret = 1; + while(ret > 0) { /* Is there space to add another buffer to the vq */ struct skb_vnet_hdr *hdr; skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN); @@ -315,8 +316,8 @@ static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp) num = skb_to_sgvec(skb, sg+1, 0, skb->len) + 1; skb_queue_head(&vi->recv, skb); - err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); - if (err < 0) { + ret = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, num, skb); + if (ret < 0) { skb_unlink(skb, &vi->recv); trim_pages(vi, skb); kfree_skb(skb); @@ -335,13 +336,14 @@ static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp) { struct sk_buff *skb; struct scatterlist sg[1]; - int err; + int ret; bool oom = false; if (!vi->mergeable_rx_bufs) return try_fill_recv_maxbufs(vi, gfp); - for (;;) { + ret = 1; + while(ret > 0) { /* Is there space to add another buffer to the vq */ skb_frag_t *f; skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN); @@ -368,8 +370,8 @@ static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp) sg_init_one(sg, page_address(f->page), PAGE_SIZE); skb_queue_head(&vi->recv, skb); - err = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb); - if (err < 0) { + ret = vi->rvq->vq_ops->add_buf(vi->rvq, sg, 0, 1, skb); + if (ret < 0) { skb_unlink(skb, &vi->recv); kfree_skb(skb); break; -- 1.6.2.5 Amit
Amit Shah
2009-Aug-26 09:28 UTC
[PATCH] virtio_net: Check for room in the vq before adding buffer
Saves us one cycle of alloc-add-free if the queue was full. Signed-off-by: Amit Shah <amit.shah at redhat.com> --- Rusty: Tested this. 256 buffers are allocated with the patch, 257 without (and one is later freed). drivers/net/virtio_net.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-) diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c index e94f817..bb54a1f 100644 --- a/drivers/net/virtio_net.c +++ b/drivers/net/virtio_net.c @@ -280,7 +280,7 @@ static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp) bool oom = false; sg_init_table(sg, 2+MAX_SKB_FRAGS); - for (;;) { + do { struct skb_vnet_hdr *hdr; skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN); @@ -323,7 +323,7 @@ static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp) break; } vi->num++; - } + } while (err > 0); if (unlikely(vi->num > vi->max)) vi->max = vi->num; vi->rvq->vq_ops->kick(vi->rvq); @@ -341,7 +341,7 @@ static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp) if (!vi->mergeable_rx_bufs) return try_fill_recv_maxbufs(vi, gfp); - for (;;) { + do { skb_frag_t *f; skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN); @@ -375,7 +375,7 @@ static bool try_fill_recv(struct virtnet_info *vi, gfp_t gfp) break; } vi->num++; - } + } while (err > 0); if (unlikely(vi->num > vi->max)) vi->max = vi->num; vi->rvq->vq_ops->kick(vi->rvq); -- 1.6.2.5
Rusty Russell
2009-Aug-27 09:47 UTC
[PATCH] virtio_net: Check for room in the vq before adding buffer
On Wed, 26 Aug 2009 06:58:28 pm Amit Shah wrote:> Saves us one cycle of alloc-add-free if the queue was full. > > Signed-off-by: Amit Shah <amit.shah at redhat.com>Thanks, applied with one change:> @@ -323,7 +323,7 @@ static bool try_fill_recv_maxbufs(struct virtnet_info *vi, gfp_t gfp) > break; > } > vi->num++; > - } > + } while (err > 0);This is better as "while (err >= num)". The other one is right (we only need 1 buffer), this one we need to be able to fit "num" entries. Thanks! Rusty.