[Devel] [PATCH hci-8.2 v4 1/4] drivers/vhost: split the descriptor chain walk out of vhost_get_vq_desc()

Alexander Ivanov alexander.ivanov at virtuozzo.com
Tue Dec 1 15:49:28 MSK 2026


vhost-blk is to hold a request failed with ENOSPC instead of completing
it, and resubmit it later by its head when QEMU asks. That head was
already taken from the avail ring, so vhost_get_vq_desc() cannot parse
it again.

Move the walk over the chain of a given head into
vhost_get_desc_chain(), and let vhost_get_vq_desc() call it for the
head it takes from the avail ring.

https://virtuozzo.atlassian.net/browse/VSTOR-152175
Feature: vhost-blk: in-kernel accelerator for virtio-blk guests
Signed-off-by: Alexander Ivanov <alexander.ivanov at virtuozzo.com>
Reviewed-by: Sashiko <no-reply at sashiko.virtuozzo.com>
---
 drivers/vhost/vhost.c | 91 ++++++++++++++++++++++++++-----------------
 drivers/vhost/vhost.h |  4 ++
 2 files changed, 60 insertions(+), 35 deletions(-)

diff --git a/drivers/vhost/vhost.c b/drivers/vhost/vhost.c
index 56c0ecbb8352..922e588938b2 100644
--- a/drivers/vhost/vhost.c
+++ b/drivers/vhost/vhost.c
@@ -2783,45 +2783,19 @@ static int get_indirect(struct vhost_virtqueue *vq,
 	return 0;
 }
 
-/* This looks in the virtqueue and for the first available buffer, and converts
- * it to an iovec for convenient access.  Since descriptors consist of some
- * number of output then some number of input descriptors, it's actually two
- * iovecs, but we pack them into one and note how many of each there were.
- *
- * This function returns the descriptor number found, or vq->num (which is
- * never a valid descriptor number) if none was found.  A negative code is
- * returned on error. */
-int vhost_get_vq_desc(struct vhost_virtqueue *vq,
-		      struct iovec iov[], unsigned int iov_size,
-		      unsigned int *out_num, unsigned int *in_num,
-		      struct vhost_log *log, unsigned int *log_num)
+/*
+ * Convert the descriptor chain of @head to an iovec without consuming anything
+ * from the avail ring. Call with vq->mutex held.
+ */
+int vhost_get_desc_chain(struct vhost_virtqueue *vq, unsigned int head,
+			 struct iovec iov[], unsigned int iov_size,
+			 unsigned int *out_num, unsigned int *in_num,
+			 struct vhost_log *log, unsigned int *log_num)
 {
 	struct vring_desc desc;
-	unsigned int i, head, found = 0;
-	u16 last_avail_idx = vq->last_avail_idx;
-	__virtio16 ring_head;
+	unsigned int i, found = 0;
 	int ret, access;
 
-	if (vq->avail_idx == vq->last_avail_idx) {
-		ret = vhost_get_avail_idx(vq);
-		if (unlikely(ret < 0))
-			return ret;
-
-		if (!ret)
-			return vq->num;
-	}
-
-	/* Grab the next descriptor number they're advertising, and increment
-	 * the index we've seen. */
-	if (unlikely(vhost_get_avail_head(vq, &ring_head, last_avail_idx))) {
-		vq_err(vq, "Failed to read head: idx %d address %p\n",
-		       last_avail_idx,
-		       &vq->avail->ring[last_avail_idx % vq->num]);
-		return -EFAULT;
-	}
-
-	head = vhost16_to_cpu(vq, ring_head);
-
 	/* If their number is silly, that's an error. */
 	if (unlikely(head >= vq->num)) {
 		vq_err(vq, "Guest says index %u > %u is available",
@@ -2901,6 +2875,53 @@ int vhost_get_vq_desc(struct vhost_virtqueue *vq,
 		}
 	} while ((i = next_desc(vq, &desc)) != -1);
 
+	return 0;
+}
+EXPORT_SYMBOL_GPL(vhost_get_desc_chain);
+
+/* This looks in the virtqueue and for the first available buffer, and converts
+ * it to an iovec for convenient access.  Since descriptors consist of some
+ * number of output then some number of input descriptors, it's actually two
+ * iovecs, but we pack them into one and note how many of each there were.
+ *
+ * This function returns the descriptor number found, or vq->num (which is
+ * never a valid descriptor number) if none was found.  A negative code is
+ * returned on error. */
+int vhost_get_vq_desc(struct vhost_virtqueue *vq,
+		      struct iovec iov[], unsigned int iov_size,
+		      unsigned int *out_num, unsigned int *in_num,
+		      struct vhost_log *log, unsigned int *log_num)
+{
+	unsigned int head;
+	u16 last_avail_idx = vq->last_avail_idx;
+	__virtio16 ring_head;
+	int ret;
+
+	if (vq->avail_idx == vq->last_avail_idx) {
+		ret = vhost_get_avail_idx(vq);
+		if (unlikely(ret < 0))
+			return ret;
+
+		if (!ret)
+			return vq->num;
+	}
+
+	/* Grab the next descriptor number they're advertising, and increment
+	 * the index we've seen. */
+	if (unlikely(vhost_get_avail_head(vq, &ring_head, last_avail_idx))) {
+		vq_err(vq, "Failed to read head: idx %d address %p\n",
+		       last_avail_idx,
+		       &vq->avail->ring[last_avail_idx % vq->num]);
+		return -EFAULT;
+	}
+
+	head = vhost16_to_cpu(vq, ring_head);
+
+	ret = vhost_get_desc_chain(vq, head, iov, iov_size, out_num, in_num,
+				   log, log_num);
+	if (unlikely(ret))
+		return ret;
+
 	/* On success, increment avail index. */
 	vq->last_avail_idx++;
 
diff --git a/drivers/vhost/vhost.h b/drivers/vhost/vhost.h
index 077fa4d0cf36..88c322d8462c 100644
--- a/drivers/vhost/vhost.h
+++ b/drivers/vhost/vhost.h
@@ -223,6 +223,10 @@ bool vhost_vq_access_ok(struct vhost_virtqueue *vq);
 bool vhost_log_access_ok(struct vhost_dev *);
 void vhost_clear_msg(struct vhost_dev *dev);
 
+int vhost_get_desc_chain(struct vhost_virtqueue *vq, unsigned int head,
+			 struct iovec iov[], unsigned int iov_size,
+			 unsigned int *out_num, unsigned int *in_num,
+			 struct vhost_log *log, unsigned int *log_num);
 int vhost_get_vq_desc(struct vhost_virtqueue *,
 		      struct iovec iov[], unsigned int iov_size,
 		      unsigned int *out_num, unsigned int *in_num,
-- 
2.53.0



More information about the Devel mailing list