Skip to contentSkip to conversation

virtio_ring: remove unnecessary to_vvq call in vring hot path

PATCH In review Xianting Tian 2 participants 3 messages Mar 24, 2022 – Mar 28, 2022 virtio core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio_ring: remove unnecessary to_vvq call in vring hot path

It passes '_vq' to virtqueue_use_indirect(), which still calls to_vvq to get 'vq', let's directly pass 'vq'. It can avoid unnecessary call of to_vvq in hot path.

Other tiny optimization:
Add unlikely to "if (vq->vq.num_free < descs_used).

---
 drivers/virtio/virtio_ring.c | 10 ++++------
 1 file changed, 4 insertions(+), 6 deletions(-)
drivers/virtio/virtio_ring.c +4 -6
index 962f1477b1fa..ab6d5f0cb579 100644
@@ -205,11 +205,9 @@ struct vring_virtqueue {
 
 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
 
-static inline bool virtqueue_use_indirect(struct virtqueue *_vq,
+static inline bool virtqueue_use_indirect(struct vring_virtqueue *vq,
 					  unsigned int total_sg)
 {
-	struct vring_virtqueue *vq = to_vvq(_vq);
-
 	/*
 	 * If the host supports indirect descriptor tables, and we have multiple
 	 * buffers, then go indirect. FIXME: tune this threshold
@@ -507,7 +505,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 
 	head = vq->free_head;
 
-	if (virtqueue_use_indirect(_vq, total_sg))
+	if (virtqueue_use_indirect(vq, total_sg))
 		desc = alloc_indirect_split(_vq, total_sg, gfp);
 	else {
 		desc = NULL;
@@ -527,7 +525,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 		descs_used = total_sg;
 	}
 
-	if (vq->vq.num_free < descs_used) {
+	if (unlikely(vq->vq.num_free < descs_used)) {
 		pr_debug("Can't add buf len %i - avail = %i\n",
 			 descs_used, vq->vq.num_free);
 		/* FIXME: for historical reasons, we force a notify here if
@@ -1194,7 +1192,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
 
 	BUG_ON(total_sg == 0);
 
-	if (virtqueue_use_indirect(_vq, total_sg)) {
+	if (virtqueue_use_indirect(vq, total_sg)) {
 		err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
 						    in_sgs, data, gfp);
 		if (err != -ENOMEM) {
Signature · 1 line
2.17.1
#1 lore raw
Quoted 3 lines · Xianting Tian

It passes '_vq' to virtqueue_use_indirect(), which still calls to_vvq to get 'vq', let's directly pass 'vq'. It can avoid unnecessary call of to_vvq in hot path.

It seems reasonable to me.

Quoted 3 lines

Other tiny optimization:
Add unlikely to "if (vq->vq.num_free < descs_used).

Better to do this change in another patch.

Thanks,
Stefano

Quoted 53 lines

---
drivers/virtio/virtio_ring.c | 10 ++++------

1 file changed, 4 insertions(+), 6 deletions(-)
drivers/virtio/virtio_ring.c +4 -6
index 962f1477b1fa..ab6d5f0cb579 100644
@@ -205,11 +205,9 @@ struct vring_virtqueue {
 
 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
 
-static inline bool virtqueue_use_indirect(struct virtqueue *_vq,
+static inline bool virtqueue_use_indirect(struct vring_virtqueue *vq,
 					  unsigned int total_sg)
 {
-	struct vring_virtqueue *vq = to_vvq(_vq);
-
 	/*
 	 * If the host supports indirect descriptor tables, and we have multiple
 	 * buffers, then go indirect. FIXME: tune this threshold
@@ -507,7 +505,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 
 	head = vq->free_head;
 
-	if (virtqueue_use_indirect(_vq, total_sg))
+	if (virtqueue_use_indirect(vq, total_sg))
 		desc = alloc_indirect_split(_vq, total_sg, gfp);
 	else {
 		desc = NULL;
@@ -527,7 +525,7 @@ static inline int virtqueue_add_split(struct virtqueue *_vq,
 		descs_used = total_sg;
 	}
 
-	if (vq->vq.num_free < descs_used) {
+	if (unlikely(vq->vq.num_free < descs_used)) {
 		pr_debug("Can't add buf len %i - avail = %i\n",
 			 descs_used, vq->vq.num_free);
 		/* FIXME: for historical reasons, we force a notify here if
@@ -1194,7 +1192,7 @@ static inline int virtqueue_add_packed(struct virtqueue *_vq,
 
 	BUG_ON(total_sg == 0);
 
-	if (virtqueue_use_indirect(_vq, total_sg)) {
+	if (virtqueue_use_indirect(vq, total_sg)) {
 		err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
 						    in_sgs, data, gfp);
 		if (err != -ENOMEM) {
Signature · 1 line
2.17.1
#2 lore raw
Quoted 5 lines · Stefano Garzarella
Quoted 3 lines · Xianting Tian

It passes '_vq' to virtqueue_use_indirect(), which still calls to_vvq to get 'vq', let's directly pass 'vq'. It can avoid unnecessary call of to_vvq in hot path.

Quoted 7 lines

It seems reasonable to me.

Quoted 3 lines

Other tiny optimization:
Add unlikely to "if (vq->vq.num_free < descs_used).

Better to do this change in another patch.

Thanks for the reply, I will send the patchset, which contains the two patches, soon.

Quoted 60 lines

Thanks,
Stefano

Quoted 56 lines

---
drivers/virtio/virtio_ring.c | 10 ++++------

1 file changed, 4 insertions(+), 6 deletions(-)
drivers/virtio/virtio_ring.c +2 -4
index 962f1477b1fa..ab6d5f0cb579 100644
@@ -205,11 +205,9 @@ struct vring_virtqueue {
 
 #define to_vvq(_vq) container_of(_vq, struct vring_virtqueue, vq)
 
-static inline bool virtqueue_use_indirect(struct virtqueue *_vq,
+static inline bool virtqueue_use_indirect(struct vring_virtqueue *vq,
                       unsigned int total_sg)
 {
-    struct vring_virtqueue *vq = to_vvq(_vq);
-
     /*
      * If the host supports indirect descriptor tables, and we have multiple
      * buffers, then go indirect. FIXME: tune this threshold
@@ -507,7 +505,7 @@ static inline int virtqueue_add_split(struct 
 virtqueue *_vq,
 
     head = vq->free_head;
 
-    if (virtqueue_use_indirect(_vq, total_sg))
+    if (virtqueue_use_indirect(vq, total_sg))
         desc = alloc_indirect_split(_vq, total_sg, gfp);
     else {

        desc = NULL;

drivers/virtio/virtio_ring.c +1 -1
@@ -527,7 +525,7 @@ static inline int virtqueue_add_split(struct 
 virtqueue *_vq,
         descs_used = total_sg;
     }
 
-    if (vq->vq.num_free < descs_used) {
+    if (unlikely(vq->vq.num_free < descs_used)) {
         pr_debug("Can't add buf len %i - avail = %i\n",
              descs_used, vq->vq.num_free);

        /* FIXME: for historical reasons, we force a notify here if

drivers/virtio/virtio_ring.c +1 -1
@@ -1194,7 +1192,7 @@ static inline int virtqueue_add_packed(struct 
 virtqueue *_vq,
 
     BUG_ON(total_sg == 0);
 
-    if (virtqueue_use_indirect(_vq, total_sg)) {
+    if (virtqueue_use_indirect(vq, total_sg)) {
         err = virtqueue_add_indirect_packed(vq, sgs, total_sg, out_sgs,
                             in_sgs, data, gfp);

        if (err != -ENOMEM) {

Signature · 1 line
2.17.1