Skip to contentSkip to conversation

virtio-net: drop rq->max and rq->num

RFC PATCH Applied Jason Wang 3 participants 6 messages Dec 27, 2013 – Jan 16, 2014

Subsystems (4)
lore
Share & tools
lore

Jason Wang

author
#0 lore raw

RFC PATCH 1/1 virtio-net: drop rq->max and rq->num

It looks like there's no need for those two fields:

- Unless there's a failure for the first refill try, rq->max should be always equal to the vring size.
- rq->num is only used to determine the condition that we need to do the refill, we could check vq->num_free instead.
- rq->num was required to be increased or decreased explicitly after each get/put which results a bad API.

So this patch removes them both to make the code simpler.

Cc Rusty Russell <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
---
 drivers/net/virtio_net.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +3 -13
index c51a988..4e1bce3 100644
@@ -72,9 +72,6 @@ struct receive_queue {
 
 	struct napi_struct napi;
 
-	/* Number of input buffers, and max we've ever had. */
-	unsigned int num, max;
-
 	/* Chain pages by the private ptr. */
 	struct page *pages;
 
@@ -360,7 +357,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 		}
 
 		page = virt_to_head_page(buf);
-		--rq->num;
 
 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
@@ -406,7 +402,6 @@ err_skb:
 		}
 		page = virt_to_head_page(buf);
 		put_page(page);
-		--rq->num;
 	}
 err_buf:
 	dev->stats.rx_dropped++;
@@ -628,10 +623,7 @@ static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
 		oom = err == -ENOMEM;
 		if (err)
 			break;
-		++rq->num;
 	} while (rq->vq->num_free);
-	if (unlikely(rq->num > rq->max))
-		rq->max = rq->num;
 	if (unlikely(!virtqueue_kick(rq->vq)))
 		return false;
 	return !oom;
@@ -699,11 +691,10 @@ again:
 	while (received < budget &&
 	       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
 		receive_buf(rq, buf, len);
-		--rq->num;
 		received++;
 	}
 
-	if (rq->num < rq->max / 2) {
+	if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
 		if (!try_fill_recv(rq, GFP_ATOMIC))
 			schedule_delayed_work(&vi->refill, 0);
 	}
@@ -1398,9 +1389,7 @@ static void free_unused_bufs(struct virtnet_info *vi)
 				give_pages(&vi->rq[i], buf);
 			else
 				dev_kfree_skb(buf);
-			--vi->rq[i].num;
 		}
-		BUG_ON(vi->rq[i].num != 0);
 	}
 }
 
@@ -1671,7 +1660,8 @@ static int virtnet_probe(struct virtio_device *vdev)
 		try_fill_recv(&vi->rq[i], GFP_KERNEL);
 
 		/* If we didn't even get one input buffer, we're useless. */
-		if (vi->rq[i].num == 0) {
+		if (vi->rq[i].vq->num_free ==
+		    virtqueue_get_vring_size(vi->rq[i].vq)) {
 			free_unused_bufs(vi);
 			err = -ENOMEM;
 			goto free_recv_bufs;
Signature · 1 line
1.8.3.2
#1 lore raw
Quoted 10 lines · Jason Wang

It looks like there's no need for those two fields:

- Unless there's a failure for the first refill try, rq->max should be always equal to the vring size.
- rq->num is only used to determine the condition that we need to do the refill, we could check vq->num_free instead.
- rq->num was required to be increased or decreased explicitly after each get/put which results a bad API.

So this patch removes them both to make the code simpler.

Nice. These fields date from when the vq struct was opaque.

Applied,
Rusty.

Quoted 83 lines
Cc Rusty Russell <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
---
 drivers/net/virtio_net.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +3 -13
index c51a988..4e1bce3 100644
@@ -72,9 +72,6 @@ struct receive_queue {
 
 	struct napi_struct napi;
 
-	/* Number of input buffers, and max we've ever had. */
-	unsigned int num, max;
-
 	/* Chain pages by the private ptr. */
 	struct page *pages;
 
@@ -360,7 +357,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 		}
 
 		page = virt_to_head_page(buf);
-		--rq->num;
 
 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
@@ -406,7 +402,6 @@ err_skb:
 		}
 		page = virt_to_head_page(buf);
 		put_page(page);
-		--rq->num;
 	}
 err_buf:
 	dev->stats.rx_dropped++;
@@ -628,10 +623,7 @@ static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
 		oom = err == -ENOMEM;
 		if (err)
 			break;
-		++rq->num;
 	} while (rq->vq->num_free);
-	if (unlikely(rq->num > rq->max))
-		rq->max = rq->num;
 	if (unlikely(!virtqueue_kick(rq->vq)))
 		return false;
 	return !oom;
@@ -699,11 +691,10 @@ again:
 	while (received < budget &&
 	       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
 		receive_buf(rq, buf, len);
-		--rq->num;
 		received++;
 	}
 
-	if (rq->num < rq->max / 2) {
+	if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
 		if (!try_fill_recv(rq, GFP_ATOMIC))
 			schedule_delayed_work(&vi->refill, 0);
 	}
@@ -1398,9 +1389,7 @@ static void free_unused_bufs(struct virtnet_info *vi)
 				give_pages(&vi->rq[i], buf);
 			else
 				dev_kfree_skb(buf);
-			--vi->rq[i].num;
 		}
-		BUG_ON(vi->rq[i].num != 0);
 	}
 }
 
@@ -1671,7 +1660,8 @@ static int virtnet_probe(struct virtio_device *vdev)
 		try_fill_recv(&vi->rq[i], GFP_KERNEL);
 
 		/* If we didn't even get one input buffer, we're useless. */
-		if (vi->rq[i].num == 0) {
+		if (vi->rq[i].vq->num_free ==
+		    virtqueue_get_vring_size(vi->rq[i].vq)) {
 			free_unused_bufs(vi);
 			err = -ENOMEM;
 			goto free_recv_bufs;
Signature · 1 line
1.8.3.2
Acked-by
#2 lore raw
Quoted 10 lines · Rusty Russell
Quoted 9 lines · Jason Wang

It looks like there's no need for those two fields:

- Unless there's a failure for the first refill try, rq->max should be always equal to the vring size.
- rq->num is only used to determine the condition that we need to do the refill, we could check vq->num_free instead.
- rq->num was required to be increased or decreased explicitly after each get/put which results a bad API.

Quoted 6 lines
Quoted 1 line

So this patch removes them both to make the code simpler.

Nice. These fields date from when the vq struct was opaque.

Applied,
Rusty.

Oops, this doesn't require any core virtio changes, so it's for DaveM:

Thanks,
Rusty.

Quoted 88 lines
Quoted 83 lines
Cc Rusty Russell <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
---
 drivers/net/virtio_net.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +3 -13
index c51a988..4e1bce3 100644
@@ -72,9 +72,6 @@ struct receive_queue {
 
 	struct napi_struct napi;
 
-	/* Number of input buffers, and max we've ever had. */
-	unsigned int num, max;
-
 	/* Chain pages by the private ptr. */
 	struct page *pages;
 
@@ -360,7 +357,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 		}
 
 		page = virt_to_head_page(buf);
-		--rq->num;
 
 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
@@ -406,7 +402,6 @@ err_skb:
 		}
 		page = virt_to_head_page(buf);
 		put_page(page);
-		--rq->num;
 	}
 err_buf:
 	dev->stats.rx_dropped++;
@@ -628,10 +623,7 @@ static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
 		oom = err == -ENOMEM;
 		if (err)
 			break;
-		++rq->num;
 	} while (rq->vq->num_free);
-	if (unlikely(rq->num > rq->max))
-		rq->max = rq->num;
 	if (unlikely(!virtqueue_kick(rq->vq)))
 		return false;
 	return !oom;
@@ -699,11 +691,10 @@ again:
 	while (received < budget &&
 	       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
 		receive_buf(rq, buf, len);
-		--rq->num;
 		received++;
 	}
 
-	if (rq->num < rq->max / 2) {
+	if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
 		if (!try_fill_recv(rq, GFP_ATOMIC))
 			schedule_delayed_work(&vi->refill, 0);
 	}
@@ -1398,9 +1389,7 @@ static void free_unused_bufs(struct virtnet_info *vi)
 				give_pages(&vi->rq[i], buf);
 			else
 				dev_kfree_skb(buf);
-			--vi->rq[i].num;
 		}
-		BUG_ON(vi->rq[i].num != 0);
 	}
 }
 
@@ -1671,7 +1660,8 @@ static int virtnet_probe(struct virtio_device *vdev)
 		try_fill_recv(&vi->rq[i], GFP_KERNEL);
 
 		/* If we didn't even get one input buffer, we're useless. */
-		if (vi->rq[i].num == 0) {
+		if (vi->rq[i].vq->num_free ==
+		    virtqueue_get_vring_size(vi->rq[i].vq)) {
 			free_unused_bufs(vi);
 			err = -ENOMEM;
 			goto free_recv_bufs;
Signature · 1 line
1.8.3.2

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

Acked-by
#3 lore raw
In reply to Rusty Russell
Quoted 10 lines · Rusty Russell
Quoted 9 lines · Jason Wang

It looks like there's no need for those two fields:

- Unless there's a failure for the first refill try, rq->max should be always equal to the vring size.
- rq->num is only used to determine the condition that we need to do the refill, we could check vq->num_free instead.
- rq->num was required to be increased or decreased explicitly after each get/put which results a bad API.

Quoted 6 lines
Quoted 1 line

So this patch removes them both to make the code simpler.

Nice. These fields date from when the vq struct was opaque.

Applied,
Rusty.

Oops, this doesn't require any core virtio changes, so it's for DaveM:

Thanks,
Rusty.

Quoted 88 lines
Quoted 83 lines
Cc Rusty Russell <[email protected]>
Cc Michael S. Tsirkin <[email protected]>
---
 drivers/net/virtio_net.c | 16 +++-------------
 1 file changed, 3 insertions(+), 13 deletions(-)
drivers/net/virtio_net.c +3 -13
index c51a988..4e1bce3 100644
@@ -72,9 +72,6 @@ struct receive_queue {
 
 	struct napi_struct napi;
 
-	/* Number of input buffers, and max we've ever had. */
-	unsigned int num, max;
-
 	/* Chain pages by the private ptr. */
 	struct page *pages;
 
@@ -360,7 +357,6 @@ static struct sk_buff *receive_mergeable(struct net_device *dev,
 		}
 
 		page = virt_to_head_page(buf);
-		--rq->num;
 
 		num_skb_frags = skb_shinfo(curr_skb)->nr_frags;
 		if (unlikely(num_skb_frags == MAX_SKB_FRAGS)) {
@@ -406,7 +402,6 @@ err_skb:
 		}
 		page = virt_to_head_page(buf);
 		put_page(page);
-		--rq->num;
 	}
 err_buf:
 	dev->stats.rx_dropped++;
@@ -628,10 +623,7 @@ static bool try_fill_recv(struct receive_queue *rq, gfp_t gfp)
 		oom = err == -ENOMEM;
 		if (err)
 			break;
-		++rq->num;
 	} while (rq->vq->num_free);
-	if (unlikely(rq->num > rq->max))
-		rq->max = rq->num;
 	if (unlikely(!virtqueue_kick(rq->vq)))
 		return false;
 	return !oom;
@@ -699,11 +691,10 @@ again:
 	while (received < budget &&
 	       (buf = virtqueue_get_buf(rq->vq, &len)) != NULL) {
 		receive_buf(rq, buf, len);
-		--rq->num;
 		received++;
 	}
 
-	if (rq->num < rq->max / 2) {
+	if (rq->vq->num_free > virtqueue_get_vring_size(rq->vq) / 2) {
 		if (!try_fill_recv(rq, GFP_ATOMIC))
 			schedule_delayed_work(&vi->refill, 0);
 	}
@@ -1398,9 +1389,7 @@ static void free_unused_bufs(struct virtnet_info *vi)
 				give_pages(&vi->rq[i], buf);
 			else
 				dev_kfree_skb(buf);
-			--vi->rq[i].num;
 		}
-		BUG_ON(vi->rq[i].num != 0);
 	}
 }
 
@@ -1671,7 +1660,8 @@ static int virtnet_probe(struct virtio_device *vdev)
 		try_fill_recv(&vi->rq[i], GFP_KERNEL);
 
 		/* If we didn't even get one input buffer, we're useless. */
-		if (vi->rq[i].num == 0) {
+		if (vi->rq[i].vq->num_free ==
+		    virtqueue_get_vring_size(vi->rq[i].vq)) {
 			free_unused_bufs(vi);
 			err = -ENOMEM;
 			goto free_recv_bufs;
Signature · 1 line
1.8.3.2

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

maintainer
#4 lore raw

From: Rusty Russell <[email protected]>
Date: Thu, 16 Jan 2014 10:25:26 +1030

Quoted 13 lines
Quoted 12 lines · Rusty Russell
Quoted 10 lines · Jason Wang

It looks like there's no need for those two fields:

- Unless there's a failure for the first refill try, rq->max should be always equal to the vring size.
- rq->num is only used to determine the condition that we need to do the refill, we could check vq->num_free instead.
- rq->num was required to be increased or decreased explicitly after each get/put which results a bad API.

So this patch removes them both to make the code simpler.

Quoted 8 lines
Quoted 4 lines

Nice. These fields date from when the vq struct was opaque.

Applied,
Rusty.

Oops, this doesn't require any core virtio changes, so it's for DaveM:

Jason please repost this with Rusty's ACK, thanks.

author
#5 lore raw
Quoted 15 lines · David Miller

From: Rusty Russell<[email protected]>
Date: Thu, 16 Jan 2014 10:25:26 +1030

Quoted 12 lines
Quoted 11 lines · Rusty Russell
Quoted 10 lines · Jason Wang

It looks like there's no need for those two fields:

- Unless there's a failure for the first refill try, rq->max should be always equal to the vring size.
- rq->num is only used to determine the condition that we need to do the refill, we could check vq->num_free instead.
- rq->num was required to be increased or decreased explicitly after each get/put which results a bad API.

So this patch removes them both to make the code simpler.

Quoted 8 lines
Quoted 7 lines
Quoted 4 lines

Nice. These fields date from when the vq struct was opaque.

Applied,
Rusty.

Oops, this doesn't require any core virtio changes, so it's for DaveM:

Jason please repost this with Rusty's ACK, thanks.

Sure, will repost.