io_uring/splice: open code 2nd direct file assignment

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>
This commit is contained in:
Jens Axboe 2024-10-28 08:03:04 -06:00
parent aaa736b186
commit 743fb58a35
3 changed files with 39 additions and 8 deletions

View File

@ -641,6 +641,7 @@ const struct io_cold_def io_cold_defs[] = {
}, },
[IORING_OP_SPLICE] = { [IORING_OP_SPLICE] = {
.name = "SPLICE", .name = "SPLICE",
.cleanup = io_splice_cleanup,
}, },
[IORING_OP_PROVIDE_BUFFERS] = { [IORING_OP_PROVIDE_BUFFERS] = {
.name = "PROVIDE_BUFFERS", .name = "PROVIDE_BUFFERS",
@ -650,6 +651,7 @@ const struct io_cold_def io_cold_defs[] = {
}, },
[IORING_OP_TEE] = { [IORING_OP_TEE] = {
.name = "TEE", .name = "TEE",
.cleanup = io_splice_cleanup,
}, },
[IORING_OP_SHUTDOWN] = { [IORING_OP_SHUTDOWN] = {
.name = "SHUTDOWN", .name = "SHUTDOWN",

View File

@ -21,6 +21,7 @@ struct io_splice {
u64 len; u64 len;
int splice_fd_in; int splice_fd_in;
unsigned int flags; unsigned int flags;
struct io_rsrc_node *rsrc_node;
}; };
static int __io_splice_prep(struct io_kiocb *req, static int __io_splice_prep(struct io_kiocb *req,
@ -34,6 +35,7 @@ static int __io_splice_prep(struct io_kiocb *req,
if (unlikely(sp->flags & ~valid_flags)) if (unlikely(sp->flags & ~valid_flags))
return -EINVAL; return -EINVAL;
sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in); sp->splice_fd_in = READ_ONCE(sqe->splice_fd_in);
sp->rsrc_node = NULL;
req->flags |= REQ_F_FORCE_ASYNC; req->flags |= REQ_F_FORCE_ASYNC;
return 0; return 0;
} }
@ -45,6 +47,38 @@ int io_tee_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
return __io_splice_prep(req, 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) int io_tee(struct io_kiocb *req, unsigned int issue_flags)
{ {
struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice); struct io_splice *sp = io_kiocb_to_cmd(req, struct io_splice);
@ -55,10 +89,7 @@ int io_tee(struct io_kiocb *req, unsigned int issue_flags)
WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK); WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
if (sp->flags & SPLICE_F_FD_IN_FIXED) in = io_splice_get_file(req, issue_flags);
in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags);
else
in = io_file_get_normal(req, sp->splice_fd_in);
if (!in) { if (!in) {
ret = -EBADF; ret = -EBADF;
goto done; goto done;
@ -96,10 +127,7 @@ int io_splice(struct io_kiocb *req, unsigned int issue_flags)
WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK); WARN_ON_ONCE(issue_flags & IO_URING_F_NONBLOCK);
if (sp->flags & SPLICE_F_FD_IN_FIXED) in = io_splice_get_file(req, issue_flags);
in = io_file_get_fixed(req, sp->splice_fd_in, issue_flags);
else
in = io_file_get_normal(req, sp->splice_fd_in);
if (!in) { if (!in) {
ret = -EBADF; ret = -EBADF;
goto done; goto done;

View File

@ -3,5 +3,6 @@
int io_tee_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe); 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); 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_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
int io_splice(struct io_kiocb *req, unsigned int issue_flags); int io_splice(struct io_kiocb *req, unsigned int issue_flags);