This is an automated email from the ASF dual-hosted git repository.
GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git
The following commit(s) were added to refs/heads/master by this push:
new eae59c74257 drivers/virtio/virtio-blk: Wait for free descriptors
before a request is added.
eae59c74257 is described below
commit eae59c74257a882e3a73c9dfca0c758b7b4a64a1
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 16 23:34:28 2026 +0100
drivers/virtio/virtio-blk: Wait for free descriptors before a request is
added.
virtqueue_add_buffer() of OpenAMP checks the number of free descriptors
only when VQUEUE_DEBUG is set (CONFIG_OPENAMP_DEBUG). Without it, a
request that needs more descriptors than the queue has free walks past
the end of the free list: it writes descriptors that other requests
still use, and the free count wraps. The device then reads a broken
chain and stops, and with it the block device.
A read or write uses 3 descriptors and a flush 2, and nothing limits
the number of requests in flight: each open of /dev/virtblkN has its
own block-to-character driver, and file systems on other partitions
send their own requests. With 16 descriptors, 6 concurrent requests
fill the queue.
Before a request is added, wait until the queue has enough free
descriptors: virtio_blk_done() wakes the waiting requests after the
replies gave descriptors back. Only a cancellation ends the wait early,
and the request then fails with that error. In an interrupt handler or
after a panic, where the request polls for its reply, poll for the
finished requests instead of a wait. The same in flat, protected and
kernel builds.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
drivers/virtio/virtio-blk.c | 83 +++++++++++++++++++++++++++++++++++++++++++--
1 file changed, 81 insertions(+), 2 deletions(-)
diff --git a/drivers/virtio/virtio-blk.c b/drivers/virtio/virtio-blk.c
index a3413ccbd96..c252c95e8c8 100644
--- a/drivers/virtio/virtio-blk.c
+++ b/drivers/virtio/virtio-blk.c
@@ -47,6 +47,8 @@ struct virtio_blk_priv_s
{
FAR struct virtio_device *vdev; /* Virtio device */
spinlock_t lock; /* Lock */
+ sem_t waitsem; /* Wait for descriptors */
+ unsigned int nwait; /* Number of the waiters */
uint64_t nsectors; /* Sectore numbers */
uint32_t block_size; /* Block size */
char name[NAME_MAX]; /* Device name */
@@ -151,6 +153,56 @@ static void virtio_blk_wait_complete(FAR struct virtqueue
*vq,
}
}
+/****************************************************************************
+ * Name: virtio_blk_wait_desc
+ *
+ * Description:
+ * virtqueue_add_buffer() does not check the number of free descriptors:
+ * wait until the queue has enough of them. Called with priv->lock held,
+ * returns with it held.
+ *
+ ****************************************************************************/
+
+static int virtio_blk_wait_desc(FAR struct virtio_blk_priv_s *priv,
+ FAR struct virtqueue *vq, int needed,
+ FAR irqstate_t *flags)
+{
+ FAR sem_t *sem;
+ int ret;
+
+ while (vq->vq_free_cnt < needed)
+ {
+ if (up_interrupt_context() || OSINIT_IS_PANIC())
+ {
+ /* No wait here: complete the finished requests by polling */
+
+ sem = virtqueue_get_buffer(vq, NULL, NULL);
+ if (sem != NULL)
+ {
+ nxsem_post(sem);
+ }
+
+ for (; priv->nwait > 0; priv->nwait--)
+ {
+ nxsem_post(&priv->waitsem);
+ }
+
+ continue;
+ }
+
+ priv->nwait++;
+ spin_unlock_irqrestore(&priv->lock, *flags);
+ ret = nxsem_wait_uninterruptible(&priv->waitsem);
+ *flags = spin_lock_irqsave(&priv->lock);
+ if (ret < 0)
+ {
+ return ret;
+ }
+ }
+
+ return OK;
+}
+
/****************************************************************************
* Name: virtio_blk_rdwr_one
*
@@ -256,7 +308,12 @@ static ssize_t virtio_blk_rdwr_one(FAR struct
virtio_blk_priv_s *priv,
virtqueue_disable_cb(vq);
}
- ret = virtqueue_add_buffer(vq, vb, readnum, 3 - readnum, &respsem);
+ ret = virtio_blk_wait_desc(priv, vq, nitems(vb), &flags);
+ if (ret >= 0)
+ {
+ ret = virtqueue_add_buffer(vq, vb, readnum, 3 - readnum, &respsem);
+ }
+
if (ret < 0)
{
if (up_interrupt_context())
@@ -500,7 +557,12 @@ static int virtio_blk_flush(FAR struct virtio_blk_priv_s
*priv)
vb[1].len = VIRTIO_BLK_RESP_HEADER_SIZE;
flags = spin_lock_irqsave(&priv->lock);
- ret = virtqueue_add_buffer(vq, vb, 1, 1, &respsem);
+ ret = virtio_blk_wait_desc(priv, vq, nitems(vb), &flags);
+ if (ret >= 0)
+ {
+ ret = virtqueue_add_buffer(vq, vb, 1, 1, &respsem);
+ }
+
if (ret < 0)
{
spin_unlock_irqrestore(&priv->lock, flags);
@@ -568,6 +630,8 @@ static void virtio_blk_done(FAR struct virtqueue *vq)
{
FAR struct virtio_blk_priv_s *priv = vq->vq_dev->priv;
FAR sem_t *respsem;
+ irqstate_t flags;
+ unsigned int nwait;
for (; ; )
{
@@ -579,6 +643,18 @@ static void virtio_blk_done(FAR struct virtqueue *vq)
nxsem_post(respsem);
}
+
+ /* The replies gave descriptors back: the waiting requests try again */
+
+ flags = spin_lock_irqsave(&priv->lock);
+ nwait = priv->nwait;
+ priv->nwait = 0;
+ spin_unlock_irqrestore(&priv->lock, flags);
+
+ while (nwait-- > 0)
+ {
+ nxsem_post(&priv->waitsem);
+ }
}
/****************************************************************************
@@ -595,6 +671,7 @@ static int virtio_blk_init(FAR struct virtio_blk_priv_s
*priv,
priv->vdev = vdev;
vdev->priv = priv;
spin_lock_init(&priv->lock);
+ nxsem_init(&priv->waitsem, 0, 0);
/* Initialize the virtio device */
@@ -690,6 +767,7 @@ static int virtio_blk_probe(FAR struct virtio_device *vdev)
err_with_init:
virtio_blk_uninit(priv);
err_with_priv:
+ nxsem_destroy(&priv->waitsem);
kmm_free(priv);
return ret;
}
@@ -723,6 +801,7 @@ static void virtio_blk_remove(FAR struct virtio_device
*vdev)
unregister_driver(priv->name);
virtio_blk_uninit(priv);
+ nxsem_destroy(&priv->waitsem);
kmm_free(priv);
}