Skip to contentSkip to conversation

net: mana: Avoid queue struct allocation failure under memory fragmentation

PATCH Applied Aditya Garg 3 participants 5 messages May 2 – May 6

Subsystems (6)
lore
Share & tools
lore
#0 lore raw

v3 cover net: mana: Avoid queue struct allocation failure under memory fragmentation

The MANA driver can fail to load on systems with high memory utilization because several allocations in the queue setup paths require large physically contiguous blocks via kmalloc. Under memory fragmentation these high-order allocations may fail, preventing the driver from creating queues when opening the interface or when reconfiguring channels, ring parameters or MTU at runtime.

Allocation sizes that are problematic:

mana_create_txq -> tx_qp flat array (sizeof(mana_tx_qp) = 35528):

    16 queues (default): 35528 * 16 =  ~555 KB contiguous
    64 queues (max):     35528 * 64 = ~2220 KB contiguous

mana_create_rxq -> rxq struct with flex array (sizeof(mana_rxq) = 35712, rx_oobs=296 per entry):

    depth 1024 (default): 35712 + 296 * 1024 =  ~331 KB per queue
    depth 8192 (max):     35712 + 296 * 8192 = ~2403 KB per queue

mana_pre_alloc_rxbufs -> rxbufs_pre and das_pre arrays:

    16 queues, depth 1024 (default): 16 * 1024 * 8 =  128 KB each
    64 queues, depth 8192 (max):     64 * 8192 * 8 = 4096 KB each

This series addresses the issue by:
1. Converting the tx_qp flat array into an array of pointers with per-queue kvzalloc (~35 KB each), replacing a single contiguous allocation that can reach ~2.2 MB at 64 queues.
2. Switching rxbufs_pre, das_pre, and rxq allocations to kvmalloc/kvzalloc so the allocator can fall back to vmalloc when contiguous memory is unavailable.

Throughput testing confirms no regression. Since kvmalloc falls back to vmalloc under memory fragmentation, all kvmalloc calls were temporarily replaced with vmalloc to simulate the fallback path (iperf3, GBits/sec):

Physically contiguous vmalloc region
Connections TX RX TX RX
--------------------------------------------------------------

  1                47.2        46.9            46.8        46.6
  16               181         181             181         181
  32               181         181             181         181
  64               181         181             181         181

---
Changes in v3:
- Rebased to latest net-next (net-next reopened)

Changes in v2:
- Rebased to latest net-next

Aditya Garg (2):
net: mana: Use per-queue allocation for tx_qp to reduce allocation
size
net: mana: Use kvmalloc for large RX queue and buffer allocations

 .../net/ethernet/microsoft/mana/mana_bpf.c    |  2 +-
 drivers/net/ethernet/microsoft/mana/mana_en.c | 61 +++++++++++--------
 .../ethernet/microsoft/mana/mana_ethtool.c    |  2 +-
 include/net/mana/mana.h                       |  2 +-
 4 files changed, 39 insertions(+), 28 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/2 net: mana: Use per-queue allocation for tx_qp to reduce allocation size

Convert tx_qp from a single contiguous array allocation to per-queue individual allocations. Each mana_tx_qp struct is approximately 35KB. With many queues (e.g., 32/64), the flat array requires a single contiguous allocation that can fail under memory fragmentation.

Change mana_tx_qp *tx_qp to mana_tx_qp **tx_qp (array of pointers), allocating each queue's mana_tx_qp individually via kvzalloc. This reduces each allocation to ~35KB and provides vmalloc fallback, avoiding allocation failure due to fragmentation.

---
 .../net/ethernet/microsoft/mana/mana_bpf.c    |  2 +-
 drivers/net/ethernet/microsoft/mana/mana_en.c | 49 ++++++++++++-------
 .../ethernet/microsoft/mana/mana_ethtool.c    |  2 +-
 include/net/mana/mana.h                       |  2 +-
 4 files changed, 33 insertions(+), 22 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_bpf.c +1 -1
index 7697c9b52ed3..b5e9bb184a1d 100644
@@ -68,7 +68,7 @@ int mana_xdp_xmit(struct net_device *ndev, int n, struct xdp_frame **frames,
 		count++;
 	}
 
-	tx_stats = &apc->tx_qp[q_idx].txq.stats;
+	tx_stats = &apc->tx_qp[q_idx]->txq.stats;
 
 	u64_stats_update_begin(&tx_stats->syncp);
 	tx_stats->xdp_xmit += count;
drivers/net/ethernet/microsoft/mana/mana_en.c +30 -19

119 lines not on this page · raw message

drivers/net/ethernet/microsoft/mana/mana_ethtool.c +1 -1
index 6a4b42fe0944..04350973e19e 100644
@@ -260,7 +260,7 @@ static void mana_get_ethtool_stats(struct net_device *ndev,
 	}
 
 	for (q = 0; q < num_queues; q++) {
-		tx_stats = &apc->tx_qp[q].txq.stats;
+		tx_stats = &apc->tx_qp[q]->txq.stats;
 
 		do {
 			start = u64_stats_fetch_begin(&tx_stats->syncp);
include/net/mana/mana.h +1 -1
index 8f721cd4e4a7..aa90a858c8e3 100644
@@ -507,7 +507,7 @@ struct mana_port_context {
 	bool tx_shortform_allowed;
 	u16 tx_vp_offset;
 
-	struct mana_tx_qp *tx_qp;
+	struct mana_tx_qp **tx_qp;
 
 	/* Indirection Table for RX & TX. The values are queue indexes */
 	u32 *indir_table;
Signature · 1 line
2.43.0
#3 lore raw

PATCH v3 2/2 net: mana: Use kvmalloc for large RX queue and buffer allocations

In reply to Aditya Garg

The RX path allocations for rxbufs_pre, das_pre, and rxq scale with queue count and queue depth. With high queue counts and depth, these can exceed what kmalloc can reliably provide from physically contiguous memory under fragmentation.

Switch these from kmalloc to kvmalloc variants so the allocator transparently falls back to vmalloc when contiguous memory is scarce, and update the corresponding frees to kvfree.

---
 drivers/net/ethernet/microsoft/mana/mana_en.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -6
index 8adf72b96145..e1d8ac3417e8 100644
@@ -685,11 +685,11 @@ void mana_pre_dealloc_rxbufs(struct mana_port_context *mpc)
 		put_page(virt_to_head_page(mpc->rxbufs_pre[i]));
 	}
 
-	kfree(mpc->das_pre);
+	kvfree(mpc->das_pre);
 	mpc->das_pre = NULL;
 
 out2:
-	kfree(mpc->rxbufs_pre);
+	kvfree(mpc->rxbufs_pre);
 	mpc->rxbufs_pre = NULL;
 
 out1:
@@ -806,11 +806,11 @@ int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu, int num_qu
 	num_rxb = num_queues * mpc->rx_queue_size;
 
 	WARN(mpc->rxbufs_pre, "mana rxbufs_pre exists\n");
-	mpc->rxbufs_pre = kmalloc_array(num_rxb, sizeof(void *), GFP_KERNEL);
+	mpc->rxbufs_pre = kvmalloc_array(num_rxb, sizeof(void *), GFP_KERNEL);
 	if (!mpc->rxbufs_pre)
 		goto error;
 
-	mpc->das_pre = kmalloc_objs(dma_addr_t, num_rxb);
+	mpc->das_pre = kvmalloc_objs(dma_addr_t, num_rxb);
 	if (!mpc->das_pre)
 		goto error;
 
@@ -2564,7 +2564,7 @@ static void mana_destroy_rxq(struct mana_port_context *apc,
 	if (rxq->gdma_rq)
 		mana_gd_destroy_queue(gc, rxq->gdma_rq);
 
-	kfree(rxq);
+	kvfree(rxq);
 }
 
 static int mana_fill_rx_oob(struct mana_recv_buf_oob *rx_oob, u32 mem_key,
@@ -2704,7 +2704,7 @@ static struct mana_rxq *mana_create_rxq(struct mana_port_context *apc,
 
 	gc = gd->gdma_context;
 
-	rxq = kzalloc_flex(*rxq, rx_oobs, apc->rx_queue_size);
+	rxq = kvzalloc_flex(*rxq, rx_oobs, apc->rx_queue_size);
 	if (!rxq)
 		return NULL;
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH net-next v3 1/2] net: mana: Use per-queue allocation for tx_qp to reduce allocation size

In reply to Aditya Garg
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] Lockless functions (mana_get_stats64 and mana_xdp_xmit) can experience a TOCTOU Use-After-Free…

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Lockless functions (mana_get_stats64 and mana_xdp_xmit) can experience a TOCTOU Use-After-Free when accessing the dynamically allocated tx_qp arrays during driver teardown.
- [High] Unprotected updates to u64_stats sequence counters in mana_xdp_xmit can corrupt stats synchronization for readers, leading to a hard lockup. --

commit a5fe13810d8b6aacf3c073e5e3abaddcaa39dd5d
Author: Aditya Garg <[email protected]>

net: mana: Use per-queue allocation for tx_qp to reduce allocation size

This commit converts tx_qp from a contiguous array allocation to an array of pointers. It allocates each queue's mana_tx_qp individually via kvzalloc, which provides vmalloc fallback and helps avoid allocation failures due to memory fragmentation.

drivers/net/ethernet/microsoft/mana/mana_bpf.c int mana_xdp_xmit(struct net_device *ndev, int n, struct xdp_frame **frames, · line 71

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_bpf.c +1 -1
index 7697c9b52ed3..b5e9bb184a1d 100644
@@ -68,7 +68,7 @@ int mana_xdp_xmit(struct net_device *ndev, int n, struct xdp_frame **frames,
 		count++;
 	}
 
-	tx_stats = &apc->tx_qp[q_idx].txq.stats;
+	tx_stats = &apc->tx_qp[q_idx]->txq.stats;

Can mana_xdp_xmit() be called concurrently on multiple CPUs that map to the same q_idx?

If the number of online CPUs exceeds the number of TX queues, smp_processor_id() % ndev->real_num_tx_queues will result in multiple CPUs sharing the same queue index.

The subsequent call to u64_stats_update_begin() requires mutual exclusion among writers. Concurrent writes without locks could corrupt the underlying sequence counter, potentially leaving it permanently odd and causing readers to spin infinitely.

Additionally, since this function runs locklessly, could this pointer dereference race with the driver teardown path?

If a teardown thread clears port_is_up right after the early check in mana_xdp_xmit(), and proceeds to call mana_destroy_txq(), it frees the tx_qp elements via kvfree() without an intervening synchronize_net() or RCU grace period. This could lead to a use-after-free when accessing txq.stats.

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
index a654b3699c4c..8adf72b96145 100644

[ ... ]

drivers/net/ethernet/microsoft/mana/mana_en.c static void mana_get_stats64(struct net_device *ndev, · line 617

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +1 -1
@@ -614,7 +614,7 @@ static void mana_get_stats64(struct net_device *ndev,
 	}
 
 	for (q = 0; q < num_queues; q++) {
-		tx_stats = &apc->tx_qp[q].txq.stats;
+		tx_stats = &apc->tx_qp[q]->txq.stats;

Could this suffer from the same use-after-free race with teardown as mentioned above?

Since mana_get_stats64() runs locklessly, it can run concurrently with teardown paths like mana_per_port_queue_reset_work_handler(). The tx_qp array elements might be freed by mana_destroy_txq() while mana_get_stats64() is attempting to read the stats.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#4 lore raw
In reply to Aditya Garg
This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This series was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Sat, 2 May 2026 00:45:32 -0700 you wrote:

The MANA driver can fail to load on systems with high memory utilization because several allocations in the queue setup paths require large physically contiguous blocks via kmalloc. Under memory fragmentation these high-order allocations may fail, preventing the driver from creating queues when opening the interface or when reconfiguring channels, ring parameters or MTU at runtime.

[...]

Here is the summary with links:
- [net-next,v3,1/2] net: mana: Use per-queue allocation for tx_qp to reduce allocation size

    https://git.kernel.org/netdev/net-next/c/d07efe5a6e64
  - [net-next,v3,2/2] net: mana: Use kvmalloc for large RX queue and buffer allocations
    https://git.kernel.org/netdev/net-next/c/3af0820c878e

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html