From: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
To: "Björn Töpel" <bjorn@kernel.org>
Cc: Magnus Karlsson <magnus.karlsson@intel.com>,
Alexander Lobakin <aleksander.lobakin@intel.com>,
Alexei Starovoitov <ast@kernel.org>,
"Daniel Borkmann" <daniel@iogearbox.net>,
Jesper Dangaard Brouer <hawk@kernel.org>,
John Fastabend <john.fastabend@gmail.com>,
"David S. Miller" <davem@davemloft.net>,
Eric Dumazet <edumazet@kernel.org>,
Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
Willem de Bruijn <willemb@google.com>,
Andrii Nakryiko <andrii@kernel.org>,
Eduard Zingerman <eddyz87@gmail.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Shuah Khan <shuah@kernel.org>, <netdev@vger.kernel.org>,
<bpf@vger.kernel.org>, "Simon Horman" <horms@kernel.org>,
Stanislav Fomichev <sdf@fomichev.me>,
"Martin KaFai Lau" <martin.lau@linux.dev>,
Song Liu <song@kernel.org>,
Yonghong Song <yonghong.song@linux.dev>,
Jiri Olsa <jolsa@kernel.org>,
Emil Tsalapatis <emil@etsalapatis.com>,
Ihor Solodrai <ihor.solodrai@linux.dev>,
Tony Nguyen <anthony.l.nguyen@intel.com>,
Przemek Kitszel <przemyslaw.kitszel@intel.com>,
Larysa Zaremba <larysa.zaremba@intel.com>,
Tushar Vyavahare <tushar.vyavahare@intel.com>,
Jason Xing <kerneljasonxing@gmail.com>,
"Bastien Curutchet" <bastien.curutchet@bootlin.com>,
Colin Ian King <colin.i.king@gmail.com>,
<intel-wired-lan@lists.osuosl.org>,
<linux-kselftest@vger.kernel.org>
Subject: Re: [PATCH net 2/3] xsk: Keep the shared info of zero-copy buffers in kernel memory
Date: Fri, 27 Nov 2026 20:26:59 +0100 [thread overview]
Message-ID: <awnZgwW2z3fDj1Jx@boxer> (raw)
In-Reply-To: <20261127142131.3527737-3-bjorn@kernel.org>
On Fri, Nov 27, 2026 at 03:21:28PM +0100, Björn Töpel wrote:
> A 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 the previous patch fixes.
>
> Multi-buffer pools now give the device the whole chunk, as
> single-buffer pools already do, and allow a larger UMEM headroom.
> bpf_xdp_adjust_tail() can grow a packet up to the end of its chunk.
> Copy mode does not change.
>
> Found by an AI code review agent, in its review of another series.
>
> Suggested-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
> Fixes: 24ea50127ecf ("xsk: support mbuf on ZC RX")
> Signed-off-by: Björn Töpel <bjorn@kernel.org>
I applied this change on top of my changes implementing xsk.py in drv-net
selftests and gave this a run, plus a patch 3/3 was adjusted to fit the
new python runner but intent stayed the same.
Test used Intel's e810 (ice) back-to-back connection, 84 cases passed 0
failed.
Tested-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
Reviewed-by: Maciej Fijalkowski <maciej.fijalkowski@intel.com>
> ---
> include/net/xdp.h | 32 ++++++++++++++++++++++++++------
> include/net/xdp_sock_drv.h | 29 +++++++++--------------------
> include/net/xsk_buff_pool.h | 2 ++
> net/xdp/xdp_umem.c | 3 +--
> net/xdp/xsk_buff_pool.c | 17 +++++++++--------
> 5 files changed, 47 insertions(+), 36 deletions(-)
>
> diff --git a/include/net/xdp.h b/include/net/xdp.h
> index 07231adfb5f8..68dc9caacfa9 100644
> --- a/include/net/xdp.h
> +++ b/include/net/xdp.h
> @@ -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 xdp->rxq->ext_sinfo;
> +
> return (struct skb_shared_info *)xdp_data_hard_end(xdp);
> }
>
> diff --git a/include/net/xdp_sock_drv.h b/include/net/xdp_sock_drv.h
> index a40ad3f90f13..fc22314e72c4 100644
> --- a/include/net/xdp_sock_drv.h
> +++ b/include/net/xdp_sock_drv.h
> @@ -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;
> @@ -56,20 +51,13 @@ static inline u32 __xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
> return xsk_pool_get_chunk_size(pool) - xsk_pool_get_headroom(pool);
> }
>
> +/* Device RX buffers typically come in 128-byte units. The shared info
> + * of zero-copy buffers lives in the pool, see xp_set_rxq_info(), so the
> + * device may write up to the end of the chunk.
> + */
> 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 +140,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 +213,10 @@ 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 flag stays, see xp_set_rxq_info().
> + */
> + 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,
> diff --git a/include/net/xsk_buff_pool.h b/include/net/xsk_buff_pool.h
> index 02e89350faf9..f43e618ae707 100644
> --- a/include/net/xsk_buff_pool.h
> +++ b/include/net/xsk_buff_pool.h
> @@ -59,6 +59,8 @@ struct xsk_buff_pool {
> spinlock_t rx_lock;
> struct list_head free_list;
> struct list_head xskb_list;
> + /* Fragment list of the packet being built, out of userspace reach */
> + struct skb_shared_info sinfo;
> u32 heads_cnt;
> u16 queue_id;
>
> diff --git a/net/xdp/xdp_umem.c b/net/xdp/xdp_umem.c
> index cd8643360eb3..e78a43c3c3e9 100644
> --- a/net/xdp/xdp_umem.c
> +++ b/net/xdp/xdp_umem.c
> @@ -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) {
> diff --git a/net/xdp/xsk_buff_pool.c b/net/xdp/xsk_buff_pool.c
> index 5ec092f7cb6f..553630170229 100644
> --- a/net/xdp/xsk_buff_pool.c
> +++ b/net/xdp/xsk_buff_pool.c
> @@ -125,8 +125,14 @@ void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq)
> {
> u32 i;
>
> - for (i = 0; i < pool->heads_cnt; 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.
> + */
> + for (i = 0; i < pool->heads_cnt; i++) {
> pool->heads[i].xdp.rxq = rxq;
> + pool->heads[i].xdp.flags |= XDP_FLAGS_SINFO_EXT;
> + }
> + rxq->ext_sinfo = &pool->sinfo;
> }
> EXPORT_SYMBOL(xp_set_rxq_info);
>
> @@ -229,12 +235,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 +630,7 @@ 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 &= XDP_FLAGS_HAS_FRAGS | XDP_FLAGS_SINFO_EXT;
>
> if (pool->dev)
> xp_dma_sync_for_device(pool, xskb->dma, pool->frame_len);
> --
> 2.56.1
>
next prev parent reply other threads:[~2026-11-27 19:27 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-27 14:21 [PATCH net 0/3] xsk: Keep the shared info of zero-copy buffers in kernel memory Björn Töpel
2026-11-27 14:21 ` [PATCH net 1/3] libeth: xsk: Count each XDP_TX buffer on its own Björn Töpel
2026-11-27 14:21 ` [PATCH net 2/3] xsk: Keep the shared info of zero-copy buffers in kernel memory Björn Töpel
2026-11-27 19:26 ` Maciej Fijalkowski [this message]
2026-11-27 14:21 ` [PATCH net 3/3] selftests/bpf: Drop the zero-copy tailroom from the xsk tests Björn Töpel
2026-11-27 15:49 ` Maciej Fijalkowski
2026-11-27 16:56 ` Björn Töpel
2026-11-27 14:24 ` [PATCH net 0/3] xsk: Keep the shared info of zero-copy buffers in kernel memory netdev-bot+sinfo
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=awnZgwW2z3fDj1Jx@boxer \
--to=maciej.fijalkowski@intel.com \
--cc=aleksander.lobakin@intel.com \
--cc=andrii@kernel.org \
--cc=anthony.l.nguyen@intel.com \
--cc=ast@kernel.org \
--cc=bastien.curutchet@bootlin.com \
--cc=bjorn@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=colin.i.king@gmail.com \
--cc=daniel@iogearbox.net \
--cc=davem@davemloft.net \
--cc=eddyz87@gmail.com \
--cc=edumazet@kernel.org \
--cc=emil@etsalapatis.com \
--cc=hawk@kernel.org \
--cc=horms@kernel.org \
--cc=ihor.solodrai@linux.dev \
--cc=intel-wired-lan@lists.osuosl.org \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kerneljasonxing@gmail.com \
--cc=kuba@kernel.org \
--cc=larysa.zaremba@intel.com \
--cc=linux-kselftest@vger.kernel.org \
--cc=magnus.karlsson@intel.com \
--cc=martin.lau@linux.dev \
--cc=memxor@gmail.com \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=przemyslaw.kitszel@intel.com \
--cc=sdf@fomichev.me \
--cc=shuah@kernel.org \
--cc=song@kernel.org \
--cc=tushar.vyavahare@intel.com \
--cc=willemb@google.com \
--cc=yonghong.song@linux.dev \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox