Skip to contentSkip to conversation

net: mvpp2: fix XDP RX buffer handling

PATCH Superseded Til Kaiser 2 participants 9 messages Jun 5 – Jun 6

Subsystems (4)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Til Kaiser

author
#0 lore raw

v4 cover net: mvpp2: fix XDP RX buffer handling

This is v4 of the earlier XDP_PASS fix. The XDP_PASS change is retained, and the series also fixes related RX/XDP buffer handling issues found during review.

Tested with tools/testing/selftests/drivers/net/xdp.py on mvpp2 hardware.

---
v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/

Changes in v4:
- Reposted as a new independent thread, no code changes.

Changes in v3:
- Reworked RX refill failure handling to preserve BM pool depth by refilling before the current buffer is consumed by XDP or an skb.

Changes in v2:
- Fixed RX DMA sync range to account for RX headroom.
- Limited XDP frame size to the actual RX buffer size.
- Fixed refill error paths so retired buffers are not returned to BM.

Til Kaiser (4):
net: mvpp2: sync RX data at the hardware packet offset
net: mvpp2: limit XDP frame size to the RX buffer
net: mvpp2: refill RX buffers before XDP or skb use
net: mvpp2: build skb from XDP-adjusted data on XDP_PASS

 .../net/ethernet/marvell/mvpp2/mvpp2_main.c   | 61 +++++++++++--------
 1 file changed, 36 insertions(+), 25 deletions(-)
Signature · 1 line
2.54.0
author
#1 lore raw

PATCH v4 1/4 net: mvpp2: sync RX data at the hardware packet offset

mvpp2 programs the RX queue packet offset, so hardware writes received data at dma_addr + MVPP2_SKB_HEADROOM. The current CPU sync starts at dma_addr and only covers rx_bytes + MVPP2_MH_SIZE bytes, which syncs the unused headroom and misses the same number of bytes at the packet tail.

On non-coherent DMA systems this can leave the CPU reading stale cache contents for the end of the received frame.

Use dma_sync_single_range_for_cpu() with MVPP2_SKB_HEADROOM as the range offset so the sync covers the Marvell header and packet data actually written by hardware.

Fixes e1921168bbd4 ("mvpp2: sync only the received frame")
---
 drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +4 -3
index f442b874bb59..92a701f4fe3f 100644
@@ -3946,9 +3946,10 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 			dma_dir = DMA_FROM_DEVICE;
 		}
 
-		dma_sync_single_for_cpu(dev->dev.parent, dma_addr,
-					rx_bytes + MVPP2_MH_SIZE,
-					dma_dir);
+		dma_sync_single_range_for_cpu(dev->dev.parent, dma_addr,
+					      MVPP2_SKB_HEADROOM,
+					      rx_bytes + MVPP2_MH_SIZE,
+					      dma_dir);
 
 		/* Buffer header not supported */
 		if (rx_status & MVPP2_RXD_BUF_HDR)
Signature · 1 line
2.54.0
#2 lore raw

Re: [PATCH net v4 1/4] net: mvpp2: sync RX data at the hardware packet offset

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] In the `mvpp2_rx` error path for `build_skb()` failure, the driver passes the potentially shrunk…

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] In the mvpp2_rx error path for build_skb() failure, the driver passes the potentially shrunk rx_bytes to page_pool_put_page(), failing to sync the entire CPU-touched region back to the device. --

commit f0a643ac2be4e337423088cdfdd2a25a0f4e5e19
Author: Til Kaiser <[email protected]>

net: mvpp2: sync RX data at the hardware packet offset

mvpp2 programs the RX queue packet offset, so hardware writes received data at dma_addr + MVPP2_SKB_HEADROOM. The current CPU sync starts at dma_addr and only covers rx_bytes + MVPP2_MH_SIZE bytes, which syncs the unused headroom and misses the same number of bytes at the packet tail.

Quoted 5 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0 -0
index f442b874bb593..92a701f4fe3f5 100644

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 3952

Quoted 10 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +4 -3
@@ -3946,9 +3946,10 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 			dma_dir = DMA_FROM_DEVICE;
 		}
 
-		dma_sync_single_for_cpu(dev->dev.parent, dma_addr,
-					rx_bytes + MVPP2_MH_SIZE,
-					dma_dir);
+		dma_sync_single_range_for_cpu(dev->dev.parent, dma_addr,
+					      MVPP2_SKB_HEADROOM,
+					      rx_bytes + MVPP2_MH_SIZE,
+					      dma_dir);

[Severity: High]
Does this regression also affect the build_skb() error path in mvpp2_rx()?

If an XDP program modifies the packet payload and shrinks the packet length (e.g., via bpf_xdp_adjust_tail()), rx_bytes becomes smaller than the originally received packet size.

If build_skb() subsequently fails, the error path calls:

    if (pp) {
        page_pool_put_page(pp, virt_to_head_page(data),
                           rx_bytes + MVPP2_MH_SIZE,
                           true);

Since page_pool_put_page() syncs this truncated rx_bytes length back to the device, could the CPU cache lines at the tail of the original packet remain unflushed?

When the page is recycled and non-coherent hardware DMA writes a new packet into the buffer, might the dirty CPU cache lines be evicted and silently overwrite the new packet's data?

Quoted 3 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0 -0
 		/* Buffer header not supported */
 		if (rx_status & MVPP2_RXD_BUF_HDR)
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
author
#3 lore raw

PATCH v4 2/4 net: mvpp2: limit XDP frame size to the RX buffer

In reply to Til Kaiser

mvpp2 has short and long BM pools, and short pool buffers can be smaller than PAGE_SIZE. The XDP path nevertheless initializes every xdp_buff with PAGE_SIZE as frame size.

XDP helpers use frame_sz to validate tail growth and to derive the hard end of the data area. Advertising PAGE_SIZE for short buffers can let bpf_xdp_adjust_tail() grow a packet past the real allocation, corrupting memory or later tripping skb tailroom checks.

Initialize the XDP buffer with bm_pool->frag_size so XDP tailroom matches the actual buffer backing the packet.

Fixes 07dd0a7aae7f ("mvpp2: add basic XDP support")
---
 drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +1 -1
index 92a701f4fe3f..3372ed27cc8d 100644
@@ -3979,7 +3979,7 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 			else
 				xdp_rxq = &rxq->xdp_rxq_long;
 
-			xdp_init_buff(&xdp, PAGE_SIZE, xdp_rxq);
+			xdp_init_buff(&xdp, bm_pool->frag_size, xdp_rxq);
 			xdp_prepare_buff(&xdp, data,
 					 MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM,
 					 rx_bytes, true);
Signature · 1 line
2.54.0
#4 lore raw

Re: [PATCH net v4 2/4] net: mvpp2: limit XDP frame size to the RX buffer

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] MTU change while XDP is loaded disables percpu_pools globally, causing NULL pointer dereference in XDP datapath. --

commit 17d23d36e2ff657dddc4655e8e281c3847e806d4
Author: Til Kaiser <[email protected]>

net: mvpp2: limit XDP frame size to the RX buffer

mvpp2 has short and long BM pools, and short pool buffers can be smaller than PAGE_SIZE. The XDP path nevertheless initializes every xdp_buff with PAGE_SIZE as frame size. This patch initializes the XDP buffer with bm_pool->frag_size so XDP tailroom matches the actual buffer backing the packet.

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 3982

Quoted 10 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +1 -1
index 92a701f4fe3f5..3372ed27cc8d6 100644
@@ -3979,7 +3979,7 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 			else
 				xdp_rxq = &rxq->xdp_rxq_long;
 
-			xdp_init_buff(&xdp, PAGE_SIZE, xdp_rxq);
+			xdp_init_buff(&xdp, bm_pool->frag_size, xdp_rxq);

[Severity: High]
This is a pre-existing issue, but while looking at XDP buffer initialization, is there a NULL pointer dereference in the XDP datapath if the MTU is changed?

If an MTU change occurs and the new MTU is larger than MVPP2_BM_LONG_PKT_SIZE, the driver disables percpu_pools globally:

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c:mvpp2_change_mtu() {
	...
	if (MVPP2_RX_PKT_SIZE(mtu) > MVPP2_BM_LONG_PKT_SIZE) {
		if (priv->percpu_pools) {
			netdev_warn(dev, "mtu %d too high, switching to shared buffers", mtu);
			mvpp2_bm_switch_buffers(priv, false);
		}
	}
	...
}

Because priv->percpu_pools is global across the mvpp2 controller, this affects all ports. However, mvpp2_bm_switch_buffers() does not detach the existing XDP programs.

When a packet is subsequently received on a port with XDP attached, mvpp2_rx() leaves the page pool pointer pp as NULL because port->priv->percpu_pools is now false:

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c:mvpp2_rx() {
	...
	if (port->priv->percpu_pools) {
		pp = port->priv->page_pool[pool];
		...
	} else {
		dma_dir = DMA_FROM_DEVICE;
		/* pp remains NULL */
	}
	...
	if (xdp_prog) {
		...
		ret = mvpp2_run_xdp(port, xdp_prog, &xdp, pp, &ps);
	...
}

If the XDP program drops the packet (XDP_DROP) or fails a redirect/tx action, mvpp2_run_xdp() unconditionally dereferences the NULL pp pointer:

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c:mvpp2_run_xdp() {
	...
	case XDP_DROP:
		page = virt_to_head_page(xdp->data);
		page_pool_put_page(pp, page, sync, true);
		ret = MVPP2_XDP_DROPPED;
		...
}

Can this lead to a kernel crash when page_pool_put_page() accesses the NULL page pool pointer?

Quoted 3 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0 -0
 			xdp_prepare_buff(&xdp, data,
 					 MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM,
 					 rx_bytes, true);
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
author
#5 lore raw

PATCH v4 3/4 net: mvpp2: refill RX buffers before XDP or skb use

In reply to Til Kaiser

The RX error path returns the current descriptor buffer to the hardware BM pool. That is only valid while the driver still owns the buffer.

mvpp2_rx_refill() can fail after the current buffer has been handed to XDP or attached to an skb. In those cases mvpp2_run_xdp() may have recycled, redirected, or queued the page for XDP_TX, and an skb free also retires the data buffer. Returning such a buffer to BM lets hardware DMA into memory that is no longer owned by the RX ring.

Refill the BM pool before handing the current buffer to XDP or to the skb. If the allocation fails there, drop the packet and return the still-owned current buffer to BM, preserving the pool depth. Once the refill succeeds, later local drops retire/free the current buffer instead of returning it to BM.

Fixes 07dd0a7aae7f ("mvpp2: add basic XDP support")
Fixes d6526926de73 ("net: mvpp2: fix memory leak in mvpp2_rx")
---
 .../net/ethernet/marvell/mvpp2/mvpp2_main.c   | 43 +++++++++++--------
 1 file changed, 24 insertions(+), 19 deletions(-)
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +24 -19
index 3372ed27cc8d..481daafdc1cb 100644
@@ -3971,6 +3971,12 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 		else
 			frag_size = bm_pool->frag_size;
 
+		err = mvpp2_rx_refill(port, bm_pool, pp, pool);
+		if (err) {
+			netdev_err(port->dev, "failed to refill BM pools\n");
+			goto err_drop_frame;
+		}
+
 		if (xdp_prog) {
 			struct xdp_rxq_info *xdp_rxq;
 
@@ -3988,12 +3994,6 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 
 			if (ret) {
 				xdp_ret |= ret;
-				err = mvpp2_rx_refill(port, bm_pool, pp, pool);
-				if (err) {
-					netdev_err(port->dev, "failed to refill BM pools\n");
-					goto err_drop_frame;
-				}
-
 				ps.rx_packets++;
 				ps.rx_bytes += rx_bytes;
 				continue;
@@ -4008,8 +4008,21 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 			skb = slab_build_skb(data);
 		if (!skb) {
 			netdev_warn(port->dev, "skb build failed\n");
-			goto err_drop_frame;
+			if (pp) {
+				page_pool_put_page(pp, virt_to_head_page(data),
+						   rx_bytes + MVPP2_MH_SIZE,
+						   true);
+			} else {
+				dma_unmap_single_attrs(dev->dev.parent, dma_addr,
+						       bm_pool->buf_size,
+						       DMA_FROM_DEVICE,
+						       DMA_ATTR_SKIP_CPU_SYNC);
+				mvpp2_frag_free(bm_pool, pp, data);
+			}
+			goto err_drop_frame_retired;
 		}
+		if (pp)
+			skb_mark_for_recycle(skb);
 
 		/* If we have RX hardware timestamping enabled, grab the
 		 * timestamp from the queue and convert.
@@ -4020,16 +4033,7 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 					 skb_hwtstamps(skb));
 		}
 
-		err = mvpp2_rx_refill(port, bm_pool, pp, pool);
-		if (err) {
-			netdev_err(port->dev, "failed to refill BM pools\n");
-			dev_kfree_skb_any(skb);
-			goto err_drop_frame;
-		}
-
-		if (pp)
-			skb_mark_for_recycle(skb);
-		else
+		if (!pp)
 			dma_unmap_single_attrs(dev->dev.parent, dma_addr,
 					       bm_pool->buf_size, DMA_FROM_DEVICE,
 					       DMA_ATTR_SKIP_CPU_SYNC);
@@ -4048,13 +4052,14 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 		continue;
 
 err_drop_frame:
-		dev->stats.rx_errors++;
-		mvpp2_rx_error(port, rx_desc);
 		/* Return the buffer to the pool */
 		if (rx_status & MVPP2_RXD_BUF_HDR)
 			mvpp2_buff_hdr_pool_put(port, rx_desc, pool, rx_status);
 		else
 			mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);
+err_drop_frame_retired:
+		dev->stats.rx_errors++;
+		mvpp2_rx_error(port, rx_desc);
 	}
 
 	if (xdp_ret & MVPP2_XDP_REDIR)
Signature · 1 line
2.54.0
#6 lore raw

Re: [PATCH net v4 3/4] net: mvpp2: refill RX buffers before XDP or skb use

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:

Pre-existing issues:
- [Critical] Pre-existing bug in mvpp2_buff_hdr_pool_put where phys_addr and dma_addr are swapped, leading to a potential crash during phys_to_virt().
- [High] Unratelimited netdev_err and netdev_warn calls in the NAPI poll loop can cause syslog flooding and CPU livelock.
- [High] Missing dma_sync_single_for_device when dropping packets and returning them to the hardware pool.
--

commit 475b5364852b812377b363f98f67c255857f76d2
Author: Til Kaiser <[email protected]>

net: mvpp2: refill RX buffers before XDP or skb use

This commit reorders the RX processing in the mvpp2 driver to refill the hardware buffer manager (BM) pool before handing the current descriptor buffer over to XDP or allocating an skb. If the refill fails, the packet is dropped and the original buffer is returned to the pool.

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 3976

Quoted 11 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +3 -0
index 3372ed27cc8d6..481daafdc1cbc 100644
@@ -3971,6 +3971,12 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 		else
 			frag_size = bm_pool->frag_size;
 
+		err = mvpp2_rx_refill(port, bm_pool, pp, pool);
+		if (err) {
+			netdev_err(port->dev, "failed to refill BM pools\n");

[Severity: High]
This isn't a bug introduced by this patch, but since this log is inside the high-frequency NAPI poll loop, could persistent memory pressure cause syslog flooding and CPU livelock? Should this use netdev_err_ratelimited() instead?

Quoted 4 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +3 -0
+			goto err_drop_frame;
+		}
+
 		if (xdp_prog) {

[ ... ]

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 4010

Quoted 4 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0 -0
@@ -4008,8 +4008,21 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 			skb = slab_build_skb(data);
 		if (!skb) {
 			netdev_warn(port->dev, "skb build failed\n");

[Severity: High]
This is a pre-existing issue, but similar to the refill error above, could this cause syslog flooding under memory pressure? Would it be better to use netdev_warn_ratelimited() here?

Quoted 2 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +1 -1
-			goto err_drop_frame;
+			if (pp) {

[ ... ]

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 4057

Quoted 9 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0 -2
@@ -4048,13 +4052,14 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 		continue;
 
 err_drop_frame:
-		dev->stats.rx_errors++;
-		mvpp2_rx_error(port, rx_desc);
 		/* Return the buffer to the pool */
 		if (rx_status & MVPP2_RXD_BUF_HDR)
 			mvpp2_buff_hdr_pool_put(port, rx_desc, pool, rx_status);

[Severity: Critical]
This is a pre-existing issue, but this patch makes the err_drop_frame path more likely to be hit on refill failure. Looking at mvpp2_buff_hdr_pool_put(), the physical and DMA addresses appear to be extracted backwards:

mvpp2_buff_hdr_pool_put() {
    phys_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
    dma_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
    ...
    buff_hdr = (struct mvpp2_buff_hdr *)phys_to_virt(phys_addr);
}

Does this cause an invalid memory dereference since phys_addr actually contains an IOVA instead of the physical address?

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 4059

Quoted 2 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0 -0
 		else
 			mvpp2_bm_pool_put(port, pool, dma_addr, phys_addr);

[Severity: High]
This is a pre-existing issue, but early in mvpp2_rx(), ownership of the packet buffer is explicitly transferred to the CPU via dma_sync_single_range_for_cpu().

If a subsequent error is detected and we jump to the err_drop_frame label, the buffer is returned directly to the hardware BM pool without calling dma_sync_single_range_for_device().

Does this violate the DMA API rules and potentially cause memory corruption for the next packet if dirty cache lines are written back?

Quoted 4 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +3 -0
+err_drop_frame_retired:
+		dev->stats.rx_errors++;
+		mvpp2_rx_error(port, rx_desc);
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
author
#7 lore raw

PATCH v4 4/4 net: mvpp2: build skb from XDP-adjusted data on XDP_PASS

In reply to Til Kaiser

When an XDP program uses bpf_xdp_adjust_head() or bpf_xdp_adjust_tail() and then returns XDP_PASS, mvpp2 still builds the skb from fixed offsets derived from the original RX descriptor. Packet geometry changes made by the XDP program are therefore discarded before the skb reaches the stack.

Update rx_offset and rx_bytes from xdp.data and xdp.data_end for XDP_PASS. This makes skb_reserve() and skb_put() reflect the packet seen by XDP, and makes RX byte accounting for XDP_PASS follow the length of the skb passed to the network stack.

Non-PASS verdicts continue to account the descriptor length because no skb is passed up in those cases.

Fixes 07dd0a7aae7f ("mvpp2: add basic XDP support")
---
 drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +7 -2
index 481daafdc1cb..dd30674148aa 100644
@@ -3920,7 +3920,7 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 		unsigned int frag_size;
 		dma_addr_t dma_addr;
 		phys_addr_t phys_addr;
-		int pool, rx_bytes, err, ret;
+		int pool, rx_bytes, rx_offset, err, ret;
 		struct page *page;
 		void *data;
 
@@ -3933,6 +3933,7 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 		rx_status = mvpp2_rxdesc_status_get(port, rx_desc);
 		rx_bytes = mvpp2_rxdesc_size_get(port, rx_desc);
 		rx_bytes -= MVPP2_MH_SIZE;
+		rx_offset = MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM;
 		dma_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
 
 		pool = (rx_status & MVPP2_RXD_BM_POOL_ID_MASK) >>
@@ -3999,6 +4000,10 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 				continue;
 			}
 
+			/* Update offset and length to reflect any XDP adjustments. */
+			rx_offset = xdp.data     - data;
+			rx_bytes  = xdp.data_end - xdp.data;
+
 			metasize = xdp.data - xdp.data_meta;
 		}
 
@@ -4041,7 +4046,7 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 		ps.rx_packets++;
 		ps.rx_bytes += rx_bytes;
 
-		skb_reserve(skb, MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM);
+		skb_reserve(skb, rx_offset);
 		skb_put(skb, rx_bytes);
 		if (metasize)
 			skb_metadata_set(skb, metasize);
Signature · 1 line
2.54.0
#8 lore raw

Re: [PATCH net v4 4/4] net: mvpp2: build skb from XDP-adjusted data on XDP_PASS

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] DMA Cache Coherency / Data Corruption on SKB allocation failure
- [Medium] Inconsistent RX statistics accounting based on XDP verdict

Pre-existing issues:
- [High] Invalid Checksum Offload after XDP modifications --

commit 0263440b3040a39c7d150dacdd56fed200308953
Author: Til Kaiser <[email protected]>

net: mvpp2: build skb from XDP-adjusted data on XDP_PASS

This commit updates rx_offset and rx_bytes from XDP data pointers for XDP_PASS verdicts. This allows the built SKB to reflect packet geometry changes made by the XDP program before reaching the network stack.

Quoted 4 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0 -0
index 481daafdc1cbc..dd30674148aa4 100644

[ ... ]

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 4005

Quoted 7 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +3 -0
@@ -3999,6 +4000,10 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 				continue;
 			}
 
+			/* Update offset and length to reflect any XDP adjustments. */
+			rx_offset = xdp.data     - data;
+			rx_bytes  = xdp.data_end - xdp.data;

[Severity: High]
Could this cause DMA cache coherency issues if SKB allocation fails?

If an XDP program shrinks the packet (for example, by calling bpf_xdp_adjust_tail()), rx_bytes is reduced. If build_skb() then fails shortly after this, the buffer is recycled via page_pool_put_page() using the modified rx_bytes:

mvpp2_rx() {
    ...
    if (!skb) {
        netdev_warn(port->dev, "skb build failed\n");
        if (pp) {
            page_pool_put_page(pp, virt_to_head_page(data),
                               rx_bytes + MVPP2_MH_SIZE,
                               true);
    ...
}

Does this leave the trailing bytes of the original packet buffer unsynced for the device, potentially causing data corruption for the next packet received into this recycled page?

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 4047

Quoted 7 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +1 -0
+
 			metasize = xdp.data - xdp.data_meta;
 		}
 
@@ -4041,7 +4046,7 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,
 		ps.rx_packets++;
 		ps.rx_bytes += rx_bytes;

[Severity: Medium]
Will this result in inconsistent RX byte statistics?

If the XDP program returns a non-PASS verdict like XDP_DROP or XDP_TX, the loop continues early and adds the original hardware descriptor length to ps.rx_bytes. For an XDP_PASS verdict, it reaches this point and adds the potentially smaller XDP-adjusted rx_bytes length.

Could this discrepancy lead to inconsistent interface statistics?

drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,

Quoted 7 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +1 -1
-		skb_reserve(skb, MVPP2_MH_SIZE + MVPP2_SKB_HEADROOM);
+		skb_reserve(skb, rx_offset);
 		skb_put(skb, rx_bytes);
 		if (metasize)
 			skb_metadata_set(skb, metasize);
 		skb->ip_summed = mvpp2_rx_csum(port, rx_status);

[Severity: High]
This is a pre-existing issue, but will applying the hardware checksum status still be safe after XDP modifications?

If the XDP program alters the packet contents or geometry (for example, decapsulation via bpf_xdp_adjust_head()), the hardware checksum validation performed on the original wire packet is no longer valid.

Now that XDP geometry changes are allowed to reach the network stack via this patch, does the driver need to invalidate the hardware checksum status if XDP might have modified the packet? Without doing so, corrupted packets might bypass software checksum validation.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4