io_uring/splice: open code 2nd direct file assignment
authorJens Axboe <axboe@kernel.dk>
Mon, 28 Oct 2024 14:03:04 +0000 (08:03 -0600)
committerJens Axboe <axboe@kernel.dk>
Tue, 29 Oct 2024 19:43:28 +0000 (13:43 -0600)
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>
io_uring/opdef.c
io_uring/splice.c
io_uring/splice.h

index a2be3bb..3de75ec 100644 (file)
@@ -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",
@@ -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",
index 3b659cd..e62bc64 100644 (file)
@@ -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,
@@ -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;
 }
@@ -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);
@@ -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;
@@ -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;
index 542f941..b9b2848 100644 (file)
@@ -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);