From 4cfc1ea41b9b0516973a37ee417347e34002d061 Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Fri, 18 Apr 2025 14:27:15 +1000 Subject: [PATCH 1/2] wifi: nrf_wifi: net_if: allocate memory before mutex Allocate the memory in `nrf_wifi_if_send` *before* taking the nrf70 global mutex. This prevents the function from deadlocking the application if attempting to send under memory-pressure, since many of the memory release paths also happen under the global mutex. Signed-off-by: Jordan Yates --- drivers/wifi/nrf_wifi/src/net_if.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/drivers/wifi/nrf_wifi/src/net_if.c b/drivers/wifi/nrf_wifi/src/net_if.c index 8cdb9a472bda..633ee48f9013 100644 --- a/drivers/wifi/nrf_wifi/src/net_if.c +++ b/drivers/wifi/nrf_wifi/src/net_if.c @@ -358,6 +358,7 @@ int nrf_wifi_if_send(const struct device *dev, struct nrf_wifi_sys_fmac_dev_ctx *sys_dev_ctx = NULL; struct rpu_host_stats *host_stats = NULL; void *nbuf = NULL; + bool locked = false; if (!dev || !pkt) { LOG_ERR("%s: vif_ctx_zep is NULL", __func__); @@ -371,24 +372,27 @@ int nrf_wifi_if_send(const struct device *dev, goto out; } + /* Allocate packet before locking mutex (blocks until allocation success) */ + nbuf = net_pkt_to_nbuf(pkt); + ret = k_mutex_lock(&vif_ctx_zep->vif_lock, K_FOREVER); if (ret != 0) { LOG_ERR("%s: Failed to lock vif_lock", __func__); - goto out; + goto drop; } + locked = true; rpu_ctx_zep = vif_ctx_zep->rpu_ctx_zep; if (!rpu_ctx_zep || !rpu_ctx_zep->rpu_ctx) { - goto unlock; + goto drop; } sys_dev_ctx = wifi_dev_priv(rpu_ctx_zep->rpu_ctx); host_stats = &sys_dev_ctx->host_stats; - nbuf = net_pkt_to_nbuf(pkt); - if (!nbuf) { - LOG_DBG("Failed to allocate net_pkt"); - host_stats->total_tx_drop_pkts++; - goto out; + + if (nbuf == NULL) { + LOG_ERR("%s: allocation failed", __func__); + goto drop; } #ifdef CONFIG_NRF70_RAW_DATA_TX @@ -415,10 +419,16 @@ int nrf_wifi_if_send(const struct device *dev, #endif /* CONFIG_NRF70_RAW_DATA_TX */ goto unlock; drop: - host_stats->total_tx_drop_pkts++; - nrf_wifi_osal_nbuf_free(nbuf); + if (host_stats != NULL) { + host_stats->total_tx_drop_pkts++; + } + if (nbuf != NULL) { + nrf_wifi_osal_nbuf_free(nbuf); + } unlock: - k_mutex_unlock(&vif_ctx_zep->vif_lock); + if (locked) { + k_mutex_unlock(&vif_ctx_zep->vif_lock); + } #else ARG_UNUSED(dev); ARG_UNUSED(pkt); From 04601459d05547b7986db640d0c4975391781efd Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Fri, 18 Apr 2025 15:24:14 +1000 Subject: [PATCH 2/2] samples: net: zperf: fix `NRF_WIFI_DATA_HEAP_SIZE` Commit e33d9ee4 incorrectly stated that: > Now that nRF70 by default uses zero-copy fine-tune the configuration to get optimal memory while getting peak throughputs. This is incorrect since `NRF_WIFI_ZERO_COPY_TX` is only enabled by default for the nRF54L series. Update the data heap size so that the smaller value is only used when `NRF_WIFI_ZERO_COPY_TX` is enabled. Signed-off-by: Jordan Yates --- samples/net/zperf/Kconfig | 8 ++++++-- samples/net/zperf/boards/nrf7002dk_nrf5340_cpuapp.conf | 1 - 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/samples/net/zperf/Kconfig b/samples/net/zperf/Kconfig index 795613f1a490..a476a9fec3f0 100644 --- a/samples/net/zperf/Kconfig +++ b/samples/net/zperf/Kconfig @@ -1,8 +1,6 @@ # Copyright 2023 NXP # SPDX-License-Identifier: Apache-2.0 -source "Kconfig.zephyr" - config NET_SAMPLE_CODE_RELOCATE bool "Relocate networking code into RAM" select CODE_DATA_RELOCATION @@ -27,3 +25,9 @@ if USB_DEVICE_STACK_NEXT # tree, you cannot use them in your own application. source "samples/subsys/usb/common/Kconfig.sample_usbd" endif + +configdefault NRF_WIFI_DATA_HEAP_SIZE + default 30000 if NRF_WIFI_ZERO_COPY_TX + default 50000 + +source "Kconfig.zephyr" diff --git a/samples/net/zperf/boards/nrf7002dk_nrf5340_cpuapp.conf b/samples/net/zperf/boards/nrf7002dk_nrf5340_cpuapp.conf index 2b7f5e48de16..8907ee8fbbde 100644 --- a/samples/net/zperf/boards/nrf7002dk_nrf5340_cpuapp.conf +++ b/samples/net/zperf/boards/nrf7002dk_nrf5340_cpuapp.conf @@ -4,7 +4,6 @@ CONFIG_NET_PKT_TX_COUNT=28 CONFIG_NET_BUF_RX_COUNT=28 CONFIG_NET_BUF_TX_COUNT=28 CONFIG_NRF70_RX_NUM_BUFS=16 -CONFIG_NRF_WIFI_DATA_HEAP_SIZE=30000 CONFIG_NET_PKT_BUF_TX_DATA_POOL_SIZE=20000 CONFIG_NRF70_MAX_TX_AGGREGATION=4 CONFIG_NRF70_QSPI_LOW_POWER=n