From: Long Li <[email protected]>

Carry the XDP program with the queue set and install its per-queue
references before redirecting RSS. This keeps the RX buffer layout and
program together during publication and rollback.

Do not replace the live program during allocation. This also avoids the
pre-existing failed-preallocation stale-pointer bug; its standalone net
fix is linked below.

This full rebuild temporarily keeps both sets of SQ/RQ/CQ objects,
RX buffers and page pools live. The port-owned EQ pool is shared,
not doubled. Device resource limits may reject this temporary peak;
allocation failure preserves the live queues and configuration.

Reserve RAW-QP admission under vport_mutex for the entire live
replacement, including unpublished queues, fail-close and scratch
cleanup. Preserve validation, no-op and down-port behavior.

Link: https://lore.kernel.org/all/[email protected]/

Signed-off-by: Long Li <[email protected]>
Signed-off-by: Wei Hu <[email protected]>
---
 .../net/ethernet/microsoft/mana/mana_bpf.c    | 75 +++++++++++--------
 drivers/net/ethernet/microsoft/mana/mana_en.c | 12 ++-
 .../ethernet/microsoft/mana/mana_ethtool.c    | 11 +--
 include/net/mana/mana.h                       |  5 +-
 4 files changed, 60 insertions(+), 43 deletions(-)

diff --git a/drivers/net/ethernet/microsoft/mana/mana_bpf.c 
b/drivers/net/ethernet/microsoft/mana/mana_bpf.c
index 80950d5b62c5..0ad3a0fa6747 100644
--- a/drivers/net/ethernet/microsoft/mana/mana_bpf.c
+++ b/drivers/net/ethernet/microsoft/mana/mana_bpf.c
@@ -177,6 +177,8 @@ 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;
@@ -196,47 +198,59 @@ 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.
-                */
+               /* Fail-close may release the Ethernet vport reference. */
+               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);
 
-               /* 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");
-                       apc->bpf_prog = old_prog;
-                       return err;
+                                          "XDP: Insufficient memory for 
re-config");
+                       err = -ENOMEM;
+                       goto clear_flag;
                }
 
-               err = mana_detach(ndev, false);
+               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);
                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");
+                       goto free_scratch;
                }
 
-               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);
+                       /* Free the queues before closing their shared EQ pool.
+                        */
+                       mana_publish_close_if_needed(apc);
+                       goto free_scratch;
                }
 
-               mana_chn_setxdp(apc, prog);
-               mana_pre_dealloc_rxbufs(apc);
+               mana_free_qset(scratch, &oldq);
+free_scratch:
+               mana_qset_scratch_free(scratch);
+clear_flag:
+               mutex_lock(&apc->vport_mutex);
+               apc->channel_changing = false;
+               mutex_unlock(&apc->vport_mutex);
+               if (err)
+                       return err;
+       } else {
+               /* Use the caller's program reference; mana_open() installs it
+                * on queues.
+                */
+               apc->bpf_prog = prog;
        }
 
        if (old_prog)
@@ -249,11 +263,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)
diff --git a/drivers/net/ethernet/microsoft/mana/mana_en.c 
b/drivers/net/ethernet/microsoft/mana/mana_en.c
index 4577a473ff42..51f9bfc64c6b 100644
--- a/drivers/net/ethernet/microsoft/mana/mana_en.c
+++ b/drivers/net/ethernet/microsoft/mana/mana_en.c
@@ -952,9 +952,9 @@ static int mana_change_mtu(struct net_device *ndev, int 
new_mtu)
                goto clear_flag;
        }
 
-       err = mana_alloc_qset(mpc, scratch, mpc->num_queues,
-                             mpc->rx_queue_size, mpc->tx_queue_size,
-                             mpc->priv_flags, new_mtu, &newq);
+       err = mana_alloc_qset(mpc, scratch, mpc->num_queues, mpc->rx_queue_size,
+                             mpc->tx_queue_size, mpc->priv_flags, new_mtu,
+                             mpc->bpf_prog, &newq);
        if (err)
                goto free_scratch;
 
@@ -4039,6 +4039,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;
 }
 
 /* Vport identity and port debugfs outlive queue sets. */
@@ -4056,6 +4057,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;
 }
 
 /* Scratch starts without SQs/RQs and borrows the port's EQ pool. Never call
@@ -4096,7 +4098,8 @@ 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,
                    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;
@@ -4109,6 +4112,7 @@ int mana_alloc_qset(struct mana_port_context *apc,
        scratch->priv_flags     = priv_flags;
 
        scratch->configured_mtu = mtu;
+       scratch->bpf_prog       = bpf_prog;
 
        err = mana_init_port_context(scratch);
        if (err)
diff --git a/drivers/net/ethernet/microsoft/mana/mana_ethtool.c 
b/drivers/net/ethernet/microsoft/mana/mana_ethtool.c
index 2ad0fb4d5008..a438bc6097d6 100644
--- a/drivers/net/ethernet/microsoft/mana/mana_ethtool.c
+++ b/drivers/net/ethernet/microsoft/mana/mana_ethtool.c
@@ -739,7 +739,7 @@ static int mana_set_channels(struct net_device *ndev,
 
        err = mana_alloc_qset(apc, 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;
 
@@ -828,7 +828,8 @@ static int mana_set_ringparam(struct net_device *ndev,
        }
 
        err = mana_alloc_qset(apc, 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,9 +919,9 @@ static int mana_set_priv_flags(struct net_device *ndev, u32 
priv_flags)
                goto clear_flag;
        }
 
-       err = mana_alloc_qset(apc, scratch, apc->num_queues,
-                             apc->rx_queue_size, apc->tx_queue_size,
-                             priv_flags, apc->configured_mtu, &newq);
+       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)
                goto free_scratch;
 
diff --git a/include/net/mana/mana.h b/include/net/mana/mana.h
index 3faad257e97e..5f9461bc16e9 100644
--- a/include/net/mana/mana.h
+++ b/include/net/mana/mana.h
@@ -707,6 +707,8 @@ struct mana_qset {
        u32                     priv_flags;
 
        int                     mtu;
+       struct bpf_prog         *bpf_prog;
+
 };
 
 netdev_tx_t mana_start_xmit(struct sk_buff *skb, struct net_device *ndev);
@@ -729,7 +731,8 @@ static inline struct mana_stats_rx *mana_rxq_stats(struct 
mana_rxq *rxq)
 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 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);

Reply via email to