io_uring zcrx ifq sharing
Subsystems (4)
v6 cover io_uring zcrx ifq sharing
Each ifq is bound to a HW RX queue with no way to share this across multiple rings. It is possible that one ring will not be able to fully saturate an entire HW RX queue due to userspace work. There are two ways to handle more work:
1. Move work to other threads, but have to pay context switch overhead and cold caches.
2. Add more rings with ifqs, but HW RX queues are a limited resource.
This patchset add a way for multiple rings to share the same underlying src ifq that is bound to a HW RX queue. Rings with shared ifqs can issue io_recvzc on zero copy sockets, just like the src ring.
Userspace are expected to create rings in separate threads and not processes, such that all rings share the same address space. This is because the sharing and synchronisation of refill rings is purely done in userspace with no kernel involvement e.g. dst rings do not mmap the refill ring. Also, userspace must distribute zero copy sockets steered into the same HW RX queue across rings sharing the ifq.
v6:
- removed ifq refcounting that merged separately
- fill in struct io_uring_zcrx_offsets
- shorten functions + structs
- move ctx flags checks from export to import
v5:
- remove sync refill api
- pp mp taking ref on ifq
- add ifq export to file
- implement sharing by importing ifq fd
v4:
- lock rings in seq instead of both
- drop export io_lock_two_rings()
- break circular ref between ifq and ring ctx
- remove io_shutdown_zcrx_ifqs()
- copy reg struct to user before writing ifq to ctx->zcrx_ctxs
v3:
- drop ifq->proxy
- use dec_and_test to clean up ifq
v2:
- split patch
David Wei (3):
io_uring/zcrx: move io_zcrx_scrub() and dependencies up io_uring/zcrx: add io_fill_zcrx_offsets() io_uring/zcrx: share an ifq between rings
Pavel Begunkov (2):
io_uring/zcrx: count zcrx users
io_uring/zcrx: export zcrx via a file
include/uapi/linux/io_uring.h | 5 + io_uring/zcrx.c | 218 ++++++++++++++++++++++++++-------- io_uring/zcrx.h | 2 + 3 files changed, 173 insertions(+), 52 deletions(-)
Signature · 1 line
2.47.3
PATCH v6 1/5 io_uring/zcrx: count zcrx users
From: Pavel Begunkov <[email protected]>
zcrx tries to detach ifq / terminate page pools when the io_uring ctx owning it is being destroyed. There will be multiple io_uring instances attached to it in the future, so add a separate counter to track the users. Note, refs can't be reused for this purpose as it only used to prevent zcrx and rings destruction, and also used by page pools to keep it alive.
--- io_uring/zcrx.c | 7 +++++-- io_uring/zcrx.h | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-)
io_uring/zcrx.c +5 -2
@@ -482,6 +482,7 @@ static struct io_zcrx_ifq *io_zcrx_ifq_alloc(struct io_ring_ctx *ctx)spin_lock_init (&ifq->rq_lock);mutex_init (&ifq->pp_lock);refcount_set (&ifq->refs,1 ); +refcount_set (&ifq->user_refs,1 );return ifq; }
@@ -742,8 +743,10 @@ void io_unregister_zcrx_ifqs(struct io_ring_ctx *ctx)if (!ifq)break ; -io_close_queue (ifq); -io_zcrx_scrub (ifq); +if (refcount_dec_and_test (&ifq->user_refs)) { +io_close_queue (ifq); +io_zcrx_scrub (ifq); + }io_put_zcrx_ifq (ifq); }
io_uring/zcrx.h +2 -0
@@ -55,6 +55,8 @@ struct io_zcrx_ifq {struct net_device *netdev;netdevice_tracker netdev_tracker;refcount_t refs; +/* counts userspace facing users like io_uring */ +refcount_t user_refs;/* * Page pool and net configuration lock, can be taken deeper in the
Signature · 1 line
2.47.3
PATCH v6 2/5 io_uring/zcrx: move io_zcrx_scrub() and dependencies up
In reply to David WeiIn preparation for adding zcrx ifq exporting and importing, move io_zcrx_scrub() and its dependencies up the file to be closer to io_close_queue().
--- io_uring/zcrx.c | 84 ++++++++++++++++++++++++------------------------- 1 file changed, 42 insertions(+), 42 deletions(-)
io_uring/zcrx.c +42 -42
@@ -544,6 +544,48 @@ static void io_put_zcrx_ifq(struct io_zcrx_ifq *ifq)io_zcrx_ifq_free (ifq); } +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); + +if (!niov->desc.pp) { +/* copy fallback allocated niovs */ +io_zcrx_return_niov_freelist (niov); +return ; + } +page_pool_put_unrefed_netmem (niov->desc.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); + } +} +struct io_mapped_region *io_zcrx_get_region (struct io_ring_ctx *ctx,unsigned int id) {
@@ -684,48 +726,6 @@ static struct net_iov *__io_zcrx_get_free_niov(struct io_zcrx_area *area)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); - -if (!niov->desc.pp) { -/* copy fallback allocated niovs */ -io_zcrx_return_niov_freelist (niov); -return ; - } -page_pool_put_unrefed_netmem (niov->desc.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_unregister_zcrx_ifqs (struct io_ring_ctx *ctx) {struct io_zcrx_ifq *ifq;
Signature · 1 line
2.47.3
PATCH v6 3/5 io_uring/zcrx: export zcrx via a file
In reply to David WeiFrom: Pavel Begunkov <[email protected]>
Add an option to wrap a zcrx instance into a file and expose it to the user space. Currently, users can't do anything meaningful with the file, but it'll be used in a next patch to import it into another io_uring instance. It's implemented as a new op called ZCRX_CTRL_EXPORT for the IORING_REGISTER_ZCRX_CTRL registration opcode.
--- include/uapi/linux/io_uring.h | 1 + io_uring/zcrx.c | 59 +++++++++++++++++++++++++++++++---- 2 files changed, 54 insertions(+), 6 deletions(-)
include/uapi/linux/io_uring.h +1 -0
@@ -1087,6 +1087,7 @@ struct io_uring_zcrx_ifq_reg {enum zcrx_ctrl_op {ZCRX_CTRL_FLUSH_RQ , +ZCRX_CTRL_EXPORT , __ZCRX_CTRL_LAST, };
io_uring/zcrx.c +53 -6
89 lines not on this page · raw message
Signature · 1 line
2.47.3
PATCH v6 4/5 io_uring/zcrx: add io_fill_zcrx_offsets()
In reply to David WeiAdd a helper io_fill_zcrx_offsets() that sets the constant offsets in struct io_uring_zcrx_offsets returned to userspace.
--- io_uring/zcrx.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-)
io_uring/zcrx.c +9 -4
@@ -345,6 +345,13 @@ static void io_zcrx_get_niov_uref(struct net_iov *niov)atomic_inc (io_get_user_counter (niov)); } +static void io_fill_zcrx_offsets (struct io_uring_zcrx_offsets *offsets) +{ + offsets->head = offsetof(struct io_uring , head); + offsets->tail = offsetof(struct io_uring , tail); + offsets->rqes =ALIGN (sizeof (struct io_uring ),L1_CACHE_BYTES ); +} + static int io_allocate_rbuf_ring(struct io_ring_ctx *ctx, struct io_zcrx_ifq *ifq, struct io_uring_zcrx_ifq_reg *reg,
@@ -356,7 +363,8 @@ static int io_allocate_rbuf_ring(struct io_ring_ctx *ctx,void *ptr;int ret; - off =ALIGN (sizeof (struct io_uring ),L1_CACHE_BYTES ); +io_fill_zcrx_offsets (®->offsets); + off = reg->offsets.rqes; size = off +sizeof (struct io_uring_zcrx_rqe ) * reg->rq_entries;if (size > rd->size)return -EINVAL ;
@@ -372,9 +380,6 @@ static int io_allocate_rbuf_ring(struct io_ring_ctx *ctx, ifq->rq_ring = (struct io_uring *)ptr; ifq->rqes = (struct io_uring_zcrx_rqe *)(ptr + off); - reg->offsets.head = offsetof(struct io_uring , head); - reg->offsets.tail = offsetof(struct io_uring , tail); - reg->offsets.rqes = off;return 0 ; }
Signature · 1 line
2.47.3
PATCH v6 5/5 io_uring/zcrx: share an ifq between rings
In reply to David WeiAdd a way to share an ifq from a src ring that is real (i.e. bound to a HW RX queue) with other rings. This is done by passing a new flag IORING_ZCRX_IFQ_REG_IMPORT in the registration struct io_uring_zcrx_ifq_reg, alongside the fd of an exported zcrx ifq.
--- include/uapi/linux/io_uring.h | 4 +++ io_uring/zcrx.c | 63 +++++++++++++++++++++++++++++++++-- 2 files changed, 65 insertions(+), 2 deletions(-)
include/uapi/linux/io_uring.h +4 -0
@@ -1067,6 +1067,10 @@ struct io_uring_zcrx_area_reg {__u64 __resv2[2 ]; }; +enum zcrx_reg_flags { +ZCRX_REG_IMPORT =1 , +}; +/* * Argument for IORING_REGISTER_ZCRX_IFQ */
io_uring/zcrx.c +61 -2
78 lines not on this page · raw message
Signature · 1 line
2.47.3
Pavel Begunkov
maintainerQuoted 13 lines · David Wei
Each ifq is bound to a HW RX queue with no way to share this across multiple rings. It is possible that one ring will not be able to fully saturate an entire HW RX queue due to userspace work. There are two ways to handle more work:
1. Move work to other threads, but have to pay context switch overhead and cold caches.
2. Add more rings with ifqs, but HW RX queues are a limited resource.This patchset add a way for multiple rings to share the same underlying src ifq that is bound to a HW RX queue. Rings with shared ifqs can issue
io_recvzcon zero copy sockets, just like the src ring.
Quoted 6 lines
Userspace are expected to create rings in separate threads and not processes, such that all rings share the same address space. This is because the sharing and synchronisation of refill rings is purely done in userspace with no kernel involvement e.g. dst rings do not mmap the refill ring. Also, userspace must distribute zero copy sockets steered into the same HW RX queue across rings sharing the ifq.
I agree it's the simplest way to use it, but cross process sharing is a valid use case. I'm sure you can mmap it by guessing offset and you can place it into some shared memory otherwise.
The implementation lgtm. I need to give it a run, but let me queue it up with other dependencies.
Signature · 1 line
Pavel Begunkov
Quoted 21 lines · Pavel Begunkov
Quoted 13 lines · David Wei
Each ifq is bound to a HW RX queue with no way to share this across multiple rings. It is possible that one ring will not be able to fully saturate an entire HW RX queue due to userspace work. There are two ways to handle more work:
1. Move work to other threads, but have to pay context switch overhead and cold caches.
2. Add more rings with ifqs, but HW RX queues are a limited resource.This patchset add a way for multiple rings to share the same underlying src ifq that is bound to a HW RX queue. Rings with shared ifqs can issue
io_recvzcon zero copy sockets, just like the src ring.Quoted 6 lines
Userspace are expected to create rings in separate threads and not processes, such that all rings share the same address space. This is because the sharing and synchronisation of refill rings is purely done in userspace with no kernel involvement e.g. dst rings do not mmap the refill ring. Also, userspace must distribute zero copy sockets steered into the same HW RX queue across rings sharing the ifq.
Quoted 7 lines
I agree it's the simplest way to use it, but cross process sharing is a valid use case. I'm sure you can mmap it by guessing offset and you can place it into some shared memory otherwise.
The implementation lgtm. I need to give it a run, but let me queue it up with other dependencies.
Yeah there's no reason why shm + mmap wouldn't work cross process with the right offsets, but I do suspect that it'll be niche with most users running iou across threads in the same process.
We can add cross process support in the future.