Skip to contentSkip to conversation

net: mana: Allocate queue MSI-X vectors on demand

PATCH Applied Erni Sri Satya Vennela 3 participants 3 messages Nov 6 – Nov 14

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

PATCH 1/1 net: mana: Allocate queue MSI-X vectors on demand

MANA allocates its entire MSI-X budget at probe time, even when the active event queues (EQs) use only a small part of it. With dynamic MSI-X, this budget can be num_online_cpus() + 1, leaving unused vectors that consume interrupt resources.

Remove eager allocation of queue vectors and use mana_gd_get_gic(), which already allocates a vector when an EQ needs one. Keep the HWC interrupt available during device initialization.

Assign affinity as each vector is created instead of spreading a preallocated array. Account for both Ethernet queue limits and RNIC completion vectors when choosing NUMA-aware or linear placement, since RNIC can use more vectors than the Ethernet limit. Wrap excess vector positions over the online CPUs if CPUs have gone offline.

Measured results with one vPort per setup:

vCPUs IRQs before IRQs after Affinity before Affinity after
128 129 65 Linear NUMA-aware
192 193 65 Linear NUMA-aware
372 256 65 NUMA-aware NUMA-aware

Walk the interrupt-context xarray during PCI teardown rather than using the preallocated vector count, so sparse, on-demand allocations are released correctly. Leave CDX's preallocated vector pool unchanged.

---
 .../net/ethernet/microsoft/mana/gdma_main.c   |  50 +++--
 .../net/ethernet/microsoft/mana/gdma_pci.c    | 174 +++++-------------
 include/net/mana/gdma.h                       |   4 +
 3 files changed, 77 insertions(+), 151 deletions(-)
drivers/net/ethernet/microsoft/mana/gdma_main.c +32 -18
index ae0ed700b3b9..d33abb5b0a6f 100644
@@ -1802,44 +1802,45 @@ struct gdma_irq_context *mana_gd_get_gic(struct gdma_context *gc,
 	const struct gdma_bus_ops *ops = gc->bus_ops;
 	struct gdma_irq_context *gic;
 	bool dyn_msix = false;
-	int msi, irq, err;
+	int msix_index, irq, err;
 
 	mutex_lock(&gc->gic_mutex);
 
 	if (use_msi_bitmap) {
-		msi = find_first_zero_bit(gc->msi_bitmap, gc->num_msix_usable);
-		if (msi >= gc->num_msix_usable) {
+		msix_index = find_first_zero_bit(gc->msi_bitmap,
+						 gc->num_msix_usable);
+		if (msix_index >= gc->num_msix_usable) {
 			dev_err(gc->dev, "No free MSI vectors available\n");
 			gic = ERR_PTR(-ENOSPC);
 			goto out;
 		}
-		*msi_requested = msi;
+		*msi_requested = msix_index;
 	} else {
-		msi = *msi_requested;
+		msix_index = *msi_requested;
 	}
 
-	gic = xa_load(&gc->irq_contexts, msi);
+	gic = xa_load(&gc->irq_contexts, msix_index);
 	if (gic) {
 		refcount_inc(&gic->refcount);
 		if (use_msi_bitmap) {
 			gic->bitmap_refs++;
-			set_bit(msi, gc->msi_bitmap);
+			set_bit(msix_index, gc->msi_bitmap);
 		}
 		goto out;
 	}
 
-	irq = ops->msix_virq(gc, msi);
+	irq = ops->msix_virq(gc, msix_index);
 	if (irq == -EINVAL) {
 		/* A bus that sizes its vector pool up front has nothing left
 		 * to hand out once every vector has been claimed.
 		 */
 		if (!ops->msix_alloc_at) {
-			dev_err(gc->dev, "No IRQ for MSI %d\n", msi);
+			dev_err(gc->dev, "No IRQ for MSI %d\n", msix_index);
 			gic = ERR_PTR(-ENOENT);
 			goto out;
 		}
 
-		irq = ops->msix_alloc_at(gc, &msi);
+		irq = ops->msix_alloc_at(gc, &msix_index);
 		if (irq < 0) {
 			dev_err(gc->dev, "Failed to alloc irq msi %d err %d\n",
 				*msi_requested, irq);
@@ -1848,19 +1849,19 @@ struct gdma_irq_context *mana_gd_get_gic(struct gdma_context *gc,
 		}
 
 		dyn_msix = true;
-		*msi_requested = msi;
+		*msi_requested = msix_index;
 	}
 
 	gic = kzalloc_obj(*gic);
 	if (!gic) {
 		gic = ERR_PTR(-ENOMEM);
 		if (dyn_msix)
-			ops->msix_free(gc, msi, irq);
+			ops->msix_free(gc, msix_index, irq);
 		goto out;
 	}
 
 	gic->handler = mana_gd_process_eq_events;
-	gic->msi = msi;
+	gic->msi = msix_index;
 	gic->irq = irq;
 	INIT_LIST_HEAD(&gic->eq_list);
 	spin_lock_init(&gic->lock);
@@ -1879,28 +1880,41 @@ struct gdma_irq_context *mana_gd_get_gic(struct gdma_context *gc,
 		kfree(gic);
 		gic = ERR_PTR(err);
 		if (dyn_msix)
-			ops->msix_free(gc, msi, irq);
+			ops->msix_free(gc, msix_index, irq);
 		goto out;
 	}
 
+	if (dyn_msix && ops->setup_dyn_affinity) {
+		err = ops->setup_dyn_affinity(gc, irq, msix_index);
+		if (err) {
+			free_irq(irq, gic);
+			kfree(gic);
+			gic = ERR_PTR(err);
+			ops->msix_free(gc, msix_index, irq);
+			goto out;
+		}
+	}
+
 	gic->dyn_msix = dyn_msix;
 	refcount_set(&gic->refcount, 1);
 	gic->bitmap_refs = use_msi_bitmap ? 1 : 0;
 
-	err = xa_err(xa_store(&gc->irq_contexts, msi, gic, GFP_KERNEL));
+	err = xa_err(xa_store(&gc->irq_contexts, msix_index, gic, GFP_KERNEL));
 	if (err) {
 		dev_err(gc->dev, "Failed to store irq context for msi %d: %d\n",
-			msi, err);
+			msix_index, err);
+		if (dyn_msix)
+			irq_update_affinity_hint(irq, NULL);
 		free_irq(irq, gic);
 		kfree(gic);
 		gic = ERR_PTR(err);
 		if (dyn_msix)
-			ops->msix_free(gc, msi, irq);
+			ops->msix_free(gc, msix_index, irq);
 		goto out;
 	}
 
 	if (use_msi_bitmap)
-		set_bit(msi, gc->msi_bitmap);
+		set_bit(msix_index, gc->msi_bitmap);
 
 out:
 	mutex_unlock(&gc->gic_mutex);
drivers/net/ethernet/microsoft/mana/gdma_pci.c +41 -133

250 lines not on this page · raw message

include/net/mana/gdma.h +4 -0
index c610fc1067e0..fc8a0ef6c74e 100644
@@ -443,6 +443,8 @@ struct gdma_context;
  *	allocate their whole pool up front leave this NULL.
  * @msix_free: Release a vector obtained from @msix_alloc_at. Only needed
  *	by buses that implement @msix_alloc_at.
+ * @setup_dyn_affinity: Place a vector obtained from @msix_alloc_at on a
+ *	CPU. Only needed by buses that implement @msix_alloc_at.
  * @msix_vec_count: Return the number of entries in the device MSI-X table,
  *	used to clamp the vector count the core asks for. Buses that cannot
  *	report a table size leave this NULL and the clamp is skipped.
@@ -465,6 +467,8 @@ struct gdma_bus_ops {
 	int (*msix_virq)(struct gdma_context *gc, int msi);
 	int (*msix_alloc_at)(struct gdma_context *gc, int *msi);
 	void (*msix_free)(struct gdma_context *gc, int msi, int irq);
+	int (*setup_dyn_affinity)(struct gdma_context *gc, unsigned int irq,
+				  unsigned int msix_index);
 	int (*msix_vec_count)(struct gdma_context *gc);
 	int (*setup_hwc_irqs)(struct gdma_context *gc);
 	int (*setup_remaining_irqs)(struct gdma_context *gc);

base-commit: 98c2ad4f5d805dbc1ea436a334ee59311806a48c

Signature · 1 line
2.34.1
#1 lore raw
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 2

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [Medium] On PCI devices with dynamic MSI-X, queue MSI-X vectors used to be created at probe and kept for the life of the device.
- [Low] The kernel-doc for struct gdma_bus_ops in include/net/mana/gdma.h is now wrong for the PCI bus.
- [Low] mana_gd_remove_irqs() now calls mana_gd_put_gic(gc, false, msi) once for every entry in gc->irq_contexts, under a new comment, "Only…

--

net: mana: Allocate queue MSI-X vectors on demand

This commit stops allocating every queue MSI-X vector at probe time on PCI devices with dynamic MSI-X. Vectors are now allocated by mana_gd_get_gic() when an EQ needs one. A new setup_dyn_affinity bus op sets affinity for each vector as it is created, and PCI teardown now walks the irq_contexts xarray.

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/gdma_pci.c +0 -0
index ed8925296c8a7..5aebf57eb4125 100644

[ ... ]

Quoted 13 lines
drivers/net/ethernet/microsoft/mana/gdma_pci.c +1 -8
@@ -564,17 +485,12 @@ static int mana_gd_setup_remaining_irqs(struct pci_dev *pdev)
 static void mana_gd_remove_irqs(struct pci_dev *pdev)
 {
 	struct gdma_context *gc = pci_get_drvdata(pdev);
-	int i;
-
-	if (gc->max_num_msix < 1)
-		return;
-
-	for (i = 0; i < gc->max_num_msix; i++) {
-		if (!xa_load(&gc->irq_contexts, i))
-			continue;
+	struct gdma_irq_context *gic;

drivers/net/ethernet/microsoft/mana/gdma_pci.c static int mana_gd_setup_remaining_irqs(struct pci_dev *pdev) · line 496

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/gdma_pci.c +4 -2
+	unsigned long msi;
 
-		mana_gd_put_gic(gc, false, i);
-	}
+	/* Only the vectors an EQ asked for were ever created */
+	xa_for_each(&gc->irq_contexts, msi, gic)
+		mana_gd_put_gic(gc, false, msi);
 
 	WARN_ON(!xa_empty(&gc->irq_contexts));
 

[Severity: Low]
Is this comment accurate? mana_gd_setup_irqs() still creates a gic with a setup reference for every vector it allocates, before any EQ asks for one. In dynamic mode that is the HWC vector 0. In msi_sharing mode it is the whole 0..nvec-1 pool:

mana_gd_setup_irqs() {
    ...
	for (i = 0; i < nvec; i++) {
		msi = i;
		gic = mana_gd_get_gic(gc, false, &msi);
    ...
}

This loop also drops one reference per entry. For a dynamically created vector at index > 0, the only references now belong to EQ owners (mana_create_eq() in mana_en, and the comp/fatal EQs in mana_ib). So this put drops a reference the PCI layer never took.

Every current teardown path runs mana_rdma_remove() and mana_remove() before mana_gd_cleanup(), so I don't think this can cause a use-after-free today.

Before this patch, a single leaked EQ reference on a dynamic vector left the refcount above zero after this put, and the WARN_ON(!xa_empty()) below fired. Now that reference is consumed without a warning and the vector is freed. The leaking owner's later put would then hit WARN_ON(!gic) in mana_gd_put_gic(). After a resume or reset it could instead drop a reference on a new gic at the same index.

Could this put only the references the PCI layer itself took, so the WARN_ON still catches such leaks?

[ ... ]

drivers/net/ethernet/microsoft/mana/gdma_pci.c static int mana_pci_setup_hwc_irqs(struct gdma_context *gc) · line 603

Quoted 12 lines
drivers/net/ethernet/microsoft/mana/gdma_pci.c +0 -8
@@ -593,14 +508,6 @@ static int mana_pci_setup_hwc_irqs(struct gdma_context *gc)
 
 static int mana_pci_setup_remaining_irqs(struct gdma_context *gc)
 {
-	int err;
-
-	err = mana_gd_setup_remaining_irqs(to_pci_dev(gc->dev));
-	if (err) {
-		dev_err(gc->dev, "Failed to setup remaining IRQs: %d", err);
-		return err;
-	}
-

[Severity: Medium]
With the probe-time allocation gone, are the queue MSI-X vectors on dynamic MSI-X PCI devices now freed and re-created on every queue reconfiguration?

Before this patch, mana_gd_setup_dyn_irqs() took a base reference on each gic at indices 1..num_msix_usable-1. The EQ put in mana_destroy_eq() then only decremented the refcount. Now the EQ holds the only reference:

mana_detach()
mana_dealloc_queues()

    mana_destroy_eq()
      mana_gd_put_gic(gc, !gc->msi_sharing, msi)
        refcount_dec_and_test() returns true
        irq_update_affinity_hint(irq, NULL)
        free_irq(irq, gic)
        ops->msix_free()          /* pci_msix_free_irq() */

and on the way back up:

mana_attach()
mana_alloc_queues()

    mana_create_eq()
      mana_gd_get_gic()
        ops->msix_virq() returns -EINVAL
        ops->msix_alloc_at()      /* pci_msix_alloc_irq_at() */
        ops->setup_dyn_affinity()

This path runs on ifdown/ifup, MTU and channel changes, XDP attach/detach, and any other mana_detach()/mana_attach() cycle.

mana_pci_setup_dyn_affinity() puts the driver's default placement back on each new vector. Does that mean affinity set through /proc/irq/N/smp_affinity, by an admin or by irqbalance, is lost on each of these operations?

pci_msix_alloc_irq_at() can also return a different Linux IRQ number, which affects tools that pin IRQs by number.

That allocation can also fail at runtime with -ENOSPC or -ENOMEM. Could an MTU change or an ifdown/ifup then leave the interface unable to come back up? Before this patch the vectors were reserved once at probe.

The commit message only describes the lower steady-state IRQ count. Should it also say that vectors are now torn down and rebuilt on every queue reconfiguration?

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/gdma_pci.c +0 -0
 	if (!gc->msi_sharing) {
 		gc->msi_bitmap = bitmap_zalloc(gc->num_msix_usable, GFP_KERNEL);
 		if (!gc->msi_bitmap)

[ ... ]

include/net/mana/gdma.h struct gdma_context; · line 450

Quoted 13 lines
include/net/mana/gdma.h +2 -0
index c610fc1067e0d..fc8a0ef6c74e4 100644
@@ -443,6 +443,8 @@ struct gdma_context;
  *	allocate their whole pool up front leave this NULL.
  * @msix_free: Release a vector obtained from @msix_alloc_at. Only needed
  *	by buses that implement @msix_alloc_at.
+ * @setup_dyn_affinity: Place a vector obtained from @msix_alloc_at on a
+ *	CPU. Only needed by buses that implement @msix_alloc_at.
  * @msix_vec_count: Return the number of entries in the device MSI-X table,
  *	used to clamp the vector count the core asks for. Buses that cannot
  *	report a table size leave this NULL and the clamp is skipped.

[Severity: Low]
This isn't a bug, but this patch already edits this kernel-doc block. Should the @setup_remaining_irqs and @remove_irqs entries be updated too? They still say:

* @setup_remaining_irqs: Set up the remaining interrupts once the device
* resource limits are known.
* @remove_irqs: Release the interrupts set up by @setup_hwc_irqs and
* @setup_remaining_irqs.

After this patch, mana_pci_setup_remaining_irqs() sets up no interrupts. It only allocates msi_bitmap and sets bit 0.

Queue vectors are now created in mana_gd_get_gic() through @msix_alloc_at and @setup_dyn_affinity. They are freed by mana_gd_put_gic() when the last EQ reference is dropped.

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261106063011.43293-1-ernis%40linux.microsoft.com
#2 lore raw
In reply to Erni Sri Satya Vennela
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

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

Quoted 10 lines · On Thu, 5 Nov 2026 22:30:11 -0800 you wrote:

MANA allocates its entire MSI-X budget at probe time, even when the active event queues (EQs) use only a small part of it. With dynamic MSI-X, this budget can be num_online_cpus() + 1, leaving unused vectors that consume interrupt resources.

Remove eager allocation of queue vectors and use mana_gd_get_gic(), which already allocates a vector when an EQ needs one. Keep the HWC interrupt available during device initialization.

[...]

Here is the summary with links:
- [net-next] net: mana: Allocate queue MSI-X vectors on demand

    https://git.kernel.org/netdev/net-next/c/6964ff21c5d8

You are awesome, thank you!

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