This is an automated email from the ASF dual-hosted git repository.
xiaoxiang781216 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 eebc303e73b fs/v9fs/virtio_9p: Wait for free descriptors before a
request is added.
eebc303e73b is described below
commit eebc303e73b9fcb122167af1b6bd53f556375302
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 16 22:45:46 2026 +0100
fs/v9fs/virtio_9p: 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 descriptor entries far past the end of the table (index
VQ_RING_DESC_CHAIN_END), and the free count wraps. The device then
reads a broken chain and stops (QEMU: "Desc next is 32768"), and with
it the file system.
With CONFIG_ARCH_ADDRENV, a user buffer needs one descriptor per page
that is not physically next to the one before. A 64 KiB read or write
can need 17 descriptors, so 7 such requests at the same time fill a
queue of 128 descriptors (the 9p device of QEMU over virtio-pci).
Threads that read large files at the same time do this.
Before the request is added, wait until the queue has enough free
descriptors: virtio_9p_done() wakes the waiting requests after the
replies of other requests gave descriptors back. Only a cancellation
ends the wait early (the device has nothing of the request yet), and
the request then fails with that error. A request that needs more
descriptors than the queue has gets -E2BIG. The same in flat,
protected and kernel builds.
Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Marco Casaroli <[email protected]>
---
fs/v9fs/virtio_9p.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 46 insertions(+)
diff --git a/fs/v9fs/virtio_9p.c b/fs/v9fs/virtio_9p.c
index a4e18996abb..f6c5834e885 100644
--- a/fs/v9fs/virtio_9p.c
+++ b/fs/v9fs/virtio_9p.c
@@ -28,6 +28,7 @@
#include <errno.h>
#include <nuttx/nuttx.h>
+#include <nuttx/semaphore.h>
#include <nuttx/spinlock.h>
#ifdef CONFIG_ARCH_ADDRENV
# include <nuttx/arch.h>
@@ -60,6 +61,8 @@ struct virtio_9p_priv_s
struct v9fs_transport_s transport;
struct virtio_driver vdrv;
spinlock_t lock;
+ sem_t waitsem; /* Requests that wait for descriptors */
+ unsigned int nwait; /* The number of these requests */
char tag[0];
};
@@ -185,6 +188,7 @@ static int virtio_9p_create(FAR struct v9fs_transport_s
**transport,
}
spin_lock_init(&priv->lock);
+ nxsem_init(&priv->waitsem, 0, 0);
memcpy(priv->tag, start, length);
priv->vdrv.device = VIRTIO_ID_9P;
priv->vdrv.probe = virtio_9p_probe;
@@ -202,6 +206,7 @@ static int virtio_9p_create(FAR struct v9fs_transport_s
**transport,
if (ret < 0)
{
+ nxsem_destroy(&priv->waitsem);
fs_heap_free(priv);
}
@@ -218,6 +223,7 @@ static void virtio_9p_destroy(FAR struct v9fs_transport_s
*transport)
container_of(transport, struct virtio_9p_priv_s, transport);
virtio_unregister_driver(&priv->vdrv);
+ nxsem_destroy(&priv->waitsem);
fs_heap_free(priv);
}
@@ -285,7 +291,33 @@ static int virtio_9p_request(FAR struct v9fs_transport_s
*transport,
}
#endif
+ /* virtqueue_add_buffer() does not check the number of free descriptors:
+ * wait until the replies of other requests give enough of them back.
+ */
+
+ if (wcount + rcount > vq->vq_nentries)
+ {
+ vrterr("request of %zu descriptors, queue of %u\n",
+ wcount + rcount, vq->vq_nentries);
+ return -E2BIG;
+ }
+
flags = spin_lock_irqsave(&priv->lock);
+ while (vq->vq_free_cnt < wcount + rcount)
+ {
+ priv->nwait++;
+ spin_unlock_irqrestore(&priv->lock, flags);
+ ret = nxsem_wait_uninterruptible(&priv->waitsem);
+ if (ret < 0)
+ {
+ /* A cancellation: the device has nothing of this request yet */
+
+ return ret;
+ }
+
+ flags = spin_lock_irqsave(&priv->lock);
+ }
+
ret = virtqueue_add_buffer(vq, vb, wcount, rcount, payload);
if (ret < 0)
{
@@ -307,6 +339,8 @@ static void virtio_9p_done(FAR struct virtqueue *vq)
{
FAR struct virtio_9p_priv_s *priv = vq->vq_dev->priv;
FAR struct v9fs_payload_s *payload;
+ irqstate_t flags;
+ unsigned int nwait;
for (; ; )
{
@@ -318,6 +352,18 @@ static void virtio_9p_done(FAR struct virtqueue *vq)
v9fs_transport_done(payload, 0);
}
+
+ /* 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);
+ }
}
/****************************************************************************