xsk: Keep the shared info of zero-copy buffers in kernel memory
Subsystems (8)
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 2 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. Patch 3 updates the xsk selftests.
Sashiko found this in its review of the AF_XDP device buffer layout series [1].
I tested this on mlx5 hardware, and on igb and veth in QEMU. None of them receives multi-buffer packets in zero copy, so i40e, ice and idpf, which do, 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.
[1] https://lore.kernel.org/netdev/[email protected]/
Björn Töpel (3):
libeth: xsk: Count each XDP_TX buffer on its own
xsk: Keep the shared info of zero-copy buffers in kernel memory selftests/bpf: Drop the zero-copy tailroom from the xsk tests
include/net/libeth/tx.h | 3 +- include/net/libeth/xdp.h | 9 +++++- include/net/libeth/xsk.h | 6 ++-- 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 +++++----- .../selftests/bpf/prog_tests/test_xsk.c | 10 +++--- .../selftests/bpf/prog_tests/test_xsk.h | 2 -- tools/testing/selftests/bpf/prog_tests/xsk.c | 13 +------- tools/testing/selftests/bpf/xskxceiver.c | 15 +-------- 12 files changed, 67 insertions(+), 74 deletions(-)
base-commit: 28a371228d4eb4857cc31eeb1a0d7d01e9c3ea2f
Signature · 1 line
2.56.1
PATCH 1/3 libeth: xsk: Count each XDP_TX buffer on its own
libeth reads the shared info of an XSk XDP_TX frame when it flushes the bulk, not when it queues the frame. The fragment count it finds there decides how many completions idpf reports to the XSk pool.
The shared info of a zero-copy buffer lives in the UMEM, which userspace may write at any time, so a rewritten count makes idpf report the wrong number of completions. Once the next patch moves the shared info into the buffer pool, the next multi-buffer frame of the same poll can overwrite it before the flush.
Count each buffer on its own SQE. Head and fragments each take one off the in-flight count, and the head counts the packet. Stats do not change.
Found by code inspection.
--- include/net/libeth/tx.h | 3 ++- include/net/libeth/xdp.h | 9 ++++++++- include/net/libeth/xsk.h | 6 ++++-- 3 files changed, 14 insertions(+), 4 deletions(-)
include/net/libeth/tx.h +2 -1
@@ -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 ,
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; }
base-commit: 28a371228d4eb4857cc31eeb1a0d7d01e9c3ea2f
Signature · 1 line
2.56.1
PATCH 2/3 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 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.
--- 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(-)
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 xdp->rxq->ext_sinfo; +return (struct skb_shared_info *)xdp_data_hard_end (xdp); }
include/net/xdp_sock_drv.h +9 -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;
@@ -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,
include/net/xsk_buff_pool.h +2 -0
@@ -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;
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 +9 -8
@@ -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);
Signature · 1 line
2.56.1
PATCH 3/3 selftests/bpf: Drop the zero-copy tailroom from the xsk tests
In reply to Björn TöpelThe xsk tests take the size of the shared info off the UMEM headroom of RX_DROPPED and off the growth of XDP_ADJUST_TAIL_GROW_MULTI_BUFF, as zero-copy multi-buffer pools kept it at the end of each chunk.
The previous patch moves the shared info into the buffer pool, so the UMEM headroom limit and the frame size no longer leave room for it. Drop it from both tests.
--- tools/testing/selftests/bpf/prog_tests/test_xsk.c | 10 ++++------ tools/testing/selftests/bpf/prog_tests/test_xsk.h | 2 -- tools/testing/selftests/bpf/prog_tests/xsk.c | 13 +------------ tools/testing/selftests/bpf/xskxceiver.c | 15 +-------------- 4 files changed, 6 insertions(+), 34 deletions(-)
tools/testing/selftests/bpf/prog_tests/test_xsk.c +4 -6
@@ -1984,17 +1984,16 @@ int testapp_headroom(struct test_spec *test)int testapp_stats_rx_dropped (struct test_spec *test) {struct xsk_umem_info *umem = test->ifobj_rx->xsk->umem; -u32 umem_tr = test->ifobj_tx->umem_tailroom;if (test->mode ==TEST_MODE_ZC ) {ksft_print_msg ("Can not run RX_DROPPED test for ZC mode\n" );return TEST_SKIP ; } -if (pkt_stream_replace_half (test, (MIN_PKT_SIZE *3 ) + umem_tr,0 )) +if (pkt_stream_replace_half (test,MIN_PKT_SIZE *4 ,0 ))return TEST_FAILURE ; umem->frame_headroom = umem->frame_size - -XDP_PACKET_HEADROOM - (MIN_PKT_SIZE *2 ) - umem_tr; +XDP_PACKET_HEADROOM -MIN_PKT_SIZE *3 ;if (pkt_stream_receive_half (test))return TEST_FAILURE ; test->ifobj_rx->validation_func = validate_rx_dropped;
@@ -2592,12 +2591,11 @@ int testapp_adjust_tail_grow_mb(struct test_spec *test)* pkt_len, expect that it will be broken down to 2 descs each * with 3k payload; * -* 4k is truesize, 3k payload, 256 HR, 320 TR; +* 4k is truesize, 3k payload, 256 HR; */ grow_size =XSK_UMEM__MAX_FRAME_SIZE -XSK_UMEM__LARGE_FRAME_SIZE - -XDP_PACKET_HEADROOM - - test->ifobj_tx->umem_tailroom; +XDP_PACKET_HEADROOM ; test->mtu =MAX_ETH_JUMBO_SIZE ;return testapp_adjust_tail (test, grow_size,XSK_UMEM__LARGE_FRAME_SIZE *2 );
tools/testing/selftests/bpf/prog_tests/test_xsk.h +0 -2
@@ -32,7 +32,6 @@#define USLEEP_MAX 10000#define MAX_SKB_FRAGS_PATH "/proc/sys/net/core/max_skb_frags" -#define SMP_CACHE_BYTES_PATH "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"extern bool opt_verbose; #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
@@ -137,7 +136,6 @@ struct ifobject {int mtu;u32 bind_flags;u32 xdp_zc_max_segs; -u32 umem_tailroom;u32 max_skb_frags;bool tx_on;bool rx_on;
tools/testing/selftests/bpf/prog_tests/xsk.c +1 -12
@@ -62,7 +62,7 @@ int configure_ifobj(struct ifobject *tx, struct ifobject *rx)static void test_xsk (const struct test_spec *test_to_run,enum test_mode mode) { -u32 max_frags, umem_tailroom, cache_line_size; +u32 max_frags;struct ifobject *ifobj_tx, *ifobj_rx;struct test_spec test;int ret;
@@ -85,10 +85,6 @@ static void test_xsk(const struct test_spec *test_to_run, enum test_mode mode) ifobj_tx->set_ring.default_rx = ifobj_tx->ring.rx_pending; } - cache_line_size =read_procfs_val (SMP_CACHE_BYTES_PATH ); -if (!cache_line_size) - cache_line_size =64 ; - max_frags =read_procfs_val (MAX_SKB_FRAGS_PATH );if (!max_frags) max_frags =17 ;
@@ -96,13 +92,6 @@ static void test_xsk(const struct test_spec *test_to_run, enum test_mode mode) ifobj_tx->max_skb_frags = max_frags; ifobj_rx->max_skb_frags = max_frags; -/* 48 bytes is a part of skb_shared_info w/o frags array; -* 16 bytes is sizeof(skb_frag_t) -*/ - umem_tailroom =ALIGN (48 + (max_frags *16 ), cache_line_size); - ifobj_tx->umem_tailroom = umem_tailroom; - ifobj_rx->umem_tailroom = umem_tailroom; -if (!ASSERT_OK (init_iface (ifobj_rx, worker_testapp_validate_rx),"init RX" )) goto delete_rx;if (!ASSERT_OK (init_iface (ifobj_tx, worker_testapp_validate_tx),"init TX" ))
tools/testing/selftests/bpf/xskxceiver.c +1 -14
@@ -334,7 +334,7 @@ static void print_tests(void)int main (int argc,char **argv) {const size_t total_tests =ARRAY_SIZE (tests) +ARRAY_SIZE (ci_skip_tests); -u32 cache_line_size, max_frags, umem_tailroom; +u32 max_frags;struct pkt_stream *rx_pkt_stream_default;struct pkt_stream *tx_pkt_stream_default;struct ifobject *ifobj_tx, *ifobj_rx;
@@ -356,12 +356,6 @@ int main(int argc, char **argv)setlocale (LC_ALL ,"" ); - cache_line_size =read_procfs_val (SMP_CACHE_BYTES_PATH ); -if (!cache_line_size) { -ksft_print_msg ("Can't get SMP_CACHE_BYTES from system, using default (64)\n" ); - cache_line_size =64 ; - } - max_frags =read_procfs_val (MAX_SKB_FRAGS_PATH );if (!max_frags) {ksft_print_msg ("Can't get MAX_SKB_FRAGS from system, using default (17)\n" );
@@ -370,13 +364,6 @@ int main(int argc, char **argv) ifobj_tx->max_skb_frags = max_frags; ifobj_rx->max_skb_frags = max_frags; -/* 48 bytes is a part of skb_shared_info w/o frags array; -* 16 bytes is sizeof(skb_frag_t) -*/ - umem_tailroom =ALIGN (48 + (max_frags *16 ), cache_line_size); - ifobj_tx->umem_tailroom = umem_tailroom; - ifobj_rx->umem_tailroom = umem_tailroom; -parse_command_line (ifobj_tx, ifobj_rx, argc, argv);if (opt_print_tests) {
Signature · 1 line
2.56.1
[email protected]
AI reviewThis is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
Hi!
This is an automated message. This series looks like a fix, but its commit messages seem to be missing some information:
- Whether the issue was actually triggered, or is only theoretical (e.g. found by LLM and not proven at runtime). If it was triggered please include the symptoms, like the stack trace or kernel messages.
Please do not repost the series just to address the above. Instead, reply to this email with the missing information, so that reviewers can take it into account.
*IMPORTANT*: If you do repost (for other reasons) - please include the relevant info in the cover letter or commit messages.
Signature · 2 lines
The evaluation is done by an LLM so it may be wrong, if you think that is the case please reply and explain.
Maciej Fijalkowski
maintainerRe: [PATCH net 3/3] selftests/bpf: Drop the zero-copy tailroom from the xsk tests
In reply to Björn TöpelQuoted 8 lines · Björn Töpel
The xsk tests take the size of the shared info off the UMEM headroom of
RX_DROPPEDand off the growth ofXDP_ADJUST_TAIL_GROW_MULTI_BUFF, as zero-copy multi-buffer pools kept it at the end of each chunk.The previous patch moves the shared info into the buffer pool, so the UMEM headroom limit and the frame size no longer leave room for it. Drop it from both tests.
Quoted 6 lines
Signed-off-by Björn Töpel <[email protected]>--- tools/testing/selftests/bpf/prog_tests/test_xsk.c | 10 ++++------ tools/testing/selftests/bpf/prog_tests/test_xsk.h | 2 -- tools/testing/selftests/bpf/prog_tests/xsk.c | 13 +------------ tools/testing/selftests/bpf/xskxceiver.c | 15 +--------------
This is based on old xskxceiver location, would it be better to have this one send to -next out of this set? I assume the move might not have been back-propagated yet...so seems we would have to double the effort?
Quoted 142 lines
4 files changed, 6 insertions(+), 34 deletions(-)tools/testing/selftests/bpf/prog_tests/test_xsk.c +4
-6@@ -1984,17 +1984,16 @@ int testapp_headroom(struct test_spec *test)int testapp_stats_rx_dropped (struct test_spec *test) {struct xsk_umem_info *umem = test->ifobj_rx->xsk->umem; -u32 umem_tr = test->ifobj_tx->umem_tailroom;if (test->mode ==TEST_MODE_ZC ) {ksft_print_msg ("Can not run RX_DROPPED test for ZC mode\n" );return TEST_SKIP ; } -if (pkt_stream_replace_half (test, (MIN_PKT_SIZE *3 ) + umem_tr,0 )) +if (pkt_stream_replace_half (test,MIN_PKT_SIZE *4 ,0 ))return TEST_FAILURE ; umem->frame_headroom = umem->frame_size - -XDP_PACKET_HEADROOM - (MIN_PKT_SIZE *2 ) - umem_tr; +XDP_PACKET_HEADROOM -MIN_PKT_SIZE *3 ;if (pkt_stream_receive_half (test))return TEST_FAILURE ; test->ifobj_rx->validation_func = validate_rx_dropped;@@ -2592,12 +2591,11 @@ int testapp_adjust_tail_grow_mb(struct test_spec *test)* pkt_len, expect that it will be broken down to 2 descs each * with 3k payload; * -* 4k is truesize, 3k payload, 256 HR, 320 TR; +* 4k is truesize, 3k payload, 256 HR; */ grow_size =XSK_UMEM__MAX_FRAME_SIZE -XSK_UMEM__LARGE_FRAME_SIZE - -XDP_PACKET_HEADROOM - - test->ifobj_tx->umem_tailroom; +XDP_PACKET_HEADROOM ; test->mtu =MAX_ETH_JUMBO_SIZE ;return testapp_adjust_tail (test, grow_size,XSK_UMEM__LARGE_FRAME_SIZE *2 );tools/testing/selftests/bpf/prog_tests/test_xsk.h +0
-2@@ -32,7 +32,6 @@#define USLEEP_MAX 10000#define MAX_SKB_FRAGS_PATH "/proc/sys/net/core/max_skb_frags" -#define SMP_CACHE_BYTES_PATH "/sys/devices/system/cpu/cpu0/cache/index0/coherency_line_size"extern bool opt_verbose; #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)@@ -137,7 +136,6 @@ struct ifobject {int mtu;u32 bind_flags;u32 xdp_zc_max_segs; -u32 umem_tailroom;u32 max_skb_frags;bool tx_on;bool rx_on;tools/testing/selftests/bpf/prog_tests/xsk.c +1
-12@@ -62,7 +62,7 @@ int configure_ifobj(struct ifobject *tx, struct ifobject *rx)static void test_xsk (const struct test_spec *test_to_run,enum test_mode mode) { -u32 max_frags, umem_tailroom, cache_line_size; +u32 max_frags;struct ifobject *ifobj_tx, *ifobj_rx;struct test_spec test;int ret;@@ -85,10 +85,6 @@ static void test_xsk(const struct test_spec *test_to_run, enum test_mode mode) ifobj_tx->set_ring.default_rx = ifobj_tx->ring.rx_pending; } - cache_line_size =read_procfs_val (SMP_CACHE_BYTES_PATH ); -if (!cache_line_size) - cache_line_size =64 ; - max_frags =read_procfs_val (MAX_SKB_FRAGS_PATH );if (!max_frags) max_frags =17 ;@@ -96,13 +92,6 @@ static void test_xsk(const struct test_spec *test_to_run, enum test_mode mode) ifobj_tx->max_skb_frags = max_frags; ifobj_rx->max_skb_frags = max_frags; -/* 48 bytes is a part of skb_shared_info w/o frags array; -* 16 bytes is sizeof(skb_frag_t) -*/ - umem_tailroom =ALIGN (48 + (max_frags *16 ), cache_line_size); - ifobj_tx->umem_tailroom = umem_tailroom; - ifobj_rx->umem_tailroom = umem_tailroom; -if (!ASSERT_OK (init_iface (ifobj_rx, worker_testapp_validate_rx),"init RX" )) goto delete_rx;if (!ASSERT_OK (init_iface (ifobj_tx, worker_testapp_validate_tx),"init TX" ))tools/testing/selftests/bpf/xskxceiver.c +1
-14@@ -334,7 +334,7 @@ static void print_tests(void)int main (int argc,char **argv) {const size_t total_tests =ARRAY_SIZE (tests) +ARRAY_SIZE (ci_skip_tests); -u32 cache_line_size, max_frags, umem_tailroom; +u32 max_frags;struct pkt_stream *rx_pkt_stream_default;struct pkt_stream *tx_pkt_stream_default;struct ifobject *ifobj_tx, *ifobj_rx;@@ -356,12 +356,6 @@ int main(int argc, char **argv)setlocale (LC_ALL ,"" ); - cache_line_size =read_procfs_val (SMP_CACHE_BYTES_PATH ); -if (!cache_line_size) { -ksft_print_msg ("Can't get SMP_CACHE_BYTES from system, using default (64)\n" ); - cache_line_size =64 ; - } - max_frags =read_procfs_val (MAX_SKB_FRAGS_PATH );if (!max_frags) {ksft_print_msg ("Can't get MAX_SKB_FRAGS from system, using default (17)\n" );@@ -370,13 +364,6 @@ int main(int argc, char **argv) ifobj_tx->max_skb_frags = max_frags; ifobj_rx->max_skb_frags = max_frags; -/* 48 bytes is a part of skb_shared_info w/o frags array; -* 16 bytes is sizeof(skb_frag_t) -*/ - umem_tailroom =ALIGN (48 + (max_frags *16 ), cache_line_size); - ifobj_tx->umem_tailroom = umem_tailroom; - ifobj_rx->umem_tailroom = umem_tailroom; -parse_command_line (ifobj_tx, ifobj_rx, argc, argv);if (opt_print_tests) {Signature · 1 line
2.56.1
Re: [PATCH net 3/3] selftests/bpf: Drop the zero-copy tailroom from the xsk tests
Quoted 17 lines · Maciej Fijalkowski
Quoted 8 lines · Björn Töpel
The xsk tests take the size of the shared info off the UMEM headroom of
RX_DROPPEDand off the growth ofXDP_ADJUST_TAIL_GROW_MULTI_BUFF, as zero-copy multi-buffer pools kept it at the end of each chunk.The previous patch moves the shared info into the buffer pool, so the UMEM headroom limit and the frame size no longer leave room for it. Drop it from both tests.
Quoted 6 lines
Signed-off-by Björn Töpel <[email protected]>--- tools/testing/selftests/bpf/prog_tests/test_xsk.c | 10 ++++------ tools/testing/selftests/bpf/prog_tests/test_xsk.h | 2 -- tools/testing/selftests/bpf/prog_tests/xsk.c | 13 +------------ tools/testing/selftests/bpf/xskxceiver.c | 15 +--------------
Quoted 3 lines
This is based on old xskxceiver location, would it be better to have this one send to -next out of this set? I assume the move might not have been back-propagated yet...so seems we would have to double the effort?
Ya, good call. I'll pop this out of the series, and respin a v2 w/o this.
Please have a look at the Intel side of things if 1 and 2 is a good fit. I'll spin a v2 when Sashiko has spoken!
Björn
Quoted 17 lines · Björn Töpel
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_TXbulk 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.
Quoted 5 lines
Found by an AI code review agent, in its review of another series.
Suggested-by Maciej Fijalkowski <[email protected]>Fixes 24ea50127ecf ("xsk: support mbuf on ZC RX")Signed-off-by Björn Töpel <[email protected]>
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.
Quoted 206 lines
--- 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(-)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 xdp->rxq->ext_sinfo; +return (struct skb_shared_info *)xdp_data_hard_end (xdp); }include/net/xdp_sock_drv.h +9
-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;@@ -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,include/net/xsk_buff_pool.h +2
-0@@ -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;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 +9
-8@@ -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);Signature · 1 line
2.56.1