ublk zero-copy support
Subsystems (3)
v4 cover ublk zero-copy support
From: Keith Busch <[email protected]>
Changes from v3:
Fixed putting the imu back in the cache on free instead of releasing it to the system (Caleb)
Fixed the build bisect breakage (Caleb)
Use appropriate error value if cache initialization fails (Caleb)
Check data direction when importing the buffer (Ming)
Using the array_no_spec accessor when using a user index (Pavel)
Various cleanups
Keith Busch (5):
io_uring: move fixed buffer import to issue path
io_uring: add support for kernel registered bvecs
ublk: zc register/unregister bvec
io_uring: add abstraction for buf_table rsrc data
io_uring: cache nodes and mapped buffers
drivers/block/ublk_drv.c | 137 ++++++++++++----- include/linux/io_uring.h | 1 + include/linux/io_uring_types.h | 33 ++-- include/uapi/linux/ublk_cmd.h | 4 + io_uring/fdinfo.c | 8 +- io_uring/filetable.c | 2 +- io_uring/io_uring.c | 19 +++ io_uring/net.c | 25 +--- io_uring/nop.c | 22 +-- io_uring/register.c | 2 +- io_uring/rsrc.c | 266 ++++++++++++++++++++++++++------- io_uring/rsrc.h | 6 +- io_uring/rw.c | 45 ++++-- io_uring/uring_cmd.c | 16 +- 14 files changed, 419 insertions(+), 167 deletions(-)
Signature · 1 line
2.43.5
PATCH v4 2/5 io_uring: add support for kernel registered bvecs
From: Keith Busch <[email protected]>
Provide an interface for the kernel to leverage the existing pre-registered buffers that io_uring provides. User space can reference these later to achieve zero-copy IO.
User space must register an empty fixed buffer table with io_uring in order for the kernel to make use of it.
--- include/linux/io_uring.h | 1 + include/linux/io_uring_types.h | 6 ++ io_uring/rsrc.c | 115 ++++++++++++++++++++++++++++++--- io_uring/rsrc.h | 4 ++ 4 files changed, 118 insertions(+), 8 deletions(-)
include/linux/io_uring.h +1 -0
@@ -5,6 +5,7 @@#include <linux/sched.h> #include <linux/xarray.h> #include <uapi/linux/io_uring.h> +#include <linux/blk-mq.h> #if defined(CONFIG_IO_URING )void __io_uring_cancel (bool cancel_all);
include/linux/io_uring_types.h +6 -0
@@ -709,4 +709,10 @@ static inline bool io_ctx_cqe32(struct io_ring_ctx *ctx) return ctx->flags & IORING_SETUP_CQE32; } +int io_buffer_register_bvec (struct io_ring_ctx *ctx,struct request *rq, +void (*release)(void *),unsigned int index, +unsigned int issue_flags); +void io_buffer_unregister_bvec (struct io_ring_ctx *ctx,unsigned int index, +unsigned int issue_flags); +#endif
io_uring/rsrc.c +107 -8
160 lines not on this page · raw message
io_uring/rsrc.h +4 -0
@@ -33,6 +33,10 @@ struct io_mapped_ubuf {unsigned int folio_shift;refcount_t refs;unsigned long acct_pages; +void (*release)(void *); +void *priv; +bool readable; +bool writeable; struct bio_vec bvec[] __counted_by(nr_bvecs); };
Signature · 1 line
2.43.5
PATCH v4 1/5 io_uring: move fixed buffer import to issue path
In reply to Keith BuschFrom: Keith Busch <[email protected]>
Similar to the fixed file path, requests may depend on a previous one to set up an index, so we need to allow linking them. The prep callback happens too soon for linked commands, so the lookup needs to be deferred to the issue path. Change the prep callbacks to just set the buf_index and let generic io_uring code handle the fixed buffer node setup, just like it already does for fixed files.
--- include/linux/io_uring_types.h | 3 +++ io_uring/io_uring.c | 19 ++++++++++++++ io_uring/net.c | 25 ++++++------------- io_uring/nop.c | 22 +++-------------- io_uring/rw.c | 45 ++++++++++++++++++++++++---------- io_uring/uring_cmd.c | 16 ++---------- 6 files changed, 67 insertions(+), 63 deletions(-)
include/linux/io_uring_types.h +3 -0
@@ -482,6 +482,7 @@ enum {REQ_F_DOUBLE_POLL_BIT ,REQ_F_APOLL_MULTISHOT_BIT ,REQ_F_CLEAR_POLLIN_BIT , +REQ_F_FIXED_BUFFER_BIT ,/* keep async read/write and isreg together and in order */ REQ_F_SUPPORT_NOWAIT_BIT , REQ_F_ISREG_BIT,
@@ -574,6 +575,8 @@ enum {REQ_F_BUF_NODE =IO_REQ_FLAG (REQ_F_BUF_NODE_BIT ),/* request has read/write metadata assigned */ REQ_F_HAS_METADATA =IO_REQ_FLAG (REQ_F_HAS_METADATA_BIT ), +/* request has a fixed buffer at buf_index */ + REQ_F_FIXED_BUFFER = IO_REQ_FLAG(REQ_F_FIXED_BUFFER_BIT), };typedef void (*io_req_tw_func_t )(struct io_kiocb *req,io_tw_token_t tw);
io_uring/io_uring.c +19 -0
@@ -1721,6 +1721,23 @@ static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,return !!req->file; } +static bool io_assign_buffer (struct io_kiocb *req,unsigned int issue_flags) +{ +struct io_ring_ctx *ctx = req->ctx; +struct io_rsrc_node *node; + +if (req->buf_node || !(req->flags &REQ_F_FIXED_BUFFER )) +return true; + +io_ring_submit_lock (ctx, issue_flags); + node =io_rsrc_node_lookup (&ctx->buf_table, req->buf_index); +if (node) +io_req_assign_buf_node (req, node); +io_ring_submit_unlock (ctx, issue_flags); + +return !!node; +} +static int io_issue_sqe (struct io_kiocb *req,unsigned int issue_flags) {const struct io_issue_def *def = &io_issue_defs[req->opcode];
@@ -1729,6 +1746,8 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)if (unlikely (!io_assign_file (req, def, issue_flags)))return -EBADF ; +if (unlikely (!io_assign_buffer (req, issue_flags))) +return -EFAULT ;if (unlikely ((req->flags &REQ_F_CREDS ) && req->creds !=current_cred ())) creds =override_creds (req->creds);
io_uring/net.c +7 -18
@@ -1373,6 +1373,10 @@ int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)#endif if (unlikely (!io_msg_alloc_async (req)))return -ENOMEM ; +if (zc->flags &IORING_RECVSEND_FIXED_BUF ) { + req->buf_index = zc->buf_index; + req->flags |=REQ_F_FIXED_BUFFER ; + }if (req->opcode !=IORING_OP_SENDMSG_ZC )return io_send_setup (req, sqe);return io_sendmsg_setup (req, sqe);
@@ -1434,25 +1438,10 @@ static int io_send_zc_import(struct io_kiocb *req, unsigned int issue_flags)struct io_async_msghdr *kmsg = req->async_data;int ret; -if (sr->flags &IORING_RECVSEND_FIXED_BUF ) { -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node; - - ret = -EFAULT ; -io_ring_submit_lock (ctx, issue_flags); - node =io_rsrc_node_lookup (&ctx->buf_table, sr->buf_index); -if (node) { -io_req_assign_buf_node (sr->notif, node); - ret =0 ; - } -io_ring_submit_unlock (ctx, issue_flags); - -if (unlikely (ret)) -return ret; - +if (req->flags &REQ_F_FIXED_BUFFER ) { ret =io_import_fixed (ITER_SOURCE , &kmsg->msg.msg_iter, - node->buf, (u64 )(uintptr_t )sr->buf, - sr->len); + req->buf_node->buf, + (u64 )(uintptr_t )sr->buf, sr->len);if (unlikely (ret))return ret; kmsg->msg.sg_from_iter = io_sg_from_iter;
io_uring/nop.c +4 -18
@@ -16,7 +16,6 @@ struct io_nop {struct file *file;int result;int fd; -int buffer;unsigned int flags; };
@@ -39,10 +38,10 @@ int io_nop_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) nop->fd =READ_ONCE (sqe->fd); else nop->fd = -1; -if (nop->flags &IORING_NOP_FIXED_BUFFER ) - nop->buffer =READ_ONCE (sqe->buf_index); -else - nop->buffer =-1 ; +if (nop->flags &IORING_NOP_FIXED_BUFFER ) { + req->buf_index =READ_ONCE (sqe->buf_index); + req->flags |=REQ_F_FIXED_BUFFER ; + }return 0 ; }
@@ -63,19 +62,6 @@ int io_nop(struct io_kiocb *req, unsigned int issue_flags) goto done; } } -if (nop->flags &IORING_NOP_FIXED_BUFFER ) { -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node; - - ret = -EFAULT ; -io_ring_submit_lock (ctx, issue_flags); - node =io_rsrc_node_lookup (&ctx->buf_table, nop->buffer); -if (node) { -io_req_assign_buf_node (req, node); - ret =0 ; - } -io_ring_submit_unlock (ctx, issue_flags); - } done:if (ret <0 )req_set_fail (req);
io_uring/rw.c +32 -13
73 lines not on this page · raw message
io_uring/uring_cmd.c +2 -14
@@ -200,19 +200,8 @@ int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)return -EINVAL ;if (ioucmd->flags &IORING_URING_CMD_FIXED ) { -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node; -u16 index =READ_ONCE (sqe->buf_index); - - node =io_rsrc_node_lookup (&ctx->buf_table, index); -if (unlikely (!node)) -return -EFAULT ; -/* -* Pi node upfront, prior to io_uring_cmd_import_fixed() -* being called. This prevents destruction of the mapped buffer -* we'll need at actual import time. -*/ -io_req_assign_buf_node (req, node); + req->buf_index =READ_ONCE (sqe->buf_index); + req->flags |=REQ_F_FIXED_BUFFER ; } ioucmd->cmd_op =READ_ONCE (sqe->cmd_op);
@@ -262,7 +251,6 @@ int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,struct io_kiocb *req =cmd_to_io_kiocb (ioucmd);struct io_rsrc_node *node = req->buf_node; -/* Must have had rsrc_node assigned at prep time */ if (node)return io_import_fixed (rw, iter, node->buf, ubuf, len);
Signature · 1 line
2.43.5
PATCH v4 3/5 ublk: zc register/unregister bvec
In reply to Keith BuschFrom: Keith Busch <[email protected]>
Provide new operations for the user to request mapping an active request to an io uring instance's buf_table. The user has to provide the index it wants to install the buffer.
A reference count is taken on the request to ensure it can't be completed while it is active in a ring's buf_table.
--- drivers/block/ublk_drv.c | 137 +++++++++++++++++++++++++--------- include/uapi/linux/ublk_cmd.h | 4 + 2 files changed, 105 insertions(+), 36 deletions(-)
drivers/block/ublk_drv.c +101 -36
@@ -51,6 +51,9 @@/* private ioctl command mirror */ #define UBLK_CMD_DEL_DEV_ASYNC _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC) +#define UBLK_IO_REGISTER_IO_BUF _IOC_NR(UBLK_U_IO_REGISTER_IO_BUF) +#define UBLK_IO_UNREGISTER_IO_BUF _IOC_NR(UBLK_U_IO_UNREGISTER_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 \
@@ -201,7 +204,7 @@ static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq, int tag);static inline bool ublk_dev_is_user_copy (const struct ublk_device *ub) { -return ub->dev_info.flags &UBLK_F_USER_COPY ; +return ub->dev_info.flags & (UBLK_F_USER_COPY |UBLK_F_SUPPORT_ZERO_COPY ); }static inline bool ublk_dev_is_zoned (const struct ublk_device *ub)
@@ -581,7 +584,7 @@ static void ublk_apply_params(struct ublk_device *ub)static inline bool ublk_support_user_copy (const struct ublk_queue *ubq) { -return ubq->flags &UBLK_F_USER_COPY ; +return ubq->flags & (UBLK_F_USER_COPY |UBLK_F_SUPPORT_ZERO_COPY ); }static inline bool ublk_need_req_ref (const struct ublk_queue *ubq)
@@ -1747,6 +1750,96 @@ static inline void ublk_prep_cancel(struct io_uring_cmd *cmd,io_uring_cmd_mark_cancelable (cmd, issue_flags); } +static inline struct request *__ublk_check_and_get_req (struct ublk_device *ub, +struct ublk_queue *ubq,int tag,size_t offset) +{ +struct request *req; + +if (!ublk_need_req_ref (ubq)) +return NULL ; + + req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag); +if (!req) +return NULL ; + +if (!ublk_get_req_ref (ubq, req)) +return NULL ; + +if (unlikely (!blk_mq_request_started (req) || req->tag != tag)) + goto fail_put; + +if (!ublk_rq_has_data (req)) + goto fail_put; + +if (offset >blk_rq_bytes (req)) + goto fail_put; + +return req; +fail_put: +ublk_put_req_ref (ubq, req); +return NULL ; +} + +static void ublk_io_release (void *priv) +{ +struct request *rq = priv; +struct ublk_queue *ubq = rq->mq_hctx->driver_data; + +ublk_put_req_ref (ubq, rq); +} + +static int ublk_register_io_buf (struct io_uring_cmd *cmd, +struct ublk_queue *ubq,int tag, +const struct ublksrv_io_cmd *ub_cmd, +unsigned int issue_flags) +{ +struct io_ring_ctx *ctx =cmd_to_io_kiocb (cmd)->ctx; +struct ublk_device *ub = cmd->file->private_data; +int index = (int )ub_cmd->addr, ret; +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 ; + + data =blk_mq_rq_to_pdu (req); + ret =io_buffer_register_bvec (ctx, req, ublk_io_release, index, + issue_flags); +if (ret) { +ublk_put_req_ref (ubq, req); +return ret; + } + +return 0 ; +} + +static int ublk_unregister_io_buf (struct io_uring_cmd *cmd, +struct ublk_queue *ubq,int tag, +const struct ublksrv_io_cmd *ub_cmd, +unsigned int issue_flags) +{ +struct io_ring_ctx *ctx =cmd_to_io_kiocb (cmd)->ctx; +struct ublk_device *ub = cmd->file->private_data; +int index = (int )ub_cmd->addr; +struct ublk_rq_data *data; +struct request *req; + +if (!ub) +return -EPERM ; + + req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag); +if (!req) +return -EINVAL ; + + data =blk_mq_rq_to_pdu (req); +io_buffer_unregister_bvec (ctx, index, issue_flags); +return 0 ; +} +static int __ublk_ch_uring_cmd (struct io_uring_cmd *cmd,unsigned int issue_flags,const struct ublksrv_io_cmd *ub_cmd)
@@ -1798,6 +1891,11 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd, ret = -EINVAL ;switch (_IOC_NR (cmd_op)) { +case UBLK_IO_REGISTER_IO_BUF : +return ublk_register_io_buf (cmd, ubq, tag, ub_cmd, issue_flags); +case UBLK_IO_UNREGISTER_IO_BUF : +return ublk_unregister_io_buf (cmd, ubq, tag, ub_cmd, + issue_flags);case UBLK_IO_FETCH_REQ :/* UBLK_IO_FETCH_REQ is only allowed before queue is setup */ if (ublk_queue_ready (ubq)) {
@@ -1872,36 +1970,6 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,return -EIOCBQUEUED ; } -static inline struct request *__ublk_check_and_get_req (struct ublk_device *ub, -struct ublk_queue *ubq,int tag,size_t offset) -{ -struct request *req; - -if (!ublk_need_req_ref (ubq)) -return NULL ; - - req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag); -if (!req) -return NULL ; - -if (!ublk_get_req_ref (ubq, req)) -return NULL ; - -if (unlikely (!blk_mq_request_started (req) || req->tag != tag)) - goto fail_put; - -if (!ublk_rq_has_data (req)) - goto fail_put; - -if (offset >blk_rq_bytes (req)) - goto fail_put; - -return req; -fail_put: -ublk_put_req_ref (ubq, req); -return NULL ; -} -static inline int ublk_ch_uring_cmd_local (struct io_uring_cmd *cmd,unsigned int issue_flags) {
@@ -2527,9 +2595,6 @@ 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 ; - ub->dev_info.nr_hw_queues = min_t(unsigned int, ub->dev_info.nr_hw_queues, nr_cpu_ids);ublk_align_max_io_size (ub);
@@ -2860,7 +2925,7 @@ static int ublk_ctrl_get_features(struct io_uring_cmd *cmd) {const struct ublksrv_ctrl_cmd *header =io_uring_sqe_cmd (cmd->sqe);void __user *argp = (void __user *)(unsigned long )header->addr; -u64 features =UBLK_F_ALL & ~UBLK_F_SUPPORT_ZERO_COPY ; +u64 features =UBLK_F_ALL ;if (header->len !=UBLK_FEATURES_LEN || !header->addr)return -EINVAL ;
include/uapi/linux/ublk_cmd.h +4 -0
@@ -94,6 +94,10 @@ _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_REGISTER_IO_BUF \ + _IOWR('u', 0x23, struct ublksrv_io_cmd) +#define UBLK_U_IO_UNREGISTER_IO_BUF \ + _IOWR('u', 0x24, struct ublksrv_io_cmd)/* only ABORT means that no re-fetch */ #define UBLK_IO_RES_OK 0
Signature · 1 line
2.43.5
PATCH v4 4/5 io_uring: add abstraction for buf_table rsrc data
In reply to Keith BuschFrom: Keith Busch <[email protected]>
We'll need to add more fields specific to the registered buffers, so make a layer for it now. No functional change in this patch.
--- include/linux/io_uring_types.h | 6 ++++- io_uring/fdinfo.c | 8 +++--- io_uring/io_uring.c | 2 +- io_uring/register.c | 2 +- io_uring/rsrc.c | 49 +++++++++++++++++----------------- 5 files changed, 35 insertions(+), 32 deletions(-)
include/linux/io_uring_types.h +5 -1
@@ -69,6 +69,10 @@ struct io_file_table {unsigned int alloc_hint; }; +struct io_buf_table { +struct io_rsrc_data data; +}; +struct io_hash_bucket {struct hlist_head list; }____cacheline_aligned_in_smp ;
@@ -293,7 +297,7 @@ struct io_ring_ctx {struct io_wq_work_list iopoll_list;struct io_file_table file_table; -struct io_rsrc_data buf_table; +struct io_buf_table buf_table;struct io_submit_state submit_state;
io_uring/fdinfo.c +4 -4
@@ -217,12 +217,12 @@ __cold void io_uring_show_fdinfo(struct seq_file *m, struct file *file)seq_puts (m,"\n" ); } } -seq_printf (m,"UserBufs:\t%u\n" , ctx->buf_table.nr); -for (i =0 ; has_lock && i < ctx->buf_table.nr; i++) { +seq_printf (m,"UserBufs:\t%u\n" , ctx->buf_table.data.nr); +for (i =0 ; has_lock && i < ctx->buf_table.data.nr; i++) {struct io_mapped_ubuf *buf =NULL ; -if (ctx->buf_table.nodes[i]) - buf = ctx->buf_table.nodes[i]->buf; +if (ctx->buf_table.data.nodes[i]) + buf = ctx->buf_table.data.nodes[i]->buf;if (buf)seq_printf (m,"%5u: 0x%llx/%u\n" , i, buf->ubuf, buf->len);else
io_uring/io_uring.c +1 -1
@@ -1730,7 +1730,7 @@ static bool io_assign_buffer(struct io_kiocb *req, unsigned int issue_flags)return true;io_ring_submit_lock (ctx, issue_flags); - node =io_rsrc_node_lookup (&ctx->buf_table, req->buf_index); + node =io_rsrc_node_lookup (&ctx->buf_table.data, req->buf_index);if (node)io_req_assign_buf_node (req, node);io_ring_submit_unlock (ctx, issue_flags);
io_uring/register.c +1 -1
@@ -926,7 +926,7 @@ SYSCALL_DEFINE4(io_uring_register, unsigned int, fd, unsigned int, opcode, ret =__io_uring_register (ctx, opcode, arg, nr_args);trace_io_uring_register (ctx, opcode, ctx->file_table.data.nr, - ctx->buf_table.nr, ret); + ctx->buf_table.data.nr, ret);mutex_unlock (&ctx->uring_lock);fput (file);
io_uring/rsrc.c +24 -25
148 lines not on this page · raw message
Signature · 1 line
2.43.5
PATCH v4 5/5 io_uring: cache nodes and mapped buffers
In reply to Keith BuschFrom: Keith Busch <[email protected]>
Frequent alloc/free cycles on these is pretty costly. Use an io cache to more efficiently reuse these buffers.
--- include/linux/io_uring_types.h | 18 ++--- io_uring/filetable.c | 2 +- io_uring/rsrc.c | 120 +++++++++++++++++++++++++-------- io_uring/rsrc.h | 2 +- 4 files changed, 104 insertions(+), 38 deletions(-)
include/linux/io_uring_types.h +10 -8
@@ -69,8 +69,18 @@ struct io_file_table {unsigned int alloc_hint; }; +struct io_alloc_cache { +void **entries; +unsigned int nr_cached; +unsigned int max_cached; +size_t elem_size; +unsigned int init_clear; +}; +struct io_buf_table {struct io_rsrc_data data; +struct io_alloc_cache node_cache; +struct io_alloc_cache imu_cache; };struct io_hash_bucket {
@@ -224,14 +234,6 @@ struct io_submit_state {struct blk_plug plug; }; -struct io_alloc_cache { -void **entries; -unsigned int nr_cached; -unsigned int max_cached; -unsigned int elem_size; -unsigned int init_clear; -}; -struct io_ring_ctx {/* const or read-mostly hot data */ struct {
io_uring/filetable.c +1 -1
@@ -68,7 +68,7 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,if (slot_index >= ctx->file_table.data.nr)return -EINVAL ; - node =io_rsrc_node_alloc (IORING_RSRC_FILE ); + node =io_rsrc_node_alloc (ctx,IORING_RSRC_FILE );if (!node)return -ENOMEM ;
io_uring/rsrc.c +92 -28
289 lines not on this page · raw message
io_uring/rsrc.h +1 -1
@@ -49,7 +49,7 @@ struct io_imu_folio_data {unsigned int nr_folios; }; -struct io_rsrc_node *io_rsrc_node_alloc (int type); +struct io_rsrc_node *io_rsrc_node_alloc (struct io_ring_ctx *ctx,int type);void io_free_rsrc_node (struct io_ring_ctx *ctx,struct io_rsrc_node *node);void io_rsrc_data_free (struct io_ring_ctx *ctx,struct io_rsrc_data *data);int io_rsrc_data_alloc (struct io_rsrc_data *data,unsigned nr);
Signature · 1 line
2.43.5
Re: [PATCHv4 1/5] io_uring: move fixed buffer import to issue path
In reply to Keith BuschQuoted 25 lines · Keith Busch
From: Keith Busch <[email protected]>
Similar to the fixed file path, requests may depend on a previous one to set up an index, so we need to allow linking them. The prep callback happens too soon for linked commands, so the lookup needs to be deferred to the issue path. Change the prep callbacks to just set the
buf_indexand let genericio_uringcode handle the fixed buffer node setup, just like it already does for fixed files.Signed-off-by Keith Busch <[email protected]>--- include/linux/io_uring_types.h | 3 +++ io_uring/io_uring.c | 19 ++++++++++++++ io_uring/net.c | 25 ++++++------------- io_uring/nop.c | 22 +++-------------- io_uring/rw.c | 45 ++++++++++++++++++++++++---------- io_uring/uring_cmd.c | 16 ++---------- 6 files changed, 67 insertions(+), 63 deletions(-)include/linux/io_uring_types.h +0
-0
include/linux/io_uring_types.h enum { · line 485
Quoted 4 lines
include/linux/io_uring_types.h +1
-0@@ -482,6 +482,7 @@ enum {REQ_F_DOUBLE_POLL_BIT ,REQ_F_APOLL_MULTISHOT_BIT ,REQ_F_CLEAR_POLLIN_BIT , + REQ_F_FIXED_BUFFER_BIT,
Move this to the end of the REQ_F_*_BIT definitions (before __REQ_F_LAST_BIT)?
Quoted 54 lines
include/linux/io_uring_types.h +2
-0/* keep async read/write and isreg together and in order */ REQ_F_SUPPORT_NOWAIT_BIT , REQ_F_ISREG_BIT,@@ -574,6 +575,8 @@ enum {REQ_F_BUF_NODE =IO_REQ_FLAG (REQ_F_BUF_NODE_BIT ),/* request has read/write metadata assigned */ REQ_F_HAS_METADATA =IO_REQ_FLAG (REQ_F_HAS_METADATA_BIT ), +/* request has a fixed buffer at buf_index */ + REQ_F_FIXED_BUFFER = IO_REQ_FLAG(REQ_F_FIXED_BUFFER_BIT), };typedef void (*io_req_tw_func_t )(struct io_kiocb *req,io_tw_token_t tw);io_uring/io_uring.c +19
-0@@ -1721,6 +1721,23 @@ static bool io_assign_file(struct io_kiocb *req, const struct io_issue_def *def,return !!req->file; } +static bool io_assign_buffer (struct io_kiocb *req,unsigned int issue_flags) +{ +struct io_ring_ctx *ctx = req->ctx; +struct io_rsrc_node *node; + +if (req->buf_node || !(req->flags &REQ_F_FIXED_BUFFER )) +return true; + +io_ring_submit_lock (ctx, issue_flags); + node =io_rsrc_node_lookup (&ctx->buf_table, req->buf_index); +if (node) +io_req_assign_buf_node (req, node); +io_ring_submit_unlock (ctx, issue_flags); + +return !!node; +} +static int io_issue_sqe (struct io_kiocb *req,unsigned int issue_flags) {const struct io_issue_def *def = &io_issue_defs[req->opcode];@@ -1729,6 +1746,8 @@ static int io_issue_sqe(struct io_kiocb *req, unsigned int issue_flags)if (unlikely (!io_assign_file (req, def, issue_flags)))return -EBADF ; +if (unlikely (!io_assign_buffer (req, issue_flags))) +return -EFAULT ;if (unlikely ((req->flags &REQ_F_CREDS ) && req->creds !=current_cred ())) creds =override_creds (req->creds);io_uring/net.c +0
-0
io_uring/net.c int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) · line 1377
Quoted 5 lines
io_uring/net.c +2
-0@@ -1373,6 +1373,10 @@ int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)#endif if (unlikely (!io_msg_alloc_async (req)))return -ENOMEM ; +if (zc->flags &IORING_RECVSEND_FIXED_BUF ) { + req->buf_index = zc->buf_index;
Can the buf_index field of io_sr_msg be removed now that it's only used within io_send_zc_prep()?
Quoted 188 lines
io_uring/net.c +5
-18+ req->flags |=REQ_F_FIXED_BUFFER ; + }if (req->opcode !=IORING_OP_SENDMSG_ZC )return io_send_setup (req, sqe);return io_sendmsg_setup (req, sqe);@@ -1434,25 +1438,10 @@ static int io_send_zc_import(struct io_kiocb *req, unsigned int issue_flags)struct io_async_msghdr *kmsg = req->async_data;int ret; -if (sr->flags &IORING_RECVSEND_FIXED_BUF ) { -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node; - - ret = -EFAULT ; -io_ring_submit_lock (ctx, issue_flags); - node =io_rsrc_node_lookup (&ctx->buf_table, sr->buf_index); -if (node) { -io_req_assign_buf_node (sr->notif, node); - ret =0 ; - } -io_ring_submit_unlock (ctx, issue_flags); - -if (unlikely (ret)) -return ret; - +if (req->flags &REQ_F_FIXED_BUFFER ) { ret =io_import_fixed (ITER_SOURCE , &kmsg->msg.msg_iter, - node->buf, (u64 )(uintptr_t )sr->buf, - sr->len); + req->buf_node->buf, + (u64 )(uintptr_t )sr->buf, sr->len);if (unlikely (ret))return ret; kmsg->msg.sg_from_iter = io_sg_from_iter;io_uring/nop.c +4
-18@@ -16,7 +16,6 @@ struct io_nop {struct file *file;int result;int fd; -int buffer;unsigned int flags; };@@ -39,10 +38,10 @@ int io_nop_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe) nop->fd =READ_ONCE (sqe->fd); else nop->fd = -1; -if (nop->flags &IORING_NOP_FIXED_BUFFER ) - nop->buffer =READ_ONCE (sqe->buf_index); -else - nop->buffer =-1 ; +if (nop->flags &IORING_NOP_FIXED_BUFFER ) { + req->buf_index =READ_ONCE (sqe->buf_index); + req->flags |=REQ_F_FIXED_BUFFER ; + }return 0 ; }@@ -63,19 +62,6 @@ int io_nop(struct io_kiocb *req, unsigned int issue_flags) goto done; } } -if (nop->flags &IORING_NOP_FIXED_BUFFER ) { -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node; - - ret = -EFAULT ; -io_ring_submit_lock (ctx, issue_flags); - node =io_rsrc_node_lookup (&ctx->buf_table, nop->buffer); -if (node) { -io_req_assign_buf_node (req, node); - ret =0 ; - } -io_ring_submit_unlock (ctx, issue_flags); - } done:if (ret <0 )req_set_fail (req);io_uring/rw.c +32
-13@@ -353,25 +353,14 @@ int io_prep_writev(struct io_kiocb *req, const struct io_uring_sqe *sqe)static int io_prep_rw_fixed (struct io_kiocb *req,const struct io_uring_sqe *sqe,int ddir) { - struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node; -struct io_async_rw *io;int ret; ret =io_prep_rw (req, sqe, ddir, false);if (unlikely (ret))return ret; - node =io_rsrc_node_lookup (&ctx->buf_table, req->buf_index); -if (!node) -return -EFAULT ; -io_req_assign_buf_node (req, node); - - io = req->async_data; - ret =io_import_fixed (ddir, &io->iter, node->buf, rw->addr, rw->len); -iov_iter_save_state (&io->iter, &io->iter_state); -return ret; + req->flags |=REQ_F_FIXED_BUFFER ; +return 0 ; }int io_prep_read_fixed (struct io_kiocb *req,const struct io_uring_sqe *sqe)@@ -954,10 +943,36 @@ static int __io_read(struct io_kiocb *req, unsigned int issue_flags)return ret; } +static int io_import_fixed_buffer (struct io_kiocb *req,int ddir) +{ + struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); +struct io_async_rw *io; +int ret; + +if (!(req->flags &REQ_F_FIXED_BUFFER )) +return 0 ; + + io = req->async_data; +if (io->bytes_done) +return 0 ; + + ret =io_import_fixed (ddir, &io->iter, req->buf_node->buf, rw->addr, + rw->len); +if (ret) +return ret; + +iov_iter_save_state (&io->iter, &io->iter_state); +return 0 ; +} +int io_read (struct io_kiocb *req,unsigned int issue_flags) {int ret; + ret =io_import_fixed_buffer (req,READ ); +if (unlikely (ret)) +return ret; + ret =__io_read (req, issue_flags);if (ret >=0 )return kiocb_done (req, ret, issue_flags);@@ -1062,6 +1077,10 @@ int io_write(struct io_kiocb *req, unsigned int issue_flags)ssize_t ret, ret2;loff_t *ppos; + ret =io_import_fixed_buffer (req,WRITE ); +if (unlikely (ret)) +return ret; + ret =io_rw_init_file (req,FMODE_WRITE ,WRITE );if (unlikely (ret))return ret;io_uring/uring_cmd.c +2
-13@@ -200,19 +200,8 @@ int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)return -EINVAL ;if (ioucmd->flags &IORING_URING_CMD_FIXED ) { -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node; -u16 index =READ_ONCE (sqe->buf_index); - - node =io_rsrc_node_lookup (&ctx->buf_table, index); -if (unlikely (!node)) -return -EFAULT ; -/* -* Pi node upfront, prior to io_uring_cmd_import_fixed() -* being called. This prevents destruction of the mapped buffer -* we'll need at actual import time. -*/ -io_req_assign_buf_node (req, node); + req->buf_index =READ_ONCE (sqe->buf_index); + req->flags |=REQ_F_FIXED_BUFFER ; } ioucmd->cmd_op =READ_ONCE (sqe->cmd_op);
io_uring/uring_cmd.c int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, · line 255
Quoted 6 lines
io_uring/uring_cmd.c +0
-1@@ -262,7 +251,6 @@ int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw,struct io_kiocb *req =cmd_to_io_kiocb (ioucmd);struct io_rsrc_node *node = req->buf_node; -/* Must have had rsrc_node assigned at prep time */ if (node)return io_import_fixed (rw, iter, node->buf, ubuf, len);
Is it possible for node to be NULL? If the buffer lookup failed, io_issue_sqe() would have returned early and not called ->issue(), so this function wouldn't have been called.
Best,
Caleb
Quoted 4 lines
io_uring/uring_cmd.c +0
-1--2.43.5
Re: [PATCHv4 2/5] io_uring: add support for kernel registered bvecs
In reply to Keith BuschQuoted 23 lines · Keith Busch
From: Keith Busch <[email protected]>
Provide an interface for the kernel to leverage the existing pre-registered buffers that
io_uringprovides. User space can reference these later to achieve zero-copy IO.User space must register an empty fixed buffer table with
io_uringin order for the kernel to make use of it.Signed-off-by Keith Busch <[email protected]>--- include/linux/io_uring.h | 1 + include/linux/io_uring_types.h | 6 ++ io_uring/rsrc.c | 115 ++++++++++++++++++++++++++++++--- io_uring/rsrc.h | 4 ++ 4 files changed, 118 insertions(+), 8 deletions(-)include/linux/io_uring.h +0
-0
include/linux/io_uring.h line 8
Quoted 4 lines
include/linux/io_uring.h +1
-0@@ -5,6 +5,7 @@#include <linux/sched.h> #include <linux/xarray.h> #include <uapi/linux/io_uring.h> +#include <linux/blk-mq.h>
Should this be added to the header file that declares io_buffer_register_bvec() instead?
Quoted 8 lines
include/linux/io_uring.h +0
-0#if defined(CONFIG_IO_URING )void __io_uring_cancel (bool cancel_all);include/linux/io_uring_types.h +0
-0
include/linux/io_uring_types.h static inline bool io_ctx_cqe32(struct io_ring_ctx *ctx) · line 716
Quoted 8 lines
include/linux/io_uring_types.h +5
-0@@ -709,4 +709,10 @@ static inline bool io_ctx_cqe32(struct io_ring_ctx *ctx)return ctx->flags &IORING_SETUP_CQE32 ; } +int io_buffer_register_bvec (struct io_ring_ctx *ctx,struct request *rq, +void (*release)(void *),unsigned int index, +unsigned int issue_flags); +void io_buffer_unregister_bvec (struct io_ring_ctx *ctx,unsigned int index, +unsigned int issue_flags);
Hmm, io_uring_types.h seems like a strange place for these. The rest of this file consists of types and inline functions. Maybe io_uring/rsrc.h would make more sense?
Quoted 84 lines
include/linux/io_uring_types.h +1
-0+#endif io_uring/rsrc.c +47
-6@@ -103,19 +103,23 @@ int io_buffer_validate(struct iovec *iov)static void io_buffer_unmap (struct io_ring_ctx *ctx,struct io_rsrc_node *node) { -unsigned int i; +struct io_mapped_ubuf *imu = node->buf; -if (node->buf) { -struct io_mapped_ubuf *imu = node->buf; +if (!refcount_dec_and_test (&imu->refs)) +return ; + +if (imu->release) { + imu->release (imu->priv); + }else { +unsigned int i; -if (!refcount_dec_and_test (&imu->refs)) -return ;for (i =0 ; i < imu->nr_bvecs; i++)unpin_user_page (imu->bvec[i].bv_page);if (imu->acct_pages)io_unaccount_mem (ctx, imu->acct_pages); -kvfree (imu); } + +kvfree (imu); }struct io_rsrc_node *io_rsrc_node_alloc (int type)@@ -764,6 +768,10 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, imu->len = iov->iov_len; imu->nr_bvecs = nr_pages; imu->folio_shift =PAGE_SHIFT ; + imu->release =NULL ; + imu->priv =NULL ; + imu->readable = true; + imu->writeable = true;if (coalesced) imu->folio_shift = data.folio_shift;refcount_set (&imu->refs,1 );@@ -860,6 +868,87 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,return ret; } +int io_buffer_register_bvec (struct io_ring_ctx *ctx,struct request *rq, +void (*release)(void *),unsigned int index, +unsigned int issue_flags) +{ +struct io_rsrc_data *data = &ctx->buf_table; +struct req_iterator rq_iter; +struct io_mapped_ubuf *imu; +struct io_rsrc_node *node; +struct bio_vec bv, *bvec; +int ret =0 ; +u16 nr_bvecs; + +io_ring_submit_lock (ctx, issue_flags); + +if (io_rsrc_node_lookup (data, index)) { + ret = -EBUSY ; + goto unlock; + } + + node =io_rsrc_node_alloc (IORING_RSRC_BUFFER ); +if (!node) { + ret = -ENOMEM ; + goto unlock; + } + + nr_bvecs =blk_rq_nr_phys_segments (rq); + imu =kvmalloc (struct_size (imu, bvec, nr_bvecs),GFP_KERNEL ); +if (!imu) { +kfree (node); + ret = -ENOMEM ; + goto unlock; + } +
io_uring/rsrc.c int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, · line 913
Quoted 10 lines
io_uring/rsrc.c +10
-0+ imu->ubuf =0 ; + imu->len =blk_rq_bytes (rq); + imu->acct_pages =0 ; + imu->folio_shift =PAGE_SHIFT ; + imu->nr_bvecs = nr_bvecs; +refcount_set (&imu->refs,1 ); + imu->release = release; + imu->priv = rq; + +if (rq_data_dir (rq))
Check rq_data_dir(rq) == WRITE instead to avoid depending on the value of READ/WRITE?
Quoted 20 lines
io_uring/rsrc.c +20
-0+ imu->writeable = true; + else + imu->readable = true; + + bvec = imu->bvec; +rq_for_each_bvec (bv, rq, rq_iter) + *bvec++ = bv; + + node->buf = imu; + data->nodes[index] = node; +unlock: +io_ring_submit_unlock (ctx, issue_flags); +return ret; +} +EXPORT_SYMBOL_GPL (io_buffer_register_bvec); + +void io_buffer_unregister_bvec (struct io_ring_ctx *ctx,unsigned int index, +unsigned int issue_flags) +{ +struct io_rsrc_data *data = &ctx->buf_table;
io_uring/rsrc.c int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, · line 943
Quoted 10 lines
io_uring/rsrc.c +10
-0+struct io_rsrc_node *node; + +io_ring_submit_lock (ctx, issue_flags); + +if (!data->nr) + goto unlock; + + node =io_rsrc_node_lookup (data, index); +if (!node || !node->buf->release) + goto unlock;
Still think the data->nr check is unnecessary, since io_rsrc_node_lookup() would return NULL in that case.
Quoted 12 lines
io_uring/rsrc.c +8
-0+ +io_put_rsrc_node (ctx, node); + data->nodes[index] =NULL ; +unlock: +io_ring_submit_unlock (ctx, issue_flags); +} +EXPORT_SYMBOL_GPL (io_buffer_unregister_bvec); +int io_import_fixed (int ddir,struct iov_iter *iter,struct io_mapped_ubuf *imu,u64 buf_addr,size_t len)
io_uring/rsrc.c int io_import_fixed(int ddir, struct iov_iter *iter, · line 968
Quoted 6 lines
io_uring/rsrc.c +3
-0@@ -874,6 +963,9 @@ int io_import_fixed(int ddir, struct iov_iter *iter,/* not inside the mapped region */ if (unlikely (buf_addr < imu->ubuf || buf_end > (imu->ubuf + imu->len)))return -EFAULT ; +if ((ddir ==READ && !imu->readable) || + (ddir ==WRITE && !imu->writeable)) +return -EFAULT ;
This could be made less branchy by storing a bitmask of allowed data transfer directions instead of 2 bool fields. Then this could just be:
if (!(imu->ddirs >> ddir & 1)
return -EFAULT;
Best,
Caleb
Quoted 48 lines
io_uring/rsrc.c +9
-2/* * Might not be a start of buffer, set size appropriately @@ -886,8 +978,8 @@ int io_import_fixed(int ddir, struct iov_iter *iter,/* * Don't use iov_iter_advance() here, as it's really slow for * using the latter parts of a big fixed buffer - it iterates -* over each segment manually. We can cheat a bit here, because -* we know that: +* over each segment manually. We can cheat a bit here for user +* registered nodes, because we know that: * * 1) it's a BVEC iter, we set it up * 2) all bvecs are the same in size, except potentially the @@ -901,8 +993,15 @@ int io_import_fixed(int ddir, struct iov_iter *iter,*/ const struct bio_vec *bvec = imu->bvec; +/* +* Kernel buffer bvecs, on the other hand, don't necessarily +* have the size property of user registered ones, so we have +* to use the slow iter advance. +*/ if (offset < bvec->bv_len) { iter->iov_offset = offset; + }else if (imu->release) { +iov_iter_advance (iter, offset); }else {unsigned long seg_skip;io_uring/rsrc.h +4
-1@@ -33,6 +33,10 @@ struct io_mapped_ubuf {unsigned int folio_shift;refcount_t refs;unsigned long acct_pages; +void (*release)(void *); +void *priv; +bool readable; +bool writeable; struct bio_vec bvec[] __counted_by(nr_bvecs); }; --2.43.5
Re: [PATCHv4 3/5] ublk: zc register/unregister bvec
In reply to Keith BuschSorry, I sent these comments on v3 to you directly and forgot to CC the list. Copying them here.
Quoted 88 lines · Keith Busch
From: Keith Busch <[email protected]>
Provide new operations for the user to request mapping an active request to an io uring instance's
buf_table. The user has to provide the index it wants to install the buffer.A reference count is taken on the request to ensure it can't be completed while it is active in a ring's
buf_table.Signed-off-by Keith Busch <[email protected]>--- drivers/block/ublk_drv.c | 137 +++++++++++++++++++++++++--------- include/uapi/linux/ublk_cmd.h | 4 + 2 files changed, 105 insertions(+), 36 deletions(-)drivers/block/ublk_drv.c +41
-2@@ -51,6 +51,9 @@/* private ioctl command mirror */ #define UBLK_CMD_DEL_DEV_ASYNC _IOC_NR(UBLK_U_CMD_DEL_DEV_ASYNC) +#define UBLK_IO_REGISTER_IO_BUF _IOC_NR(UBLK_U_IO_REGISTER_IO_BUF) +#define UBLK_IO_UNREGISTER_IO_BUF _IOC_NR(UBLK_U_IO_UNREGISTER_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 \@@ -201,7 +204,7 @@ static inline struct ublksrv_io_desc *ublk_get_iod(struct ublk_queue *ubq, int tag);static inline bool ublk_dev_is_user_copy (const struct ublk_device *ub) { -return ub->dev_info.flags &UBLK_F_USER_COPY ; +return ub->dev_info.flags & (UBLK_F_USER_COPY |UBLK_F_SUPPORT_ZERO_COPY ); }static inline bool ublk_dev_is_zoned (const struct ublk_device *ub)@@ -581,7 +584,7 @@ static void ublk_apply_params(struct ublk_device *ub)static inline bool ublk_support_user_copy (const struct ublk_queue *ubq) { -return ubq->flags &UBLK_F_USER_COPY ; +return ubq->flags & (UBLK_F_USER_COPY |UBLK_F_SUPPORT_ZERO_COPY ); }static inline bool ublk_need_req_ref (const struct ublk_queue *ubq)@@ -1747,6 +1750,96 @@ static inline void ublk_prep_cancel(struct io_uring_cmd *cmd,io_uring_cmd_mark_cancelable (cmd, issue_flags); } +static inline struct request *__ublk_check_and_get_req (struct ublk_device *ub, +struct ublk_queue *ubq,int tag,size_t offset) +{ +struct request *req; + +if (!ublk_need_req_ref (ubq)) +return NULL ; + + req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag); +if (!req) +return NULL ; + +if (!ublk_get_req_ref (ubq, req)) +return NULL ; + +if (unlikely (!blk_mq_request_started (req) || req->tag != tag)) + goto fail_put; + +if (!ublk_rq_has_data (req)) + goto fail_put; + +if (offset >blk_rq_bytes (req)) + goto fail_put; + +return req; +fail_put: +ublk_put_req_ref (ubq, req); +return NULL ; +} + +static void ublk_io_release (void *priv) +{ +struct request *rq = priv; +struct ublk_queue *ubq = rq->mq_hctx->driver_data; + +ublk_put_req_ref (ubq, rq);
drivers/block/ublk_drv.c static inline void ublk_prep_cancel(struct io_uring_cmd *cmd, · line 1798
Quoted 10 lines
drivers/block/ublk_drv.c +10
-0+} + +static int ublk_register_io_buf (struct io_uring_cmd *cmd, +struct ublk_queue *ubq,int tag, +const struct ublksrv_io_cmd *ub_cmd, +unsigned int issue_flags) +{ +struct io_ring_ctx *ctx =cmd_to_io_kiocb (cmd)->ctx; +struct ublk_device *ub = cmd->file->private_data; +int index = (int )ub_cmd->addr, ret;
Make index an unsigned to match io_buffer_register_bvec()? Same comment for ublk_unregister_io_buf().
drivers/block/ublk_drv.c static inline void ublk_prep_cancel(struct io_uring_cmd *cmd, · line 1803
Quoted 5 lines
drivers/block/ublk_drv.c +5
-0+struct ublk_rq_data *data; +struct request *req; + +if (!ub) +return -EPERM ;
__ublk_ch_uring_cmd() has already dereferenced ub = cmd->file->private_data, how is it possible to hit this? Same comment for ublk_unregister_io_buf()
drivers/block/ublk_drv.c static inline void ublk_prep_cancel(struct io_uring_cmd *cmd, · line 1805
Quoted 2 lines
drivers/block/ublk_drv.c +2
-0+ + req =__ublk_check_and_get_req (ub, ubq, tag,0 );
Consider moving the offset > blk_rq_bytes(req) check from __ublk_check_and_get_req() to ublk_check_and_get_req() so we don't need to pass an unused offset here.
drivers/block/ublk_drv.c static inline void ublk_prep_cancel(struct io_uring_cmd *cmd, · line 1809
Quoted 4 lines
drivers/block/ublk_drv.c +4
-0+if (!req) +return -EINVAL ; + + data =blk_mq_rq_to_pdu (req);
data appears unused in this function. Same comment for ublk_unregister_io_buf().
Quoted 4 lines
drivers/block/ublk_drv.c +4
-0+ ret =io_buffer_register_bvec (ctx, req, ublk_io_release, index, + issue_flags); +if (ret) { +ublk_put_req_ref (ubq, req);
drivers/block/ublk_drv.c static inline void ublk_prep_cancel(struct io_uring_cmd *cmd, · line 1823
Quoted 10 lines
drivers/block/ublk_drv.c +10
-0+return ret; + } + +return 0 ; +} + +static int ublk_unregister_io_buf (struct io_uring_cmd *cmd, +struct ublk_queue *ubq,int tag, +const struct ublksrv_io_cmd *ub_cmd, +unsigned int issue_flags)
Make tag an unsigned to match __ublk_ch_uring_cmd() and blk_mq_tag_to_rq()? Same comment for ublk_register_io_buf().
Quoted 6 lines
drivers/block/ublk_drv.c +6
-0+{ +struct io_ring_ctx *ctx =cmd_to_io_kiocb (cmd)->ctx; +struct ublk_device *ub = cmd->file->private_data; +int index = (int )ub_cmd->addr; +struct ublk_rq_data *data; +struct request *req;
drivers/block/ublk_drv.c static inline void ublk_prep_cancel(struct io_uring_cmd *cmd, · line 1839
Quoted 10 lines
drivers/block/ublk_drv.c +10
-0+ +if (!ub) +return -EPERM ; + + req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag); +if (!req) +return -EINVAL ; + + data =blk_mq_rq_to_pdu (req); +io_buffer_unregister_bvec (ctx, index, issue_flags);
Should we check that the registered bvec actually corresponds to this ublk request? Otherwise, I don't see a reason for the unregister command to involve the ublk request at all. Perhaps a generic io_uring "unregister buffer index" operation similar to IORING_OP_FILES_UPDATE would make more sense?
Best,
Caleb
Quoted 92 lines
drivers/block/ublk_drv.c +9
-34+return 0 ; +} +static int __ublk_ch_uring_cmd (struct io_uring_cmd *cmd,unsigned int issue_flags,const struct ublksrv_io_cmd *ub_cmd)@@ -1798,6 +1891,11 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd, ret = -EINVAL ;switch (_IOC_NR (cmd_op)) { +case UBLK_IO_REGISTER_IO_BUF : +return ublk_register_io_buf (cmd, ubq, tag, ub_cmd, issue_flags); +case UBLK_IO_UNREGISTER_IO_BUF : +return ublk_unregister_io_buf (cmd, ubq, tag, ub_cmd, + issue_flags);case UBLK_IO_FETCH_REQ :/* UBLK_IO_FETCH_REQ is only allowed before queue is setup */ if (ublk_queue_ready (ubq)) {@@ -1872,36 +1970,6 @@ static int __ublk_ch_uring_cmd(struct io_uring_cmd *cmd,return -EIOCBQUEUED ; } -static inline struct request *__ublk_check_and_get_req (struct ublk_device *ub, -struct ublk_queue *ubq,int tag,size_t offset) -{ -struct request *req; - -if (!ublk_need_req_ref (ubq)) -return NULL ; - - req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag); -if (!req) -return NULL ; - -if (!ublk_get_req_ref (ubq, req)) -return NULL ; - -if (unlikely (!blk_mq_request_started (req) || req->tag != tag)) - goto fail_put; - -if (!ublk_rq_has_data (req)) - goto fail_put; - -if (offset >blk_rq_bytes (req)) - goto fail_put; - -return req; -fail_put: -ublk_put_req_ref (ubq, req); -return NULL ; -} -static inline int ublk_ch_uring_cmd_local (struct io_uring_cmd *cmd,unsigned int issue_flags) {@@ -2527,9 +2595,6 @@ 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 ; - ub->dev_info.nr_hw_queues = min_t(unsigned int, ub->dev_info.nr_hw_queues, nr_cpu_ids);ublk_align_max_io_size (ub);@@ -2860,7 +2925,7 @@ static int ublk_ctrl_get_features(struct io_uring_cmd *cmd) {const struct ublksrv_ctrl_cmd *header =io_uring_sqe_cmd (cmd->sqe);void __user *argp = (void __user *)(unsigned long )header->addr; -u64 features =UBLK_F_ALL & ~UBLK_F_SUPPORT_ZERO_COPY ; +u64 features =UBLK_F_ALL ;if (header->len !=UBLK_FEATURES_LEN || !header->addr)return -EINVAL ;include/uapi/linux/ublk_cmd.h +4
-1@@ -94,6 +94,10 @@ _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_REGISTER_IO_BUF \ + _IOWR('u', 0x23, struct ublksrv_io_cmd) +#define UBLK_U_IO_UNREGISTER_IO_BUF \ + _IOWR('u', 0x24, struct ublksrv_io_cmd)/* only ABORT means that no re-fetch */ #define UBLK_IO_RES_OK 0 --2.43.5
Re: [PATCHv4 4/5] io_uring: add abstraction for buf_table rsrc data
In reply to Keith BuschQuoted 7 lines · Keith Busch
From: Keith Busch <[email protected]>
We'll need to add more fields specific to the registered buffers, so make a layer for it now. No functional change in this patch.
Signed-off-by Keith Busch <[email protected]>
Re: [PATCHv4 5/5] io_uring: cache nodes and mapped buffers
In reply to Keith BuschQuoted 19 lines · Keith Busch
From: Keith Busch <[email protected]>
Frequent alloc/free cycles on these is pretty costly. Use an io cache to more efficiently reuse these buffers.
Signed-off-by Keith Busch <[email protected]>--- include/linux/io_uring_types.h | 18 ++--- io_uring/filetable.c | 2 +- io_uring/rsrc.c | 120 +++++++++++++++++++++++++-------- io_uring/rsrc.h | 2 +- 4 files changed, 104 insertions(+), 38 deletions(-)include/linux/io_uring_types.h +0
-0
include/linux/io_uring_types.h struct io_file_table { · line 76
Quoted 8 lines
include/linux/io_uring_types.h +5
-0@@ -69,8 +69,18 @@ struct io_file_table {unsigned int alloc_hint; }; +struct io_alloc_cache { +void **entries; +unsigned int nr_cached; +unsigned int max_cached; +size_t elem_size;
Is growing this field from unsigned to size_t really necessary? It probably doesn't make sense to be caching allocations > 4 GB.
Quoted 52 lines
include/linux/io_uring_types.h +5
-8+unsigned int init_clear; +}; +struct io_buf_table {struct io_rsrc_data data; +struct io_alloc_cache node_cache; +struct io_alloc_cache imu_cache; };struct io_hash_bucket {@@ -224,14 +234,6 @@ struct io_submit_state {struct blk_plug plug; }; -struct io_alloc_cache { -void **entries; -unsigned int nr_cached; -unsigned int max_cached; -unsigned int elem_size; -unsigned int init_clear; -}; -struct io_ring_ctx {/* const or read-mostly hot data */ struct {io_uring/filetable.c +1
-1@@ -68,7 +68,7 @@ static int io_install_fixed_file(struct io_ring_ctx *ctx, struct file *file,if (slot_index >= ctx->file_table.data.nr)return -EINVAL ; - node =io_rsrc_node_alloc (IORING_RSRC_FILE ); + node =io_rsrc_node_alloc (ctx,IORING_RSRC_FILE );if (!node)return -ENOMEM ;io_uring/rsrc.c +2
-0@@ -32,6 +32,8 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,#define IORING_MAX_FIXED_FILES (1U << 20)#define IORING_MAX_REG_BUFFERS (1U << 14) +#define IO_CACHED_BVECS_SEGS 32 +int __io_account_mem (struct user_struct *user,unsigned long nr_pages) {unsigned long page_limit, cur_pages, new_pages;
io_uring/rsrc.c int io_buffer_validate(struct iovec *iov) · line 107
Quoted 5 lines
io_uring/rsrc.c +2
-0@@ -101,6 +103,22 @@ int io_buffer_validate(struct iovec *iov)return 0 ; } +static struct io_mapped_ubuf *io_alloc_imu (struct io_ring_ctx *ctx, +int nr_bvecs)
Pass nr_bvecs as unsigned to avoid sign-extension in struct_size_t()?
Quoted 203 lines
io_uring/rsrc.c +76
-15+{ +if (nr_bvecs <=IO_CACHED_BVECS_SEGS ) +return io_cache_alloc (&ctx->buf_table.imu_cache,GFP_KERNEL ); + return kvmalloc(struct_size_t(struct io_mapped_ubuf, bvec, nr_bvecs), +GFP_KERNEL ); +} + +static void io_free_imu (struct io_ring_ctx *ctx,struct io_mapped_ubuf *imu) +{ +if (imu->nr_bvecs >IO_CACHED_BVECS_SEGS || + !io_alloc_cache_put (&ctx->buf_table.imu_cache, imu)) +kvfree (imu); +} +static void io_buffer_unmap (struct io_ring_ctx *ctx,struct io_rsrc_node *node) {struct io_mapped_ubuf *imu = node->buf;@@ -119,22 +137,35 @@ static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_rsrc_node *node)io_unaccount_mem (ctx, imu->acct_pages); } -kvfree (imu); +io_free_imu (ctx, imu); } -struct io_rsrc_node *io_rsrc_node_alloc (int type) +struct io_rsrc_node *io_rsrc_node_alloc (struct io_ring_ctx *ctx,int type) {struct io_rsrc_node *node; - node =kzalloc (sizeof (*node),GFP_KERNEL ); +if (type ==IORING_RSRC_FILE ) + node =kmalloc (sizeof (*node),GFP_KERNEL ); +else + node =io_cache_alloc (&ctx->buf_table.node_cache,GFP_KERNEL );if (node) { node->type = type; node->refs =1 ; + node->tag =0 ; + node->file_ptr =0 ; }return node; } -__cold void io_rsrc_data_free (struct io_ring_ctx *ctx,struct io_rsrc_data *data) +static __cold void __io_rsrc_data_free (struct io_rsrc_data *data) +{ +kvfree (data->nodes); + data->nodes =NULL ; + data->nr =0 ; +} + +__cold void io_rsrc_data_free (struct io_ring_ctx *ctx, +struct io_rsrc_data *data) {if (!data->nr)return ;@@ -142,9 +173,7 @@ __cold void io_rsrc_data_free(struct io_ring_ctx *ctx, struct io_rsrc_data *dataif (data->nodes[data->nr])io_put_rsrc_node (ctx, data->nodes[data->nr]); } -kvfree (data->nodes); - data->nodes =NULL ; - data->nr =0 ; +__io_rsrc_data_free (data); }__cold int io_rsrc_data_alloc (struct io_rsrc_data *data,unsigned nr)@@ -158,6 +187,31 @@ __cold int io_rsrc_data_alloc(struct io_rsrc_data *data, unsigned nr)return -ENOMEM ; } +static __cold int io_rsrc_buffer_alloc (struct io_buf_table *table,unsigned nr) +{ + const int imu_cache_size = struct_size_t(struct io_mapped_ubuf, bvec, +IO_CACHED_BVECS_SEGS ); +const int node_size =sizeof (struct io_rsrc_node ); +int ret; + + ret =io_rsrc_data_alloc (&table->data, nr); +if (ret) +return ret; + +if (io_alloc_cache_init (&table->node_cache, nr, node_size,0 )) + goto free_data; + +if (io_alloc_cache_init (&table->imu_cache, nr, imu_cache_size,0 )) + goto free_cache; + +return 0 ; +free_cache: +io_alloc_cache_free (&table->node_cache, kfree); +free_data: +__io_rsrc_data_free (&table->data); +return -ENOMEM ; +} +static int __io_sqe_files_update (struct io_ring_ctx *ctx,struct io_uring_rsrc_update2 *up,unsigned nr_args)@@ -207,7 +261,7 @@ static int __io_sqe_files_update(struct io_ring_ctx *ctx, err = -EBADF ;break ; } - node =io_rsrc_node_alloc (IORING_RSRC_FILE ); + node =io_rsrc_node_alloc (ctx,IORING_RSRC_FILE );if (!node) { err = -ENOMEM ;fput (file);@@ -459,6 +513,8 @@ void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node)case IORING_RSRC_BUFFER :if (node->buf)io_buffer_unmap (ctx, node); +if (io_alloc_cache_put (&ctx->buf_table.node_cache, node)) +return ;break ;default :WARN_ON_ONCE (1 );@@ -527,7 +583,7 @@ int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg, goto fail; } ret = -ENOMEM ; - node =io_rsrc_node_alloc (IORING_RSRC_FILE ); + node =io_rsrc_node_alloc (ctx,IORING_RSRC_FILE );if (!node) {fput (file); goto fail;@@ -547,11 +603,19 @@ int io_sqe_files_register(struct io_ring_ctx *ctx, void __user *arg,return ret; } +static void io_rsrc_buffer_free (struct io_ring_ctx *ctx, +struct io_buf_table *table) +{ +io_rsrc_data_free (ctx, &table->data); +io_alloc_cache_free (&table->node_cache, kfree); +io_alloc_cache_free (&table->imu_cache, kfree); +} +int io_sqe_buffers_unregister (struct io_ring_ctx *ctx) {if (!ctx->buf_table.data.nr)return -ENXIO ; -io_rsrc_data_free (ctx, &ctx->buf_table.data); +io_rsrc_buffer_free (ctx, &ctx->buf_table);return 0 ; }@@ -732,7 +796,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx,if (!iov->iov_base)return NULL ; - node =io_rsrc_node_alloc (IORING_RSRC_BUFFER ); + node =io_rsrc_node_alloc (ctx,IORING_RSRC_BUFFER );if (!node)return ERR_PTR (-ENOMEM ); node->buf =NULL ;@@ -752,7 +816,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, coalesced =io_coalesce_buffer (&pages, &nr_pages, &data); } - imu =kvmalloc (struct_size (imu, bvec, nr_pages),GFP_KERNEL ); + imu =io_alloc_imu (ctx, nr_pages);if (!imu) goto done;@@ -789,7 +853,7 @@ static struct io_rsrc_node *io_sqe_buffer_register(struct io_ring_ctx *ctx, } done:if (ret) { -kvfree (imu); +io_free_imu (ctx, imu);if (node)io_put_rsrc_node (ctx, node); node =ERR_PTR (ret);@@ -802,9 +866,9 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, unsigned int nr_args, u64 __user *tags) { struct page *last_hpage = NULL; -struct io_rsrc_data data;struct iovec fast_iov, *iov = &fast_iov;const struct iovec __user *uvec; +struct io_buf_table table;int i, ret;BUILD_BUG_ON (IORING_MAX_REG_BUFFERS >= (1u <<16 ));@@ -813,13 +877,14 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,return -EBUSY ;if (!nr_args || nr_args >IORING_MAX_REG_BUFFERS )return -EINVAL ; - ret =io_rsrc_data_alloc (&data, nr_args); + ret =io_rsrc_buffer_alloc (&table, nr_args);if (ret)return ret;if (!arg)memset (iov,0 ,sizeof (*iov)); + ctx->buf_table = table;for (i =0 ; i < nr_args; i++) {struct io_rsrc_node *node;u64 tag =0 ;
io_uring/rsrc.c int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, · line 865
Quoted 8 lines
io_uring/rsrc.c +1
-3@@ -859,10 +924,8 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, } node->tag = tag; } - data.nodes[i] = node; + table.data.nodes[i] = node; } - - ctx->buf_table.data = data;
Still don't see the need to move this assignment. Is there a reason you prefer setting ctx->buf_table before initializing its nodes? I find the existing code easier to follow, where the table is moved to ctx->buf_table after filling it in. It's also consistent with io_clone_buffers().
Quoted 8 lines
io_uring/rsrc.c +0
-1if (ret)io_sqe_buffers_unregister (ctx);return ret;@@ -887,14 +950,15 @@ int io_buffer_register_bvec(struct io_ring_ctx *ctx, struct request *rq, goto unlock; } - node =io_rsrc_node_alloc (IORING_RSRC_BUFFER );
io_uring/rsrc.c int io_buffer_register_bvec(struct io_ring_ctx *ctx, struct request *rq, · line 961
Quoted 10 lines
io_uring/rsrc.c +3
-1+ node =io_rsrc_node_alloc (ctx,IORING_RSRC_BUFFER );if (!node) { ret = -ENOMEM ; goto unlock; } nr_bvecs =blk_rq_nr_phys_segments (rq); - imu =kvmalloc (struct_size (imu, bvec, nr_bvecs),GFP_KERNEL ); + + imu =io_alloc_imu (ctx, nr_bvecs);
nit: probably don't need to add a blank line here
Best,
Caleb
Quoted 83 lines
io_uring/rsrc.c +8
-8if (!imu) {kfree (node); ret = -ENOMEM ;@@ -1031,7 +1095,7 @@ static void lock_two_rings(struct io_ring_ctx *ctx1, struct io_ring_ctx *ctx2)static int io_clone_buffers (struct io_ring_ctx *ctx,struct io_ring_ctx *src_ctx,struct io_uring_clone_buffers *arg) { -struct io_rsrc_data data; +struct io_buf_table table;int i, ret, off, nr;unsigned int nbufs;@@ -1062,7 +1126,7 @@ static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctxif (check_add_overflow (arg->nr, arg->dst_off, &nbufs))return -EOVERFLOW ; - ret =io_rsrc_data_alloc (&data,max (nbufs, ctx->buf_table.data.nr)); + ret =io_rsrc_buffer_alloc (&table,max (nbufs, ctx->buf_table.data.nr));if (ret)return ret;@@ -1071,7 +1135,7 @@ static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctxstruct io_rsrc_node *src_node = ctx->buf_table.data.nodes[i];if (src_node) { - data.nodes[i] = src_node; + table.data.nodes[i] = src_node; src_node->refs++; } }@@ -1101,7 +1165,7 @@ static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctxif (!src_node) { dst_node =NULL ; }else { - dst_node =io_rsrc_node_alloc (IORING_RSRC_BUFFER ); + dst_node =io_rsrc_node_alloc (ctx,IORING_RSRC_BUFFER );if (!dst_node) { ret = -ENOMEM ; goto out_free;@@ -1110,12 +1174,12 @@ static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctxrefcount_inc (&src_node->buf->refs); dst_node->buf = src_node->buf; } - data.nodes[off++] = dst_node; + table.data.nodes[off++] = dst_node; i++; }/* -* If asked for replace, put the old table. data->nodes[] holds both +* If asked for replace, put the old table. table.data->nodes[] holds both * old and new nodes at this point. */ if (arg->flags &IORING_REGISTER_DST_REPLACE )@@ -1128,10 +1192,10 @@ static int io_clone_buffers(struct io_ring_ctx *ctx, struct io_ring_ctx *src_ctx* entry). */ WARN_ON_ONCE (ctx->buf_table.data.nr); - ctx->buf_table.data = data; + ctx->buf_table = table;return 0 ; out_free: -io_rsrc_data_free (ctx, &data); +io_rsrc_buffer_free (ctx, &table);return ret; }io_uring/rsrc.h +1
-2@@ -49,7 +49,7 @@ struct io_imu_folio_data {unsigned int nr_folios; }; -struct io_rsrc_node *io_rsrc_node_alloc (int type); +struct io_rsrc_node *io_rsrc_node_alloc (struct io_ring_ctx *ctx,int type);void io_free_rsrc_node (struct io_ring_ctx *ctx,struct io_rsrc_node *node);void io_rsrc_data_free (struct io_ring_ctx *ctx,struct io_rsrc_data *data);int io_rsrc_data_alloc (struct io_rsrc_data *data,unsigned nr); --2.43.5
Re: [PATCHv4 1/5] io_uring: move fixed buffer import to issue path
In reply to Keith BuschQuoted 11 lines · Keith Busch
From: Keith Busch <[email protected]>
Similar to the fixed file path, requests may depend on a previous one to set up an index, so we need to allow linking them. The prep callback happens too soon for linked commands, so the lookup needs to be deferred to the issue path. Change the prep callbacks to just set the
buf_indexand let genericio_uringcode handle the fixed buffer node setup, just like it already does for fixed files.Signed-off-by Keith Busch <[email protected]>---
...
Quoted 18 lines
io_uring/net.c +4
-0@@ -1373,6 +1373,10 @@ int io_send_zc_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)#endif if (unlikely (!io_msg_alloc_async (req)))return -ENOMEM ; +if (zc->flags &IORING_RECVSEND_FIXED_BUF ) { + req->buf_index = zc->buf_index; + req->flags |=REQ_F_FIXED_BUFFER ; + }if (req->opcode !=IORING_OP_SENDMSG_ZC )return io_send_setup (req, sqe);return io_sendmsg_setup (req, sqe);@@ -1434,25 +1438,10 @@ static int io_send_zc_import(struct io_kiocb *req, unsigned int issue_flags)struct io_async_msghdr *kmsg = req->async_data;int ret;
io_uring/net.c static int io_send_zc_import(struct io_kiocb *req, unsigned int issue_flags) · line 1445
Quoted 10 lines
io_uring/net.c +0
-9-if (sr->flags &IORING_RECVSEND_FIXED_BUF ) { -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node; - - ret = -EFAULT ; -io_ring_submit_lock (ctx, issue_flags); - node =io_rsrc_node_lookup (&ctx->buf_table, sr->buf_index); -if (node) { -io_req_assign_buf_node (sr->notif, node);
Here the node buffer is assigned to ->notif req, instead of the current request, and you may have to deal with this case here.
Otherwise, this patch looks fine:
Thanks,
Ming
Re: [PATCHv4 1/5] io_uring: move fixed buffer import to issue path
In reply to Keith BuschQuoted 10 lines · Keith Busch
From: Keith Busch <[email protected]>
Similar to the fixed file path, requests may depend on a previous one to set up an index, so we need to allow linking them. The prep callback happens too soon for linked commands, so the lookup needs to be deferred to the issue path. Change the prep callbacks to just set the
buf_indexand let genericio_uringcode handle the fixed buffer node setup, just like it already does for fixed files.Signed-off-by Keith Busch <[email protected]>
It wasn't great before, and it'd be harder to follow if we shove it into the issue path like that. Add additional overhead in the common path and that it's not super flexible, like the notification problem and what we need out of it for other features.
We're better to remove the lookup vs import split like below. Here is a branch, let's do it on top.
https://github.com/isilence/linux.git regbuf-import
io_uring/net.c +4 -18
@@ -1360,24 +1360,10 @@ static int io_send_zc_import(struct io_kiocb *req, unsigned int issue_flags)int ret;if (sr->flags &IORING_RECVSEND_FIXED_BUF ) { -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node; - - ret = -EFAULT ; -io_ring_submit_lock (ctx, issue_flags); - node =io_rsrc_node_lookup (&ctx->buf_table, req->buf_index); -if (node) { -io_req_assign_buf_node (sr->notif, node); - ret =0 ; - } -io_ring_submit_unlock (ctx, issue_flags); - -if (unlikely (ret)) -return ret; - - ret =io_import_fixed (ITER_SOURCE , &kmsg->msg.msg_iter, - node->buf, (u64 )(uintptr_t )sr->buf, - sr->len); + sr->notif->buf_index = req->buf_index; + ret =io_import_reg_buf (sr->notif, &kmsg->msg.msg_iter, + (u64 )(uintptr_t )sr->buf, sr->len, +ITER_SOURCE , issue_flags);if (unlikely (ret))return ret; kmsg->msg.sg_from_iter = io_sg_from_iter;
io_uring/rsrc.c +32 -3
@@ -860,9 +860,9 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg,return ret; } -int io_import_fixed (int ddir,struct iov_iter *iter, -struct io_mapped_ubuf *imu, -u64 buf_addr,size_t len) +static int io_import_fixed_imu (int ddir,struct iov_iter *iter, +struct io_mapped_ubuf *imu, +u64 buf_addr,size_t len) {u64 buf_end;size_t offset;
@@ -919,6 +919,35 @@ int io_import_fixed(int ddir, struct iov_iter *iter,return 0 ; } +static inline struct io_rsrc_node *io_find_buf_node (struct io_kiocb *req, +unsigned issue_flags) +{ +struct io_ring_ctx *ctx = req->ctx; +struct io_rsrc_node *node; + +if (req->buf_node) +return req->buf_node; + +io_ring_submit_lock (ctx, issue_flags); + node =io_rsrc_node_lookup (&ctx->buf_table, req->buf_index); +if (node) +io_req_assign_buf_node (req, node); +io_ring_submit_unlock (ctx, issue_flags); +return node; +} + +int io_import_reg_buf (struct io_kiocb *req,struct iov_iter *iter, +u64 buf_addr,size_t len,int ddir, +unsigned issue_flags) +{ +struct io_rsrc_node *node; + + node =io_find_buf_node (req, issue_flags); +if (!node) +return -EFAULT ; +return io_import_fixed_imu (ddir, iter, node->buf, buf_addr, len); +} +/* Lock two rings at once. The rings must be different! */ static void lock_two_rings (struct io_ring_ctx *ctx1,struct io_ring_ctx *ctx2) {
io_uring/rsrc.h +3 -3
@@ -50,9 +50,9 @@ void io_free_rsrc_node(struct io_ring_ctx *ctx, struct io_rsrc_node *node);void io_rsrc_data_free (struct io_ring_ctx *ctx,struct io_rsrc_data *data);int io_rsrc_data_alloc (struct io_rsrc_data *data,unsigned nr); -int io_import_fixed (int ddir,struct iov_iter *iter, -struct io_mapped_ubuf *imu, -u64 buf_addr,size_t len); +int io_import_reg_buf (struct io_kiocb *req,struct iov_iter *iter, +u64 buf_addr,size_t len,int ddir, +unsigned issue_flags);int io_register_clone_buffers (struct io_ring_ctx *ctx,void __user *arg);int io_sqe_buffers_unregister (struct io_ring_ctx *ctx);
io_uring/rw.c +1 -8
@@ -354,8 +354,6 @@ static int io_prep_rw_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqe int ddir) { struct io_rw *rw = io_kiocb_to_cmd(req, struct io_rw); -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node;struct io_async_rw *io;int ret;
@@ -363,13 +361,8 @@ static int io_prep_rw_fixed(struct io_kiocb *req, const struct io_uring_sqe *sqeif (unlikely (ret))return ret; - node =io_rsrc_node_lookup (&ctx->buf_table, req->buf_index); -if (!node) -return -EFAULT ; -io_req_assign_buf_node (req, node); - io = req->async_data; - ret =io_import_fixed (ddir, &io->iter, node->buf, rw->addr, rw->len); + ret =io_import_reg_buf (req, &io->iter, rw->addr, rw->len, ddir,0 );iov_iter_save_state (&io->iter, &io->iter_state);return ret; }
io_uring/uring_cmd.c +4 -21
@@ -199,21 +199,9 @@ int io_uring_cmd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)if (ioucmd->flags & ~IORING_URING_CMD_MASK )return -EINVAL ; -if (ioucmd->flags &IORING_URING_CMD_FIXED ) { -struct io_ring_ctx *ctx = req->ctx; -struct io_rsrc_node *node; -u16 index =READ_ONCE (sqe->buf_index); - - node =io_rsrc_node_lookup (&ctx->buf_table, index); -if (unlikely (!node)) -return -EFAULT ; -/* -* Pi node upfront, prior to io_uring_cmd_import_fixed() -* being called. This prevents destruction of the mapped buffer -* we'll need at actual import time. -*/ -io_req_assign_buf_node (req, node); - } +if (ioucmd->flags &IORING_URING_CMD_FIXED ) + req->buf_index =READ_ONCE (sqe->buf_index); + ioucmd->cmd_op =READ_ONCE (sqe->cmd_op);return io_uring_cmd_prep_setup (req, sqe);
@@ -261,13 +249,8 @@ int io_uring_cmd_import_fixed(u64 ubuf, unsigned long len, int rw, unsigned int issue_flags) {struct io_kiocb *req =cmd_to_io_kiocb (ioucmd); -struct io_rsrc_node *node = req->buf_node; - -/* Must have had rsrc_node assigned at prep time */ -if (node) -return io_import_fixed (rw, iter, node->buf, ubuf, len); -return -EFAULT ; +return io_import_reg_buf (req, iter, ubuf, len, rw, issue_flags); }EXPORT_SYMBOL_GPL (io_uring_cmd_import_fixed);
Signature · 1 line
Pavel Begunkov
Re: [PATCHv4 1/5] io_uring: move fixed buffer import to issue path
Quoted 28 lines · Pavel Begunkov
Quoted 10 lines · Keith Busch
From: Keith Busch <[email protected]>
Similar to the fixed file path, requests may depend on a previous one to set up an index, so we need to allow linking them. The prep callback happens too soon for linked commands, so the lookup needs to be deferred to the issue path. Change the prep callbacks to just set the
buf_indexand let genericio_uringcode handle the fixed buffer node setup, just like it already does for fixed files.Signed-off-by Keith Busch <[email protected]>It wasn't great before, and it'd be harder to follow if we shove it into the issue path like that. Add additional overhead in the common path and that it's not super flexible, like the notification problem and what we need out of it for other features.
We're better to remove the lookup vs import split like below. Here is a branch, let's do it on top.
https://github.com/isilence/linux.git regbuf-import
io_uring/net.c +0
-0@@ -1360,24 +1360,10 @@ static int io_send_zc_import(struct io_kiocb *req, unsigned int issue_flags)
...
Quoted 10 lines
io_uring/net.c +3
-3-int io_import_fixed (int ddir,struct iov_iter *iter, - struct io_mapped_ubuf *imu, - u64 buf_addr, size_t len) +static int io_import_fixed_imu (int ddir,struct iov_iter *iter, + struct io_mapped_ubuf *imu, + u64 buf_addr, size_t len) { u64 buf_end; size_t offset;
io_uring/net.c int io_import_fixed(int ddir, struct iov_iter *iter, · line 928
Quoted 10 lines
io_uring/net.c +7
-0@@ -919,6 +919,35 @@ int io_import_fixed(int ddir, struct iov_iter *iter, return 0; } +static inline struct io_rsrc_node *io_find_buf_node (struct io_kiocb *req, + unsigned issue_flags) +{ + struct io_ring_ctx *ctx = req->ctx; + struct io_rsrc_node *node; + + if (req->buf_node)
Seems it should be checking for REQ_F_BUF_NODE instead
Quoted 9 lines
io_uring/net.c +9
-0+ return req->buf_node; + + io_ring_submit_lock(ctx, issue_flags); + node = io_rsrc_node_lookup(&ctx->buf_table, req->buf_index); + if (node) + io_req_assign_buf_node(req, node); + io_ring_submit_unlock(ctx, issue_flags); + return node; +}
Signature · 1 line
Pavel Begunkov
Re: [PATCHv4 2/5] io_uring: add support for kernel registered bvecs
In reply to Caleb Sander MateosQuoted 1 line · Caleb Sander Mateos
On Tue, Feb 18, 2025 at 2:42 PM Keith Busch <[email protected]> wrote:
...
Quoted 8 lines
Quoted 8 lines
include/linux/io_uring_types.h +0
-0@@ -709,4 +709,10 @@ static inline bool io_ctx_cqe32(struct io_ring_ctx *ctx)return ctx->flags &IORING_SETUP_CQE32 ; }
Quoted 9 lines
include/linux/io_uring_types.h static inline bool io_ctx_cqe32(struct io_ring_ctx *ctx) · line 716
Quoted 5 lines
include/linux/io_uring_types.h +5
-0+int io_buffer_register_bvec (struct io_ring_ctx *ctx,struct request *rq, +void (*release)(void *),unsigned int index, +unsigned int issue_flags); +void io_buffer_unregister_bvec (struct io_ring_ctx *ctx,unsigned int index, +unsigned int issue_flags);Hmm,
io_uring_types.hseems like a strange place for these. The rest of this file consists of types and inline functions. Maybeio_uring/rsrc.hwould make more sense?
There is linux/io_uring/cmd.h for that
And it should be taking struct io_uring_cmd. ublk shouldn't know anything about ctx, and this line from the following patch in not allowed.
struct io_ring_ctx *ctx = cmd_to_io_kiocb(cmd)->ctx;
Signature · 1 line
Pavel Begunkov
Re: [PATCHv4 1/5] io_uring: move fixed buffer import to issue path
In reply to Pavel BegunkovQuoted 4 lines · Pavel Begunkov
We're better to remove the lookup vs import split like below. Here is a branch, let's do it on top.
https://github.com/isilence/linux.git regbuf-import
Your first patch adds a 10th parameter to an nvme function, most of which are unused in half the branches. I think we've done something wrong here, so I want to take a shot at cleaning that up. Otherwise I think what you're proposing is an improvement.
Re: [PATCHv4 1/5] io_uring: move fixed buffer import to issue path
Quoted 9 lines · Keith Busch
Quoted 4 lines · Pavel Begunkov
We're better to remove the lookup vs import split like below. Here is a branch, let's do it on top.
https://github.com/isilence/linux.git regbuf-import
Your first patch adds a 10th parameter to an nvme function, most of which are unused in half the branches. I think we've done something wrong here, so I want to take a shot at cleaning that up. Otherwise I
That would be great, I didn't feel great about it either, even the existing ioucmd arg doesn't seem like a perfect fit.
Quoted 1 line
think what you're proposing is an improvement.
Signature · 1 line
Pavel Begunkov
Re: [PATCHv4 2/5] io_uring: add support for kernel registered bvecs
In reply to Caleb Sander MateosQuoted 1 line · Caleb Sander Mateos
On Tue, Feb 18, 2025 at 2:42 PM Keith Busch <[email protected]> wrote:
...
Quoted 14 lines
io_uring/rsrc.c int io_import_fixed(int ddir, struct iov_iter *iter, · line 968
Quoted 10 lines
int io_import_fixed(int ddir, struct
iov_iter*iter,
structio_mapped_ubuf*imu, u64buf_addr,size_tlen)io_uring/rsrc.c +3
-0@@ -874,6 +963,9 @@ int io_import_fixed(int ddir, struct iov_iter *iter,/* not inside the mapped region */ if (unlikely (buf_addr < imu->ubuf || buf_end > (imu->ubuf + imu->len)))return -EFAULT ; +if ((ddir ==READ && !imu->readable) || + (ddir ==WRITE && !imu->writeable)) +return -EFAULT ;This could be made less branchy by storing a bitmask of allowed data transfer directions instead of 2 bool fields. Then this could just be:
if (!(imu->ddirs >> ddir & 1)
Quoted 1 line
return -EFAULT;
I'd prefer a direction mask indeed.
Signature · 1 line
Pavel Begunkov
Re: [PATCHv4 2/5] io_uring: add support for kernel registered bvecs
In reply to Keith BuschQuoted 1 line · Keith Busch
From: Keith Busch <[email protected]>
...
Quoted 5 lines
io_uring/rsrc.c +5
-0+int io_buffer_register_bvec (struct io_ring_ctx *ctx,struct request *rq, +void (*release)(void *),unsigned int index, +unsigned int issue_flags) +{ +struct io_rsrc_data *data = &ctx->buf_table;
io_uring/rsrc.c int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, · line 885
Quoted 10 lines
io_uring/rsrc.c +10
-0+struct req_iterator rq_iter; +struct io_mapped_ubuf *imu; +struct io_rsrc_node *node; +struct bio_vec bv, *bvec; +int ret =0 ; +u16 nr_bvecs; + +io_ring_submit_lock (ctx, issue_flags); + +if (io_rsrc_node_lookup (data, index)) {
I don't think array nospec inside is enough as you use the old unsanitised index below to assign the node, and it seems like it can speculate there.
Same in io_buffer_unregister_bvec().
...
Quoted 29 lines
io_uring/rsrc.c +29
-0+ node->buf = imu; + data->nodes[index] = node; +unlock: +io_ring_submit_unlock (ctx, issue_flags); +return ret; +} +EXPORT_SYMBOL_GPL (io_buffer_register_bvec); + +void io_buffer_unregister_bvec (struct io_ring_ctx *ctx,unsigned int index, +unsigned int issue_flags) +{ +struct io_rsrc_data *data = &ctx->buf_table; +struct io_rsrc_node *node; + +io_ring_submit_lock (ctx, issue_flags); + +if (!data->nr) + goto unlock; + + node =io_rsrc_node_lookup (data, index); +if (!node || !node->buf->release) + goto unlock; + +io_put_rsrc_node (ctx, node); + data->nodes[index] =NULL ; +unlock: +io_ring_submit_unlock (ctx, issue_flags); +} +EXPORT_SYMBOL_GPL (io_buffer_unregister_bvec);
Signature · 1 line
Pavel Begunkov
Re: [PATCHv4 5/5] io_uring: cache nodes and mapped buffers
In reply to Keith BuschQuoted 19 lines · Keith Busch
From: Keith Busch <[email protected]>
Frequent alloc/free cycles on these is pretty costly. Use an io cache to more efficiently reuse these buffers.
Signed-off-by Keith Busch <[email protected]>--- include/linux/io_uring_types.h | 18 ++--- io_uring/filetable.c | 2 +- io_uring/rsrc.c | 120 +++++++++++++++++++++++++-------- io_uring/rsrc.h | 2 +- 4 files changed, 104 insertions(+), 38 deletions(-)include/linux/io_uring_types.h +0
-0@@ -69,8 +69,18 @@ struct io_file_table {unsigned int alloc_hint;
...
include/linux/io_uring_types.h
Quoted 10 lines
include/linux/io_uring_types.h +5
-2-struct io_rsrc_node *io_rsrc_node_alloc (int type) +struct io_rsrc_node *io_rsrc_node_alloc (struct io_ring_ctx *ctx,int type) {struct io_rsrc_node *node; - node =kzalloc (sizeof (*node),GFP_KERNEL ); +if (type ==IORING_RSRC_FILE ) + node =kmalloc (sizeof (*node),GFP_KERNEL ); +else + node =io_cache_alloc (&ctx->buf_table.node_cache,GFP_KERNEL );
That's why node allocators shouldn't be a part of the buffer table.
Signature · 1 line
Pavel Begunkov
Re: [PATCHv4 3/5] ublk: zc register/unregister bvec
In reply to Keith BuschQuoted 20 lines · Keith Busch
From: Keith Busch <[email protected]>
Provide new operations for the user to request mapping an active request to an io uring instance's
buf_table. The user has to provide the index it wants to install the buffer.A reference count is taken on the request to ensure it can't be completed while it is active in a ring's
buf_table.Signed-off-by Keith Busch <[email protected]>--- drivers/block/ublk_drv.c | 137 +++++++++++++++++++++++++--------- include/uapi/linux/ublk_cmd.h | 4 + 2 files changed, 105 insertions(+), 36 deletions(-)drivers/block/ublk_drv.c +0
-0@@ -51,6 +51,9 @@
...
Quoted 5 lines
drivers/block/ublk_drv.c +5
-0+static int ublk_unregister_io_buf (struct io_uring_cmd *cmd, +struct ublk_queue *ubq,int tag, +const struct ublksrv_io_cmd *ub_cmd, +unsigned int issue_flags) +{
drivers/block/ublk_drv.c static inline void ublk_prep_cancel(struct io_uring_cmd *cmd, · line 1834
Quoted 10 lines
drivers/block/ublk_drv.c +10
-0+struct io_ring_ctx *ctx =cmd_to_io_kiocb (cmd)->ctx; +struct ublk_device *ub = cmd->file->private_data; +int index = (int )ub_cmd->addr; +struct ublk_rq_data *data; +struct request *req; + +if (!ub) +return -EPERM ; + + req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag);
Shouldn't there some speculation sanitisation for the tag as well? Looks like a user passed value directly indexing an array.
drivers/block/ublk_drv.c static inline void ublk_prep_cancel(struct io_uring_cmd *cmd, · line 1842
Quoted 8 lines
drivers/block/ublk_drv.c +8
-0+if (!req) +return -EINVAL ; + + data =blk_mq_rq_to_pdu (req); +io_buffer_unregister_bvec (ctx, index, issue_flags); +return 0 ; +} +
Pavel Begunkov
Re: [PATCHv4 5/5] io_uring: cache nodes and mapped buffers
In reply to Pavel BegunkovQuoted 21 lines · Pavel Begunkov
Quoted 19 lines · Keith Busch
From: Keith Busch <[email protected]>
Frequent alloc/free cycles on these is pretty costly. Use an io cache to more efficiently reuse these buffers.
Signed-off-by Keith Busch <[email protected]>--- include/linux/io_uring_types.h | 18 ++--- io_uring/filetable.c | 2 +- io_uring/rsrc.c | 120 +++++++++++++++++++++++++-------- io_uring/rsrc.h | 2 +- 4 files changed, 104 insertions(+), 38 deletions(-)include/linux/io_uring_types.h +0
-0@@ -69,8 +69,18 @@ struct io_file_table {unsigned int alloc_hint;...
Quoted 11 lines
include/linux/io_uring_types.h
Quoted 9 lines
include/linux/io_uring_types.h +5
-2-struct io_rsrc_node *io_rsrc_node_alloc (int type) +struct io_rsrc_node *io_rsrc_node_alloc (struct io_ring_ctx *ctx,int type) {struct io_rsrc_node *node; - node =kzalloc (sizeof (*node),GFP_KERNEL ); +if (type ==IORING_RSRC_FILE ) + node =kmalloc (sizeof (*node),GFP_KERNEL ); +else + node =io_cache_alloc (&ctx->buf_table.node_cache,GFP_KERNEL );That's why node allocators shouldn't be a part of the buffer table.
Are you saying you want file nodes to also subscribe to the cache? The two tables can be resized independently of each other, we don't know how many elements the cache needs to hold.
Re: [PATCHv4 5/5] io_uring: cache nodes and mapped buffers
Quoted 34 lines · Keith Busch
Quoted 21 lines · Pavel Begunkov
Quoted 19 lines · Keith Busch
From: Keith Busch <[email protected]>
Frequent alloc/free cycles on these is pretty costly. Use an io cache to more efficiently reuse these buffers.
Signed-off-by Keith Busch <[email protected]>--- include/linux/io_uring_types.h | 18 ++--- io_uring/filetable.c | 2 +- io_uring/rsrc.c | 120 +++++++++++++++++++++++++-------- io_uring/rsrc.h | 2 +- 4 files changed, 104 insertions(+), 38 deletions(-)include/linux/io_uring_types.h +0
-0@@ -69,8 +69,18 @@ struct io_file_table {unsigned int alloc_hint;...
Quoted 11 lines
include/linux/io_uring_types.h
Quoted 9 lines
include/linux/io_uring_types.h +5
-2-struct io_rsrc_node *io_rsrc_node_alloc (int type) +struct io_rsrc_node *io_rsrc_node_alloc (struct io_ring_ctx *ctx,int type) {struct io_rsrc_node *node; - node =kzalloc (sizeof (*node),GFP_KERNEL ); +if (type ==IORING_RSRC_FILE ) + node =kmalloc (sizeof (*node),GFP_KERNEL ); +else + node =io_cache_alloc (&ctx->buf_table.node_cache,GFP_KERNEL );That's why node allocators shouldn't be a part of the buffer table.
Quoted 1 line
Are you saying you want file nodes to also subscribe to the cache? The
Yes, but it might be easier for you to focus on finalising the essential parts, and then we can improve later.
Quoted 2 lines
two tables can be resized independently of each other, we don't know how many elements the cache needs to hold.
I wouldn't try to correlate table sizes with desired cache sizes, users can have quite different patterns like allocating a barely used huge table. And you care about the speed of node change, which at extremes is rather limited by CPU and performance and not spatiality of the table. And you can also reallocate it as well.
Signature · 1 line
Pavel Begunkov
Re: [PATCHv4 5/5] io_uring: cache nodes and mapped buffers
In reply to Caleb Sander MateosQuoted 8 lines · Caleb Sander Mateos
include/linux/io_uring_types.h struct io_file_table { · line 76
Quoted 5 lines
include/linux/io_uring_types.h +5
-0+struct io_alloc_cache { +void **entries; +unsigned int nr_cached; +unsigned int max_cached; +size_t elem_size;Is growing this field from unsigned to
size_treally necessary? It probably doesn't make sense to be caching allocations > 4 GB.
It used to be a size_t when I initially moved the struct to here, but it's not anymore, so I'm out of sync. I'll fix it up.
Quoted 7 lines
Quoted 7 lines
io_uring/rsrc.c +1
-1@@ -859,10 +924,8 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, } node->tag = tag; } - data.nodes[i] = node; + table.data.nodes[i] = node; }
Quoted 8 lines
io_uring/rsrc.c int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, · line 865
Quoted 2 lines
io_uring/rsrc.c +0
-2- - ctx->buf_table.data = data;Still don't see the need to move this assignment. Is there a reason you prefer setting
ctx->buf_tablebefore initializing its nodes? I find the existing code easier to follow, where the table is moved toctx->buf_tableafter filling it in. It's also consistent withio_clone_buffers().
Yes, it needs to move to earlier. The ctx buf_table needs to be set before any allocations from io_rsrc_node_alloc() can happen.
Re: [PATCHv4 3/5] ublk: zc register/unregister bvec
In reply to Pavel BegunkovQuoted 11 lines · Pavel Begunkov
Quoted 10 lines · Keith Busch
drivers/block/ublk_drv.c +10
-0+static int ublk_unregister_io_buf (struct io_uring_cmd *cmd, +struct ublk_queue *ubq,int tag, +const struct ublksrv_io_cmd *ub_cmd, +unsigned int issue_flags) +{ +struct io_ring_ctx *ctx =cmd_to_io_kiocb (cmd)->ctx; +struct ublk_device *ub = cmd->file->private_data; +int index = (int )ub_cmd->addr; +struct ublk_rq_data *data; +struct request *req;
Quoted 8 lines
drivers/block/ublk_drv.c static inline void ublk_prep_cancel(struct io_uring_cmd *cmd, · line 1834
Quoted 5 lines
drivers/block/ublk_drv.c +5
-0+ +if (!ub) +return -EPERM ; + + req =blk_mq_tag_to_rq (ub->tag_set.tags[ubq->q_id], tag);Shouldn't there some speculation sanitisation for the tag as well? Looks like a user passed value directly indexing an array.
There are no other array speculation defenses here, so looks like a pre-existing issue. I'll send something to address that separate from this series.
Re: [PATCHv4 5/5] io_uring: cache nodes and mapped buffers
In reply to Pavel BegunkovQuoted 10 lines · Pavel Begunkov
Quoted 5 lines · Keith Busch
Quoted 3 lines
io_uring/rsrc.c static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_rsrc_node *node) · line 150
Quoted 1 line
io_uring/rsrc.c +1
-0+ node =io_cache_alloc (&ctx->buf_table.node_cache,GFP_KERNEL );That's why node allocators shouldn't be a part of the buffer table.
Are you saying you want file nodes to also subscribe to the cache? The
Yes, but it might be easier for you to focus on finalising the essential parts, and then we can improve later.
Quoted 8 lines
Quoted 2 lines
two tables can be resized independently of each other, we don't know how many elements the cache needs to hold.
I wouldn't try to correlate table sizes with desired cache sizes, users can have quite different patterns like allocating a barely used huge table. And you care about the speed of node change, which at extremes is rather limited by CPU and performance and not spatiality of the table. And you can also reallocate it as well.
Having the cache size and lifetime match a table that it's providing seems as simple as I can make this. This is still an optimization at the end of the day, so it's not strictly necessary to take the last two patches from this series to make zero copy work if you don't want to include it from the beginning.
Re: [PATCHv4 5/5] io_uring: cache nodes and mapped buffers
In reply to Keith BuschQuoted 30 lines · Keith Busch
Quoted 8 lines · Caleb Sander Mateos
include/linux/io_uring_types.h struct io_file_table { · line 76
Quoted 5 lines
include/linux/io_uring_types.h +5
-0+struct io_alloc_cache { +void **entries; +unsigned int nr_cached; +unsigned int max_cached; +size_t elem_size;Is growing this field from unsigned to
size_treally necessary? It probably doesn't make sense to be caching allocations > 4 GB.It used to be a
size_twhen I initially moved the struct to here, but it's not anymore, so I'm out of sync. I'll fix it up.Quoted 7 lines
Quoted 7 lines
io_uring/rsrc.c +1
-1@@ -859,10 +924,8 @@ int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, } node->tag = tag; } - data.nodes[i] = node; + table.data.nodes[i] = node; }Quoted 8 lines
io_uring/rsrc.c int io_sqe_buffers_register(struct io_ring_ctx *ctx, void __user *arg, · line 865
Quoted 2 lines
io_uring/rsrc.c +0
-2- - ctx->buf_table.data = data;Still don't see the need to move this assignment. Is there a reason you prefer setting
ctx->buf_tablebefore initializing its nodes? I find the existing code easier to follow, where the table is moved toctx->buf_tableafter filling it in. It's also consistent withio_clone_buffers().
Quoted 2 lines
Yes, it needs to move to earlier. The ctx
buf_tableneeds to be set before any allocations fromio_rsrc_node_alloc()can happen.
Got it, thank you for explaining.
Best,
Caleb
Re: [PATCHv4 5/5] io_uring: cache nodes and mapped buffers
In reply to Keith BuschQuoted 14 lines · Keith Busch
Quoted 13 lines · Pavel Begunkov
Quoted 5 lines · Keith Busch
Quoted 3 lines
io_uring/rsrc.c static void io_buffer_unmap(struct io_ring_ctx *ctx, struct io_rsrc_node *node) · line 150
Quoted 1 line
io_uring/rsrc.c +1
-0+ node =io_cache_alloc (&ctx->buf_table.node_cache,GFP_KERNEL );That's why node allocators shouldn't be a part of the buffer table.
Are you saying you want file nodes to also subscribe to the cache? The
Yes, but it might be easier for you to focus on finalising the essential parts, and then we can improve later.
Quoted 2 lines
two tables can be resized independently of each other, we don't know how many elements the cache needs to hold.
Quoted 7 lines
Quoted 5 lines
I wouldn't try to correlate table sizes with desired cache sizes, users can have quite different patterns like allocating a barely used huge table. And you care about the speed of node change, which at extremes is rather limited by CPU and performance and not spatiality of the table. And you can also reallocate it as well.
Having the cache size and lifetime match a table that it's providing
You don't need to align it with the table size, I actually overlooked it, will comment on v5.
Quoted 4 lines
seems as simple as I can make this. This is still an optimization at the end of the day, so it's not strictly necessary to take the last two patches from this series to make zero copy work if you don't want to include it from the beginning.
Ok. As I mentioned above, I do think it's wrong for nodes, but we can merge the patch and rework on top.
Signature · 1 line
Pavel Begunkov