xsk: Keep the shared info of zero-copy buffers in kernel memory
Subsystems (8)
v4 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 5 moves the shared info into the buffer pool and lets the device write up to the end of each chunk. Patch 2 makes libeth count XSk XDP_TX buffers one by one, as it read the shared info at a later bulk flush. Patch 1 makes libeth count completed XDP_TX buffers up, as idpf subtracts them. Without it the count of patch 2 still goes the wrong way. Patches 3 and 4 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 issues [1], Clément the one of patch 3 earlier [2]. Patch 4 was reproduced on igb in QEMU.
v4 was tested on igb and veth in QEMU, v2 also on virtio-net and on mlx5 hardware. None covers multi-buffer zero copy, XDP_TX on idpf or the generic skb builder, so i40e, ice, idpf and gve are only build-tested, patch 1 included. Intel folks, could you run your multi-buffer zero-copy and XDP_TX tests on them?
Changes in v4:
- New patch 1: count completed XDP_TX buffers up in libeth. v2 left it for a separate fix, but patch 2 depends on it (Sashiko)
- Patch 2: count up after patch 1, and describe a frame cut short by a full XDPSQ (Sashiko)
- Patch 3: give the worst case, the whole shared info (Sashiko)
- Patch 5: state what a forged shared info allows, and the privilege it needs (Sashiko)
- Add Maciej's reviews, rebase on net
Changes in v3:
- Patch 2: describe the 2K chunk case and its impact (Sashiko)
- Patch 4: give the shared info of the pool its own cache lines after the data path members (Maciej)
- Patch 4: say that backports need patches 1 to 3 (Sashiko)
- Add Olek's acks, rebase on net
Changes in v2:
- New patches 2 and 3 (Sashiko, Clément)
- Patches 1 and 4: kernel-doc, buffer flag and ordering fixes (Sashiko)
- Drop the selftest patch, it goes to net-next (Maciej)
v3: https://lore.kernel.org/netdev/[email protected]/
v2: https://lore.kernel.org/netdev/[email protected]/
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 (5):
libeth: xdp: Count completed XDP_TX buffers up
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 | 10 +++-- include/net/libeth/xdp.h | 11 +++++- include/net/libeth/xsk.h | 6 ++- include/net/xdp.h | 32 +++++++++++++--- include/net/xdp_sock_drv.h | 26 +++---------- include/net/xsk_buff_pool.h | 2 + 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, 99 insertions(+), 65 deletions(-)
base-commit: 19727ac1afdaa3dac66e0f75daca7c3d156e1ca9
Signature · 1 line
2.53.1
PATCH v4 1/5 libeth: xdp: Count completed XDP_TX buffers up
libeth adds the buffers it sends on an XDPSQ to the driver's in-flight XDP_TX count. On completion, libeth counts them in struct libeth_cq_pp, whose kernel-doc calls it the number processed, and idpf subtracts that number. Currently libeth counts down from 0, so the subtraction adds, and the count grows by two per buffer.
idpf reads the count only on its XSk queues. At 0 it completes all done descriptors as XSk xmit frames at once. After the first XDP_TX buffer the count never gets back to 0, and every completion takes the slow path. After about 2^31 buffers the count wraps. If it lands on 0 while XDP_TX buffers are in flight, idpf completes them as XSk xmit frames. Their buffers are never freed, and userspace gets completions for xmit descriptors that are still in flight.
Count the completed buffers up, as the kernel-doc says, and say buffers there, not frames.
Found by an AI code review agent in review of this series. Not triggered at runtime.
--- include/net/libeth/tx.h | 2 +- include/net/libeth/xdp.h | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-)
include/net/libeth/tx.h +1 -1
@@ -93,7 +93,7 @@ struct libeth_sqe {* @bq: XDP frame bulk to combine return operations * @ss: onstack NAPI stats to fill * @xss: onstack XDPSQ NAPI stats to fill -* @xdp_tx: number of XDP-not-XSk frames processed +* @xdp_tx: number of non-XSk-xmit XDP buffers processed * @napi: whether it's called from the NAPI context * * libeth uses this structure to access objects needed for performing full
include/net/libeth/xdp.h +1 -1
@@ -1795,7 +1795,7 @@ __libeth_xdp_complete_tx(struct libeth_sqe *sqe, struct libeth_cq_pp *cp,case LIBETH_SQE_XDP_TX :case LIBETH_SQE_XDP_XMIT :case LIBETH_SQE_XSK_TX : - cp->xdp_tx -= sqe->nr_frags; + cp->xdp_tx += sqe->nr_frags; cp->xss->packets++; cp->xss->bytes += sqe->bytes;
Signature · 1 line
2.53.1
PATCH v4 2/5 libeth: xsk: Count each XDP_TX buffer on its own
In reply to Björn Töpellibeth counts the completed buffers of an XSk XDP_TX frame from its shared info, which it reads at the bulk flush, not when it queues the frame. A zero-copy buffer keeps its shared info in the UMEM, which userspace may write at any time. Once the last patch of this series moves it into the buffer pool, the next multi-buffer frame of the poll can overwrite it first.
It also goes wrong without userspace. When the XDPSQ fills up inside a multi-buffer frame, libeth sends the first buffers, drops the rest, and still reports the whole frame.
That breaks the in-flight XDP_TX count 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, and the packet on the head. A partly sent frame now counts the bytes of the buffers sent. 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.53.1
PATCH v4 3/5 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, with the skb shared info at its end. Only multi-buffer pools reserve tailroom, so a device may fill a single-buffer XSk frame to its end. Packet data then overwrites the whole shared info of the new skb, 320 bytes on x86_64, its flags, frag list and destructor_arg included. With 2K chunks, no UMEM headroom and the default MTU, a 1514 byte frame that XDP passes on ice, idpf or gve needs 256 + 1514 + 320 = 2090 bytes and gets 2048, and the sender controls the first 48 bytes. 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 the fragments also round the length up to 16 bytes, and read and write past the data, with 4K chunks past the end of the page.
Size the head from the data, and copy the exact length. Heads larger than a page come from kmalloc, not the page pool.
Found again by an AI code review agent, and the 2K case by reading the code. Reproduced out of tree, with 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.53.1
PATCH v4 4/5 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.53.1
PATCH v4 5/5 xsk: Keep the shared info of zero-copy buffers in kernel memory
In reply to Björn TöpelA multi-buffer packet received in zero copy 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. A forged page, offset and length of a fragment make xdp_copy_frags_from_zc() copy kernel memory at an address userspace picks into an skb, and bpf_xdp_frags_increase_tail() clear memory there. A forged count makes the next fragment land up to 4 KB past the chunk. This needs CAP_NET_RAW in the user namespace of the netns, a zero-copy device and a socket bound with XDP_USE_SG.
Keep the shared info in the buffer pool, which outlives its buffers and the queues that point to it. One shared info per pool is enough, as a pool builds one multi-buffer packet at a time. Buffers carry a flag for this, set again on every reuse, as drivers may clear it.
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.
A backport needs the earlier patches of this series.
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 | 2 ++ net/core/filter.c | 2 +- net/xdp/xdp_umem.c | 3 +-- net/xdp/xsk_buff_pool.c | 20 ++++++++++++-------- 6 files changed, 48 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 +2 -0
@@ -95,6 +95,8 @@ struct xsk_buff_pool {* destructor callback. */ spinlock_t cq_prod_lock; +/* Own cache lines, as zero-copy multi-buffer Rx writes it per packet */ +struct skb_shared_info sinfo____cacheline_aligned_in_smp ;struct xdp_buff_xsk *free_heads[]; };
net/core/filter.c +1 -1
@@ -4522,7 +4522,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
@@ -195,8 +195,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.53.1
[email protected]
AI reviewRe: [PATCH net v4 1/5] libeth: xdp: Count completed XDP_TX buffers up
In reply to Björn TöpelThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] libeth: xdp: Fixes trailer cites idpf user instead of introducing commit --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] libeth: xdp: Fixes trailer cites idpf user instead of introducing commit
--
commit 030e6e7c99b2e23af9fccfaae5698263d3fe1e21
Author: Björn Töpel <[email protected]>
Quoted 16 lines
libeth: xdp: Count completed
XDP_TXbuffers uplibeth adds the buffers it sends on an XDPSQ to the driver's in-flight
XDP_TXcount. On completion, libeth counts them in structlibeth_cq_pp, whose kernel-doc calls it the number processed, and idpf subtracts that number. Currently libeth counts down from 0, so the subtraction adds, and the count grows by two per buffer.idpf reads the count only on its XSk queues. At 0 it completes all done descriptors as XSk xmit frames at once. After the first
XDP_TXbuffer the count never gets back to 0, and every completion takes the slow path. After about 2^31 buffers the count wraps. If it lands on 0 whileXDP_TXbuffers are in flight, idpf completes them as XSk xmit frames. Their buffers are never freed, and userspace gets completions for xmit descriptors that are still in flight.
Quoted 8 lines
Count the completed buffers up, as the kernel-doc says, and say buffers there, not frames.
Found by an AI code review agent in review of this series. Not triggered at runtime.
Fixes 8ff6d62261a3 ("idpf: implement XSk xmit")Signed-off-by Björn Töpel <[email protected]>
[Severity: Low]
Should the Fixes: tag point to the libeth commit that introduced the inverted cp->xdp_tx -= sqe->nr_frags arithmetic in __libeth_xdp_complete_tx() and struct libeth_cq_pp::xdp_tx?
Commit 8ff6d62261a3 ("idpf: implement XSk xmit") did not modify include/net/libeth/ and only exposed the bug when reading xdpsq->xdp_tx in idpf_xsksq_complete().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] xsk: order-1 kmalloc demotion on 4K multi-buffer XDP_PASS skb heads --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] xsk: order-1 kmalloc demotion on 4K multi-buffer XDP_PASS skb heads
--
commit 97c565848cf50ed7cfbc3a8831daad9dd6b074f8
Author: Björn Töpel <[email protected]>
xsk: Keep the shared info of zero-copy buffers in kernel memory
This commit moves the skb_shared_info of zero-copy XSK buffers from UMEM tailroom into struct xsk_buff_pool and drops the tailroom reservation from zero-copy multi-buffer Rx frame sizes.
Quoted 4 lines
include/net/xdp_sock_drv.h +0
-0
[ ... ]
Quoted 8 lines
include/net/xdp_sock_drv.h +0
-4@@ -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; -
include/net/xdp_sock_drv.h static inline u32 __xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool) · line 57
Quoted 10 lines
include/net/xdp_sock_drv.h +1
-8-/* 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 ); }
[Severity: Medium]
Does removing the tailroom subtraction demote 4 KiB-chunk multi-buffer XDP_PASS skb heads to order-1 kmalloc-8k allocations?
With a 4096-byte UMEM chunk and zero headroom, xsk_pool_get_rx_frame_size() now returns 3840 bytes instead of 3456. On idpf (libeth_xskfq_create()), a multi-buffer XDP_PASS head passes 256 + 3840 = 4096 bytes to xdp_build_skb_from_zc() -> xdp_zc_alloc_head().
Because SKB_HEAD_ALIGN(4096) is 4416 > PAGE_SIZE (previously 4032), xdp_zc_alloc_head() bypasses system_page_pool and falls back to __netdev_alloc_skb() -> kmalloc-8k. i40e_construct_skb_zc() hits the same order-1 fallback in napi_alloc_skb() once totalsize reaches 3840.
This doubles skb head memory and truesize from 4 KiB to 8 KiB on every multi-buffer XDP_PASS packet, loses per-CPU page recycling, and risks GFP_ATOMIC order-1 allocation failures under memory fragmentation.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
[email protected]
AI reviewRe: [PATCH net v4 1/5] libeth: xdp: Count completed XDP_TX buffers up
In reply to Björn TöpelThank 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.
Critical: 0 · High: 2 (1) · Medium: 0 · Low: 0
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [High] On an idpf XDPSQ that has an XSk pool, the frag descriptors of a non-XSk multi-buffer XDP_TX frame are counted as XSk xmit completions.
- [High] The restored in-flight counter still drifts for non-XSk XDP_TX frames that are only partly sent.
Pre-existing issues:
- [High] When an idpf XDPSQ is in XSk mode, it can still be cleaned by the non-XSk routine idpf_xdpsq_complete().
--
libeth: xdp: Count completed XDP_TX buffers up
This patch changes __libeth_xdp_complete_tx() to add the completed non-XSk-xmit buffers to cp->xdp_tx instead of subtracting them. With that, idpf's in-flight XDP_TX counter can return to zero. It also updates the struct libeth_cq_pp kernel-doc to say buffers rather than frames.
include/net/libeth/xdp.h __libeth_xdp_complete_tx(struct libeth_sqe *sqe, struct libeth_cq_pp *cp, · line 1798
Quoted 10 lines
include/net/libeth/xdp.h +1
-1@@ -1795,7 +1795,7 @@ __libeth_xdp_complete_tx(struct libeth_sqe *sqe, struct libeth_cq_pp *cp,case LIBETH_SQE_XDP_TX :case LIBETH_SQE_XDP_XMIT :case LIBETH_SQE_XSK_TX : - cp->xdp_tx -= sqe->nr_frags; + cp->xdp_tx += sqe->nr_frags;
[Severity: High]
The counter is exact now, but can idpf_xsksq_complete_slow() tell non-XSk XDP_TX frags apart from XSk xmit descriptors?
A regular Rx queue picks its XDPSQ by CPU. __idpf_xdp_rxq_info_init() sets:
rxq->xdpsqs = &vport->txqs[rsrc->xdp_txq_offset];
and __libeth_xdp_tx_init_bulk() then picks xdpsqs[libeth_xdpsq_id(num)]. So XDP_TX from a regular Rx queue whose NAPI runs on CPU k lands on XDPSQ k, even when that queue is in XSk mode.
For a multi-buffer XDP_TX frame, libeth_xdp_tx_fill_buf() sets a type only on the head SQE:
if (!sinfo) return desc; sqe = &sq->sqes[i]; sqe->type = LIBETH_SQE_XDP_TX;
The non-first frag SQEs stay LIBETH_SQE_EMPTY. In contrast, ndo_xdp_xmit frags get LIBETH_SQE_XDP_XMIT_FRAG and XSk XDP_TX frags get LIBETH_SQE_XSK_TX_FRAG.
idpf_xsksq_complete_slow() counts every EMPTY slot as an XSk completion:
if (sqe->type) libeth_xdp_complete_tx(sqe, &cp); else xsk_frames++;
idpf_xsksq_complete() then passes that count toxsk_tx_completed(xdpsq->pool, xsk_frames).
For a frame with m frags, would the XSk completion ring producer move m entries past what was reserved? Userspace would then get completions for XSk xmit descriptors the NIC is still reading. The commit message says this outcome is removed.
The next patch in the series, "libeth: xsk: Count each XDP_TX buffer on its own", only changes libeth_xsk_tx_fill_buf() and the XSK_TX completion accounting. libeth_xdp_tx_fill_buf() is unchanged at the end of the series.
[Severity: High]
Can xdpsq->xdp_tx still drift when a non-XSk XDP_TX frame is only partly sent?
On the send side, libeth_xdp_tx_xmit_bulk() counts only the descriptors it actually placed:
*sq.xdp_tx += n;
Here n can stop partway through a multi-buffer frame.
On the completion side, __libeth_xdp_tx_fill_stats() sets the head SQE's nr_frags to the full frame:
ue->nr_frags += us->nr_frags;
The line above returns that full count. idpf then does xdpsq->xdp_tx -= cp.xdp_tx.
A frame can be sent short in at least two ways. First, __libeth_xdp_finalize_rx() flushes with:
flush_bulk(bq, flags | LIBETH_XDP_TX_DROP);
If the XDPSQ fills mid-frame, the head and some frags go out. Then libeth_xdp_tx_exception()->libeth_xdp_tx_return_bulk() skips the unsent frags without adjusting any counter:
if (!(frm->len_fl & LIBETH_XDP_TX_FIRST)) continue;
Second, libeth_xdp_tx_queue_bulk() breaks out of its frag loop when flush_bulk(bq, 0) fails. That leaves a MULTI head in the bulk without its remaining frags, so it is sent short later.
Each such event leaves the u32 xdpsq->xdp_tx lower by the number of missing frags. On an XSk XDPSQ, could xdp_tx then read 0 while that many XDP_TX buffers are still in flight? That would happen long before the 2^31 wrap described in the commit message.
When that happens, idpf_xsksq_complete() takes the fast path:
if (likely(!xdpsq->xdp_tx)) {
...
xsk_frames = done_frames;This reports in-flight XDP_TX descriptors through xsk_tx_completed(). It also skips their SQEs without freeing the page-pool buffers.
Before this patch the fast path never ran after the first XDP_TX, so the drift had no effect. The next patch in the series switches only LIBETH_SQE_XSK_TX / LIBETH_SQE_XSK_TX_FRAG to per-buffer counting. The LIBETH_SQE_XDP_TX case still adds sqe->nr_frags at the end of the series.
[Severity: High]
This is a pre-existing issue and was not introduced by this patch. Can an XSk-mode idpf XDPSQ be cleaned by idpf_xdpsq_complete() instead of idpf_xsksq_complete()?
In struct idpf_tx_queue, dev and pool share a union:
union {
struct device *dev;
struct xsk_buff_pool *pool;
};idpf_xsk_setup_txq() sets txq->pool = pool, so txq->dev then holds the pool pointer.
idpf_xdp_xmit() passes &vport->txqs[xdp_txq_offset] to libeth_xdp_xmit_do_bulk(), which picks a queue by CPU without checking XSK. Regular-Rx XDP_TX goes through idpf_xdp_tx_flush_bulk, which uses the same prep callback. idpf_xdp_tx_prep() then cleans with the non-XSk routine whatever the queue mode:
if (free < xdpsq->thresh) free += idpf_xdpsq_complete(xdpsq, xdpsq->thresh);
idpf_xdpsq_complete() builds:
struct libeth_cq_pp cp = {
.dev = xdpsq->dev,For completed LIBETH_SQE_XDP_XMIT and LIBETH_SQE_XDP_XMIT_FRAG SQEs, __libeth_xdp_complete_tx() then calls:
dma_unmap_page(cp->dev, dma_unmap_addr(sqe, dma), dma_unmap_len(sqe, len), DMA_TO_DEVICE);
Does that hand a struct xsk_buff_pool to the DMA API as a struct device? The DMA ops, IOMMU and swiotlb lookups would then read fields out of pool memory.
In the same loop, XSk xmit SQEs are EMPTY. __libeth_xdp_complete_tx() returns early for them, and next_to_clean moves past them, but xsk_tx_completed() is never called. Would the completion ring entries reserved for those frames be lost?
idpf_xsksq_complete() follows the intended rules: it uses xdpsq->pool->dev and reports EMPTY slots. No patch in this series changes idpf_xdp_tx_prep(), idpf_xdpsq_complete() or idpf_xdp_xmit().
Quoted 3 lines
include/net/libeth/xdp.h +0
-0cp->xss->packets++; cp->xss->bytes += sqe->bytes;
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261204142030.626820-1-bjorn%40kernel.org