]> git.itanic.dy.fi Git - linux-stable/commitdiff
io_uring/openclose: add support for IORING_OP_FIXED_FD_INSTALL
authorJens Axboe <axboe@kernel.dk>
Fri, 8 Dec 2023 03:06:02 +0000 (20:06 -0700)
committerJens Axboe <axboe@kernel.dk>
Tue, 12 Dec 2023 14:42:57 +0000 (07:42 -0700)
io_uring can currently open/close regular files or fixed/direct
descriptors. Or you can instantiate a fixed descriptor from a regular
one, and then close the regular descriptor. But you currently can't turn
a purely fixed/direct descriptor into a regular file descriptor.

IORING_OP_FIXED_FD_INSTALL adds support for installing a direct
descriptor into the normal file table, just like receiving a file
descriptor or opening a new file would do. This is all nicely abstracted
into receive_fd(), and hence adding support for this is truly trivial.

Since direct descriptors are only usable within io_uring itself, it can
be useful to turn them into real file descriptors if they ever need to
be accessed via normal syscalls. This can either be a transitory thing,
or just a permanent transition for a given direct descriptor.

By default, new fds are installed with O_CLOEXEC set. The application
can disable O_CLOEXEC by setting IORING_FIXED_FD_NO_CLOEXEC in the
sqe->install_fd_flags member.

Suggested-by: Christian Brauner <brauner@kernel.org>
Reviewed-by: Christian Brauner <brauner@kernel.org>
Signed-off-by: Jens Axboe <axboe@kernel.dk>
include/uapi/linux/io_uring.h
io_uring/opdef.c
io_uring/openclose.c
io_uring/openclose.h

index f1c16f8177420065e7c98c3b0b00bba107146086..db4b913e6b39cc600db4aa792f26243aaa2b62ec 100644 (file)
@@ -71,6 +71,7 @@ struct io_uring_sqe {
                __u32           uring_cmd_flags;
                __u32           waitid_flags;
                __u32           futex_flags;
+               __u32           install_fd_flags;
        };
        __u64   user_data;      /* data to be passed back at completion time */
        /* pack this to avoid bogus arm OABI complaints */
@@ -253,6 +254,7 @@ enum io_uring_op {
        IORING_OP_FUTEX_WAIT,
        IORING_OP_FUTEX_WAKE,
        IORING_OP_FUTEX_WAITV,
+       IORING_OP_FIXED_FD_INSTALL,
 
        /* this goes last, obviously */
        IORING_OP_LAST,
@@ -386,6 +388,13 @@ enum {
 /* Pass through the flags from sqe->file_index to cqe->flags */
 #define IORING_MSG_RING_FLAGS_PASS     (1U << 1)
 
+/*
+ * IORING_OP_FIXED_FD_INSTALL flags (sqe->install_fd_flags)
+ *
+ * IORING_FIXED_FD_NO_CLOEXEC  Don't mark the fd as O_CLOEXEC
+ */
+#define IORING_FIXED_FD_NO_CLOEXEC     (1U << 0)
+
 /*
  * IO completion data structure (Completion Queue Entry)
  */
index 799db44283c7e3782ad451f5ad417547e09d486c..6705634e5f52aa625b797f3c7931903c55130359 100644 (file)
@@ -469,6 +469,12 @@ const struct io_issue_def io_issue_defs[] = {
                .prep                   = io_eopnotsupp_prep,
 #endif
        },
+       [IORING_OP_FIXED_FD_INSTALL] = {
+               .needs_file             = 1,
+               .audit_skip             = 1,
+               .prep                   = io_install_fixed_fd_prep,
+               .issue                  = io_install_fixed_fd,
+       },
 };
 
 const struct io_cold_def io_cold_defs[] = {
@@ -704,6 +710,9 @@ const struct io_cold_def io_cold_defs[] = {
        [IORING_OP_FUTEX_WAITV] = {
                .name                   = "FUTEX_WAITV",
        },
+       [IORING_OP_FIXED_FD_INSTALL] = {
+               .name                   = "FIXED_FD_INSTALL",
+       },
 };
 
 const char *io_uring_get_opcode(u8 opcode)
index 74fc22461f48f3180121a4e710c6880dfe83974d..0fe0dd30554623edb87cd159b4ffe0c52288211a 100644 (file)
@@ -31,6 +31,11 @@ struct io_close {
        u32                             file_slot;
 };
 
+struct io_fixed_install {
+       struct file                     *file;
+       unsigned int                    o_flags;
+};
+
 static bool io_openat_force_async(struct io_open *open)
 {
        /*
@@ -254,3 +259,42 @@ int io_close(struct io_kiocb *req, unsigned int issue_flags)
        io_req_set_res(req, ret, 0);
        return IOU_OK;
 }
+
+int io_install_fixed_fd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
+{
+       struct io_fixed_install *ifi;
+       unsigned int flags;
+
+       if (sqe->off || sqe->addr || sqe->len || sqe->buf_index ||
+           sqe->splice_fd_in || sqe->addr3)
+               return -EINVAL;
+
+       /* must be a fixed file */
+       if (!(req->flags & REQ_F_FIXED_FILE))
+               return -EBADF;
+
+       flags = READ_ONCE(sqe->install_fd_flags);
+       if (flags & ~IORING_FIXED_FD_NO_CLOEXEC)
+               return -EINVAL;
+
+       /* default to O_CLOEXEC, disable if IORING_FIXED_FD_NO_CLOEXEC is set */
+       ifi = io_kiocb_to_cmd(req, struct io_fixed_install);
+       ifi->o_flags = O_CLOEXEC;
+       if (flags & IORING_FIXED_FD_NO_CLOEXEC)
+               ifi->o_flags = 0;
+
+       return 0;
+}
+
+int io_install_fixed_fd(struct io_kiocb *req, unsigned int issue_flags)
+{
+       struct io_fixed_install *ifi;
+       int ret;
+
+       ifi = io_kiocb_to_cmd(req, struct io_fixed_install);
+       ret = receive_fd(req->file, NULL, ifi->o_flags);
+       if (ret < 0)
+               req_set_fail(req);
+       io_req_set_res(req, ret, 0);
+       return IOU_OK;
+}
index 4b1c28d3a66c3f1cc70e5c5a8b8a379165f7b390..8a93c98ad0adc667844a92576d1022edd6e5fa00 100644 (file)
@@ -12,3 +12,6 @@ int io_openat2(struct io_kiocb *req, unsigned int issue_flags);
 
 int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
 int io_close(struct io_kiocb *req, unsigned int issue_flags);
+
+int io_install_fixed_fd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
+int io_install_fixed_fd(struct io_kiocb *req, unsigned int issue_flags);