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

commit 700bf3bf5996a815ae430350452fde19514a356a
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 16 23:34:30 2026 +0100

    drivers/virtio/virtio-snd: Do not add a buffer to a full queue.
    
    virtqueue_add_buffer() of OpenAMP checks the number of free descriptors
    only when VQUEUE_DEBUG is set (CONFIG_OPENAMP_DEBUG).  Without it, a
    buffer added to a full queue walks past the end of the free list: it
    writes descriptors that the device still owns, and the free count
    wraps.  The device then reads a broken chain and the stream stops.
    
    Each audio buffer uses 2 descriptors, and the number of buffers is set
    by the application (AUDIOIOC_ALLOCBUFFER), not by the queue: the
    period count that AUDIOIOC_GETBUFFERINFO reports is only a hint, and
    virtio_snd_send_pcm() only logs an error when more buffers are queued.
    
    Return -ENOSPC when the queue has not enough free descriptors.  The
    function can run in the completion interrupt (the upper half enqueues
    the next buffer there), so it does not wait: the caller keeps the
    buffer, and AUDIOIOC_ENQUEUEBUFFER returns the error, or the upper half
    tries the buffer again after the next completion.  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-snd.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/drivers/virtio/virtio-snd.c b/drivers/virtio/virtio-snd.c
index 367e291845c..e02853a980c 100644
--- a/drivers/virtio/virtio-snd.c
+++ b/drivers/virtio/virtio-snd.c
@@ -435,7 +435,18 @@ static int virtio_snd_send_pcm(FAR struct virtio_snd_dev_s 
*sdev,
       vb[1].len = sizeof(*(buf->status));
     }
 
+  /* virtqueue_add_buffer() does not check the number of free descriptors.
+   * This can run in the completion interrupt, so do not wait: the caller
+   * keeps the buffer.
+   */
+
   flags = spin_lock_irqsave(&priv->lock);
+  if (vq->vq_free_cnt < nitems(vb))
+    {
+      spin_unlock_irqrestore(&priv->lock, flags);
+      return -ENOSPC;
+    }
+
   virtqueue_add_buffer(vq, vb, 1, 1, buf);
   virtqueue_kick(vq);
   sdev->cache_buffers++;

Reply via email to