[PATCH] virtio_net: Check for room in the vq before adding buffer

Amit Shah amit.shah at redhat.com
Mon Aug 24 00:37:04 PDT 2009


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


More information about the Virtualization mailing list