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

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


The following commit(s) were added to refs/heads/master by this push:
     new 54ee939f826 arch/espressif: serialize Wi-Fi RX queue access.
54ee939f826 is described below

commit 54ee939f826730d266a0e23c968f5f670d92364d
Author: aviralgarg05 <[email protected]>
AuthorDate: Tue Mar 31 01:31:19 2026 +0530

    arch/espressif: serialize Wi-Fi RX queue access.
    
    Protect the common Espressif Wi-Fi netdev RX queue with a spinlock. The 
Wi-Fi RX callback enqueues packets while the netdev upper-half RX thread 
dequeues them, and the IOB queue helpers are not internally serialized.
    
    Under sustained receive traffic that race can corrupt the queue state, 
strand IOBs, and degrade throughput over time as reported in issue #16915.
    
    Initialize the lock during device setup and use it when clearing the queue 
on ifup, enqueueing received packets, and dequeuing them for the upper half.
    
    Signed-off-by: aviralgarg05 <[email protected]>
---
 arch/risc-v/src/common/espressif/esp_wlan_netdev.c | 16 +++++++++++++++-
 arch/xtensa/src/common/espressif/esp_wlan_netdev.c | 16 +++++++++++++++-
 2 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/arch/risc-v/src/common/espressif/esp_wlan_netdev.c 
b/arch/risc-v/src/common/espressif/esp_wlan_netdev.c
index 64649cb433a..da4e55157c0 100644
--- a/arch/risc-v/src/common/espressif/esp_wlan_netdev.c
+++ b/arch/risc-v/src/common/espressif/esp_wlan_netdev.c
@@ -117,6 +117,7 @@ struct esp_wlan_priv_s
   struct esp_common_wifi_s *common;
   uint32_t mode;
 
+  spinlock_t rx_lock;
   netpkt_queue_t netdev_rx_queue;
   uint8_t flatbuf[CONFIG_NET_ETH_PKTSIZE];
 };
@@ -212,6 +213,7 @@ static int esp_wlan_ifup(struct netdev_lowerhalf_s *dev)
 {
   struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev;
   struct net_driver_s *netdev = &priv->dev.netdev;
+  irqstate_t flags;
   int ret = OK;
 
 #ifdef CONFIG_NET_IPv4
@@ -228,7 +230,9 @@ static int esp_wlan_ifup(struct netdev_lowerhalf_s *dev)
 
   /* Clear RX queue */
 
+  flags = spin_lock_irqsave(&priv->rx_lock);
   netpkt_free_queue(&priv->netdev_rx_queue);
+  spin_unlock_irqrestore(&priv->rx_lock, flags);
 
   /* Start Wi-Fi interface */
 
@@ -349,7 +353,13 @@ static int esp_wlan_transmit(struct netdev_lowerhalf_s 
*dev,
 static netpkt_t *esp_wlan_receive(struct netdev_lowerhalf_s *dev)
 {
   struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev;
-  netpkt_t *pkt = netpkt_remove_queue(&priv->netdev_rx_queue);
+  irqstate_t flags;
+  netpkt_t *pkt;
+
+  flags = spin_lock_irqsave(&priv->rx_lock);
+  pkt = netpkt_remove_queue(&priv->netdev_rx_queue);
+  spin_unlock_irqrestore(&priv->rx_lock, flags);
+
   return pkt;
 }
 
@@ -1008,6 +1018,7 @@ void IRAM_ATTR esp_wifi_tx_done_cb(uint8_t ifidx,
 static int wlan_rx_done(struct esp_wlan_priv_s *priv,
                         void *buffer, uint16_t len, void *eb)
 {
+  irqstate_t flags;
   int ret = OK;
   netpkt_t *pkt = NULL;
 
@@ -1025,7 +1036,9 @@ static int wlan_rx_done(struct esp_wlan_priv_s *priv,
       goto out;
     }
 
+  flags = spin_lock_irqsave(&priv->rx_lock);
   ret = netpkt_tryadd_queue(pkt, &priv->netdev_rx_queue);
+  spin_unlock_irqrestore(&priv->rx_lock, flags);
   if (ret < 0)
     {
       wlerr("ERROR: Failed to add packet to queue\n");
@@ -1221,6 +1234,7 @@ static int esp_wlan_initialize(uint32_t mode)
   priv->dev.rxtype           = NETDEV_RX_THREAD;
   priv->dev.priority         = 100;
 
+  spin_lock_init(&priv->rx_lock);
   IOB_QINIT(&priv->netdev_rx_queue);
 
   /* Register RX done callback. Called when the RX packet is received. */
diff --git a/arch/xtensa/src/common/espressif/esp_wlan_netdev.c 
b/arch/xtensa/src/common/espressif/esp_wlan_netdev.c
index 1b0c69347b6..bd0a30fbc60 100644
--- a/arch/xtensa/src/common/espressif/esp_wlan_netdev.c
+++ b/arch/xtensa/src/common/espressif/esp_wlan_netdev.c
@@ -117,6 +117,7 @@ struct esp_wlan_priv_s
   struct esp_common_wifi_s *common;
   uint32_t mode;
 
+  spinlock_t rx_lock;
   netpkt_queue_t netdev_rx_queue;
   uint8_t flatbuf[CONFIG_NET_ETH_PKTSIZE];
 };
@@ -212,6 +213,7 @@ static int esp_wlan_ifup(struct netdev_lowerhalf_s *dev)
 {
   struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev;
   struct net_driver_s *netdev = &priv->dev.netdev;
+  irqstate_t flags;
   int ret = OK;
 
 #ifdef CONFIG_NET_IPv4
@@ -228,7 +230,9 @@ static int esp_wlan_ifup(struct netdev_lowerhalf_s *dev)
 
   /* Clear RX queue */
 
+  flags = spin_lock_irqsave(&priv->rx_lock);
   netpkt_free_queue(&priv->netdev_rx_queue);
+  spin_unlock_irqrestore(&priv->rx_lock, flags);
 
   /* Start Wi-Fi interface */
 
@@ -349,7 +353,13 @@ static int esp_wlan_transmit(struct netdev_lowerhalf_s 
*dev,
 static netpkt_t *esp_wlan_receive(struct netdev_lowerhalf_s *dev)
 {
   struct esp_wlan_priv_s *priv = (struct esp_wlan_priv_s *)dev;
-  netpkt_t *pkt = netpkt_remove_queue(&priv->netdev_rx_queue);
+  irqstate_t flags;
+  netpkt_t *pkt;
+
+  flags = spin_lock_irqsave(&priv->rx_lock);
+  pkt = netpkt_remove_queue(&priv->netdev_rx_queue);
+  spin_unlock_irqrestore(&priv->rx_lock, flags);
+
   return pkt;
 }
 
@@ -1008,6 +1018,7 @@ void IRAM_ATTR esp_wifi_tx_done_cb(uint8_t ifidx,
 static int wlan_rx_done(struct esp_wlan_priv_s *priv,
                         void *buffer, uint16_t len, void *eb)
 {
+  irqstate_t flags;
   int ret = OK;
   netpkt_t *pkt = NULL;
 
@@ -1025,7 +1036,9 @@ static int wlan_rx_done(struct esp_wlan_priv_s *priv,
       goto out;
     }
 
+  flags = spin_lock_irqsave(&priv->rx_lock);
   ret = netpkt_tryadd_queue(pkt, &priv->netdev_rx_queue);
+  spin_unlock_irqrestore(&priv->rx_lock, flags);
   if (ret != OK)
     {
       wlerr("ERROR: Failed to add packet to queue\n");
@@ -1224,6 +1237,7 @@ static int esp_wlan_initialize(uint32_t mode)
   priv->dev.rxtype           = NETDEV_RX_THREAD;
   priv->dev.priority         = 100;
 
+  spin_lock_init(&priv->rx_lock);
   IOB_QINIT(&priv->netdev_rx_queue);
 
   /* Register RX done callback. Called when the RX packet is received. */

Reply via email to