virtualization.lists.linux-foundation.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 1/2] virtio: Add a can_add_buf helper
@ 2009-08-19  8:25 Amit Shah
  2009-08-19  8:25 ` [PATCH v2 2/2] virtio_net: Use can_add_buf to test for enough room to add Amit Shah
  0 siblings, 1 reply; 2+ messages in thread
From: Amit Shah @ 2009-08-19  8:25 UTC (permalink / raw)
  To: rusty; +Cc: Amit Shah, virtualization

This helper returns 1 if a call to add_buf will not fail
with -ENOSPC.

This will help callers that do

while(1) {
	alloc()
	if (add_buf()) {
		free();
		break;
	}
}

This will result in one less alloc/free exercise.

Signed-off-by: Amit Shah <amit.shah@redhat.com>
---

v2:
  return true/false instead of 1/0

 drivers/virtio/virtio_ring.c |    8 ++++++++
 include/linux/virtio.h       |    5 +++++
 2 files changed, 13 insertions(+), 0 deletions(-)

diff --git a/drivers/virtio/virtio_ring.c b/drivers/virtio/virtio_ring.c
index a882f26..ea7efe6 100644
--- a/drivers/virtio/virtio_ring.c
+++ b/drivers/virtio/virtio_ring.c
@@ -137,6 +137,13 @@ static int vring_add_indirect(struct vring_virtqueue *vq,
 	return head;
 }
 
+static bool vring_can_add_buf(struct virtqueue *_vq)
+{
+	struct vring_virtqueue *vq = to_vvq(_vq);
+
+	return vq->num_free ? true : false;
+}
+
 static int vring_add_buf(struct virtqueue *_vq,
 			 struct scatterlist sg[],
 			 unsigned int out,
@@ -350,6 +357,7 @@ EXPORT_SYMBOL_GPL(vring_interrupt);
 static struct virtqueue_ops vring_vq_ops = {
 	.add_buf = vring_add_buf,
 	.get_buf = vring_get_buf,
+	.can_add_buf = vring_can_add_buf,
 	.kick = vring_kick,
 	.disable_cb = vring_disable_cb,
 	.enable_cb = vring_enable_cb,
diff --git a/include/linux/virtio.h b/include/linux/virtio.h
index 4fca4f5..cc0e3aa 100644
--- a/include/linux/virtio.h
+++ b/include/linux/virtio.h
@@ -35,6 +35,9 @@ struct virtqueue {
  *	in_num: the number of sg which are writable (after readable ones)
  *	data: the token identifying the buffer.
  *      Returns 0 or an error.
+ * @can_add_buf: indicates whether we have a free slot to add a buffer
+ *      vq: the struct virtqueue we're talking about.
+ *      Returns 0 or 1.
  * @kick: update after add_buf
  *	vq: the struct virtqueue
  *	After one or more add_buf calls, invoke this to kick the other side.
@@ -65,6 +68,8 @@ struct virtqueue_ops {
 		       unsigned int in_num,
 		       void *data);
 
+	bool (*can_add_buf)(struct virtqueue *vq);
+
 	void (*kick)(struct virtqueue *vq);
 
 	void *(*get_buf)(struct virtqueue *vq, unsigned int *len);
-- 
1.6.2.5

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH v2 2/2] virtio_net: Use can_add_buf to test for enough room to add
  2009-08-19  8:25 [PATCH v2 1/2] virtio: Add a can_add_buf helper Amit Shah
@ 2009-08-19  8:25 ` Amit Shah
  0 siblings, 0 replies; 2+ messages in thread
From: Amit Shah @ 2009-08-19  8:25 UTC (permalink / raw)
  To: rusty; +Cc: Amit Shah, virtualization

Use the can_add_buf virtqueue operation to test if
there's room to add another buf to the queue.

Saves us one cycle of alloc-add-free if the queue
was full.

Signed-off-by: Amit Shah <amit.shah@redhat.com>
---
v2:
  convert usage in try_fill_recv() as well

 drivers/net/virtio_net.c |    4 ++--
 1 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c
index 2a6e81d..a93ca07 100644
--- a/drivers/net/virtio_net.c
+++ b/drivers/net/virtio_net.c
@@ -280,7 +280,7 @@ static void try_fill_recv_maxbufs(struct virtnet_info *vi)
 	int num, err, i;
 
 	sg_init_table(sg, 2+MAX_SKB_FRAGS);
-	for (;;) {
+	while (vi->rvq->vq_ops->can_add_buf(vi->rvq)) {
 		struct virtio_net_hdr *hdr;
 
 		skb = netdev_alloc_skb(vi->dev, MAX_PACKET_LEN + NET_IP_ALIGN);
@@ -338,7 +338,7 @@ static void try_fill_recv(struct virtnet_info *vi)
 		return;
 	}
 
-	for (;;) {
+	while (vi->rvq->vq_ops->can_add_buf(vi->rvq)) {
 		skb_frag_t *f;
 
 		skb = netdev_alloc_skb(vi->dev, GOOD_COPY_LEN + NET_IP_ALIGN);
-- 
1.6.2.5

^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2009-08-19  8:25 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-08-19  8:25 [PATCH v2 1/2] virtio: Add a can_add_buf helper Amit Shah
2009-08-19  8:25 ` [PATCH v2 2/2] virtio_net: Use can_add_buf to test for enough room to add Amit Shah

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).