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 a1746bd081ce5276ead1fe5cd4b40ae1fc3a888d
Author: xuxingliang <[email protected]>
AuthorDate: Mon Mar 30 11:57:33 2026 +0800

    drivers/usbdev/usbmsc: fix WRITE deadlock and improve error handling
    
    Fix a deadlock in WRITE handling where the bulk OUT endpoint request
    length was set to CONFIG_USBMSC_BULKOUTREQLEN instead of maxpacket.
    The USB controller requires req->len <= maxpacket for correct bulk
    OUT transfer completion signaling.
    
    Improve request lifecycle management:
    - Use sq_remfirst under spinlock instead of sq_peek to eliminate
      race window between peek and remove in ISR context
    - Return requests to the free list on EP_SUBMIT failure to prevent
      permanent request leaks
    - Widen nsectbytes/nreqbytes/iosize from uint16_t to uint32_t to
      support iobuffer sizes beyond 64KB
    
    Signed-off-by: xuxingliang <[email protected]>
---
 drivers/usbdev/usbmsc.c      | 10 ++++-----
 drivers/usbdev/usbmsc.h      |  6 ++---
 drivers/usbdev/usbmsc_scsi.c | 53 ++++++++++++++++++++++----------------------
 3 files changed, 34 insertions(+), 35 deletions(-)

diff --git a/drivers/usbdev/usbmsc.c b/drivers/usbdev/usbmsc.c
index f668f702da6..1d381b49a74 100644
--- a/drivers/usbdev/usbmsc.c
+++ b/drivers/usbdev/usbmsc.c
@@ -1060,7 +1060,7 @@ int usbmsc_setconfig(FAR struct usbmsc_dev_s *priv, 
uint8_t config)
     {
       privreq       = &priv->rdreqs[i];
       req           = privreq->req;
-      req->len      = CONFIG_USBMSC_BULKOUTREQLEN;
+      req->len      = priv->epbulkout->maxpacket;
       req->priv     = privreq;
       req->callback = usbmsc_rdcomplete;
 
@@ -1547,10 +1547,10 @@ int usbmsc_bindlun(FAR void *handle, FAR const char 
*drvrpath,
 
   memset(lun, 0, sizeof(struct usbmsc_lun_s));
 
-  /* Allocate an I/O buffer big enough to hold one hardware sector.  SCSI
-   * commands are processed one at a time so all LUNs may share a single I/O
-   * buffer.  The I/O buffer will be allocated so that is it as large as the
-   * largest block device sector size
+  /* Allocate an I/O buffer big enough to hold one hardware sector.
+   * SCSI commands are processed one at a time so all LUNs may share
+   * a single I/O buffer.  The I/O buffer will be allocated so that
+   * it is as large as the largest block device sector size.
    */
 
   if (!priv->iobuffer)
diff --git a/drivers/usbdev/usbmsc.h b/drivers/usbdev/usbmsc.h
index 3c241e2c1c4..3eaee06a1a8 100644
--- a/drivers/usbdev/usbmsc.h
+++ b/drivers/usbdev/usbmsc.h
@@ -410,9 +410,9 @@ struct usbmsc_dev_s
   uint8_t           cbwdir:2;         /* Direction from CBW. See 
USBMSC_FLAGS_DIR* definitions */
   uint8_t           cdblen;           /* Length of cdb[] from CBW */
   uint8_t           cbwlun;           /* LUN from the CBW */
-  uint16_t          nsectbytes;       /* Bytes buffered in iobuffer[] */
-  uint16_t          nreqbytes;        /* Bytes buffered in head write requests 
*/
-  uint16_t          iosize;           /* Size of iobuffer[] */
+  uint32_t          nsectbytes;       /* Bytes buffered in iobuffer[] */
+  uint32_t          nreqbytes;        /* Bytes buffered in head write requests 
*/
+  uint32_t          iosize;           /* Size of iobuffer[] */
   uint32_t          cbwlen;           /* Length of data from CBW */
   uint32_t          cbwtag;           /* Tag from the CBW */
   union
diff --git a/drivers/usbdev/usbmsc_scsi.c b/drivers/usbdev/usbmsc_scsi.c
index 0b2f217d77b..0932cf123ee 100644
--- a/drivers/usbdev/usbmsc_scsi.c
+++ b/drivers/usbdev/usbmsc_scsi.c
@@ -2282,17 +2282,11 @@ static int usbmsc_cmdreadstate(FAR struct usbmsc_dev_s 
*priv)
           priv->sector++;
         }
 
-      /* Check if there is a request in the wrreqlist that we will be able to
-       * use for data transfer.
-       */
-
-      privreq = (FAR struct usbmsc_req_s *)sq_peek(&priv->wrreqlist);
+      /* Dequeue a request under spinlock to ensure exclusive ownership */
 
-      /* If there no request structures available, then just return an
-       * error.  This will cause us to remain in the CMDREAD state.  When a
-       * request is returned, the worker thread will be awakened in the
-       * USBMSC_STATE_CMDREAD and we will be called again.
-       */
+      flags = spin_lock_irqsave(&priv->spinlock);
+      privreq = (FAR struct usbmsc_req_s *)sq_remfirst(&priv->wrreqlist);
+      spin_unlock_irqrestore(&priv->spinlock, flags);
 
       if (!privreq)
         {
@@ -2329,15 +2323,7 @@ static int usbmsc_cmdreadstate(FAR struct usbmsc_dev_s 
*priv)
       if (priv->nreqbytes >= priv->epbulkin->maxpacket ||
           (priv->u.xfrlen <= 0 && priv->nsectbytes <= 0))
         {
-          /* Remove the request that we just filled from wrreqlist (we've
-           * already checked that is it not NULL
-           */
-
-          flags = spin_lock_irqsave(&priv->spinlock);
-          privreq = (FAR struct usbmsc_req_s *)sq_remfirst(&priv->wrreqlist);
-          spin_unlock_irqrestore(&priv->spinlock, flags);
-
-          /* And submit the request to the bulk IN endpoint */
+          /* Submit the request to the bulk IN endpoint */
 
           req->len      = priv->nreqbytes;
           req->priv     = privreq;
@@ -2351,17 +2337,26 @@ static int usbmsc_cmdreadstate(FAR struct usbmsc_dev_s 
*priv)
                        (uint16_t)-ret);
               lun->sd     = SCSI_KCQME_UNRRE1;
               lun->sdinfo = priv->sector;
+
+              /* Return request to list on failure */
+
+              flags = spin_lock_irqsave(&priv->spinlock);
+              sq_addfirst((FAR sq_entry_t *)privreq, &priv->wrreqlist);
+              spin_unlock_irqrestore(&priv->spinlock, flags);
               break;
             }
 
-          /* Assume success... residue should probably really be decremented
-           * in wrcomplete when we know that the transfer completed
-           * successfully.
-           */
-
           priv->residue  -= priv->nreqbytes;
           priv->nreqbytes = 0;
         }
+      else
+        {
+          /* Request not full yet, return it to the list */
+
+          flags = spin_lock_irqsave(&priv->spinlock);
+          sq_addfirst((FAR sq_entry_t *)privreq, &priv->wrreqlist);
+          spin_unlock_irqrestore(&priv->spinlock, flags);
+        }
     }
 
   usbtrace(TRACE_CLASSSTATE(USBMSC_CLASSSTATE_CMDREADCMDFINISH),
@@ -2401,8 +2396,9 @@ static int usbmsc_cmdwritestate(FAR struct usbmsc_dev_s 
*priv)
   FAR struct usbmsc_lun_s *lun = priv->lun;
   FAR struct usbmsc_req_s *privreq;
   FAR struct usbdev_req_s *req;
+  irqstate_t flags;
   ssize_t nwritten;
-  uint16_t xfrd;
+  uint32_t xfrd;
   FAR uint8_t *src;
   FAR uint8_t *dest;
   int nbytes;
@@ -2421,7 +2417,7 @@ static int usbmsc_cmdwritestate(FAR struct usbmsc_dev_s 
*priv)
        * data to be written.
        */
 
-      irqstate_t flags = spin_lock_irqsave(&priv->spinlock);
+      flags = spin_lock_irqsave(&priv->spinlock);
       privreq = (FAR struct usbmsc_req_s *)sq_remfirst(&priv->rdreqlist);
       spin_unlock_irqrestore(&priv->spinlock, flags);
 
@@ -2546,11 +2542,14 @@ static int usbmsc_cmdwritestate(FAR struct usbmsc_dev_s 
*priv)
         {
           usbtrace(TRACE_CLSERROR(USBMSC_TRACEERR_CMDWRITERDSUBMIT),
                    (uint16_t)-ret);
+          flags = spin_lock_irqsave(&priv->spinlock);
+          sq_addlast((FAR sq_entry_t *)privreq, &priv->rdreqlist);
+          spin_unlock_irqrestore(&priv->spinlock, flags);
         }
 
       /* Did the host decide to stop early? */
 
-      if (xfrd != priv->epbulkout->maxpacket)
+      if (xfrd < priv->epbulkout->maxpacket)
         {
           priv->shortpacket = 1;
           goto errout;

Reply via email to