Skip to content

Commit d5675bd

Browse files
committed
vhost: break out of polling loop on error
When ring parsing fails, we currently handle this as ring empty condition. This means that we enable kicks and recheck ring empty: if this not empty, we re-start polling which of course will fail again. Instead, let's return a negative error code and stop polling. Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent 38000a9 commit d5675bd

3 files changed

Lines changed: 31 additions & 22 deletions

File tree

‎drivers/vhost/net.c‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ static void tx_poll_start(struct vhost_net *net, struct socket *sock)
9898
static void handle_tx(struct vhost_net *net)
9999
{
100100
struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_TX];
101-
unsigned head, out, in, s;
101+
unsigned out, in, s;
102+
int head;
102103
struct msghdr msg = {
103104
.msg_name = NULL,
104105
.msg_namelen = 0,
@@ -135,6 +136,9 @@ static void handle_tx(struct vhost_net *net)
135136
ARRAY_SIZE(vq->iov),
136137
&out, &in,
137138
NULL, NULL);
139+
/* On error, stop handling until the next kick. */
140+
if (head < 0)
141+
break;
138142
/* Nothing new? Wait for eventfd to tell us they refilled. */
139143
if (head == vq->num) {
140144
wmem = atomic_read(&sock->sk->sk_wmem_alloc);
@@ -192,7 +196,8 @@ static void handle_tx(struct vhost_net *net)
192196
static void handle_rx(struct vhost_net *net)
193197
{
194198
struct vhost_virtqueue *vq = &net->dev.vqs[VHOST_NET_VQ_RX];
195-
unsigned head, out, in, log, s;
199+
unsigned out, in, log, s;
200+
int head;
196201
struct vhost_log *vq_log;
197202
struct msghdr msg = {
198203
.msg_name = NULL,
@@ -228,6 +233,9 @@ static void handle_rx(struct vhost_net *net)
228233
ARRAY_SIZE(vq->iov),
229234
&out, &in,
230235
vq_log, &log);
236+
/* On error, stop handling until the next kick. */
237+
if (head < 0)
238+
break;
231239
/* OK, now we need to know about added descriptors. */
232240
if (head == vq->num) {
233241
if (unlikely(vhost_enable_notify(vq))) {

‎drivers/vhost/vhost.c‎

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -873,12 +873,13 @@ static unsigned get_indirect(struct vhost_dev *dev, struct vhost_virtqueue *vq,
873873
* number of output then some number of input descriptors, it's actually two
874874
* iovecs, but we pack them into one and note how many of each there were.
875875
*
876-
* This function returns the descriptor number found, or vq->num (which
877-
* is never a valid descriptor number) if none was found. */
878-
unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
879-
struct iovec iov[], unsigned int iov_size,
880-
unsigned int *out_num, unsigned int *in_num,
881-
struct vhost_log *log, unsigned int *log_num)
876+
* This function returns the descriptor number found, or vq->num (which is
877+
* never a valid descriptor number) if none was found. A negative code is
878+
* returned on error. */
879+
int vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
880+
struct iovec iov[], unsigned int iov_size,
881+
unsigned int *out_num, unsigned int *in_num,
882+
struct vhost_log *log, unsigned int *log_num)
882883
{
883884
struct vring_desc desc;
884885
unsigned int i, head, found = 0;
@@ -890,13 +891,13 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
890891
if (get_user(vq->avail_idx, &vq->avail->idx)) {
891892
vq_err(vq, "Failed to access avail idx at %p\n",
892893
&vq->avail->idx);
893-
return vq->num;
894+
return -EFAULT;
894895
}
895896

896897
if ((u16)(vq->avail_idx - last_avail_idx) > vq->num) {
897898
vq_err(vq, "Guest moved used index from %u to %u",
898899
last_avail_idx, vq->avail_idx);
899-
return vq->num;
900+
return -EFAULT;
900901
}
901902

902903
/* If there's nothing new since last we looked, return invalid. */
@@ -912,14 +913,14 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
912913
vq_err(vq, "Failed to read head: idx %d address %p\n",
913914
last_avail_idx,
914915
&vq->avail->ring[last_avail_idx % vq->num]);
915-
return vq->num;
916+
return -EFAULT;
916917
}
917918

918919
/* If their number is silly, that's an error. */
919920
if (head >= vq->num) {
920921
vq_err(vq, "Guest says index %u > %u is available",
921922
head, vq->num);
922-
return vq->num;
923+
return -EINVAL;
923924
}
924925

925926
/* When we start there are none of either input nor output. */
@@ -933,19 +934,19 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
933934
if (i >= vq->num) {
934935
vq_err(vq, "Desc index is %u > %u, head = %u",
935936
i, vq->num, head);
936-
return vq->num;
937+
return -EINVAL;
937938
}
938939
if (++found > vq->num) {
939940
vq_err(vq, "Loop detected: last one at %u "
940941
"vq size %u head %u\n",
941942
i, vq->num, head);
942-
return vq->num;
943+
return -EINVAL;
943944
}
944945
ret = copy_from_user(&desc, vq->desc + i, sizeof desc);
945946
if (ret) {
946947
vq_err(vq, "Failed to get descriptor: idx %d addr %p\n",
947948
i, vq->desc + i);
948-
return vq->num;
949+
return -EFAULT;
949950
}
950951
if (desc.flags & VRING_DESC_F_INDIRECT) {
951952
ret = get_indirect(dev, vq, iov, iov_size,
@@ -954,7 +955,7 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
954955
if (ret < 0) {
955956
vq_err(vq, "Failure detected "
956957
"in indirect descriptor at idx %d\n", i);
957-
return vq->num;
958+
return ret;
958959
}
959960
continue;
960961
}
@@ -964,7 +965,7 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
964965
if (ret < 0) {
965966
vq_err(vq, "Translation failure %d descriptor idx %d\n",
966967
ret, i);
967-
return vq->num;
968+
return ret;
968969
}
969970
if (desc.flags & VRING_DESC_F_WRITE) {
970971
/* If this is an input descriptor,
@@ -981,7 +982,7 @@ unsigned vhost_get_vq_desc(struct vhost_dev *dev, struct vhost_virtqueue *vq,
981982
if (*in_num) {
982983
vq_err(vq, "Descriptor has out after in: "
983984
"idx %d\n", i);
984-
return vq->num;
985+
return -EINVAL;
985986
}
986987
*out_num += ret;
987988
}

‎drivers/vhost/vhost.h‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,10 @@ long vhost_dev_ioctl(struct vhost_dev *, unsigned int ioctl, unsigned long arg);
120120
int vhost_vq_access_ok(struct vhost_virtqueue *vq);
121121
int vhost_log_access_ok(struct vhost_dev *);
122122

123-
unsigned vhost_get_vq_desc(struct vhost_dev *, struct vhost_virtqueue *,
124-
struct iovec iov[], unsigned int iov_count,
125-
unsigned int *out_num, unsigned int *in_num,
126-
struct vhost_log *log, unsigned int *log_num);
123+
int vhost_get_vq_desc(struct vhost_dev *, struct vhost_virtqueue *,
124+
struct iovec iov[], unsigned int iov_count,
125+
unsigned int *out_num, unsigned int *in_num,
126+
struct vhost_log *log, unsigned int *log_num);
127127
void vhost_discard_vq_desc(struct vhost_virtqueue *);
128128

129129
int vhost_add_used(struct vhost_virtqueue *, unsigned int head, int len);

0 commit comments

Comments
 (0)