This is an automated email from the ASF dual-hosted git repository.

GUIDINGLI pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 8b472045fee0f1de62389cf1969cd6461dad648e
Author: zhanghongyu <[email protected]>
AuthorDate: Wed Jan 7 12:15:00 2026 +0800

    net_lock.c: add conn_dev_iobtimedalloc to replace net_iobtimedalloc
    
    net_iobtimedalloc() only released the global network lock while waiting
    for an I/O buffer.  With the per-connection s_lock and per-device d_lock,
    that is no longer the right lock to break.
    
    Add conn_dev_iobtimedalloc() and conn_dev_ioballoc() that break both the
    connection s_lock and the device d_lock while waiting for an IOB, and route
    the UDP/TCP/PKT/CAN and other write-buffer allocators through them so the
    IOB wait no longer blocks holders of the new locks.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 drivers/net/netdev_upperhalf.c      |   2 +-
 include/nuttx/net/can.h             |   7 ++-
 include/nuttx/net/net.h             |  45 ++++++----------
 net/bluetooth/bluetooth_sendmsg.c   |   3 +-
 net/can/can_bufpool.c               |  43 +++++++++++++---
 net/can/can_isotp.c                 |   4 +-
 net/can/can_raw.c                   |   6 ++-
 net/ieee802154/ieee802154_sendmsg.c |   3 +-
 net/igmp/igmp_group.c               |   4 +-
 net/netdev/netdev_iob.c             |   5 +-
 net/pkt/pkt_sendmsg_buffered.c      |   5 +-
 net/route/net_fileroute.c           |  14 ++---
 net/sixlowpan/sixlowpan_framelist.c |   4 +-
 net/tcp/tcp.h                       |  25 ++-------
 net/tcp/tcp_send_buffered.c         |  19 ++++---
 net/tcp/tcp_wrbuffer.c              |  32 +++---------
 net/udp/udp.h                       |  23 +--------
 net/udp/udp_wrbuffer.c              |  52 ++-----------------
 net/utils/net_lock.c                | 100 +++++++++++++++++++-----------------
 19 files changed, 159 insertions(+), 237 deletions(-)

diff --git a/drivers/net/netdev_upperhalf.c b/drivers/net/netdev_upperhalf.c
index 240833ee3d6..9c43464dcc9 100644
--- a/drivers/net/netdev_upperhalf.c
+++ b/drivers/net/netdev_upperhalf.c
@@ -1979,7 +1979,7 @@ FAR netpkt_t *netpkt_alloc(FAR struct netdev_lowerhalf_s 
*dev,
 #ifdef CONFIG_NET_CAN
   if (dev->netdev.d_lltype == NET_LL_CAN)
     {
-      pkt = can_iob_timedalloc(0);
+      pkt = can_iob_timedalloc(false, 0, NULL, NULL);
     }
   else
 #endif
diff --git a/include/nuttx/net/can.h b/include/nuttx/net/can.h
index 60b282d0837..f4c85af84b6 100644
--- a/include/nuttx/net/can.h
+++ b/include/nuttx/net/can.h
@@ -156,9 +156,12 @@ int can_input(FAR struct net_driver_s *dev);
  ****************************************************************************/
 
 #if CONFIG_NET_CAN_NBUFFERS > 0
-FAR struct iob_s *can_iob_timedalloc(unsigned int timeout);
+struct socket_conn_s; /* Forward reference */
+FAR struct iob_s *can_iob_timedalloc(bool throttled, unsigned int timeout,
+                                     FAR struct socket_conn_s *sconn,
+                                     FAR struct net_driver_s *dev);
 #else
-#define can_iob_timedalloc(t) net_iobtimedalloc(true, t)
+#define can_iob_timedalloc(th,t,s,d) conn_dev_iobtimedalloc(th, t, s, d)
 #endif
 
 /****************************************************************************
diff --git a/include/nuttx/net/net.h b/include/nuttx/net/net.h
index 03b5cc0b9d5..65a52641ac7 100644
--- a/include/nuttx/net/net.h
+++ b/include/nuttx/net/net.h
@@ -338,9 +338,6 @@ ssize_t net_ioctl_arglen(uint8_t domain, int cmd);
  *   net_unlock()      - Unlocks the network.
  *   net_sem_wait()    - Like pthread_cond_wait() except releases the
  *                       network momentarily to wait on another semaphore.
- *   net_ioballoc()    - Like iob_alloc() except releases the network
- *                       momentarily to wait for an IOB to become
- *                       available.
  *
  ****************************************************************************/
 
@@ -361,25 +358,6 @@ ssize_t net_ioctl_arglen(uint8_t domain, int cmd);
 
 int net_lock(void);
 
-/****************************************************************************
- * Name: net_trylock
- *
- * Description:
- *   Try to take the network lock only when it is currently not locked.
- *   Otherwise, it locks the semaphore.  In either
- *   case, the call returns without blocking.
- *
- * Input Parameters:
- *   None
- *
- * Returned Value:
- *   Zero (OK) is returned on success; a negated errno value is returned on
- *   failure (probably -EAGAIN).
- *
- ****************************************************************************/
-
-int net_trylock(void);
-
 /****************************************************************************
  * Name: net_unlock
  *
@@ -504,15 +482,16 @@ int net_sem_timedwait2(FAR sem_t *sem, bool interruptible,
                        unsigned int timeout, FAR rmutex_t *mutex1,
                        FAR rmutex_t *mutex2);
 
+struct net_driver_s; /* Forward reference */
 #ifdef CONFIG_MM_IOB
 
 /****************************************************************************
- * Name: net_iobtimedalloc
+ * Name: conn_dev_iobtimedalloc
  *
  * Description:
  *   Allocate an IOB.  If no IOBs are available, then atomically wait for
  *   for the IOB while temporarily releasing the lock on the network.
- *   This function is wrapped version of net_ioballoc(), this wait will
+ *   This function is wrapped version of conn_dev_ioballoc(), this wait will
  *   be terminated when the specified timeout expires.
  *
  *   Caution should be utilized.  Because the network lock is relinquished
@@ -523,6 +502,8 @@ int net_sem_timedwait2(FAR sem_t *sem, bool interruptible,
  * Input Parameters:
  *   throttled  - An indication of the IOB allocation is "throttled"
  *   timeout    - The relative time to wait until a timeout is declared.
+ *   sconn      - The socket connection structure.
+ *   dev        - The network device structure.
  *
  * Returned Value:
  *   A pointer to the newly allocated IOB is returned on success.  NULL is
@@ -530,22 +511,27 @@ int net_sem_timedwait2(FAR sem_t *sem, bool interruptible,
  *
  ****************************************************************************/
 
-FAR struct iob_s *net_iobtimedalloc(bool throttled, unsigned int timeout);
+FAR struct iob_s *
+conn_dev_iobtimedalloc(bool throttled, unsigned int timeout,
+                       FAR struct socket_conn_s *sconn,
+                       FAR struct net_driver_s *dev);
 
 /****************************************************************************
- * Name: net_ioballoc
+ * Name: conn_dev_ioballoc
  *
  * Description:
  *   Allocate an IOB.  If no IOBs are available, then atomically wait for
  *   for the IOB while temporarily releasing the lock on the network.
  *
  *   Caution should be utilized.  Because the network lock is relinquished
- *   during the wait, there could changes in the network state that occur
+ *   during the wait, there could be changes in the network state that occur
  *   before the lock is recovered.  Your design should account for this
  *   possibility.
  *
  * Input Parameters:
  *   throttled  - An indication of the IOB allocation is "throttled"
+ *   sconn      - The socket connection structure.
+ *   dev        - The network device structure.
  *
  * Returned Value:
  *   A pointer to the newly allocated IOB is returned on success.  NULL is
@@ -553,7 +539,9 @@ FAR struct iob_s *net_iobtimedalloc(bool throttled, 
unsigned int timeout);
  *
  ****************************************************************************/
 
-FAR struct iob_s *net_ioballoc(bool throttled);
+FAR struct iob_s *conn_dev_ioballoc(bool throttled,
+                                    FAR struct socket_conn_s *sconn,
+                                    FAR struct net_driver_s *dev);
 #endif
 
 /****************************************************************************
@@ -1488,7 +1476,6 @@ int psock_socketpair(int domain, int type, int protocol,
  *
  ****************************************************************************/
 
-struct net_driver_s; /* Forward reference */
 int netdev_register(FAR struct net_driver_s *dev, enum net_lltype_e lltype);
 
 /****************************************************************************
diff --git a/net/bluetooth/bluetooth_sendmsg.c 
b/net/bluetooth/bluetooth_sendmsg.c
index 760cee96c3d..c12c87665ef 100644
--- a/net/bluetooth/bluetooth_sendmsg.c
+++ b/net/bluetooth/bluetooth_sendmsg.c
@@ -147,7 +147,7 @@ static uint32_t bluetooth_sendto_eventhandler(FAR struct 
net_driver_s *dev,
 
       /* Allocate an IOB to hold the frame data */
 
-      iob = net_ioballoc(false);
+      iob = conn_dev_ioballoc(false, pstate->is_sock->s_conn, &radio->r_dev);
       if (iob == NULL)
         {
           nwarn("WARNING: Failed to allocate IOB\n");
@@ -170,6 +170,7 @@ static uint32_t bluetooth_sendto_eventhandler(FAR struct 
net_driver_s *dev,
       if (ret < 0)
         {
           nerr("ERROR: r_req_data() failed: %d\n", ret);
+          iob_free_chain(iob);
           goto errout;
         }
 
diff --git a/net/can/can_bufpool.c b/net/can/can_bufpool.c
index a5140a7d80b..a2752e3900c 100644
--- a/net/can/can_bufpool.c
+++ b/net/can/can_bufpool.c
@@ -81,17 +81,48 @@ static void can_buf_free(FAR void *buf)
  *
  ****************************************************************************/
 
-FAR struct iob_s *can_iob_timedalloc(unsigned int timeout)
+FAR struct iob_s *can_iob_timedalloc(bool throttled, unsigned int timeout,
+                                     FAR struct socket_conn_s *sconn,
+                                     FAR struct net_driver_s *dev)
 {
-  FAR void *buf = NET_BUFPOOL_TIMEDALLOC(g_can_buffer, timeout);
+  unsigned int count1 = 0;
+  unsigned int count2 = 0;
+  int          blresult1 = -ENOENT;
+  int          blresult2 = -ENOENT;
 
-  if (buf == NULL)
+  if (sconn != NULL)
+    {
+      blresult1 = nxrmutex_breaklock(&sconn->s_lock, &count1);
+    }
+
+  if (dev != NULL)
+    {
+      blresult2 = nxrmutex_breaklock(&dev->d_lock, &count2);
+    }
+
+  FAR void *buf = mempool_zallocate_throttled(&g_can_buffer, timeout,
+                                              throttled);
+
+  if (buf != NULL)
+    {
+      buf = iob_init_with_data(buf, CAN_BUFFER_SIZE, can_buf_free);
+    }
+  else
     {
       nwarn("Failed to allocate CAN buffer\n");
-      return NULL;
     }
 
-  return iob_init_with_data(buf, CAN_BUFFER_SIZE, can_buf_free);
+  if (blresult2 >= 0)
+    {
+      nxrmutex_restorelock(&dev->d_lock, count2);
+    }
+
+  if (blresult1 >= 0)
+    {
+      nxrmutex_restorelock(&sconn->s_lock, count1);
+    }
+
+  return buf;
 }
 
 /****************************************************************************
@@ -106,7 +137,7 @@ FAR struct iob_s *can_iob_clone(FAR struct net_driver_s 
*dev)
 {
   FAR struct iob_s *iob;
 
-  iob = can_iob_timedalloc(0);
+  iob = can_iob_timedalloc(throttled, 0, NULL, NULL);
   if (iob == NULL)
     {
       return NULL;
diff --git a/net/can/can_isotp.c b/net/can/can_isotp.c
index f826f18f0d6..3a9682992de 100644
--- a/net/can/can_isotp.c
+++ b/net/can/can_isotp.c
@@ -562,7 +562,7 @@ isotp_rx_input_cf(FAR struct isotp_conn_s *isotp,
 static inline_function FAR struct iob_s *
 isotp_build_fc_frame(FAR struct isotp_conn_s *conn)
 {
-  FAR struct iob_s *iob = can_iob_timedalloc(0);
+  FAR struct iob_s *iob = can_iob_timedalloc(false, 0, NULL, NULL);
 
   if (iob == NULL)
     {
@@ -931,7 +931,7 @@ static int isotp_tx_enqueue(FAR struct isotp_conn_s *isotp,
       unsigned int          frame_size;
       uint8_t               dlen = isotp->ll.tx_dl;
 
-      iob = can_iob_timedalloc(0);
+      iob = can_iob_timedalloc(false, 0, NULL, NULL);
       if (iob == NULL)
         {
           ret = -EAGAIN;
diff --git a/net/can/can_raw.c b/net/can/can_raw.c
index e5c1a270634..5d866cfc1d6 100644
--- a/net/can/can_raw.c
+++ b/net/can/can_raw.c
@@ -1409,11 +1409,13 @@ static ssize_t can_raw_sendmsg(FAR struct socket *psock,
 
   if (nonblock)
     {
-      wb_iob = can_iob_timedalloc(0);
+      wb_iob = can_iob_timedalloc(false, 0, NULL, NULL);
     }
   else
     {
-      wb_iob = can_iob_timedalloc(_SO_TIMEOUT(conn->sconn.s_sndtimeo));
+      wb_iob = can_iob_timedalloc(false,
+                                  _SO_TIMEOUT(conn->sconn.s_sndtimeo),
+                                  &conn->sconn, NULL);
     }
 
   if (wb_iob == NULL)
diff --git a/net/ieee802154/ieee802154_sendmsg.c 
b/net/ieee802154/ieee802154_sendmsg.c
index 14b0654d915..8ca0753943c 100644
--- a/net/ieee802154/ieee802154_sendmsg.c
+++ b/net/ieee802154/ieee802154_sendmsg.c
@@ -332,7 +332,7 @@ static uint32_t ieee802154_sendto_eventhandler(FAR struct 
net_driver_s *dev,
 
       /* Allocate an IOB to hold the frame data */
 
-      iob = net_ioballoc(false);
+      iob = conn_dev_ioballoc(false, pstate->is_sock->s_conn, dev);
       if (iob == NULL)
         {
           nwarn("WARNING: Failed to allocate IOB\n");
@@ -355,6 +355,7 @@ static uint32_t ieee802154_sendto_eventhandler(FAR struct 
net_driver_s *dev,
       if (ret < 0)
         {
           nerr("ERROR: r_req_data() failed: %d\n", ret);
+          iob_free_chain(iob);
           goto errout;
         }
 
diff --git a/net/igmp/igmp_group.c b/net/igmp/igmp_group.c
index 9f7934fb442..a3fe37192ef 100644
--- a/net/igmp/igmp_group.c
+++ b/net/igmp/igmp_group.c
@@ -224,11 +224,11 @@ void igmp_grpfree(FAR struct net_driver_s *dev,
 
   /* Cancel the workqueue */
 
-  blresult = net_breaklock(&count);
+  blresult = netdev_breaklock(dev, &count);
   work_cancel_sync(LPWORK, &group->work);
   if (blresult >= 0)
     {
-      net_restorelock(count);
+      netdev_restorelock(dev, count);
     }
 
   /* Remove the group structure from the group list in the device structure */
diff --git a/net/netdev/netdev_iob.c b/net/netdev/netdev_iob.c
index 874cc7b7f9a..be815ffed8f 100644
--- a/net/netdev/netdev_iob.c
+++ b/net/netdev/netdev_iob.c
@@ -62,10 +62,10 @@ int netdev_iob_prepare(FAR struct net_driver_s *dev, bool 
throttled,
 
   if (dev->d_iob == NULL)
     {
-      dev->d_iob = net_iobtimedalloc(false, timeout);
+      dev->d_iob = conn_dev_iobtimedalloc(false, timeout, NULL, dev);
       if (dev->d_iob == NULL && throttled)
         {
-          dev->d_iob = net_iobtimedalloc(true, timeout);
+          dev->d_iob = conn_dev_iobtimedalloc(true, timeout, NULL, dev);
         }
     }
 
@@ -101,6 +101,7 @@ int netdev_iob_prepare(FAR struct net_driver_s *dev, bool 
throttled,
 void netdev_iob_prepare_dynamic(FAR struct net_driver_s *dev, uint16_t size)
 {
   FAR struct iob_s *iob;
+
   size += CONFIG_NET_LL_GUARDSIZE;
 
   if (dev->d_iob && size <= IOB_BUFSIZE(dev->d_iob))
diff --git a/net/pkt/pkt_sendmsg_buffered.c b/net/pkt/pkt_sendmsg_buffered.c
index e39cbb2d7b1..c6ce76a4980 100644
--- a/net/pkt/pkt_sendmsg_buffered.c
+++ b/net/pkt/pkt_sendmsg_buffered.c
@@ -271,9 +271,8 @@ ssize_t pkt_sendmsg(FAR struct socket *psock, FAR const 
struct msghdr *msg,
   iob = iob_tryalloc(true);
   if (iob == NULL && !nonblock)
     {
-      conn_dev_unlock(&conn->sconn, dev);
-      iob = net_iobtimedalloc(true, _SO_TIMEOUT(conn->sconn.s_sndtimeo));
-      conn_dev_lock(&conn->sconn, dev);
+      iob = conn_dev_iobtimedalloc(true, _SO_TIMEOUT(conn->sconn.s_sndtimeo),
+                                   &conn->sconn, dev);
     }
 
   if (iob == NULL)
diff --git a/net/route/net_fileroute.c b/net/route/net_fileroute.c
index 1f30e897c1a..c94e38c1ff2 100644
--- a/net/route/net_fileroute.c
+++ b/net/route/net_fileroute.c
@@ -41,16 +41,6 @@
 
 #if defined(CONFIG_ROUTE_IPv4_FILEROUTE) || 
defined(CONFIG_ROUTE_IPv6_FILEROUTE)
 
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-/* Special "impossible" PID value used to indicate that there is no holder
- * of the lock.
- */
-
-#define NO_HOLDER (INVALID_PROCESS_ID)
-
 /****************************************************************************
  * Private Data
  ****************************************************************************/
@@ -596,6 +586,7 @@ int net_routesize_ipv6(void)
 int net_lockroute_ipv4(void)
 {
   int ret = nxrmutex_lock(&g_ipv4_lock);
+
   if (ret < 0)
     {
       nerr("ERROR: nxrmutex_lock() failed: %d\n", ret);
@@ -609,6 +600,7 @@ int net_lockroute_ipv4(void)
 int net_lockroute_ipv6(void)
 {
   int ret = nxrmutex_lock(&g_ipv6_lock);
+
   if (ret < 0)
     {
       nerr("ERROR: nxrmutex_lock() failed: %d\n", ret);
@@ -637,6 +629,7 @@ int net_lockroute_ipv6(void)
 int net_unlockroute_ipv4(void)
 {
   int ret = nxrmutex_unlock(&g_ipv4_lock);
+
   if (ret < 0)
     {
       nerr("ERROR: nxrmutex_unlock() failed: %d\n", ret);
@@ -650,6 +643,7 @@ int net_unlockroute_ipv4(void)
 int net_unlockroute_ipv6(void)
 {
   int ret = nxrmutex_unlock(&g_ipv6_lock);
+
   if (ret < 0)
     {
       nerr("ERROR: nxrmutex_unlock() failed: %d\n", ret);
diff --git a/net/sixlowpan/sixlowpan_framelist.c 
b/net/sixlowpan/sixlowpan_framelist.c
index 1c6cde5c91f..abe5545e829 100644
--- a/net/sixlowpan/sixlowpan_framelist.c
+++ b/net/sixlowpan/sixlowpan_framelist.c
@@ -414,7 +414,7 @@ int sixlowpan_queue_frames(FAR struct radio_driver_s *radio,
    * necessary.
    */
 
-  iob = net_ioballoc(false);
+  iob = conn_dev_ioballoc(false, NULL, &radio->r_dev);
   DEBUGASSERT(iob != NULL);
 
   fptr = iob->io_data;
@@ -628,7 +628,7 @@ int sixlowpan_queue_frames(FAR struct radio_driver_s *radio,
            * necessary.
            */
 
-          iob = net_ioballoc(false);
+          iob = conn_dev_ioballoc(false, NULL, &radio->r_dev);
           DEBUGASSERT(iob != NULL);
 
           /* Initialize the IOB */
diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index 391bc656ab4..0ac488d22df 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -1855,10 +1855,11 @@ struct tcp_wrbuffer_s;
  *   Allocate a TCP write buffer by taking a pre-allocated buffer from
  *   the free list.  This function is called from TCP logic when a buffer
  *   of TCP data is about to sent
- *   This function is wrapped version of tcp_wrbuffer_alloc(),
  *   this wait will be terminated when the specified timeout expires.
  *
  * Input Parameters:
+ *   sconn     - The socket connection structure.
+ *   dev       - The network device structure.
  *   timeout   - The relative time to wait until a timeout is declared.
  *
  * Assumptions:
@@ -1866,25 +1867,9 @@ struct tcp_wrbuffer_s;
  *
  ****************************************************************************/
 
-FAR struct tcp_wrbuffer_s *tcp_wrbuffer_timedalloc(unsigned int timeout);
-
-/****************************************************************************
- * Name: tcp_wrbuffer_alloc
- *
- * Description:
- *   Allocate a TCP write buffer by taking a pre-allocated buffer from
- *   the free list.  This function is called from TCP logic when a buffer
- *   of TCP data is about to sent
- *
- * Input Parameters:
- *   None
- *
- * Assumptions:
- *   Called from user logic with the network locked.
- *
- ****************************************************************************/
-
-FAR struct tcp_wrbuffer_s *tcp_wrbuffer_alloc(void);
+FAR struct tcp_wrbuffer_s *
+tcp_wrbuffer_timedalloc(FAR struct socket_conn_s *sconn,
+                        FAR struct net_driver_s *dev, unsigned int timeout);
 
 /****************************************************************************
  * Name: tcp_wrbuffer_tryalloc
diff --git a/net/tcp/tcp_send_buffered.c b/net/tcp/tcp_send_buffered.c
index c6c389cee46..ef251305061 100644
--- a/net/tcp/tcp_send_buffered.c
+++ b/net/tcp/tcp_send_buffered.c
@@ -1519,10 +1519,8 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR 
const void *buf,
             }
           else
             {
-              conn_dev_unlock(&conn->sconn, conn->dev);
-              wrb = tcp_wrbuffer_timedalloc(tcp_send_gettimeout(start,
-                                                                timeout));
-              conn_dev_lock(&conn->sconn, conn->dev);
+              wrb = tcp_wrbuffer_timedalloc(&conn->sconn, conn->dev,
+                                        tcp_send_gettimeout(start, timeout));
               ninfo("new wrb %p\n", wrb);
             }
 
@@ -1623,14 +1621,15 @@ ssize_t psock_tcp_send(FAR struct socket *psock, FAR 
const void *buf,
 
           /* Wait for at least one IOB getting available.
            *
-           * Note: net_ioballoc releases the network lock when blocking.
-           * It allows our write_q being drained in the meantime. Otherwise,
-           * we risk a deadlock with other threads competing on IOBs.
+           * Note: conn_dev_iobtimedalloc releases the network lock when
+           * blocking. It allows our write_q being drained in the meantime.
+           * Otherwise, we risk a deadlock with other threads competing on
+           * IOBs.
            */
 
-          conn_dev_unlock(&conn->sconn, conn->dev);
-          iob = net_iobtimedalloc(true, tcp_send_gettimeout(start, timeout));
-          conn_dev_lock(&conn->sconn, conn->dev);
+          iob = conn_dev_iobtimedalloc(true,
+                                       tcp_send_gettimeout(start, timeout),
+                                       &conn->sconn, conn->dev);
           if (iob != NULL)
             {
               iob_free_chain(iob);
diff --git a/net/tcp/tcp_wrbuffer.c b/net/tcp/tcp_wrbuffer.c
index 5c8a738568e..828c0e0ea34 100644
--- a/net/tcp/tcp_wrbuffer.c
+++ b/net/tcp/tcp_wrbuffer.c
@@ -69,10 +69,11 @@ NET_BUFPOOL_DECLARE(g_wrbuffer, sizeof(struct 
tcp_wrbuffer_s),
  *   Allocate a TCP write buffer by taking a pre-allocated buffer from
  *   the free list.  This function is called from TCP logic when a buffer
  *   of TCP data is about to sent
- *   This function is wrapped version of tcp_wrbuffer_alloc(),
  *   this wait will be terminated when the specified timeout expires.
  *
  * Input Parameters:
+ *   sconn     - The socket connection structure.
+ *   dev       - The network device structure.
  *   timeout   - The relative time to wait until a timeout is declared.
  *
  * Assumptions:
@@ -80,7 +81,9 @@ NET_BUFPOOL_DECLARE(g_wrbuffer, sizeof(struct tcp_wrbuffer_s),
  *
  ****************************************************************************/
 
-FAR struct tcp_wrbuffer_s *tcp_wrbuffer_timedalloc(unsigned int timeout)
+FAR struct tcp_wrbuffer_s *
+tcp_wrbuffer_timedalloc(FAR struct socket_conn_s *sconn,
+                        FAR struct net_driver_s *dev, unsigned int timeout)
 {
   FAR struct tcp_wrbuffer_s *wrb;
 
@@ -100,7 +103,7 @@ FAR struct tcp_wrbuffer_s *tcp_wrbuffer_timedalloc(unsigned 
int timeout)
 
   /* Now get the first I/O buffer for the write buffer structure */
 
-  wrb->wb_iob = net_iobtimedalloc(true, timeout);
+  wrb->wb_iob = conn_dev_iobtimedalloc(true, timeout, sconn, dev);
 
   /* Did we get an IOB?  We should always get one except under some really
    * weird error conditions.
@@ -116,27 +119,6 @@ FAR struct tcp_wrbuffer_s 
*tcp_wrbuffer_timedalloc(unsigned int timeout)
   return wrb;
 }
 
-/****************************************************************************
- * Name: tcp_wrbuffer_alloc
- *
- * Description:
- *   Allocate a TCP write buffer by taking a pre-allocated buffer from
- *   the free list.  This function is called from TCP logic when a buffer
- *   of TCP data is about to sent
- *
- * Input Parameters:
- *   None
- *
- * Assumptions:
- *   Called from user logic with the network locked.
- *
- ****************************************************************************/
-
-FAR struct tcp_wrbuffer_s *tcp_wrbuffer_alloc(void)
-{
-  return tcp_wrbuffer_timedalloc(UINT_MAX);
-}
-
 /****************************************************************************
  * Name: tcp_wrbuffer_tryalloc
  *
@@ -157,7 +139,7 @@ FAR struct tcp_wrbuffer_s *tcp_wrbuffer_alloc(void)
 
 FAR struct tcp_wrbuffer_s *tcp_wrbuffer_tryalloc(void)
 {
-  return tcp_wrbuffer_timedalloc(0);
+  return tcp_wrbuffer_timedalloc(NULL, NULL, 0);
 }
 
 /****************************************************************************
diff --git a/net/udp/udp.h b/net/udp/udp.h
index d448c1e1500..ae11535a37f 100644
--- a/net/udp/udp.h
+++ b/net/udp/udp.h
@@ -503,27 +503,6 @@ int udp_setsockopt(FAR struct socket *psock, int option,
                    FAR const void *value, socklen_t value_len);
 #endif
 
-/****************************************************************************
- * Name: udp_wrbuffer_alloc
- *
- * Description:
- *   Allocate a UDP write buffer by taking a pre-allocated buffer from
- *   the free list.  This function is called from UDP logic when a buffer
- *   of UDP data is about to sent
- *
- * Input Parameters:
- *   None
- *
- * Assumptions:
- *   Called from user logic with the network locked.
- *
- ****************************************************************************/
-
-#ifdef CONFIG_NET_UDP_WRITE_BUFFERS
-struct udp_wrbuffer_s;
-FAR struct udp_wrbuffer_s *udp_wrbuffer_alloc(void);
-#endif /* CONFIG_NET_UDP_WRITE_BUFFERS */
-
 /****************************************************************************
  * Name: udp_wrbuffer_timedalloc
  *
@@ -531,7 +510,6 @@ FAR struct udp_wrbuffer_s *udp_wrbuffer_alloc(void);
  *   Allocate a UDP write buffer by taking a pre-allocated buffer from
  *   the free list.  This function is called from udp logic when a buffer
  *   of udp data is about to sent
- *   This function is wrapped version of udp_wrbuffer_alloc(),
  *   this wait will be terminated when the specified timeout expires.
  *
  * Input Parameters:
@@ -543,6 +521,7 @@ FAR struct udp_wrbuffer_s *udp_wrbuffer_alloc(void);
  ****************************************************************************/
 
 #ifdef CONFIG_NET_UDP_WRITE_BUFFERS
+struct udp_wrbuffer_s;
 FAR struct udp_wrbuffer_s *udp_wrbuffer_timedalloc(unsigned int timeout);
 #endif /* CONFIG_NET_UDP_WRITE_BUFFERS */
 
diff --git a/net/udp/udp_wrbuffer.c b/net/udp/udp_wrbuffer.c
index 45b927d66ff..76c387a7313 100644
--- a/net/udp/udp_wrbuffer.c
+++ b/net/udp/udp_wrbuffer.c
@@ -61,50 +61,6 @@ NET_BUFPOOL_DECLARE(g_wrbuffer, sizeof(struct 
udp_wrbuffer_s),
  * Public Functions
  ****************************************************************************/
 
-/****************************************************************************
- * Name: udp_wrbuffer_alloc
- *
- * Description:
- *   Allocate a UDP write buffer by taking a pre-allocated buffer from
- *   the free list.  This function is called from UDP logic when a buffer
- *   of UDP data is about to sent
- *
- * Input Parameters:
- *   None
- *
- * Assumptions:
- *   Called from user logic with the network locked.
- *
- ****************************************************************************/
-
-FAR struct udp_wrbuffer_s *udp_wrbuffer_alloc(void)
-{
-  FAR struct udp_wrbuffer_s *wrb;
-
-  /* We need to allocate two things:  (1) A write buffer structure and (2)
-   * at least one I/O buffer to start the chain.
-   *
-   * Allocate the write buffer structure first then the IOBG.  In order to
-   * avoid deadlocks, we will need to free the IOB first, then the write
-   * buffer
-   */
-
-  wrb = NET_BUFPOOL_ALLOC(g_wrbuffer);
-  DEBUGASSERT(wrb);
-
-  /* Now get the first I/O buffer for the write buffer structure */
-
-  wrb->wb_iob = net_ioballoc(false);
-  if (!wrb->wb_iob)
-    {
-      nerr("ERROR: Failed to allocate I/O buffer\n");
-      udp_wrbuffer_release(wrb);
-      return NULL;
-    }
-
-  return wrb;
-}
-
 /****************************************************************************
  * Name: udp_wrbuffer_timedalloc
  *
@@ -112,7 +68,6 @@ FAR struct udp_wrbuffer_s *udp_wrbuffer_alloc(void)
  *   Allocate a UDP write buffer by taking a pre-allocated buffer from
  *   the free list.  This function is called from udp logic when a buffer
  *   of udp data is about to sent
- *   This function is wrapped version of udp_wrbuffer_alloc(),
  *   this wait will be terminated when the specified timeout expires.
  *
  * Input Parameters:
@@ -143,7 +98,7 @@ FAR struct udp_wrbuffer_s *udp_wrbuffer_timedalloc(unsigned 
int timeout)
 
   /* Now get the first I/O buffer for the write buffer structure */
 
-  wrb->wb_iob = net_iobtimedalloc(true, timeout);
+  wrb->wb_iob = iob_timedalloc(true, timeout);
 
   /* Did we get an IOB?  We should always get one except under some really
    * weird error conditions.
@@ -201,11 +156,10 @@ FAR struct udp_wrbuffer_s *udp_wrbuffer_tryalloc(void)
 
   /* Now get the first I/O buffer for the write buffer structure */
 
-  wrb->wb_iob =
 #ifdef CONFIG_NET_JUMBO_FRAME
-    iob_alloc_dynamic(len);
+  wrb->wb_iob = iob_alloc_dynamic(len);
 #else
-    iob_tryalloc(false);
+  wrb->wb_iob = iob_tryalloc(false);
 #endif
   if (!wrb->wb_iob)
     {
diff --git a/net/utils/net_lock.c b/net/utils/net_lock.c
index eba40a483db..59ce70429c7 100644
--- a/net/utils/net_lock.c
+++ b/net/utils/net_lock.c
@@ -42,12 +42,6 @@
 
 #include "utils/utils.h"
 
-/****************************************************************************
- * Pre-processor Definitions
- ****************************************************************************/
-
-#define NO_HOLDER (INVALID_PROCESS_ID)
-
 /****************************************************************************
  * Private Data
  ****************************************************************************/
@@ -72,8 +66,8 @@ int net_sem_timedwait2(FAR sem_t *sem, bool interruptible,
   int          blresult2 = -ENOENT;
   int          ret;
 
-  /* Release the network lock, remembering my count.  net_breaklock will
-   * return a negated value if the caller does not hold the network lock.
+  /* Release the network lock, remembering my count.  breaklock will return
+   * a negated value if the caller does not hold the network lock.
    */
 
   if (mutex1 != NULL)
@@ -150,28 +144,6 @@ int net_lock(void)
   return nxrmutex_lock(&g_netlock);
 }
 
-/****************************************************************************
- * Name: net_trylock
- *
- * Description:
- *   Try to take the network lock only when it is currently not locked.
- *   Otherwise, it locks the semaphore.  In either
- *   case, the call returns without blocking.
- *
- * Input Parameters:
- *   None
- *
- * Returned Value:
- *   Zero (OK) is returned on success; a negated errno value is returned on
- *   failure (probably -EAGAIN).
- *
- ****************************************************************************/
-
-int net_trylock(void)
-{
-  return nxrmutex_trylock(&g_netlock);
-}
-
 /****************************************************************************
  * Name: net_unlock
  *
@@ -321,12 +293,12 @@ int net_sem_wait_uninterruptible(FAR sem_t *sem)
 #ifdef CONFIG_MM_IOB
 
 /****************************************************************************
- * Name: net_timedalloc
+ * Name: conn_dev_iobtimedalloc
  *
  * Description:
  *   Allocate an IOB.  If no IOBs are available, then atomically wait for
  *   for the IOB while temporarily releasing the lock on the network.
- *   This function is wrapped version of nxsem_tickwait(), this wait will
+ *   This function is wrapped version of conn_dev_ioballoc(), this wait will
  *   be terminated when the specified timeout expires.
  *
  *   Caution should be utilized.  Because the network lock is relinquished
@@ -337,6 +309,8 @@ int net_sem_wait_uninterruptible(FAR sem_t *sem)
  * Input Parameters:
  *   throttled  - An indication of the IOB allocation is "throttled"
  *   timeout    - The relative time to wait until a timeout is declared.
+ *   sconn      - The socket connection structure.
+ *   dev        - The network device structure.
  *
  * Returned Value:
  *   A pointer to the newly allocated IOB is returned on success.  NULL is
@@ -344,33 +318,58 @@ int net_sem_wait_uninterruptible(FAR sem_t *sem)
  *
  ****************************************************************************/
 
-FAR struct iob_s *net_iobtimedalloc(bool throttled, unsigned int timeout)
+FAR struct iob_s *
+conn_dev_iobtimedalloc(bool throttled, unsigned int timeout,
+                       FAR struct socket_conn_s *sconn,
+                       FAR struct net_driver_s *dev)
 {
   FAR struct iob_s *iob;
+  unsigned int count1 = 0;
+  unsigned int count2 = 0;
+  int          blresult1 = -ENOENT;
+  int          blresult2 = -ENOENT;
 
   iob = iob_tryalloc(throttled);
-  if (iob == NULL && timeout != 0)
+  if (iob != NULL || timeout == 0)
     {
-      unsigned int count;
-      int blresult;
+      return iob;
+    }
 
-      /* There are no buffers available now.  We will have to wait for one to
-       * become available. But let's not do that with the network locked.
-       */
+  /* Release the network lock, remembering my count.  breaklock will return
+   * a negated value if the caller does not hold the network lock.
+   */
 
-      blresult = net_breaklock(&count);
-      iob      = iob_timedalloc(throttled, timeout);
-      if (blresult >= 0)
-        {
-          net_restorelock(count);
-        }
+  if (sconn != NULL)
+    {
+      blresult1 = nxrmutex_breaklock(&sconn->s_lock, &count1);
+    }
+
+  if (dev != NULL)
+    {
+      blresult2 = nxrmutex_breaklock(&dev->d_lock, &count2);
+    }
+
+  /* Now take the semaphore, waiting if so requested. */
+
+  iob = iob_timedalloc(throttled, timeout);
+
+  /* Recover the network lock at the proper count (if we held it before) */
+
+  if (blresult2 >= 0)
+    {
+      nxrmutex_restorelock(&dev->d_lock, count2);
+    }
+
+  if (blresult1 >= 0)
+    {
+      nxrmutex_restorelock(&sconn->s_lock, count1);
     }
 
   return iob;
 }
 
 /****************************************************************************
- * Name: net_ioballoc
+ * Name: conn_dev_ioballoc
  *
  * Description:
  *   Allocate an IOB.  If no IOBs are available, then atomically wait for
@@ -383,6 +382,8 @@ FAR struct iob_s *net_iobtimedalloc(bool throttled, 
unsigned int timeout)
  *
  * Input Parameters:
  *   throttled  - An indication of the IOB allocation is "throttled"
+ *   sconn      - The socket connection structure.
+ *   dev        - The network device structure.
  *
  * Returned Value:
  *   A pointer to the newly allocated IOB is returned on success.  NULL is
@@ -390,8 +391,11 @@ FAR struct iob_s *net_iobtimedalloc(bool throttled, 
unsigned int timeout)
  *
  ****************************************************************************/
 
-FAR struct iob_s *net_ioballoc(bool throttled)
+FAR struct iob_s *conn_dev_ioballoc(bool throttled,
+                                    FAR struct socket_conn_s *sconn,
+                                    FAR struct net_driver_s *dev)
 {
-  return net_iobtimedalloc(throttled, UINT_MAX);
+  return conn_dev_iobtimedalloc(throttled, UINT_MAX, sconn, dev);
 }
+
 #endif

Reply via email to