Skip to content

Commit 7b3384f

Browse files
committed
vhost: add unlikely annotations to error path
patch 'break out of polling loop on error' caused a minor performance regression on my machine: recover that performance by adding a bunch of unlikely annotations in the error handling. Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent d5675bd commit 7b3384f

2 files changed

Lines changed: 29 additions & 28 deletions

File tree

‎drivers/vhost/net.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static void handle_tx(struct vhost_net *net)
137137
&out, &in,
138138
NULL, NULL);
139139
/* On error, stop handling until the next kick. */
140-
if (head < 0)
140+
if (unlikely(head < 0))
141141
break;
142142
/* Nothing new? Wait for eventfd to tell us they refilled. */
143143
if (head == vq->num) {
@@ -234,7 +234,7 @@ static void handle_rx(struct vhost_net *net)
234234
&out, &in,
235235
vq_log, &log);
236236
/* On error, stop handling until the next kick. */
237-
if (head < 0)
237+
if (unlikely(head < 0))
238238
break;
239239
/* OK, now we need to know about added descriptors. */
240240
if (head == vq->num) {

‎drivers/vhost/vhost.c‎

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -736,12 +736,12 @@ static int translate_desc(struct vhost_dev *dev, u64 addr, u32 len,
736736
mem = rcu_dereference(dev->memory);
737737
while ((u64)len > s) {
738738
u64 size;
739-
if (ret >= iov_size) {
739+
if (unlikely(ret >= iov_size)) {
740740
ret = -ENOBUFS;
741741
break;
742742
}
743743
reg = find_region(mem, addr, len);
744-
if (!reg) {
744+
if (unlikely(!reg)) {
745745
ret = -EFAULT;
746746
break;
747747
}
@@ -780,18 +780,18 @@ static unsigned next_desc(struct vring_desc *desc)
780780
return next;
781781
}
782782

783-
static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
784-
struct iovec iov[], unsigned int iov_size,
785-
unsigned int *out_num, unsigned int *in_num,
786-
struct vhost_log *log, unsigned int *log_num,
787-
struct vring_desc *indirect)
783+
static int get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
784+
struct iovec iov[], unsigned int iov_size,
785+
unsigned int *out_num, unsigned int *in_num,
786+
struct vhost_log *log, unsigned int *log_num,
787+
struct vring_desc *indirect)
788788
{
789789
struct vring_desc desc;
790790
unsigned int i = 0, count, found = 0;
791791
int ret;
792792

793793
/* Sanity check */
794-
if (indirect->len % sizeof desc) {
794+
if (unlikely(indirect->len % sizeof desc)) {
795795
vq_err(vq, "Invalid length in indirect descriptor: "
796796
"len 0x%llx not multiple of 0x%zx\n",
797797
(unsigned long long)indirect->len,
@@ -801,7 +801,7 @@ static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
801801

802802
ret = translate_desc(dev, indirect->addr, indirect->len, vq->indirect,
803803
ARRAY_SIZE(vq->indirect));
804-
if (ret < 0) {
804+
if (unlikely(ret < 0)) {
805805
vq_err(vq, "Translation failure %d in indirect.\n", ret);
806806
return ret;
807807
}
@@ -813,35 +813,35 @@ static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
813813
count = indirect->len / sizeof desc;
814814
/* Buffers are chained via a 16 bit next field, so
815815
* we can have at most 2^16 of these. */
816-
if (count > USHRT_MAX + 1) {
816+
if (unlikely(count > USHRT_MAX + 1)) {
817817
vq_err(vq, "Indirect buffer length too big: %d\n",
818818
indirect->len);
819819
return -E2BIG;
820820
}
821821

822822
do {
823823
unsigned iov_count = *in_num + *out_num;
824-
if (++found > count) {
824+
if (unlikely(++found > count)) {
825825
vq_err(vq, "Loop detected: last one at %u "
826826
"indirect size %u\n",
827827
i, count);
828828
return -EINVAL;
829829
}
830-
if (memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
831-
sizeof desc)) {
830+
if (unlikely(memcpy_fromiovec((unsigned char *)&desc, vq->indirect,
831+
sizeof desc))) {
832832
vq_err(vq, "Failed indirect descriptor: idx %d, %zx\n",
833833
i, (size_t)indirect->addr + i * sizeof desc);
834834
return -EINVAL;
835835
}
836-
if (desc.flags & VRING_DESC_F_INDIRECT) {
836+
if (unlikely(desc.flags & VRING_DESC_F_INDIRECT)) {
837837
vq_err(vq, "Nested indirect descriptor: idx %d, %zx\n",
838838
i, (size_t)indirect->addr + i * sizeof desc);
839839
return -EINVAL;
840840
}
841841

842842
ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
843843
iov_size - iov_count);
844-
if (ret < 0) {
844+
if (unlikely(ret < 0)) {
845845
vq_err(vq, "Translation failure %d indirect idx %d\n",
846846
ret, i);
847847
return ret;
@@ -857,7 +857,7 @@ static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
857857
} else {
858858
/* If it's an output descriptor, they're all supposed
859859
* to come before any input descriptors. */
860-
if (*in_num) {
860+
if (unlikely(*in_num)) {
861861
vq_err(vq, "Indirect descriptor "
862862
"has out after in: idx %d\n", i);
863863
return -EINVAL;
@@ -888,13 +888,13 @@ int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
888888

889889
/* Check it isn't doing very strange things with descriptor numbers. */
890890
last_avail_idx = vq->last_avail_idx;
891-
if (get_user(vq->avail_idx, &vq->avail->idx)) {
891+
if (unlikely(get_user(vq->avail_idx, &vq->avail->idx))) {
892892
vq_err(vq, "Failed to access avail idx at %p\n",
893893
&vq->avail->idx);
894894
return -EFAULT;
895895
}
896896

897-
if ((u16)(vq->avail_idx - last_avail_idx) > vq->num) {
897+
if (unlikely((u16)(vq->avail_idx - last_avail_idx) > vq->num)) {
898898
vq_err(vq, "Guest moved used index from %u to %u",
899899
last_avail_idx, vq->avail_idx);
900900
return -EFAULT;
@@ -909,15 +909,16 @@ int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
909909

910910
/* Grab the next descriptor number they're advertising, and increment
911911
* the index we've seen. */
912-
if (get_user(head, &vq->avail->ring[last_avail_idx % vq->num])) {
912+
if (unlikely(get_user(head,
913+
&vq->avail->ring[last_avail_idx % vq->num]))) {
913914
vq_err(vq, "Failed to read head: idx %d address %p\n",
914915
last_avail_idx,
915916
&vq->avail->ring[last_avail_idx % vq->num]);
916917
return -EFAULT;
917918
}
918919

919920
/* If their number is silly, that's an error. */
920-
if (head >= vq->num) {
921+
if (unlikely(head >= vq->num)) {
921922
vq_err(vq, "Guest says index %u > %u is available",
922923
head, vq->num);
923924
return -EINVAL;
@@ -931,19 +932,19 @@ int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
931932
i = head;
932933
do {
933934
unsigned iov_count = *in_num + *out_num;
934-
if (i >= vq->num) {
935+
if (unlikely(i >= vq->num)) {
935936
vq_err(vq, "Desc index is %u > %u, head = %u",
936937
i, vq->num, head);
937938
return -EINVAL;
938939
}
939-
if (++found > vq->num) {
940+
if (unlikely(++found > vq->num)) {
940941
vq_err(vq, "Loop detected: last one at %u "
941942
"vq size %u head %u\n",
942943
i, vq->num, head);
943944
return -EINVAL;
944945
}
945946
ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
946-
if (ret) {
947+
if (unlikely(ret)) {
947948
vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
948949
i, vq->desc + i);
949950
return -EFAULT;
@@ -952,7 +953,7 @@ int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
952953
ret = get_indirect(dev, vq, iov, iov_size,
953954
out_num, in_num,
954955
log, log_num, &desc);
955-
if (ret < 0) {
956+
if (unlikely(ret < 0)) {
956957
vq_err(vq, "Failure detected "
957958
"in indirect descriptor at idx %d\n", i);
958959
return ret;
@@ -962,7 +963,7 @@ int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
962963

963964
ret = translate_desc(dev, desc.addr, desc.len, iov + iov_count,
964965
iov_size - iov_count);
965-
if (ret < 0) {
966+
if (unlikely(ret < 0)) {
966967
vq_err(vq, "Translation failure %d descriptor idx %d\n",
967968
ret, i);
968969
return ret;
@@ -979,7 +980,7 @@ int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
979980
} else {
980981
/* If it's an output descriptor, they're all supposed
981982
* to come before any input descriptors. */
982-
if (*in_num) {
983+
if (unlikely(*in_num)) {
983984
vq_err(vq, "Descriptor has out after in: "
984985
"idx %d\n", i);
985986
return -EINVAL;

0 commit comments

Comments
 (0)