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 a1dc6b3cc6edb1c9f1ea8fc0f38abeb123f268d3
Author: Marco Casaroli <[email protected]>
AuthorDate: Fri Nov 20 03:59:07 2026 +0100

    net/tcp: Free the pending connections of a backlog without a lock.
    
    tcp_free() destroyed the backlog of a listener with the TCP connection
    list lock held, and tcp_backlogdestroy() freed each pending connection
    with tcp_free(), which takes the device lock of that connection.  The
    input path takes the list lock under the device lock (tcp_input() for a
    reset of a pending connection, the allocation of a new connection), so
    the close of a listener with pending connections and the input path
    could wait for each other: the list lock and the device lock stayed
    held, and the work queues stopped in the list lock.
    
    tcp_backlogcreate() (listen() again on a listener) had the same order
    with the listener's lock: it destroyed the old backlog with that lock
    held, and tcp_accept_connection() takes the listener's lock under the
    device lock.
    
    tcp_backlogdestroy() is now split.  tcp_backlogdetach() takes the
    backlog away from the connection and sets the blparent of each pending
    connection to NULL: tcp_free() calls it with the list lock held, as
    before, so that tcp_input() sees either the old backlog or no parent,
    and tcp_backlogcreate() calls it with the listener's lock held.
    tcp_backlogfree() then frees the pending connections and the backlog
    after the lock is released.
    
    tcp_free() also took the backlog away only after conn_uninit() had
    destroyed the lock of the listener.  In between, tcp_input() for a reset
    of a pending connection could find the listener through blparent and
    lock the destroyed mutex.  tcp_free() now detaches the backlog before
    conn_uninit().
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 net/tcp/tcp.h         |  38 +++++++++++++----
 net/tcp/tcp_backlog.c | 114 +++++++++++++++++++++++++++++++-------------------
 net/tcp/tcp_conn.c    |  28 ++++++++-----
 3 files changed, 118 insertions(+), 62 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index bf85333cf63..5b263fab540 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -1601,24 +1601,44 @@ int tcp_backlogcreate(FAR struct tcp_conn_s *conn, int 
nblg);
 #endif
 
 /****************************************************************************
- * Name: tcp_backlogdestroy
+ * Name: tcp_backlogdetach
  *
  * Description:
- *   (1) Called from tcp_free() whenever a connection is freed.
- *   (2) Called from tcp_backlogcreate() to destroy any old backlog
+ *   Take the backlog of a connection away from it, with the pending
+ *   connections in it: their blparent is set to NULL.  tcp_backlogfree()
+ *   frees them later.  (1) Called from tcp_free() whenever a connection is
+ *   freed, with the TCP connection list locked.  (2) Called from
+ *   tcp_backlogcreate() for the old backlog, with the connection locked.
  *
- *   NOTE: This function may re-enter tcp_free when a connection that
- *   is freed that has pending connections.
+ * Returned Value:
+ *   The backlog that was detached, or NULL if there was none.
  *
- * Assumptions:
- *   Called from network socket logic with the network stack locked
+ ****************************************************************************/
+
+#ifdef CONFIG_NET_TCPBACKLOG
+FAR struct tcp_backlog_s *tcp_backlogdetach(FAR struct tcp_conn_s *conn);
+#else
+#  define tcp_backlogdetach(conn)      (NULL)
+#endif
+
+/****************************************************************************
+ * Name: tcp_backlogfree
+ *
+ * Description:
+ *   Free a backlog that tcp_backlogdetach() returned, and the pending
+ *   connections in it.
+ *
+ *   NOTE: This function re-enters tcp_free() for each pending connection,
+ *   which takes the device lock of that connection.  So the caller holds
+ *   no lock of the network: not the TCP connection list lock, which the
+ *   input path takes under a device lock, and not the lock of a connection.
  *
  ****************************************************************************/
 
 #ifdef CONFIG_NET_TCPBACKLOG
-int tcp_backlogdestroy(FAR struct tcp_conn_s *conn);
+void tcp_backlogfree(FAR struct tcp_backlog_s *blg);
 #else
-#  define tcp_backlogdestroy(conn)     (-ENOSYS)
+#  define tcp_backlogfree(blg)
 #endif
 
 /****************************************************************************
diff --git a/net/tcp/tcp_backlog.c b/net/tcp/tcp_backlog.c
index 1a63a690b57..cf6904d8dd4 100644
--- a/net/tcp/tcp_backlog.c
+++ b/net/tcp/tcp_backlog.c
@@ -59,6 +59,7 @@
 int tcp_backlogcreate(FAR struct tcp_conn_s *conn, int nblg)
 {
   FAR struct tcp_backlog_s     *bls = NULL;
+  FAR struct tcp_backlog_s     *old;
   FAR struct tcp_blcontainer_s *blc;
   FAR struct tcp_conn_s        *blconn;
   int size;
@@ -139,83 +140,112 @@ int tcp_backlogcreate(FAR struct tcp_conn_s *conn, int 
nblg)
       blconn->blparent = conn;
     }
 
-  tcp_backlogdestroy(conn);
+  old = tcp_backlogdetach(conn);
 
   /* Now install the backlog tear-off in the connection.  NOTE that bls may
    * actually be NULL if nblg is <= 0;  In that case, we are disabling
-   * backlog support.  Since the network is locked, destroying the old
-   * backlog and replace it with the new is an atomic operation
+   * backlog support.  Since the connection is locked, detaching the old
+   * backlog and installing the new one is an atomic operation.
    */
 
   conn->backlog = bls;
   conn_unlock(&conn->sconn);
+
+  /* Free the connections left in the old backlog without the lock of the
+   * connection: tcp_free() takes their device locks.
+   */
+
+  tcp_backlogfree(old);
   return OK;
 }
 
 /****************************************************************************
- * Name: tcp_backlogdestroy
+ * Name: tcp_backlogdetach
  *
  * Description:
- *   (1) Called from tcp_free() whenever a connection is freed.
- *   (2) Called from tcp_backlogcreate() to destroy any old backlog
- *
- *   NOTE: This function may re-enter tcp_free when a connection that
- *   is freed that has pending connections.
+ *   Take the backlog of a connection away from it, with the pending
+ *   connections in it: their blparent is set to NULL, so that the input
+ *   path does not use the backlog any more.  tcp_backlogfree() frees them.
  *
  * Assumptions:
- *   Called from network socket logic with the network locked
+ *   Called from tcp_free() with the TCP connection list locked, or from
+ *   tcp_backlogcreate() with the connection locked.
  *
  ****************************************************************************/
 
-int tcp_backlogdestroy(FAR struct tcp_conn_s *conn)
+FAR struct tcp_backlog_s *tcp_backlogdetach(FAR struct tcp_conn_s *conn)
 {
   FAR struct tcp_backlog_s     *blg;
   FAR struct tcp_blcontainer_s *blc;
-  FAR struct tcp_conn_s        *blconn;
 
   ninfo("conn=%p\n", conn);
 
-#ifdef CONFIG_DEBUG_FEATURES
-  if (!conn)
+  blg           = conn->backlog;
+  conn->backlog = NULL;
+
+  if (blg != NULL)
     {
-      return -EINVAL;
+      for (blc = (FAR struct tcp_blcontainer_s *)sq_peek(&blg->bl_pending);
+           blc != NULL;
+           blc = (FAR struct tcp_blcontainer_s *)sq_next(&blc->bc_node))
+        {
+          if (blc->bc_conn != NULL)
+            {
+              blc->bc_conn->blparent = NULL;
+            }
+        }
     }
-#endif
 
-  /* Make sure that the connection has a backlog to be destroyed */
+  return blg;
+}
 
-  if (conn->backlog)
-    {
-      /* Remove the backlog structure reference from the connection */
+/****************************************************************************
+ * Name: tcp_backlogfree
+ *
+ * Description:
+ *   Free a backlog that tcp_backlogdetach() returned, and the pending
+ *   connections in it.
+ *
+ *   NOTE: This function re-enters tcp_free() for each pending connection.
+ *
+ * Assumptions:
+ *   The caller holds no lock of the network: tcp_free() takes the device
+ *   lock of each pending connection, and the input path takes the TCP
+ *   connection list lock under a device lock.
+ *
+ ****************************************************************************/
 
-      blg           = conn->backlog;
-      conn->backlog = NULL;
+void tcp_backlogfree(FAR struct tcp_backlog_s *blg)
+{
+  FAR struct tcp_blcontainer_s *blc;
+  FAR struct tcp_conn_s        *blconn;
+
+  if (blg == NULL)
+    {
+      return;
+    }
 
-      /* Handle any pending connections in the backlog */
+  /* Handle any pending connections in the backlog */
 
-      while ((blc = (FAR struct tcp_blcontainer_s *)
-                    sq_remfirst(&blg->bl_pending)) != NULL)
+  while ((blc = (FAR struct tcp_blcontainer_s *)
+                sq_remfirst(&blg->bl_pending)) != NULL)
+    {
+      blconn = blc->bc_conn;
+      if (blconn)
         {
-          blconn = blc->bc_conn;
-          if (blconn)
-            {
-              /* REVISIT
-               * -- such connections really need to be gracefully closed
-               */
-
-              blconn->blparent = NULL;
-              blconn->backlog  = NULL;
-              blconn->crefs    = 0;
-              tcp_free(blconn);
-            }
-        }
-
-      /* Then free the entire backlog structure */
+          /* REVISIT
+           * -- such connections really need to be gracefully closed
+           */
 
-      kmm_free(blg);
+          blconn->backlog = NULL;
+          blconn->crefs   = 0;
+          tcp_free(blconn);
+        }
     }
 
-  return OK;
+  /* Then free the entire backlog structure */
+
+  kmm_free(blg);
 }
 
 /****************************************************************************
diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index 3939e67871a..a09648a1752 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -948,6 +948,9 @@ void tcp_free(FAR struct tcp_conn_s *conn)
 #ifdef CONFIG_NET_TCP_WRITE_BUFFERS
   FAR struct tcp_wrbuffer_s *wrbuffer;
 #endif
+#ifdef CONFIG_NET_TCPBACKLOG
+  FAR struct tcp_backlog_s *blg;
+#endif
 
   DEBUGASSERT(conn->crefs == 0);
 
@@ -1018,22 +1021,25 @@ void tcp_free(FAR struct tcp_conn_s *conn)
   nxsem_destroy(&conn->snd_sem);
 #endif
 
-  /* Use conn_uninit to release all connection resources */
-
-  conn_uninit(&conn->sconn);
-
 #ifdef CONFIG_NET_TCPBACKLOG
+  /* Remove any backlog attached to this connection.  Take it away with the
+   * list locked: tcp_input() looks at the blparent of a pending connection
+   * under the same lock, and then locks this connection, so do it before
+   * conn_uninit() destroys that lock.  Free the pending connections after
+   * the list is unlocked: tcp_free() takes their device locks, and the
+   * input path takes the list lock under a device lock.
+   */
+
   tcp_conn_list_lock();
+  blg = tcp_backlogdetach(conn);
+  tcp_conn_list_unlock();
 
-  /* Remove any backlog attached to this connection */
+  tcp_backlogfree(blg);
+#endif
 
-  if (conn->backlog)
-    {
-      tcp_backlogdestroy(conn);
-    }
+  /* Use conn_uninit to release all connection resources */
 
-  tcp_conn_list_unlock();
-#endif
+  conn_uninit(&conn->sconn);
 
   nxrmutex_destroy(&conn->sconn.s_lock);
 

Reply via email to