xsk: Keep the shared info of zero-copy buffers in kernel memory
Subsystems (8)
v2 cover xsk: Keep the shared info of zero-copy buffers in kernel memory
A packet that AF_XDP receives in zero-copy mode over several buffers keeps its shared info, the fragment list and its count, in the UMEM, which userspace can write while the kernel uses it. A rewritten list or count makes the kernel read and write out of bounds.
Patch 4 moves the shared info into the buffer pool and lets the device write up to the end of each chunk. Patch 1 makes libeth count XSk XDP_TX buffers one by one, as it read the shared info at a later bulk flush. Patches 2 and 3 fix skb builders that relied on the old
tailroom: the generic one sized the skb head from the frame size, and i40e, igb, igc and ixgbe read past the data.
Sashiko found the shared info issue in its review of the AF_XDP device buffer layout series [1], and the issues fixed by patches 2 and 3 in its review of v1. Clément reported the issue of patch 2 earlier [2]. Patch 3 was reproduced on igb in QEMU; the others were not triggered at runtime.
I tested this on mlx5 hardware, and on igb, virtio-net and veth in QEMU. None of them receives multi-buffer packets in zero copy or uses the generic skb builder, so i40e, ice, idpf and gve are only build-tested. Intel folks, could you run your multi-buffer zero-copy tests on them?
Also for Intel, found while auditing the drivers and not touched here:
ice no longer sizes its zero-copy RX buffers from the XSK pool. Commit 3a4f419f7509 ("ice: drop page splitting and recycling") programmed a fixed 3072 bytes, and 93f53db9f9dc ("ice: switch to Page Pool") changed ice_setup_rx_ctx() to use ring->rx_buf_len, which only the page pool path sets. An XSK ring gets the length of its last page pool setup, 3456 bytes on 4K pages, or 0 if it never had one. With 2K chunks the device can write past the chunk into the next one. I found this by reading the code and did not test it.
Changes in v2:
- Patch 1: fix the kernel-doc of struct libeth_sqe (Sashiko)
- New patch 2: size zero-copy skbs from the data, and copy the exact length (Sashiko, Clément)
- New patch 3: copy the exact length in i40e, igb, igc and ixgbe (Sashiko)
- Patch 4: set the flag on every buffer when the pool is created, and again in xp_alloc() and xsk_buff_set_size(), as virtio_net clears the flags of some buffers (Sashiko)
- Patch 4: publish the shared info before the buffers point to the queue (Sashiko)
- Patch 4: update the comment in bpf_xdp_adjust_tail() (Sashiko)
- Drop the selftest patch, it goes to net-next once net is merged (Maciej)
- The libeth XDP_TX completion count that Sashiko found going the wrong way is older and unrelated to the shared info; Separate fix
v1: https://lore.kernel.org/netdev/[email protected]/
[1] https://lore.kernel.org/netdev/[email protected]/
[2] https://lore.kernel.org/bpf/[email protected]/
Björn Töpel (4):
libeth: xsk: Count each XDP_TX buffer on its own
xdp: Size zero-copy skbs from the data, not the frame size
intel: Copy the exact length of XSk packets to skbs
xsk: Keep the shared info of zero-copy buffers in kernel memory
drivers/net/ethernet/intel/i40e/i40e_xsk.c | 3 +- drivers/net/ethernet/intel/igb/igb_xsk.c | 3 +- drivers/net/ethernet/intel/igc/igc_main.c | 3 +- drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c | 3 +- include/net/libeth/tx.h | 8 ++-- include/net/libeth/xdp.h | 9 ++++- include/net/libeth/xsk.h | 6 ++- include/net/xdp.h | 32 +++++++++++++--- include/net/xdp_sock_drv.h | 26 +++---------- include/net/xsk_buff_pool.h | 1 + net/core/filter.c | 2 +- net/core/xdp.c | 40 ++++++++++++++------ net/xdp/xdp_umem.c | 3 +- net/xdp/xsk_buff_pool.c | 20 ++++++---- 14 files changed, 96 insertions(+), 63 deletions(-)
base-commit: e2fb9368a41662bc54624f9be87ea8989b109306
Signature · 1 line
2.56.1
PATCH v2 1/4 libeth: xsk: Count each XDP_TX buffer on its own
libeth takes the number of buffers of an XSk XDP_TX frame from its shared info, and reports that many completed buffers to the driver. It reads the shared info when it flushes the bulk, not when it queues the frame. The shared info of a zero-copy buffer lives in the UMEM, which userspace may write at any time, so a rewritten count makes libeth report a wrong number. Once the last patch of this series moves the shared info into the buffer pool, the next multi-buffer frame of the same poll can overwrite it before the flush.
A wrong number breaks the in-flight count of XDP_TX buffers that idpf keeps on its XSk queues, and idpf may then complete XDP_TX buffers as XSk xmit frames.
Make libeth count each buffer on its own SQE. Head and fragments each count one completed buffer, and the head counts the packet. Stats stay the same for frames sent in full.
Found by an AI agent while reviewing this code. Not triggered at runtime.
--- include/net/libeth/tx.h | 8 +++++--- include/net/libeth/xdp.h | 9 ++++++++- include/net/libeth/xsk.h | 6 ++++-- 3 files changed, 17 insertions(+), 6 deletions(-)
include/net/libeth/tx.h +5 -3
@@ -22,7 +22,8 @@* @LIBETH_SQE_XDP_XMIT: &xdp_frame, unmap and xdp_return_frame_bulk(), stats * @LIBETH_SQE_XDP_XMIT_FRAG: &xdp_frame frag, only unmap DMA * @LIBETH_SQE_XSK_TX: &libeth_xdp_buff on XSk queue, xsk_buff_free(), stats -* @LIBETH_SQE_XSK_TX_FRAG: &libeth_xdp_buff frag on XSk queue, xsk_buff_free() +* @LIBETH_SQE_XSK_TX_FRAG: &libeth_xdp_buff frag on XSk queue, xsk_buff_free(), +* stats */ enum libeth_sqe_type {LIBETH_SQE_EMPTY =0U ,
@@ -50,9 +51,10 @@ enum libeth_sqe_type {* @xsk: XSk Rx frame from XDP_TX action * @dma: DMA address to unmap * @len: length of the mapped region to unmap -* @nr_frags: number of frags in the frame this buffer belongs to +* @nr_frags: number of frags in the frame this buffer belongs to, not for XSk * @packets: number of physical packets sent for this frame -* @bytes: number of physical bytes sent for this frame +* @bytes: number of physical bytes sent for this frame, or for this buffer +* on XSk * @priv: driver-private scratchpad */ struct libeth_sqe {
include/net/libeth/xdp.h +8 -1
@@ -1794,12 +1794,19 @@ __libeth_xdp_complete_tx(struct libeth_sqe *sqe, struct libeth_cq_pp *cp,switch (type) {case LIBETH_SQE_XDP_TX :case LIBETH_SQE_XDP_XMIT : -case LIBETH_SQE_XSK_TX : cp->xdp_tx -= sqe->nr_frags; cp->xss->packets++; cp->xss->bytes += sqe->bytes;break ; +case LIBETH_SQE_XSK_TX : + cp->xss->packets++; + fallthrough; +case LIBETH_SQE_XSK_TX_FRAG : +/* One buffer per SQE, see libeth_xsk_tx_fill_buf() */ + cp->xdp_tx--; + cp->xss->bytes += sqe->bytes; +break ;default :break ; }
include/net/libeth/xsk.h +4 -2
@@ -129,6 +129,10 @@ libeth_xsk_tx_fill_buf(struct libeth_xdp_tx_frame frm, u32 i, sqe = &sq->sqes[i]; sqe->xsk = xdp; +/* Count each buffer on its own SQE. The shared info of the frame +* may change before the bulk is flushed. +*/ + sqe->bytes = desc.len;if (!(desc.flags &LIBETH_XDP_TX_FIRST )) { sqe->type =LIBETH_SQE_XSK_TX_FRAG ;
@@ -136,8 +140,6 @@ libeth_xsk_tx_fill_buf(struct libeth_xdp_tx_frame frm, u32 i, } sqe->type =LIBETH_SQE_XSK_TX ; -libeth_xdp_tx_fill_stats (sqe, &desc, -xdp_get_shared_info_from_buff (&xdp->base));return desc; }
Signature · 1 line
2.56.1
Re: [PATCH net v2 1/4] libeth: xsk: Count each XDP_TX buffer on its own
From: Björn Töpel <[email protected]>
Date: Mon, 30 Nov 2026 13:37:32 +0100
Quoted 17 lines
libeth takes the number of buffers of an XSk
XDP_TXframe from its shared info, and reports that many completed buffers to the driver. It reads the shared info when it flushes the bulk, not when it queues the frame. The shared info of a zero-copy buffer lives in the UMEM, which userspace may write at any time, so a rewritten count makes libeth report a wrong number. Once the last patch of this series moves the shared info into the buffer pool, the next multi-buffer frame of the same poll can overwrite it before the flush.A wrong number breaks the in-flight count of
XDP_TXbuffers that idpf keeps on its XSk queues, and idpf may then completeXDP_TXbuffers as XSk xmit frames.Make libeth count each buffer on its own SQE. Head and fragments each count one completed buffer, and the head counts the packet. Stats stay the same for frames sent in full.
Quoted 5 lines
Found by an AI agent while reviewing this code. Not triggered at runtime.
Fixes b3ad8450b4dc ("libeth: xsk: add XSk XDP_TX sending helpers")Signed-off-by Björn Töpel <[email protected]>
Thanks!
Olek
PATCH v2 2/4 xdp: Size zero-copy skbs from the data, not the frame size
In reply to Björn Töpelxdp_build_skb_from_zc() gives the skb head the frame size of the XSk buffer, and the skb shared info takes the end of it. A device may fill a single-buffer XSk frame to its end, so a long packet overwrites the shared info of the new skb with packet data. For example, with 4K chunks, MTU 3800, an xdp.frags program that returns XDP_PASS and a socket bound without XDP_USE_SG, a 3814 byte frame needs 256 + 3814 + 320 = 4390 bytes and gets 4096. The last patch of this series lets multi-buffer pools and bpf_xdp_adjust_tail() reach the end of the frame as well.
The copies of the head and of the fragments also round the length up to 16 bytes, and read and write past the end of the data.
Size the head from the data, and copy the exact length. A head that does not fit into a page of the system page pool, such as one that fills a whole 4K chunk, gets a kmalloc'd head from __netdev_alloc_skb() instead.
Reported by Clément Lecigne, and found again by an AI code review agent. Reproduced only with out-of-tree code: a test module, and the AF_XDP page-pool RFC on fbnic in QEMU.
--- net/core/xdp.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-)
net/core/xdp.c +28 -12
@@ -709,7 +709,7 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, }memcpy (page_address (page) + offset,skb_frag_address (frag), -LARGEST_ALIGN (len)); + len);__skb_fill_page_desc_noacc (sinfo, i, page, offset, len); tsize += truesize;
@@ -723,6 +723,27 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,return true; } +static struct sk_buff *xdp_zc_alloc_head (struct page_pool *pp, +struct net_device *dev,u32 size) +{ +u32 truesize =SKB_HEAD_ALIGN (size); +struct sk_buff *skb; +void *data; + +if (truesize >PAGE_SIZE ) +return __netdev_alloc_skb (dev, size,GFP_ATOMIC | __GFP_NOWARN); + + data =page_pool_dev_alloc_va (pp, &truesize); +if (unlikely (!data)) +return NULL ; + + skb =napi_build_skb (data, truesize); +if (unlikely (!skb)) +page_pool_free_va (pp, data, true); + +return skb; +} +/** * xdp_build_skb_from_zc - create an skb from XSk &xdp_buff * @xdp: source XSk buff
@@ -730,7 +751,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,* Similar to xdp_build_skb_from_buff(), but for XSk frames. Allocate an skb * head, new buffer for the head, copy the data and initialize the skb fields. * If there are frags, allocate new buffers for them and copy. -* Buffers are allocated from the system percpu pools to try recycling them. +* Buffers are allocated from the system percpu pools to try recycling them, +* except a head that does not fit into a page. * If new skb was built successfully, @xdp is returned to XSk pool's freelist. * On error, it remains untouched and the caller must take care of this. *
@@ -738,33 +760,27 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,*/ struct sk_buff *xdp_build_skb_from_zc (struct xdp_buff *xdp) { +u32 size = xdp->data_end - xdp->data_hard_start;const struct xdp_rxq_info *rxq = xdp->rxq;u32 len = xdp->data_end - xdp->data_meta; -u32 truesize = xdp->frame_sz;struct sk_buff *skb =NULL ;struct page_pool *pp;int metalen; -void *data;if (!IS_ENABLED (CONFIG_PAGE_POOL ))return NULL ;local_lock_nested_bh (&system_page_pool.bh_lock); pp =this_cpu_read (system_page_pool.pool); - data =page_pool_dev_alloc_va (pp, &truesize); -if (unlikely (!data)) - goto out; - skb =napi_build_skb (data, truesize); -if (unlikely (!skb)) { -page_pool_free_va (pp, data, true); + skb =xdp_zc_alloc_head (pp, rxq->dev, size); +if (unlikely (!skb)) goto out; - }skb_mark_for_recycle (skb);skb_reserve (skb, xdp->data_meta - xdp->data_hard_start); -memcpy (__skb_put (skb, len), xdp->data_meta,LARGEST_ALIGN (len)); +memcpy (__skb_put (skb, len), xdp->data_meta, len); metalen = xdp->data - xdp->data_meta;if (metalen >0 ) {
Signature · 1 line
2.56.1
Re: [PATCH net v2 2/4] xdp: Size zero-copy skbs from the data, not the frame size
From: Björn Töpel <[email protected]>
Date: Mon, 30 Nov 2026 13:37:33 +0100
Quoted 102 lines
xdp_build_skb_from_zc()gives the skb head the frame size of the XSk buffer, and the skb shared info takes the end of it. A device may fill a single-buffer XSk frame to its end, so a long packet overwrites the shared info of the new skb with packet data. For example, with 4K chunks, MTU 3800, an xdp.frags program that returnsXDP_PASSand a socket bound withoutXDP_USE_SG, a 3814 byte frame needs 256 + 3814 + 320 = 4390 bytes and gets 4096. The last patch of this series lets multi-buffer pools andbpf_xdp_adjust_tail()reach the end of the frame as well.The copies of the head and of the fragments also round the length up to 16 bytes, and read and write past the end of the data.
Size the head from the data, and copy the exact length. A head that does not fit into a page of the system page pool, such as one that fills a whole 4K chunk, gets a kmalloc'd head from
__netdev_alloc_skb()instead.Reported by Clément Lecigne, and found again by an AI code review agent. Reproduced only with out-of-tree code: a test module, and the
AF_XDPpage-pool RFC on fbnic in QEMU.Reported-by Clément Lecigne <[email protected]>Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")Signed-off-by Björn Töpel <[email protected]>--- net/core/xdp.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-)net/core/xdp.c +25
-7@@ -709,7 +709,7 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, }memcpy (page_address (page) + offset,skb_frag_address (frag), -LARGEST_ALIGN (len)); + len);__skb_fill_page_desc_noacc (sinfo, i, page, offset, len); tsize += truesize;@@ -723,6 +723,27 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,return true; } +static struct sk_buff *xdp_zc_alloc_head (struct page_pool *pp, +struct net_device *dev,u32 size) +{ +u32 truesize =SKB_HEAD_ALIGN (size); +struct sk_buff *skb; +void *data; + +if (truesize >PAGE_SIZE ) +return __netdev_alloc_skb (dev, size,GFP_ATOMIC | __GFP_NOWARN); + + data =page_pool_dev_alloc_va (pp, &truesize); +if (unlikely (!data)) +return NULL ; + + skb =napi_build_skb (data, truesize); +if (unlikely (!skb)) +page_pool_free_va (pp, data, true); + +return skb; +} +/** * xdp_build_skb_from_zc - create an skb from XSk &xdp_buff * @xdp: source XSk buff @@ -730,7 +751,8 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,* Similar to xdp_build_skb_from_buff(), but for XSk frames. Allocate an skb * head, new buffer for the head, copy the data and initialize the skb fields. * If there are frags, allocate new buffers for them and copy. -* Buffers are allocated from the system percpu pools to try recycling them. +* Buffers are allocated from the system percpu pools to try recycling them, +* except a head that does not fit into a page. * If new skb was built successfully, @xdp is returned to XSk pool's freelist. * On error, it remains untouched and the caller must take care of this. * @@ -738,33 +760,27 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,*/ struct sk_buff *xdp_build_skb_from_zc (struct xdp_buff *xdp) { +u32 size = xdp->data_end - xdp->data_hard_start;const struct xdp_rxq_info *rxq = xdp->rxq;u32 len = xdp->data_end - xdp->data_meta; -u32 truesize = xdp->frame_sz;struct sk_buff *skb =NULL ;struct page_pool *pp;int metalen; -void *data;if (!IS_ENABLED (CONFIG_PAGE_POOL ))return NULL ;local_lock_nested_bh (&system_page_pool.bh_lock); pp =this_cpu_read (system_page_pool.pool); - data =page_pool_dev_alloc_va (pp, &truesize); -if (unlikely (!data)) - goto out;
net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 780
Quoted 10 lines
net/core/xdp.c +2
-4- skb =napi_build_skb (data, truesize); -if (unlikely (!skb)) { -page_pool_free_va (pp, data, true); + skb =xdp_zc_alloc_head (pp, rxq->dev, size); +if (unlikely (!skb)) goto out; - }skb_mark_for_recycle (skb);
I'd move this call to the bottom of xdp_zc_alloc_head() -- kmalloc'd skbs shouldn't be marked for PP recycling.
net/core/xdp.c static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb, · line 786
Quoted 7 lines
net/core/xdp.c +1
-1skb_reserve (skb, xdp->data_meta - xdp->data_hard_start); -memcpy (__skb_put (skb, len), xdp->data_meta,LARGEST_ALIGN (len)); +memcpy (__skb_put (skb, len), xdp->data_meta, len); metalen = xdp->data - xdp->data_meta;if (metalen >0 ) {
Other than that:
The idea behind those LARGEST_ALIGN() was that I thought there would always be some empty room after the frame -- like "who would like to align chunks to less than 16"...
Thanks,
Olek
PATCH v2 3/4 intel: Copy the exact length of XSk packets to skbs
In reply to Björn Töpeli40e, igb, igc and ixgbe copy an XSk zero-copy packet that the XDP program passes into a new skb, and round the length of the copy up to a multiple of 8 bytes. The copy then reads up to 7 bytes past the end of the data. When the data ends at the end of the last chunk of the UMEM, the read hits the guard page after the UMEM mapping and faults.
On i40e the device can already fill a single-buffer chunk to its end. The last patch of this series lets multi-buffer pools and bpf_xdp_adjust_tail() reach the end of a chunk on all four.
Copy the exact length.
Found by an AI code review agent in review of this series. Reproduced on igb in QEMU. An AF_XDP socket in zero copy with 4K chunks and only the last chunk of the UMEM in the FILL ring, and an XDP program that adds 4 bytes of metadata, grows the tail to the end of the chunk and passes the packet:
BUG: unable to handle page fault for address: ffa0000001048000 RIP: 0010:memcpy+0xc/0x30 igb_clean_rx_irq_zc+0x8a7/0x1520 igb_poll+0x1039/0x4f50
With this patch the same test passes.
--- drivers/net/ethernet/intel/i40e/i40e_xsk.c | 3 +-- drivers/net/ethernet/intel/igb/igb_xsk.c | 3 +-- drivers/net/ethernet/intel/igc/igc_main.c | 3 +-- drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c | 3 +-- 4 files changed, 4 insertions(+), 8 deletions(-)
drivers/net/ethernet/intel/i40e/i40e_xsk.c +1 -2
@@ -308,8 +308,7 @@ static struct sk_buff *i40e_construct_skb_zc(struct i40e_ring *rx_ring,if (unlikely (!skb)) goto out; -memcpy (__skb_put (skb, totalsize), xdp->data_meta, -ALIGN (totalsize,sizeof (long ))); +memcpy (__skb_put (skb, totalsize), xdp->data_meta, totalsize);if (metasize) {skb_metadata_set (skb, metasize);
drivers/net/ethernet/intel/igb/igb_xsk.c +1 -2
@@ -280,8 +280,7 @@ static struct sk_buff *igb_construct_skb_zc(struct igb_ring *rx_ring,if (timestamp)skb_hwtstamps (skb)->hwtstamp = timestamp; -memcpy (__skb_put (skb, totalsize), xdp->data_meta, -ALIGN (totalsize,sizeof (long ))); +memcpy (__skb_put (skb, totalsize), xdp->data_meta, totalsize);if (metasize) {skb_metadata_set (skb, metasize);
drivers/net/ethernet/intel/igc/igc_main.c +1 -2
@@ -2749,8 +2749,7 @@ static struct sk_buff *igc_construct_skb_zc(struct igc_ring *ring,if (unlikely (!skb))return NULL ; -memcpy (__skb_put (skb, totalsize), xdp->data_meta, -ALIGN (totalsize,sizeof (long ))); +memcpy (__skb_put (skb, totalsize), xdp->data_meta, totalsize);if (metasize) {skb_metadata_set (skb, metasize);
drivers/net/ethernet/intel/ixgbe/ixgbe_xsk.c +1 -2
@@ -224,8 +224,7 @@ static struct sk_buff *ixgbe_construct_skb_zc(struct ixgbe_ring *rx_ring,if (unlikely (!skb))return NULL ; -memcpy (__skb_put (skb, totalsize), xdp->data_meta, -ALIGN (totalsize,sizeof (long ))); +memcpy (__skb_put (skb, totalsize), xdp->data_meta, totalsize);if (metasize) {skb_metadata_set (skb, metasize);
Signature · 1 line
2.56.1
Re: [PATCH net v2 3/4] intel: Copy the exact length of XSk packets to skbs
From: Björn Töpel <[email protected]>
Date: Mon, 30 Nov 2026 13:37:34 +0100
Quoted 23 lines
i40e, igb, igc and ixgbe copy an XSk zero-copy packet that the XDP program passes into a new skb, and round the length of the copy up to a multiple of 8 bytes. The copy then reads up to 7 bytes past the end of the data. When the data ends at the end of the last chunk of the UMEM, the read hits the guard page after the UMEM mapping and faults.
On i40e the device can already fill a single-buffer chunk to its end. The last patch of this series lets multi-buffer pools and
bpf_xdp_adjust_tail()reach the end of a chunk on all four.Copy the exact length.
Found by an AI code review agent in review of this series. Reproduced on igb in QEMU. An
AF_XDPsocket in zero copy with 4K chunks and only the last chunk of the UMEM in the FILL ring, and an XDP program that adds 4 bytes of metadata, grows the tail to the end of the chunk and passes the packet:BUG: unable to handle page fault for address: ffa0000001048000 RIP: 0010:memcpy+0xc/0x30 igb_clean_rx_irq_zc+0x8a7/0x1520 igb_poll+0x1039/0x4f50
Quoted 7 lines
With this patch the same test passes.
Fixes 6dba29537c0f ("i40e: respect metadata on XSK Rx to skb")Fixes f322a620be69 ("ixgbe: respect metadata on XSK Rx to skb")Fixes f9e61d365baf ("igc: don't reserve excessive XDP_PACKET_HEADROOM on XSK Rx to skb")Fixes 2c6196013f84 ("igb: Add AF_XDP zero-copy Rx support")Signed-off-by Björn Töpel <[email protected]>
Thanks,
Olek
PATCH v2 4/4 xsk: Keep the shared info of zero-copy buffers in kernel memory
In reply to Björn TöpelA packet that a driver receives in zero-copy mode over several buffers keeps its shared info, the fragment list and its count, in the tailroom of the UMEM chunk of its head. Userspace may write the UMEM at any time, also while the kernel walks the fragments, and a rewritten list or count makes the kernel read and write out of bounds.
Keep the shared info in the buffer pool, which outlives its buffers and the queues that point to it, and let the device write up to the end of the chunk. One shared info per pool is enough, as a pool builds one multi-buffer packet at a time. libeth read it later, at an XDP_TX bulk flush, which an earlier patch of this series fixes.
Every buffer of a pool carries the flag from the start, and xp_alloc() and xsk_buff_set_size() set it again, as a driver may clear the flags of the buffers it receives into.
Multi-buffer pools now give the device the whole chunk, as single-buffer pools already do. Every UMEM accepts a larger headroom, also in copy mode, and bpf_xdp_adjust_tail() can grow a zero-copy packet up to the end of its chunk.
Found by an AI code review agent, in its review of another series. Not triggered at runtime.
--- include/net/xdp.h | 32 ++++++++++++++++++++++++++------ include/net/xdp_sock_drv.h | 26 ++++++-------------------- include/net/xsk_buff_pool.h | 1 + net/core/filter.c | 2 +- net/xdp/xdp_umem.c | 3 +-- net/xdp/xsk_buff_pool.c | 20 ++++++++++++-------- 6 files changed, 47 insertions(+), 37 deletions(-)
include/net/xdp.h +26 -6
@@ -65,6 +65,10 @@ struct xdp_rxq_info {u32 reg_state;struct xdp_mem_info mem;u32 frag_size; +/* Shared info of buffers with %XDP_FLAGS_SINFO_EXT, in the XSk buffer +* pool, set by xsk_pool_set_rxq_info(). +*/ +struct skb_shared_info *ext_sinfo; }____cacheline_aligned ;/* perf critical, avoid false-sharing */ struct xdp_txq_info {
@@ -81,6 +85,13 @@ enum xdp_buff_flags {* XDP program is not attached. */ XDP_FLAGS_FRAGS_UNREADABLE =BIT (2 ), +/* The shared info lives in &xdp_rxq_info.ext_sinfo, not at the end of +* the frame, as userspace may write the frame. The frame reserves no +* tailroom, so the data may reach its end. The pool builds one such +* buffer at a time, so a consumer that uses the shared info later +* must copy what it needs. +*/ + XDP_FLAGS_SINFO_EXT = BIT(3), }; struct xdp_buff {
@@ -178,17 +189,26 @@ xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start,/* Reserve memory area at end-of data area. * -* This macro reserves tailroom in the XDP buffer by limiting the -* XDP/BPF data access to data_hard_end. Notice same area (and size) -* is used for XDP_PASS, when constructing the SKB via build_skb(). +* This reserves tailroom in the XDP buffer by limiting the XDP/BPF data +* access to data_hard_end. Notice same area (and size) is used for +* XDP_PASS, when constructing the SKB via build_skb(). Buffers with +* %XDP_FLAGS_SINFO_EXT keep the shared info elsewhere and reserve none. */ -#define xdp_data_hard_end (xdp) \ - ((xdp)->data_hard_start + (xdp)->frame_sz - \ - SKB_DATA_ALIGN(sizeof(struct skb_shared_info))) +static __always_inline void *xdp_data_hard_end(const struct xdp_buff *xdp) +{ +u32 tailroom =SKB_DATA_ALIGN (sizeof (struct skb_shared_info )); + +if (xdp->flags &XDP_FLAGS_SINFO_EXT ) + tailroom =0 ; +return xdp->data_hard_start + xdp->frame_sz - tailroom; +}static inline struct skb_shared_info *xdp_get_shared_info_from_buff (const struct xdp_buff *xdp) { +if (xdp->flags &XDP_FLAGS_SINFO_EXT ) +return READ_ONCE (xdp->rxq)->ext_sinfo; +return (struct skb_shared_info *)xdp_data_hard_end (xdp); }
include/net/xdp_sock_drv.h +6 -20
@@ -41,11 +41,6 @@ static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)return XDP_PACKET_HEADROOM + pool->headroom; } -static inline u32 xsk_pool_get_tailroom (bool mbuf) -{ -return mbuf ?SKB_DATA_ALIGN (sizeof (struct skb_shared_info )) :0 ; -} -static inline u32 xsk_pool_get_chunk_size (struct xsk_buff_pool *pool) {return pool->chunk_size;
@@ -58,18 +53,7 @@ static inline u32 __xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)static inline u32 xsk_pool_get_rx_frame_size (struct xsk_buff_pool *pool) { -u32 frame_size =__xsk_pool_get_rx_frame_size (pool); -struct xdp_umem *umem = pool->umem; -bool mbuf; - -/* Reserve tailroom only for zero-copy pools that opted into -* multi-buffer. The reserved area is used for skb_shared_info, -* matching the XDP core's xdp_data_hard_end() layout. -*/ - mbuf = pool->dev && (umem->flags &XDP_UMEM_SG_FLAG ); - frame_size -=xsk_pool_get_tailroom (mbuf); - -return ALIGN_DOWN (frame_size,128 ); +return ALIGN_DOWN (__xsk_pool_get_rx_frame_size (pool),128 ); }static inline u32 xsk_pool_get_rx_frag_step (struct xsk_buff_pool *pool)
@@ -152,7 +136,6 @@ static inline void xsk_buff_free(struct xdp_buff *xdp)xp_free (pos); } -xdp_get_shared_info_from_buff (xdp)->nr_frags =0 ;xdp_buff_clear_frags_flag (xdp); out:xp_free (xskb);
@@ -226,8 +209,11 @@ static inline void xsk_buff_set_size(struct xdp_buff *xdp, u32 size) xdp->data = xdp->data_hard_start +XDP_PACKET_HEADROOM ; xdp->data_meta = xdp->data; xdp->data_end = xdp->data + size; -/* Frags flag is cleared when the frags of a head are released */ - xdp->flags &=XDP_FLAGS_HAS_FRAGS ; +/* Frags flag is cleared when the frags of a head are released. The +* shared info stays in the pool, see xp_set_rxq_info(), also for a +* buffer whose flags a driver cleared. +*/ + xdp->flags = (xdp->flags &XDP_FLAGS_HAS_FRAGS ) |XDP_FLAGS_SINFO_EXT ; } static inline dma_addr_t xsk_buff_raw_get_dma(struct xsk_buff_pool *pool,
include/net/xsk_buff_pool.h +1 -0
@@ -59,6 +59,7 @@ struct xsk_buff_pool {spinlock_t rx_lock;struct list_head free_list;struct list_head xskb_list; +struct skb_shared_info sinfo;u32 heads_cnt;u16 queue_id;
net/core/filter.c +1 -1
@@ -4510,7 +4510,7 @@ BPF_CALL_2(bpf_xdp_adjust_tail, struct xdp_buff *, xdp, int, offset)return bpf_xdp_frags_increase_tail (xdp, offset); } -/* Notice that xdp_data_hard_end have reserved some tailroom */ +/* Notice that xdp_data_hard_end may have reserved some tailroom */ if (unlikely (data_end > data_hard_end))return -EINVAL ;
net/xdp/xdp_umem.c +1 -2
@@ -203,8 +203,7 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)if (!unaligned_chunks && chunks_rem)return -EINVAL ; -if (headroom > chunk_size -XDP_PACKET_HEADROOM - -SKB_DATA_ALIGN (sizeof (struct skb_shared_info )) -128 ) +if (headroom > chunk_size -XDP_PACKET_HEADROOM -128 )return -EINVAL ;if (mr->flags &XDP_UMEM_TX_METADATA_LEN ) {
net/xdp/xsk_buff_pool.c +12 -8
@@ -107,6 +107,7 @@ struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs, xskb = &pool->heads[i]; xskb->pool = pool; xskb->xdp.frame_sz = umem->chunk_size - umem->headroom; + xskb->xdp.flags =XDP_FLAGS_SINFO_EXT ;INIT_LIST_HEAD (&xskb->list_node);if (pool->unaligned) pool->free_heads[i] = xskb;
@@ -125,8 +126,15 @@ void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq) {u32 i; +/* Userspace may write the UMEM. Keep the shared info in the pool, +* and let the device write up to the end of each chunk. A NAPI may +* still run on the buffers, so publish the shared info first. +*/ + rxq->ext_sinfo = &pool->sinfo; +/* Pairs with READ_ONCE(xdp->rxq) in xdp_get_shared_info_from_buff() */ +smp_wmb ();for (i =0 ; i < pool->heads_cnt; i++) - pool->heads[i].xdp.rxq = rxq; +WRITE_ONCE (pool->heads[i].xdp.rxq, rxq); }EXPORT_SYMBOL (xp_set_rxq_info);
@@ -229,12 +237,7 @@ int xp_assign_dev(struct xsk_buff_pool *pool, segs =1 ; } -/* open-code xsk_pool_get_rx_frame_size() as pool->dev is not -* set yet at this point; we are before getting down to driver -*/ - frame_size =__xsk_pool_get_rx_frame_size (pool) - -xsk_pool_get_tailroom (mbuf); - frame_size =ALIGN_DOWN (frame_size,128 ); + frame_size =xsk_pool_get_rx_frame_size (pool);if (needed > frame_size * segs) { err = -EINVAL ;
@@ -629,7 +632,8 @@ struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool) xskb->xdp.data = xskb->xdp.data_hard_start +XDP_PACKET_HEADROOM ; xskb->xdp.data_meta = xskb->xdp.data; - xskb->xdp.flags &=XDP_FLAGS_HAS_FRAGS ; + xskb->xdp.flags = (xskb->xdp.flags &XDP_FLAGS_HAS_FRAGS ) | +XDP_FLAGS_SINFO_EXT ;if (pool->dev)xp_dma_sync_for_device (pool, xskb->dma, pool->frame_len);
Signature · 1 line
2.56.1