Skip to content

Commit dc18b89

Browse files
committed
io_uring/openclose: add support for IORING_OP_FIXED_FD_INSTALL
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 <[email protected]> Reviewed-by: Christian Brauner <[email protected]> Signed-off-by: Jens Axboe <[email protected]>
1 parent 055c156 commit dc18b89

File tree

4 files changed

+65
-0
lines changed

4 files changed

+65
-0
lines changed

Diff for: include/uapi/linux/io_uring.h

+9
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ struct io_uring_sqe {
7171
__u32 uring_cmd_flags;
7272
__u32 waitid_flags;
7373
__u32 futex_flags;
74+
__u32 install_fd_flags;
7475
};
7576
__u64 user_data; /* data to be passed back at completion time */
7677
/* pack this to avoid bogus arm OABI complaints */
@@ -253,6 +254,7 @@ enum io_uring_op {
253254
IORING_OP_FUTEX_WAIT,
254255
IORING_OP_FUTEX_WAKE,
255256
IORING_OP_FUTEX_WAITV,
257+
IORING_OP_FIXED_FD_INSTALL,
256258

257259
/* this goes last, obviously */
258260
IORING_OP_LAST,
@@ -386,6 +388,13 @@ enum {
386388
/* Pass through the flags from sqe->file_index to cqe->flags */
387389
#define IORING_MSG_RING_FLAGS_PASS (1U << 1)
388390

391+
/*
392+
* IORING_OP_FIXED_FD_INSTALL flags (sqe->install_fd_flags)
393+
*
394+
* IORING_FIXED_FD_NO_CLOEXEC Don't mark the fd as O_CLOEXEC
395+
*/
396+
#define IORING_FIXED_FD_NO_CLOEXEC (1U << 0)
397+
389398
/*
390399
* IO completion data structure (Completion Queue Entry)
391400
*/

Diff for: io_uring/opdef.c

+9
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,12 @@ const struct io_issue_def io_issue_defs[] = {
469469
.prep = io_eopnotsupp_prep,
470470
#endif
471471
},
472+
[IORING_OP_FIXED_FD_INSTALL] = {
473+
.needs_file = 1,
474+
.audit_skip = 1,
475+
.prep = io_install_fixed_fd_prep,
476+
.issue = io_install_fixed_fd,
477+
},
472478
};
473479

474480
const struct io_cold_def io_cold_defs[] = {
@@ -704,6 +710,9 @@ const struct io_cold_def io_cold_defs[] = {
704710
[IORING_OP_FUTEX_WAITV] = {
705711
.name = "FUTEX_WAITV",
706712
},
713+
[IORING_OP_FIXED_FD_INSTALL] = {
714+
.name = "FIXED_FD_INSTALL",
715+
},
707716
};
708717

709718
const char *io_uring_get_opcode(u8 opcode)

Diff for: io_uring/openclose.c

+44
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ struct io_close {
3131
u32 file_slot;
3232
};
3333

34+
struct io_fixed_install {
35+
struct file *file;
36+
unsigned int o_flags;
37+
};
38+
3439
static bool io_openat_force_async(struct io_open *open)
3540
{
3641
/*
@@ -254,3 +259,42 @@ int io_close(struct io_kiocb *req, unsigned int issue_flags)
254259
io_req_set_res(req, ret, 0);
255260
return IOU_OK;
256261
}
262+
263+
int io_install_fixed_fd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe)
264+
{
265+
struct io_fixed_install *ifi;
266+
unsigned int flags;
267+
268+
if (sqe->off || sqe->addr || sqe->len || sqe->buf_index ||
269+
sqe->splice_fd_in || sqe->addr3)
270+
return -EINVAL;
271+
272+
/* must be a fixed file */
273+
if (!(req->flags & REQ_F_FIXED_FILE))
274+
return -EBADF;
275+
276+
flags = READ_ONCE(sqe->install_fd_flags);
277+
if (flags & ~IORING_FIXED_FD_NO_CLOEXEC)
278+
return -EINVAL;
279+
280+
/* default to O_CLOEXEC, disable if IORING_FIXED_FD_NO_CLOEXEC is set */
281+
ifi = io_kiocb_to_cmd(req, struct io_fixed_install);
282+
ifi->o_flags = O_CLOEXEC;
283+
if (flags & IORING_FIXED_FD_NO_CLOEXEC)
284+
ifi->o_flags = 0;
285+
286+
return 0;
287+
}
288+
289+
int io_install_fixed_fd(struct io_kiocb *req, unsigned int issue_flags)
290+
{
291+
struct io_fixed_install *ifi;
292+
int ret;
293+
294+
ifi = io_kiocb_to_cmd(req, struct io_fixed_install);
295+
ret = receive_fd(req->file, NULL, ifi->o_flags);
296+
if (ret < 0)
297+
req_set_fail(req);
298+
io_req_set_res(req, ret, 0);
299+
return IOU_OK;
300+
}

Diff for: io_uring/openclose.h

+3
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ int io_openat2(struct io_kiocb *req, unsigned int issue_flags);
1212

1313
int io_close_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
1414
int io_close(struct io_kiocb *req, unsigned int issue_flags);
15+
16+
int io_install_fixed_fd_prep(struct io_kiocb *req, const struct io_uring_sqe *sqe);
17+
int io_install_fixed_fd(struct io_kiocb *req, unsigned int issue_flags);

0 commit comments

Comments
 (0)