Skip to contentSkip to conversation

xsk: Return UMEM chunks explicitly

PATCH In review Björn Töpel 2 participants 17 messages Dec 4

Subsystems (16)
lore
Share & tools
lore
#0 lore raw

v2 cover xsk: Return UMEM chunks explicitly

Currently, AF_XDP zero copy fills one UMEM chunk per buffer at the UMEM headroom, and each RX descriptor gives its chunk back. Drivers carry a second RX path for that layout, and cannot use their own buffer sizes or pack small packets into a page.

This series adds explicit chunk return. With XDP_UMEM_EXPLICIT_RETURN, the device lays out buffers in chunks as in pages, so a chunk may hold several buffers and a packet may span chunks. Each buffer reaches the socket as an RX descriptor. A chunk comes back on a descriptor with XDP_RX_CHUNK_RETURNED, or without one once the receiving socket is gone. The application cannot influence where packets land, how many share a chunk, or the headroom.

The design came out of off-list discussions with Magnus, Maciej, Stanislav and Jakub, where Jakub proposed letting the device keep its own buffer layout.

The chunks back the bound queue's RX page pools through an AF_XDP memory provider, so drivers need no AF_XDP code. fbnic and mlx5 add 54 and 165 lines, to handle buffers as netmem, refuse settings that the provider cannot take and, for mlx5, restart queues with a provider bound. A native XDP program steers packets to the socket. The kernel keeps nothing in the chunks, as userspace may write them at any time.

The provider takes no lock. The driver uses the queue's buffers from one context at a time, its NAPI or an operation that runs only while that NAPI cannot use the rings. It runs that NAPI once after the queue starts, and passes the buffers only to the queue's socket. That context writes every return at once, as the RX ring keeps room for each chunk the kernel owes, so packets drop a little before the ring is full.

This posting drops XDP_PASS and XDP_TX of these buffers. Both need a copy, which a follow-up adds. Until then, management traffic should use other queues. Sockets transmit in copy mode only.

Performance in Mpps with 4 KiB chunks and preferred busy polling, so application and NAPI share one CPU. Native XDP drops each packet and AF_XDP sockets only return it, untouched. Min-max of 3 runs, 5 for mlx5 XDP_DROP below 300 B; * is within 2% of the offered load, ** a PCIe limit (Gen4 x4).

MTU 1500:

                 offered  native        explicit      classic
  frame   NIC             XDP_DROP      return        zero copy
  66 B    mlx5   61.0     31.38-32.71   18.19-18.25   23.40-23.87
          fbnic  65.3     37.68-38.15   26.61-26.96   n/a
  128 B   mlx5   37.0     31.07-31.27** 18.09-18.28   23.22-23.64
          fbnic  40.5     37.99-38.21   26.81-27.14   n/a
  300 B   mlx5   19.3     17.70-17.71** 17.72-17.73** 17.72-17.73**
          fbnic  17.4     17.38-17.43*  17.41-17.52*  n/a
  1500 B  mlx5   4.10     4.10*         4.10*         4.09*
          fbnic  4.04     4.03-4.08*    4.03-4.05*    n/a

MTU 5000, where classic zero copy cannot bind on mlx5; fbnic has none:

                 offered  native        explicit      classic
  frame   NIC             XDP_DROP      return        zero copy
  300 B   mlx5   19.3     17.78**       17.21-17.48** n/a
          fbnic  17.4     17.41-17.48*  17.39-17.46*  n/a
  1500 B  mlx5   4.10     4.10*         4.10*         n/a
          fbnic  4.04     4.04-4.06*    4.03-4.04*    n/a
  3000 B  mlx5   2.07     2.07*         2.07*         n/a
          fbnic  2.02     2.02*         2.02*         n/a
  5000 B  mlx5   1.24     1.24*         1.24*         n/a
          fbnic  1.21     1.20-1.21*    1.20-1.21*    n/a

Native XDP (XDP_DROP, XDP_TX and XDP_REDIRECT), classic AF_XDP and the kernel UDP and TCP paths run at the rate of the base on mlx5, fbnic and bnxt, within the run-to-run spread.

This series transmits in copy mode; the full branch, with bnxt, zero-copy TX and the follow-up optimizations, and its numbers are at
https://github.com/fb-bjorn/linux/tree/xsk-explicit-return-v2.

Dependencies:
- "xsk: Keep the shared info of zero-copy buffers in kernel memory",
https://lore.kernel.org/netdev/[email protected]/;
- "eth: fbnic: Support XDP redirect",
https://lore.kernel.org/netdev/[email protected]/;
- "net/mlx5e: Report a down device as -ENETDOWN to queue restarts",
https://lore.kernel.org/netdev/[email protected]/;
- "ethtool: Allow the channel count that a memory provider needs",
https://lore.kernel.org/netdev/[email protected]/;
- "bpf: Use the checked TCP header length in the syncookie helpers",
https://lore.kernel.org/netdev/[email protected]/.

v1: https://lore.kernel.org/netdev/[email protected]/

Changes since v1:
- v1 was "xsk: Receive in the device's buffer layout". The flags are now XDP_UMEM_EXPLICIT_RETURN and XDP_RX_CHUNK_RETURNED, not XDP_UMEM_DRV_LAYOUT and XDP_RX_CHUNK_FREE, after Magnus. xsk_sink is now xsk_mp, and "sink" is "XDP-only" in kernel names;
- v1 patch 2 became a net series, see Dependencies, which also moves the shared info of classic zero-copy buffers out of the UMEM, after Maciej;
- v1 patch 9, fbnic XDP_REDIRECT, is a dependency. v1 patch 11 is part of patch 2, and v1 patch 12 is split into patches 10 and
11. bnxt, v1 patch 13, comes back in a follow-up, after fixes to its queue restarts. Patches 8, 12 and 13 are new;
- binding also needs CAP_NET_ADMIN, and XDP_ZEROCOPY fails as XDP_COPY did. Classic zero copy may bind next to an XDP-only provider on another queue;
- an empty head with metadata reaches the socket as a zero-length first descriptor with XDP_PKT_CONTD. v1 dropped such packets;
- a returned chunk counts against no page pool, so a restart while bound does not starve the new queue;
- the provider takes no lock, as the driver uses a queue's buffers from one context at a time. Returns go into RX ring room kept for them, and need_wakeup reports only an empty FILL ring;
- the provider drops the references of a run of buffers at once, and mlx5 lets go of a page after its last stride, so fewer chunks need an extra empty descriptor;
- the provider goes with the last socket of the queue, not with the receiving socket. Once the receiver is gone, the queue drops what it redirects and the chunks the kernel owes return. The rest return with the last socket, and a program detach right after that may briefly fail with EBUSY;
- redirects show in the xdp_redirect_map tracepoints, and page-pool dumps carry a new, empty xsk nest, NETDEV_A_PAGE_POOL_XSK;
- mlx5 takes the provider on striding RQs only. While a queue has one, it refuses to leave them, PTP, HTB offload and growing the RQs of a down device. A channel switch closes the old channels first;
- xskxceiver's --drv-layout is now --explicit-return, -H checks the headroom, and RX_RING_FULL_RETURNS is new. The test now fits the reworked xsk selftests, and drivers/net/xsk.py runs its rx cases with explicit return on a NIC. The af_xdp.rst section "Device Buffer Layout" is now "Explicit Chunk Return", with a part for drivers;
- many fixes from a machine review of v1, listed in each patch.

Björn Töpel (15):
xdp: Let memory providers expose net_iovs to XDP
net: Add memory providers that only feed XDP
page_pool: Let memory providers take back net_iovs
xdp: Run programs on XDP-only buffers
xsk: Back a queue's page pools with UMEM chunks
xsk: Deliver packets and return chunks explicitly
xsk: Receive with explicit chunk return
xsk: Drop references of a run of buffers at once
eth: fbnic: Receive into XDP-only memory providers net/mlx5e: Handle restarts with XDP-only providers net/mlx5e: Receive into XDP-only memory providers net/mlx5e: Release XDP-only pages earlier net/mlx5e: Test for XDP-only pages per RQ
selftests: net: xsk: Test explicit chunk return
Documentation: xsk: Describe explicit chunk return

 Documentation/netlink/specs/netdev.yaml       |   8 +
 Documentation/networking/af_xdp.rst           | 244 ++++-
 drivers/net/ethernet/mellanox/mlx5/core/en.h  |   1 +
 .../net/ethernet/mellanox/mlx5/core/en_main.c | 100 +-
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   |  83 +-
 .../net/ethernet/meta/fbnic/fbnic_ethtool.c   |  22 +
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.c  |  41 +-
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.h  |   8 +
 drivers/net/netkit.c                          |   2 +-
 include/linux/netdevice.h                     |   2 +
 include/net/netdev_queues.h                   |   1 +
 include/net/netdev_rx_queue.h                 |   5 +
 include/net/netmem.h                          |  31 +-
 include/net/page_pool/helpers.h               |   4 +-
 include/net/page_pool/memory_provider.h       |  22 +
 include/net/page_pool/types.h                 |  24 +-
 include/net/xdp.h                             | 117 ++-
 include/net/xdp_sock.h                        |   8 +
 include/net/xsk_buff_pool.h                   |   3 +
 include/uapi/linux/if_xdp.h                   |  23 +-
 include/uapi/linux/netdev.h                   |   1 +
 net/core/dev.c                                |  56 +-
 net/core/filter.c                             |  46 +-
 net/core/netdev_rx_queue.c                    |  51 +-
 net/core/page_pool.c                          |  71 +-
 net/core/xdp.c                                |  55 ++
 net/ethtool/rings.c                           |   5 +-
 net/xdp/Kconfig                               |   1 +
 net/xdp/Makefile                              |   2 +-
 net/xdp/xdp_umem.c                            |   5 +
 net/xdp/xsk.c                                 | 110 ++-
 net/xdp/xsk_buff_pool.c                       |  38 +-
 net/xdp/xsk_mp.c                              | 900 ++++++++++++++++++
 net/xdp/xsk_mp.h                              |  21 +
 tools/include/uapi/linux/if_xdp.h             |  23 +-
 tools/include/uapi/linux/netdev.h             |   1 +
 tools/testing/selftests/drivers/net/xsk.py    |  28 +-
 .../testing/selftests/net/lib/xsk/test_xsk.c  | 556 ++++++++++-
 .../testing/selftests/net/lib/xsk/test_xsk.h  |   9 +
 .../selftests/net/lib/xsk/xskxceiver.c        |  57 +-
 40 files changed, 2659 insertions(+), 126 deletions(-)
 create mode 100644 net/xdp/xsk_mp.c
 create mode 100644 net/xdp/xsk_mp.h

base-commit: e5ba10df689a988da8480e4f3e2a6cd88a506f97

Signature · 1 line
2.56.1
#1 lore raw

PATCH v2 1/15 xdp: Let memory providers expose net_iovs to XDP

Currently, the address helpers give NULL for net_iovs, so XDP cannot read them. The AF_XDP provider later in this series backs net_iovs with UMEM chunks that XDP must read. As for io_uring zcrx, these are user pages pinned with GUP, whose struct page fields mm owns, so they cannot be page_pool pages.

Let a net_iov area offer an XDP view, an address per net_iov that XDP may read and write. Add xdp_netmem_address() and xdp_frag_address() for drivers and the BPF helpers. They give NULL for a net_iov without a view.

The stack still treats every net_iov as unreadable, so other readers get NULL and fail closed. Such buffers must never become skb frags or xdp_frames.

Providers still block native XDP and need header split until the next patch adds MP_XDP_ONLY.

---

Notes (v2changes):
Changes in v2:
- Rename from "xdp: Let memory providers offer XDP a view of their net_iovs".
- Name the provider by what it does, AF_XDP with explicit chunk return, and say that MP_XDP_ONLY comes with the next patch.
- Bring the comment on unreadable XDP frags up to date, as XDP reads such frags through the view of their provider (Sashiko).

 include/net/netmem.h |  8 ++++++++
 include/net/xdp.h    | 47 +++++++++++++++++++++++++++++++++++++++++---
 net/core/filter.c    |  8 ++++----
 3 files changed, 56 insertions(+), 7 deletions(-)
include/net/netmem.h +8 -0
index fb693bc1702a..4cf4ee1cd55d 100644
@@ -104,6 +104,14 @@ struct net_iov_area {
 	/* Array of net_iovs for this area. */
 	struct net_iov *niovs;
 	size_t num_niovs;
+	/* XDP view, see xdp_netmem_address(): XDP may read and write net_iov i
+	 * at xdp_vaddr + (i << xdp_shift), xdp_vsize bytes in all. NULL if the
+	 * provider offers none. The core and the stack take every net_iov as
+	 * unreadable either way.
+	 */
+	void *xdp_vaddr;
+	size_t xdp_vsize;
+	unsigned int xdp_shift;
 };
 
 static inline struct net_iov_area *net_iov_owner(const struct net_iov *niov)
include/net/xdp.h +44 -3

59 lines not on this page · raw message

net/core/filter.c +4 -4
index 1ebc2fab7211..90e4bf78c689 100644
@@ -4311,7 +4311,7 @@ void bpf_xdp_copy_buf(struct xdp_buff *xdp, unsigned long off,
 			break;
 
 		ptr_off += ptr_len;
-		ptr_buf = skb_frag_address(next_frag);
+		ptr_buf = xdp_frag_address(next_frag);
 		ptr_len = skb_frag_size(next_frag);
 		next_frag++;
 	}
@@ -4339,7 +4339,7 @@ void *bpf_xdp_pointer(struct xdp_buff *xdp, u32 offset, u32 len)
 		u32 frag_size = skb_frag_size(&sinfo->frags[i]);
 
 		if  (offset < frag_size) {
-			addr = skb_frag_address(&sinfo->frags[i]);
+			addr = xdp_frag_address(&sinfo->frags[i]);
 			size = frag_size;
 			break;
 		}
@@ -4430,7 +4430,7 @@ static int bpf_xdp_frags_increase_tail(struct xdp_buff *xdp, int offset)
 	if (unlikely(offset > tailroom))
 		return -EINVAL;
 
-	memset(skb_frag_address(frag) + skb_frag_size(frag), 0, offset);
+	memset(xdp_frag_address(frag) + skb_frag_size(frag), 0, offset);
 	skb_frag_size_add(frag, offset);
 	sinfo->xdp_frags_size += offset;
 	if (rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL)
@@ -12846,7 +12846,7 @@ __bpf_kfunc int bpf_xdp_pull_data(struct xdp_md *x, u32 len)
 		skb_frag_t *frag = &sinfo->frags[i];
 		u32 shrink = min_t(u32, delta, skb_frag_size(frag));
 
-		memcpy(xdp->data_end, skb_frag_address(frag), shrink);
+		memcpy(xdp->data_end, xdp_frag_address(frag), shrink);
 
 		xdp->data_end += shrink;
 		sinfo->xdp_frags_size -= shrink;
Signature · 1 line
2.56.1
#2 lore raw

PATCH v2 2/15 net: Add memory providers that only feed XDP

In reply to Björn Töpel

So far, the core requires header split for memory providers and refuses native XDP next to them, as they serve payload to the stack. The AF_XDP provider later in this series feeds only XDP.

Let a provider declare itself XDP-only with MP_XDP_ONLY, which lifts both rules. Drivers opt in with PP_FLAG_ALLOW_XDP_ONLY_NETMEM, and the install fails on others.

The provider takes no lock. With the flag, the driver lets one context at a time use the queue's buffers, its NAPI or an operation that runs only while that NAPI cannot use the rings. It runs that NAPI once after the queue starts, so that a queue that starts with FILL empty asks for a wakeup, and passes the buffers only to the queue's AF_XDP socket.

Add netif_rxq_has_xdp_only_mp() for drivers that size a queue before its page pools exist.

Until now, classic zero copy could not bind next to any provider of the device. Refuse it only next to providers that block native XDP.

---

Notes (v2changes):
Changes in v2:
- Fold in v1 patch 11, netif_rxq_has_xdp_only_mp(), which drivers call before the page pools of a queue exist.
- Refuse classic zero copy, also through netkit, only next to providers that block native XDP, so that it binds next to an XDP-only provider on another queue of the device, in either order (Sashiko).
- Say that the private data of such a provider starts with its net_iov area, and that a driver that sets the flag lets one context at a time use the buffers of the queue and runs its NAPI once after a start, as the provider takes no lock.
- Say in a comment that the core counts an XDP-only provider as unreadable.

 drivers/net/netkit.c                    |  2 +-
 include/linux/netdevice.h               |  1 +
 include/net/netdev_queues.h             |  1 +
 include/net/page_pool/helpers.h         |  4 +-
 include/net/page_pool/memory_provider.h | 19 +++++++++
 include/net/page_pool/types.h           | 23 ++++++++++-
 net/core/dev.c                          | 21 +++++++++-
 net/core/netdev_rx_queue.c              | 51 +++++++++++++++++++++----
 net/core/page_pool.c                    | 16 ++++++--
 net/ethtool/rings.c                     |  5 ++-
 net/xdp/xsk_buff_pool.c                 |  2 +-
 11 files changed, 126 insertions(+), 19 deletions(-)
drivers/net/netkit.c +1 -1
index 82e608e733d9..b1f8e3b9292a 100644
@@ -289,7 +289,7 @@ static int netkit_xsk(struct net_device *dev, struct netdev_bpf *xdp)
 		ret = -EOPNOTSUPP;
 		goto out;
 	}
-	if (!create || !dev_get_min_mp_channel_count(phys))
+	if (!create || !dev_mp_blocks_xdp(phys))
 		ret = phys->netdev_ops->ndo_bpf(phys, &xdp_lower);
 out:
 	netdev_unlock(phys);
include/linux/netdevice.h +1 -0
index 694b6d29f680..9e542d793df7 100644
@@ -4459,6 +4459,7 @@ u8 dev_xdp_sb_prog_count(struct net_device *dev);
 u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode);
 
 u32 dev_get_min_mp_channel_count(const struct net_device *dev);
+bool dev_mp_blocks_xdp(const struct net_device *dev);
 
 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
include/net/netdev_queues.h +1 -0
index c5335e935b7d..813388778797 100644
@@ -235,6 +235,7 @@ void netdev_queue_config(struct net_device *dev, int rxq,
 			 struct netdev_queue_config *qcfg);
 
 bool netif_rxq_has_unreadable_mp(struct net_device *dev, unsigned int rxq_idx);
+bool netif_rxq_has_xdp_only_mp(struct net_device *dev, unsigned int rxq_idx);
 
 /**
  * DOC: Lockless queue stopping / waking helpers.
include/net/page_pool/helpers.h +3 -1
index 28635ce9454e..e3cca0a8efa1 100644
@@ -513,7 +513,9 @@ static inline void page_pool_nid_changed(struct page_pool *pool, int new_nid)
  * kernel. This will only be the case if user space bound a memory provider (mp)
  * which returns unreadable memory to the queue served by the page pool.
  * If %PP_FLAG_ALLOW_UNREADABLE_NETMEM was set but there is no mp bound
- * this helper will return false. See also netif_rxq_has_unreadable_mp().
+ * this helper will return false. An XDP-only mp is unreadable here too,
+ * though netif_rxq_has_unreadable_mp() leaves it out, as its queue needs
+ * no header split.
  *
  * Return: true if memory allocated by the page pool may be unreadable
  */
include/net/page_pool/memory_provider.h +19 -0
index bceb5f7740e2..ae300f9504d0 100644
@@ -19,6 +19,24 @@ struct sk_buff;
  * Per-provider custom logic must be delegated to memory_provider_ops rather
  * than handled directly in page_pool core code.
  */
+
+/**
+ * enum mp_flags - how a memory provider differs from the default
+ * @MP_XDP_ONLY: Only XDP consumes the buffers: they never reach the stack.
+ *	They back the RX page pools of the queue that set
+ *	%PP_FLAG_ALLOW_XDP_ONLY_NETMEM, so header split is not needed, and
+ *	native XDP may run next to them.
+ *	XDP may read them, but userspace may write them at any time,
+ *	so the kernel must keep its own data out of them.
+ *	The core finds the &net_iov_area of such a provider at the start
+ *	of &page_pool.mp_priv, which must point to a struct that begins
+ *	with one. The driver uses the buffers of the queue from one
+ *	context at a time, see %PP_FLAG_ALLOW_XDP_ONLY_NETMEM.
+ */
+enum mp_flags {
+	MP_XDP_ONLY		= BIT(0),
+};
+
 struct memory_provider_ops {
 	netmem_ref (*alloc_netmems)(struct page_pool *pool, gfp_t gfp);
 	bool (*release_netmem)(struct page_pool *pool, netmem_ref netmem);
@@ -27,6 +45,7 @@ struct memory_provider_ops {
 	int (*nl_fill)(void *mp_priv, struct sk_buff *rsp,
 		       struct netdev_rx_queue *rxq);
 	void (*uninstall)(void *mp_priv, struct netdev_rx_queue *rxq);
+	u32 flags;
 };
 
 bool net_mp_niov_set_dma_addr(struct net_iov *niov, dma_addr_t addr);
include/net/page_pool/types.h +21 -2
index 5306e34105eb..c9b9b738e8ba 100644
@@ -31,8 +31,27 @@
  */
 #define PP_FLAG_ALLOW_UNREADABLE_NETMEM BIT(3)
 
+/*
+ * Allow an XDP-only memory provider of the queue (%MP_XDP_ONLY) to back the
+ * pool. The driver registers the pool as the memory model of the RX queue,
+ * sets up XDP buffers with xdp_prepare_buff() at xdp_netmem_address() of
+ * their head, and puts heads back by netmem, not by address.
+ *
+ * Such a provider takes no lock, so the driver also lets one context at a
+ * time use the buffers of the queue: its NAPI, or an operation that runs
+ * only while that NAPI cannot use the rings of the queue, as a queue
+ * restart or an error recovery. It fills and drains the rings only then,
+ * runs the NAPI once after the queue starts, so that its refill can ask
+ * for a wakeup, and lets the buffers leave the driver only to the AF_XDP
+ * socket of the queue, as the core turns XDP_PASS and XDP_TX of them into
+ * drops.
+ */
+#define PP_FLAG_ALLOW_XDP_ONLY_NETMEM BIT(4)
+
 #define PP_FLAG_ALL		(PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV | \
-				 PP_FLAG_SYSTEM_POOL | PP_FLAG_ALLOW_UNREADABLE_NETMEM)
+				 PP_FLAG_SYSTEM_POOL | \
+				 PP_FLAG_ALLOW_UNREADABLE_NETMEM | \
+				 PP_FLAG_ALLOW_XDP_ONLY_NETMEM)
 
 /* Index limit to stay within PP_DMA_INDEX_BITS for DMA indices */
 #define PP_DMA_INDEX_LIMIT XA_LIMIT(1, BIT(PP_DMA_INDEX_BITS) - 1)
@@ -84,7 +103,7 @@ struct pp_alloc_cache {
  * @netdev:	netdev this pool will serve (leave as NULL if none or multiple)
  * @queue_idx:	queue idx this page_pool is being created for.
  * @flags:	PP_FLAG_DMA_MAP, PP_FLAG_DMA_SYNC_DEV, PP_FLAG_SYSTEM_POOL,
- *		PP_FLAG_ALLOW_UNREADABLE_NETMEM.
+ *		PP_FLAG_ALLOW_UNREADABLE_NETMEM, PP_FLAG_ALLOW_XDP_ONLY_NETMEM.
  */
 struct page_pool_params {
 	struct_group_tagged(page_pool_params_fast, fast,
net/core/dev.c +19 -2
index b7757f91fdf9..9ca2bfedcab5 100644
@@ -10666,7 +10666,7 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
 			return -EBUSY;
 		}
 
-		if (dev_get_min_mp_channel_count(dev)) {
+		if (dev_mp_blocks_xdp(dev)) {
 			NL_SET_ERR_MSG(bpf->extack,
 				       "unable to propagate XDP to device using memory provider");
 			return -EBUSY;
@@ -10747,7 +10747,7 @@ static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
 				       "unable to install XDP to device using tcp-data-split");
 			return -EBUSY;
 		}
-		if (dev_get_min_mp_channel_count(dev)) {
+		if (dev_mp_blocks_xdp(dev)) {
 			NL_SET_ERR_MSG(extack,
 				       "unable to install XDP to device using memory provider");
 			return -EBUSY;
@@ -11173,6 +11173,23 @@ int dev_change_xdp_fd(struct net_device *dev, struct netlink_ext_ack *extack,
 	return err;
 }
 
+/* Native XDP runs next to XDP-only providers only, see %MP_XDP_ONLY */
+bool dev_mp_blocks_xdp(const struct net_device *dev)
+{
+	const struct memory_provider_ops *ops;
+	unsigned int i;
+
+	netdev_assert_locked_ops_compat(dev);
+
+	for (i = 0; i < dev->real_num_rx_queues; i++) {
+		ops = dev->_rx[i].mp_params.mp_ops;
+		if (ops && !(ops->flags & MP_XDP_ONLY))
+			return true;
+	}
+
+	return false;
+}
+
 u32 dev_get_min_mp_channel_count(const struct net_device *dev)
 {
 	int i;
net/core/netdev_rx_queue.c +43 -8

86 lines not on this page · raw message

net/core/page_pool.c +13 -3
index d7c88c0b67e5..8a1ea21ccdcd 100644
@@ -272,12 +272,22 @@ static int page_pool_init(struct page_pool *pool,
 
 	xa_init_flags(&pool->dma_mapped, XA_FLAGS_ALLOC1);
 
-	if (pool->slow.flags & PP_FLAG_ALLOW_UNREADABLE_NETMEM) {
+	if (pool->slow.flags & (PP_FLAG_ALLOW_UNREADABLE_NETMEM |
+				PP_FLAG_ALLOW_XDP_ONLY_NETMEM)) {
+		const struct memory_provider_ops *ops;
+		u32 allow;
+
 		netdev_assert_locked(pool->slow.netdev);
 		rxq = __netif_get_rx_queue(pool->slow.netdev,
 					   pool->slow.queue_idx);
-		pool->mp_priv = rxq->mp_params.mp_priv;
-		pool->mp_ops = rxq->mp_params.mp_ops;
+		ops = rxq->mp_params.mp_ops;
+		allow = ops && ops->flags & MP_XDP_ONLY ?
+			PP_FLAG_ALLOW_XDP_ONLY_NETMEM :
+			PP_FLAG_ALLOW_UNREADABLE_NETMEM;
+		if (pool->slow.flags & allow) {
+			pool->mp_priv = rxq->mp_params.mp_priv;
+			pool->mp_ops = ops;
+		}
 	}
 
 	if (pool->mp_ops) {
net/ethtool/rings.c +4 -1
index e3810c0320e3..84ba17412a40 100644
@@ -258,7 +258,10 @@ ethnl_set_rings(struct ethnl_req_info *req_info, struct genl_info *info)
 		return -EINVAL;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
+	/* Providers that take the payload need header split. XDP-only
+	 * providers back whole packets: they neither need it nor block XDP.
+	 */
+	if (dev_mp_blocks_xdp(dev)) {
 		if (kernel_ringparam.tcp_data_split !=
 		    ETHTOOL_TCP_DATA_SPLIT_ENABLED) {
 			NL_SET_ERR_MSG(info->extack,
net/xdp/xsk_buff_pool.c +1 -1
index 28bdc63551bf..d707fb308001 100644
@@ -244,7 +244,7 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
 		goto err_unreg_pool;
 	}
 
-	if (dev_get_min_mp_channel_count(netdev)) {
+	if (dev_mp_blocks_xdp(netdev)) {
 		err = -EBUSY;
 		goto err_unreg_pool;
 	}
Signature · 1 line
2.56.1
#3 lore raw

PATCH v2 3/15 page_pool: Let memory providers take back net_iovs

In reply to Björn Töpel

Currently, a provider learns of the last put of a net_iov only through its release hook, which takes the net_iov out of its page pool. The AF_XDP provider later in this series must see that put, as it returns a chunk to userspace only once the device is done with it too.

Let a provider mark a net_iov so that its last put goes to the provider's new hand_off() instead of the page pool cache. It may decline, never runs in hard IRQ context, and for an XDP-only pool runs in the one context that uses the queue's buffers. The cached net_iovs of a pool that goes away are unmarked and never reach the provider.

Let a provider also count net_iovs off their page pool and back on in bulk, keeping their page pool info, so that net_iovs in userspace pin no pool.

---

Notes (v2changes):
Changes in v2:
- Let a provider count net_iovs that it took back off their page pool, and back on, in bulk (Sashiko).
- Add a helper that tests the mark (Sashiko).
- Say in the message that hand_off() may decline, that it never runs in hard IRQ context, and that for an XDP-only pool it runs in the one context that uses the buffers of the queue (Sashiko).
- Make the mark a u8.

 include/net/netmem.h                    | 14 +++++++
 include/net/page_pool/memory_provider.h |  3 ++
 net/core/page_pool.c                    | 55 ++++++++++++++++++++++++-
 3 files changed, 70 insertions(+), 2 deletions(-)
include/net/netmem.h +14 -0
index 4cf4ee1cd55d..5d3ec8554fb0 100644
@@ -97,6 +97,9 @@ enum net_iov_type {
 struct net_iov {
 	struct netmem_desc desc;
 	enum net_iov_type type;
+	u8 mp_hand_off;
+	/* Owned by the memory provider */
+	u8 mp_state;
 	struct net_iov_area *owner;
 };
 
@@ -132,6 +135,8 @@ static inline void net_iov_init(struct net_iov *niov,
 {
 	niov->owner = owner;
 	niov->type = type;
+	niov->mp_hand_off = 0;
+	niov->mp_state = 0;
 }
 
 /* netmem */
@@ -194,6 +199,15 @@ static inline struct net_iov *netmem_to_net_iov(netmem_ref netmem)
 	return NULL;
 }
 
+/* A provider takes the netmem back at its last put, see
+ * &memory_provider_ops.hand_off. Never true for pages.
+ */
+static inline bool netmem_mp_hand_off(netmem_ref netmem)
+{
+	return netmem_is_net_iov(netmem) &&
+	       netmem_to_net_iov(netmem)->mp_hand_off;
+}
+
 static inline netmem_ref net_iov_to_netmem(struct net_iov *niov)
 {
 	return (__force netmem_ref)((unsigned long)niov | NET_IOV);
include/net/page_pool/memory_provider.h +3 -0
index ae300f9504d0..38bf292b8505 100644
@@ -40,6 +40,7 @@ enum mp_flags {
 struct memory_provider_ops {
 	netmem_ref (*alloc_netmems)(struct page_pool *pool, gfp_t gfp);
 	bool (*release_netmem)(struct page_pool *pool, netmem_ref netmem);
+	bool (*hand_off)(struct page_pool *pool, netmem_ref netmem);
 	int (*init)(struct page_pool *pool);
 	void (*destroy)(struct page_pool *pool);
 	int (*nl_fill)(void *mp_priv, struct sk_buff *rsp,
@@ -51,6 +52,8 @@ struct memory_provider_ops {
 bool net_mp_niov_set_dma_addr(struct net_iov *niov, dma_addr_t addr);
 void net_mp_niov_set_page_pool(struct page_pool *pool, struct net_iov *niov);
 void net_mp_niov_clear_page_pool(struct net_iov *niov);
+void net_mp_release_page_pool(struct page_pool *pool, u32 nr);
+void net_mp_hold_page_pool(struct page_pool *pool, u32 nr);
 
 int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 		      const struct pp_memory_provider_params *p,
net/core/page_pool.c +53 -2

88 lines not on this page · raw message

Signature · 1 line
2.56.1
#4 lore raw

PATCH v2 4/15 xdp: Run programs on XDP-only buffers

In reply to Björn Töpel

The AF_XDP provider needs native XDP to steer packets and for the RX metadata kfuncs. XDP reads its UMEM chunks through the kernel mapping, but userspace may write them at any time. Currently, multi-buffer shared info, XDP_TX and redirect frames, and XDP_PASS skbs live in the buffer.

Run programs on such XDP-only buffers, but keep kernel structures out of them.

The core tells an XDP-only head by its address, with one range check under a static key, so other buffers pay nothing while no provider is bound. Such a buffer keeps its shared info in the provider. Registering such a page pool without its RX queue fails with -EOPNOTSUPP.

The core turns XDP_TX and XDP_PASS of these buffers into XDP_DROP, so drivers need no checks. Frame and skb construction refuse these buffers, so devmap and cpumap drop them.

The kernel takes bounds only from its own fields, and a helper that reads bytes twice must check a copy.

---

Notes (v2changes):
Changes in v2:
- Rename from "xdp: Run programs on buffers that must not hold kernel structures".
- Keep the shared info of XDP-only buffers in the provider, with the queue only pointing to it. In v1 the queue owned it, which breaks for drivers that copy their queue info.
- Take the frame and skb refusals of v1 patch 2. The rest of that patch went to net as a series of its own, which covers classic zero copy too (Maciej). The frame and skb paths of classic zero copy stay as they are.
- XDP_PASS and XDP_TX stay drops in this posting. A follow-up adds them as copies (Maciej).
- Call the buffers XDP-only rather than sink buffers, also in the names of their flag, test and static key.

 include/net/netmem.h          |  9 ++++-
 include/net/page_pool/types.h |  7 ++--
 include/net/xdp.h             | 70 ++++++++++++++++++++++++++++++++++-
 net/core/xdp.c                | 55 +++++++++++++++++++++++++++
 4 files changed, 135 insertions(+), 6 deletions(-)
include/net/netmem.h +8 -1
index 5d3ec8554fb0..9a3994fb12ab 100644
@@ -68,6 +68,7 @@ DECLARE_STATIC_KEY_FALSE(page_pool_mem_providers);
 enum net_iov_type {
 	NET_IOV_DMABUF,
 	NET_IOV_IOURING,
+	NET_IOV_XSK,
 };
 
 /* A memory descriptor representing abstract networking I/O vectors.
@@ -81,7 +82,9 @@ enum net_iov_type {
  * CPU-readable or unreadable. New code must not assume net_iov implies
  * unreadable memory (check readability via netmem_address() or
  * skb_frags_readable() instead) and should, as much as possible, generalize
- * existing limitations to match the design principles.
+ * existing limitations to match the design principles. AF_XDP memory provider
+ * net_iovs (%NET_IOV_XSK) are unreadable for the core and the stack as well:
+ * only XDP reads them, through their XDP view (&net_iov_area.xdp_vaddr).
  *
  * @pp_magic:	pp field, similar to the one in struct page/struct
  *		netmem_desc.
@@ -115,6 +118,10 @@ struct net_iov_area {
 	void *xdp_vaddr;
 	size_t xdp_vsize;
 	unsigned int xdp_shift;
+	/* Shared info of the XDP buffers of an MP_XDP_ONLY provider, in its
+	 * kernel memory, see %XDP_FLAGS_SINFO_EXT
+	 */
+	struct skb_shared_info *xdp_sinfo;
 };
 
 static inline struct net_iov_area *net_iov_owner(const struct net_iov *niov)
include/net/page_pool/types.h +4 -3
index c9b9b738e8ba..4172e9aee28f 100644
@@ -33,9 +33,10 @@
 
 /*
  * Allow an XDP-only memory provider of the queue (%MP_XDP_ONLY) to back the
- * pool. The driver registers the pool as the memory model of the RX queue,
- * sets up XDP buffers with xdp_prepare_buff() at xdp_netmem_address() of
- * their head, and puts heads back by netmem, not by address.
+ * pool. The driver registers the pool as the memory model of the RX queue
+ * with xdp_rxq_info_reg_mem_model(), sets up XDP buffers with
+ * xdp_prepare_buff() at xdp_netmem_address() of their head, and puts heads
+ * back by netmem, not by address.
  *
  * Such a provider takes no lock, so the driver also lets one context at a
  * time use the buffers of the queue: its NAPI, or an operation that runs
include/net/xdp.h +68 -2

126 lines not on this page · raw message

net/core/xdp.c +55 -0

106 lines not on this page · raw message

Signature · 1 line
2.56.1
#5 lore raw

PATCH v2 5/15 xsk: Back a queue's page pools with UMEM chunks

In reply to Björn Töpel

Currently, AF_XDP zero copy fills one chunk per buffer at the UMEM headroom, so drivers carry a second RX path and cannot use their own buffer layout.

Add XDP_UMEM_EXPLICIT_RETURN, under which the device lays out buffers in chunks as in pages and chunks come back explicitly. Back the bound queue's RX page pools with the chunks through an AF_XDP memory provider, so drivers need no AF_XDP code.

A chunk on FILL twice or outside the UMEM is an invalid entry. A chunk counts against the page pool that took it, and against none in userspace. FILL needs no lock, as only the queue's NAPI, or an operation that runs only while that NAPI cannot use the rings, refills.

Binding needs:
- a native XDP program attached through netlink (-ENOENT), which cannot be detached, as drivers would build skbs on the buffers;
- CAP_NET_ADMIN in the initial user namespace (-EPERM), as the provider changes what the program does;
- XDP_USE_NEED_WAKEUP (-EINVAL), as a queue that drained FILL needs a wakeup.

XDP_COPY or XDP_ZEROCOPY fails with -EOPNOTSUPP. Page pools whose ring and cache could hold every chunk fail with -ENOSPC.

Unbind restarts the queue without the provider, or closes the device if that fails, as the old queue may still use the UMEM.

UMEM registration accepts the flag two patches later.

---

Notes (v2changes):
Changes in v2:
- Rename from "xsk: Lend UMEM chunks to the page pools of a queue".
- Rename XDP_UMEM_DRV_LAYOUT to XDP_UMEM_EXPLICIT_RETURN, and the sink to the AF_XDP memory provider in xsk_mp.c, after what changes for the application (Magnus).
- Require XDP_USE_NEED_WAKEUP here, where bind is added, and drop the tests of whether a socket uses it (Magnus).
- Require CAP_NET_ADMIN in the initial user namespace, and fail XDP_ZEROCOPY with -EOPNOTSUPP, as XDP_COPY already did. The check of both now sits next to the checks of the classic modes (Sashiko).
- Charge a FILL chunk to the page pool that takes it, so that a restart while bound no longer leaves the new queue without buffers (Sashiko).
- Refuse page pools with an init callback, as a chunk that a page pool takes back from FILL keeps its page pool info and would miss the callback.
- Add the page-pool netlink attribute xsk, an empty nest that page-pool dumps show for this provider (Sashiko).
- Keep the bind checks apart from those of classic zero copy, as sharing them takes more lines than it saves (Magnus).
- Take FILL without a lock: only the owner of the queue, its NAPI or an operation that runs only while that NAPI cannot use the rings, refills, which the driver agrees to with PP_FLAG_ALLOW_XDP_ONLY_NETMEM.

 Documentation/netlink/specs/netdev.yaml |   8 +
 include/linux/netdevice.h               |   1 +
 include/net/xsk_buff_pool.h             |   3 +
 include/uapi/linux/if_xdp.h             |   8 +
 include/uapi/linux/netdev.h             |   1 +
 net/core/dev.c                          |  32 ++
 net/xdp/Kconfig                         |   1 +
 net/xdp/Makefile                        |   2 +-
 net/xdp/xsk_buff_pool.c                 |  36 +-
 net/xdp/xsk_mp.c                        | 443 ++++++++++++++++++++++++
 net/xdp/xsk_mp.h                        |  14 +
 tools/include/uapi/linux/if_xdp.h       |   8 +
 tools/include/uapi/linux/netdev.h       |   1 +
 13 files changed, 551 insertions(+), 7 deletions(-)
 create mode 100644 net/xdp/xsk_mp.c
 create mode 100644 net/xdp/xsk_mp.h
Documentation/netlink/specs/netdev.yaml +8 -0
index 0de7f93f0c4c..cf07ea7f0727 100644
@@ -206,6 +206,13 @@ attribute-sets:
         doc: io-uring memory provider information.
         type: nest
         nested-attributes: io-uring-provider-info
+      -
+        name: xsk
+        doc: |
+          The page pool takes its buffers from the UMEM of an AF_XDP
+          socket with explicit chunk return.
+        type: nest
+        nested-attributes: xsk-info
   -
     name: page-pool-info
     subset-of: page-pool
@@ -678,6 +685,7 @@ operations:
             - detach-time
             - dmabuf
             - io-uring
+            - xsk
       dump:
         request:
           attributes:
include/linux/netdevice.h +1 -0
index 9e542d793df7..c1fde6420940 100644
@@ -4460,6 +4460,7 @@ u32 dev_xdp_prog_id(struct net_device *dev, enum bpf_xdp_mode mode);
 
 u32 dev_get_min_mp_channel_count(const struct net_device *dev);
 bool dev_mp_blocks_xdp(const struct net_device *dev);
+bool dev_xdp_drv_prog_held(struct net_device *dev);
 
 int __dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
 int dev_forward_skb(struct net_device *dev, struct sk_buff *skb);
include/net/xsk_buff_pool.h +3 -0
index ef4bbf7f66b9..e26162f956c4 100644
@@ -11,6 +11,7 @@
 #include <net/xdp.h>
 
 struct xsk_buff_pool;
+struct xsk_mp;
 struct xdp_rxq_info;
 struct xsk_cb_desc;
 struct xsk_queue;
@@ -54,6 +55,8 @@ struct xsk_buff_pool {
 	spinlock_t xsk_tx_list_lock;
 	refcount_t users;
 	struct xdp_umem *umem;
+	/* RX with explicit return, see net/xdp/xsk_mp.c */
+	struct xsk_mp *mp;
 	struct work_struct work;
 	/* Protects generic receive in shared and non-shared umem mode. */
 	spinlock_t rx_lock;
include/uapi/linux/if_xdp.h +8 -0
index 50d67df78911..60f01759a1d4 100644
@@ -45,6 +45,14 @@
  */
 #define XDP_UMEM_TX_METADATA_LEN	(1 << 2)
 
+/* With explicit return, the kernel and the driver decide where packets
+ * land in a chunk, how many share it and the headroom: userspace only
+ * supplies chunks on FILL. An RX descriptor does not give its chunk
+ * back, and a packet may span chunks. Needs aligned page-sized chunks
+ * and no headroom.
+ */
+#define XDP_UMEM_EXPLICIT_RETURN	(1 << 4)
+
 struct sockaddr_xdp {
 	__u16 sxdp_family;
 	__u16 sxdp_flags;
include/uapi/linux/netdev.h +1 -0
index 9026e0414fbe..0e2b4cdf1ef4 100644
@@ -112,6 +112,7 @@ enum {
 	NETDEV_A_PAGE_POOL_DETACH_TIME,
 	NETDEV_A_PAGE_POOL_DMABUF,
 	NETDEV_A_PAGE_POOL_IO_URING,
+	NETDEV_A_PAGE_POOL_XSK,
 
 	__NETDEV_A_PAGE_POOL_MAX,
 	NETDEV_A_PAGE_POOL_MAX = (__NETDEV_A_PAGE_POOL_MAX - 1)
net/core/dev.c +32 -0
index 9ca2bfedcab5..12e852cc3855 100644
@@ -10813,6 +10813,22 @@ static void dev_xdp_uninstall(struct net_device *dev)
 	dev->xdp_from_upper = 0;
 }
 
+/* A queue of @dev has an XDP-only memory provider */
+static bool dev_has_xdp_only_mp(const struct net_device *dev)
+{
+	const struct memory_provider_ops *ops;
+	unsigned int i;
+
+	netdev_assert_locked_ops(dev);
+	for (i = 0; i < dev->real_num_rx_queues; i++) {
+		ops = dev->_rx[i].mp_params.mp_ops;
+		if (ops && ops->flags & MP_XDP_ONLY)
+			return true;
+	}
+
+	return false;
+}
+
 static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack,
 			  struct bpf_xdp_link *link, struct bpf_prog *new_prog,
 			  struct bpf_prog *old_prog, u32 flags)
@@ -10881,6 +10897,12 @@ static int dev_xdp_attach(struct net_device *dev, struct netlink_ext_ack *extack
 		NL_SET_ERR_MSG(extack, "Active program does not match expected");
 		return -EEXIST;
 	}
+	if (cur_prog && !new_prog && !link && mode == XDP_MODE_DRV &&
+	    dev_has_xdp_only_mp(dev)) {
+		NL_SET_ERR_MSG(extack,
+			       "An XDP-only provider needs the program");
+		return -EBUSY;
+	}
 
 	/* put effective new program into new_prog */
 	if (link)
@@ -11190,6 +11212,16 @@ bool dev_mp_blocks_xdp(const struct net_device *dev)
 	return false;
 }
 
+/* A native program is attached, and dev_xdp_attach() keeps it while an
+ * AF_XDP socket receives with explicit return on @dev. A BPF link could go
+ * without asking, and so could a program that an upper device propagated.
+ */
+bool dev_xdp_drv_prog_held(struct net_device *dev)
+{
+	return dev_xdp_prog_id(dev, XDP_MODE_DRV) &&
+	       !dev_xdp_link(dev, XDP_MODE_DRV) && !dev->xdp_from_upper;
+}
+
 u32 dev_get_min_mp_channel_count(const struct net_device *dev)
 {
 	int i;
net/xdp/Kconfig +1 -0
index 71af2febe72a..c9c68d3b3712 100644
@@ -2,6 +2,7 @@
 config XDP_SOCKETS
 	bool "XDP sockets"
 	depends on BPF_SYSCALL
+	select PAGE_POOL
 	default n
 	help
 	  XDP sockets allows a channel between XDP programs and
net/xdp/Makefile +1 -1
index 30cdc4315f42..add6c7a7dec4 100644
@@ -1,4 +1,4 @@
 # SPDX-License-Identifier: GPL-2.0-only
 obj-$(CONFIG_XDP_SOCKETS) += xsk.o xdp_umem.o xsk_queue.o xskmap.o
-obj-$(CONFIG_XDP_SOCKETS) += xsk_buff_pool.o
+obj-$(CONFIG_XDP_SOCKETS) += xsk_buff_pool.o xsk_mp.o
 obj-$(CONFIG_XDP_SOCKETS_DIAG) += xsk_diag.o
net/xdp/xsk_buff_pool.c +30 -6
index d707fb308001..2cfe069e9959 100644
@@ -7,6 +7,7 @@
 #include <net/xdp_sock_drv.h>
 
 #include "xsk_queue.h"
+#include "xsk_mp.h"
 #include "xdp_umem.h"
 #include "xsk.h"
 
@@ -69,9 +70,14 @@ struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
 	if (!pool)
 		goto out;
 
-	pool->heads = kvzalloc_objs(*pool->heads, umem->chunks);
-	if (!pool->heads)
-		goto out;
+	/* Receiving with explicit return and copy TX take none */
+	if (!(umem->flags & XDP_UMEM_EXPLICIT_RETURN)) {
+		pool->heads = kvzalloc_objs(*pool->heads, umem->chunks);
+		if (!pool->heads)
+			goto out;
+		pool->heads_cnt = umem->chunks;
+		pool->free_heads_cnt = umem->chunks;
+	}
 
 	if (xs->tx)
 		if (xp_alloc_tx_descs(pool, xs, max_segs))
@@ -79,8 +85,6 @@ struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
 
 	pool->chunk_mask = ~((u64)umem->chunk_size - 1);
 	pool->addrs_cnt = umem->size;
-	pool->heads_cnt = umem->chunks;
-	pool->free_heads_cnt = umem->chunks;
 	pool->headroom = umem->headroom;
 	pool->chunk_size = umem->chunk_size;
 	pool->chunk_shift = ffs(umem->chunk_size) - 1;
@@ -189,6 +193,12 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
 	if (force_zc && force_copy)
 		return -EINVAL;
 
+	/* Explicit return lets the kernel place the packets, which is
+	 * neither of the classic modes
+	 */
+	if (umem_flags & XDP_UMEM_EXPLICIT_RETURN && (force_zc || force_copy))
+		return -EOPNOTSUPP;
+
 	if (pool->tx_sw_csum && (netdev->priv_flags & IFF_TX_SKB_NO_LINEAR))
 		return -EOPNOTSUPP;
 
@@ -219,6 +229,13 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
 	 */
 	hlist_add_head(&pool->dev_node, &netdev->xsk_pools);
 
+	if (pool->umem->flags & XDP_UMEM_EXPLICIT_RETURN) {
+		err = xsk_mp_bind(pool, netdev, queue_id, flags);
+		if (err)
+			goto err_unbind;
+		return 0;
+	}
+
 	if (force_copy)
 		/* For copy-mode, we are done. */
 		return 0;
@@ -271,6 +288,7 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
 err_unreg_pool:
 	if (!force_zc)
 		err = 0; /* fallback to copy mode */
+err_unbind:
 	if (err) {
 		pool->umem->flags = umem_flags;
 		xsk_clear_pool_at_qid(netdev, queue_id);
@@ -288,6 +306,9 @@ int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs,
 	struct xdp_umem *umem = umem_xs->umem;
 
 	flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
+	/* Explicit return has no copy mode */
+	if (umem->flags & XDP_UMEM_EXPLICIT_RETURN)
+		flags = 0;
 
 	if (umem->flags & XDP_UMEM_SG_FLAG)
 		flags |= XDP_USE_SG;
@@ -303,7 +324,10 @@ static void __xp_clear_dev(struct xsk_buff_pool *pool)
 	struct net_device *netdev = pool->netdev;
 
 	netdev_assert_locked_ops_compat(netdev);
-	xp_disable_drv_zc(pool);
+	if (pool->mp)
+		xsk_mp_unbind(pool);
+	else
+		xp_disable_drv_zc(pool);
 	xsk_clear_pool_at_qid(netdev, pool->queue_id);
 	hlist_del(&pool->dev_node);
 	pool->netdev = NULL;
net/xdp/xsk_mp.c new file +443 -0

443 lines not on this page · raw message

net/xdp/xsk_mp.h new file +14 -0
new file mode 100644
index 000000000000..e6c965bd3b2c
@@ -0,0 +1,14 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/* AF_XDP RX with explicit return, see xsk_mp.c */
+
+#ifndef XSK_MP_H_
+#define XSK_MP_H_
+
+struct net_device;
+struct xsk_buff_pool;
+
+int xsk_mp_bind(struct xsk_buff_pool *pool, struct net_device *dev,
+		u16 queue_id, u16 flags);
+void xsk_mp_unbind(struct xsk_buff_pool *pool);
+
+#endif /* XSK_MP_H_ */
tools/include/uapi/linux/if_xdp.h +8 -0
index 50d67df78911..60f01759a1d4 100644
@@ -45,6 +45,14 @@
  */
 #define XDP_UMEM_TX_METADATA_LEN	(1 << 2)
 
+/* With explicit return, the kernel and the driver decide where packets
+ * land in a chunk, how many share it and the headroom: userspace only
+ * supplies chunks on FILL. An RX descriptor does not give its chunk
+ * back, and a packet may span chunks. Needs aligned page-sized chunks
+ * and no headroom.
+ */
+#define XDP_UMEM_EXPLICIT_RETURN	(1 << 4)
+
 struct sockaddr_xdp {
 	__u16 sxdp_family;
 	__u16 sxdp_flags;
tools/include/uapi/linux/netdev.h +1 -0
index 9026e0414fbe..0e2b4cdf1ef4 100644
@@ -112,6 +112,7 @@ enum {
 	NETDEV_A_PAGE_POOL_DETACH_TIME,
 	NETDEV_A_PAGE_POOL_DMABUF,
 	NETDEV_A_PAGE_POOL_IO_URING,
+	NETDEV_A_PAGE_POOL_XSK,
 
 	__NETDEV_A_PAGE_POOL_MAX,
 	NETDEV_A_PAGE_POOL_MAX = (__NETDEV_A_PAGE_POOL_MAX - 1)
Signature · 1 line
2.56.1
#6 lore raw

PATCH v2 6/15 xsk: Deliver packets and return chunks explicitly

In reply to Björn Töpel

Once a chunk holds several buffers, an RX descriptor can no longer give its chunk back.

Deliver redirected XDP-only buffers to the queue's receiving socket, the first bound socket with an RX ring.

Each buffer becomes one RX descriptor where the device put it, chained with XDP_PKT_CONTD, with the program's metadata in front of the head. An empty head becomes a zero-length descriptor only if it carries metadata. The socket drops and counts packets that do not fit its UMEM, XDP_USE_SG setting or RX ring. Redirects, traced by the xdp_redirect_map tracepoints, fail with -EOPNOTSUPP outside an XSKMAP and with -EINVAL to another socket.

Once the kernel is done with a chunk whose buffer reached the socket, the new XDP_RX_CHUNK_RETURNED marks the descriptor of the buffer that freed it, or an empty descriptor if the device let go later. The RX ring keeps a slot for each chunk owed, so returns never wait, and packets drop that much earlier under overload. Returned chunks count against no page pool, so they pin none after a restart.

The provider takes no lock, as the socket drops its references in the queue's NAPI. So the NAPI, or an operation that runs only while that NAPI cannot use the rings, makes every last put of a chunk and every RX ring write.

---

Notes (v2changes):
Changes in v2:
- Rename from "xsk: Deliver to the socket in the device's buffer layout".
- Rename XDP_RX_CHUNK_FREE to XDP_RX_CHUNK_RETURNED (Magnus).
- Deliver an empty head with metadata as a zero-length first descriptor with XDP_PKT_CONTD. v1 dropped such packets (Sashiko).
- Say in the uAPI comment of XDP_PKT_CONTD that single-buffer packets never set it, as the options of their descriptor may carry XDP_RX_CHUNK_RETURNED.
- Trace redirects and their errors in the xdp_redirect_map tracepoints, at no cost while they are off (Sashiko).
- Stop counting a chunk that goes back to userspace against its page pool, once per poll, so that chunks that userspace keeps hold no old page pool alive after a restart (Sashiko).
- A hand-off writes the return descriptor itself, and the RX ring keeps room for the chunks that the device still holds, replacing the list that the next poll posted (Sashiko).

 include/net/xdp_sock.h            |   8 +
 include/uapi/linux/if_xdp.h       |  21 +-
 net/core/filter.c                 |  38 ++++
 net/xdp/xsk.c                     |  28 ++-
 net/xdp/xsk_mp.c                  | 365 +++++++++++++++++++++++++++++-
 net/xdp/xsk_mp.h                  |   5 +
 tools/include/uapi/linux/if_xdp.h |  21 +-
 7 files changed, 463 insertions(+), 23 deletions(-)
include/net/xdp_sock.h +8 -0
index 6e70b320b399..f56cacd5c343 100644
@@ -126,6 +126,8 @@ struct xsk_tx_metadata_ops {
 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp);
 int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp);
 void __xsk_map_flush(struct list_head *flush_list);
+int xsk_mp_redirect(struct xdp_buff *xdp, enum bpf_map_type map_type,
+		    void *fwd);
 INDIRECT_CALLABLE_DECLARE(void xsk_destruct_skb(struct sk_buff *));
 
 /**
@@ -182,6 +184,12 @@ static inline int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
 	return -ENOTSUPP;
 }
 
+static inline int xsk_mp_redirect(struct xdp_buff *xdp,
+				  enum bpf_map_type map_type, void *fwd)
+{
+	return -EOPNOTSUPP;
+}
+
 static inline int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
 {
 	return -EOPNOTSUPP;
include/uapi/linux/if_xdp.h +16 -5
index 60f01759a1d4..a319e8d83a97 100644
@@ -47,9 +47,12 @@
 
 /* With explicit return, the kernel and the driver decide where packets
  * land in a chunk, how many share it and the headroom: userspace only
- * supplies chunks on FILL. An RX descriptor does not give its chunk
- * back, and a packet may span chunks. Needs aligned page-sized chunks
- * and no headroom.
+ * supplies chunks on FILL, and a packet may span chunks. Receiving a
+ * buffer does not give its chunk back. A chunk is userspace's again
+ * once an RX descriptor carries XDP_RX_CHUNK_RETURNED for it, or,
+ * without a descriptor, when the receiving socket closes or the UMEM
+ * stops backing the queue. Needs aligned page-sized chunks and no
+ * headroom.
  */
 #define XDP_UMEM_EXPLICIT_RETURN	(1 << 4)
 
@@ -182,12 +185,20 @@ struct xdp_desc {
 
 /* Flag indicating that the packet continues with the buffer pointed out by the
  * next frame in the ring. The end of the packet is signalled by setting this
- * bit to zero. For single buffer packets, every descriptor has 'options' set
- * to 0 and this maintains backward compatibility.
+ * bit to zero. Single buffer packets never set it, and this maintains
+ * backward compatibility.
  */
 #define XDP_PKT_CONTD (1 << 0)
 
 /* TX packet carries valid metadata. */
 #define XDP_TX_METADATA (1 << 1)
 
+/* Flag on an RX descriptor of an XDP_UMEM_EXPLICIT_RETURN UMEM: the
+ * kernel is done with the UMEM chunk of the descriptor, and puts nothing
+ * more in it. The chunk is userspace's again. An empty descriptor at the
+ * chunk carries only this flag, when the device let go of the chunk after
+ * its last buffer.
+ */
+#define XDP_RX_CHUNK_RETURNED (1 << 2)
+
 #endif /* _UAPI_LINUX_IF_XDP_H */
net/core/filter.c +38 -0
index 90e4bf78c689..71a4f93c2b20 100644
@@ -4770,12 +4770,50 @@ __xdp_do_redirect_frame(struct bpf_redirect_info *ri, struct net_device *dev,
 	return err;
 }
 
+static noinline int xdp_redirect_xdp_only_traced(const struct net_device *dev,
+						 struct xdp_buff *xdp,
+						 const struct bpf_prog *prog,
+						 struct bpf_redirect_info *ri)
+{
+	enum bpf_map_type map_type = ri->map_type;
+	void *fwd = ri->tgt_value;
+	u32 index = ri->tgt_index;
+	u32 map_id = ri->map_id;
+	int err;
+
+	ri->map_id = 0;
+	ri->map_type = BPF_MAP_TYPE_UNSPEC;
+	err = xsk_mp_redirect(xdp, map_type, fwd);
+	if (err)
+		_trace_xdp_redirect_map_err(dev, prog, fwd, map_type, map_id,
+					    index, err);
+	else
+		_trace_xdp_redirect_map(dev, prog, fwd, map_type, map_id,
+					index);
+	return err;
+}
+
 int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
 		    const struct bpf_prog *xdp_prog)
 {
 	struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
 	enum bpf_map_type map_type = ri->map_type;
 
+	if (static_branch_unlikely(&xdp_only_mp_key) &&
+	    xdp_buff_is_xdp_only(xdp)) {
+		void *fwd;
+
+		if (trace_xdp_redirect_enabled() ||
+		    trace_xdp_redirect_err_enabled())
+			return xdp_redirect_xdp_only_traced(dev, xdp, xdp_prog,
+							    ri);
+
+		fwd = ri->tgt_value;
+		ri->map_id = 0;
+		ri->map_type = BPF_MAP_TYPE_UNSPEC;
+		return xsk_mp_redirect(xdp, map_type, fwd);
+	}
+
 	if (map_type == BPF_MAP_TYPE_XSKMAP)
 		return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
 
net/xdp/xsk.c +27 -1
index 48a835e53518..482bb2cd94bf 100644
@@ -35,6 +35,7 @@
 #include "../core/dev.h"
 
 #include "xsk_queue.h"
+#include "xsk_mp.h"
 #include "xdp_umem.h"
 #include "xsk.h"
 
@@ -437,6 +438,12 @@ static bool xsk_dev_queue_valid(const struct xdp_sock *xs,
 	return false;
 }
 
+/* The pool receives with explicit return, or did */
+static bool xsk_explicit_return(const struct xdp_sock *xs)
+{
+	return xs->pool->umem->flags & XDP_UMEM_EXPLICIT_RETURN;
+}
+
 static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
 {
 	if (!xsk_is_bound(xs))
@@ -454,8 +461,14 @@ static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
 
 static void xsk_flush(struct xdp_sock *xs)
 {
+	struct xsk_mp *mp = xs->pool->mp;
+
+	if (mp)
+		xsk_mp_flush(mp);
 	xskq_prod_submit(xs->rx);
-	__xskq_cons_release(xs->pool->fq);
+	/* The provider releases FILL as it takes it, see xsk_mp_take_fill() */
+	if (!mp)
+		__xskq_cons_release(xs->pool->fq);
 	sock_def_readable(&xs->sk);
 }
 
@@ -465,6 +478,11 @@ int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
 	int err;
 
 	err = xsk_rcv_check(xs, xdp, len);
+	/* A socket with explicit return takes buffers of its provider only */
+	if (!err && xsk_explicit_return(xs)) {
+		xs->rx_dropped++;
+		err = -EOPNOTSUPP;
+	}
 	if (!err) {
 		spin_lock_bh(&xs->pool->rx_lock);
 		err = __xsk_rcv(xs, xdp, len);
@@ -489,6 +507,14 @@ static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
 		return xsk_rcv_zc(xs, xdp, len);
 	}
 
+	/* XDP-only buffers take xsk_mp_redirect(); others cannot go to a
+	 * socket with explicit return
+	 */
+	if (xsk_explicit_return(xs)) {
+		xs->rx_dropped++;
+		return -EOPNOTSUPP;
+	}
+
 	err = __xsk_rcv(xs, xdp, len);
 	if (!err)
 		xdp_return_buff(xdp);
net/xdp/xsk_mp.c +353 -12

422 lines not on this page · raw message

net/xdp/xsk_mp.h +5 -0
index e6c965bd3b2c..46d73fdb40a1 100644
@@ -5,10 +5,15 @@
 #define XSK_MP_H_
 
 struct net_device;
+struct xdp_sock;
 struct xsk_buff_pool;
+struct xsk_mp;
 
 int xsk_mp_bind(struct xsk_buff_pool *pool, struct net_device *dev,
 		u16 queue_id, u16 flags);
 void xsk_mp_unbind(struct xsk_buff_pool *pool);
+void xsk_mp_flush(struct xsk_mp *mp);
+void xsk_mp_add_sock(struct xsk_mp *mp, struct xdp_sock *xs);
+struct xdp_sock *xsk_mp_sock(struct xsk_mp *mp);
 
 #endif /* XSK_MP_H_ */
tools/include/uapi/linux/if_xdp.h +16 -5
index 60f01759a1d4..a319e8d83a97 100644
@@ -47,9 +47,12 @@
 
 /* With explicit return, the kernel and the driver decide where packets
  * land in a chunk, how many share it and the headroom: userspace only
- * supplies chunks on FILL. An RX descriptor does not give its chunk
- * back, and a packet may span chunks. Needs aligned page-sized chunks
- * and no headroom.
+ * supplies chunks on FILL, and a packet may span chunks. Receiving a
+ * buffer does not give its chunk back. A chunk is userspace's again
+ * once an RX descriptor carries XDP_RX_CHUNK_RETURNED for it, or,
+ * without a descriptor, when the receiving socket closes or the UMEM
+ * stops backing the queue. Needs aligned page-sized chunks and no
+ * headroom.
  */
 #define XDP_UMEM_EXPLICIT_RETURN	(1 << 4)
 
@@ -182,12 +185,20 @@ struct xdp_desc {
 
 /* Flag indicating that the packet continues with the buffer pointed out by the
  * next frame in the ring. The end of the packet is signalled by setting this
- * bit to zero. For single buffer packets, every descriptor has 'options' set
- * to 0 and this maintains backward compatibility.
+ * bit to zero. Single buffer packets never set it, and this maintains
+ * backward compatibility.
  */
 #define XDP_PKT_CONTD (1 << 0)
 
 /* TX packet carries valid metadata. */
 #define XDP_TX_METADATA (1 << 1)
 
+/* Flag on an RX descriptor of an XDP_UMEM_EXPLICIT_RETURN UMEM: the
+ * kernel is done with the UMEM chunk of the descriptor, and puts nothing
+ * more in it. The chunk is userspace's again. An empty descriptor at the
+ * chunk carries only this flag, when the device let go of the chunk after
+ * its last buffer.
+ */
+#define XDP_RX_CHUNK_RETURNED (1 << 2)
+
 #endif /* _UAPI_LINUX_IF_XDP_H */
Signature · 1 line
2.56.1
#7 lore raw

PATCH v2 7/15 xsk: Receive with explicit chunk return

In reply to Björn Töpel

UMEM registration still refuses XDP_UMEM_EXPLICIT_RETURN.

Accept it with aligned page-sized chunks and no headroom, and install the provider when a socket with such a UMEM binds. A bind that cannot install it fails rather than fall back.

Only the first socket with an RX ring receives, and later ones get -EBUSY until the provider goes, so the NAPI never sees the receiver replaced. Sockets sharing the UMEM may transmit, in copy mode.

The provider goes with the queue's last socket, from the freezable buffer pool teardown, so unbind never restarts the queue of a suspended device. Until then, a queue without a receiver drops what it redirects.

Returns need no wakeup, so recvmsg and poll wake the NAPI only for an empty FILL ring. A wakeup fails with -ENETDOWN once the provider or NAPI is gone.

Once its fd is closed and its rings unmapped, the receiving socket waits out the NAPI polls and hand-offs that may write to its RX ring. Later hand-offs return chunks without descriptors.

---

Notes (v2changes):
Changes in v2:
- Rename from "xsk: Receive in the device's buffer layout".
- Move the XDP_USE_NEED_WAKEUP requirement to patch 5 (Magnus).
- Let the provider go with the last socket of the queue, from the deferred teardown of the buffer pool on a freezable workqueue, so that unbind never restarts the queue of a suspended device. v1 let it go with the receiving socket, but a queue that only TX-only sockets used kept it until its pool went (Sashiko). A program detach right after the last close may now fail with EBUSY for a short time.
- Hand-offs and wakeups follow the single owner of a queue, see patch 6; need_wakeup reports only an empty FILL ring. Read the NAPI of the queue under RCU, and mark its writes (Sashiko).
- Once the receiving socket closes, give the chunks that the kernel owes it back to the application without descriptors, and drop what the queue redirects until the provider goes (Sashiko).

 include/net/netdev_rx_queue.h |  5 +++
 net/core/dev.c                |  3 +-
 net/xdp/xdp_umem.c            |  5 +++
 net/xdp/xsk.c                 | 82 ++++++++++++++++++++++++++++-----
 net/xdp/xsk_mp.c              | 85 ++++++++++++++++++++++++++++++++---
 net/xdp/xsk_mp.h              |  4 +-
 6 files changed, 163 insertions(+), 21 deletions(-)
include/net/netdev_rx_queue.h +5 -0
index 9415a94d333d..c5cc9d10f0c2 100644
@@ -28,6 +28,11 @@ struct netdev_rx_queue {
 #ifdef CONFIG_XDP_SOCKETS
 	struct xsk_buff_pool            *pool;
 #endif
+	/* Readers without the instance lock, such as AF_XDP wakeups, use
+	 * READ_ONCE() under RCU. Drivers clear it before netif_napi_del(),
+	 * and wait for a grace period before they free the NAPI, as they
+	 * already must for napi_by_id(), so the NAPI stays valid for them.
+	 */
 	struct napi_struct		*napi;
 	struct netdev_queue_config	qcfg;
 	struct pp_memory_provider_params mp_params;
net/core/dev.c +2 -1
index 12e852cc3855..42ed8e2514d2 100644
@@ -7631,7 +7631,8 @@ void netif_queue_set_napi(struct net_device *dev, unsigned int queue_index,
 	switch (type) {
 	case NETDEV_QUEUE_TYPE_RX:
 		rxq = __netif_get_rx_queue(dev, queue_index);
-		rxq->napi = napi;
+		/* See &netdev_rx_queue.napi */
+		WRITE_ONCE(rxq->napi, napi);
 		return;
 	case NETDEV_QUEUE_TYPE_TX:
 		txq = netdev_get_tx_queue(dev, queue_index);
net/xdp/xdp_umem.c +5 -0
index de8a2747ac9f..84caa001ff6a 100644
@@ -145,6 +145,7 @@ static int xdp_umem_account_pages(struct xdp_umem *umem)
 		XDP_UMEM_UNALIGNED_CHUNK_FLAG | \
 		XDP_UMEM_TX_SW_CSUM | \
 		XDP_UMEM_TX_METADATA_LEN | \
+		XDP_UMEM_EXPLICIT_RETURN | \
 	0)
 
 static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
@@ -198,6 +199,10 @@ static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr)
 	if (headroom > chunk_size - XDP_PACKET_HEADROOM - 128)
 		return -EINVAL;
 
+	if (mr->flags & XDP_UMEM_EXPLICIT_RETURN &&
+	    (unaligned_chunks || chunk_size != PAGE_SIZE || headroom))
+		return -EINVAL;
+
 	if (mr->flags & XDP_UMEM_TX_METADATA_LEN) {
 		if (mr->tx_metadata_len >= 256 || mr->tx_metadata_len % 8)
 			return -EINVAL;
net/xdp/xsk.c +70 -12

164 lines not on this page · raw message

net/xdp/xsk_mp.c +78 -7

127 lines not on this page · raw message

net/xdp/xsk_mp.h +3 -1
index 46d73fdb40a1..b2e3909f282f 100644
@@ -12,8 +12,10 @@ struct xsk_mp;
 int xsk_mp_bind(struct xsk_buff_pool *pool, struct net_device *dev,
 		u16 queue_id, u16 flags);
 void xsk_mp_unbind(struct xsk_buff_pool *pool);
+int xsk_mp_wakeup(struct xsk_mp *mp);
 void xsk_mp_flush(struct xsk_mp *mp);
 void xsk_mp_add_sock(struct xsk_mp *mp, struct xdp_sock *xs);
-struct xdp_sock *xsk_mp_sock(struct xsk_mp *mp);
+void xsk_mp_del_sock(struct xsk_mp *mp, struct xdp_sock *xs);
+bool xsk_mp_rx_taken(struct xsk_mp *mp);
 
 #endif /* XSK_MP_H_ */
Signature · 1 line
2.56.1
#8 lore raw

PATCH v2 8/15 xsk: Drop references of a run of buffers at once

In reply to Björn Töpel

Currently, the AF_XDP provider drops the page pool reference of a buffer inside the redirect, before the driver drops its own. A driver that lets go of a page after its last buffer still holds the chunk then, so the chunk returns later, on an extra empty descriptor. A return in the middle of a poll also shows the RX ring and wakes the reader.

Drop the references of a run of buffers from one chunk together, when a buffer comes from another chunk or at the NAPI flush. A chunk that this frees returns on the descriptor of the last buffer of the run. A return while a run is pending ends the run first. Returns wait for the flush of a poll that delivered to the socket, and go out at once otherwise.

Only the queue NAPI delivers, so a run needs no lock and never outlives its poll. A receiver that goes during the poll still gets its run dropped before its RX ring goes.

---

Notes (v2changes):
Changes in v2:
- New in v2, reworked from "xsk: Drop the references of a run of buffers at once" of the v1 full series. The socket dropped its reference to a buffer inside the redirect, before the driver was done with the page, so a chunk that the driver lets go of after its last buffer still came back on an extra empty descriptor. A return while a run is pending ends the run first, so the run's last descriptor is the last one reserved, and returns of a poll that delivered to the socket wait for its flush.

 net/xdp/xsk.c    |  2 +-
 net/xdp/xsk_mp.c | 81 +++++++++++++++++++++++++++++++++++++-----------
 net/xdp/xsk_mp.h |  2 +-
 3 files changed, 65 insertions(+), 20 deletions(-)
net/xdp/xsk.c +1 -1
index 33d4cbee658d..2e37f3b2f82f 100644
@@ -468,7 +468,7 @@ static void xsk_flush(struct xdp_sock *xs)
 	struct xsk_mp *mp = xs->pool->mp;
 
 	if (mp)
-		xsk_mp_flush(mp);
+		xsk_mp_flush(mp, xs);
 	xskq_prod_submit(xs->rx);
 	/* The provider releases FILL as it takes it, see xsk_mp_take_fill() */
 	if (!mp)
net/xdp/xsk_mp.c +63 -18

157 lines not on this page · raw message

net/xdp/xsk_mp.h +1 -1
index b2e3909f282f..3a83b615396b 100644
@@ -13,7 +13,7 @@ int xsk_mp_bind(struct xsk_buff_pool *pool, struct net_device *dev,
 		u16 queue_id, u16 flags);
 void xsk_mp_unbind(struct xsk_buff_pool *pool);
 int xsk_mp_wakeup(struct xsk_mp *mp);
-void xsk_mp_flush(struct xsk_mp *mp);
+void xsk_mp_flush(struct xsk_mp *mp, struct xdp_sock *xs);
 void xsk_mp_add_sock(struct xsk_mp *mp, struct xdp_sock *xs);
 void xsk_mp_del_sock(struct xsk_mp *mp, struct xdp_sock *xs);
 bool xsk_mp_rx_taken(struct xsk_mp *mp);
Signature · 1 line
2.56.1
#9 lore raw

PATCH v2 9/15 eth: fbnic: Receive into XDP-only memory providers

In reply to Björn Töpel

fbnic still handles RX heads by page address, so an XDP-only provider cannot back its RX pool.

Let the header page pool take an XDP-only provider, and handle heads as netmem. The payload ring shares that pool, so both rings fill from the provider, and header-split packets reach the program as multi-buffer packets. Skip the prefetch of XDP-only heads, which may cost a page walk.

Size the page pool of such a queue in the pages that the rings may post, as the provider checks that size against its chunks. A device that is down creates no page pool on ring changes, so refuse to grow its BDQs with -EBUSY while a queue has such a provider, rather than fail the next open. On a running device, the provider checks the new pool first.

---

Notes (v2changes):
Changes in v2:
- v1 patch 9, "eth: fbnic: Support XDP redirect", was posted to net-next on its own and is a dependency.
- Refuse to grow the BDQs while the device is down and a queue has such a provider, as nothing checks them before the next open, and size the page pool for the provider in the pages that the BDQs post (Sashiko).
- The message now says that the payload ring shares the header page pool, so both rings fill from the provider.

 .../net/ethernet/meta/fbnic/fbnic_ethtool.c   | 22 ++++++++++
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.c  | 41 +++++++++++--------
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.h  |  8 ++++
 3 files changed, 54 insertions(+), 17 deletions(-)
drivers/net/ethernet/meta/fbnic/fbnic_ethtool.c +22 -0
index 9714a89977fa..3355aadeb8ff 100644
@@ -6,6 +6,7 @@
 #include <linux/netdevice.h>
 #include <linux/pci.h>
 #include <net/ipv6.h>
+#include <net/netdev_queues.h>
 
 #include "fbnic.h"
 #include "fbnic_netdev.h"
@@ -341,6 +342,16 @@ static void fbnic_set_rings(struct fbnic_net *fbn,
 	fbn->hds_thresh = kernel_ring->hds_thresh;
 }
 
+static bool fbnic_has_xdp_only_mp(struct net_device *netdev)
+{
+	unsigned int i;
+
+	for (i = 0; i < netdev->real_num_rx_queues; i++)
+		if (netif_rxq_has_xdp_only_mp(netdev, i))
+			return true;
+	return false;
+}
+
 static int
 fbnic_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
 		    struct kernel_ethtool_ringparam *kernel_ring,
@@ -386,6 +397,17 @@ fbnic_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
 	}
 
 	if (!netif_running(netdev)) {
+		/* No page pool lets the provider check larger rings before
+		 * the device opens, which would then fail
+		 */
+		if (fbnic_bdq_pages(ring->rx_mini_pending,
+				    ring->rx_jumbo_pending) >
+		    fbnic_bdq_pages(fbn->hpq_size, fbn->ppq_size) &&
+		    fbnic_has_xdp_only_mp(netdev)) {
+			NL_SET_ERR_MSG_MOD(extack,
+					   "Cannot grow the BDQs while the device is down and a queue has an XDP-only memory provider");
+			return -EBUSY;
+		}
 		fbnic_set_rings(fbn, ring, kernel_ring);
 		return 0;
 	}
drivers/net/ethernet/meta/fbnic/fbnic_txrx.c +24 -17

98 lines not on this page · raw message

drivers/net/ethernet/meta/fbnic/fbnic_txrx.h +8 -0
index a61ac2df7c03..03b668ff8a42 100644
@@ -81,6 +81,7 @@ struct fbnic_net;
 
 struct fbnic_pkt_buff {
 	struct xdp_buff buff;
+	netmem_ref head;
 	ktime_t hwtstamp;
 	bool add_frag_failed;
 };
@@ -185,6 +186,13 @@ static inline u16 fbnic_bd_page_count(const struct fbnic_ring *bdq)
 	return 1U << bdq->bd_page_shift;
 }
 
+static inline u32 fbnic_bdq_pages(u32 hpq_size, u32 ppq_size)
+{
+	u32 shift = ilog2(PAGE_SIZE / FBNIC_BD_PAGE_SIZE);
+
+	return (hpq_size >> shift) + (ppq_size >> shift);
+}
+
 netdev_tx_t fbnic_xmit_frame(struct sk_buff *skb, struct net_device *dev);
 netdev_features_t
 fbnic_features_check(struct sk_buff *skb, struct net_device *dev,
Signature · 1 line
2.56.1
Reviewed-by on v2 9/15
#10 lore raw

Re: [PATCH net-next v2 09/15] eth: fbnic: Receive into XDP-only memory providers

Quoted 2 lines · Björn Töpel

fbnic still handles RX heads by page address, so an XDP-only provider cannot back its RX pool.

[...]

drivers/net/ethernet/meta/fbnic/fbnic_ethtool.c static void fbnic_set_rings(struct fbnic_net *fbn, · line 353

Quoted 9 lines
drivers/net/ethernet/meta/fbnic/fbnic_ethtool.c +9 -0
+static bool fbnic_has_xdp_only_mp(struct net_device *netdev)
+{
+     unsigned int i;
+
+     for (i = 0; i < netdev->real_num_rx_queues; i++)
+             if (netif_rxq_has_xdp_only_mp(netdev, i))
+                     return true;
+     return false;
+}

Not a blocker: net/core/dev.c already has this loop as the static dev_has_xdp_only_mp function. Exporting it next to the netif_rxq_has_xdp_only_mp function would save drivers a copy.

The rest looks good to me. Heads go back by netmem through pkt->head. The rings are filled and drained only while the NAPI is disabled, and the FBNIC_INTR_SET write in the open and restart paths runs the NAPI once after the queue starts, as PP_FLAG_ALLOW_XDP_ONLY_NETMEM asks. The fbnic_pkt_tx function still finds the head with virt_to_page, which is safe only because the core turns XDP_TX of XDP-only buffers into XDP_DROP.

#11 lore raw

PATCH v2 10/15 net/mlx5e: Handle restarts with XDP-only providers

In reply to Björn Töpel

An XDP-only provider backs an RQ with UMEM chunks that come back only through that queue. Currently, mlx5 assumes that an RQ can always get more pages, which does not hold across a channel restart.

While a queue has such a provider:
- skip its RQ, as the XSK RQ already is, in the wait for posted WQEs on activation. FILL may stay empty, so the wait would time out and the health reporter could turn off the ICOSQ;
- close the old channels of a switch before the new ones run, as the old RQs hold the buffers that the new ones need, and their last puts must not overlap the new NAPIs.

A channel switch, as for an MTU change, then stops traffic on every channel while it runs, as without tis_tir_td_order.

Nothing installs such a provider on mlx5 before the next patch.

---

Notes (v2changes):
Changes in v2:
- Split out of v1 patch 12, which skipped the wait for the minimum of posted WQEs.
- Close the old channels of a switch before the new ones run (Sashiko).
- The message says that every channel stops while a switch runs, and that closing the old channels first also keeps the last puts of the old buffers off the NAPIs of the new ones.

 .../net/ethernet/mellanox/mlx5/core/en_main.c | 36 ++++++++++++++++---
 1 file changed, 32 insertions(+), 4 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_main.c +32 -4
index 54f4f5a097c8..a76ba297c9a9 100644
@@ -3102,6 +3102,14 @@ static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs)
 		if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state))
 			continue;
 
+		/* An RQ that an XDP-only provider backs fills from the FILL
+		 * ring of its socket, which stays empty until the socket
+		 * gets its chunks back, so a queue that restarts under a
+		 * slow reader would never meet the bound.
+		 */
+		if (netif_rxq_has_xdp_only_mp(c->netdev, c->ix))
+			continue;
+
 		err |= mlx5e_wait_for_min_rx_wqes(&c->rq, timeout);
 
 		/* Don't wait on the XSK RQ, because the newer xdpsock sample
@@ -3528,11 +3536,29 @@ static void mlx5e_close_channels_xsk(struct mlx5e_channels *chs)
 	}
 }
 
+static bool mlx5e_has_xdp_only_mp(struct mlx5e_priv *priv, unsigned int n)
+{
+	unsigned int i;
+
+	for (i = 0; i < n; i++)
+		if (netif_rxq_has_xdp_only_mp(priv->netdev, i))
+			return true;
+	return false;
+}
+
+static bool mlx5e_close_old_first(struct mlx5e_priv *priv,
+				  struct mlx5e_channels *chs)
+{
+	if (mlx5e_has_xdp_only_mp(priv, priv->netdev->real_num_rx_queues))
+		return true;
+	return !MLX5_CAP_GEN(priv->mdev, tis_tir_td_order);
+}
+
 static int mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
 				      struct mlx5e_channels *old_chs,
 				      struct mlx5e_channels *new_chs,
 				      mlx5e_fp_preactivate preactivate,
-				      void *context)
+				      void *context, bool old_first)
 {
 	struct net_device *netdev = priv->netdev;
 	int carrier_ok;
@@ -3557,7 +3583,7 @@ static int mlx5e_switch_priv_channels(struct mlx5e_priv *priv,
 	}
 
 	mlx5e_set_xdp_feature(priv);
-	if (!MLX5_CAP_GEN(priv->mdev, tis_tir_td_order))
+	if (old_first)
 		mlx5e_close_channels(old_chs);
 	else
 		mlx5e_close_channels_xsk(old_chs);
@@ -3580,6 +3606,7 @@ int mlx5e_safe_switch_params(struct mlx5e_priv *priv,
 			     void *context, bool reset)
 {
 	struct mlx5e_channels *old_chs, *new_chs;
+	bool old_first;
 	int err;
 
 	reset &= test_bit(MLX5E_STATE_OPENED, &priv->state);
@@ -3602,13 +3629,14 @@ int mlx5e_safe_switch_params(struct mlx5e_priv *priv,
 		goto err_cancel_selq;
 
 	*old_chs = priv->channels;
+	old_first = mlx5e_close_old_first(priv, old_chs);
 
 	err = mlx5e_switch_priv_channels(priv, old_chs, new_chs,
-					 preactivate, context);
+					 preactivate, context, old_first);
 	if (err)
 		goto err_close;
 
-	if (MLX5_CAP_GEN(priv->mdev, tis_tir_td_order))
+	if (!old_first)
 		mlx5e_close_channels(old_chs);
 
 	kfree(new_chs);
Signature · 1 line
2.56.1
#12 lore raw

PATCH v2 11/15 net/mlx5e: Receive into XDP-only memory providers

In reply to Björn Töpel

mlx5 still refuses queue restarts with an XDP program, and its page pools take no provider next to XDP.

Let striding RQs with a program take XDP-only providers, and accept queue restarts with one, as other providers refuse XDP. Binds on the cyclic RQ fail, as it would spin on a short FILL ring.

While a queue has such a provider, refuse with -EBUSY:
- a switch to the cyclic RQ;
- PTP RX, TX port timestamps and HTB offload, as queue restarts clone no channel under them, so unbind would fail and binds fail with -EOPNOTSUPP;
- growing the RQs of a device that is down, which would fail the next open.

A devlink eswitch mode change bypasses these checks, leaving the queue on system pages that the socket drops.

At MTU 1500, the linear RQ puts one packet per 4 KiB chunk. The nonlinear RQ puts every byte in fragments, so its head reaches the socket only with metadata. A bind or unbind pauses all channels briefly.

---

Notes (v2changes):
Changes in v2:
- Move the skip of the wait for posted WQEs to patch 10, and use the net/mlx5e prefix.
- Let only striding RQs take XDP-only providers. The cyclic RQ posts whole bulks only, and would spin on a short FILL ring (Sashiko).
- Refuse a switch from the striding RQ to the cyclic one, PTP RX, TX port timestamps and HTB offload while a queue has such a provider, and growing the RQs while the device is down (Sashiko).
- The message covers the nonlinear RQ and its empty heads (Sashiko), and says what users see.

 .../net/ethernet/mellanox/mlx5/core/en_main.c | 62 ++++++++++++++++++-
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   | 10 +--
 2 files changed, 64 insertions(+), 8 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_main.c +59 -3
index a76ba297c9a9..74a2ae37a587 100644
@@ -1012,6 +1012,13 @@ static int mlx5e_alloc_rq(struct mlx5e_params *params,
 
 		pp_params.order     = pool_order;
 		pp_params.flags     = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV;
+		/* Trap and PTP RQs use queue 0 too, without a program. A
+		 * cyclic RQ posts whole bulks only, and would spin in NAPI
+		 * on a short FILL ring.
+		 */
+		if (params->xdp_prog &&
+		    rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
+			pp_params.flags |= PP_FLAG_ALLOW_XDP_ONLY_NETMEM;
 		pp_params.pool_size = pool_size;
 		pp_params.nid       = node;
 		pp_params.dev       = rq->pdev;
@@ -3609,6 +3616,46 @@ int mlx5e_safe_switch_params(struct mlx5e_priv *priv,
 	bool old_first;
 	int err;
 
+	/* An XDP-only memory provider takes a striding RQ, and the socket
+	 * that installed it would lose its queue without a word, also when
+	 * the device opens later
+	 */
+	if (priv->channels.params.rq_wq_type ==
+	    MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
+	    params->rq_wq_type != MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
+	    mlx5e_has_xdp_only_mp(priv, priv->netdev->real_num_rx_queues)) {
+		netdev_warn(priv->netdev,
+			    "Cannot leave the striding RQ while a queue has an XDP-only memory provider\n");
+		return -EBUSY;
+	}
+
+	/* mlx5e_queue_mem_alloc() clones no channel while PTP is on, so the
+	 * provider of any queue could not go: its unbind restarts the queue
+	 */
+	if (((params->ptp_rx && !priv->channels.params.ptp_rx) ||
+	     (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_TX_PORT_TS) &&
+	      !MLX5E_GET_PFLAG(&priv->channels.params,
+			       MLX5E_PFLAG_TX_PORT_TS))) &&
+	    mlx5e_has_xdp_only_mp(priv, priv->netdev->real_num_rx_queues)) {
+		netdev_warn(priv->netdev,
+			    "Cannot turn on PTP while a queue has an XDP-only memory provider\n");
+		return -EBUSY;
+	}
+
+	/* Closed channels open no RQ that would let the provider check a
+	 * larger page pool, so the device would fail to open later
+	 */
+	if (!test_bit(MLX5E_STATE_OPENED, &priv->state) &&
+	    params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ &&
+	    mlx5e_mpwqe_get_log_rq_size(priv->mdev, params, NULL) >
+	    mlx5e_mpwqe_get_log_rq_size(priv->mdev, &priv->channels.params,
+					NULL) &&
+	    mlx5e_has_xdp_only_mp(priv, priv->netdev->real_num_rx_queues)) {
+		netdev_warn(priv->netdev,
+			    "Cannot grow the RQs while the device is down and a queue has an XDP-only memory provider\n");
+		return -EBUSY;
+	}
+
 	reset &= test_bit(MLX5E_STATE_OPENED, &priv->state);
 	if (!reset)
 		return mlx5e_switch_priv_params(priv, params, preactivate, context);
@@ -4166,11 +4213,21 @@ static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type,
 		err = mlx5e_setup_tc_mqprio(priv, type_data);
 		mutex_unlock(&priv->state_lock);
 		return err;
-	case TC_SETUP_QDISC_HTB:
+	case TC_SETUP_QDISC_HTB: {
+		struct tc_htb_qopt_offload *htb_qopt = type_data;
+
+		/* As for PTP, see mlx5e_safe_switch_params() */
+		if (htb_qopt->command == TC_HTB_CREATE &&
+		    mlx5e_has_xdp_only_mp(priv, dev->real_num_rx_queues)) {
+			NL_SET_ERR_MSG_MOD(htb_qopt->extack,
+					   "Cannot offload HTB while a queue has an XDP-only memory provider");
+			return -EBUSY;
+		}
 		mutex_lock(&priv->state_lock);
 		err = mlx5e_htb_setup_tc(priv, type_data);
 		mutex_unlock(&priv->state_lock);
 		return err;
+	}
 	default:
 		return -EOPNOTSUPP;
 	}
@@ -5872,10 +5929,9 @@ static int mlx5e_queue_mem_alloc(struct net_device *dev,
 
 	if (MLX5E_GET_PFLAG(&chs->params, MLX5E_PFLAG_TX_PORT_TS) ||
 	    chs->params.ptp_rx   ||
-	    chs->params.xdp_prog ||
 	    priv->htb) {
 		netdev_err(priv->netdev,
-			   "Cloning channels with Port/rx PTP, XDP or HTB is not supported\n");
+			   "Cloning channels with Port/rx PTP or HTB is not supported\n");
 		err = -EOPNOTSUPP;
 		goto unlock;
 	}
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +5 -5
index 1586cb2404c0..43a382fcd3ad 100644
@@ -327,7 +327,7 @@ static void *mlx5e_mpwqe_get_linear_page_frag(struct mlx5e_rq *rq)
 	frag_offset = li->frag_page.frags << MLX5E_XDP_LOG_MAX_LINEAR_SZ;
 	WARN_ON(frag_offset >= BIT(rq->mpwqe.page_shift));
 
-	return netmem_address(li->frag_page.netmem) + frag_offset;
+	return xdp_netmem_address(li->frag_page.netmem) + frag_offset;
 }
 
 static inline int mlx5e_get_rx_frag(struct mlx5e_rq *rq,
@@ -1585,7 +1585,7 @@ mlx5e_skb_from_cqe_linear(struct mlx5e_rq *rq, struct mlx5e_wqe_frag_info *wi,
 	dma_addr_t addr;
 	u32 frag_size;
 
-	va             = netmem_address(frag_page->netmem) + wi->offset;
+	va             = xdp_netmem_address(frag_page->netmem) + wi->offset;
 	data           = va + rx_headroom;
 	frag_size      = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt);
 
@@ -1640,7 +1640,7 @@ mlx5e_skb_from_cqe_nonlinear(struct mlx5e_rq *rq, struct mlx5e_wqe_frag_info *wi
 
 	frag_page = wi->frag_page;
 
-	va = netmem_address(frag_page->netmem) + wi->offset;
+	va = xdp_netmem_address(frag_page->netmem) + wi->offset;
 	frag_consumed_bytes = min_t(u32, frag_info->frag_size, cqe_bcnt);
 
 	addr = page_pool_get_dma_addr_netmem(frag_page->netmem);
@@ -1940,7 +1940,7 @@ mlx5e_skb_from_cqe_mpwrq_nonlinear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *w
 
 	prog = rcu_dereference(rq->xdp_prog);
 
-	head_addr = netmem_address(head_page->netmem) + head_offset;
+	head_addr = xdp_netmem_address(head_page->netmem) + head_offset;
 
 	if (prog) {
 		/* area for bpf_xdp_[store|load]_bytes */
@@ -2114,7 +2114,7 @@ mlx5e_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
 		return NULL;
 	}
 
-	va             = netmem_address(frag_page->netmem) + head_offset;
+	va             = xdp_netmem_address(frag_page->netmem) + head_offset;
 	data           = va + rx_headroom;
 	frag_size      = MLX5_SKB_FRAG_SZ(rx_headroom + cqe_bcnt);
 
Signature · 1 line
2.56.1
#13 lore raw

PATCH v2 12/15 net/mlx5e: Release XDP-only pages earlier

In reply to Björn Töpel

Before this patch, a striding RQ released pages only when reposting their WQE, so a provider page returned its chunk late, on an extra empty descriptor. The chunks come back without this patch; it only saves that descriptor.

On the linear path, release a provider page once XDP took the buffer in its last stride, and let the WQE skip it, also at close. The chunk then comes back on the packet's descriptor, as the socket drops its references last. The device writes nothing to a page after its last stride, so this cannot race with DMA.

Also skip the head and xdp_frame prefetches of XDP-only pages. Only the program reads them, and after the early release each packet lands in an untouched page, where a prefetch may cost a page walk.

On ConnectX-7, one core, 66-byte frames offered at 61 Mpps, a program that parses headers and an application that reads the payload, this and the batched reference drops took a 4 KiB-chunk queue from 12.40-12.66 to 15.65-15.92 Mpps.

---

Notes (v2changes):
Changes in v2:
- New in v2, from "eth: mlx5e: Let go of XDP-only pages after their last stride" and "eth: mlx5e: Skip head prefetches for XDP-only buffers" of the v1 full series. A striding RQ let go of a page only when it reposted its WQE, so its chunk came back on an extra empty descriptor, and the head prefetch of a page that the CPU had not touched cost a page walk. The message has the numbers.

 .../net/ethernet/mellanox/mlx5/core/en_rx.c   | 23 +++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +21 -2
index 43a382fcd3ad..a93eefa581c0 100644
@@ -2107,6 +2107,7 @@ mlx5e_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
 	void *va, *data;
 	dma_addr_t addr;
 	u32 frag_size;
+	bool xdp_only;
 
 	/* Check packet size. Note LRO doesn't use linear SKB */
 	if (unlikely(cqe_bcnt > rq->hw_mtu)) {
@@ -2121,18 +2122,36 @@ mlx5e_skb_from_cqe_mpwrq_linear(struct mlx5e_rq *rq, struct mlx5e_mpw_info *wi,
 	addr = page_pool_get_dma_addr_netmem(frag_page->netmem);
 	dma_sync_single_range_for_cpu(rq->pdev, addr, head_offset,
 				      frag_size, rq->buff.map_dir);
-	net_prefetch(data);
+	/* The kernel reads an XDP-only head only if the program does, and a
+	 * prefetch may cost a page walk
+	 */
+	xdp_only = netmem_is_net_iov(frag_page->netmem);
+	if (!xdp_only)
+		net_prefetch(data);
 
 	prog = rcu_dereference(rq->xdp_prog);
 	if (prog) {
 		struct mlx5e_xdp_buff *mxbuf = &rq->mxbuf;
 
-		net_prefetchw(va); /* xdp_frame data area */
+		if (!xdp_only)
+			net_prefetchw(va); /* xdp_frame data area */
 		mlx5e_fill_mxbuf(rq, cqe, va, rx_headroom, rq->buff.frame0_sz,
 				 cqe_bcnt, mxbuf);
 		if (mlx5e_xdp_handle(rq, prog, mxbuf)) {
 			if (__test_and_clear_bit(MLX5E_RQ_FLAG_XDP_XMIT, rq->flags))
 				frag_page->frags++;
+			/* The device is done with a provider page after its
+			 * last stride. Letting go of it now, before the socket
+			 * drops its references, gives the chunk back on the
+			 * packet's descriptor rather than an empty one.
+			 */
+			if (xdp_only &&
+			    head_offset + BIT(rq->mpwqe.log_stride_sz) >=
+			    BIT(rq->mpwqe.page_shift)) {
+				mlx5e_page_release_fragmented(rq->page_pool,
+							      frag_page);
+				__set_bit(page_idx, wi->skip_release_bitmap);
+			}
 			return NULL; /* page/packet was consumed by XDP */
 		}
 
Signature · 1 line
2.56.1
#14 lore raw

PATCH v2 13/15 net/mlx5e: Test for XDP-only pages per RQ

In reply to Björn Töpel

Before this patch, the linear striding RQ handler tested every page for a net_iov, to skip the prefetches of XDP-only pages and to release them after their last stride. RQs of system pages never get a net_iov, yet the test, and the values it keeps live across the program call, cost native XDP 7 instructions per packet on every RQ.

An RQ gets an XDP-only provider only through a queue restart, which allocates new RQs, so whether an RQ receives into net_iovs is fixed when its page pool is created. Make the choice once per RQ. Build the linear handler in two variants and give an RQ the XDP-only one when its pool has such a provider. RQs of system pages then run the code they ran before XDP-only providers existed, and provider RQs skip the test as well.

Binding or unbinding a provider restarts the queue and so picks the handler again. Swapping the program keeps the RQs, and a provider needs a program throughout.

---

Notes (v2changes):
Changes in v2:
- New in v2.

 drivers/net/ethernet/mellanox/mlx5/core/en.h  |  1 +
 .../net/ethernet/mellanox/mlx5/core/en_main.c |  2 +
 .../net/ethernet/mellanox/mlx5/core/en_rx.c   | 54 ++++++++++++++++---
 3 files changed, 50 insertions(+), 7 deletions(-)
drivers/net/ethernet/mellanox/mlx5/core/en.h +1 -0
index 657a7d59c845..05d2216ab981 100644
@@ -618,6 +618,7 @@ typedef void (*mlx5e_fp_dealloc_wqe)(struct mlx5e_rq*, u16);
 typedef void (*mlx5e_fp_shampo_dealloc_hd)(struct mlx5e_rq*, u16, u16, bool);
 
 int mlx5e_rq_set_handlers(struct mlx5e_rq *rq, struct mlx5e_params *params, bool xsk);
+void mlx5e_rq_set_xdp_only(struct mlx5e_rq *rq);
 void mlx5e_rq_set_trap_handlers(struct mlx5e_rq *rq, struct mlx5e_params *params);
 
 enum mlx5e_rq_flag {
drivers/net/ethernet/mellanox/mlx5/core/en_main.c +2 -0
index 74a2ae37a587..8fdd78f74f38 100644
@@ -1050,6 +1050,8 @@ static int mlx5e_alloc_rq(struct mlx5e_params *params,
 							 MEM_TYPE_PAGE_POOL, rq->page_pool);
 			if (err)
 				goto err_destroy_page_pool;
+			if (rq->xdp_rxq.xdp_only_area)
+				mlx5e_rq_set_xdp_only(rq);
 		}
 	}
 
drivers/net/ethernet/mellanox/mlx5/core/en_rx.c +47 -7

93 lines not on this page · raw message

Signature · 1 line
2.56.1
#15 lore raw

PATCH v2 14/15 selftests: net: xsk: Test explicit chunk return

In reply to Björn Töpel

xskxceiver cannot test explicit chunk return yet, as it expects one chunk per packet at the UMEM headroom.

Add -D (--explicit-return). The RX endpoint, in drv mode, registers its UMEM with XDP_UMEM_EXPLICIT_RETURN, checks bytes by packet offset, and refills a chunk only on XDP_RX_CHUNK_RETURNED. The TX endpoint takes -D too, as it keeps fewer packets in flight to such a receiver. -H BYTES (--headroom) fails a run in which a packet starts less than BYTES into its chunk. Cases that cannot run skip and say why. Run STAT_RX_FULL under -D too, and add RX_RING_FULL_RETURNS.

With NETIF, xsk.py runs each case that tests the rx role once more with explicit return, e.g. test_xsk.er_SEND_RECEIVE.

Also stop the cleanup of a UMEM that failed to register from unmapping address zero.

---

Notes (v2changes):
Changes in v2:
- Rename from "selftests: net: xsk: Test the device's RX buffer layout".
- Rename --drv-layout (-D) to --explicit-return, and add -H BYTES (--headroom), a headroom check that needs -D.
- Port to the reworked xsk selftests in net-next: one case per invocation, with -D on both endpoints, as the sender paces for an explicit-return receiver. drivers/net/xsk.py runs the rx-role cases with explicit return on NETIF (er_ variants), in place of the test_xsk.sh options. RX_RING_FULL_RETURNS lists the rx role.
- Reserve FILL slots only for chunks that came back, so that the receive loop cannot spin (Sashiko).
- Register only the receiving UMEM with the flag, with page-sized chunks also on 16K and 64K pages, and bind with XDP_USE_SG only where the device takes multi-buffer XDP (Sashiko).
- Skip what the mode cannot run, and say why, among them STAT_FILL_EMPTY and HW_SW_MIN_RING_SIZE (Sashiko). A kernel or device without the mode, or a UMEM with too few chunks for the device, skips instead of failing or hanging.
- Check empty heads with metadata, and count a packet only once it completes (Sashiko).
- Check the sequence number of the last payload word unless the tail grew (Sashiko).
- Clean up a UMEM that failed to register without unmapping address zero.
- Size the too-large packets of the invalid-descriptor tests by the chunk, and let INVALID_DESC straddle a chunk on pages larger than 4K (Sashiko).
- Skip -D on a device without native XDP (Sashiko).
- Run STAT_RX_FULL under -D: the RX ring fills while the reader waits, and each return must come outside a packet, after data of its chunk.
- Add RX_RING_FULL_RETURNS, a slow reader on a small RX ring.

 tools/testing/selftests/drivers/net/xsk.py    |  28 +-
 .../testing/selftests/net/lib/xsk/test_xsk.c  | 556 +++++++++++++++++-
 .../testing/selftests/net/lib/xsk/test_xsk.h  |   9 +
 .../selftests/net/lib/xsk/xskxceiver.c        |  57 +-
 4 files changed, 617 insertions(+), 33 deletions(-)
tools/testing/selftests/drivers/net/xsk.py +21 -7
index ebde81fa5140..6c296e6bd257 100755
@@ -34,6 +34,12 @@ them for IPv6, the case runs over IPv4. NETIF thus needs RSS, and both hosts
 ntuple filters. The cases in RING_SIZES set the rings that they test with
 ethtool -G.
 
+With NETIF, each case that tests the rx role also runs with the local
+endpoint receiving in drv mode with explicit chunk return
+(XDP_UMEM_EXPLICIT_RETURN), e.g. test_xsk.er_SEND_RECEIVE. Both endpoints
+take -D, as the sender paces for it. They skip the case if NETIF cannot back
+the queue with an XDP-only memory provider.
+
 The UNALIGNED cases need 32 huge pages of 2 MiB on both hosts, and are
 expected failures without them.
 """
@@ -74,8 +80,10 @@ HUGEPAGES = "/sys/kernel/mm/hugepages/hugepages-2048kB/nr_hugepages"
 MAX_SKB_FRAGS = "/proc/sys/net/core/max_skb_frags"
 # What xskxceiver takes when it cannot read MAX_SKB_FRAGS.
 DEFAULT_SKB_FRAGS = 17
-# The ip link option that detaches the XDP program of an endpoint mode.
-XDP_OFF = {"skb": "xdpgeneric off", "drv": "xdpdrv off", "zc": "xdpdrv off"}
+# The ip link option that detaches the XDP program of an endpoint mode, er
+# being drv mode with explicit chunk return.
+XDP_OFF = {"skb": "xdpgeneric off", "drv": "xdpdrv off", "zc": "xdpdrv off",
+           "er": "xdpdrv off"}
 # Why a case cannot pass over veth, by the mode, None for any, and the name.
 VETH_XFAIL = {
     # Generic XDP leaves no room to grow the tail, so the RX endpoint skips.
@@ -134,6 +142,9 @@ def _get_cases(cfg):
         for direction in roles.split():
             case = Case("zc", direction, int(test_id), name)
             ret.append(KsftNamedVariant(f"{direction}_{name}", case))
+        if "rx" in roles.split():
+            case = Case("er", "rx", int(test_id), name)
+            ret.append(KsftNamedVariant(f"er_{name}", case))
     return ret
 
 
@@ -228,12 +239,14 @@ def _steer(cfg, queue, udp_port, ctrl_port):
     raise KsftSkipEx("the hosts reject the ntuple rules of the test")
 
 
-def _setup_nic(cfg, udp_port, ctrl_port):
+def _setup_nic(cfg, case, udp_port, ctrl_port):
     """Take the last queue with both an RX and a TX ring out of RSS and steer
     the flows of a case until it ends. Return the queue and the frags per
     packet that the DUT takes in zero-copy mode."""
     dev = cfg.netnl.dev_get({"ifindex": cfg.ifindex})
-    if "xsk-zerocopy" not in dev["xdp-features"]:
+    # Explicit return needs no zero-copy support; xskxceiver skips if the
+    # bind fails.
+    if case.mode == "zc" and "xsk-zerocopy" not in dev["xdp-features"]:
         raise KsftSkipEx(f"{cfg.ifname} does not support AF_XDP zero-copy")
 
     req = {"header": {"dev-index": cfg.ifindex}}
@@ -361,7 +374,7 @@ def _test(cfg, case, busy_poll):
         queue, max_frags = 0, _setup_veth(case)
     else:
         timeout, xsk_timeout = NIC_CASE_TIMEOUT, NIC_XSK_TIMEOUT
-        queue, max_frags = _setup_nic(cfg, udp_port, ctrl_port)
+        queue, max_frags = _setup_nic(cfg, case, udp_port, ctrl_port)
 
     if busy_poll:
         # So that SO_PREFER_BUSY_POLL of xskxceiver -b keeps the IRQs masked.
@@ -392,12 +405,13 @@ def _test(cfg, case, busy_poll):
             "-t", str(case.test_id),
             "--udp-port", str(udp_port),
             "--max-frags", str(max_frags),
-            "--timeout", str(xsk_timeout)]
+            "--timeout", str(xsk_timeout),
+            *(["-D"] if case.mode == "er" else [])]
     local = ["timeout", "-k", "5", str(timeout), str(cfg.bin_local),
              "-i", cfg.ifname,
              "--queue", str(queue),
              "--listen",
-             "-m", case.mode,
+             "-m", "drv" if case.mode == "er" else case.mode,
              "-e", case.direction,
              *(["-b"] if busy_poll else []),
              "--src-ip", cfg.addr,
tools/testing/selftests/net/lib/xsk/test_xsk.c +534 -22

827 lines not on this page · raw message

tools/testing/selftests/net/lib/xsk/test_xsk.h +9 -0
index 872e15206b48..7d73df25e819 100644
@@ -36,6 +36,11 @@
 extern bool opt_verbose;
 /* The smallest test packet, set by xsk_set_flow() */
 extern int min_pkt_size;
+/* XDP_UMEM_EXPLICIT_RETURN: the device lays out RX buffers in UMEM chunks */
+extern bool opt_explicit_return;
+extern u64 drv_rx_descs, drv_rx_shared, drv_rx_empty_heads;
+extern u64 drv_rx_empty_heads_freed;
+extern u32 drv_rx_headroom, opt_headroom;
 #define print_verbose(x...) do { if (opt_verbose) ksft_print_msg(x); } while (0)
 
 
@@ -234,6 +239,7 @@ struct test_spec {
 	struct bpf_map *xskmap_tx;
 	test_func_t test_func;
 	int mtu;
+	u32 adjust_grow;
 	u16 total_steps;
 	u16 current_step;
 	u16 nb_sockets;
@@ -288,6 +294,7 @@ int testapp_stats_fill_empty(struct test_spec *test);
 int testapp_stats_rx_dropped(struct test_spec *test);
 int testapp_stats_tx_invalid_descs(struct test_spec *test);
 int testapp_stats_rx_full(struct test_spec *test);
+int testapp_rx_ring_full_returns(struct test_spec *test);
 int testapp_teardown(struct test_spec *test);
 int testapp_too_many_frags(struct test_spec *test);
 int testapp_tx_queue_consumer(struct test_spec *test);
@@ -336,6 +343,8 @@ static const struct test_spec hw_rx_tests[] = {
 	{.name = "XDP_ADJUST_TAIL_SHRINK_MULTI_BUFF", .test_func = testapp_adjust_tail_shrink_mb},
 	{.name = "XDP_ADJUST_TAIL_GROW", .test_func = testapp_adjust_tail_grow},
 	{.name = "XDP_ADJUST_TAIL_GROW_MULTI_BUFF", .test_func = testapp_adjust_tail_grow_mb},
+	{.name = "RX_RING_FULL_RETURNS",
+	 .test_func = testapp_rx_ring_full_returns},
 };
 
 static const struct test_spec hw_tx_tests[] = {
tools/testing/selftests/net/lib/xsk/xskxceiver.c +53 -4

105 lines not on this page · raw message

Signature · 1 line
2.56.1
#16 lore raw

PATCH v2 15/15 Documentation: xsk: Describe explicit chunk return

In reply to Björn Töpel

XDP_UMEM_EXPLICIT_RETURN changes who owns a chunk and when, what the program may do, and how chunks come back. None of it is documented.

Describe the setup and its errors, the program, receive and chunk return, need_wakeup, DMA syncs, TX of received buffers, and the driver rule.

---

Notes (v2changes):
Changes in v2:
- Rename from "Documentation: networking: Describe AF_XDP RX in the device's layout", and the section from "Device Buffer Layout" to "Explicit Chunk Return". Say that the application cannot influence where packets land, how many share a chunk, or the headroom.
- Describe the bind requirements and their errors: CAP_NET_ADMIN in the initial user namespace and XDP_ZEROCOPY (Sashiko), the other EOPNOTSUPP cases, the UMEM size, and binds with XDP_SHARED_UMEM.
- Describe empty heads on RX, the headroom, and when need_wakeup is set and cleared, with a table that tells descriptors apart.
- Describe what TX may do with received buffers, and when a chunk is the application's again.
- Say that the UMEM stops backing the queue once the last socket of the queue is gone, and that a tool that detaches the program right after closing its sockets should retry on EBUSY.
- Say that only netlink can replace the program while the UMEM backs the queue.
- Give the page pool cache size per range of page sizes, EBUSY for a queue that the device leases to another one, and ENODATA for page pools that do not take the provider (Sashiko).
- Say that the RX ring keeps room for the chunks the kernel owes, so returns never wait and need no wakeup, and that the chunks a queue did not return are the application's once it goes or its receiving socket closes (Sashiko).
- Say that the kernel does not check a chunk on the FILL rings of two queues, which can only corrupt the application's own data (Sashiko).
- Say that a socket whose fd is closed while its rings stay mapped is still bound.
- Add a "Drivers" paragraph on the context rule that a driver agrees to with PP_FLAG_ALLOW_XDP_ONLY_NETMEM, and on running the NAPI once after a queue start.

 Documentation/networking/af_xdp.rst | 244 +++++++++++++++++++++++++++-
 1 file changed, 243 insertions(+), 1 deletion(-)
Documentation/networking/af_xdp.rst +243 -1
index 538e71adb759..073eab1da103 100644
@@ -429,7 +429,9 @@ There is also an option to set the headroom of each single buffer in
 the UMEM. If you set this to N bytes, it means that the packet will
 start N bytes into the buffer leaving the first N bytes for the
 application to use. The final option is the flags field, but it will
-be dealt with in separate sections for each UMEM flag.
+be dealt with in separate sections for each UMEM flag. With the
+XDP_UMEM_EXPLICIT_RETURN flag, chunks are pages that the device fills in
+its own layout, see the "Explicit Chunk Return" section.
 
 SO_BINDTODEVICE setsockopt
 --------------------------
@@ -761,6 +763,246 @@ processing. Note that the Tx batch API guarantees that it will provide
 a batch of Tx descriptors that ends with full packet at the end. This
 to facilitate extending a zero-copy driver with multi-buffer support.
 
+Explicit Chunk Return
+=====================
+
+In the classic zero-copy mode, the driver fills one UMEM frame per
+buffer, at the UMEM headroom, and each RX descriptor gives its frame
+back to the application. The driver needs a receive path of its own
+for AF_XDP.
+
+With the XDP_UMEM_EXPLICIT_RETURN UMEM flag, an RX descriptor does not
+give its chunk back. The device places buffers in UMEM chunks as it
+places them in the pages of its page pool: several buffers may share a
+chunk, a packet may span chunks, and the device picks the offsets. A
+chunk comes back only when an RX descriptor carries
+XDP_RX_CHUNK_RETURNED, and the application must not reuse it before.
+The XDP program reads and writes packets in place, in the UMEM, but
+the kernel stores none of its own structures in the chunks.
+
+The application cannot influence the layout. The kernel and the driver
+decide where packets land, how many share a chunk, and the headroom in
+front of a packet. The application only puts chunks on the FILL ring
+and takes back the chunks that XDP_RX_CHUNK_RETURNED returns.
+
+The UMEM backs the page pools of the bound queue through a memory
+provider, so the driver needs no AF_XDP code. A driver supports the
+mode if it runs native XDP with XDP_REDIRECT, takes its receive
+buffers from a page pool of netmem that allows such providers, and
+can restart a queue.
+
+Setting up
+----------
+
+* Register the UMEM with XDP_UMEM_EXPLICIT_RETURN, aligned chunks of
+  PAGE_SIZE, and no headroom. The UMEM must hold more chunks than the
+  size of each page pool of the queue plus its cache, or the bind
+  fails with ENOSPC. The driver sizes its page pools from the ring
+  size and the MTU. The cache holds PP_ALLOC_CACHE_SIZE chunks: 128
+  with pages smaller than 16K, 32 with pages from 16K to below 64K,
+  and 8 with pages of 64K or larger.
+
+* Attach a native XDP program to the device itself through netlink, not
+  through a BPF link or an upper device such as a bond, before the bind.
+  The bind fails with ENOENT without one. While the UMEM backs the
+  queue, the program cannot be detached (EBUSY), and only netlink can
+  replace it, not a BPF link, so the queue always runs a program until
+  the UMEM no longer backs the queue. That happens shortly after the
+  last socket of the queue is gone, its fd closed and its rings
+  unmapped: a tool that detaches the program right after closing its
+  sockets should retry on EBUSY.
+
+* Put chunks on the FILL ring before the bind. The bind starts the
+  queue with the chunks it finds there, and the queue drops packets
+  while it has none. A queue that finds the FILL ring empty sets the
+  need_wakeup flag of the FILL ring, and takes chunks again at the
+  wakeup that follows, see below.
+
+* Bind with XDP_USE_NEED_WAKEUP: a queue that used up the FILL ring
+  takes chunks again only after a wakeup. A bind without it fails with
+  EINVAL. Bind with XDP_USE_SG to receive packets that the device
+  splits over more than one buffer.
+
+* The bind needs CAP_NET_ADMIN in the initial user namespace, as
+  io_uring zero-copy receive does, because the queue's program then
+  drops every packet that it does not redirect to the socket, and
+  cannot be detached. Without it, the bind fails with EPERM, also for
+  a process that has CAP_NET_ADMIN only in a user namespace of its
+  own.
+
+* XDP_COPY and XDP_ZEROCOPY fail with EOPNOTSUPP. Both ask for a
+  classic mode, a copy or the driver's own zero-copy path, and this
+  mode is neither. The bind also fails with EOPNOTSUPP on a device
+  without native XDP_REDIRECT or queue management, with XDP_USE_SG on
+  a device without multi-buffer RX (NETDEV_XDP_ACT_RX_SG), on a queue
+  that the device leases from another device, on a queue without a DMA
+  device, and on a queue that has no RX queue, such as a TX-only
+  queue. A queue that the device leases to another device fails with
+  EBUSY. A queue whose page pools do not take the provider, as on mlx5
+  without a striding RQ, fails with ENODATA.
+  A queue id beyond both the RX and the TX queues fails with EINVAL, as
+  in the classic modes.
+
+* These requirements apply to the bind that sets up the queue. A
+  socket that binds with XDP_SHARED_UMEM to the same queue takes the
+  setup of the first, and passes no flags of its own.
+
+* One socket per queue receives: the first one with an RX ring, also
+  when it binds with XDP_SHARED_UMEM. Another socket with an RX ring
+  gets EBUSY, and so does any socket with an RX ring once the receiving
+  socket is gone. Sockets that share the UMEM with XDP_SHARED_UMEM may
+  transmit. A queue without a receiving socket drops every packet that
+  its program redirects. The UMEM stops backing the queue shortly after
+  the last socket of the queue closes, when the kernel tears down its
+  buffer pool from a workqueue, as with classic zero copy.
+
+The XDP program
+---------------
+
+The program works on the packet in place, in the UMEM: direct packet
+access, bpf_xdp_load_bytes(), bpf_xdp_store_bytes(), dynptrs,
+bpf_xdp_adjust_head(), bpf_xdp_adjust_tail(), bpf_xdp_adjust_meta()
+and the RX metadata kfuncs work as with pages, on every buffer of the
+packet. What the program writes is in the UMEM for the application to
+see. The kernel keeps its own data, such as the fragment list of a
+packet that spans buffers, in kernel memory, so the application cannot
+change what the kernel relies on. The application may still change
+the packet at any time, also while the program runs: two reads of the
+same bytes may differ.
+
+Buffers of the bound queue may only go to its receiving socket. Every
+verdict but XDP_REDIRECT drops the packet, XDP_PASS included, and
+redirects to anything but the receiving socket fail.
+
+Redirect through an XSKMAP entry that holds the receiving socket.
+
+Receiving
+---------
+
+A FILL entry hands a whole chunk to the kernel, by its address, as
+in the classic modes. Each buffer of a packet becomes one RX
+descriptor that points at the data, anywhere in a chunk, and
+XDP_PKT_CONTD chains the buffers of a packet as described in the
+multi-buffer section. In front of the first buffer
+the device leaves the headroom that native XDP has on it, so a program
+may grow a header as it does without a socket. Metadata that the program
+wrote is in front of the first buffer, where the program put it. A
+packet whose metadata does not fit in front of its first buffer, in
+the same chunk, is dropped.
+
+The kernel gives a chunk back with XDP_RX_CHUNK_RETURNED in the options
+of an RX descriptor:
+
+* On a descriptor of a packet, the flag means that the kernel is done
+  with the chunk of that descriptor. The application owns the whole
+  chunk again. An empty head carries the flag like any other
+  descriptor of the packet.
+
+* A descriptor with length zero and without XDP_PKT_CONTD, at the
+  address of a chunk, only gives that chunk back. The kernel posts one
+  when the device lets go of the chunk after its last buffer reached the
+  socket, after the descriptors of its buffers. Such a descriptor is
+  never part of a packet, and it always carries XDP_RX_CHUNK_RETURNED.
+
+A first descriptor of length zero with XDP_PKT_CONTD is an empty head:
+it carries no bytes, and its address anchors the metadata in front of
+it. The data of the packet sits in the descriptors that follow. The
+kernel posts one when the device put every byte of a packet in
+fragments, as some devices do for frames larger than a page, and the
+program wrote metadata. Such a head reaches the socket only on a
+socket bound with XDP_USE_SG, as the packet then takes more than one
+descriptor.
+
+XDP_PKT_CONTD tells the two apart, never XDP_RX_CHUNK_RETURNED. The
+table assumes that the device puts no fragment of length zero in a
+packet, which no driver of this mode does:
+
+=========  ==============  ======================  ==========================
+Length     XDP_PKT_CONTD   XDP_RX_CHUNK_RETURNED   The descriptor
+=========  ==============  ======================  ==========================
+non-zero   either          either                  carries data of a packet;
+                                                   with the flag, its chunk
+                                                   came back
+zero       set             either                  is an empty head; with the
+                                                   flag, its chunk came back
+zero       clear           always set              only gives a chunk back
+=========  ==============  ======================  ==========================
+
+XDP_RX_CHUNK_RETURNED belongs to RX, and a TX descriptor that carries
+it is invalid.
+
+A chunk may go on the FILL ring again once it came back and the
+application is done with all its buffers. The kernel never gives back
+single buffers, so a packet that the application holds keeps its
+chunk. Chunks whose buffers all stayed in the kernel, as for dropped
+packets, stay with the kernel. Once the UMEM stops backing the queue,
+every chunk that the queue had not returned is the application's
+again, without a descriptor. So is every chunk that the kernel still
+owes when the receiving socket goes. A socket goes once its fd is
+closed and its rings are unmapped, as usual for AF_XDP: a socket whose
+fd is closed while its rings stay mapped is still bound.
+
+The kernel keeps room in the RX ring for one descriptor per chunk that
+it still owes back, so a return never waits for room. A packet that
+would leave less room is dropped and counted in rx_ring_full, so under
+overload packets drop a little earlier than the ring is full. A queue
+restart returns the chunks of the old queue as it stops it.
+
+Within a queue, a FILL entry of a chunk that the kernel still holds is
+invalid, and the kernel skips it. Across queues that share a UMEM, the
+kernel does not check: as with classic zero copy, a chunk on the FILL
+rings of two queues at once may be written by both devices. That
+corrupts only the application's data, as the kernel keeps nothing in
+the chunks.
+
+The kernel sets the need_wakeup flag of the FILL ring at bind and when
+the queue finds no chunks to take, and clears it once the queue took
+chunks, from the FILL ring or from those it kept, as in the classic
+zero-copy mode. Check the flag, not the FILL ring: recvmsg() or poll()
+then schedules the queue's NAPI. Returns need no wakeup, as the kernel
+posts each one as the device lets go of its chunk. With preferred busy
+polling, the application's poll runs the queue's NAPI.
+
+Devices that need DMA syncs work as they do with page pools: the
+kernel syncs a chunk for the device when it takes the chunk from the
+FILL ring, and the driver syncs each buffer for the CPU before the
+program runs.
+
+Drivers
+-------
+
+The provider takes no lock. A driver that lets it back a page pool,
+with PP_FLAG_ALLOW_XDP_ONLY_NETMEM, keeps one context at a time on the
+buffers of the queue: its NAPI, or an operation that runs only while
+that NAPI cannot use the rings, as when the driver restarts the queue,
+changes its configuration or recovers from an error. It fills and
+drains the rings only then, and runs the NAPI once after the queue
+starts, so that a queue that starts with FILL empty asks for a wakeup.
+The buffers leave the driver only to the AF_XDP socket of the queue,
+which drops its references in that NAPI.
+
+Transmitting
+------------
+
+Sockets in this mode transmit in copy mode only. A buffer that the
+application received may go on the TX ring, within these limits:
+
+* TX takes no descriptor of length zero. A forwarder skips the empty
+  head of a received packet and starts the packet at the next
+  descriptor.
+
+* With XDP_UMEM_TX_METADATA_LEN, every TX descriptor needs that much
+  room in front of it, in its chunk. Forwarded buffers cannot carry TX
+  metadata: a buffer that RX delivered may start anywhere in a chunk,
+  and in a chunk that it shares, the room in front of it may hold the
+  packet before it, which the metadata would overwrite.
+
+* A TX completion returns the address of the descriptor, not its
+  chunk. A chunk is the application's again only once an RX
+  descriptor returned it with XDP_RX_CHUNK_RETURNED and every TX
+  descriptor that points into it is on the COMPLETION ring. Only then
+  may it go on the FILL ring.
+
 Sample application
 ==================
 There is a xdpsock benchmarking/test application that can be found at
Signature · 1 line
2.56.1