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 78d7c6760f2de3cabc4f461378632bcc433737c1
Author: Marco Casaroli <[email protected]>
AuthorDate: Mon Nov 16 22:43:28 2026 +0100

    audio: Do not sleep or free memory under the spinlock in the last close.
    
    The last close calls the shutdown() method of the lower half and frees
    the status while it holds the spinlock of the upper half, with the
    interrupts off.  The shutdown() of most codec drivers (cs43l22, es8388,
    wm8904 and others) resets the chip over I2C, and an I2C transfer waits
    on a semaphore.  kumm_free() can wait for the heap mutex.  The buffer
    completions take the same spinlock, so on SMP a completion on another
    CPU spins until the close continues.
    
    Clear upper->status under the spinlock as before, so that a completion
    after the last close is still dropped.  Then release the spinlock, call
    shutdown() and free the status.  The mutex of the upper half keeps other
    opens and closes out.  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 | 19 ++++++++++++++-----
 1 file changed, 14 insertions(+), 5 deletions(-)

diff --git a/audio/audio.c b/audio/audio.c
index a580ba0234d..aa06a861af4 100644
--- a/audio/audio.c
+++ b/audio/audio.c
@@ -297,6 +297,7 @@ static int audio_close(FAR struct file *filep)
   FAR struct audio_openpriv_s *priv = filep->f_priv;
   FAR struct audio_openpriv_s *curr;
   FAR struct audio_openpriv_s *prev;
+  FAR struct audio_status_s *status = NULL;
   irqstate_t flags;
   int ret;
 
@@ -325,10 +326,21 @@ static int audio_close(FAR struct file *filep)
     }
 
   /*  If the reference head decrement to NULL,
-   *  then uninitialize the driver.
+   *  then uninitialize the driver.  A completion after this sees no status
+   *  and is dropped.
    */
 
   if (upper->head == NULL)
+    {
+      status = upper->status;
+      upper->status = NULL;
+    }
+
+  spin_unlock_irqrestore(&upper->spinlock, flags);
+
+  /* Shut down and free outside the spinlock: both can sleep */
+
+  if (status != NULL)
     {
       FAR struct audio_lowerhalf_s *lower = upper->dev;
 
@@ -338,12 +350,9 @@ static int audio_close(FAR struct file *filep)
       audinfo("calling shutdown\n");
 
       lower->ops->shutdown(lower);
-      kumm_free(upper->status);
-      upper->status = NULL;
+      kumm_free(status);
     }
 
-  spin_unlock_irqrestore(&upper->spinlock, flags);
-
   /* Clean up message queue */
 
   audio_unregistermq(priv);

Reply via email to