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 a3af6fe3666bdfbe2efcf2b3b4734af59cc9663b
Author: yangsong8 <[email protected]>
AuthorDate: Thu Aug 20 14:04:42 2026 +0800

    drivers/usbdev/cdcacm: refine init lock and polling flag ordering
    
    Two related concurrency tweaks in the CDC/ACM driver:
    - Move g_init_lock into cdcacm_classobject to protect both paths.
    - Set priv->ispolling under priv->lock in cdcuart_txempty.
    
    Co-authored-by: Kiro AI <[email protected]>
    Signed-off-by: yangsong8 <[email protected]>
---
 drivers/usbdev/cdcacm.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)

diff --git a/drivers/usbdev/cdcacm.c b/drivers/usbdev/cdcacm.c
index a2d4e742f0c..770a3e0d536 100644
--- a/drivers/usbdev/cdcacm.c
+++ b/drivers/usbdev/cdcacm.c
@@ -3013,18 +3013,18 @@ static bool cdcuart_txempty(FAR struct uart_dev_s *dev)
       return true;
     }
 
+  priv->ispolling = true;
   spin_unlock_irqrestore(&priv->lock, flags);
 
-  priv->ispolling = true;
   EP_POLL(ep);
+
+  flags = spin_lock_irqsave(&priv->lock);
   priv->ispolling = false;
 
   /* When all of the allocated write requests have been returned to the
    * txfree, then there is no longer any TX data in flight.
    */
 
-  flags = spin_lock_irqsave(&priv->lock);
-
 #ifdef CONFIG_CDCACM_DISABLE_TXBUF
   /* dev->xmit.buffer always take one req, so just compare
    * CONFIG_CDCACM_NWRREQS - 1.
@@ -3330,6 +3330,12 @@ int cdcacm_classobject(int minor, FAR struct 
usbdev_devinfo_s *devinfo,
   char devname[CDCACM_DEVNAME_SIZE];
   int ret;
 
+  ret = nxmutex_lock(&g_init_lock);
+  if (ret < 0)
+    {
+      return ret;
+    }
+
   /* Allocate the structures needed */
 
   alloc = (FAR struct cdcacm_alloc_s *)
@@ -3338,6 +3344,7 @@ int cdcacm_classobject(int minor, FAR struct 
usbdev_devinfo_s *devinfo,
   if (!alloc)
     {
       usbtrace(TRACE_CLSERROR(USBSER_TRACEERR_ALLOCDEVSTRUCT), 0);
+      nxmutex_unlock(&g_init_lock);
       return -ENOMEM;
     }
 
@@ -3442,10 +3449,12 @@ int cdcacm_classobject(int minor, FAR struct 
usbdev_devinfo_s *devinfo,
     }
 #endif
 
+  nxmutex_unlock(&g_init_lock);
   return OK;
 
 errout_with_class:
   kmm_free(alloc);
+  nxmutex_unlock(&g_init_lock);
   return ret;
 }
 
@@ -3501,12 +3510,6 @@ int cdcacm_initialize(int minor, FAR void **handle)
   devinfo.epno[CDCACM_EP_BULKIN_IDX]  = CONFIG_CDCACM_EPBULKIN;
   devinfo.epno[CDCACM_EP_BULKOUT_IDX] = CONFIG_CDCACM_EPBULKOUT;
 
-  ret = nxmutex_lock(&g_init_lock);
-  if (ret < 0)
-    {
-      return ret;
-    }
-
   /* Get an instance of the serial driver class object */
 
   ret = cdcacm_classobject(minor, &devinfo, &drvr);
@@ -3523,8 +3526,6 @@ int cdcacm_initialize(int minor, FAR void **handle)
         }
     }
 
-  nxmutex_unlock(&g_init_lock);
-
   /* Return the driver instance (if any) if the caller has requested it
    * by provided a pointer to the location to return it.
    */

Reply via email to