io_uring zero copy rx
Subsystems (6)
v14 cover io_uring zero copy rx
This patchset contains io_uring patches needed by a new io_uring request implementing zero copy rx into userspace pages, eliminating a kernel to user copy.
We configure a page pool that a driver uses to fill a hw rx queue to hand out user pages instead of kernel pages. Any data that ends up hitting this hw rx queue will thus be dma'd into userspace memory directly, without needing to be bounced through kernel memory. 'Reading' data out of a socket instead becomes a _notification_ mechanism, where the kernel tells userspace where the data is. The overall approach is similar to the devmem TCP proposal.
This relies on hw header/data split, flow steering and RSS to ensure packet headers remain in kernel memory and only desired flows hit a hw rx queue configured for zero copy. Configuring this is outside of the scope of this patchset.
We share netdev core infra with devmem TCP. The main difference is that io_uring is used for the uAPI and the lifetime of all objects are bound to an io_uring instance. Data is 'read' using a new io_uring request type. When done, data is returned via a new shared refill queue. A zero copy page pool refills a hw rx queue from this refill queue directly. Of course, the lifetime of these data buffers are managed by io_uring rather than the networking stack, with different refcounting rules.
This patchset is the first step adding basic zero copy support. We will extend this iteratively with new features e.g. dynamically allocated zero copy areas, THP support, dmabuf support, improved copy fallback, general optimisations and more.
In terms of netdev support, we're first targeting Broadcom bnxt. Patches aren't included since Taehee Yoo has already sent a more comprehensive patchset adding support in [1]. Google gve should already support this, and Mellanox mlx5 support is WIP pending driver changes.
===========
Performance
===========
Note: Comparison with epoll + TCP_ZEROCOPY_RECEIVE isn't done yet.
Test setup:
* AMD EPYC 9454
* Broadcom BCM957508 200G
* Kernel v6.11 base [2]
* liburing fork [3]
* kperf fork [4]
* 4K MTU
* Single TCP flow
With application thread + net rx softirq pinned to _different_ cores:
+-------------------------------+ | epoll | io_uring | |-----------|-------------------| | 82.2 Gbps | 116.2 Gbps (+41%) | +-------------------------------+
Pinned to _same_ core:
+-------------------------------+ | epoll | io_uring | |-----------|-------------------| | 62.6 Gbps | 80.9 Gbps (+29%) | +-------------------------------+
=====
Links
=====
Broadcom bnxt support:
[1]: https://lore.kernel.org/netdev/[email protected]/
Linux kernel branch including io_uring bits:
[2]: https://github.com/isilence/linux.git zcrx/v13
liburing for testing:
[3]: https://github.com/isilence/liburing.git zcrx/next
kperf for testing:
[4]: https://git.kernel.dk/kperf.git
Changes in v14:
---------------
* Remove net-next in patch subjects
* Cast to struct sockaddr in selftest for bind() and connect()
Changes in v13:
---------------io_uring
* Add missing ~IORING_ZCRX_AREA_MASK
* Update documentation
* Selftest changes
* Remove ipv4 support
* Use ethtool() instea dof cmd()
net
* Fix race between io_uring closing and netdev unregister
* Regenerate Netlink YAML
Changes in v12:
---------------
* Check nla_nest_start() errors
* Don't leak a netdev, add missing netdev_put()
* Warn on failed queue restart during close
Changes in v11:
---------------
* Add a shim provider helper for page_pool_set_dma_addr_netmem()
* Drop netdev in ->uninstall, pin struct device instead
* Add net_mp_open_rxq() and net_mp_close_rxq()
* Remove unneeded CFLAGS += -I/usr/include/ in selftest Makefile
Changes in v10:
---------------
* Fix !CONFIG_PAGE_POOL build
* Use acquire/release for RQ in examples
* Fix page_pool_ref_netmem for net_iov
* Move provider helpers / definitions into a new file
* Don’t export page_pool_{set,clear}_pp_info, introduce net_mp_niov_{set,clear}_page_pool() instead
* Remove devmem.h from net/core/page_pool_user.c
* Add Netdev yaml for io-uring attribute
* Add memory provider ops for filling in Netlink info
Changes in v9:
--------------
* Fail proof against multiple page pools running the same memory provider
* Lock the consumer side of the refill queue.
* Move scrub into io_uring exit.
* Kill napi_execute.
* Kill area init api and export finer grained net helpers as partial
init now need to happen in ->alloc_netmems()
* Separate user refcounting.
* Fix copy fallback path math.
* Add rodata check to page_pool_init()
* Fix incorrect path in documentation
Changes in v8:
--------------
* add documentation and selftest
* use io_uring regions for the refill ring
Changes in v7:
--------------
net:
* Use NAPI_F_PREFER_BUSY_POLL for napi_execute + stylistics changes.
Changes in v6:
--------------
Please note: Comparison with TCP_ZEROCOPY_RECEIVE isn't done yet.
net:
* Drop a devmem.h clean up patch.
* Migrate to netdev_get_by_index from deprecated API.
* Fix !CONFIG_NET_DEVMEM build.
* Don’t return into the page pool cache directly, use a new helper
* Refactor napi_execute
io_uring:
* Require IORING_RECV_MULTISHOT flag set.
* Add unselectable CONFIG_IO_URING_ZCRX.
* Pulled latest io_uring changes.
* Unexport io_uring_pp_zc_ops.
Changes in v5:
--------------
* Rebase on top of merged net_iov + netmem infra.
* Decouple net_iov from devmem TCP.
* Use netdev queue API to allocate an rx queue.
* Minor uAPI enhancements for future extensibility.
* QoS improvements with request throttling.
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.
David Wei (6):
io_uring/zcrx: add interface queue and refill queue io_uring/zcrx: add io_zcrx_area io_uring/zcrx: add io_recvzc request io_uring/zcrx: set pp memory provider for an rx queue
net: add documentation for io_uring zcrx io_uring/zcrx: add selftest
Pavel Begunkov (5):
io_uring/zcrx: grab a net device
io_uring/zcrx: implement zerocopy receive pp memory provider io_uring/zcrx: dma-map area for the device io_uring/zcrx: throttle receive requests io_uring/zcrx: add copy fallback
Documentation/networking/index.rst | 1 + Documentation/networking/iou-zcrx.rst | 202 ++++ Kconfig | 2 + include/linux/io_uring_types.h | 6 + include/uapi/linux/io_uring.h | 54 +- io_uring/KConfig | 10 + io_uring/Makefile | 1 + io_uring/io_uring.c | 7 + io_uring/io_uring.h | 10 + io_uring/memmap.h | 1 + io_uring/net.c | 74 ++ io_uring/opdef.c | 16 + io_uring/register.c | 7 + io_uring/rsrc.c | 2 +- io_uring/rsrc.h | 1 + io_uring/zcrx.c | 954 ++++++++++++++++++ io_uring/zcrx.h | 73 ++ .../selftests/drivers/net/hw/.gitignore | 2 + .../testing/selftests/drivers/net/hw/Makefile | 5 + .../selftests/drivers/net/hw/iou-zcrx.c | 426 ++++++++ .../selftests/drivers/net/hw/iou-zcrx.py | 64 ++ 21 files changed, 1916 insertions(+), 2 deletions(-) create mode 100644 Documentation/networking/iou-zcrx.rst create mode 100644 io_uring/KConfig create mode 100644 io_uring/zcrx.c create mode 100644 io_uring/zcrx.h create mode 100644 tools/testing/selftests/drivers/net/hw/iou-zcrx.c create mode 100755 tools/testing/selftests/drivers/net/hw/iou-zcrx.py
Signature · 1 line
2.43.5
PATCH v14 1/11 io_uring/zcrx: add interface queue and refill queue
Add a new object called an interface queue (ifq) that represents a net rx queue that has been configured for zero copy. Each ifq is registered using a new registration opcode IORING_REGISTER_ZCRX_IFQ.
The refill queue is allocated by the kernel and mapped by userspace using a new offset IORING_OFF_RQ_RING, in a similar fashion to the main SQ/CQ. It is used by userspace to return buffers that it is done with, which will then be re-used by the netdev again.
The main CQ ring is used to notify userspace of received data by using the upper 16 bytes of a big CQE as a new struct io_uring_zcrx_cqe. Each entry contains the offset + len to the data.
For now, each io_uring instance only has a single ifq.
--- Kconfig | 2 + include/linux/io_uring_types.h | 6 ++ include/uapi/linux/io_uring.h | 43 +++++++++- io_uring/KConfig | 10 +++ io_uring/Makefile | 1 + io_uring/io_uring.c | 7 ++ io_uring/memmap.h | 1 + io_uring/register.c | 7 ++ io_uring/zcrx.c | 149 +++++++++++++++++++++++++++++++++ io_uring/zcrx.h | 35 ++++++++ 10 files changed, 260 insertions(+), 1 deletion(-) create mode 100644 io_uring/KConfig create mode 100644 io_uring/zcrx.c create mode 100644 io_uring/zcrx.h
Kconfig +2 -0
@@ -30,3 +30,5 @@ source "lib/Kconfig" source "lib/Kconfig.debug" source "Documentation/Kconfig" + +source "io_uring/KConfig"
include/linux/io_uring_types.h +6 -0
@@ -40,6 +40,8 @@ enum io_uring_cmd_flags { IO_URING_F_TASK_DEAD = (1 << 13), }; +struct io_zcrx_ifq ; +struct io_wq_work_node {struct io_wq_work_node *next; };
@@ -382,6 +384,8 @@ struct io_ring_ctx {struct wait_queue_head poll_wq;struct io_restriction restrictions; +struct io_zcrx_ifq *ifq; +u32 pers_next;struct xarray personalities;
@@ -434,6 +438,8 @@ struct io_ring_ctx {struct io_mapped_region ring_region;/* used for optimised request parameter and wait argument passing */ struct io_mapped_region param_region; +/* just one zcrx per ring for now, will move to io_zcrx_ifq eventually */ +struct io_mapped_region zcrx_region; };struct io_tw_state {
include/uapi/linux/io_uring.h +42 -1
55 lines not on this page · raw message
io_uring/KConfig new file +10 -0
@@ -0,0 +1,10 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# io_uring configuration +# + +config IO_URING_ZCRX + def_bool y + depends on PAGE_POOL + depends on INET + depends on NET_RX_BUSY_POLL
io_uring/Makefile +1 -0
@@ -14,6 +14,7 @@ obj-$(CONFIG_IO_URING) += io_uring.o opdef.o kbuf.o rsrc.o notif.o \ epoll.o statx.o timeout.o fdinfo.o \ cancel.o waitid.o register.o \ truncate.o memmap.o alloc_cache.o +obj-$(CONFIG_IO_URING_ZCRX) += zcrx.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
@@ -97,6 +97,7 @@#include "uring_cmd.h" #include "msg_ring.h" #include "memmap.h" +#include "zcrx.h" #include "timeout.h" #include "poll.h"
@@ -2700,6 +2701,7 @@ static __cold void io_ring_ctx_free(struct io_ring_ctx *ctx)mutex_lock (&ctx->uring_lock);io_sqe_buffers_unregister (ctx);io_sqe_files_unregister (ctx); +io_unregister_zcrx_ifqs (ctx);io_cqring_overflow_kill (ctx);io_eventfd_unregister (ctx);io_free_alloc_caches (ctx);
@@ -2859,6 +2861,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_zcrx_ifqs (ctx); +mutex_unlock (&ctx->uring_lock); + }if (ctx->flags &IORING_SETUP_DEFER_TASKRUN )io_move_task_work_from_local (ctx);
io_uring/memmap.h +1 -0
@@ -2,6 +2,7 @@#define IO_URING_MEMMAP_H #define IORING_MAP_OFF_PARAM_REGION 0x20000000ULL +#define IORING_MAP_OFF_ZCRX_REGION 0x30000000ULLstruct page **io_pin_pages (unsigned long ubuf,unsigned long len,int *npages);
io_uring/register.c +7 -0
@@ -30,6 +30,7 @@#include "eventfd.h" #include "msg_ring.h" #include "memmap.h" +#include "zcrx.h" #define IORING_MAX_RESTRICTIONS (IORING_RESTRICTION_LAST + \ IORING_REGISTER_LAST + IORING_OP_LAST)
@@ -813,6 +814,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,break ; ret =io_register_clone_buffers (ctx, arg);break ; +case IORING_REGISTER_ZCRX_IFQ : + ret = -EINVAL ; +if (!arg || nr_args !=1 ) +break ; + ret =io_register_zcrx_ifq (ctx, arg); +break ;case IORING_REGISTER_RESIZE_RINGS : ret = -EINVAL ;if (!arg || nr_args !=1 )
io_uring/zcrx.c new file +149 -0
149 lines not on this page · raw message
io_uring/zcrx.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 + +#include <linux/io_uring_types.h> + +struct io_zcrx_ifq { +struct io_ring_ctx *ctx; +struct io_uring *rq_ring; +struct io_uring_zcrx_rqe *rqes; +u32 rq_entries; + +u32 if_rxq; +}; + +#if defined(CONFIG_IO_URING_ZCRX ) +int io_register_zcrx_ifq (struct io_ring_ctx *ctx, +struct io_uring_zcrx_ifq_reg __user *arg); +void io_unregister_zcrx_ifqs (struct io_ring_ctx *ctx); +void io_shutdown_zcrx_ifqs (struct io_ring_ctx *ctx); +#else +static inline int io_register_zcrx_ifq (struct io_ring_ctx *ctx, +struct io_uring_zcrx_ifq_reg __user *arg) +{ +return -EOPNOTSUPP ; +} +static inline void io_unregister_zcrx_ifqs (struct io_ring_ctx *ctx) +{ +} +static inline void io_shutdown_zcrx_ifqs (struct io_ring_ctx *ctx) +{ +} +#endif + +#endif
Signature · 1 line
2.43.5
PATCH v14 2/11 io_uring/zcrx: add io_zcrx_area
In reply to David WeiAdd io_zcrx_area that represents a region of userspace memory that is used for zero copy. During ifq registration, userspace passes in the uaddr and len of userspace memory, which is then pinned by the kernel. Each net_iov is mapped to one of these pages.
The freelist is a spinlock protected list that keeps track of all the net_iovs/pages that aren't used.
For now, there is only one area per ifq and area registration happens implicitly as part of ifq registration. There is no API for adding/removing areas yet. The struct for area registration is there for future extensibility once we support multiple areas and TCP devmem.
--- include/uapi/linux/io_uring.h | 9 ++++ io_uring/rsrc.c | 2 +- io_uring/rsrc.h | 1 + io_uring/zcrx.c | 89 ++++++++++++++++++++++++++++++++++- io_uring/zcrx.h | 16 +++++++ 5 files changed, 114 insertions(+), 3 deletions(-)
include/uapi/linux/io_uring.h +9 -0
@@ -981,6 +981,15 @@ struct io_uring_zcrx_offsets {__u64 __resv[2 ]; }; +struct io_uring_zcrx_area_reg { +__u64 addr; +__u64 len; +__u64 rq_area_token; +__u32 flags; +__u32 __resv1; +__u64 __resv2[2 ]; +}; +/* * Argument for IORING_REGISTER_ZCRX_IFQ */
io_uring/rsrc.c +1 -1
@@ -77,7 +77,7 @@ static int io_account_mem(struct io_ring_ctx *ctx, unsigned long nr_pages)return 0 ; } -static int io_buffer_validate (struct iovec *iov) +int io_buffer_validate (struct iovec *iov) {unsigned long tmp, acct_len = iov->iov_len + (PAGE_SIZE -1 );
io_uring/rsrc.h +1 -0
@@ -68,6 +68,7 @@ int io_register_rsrc_update(struct io_ring_ctx *ctx, void __user *arg, unsigned size, unsigned type);int io_register_rsrc (struct io_ring_ctx *ctx,void __user *arg,unsigned int size,unsigned int type); +int io_buffer_validate (struct iovec *iov);bool io_check_coalesce_buffer (struct page **page_array,int nr_pages,struct io_imu_folio_data *data);
io_uring/zcrx.c +87 -2
131 lines not on this page · raw message
io_uring/zcrx.h +16 -0
@@ -3,9 +3,25 @@#define IOU_ZC_RX_H #include <linux/io_uring_types.h> +#include <net/page_pool/types.h> + +struct io_zcrx_area { +struct net_iov_area nia; +struct io_zcrx_ifq *ifq; + +u16 area_id; +struct page **pages; + +/* freelist */ +spinlock_t freelist_lock____cacheline_aligned_in_smp ; +u32 free_count; +u32 *freelist; +};struct io_zcrx_ifq {struct io_ring_ctx *ctx; +struct io_zcrx_area *area; +struct io_uring *rq_ring;struct io_uring_zcrx_rqe *rqes;u32 rq_entries;
Signature · 1 line
2.43.5
PATCH v14 3/11 io_uring/zcrx: grab a net device
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
Zerocopy receive needs a net device to bind to its rx queue and dma map buffers. As a preparation to following patches, resolve a net device from the if_idx parameter with no functional changes otherwise.
--- io_uring/zcrx.c | 28 ++++++++++++++++++++++++++++ io_uring/zcrx.h | 5 +++++ 2 files changed, 33 insertions(+)
io_uring/zcrx.c +28 -0
@@ -3,6 +3,8 @@#include <linux/errno.h> #include <linux/mm.h> #include <linux/io_uring.h> +#include <linux/netdevice.h> +#include <linux/rtnetlink.h> #include <uapi/linux/io_uring.h>
@@ -128,13 +130,28 @@ static struct io_zcrx_ifq *io_zcrx_ifq_alloc(struct io_ring_ctx *ctx) ifq->if_rxq =-1 ; ifq->ctx = ctx; +spin_lock_init (&ifq->lock);return ifq; } +static void io_zcrx_drop_netdev (struct io_zcrx_ifq *ifq) +{ +spin_lock (&ifq->lock); +if (ifq->netdev) { +netdev_put (ifq->netdev, &ifq->netdev_tracker); + ifq->netdev =NULL ; + } +spin_unlock (&ifq->lock); +} +static void io_zcrx_ifq_free (struct io_zcrx_ifq *ifq) { +io_zcrx_drop_netdev (ifq); +if (ifq->area)io_zcrx_free_area (ifq->area); +if (ifq->dev) +put_device (ifq->dev);io_free_rbuf_ring (ifq);kfree (ifq);
@@ -195,6 +212,17 @@ int io_register_zcrx_ifq(struct io_ring_ctx *ctx, ifq->rq_entries = reg.rq_entries; ifq->if_rxq = reg.if_rxq; + ret = -ENODEV ; + ifq->netdev =netdev_get_by_index (current->nsproxy->net_ns, reg.if_idx, + &ifq->netdev_tracker,GFP_KERNEL ); +if (!ifq->netdev) + goto err; + + ifq->dev = ifq->netdev->dev.parent; +if (!ifq->dev) +return -EOPNOTSUPP ; +get_device (ifq->dev); + reg.offsets.rqes =sizeof (struct io_uring ); reg.offsets.head = offsetof(struct io_uring , head); reg.offsets.tail = offsetof(struct io_uring , tail);
io_uring/zcrx.h +5 -0
@@ -4,6 +4,7 @@#include <linux/io_uring_types.h> #include <net/page_pool/types.h> +#include <net/net_trackers.h> struct io_zcrx_area {struct net_iov_area nia;
@@ -27,6 +28,10 @@ struct io_zcrx_ifq {u32 rq_entries;u32 if_rxq; +struct device *dev; +struct net_device *netdev; +netdevice_tracker netdev_tracker; +spinlock_t lock; };#if defined(CONFIG_IO_URING_ZCRX )
Signature · 1 line
2.43.5
PATCH v14 4/11 io_uring/zcrx: implement zerocopy receive pp memory provider
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
Implement a page pool memory provider for io_uring to receieve in a zero copy fashion. For that, the provider allocates user pages wrapped around into struct net_iovs, that are stored in a previously registered struct net_iov_area.
Unlike the traditional receive, that frees pages and returns them back to the page pool right after data was copied to the user, e.g. inside recv(2), we extend the lifetime until the user space confirms that it's done processing the data. That's done by taking a net_iov reference. When the user is done with the buffer, it must return it back to the kernel by posting an entry into the refill ring, which is usually polled off the io_uring memory provider callback in the page pool's netmem allocation path.
There is also a separate set of per net_iov "user" references accounting whether a buffer is currently given to the user (including possible fragmentation).
--- io_uring/zcrx.c | 274 ++++++++++++++++++++++++++++++++++++++++++++++++ io_uring/zcrx.h | 3 + 2 files changed, 277 insertions(+)
io_uring/zcrx.c +274 -0
@@ -2,10 +2,17 @@#include <linux/kernel.h> #include <linux/errno.h> #include <linux/mm.h> +#include <linux/nospec.h> #include <linux/io_uring.h> #include <linux/netdevice.h> #include <linux/rtnetlink.h> +#include <net/page_pool/helpers.h> +#include <net/page_pool/memory_provider.h> +#include <net/netlink.h> + +#include <trace/events/page_pool.h> +#include <uapi/linux/io_uring.h> #include "io_uring.h"
@@ -16,6 +23,33 @@#define IO_RQ_MAX_ENTRIES 32768 +__maybe_unused +static const struct memory_provider_ops io_uring_pp_zc_ops; + +static inline struct io_zcrx_area *io_zcrx_iov_to_area (const struct net_iov *niov) +{ +struct net_iov_area *owner =net_iov_owner (niov); + + return container_of(owner, struct io_zcrx_area, nia); +} + +static inline atomic_t *io_get_user_counter (struct net_iov *niov) +{ +struct io_zcrx_area *area =io_zcrx_iov_to_area (niov); + +return &area->user_refs[net_iov_idx (niov)]; +} + +static bool io_zcrx_put_niov_uref (struct net_iov *niov) +{ +atomic_t *uref =io_get_user_counter (niov); + +if (unlikely (!atomic_read (uref))) +return false; +atomic_dec (uref); +return true; +} +static int io_allocate_rbuf_ring (struct io_zcrx_ifq *ifq,struct io_uring_zcrx_ifq_reg *reg,struct io_uring_region_desc *rd)
@@ -51,6 +85,7 @@ static void io_zcrx_free_area(struct io_zcrx_area *area) {kvfree (area->freelist);kvfree (area->nia.niovs); +kvfree (area->user_refs);if (area->pages) {unpin_user_pages (area->pages, area->nia.num_niovs);kvfree (area->pages);
@@ -106,6 +141,19 @@ static int io_zcrx_create_area(struct io_zcrx_ifq *ifq,for (i =0 ; i < nr_pages; i++) area->freelist[i] = i; + area->user_refs =kvmalloc_array (nr_pages,sizeof (area->user_refs[0 ]), +GFP_KERNEL | __GFP_ZERO); +if (!area->user_refs) + goto err; + +for (i =0 ; i < nr_pages; i++) { +struct net_iov *niov = &area->nia.niovs[i]; + + niov->owner = &area->nia; + area->freelist[i] = i; +atomic_set (&area->user_refs[i],0 ); + } + area->free_count = nr_pages; area->ifq = ifq;/* we're only supporting one area per ifq for now */
@@ -131,6 +179,7 @@ static struct io_zcrx_ifq *io_zcrx_ifq_alloc(struct io_ring_ctx *ctx) ifq->if_rxq =-1 ; ifq->ctx = ctx;spin_lock_init (&ifq->lock); +spin_lock_init (&ifq->rq_lock);return ifq; }
@@ -256,7 +305,232 @@ void io_unregister_zcrx_ifqs(struct io_ring_ctx *ctx)io_zcrx_ifq_free (ifq); } +static struct net_iov *__io_zcrx_get_free_niov (struct io_zcrx_area *area) +{ +unsigned niov_idx; + +lockdep_assert_held (&area->freelist_lock); + + niov_idx = area->freelist[--area->free_count]; +return &area->nia.niovs[niov_idx]; +} + +static void io_zcrx_return_niov_freelist (struct net_iov *niov) +{ +struct io_zcrx_area *area =io_zcrx_iov_to_area (niov); + +spin_lock_bh (&area->freelist_lock); + area->freelist[area->free_count++] =net_iov_idx (niov); +spin_unlock_bh (&area->freelist_lock); +} + +static void io_zcrx_return_niov (struct net_iov *niov) +{ +netmem_ref netmem =net_iov_to_netmem (niov); + +page_pool_put_unrefed_netmem (niov->pp, netmem,-1 , false); +} + +static void io_zcrx_scrub (struct io_zcrx_ifq *ifq) +{ +struct io_zcrx_area *area = ifq->area; +int i; + +if (!area) +return ; + +/* Reclaim back all buffers given to the user space. */ +for (i =0 ; i < area->nia.num_niovs; i++) { +struct net_iov *niov = &area->nia.niovs[i]; +int nr; + +if (!atomic_read (io_get_user_counter (niov))) +continue ; + nr =atomic_xchg (io_get_user_counter (niov),0 ); +if (nr && !page_pool_unref_netmem (net_iov_to_netmem (niov), nr)) +io_zcrx_return_niov (niov); + } +} +void io_shutdown_zcrx_ifqs (struct io_ring_ctx *ctx) {lockdep_assert_held (&ctx->uring_lock); + +if (ctx->ifq) +io_zcrx_scrub (ctx->ifq); +} + +static inline u32 io_zcrx_rqring_entries (struct io_zcrx_ifq *ifq) +{ +u32 entries; + + entries =smp_load_acquire (&ifq->rq_ring->tail) - ifq->cached_rq_head; +return min (entries, ifq->rq_entries); } + +static struct io_uring_zcrx_rqe *io_zcrx_get_rqe (struct io_zcrx_ifq *ifq, +unsigned mask) +{ +unsigned int idx = ifq->cached_rq_head++ & mask; + +return &ifq->rqes[idx]; +} + +static void io_zcrx_ring_refill (struct page_pool *pp, +struct io_zcrx_ifq *ifq) +{ +unsigned int mask = ifq->rq_entries -1 ; +unsigned int entries; +netmem_ref netmem; + +spin_lock_bh (&ifq->rq_lock); + + entries =io_zcrx_rqring_entries (ifq); + entries =min_t (unsigned, entries,PP_ALLOC_CACHE_REFILL - pp->alloc.count); +if (unlikely (!entries)) { +spin_unlock_bh (&ifq->rq_lock); +return ; + } + +do { +struct io_uring_zcrx_rqe *rqe =io_zcrx_get_rqe (ifq, mask); +struct io_zcrx_area *area; +struct net_iov *niov; +unsigned niov_idx, area_idx; + + area_idx = rqe->off >>IORING_ZCRX_AREA_SHIFT ; + niov_idx = (rqe->off & ~IORING_ZCRX_AREA_MASK ) >>PAGE_SHIFT ; + +if (unlikely (rqe->__pad || area_idx)) +continue ; + area = ifq->area; + +if (unlikely (niov_idx >= area->nia.num_niovs)) +continue ; + niov_idx =array_index_nospec (niov_idx, area->nia.num_niovs); + + niov = &area->nia.niovs[niov_idx]; +if (!io_zcrx_put_niov_uref (niov)) +continue ; + + netmem =net_iov_to_netmem (niov); +if (page_pool_unref_netmem (netmem,1 ) !=0 ) +continue ; + +if (unlikely (niov->pp != pp)) { +io_zcrx_return_niov (niov); +continue ; + } + +net_mp_netmem_place_in_cache (pp, netmem); + }while (--entries); + +smp_store_release (&ifq->rq_ring->head, ifq->cached_rq_head); +spin_unlock_bh (&ifq->rq_lock); +} + +static void io_zcrx_refill_slow (struct page_pool *pp,struct io_zcrx_ifq *ifq) +{ +struct io_zcrx_area *area = ifq->area; + +spin_lock_bh (&area->freelist_lock); +while (area->free_count && pp->alloc.count <PP_ALLOC_CACHE_REFILL ) { +struct net_iov *niov =__io_zcrx_get_free_niov (area); +netmem_ref netmem =net_iov_to_netmem (niov); + +net_mp_niov_set_page_pool (pp, niov); +net_mp_netmem_place_in_cache (pp, netmem); + } +spin_unlock_bh (&area->freelist_lock); +} + +static netmem_ref io_pp_zc_alloc_netmems (struct page_pool *pp,gfp_t gfp) +{ +struct io_zcrx_ifq *ifq = pp->mp_priv; + +/* pp should already be ensuring that */ +if (unlikely (pp->alloc.count)) + goto out_return; + +io_zcrx_ring_refill (pp, ifq); +if (likely (pp->alloc.count)) + goto out_return; + +io_zcrx_refill_slow (pp, ifq); +if (!pp->alloc.count) +return 0 ; +out_return: +return pp->alloc.cache[--pp->alloc.count]; +} + +static bool io_pp_zc_release_netmem (struct page_pool *pp,netmem_ref netmem) +{ +struct net_iov *niov; + +if (WARN_ON_ONCE (!netmem_is_net_iov (netmem))) +return false; + + niov =netmem_to_net_iov (netmem); +net_mp_niov_clear_page_pool (niov); +io_zcrx_return_niov_freelist (niov); +return false; +} + +static int io_pp_zc_init (struct page_pool *pp) +{ +struct io_zcrx_ifq *ifq = pp->mp_priv; + +if (WARN_ON_ONCE (!ifq)) +return -EINVAL ; +if (pp->dma_map) +return -EOPNOTSUPP ; +if (pp->p.order !=0 ) +return -EOPNOTSUPP ; + +percpu_ref_get (&ifq->ctx->refs); +return 0 ; +} + +static void io_pp_zc_destroy (struct page_pool *pp) +{ +struct io_zcrx_ifq *ifq = pp->mp_priv; +struct io_zcrx_area *area = ifq->area; + +if (WARN_ON_ONCE (area->free_count != area->nia.num_niovs)) +return ; +percpu_ref_put (&ifq->ctx->refs); +} + +static int io_pp_nl_fill (void *mp_priv,struct sk_buff *rsp, +struct netdev_rx_queue *rxq) +{ +struct nlattr *nest; +int type; + + type = rxq ?NETDEV_A_QUEUE_IO_URING :NETDEV_A_PAGE_POOL_IO_URING ; + nest =nla_nest_start (rsp, type); +if (!nest) +return -EMSGSIZE ; +nla_nest_end (rsp, nest); + +return 0 ; +} + +static void io_pp_uninstall (void *mp_priv,struct netdev_rx_queue *rxq) +{ +struct pp_memory_provider_params *p = &rxq->mp_params; +struct io_zcrx_ifq *ifq = mp_priv; + +io_zcrx_drop_netdev (ifq); + p->mp_ops =NULL ; + p->mp_priv =NULL ; +} + +static const struct memory_provider_ops io_uring_pp_zc_ops = { + .alloc_netmems = io_pp_zc_alloc_netmems, + .release_netmem = io_pp_zc_release_netmem, + .init = io_pp_zc_init, + .destroy = io_pp_zc_destroy, + .nl_fill = io_pp_nl_fill, + .uninstall = io_pp_uninstall, +};
io_uring/zcrx.h +3 -0
@@ -9,6 +9,7 @@struct io_zcrx_area {struct net_iov_area nia;struct io_zcrx_ifq *ifq; +atomic_t *user_refs;u16 area_id;struct page **pages;
@@ -26,6 +27,8 @@ struct io_zcrx_ifq {struct io_uring *rq_ring;struct io_uring_zcrx_rqe *rqes;u32 rq_entries; +u32 cached_rq_head; +spinlock_t rq_lock;u32 if_rxq;struct device *dev;
Signature · 1 line
2.43.5
PATCH v14 5/11 io_uring/zcrx: dma-map area for the device
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
Setup DMA mappings for the area into which we intend to receive data later on. We know the device we want to attach to even before we get a page pool and can pre-map in advance. All net_iov are synchronised for device when allocated, see page_pool_mp_return_in_cache().
--- io_uring/zcrx.c | 82 ++++++++++++++++++++++++++++++++++++++++++++++++- io_uring/zcrx.h | 1 + 2 files changed, 82 insertions(+), 1 deletion(-)
io_uring/zcrx.c +81 -1
@@ -1,6 +1,7 @@// SPDX-License-Identifier: GPL-2.0 #include <linux/kernel.h> #include <linux/errno.h> +#include <linux/dma-map-ops.h> #include <linux/mm.h> #include <linux/nospec.h> #include <linux/io_uring.h>
@@ -21,6 +22,73 @@#include "zcrx.h" #include "rsrc.h" +#define IO_DMA_ATTR (DMA_ATTR_SKIP_CPU_SYNC | DMA_ATTR_WEAK_ORDERING) + +static void __io_zcrx_unmap_area (struct io_zcrx_ifq *ifq, +struct io_zcrx_area *area,int nr_mapped) +{ +int i; + +for (i =0 ; i < nr_mapped; i++) { +struct net_iov *niov = &area->nia.niovs[i]; +dma_addr_t dma; + + dma =page_pool_get_dma_addr_netmem (net_iov_to_netmem (niov)); +dma_unmap_page_attrs (ifq->dev, dma,PAGE_SIZE , +DMA_FROM_DEVICE ,IO_DMA_ATTR ); +net_mp_niov_set_dma_addr (niov,0 ); + } +} + +static void io_zcrx_unmap_area (struct io_zcrx_ifq *ifq,struct io_zcrx_area *area) +{ +if (area->is_mapped) +__io_zcrx_unmap_area (ifq, area, area->nia.num_niovs); +} + +static int io_zcrx_map_area (struct io_zcrx_ifq *ifq,struct io_zcrx_area *area) +{ +int i; + +for (i =0 ; i < area->nia.num_niovs; i++) { +struct net_iov *niov = &area->nia.niovs[i]; +dma_addr_t dma; + + dma =dma_map_page_attrs (ifq->dev, area->pages[i],0 ,PAGE_SIZE , +DMA_FROM_DEVICE ,IO_DMA_ATTR ); +if (dma_mapping_error (ifq->dev, dma)) +break ; +if (net_mp_niov_set_dma_addr (niov, dma)) { +dma_unmap_page_attrs (ifq->dev, dma,PAGE_SIZE , +DMA_FROM_DEVICE ,IO_DMA_ATTR ); +break ; + } + } + +if (i != area->nia.num_niovs) { +__io_zcrx_unmap_area (ifq, area, i); +return -EINVAL ; + } + + area->is_mapped = true; +return 0 ; +} + +static void io_zcrx_sync_for_device (const struct page_pool *pool, +struct net_iov *niov) +{ +#if defined(CONFIG_HAS_DMA ) && defined(CONFIG_DMA_NEED_SYNC ) +dma_addr_t dma_addr; + +if (!dma_dev_need_sync (pool->p.dev)) +return ; + + dma_addr =page_pool_get_dma_addr_netmem (net_iov_to_netmem (niov)); +__dma_sync_single_for_device (pool->p.dev, dma_addr + pool->p.offset, +PAGE_SIZE , pool->p.dma_dir); +#endif +} +#define IO_RQ_MAX_ENTRIES 32768__maybe_unused
@@ -83,6 +151,8 @@ static void io_free_rbuf_ring(struct io_zcrx_ifq *ifq)static void io_zcrx_free_area (struct io_zcrx_area *area) { +io_zcrx_unmap_area (area->ifq, area); +kvfree (area->freelist);kvfree (area->nia.niovs);kvfree (area->user_refs);
@@ -272,6 +342,10 @@ int io_register_zcrx_ifq(struct io_ring_ctx *ctx,return -EOPNOTSUPP ;get_device (ifq->dev); + ret =io_zcrx_map_area (ifq, ifq->area); +if (ret) + goto err; + reg.offsets.rqes =sizeof (struct io_uring ); reg.offsets.head = offsetof(struct io_uring , head); reg.offsets.tail = offsetof(struct io_uring , tail);
@@ -422,6 +496,7 @@ static void io_zcrx_ring_refill(struct page_pool *pp,continue ; } +io_zcrx_sync_for_device (pp, niov);net_mp_netmem_place_in_cache (pp, netmem); }while (--entries);
@@ -439,6 +514,7 @@ static void io_zcrx_refill_slow(struct page_pool *pp, struct io_zcrx_ifq *ifq)netmem_ref netmem =net_iov_to_netmem (niov);net_mp_niov_set_page_pool (pp, niov); +io_zcrx_sync_for_device (pp, niov);net_mp_netmem_place_in_cache (pp, netmem); }spin_unlock_bh (&area->freelist_lock);
@@ -482,10 +558,14 @@ static int io_pp_zc_init(struct page_pool *pp)if (WARN_ON_ONCE (!ifq))return -EINVAL ; -if (pp->dma_map) +if (WARN_ON_ONCE (ifq->dev != pp->p.dev)) +return -EINVAL ; +if (WARN_ON_ONCE (!pp->dma_map))return -EOPNOTSUPP ;if (pp->p.order !=0 )return -EOPNOTSUPP ; +if (pp->p.dma_dir !=DMA_FROM_DEVICE ) +return -EOPNOTSUPP ;percpu_ref_get (&ifq->ctx->refs);return 0 ;
io_uring/zcrx.h +1 -0
@@ -11,6 +11,7 @@ struct io_zcrx_area {struct io_zcrx_ifq *ifq;atomic_t *user_refs; +bool is_mapped;u16 area_id;struct page **pages;
Signature · 1 line
2.43.5
PATCH v14 6/11 io_uring/zcrx: add io_recvzc request
In reply to David WeiAdd io_uring opcode OP_RECV_ZC for doing zero copy reads out of a socket. Only the connection should be land on the specific rx queue set up for zero copy, and the socket must be handled by the io_uring instance that the rx queue was registered for zero copy with. That's because neither net_iovs / buffers from our queue can be read by outside applications, nor zero copy is possible if traffic for the zero copy connection goes to another queue. This coordination is outside of the scope of this patch series. Also, any traffic directed to the zero copy enabled queue is immediately visible to the application, which is why CAP_NET_ADMIN is required at the registration step.
Of course, no data is actually read out of the socket, it has already been copied by the netdev into userspace memory via DMA. OP_RECV_ZC reads skbs out of the socket and checks that its frags are indeed net_iovs that belong to io_uring. A cqe is queued for each one of these frags.
Recall that each cqe is a big cqe, with the top half being an io_uring_zcrx_cqe. The cqe res field contains the len or error. The lower IORING_ZCRX_AREA_SHIFT bits of the struct io_uring_zcrx_cqe::off field contain the offset relative to the start of the zero copy area. The upper part of the off field is trivially zero, and will be used to carry the area id.
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. This request always operates in multishot mode.
--- include/uapi/linux/io_uring.h | 2 + io_uring/io_uring.h | 10 ++ io_uring/net.c | 72 +++++++++++++ io_uring/opdef.c | 16 +++ io_uring/zcrx.c | 190 +++++++++++++++++++++++++++++++++- io_uring/zcrx.h | 13 +++ 6 files changed, 302 insertions(+), 1 deletion(-)
include/uapi/linux/io_uring.h +2 -0
@@ -87,6 +87,7 @@ struct io_uring_sqe {union {__s32 splice_fd_in;__u32 file_index; +__u32 zcrx_ifq_idx;__u32 optlen;struct {__u16 addr_len;
@@ -278,6 +279,7 @@ enum io_uring_op {IORING_OP_FTRUNCATE ,IORING_OP_BIND ,IORING_OP_LISTEN , +IORING_OP_RECV_ZC ,/* this goes last, obviously */ IORING_OP_LAST,
io_uring/io_uring.h +10 -0
@@ -185,6 +185,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.cq_flush = 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 +72 -0
90 lines not on this page · raw message
io_uring/opdef.c +16 -0
@@ -37,6 +37,7 @@#include "waitid.h" #include "futex.h" #include "truncate.h" +#include "zcrx.h" static int io_no_issue (struct io_kiocb *req,unsigned int issue_flags) {
@@ -514,6 +515,18 @@ const struct io_issue_def io_issue_defs[] = { .async_size = sizeof(struct io_async_msghdr), #else .prep = io_eopnotsupp_prep, +#endif + }, + [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 }, };
@@ -745,6 +758,9 @@ const struct io_cold_def io_cold_defs[] = { [IORING_OP_LISTEN ] = { .name ="LISTEN" , }, + [IORING_OP_RECV_ZC ] = { + .name ="RECV_ZC" , + }, };const char *io_uring_get_opcode (u8 opcode)
io_uring/zcrx.c +189 -1
211 lines not on this page · raw message
io_uring/zcrx.h +13 -0
@@ -3,6 +3,7 @@#define IOU_ZC_RX_H #include <linux/io_uring_types.h> +#include <linux/socket.h> #include <net/page_pool/types.h> #include <net/net_trackers.h>
@@ -43,6 +44,9 @@ int io_register_zcrx_ifq(struct io_ring_ctx *ctx, struct io_uring_zcrx_ifq_reg __user *arg);void io_unregister_zcrx_ifqs (struct io_ring_ctx *ctx);void io_shutdown_zcrx_ifqs (struct io_ring_ctx *ctx); +int io_zcrx_recv (struct io_kiocb *req,struct io_zcrx_ifq *ifq, +struct socket *sock,unsigned int flags, +unsigned issue_flags);#else static inline int io_register_zcrx_ifq (struct io_ring_ctx *ctx,struct io_uring_zcrx_ifq_reg __user *arg)
@@ -55,6 +59,15 @@ static inline void io_unregister_zcrx_ifqs(struct io_ring_ctx *ctx)static inline void io_shutdown_zcrx_ifqs (struct io_ring_ctx *ctx) { } +static inline int io_zcrx_recv (struct io_kiocb *req,struct io_zcrx_ifq *ifq, +struct socket *sock,unsigned int flags, +unsigned issue_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.5
PATCH v14 7/11 io_uring/zcrx: set pp memory provider for an rx queue
In reply to David WeiSet the page pool memory provider for the rx queue configured for zero copy to io_uring. Then the rx queue is reset using netdev_rx_queue_restart() and netdev core + page pool will take care of filling the rx queue from the io_uring zero copy memory provider.
For now, there is only one ifq so its destruction happens implicitly during io_uring cleanup.
--- io_uring/zcrx.c | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-)
io_uring/zcrx.c +41 -8
@@ -11,11 +11,12 @@#include <net/page_pool/helpers.h> #include <net/page_pool/memory_provider.h> #include <net/netlink.h> - -#include <trace/events/page_pool.h> +#include <net/netdev_rx_queue.h> #include <net/tcp.h> #include <net/rps.h> +#include <trace/events/page_pool.h> +#include <uapi/linux/io_uring.h> #include "io_uring.h"
@@ -275,8 +276,34 @@ static void io_zcrx_drop_netdev(struct io_zcrx_ifq *ifq)spin_unlock (&ifq->lock); } +static void io_close_queue (struct io_zcrx_ifq *ifq) +{ +struct net_device *netdev; +netdevice_tracker netdev_tracker; +struct pp_memory_provider_params p = { + .mp_ops = &io_uring_pp_zc_ops, + .mp_priv = ifq, + }; + +if (ifq->if_rxq ==-1 ) +return ; + +spin_lock (&ifq->lock); + netdev = ifq->netdev; + netdev_tracker = ifq->netdev_tracker; + ifq->netdev =NULL ; +spin_unlock (&ifq->lock); + +if (netdev) { +net_mp_close_rxq (netdev, ifq->if_rxq, &p); +netdev_put (netdev, &netdev_tracker); + } + ifq->if_rxq =-1 ; +} +static void io_zcrx_ifq_free (struct io_zcrx_ifq *ifq) { +io_close_queue (ifq);io_zcrx_drop_netdev (ifq);if (ifq->area)
@@ -291,6 +318,7 @@ static void io_zcrx_ifq_free(struct io_zcrx_ifq *ifq)int io_register_zcrx_ifq (struct io_ring_ctx *ctx,struct io_uring_zcrx_ifq_reg __user *arg) { +struct pp_memory_provider_params mp_param = {};struct io_uring_zcrx_area_reg area;struct io_uring_zcrx_ifq_reg reg;struct io_uring_region_desc rd;
@@ -341,7 +369,6 @@ int io_register_zcrx_ifq(struct io_ring_ctx *ctx, goto err; ifq->rq_entries = reg.rq_entries; - ifq->if_rxq = reg.if_rxq; ret = -ENODEV ; ifq->netdev = netdev_get_by_index(current->nsproxy->net_ns, reg.if_idx,
@@ -358,16 +385,20 @@ int io_register_zcrx_ifq(struct io_ring_ctx *ctx,if (ret) goto err; + mp_param.mp_ops = &io_uring_pp_zc_ops; + mp_param.mp_priv = ifq; + ret =net_mp_open_rxq (ifq->netdev, reg.if_rxq, &mp_param); +if (ret) + goto err; + ifq->if_rxq = reg.if_rxq; + reg.offsets.rqes =sizeof (struct io_uring ); reg.offsets.head = offsetof(struct io_uring , head); reg.offsets.tail = offsetof(struct io_uring , tail);if (copy_to_user (arg, ®,sizeof (reg)) || -copy_to_user (u64_to_user_ptr (reg.region_ptr), &rd,sizeof (rd))) { - ret = -EFAULT ; - goto err; - } -if (copy_to_user (u64_to_user_ptr (reg.area_ptr), &area,sizeof (area))) { +copy_to_user (u64_to_user_ptr (reg.region_ptr), &rd,sizeof (rd)) || +copy_to_user (u64_to_user_ptr (reg.area_ptr), &area,sizeof (area))) { ret = -EFAULT ; goto err; }
@@ -444,6 +475,8 @@ void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx)if (ctx->ifq)io_zcrx_scrub (ctx->ifq); + +io_close_queue (ctx->ifq); }static inline u32 io_zcrx_rqring_entries (struct io_zcrx_ifq *ifq)
Signature · 1 line
2.43.5
Re: [PATCH v14 07/11] io_uring/zcrx: set pp memory provider for an rx queue
Op 15-02-2025 om 01:09 schreef David Wei:
Quoted 20 lines
Set the page pool memory provider for the rx queue configured for zero copy to
io_uring. Then the rx queue is reset usingnetdev_rx_queue_restart()and netdev core + page pool will take care of filling the rx queue from theio_uringzero copy memory provider.For now, there is only one ifq so its destruction happens implicitly during
io_uringcleanup.Reviewed-by Jens Axboe <[email protected]>Signed-off-by David Wei <[email protected]>--- io_uring/zcrx.c | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-)io_uring/zcrx.c +0
-0
io_uring/zcrx.c void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx) · line 479
Quoted 5 lines
io_uring/zcrx.c +2
-0@@ -444,6 +475,8 @@ void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx)if (ctx->ifq)io_zcrx_scrub (ctx->ifq); + +io_close_queue (ctx->ifq);
If ctx->ifq is NULL (which seems to be not unlikely given the if statement above) then you'll get a NULL pointer dereference in io_close_queue().
Quoted 3 lines
io_uring/zcrx.c +0
-0}static inline u32 io_zcrx_rqring_entries (struct io_zcrx_ifq *ifq)
Re: [PATCH v14 07/11] io_uring/zcrx: set pp memory provider for an rx queue
Quoted 15 lines · Kees Bakker
Op 15-02-2025 om 01:09 schreef David Wei:
Quoted 14 lines
Set the page pool memory provider for the rx queue configured for zero copy to
io_uring. Then the rx queue is reset usingnetdev_rx_queue_restart()and netdev core + page pool will take care of filling the rx queue from theio_uringzero copy memory provider.For now, there is only one ifq so its destruction happens implicitly during
io_uringcleanup.Reviewed-by Jens Axboe <[email protected]>Signed-off-by David Wei <[email protected]>--- io_uring/zcrx.c | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-)
Quoted 12 lines
io_uring/zcrx.c void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx) · line 478
Quoted 10 lines
io_uring/zcrx.c +2
-0@@ -444,6 +475,8 @@ void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx)if (ctx->ifq)io_zcrx_scrub (ctx->ifq); + +io_close_queue (ctx->ifq);If
ctx->ifqis NULL (which seems to be not unlikely given the if statement above) then you'll get a NULL pointer dereference inio_close_queue().
The only caller of io_shutdown_zcrx_ifqs() is io_ring_exit_work() which checks for ctx->ifq first. That does mean the ctx->ifq check is redundant in this function though.
Quoted 3 lines
Quoted 2 lines
io_uring/zcrx.c +0
-0}static inline u32 io_zcrx_rqring_entries (struct io_zcrx_ifq *ifq)
Pavel Begunkov
maintainerRe: [PATCH v14 07/11] io_uring/zcrx: set pp memory provider for an rx queue
Quoted 29 lines · David Wei
Quoted 15 lines · Kees Bakker
Op 15-02-2025 om 01:09 schreef David Wei:
Quoted 14 lines
Set the page pool memory provider for the rx queue configured for zero copy to
io_uring. Then the rx queue is reset usingnetdev_rx_queue_restart()and netdev core + page pool will take care of filling the rx queue from theio_uringzero copy memory provider.For now, there is only one ifq so its destruction happens implicitly during
io_uringcleanup.Reviewed-by Jens Axboe <[email protected]>Signed-off-by David Wei <[email protected]>--- io_uring/zcrx.c | 49 +++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-)Quoted 12 lines
io_uring/zcrx.c void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx) · line 478
Quoted 10 lines
io_uring/zcrx.c +2
-0@@ -444,6 +475,8 @@ void io_shutdown_zcrx_ifqs(struct io_ring_ctx *ctx)if (ctx->ifq)io_zcrx_scrub (ctx->ifq); + +io_close_queue (ctx->ifq);If
ctx->ifqis NULL (which seems to be not unlikely given the if statement above) then you'll get a NULL pointer dereference inio_close_queue().
Quoted 3 lines
The only caller of
io_shutdown_zcrx_ifqs()isio_ring_exit_work()which checks forctx->ifqfirst. That does mean thectx->ifqcheck is redundant in this function though.
And despite the check being outside of the lock, it's even well synchronised, but in any case we should just make it saner. Thanks for letting know
Signature · 1 line
Pavel Begunkov
PATCH v14 8/11 io_uring/zcrx: throttle receive requests
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
io_zc_rx_tcp_recvmsg() continues until it fails or there is nothing to receive. If the other side sends fast enough, we might get stuck in io_zc_rx_tcp_recvmsg() producing more and more CQEs but not letting the user to handle them leading to unbound latencies.
Break out of it based on an arbitrarily chosen limit, the upper layer will either return to userspace or requeue the request.
--- io_uring/net.c | 2 ++ io_uring/zcrx.c | 9 +++++++++ 2 files changed, 11 insertions(+)
io_uring/net.c +2 -0
@@ -1285,6 +1285,8 @@ int io_recvzc(struct io_kiocb *req, unsigned int issue_flags)if (unlikely (ret <=0 ) && ret != -EAGAIN ) {if (ret == -ERESTARTSYS ) ret = -EINTR ; +if (ret ==IOU_REQUEUE ) +return IOU_REQUEUE ;req_set_fail (req);io_req_set_res (req, ret,0 );
io_uring/zcrx.c +9 -0
@@ -94,10 +94,13 @@ static void io_zcrx_sync_for_device(const struct page_pool *pool,#define IO_RQ_MAX_ENTRIES 32768 +#define IO_SKBS_PER_CALL_LIMIT 20 +struct io_zcrx_args {struct io_kiocb *req;struct io_zcrx_ifq *ifq;struct socket *sock; +unsigned nr_skbs; };static const struct memory_provider_ops io_uring_pp_zc_ops;
@@ -720,6 +723,9 @@ io_zcrx_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,int i, copy, end, off;int ret =0 ; +if (unlikely (args->nr_skbs++ >IO_SKBS_PER_CALL_LIMIT )) +return -EAGAIN ; + start =skb_headlen (skb); start_off = offset;
@@ -810,6 +816,9 @@ static int io_zcrx_tcp_recvmsg(struct io_kiocb *req, struct io_zcrx_ifq *ifq, ret = -ENOTCONN ; else ret = -EAGAIN ; + }else if (unlikely (args.nr_skbs >IO_SKBS_PER_CALL_LIMIT ) && + (issue_flags &IO_URING_F_MULTISHOT )) { + ret =IOU_REQUEUE ; }else if (sock_flag (sk,SOCK_DONE )) {/* Make it to retry until it finally gets 0. */ if (issue_flags &IO_URING_F_MULTISHOT )
Signature · 1 line
2.43.5
PATCH v14 9/11 io_uring/zcrx: add copy fallback
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
There are scenarios in which the zerocopy path can get a kernel buffer instead of a net_iov and needs to copy it to the user, whether it is because of mis-steering or simply getting an skb with the linear part. In this case, grab a net_iov, copy into it and return it to the user as normally.
At the moment the user doesn't get any indication whether there was a copy or not, which is left for follow up work.
--- io_uring/zcrx.c | 120 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 114 insertions(+), 6 deletions(-)
io_uring/zcrx.c +114 -6
@@ -7,6 +7,7 @@#include <linux/io_uring.h> #include <linux/netdevice.h> #include <linux/rtnetlink.h> +#include <linux/skbuff_ref.h> #include <net/page_pool/helpers.h> #include <net/page_pool/memory_provider.h>
@@ -134,6 +135,13 @@ static void io_zcrx_get_niov_uref(struct net_iov *niov)atomic_inc (io_get_user_counter (niov)); } +static inline struct page *io_zcrx_iov_page (const struct net_iov *niov) +{ +struct io_zcrx_area *area =io_zcrx_iov_to_area (niov); + +return area->pages[net_iov_idx (niov)]; +} +static int io_allocate_rbuf_ring (struct io_zcrx_ifq *ifq,struct io_uring_zcrx_ifq_reg *reg,struct io_uring_region_desc *rd)
@@ -448,6 +456,11 @@ static void io_zcrx_return_niov(struct net_iov *niov) {netmem_ref netmem =net_iov_to_netmem (niov); +if (!niov->pp) { +/* copy fallback allocated niovs */ +io_zcrx_return_niov_freelist (niov); +return ; + }page_pool_put_unrefed_netmem (niov->pp, netmem,-1 , false); }
@@ -686,13 +699,93 @@ static bool io_zcrx_queue_cqe(struct io_kiocb *req, struct net_iov *niov,return true; } +static struct net_iov *io_zcrx_alloc_fallback (struct io_zcrx_area *area) +{ +struct net_iov *niov =NULL ; + +spin_lock_bh (&area->freelist_lock); +if (area->free_count) + niov =__io_zcrx_get_free_niov (area); +spin_unlock_bh (&area->freelist_lock); + +if (niov) +page_pool_fragment_netmem (net_iov_to_netmem (niov),1 ); +return niov; +} + +static ssize_t io_zcrx_copy_chunk (struct io_kiocb *req,struct io_zcrx_ifq *ifq, +void *src_base,struct page *src_page, +unsigned int src_offset,size_t len) +{ +struct io_zcrx_area *area = ifq->area; +size_t copied =0 ; +int ret =0 ; + +while (len) { +size_t copy_size =min_t (size_t,PAGE_SIZE , len); +const int dst_off =0 ; +struct net_iov *niov; +struct page *dst_page; +void *dst_addr; + + niov =io_zcrx_alloc_fallback (area); +if (!niov) { + ret = -ENOMEM ; +break ; + } + + dst_page =io_zcrx_iov_page (niov); + dst_addr =kmap_local_page (dst_page); +if (src_page) + src_base =kmap_local_page (src_page); + +memcpy (dst_addr, src_base + src_offset, copy_size); + +if (src_page) +kunmap_local (src_base); +kunmap_local (dst_addr); + +if (!io_zcrx_queue_cqe (req, niov, ifq, dst_off, copy_size)) { +io_zcrx_return_niov (niov); + ret = -ENOSPC ; +break ; + } + +io_zcrx_get_niov_uref (niov); + src_offset += copy_size; + len -= copy_size; + copied += copy_size; + } + +return copied ? copied : ret; +} + +static int io_zcrx_copy_frag (struct io_kiocb *req,struct io_zcrx_ifq *ifq, +const skb_frag_t *frag,int off,int len) +{ +struct page *page =skb_frag_page (frag); +u32 p_off, p_len, t, copied =0 ; +int ret =0 ; + + off +=skb_frag_off (frag); + +skb_frag_foreach_page (frag, off, len, + page, p_off, p_len, t) { + ret =io_zcrx_copy_chunk (req, ifq,NULL , page, p_off, p_len); +if (ret <0 ) +return copied ? copied : ret; + copied += ret; + } +return copied; +} +static int io_zcrx_recv_frag (struct io_kiocb *req,struct io_zcrx_ifq *ifq,const skb_frag_t *frag,int off,int len) {struct net_iov *niov;if (unlikely (!skb_frag_is_net_iov (frag))) -return -EOPNOTSUPP ; +return io_zcrx_copy_frag (req, ifq, frag, off, len); niov =netmem_to_net_iov (frag->netmem); if (niov->pp->mp_ops != &io_uring_pp_zc_ops ||
@@ -719,18 +812,33 @@ io_zcrx_recv_skb(read_descriptor_t *desc, struct sk_buff *skb,struct io_zcrx_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 ;if (unlikely (args->nr_skbs++ >IO_SKBS_PER_CALL_LIMIT ))return -EAGAIN ; - 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 =io_zcrx_copy_chunk (req, ifq, skb->data,NULL , + 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.5
PATCH v14 10/11 net: add documentation for io_uring zcrx
In reply to David WeiAdd documentation for io_uring zero copy Rx that explains requirements and the user API.
--- Documentation/networking/index.rst | 1 + Documentation/networking/iou-zcrx.rst | 202 ++++++++++++++++++++++++++ 2 files changed, 203 insertions(+) create mode 100644 Documentation/networking/iou-zcrx.rst
Documentation/networking/index.rst +1 -0
@@ -63,6 +63,7 @@ Contents: gtp ila ioam6-sysctl + iou-zcrx ip_dynaddr ipsec ip-sysctl
Documentation/networking/iou-zcrx.rst new file +202 -0
202 lines not on this page · raw message
Signature · 1 line
2.43.5
PATCH v14 11/11 io_uring/zcrx: add selftest
In reply to David WeiAdd a selftest for io_uring zero copy Rx. This test cannot run locally and requires a remote host to be configured in net.config. The remote host must have hardware support for zero copy Rx as listed in the documentation page. The test will restore the NIC config back to before the test and is idempotent.
liburing is required to compile the test and be installed on the remote host running the test.
--- .../selftests/drivers/net/hw/.gitignore | 2 + .../testing/selftests/drivers/net/hw/Makefile | 5 + .../selftests/drivers/net/hw/iou-zcrx.c | 426 ++++++++++++++++++ .../selftests/drivers/net/hw/iou-zcrx.py | 64 +++ 4 files changed, 497 insertions(+) create mode 100644 tools/testing/selftests/drivers/net/hw/iou-zcrx.c create mode 100755 tools/testing/selftests/drivers/net/hw/iou-zcrx.py
tools/testing/selftests/drivers/net/hw/.gitignore +2 -0
@@ -1 +1,3 @@ +# SPDX-License-Identifier: GPL-2.0-only +iou-zcrx ncdevmem
tools/testing/selftests/drivers/net/hw/Makefile +5 -0
@@ -1,5 +1,7 @@ # SPDX-License-Identifier: GPL-2.0+ OR MIT +TEST_GEN_FILES = iou-zcrx + TEST_PROGS = \ csum.py \ devlink_port_split.py \
@@ -10,6 +12,7 @@ TEST_PROGS = \ ethtool_rmon.sh \ hw_stats_l3.sh \ hw_stats_l3_gre.sh \ + iou-zcrx.py \ loopback.sh \ nic_link_layer.py \ nic_performance.py \
@@ -38,3 +41,5 @@ include ../../../lib.mk # YNL build YNL_GENS := ethtool netdev include ../../../net/ynl.mk + +$(OUTPUT)/iou-zcrx: LDLIBS += -luring
tools/testing/selftests/drivers/net/hw/iou-zcrx.c new file +426 -0
426 lines not on this page · raw message
tools/testing/selftests/drivers/net/hw/iou-zcrx.py new file +64 -0
64 lines not on this page · raw message
Signature · 1 line
2.43.5
Quoted 3 lines · On Fri, 14 Feb 2025 16:09:35 -0800 David Wei wrote:
This patchset contains
io_uringpatches needed by a newio_uringrequest implementing zero copy rx into userspace pages, eliminating a kernel to user copy.
Quoted 12 lines · David Wei
This patchset contains
io_uringpatches needed by a newio_uringrequest implementing zero copy rx into userspace pages, eliminating a kernel to user copy.We configure a page pool that a driver uses to fill a hw rx queue to hand out user pages instead of kernel pages. Any data that ends up hitting this hw rx queue will thus be dma'd into userspace memory directly, without needing to be bounced through kernel memory. 'Reading' data out of a socket instead becomes a _notification_ mechanism, where the kernel tells userspace where the data is. The overall approach is similar to the devmem TCP proposal.
Quoted 1 line
[...]
Applied, thanks!
[01/11] io_uring/zcrx: add interface queue and refill queue
commit: 6f377873cb23905009759b7366b9fe85c2a6ff37
[02/11] io_uring/zcrx: add io_zcrx_area
commit: cf96310c5f9a0d542db99c887742811425ba2ec0
[03/11] io_uring/zcrx: grab a net device
commit: 035af94b39fd13751abf5f0a2948c9eddede55d0
[04/11] io_uring/zcrx: implement zerocopy receive pp memory provider
commit: 34a3e60821ab9f335a58d43a88cccdbefdebdec3
[05/11] io_uring/zcrx: dma-map area for the device
commit: db070446f5af8c7a384b89367a10cddbf5498717
[06/11] io_uring/zcrx: add io_recvzc request
commit: 11ed914bbf948c4a37248f2876973ac18014056d
[07/11] io_uring/zcrx: set pp memory provider for an rx queue
commit: e0793de24a9f610bd8ce106f7033b3966e7fca0e
[08/11] io_uring/zcrx: throttle receive requests
commit: 931dfae19032d13266cf1fac080cec66469a2042
[09/11] io_uring/zcrx: add copy fallback
commit: bc57c7d36c4c9c352ed13d98a4f1e4dc27919d6a
[10/11] net: add documentation for io_uring zcrx
commit: d9ac1d5fc9510a170eb43e8c129b8e1cd5e1c3e1
[11/11] io_uring/zcrx: add selftest
commit: 71082faa2c648a2adc1167b37565e195a8df1bc7Best regards,
Signature · 1 line
Jens Axboe