Skip to contentSkip to conversation

net: mana: reconfigure by replacing the queue set

PATCH Superseded Long Li 3 participants 31 messages Aug 13 – Aug 17

Subsystems (5)

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Long Li

author
#0 lore raw

v2 cover net: mana: reconfigure by replacing the queue set

MANA reconfigures a port by destroying its queues and building them again. mana_detach() tears the whole queue set down, mana_attach() allocates a new one, and every path that changes a queue property does this: the ethtool channel, ring and private-flag setters, the MTU change and XDP attach.

If the second half fails there is nothing left to fall back to. The old queues are already gone, so the port stays down and the failure cannot be undone from userspace:

  # ethtool -G ens1 rx 8192 tx 16384      # under memory pressure
  netlink error: Cannot allocate memory
  mana 7870:00:00.0 ens1: Failed to create 16 TX queues, -12
  mana 7870:00:00.0 ens1: mana_attach failed: -12
  # ip link show ens1
  ... state DOWN

On VM SKUs with no netvsc fallback interface this takes the VM off the network entirely, and it is reachable from an ordinary ethtool ring resize that happens to run when memory is tight.

v1 [1] tried to recover after the fact: schedule a reset and retry mana_attach() with smaller values, falling back to the previous setting or to the defaults. Review pointed out that silently replacing a user's setting with a different one is the wrong behaviour, and that pre-allocating the resources and replacing the queue set looked feasible and should be investigated instead. This series does that, so there is no failure to recover from and no user setting to override.

The model is to build the new queue set alongside the running one, publish it, then retire the old one.

carrier off -> netif_tx_disable -> swap the queue pointers -> update the netdev queue counts -> reprogram RSS/steering -> reattach XDP -> netif_tx_start_all_queues -> restore carrier

Everything that can fail happens before anything is swapped. If allocation fails the running queues have not been touched at all: the ethtool call returns the error, the interface keeps forwarding traffic and the configuration is unchanged. If the swap itself fails, the previous set is put back and the port keeps running on it.

Patch layout:

1 the queue-set allocate/publish/free helpers.
2-6 convert the five callers: ethtool channels, rings and private
flags, the MTU change and XDP attach.
7 the remaining detach/attach users are the TX-timeout reset handler
and suspend; make mana_detach() always finish its teardown so the reset handler cannot leave a port permanently dead.
8-11 keep behaviour the swap model would otherwise change: per-queue
counters move into the port context so a rebuild no longer zeroes the interface statistics, the EQ pool is shared across a swap instead of being duplicated, EQs left idle by a reduction are released, and a user-configured RSS indirection table survives a rebuild.
12-13 stop rebuilding queues that do not change. A reduction keeps the

        surviving queues and an increase keeps the existing ones, so
        32 -> 8 channels destroys 24 queue pairs instead of building 8 and
        destroying 32, and 4 -> 8 builds 4 instead of 8.

Known trade-off, since it is deliberate: RX counters are now indexed by queue number in the port context, so during the window between steering moving to the new queues and the old ones being destroyed, both generations can update one slot. MANA is 64-bit only, so u64_stats_sync carries no seqcount and nothing can be corrupted; at worst a few increments are lost. Serialising them would put a lock in the per-packet receive path, and giving each set its own slots would make ndo_get_stats64() report a dip during a swap, which is the regression this is meant to remove.

Testing, on Standard_D32ds_v6 (MANA) running Ubuntu 24.04:

- the failure above, made deterministic with fail_page_alloc: before the series the port ends up down and unrecoverable; after it, ethtool returns -ENOMEM, the ring sizes are unchanged, the link stays up and traffic is uninterrupted.
- 33-case functional matrix over channel counts 1/2/4/8/32, ring sizes, MTU, the private flag and XDP attach/detach across each, including reconfiguration while the port is administratively down.
- XDP: all four verdicts exercised. PASS and TX across every reconfiguration, DROP and REDIRECT verified by counter and by effect. The program stays attached and RX keeps flowing across channel shrink/grow, ring resize, MTU change and private-flag toggle, with 16.8 Gbit/s of received traffic running through the program while the queue set is replaced underneath it.
- a queue-set allocation forced to fail while an XDP program is attached, to exercise the unpublished-set teardown.
- no KASAN reports, BUGs or WARNs in any of the above.
- every patch builds with W=1 with no new warnings, checkpatch clean.

[1] https://lore.kernel.org/netdev/[email protected]/

Dipayaan Roy (1):
net: mana: do not bail out of mana_detach on dealloc failure

Long Li (12):
net: mana: add queue-set allocation and teardown helpers
net: mana: swap queue sets in mana_set_channels
net: mana: swap queue sets in mana_set_ringparam
net: mana: swap queue sets in mana_set_priv_flags
net: mana: swap queue sets in mana_change_mtu
net: mana: swap queue sets in mana_xdp_set
net: mana: keep per-queue statistics in the port context
net: mana: share the EQ pool across a queue-set swap
net: mana: release EQs left idle by a channel-count reduction
net: mana: keep a user-configured RSS table across a queue rebuild
net: mana: keep the surviving queues when the channel count is reduced
net: mana: keep the existing queues when the channel count is raised

 .../net/ethernet/microsoft/mana/mana_bpf.c    |  110 +-
 drivers/net/ethernet/microsoft/mana/mana_en.c | 1433 +++++++++++++++--
 .../ethernet/microsoft/mana/mana_ethtool.c    |  300 +++-
 include/net/mana/gdma.h                       |   11 +-
 include/net/mana/mana.h                       |  117 +-
 5 files changed, 1746 insertions(+), 225 deletions(-)

base-commit: d67e5dbda22604d0fcde32fce58c65f88676e676

Signature · 1 line
2.43.0
author
#1 lore raw

PATCH v2 1/13 net: mana: add queue-set allocation and teardown helpers

The ethtool reconfiguration paths (channel count, ring size, private flags), mana_change_mtu() and mana_xdp_set() rebuild the queues with mana_detach() then mana_attach(). That tears the vport down, so RDMA can claim it while released, and a failed mana_attach() leaves the port down with no way back but manual intervention.

Add the data model and helpers for pre-allocate and swap: a queue set built, published and torn down independently of the vport, against a scratch port context so the live one never points at queues still being built or freed. Building in place is not an option: mana_start_xmit() dereferences apc->tx_qp[] guarded only by apc->port_is_up.

The TX drain moves out of mana_dealloc_queues() so the new teardown path gets it too, and its fallback reset becomes pci_try_reset_function() rather than an open-coded pcie_flr(), which does not save and restore config space. Trylock because this runs under RTNL while removal takes the device lock first.

No functional change otherwise: nothing calls the new helpers yet.

---
 .../net/ethernet/microsoft/mana/mana_bpf.c    |  31 ++
 drivers/net/ethernet/microsoft/mana/mana_en.c | 477 ++++++++++++++++--
 .../ethernet/microsoft/mana/mana_ethtool.c    |   9 +-
 include/net/mana/mana.h                       |  63 +++
 4 files changed, 538 insertions(+), 42 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_bpf.c +31 -0
index 53308e139cbe917b074dd381c83546fc74d7b79f..ca602e27044f92b87295cbc2de924adc71efa780 100644
@@ -265,3 +265,34 @@ int mana_bpf(struct net_device *ndev, struct netdev_bpf *bpf)
 
 	return ret;
 }
+
+/* Read the XDP program a queue set is running, without changing anything. */
+struct bpf_prog *mana_chn_xdp_peek(struct mana_port_context *apc)
+{
+	ASSERT_RTNL();
+
+	if (!apc->rxqs || !apc->rxqs[0])
+		return NULL;
+
+	return rtnl_dereference(apc->rxqs[0]->bpf_prog);
+}
+
+/* Drop the per-queue references a retiring set holds on @prog.
+ *
+ * Kept separate from mana_chn_setxdp() so the pointers can stay in place
+ * until the queues stop polling: clearing them up front would let packets
+ * already sitting in a retiring RQ take the pass path and reach the stack
+ * without the program ever seeing them.
+ */
+void mana_chn_xdp_release(struct bpf_prog *prog, unsigned int num_queues)
+{
+	unsigned int i;
+
+	ASSERT_RTNL();
+
+	if (!prog)
+		return;
+
+	for (i = 0; i < num_queues; i++)
+		bpf_prog_put(prog);
+}
drivers/net/ethernet/microsoft/mana/mana_en.c +439 -38

531 lines not on this page · raw message

drivers/net/ethernet/microsoft/mana/mana_ethtool.c +5 -4
index ece7ff9cc409a806b6a6de70a85b44874bfa6dad..04b7a5c0fdabc9abc693065c32d4f60fc9ac6809 100644
@@ -648,10 +648,11 @@ static int mana_set_coalesce(struct net_device *ndev,
 	return 0;
 }
 
-/* mana_set_channels - change the number of queues on a port
- *
- * Returns -EBUSY if RDMA holds the vport with EQs sized to the
- * current num_queues.
+/* A count change leaves every surviving queue configured as it was, so
+ * neither direction rebuilds: a reduction retires the tail, an increase
+ * builds only the queues added. On failure the existing queues keep running
+ * and the requested value is never replaced by a fallback. The vport is never
+ * torn down, so RDMA cannot take it mid-reconfiguration.
  */
 static int mana_set_channels(struct net_device *ndev,
 			     struct ethtool_channels *channels)
include/net/mana/mana.h +63 -0

93 lines not on this page · raw message

Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH net-next v2 01/13] net: mana: add queue-set allocation and teardown helpers

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] User-configured RSS indirection table is silently discarded and reset to defaults during…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] User-configured RSS indirection table is silently discarded and reset to defaults during per-queue reconfigurations. --

--- Patch [1]: [PATCH net-next v2 01/13] net: mana: add queue-set allocation and teardown helpers --- commit fcc6a74602d4e66dc287809434c5e9b44c3be829
Author: Long Li <[email protected]>
Subject: net: mana: add queue-set allocation and teardown helpers

The ethtool reconfiguration paths (channel count, ring size, private flags), mana_change_mtu() and mana_xdp_set() rebuild the queues with mana_detach() then mana_attach(). This adds the data model and helpers for pre-allocate and swap to independently build and tear down a queue set.

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0

[ ... ]

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +6 -0
+int mana_alloc_qset(struct mana_port_context *scratch, unsigned int num_queues,
+		    unsigned int rx_queue_size, unsigned int tx_queue_size,
+		    u32 priv_flags, struct mana_qset *out)
+{
+	struct net_device *ndev = scratch->ndev;
+	int err;

[ ... ]

Quoted 7 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +7 -0
+	err = mana_rss_table_alloc(scratch);
+	if (err)
+		goto cleanup_rxq_array;
+
+	err = mana_create_eq(scratch);
+	if (err)
+		goto cleanup_rss;

drivers/net/ethernet/microsoft/mana/mana_en.c static int mana_dealloc_queues(struct net_device *ndev) · line 4019

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +10 -0
+
+	err = mana_create_txq(scratch, ndev);
+	if (err)
+		goto cleanup_eq;
+
+	err = mana_add_rx_queues(scratch, ndev);
+	if (err)
+		goto cleanup_rxq;
+
+	mana_rss_table_init(scratch);

[Severity: Medium]
Will this unconditionally reset the RSS indirection table to defaults during reconfigurations?

Since mana_rss_table_alloc() creates a new table and mana_rss_table_init() initializes it to default round-robin values, it seems like a user's existing custom RSS steering configuration might be silently overwritten when the new queue set is published.

Could this cause a regression where the custom configuration is lost? Should it be preserved by copying the existing table from the active port context instead?

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +3 -0
+
+	mana_qset_snapshot(scratch, out);
+	return 0;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

RE: [EXTERNAL] Re: [PATCH net-next v2 01/13] net: mana: add queue-set allocation and teardown helpers

Quoted 51 lines

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] User-configured RSS indirection table is silently discarded and reset to defaults during per-queue reconfigurations.
--

--- Patch [1]: [PATCH net-next v2 01/13] net: mana: add queue-set allocation and teardown helpers --- commit fcc6a74602d4e66dc287809434c5e9b44c3be829
Author: Long Li <[email protected]>
Subject: net: mana: add queue-set allocation and teardown helpers

The ethtool reconfiguration paths (channel count, ring size, private flags), mana_change_mtu() and mana_xdp_set() rebuild the queues with mana_detach() then mana_attach(). This adds the data model and helpers for pre-allocate and swap to independently build and tear down a queue set.

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0

[ ... ]

Quoted 1 line

+int mana_alloc_qset(struct mana_port_context *scratch, unsigned int

num_queues,

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +4 -0
+		    unsigned int rx_queue_size, unsigned int tx_queue_size,
+		    u32 priv_flags, struct mana_qset *out) {
+	struct net_device *ndev = scratch->ndev;
+	int err;

[ ... ]

Quoted 7 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +7 -0
+	err = mana_rss_table_alloc(scratch);
+	if (err)
+		goto cleanup_rxq_array;
+
+	err = mana_create_eq(scratch);
+	if (err)
+		goto cleanup_rss;

drivers/net/ethernet/microsoft/mana/mana_en.c static int mana_dealloc_queues(struct net_device *ndev) · line 4019

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +10 -0
+
+	err = mana_create_txq(scratch, ndev);
+	if (err)
+		goto cleanup_eq;
+
+	err = mana_add_rx_queues(scratch, ndev);
+	if (err)
+		goto cleanup_rxq;
+
+	mana_rss_table_init(scratch);

[Severity: Medium]
Will this unconditionally reset the RSS indirection table to defaults during reconfigurations?

Quoted 8 lines

Since mana_rss_table_alloc() creates a new table and mana_rss_table_init() initializes it to default round-robin values, it seems like a user's existing custom RSS steering configuration might be silently overwritten when the new queue set is published.

Could this cause a regression where the custom configuration is lost? Should it be preserved by copying the existing table from the active port context instead?

patch 11/13 fixed this.

Long

maintainer
#4 lore raw

Re: [PATCH net-next v2 01/13] net: mana: add queue-set allocation and teardown helpers

In reply to Long Li
Quoted 5 lines · On Wed, 12 Aug 2026 22:04:06 -0700 Long Li wrote:

The ethtool reconfiguration paths (channel count, ring size, private flags), mana_change_mtu() and mana_xdp_set() rebuild the queues with mana_detach() then mana_attach(). That tears the vport down, so RDMA can claim it while released, and a failed mana_attach() leaves the port down with no way back but manual intervention.

The kernel-doc script run with -Wall reports:

Warning: drivers/net/ethernet/microsoft/mana/mana_en.c:3936 No description found for return value of 'mana_qset_scratch_alloc'
Warning: drivers/net/ethernet/microsoft/mana/mana_en.c:3936 No description found for return value of 'mana_qset_scratch_alloc'

Please note that net-next is *closed* until Aug 31st (merge window).

Signature · 1 line
pw-bot: cr
author
#5 lore raw

PATCH v2 2/13 net: mana: swap queue sets in mana_set_channels

In reply to Long Li

Convert the channel count path away from detach/attach: build the new queue set while the current one keeps serving traffic, then swap it in and retire the old one.

An allocation failure now returns the error with the running configuration untouched, instead of leaving the port down, and the value the user asked for is never silently replaced by a fallback. The vport is never released, so RDMA cannot claim it mid-reconfiguration. If the swap itself fails the previous set is put back and the port keeps running on it.

---
 .../net/ethernet/microsoft/mana/mana_bpf.c    |   7 +
 drivers/net/ethernet/microsoft/mana/mana_en.c | 345 ++++++++++++++++--
 .../ethernet/microsoft/mana/mana_ethtool.c    |  84 ++++-
 include/net/mana/mana.h                       |  10 +
 4 files changed, 400 insertions(+), 46 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_bpf.c +7 -0
index ca602e27044f92b87295cbc2de924adc71efa780..e16ce2a0715839594a5837288c1d4c1de412e7fb 100644
@@ -59,6 +59,13 @@ int mana_xdp_xmit(struct net_device *ndev, int n, struct xdp_frame **frames,
 	if (unlikely(!apc->port_is_up))
 		return 0;
 
+	/* Pair with the smp_wmb() in mana_publish_qset(), as mana_start_xmit()
+	 * does. This path is gated only by the flag above, so without the
+	 * barrier it could pick q_idx from a stale real_num_tx_queues and
+	 * index a freshly installed, smaller apc->tx_qp[].
+	 */
+	smp_rmb();
+
 	q_idx = smp_processor_id() % ndev->real_num_tx_queues;
 
 	for (i = 0; i < n; i++) {
drivers/net/ethernet/microsoft/mana/mana_en.c +318 -27

399 lines not on this page · raw message

drivers/net/ethernet/microsoft/mana/mana_ethtool.c +65 -19

107 lines not on this page · raw message

include/net/mana/mana.h +10 -0
index a7b7a00a57f176e9dc889f7b09b6ef56d5608a8e..66a653bad22eba511d8cf8cf71b572c604044ecb 100644
@@ -633,6 +633,13 @@ struct mana_port_context {
 	 */
 	bool channel_changing;
 
+	/* mana_publish_qset() could neither publish the new set nor restore the
+	 * old one. Vport RX is already off; the port still has to be closed,
+	 * which mana_publish_close_if_needed() does once the caller has
+	 * released the set that failed.
+	 */
+	bool publish_dead_end;
+
 	/* Net shaper handle*/
 	struct net_shaper_handle handle;
 
@@ -729,6 +736,9 @@ void mana_qset_scratch_free(struct mana_port_context *scratch);
 int mana_alloc_qset(struct mana_port_context *scratch, unsigned int num_queues,
 		    unsigned int rx_queue_size, unsigned int tx_queue_size,
 		    u32 priv_flags, struct mana_qset *out);
+int mana_publish_qset(struct mana_port_context *apc, struct mana_qset *newq,
+		      struct mana_qset *out_old);
+void mana_publish_close_if_needed(struct mana_port_context *apc);
 void mana_free_qset(struct mana_port_context *scratch, struct mana_qset *qset);
 
 void mana_dim_change(struct mana_cq *cq, bool enable);
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH net-next v2 02/13] net: mana: swap queue sets in mana_set_channels

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] mana_set_channels silently drops user-configured RSS indirection tables --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mana_set_channels silently drops user-configured RSS indirection tables --

--- Patch [2]: [PATCH net-next v2 02/13] net: mana: swap queue sets in mana_set_channels --- commit 20d58255702f027247917ff013e18e0c18e9845c
Author: Long Li <[email protected]>
Subject: net: mana: swap queue sets in mana_set_channels

This converts the channel count path away from detach/attach. It builds the new queue set while the current one keeps serving traffic, then swaps it in and retires the old one.

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0
index 04b7a5c0fdabc..b12291555eaeb 100644

[ ... ]

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +3 -3
@@ -659,42 +659,88 @@ static int mana_set_channels(struct net_device *ndev,
 	apc->channel_changing = true;
 	mutex_unlock(&apc->vport_mutex);
 
-	err = mana_pre_alloc_rxbufs(apc, ndev->mtu, new_count);
-	if (err) {
-		netdev_err(ndev, "Insufficient memory for new allocations");
+	scratch = mana_qset_scratch_alloc(apc);
+	if (!scratch) {
+		err = -ENOMEM;

drivers/net/ethernet/microsoft/mana/mana_ethtool.c static int mana_set_channels(struct net_device *ndev, · line 669

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +2 -5
 		goto clear_flag;
 	}
 
-	err = mana_detach(ndev, false);
-	if (err) {
-		netdev_err(ndev, "mana_detach failed: %d\n", err);
-		goto out;
-	}
+	err = mana_alloc_qset(scratch, new_count, apc->rx_queue_size,
+			      apc->tx_queue_size, apc->priv_flags, &newq);

[Severity: Medium]
Does this silently drop a user-configured RSS indirection table?

By using mana_alloc_qset() here in mana_set_channels(), a custom RSS indirection table configured via ethtool will be lost. When mana_alloc_qset() is called, it unconditionally initializes the table with default values in drivers/net/ethernet/microsoft/mana/mana_en.c:

mana_alloc_qset() {
    ...
    mana_rss_table_init(scratch);
    ...
    mana_qset_snapshot(scratch, out);
}

Because out->rxfh_indir_lost is explicitly set to false, the old indirection table is freed without preserving the entries, and no ethtool_rxfh_indir_lost() notification is sent.

Will this cause the interface to silently revert to a default hashing distribution after a channel count change instead of preserving it?

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +3 -2
+	if (err)
+		goto free_scratch; /* current qset untouched, nothing to undo */
 
-	apc->num_queues = new_count;
-	err = mana_attach(ndev);
+	err = mana_publish_qset(apc, &newq, &oldq);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#7 lore raw

RE: [EXTERNAL] Re: [PATCH net-next v2 02/13] net: mana: swap queue sets in mana_set_channels

Quoted 59 lines

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] mana_set_channels silently drops user-configured RSS indirection tables
--

--- Patch [2]: [PATCH net-next v2 02/13] net: mana: swap queue sets in mana_set_channels --- commit 20d58255702f027247917ff013e18e0c18e9845c
Author: Long Li <[email protected]>
Subject: net: mana: swap queue sets in mana_set_channels

This converts the channel count path away from detach/attach. It builds the new queue set while the current one keeps serving traffic, then swaps it in and retires the old one.

Quoted 5 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0
index 04b7a5c0fdabc..b12291555eaeb 100644

[ ... ]

drivers/net/ethernet/microsoft/mana/mana_ethtool.c static int mana_set_channels(struct net_device

Quoted 1 line
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0
@@ -659,42 +659,88 @@ static int mana_set_channels(struct net_device

*ndev,

Quoted 9 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +3 -3
 	apc->channel_changing = true;
 	mutex_unlock(&apc->vport_mutex);
 
-	err = mana_pre_alloc_rxbufs(apc, ndev->mtu, new_count);
-	if (err) {
-		netdev_err(ndev, "Insufficient memory for new allocations");
+	scratch = mana_qset_scratch_alloc(apc);
+	if (!scratch) {
+		err = -ENOMEM;

drivers/net/ethernet/microsoft/mana/mana_ethtool.c static int mana_set_channels(struct net_device *ndev, · line 726

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +2 -5
 		goto clear_flag;
 	}
 
-	err = mana_detach(ndev, false);
-	if (err) {
-		netdev_err(ndev, "mana_detach failed: %d\n", err);
-		goto out;
-	}
+	err = mana_alloc_qset(scratch, new_count, apc->rx_queue_size,
+			      apc->tx_queue_size, apc->priv_flags, &newq);

[Severity: Medium]
Does this silently drop a user-configured RSS indirection table?

By using mana_alloc_qset() here in mana_set_channels(), a custom RSS indirection table configured via ethtool will be lost. When mana_alloc_qset() is called, it unconditionally initializes the table with default values in drivers/net/ethernet/microsoft/mana/mana_en.c:

mana_alloc_qset() {
    ...
    mana_rss_table_init(scratch);
    ...
    mana_qset_snapshot(scratch, out);
}
Quoted 6 lines

Because out->rxfh_indir_lost is explicitly set to false, the old indirection table is freed without preserving the entries, and no ethtool_rxfh_indir_lost() notification is sent.

Will this cause the interface to silently revert to a default hashing distribution after a channel count change instead of preserving it?

This is a pre-existing upstream behavior. Patch 11/13 adds  mana_rss_table_keep() and fixes it.

Long

author
#8 lore raw

PATCH v2 3/13 net: mana: swap queue sets in mana_set_ringparam

In reply to Long Li

Convert the ring size path to pre-allocate and swap, for the same reasons as the channel count path: an allocation failure returns the error with the running configuration untouched, and the vport is never released.

This drops the fallback on failure. Previously a failed mana_attach() was retried with the previous values, or the defaults, or the minimums, so a user who asked for a specific size could end up with a different one with no indication beyond dmesg. There is nothing to recover from now, so the error is returned.

Also return early when the requested sizes round to the values already in use.

---
 .../ethernet/microsoft/mana/mana_ethtool.c    | 74 +++++++++++++------
 1 file changed, 51 insertions(+), 23 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +51 -23
index b12291555eaeb4fafc305ffe7d9e550ec01952c8..40e9886c87a81c86080e52955a0a685cf0472772 100644
@@ -761,19 +761,18 @@ static void mana_get_ringparam(struct net_device *ndev,
 	ring->tx_max_pending = MAX_TX_BUFFERS_PER_QUEUE;
 }
 
+
 static int mana_set_ringparam(struct net_device *ndev,
 			      struct ethtool_ringparam *ring,
 			      struct kernel_ethtool_ringparam *kernel_ring,
 			      struct netlink_ext_ack *extack)
 {
 	struct mana_port_context *apc = netdev_priv(ndev);
+	struct mana_port_context *scratch;
+	struct mana_qset newq, oldq;
 	u32 new_tx, new_rx;
-	u32 old_tx, old_rx;
 	int err;
 
-	old_tx = apc->tx_queue_size;
-	old_rx = apc->rx_queue_size;
-
 	if (ring->tx_pending < MIN_TX_BUFFERS_PER_QUEUE) {
 		NL_SET_ERR_MSG_FMT(extack, "tx:%d less than the min:%d", ring->tx_pending,
 				   MIN_TX_BUFFERS_PER_QUEUE);
@@ -791,32 +790,61 @@ static int mana_set_ringparam(struct net_device *ndev,
 	netdev_info(ndev, "Using nearest power of 2 values for Txq:%d Rxq:%d\n",
 		    new_tx, new_rx);
 
-	/* pre-allocating new buffers to prevent failures in mana_attach() later */
-	apc->rx_queue_size = new_rx;
-	err = mana_pre_alloc_rxbufs(apc, ndev->mtu, apc->num_queues);
-	apc->rx_queue_size = old_rx;
-	if (err) {
-		netdev_err(ndev, "Insufficient memory for new allocations\n");
-		return err;
+	if (new_rx == apc->rx_queue_size && new_tx == apc->tx_queue_size)
+		return 0;
+
+	/* Port is down: no queues to rebuild, just record the new sizes. */
+	if (!apc->port_is_up) {
+		apc->rx_queue_size = new_rx;
+		apc->tx_queue_size = new_tx;
+		return 0;
 	}
 
-	err = mana_detach(ndev, false);
-	if (err) {
-		netdev_err(ndev, "mana_detach failed: %d\n", err);
-		goto out;
+	/* Block RDMA from acquiring the vport for the duration. The vport
+	 * itself is never released, so vport_use_count stays > 0.
+	 */
+	mutex_lock(&apc->vport_mutex);
+	if (apc->channel_changing) {
+		mutex_unlock(&apc->vport_mutex);
+		return -EBUSY;
 	}
+	apc->channel_changing = true;
+	mutex_unlock(&apc->vport_mutex);
 
-	apc->tx_queue_size = new_tx;
-	apc->rx_queue_size = new_rx;
+	scratch = mana_qset_scratch_alloc(apc);
+	if (!scratch) {
+		err = -ENOMEM;
+		goto clear_flag;
+	}
 
-	err = mana_attach(ndev);
+	err = mana_alloc_qset(scratch, apc->num_queues, new_rx, new_tx,
+			      apc->priv_flags, &newq);
 	if (err) {
-		netdev_err(ndev, "mana_attach failed: %d\n", err);
-		apc->tx_queue_size = old_tx;
-		apc->rx_queue_size = old_rx;
+		NL_SET_ERR_MSG_FMT(extack, "failed to change ring params: %d",
+				   err);
+		goto free_scratch; /* current qset untouched */
 	}
-out:
-	mana_pre_dealloc_rxbufs(apc);
+
+	err = mana_publish_qset(apc, &newq, &oldq);
+	if (err) {
+		NL_SET_ERR_MSG_FMT(extack, "failed to change ring params: %d",
+				   err);
+		mana_free_qset(scratch, &newq);
+		goto free_scratch;
+	}
+
+	mana_free_qset(scratch, &oldq);
+
+free_scratch:
+	/* After the caller-side cleanup above, so the EQ pool outlives the
+	 * CQs that reference it.
+	 */
+	mana_publish_close_if_needed(apc);
+	mana_qset_scratch_free(scratch);
+clear_flag:
+	mutex_lock(&apc->vport_mutex);
+	apc->channel_changing = false;
+	mutex_unlock(&apc->vport_mutex);
 	return err;
 }
 
Signature · 1 line
2.43.0
#9 lore raw

Re: [PATCH net-next v2 03/13] net: mana: swap queue sets in mana_set_ringparam

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:

New issues:
- [High] Hardware Resource Exhaustion (MSI-X vectors) / Functional Regression
- [High] Ad-hoc Synchronization with Flags

Pre-existing issues:
- [High] State Loss (RSS Indirection Table)
--

--- Patch [3]: [PATCH net-next v2 03/13] net: mana: swap queue sets in mana_set_ringparam --- commit e63909b90b30b5041af0319613913c9654a4cc80
Author: Long Li <[email protected]>

net: mana: swap queue sets in mana_set_ringparam

Convert the ring size path to pre-allocate and swap, for the same reasons as the channel count path. An allocation failure returns the error with the running configuration untouched, and the vport is never released.

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0
index b12291555eaeb..40e9886c87a81 100644

[ ... ]

Quoted 1 line
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0
@@ -791,32 +790,61 @@ static int mana_set_ringparam(struct net_device *ndev,

[ ... ]

drivers/net/ethernet/microsoft/mana/mana_ethtool.c static int mana_set_ringparam(struct net_device *ndev, · line 812

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +9 -0
+	/* Block RDMA from acquiring the vport for the duration. The vport
+	 * itself is never released, so vport_use_count stays > 0.
+	 */
+	mutex_lock(&apc->vport_mutex);
+	if (apc->channel_changing) {
+		mutex_unlock(&apc->vport_mutex);
+		return -EBUSY;
 	}
+	apc->channel_changing = true;
+	mutex_unlock(&apc->vport_mutex);

[Severity: High]
Does this code use apc->channel_changing as an ad-hoc lock?

Since this flag is set on entry and cleared on exit to prevent concurrent RDMA vport access, could we use a proper synchronization primitive here instead of a boolean flag?

drivers/net/ethernet/microsoft/mana/mana_ethtool.c static int mana_set_ringparam(struct net_device *ndev, · line 821

Quoted 12 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +7 -3
-	apc->tx_queue_size = new_tx;
-	apc->rx_queue_size = new_rx;
+	scratch = mana_qset_scratch_alloc(apc);
+	if (!scratch) {
+		err = -ENOMEM;
+		goto clear_flag;
+	}
 
-	err = mana_attach(ndev);
+	err = mana_alloc_qset(scratch, apc->num_queues, new_rx, new_tx,
+			      apc->priv_flags, &newq);

[Severity: High]
By pre-allocating a complete new queue set alongside the existing one, does this require twice as many MSI-X vectors simultaneously?

If the system is already fully utilizing its vectors, could mana_create_eq() fail to acquire vectors from the MSI-X pool, causing the ring parameter changes to reliably fail with -ENOSPC?

[Severity: High]
This is a pre-existing issue, but does this code cause a loss of the custom RSS indirection table?

During mana_alloc_qset(), mana_rss_table_init() is called, which unconditionally overwrites the indirection table with default round-robin values. If a user has configured a custom RSS indirection table, will those settings be irrevocably destroyed here?

[ ... ]

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +4 -0
+clear_flag:
+	mutex_lock(&apc->vport_mutex);
+	apc->channel_changing = false;
+	mutex_unlock(&apc->vport_mutex);
 	return err;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#10 lore raw

RE: [EXTERNAL] Re: [PATCH net-next v2 03/13] net: mana: swap queue sets in mana_set_ringparam

Quoted 46 lines

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

New issues:
- [High] Hardware Resource Exhaustion (MSI-X vectors) / Functional Regression
- [High] Ad-hoc Synchronization with Flags

Pre-existing issues:
- [High] State Loss (RSS Indirection Table)
--

--- Patch [3]: [PATCH net-next v2 03/13] net: mana: swap queue sets in mana_set_ringparam --- commit e63909b90b30b5041af0319613913c9654a4cc80
Author: Long Li <[email protected]>

net: mana: swap queue sets in mana_set_ringparam

Convert the ring size path to pre-allocate and swap, for the same reasons as the channel count path. An allocation failure returns the error with the running configuration untouched, and the vport is never released.

Quoted 5 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0
index b12291555eaeb..40e9886c87a81 100644

[ ... ]

Quoted 2 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0
@@ -791,32 +790,61 @@ static int mana_set_ringparam(struct net_device
 *ndev,

[ ... ]

drivers/net/ethernet/microsoft/mana/mana_ethtool.c static int mana_set_ringparam(struct net_device *ndev, · line 812

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +9 -0
+	/* Block RDMA from acquiring the vport for the duration. The vport
+	 * itself is never released, so vport_use_count stays > 0.
+	 */
+	mutex_lock(&apc->vport_mutex);
+	if (apc->channel_changing) {
+		mutex_unlock(&apc->vport_mutex);
+		return -EBUSY;
 	}
+	apc->channel_changing = true;
+	mutex_unlock(&apc->vport_mutex);

[Severity: High]
Does this code use apc->channel_changing as an ad-hoc lock?

Quoted 3 lines

Since this flag is set on entry and cleared on exit to prevent concurrent RDMA vport access, could we use a proper synchronization primitive here instead of a boolean flag?

This is pre-existing upstream mechanism, already used by  mana_set_channels() ; this patch reuses it for the ring path rather than inventing a second scheme.

Quoted 18 lines

drivers/net/ethernet/microsoft/mana/mana_ethtool.c static int mana_set_ringparam(struct net_device *ndev, · line 821

Quoted 13 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +7 -3
-	apc->tx_queue_size = new_tx;
-	apc->rx_queue_size = new_rx;
+	scratch = mana_qset_scratch_alloc(apc);
+	if (!scratch) {
+		err = -ENOMEM;
+		goto clear_flag;
+	}
 
-	err = mana_attach(ndev);
+	err = mana_alloc_qset(scratch, apc->num_queues, new_rx, new_tx,
+			      apc->priv_flags, &newq);

[Severity: High]
By pre-allocating a complete new queue set alongside the existing one, does this require twice as many MSI-X vectors simultaneously?

Quoted 3 lines

If the system is already fully utilizing its vectors, could mana_create_eq() fail to acquire vectors from the MSI-X pool, causing the ring parameter changes to reliably fail with -ENOSPC?

patch 9/13 ("share the EQ pool across a queue-set swap") fixes this.

Quoted 9 lines

[Severity: High]
This is a pre-existing issue, but does this code cause a loss of the custom RSS indirection table?

During mana_alloc_qset(), mana_rss_table_init() is called, which unconditionally overwrites the indirection table with default round-robin values. If a user has configured a custom RSS indirection table, will those settings be irrevocably destroyed here?

Patch 11/13 fixes it.

Long

author
#11 lore raw

PATCH v2 4/13 net: mana: swap queue sets in mana_set_priv_flags

In reply to Long Li

MANA_PRIV_FLAG_USE_FULL_PAGE_RXBUF changes the RX buffer layout, so toggling it rebuilds the queues. Convert that path to pre-allocate and
swap: a failure returns the error with both the queues and the flag word untouched.

The flag becomes part of the queue-set configuration rather than being written before the rebuild and rolled back on failure, so apc->priv_flags never describes queues that do not exist. Scheduling queue_reset_work() on failure goes with it, leaving the TX timeout handler as its only user.

The existing shortcuts keep their behaviour - a down port, or full-page RX already forced by a jumbo MTU or an attached XDP program, still just records the value - but now share one condition.

---
 .../ethernet/microsoft/mana/mana_ethtool.c    | 79 ++++++++++---------
 1 file changed, 41 insertions(+), 38 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +41 -38
index 40e9886c87a81c86080e52955a0a685cf0472772..dc3a0a22959ef19e2ea9921e81c7295aef628ccf 100644
@@ -870,11 +870,15 @@ static u32 mana_get_priv_flags(struct net_device *ndev)
 	return apc->priv_flags;
 }
 
+/* MANA_PRIV_FLAG_USE_FULL_PAGE_RXBUF changes the RX buffer layout, so the
+ * queues have to be rebuilt.
+ */
 static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags)
 {
 	struct mana_port_context *apc = netdev_priv(ndev);
 	u32 changed = apc->priv_flags ^ priv_flags;
-	u32 old_priv_flags = apc->priv_flags;
+	struct mana_port_context *scratch;
+	struct mana_qset newq, oldq;
 	int err = 0;
 
 	if (!changed)
@@ -884,54 +888,53 @@ static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags)
 	if (priv_flags & ~GENMASK(MANA_PRIV_FLAG_MAX - 1, 0))
 		return -EINVAL;
 
-	apc->priv_flags = priv_flags;
-
-	if (changed & BIT(MANA_PRIV_FLAG_USE_FULL_PAGE_RXBUF)) {
-		if (!apc->port_is_up)
-			return 0;
-
-		/* If XDP is attached or MTU is jumbo, single-buffer-per-page
-		 * is already forced regardless of this flag. Skip the
-		 * expensive detach/attach cycle since nothing changes.
-		 */
-		if (ndev->mtu + MANA_RXBUF_PAD > PAGE_SIZE / 2 ||
-		    mana_xdp_get(apc))
-			return 0;
+	/* Only the RX buffer layout flag requires a queue rebuild. Anything
+	 * else, a down port, or a configuration where single-buffer-per-page
+	 * is already forced, just records the new value.
+	 */
+	if (!(changed & BIT(MANA_PRIV_FLAG_USE_FULL_PAGE_RXBUF)) ||
+	    !apc->port_is_up ||
+	    ndev->mtu + MANA_RXBUF_PAD > PAGE_SIZE / 2 ||
+	    mana_xdp_get(apc)) {
+		apc->priv_flags = priv_flags;
+		return 0;
+	}
 
-		/* Block RDMA from grabbing the vport during detach/attach */
-		mutex_lock(&apc->vport_mutex);
-		apc->channel_changing = true;
+	/* Block RDMA from acquiring the vport for the duration. */
+	mutex_lock(&apc->vport_mutex);
+	if (apc->channel_changing) {
 		mutex_unlock(&apc->vport_mutex);
+		return -EBUSY;
+	}
+	apc->channel_changing = true;
+	mutex_unlock(&apc->vport_mutex);
 
-		err = mana_pre_alloc_rxbufs(apc, ndev->mtu, apc->num_queues);
-		if (err) {
-			netdev_err(ndev,
-				   "Insufficient memory for new allocations\n");
-			apc->priv_flags = old_priv_flags;
-			goto clear_flag;
-		}
+	scratch = mana_qset_scratch_alloc(apc);
+	if (!scratch) {
+		err = -ENOMEM;
+		goto clear_flag;
+	}
 
-		err = mana_detach(ndev, false);
-		if (err) {
-			netdev_err(ndev, "mana_detach failed: %d\n", err);
-			apc->priv_flags = old_priv_flags;
-			goto out;
-		}
+	err = mana_alloc_qset(scratch, apc->num_queues, apc->rx_queue_size,
+			      apc->tx_queue_size, priv_flags, &newq);
+	if (err)
+		goto free_scratch; /* current qset and priv_flags untouched */
 
-		err = mana_attach(ndev);
-		if (err) {
-			netdev_err(ndev, "mana_attach failed: %d\n", err);
-			apc->priv_flags = old_priv_flags;
-		}
+	err = mana_publish_qset(apc, &newq, &oldq);
+	if (err) {
+		mana_free_qset(scratch, &newq);
+		goto free_scratch;
 	}
 
-out:
-	mana_pre_dealloc_rxbufs(apc);
+	mana_free_qset(scratch, &oldq);
+
+free_scratch:
+	mana_publish_close_if_needed(apc);
+	mana_qset_scratch_free(scratch);
 clear_flag:
 	mutex_lock(&apc->vport_mutex);
 	apc->channel_changing = false;
 	mutex_unlock(&apc->vport_mutex);
-
 	return err;
 }
 
Signature · 1 line
2.43.0
#12 lore raw

Re: [PATCH net-next v2 04/13] net: mana: swap queue sets in mana_set_priv_flags

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

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

Pre-existing issues:
- [High] The boolean flag apc->channel_changing is used as an open-coded, ad-hoc lock to protect vport reconfiguration against concurrent RDMA access, violating core kernel locking rules. --

--- Patch [4]: [PATCH net-next v2 04/13] net: mana: swap queue sets in mana_set_priv_flags --- commit 922132e276d163d3c58e43d88b85f6b85a403949
Author: Long Li <[email protected]>

net: mana: swap queue sets in mana_set_priv_flags

This commit modifies mana_set_priv_flags to pre-allocate and swap queue sets instead of rebuilding them in place. This ensures that if the rebuild fails, both the queues and the flag word remain untouched.

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0

[ ... ]

Quoted 1 line
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0
@@ -884,54 +888,53 @@ static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags)

[ ... ]

drivers/net/ethernet/microsoft/mana/mana_ethtool.c static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags) · line 910

Quoted 11 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +7 -3
-		/* Block RDMA from grabbing the vport during detach/attach */
-		mutex_lock(&apc->vport_mutex);
-		apc->channel_changing = true;
+	/* Block RDMA from acquiring the vport for the duration. */
+	mutex_lock(&apc->vport_mutex);
+	if (apc->channel_changing) {
 		mutex_unlock(&apc->vport_mutex);
+		return -EBUSY;
+	}
+	apc->channel_changing = true;
+	mutex_unlock(&apc->vport_mutex);

[Severity: High]
This is a pre-existing issue, but in mana_set_priv_flags, is using a boolean flag like apc->channel_changing as an open-coded trylock safe?

It appears to be set on entry here and cleared on exit to protect vport reconfiguration against concurrent RDMA access, rather than using a standard synchronization primitive. Does this ad-hoc locking pattern evade lockdep and risk races since it bypasses proper kernel locking rules?

[ ... ]

Quoted 7 lines

clear_flag:

drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -1
 	mutex_lock(&apc->vport_mutex);
 	apc->channel_changing = false;
 	mutex_unlock(&apc->vport_mutex);
-
 	return err;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
author
#13 lore raw

RE: [EXTERNAL] Re: [PATCH net-next v2 04/13] net: mana: swap queue sets in mana_set_priv_flags

Quoted 8 lines

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

Pre-existing issues:
- [High] The boolean flag apc->channel_changing is used as an open-coded, ad-hoc lock to protect vport reconfiguration against concurrent RDMA access, violating core kernel locking rules.

This patch set doesn't change the locking mechanism used by upstream code.

Long

Quoted 62 lines

--

--- Patch [4]: [PATCH net-next v2 04/13] net: mana: swap queue sets in mana_set_priv_flags --- commit 922132e276d163d3c58e43d88b85f6b85a403949
Author: Long Li <[email protected]>

net: mana: swap queue sets in mana_set_priv_flags

This commit modifies mana_set_priv_flags to pre-allocate and swap queue sets instead of rebuilding them in place. This ensures that if the rebuild fails, both the queues and the flag word remain untouched.

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0

[ ... ]

Quoted 2 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -0
@@ -884,54 +888,53 @@ static int mana_set_priv_flags(struct net_device
 *ndev, u32 priv_flags)

[ ... ]

drivers/net/ethernet/microsoft/mana/mana_ethtool.c

Quoted 1 line
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -1
-		/* Block RDMA from grabbing the vport during detach/attach

*/

drivers/net/ethernet/microsoft/mana/mana_ethtool.c static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags) · line 910

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +7 -2
-		mutex_lock(&apc->vport_mutex);
-		apc->channel_changing = true;
+	/* Block RDMA from acquiring the vport for the duration. */
+	mutex_lock(&apc->vport_mutex);
+	if (apc->channel_changing) {
 		mutex_unlock(&apc->vport_mutex);
+		return -EBUSY;
+	}
+	apc->channel_changing = true;
+	mutex_unlock(&apc->vport_mutex);

[Severity: High]
This is a pre-existing issue, but in mana_set_priv_flags, is using a boolean flag like apc->channel_changing as an open-coded trylock safe?

It appears to be set on entry here and cleared on exit to protect vport reconfiguration against concurrent RDMA access, rather than using a standard synchronization primitive. Does this ad-hoc locking pattern evade lockdep and risk races since it bypasses proper kernel locking rules?

[ ... ]

drivers/net/ethernet/microsoft/mana/mana_ethtool.c static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags) · line 939

Quoted 7 lines

clear_flag:

drivers/net/ethernet/microsoft/mana/mana_ethtool.c +0 -1
 	mutex_lock(&apc->vport_mutex);
 	apc->channel_changing = false;
 	mutex_unlock(&apc->vport_mutex);
-
 	return err;
 }

--
Sashiko AI
review * https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%
2Fsashiko.dev%2F%23%2Fpatchset%2F20260813050418.2906468-1- longli%40microsoft.com%3Fpart%3D4&data=05%7C02%7Clongli%40microsoft. com%7C12b5de9dce024bffb15908def9c1a4bb%7C72f988bf86f141af91ab2d7c d011db47%7C1%7C0%7C639222807267503180%7CUnknown%7CTWFpbGZsb 3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkF OIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=mmPoxVwzW%2F 5aQYtA63m5ts8vN8Y3bM45YQmyRwYiTLA%3D&reserved=0

author
#14 lore raw

PATCH v2 5/13 net: mana: swap queue sets in mana_change_mtu

In reply to Long Li

The RX buffer layout depends on the MTU, so changing it rebuilds the queues. Convert mana_change_mtu() to pre-allocate and swap.

The MTU becomes part of the queue-set configuration, so a new set can be built for the new MTU while the running one still serves traffic at the old one, and ndev->mtu is updated only once the new set is live. Previously it was written before mana_attach() and rolled back on failure, so a failed change was briefly visible to the stack.

---
 drivers/net/ethernet/microsoft/mana/mana_en.c | 69 ++++++++++++++-----
 .../ethernet/microsoft/mana/mana_ethtool.c    |  8 ++-
 include/net/mana/mana.h                       | 13 +++-
 3 files changed, 67 insertions(+), 23 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_en.c +50 -19
index c0f31b386536a34af338c4d8be50537e2fe55317..c858a58577dfb1774b2b9394c01e9000edbe8bf9 100644
@@ -884,35 +884,49 @@ int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu, int num_qu
 	return -ENOMEM;
 }
 
+/* ndev->mtu is updated only once the new set is live (mana_publish_qset), so
+ * a failed allocation leaves the queues and the advertised MTU untouched.
+ */
 static int mana_change_mtu(struct net_device *ndev, int new_mtu)
 {
 	struct mana_port_context *mpc = netdev_priv(ndev);
-	unsigned int old_mtu = ndev->mtu;
+	struct mana_port_context *scratch;
+	struct mana_qset newq, oldq;
 	int err;
 
-	/* Pre-allocate buffers to prevent failure in mana_attach later */
-	err = mana_pre_alloc_rxbufs(mpc, new_mtu, mpc->num_queues);
-	if (err) {
-		netdev_err(ndev, "Insufficient memory for new MTU\n");
-		return err;
+	/* Port is down: no queues to rebuild, just record the new MTU.
+	 * mana_open() will size the RX buffers accordingly.
+	 */
+	if (!mpc->port_is_up) {
+		mpc->configured_mtu = new_mtu;
+		WRITE_ONCE(ndev->mtu, new_mtu);
+		return 0;
 	}
 
-	err = mana_detach(ndev, false);
-	if (err) {
-		netdev_err(ndev, "mana_detach failed: %d\n", err);
-		goto out;
-	}
+	scratch = mana_qset_scratch_alloc(mpc);
+	if (!scratch)
+		return -ENOMEM;
 
-	WRITE_ONCE(ndev->mtu, new_mtu);
+	err = mana_alloc_qset(scratch, mpc->num_queues, mpc->rx_queue_size,
+			      mpc->tx_queue_size, mpc->priv_flags, new_mtu,
+			      &newq);
+	if (err)
+		goto free_scratch; /* current qset and ndev->mtu untouched */
 
-	err = mana_attach(ndev);
+	err = mana_publish_qset(mpc, &newq, &oldq);
 	if (err) {
-		netdev_err(ndev, "mana_attach failed: %d\n", err);
-		WRITE_ONCE(ndev->mtu, old_mtu);
+		mana_free_qset(scratch, &newq);
+		goto free_scratch;
 	}
 
-out:
-	mana_pre_dealloc_rxbufs(mpc);
+	mana_free_qset(scratch, &oldq);
+
+free_scratch:
+	/* After the caller-side cleanup above, so the EQ pool outlives the
+	 * CQs that reference it.
+	 */
+	mana_publish_close_if_needed(mpc);
+	mana_qset_scratch_free(scratch);
 	return err;
 }
 
@@ -3089,7 +3103,8 @@ static struct mana_rxq *mana_create_rxq(struct mana_port_context *apc,
 	rxq->rxq_idx = rxq_idx;
 	rxq->rxobj = INVALID_MANA_HANDLE;
 
-	mana_get_rxbuf_cfg(apc, ndev->mtu, &rxq->datasize, &rxq->alloc_size,
+	mana_get_rxbuf_cfg(apc, apc->configured_mtu, &rxq->datasize,
+			   &rxq->alloc_size,
 			   &rxq->headroom, &rxq->frag_count);
 	/* Create page pool for RX queue */
 	err = mana_create_page_pool(rxq, gc);
@@ -3928,6 +3943,7 @@ static void mana_qset_snapshot(const struct mana_port_context *ctx,
 	out->rx_queue_size	= ctx->rx_queue_size;
 	out->tx_queue_size	= ctx->tx_queue_size;
 	out->priv_flags		= ctx->priv_flags;
+	out->mtu		= ctx->configured_mtu;
 	out->mana_eqs_debugfs	= ctx->mana_eqs_debugfs;
 }
 
@@ -3949,6 +3965,7 @@ static void mana_qset_install(struct mana_port_context *ctx,
 	ctx->rx_queue_size	= qset->rx_queue_size;
 	ctx->tx_queue_size	= qset->tx_queue_size;
 	ctx->priv_flags		= qset->priv_flags;
+	ctx->configured_mtu	= qset->mtu;
 	ctx->mana_eqs_debugfs	= qset->mana_eqs_debugfs;
 }
 
@@ -4011,7 +4028,7 @@ void mana_qset_scratch_free(struct mana_port_context *scratch)
  */
 int mana_alloc_qset(struct mana_port_context *scratch, unsigned int num_queues,
 		    unsigned int rx_queue_size, unsigned int tx_queue_size,
-		    u32 priv_flags, struct mana_qset *out)
+		    u32 priv_flags, int mtu, struct mana_qset *out)
 {
 	struct net_device *ndev = scratch->ndev;
 	int err;
@@ -4023,6 +4040,12 @@ int mana_alloc_qset(struct mana_port_context *scratch, unsigned int num_queues,
 	scratch->tx_queue_size	= tx_queue_size;
 	scratch->priv_flags	= priv_flags;
 
+	/* mana_get_rxbuf_cfg() reads this when sizing RX buffers, so the
+	 * new set is built for the requested MTU without disturbing the
+	 * running set.
+	 */
+	scratch->configured_mtu	= mtu;
+
 	err = mana_init_port_context(scratch);
 	if (err)
 		goto out_err;
@@ -4237,6 +4260,11 @@ int mana_publish_qset(struct mana_port_context *apc, struct mana_qset *newq,
 	if (err)
 		goto rollback;
 
+	/* The new set is serving traffic, so advertise its MTU. A no-op unless
+	 * the caller is changing it.
+	 */
+	WRITE_ONCE(ndev->mtu, apc->configured_mtu);
+
 	/* Pair with the queue-state stores above: a datapath reader that sees
 	 * the gate open must also see the queue set it is about to index.
 	 */
@@ -4289,6 +4317,8 @@ int mana_publish_qset(struct mana_port_context *apc, struct mana_qset *newq,
 		return err;
 	}
 
+	WRITE_ONCE(ndev->mtu, apc->configured_mtu);
+
 	/* Same pairing as the success path: the restored queue set has to be
 	 * visible before the gate reopens on it.
 	 */
@@ -4508,6 +4538,7 @@ static int mana_probe_port(struct mana_context *ac, int port_idx,
 	apc->port_handle = INVALID_MANA_HANDLE;
 	apc->pf_filter_handle = INVALID_MANA_HANDLE;
 	apc->port_idx = port_idx;
+	apc->configured_mtu = ndev->mtu;
 	apc->link_cfg_error = 1;
 	apc->cqe_coalescing_enable = 0;
 	apc->cqe8_coalescing_enable = 0;
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +5 -3
index dc3a0a22959ef19e2ea9921e81c7295aef628ccf..f4818305bec83f8a60ce02baf280b0d4b9fcb51e 100644
@@ -723,7 +723,8 @@ static int mana_set_channels(struct net_device *ndev,
 	}
 
 	err = mana_alloc_qset(scratch, new_count, apc->rx_queue_size,
-			      apc->tx_queue_size, apc->priv_flags, &newq);
+			      apc->tx_queue_size, apc->priv_flags,
+			      apc->configured_mtu, &newq);
 	if (err)
 		goto free_scratch; /* current qset untouched, nothing to undo */
 
@@ -818,7 +819,7 @@ static int mana_set_ringparam(struct net_device *ndev,
 	}
 
 	err = mana_alloc_qset(scratch, apc->num_queues, new_rx, new_tx,
-			      apc->priv_flags, &newq);
+			      apc->priv_flags, apc->configured_mtu, &newq);
 	if (err) {
 		NL_SET_ERR_MSG_FMT(extack, "failed to change ring params: %d",
 				   err);
@@ -916,7 +917,8 @@ static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags)
 	}
 
 	err = mana_alloc_qset(scratch, apc->num_queues, apc->rx_queue_size,
-			      apc->tx_queue_size, priv_flags, &newq);
+			      apc->tx_queue_size, priv_flags,
+			      apc->configured_mtu, &newq);
 	if (err)
 		goto free_scratch; /* current qset and priv_flags untouched */
 
include/net/mana/mana.h +12 -1
index 66a653bad22eba511d8cf8cf71b572c604044ecb..b9d79240dbc7cc0fb106336d68b6f0cf3a34f2bf 100644
@@ -620,6 +620,11 @@ struct mana_port_context {
 	unsigned int rx_queue_size;
 	unsigned int tx_queue_size;
 
+	/* MTU the RX queues were built for. Equal to ndev->mtu except during a
+	 * swap, when the new set is built before ndev->mtu is updated.
+	 */
+	int configured_mtu;
+
 	mana_handle_t port_handle;
 	mana_handle_t pf_filter_handle;
 
@@ -710,6 +715,12 @@ struct mana_qset {
 	unsigned int		tx_queue_size;
 	u32			priv_flags;
 
+	/* MTU the RX buffers of this set were sized for. It feeds
+	 * mana_get_rxbuf_cfg(), so it is part of the queue-set
+	 * configuration and must be swapped atomically with the queues.
+	 */
+	int			mtu;
+
 	/* Per-queue-set debugfs root ("EQs"). Owned by the qset: it is
 	 * recreated by mana_create_eq() for each new set and torn down
 	 * with that set, so it must travel with the qset rather than
@@ -735,7 +746,7 @@ struct mana_port_context *mana_qset_scratch_alloc(struct mana_port_context *apc)
 void mana_qset_scratch_free(struct mana_port_context *scratch);
 int mana_alloc_qset(struct mana_port_context *scratch, unsigned int num_queues,
 		    unsigned int rx_queue_size, unsigned int tx_queue_size,
-		    u32 priv_flags, struct mana_qset *out);
+		    u32 priv_flags, int mtu, struct mana_qset *out);
 int mana_publish_qset(struct mana_port_context *apc, struct mana_qset *newq,
 		      struct mana_qset *out_old);
 void mana_publish_close_if_needed(struct mana_port_context *apc);
Signature · 1 line
2.43.0
#15 lore raw

Re: [PATCH net-next v2 05/13] net: mana: swap queue sets in mana_change_mtu

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

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

Pre-existing issues:
- [High] mana_xdp_set() is vulnerable to a race condition with RDMA vport creation because it continues to use the legacy mana_detach()/mana_attach() sequence without blocking RDMA, leaving the interface permanently down if a race occurs. --

--- Patch [5]: [PATCH net-next v2 05/13] net: mana: swap queue sets in mana_change_mtu --- commit 07ab252b810cb193b26b16516fc0a40653d2b458
Author: Long Li <[email protected]>

net: mana: swap queue sets in mana_change_mtu

The RX buffer layout depends on the MTU, so changing it rebuilds the queues. Convert mana_change_mtu() to pre-allocate and swap.

The MTU becomes part of the queue-set configuration, so a new set can be built for the new MTU while the running one still serves traffic at the old one, and ndev->mtu is updated only once the new set is live. Previously it was written before mana_attach() and rolled back on failure, so a failed change was briefly visible to the stack.

Quoted 28 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +9 -6
index c0f31b386536a..c858a58577dfb 100644
@@ -884,35 +884,49 @@ int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu, int num_qu
 	return -ENOMEM;
 }
 
+/* ndev->mtu is updated only once the new set is live (mana_publish_qset), so
+ * a failed allocation leaves the queues and the advertised MTU untouched.
+ */
 static int mana_change_mtu(struct net_device *ndev, int new_mtu)
 {
 	struct mana_port_context *mpc = netdev_priv(ndev);
-	unsigned int old_mtu = ndev->mtu;
+	struct mana_port_context *scratch;
+	struct mana_qset newq, oldq;
 	int err;
 
-	/* Pre-allocate buffers to prevent failure in mana_attach later */
-	err = mana_pre_alloc_rxbufs(mpc, new_mtu, mpc->num_queues);
-	if (err) {
-		netdev_err(ndev, "Insufficient memory for new MTU\n");
-		return err;
+	/* Port is down: no queues to rebuild, just record the new MTU.
+	 * mana_open() will size the RX buffers accordingly.
+	 */
+	if (!mpc->port_is_up) {

drivers/net/ethernet/microsoft/mana/mana_en.c int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu, int num_qu · line 904

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +3 -5
+		mpc->configured_mtu = new_mtu;
+		WRITE_ONCE(ndev->mtu, new_mtu);
+		return 0;
 	}
 
-	err = mana_detach(ndev, false);
-	if (err) {
-		netdev_err(ndev, "mana_detach failed: %d\n", err);
-		goto out;
-	}

[Severity: High]
This isn't a bug introduced by this patch, but since it converts mana_change_mtu() to avoid the mana_detach()/mana_attach() sequence, is mana_xdp_set() still vulnerable to a race condition with RDMA vport creation?

Looking at mana_xdp_set() in drivers/net/ethernet/microsoft/mana/mana_bpf.c, it still uses the legacy detach/attach pattern without blocking RDMA:

	err = mana_detach(ndev, false);
	if (err) {
		netdev_err(ndev,
			   "mana_detach failed at xdp set: %d\n", err);
		NL_SET_ERR_MSG_MOD(extack,
				   "XDP: Re-config failed at detach");
		goto err_dealloc_rxbuffs;
	}

	err = mana_attach(ndev);

Because mana_xdp_set() drops vport_use_count to 0 without holding vport_mutex or setting channel_changing = true, couldn't an RDMA application successfully acquire the vport in the middle of XDP reconfiguration?

If a race occurs, mana_attach() would fail with -EBUSY because RDMA owns the vport, leaving the Ethernet port permanently down and requiring manual intervention to recover.

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +3 -1
-	WRITE_ONCE(ndev->mtu, new_mtu);
+	scratch = mana_qset_scratch_alloc(mpc);
+	if (!scratch)
+		return -ENOMEM;

[ ... ]

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
author
#16 lore raw

RE: [EXTERNAL] Re: [PATCH net-next v2 05/13] net: mana: swap queue sets in mana_change_mtu

Quoted 90 lines

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

Pre-existing issues:
- [High] mana_xdp_set() is vulnerable to a race condition with RDMA vport creation because it continues to use the legacy mana_detach()/mana_attach() sequence without blocking RDMA, leaving the interface permanently down if a race occurs.
--

--- Patch [5]: [PATCH net-next v2 05/13] net: mana: swap queue sets in mana_change_mtu --- commit 07ab252b810cb193b26b16516fc0a40653d2b458
Author: Long Li <[email protected]>

net: mana: swap queue sets in mana_change_mtu

The RX buffer layout depends on the MTU, so changing it rebuilds the queues. Convert mana_change_mtu() to pre-allocate and swap.

The MTU becomes part of the queue-set configuration, so a new set can be built for the new MTU while the running one still serves traffic at the old one, and ndev->mtu is updated only once the new set is live. Previously it was written before mana_attach() and rolled back on failure, so a failed change was briefly visible to the stack.

drivers/net/ethernet/microsoft/mana/mana_en.c int mana_pre_alloc_rxbufs(struct

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
index c0f31b386536a..c858a58577dfb 100644
@@ -884,35 +884,49 @@ int mana_pre_alloc_rxbufs(struct

mana_port_context *mpc, int new_mtu, int num_qu

drivers/net/ethernet/microsoft/mana/mana_en.c int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu, int num_qu

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +3 -0
 	return -ENOMEM;
 }
 
+/* ndev->mtu is updated only once the new set is live
+(mana_publish_qset), so
+ * a failed allocation leaves the queues and the advertised MTU

untouched.

Quoted 17 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +7 -6
+ */
 static int mana_change_mtu(struct net_device *ndev, int new_mtu)  {
 	struct mana_port_context *mpc = netdev_priv(ndev);
-	unsigned int old_mtu = ndev->mtu;
+	struct mana_port_context *scratch;
+	struct mana_qset newq, oldq;
 	int err;
 
-	/* Pre-allocate buffers to prevent failure in mana_attach later */
-	err = mana_pre_alloc_rxbufs(mpc, new_mtu, mpc->num_queues);
-	if (err) {
-		netdev_err(ndev, "Insufficient memory for new MTU\n");
-		return err;
+	/* Port is down: no queues to rebuild, just record the new MTU.
+	 * mana_open() will size the RX buffers accordingly.
+	 */
+	if (!mpc->port_is_up) {

drivers/net/ethernet/microsoft/mana/mana_en.c int mana_pre_alloc_rxbufs(struct mana_port_context *mpc, int new_mtu, int num_qu · line 904

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +3 -5
+		mpc->configured_mtu = new_mtu;
+		WRITE_ONCE(ndev->mtu, new_mtu);
+		return 0;
 	}
 
-	err = mana_detach(ndev, false);
-	if (err) {
-		netdev_err(ndev, "mana_detach failed: %d\n", err);
-		goto out;
-	}

[Severity: High]
This isn't a bug introduced by this patch, but since it converts mana_change_mtu() to avoid the mana_detach()/mana_attach() sequence, is mana_xdp_set() still vulnerable to a race condition with RDMA vport creation?

Looking at mana_xdp_set() in drivers/net/ethernet/microsoft/mana/mana_bpf.c,
it still uses the legacy detach/attach pattern without blocking RDMA:

	err = mana_detach(ndev, false);
	if (err) {
		netdev_err(ndev,
			   "mana_detach failed at xdp set: %d\n", err);
		NL_SET_ERR_MSG_MOD(extack,
				   "XDP: Re-config failed at detach");
		goto err_dealloc_rxbuffs;
	}

	err = mana_attach(ndev);
Quoted 8 lines

Because mana_xdp_set() drops vport_use_count to 0 without holding vport_mutex or setting channel_changing = true, couldn't an RDMA application successfully acquire the vport in the middle of XDP reconfiguration?

If a race occurs, mana_attach() would fail with -EBUSY because RDMA owns the vport, leaving the Ethernet port permanently down and requiring manual intervention to recover.

It's fixed by 06/13 patch.

Long

author
#17 lore raw

PATCH v2 6/13 net: mana: swap queue sets in mana_xdp_set

In reply to Long Li

Attaching or detaching an XDP program changes the RX buffer layout - full pages with headroom rather than page fragments - so it rebuilds the queues. Convert mana_xdp_set() to pre-allocate and swap, completing the removal of detach/attach from the reconfiguration paths.

The program becomes part of the queue-set configuration, so it is swapped atomically with the queues it was built for and restored by the rollback path. Program references follow the same swap, so attaching no longer leaves the queues briefly running without one.

---
 .../net/ethernet/microsoft/mana/mana_bpf.c    | 68 ++++++++++---------
 drivers/net/ethernet/microsoft/mana/mana_en.c | 14 ++--
 .../ethernet/microsoft/mana/mana_ethtool.c    |  7 +-
 include/net/mana/mana.h                       | 16 +++--
 4 files changed, 57 insertions(+), 48 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_bpf.c +35 -33
index e16ce2a0715839594a5837288c1d4c1de412e7fb..84b1484faaec8bf411ce402538862347e68954f0 100644
@@ -175,10 +175,17 @@ void mana_chn_setxdp(struct mana_port_context *apc, struct bpf_prog *prog)
 			bpf_prog_put(old_prog);
 }
 
+/* Attaching or detaching XDP changes the RX buffer layout (full pages vs
+ * fragments), so the RX queues are rebuilt. The swap helpers handle
+ * refcounting: mana_publish_qset() attaches the program to the new queues,
+ * mana_free_qset() drops the old set's references.
+ */
 static int mana_xdp_set(struct net_device *ndev, struct bpf_prog *prog,
 			struct netlink_ext_ack *extack)
 {
 	struct mana_port_context *apc = netdev_priv(ndev);
+	struct mana_port_context *scratch;
+	struct mana_qset newq, oldq;
 	struct bpf_prog *old_prog;
 	struct gdma_context *gc;
 	int err;
@@ -198,46 +205,46 @@ static int mana_xdp_set(struct net_device *ndev, struct bpf_prog *prog,
 		return -EOPNOTSUPP;
 	}
 
-	/* One refcnt of the prog is hold by the caller already, so
-	 * don't increase refcnt for this one.
-	 */
-	apc->bpf_prog = prog;
-
 	if (apc->port_is_up) {
-		/* Re-create rxq's after xdp prog was loaded or unloaded.
-		 * Ex: re create rxq's to switch from full pages to smaller
-		 * size page fragments when xdp prog is unloaded and
-		 * vice-versa.
-		 */
-
-		/* Pre-allocate buffers to prevent failure in mana_attach */
-		err = mana_pre_alloc_rxbufs(apc, ndev->mtu, apc->num_queues);
-		if (err) {
+		scratch = mana_qset_scratch_alloc(apc);
+		if (!scratch) {
 			NL_SET_ERR_MSG_MOD(extack,
-					   "XDP: Insufficient memory for tx/rx re-config");
-			return err;
+					   "XDP: Insufficient memory for re-config");
+			return -ENOMEM;
 		}
 
-		err = mana_detach(ndev, false);
+		err = mana_alloc_qset(scratch, apc->num_queues,
+				      apc->rx_queue_size, apc->tx_queue_size,
+				      apc->priv_flags, apc->configured_mtu,
+				      prog, &newq);
 		if (err) {
-			netdev_err(ndev,
-				   "mana_detach failed at xdp set: %d\n", err);
 			NL_SET_ERR_MSG_MOD(extack,
-					   "XDP: Re-config failed at detach");
-			goto err_dealloc_rxbuffs;
+					   "XDP: Re-config failed at alloc");
+			mana_qset_scratch_free(scratch);
+			return err;
 		}
 
-		err = mana_attach(ndev);
+		err = mana_publish_qset(apc, &newq, &oldq);
 		if (err) {
-			netdev_err(ndev,
-				   "mana_attach failed at xdp set: %d\n", err);
 			NL_SET_ERR_MSG_MOD(extack,
-					   "XDP: Re-config failed at attach");
-			goto err_dealloc_rxbuffs;
+					   "XDP: Re-config failed at publish");
+			mana_free_qset(scratch, &newq);
+			/* After the cleanup above: closing destroys the EQ pool
+			 * those queues' CQs were attached to.
+			 */
+			mana_publish_close_if_needed(apc);
+			mana_qset_scratch_free(scratch);
+			return err;
 		}
 
-		mana_chn_setxdp(apc, prog);
-		mana_pre_dealloc_rxbufs(apc);
+		mana_free_qset(scratch, &oldq);
+		mana_qset_scratch_free(scratch);
+	} else {
+		/* No queues to rebuild; mana_open() will size the RX buffers
+		 * for this program. One refcnt is held by the caller already,
+		 * so don't take another.
+		 */
+		apc->bpf_prog = prog;
 	}
 
 	if (old_prog)
@@ -250,11 +257,6 @@ static int mana_xdp_set(struct net_device *ndev, struct bpf_prog *prog,
 		ndev->max_mtu = gc->adapter_mtu - ETH_HLEN;
 
 	return 0;
-
-err_dealloc_rxbuffs:
-	apc->bpf_prog = old_prog;
-	mana_pre_dealloc_rxbufs(apc);
-	return err;
 }
 
 int mana_bpf(struct net_device *ndev, struct netdev_bpf *bpf)
drivers/net/ethernet/microsoft/mana/mana_en.c +9 -5
index c858a58577dfb1774b2b9394c01e9000edbe8bf9..e54ad4db918ce3d662092a94ac13c41a4b958d6a 100644
@@ -909,7 +909,7 @@ static int mana_change_mtu(struct net_device *ndev, int new_mtu)
 
 	err = mana_alloc_qset(scratch, mpc->num_queues, mpc->rx_queue_size,
 			      mpc->tx_queue_size, mpc->priv_flags, new_mtu,
-			      &newq);
+			      mpc->bpf_prog, &newq);
 	if (err)
 		goto free_scratch; /* current qset and ndev->mtu untouched */
 
@@ -3944,6 +3944,7 @@ static void mana_qset_snapshot(const struct mana_port_context *ctx,
 	out->tx_queue_size	= ctx->tx_queue_size;
 	out->priv_flags		= ctx->priv_flags;
 	out->mtu		= ctx->configured_mtu;
+	out->bpf_prog		= ctx->bpf_prog;
 	out->mana_eqs_debugfs	= ctx->mana_eqs_debugfs;
 }
 
@@ -3966,6 +3967,7 @@ static void mana_qset_install(struct mana_port_context *ctx,
 	ctx->tx_queue_size	= qset->tx_queue_size;
 	ctx->priv_flags		= qset->priv_flags;
 	ctx->configured_mtu	= qset->mtu;
+	ctx->bpf_prog		= qset->bpf_prog;
 	ctx->mana_eqs_debugfs	= qset->mana_eqs_debugfs;
 }
 
@@ -4028,7 +4030,8 @@ void mana_qset_scratch_free(struct mana_port_context *scratch)
  */
 int mana_alloc_qset(struct mana_port_context *scratch, unsigned int num_queues,
 		    unsigned int rx_queue_size, unsigned int tx_queue_size,
-		    u32 priv_flags, int mtu, struct mana_qset *out)
+		    u32 priv_flags, int mtu, struct bpf_prog *bpf_prog,
+		    struct mana_qset *out)
 {
 	struct net_device *ndev = scratch->ndev;
 	int err;
@@ -4040,11 +4043,12 @@ int mana_alloc_qset(struct mana_port_context *scratch, unsigned int num_queues,
 	scratch->tx_queue_size	= tx_queue_size;
 	scratch->priv_flags	= priv_flags;
 
-	/* mana_get_rxbuf_cfg() reads this when sizing RX buffers, so the
-	 * new set is built for the requested MTU without disturbing the
-	 * running set.
+	/* mana_get_rxbuf_cfg() reads both of these when sizing RX buffers,
+	 * so the new set is built for the requested MTU / XDP program
+	 * without disturbing the running set.
 	 */
 	scratch->configured_mtu	= mtu;
+	scratch->bpf_prog	= bpf_prog;
 
 	err = mana_init_port_context(scratch);
 	if (err)
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +4 -3
index f4818305bec83f8a60ce02baf280b0d4b9fcb51e..0daacee28001ed45a66ef00449b2c2acbf2859d4 100644
@@ -724,7 +724,7 @@ static int mana_set_channels(struct net_device *ndev,
 
 	err = mana_alloc_qset(scratch, new_count, apc->rx_queue_size,
 			      apc->tx_queue_size, apc->priv_flags,
-			      apc->configured_mtu, &newq);
+			      apc->configured_mtu, apc->bpf_prog, &newq);
 	if (err)
 		goto free_scratch; /* current qset untouched, nothing to undo */
 
@@ -819,7 +819,8 @@ static int mana_set_ringparam(struct net_device *ndev,
 	}
 
 	err = mana_alloc_qset(scratch, apc->num_queues, new_rx, new_tx,
-			      apc->priv_flags, apc->configured_mtu, &newq);
+			      apc->priv_flags, apc->configured_mtu,
+			      apc->bpf_prog, &newq);
 	if (err) {
 		NL_SET_ERR_MSG_FMT(extack, "failed to change ring params: %d",
 				   err);
@@ -918,7 +919,7 @@ static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags)
 
 	err = mana_alloc_qset(scratch, apc->num_queues, apc->rx_queue_size,
 			      apc->tx_queue_size, priv_flags,
-			      apc->configured_mtu, &newq);
+			      apc->configured_mtu, apc->bpf_prog, &newq);
 	if (err)
 		goto free_scratch; /* current qset and priv_flags untouched */
 
include/net/mana/mana.h +9 -7
index b9d79240dbc7cc0fb106336d68b6f0cf3a34f2bf..4fcd5e307a4805b81c5c82482fc65a4ba68eea89 100644
@@ -589,7 +589,7 @@ struct mana_port_context {
 
 	/* Indirection Table for RX & TX. The values are queue indexes */
 	u32 *indir_table;
-	u32 indir_table_sz;
+	u32			indir_table_sz;
 
 	/* Indirection table containing RxObject Handles */
 	mana_handle_t *rxobj_table;
@@ -715,11 +715,12 @@ struct mana_qset {
 	unsigned int		tx_queue_size;
 	u32			priv_flags;
 
-	/* MTU the RX buffers of this set were sized for. It feeds
-	 * mana_get_rxbuf_cfg(), so it is part of the queue-set
+	/* MTU and XDP program the RX buffers of this set were sized for.
+	 * Both feed mana_get_rxbuf_cfg(), so they are part of the queue-set
 	 * configuration and must be swapped atomically with the queues.
 	 */
 	int			mtu;
+	struct bpf_prog		*bpf_prog;
 
 	/* Per-queue-set debugfs root ("EQs"). Owned by the qset: it is
 	 * recreated by mana_create_eq() for each new set and torn down
@@ -738,15 +739,16 @@ int mana_alloc_queues(struct net_device *ndev);
 int mana_attach(struct net_device *ndev);
 int mana_detach(struct net_device *ndev, bool from_close);
 
-/* Pre-allocate + swap reconfiguration. Allocation and teardown run against a
- * scratch context, so the live port context is mutated only inside
- * mana_publish_qset() with TX disabled. Both sets share a port-owned EQ pool.
+/* Pre-allocate + swap reconfiguration path (prototype). Allocation and
+ * teardown run against a scratch context so the live port context is only
+ * mutated inside mana_publish_qset(), with TX disabled.
  */
 struct mana_port_context *mana_qset_scratch_alloc(struct mana_port_context *apc);
 void mana_qset_scratch_free(struct mana_port_context *scratch);
 int mana_alloc_qset(struct mana_port_context *scratch, unsigned int num_queues,
 		    unsigned int rx_queue_size, unsigned int tx_queue_size,
-		    u32 priv_flags, int mtu, struct mana_qset *out);
+		    u32 priv_flags, int mtu, struct bpf_prog *bpf_prog,
+		    struct mana_qset *out);
 int mana_publish_qset(struct mana_port_context *apc, struct mana_qset *newq,
 		      struct mana_qset *out_old);
 void mana_publish_close_if_needed(struct mana_port_context *apc);
Signature · 1 line
2.43.0
author
#18 lore raw

PATCH v2 7/13 net: mana: do not bail out of mana_detach on dealloc failure

In reply to Long Li

From: Dipayaan Roy <[email protected]>

mana_detach() sets port_is_up = false before calling mana_dealloc_queues(). If that call were to fail and return early, netif_device_detach() and mana_cleanup_port_context() are skipped, leaving the port in an inconsistent state where port_is_up is false but netif_device_present() still returns true.

With the ethtool, MTU and XDP paths converted to pre-allocate and swap, the callers that reach this code with from_close == false are the TX timeout reset handler and the suspend path. For the reset handler the inconsistent state is fatal: its mana_detach() would overwrite port_st_save with false, so the following mana_attach() skips queue allocation and leaves the port permanently dead. For suspend it leaves the port context allocated across the transition.

Remove the early return so that mana_detach() always completes its full teardown. mana_dealloc_queues() already performs best-effort cleanup regardless of internal errors, and in practice cannot fail here since port_is_up is already false, so continuing to netif_device_detach() and mana_cleanup_port_context() is safe and keeps the state consistent.

---
 drivers/net/ethernet/microsoft/mana/mana_en.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_en.c +1 -3
index e54ad4db918ce3d662092a94ac13c41a4b958d6a..2b3250483b43954c74d8cbf6a9e6e77880eac8ca 100644
@@ -4502,10 +4502,8 @@ int mana_detach(struct net_device *ndev, bool from_close)
 
 	if (apc->port_st_save) {
 		err = mana_dealloc_queues(ndev);
-		if (err) {
+		if (err)
 			netdev_err(ndev, "%s failed to deallocate queues: %d\n", __func__, err);
-			return err;
-		}
 	}
 
 	if (!from_close) {
Signature · 1 line
2.43.0
#19 lore raw

Re: [PATCH net-next v2 07/13] net: mana: do not bail out of mana_detach on dealloc failure

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

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

New issues:
- [High] The 'channel_changing' boolean flag is used as an ad-hoc lock to guard code sections and prevent concurrent access, violating kernel synchronization guidelines.

Pre-existing issues:
- [Critical] Hardware queue memory and RX page pools are freed while the device may still be DMAing to them if it fails to quiesce during teardown (DMA Use-After-Free). --

--- Patch [7]: [PATCH net-next v2 07/13] net: mana: do not bail out of mana_detach on dealloc failure --- commit 04170ba1361c402d70e39fad639e6d6455da30a2
Author: Dipayaan Roy <[email protected]>

net: mana: do not bail out of mana_detach on dealloc failure

This commit modifies mana_detach() to remove the early return if mana_dealloc_queues() fails. This ensures that the function completes its full teardown, including netif_device_detach() and mana_cleanup_port_context(), avoiding an inconsistent port state.

drivers/net/ethernet/microsoft/mana/mana_en.c int mana_detach(struct net_device *ndev, bool from_close) · line 4504

Quoted 8 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
index e54ad4db918ce..2b3250483b439 100644
@@ -4502,10 +4502,8 @@ int mana_detach(struct net_device *ndev, bool from_close)
 
 	if (apc->port_st_save) {
 		err = mana_dealloc_queues(ndev);

[Severity: Critical]
This is a pre-existing issue, but does mana_dealloc_queues() safely handle a failure to quiesce the device?

If mana_drain_txqs() times out and pci_try_reset_function() fails, it skips freeing the TX SKB buffers and returns false:

drivers/net/ethernet/microsoft/mana/mana_en.c:mana_drain_txqs() {
    ...
		err = pci_try_reset_function(to_pci_dev(gd->gdma_context->dev));
		if (err) {
			netdev_err(apc->ndev,
				   "function reset failed: %d, %d pkts pending in txq %u\n",
				   err, atomic_read(&txq->pending_sends),
				   txq->gdma_txq_id);
			quiesced = false;
    ...
	if (!quiesced) {
		netdev_err(apc->ndev,
			   "device not quiesced, leaking pending TX buffers instead of unmapping memory it can still DMA from\n");
		return reset;
	}
    ...
}

However, mana_dealloc_queues() ignores this false return value and proceeds with the teardown:

drivers/net/ethernet/microsoft/mana/mana_en.c:mana_dealloc_queues() {
    ...
	if (mana_drain_txqs(apc)) {
		struct mana_context *ac = apc->ac;
		unsigned int i;
    ...
	/* Even in err case, still need to cleanup the vPort */
	mana_destroy_rxqs(apc);
	mana_destroy_txq(apc);
    ...
}

Because the device was never successfully quiesced, it remains active. Will unconditionally freeing the GDMA SQ and RQ hardware ring buffers and RX page pool buffers here lead to a DMA use-after-free when the hardware continues to DMA read/write to the now-freed memory?

drivers/net/ethernet/microsoft/mana/mana_en.c int mana_detach(struct net_device *ndev, bool from_close) · line 4507

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +1 -3
-		if (err) {
+		if (err)
 			netdev_err(ndev, "%s failed to deallocate queues: %d\n", __func__, err);
-			return err;
-		}
 	}

[Severity: High]
Is the channel_changing boolean flag being used as an ad-hoc lock to guard code sections and prevent concurrent access?

drivers/net/ethernet/microsoft/mana/mana_ethtool.c:mana_set_ringparam() {
    ...
	mutex_lock(&apc->vport_mutex);
	if (apc->channel_changing) {
		mutex_unlock(&apc->vport_mutex);
		return -EBUSY;
	}
	apc->channel_changing = true;
	mutex_unlock(&apc->vport_mutex);
    ...
}

Using a boolean flag to protect entire blocks of code bypasses standard kernel locking mechanisms. This prevents lockdep from analyzing lock dependencies and forces concurrent callers to fail with -EBUSY instead of gracefully waiting.

Could a proper mutex or rwsem be used here instead?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
author
#20 lore raw

RE: [EXTERNAL] Re: [PATCH net-next v2 07/13] net: mana: do not bail out of mana_detach on dealloc failure

Quoted 81 lines

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

New issues:
- [High] The 'channel_changing' boolean flag is used as an ad-hoc lock to guard code sections and prevent concurrent access, violating kernel synchronization guidelines.

Pre-existing issues:
- [Critical] Hardware queue memory and RX page pools are freed while the device may still be DMAing to them if it fails to quiesce during teardown (DMA Use-After-Free).
--

--- Patch [7]: [PATCH net-next v2 07/13] net: mana: do not bail out of mana_detach on dealloc failure --- commit 04170ba1361c402d70e39fad639e6d6455da30a2
Author: Dipayaan Roy <[email protected]>

net: mana: do not bail out of mana_detach on dealloc failure

This commit modifies mana_detach() to remove the early return if mana_dealloc_queues() fails. This ensures that the function completes its full teardown, including netif_device_detach() and mana_cleanup_port_context(), avoiding an inconsistent port state.

drivers/net/ethernet/microsoft/mana/mana_en.c int mana_detach(struct net_device *ndev, bool · line 4505

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
index e54ad4db918ce..2b3250483b439 100644
@@ -4502,10 +4502,8 @@ int mana_detach(struct net_device *ndev, bool
 from_close)
 
 	if (apc->port_st_save) {
 		err = mana_dealloc_queues(ndev);

[Severity: Critical]
This is a pre-existing issue, but does mana_dealloc_queues() safely handle a failure to quiesce the device?

If mana_drain_txqs() times out and pci_try_reset_function() fails, it skips freeing the TX SKB buffers and returns false:

drivers/net/ethernet/microsoft/mana/mana_en.c:mana_drain_txqs() {
    ...
		err = pci_try_reset_function(to_pci_dev(gd->gdma_context-

drivers/net/ethernet/microsoft/mana/mana_en.c int mana_detach(struct net_device *ndev, bool · line 4506

Quoted 1 line
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
 dev));
		if (err) {
			netdev_err(apc->ndev,
				   "function reset failed: %d, %d pkts pending
in txq %u\n",
				   err, atomic_read(&txq->pending_sends),
				   txq->gdma_txq_id);
			quiesced = false;
    ...
	if (!quiesced) {
		netdev_err(apc->ndev,
			   "device not quiesced, leaking pending TX buffers
instead of unmapping memory it can still DMA from\n");
		return reset;
	}
    ...
}

However, mana_dealloc_queues() ignores this false return value and proceeds with the teardown:

drivers/net/ethernet/microsoft/mana/mana_en.c:mana_dealloc_queues() {
    ...
	if (mana_drain_txqs(apc)) {
		struct mana_context *ac = apc->ac;
		unsigned int i;
    ...
	/* Even in err case, still need to cleanup the vPort */
	mana_destroy_rxqs(apc);
	mana_destroy_txq(apc);
    ...
}
Quoted 4 lines

Because the device was never successfully quiesced, it remains active. Will unconditionally freeing the GDMA SQ and RQ hardware ring buffers and RX page pool buffers here lead to a DMA use-after-free when the hardware continues to DMA read/write to the now-freed memory?

Pre-existing behavior exactly the same in upstream, and the ordering already accounts for it:  mana_destroy_txq() / mana_destroy_rxq()  call  mana_destroy_wq_obj()  first, which tells the device to stop using the queue, and only then free the rings and page pool. This patch set doesn't attempt to address this.

Quoted 26 lines

drivers/net/ethernet/microsoft/mana/mana_en.c int mana_detach(struct net_device *ndev, bool from_close)

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +1 -1
-		if (err) {
+		if (err)
 			netdev_err(ndev, "%s failed to deallocate

queues: %d\n", __func__, err);

drivers/net/ethernet/microsoft/mana/mana_en.c int mana_detach(struct net_device *ndev, bool from_close) · line 4507

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -2
-			return err;
-		}
 	}

[Severity: High]
Is the channel_changing boolean flag being used as an ad-hoc lock to guard code sections and prevent concurrent access?

drivers/net/ethernet/microsoft/mana/mana_ethtool.c:mana_set_ringparam(

) {
    ...
	mutex_lock(&apc->vport_mutex);
	if (apc->channel_changing) {
		mutex_unlock(&apc->vport_mutex);
		return -EBUSY;
	}
	apc->channel_changing = true;
	mutex_unlock(&apc->vport_mutex);
    ...
}
Quoted 6 lines

Using a boolean flag to protect entire blocks of code bypasses standard kernel locking mechanisms. This prevents lockdep from analyzing lock dependencies and forces concurrent callers to fail with -EBUSY instead of gracefully waiting.

Could a proper mutex or rwsem be used here instead?

This is the pre-existing upstream locking mechanism. The patch set doesn't attempt to change it.

Long

author
#21 lore raw

PATCH v2 8/13 net: mana: keep per-queue statistics in the port context

In reply to Long Li

struct mana_rxq and struct mana_txq embed their statistics by value, so every reconfiguration - ethtool channel count, ring size and private flags, MTU changes, XDP attach - destroys and recreates them, resetting the interface counters. rx_bytes can be observed going backwards:

rx_bytes before: 4475831638
rx_bytes after: 526629152

Move them into port-context arrays sized to max_queues and allocated for the lifetime of the port, so a queue set can be freed without losing what it accumulated. ndo_get_stats64() walks max_queues, so counters from queues a later reconfiguration removed are still reported and the totals stay monotonic. The ethtool per-queue statistics keep iterating the current count, which is what sizes their string table.

Counters reset on detach/attach the same way before this, and are kept across it now too; it matters more as reconfiguration is hitless and so typically done on a live link.

---
 .../net/ethernet/microsoft/mana/mana_bpf.c    |  4 +-
 drivers/net/ethernet/microsoft/mana/mana_en.c | 93 +++++++++++++++----
 .../ethernet/microsoft/mana/mana_ethtool.c    |  4 +-
 include/net/mana/mana.h                       | 28 ++++--
 4 files changed, 100 insertions(+), 29 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_bpf.c +2 -2
index 84b1484faaec8bf411ce402538862347e68954f0..ed9ec6b8af480a303bfa9c0dd9894f5049d8b6d6 100644
@@ -75,7 +75,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;
@@ -102,7 +102,7 @@ u32 mana_run_xdp(struct net_device *ndev, struct mana_rxq *rxq,
 
 	act = bpf_prog_run_xdp(prog, xdp);
 
-	rx_stats = &rxq->stats;
+	rx_stats = rxq->stats;
 
 	switch (act) {
 	case XDP_PASS:
drivers/net/ethernet/microsoft/mana/mana_en.c +76 -17

198 lines not on this page · raw message

drivers/net/ethernet/microsoft/mana/mana_ethtool.c +2 -2
index 0daacee28001ed45a66ef00449b2c2acbf2859d4..26f5ea1f5091a7fab729ecf3468bdc13886fcccb 100644
@@ -271,7 +271,7 @@ static void mana_get_ethtool_stats(struct net_device *ndev,
 		data[i++] = *(u64 *)(phy_stats + mana_phy_stats[q].offset);
 
 	for (q = 0; q < num_queues; q++) {
-		rx_stats = &apc->rxqs[q]->stats;
+		rx_stats = &apc->rxq_stats[q];
 
 		do {
 			start = u64_stats_fetch_begin(&rx_stats->syncp);
@@ -296,7 +296,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->txq_stats[q];
 
 		do {
 			start = u64_stats_fetch_begin(&tx_stats->syncp);
include/net/mana/mana.h +20 -8
index 4fcd5e307a4805b81c5c82482fc65a4ba68eea89..77cec5b44049202e21f919d4336fdf8ebc248a18 100644
@@ -102,7 +102,10 @@ struct mana_stats_rx {
 	u64 pkt_len0_err;
 	u64 coalesced_cqe[MANA_CQE_COAL_PKTS_8 - 1];
 	struct u64_stats_sync syncp;
-};
+	/* Per-port array indexed by queue, so keep entries on separate cache
+	 * lines: queues polled on different CPUs would bounce a shared one.
+	 */
+} ____cacheline_aligned_in_smp;
 
 struct mana_stats_tx {
 	u64 packets;
@@ -117,7 +120,8 @@ struct mana_stats_tx {
 	u64 csum_partial;
 	u64 mana_map_err;
 	struct u64_stats_sync syncp;
-};
+	/* Per-queue array entry, same cache line reasoning as the RX side. */
+} ____cacheline_aligned_in_smp;
 
 struct mana_txq {
 	struct gdma_queue *gdma_sq;
@@ -146,14 +150,14 @@ struct mana_txq {
 	/* Value of mana_context.reset_gen when this queue was created. */
 	u32 reset_gen;
 
-	/* Set once this queue has been unpublished and is on its way out.
-	 * Its completions must not touch flow control any more: net_txq is
-	 * shared with the queue that replaced it at the same index, and a
-	 * draining queue always looks like it has room.
+	/* Unpublished and draining. Its completions must leave flow control
+	 * alone: net_txq is shared with its replacement, and a draining queue
+	 * always looks like it has room.
 	 */
 	bool retiring;
 
-	struct mana_stats_tx stats;
+	/* Points into apc->txq_stats[], which outlives the queue. */
+	struct mana_stats_tx *stats;
 };
 
 /* skb data and frags dma mappings */
@@ -415,7 +419,8 @@ struct mana_rxq {
 
 	u32 buf_index;
 
-	struct mana_stats_rx stats;
+	/* Points into apc->rxq_stats[], which outlives the queue. */
+	struct mana_stats_rx *stats;
 
 	struct bpf_prog __rcu *bpf_prog;
 	struct xdp_rxq_info xdp_rxq;
@@ -617,6 +622,13 @@ struct mana_port_context {
 	unsigned int max_queues;
 	unsigned int num_queues;
 
+	/* Per-queue counters, max_queues entries each. Allocated at probe and
+	 * freed at remove, never on queue teardown, so a reconfiguration does
+	 * not reset them.
+	 */
+	struct mana_stats_rx *rxq_stats;
+	struct mana_stats_tx *txq_stats;
+
 	unsigned int rx_queue_size;
 	unsigned int tx_queue_size;
 
Signature · 1 line
2.43.0
#22 lore raw

Re: [PATCH net-next v2 08/13] net: mana: keep per-queue statistics in the port context

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [Medium] Concurrent writers to the same `u64_stats_sync` structure during RX queue swap. --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Concurrent writers to the same u64_stats_sync structure during RX queue swap. --

--- Patch [8]: [PATCH net-next v2 08/13] net: mana: keep per-queue statistics in the port context --- commit 48e144b542daf3b7f2ff07b74db60098e784159d
Author: Long Li <[email protected]>

net: mana: keep per-queue statistics in the port context

This commit moves per-queue statistics out of the queue structures and into a port-context array to prevent statistics from resetting when the queue is reconfigured. The stats are indexed by the queue index, and the arrays outlive the queues so totals stay monotonic.

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0

[ ... ]

drivers/net/ethernet/microsoft/mana/mana_en.c static void mana_cleanup_port_context(struct mana_port_context *apc) · line 1076

Quoted 13 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +9 -0
@@ -1059,6 +1065,48 @@ static void mana_cleanup_port_context(struct mana_port_context *apc)
 	apc->rxqs = NULL;
 }
 
+/* Counters belong to the port, not the queues, so a queue-set replacement
+ * does not reset them. Sized to max_queues, allocated once.
+ *
+ * A swap adds no writer to a TX slot. RX slots do overlap briefly, since a
+ * retiring rxq keeps its NAPI until mana_free_qset() destroys it. MANA is
+ * 64-bit only, so u64_stats_sync has no seqcount and at worst a few
+ * increments are lost; the alternatives are a lock in the receive path or
+ * per-set slots that make ndo_get_stats64() dip during a swap.
+ */

[Severity: Medium]
Is it safe to allow concurrent writers to the same u64_stats_sync structure?

The u64_stats_sync API requires that writers must be mutually exclusive for a given syncp. While MANA is 64-bit only and lacks a seqcount in the syncp, allowing both the retiring RX queue's NAPI and the new RX queue's NAPI to concurrently update the same apc->rxq_stats[rxq_idx] introduces a data race.

For example, both could be running mana_rx_skb() at the same time:

	u64_stats_update_begin(&rx_stats->syncp);
	rx_stats->packets++;
	rx_stats->bytes += pkt_len;

Could this lead to undefined behavior or KCSAN warnings when the queues overlap during a swap?

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +3 -0
+static int mana_alloc_queue_stats(struct mana_port_context *apc)
+{
+	unsigned int i;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
author
#23 lore raw

RE: [EXTERNAL] Re: [PATCH net-next v2 08/13] net: mana: keep per-queue statistics in the port context

Quoted 50 lines

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] Concurrent writers to the same u64_stats_sync structure during RX queue swap.
--

--- Patch [8]: [PATCH net-next v2 08/13] net: mana: keep per-queue statistics in the port context --- commit 48e144b542daf3b7f2ff07b74db60098e784159d
Author: Long Li <[email protected]>

net: mana: keep per-queue statistics in the port context

This commit moves per-queue statistics out of the queue structures and into a port-context array to prevent statistics from resetting when the queue is reconfigured. The stats are indexed by the queue index, and the arrays outlive the queues so totals stay monotonic.

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0

[ ... ]

drivers/net/ethernet/microsoft/mana/mana_en.c static void mana_cleanup_port_context(struct

Quoted 1 line
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
@@ -1059,6 +1065,48 @@ static void mana_cleanup_port_context(struct

mana_port_context *apc)

Quoted 6 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +3 -0
 	apc->rxqs = NULL;
 }
 
+/* Counters belong to the port, not the queues, so a queue-set
+replacement
+ * does not reset them. Sized to max_queues, allocated once.

drivers/net/ethernet/microsoft/mana/mana_en.c static void mana_cleanup_port_context(struct mana_port_context *apc) · line 1076

Quoted 10 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +10 -0
+ *
+ * A swap adds no writer to a TX slot. RX slots do overlap briefly,
+since a
+ * retiring rxq keeps its NAPI until mana_free_qset() destroys it.
+MANA is
+ * 64-bit only, so u64_stats_sync has no seqcount and at worst a few
+ * increments are lost; the alternatives are a lock in the receive
+path or
+ * per-set slots that make ndo_get_stats64() dip during a swap.
+ */

[Severity: Medium]
Is it safe to allow concurrent writers to the same u64_stats_sync structure?

The u64_stats_sync API requires that writers must be mutually exclusive for a given syncp. While MANA is 64-bit only and lacks a seqcount in the syncp, allowing both the retiring RX queue's NAPI and the new RX queue's NAPI to concurrently update the same apc->rxq_stats[rxq_idx] introduces a data race.

Quoted 8 lines

For example, both could be running mana_rx_skb() at the same time:

	u64_stats_update_begin(&rx_stats->syncp);
	rx_stats->packets++;
	rx_stats->bytes += pkt_len;

Could this lead to undefined behavior or KCSAN warnings when the queues overlap during a swap?

Wil fix this.

Long

Quoted 14 lines

drivers/net/ethernet/microsoft/mana/mana_en.c static void mana_cleanup_port_context(struct · line 1082

Quoted 3 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +2 -0
+static int mana_alloc_queue_stats(struct mana_port_context *apc) {
+	unsigned int i;

--
Sashiko AI
review * https://nam06.safelinks.protection.outlook.com/?url=https%3A%2F%
2Fsashiko.dev%2F%23%2Fpatchset%2F20260813050418.2906468-1- longli%40microsoft.com%3Fpart%3D8&data=05%7C02%7Clongli%40microsoft. com%7C310903ff6b08415ea5b508def9c1a301%7C72f988bf86f141af91ab2d7c d011db47%7C1%7C0%7C639222807222824332%7CUnknown%7CTWFpbGZsb 3d8eyJFbXB0eU1hcGkiOnRydWUsIlYiOiIwLjAuMDAwMCIsIlAiOiJXaW4zMiIsIkF OIjoiTWFpbCIsIldUIjoyfQ%3D%3D%7C0%7C%7C%7C&sdata=%2BNqZwTPmXgY tTuwJUM%2Fj%2FSq%2F8ZbA9FR0XOQrz81lpDQ%3D&reserved=0

author
#24 lore raw

PATCH v2 9/13 net: mana: share the EQ pool across a queue-set swap

In reply to Long Li

Raising the channel count fails with -ENOSPC once the queue-set swap is in use:

  # ethtool -L ens1 combined 32
  netlink error: No space left on device
  mana 7870:00:00.0: No free MSI vectors available

Building a second set of EQs while the running set still held its own meant peak demand of old + new MSI-X vectors. With 32 usable vectors and a driver that comes up at 16 queues, 16 -> 17 already needs 33, so the advertised maximum is unreachable.

Make the EQ pool belong to the port rather than to a queue set, so both sets share it and peak usage is max(old, new) rather than the sum. The pool only ever grows, up to the maximum channel count ethtool reports, and is released on detach as before.

---
 .../net/ethernet/microsoft/mana/mana_bpf.c    |   2 +-
 drivers/net/ethernet/microsoft/mana/mana_en.c | 173 +++++++++++-------
 .../ethernet/microsoft/mana/mana_ethtool.c    |   6 +-
 include/net/mana/mana.h                       |  34 ++--
 4 files changed, 120 insertions(+), 95 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_bpf.c +1 -1
index ed9ec6b8af480a303bfa9c0dd9894f5049d8b6d6..05936453fbbfa59c563fdea50e0b40096c2b46ac 100644
@@ -213,7 +213,7 @@ static int mana_xdp_set(struct net_device *ndev, struct bpf_prog *prog,
 			return -ENOMEM;
 		}
 
-		err = mana_alloc_qset(scratch, apc->num_queues,
+		err = mana_alloc_qset(apc, scratch, apc->num_queues,
 				      apc->rx_queue_size, apc->tx_queue_size,
 				      apc->priv_flags, apc->configured_mtu,
 				      prog, &newq);
drivers/net/ethernet/microsoft/mana/mana_en.c +103 -70

297 lines not on this page · raw message

drivers/net/ethernet/microsoft/mana/mana_ethtool.c +3 -3
index 26f5ea1f5091a7fab729ecf3468bdc13886fcccb..08e6fb7785cd3be72f8737083e6424783c5e0d21 100644
@@ -722,7 +722,7 @@ static int mana_set_channels(struct net_device *ndev,
 		goto clear_flag;
 	}
 
-	err = mana_alloc_qset(scratch, new_count, apc->rx_queue_size,
+	err = mana_alloc_qset(apc, scratch, new_count, apc->rx_queue_size,
 			      apc->tx_queue_size, apc->priv_flags,
 			      apc->configured_mtu, apc->bpf_prog, &newq);
 	if (err)
@@ -818,7 +818,7 @@ static int mana_set_ringparam(struct net_device *ndev,
 		goto clear_flag;
 	}
 
-	err = mana_alloc_qset(scratch, apc->num_queues, new_rx, new_tx,
+	err = mana_alloc_qset(apc, scratch, apc->num_queues, new_rx, new_tx,
 			      apc->priv_flags, apc->configured_mtu,
 			      apc->bpf_prog, &newq);
 	if (err) {
@@ -917,7 +917,7 @@ static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags)
 		goto clear_flag;
 	}
 
-	err = mana_alloc_qset(scratch, apc->num_queues, apc->rx_queue_size,
+	err = mana_alloc_qset(apc, scratch, apc->num_queues, apc->rx_queue_size,
 			      apc->tx_queue_size, priv_flags,
 			      apc->configured_mtu, apc->bpf_prog, &newq);
 	if (err)
include/net/mana/mana.h +13 -21
index 77cec5b44049202e21f919d4336fdf8ebc248a18..dfb6ba0012fda629192e4fe9cf8aba57fd5bb451 100644
@@ -581,7 +581,12 @@ struct mana_port_context {
 
 	u8 mac_addr[ETH_ALEN];
 
+	/* EQ pool, owned by the port rather than a queue set: EQs are bound to
+	 * MSI-X vectors, which a swap must not double-book. Sized to
+	 * max_queues; num_eqs is how many exist.
+	 */
 	struct mana_eq *eqs;
+	unsigned int num_eqs;
 	struct dentry *mana_eqs_debugfs;
 
 	enum TRI_STATE rss_state;
@@ -702,18 +707,11 @@ struct mana_port_context {
 	u32 steer_cqe_coalescing;
 };
 
-/* struct mana_qset - a self-contained snapshot of the queue-related
- * fields inside mana_port_context that can be swapped atomically.
- *
- * Prototype for the "pre-allocate + swap" reconfiguration path (as
- * suggested by netdev maintainers): a new qset is allocated while the
- * current one keeps serving traffic, then apc's queue fields are
- * atomically switched to the new set and the old set is torn down.
- * The vport (port_handle / vport_use_count) is *not* touched, so RDMA
- * can never race in during reconfiguration.
+/* The queue-related fields of mana_port_context that can be swapped as a
+ * unit. The vport (port_handle, vport_use_count) is not part of it and is
+ * never touched by a swap.
  */
 struct mana_qset {
-	struct mana_eq		*eqs;
 	struct mana_tx_qp	**tx_qp;
 	struct mana_rxq		**rxqs;
 
@@ -733,13 +731,6 @@ struct mana_qset {
 	 */
 	int			mtu;
 	struct bpf_prog		*bpf_prog;
-
-	/* Per-queue-set debugfs root ("EQs"). Owned by the qset: it is
-	 * recreated by mana_create_eq() for each new set and torn down
-	 * with that set, so it must travel with the qset rather than
-	 * staying on apc.
-	 */
-	struct dentry		*mana_eqs_debugfs;
 };
 
 netdev_tx_t mana_start_xmit(struct sk_buff *skb, struct net_device *ndev);
@@ -751,13 +742,14 @@ int mana_alloc_queues(struct net_device *ndev);
 int mana_attach(struct net_device *ndev);
 int mana_detach(struct net_device *ndev, bool from_close);
 
-/* Pre-allocate + swap reconfiguration path (prototype). Allocation and
- * teardown run against a scratch context so the live port context is only
- * mutated inside mana_publish_qset(), with TX disabled.
+/* Pre-allocate + swap reconfiguration. Allocation and teardown run against a
+ * scratch context, so the live port context is mutated only inside
+ * mana_publish_qset() with TX disabled. Both sets share a port-owned EQ pool.
  */
 struct mana_port_context *mana_qset_scratch_alloc(struct mana_port_context *apc);
 void mana_qset_scratch_free(struct mana_port_context *scratch);
-int mana_alloc_qset(struct mana_port_context *scratch, unsigned int num_queues,
+int mana_alloc_qset(struct mana_port_context *apc,
+		    struct mana_port_context *scratch, unsigned int num_queues,
 		    unsigned int rx_queue_size, unsigned int tx_queue_size,
 		    u32 priv_flags, int mtu, struct bpf_prog *bpf_prog,
 		    struct mana_qset *out);
Signature · 1 line
2.43.0
maintainer
#25 lore raw

Re: [PATCH net-next v2 09/13] net: mana: share the EQ pool across a queue-set swap

Quoted 12 lines · On Wed, 12 Aug 2026 22:04:14 -0700 Long Li wrote:

Raising the channel count fails with -ENOSPC once the queue-set swap is in use:

  # ethtool -L ens1 combined 32
  netlink error: No space left on device
  mana 7870:00:00.0: No free MSI vectors available

Building a second set of EQs while the running set still held its own meant peak demand of old + new MSI-X vectors. With 32 usable vectors and a driver that comes up at 16 queues, 16 -> 17 already needs 33, so the advertised maximum is unreachable.

Quoted 4 lines

Make the EQ pool belong to the port rather than to a queue set, so both sets share it and peak usage is max(old, new) rather than the sum. The pool only ever grows, up to the maximum channel count ethtool reports, and is released on detach as before.

Warning: drivers/net/ethernet/microsoft/mana/mana_en.c:1909 No description found for return value of 'mana_grow_eqs'

author
#26 lore raw

PATCH v2 10/13 net: mana: release EQs left idle by a channel-count reduction

In reply to Long Li

The shared EQ pool only grows, so it sits at the high-water mark of every channel count the port has ever used. After "ethtool -L ens1 combined 32" then "combined 4" the port keeps 32 EQs and 32 MSI-X vectors while using four:

  # ethtool -L ens1 combined 4
  # grep -c mana /proc/interrupts
  33

The pre-swap path recreated every EQ per reconfiguration, so this is new.

Release the EQs above the live queue count once a retiring set has been torn down. That is the only safe point: a CQ holds the gdma_queue pointer of its parent EQ, so an EQ may only be destroyed once the set referencing it is gone.

While here, fix mana_create_eq_debugfs(), which stored the new dentry in a stack copy rather than in apc->eqs[i].

---
 .../net/ethernet/microsoft/mana/mana_bpf.c    |  4 +-
 drivers/net/ethernet/microsoft/mana/mana_en.c | 73 +++++++++++++++----
 .../ethernet/microsoft/mana/mana_ethtool.c    | 15 ++--
 include/net/mana/mana.h                       |  3 +-
 4 files changed, 70 insertions(+), 25 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_bpf.c +2 -2
index 05936453fbbfa59c563fdea50e0b40096c2b46ac..4b29406595b37877e1e5d14cf93d68aa3c4ace02 100644
@@ -228,7 +228,7 @@ static int mana_xdp_set(struct net_device *ndev, struct bpf_prog *prog,
 		if (err) {
 			NL_SET_ERR_MSG_MOD(extack,
 					   "XDP: Re-config failed at publish");
-			mana_free_qset(scratch, &newq);
+			mana_free_qset(apc, scratch, &newq);
 			/* After the cleanup above: closing destroys the EQ pool
 			 * those queues' CQs were attached to.
 			 */
@@ -237,7 +237,7 @@ static int mana_xdp_set(struct net_device *ndev, struct bpf_prog *prog,
 			return err;
 		}
 
-		mana_free_qset(scratch, &oldq);
+		mana_free_qset(apc, scratch, &oldq);
 		mana_qset_scratch_free(scratch);
 	} else {
 		/* No queues to rebuild; mana_open() will size the RX buffers
drivers/net/ethernet/microsoft/mana/mana_en.c +60 -13

128 lines not on this page · raw message

drivers/net/ethernet/microsoft/mana/mana_ethtool.c +6 -9
index 08e6fb7785cd3be72f8737083e6424783c5e0d21..024119dd4e353e33d11ccc883b4fe09a99434a26 100644
@@ -730,16 +730,13 @@ static int mana_set_channels(struct net_device *ndev,
 
 	err = mana_publish_qset(apc, &newq, &oldq);
 	if (err) {
-		mana_free_qset(scratch, &newq);
+		mana_free_qset(apc, scratch, &newq);
 		goto free_scratch;
 	}
 
-	mana_free_qset(scratch, &oldq);
+	mana_free_qset(apc, scratch, &oldq);
 
 free_scratch:
-	/* After the caller-side cleanup above, so the EQ pool outlives the
-	 * CQs that reference it.
-	 */
 	mana_publish_close_if_needed(apc);
 	mana_qset_scratch_free(scratch);
 clear_flag:
@@ -831,11 +828,11 @@ static int mana_set_ringparam(struct net_device *ndev,
 	if (err) {
 		NL_SET_ERR_MSG_FMT(extack, "failed to change ring params: %d",
 				   err);
-		mana_free_qset(scratch, &newq);
+		mana_free_qset(apc, scratch, &newq);
 		goto free_scratch;
 	}
 
-	mana_free_qset(scratch, &oldq);
+	mana_free_qset(apc, scratch, &oldq);
 
 free_scratch:
 	/* After the caller-side cleanup above, so the EQ pool outlives the
@@ -925,11 +922,11 @@ static int mana_set_priv_flags(struct net_device *ndev, u32 priv_flags)
 
 	err = mana_publish_qset(apc, &newq, &oldq);
 	if (err) {
-		mana_free_qset(scratch, &newq);
+		mana_free_qset(apc, scratch, &newq);
 		goto free_scratch;
 	}
 
-	mana_free_qset(scratch, &oldq);
+	mana_free_qset(apc, scratch, &oldq);
 
 free_scratch:
 	mana_publish_close_if_needed(apc);
include/net/mana/mana.h +2 -1
index dfb6ba0012fda629192e4fe9cf8aba57fd5bb451..619c66f3c6192dd2988a1bba73e991df2a773923 100644
@@ -756,7 +756,8 @@ int mana_alloc_qset(struct mana_port_context *apc,
 int mana_publish_qset(struct mana_port_context *apc, struct mana_qset *newq,
 		      struct mana_qset *out_old);
 void mana_publish_close_if_needed(struct mana_port_context *apc);
-void mana_free_qset(struct mana_port_context *scratch, struct mana_qset *qset);
+void mana_free_qset(struct mana_port_context *apc,
+		    struct mana_port_context *scratch, struct mana_qset *qset);
 
 void mana_dim_change(struct mana_cq *cq, bool enable);
 
Signature · 1 line
2.43.0
author
#27 lore raw

PATCH v2 11/13 net: mana: keep a user-configured RSS table across a queue rebuild

In reply to Long Li

A queue rebuild regenerates the RSS indirection table from the driver default, so a table the user installed with "ethtool -X" is silently replaced by any reconfiguration that rebuilds the queues.

Carry it over instead when the queue count is unchanged: the entries are queue indices, so they stay meaningful. A driver-generated table is still regenerated, and a table that cannot be honoured for the new queue count is reported to the core as lost - but only once the new set is actually live, so a failed reconfiguration does not clear IFF_RXFH_CONFIGURED on a port that kept its old queues.

---
 drivers/net/ethernet/microsoft/mana/mana_en.c | 78 +++++++++++++++++--
 include/net/mana/mana.h                       |  7 ++
 2 files changed, 80 insertions(+), 5 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_en.c +73 -5
index 7c43c2f9043ba591b58e4ee2211cf37da9fead36..6e77b59cfcf907f7e584625273972bf185501d20 100644
@@ -3496,6 +3496,38 @@ static void mana_rss_table_init(struct mana_port_context *apc)
 			ethtool_rxfh_indir_default(i, apc->num_queues);
 }
 
+/* Whether @apc's indirection table can be carried to a set of @num_queues,
+ * rather than rebuilt from the driver default.
+ *
+ * Only a user table ("ethtool -X") is kept; a driver one is rebuilt to spread
+ * over the new count. ethtool_check_max_channel() already refuses a reduction
+ * that leaves a user table pointing past the last queue, so the bounds check
+ * below only guards rebuild paths that bypass ethtool.
+ *
+ * @lost reports a table that cannot be carried instead of calling
+ * ethtool_rxfh_indir_lost() here, since the swap may still fail and leave the
+ * port on queues where the table still applies. False when none was set.
+ */
+static bool mana_rss_table_keep(struct mana_port_context *apc,
+				unsigned int num_queues, bool *lost)
+{
+	u32 i;
+
+	*lost = false;
+
+	if (!netif_is_rxfh_configured(apc->ndev))
+		return false;
+
+	for (i = 0; i < apc->indir_table_sz; i++) {
+		if (apc->indir_table[i] >= num_queues) {
+			*lost = true;
+			return false;
+		}
+	}
+
+	return true;
+}
+
 int mana_disable_vport_rx(struct mana_port_context *apc)
 {
 	return mana_cfg_vport_steering(apc, TRI_STATE_FALSE, false, false,
@@ -3766,6 +3798,7 @@ int mana_alloc_queues(struct net_device *ndev)
 {
 	struct mana_port_context *apc = netdev_priv(ndev);
 	struct gdma_dev *gd = apc->ac->gdma_dev;
+	bool indir_lost;
 	int err;
 
 	err = mana_create_vport(apc, ndev);
@@ -3811,7 +3844,18 @@ int mana_alloc_queues(struct net_device *ndev)
 		goto destroy_rxq;
 	}
 
-	mana_rss_table_init(apc);
+	/* Keep a user-configured RSS table across a rebuild; the entries are
+	 * queue indices, so they stay meaningful as long as the queue count
+	 * is unchanged. Only a driver-generated table is regenerated here.
+	 *
+	 * Nothing to roll back to here, so report the loss as soon as it is
+	 * decided and keep the table and the core's view of it in step.
+	 */
+	if (!mana_rss_table_keep(apc, apc->num_queues, &indir_lost)) {
+		if (indir_lost)
+			ethtool_rxfh_indir_lost(ndev);
+		mana_rss_table_init(apc);
+	}
 
 	err = mana_config_rss(apc, TRI_STATE_TRUE, true, true);
 	if (err) {
@@ -4068,11 +4112,15 @@ static void mana_qset_snapshot(const struct mana_port_context *ctx,
 	out->priv_flags		= ctx->priv_flags;
 	out->mtu		= ctx->configured_mtu;
 	out->bpf_prog		= ctx->bpf_prog;
+
+	/* A set taken from a live context has nothing pending; the builders
+	 * set this after snapshotting if they had to drop the user's table.
+	 */
+	out->rxfh_indir_lost	= false;
 }
 
-/* Install @qset's fields onto @ctx. The vport (port_handle,
- * vport_use_count) and the port-level debugfs dir are deliberately not
- * touched: they outlive any individual queue set.
+/* The vport (port_handle, vport_use_count) and the port-level debugfs dir are
+ * not touched: they outlive any individual queue set.
  */
 static void mana_qset_install(struct mana_port_context *ctx,
 			      const struct mana_qset *qset)
@@ -4150,6 +4198,7 @@ int mana_alloc_qset(struct mana_port_context *apc,
 		    struct mana_qset *out)
 {
 	struct net_device *ndev = scratch->ndev;
+	bool indir_lost;
 	int err;
 
 	ASSERT_RTNL();
@@ -4193,9 +4242,19 @@ int mana_alloc_qset(struct mana_port_context *apc,
 	if (err)
 		goto cleanup_rxq;
 
-	mana_rss_table_init(scratch);
+	/* Carry a user-configured RSS table over to the new set. The entries
+	 * are queue indices, so mana_config_rss() in mana_publish_qset() maps
+	 * them onto the new set's RX objects. A driver-generated table is
+	 * rebuilt instead, so it covers every queue of the new set.
+	 */
+	if (mana_rss_table_keep(apc, num_queues, &indir_lost))
+		memcpy(scratch->indir_table, apc->indir_table,
+		       apc->indir_table_sz * sizeof(*apc->indir_table));
+	else
+		mana_rss_table_init(scratch);
 
 	mana_qset_snapshot(scratch, out);
+	out->rxfh_indir_lost = indir_lost;
 	return 0;
 
 cleanup_rxq:
@@ -4407,6 +4466,15 @@ int mana_publish_qset(struct mana_port_context *apc, struct mana_qset *newq,
 	if (carrier_ok)
 		netif_carrier_on(ndev);
 
+	/* The set that could not carry the user's indirection table is the one
+	 * serving traffic now, so the table really is gone. Reporting it here
+	 * rather than while the set was being built keeps a failed swap from
+	 * clearing IFF_RXFH_CONFIGURED on a port that kept its old queues, and
+	 * with them a table that is still valid and still programmed.
+	 */
+	if (newq->rxfh_indir_lost)
+		ethtool_rxfh_indir_lost(ndev);
+
 	return 0;
 
 rollback:
include/net/mana/mana.h +7 -0
index 619c66f3c6192dd2988a1bba73e991df2a773923..0d7375adeb5366bbbb99a7faa931a8830f71ef10 100644
@@ -731,6 +731,13 @@ struct mana_qset {
 	 */
 	int			mtu;
 	struct bpf_prog		*bpf_prog;
+
+	/* The user's RSS indirection table could not be carried onto this set,
+	 * so a default one was generated for it. mana_publish_qset() tells the
+	 * core once this set is live; until then the port is still running on
+	 * a set where the user's table applies.
+	 */
+	bool			rxfh_indir_lost;
 };
 
 netdev_tx_t mana_start_xmit(struct sk_buff *skb, struct net_device *ndev);
Signature · 1 line
2.43.0
#28 lore raw

Re: [PATCH net-next v2 11/13] net: mana: keep a user-configured RSS table across a queue rebuild

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

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

Pre-existing issues:
- [Medium] Desynchronization between apc->indir_table_sz and the actual heap allocation size of apc->indir_table during device reset leads to heap out-of-bounds read and write. --

--- Patch [11]: [PATCH net-next v2 11/13] net: mana: keep a user-configured RSS table across a queue rebuild --- commit e2bf133521a3f8f376f19cc855758c1eb53a195b
Author: Long Li <[email protected]>
net: mana: keep a user-configured RSS table across a queue rebuild

A queue rebuild regenerates the RSS indirection table from the driver default. This patch updates the driver to carry the user-configured table over instead when the queue count is unchanged.

Quoted 4 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +0 -0
index 7c43c2f9043ba..6e77b59cfcf90 100644

[ ... ]

drivers/net/ethernet/microsoft/mana/mana_en.c static void mana_rss_table_init(struct mana_port_context *apc) · line 3522

Quoted 12 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +12 -0
+static bool mana_rss_table_keep(struct mana_port_context *apc,
+				unsigned int num_queues, bool *lost)
+{
+	u32 i;
+
+	*lost = false;
+
+	if (!netif_is_rxfh_configured(apc->ndev))
+		return false;
+
+	for (i = 0; i < apc->indir_table_sz; i++) {
+		if (apc->indir_table[i] >= num_queues) {

[Severity: Medium]
This is a pre-existing issue, but can this loop perform an out-of-bounds read on apc->indir_table?

During a device reset, mana_init_port() re-queries the hardware and updates apc->indir_table_sz directly:

mana_init_port() {
	...
	err = mana_query_vport_cfg(apc, port_idx, &max_txq, &max_rxq,
				   &apc->indir_table_sz);
	...
}

If the hardware returns a larger size than what was originally allocated for apc->indir_table, does the driver reallocate the array to match the new apc->indir_table_sz? If not, this loop might read past the end of the existing heap allocation, which this patch extends into the new mana_rss_table_keep() function.

Quoted 7 lines
drivers/net/ethernet/microsoft/mana/mana_en.c +7 -0
+			*lost = true;
+			return false;
+		}
+	}
+
+	return true;
+}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
author
#29 lore raw

PATCH v2 12/13 net: mana: keep the surviving queues when the channel count is reduced

In reply to Long Li

A channel-count reduction rebuilds every queue it keeps, even though none of their properties change. Shrinking 16 channels to 8 creates 8 TX and 8 RX queues and then destroys all 16 of each:

set_channels 16 -> 8 created SQ=8 RQ=8 | destroyed SQ=16 RQ=16

A queue is derived from the ring sizes, the MTU/priv-flag/XDP buffer layout and its EQ, none of which depend on the count, and queue i is bound to EQ i at any count. Carry the survivors over and retire only the
tail: a reduction allocates two pointer arrays and a steering table instead of a full queue set, the peak stays at one set rather than two, and the queues that stay keep their page pools, posted RX buffers and NAPI state.

A ring resize, MTU change, priv-flag toggle or XDP attach changes a property of every queue, so those still rebuild.

---
 drivers/net/ethernet/microsoft/mana/mana_en.c | 127 ++++++++++++++++++
 .../ethernet/microsoft/mana/mana_ethtool.c    |  33 +++++
 include/net/mana/mana.h                       |   4 +
 3 files changed, 164 insertions(+)
drivers/net/ethernet/microsoft/mana/mana_en.c +127 -0
index 6e77b59cfcf907f7e584625273972bf185501d20..c2b8ac67963fb6134a21b682a1b9ee373b1d0b7c 100644
@@ -4186,6 +4186,133 @@ void mana_qset_scratch_free(struct mana_port_context *scratch)
 	kvfree(scratch);
 }
 
+/* Carve the live set into a kept prefix [0, @new_count) in @out_new and a
+ * tail to retire in @out_tail. @apc is untouched, on failure too.
+ *
+ * Queue i is built from the ring sizes and the buffer layout and keeps EQ i
+ * at any count, so a reduction carries the survivors over and destroys only
+ * the tail. Allocates two pointer arrays and a steering table, nothing else:
+ * the queues that stay keep their page pools, posted buffers and NAPI.
+ */
+int mana_split_qset(struct mana_port_context *apc,
+		    struct mana_port_context *scratch, unsigned int new_count,
+		    struct mana_qset *out_new, struct mana_qset *out_tail)
+{
+	unsigned int old_count = apc->num_queues;
+	struct mana_tx_qp **new_tx, **tail_tx;
+	struct mana_rxq **new_rx, **tail_rx;
+	unsigned int tail_count;
+	bool indir_lost;
+	unsigned int i;
+	int err;
+
+	ASSERT_RTNL();
+
+	if (WARN_ON(new_count == 0 || new_count >= old_count))
+		return -EINVAL;
+	if (WARN_ON(!apc->tx_qp || !apc->rxqs))
+		return -EINVAL;
+
+	tail_count = old_count - new_count;
+
+	/* Build the smaller set's steering table separately: mana_config_rss()
+	 * would otherwise index the shorter rxqs[] with entries still referring
+	 * to retired queues.
+	 */
+	scratch->num_queues = new_count;
+	err = mana_rss_table_alloc(scratch);
+	if (err)
+		return err;
+
+	if (mana_rss_table_keep(apc, new_count, &indir_lost))
+		memcpy(scratch->indir_table, apc->indir_table,
+		       apc->indir_table_sz * sizeof(*apc->indir_table));
+	else
+		mana_rss_table_init(scratch);
+
+	new_tx = kzalloc_objs(struct mana_tx_qp *, new_count);
+	new_rx = kzalloc_objs(struct mana_rxq *, new_count);
+	tail_tx = kzalloc_objs(struct mana_tx_qp *, tail_count);
+	tail_rx = kzalloc_objs(struct mana_rxq *, tail_count);
+	if (!new_tx || !new_rx || !tail_tx || !tail_rx) {
+		err = -ENOMEM;
+		goto free_arrays;
+	}
+
+	for (i = 0; i < new_count; i++) {
+		new_tx[i] = apc->tx_qp[i];
+		new_rx[i] = apc->rxqs[i];
+	}
+	for (i = 0; i < tail_count; i++) {
+		tail_tx[i] = apc->tx_qp[new_count + i];
+		tail_rx[i] = apc->rxqs[new_count + i];
+	}
+
+	/* The kept prefix, with the new steering table. */
+	out_new->tx_qp		= new_tx;
+	out_new->rxqs		= new_rx;
+	out_new->indir_table	= scratch->indir_table;
+	out_new->indir_table_sz	= scratch->indir_table_sz;
+	out_new->rxobj_table	= scratch->rxobj_table;
+	out_new->default_rxobj	= apc->rxqs[0]->rxobj;
+	out_new->num_queues	= new_count;
+	out_new->rx_queue_size	= apc->rx_queue_size;
+	out_new->tx_queue_size	= apc->tx_queue_size;
+	out_new->priv_flags	= apc->priv_flags;
+	out_new->mtu		= apc->configured_mtu;
+	out_new->bpf_prog	= apc->bpf_prog;
+	out_new->rxfh_indir_lost = indir_lost;
+
+	/* Ownership of the table moved to @out_new. */
+	scratch->indir_table	= NULL;
+	scratch->rxobj_table	= NULL;
+
+	/* The tail. It owns no steering table; bpf_prog is carried so that
+	 * retiring it drops exactly the tail's per-queue program references
+	 * and leaves the kept ones alone.
+	 */
+	memset(out_tail, 0, sizeof(*out_tail));
+	out_tail->tx_qp		= tail_tx;
+	out_tail->rxqs		= tail_rx;
+	out_tail->default_rxobj	= INVALID_MANA_HANDLE;
+	out_tail->num_queues	= tail_count;
+	out_tail->rx_queue_size	= apc->rx_queue_size;
+	out_tail->tx_queue_size	= apc->tx_queue_size;
+	out_tail->priv_flags	= apc->priv_flags;
+	out_tail->mtu		= apc->configured_mtu;
+	out_tail->bpf_prog	= apc->bpf_prog;
+
+	return 0;
+
+free_arrays:
+	kfree(new_tx);
+	kfree(new_rx);
+	kfree(tail_tx);
+	kfree(tail_rx);
+	mana_cleanup_indir_table(scratch);
+	return err;
+}
+
+/**
+ * mana_discard_split - drop the containers built by mana_split_qset()
+ * @newq:  set that was never published
+ * @tailq: matching tail
+ *
+ * Frees the pointer arrays and the steering table only: the queues they refer
+ * to are still owned by the live port context.
+ */
+void mana_discard_split(struct mana_qset *newq, struct mana_qset *tailq)
+{
+	kfree(newq->tx_qp);
+	kfree(newq->rxqs);
+	kfree(newq->indir_table);
+	kfree(newq->rxobj_table);
+	kfree(tailq->tx_qp);
+	kfree(tailq->rxqs);
+	memset(newq, 0, sizeof(*newq));
+	memset(tailq, 0, sizeof(*tailq));
+}
+
 /* Rebuild the queues at the current count in @scratch, for callers changing a
  * per-queue property; a count change goes through mana_split_qset() or
  * mana_grow_qset(), so this never has to add an EQ. The installed set keeps
drivers/net/ethernet/microsoft/mana/mana_ethtool.c +33 -0
index 024119dd4e353e33d11ccc883b4fe09a99434a26..415422aa68672accd03c161732fa1e96dd5562ba 100644
@@ -722,6 +722,39 @@ static int mana_set_channels(struct net_device *ndev,
 		goto clear_flag;
 	}
 
+	/* A reduction keeps its queues configured identically, so carry them
+	 * over and retire only the tail: no DMA ring, no hardware WQ object,
+	 * and no old+new peak.
+	 */
+	if (new_count < apc->num_queues) {
+		struct mana_qset tailq;
+
+		err = mana_split_qset(apc, scratch, new_count, &newq, &tailq);
+		if (err)
+			goto free_scratch; /* current qset untouched */
+
+		err = mana_publish_qset(apc, &newq, &oldq);
+		if (err) {
+			/* The old set is live again; drop only the containers
+			 * built above, never the queues they point at.
+			 */
+			mana_discard_split(&newq, &tailq);
+			goto free_scratch;
+		}
+
+		/* @oldq holds the original arrays and steering table. Every
+		 * queue they referenced is now owned by either the published
+		 * set or the tail, so only the containers are freed here.
+		 */
+		kfree(oldq.tx_qp);
+		kfree(oldq.rxqs);
+		kfree(oldq.indir_table);
+		kfree(oldq.rxobj_table);
+
+		mana_free_qset(apc, scratch, &tailq);
+		goto free_scratch;
+	}
+
 	err = mana_alloc_qset(apc, scratch, new_count, apc->rx_queue_size,
 			      apc->tx_queue_size, apc->priv_flags,
 			      apc->configured_mtu, apc->bpf_prog, &newq);
include/net/mana/mana.h +4 -0
index 0d7375adeb5366bbbb99a7faa931a8830f71ef10..cd41a135815710162b1e5fd3edafa119ae12d1dd 100644
@@ -760,6 +760,10 @@ int mana_alloc_qset(struct mana_port_context *apc,
 		    unsigned int rx_queue_size, unsigned int tx_queue_size,
 		    u32 priv_flags, int mtu, struct bpf_prog *bpf_prog,
 		    struct mana_qset *out);
+int mana_split_qset(struct mana_port_context *apc,
+		    struct mana_port_context *scratch, unsigned int new_count,
+		    struct mana_qset *out_new, struct mana_qset *out_tail);
+void mana_discard_split(struct mana_qset *newq, struct mana_qset *tailq);
 int mana_publish_qset(struct mana_port_context *apc, struct mana_qset *newq,
 		      struct mana_qset *out_old);
 void mana_publish_close_if_needed(struct mana_port_context *apc);
Signature · 1 line
2.43.0
author
#30 lore raw

PATCH v2 13/13 net: mana: keep the existing queues when the channel count is raised

In reply to Long Li

The reduction path already carries its surviving queues over. An increase still builds a complete second set and throws the running one away, even though it keeps every queue it already had:

set_channels 4 -> 8 created SQ=8 RQ=8 | destroyed SQ=4 RQ=4

The same reasoning applies in both directions. Carry the running queues over and build only the new tail, so growing 4 channels to 8 creates 4 SQ/RQ pairs instead of 8 and never holds 12 of each against the vport maximum.

This completes the conversion, so advertise it to the firmware. Every resize path - channel count, ring size, MTU, the full-page RX private flag and XDP attach - now builds the new set before retiring the old and keeps the old one running if that fails.

---
 .../net/ethernet/microsoft/mana/mana_bpf.c    |   2 +-
 drivers/net/ethernet/microsoft/mana/mana_en.c | 246 +++++++++++++++---
 .../ethernet/microsoft/mana/mana_ethtool.c    |  41 ++-
 include/net/mana/gdma.h                       |  11 +-
 include/net/mana/mana.h                       |   7 +-
 5 files changed, 258 insertions(+), 49 deletions(-)
drivers/net/ethernet/microsoft/mana/mana_bpf.c +1 -1
index 4b29406595b37877e1e5d14cf93d68aa3c4ace02..f47755fa866004612bd4bd06b417172c7acf9eb7 100644
@@ -213,7 +213,7 @@ static int mana_xdp_set(struct net_device *ndev, struct bpf_prog *prog,
 			return -ENOMEM;
 		}
 
-		err = mana_alloc_qset(apc, scratch, apc->num_queues,
+		err = mana_alloc_qset(apc, scratch,
 				      apc->rx_queue_size, apc->tx_queue_size,
 				      apc->priv_flags, apc->configured_mtu,
 				      prog, &newq);
drivers/net/ethernet/microsoft/mana/mana_en.c +211 -35

388 lines not on this page · raw message

drivers/net/ethernet/microsoft/mana/mana_ethtool.c +30 -11

79 lines not on this page · raw message

include/net/mana/gdma.h +10 -1
index 70a7f1fee5d3b0a6460cbd76159d01a369838d68..c54500700f6f2f4b432102364e64fd021e7c2c88 100644
@@ -672,6 +672,14 @@ enum {
 /* Driver supports dynamic interrupt moderation - DIM */
 #define GDMA_DRV_CAP_FLAG_1_DYN_INTERRUPT_MODERATION BIT(28)
 
+/* Driver recovers by itself when a queue resize fails: a failed resize leaves
+ * the queues that were already serving traffic in place, so the host does not
+ * have to bring the port back. This covers the resize itself failing. It does
+ * not promise recovery when restoring the previous queue set fails too, which
+ * leaves the port administratively down for the admin to bring back up.
+ */
+#define GDMA_DRV_CAP_FLAG_1_SELF_RECOVERY_ON_QUEUE_RESIZE_FAILURE BIT(31)
+
 #define GDMA_DRV_CAP_FLAGS1 \
 	(GDMA_DRV_CAP_FLAG_1_EQ_SHARING_MULTI_VPORT | \
 	 GDMA_DRV_CAP_FLAG_1_NAPI_WKDONE_FIX | \
@@ -688,7 +696,8 @@ enum {
 	 GDMA_DRV_CAP_FLAG_1_HANDLE_STALL_SQ_RECOVERY | \
 	 GDMA_DRV_CAP_FLAG_1_HWC_TIMEOUT_RECOVERY | \
 	 GDMA_DRV_CAP_FLAG_1_EQ_MSI_UNSHARE_MULTI_VPORT | \
-	 GDMA_DRV_CAP_FLAG_1_DYN_INTERRUPT_MODERATION)
+	 GDMA_DRV_CAP_FLAG_1_DYN_INTERRUPT_MODERATION | \
+	 GDMA_DRV_CAP_FLAG_1_SELF_RECOVERY_ON_QUEUE_RESIZE_FAILURE)
 
 #define GDMA_DRV_CAP_FLAGS2 0
 
include/net/mana/mana.h +6 -1
index cd41a135815710162b1e5fd3edafa119ae12d1dd..8857e7739d2c2505255403defb09749c85804922 100644
@@ -756,7 +756,7 @@ int mana_detach(struct net_device *ndev, bool from_close);
 struct mana_port_context *mana_qset_scratch_alloc(struct mana_port_context *apc);
 void mana_qset_scratch_free(struct mana_port_context *scratch);
 int mana_alloc_qset(struct mana_port_context *apc,
-		    struct mana_port_context *scratch, unsigned int num_queues,
+		    struct mana_port_context *scratch,
 		    unsigned int rx_queue_size, unsigned int tx_queue_size,
 		    u32 priv_flags, int mtu, struct bpf_prog *bpf_prog,
 		    struct mana_qset *out);
@@ -764,11 +764,16 @@ int mana_split_qset(struct mana_port_context *apc,
 		    struct mana_port_context *scratch, unsigned int new_count,
 		    struct mana_qset *out_new, struct mana_qset *out_tail);
 void mana_discard_split(struct mana_qset *newq, struct mana_qset *tailq);
+int mana_grow_qset(struct mana_port_context *apc,
+		   struct mana_port_context *scratch, unsigned int new_count,
+		   struct mana_qset *out_new, struct mana_qset *out_fresh);
+void mana_discard_grow(struct mana_qset *newq);
 int mana_publish_qset(struct mana_port_context *apc, struct mana_qset *newq,
 		      struct mana_qset *out_old);
 void mana_publish_close_if_needed(struct mana_port_context *apc);
 void mana_free_qset(struct mana_port_context *apc,
 		    struct mana_port_context *scratch, struct mana_qset *qset);
+void mana_qset_debugfs_publish(struct mana_port_context *apc);
 
 void mana_dim_change(struct mana_cq *cq, bool enable);
 
Signature · 1 line
2.43.0