mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH net v4] octeontx2-af: Fix BPID leaks in NIX backpressure enable/disable
@ 2026-11-19  8:54 nshettyj
  2026-11-19  8:57 ` netdev-bot+sinfo
  0 siblings, 1 reply; 2+ messages in thread
From: nshettyj @ 2026-11-19  8:54 UTC (permalink / raw)
  To: netdev, linux-kernel
  Cc: Rakesh Kudurumalla, Nitin Shetty J, Sunil Goutham,
	Ratheesh Kannoth, Geetha sowjanya, Subbaraya Sundeep,
	Andrew Lunn, David S. Miller, Eric Dumazet, Jakub Kicinski,
	Paolo Abeni, Simon Horman

From: Rakesh Kudurumalla <rkudurumalla@marvell.com>

For LBK interfaces, rvu_nix_get_bpid() allocates a BPID from the free
pool on every call. nix_bp_enable() called it once before the channel
loop and then again after programming each channel, including the last
one, so every NIX_BP_ENABLE request leaked one BPID whose result was
never used.

Fix the leak by allocating one BPID per programmed channel. Also fix
these related problems in nix_bp_enable() and nix_bp_disable():

- In nix_bp_enable(), allocate the BPIDs up front, before touching any
  channel register. If an allocation fails, release the LBK BPIDs
  obtained so far and return, leaving the channels unmodified.
- Report the BPID actually programmed for each channel in the
  response instead of deriving it from the first channel's BPID. LBK
  BPIDs come from a shared pool and need not be contiguous.
- In nix_bp_disable(), release only the BPID read back from the
  channel, and only if it lies in the LBK free pool and is owned by
  the requesting pcifunc. Previously the BPID was freed without a
  range check, and fn_map/ref_cnt were cleared for every BPID owned
  by the function, so FLR could no longer reclaim the rest.
- Iterate over chan_cnt with a counter instead of a u16 channel
  number, which wrapped and never terminated when chan_base +
  chan_cnt went past 0xFFFF.
- Reject requests whose channels fall outside the function's RX
  channels (NIX_BPIDS_PER_LMAC for CGX, rx_chan_cnt otherwise). LBK
  had no such check.
- Keep a request with chan_cnt == 0 a successful no-op that allocates
  nothing, as SDP VFs can legitimately have 0 rings.

The per-channel release logic is factored into nix_bp_free_lbk_bpid()
and nix_bp_chan_disable().

Fixes: d6212d2e41a0 ("octeontx2-af: Create BPIDs free pool")
Signed-off-by: Rakesh Kudurumalla <rkudurumalla@marvell.com>
Signed-off-by: Nitin Shetty J <nshettyj@marvell.com>
---
Changes in v4:
- Address Sashiko AI review comments on v3:
  https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260928065718.3378580-1-nshettyj%40marvell.com
- nix_bp_enable(): allocate all BPIDs up front and program the channels
  afterwards. On failure only the LBK BPIDs obtained so far are
  released and the channels stay untouched. This replaces the v3
  nix_bp_enable_unwind(), which disabled already-programmed channels
- nix_bp_enable(): a request with chan_cnt == 0 is a successful no-op
  (rsp->chan_cnt = 0) again instead of returning
  NIX_AF_ERR_INVALID_BPID_REQ, since SDP VFs can have 0 rings
- nix_bp_enable() and nix_bp_disable(): iterate on a counter bounded by
  chan_cnt instead of a u16 channel number, which wrapped and never
  terminated when chan_base + chan_cnt went past 0xFFFF
- Reject requests whose channels go past the function's RX channels
  (NIX_BPIDS_PER_LMAC for CGX, rx_chan_cnt otherwise). LBK had no such
  check
- nix_bp_disable(): release only the BPID read from the channel, and
  only if it is in the LBK free pool and owned by the requesting
  pcifunc. v3 still cleared fn_map/ref_cnt for every BPID owned by
  the function, so FLR could not reclaim the rest
- Factor the per-channel release logic into nix_bp_free_lbk_bpid() and
  nix_bp_chan_disable()
- Use GENMASK_ULL() for the 64-bit channel config
- Reorder local variable declarations and update comments
- Expand the commit message to cover the above

Changes in v3:
- Address Sashiko AI review comments on v2:
  https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260910031604.1045397-1-nshettyj%40marvell.com
- Free allocated BPIDs on allocation failure in nix_bp_enable()
- Report the BPID written per channel instead of reconstructing it
- Validate the BPID range in nix_bp_disable() before freeing it

Changes in v2:
- Address Sashiko AI review comments on v1:
  https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20260901102500.1534911-1-nshettyj%40marvell.com
- Move the rvu_nix_get_bpid() call for LBK from before the loop into
  the loop body
- Validate req->chan_cnt before allocating BPIDs
- Update commit message and Fixes tag
---
 .../ethernet/marvell/octeontx2/af/rvu_nix.c   | 128 +++++++++++++-----
 1 file changed, 91 insertions(+), 37 deletions(-)

diff --git a/drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c b/drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c
index 199723d5ec94..274662086d2a 100644
--- a/drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c
+++ b/drivers/net/ethernet/marvell/octeontx2/af/rvu_nix.c
@@ -599,18 +599,56 @@ static u16 nix_get_channel(u16 chan, bool cpt_link)
 	return cpt_link ? chan | BIT(11) : chan;
 }
 
+static void nix_bp_free_lbk_bpid(struct rvu *rvu, struct nix_bp *bp,
+				 u16 bpid, u16 pcifunc)
+{
+	int idx;
+
+	/* Skip channels never enabled (reset value 0) or not using an LBK bpid */
+	if (bpid < bp->free_pool_base ||
+	    bpid >= bp->free_pool_base + bp->bpids.max)
+		return;
+
+	idx = bpid - bp->free_pool_base;
+
+	/* Only release the bpid if it is owned by this function. */
+	mutex_lock(&rvu->rsrc_lock);
+	if (bp->fn_map[idx] == pcifunc) {
+		rvu_free_rsrc(&bp->bpids, idx);
+		bp->fn_map[idx] = 0;
+		bp->ref_cnt[idx] = 0;
+	}
+	mutex_unlock(&rvu->rsrc_lock);
+}
+
+static void nix_bp_chan_disable(struct rvu *rvu, struct nix_bp *bp,
+				int blkaddr, u16 chan, bool cpt_link,
+				int type, u16 pcifunc)
+{
+	u16 chan_v = nix_get_channel(chan, cpt_link);
+	u64 cfg;
+
+	cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan_v));
+	rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan_v),
+		    cfg & ~BIT_ULL(16));
+
+	if (type != NIX_INTF_TYPE_LBK)
+		return;
+
+	nix_bp_free_lbk_bpid(rvu, bp, cfg & GENMASK_ULL(8, 0), pcifunc);
+}
+
 static int nix_bp_disable(struct rvu *rvu,
 			  struct nix_bp_cfg_req *req,
 			  struct msg_rsp *rsp, bool cpt_link)
 {
 	u16 pcifunc = req->hdr.pcifunc;
 	int blkaddr, pf, type, err;
-	u16 chan_base, chan, bpid;
 	struct rvu_pfvf *pfvf;
 	struct nix_hw *nix_hw;
 	struct nix_bp *bp;
-	u16 chan_v;
-	u64 cfg;
+	u16 chan_base;
+	int i;
 
 	pf = rvu_get_pf(rvu->pdev, pcifunc);
 	type = is_lbk_vf(rvu, pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
@@ -629,26 +667,19 @@ static int nix_bp_disable(struct rvu *rvu,
 		return err;
 
 	bp = &nix_hw->bp;
-	chan_base = pfvf->rx_chan_base + req->chan_base;
-	for (chan = chan_base; chan < (chan_base + req->chan_cnt); chan++) {
-		chan_v = nix_get_channel(chan, cpt_link);
-		cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan_v));
-		rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan_v),
-			    cfg & ~BIT_ULL(16));
 
-		if (type == NIX_INTF_TYPE_LBK) {
-			bpid = cfg & GENMASK(8, 0);
-			mutex_lock(&rvu->rsrc_lock);
-			rvu_free_rsrc(&bp->bpids, bpid - bp->free_pool_base);
-			for (bpid = 0; bpid < bp->bpids.max; bpid++) {
-				if (bp->fn_map[bpid] == pcifunc) {
-					bp->fn_map[bpid] = 0;
-					bp->ref_cnt[bpid] = 0;
-				}
-			}
-			mutex_unlock(&rvu->rsrc_lock);
-		}
+	/* CGX may use up to NIX_BPIDS_PER_LMAC channels (PFC) */
+	if (type == NIX_INTF_TYPE_CGX) {
+		if ((u32)req->chan_base + req->chan_cnt > NIX_BPIDS_PER_LMAC)
+			return NIX_AF_ERR_INVALID_BPID_REQ;
+	} else if ((u32)req->chan_base + req->chan_cnt > pfvf->rx_chan_cnt) {
+		return NIX_AF_ERR_INVALID_BPID_REQ;
 	}
+
+	chan_base = pfvf->rx_chan_base + req->chan_base;
+	for (i = 0; i < req->chan_cnt; i++)
+		nix_bp_chan_disable(rvu, bp, blkaddr, chan_base + i, cpt_link,
+				    type, pcifunc);
 	return 0;
 }
 
@@ -750,13 +781,17 @@ static int nix_bp_enable(struct rvu *rvu,
 			 struct nix_bp_cfg_rsp *rsp,
 			 bool cpt_link)
 {
-	int blkaddr, pf, type, chan_id = 0;
 	u16 pcifunc = req->hdr.pcifunc;
 	struct rvu_pfvf *pfvf;
+	struct nix_hw *nix_hw;
+	int blkaddr, pf, type;
 	u16 chan_base, chan;
-	s16 bpid, bpid_base;
+	struct nix_bp *bp;
 	u16 chan_v;
+	s16 bpid;
 	u64 cfg;
+	int err;
+	int i;
 
 	pf = rvu_get_pf(rvu->pdev, pcifunc);
 	type = is_lbk_vf(rvu, pcifunc) ? NIX_INTF_TYPE_LBK : NIX_INTF_TYPE_CGX;
@@ -771,35 +806,54 @@ static int nix_bp_enable(struct rvu *rvu,
 	if (cpt_link && !rvu->hw->cpt_links)
 		return 0;
 
+	if (!req->chan_cnt) {
+		rsp->chan_cnt = 0;
+		return 0;
+	}
+
 	pfvf = rvu_get_pfvf(rvu, pcifunc);
-	blkaddr = rvu_get_blkaddr(rvu, BLKTYPE_NIX, pcifunc);
+	err = nix_get_struct_ptrs(rvu, pcifunc, &nix_hw, &blkaddr);
+	if (err)
+		return err;
+
+	bp = &nix_hw->bp;
+
+	/* CGX may use up to NIX_BPIDS_PER_LMAC channels (PFC) */
+	if (type == NIX_INTF_TYPE_CGX) {
+		if ((u32)req->chan_base + req->chan_cnt > NIX_BPIDS_PER_LMAC)
+			return NIX_AF_ERR_INVALID_BPID_REQ;
+	} else if ((u32)req->chan_base + req->chan_cnt > pfvf->rx_chan_cnt) {
+		return NIX_AF_ERR_INVALID_BPID_REQ;
+	}
 
-	bpid_base = rvu_nix_get_bpid(rvu, req, type, chan_id);
 	chan_base = pfvf->rx_chan_base + req->chan_base;
-	bpid = bpid_base;
 
-	for (chan = chan_base; chan < (chan_base + req->chan_cnt); chan++) {
+	/* Get all bpids first so a failure leaves the channels untouched */
+	for (i = 0; i < req->chan_cnt; i++) {
+		bpid = rvu_nix_get_bpid(rvu, req, type, i);
 		if (bpid < 0) {
 			dev_warn(rvu->dev, "Fail to enable backpressure\n");
+			if (type == NIX_INTF_TYPE_LBK)
+				while (i--)
+					nix_bp_free_lbk_bpid(rvu, bp,
+							     rsp->chan_bpid[i] & 0x3FF,
+							     pcifunc);
 			return -EINVAL;
 		}
 
+		rsp->chan_bpid[i] = ((req->chan_base + i) & 0x7F) << 10 |
+				    (bpid & 0x3FF);
+	}
+
+	for (i = 0; i < req->chan_cnt; i++) {
+		chan = chan_base + i;
+		bpid = rsp->chan_bpid[i] & 0x3FF;
 		chan_v = nix_get_channel(chan, cpt_link);
 
 		cfg = rvu_read64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan_v));
 		cfg &= ~GENMASK_ULL(8, 0);
 		rvu_write64(rvu, blkaddr, NIX_AF_RX_CHANX_CFG(chan_v),
 			    cfg | (bpid & GENMASK_ULL(8, 0)) | BIT_ULL(16));
-		chan_id++;
-		bpid = rvu_nix_get_bpid(rvu, req, type, chan_id);
-	}
-
-	for (chan = 0; chan < req->chan_cnt; chan++) {
-		/* Map channel and bpid assign to it */
-		rsp->chan_bpid[chan] = ((req->chan_base + chan) & 0x7F) << 10 |
-					(bpid_base & 0x3FF);
-		if (req->bpid_per_chan)
-			bpid_base++;
 	}
 	rsp->chan_cnt = req->chan_cnt;
 
-- 
2.53.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-19  8:58 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-19  8:54 [PATCH net v4] octeontx2-af: Fix BPID leaks in NIX backpressure enable/disable nshettyj
2026-11-19  8:57 ` netdev-bot+sinfo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®