This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 67e49fa5c006aecaad84c2396b95d3d7727c61af Author: Marco Casaroli <[email protected]> AuthorDate: Mon Nov 16 23:26:27 2026 +0100 audio: Do not send to the message queues under the spinlock in a thread. audio_sendmsg() calls file_mq_send() for every open while it holds the spinlock of the upper half, with the interrupts off. It runs from the callback of the lower half: in an interrupt handler for some drivers (virtio-snd), in a worker thread for others (the codec drivers, audio_null). In a thread, file_mq_send() waits when the queue of the application is full, and it allocates the message from the heap when the free list is empty. The buffer completions take the same spinlock, so on SMP they spin until the sender continues. In a thread, send to the queues after the spinlock is released, under a new mutex of the upper half. open(), close() and the REGISTERMQ and UNREGISTERMQ commands change the list of opens and their queues under the same mutex, so the list stays valid while the thread sends. In an interrupt handler, file_mq_send() never waits, so the send stays under the spinlock as before. The poll notification stays under the spinlock. The same in flat, protected and kernel builds. Assisted-by: Claude Code:claude-opus-5-5 Signed-off-by: Marco Casaroli <[email protected]> --- audio/audio.c | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/audio/audio.c b/audio/audio.c index aa06a861af4..122991674ae 100644 --- a/audio/audio.c +++ b/audio/audio.c @@ -89,6 +89,7 @@ struct audio_upperhalf_s { struct audio_info_s info; /* Record the last playing audio format */ mutex_t lock; /* Supports mutual exclusion */ + mutex_t mqlock; /* Keeps the opens for a thread that sends */ uint8_t nbuffers; /* Max ap buffers number */ spinlock_t spinlock; /* Supports spin lock */ uint8_t periods; /* Ap buffers number */ @@ -265,11 +266,13 @@ static int audio_open(FAR struct file *filep) } } + nxmutex_lock(&upper->mqlock); flags = spin_lock_irqsave(&upper->spinlock); priv->flink = upper->head; filep->f_priv = priv; upper->head = priv; spin_unlock_irqrestore(&upper->spinlock, flags); + nxmutex_unlock(&upper->mqlock); ret = OK; errout: @@ -309,6 +312,7 @@ static int audio_close(FAR struct file *filep) return ret; } + nxmutex_lock(&upper->mqlock); flags = spin_lock_irqsave(&upper->spinlock); /* find and drop priv from list */ @@ -337,6 +341,7 @@ static int audio_close(FAR struct file *filep) } spin_unlock_irqrestore(&upper->spinlock, flags); + nxmutex_unlock(&upper->mqlock); /* Shut down and free outside the spinlock: both can sleep */ @@ -1220,7 +1225,9 @@ static int audio_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { audinfo("AUDIOIOC_REGISTERMQ\n"); + nxmutex_lock(&upper->mqlock); ret = audio_registermq(priv, arg); + nxmutex_unlock(&upper->mqlock); } break; @@ -1233,7 +1240,9 @@ static int audio_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { audinfo("AUDIOIOC_UNREGISTERMQ\n"); + nxmutex_lock(&upper->mqlock); audio_unregistermq(priv); + nxmutex_unlock(&upper->mqlock); ret = OK; } break; @@ -1573,11 +1582,22 @@ static inline void audio_sendmsg(FAR struct audio_upperhalf_s *upper, FAR struct audio_openpriv_s *priv; pollevent_t eventset; irqstate_t flags; + bool intr = up_interrupt_context(); + + /* In a thread, file_mq_send() can wait for room in a full queue and can + * allocate the message: send under the mutex, not under the spinlock. + * In an interrupt handler it never waits. + */ + + if (!intr) + { + nxmutex_lock(&upper->mqlock); + } flags = spin_lock_irqsave_nopreempt(&upper->spinlock); for (priv = upper->head; priv != NULL; priv = priv->flink) { - if (priv->usermq != NULL) + if (intr && priv->usermq != NULL) { file_mq_send(priv->usermq, (FAR const char *)msg, sizeof(*msg), CONFIG_AUDIO_BUFFER_DEQUEUE_PRIO); @@ -1597,6 +1617,20 @@ static inline void audio_sendmsg(FAR struct audio_upperhalf_s *upper, } spin_unlock_irqrestore_nopreempt(&upper->spinlock, flags); + + if (!intr) + { + for (priv = upper->head; priv != NULL; priv = priv->flink) + { + if (priv->usermq != NULL) + { + file_mq_send(priv->usermq, (FAR const char *)msg, + sizeof(*msg), CONFIG_AUDIO_BUFFER_DEQUEUE_PRIO); + } + } + + nxmutex_unlock(&upper->mqlock); + } } /**************************************************************************** @@ -2037,6 +2071,7 @@ int audio_register(FAR const char *name, FAR struct audio_lowerhalf_s *dev) */ nxmutex_init(&upper->lock); + nxmutex_init(&upper->mqlock); spin_lock_init(&upper->spinlock); upper->dev = dev; @@ -2126,6 +2161,7 @@ int audio_unregister(FAR const char *name, FAR struct audio_lowerhalf_s *dev) } nxmutex_destroy(&upper->lock); + nxmutex_destroy(&upper->mqlock); kmm_free(upper); dev->priv = NULL; return 0;
