io_uring: support sqe group and leased group kbuf
Subsystems (3)
v7 cover io_uring: support sqe group and leased group kbuf
The 1st 3 patches are cleanup, and prepare for adding sqe group.
The 4th patch supports generic sqe group which is like link chain, but allows each sqe in group to be issued in parallel and the group shares same IO_LINK & IO_DRAIN boundary, so N:M dependency can be supported with sqe group & io link together.
The 5th & 6th patches supports to lease other subsystem's kbuf to io_uring for use in sqe group wide.
The 7th patch supports ublk zero copy based on io_uring sqe group & leased kbuf.
Tests:
1) pass liburing test
- make runtests
2) write/pass sqe group test case and sqe provide buffer case:
https://github.com/ming1/liburing/tree/uring_group
- covers related sqe flags combination and linking groups, both nop and one multi-destination file copy.
- cover failure handling test: fail leader IO or member IO in both single group and linked groups, which is done in each sqe flags combination test
- cover io_uring with leased group kbuf by adding ublk-loop-zc
V7:
- remove dead code in sqe group support(Pavel)
- fail single group request(Pavel)
- remove IORING_PROVIDE_GROUP_KBUF(Pavel)
- remove REQ_F_SQE_GROUP_DEP(Pavel)
- rename as leasing buffer
- improve commit log
- map group member's IOSQE_IO_DRAIN to GROUP_KBUF, which aligns with buffer select use, and it means that io_uring starts to support leased kbuf from other subsystem for group member requests only
V6:
- follow Pavel's suggestion to disallow IOSQE_CQE_SKIP_SUCCESS & LINK_TIMEOUT
- kill __io_complete_group_member() (Pavel)
- simplify link failure handling (Pavel)
- move members' queuing out of completion lock (Pavel)
- cleanup group io complete handler
- add more comment
- add ublk zc into liburing test for covering IOSQE_SQE_GROUP & IORING_PROVIDE_GROUP_KBUF
V5:
- follow Pavel's suggestion to minimize change on io_uring fast code
path: sqe group code is called in by single 'if (unlikely())' from both issue & completion code path
- simplify & re-write group request completion
avoid to touch io-wq code by completing group leader via tw directly, just like ->task_complete
re-write group member & leader completion handling, one simplification is always to free leader via the last member
simplify queueing group members, not support issuing leader and members in parallel
- fail the whole group if IO_*LINK & IO_DRAIN is set on group members, and test code to cover this change
- misc cleanup
V4:
- address most comments from Pavel
- fix request double free
- don't use io_req_commit_cqe() in io_req_complete_defer()
- make members' REQ_F_INFLIGHT discoverable
- use common assembling check in submission code path
- drop patch 3 and don't move REQ_F_CQE_SKIP out of io_free_req()
- don't set .accept_group_kbuf for net send zc, in which members need to be queued after buffer notification is got, and can be enabled in future
- add .grp_leader field via union, and share storage with .grp_link
- move .grp_refs into one hole of io_kiocb, so that one extra cacheline isn't needed for io_kiocb
- cleanup & document improvement
V3:
- add IORING_FEAT_SQE_GROUP
- simplify group completion, and minimize change on io_req_complete_defer()
- simplify & cleanup io_queue_group_members()
- fix many failure handling issues
- cover failure handling code in added liburing tests
- remove RFC
V2:
- add generic sqe group, suggested by Kevin Wolf
- add REQ_F_SQE_GROUP_DEP which is based on IOSQE_SQE_GROUP, for sharing kernel resource in group wide, suggested by Kevin Wolf
- remove sqe ext flag, and use the last bit for IOSQE_SQE_GROUP(Pavel), in future we still can extend sqe flags with one uring context flag
- initialize group requests via submit state pattern, suggested by Pavel
- all kinds of cleanup & bug fixes
Ming Lei (7):
io_uring: add io_link_req() helper
io_uring: add io_submit_fail_link() helper
io_uring: add helper of io_req_commit_cqe()
io_uring: support SQE group
io_uring: support leased group buffer with REQ_F_GROUP_KBUF io_uring/uring_cmd: support leasing device kernel buffer to io_uring
ublk: support lease io buffer to io_uring
drivers/block/ublk_drv.c | 157 +++++++++++++- include/linux/io_uring/cmd.h | 7 + include/linux/io_uring_types.h | 58 +++++ include/uapi/linux/io_uring.h | 4 + include/uapi/linux/ublk_cmd.h | 7 +- io_uring/io_uring.c | 382 ++++++++++++++++++++++++++++++--- io_uring/io_uring.h | 11 + io_uring/kbuf.c | 58 +++++ io_uring/kbuf.h | 22 ++ io_uring/net.c | 23 +- io_uring/rw.c | 21 +- io_uring/timeout.c | 6 + io_uring/uring_cmd.c | 13 ++ 13 files changed, 721 insertions(+), 48 deletions(-)
Signature · 1 line
2.46.0
PATCH v7 1/7 io_uring: add io_link_req() helper
Add io_link_req() helper, so that io_submit_sqe() can become more readable.
--- io_uring/io_uring.c | 41 +++++++++++++++++++++++++++-------------- 1 file changed, 27 insertions(+), 14 deletions(-)
io_uring/io_uring.c +27 -14
@@ -2158,19 +2158,11 @@ static __cold int io_submit_fail_init(const struct io_uring_sqe *sqe,return 0 ; } -static inline int io_submit_sqe (struct io_ring_ctx *ctx,struct io_kiocb *req, -const struct io_uring_sqe *sqe) - __must_hold(&ctx->uring_lock) +/* +* Return NULL if nothing to be queued, otherwise return request for queueing */ +static struct io_kiocb *io_link_sqe (struct io_submit_link *link, +struct io_kiocb *req) { -struct io_submit_link *link = &ctx->submit_state.link; -int ret; - - ret =io_init_req (ctx, req, sqe); -if (unlikely (ret)) -return io_submit_fail_init (sqe, req, ret); - -trace_io_uring_submit_req (req); -/* * If we already have a head request, queue this one for async * submittal once the head completes. If we don't have a head but
@@ -2184,7 +2176,7 @@ static inline int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, link->last = req;if (req->flags &IO_REQ_LINK_FLAGS ) -return 0 ; +return NULL ;/* last request of the link, flush it */ req = link->head; link->head =NULL ;
@@ -2200,9 +2192,30 @@ static inline int io_submit_sqe(struct io_ring_ctx *ctx, struct io_kiocb *req, fallback:io_queue_sqe_fallback (req); } -return 0 ; +return NULL ; } +return req; +} + +static inline int io_submit_sqe (struct io_ring_ctx *ctx,struct io_kiocb *req, +const struct io_uring_sqe *sqe) + __must_hold(&ctx->uring_lock) +{ +struct io_submit_link *link = &ctx->submit_state.link; +int ret; + ret =io_init_req (ctx, req, sqe); +if (unlikely (ret)) +return io_submit_fail_init (sqe, req, ret); + +trace_io_uring_submit_req (req); + +if (unlikely (link->head || (req->flags & (IO_REQ_LINK_FLAGS | +REQ_F_FORCE_ASYNC |REQ_F_FAIL )))) { + req =io_link_sqe (link, req); +if (!req) +return 0 ; + }io_queue_sqe (req);return 0 ; }
Signature · 1 line
2.46.0
PATCH v7 2/7 io_uring: add io_submit_fail_link() helper
In reply to Ming LeiAdd io_submit_fail_link() helper and put linking fail logic into this helper.
This way simplifies io_submit_fail_init(), and becomes easier to add sqe group failing logic.
--- io_uring/io_uring.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-)
io_uring/io_uring.c +16 -6
@@ -2121,22 +2121,17 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,return def->prep (req, sqe); } -static __cold int io_submit_fail_init (const struct io_uring_sqe *sqe, +static __cold int io_submit_fail_link (struct io_submit_link *link,struct io_kiocb *req,int ret) { -struct io_ring_ctx *ctx = req->ctx; -struct io_submit_link *link = &ctx->submit_state.link;struct io_kiocb *head = link->head; -trace_io_uring_req_failed (sqe, req, ret); -/* * Avoid breaking links in the middle as it renders links with SQPOLL * unusable. Instead of failing eagerly, continue assembling the link if * applicable and mark the head with REQ_F_FAIL. The link flushing code * should find the flag and handle the rest. */ -req_fail_link_node (req, ret);if (head && !(head->flags &REQ_F_FAIL ))req_fail_link_node (head, -ECANCELED );
@@ -2155,9 +2150,24 @@ static __cold int io_submit_fail_init(const struct io_uring_sqe *sqe,else link->head = req; link->last = req; +return 0 ; } +static __cold int io_submit_fail_init (const struct io_uring_sqe *sqe, +struct io_kiocb *req,int ret) +{ +struct io_ring_ctx *ctx = req->ctx; +struct io_submit_link *link = &ctx->submit_state.link; + +trace_io_uring_req_failed (sqe, req, ret); + +req_fail_link_node (req, ret); + +/* cover both linked and non-linked request */ +return io_submit_fail_link (link, req, ret); +} +/* * Return NULL if nothing to be queued, otherwise return request for queueing */ static struct io_kiocb *io_link_sqe(struct io_submit_link *link,
Signature · 1 line
2.46.0
PATCH v7 3/7 io_uring: add helper of io_req_commit_cqe()
In reply to Ming LeiAdd helper of io_req_commit_cqe() for simplifying__io_submit_flush_completions() a bit.
No functional change, and the added helper will be reused in sqe group code with same lock rule.
--- io_uring/io_uring.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-)
io_uring/io_uring.c +16 -10
@@ -887,6 +887,20 @@ bool io_req_post_cqe(struct io_kiocb *req, s32 res, u32 cflags)return posted; } +static __always_inline void io_req_commit_cqe(struct io_ring_ctx *ctx, +struct io_kiocb *req) +{ +if (unlikely (!io_fill_cqe_req (ctx, req))) { +if (ctx->lockless_cq) { +spin_lock (&ctx->completion_lock); +io_req_cqe_overflow (req); +spin_unlock (&ctx->completion_lock); + }else { +io_req_cqe_overflow (req); + } + } +} +static void io_req_complete_post (struct io_kiocb *req,unsigned issue_flags) {struct io_ring_ctx *ctx = req->ctx;
@@ -1439,16 +1453,8 @@ void __io_submit_flush_completions(struct io_ring_ctx *ctx) struct io_kiocb *req = container_of(node, struct io_kiocb, comp_list); -if (!(req->flags &REQ_F_CQE_SKIP ) && -unlikely (!io_fill_cqe_req (ctx, req))) { -if (ctx->lockless_cq) { -spin_lock (&ctx->completion_lock); -io_req_cqe_overflow (req); -spin_unlock (&ctx->completion_lock); - }else { -io_req_cqe_overflow (req); - } - } +if (!(req->flags &REQ_F_CQE_SKIP )) +io_req_commit_cqe (ctx, req); }__io_cq_unlock_post (ctx);
Signature · 1 line
2.46.0
PATCH v7 4/7 io_uring: support SQE group
In reply to Ming LeiSQE group is defined as one chain of SQEs starting with the first SQE that has IOSQE_SQE_GROUP set, and ending with the first subsequent SQE that doesn't have it set, and it is similar with chain of linked SQEs.
Not like linked SQEs, each sqe is issued after the previous one is completed. All SQEs in one group can be submitted in parallel. To simplify the implementation from beginning, all members are queued after the leader is completed, however, this way may be changed and leader and members may be issued concurrently in future.
The 1st SQE is group leader, and the other SQEs are group member. The whole group share single IOSQE_IO_LINK and IOSQE_IO_DRAIN from group leader, and the two flags can't be set for group members. For the sake of simplicity, IORING_OP_LINK_TIMEOUT is disallowed for SQE group now.
When the group is in one link chain, this group isn't submitted until the previous SQE or group is completed. And the following SQE or group can't be started if this group isn't completed. Failure from any group member will fail the group leader, then the link chain can be terminated.
When IOSQE_IO_DRAIN is set for group leader, all requests in this group and previous requests submitted are drained. Given IOSQE_IO_DRAIN can be set for group leader only, we respect IO_DRAIN by always completing group leader as the last one in the group. Meantime it is natural to post leader's CQE as the last one from application viewpoint.
Working together with IOSQE_IO_LINK, SQE group provides flexible way to support N:M dependency, such as:
- group A is chained with group B together
- group A has N SQEs
- group B has M SQEs
then M SQEs in group B depend on N SQEs in group A.
N:M dependency can support some interesting use cases in efficient way:
1) read from multiple files, then write the read data into single file
2) read from single file, and write the read data into multiple files
3) write same data into multiple files, and read data from multiple files and compare if correct data is written
Also IOSQE_SQE_GROUP takes the last bit in sqe->flags, but we still can extend sqe->flags with io_uring context flag, such as use __pad3 for non-uring_cmd OPs and part of uring_cmd_flags for uring_cmd OP.
--- include/linux/io_uring_types.h | 18 ++ include/uapi/linux/io_uring.h | 4 + io_uring/io_uring.c | 300 +++++++++++++++++++++++++++++++-- io_uring/io_uring.h | 6 + io_uring/timeout.c | 6 + 5 files changed, 319 insertions(+), 15 deletions(-)
include/linux/io_uring_types.h +18 -0
@@ -202,6 +202,8 @@ struct io_submit_state {/* batch completion logic */ struct io_wq_work_list compl_reqs;struct io_submit_link link; +/* points to current group */ +struct io_submit_link group;bool plug_started;bool need_plug;
@@ -438,6 +440,7 @@ enum {REQ_F_FORCE_ASYNC_BIT =IOSQE_ASYNC_BIT ,REQ_F_BUFFER_SELECT_BIT =IOSQE_BUFFER_SELECT_BIT ,REQ_F_CQE_SKIP_BIT =IOSQE_CQE_SKIP_SUCCESS_BIT , +REQ_F_SQE_GROUP_BIT =IOSQE_SQE_GROUP_BIT ,/* first byte is taken by user flags, shift it to not overlap */ REQ_F_FAIL_BIT = 8,
@@ -468,6 +471,7 @@ enum {REQ_F_BL_EMPTY_BIT ,REQ_F_BL_NO_RECYCLE_BIT ,REQ_F_BUFFERS_COMMIT_BIT , +REQ_F_SQE_GROUP_LEADER_BIT ,/* not a real bit, just to check we're not overflowing the space */ __REQ_F_LAST_BIT,
@@ -491,6 +495,8 @@ enum {REQ_F_BUFFER_SELECT =IO_REQ_FLAG (REQ_F_BUFFER_SELECT_BIT ),/* IOSQE_CQE_SKIP_SUCCESS */ REQ_F_CQE_SKIP =IO_REQ_FLAG (REQ_F_CQE_SKIP_BIT ), +/* IOSQE_SQE_GROUP */ +REQ_F_SQE_GROUP =IO_REQ_FLAG (REQ_F_SQE_GROUP_BIT ),/* fail rest of links */ REQ_F_FAIL = IO_REQ_FLAG(REQ_F_FAIL_BIT),
@@ -546,6 +552,8 @@ enum {REQ_F_BL_NO_RECYCLE =IO_REQ_FLAG (REQ_F_BL_NO_RECYCLE_BIT ),/* buffer ring head needs incrementing on put */ REQ_F_BUFFERS_COMMIT =IO_REQ_FLAG (REQ_F_BUFFERS_COMMIT_BIT ), +/* sqe group lead */ + REQ_F_SQE_GROUP_LEADER = IO_REQ_FLAG(REQ_F_SQE_GROUP_LEADER_BIT), };typedef void (*io_req_tw_func_t )(struct io_kiocb *req,struct io_tw_state *ts);
@@ -651,6 +659,8 @@ struct io_kiocb {void *async_data;/* linked requests, IFF REQ_F_HARDLINK or REQ_F_LINK are set */ atomic_t poll_refs; +/* reference for group leader request */ +int grp_refs;struct io_kiocb *link;/* custom credentials, valid IFF REQ_F_CREDS is set */ const struct cred *creds;
@@ -660,6 +670,14 @@ struct io_kiocb {u64 extra1;u64 extra2; } big_cqe; + +union { +/* links all group members for leader */ +struct io_kiocb *grp_link; + +/* points to group leader for member */ +struct io_kiocb *grp_leader; + }; };struct io_overflow_cqe {
include/uapi/linux/io_uring.h +4 -0
@@ -124,6 +124,7 @@ enum io_uring_sqe_flags_bit {IOSQE_ASYNC_BIT ,IOSQE_BUFFER_SELECT_BIT ,IOSQE_CQE_SKIP_SUCCESS_BIT , + IOSQE_SQE_GROUP_BIT, };/*
@@ -143,6 +144,8 @@ enum io_uring_sqe_flags_bit {#define IOSQE_BUFFER_SELECT (1U << IOSQE_BUFFER_SELECT_BIT)/* don't post CQE if request succeeded */ #define IOSQE_CQE_SKIP_SUCCESS (1U << IOSQE_CQE_SKIP_SUCCESS_BIT) +/* defines sqe group */ +#define IOSQE_SQE_GROUP (1U << IOSQE_SQE_GROUP_BIT)/* * io_uring_setup() flags
@@ -554,6 +557,7 @@ struct io_uring_params {#define IORING_FEAT_REG_REG_RING (1U << 13)#define IORING_FEAT_RECVSEND_BUNDLE (1U << 14)#define IORING_FEAT_MIN_TIMEOUT (1U << 15) +#define IORING_FEAT_SQE_GROUP (1U << 16)/* * io_uring_register(2) opcodes and arguments
io_uring/io_uring.c +285 -15
398 lines not on this page · raw message
io_uring/io_uring.h +6 -0
@@ -72,6 +72,7 @@ bool io_post_aux_cqe(struct io_ring_ctx *ctx, u64 user_data, s32 res, u32 cflagsvoid io_add_aux_cqe (struct io_ring_ctx *ctx,u64 user_data,s32 res,u32 cflags);bool io_req_post_cqe (struct io_kiocb *req,s32 res,u32 cflags);void __io_commit_cqring_flush (struct io_ring_ctx *ctx); +void io_cancel_group_members (struct io_kiocb *req,bool ignore_cqes);struct file *io_file_get_normal (struct io_kiocb *req,int fd); struct file *io_file_get_fixed(struct io_kiocb *req, int fd,
@@ -344,6 +345,11 @@ static inline void io_tw_lock(struct io_ring_ctx *ctx, struct io_tw_state *ts)lockdep_assert_held (&ctx->uring_lock); } +static inline bool req_is_group_leader (struct io_kiocb *req) +{ +return req->flags &REQ_F_SQE_GROUP_LEADER ; +} +/* * Don't complete immediately but use deferred completion infrastructure. * Protected by ->uring_lock and can only be used either with
io_uring/timeout.c +6 -0
@@ -168,6 +168,8 @@ static void io_fail_links(struct io_kiocb *req) link->flags |=REQ_F_CQE_SKIP ; else link->flags &= ~REQ_F_CQE_SKIP; +if (req_is_group_leader (link)) +io_cancel_group_members (link, ignore_cqes);trace_io_uring_fail_link (req, link); link = link->link; }
@@ -543,6 +545,10 @@ static int __io_timeout_prep(struct io_kiocb *req,if (is_timeout_link) {struct io_submit_link *link = &req->ctx->submit_state.link; +/* so far disallow IO group link timeout */ +if (req->ctx->submit_state.group.head) +return -EINVAL ; +if (!link->head)return -EINVAL ;if (link->last->opcode ==IORING_OP_LINK_TIMEOUT )
Signature · 1 line
2.46.0
PATCH v7 5/7 io_uring: support leased group buffer with REQ_F_GROUP_KBUF
In reply to Ming LeiSQE group introduces one new mechanism to share resource among one group of requests, and all member requests can consume the resource leased by group leader efficiently in parallel.
This patch uses the added SQE group to lease kernel buffer from group leader(driver) to members(io_uring) in sqe group:
- this kernel buffer is owned by kernel device(driver), and has very short lifetime, such as, it is often aligned with block IO lifetime
- group leader leases the kernel buffer from driver to member requests of io_uring subsystem
- member requests uses the leased buffer to do FS or network IO, or more operations in future; IOSQE_IO_DRAIN bit isn't used for group member IO, so it is mapped to GROUP_KBUF; the actual use becomes very similar with buffer select.
- this kernel buffer is returned back after all member requests consume it
io_uring builtin provide/register buffer isn't one good match for this use case:
- complicated dependency on add/remove buffer
this buffer has to be added/removed to one global table by add/remove OPs, and all consumer OPs have to sync with the add/remove OPs; either consumer OPs have to by issued one by one with IO_LINK; or two extra syscall are added for one time of buffer consumption, this way slows down ublk io handling, and may lose zero copy value
- application becomes more complicated
- application may panic and the kernel buffer is left in io_uring, which complicates io_uring shutdown handling since returning back buffer needs to cowork with buffer owner
- big change is needed in io_uring provide/register buffer
- the requirement is just to lease the kernel buffer to io_uring subsystem for very short time, not necessary to move it into io_uring and make it global
This way looks a bit similar with kernel's pipe/splice, but there are some important differences:
- splice is for transferring data between two FDs via pipe, and fd_out can only read data from pipe, but data can't be written to; this feature can lease buffer from group leader(driver subsystem) to members(io_uring subsystem), so member request can write data to this buffer if the buffer direction is allowed to write to.
- splice implements data transfer by moving pages between subsystem and pipe, that means page ownership is transferred, and this way is one of the most complicated thing of splice; this patch supports scenarios in which the buffer can't be transferred, and buffer is only borrowed to member requests for consumption, and is returned back after member requests consume the leased buffer, so buffer lifetime is aligned with group leader lifetime, and buffer lifetime is simplified a lot. Especially the buffer is guaranteed to be returned back.
- splice can't run in async way basically
It can help to implement generic zero copy between device and related operations, such as ublk, fuse, vdpa.
--- include/linux/io_uring_types.h | 40 +++++++++++++++++++++++ io_uring/io_uring.c | 25 +++++++++++---- io_uring/io_uring.h | 5 +++ io_uring/kbuf.c | 58 ++++++++++++++++++++++++++++++++++ io_uring/kbuf.h | 22 +++++++++++++ io_uring/net.c | 23 +++++++++++++- io_uring/rw.c | 21 +++++++++++- 7 files changed, 186 insertions(+), 8 deletions(-)
include/linux/io_uring_types.h +40 -0
@@ -6,6 +6,7 @@#include <linux/task_work.h> #include <linux/bitmap.h> #include <linux/llist.h> +#include <linux/bvec.h> #include <uapi/linux/io_uring.h> enum {
@@ -39,6 +40,26 @@ enum io_uring_cmd_flags { IO_URING_F_COMPAT = (1 << 12), }; +struct io_uring_kernel_buf ; +typedef void (io_uring_buf_giveback_t ) (const struct io_uring_kernel_buf *); + +/* kernel owned buffer, leased to io_uring OPs */ +struct io_uring_kernel_buf { +unsigned long len; +unsigned short nr_bvecs; +unsigned char dir;/* ITER_SOURCE or ITER_DEST */ + +/* offset in the 1st bvec */ +unsigned int offset; +const struct bio_vec *bvec; + +/* called when we are done with this buffer */ +io_uring_buf_giveback_t *grp_kbuf_ack; + +/* private field, user don't touch it */ +struct bio_vec __bvec[]; +}; +struct io_wq_work_node {struct io_wq_work_node *next; };
@@ -472,6 +493,7 @@ enum {REQ_F_BL_NO_RECYCLE_BIT ,REQ_F_BUFFERS_COMMIT_BIT ,REQ_F_SQE_GROUP_LEADER_BIT , +REQ_F_GROUP_KBUF_BIT ,/* not a real bit, just to check we're not overflowing the space */ __REQ_F_LAST_BIT,
@@ -554,6 +576,15 @@ enum {REQ_F_BUFFERS_COMMIT =IO_REQ_FLAG (REQ_F_BUFFERS_COMMIT_BIT ),/* sqe group lead */ REQ_F_SQE_GROUP_LEADER =IO_REQ_FLAG (REQ_F_SQE_GROUP_LEADER_BIT ), +/* +* Group leader leases kbuf to io_uring. Set for leader when the +* leader starts to lease kbuf, and set for member in case that +* the member needs to consume the group kbuf +* +* For group member, this flag is mapped from IOSQE_IO_DRAIN which +* isn't used for group members +*/ + REQ_F_GROUP_KBUF = IO_REQ_FLAG(REQ_F_GROUP_KBUF_BIT), };typedef void (*io_req_tw_func_t )(struct io_kiocb *req,struct io_tw_state *ts);
@@ -637,6 +668,15 @@ struct io_kiocb {* REQ_F_BUFFER_RING is set. */ struct io_buffer_list *buf_list; + +/* +* store kernel buffer leased from sqe group lead, valid +* IFF REQ_F_GROUP_KBUF is set +* +* The buffer meta is immutable since it is shared by +* all member requests +*/ +const struct io_uring_kernel_buf *grp_kbuf; };union {
io_uring/io_uring.c +19 -6
@@ -116,7 +116,7 @@#define IO_REQ_CLEAN_FLAGS (REQ_F_BUFFER_SELECTED | REQ_F_NEED_CLEANUP | \ REQ_F_POLLED | REQ_F_INFLIGHT | REQ_F_CREDS | \ - REQ_F_ASYNC_DATA) + REQ_F_ASYNC_DATA | REQ_F_GROUP_KBUF)#define IO_REQ_CLEAN_SLOW_FLAGS (REQ_F_REFCOUNT | REQ_F_LINK | REQ_F_HARDLINK |\ REQ_F_SQE_GROUP | REQ_F_SQE_GROUP_LEADER | \
@@ -387,6 +387,8 @@ static bool req_need_defer(struct io_kiocb *req, u32 seq)static void io_clean_op (struct io_kiocb *req) { +if (req->flags &REQ_F_GROUP_KBUF ) +io_drop_leased_grp_kbuf (req);if (req->flags &REQ_F_BUFFER_SELECTED ) {spin_lock (&req->ctx->completion_lock);io_kbuf_drop (req);
@@ -981,8 +983,13 @@ static void io_complete_group_member(struct io_kiocb *req)if (!(req->flags &REQ_F_CQE_SKIP ))io_req_commit_cqe (req->ctx, req); -/* clear it so it can be reused for marking last member */ - req->flags &= ~REQ_F_SQE_GROUP ; +/* +* Clear GROUP so it can be reused for marking last member +* +* Clear GROUP_KBUF since we are done with provided group +* buffer now if there is +*/ + req->flags &= ~(REQ_F_SQE_GROUP |REQ_F_GROUP_KBUF );/* Set leader as failed in case of any member failed */ if (unlikely ((req->flags &REQ_F_FAIL )))
@@ -2222,9 +2229,15 @@ static int io_init_req(struct io_ring_ctx *ctx, struct io_kiocb *req,if (sqe_flags &IOSQE_CQE_SKIP_SUCCESS ) ctx->drain_disabled = true;if (sqe_flags &IOSQE_IO_DRAIN ) { -if (ctx->drain_disabled) -return io_init_fail_req (req, -EOPNOTSUPP ); -io_init_req_drain (req); +/* IO_DRAIN is mapped to GROUP_KBUF for group members */ +if (ctx->submit_state.group.head) { + req->flags &= ~REQ_F_IO_DRAIN ; + req->flags |=REQ_F_GROUP_KBUF ; + }else { +if (ctx->drain_disabled) +return io_init_fail_req (req, -EOPNOTSUPP ); +io_init_req_drain (req); + } } }if (unlikely (ctx->restricted || ctx->drain_active || ctx->drain_next)) {
io_uring/io_uring.h +5 -0
@@ -350,6 +350,11 @@ static inline bool req_is_group_leader(struct io_kiocb *req)return req->flags &REQ_F_SQE_GROUP_LEADER ; } +static inline bool req_is_group_member (struct io_kiocb *req) +{ +return !req_is_group_leader (req) && (req->flags &REQ_F_SQE_GROUP ); +} +/* * Don't complete immediately but use deferred completion infrastructure. * Protected by ->uring_lock and can only be used either with
io_uring/kbuf.c +58 -0
61 lines not on this page · raw message
io_uring/kbuf.h +22 -0
@@ -89,6 +89,11 @@ struct io_buffer_list *io_pbuf_get_bl(struct io_ring_ctx *ctx, unsigned long bgid);int io_pbuf_mmap (struct file *file,struct vm_area_struct *vma); +int io_lease_group_kbuf (struct io_kiocb *req, +const struct io_uring_kernel_buf *grp_kbuf); +int io_import_group_kbuf (struct io_kiocb *req,unsigned long buf_off, +unsigned int len,int dir,struct iov_iter *iter); +static inline bool io_kbuf_recycle_ring (struct io_kiocb *req) {/*
@@ -220,4 +225,21 @@ static inline unsigned int io_put_kbufs(struct io_kiocb *req, int len, {return __io_put_kbufs (req, len, nbufs, issue_flags); } + +static inline bool io_use_leased_grp_kbuf (struct io_kiocb *req) +{ +/* can't use group kbuf in case of buffer select or fixed buffer */ +if (req->flags &REQ_F_BUFFER_SELECT ) +return false; + +return req->flags &REQ_F_GROUP_KBUF ; +} + +static inline void io_drop_leased_grp_kbuf (struct io_kiocb *req) +{ +const struct io_uring_kernel_buf *gbuf = req->grp_kbuf; + +if (gbuf) + gbuf->grp_kbuf_ack (gbuf); +}#endif
io_uring/net.c +22 -1
@@ -89,6 +89,13 @@ struct io_sr_msg {*/ #define MULTISHOT_MAX_RETRY 32 +#define user_ptr_to_u64 (x) ( \ +{ \ + typecheck(void __user *, (x)); \ + (u64)(unsigned long)(x); \ +} \ +) +int io_shutdown_prep (struct io_kiocb *req,const struct io_uring_sqe *sqe) { struct io_shutdown *shutdown = io_kiocb_to_cmd(req, struct io_shutdown);
@@ -375,7 +382,7 @@ static int io_send_setup(struct io_kiocb *req) kmsg->msg.msg_name = &kmsg->addr; kmsg->msg.msg_namelen = sr->addr_len; } -if (!io_do_buffer_select (req)) { +if (!io_do_buffer_select (req) && !io_use_leased_grp_kbuf (req)) { ret =import_ubuf (ITER_SOURCE , sr->buf, sr->len, &kmsg->msg.msg_iter);if (unlikely (ret <0 ))
@@ -593,6 +600,15 @@ int io_send(struct io_kiocb *req, unsigned int issue_flags)if (issue_flags &IO_URING_F_NONBLOCK ) flags |=MSG_DONTWAIT ; +if (io_use_leased_grp_kbuf (req)) { + ret =io_import_group_kbuf (req, +user_ptr_to_u64 (sr->buf), + sr->len,ITER_SOURCE , + &kmsg->msg.msg_iter); +if (unlikely (ret)) +return ret; + } + retry_bundle:if (io_do_buffer_select (req)) {struct buf_sel_arg arg = {
@@ -1154,6 +1170,11 @@ int io_recv(struct io_kiocb *req, unsigned int issue_flags) goto out_free; } sr->buf =NULL ; + }else if (io_use_leased_grp_kbuf (req)) { + ret =io_import_group_kbuf (req,user_ptr_to_u64 (sr->buf), + sr->len,ITER_DEST , &kmsg->msg.msg_iter); +if (unlikely (ret)) + goto out_free; } kmsg->msg.msg_flags =0 ;
io_uring/rw.c +20 -1
@@ -235,7 +235,8 @@ static int io_prep_rw_setup(struct io_kiocb *req, int ddir, bool do_import)if (io_rw_alloc_async (req))return -ENOMEM ; -if (!do_import ||io_do_buffer_select (req)) +if (!do_import ||io_do_buffer_select (req) || +io_use_leased_grp_kbuf (req))return 0 ; rw = req->async_data;
@@ -619,11 +620,16 @@ static inline loff_t *io_kiocb_ppos(struct kiocb *kiocb)*/ static ssize_t loop_rw_iter (int ddir,struct io_rw *rw,struct iov_iter *iter) { +struct io_kiocb *req =cmd_to_io_kiocb (rw);struct kiocb *kiocb = &rw->kiocb;struct file *file = kiocb->ki_filp;ssize_t ret =0 ;loff_t *ppos; +/* group buffer is kernel buffer and doesn't have userspace addr */ +if (io_use_leased_grp_kbuf (req)) +return -EOPNOTSUPP ; +/* * Don't support polled IO through this interface, and we can't * support non-blocking either. For the latter, this just causes
@@ -830,6 +836,12 @@ static int __io_read(struct io_kiocb *req, unsigned int issue_flags) ret =io_import_iovec (ITER_DEST , req, io, issue_flags);if (unlikely (ret <0 ))return ret; + }else if (io_use_leased_grp_kbuf (req)) { + ret =io_import_group_kbuf (req, rw->addr, rw->len,ITER_DEST , + &io->iter); +if (unlikely (ret)) +return ret; +iov_iter_save_state (&io->iter, &io->iter_state); } ret =io_rw_init_file (req,FMODE_READ ,READ );if (unlikely (ret))
@@ -1019,6 +1031,13 @@ int io_write(struct io_kiocb *req, unsigned int issue_flags)ssize_t ret, ret2;loff_t *ppos; +if (io_use_leased_grp_kbuf (req)) { + ret =io_import_group_kbuf (req, rw->addr, rw->len,ITER_SOURCE , + &io->iter); +if (unlikely (ret)) +return ret; + } + ret =io_rw_init_file (req,FMODE_WRITE ,WRITE );if (unlikely (ret))return ret;
Signature · 1 line
2.46.0
Re: [PATCH V7 5/7] io_uring: support leased group buffer with REQ_F_GROUP_KBUF
io_uring/kbuf.c int io_pbuf_mmap(struct file *file, struct vm_area_struct *vma) · line 869
Quoted 4 lines · Ming Lei
io_uring/kbuf.c +4
-0+int io_import_group_kbuf (struct io_kiocb *req,unsigned long buf_off, +unsigned int len,int dir,struct iov_iter *iter) +{ +struct io_kiocb *lead = req->grp_link;
This works since grp_link and grp_leader are in a union together, but this should really be req->grp_leader, right?
Re: [PATCH V7 5/7] io_uring: support leased group buffer with REQ_F_GROUP_KBUF
Quoted 8 lines · Uday Shankar
io_uring/kbuf.c int io_pbuf_mmap(struct file *file, struct vm_area_struct *vma) · line 869
Quoted 4 lines · Ming Lei
io_uring/kbuf.c +4
-0+int io_import_group_kbuf (struct io_kiocb *req,unsigned long buf_off, +unsigned int len,int dir,struct iov_iter *iter) +{ +struct io_kiocb *lead = req->grp_link;This works since
grp_linkandgrp_leaderare in a union together, but this should really bereq->grp_leader, right?
Yeah, will fix it in V8.
Thanks,
Ming
PATCH v7 6/7 io_uring/uring_cmd: support leasing device kernel buffer to io_uring
In reply to Ming LeiAdd API of io_uring_cmd_lease_kbuf() for driver to lease its kernel buffer to io_uring.
The leased buffer can only be consumed by io_uring OPs in group wide, and the uring_cmd has to be one group leader.
This way can support generic device zero copy over device buffer in userspace:
- create one sqe group
- lease one device buffer to io_uring by the group leader of uring_cmd
- io_uring member OPs consume this kernel buffer by passing IOSQE_IO_DRAIN which isn't used for group member, and mapped to GROUP_KBUF.
- the kernel buffer is returned back after all member OPs are completed
--- include/linux/io_uring/cmd.h | 7 +++++++ io_uring/uring_cmd.c | 13 +++++++++++++ 2 files changed, 20 insertions(+)
include/linux/io_uring/cmd.h +7 -0
@@ -60,6 +60,8 @@ void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd,/* Execute the request from a blocking context */ void io_uring_cmd_issue_blocking (struct io_uring_cmd *ioucmd); +int io_uring_cmd_lease_kbuf (struct io_uring_cmd *ioucmd, +const struct io_uring_kernel_buf *grp_kbuf);#else static inline int io_uring_cmd_import_fixed (u64 ubuf,unsigned long len,int rw,struct iov_iter *iter,void *ioucmd)
@@ -82,6 +84,11 @@ static inline void io_uring_cmd_mark_cancelable(struct io_uring_cmd *cmd,static inline void io_uring_cmd_issue_blocking (struct io_uring_cmd *ioucmd) { } +static inline int io_uring_cmd_lease_kbuf (struct io_uring_cmd *ioucmd, +const struct io_uring_kernel_buf *grp_kbuf) +{ +return -EOPNOTSUPP ; +}#endif /*
io_uring/uring_cmd.c +13 -0
@@ -15,6 +15,7 @@#include "alloc_cache.h" #include "rsrc.h" #include "uring_cmd.h" +#include "kbuf.h" static struct uring_cache *io_uring_async_get (struct io_kiocb *req) {
@@ -175,6 +176,18 @@ void io_uring_cmd_done(struct io_uring_cmd *ioucmd, ssize_t ret, ssize_t res2, }EXPORT_SYMBOL_GPL (io_uring_cmd_done); +int io_uring_cmd_lease_kbuf (struct io_uring_cmd *ioucmd, +const struct io_uring_kernel_buf *grp_kbuf) +{ +struct io_kiocb *req =cmd_to_io_kiocb (ioucmd); + +if (unlikely (ioucmd->flags &IORING_URING_CMD_FIXED )) +return -EINVAL ; + +return io_lease_group_kbuf (req, grp_kbuf); +} +EXPORT_SYMBOL_GPL (io_uring_cmd_lease_kbuf); +static int io_uring_cmd_prep_setup (struct io_kiocb *req,const struct io_uring_sqe *sqe) {
Signature · 1 line
2.46.0
PATCH v7 7/7 ublk: support lease io buffer to io_uring
In reply to Ming LeiSuopport to lease block IO buffer for userpace to run io_uring operations(FS, network IO), then ublk zero copy can be supported.
userspace code:
git clone https://github.com/ublk-org/ublksrv.git -b uring_group
And both loop and nbd zero copy(io_uring send and send zc) are covered.
Performance improvement is quite obvious in big block size test, such as 'loop --buffered_io' perf is doubled in 64KB block test("loop/007 vs loop/009").
--- drivers/block/ublk_drv.c | 157 ++++++++++++++++++++++++++++++++-- include/uapi/linux/ublk_cmd.h | 7 +- 2 files changed, 153 insertions(+), 11 deletions(-)
drivers/block/ublk_drv.c +150 -7
@@ -51,6 +51,8 @@/* private ioctl command mirror */ #define UBLK_CMD_DEL_DEV_ASYNC _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC) +#define UBLK_IO_PROVIDE_IO_BUF _IOC_NR(UBLK_U_IO_PROVIDE_IO_BUF) +/* All UBLK_F_* have to be included into UBLK_F_ALL */ #define UBLK_F_ALL (UBLK_F_SUPPORT_ZERO_COPY \ | UBLK_F_URING_CMD_COMP_IN_TASK \
@@ -71,6 +73,9 @@ struct ublk_rq_data {struct llist_node node;struct kref ref; + +bool allocated_bvec; +struct io_uring_kernel_buf buf[0 ]; };struct ublk_uring_cmd_pdu {
@@ -189,11 +194,15 @@ struct ublk_params_header {__u32 types; }; +static inline struct request *__ublk_check_and_get_req (struct ublk_device *ub, +struct ublk_queue *ubq,int tag,size_t offset);static bool ublk_abort_requests (struct ublk_device *ub,struct ublk_queue *ubq);static inline unsigned int ublk_req_build_flags (struct request *req);static inline struct ublksrv_io_desc *ublk_get_iod (struct ublk_queue *ubq,int tag); +static void ublk_io_buf_giveback_cb (const struct io_uring_kernel_buf *buf); +static inline bool ublk_dev_is_user_copy (const struct ublk_device *ub) {return ub->dev_info.flags &UBLK_F_USER_COPY ;
@@ -588,6 +597,11 @@ static inline bool ublk_need_req_ref(const struct ublk_queue *ubq)return ublk_support_user_copy (ubq); } +static inline bool ublk_support_zc (const struct ublk_queue *ubq) +{ +return ubq->flags &UBLK_F_SUPPORT_ZERO_COPY ; +} +static inline void ublk_init_req_ref (const struct ublk_queue *ubq,struct request *req) {
@@ -851,6 +865,71 @@ static size_t ublk_copy_user_pages(const struct request *req,return done; } +/* +* The built command buffer is immutable, so it is fine to feed it to +* concurrent io_uring provide buf commands +*/ +static int ublk_init_zero_copy_buffer (struct request *req) +{ +struct ublk_rq_data *data =blk_mq_rq_to_pdu (req); +struct io_uring_kernel_buf *imu = data->buf; +struct req_iterator rq_iter; +unsigned int nr_bvecs =0 ; +struct bio_vec *bvec; +unsigned int offset; +struct bio_vec bv; + +if (!ublk_rq_has_data (req)) + goto exit; + +rq_for_each_bvec (bv, req, rq_iter) + nr_bvecs++; + +if (!nr_bvecs) + goto exit; + +if (req->bio != req->biotail) { +int idx =0 ; + + bvec =kvmalloc_array (nr_bvecs,sizeof (struct bio_vec ), +GFP_NOIO ); +if (!bvec) +return -ENOMEM ; + + offset =0 ; +rq_for_each_bvec (bv, req, rq_iter) + bvec[idx++] = bv; + data->allocated_bvec = true; + }else { +struct bio *bio = req->bio; + + offset = bio->bi_iter.bi_bvec_done; + bvec =__bvec_iter_bvec (bio->bi_io_vec, bio->bi_iter); + } + imu->bvec = bvec; + imu->nr_bvecs = nr_bvecs; + imu->offset = offset; + imu->len =blk_rq_bytes (req); + imu->dir =req_op (req) ==REQ_OP_READ ?ITER_DEST :ITER_SOURCE ; + imu->grp_kbuf_ack = ublk_io_buf_giveback_cb; + +return 0 ; +exit: + imu->bvec =NULL ; +return 0 ; +} + +static void ublk_deinit_zero_copy_buffer (struct request *req) +{ +struct ublk_rq_data *data =blk_mq_rq_to_pdu (req); +struct io_uring_kernel_buf *imu = data->buf; + +if (data->allocated_bvec) { +kvfree (imu->bvec); + data->allocated_bvec = false; + } +} +static inline bool ublk_need_map_req (const struct request *req) {return ublk_rq_has_data (req) &&req_op (req) ==REQ_OP_WRITE ;
@@ -862,13 +941,25 @@ static inline bool ublk_need_unmap_req(const struct request *req) (req_op (req) ==REQ_OP_READ ||req_op (req) ==REQ_OP_DRV_IN ); } -static int ublk_map_io (const struct ublk_queue *ubq,const struct request *req, +static int ublk_map_io (const struct ublk_queue *ubq,struct request *req,struct ublk_io *io) {const unsigned int rq_bytes =blk_rq_bytes (req); -if (ublk_support_user_copy (ubq)) +if (ublk_support_user_copy (ubq)) { +if (ublk_support_zc (ubq)) { +int ret =ublk_init_zero_copy_buffer (req); + +/* +* The only failure is -ENOMEM for allocating providing +* buffer command, return zero so that we can requeue +* this req. +*/ +if (unlikely (ret)) +return 0 ; + }return rq_bytes; + }/* * no zero copy, we delay copy WRITE request data into ublksrv
@@ -886,13 +977,16 @@ static int ublk_map_io(const struct ublk_queue *ubq, const struct request *req, }static int ublk_unmap_io (const struct ublk_queue *ubq, -const struct request *req, +struct request *req,struct ublk_io *io) {const unsigned int rq_bytes =blk_rq_bytes (req); -if (ublk_support_user_copy (ubq)) +if (ublk_support_user_copy (ubq)) { +if (ublk_support_zc (ubq)) +ublk_deinit_zero_copy_buffer (req);return rq_bytes; + }if (ublk_need_unmap_req (req)) {struct iov_iter iter;
@@ -1038,6 +1132,7 @@ static inline void __ublk_complete_rq(struct request *req)return ; exit: +ublk_deinit_zero_copy_buffer (req);blk_mq_end_request (req, res); }
@@ -1680,6 +1775,45 @@ static inline void ublk_prep_cancel(struct io_uring_cmd *cmd,io_uring_cmd_mark_cancelable (cmd, issue_flags); } +static void ublk_io_buf_giveback_cb (const struct io_uring_kernel_buf *buf) +{ + struct ublk_rq_data *data = container_of(buf, struct ublk_rq_data, buf[0]); +struct request *req =blk_mq_rq_from_pdu (data); +struct ublk_queue *ubq = req->mq_hctx->driver_data; + +ublk_put_req_ref (ubq, req); +} + +static int ublk_provide_io_buf (struct io_uring_cmd *cmd, +struct ublk_queue *ubq,int tag) +{ +struct ublk_device *ub = cmd->file->private_data; +struct ublk_rq_data *data; +struct request *req; + +if (!ub) +return -EPERM ; + + req =__ublk_check_and_get_req (ub, ubq, tag,0 ); +if (!req) +return -EINVAL ; + +pr_devel ("%s: qid %d tag %u request bytes %u\n" , + __func__, tag, ubq->q_id,blk_rq_bytes (req)); + + data =blk_mq_rq_to_pdu (req); + +/* +* io_uring guarantees that the callback will be called after +* the provided buffer is consumed, and it is automatic removal +* before this uring command is freed. +* +* This request won't be completed unless the callback is called, +* so ublk module won't be unloaded too. +*/ +return io_uring_cmd_lease_kbuf (cmd, data->buf); +} +static int __ublk_ch_uring_cmd (struct io_uring_cmd *cmd,unsigned int issue_flags,const struct ublksrv_io_cmd *ub_cmd)
@@ -1731,6 +1865,8 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd, ret = -EINVAL ;switch (_IOC_NR (cmd_op)) { +case UBLK_IO_PROVIDE_IO_BUF : +return ublk_provide_io_buf (cmd, ubq, tag);case UBLK_IO_FETCH_REQ :/* UBLK_IO_FETCH_REQ is only allowed before queue is setup */ if (ublk_queue_ready (ubq)) {
@@ -2149,11 +2285,14 @@ static void ublk_align_max_io_size(struct ublk_device *ub)static int ublk_add_tag_set (struct ublk_device *ub) { +int zc = !!(ub->dev_info.flags &UBLK_F_SUPPORT_ZERO_COPY ); +struct ublk_rq_data *data; + ub->tag_set.ops = &ublk_mq_ops; ub->tag_set.nr_hw_queues = ub->dev_info.nr_hw_queues; ub->tag_set.queue_depth = ub->dev_info.queue_depth; ub->tag_set.numa_node =NUMA_NO_NODE ; - ub->tag_set.cmd_size =sizeof (struct ublk_rq_data ); + ub->tag_set.cmd_size =struct_size (data, buf, zc); ub->tag_set.flags =BLK_MQ_F_SHOULD_MERGE ; ub->tag_set.driver_data = ub;return blk_mq_alloc_tag_set (&ub->tag_set);
@@ -2449,8 +2588,12 @@ static int ublk_ctrl_add_dev(struct io_uring_cmd *cmd) goto out_free_dev_number; } -/* We are not ready to support zero copy */ - ub->dev_info.flags &= ~UBLK_F_SUPPORT_ZERO_COPY ; +/* zero copy depends on user copy */ +if ((ub->dev_info.flags &UBLK_F_SUPPORT_ZERO_COPY ) && + !ublk_dev_is_user_copy (ub)) { + ret = -EINVAL ; + goto out_free_dev_number; + } ub->dev_info.nr_hw_queues = min_t(unsigned int, ub->dev_info.nr_hw_queues, nr_cpu_ids);
include/uapi/linux/ublk_cmd.h +3 -4
@@ -94,6 +94,8 @@ _IOWR('u', UBLK_IO_COMMIT_AND_FETCH_REQ, struct ublksrv_io_cmd)#define UBLK_U_IO_NEED_GET_DATA \ _IOWR('u', UBLK_IO_NEED_GET_DATA, struct ublksrv_io_cmd) +#define UBLK_U_IO_PROVIDE_IO_BUF \ + _IOWR('u', 0x23, struct ublksrv_io_cmd)/* only ABORT means that no re-fetch */ #define UBLK_IO_RES_OK 0
@@ -126,10 +128,7 @@#define UBLKSRV_IO_BUF_TOTAL_BITS (UBLK_QID_OFF + UBLK_QID_BITS)#define UBLKSRV_IO_BUF_TOTAL_SIZE (1ULL << UBLKSRV_IO_BUF_TOTAL_BITS) -/* -* zero copy requires 4k block size, and can remap ublk driver's io -* request into ublksrv's vm space -*/ +/* io_uring provide kbuf command based zero copy */ #define UBLK_F_SUPPORT_ZERO_COPY (1ULL << 0)/*
Signature · 1 line
2.46.0