Zero copy network RX using io_uring
Subsystems (5)
cover Zero copy network RX using io_uring
From: David Wei <[email protected]>
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.
Software support is added to the Broadcom BNXT driver. Hardware support for receive flow steering and header splitting is required.
On the userspace side, a sample server is added in this branch of liburing:
https://github.com/spikeh/liburing/tree/zcrx2
Build liburing as normal, and run examples/zcrx. Then, set flow steering rules using ethtool. A sample shell script is included in examples/zcrx_flow.sh, but you need to change the source IP. Finally, connect a client using e.g. netcat and send data.
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.
Mina et al. from Google and Kuba are collaborating on a similar proposal to ZC from NIC to devmem. There are many shared functionality in netdev that we can collaborate on e.g.:
* Page pool memory provider backend and resource registration
* Page pool refcounted iov/buf representation and lifecycle
* Setting receive flow steering
As mentioned earlier, this is an early prototype. It is brittle, some functionality is missing and there's little optimisation. We're looking for feedback on the overall approach and points of collaboration in netdev.
* No copy fallback, if payload ends up in linear part of skb then the code will not work
* No way to pin an RX queue to a specific CPU
* Only one ifq, one pool region, on RX queue...
This patchset is based on the work by Jonathan Lemon <[email protected]>:
https://lore.kernel.org/io-uring/[email protected]/
David Wei (11):
io_uring: add 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 ZC buf and pool
io_uring: add ZC pool API
skbuff: add SKBFL_FIXED_FRAG and skb_fixed()
io_uring: allocate a uarg for freeing zero copy skbs
io_uring: delay ZC pool destruction
netdev/bnxt: add data pool and use it in BNXT driver
io_uring: add io_recvzc request
drivers/net/ethernet/broadcom/bnxt/bnxt.c | 59 +- drivers/net/ethernet/broadcom/bnxt/bnxt.h | 4 + drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c | 3 + include/linux/io_uring.h | 31 + include/linux/io_uring_types.h | 6 + include/linux/netdevice.h | 11 + include/linux/skbuff.h | 10 +- include/net/data_pool.h | 96 +++ include/uapi/linux/io_uring.h | 53 ++ io_uring/Makefile | 3 +- io_uring/io_uring.c | 13 + io_uring/kbuf.c | 30 + io_uring/kbuf.h | 5 + io_uring/net.c | 83 +- io_uring/opdef.c | 16 + io_uring/zc_rx.c | 723 ++++++++++++++++++ io_uring/zc_rx.h | 42 + 17 files changed, 1168 insertions(+), 20 deletions(-) create mode 100644 include/net/data_pool.h create mode 100644 io_uring/zc_rx.c create mode 100644 io_uring/zc_rx.h
Signature · 1 line
2.39.3
PATCH 1/11 io_uring: add interface queue
From: 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 written to.
* A net device and one specific RX queue in it that will be configured for ZC RX.
* A pair of shared ringbuffers w/ userspace, dubbed registered buf (rbuf) rings. Each entry contains a pool region id and an offset + len within that region. The kernel writes entries into the completion ring to tell userspace where RX data is relative to the start of a region. Userspace writes entries into the refill ring to tell the kernel when it is done with the data.
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 to io_uring_register that sets up an ifq. Size and offsets of shared ringbuffers are returned to userspace for it to mmap. The implementation will be added in a later patch.
--- include/linux/io_uring_types.h | 6 ++++ include/uapi/linux/io_uring.h | 50 ++++++++++++++++++++++++++ io_uring/Makefile | 3 +- io_uring/io_uring.c | 7 ++++ io_uring/kbuf.c | 30 ++++++++++++++++ io_uring/kbuf.h | 5 +++ io_uring/zc_rx.c | 65 ++++++++++++++++++++++++++++++++++ io_uring/zc_rx.h | 21 +++++++++++ 8 files changed, 186 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 +6 -0
@@ -151,6 +151,10 @@ struct io_rings {struct io_uring_cqe cqes[]____cacheline_aligned_in_smp ; }; +struct io_rbuf_ring { +struct io_uring rq, cq; +}; +struct io_restriction { DECLARE_BITMAP(register_op, IORING_REGISTER_LAST); DECLARE_BITMAP(sqe_op, IORING_OP_LAST);
@@ -330,6 +334,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 +50 -0
62 lines not on this page · raw message
io_uring/Makefile +2 -1
@@ -7,5 +7,6 @@ obj-$(CONFIG_IO_URING) += io_uring.o xattr.o nop.o fs.o splice.o \ openclose.o uring_cmd.o epoll.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 + cancel.o kbuf.o rsrc.o rw.o opdef.o \ + notif.o zc_rx.o obj-$(CONFIG_IO_WQ) += io-wq.o
io_uring/io_uring.c +7 -0
@@ -92,6 +92,7 @@#include "cancel.h" #include "net.h" #include "notif.h" +#include "zc_rx.h" #include "timeout.h" #include "poll.h"
@@ -4418,6 +4419,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,break ; ret =io_register_file_alloc_range (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/kbuf.c +30 -0
@@ -634,3 +634,33 @@ void *io_pbuf_get_address(struct io_ring_ctx *ctx, unsigned long bgid)return bl->buf_ring; } + +int io_allocate_rbuf_ring (struct io_zc_rx_ifq *ifq, +struct io_uring_zc_rx_ifq_reg *reg) +{ +gfp_t gfp =GFP_KERNEL_ACCOUNT | __GFP_ZERO | __GFP_NOWARN | __GFP_COMP; +size_t off, size, rq_size, cq_size; +void *ptr; + + off =sizeof (struct io_rbuf_ring ); + rq_size = reg->rq_entries *sizeof (struct io_uring_rbuf_rqe ); + cq_size = reg->cq_entries *sizeof (struct io_uring_rbuf_cqe ); + size = off + rq_size + cq_size; + ptr = (void *)__get_free_pages (gfp,get_order (size)); +if (!ptr) +return -ENOMEM ; + ifq->ring = (struct io_rbuf_ring *)ptr; + ifq->rqes = (struct io_uring_rbuf_rqe *)((char *)ptr + off); + ifq->cqes = (struct io_uring_rbuf_cqe *)((char *)ifq->rqes + rq_size); + +return 0 ; +} + +void io_free_rbuf_ring (struct io_zc_rx_ifq *ifq) +{ +struct page *page; + + page =virt_to_head_page (ifq->ring); +if (put_page_testzero (page)) +free_compound_page (page); +}
io_uring/kbuf.h +5 -0
@@ -4,6 +4,8 @@#include <uapi/linux/io_uring.h> +#include "zc_rx.h" +struct io_buffer_list {/* * If ->buf_nr_pages is set, then buf_pages/buf_ring are used. If not,
@@ -57,6 +59,9 @@ void io_kbuf_recycle_legacy(struct io_kiocb *req, unsigned issue_flags);void *io_pbuf_get_address (struct io_ring_ctx *ctx,unsigned long bgid); +int io_allocate_rbuf_ring (struct io_zc_rx_ifq *ifq,struct io_uring_zc_rx_ifq_reg *reg); +void io_free_rbuf_ring (struct io_zc_rx_ifq *ifq); +static inline void io_kbuf_recycle_ring (struct io_kiocb *req) {/*
io_uring/zc_rx.c new file +65 -0
65 lines not on this page · raw message
io_uring/zc_rx.h new file +21 -0
@@ -0,0 +1,21 @@ +// 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_rbuf_ring *ring; +struct io_uring_rbuf_rqe *rqes; +struct io_uring_rbuf_cqe *cqes; +u32 rq_entries, cq_entries; +void *pool; + +/* hw rx descriptor ring id */ +u32 if_rxq_id; +}; + +int io_register_zc_rx_ifq (struct io_ring_ctx *ctx, +struct io_uring_zc_rx_ifq_reg __user *arg); + +#endif
Signature · 1 line
2.39.3
PATCH 2/11 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 rbuf rings. There are two rings and a struct io_rbuf_ring that contains the head and tail ptrs into each 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_RBUF_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 | 17 +++++++++++++++++ 3 files changed, 24 insertions(+)
include/uapi/linux/io_uring.h +2 -0
@@ -393,6 +393,8 @@ enum {#define IORING_OFF_PBUF_RING 0x80000000ULL#define IORING_OFF_PBUF_SHIFT 16#define IORING_OFF_MMAP_MASK 0xf8000000ULL +#define IORING_OFF_RBUF_RING 0x20000000ULL +#define IORING_OFF_RBUF_SHIFT 16/* * Filled with the offset for mmap(2)
io_uring/io_uring.c +5 -0
@@ -3368,6 +3368,11 @@ static void *io_uring_validate_mmap_request(struct file *file,return ERR_PTR (-EINVAL );break ; } +case IORING_OFF_RBUF_RING : +if (!ctx->ifq) +return ERR_PTR (-EINVAL ); + ptr = ctx->ifq->ring; +break ;default :return ERR_PTR (-EINVAL ); }
io_uring/zc_rx.c +17 -0
@@ -34,6 +34,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, cqes_sz;int ret;if (copy_from_user (®, arg,sizeof (reg)))
@@ -58,6 +59,22 @@ int io_register_zc_rx_ifq(struct io_ring_ctx *ctx, ifq->if_rxq_id = reg.if_rxq_id; ctx->ifq = ifq; + ring_sz =sizeof (struct io_rbuf_ring ); + rqes_sz =sizeof (struct io_uring_rbuf_rqe ) * ifq->rq_entries; + cqes_sz =sizeof (struct io_uring_rbuf_cqe ) * ifq->cq_entries; + reg.mmap_sz = ring_sz + rqes_sz + cqes_sz; + reg.rq_off.rqes = ring_sz; + reg.cq_off.cqes = ring_sz + rqes_sz; + reg.rq_off.head = offsetof(struct io_rbuf_ring, rq.head); + reg.rq_off.tail = offsetof(struct io_rbuf_ring, rq.tail); + reg.cq_off.head = offsetof(struct io_rbuf_ring, cq.head); + reg.cq_off.tail = offsetof(struct io_rbuf_ring, cq.tail); + +if (copy_to_user (arg, ®,sizeof (reg))) { + ret = -EFAULT ; + goto err; + } +return 0 ; err:io_zc_rx_ifq_free (ifq);
Signature · 1 line
2.39.3
PATCH 3/11 netdev: add XDP_SETUP_ZC_RX command
In reply to David WeiFrom: David Wei <[email protected]>
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.
--- include/linux/netdevice.h | 6 ++++++ 1 file changed, 6 insertions(+)
include/linux/netdevice.h +6 -0
@@ -1000,6 +1000,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 ;
@@ -1038,6 +1039,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.39.3
PATCH 4/11 io_uring: setup ZC for an RX queue when registering an ifq
In reply to David WeiFrom: David Wei <[email protected]>
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. The XDP command added in the previous patch is used to enable or disable ZC RX.
For now since there is only one ifq, its destruction is implicit during io_uring cleanup.
--- io_uring/io_uring.c | 1 + io_uring/zc_rx.c | 62 +++++++++++++++++++++++++++++++++++++++++++-- io_uring/zc_rx.h | 1 + 3 files changed, 62 insertions(+), 2 deletions(-)
io_uring/io_uring.c +1 -0
@@ -3098,6 +3098,7 @@ static __cold void io_ring_ctx_wait_and_kill(struct io_ring_ctx *ctx)percpu_ref_kill (&ctx->refs);xa_for_each (&ctx->personalities, index, creds)io_unregister_personality (ctx, index); +io_unregister_zc_rx_ifq (ctx);if (ctx->rings)io_poll_remove_all (ctx,NULL , true);mutex_unlock (&ctx->uring_lock);
io_uring/zc_rx.c +60 -2
110 lines not on this page · raw message
io_uring/zc_rx.h +1 -0
@@ -17,5 +17,6 @@ struct io_zc_rx_ifq {int io_register_zc_rx_ifq (struct io_ring_ctx *ctx,struct io_uring_zc_rx_ifq_reg __user *arg); +int io_unregister_zc_rx_ifq (struct io_ring_ctx *ctx);#endif
Signature · 1 line
2.39.3
PATCH 5/11 io_uring: add ZC buf and pool
In reply to David WeiFrom: David Wei <[email protected]>
This patch adds two objects:
* Zero copy buffer representation, holding a page, its mapped dma_addr, and a refcount for lifetime management.
* Zero copy pool, spiritually similar to page pool, that holds ZC bufs and hands them out to net devices.
The ZC pool is tiered with currently two tiers: a fast lockless cache that should only be accessed from the NAPI context of a single RX queue, and a freelist. When a ZC pool region is first mapped, it is added to the freelist.
During normal operation, bufs are moved from the freelist into the cache in POOL_CACHE_SIZE blocks before being given out.
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.h | 6 ++ io_uring/zc_rx.c | 173 ++++++++++++++++++++++++++++++++++++++- io_uring/zc_rx.h | 1 + 3 files changed, 179 insertions(+), 1 deletion(-)
include/linux/io_uring.h +6 -0
@@ -41,6 +41,12 @@ static inline const void *io_uring_sqe_cmd(const struct io_uring_sqe *sqe)return sqe->cmd; } +struct io_zc_rx_buf { +dma_addr_t dma; +struct page *page; +atomic_t refcount; +}; +#if defined(CONFIG_IO_URING )int io_uring_cmd_import_fixed (u64 ubuf,unsigned long len,int rw,struct iov_iter *iter,void *ioucmd);
io_uring/zc_rx.c +172 -1
204 lines not on this page · raw message
io_uring/zc_rx.h +1 -0
@@ -18,5 +18,6 @@ struct io_zc_rx_ifq {int io_register_zc_rx_ifq (struct io_ring_ctx *ctx,struct io_uring_zc_rx_ifq_reg __user *arg);int io_unregister_zc_rx_ifq (struct io_ring_ctx *ctx); +int io_zc_rx_pool_create (struct io_zc_rx_ifq *ifq,u16 id);#endif
Signature · 1 line
2.39.3
PATCH 6/11 io_uring: add ZC pool API
In reply to David WeiFrom: David Wei <[email protected]>
This patch adds an API to get/put bufs from a ZC pool added in the previous patch.
Recall that there is an rbuf refill ring in an ifq that is shared w/ userspace, which puts bufs it is done with back into it. A new tier is added to the ZC pool that drains entries from the refill ring to put into the cache. So when the cache is empty, it is refilled from the refill ring first, then the freelist.
ZC bufs are refcounted, with both a kref and a uref. Userspace is given an off + len into the entire ZC pool region, not individual pages from ZC bufs. A net device may pack multiple packets into the same page it gets from a ZC buf, so it is possible for the same ZC buf to be handed out to userspace multiple times.
This means it is possible to drain the entire refill ring, and have no usable free bufs. Suggestions for dealing w/ this are very welcome!
Only up to POOL_REFILL_COUNT entries are refilled from the refill ring. Given the above, we may want to limit the amount of work being done since refilling happens inside the NAPI softirq context.
--- include/linux/io_uring.h | 18 ++++++++ io_uring/zc_rx.c | 98 ++++++++++++++++++++++++++++++++++++++++ io_uring/zc_rx.h | 13 ++++++ 3 files changed, 129 insertions(+)
include/linux/io_uring.h +18 -0
@@ -60,6 +60,17 @@ void __io_uring_free(struct task_struct *tsk);void io_uring_unreg_ringfd (void );const char *io_uring_get_opcode (u8 opcode); +struct io_zc_rx_ifq ; +struct io_zc_rx_buf *io_zc_rx_get_buf (struct io_zc_rx_ifq *ifq); +void io_zc_rx_put_buf (struct io_zc_rx_ifq *ifq,struct io_zc_rx_buf *buf); +static inline dma_addr_t io_zc_rx_buf_dma (struct io_zc_rx_buf *buf) +{ +return buf->dma; +} +static inline struct page *io_zc_rx_buf_page (struct io_zc_rx_buf *buf) +{ +return buf->page; +}static inline void io_uring_files_cancel (void ) {if (current->io_uring) {
@@ -108,6 +119,13 @@ static inline const char *io_uring_get_opcode(u8 opcode) {return "" ; } +static inline struct io_zc_rx_buf *io_zc_rx_get_buf (struct io_zc_rx_ifq *ifq) +{ +return NULL ; +} +void io_zc_rx_put_buf (struct io_zc_rx_ifq *ifq,struct io_zc_rx_buf *buf) +{ +}#endif #endif
io_uring/zc_rx.c +98 -0
113 lines not on this page · raw message
io_uring/zc_rx.h +13 -0
@@ -2,6 +2,8 @@ #ifndef IOU_ZC_RX_H #define IOU_ZC_RX_H +#include <linux/io_uring_types.h> + struct io_zc_rx_ifq { struct io_ring_ctx *ctx; struct net_device *dev;
@@ -9,12 +11,23 @@ struct io_zc_rx_ifq {struct io_uring_rbuf_rqe *rqes;struct io_uring_rbuf_cqe *cqes;u32 rq_entries, cq_entries; +u32 cached_rq_head; +u32 cached_cq_tail;void *pool;/* hw rx descriptor ring id */ u32 if_rxq_id; }; +static inline u32 io_zc_rx_rqring_entries (struct io_zc_rx_ifq *ifq) +{ +struct io_rbuf_ring *ring = ifq->ring; +u32 entries; + + entries =smp_load_acquire (&ring->rq.tail) - ifq->cached_rq_head; +return min (entries, ifq->rq_entries); +} +int io_register_zc_rx_ifq (struct io_ring_ctx *ctx,struct io_uring_zc_rx_ifq_reg __user *arg);int io_unregister_zc_rx_ifq (struct io_ring_ctx *ctx);
Signature · 1 line
2.39.3
PATCH 7/11 skbuff: add SKBFL_FIXED_FRAG and skb_fixed()
In reply to David WeiFrom: David Wei <[email protected]>
When an skb that is marked as zero copy goes up the network stack during RX, skb_orphan_frags_rx is called which then calls skb_copy_ubufs and defeat the purpose of ZC.
This is because currently zero copy is only for TX and this behaviour is designed to prevent TX zero copy data being redirected up the network stack rather than new zero copy RX data coming from the driver.
This patch adds a new flag SKBFL_FIXED_FRAG and checks for this in skb_orphan_frags, not calling skb_copy_ubufs if it is set.
--- include/linux/skbuff.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-)
include/linux/skbuff.h +9 -1
@@ -518,6 +518,9 @@ enum {* use frags only up until ubuf_info is released */ SKBFL_MANAGED_FRAG_REFS =BIT (4 ), + +/* don't move or copy the fragment */ + SKBFL_FIXED_FRAG = BIT(5), };#define SKBFL_ZEROCOPY_FRAG (SKBFL_ZEROCOPY_ENABLE | SKBFL_SHARED_FRAG)
@@ -1674,6 +1677,11 @@ static inline bool skb_zcopy_managed(const struct sk_buff *skb)return skb_shinfo (skb)->flags &SKBFL_MANAGED_FRAG_REFS ; } +static inline bool skb_fixed (const struct sk_buff *skb) +{ +return skb_shinfo (skb)->flags &SKBFL_FIXED_FRAG ; +} +static inline bool skb_pure_zcopy_same (const struct sk_buff *skb1,const struct sk_buff *skb2) {
@@ -3135,7 +3143,7 @@ static inline int skb_orphan_frags(struct sk_buff *skb, gfp_t gfp_mask)/* Frags must be orphaned, even if refcounted, if skb might loop to rx path */ static inline int skb_orphan_frags_rx (struct sk_buff *skb,gfp_t gfp_mask) { -if (likely (!skb_zcopy (skb))) +if (likely (!skb_zcopy (skb) ||skb_fixed (skb)))return 0 ;return skb_copy_ubufs (skb, gfp_mask); }
Signature · 1 line
2.39.3
PATCH 8/11 io_uring: allocate a uarg for freeing zero copy skbs
In reply to David WeiFrom: David Wei <[email protected]>
As ZC skbs are marked as zero copy, they will bypass the default skb frag destructor. This patch adds a static uarg that is attached to ZC bufs and a callback that returns them to the freelist of a ZC pool.
--- include/linux/io_uring.h | 7 +++++++ include/linux/netdevice.h | 1 + io_uring/zc_rx.c | 44 +++++++++++++++++++++++++++++++++++++++ io_uring/zc_rx.h | 2 ++ 4 files changed, 54 insertions(+)
include/linux/io_uring.h +7 -0
@@ -62,6 +62,8 @@ const char *io_uring_get_opcode(u8 opcode);struct io_zc_rx_ifq ;struct io_zc_rx_buf *io_zc_rx_get_buf (struct io_zc_rx_ifq *ifq); +struct io_zc_rx_buf *io_zc_rx_buf_from_page (struct io_zc_rx_ifq *ifq, +struct page *page);void io_zc_rx_put_buf (struct io_zc_rx_ifq *ifq,struct io_zc_rx_buf *buf);static inline dma_addr_t io_zc_rx_buf_dma (struct io_zc_rx_buf *buf) {
@@ -123,6 +125,11 @@ static inline struct io_zc_rx_buf *io_zc_rx_get_buf(struct io_zc_rx_ifq *ifq) {return NULL ; } +static inline struct io_zc_rx_buf *io_zc_rx_buf_from_page (struct io_zc_rx_ifq *ifq, +struct page *page) +{ +return NULL ; +}void io_zc_rx_put_buf (struct io_zc_rx_ifq *ifq,struct io_zc_rx_buf *buf) { }
include/linux/netdevice.h +1 -0
@@ -1043,6 +1043,7 @@ struct netdev_bpf {struct {struct io_zc_rx_ifq *ifq;u16 queue_id; +struct ubuf_info *uarg; } zc_rx; }; };
io_uring/zc_rx.c +44 -0
71 lines not on this page · raw message
io_uring/zc_rx.h +2 -0
@@ -3,6 +3,7 @@#define IOU_ZC_RX_H #include <linux/io_uring_types.h> +#include <linux/skbuff.h> struct io_zc_rx_ifq {struct io_ring_ctx *ctx;
@@ -10,6 +11,7 @@ struct io_zc_rx_ifq {struct io_rbuf_ring *ring;struct io_uring_rbuf_rqe *rqes;struct io_uring_rbuf_cqe *cqes; +struct ubuf_info uarg;u32 rq_entries, cq_entries;u32 cached_rq_head;u32 cached_cq_tail;
Signature · 1 line
2.39.3
PATCH 9/11 io_uring: delay ZC pool destruction
In reply to David WeiFrom: David Wei <[email protected]>
At a point in time, a ZC buf may be in:
* RX queue
* Socket
* One of the ifq ringbufs
* Userspace
The ZC pool region and the pool itself cannot be destroyed until all bufs have been returned.
This patch changes the ZC pool destruction to be delayed work, waiting for up to 10 seconds for bufs to be returned before unconditionally destroying the pool.
--- io_uring/zc_rx.c | 50 ++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 6 deletions(-)
io_uring/zc_rx.c +44 -6
@@ -28,6 +28,10 @@ struct io_zc_rx_pool {u32 cache_count;u32 cache[POOL_CACHE_SIZE ]; +/* delayed destruction */ +unsigned long delay_end; +struct delayed_work destroy_work; +/* freelist */ spinlock_t freelist_lock;u32 free_count;
@@ -222,20 +226,56 @@ int io_zc_rx_create_pool(struct io_ring_ctx *ctx,return ret; } -static void io_zc_rx_destroy_pool (struct io_zc_rx_pool *pool) +static void io_zc_rx_destroy_ifq (struct io_zc_rx_ifq *ifq) +{ +if (ifq->dev) +dev_put (ifq->dev); +io_free_rbuf_ring (ifq); +kfree (ifq); +} + +static void io_zc_rx_destroy_pool_work (struct work_struct *work) { +struct io_zc_rx_pool *pool =container_of ( + to_delayed_work(work), struct io_zc_rx_pool, destroy_work);struct device *dev =netdev2dev (pool->ifq->dev);struct io_zc_rx_buf *buf; +int i, refc, count; -for (int i =0 ; i < pool->nr_pages; i++) { +for (i =0 ; i < pool->nr_pages; i++) { buf = &pool->bufs[i]; + refc =atomic_read (&buf->refcount) &IO_ZC_RX_KREF_MASK ; +if (refc) { +if (time_before (jiffies, pool->delay_end)) { +schedule_delayed_work (&pool->destroy_work,HZ ); +return ; + } + count++; + } + } + +if (count) +pr_debug ("freeing pool with %d/%d outstanding pages\n" , + count, pool->nr_pages); + +for (i =0 ; i < pool->nr_pages; i++) { + buf = &pool->bufs[i];io_zc_rx_unmap_buf (dev, buf); } + +io_zc_rx_destroy_ifq (pool->ifq);kvfree (pool->bufs);kvfree (pool); } +static void io_zc_rx_destroy_pool (struct io_zc_rx_pool *pool) +{ + pool->delay_end = jiffies +HZ *10 ; +INIT_DELAYED_WORK (&pool->destroy_work, io_zc_rx_destroy_pool_work); +schedule_delayed_work (&pool->destroy_work,0 ); +} +static struct io_zc_rx_ifq *io_zc_rx_ifq_alloc (struct io_ring_ctx *ctx) {struct io_zc_rx_ifq *ifq;
@@ -256,10 +296,8 @@ static void io_zc_rx_ifq_free(struct io_zc_rx_ifq *ifq)io_close_zc_rxq (ifq);if (ifq->pool)io_zc_rx_destroy_pool (ifq->pool); -if (ifq->dev) -dev_put (ifq->dev); -io_free_rbuf_ring (ifq); -kfree (ifq); +else +io_zc_rx_destroy_ifq (ifq); } int io_register_zc_rx_ifq(struct io_ring_ctx *ctx,
Signature · 1 line
2.39.3
PATCH 10/11 netdev/bnxt: add data pool and use it in BNXT driver
In reply to David WeiFrom: David Wei <[email protected]>
This patch adds a thin wrapper called data pool that wraps the existing page pool and the newly added ZC pool.
There is one struct netdev_rx_queue per logical RX queue. This patch adds ZC ifq, page pool, and uarg (set during skb construction) to a netdev_rx_queue. The data pool wrapper uses the ZC pool if an ifq is present, otherwise using the page pool.
The BNXT driver is modified to use data pool in order to support ZC RX. A setup function bnxt_zc_rx is added that is called on XDP_SETUP_ZC_RX XDP command which sets fields in netdev_rx_queue. Calls to get/put bufs from the page pool are related w/ the data pool.
--- drivers/net/ethernet/broadcom/bnxt/bnxt.c | 59 ++++++++---- drivers/net/ethernet/broadcom/bnxt/bnxt.h | 4 + drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c | 3 + include/linux/netdevice.h | 4 + include/net/data_pool.h | 96 +++++++++++++++++++ 5 files changed, 149 insertions(+), 17 deletions(-) create mode 100644 include/net/data_pool.h
drivers/net/ethernet/broadcom/bnxt/bnxt.c +42 -17
@@ -57,6 +57,7 @@#include <net/page_pool.h> #include <linux/align.h> #include <net/netdev_queues.h> +#include <net/data_pool.h> #include "bnxt_hsi.h" #include "bnxt.h"
@@ -724,7 +725,7 @@ static void bnxt_tx_int(struct bnxt *bp, struct bnxt_napi *bnapi, int nr_pkts)__netif_txq_completed_wake (txq, nr_pkts, tx_bytes,bnxt_tx_avail (bp, txr), bp->tx_wake_thresh, -READ_ONCE (txr->dev_state) !=BNXT_DEV_STATE_CLOSING ); +READ_ONCE (txr->dev_state) ==BNXT_DEV_STATE_CLOSING ); } static struct page *__bnxt_alloc_rx_64k_page(struct bnxt *bp, dma_addr_t *mapping,
@@ -738,13 +739,7 @@ static struct page *__bnxt_alloc_rx_64k_page(struct bnxt *bp, dma_addr_t *mappinif (!page)return NULL ; - *mapping =dma_map_page_attrs (&bp->pdev->dev, page, offset, -BNXT_RX_PAGE_SIZE ,DMA_FROM_DEVICE , -DMA_ATTR_WEAK_ORDERING ); -if (dma_mapping_error (&bp->pdev->dev, *mapping)) { -page_pool_recycle_direct (rxr->page_pool, page); -return NULL ; - } + *mapping =page_pool_get_dma_addr (page);if (page_offset) *page_offset = offset;
@@ -757,19 +752,14 @@ static struct page *__bnxt_alloc_rx_page(struct bnxt *bp, dma_addr_t *mapping, struct bnxt_rx_ring_info *rxr, gfp_t gfp) { -struct device *dev = &bp->pdev->dev;struct page *page; - page =page_pool_dev_alloc_pages (rxr->page_pool); + page =data_pool_alloc_page (rxr->rx_queue);if (!page)return NULL ; - *mapping =dma_map_page_attrs (dev, page,0 ,PAGE_SIZE , bp->rx_dir, -DMA_ATTR_WEAK_ORDERING ); -if (dma_mapping_error (dev, *mapping)) { -page_pool_recycle_direct (rxr->page_pool, page); -return NULL ; - } + *mapping =data_pool_get_dma_addr (rxr->rx_queue, page); +return page; }
@@ -1787,6 +1777,8 @@ static void bnxt_deliver_skb(struct bnxt *bp, struct bnxt_napi *bnapi,return ; }skb_record_rx_queue (skb, bnapi->index); +if (bnapi->rx_ring->rx_queue->zc_ifq) +skb_zcopy_init (skb, bnapi->rx_ring->rx_queue->zc_uarg);skb_mark_for_recycle (skb);napi_gro_receive (&bnapi->napi, skb); }
@@ -3016,7 +3008,7 @@ static void bnxt_free_one_rx_ring_skbs(struct bnxt *bp, int ring_nr) rx_agg_buf->page =NULL ;__clear_bit (i, rxr->rx_agg_bmap); -page_pool_recycle_direct (rxr->page_pool, page); +data_pool_put_page (rxr->rx_queue, page); } skip_rx_agg_free:
@@ -3225,6 +3217,8 @@ static void bnxt_free_rx_rings(struct bnxt *bp)page_pool_destroy (rxr->page_pool); rxr->page_pool =NULL ; + rxr->rx_queue->page_pool =NULL ; + rxr->rx_queue->zc_ifq =NULL ;kfree (rxr->rx_agg_bmap); rxr->rx_agg_bmap =NULL ;
@@ -3251,12 +3245,16 @@ static int bnxt_alloc_rx_page_pool(struct bnxt *bp, pp.dma_dir =DMA_BIDIRECTIONAL ;if (PAGE_SIZE >BNXT_RX_PAGE_SIZE ) pp.flags |=PP_FLAG_PAGE_FRAG ; + pp.flags |=PP_FLAG_DMA_MAP |PP_FLAG_DMA_SYNC_DEV ; + pp.max_len =PAGE_SIZE ; rxr->page_pool =page_pool_create (&pp); +data_pool_set_page_pool (bp->dev, rxr->bnapi->index, rxr->page_pool);if (IS_ERR (rxr->page_pool)) {int err =PTR_ERR (rxr->page_pool); rxr->page_pool =NULL ; +data_pool_set_page_pool (bp->dev, rxr->bnapi->index,NULL );return err; }return 0 ;
@@ -4620,6 +4618,8 @@ static int bnxt_alloc_mem(struct bnxt *bp, bool irq_re_init) rxr->rx_agg_ring_struct.ring_mem.flags =BNXT_RMEM_RING_PTE_FLAG ; } + + rxr->rx_queue =data_pool_get_rx_queue (bp->dev, bp->bnapi[i]->index); rxr->bnapi = bp->bnapi[i]; bp->bnapi[i]->rx_ring = &bp->rx_ring[i]; }
@@ -13904,6 +13904,31 @@ void bnxt_print_device_info(struct bnxt *bp)pcie_print_link_status (bp->pdev); } +int bnxt_zc_rx (struct bnxt *bp,struct netdev_bpf *xdp) +{ +if (xdp->zc_rx.queue_id >= bp->rx_nr_rings) +return -EINVAL ; + +bnxt_rtnl_lock_sp (bp); +if (netif_running (bp->dev)) { +struct netdev_rx_queue *rxq; +int rc; + +bnxt_ulp_stop (bp); +bnxt_close_nic (bp, true, false); + + rxq =data_pool_get_rx_queue (bp->dev, xdp->zc_rx.queue_id); + rxq->queue_id = xdp->zc_rx.queue_id; + rxq->zc_ifq = xdp->zc_rx.ifq; + rxq->zc_uarg = xdp->zc_rx.uarg; + + rc =bnxt_open_nic (bp, true, false); +bnxt_ulp_start (bp, rc); + } +bnxt_rtnl_unlock_sp (bp); +return 0 ; +} +static int bnxt_init_one (struct pci_dev *pdev,const struct pci_device_id *ent) {struct net_device *dev;
drivers/net/ethernet/broadcom/bnxt/bnxt.h +4 -0
@@ -949,6 +949,7 @@ struct bnxt_rx_ring_info {struct bnxt_ring_struct rx_agg_ring_struct;struct xdp_rxq_info xdp_rxq;struct page_pool *page_pool; +struct netdev_rx_queue *rx_queue; };struct bnxt_rx_sw_stats {
@@ -2454,4 +2455,7 @@ int bnxt_get_port_parent_id(struct net_device *dev,void bnxt_dim_work (struct work_struct *work);int bnxt_hwrm_set_ring_coal (struct bnxt *bp,struct bnxt_napi *bnapi);void bnxt_print_device_info (struct bnxt *bp); + +int bnxt_zc_rx (struct bnxt *bp,struct netdev_bpf *xdp); +#endif
drivers/net/ethernet/broadcom/bnxt/bnxt_xdp.c +3 -0
@@ -455,6 +455,9 @@ int bnxt_xdp(struct net_device *dev, struct netdev_bpf *xdp)case XDP_SETUP_PROG : rc =bnxt_xdp_set (bp, xdp->prog);break ; +case XDP_SETUP_ZC_RX : +return bnxt_zc_rx (bp, xdp); +break ;default : rc = -EINVAL ;break ;
include/linux/netdevice.h +4 -0
@@ -789,6 +789,10 @@ struct netdev_rx_queue {struct kobject kobj;struct net_device *dev;netdevice_tracker dev_tracker; +unsigned int queue_id; +struct page_pool *page_pool; +struct io_zc_rx_ifq *zc_ifq; +struct ubuf_info *zc_uarg;#ifdef CONFIG_XDP_SOCKETS struct xsk_buff_pool *pool;
include/net/data_pool.h new file +96 -0
96 lines not on this page · raw message
Signature · 1 line
2.39.3
PATCH 11/11 io_uring: add io_recvzc request
In reply to David WeiFrom: David Wei <[email protected]>
This patch adds 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 where its page frags are tagged w/ a magic cookie in their page private field. For each frag, entries are written into the ifq rbuf completion ring, and the total number of bytes read is returned to user as an io_uring completion event.
Multishot requests work. There is no need to specify provided buffers as data is returned in the ifq rbuf completion rings.
Userspace is expected to look into the ifq rbuf completion ring when it receives an io_uring completion event.
The addr3 field is used to encode params in the following format:
addr3 = (readlen << 32) | ifq_id;
readlen is the max amount of data to read from the socket. ifq_id is the interface queue id, and currently only 0 is supported.
--- include/uapi/linux/io_uring.h | 1 + io_uring/net.c | 83 +++++++++++- io_uring/opdef.c | 16 +++ io_uring/zc_rx.c | 232 ++++++++++++++++++++++++++++++++++ io_uring/zc_rx.h | 4 + 5 files changed, 335 insertions(+), 1 deletion(-)
include/uapi/linux/io_uring.h +1 -0
@@ -223,6 +223,7 @@ enum io_uring_op {IORING_OP_URING_CMD ,IORING_OP_SEND_ZC ,IORING_OP_SENDMSG_ZC , +IORING_OP_RECV_ZC ,/* this goes last, obviously */ IORING_OP_LAST,
io_uring/net.c +82 -1
101 lines not on this page · raw message
io_uring/opdef.c +16 -0
@@ -33,6 +33,7 @@#include "poll.h" #include "cancel.h" #include "rw.h" +#include "zc_rx.h" static int io_no_issue (struct io_kiocb *req,unsigned int issue_flags) {
@@ -426,6 +427,18 @@ const struct io_issue_def io_issue_defs[] = { .issue = io_sendmsg_zc, #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 }, };
@@ -648,6 +661,9 @@ const struct io_cold_def io_cold_defs[] = { .fail = io_sendrecv_fail,#endif }, + [IORING_OP_RECV_ZC] = { + .name = "RECV_ZC", + }, };const char *io_uring_get_opcode (u8 opcode)
io_uring/zc_rx.c +232 -0
253 lines not on this page · raw message
io_uring/zc_rx.h +4 -0
@@ -35,4 +35,8 @@ int io_register_zc_rx_ifq(struct io_ring_ctx *ctx,int io_unregister_zc_rx_ifq (struct io_ring_ctx *ctx);int io_zc_rx_pool_create (struct io_zc_rx_ifq *ifq,u16 id); +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); +int io_zc_rx_recv (struct socket *sock,unsigned int limit,unsigned int flags); +#endif
Signature · 1 line
2.39.3
Jakub Kicinski
maintainerQuoted 6 lines · On Fri, 25 Aug 2023 15:55:39 -0700 David Wei wrote:
From: David Wei <[email protected]>
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.
Please repost this and CC netdev@.