io_uring/register: add IORING_REGISTER_CLONE_FILES opcode
PATCH v7 1/1 io_uring/register: add IORING_REGISTER_CLONE_FILES opcode
Currently, if an application wants to duplicate registered file descriptors from one io_uring instance to another, it must manually unregister and re-register them, incurring unnecessary overhead.
Add IORING_REGISTER_CLONE_FILES to allow direct cloning of the file table from a source ring to a destination ring. This implementation strictly mirrors the io_clone_buffers UAPI, supporting partial offsets and the IORING_REGISTER_DST_REPLACE flag.
To ensure lock synchronization safety, destination nodes are strictly allocated as new, private io_rsrc_nodes rather than sharing references across rings.
---
v7:
- Copy src_node->tag to dst_node in io_clone_file_node (Gabriel).
v6:
- Fixed trailing whitespace checkpatch error.
v5:
- Added missing spacing in comment (Gabriel).
- Removed ctx->user and mm_account checks (Gabriel).
- Used !! for boolean conversion (Gabriel).
- Moved mutex_unlock unconditionally above the out label (Gabriel).
- liburing implementation and tests: https://github.com/axboe/liburing/pull/1606
v4:
- Updated Signed-off-by to use real name and moved above the scissors line (Greg KH).
v3:
- Rewrote the cloning loop to allocate private destination nodes via io_rsrc_node_alloc to fix non-atomic ref lock synchronization (Jens).
- Maintained partial offset/copy support to mirror io_clone_buffers UAPI (Jens).
- Gated the replacement free check on ctx->file_table.data.nr (Gabriel).
- Prevented self-cloning by checking ctx == src_ctx (Gabriel).
- Removed submitter_task check to allow cross-thread pooling setups (Gabriel).
v2:
- Dropped unrelated whitespace formatting changes from v1.
--- include/uapi/linux/io_uring.h | 12 +++ io_uring/register.c | 6 ++ io_uring/rsrc.c | 140 ++++++++++++++++++++++++++++++++++ io_uring/rsrc.h | 1 + 4 files changed, 159 insertions(+)
include/uapi/linux/io_uring.h +12 -0
@@ -723,6 +723,9 @@ enum io_uring_register_op {/* register bpf filtering programs */ IORING_REGISTER_BPF_FILTER =37 , +/* clone registered files from source ring to current ring */ +IORING_REGISTER_CLONE_FILES =38 , +/* this goes last */ IORING_REGISTER_LAST,
@@ -845,6 +848,15 @@ enum { IORING_REGISTER_DST_REPLACE = (1U << 1), }; +struct io_uring_clone_files { +__u32 src_fd; +__u32 flags; +__u32 src_off; +__u32 dst_off; +__u32 nr; +__u32 pad[3 ]; +}; +struct io_uring_clone_buffers {__u32 src_fd;__u32 flags;
io_uring/register.c +6 -0
@@ -978,6 +978,12 @@ static int __io_uring_register(struct io_ring_ctx *ctx, unsigned opcode,WRITE_ONCE (ctx->bpf_filters, ctx->restrictions.bpf_filters->filters);break ; +case IORING_REGISTER_CLONE_FILES : + ret = -EINVAL ; +if (!arg || nr_args !=1 ) +break ; + ret =io_register_clone_files (ctx, arg); +break ;default : ret = -EINVAL ;break ;
io_uring/rsrc.c +140 -0
146 lines not on this page · raw message
io_uring/rsrc.h +1 -0
@@ -71,6 +71,7 @@ int io_prep_reg_iovec(struct io_kiocb *req, struct iou_vec *iv, const struct iovec __user *uvec, size_t uvec_segs);int io_register_clone_buffers (struct io_ring_ctx *ctx,void __user *arg); +int io_register_clone_files (struct io_ring_ctx *ctx,void __user *arg);int io_sqe_buffers_unregister (struct io_ring_ctx *ctx);int io_sqe_buffers_register (struct io_ring_ctx *ctx,void __user *arg,unsigned int nr_args,u64 __user *tags);
Signature · 1 line
2.55.0