net: mvpp2: fix XDP RX buffer handling
Subsystems (4)
v2 cover net: mvpp2: fix XDP RX buffer handling
This is v2 of the earlier XDP_PASS fix. The XDP_PASS change is retained, and the series now also fixes related RX/XDP buffer handling issues found during review.
---
v1: https://lore.kernel.org/all/[email protected]/
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: do not return retired RX buffers to BM
net: mvpp2: build skb from XDP-adjusted data on XDP_PASS
.../net/ethernet/marvell/mvpp2/mvpp2_main.c | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-)
Signature · 1 line
2.54.0
PATCH v2 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.
--- 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
@@ -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
[email protected]
AI reviewRe: [PATCH net v2 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:
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
Pre-existing issues:
- [High] Hardware address confusion in mvpp2_buff_hdr_pool_put leading to wild pointer dereference. --
commit 5455df1ccac8168f900ec83f5414e617fb3ad72f
Author: Til Kaiser <[email protected]>
net: mvpp2: sync RX data at the hardware packet offset
This commit modifies the DMA sync offset for received packets in mvpp2_rx. It uses dma_sync_single_range_for_cpu with MVPP2_SKB_HEADROOM so the CPU sync correctly aligns with the data written by the hardware.
Quoted 8 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0
-0@@ -3946,9 +3946,10 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, dma_dir =DMA_FROM_DEVICE ; }
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 3955
Quoted 10 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +4
-3-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 )
[Severity: High]
This isn't a bug introduced by this patch, but when rx_status has MVPP2_RXD_BUF_HDR set, this code jumps to err_drop_frame and eventually calls mvpp2_buff_hdr_pool_put(). Does mvpp2_buff_hdr_pool_put() swap the physical and DMA addresses?
Looking at mvpp2_buff_hdr_pool_put():
phys_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
dma_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
do {
buff_hdr = (struct mvpp2_buff_hdr *)phys_to_virt(phys_addr);It appears that phys_addr is populated with a DMA address and dma_addr is populated with the physical cookie.
On systems with an IOMMU where DMA addresses do not map 1:1 to physical addresses, will calling phys_to_virt() on a DMA address cause a wild pointer dereference? Could this also corrupt the hardware Buffer Manager pool when passing these swapped addresses into mvpp2_bm_pool_put()?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v2 2/4 net: mvpp2: limit XDP frame size to the RX buffer
In reply to Til Kaisermvpp2 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.
--- 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
@@ -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
[email protected]
AI reviewRe: [PATCH net v2 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] Changing MTU to jumbo frames switches to shared buffers without detaching XDP, leading to NULL pointer dereference. --
commit 6697a25a0b55fb9d610bfbe672d3935e951e1d53
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 initializes the XDP buffer with bm_pool->frag_size so XDP tailroom matches the actual buffer backing the packet.
Quoted 4 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0
-0
[ ... ]
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 3985
Quoted 9 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +1
-1@@ -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);
[Severity: High]
This is a pre-existing issue, but is there a problem with XDP and jumbo frames here?
If mvpp2_change_mtu() is called with an MTU that requires jumbo frames but is smaller than MVPP2_MAX_RX_BUF_SIZE, it allows the MTU change and switches to shared pools by calling mvpp2_bm_switch_buffers().
This destroys the page_pool structures and sets priv->percpu_pools to false, but the XDP program remains attached.
Later, when packets arrive in mvpp2_rx():
if (port->priv->percpu_pools) {
pp = port->priv->page_pool[pool];
dma_dir = page_pool_get_dma_dir(pp);
} else {
dma_dir = DMA_FROM_DEVICE;
}Because priv->percpu_pools is now false, pp remains NULL.
When mvpp2_run_xdp() is called with this NULL pp, and the XDP program returns XDP_DROP, it unconditionally calls page_pool_put_page() with the NULL pp and a non-page-pool page:
case XDP_DROP: page = virt_to_head_page(xdp->data); page_pool_put_page(pp, page, sync, true);
Could this cause a kernel panic when XDP drops or redirects a packet after an MTU change?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/4 net: mvpp2: do not return retired RX buffers to BM
In reply to Til KaiserThe RX refill failure path jumps to err_drop_frame, which returns the descriptor buffer to the hardware BM pool. That is only valid while the driver still owns the buffer.
After a non-PASS XDP verdict, mvpp2_run_xdp() may already have recycled, redirected, or queued the page for XDP_TX. After build_skb(), freeing the skb on refill failure also retires the data buffer. Returning either of those buffers to BM lets hardware DMA into memory that is no longer owned by the RX ring.
Split the error handling so buffers are returned to BM only while still owned by the driver. Once XDP or an skb owns the buffer, only account the RX error. Mark page-pool skbs for recycle before they can be freed on the refill failure path, and unmap non-page-pool buffers before freeing them.
--- .../net/ethernet/marvell/mvpp2/mvpp2_main.c | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +13 -7
@@ -3991,7 +3991,7 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, 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; + goto err_drop_frame_retired; } ps.rx_packets++;
@@ -4010,6 +4010,8 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi,netdev_warn (port->dev,"skb build failed\n" ); goto err_drop_frame; } +if (pp) +skb_mark_for_recycle (skb);/* If we have RX hardware timestamping enabled, grab the * timestamp from the queue and convert.
@@ -4023,13 +4025,16 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, err =mvpp2_rx_refill (port, bm_pool, pp, pool);if (err) {netdev_err (port->dev,"failed to refill BM pools\n" ); +if (!pp) +dma_unmap_single_attrs (dev->dev.parent, dma_addr, + bm_pool->buf_size, +DMA_FROM_DEVICE , +DMA_ATTR_SKIP_CPU_SYNC );dev_kfree_skb_any (skb); - goto err_drop_frame; + goto err_drop_frame_retired; } -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 +4053,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
[email protected]
AI reviewRe: [PATCH net v2 3/4] net: mvpp2: do not return retired RX buffers to BM
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Fixing the UAF introduces a permanent leak of hardware Buffer Manager (BM) pool slots on RX refill…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Fixing the UAF introduces a permanent leak of hardware Buffer Manager (BM) pool slots on RX refill failure, leading to a permanent RX stall. --
commit 0804789e54c7ef2fb92f8bc16ab030a2285b6149
Author: Til Kaiser <[email protected]>
net: mvpp2: do not return retired RX buffers to BM
This patch splits the RX error handling path to avoid returning buffers to the hardware Buffer Manager (BM) pool after they have been passed to XDP or built into an skb.
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 3994
Quoted 10 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +1
-1@@ -3991,7 +3991,7 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, 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; + goto err_drop_frame_retired;
[Severity: High]
Does skipping the buffer return here cause a permanent hardware pool leak?
By jumping to err_drop_frame_retired, we no longer put a replacement buffer into the hardware Buffer Manager (BM) pool. If mvpp2_rx_refill() repeatedly fails under memory pressure, will this drain the BM pool slots to zero and permanently stall packet reception?
To avoid returning an already-consumed buffer while keeping the BM pool full, would it be safer to perform the buffer allocation and refill before passing the original buffer to XDP?
[ ... ]
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 4034
Quoted 12 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +6
-1@@ -4023,13 +4025,16 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, err =mvpp2_rx_refill (port, bm_pool, pp, pool);if (err) {netdev_err (port->dev,"failed to refill BM pools\n" ); +if (!pp) +dma_unmap_single_attrs (dev->dev.parent, dma_addr, + bm_pool->buf_size, +DMA_FROM_DEVICE , +DMA_ATTR_SKIP_CPU_SYNC );dev_kfree_skb_any (skb); - goto err_drop_frame; + goto err_drop_frame_retired;
[Severity: High]
Does this path also permanently lose a hardware buffer slot?
Since the successfully built skb is freed and we jump to err_drop_frame_retired, the hardware BM pool does not get replenished.
Could the refill be done before calling build_skb() so the original buffer can be safely returned to the hardware pool if allocation fails?
[ ... ]
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Jakub Kicinski
maintainerRe: [PATCH net v2 3/4] net: mvpp2: do not return retired RX buffers to BM
In reply to Til KaiserQuoted 10 lines · On Tue, 2 Jun 2026 18:46:34 +0200 Til Kaiser wrote:
The RX refill failure path jumps to
err_drop_frame, which returns the descriptor buffer to the hardware BM pool. That is only valid while the driver still owns the buffer.After a non-PASS XDP verdict,
mvpp2_run_xdp()may already have recycled, redirected, or queued the page forXDP_TX. Afterbuild_skb(), freeing the skb on refill failure also retires the data buffer. Returning either of those buffers to BM lets hardware DMA into memory that is no longer owned by the RX ring.
Quoted 4 lines
Split the error handling so buffers are returned to BM only while still owned by the driver. Once XDP or an skb owns the buffer, only account the RX error. Mark page-pool skbs for recycle before they can be freed on the refill failure path, and unmap non-page-pool buffers before freeing them.
As Gemini points out the driver seems to be following the "if I can't allocate a new buffer I'll drop the packet and give the current buffer back to the HW" flow. So after your change on allocation error we're permanently leaking a slot on in the buffer pool. Leaking as in it will never be filled, if it happens enough times the pool will be empty, and no Rx can happen.
Signature · 1 line
pw-bot: cr
PATCH v2 4/4 net: mvpp2: build skb from XDP-adjusted data on XDP_PASS
In reply to Til KaiserWhen 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.
--- 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
@@ -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; }
@@ -4042,7 +4047,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
[email protected]
AI reviewRe: [PATCH net v2 4/4] net: mvpp2: build skb from XDP-adjusted data on XDP_PASS
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] Hardware checksum status is incorrectly applied to XDP-modified packets, bypassing network stack…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] Hardware checksum status is incorrectly applied to XDP-modified packets, bypassing network stack validation. --
commit f1fdb866a6579f01cab52a8f590b1009e5b86b4d
Author: Til Kaiser <[email protected]>
net: mvpp2: build skb from XDP-adjusted data on XDP_PASS
This commit updates the skb creation logic in the mvpp2 driver to account for packet geometry changes made by XDP programs. It updates rx_offset and rx_bytes from xdp.data and xdp.data_end for XDP_PASS packets.
Quoted 4 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0
-0
[ ... ]
Quoted 10 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +1
-1@@ -4042,7 +4047,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);
skb->ip_summed=mvpp2_rx_csum(port, rx_status);
[Severity: High]
If an XDP program modifies the packet (for example, by popping an encapsulation header using bpf_xdp_adjust_head), is it safe to apply the hardware's original checksum status to the modified skb here in mvpp2_rx?
The hardware validation of the outer L4 checksum might incorrectly be applied to the inner packet, causing the network stack to inappropriately trust CHECKSUM_UNNECESSARY. Should the driver clear the checksum status (for example, setting it to CHECKSUM_NONE) if the XDP program modified the packet?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
From: Til Kaiser <[email protected]>
Date: Tue, 2 Jun 2026 18:46:31 +0200
Quoted 12 lines
This is v2 of the earlier
XDP_PASSfix. TheXDP_PASSchange is retained, and the series now also fixes related RX/XDP buffer handling issues found during review.---
v1: https://lore.kernel.org/all/[email protected]/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.
Quoted 8 lines
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: do not return retired RX buffers to BM
net: mvpp2: build skb from XDP-adjusted data onXDP_PASS.../net/ethernet/marvell/mvpp2/mvpp2_main.c | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-)
Series:
Thanks,
Olek
Hi Tim,
śr., 3 cze 2026 o 15:55 Alexander Lobakin
<[email protected]> napisał(a):
Quoted 22 lines
From: Til Kaiser <[email protected]>
Date: Tue, 2 Jun 2026 18:46:31 +0200Quoted 18 lines
This is v2 of the earlier
XDP_PASSfix. TheXDP_PASSchange is retained, and the series now also fixes related RX/XDP buffer handling issues found during review.---
v1: https://lore.kernel.org/all/[email protected]/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: do not return retired RX buffers to BM
net: mvpp2: build skb from XDP-adjusted data onXDP_PASS
Quoted 7 lines
Quoted 2 lines
.../net/ethernet/marvell/mvpp2/mvpp2_main.c | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-)Series:
Reviewed-by Alexander Lobakin <[email protected]>
Tha changes look good, thank you.
Best regards,
Marcin
v3 cover net: mvpp2: fix XDP RX buffer handling
In reply to Til KaiserThis is v3 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 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
PATCH v3 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.
--- 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
@@ -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
[email protected]
AI reviewRe: [PATCH net v3 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] If SKB allocation fails after XDP processing, the buffer is recycled with a potentially truncated…
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] If SKB allocation fails after XDP processing, the buffer is recycled with a potentially truncated DMA sync size, leading to possible data corruption. --
commit 2937dd51dcd41c34cf709a921bae57060051477d
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 8 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0
-0@@ -3946,9 +3946,10 @@ static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, dma_dir =DMA_FROM_DEVICE ; }
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 3955
Quoted 10 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +4
-3-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 )
[Severity: High]
Can recycling the buffer with a truncated DMA sync size corrupt memory?
Looking further down in mvpp2_rx(), if an XDP program shrinks the packet, rx_bytes is updated to the smaller size:
rx_bytes = xdp.data_end - xdp.data;
If the subsequent build_skb() or slab_build_skb() allocation fails, the error path recycles the buffer with the modified rx_bytes:
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);Because rx_bytes is truncated, does this leave dirty CPU cache lines at the tail of the original packet uncleaned?
If the hardware reuses this buffer to DMA a new packet, random CPU cache evictions of these dirty lines could silently corrupt the new packet data in RAM on non-coherent architectures.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v3 2/4 net: mvpp2: limit XDP frame size to the RX buffer
In reply to Til Kaisermvpp2 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.
--- 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
@@ -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
PATCH v3 3/4 net: mvpp2: refill RX buffers before XDP or skb use
In reply to Til KaiserThe 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.
--- .../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
@@ -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
[email protected]
AI reviewRe: [PATCH net v3 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:
New issues:
- [High] Incomplete DMA sync when recycling an XDP-modified page during SKB allocation failure.
Pre-existing issues:
- [High] Variable swap leading to invalid memory access in mvpp2_buff_hdr_pool_put().
- [High] Acquiring a spinlock with preemption disabled causes a sleep-in-atomic bug on PREEMPT_RT. --
commit 9aeeec1a49aec085a29bfd1f85e72ffb42da9a5c
Author: Til Kaiser <[email protected]>
net: mvpp2: refill RX buffers before XDP or skb use
This patch adjusts the RX error path to prevent returning a buffer to the hardware BM pool if it has already been handed to XDP or attached to an skb. It refills the BM pool earlier so that if allocation fails, the original still-owned buffer can safely be returned to the hardware pool.
Quoted 4 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0
-0
[ ... ]
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 4014
Quoted 9 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +4
-1@@ -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);
[Severity: High]
Does this truncate the DMA sync size if the packet length was modified by XDP?
Earlier in mvpp2_rx(), an XDP program might have been executed which could reduce the packet length using helpers like bpf_xdp_adjust_tail, updating rx_bytes to a smaller value.
If build_skb() subsequently fails here, we use this reduced rx_bytes for the page pool DMA sync. Because the sync does not cover the tail of the buffer that the CPU may have fetched into its cache, can this lead to memory corruption when the hardware DMA writes a new packet and the dirty CPU cache lines are evicted over it?
In mvpp2_run_xdp(), this appears to be handled properly by calculating max(sync, len).
Quoted 9 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +8
-0+ }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; }
[ ... ]
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: High]
This is a pre-existing issue, but does mvpp2_buff_hdr_pool_put() swap the physical and DMA addresses?
Looking at mvpp2_buff_hdr_pool_put():
static void mvpp2_buff_hdr_pool_put(...)
{
...
phys_addr = mvpp2_rxdesc_dma_addr_get(port, rx_desc);
dma_addr = mvpp2_rxdesc_cookie_get(port, rx_desc);
do {
buff_hdr = (struct mvpp2_buff_hdr *)phys_to_virt(phys_addr);It appears phys_addr is initialized with the DMA address, and dma_addr with the physical address. Because of this swap, we call phys_to_virt() on a DMA address. On systems where the DMA address and physical address differ (like when an IOMMU is active), could this cause an invalid memory access?
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
-0else mvpp2_bm_pool_put (port, pool, dma_addr, phys_addr);
[Severity: High]
This is also a pre-existing issue, but does mvpp2_bm_pool_put() acquire a spinlock while preemption is disabled?
Looking at mvpp2_bm_pool_put():
static inline void mvpp2_bm_pool_put(...)
{
unsigned int thread = mvpp2_cpu_to_thread(port->priv, get_cpu());
unsigned long flags = 0;
if (test_bit(thread, &port->priv->lock_map))
spin_lock_irqsave(&port->bm_lock[thread], flags);Since get_cpu() executes preempt_disable(), we conditionally attempt to acquire the bm_lock spinlock immediately after. On PREEMPT_RT kernels, where standard spinlocks are mapped to sleepable rt_mutexes, won't this cause a sleep-in-atomic bug when this code path is hit?
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
PATCH v3 4/4 net: mvpp2: build skb from XDP-adjusted data on XDP_PASS
In reply to Til KaiserWhen 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.
--- 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
@@ -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
[email protected]
AI reviewRe: [PATCH net v3 4/4] net: mvpp2: build skb from XDP-adjusted data on XDP_PASS
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] Overwriting rx_bytes with the XDP-modified packet length causes a partial DMA sync on the SKB allocation error path, potentially corrupting subsequent packets.
Pre-existing issues:
- [High] Hardware checksum offload is unconditionally applied even after XDP program modifications, potentially bypassing network stack checksum validation for corrupted packets. --
commit 92e9744c23c0c27c625a826b0e44e159927ea85a
Author: Til Kaiser <[email protected]>
net: mvpp2: build skb from XDP-adjusted data on XDP_PASS
When an XDP program modifies packet geometry and returns XDP_PASS, mvpp2 currently discards these adjustments and builds the skb using fixed offsets. This commit updates the offset and length variables to reflect the XDP-adjusted data, ensuring the skb correctly represents the modified packet.
Quoted 4 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +0
-0
[ ... ]
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 4006
Quoted 8 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +4
-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]
If rx_bytes is updated to reflect the XDP-modified packet length here, what happens if skb allocation fails later in mvpp2_rx()?
Looking at the error path:
mvpp2_rx() {
...
if (!skb) {
if (pp) {
page_pool_put_page(pp, virt_to_head_page(data),
rx_bytes + MVPP2_MH_SIZE,
true);
...
}If the XDP program truncated the packet, rx_bytes is smaller than the original size synced to the CPU. Does this cause an incomplete DMA sync back to the device, leaving dirty CPU cache lines in the remainder of the buffer? Could this corrupt subsequent packets when the hardware reuses the buffer?
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c static int mvpp2_rx(struct mvpp2_port *port, struct napi_struct *napi, · line 4052
Quoted 12 lines
drivers/net/ethernet/marvell/mvpp2/mvpp2_main.c +1
-1metasize = 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);
[Severity: High]
This isn't a bug introduced by this patch, but does unconditionally applying the hardware checksum status to the skb here bypass software validation for packets modified by XDP?
Looking at the code immediately following this block in mvpp2_rx():
skb->ip_summed = mvpp2_rx_csum(port, rx_status);
Since XDP programs can modify packet headers or payloads, the hardware-validated checksum might no longer be correct. If the hardware reports CHECKSUM_UNNECESSARY, could this allow corrupted or improperly modified packets to be incorrectly accepted by the network stack? Should the hardware checksum be invalidated (e.g., set to CHECKSUM_NONE) if an XDP program has been executed?
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4