Zero copy Rx using io_uring
Subsystems (5)
v4 cover Zero copy Rx using io_uring
This patchset is a proposal that adds zero copy network Rx to io_uring. With it, userspace can register a region of host memory for receiving data directly from a NIC using DMA, without needing a kernel to user copy.
This is still a WIP and has a list of known issues. We're looking for feedback on the overall approach.
Full kernel tree including some out of tree BNXT changes:
https://github.com/spikeh/linux/tree/zcrx
On the userspace side, support is added to both liburing and Netbench:
https://github.com/spikeh/liburing/tree/zcrx2
https://github.com/spikeh/netbench/tree/zcrx
Hardware support is added to the Broadcom BNXT driver. This patchset + userspace code was tested on an Intel Xeon Platinum 8321HC CPU and Broadcom BCM57504 NIC.
Early benchmarks using this prototype, with iperf3 as a load generator, showed a ~50% reduction in overall system memory bandwidth as measured using perf counters. Note that DDIO must be disabled on Intel systems. Build Netbench using the modified liburing above.
This patchset is based on the work by Jonathan Lemon <[email protected]>:
https://lore.kernel.org/io-uring/[email protected]/
Changes in RFC v4:
------------------
* Rebased on top of Mina Almasry's TCP devmem patchset and latest net-next, now sharing common infra e.g.:
* netmem_t and net_iovs
* Page pool memory provider
* The registered buffer (rbuf) completion queue where completions from io_recvzc requests are posted is removed. Now these post into the main completion queue, using big (32-byte) CQEs. The first 16 bytes is an ordinary CQE, while the latter 16 bytes contain the io_uring_rbuf_cqe as before. This vastly simplifies the uAPI and removes a level of indirection in userspace when looking for payloads.
* The rbuf refill queue is still needed for userspace to return buffers to kernel.
* Simplified code and uAPI on the io_uring side, particularly io_recvzc() and io_zc_rx_recv(). Many unnecessary lines were removed e.g. extra msg flags, readlen, etc.
Changes in RFC v3:
------------------
* Rebased on top of Jakub Kicinski's memory provider API RFC. The ZC pool added is now a backend for memory provider.
* We're also reusing ppiov infrastructure. The refcounting rules stay the same but it's shifted into ppiov->refcount. That lets us to flexibly manage buffer lifetimes without adding any extra code to the common networking paths. It'd also make it easier to support dmabufs and device memory in the future.
* io_uring also knows about pages, and so ppiovs might unnecessarily break tools inspecting data, that can easily be solved later.
Many patches are not for upstream as they depend on work in progress, namely from Mina:
* struct netmem_t
* Driver ndo commands for Rx queue configs
* struct page_pool_iov and shared pp infra
Changes in RFC v2:
------------------
* Added copy fallback support if userspace memory allocated for ZC Rx runs out, or if header splitting or flow steering fails.
* Added veth support for ZC Rx, for testing and demonstration. We will need to figure out what driver would be best for such testing functionality in the future. Perhaps netdevsim?
* Added socket registration API to io_uring to associate specific sockets with ifqs/Rx queues for ZC.
* Added multi-socket support, such that multiple connections can be steered into the same hardware Rx queue.
* Added Netbench server/client support.
Known deficiencies that we will address in a future patchset:
* Proper test driver + selftests, maybe netdevsim.
* Revisiting userspace API.
* Multi-region support.
* Steering setup.
* Further optimisation work.
* ...and more.
If you would like to try out this patchset, build and run the kernel tree then build Netbench using liburing, all from forks above.
Run setup.sh first:
https://gist.github.com/isilence/e6a28ce41a545a261566672104afa461
Then run the following commands:
sudo ip netns exec nsserv ./netbench --server_only 1 --v6 false \
--rx "io_uring --provide_buffers 0 --use_zc 1 \
--zc_pool_pages 16384 --zc_ifname ptp-serv" --use_port 9999sudo ip netns exec nscl ./netbench --client_only 1 --v6 false \
--tx "epoll --threads 1 --per_thread 1 --size 2800" \
--host 10.10.10.20 --use_port 9999David Wei (7):
io_uring: introduce interface queue
io_uring: add mmap support for shared ifq ringbuffers
netdev: add XDP_SETUP_ZC_RX command
io_uring: setup ZC for an Rx queue when registering an ifq
io_uring: add zero copy buf representation and pool
io_uring: add io_recvzc request
io_uring/zcrx: add copy fallback
Pavel Begunkov (9):
net: generalise pp provider params passing
io_uring: delayed cqe commit
net: page_pool: add ->scrub mem provider callback
io_uring: separate header for exported net bits io_uring/zcrx: implement socket registration
io_uring: implement pp memory provider for zc rx io_uring/zcrx: implement PP_FLAG_DMA_* handling
net: execute custom callback from napi
veth: add support for io_uring zc rx
drivers/net/veth.c | 214 +++++++- include/linux/io_uring.h | 6 - include/linux/io_uring/net.h | 30 ++ include/linux/io_uring_types.h | 5 + include/linux/net.h | 2 + include/linux/netdevice.h | 6 + include/net/busy_poll.h | 7 + include/net/netdev_rx_queue.h | 3 + include/net/page_pool/types.h | 2 + include/uapi/linux/io_uring.h | 50 ++ io_uring/Makefile | 3 +- io_uring/io_uring.c | 15 +- io_uring/io_uring.h | 10 + io_uring/net.c | 108 +++- io_uring/opdef.c | 16 + io_uring/register.c | 13 + io_uring/uring_cmd.c | 1 + io_uring/zc_rx.c | 916 +++++++++++++++++++++++++++++++++ io_uring/zc_rx.h | 83 +++ net/core/dev.c | 48 +- net/core/page_pool.c | 8 +- net/socket.c | 3 +- 22 files changed, 1530 insertions(+), 19 deletions(-) create mode 100644 include/linux/io_uring/net.h create mode 100644 io_uring/zc_rx.c create mode 100644 io_uring/zc_rx.h
Signature · 1 line
2.43.0
RFC PATCH v4 1/16 net: generalise pp provider params passing
From: Pavel Begunkov <[email protected]>
RFC only, not for upstream
Add a way to pass custom page pool parameters, but the final version should converge with devmem.
--- include/net/netdev_rx_queue.h | 3 +++ net/core/dev.c | 2 +- net/core/page_pool.c | 3 +++ 3 files changed, 7 insertions(+), 1 deletion(-)
include/net/netdev_rx_queue.h +3 -0
@@ -26,6 +26,9 @@ struct netdev_rx_queue {*/ struct napi_struct *napi;struct netdev_dmabuf_binding *binding; + +const struct memory_provider_ops *pp_ops; +void *pp_private; }____cacheline_aligned_in_smp ;/*
net/core/dev.c +1 -1
@@ -2189,7 +2189,7 @@ int netdev_bind_dmabuf_to_queue(struct net_device *dev, u32 rxq_idx, rxq =__netif_get_rx_queue (dev, rxq_idx); -if (rxq->binding) +if (rxq->binding || rxq->pp_ops)return -EEXIST ; err = xa_alloc(&binding->bound_rxq_list, &xa_idx, rxq, xa_limit_32b,
net/core/page_pool.c +3 -0
@@ -262,6 +262,9 @@ static int page_pool_init(struct page_pool *pool,if (binding) { pool->mp_ops = &dmabuf_devmem_ops; pool->mp_priv = binding; + }else if (pool->p.queue && pool->p.queue->pp_ops) { + pool->mp_ops = pool->p.queue->pp_ops; + pool->mp_priv = pool->p.queue->pp_private; }if (pool->mp_ops) {
Signature · 1 line
2.43.0
RFC PATCH v4 2/16 io_uring: delayed cqe commit
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
RFC only, not for upstream
A stub patch allowing to delay and batch the final step of cqe posting for aux cqes. A different version will be sent separately to upstream.
--- include/linux/io_uring_types.h | 1 + io_uring/io_uring.c | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-)
include/linux/io_uring_types.h +1 -0
@@ -205,6 +205,7 @@ struct io_submit_state {bool plug_started;bool need_plug; +bool flush_cqes;unsigned short submit_nr;unsigned int cqes_count;struct blk_plug plug;
io_uring/io_uring.c +2 -1
@@ -176,7 +176,7 @@ static struct ctl_table kernel_io_uring_disabled_table[] = {static inline void io_submit_flush_completions (struct io_ring_ctx *ctx) {if (!wq_list_empty (&ctx->submit_state.compl_reqs) || - ctx->submit_state.cqes_count) + ctx->submit_state.cqes_count || ctx->submit_state.flush_cqes)__io_submit_flush_completions (ctx); }
@@ -1598,6 +1598,7 @@ void __io_submit_flush_completions(struct io_ring_ctx *ctx)io_free_batch_list (ctx, state->compl_reqs.first);INIT_WQ_LIST (&state->compl_reqs); } + ctx->submit_state.flush_cqes = false; }static unsigned io_cqring_events (struct io_ring_ctx *ctx)
Signature · 1 line
2.43.0
RFC PATCH v4 3/16 net: page_pool: add ->scrub mem provider callback
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
page pool is now waiting for all ppiovs to return before destroying itself, and for that to happen the memory provider might need to push some buffers, flush caches and so on.
todo: we'll try to get by without it before the final release
--- include/net/page_pool/types.h | 1 + net/core/page_pool.c | 3 +++ 2 files changed, 4 insertions(+)
include/net/page_pool/types.h +1 -0
@@ -134,6 +134,7 @@ struct memory_provider_ops {void (*destroy)(struct page_pool *pool);netmem_ref (*alloc_pages)(struct page_pool *pool,gfp_t gfp);bool (*release_page)(struct page_pool *pool,netmem_ref netmem); +void (*scrub)(struct page_pool *pool); };extern const struct memory_provider_ops dmabuf_devmem_ops;
net/core/page_pool.c +3 -0
@@ -984,6 +984,9 @@ static void page_pool_empty_alloc_cache_once(struct page_pool *pool)static void page_pool_scrub (struct page_pool *pool) { +if (pool->mp_ops && pool->mp_ops->scrub) + pool->mp_ops->scrub (pool); +page_pool_empty_alloc_cache_once (pool); pool->destroy_cnt++;
Signature · 1 line
2.43.0
RFC PATCH v4 4/16 io_uring: separate header for exported net bits
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
We're exporting some io_uring bits to networking, e.g. for implementing a net callback for io_uring cmds, but we don't want to expose more than needed. Add a separate header for networking.
--- include/linux/io_uring.h | 6 ------ include/linux/io_uring/net.h | 18 ++++++++++++++++++ io_uring/uring_cmd.c | 1 + net/socket.c | 2 +- 4 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 include/linux/io_uring/net.h
include/linux/io_uring.h +0 -6
@@ -11,7 +11,6 @@ void __io_uring_cancel(bool cancel_all);void __io_uring_free (struct task_struct *tsk);void io_uring_unreg_ringfd (void );const char *io_uring_get_opcode (u8 opcode); -int io_uring_cmd_sock (struct io_uring_cmd *cmd,unsigned int issue_flags);bool io_is_uring_fops (struct file *file);static inline void io_uring_files_cancel (void )
@@ -45,11 +44,6 @@ static inline const char *io_uring_get_opcode(u8 opcode) {return "" ; } -static inline int io_uring_cmd_sock (struct io_uring_cmd *cmd, -unsigned int issue_flags) -{ -return -EOPNOTSUPP ; -}static inline bool io_is_uring_fops (struct file *file) {return false;
include/linux/io_uring/net.h new file +18 -0
@@ -0,0 +1,18 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +#ifndef _LINUX_IO_URING_NET_H +#define _LINUX_IO_URING_NET_H + +struct io_uring_cmd ; + +#if defined(CONFIG_IO_URING ) +int io_uring_cmd_sock (struct io_uring_cmd *cmd,unsigned int issue_flags); + +#else +static inline int io_uring_cmd_sock (struct io_uring_cmd *cmd, +unsigned int issue_flags) +{ +return -EOPNOTSUPP ; +} +#endif + +#endif
io_uring/uring_cmd.c +1 -0
@@ -3,6 +3,7 @@#include <linux/errno.h> #include <linux/file.h> #include <linux/io_uring/cmd.h> +#include <linux/io_uring/net.h> #include <linux/security.h> #include <linux/nospec.h> #include <net/sock.h>
net/socket.c +1 -1
@@ -88,7 +88,7 @@#include <linux/xattr.h> #include <linux/nospec.h> #include <linux/indirect_call_wrapper.h> -#include <linux/io_uring.h> +#include <linux/io_uring/net.h> #include <linux/uaccess.h> #include <asm/unistd.h>
Signature · 1 line
2.43.0
RFC PATCH v4 5/16 io_uring: introduce interface queue
In reply to David WeiFrom: David Wei <[email protected]>
This patch introduces a new object in io_uring called an interface queue (ifq) which contains:
* A pool region allocated by userspace and registered w/ io_uring where Rx data is transferred to via DMA.
* A net device and one specific Rx queue in it that will be configured for ZC Rx.
* A new shared ringbuf w/ userspace called a refill ring. When userspace is done with bufs with Rx packet payloads, it writes entries into this ring to tell the kernel that bufs can be re-used by the NIC again. Each entry in the refill ring is a struct io_uring_rbuf_rqe.
On the completion side, the main CQ ring is used to notify userspace of recv()'d packets. Big CQEs (32 bytes) are required to support this, as the upper 16 bytes are used by ZC Rx to store a feature specific struct io_uring_rbuf_cqe.
Add two new struct types:
1. io_uring_rbuf_rqe - entry in refill ring
2. io_uring_rbuf_cqe - entry in upper 16 bytes of a big CQE
For now, each io_uring instance has a single ifq, and each ifq has a single pool region associated with one Rx queue.
Add a new opcode and functions to setup and tear down an ifq. Size and offsets of the shared refill ring are returned to userspace for it to mmap in the registration struct io_uring_zc_rx_ifq_reg, similar to the main SQ/CQ rings.
--- include/linux/io_uring_types.h | 4 ++ include/uapi/linux/io_uring.h | 40 ++++++++++++ io_uring/Makefile | 3 +- io_uring/io_uring.c | 7 +++ io_uring/register.c | 7 +++ io_uring/zc_rx.c | 109 +++++++++++++++++++++++++++++++++ io_uring/zc_rx.h | 35 +++++++++++ 7 files changed, 204 insertions(+), 1 deletion(-) create mode 100644 io_uring/zc_rx.c create mode 100644 io_uring/zc_rx.h
include/linux/io_uring_types.h +4 -0
@@ -39,6 +39,8 @@ enum io_uring_cmd_flags { IO_URING_F_COMPAT = (1 << 12), }; +struct io_zc_rx_ifq ; +struct io_wq_work_node {struct io_wq_work_node *next; };
@@ -385,6 +387,8 @@ struct io_ring_ctx {struct io_rsrc_data *file_data;struct io_rsrc_data *buf_data; +struct io_zc_rx_ifq *ifq; +/* protected by ->uring_lock */ struct list_head rsrc_ref_list;struct io_alloc_cache rsrc_node_cache;
include/uapi/linux/io_uring.h +40 -0
@@ -575,6 +575,9 @@ enum {IORING_REGISTER_NAPI =27 ,IORING_UNREGISTER_NAPI =28 , +/* register a network interface queue for zerocopy */ +IORING_REGISTER_ZC_RX_IFQ =29 , +/* this goes last */ IORING_REGISTER_LAST,
@@ -782,6 +785,43 @@ enum { SOCKET_URING_OP_SETSOCKOPT, }; +struct io_uring_rbuf_rqe { +__u32 off; +__u32 len; +__u16 region; +__u8 __pad[6 ]; +}; + +struct io_uring_rbuf_cqe { +__u32 off; +__u32 len; +__u16 region; +__u8 __pad[6 ]; +}; + +struct io_rbuf_rqring_offsets { +__u32 head; +__u32 tail; +__u32 rqes; +__u8 __pad[4 ]; +}; + +/* +* Argument for IORING_REGISTER_ZC_RX_IFQ +*/ +struct io_uring_zc_rx_ifq_reg { +__u32 if_idx; +/* hw rx descriptor ring id */ +__u32 if_rxq_id; +__u32 region_id; +__u32 rq_entries; +__u32 flags; +__u16 cpu; + +__u32 mmap_sz; +struct io_rbuf_rqring_offsets rq_off; +}; + #ifdef __cplusplus }#endif
io_uring/Makefile +2 -1
@@ -8,7 +8,8 @@ obj-$(CONFIG_IO_URING) += io_uring.o xattr.o nop.o fs.o splice.o \ statx.o net.o msg_ring.o timeout.o \ sqpoll.o fdinfo.o tctx.o poll.o \ cancel.o kbuf.o rsrc.o rw.o opdef.o \ - notif.o waitid.o register.o truncate.o + notif.o waitid.o register.o truncate.o \ + zc_rx.o obj-$(CONFIG_IO_WQ) += io-wq.o obj-$(CONFIG_FUTEX) += futex.o obj-$(CONFIG_NET_RX_BUSY_POLL) += napi.o
io_uring/io_uring.c +7 -0
@@ -95,6 +95,7 @@#include "waitid.h" #include "futex.h" #include "napi.h" +#include "zc_rx.h" #include "timeout.h" #include "poll.h"
@@ -2861,6 +2862,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)return ;mutex_lock (&ctx->uring_lock); +io_unregister_zc_rx_ifqs (ctx);if (ctx->buf_data)__io_sqe_buffers_unregister (ctx);if (ctx->file_data)
@@ -3032,6 +3034,11 @@ static __cold void io_ring_exit_work(struct work_struct *work)io_cqring_overflow_kill (ctx);mutex_unlock (&ctx->uring_lock); } +if (ctx->ifq) { +mutex_lock (&ctx->uring_lock); +io_shutdown_zc_rx_ifqs (ctx); +mutex_unlock (&ctx->uring_lock); + }if (ctx->flags &IORING_SETUP_DEFER_TASKRUN )io_move_task_work_from_local (ctx);
io_uring/register.c +7 -0
@@ -27,6 +27,7 @@#include "cancel.h" #include "kbuf.h" #include "napi.h" +#include "zc_rx.h" #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ IORING_REGISTER_LAST + IORING_OP_LAST)
@@ -563,6 +564,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,break ; ret =io_unregister_napi (ctx, arg);break ; +case IORING_REGISTER_ZC_RX_IFQ : + ret = -EINVAL ; +if (!arg || nr_args !=1 ) +break ; + ret =io_register_zc_rx_ifq (ctx, arg); +break ;default : ret = -EINVAL ;break ;
io_uring/zc_rx.c new file +109 -0
109 lines not on this page · raw message
io_uring/zc_rx.h new file +35 -0
@@ -0,0 +1,35 @@ +// SPDX-License-Identifier: GPL-2.0 +#ifndef IOU_ZC_RX_H +#define IOU_ZC_RX_H + +struct io_zc_rx_ifq { +struct io_ring_ctx *ctx; +struct net_device *dev; +struct io_uring *rq_ring; +struct io_uring_rbuf_rqe *rqes; +u32 rq_entries; + +/* hw rx descriptor ring id */ +u32 if_rxq_id; +}; + +#if defined(CONFIG_PAGE_POOL ) +int io_register_zc_rx_ifq (struct io_ring_ctx *ctx, +struct io_uring_zc_rx_ifq_reg __user *arg); +void io_unregister_zc_rx_ifqs (struct io_ring_ctx *ctx); +void io_shutdown_zc_rx_ifqs (struct io_ring_ctx *ctx); +#else +static inline int io_register_zc_rx_ifq (struct io_ring_ctx *ctx, +struct io_uring_zc_rx_ifq_reg __user *arg) +{ +return -EOPNOTSUPP ; +} +static inline void io_unregister_zc_rx_ifqs (struct io_ring_ctx *ctx) +{ +} +static inline void io_shutdown_zc_rx_ifqs (struct io_ring_ctx *ctx) +{ +} +#endif + +#endif
Signature · 1 line
2.43.0
RFC PATCH v4 6/16 io_uring: add mmap support for shared ifq ringbuffers
In reply to David WeiFrom: David Wei <[email protected]>
This patch adds mmap support for ifq refill ring. Just like the io_uring SQ/CQ rings, userspace issues a single mmap call using the io_uring fd w/ magic offset IORING_OFF_RQ_RING. An opaque ptr is returned to userspace, which is then expected to use the offsets returned in the registration struct to get access to the head/tail and rings.
--- include/uapi/linux/io_uring.h | 2 ++ io_uring/io_uring.c | 5 +++++ io_uring/zc_rx.c | 15 ++++++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-)
include/uapi/linux/io_uring.h +2 -0
@@ -438,6 +438,8 @@ enum {#define IORING_OFF_PBUF_RING 0x80000000ULL#define IORING_OFF_PBUF_SHIFT 16#define IORING_OFF_MMAP_MASK 0xf8000000ULL +#define IORING_OFF_RQ_RING 0x20000000ULL +#define IORING_OFF_RQ_SHIFT 16/* * Filled with the offset for mmap(2)
io_uring/io_uring.c +5 -0
@@ -3434,6 +3434,11 @@ static void *io_uring_validate_mmap_request(struct file *file,return ERR_PTR (-EINVAL );break ; } +case IORING_OFF_RQ_RING : +if (!ctx->ifq) +return ERR_PTR (-EINVAL ); + ptr = ctx->ifq->rq_ring; +break ;default :return ERR_PTR (-EINVAL ); }
io_uring/zc_rx.c +14 -1
@@ -58,6 +58,7 @@ int io_register_zc_rx_ifq(struct io_ring_ctx *ctx, {struct io_uring_zc_rx_ifq_reg reg;struct io_zc_rx_ifq *ifq; +size_t ring_sz, rqes_sz;int ret; if (!(ctx->flags & IORING_SETUP_DEFER_TASKRUN &&
@@ -80,8 +81,20 @@ int io_register_zc_rx_ifq(struct io_ring_ctx *ctx, ifq->rq_entries = reg.rq_entries; ifq->if_rxq_id = reg.if_rxq_id; - ctx->ifq = ifq; + ring_sz =sizeof (struct io_uring ); + rqes_sz =sizeof (struct io_uring_rbuf_rqe ) * ifq->rq_entries; + reg.mmap_sz = ring_sz + rqes_sz; + reg.rq_off.rqes = ring_sz; + reg.rq_off.head = offsetof(struct io_uring , head); + reg.rq_off.tail = offsetof(struct io_uring , tail); + +if (copy_to_user (arg, ®,sizeof (reg))) { + ret = -EFAULT ; + goto err; + } + + ctx->ifq = ifq;return 0 ; err:io_zc_rx_ifq_free (ifq);
Signature · 1 line
2.43.0
RFC PATCH v4 7/16 netdev: add XDP_SETUP_ZC_RX command
In reply to David WeiFrom: David Wei <[email protected]>
RFC only, not for upstream.
This will be replaced with a separate ndo callback or some other mechanism in next patchset revisions.
This patch adds a new XDP_SETUP_ZC_RX command that will be used in a later patch to enable or disable ZC RX for a specific RX queue.
We are open to suggestions on a better way of doing this. Google's TCP devmem proposal sets up struct netdev_rx_queue which persists across device reset, then expects userspace to use an out-of-band method (e.g. ethtool) to reset the device, thus re-filling a hardware Rx queue.
--- include/linux/netdevice.h | 6 ++++++ 1 file changed, 6 insertions(+)
include/linux/netdevice.h +6 -0
@@ -1009,6 +1009,7 @@ enum bpf_netdev_command {BPF_OFFLOAD_MAP_ALLOC ,BPF_OFFLOAD_MAP_FREE ,XDP_SETUP_XSK_POOL , + XDP_SETUP_ZC_RX, };struct bpf_prog_offload_ops ;
@@ -1047,6 +1048,11 @@ struct netdev_bpf {struct xsk_buff_pool *pool;u16 queue_id; } xsk; +/* XDP_SETUP_ZC_RX */ +struct { +struct io_zc_rx_ifq *ifq; +u16 queue_id; + } zc_rx; }; };
Signature · 1 line
2.43.0
RFC PATCH v4 8/16 io_uring: setup ZC for an Rx queue when registering an ifq
In reply to David WeiFrom: David Wei <[email protected]>
RFC only, not for upstream
Just as with the previous patch, it will be migrated from ndo_bpf
This patch sets up ZC for an Rx queue in a net device when an ifq is registered with io_uring. The Rx queue is specified in the registration struct.
For now since there is only one ifq, its destruction is implicit during io_uring cleanup.
--- io_uring/zc_rx.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+)
io_uring/zc_rx.c +43 -0
@@ -4,6 +4,7 @@#include <linux/errno.h> #include <linux/mm.h> #include <linux/io_uring.h> +#include <linux/netdevice.h> #include <uapi/linux/io_uring.h>
@@ -11,6 +12,34 @@#include "kbuf.h" #include "zc_rx.h" +typedef int (*bpf_op_t )(struct net_device *dev,struct netdev_bpf *bpf); + +static int __io_queue_mgmt (struct net_device *dev,struct io_zc_rx_ifq *ifq, +u16 queue_id) +{ +struct netdev_bpf cmd; +bpf_op_t ndo_bpf; + + ndo_bpf = dev->netdev_ops->ndo_bpf; +if (!ndo_bpf) +return -EINVAL ; + + cmd.command =XDP_SETUP_ZC_RX ; + cmd.zc_rx.ifq = ifq; + cmd.zc_rx.queue_id = queue_id; +return ndo_bpf (dev, &cmd); +} + +static int io_open_zc_rxq (struct io_zc_rx_ifq *ifq) +{ +return __io_queue_mgmt (ifq->dev, ifq, ifq->if_rxq_id); +} + +static int io_close_zc_rxq (struct io_zc_rx_ifq *ifq) +{ +return __io_queue_mgmt (ifq->dev,NULL , ifq->if_rxq_id); +} +static int io_allocate_rbuf_ring (struct io_zc_rx_ifq *ifq,struct io_uring_zc_rx_ifq_reg *reg) {
@@ -49,6 +78,10 @@ static struct io_zc_rx_ifq *io_zc_rx_ifq_alloc(struct io_ring_ctx *ctx)static void io_zc_rx_ifq_free (struct io_zc_rx_ifq *ifq) { +if (ifq->if_rxq_id !=-1 ) +io_close_zc_rxq (ifq); +if (ifq->dev) +dev_put (ifq->dev);io_free_rbuf_ring (ifq);kfree (ifq); }
@@ -79,9 +112,18 @@ int io_register_zc_rx_ifq(struct io_ring_ctx *ctx,if (ret) goto err; + ret = -ENODEV ; + ifq->dev =dev_get_by_index (current->nsproxy->net_ns, reg.if_idx); +if (!ifq->dev) + goto err; + ifq->rq_entries = reg.rq_entries; ifq->if_rxq_id = reg.if_rxq_id; + ret =io_open_zc_rxq (ifq); +if (ret) + goto err; + ring_sz =sizeof (struct io_uring ); rqes_sz =sizeof (struct io_uring_rbuf_rqe ) * ifq->rq_entries; reg.mmap_sz = ring_sz + rqes_sz;
@@ -90,6 +132,7 @@ int io_register_zc_rx_ifq(struct io_ring_ctx *ctx, reg.rq_off.tail = offsetof(struct io_uring , tail);if (copy_to_user (arg, ®,sizeof (reg))) { +io_close_zc_rxq (ifq); ret = -EFAULT ; goto err; }
Signature · 1 line
2.43.0
RFC PATCH v4 9/16 io_uring/zcrx: implement socket registration
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
We want userspace to explicitly list all sockets it'll be using with a particular zc ifq, so we can properly configure them, e.g. binding the sockets to the corresponding interface and setting steering rules. We'll also need it to better control ifq lifetime and for termination / unregistration purposes.
TODO: remove zc_rx_idx from struct socket, which will fix zc_rx_idx token init races and re-registration bug.
--- include/linux/net.h | 2 + include/uapi/linux/io_uring.h | 7 +++ io_uring/net.c | 20 ++++++++ io_uring/register.c | 6 +++ io_uring/zc_rx.c | 91 +++++++++++++++++++++++++++++++++-- io_uring/zc_rx.h | 17 +++++++ net/socket.c | 1 + 7 files changed, 141 insertions(+), 3 deletions(-)
include/linux/net.h +2 -0
@@ -126,6 +126,8 @@ struct socket {const struct proto_ops *ops;/* Might change with IPV6_ADDRFORM or MPTCP. */ struct socket_wq wq; + +unsigned zc_rx_idx; };/*
include/uapi/linux/io_uring.h +7 -0
@@ -579,6 +579,7 @@ enum {/* register a network interface queue for zerocopy */ IORING_REGISTER_ZC_RX_IFQ =29 , +IORING_REGISTER_ZC_RX_SOCK =30 ,/* this goes last */ IORING_REGISTER_LAST,
@@ -824,6 +825,12 @@ struct io_uring_zc_rx_ifq_reg {struct io_rbuf_rqring_offsets rq_off; }; +struct io_uring_zc_rx_sock_reg { +__u32 sockfd; +__u32 zc_rx_ifq_idx; +__u32 __resv[2 ]; +}; + #ifdef __cplusplus }#endif
io_uring/net.c +20 -0
@@ -16,6 +16,7 @@#include "net.h" #include "notif.h" #include "rsrc.h" +#include "zc_rx.h" #if defined(CONFIG_NET ) struct io_shutdown {
@@ -1033,6 +1034,25 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags)return ret; } +static __maybe_unused +struct io_zc_rx_ifq *io_zc_verify_sock (struct io_kiocb *req, +struct socket *sock) +{ +unsigned token =READ_ONCE (sock->zc_rx_idx); +unsigned ifq_idx = token >>IO_ZC_IFQ_IDX_OFFSET ; +unsigned sock_idx = token &IO_ZC_IFQ_IDX_MASK ; +struct io_zc_rx_ifq *ifq; + +if (ifq_idx) +return NULL ; + ifq = req->ctx->ifq; +if (!ifq || sock_idx >= ifq->nr_sockets) +return NULL ; +if (ifq->sockets[sock_idx] != req->file) +return NULL ; +return ifq; +} +void io_send_zc_cleanup (struct io_kiocb *req) { struct io_sr_msg *zc = io_kiocb_to_cmd(req, struct io_sr_msg);
io_uring/register.c +6 -0
@@ -570,6 +570,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,break ; ret =io_register_zc_rx_ifq (ctx, arg);break ; +case IORING_REGISTER_ZC_RX_SOCK : + ret = -EINVAL ; +if (!arg || nr_args !=1 ) +break ; + ret =io_register_zc_rx_sock (ctx, arg); +break ;default : ret = -EINVAL ;break ;
io_uring/zc_rx.c +88 -3
129 lines not on this page · raw message
io_uring/zc_rx.h +17 -0
@@ -2,6 +2,13 @@ #ifndef IOU_ZC_RX_H #define IOU_ZC_RX_H +#include <linux/io_uring_types.h> +#include <linux/skbuff.h> + +#define IO_ZC_MAX_IFQ_SOCKETS 16 +#define IO_ZC_IFQ_IDX_OFFSET 16 +#define IO_ZC_IFQ_IDX_MASK ((1U << IO_ZC_IFQ_IDX_OFFSET) - 1) + struct io_zc_rx_ifq { struct io_ring_ctx *ctx; struct net_device *dev;
@@ -11,6 +18,9 @@ struct io_zc_rx_ifq {/* hw rx descriptor ring id */ u32 if_rxq_id; + +unsigned nr_sockets; +struct file *sockets[IO_ZC_MAX_IFQ_SOCKETS ]; };#if defined(CONFIG_PAGE_POOL )
@@ -18,6 +28,8 @@ int io_register_zc_rx_ifq(struct io_ring_ctx *ctx, struct io_uring_zc_rx_ifq_reg __user *arg);void io_unregister_zc_rx_ifqs (struct io_ring_ctx *ctx);void io_shutdown_zc_rx_ifqs (struct io_ring_ctx *ctx); +int io_register_zc_rx_sock (struct io_ring_ctx *ctx, +struct io_uring_zc_rx_sock_reg __user *arg);#else static inline int io_register_zc_rx_ifq (struct io_ring_ctx *ctx,struct io_uring_zc_rx_ifq_reg __user *arg)
@@ -30,6 +42,11 @@ static inline void io_unregister_zc_rx_ifqs(struct io_ring_ctx *ctx)static inline void io_shutdown_zc_rx_ifqs (struct io_ring_ctx *ctx) { } +static inline int io_register_zc_rx_sock (struct io_ring_ctx *ctx, +struct io_uring_zc_rx_sock_reg __user *arg) +{ +return -EOPNOTSUPP ; +}#endif #endif
net/socket.c +1 -0
@@ -637,6 +637,7 @@ struct socket *sock_alloc(void) sock =SOCKET_I (inode); + sock->zc_rx_idx =0 ; inode->i_ino =get_next_ino (); inode->i_mode =S_IFSOCK |S_IRWXUGO ; inode->i_uid =current_fsuid ();
Signature · 1 line
2.43.0
RFC PATCH v4 10/16 io_uring: add zero copy buf representation and pool
In reply to David WeiFrom: David Wei <[email protected]>
This patch adds two objects:
* Zero copy buffer representation, holding a page and a net_iov. The page is needed as net_iov is designed for opaque device memory, whereas we are backed by real pages.
* Zero copy pool, spiritually similar to page pool, that holds ZC bufs and hands them out to net devices. This will be used as an implementation of page pool memory provider.
Pool regions are registered w/ io_uring using the registered buffer API, with a 1:1 mapping between region and nr_iovec in io_uring_register_buffers. This does the heavy lifting of pinning and chunking into bvecs into a struct io_mapped_ubuf for us.
For now as there is only one pool region per ifq, there is no separate API for adding/removing regions yet and it is mapped implicitly during ifq registration.
--- include/linux/io_uring/net.h | 7 +++ io_uring/zc_rx.c | 110 +++++++++++++++++++++++++++++++++++ io_uring/zc_rx.h | 15 +++++ 3 files changed, 132 insertions(+)
include/linux/io_uring/net.h +7 -0
@@ -2,8 +2,15 @@#ifndef _LINUX_IO_URING_NET_H#define _LINUX_IO_URING_NET_H +#include <net/page_pool/types.h> +struct io_uring_cmd ; +struct io_zc_rx_buf { +struct net_iov niov; +struct page *page; +}; +#if defined(CONFIG_IO_URING )int io_uring_cmd_sock (struct io_uring_cmd *cmd,unsigned int issue_flags);
io_uring/zc_rx.c +110 -0
134 lines not on this page · raw message
io_uring/zc_rx.h +15 -0
@@ -3,15 +3,30 @@#define IOU_ZC_RX_H #include <linux/io_uring_types.h> +#include <linux/io_uring/net.h> #include <linux/skbuff.h> #define IO_ZC_MAX_IFQ_SOCKETS 16#define IO_ZC_IFQ_IDX_OFFSET 16#define IO_ZC_IFQ_IDX_MASK ((1U << IO_ZC_IFQ_IDX_OFFSET) - 1) +struct io_zc_rx_pool { +struct io_zc_rx_ifq *ifq; +struct io_zc_rx_buf *bufs; +u32 nr_bufs; +u16 pool_id; + +/* freelist */ +spinlock_t freelist_lock; +u32 free_count; +u32 freelist[]; +}; +struct io_zc_rx_ifq {struct io_ring_ctx *ctx;struct net_device *dev; +struct io_zc_rx_pool *pool; +struct io_uring *rq_ring;struct io_uring_rbuf_rqe *rqes;u32 rq_entries;
Signature · 1 line
2.43.0
RFC PATCH v4 11/16 io_uring: implement pp memory provider for zc rx
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
Implement a new pp memory provider for io_uring zerocopy receive.
All buffers are backed by struct io_zc_rx_buf, which is a thin extension of struct net_iov. Initially, all of them are unallocated and placed in a spinlock protected ->freelist. Then, they will be allocate via the ->alloc_pages callback, which sets refcount to 1.
Later, buffers would either be dropped by the net stack and recycled back into page pool / released by ->release_page, or, more likely, get transferred to the userspace by posting a corresponding CQE and elevating refcount by IO_ZC_RX_UREF. When the user is done with a buffer, it should be put into the refill ring.
Next time io_pp_zc_alloc_pages() runs it'll check the ring, put user refs and ultimately grab buffers from there. That's done in the attached napi context and so doesn't need any additional synchronisation. That is the second hottest path after getting a buffer from the pp lockless cache.
--- include/linux/io_uring/net.h | 5 + include/net/page_pool/types.h | 1 + io_uring/zc_rx.c | 202 ++++++++++++++++++++++++++++++++++ io_uring/zc_rx.h | 5 + net/core/page_pool.c | 2 +- 5 files changed, 214 insertions(+), 1 deletion(-)
include/linux/io_uring/net.h +5 -0
@@ -12,6 +12,11 @@ struct io_zc_rx_buf { };#if defined(CONFIG_IO_URING ) + +#if defined(CONFIG_PAGE_POOL ) +extern const struct memory_provider_ops io_uring_pp_zc_ops; +#endif +int io_uring_cmd_sock (struct io_uring_cmd *cmd,unsigned int issue_flags);#else
include/net/page_pool/types.h +1 -0
@@ -227,6 +227,7 @@ netmem_ref page_pool_alloc_frag_netmem(struct page_pool *pool,struct page_pool *page_pool_create (const struct page_pool_params *params);struct page_pool *page_pool_create_percpu (const struct page_pool_params *params,int cpuid); +void page_pool_set_pp_info (struct page_pool *pool,netmem_ref netmem);struct xdp_mem_info ;
io_uring/zc_rx.c +202 -0
212 lines not on this page · raw message
io_uring/zc_rx.h +5 -0
@@ -10,6 +10,9 @@#define IO_ZC_IFQ_IDX_OFFSET 16#define IO_ZC_IFQ_IDX_MASK ((1U << IO_ZC_IFQ_IDX_OFFSET) - 1) +#define IO_ZC_RX_UREF 0x10000 +#define IO_ZC_RX_KREF_MASK (IO_ZC_RX_UREF - 1) +struct io_zc_rx_pool {struct io_zc_rx_ifq *ifq;struct io_zc_rx_buf *bufs;
@@ -26,10 +29,12 @@ struct io_zc_rx_ifq {struct io_ring_ctx *ctx;struct net_device *dev;struct io_zc_rx_pool *pool; +struct page_pool *pp;struct io_uring *rq_ring;struct io_uring_rbuf_rqe *rqes;u32 rq_entries; +u32 cached_rq_head;/* hw rx descriptor ring id */ u32 if_rxq_id;
net/core/page_pool.c +1 -1
@@ -460,7 +460,7 @@ static bool page_pool_dma_map(struct page_pool *pool, netmem_ref netmem)return false; } -static void page_pool_set_pp_info (struct page_pool *pool,netmem_ref netmem) +void page_pool_set_pp_info (struct page_pool *pool,netmem_ref netmem) {netmem_set_pp (netmem, pool);netmem_or_pp_magic (netmem,PP_SIGNATURE );
Signature · 1 line
2.43.0
RFC PATCH v4 12/16 io_uring/zcrx: implement PP_FLAG_DMA_* handling
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
The patch implements support for PP_FLAG_DMA_MAP and PP_FLAG_DMA_SYNC_DEV. Dma map buffers when creating a page pool if needed, and unmap on tear down. Most of synching is done by page pool apart from when we're grabbing buffers from the refill ring, in which case it we need to do it by hand.
--- io_uring/zc_rx.c | 90 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 4 deletions(-)
io_uring/zc_rx.c +86 -4
@@ -9,6 +9,7 @@#include <net/tcp.h> #include <net/af_unix.h> #include <trace/events/page_pool.h> +#include <net/page_pool/helpers.h> #include <uapi/linux/io_uring.h>
@@ -72,6 +73,7 @@ static int io_zc_rx_init_buf(struct page *page, struct io_zc_rx_buf *buf) {memset (&buf->niov,0 ,sizeof (buf->niov));atomic_long_set (&buf->niov.pp_ref_count,0 ); +page_pool_set_dma_addr_netmem (net_iov_to_netmem (&buf->niov),0 ); buf->page = page;get_page (page);
@@ -392,12 +394,25 @@ static inline netmem_ref io_zc_buf_to_netmem(struct io_zc_rx_buf *buf)return net_iov_to_netmem (&buf->niov); } +static inline void io_zc_sync_for_device (struct page_pool *pp, +netmem_ref netmem) +{ +if (pp->p.flags &PP_FLAG_DMA_SYNC_DEV ) { +dma_addr_t dma_addr =page_pool_get_dma_addr_netmem (netmem); + +dma_sync_single_range_for_device (pp->p.dev, dma_addr, + pp->p.offset, pp->p.max_len, + pp->p.dma_dir); + } +} +static inline void io_zc_add_pp_cache (struct page_pool *pp,struct io_zc_rx_buf *buf) {netmem_ref netmem =io_zc_buf_to_netmem (buf);page_pool_set_pp_info (pp, netmem); +io_zc_sync_for_device (pp, netmem); pp->alloc.cache[pp->alloc.count++] = netmem; }
@@ -517,9 +532,71 @@ static void io_pp_zc_scrub(struct page_pool *pp) } } +#define IO_PP_DMA_ATTRS (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING) + +static void io_pp_unmap_buf (struct io_zc_rx_buf *buf,struct page_pool *pp) +{ +netmem_ref netmem =net_iov_to_netmem (&buf->niov); +dma_addr_t dma =page_pool_get_dma_addr_netmem (netmem); + +dma_unmap_page_attrs (pp->p.dev, dma,PAGE_SIZE << pp->p.order, + pp->p.dma_dir,IO_PP_DMA_ATTRS ); +page_pool_set_dma_addr_netmem (netmem,0 ); +} + +static int io_pp_map_buf (struct io_zc_rx_buf *buf,struct page_pool *pp) +{ +netmem_ref netmem =net_iov_to_netmem (&buf->niov); +dma_addr_t dma_addr; +int ret; + + dma_addr =dma_map_page_attrs (pp->p.dev, buf->page,0 , +PAGE_SIZE << pp->p.order, pp->p.dma_dir, +IO_PP_DMA_ATTRS ); + ret =dma_mapping_error (pp->p.dev, dma_addr); +if (ret) +return ret; + +if (WARN_ON_ONCE (page_pool_set_dma_addr_netmem (netmem, dma_addr))) { +dma_unmap_page_attrs (pp->p.dev, dma_addr, +PAGE_SIZE << pp->p.order, pp->p.dma_dir, +IO_PP_DMA_ATTRS ); +return -EFAULT ; + } + +io_zc_sync_for_device (pp, netmem); +return 0 ; +} + +static int io_pp_map_pool (struct io_zc_rx_pool *pool,struct page_pool *pp) +{ +int i, ret =0 ; + +for (i =0 ; i < pool->nr_bufs; i++) { + ret =io_pp_map_buf (&pool->bufs[i], pp); +if (ret) +break ; + } + +if (ret) { +while (i--) +io_pp_unmap_buf (&pool->bufs[i], pp); + } +return ret; +} + +static void io_pp_unmap_pool (struct io_zc_rx_pool *pool,struct page_pool *pp) +{ +int i; + +for (i =0 ; i < pool->nr_bufs; i++) +io_pp_unmap_buf (&pool->bufs[i], pp); +} +static int io_pp_zc_init (struct page_pool *pp) {struct io_zc_rx_ifq *ifq = pp->mp_priv; +int ret;if (!ifq)return -EINVAL ;
@@ -527,10 +604,12 @@ static int io_pp_zc_init(struct page_pool *pp)return -EINVAL ;if (!pp->p.napi)return -EINVAL ; -if (pp->p.flags &PP_FLAG_DMA_MAP ) -return -EOPNOTSUPP ; -if (pp->p.flags &PP_FLAG_DMA_SYNC_DEV ) -return -EOPNOTSUPP ; + +if (pp->p.flags &PP_FLAG_DMA_MAP ) { + ret =io_pp_map_pool (ifq->pool, pp); +if (ret) +return ret; + }percpu_ref_get (&ifq->ctx->refs); ifq->pp = pp;
@@ -542,6 +621,9 @@ static void io_pp_zc_destroy(struct page_pool *pp)struct io_zc_rx_ifq *ifq = pp->mp_priv;struct io_zc_rx_pool *pool = ifq->pool; +if (pp->p.flags &PP_FLAG_DMA_MAP ) +io_pp_unmap_pool (ifq->pool, pp); + ifq->pp =NULL ;if (WARN_ON_ONCE (pool->free_count != pool->nr_bufs))
Signature · 1 line
2.43.0
RFC PATCH v4 13/16 io_uring: add io_recvzc request
In reply to David WeiAdd an io_uring opcode OP_RECV_ZC for doing ZC reads from a socket that is set up for ZC Rx. The request reads skbs from a socket. Completions are posted into the main CQ for each page frag read.
Big CQEs (CQE32) is required as the OP_RECV_ZC specific metadata (ZC region, offset, len) are stored in the extended 16 bytes as a struct io_uring_rbuf_cqe.
For now there is no limit as to how much work each OP_RECV_ZC request does. It will attempt to drain a socket of all available data.
Multishot requests are also supported. The first time an io_recvzc request completes, EAGAIN is returned which arms an async poll. Then, in subsequent runs in task work, IOU_ISSUE_SKIP_COMPLETE is returned to continue async polling.
--- include/uapi/linux/io_uring.h | 1 + io_uring/io_uring.h | 10 ++ io_uring/net.c | 94 +++++++++++++++++- io_uring/opdef.c | 16 +++ io_uring/zc_rx.c | 177 +++++++++++++++++++++++++++++++++- io_uring/zc_rx.h | 11 +++ 6 files changed, 302 insertions(+), 7 deletions(-)
include/uapi/linux/io_uring.h +1 -0
@@ -256,6 +256,7 @@ enum io_uring_op {IORING_OP_FUTEX_WAITV ,IORING_OP_FIXED_FD_INSTALL ,IORING_OP_FTRUNCATE , +IORING_OP_RECV_ZC ,/* this goes last, obviously */ IORING_OP_LAST,
io_uring/io_uring.h +10 -0
@@ -180,6 +180,16 @@ static inline bool io_get_cqe(struct io_ring_ctx *ctx, struct io_uring_cqe **retreturn io_get_cqe_overflow (ctx, ret, false); } +static inline bool io_defer_get_uncommited_cqe (struct io_ring_ctx *ctx, +struct io_uring_cqe **cqe_ret) +{ +io_lockdep_assert_cq_locked (ctx); + + ctx->cq_extra++; + ctx->submit_state.flush_cqes = true; +return io_get_cqe (ctx, cqe_ret); +} + static __always_inline bool io_fill_cqe_req(struct io_ring_ctx *ctx,struct io_kiocb *req) {
io_uring/net.c +89 -5
124 lines not on this page · raw message
io_uring/opdef.c +16 -0
@@ -36,6 +36,7 @@#include "waitid.h" #include "futex.h" #include "truncate.h" +#include "zc_rx.h" static int io_no_issue (struct io_kiocb *req,unsigned int issue_flags) {
@@ -481,6 +482,18 @@ const struct io_issue_def io_issue_defs[] = { .prep = io_ftruncate_prep, .issue = io_ftruncate, }, + [IORING_OP_RECV_ZC] = { + .needs_file = 1, + .unbound_nonreg_file = 1, + .pollin = 1, + .ioprio = 1, +#if defined(CONFIG_NET ) + .prep = io_recvzc_prep, + .issue = io_recvzc, +#else + .prep = io_eopnotsupp_prep, +#endif + }, };const struct io_cold_def io_cold_defs[] = {
@@ -722,6 +735,9 @@ const struct io_cold_def io_cold_defs[] = { [IORING_OP_FTRUNCATE ] = { .name ="FTRUNCATE" , }, + [IORING_OP_RECV_ZC ] = { + .name ="RECV_ZC" , + }, };const char *io_uring_get_opcode (u8 opcode)
io_uring/zc_rx.c +175 -2
203 lines not on this page · raw message
io_uring/zc_rx.h +11 -0
@@ -50,6 +50,8 @@ void io_unregister_zc_rx_ifqs(struct io_ring_ctx *ctx);void io_shutdown_zc_rx_ifqs (struct io_ring_ctx *ctx);int io_register_zc_rx_sock (struct io_ring_ctx *ctx,struct io_uring_zc_rx_sock_reg __user *arg); +int io_zc_rx_recv (struct io_kiocb *req,struct io_zc_rx_ifq *ifq, +struct socket *sock,unsigned int flags);#else static inline int io_register_zc_rx_ifq (struct io_ring_ctx *ctx,struct io_uring_zc_rx_ifq_reg __user *arg)
@@ -67,6 +69,15 @@ static inline int io_register_zc_rx_sock(struct io_ring_ctx *ctx, {return -EOPNOTSUPP ; } + +static inline int io_zc_rx_recv (struct io_kiocb *req,struct io_zc_rx_ifq *ifq, +struct socket *sock,unsigned int flags) +{ +return -EOPNOTSUPP ; +}#endif +int io_recvzc (struct io_kiocb *req,unsigned int issue_flags); +int io_recvzc_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe); +#endif
Signature · 1 line
2.43.0
Jens Axboe
maintainerRe: [RFC PATCH v4 13/16] io_uring: add io_recvzc request
Quoted 8 lines · David Wei
Add an
io_uringopcodeOP_RECV_ZCfor doing ZC reads from a socket that is set up for ZC Rx. The request reads skbs from a socket. Completions are posted into the main CQ for each page frag read.Big CQEs (CQE32) is required as the
OP_RECV_ZCspecific metadata (ZC region, offset, len) are stored in the extended 16 bytes as a structio_uring_rbuf_cqe.
Quoted 7 lines
For now there is no limit as to how much work each
OP_RECV_ZCrequest does. It will attempt to drain a socket of all available data.Multishot requests are also supported. The first time an
io_recvzcrequest completes, EAGAIN is returned which arms an async poll. Then, in subsequent runs in task work,IOU_ISSUE_SKIP_COMPLETEis returned to continue async polling.
I'd probably drop that last paragraph, this is how all multishot requests work and is implementation detail that need not go in the commit message. Probably suffices just to say it supports multishot.
Quoted 10 lines
io_uring/net.c +1
-1@@ -695,7 +701,7 @@ static inline bool io_recv_finish(struct io_kiocb *req, int *ret,unsigned int cflags; cflags =io_put_kbuf (req, issue_flags); -if (msg->msg_inq && msg->msg_inq !=-1 ) +if (msg && msg->msg_inq && msg->msg_inq !=-1 ) cflags |=IORING_CQE_F_SOCK_NONEMPTY ;if (!(req->flags &REQ_F_APOLL_MULTISHOT )) {
io_uring/net.c static inline bool io_recv_finish(struct io_kiocb *req, int *ret, · line 735
Quoted 8 lines
io_uring/net.c +1
-1@@ -723,7 +729,7 @@ static inline bool io_recv_finish(struct io_kiocb *req, int *ret, goto enobufs;/* Known not-empty or unknown state, retry */ -if (cflags &IORING_CQE_F_SOCK_NONEMPTY || msg->msg_inq ==-1 ) { +if (cflags &IORING_CQE_F_SOCK_NONEMPTY || (msg && msg->msg_inq ==-1 )) {if (sr->nr_multishot_loops++ <MULTISHOT_MAX_RETRY )return false;/* mshot retries exceeded, force a requeue */
Maybe refactor this a bit so that you don't need to add these NULL checks? That seems pretty fragile, hard to read, and should be doable without extra checks.
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1067
Quoted 11 lines
io_uring/net.c +7
-0@@ -1053,6 +1058,85 @@ struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req,return ifq; } +int io_recvzc_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe) +{ + struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + +/* non-iopoll defer_taskrun only */ +if (!req->ctx->task_complete) +return -EINVAL ;
What's the reasoning behind this?
Quoted 5 lines
io_uring/net.c +5
-0+ struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); +struct io_zc_rx_ifq *ifq; +struct socket *sock; +int ret; +
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1105
Quoted 10 lines
io_uring/net.c +10
-0+/* +* We're posting CQEs deeper in the stack, and to avoid taking CQ locks +* we serialise by having only the master thread modifying the CQ with +* DEFER_TASkRUN checked earlier and forbidding executing it from io-wq. +* That's similar to io_check_multishot() for multishot CQEs. +*/ +if (issue_flags &IO_URING_F_IOWQ ) +return -EAGAIN ; +if (WARN_ON_ONCE (!(issue_flags &IO_URING_F_NONBLOCK ))) +return -EAGAIN ;
If rebased on the current tree, does this go away?
Signature · 1 line
Jens Axboe
Re: [RFC PATCH v4 13/16] io_uring: add io_recvzc request
Quoted 33 lines · Jens Axboe
Quoted 8 lines · David Wei
Add an
io_uringopcodeOP_RECV_ZCfor doing ZC reads from a socket that is set up for ZC Rx. The request reads skbs from a socket. Completions are posted into the main CQ for each page frag read.Big CQEs (CQE32) is required as the
OP_RECV_ZCspecific metadata (ZC region, offset, len) are stored in the extended 16 bytes as a structio_uring_rbuf_cqe.Quoted 7 lines
For now there is no limit as to how much work each
OP_RECV_ZCrequest does. It will attempt to drain a socket of all available data.Multishot requests are also supported. The first time an
io_recvzcrequest completes, EAGAIN is returned which arms an async poll. Then, in subsequent runs in task work,IOU_ISSUE_SKIP_COMPLETEis returned to continue async polling.I'd probably drop that last paragraph, this is how all multishot requests work and is implementation detail that need not go in the commit message. Probably suffices just to say it supports multishot.
Quoted 12 lines
io_uring/net.c +1
-1@@ -695,7 +701,7 @@ static inline bool io_recv_finish(struct io_kiocb *req, int *ret,unsigned int cflags; cflags =io_put_kbuf (req, issue_flags); -if (msg->msg_inq && msg->msg_inq !=-1 ) +if (msg && msg->msg_inq && msg->msg_inq !=-1 ) cflags |=IORING_CQE_F_SOCK_NONEMPTY ;if (!(req->flags &REQ_F_APOLL_MULTISHOT )) {@@ -723,7 +729,7 @@ static inline bool io_recv_finish(struct io_kiocb *req, int *ret, goto enobufs;
Quoted 10 lines
io_uring/net.c static inline bool io_recv_finish(struct io_kiocb *req, int *ret, · line 735
Quoted 6 lines
io_uring/net.c +1
-1/* Known not-empty or unknown state, retry */ -if (cflags &IORING_CQE_F_SOCK_NONEMPTY || msg->msg_inq ==-1 ) { +if (cflags &IORING_CQE_F_SOCK_NONEMPTY || (msg && msg->msg_inq ==-1 )) {if (sr->nr_multishot_loops++ <MULTISHOT_MAX_RETRY )return false;/* mshot retries exceeded, force a requeue */ Maybe refactor this a bit so that you don't need to add these NULL checks? That seems pretty fragile, hard to read, and should be doable without extra checks.
That chunk can be completely thrown away, we're not using io_recv_finish() here anymore
Quoted 4 lines
Quoted 4 lines
io_uring/net.c +0
-0@@ -1053,6 +1058,85 @@ struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req,return ifq; }
Quoted 9 lines
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1067
Quoted 7 lines
io_uring/net.c +7
-0+int io_recvzc_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe) +{ + struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + +/* non-iopoll defer_taskrun only */ +if (!req->ctx->task_complete) +return -EINVAL ;What's the reasoning behind this?
CQ locking, see the comment a couple lines below
Quoted 11 lines
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1101
Quoted 11 lines
io_uring/net.c +11
-0+ struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); +struct io_zc_rx_ifq *ifq; +struct socket *sock; +int ret; + +/* +* We're posting CQEs deeper in the stack, and to avoid taking CQ locks +* we serialise by having only the master thread modifying the CQ with +* DEFER_TASkRUN checked earlier and forbidding executing it from io-wq. +* That's similar to io_check_multishot() for multishot CQEs. +*/
This one ^^, though it doesn't read well, I should reword it for next time.
Quoted 6 lines
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1105
Quoted 4 lines
io_uring/net.c +4
-0+if (issue_flags &IO_URING_F_IOWQ ) +return -EAGAIN ; +if (WARN_ON_ONCE (!(issue_flags &IO_URING_F_NONBLOCK ))) +return -EAGAIN ;If rebased on the current tree, does this go away?
It's just a little behind not to have that change
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerRe: [RFC PATCH v4 13/16] io_uring: add io_recvzc request
Quoted 86 lines · Pavel Begunkov
Quoted 32 lines · Jens Axboe
Quoted 8 lines · David Wei
Add an
io_uringopcodeOP_RECV_ZCfor doing ZC reads from a socket that is set up for ZC Rx. The request reads skbs from a socket. Completions are posted into the main CQ for each page frag read.Big CQEs (CQE32) is required as the
OP_RECV_ZCspecific metadata (ZC region, offset, len) are stored in the extended 16 bytes as a structio_uring_rbuf_cqe.Quoted 7 lines
For now there is no limit as to how much work each
OP_RECV_ZCrequest does. It will attempt to drain a socket of all available data.Multishot requests are also supported. The first time an
io_recvzcrequest completes, EAGAIN is returned which arms an async poll. Then, in subsequent runs in task work,IOU_ISSUE_SKIP_COMPLETEis returned to continue async polling.I'd probably drop that last paragraph, this is how all multishot requests work and is implementation detail that need not go in the commit message. Probably suffices just to say it supports multishot.
Quoted 11 lines
io_uring/net.c +1
-2@@ -695,7 +701,7 @@ static inline bool io_recv_finish(struct io_kiocb *req, int *ret, unsigned int cflags; cflags = io_put_kbuf(req, issue_flags); - if (msg->msg_inq && msg->msg_inq != -1) + if (msg && msg->msg_inq && msg->msg_inq != -1) cflags |= IORING_CQE_F_SOCK_NONEMPTY; if (!(req->flags & REQ_F_APOLL_MULTISHOT)) {@@ -723,7 +729,7 @@ static inline bool io_recv_finish(struct io_kiocb *req, int *ret, goto enobufs; /* Known not-empty or unknown state, retry */ - if (cflags & IORING_CQE_F_SOCK_NONEMPTY || msg->msg_inq == -1) {Quoted 8 lines
io_uring/net.c static inline bool io_recv_finish(struct io_kiocb *req, int *ret, · line 735
Quoted 4 lines
io_uring/net.c +1
-0+ if (cflags & IORING_CQE_F_SOCK_NONEMPTY || (msg && msg->msg_inq == -1)) { if (sr->nr_multishot_loops++ < MULTISHOT_MAX_RETRY) return false; /* mshot retries exceeded, force a requeue */Maybe refactor this a bit so that you don't need to add these NULL checks? That seems pretty fragile, hard to read, and should be doable without extra checks.
That chunk can be completely thrown away, we're not using
io_recv_finish()here anymoreQuoted 4 lines
Quoted 4 lines
io_uring/net.c +0
-0@@ -1053,6 +1058,85 @@ struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, return ifq; } +int io_recvzc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)Quoted 8 lines
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1067
Quoted 6 lines
io_uring/net.c +6
-0+{ + struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + + /* non-iopoll defer_taskrun only */ + if (!req->ctx->task_complete) + return -EINVAL;What's the reasoning behind this?
CQ locking, see the comment a couple lines below
Quoted 11 lines
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1101
Quoted 11 lines
io_uring/net.c +11
-0+ struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + struct io_zc_rx_ifq *ifq; + struct socket *sock; + int ret; + + /* +* We're posting CQEs deeper in the stack, and to avoid taking CQ locks +* we serialise by having only the master thread modifying the CQ with +* DEFER_TASkRUN checked earlier and forbidding executing it from io-wq. +* That's similar to io_check_multishot() for multishot CQEs. +*/ This one ^^, though it doesn't read well, I should reword it for next time.
Quoted 6 lines
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1105
Quoted 4 lines
io_uring/net.c +4
-0+ if (issue_flags & IO_URING_F_IOWQ) + return -EAGAIN; + if (WARN_ON_ONCE(!(issue_flags & IO_URING_F_NONBLOCK))) + return -EAGAIN;If rebased on the current tree, does this go away?
It's just a little behind not to have that change
Signature · 1 line
Jens Axboe
Jens Axboe
maintainerRe: [RFC PATCH v4 13/16] io_uring: add io_recvzc request
In reply to Pavel Begunkov(Apparently this went out without my comments attached, only one thing worth noting so repeating that)
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
io_uring/net.c +1
-2@@ -695,7 +701,7 @@ static inline bool io_recv_finish(struct io_kiocb *req, int *ret,unsigned int cflags; cflags =io_put_kbuf (req, issue_flags); -if (msg->msg_inq && msg->msg_inq !=-1 ) +if (msg && msg->msg_inq && msg->msg_inq !=-1 ) cflags |=IORING_CQE_F_SOCK_NONEMPTY ;if (!(req->flags &REQ_F_APOLL_MULTISHOT )) {@@ -723,7 +729,7 @@ static inline bool io_recv_finish(struct io_kiocb *req, int *ret, goto enobufs;/* Known not-empty or unknown state, retry */ -if (cflags &IORING_CQE_F_SOCK_NONEMPTY || msg->msg_inq ==-1 ) {Quoted 8 lines
io_uring/net.c static inline bool io_recv_finish(struct io_kiocb *req, int *ret, · line 735
Quoted 4 lines
io_uring/net.c +1
-0+if (cflags &IORING_CQE_F_SOCK_NONEMPTY || (msg && msg->msg_inq ==-1 )) {if (sr->nr_multishot_loops++ <MULTISHOT_MAX_RETRY )return false;/* mshot retries exceeded, force a requeue */ Maybe refactor this a bit so that you don't need to add these NULL checks? That seems pretty fragile, hard to read, and should be doable without extra checks.
Quoted 2 lines
That chunk can be completely thrown away, we're not using
io_recv_finish()here anymore
OK good!
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
io_uring/net.c +0
-0@@ -1053,6 +1058,85 @@ struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req,return ifq; } +int io_recvzc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)Quoted 8 lines
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1067
Quoted 6 lines
io_uring/net.c +6
-0+{ + struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + +/* non-iopoll defer_taskrun only */ +if (!req->ctx->task_complete) +return -EINVAL ;What's the reasoning behind this?
Quoted 1 line
CQ locking, see the comment a couple lines below
My question here was more towards "is this something we want to do". Maybe this is just a temporary work-around and it's nothing to discuss, but I'm not sure we want to have opcodes only work on certain ring setups.
Signature · 1 line
Jens Axboe
Re: [RFC PATCH v4 13/16] io_uring: add io_recvzc request
On 3/14/24 16:14, Jens Axboe wrote:
[...]
Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1066
Quoted 10 lines
io_uring/net.c +6
-0@@ -1053,6 +1058,85 @@ struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req,return ifq; } +int io_recvzc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) +{ + struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + +/* non-iopoll defer_taskrun only */ +if (!req->ctx->task_complete) +return -EINVAL ;What's the reasoning behind this?
Quoted 6 lines
Quoted 1 line
CQ locking, see the comment a couple lines below
My question here was more towards "is this something we want to do". Maybe this is just a temporary work-around and it's nothing to discuss, but I'm not sure we want to have opcodes only work on certain ring setups.
I don't think it's that unreasonable restricting it. It's hard to care about !DEFER_TASKRUN for net workloads, it makes CQE posting a bit cleaner, and who knows where the single task part would become handy. Thinking about ifq termination, which should better cancel and wait for all corresponding zc requests, it's should be easier without parallel threads. E.g. what if another thread is in the enter syscall using ifq, or running task_work and not cancellable. Then apart from (non-atomic) refcounting, we'd need to somehow wait for it, doing wake ups on the zc side, and so on.
The CQ side is easy to support though, put conditional locking around the posting like fill/post_cqe does with the todays patchset.
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerRe: [RFC PATCH v4 13/16] io_uring: add io_recvzc request
Quoted 17 lines · Pavel Begunkov
On 3/14/24 16:14, Jens Axboe wrote:
[...]Quoted 15 lines
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
io_uring/net.c +0
-0@@ -1053,6 +1058,85 @@ struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req,return ifq; } +int io_recvzc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)Quoted 8 lines
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1067
Quoted 6 lines
io_uring/net.c +6
-0+{ + struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + +/* non-iopoll defer_taskrun only */ +if (!req->ctx->task_complete) +return -EINVAL ;What's the reasoning behind this?
CQ locking, see the comment a couple lines below
Quoted 7 lines
Quoted 4 lines
My question here was more towards "is this something we want to do". Maybe this is just a temporary work-around and it's nothing to discuss, but I'm not sure we want to have opcodes only work on certain ring setups.
I don't think it's that unreasonable restricting it. It's hard to care about !DEFER_TASKRUN for net workloads, it makes CQE posting a bit
I think there's a distinction between "not reasonable to support because it's complicated/impossible to do so", and "we prefer not to support it". I agree, as a developer it's hard to care about !DEFER_TASKRUN for networking workloads, but as a user, they will just setup a default queue until they wise up. And maybe this can be a good thing in that they'd be nudged toward DEFER_TASKRUN, but I can also see some head scratching when something just returns (the worst of all error codes) -EINVAL when they attempt to use it.
Quoted 1 line
cleaner, and who knows where the single task part would become handy.
But you can still take advantage of single task, since you know if that's going to be true or not. It just can't be unconditional.
Quoted 6 lines
Thinking about ifq termination, which should better cancel and wait for all corresponding zc requests, it's should be easier without parallel threads. E.g. what if another thread is in the enter syscall using ifq, or running
task_workand not cancellable. Then apart from (non-atomic) refcounting, we'd need to somehow wait for it, doing wake ups on the zc side, and so on.
I don't know, not seeing a lot of strong arguments for making it DEFER_TASKRUN only. My worry is that once we starting doing that, then more will follow. And honestly I think that would be a shame.
For ifq termination, surely these things are referenced, and termination would need to wait for the last reference to drop? And if that isn't an expected condition (it should not be), then a percpu ref would suffice. Nobody cares if the teardown side is more expensive, as long as the fast path is efficient.
Dunno - anyway, for now let's just leave it as-is, it's just something to consider once we get closer to a more finished patchset.
Quoted 3 lines
The CQ side is easy to support though, put conditional locking around the posting like fill/post_cqe does with the todays patchset.
Yep, which is one of the reasons why I was hopeful this could go away!
Signature · 1 line
Jens Axboe
Re: [RFC PATCH v4 13/16] io_uring: add io_recvzc request
Quoted 23 lines · Jens Axboe
Quoted 22 lines · Pavel Begunkov
On 3/14/24 16:14, Jens Axboe wrote:
[...]Quoted 19 lines
Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
io_uring/net.c +0
-0@@ -1053,6 +1058,85 @@ struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req,return ifq; } +int io_recvzc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)Quoted 8 lines
io_uring/net.c struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1067
Quoted 6 lines
io_uring/net.c +6
-0+{ + struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + +/* non-iopoll defer_taskrun only */ +if (!req->ctx->task_complete) +return -EINVAL ;What's the reasoning behind this?
Quoted 1 line
CQ locking, see the comment a couple lines below
My question here was more towards "is this something we want to do". Maybe this is just a temporary work-around and it's nothing to discuss, but I'm not sure we want to have opcodes only work on certain ring setups.
Quoted 8 lines
Quoted 2 lines
I don't think it's that unreasonable restricting it. It's hard to care about !DEFER_TASKRUN for net workloads, it makes CQE posting a bit
I think there's a distinction between "not reasonable to support because it's complicated/impossible to do so", and "we prefer not to support it". I agree, as a developer it's hard to care about !DEFER_TASKRUN for networking workloads, but as a user, they will just setup a default queue until they wise up. And maybe this can be a good thing in that
They'd still need to find a supported NIC and do all the other setup, comparably to that it doesn't add much trouble. And my usual argument is that io_uring is a low-level api, it's expected that people interacting with it directly are experienced enough, expect to spend some time to make it right and likely library devs.
Quoted 3 lines
they'd be nudged toward
DEFER_TASKRUN, but I can also see some head scratching when something just returns (the worst of all error codes)-EINVALwhen they attempt to use it.
Yeah, we should try to find a better error code, and the check should migrate to ifq registration.
Quoted 16 lines
Quoted 1 line
cleaner, and who knows where the single task part would become handy.
But you can still take advantage of single task, since you know if that's going to be true or not. It just can't be unconditional.
Quoted 6 lines
Thinking about ifq termination, which should better cancel and wait for all corresponding zc requests, it's should be easier without parallel threads. E.g. what if another thread is in the enter syscall using ifq, or running
task_workand not cancellable. Then apart from (non-atomic) refcounting, we'd need to somehow wait for it, doing wake ups on the zc side, and so on.I don't know, not seeing a lot of strong arguments for making it
DEFER_TASKRUNonly. My worry is that once we starting doing that, then more will follow. And honestly I think that would be a shame.
Quoted 5 lines
For ifq termination, surely these things are referenced, and termination would need to wait for the last reference to drop? And if that isn't an expected condition (it should not be), then a percpu ref would suffice. Nobody cares if the teardown side is more expensive, as long as the fast path is efficient.
You can solve any of that, it's true, the question how much crap you'd need to add in hot paths and diffstat wise. Just take a look at what a nice function io_recvmsg() is together with its helpers like io_recvmsg_multishot().
The biggest concern is optimisations and quirks that we can't predict at the moment. DEFER_TASKRUN/SINGLE_ISSUER provide a simpler model, I'd rather keep recvzc simple than having tens of conditional optimisations with different execution flavours and contexts. Especially, since it can be implemented later, wouldn't work the other way around.
Quoted 9 lines
Dunno - anyway, for now let's just leave it as-is, it's just something to consider once we get closer to a more finished patchset.
Quoted 3 lines
The CQ side is easy to support though, put conditional locking around the posting like fill/post_cqe does with the todays patchset.
Yep, which is one of the reasons why I was hopeful this could go away!
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerRe: [RFC PATCH v4 13/16] io_uring: add io_recvzc request
Quoted 27 lines · Pavel Begunkov
Quoted 26 lines · Jens Axboe
Quoted 24 lines · Pavel Begunkov
On 3/14/24 16:14, Jens Axboe wrote:
[...]Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Review struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1066
Quoted 10 lines
(quoted diff) +6
-0@@ -1053,6 +1058,85 @@ struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, return ifq; } +int io_recvzc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) +{ + struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + + /* non-iopoll defer_taskrun only */ + if (!req->ctx->task_complete) + return -EINVAL;What's the reasoning behind this?
Quoted 6 lines
Quoted 1 line
CQ locking, see the comment a couple lines below
My question here was more towards "is this something we want to do". Maybe this is just a temporary work-around and it's nothing to discuss, but I'm not sure we want to have opcodes only work on certain ring setups.
I don't think it's that unreasonable restricting it. It's hard to care about !DEFER_TASKRUN for net workloads, it makes CQE posting a bit
Quoted 8 lines
Quoted 5 lines
I think there's a distinction between "not reasonable to support because it's complicated/impossible to do so", and "we prefer not to support it". I agree, as a developer it's hard to care about !DEFER_TASKRUN for networking workloads, but as a user, they will just setup a default queue until they wise up. And maybe this can be a good thing in that
They'd still need to find a supported NIC and do all the other setup, comparably to that it doesn't add much trouble. And my
Hopefully down the line, it'll work on more NICs, and configuration will be less of a nightmare than it is now.
Quoted 4 lines
usual argument is that
io_uringis a low-level api, it's expected that people interacting with it directly are experienced enough, expect to spend some time to make it right and likely library devs.
Have you seen some of the code that has gone in to libraries for io_uring support? I have, and I don't think that statement is true at all for that side.
It should work out of the box even with a naive approach, while the best approach may require some knowledge. At least I think that's the sanest stance on that.
Quoted 6 lines
Quoted 3 lines
they'd be nudged toward
DEFER_TASKRUN, but I can also see some head scratching when something just returns (the worst of all error codes)-EINVALwhen they attempt to use it.Yeah, we should try to find a better error code, and the check should migrate to ifq registration.
Wasn't really a jab at the code in question, just more that -EINVAL is the ubiqitious error code for all kinds of things and it's hard to diagnose in general for a user. You just have to start guessing...
Quoted 22 lines
Quoted 16 lines
Quoted 1 line
cleaner, and who knows where the single task part would become handy.
But you can still take advantage of single task, since you know if that's going to be true or not. It just can't be unconditional.
Quoted 6 lines
Thinking about ifq termination, which should better cancel and wait for all corresponding zc requests, it's should be easier without parallel threads. E.g. what if another thread is in the enter syscall using ifq, or running
task_workand not cancellable. Then apart from (non-atomic) refcounting, we'd need to somehow wait for it, doing wake ups on the zc side, and so on.I don't know, not seeing a lot of strong arguments for making it
DEFER_TASKRUNonly. My worry is that once we starting doing that, then more will follow. And honestly I think that would be a shame.Quoted 5 lines
For ifq termination, surely these things are referenced, and termination would need to wait for the last reference to drop? And if that isn't an expected condition (it should not be), then a percpu ref would suffice. Nobody cares if the teardown side is more expensive, as long as the fast path is efficient.
Quoted 4 lines
You can solve any of that, it's true, the question how much crap you'd need to add in hot paths and diffstat wise. Just take a look at what a nice function
io_recvmsg()is together with its helpers likeio_recvmsg_multishot().
That is true, and I guess my real question is "what would it look like if we supported !DEFER_TASKRUN". Which I think is a valid question.
Quoted 6 lines
The biggest concern is optimisations and quirks that we can't predict at the moment. DEFER_TASKRUN/SINGLE_ISSUER provide a simpler model, I'd rather keep recvzc simple than having tens of conditional optimisations with different execution flavours and contexts. Especially, since it can be implemented later, wouldn't work the other way around.
Yes me too, and I'd hate to have two variants just because of that. But comparing to eg io_recv() and helpers, it's really not that bad. Hence my question on how much would it take, and how nasty would it be, to support !DEFER_TASKRUN.
Signature · 1 line
Jens Axboe
Re: [RFC PATCH v4 13/16] io_uring: add io_recvzc request
Quoted 34 lines · Jens Axboe
Quoted 33 lines · Pavel Begunkov
Quoted 31 lines · Jens Axboe
Quoted 17 lines · Pavel Begunkov
On 3/14/24 16:14, Jens Axboe wrote:
[...]Quoted 15 lines
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0
-0@@ -1053,6 +1058,85 @@ struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, return ifq; } +int io_recvzc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)Quoted 8 lines
Review struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1066
Quoted 6 lines
(quoted diff) +6
-0+{ + struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + + /* non-iopoll defer_taskrun only */ + if (!req->ctx->task_complete) + return -EINVAL;What's the reasoning behind this?
CQ locking, see the comment a couple lines below
Quoted 7 lines
Quoted 4 lines
My question here was more towards "is this something we want to do". Maybe this is just a temporary work-around and it's nothing to discuss, but I'm not sure we want to have opcodes only work on certain ring setups.
I don't think it's that unreasonable restricting it. It's hard to care about !DEFER_TASKRUN for net workloads, it makes CQE posting a bit
I think there's a distinction between "not reasonable to support because it's complicated/impossible to do so", and "we prefer not to support it". I agree, as a developer it's hard to care about !DEFER_TASKRUN for networking workloads, but as a user, they will just setup a default queue until they wise up. And maybe this can be a good thing in that
Quoted 4 lines
Quoted 2 lines
They'd still need to find a supported NIC and do all the other setup, comparably to that it doesn't add much trouble. And my
Hopefully down the line, it'll work on more NICs,
I wouldn't hope all necessary features will be seen in consumer cards
Quoted 1 line
and configuration will be less of a nightmare than it is now.
I'm already assuming steering will be taken care by the kernel, but you have to choose your nic, allocate an ifq, mmap a ring, and then you're getting scattered chunks instead of
recv((void *)one_large_buffer);
My point is that it requires more involvement from user by design.
Quoted 8 lines
Quoted 4 lines
usual argument is that
io_uringis a low-level api, it's expected that people interacting with it directly are experienced enough, expect to spend some time to make it right and likely library devs.Have you seen some of the code that has gone in to libraries for
io_uringsupport? I have, and I don't think that statement is true at all for that side.
Well, some implementations are crappy, some are ok, some are learning and improving what they have.
Quoted 53 lines
It should work out of the box even with a naive approach, while the best approach may require some knowledge. At least I think that's the sanest stance on that.
Quoted 6 lines
Quoted 3 lines
they'd be nudged toward
DEFER_TASKRUN, but I can also see some head scratching when something just returns (the worst of all error codes)-EINVALwhen they attempt to use it.Yeah, we should try to find a better error code, and the check should migrate to ifq registration.
Wasn't really a jab at the code in question, just more that
-EINVALis the ubiqitious error code for all kinds of things and it's hard to diagnose in general for a user. You just have to start guessing...Quoted 22 lines
Quoted 16 lines
Quoted 1 line
cleaner, and who knows where the single task part would become handy.
But you can still take advantage of single task, since you know if that's going to be true or not. It just can't be unconditional.
Quoted 6 lines
Thinking about ifq termination, which should better cancel and wait for all corresponding zc requests, it's should be easier without parallel threads. E.g. what if another thread is in the enter syscall using ifq, or running
task_workand not cancellable. Then apart from (non-atomic) refcounting, we'd need to somehow wait for it, doing wake ups on the zc side, and so on.I don't know, not seeing a lot of strong arguments for making it
DEFER_TASKRUNonly. My worry is that once we starting doing that, then more will follow. And honestly I think that would be a shame.Quoted 5 lines
For ifq termination, surely these things are referenced, and termination would need to wait for the last reference to drop? And if that isn't an expected condition (it should not be), then a percpu ref would suffice. Nobody cares if the teardown side is more expensive, as long as the fast path is efficient.
Quoted 4 lines
You can solve any of that, it's true, the question how much crap you'd need to add in hot paths and diffstat wise. Just take a look at what a nice function
io_recvmsg()is together with its helpers likeio_recvmsg_multishot().That is true, and I guess my real question is "what would it look like if we supported !DEFER_TASKRUN". Which I think is a valid question.
Quoted 6 lines
The biggest concern is optimisations and quirks that we can't predict at the moment. DEFER_TASKRUN/SINGLE_ISSUER provide a simpler model, I'd rather keep recvzc simple than having tens of conditional optimisations with different execution flavours and contexts. Especially, since it can be implemented later, wouldn't work the other way around.
Quoted 4 lines
Yes me too, and I'd hate to have two variants just because of that. But comparing to eg
io_recv()and helpers, it's really not that bad. Hence my question on how much would it take, and how nasty would it be, to support !DEFER_TASKRUN.
It might look bearable... at first, but when it stops on that? There will definitely be fixes and optimisations, whenever in my mind it's something that is not even needed. I guess I'm too traumatised by the amount of uapi binding features I wish I could axe out and never see again.
Signature · 1 line
Pavel Begunkov
Jens Axboe
maintainerRe: [RFC PATCH v4 13/16] io_uring: add io_recvzc request
Quoted 35 lines · Pavel Begunkov
Quoted 34 lines · Jens Axboe
Quoted 33 lines · Pavel Begunkov
Quoted 31 lines · Jens Axboe
Quoted 17 lines · Pavel Begunkov
On 3/14/24 16:14, Jens Axboe wrote:
[...]Quoted 15 lines
Quoted 14 lines
Quoted 4 lines
Quoted 4 lines
(quoted diff) +0
-0@@ -1053,6 +1058,85 @@ struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, return ifq; } +int io_recvzc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)Quoted 8 lines
Review struct io_zc_rx_ifq *io_zc_verify_sock(struct io_kiocb *req, · line 1066
Quoted 6 lines
(quoted diff) +6
-0+{ + struct io_recvzc *zc = io_kiocb_to_cmd(req, struct io_recvzc); + + /* non-iopoll defer_taskrun only */ + if (!req->ctx->task_complete) + return -EINVAL;What's the reasoning behind this?
CQ locking, see the comment a couple lines below
Quoted 7 lines
Quoted 4 lines
My question here was more towards "is this something we want to do". Maybe this is just a temporary work-around and it's nothing to discuss, but I'm not sure we want to have opcodes only work on certain ring setups.
I don't think it's that unreasonable restricting it. It's hard to care about !DEFER_TASKRUN for net workloads, it makes CQE posting a bit
I think there's a distinction between "not reasonable to support because it's complicated/impossible to do so", and "we prefer not to support it". I agree, as a developer it's hard to care about !DEFER_TASKRUN for networking workloads, but as a user, they will just setup a default queue until they wise up. And maybe this can be a good thing in that
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
They'd still need to find a supported NIC and do all the other setup, comparably to that it doesn't add much trouble. And my
Hopefully down the line, it'll work on more NICs,
I wouldn't hope all necessary features will be seen in consumer cards
Nah that would never be the case, but normal users aren't going to be interested in zerocopy rx. If they are, then it's power users, and they can pick an appropriate NIC rather than just rely on what's in their laptop or desktop PC. But hopefully on the server production front, there will be more NICs that support it. It's all driven by demand. If it's a useful feature, then customers will ask for it.
Quoted 9 lines
Quoted 1 line
and configuration will be less of a nightmare than it is now.
I'm already assuming steering will be taken care by the kernel, but you have to choose your nic, allocate an ifq, mmap a ring, and then you're getting scattered chunks instead of
recv((void *)one_large_buffer);My point is that it requires more involvement from user by design.
For sure, it's more complicated than non-zerocopy, that's unavoidable.
Quoted 11 lines
Quoted 8 lines
Quoted 4 lines
usual argument is that
io_uringis a low-level api, it's expected that people interacting with it directly are experienced enough, expect to spend some time to make it right and likely library devs.Have you seen some of the code that has gone in to libraries for
io_uringsupport? I have, and I don't think that statement is true at all for that side.Well, some implementations are crappy, some are ok, some are learning and improving what they have.
Right, it wasn't a jab at them in general, it's natural to start somewhere simple and then improve things as they go along. I don't expect folks to have the level of knowledge of the internals that we do, nor should they need to.
Quoted 57 lines
Quoted 52 lines
It should work out of the box even with a naive approach, while the best approach may require some knowledge. At least I think that's the sanest stance on that.
Quoted 6 lines
Quoted 3 lines
they'd be nudged toward
DEFER_TASKRUN, but I can also see some head scratching when something just returns (the worst of all error codes)-EINVALwhen they attempt to use it.Yeah, we should try to find a better error code, and the check should migrate to ifq registration.
Wasn't really a jab at the code in question, just more that
-EINVALis the ubiqitious error code for all kinds of things and it's hard to diagnose in general for a user. You just have to start guessing...Quoted 22 lines
Quoted 16 lines
Quoted 1 line
cleaner, and who knows where the single task part would become handy.
But you can still take advantage of single task, since you know if that's going to be true or not. It just can't be unconditional.
Quoted 6 lines
Thinking about ifq termination, which should better cancel and wait for all corresponding zc requests, it's should be easier without parallel threads. E.g. what if another thread is in the enter syscall using ifq, or running
task_workand not cancellable. Then apart from (non-atomic) refcounting, we'd need to somehow wait for it, doing wake ups on the zc side, and so on.I don't know, not seeing a lot of strong arguments for making it
DEFER_TASKRUNonly. My worry is that once we starting doing that, then more will follow. And honestly I think that would be a shame.Quoted 5 lines
For ifq termination, surely these things are referenced, and termination would need to wait for the last reference to drop? And if that isn't an expected condition (it should not be), then a percpu ref would suffice. Nobody cares if the teardown side is more expensive, as long as the fast path is efficient.
Quoted 4 lines
You can solve any of that, it's true, the question how much crap you'd need to add in hot paths and diffstat wise. Just take a look at what a nice function
io_recvmsg()is together with its helpers likeio_recvmsg_multishot().That is true, and I guess my real question is "what would it look like if we supported !DEFER_TASKRUN". Which I think is a valid question.
Quoted 6 lines
The biggest concern is optimisations and quirks that we can't predict at the moment. DEFER_TASKRUN/SINGLE_ISSUER provide a simpler model, I'd rather keep recvzc simple than having tens of conditional optimisations with different execution flavours and contexts. Especially, since it can be implemented later, wouldn't work the other way around.
Quoted 4 lines
Yes me too, and I'd hate to have two variants just because of that. But comparing to eg
io_recv()and helpers, it's really not that bad. Hence my question on how much would it take, and how nasty would it be, to support !DEFER_TASKRUN.
Quoted 5 lines
It might look bearable... at first, but when it stops on that? There will definitely be fixes and optimisations, whenever in my mind it's something that is not even needed. I guess I'm too traumatised by the amount of uapi binding features I wish I could axe out and never see again.
But that's real world though, particularly for the kernel. We'd all love to restart things from scratch, and sometimes that'd lead to something better which then down the line inevitably you'd love to redo again.
Signature · 1 line
Jens Axboe
RFC PATCH v4 14/16 net: execute custom callback from napi
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
Sometimes we want to access a napi protected resource from task context like in the case of io_uring zc falling back to copy and accessing the buffer ring. Add a helper function that allows to execute a custom function from napi context by first stopping it similarly to napi_busy_loop().
Experimental, needs much polishing and sharing bits with napi_busy_loop().
--- include/net/busy_poll.h | 7 +++++++ net/core/dev.c | 46 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+)
include/net/busy_poll.h +7 -0
@@ -47,6 +47,8 @@ bool sk_busy_loop_end(void *p, unsigned long start_time);void napi_busy_loop (unsigned int napi_id,bool (*loop_end)(void *,unsigned long ),void *loop_end_arg,bool prefer_busy_poll,u16 budget); +void napi_execute (struct napi_struct *napi, +void (*cb)(void *),void *cb_arg); void napi_busy_loop_rcu(unsigned int napi_id, bool (*loop_end)(void *, unsigned long),
@@ -63,6 +65,11 @@ static inline bool sk_can_busy_loop(struct sock *sk) return false; } +static inline void napi_execute (struct napi_struct *napi, +void (*cb)(void *),void *cb_arg) +{ +} +#endif /* CONFIG_NET_RX_BUSY_POLL */ static inline unsigned long busy_loop_current_time (void )
net/core/dev.c +46 -0
52 lines not on this page · raw message
Signature · 1 line
2.43.0
RFC PATCH v4 15/16 io_uring/zcrx: add copy fallback
In reply to David WeiCurrently, if user fails to keep up with the network and doesn't refill the buffer ring fast enough the NIC/driver will start dropping packets. That might be too punishing. Add a fallback path, which would allow drivers to allocate normal pages when there is starvation, then zc_rx_recv_skb() we'll detect them and copy into the user specified buffers, when they become available.
That should help with adoption and also help the user striking the right balance allocating just the right amount of zerocopy buffers but also being resilient to sudden surges in traffic.
--- io_uring/zc_rx.c | 111 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 105 insertions(+), 6 deletions(-)
io_uring/zc_rx.c +105 -6
@@ -8,6 +8,7 @@#include <linux/nospec.h> #include <net/page_pool/helpers.h> +#include <net/busy_poll.h> #include <net/tcp.h> #include <net/af_unix.h>
@@ -26,6 +27,11 @@ struct io_zc_rx_args {struct socket *sock; }; +struct io_zc_refill_data { +struct io_zc_rx_ifq *ifq; +struct io_zc_rx_buf *buf; +}; +typedef int (*bpf_op_t )(struct net_device *dev,struct netdev_bpf *bpf); static int __io_queue_mgmt(struct net_device *dev, struct io_zc_rx_ifq *ifq,
@@ -648,6 +654,34 @@ const struct memory_provider_ops io_uring_pp_zc_ops = { };EXPORT_SYMBOL (io_uring_pp_zc_ops); +static void io_napi_refill (void *data) +{ +struct io_zc_refill_data *rd = data; +struct io_zc_rx_ifq *ifq = rd->ifq; +netmem_ref netmem; + +if (WARN_ON_ONCE (!ifq->pp)) +return ; + + netmem =page_pool_alloc_netmem (ifq->pp,GFP_ATOMIC | __GFP_NOWARN); +if (!netmem) +return ; +if (WARN_ON_ONCE (!netmem_is_net_iov (netmem))) +return ; + + rd->buf =io_niov_to_buf (netmem_to_net_iov (netmem)); +} + +static struct io_zc_rx_buf *io_zc_get_buf_task_safe (struct io_zc_rx_ifq *ifq) +{ +struct io_zc_refill_data rd = { + .ifq = ifq, + }; + +napi_execute (ifq->pp->p.napi, io_napi_refill, &rd); +return rd.buf; +} +static bool zc_rx_queue_cqe (struct io_kiocb *req,struct io_zc_rx_buf *buf,struct io_zc_rx_ifq *ifq,int off,int len) {
@@ -669,6 +703,42 @@ static bool zc_rx_queue_cqe(struct io_kiocb *req, struct io_zc_rx_buf *buf,return true; } +static ssize_t zc_rx_copy_chunk (struct io_kiocb *req,struct io_zc_rx_ifq *ifq, +void *data,unsigned int offset,size_t len) +{ +size_t copy_size, copied =0 ; +struct io_zc_rx_buf *buf; +int ret =0 , off =0 ; +u8 *vaddr; + +do { + buf =io_zc_get_buf_task_safe (ifq); +if (!buf) { + ret = -ENOMEM ; +break ; + } + + vaddr =kmap_local_page (buf->page); + copy_size =min_t (size_t,PAGE_SIZE , len); +memcpy (vaddr, data + offset, copy_size); +kunmap_local (vaddr); + +if (!zc_rx_queue_cqe (req, buf, ifq, off, copy_size)) { +napi_pp_put_page (net_iov_to_netmem (&buf->niov), false); +return -ENOSPC ; + } + +io_zc_rx_get_buf_uref (buf); +napi_pp_put_page (net_iov_to_netmem (&buf->niov), false); + + offset += copy_size; + len -= copy_size; + copied += copy_size; + }while (offset < len); + +return copied ? copied : ret; +} +static int zc_rx_recv_frag (struct io_kiocb *req,struct io_zc_rx_ifq *ifq,const skb_frag_t *frag,int off,int len) {
@@ -688,7 +758,22 @@ static int zc_rx_recv_frag(struct io_kiocb *req, struct io_zc_rx_ifq *ifq,return -ENOSPC ;io_zc_rx_get_buf_uref (buf); }else { -return -EOPNOTSUPP ; +struct page *page =skb_frag_page (frag); +u32 p_off, p_len, t, copied =0 ; +u8 *vaddr; +int ret =0 ; + +skb_frag_foreach_page (frag, off, len, + page, p_off, p_len, t) { + vaddr =kmap_local_page (page); + ret =zc_rx_copy_chunk (req, ifq, vaddr, p_off, p_len); +kunmap_local (vaddr); + +if (ret <0 ) +return copied ? copied : ret; + copied += ret; + } + len = copied; }return len;
@@ -702,15 +787,29 @@ zc_rx_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,struct io_zc_rx_ifq *ifq = args->ifq;struct io_kiocb *req = args->req;struct sk_buff *frag_iter; -unsigned start, start_off; +unsigned start, start_off = offset;int i, copy, end, off;int ret =0 ; - start =skb_headlen (skb); - start_off = offset; +if (unlikely (offset <skb_headlen (skb))) { +ssize_t copied; +size_t to_copy; -if (offset < start) -return -EOPNOTSUPP ; + to_copy =min_t (size_t,skb_headlen (skb) - offset, len); + copied =zc_rx_copy_chunk (req, ifq, skb->data, offset, to_copy); +if (copied <0 ) { + ret = copied; + goto out; + } + offset += copied; + len -= copied; +if (!len) + goto out; +if (offset !=skb_headlen (skb)) + goto out; + } + + start =skb_headlen (skb);for (i =0 ; i <skb_shinfo (skb)->nr_frags; i++) {const skb_frag_t *frag;
Signature · 1 line
2.43.0
RFC PATCH v4 16/16 veth: add support for io_uring zc rx
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
Not for upstream, testing only
Add io_uring zerocopy support for veth. It's not truly zerocopy, the data is copied in napi, but that's early in the stack and so useful for now for testing.
--- drivers/net/veth.c | 214 +++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 208 insertions(+), 6 deletions(-)
drivers/net/veth.c +208 -6
@@ -26,6 +26,8 @@#include <linux/ptr_ring.h> #include <linux/bpf_trace.h> #include <linux/net_tstamp.h> +#include <linux/io_uring/net.h> +#include <net/netdev_rx_queue.h> #include <net/page_pool/helpers.h> #define DRV_NAME "veth"
@@ -67,6 +69,7 @@ struct veth_rq {struct ptr_ring xdp_ring;struct xdp_rxq_info xdp_rxq;struct page_pool *page_pool; +struct netdev_rx_queue rq; };struct veth_priv {
@@ -75,6 +78,7 @@ struct veth_priv {struct bpf_prog *_xdp_prog;struct veth_rq *rq;unsigned int requested_headroom; +bool zc_installed; };struct veth_xdp_tx_bq {
@@ -335,9 +339,12 @@ static bool veth_skb_is_eligible_for_gro(const struct net_device *dev,const struct net_device *rcv, const struct sk_buff *skb) { + struct veth_priv *rcv_priv = netdev_priv(rcv); +return !(dev->features &NETIF_F_ALL_TSO ) || (skb->destructor == sock_wfree && - rcv->features & (NETIF_F_GRO_FRAGLIST | NETIF_F_GRO_UDP_FWD)); + rcv->features & (NETIF_F_GRO_FRAGLIST |NETIF_F_GRO_UDP_FWD )) || + rcv_priv->zc_installed; }static netdev_tx_t veth_xmit (struct sk_buff *skb,struct net_device *dev)
@@ -726,6 +733,9 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,struct sk_buff *skb = *pskb;u32 frame_sz; +if (WARN_ON_ONCE (1 )) +return -EFAULT ; +if (skb_shared (skb) ||skb_head_is_locked (skb) ||skb_shinfo (skb)->nr_frags ||skb_headroom (skb) <XDP_PACKET_HEADROOM ) {
@@ -758,6 +768,90 @@ static int veth_convert_skb_to_xdp_buff(struct veth_rq *rq,return -ENOMEM ; } +static noinline struct sk_buff *veth_iou_rcv_skb(struct veth_rq *rq, +struct sk_buff *skb) +{ +struct sk_buff *nskb; +u32 size, len, off, max_head_size; +struct page *page; +int ret, i, head_off; +void *vaddr; + +/* Testing only, randomly send normal pages to test copy fallback */ +if (ktime_get_ns () %16 ==0 ) +return skb; + +skb_prepare_for_gro (skb); + max_head_size =skb_headlen (skb); + +rcu_read_lock (); + nskb =napi_alloc_skb (&rq->xdp_napi, max_head_size); +if (!nskb) + goto drop; + +skb_copy_header (nskb, skb); +skb_mark_for_recycle (nskb); + + size = max_head_size; +if (skb_copy_bits (skb,0 , nskb->data, size)) { +consume_skb (nskb); + goto drop; + } +skb_put (nskb, size); + head_off =skb_headroom (nskb) -skb_headroom (skb); +skb_headers_offset_update (nskb, head_off); + +/* Allocate paged area of new skb */ + off = size; + len = skb->len - off; + +for (i =0 ; i <MAX_SKB_FRAGS && off < skb->len; i++) { +struct io_zc_rx_buf *buf; +netmem_ref netmem; + + netmem =page_pool_alloc_netmem (rq->page_pool,GFP_ATOMIC | __GFP_NOWARN); +if (!netmem) { +consume_skb (nskb); + goto drop; + } +if (WARN_ON_ONCE (!netmem_is_net_iov (netmem))) { +consume_skb (nskb); + goto drop; + } + + buf =container_of (netmem_to_net_iov (netmem), + struct io_zc_rx_buf, niov); + page = buf->page; + +if (WARN_ON_ONCE (buf->niov.pp != rq->page_pool)) + goto drop; + + size =min_t (u32, len,PAGE_SIZE ); +skb_add_rx_frag_netmem (nskb, i, netmem,0 , size,PAGE_SIZE ); + + vaddr =kmap_atomic (page); + ret =skb_copy_bits (skb, off, vaddr, size); +kunmap_atomic (vaddr); + +if (ret) { +consume_skb (nskb); + goto drop; + } + len -= size; + off += size; + } +rcu_read_unlock (); + +consume_skb (skb); + skb = nskb; +return skb; +drop: +rcu_read_unlock (); +kfree_skb (skb); +return NULL ; +} + + static struct sk_buff *veth_xdp_rcv_skb(struct veth_rq *rq, struct sk_buff *skb, struct veth_xdp_tx_bq *bq,
@@ -901,8 +995,13 @@ static int veth_xdp_rcv(struct veth_rq *rq, int budget,/* ndo_start_xmit */ struct sk_buff *skb = ptr; - stats->xdp_bytes += skb->len; - skb =veth_xdp_rcv_skb (rq, skb, bq, stats); +if (rq->page_pool->mp_ops == &io_uring_pp_zc_ops) { + skb =veth_iou_rcv_skb (rq, skb); + }else { + stats->xdp_bytes += skb->len; + skb =veth_xdp_rcv_skb (rq, skb, bq, stats); + } +if (skb) {if (skb_shared (skb) ||skb_unclone (skb,GFP_ATOMIC ))netif_receive_skb (skb);
@@ -961,15 +1060,22 @@ static int veth_poll(struct napi_struct *napi, int budget)return done; } -static int veth_create_page_pool (struct veth_rq *rq) +static int veth_create_page_pool (struct veth_rq *rq,struct io_zc_rx_ifq *ifq) {struct page_pool_params pp_params = { .order =0 , .pool_size =VETH_RING_SIZE , .nid =NUMA_NO_NODE , .dev = &rq->dev->dev, + .napi = &rq->xdp_napi, }; +if (ifq) { + rq->rq.pp_private = ifq; + rq->rq.pp_ops = &io_uring_pp_zc_ops; + pp_params.queue = &rq->rq; + } + rq->page_pool =page_pool_create (&pp_params);if (IS_ERR (rq->page_pool)) {int err =PTR_ERR (rq->page_pool);
@@ -987,7 +1093,7 @@ static int __veth_napi_enable_range(struct net_device *dev, int start, int end)int err, i;for (i = start; i < end; i++) { - err =veth_create_page_pool (&priv->rq[i]); + err =veth_create_page_pool (&priv->rq[i],NULL );if (err) goto err_page_pool; }
@@ -1043,9 +1149,17 @@ static void veth_napi_del_range(struct net_device *dev, int start, int end)for (i = start; i < end; i++) {struct veth_rq *rq = &priv->rq[i]; +void *ptr; +int nr =0 ; rq->rx_notify_masked = false; -ptr_ring_cleanup (&rq->xdp_ring, veth_ptr_free); + +while ((ptr =ptr_ring_consume (&rq->xdp_ring))) { +veth_ptr_free (ptr); + nr++; + } + +ptr_ring_cleanup (&rq->xdp_ring,NULL ); }for (i = start; i < end; i++) {
@@ -1281,6 +1395,9 @@ static int veth_set_channels(struct net_device *dev,struct net_device *peer;int err; +if (priv->zc_installed) +return -EINVAL ; +/* sanity check. Upper bounds are already enforced by the caller */ if (!ch->rx_count || !ch->tx_count)return -EINVAL ;
@@ -1358,6 +1475,8 @@ static int veth_open(struct net_device *dev)struct net_device *peer =rtnl_dereference (priv->peer);int err; + priv->zc_installed = false; +if (!peer)return -ENOTCONN ;
@@ -1536,6 +1655,84 @@ static void veth_set_rx_headroom(struct net_device *dev, int new_hr)rcu_read_unlock (); } +static int __veth_iou_set (struct net_device *dev, +struct netdev_bpf *xdp) +{ +bool napi_already_on =veth_gro_requested (dev) && (dev->flags &IFF_UP ); +unsigned qid = xdp->zc_rx.queue_id; +struct veth_priv *priv =netdev_priv (dev); +struct net_device *peer; +struct veth_rq *rq; +int ret; + +if (priv->_xdp_prog) +return -EINVAL ; +if (qid >= dev->real_num_rx_queues) +return -EINVAL ; +if (!(dev->flags &IFF_UP )) +return -EOPNOTSUPP ; +if (dev->real_num_rx_queues !=1 ) +return -EINVAL ; + rq = &priv->rq[qid]; + +if (!xdp->zc_rx.ifq) { +if (!priv->zc_installed) +return -EINVAL ; + +veth_napi_del (dev); + priv->zc_installed = false; +if (!veth_gro_requested (dev) &&netif_running (dev)) { + dev->features &= ~NETIF_F_GRO ; +netdev_features_change (dev); + } +return 0 ; + } + +if (priv->zc_installed) +return -EINVAL ; + + peer =rtnl_dereference (priv->peer); + peer->hw_features &= ~NETIF_F_GSO_SOFTWARE ; + + ret =veth_create_page_pool (rq, xdp->zc_rx.ifq); +if (ret) +return ret; + + ret =ptr_ring_init (&rq->xdp_ring,VETH_RING_SIZE ,GFP_KERNEL ); +if (ret) { +page_pool_destroy (rq->page_pool); + rq->page_pool =NULL ; +return ret; + } + + priv->zc_installed = true; + +if (!veth_gro_requested (dev)) { +/* user-space did not require GRO, but adding XDP +* is supposed to get GRO working +*/ + dev->features |=NETIF_F_GRO ; +netdev_features_change (dev); + } +if (!napi_already_on) { +netif_napi_add (dev, &rq->xdp_napi, veth_poll); +napi_enable (&rq->xdp_napi); +rcu_assign_pointer (rq->napi, &rq->xdp_napi); + } +return 0 ; +} + +static int veth_iou_set (struct net_device *dev, +struct netdev_bpf *xdp) +{ +int ret; + +rtnl_lock (); + ret =__veth_iou_set (dev, xdp); +rtnl_unlock (); +return ret; +} +static int veth_xdp_set (struct net_device *dev,struct bpf_prog *prog,struct netlink_ext_ack *extack) {
@@ -1545,6 +1742,9 @@ static int veth_xdp_set(struct net_device *dev, struct bpf_prog *prog,unsigned int max_mtu;int err; +if (priv->zc_installed) +return -EINVAL ; + old_prog = priv->_xdp_prog; priv->_xdp_prog = prog; peer =rtnl_dereference (priv->peer);
@@ -1623,6 +1823,8 @@ static int veth_xdp(struct net_device *dev, struct netdev_bpf *xdp)switch (xdp->command) {case XDP_SETUP_PROG :return veth_xdp_set (dev, xdp->prog, xdp->extack); +case XDP_SETUP_ZC_RX : +return veth_iou_set (dev, xdp);default :return -EINVAL ; }
Signature · 1 line
2.43.0