Skip to contentSkip to conversation

xsk: Zero copy through page-pool memory providers

RFC In review Björn Töpel 5 participants 38 messages Oct 2 – Oct 16

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

cover xsk: Zero copy through page-pool memory providers

Hi!

Here's an RFC for you all to enjoy with your favorite Friday $BEVERAGE.

AF_XDP zero copy needs a second receive allocator in every driver. The driver takes xdp_buff_xsk objects from the XSK buffer pool beside its page_pool path and owns their lifetime. Drivers built around page_pool and the queue management API, which already support devmem and io_uring zero-copy receive, have to duplicate their receive path.

This series makes UMEM a page-pool memory provider instead. Each aligned 4 KiB chunk is a NET_IOV_XSK net_iov. Binding a zero-copy socket installs the provider on the queue through the queue management API. The driver keeps its page_pool allocation, DMA sync, recycling and refill code. The provider consumes FILL, drops invalid and repeated addresses, and owns RX need-wakeup.

An XSKMAP redirect publishes the UMEM address when every buffer of the frame belongs to the socket's provider. XDP_PASS and other redirect targets copy into page-backed memory first. TX is unchanged; drivers read socket TX descriptors directly.

Core changes:

- MP_CAP_READABLE and MP_CAP_FRAG. A readable provider may back header and regular pools, and XDP may run on its buffers. devmem and io_uring set MP_CAP_FRAG and keep their behavior.
- Providers request RX headroom through the queue configuration.
- Readable net_iov areas. netmem_address() resolves provider memory with a load and a shift, without an indirect call.
- A refill-completion callback. The XSK provider keeps NAPI scheduled while FILL has entries, and sets NEED_WAKEUP when FILL is empty.
- Batched page-pool release for objects handed to userspace.
- xdp_buff carries the netmem and a pointer to kernel-owned shared info, so fragment metadata never lives in user-writable UMEM. It grows from 56 to 64 bytes on 64-bit.

Differences from classic zero copy:

- 4 KiB pages and aligned 4 KiB chunks only. Other layouts fail to bind with -EOPNOTSUPP, but can be supported in the future.
- Generic XDP and CPUMAP cannot deliver to a provider-backed socket.

Classic zero copy is unchanged. This does not propose converting existing drivers; it is for page_pool drivers without zero copy.

fbnic is the only driver user, +640/-122 for RX and TX. I have bnxt working with AF_XDP, plus some performance patches/fixes for the AF_XDP core on top of this -- but let's start with these patches.

Patches 1-2 fix bugs in net-next that the series hits. They are carried here so the series can be tested on its own.

1. "xdp: Size zero-copy skb heads by their contents". XDP_PASS of a zero-copy buffer copies it into an skb whose head is sized by the XSK frame size, which leaves no room for skb_shared_info. A frame that fills its buffer overwrites skb_shared_info. Provider buffers take this path on XDP_PASS, which the usual AF_XDP program returns when no socket is bound to the queue. Reproduced on fbnic in QEMU.

2. "eth: fbnic: Report the logical XDP RX queue". fbnic reports queue 0 in rx_queue_index for every queue. AF_XDP drops frames whose queue differs from the socket's, and the usual program looks up its socket by that index, so zero copy works on queue 0 only.

Feedback wanted on:

0. General thoughts on extending the page pool provider.
1. Does refill_done belong in page_pool, or should finite providers keep NAPI scheduled some other way?
2. PP_FLAG_ALLOW_UNREADABLE_NETMEM is how a pool picks up any provider, readable or not. A driver that only wants AF_XDP must set it, and then passes the core checks for devmem and io_uring too if it supports header split. Drivers that set the flag today may also split buffers, for example mlx5 through page_pool_fragment_netmem(). Only QCFG_RX_HEADROOM keeps the unsplittable XSK provider away from them. Split the flag, or let drivers declare support for unsplittable providers?
3. Is growing xdp_buff by 8 bytes acceptable?
4. How should userspace learn the chunk constraints before bind?

This is a way to move code from the drivers to the core, reducing the work for driver developers.

I hope to see you at LPC next week! Particular the netdev and bpf MC, and the AF_XDP BoF on Monday.

Björn

Björn Töpel (15):
xdp: Size zero-copy skb heads by their contents
eth: fbnic: Report the logical XDP RX queue
net: Add memory provider capabilities
net: Let memory providers set RX buffer headroom
page_pool: Extend memory provider operations
xdp: Track non-page netmem in receive buffers
xsk: Keep the DMA mapping in the buffer pool
xsk: Handle a detached FILL ring in RX wakeup
xsk: Add a page-pool memory provider for UMEM
xsk: Add RX helpers for page-pool drivers
xdp: Copy provider buffers on pass and redirect
xsk: Receive provider UMEM without copying
eth: fbnic: Support AF_XDP zero-copy receive
eth: fbnic: Support AF_XDP zero-copy transmit
Documentation: xsk: Document page-pool zero copy

 Documentation/networking/af_xdp.rst           |  65 ++
 Documentation/networking/netmem.rst           |   7 +-
 .../net/ethernet/meta/fbnic/fbnic_ethtool.c   |   5 +
 .../net/ethernet/meta/fbnic/fbnic_netdev.c    | 164 ++++-
 .../net/ethernet/meta/fbnic/fbnic_netdev.h    |   4 +
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.c  | 561 ++++++++++++---
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.h  |  24 +
 include/net/netdev_queues.h                   |   6 +
 include/net/netmem.h                          |  32 +-
 include/net/page_pool/helpers.h               |  62 +-
 include/net/page_pool/memory_provider.h       |  39 +-
 include/net/page_pool/types.h                 |   8 +-
 include/net/xdp.h                             |  64 +-
 include/net/xdp_sock.h                        |  10 +
 include/net/xdp_sock_drv.h                    |  58 +-
 include/net/xsk_buff_pool.h                   |   7 +-
 io_uring/zcrx.c                               |   4 +-
 net/core/dev.c                                |   4 +-
 net/core/dev.h                                |   5 +
 net/core/devmem.c                             |   7 +-
 net/core/filter.c                             |  28 +-
 net/core/netdev_config.c                      |   2 +
 net/core/netdev_rx_queue.c                    |  61 +-
 net/core/page_pool.c                          |  72 +-
 net/core/xdp.c                                | 112 ++-
 net/ethtool/rings.c                           |  18 +-
 net/xdp/Kconfig                               |   1 +
 net/xdp/xsk.c                                 | 253 ++++++-
 net/xdp/xsk.h                                 |  58 ++
 net/xdp/xsk_buff_pool.c                       | 667 +++++++++++++++++-
 30 files changed, 2164 insertions(+), 244 deletions(-)

base-commit: 071876fd50482a68603a9460d80dd6dd58827ee1

Signature · 1 line
2.55.0
#1 lore raw

RFC 1/15 xdp: Size zero-copy skb heads by their contents

xdp_build_skb_from_zc() sizes the skb head by the XSK frame size, which leaves no room for skb_shared_info. With 2 KiB chunks, a 1514 byte frame overwrites 48 bytes of it. Both copies also round the length up to LARGEST_ALIGN, past the end of the new buffers.

Size the head from headroom plus data, copy exactly the data, and drop frames whose head does not fit in a page.

Discovered by an AI code review agent. Reproduced on fbnic in QEMU with AF_XDP zero-copy from the page-pool series: an XDP program that adds metadata, grows the frame to its end and passes it gets UMEM bytes in the skb's shared info. A new xskxceiver test, XDP_PASS_FULL_FRAME, checks this and passes with the fix. The test will be posted separately.

Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")
---
 net/core/xdp.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
net/core/xdp.c +10 -4
index 1d679e8fd649..386240bd24c9 100644
@@ -709,7 +709,7 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 		}
 
 		memcpy(page_address(page) + offset, skb_frag_address(frag),
-		       LARGEST_ALIGN(len));
+		       len);
 		__skb_fill_page_desc_noacc(sinfo, i, page, offset, len);
 
 		tsize += truesize;
@@ -738,17 +738,23 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
  */
 struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp)
 {
+	u32 headroom = xdp->data_meta - xdp->data_hard_start;
 	const struct xdp_rxq_info *rxq = xdp->rxq;
 	u32 len = xdp->data_end - xdp->data_meta;
-	u32 truesize = xdp->frame_sz;
 	struct sk_buff *skb = NULL;
 	struct page_pool *pp;
+	u32 truesize;
 	int metalen;
 	void *data;
 
 	if (!IS_ENABLED(CONFIG_PAGE_POOL))
 		return NULL;
 
+	/* The XSK frame size leaves no room for skb_shared_info. */
+	truesize = SKB_HEAD_ALIGN(headroom + len);
+	if (unlikely(truesize > PAGE_SIZE))
+		return NULL;
+
 	local_lock_nested_bh(&system_page_pool.bh_lock);
 	pp = this_cpu_read(system_page_pool.pool);
 	data = page_pool_dev_alloc_va(pp, &truesize);
@@ -762,9 +768,9 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp)
 	}
 
 	skb_mark_for_recycle(skb);
-	skb_reserve(skb, xdp->data_meta - xdp->data_hard_start);
+	skb_reserve(skb, headroom);
 
-	memcpy(__skb_put(skb, len), xdp->data_meta, LARGEST_ALIGN(len));
+	memcpy(__skb_put(skb, len), xdp->data_meta, len);
 
 	metalen = xdp->data - xdp->data_meta;
 	if (metalen > 0) {
Signature · 1 line
2.55.0
#2 lore raw

Re: [RFC net-next 01/15] xdp: Size zero-copy skb heads by their contents

Quoted 15 lines · Björn Töpel

xdp_build_skb_from_zc() sizes the skb head by the XSK frame size, which leaves no room for skb_shared_info. With 2 KiB chunks, a 1514 byte frame overwrites 48 bytes of it. Both copies also round the length up to LARGEST_ALIGN, past the end of the new buffers.

Size the head from headroom plus data, copy exactly the data, and drop frames whose head does not fit in a page.

Discovered by an AI code review agent. Reproduced on fbnic in QEMU with AF_XDP zero-copy from the page-pool series: an XDP program that adds metadata, grows the frame to its end and passes it gets UMEM bytes in the skb's shared info. A new xskxceiver test, XDP_PASS_FULL_FRAME, checks this and passes with the fix. The test will be posted separately.

Quoted 2 lines
Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")

Late on the ball here, but I believe this is the case Clement reported and we rejected it as the reproducer was OOT module.

https://lore.kernel.org/bpf/[email protected]/

Should we proceed with Clement's fix then?

Quoted 57 lines
---
 net/core/xdp.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
net/core/xdp.c +10 -4
index 1d679e8fd649..386240bd24c9 100644
@@ -709,7 +709,7 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
 		}
 
 		memcpy(page_address(page) + offset, skb_frag_address(frag),
-		       LARGEST_ALIGN(len));
+		       len);
 		__skb_fill_page_desc_noacc(sinfo, i, page, offset, len);
 
 		tsize += truesize;
@@ -738,17 +738,23 @@ static noinline bool xdp_copy_frags_from_zc(struct sk_buff *skb,
  */
 struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp)
 {
+	u32 headroom = xdp->data_meta - xdp->data_hard_start;
 	const struct xdp_rxq_info *rxq = xdp->rxq;
 	u32 len = xdp->data_end - xdp->data_meta;
-	u32 truesize = xdp->frame_sz;
 	struct sk_buff *skb = NULL;
 	struct page_pool *pp;
+	u32 truesize;
 	int metalen;
 	void *data;
 
 	if (!IS_ENABLED(CONFIG_PAGE_POOL))
 		return NULL;
 
+	/* The XSK frame size leaves no room for skb_shared_info. */
+	truesize = SKB_HEAD_ALIGN(headroom + len);
+	if (unlikely(truesize > PAGE_SIZE))
+		return NULL;
+
 	local_lock_nested_bh(&system_page_pool.bh_lock);
 	pp = this_cpu_read(system_page_pool.pool);
 	data = page_pool_dev_alloc_va(pp, &truesize);
@@ -762,9 +768,9 @@ struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp)
 	}
 
 	skb_mark_for_recycle(skb);
-	skb_reserve(skb, xdp->data_meta - xdp->data_hard_start);
+	skb_reserve(skb, headroom);
 
-	memcpy(__skb_put(skb, len), xdp->data_meta, LARGEST_ALIGN(len));
+	memcpy(__skb_put(skb, len), xdp->data_meta, len);
 
 	metalen = xdp->data - xdp->data_meta;
 	if (metalen > 0) {
Signature · 1 line
2.55.0
#3 lore raw

Re: [RFC net-next 01/15] xdp: Size zero-copy skb heads by their contents

Quoted 19 lines · Maciej Fijalkowski
Quoted 15 lines · Björn Töpel

xdp_build_skb_from_zc() sizes the skb head by the XSK frame size, which leaves no room for skb_shared_info. With 2 KiB chunks, a 1514 byte frame overwrites 48 bytes of it. Both copies also round the length up to LARGEST_ALIGN, past the end of the new buffers.

Size the head from headroom plus data, copy exactly the data, and drop frames whose head does not fit in a page.

Discovered by an AI code review agent. Reproduced on fbnic in QEMU with AF_XDP zero-copy from the page-pool series: an XDP program that adds metadata, grows the frame to its end and passes it gets UMEM bytes in the skb's shared info. A new xskxceiver test, XDP_PASS_FULL_FRAME, checks this and passes with the fix. The test will be posted separately.

Quoted 2 lines
Fixes 560d958c6c68 ("xsk: add generic XSk &xdp_buff -> skb conversion")
Quoted 6 lines

Late on the ball here, but I believe this is the case Clement reported and we rejected it as the reproducer was OOT module.

https://lore.kernel.org/bpf/[email protected]/

Should we proceed with Clement's fix then?

I'll wake that thread up!

Björn

#4 lore raw

RFC 2/15 eth: fbnic: Report the logical XDP RX queue

In reply to Björn Töpel

XDP gives the RX queue to programs in ctx->rx_queue_index. fbnic fills it from the header buffer queue index. The header and payload buffer queues do not carry the netdev queue id, and the header queue index is zero. So XDP programs see queue 0 for packets on every queue.

Programs that act on the RX queue get the wrong value. For example, the default AF_XDP program looks up its socket with ctx->rx_queue_index, so it cannot find sockets on other queues.

Use the completion queue index. It is the netdev RX queue id, which queue steering and AF_XDP socket binding use.

Tested on fbnic hardware (50G, firmware 25.08.05-004) with AF_XDP zero-copy from the page-pool series: sockets on queue 43, and on four queues at once, receive their packets.

Fixes 894d4a4ea6cb ("eth: fbnic: move xdp_rxq_info_reg() to resource alloc")
---
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/net/ethernet/meta/fbnic/fbnic_txrx.c +1 -1
index c62d885a678a..0c7b1ea002d8 100644
@@ -2072,7 +2072,7 @@ static int fbnic_alloc_rx_qt_resources(struct fbnic_net *fbn,
 
 	fbnic_bdq_set_page_size(&qt->sub1, rx_page_size);
 
-	err = xdp_rxq_info_reg(&qt->xdp_rxq, fbn->netdev, qt->sub0.q_idx,
+	err = xdp_rxq_info_reg(&qt->xdp_rxq, fbn->netdev, qt->cmpl.q_idx,
 			       nv->napi.napi_id);
 	if (err)
 		goto free_page_pools;
Signature · 1 line
2.55.0
#5 lore raw

RFC 3/15 net: Add memory provider capabilities

In reply to Björn Töpel

The current memory providers, devmem and io_uring zero-copy RX, give memory that the CPU cannot read. The core allows them only on queues with header-data split on, a zero header-split threshold, and no XDP program. AF_XDP needs a provider that the CPU can read and that works with XDP.

Add a capability field to the provider operations, with MP_CAP_READABLE as the first flag. Apply the header-split and XDP rules only to providers without it. A queue with an AF_XDP buffer pool still rejects other providers, but it accepts the provider made from that same pool.

Provider memory can become free while a queue is restarted, and no device event reports it. After a restart, and after a failed restart is rolled back, schedule the queue's NAPI if the queue has a provider, so the driver tries to refill.

---
 Documentation/networking/netmem.rst     |  7 ++-
 include/net/page_pool/memory_provider.h | 11 +++++
 include/net/page_pool/types.h           |  7 +--
 net/core/dev.c                          |  4 +-
 net/core/netdev_rx_queue.c              | 57 ++++++++++++++++++++-----
 net/ethtool/rings.c                     | 18 +++-----
 net/xdp/xsk_buff_pool.c                 |  3 +-
 7 files changed, 78 insertions(+), 29 deletions(-)
Documentation/networking/netmem.rst +5 -2
index 217869d1108d..aacd185843ac 100644
@@ -48,8 +48,11 @@ Driver RX Requirements
    - PP_FLAG_DMA_SYNC_DEV: netmem dma addr is not necessarily dma-syncable
      by the driver. The driver must delegate the dma syncing to the page_pool,
      which knows when dma-syncing is (or is not) appropriate.
-   - PP_FLAG_ALLOW_UNREADABLE_NETMEM. The driver must specify this flag iff
-     tcp-data-split is enabled.
+   - PP_FLAG_ALLOW_UNREADABLE_NETMEM. This opts the page pool into the memory
+     provider configured on its RX queue. For a provider without
+     MP_CAP_READABLE, the driver must specify it only for a payload pool
+     with tcp-data-split enabled. A readable provider may also back a
+     regular or header pool.
 
 5. The driver must not assume the netmem is readable and/or backed by pages.
    The netmem returned by the page_pool may be unreadable, in which case
include/net/page_pool/memory_provider.h +11 -0
index 255ce4cfd975..d18ec079ffe2 100644
@@ -9,6 +9,15 @@ struct netdev_rx_queue;
 struct netlink_ext_ack;
 struct sk_buff;
 
+/**
+ * enum mp_caps - memory provider capabilities
+ * @MP_CAP_READABLE: The CPU can access provider buffers. They may back
+ *	header and regular page pools, and XDP programs may run on them.
+ */
+enum mp_caps {
+	MP_CAP_READABLE		= 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);
@@ -17,11 +26,13 @@ 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 caps;
 };
 
 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);
+bool netif_mp_lacks_cap(struct net_device *dev, u32 cap);
 
 int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 		      const struct pp_memory_provider_params *p,
include/net/page_pool/types.h +4 -3
index 03da138722f5..a96376613dda 100644
@@ -22,9 +22,10 @@
 					*/
 #define PP_FLAG_SYSTEM_POOL	BIT(2) /* Global system page_pool */
 
-/* Allow unreadable (net_iov backed) netmem in this page_pool. Drivers setting
- * this must be able to support unreadable netmem, where netmem_address() would
- * return NULL. This flag should not be set for header page_pools.
+/* Allow memory-provider (net_iov backed) netmem in this page_pool. Drivers
+ * setting this must honor the provider's capabilities. Unless the provider has
+ * MP_CAP_READABLE, netmem_address() returns NULL, and the flag must not be set
+ * for a header page_pool.
  *
  * If the driver sets PP_FLAG_ALLOW_UNREADABLE_NETMEM, it should also set
  * page_pool_params.slow.queue_idx.
net/core/dev.c +2 -2
index 5ac08da8b9d7..12b532c09a9c 100644
@@ -10423,7 +10423,7 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
 		return -EBUSY;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
+	if (netif_mp_lacks_cap(dev, MP_CAP_READABLE)) {
 		NL_SET_ERR_MSG(bpf->extack, "unable to propagate XDP to device using memory provider");
 		return -EBUSY;
 	}
@@ -10499,7 +10499,7 @@ static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
 		return -EBUSY;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
+	if (netif_mp_lacks_cap(dev, MP_CAP_READABLE)) {
 		NL_SET_ERR_MSG(extack, "unable to install XDP to device using memory provider");
 		return -EBUSY;
 	}
net/core/netdev_rx_queue.c +47 -10

101 lines not on this page · raw message

net/ethtool/rings.c +7 -11
index e3810c0320e3..789d0e328b77 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 #include <net/netdev_queues.h>
+#include <net/page_pool/memory_provider.h>
 
 #include "common.h"
 #include "netlink.h"
@@ -258,17 +259,12 @@ ethnl_set_rings(struct ethnl_req_info *req_info, struct genl_info *info)
 		return -EINVAL;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
-		if (kernel_ringparam.tcp_data_split !=
-		    ETHTOOL_TCP_DATA_SPLIT_ENABLED) {
-			NL_SET_ERR_MSG(info->extack,
-				       "can't disable tcp-data-split while device has memory provider enabled");
-			return -EINVAL;
-		} else if (kernel_ringparam.hds_thresh) {
-			NL_SET_ERR_MSG(info->extack,
-				       "can't set non-zero hds_thresh while device is memory provider enabled");
-			return -EINVAL;
-		}
+	if ((kernel_ringparam.tcp_data_split !=
+	     ETHTOOL_TCP_DATA_SPLIT_ENABLED || kernel_ringparam.hds_thresh) &&
+	    netif_mp_lacks_cap(dev, MP_CAP_READABLE)) {
+		NL_SET_ERR_MSG(info->extack,
+			       "memory provider does not support requested tcp-data-split configuration");
+		return -EINVAL;
 	}
 
 	/* ensure new ring parameters are within limits */
net/xdp/xsk_buff_pool.c +2 -1
index 9d2d94f1fb75..f01e1de7360e 100644
@@ -2,6 +2,7 @@
 
 #include <linux/netdevice.h>
 #include <net/netdev_lock.h>
+#include <net/page_pool/memory_provider.h>
 #include <net/xsk_buff_pool.h>
 #include <net/xdp_sock.h>
 #include <net/xdp_sock_drv.h>
@@ -235,7 +236,7 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
 		goto err_unreg_pool;
 	}
 
-	if (dev_get_min_mp_channel_count(netdev)) {
+	if (netif_mp_lacks_cap(netdev, MP_CAP_READABLE)) {
 		err = -EBUSY;
 		goto err_unreg_pool;
 	}
Signature · 1 line
2.55.0
#6 lore raw

Re: [RFC net-next 03/15] net: Add memory provider capabilities

Quoted 34 lines · Björn Töpel

The current memory providers, devmem and io_uring zero-copy RX, give memory that the CPU cannot read. The core allows them only on queues with header-data split on, a zero header-split threshold, and no XDP program. AF_XDP needs a provider that the CPU can read and that works with XDP.

Add a capability field to the provider operations, with MP_CAP_READABLE as the first flag. Apply the header-split and XDP rules only to providers without it. A queue with an AF_XDP buffer pool still rejects other providers, but it accepts the provider made from that same pool.

Provider memory can become free while a queue is restarted, and no device event reports it. After a restart, and after a failed restart is rolled back, schedule the queue's NAPI if the queue has a provider, so the driver tries to refill.

---
 Documentation/networking/netmem.rst     |  7 ++-
 include/net/page_pool/memory_provider.h | 11 +++++
 include/net/page_pool/types.h           |  7 +--
 net/core/dev.c                          |  4 +-
 net/core/netdev_rx_queue.c              | 57 ++++++++++++++++++++-----
 net/ethtool/rings.c                     | 18 +++-----
 net/xdp/xsk_buff_pool.c                 |  3 +-
 7 files changed, 78 insertions(+), 29 deletions(-)
Documentation/networking/netmem.rst +0 -0
index 217869d1108d..aacd185843ac 100644

Documentation/networking/netmem.rst Driver RX Requirements · line 55

Quoted 10 lines
Documentation/networking/netmem.rst +5 -2
@@ -48,8 +48,11 @@ Driver RX Requirements
    - PP_FLAG_DMA_SYNC_DEV: netmem dma addr is not necessarily dma-syncable
      by the driver. The driver must delegate the dma syncing to the page_pool,
      which knows when dma-syncing is (or is not) appropriate.
-   - PP_FLAG_ALLOW_UNREADABLE_NETMEM. The driver must specify this flag iff
-     tcp-data-split is enabled.
+   - PP_FLAG_ALLOW_UNREADABLE_NETMEM. This opts the page pool into the memory
+     provider configured on its RX queue. For a provider without
+     MP_CAP_READABLE, the driver must specify it only for a payload pool
+     with tcp-data-split enabled. A readable provider may also back a
+     regular or header pool.

I kinda don't like this doc update. This flag was never meant to say 'I support memory providers'. It's just that the existing memory providers are all unreadable so support-unreadable (headersplit) == supports-memory-providers.

The code should be updated such that if the memory provider is !MP_CAP_READABLE and driver doesn't support PP_FLAG_ALLOW_UNReADABLE_NETMEM, fail.

If another limitation applies to the new provider, add it as a separate flag. The doc is accurate as-is I think.

Quoted 42 lines
Documentation/networking/netmem.rst +0 -0
 5. The driver must not assume the netmem is readable and/or backed by pages.
    The netmem returned by the page_pool may be unreadable, in which case
include/net/page_pool/memory_provider.h +11 -0
index 255ce4cfd975..d18ec079ffe2 100644
@@ -9,6 +9,15 @@ struct netdev_rx_queue;
 struct netlink_ext_ack;
 struct sk_buff;
 
+/**
+ * enum mp_caps - memory provider capabilities
+ * @MP_CAP_READABLE: The CPU can access provider buffers. They may back
+ *     header and regular page pools, and XDP programs may run on them.
+ */
+enum mp_caps {
+       MP_CAP_READABLE         = 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);
@@ -17,11 +26,13 @@ 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 caps;
 };
 
 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);
+bool netif_mp_lacks_cap(struct net_device *dev, u32 cap);
 
 int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
                      const struct pp_memory_provider_params *p,
include/net/page_pool/types.h +0 -0
index 03da138722f5..a96376613dda 100644

include/net/page_pool/types.h line 25

Quoted 7 lines
include/net/page_pool/types.h +1 -3
@@ -22,9 +22,10 @@
                                        */
 #define PP_FLAG_SYSTEM_POOL    BIT(2) /* Global system page_pool */
 
-/* Allow unreadable (net_iov backed) netmem in this page_pool. Drivers setting
- * this must be able to support unreadable netmem, where netmem_address() would
- * return NULL. This flag should not be set for header page_pools.
+/* Allow memory-provider (net_iov backed) netmem in this page_pool. Drivers

Comment is slightly inaccurate. It implies memory-providers can only-even be net_iov backed. There is nothing wrong with a memory provider returning page-backed-netmems. Mke it something like:

Allow unreadable memory-providers in this page_pool. Drivers that support this must support unreadable netmem...

Have your pet LLM please go over the code for any instances in the code/comments where we assumed net_iov == unreadable and fix those with checks.

Here are the places my pet LLM thinks you missed:

### Handled by the Series

• netmem_address(): Returns net_iov_address() instead of NULL for
readable net_iovs.
• page_pool_is_unreadable() & netif_rxq_has_unreadable_mp(): Check
!(ops->caps & MP_CAP_READABLE).
• xdp_buff_add_frag(): Only sets XDP_FLAGS_FRAGS_UNREADABLE when
!net_iov_is_readable(niov).

### Missed by the Series (Feedback to Add)

1. xdp_build_skb_from_buff() (page head + readable net_iov frags):
Only checks xdp_buff_has_netmem(xdp) (head buffer), and xdp_buff_add_frag() no longer sets XDP_FLAGS_FRAGS_UNREADABLE for readable net_iovs.
A packet with a page-backed head and NET_IOV_XSK frags won't be
copied and will leak NET_IOV_XSK into an skb with skb->unreadable = 0.
2. skb_frag_address() & skb_frag_address_safe(): Still return NULL
if !skb_frag_page(frag). Patch 11 added a duplicate xdp_frag_address() for 6 call sites, leaving other XDP frag readers (bpf_test_finish(),
driver multi-buffer XDP_TX like mlx5e_xdp_mpwqe_add_dseg()) broken.
skb_frag_address() itself should use netmem_address().
3. __skb_fill_netmem_desc() & skb_dump(): Still treat all net_iovs
as unreadable (skb->unreadable = true). Also, drivers that build skbs via skb_add_rx_frag_netmem() when !xdp_prog will attach NET_IOV_XSK
directly to skbs (which __get_netmem() / __put_netmem() don't refcount).
4. bnxt & bnge need_head_pool: Set rxr->need_head_pool =
page_pool_is_unreadable(pool) to decide whether head_pool needs a separate struct page pool, then call page-only page_pool_alloc_frag() and
page_pool_free_va() on head_pool. Returning false from
page_pool_is_unreadable() breaks them.
5. Drivers checking netmem_is_net_iov() as "unreadable":
• gve_rx_dqo.c:892, 953: Uses netmem_is_net_iov() to drop
unsplit headers and skip copybreak; should check !netmem_address().
• en_rx.c:2282-2286: Uses netmem_is_net_iov() to drop unsplit
headers; should check !netmem_address().
• en_main.c:5694-5705: Uses netif_rxq_has_unreadable_mp() to
validate custom page size; should be netif_rxq_has_mp().
• idpf_txrx.c:3470-3479: Uses netmem_is_net_iov() and
__netmem_to_page() in idpf_rx_hsplit_wa(); should use netmem_address().
6. Stale comments/docs:
• netmem.h:73-83: Comment on struct net_iov still says it is
only for non-struct page memory and fixed PAGE_SIZE chunks.
• netmem.rst:27 & types.h:32: Still state tcp-data-split is
unconditionally required and keep the PP_FLAG_ALLOW_UNREADABLE_NETMEM name for readable providers.

include/net/page_pool/types.h line 29

Quoted 4 lines
include/net/page_pool/types.h +3 -0
+ * setting this must honor the provider's capabilities. Unless the provider has
+ * MP_CAP_READABLE, netmem_address() returns NULL, and the flag must not be set
+ * for a header page_pool.
  *

That bit about header page_pool is good to add anyway.

net/core/dev.c int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf) · line 10426

Quoted 12 lines
include/net/page_pool/types.h +0 -0
  * If the driver sets PP_FLAG_ALLOW_UNREADABLE_NETMEM, it should also set
  * page_pool_params.slow.queue_idx.
net/core/dev.c +1 -1
index 5ac08da8b9d7..12b532c09a9c 100644
@@ -10423,7 +10423,7 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
                return -EBUSY;
        }
 
-       if (dev_get_min_mp_channel_count(dev)) {
+       if (netif_mp_lacks_cap(dev, MP_CAP_READABLE)) {

Could use a name easier to read?

if (!netif_all_mps_have_cap())

net/core/dev.c int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf) · line 10427

Quoted 1 line
net/core/dev.c +0 -0
                NL_SET_ERR_MSG(bpf->extack, "unable to propagate XDP to device using memory provider");

...using unreadable memory provider");

net/core/dev.c static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode, · line 10503

Quoted 9 lines
net/core/dev.c +1 -1
                return -EBUSY;
        }
@@ -10499,7 +10499,7 @@ static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
                return -EBUSY;
        }
 
-       if (dev_get_min_mp_channel_count(dev)) {
+       if (netif_mp_lacks_cap(dev, MP_CAP_READABLE)) {
                NL_SET_ERR_MSG(extack, "unable to install XDP to device using memory provider");

Same.

Quoted 8 lines
net/core/dev.c +0 -0
                return -EBUSY;
        }
net/core/netdev_rx_queue.c +0 -0
index 00a7011eb4d5..610e31d1a717 100644
@@ -82,8 +82,12 @@ __netif_get_rx_queue_lease(struct net_device **dev, unsigned int *rxq_idx,
 /* See also page_pool_is_unreadable() */

net/core/netdev_rx_queue.c __netif_get_rx_queue_lease(struct net_device **dev, unsigned int *rxq_idx, · line 90

Quoted 10 lines
net/core/netdev_rx_queue.c +6 -2
 bool netif_rxq_has_unreadable_mp(struct net_device *dev, unsigned int rxq_idx)
 {
-       if (rxq_idx < dev->real_num_rx_queues)
-               return __netif_get_rx_queue(dev, rxq_idx)->mp_params.mp_ops;
+       const struct memory_provider_ops *ops;
+
+       if (rxq_idx < dev->real_num_rx_queues) {
+               ops = __netif_get_rx_queue(dev, rxq_idx)->mp_params.mp_ops;
+               return ops && !(ops->caps & MP_CAP_READABLE);
+       }

handle error case inside if() block please.

Quoted 23 lines
net/core/netdev_rx_queue.c +16 -0
        return false;
 }
 EXPORT_SYMBOL(netif_rxq_has_unreadable_mp);
@@ -95,6 +99,32 @@ bool netif_rxq_has_mp(struct net_device *dev, unsigned int rxq_idx)
        return false;
 }
 
+/* Return true if an installed memory provider lacks @cap. */
+bool netif_mp_lacks_cap(struct net_device *dev, u32 cap)
+{
+       const struct memory_provider_ops *ops;
+       int i;
+
+       netdev_assert_locked_ops_compat(dev);
+
+       for (i = dev->real_num_rx_queues - 1; i >= 0; i--) {
+               ops = dev->_rx[i].mp_params.mp_ops;
+               if (ops && (ops->caps & cap) != cap)
+                       return true;
+       }
+
+       return false;
+}

net/core/netdev_rx_queue.c bool netif_rxq_has_mp(struct net_device *dev, unsigned int rxq_idx) · line 127

Quoted 10 lines
net/core/netdev_rx_queue.c +10 -0
+
+/* Provider memory may become available while the queue is replaced,
+ * without a device event to report it.
+ */
+static void netdev_rx_queue_kick(struct netdev_rx_queue *rxq)
+{
+       if (rxq->mp_params.mp_ops && rxq->napi)
+               napi_schedule(rxq->napi);
+}
+

Honestly maybe open code this at the call site. It's confusing to have a hepler that doesn't mention mp only do something if the rx queue has an mp configured. Or maybe netdev_mp_rx_queue_kick().

Quoted 13 lines
net/core/netdev_rx_queue.c +2 -0
 static int netdev_rx_queue_reconfig(struct net_device *dev,
                                    unsigned int rxq_idx,
                                    struct netdev_queue_config *qcfg_old,
@@ -142,6 +172,8 @@ static int netdev_rx_queue_reconfig(struct net_device *dev,
        }
 
        qops->ndo_queue_mem_free(dev, old_mem);
+       if (netif_running(dev))
+               netdev_rx_queue_kick(rxq);
 
        kvfree(old_mem);
        kvfree(new_mem);

net/core/netdev_rx_queue.c static int netdev_rx_queue_reconfig(struct net_device *dev, · line 204

Quoted 8 lines
net/core/netdev_rx_queue.c +5 -0
@@ -165,6 +197,11 @@ static int netdev_rx_queue_reconfig(struct net_device *dev,
 
 err_free_new_queue_mem:
        qops->ndo_queue_mem_free(dev, new_mem);
+       /* Freeing the replacement can hand provider memory back to the old
+        * queue, which still runs unless the rollback failed.
+        */
+       if (err != -ENETDOWN && netif_running(dev))
+               netdev_rx_queue_kick(rxq);

These kicks feel extremely error prone and hard to point where in the code we missed a necessary kick. Try to find something better to do.

Quoted 12 lines
net/core/netdev_rx_queue.c +1 -0
 err_free_old_mem:
        kvfree(old_mem);
@@ -189,6 +226,7 @@ static int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
                               struct netlink_ext_ack *extack)
 {
        const struct netdev_queue_mgmt_ops *qops = dev->queue_mgmt_ops;
+       u32 caps = p->mp_ops->caps;
        struct netdev_queue_config qcfg[2];
        struct netdev_rx_queue *rxq;
        int ret;

net/core/netdev_rx_queue.c static int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx, · line 238

Quoted 7 lines
net/core/netdev_rx_queue.c +2 -2
@@ -196,15 +234,13 @@ static int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
        if (!qops)
                return -EOPNOTSUPP;
 
-       if (dev->cfg->hds_config != ETHTOOL_TCP_DATA_SPLIT_ENABLED) {
-               NL_SET_ERR_MSG(extack, "tcp-data-split is disabled");
+       if ((dev->cfg->hds_config != ETHTOOL_TCP_DATA_SPLIT_ENABLED ||
+            dev->cfg->hds_thresh) && !(caps & MP_CAP_READABLE)) {

nit: This could use a helper mp_caps_readable() or something.

net/core/netdev_rx_queue.c static int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx, · line 244

Quoted 11 lines
net/core/netdev_rx_queue.c +3 -5
+               NL_SET_ERR_MSG(extack,
+                              "memory provider does not support current tcp-data-split configuration");
                return -EINVAL;
        }
-       if (dev->cfg->hds_thresh) {
-               NL_SET_ERR_MSG(extack, "hds-thresh is not zero");
-               return -EINVAL;
-       }
-       if (dev_xdp_prog_count(dev)) {
+       if (dev_xdp_prog_count(dev) && !(caps & MP_CAP_READABLE)) {
                NL_SET_ERR_MSG(extack, "unable to custom memory provider to device with XDP program attached");

unable to use unreadable memory provider with XDP program attached maybe.

Quoted 72 lines
net/core/netdev_rx_queue.c +2 -1
                return -EEXIST;
        }
@@ -219,7 +255,8 @@ static int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
                return -EEXIST;
        }
 #ifdef CONFIG_XDP_SOCKETS
-       if (rxq->pool) {
+       /* An AF_XDP pool owns the queue unless it is this provider. */
+       if (rxq->pool && rxq->pool != p->mp_priv) {
                NL_SET_ERR_MSG(extack, "designated queue already in use by AF_XDP");
                return -EBUSY;
        }
net/ethtool/rings.c +7 -11
index e3810c0320e3..789d0e328b77 100644
@@ -1,6 +1,7 @@
 // SPDX-License-Identifier: GPL-2.0-only
 
 #include <net/netdev_queues.h>
+#include <net/page_pool/memory_provider.h>
 
 #include "common.h"
 #include "netlink.h"
@@ -258,17 +259,12 @@ ethnl_set_rings(struct ethnl_req_info *req_info, struct genl_info *info)
                return -EINVAL;
        }
 
-       if (dev_get_min_mp_channel_count(dev)) {
-               if (kernel_ringparam.tcp_data_split !=
-                   ETHTOOL_TCP_DATA_SPLIT_ENABLED) {
-                       NL_SET_ERR_MSG(info->extack,
-                                      "can't disable tcp-data-split while device has memory provider enabled");
-                       return -EINVAL;
-               } else if (kernel_ringparam.hds_thresh) {
-                       NL_SET_ERR_MSG(info->extack,
-                                      "can't set non-zero hds_thresh while device is memory provider enabled");
-                       return -EINVAL;
-               }
+       if ((kernel_ringparam.tcp_data_split !=
+            ETHTOOL_TCP_DATA_SPLIT_ENABLED || kernel_ringparam.hds_thresh) &&
+           netif_mp_lacks_cap(dev, MP_CAP_READABLE)) {
+               NL_SET_ERR_MSG(info->extack,
+                              "memory provider does not support requested tcp-data-split configuration");
+               return -EINVAL;
        }
 
        /* ensure new ring parameters are within limits */
net/xdp/xsk_buff_pool.c +2 -2
index 9d2d94f1fb75..f01e1de7360e 100644
@@ -2,6 +2,7 @@
 
 #include <linux/netdevice.h>
 #include <net/netdev_lock.h>
+#include <net/page_pool/memory_provider.h>
 #include <net/xsk_buff_pool.h>
 #include <net/xdp_sock.h>
 #include <net/xdp_sock_drv.h>
@@ -235,7 +236,7 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
                goto err_unreg_pool;
        }
 
-       if (dev_get_min_mp_channel_count(netdev)) {
+       if (netif_mp_lacks_cap(netdev, MP_CAP_READABLE)) {
                err = -EBUSY;
                goto err_unreg_pool;
        }
--

2.55.0

Signature · 2 lines
Thanks,
Mina
#7 lore raw

Re: [RFC net-next 03/15] net: Add memory provider capabilities

Mina Almasry <[email protected]> writes:

[...]

Quoted 2 lines

Thanks,
Mina

Mina (back from LPC, sorry about the delay...)!

Thanks for spending time on the series! I'll try to address all your comments! Need to think about the napi kicking a bit more...

Björn

#8 lore raw

Re: [RFC net-next 03/15] net: Add memory provider capabilities

In reply to Björn Töpel
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] net: Use-After-Free in NAPI poll due to erroneous kick after failed queue rollback --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] net: Use-After-Free in NAPI poll due to erroneous kick after failed queue rollback --

commit 6916b55bc2fd7fcbee42e2cf1fc4f277ee96ed12
Author: Björn Töpel <[email protected]>

net: Add memory provider capabilities

The current memory providers, devmem and io_uring zero-copy RX, give memory that the CPU cannot read. The core allows them only on queues with header-data split on, a zero header-split threshold, and no XDP program. AF_XDP needs a provider that the CPU can read and that works with XDP.

Quoted 4 lines
net/core/netdev_rx_queue.c +0 -0
index 00a7011eb4d57..610e31d1a7173 100644

[ ... ]

net/core/netdev_rx_queue.c static int netdev_rx_queue_reconfig(struct net_device *dev, · line 207

Quoted 12 lines
net/core/netdev_rx_queue.c +5 -0
@@ -165,6 +197,11 @@ static int netdev_rx_queue_reconfig(struct net_device *dev,
 
 err_free_new_queue_mem:
 	qops->ndo_queue_mem_free(dev, new_mem);
+	/* Freeing the replacement can hand provider memory back to the old
+	 * queue, which still runs unless the rollback failed.
+	 */
+	if (err != -ENETDOWN && netif_running(dev))
+		netdev_rx_queue_kick(rxq);
 
 err_free_old_mem:
 	kvfree(old_mem);

[Severity: High]
Does this code risk a use-after-free if the queue rollback fails?

If ndo_queue_start() fails during the rollback, the old memory is freed, but err is not updated to reflect the failure:

net/core/netdev_rx_queue.c:netdev_rx_queue_reconfig() { err_start_queue:

	if (qops->ndo_queue_start(dev, qcfg_old, old_mem, rxq_idx)) {
		WARN(1, ...);
		qops->ndo_queue_mem_free(dev, old_mem);
	}

err_free_new_queue_mem:

	qops->ndo_queue_mem_free(dev, new_mem);
	/* err still holds its previous value (e.g., -ENOMEM) */
	if (err != -ENETDOWN && netif_running(dev))
		netdev_rx_queue_kick(rxq);

Because err isn't updated to -ENETDOWN when the rollback fails, the kick will proceed and schedule a NAPI poll on a stopped queue whose memory was just freed.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#9 lore raw

Re: [RFC net-next 03/15] net: Add memory provider capabilities

Quoted 47 lines · [email protected] writes:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] net: Use-After-Free in NAPI poll due to erroneous kick after failed queue rollback --

commit 6916b55bc2fd7fcbee42e2cf1fc4f277ee96ed12
Author: Björn Töpel <[email protected]>

net: Add memory provider capabilities

The current memory providers, devmem and io_uring zero-copy RX, give memory that the CPU cannot read. The core allows them only on queues with header-data split on, a zero header-split threshold, and no XDP program. AF_XDP needs a provider that the CPU can read and that works with XDP.

Quoted 4 lines
net/core/netdev_rx_queue.c +0 -0
index 00a7011eb4d57..610e31d1a7173 100644

[ ... ]

net/core/netdev_rx_queue.c static int netdev_rx_queue_reconfig(struct net_device *dev, · line 207

Quoted 12 lines
net/core/netdev_rx_queue.c +5 -0
@@ -165,6 +197,11 @@ static int netdev_rx_queue_reconfig(struct net_device *dev,
 
 err_free_new_queue_mem:
 	qops->ndo_queue_mem_free(dev, new_mem);
+	/* Freeing the replacement can hand provider memory back to the old
+	 * queue, which still runs unless the rollback failed.
+	 */
+	if (err != -ENETDOWN && netif_running(dev))
+		netdev_rx_queue_kick(rxq);
 
 err_free_old_mem:
 	kvfree(old_mem);

[Severity: High]
Does this code risk a use-after-free if the queue rollback fails?

If ndo_queue_start() fails during the rollback, the old memory is freed, but err is not updated to reflect the failure:

net/core/netdev_rx_queue.c:netdev_rx_queue_reconfig() { err_start_queue:

	if (qops->ndo_queue_start(dev, qcfg_old, old_mem, rxq_idx)) {
		WARN(1, ...);
		qops->ndo_queue_mem_free(dev, old_mem);
	}

err_free_new_queue_mem:

Quoted 8 lines
	qops->ndo_queue_mem_free(dev, new_mem);
	/* err still holds its previous value (e.g., -ENOMEM) */
	if (err != -ENETDOWN && netif_running(dev))
		netdev_rx_queue_kick(rxq);

Because err isn't updated to -ENETDOWN when the rollback fails, the kick will proceed and schedule a NAPI poll on a stopped queue whose memory was just freed.

The error check doesn't track rollback success. I'll fix that.

#10 lore raw

Re: [RFC net-next 03/15] net: Add memory provider capabilities

In reply to Björn Töpel
Quoted 172 lines · Björn Töpel

The current memory providers, devmem and io_uring zero-copy RX, give memory that the CPU cannot read. The core allows them only on queues with header-data split on, a zero header-split threshold, and no XDP program. AF_XDP needs a provider that the CPU can read and that works with XDP.

Add a capability field to the provider operations, with MP_CAP_READABLE as the first flag. Apply the header-split and XDP rules only to providers without it. A queue with an AF_XDP buffer pool still rejects other providers, but it accepts the provider made from that same pool.

Provider memory can become free while a queue is restarted, and no device event reports it. After a restart, and after a failed restart is rolled back, schedule the queue's NAPI if the queue has a provider, so the driver tries to refill.

---
 Documentation/networking/netmem.rst     |  7 ++-
 include/net/page_pool/memory_provider.h | 11 +++++
 include/net/page_pool/types.h           |  7 +--
 net/core/dev.c                          |  4 +-
 net/core/netdev_rx_queue.c              | 57 ++++++++++++++++++++-----
 net/ethtool/rings.c                     | 18 +++-----
 net/xdp/xsk_buff_pool.c                 |  3 +-
 7 files changed, 78 insertions(+), 29 deletions(-)
Documentation/networking/netmem.rst +5 -2
index 217869d1108d..aacd185843ac 100644
@@ -48,8 +48,11 @@ Driver RX Requirements
    - PP_FLAG_DMA_SYNC_DEV: netmem dma addr is not necessarily dma-syncable
      by the driver. The driver must delegate the dma syncing to the page_pool,
      which knows when dma-syncing is (or is not) appropriate.
-   - PP_FLAG_ALLOW_UNREADABLE_NETMEM. The driver must specify this flag iff
-     tcp-data-split is enabled.
+   - PP_FLAG_ALLOW_UNREADABLE_NETMEM. This opts the page pool into the memory
+     provider configured on its RX queue. For a provider without
+     MP_CAP_READABLE, the driver must specify it only for a payload pool
+     with tcp-data-split enabled. A readable provider may also back a
+     regular or header pool.
 
 5. The driver must not assume the netmem is readable and/or backed by pages.
    The netmem returned by the page_pool may be unreadable, in which case
include/net/page_pool/memory_provider.h +11 -0
index 255ce4cfd975..d18ec079ffe2 100644
@@ -9,6 +9,15 @@ struct netdev_rx_queue;
 struct netlink_ext_ack;
 struct sk_buff;
 
+/**
+ * enum mp_caps - memory provider capabilities
+ * @MP_CAP_READABLE: The CPU can access provider buffers. They may back
+ *	header and regular page pools, and XDP programs may run on them.
+ */
+enum mp_caps {
+	MP_CAP_READABLE		= 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);
@@ -17,11 +26,13 @@ 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 caps;
 };
 
 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);
+bool netif_mp_lacks_cap(struct net_device *dev, u32 cap);
 
 int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 		      const struct pp_memory_provider_params *p,
include/net/page_pool/types.h +4 -3
index 03da138722f5..a96376613dda 100644
@@ -22,9 +22,10 @@
 					*/
 #define PP_FLAG_SYSTEM_POOL	BIT(2) /* Global system page_pool */
 
-/* Allow unreadable (net_iov backed) netmem in this page_pool. Drivers setting
- * this must be able to support unreadable netmem, where netmem_address() would
- * return NULL. This flag should not be set for header page_pools.
+/* Allow memory-provider (net_iov backed) netmem in this page_pool. Drivers
+ * setting this must honor the provider's capabilities. Unless the provider has
+ * MP_CAP_READABLE, netmem_address() returns NULL, and the flag must not be set
+ * for a header page_pool.
  *
  * If the driver sets PP_FLAG_ALLOW_UNREADABLE_NETMEM, it should also set
  * page_pool_params.slow.queue_idx.
net/core/dev.c +2 -2
index 5ac08da8b9d7..12b532c09a9c 100644
@@ -10423,7 +10423,7 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
 		return -EBUSY;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
+	if (netif_mp_lacks_cap(dev, MP_CAP_READABLE)) {
 		NL_SET_ERR_MSG(bpf->extack, "unable to propagate XDP to device using memory provider");
 		return -EBUSY;
 	}
@@ -10499,7 +10499,7 @@ static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
 		return -EBUSY;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
+	if (netif_mp_lacks_cap(dev, MP_CAP_READABLE)) {
 		NL_SET_ERR_MSG(extack, "unable to install XDP to device using memory provider");
 		return -EBUSY;
 	}
net/core/netdev_rx_queue.c +32 -2
index 00a7011eb4d5..610e31d1a717 100644
@@ -82,8 +82,12 @@ __netif_get_rx_queue_lease(struct net_device **dev, unsigned int *rxq_idx,
 /* See also page_pool_is_unreadable() */
 bool netif_rxq_has_unreadable_mp(struct net_device *dev, unsigned int rxq_idx)
 {
-	if (rxq_idx < dev->real_num_rx_queues)
-		return __netif_get_rx_queue(dev, rxq_idx)->mp_params.mp_ops;
+	const struct memory_provider_ops *ops;
+
+	if (rxq_idx < dev->real_num_rx_queues) {
+		ops = __netif_get_rx_queue(dev, rxq_idx)->mp_params.mp_ops;
+		return ops && !(ops->caps & MP_CAP_READABLE);
+	}
 	return false;
 }
 EXPORT_SYMBOL(netif_rxq_has_unreadable_mp);
@@ -95,6 +99,32 @@ bool netif_rxq_has_mp(struct net_device *dev, unsigned int rxq_idx)
 	return false;
 }
 
+/* Return true if an installed memory provider lacks @cap. */
+bool netif_mp_lacks_cap(struct net_device *dev, u32 cap)
+{
+	const struct memory_provider_ops *ops;
+	int i;
+
+	netdev_assert_locked_ops_compat(dev);
+
+	for (i = dev->real_num_rx_queues - 1; i >= 0; i--) {
+		ops = dev->_rx[i].mp_params.mp_ops;
+		if (ops && (ops->caps & cap) != cap)
+			return true;
+	}
+
+	return false;
+}
+
+/* Provider memory may become available while the queue is replaced,
+ * without a device event to report it.
+ */
+static void netdev_rx_queue_kick(struct netdev_rx_queue *rxq)
+{
+	if (rxq->mp_params.mp_ops && rxq->napi)
+		napi_schedule(rxq->napi);
+}
+
 static int netdev_rx_queue_reconfig(struct net_device *dev,
 				    unsigned int rxq_idx,
 				    struct netdev_queue_config *qcfg_old,

[..]

Quoted 11 lines
net/core/netdev_rx_queue.c +2 -0
@@ -142,6 +172,8 @@ static int netdev_rx_queue_reconfig(struct net_device *dev,
 	}
 
 	qops->ndo_queue_mem_free(dev, old_mem);
+	if (netif_running(dev))
+		netdev_rx_queue_kick(rxq);
 
 	kvfree(old_mem);
 	kvfree(new_mem);
@@ -165,6 +197,11 @@ static int netdev_rx_queue_reconfig(struct net_device *dev,
 

net/core/netdev_rx_queue.c static int netdev_rx_queue_reconfig(struct net_device *dev, · line 207

Quoted 10 lines
net/core/netdev_rx_queue.c +5 -0
 err_free_new_queue_mem:
 	qops->ndo_queue_mem_free(dev, new_mem);
+	/* Freeing the replacement can hand provider memory back to the old
+	 * queue, which still runs unless the rollback failed.
+	 */
+	if (err != -ENETDOWN && netif_running(dev))
+		netdev_rx_queue_kick(rxq);
 
 err_free_old_mem:
 	kvfree(old_mem);

These are separate fixes?

#11 lore raw

Re: [RFC net-next 03/15] net: Add memory provider capabilities

Quoted 195 lines · Stanislav Fomichev
Quoted 172 lines · Björn Töpel

The current memory providers, devmem and io_uring zero-copy RX, give memory that the CPU cannot read. The core allows them only on queues with header-data split on, a zero header-split threshold, and no XDP program. AF_XDP needs a provider that the CPU can read and that works with XDP.

Add a capability field to the provider operations, with MP_CAP_READABLE as the first flag. Apply the header-split and XDP rules only to providers without it. A queue with an AF_XDP buffer pool still rejects other providers, but it accepts the provider made from that same pool.

Provider memory can become free while a queue is restarted, and no device event reports it. After a restart, and after a failed restart is rolled back, schedule the queue's NAPI if the queue has a provider, so the driver tries to refill.

---
 Documentation/networking/netmem.rst     |  7 ++-
 include/net/page_pool/memory_provider.h | 11 +++++
 include/net/page_pool/types.h           |  7 +--
 net/core/dev.c                          |  4 +-
 net/core/netdev_rx_queue.c              | 57 ++++++++++++++++++++-----
 net/ethtool/rings.c                     | 18 +++-----
 net/xdp/xsk_buff_pool.c                 |  3 +-
 7 files changed, 78 insertions(+), 29 deletions(-)
Documentation/networking/netmem.rst +5 -2
index 217869d1108d..aacd185843ac 100644
@@ -48,8 +48,11 @@ Driver RX Requirements
    - PP_FLAG_DMA_SYNC_DEV: netmem dma addr is not necessarily dma-syncable
      by the driver. The driver must delegate the dma syncing to the page_pool,
      which knows when dma-syncing is (or is not) appropriate.
-   - PP_FLAG_ALLOW_UNREADABLE_NETMEM. The driver must specify this flag iff
-     tcp-data-split is enabled.
+   - PP_FLAG_ALLOW_UNREADABLE_NETMEM. This opts the page pool into the memory
+     provider configured on its RX queue. For a provider without
+     MP_CAP_READABLE, the driver must specify it only for a payload pool
+     with tcp-data-split enabled. A readable provider may also back a
+     regular or header pool.
 
 5. The driver must not assume the netmem is readable and/or backed by pages.
    The netmem returned by the page_pool may be unreadable, in which case
include/net/page_pool/memory_provider.h +11 -0
index 255ce4cfd975..d18ec079ffe2 100644
@@ -9,6 +9,15 @@ struct netdev_rx_queue;
 struct netlink_ext_ack;
 struct sk_buff;
 
+/**
+ * enum mp_caps - memory provider capabilities
+ * @MP_CAP_READABLE: The CPU can access provider buffers. They may back
+ *	header and regular page pools, and XDP programs may run on them.
+ */
+enum mp_caps {
+	MP_CAP_READABLE		= 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);
@@ -17,11 +26,13 @@ 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 caps;
 };
 
 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);
+bool netif_mp_lacks_cap(struct net_device *dev, u32 cap);
 
 int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 		      const struct pp_memory_provider_params *p,
include/net/page_pool/types.h +4 -3
index 03da138722f5..a96376613dda 100644
@@ -22,9 +22,10 @@
 					*/
 #define PP_FLAG_SYSTEM_POOL	BIT(2) /* Global system page_pool */
 
-/* Allow unreadable (net_iov backed) netmem in this page_pool. Drivers setting
- * this must be able to support unreadable netmem, where netmem_address() would
- * return NULL. This flag should not be set for header page_pools.
+/* Allow memory-provider (net_iov backed) netmem in this page_pool. Drivers
+ * setting this must honor the provider's capabilities. Unless the provider has
+ * MP_CAP_READABLE, netmem_address() returns NULL, and the flag must not be set
+ * for a header page_pool.
  *
  * If the driver sets PP_FLAG_ALLOW_UNREADABLE_NETMEM, it should also set
  * page_pool_params.slow.queue_idx.
net/core/dev.c +2 -2
index 5ac08da8b9d7..12b532c09a9c 100644
@@ -10423,7 +10423,7 @@ int netif_xdp_propagate(struct net_device *dev, struct netdev_bpf *bpf)
 		return -EBUSY;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
+	if (netif_mp_lacks_cap(dev, MP_CAP_READABLE)) {
 		NL_SET_ERR_MSG(bpf->extack, "unable to propagate XDP to device using memory provider");
 		return -EBUSY;
 	}
@@ -10499,7 +10499,7 @@ static int dev_xdp_install(struct net_device *dev, enum bpf_xdp_mode mode,
 		return -EBUSY;
 	}
 
-	if (dev_get_min_mp_channel_count(dev)) {
+	if (netif_mp_lacks_cap(dev, MP_CAP_READABLE)) {
 		NL_SET_ERR_MSG(extack, "unable to install XDP to device using memory provider");
 		return -EBUSY;
 	}
net/core/netdev_rx_queue.c +32 -2
index 00a7011eb4d5..610e31d1a717 100644
@@ -82,8 +82,12 @@ __netif_get_rx_queue_lease(struct net_device **dev, unsigned int *rxq_idx,
 /* See also page_pool_is_unreadable() */
 bool netif_rxq_has_unreadable_mp(struct net_device *dev, unsigned int rxq_idx)
 {
-	if (rxq_idx < dev->real_num_rx_queues)
-		return __netif_get_rx_queue(dev, rxq_idx)->mp_params.mp_ops;
+	const struct memory_provider_ops *ops;
+
+	if (rxq_idx < dev->real_num_rx_queues) {
+		ops = __netif_get_rx_queue(dev, rxq_idx)->mp_params.mp_ops;
+		return ops && !(ops->caps & MP_CAP_READABLE);
+	}
 	return false;
 }
 EXPORT_SYMBOL(netif_rxq_has_unreadable_mp);
@@ -95,6 +99,32 @@ bool netif_rxq_has_mp(struct net_device *dev, unsigned int rxq_idx)
 	return false;
 }
 
+/* Return true if an installed memory provider lacks @cap. */
+bool netif_mp_lacks_cap(struct net_device *dev, u32 cap)
+{
+	const struct memory_provider_ops *ops;
+	int i;
+
+	netdev_assert_locked_ops_compat(dev);
+
+	for (i = dev->real_num_rx_queues - 1; i >= 0; i--) {
+		ops = dev->_rx[i].mp_params.mp_ops;
+		if (ops && (ops->caps & cap) != cap)
+			return true;
+	}
+
+	return false;
+}
+
+/* Provider memory may become available while the queue is replaced,
+ * without a device event to report it.
+ */
+static void netdev_rx_queue_kick(struct netdev_rx_queue *rxq)
+{
+	if (rxq->mp_params.mp_ops && rxq->napi)
+		napi_schedule(rxq->napi);
+}
+
 static int netdev_rx_queue_reconfig(struct net_device *dev,
 				    unsigned int rxq_idx,
 				    struct netdev_queue_config *qcfg_old,

[..]

Quoted 19 lines
net/core/netdev_rx_queue.c +7 -0
@@ -142,6 +172,8 @@ static int netdev_rx_queue_reconfig(struct net_device *dev,
 	}
 
 	qops->ndo_queue_mem_free(dev, old_mem);
+	if (netif_running(dev))
+		netdev_rx_queue_kick(rxq);
 
 	kvfree(old_mem);
 	kvfree(new_mem);
@@ -165,6 +197,11 @@ static int netdev_rx_queue_reconfig(struct net_device *dev,
 
 err_free_new_queue_mem:
 	qops->ndo_queue_mem_free(dev, new_mem);
+	/* Freeing the replacement can hand provider memory back to the old
+	 * queue, which still runs unless the rollback failed.
+	 */
+	if (err != -ENETDOWN && netif_running(dev))
+		netdev_rx_queue_kick(rxq);
 
Quoted 4 lines

net/core/netdev_rx_queue.c static int netdev_rx_queue_reconfig(struct net_device *dev, · line 207

Quoted 2 lines
net/core/netdev_rx_queue.c +0 -0
 err_free_old_mem:
 	kvfree(old_mem);

These are separate fixes?

Thanks for taking a look!

The old pool can hold buffers the new pool needs. We need another refill after those buffers return. I'll split that from the capabilities patch.

Björn

#12 lore raw

RFC 4/15 net: Let memory providers set RX buffer headroom

In reply to Björn Töpel

AF_XDP lets userspace choose a headroom in front of the packet data in each UMEM chunk, and the kernel adds XDP_PACKET_HEADROOM to it. A driver whose buffers come from an AF_XDP memory provider must put packets after this headroom. The driver sees only the page pool, so it has no generic way to learn the value, and nothing checks that the driver supports it.

Add rx_headroom to the provider parameters and to the queue configuration. It is the full headroom, including XDP_PACKET_HEADROOM; zero means the driver default. Add QCFG_RX_HEADROOM. A provider that sets a headroom can only be installed on a queue that supports it; otherwise the install fails with -EOPNOTSUPP. The driver checks the value with the rest of the queue configuration.

---
 include/net/netdev_queues.h   | 6 ++++++
 include/net/page_pool/types.h | 1 +
 net/core/netdev_config.c      | 2 ++
 net/core/netdev_rx_queue.c    | 4 ++++
 4 files changed, 13 insertions(+)
include/net/netdev_queues.h +6 -0
index c5335e935b7d..f9eba63a5d78 100644
@@ -45,12 +45,16 @@ struct netdev_config {
 /**
  * struct netdev_queue_config - rendered configuration for an RX queue
  * @rx_page_size:	Size of one RX page-pool allocation.
+ * @rx_headroom:	Headroom before packet data in the first buffer,
+ *			including XDP_PACKET_HEADROOM. Zero selects the
+ *			driver default.
  * @rx_ring_size:	Configured size of the regular RX ring.
  * @rx_mini_ring_size:	Configured size of the RX mini ring.
  * @rx_jumbo_ring_size:	Configured size of the RX jumbo ring.
  */
 struct netdev_queue_config {
 	u32	rx_page_size;
+	u32	rx_headroom;
 	u32	rx_ring_size;
 	u32	rx_mini_ring_size;
 	u32	rx_jumbo_ring_size;
@@ -156,6 +160,8 @@ void netdev_stat_queue_sum(struct net_device *netdev,
 enum {
 	/* The queue checks and honours the page size qcfg parameter */
 	QCFG_RX_PAGE_SIZE	= 0x1,
+	/* The queue checks and honours the headroom qcfg parameter */
+	QCFG_RX_HEADROOM	= 0x2,
 };
 
 /**
include/net/page_pool/types.h +1 -0
index a96376613dda..a673fa35febb 100644
@@ -172,6 +172,7 @@ struct pp_memory_provider_params {
 	void *mp_priv;
 	const struct memory_provider_ops *mp_ops;
 	u32 rx_page_size;
+	u32 rx_headroom;
 };
 
 struct page_pool {
net/core/netdev_config.c +2 -0
index 1975de42a60d..97e33764737b 100644
@@ -88,6 +88,8 @@ static int __netdev_queue_config(struct net_device *dev, int rxq_idx,
 	mpp = &__netif_get_rx_queue(dev, rxq_idx)->mp_params;
 	if (mpp->rx_page_size)
 		qcfg->rx_page_size = mpp->rx_page_size;
+	if (mpp->rx_headroom)
+		qcfg->rx_headroom = mpp->rx_headroom;
 	err = validate_cb(dev, qcfg, extack);
 	if (err)
 		return err;
net/core/netdev_rx_queue.c +4 -0
index 610e31d1a717..476289000e78 100644
@@ -248,6 +248,10 @@ static int __netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 		NL_SET_ERR_MSG(extack, "device does not support: rx_page_size");
 		return -EOPNOTSUPP;
 	}
+	if (p->rx_headroom && !(qops->supported_params & QCFG_RX_HEADROOM)) {
+		NL_SET_ERR_MSG(extack, "device does not support: rx_headroom");
+		return -EOPNOTSUPP;
+	}
 
 	rxq = __netif_get_rx_queue(dev, rxq_idx);
 	if (rxq->mp_params.mp_ops) {
Signature · 1 line
2.55.0
#13 lore raw

Re: [RFC net-next 04/15] net: Let memory providers set RX buffer headroom

Quoted 15 lines · Björn Töpel

AF_XDP lets userspace choose a headroom in front of the packet data in each UMEM chunk, and the kernel adds XDP_PACKET_HEADROOM to it. A driver whose buffers come from an AF_XDP memory provider must put packets after this headroom. The driver sees only the page pool, so it has no generic way to learn the value, and nothing checks that the driver supports it.

Add rx_headroom to the provider parameters and to the queue configuration. It is the full headroom, including XDP_PACKET_HEADROOM; zero means the driver default. Add QCFG_RX_HEADROOM. A provider that sets a headroom can only be installed on a queue that supports it; otherwise the install fails with -EOPNOTSUPP. The driver checks the value with the rest of the queue configuration.

Quoted 1 line

This is also a bit confusing to read on its own. Do we not need to convert existing users? The next patch does not seem to use this new rx_headroom?

#14 lore raw

Re: [RFC net-next 04/15] net: Let memory providers set RX buffer headroom

Quoted 16 lines · Stanislav Fomichev
Quoted 15 lines · Björn Töpel

AF_XDP lets userspace choose a headroom in front of the packet data in each UMEM chunk, and the kernel adds XDP_PACKET_HEADROOM to it. A driver whose buffers come from an AF_XDP memory provider must put packets after this headroom. The driver sees only the page pool, so it has no generic way to learn the value, and nothing checks that the driver supports it.

Add rx_headroom to the provider parameters and to the queue configuration. It is the full headroom, including XDP_PACKET_HEADROOM; zero means the driver default. Add QCFG_RX_HEADROOM. A provider that sets a headroom can only be installed on a queue that supports it; otherwise the install fails with -EOPNOTSUPP. The driver checks the value with the rest of the queue configuration.

Quoted 4 lines
Quoted 1 line

This is also a bit confusing to read on its own. Do we not need to convert existing users? The next patch does not seem to use this new rx_headroom?

Existing providers leave it at zero and keep their defaults. XSK sets it in patch 9, and fbnic uses it in patch 13. I'll fix the split so the users are easier to follow.

Björn

#15 lore raw

RFC 5/15 page_pool: Extend memory provider operations

In reply to Björn Töpel

The current memory providers give memory that the CPU cannot read, and they can always refill. An AF_XDP provider is different. The CPU can read its memory, it has only the buffers that userspace puts in the FILL ring, and its buffers go back to userspace instead of being freed. It needs four things from page_pool:

- A CPU address for each net_iov. A provider can set the address of the first net_iov in its net_iov_area and the size each net_iov covers. netmem_address() then computes the address without a call into the provider; drivers call it for every packet. A net_iov_area without an address stays unreadable. page_pool_is_unreadable() is false for a readable provider.

- A way to say that the refill is not done. The new refill_done callback tells the driver whether it may stop refilling. A provider that still has buffers returns false, and the driver keeps NAPI scheduled.

- A way to give up a buffer without freeing it. Add a batched call that ends page_pool ownership of buffers that leave through the provider, for example to userspace. It clears the page_pool link, so a later page pool can take the buffer. Add the matching batched call that sets the link, and a DMA sync helper for drivers that post a buffer again directly.

- No buffer splitting, because a split buffer has several owners. Add MP_CAP_FRAG. page_pool refuses fragment allocation from a provider without it.

devmem and io_uring set MP_CAP_FRAG and keep their fragment behaviour. Their refill paths now use the batched call that sets the link.

---
 include/net/netmem.h                    | 31 ++++++++++-
 include/net/page_pool/helpers.h         | 62 +++++++++++++++++++++-
 include/net/page_pool/memory_provider.h | 28 +++++-----
 io_uring/zcrx.c                         |  4 +-
 net/core/devmem.c                       |  7 ++-
 net/core/page_pool.c                    | 68 ++++++++++++++++++++-----
 6 files changed, 165 insertions(+), 35 deletions(-)
include/net/netmem.h +30 -1
index cc97611632dc..e6dff0b01581 100644
@@ -105,6 +105,12 @@ struct net_iov_area {
 
 	/* Offset into the dma-buf where this chunk starts.  */
 	unsigned long base_virtual;
+
+	/* CPU address of the first net_iov's memory, or NULL when the CPU
+	 * cannot read the area. Each net_iov covers 1 << @niov_shift bytes.
+	 */
+	void *vaddr;
+	u8 niov_shift;
 };
 
 static inline struct net_iov_area *net_iov_owner(const struct net_iov *niov)
@@ -117,6 +123,23 @@ static inline unsigned int net_iov_idx(const struct net_iov *niov)
 	return niov - net_iov_owner(niov)->niovs;
 }
 
+static inline bool net_iov_is_readable(const struct net_iov *niov)
+{
+	return net_iov_owner(niov)->vaddr;
+}
+
+static inline void *net_iov_address(const struct net_iov *niov)
+{
+	const struct net_iov_area *area = net_iov_owner(niov);
+	unsigned long off;
+
+	if (!area->vaddr)
+		return NULL;
+
+	off = (unsigned long)net_iov_idx(niov) << area->niov_shift;
+	return area->vaddr + off;
+}
+
 /* Initialize a niov: stamp the owning area, the memory provider type.
  */
 static inline void net_iov_init(struct net_iov *niov,
@@ -335,10 +358,16 @@ static inline void *__netmem_address(netmem_ref netmem)
 	return page_address(__netmem_to_page(netmem));
 }
 
+/**
+ * netmem_address - get pointer to the memory backing @netmem
+ * @netmem: netmem reference to get the pointer for
+ *
+ * Return: pointer to the memory, or NULL if the CPU cannot read @netmem.
+ */
 static inline void *netmem_address(netmem_ref netmem)
 {
 	if (netmem_is_net_iov(netmem))
-		return NULL;
+		return net_iov_address(netmem_to_net_iov(netmem));
 
 	return __netmem_address(netmem);
 }
include/net/page_pool/helpers.h +61 -1

86 lines not on this page · raw message

include/net/page_pool/memory_provider.h +12 -16
index d18ec079ffe2..61768f2592c9 100644
@@ -13,14 +13,22 @@ struct sk_buff;
  * enum mp_caps - memory provider capabilities
  * @MP_CAP_READABLE: The CPU can access provider buffers. They may back
  *	header and regular page pools, and XDP programs may run on them.
+ * @MP_CAP_FRAG: Page pool fragments may split provider buffers. A
+ *	provider without it hands out objects with a reference count of one.
  */
 enum mp_caps {
 	MP_CAP_READABLE		= BIT(0),
+	MP_CAP_FRAG		= BIT(1),
 };
 
 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);
+	/* Called after RX refill. @full says that the caller reached its refill
+	 * target. Return true when NAPI may complete, or false to keep it
+	 * scheduled even without packet completions.
+	 */
+	bool (*refill_done)(struct page_pool *pool, bool full);
 	int (*init)(struct page_pool *pool);
 	void (*destroy)(struct page_pool *pool);
 	int (*nl_fill)(void *mp_priv, struct sk_buff *rsp,
@@ -30,7 +38,10 @@ 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_netmem_set_page_pool_bulk(struct page_pool *pool,
+				      netmem_ref *netmems, u32 count);
+void net_mp_release_page_pool_bulk(struct page_pool *pool,
+				   netmem_ref *netmems, u32 count);
 void net_mp_niov_clear_page_pool(struct net_iov *niov);
 bool netif_mp_lacks_cap(struct net_device *dev, u32 cap);
 
@@ -40,19 +51,4 @@ int netif_mp_open_rxq(struct net_device *dev, unsigned int rxq_idx,
 void netif_mp_close_rxq(struct net_device *dev, unsigned int rxq_idx,
 			const struct pp_memory_provider_params *old_p);
 
-/**
-  * net_mp_netmem_place_in_cache() - give a netmem to a page pool
-  * @pool:      the page pool to place the netmem into
-  * @netmem:    netmem to give
-  *
-  * Push an accounted netmem into the page pool's allocation cache. The caller
-  * must ensure that there is space in the cache. It should only be called off
-  * the mp_ops->alloc_netmems() path.
-  */
-static inline void net_mp_netmem_place_in_cache(struct page_pool *pool,
-						netmem_ref netmem)
-{
-	pool->alloc.cache[pool->alloc.count++] = netmem;
-}
-
 #endif
io_uring/zcrx.c +3 -1
index 86d580d4410d..ffffbccd517f 100644
@@ -1315,10 +1315,11 @@ static unsigned io_zcrx_refill_slow(struct page_pool *pp, struct io_zcrx_ifq *if
 			continue;
 		}
 
-		net_mp_niov_set_page_pool(pp, niov);
 		netmems[allocated] = net_iov_to_netmem(niov);
 		allocated++;
 	}
+	if (allocated)
+		net_mp_netmem_set_page_pool_bulk(pp, netmems, allocated);
 	return allocated;
 }
 
@@ -1476,6 +1477,7 @@ static const struct memory_provider_ops io_uring_pp_zc_ops = {
 	.destroy		= io_pp_zc_destroy,
 	.nl_fill		= io_pp_nl_fill,
 	.uninstall		= io_pp_uninstall,
+	.caps			= MP_CAP_FRAG,
 };
 
 static unsigned zcrx_parse_rq(netmem_ref *netmem_array, unsigned nr,
net/core/devmem.c +3 -4
index 71c83730d73d..aeb804662668 100644
@@ -459,7 +459,7 @@ netmem_ref mp_dmabuf_devmem_alloc_netmems(struct page_pool *pool, gfp_t gfp)
 {
 	struct net_devmem_dmabuf_binding *binding = pool->mp_priv;
 	netmem_ref *netmems = pool->alloc.cache;
-	unsigned int allocated, i;
+	unsigned int allocated;
 
 	if (WARN_ON_ONCE(pool->alloc.count))
 		return 0;
@@ -469,9 +469,7 @@ netmem_ref mp_dmabuf_devmem_alloc_netmems(struct page_pool *pool, gfp_t gfp)
 	if (unlikely(!allocated))
 		return 0;
 
-	for (i = 0; i < allocated; i++)
-		net_mp_niov_set_page_pool(pool,
-					  netmem_to_net_iov(netmems[i]));
+	net_mp_netmem_set_page_pool_bulk(pool, netmems, allocated);
 
 	/* Return the last one, the rest stay in the page_pool cache. */
 	allocated--;
@@ -540,4 +538,5 @@ static const struct memory_provider_ops dmabuf_devmem_ops = {
 	.release_netmem		= mp_dmabuf_devmem_release_page,
 	.nl_fill		= mp_dmabuf_devmem_nl_fill,
 	.uninstall		= mp_dmabuf_devmem_uninstall,
+	.caps			= MP_CAP_FRAG,
 };
net/core/page_pool.c +56 -12

90 lines not on this page · raw message

Signature · 1 line
2.55.0
#16 lore raw

Re: [RFC net-next 05/15] page_pool: Extend memory provider operations

Quoted 49 lines · Björn Töpel

The current memory providers give memory that the CPU cannot read, and they can always refill. An AF_XDP provider is different. The CPU can read its memory, it has only the buffers that userspace puts in the FILL ring, and its buffers go back to userspace instead of being freed. It needs four things from page_pool:

- A CPU address for each net_iov. A provider can set the address of the first net_iov in its net_iov_area and the size each net_iov covers. netmem_address() then computes the address without a call into the provider; drivers call it for every packet. A net_iov_area without an address stays unreadable. page_pool_is_unreadable() is false for a readable provider.

- A way to say that the refill is not done. The new refill_done callback tells the driver whether it may stop refilling. A provider that still has buffers returns false, and the driver keeps NAPI scheduled.

- A way to give up a buffer without freeing it. Add a batched call that ends page_pool ownership of buffers that leave through the provider, for example to userspace. It clears the page_pool link, so a later page pool can take the buffer. Add the matching batched call that sets the link, and a DMA sync helper for drivers that post a buffer again directly.

- No buffer splitting, because a split buffer has several owners. Add MP_CAP_FRAG. page_pool refuses fragment allocation from a provider without it.

devmem and io_uring set MP_CAP_FRAG and keep their fragment behaviour. Their refill paths now use the batched call that sets the link.

---
 include/net/netmem.h                    | 31 ++++++++++-
 include/net/page_pool/helpers.h         | 62 +++++++++++++++++++++-
 include/net/page_pool/memory_provider.h | 28 +++++-----
 io_uring/zcrx.c                         |  4 +-
 net/core/devmem.c                       |  7 ++-
 net/core/page_pool.c                    | 68 ++++++++++++++++++++-----
 6 files changed, 165 insertions(+), 35 deletions(-)
include/net/netmem.h +0 -0
index cc97611632dc..e6dff0b01581 100644
@@ -105,6 +105,12 @@ struct net_iov_area {
 

[..]

include/net/netmem.h struct net_iov_area { · line 107

Quoted 2 lines
include/net/netmem.h +0 -0
 	/* Offset into the dma-buf where this chunk starts.  */
 	unsigned long base_virtual;

Will send a patch to drop this one, don't think we use it anymore..

[..]

include/net/netmem.h struct net_iov_area { · line 112

Quoted 4 lines
include/net/netmem.h +4 -0
+	/* CPU address of the first net_iov's memory, or NULL when the CPU
+	 * cannot read the area. Each net_iov covers 1 << @niov_shift bytes.
+	 */
+	void *vaddr;

Who is setting this? Somewhere in another patch? Can we keep it here?

include/net/netmem.h struct net_iov_area { · line 113

Quoted 1 line
include/net/netmem.h +1 -0
+	u8 niov_shift;

If we need niov_shift at area level, let's move the one from binding here? Then the existing callers can do binding->area.niov_shift.

Quoted 12 lines

};

static inline struct net_iov_area *net_iov_owner(const struct net_iov *niov)

include/net/netmem.h +5 -0
@@ -117,6 +123,23 @@ static inline unsigned int net_iov_idx(const struct net_iov *niov)
 	return niov - net_iov_owner(niov)->niovs;
 }
 
+static inline bool net_iov_is_readable(const struct net_iov *niov)
+{
+	return net_iov_owner(niov)->vaddr;
+}
+

include/net/netmem.h static inline unsigned int net_iov_idx(const struct net_iov *niov) · line 140

Quoted 10 lines
include/net/netmem.h +10 -0
+static inline void *net_iov_address(const struct net_iov *niov)
+{
+	const struct net_iov_area *area = net_iov_owner(niov);
+	unsigned long off;
+
+	if (!area->vaddr)
+		return NULL;
+
+	off = (unsigned long)net_iov_idx(niov) << area->niov_shift;
+	return area->vaddr + off;

Or, alternatively, store dma_base in area and do:

return niov->dma_addr - area.dma_base + vadd ?

#17 lore raw

Re: [RFC net-next 05/15] page_pool: Extend memory provider operations

Quoted 53 lines · Stanislav Fomichev
Quoted 49 lines · Björn Töpel

The current memory providers give memory that the CPU cannot read, and they can always refill. An AF_XDP provider is different. The CPU can read its memory, it has only the buffers that userspace puts in the FILL ring, and its buffers go back to userspace instead of being freed. It needs four things from page_pool:

- A CPU address for each net_iov. A provider can set the address of the first net_iov in its net_iov_area and the size each net_iov covers. netmem_address() then computes the address without a call into the provider; drivers call it for every packet. A net_iov_area without an address stays unreadable. page_pool_is_unreadable() is false for a readable provider.

- A way to say that the refill is not done. The new refill_done callback tells the driver whether it may stop refilling. A provider that still has buffers returns false, and the driver keeps NAPI scheduled.

- A way to give up a buffer without freeing it. Add a batched call that ends page_pool ownership of buffers that leave through the provider, for example to userspace. It clears the page_pool link, so a later page pool can take the buffer. Add the matching batched call that sets the link, and a DMA sync helper for drivers that post a buffer again directly.

- No buffer splitting, because a split buffer has several owners. Add MP_CAP_FRAG. page_pool refuses fragment allocation from a provider without it.

devmem and io_uring set MP_CAP_FRAG and keep their fragment behaviour. Their refill paths now use the batched call that sets the link.

---
 include/net/netmem.h                    | 31 ++++++++++-
 include/net/page_pool/helpers.h         | 62 +++++++++++++++++++++-
 include/net/page_pool/memory_provider.h | 28 +++++-----
 io_uring/zcrx.c                         |  4 +-
 net/core/devmem.c                       |  7 ++-
 net/core/page_pool.c                    | 68 ++++++++++++++++++++-----
 6 files changed, 165 insertions(+), 35 deletions(-)
include/net/netmem.h +0 -0
index cc97611632dc..e6dff0b01581 100644
@@ -105,6 +105,12 @@ struct net_iov_area {
 

[..]

Quoted 4 lines

include/net/netmem.h struct net_iov_area { · line 107

Quoted 2 lines
include/net/netmem.h +0 -0
 	/* Offset into the dma-buf where this chunk starts.  */
 	unsigned long base_virtual;

Will send a patch to drop this one, don't think we use it anymore..

Ok!

Quoted 6 lines

include/net/netmem.h struct net_iov_area { · line 112

Quoted 4 lines
include/net/netmem.h +4 -0
+	/* CPU address of the first net_iov's memory, or NULL when the CPU
+	 * cannot read the area. Each net_iov covers 1 << @niov_shift bytes.
+	 */
+	void *vaddr;

Who is setting this? Somewhere in another patch? Can we keep it here?

xp_pp_create() sets vaddr in patch 9. I'll move that closer.

Quoted 4 lines

include/net/netmem.h struct net_iov_area { · line 113

Quoted 1 line
include/net/netmem.h +1 -0
+	u8 niov_shift;

If we need niov_shift at area level, let's move the one from binding here? Then the existing callers can do binding->area.niov_shift.

Good! I'll consolidate the binding's shift (and rebase on your base_virtual cleanup)

Quoted 25 lines
Quoted 12 lines

};

static inline struct net_iov_area *net_iov_owner(const struct net_iov *niov)

include/net/netmem.h +5 -0
@@ -117,6 +123,23 @@ static inline unsigned int net_iov_idx(const struct net_iov *niov)
 	return niov - net_iov_owner(niov)->niovs;
 }
 
+static inline bool net_iov_is_readable(const struct net_iov *niov)
+{
+	return net_iov_owner(niov)->vaddr;
+}
+

include/net/netmem.h static inline unsigned int net_iov_idx(const struct net_iov *niov) · line 140

Quoted 10 lines
include/net/netmem.h +10 -0
+static inline void *net_iov_address(const struct net_iov *niov)
+{
+	const struct net_iov_area *area = net_iov_owner(niov);
+	unsigned long off;
+
+	if (!area->vaddr)
+		return NULL;
+
+	off = (unsigned long)net_iov_idx(niov) << area->niov_shift;
+	return area->vaddr + off;

Or, alternatively, store dma_base in area and do:

Quoted 1 line

return niov->dma_addr - area.dma_base + vadd ?

UMEM pages don't have to have contiguous DMA addresses, so I'd keep the index-based CPU offset.

Björn

#18 lore raw

RFC 6/15 xdp: Track non-page netmem in receive buffers

In reply to Björn Töpel

A page-pool memory provider can give net_iov buffers, and virt_to_page() does not work for them. An xdp_buff holds only the data pointer, so code that returns or converts a buffer cannot find its net_iov.

Add NET_IOV_XSK, the net_iov type for AF_XDP. Add the internal flag XDP_FLAGS_HAS_NETMEM. When it is set, the xdp_buff holds a netmem reference in the union with the devmap TX queue, which only devmap egress uses. The flag is cleared when flags are copied to an skb or an xdp_frame. xdp_convert_buff_to_frame() sends such a buffer to the zero-copy copy path.

A fragment from a readable net_iov area no longer marks the buffer's fragments as unreadable. The skb layer still treats every net_iov as unreadable, so such a buffer must be copied before it becomes an skb. Later patches in this series do that.

skb_shared_info holds kernel pointers, so it cannot be in UMEM, which userspace can write. A netmem xdp_buff points to a skb_shared_info that the RX queue keeps in kernel memory instead. xdp_init_buff_from_netmem() sets this up. Page-backed buffers still use their tailroom.

On 64-bit, struct xdp_buff grows from 56 to 64 bytes. struct xdp_buff_xsk stays at 128 bytes on x86-64. Where the largest alignment is 8 bytes, as on s390, it grows from 120 to 128 bytes, so a pool needs 8 more bytes per UMEM chunk. Only helpers that need the netmem or the fragment info test the new flag. Page-backed receive and return paths do not.

---
 include/net/netmem.h |  1 +
 include/net/xdp.h    | 56 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 52 insertions(+), 5 deletions(-)
include/net/netmem.h +1 -0
index e6dff0b01581..76e4fe878863 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,
include/net/xdp.h +51 -5

122 lines not on this page · raw message

Signature · 1 line
2.55.0
#19 lore raw

Re: [RFC net-next 06/15] xdp: Track non-page netmem in receive buffers

Quoted 121 lines · Björn Töpel

A page-pool memory provider can give net_iov buffers, and virt_to_page() does not work for them. An xdp_buff holds only the data pointer, so code that returns or converts a buffer cannot find its net_iov.

Add NET_IOV_XSK, the net_iov type for AF_XDP. Add the internal flag XDP_FLAGS_HAS_NETMEM. When it is set, the xdp_buff holds a netmem reference in the union with the devmap TX queue, which only devmap egress uses. The flag is cleared when flags are copied to an skb or an xdp_frame. xdp_convert_buff_to_frame() sends such a buffer to the zero-copy copy path.

A fragment from a readable net_iov area no longer marks the buffer's fragments as unreadable. The skb layer still treats every net_iov as unreadable, so such a buffer must be copied before it becomes an skb. Later patches in this series do that.

skb_shared_info holds kernel pointers, so it cannot be in UMEM, which userspace can write. A netmem xdp_buff points to a skb_shared_info that the RX queue keeps in kernel memory instead. xdp_init_buff_from_netmem() sets this up. Page-backed buffers still use their tailroom.

On 64-bit, struct xdp_buff grows from 56 to 64 bytes. struct xdp_buff_xsk stays at 128 bytes on x86-64. Where the largest alignment is 8 bytes, as on s390, it grows from 120 to 128 bytes, so a pool needs 8 more bytes per UMEM chunk. Only helpers that need the netmem or the fragment info test the new flag. Page-backed receive and return paths do not.

---
 include/net/netmem.h |  1 +
 include/net/xdp.h    | 56 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 52 insertions(+), 5 deletions(-)
include/net/netmem.h +1 -0
index e6dff0b01581..76e4fe878863 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,
include/net/xdp.h +21 -2
index 07231adfb5f8..80931490ca3a 100644
@@ -11,6 +11,7 @@
 #include <linux/netdevice.h>
 #include <linux/skbuff.h> /* skb_shared_info */
 
+#include <net/netmem.h>
 #include <net/page_pool/types.h>
 
 /**
@@ -81,6 +82,8 @@ enum xdp_buff_flags {
 	 * XDP program is not attached.
 	 */
 	XDP_FLAGS_FRAGS_UNREADABLE	= BIT(2),
+	/* The txq/netmem union contains a receive netmem reference. */
+	XDP_FLAGS_HAS_NETMEM		= BIT(3),
 };
 
 struct xdp_buff {
@@ -89,7 +92,12 @@ struct xdp_buff {
 	void *data_meta;
 	void *data_hard_start;
 	struct xdp_rxq_info *rxq;
-	struct xdp_txq_info *txq;
+	union {
+		/* Valid for DEVMAP egress programs. */
+		struct xdp_txq_info *txq;
+		/* Valid for receive buffers backed by non-page netmem. */
+		netmem_ref netmem;
+	};
 
 	union {
 		struct {
@@ -104,6 +112,8 @@ struct xdp_buff {
 		u64 frame_sz_flags_init;
 #endif
 	};
+	/* Kernel-owned fragment metadata for non-page netmem. */
+	struct skb_shared_info *sinfo;
 };
 
 static __always_inline void xdp_reinit_buff(struct xdp_buff *xdp)
@@ -138,7 +148,7 @@ static __always_inline void xdp_buff_set_frag_unreadable(struct xdp_buff *xdp)
 
 static __always_inline u32 xdp_buff_get_skb_flags(const struct xdp_buff *xdp)
 {
-	return xdp->flags;
+	return xdp->flags & ~XDP_FLAGS_HAS_NETMEM;
 }
 
 static __always_inline void xdp_buff_clear_frag_pfmemalloc(struct xdp_buff *xdp)
@@ -150,6 +160,10 @@ static __always_inline void
 xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
 {
 	xdp->rxq = rxq;
+	/* Do not initialize the txq/netmem union here. DEVMAP generic XDP
+	 * sets txq before this helper is called; receive drivers set netmem
+	 * explicitly.
+	 */
 
 #ifdef __LITTLE_ENDIAN
 	/*
@@ -176,6 +190,33 @@ xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start,
 	xdp->data_meta = meta_valid ? data : data + 1;
 }
 
+static __always_inline bool xdp_buff_has_netmem(const struct xdp_buff *xdp)
+{
+	return !!(xdp->flags & XDP_FLAGS_HAS_NETMEM);
+}
+

[..]

include/net/xdp.h xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start, · line 205

Quoted 8 lines
include/net/xdp.h +8 -0
+static __always_inline netmem_ref
+xdp_buff_get_netmem(const struct xdp_buff *xdp)
+{
+	if (xdp_buff_has_netmem(xdp))
+		return xdp->netmem;
+
+	return virt_to_netmem(xdp->data);
+}

You keep adding methods that are not used in the patch where they are added. I keep getting confused :-p

For virt_to_netmem, maybe a meta question: does xsk pool need a treatment similar to the socket path? Where we did s/page/netmem_ref/ ? Then might be less reasons to special case?

#20 lore raw

Re: [RFC net-next 06/15] xdp: Track non-page netmem in receive buffers

Quoted 137 lines · Stanislav Fomichev
Quoted 121 lines · Björn Töpel

A page-pool memory provider can give net_iov buffers, and virt_to_page() does not work for them. An xdp_buff holds only the data pointer, so code that returns or converts a buffer cannot find its net_iov.

Add NET_IOV_XSK, the net_iov type for AF_XDP. Add the internal flag XDP_FLAGS_HAS_NETMEM. When it is set, the xdp_buff holds a netmem reference in the union with the devmap TX queue, which only devmap egress uses. The flag is cleared when flags are copied to an skb or an xdp_frame. xdp_convert_buff_to_frame() sends such a buffer to the zero-copy copy path.

A fragment from a readable net_iov area no longer marks the buffer's fragments as unreadable. The skb layer still treats every net_iov as unreadable, so such a buffer must be copied before it becomes an skb. Later patches in this series do that.

skb_shared_info holds kernel pointers, so it cannot be in UMEM, which userspace can write. A netmem xdp_buff points to a skb_shared_info that the RX queue keeps in kernel memory instead. xdp_init_buff_from_netmem() sets this up. Page-backed buffers still use their tailroom.

On 64-bit, struct xdp_buff grows from 56 to 64 bytes. struct xdp_buff_xsk stays at 128 bytes on x86-64. Where the largest alignment is 8 bytes, as on s390, it grows from 120 to 128 bytes, so a pool needs 8 more bytes per UMEM chunk. Only helpers that need the netmem or the fragment info test the new flag. Page-backed receive and return paths do not.

---
 include/net/netmem.h |  1 +
 include/net/xdp.h    | 56 ++++++++++++++++++++++++++++++++++++++++----
 2 files changed, 52 insertions(+), 5 deletions(-)
include/net/netmem.h +1 -0
index e6dff0b01581..76e4fe878863 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,
include/net/xdp.h +21 -2
index 07231adfb5f8..80931490ca3a 100644
@@ -11,6 +11,7 @@
 #include <linux/netdevice.h>
 #include <linux/skbuff.h> /* skb_shared_info */
 
+#include <net/netmem.h>
 #include <net/page_pool/types.h>
 
 /**
@@ -81,6 +82,8 @@ enum xdp_buff_flags {
 	 * XDP program is not attached.
 	 */
 	XDP_FLAGS_FRAGS_UNREADABLE	= BIT(2),
+	/* The txq/netmem union contains a receive netmem reference. */
+	XDP_FLAGS_HAS_NETMEM		= BIT(3),
 };
 
 struct xdp_buff {
@@ -89,7 +92,12 @@ struct xdp_buff {
 	void *data_meta;
 	void *data_hard_start;
 	struct xdp_rxq_info *rxq;
-	struct xdp_txq_info *txq;
+	union {
+		/* Valid for DEVMAP egress programs. */
+		struct xdp_txq_info *txq;
+		/* Valid for receive buffers backed by non-page netmem. */
+		netmem_ref netmem;
+	};
 
 	union {
 		struct {
@@ -104,6 +112,8 @@ struct xdp_buff {
 		u64 frame_sz_flags_init;
 #endif
 	};
+	/* Kernel-owned fragment metadata for non-page netmem. */
+	struct skb_shared_info *sinfo;
 };
 
 static __always_inline void xdp_reinit_buff(struct xdp_buff *xdp)
@@ -138,7 +148,7 @@ static __always_inline void xdp_buff_set_frag_unreadable(struct xdp_buff *xdp)
 
 static __always_inline u32 xdp_buff_get_skb_flags(const struct xdp_buff *xdp)
 {
-	return xdp->flags;
+	return xdp->flags & ~XDP_FLAGS_HAS_NETMEM;
 }
 
 static __always_inline void xdp_buff_clear_frag_pfmemalloc(struct xdp_buff *xdp)
@@ -150,6 +160,10 @@ static __always_inline void
 xdp_init_buff(struct xdp_buff *xdp, u32 frame_sz, struct xdp_rxq_info *rxq)
 {
 	xdp->rxq = rxq;
+	/* Do not initialize the txq/netmem union here. DEVMAP generic XDP
+	 * sets txq before this helper is called; receive drivers set netmem
+	 * explicitly.
+	 */
 
 #ifdef __LITTLE_ENDIAN
 	/*
@@ -176,6 +190,33 @@ xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start,
 	xdp->data_meta = meta_valid ? data : data + 1;
 }
 
+static __always_inline bool xdp_buff_has_netmem(const struct xdp_buff *xdp)
+{
+	return !!(xdp->flags & XDP_FLAGS_HAS_NETMEM);
+}
+

[..]

include/net/xdp.h xdp_prepare_buff(struct xdp_buff *xdp, unsigned char *hard_start, · line 205

Quoted 8 lines
include/net/xdp.h +8 -0
+static __always_inline netmem_ref
+xdp_buff_get_netmem(const struct xdp_buff *xdp)
+{
+	if (xdp_buff_has_netmem(xdp))
+		return xdp->netmem;
+
+	return virt_to_netmem(xdp->data);
+}

You keep adding methods that are not used in the patch where they are added. I keep getting confused :-p

Quoted 3 lines

For virt_to_netmem, maybe a meta question: does xsk pool need a treatment similar to the socket path? Where we did s/page/netmem_ref/ ? Then might be less reasons to special case?

Fair point! I'll move the helpers with their users and see what netmem_ref can unify. UMEM still needs the saved descriptor for returning buffers and kernel-owned fragment info...

#21 lore raw

RFC 7/15 xsk: Keep the DMA mapping in the buffer pool

In reply to Björn Töpel

At unmap, an XSK buffer pool looks up its DMA mapping again. It searches the UMEM's mapping list for the pool's netdev. A memory provider, added later in this series, can keep the mapping after the queue is gone. By then the netdev may be cleared, so the search is not reliable.

Store the mapping that xp_dma_map() picked in the pool, and unmap that mapping. Hold a reference on the DMA device while the mapping exists, so a late final unmap does not use a freed device.

The reference counting of shared mappings does not change.

---
 include/net/xsk_buff_pool.h | 1 +
 net/xdp/xsk_buff_pool.c     | 8 +++++---
 2 files changed, 6 insertions(+), 3 deletions(-)
include/net/xsk_buff_pool.h +1 -0
index a7df573784fd..77264c4902c0 100644
@@ -67,6 +67,7 @@ struct xsk_buff_pool {
 	 * even when they are identical.
 	 */
 	dma_addr_t *dma_pages;
+	struct xsk_dma_map *dma_map;
 	struct xdp_buff_xsk *heads;
 	struct xdp_desc *tx_descs;
 	u64 chunk_mask;
net/xdp/xsk_buff_pool.c +5 -3
index f01e1de7360e..244776a72961 100644
@@ -373,7 +373,7 @@ static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_devi
 	}
 
 	dma_map->netdev = netdev;
-	dma_map->dev = dev;
+	dma_map->dev = get_device(dev);
 	dma_map->dma_pages_cnt = nr_pages;
 	refcount_set(&dma_map->users, 1);
 	list_add(&dma_map->list, &umem->xsk_dma_list);
@@ -384,6 +384,7 @@ static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
 {
 	list_del(&dma_map->list);
 	kvfree(dma_map->dma_pages);
+	put_device(dma_map->dev);
 	kfree(dma_map);
 }
 
@@ -407,12 +408,11 @@ static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs)
 
 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
 {
-	struct xsk_dma_map *dma_map;
+	struct xsk_dma_map *dma_map = pool->dma_map;
 
 	if (!pool->dma_pages)
 		return;
 
-	dma_map = xp_find_dma_map(pool);
 	if (!dma_map) {
 		WARN(1, "Could not find dma_map for device");
 		return;
@@ -424,6 +424,7 @@ void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
 	kvfree(pool->dma_pages);
 	pool->dma_pages = NULL;
 	pool->dma_pages_cnt = 0;
+	pool->dma_map = NULL;
 	pool->dev = NULL;
 }
 EXPORT_SYMBOL(xp_dma_unmap);
@@ -460,6 +461,7 @@ static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_
 		return -ENOMEM;
 
 	pool->dev = dma_map->dev;
+	pool->dma_map = dma_map;
 	pool->dma_pages_cnt = dma_map->dma_pages_cnt;
 	memcpy(pool->dma_pages, dma_map->dma_pages,
 	       pool->dma_pages_cnt * sizeof(*pool->dma_pages));
Signature · 1 line
2.55.0
#22 lore raw

RFC 8/15 xsk: Handle a detached FILL ring in RX wakeup

In reply to Björn Töpel

A buffer pool used as a memory provider can live longer than its FILL ring, while an old page pool finishes a deferred destroy. The RX need-wakeup helpers use pool->fq without a NULL check.

Read pool->fq once and return if it is NULL. A normal pool always has a FILL ring here, so nothing changes for it.

---
 net/xdp/xsk.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
net/xdp/xsk.c +12 -2
index 33475b180ea6..b68dda9c37d1 100644
@@ -50,10 +50,15 @@ static struct kmem_cache *xsk_tx_generic_cache;
 
 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
 {
+	struct xsk_queue *fq = READ_ONCE(pool->fq);
+
+	if (!fq)
+		return;
+
 	if (pool->cached_need_wakeup & XDP_WAKEUP_RX)
 		return;
 
-	pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
+	fq->ring->flags |= XDP_RING_NEED_WAKEUP;
 	pool->cached_need_wakeup |= XDP_WAKEUP_RX;
 }
 EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
@@ -77,10 +82,15 @@ EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
 
 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
 {
+	struct xsk_queue *fq = READ_ONCE(pool->fq);
+
+	if (!fq)
+		return;
+
 	if (!(pool->cached_need_wakeup & XDP_WAKEUP_RX))
 		return;
 
-	pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
+	fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
 	pool->cached_need_wakeup &= ~XDP_WAKEUP_RX;
 }
 EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
Signature · 1 line
2.55.0
#23 lore raw

RFC 9/15 xsk: Add a page-pool memory provider for UMEM

In reply to Björn Töpel

AF_XDP zero-copy drivers get RX buffers from an xsk_buff_pool. A driver built on page_pool would need a second RX allocator for that. Instead, let an XSK buffer pool act as a page-pool memory provider. It hands out UMEM chunks as NET_IOV_XSK net_iovs, and the driver uses the normal page_pool API.

A driver calls xsk_pool_setup_page_pool() for XDP_SETUP_XSK_POOL. For now only 4 KiB pages and aligned UMEM with 4 KiB chunks work; other setups get -EOPNOTSUPP. A bind without XDP_ZEROCOPY then falls back to copy mode, as before. The exception is a failed setup that leaves an old page pool being destroyed. That page pool still uses the DMA mapping, so the bind fails.

The provider reads the FILL ring in batches of the page-pool cache refill size, and it handles RX need-wakeup. Each allocation reads at most one batch, which limits the work spent on bad descriptors in NAPI. Addresses outside the UMEM, and addresses that are already in use, count as invalid descriptors and are dropped. refill_done keeps NAPI scheduled while the FILL ring has entries. When the ring is empty, it sets NEED_WAKEUP and then checks the ring once more.

The provider asks for page-sized buffers with the UMEM headroom plus XDP_PACKET_HEADROOM. It refuses a page pool whose DMA sync range goes past the chunk.

During a queue restart, two page pools can use one provider at the same time. A provider lock protects the FILL ring, the reuse stack and need-wakeup. Allocation takes it once per page-pool cache refill of up to 64 buffers. A packet that is copied into a socket with a provider takes it once per packet, because the copy also reads the FILL ring.

A buffer has one owner at a time, as a normal page-pool page does. Allocation claims a buffer by setting its page pool under the provider lock. Release clears the link, so destroying the provider does not need to scan the UMEM.

Generic XDP and synthetic RX queues, such as CPUMAP, would write to the socket RX ring outside the queue's NAPI. While the provider is installed, generic XDP drops such packets and counts them in rx_dropped, and synthetic queues get -EINVAL. Classic zero-copy sockets do not change.

A failed queue restart can leave the old page pool draining. Keep the UMEM, the DMA mapping and the netdev until the provider's last page pool is destroyed. Charge the provider arrays, whose size grows with the UMEM, to the memory cgroup of the socket owner. XDP_SOCKETS now selects PAGE_POOL.

---
 include/net/xdp_sock_drv.h  |  12 +
 include/net/xsk_buff_pool.h |   6 +-
 net/core/page_pool.c        |   4 +-
 net/xdp/Kconfig             |   1 +
 net/xdp/xsk.c               |  50 ++-
 net/xdp/xsk.h               |  58 ++++
 net/xdp/xsk_buff_pool.c     | 654 +++++++++++++++++++++++++++++++++++-
 7 files changed, 756 insertions(+), 29 deletions(-)
include/net/xdp_sock_drv.h +12 -0
index d94aeb506379..b9288f5dd48b 100644
@@ -9,6 +9,8 @@
 #include <net/xdp_sock.h>
 #include <net/xsk_buff_pool.h>
 
+struct netlink_ext_ack;
+
 #define XDP_UMEM_MIN_CHUNK_SHIFT 11
 #define XDP_UMEM_MIN_CHUNK_SIZE (1 << XDP_UMEM_MIN_CHUNK_SHIFT)
 
@@ -28,6 +30,8 @@ void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
 void xsk_tx_release(struct xsk_buff_pool *pool);
+int xsk_pool_setup_page_pool(struct net_device *dev, struct xsk_buff_pool *pool,
+			     u16 queue_id, struct netlink_ext_ack *extack);
 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
 					    u16 queue_id);
 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
@@ -370,6 +374,14 @@ static inline void xsk_tx_release(struct xsk_buff_pool *pool)
 {
 }
 
+static inline int xsk_pool_setup_page_pool(struct net_device *dev,
+					   struct xsk_buff_pool *pool,
+					   u16 queue_id,
+					   struct netlink_ext_ack *extack)
+{
+	return -EOPNOTSUPP;
+}
+
 static inline struct xsk_buff_pool *
 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
 {
include/net/xsk_buff_pool.h +3 -3
index 77264c4902c0..9eed8796a356 100644
@@ -11,6 +11,7 @@
 #include <net/xdp.h>
 
 struct xsk_buff_pool;
+struct xsk_pp;
 struct xdp_rxq_info;
 struct xsk_cb_desc;
 struct xsk_queue;
@@ -52,7 +53,8 @@ struct xsk_buff_pool {
 	spinlock_t xsk_tx_list_lock;
 	refcount_t users;
 	struct xdp_umem *umem;
-	struct work_struct work;
+	struct xsk_pp *pp;
+	struct delayed_work work;
 	/* Protects generic receive in shared and non-shared umem mode. */
 	spinlock_t rx_lock;
 	struct list_head free_list;
@@ -117,10 +119,8 @@ int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs,
 void xp_destroy(struct xsk_buff_pool *pool);
 void xp_get_pool(struct xsk_buff_pool *pool);
 bool xp_put_pool(struct xsk_buff_pool *pool);
-void xp_clear_dev(struct xsk_buff_pool *pool);
 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
-
 /* AF_XDP, and XDP core. */
 void xp_free(struct xdp_buff_xsk *xskb);
 
net/core/page_pool.c +2 -2
index e36ae6123adf..1f446e9a10d7 100644
@@ -1397,8 +1397,8 @@ void net_mp_release_page_pool_bulk(struct page_pool *pool,
 		atomic_add(count, release_cnt);
 }
 
-/* Disassociate a niov from a page pool. Should only be used in the
- * ->release_netmem() path.
+/* Disassociate a niov from a page pool. Memory providers may do this either
+ * from ->release_netmem() or from ->destroy() after all objects were released.
  */
 void net_mp_niov_clear_page_pool(struct net_iov *niov)
 {
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/xsk.c +42 -8

80 lines not on this page · raw message

net/xdp/xsk.h +58 -0

64 lines not on this page · raw message

net/xdp/xsk_buff_pool.c +638 -16

739 lines not on this page · raw message

Signature · 1 line
2.55.0
#24 lore raw

Re: [RFC net-next 09/15] xsk: Add a page-pool memory provider for UMEM

Quoted 362 lines · Björn Töpel

AF_XDP zero-copy drivers get RX buffers from an xsk_buff_pool. A driver built on page_pool would need a second RX allocator for that. Instead, let an XSK buffer pool act as a page-pool memory provider. It hands out UMEM chunks as NET_IOV_XSK net_iovs, and the driver uses the normal page_pool API.

A driver calls xsk_pool_setup_page_pool() for XDP_SETUP_XSK_POOL. For now only 4 KiB pages and aligned UMEM with 4 KiB chunks work; other setups get -EOPNOTSUPP. A bind without XDP_ZEROCOPY then falls back to copy mode, as before. The exception is a failed setup that leaves an old page pool being destroyed. That page pool still uses the DMA mapping, so the bind fails.

The provider reads the FILL ring in batches of the page-pool cache refill size, and it handles RX need-wakeup. Each allocation reads at most one batch, which limits the work spent on bad descriptors in NAPI. Addresses outside the UMEM, and addresses that are already in use, count as invalid descriptors and are dropped. refill_done keeps NAPI scheduled while the FILL ring has entries. When the ring is empty, it sets NEED_WAKEUP and then checks the ring once more.

The provider asks for page-sized buffers with the UMEM headroom plus XDP_PACKET_HEADROOM. It refuses a page pool whose DMA sync range goes past the chunk.

During a queue restart, two page pools can use one provider at the same time. A provider lock protects the FILL ring, the reuse stack and need-wakeup. Allocation takes it once per page-pool cache refill of up to 64 buffers. A packet that is copied into a socket with a provider takes it once per packet, because the copy also reads the FILL ring.

A buffer has one owner at a time, as a normal page-pool page does. Allocation claims a buffer by setting its page pool under the provider lock. Release clears the link, so destroying the provider does not need to scan the UMEM.

Generic XDP and synthetic RX queues, such as CPUMAP, would write to the socket RX ring outside the queue's NAPI. While the provider is installed, generic XDP drops such packets and counts them in rx_dropped, and synthetic queues get -EINVAL. Classic zero-copy sockets do not change.

A failed queue restart can leave the old page pool draining. Keep the UMEM, the DMA mapping and the netdev until the provider's last page pool is destroyed. Charge the provider arrays, whose size grows with the UMEM, to the memory cgroup of the socket owner. XDP_SOCKETS now selects PAGE_POOL.

---
 include/net/xdp_sock_drv.h  |  12 +
 include/net/xsk_buff_pool.h |   6 +-
 net/core/page_pool.c        |   4 +-
 net/xdp/Kconfig             |   1 +
 net/xdp/xsk.c               |  50 ++-
 net/xdp/xsk.h               |  58 ++++
 net/xdp/xsk_buff_pool.c     | 654 +++++++++++++++++++++++++++++++++++-
 7 files changed, 756 insertions(+), 29 deletions(-)
include/net/xdp_sock_drv.h +12 -0
index d94aeb506379..b9288f5dd48b 100644
@@ -9,6 +9,8 @@
 #include <net/xdp_sock.h>
 #include <net/xsk_buff_pool.h>
 
+struct netlink_ext_ack;
+
 #define XDP_UMEM_MIN_CHUNK_SHIFT 11
 #define XDP_UMEM_MIN_CHUNK_SIZE (1 << XDP_UMEM_MIN_CHUNK_SHIFT)
 
@@ -28,6 +30,8 @@ void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
 void xsk_tx_release(struct xsk_buff_pool *pool);
+int xsk_pool_setup_page_pool(struct net_device *dev, struct xsk_buff_pool *pool,
+			     u16 queue_id, struct netlink_ext_ack *extack);
 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
 					    u16 queue_id);
 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
@@ -370,6 +374,14 @@ static inline void xsk_tx_release(struct xsk_buff_pool *pool)
 {
 }
 
+static inline int xsk_pool_setup_page_pool(struct net_device *dev,
+					   struct xsk_buff_pool *pool,
+					   u16 queue_id,
+					   struct netlink_ext_ack *extack)
+{
+	return -EOPNOTSUPP;
+}
+
 static inline struct xsk_buff_pool *
 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
 {
include/net/xsk_buff_pool.h +3 -3
index 77264c4902c0..9eed8796a356 100644
@@ -11,6 +11,7 @@
 #include <net/xdp.h>
 
 struct xsk_buff_pool;
+struct xsk_pp;
 struct xdp_rxq_info;
 struct xsk_cb_desc;
 struct xsk_queue;
@@ -52,7 +53,8 @@ struct xsk_buff_pool {
 	spinlock_t xsk_tx_list_lock;
 	refcount_t users;
 	struct xdp_umem *umem;
-	struct work_struct work;
+	struct xsk_pp *pp;
+	struct delayed_work work;
 	/* Protects generic receive in shared and non-shared umem mode. */
 	spinlock_t rx_lock;
 	struct list_head free_list;
@@ -117,10 +119,8 @@ int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs,
 void xp_destroy(struct xsk_buff_pool *pool);
 void xp_get_pool(struct xsk_buff_pool *pool);
 bool xp_put_pool(struct xsk_buff_pool *pool);
-void xp_clear_dev(struct xsk_buff_pool *pool);
 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
-
 /* AF_XDP, and XDP core. */
 void xp_free(struct xdp_buff_xsk *xskb);
 
net/core/page_pool.c +2 -2
index e36ae6123adf..1f446e9a10d7 100644
@@ -1397,8 +1397,8 @@ void net_mp_release_page_pool_bulk(struct page_pool *pool,
 		atomic_add(count, release_cnt);
 }
 
-/* Disassociate a niov from a page pool. Should only be used in the
- * ->release_netmem() path.
+/* Disassociate a niov from a page pool. Memory providers may do this either
+ * from ->release_netmem() or from ->destroy() after all objects were released.
  */
 void net_mp_niov_clear_page_pool(struct net_iov *niov)
 {
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/xsk.c +42 -8
index b68dda9c37d1..90c98b42a18a 100644
@@ -464,7 +464,9 @@ static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
 static void xsk_flush(struct xdp_sock *xs)
 {
 	xskq_prod_submit(xs->rx);
-	__xskq_cons_release(xs->pool->fq);
+	/* Provider pools publish FILL consumption under the provider lock. */
+	if (!READ_ONCE(xs->pool->pp))
+		__xskq_cons_release(xs->pool->fq);
 	sock_def_readable(&xs->sk);
 }
 
@@ -474,16 +476,40 @@ int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
 	int err;
 
 	err = xsk_rcv_check(xs, xdp, len);
-	if (!err) {
-		spin_lock_bh(&xs->pool->rx_lock);
-		err = __xsk_rcv(xs, xdp, len);
-		xsk_flush(xs);
+	if (err)
+		return err;
+	spin_lock_bh(&xs->pool->rx_lock);
+	if (unlikely(READ_ONCE(xs->pool->pp))) {
+		xs->rx_dropped++;
 		spin_unlock_bh(&xs->pool->rx_lock);
+		return -EOPNOTSUPP;
 	}
+	err = __xsk_rcv(xs, xdp, len);
+	xsk_flush(xs);
+	spin_unlock_bh(&xs->pool->rx_lock);
 
 	return err;
 }
 
+/* Copy into the socket's UMEM. A provider-backed pool shares its FILL ring
+ * with provider allocation, which can run for another page pool of the queue
+ * while the queue is replaced.
+ */
+static int xsk_rcv_copy(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
+{
+	struct xsk_pp *provider = READ_ONCE(xs->pool->pp);
+	int err;
+
+	if (likely(!provider))
+		return __xsk_rcv(xs, xdp, len);
+
+	spin_lock_bh(&provider->lock);
+	err = __xsk_rcv(xs, xdp, len);
+	__xskq_cons_release(xs->pool->fq);
+	spin_unlock_bh(&provider->lock);
+	return err;
+}
+
 static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
 {
 	u32 len = xdp_get_buff_len(xdp);
@@ -498,7 +524,15 @@ static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
 		return xsk_rcv_zc(xs, xdp, len);
 	}
 
-	err = __xsk_rcv(xs, xdp, len);
+	/* The socket RX ring has a single producer, the queue's poll context.
+	 * Reject synthetic RX queues before their remote context produces
+	 * into a provider-backed socket.
+	 */
+	if (unlikely(READ_ONCE(xs->pool->pp) &&
+		     !xdp_rxq_info_is_reg(xdp->rxq)))
+		return -EINVAL;
+
+	err = xsk_rcv_copy(xs, xdp, len);
 	if (!err)
 		xdp_return_buff(xdp);
 	return err;
@@ -2149,8 +2183,8 @@ static int xsk_notifier(struct notifier_block *this,
 
 				xsk_unbind_dev(xs);
 
-				/* Clear device references. */
-				xp_clear_dev(xs->pool);
+				/* Unregister cannot hold a device reference. */
+				xp_clear_dev(xs->pool, XSK_POOL_CLEAR_FORCE);
 			}
 			mutex_unlock(&xs->mutex);
 		}
net/xdp/xsk.h +58 -0
index 7c811b5cce76..8770778cd322 100644
@@ -4,6 +4,64 @@
 #ifndef XSK_H_
 #define XSK_H_
 
+#include <net/netmem.h>
+
+struct xsk_buff_pool;
+
+enum xsk_pool_clear_mode {
+	XSK_POOL_CLEAR_NORMAL,
+	XSK_POOL_CLEAR_FORCE,
+};
+
+struct xsk_pp_info {
+	struct net_iov_area area;
+	struct xsk_buff_pool *pool;
+	u32 chunk_shift;
+};
+
+/* Keep page_pool descriptors independent from direct-driver XSK buffers.
+ * Queue replacement creates the new page_pool before it stops the old queue
+ * and destroys the old page_pool after the new queue started, so two
+ * page_pools can use one provider. @lock serializes the state they share.
+ * Generic XDP cannot deliver to a provider-backed socket.
+ */
+struct xsk_pp {
+	struct xsk_pp_info info;
+	struct xsk_queue __rcu *fq;
+	/* FILL consumers, @reuse and RX need_wakeup state */
+	spinlock_t lock;
+	u32 reuse_cnt;
+	u32 nr_pools;
+	u64 chunk_mask;
+	u64 addrs_cnt;
+	u8 release_retries;
+	bool dma_need_sync;
+	bool detached;
+	u32 reuse[];
+};
+
+void xp_clear_dev(struct xsk_buff_pool *pool, enum xsk_pool_clear_mode mode);
+
+static inline bool xp_netmem_is_xsk(netmem_ref netmem)
+{
+	return netmem_is_net_iov(netmem) &&
+	       netmem_to_net_iov(netmem)->type == NET_IOV_XSK;
+}
+
+static inline struct xsk_pp_info *xp_netmem_to_pp(netmem_ref netmem)
+{
+	struct net_iov *niov = netmem_to_net_iov(netmem);
+
+	return container_of(net_iov_owner(niov), struct xsk_pp_info, area);
+}
+
+static inline bool xp_netmem_is_from_pool(netmem_ref netmem,
+					  const struct xsk_buff_pool *pool)
+{
+	return xp_netmem_is_xsk(netmem) &&
+	       xp_netmem_to_pp(netmem)->pool == pool;
+}
+
 struct xdp_ring_offset_v1 {
 	__u64 producer;
 	__u64 consumer;
net/xdp/xsk_buff_pool.c +22 -3
index 244776a72961..e25347f8c208 100644
@@ -1,7 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/netdevice.h>
+#include <linux/sizes.h>
 #include <net/netdev_lock.h>
+#include <net/netdev_queues.h>
+#include <net/netdev_rx_queue.h>
+#include <net/page_pool/helpers.h>
 #include <net/page_pool/memory_provider.h>
 #include <net/xsk_buff_pool.h>
 #include <net/xdp_sock.h>
@@ -12,6 +16,20 @@
 #include "xsk.h"
 
 #define ETH_PAD_LEN (ETH_HLEN + 2 * VLAN_HLEN  + ETH_FCS_LEN)
+#define XSK_PAGE_POOL_DMA_ATTR	(DMA_ATTR_WEAK_ORDERING | \
+				 DMA_ATTR_SKIP_CPU_SYNC)
+#define XSK_POOL_RELEASE_RETRY_MAX	(60 * HZ)
+
+static bool xp_pp_teardown(struct xsk_buff_pool *pool);
+static void xp_pp_retry_release(struct xsk_buff_pool *pool);
+static void xp_destroy_unbound_deferred(struct work_struct *work);
+
+static void __xp_destroy(struct xsk_buff_pool *pool)
+{
+	kvfree(pool->tx_descs);
+	kvfree(pool->heads);
+	kvfree(pool);
+}
 
 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
 {
@@ -38,9 +56,22 @@ void xp_destroy(struct xsk_buff_pool *pool)
 	if (!pool)
 		return;
 
-	kvfree(pool->tx_descs);
-	kvfree(pool->heads);
-	kvfree(pool);
+	/* A failed queue replacement can leave a page_pool waiting for an
+	 * in-flight buffer.  Keep the UMEM and pool alive until its provider
+	 * destroy callback has run.
+	 */

[..]

net/xdp/xsk_buff_pool.c void xp_destroy(struct xsk_buff_pool *pool) · line 63

Quoted 1 line
net/xdp/xsk_buff_pool.c +1 -0
+	if (pool->pp) {

Would be nice to do a cleanup in xsk_buff_pool: separate it into common/generic parts and pp vs non-pp parts. And maybe pp vs non-pp can be a union? With the addition of pp, it's gonna be hard to understand what is new/shiny vs old/deprecated. I'm assuming, at some point, when every driver is backed by pp umem, we can drop the non-umem path?

#25 lore raw

Re: [RFC net-next 09/15] xsk: Add a page-pool memory provider for UMEM

Quoted 366 lines · Stanislav Fomichev
Quoted 362 lines · Björn Töpel

AF_XDP zero-copy drivers get RX buffers from an xsk_buff_pool. A driver built on page_pool would need a second RX allocator for that. Instead, let an XSK buffer pool act as a page-pool memory provider. It hands out UMEM chunks as NET_IOV_XSK net_iovs, and the driver uses the normal page_pool API.

A driver calls xsk_pool_setup_page_pool() for XDP_SETUP_XSK_POOL. For now only 4 KiB pages and aligned UMEM with 4 KiB chunks work; other setups get -EOPNOTSUPP. A bind without XDP_ZEROCOPY then falls back to copy mode, as before. The exception is a failed setup that leaves an old page pool being destroyed. That page pool still uses the DMA mapping, so the bind fails.

The provider reads the FILL ring in batches of the page-pool cache refill size, and it handles RX need-wakeup. Each allocation reads at most one batch, which limits the work spent on bad descriptors in NAPI. Addresses outside the UMEM, and addresses that are already in use, count as invalid descriptors and are dropped. refill_done keeps NAPI scheduled while the FILL ring has entries. When the ring is empty, it sets NEED_WAKEUP and then checks the ring once more.

The provider asks for page-sized buffers with the UMEM headroom plus XDP_PACKET_HEADROOM. It refuses a page pool whose DMA sync range goes past the chunk.

During a queue restart, two page pools can use one provider at the same time. A provider lock protects the FILL ring, the reuse stack and need-wakeup. Allocation takes it once per page-pool cache refill of up to 64 buffers. A packet that is copied into a socket with a provider takes it once per packet, because the copy also reads the FILL ring.

A buffer has one owner at a time, as a normal page-pool page does. Allocation claims a buffer by setting its page pool under the provider lock. Release clears the link, so destroying the provider does not need to scan the UMEM.

Generic XDP and synthetic RX queues, such as CPUMAP, would write to the socket RX ring outside the queue's NAPI. While the provider is installed, generic XDP drops such packets and counts them in rx_dropped, and synthetic queues get -EINVAL. Classic zero-copy sockets do not change.

A failed queue restart can leave the old page pool draining. Keep the UMEM, the DMA mapping and the netdev until the provider's last page pool is destroyed. Charge the provider arrays, whose size grows with the UMEM, to the memory cgroup of the socket owner. XDP_SOCKETS now selects PAGE_POOL.

---
 include/net/xdp_sock_drv.h  |  12 +
 include/net/xsk_buff_pool.h |   6 +-
 net/core/page_pool.c        |   4 +-
 net/xdp/Kconfig             |   1 +
 net/xdp/xsk.c               |  50 ++-
 net/xdp/xsk.h               |  58 ++++
 net/xdp/xsk_buff_pool.c     | 654 +++++++++++++++++++++++++++++++++++-
 7 files changed, 756 insertions(+), 29 deletions(-)
include/net/xdp_sock_drv.h +12 -0
index d94aeb506379..b9288f5dd48b 100644
@@ -9,6 +9,8 @@
 #include <net/xdp_sock.h>
 #include <net/xsk_buff_pool.h>
 
+struct netlink_ext_ack;
+
 #define XDP_UMEM_MIN_CHUNK_SHIFT 11
 #define XDP_UMEM_MIN_CHUNK_SIZE (1 << XDP_UMEM_MIN_CHUNK_SHIFT)
 
@@ -28,6 +30,8 @@ void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries);
 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc);
 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, u32 max);
 void xsk_tx_release(struct xsk_buff_pool *pool);
+int xsk_pool_setup_page_pool(struct net_device *dev, struct xsk_buff_pool *pool,
+			     u16 queue_id, struct netlink_ext_ack *extack);
 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
 					    u16 queue_id);
 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
@@ -370,6 +374,14 @@ static inline void xsk_tx_release(struct xsk_buff_pool *pool)
 {
 }
 
+static inline int xsk_pool_setup_page_pool(struct net_device *dev,
+					   struct xsk_buff_pool *pool,
+					   u16 queue_id,
+					   struct netlink_ext_ack *extack)
+{
+	return -EOPNOTSUPP;
+}
+
 static inline struct xsk_buff_pool *
 xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
 {
include/net/xsk_buff_pool.h +3 -3
index 77264c4902c0..9eed8796a356 100644
@@ -11,6 +11,7 @@
 #include <net/xdp.h>
 
 struct xsk_buff_pool;
+struct xsk_pp;
 struct xdp_rxq_info;
 struct xsk_cb_desc;
 struct xsk_queue;
@@ -52,7 +53,8 @@ struct xsk_buff_pool {
 	spinlock_t xsk_tx_list_lock;
 	refcount_t users;
 	struct xdp_umem *umem;
-	struct work_struct work;
+	struct xsk_pp *pp;
+	struct delayed_work work;
 	/* Protects generic receive in shared and non-shared umem mode. */
 	spinlock_t rx_lock;
 	struct list_head free_list;
@@ -117,10 +119,8 @@ int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs,
 void xp_destroy(struct xsk_buff_pool *pool);
 void xp_get_pool(struct xsk_buff_pool *pool);
 bool xp_put_pool(struct xsk_buff_pool *pool);
-void xp_clear_dev(struct xsk_buff_pool *pool);
 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs);
-
 /* AF_XDP, and XDP core. */
 void xp_free(struct xdp_buff_xsk *xskb);
 
net/core/page_pool.c +2 -2
index e36ae6123adf..1f446e9a10d7 100644
@@ -1397,8 +1397,8 @@ void net_mp_release_page_pool_bulk(struct page_pool *pool,
 		atomic_add(count, release_cnt);
 }
 
-/* Disassociate a niov from a page pool. Should only be used in the
- * ->release_netmem() path.
+/* Disassociate a niov from a page pool. Memory providers may do this either
+ * from ->release_netmem() or from ->destroy() after all objects were released.
  */
 void net_mp_niov_clear_page_pool(struct net_iov *niov)
 {
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/xsk.c +42 -8
index b68dda9c37d1..90c98b42a18a 100644
@@ -464,7 +464,9 @@ static int xsk_rcv_check(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
 static void xsk_flush(struct xdp_sock *xs)
 {
 	xskq_prod_submit(xs->rx);
-	__xskq_cons_release(xs->pool->fq);
+	/* Provider pools publish FILL consumption under the provider lock. */
+	if (!READ_ONCE(xs->pool->pp))
+		__xskq_cons_release(xs->pool->fq);
 	sock_def_readable(&xs->sk);
 }
 
@@ -474,16 +476,40 @@ int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
 	int err;
 
 	err = xsk_rcv_check(xs, xdp, len);
-	if (!err) {
-		spin_lock_bh(&xs->pool->rx_lock);
-		err = __xsk_rcv(xs, xdp, len);
-		xsk_flush(xs);
+	if (err)
+		return err;
+	spin_lock_bh(&xs->pool->rx_lock);
+	if (unlikely(READ_ONCE(xs->pool->pp))) {
+		xs->rx_dropped++;
 		spin_unlock_bh(&xs->pool->rx_lock);
+		return -EOPNOTSUPP;
 	}
+	err = __xsk_rcv(xs, xdp, len);
+	xsk_flush(xs);
+	spin_unlock_bh(&xs->pool->rx_lock);
 
 	return err;
 }
 
+/* Copy into the socket's UMEM. A provider-backed pool shares its FILL ring
+ * with provider allocation, which can run for another page pool of the queue
+ * while the queue is replaced.
+ */
+static int xsk_rcv_copy(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
+{
+	struct xsk_pp *provider = READ_ONCE(xs->pool->pp);
+	int err;
+
+	if (likely(!provider))
+		return __xsk_rcv(xs, xdp, len);
+
+	spin_lock_bh(&provider->lock);
+	err = __xsk_rcv(xs, xdp, len);
+	__xskq_cons_release(xs->pool->fq);
+	spin_unlock_bh(&provider->lock);
+	return err;
+}
+
 static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
 {
 	u32 len = xdp_get_buff_len(xdp);
@@ -498,7 +524,15 @@ static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
 		return xsk_rcv_zc(xs, xdp, len);
 	}
 
-	err = __xsk_rcv(xs, xdp, len);
+	/* The socket RX ring has a single producer, the queue's poll context.
+	 * Reject synthetic RX queues before their remote context produces
+	 * into a provider-backed socket.
+	 */
+	if (unlikely(READ_ONCE(xs->pool->pp) &&
+		     !xdp_rxq_info_is_reg(xdp->rxq)))
+		return -EINVAL;
+
+	err = xsk_rcv_copy(xs, xdp, len);
 	if (!err)
 		xdp_return_buff(xdp);
 	return err;
@@ -2149,8 +2183,8 @@ static int xsk_notifier(struct notifier_block *this,
 
 				xsk_unbind_dev(xs);
 
-				/* Clear device references. */
-				xp_clear_dev(xs->pool);
+				/* Unregister cannot hold a device reference. */
+				xp_clear_dev(xs->pool, XSK_POOL_CLEAR_FORCE);
 			}
 			mutex_unlock(&xs->mutex);
 		}
net/xdp/xsk.h +58 -0
index 7c811b5cce76..8770778cd322 100644
@@ -4,6 +4,64 @@
 #ifndef XSK_H_
 #define XSK_H_
 
+#include <net/netmem.h>
+
+struct xsk_buff_pool;
+
+enum xsk_pool_clear_mode {
+	XSK_POOL_CLEAR_NORMAL,
+	XSK_POOL_CLEAR_FORCE,
+};
+
+struct xsk_pp_info {
+	struct net_iov_area area;
+	struct xsk_buff_pool *pool;
+	u32 chunk_shift;
+};
+
+/* Keep page_pool descriptors independent from direct-driver XSK buffers.
+ * Queue replacement creates the new page_pool before it stops the old queue
+ * and destroys the old page_pool after the new queue started, so two
+ * page_pools can use one provider. @lock serializes the state they share.
+ * Generic XDP cannot deliver to a provider-backed socket.
+ */
+struct xsk_pp {
+	struct xsk_pp_info info;
+	struct xsk_queue __rcu *fq;
+	/* FILL consumers, @reuse and RX need_wakeup state */
+	spinlock_t lock;
+	u32 reuse_cnt;
+	u32 nr_pools;
+	u64 chunk_mask;
+	u64 addrs_cnt;
+	u8 release_retries;
+	bool dma_need_sync;
+	bool detached;
+	u32 reuse[];
+};
+
+void xp_clear_dev(struct xsk_buff_pool *pool, enum xsk_pool_clear_mode mode);
+
+static inline bool xp_netmem_is_xsk(netmem_ref netmem)
+{
+	return netmem_is_net_iov(netmem) &&
+	       netmem_to_net_iov(netmem)->type == NET_IOV_XSK;
+}
+
+static inline struct xsk_pp_info *xp_netmem_to_pp(netmem_ref netmem)
+{
+	struct net_iov *niov = netmem_to_net_iov(netmem);
+
+	return container_of(net_iov_owner(niov), struct xsk_pp_info, area);
+}
+
+static inline bool xp_netmem_is_from_pool(netmem_ref netmem,
+					  const struct xsk_buff_pool *pool)
+{
+	return xp_netmem_is_xsk(netmem) &&
+	       xp_netmem_to_pp(netmem)->pool == pool;
+}
+
 struct xdp_ring_offset_v1 {
 	__u64 producer;
 	__u64 consumer;
net/xdp/xsk_buff_pool.c +22 -3
index 244776a72961..e25347f8c208 100644
@@ -1,7 +1,11 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/netdevice.h>
+#include <linux/sizes.h>
 #include <net/netdev_lock.h>
+#include <net/netdev_queues.h>
+#include <net/netdev_rx_queue.h>
+#include <net/page_pool/helpers.h>
 #include <net/page_pool/memory_provider.h>
 #include <net/xsk_buff_pool.h>
 #include <net/xdp_sock.h>
@@ -12,6 +16,20 @@
 #include "xsk.h"
 
 #define ETH_PAD_LEN (ETH_HLEN + 2 * VLAN_HLEN  + ETH_FCS_LEN)
+#define XSK_PAGE_POOL_DMA_ATTR	(DMA_ATTR_WEAK_ORDERING | \
+				 DMA_ATTR_SKIP_CPU_SYNC)
+#define XSK_POOL_RELEASE_RETRY_MAX	(60 * HZ)
+
+static bool xp_pp_teardown(struct xsk_buff_pool *pool);
+static void xp_pp_retry_release(struct xsk_buff_pool *pool);
+static void xp_destroy_unbound_deferred(struct work_struct *work);
+
+static void __xp_destroy(struct xsk_buff_pool *pool)
+{
+	kvfree(pool->tx_descs);
+	kvfree(pool->heads);
+	kvfree(pool);
+}
 
 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
 {
@@ -38,9 +56,22 @@ void xp_destroy(struct xsk_buff_pool *pool)
 	if (!pool)
 		return;
 
-	kvfree(pool->tx_descs);
-	kvfree(pool->heads);
-	kvfree(pool);
+	/* A failed queue replacement can leave a page_pool waiting for an
+	 * in-flight buffer.  Keep the UMEM and pool alive until its provider
+	 * destroy callback has run.
+	 */

[..]

Quoted 8 lines

net/xdp/xsk_buff_pool.c void xp_destroy(struct xsk_buff_pool *pool) · line 63

Quoted 1 line
net/xdp/xsk_buff_pool.c +1 -0
+	if (pool->pp) {

Would be nice to do a cleanup in xsk_buff_pool: separate it into common/generic parts and pp vs non-pp parts. And maybe pp vs non-pp can be a union? With the addition of pp, it's gonna be hard to understand what is new/shiny vs old/deprecated. I'm assuming, at some point, when every driver is backed by pp umem, we can drop the non-umem path?

Yep. I'll try to split the shared and allocator-specific state. Need to check fallback and teardown before using a union.

Both paths use UMEM, so the direct allocator can go only after (if ever) its users migrate.

#26 lore raw

RFC 10/15 xsk: Add RX helpers for page-pool drivers

In reply to Björn Töpel

A driver that uses an XSK pool as its page-pool provider must know whether the pool on a queue is the installed provider, or only registered on the queue. It also needs the scatter-gather setting of the pool.

Add xsk_get_pool_from_rxq(), which returns the pool only while it is the queue's installed provider, and xsk_pool_uses_sg(). Name the 128-byte RX frame alignment XSK_RX_FRAME_SIZE_ALIGN. Add !CONFIG_XDP_SOCKETS stubs for the new helpers and for two frame-size helpers that had none, so drivers need no #ifdefs.

---
 include/net/xdp_sock_drv.h | 46 +++++++++++++++++++++++++++++++++++++-
 net/xdp/xsk_buff_pool.c    |  2 +-
 2 files changed, 46 insertions(+), 2 deletions(-)
include/net/xdp_sock_drv.h +45 -1
index b9288f5dd48b..28461e675b8e 100644
@@ -6,6 +6,7 @@
 #ifndef _LINUX_XDP_SOCK_DRV_H
 #define _LINUX_XDP_SOCK_DRV_H
 
+#include <net/netdev_rx_queue.h>
 #include <net/xdp_sock.h>
 #include <net/xsk_buff_pool.h>
 
@@ -13,6 +14,7 @@ struct netlink_ext_ack;
 
 #define XDP_UMEM_MIN_CHUNK_SHIFT 11
 #define XDP_UMEM_MIN_CHUNK_SIZE (1 << XDP_UMEM_MIN_CHUNK_SHIFT)
+#define XSK_RX_FRAME_SIZE_ALIGN	128
 
 #define NETDEV_XDP_ACT_XSK	(NETDEV_XDP_ACT_BASIC |		\
 				 NETDEV_XDP_ACT_REDIRECT |	\
@@ -34,12 +36,33 @@ int xsk_pool_setup_page_pool(struct net_device *dev, struct xsk_buff_pool *pool,
 			     u16 queue_id, struct netlink_ext_ack *extack);
 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
 					    u16 queue_id);
+
+/* Return the XSK pool only while it is the installed RX memory provider. */
+static inline struct xsk_buff_pool *
+xsk_get_pool_from_rxq(struct net_device *dev, u16 queue_id)
+{
+	struct netdev_rx_queue *rxq;
+	struct xsk_buff_pool *pool;
+
+	if (queue_id >= dev->real_num_rx_queues)
+		return NULL;
+
+	rxq = __netif_get_rx_queue(dev, queue_id);
+	pool = rxq->pool;
+	return pool == rxq->mp_params.mp_priv ? pool : NULL;
+}
+
 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool);
 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool);
 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool);
 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool);
 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool);
 
+static inline bool xsk_pool_uses_sg(struct xsk_buff_pool *pool)
+{
+	return pool->umem->flags & XDP_UMEM_SG_FLAG;
+}
+
 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
 {
 	return XDP_PACKET_HEADROOM + pool->headroom;
@@ -73,7 +96,7 @@ static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
 	mbuf = pool->dev && (umem->flags & XDP_UMEM_SG_FLAG);
 	frame_size -= xsk_pool_get_tailroom(mbuf);
 
-	return ALIGN_DOWN(frame_size, 128);
+	return ALIGN_DOWN(frame_size, XSK_RX_FRAME_SIZE_ALIGN);
 }
 
 static inline u32 xsk_pool_get_rx_frag_step(struct xsk_buff_pool *pool)
@@ -388,6 +411,12 @@ xsk_get_pool_from_qid(struct net_device *dev, u16 queue_id)
 	return NULL;
 }
 
+static inline struct xsk_buff_pool *
+xsk_get_pool_from_rxq(struct net_device *dev, u16 queue_id)
+{
+	return NULL;
+}
+
 static inline void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
 {
 }
@@ -409,16 +438,31 @@ static inline bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
 	return false;
 }
 
+static inline bool xsk_pool_uses_sg(struct xsk_buff_pool *pool)
+{
+	return false;
+}
+
 static inline u32 xsk_pool_get_headroom(struct xsk_buff_pool *pool)
 {
 	return 0;
 }
 
+static inline u32 xsk_pool_get_tailroom(bool mbuf)
+{
+	return 0;
+}
+
 static inline u32 xsk_pool_get_chunk_size(struct xsk_buff_pool *pool)
 {
 	return 0;
 }
 
+static inline u32 __xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
+{
+	return 0;
+}
+
 static inline u32 xsk_pool_get_rx_frame_size(struct xsk_buff_pool *pool)
 {
 	return 0;
net/xdp/xsk_buff_pool.c +1 -1
index e25347f8c208..9c32fb165533 100644
@@ -261,7 +261,7 @@ int xp_assign_dev(struct xsk_buff_pool *pool,
 	 */
 	frame_size = __xsk_pool_get_rx_frame_size(pool) -
 		     xsk_pool_get_tailroom(mbuf);
-	frame_size = ALIGN_DOWN(frame_size, 128);
+	frame_size = ALIGN_DOWN(frame_size, XSK_RX_FRAME_SIZE_ALIGN);
 
 	if (needed > frame_size * segs) {
 		err = -EINVAL;
Signature · 1 line
2.55.0
#27 lore raw

RFC 11/15 xdp: Copy provider buffers on pass and redirect

In reply to Björn Töpel

A provider buffer must go back to its page pool from the NAPI poll that allocated it. Userspace gets UMEM ownership back through the FILL ring, without a reference count. An skb or xdp_frame can outlive the poll, so it cannot hold a provider buffer.

Copy the packet, fragments included, into kernel pages on XDP_PASS and on redirects to targets other than XSKMAP. On redirect, split the copy from the release: return the provider buffer only after the target accepts the copy. The classic zero-copy redirect path is unchanged.

The copy and the BPF fragment helpers get a fragment's address from the new xdp_frag_address(). It works for pages and for provider net_iovs; skb_frag_address() works only for pages. The next patch handles delivery to an XSKMAP socket.

---
 include/net/xdp.h |  8 +++-
 net/core/dev.h    |  5 +++
 net/core/filter.c | 28 +++++++++++---
 net/core/xdp.c    | 98 ++++++++++++++++++++++++++++++++++++++++++++---
 4 files changed, 126 insertions(+), 13 deletions(-)
include/net/xdp.h +7 -1
index 80931490ca3a..4d7040debcca 100644
@@ -420,11 +420,17 @@ xdp_update_skb_frags_info(struct sk_buff *skb, u8 nr_frags,
 	skb->unreadable |= !!(xdp_flags & XDP_FLAGS_FRAGS_UNREADABLE);
 }
 
+/* Page and readable provider fragments alike resolve through their netmem. */
+static inline void *xdp_frag_address(const skb_frag_t *frag)
+{
+	return netmem_address(skb_frag_netmem(frag)) + skb_frag_off(frag);
+}
+
 /* Avoids inlining WARN macro in fast-path */
 void xdp_warn(const char *msg, const char *func, const int line);
 #define XDP_WARN(msg) xdp_warn(msg, __func__, __LINE__)
 
-struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp);
+struct sk_buff *xdp_build_skb_from_buff(struct xdp_buff *xdp);
 struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp);
 struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp);
 struct sk_buff *__xdp_build_skb_from_frame(struct xdp_frame *xdpf,
net/core/dev.h +5 -0
index 4793a994f7f1..6598a4c5cfec 100644
@@ -13,6 +13,11 @@ struct netlink_ext_ack;
 struct netdev_queue_config;
 struct cpumask;
 struct pp_memory_provider_params;
+struct xdp_buff;
+struct xdp_frame;
+
+struct xdp_frame *xdp_copy_zc_to_xdp_frame(struct xdp_buff *xdp);
+void xdp_release_zc_buff(struct xdp_buff *xdp);
 
 /* Random bits of netdevice that don't need to be exposed */
 #define FLOW_LIMIT_HISTORY	(1 << 7)  /* must be ^2 and !overflow buckets */
net/core/filter.c +22 -6
index 70dc621672f2..23b3923c0869 100644
@@ -4221,7 +4221,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++;
 	}
@@ -4249,7 +4249,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;
 		}
@@ -4339,7 +4339,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)
@@ -4684,12 +4684,28 @@ int xdp_do_redirect(struct net_device *dev, struct xdp_buff *xdp,
 {
 	struct bpf_redirect_info *ri = bpf_net_ctx_get_ri();
 	enum bpf_map_type map_type = ri->map_type;
+	struct xdp_frame *xdpf;
+	int err;
 
 	if (map_type == BPF_MAP_TYPE_XSKMAP)
 		return __xdp_do_redirect_xsk(ri, dev, xdp, xdp_prog);
 
-	return __xdp_do_redirect_frame(ri, dev, xdp_convert_buff_to_frame(xdp),
-				       xdp_prog);
+	if (!xdp_buff_has_netmem(xdp))
+		return __xdp_do_redirect_frame(ri, dev,
+					       xdp_convert_buff_to_frame(xdp),
+					       xdp_prog);
+
+	/* Return provider buffers only after the target accepts the copy. */
+	xdpf = xdp_copy_zc_to_xdp_frame(xdp);
+	err = __xdp_do_redirect_frame(ri, dev, xdpf, xdp_prog);
+	if (err) {
+		if (xdpf)
+			xdp_return_frame_rx_napi(xdpf);
+		return err;
+	}
+
+	xdp_release_zc_buff(xdp);
+	return 0;
 }
 EXPORT_SYMBOL_GPL(xdp_do_redirect);
 
@@ -12677,7 +12693,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;
net/core/xdp.c +92 -6

149 lines not on this page · raw message

Signature · 1 line
2.55.0
#28 lore raw

Re: [RFC net-next 11/15] xdp: Copy provider buffers on pass and redirect

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] net: xdp: payload truncation in xdp_build_skb_from_zc() when metadata unsupported --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] net: xdp: payload truncation in xdp_build_skb_from_zc() when metadata unsupported --

commit a36f92358b032a7e20669d832f27102e55f9f4eb
Author: Björn Töpel <[email protected]>

xdp: Copy provider buffers on pass and redirect

This commit introduces logic to copy provider buffers into kernel pages on pass and redirects, handling fragments appropriately. It splits the copy from the release for redirects, maintaining the classic zero-copy path while resolving frag addresses through xdp_frag_address().

Quoted 4 lines
net/core/xdp.c +0 -0
index 386240bd24c98..b7f16f44dac22 100644

[ ... ]

net/core/xdp.c struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp) · line 724

Quoted 8 lines
net/core/xdp.c +3 -0
@@ -638,6 +718,9 @@ struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp)
 	u32 nr_frags = 0;
 	int metalen;
 
+	if (xdp_buff_has_netmem(xdp))
+		return xdp_build_skb_from_zc(xdp);
+
 	if (unlikely(xdp_buff_has_frags(xdp))) {

[Severity: High]
Does generalizing xdp_build_skb_from_zc() to handle all netmem buffers cause a payload truncation when metadata is unsupported?

Previously, xdp_build_skb_from_zc() only handled pure XSK buffers, where data_meta == data is guaranteed. Now, it can receive generic provider-backed netmem buffers.

If a netmem provider driver does not support metadata and initializes the buffer via xdp_prepare_buff() with false, xdp->data_meta is set to xdp->data + 1.

Looking at the length calculation and copy in xdp_build_skb_from_zc():

struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp)

{
        u32 headroom = xdp->data_meta - xdp->data_hard_start;
        ...
        u32 len = xdp->data_end - xdp->data_meta;
        ...
        memcpy(__skb_put(skb, len), xdp->data_meta, len);
}

Without checking xdp_data_meta_unsupported(xdp), it blindly uses xdp->data_meta to determine the length and as the source for memcpy().

Could this cause the first byte of every packet to be truncated and the payload to be shifted by one byte, leading to silent data corruption on XDP_PASS?

Should xdp_build_skb_from_zc() check xdp_data_meta_unsupported(xdp) similar to how xdp_copy_zc_to_xdp_frame() handles it?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
#29 lore raw

Re: [RFC net-next 11/15] xdp: Copy provider buffers on pass and redirect

Quoted 54 lines · [email protected] writes:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] net: xdp: payload truncation in xdp_build_skb_from_zc() when metadata unsupported --

commit a36f92358b032a7e20669d832f27102e55f9f4eb
Author: Björn Töpel <[email protected]>

xdp: Copy provider buffers on pass and redirect

This commit introduces logic to copy provider buffers into kernel pages on pass and redirects, handling fragments appropriately. It splits the copy from the release for redirects, maintaining the classic zero-copy path while resolving frag addresses through xdp_frag_address().

Quoted 4 lines
net/core/xdp.c +0 -0
index 386240bd24c98..b7f16f44dac22 100644

[ ... ]

net/core/xdp.c struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp) · line 724

Quoted 8 lines
net/core/xdp.c +3 -0
@@ -638,6 +718,9 @@ struct sk_buff *xdp_build_skb_from_buff(const struct xdp_buff *xdp)
 	u32 nr_frags = 0;
 	int metalen;
 
+	if (xdp_buff_has_netmem(xdp))
+		return xdp_build_skb_from_zc(xdp);
+
 	if (unlikely(xdp_buff_has_frags(xdp))) {

[Severity: High]
Does generalizing xdp_build_skb_from_zc() to handle all netmem buffers cause a payload truncation when metadata is unsupported?

Previously, xdp_build_skb_from_zc() only handled pure XSK buffers, where data_meta == data is guaranteed. Now, it can receive generic provider-backed netmem buffers.

If a netmem provider driver does not support metadata and initializes the buffer via xdp_prepare_buff() with false, xdp->data_meta is set to xdp->data + 1.

Looking at the length calculation and copy in xdp_build_skb_from_zc():

struct sk_buff *xdp_build_skb_from_zc(struct xdp_buff *xdp)

{
        u32 headroom = xdp->data_meta - xdp->data_hard_start;
        ...
        u32 len = xdp->data_end - xdp->data_meta;
        ...
        memcpy(__skb_put(skb, len), xdp->data_meta, len);
}

Without checking xdp_data_meta_unsupported(xdp), it blindly uses xdp->data_meta to determine the length and as the source for memcpy().

Quoted 6 lines

Could this cause the first byte of every packet to be truncated and the payload to be shifted by one byte, leading to silent data corruption on XDP_PASS?

Should xdp_build_skb_from_zc() check xdp_data_meta_unsupported(xdp) similar to how xdp_copy_zc_to_xdp_frame() handles it?

Yep. Unsupported metadata should copy from data, with the matching length. I'll fix that and add coverage.

#30 lore raw

RFC 12/15 xsk: Receive provider UMEM without copying

In reply to Björn Töpel

When all buffers of a packet come from the provider of the target socket, put their UMEM addresses straight on the socket's RX ring. For a single-buffer packet, xsk_rcv() checks the head buffer. For a multi-buffer packet, every fragment is checked. A socket without XDP_USE_SG drops multi-buffer packets and counts them in rx_dropped. If any fragment comes from elsewhere, the packet is copied as before. Fragment descriptors carry the offset that the device used.

The buffers leave page_pool ownership in batches when the socket is flushed. A batch is split where a queue restart changed the page pool.

The copy path now reads fragments with xdp_frag_address().

---
 include/net/xdp_sock.h |  10 +++
 net/xdp/xsk.c          | 191 ++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 198 insertions(+), 3 deletions(-)
include/net/xdp_sock.h +10 -0
index 6e70b320b399..92a7cf49f5e3 100644
@@ -12,10 +12,18 @@
 #include <linux/mutex.h>
 #include <linux/spinlock.h>
 #include <linux/mm.h>
+#include <net/netmem.h>
 #include <net/sock.h>
 
 #define XDP_UMEM_SG_FLAG BIT(3)
 
+/* Bound both one maximum-SG packet and single-buffer release batching. */
+#if MAX_SKB_FRAGS < 31
+#define XSK_PP_RELEASE_BATCH 32
+#else
+#define XSK_PP_RELEASE_BATCH (MAX_SKB_FRAGS + 1)
+#endif
+
 struct net_device;
 struct xsk_queue;
 struct xdp_buff;
@@ -61,6 +69,8 @@ struct xdp_sock {
 		XSK_BOUND,
 		XSK_UNBOUND,
 	} state;
+	u32 pp_release_cnt;
+	netmem_ref pp_release[XSK_PP_RELEASE_BATCH];
 
 	struct xsk_queue *tx ____cacheline_aligned_in_smp;
 	struct list_head tx_list;
net/xdp/xsk.c +188 -3

242 lines not on this page · raw message

Signature · 1 line
2.55.0
#31 lore raw

RFC 13/15 eth: fbnic: Support AF_XDP zero-copy receive

In reply to Björn Töpel

fbnic gets RX buffers from page_pool. Use the XSK buffer pool as the RX queue's page-pool memory provider, so allocation, DMA sync, recycling and refill stay in page_pool. Only aligned 4 KiB UMEM chunks are supported.

Each UMEM frame on the header queue must hold one packet. Raise the header-split threshold to at least 3072 bytes, more than half of a 4 KiB page, so the device finishes the frame after one header. Cap it to the space left in the frame after the headroom. An MTU above the threshold needs a socket with XDP_USE_SG and an XDP program with frags support. With XDP_USE_SG, the payload queue also uses UMEM frames, and the device uses each frame for one descriptor only. Without it, the payload queue uses a normal page pool. If the device does not finish a provider frame after one descriptor, drop the packet.

The BDQ refill loop now alternates header and payload buffers on all queues, so two rings that share one finite provider stay balanced. It keeps NAPI scheduled while the provider still has buffers.

Provider frames are never split, so they get no fragment bias. A redirected frame can be recycled before its old BDQ slot is cleaned, and a stale bias would corrupt the new allocation.

The headroom comes from the queue configuration. It must be at least XDP_PACKET_HEADROOM, a multiple of 128 bytes, and fit the hardware field; otherwise queue validation fails. MTU, ring parameter and XDP program changes that do not fit an active provider are rejected.

fbnic did not support XDP_REDIRECT. Add it, with xdp_do_flush(), and advertise it. This also enables copy-mode AF_XDP and the other redirect targets on fbnic. Do not advertise AF_XDP zero copy until TX support is added.

---
 .../net/ethernet/meta/fbnic/fbnic_ethtool.c   |   5 +
 .../net/ethernet/meta/fbnic/fbnic_netdev.c    | 140 ++++++-
 .../net/ethernet/meta/fbnic/fbnic_netdev.h    |   4 +
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.c  | 350 +++++++++++++-----
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.h  |   6 +
 5 files changed, 402 insertions(+), 103 deletions(-)
drivers/net/ethernet/meta/fbnic/fbnic_ethtool.c +5 -0
index e774740fcb5d..188abe0ead14 100644
@@ -385,6 +385,11 @@ fbnic_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
 		return -EINVAL;
 	}
 
+	err = fbnic_xsk_validate(fbn, fbn->xdp_prog, netdev->mtu,
+				 kernel_ring->hds_thresh, extack);
+	if (err)
+		return err;
+
 	if (!netif_running(netdev)) {
 		fbnic_set_rings(fbn, ring, kernel_ring);
 		return 0;
drivers/net/ethernet/meta/fbnic/fbnic_netdev.c +126 -14

177 lines not on this page · raw message

drivers/net/ethernet/meta/fbnic/fbnic_netdev.h +4 -0
index eded20b0e9e4..ba425302ccfd 100644
@@ -116,4 +116,8 @@ int fbnic_phylink_init(struct net_device *netdev);
 void fbnic_phylink_pmd_training_complete_notify(struct net_device *netdev);
 bool fbnic_check_split_frames(struct bpf_prog *prog,
 			      unsigned int mtu, u32 hds_threshold);
+int fbnic_xsk_validate(struct fbnic_net *fbn, struct bpf_prog *prog,
+		       unsigned int mtu, u32 hds_thresh,
+		       struct netlink_ext_ack *extack);
+u32 fbnic_xsk_hds_thresh(u32 headroom, u32 hds_thresh);
 #endif /* _FBNIC_NETDEV_H_ */
drivers/net/ethernet/meta/fbnic/fbnic_txrx.c +261 -89

624 lines not on this page · raw message

drivers/net/ethernet/meta/fbnic/fbnic_txrx.h +6 -0
index 6ee3cce3d942..abc96a12ea5c 100644
@@ -15,6 +15,7 @@
 #include "fbnic_csr.h"
 
 struct fbnic_net;
+struct xsk_buff_pool;
 
 /* Guarantee we have space needed for storing the buffer
  * To store the buffer we need:
@@ -77,12 +78,14 @@ struct fbnic_net;
 	(4096 - FBNIC_RX_HROOM - FBNIC_RX_TROOM - FBNIC_RX_PAD)
 #define FBNIC_HDS_THRESH_DEFAULT \
 	(1536 - FBNIC_RX_PAD)
+#define FBNIC_XSK_HDS_THRESH		3072
 #define FBNIC_HDR_BYTES_MIN		256
 
 struct fbnic_pkt_buff {
 	struct xdp_buff buff;
 	ktime_t hwtstamp;
 	bool add_frag_failed;
+	struct skb_shared_info sinfo;
 };
 
 struct fbnic_queue_stats {
@@ -162,6 +165,9 @@ struct fbnic_ring {
 struct fbnic_q_triad {
 	struct fbnic_ring sub0, sub1, cmpl;
 	struct xdp_rxq_info xdp_rxq;
+	struct xsk_buff_pool *xsk_pool;
+	u16 rx_hroom;
+	u16 rx_frame_off;
 };
 
 struct fbnic_napi_vector {
Signature · 1 line
2.55.0
#32 lore raw

Re: [RFC net-next 13/15] eth: fbnic: Support AF_XDP zero-copy receive

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider: - [High] net: deadlock on 32-bit during u64_stats_sync update with preemption enabled --

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] net: deadlock on 32-bit during u64_stats_sync update with preemption enabled --

commit 1bc54e591deb70184971cd19a4b1a9d18715cb4e
Author: Björn Töpel <[email protected]>

eth: fbnic: Support AF_XDP zero-copy receive

This commit adds support for AF_XDP zero-copy receive in the fbnic driver. It integrates the XSK buffer pool as a memory provider for page_pool and refactors the receive ring refill loops to balance header and payload buffers.

Quoted 3 lines
drivers/net/ethernet/meta/fbnic/fbnic_txrx.c +0 -0

[ ... ]

drivers/net/ethernet/meta/fbnic/fbnic_txrx.c static void fbnic_bd_prep(struct fbnic_ring *bdq, u16 id, netmem_ref netmem) · line 950

Quoted 12 lines
drivers/net/ethernet/meta/fbnic/fbnic_txrx.c +5 -5
-static void fbnic_fill_bdq(struct fbnic_ring *bdq)
+static bool fbnic_fill_bdq_one(struct fbnic_ring *bdq, unsigned int *tail)
 {
-	unsigned int count = fbnic_desc_unused(bdq);
-	unsigned int i = bdq->tail;
+	netmem_ref netmem;
 
-	if (!count)
-		return;
+	netmem = page_pool_dev_alloc_netmems(bdq->page_pool);
+	if (!netmem) {
+		u64_stats_update_begin(&bdq->stats.syncp);

[Severity: High]
Does this code risk a deadlock on 32-bit systems when called from process context with preemption enabled?

If memory allocation fails here during interface bring-up (e.g., via ndo_open -> fbnic_up() -> fbnic_fill() -> fbnic_nv_fill() -> fbnic_fill_bdqs() -> fbnic_fill_bdq_one()), the sequence counter is updated without disabling preemption.

If the thread is preempted while the sequence counter is odd, and another thread attempts to read network statistics (like ndo_get_stats64 or get_queue_stats_rx), the reader will spin forever preventing the writer from finishing, leading to a hard deadlock.

Quoted 4 lines
drivers/net/ethernet/meta/fbnic/fbnic_txrx.c +4 -0
+		bdq->stats.bdq.alloc_failed++;
+		u64_stats_update_end(&bdq->stats.syncp);
+		return false;
+	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
#33 lore raw

Re: [RFC net-next 13/15] eth: fbnic: Support AF_XDP zero-copy receive

Quoted 39 lines · [email protected] writes:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [High] net: deadlock on 32-bit during u64_stats_sync update with preemption enabled --

commit 1bc54e591deb70184971cd19a4b1a9d18715cb4e
Author: Björn Töpel <[email protected]>

eth: fbnic: Support AF_XDP zero-copy receive

This commit adds support for AF_XDP zero-copy receive in the fbnic driver. It integrates the XSK buffer pool as a memory provider for page_pool and refactors the receive ring refill loops to balance header and payload buffers.

Quoted 3 lines
drivers/net/ethernet/meta/fbnic/fbnic_txrx.c +0 -0

[ ... ]

drivers/net/ethernet/meta/fbnic/fbnic_txrx.c static void fbnic_bd_prep(struct fbnic_ring *bdq, u16 id, netmem_ref netmem) · line 950

Quoted 12 lines
drivers/net/ethernet/meta/fbnic/fbnic_txrx.c +5 -5
-static void fbnic_fill_bdq(struct fbnic_ring *bdq)
+static bool fbnic_fill_bdq_one(struct fbnic_ring *bdq, unsigned int *tail)
 {
-	unsigned int count = fbnic_desc_unused(bdq);
-	unsigned int i = bdq->tail;
+	netmem_ref netmem;
 
-	if (!count)
-		return;
+	netmem = page_pool_dev_alloc_netmems(bdq->page_pool);
+	if (!netmem) {
+		u64_stats_update_begin(&bdq->stats.syncp);

[Severity: High]
Does this code risk a deadlock on 32-bit systems when called from process context with preemption enabled?

If memory allocation fails here during interface bring-up (e.g., via ndo_open -> fbnic_up() -> fbnic_fill() -> fbnic_nv_fill() -> fbnic_fill_bdqs() -> fbnic_fill_bdq_one()), the sequence counter is updated without disabling preemption.

Quoted 4 lines

If the thread is preempted while the sequence counter is odd, and another thread attempts to read network statistics (like ndo_get_stats64 or get_queue_stats_rx), the reader will spin forever preventing the writer from finishing, leading to a hard deadlock.

Hmm, this concerns the 32-bit path.I'll check the process-context protection on that path.

#34 lore raw

Re: [RFC net-next 13/15] eth: fbnic: Support AF_XDP zero-copy receive

In reply to Björn Töpel
Quoted 213 lines · Björn Töpel

fbnic gets RX buffers from page_pool. Use the XSK buffer pool as the RX queue's page-pool memory provider, so allocation, DMA sync, recycling and refill stay in page_pool. Only aligned 4 KiB UMEM chunks are supported.

Each UMEM frame on the header queue must hold one packet. Raise the header-split threshold to at least 3072 bytes, more than half of a 4 KiB page, so the device finishes the frame after one header. Cap it to the space left in the frame after the headroom. An MTU above the threshold needs a socket with XDP_USE_SG and an XDP program with frags support. With XDP_USE_SG, the payload queue also uses UMEM frames, and the device uses each frame for one descriptor only. Without it, the payload queue uses a normal page pool. If the device does not finish a provider frame after one descriptor, drop the packet.

The BDQ refill loop now alternates header and payload buffers on all queues, so two rings that share one finite provider stay balanced. It keeps NAPI scheduled while the provider still has buffers.

Provider frames are never split, so they get no fragment bias. A redirected frame can be recycled before its old BDQ slot is cleaned, and a stale bias would corrupt the new allocation.

The headroom comes from the queue configuration. It must be at least XDP_PACKET_HEADROOM, a multiple of 128 bytes, and fit the hardware field; otherwise queue validation fails. MTU, ring parameter and XDP program changes that do not fit an active provider are rejected.

fbnic did not support XDP_REDIRECT. Add it, with xdp_do_flush(), and advertise it. This also enables copy-mode AF_XDP and the other redirect targets on fbnic. Do not advertise AF_XDP zero copy until TX support is added.

---
 .../net/ethernet/meta/fbnic/fbnic_ethtool.c   |   5 +
 .../net/ethernet/meta/fbnic/fbnic_netdev.c    | 140 ++++++-
 .../net/ethernet/meta/fbnic/fbnic_netdev.h    |   4 +
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.c  | 350 +++++++++++++-----
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.h  |   6 +
 5 files changed, 402 insertions(+), 103 deletions(-)
drivers/net/ethernet/meta/fbnic/fbnic_ethtool.c +5 -0
index e774740fcb5d..188abe0ead14 100644
@@ -385,6 +385,11 @@ fbnic_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
 		return -EINVAL;
 	}
 
+	err = fbnic_xsk_validate(fbn, fbn->xdp_prog, netdev->mtu,
+				 kernel_ring->hds_thresh, extack);
+	if (err)
+		return err;
+
 	if (!netif_running(netdev)) {
 		fbnic_set_rings(fbn, ring, kernel_ring);
 		return 0;
drivers/net/ethernet/meta/fbnic/fbnic_netdev.c +119 -1
index 8a6703afca38..038e91ef14e4 100644
@@ -5,6 +5,8 @@
 #include <linux/ipv6.h>
 #include <linux/types.h>
 #include <net/netdev_queues.h>
+#include <net/netdev_rx_queue.h>
+#include <net/xdp_sock_drv.h>
 
 #include "fbnic.h"
 #include "fbnic_netdev.h"
@@ -276,6 +278,8 @@ static int fbnic_set_mac(struct net_device *netdev, void *p)
 static int fbnic_change_mtu(struct net_device *dev, int new_mtu)
 {
 	struct fbnic_net *fbn = netdev_priv(dev);
+	struct netlink_ext_ack extack = {};
+	int err;
 
 	if (fbnic_check_split_frames(fbn->xdp_prog, new_mtu, fbn->hds_thresh)) {
 		dev_err(&dev->dev,
@@ -285,6 +289,15 @@ static int fbnic_change_mtu(struct net_device *dev, int new_mtu)
 		return -EINVAL;
 	}
 
+	err = fbnic_xsk_validate(fbn, fbn->xdp_prog, new_mtu,
+				 fbn->hds_thresh, &extack);
+	if (err) {
+		if (extack._msg)
+			netdev_err(dev, "%s\n", extack._msg);
+
+		return err;
+	}
+
 	WRITE_ONCE(dev->mtu, new_mtu);
 
 	return 0;
@@ -534,26 +547,123 @@ bool fbnic_check_split_frames(struct bpf_prog *prog, unsigned int mtu,
 	return mtu + ETH_HLEN > hds_thresh;
 }
 
+u32 fbnic_xsk_hds_thresh(u32 headroom, u32 hds_thresh)
+{
+	u32 max_hds;
+
+	/* Reserve more than half a device page for each header so the device
+	 * retires the provider frame after one descriptor.
+	 */
+	static_assert(2 * FBNIC_XSK_HDS_THRESH > FBNIC_BD_PAGE_SIZE);
+
+	max_hds = FBNIC_BD_PAGE_SIZE - headroom - FBNIC_RX_TROOM -
+		  FBNIC_RX_PAD;
+
+	return min(max_t(u32, hds_thresh, FBNIC_XSK_HDS_THRESH), max_hds);
+}
+
+static int fbnic_xsk_validate_frames(struct xsk_buff_pool *pool,
+				     struct bpf_prog *prog,
+				     unsigned int mtu, u32 hds_thresh,
+				     struct netlink_ext_ack *extack)
+{
+	u32 xsk_hds_thresh;
+
+	xsk_hds_thresh = fbnic_xsk_hds_thresh(xsk_pool_get_headroom(pool),
+					      hds_thresh);
+
+	if (mtu + ETH_HLEN <= xsk_hds_thresh)
+		return 0;
+
+	if (!xsk_pool_uses_sg(pool)) {
+		NL_SET_ERR_MSG_MOD(extack,
+				   "AF_XDP zero-copy socket requires XDP_USE_SG");
+		return -EOPNOTSUPP;
+	}
+
+	if (fbnic_check_split_frames(prog, mtu, xsk_hds_thresh)) {
+		NL_SET_ERR_MSG_MOD(extack,
+				   "AF_XDP zero-copy socket requires XDP frags");
+		return -EOPNOTSUPP;
+	}
+
+	return 0;
+}
+
+int fbnic_xsk_validate(struct fbnic_net *fbn, struct bpf_prog *prog,
+		       unsigned int mtu, u32 hds_thresh,
+		       struct netlink_ext_ack *extack)
+{
+	struct net_device *netdev = fbn->netdev;
+	struct xsk_buff_pool *pool;
+	unsigned int qid;
+	int err;
+
+	for (qid = 0; qid < fbn->num_rx_queues; qid++) {
+		pool = xsk_get_pool_from_rxq(netdev, qid);
+		if (!pool)
+			continue;
+
+		err = fbnic_xsk_validate_frames(pool, prog, mtu, hds_thresh,
+						extack);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
+static int fbnic_xsk_setup_pool(struct net_device *netdev,
+				struct xsk_buff_pool *pool, u16 queue_id,
+				struct netlink_ext_ack *extack)
+{
+	struct fbnic_net *fbn = netdev_priv(netdev);
+	int err;
+
+	if (queue_id >= fbn->num_rx_queues)
+		return -EINVAL;
+
+	if (!pool)
+		return xsk_pool_setup_page_pool(netdev, NULL, queue_id, extack);
+
+	err = fbnic_xsk_validate_frames(pool, fbn->xdp_prog, netdev->mtu,
+					fbn->hds_thresh, extack);
+	if (err)
+		return err;
+
+	return xsk_pool_setup_page_pool(netdev, pool, queue_id, extack);
+}
+
 static int fbnic_bpf(struct net_device *netdev, struct netdev_bpf *bpf)
 {
 	struct bpf_prog *prog = bpf->prog, *prev_prog;
 	struct fbnic_net *fbn = netdev_priv(netdev);
+	int err;
 
-	if (bpf->command != XDP_SETUP_PROG)
+	switch (bpf->command) {
+	case XDP_SETUP_PROG:
+		if (fbnic_check_split_frames(prog, netdev->mtu,
+					     fbn->hds_thresh)) {
+			NL_SET_ERR_MSG_MOD(bpf->extack,
+					   "MTU too high, or HDS threshold is too low for single buffer XDP");
+			return -EOPNOTSUPP;
+		}
+
+		err = fbnic_xsk_validate(fbn, prog, netdev->mtu,
+					 fbn->hds_thresh, bpf->extack);
+		if (err)
+			return err;
+
+		prev_prog = xchg(&fbn->xdp_prog, prog);
+		if (prev_prog)
+			bpf_prog_put(prev_prog);
+		return 0;

[..]

drivers/net/ethernet/meta/fbnic/fbnic_netdev.c bool fbnic_check_split_frames(struct bpf_prog *prog, unsigned int mtu, · line 663

Quoted 3 lines
drivers/net/ethernet/meta/fbnic/fbnic_netdev.c +3 -0
+	case XDP_SETUP_XSK_POOL:
+		return fbnic_xsk_setup_pool(netdev, bpf->xsk.pool,
+					    bpf->xsk.queue_id, bpf->extack);

Is there a good reason to ask drivers to call us back with xsk_pool_setup_page_pool? For the PP mode, maybe we should add new XSK_VALIDATE_POOL or something? So most of that fbnic_xsk_setup_pool stays in core?

I guess I'm mentally trying to have a clear separation between pp vs non-pp modes. Even that whole xsk_buff_pool might be legacy non-pp artifact? New drivers shouldn't bother with it? Or is it too much?

#35 lore raw

Re: [RFC net-next 13/15] eth: fbnic: Support AF_XDP zero-copy receive

Quoted 217 lines · Stanislav Fomichev
Quoted 213 lines · Björn Töpel

fbnic gets RX buffers from page_pool. Use the XSK buffer pool as the RX queue's page-pool memory provider, so allocation, DMA sync, recycling and refill stay in page_pool. Only aligned 4 KiB UMEM chunks are supported.

Each UMEM frame on the header queue must hold one packet. Raise the header-split threshold to at least 3072 bytes, more than half of a 4 KiB page, so the device finishes the frame after one header. Cap it to the space left in the frame after the headroom. An MTU above the threshold needs a socket with XDP_USE_SG and an XDP program with frags support. With XDP_USE_SG, the payload queue also uses UMEM frames, and the device uses each frame for one descriptor only. Without it, the payload queue uses a normal page pool. If the device does not finish a provider frame after one descriptor, drop the packet.

The BDQ refill loop now alternates header and payload buffers on all queues, so two rings that share one finite provider stay balanced. It keeps NAPI scheduled while the provider still has buffers.

Provider frames are never split, so they get no fragment bias. A redirected frame can be recycled before its old BDQ slot is cleaned, and a stale bias would corrupt the new allocation.

The headroom comes from the queue configuration. It must be at least XDP_PACKET_HEADROOM, a multiple of 128 bytes, and fit the hardware field; otherwise queue validation fails. MTU, ring parameter and XDP program changes that do not fit an active provider are rejected.

fbnic did not support XDP_REDIRECT. Add it, with xdp_do_flush(), and advertise it. This also enables copy-mode AF_XDP and the other redirect targets on fbnic. Do not advertise AF_XDP zero copy until TX support is added.

---
 .../net/ethernet/meta/fbnic/fbnic_ethtool.c   |   5 +
 .../net/ethernet/meta/fbnic/fbnic_netdev.c    | 140 ++++++-
 .../net/ethernet/meta/fbnic/fbnic_netdev.h    |   4 +
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.c  | 350 +++++++++++++-----
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.h  |   6 +
 5 files changed, 402 insertions(+), 103 deletions(-)
drivers/net/ethernet/meta/fbnic/fbnic_ethtool.c +5 -0
index e774740fcb5d..188abe0ead14 100644
@@ -385,6 +385,11 @@ fbnic_set_ringparam(struct net_device *netdev, struct ethtool_ringparam *ring,
 		return -EINVAL;
 	}
 
+	err = fbnic_xsk_validate(fbn, fbn->xdp_prog, netdev->mtu,
+				 kernel_ring->hds_thresh, extack);
+	if (err)
+		return err;
+
 	if (!netif_running(netdev)) {
 		fbnic_set_rings(fbn, ring, kernel_ring);
 		return 0;
drivers/net/ethernet/meta/fbnic/fbnic_netdev.c +119 -1
index 8a6703afca38..038e91ef14e4 100644
@@ -5,6 +5,8 @@
 #include <linux/ipv6.h>
 #include <linux/types.h>
 #include <net/netdev_queues.h>
+#include <net/netdev_rx_queue.h>
+#include <net/xdp_sock_drv.h>
 
 #include "fbnic.h"
 #include "fbnic_netdev.h"
@@ -276,6 +278,8 @@ static int fbnic_set_mac(struct net_device *netdev, void *p)
 static int fbnic_change_mtu(struct net_device *dev, int new_mtu)
 {
 	struct fbnic_net *fbn = netdev_priv(dev);
+	struct netlink_ext_ack extack = {};
+	int err;
 
 	if (fbnic_check_split_frames(fbn->xdp_prog, new_mtu, fbn->hds_thresh)) {
 		dev_err(&dev->dev,
@@ -285,6 +289,15 @@ static int fbnic_change_mtu(struct net_device *dev, int new_mtu)
 		return -EINVAL;
 	}
 
+	err = fbnic_xsk_validate(fbn, fbn->xdp_prog, new_mtu,
+				 fbn->hds_thresh, &extack);
+	if (err) {
+		if (extack._msg)
+			netdev_err(dev, "%s\n", extack._msg);
+
+		return err;
+	}
+
 	WRITE_ONCE(dev->mtu, new_mtu);
 
 	return 0;
@@ -534,26 +547,123 @@ bool fbnic_check_split_frames(struct bpf_prog *prog, unsigned int mtu,
 	return mtu + ETH_HLEN > hds_thresh;
 }
 
+u32 fbnic_xsk_hds_thresh(u32 headroom, u32 hds_thresh)
+{
+	u32 max_hds;
+
+	/* Reserve more than half a device page for each header so the device
+	 * retires the provider frame after one descriptor.
+	 */
+	static_assert(2 * FBNIC_XSK_HDS_THRESH > FBNIC_BD_PAGE_SIZE);
+
+	max_hds = FBNIC_BD_PAGE_SIZE - headroom - FBNIC_RX_TROOM -
+		  FBNIC_RX_PAD;
+
+	return min(max_t(u32, hds_thresh, FBNIC_XSK_HDS_THRESH), max_hds);
+}
+
+static int fbnic_xsk_validate_frames(struct xsk_buff_pool *pool,
+				     struct bpf_prog *prog,
+				     unsigned int mtu, u32 hds_thresh,
+				     struct netlink_ext_ack *extack)
+{
+	u32 xsk_hds_thresh;
+
+	xsk_hds_thresh = fbnic_xsk_hds_thresh(xsk_pool_get_headroom(pool),
+					      hds_thresh);
+
+	if (mtu + ETH_HLEN <= xsk_hds_thresh)
+		return 0;
+
+	if (!xsk_pool_uses_sg(pool)) {
+		NL_SET_ERR_MSG_MOD(extack,
+				   "AF_XDP zero-copy socket requires XDP_USE_SG");
+		return -EOPNOTSUPP;
+	}
+
+	if (fbnic_check_split_frames(prog, mtu, xsk_hds_thresh)) {
+		NL_SET_ERR_MSG_MOD(extack,
+				   "AF_XDP zero-copy socket requires XDP frags");
+		return -EOPNOTSUPP;
+	}
+
+	return 0;
+}
+
+int fbnic_xsk_validate(struct fbnic_net *fbn, struct bpf_prog *prog,
+		       unsigned int mtu, u32 hds_thresh,
+		       struct netlink_ext_ack *extack)
+{
+	struct net_device *netdev = fbn->netdev;
+	struct xsk_buff_pool *pool;
+	unsigned int qid;
+	int err;
+
+	for (qid = 0; qid < fbn->num_rx_queues; qid++) {
+		pool = xsk_get_pool_from_rxq(netdev, qid);
+		if (!pool)
+			continue;
+
+		err = fbnic_xsk_validate_frames(pool, prog, mtu, hds_thresh,
+						extack);
+		if (err)
+			return err;
+	}
+
+	return 0;
+}
+
+static int fbnic_xsk_setup_pool(struct net_device *netdev,
+				struct xsk_buff_pool *pool, u16 queue_id,
+				struct netlink_ext_ack *extack)
+{
+	struct fbnic_net *fbn = netdev_priv(netdev);
+	int err;
+
+	if (queue_id >= fbn->num_rx_queues)
+		return -EINVAL;
+
+	if (!pool)
+		return xsk_pool_setup_page_pool(netdev, NULL, queue_id, extack);
+
+	err = fbnic_xsk_validate_frames(pool, fbn->xdp_prog, netdev->mtu,
+					fbn->hds_thresh, extack);
+	if (err)
+		return err;
+
+	return xsk_pool_setup_page_pool(netdev, pool, queue_id, extack);
+}
+
 static int fbnic_bpf(struct net_device *netdev, struct netdev_bpf *bpf)
 {
 	struct bpf_prog *prog = bpf->prog, *prev_prog;
 	struct fbnic_net *fbn = netdev_priv(netdev);
+	int err;
 
-	if (bpf->command != XDP_SETUP_PROG)
+	switch (bpf->command) {
+	case XDP_SETUP_PROG:
+		if (fbnic_check_split_frames(prog, netdev->mtu,
+					     fbn->hds_thresh)) {
+			NL_SET_ERR_MSG_MOD(bpf->extack,
+					   "MTU too high, or HDS threshold is too low for single buffer XDP");
+			return -EOPNOTSUPP;
+		}
+
+		err = fbnic_xsk_validate(fbn, prog, netdev->mtu,
+					 fbn->hds_thresh, bpf->extack);
+		if (err)
+			return err;
+
+		prev_prog = xchg(&fbn->xdp_prog, prog);
+		if (prev_prog)
+			bpf_prog_put(prev_prog);
+		return 0;

[..]

Quoted 8 lines

drivers/net/ethernet/meta/fbnic/fbnic_netdev.c bool fbnic_check_split_frames(struct bpf_prog *prog, unsigned int mtu, · line 663

Quoted 3 lines
drivers/net/ethernet/meta/fbnic/fbnic_netdev.c +3 -0
+	case XDP_SETUP_XSK_POOL:
+		return fbnic_xsk_setup_pool(netdev, bpf->xsk.pool,
+					    bpf->xsk.queue_id, bpf->extack);

Is there a good reason to ask drivers to call us back with xsk_pool_setup_page_pool? For the PP mode, maybe we should add new XSK_VALIDATE_POOL or something? So most of that fbnic_xsk_setup_pool stays in core?

Makes sense. I'll try core-owned setup with driver opt-in and validation. Existing direct drivers can keep their setup.

Quoted 3 lines

I guess I'm mentally trying to have a clear separation between pp vs non-pp modes. Even that whole xsk_buff_pool might be legacy non-pp artifact? New drivers shouldn't bother with it? Or is it too much?

Hmm. New RX code shouldn't need allocator internals, but rings, DMA and TX state are still shared. I need to noodle a bit on it...

#36 lore raw

RFC 14/15 eth: fbnic: Support AF_XDP zero-copy transmit

In reply to Björn Töpel

AF_XDP zero copy cannot be advertised until fbnic can send socket TX descriptors and report their completion.

Send socket descriptors from the RX NAPI poll, and add ndo_xsk_wakeup to schedule that poll. Record the type of each XDP TX ring entry. On completion, page-pool buffers go back to page_pool and UMEM frames go to the AF_XDP completion ring. XDP_TX of provider RX buffers uses the same records.

Each poll sends at most 64 descriptors per socket, so the application and NAPI can run at the same time. When a vector has several sockets, the first one served changes on each poll. The batch helper does not take a multi-buffer packet that does not fit the budget left. If a batch was sent and the budget left is smaller than a maximum-size packet, keep NAPI scheduled.

Socket TX shares the XDP TX ring with XDP_TX. Do socket TX after RX:
XDP_TX drops a frame that does not fit, but a socket descriptor waits in the socket TX ring.

Advertise AF_XDP zero copy on systems with 4 KiB pages, now that RX and TX both work.

---
 .../net/ethernet/meta/fbnic/fbnic_netdev.c    |  30 +++
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.c  | 209 ++++++++++++++++--
 drivers/net/ethernet/meta/fbnic/fbnic_txrx.h  |  18 ++
 3 files changed, 238 insertions(+), 19 deletions(-)
drivers/net/ethernet/meta/fbnic/fbnic_netdev.c +30 -0
index 038e91ef14e4..f8495b665d97 100644
@@ -666,6 +666,31 @@ static int fbnic_bpf(struct net_device *netdev, struct netdev_bpf *bpf)
 	}
 }
 
+static int fbnic_xsk_wakeup(struct net_device *netdev, u32 queue_id,
+			    u32 __always_unused flags)
+{
+	struct fbnic_net *fbn = netdev_priv(netdev);
+	struct xsk_buff_pool *pool;
+	struct napi_struct *napi;
+
+	if (queue_id >= fbn->num_rx_queues)
+		return -EINVAL;
+	if (!netif_running(netdev))
+		return -ENETDOWN;
+	pool = xsk_get_pool_from_rxq(netdev, queue_id);
+	if (!pool)
+		return -EINVAL;
+
+	napi = __netif_get_rx_queue(netdev, queue_id)->napi;
+	if (WARN_ON_ONCE(!napi))
+		return -EINVAL;
+
+	if (!napi_if_scheduled_mark_missed(napi))
+		napi_schedule(napi);
+
+	return 0;
+}
+
 static const struct net_device_ops fbnic_netdev_ops = {
 	.ndo_open		= fbnic_open,
 	.ndo_stop		= fbnic_stop,
@@ -677,6 +702,7 @@ static const struct net_device_ops fbnic_netdev_ops = {
 	.ndo_set_rx_mode_async	= fbnic_set_rx_mode,
 	.ndo_get_stats64	= fbnic_get_stats64,
 	.ndo_bpf		= fbnic_bpf,
+	.ndo_xsk_wakeup		= fbnic_xsk_wakeup,
 	.ndo_hwtstamp_get	= fbnic_hwtstamp_get,
 	.ndo_hwtstamp_set	= fbnic_hwtstamp_set,
 };
@@ -935,6 +961,10 @@ struct net_device *fbnic_netdev_alloc(struct fbnic_dev *fbd)
 	netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
 			       NETDEV_XDP_ACT_REDIRECT |
 			       NETDEV_XDP_ACT_RX_SG;
+	if (PAGE_SIZE == FBNIC_BD_PAGE_SIZE) {
+		netdev->xdp_features |= NETDEV_XDP_ACT_XSK_ZEROCOPY;
+		netdev->xdp_zc_max_segs = FBNIC_MAX_RX_DATA_DESC;
+	}
 
 	netdev->min_mtu = IPV6_MIN_MTU;
 	netdev->max_mtu = FBNIC_MAX_JUMBO_FRAME_SIZE - ETH_HLEN;
drivers/net/ethernet/meta/fbnic/fbnic_txrx.c +190 -19

313 lines not on this page · raw message

drivers/net/ethernet/meta/fbnic/fbnic_txrx.h +18 -0
index abc96a12ea5c..2fcfe6ac0232 100644
@@ -36,6 +36,7 @@ struct xsk_buff_pool;
  *	+ 4 descriptors for payload
  */
 #define FBNIC_MAX_RX_PKT_DESC	7
+#define FBNIC_MAX_RX_DATA_DESC	(FBNIC_MAX_RX_PKT_DESC - 2)
 #define FBNIC_RX_DESC_MIN	roundup_pow_of_two(FBNIC_MAX_RX_PKT_DESC * 2)
 
 #define FBNIC_MAX_TXQS			128u
@@ -73,6 +74,7 @@ struct xsk_buff_pool;
 #define FBNIC_RING_F_DISABLED		BIT(0)
 #define FBNIC_RING_F_CTX		BIT(1)
 #define FBNIC_RING_F_STATS		BIT(2)	/* Ring's stats may be used */
+#define FBNIC_RING_F_XDP		BIT(3)
 
 #define FBNIC_HDS_THRESH_MAX \
 	(4096 - FBNIC_RX_HROOM - FBNIC_RX_TROOM - FBNIC_RX_PAD)
@@ -121,11 +123,26 @@ struct fbnic_rx_buf {
 	long pagecnt_bias;
 };
 
+enum fbnic_xdp_tx_buf_type {
+	FBNIC_XDP_TX_BUF_NONE	= 0,
+	FBNIC_XDP_TX_BUF_NETMEM,
+	FBNIC_XDP_TX_BUF_XSK,
+};
+
+struct fbnic_xdp_tx_buf {
+	union {
+		struct xsk_buff_pool *xsk_pool;
+		netmem_ref netmem;
+	};
+	u8 type;
+};
+
 struct fbnic_ring {
 	/* Pointer to buffer specific info */
 	union {
 		struct fbnic_pkt_buff *pkt;	/* RCQ */
 		struct fbnic_rx_buf *rx_buf;	/* BDQ */
+		struct fbnic_xdp_tx_buf *xdp_buf; /* TWQ1 */
 		void **tx_buf;			/* TWQ */
 		void *buffer;			/* Generic pointer */
 	};
@@ -179,6 +196,7 @@ struct fbnic_napi_vector {
 	u16 v_idx;
 	u8 txt_count;
 	u8 rxt_count;
+	u8 xsk_tx_start;
 
 	struct fbnic_q_triad qt[];
 };
Signature · 1 line
2.55.0
#37 lore raw

RFC 15/15 Documentation: xsk: Document page-pool zero copy

In reply to Björn Töpel

Describe AF_XDP zero copy on page-pool drivers: the aligned 4 KiB UMEM requirement, how the headroom reaches the driver through the queue configuration, the device-chosen offset in fragment descriptors, the scatter-gather layout, buffer ownership, provider locking, and the order in which a driver enables it.

---
 Documentation/networking/af_xdp.rst | 65 +++++++++++++++++++++++++++++
 1 file changed, 65 insertions(+)
Documentation/networking/af_xdp.rst +65 -0
index cc3f0d16b28f..bdf4029aedeb 100644
@@ -346,6 +346,71 @@ Note that a UMEM can be shared between sockets on the same queue id
 and device, as well as between queues on the same device and between
 devices at the same time.
 
+Page-pool backed zero-copy
+--------------------------
+
+Drivers which use the queue management API can obtain UMEM frames through a
+page-pool memory provider. This is selected by the driver when zero-copy mode
+is requested and does not require a new userspace flag. The FILL ring remains
+the source of receive buffers and all normal AF_XDP ownership rules apply.
+Once installed, the driver remains an ordinary page-pool consumer: allocation,
+DMA synchronization, recycling, release, and refill use the normal page-pool
+interfaces, while provider callbacks hide the UMEM-specific operations.
+
+The initial provider requires 4 KiB base pages and aligned 4 KiB chunks.
+Unaligned chunks are rejected. Configured UMEM headroom is supported. The
+provider requests it through the queue configuration, and the driver includes
+it in the receive DMA offset; with multi-buffer packets it applies to the first
+descriptor as described below.
+
+Like the normal XSK buffer allocator, provider-backed page-pool allocation and
+recycling run in the receive queue's NAPI context. A queue restart prepares
+its replacement before it stops the current queue, so two page pools can use
+one provider for a short time. A provider lock serializes FILL-ring
+consumption and the provider's buffer stack. It is taken once per page-pool
+refill of up to 64 buffers, not per packet. Generic XDP cannot redirect to a
+provider-backed socket; its copy-mode receive path retains the existing XSK
+receive lock.
+
+UMEM frames retain the direct XSK ownership model. The provider does not add a
+per-frame reference count, generation, ownership bitmap, or quarantine state.
+A frame moves between the FILL ring, the owning NAPI context, and userspace;
+userspace must not publish a frame which it does not own. Page-pool teardown
+accounting protects the lifetime of the pool, not ownership of an individual
+UMEM frame.
+
+Provider-backed buffers do not leave that context as kernel-owned memory.
+``XDP_PASS`` copies the packet to kernel-backed skb storage before returning
+the UMEM buffers. Redirects other than a compatible XSKMAP target likewise
+copy to kernel memory. A compatible XSKMAP transfer publishes the UMEM
+descriptors directly to userspace; returning them through the FILL ring makes
+them available to the same queue context again.
+
+For multi-buffer packets, fragment descriptors are assembled in transient
+kernel-owned storage belonging to the RX queue. They are never stored in the
+user-writable UMEM, and are consumed before the NAPI context starts the next
+packet.
+
+The copy on ``XDP_PASS`` is intentional: an skb may outlive the receive NAPI
+poll, whereas a provider frame must be returned by the context which allocated
+it. Applications which expect most packets to pass to the network stack should
+therefore account for this copy when choosing page-pool backed zero-copy.
+
+Drivers may impose additional layout and queue requirements. The initial fbnic
+support accepts configured UMEM headroom from 0 through 256 bytes in 128-byte
+increments (up to 512 bytes including ``XDP_PACKET_HEADROOM``).
+Packets larger than its selected header-data-split threshold require an
+``XDP_USE_SG`` socket and an XDP program with fragment support. Their
+continuation descriptors start at offsets chosen by the device.
+When these restrictions are not met, a bind forced with ``XDP_ZEROCOPY``
+fails with an error; automatic mode may fall back to copy mode.
+
+On a running device, installing or removing the provider restarts the
+selected hardware queue.
+Applications should populate the FILL ring before binding when possible. If
+the ring is empty, the kernel schedules the queue once after installation;
+the usual ``XDP_USE_NEED_WAKEUP`` rules apply after that.
+
 XDP_USE_NEED_WAKEUP bind flag
 -----------------------------
 
Signature · 1 line
2.55.0