Skip to content

Commit

Permalink
io_uring/splice: open code 2nd direct file assignment
Browse files Browse the repository at this point in the history
In preparation for not pinning the whole registered file table, open
code the second potential direct file assignment. This will be handled
by appropriate helpers in the future, for now just do it manually.

Signed-off-by: Jens Axboe <axboe@kernel.dk>
  • Loading branch information
Jens Axboe committed Oct 29, 2024
1 parent aaa736b commit 743fb58
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
2 changes: 2 additions & 0 deletions io_uring/opdef.c
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ const struct io_cold_def io_cold_defs[] = {
},
[IORING_OP_SPLICE] = {
.name = "SPLICE",
.cleanup = io_splice_cleanup,
},
[IORING_OP_PROVIDE_BUFFERS] = {
.name = "PROVIDE_BUFFERS",
Expand All @@ -650,6 +651,7 @@ const struct io_cold_def io_cold_defs[] = {
},
[IORING_OP_TEE] = {
.name = "TEE",
.cleanup = io_splice_cleanup,
},
[IORING_OP_SHUTDOWN] = {
.name = "SHUTDOWN",
Expand Down
44 changes: 36 additions & 8 deletions io_uring/splice.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct io_splice {
u64 len;
int splice_fd_in;
unsigned int flags;
struct io_rsrc_node *rsrc_node;
};

static int __io_splice_prep(struct io_kiocb *req,
Expand All @@ -34,6 +35,7 @@ static int __io_splice_prep(struct io_kiocb *req,
if (unlikely(sp->flags & ~valid_flags))
return -EINVAL;
sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in);
sp->rsrc_node = NULL;
req->flags |= REQ_F_FORCE_ASYNC;
return 0;
}
Expand All @@ -45,6 +47,38 @@ int io_tee_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
return __io_splice_prep(req, sqe);
}

void io_splice_cleanup(struct io_kiocb *req)
{
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);

io_put_rsrc_node(req->ctx, sp->rsrc_node);
}

static struct file *io_splice_get_file(struct io_kiocb *req,
unsigned int issue_flags)
{
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
struct io_ring_ctx *ctx = req->ctx;
struct io_fixed_file *slot;
struct file *file = NULL;

if (!(sp->flags & SPLICE_F_FD_IN_FIXED))
return io_file_get_normal(req, sp->splice_fd_in);

io_ring_submit_lock(ctx, issue_flags);
if (unlikely(sp->splice_fd_in >= ctx->nr_user_files))
goto out;
sp->splice_fd_in = array_index_nospec(sp->splice_fd_in, ctx->nr_user_files);
slot = &ctx->file_table.files[sp->splice_fd_in];
if (!req->rsrc_node)
__io_req_set_rsrc_node(req, ctx);
file = io_slot_file(slot);
req->flags |= REQ_F_NEED_CLEANUP;
out:
io_ring_submit_unlock(ctx, issue_flags);
return file;
}

int io_tee(struct io_kiocb *req, unsigned int issue_flags)
{
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
Expand All @@ -55,10 +89,7 @@ int io_tee(struct io_kiocb *req, unsigned int issue_flags)

WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);

if (sp->flags & SPLICE_F_FD_IN_FIXED)
in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags);
else
in = io_file_get_normal(req, sp->splice_fd_in);
in = io_splice_get_file(req, issue_flags);
if (!in) {
ret = -EBADF;
goto done;
Expand Down Expand Up @@ -96,10 +127,7 @@ int io_splice(struct io_kiocb *req, unsigned int issue_flags)

WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);

if (sp->flags & SPLICE_F_FD_IN_FIXED)
in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags);
else
in = io_file_get_normal(req, sp->splice_fd_in);
in = io_splice_get_file(req, issue_flags);
if (!in) {
ret = -EBADF;
goto done;
Expand Down
1 change: 1 addition & 0 deletions io_uring/splice.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@
int io_tee_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
int io_tee(struct io_kiocb *req, unsigned int issue_flags);

void io_splice_cleanup(struct io_kiocb *req);
int io_splice_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
int io_splice(struct io_kiocb *req, unsigned int issue_flags);

0 comments on commit 743fb58

Please sign in to comment.